firmware_class.c 39 KB

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