blob: ac350c518e0c9479c05c4d9ff9f6ae918f26b96c [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>
Daniel Wagner5b029622016-11-17 11:00:50 +010033#include <linux/swait.h>
Ming Lei37276a52012-08-04 12:01:27 +080034
Linus Torvaldsabb139e2012-10-03 15:58:32 -070035#include <generated/utsrelease.h>
36
Ming Lei37276a52012-08-04 12:01:27 +080037#include "base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Markus Rechberger87d37a42007-06-04 18:45:44 +020039MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070040MODULE_DESCRIPTION("Multi purpose firmware loading support");
41MODULE_LICENSE("GPL");
42
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080043/* Builtin firmware support */
44
45#ifdef CONFIG_FW_LOADER
46
47extern struct builtin_fw __start_builtin_fw[];
48extern struct builtin_fw __end_builtin_fw[];
49
Stephen Boyda098ecd2016-08-02 14:04:28 -070050static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
51 void *buf, size_t size)
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080052{
53 struct builtin_fw *b_fw;
54
55 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
56 if (strcmp(name, b_fw->name) == 0) {
57 fw->size = b_fw->size;
58 fw->data = b_fw->data;
Stephen Boyda098ecd2016-08-02 14:04:28 -070059
60 if (buf && fw->size <= size)
61 memcpy(buf, fw->data, fw->size);
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080062 return true;
63 }
64 }
65
66 return false;
67}
68
69static bool fw_is_builtin_firmware(const struct firmware *fw)
70{
71 struct builtin_fw *b_fw;
72
73 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
74 if (fw->data == b_fw->data)
75 return true;
76
77 return false;
78}
79
80#else /* Module case - no builtin firmware support */
81
Stephen Boyda098ecd2016-08-02 14:04:28 -070082static inline bool fw_get_builtin_firmware(struct firmware *fw,
83 const char *name, void *buf,
84 size_t size)
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080085{
86 return false;
87}
88
89static inline bool fw_is_builtin_firmware(const struct firmware *fw)
90{
91 return false;
92}
93#endif
94
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010095enum fw_status {
96 FW_STATUS_UNKNOWN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 FW_STATUS_LOADING,
98 FW_STATUS_DONE,
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010099 FW_STATUS_ABORTED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
Dave Jones2f651682007-01-25 15:56:15 -0500102static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200104static inline long firmware_loading_timeout(void)
105{
Ming Lei68ff2a02015-01-13 00:02:01 +0800106 return loading_timeout > 0 ? loading_timeout * HZ : MAX_JIFFY_OFFSET;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200107}
108
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100109/*
110 * Concurrent request_firmware() for the same firmware need to be
111 * serialized. struct fw_state is simple state machine which hold the
112 * state of the firmware loading.
113 */
114struct fw_state {
Daniel Wagner5b029622016-11-17 11:00:50 +0100115 struct swait_queue_head wq;
Daniel Wagner0430caf2016-11-17 11:00:49 +0100116 enum fw_status status;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100117};
118
119static void fw_state_init(struct fw_state *fw_st)
120{
Daniel Wagner5b029622016-11-17 11:00:50 +0100121 init_swait_queue_head(&fw_st->wq);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100122 fw_st->status = FW_STATUS_UNKNOWN;
123}
124
Daniel Wagner5b029622016-11-17 11:00:50 +0100125static inline bool __fw_state_is_done(enum fw_status status)
126{
127 return status == FW_STATUS_DONE || status == FW_STATUS_ABORTED;
128}
129
Bjorn Andersson5d47ec02016-12-06 17:01:45 -0800130static int __fw_state_wait_common(struct fw_state *fw_st, long timeout)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100131{
132 long ret;
133
Daniel Wagner5b029622016-11-17 11:00:50 +0100134 ret = swait_event_interruptible_timeout(fw_st->wq,
135 __fw_state_is_done(READ_ONCE(fw_st->status)),
136 timeout);
137 if (ret != 0 && fw_st->status == FW_STATUS_ABORTED)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100138 return -ENOENT;
Bjorn Andersson5d47ec02016-12-06 17:01:45 -0800139 if (!ret)
140 return -ETIMEDOUT;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100141
Bjorn Andersson5d47ec02016-12-06 17:01:45 -0800142 return ret < 0 ? ret : 0;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100143}
144
145static void __fw_state_set(struct fw_state *fw_st,
146 enum fw_status status)
147{
Daniel Wagner0430caf2016-11-17 11:00:49 +0100148 WRITE_ONCE(fw_st->status, status);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100149
Daniel Wagner0430caf2016-11-17 11:00:49 +0100150 if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED)
Daniel Wagner5b029622016-11-17 11:00:50 +0100151 swake_up(&fw_st->wq);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100152}
153
154#define fw_state_start(fw_st) \
155 __fw_state_set(fw_st, FW_STATUS_LOADING)
156#define fw_state_done(fw_st) \
157 __fw_state_set(fw_st, FW_STATUS_DONE)
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
161#ifndef CONFIG_FW_LOADER_USER_HELPER
162
163#define fw_state_is_aborted(fw_st) false
164
165#else /* CONFIG_FW_LOADER_USER_HELPER */
166
Daniel Wagnerfab82cb2016-11-17 11:00:51 +0100167static int __fw_state_check(struct fw_state *fw_st, enum fw_status status)
168{
169 return fw_st->status == status;
170}
171
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100172#define fw_state_aborted(fw_st) \
173 __fw_state_set(fw_st, FW_STATUS_ABORTED)
Daniel Wagnerfab82cb2016-11-17 11:00:51 +0100174#define fw_state_is_done(fw_st) \
175 __fw_state_check(fw_st, FW_STATUS_DONE)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100176#define fw_state_is_loading(fw_st) \
177 __fw_state_check(fw_st, FW_STATUS_LOADING)
178#define fw_state_is_aborted(fw_st) \
179 __fw_state_check(fw_st, FW_STATUS_ABORTED)
180#define fw_state_wait_timeout(fw_st, timeout) \
181 __fw_state_wait_common(fw_st, timeout)
182
183#endif /* CONFIG_FW_LOADER_USER_HELPER */
184
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100185/* firmware behavior options */
186#define FW_OPT_UEVENT (1U << 0)
187#define FW_OPT_NOWAIT (1U << 1)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100188#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200189#define FW_OPT_USERHELPER (1U << 2)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100190#else
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200191#define FW_OPT_USERHELPER 0
192#endif
193#ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK
194#define FW_OPT_FALLBACK FW_OPT_USERHELPER
195#else
196#define FW_OPT_FALLBACK 0
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100197#endif
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -0700198#define FW_OPT_NO_WARN (1U << 3)
Vikram Mulukutla0e742e92016-08-02 14:04:25 -0700199#define FW_OPT_NOCACHE (1U << 4)
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100200
Ming Lei1f2b7952012-08-04 12:01:21 +0800201struct firmware_cache {
202 /* firmware_buf instance will be added into the below list */
203 spinlock_t lock;
204 struct list_head head;
Ming Leicfe016b2012-09-08 17:32:30 +0800205 int state;
Ming Lei37276a52012-08-04 12:01:27 +0800206
Ming Leicfe016b2012-09-08 17:32:30 +0800207#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +0800208 /*
209 * Names of firmware images which have been cached successfully
210 * will be added into the below list so that device uncache
211 * helper can trace which firmware images have been cached
212 * before.
213 */
214 spinlock_t name_lock;
215 struct list_head fw_names;
216
Ming Lei37276a52012-08-04 12:01:27 +0800217 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +0800218
219 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +0800220#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800221};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Ming Lei12446912012-08-04 12:01:20 +0800223struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800224 struct kref ref;
225 struct list_head list;
Ming Lei1f2b7952012-08-04 12:01:21 +0800226 struct firmware_cache *fwc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100227 struct fw_state fw_st;
Ming Lei65710cb2012-08-04 12:01:16 +0800228 void *data;
229 size_t size;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700230 size_t allocated_size;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100231#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100232 bool is_paged_buf;
Ming Leiaf5bc112013-05-27 18:30:04 +0800233 bool need_uevent;
David Woodhouse6e03a202009-04-09 22:04:07 -0700234 struct page **pages;
235 int nr_pages;
236 int page_array_size;
Takashi Iwaife304142013-05-22 18:28:38 +0200237 struct list_head pending_list;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100238#endif
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700239 const char *fw_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240};
241
Ming Lei37276a52012-08-04 12:01:27 +0800242struct fw_cache_entry {
243 struct list_head list;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700244 const char *name;
Ming Lei37276a52012-08-04 12:01:27 +0800245};
246
Ming Leif531f05a2012-08-04 12:01:25 +0800247struct fw_name_devm {
248 unsigned long magic;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700249 const char *name;
Ming Leif531f05a2012-08-04 12:01:25 +0800250};
251
Ming Lei1f2b7952012-08-04 12:01:21 +0800252#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
253
Ming Leiac39b3e2012-08-20 19:04:16 +0800254#define FW_LOADER_NO_CACHE 0
255#define FW_LOADER_START_CACHE 1
256
257static int fw_cache_piggyback_on_request(const char *name);
258
Ming Lei1f2b7952012-08-04 12:01:21 +0800259/* fw_lock could be moved to 'struct firmware_priv' but since it is just
260 * guarding for corner cases a global lock should be OK */
261static DEFINE_MUTEX(fw_lock);
262
263static struct firmware_cache fw_cache;
264
265static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
Stephen Boyda098ecd2016-08-02 14:04:28 -0700266 struct firmware_cache *fwc,
267 void *dbuf, size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800268{
269 struct firmware_buf *buf;
270
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700271 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
Ming Lei1f2b7952012-08-04 12:01:21 +0800272 if (!buf)
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700273 return NULL;
274
275 buf->fw_id = kstrdup_const(fw_name, GFP_ATOMIC);
276 if (!buf->fw_id) {
277 kfree(buf);
278 return NULL;
279 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800280
281 kref_init(&buf->ref);
Ming Lei1f2b7952012-08-04 12:01:21 +0800282 buf->fwc = fwc;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700283 buf->data = dbuf;
284 buf->allocated_size = size;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100285 fw_state_init(&buf->fw_st);
Takashi Iwaife304142013-05-22 18:28:38 +0200286#ifdef CONFIG_FW_LOADER_USER_HELPER
287 INIT_LIST_HEAD(&buf->pending_list);
288#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800289
290 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
291
292 return buf;
293}
294
Ming Lei2887b392012-08-04 12:01:22 +0800295static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
296{
297 struct firmware_buf *tmp;
298 struct firmware_cache *fwc = &fw_cache;
299
300 list_for_each_entry(tmp, &fwc->head, list)
301 if (!strcmp(tmp->fw_id, fw_name))
302 return tmp;
303 return NULL;
304}
305
Ming Lei1f2b7952012-08-04 12:01:21 +0800306static int fw_lookup_and_allocate_buf(const char *fw_name,
307 struct firmware_cache *fwc,
Stephen Boyda098ecd2016-08-02 14:04:28 -0700308 struct firmware_buf **buf, void *dbuf,
309 size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800310{
311 struct firmware_buf *tmp;
312
313 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800314 tmp = __fw_lookup_buf(fw_name);
315 if (tmp) {
316 kref_get(&tmp->ref);
317 spin_unlock(&fwc->lock);
318 *buf = tmp;
319 return 1;
320 }
Stephen Boyda098ecd2016-08-02 14:04:28 -0700321 tmp = __allocate_fw_buf(fw_name, fwc, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +0800322 if (tmp)
323 list_add(&tmp->list, &fwc->head);
324 spin_unlock(&fwc->lock);
325
326 *buf = tmp;
327
328 return tmp ? 0 : -ENOMEM;
329}
330
331static void __fw_free_buf(struct kref *ref)
Bart Van Assche98233b22014-01-04 14:20:36 +0100332 __releases(&fwc->lock)
Ming Lei1f2b7952012-08-04 12:01:21 +0800333{
334 struct firmware_buf *buf = to_fwbuf(ref);
335 struct firmware_cache *fwc = buf->fwc;
Ming Lei1f2b7952012-08-04 12:01:21 +0800336
337 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
338 __func__, buf->fw_id, buf, buf->data,
339 (unsigned int)buf->size);
340
Ming Lei1f2b7952012-08-04 12:01:21 +0800341 list_del(&buf->list);
342 spin_unlock(&fwc->lock);
343
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100344#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100345 if (buf->is_paged_buf) {
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100346 int i;
Ming Lei746058f2012-10-09 12:01:03 +0800347 vunmap(buf->data);
348 for (i = 0; i < buf->nr_pages; i++)
349 __free_page(buf->pages[i]);
Chen Feng10a3fbf12015-12-16 17:03:15 +0800350 vfree(buf->pages);
Ming Lei746058f2012-10-09 12:01:03 +0800351 } else
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100352#endif
Stephen Boyda098ecd2016-08-02 14:04:28 -0700353 if (!buf->allocated_size)
Ming Lei746058f2012-10-09 12:01:03 +0800354 vfree(buf->data);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700355 kfree_const(buf->fw_id);
Ming Lei1f2b7952012-08-04 12:01:21 +0800356 kfree(buf);
357}
358
359static void fw_free_buf(struct firmware_buf *buf)
360{
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800361 struct firmware_cache *fwc = buf->fwc;
362 spin_lock(&fwc->lock);
363 if (!kref_put(&buf->ref, __fw_free_buf))
364 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800365}
366
Ming Lei746058f2012-10-09 12:01:03 +0800367/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800368static char fw_path_para[256];
369static const char * const fw_path[] = {
370 fw_path_para,
Ming Lei746058f2012-10-09 12:01:03 +0800371 "/lib/firmware/updates/" UTS_RELEASE,
372 "/lib/firmware/updates",
373 "/lib/firmware/" UTS_RELEASE,
374 "/lib/firmware"
375};
376
Ming Lei27602842012-11-03 17:47:58 +0800377/*
378 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
379 * from kernel command line because firmware_class is generally built in
380 * kernel instead of module.
381 */
382module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
383MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
384
Stephen Boyda098ecd2016-08-02 14:04:28 -0700385static int
386fw_get_filesystem_firmware(struct device *device, struct firmware_buf *buf)
Ming Lei746058f2012-10-09 12:01:03 +0800387{
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500388 loff_t size;
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700389 int i, len;
Neil Horman3e358ac2013-09-06 15:36:08 -0400390 int rc = -ENOENT;
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700391 char *path;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700392 enum kernel_read_file_id id = READING_FIRMWARE;
393 size_t msize = INT_MAX;
394
395 /* Already populated data member means we're loading into a buffer */
396 if (buf->data) {
397 id = READING_FIRMWARE_PREALLOC_BUFFER;
398 msize = buf->allocated_size;
399 }
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700400
401 path = __getname();
402 if (!path)
403 return -ENOMEM;
Ming Lei746058f2012-10-09 12:01:03 +0800404
405 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
Ming Lei27602842012-11-03 17:47:58 +0800406 /* skip the unset customized path */
407 if (!fw_path[i][0])
408 continue;
409
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700410 len = snprintf(path, PATH_MAX, "%s/%s",
411 fw_path[i], buf->fw_id);
412 if (len >= PATH_MAX) {
413 rc = -ENAMETOOLONG;
414 break;
415 }
Ming Lei746058f2012-10-09 12:01:03 +0800416
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500417 buf->size = 0;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700418 rc = kernel_read_file_from_path(path, &buf->data, &size, msize,
419 id);
Kees Cook4b2530d2016-02-04 13:15:02 -0800420 if (rc) {
Luis R. Rodriguez8e516aa2016-02-28 21:57:55 +0100421 if (rc == -ENOENT)
422 dev_dbg(device, "loading %s failed with error %d\n",
423 path, rc);
424 else
425 dev_warn(device, "loading %s failed with error %d\n",
426 path, rc);
Kees Cook4b2530d2016-02-04 13:15:02 -0800427 continue;
428 }
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500429 dev_dbg(device, "direct-loading %s\n", buf->fw_id);
430 buf->size = size;
Daniel Wagner5b029622016-11-17 11:00:50 +0100431 fw_state_done(&buf->fw_st);
Kees Cook4b2530d2016-02-04 13:15:02 -0800432 break;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100433 }
Kees Cook4b2530d2016-02-04 13:15:02 -0800434 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100435
Neil Horman3e358ac2013-09-06 15:36:08 -0400436 return rc;
Ming Lei746058f2012-10-09 12:01:03 +0800437}
438
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100439/* firmware holds the ownership of pages */
440static void firmware_free_data(const struct firmware *fw)
441{
442 /* Loaded directly? */
443 if (!fw->priv) {
444 vfree(fw->data);
445 return;
446 }
447 fw_free_buf(fw->priv);
448}
449
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100450/* store the pages buffer info firmware from buf */
451static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
452{
453 fw->priv = buf;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100454#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100455 fw->pages = buf->pages;
456#endif
457 fw->size = buf->size;
458 fw->data = buf->data;
459
460 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
461 __func__, buf->fw_id, buf, buf->data,
462 (unsigned int)buf->size);
463}
464
465#ifdef CONFIG_PM_SLEEP
466static void fw_name_devm_release(struct device *dev, void *res)
467{
468 struct fw_name_devm *fwn = res;
469
470 if (fwn->magic == (unsigned long)&fw_cache)
471 pr_debug("%s: fw_name-%s devm-%p released\n",
472 __func__, fwn->name, res);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700473 kfree_const(fwn->name);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100474}
475
476static int fw_devm_match(struct device *dev, void *res,
477 void *match_data)
478{
479 struct fw_name_devm *fwn = res;
480
481 return (fwn->magic == (unsigned long)&fw_cache) &&
482 !strcmp(fwn->name, match_data);
483}
484
485static struct fw_name_devm *fw_find_devm_name(struct device *dev,
486 const char *name)
487{
488 struct fw_name_devm *fwn;
489
490 fwn = devres_find(dev, fw_name_devm_release,
491 fw_devm_match, (void *)name);
492 return fwn;
493}
494
495/* add firmware name into devres list */
496static int fw_add_devm_name(struct device *dev, const char *name)
497{
498 struct fw_name_devm *fwn;
499
500 fwn = fw_find_devm_name(dev, name);
501 if (fwn)
502 return 1;
503
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700504 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
505 GFP_KERNEL);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100506 if (!fwn)
507 return -ENOMEM;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700508 fwn->name = kstrdup_const(name, GFP_KERNEL);
509 if (!fwn->name) {
Vladimir Zapolskiya885de62015-07-29 23:26:28 +0300510 devres_free(fwn);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700511 return -ENOMEM;
512 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100513
514 fwn->magic = (unsigned long)&fw_cache;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100515 devres_add(dev, fwn);
516
517 return 0;
518}
519#else
520static int fw_add_devm_name(struct device *dev, const char *name)
521{
522 return 0;
523}
524#endif
525
526
527/*
528 * user-mode helper code
529 */
530#ifdef CONFIG_FW_LOADER_USER_HELPER
531struct firmware_priv {
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100532 bool nowait;
533 struct device dev;
534 struct firmware_buf *buf;
535 struct firmware *fw;
536};
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100537
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700538static struct firmware_priv *to_firmware_priv(struct device *dev)
539{
540 return container_of(dev, struct firmware_priv, dev);
541}
542
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700543static void __fw_load_abort(struct firmware_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Ming Lei87597932013-06-15 16:36:38 +0800545 /*
546 * There is a small window in which user can write to 'loading'
547 * between loading done and disappearance of 'loading'
548 */
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100549 if (fw_state_is_done(&buf->fw_st))
Ming Lei87597932013-06-15 16:36:38 +0800550 return;
551
Takashi Iwaife304142013-05-22 18:28:38 +0200552 list_del_init(&buf->pending_list);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100553 fw_state_aborted(&buf->fw_st);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700556static void fw_load_abort(struct firmware_priv *fw_priv)
557{
558 struct firmware_buf *buf = fw_priv->buf;
559
560 __fw_load_abort(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Takashi Iwaife304142013-05-22 18:28:38 +0200563static LIST_HEAD(pending_fw_head);
564
565/* reboot notifier for avoid deadlock with usermode_lock */
566static int fw_shutdown_notify(struct notifier_block *unused1,
567 unsigned long unused2, void *unused3)
568{
569 mutex_lock(&fw_lock);
570 while (!list_empty(&pending_fw_head))
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700571 __fw_load_abort(list_first_entry(&pending_fw_head,
Takashi Iwaife304142013-05-22 18:28:38 +0200572 struct firmware_buf,
573 pending_list));
574 mutex_unlock(&fw_lock);
575 return NOTIFY_DONE;
576}
577
578static struct notifier_block fw_shutdown_nb = {
579 .notifier_call = fw_shutdown_notify,
580};
581
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700582static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
583 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 return sprintf(buf, "%d\n", loading_timeout);
586}
587
588/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800589 * firmware_timeout_store - set number of seconds to wait for firmware
590 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800591 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800592 * @buf: buffer to scan for timeout value
593 * @count: number of bytes in @buf
594 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800596 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 * firmware will be provided.
598 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800599 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 **/
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700601static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
602 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700605 if (loading_timeout < 0)
606 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return count;
609}
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100610static CLASS_ATTR_RW(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100612static struct attribute *firmware_class_attrs[] = {
613 &class_attr_timeout.attr,
614 NULL,
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800615};
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100616ATTRIBUTE_GROUPS(firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800618static void fw_dev_release(struct device *dev)
619{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700620 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800621
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800622 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800623}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Linus Torvalds6f957722015-07-09 11:20:01 -0700625static int do_firmware_uevent(struct firmware_priv *fw_priv, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Ming Lei12446912012-08-04 12:01:20 +0800627 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200629 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700630 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200631 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
632 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 return 0;
635}
636
Linus Torvalds6f957722015-07-09 11:20:01 -0700637static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
638{
639 struct firmware_priv *fw_priv = to_firmware_priv(dev);
640 int err = 0;
641
642 mutex_lock(&fw_lock);
643 if (fw_priv->buf)
644 err = do_firmware_uevent(fw_priv, env);
645 mutex_unlock(&fw_lock);
646 return err;
647}
648
Adrian Bunk1b81d662006-05-20 15:00:16 -0700649static struct class firmware_class = {
650 .name = "firmware",
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100651 .class_groups = firmware_class_groups,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700652 .dev_uevent = firmware_uevent,
653 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700654};
655
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700656static ssize_t firmware_loading_show(struct device *dev,
657 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700659 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800660 int loading = 0;
661
662 mutex_lock(&fw_lock);
663 if (fw_priv->buf)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100664 loading = fw_state_is_loading(&fw_priv->buf->fw_st);
Ming Lei87597932013-06-15 16:36:38 +0800665 mutex_unlock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return sprintf(buf, "%d\n", loading);
668}
669
David Woodhouse6e03a202009-04-09 22:04:07 -0700670/* Some architectures don't have PAGE_KERNEL_RO */
671#ifndef PAGE_KERNEL_RO
672#define PAGE_KERNEL_RO PAGE_KERNEL
673#endif
Ming Lei253c9242012-10-09 12:01:02 +0800674
675/* one pages buffer should be mapped/unmapped only once */
676static int fw_map_pages_buf(struct firmware_buf *buf)
677{
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100678 if (!buf->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800679 return 0;
680
Markus Elfringdaa3d672014-11-19 11:38:38 +0100681 vunmap(buf->data);
Ming Lei253c9242012-10-09 12:01:02 +0800682 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
683 if (!buf->data)
684 return -ENOMEM;
685 return 0;
686}
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800689 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700690 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800691 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800692 * @buf: buffer to scan for loading control value
693 * @count: number of bytes in @buf
694 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 * The relevant values are:
696 *
697 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800698 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 * -1: Conclude the load with an error and discard any written data.
700 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700701static ssize_t firmware_loading_store(struct device *dev,
702 struct device_attribute *attr,
703 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700705 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800706 struct firmware_buf *fw_buf;
Kees Cook6593d922014-02-25 13:06:00 -0800707 ssize_t written = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700709 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Neil Hormaneea915b2012-01-02 15:31:23 -0500711 mutex_lock(&fw_lock);
Ming Lei87597932013-06-15 16:36:38 +0800712 fw_buf = fw_priv->buf;
Luis R. Rodriguez191e8852017-01-25 10:31:52 -0800713 if (fw_state_is_aborted(&fw_buf->fw_st))
Neil Hormaneea915b2012-01-02 15:31:23 -0500714 goto out;
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 switch (loading) {
717 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800718 /* discarding any previous partial load */
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100719 if (!fw_state_is_done(&fw_buf->fw_st)) {
Ming Lei12446912012-08-04 12:01:20 +0800720 for (i = 0; i < fw_buf->nr_pages; i++)
721 __free_page(fw_buf->pages[i]);
Chen Feng10a3fbf12015-12-16 17:03:15 +0800722 vfree(fw_buf->pages);
Ming Lei12446912012-08-04 12:01:20 +0800723 fw_buf->pages = NULL;
724 fw_buf->page_array_size = 0;
725 fw_buf->nr_pages = 0;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100726 fw_state_start(&fw_buf->fw_st);
Ming Lei28eefa72012-08-04 12:01:17 +0800727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 break;
729 case 0:
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100730 if (fw_state_is_loading(&fw_buf->fw_st)) {
Kees Cook6593d922014-02-25 13:06:00 -0800731 int rc;
732
Ming Lei253c9242012-10-09 12:01:02 +0800733 /*
734 * Several loading requests may be pending on
735 * one same firmware buf, so let all requests
736 * see the mapped 'buf->data' once the loading
737 * is completed.
738 * */
Kees Cook6593d922014-02-25 13:06:00 -0800739 rc = fw_map_pages_buf(fw_buf);
740 if (rc)
zhang jun2b1278c2014-02-13 15:18:47 +0800741 dev_err(dev, "%s: map pages failed\n",
742 __func__);
Kees Cook6593d922014-02-25 13:06:00 -0800743 else
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500744 rc = security_kernel_post_read_file(NULL,
745 fw_buf->data, fw_buf->size,
746 READING_FIRMWARE);
Kees Cook6593d922014-02-25 13:06:00 -0800747
748 /*
749 * Same logic as fw_load_abort, only the DONE bit
750 * is ignored and we set ABORT only on failure.
751 */
Takashi Iwaife304142013-05-22 18:28:38 +0200752 list_del_init(&fw_buf->pending_list);
Kees Cook6593d922014-02-25 13:06:00 -0800753 if (rc) {
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100754 fw_state_aborted(&fw_buf->fw_st);
Kees Cook6593d922014-02-25 13:06:00 -0800755 written = rc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100756 } else {
757 fw_state_done(&fw_buf->fw_st);
Kees Cook6593d922014-02-25 13:06:00 -0800758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 break;
760 }
761 /* fallthrough */
762 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700763 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 /* fallthrough */
765 case -1:
766 fw_load_abort(fw_priv);
767 break;
768 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500769out:
770 mutex_unlock(&fw_lock);
Kees Cook6593d922014-02-25 13:06:00 -0800771 return written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700774static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Stephen Boyda098ecd2016-08-02 14:04:28 -0700776static void firmware_rw_buf(struct firmware_buf *buf, char *buffer,
777 loff_t offset, size_t count, bool read)
778{
779 if (read)
780 memcpy(buffer, buf->data + offset, count);
781 else
782 memcpy(buf->data + offset, buffer, count);
783}
784
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700785static void firmware_rw(struct firmware_buf *buf, char *buffer,
786 loff_t offset, size_t count, bool read)
787{
788 while (count) {
789 void *page_data;
790 int page_nr = offset >> PAGE_SHIFT;
791 int page_ofs = offset & (PAGE_SIZE-1);
792 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
793
794 page_data = kmap(buf->pages[page_nr]);
795
796 if (read)
797 memcpy(buffer, page_data + page_ofs, page_cnt);
798 else
799 memcpy(page_data + page_ofs, buffer, page_cnt);
800
801 kunmap(buf->pages[page_nr]);
802 buffer += page_cnt;
803 offset += page_cnt;
804 count -= page_cnt;
805 }
806}
807
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700808static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
809 struct bin_attribute *bin_attr,
810 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200812 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700813 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800814 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700815 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Laura Garciacad1e552006-05-23 23:22:38 +0200817 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800818 buf = fw_priv->buf;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100819 if (!buf || fw_state_is_done(&buf->fw_st)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 ret_count = -ENODEV;
821 goto out;
822 }
Ming Lei12446912012-08-04 12:01:20 +0800823 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200824 ret_count = 0;
825 goto out;
826 }
Ming Lei12446912012-08-04 12:01:20 +0800827 if (count > buf->size - offset)
828 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700829
830 ret_count = count;
831
Stephen Boyda098ecd2016-08-02 14:04:28 -0700832 if (buf->data)
833 firmware_rw_buf(buf, buffer, offset, count, true);
834 else
835 firmware_rw(buf, buffer, offset, count, true);
David Woodhouse6e03a202009-04-09 22:04:07 -0700836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837out:
Laura Garciacad1e552006-05-23 23:22:38 +0200838 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return ret_count;
840}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800841
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700842static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Ming Lei12446912012-08-04 12:01:20 +0800844 struct firmware_buf *buf = fw_priv->buf;
Fabian Fredericka76040d2014-07-01 21:13:30 +0200845 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
David Woodhouse6e03a202009-04-09 22:04:07 -0700847 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800848 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700849 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800850 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700851 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Chen Feng10a3fbf12015-12-16 17:03:15 +0800853 new_pages = vmalloc(new_array_size * sizeof(void *));
David Woodhouse6e03a202009-04-09 22:04:07 -0700854 if (!new_pages) {
855 fw_load_abort(fw_priv);
856 return -ENOMEM;
857 }
Ming Lei12446912012-08-04 12:01:20 +0800858 memcpy(new_pages, buf->pages,
859 buf->page_array_size * sizeof(void *));
860 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
861 (new_array_size - buf->page_array_size));
Chen Feng10a3fbf12015-12-16 17:03:15 +0800862 vfree(buf->pages);
Ming Lei12446912012-08-04 12:01:20 +0800863 buf->pages = new_pages;
864 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700866
Ming Lei12446912012-08-04 12:01:20 +0800867 while (buf->nr_pages < pages_needed) {
868 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700869 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
870
Ming Lei12446912012-08-04 12:01:20 +0800871 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700872 fw_load_abort(fw_priv);
873 return -ENOMEM;
874 }
Ming Lei12446912012-08-04 12:01:20 +0800875 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return 0;
878}
879
880/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800881 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700882 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700883 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700884 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800885 * @buffer: buffer being written
886 * @offset: buffer offset for write in total data store area
887 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800889 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 * the driver as a firmware image.
891 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700892static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
893 struct bin_attribute *bin_attr,
894 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200896 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700897 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800898 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 ssize_t retval;
900
901 if (!capable(CAP_SYS_RAWIO))
902 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700903
Laura Garciacad1e552006-05-23 23:22:38 +0200904 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800905 buf = fw_priv->buf;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100906 if (!buf || fw_state_is_done(&buf->fw_st)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 retval = -ENODEV;
908 goto out;
909 }
Ming Lei65710cb2012-08-04 12:01:16 +0800910
Stephen Boyda098ecd2016-08-02 14:04:28 -0700911 if (buf->data) {
912 if (offset + count > buf->allocated_size) {
913 retval = -ENOMEM;
914 goto out;
915 }
916 firmware_rw_buf(buf, buffer, offset, count, false);
917 retval = count;
918 } else {
919 retval = fw_realloc_buffer(fw_priv, offset + count);
920 if (retval)
921 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Stephen Boyda098ecd2016-08-02 14:04:28 -0700923 retval = count;
924 firmware_rw(buf, buffer, offset, count, false);
925 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700926
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700927 buf->size = max_t(size_t, offset + count, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928out:
Laura Garciacad1e552006-05-23 23:22:38 +0200929 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return retval;
931}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800932
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700933static struct bin_attribute firmware_attr_data = {
934 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 .size = 0,
936 .read = firmware_data_read,
937 .write = firmware_data_write,
938};
939
Takashi Iwai46239902015-02-04 15:18:07 +0100940static struct attribute *fw_dev_attrs[] = {
941 &dev_attr_loading.attr,
942 NULL
943};
944
945static struct bin_attribute *fw_dev_bin_attrs[] = {
946 &firmware_attr_data,
947 NULL
948};
949
950static const struct attribute_group fw_dev_attr_group = {
951 .attrs = fw_dev_attrs,
952 .bin_attrs = fw_dev_bin_attrs,
953};
954
955static const struct attribute_group *fw_dev_attr_groups[] = {
956 &fw_dev_attr_group,
957 NULL
958};
959
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700960static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200961fw_create_instance(struct firmware *firmware, const char *fw_name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100962 struct device *device, unsigned int opt_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700964 struct firmware_priv *fw_priv;
965 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Ming Lei12446912012-08-04 12:01:20 +0800967 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700968 if (!fw_priv) {
Ming Lei12446912012-08-04 12:01:20 +0800969 fw_priv = ERR_PTR(-ENOMEM);
970 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100973 fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT);
Ming Lei1f2b7952012-08-04 12:01:21 +0800974 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700975 f_dev = &fw_priv->dev;
976
977 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800978 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700979 f_dev->parent = device;
980 f_dev->class = &firmware_class;
Takashi Iwai46239902015-02-04 15:18:07 +0100981 f_dev->groups = fw_dev_attr_groups;
Ming Lei12446912012-08-04 12:01:20 +0800982exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700983 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100985
986/* load a firmware via user helper */
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100987static int _request_firmware_load(struct firmware_priv *fw_priv,
988 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100989{
990 int retval = 0;
991 struct device *f_dev = &fw_priv->dev;
992 struct firmware_buf *buf = fw_priv->buf;
993
994 /* fall back on userspace loading */
Stephen Boyda098ecd2016-08-02 14:04:28 -0700995 if (!buf->data)
996 buf->is_paged_buf = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100997
998 dev_set_uevent_suppress(f_dev, true);
999
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001000 retval = device_add(f_dev);
1001 if (retval) {
1002 dev_err(f_dev, "%s: device_register failed\n", __func__);
1003 goto err_put_dev;
1004 }
1005
Maxime Bizon1eeeef12013-08-29 20:28:13 +02001006 mutex_lock(&fw_lock);
1007 list_add(&buf->pending_list, &pending_fw_head);
1008 mutex_unlock(&fw_lock);
1009
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001010 if (opt_flags & FW_OPT_UEVENT) {
Ming Leiaf5bc112013-05-27 18:30:04 +08001011 buf->need_uevent = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001012 dev_set_uevent_suppress(f_dev, false);
1013 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001014 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
Ming Lei68ff2a02015-01-13 00:02:01 +08001015 } else {
1016 timeout = MAX_JIFFY_OFFSET;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001017 }
1018
Bjorn Andersson5d47ec02016-12-06 17:01:45 -08001019 retval = fw_state_wait_timeout(&buf->fw_st, timeout);
1020 if (retval < 0) {
Ming Lei0cb64242015-01-13 00:01:35 +08001021 mutex_lock(&fw_lock);
1022 fw_load_abort(fw_priv);
1023 mutex_unlock(&fw_lock);
1024 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001025
Daniel Wagnerf52cc3792016-11-17 11:00:48 +01001026 if (fw_state_is_aborted(&buf->fw_st))
Shuah Khan0542ad82014-07-22 18:10:38 -06001027 retval = -EAGAIN;
Stephen Boyda098ecd2016-08-02 14:04:28 -07001028 else if (buf->is_paged_buf && !buf->data)
zhang jun2b1278c2014-02-13 15:18:47 +08001029 retval = -ENOMEM;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001030
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001031 device_del(f_dev);
1032err_put_dev:
1033 put_device(f_dev);
1034 return retval;
1035}
1036
1037static int fw_load_from_user_helper(struct firmware *firmware,
1038 const char *name, struct device *device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001039 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001040{
1041 struct firmware_priv *fw_priv;
1042
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001043 fw_priv = fw_create_instance(firmware, name, device, opt_flags);
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001044 if (IS_ERR(fw_priv))
1045 return PTR_ERR(fw_priv);
1046
1047 fw_priv->buf = firmware->priv;
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001048 return _request_firmware_load(fw_priv, opt_flags, timeout);
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001049}
Ming Leiddf1f062013-06-04 10:01:13 +08001050
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001051#ifdef CONFIG_PM_SLEEP
Ming Leiddf1f062013-06-04 10:01:13 +08001052/* kill pending requests without uevent to avoid blocking suspend */
1053static void kill_requests_without_uevent(void)
1054{
1055 struct firmware_buf *buf;
1056 struct firmware_buf *next;
1057
1058 mutex_lock(&fw_lock);
1059 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
1060 if (!buf->need_uevent)
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -07001061 __fw_load_abort(buf);
Ming Leiddf1f062013-06-04 10:01:13 +08001062 }
1063 mutex_unlock(&fw_lock);
1064}
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001065#endif
Ming Leiddf1f062013-06-04 10:01:13 +08001066
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001067#else /* CONFIG_FW_LOADER_USER_HELPER */
1068static inline int
1069fw_load_from_user_helper(struct firmware *firmware, const char *name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001070 struct device *device, unsigned int opt_flags,
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001071 long timeout)
1072{
1073 return -ENOENT;
1074}
Takashi Iwai807be032013-01-31 11:13:57 +01001075
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001076#ifdef CONFIG_PM_SLEEP
Ming Leiddf1f062013-06-04 10:01:13 +08001077static inline void kill_requests_without_uevent(void) { }
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001078#endif
Ming Leiddf1f062013-06-04 10:01:13 +08001079
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001080#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001082/* prepare firmware and firmware_buf structs;
1083 * return 0 if a firmware is already assigned, 1 if need to load one,
1084 * or a negative error code
1085 */
1086static int
1087_request_firmware_prepare(struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001088 struct device *device, void *dbuf, size_t size)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001089{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +08001091 struct firmware_buf *buf;
1092 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Jiri Slaby4aed0642005-09-13 01:25:01 -07001094 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -07001096 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1097 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001098 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Stephen Boyda098ecd2016-08-02 14:04:28 -07001101 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
Luis R. Rodriguezed046302015-04-29 16:30:43 -07001102 dev_dbg(device, "using built-in %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001103 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +01001104 }
1105
Stephen Boyda098ecd2016-08-02 14:04:28 -07001106 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +08001107
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001108 /*
1109 * bind with 'buf' now to avoid warning in failure path
1110 * of requesting firmware.
1111 */
1112 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +08001113
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001114 if (ret > 0) {
Daniel Wagner5b029622016-11-17 11:00:50 +01001115 ret = fw_state_wait(&buf->fw_st);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001116 if (!ret) {
1117 fw_set_page_data(buf, firmware);
1118 return 0; /* assigned */
1119 }
Stephen Boyddddb5542012-03-28 23:30:43 +02001120 }
Ming Lei1f2b7952012-08-04 12:01:21 +08001121
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001122 if (ret < 0)
1123 return ret;
1124 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001125}
1126
Ming Leie771d1a2013-05-27 18:30:03 +08001127static int assign_firmware_buf(struct firmware *fw, struct device *device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001128 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001129{
1130 struct firmware_buf *buf = fw->priv;
1131
1132 mutex_lock(&fw_lock);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +01001133 if (!buf->size || fw_state_is_aborted(&buf->fw_st)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001134 mutex_unlock(&fw_lock);
1135 return -ENOENT;
1136 }
1137
1138 /*
1139 * add firmware name into devres list so that we can auto cache
1140 * and uncache firmware for device.
1141 *
1142 * device may has been deleted already, but the problem
1143 * should be fixed in devres or driver core.
1144 */
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001145 /* don't cache firmware handled without uevent */
Vikram Mulukutla0e742e92016-08-02 14:04:25 -07001146 if (device && (opt_flags & FW_OPT_UEVENT) &&
1147 !(opt_flags & FW_OPT_NOCACHE))
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001148 fw_add_devm_name(device, buf->fw_id);
1149
1150 /*
1151 * After caching firmware image is started, let it piggyback
1152 * on request firmware.
1153 */
Vikram Mulukutla0e742e92016-08-02 14:04:25 -07001154 if (!(opt_flags & FW_OPT_NOCACHE) &&
1155 buf->fwc->state == FW_LOADER_START_CACHE) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001156 if (fw_cache_piggyback_on_request(buf->fw_id))
1157 kref_get(&buf->ref);
1158 }
1159
1160 /* pass the pages buffer to driver at the last minute */
1161 fw_set_page_data(buf, fw);
1162 mutex_unlock(&fw_lock);
1163 return 0;
1164}
1165
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001166/* called from request_firmware() and request_firmware_work_func() */
1167static int
1168_request_firmware(const struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001169 struct device *device, void *buf, size_t size,
1170 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001171{
Brian Norris715780a2015-12-09 14:50:28 -08001172 struct firmware *fw = NULL;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001173 long timeout;
1174 int ret;
1175
1176 if (!firmware_p)
1177 return -EINVAL;
1178
Brian Norris715780a2015-12-09 14:50:28 -08001179 if (!name || name[0] == '\0') {
1180 ret = -EINVAL;
1181 goto out;
1182 }
Kees Cook471b0952014-09-18 11:25:37 -07001183
Stephen Boyda098ecd2016-08-02 14:04:28 -07001184 ret = _request_firmware_prepare(&fw, name, device, buf, size);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001185 if (ret <= 0) /* error or already assigned */
1186 goto out;
1187
1188 ret = 0;
1189 timeout = firmware_loading_timeout();
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001190 if (opt_flags & FW_OPT_NOWAIT) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001191 timeout = usermodehelper_read_lock_wait(timeout);
1192 if (!timeout) {
1193 dev_dbg(device, "firmware: %s loading timed out\n",
1194 name);
1195 ret = -EBUSY;
1196 goto out;
1197 }
1198 } else {
1199 ret = usermodehelper_read_trylock();
1200 if (WARN_ON(ret)) {
1201 dev_err(device, "firmware: %s will not be loaded\n",
1202 name);
1203 goto out;
1204 }
1205 }
1206
Neil Horman3e358ac2013-09-06 15:36:08 -04001207 ret = fw_get_filesystem_firmware(device, fw->priv);
1208 if (ret) {
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001209 if (!(opt_flags & FW_OPT_NO_WARN))
Takashi Iwaibba3a872013-12-02 15:38:16 +01001210 dev_warn(device,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001211 "Direct firmware load for %s failed with error %d\n",
1212 name, ret);
1213 if (opt_flags & FW_OPT_USERHELPER) {
Takashi Iwaibba3a872013-12-02 15:38:16 +01001214 dev_warn(device, "Falling back to user helper\n");
1215 ret = fw_load_from_user_helper(fw, name, device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001216 opt_flags, timeout);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001217 }
Neil Horman3e358ac2013-09-06 15:36:08 -04001218 }
Ming Leie771d1a2013-05-27 18:30:03 +08001219
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001220 if (!ret)
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001221 ret = assign_firmware_buf(fw, device, opt_flags);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001222
1223 usermodehelper_read_unlock();
1224
1225 out:
1226 if (ret < 0) {
1227 release_firmware(fw);
1228 fw = NULL;
1229 }
1230
1231 *firmware_p = fw;
1232 return ret;
1233}
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235/**
Kay Sievers312c0042005-11-16 09:00:00 +01001236 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001237 * @firmware_p: pointer to firmware image
1238 * @name: name of firmware file
1239 * @device: device for which firmware is being loaded
1240 *
1241 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001242 * of @name for device @device.
1243 *
1244 * Should be called from user context where sleeping is allowed.
1245 *
Kay Sievers312c0042005-11-16 09:00:00 +01001246 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001247 * should be distinctive enough not to be confused with any other
1248 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001249 *
1250 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001251 *
1252 * The function can be called safely inside device's suspend and
1253 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001254 **/
1255int
1256request_firmware(const struct firmware **firmware_p, const char *name,
Andrei Opreaea310032015-03-08 12:41:15 +02001257 struct device *device)
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001258{
Ming Leid6c8aa32013-06-06 20:01:48 +08001259 int ret;
1260
1261 /* Need to pin this module until return */
1262 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001263 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001264 FW_OPT_UEVENT | FW_OPT_FALLBACK);
Ming Leid6c8aa32013-06-06 20:01:48 +08001265 module_put(THIS_MODULE);
1266 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001267}
Daniel Mackf4945132013-05-23 22:17:18 +02001268EXPORT_SYMBOL(request_firmware);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001269
Takashi Iwaibba3a872013-12-02 15:38:16 +01001270/**
Borislav Petkov3c1556b2014-12-03 22:46:37 +01001271 * request_firmware_direct: - load firmware directly without usermode helper
Takashi Iwaibba3a872013-12-02 15:38:16 +01001272 * @firmware_p: pointer to firmware image
1273 * @name: name of firmware file
1274 * @device: device for which firmware is being loaded
1275 *
1276 * This function works pretty much like request_firmware(), but this doesn't
1277 * fall back to usermode helper even if the firmware couldn't be loaded
1278 * directly from fs. Hence it's useful for loading optional firmwares, which
1279 * aren't always present, without extra long timeouts of udev.
1280 **/
1281int request_firmware_direct(const struct firmware **firmware_p,
1282 const char *name, struct device *device)
1283{
1284 int ret;
Andrei Opreaea310032015-03-08 12:41:15 +02001285
Takashi Iwaibba3a872013-12-02 15:38:16 +01001286 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001287 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001288 FW_OPT_UEVENT | FW_OPT_NO_WARN);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001289 module_put(THIS_MODULE);
1290 return ret;
1291}
1292EXPORT_SYMBOL_GPL(request_firmware_direct);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001293
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001294/**
Stephen Boyda098ecd2016-08-02 14:04:28 -07001295 * request_firmware_into_buf - load firmware into a previously allocated buffer
1296 * @firmware_p: pointer to firmware image
1297 * @name: name of firmware file
1298 * @device: device for which firmware is being loaded and DMA region allocated
1299 * @buf: address of buffer to load firmware into
1300 * @size: size of buffer
1301 *
1302 * This function works pretty much like request_firmware(), but it doesn't
1303 * allocate a buffer to hold the firmware data. Instead, the firmware
1304 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
1305 * data member is pointed at @buf.
1306 *
1307 * This function doesn't cache firmware either.
1308 */
1309int
1310request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
1311 struct device *device, void *buf, size_t size)
1312{
1313 int ret;
1314
1315 __module_get(THIS_MODULE);
1316 ret = _request_firmware(firmware_p, name, device, buf, size,
1317 FW_OPT_UEVENT | FW_OPT_FALLBACK |
1318 FW_OPT_NOCACHE);
1319 module_put(THIS_MODULE);
1320 return ret;
1321}
1322EXPORT_SYMBOL(request_firmware_into_buf);
1323
1324/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001326 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001328void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
1330 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001331 if (!fw_is_builtin_firmware(fw))
1332 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 kfree(fw);
1334 }
1335}
Daniel Mackf4945132013-05-23 22:17:18 +02001336EXPORT_SYMBOL(release_firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338/* Async support */
1339struct firmware_work {
1340 struct work_struct work;
1341 struct module *module;
1342 const char *name;
1343 struct device *device;
1344 void *context;
1345 void (*cont)(const struct firmware *fw, void *context);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001346 unsigned int opt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347};
1348
Stephen Boyda36cf842012-03-28 23:31:00 +02001349static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Stephen Boyda36cf842012-03-28 23:31:00 +02001351 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001353
Stephen Boyda36cf842012-03-28 23:31:00 +02001354 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001355
Stephen Boyda098ecd2016-08-02 14:04:28 -07001356 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001357 fw_work->opt_flags);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001358 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001359 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 module_put(fw_work->module);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001362 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
1366/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001367 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001368 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001369 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001370 * is non-zero else the firmware copy must be done manually.
1371 * @name: name of firmware file
1372 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001373 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001374 * @context: will be passed over to @cont, and
1375 * @fw may be %NULL if firmware request fails.
1376 * @cont: function will be called asynchronously when the firmware
1377 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001379 * Caller must hold the reference count of @device.
1380 *
Ming Lei6f21a622012-08-04 12:01:24 +08001381 * Asynchronous variant of request_firmware() for user contexts:
1382 * - sleep for as small periods as possible since it may
Silvio Fricke88bcef52016-11-25 15:59:47 +01001383 * increase kernel boot time of built-in device drivers
1384 * requesting firmware in their ->probe() methods, if
1385 * @gfp is GFP_KERNEL.
Ming Lei6f21a622012-08-04 12:01:24 +08001386 *
1387 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 **/
1389int
1390request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001391 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001392 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 void (*cont)(const struct firmware *fw, void *context))
1394{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001395 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Andrei Opreaea310032015-03-08 12:41:15 +02001397 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (!fw_work)
1399 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001400
1401 fw_work->module = module;
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001402 fw_work->name = kstrdup_const(name, gfp);
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001403 if (!fw_work->name) {
1404 kfree(fw_work);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001405 return -ENOMEM;
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001406 }
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001407 fw_work->device = device;
1408 fw_work->context = context;
1409 fw_work->cont = cont;
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001410 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
Takashi Iwai5a1379e2014-06-04 17:48:15 +02001411 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (!try_module_get(module)) {
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001414 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 kfree(fw_work);
1416 return -EFAULT;
1417 }
1418
Ming Lei0cfc1e12012-08-04 12:01:23 +08001419 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001420 INIT_WORK(&fw_work->work, request_firmware_work_func);
1421 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return 0;
1423}
Daniel Mackf4945132013-05-23 22:17:18 +02001424EXPORT_SYMBOL(request_firmware_nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Ming Lei90f890812013-06-20 12:30:16 +08001426#ifdef CONFIG_PM_SLEEP
1427static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1428
Ming Lei2887b392012-08-04 12:01:22 +08001429/**
1430 * cache_firmware - cache one firmware image in kernel memory space
1431 * @fw_name: the firmware image name
1432 *
1433 * Cache firmware in kernel memory so that drivers can use it when
1434 * system isn't ready for them to request firmware image from userspace.
1435 * Once it returns successfully, driver can use request_firmware or its
1436 * nowait version to get the cached firmware without any interacting
1437 * with userspace
1438 *
1439 * Return 0 if the firmware image has been cached successfully
1440 * Return !0 otherwise
1441 *
1442 */
Ming Lei93232e42013-06-06 20:01:47 +08001443static int cache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001444{
1445 int ret;
1446 const struct firmware *fw;
1447
1448 pr_debug("%s: %s\n", __func__, fw_name);
1449
1450 ret = request_firmware(&fw, fw_name, NULL);
1451 if (!ret)
1452 kfree(fw);
1453
1454 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1455
1456 return ret;
1457}
1458
Ming Lei6a2c1232013-06-26 09:28:17 +08001459static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1460{
1461 struct firmware_buf *tmp;
1462 struct firmware_cache *fwc = &fw_cache;
1463
1464 spin_lock(&fwc->lock);
1465 tmp = __fw_lookup_buf(fw_name);
1466 spin_unlock(&fwc->lock);
1467
1468 return tmp;
1469}
1470
Ming Lei2887b392012-08-04 12:01:22 +08001471/**
1472 * uncache_firmware - remove one cached firmware image
1473 * @fw_name: the firmware image name
1474 *
1475 * Uncache one firmware image which has been cached successfully
1476 * before.
1477 *
1478 * Return 0 if the firmware cache has been removed successfully
1479 * Return !0 otherwise
1480 *
1481 */
Ming Lei93232e42013-06-06 20:01:47 +08001482static int uncache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001483{
1484 struct firmware_buf *buf;
1485 struct firmware fw;
1486
1487 pr_debug("%s: %s\n", __func__, fw_name);
1488
Stephen Boyda098ecd2016-08-02 14:04:28 -07001489 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
Ming Lei2887b392012-08-04 12:01:22 +08001490 return 0;
1491
1492 buf = fw_lookup_buf(fw_name);
1493 if (buf) {
1494 fw_free_buf(buf);
1495 return 0;
1496 }
1497
1498 return -EINVAL;
1499}
1500
Ming Lei37276a52012-08-04 12:01:27 +08001501static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1502{
1503 struct fw_cache_entry *fce;
1504
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001505 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
Ming Lei37276a52012-08-04 12:01:27 +08001506 if (!fce)
1507 goto exit;
1508
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001509 fce->name = kstrdup_const(name, GFP_ATOMIC);
1510 if (!fce->name) {
1511 kfree(fce);
1512 fce = NULL;
1513 goto exit;
1514 }
Ming Lei37276a52012-08-04 12:01:27 +08001515exit:
1516 return fce;
1517}
1518
Ming Lei373304f2012-10-09 12:01:01 +08001519static int __fw_entry_found(const char *name)
1520{
1521 struct firmware_cache *fwc = &fw_cache;
1522 struct fw_cache_entry *fce;
1523
1524 list_for_each_entry(fce, &fwc->fw_names, list) {
1525 if (!strcmp(fce->name, name))
1526 return 1;
1527 }
1528 return 0;
1529}
1530
Ming Leiac39b3e2012-08-20 19:04:16 +08001531static int fw_cache_piggyback_on_request(const char *name)
1532{
1533 struct firmware_cache *fwc = &fw_cache;
1534 struct fw_cache_entry *fce;
1535 int ret = 0;
1536
1537 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001538 if (__fw_entry_found(name))
1539 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001540
1541 fce = alloc_fw_cache_entry(name);
1542 if (fce) {
1543 ret = 1;
1544 list_add(&fce->list, &fwc->fw_names);
1545 pr_debug("%s: fw: %s\n", __func__, name);
1546 }
1547found:
1548 spin_unlock(&fwc->name_lock);
1549 return ret;
1550}
1551
Ming Lei37276a52012-08-04 12:01:27 +08001552static void free_fw_cache_entry(struct fw_cache_entry *fce)
1553{
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001554 kfree_const(fce->name);
Ming Lei37276a52012-08-04 12:01:27 +08001555 kfree(fce);
1556}
1557
1558static void __async_dev_cache_fw_image(void *fw_entry,
1559 async_cookie_t cookie)
1560{
1561 struct fw_cache_entry *fce = fw_entry;
1562 struct firmware_cache *fwc = &fw_cache;
1563 int ret;
1564
1565 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001566 if (ret) {
1567 spin_lock(&fwc->name_lock);
1568 list_del(&fce->list);
1569 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001570
Ming Leiac39b3e2012-08-20 19:04:16 +08001571 free_fw_cache_entry(fce);
1572 }
Ming Lei37276a52012-08-04 12:01:27 +08001573}
1574
1575/* called with dev->devres_lock held */
1576static void dev_create_fw_entry(struct device *dev, void *res,
1577 void *data)
1578{
1579 struct fw_name_devm *fwn = res;
1580 const char *fw_name = fwn->name;
1581 struct list_head *head = data;
1582 struct fw_cache_entry *fce;
1583
1584 fce = alloc_fw_cache_entry(fw_name);
1585 if (fce)
1586 list_add(&fce->list, head);
1587}
1588
1589static int devm_name_match(struct device *dev, void *res,
1590 void *match_data)
1591{
1592 struct fw_name_devm *fwn = res;
1593 return (fwn->magic == (unsigned long)match_data);
1594}
1595
Ming Leiab6dd8e2012-08-17 22:07:00 +08001596static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001597{
1598 LIST_HEAD(todo);
1599 struct fw_cache_entry *fce;
1600 struct fw_cache_entry *fce_next;
1601 struct firmware_cache *fwc = &fw_cache;
1602
1603 devres_for_each_res(dev, fw_name_devm_release,
1604 devm_name_match, &fw_cache,
1605 dev_create_fw_entry, &todo);
1606
1607 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1608 list_del(&fce->list);
1609
1610 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001611 /* only one cache entry for one firmware */
1612 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001613 list_add(&fce->list, &fwc->fw_names);
1614 } else {
1615 free_fw_cache_entry(fce);
1616 fce = NULL;
1617 }
Ming Lei37276a52012-08-04 12:01:27 +08001618 spin_unlock(&fwc->name_lock);
1619
Ming Lei373304f2012-10-09 12:01:01 +08001620 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001621 async_schedule_domain(__async_dev_cache_fw_image,
1622 (void *)fce,
1623 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001624 }
1625}
1626
1627static void __device_uncache_fw_images(void)
1628{
1629 struct firmware_cache *fwc = &fw_cache;
1630 struct fw_cache_entry *fce;
1631
1632 spin_lock(&fwc->name_lock);
1633 while (!list_empty(&fwc->fw_names)) {
1634 fce = list_entry(fwc->fw_names.next,
1635 struct fw_cache_entry, list);
1636 list_del(&fce->list);
1637 spin_unlock(&fwc->name_lock);
1638
1639 uncache_firmware(fce->name);
1640 free_fw_cache_entry(fce);
1641
1642 spin_lock(&fwc->name_lock);
1643 }
1644 spin_unlock(&fwc->name_lock);
1645}
1646
1647/**
1648 * device_cache_fw_images - cache devices' firmware
1649 *
1650 * If one device called request_firmware or its nowait version
1651 * successfully before, the firmware names are recored into the
1652 * device's devres link list, so device_cache_fw_images can call
1653 * cache_firmware() to cache these firmwares for the device,
1654 * then the device driver can load its firmwares easily at
1655 * time when system is not ready to complete loading firmware.
1656 */
1657static void device_cache_fw_images(void)
1658{
1659 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001660 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001661 DEFINE_WAIT(wait);
1662
1663 pr_debug("%s\n", __func__);
1664
Ming Lei373304f2012-10-09 12:01:01 +08001665 /* cancel uncache work */
1666 cancel_delayed_work_sync(&fwc->work);
1667
Ming Leiffe53f62012-08-04 12:01:28 +08001668 /*
1669 * use small loading timeout for caching devices' firmware
1670 * because all these firmware images have been loaded
1671 * successfully at lease once, also system is ready for
1672 * completing firmware loading now. The maximum size of
1673 * firmware in current distributions is about 2M bytes,
1674 * so 10 secs should be enough.
1675 */
1676 old_timeout = loading_timeout;
1677 loading_timeout = 10;
1678
Ming Leiac39b3e2012-08-20 19:04:16 +08001679 mutex_lock(&fw_lock);
1680 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001681 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001682 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001683
1684 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001685 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001686
1687 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001688}
1689
1690/**
1691 * device_uncache_fw_images - uncache devices' firmware
1692 *
1693 * uncache all firmwares which have been cached successfully
1694 * by device_uncache_fw_images earlier
1695 */
1696static void device_uncache_fw_images(void)
1697{
1698 pr_debug("%s\n", __func__);
1699 __device_uncache_fw_images();
1700}
1701
1702static void device_uncache_fw_images_work(struct work_struct *work)
1703{
1704 device_uncache_fw_images();
1705}
1706
1707/**
1708 * device_uncache_fw_images_delay - uncache devices firmwares
1709 * @delay: number of milliseconds to delay uncache device firmwares
1710 *
1711 * uncache all devices's firmwares which has been cached successfully
1712 * by device_cache_fw_images after @delay milliseconds.
1713 */
1714static void device_uncache_fw_images_delay(unsigned long delay)
1715{
Shaibal Duttabce66182014-01-31 15:44:58 -08001716 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1717 msecs_to_jiffies(delay));
Ming Lei37276a52012-08-04 12:01:27 +08001718}
1719
Ming Lei07646d92012-08-04 12:01:29 +08001720static int fw_pm_notify(struct notifier_block *notify_block,
1721 unsigned long mode, void *unused)
1722{
1723 switch (mode) {
1724 case PM_HIBERNATION_PREPARE:
1725 case PM_SUSPEND_PREPARE:
Sebastian Capellaf8d5b9e2014-02-18 17:52:08 -08001726 case PM_RESTORE_PREPARE:
Ming Leiaf5bc112013-05-27 18:30:04 +08001727 kill_requests_without_uevent();
Ming Lei07646d92012-08-04 12:01:29 +08001728 device_cache_fw_images();
1729 break;
1730
1731 case PM_POST_SUSPEND:
1732 case PM_POST_HIBERNATION:
1733 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001734 /*
1735 * In case that system sleep failed and syscore_suspend is
1736 * not called.
1737 */
1738 mutex_lock(&fw_lock);
1739 fw_cache.state = FW_LOADER_NO_CACHE;
1740 mutex_unlock(&fw_lock);
1741
Ming Lei07646d92012-08-04 12:01:29 +08001742 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1743 break;
1744 }
1745
1746 return 0;
1747}
Ming Lei07646d92012-08-04 12:01:29 +08001748
Ming Leiac39b3e2012-08-20 19:04:16 +08001749/* stop caching firmware once syscore_suspend is reached */
1750static int fw_suspend(void)
1751{
1752 fw_cache.state = FW_LOADER_NO_CACHE;
1753 return 0;
1754}
1755
1756static struct syscore_ops fw_syscore_ops = {
1757 .suspend = fw_suspend,
1758};
Ming Leicfe016b2012-09-08 17:32:30 +08001759#else
1760static int fw_cache_piggyback_on_request(const char *name)
1761{
1762 return 0;
1763}
1764#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001765
Ming Lei37276a52012-08-04 12:01:27 +08001766static void __init fw_cache_init(void)
1767{
1768 spin_lock_init(&fw_cache.lock);
1769 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001770 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001771
Ming Leicfe016b2012-09-08 17:32:30 +08001772#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001773 spin_lock_init(&fw_cache.name_lock);
1774 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001775
Ming Lei37276a52012-08-04 12:01:27 +08001776 INIT_DELAYED_WORK(&fw_cache.work,
1777 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001778
1779 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1780 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001781
1782 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001783#endif
Ming Lei37276a52012-08-04 12:01:27 +08001784}
1785
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001786static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Ming Lei1f2b7952012-08-04 12:01:21 +08001788 fw_cache_init();
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001789#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001790 register_reboot_notifier(&fw_shutdown_nb);
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001791 return class_register(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001792#else
1793 return 0;
1794#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001796
1797static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
Ming Leicfe016b2012-09-08 17:32:30 +08001799#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001800 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001801 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001802#endif
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001803#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001804 unregister_reboot_notifier(&fw_shutdown_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 class_unregister(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001806#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808
Shaohua Lia30a6a22006-09-27 01:50:52 -07001809fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810module_exit(firmware_class_exit);