firmware_class.c 15 KB

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