firmware_class.c 36 KB

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