firmware_class.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  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. #include <linux/async.h>
  25. #include <linux/pm.h>
  26. #include "base.h"
  27. #include "power/power.h"
  28. MODULE_AUTHOR("Manuel Estrada Sainz");
  29. MODULE_DESCRIPTION("Multi purpose firmware loading support");
  30. MODULE_LICENSE("GPL");
  31. /* Builtin firmware support */
  32. #ifdef CONFIG_FW_LOADER
  33. extern struct builtin_fw __start_builtin_fw[];
  34. extern struct builtin_fw __end_builtin_fw[];
  35. static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
  36. {
  37. struct builtin_fw *b_fw;
  38. for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
  39. if (strcmp(name, b_fw->name) == 0) {
  40. fw->size = b_fw->size;
  41. fw->data = b_fw->data;
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. static bool fw_is_builtin_firmware(const struct firmware *fw)
  48. {
  49. struct builtin_fw *b_fw;
  50. for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
  51. if (fw->data == b_fw->data)
  52. return true;
  53. return false;
  54. }
  55. #else /* Module case - no builtin firmware support */
  56. static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
  57. {
  58. return false;
  59. }
  60. static inline bool fw_is_builtin_firmware(const struct firmware *fw)
  61. {
  62. return false;
  63. }
  64. #endif
  65. enum {
  66. FW_STATUS_LOADING,
  67. FW_STATUS_DONE,
  68. FW_STATUS_ABORT,
  69. };
  70. static int loading_timeout = 60; /* In seconds */
  71. static inline long firmware_loading_timeout(void)
  72. {
  73. return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
  74. }
  75. struct firmware_cache {
  76. /* firmware_buf instance will be added into the below list */
  77. spinlock_t lock;
  78. struct list_head head;
  79. /*
  80. * Names of firmware images which have been cached successfully
  81. * will be added into the below list so that device uncache
  82. * helper can trace which firmware images have been cached
  83. * before.
  84. */
  85. spinlock_t name_lock;
  86. struct list_head fw_names;
  87. wait_queue_head_t wait_queue;
  88. int cnt;
  89. struct delayed_work work;
  90. };
  91. struct firmware_buf {
  92. struct kref ref;
  93. struct list_head list;
  94. struct completion completion;
  95. struct firmware_cache *fwc;
  96. unsigned long status;
  97. void *data;
  98. size_t size;
  99. struct page **pages;
  100. int nr_pages;
  101. int page_array_size;
  102. char fw_id[];
  103. };
  104. struct fw_cache_entry {
  105. struct list_head list;
  106. char name[];
  107. };
  108. struct firmware_priv {
  109. struct timer_list timeout;
  110. bool nowait;
  111. struct device dev;
  112. struct firmware_buf *buf;
  113. struct firmware *fw;
  114. };
  115. struct fw_name_devm {
  116. unsigned long magic;
  117. char name[];
  118. };
  119. #define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
  120. /* fw_lock could be moved to 'struct firmware_priv' but since it is just
  121. * guarding for corner cases a global lock should be OK */
  122. static DEFINE_MUTEX(fw_lock);
  123. static struct firmware_cache fw_cache;
  124. static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
  125. struct firmware_cache *fwc)
  126. {
  127. struct firmware_buf *buf;
  128. buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
  129. if (!buf)
  130. return buf;
  131. kref_init(&buf->ref);
  132. strcpy(buf->fw_id, fw_name);
  133. buf->fwc = fwc;
  134. init_completion(&buf->completion);
  135. pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
  136. return buf;
  137. }
  138. static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
  139. {
  140. struct firmware_buf *tmp;
  141. struct firmware_cache *fwc = &fw_cache;
  142. list_for_each_entry(tmp, &fwc->head, list)
  143. if (!strcmp(tmp->fw_id, fw_name))
  144. return tmp;
  145. return NULL;
  146. }
  147. static int fw_lookup_and_allocate_buf(const char *fw_name,
  148. struct firmware_cache *fwc,
  149. struct firmware_buf **buf)
  150. {
  151. struct firmware_buf *tmp;
  152. spin_lock(&fwc->lock);
  153. tmp = __fw_lookup_buf(fw_name);
  154. if (tmp) {
  155. kref_get(&tmp->ref);
  156. spin_unlock(&fwc->lock);
  157. *buf = tmp;
  158. return 1;
  159. }
  160. tmp = __allocate_fw_buf(fw_name, fwc);
  161. if (tmp)
  162. list_add(&tmp->list, &fwc->head);
  163. spin_unlock(&fwc->lock);
  164. *buf = tmp;
  165. return tmp ? 0 : -ENOMEM;
  166. }
  167. static struct firmware_buf *fw_lookup_buf(const char *fw_name)
  168. {
  169. struct firmware_buf *tmp;
  170. struct firmware_cache *fwc = &fw_cache;
  171. spin_lock(&fwc->lock);
  172. tmp = __fw_lookup_buf(fw_name);
  173. spin_unlock(&fwc->lock);
  174. return tmp;
  175. }
  176. static void __fw_free_buf(struct kref *ref)
  177. {
  178. struct firmware_buf *buf = to_fwbuf(ref);
  179. struct firmware_cache *fwc = buf->fwc;
  180. int i;
  181. pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
  182. __func__, buf->fw_id, buf, buf->data,
  183. (unsigned int)buf->size);
  184. spin_lock(&fwc->lock);
  185. list_del(&buf->list);
  186. spin_unlock(&fwc->lock);
  187. vunmap(buf->data);
  188. for (i = 0; i < buf->nr_pages; i++)
  189. __free_page(buf->pages[i]);
  190. kfree(buf->pages);
  191. kfree(buf);
  192. }
  193. static void fw_free_buf(struct firmware_buf *buf)
  194. {
  195. kref_put(&buf->ref, __fw_free_buf);
  196. }
  197. static struct firmware_priv *to_firmware_priv(struct device *dev)
  198. {
  199. return container_of(dev, struct firmware_priv, dev);
  200. }
  201. static void fw_load_abort(struct firmware_priv *fw_priv)
  202. {
  203. struct firmware_buf *buf = fw_priv->buf;
  204. set_bit(FW_STATUS_ABORT, &buf->status);
  205. complete_all(&buf->completion);
  206. }
  207. static ssize_t firmware_timeout_show(struct class *class,
  208. struct class_attribute *attr,
  209. char *buf)
  210. {
  211. return sprintf(buf, "%d\n", loading_timeout);
  212. }
  213. /**
  214. * firmware_timeout_store - set number of seconds to wait for firmware
  215. * @class: device class pointer
  216. * @attr: device attribute pointer
  217. * @buf: buffer to scan for timeout value
  218. * @count: number of bytes in @buf
  219. *
  220. * Sets the number of seconds to wait for the firmware. Once
  221. * this expires an error will be returned to the driver and no
  222. * firmware will be provided.
  223. *
  224. * Note: zero means 'wait forever'.
  225. **/
  226. static ssize_t firmware_timeout_store(struct class *class,
  227. struct class_attribute *attr,
  228. const char *buf, size_t count)
  229. {
  230. loading_timeout = simple_strtol(buf, NULL, 10);
  231. if (loading_timeout < 0)
  232. loading_timeout = 0;
  233. return count;
  234. }
  235. static struct class_attribute firmware_class_attrs[] = {
  236. __ATTR(timeout, S_IWUSR | S_IRUGO,
  237. firmware_timeout_show, firmware_timeout_store),
  238. __ATTR_NULL
  239. };
  240. static void fw_dev_release(struct device *dev)
  241. {
  242. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  243. kfree(fw_priv);
  244. module_put(THIS_MODULE);
  245. }
  246. static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
  247. {
  248. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  249. if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
  250. return -ENOMEM;
  251. if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
  252. return -ENOMEM;
  253. if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
  254. return -ENOMEM;
  255. return 0;
  256. }
  257. static struct class firmware_class = {
  258. .name = "firmware",
  259. .class_attrs = firmware_class_attrs,
  260. .dev_uevent = firmware_uevent,
  261. .dev_release = fw_dev_release,
  262. };
  263. static ssize_t firmware_loading_show(struct device *dev,
  264. struct device_attribute *attr, char *buf)
  265. {
  266. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  267. int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
  268. return sprintf(buf, "%d\n", loading);
  269. }
  270. /* firmware holds the ownership of pages */
  271. static void firmware_free_data(const struct firmware *fw)
  272. {
  273. WARN_ON(!fw->priv);
  274. fw_free_buf(fw->priv);
  275. }
  276. /* Some architectures don't have PAGE_KERNEL_RO */
  277. #ifndef PAGE_KERNEL_RO
  278. #define PAGE_KERNEL_RO PAGE_KERNEL
  279. #endif
  280. /**
  281. * firmware_loading_store - set value in the 'loading' control file
  282. * @dev: device pointer
  283. * @attr: device attribute pointer
  284. * @buf: buffer to scan for loading control value
  285. * @count: number of bytes in @buf
  286. *
  287. * The relevant values are:
  288. *
  289. * 1: Start a load, discarding any previous partial load.
  290. * 0: Conclude the load and hand the data to the driver code.
  291. * -1: Conclude the load with an error and discard any written data.
  292. **/
  293. static ssize_t firmware_loading_store(struct device *dev,
  294. struct device_attribute *attr,
  295. const char *buf, size_t count)
  296. {
  297. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  298. struct firmware_buf *fw_buf = fw_priv->buf;
  299. int loading = simple_strtol(buf, NULL, 10);
  300. int i;
  301. mutex_lock(&fw_lock);
  302. if (!fw_buf)
  303. goto out;
  304. switch (loading) {
  305. case 1:
  306. /* discarding any previous partial load */
  307. if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
  308. for (i = 0; i < fw_buf->nr_pages; i++)
  309. __free_page(fw_buf->pages[i]);
  310. kfree(fw_buf->pages);
  311. fw_buf->pages = NULL;
  312. fw_buf->page_array_size = 0;
  313. fw_buf->nr_pages = 0;
  314. set_bit(FW_STATUS_LOADING, &fw_buf->status);
  315. }
  316. break;
  317. case 0:
  318. if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
  319. set_bit(FW_STATUS_DONE, &fw_buf->status);
  320. clear_bit(FW_STATUS_LOADING, &fw_buf->status);
  321. complete_all(&fw_buf->completion);
  322. break;
  323. }
  324. /* fallthrough */
  325. default:
  326. dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
  327. /* fallthrough */
  328. case -1:
  329. fw_load_abort(fw_priv);
  330. break;
  331. }
  332. out:
  333. mutex_unlock(&fw_lock);
  334. return count;
  335. }
  336. static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
  337. static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
  338. struct bin_attribute *bin_attr,
  339. char *buffer, loff_t offset, size_t count)
  340. {
  341. struct device *dev = kobj_to_dev(kobj);
  342. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  343. struct firmware_buf *buf;
  344. ssize_t ret_count;
  345. mutex_lock(&fw_lock);
  346. buf = fw_priv->buf;
  347. if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
  348. ret_count = -ENODEV;
  349. goto out;
  350. }
  351. if (offset > buf->size) {
  352. ret_count = 0;
  353. goto out;
  354. }
  355. if (count > buf->size - offset)
  356. count = buf->size - offset;
  357. ret_count = count;
  358. while (count) {
  359. void *page_data;
  360. int page_nr = offset >> PAGE_SHIFT;
  361. int page_ofs = offset & (PAGE_SIZE-1);
  362. int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
  363. page_data = kmap(buf->pages[page_nr]);
  364. memcpy(buffer, page_data + page_ofs, page_cnt);
  365. kunmap(buf->pages[page_nr]);
  366. buffer += page_cnt;
  367. offset += page_cnt;
  368. count -= page_cnt;
  369. }
  370. out:
  371. mutex_unlock(&fw_lock);
  372. return ret_count;
  373. }
  374. static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
  375. {
  376. struct firmware_buf *buf = fw_priv->buf;
  377. int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
  378. /* If the array of pages is too small, grow it... */
  379. if (buf->page_array_size < pages_needed) {
  380. int new_array_size = max(pages_needed,
  381. buf->page_array_size * 2);
  382. struct page **new_pages;
  383. new_pages = kmalloc(new_array_size * sizeof(void *),
  384. GFP_KERNEL);
  385. if (!new_pages) {
  386. fw_load_abort(fw_priv);
  387. return -ENOMEM;
  388. }
  389. memcpy(new_pages, buf->pages,
  390. buf->page_array_size * sizeof(void *));
  391. memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
  392. (new_array_size - buf->page_array_size));
  393. kfree(buf->pages);
  394. buf->pages = new_pages;
  395. buf->page_array_size = new_array_size;
  396. }
  397. while (buf->nr_pages < pages_needed) {
  398. buf->pages[buf->nr_pages] =
  399. alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
  400. if (!buf->pages[buf->nr_pages]) {
  401. fw_load_abort(fw_priv);
  402. return -ENOMEM;
  403. }
  404. buf->nr_pages++;
  405. }
  406. return 0;
  407. }
  408. /**
  409. * firmware_data_write - write method for firmware
  410. * @filp: open sysfs file
  411. * @kobj: kobject for the device
  412. * @bin_attr: bin_attr structure
  413. * @buffer: buffer being written
  414. * @offset: buffer offset for write in total data store area
  415. * @count: buffer size
  416. *
  417. * Data written to the 'data' attribute will be later handed to
  418. * the driver as a firmware image.
  419. **/
  420. static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
  421. struct bin_attribute *bin_attr,
  422. char *buffer, loff_t offset, size_t count)
  423. {
  424. struct device *dev = kobj_to_dev(kobj);
  425. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  426. struct firmware_buf *buf;
  427. ssize_t retval;
  428. if (!capable(CAP_SYS_RAWIO))
  429. return -EPERM;
  430. mutex_lock(&fw_lock);
  431. buf = fw_priv->buf;
  432. if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
  433. retval = -ENODEV;
  434. goto out;
  435. }
  436. retval = fw_realloc_buffer(fw_priv, offset + count);
  437. if (retval)
  438. goto out;
  439. retval = count;
  440. while (count) {
  441. void *page_data;
  442. int page_nr = offset >> PAGE_SHIFT;
  443. int page_ofs = offset & (PAGE_SIZE - 1);
  444. int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
  445. page_data = kmap(buf->pages[page_nr]);
  446. memcpy(page_data + page_ofs, buffer, page_cnt);
  447. kunmap(buf->pages[page_nr]);
  448. buffer += page_cnt;
  449. offset += page_cnt;
  450. count -= page_cnt;
  451. }
  452. buf->size = max_t(size_t, offset, buf->size);
  453. out:
  454. mutex_unlock(&fw_lock);
  455. return retval;
  456. }
  457. static struct bin_attribute firmware_attr_data = {
  458. .attr = { .name = "data", .mode = 0644 },
  459. .size = 0,
  460. .read = firmware_data_read,
  461. .write = firmware_data_write,
  462. };
  463. static void firmware_class_timeout(u_long data)
  464. {
  465. struct firmware_priv *fw_priv = (struct firmware_priv *) data;
  466. fw_load_abort(fw_priv);
  467. }
  468. static struct firmware_priv *
  469. fw_create_instance(struct firmware *firmware, const char *fw_name,
  470. struct device *device, bool uevent, bool nowait)
  471. {
  472. struct firmware_priv *fw_priv;
  473. struct device *f_dev;
  474. fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
  475. if (!fw_priv) {
  476. dev_err(device, "%s: kmalloc failed\n", __func__);
  477. fw_priv = ERR_PTR(-ENOMEM);
  478. goto exit;
  479. }
  480. fw_priv->nowait = nowait;
  481. fw_priv->fw = firmware;
  482. setup_timer(&fw_priv->timeout,
  483. firmware_class_timeout, (u_long) fw_priv);
  484. f_dev = &fw_priv->dev;
  485. device_initialize(f_dev);
  486. dev_set_name(f_dev, "%s", fw_name);
  487. f_dev->parent = device;
  488. f_dev->class = &firmware_class;
  489. exit:
  490. return fw_priv;
  491. }
  492. /* one pages buffer is mapped/unmapped only once */
  493. static int fw_map_pages_buf(struct firmware_buf *buf)
  494. {
  495. buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
  496. if (!buf->data)
  497. return -ENOMEM;
  498. return 0;
  499. }
  500. /* store the pages buffer info firmware from buf */
  501. static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
  502. {
  503. fw->priv = buf;
  504. fw->pages = buf->pages;
  505. fw->size = buf->size;
  506. fw->data = buf->data;
  507. pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
  508. __func__, buf->fw_id, buf, buf->data,
  509. (unsigned int)buf->size);
  510. }
  511. static void fw_name_devm_release(struct device *dev, void *res)
  512. {
  513. struct fw_name_devm *fwn = res;
  514. if (fwn->magic == (unsigned long)&fw_cache)
  515. pr_debug("%s: fw_name-%s devm-%p released\n",
  516. __func__, fwn->name, res);
  517. }
  518. static int fw_devm_match(struct device *dev, void *res,
  519. void *match_data)
  520. {
  521. struct fw_name_devm *fwn = res;
  522. return (fwn->magic == (unsigned long)&fw_cache) &&
  523. !strcmp(fwn->name, match_data);
  524. }
  525. static struct fw_name_devm *fw_find_devm_name(struct device *dev,
  526. const char *name)
  527. {
  528. struct fw_name_devm *fwn;
  529. fwn = devres_find(dev, fw_name_devm_release,
  530. fw_devm_match, (void *)name);
  531. return fwn;
  532. }
  533. /* add firmware name into devres list */
  534. static int fw_add_devm_name(struct device *dev, const char *name)
  535. {
  536. struct fw_name_devm *fwn;
  537. fwn = fw_find_devm_name(dev, name);
  538. if (fwn)
  539. return 1;
  540. fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
  541. strlen(name) + 1, GFP_KERNEL);
  542. if (!fwn)
  543. return -ENOMEM;
  544. fwn->magic = (unsigned long)&fw_cache;
  545. strcpy(fwn->name, name);
  546. devres_add(dev, fwn);
  547. return 0;
  548. }
  549. static void _request_firmware_cleanup(const struct firmware **firmware_p)
  550. {
  551. release_firmware(*firmware_p);
  552. *firmware_p = NULL;
  553. }
  554. static struct firmware_priv *
  555. _request_firmware_prepare(const struct firmware **firmware_p, const char *name,
  556. struct device *device, bool uevent, bool nowait)
  557. {
  558. struct firmware *firmware;
  559. struct firmware_priv *fw_priv = NULL;
  560. struct firmware_buf *buf;
  561. int ret;
  562. if (!firmware_p)
  563. return ERR_PTR(-EINVAL);
  564. *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
  565. if (!firmware) {
  566. dev_err(device, "%s: kmalloc(struct firmware) failed\n",
  567. __func__);
  568. return ERR_PTR(-ENOMEM);
  569. }
  570. if (fw_get_builtin_firmware(firmware, name)) {
  571. dev_dbg(device, "firmware: using built-in firmware %s\n", name);
  572. return NULL;
  573. }
  574. ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
  575. if (!ret)
  576. fw_priv = fw_create_instance(firmware, name, device,
  577. uevent, nowait);
  578. if (IS_ERR(fw_priv) || ret < 0) {
  579. kfree(firmware);
  580. *firmware_p = NULL;
  581. return ERR_PTR(-ENOMEM);
  582. } else if (fw_priv) {
  583. fw_priv->buf = buf;
  584. /*
  585. * bind with 'buf' now to avoid warning in failure path
  586. * of requesting firmware.
  587. */
  588. firmware->priv = buf;
  589. return fw_priv;
  590. }
  591. /* share the cached buf, which is inprogessing or completed */
  592. check_status:
  593. mutex_lock(&fw_lock);
  594. if (test_bit(FW_STATUS_ABORT, &buf->status)) {
  595. fw_priv = ERR_PTR(-ENOENT);
  596. _request_firmware_cleanup(firmware_p);
  597. goto exit;
  598. } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
  599. fw_priv = NULL;
  600. fw_set_page_data(buf, firmware);
  601. goto exit;
  602. }
  603. mutex_unlock(&fw_lock);
  604. wait_for_completion(&buf->completion);
  605. goto check_status;
  606. exit:
  607. mutex_unlock(&fw_lock);
  608. return fw_priv;
  609. }
  610. static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
  611. long timeout)
  612. {
  613. int retval = 0;
  614. struct device *f_dev = &fw_priv->dev;
  615. struct firmware_buf *buf = fw_priv->buf;
  616. dev_set_uevent_suppress(f_dev, true);
  617. /* Need to pin this module until class device is destroyed */
  618. __module_get(THIS_MODULE);
  619. retval = device_add(f_dev);
  620. if (retval) {
  621. dev_err(f_dev, "%s: device_register failed\n", __func__);
  622. goto err_put_dev;
  623. }
  624. retval = device_create_bin_file(f_dev, &firmware_attr_data);
  625. if (retval) {
  626. dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
  627. goto err_del_dev;
  628. }
  629. retval = device_create_file(f_dev, &dev_attr_loading);
  630. if (retval) {
  631. dev_err(f_dev, "%s: device_create_file failed\n", __func__);
  632. goto err_del_bin_attr;
  633. }
  634. if (uevent) {
  635. dev_set_uevent_suppress(f_dev, false);
  636. dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
  637. if (timeout != MAX_SCHEDULE_TIMEOUT)
  638. mod_timer(&fw_priv->timeout,
  639. round_jiffies_up(jiffies + timeout));
  640. kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
  641. }
  642. wait_for_completion(&buf->completion);
  643. del_timer_sync(&fw_priv->timeout);
  644. mutex_lock(&fw_lock);
  645. if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
  646. retval = -ENOENT;
  647. /*
  648. * add firmware name into devres list so that we can auto cache
  649. * and uncache firmware for device.
  650. *
  651. * f_dev->parent may has been deleted already, but the problem
  652. * should be fixed in devres or driver core.
  653. */
  654. if (!retval && f_dev->parent)
  655. fw_add_devm_name(f_dev->parent, buf->fw_id);
  656. if (!retval)
  657. retval = fw_map_pages_buf(buf);
  658. /* pass the pages buffer to driver at the last minute */
  659. fw_set_page_data(buf, fw_priv->fw);
  660. fw_priv->buf = NULL;
  661. mutex_unlock(&fw_lock);
  662. device_remove_file(f_dev, &dev_attr_loading);
  663. err_del_bin_attr:
  664. device_remove_bin_file(f_dev, &firmware_attr_data);
  665. err_del_dev:
  666. device_del(f_dev);
  667. err_put_dev:
  668. put_device(f_dev);
  669. return retval;
  670. }
  671. /**
  672. * request_firmware: - send firmware request and wait for it
  673. * @firmware_p: pointer to firmware image
  674. * @name: name of firmware file
  675. * @device: device for which firmware is being loaded
  676. *
  677. * @firmware_p will be used to return a firmware image by the name
  678. * of @name for device @device.
  679. *
  680. * Should be called from user context where sleeping is allowed.
  681. *
  682. * @name will be used as $FIRMWARE in the uevent environment and
  683. * should be distinctive enough not to be confused with any other
  684. * firmware image for this or any other device.
  685. *
  686. * Caller must hold the reference count of @device.
  687. **/
  688. int
  689. request_firmware(const struct firmware **firmware_p, const char *name,
  690. struct device *device)
  691. {
  692. struct firmware_priv *fw_priv;
  693. int ret;
  694. fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
  695. false);
  696. if (IS_ERR_OR_NULL(fw_priv))
  697. return PTR_RET(fw_priv);
  698. ret = usermodehelper_read_trylock();
  699. if (WARN_ON(ret)) {
  700. dev_err(device, "firmware: %s will not be loaded\n", name);
  701. } else {
  702. ret = _request_firmware_load(fw_priv, true,
  703. firmware_loading_timeout());
  704. usermodehelper_read_unlock();
  705. }
  706. if (ret)
  707. _request_firmware_cleanup(firmware_p);
  708. return ret;
  709. }
  710. /**
  711. * release_firmware: - release the resource associated with a firmware image
  712. * @fw: firmware resource to release
  713. **/
  714. void release_firmware(const struct firmware *fw)
  715. {
  716. if (fw) {
  717. if (!fw_is_builtin_firmware(fw))
  718. firmware_free_data(fw);
  719. kfree(fw);
  720. }
  721. }
  722. /* Async support */
  723. struct firmware_work {
  724. struct work_struct work;
  725. struct module *module;
  726. const char *name;
  727. struct device *device;
  728. void *context;
  729. void (*cont)(const struct firmware *fw, void *context);
  730. bool uevent;
  731. };
  732. static void request_firmware_work_func(struct work_struct *work)
  733. {
  734. struct firmware_work *fw_work;
  735. const struct firmware *fw;
  736. struct firmware_priv *fw_priv;
  737. long timeout;
  738. int ret;
  739. fw_work = container_of(work, struct firmware_work, work);
  740. fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
  741. fw_work->uevent, true);
  742. if (IS_ERR_OR_NULL(fw_priv)) {
  743. ret = PTR_RET(fw_priv);
  744. goto out;
  745. }
  746. timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
  747. if (timeout) {
  748. ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
  749. usermodehelper_read_unlock();
  750. } else {
  751. dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
  752. fw_work->name);
  753. ret = -EAGAIN;
  754. }
  755. if (ret)
  756. _request_firmware_cleanup(&fw);
  757. out:
  758. fw_work->cont(fw, fw_work->context);
  759. put_device(fw_work->device);
  760. module_put(fw_work->module);
  761. kfree(fw_work);
  762. }
  763. /**
  764. * request_firmware_nowait - asynchronous version of request_firmware
  765. * @module: module requesting the firmware
  766. * @uevent: sends uevent to copy the firmware image if this flag
  767. * is non-zero else the firmware copy must be done manually.
  768. * @name: name of firmware file
  769. * @device: device for which firmware is being loaded
  770. * @gfp: allocation flags
  771. * @context: will be passed over to @cont, and
  772. * @fw may be %NULL if firmware request fails.
  773. * @cont: function will be called asynchronously when the firmware
  774. * request is over.
  775. *
  776. * Caller must hold the reference count of @device.
  777. *
  778. * Asynchronous variant of request_firmware() for user contexts:
  779. * - sleep for as small periods as possible since it may
  780. * increase kernel boot time of built-in device drivers
  781. * requesting firmware in their ->probe() methods, if
  782. * @gfp is GFP_KERNEL.
  783. *
  784. * - can't sleep at all if @gfp is GFP_ATOMIC.
  785. **/
  786. int
  787. request_firmware_nowait(
  788. struct module *module, bool uevent,
  789. const char *name, struct device *device, gfp_t gfp, void *context,
  790. void (*cont)(const struct firmware *fw, void *context))
  791. {
  792. struct firmware_work *fw_work;
  793. fw_work = kzalloc(sizeof (struct firmware_work), gfp);
  794. if (!fw_work)
  795. return -ENOMEM;
  796. fw_work->module = module;
  797. fw_work->name = name;
  798. fw_work->device = device;
  799. fw_work->context = context;
  800. fw_work->cont = cont;
  801. fw_work->uevent = uevent;
  802. if (!try_module_get(module)) {
  803. kfree(fw_work);
  804. return -EFAULT;
  805. }
  806. get_device(fw_work->device);
  807. INIT_WORK(&fw_work->work, request_firmware_work_func);
  808. schedule_work(&fw_work->work);
  809. return 0;
  810. }
  811. /**
  812. * cache_firmware - cache one firmware image in kernel memory space
  813. * @fw_name: the firmware image name
  814. *
  815. * Cache firmware in kernel memory so that drivers can use it when
  816. * system isn't ready for them to request firmware image from userspace.
  817. * Once it returns successfully, driver can use request_firmware or its
  818. * nowait version to get the cached firmware without any interacting
  819. * with userspace
  820. *
  821. * Return 0 if the firmware image has been cached successfully
  822. * Return !0 otherwise
  823. *
  824. */
  825. int cache_firmware(const char *fw_name)
  826. {
  827. int ret;
  828. const struct firmware *fw;
  829. pr_debug("%s: %s\n", __func__, fw_name);
  830. ret = request_firmware(&fw, fw_name, NULL);
  831. if (!ret)
  832. kfree(fw);
  833. pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
  834. return ret;
  835. }
  836. /**
  837. * uncache_firmware - remove one cached firmware image
  838. * @fw_name: the firmware image name
  839. *
  840. * Uncache one firmware image which has been cached successfully
  841. * before.
  842. *
  843. * Return 0 if the firmware cache has been removed successfully
  844. * Return !0 otherwise
  845. *
  846. */
  847. int uncache_firmware(const char *fw_name)
  848. {
  849. struct firmware_buf *buf;
  850. struct firmware fw;
  851. pr_debug("%s: %s\n", __func__, fw_name);
  852. if (fw_get_builtin_firmware(&fw, fw_name))
  853. return 0;
  854. buf = fw_lookup_buf(fw_name);
  855. if (buf) {
  856. fw_free_buf(buf);
  857. return 0;
  858. }
  859. return -EINVAL;
  860. }
  861. static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
  862. {
  863. struct fw_cache_entry *fce;
  864. fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
  865. if (!fce)
  866. goto exit;
  867. strcpy(fce->name, name);
  868. exit:
  869. return fce;
  870. }
  871. static void free_fw_cache_entry(struct fw_cache_entry *fce)
  872. {
  873. kfree(fce);
  874. }
  875. static void __async_dev_cache_fw_image(void *fw_entry,
  876. async_cookie_t cookie)
  877. {
  878. struct fw_cache_entry *fce = fw_entry;
  879. struct firmware_cache *fwc = &fw_cache;
  880. int ret;
  881. ret = cache_firmware(fce->name);
  882. if (ret)
  883. goto free;
  884. spin_lock(&fwc->name_lock);
  885. list_add(&fce->list, &fwc->fw_names);
  886. spin_unlock(&fwc->name_lock);
  887. goto drop_ref;
  888. free:
  889. free_fw_cache_entry(fce);
  890. drop_ref:
  891. spin_lock(&fwc->name_lock);
  892. fwc->cnt--;
  893. spin_unlock(&fwc->name_lock);
  894. wake_up(&fwc->wait_queue);
  895. }
  896. /* called with dev->devres_lock held */
  897. static void dev_create_fw_entry(struct device *dev, void *res,
  898. void *data)
  899. {
  900. struct fw_name_devm *fwn = res;
  901. const char *fw_name = fwn->name;
  902. struct list_head *head = data;
  903. struct fw_cache_entry *fce;
  904. fce = alloc_fw_cache_entry(fw_name);
  905. if (fce)
  906. list_add(&fce->list, head);
  907. }
  908. static int devm_name_match(struct device *dev, void *res,
  909. void *match_data)
  910. {
  911. struct fw_name_devm *fwn = res;
  912. return (fwn->magic == (unsigned long)match_data);
  913. }
  914. static void dev_cache_fw_image(struct device *dev)
  915. {
  916. LIST_HEAD(todo);
  917. struct fw_cache_entry *fce;
  918. struct fw_cache_entry *fce_next;
  919. struct firmware_cache *fwc = &fw_cache;
  920. devres_for_each_res(dev, fw_name_devm_release,
  921. devm_name_match, &fw_cache,
  922. dev_create_fw_entry, &todo);
  923. list_for_each_entry_safe(fce, fce_next, &todo, list) {
  924. list_del(&fce->list);
  925. spin_lock(&fwc->name_lock);
  926. fwc->cnt++;
  927. spin_unlock(&fwc->name_lock);
  928. async_schedule(__async_dev_cache_fw_image, (void *)fce);
  929. }
  930. }
  931. static void __device_uncache_fw_images(void)
  932. {
  933. struct firmware_cache *fwc = &fw_cache;
  934. struct fw_cache_entry *fce;
  935. spin_lock(&fwc->name_lock);
  936. while (!list_empty(&fwc->fw_names)) {
  937. fce = list_entry(fwc->fw_names.next,
  938. struct fw_cache_entry, list);
  939. list_del(&fce->list);
  940. spin_unlock(&fwc->name_lock);
  941. uncache_firmware(fce->name);
  942. free_fw_cache_entry(fce);
  943. spin_lock(&fwc->name_lock);
  944. }
  945. spin_unlock(&fwc->name_lock);
  946. }
  947. /**
  948. * device_cache_fw_images - cache devices' firmware
  949. *
  950. * If one device called request_firmware or its nowait version
  951. * successfully before, the firmware names are recored into the
  952. * device's devres link list, so device_cache_fw_images can call
  953. * cache_firmware() to cache these firmwares for the device,
  954. * then the device driver can load its firmwares easily at
  955. * time when system is not ready to complete loading firmware.
  956. */
  957. static void device_cache_fw_images(void)
  958. {
  959. struct firmware_cache *fwc = &fw_cache;
  960. struct device *dev;
  961. int old_timeout;
  962. DEFINE_WAIT(wait);
  963. pr_debug("%s\n", __func__);
  964. /*
  965. * use small loading timeout for caching devices' firmware
  966. * because all these firmware images have been loaded
  967. * successfully at lease once, also system is ready for
  968. * completing firmware loading now. The maximum size of
  969. * firmware in current distributions is about 2M bytes,
  970. * so 10 secs should be enough.
  971. */
  972. old_timeout = loading_timeout;
  973. loading_timeout = 10;
  974. device_pm_lock();
  975. list_for_each_entry(dev, &dpm_list, power.entry)
  976. dev_cache_fw_image(dev);
  977. device_pm_unlock();
  978. /* wait for completion of caching firmware for all devices */
  979. spin_lock(&fwc->name_lock);
  980. for (;;) {
  981. prepare_to_wait(&fwc->wait_queue, &wait,
  982. TASK_UNINTERRUPTIBLE);
  983. if (!fwc->cnt)
  984. break;
  985. spin_unlock(&fwc->name_lock);
  986. schedule();
  987. spin_lock(&fwc->name_lock);
  988. }
  989. spin_unlock(&fwc->name_lock);
  990. finish_wait(&fwc->wait_queue, &wait);
  991. loading_timeout = old_timeout;
  992. }
  993. /**
  994. * device_uncache_fw_images - uncache devices' firmware
  995. *
  996. * uncache all firmwares which have been cached successfully
  997. * by device_uncache_fw_images earlier
  998. */
  999. static void device_uncache_fw_images(void)
  1000. {
  1001. pr_debug("%s\n", __func__);
  1002. __device_uncache_fw_images();
  1003. }
  1004. static void device_uncache_fw_images_work(struct work_struct *work)
  1005. {
  1006. device_uncache_fw_images();
  1007. }
  1008. /**
  1009. * device_uncache_fw_images_delay - uncache devices firmwares
  1010. * @delay: number of milliseconds to delay uncache device firmwares
  1011. *
  1012. * uncache all devices's firmwares which has been cached successfully
  1013. * by device_cache_fw_images after @delay milliseconds.
  1014. */
  1015. static void device_uncache_fw_images_delay(unsigned long delay)
  1016. {
  1017. schedule_delayed_work(&fw_cache.work,
  1018. msecs_to_jiffies(delay));
  1019. }
  1020. static void __init fw_cache_init(void)
  1021. {
  1022. spin_lock_init(&fw_cache.lock);
  1023. INIT_LIST_HEAD(&fw_cache.head);
  1024. spin_lock_init(&fw_cache.name_lock);
  1025. INIT_LIST_HEAD(&fw_cache.fw_names);
  1026. fw_cache.cnt = 0;
  1027. init_waitqueue_head(&fw_cache.wait_queue);
  1028. INIT_DELAYED_WORK(&fw_cache.work,
  1029. device_uncache_fw_images_work);
  1030. }
  1031. static int __init firmware_class_init(void)
  1032. {
  1033. fw_cache_init();
  1034. return class_register(&firmware_class);
  1035. }
  1036. static void __exit firmware_class_exit(void)
  1037. {
  1038. class_unregister(&firmware_class);
  1039. }
  1040. fs_initcall(firmware_class_init);
  1041. module_exit(firmware_class_exit);
  1042. EXPORT_SYMBOL(release_firmware);
  1043. EXPORT_SYMBOL(request_firmware);
  1044. EXPORT_SYMBOL(request_firmware_nowait);
  1045. EXPORT_SYMBOL_GPL(cache_firmware);
  1046. EXPORT_SYMBOL_GPL(uncache_firmware);