firmware_class.c 42 KB

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