firmware_class.c 39 KB

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