firmware_class.c 42 KB

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