firmware_class.c 32 KB

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