firmware_class.c 43 KB

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