firmware_class.c 39 KB

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