firmware_class.c 42 KB

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