firmware_class.c 40 KB

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