dell_rbu.c 19 KB

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