firmware_class.c 40 KB

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