firmware_class.c 39 KB

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