firmware_class.c 46 KB

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