firmware_class.c 45 KB

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