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