main.c 30 KB

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