blob: 4497d263209fb861e08e1650ddf654a98f2102e3 [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);
Ming Lei87597932013-06-15 16:36:38 +0800561
562 /* avoid user action after loading abort */
563 fw_priv->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Takashi Iwaife304142013-05-22 18:28:38 +0200566static LIST_HEAD(pending_fw_head);
567
568/* reboot notifier for avoid deadlock with usermode_lock */
569static int fw_shutdown_notify(struct notifier_block *unused1,
570 unsigned long unused2, void *unused3)
571{
572 mutex_lock(&fw_lock);
573 while (!list_empty(&pending_fw_head))
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700574 __fw_load_abort(list_first_entry(&pending_fw_head,
Takashi Iwaife304142013-05-22 18:28:38 +0200575 struct firmware_buf,
576 pending_list));
577 mutex_unlock(&fw_lock);
578 return NOTIFY_DONE;
579}
580
581static struct notifier_block fw_shutdown_nb = {
582 .notifier_call = fw_shutdown_notify,
583};
584
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700585static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
586 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 return sprintf(buf, "%d\n", loading_timeout);
589}
590
591/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800592 * firmware_timeout_store - set number of seconds to wait for firmware
593 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800594 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800595 * @buf: buffer to scan for timeout value
596 * @count: number of bytes in @buf
597 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800599 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 * firmware will be provided.
601 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800602 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 **/
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700604static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
605 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700608 if (loading_timeout < 0)
609 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return count;
612}
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100613static CLASS_ATTR_RW(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100615static struct attribute *firmware_class_attrs[] = {
616 &class_attr_timeout.attr,
617 NULL,
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800618};
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100619ATTRIBUTE_GROUPS(firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800621static void fw_dev_release(struct device *dev)
622{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700623 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800624
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800625 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800626}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Linus Torvalds6f957722015-07-09 11:20:01 -0700628static int do_firmware_uevent(struct firmware_priv *fw_priv, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Ming Lei12446912012-08-04 12:01:20 +0800630 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200632 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700633 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200634 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
635 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 return 0;
638}
639
Linus Torvalds6f957722015-07-09 11:20:01 -0700640static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
641{
642 struct firmware_priv *fw_priv = to_firmware_priv(dev);
643 int err = 0;
644
645 mutex_lock(&fw_lock);
646 if (fw_priv->buf)
647 err = do_firmware_uevent(fw_priv, env);
648 mutex_unlock(&fw_lock);
649 return err;
650}
651
Adrian Bunk1b81d662006-05-20 15:00:16 -0700652static struct class firmware_class = {
653 .name = "firmware",
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100654 .class_groups = firmware_class_groups,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700655 .dev_uevent = firmware_uevent,
656 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700657};
658
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700659static ssize_t firmware_loading_show(struct device *dev,
660 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700662 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800663 int loading = 0;
664
665 mutex_lock(&fw_lock);
666 if (fw_priv->buf)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100667 loading = fw_state_is_loading(&fw_priv->buf->fw_st);
Ming Lei87597932013-06-15 16:36:38 +0800668 mutex_unlock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return sprintf(buf, "%d\n", loading);
671}
672
David Woodhouse6e03a202009-04-09 22:04:07 -0700673/* Some architectures don't have PAGE_KERNEL_RO */
674#ifndef PAGE_KERNEL_RO
675#define PAGE_KERNEL_RO PAGE_KERNEL
676#endif
Ming Lei253c9242012-10-09 12:01:02 +0800677
678/* one pages buffer should be mapped/unmapped only once */
679static int fw_map_pages_buf(struct firmware_buf *buf)
680{
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100681 if (!buf->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800682 return 0;
683
Markus Elfringdaa3d672014-11-19 11:38:38 +0100684 vunmap(buf->data);
Ming Lei253c9242012-10-09 12:01:02 +0800685 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
686 if (!buf->data)
687 return -ENOMEM;
688 return 0;
689}
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800692 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700693 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800694 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800695 * @buf: buffer to scan for loading control value
696 * @count: number of bytes in @buf
697 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 * The relevant values are:
699 *
700 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800701 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 * -1: Conclude the load with an error and discard any written data.
703 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700704static ssize_t firmware_loading_store(struct device *dev,
705 struct device_attribute *attr,
706 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700708 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800709 struct firmware_buf *fw_buf;
Kees Cook6593d922014-02-25 13:06:00 -0800710 ssize_t written = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700712 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Neil Hormaneea915b2012-01-02 15:31:23 -0500714 mutex_lock(&fw_lock);
Ming Lei87597932013-06-15 16:36:38 +0800715 fw_buf = fw_priv->buf;
Ming Lei12446912012-08-04 12:01:20 +0800716 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500717 goto out;
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 switch (loading) {
720 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800721 /* discarding any previous partial load */
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100722 if (!fw_state_is_done(&fw_buf->fw_st)) {
Ming Lei12446912012-08-04 12:01:20 +0800723 for (i = 0; i < fw_buf->nr_pages; i++)
724 __free_page(fw_buf->pages[i]);
Chen Feng10a3fbf12015-12-16 17:03:15 +0800725 vfree(fw_buf->pages);
Ming Lei12446912012-08-04 12:01:20 +0800726 fw_buf->pages = NULL;
727 fw_buf->page_array_size = 0;
728 fw_buf->nr_pages = 0;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100729 fw_state_start(&fw_buf->fw_st);
Ming Lei28eefa72012-08-04 12:01:17 +0800730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 break;
732 case 0:
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100733 if (fw_state_is_loading(&fw_buf->fw_st)) {
Kees Cook6593d922014-02-25 13:06:00 -0800734 int rc;
735
Ming Lei253c9242012-10-09 12:01:02 +0800736 /*
737 * Several loading requests may be pending on
738 * one same firmware buf, so let all requests
739 * see the mapped 'buf->data' once the loading
740 * is completed.
741 * */
Kees Cook6593d922014-02-25 13:06:00 -0800742 rc = fw_map_pages_buf(fw_buf);
743 if (rc)
zhang jun2b1278c2014-02-13 15:18:47 +0800744 dev_err(dev, "%s: map pages failed\n",
745 __func__);
Kees Cook6593d922014-02-25 13:06:00 -0800746 else
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500747 rc = security_kernel_post_read_file(NULL,
748 fw_buf->data, fw_buf->size,
749 READING_FIRMWARE);
Kees Cook6593d922014-02-25 13:06:00 -0800750
751 /*
752 * Same logic as fw_load_abort, only the DONE bit
753 * is ignored and we set ABORT only on failure.
754 */
Takashi Iwaife304142013-05-22 18:28:38 +0200755 list_del_init(&fw_buf->pending_list);
Kees Cook6593d922014-02-25 13:06:00 -0800756 if (rc) {
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100757 fw_state_aborted(&fw_buf->fw_st);
Kees Cook6593d922014-02-25 13:06:00 -0800758 written = rc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100759 } else {
760 fw_state_done(&fw_buf->fw_st);
Kees Cook6593d922014-02-25 13:06:00 -0800761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 break;
763 }
764 /* fallthrough */
765 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700766 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /* fallthrough */
768 case -1:
769 fw_load_abort(fw_priv);
770 break;
771 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500772out:
773 mutex_unlock(&fw_lock);
Kees Cook6593d922014-02-25 13:06:00 -0800774 return written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700777static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Stephen Boyda098ecd2016-08-02 14:04:28 -0700779static void firmware_rw_buf(struct firmware_buf *buf, char *buffer,
780 loff_t offset, size_t count, bool read)
781{
782 if (read)
783 memcpy(buffer, buf->data + offset, count);
784 else
785 memcpy(buf->data + offset, buffer, count);
786}
787
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700788static void firmware_rw(struct firmware_buf *buf, char *buffer,
789 loff_t offset, size_t count, bool read)
790{
791 while (count) {
792 void *page_data;
793 int page_nr = offset >> PAGE_SHIFT;
794 int page_ofs = offset & (PAGE_SIZE-1);
795 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
796
797 page_data = kmap(buf->pages[page_nr]);
798
799 if (read)
800 memcpy(buffer, page_data + page_ofs, page_cnt);
801 else
802 memcpy(page_data + page_ofs, buffer, page_cnt);
803
804 kunmap(buf->pages[page_nr]);
805 buffer += page_cnt;
806 offset += page_cnt;
807 count -= page_cnt;
808 }
809}
810
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700811static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
812 struct bin_attribute *bin_attr,
813 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200815 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700816 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800817 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700818 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Laura Garciacad1e552006-05-23 23:22:38 +0200820 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800821 buf = fw_priv->buf;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100822 if (!buf || fw_state_is_done(&buf->fw_st)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 ret_count = -ENODEV;
824 goto out;
825 }
Ming Lei12446912012-08-04 12:01:20 +0800826 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200827 ret_count = 0;
828 goto out;
829 }
Ming Lei12446912012-08-04 12:01:20 +0800830 if (count > buf->size - offset)
831 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700832
833 ret_count = count;
834
Stephen Boyda098ecd2016-08-02 14:04:28 -0700835 if (buf->data)
836 firmware_rw_buf(buf, buffer, offset, count, true);
837 else
838 firmware_rw(buf, buffer, offset, count, true);
David Woodhouse6e03a202009-04-09 22:04:07 -0700839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840out:
Laura Garciacad1e552006-05-23 23:22:38 +0200841 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return ret_count;
843}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800844
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700845static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Ming Lei12446912012-08-04 12:01:20 +0800847 struct firmware_buf *buf = fw_priv->buf;
Fabian Fredericka76040d2014-07-01 21:13:30 +0200848 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
David Woodhouse6e03a202009-04-09 22:04:07 -0700850 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800851 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700852 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800853 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700854 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Chen Feng10a3fbf12015-12-16 17:03:15 +0800856 new_pages = vmalloc(new_array_size * sizeof(void *));
David Woodhouse6e03a202009-04-09 22:04:07 -0700857 if (!new_pages) {
858 fw_load_abort(fw_priv);
859 return -ENOMEM;
860 }
Ming Lei12446912012-08-04 12:01:20 +0800861 memcpy(new_pages, buf->pages,
862 buf->page_array_size * sizeof(void *));
863 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
864 (new_array_size - buf->page_array_size));
Chen Feng10a3fbf12015-12-16 17:03:15 +0800865 vfree(buf->pages);
Ming Lei12446912012-08-04 12:01:20 +0800866 buf->pages = new_pages;
867 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700869
Ming Lei12446912012-08-04 12:01:20 +0800870 while (buf->nr_pages < pages_needed) {
871 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700872 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
873
Ming Lei12446912012-08-04 12:01:20 +0800874 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700875 fw_load_abort(fw_priv);
876 return -ENOMEM;
877 }
Ming Lei12446912012-08-04 12:01:20 +0800878 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return 0;
881}
882
883/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800884 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700885 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700886 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700887 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800888 * @buffer: buffer being written
889 * @offset: buffer offset for write in total data store area
890 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800892 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 * the driver as a firmware image.
894 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700895static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
896 struct bin_attribute *bin_attr,
897 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200899 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700900 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800901 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 ssize_t retval;
903
904 if (!capable(CAP_SYS_RAWIO))
905 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700906
Laura Garciacad1e552006-05-23 23:22:38 +0200907 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800908 buf = fw_priv->buf;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100909 if (!buf || fw_state_is_done(&buf->fw_st)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 retval = -ENODEV;
911 goto out;
912 }
Ming Lei65710cb2012-08-04 12:01:16 +0800913
Stephen Boyda098ecd2016-08-02 14:04:28 -0700914 if (buf->data) {
915 if (offset + count > buf->allocated_size) {
916 retval = -ENOMEM;
917 goto out;
918 }
919 firmware_rw_buf(buf, buffer, offset, count, false);
920 retval = count;
921 } else {
922 retval = fw_realloc_buffer(fw_priv, offset + count);
923 if (retval)
924 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Stephen Boyda098ecd2016-08-02 14:04:28 -0700926 retval = count;
927 firmware_rw(buf, buffer, offset, count, false);
928 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700929
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700930 buf->size = max_t(size_t, offset + count, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931out:
Laura Garciacad1e552006-05-23 23:22:38 +0200932 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return retval;
934}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800935
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700936static struct bin_attribute firmware_attr_data = {
937 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 .size = 0,
939 .read = firmware_data_read,
940 .write = firmware_data_write,
941};
942
Takashi Iwai46239902015-02-04 15:18:07 +0100943static struct attribute *fw_dev_attrs[] = {
944 &dev_attr_loading.attr,
945 NULL
946};
947
948static struct bin_attribute *fw_dev_bin_attrs[] = {
949 &firmware_attr_data,
950 NULL
951};
952
953static const struct attribute_group fw_dev_attr_group = {
954 .attrs = fw_dev_attrs,
955 .bin_attrs = fw_dev_bin_attrs,
956};
957
958static const struct attribute_group *fw_dev_attr_groups[] = {
959 &fw_dev_attr_group,
960 NULL
961};
962
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700963static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200964fw_create_instance(struct firmware *firmware, const char *fw_name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100965 struct device *device, unsigned int opt_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700967 struct firmware_priv *fw_priv;
968 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Ming Lei12446912012-08-04 12:01:20 +0800970 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700971 if (!fw_priv) {
Ming Lei12446912012-08-04 12:01:20 +0800972 fw_priv = ERR_PTR(-ENOMEM);
973 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100976 fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT);
Ming Lei1f2b7952012-08-04 12:01:21 +0800977 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700978 f_dev = &fw_priv->dev;
979
980 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800981 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700982 f_dev->parent = device;
983 f_dev->class = &firmware_class;
Takashi Iwai46239902015-02-04 15:18:07 +0100984 f_dev->groups = fw_dev_attr_groups;
Ming Lei12446912012-08-04 12:01:20 +0800985exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700986 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100988
989/* load a firmware via user helper */
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100990static int _request_firmware_load(struct firmware_priv *fw_priv,
991 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100992{
993 int retval = 0;
994 struct device *f_dev = &fw_priv->dev;
995 struct firmware_buf *buf = fw_priv->buf;
996
997 /* fall back on userspace loading */
Stephen Boyda098ecd2016-08-02 14:04:28 -0700998 if (!buf->data)
999 buf->is_paged_buf = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001000
1001 dev_set_uevent_suppress(f_dev, true);
1002
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001003 retval = device_add(f_dev);
1004 if (retval) {
1005 dev_err(f_dev, "%s: device_register failed\n", __func__);
1006 goto err_put_dev;
1007 }
1008
Maxime Bizon1eeeef12013-08-29 20:28:13 +02001009 mutex_lock(&fw_lock);
1010 list_add(&buf->pending_list, &pending_fw_head);
1011 mutex_unlock(&fw_lock);
1012
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001013 if (opt_flags & FW_OPT_UEVENT) {
Ming Leiaf5bc112013-05-27 18:30:04 +08001014 buf->need_uevent = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001015 dev_set_uevent_suppress(f_dev, false);
1016 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001017 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
Ming Lei68ff2a02015-01-13 00:02:01 +08001018 } else {
1019 timeout = MAX_JIFFY_OFFSET;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001020 }
1021
Bjorn Andersson5d47ec02016-12-06 17:01:45 -08001022 retval = fw_state_wait_timeout(&buf->fw_st, timeout);
1023 if (retval < 0) {
Ming Lei0cb64242015-01-13 00:01:35 +08001024 mutex_lock(&fw_lock);
1025 fw_load_abort(fw_priv);
1026 mutex_unlock(&fw_lock);
1027 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001028
Daniel Wagnerf52cc3792016-11-17 11:00:48 +01001029 if (fw_state_is_aborted(&buf->fw_st))
Shuah Khan0542ad82014-07-22 18:10:38 -06001030 retval = -EAGAIN;
Stephen Boyda098ecd2016-08-02 14:04:28 -07001031 else if (buf->is_paged_buf && !buf->data)
zhang jun2b1278c2014-02-13 15:18:47 +08001032 retval = -ENOMEM;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001033
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001034 device_del(f_dev);
1035err_put_dev:
1036 put_device(f_dev);
1037 return retval;
1038}
1039
1040static int fw_load_from_user_helper(struct firmware *firmware,
1041 const char *name, struct device *device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001042 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001043{
1044 struct firmware_priv *fw_priv;
1045
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001046 fw_priv = fw_create_instance(firmware, name, device, opt_flags);
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001047 if (IS_ERR(fw_priv))
1048 return PTR_ERR(fw_priv);
1049
1050 fw_priv->buf = firmware->priv;
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001051 return _request_firmware_load(fw_priv, opt_flags, timeout);
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001052}
Ming Leiddf1f062013-06-04 10:01:13 +08001053
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001054#ifdef CONFIG_PM_SLEEP
Ming Leiddf1f062013-06-04 10:01:13 +08001055/* kill pending requests without uevent to avoid blocking suspend */
1056static void kill_requests_without_uevent(void)
1057{
1058 struct firmware_buf *buf;
1059 struct firmware_buf *next;
1060
1061 mutex_lock(&fw_lock);
1062 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
1063 if (!buf->need_uevent)
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -07001064 __fw_load_abort(buf);
Ming Leiddf1f062013-06-04 10:01:13 +08001065 }
1066 mutex_unlock(&fw_lock);
1067}
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001068#endif
Ming Leiddf1f062013-06-04 10:01:13 +08001069
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001070#else /* CONFIG_FW_LOADER_USER_HELPER */
1071static inline int
1072fw_load_from_user_helper(struct firmware *firmware, const char *name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001073 struct device *device, unsigned int opt_flags,
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001074 long timeout)
1075{
1076 return -ENOENT;
1077}
Takashi Iwai807be032013-01-31 11:13:57 +01001078
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001079#ifdef CONFIG_PM_SLEEP
Ming Leiddf1f062013-06-04 10:01:13 +08001080static inline void kill_requests_without_uevent(void) { }
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001081#endif
Ming Leiddf1f062013-06-04 10:01:13 +08001082
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001083#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001085/* prepare firmware and firmware_buf structs;
1086 * return 0 if a firmware is already assigned, 1 if need to load one,
1087 * or a negative error code
1088 */
1089static int
1090_request_firmware_prepare(struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001091 struct device *device, void *dbuf, size_t size)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001092{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +08001094 struct firmware_buf *buf;
1095 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Jiri Slaby4aed0642005-09-13 01:25:01 -07001097 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -07001099 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1100 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001101 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Stephen Boyda098ecd2016-08-02 14:04:28 -07001104 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
Luis R. Rodriguezed046302015-04-29 16:30:43 -07001105 dev_dbg(device, "using built-in %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001106 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +01001107 }
1108
Stephen Boyda098ecd2016-08-02 14:04:28 -07001109 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +08001110
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001111 /*
1112 * bind with 'buf' now to avoid warning in failure path
1113 * of requesting firmware.
1114 */
1115 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +08001116
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001117 if (ret > 0) {
Daniel Wagner5b029622016-11-17 11:00:50 +01001118 ret = fw_state_wait(&buf->fw_st);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001119 if (!ret) {
1120 fw_set_page_data(buf, firmware);
1121 return 0; /* assigned */
1122 }
Stephen Boyddddb5542012-03-28 23:30:43 +02001123 }
Ming Lei1f2b7952012-08-04 12:01:21 +08001124
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001125 if (ret < 0)
1126 return ret;
1127 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001128}
1129
Ming Leie771d1a2013-05-27 18:30:03 +08001130static int assign_firmware_buf(struct firmware *fw, struct device *device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001131 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001132{
1133 struct firmware_buf *buf = fw->priv;
1134
1135 mutex_lock(&fw_lock);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +01001136 if (!buf->size || fw_state_is_aborted(&buf->fw_st)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001137 mutex_unlock(&fw_lock);
1138 return -ENOENT;
1139 }
1140
1141 /*
1142 * add firmware name into devres list so that we can auto cache
1143 * and uncache firmware for device.
1144 *
1145 * device may has been deleted already, but the problem
1146 * should be fixed in devres or driver core.
1147 */
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001148 /* don't cache firmware handled without uevent */
Vikram Mulukutla0e742e92016-08-02 14:04:25 -07001149 if (device && (opt_flags & FW_OPT_UEVENT) &&
1150 !(opt_flags & FW_OPT_NOCACHE))
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001151 fw_add_devm_name(device, buf->fw_id);
1152
1153 /*
1154 * After caching firmware image is started, let it piggyback
1155 * on request firmware.
1156 */
Vikram Mulukutla0e742e92016-08-02 14:04:25 -07001157 if (!(opt_flags & FW_OPT_NOCACHE) &&
1158 buf->fwc->state == FW_LOADER_START_CACHE) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001159 if (fw_cache_piggyback_on_request(buf->fw_id))
1160 kref_get(&buf->ref);
1161 }
1162
1163 /* pass the pages buffer to driver at the last minute */
1164 fw_set_page_data(buf, fw);
1165 mutex_unlock(&fw_lock);
1166 return 0;
1167}
1168
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001169/* called from request_firmware() and request_firmware_work_func() */
1170static int
1171_request_firmware(const struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001172 struct device *device, void *buf, size_t size,
1173 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001174{
Brian Norris715780a2015-12-09 14:50:28 -08001175 struct firmware *fw = NULL;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001176 long timeout;
1177 int ret;
1178
1179 if (!firmware_p)
1180 return -EINVAL;
1181
Brian Norris715780a2015-12-09 14:50:28 -08001182 if (!name || name[0] == '\0') {
1183 ret = -EINVAL;
1184 goto out;
1185 }
Kees Cook471b0952014-09-18 11:25:37 -07001186
Stephen Boyda098ecd2016-08-02 14:04:28 -07001187 ret = _request_firmware_prepare(&fw, name, device, buf, size);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001188 if (ret <= 0) /* error or already assigned */
1189 goto out;
1190
1191 ret = 0;
1192 timeout = firmware_loading_timeout();
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001193 if (opt_flags & FW_OPT_NOWAIT) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001194 timeout = usermodehelper_read_lock_wait(timeout);
1195 if (!timeout) {
1196 dev_dbg(device, "firmware: %s loading timed out\n",
1197 name);
1198 ret = -EBUSY;
1199 goto out;
1200 }
1201 } else {
1202 ret = usermodehelper_read_trylock();
1203 if (WARN_ON(ret)) {
1204 dev_err(device, "firmware: %s will not be loaded\n",
1205 name);
1206 goto out;
1207 }
1208 }
1209
Neil Horman3e358ac2013-09-06 15:36:08 -04001210 ret = fw_get_filesystem_firmware(device, fw->priv);
1211 if (ret) {
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001212 if (!(opt_flags & FW_OPT_NO_WARN))
Takashi Iwaibba3a872013-12-02 15:38:16 +01001213 dev_warn(device,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001214 "Direct firmware load for %s failed with error %d\n",
1215 name, ret);
1216 if (opt_flags & FW_OPT_USERHELPER) {
Takashi Iwaibba3a872013-12-02 15:38:16 +01001217 dev_warn(device, "Falling back to user helper\n");
1218 ret = fw_load_from_user_helper(fw, name, device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001219 opt_flags, timeout);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001220 }
Neil Horman3e358ac2013-09-06 15:36:08 -04001221 }
Ming Leie771d1a2013-05-27 18:30:03 +08001222
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001223 if (!ret)
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001224 ret = assign_firmware_buf(fw, device, opt_flags);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001225
1226 usermodehelper_read_unlock();
1227
1228 out:
1229 if (ret < 0) {
1230 release_firmware(fw);
1231 fw = NULL;
1232 }
1233
1234 *firmware_p = fw;
1235 return ret;
1236}
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238/**
Kay Sievers312c0042005-11-16 09:00:00 +01001239 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001240 * @firmware_p: pointer to firmware image
1241 * @name: name of firmware file
1242 * @device: device for which firmware is being loaded
1243 *
1244 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001245 * of @name for device @device.
1246 *
1247 * Should be called from user context where sleeping is allowed.
1248 *
Kay Sievers312c0042005-11-16 09:00:00 +01001249 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001250 * should be distinctive enough not to be confused with any other
1251 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001252 *
1253 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001254 *
1255 * The function can be called safely inside device's suspend and
1256 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001257 **/
1258int
1259request_firmware(const struct firmware **firmware_p, const char *name,
Andrei Opreaea310032015-03-08 12:41:15 +02001260 struct device *device)
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001261{
Ming Leid6c8aa32013-06-06 20:01:48 +08001262 int ret;
1263
1264 /* Need to pin this module until return */
1265 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001266 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001267 FW_OPT_UEVENT | FW_OPT_FALLBACK);
Ming Leid6c8aa32013-06-06 20:01:48 +08001268 module_put(THIS_MODULE);
1269 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001270}
Daniel Mackf4945132013-05-23 22:17:18 +02001271EXPORT_SYMBOL(request_firmware);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001272
Takashi Iwaibba3a872013-12-02 15:38:16 +01001273/**
Borislav Petkov3c1556b2014-12-03 22:46:37 +01001274 * request_firmware_direct: - load firmware directly without usermode helper
Takashi Iwaibba3a872013-12-02 15:38:16 +01001275 * @firmware_p: pointer to firmware image
1276 * @name: name of firmware file
1277 * @device: device for which firmware is being loaded
1278 *
1279 * This function works pretty much like request_firmware(), but this doesn't
1280 * fall back to usermode helper even if the firmware couldn't be loaded
1281 * directly from fs. Hence it's useful for loading optional firmwares, which
1282 * aren't always present, without extra long timeouts of udev.
1283 **/
1284int request_firmware_direct(const struct firmware **firmware_p,
1285 const char *name, struct device *device)
1286{
1287 int ret;
Andrei Opreaea310032015-03-08 12:41:15 +02001288
Takashi Iwaibba3a872013-12-02 15:38:16 +01001289 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001290 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001291 FW_OPT_UEVENT | FW_OPT_NO_WARN);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001292 module_put(THIS_MODULE);
1293 return ret;
1294}
1295EXPORT_SYMBOL_GPL(request_firmware_direct);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001296
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001297/**
Stephen Boyda098ecd2016-08-02 14:04:28 -07001298 * request_firmware_into_buf - load firmware into a previously allocated buffer
1299 * @firmware_p: pointer to firmware image
1300 * @name: name of firmware file
1301 * @device: device for which firmware is being loaded and DMA region allocated
1302 * @buf: address of buffer to load firmware into
1303 * @size: size of buffer
1304 *
1305 * This function works pretty much like request_firmware(), but it doesn't
1306 * allocate a buffer to hold the firmware data. Instead, the firmware
1307 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
1308 * data member is pointed at @buf.
1309 *
1310 * This function doesn't cache firmware either.
1311 */
1312int
1313request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
1314 struct device *device, void *buf, size_t size)
1315{
1316 int ret;
1317
1318 __module_get(THIS_MODULE);
1319 ret = _request_firmware(firmware_p, name, device, buf, size,
1320 FW_OPT_UEVENT | FW_OPT_FALLBACK |
1321 FW_OPT_NOCACHE);
1322 module_put(THIS_MODULE);
1323 return ret;
1324}
1325EXPORT_SYMBOL(request_firmware_into_buf);
1326
1327/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001329 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001331void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
1333 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001334 if (!fw_is_builtin_firmware(fw))
1335 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 kfree(fw);
1337 }
1338}
Daniel Mackf4945132013-05-23 22:17:18 +02001339EXPORT_SYMBOL(release_firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341/* Async support */
1342struct firmware_work {
1343 struct work_struct work;
1344 struct module *module;
1345 const char *name;
1346 struct device *device;
1347 void *context;
1348 void (*cont)(const struct firmware *fw, void *context);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001349 unsigned int opt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350};
1351
Stephen Boyda36cf842012-03-28 23:31:00 +02001352static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Stephen Boyda36cf842012-03-28 23:31:00 +02001354 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001356
Stephen Boyda36cf842012-03-28 23:31:00 +02001357 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001358
Stephen Boyda098ecd2016-08-02 14:04:28 -07001359 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001360 fw_work->opt_flags);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001361 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001362 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 module_put(fw_work->module);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001365 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
1369/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001370 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001371 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001372 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001373 * is non-zero else the firmware copy must be done manually.
1374 * @name: name of firmware file
1375 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001376 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001377 * @context: will be passed over to @cont, and
1378 * @fw may be %NULL if firmware request fails.
1379 * @cont: function will be called asynchronously when the firmware
1380 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001382 * Caller must hold the reference count of @device.
1383 *
Ming Lei6f21a622012-08-04 12:01:24 +08001384 * Asynchronous variant of request_firmware() for user contexts:
1385 * - sleep for as small periods as possible since it may
Silvio Fricke88bcef52016-11-25 15:59:47 +01001386 * increase kernel boot time of built-in device drivers
1387 * requesting firmware in their ->probe() methods, if
1388 * @gfp is GFP_KERNEL.
Ming Lei6f21a622012-08-04 12:01:24 +08001389 *
1390 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 **/
1392int
1393request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001394 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001395 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 void (*cont)(const struct firmware *fw, void *context))
1397{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001398 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Andrei Opreaea310032015-03-08 12:41:15 +02001400 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (!fw_work)
1402 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001403
1404 fw_work->module = module;
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001405 fw_work->name = kstrdup_const(name, gfp);
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001406 if (!fw_work->name) {
1407 kfree(fw_work);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001408 return -ENOMEM;
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001409 }
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001410 fw_work->device = device;
1411 fw_work->context = context;
1412 fw_work->cont = cont;
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001413 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
Takashi Iwai5a1379e2014-06-04 17:48:15 +02001414 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (!try_module_get(module)) {
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001417 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 kfree(fw_work);
1419 return -EFAULT;
1420 }
1421
Ming Lei0cfc1e12012-08-04 12:01:23 +08001422 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001423 INIT_WORK(&fw_work->work, request_firmware_work_func);
1424 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return 0;
1426}
Daniel Mackf4945132013-05-23 22:17:18 +02001427EXPORT_SYMBOL(request_firmware_nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Ming Lei90f890812013-06-20 12:30:16 +08001429#ifdef CONFIG_PM_SLEEP
1430static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1431
Ming Lei2887b392012-08-04 12:01:22 +08001432/**
1433 * cache_firmware - cache one firmware image in kernel memory space
1434 * @fw_name: the firmware image name
1435 *
1436 * Cache firmware in kernel memory so that drivers can use it when
1437 * system isn't ready for them to request firmware image from userspace.
1438 * Once it returns successfully, driver can use request_firmware or its
1439 * nowait version to get the cached firmware without any interacting
1440 * with userspace
1441 *
1442 * Return 0 if the firmware image has been cached successfully
1443 * Return !0 otherwise
1444 *
1445 */
Ming Lei93232e42013-06-06 20:01:47 +08001446static int cache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001447{
1448 int ret;
1449 const struct firmware *fw;
1450
1451 pr_debug("%s: %s\n", __func__, fw_name);
1452
1453 ret = request_firmware(&fw, fw_name, NULL);
1454 if (!ret)
1455 kfree(fw);
1456
1457 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1458
1459 return ret;
1460}
1461
Ming Lei6a2c1232013-06-26 09:28:17 +08001462static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1463{
1464 struct firmware_buf *tmp;
1465 struct firmware_cache *fwc = &fw_cache;
1466
1467 spin_lock(&fwc->lock);
1468 tmp = __fw_lookup_buf(fw_name);
1469 spin_unlock(&fwc->lock);
1470
1471 return tmp;
1472}
1473
Ming Lei2887b392012-08-04 12:01:22 +08001474/**
1475 * uncache_firmware - remove one cached firmware image
1476 * @fw_name: the firmware image name
1477 *
1478 * Uncache one firmware image which has been cached successfully
1479 * before.
1480 *
1481 * Return 0 if the firmware cache has been removed successfully
1482 * Return !0 otherwise
1483 *
1484 */
Ming Lei93232e42013-06-06 20:01:47 +08001485static int uncache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001486{
1487 struct firmware_buf *buf;
1488 struct firmware fw;
1489
1490 pr_debug("%s: %s\n", __func__, fw_name);
1491
Stephen Boyda098ecd2016-08-02 14:04:28 -07001492 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
Ming Lei2887b392012-08-04 12:01:22 +08001493 return 0;
1494
1495 buf = fw_lookup_buf(fw_name);
1496 if (buf) {
1497 fw_free_buf(buf);
1498 return 0;
1499 }
1500
1501 return -EINVAL;
1502}
1503
Ming Lei37276a52012-08-04 12:01:27 +08001504static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1505{
1506 struct fw_cache_entry *fce;
1507
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001508 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
Ming Lei37276a52012-08-04 12:01:27 +08001509 if (!fce)
1510 goto exit;
1511
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001512 fce->name = kstrdup_const(name, GFP_ATOMIC);
1513 if (!fce->name) {
1514 kfree(fce);
1515 fce = NULL;
1516 goto exit;
1517 }
Ming Lei37276a52012-08-04 12:01:27 +08001518exit:
1519 return fce;
1520}
1521
Ming Lei373304f2012-10-09 12:01:01 +08001522static int __fw_entry_found(const char *name)
1523{
1524 struct firmware_cache *fwc = &fw_cache;
1525 struct fw_cache_entry *fce;
1526
1527 list_for_each_entry(fce, &fwc->fw_names, list) {
1528 if (!strcmp(fce->name, name))
1529 return 1;
1530 }
1531 return 0;
1532}
1533
Ming Leiac39b3e2012-08-20 19:04:16 +08001534static int fw_cache_piggyback_on_request(const char *name)
1535{
1536 struct firmware_cache *fwc = &fw_cache;
1537 struct fw_cache_entry *fce;
1538 int ret = 0;
1539
1540 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001541 if (__fw_entry_found(name))
1542 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001543
1544 fce = alloc_fw_cache_entry(name);
1545 if (fce) {
1546 ret = 1;
1547 list_add(&fce->list, &fwc->fw_names);
1548 pr_debug("%s: fw: %s\n", __func__, name);
1549 }
1550found:
1551 spin_unlock(&fwc->name_lock);
1552 return ret;
1553}
1554
Ming Lei37276a52012-08-04 12:01:27 +08001555static void free_fw_cache_entry(struct fw_cache_entry *fce)
1556{
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001557 kfree_const(fce->name);
Ming Lei37276a52012-08-04 12:01:27 +08001558 kfree(fce);
1559}
1560
1561static void __async_dev_cache_fw_image(void *fw_entry,
1562 async_cookie_t cookie)
1563{
1564 struct fw_cache_entry *fce = fw_entry;
1565 struct firmware_cache *fwc = &fw_cache;
1566 int ret;
1567
1568 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001569 if (ret) {
1570 spin_lock(&fwc->name_lock);
1571 list_del(&fce->list);
1572 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001573
Ming Leiac39b3e2012-08-20 19:04:16 +08001574 free_fw_cache_entry(fce);
1575 }
Ming Lei37276a52012-08-04 12:01:27 +08001576}
1577
1578/* called with dev->devres_lock held */
1579static void dev_create_fw_entry(struct device *dev, void *res,
1580 void *data)
1581{
1582 struct fw_name_devm *fwn = res;
1583 const char *fw_name = fwn->name;
1584 struct list_head *head = data;
1585 struct fw_cache_entry *fce;
1586
1587 fce = alloc_fw_cache_entry(fw_name);
1588 if (fce)
1589 list_add(&fce->list, head);
1590}
1591
1592static int devm_name_match(struct device *dev, void *res,
1593 void *match_data)
1594{
1595 struct fw_name_devm *fwn = res;
1596 return (fwn->magic == (unsigned long)match_data);
1597}
1598
Ming Leiab6dd8e2012-08-17 22:07:00 +08001599static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001600{
1601 LIST_HEAD(todo);
1602 struct fw_cache_entry *fce;
1603 struct fw_cache_entry *fce_next;
1604 struct firmware_cache *fwc = &fw_cache;
1605
1606 devres_for_each_res(dev, fw_name_devm_release,
1607 devm_name_match, &fw_cache,
1608 dev_create_fw_entry, &todo);
1609
1610 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1611 list_del(&fce->list);
1612
1613 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001614 /* only one cache entry for one firmware */
1615 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001616 list_add(&fce->list, &fwc->fw_names);
1617 } else {
1618 free_fw_cache_entry(fce);
1619 fce = NULL;
1620 }
Ming Lei37276a52012-08-04 12:01:27 +08001621 spin_unlock(&fwc->name_lock);
1622
Ming Lei373304f2012-10-09 12:01:01 +08001623 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001624 async_schedule_domain(__async_dev_cache_fw_image,
1625 (void *)fce,
1626 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001627 }
1628}
1629
1630static void __device_uncache_fw_images(void)
1631{
1632 struct firmware_cache *fwc = &fw_cache;
1633 struct fw_cache_entry *fce;
1634
1635 spin_lock(&fwc->name_lock);
1636 while (!list_empty(&fwc->fw_names)) {
1637 fce = list_entry(fwc->fw_names.next,
1638 struct fw_cache_entry, list);
1639 list_del(&fce->list);
1640 spin_unlock(&fwc->name_lock);
1641
1642 uncache_firmware(fce->name);
1643 free_fw_cache_entry(fce);
1644
1645 spin_lock(&fwc->name_lock);
1646 }
1647 spin_unlock(&fwc->name_lock);
1648}
1649
1650/**
1651 * device_cache_fw_images - cache devices' firmware
1652 *
1653 * If one device called request_firmware or its nowait version
1654 * successfully before, the firmware names are recored into the
1655 * device's devres link list, so device_cache_fw_images can call
1656 * cache_firmware() to cache these firmwares for the device,
1657 * then the device driver can load its firmwares easily at
1658 * time when system is not ready to complete loading firmware.
1659 */
1660static void device_cache_fw_images(void)
1661{
1662 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001663 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001664 DEFINE_WAIT(wait);
1665
1666 pr_debug("%s\n", __func__);
1667
Ming Lei373304f2012-10-09 12:01:01 +08001668 /* cancel uncache work */
1669 cancel_delayed_work_sync(&fwc->work);
1670
Ming Leiffe53f62012-08-04 12:01:28 +08001671 /*
1672 * use small loading timeout for caching devices' firmware
1673 * because all these firmware images have been loaded
1674 * successfully at lease once, also system is ready for
1675 * completing firmware loading now. The maximum size of
1676 * firmware in current distributions is about 2M bytes,
1677 * so 10 secs should be enough.
1678 */
1679 old_timeout = loading_timeout;
1680 loading_timeout = 10;
1681
Ming Leiac39b3e2012-08-20 19:04:16 +08001682 mutex_lock(&fw_lock);
1683 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001684 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001685 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001686
1687 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001688 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001689
1690 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001691}
1692
1693/**
1694 * device_uncache_fw_images - uncache devices' firmware
1695 *
1696 * uncache all firmwares which have been cached successfully
1697 * by device_uncache_fw_images earlier
1698 */
1699static void device_uncache_fw_images(void)
1700{
1701 pr_debug("%s\n", __func__);
1702 __device_uncache_fw_images();
1703}
1704
1705static void device_uncache_fw_images_work(struct work_struct *work)
1706{
1707 device_uncache_fw_images();
1708}
1709
1710/**
1711 * device_uncache_fw_images_delay - uncache devices firmwares
1712 * @delay: number of milliseconds to delay uncache device firmwares
1713 *
1714 * uncache all devices's firmwares which has been cached successfully
1715 * by device_cache_fw_images after @delay milliseconds.
1716 */
1717static void device_uncache_fw_images_delay(unsigned long delay)
1718{
Shaibal Duttabce66182014-01-31 15:44:58 -08001719 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1720 msecs_to_jiffies(delay));
Ming Lei37276a52012-08-04 12:01:27 +08001721}
1722
Ming Lei07646d92012-08-04 12:01:29 +08001723static int fw_pm_notify(struct notifier_block *notify_block,
1724 unsigned long mode, void *unused)
1725{
1726 switch (mode) {
1727 case PM_HIBERNATION_PREPARE:
1728 case PM_SUSPEND_PREPARE:
Sebastian Capellaf8d5b9e2014-02-18 17:52:08 -08001729 case PM_RESTORE_PREPARE:
Ming Leiaf5bc112013-05-27 18:30:04 +08001730 kill_requests_without_uevent();
Ming Lei07646d92012-08-04 12:01:29 +08001731 device_cache_fw_images();
1732 break;
1733
1734 case PM_POST_SUSPEND:
1735 case PM_POST_HIBERNATION:
1736 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001737 /*
1738 * In case that system sleep failed and syscore_suspend is
1739 * not called.
1740 */
1741 mutex_lock(&fw_lock);
1742 fw_cache.state = FW_LOADER_NO_CACHE;
1743 mutex_unlock(&fw_lock);
1744
Ming Lei07646d92012-08-04 12:01:29 +08001745 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1746 break;
1747 }
1748
1749 return 0;
1750}
Ming Lei07646d92012-08-04 12:01:29 +08001751
Ming Leiac39b3e2012-08-20 19:04:16 +08001752/* stop caching firmware once syscore_suspend is reached */
1753static int fw_suspend(void)
1754{
1755 fw_cache.state = FW_LOADER_NO_CACHE;
1756 return 0;
1757}
1758
1759static struct syscore_ops fw_syscore_ops = {
1760 .suspend = fw_suspend,
1761};
Ming Leicfe016b2012-09-08 17:32:30 +08001762#else
1763static int fw_cache_piggyback_on_request(const char *name)
1764{
1765 return 0;
1766}
1767#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001768
Ming Lei37276a52012-08-04 12:01:27 +08001769static void __init fw_cache_init(void)
1770{
1771 spin_lock_init(&fw_cache.lock);
1772 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001773 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001774
Ming Leicfe016b2012-09-08 17:32:30 +08001775#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001776 spin_lock_init(&fw_cache.name_lock);
1777 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001778
Ming Lei37276a52012-08-04 12:01:27 +08001779 INIT_DELAYED_WORK(&fw_cache.work,
1780 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001781
1782 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1783 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001784
1785 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001786#endif
Ming Lei37276a52012-08-04 12:01:27 +08001787}
1788
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001789static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Ming Lei1f2b7952012-08-04 12:01:21 +08001791 fw_cache_init();
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001792#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001793 register_reboot_notifier(&fw_shutdown_nb);
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001794 return class_register(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001795#else
1796 return 0;
1797#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001799
1800static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Ming Leicfe016b2012-09-08 17:32:30 +08001802#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001803 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001804 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001805#endif
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001806#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001807 unregister_reboot_notifier(&fw_shutdown_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 class_unregister(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001809#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811
Shaohua Lia30a6a22006-09-27 01:50:52 -07001812fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813module_exit(firmware_class_exit);