dell_rbu.c 19 KB

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