firmware_class.c 43 KB

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