firmware_class.c 35 KB

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