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