firmware_class.c 37 KB

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