firmware_class.c 42 KB

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