firmware_class.c 18 KB

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