main.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * main.c - Multi purpose firmware loading support
  4. *
  5. * Copyright (c) 2003 Manuel Estrada Sainz
  6. *
  7. * Please see Documentation/firmware_class/ for more information.
  8. *
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/capability.h>
  12. #include <linux/device.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/timer.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/bitops.h>
  19. #include <linux/mutex.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/highmem.h>
  22. #include <linux/firmware.h>
  23. #include <linux/slab.h>
  24. #include <linux/sched.h>
  25. #include <linux/file.h>
  26. #include <linux/list.h>
  27. #include <linux/fs.h>
  28. #include <linux/async.h>
  29. #include <linux/pm.h>
  30. #include <linux/suspend.h>
  31. #include <linux/syscore_ops.h>
  32. #include <linux/reboot.h>
  33. #include <linux/security.h>
  34. #include <generated/utsrelease.h>
  35. #include "../base.h"
  36. #include "firmware.h"
  37. #include "fallback.h"
  38. MODULE_AUTHOR("Manuel Estrada Sainz");
  39. MODULE_DESCRIPTION("Multi purpose firmware loading support");
  40. MODULE_LICENSE("GPL");
  41. struct firmware_cache {
  42. /* firmware_buf instance will be added into the below list */
  43. spinlock_t lock;
  44. struct list_head head;
  45. int state;
  46. #ifdef CONFIG_PM_SLEEP
  47. /*
  48. * Names of firmware images which have been cached successfully
  49. * will be added into the below list so that device uncache
  50. * helper can trace which firmware images have been cached
  51. * before.
  52. */
  53. spinlock_t name_lock;
  54. struct list_head fw_names;
  55. struct delayed_work work;
  56. struct notifier_block pm_notify;
  57. #endif
  58. };
  59. struct fw_cache_entry {
  60. struct list_head list;
  61. const char *name;
  62. };
  63. struct fw_name_devm {
  64. unsigned long magic;
  65. const char *name;
  66. };
  67. static inline struct fw_priv *to_fw_priv(struct kref *ref)
  68. {
  69. return container_of(ref, struct fw_priv, ref);
  70. }
  71. #define FW_LOADER_NO_CACHE 0
  72. #define FW_LOADER_START_CACHE 1
  73. /* fw_lock could be moved to 'struct fw_sysfs' but since it is just
  74. * guarding for corner cases a global lock should be OK */
  75. DEFINE_MUTEX(fw_lock);
  76. static struct firmware_cache fw_cache;
  77. /* Builtin firmware support */
  78. #ifdef CONFIG_FW_LOADER
  79. extern struct builtin_fw __start_builtin_fw[];
  80. extern struct builtin_fw __end_builtin_fw[];
  81. static void fw_copy_to_prealloc_buf(struct firmware *fw,
  82. void *buf, size_t size)
  83. {
  84. if (!buf || size < fw->size)
  85. return;
  86. memcpy(buf, fw->data, fw->size);
  87. }
  88. static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
  89. void *buf, size_t size)
  90. {
  91. struct builtin_fw *b_fw;
  92. for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
  93. if (strcmp(name, b_fw->name) == 0) {
  94. fw->size = b_fw->size;
  95. fw->data = b_fw->data;
  96. fw_copy_to_prealloc_buf(fw, buf, size);
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. static bool fw_is_builtin_firmware(const struct firmware *fw)
  103. {
  104. struct builtin_fw *b_fw;
  105. for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
  106. if (fw->data == b_fw->data)
  107. return true;
  108. return false;
  109. }
  110. #else /* Module case - no builtin firmware support */
  111. static inline bool fw_get_builtin_firmware(struct firmware *fw,
  112. const char *name, void *buf,
  113. size_t size)
  114. {
  115. return false;
  116. }
  117. static inline bool fw_is_builtin_firmware(const struct firmware *fw)
  118. {
  119. return false;
  120. }
  121. #endif
  122. static void fw_state_init(struct fw_priv *fw_priv)
  123. {
  124. struct fw_state *fw_st = &fw_priv->fw_st;
  125. init_completion(&fw_st->completion);
  126. fw_st->status = FW_STATUS_UNKNOWN;
  127. }
  128. static inline int fw_state_wait(struct fw_priv *fw_priv)
  129. {
  130. return __fw_state_wait_common(fw_priv, MAX_SCHEDULE_TIMEOUT);
  131. }
  132. static int fw_cache_piggyback_on_request(const char *name);
  133. static struct fw_priv *__allocate_fw_priv(const char *fw_name,
  134. struct firmware_cache *fwc,
  135. void *dbuf, size_t size)
  136. {
  137. struct fw_priv *fw_priv;
  138. fw_priv = kzalloc(sizeof(*fw_priv), GFP_ATOMIC);
  139. if (!fw_priv)
  140. return NULL;
  141. fw_priv->fw_name = kstrdup_const(fw_name, GFP_ATOMIC);
  142. if (!fw_priv->fw_name) {
  143. kfree(fw_priv);
  144. return NULL;
  145. }
  146. kref_init(&fw_priv->ref);
  147. fw_priv->fwc = fwc;
  148. fw_priv->data = dbuf;
  149. fw_priv->allocated_size = size;
  150. fw_state_init(fw_priv);
  151. #ifdef CONFIG_FW_LOADER_USER_HELPER
  152. INIT_LIST_HEAD(&fw_priv->pending_list);
  153. #endif
  154. pr_debug("%s: fw-%s fw_priv=%p\n", __func__, fw_name, fw_priv);
  155. return fw_priv;
  156. }
  157. static struct fw_priv *__lookup_fw_priv(const char *fw_name)
  158. {
  159. struct fw_priv *tmp;
  160. struct firmware_cache *fwc = &fw_cache;
  161. list_for_each_entry(tmp, &fwc->head, list)
  162. if (!strcmp(tmp->fw_name, fw_name))
  163. return tmp;
  164. return NULL;
  165. }
  166. /* Returns 1 for batching firmware requests with the same name */
  167. static int alloc_lookup_fw_priv(const char *fw_name,
  168. struct firmware_cache *fwc,
  169. struct fw_priv **fw_priv, void *dbuf,
  170. size_t size)
  171. {
  172. struct fw_priv *tmp;
  173. spin_lock(&fwc->lock);
  174. tmp = __lookup_fw_priv(fw_name);
  175. if (tmp) {
  176. kref_get(&tmp->ref);
  177. spin_unlock(&fwc->lock);
  178. *fw_priv = tmp;
  179. pr_debug("batched request - sharing the same struct fw_priv and lookup for multiple requests\n");
  180. return 1;
  181. }
  182. tmp = __allocate_fw_priv(fw_name, fwc, dbuf, size);
  183. if (tmp)
  184. list_add(&tmp->list, &fwc->head);
  185. spin_unlock(&fwc->lock);
  186. *fw_priv = tmp;
  187. return tmp ? 0 : -ENOMEM;
  188. }
  189. static void __free_fw_priv(struct kref *ref)
  190. __releases(&fwc->lock)
  191. {
  192. struct fw_priv *fw_priv = to_fw_priv(ref);
  193. struct firmware_cache *fwc = fw_priv->fwc;
  194. pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
  195. __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
  196. (unsigned int)fw_priv->size);
  197. list_del(&fw_priv->list);
  198. spin_unlock(&fwc->lock);
  199. #ifdef CONFIG_FW_LOADER_USER_HELPER
  200. if (fw_priv->is_paged_buf) {
  201. int i;
  202. vunmap(fw_priv->data);
  203. for (i = 0; i < fw_priv->nr_pages; i++)
  204. __free_page(fw_priv->pages[i]);
  205. vfree(fw_priv->pages);
  206. } else
  207. #endif
  208. if (!fw_priv->allocated_size)
  209. vfree(fw_priv->data);
  210. kfree_const(fw_priv->fw_name);
  211. kfree(fw_priv);
  212. }
  213. static void free_fw_priv(struct fw_priv *fw_priv)
  214. {
  215. struct firmware_cache *fwc = fw_priv->fwc;
  216. spin_lock(&fwc->lock);
  217. if (!kref_put(&fw_priv->ref, __free_fw_priv))
  218. spin_unlock(&fwc->lock);
  219. }
  220. /* direct firmware loading support */
  221. static char fw_path_para[256];
  222. static const char * const fw_path[] = {
  223. fw_path_para,
  224. "/lib/firmware/updates/" UTS_RELEASE,
  225. "/lib/firmware/updates",
  226. "/lib/firmware/" UTS_RELEASE,
  227. "/lib/firmware"
  228. };
  229. /*
  230. * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
  231. * from kernel command line because firmware_class is generally built in
  232. * kernel instead of module.
  233. */
  234. module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
  235. MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
  236. static int
  237. fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv)
  238. {
  239. loff_t size;
  240. int i, len;
  241. int rc = -ENOENT;
  242. char *path;
  243. enum kernel_read_file_id id = READING_FIRMWARE;
  244. size_t msize = INT_MAX;
  245. /* Already populated data member means we're loading into a buffer */
  246. if (fw_priv->data) {
  247. id = READING_FIRMWARE_PREALLOC_BUFFER;
  248. msize = fw_priv->allocated_size;
  249. }
  250. path = __getname();
  251. if (!path)
  252. return -ENOMEM;
  253. for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
  254. /* skip the unset customized path */
  255. if (!fw_path[i][0])
  256. continue;
  257. len = snprintf(path, PATH_MAX, "%s/%s",
  258. fw_path[i], fw_priv->fw_name);
  259. if (len >= PATH_MAX) {
  260. rc = -ENAMETOOLONG;
  261. break;
  262. }
  263. fw_priv->size = 0;
  264. rc = kernel_read_file_from_path(path, &fw_priv->data, &size,
  265. msize, id);
  266. if (rc) {
  267. if (rc == -ENOENT)
  268. dev_dbg(device, "loading %s failed with error %d\n",
  269. path, rc);
  270. else
  271. dev_warn(device, "loading %s failed with error %d\n",
  272. path, rc);
  273. continue;
  274. }
  275. dev_dbg(device, "direct-loading %s\n", fw_priv->fw_name);
  276. fw_priv->size = size;
  277. fw_state_done(fw_priv);
  278. break;
  279. }
  280. __putname(path);
  281. return rc;
  282. }
  283. /* firmware holds the ownership of pages */
  284. static void firmware_free_data(const struct firmware *fw)
  285. {
  286. /* Loaded directly? */
  287. if (!fw->priv) {
  288. vfree(fw->data);
  289. return;
  290. }
  291. free_fw_priv(fw->priv);
  292. }
  293. /* store the pages buffer info firmware from buf */
  294. static void fw_set_page_data(struct fw_priv *fw_priv, struct firmware *fw)
  295. {
  296. fw->priv = fw_priv;
  297. #ifdef CONFIG_FW_LOADER_USER_HELPER
  298. fw->pages = fw_priv->pages;
  299. #endif
  300. fw->size = fw_priv->size;
  301. fw->data = fw_priv->data;
  302. pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
  303. __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
  304. (unsigned int)fw_priv->size);
  305. }
  306. #ifdef CONFIG_PM_SLEEP
  307. static void fw_name_devm_release(struct device *dev, void *res)
  308. {
  309. struct fw_name_devm *fwn = res;
  310. if (fwn->magic == (unsigned long)&fw_cache)
  311. pr_debug("%s: fw_name-%s devm-%p released\n",
  312. __func__, fwn->name, res);
  313. kfree_const(fwn->name);
  314. }
  315. static int fw_devm_match(struct device *dev, void *res,
  316. void *match_data)
  317. {
  318. struct fw_name_devm *fwn = res;
  319. return (fwn->magic == (unsigned long)&fw_cache) &&
  320. !strcmp(fwn->name, match_data);
  321. }
  322. static struct fw_name_devm *fw_find_devm_name(struct device *dev,
  323. const char *name)
  324. {
  325. struct fw_name_devm *fwn;
  326. fwn = devres_find(dev, fw_name_devm_release,
  327. fw_devm_match, (void *)name);
  328. return fwn;
  329. }
  330. static bool fw_cache_is_setup(struct device *dev, const char *name)
  331. {
  332. struct fw_name_devm *fwn;
  333. fwn = fw_find_devm_name(dev, name);
  334. if (fwn)
  335. return true;
  336. return false;
  337. }
  338. /* add firmware name into devres list */
  339. static int fw_add_devm_name(struct device *dev, const char *name)
  340. {
  341. struct fw_name_devm *fwn;
  342. if (fw_cache_is_setup(dev, name))
  343. return 0;
  344. fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
  345. GFP_KERNEL);
  346. if (!fwn)
  347. return -ENOMEM;
  348. fwn->name = kstrdup_const(name, GFP_KERNEL);
  349. if (!fwn->name) {
  350. devres_free(fwn);
  351. return -ENOMEM;
  352. }
  353. fwn->magic = (unsigned long)&fw_cache;
  354. devres_add(dev, fwn);
  355. return 0;
  356. }
  357. #else
  358. static bool fw_cache_is_setup(struct device *dev, const char *name)
  359. {
  360. return false;
  361. }
  362. static int fw_add_devm_name(struct device *dev, const char *name)
  363. {
  364. return 0;
  365. }
  366. #endif
  367. int assign_fw(struct firmware *fw, struct device *device,
  368. enum fw_opt opt_flags)
  369. {
  370. struct fw_priv *fw_priv = fw->priv;
  371. int ret;
  372. mutex_lock(&fw_lock);
  373. if (!fw_priv->size || fw_state_is_aborted(fw_priv)) {
  374. mutex_unlock(&fw_lock);
  375. return -ENOENT;
  376. }
  377. /*
  378. * add firmware name into devres list so that we can auto cache
  379. * and uncache firmware for device.
  380. *
  381. * device may has been deleted already, but the problem
  382. * should be fixed in devres or driver core.
  383. */
  384. /* don't cache firmware handled without uevent */
  385. if (device && (opt_flags & FW_OPT_UEVENT) &&
  386. !(opt_flags & FW_OPT_NOCACHE)) {
  387. ret = fw_add_devm_name(device, fw_priv->fw_name);
  388. if (ret) {
  389. mutex_unlock(&fw_lock);
  390. return ret;
  391. }
  392. }
  393. /*
  394. * After caching firmware image is started, let it piggyback
  395. * on request firmware.
  396. */
  397. if (!(opt_flags & FW_OPT_NOCACHE) &&
  398. fw_priv->fwc->state == FW_LOADER_START_CACHE) {
  399. if (fw_cache_piggyback_on_request(fw_priv->fw_name))
  400. kref_get(&fw_priv->ref);
  401. }
  402. /* pass the pages buffer to driver at the last minute */
  403. fw_set_page_data(fw_priv, fw);
  404. mutex_unlock(&fw_lock);
  405. return 0;
  406. }
  407. /* prepare firmware and firmware_buf structs;
  408. * return 0 if a firmware is already assigned, 1 if need to load one,
  409. * or a negative error code
  410. */
  411. static int
  412. _request_firmware_prepare(struct firmware **firmware_p, const char *name,
  413. struct device *device, void *dbuf, size_t size)
  414. {
  415. struct firmware *firmware;
  416. struct fw_priv *fw_priv;
  417. int ret;
  418. *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
  419. if (!firmware) {
  420. dev_err(device, "%s: kmalloc(struct firmware) failed\n",
  421. __func__);
  422. return -ENOMEM;
  423. }
  424. if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
  425. dev_dbg(device, "using built-in %s\n", name);
  426. return 0; /* assigned */
  427. }
  428. ret = alloc_lookup_fw_priv(name, &fw_cache, &fw_priv, dbuf, size);
  429. /*
  430. * bind with 'priv' now to avoid warning in failure path
  431. * of requesting firmware.
  432. */
  433. firmware->priv = fw_priv;
  434. if (ret > 0) {
  435. ret = fw_state_wait(fw_priv);
  436. if (!ret) {
  437. fw_set_page_data(fw_priv, firmware);
  438. return 0; /* assigned */
  439. }
  440. }
  441. if (ret < 0)
  442. return ret;
  443. return 1; /* need to load */
  444. }
  445. /*
  446. * Batched requests need only one wake, we need to do this step last due to the
  447. * fallback mechanism. The buf is protected with kref_get(), and it won't be
  448. * released until the last user calls release_firmware().
  449. *
  450. * Failed batched requests are possible as well, in such cases we just share
  451. * the struct fw_priv and won't release it until all requests are woken
  452. * and have gone through this same path.
  453. */
  454. static void fw_abort_batch_reqs(struct firmware *fw)
  455. {
  456. struct fw_priv *fw_priv;
  457. /* Loaded directly? */
  458. if (!fw || !fw->priv)
  459. return;
  460. fw_priv = fw->priv;
  461. if (!fw_state_is_aborted(fw_priv))
  462. fw_state_aborted(fw_priv);
  463. }
  464. /* called from request_firmware() and request_firmware_work_func() */
  465. static int
  466. _request_firmware(const struct firmware **firmware_p, const char *name,
  467. struct device *device, void *buf, size_t size,
  468. enum fw_opt opt_flags)
  469. {
  470. struct firmware *fw = NULL;
  471. int ret;
  472. if (!firmware_p)
  473. return -EINVAL;
  474. if (!name || name[0] == '\0') {
  475. ret = -EINVAL;
  476. goto out;
  477. }
  478. ret = _request_firmware_prepare(&fw, name, device, buf, size);
  479. if (ret <= 0) /* error or already assigned */
  480. goto out;
  481. ret = fw_get_filesystem_firmware(device, fw->priv);
  482. if (ret) {
  483. if (!(opt_flags & FW_OPT_NO_WARN))
  484. dev_warn(device,
  485. "Direct firmware load for %s failed with error %d\n",
  486. name, ret);
  487. ret = firmware_fallback_sysfs(fw, name, device, opt_flags, ret);
  488. } else
  489. ret = assign_fw(fw, device, opt_flags);
  490. out:
  491. if (ret < 0) {
  492. fw_abort_batch_reqs(fw);
  493. release_firmware(fw);
  494. fw = NULL;
  495. }
  496. *firmware_p = fw;
  497. return ret;
  498. }
  499. /**
  500. * request_firmware() - send firmware request and wait for it
  501. * @firmware_p: pointer to firmware image
  502. * @name: name of firmware file
  503. * @device: device for which firmware is being loaded
  504. *
  505. * @firmware_p will be used to return a firmware image by the name
  506. * of @name for device @device.
  507. *
  508. * Should be called from user context where sleeping is allowed.
  509. *
  510. * @name will be used as $FIRMWARE in the uevent environment and
  511. * should be distinctive enough not to be confused with any other
  512. * firmware image for this or any other device.
  513. *
  514. * Caller must hold the reference count of @device.
  515. *
  516. * The function can be called safely inside device's suspend and
  517. * resume callback.
  518. **/
  519. int
  520. request_firmware(const struct firmware **firmware_p, const char *name,
  521. struct device *device)
  522. {
  523. int ret;
  524. /* Need to pin this module until return */
  525. __module_get(THIS_MODULE);
  526. ret = _request_firmware(firmware_p, name, device, NULL, 0,
  527. FW_OPT_UEVENT);
  528. module_put(THIS_MODULE);
  529. return ret;
  530. }
  531. EXPORT_SYMBOL(request_firmware);
  532. /**
  533. * request_firmware_direct() - load firmware directly without usermode helper
  534. * @firmware_p: pointer to firmware image
  535. * @name: name of firmware file
  536. * @device: device for which firmware is being loaded
  537. *
  538. * This function works pretty much like request_firmware(), but this doesn't
  539. * fall back to usermode helper even if the firmware couldn't be loaded
  540. * directly from fs. Hence it's useful for loading optional firmwares, which
  541. * aren't always present, without extra long timeouts of udev.
  542. **/
  543. int request_firmware_direct(const struct firmware **firmware_p,
  544. const char *name, struct device *device)
  545. {
  546. int ret;
  547. __module_get(THIS_MODULE);
  548. ret = _request_firmware(firmware_p, name, device, NULL, 0,
  549. FW_OPT_UEVENT | FW_OPT_NO_WARN |
  550. FW_OPT_NOFALLBACK);
  551. module_put(THIS_MODULE);
  552. return ret;
  553. }
  554. EXPORT_SYMBOL_GPL(request_firmware_direct);
  555. /**
  556. * firmware_request_cache() - cache firmware for suspend so resume can use it
  557. * @name: name of firmware file
  558. * @device: device for which firmware should be cached for
  559. *
  560. * There are some devices with an optimization that enables the device to not
  561. * require loading firmware on system reboot. This optimization may still
  562. * require the firmware present on resume from suspend. This routine can be
  563. * used to ensure the firmware is present on resume from suspend in these
  564. * situations. This helper is not compatible with drivers which use
  565. * request_firmware_into_buf() or request_firmware_nowait() with no uevent set.
  566. **/
  567. int firmware_request_cache(struct device *device, const char *name)
  568. {
  569. int ret;
  570. mutex_lock(&fw_lock);
  571. ret = fw_add_devm_name(device, name);
  572. mutex_unlock(&fw_lock);
  573. return ret;
  574. }
  575. EXPORT_SYMBOL_GPL(firmware_request_cache);
  576. /**
  577. * request_firmware_into_buf() - load firmware into a previously allocated buffer
  578. * @firmware_p: pointer to firmware image
  579. * @name: name of firmware file
  580. * @device: device for which firmware is being loaded and DMA region allocated
  581. * @buf: address of buffer to load firmware into
  582. * @size: size of buffer
  583. *
  584. * This function works pretty much like request_firmware(), but it doesn't
  585. * allocate a buffer to hold the firmware data. Instead, the firmware
  586. * is loaded directly into the buffer pointed to by @buf and the @firmware_p
  587. * data member is pointed at @buf.
  588. *
  589. * This function doesn't cache firmware either.
  590. */
  591. int
  592. request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
  593. struct device *device, void *buf, size_t size)
  594. {
  595. int ret;
  596. if (fw_cache_is_setup(device, name))
  597. return -EOPNOTSUPP;
  598. __module_get(THIS_MODULE);
  599. ret = _request_firmware(firmware_p, name, device, buf, size,
  600. FW_OPT_UEVENT | FW_OPT_NOCACHE);
  601. module_put(THIS_MODULE);
  602. return ret;
  603. }
  604. EXPORT_SYMBOL(request_firmware_into_buf);
  605. /**
  606. * release_firmware() - release the resource associated with a firmware image
  607. * @fw: firmware resource to release
  608. **/
  609. void release_firmware(const struct firmware *fw)
  610. {
  611. if (fw) {
  612. if (!fw_is_builtin_firmware(fw))
  613. firmware_free_data(fw);
  614. kfree(fw);
  615. }
  616. }
  617. EXPORT_SYMBOL(release_firmware);
  618. /* Async support */
  619. struct firmware_work {
  620. struct work_struct work;
  621. struct module *module;
  622. const char *name;
  623. struct device *device;
  624. void *context;
  625. void (*cont)(const struct firmware *fw, void *context);
  626. enum fw_opt opt_flags;
  627. };
  628. static void request_firmware_work_func(struct work_struct *work)
  629. {
  630. struct firmware_work *fw_work;
  631. const struct firmware *fw;
  632. fw_work = container_of(work, struct firmware_work, work);
  633. _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
  634. fw_work->opt_flags);
  635. fw_work->cont(fw, fw_work->context);
  636. put_device(fw_work->device); /* taken in request_firmware_nowait() */
  637. module_put(fw_work->module);
  638. kfree_const(fw_work->name);
  639. kfree(fw_work);
  640. }
  641. /**
  642. * request_firmware_nowait() - asynchronous version of request_firmware
  643. * @module: module requesting the firmware
  644. * @uevent: sends uevent to copy the firmware image if this flag
  645. * is non-zero else the firmware copy must be done manually.
  646. * @name: name of firmware file
  647. * @device: device for which firmware is being loaded
  648. * @gfp: allocation flags
  649. * @context: will be passed over to @cont, and
  650. * @fw may be %NULL if firmware request fails.
  651. * @cont: function will be called asynchronously when the firmware
  652. * request is over.
  653. *
  654. * Caller must hold the reference count of @device.
  655. *
  656. * Asynchronous variant of request_firmware() for user contexts:
  657. * - sleep for as small periods as possible since it may
  658. * increase kernel boot time of built-in device drivers
  659. * requesting firmware in their ->probe() methods, if
  660. * @gfp is GFP_KERNEL.
  661. *
  662. * - can't sleep at all if @gfp is GFP_ATOMIC.
  663. **/
  664. int
  665. request_firmware_nowait(
  666. struct module *module, bool uevent,
  667. const char *name, struct device *device, gfp_t gfp, void *context,
  668. void (*cont)(const struct firmware *fw, void *context))
  669. {
  670. struct firmware_work *fw_work;
  671. fw_work = kzalloc(sizeof(struct firmware_work), gfp);
  672. if (!fw_work)
  673. return -ENOMEM;
  674. fw_work->module = module;
  675. fw_work->name = kstrdup_const(name, gfp);
  676. if (!fw_work->name) {
  677. kfree(fw_work);
  678. return -ENOMEM;
  679. }
  680. fw_work->device = device;
  681. fw_work->context = context;
  682. fw_work->cont = cont;
  683. fw_work->opt_flags = FW_OPT_NOWAIT |
  684. (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
  685. if (!uevent && fw_cache_is_setup(device, name)) {
  686. kfree_const(fw_work->name);
  687. kfree(fw_work);
  688. return -EOPNOTSUPP;
  689. }
  690. if (!try_module_get(module)) {
  691. kfree_const(fw_work->name);
  692. kfree(fw_work);
  693. return -EFAULT;
  694. }
  695. get_device(fw_work->device);
  696. INIT_WORK(&fw_work->work, request_firmware_work_func);
  697. schedule_work(&fw_work->work);
  698. return 0;
  699. }
  700. EXPORT_SYMBOL(request_firmware_nowait);
  701. #ifdef CONFIG_PM_SLEEP
  702. static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
  703. /**
  704. * cache_firmware() - cache one firmware image in kernel memory space
  705. * @fw_name: the firmware image name
  706. *
  707. * Cache firmware in kernel memory so that drivers can use it when
  708. * system isn't ready for them to request firmware image from userspace.
  709. * Once it returns successfully, driver can use request_firmware or its
  710. * nowait version to get the cached firmware without any interacting
  711. * with userspace
  712. *
  713. * Return 0 if the firmware image has been cached successfully
  714. * Return !0 otherwise
  715. *
  716. */
  717. static int cache_firmware(const char *fw_name)
  718. {
  719. int ret;
  720. const struct firmware *fw;
  721. pr_debug("%s: %s\n", __func__, fw_name);
  722. ret = request_firmware(&fw, fw_name, NULL);
  723. if (!ret)
  724. kfree(fw);
  725. pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
  726. return ret;
  727. }
  728. static struct fw_priv *lookup_fw_priv(const char *fw_name)
  729. {
  730. struct fw_priv *tmp;
  731. struct firmware_cache *fwc = &fw_cache;
  732. spin_lock(&fwc->lock);
  733. tmp = __lookup_fw_priv(fw_name);
  734. spin_unlock(&fwc->lock);
  735. return tmp;
  736. }
  737. /**
  738. * uncache_firmware() - remove one cached firmware image
  739. * @fw_name: the firmware image name
  740. *
  741. * Uncache one firmware image which has been cached successfully
  742. * before.
  743. *
  744. * Return 0 if the firmware cache has been removed successfully
  745. * Return !0 otherwise
  746. *
  747. */
  748. static int uncache_firmware(const char *fw_name)
  749. {
  750. struct fw_priv *fw_priv;
  751. struct firmware fw;
  752. pr_debug("%s: %s\n", __func__, fw_name);
  753. if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
  754. return 0;
  755. fw_priv = lookup_fw_priv(fw_name);
  756. if (fw_priv) {
  757. free_fw_priv(fw_priv);
  758. return 0;
  759. }
  760. return -EINVAL;
  761. }
  762. static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
  763. {
  764. struct fw_cache_entry *fce;
  765. fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
  766. if (!fce)
  767. goto exit;
  768. fce->name = kstrdup_const(name, GFP_ATOMIC);
  769. if (!fce->name) {
  770. kfree(fce);
  771. fce = NULL;
  772. goto exit;
  773. }
  774. exit:
  775. return fce;
  776. }
  777. static int __fw_entry_found(const char *name)
  778. {
  779. struct firmware_cache *fwc = &fw_cache;
  780. struct fw_cache_entry *fce;
  781. list_for_each_entry(fce, &fwc->fw_names, list) {
  782. if (!strcmp(fce->name, name))
  783. return 1;
  784. }
  785. return 0;
  786. }
  787. static int fw_cache_piggyback_on_request(const char *name)
  788. {
  789. struct firmware_cache *fwc = &fw_cache;
  790. struct fw_cache_entry *fce;
  791. int ret = 0;
  792. spin_lock(&fwc->name_lock);
  793. if (__fw_entry_found(name))
  794. goto found;
  795. fce = alloc_fw_cache_entry(name);
  796. if (fce) {
  797. ret = 1;
  798. list_add(&fce->list, &fwc->fw_names);
  799. pr_debug("%s: fw: %s\n", __func__, name);
  800. }
  801. found:
  802. spin_unlock(&fwc->name_lock);
  803. return ret;
  804. }
  805. static void free_fw_cache_entry(struct fw_cache_entry *fce)
  806. {
  807. kfree_const(fce->name);
  808. kfree(fce);
  809. }
  810. static void __async_dev_cache_fw_image(void *fw_entry,
  811. async_cookie_t cookie)
  812. {
  813. struct fw_cache_entry *fce = fw_entry;
  814. struct firmware_cache *fwc = &fw_cache;
  815. int ret;
  816. ret = cache_firmware(fce->name);
  817. if (ret) {
  818. spin_lock(&fwc->name_lock);
  819. list_del(&fce->list);
  820. spin_unlock(&fwc->name_lock);
  821. free_fw_cache_entry(fce);
  822. }
  823. }
  824. /* called with dev->devres_lock held */
  825. static void dev_create_fw_entry(struct device *dev, void *res,
  826. void *data)
  827. {
  828. struct fw_name_devm *fwn = res;
  829. const char *fw_name = fwn->name;
  830. struct list_head *head = data;
  831. struct fw_cache_entry *fce;
  832. fce = alloc_fw_cache_entry(fw_name);
  833. if (fce)
  834. list_add(&fce->list, head);
  835. }
  836. static int devm_name_match(struct device *dev, void *res,
  837. void *match_data)
  838. {
  839. struct fw_name_devm *fwn = res;
  840. return (fwn->magic == (unsigned long)match_data);
  841. }
  842. static void dev_cache_fw_image(struct device *dev, void *data)
  843. {
  844. LIST_HEAD(todo);
  845. struct fw_cache_entry *fce;
  846. struct fw_cache_entry *fce_next;
  847. struct firmware_cache *fwc = &fw_cache;
  848. devres_for_each_res(dev, fw_name_devm_release,
  849. devm_name_match, &fw_cache,
  850. dev_create_fw_entry, &todo);
  851. list_for_each_entry_safe(fce, fce_next, &todo, list) {
  852. list_del(&fce->list);
  853. spin_lock(&fwc->name_lock);
  854. /* only one cache entry for one firmware */
  855. if (!__fw_entry_found(fce->name)) {
  856. list_add(&fce->list, &fwc->fw_names);
  857. } else {
  858. free_fw_cache_entry(fce);
  859. fce = NULL;
  860. }
  861. spin_unlock(&fwc->name_lock);
  862. if (fce)
  863. async_schedule_domain(__async_dev_cache_fw_image,
  864. (void *)fce,
  865. &fw_cache_domain);
  866. }
  867. }
  868. static void __device_uncache_fw_images(void)
  869. {
  870. struct firmware_cache *fwc = &fw_cache;
  871. struct fw_cache_entry *fce;
  872. spin_lock(&fwc->name_lock);
  873. while (!list_empty(&fwc->fw_names)) {
  874. fce = list_entry(fwc->fw_names.next,
  875. struct fw_cache_entry, list);
  876. list_del(&fce->list);
  877. spin_unlock(&fwc->name_lock);
  878. uncache_firmware(fce->name);
  879. free_fw_cache_entry(fce);
  880. spin_lock(&fwc->name_lock);
  881. }
  882. spin_unlock(&fwc->name_lock);
  883. }
  884. /**
  885. * device_cache_fw_images() - cache devices' firmware
  886. *
  887. * If one device called request_firmware or its nowait version
  888. * successfully before, the firmware names are recored into the
  889. * device's devres link list, so device_cache_fw_images can call
  890. * cache_firmware() to cache these firmwares for the device,
  891. * then the device driver can load its firmwares easily at
  892. * time when system is not ready to complete loading firmware.
  893. */
  894. static void device_cache_fw_images(void)
  895. {
  896. struct firmware_cache *fwc = &fw_cache;
  897. DEFINE_WAIT(wait);
  898. pr_debug("%s\n", __func__);
  899. /* cancel uncache work */
  900. cancel_delayed_work_sync(&fwc->work);
  901. fw_fallback_set_cache_timeout();
  902. mutex_lock(&fw_lock);
  903. fwc->state = FW_LOADER_START_CACHE;
  904. dpm_for_each_dev(NULL, dev_cache_fw_image);
  905. mutex_unlock(&fw_lock);
  906. /* wait for completion of caching firmware for all devices */
  907. async_synchronize_full_domain(&fw_cache_domain);
  908. fw_fallback_set_default_timeout();
  909. }
  910. /**
  911. * device_uncache_fw_images() - uncache devices' firmware
  912. *
  913. * uncache all firmwares which have been cached successfully
  914. * by device_uncache_fw_images earlier
  915. */
  916. static void device_uncache_fw_images(void)
  917. {
  918. pr_debug("%s\n", __func__);
  919. __device_uncache_fw_images();
  920. }
  921. static void device_uncache_fw_images_work(struct work_struct *work)
  922. {
  923. device_uncache_fw_images();
  924. }
  925. /**
  926. * device_uncache_fw_images_delay() - uncache devices firmwares
  927. * @delay: number of milliseconds to delay uncache device firmwares
  928. *
  929. * uncache all devices's firmwares which has been cached successfully
  930. * by device_cache_fw_images after @delay milliseconds.
  931. */
  932. static void device_uncache_fw_images_delay(unsigned long delay)
  933. {
  934. queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
  935. msecs_to_jiffies(delay));
  936. }
  937. static int fw_pm_notify(struct notifier_block *notify_block,
  938. unsigned long mode, void *unused)
  939. {
  940. switch (mode) {
  941. case PM_HIBERNATION_PREPARE:
  942. case PM_SUSPEND_PREPARE:
  943. case PM_RESTORE_PREPARE:
  944. /*
  945. * kill pending fallback requests with a custom fallback
  946. * to avoid stalling suspend.
  947. */
  948. kill_pending_fw_fallback_reqs(true);
  949. device_cache_fw_images();
  950. break;
  951. case PM_POST_SUSPEND:
  952. case PM_POST_HIBERNATION:
  953. case PM_POST_RESTORE:
  954. /*
  955. * In case that system sleep failed and syscore_suspend is
  956. * not called.
  957. */
  958. mutex_lock(&fw_lock);
  959. fw_cache.state = FW_LOADER_NO_CACHE;
  960. mutex_unlock(&fw_lock);
  961. device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
  962. break;
  963. }
  964. return 0;
  965. }
  966. /* stop caching firmware once syscore_suspend is reached */
  967. static int fw_suspend(void)
  968. {
  969. fw_cache.state = FW_LOADER_NO_CACHE;
  970. return 0;
  971. }
  972. static struct syscore_ops fw_syscore_ops = {
  973. .suspend = fw_suspend,
  974. };
  975. static int __init register_fw_pm_ops(void)
  976. {
  977. int ret;
  978. spin_lock_init(&fw_cache.name_lock);
  979. INIT_LIST_HEAD(&fw_cache.fw_names);
  980. INIT_DELAYED_WORK(&fw_cache.work,
  981. device_uncache_fw_images_work);
  982. fw_cache.pm_notify.notifier_call = fw_pm_notify;
  983. ret = register_pm_notifier(&fw_cache.pm_notify);
  984. if (ret)
  985. return ret;
  986. register_syscore_ops(&fw_syscore_ops);
  987. return ret;
  988. }
  989. static inline void unregister_fw_pm_ops(void)
  990. {
  991. unregister_syscore_ops(&fw_syscore_ops);
  992. unregister_pm_notifier(&fw_cache.pm_notify);
  993. }
  994. #else
  995. static int fw_cache_piggyback_on_request(const char *name)
  996. {
  997. return 0;
  998. }
  999. static inline int register_fw_pm_ops(void)
  1000. {
  1001. return 0;
  1002. }
  1003. static inline void unregister_fw_pm_ops(void)
  1004. {
  1005. }
  1006. #endif
  1007. static void __init fw_cache_init(void)
  1008. {
  1009. spin_lock_init(&fw_cache.lock);
  1010. INIT_LIST_HEAD(&fw_cache.head);
  1011. fw_cache.state = FW_LOADER_NO_CACHE;
  1012. }
  1013. static int fw_shutdown_notify(struct notifier_block *unused1,
  1014. unsigned long unused2, void *unused3)
  1015. {
  1016. /*
  1017. * Kill all pending fallback requests to avoid both stalling shutdown,
  1018. * and avoid a deadlock with the usermode_lock.
  1019. */
  1020. kill_pending_fw_fallback_reqs(false);
  1021. return NOTIFY_DONE;
  1022. }
  1023. static struct notifier_block fw_shutdown_nb = {
  1024. .notifier_call = fw_shutdown_notify,
  1025. };
  1026. static int __init firmware_class_init(void)
  1027. {
  1028. int ret;
  1029. /* No need to unfold these on exit */
  1030. fw_cache_init();
  1031. ret = register_fw_pm_ops();
  1032. if (ret)
  1033. return ret;
  1034. ret = register_reboot_notifier(&fw_shutdown_nb);
  1035. if (ret)
  1036. goto out;
  1037. return register_sysfs_loader();
  1038. out:
  1039. unregister_fw_pm_ops();
  1040. return ret;
  1041. }
  1042. static void __exit firmware_class_exit(void)
  1043. {
  1044. unregister_fw_pm_ops();
  1045. unregister_reboot_notifier(&fw_shutdown_nb);
  1046. unregister_sysfs_loader();
  1047. }
  1048. fs_initcall(firmware_class_init);
  1049. module_exit(firmware_class_exit);