firmware_class.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  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. {
  831. struct firmware_buf *buf = fw->priv;
  832. mutex_lock(&fw_lock);
  833. if (!buf->size || is_fw_load_aborted(buf)) {
  834. mutex_unlock(&fw_lock);
  835. return -ENOENT;
  836. }
  837. /*
  838. * add firmware name into devres list so that we can auto cache
  839. * and uncache firmware for device.
  840. *
  841. * device may has been deleted already, but the problem
  842. * should be fixed in devres or driver core.
  843. */
  844. if (device)
  845. fw_add_devm_name(device, buf->fw_id);
  846. /*
  847. * After caching firmware image is started, let it piggyback
  848. * on request firmware.
  849. */
  850. if (buf->fwc->state == FW_LOADER_START_CACHE) {
  851. if (fw_cache_piggyback_on_request(buf->fw_id))
  852. kref_get(&buf->ref);
  853. }
  854. /* pass the pages buffer to driver at the last minute */
  855. fw_set_page_data(buf, fw);
  856. mutex_unlock(&fw_lock);
  857. return 0;
  858. }
  859. /* called from request_firmware() and request_firmware_work_func() */
  860. static int
  861. _request_firmware(const struct firmware **firmware_p, const char *name,
  862. struct device *device, bool uevent, bool nowait)
  863. {
  864. struct firmware *fw;
  865. long timeout;
  866. int ret;
  867. if (!firmware_p)
  868. return -EINVAL;
  869. ret = _request_firmware_prepare(&fw, name, device);
  870. if (ret <= 0) /* error or already assigned */
  871. goto out;
  872. ret = 0;
  873. timeout = firmware_loading_timeout();
  874. if (nowait) {
  875. timeout = usermodehelper_read_lock_wait(timeout);
  876. if (!timeout) {
  877. dev_dbg(device, "firmware: %s loading timed out\n",
  878. name);
  879. ret = -EBUSY;
  880. goto out;
  881. }
  882. } else {
  883. ret = usermodehelper_read_trylock();
  884. if (WARN_ON(ret)) {
  885. dev_err(device, "firmware: %s will not be loaded\n",
  886. name);
  887. goto out;
  888. }
  889. }
  890. if (!fw_get_filesystem_firmware(device, fw->priv))
  891. ret = fw_load_from_user_helper(fw, name, device,
  892. uevent, nowait, timeout);
  893. if (!ret)
  894. ret = assign_firmware_buf(fw, device);
  895. usermodehelper_read_unlock();
  896. out:
  897. if (ret < 0) {
  898. release_firmware(fw);
  899. fw = NULL;
  900. }
  901. *firmware_p = fw;
  902. return ret;
  903. }
  904. /**
  905. * request_firmware: - send firmware request and wait for it
  906. * @firmware_p: pointer to firmware image
  907. * @name: name of firmware file
  908. * @device: device for which firmware is being loaded
  909. *
  910. * @firmware_p will be used to return a firmware image by the name
  911. * of @name for device @device.
  912. *
  913. * Should be called from user context where sleeping is allowed.
  914. *
  915. * @name will be used as $FIRMWARE in the uevent environment and
  916. * should be distinctive enough not to be confused with any other
  917. * firmware image for this or any other device.
  918. *
  919. * Caller must hold the reference count of @device.
  920. *
  921. * The function can be called safely inside device's suspend and
  922. * resume callback.
  923. **/
  924. int
  925. request_firmware(const struct firmware **firmware_p, const char *name,
  926. struct device *device)
  927. {
  928. return _request_firmware(firmware_p, name, device, true, false);
  929. }
  930. EXPORT_SYMBOL(request_firmware);
  931. /**
  932. * release_firmware: - release the resource associated with a firmware image
  933. * @fw: firmware resource to release
  934. **/
  935. void release_firmware(const struct firmware *fw)
  936. {
  937. if (fw) {
  938. if (!fw_is_builtin_firmware(fw))
  939. firmware_free_data(fw);
  940. kfree(fw);
  941. }
  942. }
  943. EXPORT_SYMBOL(release_firmware);
  944. /* Async support */
  945. struct firmware_work {
  946. struct work_struct work;
  947. struct module *module;
  948. const char *name;
  949. struct device *device;
  950. void *context;
  951. void (*cont)(const struct firmware *fw, void *context);
  952. bool uevent;
  953. };
  954. static void request_firmware_work_func(struct work_struct *work)
  955. {
  956. struct firmware_work *fw_work;
  957. const struct firmware *fw;
  958. fw_work = container_of(work, struct firmware_work, work);
  959. _request_firmware(&fw, fw_work->name, fw_work->device,
  960. fw_work->uevent, true);
  961. fw_work->cont(fw, fw_work->context);
  962. put_device(fw_work->device); /* taken in request_firmware_nowait() */
  963. module_put(fw_work->module);
  964. kfree(fw_work);
  965. }
  966. /**
  967. * request_firmware_nowait - asynchronous version of request_firmware
  968. * @module: module requesting the firmware
  969. * @uevent: sends uevent to copy the firmware image if this flag
  970. * is non-zero else the firmware copy must be done manually.
  971. * @name: name of firmware file
  972. * @device: device for which firmware is being loaded
  973. * @gfp: allocation flags
  974. * @context: will be passed over to @cont, and
  975. * @fw may be %NULL if firmware request fails.
  976. * @cont: function will be called asynchronously when the firmware
  977. * request is over.
  978. *
  979. * Caller must hold the reference count of @device.
  980. *
  981. * Asynchronous variant of request_firmware() for user contexts:
  982. * - sleep for as small periods as possible since it may
  983. * increase kernel boot time of built-in device drivers
  984. * requesting firmware in their ->probe() methods, if
  985. * @gfp is GFP_KERNEL.
  986. *
  987. * - can't sleep at all if @gfp is GFP_ATOMIC.
  988. **/
  989. int
  990. request_firmware_nowait(
  991. struct module *module, bool uevent,
  992. const char *name, struct device *device, gfp_t gfp, void *context,
  993. void (*cont)(const struct firmware *fw, void *context))
  994. {
  995. struct firmware_work *fw_work;
  996. fw_work = kzalloc(sizeof (struct firmware_work), gfp);
  997. if (!fw_work)
  998. return -ENOMEM;
  999. fw_work->module = module;
  1000. fw_work->name = name;
  1001. fw_work->device = device;
  1002. fw_work->context = context;
  1003. fw_work->cont = cont;
  1004. fw_work->uevent = uevent;
  1005. if (!try_module_get(module)) {
  1006. kfree(fw_work);
  1007. return -EFAULT;
  1008. }
  1009. get_device(fw_work->device);
  1010. INIT_WORK(&fw_work->work, request_firmware_work_func);
  1011. schedule_work(&fw_work->work);
  1012. return 0;
  1013. }
  1014. EXPORT_SYMBOL(request_firmware_nowait);
  1015. /**
  1016. * cache_firmware - cache one firmware image in kernel memory space
  1017. * @fw_name: the firmware image name
  1018. *
  1019. * Cache firmware in kernel memory so that drivers can use it when
  1020. * system isn't ready for them to request firmware image from userspace.
  1021. * Once it returns successfully, driver can use request_firmware or its
  1022. * nowait version to get the cached firmware without any interacting
  1023. * with userspace
  1024. *
  1025. * Return 0 if the firmware image has been cached successfully
  1026. * Return !0 otherwise
  1027. *
  1028. */
  1029. int cache_firmware(const char *fw_name)
  1030. {
  1031. int ret;
  1032. const struct firmware *fw;
  1033. pr_debug("%s: %s\n", __func__, fw_name);
  1034. ret = request_firmware(&fw, fw_name, NULL);
  1035. if (!ret)
  1036. kfree(fw);
  1037. pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
  1038. return ret;
  1039. }
  1040. EXPORT_SYMBOL_GPL(cache_firmware);
  1041. /**
  1042. * uncache_firmware - remove one cached firmware image
  1043. * @fw_name: the firmware image name
  1044. *
  1045. * Uncache one firmware image which has been cached successfully
  1046. * before.
  1047. *
  1048. * Return 0 if the firmware cache has been removed successfully
  1049. * Return !0 otherwise
  1050. *
  1051. */
  1052. int uncache_firmware(const char *fw_name)
  1053. {
  1054. struct firmware_buf *buf;
  1055. struct firmware fw;
  1056. pr_debug("%s: %s\n", __func__, fw_name);
  1057. if (fw_get_builtin_firmware(&fw, fw_name))
  1058. return 0;
  1059. buf = fw_lookup_buf(fw_name);
  1060. if (buf) {
  1061. fw_free_buf(buf);
  1062. return 0;
  1063. }
  1064. return -EINVAL;
  1065. }
  1066. EXPORT_SYMBOL_GPL(uncache_firmware);
  1067. #ifdef CONFIG_PM_SLEEP
  1068. static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
  1069. static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
  1070. {
  1071. struct fw_cache_entry *fce;
  1072. fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
  1073. if (!fce)
  1074. goto exit;
  1075. strcpy(fce->name, name);
  1076. exit:
  1077. return fce;
  1078. }
  1079. static int __fw_entry_found(const char *name)
  1080. {
  1081. struct firmware_cache *fwc = &fw_cache;
  1082. struct fw_cache_entry *fce;
  1083. list_for_each_entry(fce, &fwc->fw_names, list) {
  1084. if (!strcmp(fce->name, name))
  1085. return 1;
  1086. }
  1087. return 0;
  1088. }
  1089. static int fw_cache_piggyback_on_request(const char *name)
  1090. {
  1091. struct firmware_cache *fwc = &fw_cache;
  1092. struct fw_cache_entry *fce;
  1093. int ret = 0;
  1094. spin_lock(&fwc->name_lock);
  1095. if (__fw_entry_found(name))
  1096. goto found;
  1097. fce = alloc_fw_cache_entry(name);
  1098. if (fce) {
  1099. ret = 1;
  1100. list_add(&fce->list, &fwc->fw_names);
  1101. pr_debug("%s: fw: %s\n", __func__, name);
  1102. }
  1103. found:
  1104. spin_unlock(&fwc->name_lock);
  1105. return ret;
  1106. }
  1107. static void free_fw_cache_entry(struct fw_cache_entry *fce)
  1108. {
  1109. kfree(fce);
  1110. }
  1111. static void __async_dev_cache_fw_image(void *fw_entry,
  1112. async_cookie_t cookie)
  1113. {
  1114. struct fw_cache_entry *fce = fw_entry;
  1115. struct firmware_cache *fwc = &fw_cache;
  1116. int ret;
  1117. ret = cache_firmware(fce->name);
  1118. if (ret) {
  1119. spin_lock(&fwc->name_lock);
  1120. list_del(&fce->list);
  1121. spin_unlock(&fwc->name_lock);
  1122. free_fw_cache_entry(fce);
  1123. }
  1124. }
  1125. /* called with dev->devres_lock held */
  1126. static void dev_create_fw_entry(struct device *dev, void *res,
  1127. void *data)
  1128. {
  1129. struct fw_name_devm *fwn = res;
  1130. const char *fw_name = fwn->name;
  1131. struct list_head *head = data;
  1132. struct fw_cache_entry *fce;
  1133. fce = alloc_fw_cache_entry(fw_name);
  1134. if (fce)
  1135. list_add(&fce->list, head);
  1136. }
  1137. static int devm_name_match(struct device *dev, void *res,
  1138. void *match_data)
  1139. {
  1140. struct fw_name_devm *fwn = res;
  1141. return (fwn->magic == (unsigned long)match_data);
  1142. }
  1143. static void dev_cache_fw_image(struct device *dev, void *data)
  1144. {
  1145. LIST_HEAD(todo);
  1146. struct fw_cache_entry *fce;
  1147. struct fw_cache_entry *fce_next;
  1148. struct firmware_cache *fwc = &fw_cache;
  1149. devres_for_each_res(dev, fw_name_devm_release,
  1150. devm_name_match, &fw_cache,
  1151. dev_create_fw_entry, &todo);
  1152. list_for_each_entry_safe(fce, fce_next, &todo, list) {
  1153. list_del(&fce->list);
  1154. spin_lock(&fwc->name_lock);
  1155. /* only one cache entry for one firmware */
  1156. if (!__fw_entry_found(fce->name)) {
  1157. list_add(&fce->list, &fwc->fw_names);
  1158. } else {
  1159. free_fw_cache_entry(fce);
  1160. fce = NULL;
  1161. }
  1162. spin_unlock(&fwc->name_lock);
  1163. if (fce)
  1164. async_schedule_domain(__async_dev_cache_fw_image,
  1165. (void *)fce,
  1166. &fw_cache_domain);
  1167. }
  1168. }
  1169. static void __device_uncache_fw_images(void)
  1170. {
  1171. struct firmware_cache *fwc = &fw_cache;
  1172. struct fw_cache_entry *fce;
  1173. spin_lock(&fwc->name_lock);
  1174. while (!list_empty(&fwc->fw_names)) {
  1175. fce = list_entry(fwc->fw_names.next,
  1176. struct fw_cache_entry, list);
  1177. list_del(&fce->list);
  1178. spin_unlock(&fwc->name_lock);
  1179. uncache_firmware(fce->name);
  1180. free_fw_cache_entry(fce);
  1181. spin_lock(&fwc->name_lock);
  1182. }
  1183. spin_unlock(&fwc->name_lock);
  1184. }
  1185. /**
  1186. * device_cache_fw_images - cache devices' firmware
  1187. *
  1188. * If one device called request_firmware or its nowait version
  1189. * successfully before, the firmware names are recored into the
  1190. * device's devres link list, so device_cache_fw_images can call
  1191. * cache_firmware() to cache these firmwares for the device,
  1192. * then the device driver can load its firmwares easily at
  1193. * time when system is not ready to complete loading firmware.
  1194. */
  1195. static void device_cache_fw_images(void)
  1196. {
  1197. struct firmware_cache *fwc = &fw_cache;
  1198. int old_timeout;
  1199. DEFINE_WAIT(wait);
  1200. pr_debug("%s\n", __func__);
  1201. /* cancel uncache work */
  1202. cancel_delayed_work_sync(&fwc->work);
  1203. /*
  1204. * use small loading timeout for caching devices' firmware
  1205. * because all these firmware images have been loaded
  1206. * successfully at lease once, also system is ready for
  1207. * completing firmware loading now. The maximum size of
  1208. * firmware in current distributions is about 2M bytes,
  1209. * so 10 secs should be enough.
  1210. */
  1211. old_timeout = loading_timeout;
  1212. loading_timeout = 10;
  1213. mutex_lock(&fw_lock);
  1214. fwc->state = FW_LOADER_START_CACHE;
  1215. dpm_for_each_dev(NULL, dev_cache_fw_image);
  1216. mutex_unlock(&fw_lock);
  1217. /* wait for completion of caching firmware for all devices */
  1218. async_synchronize_full_domain(&fw_cache_domain);
  1219. loading_timeout = old_timeout;
  1220. }
  1221. /**
  1222. * device_uncache_fw_images - uncache devices' firmware
  1223. *
  1224. * uncache all firmwares which have been cached successfully
  1225. * by device_uncache_fw_images earlier
  1226. */
  1227. static void device_uncache_fw_images(void)
  1228. {
  1229. pr_debug("%s\n", __func__);
  1230. __device_uncache_fw_images();
  1231. }
  1232. static void device_uncache_fw_images_work(struct work_struct *work)
  1233. {
  1234. device_uncache_fw_images();
  1235. }
  1236. /**
  1237. * device_uncache_fw_images_delay - uncache devices firmwares
  1238. * @delay: number of milliseconds to delay uncache device firmwares
  1239. *
  1240. * uncache all devices's firmwares which has been cached successfully
  1241. * by device_cache_fw_images after @delay milliseconds.
  1242. */
  1243. static void device_uncache_fw_images_delay(unsigned long delay)
  1244. {
  1245. schedule_delayed_work(&fw_cache.work,
  1246. msecs_to_jiffies(delay));
  1247. }
  1248. static int fw_pm_notify(struct notifier_block *notify_block,
  1249. unsigned long mode, void *unused)
  1250. {
  1251. switch (mode) {
  1252. case PM_HIBERNATION_PREPARE:
  1253. case PM_SUSPEND_PREPARE:
  1254. device_cache_fw_images();
  1255. break;
  1256. case PM_POST_SUSPEND:
  1257. case PM_POST_HIBERNATION:
  1258. case PM_POST_RESTORE:
  1259. /*
  1260. * In case that system sleep failed and syscore_suspend is
  1261. * not called.
  1262. */
  1263. mutex_lock(&fw_lock);
  1264. fw_cache.state = FW_LOADER_NO_CACHE;
  1265. mutex_unlock(&fw_lock);
  1266. device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
  1267. break;
  1268. }
  1269. return 0;
  1270. }
  1271. /* stop caching firmware once syscore_suspend is reached */
  1272. static int fw_suspend(void)
  1273. {
  1274. fw_cache.state = FW_LOADER_NO_CACHE;
  1275. return 0;
  1276. }
  1277. static struct syscore_ops fw_syscore_ops = {
  1278. .suspend = fw_suspend,
  1279. };
  1280. #else
  1281. static int fw_cache_piggyback_on_request(const char *name)
  1282. {
  1283. return 0;
  1284. }
  1285. #endif
  1286. static void __init fw_cache_init(void)
  1287. {
  1288. spin_lock_init(&fw_cache.lock);
  1289. INIT_LIST_HEAD(&fw_cache.head);
  1290. fw_cache.state = FW_LOADER_NO_CACHE;
  1291. #ifdef CONFIG_PM_SLEEP
  1292. spin_lock_init(&fw_cache.name_lock);
  1293. INIT_LIST_HEAD(&fw_cache.fw_names);
  1294. INIT_DELAYED_WORK(&fw_cache.work,
  1295. device_uncache_fw_images_work);
  1296. fw_cache.pm_notify.notifier_call = fw_pm_notify;
  1297. register_pm_notifier(&fw_cache.pm_notify);
  1298. register_syscore_ops(&fw_syscore_ops);
  1299. #endif
  1300. }
  1301. static int __init firmware_class_init(void)
  1302. {
  1303. fw_cache_init();
  1304. #ifdef CONFIG_FW_LOADER_USER_HELPER
  1305. register_reboot_notifier(&fw_shutdown_nb);
  1306. return class_register(&firmware_class);
  1307. #else
  1308. return 0;
  1309. #endif
  1310. }
  1311. static void __exit firmware_class_exit(void)
  1312. {
  1313. #ifdef CONFIG_PM_SLEEP
  1314. unregister_syscore_ops(&fw_syscore_ops);
  1315. unregister_pm_notifier(&fw_cache.pm_notify);
  1316. #endif
  1317. #ifdef CONFIG_FW_LOADER_USER_HELPER
  1318. unregister_reboot_notifier(&fw_shutdown_nb);
  1319. class_unregister(&firmware_class);
  1320. #endif
  1321. }
  1322. fs_initcall(firmware_class_init);
  1323. module_exit(firmware_class_exit);