firmware_class.c 30 KB

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