firmware_class.c 21 KB

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