dell_rbu.c 19 KB

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