firmware_class.c 15 KB

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