firmware_class.c 37 KB

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