firmware_class.c 34 KB

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