firmware_class.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. if (min_size <= fw_priv->alloc_size)
  189. return 0;
  190. new_data = vmalloc(fw_priv->alloc_size + PAGE_SIZE);
  191. if (!new_data) {
  192. printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
  193. /* Make sure that we don't keep incomplete data */
  194. fw_load_abort(fw_priv);
  195. return -ENOMEM;
  196. }
  197. fw_priv->alloc_size += PAGE_SIZE;
  198. if (fw_priv->fw->data) {
  199. memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
  200. vfree(fw_priv->fw->data);
  201. }
  202. fw_priv->fw->data = new_data;
  203. BUG_ON(min_size > fw_priv->alloc_size);
  204. return 0;
  205. }
  206. /**
  207. * firmware_data_write - write method for firmware
  208. * @kobj: kobject for the class_device
  209. * @buffer: buffer being written
  210. * @offset: buffer offset for write in total data store area
  211. * @count: buffer size
  212. *
  213. * Data written to the 'data' attribute will be later handed to
  214. * the driver as a firmware image.
  215. **/
  216. static ssize_t
  217. firmware_data_write(struct kobject *kobj,
  218. char *buffer, loff_t offset, size_t count)
  219. {
  220. struct class_device *class_dev = to_class_dev(kobj);
  221. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  222. struct firmware *fw;
  223. ssize_t retval;
  224. if (!capable(CAP_SYS_RAWIO))
  225. return -EPERM;
  226. down(&fw_lock);
  227. fw = fw_priv->fw;
  228. if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
  229. retval = -ENODEV;
  230. goto out;
  231. }
  232. retval = fw_realloc_buffer(fw_priv, offset + count);
  233. if (retval)
  234. goto out;
  235. memcpy(fw->data + offset, buffer, count);
  236. fw->size = max_t(size_t, offset + count, fw->size);
  237. retval = count;
  238. out:
  239. up(&fw_lock);
  240. return retval;
  241. }
  242. static struct bin_attribute firmware_attr_data_tmpl = {
  243. .attr = {.name = "data", .mode = 0644, .owner = THIS_MODULE},
  244. .size = 0,
  245. .read = firmware_data_read,
  246. .write = firmware_data_write,
  247. };
  248. static void
  249. fw_class_dev_release(struct class_device *class_dev)
  250. {
  251. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  252. kfree(fw_priv);
  253. kfree(class_dev);
  254. module_put(THIS_MODULE);
  255. }
  256. static void
  257. firmware_class_timeout(u_long data)
  258. {
  259. struct firmware_priv *fw_priv = (struct firmware_priv *) data;
  260. fw_load_abort(fw_priv);
  261. }
  262. static inline void
  263. fw_setup_class_device_id(struct class_device *class_dev, struct device *dev)
  264. {
  265. /* XXX warning we should watch out for name collisions */
  266. strlcpy(class_dev->class_id, dev->bus_id, BUS_ID_SIZE);
  267. }
  268. static int
  269. fw_register_class_device(struct class_device **class_dev_p,
  270. const char *fw_name, struct device *device)
  271. {
  272. int retval;
  273. struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
  274. GFP_KERNEL);
  275. struct class_device *class_dev = kzalloc(sizeof(*class_dev),
  276. GFP_KERNEL);
  277. *class_dev_p = NULL;
  278. if (!fw_priv || !class_dev) {
  279. printk(KERN_ERR "%s: kmalloc failed\n", __FUNCTION__);
  280. retval = -ENOMEM;
  281. goto error_kfree;
  282. }
  283. init_completion(&fw_priv->completion);
  284. fw_priv->attr_data = firmware_attr_data_tmpl;
  285. strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
  286. fw_priv->timeout.function = firmware_class_timeout;
  287. fw_priv->timeout.data = (u_long) fw_priv;
  288. init_timer(&fw_priv->timeout);
  289. fw_setup_class_device_id(class_dev, device);
  290. class_dev->dev = device;
  291. class_dev->class = &firmware_class;
  292. class_set_devdata(class_dev, fw_priv);
  293. retval = class_device_register(class_dev);
  294. if (retval) {
  295. printk(KERN_ERR "%s: class_device_register failed\n",
  296. __FUNCTION__);
  297. goto error_kfree;
  298. }
  299. *class_dev_p = class_dev;
  300. return 0;
  301. error_kfree:
  302. kfree(fw_priv);
  303. kfree(class_dev);
  304. return retval;
  305. }
  306. static int
  307. fw_setup_class_device(struct firmware *fw, struct class_device **class_dev_p,
  308. const char *fw_name, struct device *device, int uevent)
  309. {
  310. struct class_device *class_dev;
  311. struct firmware_priv *fw_priv;
  312. int retval;
  313. *class_dev_p = NULL;
  314. retval = fw_register_class_device(&class_dev, fw_name, device);
  315. if (retval)
  316. goto out;
  317. /* Need to pin this module until class device is destroyed */
  318. __module_get(THIS_MODULE);
  319. fw_priv = class_get_devdata(class_dev);
  320. fw_priv->fw = fw;
  321. retval = sysfs_create_bin_file(&class_dev->kobj, &fw_priv->attr_data);
  322. if (retval) {
  323. printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
  324. __FUNCTION__);
  325. goto error_unreg;
  326. }
  327. retval = class_device_create_file(class_dev,
  328. &class_device_attr_loading);
  329. if (retval) {
  330. printk(KERN_ERR "%s: class_device_create_file failed\n",
  331. __FUNCTION__);
  332. goto error_unreg;
  333. }
  334. if (uevent)
  335. set_bit(FW_STATUS_READY, &fw_priv->status);
  336. else
  337. set_bit(FW_STATUS_READY_NOHOTPLUG, &fw_priv->status);
  338. *class_dev_p = class_dev;
  339. goto out;
  340. error_unreg:
  341. class_device_unregister(class_dev);
  342. out:
  343. return retval;
  344. }
  345. static int
  346. _request_firmware(const struct firmware **firmware_p, const char *name,
  347. struct device *device, int uevent)
  348. {
  349. struct class_device *class_dev;
  350. struct firmware_priv *fw_priv;
  351. struct firmware *firmware;
  352. int retval;
  353. if (!firmware_p)
  354. return -EINVAL;
  355. *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
  356. if (!firmware) {
  357. printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n",
  358. __FUNCTION__);
  359. retval = -ENOMEM;
  360. goto out;
  361. }
  362. retval = fw_setup_class_device(firmware, &class_dev, name, device,
  363. uevent);
  364. if (retval)
  365. goto error_kfree_fw;
  366. fw_priv = class_get_devdata(class_dev);
  367. if (uevent) {
  368. if (loading_timeout > 0) {
  369. fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
  370. add_timer(&fw_priv->timeout);
  371. }
  372. kobject_uevent(&class_dev->kobj, KOBJ_ADD);
  373. wait_for_completion(&fw_priv->completion);
  374. set_bit(FW_STATUS_DONE, &fw_priv->status);
  375. del_timer_sync(&fw_priv->timeout);
  376. } else
  377. wait_for_completion(&fw_priv->completion);
  378. down(&fw_lock);
  379. if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
  380. retval = -ENOENT;
  381. release_firmware(fw_priv->fw);
  382. *firmware_p = NULL;
  383. }
  384. fw_priv->fw = NULL;
  385. up(&fw_lock);
  386. class_device_unregister(class_dev);
  387. goto out;
  388. error_kfree_fw:
  389. kfree(firmware);
  390. *firmware_p = NULL;
  391. out:
  392. return retval;
  393. }
  394. /**
  395. * request_firmware: - send firmware request and wait for it
  396. * @firmware_p: pointer to firmware image
  397. * @name: name of firmware file
  398. * @device: device for which firmware is being loaded
  399. *
  400. * @firmware_p will be used to return a firmware image by the name
  401. * of @name for device @device.
  402. *
  403. * Should be called from user context where sleeping is allowed.
  404. *
  405. * @name will be used as $FIRMWARE in the uevent environment and
  406. * should be distinctive enough not to be confused with any other
  407. * firmware image for this or any other device.
  408. **/
  409. int
  410. request_firmware(const struct firmware **firmware_p, const char *name,
  411. struct device *device)
  412. {
  413. int uevent = 1;
  414. return _request_firmware(firmware_p, name, device, uevent);
  415. }
  416. /**
  417. * release_firmware: - release the resource associated with a firmware image
  418. * @fw: firmware resource to release
  419. **/
  420. void
  421. release_firmware(const struct firmware *fw)
  422. {
  423. if (fw) {
  424. vfree(fw->data);
  425. kfree(fw);
  426. }
  427. }
  428. /**
  429. * register_firmware: - provide a firmware image for later usage
  430. * @name: name of firmware image file
  431. * @data: buffer pointer for the firmware image
  432. * @size: size of the data buffer area
  433. *
  434. * Make sure that @data will be available by requesting firmware @name.
  435. *
  436. * Note: This will not be possible until some kind of persistence
  437. * is available.
  438. **/
  439. void
  440. register_firmware(const char *name, const u8 *data, size_t size)
  441. {
  442. /* This is meaningless without firmware caching, so until we
  443. * decide if firmware caching is reasonable just leave it as a
  444. * noop */
  445. }
  446. /* Async support */
  447. struct firmware_work {
  448. struct work_struct work;
  449. struct module *module;
  450. const char *name;
  451. struct device *device;
  452. void *context;
  453. void (*cont)(const struct firmware *fw, void *context);
  454. int uevent;
  455. };
  456. static int
  457. request_firmware_work_func(void *arg)
  458. {
  459. struct firmware_work *fw_work = arg;
  460. const struct firmware *fw;
  461. int ret;
  462. if (!arg) {
  463. WARN_ON(1);
  464. return 0;
  465. }
  466. daemonize("%s/%s", "firmware", fw_work->name);
  467. ret = _request_firmware(&fw, fw_work->name, fw_work->device,
  468. fw_work->uevent);
  469. if (ret < 0)
  470. fw_work->cont(NULL, fw_work->context);
  471. else {
  472. fw_work->cont(fw, fw_work->context);
  473. release_firmware(fw);
  474. }
  475. module_put(fw_work->module);
  476. kfree(fw_work);
  477. return ret;
  478. }
  479. /**
  480. * request_firmware_nowait: asynchronous version of request_firmware
  481. * @module: module requesting the firmware
  482. * @uevent: sends uevent to copy the firmware image if this flag
  483. * is non-zero else the firmware copy must be done manually.
  484. * @name: name of firmware file
  485. * @device: device for which firmware is being loaded
  486. * @context: will be passed over to @cont, and
  487. * @fw may be %NULL if firmware request fails.
  488. * @cont: function will be called asynchronously when the firmware
  489. * request is over.
  490. *
  491. * Asynchronous variant of request_firmware() for contexts where
  492. * it is not possible to sleep.
  493. **/
  494. int
  495. request_firmware_nowait(
  496. struct module *module, int uevent,
  497. const char *name, struct device *device, void *context,
  498. void (*cont)(const struct firmware *fw, void *context))
  499. {
  500. struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
  501. GFP_ATOMIC);
  502. int ret;
  503. if (!fw_work)
  504. return -ENOMEM;
  505. if (!try_module_get(module)) {
  506. kfree(fw_work);
  507. return -EFAULT;
  508. }
  509. *fw_work = (struct firmware_work) {
  510. .module = module,
  511. .name = name,
  512. .device = device,
  513. .context = context,
  514. .cont = cont,
  515. .uevent = uevent,
  516. };
  517. ret = kernel_thread(request_firmware_work_func, fw_work,
  518. CLONE_FS | CLONE_FILES);
  519. if (ret < 0) {
  520. fw_work->cont(NULL, fw_work->context);
  521. module_put(fw_work->module);
  522. kfree(fw_work);
  523. return ret;
  524. }
  525. return 0;
  526. }
  527. static int __init
  528. firmware_class_init(void)
  529. {
  530. int error;
  531. error = class_register(&firmware_class);
  532. if (error) {
  533. printk(KERN_ERR "%s: class_register failed\n", __FUNCTION__);
  534. return error;
  535. }
  536. error = class_create_file(&firmware_class, &class_attr_timeout);
  537. if (error) {
  538. printk(KERN_ERR "%s: class_create_file failed\n",
  539. __FUNCTION__);
  540. class_unregister(&firmware_class);
  541. }
  542. return error;
  543. }
  544. static void __exit
  545. firmware_class_exit(void)
  546. {
  547. class_unregister(&firmware_class);
  548. }
  549. module_init(firmware_class_init);
  550. module_exit(firmware_class_exit);
  551. EXPORT_SYMBOL(release_firmware);
  552. EXPORT_SYMBOL(request_firmware);
  553. EXPORT_SYMBOL(request_firmware_nowait);
  554. EXPORT_SYMBOL(register_firmware);