firmware_class.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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/workqueue.h>
  19. #include <linux/highmem.h>
  20. #include <linux/firmware.h>
  21. #include <linux/slab.h>
  22. #include <linux/sched.h>
  23. #define to_dev(obj) container_of(obj, struct device, kobj)
  24. MODULE_AUTHOR("Manuel Estrada Sainz");
  25. MODULE_DESCRIPTION("Multi purpose firmware loading support");
  26. MODULE_LICENSE("GPL");
  27. /* Builtin firmware support */
  28. #ifdef CONFIG_FW_LOADER
  29. extern struct builtin_fw __start_builtin_fw[];
  30. extern struct builtin_fw __end_builtin_fw[];
  31. static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
  32. {
  33. struct builtin_fw *b_fw;
  34. for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
  35. if (strcmp(name, b_fw->name) == 0) {
  36. fw->size = b_fw->size;
  37. fw->data = b_fw->data;
  38. return true;
  39. }
  40. }
  41. return false;
  42. }
  43. static bool fw_is_builtin_firmware(const struct firmware *fw)
  44. {
  45. struct builtin_fw *b_fw;
  46. for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
  47. if (fw->data == b_fw->data)
  48. return true;
  49. return false;
  50. }
  51. #else /* Module case - no builtin firmware support */
  52. static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
  53. {
  54. return false;
  55. }
  56. static inline bool fw_is_builtin_firmware(const struct firmware *fw)
  57. {
  58. return false;
  59. }
  60. #endif
  61. enum {
  62. FW_STATUS_LOADING,
  63. FW_STATUS_DONE,
  64. FW_STATUS_ABORT,
  65. };
  66. static int loading_timeout = 60; /* In seconds */
  67. static inline long firmware_loading_timeout(void)
  68. {
  69. return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
  70. }
  71. /* fw_lock could be moved to 'struct firmware_priv' but since it is just
  72. * guarding for corner cases a global lock should be OK */
  73. static DEFINE_MUTEX(fw_lock);
  74. struct firmware_priv {
  75. struct completion completion;
  76. struct firmware *fw;
  77. unsigned long status;
  78. struct page **pages;
  79. int nr_pages;
  80. int page_array_size;
  81. struct timer_list timeout;
  82. struct device dev;
  83. bool nowait;
  84. char fw_id[];
  85. };
  86. static struct firmware_priv *to_firmware_priv(struct device *dev)
  87. {
  88. return container_of(dev, struct firmware_priv, dev);
  89. }
  90. static void fw_load_abort(struct firmware_priv *fw_priv)
  91. {
  92. set_bit(FW_STATUS_ABORT, &fw_priv->status);
  93. wmb();
  94. complete(&fw_priv->completion);
  95. }
  96. static ssize_t firmware_timeout_show(struct class *class,
  97. struct class_attribute *attr,
  98. char *buf)
  99. {
  100. return sprintf(buf, "%d\n", loading_timeout);
  101. }
  102. /**
  103. * firmware_timeout_store - set number of seconds to wait for firmware
  104. * @class: device class pointer
  105. * @attr: device attribute pointer
  106. * @buf: buffer to scan for timeout value
  107. * @count: number of bytes in @buf
  108. *
  109. * Sets the number of seconds to wait for the firmware. Once
  110. * this expires an error will be returned to the driver and no
  111. * firmware will be provided.
  112. *
  113. * Note: zero means 'wait forever'.
  114. **/
  115. static ssize_t firmware_timeout_store(struct class *class,
  116. struct class_attribute *attr,
  117. const char *buf, size_t count)
  118. {
  119. loading_timeout = simple_strtol(buf, NULL, 10);
  120. if (loading_timeout < 0)
  121. loading_timeout = 0;
  122. return count;
  123. }
  124. static struct class_attribute firmware_class_attrs[] = {
  125. __ATTR(timeout, S_IWUSR | S_IRUGO,
  126. firmware_timeout_show, firmware_timeout_store),
  127. __ATTR_NULL
  128. };
  129. static void fw_dev_release(struct device *dev)
  130. {
  131. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  132. int i;
  133. for (i = 0; i < fw_priv->nr_pages; i++)
  134. __free_page(fw_priv->pages[i]);
  135. kfree(fw_priv->pages);
  136. kfree(fw_priv);
  137. module_put(THIS_MODULE);
  138. }
  139. static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
  140. {
  141. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  142. if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id))
  143. return -ENOMEM;
  144. if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
  145. return -ENOMEM;
  146. if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
  147. return -ENOMEM;
  148. return 0;
  149. }
  150. static struct class firmware_class = {
  151. .name = "firmware",
  152. .class_attrs = firmware_class_attrs,
  153. .dev_uevent = firmware_uevent,
  154. .dev_release = fw_dev_release,
  155. };
  156. static ssize_t firmware_loading_show(struct device *dev,
  157. struct device_attribute *attr, char *buf)
  158. {
  159. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  160. int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status);
  161. return sprintf(buf, "%d\n", loading);
  162. }
  163. static void firmware_free_data(const struct firmware *fw)
  164. {
  165. int i;
  166. vunmap(fw->data);
  167. if (fw->pages) {
  168. for (i = 0; i < PFN_UP(fw->size); i++)
  169. __free_page(fw->pages[i]);
  170. kfree(fw->pages);
  171. }
  172. }
  173. /* Some architectures don't have PAGE_KERNEL_RO */
  174. #ifndef PAGE_KERNEL_RO
  175. #define PAGE_KERNEL_RO PAGE_KERNEL
  176. #endif
  177. /**
  178. * firmware_loading_store - set value in the 'loading' control file
  179. * @dev: device pointer
  180. * @attr: device attribute pointer
  181. * @buf: buffer to scan for loading control value
  182. * @count: number of bytes in @buf
  183. *
  184. * The relevant values are:
  185. *
  186. * 1: Start a load, discarding any previous partial load.
  187. * 0: Conclude the load and hand the data to the driver code.
  188. * -1: Conclude the load with an error and discard any written data.
  189. **/
  190. static ssize_t firmware_loading_store(struct device *dev,
  191. struct device_attribute *attr,
  192. const char *buf, size_t count)
  193. {
  194. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  195. int loading = simple_strtol(buf, NULL, 10);
  196. int i;
  197. mutex_lock(&fw_lock);
  198. if (!fw_priv->fw)
  199. goto out;
  200. switch (loading) {
  201. case 1:
  202. firmware_free_data(fw_priv->fw);
  203. memset(fw_priv->fw, 0, sizeof(struct firmware));
  204. /* If the pages are not owned by 'struct firmware' */
  205. for (i = 0; i < fw_priv->nr_pages; i++)
  206. __free_page(fw_priv->pages[i]);
  207. kfree(fw_priv->pages);
  208. fw_priv->pages = NULL;
  209. fw_priv->page_array_size = 0;
  210. fw_priv->nr_pages = 0;
  211. set_bit(FW_STATUS_LOADING, &fw_priv->status);
  212. break;
  213. case 0:
  214. if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
  215. vunmap(fw_priv->fw->data);
  216. fw_priv->fw->data = vmap(fw_priv->pages,
  217. fw_priv->nr_pages,
  218. 0, PAGE_KERNEL_RO);
  219. if (!fw_priv->fw->data) {
  220. dev_err(dev, "%s: vmap() failed\n", __func__);
  221. goto err;
  222. }
  223. /* Pages are now owned by 'struct firmware' */
  224. fw_priv->fw->pages = fw_priv->pages;
  225. fw_priv->pages = NULL;
  226. fw_priv->page_array_size = 0;
  227. fw_priv->nr_pages = 0;
  228. complete(&fw_priv->completion);
  229. clear_bit(FW_STATUS_LOADING, &fw_priv->status);
  230. break;
  231. }
  232. /* fallthrough */
  233. default:
  234. dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
  235. /* fallthrough */
  236. case -1:
  237. err:
  238. fw_load_abort(fw_priv);
  239. break;
  240. }
  241. out:
  242. mutex_unlock(&fw_lock);
  243. return count;
  244. }
  245. static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
  246. static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
  247. struct bin_attribute *bin_attr,
  248. char *buffer, loff_t offset, size_t count)
  249. {
  250. struct device *dev = to_dev(kobj);
  251. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  252. struct firmware *fw;
  253. ssize_t ret_count;
  254. mutex_lock(&fw_lock);
  255. fw = fw_priv->fw;
  256. if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
  257. ret_count = -ENODEV;
  258. goto out;
  259. }
  260. if (offset > fw->size) {
  261. ret_count = 0;
  262. goto out;
  263. }
  264. if (count > fw->size - offset)
  265. count = fw->size - offset;
  266. ret_count = count;
  267. while (count) {
  268. void *page_data;
  269. int page_nr = offset >> PAGE_SHIFT;
  270. int page_ofs = offset & (PAGE_SIZE-1);
  271. int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
  272. page_data = kmap(fw_priv->pages[page_nr]);
  273. memcpy(buffer, page_data + page_ofs, page_cnt);
  274. kunmap(fw_priv->pages[page_nr]);
  275. buffer += page_cnt;
  276. offset += page_cnt;
  277. count -= page_cnt;
  278. }
  279. out:
  280. mutex_unlock(&fw_lock);
  281. return ret_count;
  282. }
  283. static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
  284. {
  285. int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
  286. /* If the array of pages is too small, grow it... */
  287. if (fw_priv->page_array_size < pages_needed) {
  288. int new_array_size = max(pages_needed,
  289. fw_priv->page_array_size * 2);
  290. struct page **new_pages;
  291. new_pages = kmalloc(new_array_size * sizeof(void *),
  292. GFP_KERNEL);
  293. if (!new_pages) {
  294. fw_load_abort(fw_priv);
  295. return -ENOMEM;
  296. }
  297. memcpy(new_pages, fw_priv->pages,
  298. fw_priv->page_array_size * sizeof(void *));
  299. memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
  300. (new_array_size - fw_priv->page_array_size));
  301. kfree(fw_priv->pages);
  302. fw_priv->pages = new_pages;
  303. fw_priv->page_array_size = new_array_size;
  304. }
  305. while (fw_priv->nr_pages < pages_needed) {
  306. fw_priv->pages[fw_priv->nr_pages] =
  307. alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
  308. if (!fw_priv->pages[fw_priv->nr_pages]) {
  309. fw_load_abort(fw_priv);
  310. return -ENOMEM;
  311. }
  312. fw_priv->nr_pages++;
  313. }
  314. return 0;
  315. }
  316. /**
  317. * firmware_data_write - write method for firmware
  318. * @filp: open sysfs file
  319. * @kobj: kobject for the device
  320. * @bin_attr: bin_attr structure
  321. * @buffer: buffer being written
  322. * @offset: buffer offset for write in total data store area
  323. * @count: buffer size
  324. *
  325. * Data written to the 'data' attribute will be later handed to
  326. * the driver as a firmware image.
  327. **/
  328. static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
  329. struct bin_attribute *bin_attr,
  330. char *buffer, loff_t offset, size_t count)
  331. {
  332. struct device *dev = to_dev(kobj);
  333. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  334. struct firmware *fw;
  335. ssize_t retval;
  336. if (!capable(CAP_SYS_RAWIO))
  337. return -EPERM;
  338. mutex_lock(&fw_lock);
  339. fw = fw_priv->fw;
  340. if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
  341. retval = -ENODEV;
  342. goto out;
  343. }
  344. retval = fw_realloc_buffer(fw_priv, offset + count);
  345. if (retval)
  346. goto out;
  347. retval = count;
  348. while (count) {
  349. void *page_data;
  350. int page_nr = offset >> PAGE_SHIFT;
  351. int page_ofs = offset & (PAGE_SIZE - 1);
  352. int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
  353. page_data = kmap(fw_priv->pages[page_nr]);
  354. memcpy(page_data + page_ofs, buffer, page_cnt);
  355. kunmap(fw_priv->pages[page_nr]);
  356. buffer += page_cnt;
  357. offset += page_cnt;
  358. count -= page_cnt;
  359. }
  360. fw->size = max_t(size_t, offset, fw->size);
  361. out:
  362. mutex_unlock(&fw_lock);
  363. return retval;
  364. }
  365. static struct bin_attribute firmware_attr_data = {
  366. .attr = { .name = "data", .mode = 0644 },
  367. .size = 0,
  368. .read = firmware_data_read,
  369. .write = firmware_data_write,
  370. };
  371. static void firmware_class_timeout(u_long data)
  372. {
  373. struct firmware_priv *fw_priv = (struct firmware_priv *) data;
  374. fw_load_abort(fw_priv);
  375. }
  376. static struct firmware_priv *
  377. fw_create_instance(struct firmware *firmware, const char *fw_name,
  378. struct device *device, bool uevent, bool nowait)
  379. {
  380. struct firmware_priv *fw_priv;
  381. struct device *f_dev;
  382. fw_priv = kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL);
  383. if (!fw_priv) {
  384. dev_err(device, "%s: kmalloc failed\n", __func__);
  385. return ERR_PTR(-ENOMEM);
  386. }
  387. fw_priv->fw = firmware;
  388. fw_priv->nowait = nowait;
  389. strcpy(fw_priv->fw_id, fw_name);
  390. init_completion(&fw_priv->completion);
  391. setup_timer(&fw_priv->timeout,
  392. firmware_class_timeout, (u_long) fw_priv);
  393. f_dev = &fw_priv->dev;
  394. device_initialize(f_dev);
  395. dev_set_name(f_dev, "%s", dev_name(device));
  396. f_dev->parent = device;
  397. f_dev->class = &firmware_class;
  398. return fw_priv;
  399. }
  400. static struct firmware_priv *
  401. _request_firmware_prepare(const struct firmware **firmware_p, const char *name,
  402. struct device *device, bool uevent, bool nowait)
  403. {
  404. struct firmware *firmware;
  405. struct firmware_priv *fw_priv;
  406. if (!firmware_p)
  407. return ERR_PTR(-EINVAL);
  408. *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
  409. if (!firmware) {
  410. dev_err(device, "%s: kmalloc(struct firmware) failed\n",
  411. __func__);
  412. return ERR_PTR(-ENOMEM);
  413. }
  414. if (fw_get_builtin_firmware(firmware, name)) {
  415. dev_dbg(device, "firmware: using built-in firmware %s\n", name);
  416. return NULL;
  417. }
  418. fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
  419. if (IS_ERR(fw_priv)) {
  420. release_firmware(firmware);
  421. *firmware_p = NULL;
  422. }
  423. return fw_priv;
  424. }
  425. static void _request_firmware_cleanup(const struct firmware **firmware_p)
  426. {
  427. release_firmware(*firmware_p);
  428. *firmware_p = NULL;
  429. }
  430. static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
  431. long timeout)
  432. {
  433. int retval = 0;
  434. struct device *f_dev = &fw_priv->dev;
  435. dev_set_uevent_suppress(f_dev, true);
  436. /* Need to pin this module until class device is destroyed */
  437. __module_get(THIS_MODULE);
  438. retval = device_add(f_dev);
  439. if (retval) {
  440. dev_err(f_dev, "%s: device_register failed\n", __func__);
  441. goto err_put_dev;
  442. }
  443. retval = device_create_bin_file(f_dev, &firmware_attr_data);
  444. if (retval) {
  445. dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
  446. goto err_del_dev;
  447. }
  448. retval = device_create_file(f_dev, &dev_attr_loading);
  449. if (retval) {
  450. dev_err(f_dev, "%s: device_create_file failed\n", __func__);
  451. goto err_del_bin_attr;
  452. }
  453. if (uevent) {
  454. dev_set_uevent_suppress(f_dev, false);
  455. dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_id);
  456. if (timeout != MAX_SCHEDULE_TIMEOUT)
  457. mod_timer(&fw_priv->timeout,
  458. round_jiffies_up(jiffies + timeout));
  459. kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
  460. }
  461. wait_for_completion(&fw_priv->completion);
  462. set_bit(FW_STATUS_DONE, &fw_priv->status);
  463. del_timer_sync(&fw_priv->timeout);
  464. mutex_lock(&fw_lock);
  465. if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status))
  466. retval = -ENOENT;
  467. fw_priv->fw = NULL;
  468. mutex_unlock(&fw_lock);
  469. device_remove_file(f_dev, &dev_attr_loading);
  470. err_del_bin_attr:
  471. device_remove_bin_file(f_dev, &firmware_attr_data);
  472. err_del_dev:
  473. device_del(f_dev);
  474. err_put_dev:
  475. put_device(f_dev);
  476. return retval;
  477. }
  478. /**
  479. * request_firmware: - send firmware request and wait for it
  480. * @firmware_p: pointer to firmware image
  481. * @name: name of firmware file
  482. * @device: device for which firmware is being loaded
  483. *
  484. * @firmware_p will be used to return a firmware image by the name
  485. * of @name for device @device.
  486. *
  487. * Should be called from user context where sleeping is allowed.
  488. *
  489. * @name will be used as $FIRMWARE in the uevent environment and
  490. * should be distinctive enough not to be confused with any other
  491. * firmware image for this or any other device.
  492. **/
  493. int
  494. request_firmware(const struct firmware **firmware_p, const char *name,
  495. struct device *device)
  496. {
  497. struct firmware_priv *fw_priv;
  498. int ret;
  499. fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
  500. false);
  501. if (IS_ERR_OR_NULL(fw_priv))
  502. return PTR_RET(fw_priv);
  503. ret = usermodehelper_read_trylock();
  504. if (WARN_ON(ret)) {
  505. dev_err(device, "firmware: %s will not be loaded\n", name);
  506. } else {
  507. ret = _request_firmware_load(fw_priv, true,
  508. firmware_loading_timeout());
  509. usermodehelper_read_unlock();
  510. }
  511. if (ret)
  512. _request_firmware_cleanup(firmware_p);
  513. return ret;
  514. }
  515. /**
  516. * release_firmware: - release the resource associated with a firmware image
  517. * @fw: firmware resource to release
  518. **/
  519. void release_firmware(const struct firmware *fw)
  520. {
  521. if (fw) {
  522. if (!fw_is_builtin_firmware(fw))
  523. firmware_free_data(fw);
  524. kfree(fw);
  525. }
  526. }
  527. /* Async support */
  528. struct firmware_work {
  529. struct work_struct work;
  530. struct module *module;
  531. const char *name;
  532. struct device *device;
  533. void *context;
  534. void (*cont)(const struct firmware *fw, void *context);
  535. bool uevent;
  536. };
  537. static void request_firmware_work_func(struct work_struct *work)
  538. {
  539. struct firmware_work *fw_work;
  540. const struct firmware *fw;
  541. struct firmware_priv *fw_priv;
  542. long timeout;
  543. int ret;
  544. fw_work = container_of(work, struct firmware_work, work);
  545. fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
  546. fw_work->uevent, true);
  547. if (IS_ERR_OR_NULL(fw_priv)) {
  548. ret = PTR_RET(fw_priv);
  549. goto out;
  550. }
  551. timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
  552. if (timeout) {
  553. ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
  554. usermodehelper_read_unlock();
  555. } else {
  556. dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
  557. fw_work->name);
  558. ret = -EAGAIN;
  559. }
  560. if (ret)
  561. _request_firmware_cleanup(&fw);
  562. out:
  563. fw_work->cont(fw, fw_work->context);
  564. module_put(fw_work->module);
  565. kfree(fw_work);
  566. }
  567. /**
  568. * request_firmware_nowait - asynchronous version of request_firmware
  569. * @module: module requesting the firmware
  570. * @uevent: sends uevent to copy the firmware image if this flag
  571. * is non-zero else the firmware copy must be done manually.
  572. * @name: name of firmware file
  573. * @device: device for which firmware is being loaded
  574. * @gfp: allocation flags
  575. * @context: will be passed over to @cont, and
  576. * @fw may be %NULL if firmware request fails.
  577. * @cont: function will be called asynchronously when the firmware
  578. * request is over.
  579. *
  580. * Asynchronous variant of request_firmware() for user contexts where
  581. * it is not possible to sleep for long time. It can't be called
  582. * in atomic contexts.
  583. **/
  584. int
  585. request_firmware_nowait(
  586. struct module *module, bool uevent,
  587. const char *name, struct device *device, gfp_t gfp, void *context,
  588. void (*cont)(const struct firmware *fw, void *context))
  589. {
  590. struct firmware_work *fw_work;
  591. fw_work = kzalloc(sizeof (struct firmware_work), gfp);
  592. if (!fw_work)
  593. return -ENOMEM;
  594. fw_work->module = module;
  595. fw_work->name = name;
  596. fw_work->device = device;
  597. fw_work->context = context;
  598. fw_work->cont = cont;
  599. fw_work->uevent = uevent;
  600. if (!try_module_get(module)) {
  601. kfree(fw_work);
  602. return -EFAULT;
  603. }
  604. INIT_WORK(&fw_work->work, request_firmware_work_func);
  605. schedule_work(&fw_work->work);
  606. return 0;
  607. }
  608. static int __init firmware_class_init(void)
  609. {
  610. return class_register(&firmware_class);
  611. }
  612. static void __exit firmware_class_exit(void)
  613. {
  614. class_unregister(&firmware_class);
  615. }
  616. fs_initcall(firmware_class_init);
  617. module_exit(firmware_class_exit);
  618. EXPORT_SYMBOL(release_firmware);
  619. EXPORT_SYMBOL(request_firmware);
  620. EXPORT_SYMBOL(request_firmware_nowait);