firmware_class.c 35 KB

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