firmware_class.c 34 KB

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