firmware_class.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  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/file.h>
  24. #include <linux/list.h>
  25. #include <linux/async.h>
  26. #include <linux/pm.h>
  27. #include <linux/suspend.h>
  28. #include <linux/syscore_ops.h>
  29. #include <linux/reboot.h>
  30. #include <generated/utsrelease.h>
  31. #include "base.h"
  32. MODULE_AUTHOR("Manuel Estrada Sainz");
  33. MODULE_DESCRIPTION("Multi purpose firmware loading support");
  34. MODULE_LICENSE("GPL");
  35. /* Builtin firmware support */
  36. #ifdef CONFIG_FW_LOADER
  37. extern struct builtin_fw __start_builtin_fw[];
  38. extern struct builtin_fw __end_builtin_fw[];
  39. static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
  40. {
  41. struct builtin_fw *b_fw;
  42. for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
  43. if (strcmp(name, b_fw->name) == 0) {
  44. fw->size = b_fw->size;
  45. fw->data = b_fw->data;
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. static bool fw_is_builtin_firmware(const struct firmware *fw)
  52. {
  53. struct builtin_fw *b_fw;
  54. for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
  55. if (fw->data == b_fw->data)
  56. return true;
  57. return false;
  58. }
  59. #else /* Module case - no builtin firmware support */
  60. static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
  61. {
  62. return false;
  63. }
  64. static inline bool fw_is_builtin_firmware(const struct firmware *fw)
  65. {
  66. return false;
  67. }
  68. #endif
  69. enum {
  70. FW_STATUS_LOADING,
  71. FW_STATUS_DONE,
  72. FW_STATUS_ABORT,
  73. };
  74. static int loading_timeout = 60; /* In seconds */
  75. static inline long firmware_loading_timeout(void)
  76. {
  77. return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
  78. }
  79. struct firmware_cache {
  80. /* firmware_buf instance will be added into the below list */
  81. spinlock_t lock;
  82. struct list_head head;
  83. int state;
  84. #ifdef CONFIG_PM_SLEEP
  85. /*
  86. * Names of firmware images which have been cached successfully
  87. * will be added into the below list so that device uncache
  88. * helper can trace which firmware images have been cached
  89. * before.
  90. */
  91. spinlock_t name_lock;
  92. struct list_head fw_names;
  93. struct delayed_work work;
  94. struct notifier_block pm_notify;
  95. #endif
  96. };
  97. struct firmware_buf {
  98. struct kref ref;
  99. struct list_head list;
  100. struct completion completion;
  101. struct firmware_cache *fwc;
  102. unsigned long status;
  103. void *data;
  104. size_t size;
  105. #ifdef CONFIG_FW_LOADER_USER_HELPER
  106. bool is_paged_buf;
  107. bool need_uevent;
  108. struct page **pages;
  109. int nr_pages;
  110. int page_array_size;
  111. struct list_head pending_list;
  112. #endif
  113. char fw_id[];
  114. };
  115. struct fw_cache_entry {
  116. struct list_head list;
  117. char name[];
  118. };
  119. struct fw_name_devm {
  120. unsigned long magic;
  121. char name[];
  122. };
  123. #define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
  124. #define FW_LOADER_NO_CACHE 0
  125. #define FW_LOADER_START_CACHE 1
  126. static int fw_cache_piggyback_on_request(const char *name);
  127. /* fw_lock could be moved to 'struct firmware_priv' but since it is just
  128. * guarding for corner cases a global lock should be OK */
  129. static DEFINE_MUTEX(fw_lock);
  130. static struct firmware_cache fw_cache;
  131. static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
  132. struct firmware_cache *fwc)
  133. {
  134. struct firmware_buf *buf;
  135. buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
  136. if (!buf)
  137. return buf;
  138. kref_init(&buf->ref);
  139. strcpy(buf->fw_id, fw_name);
  140. buf->fwc = fwc;
  141. init_completion(&buf->completion);
  142. #ifdef CONFIG_FW_LOADER_USER_HELPER
  143. INIT_LIST_HEAD(&buf->pending_list);
  144. #endif
  145. pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
  146. return buf;
  147. }
  148. static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
  149. {
  150. struct firmware_buf *tmp;
  151. struct firmware_cache *fwc = &fw_cache;
  152. list_for_each_entry(tmp, &fwc->head, list)
  153. if (!strcmp(tmp->fw_id, fw_name))
  154. return tmp;
  155. return NULL;
  156. }
  157. static int fw_lookup_and_allocate_buf(const char *fw_name,
  158. struct firmware_cache *fwc,
  159. struct firmware_buf **buf)
  160. {
  161. struct firmware_buf *tmp;
  162. spin_lock(&fwc->lock);
  163. tmp = __fw_lookup_buf(fw_name);
  164. if (tmp) {
  165. kref_get(&tmp->ref);
  166. spin_unlock(&fwc->lock);
  167. *buf = tmp;
  168. return 1;
  169. }
  170. tmp = __allocate_fw_buf(fw_name, fwc);
  171. if (tmp)
  172. list_add(&tmp->list, &fwc->head);
  173. spin_unlock(&fwc->lock);
  174. *buf = tmp;
  175. return tmp ? 0 : -ENOMEM;
  176. }
  177. static void __fw_free_buf(struct kref *ref)
  178. {
  179. struct firmware_buf *buf = to_fwbuf(ref);
  180. struct firmware_cache *fwc = buf->fwc;
  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. list_del(&buf->list);
  185. spin_unlock(&fwc->lock);
  186. #ifdef CONFIG_FW_LOADER_USER_HELPER
  187. if (buf->is_paged_buf) {
  188. int i;
  189. vunmap(buf->data);
  190. for (i = 0; i < buf->nr_pages; i++)
  191. __free_page(buf->pages[i]);
  192. kfree(buf->pages);
  193. } else
  194. #endif
  195. vfree(buf->data);
  196. kfree(buf);
  197. }
  198. static void fw_free_buf(struct firmware_buf *buf)
  199. {
  200. struct firmware_cache *fwc = buf->fwc;
  201. spin_lock(&fwc->lock);
  202. if (!kref_put(&buf->ref, __fw_free_buf))
  203. spin_unlock(&fwc->lock);
  204. }
  205. /* direct firmware loading support */
  206. static char fw_path_para[256];
  207. static const char * const fw_path[] = {
  208. fw_path_para,
  209. "/lib/firmware/updates/" UTS_RELEASE,
  210. "/lib/firmware/updates",
  211. "/lib/firmware/" UTS_RELEASE,
  212. "/lib/firmware"
  213. };
  214. /*
  215. * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
  216. * from kernel command line because firmware_class is generally built in
  217. * kernel instead of module.
  218. */
  219. module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
  220. MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
  221. /* Don't inline this: 'struct kstat' is biggish */
  222. static noinline_for_stack long fw_file_size(struct file *file)
  223. {
  224. struct kstat st;
  225. if (vfs_getattr(&file->f_path, &st))
  226. return -1;
  227. if (!S_ISREG(st.mode))
  228. return -1;
  229. if (st.size != (long)st.size)
  230. return -1;
  231. return st.size;
  232. }
  233. static int fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
  234. {
  235. long size;
  236. char *buf;
  237. int rc;
  238. size = fw_file_size(file);
  239. if (size <= 0)
  240. return -EINVAL;
  241. buf = vmalloc(size);
  242. if (!buf)
  243. return -ENOMEM;
  244. rc = kernel_read(file, 0, buf, size);
  245. if (rc != size) {
  246. if (rc > 0)
  247. rc = -EIO;
  248. vfree(buf);
  249. return rc;
  250. }
  251. fw_buf->data = buf;
  252. fw_buf->size = size;
  253. return 0;
  254. }
  255. static int fw_get_filesystem_firmware(struct device *device,
  256. struct firmware_buf *buf)
  257. {
  258. int i;
  259. int rc = -ENOENT;
  260. char *path = __getname();
  261. for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
  262. struct file *file;
  263. /* skip the unset customized path */
  264. if (!fw_path[i][0])
  265. continue;
  266. snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id);
  267. file = filp_open(path, O_RDONLY, 0);
  268. if (IS_ERR(file))
  269. continue;
  270. rc = fw_read_file_contents(file, buf);
  271. fput(file);
  272. if (rc)
  273. dev_warn(device, "firmware, attempted to load %s, but failed with error %d\n",
  274. path, rc);
  275. else
  276. break;
  277. }
  278. __putname(path);
  279. if (!rc) {
  280. dev_dbg(device, "firmware: direct-loading firmware %s\n",
  281. buf->fw_id);
  282. mutex_lock(&fw_lock);
  283. set_bit(FW_STATUS_DONE, &buf->status);
  284. complete_all(&buf->completion);
  285. mutex_unlock(&fw_lock);
  286. }
  287. return rc;
  288. }
  289. /* firmware holds the ownership of pages */
  290. static void firmware_free_data(const struct firmware *fw)
  291. {
  292. /* Loaded directly? */
  293. if (!fw->priv) {
  294. vfree(fw->data);
  295. return;
  296. }
  297. fw_free_buf(fw->priv);
  298. }
  299. /* store the pages buffer info firmware from buf */
  300. static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
  301. {
  302. fw->priv = buf;
  303. #ifdef CONFIG_FW_LOADER_USER_HELPER
  304. fw->pages = buf->pages;
  305. #endif
  306. fw->size = buf->size;
  307. fw->data = buf->data;
  308. pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
  309. __func__, buf->fw_id, buf, buf->data,
  310. (unsigned int)buf->size);
  311. }
  312. #ifdef CONFIG_PM_SLEEP
  313. static void fw_name_devm_release(struct device *dev, void *res)
  314. {
  315. struct fw_name_devm *fwn = res;
  316. if (fwn->magic == (unsigned long)&fw_cache)
  317. pr_debug("%s: fw_name-%s devm-%p released\n",
  318. __func__, fwn->name, res);
  319. }
  320. static int fw_devm_match(struct device *dev, void *res,
  321. void *match_data)
  322. {
  323. struct fw_name_devm *fwn = res;
  324. return (fwn->magic == (unsigned long)&fw_cache) &&
  325. !strcmp(fwn->name, match_data);
  326. }
  327. static struct fw_name_devm *fw_find_devm_name(struct device *dev,
  328. const char *name)
  329. {
  330. struct fw_name_devm *fwn;
  331. fwn = devres_find(dev, fw_name_devm_release,
  332. fw_devm_match, (void *)name);
  333. return fwn;
  334. }
  335. /* add firmware name into devres list */
  336. static int fw_add_devm_name(struct device *dev, const char *name)
  337. {
  338. struct fw_name_devm *fwn;
  339. fwn = fw_find_devm_name(dev, name);
  340. if (fwn)
  341. return 1;
  342. fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
  343. strlen(name) + 1, GFP_KERNEL);
  344. if (!fwn)
  345. return -ENOMEM;
  346. fwn->magic = (unsigned long)&fw_cache;
  347. strcpy(fwn->name, name);
  348. devres_add(dev, fwn);
  349. return 0;
  350. }
  351. #else
  352. static int fw_add_devm_name(struct device *dev, const char *name)
  353. {
  354. return 0;
  355. }
  356. #endif
  357. /*
  358. * user-mode helper code
  359. */
  360. #ifdef CONFIG_FW_LOADER_USER_HELPER
  361. struct firmware_priv {
  362. struct delayed_work timeout_work;
  363. bool nowait;
  364. struct device dev;
  365. struct firmware_buf *buf;
  366. struct firmware *fw;
  367. };
  368. static struct firmware_priv *to_firmware_priv(struct device *dev)
  369. {
  370. return container_of(dev, struct firmware_priv, dev);
  371. }
  372. static void __fw_load_abort(struct firmware_buf *buf)
  373. {
  374. /*
  375. * There is a small window in which user can write to 'loading'
  376. * between loading done and disappearance of 'loading'
  377. */
  378. if (test_bit(FW_STATUS_DONE, &buf->status))
  379. return;
  380. list_del_init(&buf->pending_list);
  381. set_bit(FW_STATUS_ABORT, &buf->status);
  382. complete_all(&buf->completion);
  383. }
  384. static void fw_load_abort(struct firmware_priv *fw_priv)
  385. {
  386. struct firmware_buf *buf = fw_priv->buf;
  387. __fw_load_abort(buf);
  388. /* avoid user action after loading abort */
  389. fw_priv->buf = NULL;
  390. }
  391. #define is_fw_load_aborted(buf) \
  392. test_bit(FW_STATUS_ABORT, &(buf)->status)
  393. static LIST_HEAD(pending_fw_head);
  394. /* reboot notifier for avoid deadlock with usermode_lock */
  395. static int fw_shutdown_notify(struct notifier_block *unused1,
  396. unsigned long unused2, void *unused3)
  397. {
  398. mutex_lock(&fw_lock);
  399. while (!list_empty(&pending_fw_head))
  400. __fw_load_abort(list_first_entry(&pending_fw_head,
  401. struct firmware_buf,
  402. pending_list));
  403. mutex_unlock(&fw_lock);
  404. return NOTIFY_DONE;
  405. }
  406. static struct notifier_block fw_shutdown_nb = {
  407. .notifier_call = fw_shutdown_notify,
  408. };
  409. static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
  410. char *buf)
  411. {
  412. return sprintf(buf, "%d\n", loading_timeout);
  413. }
  414. /**
  415. * firmware_timeout_store - set number of seconds to wait for firmware
  416. * @class: device class pointer
  417. * @attr: device attribute pointer
  418. * @buf: buffer to scan for timeout value
  419. * @count: number of bytes in @buf
  420. *
  421. * Sets the number of seconds to wait for the firmware. Once
  422. * this expires an error will be returned to the driver and no
  423. * firmware will be provided.
  424. *
  425. * Note: zero means 'wait forever'.
  426. **/
  427. static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
  428. const char *buf, size_t count)
  429. {
  430. loading_timeout = simple_strtol(buf, NULL, 10);
  431. if (loading_timeout < 0)
  432. loading_timeout = 0;
  433. return count;
  434. }
  435. static struct class_attribute firmware_class_attrs[] = {
  436. __ATTR_RW(timeout),
  437. __ATTR_NULL
  438. };
  439. static void fw_dev_release(struct device *dev)
  440. {
  441. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  442. kfree(fw_priv);
  443. }
  444. static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
  445. {
  446. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  447. if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
  448. return -ENOMEM;
  449. if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
  450. return -ENOMEM;
  451. if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
  452. return -ENOMEM;
  453. return 0;
  454. }
  455. static struct class firmware_class = {
  456. .name = "firmware",
  457. .class_attrs = firmware_class_attrs,
  458. .dev_uevent = firmware_uevent,
  459. .dev_release = fw_dev_release,
  460. };
  461. static ssize_t firmware_loading_show(struct device *dev,
  462. struct device_attribute *attr, char *buf)
  463. {
  464. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  465. int loading = 0;
  466. mutex_lock(&fw_lock);
  467. if (fw_priv->buf)
  468. loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
  469. mutex_unlock(&fw_lock);
  470. return sprintf(buf, "%d\n", loading);
  471. }
  472. /* Some architectures don't have PAGE_KERNEL_RO */
  473. #ifndef PAGE_KERNEL_RO
  474. #define PAGE_KERNEL_RO PAGE_KERNEL
  475. #endif
  476. /* one pages buffer should be mapped/unmapped only once */
  477. static int fw_map_pages_buf(struct firmware_buf *buf)
  478. {
  479. if (!buf->is_paged_buf)
  480. return 0;
  481. if (buf->data)
  482. vunmap(buf->data);
  483. buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
  484. if (!buf->data)
  485. return -ENOMEM;
  486. return 0;
  487. }
  488. /**
  489. * firmware_loading_store - set value in the 'loading' control file
  490. * @dev: device pointer
  491. * @attr: device attribute pointer
  492. * @buf: buffer to scan for loading control value
  493. * @count: number of bytes in @buf
  494. *
  495. * The relevant values are:
  496. *
  497. * 1: Start a load, discarding any previous partial load.
  498. * 0: Conclude the load and hand the data to the driver code.
  499. * -1: Conclude the load with an error and discard any written data.
  500. **/
  501. static ssize_t firmware_loading_store(struct device *dev,
  502. struct device_attribute *attr,
  503. const char *buf, size_t count)
  504. {
  505. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  506. struct firmware_buf *fw_buf;
  507. int loading = simple_strtol(buf, NULL, 10);
  508. int i;
  509. mutex_lock(&fw_lock);
  510. fw_buf = fw_priv->buf;
  511. if (!fw_buf)
  512. goto out;
  513. switch (loading) {
  514. case 1:
  515. /* discarding any previous partial load */
  516. if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
  517. for (i = 0; i < fw_buf->nr_pages; i++)
  518. __free_page(fw_buf->pages[i]);
  519. kfree(fw_buf->pages);
  520. fw_buf->pages = NULL;
  521. fw_buf->page_array_size = 0;
  522. fw_buf->nr_pages = 0;
  523. set_bit(FW_STATUS_LOADING, &fw_buf->status);
  524. }
  525. break;
  526. case 0:
  527. if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
  528. set_bit(FW_STATUS_DONE, &fw_buf->status);
  529. clear_bit(FW_STATUS_LOADING, &fw_buf->status);
  530. /*
  531. * Several loading requests may be pending on
  532. * one same firmware buf, so let all requests
  533. * see the mapped 'buf->data' once the loading
  534. * is completed.
  535. * */
  536. fw_map_pages_buf(fw_buf);
  537. list_del_init(&fw_buf->pending_list);
  538. complete_all(&fw_buf->completion);
  539. break;
  540. }
  541. /* fallthrough */
  542. default:
  543. dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
  544. /* fallthrough */
  545. case -1:
  546. fw_load_abort(fw_priv);
  547. break;
  548. }
  549. out:
  550. mutex_unlock(&fw_lock);
  551. return count;
  552. }
  553. static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
  554. static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
  555. struct bin_attribute *bin_attr,
  556. char *buffer, loff_t offset, size_t count)
  557. {
  558. struct device *dev = kobj_to_dev(kobj);
  559. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  560. struct firmware_buf *buf;
  561. ssize_t ret_count;
  562. mutex_lock(&fw_lock);
  563. buf = fw_priv->buf;
  564. if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
  565. ret_count = -ENODEV;
  566. goto out;
  567. }
  568. if (offset > buf->size) {
  569. ret_count = 0;
  570. goto out;
  571. }
  572. if (count > buf->size - offset)
  573. count = buf->size - offset;
  574. ret_count = count;
  575. while (count) {
  576. void *page_data;
  577. int page_nr = offset >> PAGE_SHIFT;
  578. int page_ofs = offset & (PAGE_SIZE-1);
  579. int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
  580. page_data = kmap(buf->pages[page_nr]);
  581. memcpy(buffer, page_data + page_ofs, page_cnt);
  582. kunmap(buf->pages[page_nr]);
  583. buffer += page_cnt;
  584. offset += page_cnt;
  585. count -= page_cnt;
  586. }
  587. out:
  588. mutex_unlock(&fw_lock);
  589. return ret_count;
  590. }
  591. static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
  592. {
  593. struct firmware_buf *buf = fw_priv->buf;
  594. int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
  595. /* If the array of pages is too small, grow it... */
  596. if (buf->page_array_size < pages_needed) {
  597. int new_array_size = max(pages_needed,
  598. buf->page_array_size * 2);
  599. struct page **new_pages;
  600. new_pages = kmalloc(new_array_size * sizeof(void *),
  601. GFP_KERNEL);
  602. if (!new_pages) {
  603. fw_load_abort(fw_priv);
  604. return -ENOMEM;
  605. }
  606. memcpy(new_pages, buf->pages,
  607. buf->page_array_size * sizeof(void *));
  608. memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
  609. (new_array_size - buf->page_array_size));
  610. kfree(buf->pages);
  611. buf->pages = new_pages;
  612. buf->page_array_size = new_array_size;
  613. }
  614. while (buf->nr_pages < pages_needed) {
  615. buf->pages[buf->nr_pages] =
  616. alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
  617. if (!buf->pages[buf->nr_pages]) {
  618. fw_load_abort(fw_priv);
  619. return -ENOMEM;
  620. }
  621. buf->nr_pages++;
  622. }
  623. return 0;
  624. }
  625. /**
  626. * firmware_data_write - write method for firmware
  627. * @filp: open sysfs file
  628. * @kobj: kobject for the device
  629. * @bin_attr: bin_attr structure
  630. * @buffer: buffer being written
  631. * @offset: buffer offset for write in total data store area
  632. * @count: buffer size
  633. *
  634. * Data written to the 'data' attribute will be later handed to
  635. * the driver as a firmware image.
  636. **/
  637. static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
  638. struct bin_attribute *bin_attr,
  639. char *buffer, loff_t offset, size_t count)
  640. {
  641. struct device *dev = kobj_to_dev(kobj);
  642. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  643. struct firmware_buf *buf;
  644. ssize_t retval;
  645. if (!capable(CAP_SYS_RAWIO))
  646. return -EPERM;
  647. mutex_lock(&fw_lock);
  648. buf = fw_priv->buf;
  649. if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
  650. retval = -ENODEV;
  651. goto out;
  652. }
  653. retval = fw_realloc_buffer(fw_priv, offset + count);
  654. if (retval)
  655. goto out;
  656. retval = count;
  657. while (count) {
  658. void *page_data;
  659. int page_nr = offset >> PAGE_SHIFT;
  660. int page_ofs = offset & (PAGE_SIZE - 1);
  661. int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
  662. page_data = kmap(buf->pages[page_nr]);
  663. memcpy(page_data + page_ofs, buffer, page_cnt);
  664. kunmap(buf->pages[page_nr]);
  665. buffer += page_cnt;
  666. offset += page_cnt;
  667. count -= page_cnt;
  668. }
  669. buf->size = max_t(size_t, offset, buf->size);
  670. out:
  671. mutex_unlock(&fw_lock);
  672. return retval;
  673. }
  674. static struct bin_attribute firmware_attr_data = {
  675. .attr = { .name = "data", .mode = 0644 },
  676. .size = 0,
  677. .read = firmware_data_read,
  678. .write = firmware_data_write,
  679. };
  680. static void firmware_class_timeout_work(struct work_struct *work)
  681. {
  682. struct firmware_priv *fw_priv = container_of(work,
  683. struct firmware_priv, timeout_work.work);
  684. mutex_lock(&fw_lock);
  685. fw_load_abort(fw_priv);
  686. mutex_unlock(&fw_lock);
  687. }
  688. static struct firmware_priv *
  689. fw_create_instance(struct firmware *firmware, const char *fw_name,
  690. struct device *device, bool uevent, bool nowait)
  691. {
  692. struct firmware_priv *fw_priv;
  693. struct device *f_dev;
  694. fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
  695. if (!fw_priv) {
  696. dev_err(device, "%s: kmalloc failed\n", __func__);
  697. fw_priv = ERR_PTR(-ENOMEM);
  698. goto exit;
  699. }
  700. fw_priv->nowait = nowait;
  701. fw_priv->fw = firmware;
  702. INIT_DELAYED_WORK(&fw_priv->timeout_work,
  703. firmware_class_timeout_work);
  704. f_dev = &fw_priv->dev;
  705. device_initialize(f_dev);
  706. dev_set_name(f_dev, "%s", fw_name);
  707. f_dev->parent = device;
  708. f_dev->class = &firmware_class;
  709. exit:
  710. return fw_priv;
  711. }
  712. /* load a firmware via user helper */
  713. static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
  714. long timeout)
  715. {
  716. int retval = 0;
  717. struct device *f_dev = &fw_priv->dev;
  718. struct firmware_buf *buf = fw_priv->buf;
  719. /* fall back on userspace loading */
  720. buf->is_paged_buf = true;
  721. dev_set_uevent_suppress(f_dev, true);
  722. retval = device_add(f_dev);
  723. if (retval) {
  724. dev_err(f_dev, "%s: device_register failed\n", __func__);
  725. goto err_put_dev;
  726. }
  727. retval = device_create_bin_file(f_dev, &firmware_attr_data);
  728. if (retval) {
  729. dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
  730. goto err_del_dev;
  731. }
  732. mutex_lock(&fw_lock);
  733. list_add(&buf->pending_list, &pending_fw_head);
  734. mutex_unlock(&fw_lock);
  735. retval = device_create_file(f_dev, &dev_attr_loading);
  736. if (retval) {
  737. mutex_lock(&fw_lock);
  738. list_del_init(&buf->pending_list);
  739. mutex_unlock(&fw_lock);
  740. dev_err(f_dev, "%s: device_create_file failed\n", __func__);
  741. goto err_del_bin_attr;
  742. }
  743. if (uevent) {
  744. buf->need_uevent = true;
  745. dev_set_uevent_suppress(f_dev, false);
  746. dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
  747. if (timeout != MAX_SCHEDULE_TIMEOUT)
  748. schedule_delayed_work(&fw_priv->timeout_work, timeout);
  749. kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
  750. }
  751. wait_for_completion(&buf->completion);
  752. cancel_delayed_work_sync(&fw_priv->timeout_work);
  753. device_remove_file(f_dev, &dev_attr_loading);
  754. err_del_bin_attr:
  755. device_remove_bin_file(f_dev, &firmware_attr_data);
  756. err_del_dev:
  757. device_del(f_dev);
  758. err_put_dev:
  759. put_device(f_dev);
  760. return retval;
  761. }
  762. static int fw_load_from_user_helper(struct firmware *firmware,
  763. const char *name, struct device *device,
  764. bool uevent, bool nowait, long timeout)
  765. {
  766. struct firmware_priv *fw_priv;
  767. fw_priv = fw_create_instance(firmware, name, device, uevent, nowait);
  768. if (IS_ERR(fw_priv))
  769. return PTR_ERR(fw_priv);
  770. fw_priv->buf = firmware->priv;
  771. return _request_firmware_load(fw_priv, uevent, timeout);
  772. }
  773. #ifdef CONFIG_PM_SLEEP
  774. /* kill pending requests without uevent to avoid blocking suspend */
  775. static void kill_requests_without_uevent(void)
  776. {
  777. struct firmware_buf *buf;
  778. struct firmware_buf *next;
  779. mutex_lock(&fw_lock);
  780. list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
  781. if (!buf->need_uevent)
  782. __fw_load_abort(buf);
  783. }
  784. mutex_unlock(&fw_lock);
  785. }
  786. #endif
  787. #else /* CONFIG_FW_LOADER_USER_HELPER */
  788. static inline int
  789. fw_load_from_user_helper(struct firmware *firmware, const char *name,
  790. struct device *device, bool uevent, bool nowait,
  791. long timeout)
  792. {
  793. return -ENOENT;
  794. }
  795. /* No abort during direct loading */
  796. #define is_fw_load_aborted(buf) false
  797. #ifdef CONFIG_PM_SLEEP
  798. static inline void kill_requests_without_uevent(void) { }
  799. #endif
  800. #endif /* CONFIG_FW_LOADER_USER_HELPER */
  801. /* wait until the shared firmware_buf becomes ready (or error) */
  802. static int sync_cached_firmware_buf(struct firmware_buf *buf)
  803. {
  804. int ret = 0;
  805. mutex_lock(&fw_lock);
  806. while (!test_bit(FW_STATUS_DONE, &buf->status)) {
  807. if (is_fw_load_aborted(buf)) {
  808. ret = -ENOENT;
  809. break;
  810. }
  811. mutex_unlock(&fw_lock);
  812. wait_for_completion(&buf->completion);
  813. mutex_lock(&fw_lock);
  814. }
  815. mutex_unlock(&fw_lock);
  816. return ret;
  817. }
  818. /* prepare firmware and firmware_buf structs;
  819. * return 0 if a firmware is already assigned, 1 if need to load one,
  820. * or a negative error code
  821. */
  822. static int
  823. _request_firmware_prepare(struct firmware **firmware_p, const char *name,
  824. struct device *device)
  825. {
  826. struct firmware *firmware;
  827. struct firmware_buf *buf;
  828. int ret;
  829. *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
  830. if (!firmware) {
  831. dev_err(device, "%s: kmalloc(struct firmware) failed\n",
  832. __func__);
  833. return -ENOMEM;
  834. }
  835. if (fw_get_builtin_firmware(firmware, name)) {
  836. dev_dbg(device, "firmware: using built-in firmware %s\n", name);
  837. return 0; /* assigned */
  838. }
  839. ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
  840. /*
  841. * bind with 'buf' now to avoid warning in failure path
  842. * of requesting firmware.
  843. */
  844. firmware->priv = buf;
  845. if (ret > 0) {
  846. ret = sync_cached_firmware_buf(buf);
  847. if (!ret) {
  848. fw_set_page_data(buf, firmware);
  849. return 0; /* assigned */
  850. }
  851. }
  852. if (ret < 0)
  853. return ret;
  854. return 1; /* need to load */
  855. }
  856. static int assign_firmware_buf(struct firmware *fw, struct device *device,
  857. bool skip_cache)
  858. {
  859. struct firmware_buf *buf = fw->priv;
  860. mutex_lock(&fw_lock);
  861. if (!buf->size || is_fw_load_aborted(buf)) {
  862. mutex_unlock(&fw_lock);
  863. return -ENOENT;
  864. }
  865. /*
  866. * add firmware name into devres list so that we can auto cache
  867. * and uncache firmware for device.
  868. *
  869. * device may has been deleted already, but the problem
  870. * should be fixed in devres or driver core.
  871. */
  872. if (device && !skip_cache)
  873. fw_add_devm_name(device, buf->fw_id);
  874. /*
  875. * After caching firmware image is started, let it piggyback
  876. * on request firmware.
  877. */
  878. if (buf->fwc->state == FW_LOADER_START_CACHE) {
  879. if (fw_cache_piggyback_on_request(buf->fw_id))
  880. kref_get(&buf->ref);
  881. }
  882. /* pass the pages buffer to driver at the last minute */
  883. fw_set_page_data(buf, fw);
  884. mutex_unlock(&fw_lock);
  885. return 0;
  886. }
  887. /* called from request_firmware() and request_firmware_work_func() */
  888. static int
  889. _request_firmware(const struct firmware **firmware_p, const char *name,
  890. struct device *device, bool uevent, bool nowait)
  891. {
  892. struct firmware *fw;
  893. long timeout;
  894. int ret;
  895. if (!firmware_p)
  896. return -EINVAL;
  897. ret = _request_firmware_prepare(&fw, name, device);
  898. if (ret <= 0) /* error or already assigned */
  899. goto out;
  900. ret = 0;
  901. timeout = firmware_loading_timeout();
  902. if (nowait) {
  903. timeout = usermodehelper_read_lock_wait(timeout);
  904. if (!timeout) {
  905. dev_dbg(device, "firmware: %s loading timed out\n",
  906. name);
  907. ret = -EBUSY;
  908. goto out;
  909. }
  910. } else {
  911. ret = usermodehelper_read_trylock();
  912. if (WARN_ON(ret)) {
  913. dev_err(device, "firmware: %s will not be loaded\n",
  914. name);
  915. goto out;
  916. }
  917. }
  918. ret = fw_get_filesystem_firmware(device, fw->priv);
  919. if (ret) {
  920. dev_warn(device, "Direct firmware load failed with error %d\n",
  921. ret);
  922. dev_warn(device, "Falling back to user helper\n");
  923. ret = fw_load_from_user_helper(fw, name, device,
  924. uevent, nowait, timeout);
  925. }
  926. /* don't cache firmware handled without uevent */
  927. if (!ret)
  928. ret = assign_firmware_buf(fw, device, !uevent);
  929. usermodehelper_read_unlock();
  930. out:
  931. if (ret < 0) {
  932. release_firmware(fw);
  933. fw = NULL;
  934. }
  935. *firmware_p = fw;
  936. return ret;
  937. }
  938. /**
  939. * request_firmware: - send firmware request and wait for it
  940. * @firmware_p: pointer to firmware image
  941. * @name: name of firmware file
  942. * @device: device for which firmware is being loaded
  943. *
  944. * @firmware_p will be used to return a firmware image by the name
  945. * of @name for device @device.
  946. *
  947. * Should be called from user context where sleeping is allowed.
  948. *
  949. * @name will be used as $FIRMWARE in the uevent environment and
  950. * should be distinctive enough not to be confused with any other
  951. * firmware image for this or any other device.
  952. *
  953. * Caller must hold the reference count of @device.
  954. *
  955. * The function can be called safely inside device's suspend and
  956. * resume callback.
  957. **/
  958. int
  959. request_firmware(const struct firmware **firmware_p, const char *name,
  960. struct device *device)
  961. {
  962. int ret;
  963. /* Need to pin this module until return */
  964. __module_get(THIS_MODULE);
  965. ret = _request_firmware(firmware_p, name, device, true, false);
  966. module_put(THIS_MODULE);
  967. return ret;
  968. }
  969. EXPORT_SYMBOL(request_firmware);
  970. /**
  971. * release_firmware: - release the resource associated with a firmware image
  972. * @fw: firmware resource to release
  973. **/
  974. void release_firmware(const struct firmware *fw)
  975. {
  976. if (fw) {
  977. if (!fw_is_builtin_firmware(fw))
  978. firmware_free_data(fw);
  979. kfree(fw);
  980. }
  981. }
  982. EXPORT_SYMBOL(release_firmware);
  983. /* Async support */
  984. struct firmware_work {
  985. struct work_struct work;
  986. struct module *module;
  987. const char *name;
  988. struct device *device;
  989. void *context;
  990. void (*cont)(const struct firmware *fw, void *context);
  991. bool uevent;
  992. };
  993. static void request_firmware_work_func(struct work_struct *work)
  994. {
  995. struct firmware_work *fw_work;
  996. const struct firmware *fw;
  997. fw_work = container_of(work, struct firmware_work, work);
  998. _request_firmware(&fw, fw_work->name, fw_work->device,
  999. fw_work->uevent, true);
  1000. fw_work->cont(fw, fw_work->context);
  1001. put_device(fw_work->device); /* taken in request_firmware_nowait() */
  1002. module_put(fw_work->module);
  1003. kfree(fw_work);
  1004. }
  1005. /**
  1006. * request_firmware_nowait - asynchronous version of request_firmware
  1007. * @module: module requesting the firmware
  1008. * @uevent: sends uevent to copy the firmware image if this flag
  1009. * is non-zero else the firmware copy must be done manually.
  1010. * @name: name of firmware file
  1011. * @device: device for which firmware is being loaded
  1012. * @gfp: allocation flags
  1013. * @context: will be passed over to @cont, and
  1014. * @fw may be %NULL if firmware request fails.
  1015. * @cont: function will be called asynchronously when the firmware
  1016. * request is over.
  1017. *
  1018. * Caller must hold the reference count of @device.
  1019. *
  1020. * Asynchronous variant of request_firmware() for user contexts:
  1021. * - sleep for as small periods as possible since it may
  1022. * increase kernel boot time of built-in device drivers
  1023. * requesting firmware in their ->probe() methods, if
  1024. * @gfp is GFP_KERNEL.
  1025. *
  1026. * - can't sleep at all if @gfp is GFP_ATOMIC.
  1027. **/
  1028. int
  1029. request_firmware_nowait(
  1030. struct module *module, bool uevent,
  1031. const char *name, struct device *device, gfp_t gfp, void *context,
  1032. void (*cont)(const struct firmware *fw, void *context))
  1033. {
  1034. struct firmware_work *fw_work;
  1035. fw_work = kzalloc(sizeof (struct firmware_work), gfp);
  1036. if (!fw_work)
  1037. return -ENOMEM;
  1038. fw_work->module = module;
  1039. fw_work->name = name;
  1040. fw_work->device = device;
  1041. fw_work->context = context;
  1042. fw_work->cont = cont;
  1043. fw_work->uevent = uevent;
  1044. if (!try_module_get(module)) {
  1045. kfree(fw_work);
  1046. return -EFAULT;
  1047. }
  1048. get_device(fw_work->device);
  1049. INIT_WORK(&fw_work->work, request_firmware_work_func);
  1050. schedule_work(&fw_work->work);
  1051. return 0;
  1052. }
  1053. EXPORT_SYMBOL(request_firmware_nowait);
  1054. #ifdef CONFIG_PM_SLEEP
  1055. static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
  1056. /**
  1057. * cache_firmware - cache one firmware image in kernel memory space
  1058. * @fw_name: the firmware image name
  1059. *
  1060. * Cache firmware in kernel memory so that drivers can use it when
  1061. * system isn't ready for them to request firmware image from userspace.
  1062. * Once it returns successfully, driver can use request_firmware or its
  1063. * nowait version to get the cached firmware without any interacting
  1064. * with userspace
  1065. *
  1066. * Return 0 if the firmware image has been cached successfully
  1067. * Return !0 otherwise
  1068. *
  1069. */
  1070. static int cache_firmware(const char *fw_name)
  1071. {
  1072. int ret;
  1073. const struct firmware *fw;
  1074. pr_debug("%s: %s\n", __func__, fw_name);
  1075. ret = request_firmware(&fw, fw_name, NULL);
  1076. if (!ret)
  1077. kfree(fw);
  1078. pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
  1079. return ret;
  1080. }
  1081. static struct firmware_buf *fw_lookup_buf(const char *fw_name)
  1082. {
  1083. struct firmware_buf *tmp;
  1084. struct firmware_cache *fwc = &fw_cache;
  1085. spin_lock(&fwc->lock);
  1086. tmp = __fw_lookup_buf(fw_name);
  1087. spin_unlock(&fwc->lock);
  1088. return tmp;
  1089. }
  1090. /**
  1091. * uncache_firmware - remove one cached firmware image
  1092. * @fw_name: the firmware image name
  1093. *
  1094. * Uncache one firmware image which has been cached successfully
  1095. * before.
  1096. *
  1097. * Return 0 if the firmware cache has been removed successfully
  1098. * Return !0 otherwise
  1099. *
  1100. */
  1101. static int uncache_firmware(const char *fw_name)
  1102. {
  1103. struct firmware_buf *buf;
  1104. struct firmware fw;
  1105. pr_debug("%s: %s\n", __func__, fw_name);
  1106. if (fw_get_builtin_firmware(&fw, fw_name))
  1107. return 0;
  1108. buf = fw_lookup_buf(fw_name);
  1109. if (buf) {
  1110. fw_free_buf(buf);
  1111. return 0;
  1112. }
  1113. return -EINVAL;
  1114. }
  1115. static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
  1116. {
  1117. struct fw_cache_entry *fce;
  1118. fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
  1119. if (!fce)
  1120. goto exit;
  1121. strcpy(fce->name, name);
  1122. exit:
  1123. return fce;
  1124. }
  1125. static int __fw_entry_found(const char *name)
  1126. {
  1127. struct firmware_cache *fwc = &fw_cache;
  1128. struct fw_cache_entry *fce;
  1129. list_for_each_entry(fce, &fwc->fw_names, list) {
  1130. if (!strcmp(fce->name, name))
  1131. return 1;
  1132. }
  1133. return 0;
  1134. }
  1135. static int fw_cache_piggyback_on_request(const char *name)
  1136. {
  1137. struct firmware_cache *fwc = &fw_cache;
  1138. struct fw_cache_entry *fce;
  1139. int ret = 0;
  1140. spin_lock(&fwc->name_lock);
  1141. if (__fw_entry_found(name))
  1142. goto found;
  1143. fce = alloc_fw_cache_entry(name);
  1144. if (fce) {
  1145. ret = 1;
  1146. list_add(&fce->list, &fwc->fw_names);
  1147. pr_debug("%s: fw: %s\n", __func__, name);
  1148. }
  1149. found:
  1150. spin_unlock(&fwc->name_lock);
  1151. return ret;
  1152. }
  1153. static void free_fw_cache_entry(struct fw_cache_entry *fce)
  1154. {
  1155. kfree(fce);
  1156. }
  1157. static void __async_dev_cache_fw_image(void *fw_entry,
  1158. async_cookie_t cookie)
  1159. {
  1160. struct fw_cache_entry *fce = fw_entry;
  1161. struct firmware_cache *fwc = &fw_cache;
  1162. int ret;
  1163. ret = cache_firmware(fce->name);
  1164. if (ret) {
  1165. spin_lock(&fwc->name_lock);
  1166. list_del(&fce->list);
  1167. spin_unlock(&fwc->name_lock);
  1168. free_fw_cache_entry(fce);
  1169. }
  1170. }
  1171. /* called with dev->devres_lock held */
  1172. static void dev_create_fw_entry(struct device *dev, void *res,
  1173. void *data)
  1174. {
  1175. struct fw_name_devm *fwn = res;
  1176. const char *fw_name = fwn->name;
  1177. struct list_head *head = data;
  1178. struct fw_cache_entry *fce;
  1179. fce = alloc_fw_cache_entry(fw_name);
  1180. if (fce)
  1181. list_add(&fce->list, head);
  1182. }
  1183. static int devm_name_match(struct device *dev, void *res,
  1184. void *match_data)
  1185. {
  1186. struct fw_name_devm *fwn = res;
  1187. return (fwn->magic == (unsigned long)match_data);
  1188. }
  1189. static void dev_cache_fw_image(struct device *dev, void *data)
  1190. {
  1191. LIST_HEAD(todo);
  1192. struct fw_cache_entry *fce;
  1193. struct fw_cache_entry *fce_next;
  1194. struct firmware_cache *fwc = &fw_cache;
  1195. devres_for_each_res(dev, fw_name_devm_release,
  1196. devm_name_match, &fw_cache,
  1197. dev_create_fw_entry, &todo);
  1198. list_for_each_entry_safe(fce, fce_next, &todo, list) {
  1199. list_del(&fce->list);
  1200. spin_lock(&fwc->name_lock);
  1201. /* only one cache entry for one firmware */
  1202. if (!__fw_entry_found(fce->name)) {
  1203. list_add(&fce->list, &fwc->fw_names);
  1204. } else {
  1205. free_fw_cache_entry(fce);
  1206. fce = NULL;
  1207. }
  1208. spin_unlock(&fwc->name_lock);
  1209. if (fce)
  1210. async_schedule_domain(__async_dev_cache_fw_image,
  1211. (void *)fce,
  1212. &fw_cache_domain);
  1213. }
  1214. }
  1215. static void __device_uncache_fw_images(void)
  1216. {
  1217. struct firmware_cache *fwc = &fw_cache;
  1218. struct fw_cache_entry *fce;
  1219. spin_lock(&fwc->name_lock);
  1220. while (!list_empty(&fwc->fw_names)) {
  1221. fce = list_entry(fwc->fw_names.next,
  1222. struct fw_cache_entry, list);
  1223. list_del(&fce->list);
  1224. spin_unlock(&fwc->name_lock);
  1225. uncache_firmware(fce->name);
  1226. free_fw_cache_entry(fce);
  1227. spin_lock(&fwc->name_lock);
  1228. }
  1229. spin_unlock(&fwc->name_lock);
  1230. }
  1231. /**
  1232. * device_cache_fw_images - cache devices' firmware
  1233. *
  1234. * If one device called request_firmware or its nowait version
  1235. * successfully before, the firmware names are recored into the
  1236. * device's devres link list, so device_cache_fw_images can call
  1237. * cache_firmware() to cache these firmwares for the device,
  1238. * then the device driver can load its firmwares easily at
  1239. * time when system is not ready to complete loading firmware.
  1240. */
  1241. static void device_cache_fw_images(void)
  1242. {
  1243. struct firmware_cache *fwc = &fw_cache;
  1244. int old_timeout;
  1245. DEFINE_WAIT(wait);
  1246. pr_debug("%s\n", __func__);
  1247. /* cancel uncache work */
  1248. cancel_delayed_work_sync(&fwc->work);
  1249. /*
  1250. * use small loading timeout for caching devices' firmware
  1251. * because all these firmware images have been loaded
  1252. * successfully at lease once, also system is ready for
  1253. * completing firmware loading now. The maximum size of
  1254. * firmware in current distributions is about 2M bytes,
  1255. * so 10 secs should be enough.
  1256. */
  1257. old_timeout = loading_timeout;
  1258. loading_timeout = 10;
  1259. mutex_lock(&fw_lock);
  1260. fwc->state = FW_LOADER_START_CACHE;
  1261. dpm_for_each_dev(NULL, dev_cache_fw_image);
  1262. mutex_unlock(&fw_lock);
  1263. /* wait for completion of caching firmware for all devices */
  1264. async_synchronize_full_domain(&fw_cache_domain);
  1265. loading_timeout = old_timeout;
  1266. }
  1267. /**
  1268. * device_uncache_fw_images - uncache devices' firmware
  1269. *
  1270. * uncache all firmwares which have been cached successfully
  1271. * by device_uncache_fw_images earlier
  1272. */
  1273. static void device_uncache_fw_images(void)
  1274. {
  1275. pr_debug("%s\n", __func__);
  1276. __device_uncache_fw_images();
  1277. }
  1278. static void device_uncache_fw_images_work(struct work_struct *work)
  1279. {
  1280. device_uncache_fw_images();
  1281. }
  1282. /**
  1283. * device_uncache_fw_images_delay - uncache devices firmwares
  1284. * @delay: number of milliseconds to delay uncache device firmwares
  1285. *
  1286. * uncache all devices's firmwares which has been cached successfully
  1287. * by device_cache_fw_images after @delay milliseconds.
  1288. */
  1289. static void device_uncache_fw_images_delay(unsigned long delay)
  1290. {
  1291. schedule_delayed_work(&fw_cache.work,
  1292. msecs_to_jiffies(delay));
  1293. }
  1294. static int fw_pm_notify(struct notifier_block *notify_block,
  1295. unsigned long mode, void *unused)
  1296. {
  1297. switch (mode) {
  1298. case PM_HIBERNATION_PREPARE:
  1299. case PM_SUSPEND_PREPARE:
  1300. kill_requests_without_uevent();
  1301. device_cache_fw_images();
  1302. break;
  1303. case PM_POST_SUSPEND:
  1304. case PM_POST_HIBERNATION:
  1305. case PM_POST_RESTORE:
  1306. /*
  1307. * In case that system sleep failed and syscore_suspend is
  1308. * not called.
  1309. */
  1310. mutex_lock(&fw_lock);
  1311. fw_cache.state = FW_LOADER_NO_CACHE;
  1312. mutex_unlock(&fw_lock);
  1313. device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
  1314. break;
  1315. }
  1316. return 0;
  1317. }
  1318. /* stop caching firmware once syscore_suspend is reached */
  1319. static int fw_suspend(void)
  1320. {
  1321. fw_cache.state = FW_LOADER_NO_CACHE;
  1322. return 0;
  1323. }
  1324. static struct syscore_ops fw_syscore_ops = {
  1325. .suspend = fw_suspend,
  1326. };
  1327. #else
  1328. static int fw_cache_piggyback_on_request(const char *name)
  1329. {
  1330. return 0;
  1331. }
  1332. #endif
  1333. static void __init fw_cache_init(void)
  1334. {
  1335. spin_lock_init(&fw_cache.lock);
  1336. INIT_LIST_HEAD(&fw_cache.head);
  1337. fw_cache.state = FW_LOADER_NO_CACHE;
  1338. #ifdef CONFIG_PM_SLEEP
  1339. spin_lock_init(&fw_cache.name_lock);
  1340. INIT_LIST_HEAD(&fw_cache.fw_names);
  1341. INIT_DELAYED_WORK(&fw_cache.work,
  1342. device_uncache_fw_images_work);
  1343. fw_cache.pm_notify.notifier_call = fw_pm_notify;
  1344. register_pm_notifier(&fw_cache.pm_notify);
  1345. register_syscore_ops(&fw_syscore_ops);
  1346. #endif
  1347. }
  1348. static int __init firmware_class_init(void)
  1349. {
  1350. fw_cache_init();
  1351. #ifdef CONFIG_FW_LOADER_USER_HELPER
  1352. register_reboot_notifier(&fw_shutdown_nb);
  1353. return class_register(&firmware_class);
  1354. #else
  1355. return 0;
  1356. #endif
  1357. }
  1358. static void __exit firmware_class_exit(void)
  1359. {
  1360. #ifdef CONFIG_PM_SLEEP
  1361. unregister_syscore_ops(&fw_syscore_ops);
  1362. unregister_pm_notifier(&fw_cache.pm_notify);
  1363. #endif
  1364. #ifdef CONFIG_FW_LOADER_USER_HELPER
  1365. unregister_reboot_notifier(&fw_shutdown_nb);
  1366. class_unregister(&firmware_class);
  1367. #endif
  1368. }
  1369. fs_initcall(firmware_class_init);
  1370. module_exit(firmware_class_exit);