firmware_class.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. for (i = 0; i < fw_priv->nr_pages; i++)
  207. __free_page(fw_priv->pages[i]);
  208. kfree(fw_priv->pages);
  209. fw_priv->pages = NULL;
  210. fw_priv->page_array_size = 0;
  211. fw_priv->nr_pages = 0;
  212. set_bit(FW_STATUS_LOADING, &fw_priv->status);
  213. break;
  214. case 0:
  215. if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) {
  216. complete(&fw_priv->completion);
  217. clear_bit(FW_STATUS_LOADING, &fw_priv->status);
  218. break;
  219. }
  220. /* fallthrough */
  221. default:
  222. dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
  223. /* fallthrough */
  224. case -1:
  225. fw_load_abort(fw_priv);
  226. break;
  227. }
  228. out:
  229. mutex_unlock(&fw_lock);
  230. return count;
  231. }
  232. static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
  233. static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
  234. struct bin_attribute *bin_attr,
  235. char *buffer, loff_t offset, size_t count)
  236. {
  237. struct device *dev = kobj_to_dev(kobj);
  238. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  239. struct firmware *fw;
  240. ssize_t ret_count;
  241. mutex_lock(&fw_lock);
  242. fw = fw_priv->fw;
  243. if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
  244. ret_count = -ENODEV;
  245. goto out;
  246. }
  247. if (offset > fw_priv->size) {
  248. ret_count = 0;
  249. goto out;
  250. }
  251. if (count > fw_priv->size - offset)
  252. count = fw_priv->size - offset;
  253. ret_count = count;
  254. while (count) {
  255. void *page_data;
  256. int page_nr = offset >> PAGE_SHIFT;
  257. int page_ofs = offset & (PAGE_SIZE-1);
  258. int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
  259. page_data = kmap(fw_priv->pages[page_nr]);
  260. memcpy(buffer, page_data + page_ofs, page_cnt);
  261. kunmap(fw_priv->pages[page_nr]);
  262. buffer += page_cnt;
  263. offset += page_cnt;
  264. count -= page_cnt;
  265. }
  266. out:
  267. mutex_unlock(&fw_lock);
  268. return ret_count;
  269. }
  270. static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
  271. {
  272. int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
  273. /* If the array of pages is too small, grow it... */
  274. if (fw_priv->page_array_size < pages_needed) {
  275. int new_array_size = max(pages_needed,
  276. fw_priv->page_array_size * 2);
  277. struct page **new_pages;
  278. new_pages = kmalloc(new_array_size * sizeof(void *),
  279. GFP_KERNEL);
  280. if (!new_pages) {
  281. fw_load_abort(fw_priv);
  282. return -ENOMEM;
  283. }
  284. memcpy(new_pages, fw_priv->pages,
  285. fw_priv->page_array_size * sizeof(void *));
  286. memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
  287. (new_array_size - fw_priv->page_array_size));
  288. kfree(fw_priv->pages);
  289. fw_priv->pages = new_pages;
  290. fw_priv->page_array_size = new_array_size;
  291. }
  292. while (fw_priv->nr_pages < pages_needed) {
  293. fw_priv->pages[fw_priv->nr_pages] =
  294. alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
  295. if (!fw_priv->pages[fw_priv->nr_pages]) {
  296. fw_load_abort(fw_priv);
  297. return -ENOMEM;
  298. }
  299. fw_priv->nr_pages++;
  300. }
  301. return 0;
  302. }
  303. /**
  304. * firmware_data_write - write method for firmware
  305. * @filp: open sysfs file
  306. * @kobj: kobject for the device
  307. * @bin_attr: bin_attr structure
  308. * @buffer: buffer being written
  309. * @offset: buffer offset for write in total data store area
  310. * @count: buffer size
  311. *
  312. * Data written to the 'data' attribute will be later handed to
  313. * the driver as a firmware image.
  314. **/
  315. static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
  316. struct bin_attribute *bin_attr,
  317. char *buffer, loff_t offset, size_t count)
  318. {
  319. struct device *dev = kobj_to_dev(kobj);
  320. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  321. struct firmware *fw;
  322. ssize_t retval;
  323. if (!capable(CAP_SYS_RAWIO))
  324. return -EPERM;
  325. mutex_lock(&fw_lock);
  326. fw = fw_priv->fw;
  327. if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) {
  328. retval = -ENODEV;
  329. goto out;
  330. }
  331. retval = fw_realloc_buffer(fw_priv, offset + count);
  332. if (retval)
  333. goto out;
  334. retval = count;
  335. while (count) {
  336. void *page_data;
  337. int page_nr = offset >> PAGE_SHIFT;
  338. int page_ofs = offset & (PAGE_SIZE - 1);
  339. int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
  340. page_data = kmap(fw_priv->pages[page_nr]);
  341. memcpy(page_data + page_ofs, buffer, page_cnt);
  342. kunmap(fw_priv->pages[page_nr]);
  343. buffer += page_cnt;
  344. offset += page_cnt;
  345. count -= page_cnt;
  346. }
  347. fw_priv->size = max_t(size_t, offset, fw_priv->size);
  348. out:
  349. mutex_unlock(&fw_lock);
  350. return retval;
  351. }
  352. static struct bin_attribute firmware_attr_data = {
  353. .attr = { .name = "data", .mode = 0644 },
  354. .size = 0,
  355. .read = firmware_data_read,
  356. .write = firmware_data_write,
  357. };
  358. static void firmware_class_timeout(u_long data)
  359. {
  360. struct firmware_priv *fw_priv = (struct firmware_priv *) data;
  361. fw_load_abort(fw_priv);
  362. }
  363. static struct firmware_priv *
  364. fw_create_instance(struct firmware *firmware, const char *fw_name,
  365. struct device *device, bool uevent, bool nowait)
  366. {
  367. struct firmware_priv *fw_priv;
  368. struct device *f_dev;
  369. fw_priv = kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL);
  370. if (!fw_priv) {
  371. dev_err(device, "%s: kmalloc failed\n", __func__);
  372. return ERR_PTR(-ENOMEM);
  373. }
  374. fw_priv->fw = firmware;
  375. fw_priv->nowait = nowait;
  376. strcpy(fw_priv->fw_id, fw_name);
  377. init_completion(&fw_priv->completion);
  378. setup_timer(&fw_priv->timeout,
  379. firmware_class_timeout, (u_long) fw_priv);
  380. f_dev = &fw_priv->dev;
  381. device_initialize(f_dev);
  382. dev_set_name(f_dev, "%s", dev_name(device));
  383. f_dev->parent = device;
  384. f_dev->class = &firmware_class;
  385. return fw_priv;
  386. }
  387. static struct firmware_priv *
  388. _request_firmware_prepare(const struct firmware **firmware_p, const char *name,
  389. struct device *device, bool uevent, bool nowait)
  390. {
  391. struct firmware *firmware;
  392. struct firmware_priv *fw_priv;
  393. if (!firmware_p)
  394. return ERR_PTR(-EINVAL);
  395. *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
  396. if (!firmware) {
  397. dev_err(device, "%s: kmalloc(struct firmware) failed\n",
  398. __func__);
  399. return ERR_PTR(-ENOMEM);
  400. }
  401. if (fw_get_builtin_firmware(firmware, name)) {
  402. dev_dbg(device, "firmware: using built-in firmware %s\n", name);
  403. return NULL;
  404. }
  405. fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
  406. if (IS_ERR(fw_priv)) {
  407. release_firmware(firmware);
  408. *firmware_p = NULL;
  409. }
  410. return fw_priv;
  411. }
  412. static void _request_firmware_cleanup(const struct firmware **firmware_p)
  413. {
  414. release_firmware(*firmware_p);
  415. *firmware_p = NULL;
  416. }
  417. /* transfer the ownership of pages to firmware */
  418. static int fw_set_page_data(struct firmware_priv *fw_priv)
  419. {
  420. struct firmware *fw = fw_priv->fw;
  421. fw_priv->data = vmap(fw_priv->pages, fw_priv->nr_pages,
  422. 0, PAGE_KERNEL_RO);
  423. if (!fw_priv->data)
  424. return -ENOMEM;
  425. fw->data = fw_priv->data;
  426. fw->pages = fw_priv->pages;
  427. fw->size = fw_priv->size;
  428. WARN_ON(PFN_UP(fw->size) != fw_priv->nr_pages);
  429. fw_priv->nr_pages = 0;
  430. fw_priv->pages = NULL;
  431. fw_priv->data = NULL;
  432. return 0;
  433. }
  434. static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
  435. long timeout)
  436. {
  437. int retval = 0;
  438. struct device *f_dev = &fw_priv->dev;
  439. dev_set_uevent_suppress(f_dev, true);
  440. /* Need to pin this module until class device is destroyed */
  441. __module_get(THIS_MODULE);
  442. retval = device_add(f_dev);
  443. if (retval) {
  444. dev_err(f_dev, "%s: device_register failed\n", __func__);
  445. goto err_put_dev;
  446. }
  447. retval = device_create_bin_file(f_dev, &firmware_attr_data);
  448. if (retval) {
  449. dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
  450. goto err_del_dev;
  451. }
  452. retval = device_create_file(f_dev, &dev_attr_loading);
  453. if (retval) {
  454. dev_err(f_dev, "%s: device_create_file failed\n", __func__);
  455. goto err_del_bin_attr;
  456. }
  457. if (uevent) {
  458. dev_set_uevent_suppress(f_dev, false);
  459. dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_id);
  460. if (timeout != MAX_SCHEDULE_TIMEOUT)
  461. mod_timer(&fw_priv->timeout,
  462. round_jiffies_up(jiffies + timeout));
  463. kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
  464. }
  465. wait_for_completion(&fw_priv->completion);
  466. set_bit(FW_STATUS_DONE, &fw_priv->status);
  467. del_timer_sync(&fw_priv->timeout);
  468. mutex_lock(&fw_lock);
  469. if (!fw_priv->size || test_bit(FW_STATUS_ABORT, &fw_priv->status))
  470. retval = -ENOENT;
  471. /* transfer pages ownership at the last minute */
  472. if (!retval)
  473. retval = fw_set_page_data(fw_priv);
  474. fw_priv->fw = NULL;
  475. mutex_unlock(&fw_lock);
  476. device_remove_file(f_dev, &dev_attr_loading);
  477. err_del_bin_attr:
  478. device_remove_bin_file(f_dev, &firmware_attr_data);
  479. err_del_dev:
  480. device_del(f_dev);
  481. err_put_dev:
  482. put_device(f_dev);
  483. return retval;
  484. }
  485. /**
  486. * request_firmware: - send firmware request and wait for it
  487. * @firmware_p: pointer to firmware image
  488. * @name: name of firmware file
  489. * @device: device for which firmware is being loaded
  490. *
  491. * @firmware_p will be used to return a firmware image by the name
  492. * of @name for device @device.
  493. *
  494. * Should be called from user context where sleeping is allowed.
  495. *
  496. * @name will be used as $FIRMWARE in the uevent environment and
  497. * should be distinctive enough not to be confused with any other
  498. * firmware image for this or any other device.
  499. **/
  500. int
  501. request_firmware(const struct firmware **firmware_p, const char *name,
  502. struct device *device)
  503. {
  504. struct firmware_priv *fw_priv;
  505. int ret;
  506. fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
  507. false);
  508. if (IS_ERR_OR_NULL(fw_priv))
  509. return PTR_RET(fw_priv);
  510. ret = usermodehelper_read_trylock();
  511. if (WARN_ON(ret)) {
  512. dev_err(device, "firmware: %s will not be loaded\n", name);
  513. } else {
  514. ret = _request_firmware_load(fw_priv, true,
  515. firmware_loading_timeout());
  516. usermodehelper_read_unlock();
  517. }
  518. if (ret)
  519. _request_firmware_cleanup(firmware_p);
  520. return ret;
  521. }
  522. /**
  523. * release_firmware: - release the resource associated with a firmware image
  524. * @fw: firmware resource to release
  525. **/
  526. void release_firmware(const struct firmware *fw)
  527. {
  528. if (fw) {
  529. if (!fw_is_builtin_firmware(fw))
  530. firmware_free_data(fw);
  531. kfree(fw);
  532. }
  533. }
  534. /* Async support */
  535. struct firmware_work {
  536. struct work_struct work;
  537. struct module *module;
  538. const char *name;
  539. struct device *device;
  540. void *context;
  541. void (*cont)(const struct firmware *fw, void *context);
  542. bool uevent;
  543. };
  544. static void request_firmware_work_func(struct work_struct *work)
  545. {
  546. struct firmware_work *fw_work;
  547. const struct firmware *fw;
  548. struct firmware_priv *fw_priv;
  549. long timeout;
  550. int ret;
  551. fw_work = container_of(work, struct firmware_work, work);
  552. fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
  553. fw_work->uevent, true);
  554. if (IS_ERR_OR_NULL(fw_priv)) {
  555. ret = PTR_RET(fw_priv);
  556. goto out;
  557. }
  558. timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
  559. if (timeout) {
  560. ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
  561. usermodehelper_read_unlock();
  562. } else {
  563. dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
  564. fw_work->name);
  565. ret = -EAGAIN;
  566. }
  567. if (ret)
  568. _request_firmware_cleanup(&fw);
  569. out:
  570. fw_work->cont(fw, fw_work->context);
  571. module_put(fw_work->module);
  572. kfree(fw_work);
  573. }
  574. /**
  575. * request_firmware_nowait - asynchronous version of request_firmware
  576. * @module: module requesting the firmware
  577. * @uevent: sends uevent to copy the firmware image if this flag
  578. * is non-zero else the firmware copy must be done manually.
  579. * @name: name of firmware file
  580. * @device: device for which firmware is being loaded
  581. * @gfp: allocation flags
  582. * @context: will be passed over to @cont, and
  583. * @fw may be %NULL if firmware request fails.
  584. * @cont: function will be called asynchronously when the firmware
  585. * request is over.
  586. *
  587. * Asynchronous variant of request_firmware() for user contexts where
  588. * it is not possible to sleep for long time. It can't be called
  589. * in atomic contexts.
  590. **/
  591. int
  592. request_firmware_nowait(
  593. struct module *module, bool uevent,
  594. const char *name, struct device *device, gfp_t gfp, void *context,
  595. void (*cont)(const struct firmware *fw, void *context))
  596. {
  597. struct firmware_work *fw_work;
  598. fw_work = kzalloc(sizeof (struct firmware_work), gfp);
  599. if (!fw_work)
  600. return -ENOMEM;
  601. fw_work->module = module;
  602. fw_work->name = name;
  603. fw_work->device = device;
  604. fw_work->context = context;
  605. fw_work->cont = cont;
  606. fw_work->uevent = uevent;
  607. if (!try_module_get(module)) {
  608. kfree(fw_work);
  609. return -EFAULT;
  610. }
  611. INIT_WORK(&fw_work->work, request_firmware_work_func);
  612. schedule_work(&fw_work->work);
  613. return 0;
  614. }
  615. static int __init firmware_class_init(void)
  616. {
  617. return class_register(&firmware_class);
  618. }
  619. static void __exit firmware_class_exit(void)
  620. {
  621. class_unregister(&firmware_class);
  622. }
  623. fs_initcall(firmware_class_init);
  624. module_exit(firmware_class_exit);
  625. EXPORT_SYMBOL(release_firmware);
  626. EXPORT_SYMBOL(request_firmware);
  627. EXPORT_SYMBOL(request_firmware_nowait);