main.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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. * firmware_request_nowarn() - request for an optional fw module
  534. * @firmware: pointer to firmware image
  535. * @name: name of firmware file
  536. * @device: device for which firmware is being loaded
  537. *
  538. * This function is similar in behaviour to request_firmware(), except
  539. * it doesn't produce warning messages when the file is not found.
  540. * The sysfs fallback mechanism is enabled if direct filesystem lookup fails,
  541. * however, however failures to find the firmware file with it are still
  542. * suppressed. It is therefore up to the driver to check for the return value
  543. * of this call and to decide when to inform the users of errors.
  544. **/
  545. int firmware_request_nowarn(const struct firmware **firmware, const char *name,
  546. struct device *device)
  547. {
  548. int ret;
  549. /* Need to pin this module until return */
  550. __module_get(THIS_MODULE);
  551. ret = _request_firmware(firmware, name, device, NULL, 0,
  552. FW_OPT_UEVENT | FW_OPT_NO_WARN);
  553. module_put(THIS_MODULE);
  554. return ret;
  555. }
  556. EXPORT_SYMBOL_GPL(firmware_request_nowarn);
  557. /**
  558. * request_firmware_direct() - load firmware directly without usermode helper
  559. * @firmware_p: pointer to firmware image
  560. * @name: name of firmware file
  561. * @device: device for which firmware is being loaded
  562. *
  563. * This function works pretty much like request_firmware(), but this doesn't
  564. * fall back to usermode helper even if the firmware couldn't be loaded
  565. * directly from fs. Hence it's useful for loading optional firmwares, which
  566. * aren't always present, without extra long timeouts of udev.
  567. **/
  568. int request_firmware_direct(const struct firmware **firmware_p,
  569. const char *name, struct device *device)
  570. {
  571. int ret;
  572. __module_get(THIS_MODULE);
  573. ret = _request_firmware(firmware_p, name, device, NULL, 0,
  574. FW_OPT_UEVENT | FW_OPT_NO_WARN |
  575. FW_OPT_NOFALLBACK);
  576. module_put(THIS_MODULE);
  577. return ret;
  578. }
  579. EXPORT_SYMBOL_GPL(request_firmware_direct);
  580. /**
  581. * firmware_request_cache() - cache firmware for suspend so resume can use it
  582. * @name: name of firmware file
  583. * @device: device for which firmware should be cached for
  584. *
  585. * There are some devices with an optimization that enables the device to not
  586. * require loading firmware on system reboot. This optimization may still
  587. * require the firmware present on resume from suspend. This routine can be
  588. * used to ensure the firmware is present on resume from suspend in these
  589. * situations. This helper is not compatible with drivers which use
  590. * request_firmware_into_buf() or request_firmware_nowait() with no uevent set.
  591. **/
  592. int firmware_request_cache(struct device *device, const char *name)
  593. {
  594. int ret;
  595. mutex_lock(&fw_lock);
  596. ret = fw_add_devm_name(device, name);
  597. mutex_unlock(&fw_lock);
  598. return ret;
  599. }
  600. EXPORT_SYMBOL_GPL(firmware_request_cache);
  601. /**
  602. * request_firmware_into_buf() - load firmware into a previously allocated buffer
  603. * @firmware_p: pointer to firmware image
  604. * @name: name of firmware file
  605. * @device: device for which firmware is being loaded and DMA region allocated
  606. * @buf: address of buffer to load firmware into
  607. * @size: size of buffer
  608. *
  609. * This function works pretty much like request_firmware(), but it doesn't
  610. * allocate a buffer to hold the firmware data. Instead, the firmware
  611. * is loaded directly into the buffer pointed to by @buf and the @firmware_p
  612. * data member is pointed at @buf.
  613. *
  614. * This function doesn't cache firmware either.
  615. */
  616. int
  617. request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
  618. struct device *device, void *buf, size_t size)
  619. {
  620. int ret;
  621. if (fw_cache_is_setup(device, name))
  622. return -EOPNOTSUPP;
  623. __module_get(THIS_MODULE);
  624. ret = _request_firmware(firmware_p, name, device, buf, size,
  625. FW_OPT_UEVENT | FW_OPT_NOCACHE);
  626. module_put(THIS_MODULE);
  627. return ret;
  628. }
  629. EXPORT_SYMBOL(request_firmware_into_buf);
  630. /**
  631. * release_firmware() - release the resource associated with a firmware image
  632. * @fw: firmware resource to release
  633. **/
  634. void release_firmware(const struct firmware *fw)
  635. {
  636. if (fw) {
  637. if (!fw_is_builtin_firmware(fw))
  638. firmware_free_data(fw);
  639. kfree(fw);
  640. }
  641. }
  642. EXPORT_SYMBOL(release_firmware);
  643. /* Async support */
  644. struct firmware_work {
  645. struct work_struct work;
  646. struct module *module;
  647. const char *name;
  648. struct device *device;
  649. void *context;
  650. void (*cont)(const struct firmware *fw, void *context);
  651. enum fw_opt opt_flags;
  652. };
  653. static void request_firmware_work_func(struct work_struct *work)
  654. {
  655. struct firmware_work *fw_work;
  656. const struct firmware *fw;
  657. fw_work = container_of(work, struct firmware_work, work);
  658. _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
  659. fw_work->opt_flags);
  660. fw_work->cont(fw, fw_work->context);
  661. put_device(fw_work->device); /* taken in request_firmware_nowait() */
  662. module_put(fw_work->module);
  663. kfree_const(fw_work->name);
  664. kfree(fw_work);
  665. }
  666. /**
  667. * request_firmware_nowait() - asynchronous version of request_firmware
  668. * @module: module requesting the firmware
  669. * @uevent: sends uevent to copy the firmware image if this flag
  670. * is non-zero else the firmware copy must be done manually.
  671. * @name: name of firmware file
  672. * @device: device for which firmware is being loaded
  673. * @gfp: allocation flags
  674. * @context: will be passed over to @cont, and
  675. * @fw may be %NULL if firmware request fails.
  676. * @cont: function will be called asynchronously when the firmware
  677. * request is over.
  678. *
  679. * Caller must hold the reference count of @device.
  680. *
  681. * Asynchronous variant of request_firmware() for user contexts:
  682. * - sleep for as small periods as possible since it may
  683. * increase kernel boot time of built-in device drivers
  684. * requesting firmware in their ->probe() methods, if
  685. * @gfp is GFP_KERNEL.
  686. *
  687. * - can't sleep at all if @gfp is GFP_ATOMIC.
  688. **/
  689. int
  690. request_firmware_nowait(
  691. struct module *module, bool uevent,
  692. const char *name, struct device *device, gfp_t gfp, void *context,
  693. void (*cont)(const struct firmware *fw, void *context))
  694. {
  695. struct firmware_work *fw_work;
  696. fw_work = kzalloc(sizeof(struct firmware_work), gfp);
  697. if (!fw_work)
  698. return -ENOMEM;
  699. fw_work->module = module;
  700. fw_work->name = kstrdup_const(name, gfp);
  701. if (!fw_work->name) {
  702. kfree(fw_work);
  703. return -ENOMEM;
  704. }
  705. fw_work->device = device;
  706. fw_work->context = context;
  707. fw_work->cont = cont;
  708. fw_work->opt_flags = FW_OPT_NOWAIT |
  709. (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
  710. if (!uevent && fw_cache_is_setup(device, name)) {
  711. kfree_const(fw_work->name);
  712. kfree(fw_work);
  713. return -EOPNOTSUPP;
  714. }
  715. if (!try_module_get(module)) {
  716. kfree_const(fw_work->name);
  717. kfree(fw_work);
  718. return -EFAULT;
  719. }
  720. get_device(fw_work->device);
  721. INIT_WORK(&fw_work->work, request_firmware_work_func);
  722. schedule_work(&fw_work->work);
  723. return 0;
  724. }
  725. EXPORT_SYMBOL(request_firmware_nowait);
  726. #ifdef CONFIG_PM_SLEEP
  727. static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
  728. /**
  729. * cache_firmware() - cache one firmware image in kernel memory space
  730. * @fw_name: the firmware image name
  731. *
  732. * Cache firmware in kernel memory so that drivers can use it when
  733. * system isn't ready for them to request firmware image from userspace.
  734. * Once it returns successfully, driver can use request_firmware or its
  735. * nowait version to get the cached firmware without any interacting
  736. * with userspace
  737. *
  738. * Return 0 if the firmware image has been cached successfully
  739. * Return !0 otherwise
  740. *
  741. */
  742. static int cache_firmware(const char *fw_name)
  743. {
  744. int ret;
  745. const struct firmware *fw;
  746. pr_debug("%s: %s\n", __func__, fw_name);
  747. ret = request_firmware(&fw, fw_name, NULL);
  748. if (!ret)
  749. kfree(fw);
  750. pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
  751. return ret;
  752. }
  753. static struct fw_priv *lookup_fw_priv(const char *fw_name)
  754. {
  755. struct fw_priv *tmp;
  756. struct firmware_cache *fwc = &fw_cache;
  757. spin_lock(&fwc->lock);
  758. tmp = __lookup_fw_priv(fw_name);
  759. spin_unlock(&fwc->lock);
  760. return tmp;
  761. }
  762. /**
  763. * uncache_firmware() - remove one cached firmware image
  764. * @fw_name: the firmware image name
  765. *
  766. * Uncache one firmware image which has been cached successfully
  767. * before.
  768. *
  769. * Return 0 if the firmware cache has been removed successfully
  770. * Return !0 otherwise
  771. *
  772. */
  773. static int uncache_firmware(const char *fw_name)
  774. {
  775. struct fw_priv *fw_priv;
  776. struct firmware fw;
  777. pr_debug("%s: %s\n", __func__, fw_name);
  778. if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
  779. return 0;
  780. fw_priv = lookup_fw_priv(fw_name);
  781. if (fw_priv) {
  782. free_fw_priv(fw_priv);
  783. return 0;
  784. }
  785. return -EINVAL;
  786. }
  787. static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
  788. {
  789. struct fw_cache_entry *fce;
  790. fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
  791. if (!fce)
  792. goto exit;
  793. fce->name = kstrdup_const(name, GFP_ATOMIC);
  794. if (!fce->name) {
  795. kfree(fce);
  796. fce = NULL;
  797. goto exit;
  798. }
  799. exit:
  800. return fce;
  801. }
  802. static int __fw_entry_found(const char *name)
  803. {
  804. struct firmware_cache *fwc = &fw_cache;
  805. struct fw_cache_entry *fce;
  806. list_for_each_entry(fce, &fwc->fw_names, list) {
  807. if (!strcmp(fce->name, name))
  808. return 1;
  809. }
  810. return 0;
  811. }
  812. static int fw_cache_piggyback_on_request(const char *name)
  813. {
  814. struct firmware_cache *fwc = &fw_cache;
  815. struct fw_cache_entry *fce;
  816. int ret = 0;
  817. spin_lock(&fwc->name_lock);
  818. if (__fw_entry_found(name))
  819. goto found;
  820. fce = alloc_fw_cache_entry(name);
  821. if (fce) {
  822. ret = 1;
  823. list_add(&fce->list, &fwc->fw_names);
  824. pr_debug("%s: fw: %s\n", __func__, name);
  825. }
  826. found:
  827. spin_unlock(&fwc->name_lock);
  828. return ret;
  829. }
  830. static void free_fw_cache_entry(struct fw_cache_entry *fce)
  831. {
  832. kfree_const(fce->name);
  833. kfree(fce);
  834. }
  835. static void __async_dev_cache_fw_image(void *fw_entry,
  836. async_cookie_t cookie)
  837. {
  838. struct fw_cache_entry *fce = fw_entry;
  839. struct firmware_cache *fwc = &fw_cache;
  840. int ret;
  841. ret = cache_firmware(fce->name);
  842. if (ret) {
  843. spin_lock(&fwc->name_lock);
  844. list_del(&fce->list);
  845. spin_unlock(&fwc->name_lock);
  846. free_fw_cache_entry(fce);
  847. }
  848. }
  849. /* called with dev->devres_lock held */
  850. static void dev_create_fw_entry(struct device *dev, void *res,
  851. void *data)
  852. {
  853. struct fw_name_devm *fwn = res;
  854. const char *fw_name = fwn->name;
  855. struct list_head *head = data;
  856. struct fw_cache_entry *fce;
  857. fce = alloc_fw_cache_entry(fw_name);
  858. if (fce)
  859. list_add(&fce->list, head);
  860. }
  861. static int devm_name_match(struct device *dev, void *res,
  862. void *match_data)
  863. {
  864. struct fw_name_devm *fwn = res;
  865. return (fwn->magic == (unsigned long)match_data);
  866. }
  867. static void dev_cache_fw_image(struct device *dev, void *data)
  868. {
  869. LIST_HEAD(todo);
  870. struct fw_cache_entry *fce;
  871. struct fw_cache_entry *fce_next;
  872. struct firmware_cache *fwc = &fw_cache;
  873. devres_for_each_res(dev, fw_name_devm_release,
  874. devm_name_match, &fw_cache,
  875. dev_create_fw_entry, &todo);
  876. list_for_each_entry_safe(fce, fce_next, &todo, list) {
  877. list_del(&fce->list);
  878. spin_lock(&fwc->name_lock);
  879. /* only one cache entry for one firmware */
  880. if (!__fw_entry_found(fce->name)) {
  881. list_add(&fce->list, &fwc->fw_names);
  882. } else {
  883. free_fw_cache_entry(fce);
  884. fce = NULL;
  885. }
  886. spin_unlock(&fwc->name_lock);
  887. if (fce)
  888. async_schedule_domain(__async_dev_cache_fw_image,
  889. (void *)fce,
  890. &fw_cache_domain);
  891. }
  892. }
  893. static void __device_uncache_fw_images(void)
  894. {
  895. struct firmware_cache *fwc = &fw_cache;
  896. struct fw_cache_entry *fce;
  897. spin_lock(&fwc->name_lock);
  898. while (!list_empty(&fwc->fw_names)) {
  899. fce = list_entry(fwc->fw_names.next,
  900. struct fw_cache_entry, list);
  901. list_del(&fce->list);
  902. spin_unlock(&fwc->name_lock);
  903. uncache_firmware(fce->name);
  904. free_fw_cache_entry(fce);
  905. spin_lock(&fwc->name_lock);
  906. }
  907. spin_unlock(&fwc->name_lock);
  908. }
  909. /**
  910. * device_cache_fw_images() - cache devices' firmware
  911. *
  912. * If one device called request_firmware or its nowait version
  913. * successfully before, the firmware names are recored into the
  914. * device's devres link list, so device_cache_fw_images can call
  915. * cache_firmware() to cache these firmwares for the device,
  916. * then the device driver can load its firmwares easily at
  917. * time when system is not ready to complete loading firmware.
  918. */
  919. static void device_cache_fw_images(void)
  920. {
  921. struct firmware_cache *fwc = &fw_cache;
  922. DEFINE_WAIT(wait);
  923. pr_debug("%s\n", __func__);
  924. /* cancel uncache work */
  925. cancel_delayed_work_sync(&fwc->work);
  926. fw_fallback_set_cache_timeout();
  927. mutex_lock(&fw_lock);
  928. fwc->state = FW_LOADER_START_CACHE;
  929. dpm_for_each_dev(NULL, dev_cache_fw_image);
  930. mutex_unlock(&fw_lock);
  931. /* wait for completion of caching firmware for all devices */
  932. async_synchronize_full_domain(&fw_cache_domain);
  933. fw_fallback_set_default_timeout();
  934. }
  935. /**
  936. * device_uncache_fw_images() - uncache devices' firmware
  937. *
  938. * uncache all firmwares which have been cached successfully
  939. * by device_uncache_fw_images earlier
  940. */
  941. static void device_uncache_fw_images(void)
  942. {
  943. pr_debug("%s\n", __func__);
  944. __device_uncache_fw_images();
  945. }
  946. static void device_uncache_fw_images_work(struct work_struct *work)
  947. {
  948. device_uncache_fw_images();
  949. }
  950. /**
  951. * device_uncache_fw_images_delay() - uncache devices firmwares
  952. * @delay: number of milliseconds to delay uncache device firmwares
  953. *
  954. * uncache all devices's firmwares which has been cached successfully
  955. * by device_cache_fw_images after @delay milliseconds.
  956. */
  957. static void device_uncache_fw_images_delay(unsigned long delay)
  958. {
  959. queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
  960. msecs_to_jiffies(delay));
  961. }
  962. static int fw_pm_notify(struct notifier_block *notify_block,
  963. unsigned long mode, void *unused)
  964. {
  965. switch (mode) {
  966. case PM_HIBERNATION_PREPARE:
  967. case PM_SUSPEND_PREPARE:
  968. case PM_RESTORE_PREPARE:
  969. /*
  970. * kill pending fallback requests with a custom fallback
  971. * to avoid stalling suspend.
  972. */
  973. kill_pending_fw_fallback_reqs(true);
  974. device_cache_fw_images();
  975. break;
  976. case PM_POST_SUSPEND:
  977. case PM_POST_HIBERNATION:
  978. case PM_POST_RESTORE:
  979. /*
  980. * In case that system sleep failed and syscore_suspend is
  981. * not called.
  982. */
  983. mutex_lock(&fw_lock);
  984. fw_cache.state = FW_LOADER_NO_CACHE;
  985. mutex_unlock(&fw_lock);
  986. device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
  987. break;
  988. }
  989. return 0;
  990. }
  991. /* stop caching firmware once syscore_suspend is reached */
  992. static int fw_suspend(void)
  993. {
  994. fw_cache.state = FW_LOADER_NO_CACHE;
  995. return 0;
  996. }
  997. static struct syscore_ops fw_syscore_ops = {
  998. .suspend = fw_suspend,
  999. };
  1000. static int __init register_fw_pm_ops(void)
  1001. {
  1002. int ret;
  1003. spin_lock_init(&fw_cache.name_lock);
  1004. INIT_LIST_HEAD(&fw_cache.fw_names);
  1005. INIT_DELAYED_WORK(&fw_cache.work,
  1006. device_uncache_fw_images_work);
  1007. fw_cache.pm_notify.notifier_call = fw_pm_notify;
  1008. ret = register_pm_notifier(&fw_cache.pm_notify);
  1009. if (ret)
  1010. return ret;
  1011. register_syscore_ops(&fw_syscore_ops);
  1012. return ret;
  1013. }
  1014. static inline void unregister_fw_pm_ops(void)
  1015. {
  1016. unregister_syscore_ops(&fw_syscore_ops);
  1017. unregister_pm_notifier(&fw_cache.pm_notify);
  1018. }
  1019. #else
  1020. static int fw_cache_piggyback_on_request(const char *name)
  1021. {
  1022. return 0;
  1023. }
  1024. static inline int register_fw_pm_ops(void)
  1025. {
  1026. return 0;
  1027. }
  1028. static inline void unregister_fw_pm_ops(void)
  1029. {
  1030. }
  1031. #endif
  1032. static void __init fw_cache_init(void)
  1033. {
  1034. spin_lock_init(&fw_cache.lock);
  1035. INIT_LIST_HEAD(&fw_cache.head);
  1036. fw_cache.state = FW_LOADER_NO_CACHE;
  1037. }
  1038. static int fw_shutdown_notify(struct notifier_block *unused1,
  1039. unsigned long unused2, void *unused3)
  1040. {
  1041. /*
  1042. * Kill all pending fallback requests to avoid both stalling shutdown,
  1043. * and avoid a deadlock with the usermode_lock.
  1044. */
  1045. kill_pending_fw_fallback_reqs(false);
  1046. return NOTIFY_DONE;
  1047. }
  1048. static struct notifier_block fw_shutdown_nb = {
  1049. .notifier_call = fw_shutdown_notify,
  1050. };
  1051. static int __init firmware_class_init(void)
  1052. {
  1053. int ret;
  1054. /* No need to unfold these on exit */
  1055. fw_cache_init();
  1056. ret = register_fw_pm_ops();
  1057. if (ret)
  1058. return ret;
  1059. ret = register_reboot_notifier(&fw_shutdown_nb);
  1060. if (ret)
  1061. goto out;
  1062. return register_sysfs_loader();
  1063. out:
  1064. unregister_fw_pm_ops();
  1065. return ret;
  1066. }
  1067. static void __exit firmware_class_exit(void)
  1068. {
  1069. unregister_fw_pm_ops();
  1070. unregister_reboot_notifier(&fw_shutdown_nb);
  1071. unregister_sysfs_loader();
  1072. }
  1073. fs_initcall(firmware_class_init);
  1074. module_exit(firmware_class_exit);