firmware_class.c 40 KB

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