firmware_class.c 39 KB

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