fallback.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/types.h>
  3. #include <linux/kconfig.h>
  4. #include <linux/list.h>
  5. #include <linux/slab.h>
  6. #include <linux/security.h>
  7. #include <linux/highmem.h>
  8. #include <linux/umh.h>
  9. #include <linux/sysctl.h>
  10. #include <linux/vmalloc.h>
  11. #include "fallback.h"
  12. #include "firmware.h"
  13. /*
  14. * firmware fallback mechanism
  15. */
  16. extern struct firmware_fallback_config fw_fallback_config;
  17. /* These getters are vetted to use int properly */
  18. static inline int __firmware_loading_timeout(void)
  19. {
  20. return fw_fallback_config.loading_timeout;
  21. }
  22. /* These setters are vetted to use int properly */
  23. static void __fw_fallback_set_timeout(int timeout)
  24. {
  25. fw_fallback_config.loading_timeout = timeout;
  26. }
  27. /*
  28. * use small loading timeout for caching devices' firmware because all these
  29. * firmware images have been loaded successfully at lease once, also system is
  30. * ready for completing firmware loading now. The maximum size of firmware in
  31. * current distributions is about 2M bytes, so 10 secs should be enough.
  32. */
  33. void fw_fallback_set_cache_timeout(void)
  34. {
  35. fw_fallback_config.old_timeout = __firmware_loading_timeout();
  36. __fw_fallback_set_timeout(10);
  37. }
  38. /* Restores the timeout to the value last configured during normal operation */
  39. void fw_fallback_set_default_timeout(void)
  40. {
  41. __fw_fallback_set_timeout(fw_fallback_config.old_timeout);
  42. }
  43. static long firmware_loading_timeout(void)
  44. {
  45. return __firmware_loading_timeout() > 0 ?
  46. __firmware_loading_timeout() * HZ : MAX_JIFFY_OFFSET;
  47. }
  48. static inline bool fw_sysfs_done(struct fw_priv *fw_priv)
  49. {
  50. return __fw_state_check(fw_priv, FW_STATUS_DONE);
  51. }
  52. static inline bool fw_sysfs_loading(struct fw_priv *fw_priv)
  53. {
  54. return __fw_state_check(fw_priv, FW_STATUS_LOADING);
  55. }
  56. static inline int fw_sysfs_wait_timeout(struct fw_priv *fw_priv, long timeout)
  57. {
  58. return __fw_state_wait_common(fw_priv, timeout);
  59. }
  60. struct fw_sysfs {
  61. bool nowait;
  62. struct device dev;
  63. struct fw_priv *fw_priv;
  64. struct firmware *fw;
  65. };
  66. static struct fw_sysfs *to_fw_sysfs(struct device *dev)
  67. {
  68. return container_of(dev, struct fw_sysfs, dev);
  69. }
  70. static void __fw_load_abort(struct fw_priv *fw_priv)
  71. {
  72. /*
  73. * There is a small window in which user can write to 'loading'
  74. * between loading done and disappearance of 'loading'
  75. */
  76. if (fw_sysfs_done(fw_priv))
  77. return;
  78. list_del_init(&fw_priv->pending_list);
  79. fw_state_aborted(fw_priv);
  80. }
  81. static void fw_load_abort(struct fw_sysfs *fw_sysfs)
  82. {
  83. struct fw_priv *fw_priv = fw_sysfs->fw_priv;
  84. __fw_load_abort(fw_priv);
  85. }
  86. static LIST_HEAD(pending_fw_head);
  87. void kill_pending_fw_fallback_reqs(bool only_kill_custom)
  88. {
  89. struct fw_priv *fw_priv;
  90. struct fw_priv *next;
  91. mutex_lock(&fw_lock);
  92. list_for_each_entry_safe(fw_priv, next, &pending_fw_head,
  93. pending_list) {
  94. if (!fw_priv->need_uevent || !only_kill_custom)
  95. __fw_load_abort(fw_priv);
  96. }
  97. mutex_unlock(&fw_lock);
  98. }
  99. static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
  100. char *buf)
  101. {
  102. return sprintf(buf, "%d\n", __firmware_loading_timeout());
  103. }
  104. /**
  105. * firmware_timeout_store() - set number of seconds to wait for firmware
  106. * @class: device class pointer
  107. * @attr: device attribute pointer
  108. * @buf: buffer to scan for timeout value
  109. * @count: number of bytes in @buf
  110. *
  111. * Sets the number of seconds to wait for the firmware. Once
  112. * this expires an error will be returned to the driver and no
  113. * firmware will be provided.
  114. *
  115. * Note: zero means 'wait forever'.
  116. **/
  117. static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
  118. const char *buf, size_t count)
  119. {
  120. int tmp_loading_timeout = simple_strtol(buf, NULL, 10);
  121. if (tmp_loading_timeout < 0)
  122. tmp_loading_timeout = 0;
  123. __fw_fallback_set_timeout(tmp_loading_timeout);
  124. return count;
  125. }
  126. static CLASS_ATTR_RW(timeout);
  127. static struct attribute *firmware_class_attrs[] = {
  128. &class_attr_timeout.attr,
  129. NULL,
  130. };
  131. ATTRIBUTE_GROUPS(firmware_class);
  132. static void fw_dev_release(struct device *dev)
  133. {
  134. struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
  135. kfree(fw_sysfs);
  136. }
  137. static int do_firmware_uevent(struct fw_sysfs *fw_sysfs, struct kobj_uevent_env *env)
  138. {
  139. if (add_uevent_var(env, "FIRMWARE=%s", fw_sysfs->fw_priv->fw_name))
  140. return -ENOMEM;
  141. if (add_uevent_var(env, "TIMEOUT=%i", __firmware_loading_timeout()))
  142. return -ENOMEM;
  143. if (add_uevent_var(env, "ASYNC=%d", fw_sysfs->nowait))
  144. return -ENOMEM;
  145. return 0;
  146. }
  147. static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
  148. {
  149. struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
  150. int err = 0;
  151. mutex_lock(&fw_lock);
  152. if (fw_sysfs->fw_priv)
  153. err = do_firmware_uevent(fw_sysfs, env);
  154. mutex_unlock(&fw_lock);
  155. return err;
  156. }
  157. static struct class firmware_class = {
  158. .name = "firmware",
  159. .class_groups = firmware_class_groups,
  160. .dev_uevent = firmware_uevent,
  161. .dev_release = fw_dev_release,
  162. };
  163. int register_sysfs_loader(void)
  164. {
  165. return class_register(&firmware_class);
  166. }
  167. void unregister_sysfs_loader(void)
  168. {
  169. class_unregister(&firmware_class);
  170. }
  171. static ssize_t firmware_loading_show(struct device *dev,
  172. struct device_attribute *attr, char *buf)
  173. {
  174. struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
  175. int loading = 0;
  176. mutex_lock(&fw_lock);
  177. if (fw_sysfs->fw_priv)
  178. loading = fw_sysfs_loading(fw_sysfs->fw_priv);
  179. mutex_unlock(&fw_lock);
  180. return sprintf(buf, "%d\n", loading);
  181. }
  182. /* Some architectures don't have PAGE_KERNEL_RO */
  183. #ifndef PAGE_KERNEL_RO
  184. #define PAGE_KERNEL_RO PAGE_KERNEL
  185. #endif
  186. /* one pages buffer should be mapped/unmapped only once */
  187. static int map_fw_priv_pages(struct fw_priv *fw_priv)
  188. {
  189. if (!fw_priv->is_paged_buf)
  190. return 0;
  191. vunmap(fw_priv->data);
  192. fw_priv->data = vmap(fw_priv->pages, fw_priv->nr_pages, 0,
  193. PAGE_KERNEL_RO);
  194. if (!fw_priv->data)
  195. return -ENOMEM;
  196. return 0;
  197. }
  198. /**
  199. * firmware_loading_store() - set value in the 'loading' control file
  200. * @dev: device pointer
  201. * @attr: device attribute pointer
  202. * @buf: buffer to scan for loading control value
  203. * @count: number of bytes in @buf
  204. *
  205. * The relevant values are:
  206. *
  207. * 1: Start a load, discarding any previous partial load.
  208. * 0: Conclude the load and hand the data to the driver code.
  209. * -1: Conclude the load with an error and discard any written data.
  210. **/
  211. static ssize_t firmware_loading_store(struct device *dev,
  212. struct device_attribute *attr,
  213. const char *buf, size_t count)
  214. {
  215. struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
  216. struct fw_priv *fw_priv;
  217. ssize_t written = count;
  218. int loading = simple_strtol(buf, NULL, 10);
  219. int i;
  220. mutex_lock(&fw_lock);
  221. fw_priv = fw_sysfs->fw_priv;
  222. if (fw_state_is_aborted(fw_priv))
  223. goto out;
  224. switch (loading) {
  225. case 1:
  226. /* discarding any previous partial load */
  227. if (!fw_sysfs_done(fw_priv)) {
  228. for (i = 0; i < fw_priv->nr_pages; i++)
  229. __free_page(fw_priv->pages[i]);
  230. vfree(fw_priv->pages);
  231. fw_priv->pages = NULL;
  232. fw_priv->page_array_size = 0;
  233. fw_priv->nr_pages = 0;
  234. fw_state_start(fw_priv);
  235. }
  236. break;
  237. case 0:
  238. if (fw_sysfs_loading(fw_priv)) {
  239. int rc;
  240. /*
  241. * Several loading requests may be pending on
  242. * one same firmware buf, so let all requests
  243. * see the mapped 'buf->data' once the loading
  244. * is completed.
  245. * */
  246. rc = map_fw_priv_pages(fw_priv);
  247. if (rc)
  248. dev_err(dev, "%s: map pages failed\n",
  249. __func__);
  250. else
  251. rc = security_kernel_post_read_file(NULL,
  252. fw_priv->data, fw_priv->size,
  253. READING_FIRMWARE);
  254. /*
  255. * Same logic as fw_load_abort, only the DONE bit
  256. * is ignored and we set ABORT only on failure.
  257. */
  258. list_del_init(&fw_priv->pending_list);
  259. if (rc) {
  260. fw_state_aborted(fw_priv);
  261. written = rc;
  262. } else {
  263. fw_state_done(fw_priv);
  264. }
  265. break;
  266. }
  267. /* fallthrough */
  268. default:
  269. dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
  270. /* fallthrough */
  271. case -1:
  272. fw_load_abort(fw_sysfs);
  273. break;
  274. }
  275. out:
  276. mutex_unlock(&fw_lock);
  277. return written;
  278. }
  279. static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
  280. static void firmware_rw_data(struct fw_priv *fw_priv, char *buffer,
  281. loff_t offset, size_t count, bool read)
  282. {
  283. if (read)
  284. memcpy(buffer, fw_priv->data + offset, count);
  285. else
  286. memcpy(fw_priv->data + offset, buffer, count);
  287. }
  288. static void firmware_rw(struct fw_priv *fw_priv, char *buffer,
  289. loff_t offset, size_t count, bool read)
  290. {
  291. while (count) {
  292. void *page_data;
  293. int page_nr = offset >> PAGE_SHIFT;
  294. int page_ofs = offset & (PAGE_SIZE-1);
  295. int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
  296. page_data = kmap(fw_priv->pages[page_nr]);
  297. if (read)
  298. memcpy(buffer, page_data + page_ofs, page_cnt);
  299. else
  300. memcpy(page_data + page_ofs, buffer, page_cnt);
  301. kunmap(fw_priv->pages[page_nr]);
  302. buffer += page_cnt;
  303. offset += page_cnt;
  304. count -= page_cnt;
  305. }
  306. }
  307. static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
  308. struct bin_attribute *bin_attr,
  309. char *buffer, loff_t offset, size_t count)
  310. {
  311. struct device *dev = kobj_to_dev(kobj);
  312. struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
  313. struct fw_priv *fw_priv;
  314. ssize_t ret_count;
  315. mutex_lock(&fw_lock);
  316. fw_priv = fw_sysfs->fw_priv;
  317. if (!fw_priv || fw_sysfs_done(fw_priv)) {
  318. ret_count = -ENODEV;
  319. goto out;
  320. }
  321. if (offset > fw_priv->size) {
  322. ret_count = 0;
  323. goto out;
  324. }
  325. if (count > fw_priv->size - offset)
  326. count = fw_priv->size - offset;
  327. ret_count = count;
  328. if (fw_priv->data)
  329. firmware_rw_data(fw_priv, buffer, offset, count, true);
  330. else
  331. firmware_rw(fw_priv, buffer, offset, count, true);
  332. out:
  333. mutex_unlock(&fw_lock);
  334. return ret_count;
  335. }
  336. static int fw_realloc_pages(struct fw_sysfs *fw_sysfs, int min_size)
  337. {
  338. struct fw_priv *fw_priv= fw_sysfs->fw_priv;
  339. int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
  340. /* If the array of pages is too small, grow it... */
  341. if (fw_priv->page_array_size < pages_needed) {
  342. int new_array_size = max(pages_needed,
  343. fw_priv->page_array_size * 2);
  344. struct page **new_pages;
  345. new_pages = vmalloc(array_size(new_array_size, sizeof(void *)));
  346. if (!new_pages) {
  347. fw_load_abort(fw_sysfs);
  348. return -ENOMEM;
  349. }
  350. memcpy(new_pages, fw_priv->pages,
  351. fw_priv->page_array_size * sizeof(void *));
  352. memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
  353. (new_array_size - fw_priv->page_array_size));
  354. vfree(fw_priv->pages);
  355. fw_priv->pages = new_pages;
  356. fw_priv->page_array_size = new_array_size;
  357. }
  358. while (fw_priv->nr_pages < pages_needed) {
  359. fw_priv->pages[fw_priv->nr_pages] =
  360. alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
  361. if (!fw_priv->pages[fw_priv->nr_pages]) {
  362. fw_load_abort(fw_sysfs);
  363. return -ENOMEM;
  364. }
  365. fw_priv->nr_pages++;
  366. }
  367. return 0;
  368. }
  369. /**
  370. * firmware_data_write() - write method for firmware
  371. * @filp: open sysfs file
  372. * @kobj: kobject for the device
  373. * @bin_attr: bin_attr structure
  374. * @buffer: buffer being written
  375. * @offset: buffer offset for write in total data store area
  376. * @count: buffer size
  377. *
  378. * Data written to the 'data' attribute will be later handed to
  379. * the driver as a firmware image.
  380. **/
  381. static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
  382. struct bin_attribute *bin_attr,
  383. char *buffer, loff_t offset, size_t count)
  384. {
  385. struct device *dev = kobj_to_dev(kobj);
  386. struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
  387. struct fw_priv *fw_priv;
  388. ssize_t retval;
  389. if (!capable(CAP_SYS_RAWIO))
  390. return -EPERM;
  391. mutex_lock(&fw_lock);
  392. fw_priv = fw_sysfs->fw_priv;
  393. if (!fw_priv || fw_sysfs_done(fw_priv)) {
  394. retval = -ENODEV;
  395. goto out;
  396. }
  397. if (fw_priv->data) {
  398. if (offset + count > fw_priv->allocated_size) {
  399. retval = -ENOMEM;
  400. goto out;
  401. }
  402. firmware_rw_data(fw_priv, buffer, offset, count, false);
  403. retval = count;
  404. } else {
  405. retval = fw_realloc_pages(fw_sysfs, offset + count);
  406. if (retval)
  407. goto out;
  408. retval = count;
  409. firmware_rw(fw_priv, buffer, offset, count, false);
  410. }
  411. fw_priv->size = max_t(size_t, offset + count, fw_priv->size);
  412. out:
  413. mutex_unlock(&fw_lock);
  414. return retval;
  415. }
  416. static struct bin_attribute firmware_attr_data = {
  417. .attr = { .name = "data", .mode = 0644 },
  418. .size = 0,
  419. .read = firmware_data_read,
  420. .write = firmware_data_write,
  421. };
  422. static struct attribute *fw_dev_attrs[] = {
  423. &dev_attr_loading.attr,
  424. NULL
  425. };
  426. static struct bin_attribute *fw_dev_bin_attrs[] = {
  427. &firmware_attr_data,
  428. NULL
  429. };
  430. static const struct attribute_group fw_dev_attr_group = {
  431. .attrs = fw_dev_attrs,
  432. .bin_attrs = fw_dev_bin_attrs,
  433. };
  434. static const struct attribute_group *fw_dev_attr_groups[] = {
  435. &fw_dev_attr_group,
  436. NULL
  437. };
  438. static struct fw_sysfs *
  439. fw_create_instance(struct firmware *firmware, const char *fw_name,
  440. struct device *device, enum fw_opt opt_flags)
  441. {
  442. struct fw_sysfs *fw_sysfs;
  443. struct device *f_dev;
  444. fw_sysfs = kzalloc(sizeof(*fw_sysfs), GFP_KERNEL);
  445. if (!fw_sysfs) {
  446. fw_sysfs = ERR_PTR(-ENOMEM);
  447. goto exit;
  448. }
  449. fw_sysfs->nowait = !!(opt_flags & FW_OPT_NOWAIT);
  450. fw_sysfs->fw = firmware;
  451. f_dev = &fw_sysfs->dev;
  452. device_initialize(f_dev);
  453. dev_set_name(f_dev, "%s", fw_name);
  454. f_dev->parent = device;
  455. f_dev->class = &firmware_class;
  456. f_dev->groups = fw_dev_attr_groups;
  457. exit:
  458. return fw_sysfs;
  459. }
  460. /**
  461. * fw_load_sysfs_fallback() - load a firmware via the sysfs fallback mechanism
  462. * @fw_sysfs: firmware sysfs information for the firmware to load
  463. * @opt_flags: flags of options, FW_OPT_*
  464. * @timeout: timeout to wait for the load
  465. *
  466. * In charge of constructing a sysfs fallback interface for firmware loading.
  467. **/
  468. static int fw_load_sysfs_fallback(struct fw_sysfs *fw_sysfs,
  469. enum fw_opt opt_flags, long timeout)
  470. {
  471. int retval = 0;
  472. struct device *f_dev = &fw_sysfs->dev;
  473. struct fw_priv *fw_priv = fw_sysfs->fw_priv;
  474. /* fall back on userspace loading */
  475. if (!fw_priv->data)
  476. fw_priv->is_paged_buf = true;
  477. dev_set_uevent_suppress(f_dev, true);
  478. retval = device_add(f_dev);
  479. if (retval) {
  480. dev_err(f_dev, "%s: device_register failed\n", __func__);
  481. goto err_put_dev;
  482. }
  483. mutex_lock(&fw_lock);
  484. list_add(&fw_priv->pending_list, &pending_fw_head);
  485. mutex_unlock(&fw_lock);
  486. if (opt_flags & FW_OPT_UEVENT) {
  487. fw_priv->need_uevent = true;
  488. dev_set_uevent_suppress(f_dev, false);
  489. dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_name);
  490. kobject_uevent(&fw_sysfs->dev.kobj, KOBJ_ADD);
  491. } else {
  492. timeout = MAX_JIFFY_OFFSET;
  493. }
  494. retval = fw_sysfs_wait_timeout(fw_priv, timeout);
  495. if (retval < 0) {
  496. mutex_lock(&fw_lock);
  497. fw_load_abort(fw_sysfs);
  498. mutex_unlock(&fw_lock);
  499. }
  500. if (fw_state_is_aborted(fw_priv)) {
  501. if (retval == -ERESTARTSYS)
  502. retval = -EINTR;
  503. else
  504. retval = -EAGAIN;
  505. } else if (fw_priv->is_paged_buf && !fw_priv->data)
  506. retval = -ENOMEM;
  507. device_del(f_dev);
  508. err_put_dev:
  509. put_device(f_dev);
  510. return retval;
  511. }
  512. static int fw_load_from_user_helper(struct firmware *firmware,
  513. const char *name, struct device *device,
  514. enum fw_opt opt_flags)
  515. {
  516. struct fw_sysfs *fw_sysfs;
  517. long timeout;
  518. int ret;
  519. timeout = firmware_loading_timeout();
  520. if (opt_flags & FW_OPT_NOWAIT) {
  521. timeout = usermodehelper_read_lock_wait(timeout);
  522. if (!timeout) {
  523. dev_dbg(device, "firmware: %s loading timed out\n",
  524. name);
  525. return -EBUSY;
  526. }
  527. } else {
  528. ret = usermodehelper_read_trylock();
  529. if (WARN_ON(ret)) {
  530. dev_err(device, "firmware: %s will not be loaded\n",
  531. name);
  532. return ret;
  533. }
  534. }
  535. fw_sysfs = fw_create_instance(firmware, name, device, opt_flags);
  536. if (IS_ERR(fw_sysfs)) {
  537. ret = PTR_ERR(fw_sysfs);
  538. goto out_unlock;
  539. }
  540. fw_sysfs->fw_priv = firmware->priv;
  541. ret = fw_load_sysfs_fallback(fw_sysfs, opt_flags, timeout);
  542. if (!ret)
  543. ret = assign_fw(firmware, device, opt_flags);
  544. out_unlock:
  545. usermodehelper_read_unlock();
  546. return ret;
  547. }
  548. static bool fw_force_sysfs_fallback(enum fw_opt opt_flags)
  549. {
  550. if (fw_fallback_config.force_sysfs_fallback)
  551. return true;
  552. if (!(opt_flags & FW_OPT_USERHELPER))
  553. return false;
  554. return true;
  555. }
  556. static bool fw_run_sysfs_fallback(enum fw_opt opt_flags)
  557. {
  558. if (fw_fallback_config.ignore_sysfs_fallback) {
  559. pr_info_once("Ignoring firmware sysfs fallback due to sysctl knob\n");
  560. return false;
  561. }
  562. if ((opt_flags & FW_OPT_NOFALLBACK))
  563. return false;
  564. return fw_force_sysfs_fallback(opt_flags);
  565. }
  566. /**
  567. * firmware_fallback_sysfs() - use the fallback mechanism to find firmware
  568. * @fw: pointer to firmware image
  569. * @name: name of firmware file to look for
  570. * @device: device for which firmware is being loaded
  571. * @opt_flags: options to control firmware loading behaviour
  572. * @ret: return value from direct lookup which triggered the fallback mechanism
  573. *
  574. * This function is called if direct lookup for the firmware failed, it enables
  575. * a fallback mechanism through userspace by exposing a sysfs loading
  576. * interface. Userspace is in charge of loading the firmware through the syfs
  577. * loading interface. This syfs fallback mechanism may be disabled completely
  578. * on a system by setting the proc sysctl value ignore_sysfs_fallback to true.
  579. * If this false we check if the internal API caller set the @FW_OPT_NOFALLBACK
  580. * flag, if so it would also disable the fallback mechanism. A system may want
  581. * to enfoce the sysfs fallback mechanism at all times, it can do this by
  582. * setting ignore_sysfs_fallback to false and force_sysfs_fallback to true.
  583. * Enabling force_sysfs_fallback is functionally equivalent to build a kernel
  584. * with CONFIG_FW_LOADER_USER_HELPER_FALLBACK.
  585. **/
  586. int firmware_fallback_sysfs(struct firmware *fw, const char *name,
  587. struct device *device,
  588. enum fw_opt opt_flags,
  589. int ret)
  590. {
  591. if (!fw_run_sysfs_fallback(opt_flags))
  592. return ret;
  593. if (!(opt_flags & FW_OPT_NO_WARN))
  594. dev_warn(device, "Falling back to syfs fallback for: %s\n",
  595. name);
  596. else
  597. dev_dbg(device, "Falling back to sysfs fallback for: %s\n",
  598. name);
  599. return fw_load_from_user_helper(fw, name, device, opt_flags);
  600. }