firmware_class.c 36 KB

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