firmware_class.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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 <linux/mutex.h>
  18. #include <linux/kthread.h>
  19. #include <linux/firmware.h>
  20. #include "base.h"
  21. MODULE_AUTHOR("Manuel Estrada Sainz <ranty@debian.org>");
  22. MODULE_DESCRIPTION("Multi purpose firmware loading support");
  23. MODULE_LICENSE("GPL");
  24. enum {
  25. FW_STATUS_LOADING,
  26. FW_STATUS_DONE,
  27. FW_STATUS_ABORT,
  28. FW_STATUS_READY,
  29. FW_STATUS_READY_NOHOTPLUG,
  30. };
  31. static int loading_timeout = 10; /* In seconds */
  32. /* fw_lock could be moved to 'struct firmware_priv' but since it is just
  33. * guarding for corner cases a global lock should be OK */
  34. static DEFINE_MUTEX(fw_lock);
  35. struct firmware_priv {
  36. char fw_id[FIRMWARE_NAME_MAX];
  37. struct completion completion;
  38. struct bin_attribute attr_data;
  39. struct firmware *fw;
  40. unsigned long status;
  41. int alloc_size;
  42. struct timer_list timeout;
  43. };
  44. static void
  45. fw_load_abort(struct firmware_priv *fw_priv)
  46. {
  47. set_bit(FW_STATUS_ABORT, &fw_priv->status);
  48. wmb();
  49. complete(&fw_priv->completion);
  50. }
  51. static ssize_t
  52. firmware_timeout_show(struct class *class, char *buf)
  53. {
  54. return sprintf(buf, "%d\n", loading_timeout);
  55. }
  56. /**
  57. * firmware_timeout_store - set number of seconds to wait for firmware
  58. * @class: device class pointer
  59. * @buf: buffer to scan for timeout value
  60. * @count: number of bytes in @buf
  61. *
  62. * Sets the number of seconds to wait for the firmware. Once
  63. * this expires an error will be returned to the driver and no
  64. * firmware will be provided.
  65. *
  66. * Note: zero means 'wait forever'.
  67. **/
  68. static ssize_t
  69. firmware_timeout_store(struct class *class, const char *buf, size_t count)
  70. {
  71. loading_timeout = simple_strtol(buf, NULL, 10);
  72. if (loading_timeout < 0)
  73. loading_timeout = 0;
  74. return count;
  75. }
  76. static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
  77. static void fw_class_dev_release(struct class_device *class_dev);
  78. static int firmware_class_uevent(struct class_device *class_dev, char **envp,
  79. int num_envp, char *buffer, int buffer_size)
  80. {
  81. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  82. int i = 0, len = 0;
  83. if (!test_bit(FW_STATUS_READY, &fw_priv->status))
  84. return -ENODEV;
  85. if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
  86. "FIRMWARE=%s", fw_priv->fw_id))
  87. return -ENOMEM;
  88. if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
  89. "TIMEOUT=%i", loading_timeout))
  90. return -ENOMEM;
  91. envp[i] = NULL;
  92. return 0;
  93. }
  94. static struct class firmware_class = {
  95. .name = "firmware",
  96. .uevent = firmware_class_uevent,
  97. .release = fw_class_dev_release,
  98. };
  99. static ssize_t
  100. firmware_loading_show(struct class_device *class_dev, char *buf)
  101. {
  102. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  103. int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
  104. return sprintf(buf, "%d\n", loading);
  105. }
  106. /**
  107. * firmware_loading_store - set value in the 'loading' control file
  108. * @class_dev: class_device pointer
  109. * @buf: buffer to scan for loading control value
  110. * @count: number of bytes in @buf
  111. *
  112. * The relevant values are:
  113. *
  114. * 1: Start a load, discarding any previous partial load.
  115. * 0: Conclude the load and hand the data to the driver code.
  116. * -1: Conclude the load with an error and discard any written data.
  117. **/
  118. static ssize_t
  119. firmware_loading_store(struct class_device *class_dev,
  120. const char *buf, size_t count)
  121. {
  122. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  123. int loading = simple_strtol(buf, NULL, 10);
  124. switch (loading) {
  125. case 1:
  126. mutex_lock(&fw_lock);
  127. if (!fw_priv->fw) {
  128. mutex_unlock(&fw_lock);
  129. break;
  130. }
  131. vfree(fw_priv->fw->data);
  132. fw_priv->fw->data = NULL;
  133. fw_priv->fw->size = 0;
  134. fw_priv->alloc_size = 0;
  135. set_bit(FW_STATUS_LOADING, &fw_priv->status);
  136. mutex_unlock(&fw_lock);
  137. break;
  138. case 0:
  139. if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
  140. complete(&fw_priv->completion);
  141. clear_bit(FW_STATUS_LOADING, &fw_priv->status);
  142. break;
  143. }
  144. /* fallthrough */
  145. default:
  146. printk(KERN_ERR "%s: unexpected value (%d)\n", __FUNCTION__,
  147. loading);
  148. /* fallthrough */
  149. case -1:
  150. fw_load_abort(fw_priv);
  151. break;
  152. }
  153. return count;
  154. }
  155. static CLASS_DEVICE_ATTR(loading, 0644,
  156. firmware_loading_show, firmware_loading_store);
  157. static ssize_t
  158. firmware_data_read(struct kobject *kobj,
  159. char *buffer, loff_t offset, size_t count)
  160. {
  161. struct class_device *class_dev = to_class_dev(kobj);
  162. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  163. struct firmware *fw;
  164. ssize_t ret_count = count;
  165. mutex_lock(&fw_lock);
  166. fw = fw_priv->fw;
  167. if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
  168. ret_count = -ENODEV;
  169. goto out;
  170. }
  171. if (offset > fw->size) {
  172. ret_count = 0;
  173. goto out;
  174. }
  175. if (offset + ret_count > fw->size)
  176. ret_count = fw->size - offset;
  177. memcpy(buffer, fw->data + offset, ret_count);
  178. out:
  179. mutex_unlock(&fw_lock);
  180. return ret_count;
  181. }
  182. static int
  183. fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
  184. {
  185. u8 *new_data;
  186. int new_size = fw_priv->alloc_size;
  187. if (min_size <= fw_priv->alloc_size)
  188. return 0;
  189. new_size = ALIGN(min_size, PAGE_SIZE);
  190. new_data = vmalloc(new_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 = new_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. mutex_lock(&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. mutex_unlock(&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. mutex_lock(&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. mutex_unlock(&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. /* Async support */
  429. struct firmware_work {
  430. struct work_struct work;
  431. struct module *module;
  432. const char *name;
  433. struct device *device;
  434. void *context;
  435. void (*cont)(const struct firmware *fw, void *context);
  436. int uevent;
  437. };
  438. static int
  439. request_firmware_work_func(void *arg)
  440. {
  441. struct firmware_work *fw_work = arg;
  442. const struct firmware *fw;
  443. int ret;
  444. if (!arg) {
  445. WARN_ON(1);
  446. return 0;
  447. }
  448. ret = _request_firmware(&fw, fw_work->name, fw_work->device,
  449. fw_work->uevent);
  450. if (ret < 0)
  451. fw_work->cont(NULL, fw_work->context);
  452. else {
  453. fw_work->cont(fw, fw_work->context);
  454. release_firmware(fw);
  455. }
  456. module_put(fw_work->module);
  457. kfree(fw_work);
  458. return ret;
  459. }
  460. /**
  461. * request_firmware_nowait: asynchronous version of request_firmware
  462. * @module: module requesting the firmware
  463. * @uevent: sends uevent to copy the firmware image if this flag
  464. * is non-zero else the firmware copy must be done manually.
  465. * @name: name of firmware file
  466. * @device: device for which firmware is being loaded
  467. * @context: will be passed over to @cont, and
  468. * @fw may be %NULL if firmware request fails.
  469. * @cont: function will be called asynchronously when the firmware
  470. * request is over.
  471. *
  472. * Asynchronous variant of request_firmware() for contexts where
  473. * it is not possible to sleep.
  474. **/
  475. int
  476. request_firmware_nowait(
  477. struct module *module, int uevent,
  478. const char *name, struct device *device, void *context,
  479. void (*cont)(const struct firmware *fw, void *context))
  480. {
  481. struct task_struct *task;
  482. struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
  483. GFP_ATOMIC);
  484. if (!fw_work)
  485. return -ENOMEM;
  486. if (!try_module_get(module)) {
  487. kfree(fw_work);
  488. return -EFAULT;
  489. }
  490. *fw_work = (struct firmware_work) {
  491. .module = module,
  492. .name = name,
  493. .device = device,
  494. .context = context,
  495. .cont = cont,
  496. .uevent = uevent,
  497. };
  498. task = kthread_run(request_firmware_work_func, fw_work,
  499. "firmware/%s", name);
  500. if (IS_ERR(task)) {
  501. fw_work->cont(NULL, fw_work->context);
  502. module_put(fw_work->module);
  503. kfree(fw_work);
  504. return PTR_ERR(task);
  505. }
  506. return 0;
  507. }
  508. static int __init
  509. firmware_class_init(void)
  510. {
  511. int error;
  512. error = class_register(&firmware_class);
  513. if (error) {
  514. printk(KERN_ERR "%s: class_register failed\n", __FUNCTION__);
  515. return error;
  516. }
  517. error = class_create_file(&firmware_class, &class_attr_timeout);
  518. if (error) {
  519. printk(KERN_ERR "%s: class_create_file failed\n",
  520. __FUNCTION__);
  521. class_unregister(&firmware_class);
  522. }
  523. return error;
  524. }
  525. static void __exit
  526. firmware_class_exit(void)
  527. {
  528. class_unregister(&firmware_class);
  529. }
  530. fs_initcall(firmware_class_init);
  531. module_exit(firmware_class_exit);
  532. EXPORT_SYMBOL(release_firmware);
  533. EXPORT_SYMBOL(request_firmware);
  534. EXPORT_SYMBOL(request_firmware_nowait);