dell_rbu.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*
  2. * dell_rbu.c
  3. * Bios Update driver for Dell systems
  4. * Author: Dell Inc
  5. * Abhay Salunke <abhay_salunke@dell.com>
  6. *
  7. * Copyright (C) 2005 Dell Inc.
  8. *
  9. * Remote BIOS Update (rbu) driver is used for updating DELL BIOS by
  10. * creating entries in the /sys file systems on Linux 2.6 and higher
  11. * kernels. The driver supports two mechanism to update the BIOS namely
  12. * contiguous and packetized. Both these methods still require having some
  13. * application to set the CMOS bit indicating the BIOS to update itself
  14. * after a reboot.
  15. *
  16. * Contiguous method:
  17. * This driver writes the incoming data in a monolithic image by allocating
  18. * contiguous physical pages large enough to accommodate the incoming BIOS
  19. * image size.
  20. *
  21. * Packetized method:
  22. * The driver writes the incoming packet image by allocating a new packet
  23. * on every time the packet data is written. This driver requires an
  24. * application to break the BIOS image in to fixed sized packet chunks.
  25. *
  26. * See Documentation/dell_rbu.txt for more info.
  27. *
  28. * This program is free software; you can redistribute it and/or modify
  29. * it under the terms of the GNU General Public License v2.0 as published by
  30. * the Free Software Foundation
  31. *
  32. * This program is distributed in the hope that it will be useful,
  33. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35. * GNU General Public License for more details.
  36. */
  37. #include <linux/init.h>
  38. #include <linux/module.h>
  39. #include <linux/slab.h>
  40. #include <linux/string.h>
  41. #include <linux/errno.h>
  42. #include <linux/blkdev.h>
  43. #include <linux/platform_device.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/moduleparam.h>
  46. #include <linux/firmware.h>
  47. #include <linux/dma-mapping.h>
  48. MODULE_AUTHOR("Abhay Salunke <abhay_salunke@dell.com>");
  49. MODULE_DESCRIPTION("Driver for updating BIOS image on DELL systems");
  50. MODULE_LICENSE("GPL");
  51. MODULE_VERSION("3.2");
  52. #define BIOS_SCAN_LIMIT 0xffffffff
  53. #define MAX_IMAGE_LENGTH 16
  54. static struct _rbu_data {
  55. void *image_update_buffer;
  56. unsigned long image_update_buffer_size;
  57. unsigned long bios_image_size;
  58. int image_update_ordernum;
  59. int dma_alloc;
  60. spinlock_t lock;
  61. unsigned long packet_read_count;
  62. unsigned long num_packets;
  63. unsigned long packetsize;
  64. unsigned long imagesize;
  65. int entry_created;
  66. } rbu_data;
  67. static char image_type[MAX_IMAGE_LENGTH + 1] = "mono";
  68. module_param_string(image_type, image_type, sizeof (image_type), 0);
  69. MODULE_PARM_DESC(image_type,
  70. "BIOS image type. choose- mono or packet or init");
  71. static unsigned long allocation_floor = 0x100000;
  72. module_param(allocation_floor, ulong, 0644);
  73. MODULE_PARM_DESC(allocation_floor,
  74. "Minimum address for allocations when using Packet mode");
  75. struct packet_data {
  76. struct list_head list;
  77. size_t length;
  78. void *data;
  79. int ordernum;
  80. };
  81. static struct packet_data packet_data_head;
  82. static struct platform_device *rbu_device;
  83. static int context;
  84. static dma_addr_t dell_rbu_dmaaddr;
  85. static void init_packet_head(void)
  86. {
  87. INIT_LIST_HEAD(&packet_data_head.list);
  88. rbu_data.packet_read_count = 0;
  89. rbu_data.num_packets = 0;
  90. rbu_data.packetsize = 0;
  91. rbu_data.imagesize = 0;
  92. }
  93. static int create_packet(void *data, size_t length)
  94. {
  95. struct packet_data *newpacket;
  96. int ordernum = 0;
  97. int retval = 0;
  98. unsigned int packet_array_size = 0;
  99. void **invalid_addr_packet_array = NULL;
  100. void *packet_data_temp_buf = NULL;
  101. unsigned int idx = 0;
  102. pr_debug("create_packet: entry \n");
  103. if (!rbu_data.packetsize) {
  104. pr_debug("create_packet: packetsize not specified\n");
  105. retval = -EINVAL;
  106. goto out_noalloc;
  107. }
  108. spin_unlock(&rbu_data.lock);
  109. newpacket = kzalloc(sizeof (struct packet_data), GFP_KERNEL);
  110. if (!newpacket) {
  111. printk(KERN_WARNING
  112. "dell_rbu:%s: failed to allocate new "
  113. "packet\n", __func__);
  114. retval = -ENOMEM;
  115. spin_lock(&rbu_data.lock);
  116. goto out_noalloc;
  117. }
  118. ordernum = get_order(length);
  119. /*
  120. * BIOS errata mean we cannot allocate packets below 1MB or they will
  121. * be overwritten by BIOS.
  122. *
  123. * array to temporarily hold packets
  124. * that are below the allocation floor
  125. *
  126. * NOTE: very simplistic because we only need the floor to be at 1MB
  127. * due to BIOS errata. This shouldn't be used for higher floors
  128. * or you will run out of mem trying to allocate the array.
  129. */
  130. packet_array_size = max(
  131. (unsigned int)(allocation_floor / rbu_data.packetsize),
  132. (unsigned int)1);
  133. invalid_addr_packet_array = kzalloc(packet_array_size * sizeof(void*),
  134. GFP_KERNEL);
  135. if (!invalid_addr_packet_array) {
  136. printk(KERN_WARNING
  137. "dell_rbu:%s: failed to allocate "
  138. "invalid_addr_packet_array \n",
  139. __func__);
  140. retval = -ENOMEM;
  141. spin_lock(&rbu_data.lock);
  142. goto out_alloc_packet;
  143. }
  144. while (!packet_data_temp_buf) {
  145. packet_data_temp_buf = (unsigned char *)
  146. __get_free_pages(GFP_KERNEL, ordernum);
  147. if (!packet_data_temp_buf) {
  148. printk(KERN_WARNING
  149. "dell_rbu:%s: failed to allocate new "
  150. "packet\n", __func__);
  151. retval = -ENOMEM;
  152. spin_lock(&rbu_data.lock);
  153. goto out_alloc_packet_array;
  154. }
  155. if ((unsigned long)virt_to_phys(packet_data_temp_buf)
  156. < allocation_floor) {
  157. pr_debug("packet 0x%lx below floor at 0x%lx.\n",
  158. (unsigned long)virt_to_phys(
  159. packet_data_temp_buf),
  160. allocation_floor);
  161. invalid_addr_packet_array[idx++] = packet_data_temp_buf;
  162. packet_data_temp_buf = NULL;
  163. }
  164. }
  165. spin_lock(&rbu_data.lock);
  166. newpacket->data = packet_data_temp_buf;
  167. pr_debug("create_packet: newpacket at physical addr %lx\n",
  168. (unsigned long)virt_to_phys(newpacket->data));
  169. /* packets may not have fixed size */
  170. newpacket->length = length;
  171. newpacket->ordernum = ordernum;
  172. ++rbu_data.num_packets;
  173. /* initialize the newly created packet headers */
  174. INIT_LIST_HEAD(&newpacket->list);
  175. list_add_tail(&newpacket->list, &packet_data_head.list);
  176. memcpy(newpacket->data, data, length);
  177. pr_debug("create_packet: exit \n");
  178. out_alloc_packet_array:
  179. /* always free packet array */
  180. for (;idx>0;idx--) {
  181. pr_debug("freeing unused packet below floor 0x%lx.\n",
  182. (unsigned long)virt_to_phys(
  183. invalid_addr_packet_array[idx-1]));
  184. free_pages((unsigned long)invalid_addr_packet_array[idx-1],
  185. ordernum);
  186. }
  187. kfree(invalid_addr_packet_array);
  188. out_alloc_packet:
  189. /* if error, free data */
  190. if (retval)
  191. kfree(newpacket);
  192. out_noalloc:
  193. return retval;
  194. }
  195. static int packetize_data(const u8 *data, size_t length)
  196. {
  197. int rc = 0;
  198. int done = 0;
  199. int packet_length;
  200. u8 *temp;
  201. u8 *end = (u8 *) data + length;
  202. pr_debug("packetize_data: data length %zd\n", length);
  203. if (!rbu_data.packetsize) {
  204. printk(KERN_WARNING
  205. "dell_rbu: packetsize not specified\n");
  206. return -EIO;
  207. }
  208. temp = (u8 *) data;
  209. /* packetize the hunk */
  210. while (!done) {
  211. if ((temp + rbu_data.packetsize) < end)
  212. packet_length = rbu_data.packetsize;
  213. else {
  214. /* this is the last packet */
  215. packet_length = end - temp;
  216. done = 1;
  217. }
  218. if ((rc = create_packet(temp, packet_length)))
  219. return rc;
  220. pr_debug("%p:%td\n", temp, (end - temp));
  221. temp += packet_length;
  222. }
  223. rbu_data.imagesize = length;
  224. return rc;
  225. }
  226. static int do_packet_read(char *data, struct list_head *ptemp_list,
  227. int length, int bytes_read, int *list_read_count)
  228. {
  229. void *ptemp_buf;
  230. struct packet_data *newpacket = NULL;
  231. int bytes_copied = 0;
  232. int j = 0;
  233. newpacket = list_entry(ptemp_list, struct packet_data, list);
  234. *list_read_count += newpacket->length;
  235. if (*list_read_count > bytes_read) {
  236. /* point to the start of unread data */
  237. j = newpacket->length - (*list_read_count - bytes_read);
  238. /* point to the offset in the packet buffer */
  239. ptemp_buf = (u8 *) newpacket->data + j;
  240. /*
  241. * check if there is enough room in
  242. * * the incoming buffer
  243. */
  244. if (length > (*list_read_count - bytes_read))
  245. /*
  246. * copy what ever is there in this
  247. * packet and move on
  248. */
  249. bytes_copied = (*list_read_count - bytes_read);
  250. else
  251. /* copy the remaining */
  252. bytes_copied = length;
  253. memcpy(data, ptemp_buf, bytes_copied);
  254. }
  255. return bytes_copied;
  256. }
  257. static int packet_read_list(char *data, size_t * pread_length)
  258. {
  259. struct list_head *ptemp_list;
  260. int temp_count = 0;
  261. int bytes_copied = 0;
  262. int bytes_read = 0;
  263. int remaining_bytes = 0;
  264. char *pdest = data;
  265. /* check if we have any packets */
  266. if (0 == rbu_data.num_packets)
  267. return -ENOMEM;
  268. remaining_bytes = *pread_length;
  269. bytes_read = rbu_data.packet_read_count;
  270. ptemp_list = (&packet_data_head.list)->next;
  271. while (!list_empty(ptemp_list)) {
  272. bytes_copied = do_packet_read(pdest, ptemp_list,
  273. remaining_bytes, bytes_read, &temp_count);
  274. remaining_bytes -= bytes_copied;
  275. bytes_read += bytes_copied;
  276. pdest += bytes_copied;
  277. /*
  278. * check if we reached end of buffer before reaching the
  279. * last packet
  280. */
  281. if (remaining_bytes == 0)
  282. break;
  283. ptemp_list = ptemp_list->next;
  284. }
  285. /*finally set the bytes read */
  286. *pread_length = bytes_read - rbu_data.packet_read_count;
  287. rbu_data.packet_read_count = bytes_read;
  288. return 0;
  289. }
  290. static void packet_empty_list(void)
  291. {
  292. struct list_head *ptemp_list;
  293. struct list_head *pnext_list;
  294. struct packet_data *newpacket;
  295. ptemp_list = (&packet_data_head.list)->next;
  296. while (!list_empty(ptemp_list)) {
  297. newpacket =
  298. list_entry(ptemp_list, struct packet_data, list);
  299. pnext_list = ptemp_list->next;
  300. list_del(ptemp_list);
  301. ptemp_list = pnext_list;
  302. /*
  303. * zero out the RBU packet memory before freeing
  304. * to make sure there are no stale RBU packets left in memory
  305. */
  306. memset(newpacket->data, 0, rbu_data.packetsize);
  307. free_pages((unsigned long) newpacket->data,
  308. newpacket->ordernum);
  309. kfree(newpacket);
  310. }
  311. rbu_data.packet_read_count = 0;
  312. rbu_data.num_packets = 0;
  313. rbu_data.imagesize = 0;
  314. }
  315. /*
  316. * img_update_free: Frees the buffer allocated for storing BIOS image
  317. * Always called with lock held and returned with lock held
  318. */
  319. static void img_update_free(void)
  320. {
  321. if (!rbu_data.image_update_buffer)
  322. return;
  323. /*
  324. * zero out this buffer before freeing it to get rid of any stale
  325. * BIOS image copied in memory.
  326. */
  327. memset(rbu_data.image_update_buffer, 0,
  328. rbu_data.image_update_buffer_size);
  329. if (rbu_data.dma_alloc == 1)
  330. dma_free_coherent(NULL, rbu_data.bios_image_size,
  331. rbu_data.image_update_buffer, dell_rbu_dmaaddr);
  332. else
  333. free_pages((unsigned long) rbu_data.image_update_buffer,
  334. rbu_data.image_update_ordernum);
  335. /*
  336. * Re-initialize the rbu_data variables after a free
  337. */
  338. rbu_data.image_update_ordernum = -1;
  339. rbu_data.image_update_buffer = NULL;
  340. rbu_data.image_update_buffer_size = 0;
  341. rbu_data.bios_image_size = 0;
  342. rbu_data.dma_alloc = 0;
  343. }
  344. /*
  345. * img_update_realloc: This function allocates the contiguous pages to
  346. * accommodate the requested size of data. The memory address and size
  347. * values are stored globally and on every call to this function the new
  348. * size is checked to see if more data is required than the existing size.
  349. * If true the previous memory is freed and new allocation is done to
  350. * accommodate the new size. If the incoming size is less then than the
  351. * already allocated size, then that memory is reused. This function is
  352. * called with lock held and returns with lock held.
  353. */
  354. static int img_update_realloc(unsigned long size)
  355. {
  356. unsigned char *image_update_buffer = NULL;
  357. unsigned long rc;
  358. unsigned long img_buf_phys_addr;
  359. int ordernum;
  360. int dma_alloc = 0;
  361. /*
  362. * check if the buffer of sufficient size has been
  363. * already allocated
  364. */
  365. if (rbu_data.image_update_buffer_size >= size) {
  366. /*
  367. * check for corruption
  368. */
  369. if ((size != 0) && (rbu_data.image_update_buffer == NULL)) {
  370. printk(KERN_ERR "dell_rbu:%s: corruption "
  371. "check failed\n", __func__);
  372. return -EINVAL;
  373. }
  374. /*
  375. * we have a valid pre-allocated buffer with
  376. * sufficient size
  377. */
  378. return 0;
  379. }
  380. /*
  381. * free any previously allocated buffer
  382. */
  383. img_update_free();
  384. spin_unlock(&rbu_data.lock);
  385. ordernum = get_order(size);
  386. image_update_buffer =
  387. (unsigned char *) __get_free_pages(GFP_KERNEL, ordernum);
  388. img_buf_phys_addr =
  389. (unsigned long) virt_to_phys(image_update_buffer);
  390. if (img_buf_phys_addr > BIOS_SCAN_LIMIT) {
  391. free_pages((unsigned long) image_update_buffer, ordernum);
  392. ordernum = -1;
  393. image_update_buffer = dma_alloc_coherent(NULL, size,
  394. &dell_rbu_dmaaddr, GFP_KERNEL);
  395. dma_alloc = 1;
  396. }
  397. spin_lock(&rbu_data.lock);
  398. if (image_update_buffer != NULL) {
  399. rbu_data.image_update_buffer = image_update_buffer;
  400. rbu_data.image_update_buffer_size = size;
  401. rbu_data.bios_image_size =
  402. rbu_data.image_update_buffer_size;
  403. rbu_data.image_update_ordernum = ordernum;
  404. rbu_data.dma_alloc = dma_alloc;
  405. rc = 0;
  406. } else {
  407. pr_debug("Not enough memory for image update:"
  408. "size = %ld\n", size);
  409. rc = -ENOMEM;
  410. }
  411. return rc;
  412. }
  413. static ssize_t read_packet_data(char *buffer, loff_t pos, size_t count)
  414. {
  415. int retval;
  416. size_t bytes_left;
  417. size_t data_length;
  418. char *ptempBuf = buffer;
  419. /* check to see if we have something to return */
  420. if (rbu_data.num_packets == 0) {
  421. pr_debug("read_packet_data: no packets written\n");
  422. retval = -ENOMEM;
  423. goto read_rbu_data_exit;
  424. }
  425. if (pos > rbu_data.imagesize) {
  426. retval = 0;
  427. printk(KERN_WARNING "dell_rbu:read_packet_data: "
  428. "data underrun\n");
  429. goto read_rbu_data_exit;
  430. }
  431. bytes_left = rbu_data.imagesize - pos;
  432. data_length = min(bytes_left, count);
  433. if ((retval = packet_read_list(ptempBuf, &data_length)) < 0)
  434. goto read_rbu_data_exit;
  435. if ((pos + count) > rbu_data.imagesize) {
  436. rbu_data.packet_read_count = 0;
  437. /* this was the last copy */
  438. retval = bytes_left;
  439. } else
  440. retval = count;
  441. read_rbu_data_exit:
  442. return retval;
  443. }
  444. static ssize_t read_rbu_mono_data(char *buffer, loff_t pos, size_t count)
  445. {
  446. /* check to see if we have something to return */
  447. if ((rbu_data.image_update_buffer == NULL) ||
  448. (rbu_data.bios_image_size == 0)) {
  449. pr_debug("read_rbu_data_mono: image_update_buffer %p ,"
  450. "bios_image_size %lu\n",
  451. rbu_data.image_update_buffer,
  452. rbu_data.bios_image_size);
  453. return -ENOMEM;
  454. }
  455. return memory_read_from_buffer(buffer, count, &pos,
  456. rbu_data.image_update_buffer, rbu_data.bios_image_size);
  457. }
  458. static ssize_t read_rbu_data(struct file *filp, struct kobject *kobj,
  459. struct bin_attribute *bin_attr,
  460. char *buffer, loff_t pos, size_t count)
  461. {
  462. ssize_t ret_count = 0;
  463. spin_lock(&rbu_data.lock);
  464. if (!strcmp(image_type, "mono"))
  465. ret_count = read_rbu_mono_data(buffer, pos, count);
  466. else if (!strcmp(image_type, "packet"))
  467. ret_count = read_packet_data(buffer, pos, count);
  468. else
  469. pr_debug("read_rbu_data: invalid image type specified\n");
  470. spin_unlock(&rbu_data.lock);
  471. return ret_count;
  472. }
  473. static void callbackfn_rbu(const struct firmware *fw, void *context)
  474. {
  475. rbu_data.entry_created = 0;
  476. if (!fw)
  477. return;
  478. if (!fw->size)
  479. goto out;
  480. spin_lock(&rbu_data.lock);
  481. if (!strcmp(image_type, "mono")) {
  482. if (!img_update_realloc(fw->size))
  483. memcpy(rbu_data.image_update_buffer,
  484. fw->data, fw->size);
  485. } else if (!strcmp(image_type, "packet")) {
  486. /*
  487. * we need to free previous packets if a
  488. * new hunk of packets needs to be downloaded
  489. */
  490. packet_empty_list();
  491. if (packetize_data(fw->data, fw->size))
  492. /* Incase something goes wrong when we are
  493. * in middle of packetizing the data, we
  494. * need to free up whatever packets might
  495. * have been created before we quit.
  496. */
  497. packet_empty_list();
  498. } else
  499. pr_debug("invalid image type specified.\n");
  500. spin_unlock(&rbu_data.lock);
  501. out:
  502. release_firmware(fw);
  503. }
  504. static ssize_t read_rbu_image_type(struct file *filp, struct kobject *kobj,
  505. struct bin_attribute *bin_attr,
  506. char *buffer, loff_t pos, size_t count)
  507. {
  508. int size = 0;
  509. if (!pos)
  510. size = scnprintf(buffer, count, "%s\n", image_type);
  511. return size;
  512. }
  513. static ssize_t write_rbu_image_type(struct file *filp, struct kobject *kobj,
  514. struct bin_attribute *bin_attr,
  515. char *buffer, loff_t pos, size_t count)
  516. {
  517. int rc = count;
  518. int req_firm_rc = 0;
  519. int i;
  520. spin_lock(&rbu_data.lock);
  521. /*
  522. * Find the first newline or space
  523. */
  524. for (i = 0; i < count; ++i)
  525. if (buffer[i] == '\n' || buffer[i] == ' ') {
  526. buffer[i] = '\0';
  527. break;
  528. }
  529. if (i == count)
  530. buffer[count] = '\0';
  531. if (strstr(buffer, "mono"))
  532. strcpy(image_type, "mono");
  533. else if (strstr(buffer, "packet"))
  534. strcpy(image_type, "packet");
  535. else if (strstr(buffer, "init")) {
  536. /*
  537. * If due to the user error the driver gets in a bad
  538. * state where even though it is loaded , the
  539. * /sys/class/firmware/dell_rbu entries are missing.
  540. * to cover this situation the user can recreate entries
  541. * by writing init to image_type.
  542. */
  543. if (!rbu_data.entry_created) {
  544. spin_unlock(&rbu_data.lock);
  545. req_firm_rc = request_firmware_nowait(THIS_MODULE,
  546. FW_ACTION_NOHOTPLUG, "dell_rbu",
  547. &rbu_device->dev, GFP_KERNEL, &context,
  548. callbackfn_rbu);
  549. if (req_firm_rc) {
  550. printk(KERN_ERR
  551. "dell_rbu:%s request_firmware_nowait"
  552. " failed %d\n", __func__, rc);
  553. rc = -EIO;
  554. } else
  555. rbu_data.entry_created = 1;
  556. spin_lock(&rbu_data.lock);
  557. }
  558. } else {
  559. printk(KERN_WARNING "dell_rbu: image_type is invalid\n");
  560. spin_unlock(&rbu_data.lock);
  561. return -EINVAL;
  562. }
  563. /* we must free all previous allocations */
  564. packet_empty_list();
  565. img_update_free();
  566. spin_unlock(&rbu_data.lock);
  567. return rc;
  568. }
  569. static ssize_t read_rbu_packet_size(struct file *filp, struct kobject *kobj,
  570. struct bin_attribute *bin_attr,
  571. char *buffer, loff_t pos, size_t count)
  572. {
  573. int size = 0;
  574. if (!pos) {
  575. spin_lock(&rbu_data.lock);
  576. size = scnprintf(buffer, count, "%lu\n", rbu_data.packetsize);
  577. spin_unlock(&rbu_data.lock);
  578. }
  579. return size;
  580. }
  581. static ssize_t write_rbu_packet_size(struct file *filp, struct kobject *kobj,
  582. struct bin_attribute *bin_attr,
  583. char *buffer, loff_t pos, size_t count)
  584. {
  585. unsigned long temp;
  586. spin_lock(&rbu_data.lock);
  587. packet_empty_list();
  588. sscanf(buffer, "%lu", &temp);
  589. if (temp < 0xffffffff)
  590. rbu_data.packetsize = temp;
  591. spin_unlock(&rbu_data.lock);
  592. return count;
  593. }
  594. static struct bin_attribute rbu_data_attr = {
  595. .attr = {.name = "data", .mode = 0444},
  596. .read = read_rbu_data,
  597. };
  598. static struct bin_attribute rbu_image_type_attr = {
  599. .attr = {.name = "image_type", .mode = 0644},
  600. .read = read_rbu_image_type,
  601. .write = write_rbu_image_type,
  602. };
  603. static struct bin_attribute rbu_packet_size_attr = {
  604. .attr = {.name = "packet_size", .mode = 0644},
  605. .read = read_rbu_packet_size,
  606. .write = write_rbu_packet_size,
  607. };
  608. static int __init dcdrbu_init(void)
  609. {
  610. int rc;
  611. spin_lock_init(&rbu_data.lock);
  612. init_packet_head();
  613. rbu_device = platform_device_register_simple("dell_rbu", -1, NULL, 0);
  614. if (IS_ERR(rbu_device)) {
  615. printk(KERN_ERR
  616. "dell_rbu:%s:platform_device_register_simple "
  617. "failed\n", __func__);
  618. return PTR_ERR(rbu_device);
  619. }
  620. rc = sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_data_attr);
  621. if (rc)
  622. goto out_devreg;
  623. rc = sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_image_type_attr);
  624. if (rc)
  625. goto out_data;
  626. rc = sysfs_create_bin_file(&rbu_device->dev.kobj,
  627. &rbu_packet_size_attr);
  628. if (rc)
  629. goto out_imtype;
  630. rbu_data.entry_created = 0;
  631. return 0;
  632. out_imtype:
  633. sysfs_remove_bin_file(&rbu_device->dev.kobj, &rbu_image_type_attr);
  634. out_data:
  635. sysfs_remove_bin_file(&rbu_device->dev.kobj, &rbu_data_attr);
  636. out_devreg:
  637. platform_device_unregister(rbu_device);
  638. return rc;
  639. }
  640. static __exit void dcdrbu_exit(void)
  641. {
  642. spin_lock(&rbu_data.lock);
  643. packet_empty_list();
  644. img_update_free();
  645. spin_unlock(&rbu_data.lock);
  646. platform_device_unregister(rbu_device);
  647. }
  648. module_exit(dcdrbu_exit);
  649. module_init(dcdrbu_init);
  650. /* vim:noet:ts=8:sw=8
  651. */