firmware_class.c 30 KB

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