firmware_class.c 37 KB

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