firmware_class.c 40 KB

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