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/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 DEFINE_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. static int firmware_class_uevent(struct class_device *class_dev, char **envp,
  78. int num_envp, char *buffer, int buffer_size)
  79. {
  80. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  81. int i = 0, len = 0;
  82. if (!test_bit(FW_STATUS_READY, &fw_priv->status))
  83. return -ENODEV;
  84. if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
  85. "FIRMWARE=%s", fw_priv->fw_id))
  86. return -ENOMEM;
  87. if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
  88. "TIMEOUT=%i", loading_timeout))
  89. return -ENOMEM;
  90. envp[i] = NULL;
  91. return 0;
  92. }
  93. static struct class firmware_class = {
  94. .name = "firmware",
  95. .uevent = firmware_class_uevent,
  96. .release = fw_class_dev_release,
  97. };
  98. static ssize_t
  99. firmware_loading_show(struct class_device *class_dev, char *buf)
  100. {
  101. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  102. int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
  103. return sprintf(buf, "%d\n", loading);
  104. }
  105. /**
  106. * firmware_loading_store - set value in the 'loading' control file
  107. * @class_dev: class_device pointer
  108. * @buf: buffer to scan for loading control value
  109. * @count: number of bytes in @buf
  110. *
  111. * The relevant values are:
  112. *
  113. * 1: Start a load, discarding any previous partial load.
  114. * 0: Conclude the load and hand the data to the driver code.
  115. * -1: Conclude the load with an error and discard any written data.
  116. **/
  117. static ssize_t
  118. firmware_loading_store(struct class_device *class_dev,
  119. const char *buf, size_t count)
  120. {
  121. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  122. int loading = simple_strtol(buf, NULL, 10);
  123. switch (loading) {
  124. case 1:
  125. mutex_lock(&fw_lock);
  126. if (!fw_priv->fw) {
  127. mutex_unlock(&fw_lock);
  128. break;
  129. }
  130. vfree(fw_priv->fw->data);
  131. fw_priv->fw->data = NULL;
  132. fw_priv->fw->size = 0;
  133. fw_priv->alloc_size = 0;
  134. set_bit(FW_STATUS_LOADING, &fw_priv->status);
  135. mutex_unlock(&fw_lock);
  136. break;
  137. case 0:
  138. if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
  139. complete(&fw_priv->completion);
  140. clear_bit(FW_STATUS_LOADING, &fw_priv->status);
  141. break;
  142. }
  143. /* fallthrough */
  144. default:
  145. printk(KERN_ERR "%s: unexpected value (%d)\n", __FUNCTION__,
  146. loading);
  147. /* fallthrough */
  148. case -1:
  149. fw_load_abort(fw_priv);
  150. break;
  151. }
  152. return count;
  153. }
  154. static CLASS_DEVICE_ATTR(loading, 0644,
  155. firmware_loading_show, firmware_loading_store);
  156. static ssize_t
  157. firmware_data_read(struct kobject *kobj,
  158. char *buffer, loff_t offset, size_t count)
  159. {
  160. struct class_device *class_dev = to_class_dev(kobj);
  161. struct firmware_priv *fw_priv = class_get_devdata(class_dev);
  162. struct firmware *fw;
  163. ssize_t ret_count = count;
  164. mutex_lock(&fw_lock);
  165. fw = fw_priv->fw;
  166. if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
  167. ret_count = -ENODEV;
  168. goto out;
  169. }
  170. if (offset > fw->size) {
  171. ret_count = 0;
  172. goto out;
  173. }
  174. if (offset + ret_count > fw->size)
  175. ret_count = fw->size - offset;
  176. memcpy(buffer, fw->data + offset, ret_count);
  177. out:
  178. mutex_unlock(&fw_lock);
  179. return ret_count;
  180. }
  181. static int
  182. fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
  183. {
  184. u8 *new_data;
  185. int new_size = fw_priv->alloc_size;
  186. if (min_size <= fw_priv->alloc_size)
  187. return 0;
  188. new_size = ALIGN(min_size, PAGE_SIZE);
  189. new_data = vmalloc(new_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 = new_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. mutex_lock(&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. mutex_unlock(&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. mutex_lock(&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. mutex_unlock(&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. /* Async support */
  428. struct firmware_work {
  429. struct work_struct work;
  430. struct module *module;
  431. const char *name;
  432. struct device *device;
  433. void *context;
  434. void (*cont)(const struct firmware *fw, void *context);
  435. int uevent;
  436. };
  437. static int
  438. request_firmware_work_func(void *arg)
  439. {
  440. struct firmware_work *fw_work = arg;
  441. const struct firmware *fw;
  442. int ret;
  443. if (!arg) {
  444. WARN_ON(1);
  445. return 0;
  446. }
  447. daemonize("%s/%s", "firmware", fw_work->name);
  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 firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
  482. GFP_ATOMIC);
  483. int ret;
  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. ret = kernel_thread(request_firmware_work_func, fw_work,
  499. CLONE_FS | CLONE_FILES);
  500. if (ret < 0) {
  501. fw_work->cont(NULL, fw_work->context);
  502. module_put(fw_work->module);
  503. kfree(fw_work);
  504. return ret;
  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);