blob: bfbe1e15412889dfb40e699af7c3623a06d83253 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * firmware_class.c - Multi purpose firmware loading support
3 *
Markus Rechberger87d37a42007-06-04 18:45:44 +02004 * Copyright (c) 2003 Manuel Estrada Sainz
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Please see Documentation/firmware_class/ for more information.
7 *
8 */
9
Randy.Dunlapc59ede72006-01-11 12:17:46 -080010#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/device.h>
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/timer.h>
15#include <linux/vmalloc.h>
16#include <linux/interrupt.h>
17#include <linux/bitops.h>
Laura Garciacad1e552006-05-23 23:22:38 +020018#include <linux/mutex.h>
Stephen Boyda36cf842012-03-28 23:31:00 +020019#include <linux/workqueue.h>
David Woodhouse6e03a202009-04-09 22:04:07 -070020#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/firmware.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Stephen Boyda36cf842012-03-28 23:31:00 +020023#include <linux/sched.h>
Linus Torvaldsabb139e2012-10-03 15:58:32 -070024#include <linux/file.h>
Ming Lei1f2b7952012-08-04 12:01:21 +080025#include <linux/list.h>
Mimi Zohare40ba6d2015-11-19 12:39:22 -050026#include <linux/fs.h>
Ming Lei37276a52012-08-04 12:01:27 +080027#include <linux/async.h>
28#include <linux/pm.h>
Ming Lei07646d92012-08-04 12:01:29 +080029#include <linux/suspend.h>
Ming Leiac39b3e2012-08-20 19:04:16 +080030#include <linux/syscore_ops.h>
Takashi Iwaife304142013-05-22 18:28:38 +020031#include <linux/reboot.h>
Kees Cook6593d922014-02-25 13:06:00 -080032#include <linux/security.h>
Ming Lei37276a52012-08-04 12:01:27 +080033
Linus Torvaldsabb139e2012-10-03 15:58:32 -070034#include <generated/utsrelease.h>
35
Ming Lei37276a52012-08-04 12:01:27 +080036#include "base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Markus Rechberger87d37a42007-06-04 18:45:44 +020038MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070039MODULE_DESCRIPTION("Multi purpose firmware loading support");
40MODULE_LICENSE("GPL");
41
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080042/* Builtin firmware support */
43
44#ifdef CONFIG_FW_LOADER
45
46extern struct builtin_fw __start_builtin_fw[];
47extern struct builtin_fw __end_builtin_fw[];
48
Stephen Boyda098ecd2016-08-02 14:04:28 -070049static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
50 void *buf, size_t size)
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080051{
52 struct builtin_fw *b_fw;
53
54 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
55 if (strcmp(name, b_fw->name) == 0) {
56 fw->size = b_fw->size;
57 fw->data = b_fw->data;
Stephen Boyda098ecd2016-08-02 14:04:28 -070058
59 if (buf && fw->size <= size)
60 memcpy(buf, fw->data, fw->size);
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080061 return true;
62 }
63 }
64
65 return false;
66}
67
68static bool fw_is_builtin_firmware(const struct firmware *fw)
69{
70 struct builtin_fw *b_fw;
71
72 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
73 if (fw->data == b_fw->data)
74 return true;
75
76 return false;
77}
78
79#else /* Module case - no builtin firmware support */
80
Stephen Boyda098ecd2016-08-02 14:04:28 -070081static inline bool fw_get_builtin_firmware(struct firmware *fw,
82 const char *name, void *buf,
83 size_t size)
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080084{
85 return false;
86}
87
88static inline bool fw_is_builtin_firmware(const struct firmware *fw)
89{
90 return false;
91}
92#endif
93
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010094enum fw_status {
95 FW_STATUS_UNKNOWN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 FW_STATUS_LOADING,
97 FW_STATUS_DONE,
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010098 FW_STATUS_ABORTED,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
Dave Jones2f651682007-01-25 15:56:15 -0500101static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200103static inline long firmware_loading_timeout(void)
104{
Ming Lei68ff2a02015-01-13 00:02:01 +0800105 return loading_timeout > 0 ? loading_timeout * HZ : MAX_JIFFY_OFFSET;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200106}
107
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100108/*
109 * Concurrent request_firmware() for the same firmware need to be
110 * serialized. struct fw_state is simple state machine which hold the
111 * state of the firmware loading.
112 */
113struct fw_state {
Luis R. Rodrigueze44565f2017-07-20 13:13:09 -0700114 struct completion completion;
Daniel Wagner0430caf2016-11-17 11:00:49 +0100115 enum fw_status status;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100116};
117
118static void fw_state_init(struct fw_state *fw_st)
119{
Luis R. Rodrigueze44565f2017-07-20 13:13:09 -0700120 init_completion(&fw_st->completion);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100121 fw_st->status = FW_STATUS_UNKNOWN;
122}
123
Daniel Wagner5b029622016-11-17 11:00:50 +0100124static inline bool __fw_state_is_done(enum fw_status status)
125{
126 return status == FW_STATUS_DONE || status == FW_STATUS_ABORTED;
127}
128
Bjorn Andersson5d47ec02016-12-06 17:01:45 -0800129static int __fw_state_wait_common(struct fw_state *fw_st, long timeout)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100130{
131 long ret;
132
Luis R. Rodriguez260d9f22017-07-20 13:13:11 -0700133 ret = wait_for_completion_killable_timeout(&fw_st->completion, timeout);
Daniel Wagner5b029622016-11-17 11:00:50 +0100134 if (ret != 0 && fw_st->status == FW_STATUS_ABORTED)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100135 return -ENOENT;
Bjorn Andersson5d47ec02016-12-06 17:01:45 -0800136 if (!ret)
137 return -ETIMEDOUT;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100138
Bjorn Andersson5d47ec02016-12-06 17:01:45 -0800139 return ret < 0 ? ret : 0;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100140}
141
142static void __fw_state_set(struct fw_state *fw_st,
143 enum fw_status status)
144{
Daniel Wagner0430caf2016-11-17 11:00:49 +0100145 WRITE_ONCE(fw_st->status, status);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100146
Daniel Wagner0430caf2016-11-17 11:00:49 +0100147 if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED)
Luis R. Rodrigueze44565f2017-07-20 13:13:09 -0700148 complete_all(&fw_st->completion);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100149}
150
151#define fw_state_start(fw_st) \
152 __fw_state_set(fw_st, FW_STATUS_LOADING)
153#define fw_state_done(fw_st) \
154 __fw_state_set(fw_st, FW_STATUS_DONE)
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -0700155#define fw_state_aborted(fw_st) \
156 __fw_state_set(fw_st, FW_STATUS_ABORTED)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100157#define fw_state_wait(fw_st) \
158 __fw_state_wait_common(fw_st, MAX_SCHEDULE_TIMEOUT)
159
Daniel Wagnerfab82cb2016-11-17 11:00:51 +0100160static int __fw_state_check(struct fw_state *fw_st, enum fw_status status)
161{
162 return fw_st->status == status;
163}
164
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -0700165#define fw_state_is_aborted(fw_st) \
166 __fw_state_check(fw_st, FW_STATUS_ABORTED)
167
168#ifdef CONFIG_FW_LOADER_USER_HELPER
169
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100170#define fw_state_aborted(fw_st) \
171 __fw_state_set(fw_st, FW_STATUS_ABORTED)
Daniel Wagnerfab82cb2016-11-17 11:00:51 +0100172#define fw_state_is_done(fw_st) \
173 __fw_state_check(fw_st, FW_STATUS_DONE)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100174#define fw_state_is_loading(fw_st) \
175 __fw_state_check(fw_st, FW_STATUS_LOADING)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100176#define fw_state_wait_timeout(fw_st, timeout) \
177 __fw_state_wait_common(fw_st, timeout)
178
179#endif /* CONFIG_FW_LOADER_USER_HELPER */
180
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100181/* firmware behavior options */
182#define FW_OPT_UEVENT (1U << 0)
183#define FW_OPT_NOWAIT (1U << 1)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100184#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200185#define FW_OPT_USERHELPER (1U << 2)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100186#else
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200187#define FW_OPT_USERHELPER 0
188#endif
189#ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK
190#define FW_OPT_FALLBACK FW_OPT_USERHELPER
191#else
192#define FW_OPT_FALLBACK 0
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100193#endif
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -0700194#define FW_OPT_NO_WARN (1U << 3)
Vikram Mulukutla0e742e92016-08-02 14:04:25 -0700195#define FW_OPT_NOCACHE (1U << 4)
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100196
Ming Lei1f2b7952012-08-04 12:01:21 +0800197struct firmware_cache {
198 /* firmware_buf instance will be added into the below list */
199 spinlock_t lock;
200 struct list_head head;
Ming Leicfe016b2012-09-08 17:32:30 +0800201 int state;
Ming Lei37276a52012-08-04 12:01:27 +0800202
Ming Leicfe016b2012-09-08 17:32:30 +0800203#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +0800204 /*
205 * Names of firmware images which have been cached successfully
206 * will be added into the below list so that device uncache
207 * helper can trace which firmware images have been cached
208 * before.
209 */
210 spinlock_t name_lock;
211 struct list_head fw_names;
212
Ming Lei37276a52012-08-04 12:01:27 +0800213 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +0800214
215 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +0800216#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800217};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Ming Lei12446912012-08-04 12:01:20 +0800219struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800220 struct kref ref;
221 struct list_head list;
Ming Lei1f2b7952012-08-04 12:01:21 +0800222 struct firmware_cache *fwc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100223 struct fw_state fw_st;
Ming Lei65710cb2012-08-04 12:01:16 +0800224 void *data;
225 size_t size;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700226 size_t allocated_size;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100227#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100228 bool is_paged_buf;
Ming Leiaf5bc112013-05-27 18:30:04 +0800229 bool need_uevent;
David Woodhouse6e03a202009-04-09 22:04:07 -0700230 struct page **pages;
231 int nr_pages;
232 int page_array_size;
Takashi Iwaife304142013-05-22 18:28:38 +0200233 struct list_head pending_list;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100234#endif
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700235 const char *fw_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236};
237
Ming Lei37276a52012-08-04 12:01:27 +0800238struct fw_cache_entry {
239 struct list_head list;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700240 const char *name;
Ming Lei37276a52012-08-04 12:01:27 +0800241};
242
Ming Leif531f05a2012-08-04 12:01:25 +0800243struct fw_name_devm {
244 unsigned long magic;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700245 const char *name;
Ming Leif531f05a2012-08-04 12:01:25 +0800246};
247
Ming Lei1f2b7952012-08-04 12:01:21 +0800248#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
249
Ming Leiac39b3e2012-08-20 19:04:16 +0800250#define FW_LOADER_NO_CACHE 0
251#define FW_LOADER_START_CACHE 1
252
253static int fw_cache_piggyback_on_request(const char *name);
254
Ming Lei1f2b7952012-08-04 12:01:21 +0800255/* fw_lock could be moved to 'struct firmware_priv' but since it is just
256 * guarding for corner cases a global lock should be OK */
257static DEFINE_MUTEX(fw_lock);
258
Luis R. Rodriguez81f95072017-05-02 01:31:05 -0700259static bool __enable_firmware = false;
260
261static void enable_firmware(void)
262{
263 mutex_lock(&fw_lock);
264 __enable_firmware = true;
265 mutex_unlock(&fw_lock);
266}
267
268static void disable_firmware(void)
269{
270 mutex_lock(&fw_lock);
271 __enable_firmware = false;
272 mutex_unlock(&fw_lock);
273}
274
275/*
276 * When disabled only the built-in firmware and the firmware cache will be
277 * used to look for firmware.
278 */
279static bool firmware_enabled(void)
280{
281 bool enabled = false;
282
283 mutex_lock(&fw_lock);
284 if (__enable_firmware)
285 enabled = true;
286 mutex_unlock(&fw_lock);
287
288 return enabled;
289}
290
Ming Lei1f2b7952012-08-04 12:01:21 +0800291static struct firmware_cache fw_cache;
292
293static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
Stephen Boyda098ecd2016-08-02 14:04:28 -0700294 struct firmware_cache *fwc,
295 void *dbuf, size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800296{
297 struct firmware_buf *buf;
298
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700299 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
Ming Lei1f2b7952012-08-04 12:01:21 +0800300 if (!buf)
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700301 return NULL;
302
303 buf->fw_id = kstrdup_const(fw_name, GFP_ATOMIC);
304 if (!buf->fw_id) {
305 kfree(buf);
306 return NULL;
307 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800308
309 kref_init(&buf->ref);
Ming Lei1f2b7952012-08-04 12:01:21 +0800310 buf->fwc = fwc;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700311 buf->data = dbuf;
312 buf->allocated_size = size;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100313 fw_state_init(&buf->fw_st);
Takashi Iwaife304142013-05-22 18:28:38 +0200314#ifdef CONFIG_FW_LOADER_USER_HELPER
315 INIT_LIST_HEAD(&buf->pending_list);
316#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800317
318 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
319
320 return buf;
321}
322
Ming Lei2887b392012-08-04 12:01:22 +0800323static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
324{
325 struct firmware_buf *tmp;
326 struct firmware_cache *fwc = &fw_cache;
327
328 list_for_each_entry(tmp, &fwc->head, list)
329 if (!strcmp(tmp->fw_id, fw_name))
330 return tmp;
331 return NULL;
332}
333
Ming Lei1f2b7952012-08-04 12:01:21 +0800334static int fw_lookup_and_allocate_buf(const char *fw_name,
335 struct firmware_cache *fwc,
Stephen Boyda098ecd2016-08-02 14:04:28 -0700336 struct firmware_buf **buf, void *dbuf,
337 size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800338{
339 struct firmware_buf *tmp;
340
341 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800342 tmp = __fw_lookup_buf(fw_name);
343 if (tmp) {
344 kref_get(&tmp->ref);
345 spin_unlock(&fwc->lock);
346 *buf = tmp;
347 return 1;
348 }
Stephen Boyda098ecd2016-08-02 14:04:28 -0700349 tmp = __allocate_fw_buf(fw_name, fwc, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +0800350 if (tmp)
351 list_add(&tmp->list, &fwc->head);
352 spin_unlock(&fwc->lock);
353
354 *buf = tmp;
355
356 return tmp ? 0 : -ENOMEM;
357}
358
359static void __fw_free_buf(struct kref *ref)
Bart Van Assche98233b22014-01-04 14:20:36 +0100360 __releases(&fwc->lock)
Ming Lei1f2b7952012-08-04 12:01:21 +0800361{
362 struct firmware_buf *buf = to_fwbuf(ref);
363 struct firmware_cache *fwc = buf->fwc;
Ming Lei1f2b7952012-08-04 12:01:21 +0800364
365 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
366 __func__, buf->fw_id, buf, buf->data,
367 (unsigned int)buf->size);
368
Ming Lei1f2b7952012-08-04 12:01:21 +0800369 list_del(&buf->list);
370 spin_unlock(&fwc->lock);
371
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100372#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100373 if (buf->is_paged_buf) {
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100374 int i;
Ming Lei746058f2012-10-09 12:01:03 +0800375 vunmap(buf->data);
376 for (i = 0; i < buf->nr_pages; i++)
377 __free_page(buf->pages[i]);
Chen Feng10a3fbf2015-12-16 17:03:15 +0800378 vfree(buf->pages);
Ming Lei746058f2012-10-09 12:01:03 +0800379 } else
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100380#endif
Stephen Boyda098ecd2016-08-02 14:04:28 -0700381 if (!buf->allocated_size)
Ming Lei746058f2012-10-09 12:01:03 +0800382 vfree(buf->data);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700383 kfree_const(buf->fw_id);
Ming Lei1f2b7952012-08-04 12:01:21 +0800384 kfree(buf);
385}
386
387static void fw_free_buf(struct firmware_buf *buf)
388{
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800389 struct firmware_cache *fwc = buf->fwc;
390 spin_lock(&fwc->lock);
391 if (!kref_put(&buf->ref, __fw_free_buf))
392 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800393}
394
Ming Lei746058f2012-10-09 12:01:03 +0800395/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800396static char fw_path_para[256];
397static const char * const fw_path[] = {
398 fw_path_para,
Ming Lei746058f2012-10-09 12:01:03 +0800399 "/lib/firmware/updates/" UTS_RELEASE,
400 "/lib/firmware/updates",
401 "/lib/firmware/" UTS_RELEASE,
402 "/lib/firmware"
403};
404
Ming Lei27602842012-11-03 17:47:58 +0800405/*
406 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
407 * from kernel command line because firmware_class is generally built in
408 * kernel instead of module.
409 */
410module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
411MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
412
Stephen Boyda098ecd2016-08-02 14:04:28 -0700413static int
414fw_get_filesystem_firmware(struct device *device, struct firmware_buf *buf)
Ming Lei746058f2012-10-09 12:01:03 +0800415{
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500416 loff_t size;
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700417 int i, len;
Neil Horman3e358ac2013-09-06 15:36:08 -0400418 int rc = -ENOENT;
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700419 char *path;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700420 enum kernel_read_file_id id = READING_FIRMWARE;
421 size_t msize = INT_MAX;
422
423 /* Already populated data member means we're loading into a buffer */
424 if (buf->data) {
425 id = READING_FIRMWARE_PREALLOC_BUFFER;
426 msize = buf->allocated_size;
427 }
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700428
429 path = __getname();
430 if (!path)
431 return -ENOMEM;
Ming Lei746058f2012-10-09 12:01:03 +0800432
433 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
Ming Lei27602842012-11-03 17:47:58 +0800434 /* skip the unset customized path */
435 if (!fw_path[i][0])
436 continue;
437
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700438 len = snprintf(path, PATH_MAX, "%s/%s",
439 fw_path[i], buf->fw_id);
440 if (len >= PATH_MAX) {
441 rc = -ENAMETOOLONG;
442 break;
443 }
Ming Lei746058f2012-10-09 12:01:03 +0800444
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500445 buf->size = 0;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700446 rc = kernel_read_file_from_path(path, &buf->data, &size, msize,
447 id);
Kees Cook4b2530d2016-02-04 13:15:02 -0800448 if (rc) {
Luis R. Rodriguez8e516aa2016-02-28 21:57:55 +0100449 if (rc == -ENOENT)
450 dev_dbg(device, "loading %s failed with error %d\n",
451 path, rc);
452 else
453 dev_warn(device, "loading %s failed with error %d\n",
454 path, rc);
Kees Cook4b2530d2016-02-04 13:15:02 -0800455 continue;
456 }
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500457 dev_dbg(device, "direct-loading %s\n", buf->fw_id);
458 buf->size = size;
Daniel Wagner5b029622016-11-17 11:00:50 +0100459 fw_state_done(&buf->fw_st);
Kees Cook4b2530d2016-02-04 13:15:02 -0800460 break;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100461 }
Kees Cook4b2530d2016-02-04 13:15:02 -0800462 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100463
Neil Horman3e358ac2013-09-06 15:36:08 -0400464 return rc;
Ming Lei746058f2012-10-09 12:01:03 +0800465}
466
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100467/* firmware holds the ownership of pages */
468static void firmware_free_data(const struct firmware *fw)
469{
470 /* Loaded directly? */
471 if (!fw->priv) {
472 vfree(fw->data);
473 return;
474 }
475 fw_free_buf(fw->priv);
476}
477
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100478/* store the pages buffer info firmware from buf */
479static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
480{
481 fw->priv = buf;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100482#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100483 fw->pages = buf->pages;
484#endif
485 fw->size = buf->size;
486 fw->data = buf->data;
487
488 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
489 __func__, buf->fw_id, buf, buf->data,
490 (unsigned int)buf->size);
491}
492
493#ifdef CONFIG_PM_SLEEP
494static void fw_name_devm_release(struct device *dev, void *res)
495{
496 struct fw_name_devm *fwn = res;
497
498 if (fwn->magic == (unsigned long)&fw_cache)
499 pr_debug("%s: fw_name-%s devm-%p released\n",
500 __func__, fwn->name, res);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700501 kfree_const(fwn->name);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100502}
503
504static int fw_devm_match(struct device *dev, void *res,
505 void *match_data)
506{
507 struct fw_name_devm *fwn = res;
508
509 return (fwn->magic == (unsigned long)&fw_cache) &&
510 !strcmp(fwn->name, match_data);
511}
512
513static struct fw_name_devm *fw_find_devm_name(struct device *dev,
514 const char *name)
515{
516 struct fw_name_devm *fwn;
517
518 fwn = devres_find(dev, fw_name_devm_release,
519 fw_devm_match, (void *)name);
520 return fwn;
521}
522
523/* add firmware name into devres list */
524static int fw_add_devm_name(struct device *dev, const char *name)
525{
526 struct fw_name_devm *fwn;
527
528 fwn = fw_find_devm_name(dev, name);
529 if (fwn)
530 return 1;
531
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700532 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
533 GFP_KERNEL);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100534 if (!fwn)
535 return -ENOMEM;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700536 fwn->name = kstrdup_const(name, GFP_KERNEL);
537 if (!fwn->name) {
Vladimir Zapolskiya885de62015-07-29 23:26:28 +0300538 devres_free(fwn);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700539 return -ENOMEM;
540 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100541
542 fwn->magic = (unsigned long)&fw_cache;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100543 devres_add(dev, fwn);
544
545 return 0;
546}
547#else
548static int fw_add_devm_name(struct device *dev, const char *name)
549{
550 return 0;
551}
552#endif
553
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700554static int assign_firmware_buf(struct firmware *fw, struct device *device,
555 unsigned int opt_flags)
556{
557 struct firmware_buf *buf = fw->priv;
558
559 mutex_lock(&fw_lock);
560 if (!buf->size || fw_state_is_aborted(&buf->fw_st)) {
561 mutex_unlock(&fw_lock);
562 return -ENOENT;
563 }
564
565 /*
566 * add firmware name into devres list so that we can auto cache
567 * and uncache firmware for device.
568 *
569 * device may has been deleted already, but the problem
570 * should be fixed in devres or driver core.
571 */
572 /* don't cache firmware handled without uevent */
573 if (device && (opt_flags & FW_OPT_UEVENT) &&
574 !(opt_flags & FW_OPT_NOCACHE))
575 fw_add_devm_name(device, buf->fw_id);
576
577 /*
578 * After caching firmware image is started, let it piggyback
579 * on request firmware.
580 */
581 if (!(opt_flags & FW_OPT_NOCACHE) &&
582 buf->fwc->state == FW_LOADER_START_CACHE) {
583 if (fw_cache_piggyback_on_request(buf->fw_id))
584 kref_get(&buf->ref);
585 }
586
587 /* pass the pages buffer to driver at the last minute */
588 fw_set_page_data(buf, fw);
589 mutex_unlock(&fw_lock);
590 return 0;
591}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100592
593/*
594 * user-mode helper code
595 */
596#ifdef CONFIG_FW_LOADER_USER_HELPER
597struct firmware_priv {
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100598 bool nowait;
599 struct device dev;
600 struct firmware_buf *buf;
601 struct firmware *fw;
602};
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100603
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700604static struct firmware_priv *to_firmware_priv(struct device *dev)
605{
606 return container_of(dev, struct firmware_priv, dev);
607}
608
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700609static void __fw_load_abort(struct firmware_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Ming Lei87597932013-06-15 16:36:38 +0800611 /*
612 * There is a small window in which user can write to 'loading'
613 * between loading done and disappearance of 'loading'
614 */
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100615 if (fw_state_is_done(&buf->fw_st))
Ming Lei87597932013-06-15 16:36:38 +0800616 return;
617
Takashi Iwaife304142013-05-22 18:28:38 +0200618 list_del_init(&buf->pending_list);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100619 fw_state_aborted(&buf->fw_st);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700622static void fw_load_abort(struct firmware_priv *fw_priv)
623{
624 struct firmware_buf *buf = fw_priv->buf;
625
626 __fw_load_abort(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
Takashi Iwaife304142013-05-22 18:28:38 +0200629static LIST_HEAD(pending_fw_head);
630
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -0700631static void kill_pending_fw_fallback_reqs(bool only_kill_custom)
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700632{
633 struct firmware_buf *buf;
634 struct firmware_buf *next;
635
636 mutex_lock(&fw_lock);
637 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -0700638 if (!buf->need_uevent || !only_kill_custom)
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700639 __fw_load_abort(buf);
640 }
641 mutex_unlock(&fw_lock);
642}
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700643
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700644static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
645 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 return sprintf(buf, "%d\n", loading_timeout);
648}
649
650/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800651 * firmware_timeout_store - set number of seconds to wait for firmware
652 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800653 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800654 * @buf: buffer to scan for timeout value
655 * @count: number of bytes in @buf
656 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800658 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 * firmware will be provided.
660 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800661 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 **/
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700663static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
664 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700667 if (loading_timeout < 0)
668 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return count;
671}
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100672static CLASS_ATTR_RW(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100674static struct attribute *firmware_class_attrs[] = {
675 &class_attr_timeout.attr,
676 NULL,
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800677};
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100678ATTRIBUTE_GROUPS(firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800680static void fw_dev_release(struct device *dev)
681{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700682 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800683
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800684 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800685}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Linus Torvalds6f957722015-07-09 11:20:01 -0700687static int do_firmware_uevent(struct firmware_priv *fw_priv, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Ming Lei12446912012-08-04 12:01:20 +0800689 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200691 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700692 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200693 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
694 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 return 0;
697}
698
Linus Torvalds6f957722015-07-09 11:20:01 -0700699static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
700{
701 struct firmware_priv *fw_priv = to_firmware_priv(dev);
702 int err = 0;
703
704 mutex_lock(&fw_lock);
705 if (fw_priv->buf)
706 err = do_firmware_uevent(fw_priv, env);
707 mutex_unlock(&fw_lock);
708 return err;
709}
710
Adrian Bunk1b81d662006-05-20 15:00:16 -0700711static struct class firmware_class = {
712 .name = "firmware",
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100713 .class_groups = firmware_class_groups,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700714 .dev_uevent = firmware_uevent,
715 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700716};
717
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700718static ssize_t firmware_loading_show(struct device *dev,
719 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700721 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800722 int loading = 0;
723
724 mutex_lock(&fw_lock);
725 if (fw_priv->buf)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100726 loading = fw_state_is_loading(&fw_priv->buf->fw_st);
Ming Lei87597932013-06-15 16:36:38 +0800727 mutex_unlock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return sprintf(buf, "%d\n", loading);
730}
731
David Woodhouse6e03a202009-04-09 22:04:07 -0700732/* Some architectures don't have PAGE_KERNEL_RO */
733#ifndef PAGE_KERNEL_RO
734#define PAGE_KERNEL_RO PAGE_KERNEL
735#endif
Ming Lei253c9242012-10-09 12:01:02 +0800736
737/* one pages buffer should be mapped/unmapped only once */
738static int fw_map_pages_buf(struct firmware_buf *buf)
739{
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100740 if (!buf->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800741 return 0;
742
Markus Elfringdaa3d672014-11-19 11:38:38 +0100743 vunmap(buf->data);
Ming Lei253c9242012-10-09 12:01:02 +0800744 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
745 if (!buf->data)
746 return -ENOMEM;
747 return 0;
748}
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800751 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700752 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800753 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800754 * @buf: buffer to scan for loading control value
755 * @count: number of bytes in @buf
756 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 * The relevant values are:
758 *
759 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800760 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 * -1: Conclude the load with an error and discard any written data.
762 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700763static ssize_t firmware_loading_store(struct device *dev,
764 struct device_attribute *attr,
765 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700767 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800768 struct firmware_buf *fw_buf;
Kees Cook6593d922014-02-25 13:06:00 -0800769 ssize_t written = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700771 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Neil Hormaneea915b2012-01-02 15:31:23 -0500773 mutex_lock(&fw_lock);
Ming Lei87597932013-06-15 16:36:38 +0800774 fw_buf = fw_priv->buf;
Luis R. Rodriguez191e8852017-01-25 10:31:52 -0800775 if (fw_state_is_aborted(&fw_buf->fw_st))
Neil Hormaneea915b2012-01-02 15:31:23 -0500776 goto out;
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 switch (loading) {
779 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800780 /* discarding any previous partial load */
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100781 if (!fw_state_is_done(&fw_buf->fw_st)) {
Ming Lei12446912012-08-04 12:01:20 +0800782 for (i = 0; i < fw_buf->nr_pages; i++)
783 __free_page(fw_buf->pages[i]);
Chen Feng10a3fbf2015-12-16 17:03:15 +0800784 vfree(fw_buf->pages);
Ming Lei12446912012-08-04 12:01:20 +0800785 fw_buf->pages = NULL;
786 fw_buf->page_array_size = 0;
787 fw_buf->nr_pages = 0;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100788 fw_state_start(&fw_buf->fw_st);
Ming Lei28eefa72012-08-04 12:01:17 +0800789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 break;
791 case 0:
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100792 if (fw_state_is_loading(&fw_buf->fw_st)) {
Kees Cook6593d922014-02-25 13:06:00 -0800793 int rc;
794
Ming Lei253c9242012-10-09 12:01:02 +0800795 /*
796 * Several loading requests may be pending on
797 * one same firmware buf, so let all requests
798 * see the mapped 'buf->data' once the loading
799 * is completed.
800 * */
Kees Cook6593d922014-02-25 13:06:00 -0800801 rc = fw_map_pages_buf(fw_buf);
802 if (rc)
zhang jun2b1278c2014-02-13 15:18:47 +0800803 dev_err(dev, "%s: map pages failed\n",
804 __func__);
Kees Cook6593d922014-02-25 13:06:00 -0800805 else
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500806 rc = security_kernel_post_read_file(NULL,
807 fw_buf->data, fw_buf->size,
808 READING_FIRMWARE);
Kees Cook6593d922014-02-25 13:06:00 -0800809
810 /*
811 * Same logic as fw_load_abort, only the DONE bit
812 * is ignored and we set ABORT only on failure.
813 */
Takashi Iwaife304142013-05-22 18:28:38 +0200814 list_del_init(&fw_buf->pending_list);
Kees Cook6593d922014-02-25 13:06:00 -0800815 if (rc) {
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100816 fw_state_aborted(&fw_buf->fw_st);
Kees Cook6593d922014-02-25 13:06:00 -0800817 written = rc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100818 } else {
819 fw_state_done(&fw_buf->fw_st);
Kees Cook6593d922014-02-25 13:06:00 -0800820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 break;
822 }
823 /* fallthrough */
824 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700825 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /* fallthrough */
827 case -1:
828 fw_load_abort(fw_priv);
829 break;
830 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500831out:
832 mutex_unlock(&fw_lock);
Kees Cook6593d922014-02-25 13:06:00 -0800833 return written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700836static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Stephen Boyda098ecd2016-08-02 14:04:28 -0700838static void firmware_rw_buf(struct firmware_buf *buf, char *buffer,
839 loff_t offset, size_t count, bool read)
840{
841 if (read)
842 memcpy(buffer, buf->data + offset, count);
843 else
844 memcpy(buf->data + offset, buffer, count);
845}
846
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700847static void firmware_rw(struct firmware_buf *buf, char *buffer,
848 loff_t offset, size_t count, bool read)
849{
850 while (count) {
851 void *page_data;
852 int page_nr = offset >> PAGE_SHIFT;
853 int page_ofs = offset & (PAGE_SIZE-1);
854 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
855
856 page_data = kmap(buf->pages[page_nr]);
857
858 if (read)
859 memcpy(buffer, page_data + page_ofs, page_cnt);
860 else
861 memcpy(page_data + page_ofs, buffer, page_cnt);
862
863 kunmap(buf->pages[page_nr]);
864 buffer += page_cnt;
865 offset += page_cnt;
866 count -= page_cnt;
867 }
868}
869
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700870static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
871 struct bin_attribute *bin_attr,
872 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200874 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700875 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800876 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700877 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Laura Garciacad1e552006-05-23 23:22:38 +0200879 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800880 buf = fw_priv->buf;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100881 if (!buf || fw_state_is_done(&buf->fw_st)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 ret_count = -ENODEV;
883 goto out;
884 }
Ming Lei12446912012-08-04 12:01:20 +0800885 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200886 ret_count = 0;
887 goto out;
888 }
Ming Lei12446912012-08-04 12:01:20 +0800889 if (count > buf->size - offset)
890 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700891
892 ret_count = count;
893
Stephen Boyda098ecd2016-08-02 14:04:28 -0700894 if (buf->data)
895 firmware_rw_buf(buf, buffer, offset, count, true);
896 else
897 firmware_rw(buf, buffer, offset, count, true);
David Woodhouse6e03a202009-04-09 22:04:07 -0700898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899out:
Laura Garciacad1e552006-05-23 23:22:38 +0200900 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return ret_count;
902}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800903
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700904static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Ming Lei12446912012-08-04 12:01:20 +0800906 struct firmware_buf *buf = fw_priv->buf;
Fabian Fredericka76040d2014-07-01 21:13:30 +0200907 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
David Woodhouse6e03a202009-04-09 22:04:07 -0700909 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800910 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700911 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800912 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700913 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Chen Feng10a3fbf2015-12-16 17:03:15 +0800915 new_pages = vmalloc(new_array_size * sizeof(void *));
David Woodhouse6e03a202009-04-09 22:04:07 -0700916 if (!new_pages) {
917 fw_load_abort(fw_priv);
918 return -ENOMEM;
919 }
Ming Lei12446912012-08-04 12:01:20 +0800920 memcpy(new_pages, buf->pages,
921 buf->page_array_size * sizeof(void *));
922 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
923 (new_array_size - buf->page_array_size));
Chen Feng10a3fbf2015-12-16 17:03:15 +0800924 vfree(buf->pages);
Ming Lei12446912012-08-04 12:01:20 +0800925 buf->pages = new_pages;
926 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700928
Ming Lei12446912012-08-04 12:01:20 +0800929 while (buf->nr_pages < pages_needed) {
930 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700931 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
932
Ming Lei12446912012-08-04 12:01:20 +0800933 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700934 fw_load_abort(fw_priv);
935 return -ENOMEM;
936 }
Ming Lei12446912012-08-04 12:01:20 +0800937 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return 0;
940}
941
942/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800943 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700944 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700945 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700946 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800947 * @buffer: buffer being written
948 * @offset: buffer offset for write in total data store area
949 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800951 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 * the driver as a firmware image.
953 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700954static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
955 struct bin_attribute *bin_attr,
956 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200958 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700959 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800960 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 ssize_t retval;
962
963 if (!capable(CAP_SYS_RAWIO))
964 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700965
Laura Garciacad1e552006-05-23 23:22:38 +0200966 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800967 buf = fw_priv->buf;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100968 if (!buf || fw_state_is_done(&buf->fw_st)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 retval = -ENODEV;
970 goto out;
971 }
Ming Lei65710cb2012-08-04 12:01:16 +0800972
Stephen Boyda098ecd2016-08-02 14:04:28 -0700973 if (buf->data) {
974 if (offset + count > buf->allocated_size) {
975 retval = -ENOMEM;
976 goto out;
977 }
978 firmware_rw_buf(buf, buffer, offset, count, false);
979 retval = count;
980 } else {
981 retval = fw_realloc_buffer(fw_priv, offset + count);
982 if (retval)
983 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Stephen Boyda098ecd2016-08-02 14:04:28 -0700985 retval = count;
986 firmware_rw(buf, buffer, offset, count, false);
987 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700988
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700989 buf->size = max_t(size_t, offset + count, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990out:
Laura Garciacad1e552006-05-23 23:22:38 +0200991 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return retval;
993}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800994
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700995static struct bin_attribute firmware_attr_data = {
996 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 .size = 0,
998 .read = firmware_data_read,
999 .write = firmware_data_write,
1000};
1001
Takashi Iwai46239902015-02-04 15:18:07 +01001002static struct attribute *fw_dev_attrs[] = {
1003 &dev_attr_loading.attr,
1004 NULL
1005};
1006
1007static struct bin_attribute *fw_dev_bin_attrs[] = {
1008 &firmware_attr_data,
1009 NULL
1010};
1011
1012static const struct attribute_group fw_dev_attr_group = {
1013 .attrs = fw_dev_attrs,
1014 .bin_attrs = fw_dev_bin_attrs,
1015};
1016
1017static const struct attribute_group *fw_dev_attr_groups[] = {
1018 &fw_dev_attr_group,
1019 NULL
1020};
1021
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001022static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +02001023fw_create_instance(struct firmware *firmware, const char *fw_name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001024 struct device *device, unsigned int opt_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001026 struct firmware_priv *fw_priv;
1027 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Ming Lei12446912012-08-04 12:01:20 +08001029 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001030 if (!fw_priv) {
Ming Lei12446912012-08-04 12:01:20 +08001031 fw_priv = ERR_PTR(-ENOMEM);
1032 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001035 fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT);
Ming Lei1f2b7952012-08-04 12:01:21 +08001036 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001037 f_dev = &fw_priv->dev;
1038
1039 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +08001040 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -07001041 f_dev->parent = device;
1042 f_dev->class = &firmware_class;
Takashi Iwai46239902015-02-04 15:18:07 +01001043 f_dev->groups = fw_dev_attr_groups;
Ming Lei12446912012-08-04 12:01:20 +08001044exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001045 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001047
1048/* load a firmware via user helper */
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001049static int _request_firmware_load(struct firmware_priv *fw_priv,
1050 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001051{
1052 int retval = 0;
1053 struct device *f_dev = &fw_priv->dev;
1054 struct firmware_buf *buf = fw_priv->buf;
1055
1056 /* fall back on userspace loading */
Stephen Boyda098ecd2016-08-02 14:04:28 -07001057 if (!buf->data)
1058 buf->is_paged_buf = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001059
1060 dev_set_uevent_suppress(f_dev, true);
1061
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001062 retval = device_add(f_dev);
1063 if (retval) {
1064 dev_err(f_dev, "%s: device_register failed\n", __func__);
1065 goto err_put_dev;
1066 }
1067
Maxime Bizon1eeeef12013-08-29 20:28:13 +02001068 mutex_lock(&fw_lock);
1069 list_add(&buf->pending_list, &pending_fw_head);
1070 mutex_unlock(&fw_lock);
1071
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001072 if (opt_flags & FW_OPT_UEVENT) {
Ming Leiaf5bc112013-05-27 18:30:04 +08001073 buf->need_uevent = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001074 dev_set_uevent_suppress(f_dev, false);
1075 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001076 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
Ming Lei68ff2a02015-01-13 00:02:01 +08001077 } else {
1078 timeout = MAX_JIFFY_OFFSET;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001079 }
1080
Bjorn Andersson5d47ec02016-12-06 17:01:45 -08001081 retval = fw_state_wait_timeout(&buf->fw_st, timeout);
1082 if (retval < 0) {
Ming Lei0cb64242015-01-13 00:01:35 +08001083 mutex_lock(&fw_lock);
1084 fw_load_abort(fw_priv);
1085 mutex_unlock(&fw_lock);
1086 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001087
Daniel Wagnerf52cc3792016-11-17 11:00:48 +01001088 if (fw_state_is_aborted(&buf->fw_st))
Shuah Khan0542ad82014-07-22 18:10:38 -06001089 retval = -EAGAIN;
Stephen Boyda098ecd2016-08-02 14:04:28 -07001090 else if (buf->is_paged_buf && !buf->data)
zhang jun2b1278c2014-02-13 15:18:47 +08001091 retval = -ENOMEM;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001092
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001093 device_del(f_dev);
1094err_put_dev:
1095 put_device(f_dev);
1096 return retval;
1097}
1098
1099static int fw_load_from_user_helper(struct firmware *firmware,
1100 const char *name, struct device *device,
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001101 unsigned int opt_flags)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001102{
1103 struct firmware_priv *fw_priv;
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001104 long timeout;
1105 int ret;
1106
1107 timeout = firmware_loading_timeout();
1108 if (opt_flags & FW_OPT_NOWAIT) {
1109 timeout = usermodehelper_read_lock_wait(timeout);
1110 if (!timeout) {
1111 dev_dbg(device, "firmware: %s loading timed out\n",
1112 name);
1113 return -EBUSY;
1114 }
1115 } else {
1116 ret = usermodehelper_read_trylock();
1117 if (WARN_ON(ret)) {
1118 dev_err(device, "firmware: %s will not be loaded\n",
1119 name);
1120 return ret;
1121 }
1122 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001123
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001124 fw_priv = fw_create_instance(firmware, name, device, opt_flags);
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001125 if (IS_ERR(fw_priv)) {
1126 ret = PTR_ERR(fw_priv);
1127 goto out_unlock;
1128 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001129
1130 fw_priv->buf = firmware->priv;
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001131 ret = _request_firmware_load(fw_priv, opt_flags, timeout);
1132
1133 if (!ret)
1134 ret = assign_firmware_buf(firmware, device, opt_flags);
1135
1136out_unlock:
1137 usermodehelper_read_unlock();
1138
1139 return ret;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001140}
Ming Leiddf1f062013-06-04 10:01:13 +08001141
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001142#else /* CONFIG_FW_LOADER_USER_HELPER */
1143static inline int
1144fw_load_from_user_helper(struct firmware *firmware, const char *name,
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001145 struct device *device, unsigned int opt_flags)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001146{
1147 return -ENOENT;
1148}
Takashi Iwai807be032013-01-31 11:13:57 +01001149
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -07001150static inline void kill_pending_fw_fallback_reqs(bool only_kill_custom) { }
Ming Leiddf1f062013-06-04 10:01:13 +08001151
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001152#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001154/* prepare firmware and firmware_buf structs;
1155 * return 0 if a firmware is already assigned, 1 if need to load one,
1156 * or a negative error code
1157 */
1158static int
1159_request_firmware_prepare(struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001160 struct device *device, void *dbuf, size_t size)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001161{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +08001163 struct firmware_buf *buf;
1164 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Jiri Slaby4aed0642005-09-13 01:25:01 -07001166 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -07001168 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1169 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001170 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Stephen Boyda098ecd2016-08-02 14:04:28 -07001173 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
Luis R. Rodriguezed046302015-04-29 16:30:43 -07001174 dev_dbg(device, "using built-in %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001175 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +01001176 }
1177
Stephen Boyda098ecd2016-08-02 14:04:28 -07001178 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +08001179
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001180 /*
1181 * bind with 'buf' now to avoid warning in failure path
1182 * of requesting firmware.
1183 */
1184 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +08001185
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001186 if (ret > 0) {
Daniel Wagner5b029622016-11-17 11:00:50 +01001187 ret = fw_state_wait(&buf->fw_st);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001188 if (!ret) {
1189 fw_set_page_data(buf, firmware);
1190 return 0; /* assigned */
1191 }
Stephen Boyddddb5542012-03-28 23:30:43 +02001192 }
Ming Lei1f2b7952012-08-04 12:01:21 +08001193
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001194 if (ret < 0)
1195 return ret;
1196 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001197}
1198
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001199/*
1200 * Batched requests need only one wake, we need to do this step last due to the
1201 * fallback mechanism. The buf is protected with kref_get(), and it won't be
1202 * released until the last user calls release_firmware().
1203 *
1204 * Failed batched requests are possible as well, in such cases we just share
1205 * the struct firmware_buf and won't release it until all requests are woken
1206 * and have gone through this same path.
1207 */
1208static void fw_abort_batch_reqs(struct firmware *fw)
1209{
1210 struct firmware_buf *buf;
1211
1212 /* Loaded directly? */
1213 if (!fw || !fw->priv)
1214 return;
1215
1216 buf = fw->priv;
1217 if (!fw_state_is_aborted(&buf->fw_st))
1218 fw_state_aborted(&buf->fw_st);
1219}
1220
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001221/* called from request_firmware() and request_firmware_work_func() */
1222static int
1223_request_firmware(const struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001224 struct device *device, void *buf, size_t size,
1225 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001226{
Brian Norris715780a2015-12-09 14:50:28 -08001227 struct firmware *fw = NULL;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001228 int ret;
1229
1230 if (!firmware_p)
1231 return -EINVAL;
1232
Brian Norris715780a2015-12-09 14:50:28 -08001233 if (!name || name[0] == '\0') {
1234 ret = -EINVAL;
1235 goto out;
1236 }
Kees Cook471b0952014-09-18 11:25:37 -07001237
Stephen Boyda098ecd2016-08-02 14:04:28 -07001238 ret = _request_firmware_prepare(&fw, name, device, buf, size);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001239 if (ret <= 0) /* error or already assigned */
1240 goto out;
1241
Luis R. Rodriguez81f95072017-05-02 01:31:05 -07001242 if (!firmware_enabled()) {
1243 WARN(1, "firmware request while host is not available\n");
1244 ret = -EHOSTDOWN;
1245 goto out;
1246 }
1247
Neil Horman3e358ac2013-09-06 15:36:08 -04001248 ret = fw_get_filesystem_firmware(device, fw->priv);
1249 if (ret) {
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001250 if (!(opt_flags & FW_OPT_NO_WARN))
Takashi Iwaibba3a872013-12-02 15:38:16 +01001251 dev_warn(device,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001252 "Direct firmware load for %s failed with error %d\n",
1253 name, ret);
1254 if (opt_flags & FW_OPT_USERHELPER) {
Takashi Iwaibba3a872013-12-02 15:38:16 +01001255 dev_warn(device, "Falling back to user helper\n");
1256 ret = fw_load_from_user_helper(fw, name, device,
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001257 opt_flags);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001258 }
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001259 } else
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001260 ret = assign_firmware_buf(fw, device, opt_flags);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001261
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001262 out:
1263 if (ret < 0) {
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001264 fw_abort_batch_reqs(fw);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001265 release_firmware(fw);
1266 fw = NULL;
1267 }
1268
1269 *firmware_p = fw;
1270 return ret;
1271}
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273/**
Kay Sievers312c0042005-11-16 09:00:00 +01001274 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001275 * @firmware_p: pointer to firmware image
1276 * @name: name of firmware file
1277 * @device: device for which firmware is being loaded
1278 *
1279 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001280 * of @name for device @device.
1281 *
1282 * Should be called from user context where sleeping is allowed.
1283 *
Kay Sievers312c0042005-11-16 09:00:00 +01001284 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001285 * should be distinctive enough not to be confused with any other
1286 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001287 *
1288 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001289 *
1290 * The function can be called safely inside device's suspend and
1291 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001292 **/
1293int
1294request_firmware(const struct firmware **firmware_p, const char *name,
Andrei Opreaea310032015-03-08 12:41:15 +02001295 struct device *device)
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001296{
Ming Leid6c8aa32013-06-06 20:01:48 +08001297 int ret;
1298
1299 /* Need to pin this module until return */
1300 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001301 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001302 FW_OPT_UEVENT | FW_OPT_FALLBACK);
Ming Leid6c8aa32013-06-06 20:01:48 +08001303 module_put(THIS_MODULE);
1304 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001305}
Daniel Mackf4945132013-05-23 22:17:18 +02001306EXPORT_SYMBOL(request_firmware);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001307
Takashi Iwaibba3a872013-12-02 15:38:16 +01001308/**
Borislav Petkov3c1556b2014-12-03 22:46:37 +01001309 * request_firmware_direct: - load firmware directly without usermode helper
Takashi Iwaibba3a872013-12-02 15:38:16 +01001310 * @firmware_p: pointer to firmware image
1311 * @name: name of firmware file
1312 * @device: device for which firmware is being loaded
1313 *
1314 * This function works pretty much like request_firmware(), but this doesn't
1315 * fall back to usermode helper even if the firmware couldn't be loaded
1316 * directly from fs. Hence it's useful for loading optional firmwares, which
1317 * aren't always present, without extra long timeouts of udev.
1318 **/
1319int request_firmware_direct(const struct firmware **firmware_p,
1320 const char *name, struct device *device)
1321{
1322 int ret;
Andrei Opreaea310032015-03-08 12:41:15 +02001323
Takashi Iwaibba3a872013-12-02 15:38:16 +01001324 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001325 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001326 FW_OPT_UEVENT | FW_OPT_NO_WARN);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001327 module_put(THIS_MODULE);
1328 return ret;
1329}
1330EXPORT_SYMBOL_GPL(request_firmware_direct);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001331
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001332/**
Stephen Boyda098ecd2016-08-02 14:04:28 -07001333 * request_firmware_into_buf - load firmware into a previously allocated buffer
1334 * @firmware_p: pointer to firmware image
1335 * @name: name of firmware file
1336 * @device: device for which firmware is being loaded and DMA region allocated
1337 * @buf: address of buffer to load firmware into
1338 * @size: size of buffer
1339 *
1340 * This function works pretty much like request_firmware(), but it doesn't
1341 * allocate a buffer to hold the firmware data. Instead, the firmware
1342 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
1343 * data member is pointed at @buf.
1344 *
1345 * This function doesn't cache firmware either.
1346 */
1347int
1348request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
1349 struct device *device, void *buf, size_t size)
1350{
1351 int ret;
1352
1353 __module_get(THIS_MODULE);
1354 ret = _request_firmware(firmware_p, name, device, buf, size,
1355 FW_OPT_UEVENT | FW_OPT_FALLBACK |
1356 FW_OPT_NOCACHE);
1357 module_put(THIS_MODULE);
1358 return ret;
1359}
1360EXPORT_SYMBOL(request_firmware_into_buf);
1361
1362/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001364 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001366void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
1368 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001369 if (!fw_is_builtin_firmware(fw))
1370 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 kfree(fw);
1372 }
1373}
Daniel Mackf4945132013-05-23 22:17:18 +02001374EXPORT_SYMBOL(release_firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376/* Async support */
1377struct firmware_work {
1378 struct work_struct work;
1379 struct module *module;
1380 const char *name;
1381 struct device *device;
1382 void *context;
1383 void (*cont)(const struct firmware *fw, void *context);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001384 unsigned int opt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385};
1386
Stephen Boyda36cf842012-03-28 23:31:00 +02001387static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Stephen Boyda36cf842012-03-28 23:31:00 +02001389 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001391
Stephen Boyda36cf842012-03-28 23:31:00 +02001392 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001393
Stephen Boyda098ecd2016-08-02 14:04:28 -07001394 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001395 fw_work->opt_flags);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001396 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001397 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 module_put(fw_work->module);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001400 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
1403
1404/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001405 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001406 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001407 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001408 * is non-zero else the firmware copy must be done manually.
1409 * @name: name of firmware file
1410 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001411 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001412 * @context: will be passed over to @cont, and
1413 * @fw may be %NULL if firmware request fails.
1414 * @cont: function will be called asynchronously when the firmware
1415 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001417 * Caller must hold the reference count of @device.
1418 *
Ming Lei6f21a622012-08-04 12:01:24 +08001419 * Asynchronous variant of request_firmware() for user contexts:
1420 * - sleep for as small periods as possible since it may
Silvio Fricke88bcef52016-11-25 15:59:47 +01001421 * increase kernel boot time of built-in device drivers
1422 * requesting firmware in their ->probe() methods, if
1423 * @gfp is GFP_KERNEL.
Ming Lei6f21a622012-08-04 12:01:24 +08001424 *
1425 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 **/
1427int
1428request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001429 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001430 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 void (*cont)(const struct firmware *fw, void *context))
1432{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001433 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Andrei Opreaea310032015-03-08 12:41:15 +02001435 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (!fw_work)
1437 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001438
1439 fw_work->module = module;
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001440 fw_work->name = kstrdup_const(name, gfp);
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001441 if (!fw_work->name) {
1442 kfree(fw_work);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001443 return -ENOMEM;
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001444 }
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001445 fw_work->device = device;
1446 fw_work->context = context;
1447 fw_work->cont = cont;
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001448 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
Takashi Iwai5a1379e2014-06-04 17:48:15 +02001449 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (!try_module_get(module)) {
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001452 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 kfree(fw_work);
1454 return -EFAULT;
1455 }
1456
Ming Lei0cfc1e12012-08-04 12:01:23 +08001457 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001458 INIT_WORK(&fw_work->work, request_firmware_work_func);
1459 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return 0;
1461}
Daniel Mackf4945132013-05-23 22:17:18 +02001462EXPORT_SYMBOL(request_firmware_nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Ming Lei90f890812013-06-20 12:30:16 +08001464#ifdef CONFIG_PM_SLEEP
1465static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1466
Ming Lei2887b392012-08-04 12:01:22 +08001467/**
1468 * cache_firmware - cache one firmware image in kernel memory space
1469 * @fw_name: the firmware image name
1470 *
1471 * Cache firmware in kernel memory so that drivers can use it when
1472 * system isn't ready for them to request firmware image from userspace.
1473 * Once it returns successfully, driver can use request_firmware or its
1474 * nowait version to get the cached firmware without any interacting
1475 * with userspace
1476 *
1477 * Return 0 if the firmware image has been cached successfully
1478 * Return !0 otherwise
1479 *
1480 */
Ming Lei93232e42013-06-06 20:01:47 +08001481static int cache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001482{
1483 int ret;
1484 const struct firmware *fw;
1485
1486 pr_debug("%s: %s\n", __func__, fw_name);
1487
1488 ret = request_firmware(&fw, fw_name, NULL);
1489 if (!ret)
1490 kfree(fw);
1491
1492 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1493
1494 return ret;
1495}
1496
Ming Lei6a2c1232013-06-26 09:28:17 +08001497static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1498{
1499 struct firmware_buf *tmp;
1500 struct firmware_cache *fwc = &fw_cache;
1501
1502 spin_lock(&fwc->lock);
1503 tmp = __fw_lookup_buf(fw_name);
1504 spin_unlock(&fwc->lock);
1505
1506 return tmp;
1507}
1508
Ming Lei2887b392012-08-04 12:01:22 +08001509/**
1510 * uncache_firmware - remove one cached firmware image
1511 * @fw_name: the firmware image name
1512 *
1513 * Uncache one firmware image which has been cached successfully
1514 * before.
1515 *
1516 * Return 0 if the firmware cache has been removed successfully
1517 * Return !0 otherwise
1518 *
1519 */
Ming Lei93232e42013-06-06 20:01:47 +08001520static int uncache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001521{
1522 struct firmware_buf *buf;
1523 struct firmware fw;
1524
1525 pr_debug("%s: %s\n", __func__, fw_name);
1526
Stephen Boyda098ecd2016-08-02 14:04:28 -07001527 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
Ming Lei2887b392012-08-04 12:01:22 +08001528 return 0;
1529
1530 buf = fw_lookup_buf(fw_name);
1531 if (buf) {
1532 fw_free_buf(buf);
1533 return 0;
1534 }
1535
1536 return -EINVAL;
1537}
1538
Ming Lei37276a52012-08-04 12:01:27 +08001539static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1540{
1541 struct fw_cache_entry *fce;
1542
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001543 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
Ming Lei37276a52012-08-04 12:01:27 +08001544 if (!fce)
1545 goto exit;
1546
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001547 fce->name = kstrdup_const(name, GFP_ATOMIC);
1548 if (!fce->name) {
1549 kfree(fce);
1550 fce = NULL;
1551 goto exit;
1552 }
Ming Lei37276a52012-08-04 12:01:27 +08001553exit:
1554 return fce;
1555}
1556
Ming Lei373304f2012-10-09 12:01:01 +08001557static int __fw_entry_found(const char *name)
1558{
1559 struct firmware_cache *fwc = &fw_cache;
1560 struct fw_cache_entry *fce;
1561
1562 list_for_each_entry(fce, &fwc->fw_names, list) {
1563 if (!strcmp(fce->name, name))
1564 return 1;
1565 }
1566 return 0;
1567}
1568
Ming Leiac39b3e2012-08-20 19:04:16 +08001569static int fw_cache_piggyback_on_request(const char *name)
1570{
1571 struct firmware_cache *fwc = &fw_cache;
1572 struct fw_cache_entry *fce;
1573 int ret = 0;
1574
1575 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001576 if (__fw_entry_found(name))
1577 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001578
1579 fce = alloc_fw_cache_entry(name);
1580 if (fce) {
1581 ret = 1;
1582 list_add(&fce->list, &fwc->fw_names);
1583 pr_debug("%s: fw: %s\n", __func__, name);
1584 }
1585found:
1586 spin_unlock(&fwc->name_lock);
1587 return ret;
1588}
1589
Ming Lei37276a52012-08-04 12:01:27 +08001590static void free_fw_cache_entry(struct fw_cache_entry *fce)
1591{
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001592 kfree_const(fce->name);
Ming Lei37276a52012-08-04 12:01:27 +08001593 kfree(fce);
1594}
1595
1596static void __async_dev_cache_fw_image(void *fw_entry,
1597 async_cookie_t cookie)
1598{
1599 struct fw_cache_entry *fce = fw_entry;
1600 struct firmware_cache *fwc = &fw_cache;
1601 int ret;
1602
1603 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001604 if (ret) {
1605 spin_lock(&fwc->name_lock);
1606 list_del(&fce->list);
1607 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001608
Ming Leiac39b3e2012-08-20 19:04:16 +08001609 free_fw_cache_entry(fce);
1610 }
Ming Lei37276a52012-08-04 12:01:27 +08001611}
1612
1613/* called with dev->devres_lock held */
1614static void dev_create_fw_entry(struct device *dev, void *res,
1615 void *data)
1616{
1617 struct fw_name_devm *fwn = res;
1618 const char *fw_name = fwn->name;
1619 struct list_head *head = data;
1620 struct fw_cache_entry *fce;
1621
1622 fce = alloc_fw_cache_entry(fw_name);
1623 if (fce)
1624 list_add(&fce->list, head);
1625}
1626
1627static int devm_name_match(struct device *dev, void *res,
1628 void *match_data)
1629{
1630 struct fw_name_devm *fwn = res;
1631 return (fwn->magic == (unsigned long)match_data);
1632}
1633
Ming Leiab6dd8e2012-08-17 22:07:00 +08001634static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001635{
1636 LIST_HEAD(todo);
1637 struct fw_cache_entry *fce;
1638 struct fw_cache_entry *fce_next;
1639 struct firmware_cache *fwc = &fw_cache;
1640
1641 devres_for_each_res(dev, fw_name_devm_release,
1642 devm_name_match, &fw_cache,
1643 dev_create_fw_entry, &todo);
1644
1645 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1646 list_del(&fce->list);
1647
1648 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001649 /* only one cache entry for one firmware */
1650 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001651 list_add(&fce->list, &fwc->fw_names);
1652 } else {
1653 free_fw_cache_entry(fce);
1654 fce = NULL;
1655 }
Ming Lei37276a52012-08-04 12:01:27 +08001656 spin_unlock(&fwc->name_lock);
1657
Ming Lei373304f2012-10-09 12:01:01 +08001658 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001659 async_schedule_domain(__async_dev_cache_fw_image,
1660 (void *)fce,
1661 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001662 }
1663}
1664
1665static void __device_uncache_fw_images(void)
1666{
1667 struct firmware_cache *fwc = &fw_cache;
1668 struct fw_cache_entry *fce;
1669
1670 spin_lock(&fwc->name_lock);
1671 while (!list_empty(&fwc->fw_names)) {
1672 fce = list_entry(fwc->fw_names.next,
1673 struct fw_cache_entry, list);
1674 list_del(&fce->list);
1675 spin_unlock(&fwc->name_lock);
1676
1677 uncache_firmware(fce->name);
1678 free_fw_cache_entry(fce);
1679
1680 spin_lock(&fwc->name_lock);
1681 }
1682 spin_unlock(&fwc->name_lock);
1683}
1684
1685/**
1686 * device_cache_fw_images - cache devices' firmware
1687 *
1688 * If one device called request_firmware or its nowait version
1689 * successfully before, the firmware names are recored into the
1690 * device's devres link list, so device_cache_fw_images can call
1691 * cache_firmware() to cache these firmwares for the device,
1692 * then the device driver can load its firmwares easily at
1693 * time when system is not ready to complete loading firmware.
1694 */
1695static void device_cache_fw_images(void)
1696{
1697 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001698 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001699 DEFINE_WAIT(wait);
1700
1701 pr_debug("%s\n", __func__);
1702
Ming Lei373304f2012-10-09 12:01:01 +08001703 /* cancel uncache work */
1704 cancel_delayed_work_sync(&fwc->work);
1705
Ming Leiffe53f62012-08-04 12:01:28 +08001706 /*
1707 * use small loading timeout for caching devices' firmware
1708 * because all these firmware images have been loaded
1709 * successfully at lease once, also system is ready for
1710 * completing firmware loading now. The maximum size of
1711 * firmware in current distributions is about 2M bytes,
1712 * so 10 secs should be enough.
1713 */
1714 old_timeout = loading_timeout;
1715 loading_timeout = 10;
1716
Ming Leiac39b3e2012-08-20 19:04:16 +08001717 mutex_lock(&fw_lock);
1718 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001719 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001720 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001721
1722 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001723 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001724
1725 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001726}
1727
1728/**
1729 * device_uncache_fw_images - uncache devices' firmware
1730 *
1731 * uncache all firmwares which have been cached successfully
1732 * by device_uncache_fw_images earlier
1733 */
1734static void device_uncache_fw_images(void)
1735{
1736 pr_debug("%s\n", __func__);
1737 __device_uncache_fw_images();
1738}
1739
1740static void device_uncache_fw_images_work(struct work_struct *work)
1741{
1742 device_uncache_fw_images();
1743}
1744
1745/**
1746 * device_uncache_fw_images_delay - uncache devices firmwares
1747 * @delay: number of milliseconds to delay uncache device firmwares
1748 *
1749 * uncache all devices's firmwares which has been cached successfully
1750 * by device_cache_fw_images after @delay milliseconds.
1751 */
1752static void device_uncache_fw_images_delay(unsigned long delay)
1753{
Shaibal Duttabce66182014-01-31 15:44:58 -08001754 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1755 msecs_to_jiffies(delay));
Ming Lei37276a52012-08-04 12:01:27 +08001756}
1757
Luis R. Rodriguez81f95072017-05-02 01:31:05 -07001758/**
1759 * fw_pm_notify - notifier for suspend/resume
1760 * @notify_block: unused
1761 * @mode: mode we are switching to
1762 * @unused: unused
1763 *
1764 * Used to modify the firmware_class state as we move in between states.
1765 * The firmware_class implements a firmware cache to enable device driver
1766 * to fetch firmware upon resume before the root filesystem is ready. We
1767 * disable API calls which do not use the built-in firmware or the firmware
1768 * cache when we know these calls will not work.
1769 *
1770 * The inner logic behind all this is a bit complex so it is worth summarizing
1771 * the kernel's own suspend/resume process with context and focus on how this
1772 * can impact the firmware API.
1773 *
1774 * First a review on how we go to suspend::
1775 *
1776 * pm_suspend() --> enter_state() -->
1777 * sys_sync()
1778 * suspend_prepare() -->
1779 * __pm_notifier_call_chain(PM_SUSPEND_PREPARE, ...);
1780 * suspend_freeze_processes() -->
1781 * freeze_processes() -->
1782 * __usermodehelper_set_disable_depth(UMH_DISABLED);
1783 * freeze all tasks ...
1784 * freeze_kernel_threads()
1785 * suspend_devices_and_enter() -->
1786 * dpm_suspend_start() -->
1787 * dpm_prepare()
1788 * dpm_suspend()
1789 * suspend_enter() -->
1790 * platform_suspend_prepare()
1791 * dpm_suspend_late()
1792 * freeze_enter()
1793 * syscore_suspend()
1794 *
1795 * When we resume we bail out of a loop from suspend_devices_and_enter() and
1796 * unwind back out to the caller enter_state() where we were before as follows::
1797 *
1798 * enter_state() -->
1799 * suspend_devices_and_enter() --> (bail from loop)
1800 * dpm_resume_end() -->
1801 * dpm_resume()
1802 * dpm_complete()
1803 * suspend_finish() -->
1804 * suspend_thaw_processes() -->
1805 * thaw_processes() -->
1806 * __usermodehelper_set_disable_depth(UMH_FREEZING);
1807 * thaw_workqueues();
1808 * thaw all processes ...
1809 * usermodehelper_enable();
1810 * pm_notifier_call_chain(PM_POST_SUSPEND);
1811 *
1812 * fw_pm_notify() works through pm_notifier_call_chain().
1813 */
Ming Lei07646d92012-08-04 12:01:29 +08001814static int fw_pm_notify(struct notifier_block *notify_block,
1815 unsigned long mode, void *unused)
1816{
1817 switch (mode) {
1818 case PM_HIBERNATION_PREPARE:
1819 case PM_SUSPEND_PREPARE:
Sebastian Capellaf8d5b9e2014-02-18 17:52:08 -08001820 case PM_RESTORE_PREPARE:
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -07001821 /*
1822 * kill pending fallback requests with a custom fallback
1823 * to avoid stalling suspend.
1824 */
1825 kill_pending_fw_fallback_reqs(true);
Ming Lei07646d92012-08-04 12:01:29 +08001826 device_cache_fw_images();
Luis R. Rodriguez81f95072017-05-02 01:31:05 -07001827 disable_firmware();
Ming Lei07646d92012-08-04 12:01:29 +08001828 break;
1829
1830 case PM_POST_SUSPEND:
1831 case PM_POST_HIBERNATION:
1832 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001833 /*
1834 * In case that system sleep failed and syscore_suspend is
1835 * not called.
1836 */
1837 mutex_lock(&fw_lock);
1838 fw_cache.state = FW_LOADER_NO_CACHE;
1839 mutex_unlock(&fw_lock);
Luis R. Rodriguez81f95072017-05-02 01:31:05 -07001840 enable_firmware();
Ming Leiac39b3e2012-08-20 19:04:16 +08001841
Ming Lei07646d92012-08-04 12:01:29 +08001842 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1843 break;
1844 }
1845
1846 return 0;
1847}
Ming Lei07646d92012-08-04 12:01:29 +08001848
Ming Leiac39b3e2012-08-20 19:04:16 +08001849/* stop caching firmware once syscore_suspend is reached */
1850static int fw_suspend(void)
1851{
1852 fw_cache.state = FW_LOADER_NO_CACHE;
1853 return 0;
1854}
1855
1856static struct syscore_ops fw_syscore_ops = {
1857 .suspend = fw_suspend,
1858};
Ming Leicfe016b2012-09-08 17:32:30 +08001859#else
1860static int fw_cache_piggyback_on_request(const char *name)
1861{
1862 return 0;
1863}
1864#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001865
Ming Lei37276a52012-08-04 12:01:27 +08001866static void __init fw_cache_init(void)
1867{
1868 spin_lock_init(&fw_cache.lock);
1869 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001870 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001871
Ming Leicfe016b2012-09-08 17:32:30 +08001872#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001873 spin_lock_init(&fw_cache.name_lock);
1874 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001875
Ming Lei37276a52012-08-04 12:01:27 +08001876 INIT_DELAYED_WORK(&fw_cache.work,
1877 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001878
1879 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1880 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001881
1882 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001883#endif
Ming Lei37276a52012-08-04 12:01:27 +08001884}
1885
Luis R. Rodrigueza669f042017-05-02 01:31:04 -07001886static int fw_shutdown_notify(struct notifier_block *unused1,
1887 unsigned long unused2, void *unused3)
1888{
Luis R. Rodriguez81f95072017-05-02 01:31:05 -07001889 disable_firmware();
Luis R. Rodrigueza669f042017-05-02 01:31:04 -07001890 /*
1891 * Kill all pending fallback requests to avoid both stalling shutdown,
1892 * and avoid a deadlock with the usermode_lock.
1893 */
1894 kill_pending_fw_fallback_reqs(false);
1895
1896 return NOTIFY_DONE;
1897}
1898
1899static struct notifier_block fw_shutdown_nb = {
1900 .notifier_call = fw_shutdown_notify,
1901};
1902
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001903static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
Luis R. Rodriguez81f95072017-05-02 01:31:05 -07001905 enable_firmware();
Ming Lei1f2b7952012-08-04 12:01:21 +08001906 fw_cache_init();
Takashi Iwaife304142013-05-22 18:28:38 +02001907 register_reboot_notifier(&fw_shutdown_nb);
Luis R. Rodrigueza669f042017-05-02 01:31:04 -07001908#ifdef CONFIG_FW_LOADER_USER_HELPER
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001909 return class_register(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001910#else
1911 return 0;
1912#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001914
1915static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Luis R. Rodriguez81f95072017-05-02 01:31:05 -07001917 disable_firmware();
Ming Leicfe016b2012-09-08 17:32:30 +08001918#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001919 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001920 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001921#endif
Takashi Iwaife304142013-05-22 18:28:38 +02001922 unregister_reboot_notifier(&fw_shutdown_nb);
Luis R. Rodrigueza669f042017-05-02 01:31:04 -07001923#ifdef CONFIG_FW_LOADER_USER_HELPER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 class_unregister(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001925#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
Shaohua Lia30a6a22006-09-27 01:50:52 -07001928fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929module_exit(firmware_class_exit);