firmware_class.c 33 KB

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