blob: 76f1b702bdd627ccc54fb2343405700a1719b261 [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. Rodrigueze44565f2017-07-20 13:13:09 -0700133 ret = wait_for_completion_interruptible_timeout(&fw_st->completion,
134 timeout);
Daniel Wagner5b029622016-11-17 11:00:50 +0100135 if (ret != 0 && fw_st->status == FW_STATUS_ABORTED)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100136 return -ENOENT;
Bjorn Andersson5d47ec02016-12-06 17:01:45 -0800137 if (!ret)
138 return -ETIMEDOUT;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100139
Bjorn Andersson5d47ec02016-12-06 17:01:45 -0800140 return ret < 0 ? ret : 0;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100141}
142
143static void __fw_state_set(struct fw_state *fw_st,
144 enum fw_status status)
145{
Daniel Wagner0430caf2016-11-17 11:00:49 +0100146 WRITE_ONCE(fw_st->status, status);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100147
Daniel Wagner0430caf2016-11-17 11:00:49 +0100148 if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED)
Luis R. Rodrigueze44565f2017-07-20 13:13:09 -0700149 complete_all(&fw_st->completion);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100150}
151
152#define fw_state_start(fw_st) \
153 __fw_state_set(fw_st, FW_STATUS_LOADING)
154#define fw_state_done(fw_st) \
155 __fw_state_set(fw_st, FW_STATUS_DONE)
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -0700156#define fw_state_aborted(fw_st) \
157 __fw_state_set(fw_st, FW_STATUS_ABORTED)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100158#define fw_state_wait(fw_st) \
159 __fw_state_wait_common(fw_st, MAX_SCHEDULE_TIMEOUT)
160
Daniel Wagnerfab82cb2016-11-17 11:00:51 +0100161static int __fw_state_check(struct fw_state *fw_st, enum fw_status status)
162{
163 return fw_st->status == status;
164}
165
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -0700166#define fw_state_is_aborted(fw_st) \
167 __fw_state_check(fw_st, FW_STATUS_ABORTED)
168
169#ifdef CONFIG_FW_LOADER_USER_HELPER
170
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100171#define fw_state_aborted(fw_st) \
172 __fw_state_set(fw_st, FW_STATUS_ABORTED)
Daniel Wagnerfab82cb2016-11-17 11:00:51 +0100173#define fw_state_is_done(fw_st) \
174 __fw_state_check(fw_st, FW_STATUS_DONE)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100175#define fw_state_is_loading(fw_st) \
176 __fw_state_check(fw_st, FW_STATUS_LOADING)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100177#define fw_state_wait_timeout(fw_st, timeout) \
178 __fw_state_wait_common(fw_st, timeout)
179
180#endif /* CONFIG_FW_LOADER_USER_HELPER */
181
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100182/* firmware behavior options */
183#define FW_OPT_UEVENT (1U << 0)
184#define FW_OPT_NOWAIT (1U << 1)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100185#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200186#define FW_OPT_USERHELPER (1U << 2)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100187#else
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200188#define FW_OPT_USERHELPER 0
189#endif
190#ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK
191#define FW_OPT_FALLBACK FW_OPT_USERHELPER
192#else
193#define FW_OPT_FALLBACK 0
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100194#endif
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -0700195#define FW_OPT_NO_WARN (1U << 3)
Vikram Mulukutla0e742e92016-08-02 14:04:25 -0700196#define FW_OPT_NOCACHE (1U << 4)
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100197
Ming Lei1f2b7952012-08-04 12:01:21 +0800198struct firmware_cache {
199 /* firmware_buf instance will be added into the below list */
200 spinlock_t lock;
201 struct list_head head;
Ming Leicfe016b2012-09-08 17:32:30 +0800202 int state;
Ming Lei37276a52012-08-04 12:01:27 +0800203
Ming Leicfe016b2012-09-08 17:32:30 +0800204#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +0800205 /*
206 * Names of firmware images which have been cached successfully
207 * will be added into the below list so that device uncache
208 * helper can trace which firmware images have been cached
209 * before.
210 */
211 spinlock_t name_lock;
212 struct list_head fw_names;
213
Ming Lei37276a52012-08-04 12:01:27 +0800214 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +0800215
216 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +0800217#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800218};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Ming Lei12446912012-08-04 12:01:20 +0800220struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800221 struct kref ref;
222 struct list_head list;
Ming Lei1f2b7952012-08-04 12:01:21 +0800223 struct firmware_cache *fwc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100224 struct fw_state fw_st;
Ming Lei65710cb2012-08-04 12:01:16 +0800225 void *data;
226 size_t size;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700227 size_t allocated_size;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100228#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100229 bool is_paged_buf;
Ming Leiaf5bc112013-05-27 18:30:04 +0800230 bool need_uevent;
David Woodhouse6e03a202009-04-09 22:04:07 -0700231 struct page **pages;
232 int nr_pages;
233 int page_array_size;
Takashi Iwaife304142013-05-22 18:28:38 +0200234 struct list_head pending_list;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100235#endif
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700236 const char *fw_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237};
238
Ming Lei37276a52012-08-04 12:01:27 +0800239struct fw_cache_entry {
240 struct list_head list;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700241 const char *name;
Ming Lei37276a52012-08-04 12:01:27 +0800242};
243
Ming Leif531f05a2012-08-04 12:01:25 +0800244struct fw_name_devm {
245 unsigned long magic;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700246 const char *name;
Ming Leif531f05a2012-08-04 12:01:25 +0800247};
248
Ming Lei1f2b7952012-08-04 12:01:21 +0800249#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
250
Ming Leiac39b3e2012-08-20 19:04:16 +0800251#define FW_LOADER_NO_CACHE 0
252#define FW_LOADER_START_CACHE 1
253
254static int fw_cache_piggyback_on_request(const char *name);
255
Ming Lei1f2b7952012-08-04 12:01:21 +0800256/* fw_lock could be moved to 'struct firmware_priv' but since it is just
257 * guarding for corner cases a global lock should be OK */
258static DEFINE_MUTEX(fw_lock);
259
Luis R. Rodriguez81f95072017-05-02 01:31:05 -0700260static bool __enable_firmware = false;
261
262static void enable_firmware(void)
263{
264 mutex_lock(&fw_lock);
265 __enable_firmware = true;
266 mutex_unlock(&fw_lock);
267}
268
269static void disable_firmware(void)
270{
271 mutex_lock(&fw_lock);
272 __enable_firmware = false;
273 mutex_unlock(&fw_lock);
274}
275
276/*
277 * When disabled only the built-in firmware and the firmware cache will be
278 * used to look for firmware.
279 */
280static bool firmware_enabled(void)
281{
282 bool enabled = false;
283
284 mutex_lock(&fw_lock);
285 if (__enable_firmware)
286 enabled = true;
287 mutex_unlock(&fw_lock);
288
289 return enabled;
290}
291
Ming Lei1f2b7952012-08-04 12:01:21 +0800292static struct firmware_cache fw_cache;
293
294static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
Stephen Boyda098ecd2016-08-02 14:04:28 -0700295 struct firmware_cache *fwc,
296 void *dbuf, size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800297{
298 struct firmware_buf *buf;
299
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700300 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
Ming Lei1f2b7952012-08-04 12:01:21 +0800301 if (!buf)
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700302 return NULL;
303
304 buf->fw_id = kstrdup_const(fw_name, GFP_ATOMIC);
305 if (!buf->fw_id) {
306 kfree(buf);
307 return NULL;
308 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800309
310 kref_init(&buf->ref);
Ming Lei1f2b7952012-08-04 12:01:21 +0800311 buf->fwc = fwc;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700312 buf->data = dbuf;
313 buf->allocated_size = size;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100314 fw_state_init(&buf->fw_st);
Takashi Iwaife304142013-05-22 18:28:38 +0200315#ifdef CONFIG_FW_LOADER_USER_HELPER
316 INIT_LIST_HEAD(&buf->pending_list);
317#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800318
319 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
320
321 return buf;
322}
323
Ming Lei2887b392012-08-04 12:01:22 +0800324static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
325{
326 struct firmware_buf *tmp;
327 struct firmware_cache *fwc = &fw_cache;
328
329 list_for_each_entry(tmp, &fwc->head, list)
330 if (!strcmp(tmp->fw_id, fw_name))
331 return tmp;
332 return NULL;
333}
334
Ming Lei1f2b7952012-08-04 12:01:21 +0800335static int fw_lookup_and_allocate_buf(const char *fw_name,
336 struct firmware_cache *fwc,
Stephen Boyda098ecd2016-08-02 14:04:28 -0700337 struct firmware_buf **buf, void *dbuf,
338 size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800339{
340 struct firmware_buf *tmp;
341
342 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800343 tmp = __fw_lookup_buf(fw_name);
344 if (tmp) {
345 kref_get(&tmp->ref);
346 spin_unlock(&fwc->lock);
347 *buf = tmp;
348 return 1;
349 }
Stephen Boyda098ecd2016-08-02 14:04:28 -0700350 tmp = __allocate_fw_buf(fw_name, fwc, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +0800351 if (tmp)
352 list_add(&tmp->list, &fwc->head);
353 spin_unlock(&fwc->lock);
354
355 *buf = tmp;
356
357 return tmp ? 0 : -ENOMEM;
358}
359
360static void __fw_free_buf(struct kref *ref)
Bart Van Assche98233b22014-01-04 14:20:36 +0100361 __releases(&fwc->lock)
Ming Lei1f2b7952012-08-04 12:01:21 +0800362{
363 struct firmware_buf *buf = to_fwbuf(ref);
364 struct firmware_cache *fwc = buf->fwc;
Ming Lei1f2b7952012-08-04 12:01:21 +0800365
366 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
367 __func__, buf->fw_id, buf, buf->data,
368 (unsigned int)buf->size);
369
Ming Lei1f2b7952012-08-04 12:01:21 +0800370 list_del(&buf->list);
371 spin_unlock(&fwc->lock);
372
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100373#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100374 if (buf->is_paged_buf) {
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100375 int i;
Ming Lei746058f2012-10-09 12:01:03 +0800376 vunmap(buf->data);
377 for (i = 0; i < buf->nr_pages; i++)
378 __free_page(buf->pages[i]);
Chen Feng10a3fbf2015-12-16 17:03:15 +0800379 vfree(buf->pages);
Ming Lei746058f2012-10-09 12:01:03 +0800380 } else
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100381#endif
Stephen Boyda098ecd2016-08-02 14:04:28 -0700382 if (!buf->allocated_size)
Ming Lei746058f2012-10-09 12:01:03 +0800383 vfree(buf->data);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700384 kfree_const(buf->fw_id);
Ming Lei1f2b7952012-08-04 12:01:21 +0800385 kfree(buf);
386}
387
388static void fw_free_buf(struct firmware_buf *buf)
389{
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800390 struct firmware_cache *fwc = buf->fwc;
391 spin_lock(&fwc->lock);
392 if (!kref_put(&buf->ref, __fw_free_buf))
393 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800394}
395
Ming Lei746058f2012-10-09 12:01:03 +0800396/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800397static char fw_path_para[256];
398static const char * const fw_path[] = {
399 fw_path_para,
Ming Lei746058f2012-10-09 12:01:03 +0800400 "/lib/firmware/updates/" UTS_RELEASE,
401 "/lib/firmware/updates",
402 "/lib/firmware/" UTS_RELEASE,
403 "/lib/firmware"
404};
405
Ming Lei27602842012-11-03 17:47:58 +0800406/*
407 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
408 * from kernel command line because firmware_class is generally built in
409 * kernel instead of module.
410 */
411module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
412MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
413
Stephen Boyda098ecd2016-08-02 14:04:28 -0700414static int
415fw_get_filesystem_firmware(struct device *device, struct firmware_buf *buf)
Ming Lei746058f2012-10-09 12:01:03 +0800416{
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500417 loff_t size;
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700418 int i, len;
Neil Horman3e358ac2013-09-06 15:36:08 -0400419 int rc = -ENOENT;
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700420 char *path;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700421 enum kernel_read_file_id id = READING_FIRMWARE;
422 size_t msize = INT_MAX;
423
424 /* Already populated data member means we're loading into a buffer */
425 if (buf->data) {
426 id = READING_FIRMWARE_PREALLOC_BUFFER;
427 msize = buf->allocated_size;
428 }
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700429
430 path = __getname();
431 if (!path)
432 return -ENOMEM;
Ming Lei746058f2012-10-09 12:01:03 +0800433
434 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
Ming Lei27602842012-11-03 17:47:58 +0800435 /* skip the unset customized path */
436 if (!fw_path[i][0])
437 continue;
438
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700439 len = snprintf(path, PATH_MAX, "%s/%s",
440 fw_path[i], buf->fw_id);
441 if (len >= PATH_MAX) {
442 rc = -ENAMETOOLONG;
443 break;
444 }
Ming Lei746058f2012-10-09 12:01:03 +0800445
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500446 buf->size = 0;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700447 rc = kernel_read_file_from_path(path, &buf->data, &size, msize,
448 id);
Kees Cook4b2530d2016-02-04 13:15:02 -0800449 if (rc) {
Luis R. Rodriguez8e516aa2016-02-28 21:57:55 +0100450 if (rc == -ENOENT)
451 dev_dbg(device, "loading %s failed with error %d\n",
452 path, rc);
453 else
454 dev_warn(device, "loading %s failed with error %d\n",
455 path, rc);
Kees Cook4b2530d2016-02-04 13:15:02 -0800456 continue;
457 }
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500458 dev_dbg(device, "direct-loading %s\n", buf->fw_id);
459 buf->size = size;
Daniel Wagner5b029622016-11-17 11:00:50 +0100460 fw_state_done(&buf->fw_st);
Kees Cook4b2530d2016-02-04 13:15:02 -0800461 break;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100462 }
Kees Cook4b2530d2016-02-04 13:15:02 -0800463 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100464
Neil Horman3e358ac2013-09-06 15:36:08 -0400465 return rc;
Ming Lei746058f2012-10-09 12:01:03 +0800466}
467
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100468/* firmware holds the ownership of pages */
469static void firmware_free_data(const struct firmware *fw)
470{
471 /* Loaded directly? */
472 if (!fw->priv) {
473 vfree(fw->data);
474 return;
475 }
476 fw_free_buf(fw->priv);
477}
478
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100479/* store the pages buffer info firmware from buf */
480static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
481{
482 fw->priv = buf;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100483#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100484 fw->pages = buf->pages;
485#endif
486 fw->size = buf->size;
487 fw->data = buf->data;
488
489 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
490 __func__, buf->fw_id, buf, buf->data,
491 (unsigned int)buf->size);
492}
493
494#ifdef CONFIG_PM_SLEEP
495static void fw_name_devm_release(struct device *dev, void *res)
496{
497 struct fw_name_devm *fwn = res;
498
499 if (fwn->magic == (unsigned long)&fw_cache)
500 pr_debug("%s: fw_name-%s devm-%p released\n",
501 __func__, fwn->name, res);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700502 kfree_const(fwn->name);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100503}
504
505static int fw_devm_match(struct device *dev, void *res,
506 void *match_data)
507{
508 struct fw_name_devm *fwn = res;
509
510 return (fwn->magic == (unsigned long)&fw_cache) &&
511 !strcmp(fwn->name, match_data);
512}
513
514static struct fw_name_devm *fw_find_devm_name(struct device *dev,
515 const char *name)
516{
517 struct fw_name_devm *fwn;
518
519 fwn = devres_find(dev, fw_name_devm_release,
520 fw_devm_match, (void *)name);
521 return fwn;
522}
523
524/* add firmware name into devres list */
525static int fw_add_devm_name(struct device *dev, const char *name)
526{
527 struct fw_name_devm *fwn;
528
529 fwn = fw_find_devm_name(dev, name);
530 if (fwn)
531 return 1;
532
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700533 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
534 GFP_KERNEL);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100535 if (!fwn)
536 return -ENOMEM;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700537 fwn->name = kstrdup_const(name, GFP_KERNEL);
538 if (!fwn->name) {
Vladimir Zapolskiya885de62015-07-29 23:26:28 +0300539 devres_free(fwn);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700540 return -ENOMEM;
541 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100542
543 fwn->magic = (unsigned long)&fw_cache;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100544 devres_add(dev, fwn);
545
546 return 0;
547}
548#else
549static int fw_add_devm_name(struct device *dev, const char *name)
550{
551 return 0;
552}
553#endif
554
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700555static int assign_firmware_buf(struct firmware *fw, struct device *device,
556 unsigned int opt_flags)
557{
558 struct firmware_buf *buf = fw->priv;
559
560 mutex_lock(&fw_lock);
561 if (!buf->size || fw_state_is_aborted(&buf->fw_st)) {
562 mutex_unlock(&fw_lock);
563 return -ENOENT;
564 }
565
566 /*
567 * add firmware name into devres list so that we can auto cache
568 * and uncache firmware for device.
569 *
570 * device may has been deleted already, but the problem
571 * should be fixed in devres or driver core.
572 */
573 /* don't cache firmware handled without uevent */
574 if (device && (opt_flags & FW_OPT_UEVENT) &&
575 !(opt_flags & FW_OPT_NOCACHE))
576 fw_add_devm_name(device, buf->fw_id);
577
578 /*
579 * After caching firmware image is started, let it piggyback
580 * on request firmware.
581 */
582 if (!(opt_flags & FW_OPT_NOCACHE) &&
583 buf->fwc->state == FW_LOADER_START_CACHE) {
584 if (fw_cache_piggyback_on_request(buf->fw_id))
585 kref_get(&buf->ref);
586 }
587
588 /* pass the pages buffer to driver at the last minute */
589 fw_set_page_data(buf, fw);
590 mutex_unlock(&fw_lock);
591 return 0;
592}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100593
594/*
595 * user-mode helper code
596 */
597#ifdef CONFIG_FW_LOADER_USER_HELPER
598struct firmware_priv {
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100599 bool nowait;
600 struct device dev;
601 struct firmware_buf *buf;
602 struct firmware *fw;
603};
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100604
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700605static struct firmware_priv *to_firmware_priv(struct device *dev)
606{
607 return container_of(dev, struct firmware_priv, dev);
608}
609
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700610static void __fw_load_abort(struct firmware_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Ming Lei87597932013-06-15 16:36:38 +0800612 /*
613 * There is a small window in which user can write to 'loading'
614 * between loading done and disappearance of 'loading'
615 */
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100616 if (fw_state_is_done(&buf->fw_st))
Ming Lei87597932013-06-15 16:36:38 +0800617 return;
618
Takashi Iwaife304142013-05-22 18:28:38 +0200619 list_del_init(&buf->pending_list);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100620 fw_state_aborted(&buf->fw_st);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700623static void fw_load_abort(struct firmware_priv *fw_priv)
624{
625 struct firmware_buf *buf = fw_priv->buf;
626
627 __fw_load_abort(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Takashi Iwaife304142013-05-22 18:28:38 +0200630static LIST_HEAD(pending_fw_head);
631
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -0700632static void kill_pending_fw_fallback_reqs(bool only_kill_custom)
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700633{
634 struct firmware_buf *buf;
635 struct firmware_buf *next;
636
637 mutex_lock(&fw_lock);
638 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -0700639 if (!buf->need_uevent || !only_kill_custom)
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700640 __fw_load_abort(buf);
641 }
642 mutex_unlock(&fw_lock);
643}
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700644
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700645static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
646 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 return sprintf(buf, "%d\n", loading_timeout);
649}
650
651/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800652 * firmware_timeout_store - set number of seconds to wait for firmware
653 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800654 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800655 * @buf: buffer to scan for timeout value
656 * @count: number of bytes in @buf
657 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800659 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 * firmware will be provided.
661 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800662 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 **/
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700664static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
665 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700668 if (loading_timeout < 0)
669 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return count;
672}
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100673static CLASS_ATTR_RW(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100675static struct attribute *firmware_class_attrs[] = {
676 &class_attr_timeout.attr,
677 NULL,
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800678};
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100679ATTRIBUTE_GROUPS(firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800681static void fw_dev_release(struct device *dev)
682{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700683 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800684
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800685 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800686}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Linus Torvalds6f957722015-07-09 11:20:01 -0700688static int do_firmware_uevent(struct firmware_priv *fw_priv, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Ming Lei12446912012-08-04 12:01:20 +0800690 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200692 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700693 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200694 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
695 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 return 0;
698}
699
Linus Torvalds6f957722015-07-09 11:20:01 -0700700static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
701{
702 struct firmware_priv *fw_priv = to_firmware_priv(dev);
703 int err = 0;
704
705 mutex_lock(&fw_lock);
706 if (fw_priv->buf)
707 err = do_firmware_uevent(fw_priv, env);
708 mutex_unlock(&fw_lock);
709 return err;
710}
711
Adrian Bunk1b81d662006-05-20 15:00:16 -0700712static struct class firmware_class = {
713 .name = "firmware",
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100714 .class_groups = firmware_class_groups,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700715 .dev_uevent = firmware_uevent,
716 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700717};
718
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700719static ssize_t firmware_loading_show(struct device *dev,
720 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700722 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800723 int loading = 0;
724
725 mutex_lock(&fw_lock);
726 if (fw_priv->buf)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100727 loading = fw_state_is_loading(&fw_priv->buf->fw_st);
Ming Lei87597932013-06-15 16:36:38 +0800728 mutex_unlock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return sprintf(buf, "%d\n", loading);
731}
732
David Woodhouse6e03a202009-04-09 22:04:07 -0700733/* Some architectures don't have PAGE_KERNEL_RO */
734#ifndef PAGE_KERNEL_RO
735#define PAGE_KERNEL_RO PAGE_KERNEL
736#endif
Ming Lei253c9242012-10-09 12:01:02 +0800737
738/* one pages buffer should be mapped/unmapped only once */
739static int fw_map_pages_buf(struct firmware_buf *buf)
740{
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100741 if (!buf->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800742 return 0;
743
Markus Elfringdaa3d672014-11-19 11:38:38 +0100744 vunmap(buf->data);
Ming Lei253c9242012-10-09 12:01:02 +0800745 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
746 if (!buf->data)
747 return -ENOMEM;
748 return 0;
749}
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800752 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700753 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800754 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800755 * @buf: buffer to scan for loading control value
756 * @count: number of bytes in @buf
757 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 * The relevant values are:
759 *
760 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800761 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 * -1: Conclude the load with an error and discard any written data.
763 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700764static ssize_t firmware_loading_store(struct device *dev,
765 struct device_attribute *attr,
766 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700768 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800769 struct firmware_buf *fw_buf;
Kees Cook6593d922014-02-25 13:06:00 -0800770 ssize_t written = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700772 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Neil Hormaneea915b2012-01-02 15:31:23 -0500774 mutex_lock(&fw_lock);
Ming Lei87597932013-06-15 16:36:38 +0800775 fw_buf = fw_priv->buf;
Luis R. Rodriguez191e8852017-01-25 10:31:52 -0800776 if (fw_state_is_aborted(&fw_buf->fw_st))
Neil Hormaneea915b2012-01-02 15:31:23 -0500777 goto out;
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 switch (loading) {
780 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800781 /* discarding any previous partial load */
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100782 if (!fw_state_is_done(&fw_buf->fw_st)) {
Ming Lei12446912012-08-04 12:01:20 +0800783 for (i = 0; i < fw_buf->nr_pages; i++)
784 __free_page(fw_buf->pages[i]);
Chen Feng10a3fbf2015-12-16 17:03:15 +0800785 vfree(fw_buf->pages);
Ming Lei12446912012-08-04 12:01:20 +0800786 fw_buf->pages = NULL;
787 fw_buf->page_array_size = 0;
788 fw_buf->nr_pages = 0;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100789 fw_state_start(&fw_buf->fw_st);
Ming Lei28eefa72012-08-04 12:01:17 +0800790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 break;
792 case 0:
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100793 if (fw_state_is_loading(&fw_buf->fw_st)) {
Kees Cook6593d922014-02-25 13:06:00 -0800794 int rc;
795
Ming Lei253c9242012-10-09 12:01:02 +0800796 /*
797 * Several loading requests may be pending on
798 * one same firmware buf, so let all requests
799 * see the mapped 'buf->data' once the loading
800 * is completed.
801 * */
Kees Cook6593d922014-02-25 13:06:00 -0800802 rc = fw_map_pages_buf(fw_buf);
803 if (rc)
zhang jun2b1278c2014-02-13 15:18:47 +0800804 dev_err(dev, "%s: map pages failed\n",
805 __func__);
Kees Cook6593d922014-02-25 13:06:00 -0800806 else
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500807 rc = security_kernel_post_read_file(NULL,
808 fw_buf->data, fw_buf->size,
809 READING_FIRMWARE);
Kees Cook6593d922014-02-25 13:06:00 -0800810
811 /*
812 * Same logic as fw_load_abort, only the DONE bit
813 * is ignored and we set ABORT only on failure.
814 */
Takashi Iwaife304142013-05-22 18:28:38 +0200815 list_del_init(&fw_buf->pending_list);
Kees Cook6593d922014-02-25 13:06:00 -0800816 if (rc) {
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100817 fw_state_aborted(&fw_buf->fw_st);
Kees Cook6593d922014-02-25 13:06:00 -0800818 written = rc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100819 } else {
820 fw_state_done(&fw_buf->fw_st);
Kees Cook6593d922014-02-25 13:06:00 -0800821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 break;
823 }
824 /* fallthrough */
825 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700826 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 /* fallthrough */
828 case -1:
829 fw_load_abort(fw_priv);
830 break;
831 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500832out:
833 mutex_unlock(&fw_lock);
Kees Cook6593d922014-02-25 13:06:00 -0800834 return written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700837static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Stephen Boyda098ecd2016-08-02 14:04:28 -0700839static void firmware_rw_buf(struct firmware_buf *buf, char *buffer,
840 loff_t offset, size_t count, bool read)
841{
842 if (read)
843 memcpy(buffer, buf->data + offset, count);
844 else
845 memcpy(buf->data + offset, buffer, count);
846}
847
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700848static void firmware_rw(struct firmware_buf *buf, char *buffer,
849 loff_t offset, size_t count, bool read)
850{
851 while (count) {
852 void *page_data;
853 int page_nr = offset >> PAGE_SHIFT;
854 int page_ofs = offset & (PAGE_SIZE-1);
855 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
856
857 page_data = kmap(buf->pages[page_nr]);
858
859 if (read)
860 memcpy(buffer, page_data + page_ofs, page_cnt);
861 else
862 memcpy(page_data + page_ofs, buffer, page_cnt);
863
864 kunmap(buf->pages[page_nr]);
865 buffer += page_cnt;
866 offset += page_cnt;
867 count -= page_cnt;
868 }
869}
870
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700871static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
872 struct bin_attribute *bin_attr,
873 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200875 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700876 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800877 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700878 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Laura Garciacad1e552006-05-23 23:22:38 +0200880 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800881 buf = fw_priv->buf;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100882 if (!buf || fw_state_is_done(&buf->fw_st)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 ret_count = -ENODEV;
884 goto out;
885 }
Ming Lei12446912012-08-04 12:01:20 +0800886 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200887 ret_count = 0;
888 goto out;
889 }
Ming Lei12446912012-08-04 12:01:20 +0800890 if (count > buf->size - offset)
891 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700892
893 ret_count = count;
894
Stephen Boyda098ecd2016-08-02 14:04:28 -0700895 if (buf->data)
896 firmware_rw_buf(buf, buffer, offset, count, true);
897 else
898 firmware_rw(buf, buffer, offset, count, true);
David Woodhouse6e03a202009-04-09 22:04:07 -0700899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900out:
Laura Garciacad1e552006-05-23 23:22:38 +0200901 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return ret_count;
903}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800904
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700905static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Ming Lei12446912012-08-04 12:01:20 +0800907 struct firmware_buf *buf = fw_priv->buf;
Fabian Fredericka76040d2014-07-01 21:13:30 +0200908 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
David Woodhouse6e03a202009-04-09 22:04:07 -0700910 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800911 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700912 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800913 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700914 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Chen Feng10a3fbf2015-12-16 17:03:15 +0800916 new_pages = vmalloc(new_array_size * sizeof(void *));
David Woodhouse6e03a202009-04-09 22:04:07 -0700917 if (!new_pages) {
918 fw_load_abort(fw_priv);
919 return -ENOMEM;
920 }
Ming Lei12446912012-08-04 12:01:20 +0800921 memcpy(new_pages, buf->pages,
922 buf->page_array_size * sizeof(void *));
923 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
924 (new_array_size - buf->page_array_size));
Chen Feng10a3fbf2015-12-16 17:03:15 +0800925 vfree(buf->pages);
Ming Lei12446912012-08-04 12:01:20 +0800926 buf->pages = new_pages;
927 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700929
Ming Lei12446912012-08-04 12:01:20 +0800930 while (buf->nr_pages < pages_needed) {
931 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700932 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
933
Ming Lei12446912012-08-04 12:01:20 +0800934 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700935 fw_load_abort(fw_priv);
936 return -ENOMEM;
937 }
Ming Lei12446912012-08-04 12:01:20 +0800938 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return 0;
941}
942
943/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800944 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700945 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700946 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700947 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800948 * @buffer: buffer being written
949 * @offset: buffer offset for write in total data store area
950 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800952 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 * the driver as a firmware image.
954 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700955static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
956 struct bin_attribute *bin_attr,
957 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200959 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700960 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800961 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 ssize_t retval;
963
964 if (!capable(CAP_SYS_RAWIO))
965 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700966
Laura Garciacad1e552006-05-23 23:22:38 +0200967 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800968 buf = fw_priv->buf;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100969 if (!buf || fw_state_is_done(&buf->fw_st)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 retval = -ENODEV;
971 goto out;
972 }
Ming Lei65710cb2012-08-04 12:01:16 +0800973
Stephen Boyda098ecd2016-08-02 14:04:28 -0700974 if (buf->data) {
975 if (offset + count > buf->allocated_size) {
976 retval = -ENOMEM;
977 goto out;
978 }
979 firmware_rw_buf(buf, buffer, offset, count, false);
980 retval = count;
981 } else {
982 retval = fw_realloc_buffer(fw_priv, offset + count);
983 if (retval)
984 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Stephen Boyda098ecd2016-08-02 14:04:28 -0700986 retval = count;
987 firmware_rw(buf, buffer, offset, count, false);
988 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700989
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700990 buf->size = max_t(size_t, offset + count, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991out:
Laura Garciacad1e552006-05-23 23:22:38 +0200992 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return retval;
994}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800995
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700996static struct bin_attribute firmware_attr_data = {
997 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 .size = 0,
999 .read = firmware_data_read,
1000 .write = firmware_data_write,
1001};
1002
Takashi Iwai46239902015-02-04 15:18:07 +01001003static struct attribute *fw_dev_attrs[] = {
1004 &dev_attr_loading.attr,
1005 NULL
1006};
1007
1008static struct bin_attribute *fw_dev_bin_attrs[] = {
1009 &firmware_attr_data,
1010 NULL
1011};
1012
1013static const struct attribute_group fw_dev_attr_group = {
1014 .attrs = fw_dev_attrs,
1015 .bin_attrs = fw_dev_bin_attrs,
1016};
1017
1018static const struct attribute_group *fw_dev_attr_groups[] = {
1019 &fw_dev_attr_group,
1020 NULL
1021};
1022
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001023static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +02001024fw_create_instance(struct firmware *firmware, const char *fw_name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001025 struct device *device, unsigned int opt_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001027 struct firmware_priv *fw_priv;
1028 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Ming Lei12446912012-08-04 12:01:20 +08001030 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001031 if (!fw_priv) {
Ming Lei12446912012-08-04 12:01:20 +08001032 fw_priv = ERR_PTR(-ENOMEM);
1033 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001036 fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT);
Ming Lei1f2b7952012-08-04 12:01:21 +08001037 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001038 f_dev = &fw_priv->dev;
1039
1040 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +08001041 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -07001042 f_dev->parent = device;
1043 f_dev->class = &firmware_class;
Takashi Iwai46239902015-02-04 15:18:07 +01001044 f_dev->groups = fw_dev_attr_groups;
Ming Lei12446912012-08-04 12:01:20 +08001045exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001046 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001048
1049/* load a firmware via user helper */
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001050static int _request_firmware_load(struct firmware_priv *fw_priv,
1051 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001052{
1053 int retval = 0;
1054 struct device *f_dev = &fw_priv->dev;
1055 struct firmware_buf *buf = fw_priv->buf;
1056
1057 /* fall back on userspace loading */
Stephen Boyda098ecd2016-08-02 14:04:28 -07001058 if (!buf->data)
1059 buf->is_paged_buf = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001060
1061 dev_set_uevent_suppress(f_dev, true);
1062
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001063 retval = device_add(f_dev);
1064 if (retval) {
1065 dev_err(f_dev, "%s: device_register failed\n", __func__);
1066 goto err_put_dev;
1067 }
1068
Maxime Bizon1eeeef12013-08-29 20:28:13 +02001069 mutex_lock(&fw_lock);
1070 list_add(&buf->pending_list, &pending_fw_head);
1071 mutex_unlock(&fw_lock);
1072
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001073 if (opt_flags & FW_OPT_UEVENT) {
Ming Leiaf5bc112013-05-27 18:30:04 +08001074 buf->need_uevent = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001075 dev_set_uevent_suppress(f_dev, false);
1076 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001077 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
Ming Lei68ff2a02015-01-13 00:02:01 +08001078 } else {
1079 timeout = MAX_JIFFY_OFFSET;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001080 }
1081
Bjorn Andersson5d47ec02016-12-06 17:01:45 -08001082 retval = fw_state_wait_timeout(&buf->fw_st, timeout);
1083 if (retval < 0) {
Ming Lei0cb64242015-01-13 00:01:35 +08001084 mutex_lock(&fw_lock);
1085 fw_load_abort(fw_priv);
1086 mutex_unlock(&fw_lock);
1087 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001088
Daniel Wagnerf52cc3792016-11-17 11:00:48 +01001089 if (fw_state_is_aborted(&buf->fw_st))
Shuah Khan0542ad82014-07-22 18:10:38 -06001090 retval = -EAGAIN;
Stephen Boyda098ecd2016-08-02 14:04:28 -07001091 else if (buf->is_paged_buf && !buf->data)
zhang jun2b1278c2014-02-13 15:18:47 +08001092 retval = -ENOMEM;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001093
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001094 device_del(f_dev);
1095err_put_dev:
1096 put_device(f_dev);
1097 return retval;
1098}
1099
1100static int fw_load_from_user_helper(struct firmware *firmware,
1101 const char *name, struct device *device,
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001102 unsigned int opt_flags)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001103{
1104 struct firmware_priv *fw_priv;
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001105 long timeout;
1106 int ret;
1107
1108 timeout = firmware_loading_timeout();
1109 if (opt_flags & FW_OPT_NOWAIT) {
1110 timeout = usermodehelper_read_lock_wait(timeout);
1111 if (!timeout) {
1112 dev_dbg(device, "firmware: %s loading timed out\n",
1113 name);
1114 return -EBUSY;
1115 }
1116 } else {
1117 ret = usermodehelper_read_trylock();
1118 if (WARN_ON(ret)) {
1119 dev_err(device, "firmware: %s will not be loaded\n",
1120 name);
1121 return ret;
1122 }
1123 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001124
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001125 fw_priv = fw_create_instance(firmware, name, device, opt_flags);
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001126 if (IS_ERR(fw_priv)) {
1127 ret = PTR_ERR(fw_priv);
1128 goto out_unlock;
1129 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001130
1131 fw_priv->buf = firmware->priv;
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001132 ret = _request_firmware_load(fw_priv, opt_flags, timeout);
1133
1134 if (!ret)
1135 ret = assign_firmware_buf(firmware, device, opt_flags);
1136
1137out_unlock:
1138 usermodehelper_read_unlock();
1139
1140 return ret;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001141}
Ming Leiddf1f062013-06-04 10:01:13 +08001142
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001143#else /* CONFIG_FW_LOADER_USER_HELPER */
1144static inline int
1145fw_load_from_user_helper(struct firmware *firmware, const char *name,
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001146 struct device *device, unsigned int opt_flags)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001147{
1148 return -ENOENT;
1149}
Takashi Iwai807be032013-01-31 11:13:57 +01001150
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -07001151static inline void kill_pending_fw_fallback_reqs(bool only_kill_custom) { }
Ming Leiddf1f062013-06-04 10:01:13 +08001152
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001153#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001155/* prepare firmware and firmware_buf structs;
1156 * return 0 if a firmware is already assigned, 1 if need to load one,
1157 * or a negative error code
1158 */
1159static int
1160_request_firmware_prepare(struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001161 struct device *device, void *dbuf, size_t size)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001162{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +08001164 struct firmware_buf *buf;
1165 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Jiri Slaby4aed0642005-09-13 01:25:01 -07001167 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -07001169 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1170 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001171 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Stephen Boyda098ecd2016-08-02 14:04:28 -07001174 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
Luis R. Rodriguezed046302015-04-29 16:30:43 -07001175 dev_dbg(device, "using built-in %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001176 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +01001177 }
1178
Stephen Boyda098ecd2016-08-02 14:04:28 -07001179 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +08001180
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001181 /*
1182 * bind with 'buf' now to avoid warning in failure path
1183 * of requesting firmware.
1184 */
1185 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +08001186
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001187 if (ret > 0) {
Daniel Wagner5b029622016-11-17 11:00:50 +01001188 ret = fw_state_wait(&buf->fw_st);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001189 if (!ret) {
1190 fw_set_page_data(buf, firmware);
1191 return 0; /* assigned */
1192 }
Stephen Boyddddb5542012-03-28 23:30:43 +02001193 }
Ming Lei1f2b7952012-08-04 12:01:21 +08001194
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001195 if (ret < 0)
1196 return ret;
1197 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001198}
1199
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001200/*
1201 * Batched requests need only one wake, we need to do this step last due to the
1202 * fallback mechanism. The buf is protected with kref_get(), and it won't be
1203 * released until the last user calls release_firmware().
1204 *
1205 * Failed batched requests are possible as well, in such cases we just share
1206 * the struct firmware_buf and won't release it until all requests are woken
1207 * and have gone through this same path.
1208 */
1209static void fw_abort_batch_reqs(struct firmware *fw)
1210{
1211 struct firmware_buf *buf;
1212
1213 /* Loaded directly? */
1214 if (!fw || !fw->priv)
1215 return;
1216
1217 buf = fw->priv;
1218 if (!fw_state_is_aborted(&buf->fw_st))
1219 fw_state_aborted(&buf->fw_st);
1220}
1221
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001222/* called from request_firmware() and request_firmware_work_func() */
1223static int
1224_request_firmware(const struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001225 struct device *device, void *buf, size_t size,
1226 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001227{
Brian Norris715780a2015-12-09 14:50:28 -08001228 struct firmware *fw = NULL;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001229 int ret;
1230
1231 if (!firmware_p)
1232 return -EINVAL;
1233
Brian Norris715780a2015-12-09 14:50:28 -08001234 if (!name || name[0] == '\0') {
1235 ret = -EINVAL;
1236 goto out;
1237 }
Kees Cook471b0952014-09-18 11:25:37 -07001238
Stephen Boyda098ecd2016-08-02 14:04:28 -07001239 ret = _request_firmware_prepare(&fw, name, device, buf, size);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001240 if (ret <= 0) /* error or already assigned */
1241 goto out;
1242
Luis R. Rodriguez81f95072017-05-02 01:31:05 -07001243 if (!firmware_enabled()) {
1244 WARN(1, "firmware request while host is not available\n");
1245 ret = -EHOSTDOWN;
1246 goto out;
1247 }
1248
Neil Horman3e358ac2013-09-06 15:36:08 -04001249 ret = fw_get_filesystem_firmware(device, fw->priv);
1250 if (ret) {
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001251 if (!(opt_flags & FW_OPT_NO_WARN))
Takashi Iwaibba3a872013-12-02 15:38:16 +01001252 dev_warn(device,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001253 "Direct firmware load for %s failed with error %d\n",
1254 name, ret);
1255 if (opt_flags & FW_OPT_USERHELPER) {
Takashi Iwaibba3a872013-12-02 15:38:16 +01001256 dev_warn(device, "Falling back to user helper\n");
1257 ret = fw_load_from_user_helper(fw, name, device,
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001258 opt_flags);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001259 }
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001260 } else
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001261 ret = assign_firmware_buf(fw, device, opt_flags);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001262
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001263 out:
1264 if (ret < 0) {
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001265 fw_abort_batch_reqs(fw);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001266 release_firmware(fw);
1267 fw = NULL;
1268 }
1269
1270 *firmware_p = fw;
1271 return ret;
1272}
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274/**
Kay Sievers312c0042005-11-16 09:00:00 +01001275 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001276 * @firmware_p: pointer to firmware image
1277 * @name: name of firmware file
1278 * @device: device for which firmware is being loaded
1279 *
1280 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001281 * of @name for device @device.
1282 *
1283 * Should be called from user context where sleeping is allowed.
1284 *
Kay Sievers312c0042005-11-16 09:00:00 +01001285 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001286 * should be distinctive enough not to be confused with any other
1287 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001288 *
1289 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001290 *
1291 * The function can be called safely inside device's suspend and
1292 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001293 **/
1294int
1295request_firmware(const struct firmware **firmware_p, const char *name,
Andrei Opreaea310032015-03-08 12:41:15 +02001296 struct device *device)
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001297{
Ming Leid6c8aa32013-06-06 20:01:48 +08001298 int ret;
1299
1300 /* Need to pin this module until return */
1301 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001302 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001303 FW_OPT_UEVENT | FW_OPT_FALLBACK);
Ming Leid6c8aa32013-06-06 20:01:48 +08001304 module_put(THIS_MODULE);
1305 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001306}
Daniel Mackf4945132013-05-23 22:17:18 +02001307EXPORT_SYMBOL(request_firmware);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001308
Takashi Iwaibba3a872013-12-02 15:38:16 +01001309/**
Borislav Petkov3c1556b2014-12-03 22:46:37 +01001310 * request_firmware_direct: - load firmware directly without usermode helper
Takashi Iwaibba3a872013-12-02 15:38:16 +01001311 * @firmware_p: pointer to firmware image
1312 * @name: name of firmware file
1313 * @device: device for which firmware is being loaded
1314 *
1315 * This function works pretty much like request_firmware(), but this doesn't
1316 * fall back to usermode helper even if the firmware couldn't be loaded
1317 * directly from fs. Hence it's useful for loading optional firmwares, which
1318 * aren't always present, without extra long timeouts of udev.
1319 **/
1320int request_firmware_direct(const struct firmware **firmware_p,
1321 const char *name, struct device *device)
1322{
1323 int ret;
Andrei Opreaea310032015-03-08 12:41:15 +02001324
Takashi Iwaibba3a872013-12-02 15:38:16 +01001325 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001326 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001327 FW_OPT_UEVENT | FW_OPT_NO_WARN);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001328 module_put(THIS_MODULE);
1329 return ret;
1330}
1331EXPORT_SYMBOL_GPL(request_firmware_direct);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001332
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001333/**
Stephen Boyda098ecd2016-08-02 14:04:28 -07001334 * request_firmware_into_buf - load firmware into a previously allocated buffer
1335 * @firmware_p: pointer to firmware image
1336 * @name: name of firmware file
1337 * @device: device for which firmware is being loaded and DMA region allocated
1338 * @buf: address of buffer to load firmware into
1339 * @size: size of buffer
1340 *
1341 * This function works pretty much like request_firmware(), but it doesn't
1342 * allocate a buffer to hold the firmware data. Instead, the firmware
1343 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
1344 * data member is pointed at @buf.
1345 *
1346 * This function doesn't cache firmware either.
1347 */
1348int
1349request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
1350 struct device *device, void *buf, size_t size)
1351{
1352 int ret;
1353
1354 __module_get(THIS_MODULE);
1355 ret = _request_firmware(firmware_p, name, device, buf, size,
1356 FW_OPT_UEVENT | FW_OPT_FALLBACK |
1357 FW_OPT_NOCACHE);
1358 module_put(THIS_MODULE);
1359 return ret;
1360}
1361EXPORT_SYMBOL(request_firmware_into_buf);
1362
1363/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001365 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001367void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001370 if (!fw_is_builtin_firmware(fw))
1371 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 kfree(fw);
1373 }
1374}
Daniel Mackf4945132013-05-23 22:17:18 +02001375EXPORT_SYMBOL(release_firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377/* Async support */
1378struct firmware_work {
1379 struct work_struct work;
1380 struct module *module;
1381 const char *name;
1382 struct device *device;
1383 void *context;
1384 void (*cont)(const struct firmware *fw, void *context);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001385 unsigned int opt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386};
1387
Stephen Boyda36cf842012-03-28 23:31:00 +02001388static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Stephen Boyda36cf842012-03-28 23:31:00 +02001390 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001392
Stephen Boyda36cf842012-03-28 23:31:00 +02001393 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001394
Stephen Boyda098ecd2016-08-02 14:04:28 -07001395 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001396 fw_work->opt_flags);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001397 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001398 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 module_put(fw_work->module);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001401 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
1405/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001406 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001407 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001408 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001409 * is non-zero else the firmware copy must be done manually.
1410 * @name: name of firmware file
1411 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001412 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001413 * @context: will be passed over to @cont, and
1414 * @fw may be %NULL if firmware request fails.
1415 * @cont: function will be called asynchronously when the firmware
1416 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001418 * Caller must hold the reference count of @device.
1419 *
Ming Lei6f21a622012-08-04 12:01:24 +08001420 * Asynchronous variant of request_firmware() for user contexts:
1421 * - sleep for as small periods as possible since it may
Silvio Fricke88bcef52016-11-25 15:59:47 +01001422 * increase kernel boot time of built-in device drivers
1423 * requesting firmware in their ->probe() methods, if
1424 * @gfp is GFP_KERNEL.
Ming Lei6f21a622012-08-04 12:01:24 +08001425 *
1426 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 **/
1428int
1429request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001430 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001431 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 void (*cont)(const struct firmware *fw, void *context))
1433{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001434 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Andrei Opreaea310032015-03-08 12:41:15 +02001436 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (!fw_work)
1438 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001439
1440 fw_work->module = module;
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001441 fw_work->name = kstrdup_const(name, gfp);
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001442 if (!fw_work->name) {
1443 kfree(fw_work);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001444 return -ENOMEM;
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001445 }
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001446 fw_work->device = device;
1447 fw_work->context = context;
1448 fw_work->cont = cont;
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001449 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
Takashi Iwai5a1379e2014-06-04 17:48:15 +02001450 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (!try_module_get(module)) {
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001453 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 kfree(fw_work);
1455 return -EFAULT;
1456 }
1457
Ming Lei0cfc1e12012-08-04 12:01:23 +08001458 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001459 INIT_WORK(&fw_work->work, request_firmware_work_func);
1460 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return 0;
1462}
Daniel Mackf4945132013-05-23 22:17:18 +02001463EXPORT_SYMBOL(request_firmware_nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Ming Lei90f890812013-06-20 12:30:16 +08001465#ifdef CONFIG_PM_SLEEP
1466static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1467
Ming Lei2887b392012-08-04 12:01:22 +08001468/**
1469 * cache_firmware - cache one firmware image in kernel memory space
1470 * @fw_name: the firmware image name
1471 *
1472 * Cache firmware in kernel memory so that drivers can use it when
1473 * system isn't ready for them to request firmware image from userspace.
1474 * Once it returns successfully, driver can use request_firmware or its
1475 * nowait version to get the cached firmware without any interacting
1476 * with userspace
1477 *
1478 * Return 0 if the firmware image has been cached successfully
1479 * Return !0 otherwise
1480 *
1481 */
Ming Lei93232e42013-06-06 20:01:47 +08001482static int cache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001483{
1484 int ret;
1485 const struct firmware *fw;
1486
1487 pr_debug("%s: %s\n", __func__, fw_name);
1488
1489 ret = request_firmware(&fw, fw_name, NULL);
1490 if (!ret)
1491 kfree(fw);
1492
1493 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1494
1495 return ret;
1496}
1497
Ming Lei6a2c1232013-06-26 09:28:17 +08001498static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1499{
1500 struct firmware_buf *tmp;
1501 struct firmware_cache *fwc = &fw_cache;
1502
1503 spin_lock(&fwc->lock);
1504 tmp = __fw_lookup_buf(fw_name);
1505 spin_unlock(&fwc->lock);
1506
1507 return tmp;
1508}
1509
Ming Lei2887b392012-08-04 12:01:22 +08001510/**
1511 * uncache_firmware - remove one cached firmware image
1512 * @fw_name: the firmware image name
1513 *
1514 * Uncache one firmware image which has been cached successfully
1515 * before.
1516 *
1517 * Return 0 if the firmware cache has been removed successfully
1518 * Return !0 otherwise
1519 *
1520 */
Ming Lei93232e42013-06-06 20:01:47 +08001521static int uncache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001522{
1523 struct firmware_buf *buf;
1524 struct firmware fw;
1525
1526 pr_debug("%s: %s\n", __func__, fw_name);
1527
Stephen Boyda098ecd2016-08-02 14:04:28 -07001528 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
Ming Lei2887b392012-08-04 12:01:22 +08001529 return 0;
1530
1531 buf = fw_lookup_buf(fw_name);
1532 if (buf) {
1533 fw_free_buf(buf);
1534 return 0;
1535 }
1536
1537 return -EINVAL;
1538}
1539
Ming Lei37276a52012-08-04 12:01:27 +08001540static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1541{
1542 struct fw_cache_entry *fce;
1543
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001544 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
Ming Lei37276a52012-08-04 12:01:27 +08001545 if (!fce)
1546 goto exit;
1547
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001548 fce->name = kstrdup_const(name, GFP_ATOMIC);
1549 if (!fce->name) {
1550 kfree(fce);
1551 fce = NULL;
1552 goto exit;
1553 }
Ming Lei37276a52012-08-04 12:01:27 +08001554exit:
1555 return fce;
1556}
1557
Ming Lei373304f2012-10-09 12:01:01 +08001558static int __fw_entry_found(const char *name)
1559{
1560 struct firmware_cache *fwc = &fw_cache;
1561 struct fw_cache_entry *fce;
1562
1563 list_for_each_entry(fce, &fwc->fw_names, list) {
1564 if (!strcmp(fce->name, name))
1565 return 1;
1566 }
1567 return 0;
1568}
1569
Ming Leiac39b3e2012-08-20 19:04:16 +08001570static int fw_cache_piggyback_on_request(const char *name)
1571{
1572 struct firmware_cache *fwc = &fw_cache;
1573 struct fw_cache_entry *fce;
1574 int ret = 0;
1575
1576 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001577 if (__fw_entry_found(name))
1578 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001579
1580 fce = alloc_fw_cache_entry(name);
1581 if (fce) {
1582 ret = 1;
1583 list_add(&fce->list, &fwc->fw_names);
1584 pr_debug("%s: fw: %s\n", __func__, name);
1585 }
1586found:
1587 spin_unlock(&fwc->name_lock);
1588 return ret;
1589}
1590
Ming Lei37276a52012-08-04 12:01:27 +08001591static void free_fw_cache_entry(struct fw_cache_entry *fce)
1592{
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001593 kfree_const(fce->name);
Ming Lei37276a52012-08-04 12:01:27 +08001594 kfree(fce);
1595}
1596
1597static void __async_dev_cache_fw_image(void *fw_entry,
1598 async_cookie_t cookie)
1599{
1600 struct fw_cache_entry *fce = fw_entry;
1601 struct firmware_cache *fwc = &fw_cache;
1602 int ret;
1603
1604 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001605 if (ret) {
1606 spin_lock(&fwc->name_lock);
1607 list_del(&fce->list);
1608 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001609
Ming Leiac39b3e2012-08-20 19:04:16 +08001610 free_fw_cache_entry(fce);
1611 }
Ming Lei37276a52012-08-04 12:01:27 +08001612}
1613
1614/* called with dev->devres_lock held */
1615static void dev_create_fw_entry(struct device *dev, void *res,
1616 void *data)
1617{
1618 struct fw_name_devm *fwn = res;
1619 const char *fw_name = fwn->name;
1620 struct list_head *head = data;
1621 struct fw_cache_entry *fce;
1622
1623 fce = alloc_fw_cache_entry(fw_name);
1624 if (fce)
1625 list_add(&fce->list, head);
1626}
1627
1628static int devm_name_match(struct device *dev, void *res,
1629 void *match_data)
1630{
1631 struct fw_name_devm *fwn = res;
1632 return (fwn->magic == (unsigned long)match_data);
1633}
1634
Ming Leiab6dd8e2012-08-17 22:07:00 +08001635static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001636{
1637 LIST_HEAD(todo);
1638 struct fw_cache_entry *fce;
1639 struct fw_cache_entry *fce_next;
1640 struct firmware_cache *fwc = &fw_cache;
1641
1642 devres_for_each_res(dev, fw_name_devm_release,
1643 devm_name_match, &fw_cache,
1644 dev_create_fw_entry, &todo);
1645
1646 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1647 list_del(&fce->list);
1648
1649 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001650 /* only one cache entry for one firmware */
1651 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001652 list_add(&fce->list, &fwc->fw_names);
1653 } else {
1654 free_fw_cache_entry(fce);
1655 fce = NULL;
1656 }
Ming Lei37276a52012-08-04 12:01:27 +08001657 spin_unlock(&fwc->name_lock);
1658
Ming Lei373304f2012-10-09 12:01:01 +08001659 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001660 async_schedule_domain(__async_dev_cache_fw_image,
1661 (void *)fce,
1662 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001663 }
1664}
1665
1666static void __device_uncache_fw_images(void)
1667{
1668 struct firmware_cache *fwc = &fw_cache;
1669 struct fw_cache_entry *fce;
1670
1671 spin_lock(&fwc->name_lock);
1672 while (!list_empty(&fwc->fw_names)) {
1673 fce = list_entry(fwc->fw_names.next,
1674 struct fw_cache_entry, list);
1675 list_del(&fce->list);
1676 spin_unlock(&fwc->name_lock);
1677
1678 uncache_firmware(fce->name);
1679 free_fw_cache_entry(fce);
1680
1681 spin_lock(&fwc->name_lock);
1682 }
1683 spin_unlock(&fwc->name_lock);
1684}
1685
1686/**
1687 * device_cache_fw_images - cache devices' firmware
1688 *
1689 * If one device called request_firmware or its nowait version
1690 * successfully before, the firmware names are recored into the
1691 * device's devres link list, so device_cache_fw_images can call
1692 * cache_firmware() to cache these firmwares for the device,
1693 * then the device driver can load its firmwares easily at
1694 * time when system is not ready to complete loading firmware.
1695 */
1696static void device_cache_fw_images(void)
1697{
1698 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001699 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001700 DEFINE_WAIT(wait);
1701
1702 pr_debug("%s\n", __func__);
1703
Ming Lei373304f2012-10-09 12:01:01 +08001704 /* cancel uncache work */
1705 cancel_delayed_work_sync(&fwc->work);
1706
Ming Leiffe53f62012-08-04 12:01:28 +08001707 /*
1708 * use small loading timeout for caching devices' firmware
1709 * because all these firmware images have been loaded
1710 * successfully at lease once, also system is ready for
1711 * completing firmware loading now. The maximum size of
1712 * firmware in current distributions is about 2M bytes,
1713 * so 10 secs should be enough.
1714 */
1715 old_timeout = loading_timeout;
1716 loading_timeout = 10;
1717
Ming Leiac39b3e2012-08-20 19:04:16 +08001718 mutex_lock(&fw_lock);
1719 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001720 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001721 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001722
1723 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001724 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001725
1726 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001727}
1728
1729/**
1730 * device_uncache_fw_images - uncache devices' firmware
1731 *
1732 * uncache all firmwares which have been cached successfully
1733 * by device_uncache_fw_images earlier
1734 */
1735static void device_uncache_fw_images(void)
1736{
1737 pr_debug("%s\n", __func__);
1738 __device_uncache_fw_images();
1739}
1740
1741static void device_uncache_fw_images_work(struct work_struct *work)
1742{
1743 device_uncache_fw_images();
1744}
1745
1746/**
1747 * device_uncache_fw_images_delay - uncache devices firmwares
1748 * @delay: number of milliseconds to delay uncache device firmwares
1749 *
1750 * uncache all devices's firmwares which has been cached successfully
1751 * by device_cache_fw_images after @delay milliseconds.
1752 */
1753static void device_uncache_fw_images_delay(unsigned long delay)
1754{
Shaibal Duttabce66182014-01-31 15:44:58 -08001755 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1756 msecs_to_jiffies(delay));
Ming Lei37276a52012-08-04 12:01:27 +08001757}
1758
Luis R. Rodriguez81f95072017-05-02 01:31:05 -07001759/**
1760 * fw_pm_notify - notifier for suspend/resume
1761 * @notify_block: unused
1762 * @mode: mode we are switching to
1763 * @unused: unused
1764 *
1765 * Used to modify the firmware_class state as we move in between states.
1766 * The firmware_class implements a firmware cache to enable device driver
1767 * to fetch firmware upon resume before the root filesystem is ready. We
1768 * disable API calls which do not use the built-in firmware or the firmware
1769 * cache when we know these calls will not work.
1770 *
1771 * The inner logic behind all this is a bit complex so it is worth summarizing
1772 * the kernel's own suspend/resume process with context and focus on how this
1773 * can impact the firmware API.
1774 *
1775 * First a review on how we go to suspend::
1776 *
1777 * pm_suspend() --> enter_state() -->
1778 * sys_sync()
1779 * suspend_prepare() -->
1780 * __pm_notifier_call_chain(PM_SUSPEND_PREPARE, ...);
1781 * suspend_freeze_processes() -->
1782 * freeze_processes() -->
1783 * __usermodehelper_set_disable_depth(UMH_DISABLED);
1784 * freeze all tasks ...
1785 * freeze_kernel_threads()
1786 * suspend_devices_and_enter() -->
1787 * dpm_suspend_start() -->
1788 * dpm_prepare()
1789 * dpm_suspend()
1790 * suspend_enter() -->
1791 * platform_suspend_prepare()
1792 * dpm_suspend_late()
1793 * freeze_enter()
1794 * syscore_suspend()
1795 *
1796 * When we resume we bail out of a loop from suspend_devices_and_enter() and
1797 * unwind back out to the caller enter_state() where we were before as follows::
1798 *
1799 * enter_state() -->
1800 * suspend_devices_and_enter() --> (bail from loop)
1801 * dpm_resume_end() -->
1802 * dpm_resume()
1803 * dpm_complete()
1804 * suspend_finish() -->
1805 * suspend_thaw_processes() -->
1806 * thaw_processes() -->
1807 * __usermodehelper_set_disable_depth(UMH_FREEZING);
1808 * thaw_workqueues();
1809 * thaw all processes ...
1810 * usermodehelper_enable();
1811 * pm_notifier_call_chain(PM_POST_SUSPEND);
1812 *
1813 * fw_pm_notify() works through pm_notifier_call_chain().
1814 */
Ming Lei07646d92012-08-04 12:01:29 +08001815static int fw_pm_notify(struct notifier_block *notify_block,
1816 unsigned long mode, void *unused)
1817{
1818 switch (mode) {
1819 case PM_HIBERNATION_PREPARE:
1820 case PM_SUSPEND_PREPARE:
Sebastian Capellaf8d5b9e2014-02-18 17:52:08 -08001821 case PM_RESTORE_PREPARE:
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -07001822 /*
1823 * kill pending fallback requests with a custom fallback
1824 * to avoid stalling suspend.
1825 */
1826 kill_pending_fw_fallback_reqs(true);
Ming Lei07646d92012-08-04 12:01:29 +08001827 device_cache_fw_images();
Luis R. Rodriguez81f95072017-05-02 01:31:05 -07001828 disable_firmware();
Ming Lei07646d92012-08-04 12:01:29 +08001829 break;
1830
1831 case PM_POST_SUSPEND:
1832 case PM_POST_HIBERNATION:
1833 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001834 /*
1835 * In case that system sleep failed and syscore_suspend is
1836 * not called.
1837 */
1838 mutex_lock(&fw_lock);
1839 fw_cache.state = FW_LOADER_NO_CACHE;
1840 mutex_unlock(&fw_lock);
Luis R. Rodriguez81f95072017-05-02 01:31:05 -07001841 enable_firmware();
Ming Leiac39b3e2012-08-20 19:04:16 +08001842
Ming Lei07646d92012-08-04 12:01:29 +08001843 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1844 break;
1845 }
1846
1847 return 0;
1848}
Ming Lei07646d92012-08-04 12:01:29 +08001849
Ming Leiac39b3e2012-08-20 19:04:16 +08001850/* stop caching firmware once syscore_suspend is reached */
1851static int fw_suspend(void)
1852{
1853 fw_cache.state = FW_LOADER_NO_CACHE;
1854 return 0;
1855}
1856
1857static struct syscore_ops fw_syscore_ops = {
1858 .suspend = fw_suspend,
1859};
Ming Leicfe016b2012-09-08 17:32:30 +08001860#else
1861static int fw_cache_piggyback_on_request(const char *name)
1862{
1863 return 0;
1864}
1865#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001866
Ming Lei37276a52012-08-04 12:01:27 +08001867static void __init fw_cache_init(void)
1868{
1869 spin_lock_init(&fw_cache.lock);
1870 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001871 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001872
Ming Leicfe016b2012-09-08 17:32:30 +08001873#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001874 spin_lock_init(&fw_cache.name_lock);
1875 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001876
Ming Lei37276a52012-08-04 12:01:27 +08001877 INIT_DELAYED_WORK(&fw_cache.work,
1878 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001879
1880 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1881 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001882
1883 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001884#endif
Ming Lei37276a52012-08-04 12:01:27 +08001885}
1886
Luis R. Rodrigueza669f042017-05-02 01:31:04 -07001887static int fw_shutdown_notify(struct notifier_block *unused1,
1888 unsigned long unused2, void *unused3)
1889{
Luis R. Rodriguez81f95072017-05-02 01:31:05 -07001890 disable_firmware();
Luis R. Rodrigueza669f042017-05-02 01:31:04 -07001891 /*
1892 * Kill all pending fallback requests to avoid both stalling shutdown,
1893 * and avoid a deadlock with the usermode_lock.
1894 */
1895 kill_pending_fw_fallback_reqs(false);
1896
1897 return NOTIFY_DONE;
1898}
1899
1900static struct notifier_block fw_shutdown_nb = {
1901 .notifier_call = fw_shutdown_notify,
1902};
1903
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001904static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
Luis R. Rodriguez81f95072017-05-02 01:31:05 -07001906 enable_firmware();
Ming Lei1f2b7952012-08-04 12:01:21 +08001907 fw_cache_init();
Takashi Iwaife304142013-05-22 18:28:38 +02001908 register_reboot_notifier(&fw_shutdown_nb);
Luis R. Rodrigueza669f042017-05-02 01:31:04 -07001909#ifdef CONFIG_FW_LOADER_USER_HELPER
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001910 return class_register(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001911#else
1912 return 0;
1913#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001915
1916static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Luis R. Rodriguez81f95072017-05-02 01:31:05 -07001918 disable_firmware();
Ming Leicfe016b2012-09-08 17:32:30 +08001919#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001920 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001921 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001922#endif
Takashi Iwaife304142013-05-22 18:28:38 +02001923 unregister_reboot_notifier(&fw_shutdown_nb);
Luis R. Rodrigueza669f042017-05-02 01:31:04 -07001924#ifdef CONFIG_FW_LOADER_USER_HELPER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 class_unregister(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001926#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
Shaohua Lia30a6a22006-09-27 01:50:52 -07001929fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930module_exit(firmware_class_exit);