firmware_class.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  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. devres_free(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 do_firmware_uevent(struct firmware_priv *fw_priv, struct kobj_uevent_env *env)
  473. {
  474. if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
  475. return -ENOMEM;
  476. if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
  477. return -ENOMEM;
  478. if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
  479. return -ENOMEM;
  480. return 0;
  481. }
  482. static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
  483. {
  484. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  485. int err = 0;
  486. mutex_lock(&fw_lock);
  487. if (fw_priv->buf)
  488. err = do_firmware_uevent(fw_priv, env);
  489. mutex_unlock(&fw_lock);
  490. return err;
  491. }
  492. static struct class firmware_class = {
  493. .name = "firmware",
  494. .class_attrs = firmware_class_attrs,
  495. .dev_uevent = firmware_uevent,
  496. .dev_release = fw_dev_release,
  497. };
  498. static ssize_t firmware_loading_show(struct device *dev,
  499. struct device_attribute *attr, char *buf)
  500. {
  501. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  502. int loading = 0;
  503. mutex_lock(&fw_lock);
  504. if (fw_priv->buf)
  505. loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
  506. mutex_unlock(&fw_lock);
  507. return sprintf(buf, "%d\n", loading);
  508. }
  509. /* Some architectures don't have PAGE_KERNEL_RO */
  510. #ifndef PAGE_KERNEL_RO
  511. #define PAGE_KERNEL_RO PAGE_KERNEL
  512. #endif
  513. /* one pages buffer should be mapped/unmapped only once */
  514. static int fw_map_pages_buf(struct firmware_buf *buf)
  515. {
  516. if (!buf->is_paged_buf)
  517. return 0;
  518. vunmap(buf->data);
  519. buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
  520. if (!buf->data)
  521. return -ENOMEM;
  522. return 0;
  523. }
  524. /**
  525. * firmware_loading_store - set value in the 'loading' control file
  526. * @dev: device pointer
  527. * @attr: device attribute pointer
  528. * @buf: buffer to scan for loading control value
  529. * @count: number of bytes in @buf
  530. *
  531. * The relevant values are:
  532. *
  533. * 1: Start a load, discarding any previous partial load.
  534. * 0: Conclude the load and hand the data to the driver code.
  535. * -1: Conclude the load with an error and discard any written data.
  536. **/
  537. static ssize_t firmware_loading_store(struct device *dev,
  538. struct device_attribute *attr,
  539. const char *buf, size_t count)
  540. {
  541. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  542. struct firmware_buf *fw_buf;
  543. ssize_t written = count;
  544. int loading = simple_strtol(buf, NULL, 10);
  545. int i;
  546. mutex_lock(&fw_lock);
  547. fw_buf = fw_priv->buf;
  548. if (!fw_buf)
  549. goto out;
  550. switch (loading) {
  551. case 1:
  552. /* discarding any previous partial load */
  553. if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
  554. for (i = 0; i < fw_buf->nr_pages; i++)
  555. __free_page(fw_buf->pages[i]);
  556. kfree(fw_buf->pages);
  557. fw_buf->pages = NULL;
  558. fw_buf->page_array_size = 0;
  559. fw_buf->nr_pages = 0;
  560. set_bit(FW_STATUS_LOADING, &fw_buf->status);
  561. }
  562. break;
  563. case 0:
  564. if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
  565. int rc;
  566. set_bit(FW_STATUS_DONE, &fw_buf->status);
  567. clear_bit(FW_STATUS_LOADING, &fw_buf->status);
  568. /*
  569. * Several loading requests may be pending on
  570. * one same firmware buf, so let all requests
  571. * see the mapped 'buf->data' once the loading
  572. * is completed.
  573. * */
  574. rc = fw_map_pages_buf(fw_buf);
  575. if (rc)
  576. dev_err(dev, "%s: map pages failed\n",
  577. __func__);
  578. else
  579. rc = security_kernel_fw_from_file(NULL,
  580. fw_buf->data, fw_buf->size);
  581. /*
  582. * Same logic as fw_load_abort, only the DONE bit
  583. * is ignored and we set ABORT only on failure.
  584. */
  585. list_del_init(&fw_buf->pending_list);
  586. if (rc) {
  587. set_bit(FW_STATUS_ABORT, &fw_buf->status);
  588. written = rc;
  589. }
  590. complete_all(&fw_buf->completion);
  591. break;
  592. }
  593. /* fallthrough */
  594. default:
  595. dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
  596. /* fallthrough */
  597. case -1:
  598. fw_load_abort(fw_priv);
  599. break;
  600. }
  601. out:
  602. mutex_unlock(&fw_lock);
  603. return written;
  604. }
  605. static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
  606. static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
  607. struct bin_attribute *bin_attr,
  608. char *buffer, loff_t offset, size_t count)
  609. {
  610. struct device *dev = kobj_to_dev(kobj);
  611. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  612. struct firmware_buf *buf;
  613. ssize_t ret_count;
  614. mutex_lock(&fw_lock);
  615. buf = fw_priv->buf;
  616. if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
  617. ret_count = -ENODEV;
  618. goto out;
  619. }
  620. if (offset > buf->size) {
  621. ret_count = 0;
  622. goto out;
  623. }
  624. if (count > buf->size - offset)
  625. count = buf->size - offset;
  626. ret_count = count;
  627. while (count) {
  628. void *page_data;
  629. int page_nr = offset >> PAGE_SHIFT;
  630. int page_ofs = offset & (PAGE_SIZE-1);
  631. int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
  632. page_data = kmap(buf->pages[page_nr]);
  633. memcpy(buffer, page_data + page_ofs, page_cnt);
  634. kunmap(buf->pages[page_nr]);
  635. buffer += page_cnt;
  636. offset += page_cnt;
  637. count -= page_cnt;
  638. }
  639. out:
  640. mutex_unlock(&fw_lock);
  641. return ret_count;
  642. }
  643. static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
  644. {
  645. struct firmware_buf *buf = fw_priv->buf;
  646. int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
  647. /* If the array of pages is too small, grow it... */
  648. if (buf->page_array_size < pages_needed) {
  649. int new_array_size = max(pages_needed,
  650. buf->page_array_size * 2);
  651. struct page **new_pages;
  652. new_pages = kmalloc(new_array_size * sizeof(void *),
  653. GFP_KERNEL);
  654. if (!new_pages) {
  655. fw_load_abort(fw_priv);
  656. return -ENOMEM;
  657. }
  658. memcpy(new_pages, buf->pages,
  659. buf->page_array_size * sizeof(void *));
  660. memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
  661. (new_array_size - buf->page_array_size));
  662. kfree(buf->pages);
  663. buf->pages = new_pages;
  664. buf->page_array_size = new_array_size;
  665. }
  666. while (buf->nr_pages < pages_needed) {
  667. buf->pages[buf->nr_pages] =
  668. alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
  669. if (!buf->pages[buf->nr_pages]) {
  670. fw_load_abort(fw_priv);
  671. return -ENOMEM;
  672. }
  673. buf->nr_pages++;
  674. }
  675. return 0;
  676. }
  677. /**
  678. * firmware_data_write - write method for firmware
  679. * @filp: open sysfs file
  680. * @kobj: kobject for the device
  681. * @bin_attr: bin_attr structure
  682. * @buffer: buffer being written
  683. * @offset: buffer offset for write in total data store area
  684. * @count: buffer size
  685. *
  686. * Data written to the 'data' attribute will be later handed to
  687. * the driver as a firmware image.
  688. **/
  689. static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
  690. struct bin_attribute *bin_attr,
  691. char *buffer, loff_t offset, size_t count)
  692. {
  693. struct device *dev = kobj_to_dev(kobj);
  694. struct firmware_priv *fw_priv = to_firmware_priv(dev);
  695. struct firmware_buf *buf;
  696. ssize_t retval;
  697. if (!capable(CAP_SYS_RAWIO))
  698. return -EPERM;
  699. mutex_lock(&fw_lock);
  700. buf = fw_priv->buf;
  701. if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
  702. retval = -ENODEV;
  703. goto out;
  704. }
  705. retval = fw_realloc_buffer(fw_priv, offset + count);
  706. if (retval)
  707. goto out;
  708. retval = count;
  709. while (count) {
  710. void *page_data;
  711. int page_nr = offset >> PAGE_SHIFT;
  712. int page_ofs = offset & (PAGE_SIZE - 1);
  713. int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
  714. page_data = kmap(buf->pages[page_nr]);
  715. memcpy(page_data + page_ofs, buffer, page_cnt);
  716. kunmap(buf->pages[page_nr]);
  717. buffer += page_cnt;
  718. offset += page_cnt;
  719. count -= page_cnt;
  720. }
  721. buf->size = max_t(size_t, offset, buf->size);
  722. out:
  723. mutex_unlock(&fw_lock);
  724. return retval;
  725. }
  726. static struct bin_attribute firmware_attr_data = {
  727. .attr = { .name = "data", .mode = 0644 },
  728. .size = 0,
  729. .read = firmware_data_read,
  730. .write = firmware_data_write,
  731. };
  732. static struct attribute *fw_dev_attrs[] = {
  733. &dev_attr_loading.attr,
  734. NULL
  735. };
  736. static struct bin_attribute *fw_dev_bin_attrs[] = {
  737. &firmware_attr_data,
  738. NULL
  739. };
  740. static const struct attribute_group fw_dev_attr_group = {
  741. .attrs = fw_dev_attrs,
  742. .bin_attrs = fw_dev_bin_attrs,
  743. };
  744. static const struct attribute_group *fw_dev_attr_groups[] = {
  745. &fw_dev_attr_group,
  746. NULL
  747. };
  748. static struct firmware_priv *
  749. fw_create_instance(struct firmware *firmware, const char *fw_name,
  750. struct device *device, unsigned int opt_flags)
  751. {
  752. struct firmware_priv *fw_priv;
  753. struct device *f_dev;
  754. fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
  755. if (!fw_priv) {
  756. fw_priv = ERR_PTR(-ENOMEM);
  757. goto exit;
  758. }
  759. fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT);
  760. fw_priv->fw = firmware;
  761. f_dev = &fw_priv->dev;
  762. device_initialize(f_dev);
  763. dev_set_name(f_dev, "%s", fw_name);
  764. f_dev->parent = device;
  765. f_dev->class = &firmware_class;
  766. f_dev->groups = fw_dev_attr_groups;
  767. exit:
  768. return fw_priv;
  769. }
  770. /* load a firmware via user helper */
  771. static int _request_firmware_load(struct firmware_priv *fw_priv,
  772. unsigned int opt_flags, long timeout)
  773. {
  774. int retval = 0;
  775. struct device *f_dev = &fw_priv->dev;
  776. struct firmware_buf *buf = fw_priv->buf;
  777. /* fall back on userspace loading */
  778. buf->is_paged_buf = true;
  779. dev_set_uevent_suppress(f_dev, true);
  780. retval = device_add(f_dev);
  781. if (retval) {
  782. dev_err(f_dev, "%s: device_register failed\n", __func__);
  783. goto err_put_dev;
  784. }
  785. mutex_lock(&fw_lock);
  786. list_add(&buf->pending_list, &pending_fw_head);
  787. mutex_unlock(&fw_lock);
  788. if (opt_flags & FW_OPT_UEVENT) {
  789. buf->need_uevent = true;
  790. dev_set_uevent_suppress(f_dev, false);
  791. dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
  792. kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
  793. } else {
  794. timeout = MAX_JIFFY_OFFSET;
  795. }
  796. retval = wait_for_completion_interruptible_timeout(&buf->completion,
  797. timeout);
  798. if (retval == -ERESTARTSYS || !retval) {
  799. mutex_lock(&fw_lock);
  800. fw_load_abort(fw_priv);
  801. mutex_unlock(&fw_lock);
  802. } else if (retval > 0) {
  803. retval = 0;
  804. }
  805. if (is_fw_load_aborted(buf))
  806. retval = -EAGAIN;
  807. else if (!buf->data)
  808. retval = -ENOMEM;
  809. device_del(f_dev);
  810. err_put_dev:
  811. put_device(f_dev);
  812. return retval;
  813. }
  814. static int fw_load_from_user_helper(struct firmware *firmware,
  815. const char *name, struct device *device,
  816. unsigned int opt_flags, long timeout)
  817. {
  818. struct firmware_priv *fw_priv;
  819. fw_priv = fw_create_instance(firmware, name, device, opt_flags);
  820. if (IS_ERR(fw_priv))
  821. return PTR_ERR(fw_priv);
  822. fw_priv->buf = firmware->priv;
  823. return _request_firmware_load(fw_priv, opt_flags, timeout);
  824. }
  825. #ifdef CONFIG_PM_SLEEP
  826. /* kill pending requests without uevent to avoid blocking suspend */
  827. static void kill_requests_without_uevent(void)
  828. {
  829. struct firmware_buf *buf;
  830. struct firmware_buf *next;
  831. mutex_lock(&fw_lock);
  832. list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
  833. if (!buf->need_uevent)
  834. __fw_load_abort(buf);
  835. }
  836. mutex_unlock(&fw_lock);
  837. }
  838. #endif
  839. #else /* CONFIG_FW_LOADER_USER_HELPER */
  840. static inline int
  841. fw_load_from_user_helper(struct firmware *firmware, const char *name,
  842. struct device *device, unsigned int opt_flags,
  843. long timeout)
  844. {
  845. return -ENOENT;
  846. }
  847. /* No abort during direct loading */
  848. #define is_fw_load_aborted(buf) false
  849. #ifdef CONFIG_PM_SLEEP
  850. static inline void kill_requests_without_uevent(void) { }
  851. #endif
  852. #endif /* CONFIG_FW_LOADER_USER_HELPER */
  853. /* wait until the shared firmware_buf becomes ready (or error) */
  854. static int sync_cached_firmware_buf(struct firmware_buf *buf)
  855. {
  856. int ret = 0;
  857. mutex_lock(&fw_lock);
  858. while (!test_bit(FW_STATUS_DONE, &buf->status)) {
  859. if (is_fw_load_aborted(buf)) {
  860. ret = -ENOENT;
  861. break;
  862. }
  863. mutex_unlock(&fw_lock);
  864. ret = wait_for_completion_interruptible(&buf->completion);
  865. mutex_lock(&fw_lock);
  866. }
  867. mutex_unlock(&fw_lock);
  868. return ret;
  869. }
  870. /* prepare firmware and firmware_buf structs;
  871. * return 0 if a firmware is already assigned, 1 if need to load one,
  872. * or a negative error code
  873. */
  874. static int
  875. _request_firmware_prepare(struct firmware **firmware_p, const char *name,
  876. struct device *device)
  877. {
  878. struct firmware *firmware;
  879. struct firmware_buf *buf;
  880. int ret;
  881. *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
  882. if (!firmware) {
  883. dev_err(device, "%s: kmalloc(struct firmware) failed\n",
  884. __func__);
  885. return -ENOMEM;
  886. }
  887. if (fw_get_builtin_firmware(firmware, name)) {
  888. dev_dbg(device, "firmware: using built-in firmware %s\n", name);
  889. return 0; /* assigned */
  890. }
  891. ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
  892. /*
  893. * bind with 'buf' now to avoid warning in failure path
  894. * of requesting firmware.
  895. */
  896. firmware->priv = buf;
  897. if (ret > 0) {
  898. ret = sync_cached_firmware_buf(buf);
  899. if (!ret) {
  900. fw_set_page_data(buf, firmware);
  901. return 0; /* assigned */
  902. }
  903. }
  904. if (ret < 0)
  905. return ret;
  906. return 1; /* need to load */
  907. }
  908. static int assign_firmware_buf(struct firmware *fw, struct device *device,
  909. unsigned int opt_flags)
  910. {
  911. struct firmware_buf *buf = fw->priv;
  912. mutex_lock(&fw_lock);
  913. if (!buf->size || is_fw_load_aborted(buf)) {
  914. mutex_unlock(&fw_lock);
  915. return -ENOENT;
  916. }
  917. /*
  918. * add firmware name into devres list so that we can auto cache
  919. * and uncache firmware for device.
  920. *
  921. * device may has been deleted already, but the problem
  922. * should be fixed in devres or driver core.
  923. */
  924. /* don't cache firmware handled without uevent */
  925. if (device && (opt_flags & FW_OPT_UEVENT))
  926. fw_add_devm_name(device, buf->fw_id);
  927. /*
  928. * After caching firmware image is started, let it piggyback
  929. * on request firmware.
  930. */
  931. if (buf->fwc->state == FW_LOADER_START_CACHE) {
  932. if (fw_cache_piggyback_on_request(buf->fw_id))
  933. kref_get(&buf->ref);
  934. }
  935. /* pass the pages buffer to driver at the last minute */
  936. fw_set_page_data(buf, fw);
  937. mutex_unlock(&fw_lock);
  938. return 0;
  939. }
  940. /* called from request_firmware() and request_firmware_work_func() */
  941. static int
  942. _request_firmware(const struct firmware **firmware_p, const char *name,
  943. struct device *device, unsigned int opt_flags)
  944. {
  945. struct firmware *fw;
  946. long timeout;
  947. int ret;
  948. if (!firmware_p)
  949. return -EINVAL;
  950. if (!name || name[0] == '\0')
  951. return -EINVAL;
  952. ret = _request_firmware_prepare(&fw, name, device);
  953. if (ret <= 0) /* error or already assigned */
  954. goto out;
  955. ret = 0;
  956. timeout = firmware_loading_timeout();
  957. if (opt_flags & FW_OPT_NOWAIT) {
  958. timeout = usermodehelper_read_lock_wait(timeout);
  959. if (!timeout) {
  960. dev_dbg(device, "firmware: %s loading timed out\n",
  961. name);
  962. ret = -EBUSY;
  963. goto out;
  964. }
  965. } else {
  966. ret = usermodehelper_read_trylock();
  967. if (WARN_ON(ret)) {
  968. dev_err(device, "firmware: %s will not be loaded\n",
  969. name);
  970. goto out;
  971. }
  972. }
  973. ret = fw_get_filesystem_firmware(device, fw->priv);
  974. if (ret) {
  975. if (!(opt_flags & FW_OPT_NO_WARN))
  976. dev_warn(device,
  977. "Direct firmware load for %s failed with error %d\n",
  978. name, ret);
  979. if (opt_flags & FW_OPT_USERHELPER) {
  980. dev_warn(device, "Falling back to user helper\n");
  981. ret = fw_load_from_user_helper(fw, name, device,
  982. opt_flags, timeout);
  983. }
  984. }
  985. if (!ret)
  986. ret = assign_firmware_buf(fw, device, opt_flags);
  987. usermodehelper_read_unlock();
  988. out:
  989. if (ret < 0) {
  990. release_firmware(fw);
  991. fw = NULL;
  992. }
  993. *firmware_p = fw;
  994. return ret;
  995. }
  996. /**
  997. * request_firmware: - send firmware request and wait for it
  998. * @firmware_p: pointer to firmware image
  999. * @name: name of firmware file
  1000. * @device: device for which firmware is being loaded
  1001. *
  1002. * @firmware_p will be used to return a firmware image by the name
  1003. * of @name for device @device.
  1004. *
  1005. * Should be called from user context where sleeping is allowed.
  1006. *
  1007. * @name will be used as $FIRMWARE in the uevent environment and
  1008. * should be distinctive enough not to be confused with any other
  1009. * firmware image for this or any other device.
  1010. *
  1011. * Caller must hold the reference count of @device.
  1012. *
  1013. * The function can be called safely inside device's suspend and
  1014. * resume callback.
  1015. **/
  1016. int
  1017. request_firmware(const struct firmware **firmware_p, const char *name,
  1018. struct device *device)
  1019. {
  1020. int ret;
  1021. /* Need to pin this module until return */
  1022. __module_get(THIS_MODULE);
  1023. ret = _request_firmware(firmware_p, name, device,
  1024. FW_OPT_UEVENT | FW_OPT_FALLBACK);
  1025. module_put(THIS_MODULE);
  1026. return ret;
  1027. }
  1028. EXPORT_SYMBOL(request_firmware);
  1029. /**
  1030. * request_firmware_direct: - load firmware directly without usermode helper
  1031. * @firmware_p: pointer to firmware image
  1032. * @name: name of firmware file
  1033. * @device: device for which firmware is being loaded
  1034. *
  1035. * This function works pretty much like request_firmware(), but this doesn't
  1036. * fall back to usermode helper even if the firmware couldn't be loaded
  1037. * directly from fs. Hence it's useful for loading optional firmwares, which
  1038. * aren't always present, without extra long timeouts of udev.
  1039. **/
  1040. int request_firmware_direct(const struct firmware **firmware_p,
  1041. const char *name, struct device *device)
  1042. {
  1043. int ret;
  1044. __module_get(THIS_MODULE);
  1045. ret = _request_firmware(firmware_p, name, device,
  1046. FW_OPT_UEVENT | FW_OPT_NO_WARN);
  1047. module_put(THIS_MODULE);
  1048. return ret;
  1049. }
  1050. EXPORT_SYMBOL_GPL(request_firmware_direct);
  1051. /**
  1052. * release_firmware: - release the resource associated with a firmware image
  1053. * @fw: firmware resource to release
  1054. **/
  1055. void release_firmware(const struct firmware *fw)
  1056. {
  1057. if (fw) {
  1058. if (!fw_is_builtin_firmware(fw))
  1059. firmware_free_data(fw);
  1060. kfree(fw);
  1061. }
  1062. }
  1063. EXPORT_SYMBOL(release_firmware);
  1064. /* Async support */
  1065. struct firmware_work {
  1066. struct work_struct work;
  1067. struct module *module;
  1068. const char *name;
  1069. struct device *device;
  1070. void *context;
  1071. void (*cont)(const struct firmware *fw, void *context);
  1072. unsigned int opt_flags;
  1073. };
  1074. static void request_firmware_work_func(struct work_struct *work)
  1075. {
  1076. struct firmware_work *fw_work;
  1077. const struct firmware *fw;
  1078. fw_work = container_of(work, struct firmware_work, work);
  1079. _request_firmware(&fw, fw_work->name, fw_work->device,
  1080. fw_work->opt_flags);
  1081. fw_work->cont(fw, fw_work->context);
  1082. put_device(fw_work->device); /* taken in request_firmware_nowait() */
  1083. module_put(fw_work->module);
  1084. kfree_const(fw_work->name);
  1085. kfree(fw_work);
  1086. }
  1087. /**
  1088. * request_firmware_nowait - asynchronous version of request_firmware
  1089. * @module: module requesting the firmware
  1090. * @uevent: sends uevent to copy the firmware image if this flag
  1091. * is non-zero else the firmware copy must be done manually.
  1092. * @name: name of firmware file
  1093. * @device: device for which firmware is being loaded
  1094. * @gfp: allocation flags
  1095. * @context: will be passed over to @cont, and
  1096. * @fw may be %NULL if firmware request fails.
  1097. * @cont: function will be called asynchronously when the firmware
  1098. * request is over.
  1099. *
  1100. * Caller must hold the reference count of @device.
  1101. *
  1102. * Asynchronous variant of request_firmware() for user contexts:
  1103. * - sleep for as small periods as possible since it may
  1104. * increase kernel boot time of built-in device drivers
  1105. * requesting firmware in their ->probe() methods, if
  1106. * @gfp is GFP_KERNEL.
  1107. *
  1108. * - can't sleep at all if @gfp is GFP_ATOMIC.
  1109. **/
  1110. int
  1111. request_firmware_nowait(
  1112. struct module *module, bool uevent,
  1113. const char *name, struct device *device, gfp_t gfp, void *context,
  1114. void (*cont)(const struct firmware *fw, void *context))
  1115. {
  1116. struct firmware_work *fw_work;
  1117. fw_work = kzalloc(sizeof(struct firmware_work), gfp);
  1118. if (!fw_work)
  1119. return -ENOMEM;
  1120. fw_work->module = module;
  1121. fw_work->name = kstrdup_const(name, gfp);
  1122. if (!fw_work->name) {
  1123. kfree(fw_work);
  1124. return -ENOMEM;
  1125. }
  1126. fw_work->device = device;
  1127. fw_work->context = context;
  1128. fw_work->cont = cont;
  1129. fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
  1130. (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
  1131. if (!try_module_get(module)) {
  1132. kfree_const(fw_work->name);
  1133. kfree(fw_work);
  1134. return -EFAULT;
  1135. }
  1136. get_device(fw_work->device);
  1137. INIT_WORK(&fw_work->work, request_firmware_work_func);
  1138. schedule_work(&fw_work->work);
  1139. return 0;
  1140. }
  1141. EXPORT_SYMBOL(request_firmware_nowait);
  1142. #ifdef CONFIG_PM_SLEEP
  1143. static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
  1144. /**
  1145. * cache_firmware - cache one firmware image in kernel memory space
  1146. * @fw_name: the firmware image name
  1147. *
  1148. * Cache firmware in kernel memory so that drivers can use it when
  1149. * system isn't ready for them to request firmware image from userspace.
  1150. * Once it returns successfully, driver can use request_firmware or its
  1151. * nowait version to get the cached firmware without any interacting
  1152. * with userspace
  1153. *
  1154. * Return 0 if the firmware image has been cached successfully
  1155. * Return !0 otherwise
  1156. *
  1157. */
  1158. static int cache_firmware(const char *fw_name)
  1159. {
  1160. int ret;
  1161. const struct firmware *fw;
  1162. pr_debug("%s: %s\n", __func__, fw_name);
  1163. ret = request_firmware(&fw, fw_name, NULL);
  1164. if (!ret)
  1165. kfree(fw);
  1166. pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
  1167. return ret;
  1168. }
  1169. static struct firmware_buf *fw_lookup_buf(const char *fw_name)
  1170. {
  1171. struct firmware_buf *tmp;
  1172. struct firmware_cache *fwc = &fw_cache;
  1173. spin_lock(&fwc->lock);
  1174. tmp = __fw_lookup_buf(fw_name);
  1175. spin_unlock(&fwc->lock);
  1176. return tmp;
  1177. }
  1178. /**
  1179. * uncache_firmware - remove one cached firmware image
  1180. * @fw_name: the firmware image name
  1181. *
  1182. * Uncache one firmware image which has been cached successfully
  1183. * before.
  1184. *
  1185. * Return 0 if the firmware cache has been removed successfully
  1186. * Return !0 otherwise
  1187. *
  1188. */
  1189. static int uncache_firmware(const char *fw_name)
  1190. {
  1191. struct firmware_buf *buf;
  1192. struct firmware fw;
  1193. pr_debug("%s: %s\n", __func__, fw_name);
  1194. if (fw_get_builtin_firmware(&fw, fw_name))
  1195. return 0;
  1196. buf = fw_lookup_buf(fw_name);
  1197. if (buf) {
  1198. fw_free_buf(buf);
  1199. return 0;
  1200. }
  1201. return -EINVAL;
  1202. }
  1203. static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
  1204. {
  1205. struct fw_cache_entry *fce;
  1206. fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
  1207. if (!fce)
  1208. goto exit;
  1209. fce->name = kstrdup_const(name, GFP_ATOMIC);
  1210. if (!fce->name) {
  1211. kfree(fce);
  1212. fce = NULL;
  1213. goto exit;
  1214. }
  1215. exit:
  1216. return fce;
  1217. }
  1218. static int __fw_entry_found(const char *name)
  1219. {
  1220. struct firmware_cache *fwc = &fw_cache;
  1221. struct fw_cache_entry *fce;
  1222. list_for_each_entry(fce, &fwc->fw_names, list) {
  1223. if (!strcmp(fce->name, name))
  1224. return 1;
  1225. }
  1226. return 0;
  1227. }
  1228. static int fw_cache_piggyback_on_request(const char *name)
  1229. {
  1230. struct firmware_cache *fwc = &fw_cache;
  1231. struct fw_cache_entry *fce;
  1232. int ret = 0;
  1233. spin_lock(&fwc->name_lock);
  1234. if (__fw_entry_found(name))
  1235. goto found;
  1236. fce = alloc_fw_cache_entry(name);
  1237. if (fce) {
  1238. ret = 1;
  1239. list_add(&fce->list, &fwc->fw_names);
  1240. pr_debug("%s: fw: %s\n", __func__, name);
  1241. }
  1242. found:
  1243. spin_unlock(&fwc->name_lock);
  1244. return ret;
  1245. }
  1246. static void free_fw_cache_entry(struct fw_cache_entry *fce)
  1247. {
  1248. kfree_const(fce->name);
  1249. kfree(fce);
  1250. }
  1251. static void __async_dev_cache_fw_image(void *fw_entry,
  1252. async_cookie_t cookie)
  1253. {
  1254. struct fw_cache_entry *fce = fw_entry;
  1255. struct firmware_cache *fwc = &fw_cache;
  1256. int ret;
  1257. ret = cache_firmware(fce->name);
  1258. if (ret) {
  1259. spin_lock(&fwc->name_lock);
  1260. list_del(&fce->list);
  1261. spin_unlock(&fwc->name_lock);
  1262. free_fw_cache_entry(fce);
  1263. }
  1264. }
  1265. /* called with dev->devres_lock held */
  1266. static void dev_create_fw_entry(struct device *dev, void *res,
  1267. void *data)
  1268. {
  1269. struct fw_name_devm *fwn = res;
  1270. const char *fw_name = fwn->name;
  1271. struct list_head *head = data;
  1272. struct fw_cache_entry *fce;
  1273. fce = alloc_fw_cache_entry(fw_name);
  1274. if (fce)
  1275. list_add(&fce->list, head);
  1276. }
  1277. static int devm_name_match(struct device *dev, void *res,
  1278. void *match_data)
  1279. {
  1280. struct fw_name_devm *fwn = res;
  1281. return (fwn->magic == (unsigned long)match_data);
  1282. }
  1283. static void dev_cache_fw_image(struct device *dev, void *data)
  1284. {
  1285. LIST_HEAD(todo);
  1286. struct fw_cache_entry *fce;
  1287. struct fw_cache_entry *fce_next;
  1288. struct firmware_cache *fwc = &fw_cache;
  1289. devres_for_each_res(dev, fw_name_devm_release,
  1290. devm_name_match, &fw_cache,
  1291. dev_create_fw_entry, &todo);
  1292. list_for_each_entry_safe(fce, fce_next, &todo, list) {
  1293. list_del(&fce->list);
  1294. spin_lock(&fwc->name_lock);
  1295. /* only one cache entry for one firmware */
  1296. if (!__fw_entry_found(fce->name)) {
  1297. list_add(&fce->list, &fwc->fw_names);
  1298. } else {
  1299. free_fw_cache_entry(fce);
  1300. fce = NULL;
  1301. }
  1302. spin_unlock(&fwc->name_lock);
  1303. if (fce)
  1304. async_schedule_domain(__async_dev_cache_fw_image,
  1305. (void *)fce,
  1306. &fw_cache_domain);
  1307. }
  1308. }
  1309. static void __device_uncache_fw_images(void)
  1310. {
  1311. struct firmware_cache *fwc = &fw_cache;
  1312. struct fw_cache_entry *fce;
  1313. spin_lock(&fwc->name_lock);
  1314. while (!list_empty(&fwc->fw_names)) {
  1315. fce = list_entry(fwc->fw_names.next,
  1316. struct fw_cache_entry, list);
  1317. list_del(&fce->list);
  1318. spin_unlock(&fwc->name_lock);
  1319. uncache_firmware(fce->name);
  1320. free_fw_cache_entry(fce);
  1321. spin_lock(&fwc->name_lock);
  1322. }
  1323. spin_unlock(&fwc->name_lock);
  1324. }
  1325. /**
  1326. * device_cache_fw_images - cache devices' firmware
  1327. *
  1328. * If one device called request_firmware or its nowait version
  1329. * successfully before, the firmware names are recored into the
  1330. * device's devres link list, so device_cache_fw_images can call
  1331. * cache_firmware() to cache these firmwares for the device,
  1332. * then the device driver can load its firmwares easily at
  1333. * time when system is not ready to complete loading firmware.
  1334. */
  1335. static void device_cache_fw_images(void)
  1336. {
  1337. struct firmware_cache *fwc = &fw_cache;
  1338. int old_timeout;
  1339. DEFINE_WAIT(wait);
  1340. pr_debug("%s\n", __func__);
  1341. /* cancel uncache work */
  1342. cancel_delayed_work_sync(&fwc->work);
  1343. /*
  1344. * use small loading timeout for caching devices' firmware
  1345. * because all these firmware images have been loaded
  1346. * successfully at lease once, also system is ready for
  1347. * completing firmware loading now. The maximum size of
  1348. * firmware in current distributions is about 2M bytes,
  1349. * so 10 secs should be enough.
  1350. */
  1351. old_timeout = loading_timeout;
  1352. loading_timeout = 10;
  1353. mutex_lock(&fw_lock);
  1354. fwc->state = FW_LOADER_START_CACHE;
  1355. dpm_for_each_dev(NULL, dev_cache_fw_image);
  1356. mutex_unlock(&fw_lock);
  1357. /* wait for completion of caching firmware for all devices */
  1358. async_synchronize_full_domain(&fw_cache_domain);
  1359. loading_timeout = old_timeout;
  1360. }
  1361. /**
  1362. * device_uncache_fw_images - uncache devices' firmware
  1363. *
  1364. * uncache all firmwares which have been cached successfully
  1365. * by device_uncache_fw_images earlier
  1366. */
  1367. static void device_uncache_fw_images(void)
  1368. {
  1369. pr_debug("%s\n", __func__);
  1370. __device_uncache_fw_images();
  1371. }
  1372. static void device_uncache_fw_images_work(struct work_struct *work)
  1373. {
  1374. device_uncache_fw_images();
  1375. }
  1376. /**
  1377. * device_uncache_fw_images_delay - uncache devices firmwares
  1378. * @delay: number of milliseconds to delay uncache device firmwares
  1379. *
  1380. * uncache all devices's firmwares which has been cached successfully
  1381. * by device_cache_fw_images after @delay milliseconds.
  1382. */
  1383. static void device_uncache_fw_images_delay(unsigned long delay)
  1384. {
  1385. queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
  1386. msecs_to_jiffies(delay));
  1387. }
  1388. static int fw_pm_notify(struct notifier_block *notify_block,
  1389. unsigned long mode, void *unused)
  1390. {
  1391. switch (mode) {
  1392. case PM_HIBERNATION_PREPARE:
  1393. case PM_SUSPEND_PREPARE:
  1394. case PM_RESTORE_PREPARE:
  1395. kill_requests_without_uevent();
  1396. device_cache_fw_images();
  1397. break;
  1398. case PM_POST_SUSPEND:
  1399. case PM_POST_HIBERNATION:
  1400. case PM_POST_RESTORE:
  1401. /*
  1402. * In case that system sleep failed and syscore_suspend is
  1403. * not called.
  1404. */
  1405. mutex_lock(&fw_lock);
  1406. fw_cache.state = FW_LOADER_NO_CACHE;
  1407. mutex_unlock(&fw_lock);
  1408. device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
  1409. break;
  1410. }
  1411. return 0;
  1412. }
  1413. /* stop caching firmware once syscore_suspend is reached */
  1414. static int fw_suspend(void)
  1415. {
  1416. fw_cache.state = FW_LOADER_NO_CACHE;
  1417. return 0;
  1418. }
  1419. static struct syscore_ops fw_syscore_ops = {
  1420. .suspend = fw_suspend,
  1421. };
  1422. #else
  1423. static int fw_cache_piggyback_on_request(const char *name)
  1424. {
  1425. return 0;
  1426. }
  1427. #endif
  1428. static void __init fw_cache_init(void)
  1429. {
  1430. spin_lock_init(&fw_cache.lock);
  1431. INIT_LIST_HEAD(&fw_cache.head);
  1432. fw_cache.state = FW_LOADER_NO_CACHE;
  1433. #ifdef CONFIG_PM_SLEEP
  1434. spin_lock_init(&fw_cache.name_lock);
  1435. INIT_LIST_HEAD(&fw_cache.fw_names);
  1436. INIT_DELAYED_WORK(&fw_cache.work,
  1437. device_uncache_fw_images_work);
  1438. fw_cache.pm_notify.notifier_call = fw_pm_notify;
  1439. register_pm_notifier(&fw_cache.pm_notify);
  1440. register_syscore_ops(&fw_syscore_ops);
  1441. #endif
  1442. }
  1443. static int __init firmware_class_init(void)
  1444. {
  1445. fw_cache_init();
  1446. #ifdef CONFIG_FW_LOADER_USER_HELPER
  1447. register_reboot_notifier(&fw_shutdown_nb);
  1448. return class_register(&firmware_class);
  1449. #else
  1450. return 0;
  1451. #endif
  1452. }
  1453. static void __exit firmware_class_exit(void)
  1454. {
  1455. #ifdef CONFIG_PM_SLEEP
  1456. unregister_syscore_ops(&fw_syscore_ops);
  1457. unregister_pm_notifier(&fw_cache.pm_notify);
  1458. #endif
  1459. #ifdef CONFIG_FW_LOADER_USER_HELPER
  1460. unregister_reboot_notifier(&fw_shutdown_nb);
  1461. class_unregister(&firmware_class);
  1462. #endif
  1463. }
  1464. fs_initcall(firmware_class_init);
  1465. module_exit(firmware_class_exit);