firmware_class.c 18 KB

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