fallback.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. /* one pages buffer should be mapped/unmapped only once */
  183. static int map_fw_priv_pages(struct fw_priv *fw_priv)
  184. {
  185. if (!fw_priv->is_paged_buf)
  186. return 0;
  187. vunmap(fw_priv->data);
  188. fw_priv->data = vmap(fw_priv->pages, fw_priv->nr_pages, 0,
  189. PAGE_KERNEL_RO);
  190. if (!fw_priv->data)
  191. return -ENOMEM;
  192. return 0;
  193. }
  194. /**
  195. * firmware_loading_store() - set value in the 'loading' control file
  196. * @dev: device pointer
  197. * @attr: device attribute pointer
  198. * @buf: buffer to scan for loading control value
  199. * @count: number of bytes in @buf
  200. *
  201. * The relevant values are:
  202. *
  203. * 1: Start a load, discarding any previous partial load.
  204. * 0: Conclude the load and hand the data to the driver code.
  205. * -1: Conclude the load with an error and discard any written data.
  206. **/
  207. static ssize_t firmware_loading_store(struct device *dev,
  208. struct device_attribute *attr,
  209. const char *buf, size_t count)
  210. {
  211. struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
  212. struct fw_priv *fw_priv;
  213. ssize_t written = count;
  214. int loading = simple_strtol(buf, NULL, 10);
  215. int i;
  216. mutex_lock(&fw_lock);
  217. fw_priv = fw_sysfs->fw_priv;
  218. if (fw_state_is_aborted(fw_priv))
  219. goto out;
  220. switch (loading) {
  221. case 1:
  222. /* discarding any previous partial load */
  223. if (!fw_sysfs_done(fw_priv)) {
  224. for (i = 0; i < fw_priv->nr_pages; i++)
  225. __free_page(fw_priv->pages[i]);
  226. vfree(fw_priv->pages);
  227. fw_priv->pages = NULL;
  228. fw_priv->page_array_size = 0;
  229. fw_priv->nr_pages = 0;
  230. fw_state_start(fw_priv);
  231. }
  232. break;
  233. case 0:
  234. if (fw_sysfs_loading(fw_priv)) {
  235. int rc;
  236. /*
  237. * Several loading requests may be pending on
  238. * one same firmware buf, so let all requests
  239. * see the mapped 'buf->data' once the loading
  240. * is completed.
  241. * */
  242. rc = map_fw_priv_pages(fw_priv);
  243. if (rc)
  244. dev_err(dev, "%s: map pages failed\n",
  245. __func__);
  246. else
  247. rc = security_kernel_post_read_file(NULL,
  248. fw_priv->data, fw_priv->size,
  249. READING_FIRMWARE);
  250. /*
  251. * Same logic as fw_load_abort, only the DONE bit
  252. * is ignored and we set ABORT only on failure.
  253. */
  254. list_del_init(&fw_priv->pending_list);
  255. if (rc) {
  256. fw_state_aborted(fw_priv);
  257. written = rc;
  258. } else {
  259. fw_state_done(fw_priv);
  260. }
  261. break;
  262. }
  263. /* fallthrough */
  264. default:
  265. dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
  266. /* fallthrough */
  267. case -1:
  268. fw_load_abort(fw_sysfs);
  269. break;
  270. }
  271. out:
  272. mutex_unlock(&fw_lock);
  273. return written;
  274. }
  275. static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
  276. static void firmware_rw_data(struct fw_priv *fw_priv, char *buffer,
  277. loff_t offset, size_t count, bool read)
  278. {
  279. if (read)
  280. memcpy(buffer, fw_priv->data + offset, count);
  281. else
  282. memcpy(fw_priv->data + offset, buffer, count);
  283. }
  284. static void firmware_rw(struct fw_priv *fw_priv, char *buffer,
  285. loff_t offset, size_t count, bool read)
  286. {
  287. while (count) {
  288. void *page_data;
  289. int page_nr = offset >> PAGE_SHIFT;
  290. int page_ofs = offset & (PAGE_SIZE-1);
  291. int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
  292. page_data = kmap(fw_priv->pages[page_nr]);
  293. if (read)
  294. memcpy(buffer, page_data + page_ofs, page_cnt);
  295. else
  296. memcpy(page_data + page_ofs, buffer, page_cnt);
  297. kunmap(fw_priv->pages[page_nr]);
  298. buffer += page_cnt;
  299. offset += page_cnt;
  300. count -= page_cnt;
  301. }
  302. }
  303. static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
  304. struct bin_attribute *bin_attr,
  305. char *buffer, loff_t offset, size_t count)
  306. {
  307. struct device *dev = kobj_to_dev(kobj);
  308. struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
  309. struct fw_priv *fw_priv;
  310. ssize_t ret_count;
  311. mutex_lock(&fw_lock);
  312. fw_priv = fw_sysfs->fw_priv;
  313. if (!fw_priv || fw_sysfs_done(fw_priv)) {
  314. ret_count = -ENODEV;
  315. goto out;
  316. }
  317. if (offset > fw_priv->size) {
  318. ret_count = 0;
  319. goto out;
  320. }
  321. if (count > fw_priv->size - offset)
  322. count = fw_priv->size - offset;
  323. ret_count = count;
  324. if (fw_priv->data)
  325. firmware_rw_data(fw_priv, buffer, offset, count, true);
  326. else
  327. firmware_rw(fw_priv, buffer, offset, count, true);
  328. out:
  329. mutex_unlock(&fw_lock);
  330. return ret_count;
  331. }
  332. static int fw_realloc_pages(struct fw_sysfs *fw_sysfs, int min_size)
  333. {
  334. struct fw_priv *fw_priv= fw_sysfs->fw_priv;
  335. int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
  336. /* If the array of pages is too small, grow it... */
  337. if (fw_priv->page_array_size < pages_needed) {
  338. int new_array_size = max(pages_needed,
  339. fw_priv->page_array_size * 2);
  340. struct page **new_pages;
  341. new_pages = vmalloc(array_size(new_array_size, sizeof(void *)));
  342. if (!new_pages) {
  343. fw_load_abort(fw_sysfs);
  344. return -ENOMEM;
  345. }
  346. memcpy(new_pages, fw_priv->pages,
  347. fw_priv->page_array_size * sizeof(void *));
  348. memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
  349. (new_array_size - fw_priv->page_array_size));
  350. vfree(fw_priv->pages);
  351. fw_priv->pages = new_pages;
  352. fw_priv->page_array_size = new_array_size;
  353. }
  354. while (fw_priv->nr_pages < pages_needed) {
  355. fw_priv->pages[fw_priv->nr_pages] =
  356. alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
  357. if (!fw_priv->pages[fw_priv->nr_pages]) {
  358. fw_load_abort(fw_sysfs);
  359. return -ENOMEM;
  360. }
  361. fw_priv->nr_pages++;
  362. }
  363. return 0;
  364. }
  365. /**
  366. * firmware_data_write() - write method for firmware
  367. * @filp: open sysfs file
  368. * @kobj: kobject for the device
  369. * @bin_attr: bin_attr structure
  370. * @buffer: buffer being written
  371. * @offset: buffer offset for write in total data store area
  372. * @count: buffer size
  373. *
  374. * Data written to the 'data' attribute will be later handed to
  375. * the driver as a firmware image.
  376. **/
  377. static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
  378. struct bin_attribute *bin_attr,
  379. char *buffer, loff_t offset, size_t count)
  380. {
  381. struct device *dev = kobj_to_dev(kobj);
  382. struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
  383. struct fw_priv *fw_priv;
  384. ssize_t retval;
  385. if (!capable(CAP_SYS_RAWIO))
  386. return -EPERM;
  387. mutex_lock(&fw_lock);
  388. fw_priv = fw_sysfs->fw_priv;
  389. if (!fw_priv || fw_sysfs_done(fw_priv)) {
  390. retval = -ENODEV;
  391. goto out;
  392. }
  393. if (fw_priv->data) {
  394. if (offset + count > fw_priv->allocated_size) {
  395. retval = -ENOMEM;
  396. goto out;
  397. }
  398. firmware_rw_data(fw_priv, buffer, offset, count, false);
  399. retval = count;
  400. } else {
  401. retval = fw_realloc_pages(fw_sysfs, offset + count);
  402. if (retval)
  403. goto out;
  404. retval = count;
  405. firmware_rw(fw_priv, buffer, offset, count, false);
  406. }
  407. fw_priv->size = max_t(size_t, offset + count, fw_priv->size);
  408. out:
  409. mutex_unlock(&fw_lock);
  410. return retval;
  411. }
  412. static struct bin_attribute firmware_attr_data = {
  413. .attr = { .name = "data", .mode = 0644 },
  414. .size = 0,
  415. .read = firmware_data_read,
  416. .write = firmware_data_write,
  417. };
  418. static struct attribute *fw_dev_attrs[] = {
  419. &dev_attr_loading.attr,
  420. NULL
  421. };
  422. static struct bin_attribute *fw_dev_bin_attrs[] = {
  423. &firmware_attr_data,
  424. NULL
  425. };
  426. static const struct attribute_group fw_dev_attr_group = {
  427. .attrs = fw_dev_attrs,
  428. .bin_attrs = fw_dev_bin_attrs,
  429. };
  430. static const struct attribute_group *fw_dev_attr_groups[] = {
  431. &fw_dev_attr_group,
  432. NULL
  433. };
  434. static struct fw_sysfs *
  435. fw_create_instance(struct firmware *firmware, const char *fw_name,
  436. struct device *device, enum fw_opt opt_flags)
  437. {
  438. struct fw_sysfs *fw_sysfs;
  439. struct device *f_dev;
  440. fw_sysfs = kzalloc(sizeof(*fw_sysfs), GFP_KERNEL);
  441. if (!fw_sysfs) {
  442. fw_sysfs = ERR_PTR(-ENOMEM);
  443. goto exit;
  444. }
  445. fw_sysfs->nowait = !!(opt_flags & FW_OPT_NOWAIT);
  446. fw_sysfs->fw = firmware;
  447. f_dev = &fw_sysfs->dev;
  448. device_initialize(f_dev);
  449. dev_set_name(f_dev, "%s", fw_name);
  450. f_dev->parent = device;
  451. f_dev->class = &firmware_class;
  452. f_dev->groups = fw_dev_attr_groups;
  453. exit:
  454. return fw_sysfs;
  455. }
  456. /**
  457. * fw_load_sysfs_fallback() - load a firmware via the sysfs fallback mechanism
  458. * @fw_sysfs: firmware sysfs information for the firmware to load
  459. * @opt_flags: flags of options, FW_OPT_*
  460. * @timeout: timeout to wait for the load
  461. *
  462. * In charge of constructing a sysfs fallback interface for firmware loading.
  463. **/
  464. static int fw_load_sysfs_fallback(struct fw_sysfs *fw_sysfs,
  465. enum fw_opt opt_flags, long timeout)
  466. {
  467. int retval = 0;
  468. struct device *f_dev = &fw_sysfs->dev;
  469. struct fw_priv *fw_priv = fw_sysfs->fw_priv;
  470. /* fall back on userspace loading */
  471. if (!fw_priv->data)
  472. fw_priv->is_paged_buf = true;
  473. dev_set_uevent_suppress(f_dev, true);
  474. retval = device_add(f_dev);
  475. if (retval) {
  476. dev_err(f_dev, "%s: device_register failed\n", __func__);
  477. goto err_put_dev;
  478. }
  479. mutex_lock(&fw_lock);
  480. list_add(&fw_priv->pending_list, &pending_fw_head);
  481. mutex_unlock(&fw_lock);
  482. if (opt_flags & FW_OPT_UEVENT) {
  483. fw_priv->need_uevent = true;
  484. dev_set_uevent_suppress(f_dev, false);
  485. dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_name);
  486. kobject_uevent(&fw_sysfs->dev.kobj, KOBJ_ADD);
  487. } else {
  488. timeout = MAX_JIFFY_OFFSET;
  489. }
  490. retval = fw_sysfs_wait_timeout(fw_priv, timeout);
  491. if (retval < 0 && retval != -ENOENT) {
  492. mutex_lock(&fw_lock);
  493. fw_load_abort(fw_sysfs);
  494. mutex_unlock(&fw_lock);
  495. }
  496. if (fw_state_is_aborted(fw_priv)) {
  497. if (retval == -ERESTARTSYS)
  498. retval = -EINTR;
  499. else
  500. retval = -EAGAIN;
  501. } else if (fw_priv->is_paged_buf && !fw_priv->data)
  502. retval = -ENOMEM;
  503. device_del(f_dev);
  504. err_put_dev:
  505. put_device(f_dev);
  506. return retval;
  507. }
  508. static int fw_load_from_user_helper(struct firmware *firmware,
  509. const char *name, struct device *device,
  510. enum fw_opt opt_flags)
  511. {
  512. struct fw_sysfs *fw_sysfs;
  513. long timeout;
  514. int ret;
  515. timeout = firmware_loading_timeout();
  516. if (opt_flags & FW_OPT_NOWAIT) {
  517. timeout = usermodehelper_read_lock_wait(timeout);
  518. if (!timeout) {
  519. dev_dbg(device, "firmware: %s loading timed out\n",
  520. name);
  521. return -EBUSY;
  522. }
  523. } else {
  524. ret = usermodehelper_read_trylock();
  525. if (WARN_ON(ret)) {
  526. dev_err(device, "firmware: %s will not be loaded\n",
  527. name);
  528. return ret;
  529. }
  530. }
  531. fw_sysfs = fw_create_instance(firmware, name, device, opt_flags);
  532. if (IS_ERR(fw_sysfs)) {
  533. ret = PTR_ERR(fw_sysfs);
  534. goto out_unlock;
  535. }
  536. fw_sysfs->fw_priv = firmware->priv;
  537. ret = fw_load_sysfs_fallback(fw_sysfs, opt_flags, timeout);
  538. if (!ret)
  539. ret = assign_fw(firmware, device, opt_flags);
  540. out_unlock:
  541. usermodehelper_read_unlock();
  542. return ret;
  543. }
  544. static bool fw_force_sysfs_fallback(enum fw_opt opt_flags)
  545. {
  546. if (fw_fallback_config.force_sysfs_fallback)
  547. return true;
  548. if (!(opt_flags & FW_OPT_USERHELPER))
  549. return false;
  550. return true;
  551. }
  552. static bool fw_run_sysfs_fallback(enum fw_opt opt_flags)
  553. {
  554. int ret;
  555. if (fw_fallback_config.ignore_sysfs_fallback) {
  556. pr_info_once("Ignoring firmware sysfs fallback due to sysctl knob\n");
  557. return false;
  558. }
  559. if ((opt_flags & FW_OPT_NOFALLBACK))
  560. return false;
  561. /* Also permit LSMs and IMA to fail firmware sysfs fallback */
  562. ret = security_kernel_load_data(LOADING_FIRMWARE);
  563. if (ret < 0)
  564. return false;
  565. return fw_force_sysfs_fallback(opt_flags);
  566. }
  567. /**
  568. * firmware_fallback_sysfs() - use the fallback mechanism to find firmware
  569. * @fw: pointer to firmware image
  570. * @name: name of firmware file to look for
  571. * @device: device for which firmware is being loaded
  572. * @opt_flags: options to control firmware loading behaviour
  573. * @ret: return value from direct lookup which triggered the fallback mechanism
  574. *
  575. * This function is called if direct lookup for the firmware failed, it enables
  576. * a fallback mechanism through userspace by exposing a sysfs loading
  577. * interface. Userspace is in charge of loading the firmware through the syfs
  578. * loading interface. This syfs fallback mechanism may be disabled completely
  579. * on a system by setting the proc sysctl value ignore_sysfs_fallback to true.
  580. * If this false we check if the internal API caller set the @FW_OPT_NOFALLBACK
  581. * flag, if so it would also disable the fallback mechanism. A system may want
  582. * to enfoce the sysfs fallback mechanism at all times, it can do this by
  583. * setting ignore_sysfs_fallback to false and force_sysfs_fallback to true.
  584. * Enabling force_sysfs_fallback is functionally equivalent to build a kernel
  585. * with CONFIG_FW_LOADER_USER_HELPER_FALLBACK.
  586. **/
  587. int firmware_fallback_sysfs(struct firmware *fw, const char *name,
  588. struct device *device,
  589. enum fw_opt opt_flags,
  590. int ret)
  591. {
  592. if (!fw_run_sysfs_fallback(opt_flags))
  593. return ret;
  594. if (!(opt_flags & FW_OPT_NO_WARN))
  595. dev_warn(device, "Falling back to syfs fallback for: %s\n",
  596. name);
  597. else
  598. dev_dbg(device, "Falling back to sysfs fallback for: %s\n",
  599. name);
  600. return fw_load_from_user_helper(fw, name, device, opt_flags);
  601. }