firmware_class.c 21 KB

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