firmware_class.c 43 KB

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