firmware_class.c 40 KB

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