firmware_class.c 39 KB

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