firmware_class.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. * firmware_class.c - Multi purpose firmware loading support
  3. *
  4. * Copyright (c) 2003 Manuel Estrada Sainz
  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. #define to_dev(obj) container_of(obj, struct device, kobj)
  22. MODULE_AUTHOR("Manuel Estrada Sainz");
  23. MODULE_DESCRIPTION("Multi purpose firmware loading support");
  24. MODULE_LICENSE("GPL");
  25. enum {
  26. FW_STATUS_LOADING,
  27. FW_STATUS_DONE,
  28. FW_STATUS_ABORT,
  29. };
  30. static int loading_timeout = 60; /* 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. #ifdef CONFIG_FW_LOADER
  44. extern struct builtin_fw __start_builtin_fw[];
  45. extern struct builtin_fw __end_builtin_fw[];
  46. #else /* Module case. Avoid ifdefs later; it'll all optimise out */
  47. static struct builtin_fw *__start_builtin_fw;
  48. static struct builtin_fw *__end_builtin_fw;
  49. #endif
  50. static void
  51. fw_load_abort(struct firmware_priv *fw_priv)
  52. {
  53. set_bit(FW_STATUS_ABORT, &fw_priv->status);
  54. wmb();
  55. complete(&fw_priv->completion);
  56. }
  57. static ssize_t
  58. firmware_timeout_show(struct class *class, char *buf)
  59. {
  60. return sprintf(buf, "%d\n", loading_timeout);
  61. }
  62. /**
  63. * firmware_timeout_store - set number of seconds to wait for firmware
  64. * @class: device class pointer
  65. * @buf: buffer to scan for timeout value
  66. * @count: number of bytes in @buf
  67. *
  68. * Sets the number of seconds to wait for the firmware. Once
  69. * this expires an error will be returned to the driver and no
  70. * firmware will be provided.
  71. *
  72. * Note: zero means 'wait forever'.
  73. **/
  74. static ssize_t
  75. firmware_timeout_store(struct class *class, const char *buf, size_t count)
  76. {
  77. loading_timeout = simple_strtol(buf, NULL, 10);
  78. if (loading_timeout < 0)
  79. loading_timeout = 0;
  80. return count;
  81. }
  82. static CLASS_ATTR(timeout, 0644, firmware_timeout_show, firmware_timeout_store);
  83. static void fw_dev_release(struct device *dev);
  84. static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
  85. {
  86. struct firmware_priv *fw_priv = dev_get_drvdata(dev);
  87. if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
  88. return -ENOMEM;
  89. if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
  90. return -ENOMEM;
  91. return 0;
  92. }
  93. static struct class firmware_class = {
  94. .name = "firmware",
  95. .dev_uevent = firmware_uevent,
  96. .dev_release = fw_dev_release,
  97. };
  98. static ssize_t firmware_loading_show(struct device *dev,
  99. struct device_attribute *attr, char *buf)
  100. {
  101. struct firmware_priv *fw_priv = dev_get_drvdata(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. * @dev: device pointer
  108. * @attr: device attribute 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 firmware_loading_store(struct device *dev,
  119. struct device_attribute *attr,
  120. const char *buf, size_t count)
  121. {
  122. struct firmware_priv *fw_priv = dev_get_drvdata(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", __func__,
  147. loading);
  148. /* fallthrough */
  149. case -1:
  150. fw_load_abort(fw_priv);
  151. break;
  152. }
  153. return count;
  154. }
  155. static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
  156. static ssize_t
  157. firmware_data_read(struct kobject *kobj, struct bin_attribute *bin_attr,
  158. char *buffer, loff_t offset, size_t count)
  159. {
  160. struct device *dev = to_dev(kobj);
  161. struct firmware_priv *fw_priv = dev_get_drvdata(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", __func__);
  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 device
  208. * @bin_attr: bin_attr structure
  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, struct bin_attribute *bin_attr,
  218. char *buffer, loff_t offset, size_t count)
  219. {
  220. struct device *dev = to_dev(kobj);
  221. struct firmware_priv *fw_priv = dev_get_drvdata(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((u8 *)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},
  244. .size = 0,
  245. .read = firmware_data_read,
  246. .write = firmware_data_write,
  247. };
  248. static void fw_dev_release(struct device *dev)
  249. {
  250. struct firmware_priv *fw_priv = dev_get_drvdata(dev);
  251. kfree(fw_priv);
  252. kfree(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 fw_setup_device_id(struct device *f_dev, struct device *dev)
  262. {
  263. /* XXX warning we should watch out for name collisions */
  264. strlcpy(f_dev->bus_id, dev->bus_id, BUS_ID_SIZE);
  265. }
  266. static int fw_register_device(struct device **dev_p, const char *fw_name,
  267. struct device *device)
  268. {
  269. int retval;
  270. struct firmware_priv *fw_priv = kzalloc(sizeof(*fw_priv),
  271. GFP_KERNEL);
  272. struct device *f_dev = kzalloc(sizeof(*f_dev), GFP_KERNEL);
  273. *dev_p = NULL;
  274. if (!fw_priv || !f_dev) {
  275. printk(KERN_ERR "%s: kmalloc failed\n", __func__);
  276. retval = -ENOMEM;
  277. goto error_kfree;
  278. }
  279. init_completion(&fw_priv->completion);
  280. fw_priv->attr_data = firmware_attr_data_tmpl;
  281. strlcpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
  282. fw_priv->timeout.function = firmware_class_timeout;
  283. fw_priv->timeout.data = (u_long) fw_priv;
  284. init_timer(&fw_priv->timeout);
  285. fw_setup_device_id(f_dev, device);
  286. f_dev->parent = device;
  287. f_dev->class = &firmware_class;
  288. dev_set_drvdata(f_dev, fw_priv);
  289. f_dev->uevent_suppress = 1;
  290. retval = device_register(f_dev);
  291. if (retval) {
  292. printk(KERN_ERR "%s: device_register failed\n",
  293. __func__);
  294. goto error_kfree;
  295. }
  296. *dev_p = f_dev;
  297. return 0;
  298. error_kfree:
  299. kfree(fw_priv);
  300. kfree(f_dev);
  301. return retval;
  302. }
  303. static int fw_setup_device(struct firmware *fw, struct device **dev_p,
  304. const char *fw_name, struct device *device,
  305. int uevent)
  306. {
  307. struct device *f_dev;
  308. struct firmware_priv *fw_priv;
  309. int retval;
  310. *dev_p = NULL;
  311. retval = fw_register_device(&f_dev, fw_name, device);
  312. if (retval)
  313. goto out;
  314. /* Need to pin this module until class device is destroyed */
  315. __module_get(THIS_MODULE);
  316. fw_priv = dev_get_drvdata(f_dev);
  317. fw_priv->fw = fw;
  318. retval = sysfs_create_bin_file(&f_dev->kobj, &fw_priv->attr_data);
  319. if (retval) {
  320. printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
  321. __func__);
  322. goto error_unreg;
  323. }
  324. retval = device_create_file(f_dev, &dev_attr_loading);
  325. if (retval) {
  326. printk(KERN_ERR "%s: device_create_file failed\n",
  327. __func__);
  328. goto error_unreg;
  329. }
  330. if (uevent)
  331. f_dev->uevent_suppress = 0;
  332. *dev_p = f_dev;
  333. goto out;
  334. error_unreg:
  335. device_unregister(f_dev);
  336. out:
  337. return retval;
  338. }
  339. static int
  340. _request_firmware(const struct firmware **firmware_p, const char *name,
  341. struct device *device, int uevent)
  342. {
  343. struct device *f_dev;
  344. struct firmware_priv *fw_priv;
  345. struct firmware *firmware;
  346. struct builtin_fw *builtin;
  347. int retval;
  348. if (!firmware_p)
  349. return -EINVAL;
  350. *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
  351. if (!firmware) {
  352. printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n",
  353. __func__);
  354. retval = -ENOMEM;
  355. goto out;
  356. }
  357. for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
  358. builtin++) {
  359. if (strcmp(name, builtin->name))
  360. continue;
  361. printk(KERN_INFO "firmware: using built-in firmware %s\n",
  362. name);
  363. firmware->size = builtin->size;
  364. firmware->data = builtin->data;
  365. return 0;
  366. }
  367. if (uevent)
  368. printk(KERN_INFO "firmware: requesting %s\n", name);
  369. retval = fw_setup_device(firmware, &f_dev, name, device, uevent);
  370. if (retval)
  371. goto error_kfree_fw;
  372. fw_priv = dev_get_drvdata(f_dev);
  373. if (uevent) {
  374. if (loading_timeout > 0) {
  375. fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
  376. add_timer(&fw_priv->timeout);
  377. }
  378. kobject_uevent(&f_dev->kobj, KOBJ_ADD);
  379. wait_for_completion(&fw_priv->completion);
  380. set_bit(FW_STATUS_DONE, &fw_priv->status);
  381. del_timer_sync(&fw_priv->timeout);
  382. } else
  383. wait_for_completion(&fw_priv->completion);
  384. mutex_lock(&fw_lock);
  385. if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) {
  386. retval = -ENOENT;
  387. release_firmware(fw_priv->fw);
  388. *firmware_p = NULL;
  389. }
  390. fw_priv->fw = NULL;
  391. mutex_unlock(&fw_lock);
  392. device_unregister(f_dev);
  393. goto out;
  394. error_kfree_fw:
  395. kfree(firmware);
  396. *firmware_p = NULL;
  397. out:
  398. return retval;
  399. }
  400. /**
  401. * request_firmware: - send firmware request and wait for it
  402. * @firmware_p: pointer to firmware image
  403. * @name: name of firmware file
  404. * @device: device for which firmware is being loaded
  405. *
  406. * @firmware_p will be used to return a firmware image by the name
  407. * of @name for device @device.
  408. *
  409. * Should be called from user context where sleeping is allowed.
  410. *
  411. * @name will be used as $FIRMWARE in the uevent environment and
  412. * should be distinctive enough not to be confused with any other
  413. * firmware image for this or any other device.
  414. **/
  415. int
  416. request_firmware(const struct firmware **firmware_p, const char *name,
  417. struct device *device)
  418. {
  419. int uevent = 1;
  420. return _request_firmware(firmware_p, name, device, uevent);
  421. }
  422. /**
  423. * release_firmware: - release the resource associated with a firmware image
  424. * @fw: firmware resource to release
  425. **/
  426. void
  427. release_firmware(const struct firmware *fw)
  428. {
  429. struct builtin_fw *builtin;
  430. if (fw) {
  431. for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
  432. builtin++) {
  433. if (fw->data == builtin->data)
  434. goto free_fw;
  435. }
  436. vfree(fw->data);
  437. free_fw:
  438. kfree(fw);
  439. }
  440. }
  441. /* Async support */
  442. struct firmware_work {
  443. struct work_struct work;
  444. struct module *module;
  445. const char *name;
  446. struct device *device;
  447. void *context;
  448. void (*cont)(const struct firmware *fw, void *context);
  449. int uevent;
  450. };
  451. static int
  452. request_firmware_work_func(void *arg)
  453. {
  454. struct firmware_work *fw_work = arg;
  455. const struct firmware *fw;
  456. int ret;
  457. if (!arg) {
  458. WARN_ON(1);
  459. return 0;
  460. }
  461. ret = _request_firmware(&fw, fw_work->name, fw_work->device,
  462. fw_work->uevent);
  463. if (ret < 0)
  464. fw_work->cont(NULL, fw_work->context);
  465. else {
  466. fw_work->cont(fw, fw_work->context);
  467. release_firmware(fw);
  468. }
  469. module_put(fw_work->module);
  470. kfree(fw_work);
  471. return ret;
  472. }
  473. /**
  474. * request_firmware_nowait: asynchronous version of request_firmware
  475. * @module: module requesting the firmware
  476. * @uevent: sends uevent to copy the firmware image if this flag
  477. * is non-zero else the firmware copy must be done manually.
  478. * @name: name of firmware file
  479. * @device: device for which firmware is being loaded
  480. * @context: will be passed over to @cont, and
  481. * @fw may be %NULL if firmware request fails.
  482. * @cont: function will be called asynchronously when the firmware
  483. * request is over.
  484. *
  485. * Asynchronous variant of request_firmware() for contexts where
  486. * it is not possible to sleep.
  487. **/
  488. int
  489. request_firmware_nowait(
  490. struct module *module, int uevent,
  491. const char *name, struct device *device, void *context,
  492. void (*cont)(const struct firmware *fw, void *context))
  493. {
  494. struct task_struct *task;
  495. struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
  496. GFP_ATOMIC);
  497. if (!fw_work)
  498. return -ENOMEM;
  499. if (!try_module_get(module)) {
  500. kfree(fw_work);
  501. return -EFAULT;
  502. }
  503. *fw_work = (struct firmware_work) {
  504. .module = module,
  505. .name = name,
  506. .device = device,
  507. .context = context,
  508. .cont = cont,
  509. .uevent = uevent,
  510. };
  511. task = kthread_run(request_firmware_work_func, fw_work,
  512. "firmware/%s", name);
  513. if (IS_ERR(task)) {
  514. fw_work->cont(NULL, fw_work->context);
  515. module_put(fw_work->module);
  516. kfree(fw_work);
  517. return PTR_ERR(task);
  518. }
  519. return 0;
  520. }
  521. static int __init
  522. firmware_class_init(void)
  523. {
  524. int error;
  525. error = class_register(&firmware_class);
  526. if (error) {
  527. printk(KERN_ERR "%s: class_register failed\n", __func__);
  528. return error;
  529. }
  530. error = class_create_file(&firmware_class, &class_attr_timeout);
  531. if (error) {
  532. printk(KERN_ERR "%s: class_create_file failed\n",
  533. __func__);
  534. class_unregister(&firmware_class);
  535. }
  536. return error;
  537. }
  538. static void __exit
  539. firmware_class_exit(void)
  540. {
  541. class_unregister(&firmware_class);
  542. }
  543. fs_initcall(firmware_class_init);
  544. module_exit(firmware_class_exit);
  545. EXPORT_SYMBOL(release_firmware);
  546. EXPORT_SYMBOL(request_firmware);
  547. EXPORT_SYMBOL(request_firmware_nowait);