firmware_class.c 36 KB

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