firmware_class.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. * firmware_class.c - Multi purpose firmware loading support
  3. *
  4. * Copyright (c) 2003 Manuel Estrada Sainz <ranty@debian.org>
  5. *
  6. * Please see Documentation/firmware_class/ for more information.
  7. *
  8. */
  9. #include <linux/capability.h>
  10. #include <linux/device.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/timer.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/bitops.h>
  17. #include <asm/semaphore.h>
  18. #include <linux/firmware.h>
  19. #include "base.h"
  20. MODULE_AUTHOR("Manuel Estrada Sainz <ranty@debian.org>");
  21. MODULE_DESCRIPTION("Multi purpose firmware loading support");
  22. MODULE_LICENSE("GPL");
  23. enum {
  24. FW_STATUS_LOADING,
  25. FW_STATUS_DONE,
  26. FW_STATUS_ABORT,
  27. FW_STATUS_READY,
  28. FW_STATUS_READY_NOHOTPLUG,
  29. };
  30. static int loading_timeout = 10; /* In seconds */
  31. /* fw_lock could be moved to 'struct firmware_priv' but since it is just
  32. * guarding for corner cases a global lock should be OK */
  33. static DECLARE_MUTEX(fw_lock);
  34. struct firmware_priv {
  35. char fw_id[FIRMWARE_NAME_MAX];
  36. struct completion completion;
  37. struct bin_attribute attr_data;
  38. struct firmware *fw;
  39. unsigned long status;
  40. int alloc_size;
  41. struct timer_list timeout;
  42. };
  43. static void
  44. fw_load_abort(struct firmware_priv *fw_priv)
  45. {
  46. set_bit(FW_STATUS_ABORT, &fw_priv->status);
  47. wmb();
  48. complete(&fw_priv->completion);
  49. }
  50. static ssize_t
  51. firmware_timeout_show(struct class *class, char *buf)
  52. {
  53. return sprintf(buf, "%d\n", loading_timeout);
  54. }
  55. /**
  56. * firmware_timeout_store - set number of seconds to wait for firmware
  57. * @class: device class pointer
  58. * @buf: buffer to scan for timeout value
  59. * @count: number of bytes in @buf
  60. *
  61. * Sets the number of seconds to wait for the firmware. Once
  62. * this expires an error will be returned to the driver and no
  63. * firmware will be provided.
  64. *
  65. * Note: zero means 'wait forever'.
  66. **/
  67. static ssize_t
  68. firmware_timeout_store(struct class *class, const char *buf, size_t count)
  69. {
  70. loading_timeout = simple_strtol(buf, NULL, 10);
  71. if (loading_timeout < 0)
  72. loading_timeout = 0;
  73. return count;
  74. }
  75. static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
  76. static void fw_class_dev_release(struct class_device *class_dev);
  77. int firmware_class_uevent(struct class_device *dev, char **envp,
  78. int num_envp, char *buffer, int buffer_size);
  79. static struct class firmware_class = {
  80. .name = "firmware",
  81. .uevent = firmware_class_uevent,
  82. .release = fw_class_dev_release,
  83. };
  84. int
  85. firmware_class_uevent(struct class_device *class_dev, char **envp,
  86. int num_envp, char *buffer, int buffer_size)
  87. {
  88. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  89. int i = 0, len = 0;
  90. if (!test_bit(FW_STATUS_READY, &fw_priv->status))
  91. return -ENODEV;
  92. if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
  93. "FIRMWARE=%s", fw_priv->fw_id))
  94. return -ENOMEM;
  95. if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
  96. "TIMEOUT=%i", loading_timeout))
  97. return -ENOMEM;
  98. envp[i] = NULL;
  99. return 0;
  100. }
  101. static ssize_t
  102. firmware_loading_show(struct class_device *class_dev, char *buf)
  103. {
  104. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  105. int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
  106. return sprintf(buf, "%d\n", loading);
  107. }
  108. /**
  109. * firmware_loading_store - set value in the 'loading' control file
  110. * @class_dev: class_device pointer
  111. * @buf: buffer to scan for loading control value
  112. * @count: number of bytes in @buf
  113. *
  114. * The relevant values are:
  115. *
  116. * 1: Start a load, discarding any previous partial load.
  117. * 0: Conclude the load and hand the data to the driver code.
  118. * -1: Conclude the load with an error and discard any written data.
  119. **/
  120. static ssize_t
  121. firmware_loading_store(struct class_device *class_dev,
  122. const char *buf, size_t count)
  123. {
  124. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  125. int loading = simple_strtol(buf, NULL, 10);
  126. switch (loading) {
  127. case 1:
  128. down(&fw_lock);
  129. if (!fw_priv->fw) {
  130. up(&fw_lock);
  131. break;
  132. }
  133. vfree(fw_priv->fw->data);
  134. fw_priv->fw->data = NULL;
  135. fw_priv->fw->size = 0;
  136. fw_priv->alloc_size = 0;
  137. set_bit(FW_STATUS_LOADING, &fw_priv->status);
  138. up(&fw_lock);
  139. break;
  140. case 0:
  141. if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
  142. complete(&fw_priv->completion);
  143. clear_bit(FW_STATUS_LOADING, &fw_priv->status);
  144. break;
  145. }
  146. /* fallthrough */
  147. default:
  148. printk(KERN_ERR "%s: unexpected value (%d)\n", __FUNCTION__,
  149. loading);
  150. /* fallthrough */
  151. case -1:
  152. fw_load_abort(fw_priv);
  153. break;
  154. }
  155. return count;
  156. }
  157. static CLASS_DEVICE_ATTR(loading, 0644,
  158. firmware_loading_show, firmware_loading_store);
  159. static ssize_t
  160. firmware_data_read(struct kobject *kobj,
  161. char *buffer, loff_t offset, size_t count)
  162. {
  163. struct class_device *class_dev = to_class_dev(kobj);
  164. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  165. struct firmware *fw;
  166. ssize_t ret_count = count;
  167. down(&fw_lock);
  168. fw = fw_priv->fw;
  169. if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
  170. ret_count = -ENODEV;
  171. goto out;
  172. }
  173. if (offset > fw->size) {
  174. ret_count = 0;
  175. goto out;
  176. }
  177. if (offset + ret_count > fw->size)
  178. ret_count = fw->size - offset;
  179. memcpy(buffer, fw->data + offset, ret_count);
  180. out:
  181. up(&fw_lock);
  182. return ret_count;
  183. }
  184. static int
  185. fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
  186. {
  187. u8 *new_data;
  188. int new_size = fw_priv->alloc_size;
  189. if (min_size <= fw_priv->alloc_size)
  190. return 0;
  191. new_size = ALIGN(min_size, PAGE_SIZE);
  192. new_data = vmalloc(new_size);
  193. if (!new_data) {
  194. printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
  195. /* Make sure that we don't keep incomplete data */
  196. fw_load_abort(fw_priv);
  197. return -ENOMEM;
  198. }
  199. fw_priv->alloc_size = new_size;
  200. if (fw_priv->fw->data) {
  201. memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
  202. vfree(fw_priv->fw->data);
  203. }
  204. fw_priv->fw->data = new_data;
  205. BUG_ON(min_size > fw_priv->alloc_size);
  206. return 0;
  207. }
  208. /**
  209. * firmware_data_write - write method for firmware
  210. * @kobj: kobject for the class_device
  211. * @buffer: buffer being written
  212. * @offset: buffer offset for write in total data store area
  213. * @count: buffer size
  214. *
  215. * Data written to the 'data' attribute will be later handed to
  216. * the driver as a firmware image.
  217. **/
  218. static ssize_t
  219. firmware_data_write(struct kobject *kobj,
  220. char *buffer, loff_t offset, size_t count)
  221. {
  222. struct class_device *class_dev = to_class_dev(kobj);
  223. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  224. struct firmware *fw;
  225. ssize_t retval;
  226. if (!capable(CAP_SYS_RAWIO))
  227. return -EPERM;
  228. down(&fw_lock);
  229. fw = fw_priv->fw;
  230. if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
  231. retval = -ENODEV;
  232. goto out;
  233. }
  234. retval = fw_realloc_buffer(fw_priv, offset + count);
  235. if (retval)
  236. goto out;
  237. memcpy(fw->data + offset, buffer, count);
  238. fw->size = max_t(size_t, offset + count, fw->size);
  239. retval = count;
  240. out:
  241. up(&fw_lock);
  242. return retval;
  243. }
  244. static struct bin_attribute firmware_attr_data_tmpl = {
  245. .attr = {.name = "data", .mode = 0644, .owner = THIS_MODULE},
  246. .size = 0,
  247. .read = firmware_data_read,
  248. .write = firmware_data_write,
  249. };
  250. static void
  251. fw_class_dev_release(struct class_device *class_dev)
  252. {
  253. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  254. kfree(fw_priv);
  255. kfree(class_dev);
  256. module_put(THIS_MODULE);
  257. }
  258. static void
  259. firmware_class_timeout(u_long data)
  260. {
  261. struct firmware_priv *fw_priv = (struct firmware_priv *) data;
  262. fw_load_abort(fw_priv);
  263. }
  264. static inline void
  265. fw_setup_class_device_id(struct class_device *class_dev, struct device *dev)
  266. {
  267. /* XXX warning we should watch out for name collisions */
  268. strlcpy(class_dev->class_id, dev->bus_id, BUS_ID_SIZE);
  269. }
  270. static int
  271. fw_register_class_device(struct class_device **class_dev_p,
  272. const char *fw_name, struct device *device)
  273. {
  274. int retval;
  275. struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
  276. GFP_KERNEL);
  277. struct class_device *class_dev = kzalloc(sizeof(*class_dev),
  278. GFP_KERNEL);
  279. *class_dev_p = NULL;
  280. if (!fw_priv || !class_dev) {
  281. printk(KERN_ERR "%s: kmalloc failed\n", __FUNCTION__);
  282. retval = -ENOMEM;
  283. goto error_kfree;
  284. }
  285. init_completion(&fw_priv->completion);
  286. fw_priv->attr_data = firmware_attr_data_tmpl;
  287. strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
  288. fw_priv->timeout.function = firmware_class_timeout;
  289. fw_priv->timeout.data = (u_long) fw_priv;
  290. init_timer(&fw_priv->timeout);
  291. fw_setup_class_device_id(class_dev, device);
  292. class_dev->dev = device;
  293. class_dev->class = &firmware_class;
  294. class_set_devdata(class_dev, fw_priv);
  295. retval = class_device_register(class_dev);
  296. if (retval) {
  297. printk(KERN_ERR "%s: class_device_register failed\n",
  298. __FUNCTION__);
  299. goto error_kfree;
  300. }
  301. *class_dev_p = class_dev;
  302. return 0;
  303. error_kfree:
  304. kfree(fw_priv);
  305. kfree(class_dev);
  306. return retval;
  307. }
  308. static int
  309. fw_setup_class_device(struct firmware *fw, struct class_device **class_dev_p,
  310. const char *fw_name, struct device *device, int uevent)
  311. {
  312. struct class_device *class_dev;
  313. struct firmware_priv *fw_priv;
  314. int retval;
  315. *class_dev_p = NULL;
  316. retval = fw_register_class_device(&class_dev, fw_name, device);
  317. if (retval)
  318. goto out;
  319. /* Need to pin this module until class device is destroyed */
  320. __module_get(THIS_MODULE);
  321. fw_priv = class_get_devdata(class_dev);
  322. fw_priv->fw = fw;
  323. retval = sysfs_create_bin_file(&class_dev->kobj, &fw_priv->attr_data);
  324. if (retval) {
  325. printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
  326. __FUNCTION__);
  327. goto error_unreg;
  328. }
  329. retval = class_device_create_file(class_dev,
  330. &class_device_attr_loading);
  331. if (retval) {
  332. printk(KERN_ERR "%s: class_device_create_file failed\n",
  333. __FUNCTION__);
  334. goto error_unreg;
  335. }
  336. if (uevent)
  337. set_bit(FW_STATUS_READY, &fw_priv->status);
  338. else
  339. set_bit(FW_STATUS_READY_NOHOTPLUG, &fw_priv->status);
  340. *class_dev_p = class_dev;
  341. goto out;
  342. error_unreg:
  343. class_device_unregister(class_dev);
  344. out:
  345. return retval;
  346. }
  347. static int
  348. _request_firmware(const struct firmware **firmware_p, const char *name,
  349. struct device *device, int uevent)
  350. {
  351. struct class_device *class_dev;
  352. struct firmware_priv *fw_priv;
  353. struct firmware *firmware;
  354. int retval;
  355. if (!firmware_p)
  356. return -EINVAL;
  357. *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
  358. if (!firmware) {
  359. printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n",
  360. __FUNCTION__);
  361. retval = -ENOMEM;
  362. goto out;
  363. }
  364. retval = fw_setup_class_device(firmware, &class_dev, name, device,
  365. uevent);
  366. if (retval)
  367. goto error_kfree_fw;
  368. fw_priv = class_get_devdata(class_dev);
  369. if (uevent) {
  370. if (loading_timeout > 0) {
  371. fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
  372. add_timer(&fw_priv->timeout);
  373. }
  374. kobject_uevent(&class_dev->kobj, KOBJ_ADD);
  375. wait_for_completion(&fw_priv->completion);
  376. set_bit(FW_STATUS_DONE, &fw_priv->status);
  377. del_timer_sync(&fw_priv->timeout);
  378. } else
  379. wait_for_completion(&fw_priv->completion);
  380. down(&fw_lock);
  381. if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
  382. retval = -ENOENT;
  383. release_firmware(fw_priv->fw);
  384. *firmware_p = NULL;
  385. }
  386. fw_priv->fw = NULL;
  387. up(&fw_lock);
  388. class_device_unregister(class_dev);
  389. goto out;
  390. error_kfree_fw:
  391. kfree(firmware);
  392. *firmware_p = NULL;
  393. out:
  394. return retval;
  395. }
  396. /**
  397. * request_firmware: - send firmware request and wait for it
  398. * @firmware_p: pointer to firmware image
  399. * @name: name of firmware file
  400. * @device: device for which firmware is being loaded
  401. *
  402. * @firmware_p will be used to return a firmware image by the name
  403. * of @name for device @device.
  404. *
  405. * Should be called from user context where sleeping is allowed.
  406. *
  407. * @name will be used as $FIRMWARE in the uevent environment and
  408. * should be distinctive enough not to be confused with any other
  409. * firmware image for this or any other device.
  410. **/
  411. int
  412. request_firmware(const struct firmware **firmware_p, const char *name,
  413. struct device *device)
  414. {
  415. int uevent = 1;
  416. return _request_firmware(firmware_p, name, device, uevent);
  417. }
  418. /**
  419. * release_firmware: - release the resource associated with a firmware image
  420. * @fw: firmware resource to release
  421. **/
  422. void
  423. release_firmware(const struct firmware *fw)
  424. {
  425. if (fw) {
  426. vfree(fw->data);
  427. kfree(fw);
  428. }
  429. }
  430. /**
  431. * register_firmware: - provide a firmware image for later usage
  432. * @name: name of firmware image file
  433. * @data: buffer pointer for the firmware image
  434. * @size: size of the data buffer area
  435. *
  436. * Make sure that @data will be available by requesting firmware @name.
  437. *
  438. * Note: This will not be possible until some kind of persistence
  439. * is available.
  440. **/
  441. void
  442. register_firmware(const char *name, const u8 *data, size_t size)
  443. {
  444. /* This is meaningless without firmware caching, so until we
  445. * decide if firmware caching is reasonable just leave it as a
  446. * noop */
  447. }
  448. /* Async support */
  449. struct firmware_work {
  450. struct work_struct work;
  451. struct module *module;
  452. const char *name;
  453. struct device *device;
  454. void *context;
  455. void (*cont)(const struct firmware *fw, void *context);
  456. int uevent;
  457. };
  458. static int
  459. request_firmware_work_func(void *arg)
  460. {
  461. struct firmware_work *fw_work = arg;
  462. const struct firmware *fw;
  463. int ret;
  464. if (!arg) {
  465. WARN_ON(1);
  466. return 0;
  467. }
  468. daemonize("%s/%s", "firmware", fw_work->name);
  469. ret = _request_firmware(&fw, fw_work->name, fw_work->device,
  470. fw_work->uevent);
  471. if (ret < 0)
  472. fw_work->cont(NULL, fw_work->context);
  473. else {
  474. fw_work->cont(fw, fw_work->context);
  475. release_firmware(fw);
  476. }
  477. module_put(fw_work->module);
  478. kfree(fw_work);
  479. return ret;
  480. }
  481. /**
  482. * request_firmware_nowait: asynchronous version of request_firmware
  483. * @module: module requesting the firmware
  484. * @uevent: sends uevent to copy the firmware image if this flag
  485. * is non-zero else the firmware copy must be done manually.
  486. * @name: name of firmware file
  487. * @device: device for which firmware is being loaded
  488. * @context: will be passed over to @cont, and
  489. * @fw may be %NULL if firmware request fails.
  490. * @cont: function will be called asynchronously when the firmware
  491. * request is over.
  492. *
  493. * Asynchronous variant of request_firmware() for contexts where
  494. * it is not possible to sleep.
  495. **/
  496. int
  497. request_firmware_nowait(
  498. struct module *module, int uevent,
  499. const char *name, struct device *device, void *context,
  500. void (*cont)(const struct firmware *fw, void *context))
  501. {
  502. struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
  503. GFP_ATOMIC);
  504. int ret;
  505. if (!fw_work)
  506. return -ENOMEM;
  507. if (!try_module_get(module)) {
  508. kfree(fw_work);
  509. return -EFAULT;
  510. }
  511. *fw_work = (struct firmware_work) {
  512. .module = module,
  513. .name = name,
  514. .device = device,
  515. .context = context,
  516. .cont = cont,
  517. .uevent = uevent,
  518. };
  519. ret = kernel_thread(request_firmware_work_func, fw_work,
  520. CLONE_FS | CLONE_FILES);
  521. if (ret < 0) {
  522. fw_work->cont(NULL, fw_work->context);
  523. module_put(fw_work->module);
  524. kfree(fw_work);
  525. return ret;
  526. }
  527. return 0;
  528. }
  529. static int __init
  530. firmware_class_init(void)
  531. {
  532. int error;
  533. error = class_register(&firmware_class);
  534. if (error) {
  535. printk(KERN_ERR "%s: class_register failed\n", __FUNCTION__);
  536. return error;
  537. }
  538. error = class_create_file(&firmware_class, &class_attr_timeout);
  539. if (error) {
  540. printk(KERN_ERR "%s: class_create_file failed\n",
  541. __FUNCTION__);
  542. class_unregister(&firmware_class);
  543. }
  544. return error;
  545. }
  546. static void __exit
  547. firmware_class_exit(void)
  548. {
  549. class_unregister(&firmware_class);
  550. }
  551. module_init(firmware_class_init);
  552. module_exit(firmware_class_exit);
  553. EXPORT_SYMBOL(release_firmware);
  554. EXPORT_SYMBOL(request_firmware);
  555. EXPORT_SYMBOL(request_firmware_nowait);
  556. EXPORT_SYMBOL(register_firmware);