blob: 4f5105f57521326dec1210e0881081b0d944579b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * firmware_class.c - Multi purpose firmware loading support
3 *
Markus Rechberger87d37a42007-06-04 18:45:44 +02004 * Copyright (c) 2003 Manuel Estrada Sainz
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Please see Documentation/firmware_class/ for more information.
7 *
8 */
9
Randy.Dunlapc59ede72006-01-11 12:17:46 -080010#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/device.h>
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/timer.h>
15#include <linux/vmalloc.h>
16#include <linux/interrupt.h>
17#include <linux/bitops.h>
Laura Garciacad1e552006-05-23 23:22:38 +020018#include <linux/mutex.h>
Stephen Boyda36cf842012-03-28 23:31:00 +020019#include <linux/workqueue.h>
David Woodhouse6e03a202009-04-09 22:04:07 -070020#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/firmware.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Stephen Boyda36cf842012-03-28 23:31:00 +020023#include <linux/sched.h>
Linus Torvaldsabb139e2012-10-03 15:58:32 -070024#include <linux/file.h>
Ming Lei1f2b7952012-08-04 12:01:21 +080025#include <linux/list.h>
Mimi Zohare40ba6d2015-11-19 12:39:22 -050026#include <linux/fs.h>
Ming Lei37276a52012-08-04 12:01:27 +080027#include <linux/async.h>
28#include <linux/pm.h>
Ming Lei07646d92012-08-04 12:01:29 +080029#include <linux/suspend.h>
Ming Leiac39b3e2012-08-20 19:04:16 +080030#include <linux/syscore_ops.h>
Takashi Iwaife304142013-05-22 18:28:38 +020031#include <linux/reboot.h>
Kees Cook6593d922014-02-25 13:06:00 -080032#include <linux/security.h>
Ming Lei37276a52012-08-04 12:01:27 +080033
Linus Torvaldsabb139e2012-10-03 15:58:32 -070034#include <generated/utsrelease.h>
35
Ming Lei37276a52012-08-04 12:01:27 +080036#include "base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Markus Rechberger87d37a42007-06-04 18:45:44 +020038MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070039MODULE_DESCRIPTION("Multi purpose firmware loading support");
40MODULE_LICENSE("GPL");
41
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080042/* Builtin firmware support */
43
44#ifdef CONFIG_FW_LOADER
45
46extern struct builtin_fw __start_builtin_fw[];
47extern struct builtin_fw __end_builtin_fw[];
48
Stephen Boyda098ecd2016-08-02 14:04:28 -070049static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
50 void *buf, size_t size)
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080051{
52 struct builtin_fw *b_fw;
53
54 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
55 if (strcmp(name, b_fw->name) == 0) {
56 fw->size = b_fw->size;
57 fw->data = b_fw->data;
Stephen Boyda098ecd2016-08-02 14:04:28 -070058
59 if (buf && fw->size <= size)
60 memcpy(buf, fw->data, fw->size);
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080061 return true;
62 }
63 }
64
65 return false;
66}
67
68static bool fw_is_builtin_firmware(const struct firmware *fw)
69{
70 struct builtin_fw *b_fw;
71
72 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
73 if (fw->data == b_fw->data)
74 return true;
75
76 return false;
77}
78
79#else /* Module case - no builtin firmware support */
80
Stephen Boyda098ecd2016-08-02 14:04:28 -070081static inline bool fw_get_builtin_firmware(struct firmware *fw,
82 const char *name, void *buf,
83 size_t size)
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080084{
85 return false;
86}
87
88static inline bool fw_is_builtin_firmware(const struct firmware *fw)
89{
90 return false;
91}
92#endif
93
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010094enum fw_status {
95 FW_STATUS_UNKNOWN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 FW_STATUS_LOADING,
97 FW_STATUS_DONE,
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010098 FW_STATUS_ABORTED,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
Dave Jones2f651682007-01-25 15:56:15 -0500101static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200103static inline long firmware_loading_timeout(void)
104{
Ming Lei68ff2a02015-01-13 00:02:01 +0800105 return loading_timeout > 0 ? loading_timeout * HZ : MAX_JIFFY_OFFSET;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200106}
107
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100108/*
109 * Concurrent request_firmware() for the same firmware need to be
110 * serialized. struct fw_state is simple state machine which hold the
111 * state of the firmware loading.
112 */
113struct fw_state {
114 struct completion completion;
115 unsigned long status;
116};
117
118static void fw_state_init(struct fw_state *fw_st)
119{
120 init_completion(&fw_st->completion);
121 fw_st->status = FW_STATUS_UNKNOWN;
122}
123
124static int __fw_state_check(struct fw_state *fw_st, enum fw_status status)
125{
126 return test_bit(status, &fw_st->status);
127}
128
129static long __fw_state_wait_common(struct fw_state *fw_st, long timeout)
130{
131 long ret;
132
133 ret = wait_for_completion_interruptible_timeout(&fw_st->completion,
134 timeout);
135 if (ret != 0 && test_bit(FW_STATUS_ABORTED, &fw_st->status))
136 return -ENOENT;
137
138 return ret;
139}
140
141static void __fw_state_set(struct fw_state *fw_st,
142 enum fw_status status)
143{
144 set_bit(status, &fw_st->status);
145
146 if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED) {
147 clear_bit(FW_STATUS_LOADING, &fw_st->status);
148 complete_all(&fw_st->completion);
149 }
150}
151
152#define fw_state_start(fw_st) \
153 __fw_state_set(fw_st, FW_STATUS_LOADING)
154#define fw_state_done(fw_st) \
155 __fw_state_set(fw_st, FW_STATUS_DONE)
156#define fw_state_is_done(fw_st) \
157 __fw_state_check(fw_st, FW_STATUS_DONE)
158#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
167#define fw_state_aborted(fw_st) \
168 __fw_state_set(fw_st, FW_STATUS_ABORTED)
169#define fw_state_is_loading(fw_st) \
170 __fw_state_check(fw_st, FW_STATUS_LOADING)
171#define fw_state_is_aborted(fw_st) \
172 __fw_state_check(fw_st, FW_STATUS_ABORTED)
173#define fw_state_wait_timeout(fw_st, timeout) \
174 __fw_state_wait_common(fw_st, timeout)
175
176#endif /* CONFIG_FW_LOADER_USER_HELPER */
177
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100178/* firmware behavior options */
179#define FW_OPT_UEVENT (1U << 0)
180#define FW_OPT_NOWAIT (1U << 1)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100181#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200182#define FW_OPT_USERHELPER (1U << 2)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100183#else
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200184#define FW_OPT_USERHELPER 0
185#endif
186#ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK
187#define FW_OPT_FALLBACK FW_OPT_USERHELPER
188#else
189#define FW_OPT_FALLBACK 0
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100190#endif
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -0700191#define FW_OPT_NO_WARN (1U << 3)
Vikram Mulukutla0e742e92016-08-02 14:04:25 -0700192#define FW_OPT_NOCACHE (1U << 4)
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100193
Ming Lei1f2b7952012-08-04 12:01:21 +0800194struct firmware_cache {
195 /* firmware_buf instance will be added into the below list */
196 spinlock_t lock;
197 struct list_head head;
Ming Leicfe016b2012-09-08 17:32:30 +0800198 int state;
Ming Lei37276a52012-08-04 12:01:27 +0800199
Ming Leicfe016b2012-09-08 17:32:30 +0800200#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +0800201 /*
202 * Names of firmware images which have been cached successfully
203 * will be added into the below list so that device uncache
204 * helper can trace which firmware images have been cached
205 * before.
206 */
207 spinlock_t name_lock;
208 struct list_head fw_names;
209
Ming Lei37276a52012-08-04 12:01:27 +0800210 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +0800211
212 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +0800213#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800214};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Ming Lei12446912012-08-04 12:01:20 +0800216struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800217 struct kref ref;
218 struct list_head list;
Ming Lei1f2b7952012-08-04 12:01:21 +0800219 struct firmware_cache *fwc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100220 struct fw_state fw_st;
Ming Lei65710cb2012-08-04 12:01:16 +0800221 void *data;
222 size_t size;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700223 size_t allocated_size;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100224#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100225 bool is_paged_buf;
Ming Leiaf5bc112013-05-27 18:30:04 +0800226 bool need_uevent;
David Woodhouse6e03a202009-04-09 22:04:07 -0700227 struct page **pages;
228 int nr_pages;
229 int page_array_size;
Takashi Iwaife304142013-05-22 18:28:38 +0200230 struct list_head pending_list;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100231#endif
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700232 const char *fw_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233};
234
Ming Lei37276a52012-08-04 12:01:27 +0800235struct fw_cache_entry {
236 struct list_head list;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700237 const char *name;
Ming Lei37276a52012-08-04 12:01:27 +0800238};
239
Ming Leif531f05a2012-08-04 12:01:25 +0800240struct fw_name_devm {
241 unsigned long magic;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700242 const char *name;
Ming Leif531f05a2012-08-04 12:01:25 +0800243};
244
Ming Lei1f2b7952012-08-04 12:01:21 +0800245#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
246
Ming Leiac39b3e2012-08-20 19:04:16 +0800247#define FW_LOADER_NO_CACHE 0
248#define FW_LOADER_START_CACHE 1
249
250static int fw_cache_piggyback_on_request(const char *name);
251
Ming Lei1f2b7952012-08-04 12:01:21 +0800252/* fw_lock could be moved to 'struct firmware_priv' but since it is just
253 * guarding for corner cases a global lock should be OK */
254static DEFINE_MUTEX(fw_lock);
255
256static struct firmware_cache fw_cache;
257
258static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
Stephen Boyda098ecd2016-08-02 14:04:28 -0700259 struct firmware_cache *fwc,
260 void *dbuf, size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800261{
262 struct firmware_buf *buf;
263
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700264 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
Ming Lei1f2b7952012-08-04 12:01:21 +0800265 if (!buf)
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700266 return NULL;
267
268 buf->fw_id = kstrdup_const(fw_name, GFP_ATOMIC);
269 if (!buf->fw_id) {
270 kfree(buf);
271 return NULL;
272 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800273
274 kref_init(&buf->ref);
Ming Lei1f2b7952012-08-04 12:01:21 +0800275 buf->fwc = fwc;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700276 buf->data = dbuf;
277 buf->allocated_size = size;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100278 fw_state_init(&buf->fw_st);
Takashi Iwaife304142013-05-22 18:28:38 +0200279#ifdef CONFIG_FW_LOADER_USER_HELPER
280 INIT_LIST_HEAD(&buf->pending_list);
281#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800282
283 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
284
285 return buf;
286}
287
Ming Lei2887b392012-08-04 12:01:22 +0800288static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
289{
290 struct firmware_buf *tmp;
291 struct firmware_cache *fwc = &fw_cache;
292
293 list_for_each_entry(tmp, &fwc->head, list)
294 if (!strcmp(tmp->fw_id, fw_name))
295 return tmp;
296 return NULL;
297}
298
Ming Lei1f2b7952012-08-04 12:01:21 +0800299static int fw_lookup_and_allocate_buf(const char *fw_name,
300 struct firmware_cache *fwc,
Stephen Boyda098ecd2016-08-02 14:04:28 -0700301 struct firmware_buf **buf, void *dbuf,
302 size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800303{
304 struct firmware_buf *tmp;
305
306 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800307 tmp = __fw_lookup_buf(fw_name);
308 if (tmp) {
309 kref_get(&tmp->ref);
310 spin_unlock(&fwc->lock);
311 *buf = tmp;
312 return 1;
313 }
Stephen Boyda098ecd2016-08-02 14:04:28 -0700314 tmp = __allocate_fw_buf(fw_name, fwc, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +0800315 if (tmp)
316 list_add(&tmp->list, &fwc->head);
317 spin_unlock(&fwc->lock);
318
319 *buf = tmp;
320
321 return tmp ? 0 : -ENOMEM;
322}
323
324static void __fw_free_buf(struct kref *ref)
Bart Van Assche98233b22014-01-04 14:20:36 +0100325 __releases(&fwc->lock)
Ming Lei1f2b7952012-08-04 12:01:21 +0800326{
327 struct firmware_buf *buf = to_fwbuf(ref);
328 struct firmware_cache *fwc = buf->fwc;
Ming Lei1f2b7952012-08-04 12:01:21 +0800329
330 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
331 __func__, buf->fw_id, buf, buf->data,
332 (unsigned int)buf->size);
333
Ming Lei1f2b7952012-08-04 12:01:21 +0800334 list_del(&buf->list);
335 spin_unlock(&fwc->lock);
336
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100337#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100338 if (buf->is_paged_buf) {
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100339 int i;
Ming Lei746058f2012-10-09 12:01:03 +0800340 vunmap(buf->data);
341 for (i = 0; i < buf->nr_pages; i++)
342 __free_page(buf->pages[i]);
Chen Feng10a3fbf2015-12-16 17:03:15 +0800343 vfree(buf->pages);
Ming Lei746058f2012-10-09 12:01:03 +0800344 } else
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100345#endif
Stephen Boyda098ecd2016-08-02 14:04:28 -0700346 if (!buf->allocated_size)
Ming Lei746058f2012-10-09 12:01:03 +0800347 vfree(buf->data);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700348 kfree_const(buf->fw_id);
Ming Lei1f2b7952012-08-04 12:01:21 +0800349 kfree(buf);
350}
351
352static void fw_free_buf(struct firmware_buf *buf)
353{
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800354 struct firmware_cache *fwc = buf->fwc;
355 spin_lock(&fwc->lock);
356 if (!kref_put(&buf->ref, __fw_free_buf))
357 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800358}
359
Ming Lei746058f2012-10-09 12:01:03 +0800360/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800361static char fw_path_para[256];
362static const char * const fw_path[] = {
363 fw_path_para,
Ming Lei746058f2012-10-09 12:01:03 +0800364 "/lib/firmware/updates/" UTS_RELEASE,
365 "/lib/firmware/updates",
366 "/lib/firmware/" UTS_RELEASE,
367 "/lib/firmware"
368};
369
Ming Lei27602842012-11-03 17:47:58 +0800370/*
371 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
372 * from kernel command line because firmware_class is generally built in
373 * kernel instead of module.
374 */
375module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
376MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
377
Luis R. Rodriguez5275d192015-07-30 15:48:57 -0700378static void fw_finish_direct_load(struct device *device,
379 struct firmware_buf *buf)
380{
381 mutex_lock(&fw_lock);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100382 fw_state_done(&buf->fw_st);
Luis R. Rodriguez5275d192015-07-30 15:48:57 -0700383 mutex_unlock(&fw_lock);
384}
385
Stephen Boyda098ecd2016-08-02 14:04:28 -0700386static int
387fw_get_filesystem_firmware(struct device *device, struct firmware_buf *buf)
Ming Lei746058f2012-10-09 12:01:03 +0800388{
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500389 loff_t size;
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700390 int i, len;
Neil Horman3e358ac2013-09-06 15:36:08 -0400391 int rc = -ENOENT;
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700392 char *path;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700393 enum kernel_read_file_id id = READING_FIRMWARE;
394 size_t msize = INT_MAX;
395
396 /* Already populated data member means we're loading into a buffer */
397 if (buf->data) {
398 id = READING_FIRMWARE_PREALLOC_BUFFER;
399 msize = buf->allocated_size;
400 }
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700401
402 path = __getname();
403 if (!path)
404 return -ENOMEM;
Ming Lei746058f2012-10-09 12:01:03 +0800405
406 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
Ming Lei27602842012-11-03 17:47:58 +0800407 /* skip the unset customized path */
408 if (!fw_path[i][0])
409 continue;
410
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700411 len = snprintf(path, PATH_MAX, "%s/%s",
412 fw_path[i], buf->fw_id);
413 if (len >= PATH_MAX) {
414 rc = -ENAMETOOLONG;
415 break;
416 }
Ming Lei746058f2012-10-09 12:01:03 +0800417
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500418 buf->size = 0;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700419 rc = kernel_read_file_from_path(path, &buf->data, &size, msize,
420 id);
Kees Cook4b2530d2016-02-04 13:15:02 -0800421 if (rc) {
Luis R. Rodriguez8e516aa2016-02-28 21:57:55 +0100422 if (rc == -ENOENT)
423 dev_dbg(device, "loading %s failed with error %d\n",
424 path, rc);
425 else
426 dev_warn(device, "loading %s failed with error %d\n",
427 path, rc);
Kees Cook4b2530d2016-02-04 13:15:02 -0800428 continue;
429 }
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500430 dev_dbg(device, "direct-loading %s\n", buf->fw_id);
431 buf->size = size;
Luis R. Rodriguez5275d192015-07-30 15:48:57 -0700432 fw_finish_direct_load(device, buf);
Kees Cook4b2530d2016-02-04 13:15:02 -0800433 break;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100434 }
Kees Cook4b2530d2016-02-04 13:15:02 -0800435 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100436
Neil Horman3e358ac2013-09-06 15:36:08 -0400437 return rc;
Ming Lei746058f2012-10-09 12:01:03 +0800438}
439
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100440/* firmware holds the ownership of pages */
441static void firmware_free_data(const struct firmware *fw)
442{
443 /* Loaded directly? */
444 if (!fw->priv) {
445 vfree(fw->data);
446 return;
447 }
448 fw_free_buf(fw->priv);
449}
450
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100451/* store the pages buffer info firmware from buf */
452static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
453{
454 fw->priv = buf;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100455#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100456 fw->pages = buf->pages;
457#endif
458 fw->size = buf->size;
459 fw->data = buf->data;
460
461 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
462 __func__, buf->fw_id, buf, buf->data,
463 (unsigned int)buf->size);
464}
465
466#ifdef CONFIG_PM_SLEEP
467static void fw_name_devm_release(struct device *dev, void *res)
468{
469 struct fw_name_devm *fwn = res;
470
471 if (fwn->magic == (unsigned long)&fw_cache)
472 pr_debug("%s: fw_name-%s devm-%p released\n",
473 __func__, fwn->name, res);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700474 kfree_const(fwn->name);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100475}
476
477static int fw_devm_match(struct device *dev, void *res,
478 void *match_data)
479{
480 struct fw_name_devm *fwn = res;
481
482 return (fwn->magic == (unsigned long)&fw_cache) &&
483 !strcmp(fwn->name, match_data);
484}
485
486static struct fw_name_devm *fw_find_devm_name(struct device *dev,
487 const char *name)
488{
489 struct fw_name_devm *fwn;
490
491 fwn = devres_find(dev, fw_name_devm_release,
492 fw_devm_match, (void *)name);
493 return fwn;
494}
495
496/* add firmware name into devres list */
497static int fw_add_devm_name(struct device *dev, const char *name)
498{
499 struct fw_name_devm *fwn;
500
501 fwn = fw_find_devm_name(dev, name);
502 if (fwn)
503 return 1;
504
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700505 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
506 GFP_KERNEL);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100507 if (!fwn)
508 return -ENOMEM;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700509 fwn->name = kstrdup_const(name, GFP_KERNEL);
510 if (!fwn->name) {
Vladimir Zapolskiya885de62015-07-29 23:26:28 +0300511 devres_free(fwn);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700512 return -ENOMEM;
513 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100514
515 fwn->magic = (unsigned long)&fw_cache;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100516 devres_add(dev, fwn);
517
518 return 0;
519}
520#else
521static int fw_add_devm_name(struct device *dev, const char *name)
522{
523 return 0;
524}
525#endif
526
527
528/*
529 * user-mode helper code
530 */
531#ifdef CONFIG_FW_LOADER_USER_HELPER
532struct firmware_priv {
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100533 bool nowait;
534 struct device dev;
535 struct firmware_buf *buf;
536 struct firmware *fw;
537};
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100538
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700539static struct firmware_priv *to_firmware_priv(struct device *dev)
540{
541 return container_of(dev, struct firmware_priv, dev);
542}
543
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700544static void __fw_load_abort(struct firmware_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Ming Lei87597932013-06-15 16:36:38 +0800546 /*
547 * There is a small window in which user can write to 'loading'
548 * between loading done and disappearance of 'loading'
549 */
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100550 if (fw_state_is_done(&buf->fw_st))
Ming Lei87597932013-06-15 16:36:38 +0800551 return;
552
Takashi Iwaife304142013-05-22 18:28:38 +0200553 list_del_init(&buf->pending_list);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100554 fw_state_aborted(&buf->fw_st);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700557static void fw_load_abort(struct firmware_priv *fw_priv)
558{
559 struct firmware_buf *buf = fw_priv->buf;
560
561 __fw_load_abort(buf);
Ming Lei87597932013-06-15 16:36:38 +0800562
563 /* avoid user action after loading abort */
564 fw_priv->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Takashi Iwaife304142013-05-22 18:28:38 +0200567static LIST_HEAD(pending_fw_head);
568
569/* reboot notifier for avoid deadlock with usermode_lock */
570static int fw_shutdown_notify(struct notifier_block *unused1,
571 unsigned long unused2, void *unused3)
572{
573 mutex_lock(&fw_lock);
574 while (!list_empty(&pending_fw_head))
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700575 __fw_load_abort(list_first_entry(&pending_fw_head,
Takashi Iwaife304142013-05-22 18:28:38 +0200576 struct firmware_buf,
577 pending_list));
578 mutex_unlock(&fw_lock);
579 return NOTIFY_DONE;
580}
581
582static struct notifier_block fw_shutdown_nb = {
583 .notifier_call = fw_shutdown_notify,
584};
585
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700586static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
587 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 return sprintf(buf, "%d\n", loading_timeout);
590}
591
592/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800593 * firmware_timeout_store - set number of seconds to wait for firmware
594 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800595 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800596 * @buf: buffer to scan for timeout value
597 * @count: number of bytes in @buf
598 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800600 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 * firmware will be provided.
602 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800603 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 **/
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700605static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
606 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700609 if (loading_timeout < 0)
610 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return count;
613}
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100614static CLASS_ATTR_RW(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100616static struct attribute *firmware_class_attrs[] = {
617 &class_attr_timeout.attr,
618 NULL,
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800619};
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100620ATTRIBUTE_GROUPS(firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800622static void fw_dev_release(struct device *dev)
623{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700624 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800625
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800626 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800627}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Linus Torvalds6f957722015-07-09 11:20:01 -0700629static int do_firmware_uevent(struct firmware_priv *fw_priv, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Ming Lei12446912012-08-04 12:01:20 +0800631 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200633 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700634 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200635 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
636 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 return 0;
639}
640
Linus Torvalds6f957722015-07-09 11:20:01 -0700641static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
642{
643 struct firmware_priv *fw_priv = to_firmware_priv(dev);
644 int err = 0;
645
646 mutex_lock(&fw_lock);
647 if (fw_priv->buf)
648 err = do_firmware_uevent(fw_priv, env);
649 mutex_unlock(&fw_lock);
650 return err;
651}
652
Adrian Bunk1b81d662006-05-20 15:00:16 -0700653static struct class firmware_class = {
654 .name = "firmware",
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100655 .class_groups = firmware_class_groups,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700656 .dev_uevent = firmware_uevent,
657 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700658};
659
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700660static ssize_t firmware_loading_show(struct device *dev,
661 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700663 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800664 int loading = 0;
665
666 mutex_lock(&fw_lock);
667 if (fw_priv->buf)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100668 loading = fw_state_is_loading(&fw_priv->buf->fw_st);
Ming Lei87597932013-06-15 16:36:38 +0800669 mutex_unlock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return sprintf(buf, "%d\n", loading);
672}
673
David Woodhouse6e03a202009-04-09 22:04:07 -0700674/* Some architectures don't have PAGE_KERNEL_RO */
675#ifndef PAGE_KERNEL_RO
676#define PAGE_KERNEL_RO PAGE_KERNEL
677#endif
Ming Lei253c9242012-10-09 12:01:02 +0800678
679/* one pages buffer should be mapped/unmapped only once */
680static int fw_map_pages_buf(struct firmware_buf *buf)
681{
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100682 if (!buf->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800683 return 0;
684
Markus Elfringdaa3d672014-11-19 11:38:38 +0100685 vunmap(buf->data);
Ming Lei253c9242012-10-09 12:01:02 +0800686 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
687 if (!buf->data)
688 return -ENOMEM;
689 return 0;
690}
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800693 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700694 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800695 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800696 * @buf: buffer to scan for loading control value
697 * @count: number of bytes in @buf
698 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 * The relevant values are:
700 *
701 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800702 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 * -1: Conclude the load with an error and discard any written data.
704 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700705static ssize_t firmware_loading_store(struct device *dev,
706 struct device_attribute *attr,
707 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700709 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800710 struct firmware_buf *fw_buf;
Kees Cook6593d922014-02-25 13:06:00 -0800711 ssize_t written = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700713 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Neil Hormaneea915b2012-01-02 15:31:23 -0500715 mutex_lock(&fw_lock);
Ming Lei87597932013-06-15 16:36:38 +0800716 fw_buf = fw_priv->buf;
Ming Lei12446912012-08-04 12:01:20 +0800717 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500718 goto out;
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 switch (loading) {
721 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800722 /* discarding any previous partial load */
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100723 if (!fw_state_is_done(&fw_buf->fw_st)) {
Ming Lei12446912012-08-04 12:01:20 +0800724 for (i = 0; i < fw_buf->nr_pages; i++)
725 __free_page(fw_buf->pages[i]);
Chen Feng10a3fbf2015-12-16 17:03:15 +0800726 vfree(fw_buf->pages);
Ming Lei12446912012-08-04 12:01:20 +0800727 fw_buf->pages = NULL;
728 fw_buf->page_array_size = 0;
729 fw_buf->nr_pages = 0;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100730 fw_state_start(&fw_buf->fw_st);
Ming Lei28eefa72012-08-04 12:01:17 +0800731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 break;
733 case 0:
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100734 if (fw_state_is_loading(&fw_buf->fw_st)) {
Kees Cook6593d922014-02-25 13:06:00 -0800735 int rc;
736
Ming Lei253c9242012-10-09 12:01:02 +0800737 /*
738 * Several loading requests may be pending on
739 * one same firmware buf, so let all requests
740 * see the mapped 'buf->data' once the loading
741 * is completed.
742 * */
Kees Cook6593d922014-02-25 13:06:00 -0800743 rc = fw_map_pages_buf(fw_buf);
744 if (rc)
zhang jun2b1278c2014-02-13 15:18:47 +0800745 dev_err(dev, "%s: map pages failed\n",
746 __func__);
Kees Cook6593d922014-02-25 13:06:00 -0800747 else
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500748 rc = security_kernel_post_read_file(NULL,
749 fw_buf->data, fw_buf->size,
750 READING_FIRMWARE);
Kees Cook6593d922014-02-25 13:06:00 -0800751
752 /*
753 * Same logic as fw_load_abort, only the DONE bit
754 * is ignored and we set ABORT only on failure.
755 */
Takashi Iwaife304142013-05-22 18:28:38 +0200756 list_del_init(&fw_buf->pending_list);
Kees Cook6593d922014-02-25 13:06:00 -0800757 if (rc) {
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100758 fw_state_aborted(&fw_buf->fw_st);
Kees Cook6593d922014-02-25 13:06:00 -0800759 written = rc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100760 } else {
761 fw_state_done(&fw_buf->fw_st);
Kees Cook6593d922014-02-25 13:06:00 -0800762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 break;
764 }
765 /* fallthrough */
766 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700767 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /* fallthrough */
769 case -1:
770 fw_load_abort(fw_priv);
771 break;
772 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500773out:
774 mutex_unlock(&fw_lock);
Kees Cook6593d922014-02-25 13:06:00 -0800775 return written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700778static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Stephen Boyda098ecd2016-08-02 14:04:28 -0700780static void firmware_rw_buf(struct firmware_buf *buf, char *buffer,
781 loff_t offset, size_t count, bool read)
782{
783 if (read)
784 memcpy(buffer, buf->data + offset, count);
785 else
786 memcpy(buf->data + offset, buffer, count);
787}
788
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700789static void firmware_rw(struct firmware_buf *buf, char *buffer,
790 loff_t offset, size_t count, bool read)
791{
792 while (count) {
793 void *page_data;
794 int page_nr = offset >> PAGE_SHIFT;
795 int page_ofs = offset & (PAGE_SIZE-1);
796 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
797
798 page_data = kmap(buf->pages[page_nr]);
799
800 if (read)
801 memcpy(buffer, page_data + page_ofs, page_cnt);
802 else
803 memcpy(page_data + page_ofs, buffer, page_cnt);
804
805 kunmap(buf->pages[page_nr]);
806 buffer += page_cnt;
807 offset += page_cnt;
808 count -= page_cnt;
809 }
810}
811
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700812static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
813 struct bin_attribute *bin_attr,
814 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200816 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700817 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800818 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700819 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Laura Garciacad1e552006-05-23 23:22:38 +0200821 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800822 buf = fw_priv->buf;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100823 if (!buf || fw_state_is_done(&buf->fw_st)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 ret_count = -ENODEV;
825 goto out;
826 }
Ming Lei12446912012-08-04 12:01:20 +0800827 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200828 ret_count = 0;
829 goto out;
830 }
Ming Lei12446912012-08-04 12:01:20 +0800831 if (count > buf->size - offset)
832 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700833
834 ret_count = count;
835
Stephen Boyda098ecd2016-08-02 14:04:28 -0700836 if (buf->data)
837 firmware_rw_buf(buf, buffer, offset, count, true);
838 else
839 firmware_rw(buf, buffer, offset, count, true);
David Woodhouse6e03a202009-04-09 22:04:07 -0700840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841out:
Laura Garciacad1e552006-05-23 23:22:38 +0200842 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return ret_count;
844}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800845
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700846static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Ming Lei12446912012-08-04 12:01:20 +0800848 struct firmware_buf *buf = fw_priv->buf;
Fabian Fredericka76040d2014-07-01 21:13:30 +0200849 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
David Woodhouse6e03a202009-04-09 22:04:07 -0700851 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800852 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700853 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800854 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700855 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Chen Feng10a3fbf2015-12-16 17:03:15 +0800857 new_pages = vmalloc(new_array_size * sizeof(void *));
David Woodhouse6e03a202009-04-09 22:04:07 -0700858 if (!new_pages) {
859 fw_load_abort(fw_priv);
860 return -ENOMEM;
861 }
Ming Lei12446912012-08-04 12:01:20 +0800862 memcpy(new_pages, buf->pages,
863 buf->page_array_size * sizeof(void *));
864 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
865 (new_array_size - buf->page_array_size));
Chen Feng10a3fbf2015-12-16 17:03:15 +0800866 vfree(buf->pages);
Ming Lei12446912012-08-04 12:01:20 +0800867 buf->pages = new_pages;
868 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700870
Ming Lei12446912012-08-04 12:01:20 +0800871 while (buf->nr_pages < pages_needed) {
872 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700873 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
874
Ming Lei12446912012-08-04 12:01:20 +0800875 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700876 fw_load_abort(fw_priv);
877 return -ENOMEM;
878 }
Ming Lei12446912012-08-04 12:01:20 +0800879 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return 0;
882}
883
884/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800885 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700886 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700887 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700888 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800889 * @buffer: buffer being written
890 * @offset: buffer offset for write in total data store area
891 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800893 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 * the driver as a firmware image.
895 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700896static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
897 struct bin_attribute *bin_attr,
898 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200900 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700901 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800902 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 ssize_t retval;
904
905 if (!capable(CAP_SYS_RAWIO))
906 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700907
Laura Garciacad1e552006-05-23 23:22:38 +0200908 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800909 buf = fw_priv->buf;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100910 if (!buf || fw_state_is_done(&buf->fw_st)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 retval = -ENODEV;
912 goto out;
913 }
Ming Lei65710cb2012-08-04 12:01:16 +0800914
Stephen Boyda098ecd2016-08-02 14:04:28 -0700915 if (buf->data) {
916 if (offset + count > buf->allocated_size) {
917 retval = -ENOMEM;
918 goto out;
919 }
920 firmware_rw_buf(buf, buffer, offset, count, false);
921 retval = count;
922 } else {
923 retval = fw_realloc_buffer(fw_priv, offset + count);
924 if (retval)
925 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Stephen Boyda098ecd2016-08-02 14:04:28 -0700927 retval = count;
928 firmware_rw(buf, buffer, offset, count, false);
929 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700930
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700931 buf->size = max_t(size_t, offset + count, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932out:
Laura Garciacad1e552006-05-23 23:22:38 +0200933 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return retval;
935}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800936
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700937static struct bin_attribute firmware_attr_data = {
938 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 .size = 0,
940 .read = firmware_data_read,
941 .write = firmware_data_write,
942};
943
Takashi Iwai46239902015-02-04 15:18:07 +0100944static struct attribute *fw_dev_attrs[] = {
945 &dev_attr_loading.attr,
946 NULL
947};
948
949static struct bin_attribute *fw_dev_bin_attrs[] = {
950 &firmware_attr_data,
951 NULL
952};
953
954static const struct attribute_group fw_dev_attr_group = {
955 .attrs = fw_dev_attrs,
956 .bin_attrs = fw_dev_bin_attrs,
957};
958
959static const struct attribute_group *fw_dev_attr_groups[] = {
960 &fw_dev_attr_group,
961 NULL
962};
963
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700964static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200965fw_create_instance(struct firmware *firmware, const char *fw_name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100966 struct device *device, unsigned int opt_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700968 struct firmware_priv *fw_priv;
969 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Ming Lei12446912012-08-04 12:01:20 +0800971 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700972 if (!fw_priv) {
Ming Lei12446912012-08-04 12:01:20 +0800973 fw_priv = ERR_PTR(-ENOMEM);
974 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100977 fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT);
Ming Lei1f2b7952012-08-04 12:01:21 +0800978 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700979 f_dev = &fw_priv->dev;
980
981 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800982 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700983 f_dev->parent = device;
984 f_dev->class = &firmware_class;
Takashi Iwai46239902015-02-04 15:18:07 +0100985 f_dev->groups = fw_dev_attr_groups;
Ming Lei12446912012-08-04 12:01:20 +0800986exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700987 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100989
990/* load a firmware via user helper */
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100991static int _request_firmware_load(struct firmware_priv *fw_priv,
992 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100993{
994 int retval = 0;
995 struct device *f_dev = &fw_priv->dev;
996 struct firmware_buf *buf = fw_priv->buf;
997
998 /* fall back on userspace loading */
Stephen Boyda098ecd2016-08-02 14:04:28 -0700999 if (!buf->data)
1000 buf->is_paged_buf = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001001
1002 dev_set_uevent_suppress(f_dev, true);
1003
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001004 retval = device_add(f_dev);
1005 if (retval) {
1006 dev_err(f_dev, "%s: device_register failed\n", __func__);
1007 goto err_put_dev;
1008 }
1009
Maxime Bizon1eeeef12013-08-29 20:28:13 +02001010 mutex_lock(&fw_lock);
1011 list_add(&buf->pending_list, &pending_fw_head);
1012 mutex_unlock(&fw_lock);
1013
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001014 if (opt_flags & FW_OPT_UEVENT) {
Ming Leiaf5bc112013-05-27 18:30:04 +08001015 buf->need_uevent = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001016 dev_set_uevent_suppress(f_dev, false);
1017 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001018 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
Ming Lei68ff2a02015-01-13 00:02:01 +08001019 } else {
1020 timeout = MAX_JIFFY_OFFSET;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001021 }
1022
Daniel Wagnerf52cc3792016-11-17 11:00:48 +01001023 timeout = fw_state_wait_timeout(&buf->fw_st, timeout);
Yves-Alexis Perez2e700f82016-11-11 11:28:40 -08001024 if (timeout == -ERESTARTSYS || !timeout) {
1025 retval = timeout;
Ming Lei0cb64242015-01-13 00:01:35 +08001026 mutex_lock(&fw_lock);
1027 fw_load_abort(fw_priv);
1028 mutex_unlock(&fw_lock);
Yves-Alexis Perez2e700f82016-11-11 11:28:40 -08001029 } else if (timeout > 0) {
Zahari Doychevef518cc2015-03-10 10:45:40 +01001030 retval = 0;
Ming Lei0cb64242015-01-13 00:01:35 +08001031 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001032
Daniel Wagnerf52cc3792016-11-17 11:00:48 +01001033 if (fw_state_is_aborted(&buf->fw_st))
Shuah Khan0542ad82014-07-22 18:10:38 -06001034 retval = -EAGAIN;
Stephen Boyda098ecd2016-08-02 14:04:28 -07001035 else if (buf->is_paged_buf && !buf->data)
zhang jun2b1278c2014-02-13 15:18:47 +08001036 retval = -ENOMEM;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001037
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001038 device_del(f_dev);
1039err_put_dev:
1040 put_device(f_dev);
1041 return retval;
1042}
1043
1044static int fw_load_from_user_helper(struct firmware *firmware,
1045 const char *name, struct device *device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001046 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001047{
1048 struct firmware_priv *fw_priv;
1049
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001050 fw_priv = fw_create_instance(firmware, name, device, opt_flags);
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001051 if (IS_ERR(fw_priv))
1052 return PTR_ERR(fw_priv);
1053
1054 fw_priv->buf = firmware->priv;
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001055 return _request_firmware_load(fw_priv, opt_flags, timeout);
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001056}
Ming Leiddf1f062013-06-04 10:01:13 +08001057
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001058#ifdef CONFIG_PM_SLEEP
Ming Leiddf1f062013-06-04 10:01:13 +08001059/* kill pending requests without uevent to avoid blocking suspend */
1060static void kill_requests_without_uevent(void)
1061{
1062 struct firmware_buf *buf;
1063 struct firmware_buf *next;
1064
1065 mutex_lock(&fw_lock);
1066 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
1067 if (!buf->need_uevent)
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -07001068 __fw_load_abort(buf);
Ming Leiddf1f062013-06-04 10:01:13 +08001069 }
1070 mutex_unlock(&fw_lock);
1071}
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001072#endif
Ming Leiddf1f062013-06-04 10:01:13 +08001073
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001074#else /* CONFIG_FW_LOADER_USER_HELPER */
1075static inline int
1076fw_load_from_user_helper(struct firmware *firmware, const char *name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001077 struct device *device, unsigned int opt_flags,
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001078 long timeout)
1079{
1080 return -ENOENT;
1081}
Takashi Iwai807be032013-01-31 11:13:57 +01001082
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001083#ifdef CONFIG_PM_SLEEP
Ming Leiddf1f062013-06-04 10:01:13 +08001084static inline void kill_requests_without_uevent(void) { }
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001085#endif
Ming Leiddf1f062013-06-04 10:01:13 +08001086
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001087#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Ming Leif531f05a2012-08-04 12:01:25 +08001089
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001090/* wait until the shared firmware_buf becomes ready (or error) */
1091static int sync_cached_firmware_buf(struct firmware_buf *buf)
Ming Lei1f2b7952012-08-04 12:01:21 +08001092{
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001093 int ret = 0;
1094
1095 mutex_lock(&fw_lock);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +01001096 while (!fw_state_is_done(&buf->fw_st)) {
1097 if (fw_state_is_aborted(&buf->fw_st)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001098 ret = -ENOENT;
1099 break;
1100 }
1101 mutex_unlock(&fw_lock);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +01001102 ret = fw_state_wait(&buf->fw_st);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001103 mutex_lock(&fw_lock);
1104 }
1105 mutex_unlock(&fw_lock);
1106 return ret;
Ming Lei1f2b7952012-08-04 12:01:21 +08001107}
1108
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001109/* prepare firmware and firmware_buf structs;
1110 * return 0 if a firmware is already assigned, 1 if need to load one,
1111 * or a negative error code
1112 */
1113static int
1114_request_firmware_prepare(struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001115 struct device *device, void *dbuf, size_t size)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001116{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +08001118 struct firmware_buf *buf;
1119 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Jiri Slaby4aed0642005-09-13 01:25:01 -07001121 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -07001123 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1124 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001125 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Stephen Boyda098ecd2016-08-02 14:04:28 -07001128 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
Luis R. Rodriguezed046302015-04-29 16:30:43 -07001129 dev_dbg(device, "using built-in %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001130 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +01001131 }
1132
Stephen Boyda098ecd2016-08-02 14:04:28 -07001133 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +08001134
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001135 /*
1136 * bind with 'buf' now to avoid warning in failure path
1137 * of requesting firmware.
1138 */
1139 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +08001140
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001141 if (ret > 0) {
1142 ret = sync_cached_firmware_buf(buf);
1143 if (!ret) {
1144 fw_set_page_data(buf, firmware);
1145 return 0; /* assigned */
1146 }
Stephen Boyddddb5542012-03-28 23:30:43 +02001147 }
Ming Lei1f2b7952012-08-04 12:01:21 +08001148
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001149 if (ret < 0)
1150 return ret;
1151 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001152}
1153
Ming Leie771d1a2013-05-27 18:30:03 +08001154static int assign_firmware_buf(struct firmware *fw, struct device *device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001155 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001156{
1157 struct firmware_buf *buf = fw->priv;
1158
1159 mutex_lock(&fw_lock);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +01001160 if (!buf->size || fw_state_is_aborted(&buf->fw_st)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001161 mutex_unlock(&fw_lock);
1162 return -ENOENT;
1163 }
1164
1165 /*
1166 * add firmware name into devres list so that we can auto cache
1167 * and uncache firmware for device.
1168 *
1169 * device may has been deleted already, but the problem
1170 * should be fixed in devres or driver core.
1171 */
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001172 /* don't cache firmware handled without uevent */
Vikram Mulukutla0e742e92016-08-02 14:04:25 -07001173 if (device && (opt_flags & FW_OPT_UEVENT) &&
1174 !(opt_flags & FW_OPT_NOCACHE))
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001175 fw_add_devm_name(device, buf->fw_id);
1176
1177 /*
1178 * After caching firmware image is started, let it piggyback
1179 * on request firmware.
1180 */
Vikram Mulukutla0e742e92016-08-02 14:04:25 -07001181 if (!(opt_flags & FW_OPT_NOCACHE) &&
1182 buf->fwc->state == FW_LOADER_START_CACHE) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001183 if (fw_cache_piggyback_on_request(buf->fw_id))
1184 kref_get(&buf->ref);
1185 }
1186
1187 /* pass the pages buffer to driver at the last minute */
1188 fw_set_page_data(buf, fw);
1189 mutex_unlock(&fw_lock);
1190 return 0;
1191}
1192
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001193/* called from request_firmware() and request_firmware_work_func() */
1194static int
1195_request_firmware(const struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001196 struct device *device, void *buf, size_t size,
1197 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001198{
Brian Norris715780a2015-12-09 14:50:28 -08001199 struct firmware *fw = NULL;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001200 long timeout;
1201 int ret;
1202
1203 if (!firmware_p)
1204 return -EINVAL;
1205
Brian Norris715780a2015-12-09 14:50:28 -08001206 if (!name || name[0] == '\0') {
1207 ret = -EINVAL;
1208 goto out;
1209 }
Kees Cook471b0952014-09-18 11:25:37 -07001210
Stephen Boyda098ecd2016-08-02 14:04:28 -07001211 ret = _request_firmware_prepare(&fw, name, device, buf, size);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001212 if (ret <= 0) /* error or already assigned */
1213 goto out;
1214
1215 ret = 0;
1216 timeout = firmware_loading_timeout();
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001217 if (opt_flags & FW_OPT_NOWAIT) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001218 timeout = usermodehelper_read_lock_wait(timeout);
1219 if (!timeout) {
1220 dev_dbg(device, "firmware: %s loading timed out\n",
1221 name);
1222 ret = -EBUSY;
1223 goto out;
1224 }
1225 } else {
1226 ret = usermodehelper_read_trylock();
1227 if (WARN_ON(ret)) {
1228 dev_err(device, "firmware: %s will not be loaded\n",
1229 name);
1230 goto out;
1231 }
1232 }
1233
Neil Horman3e358ac2013-09-06 15:36:08 -04001234 ret = fw_get_filesystem_firmware(device, fw->priv);
1235 if (ret) {
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001236 if (!(opt_flags & FW_OPT_NO_WARN))
Takashi Iwaibba3a872013-12-02 15:38:16 +01001237 dev_warn(device,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001238 "Direct firmware load for %s failed with error %d\n",
1239 name, ret);
1240 if (opt_flags & FW_OPT_USERHELPER) {
Takashi Iwaibba3a872013-12-02 15:38:16 +01001241 dev_warn(device, "Falling back to user helper\n");
1242 ret = fw_load_from_user_helper(fw, name, device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001243 opt_flags, timeout);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001244 }
Neil Horman3e358ac2013-09-06 15:36:08 -04001245 }
Ming Leie771d1a2013-05-27 18:30:03 +08001246
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001247 if (!ret)
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001248 ret = assign_firmware_buf(fw, device, opt_flags);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001249
1250 usermodehelper_read_unlock();
1251
1252 out:
1253 if (ret < 0) {
1254 release_firmware(fw);
1255 fw = NULL;
1256 }
1257
1258 *firmware_p = fw;
1259 return ret;
1260}
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262/**
Kay Sievers312c0042005-11-16 09:00:00 +01001263 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001264 * @firmware_p: pointer to firmware image
1265 * @name: name of firmware file
1266 * @device: device for which firmware is being loaded
1267 *
1268 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001269 * of @name for device @device.
1270 *
1271 * Should be called from user context where sleeping is allowed.
1272 *
Kay Sievers312c0042005-11-16 09:00:00 +01001273 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001274 * should be distinctive enough not to be confused with any other
1275 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001276 *
1277 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001278 *
1279 * The function can be called safely inside device's suspend and
1280 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001281 **/
1282int
1283request_firmware(const struct firmware **firmware_p, const char *name,
Andrei Opreaea310032015-03-08 12:41:15 +02001284 struct device *device)
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001285{
Ming Leid6c8aa32013-06-06 20:01:48 +08001286 int ret;
1287
1288 /* Need to pin this module until return */
1289 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001290 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001291 FW_OPT_UEVENT | FW_OPT_FALLBACK);
Ming Leid6c8aa32013-06-06 20:01:48 +08001292 module_put(THIS_MODULE);
1293 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001294}
Daniel Mackf4945132013-05-23 22:17:18 +02001295EXPORT_SYMBOL(request_firmware);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001296
Takashi Iwaibba3a872013-12-02 15:38:16 +01001297/**
Borislav Petkov3c1556b2014-12-03 22:46:37 +01001298 * request_firmware_direct: - load firmware directly without usermode helper
Takashi Iwaibba3a872013-12-02 15:38:16 +01001299 * @firmware_p: pointer to firmware image
1300 * @name: name of firmware file
1301 * @device: device for which firmware is being loaded
1302 *
1303 * This function works pretty much like request_firmware(), but this doesn't
1304 * fall back to usermode helper even if the firmware couldn't be loaded
1305 * directly from fs. Hence it's useful for loading optional firmwares, which
1306 * aren't always present, without extra long timeouts of udev.
1307 **/
1308int request_firmware_direct(const struct firmware **firmware_p,
1309 const char *name, struct device *device)
1310{
1311 int ret;
Andrei Opreaea310032015-03-08 12:41:15 +02001312
Takashi Iwaibba3a872013-12-02 15:38:16 +01001313 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001314 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001315 FW_OPT_UEVENT | FW_OPT_NO_WARN);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001316 module_put(THIS_MODULE);
1317 return ret;
1318}
1319EXPORT_SYMBOL_GPL(request_firmware_direct);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001320
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001321/**
Stephen Boyda098ecd2016-08-02 14:04:28 -07001322 * request_firmware_into_buf - load firmware into a previously allocated buffer
1323 * @firmware_p: pointer to firmware image
1324 * @name: name of firmware file
1325 * @device: device for which firmware is being loaded and DMA region allocated
1326 * @buf: address of buffer to load firmware into
1327 * @size: size of buffer
1328 *
1329 * This function works pretty much like request_firmware(), but it doesn't
1330 * allocate a buffer to hold the firmware data. Instead, the firmware
1331 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
1332 * data member is pointed at @buf.
1333 *
1334 * This function doesn't cache firmware either.
1335 */
1336int
1337request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
1338 struct device *device, void *buf, size_t size)
1339{
1340 int ret;
1341
1342 __module_get(THIS_MODULE);
1343 ret = _request_firmware(firmware_p, name, device, buf, size,
1344 FW_OPT_UEVENT | FW_OPT_FALLBACK |
1345 FW_OPT_NOCACHE);
1346 module_put(THIS_MODULE);
1347 return ret;
1348}
1349EXPORT_SYMBOL(request_firmware_into_buf);
1350
1351/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001353 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001355void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
1357 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001358 if (!fw_is_builtin_firmware(fw))
1359 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 kfree(fw);
1361 }
1362}
Daniel Mackf4945132013-05-23 22:17:18 +02001363EXPORT_SYMBOL(release_firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365/* Async support */
1366struct firmware_work {
1367 struct work_struct work;
1368 struct module *module;
1369 const char *name;
1370 struct device *device;
1371 void *context;
1372 void (*cont)(const struct firmware *fw, void *context);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001373 unsigned int opt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374};
1375
Stephen Boyda36cf842012-03-28 23:31:00 +02001376static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Stephen Boyda36cf842012-03-28 23:31:00 +02001378 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001380
Stephen Boyda36cf842012-03-28 23:31:00 +02001381 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001382
Stephen Boyda098ecd2016-08-02 14:04:28 -07001383 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001384 fw_work->opt_flags);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001385 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001386 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 module_put(fw_work->module);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001389 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
1393/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001394 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001395 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001396 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001397 * is non-zero else the firmware copy must be done manually.
1398 * @name: name of firmware file
1399 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001400 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001401 * @context: will be passed over to @cont, and
1402 * @fw may be %NULL if firmware request fails.
1403 * @cont: function will be called asynchronously when the firmware
1404 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001406 * Caller must hold the reference count of @device.
1407 *
Ming Lei6f21a622012-08-04 12:01:24 +08001408 * Asynchronous variant of request_firmware() for user contexts:
1409 * - sleep for as small periods as possible since it may
1410 * increase kernel boot time of built-in device drivers
1411 * requesting firmware in their ->probe() methods, if
1412 * @gfp is GFP_KERNEL.
1413 *
1414 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 **/
1416int
1417request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001418 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001419 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 void (*cont)(const struct firmware *fw, void *context))
1421{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001422 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Andrei Opreaea310032015-03-08 12:41:15 +02001424 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (!fw_work)
1426 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001427
1428 fw_work->module = module;
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001429 fw_work->name = kstrdup_const(name, gfp);
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001430 if (!fw_work->name) {
1431 kfree(fw_work);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001432 return -ENOMEM;
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001433 }
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001434 fw_work->device = device;
1435 fw_work->context = context;
1436 fw_work->cont = cont;
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001437 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
Takashi Iwai5a1379e2014-06-04 17:48:15 +02001438 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (!try_module_get(module)) {
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001441 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 kfree(fw_work);
1443 return -EFAULT;
1444 }
1445
Ming Lei0cfc1e12012-08-04 12:01:23 +08001446 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001447 INIT_WORK(&fw_work->work, request_firmware_work_func);
1448 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return 0;
1450}
Daniel Mackf4945132013-05-23 22:17:18 +02001451EXPORT_SYMBOL(request_firmware_nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Ming Lei90f890812013-06-20 12:30:16 +08001453#ifdef CONFIG_PM_SLEEP
1454static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1455
Ming Lei2887b392012-08-04 12:01:22 +08001456/**
1457 * cache_firmware - cache one firmware image in kernel memory space
1458 * @fw_name: the firmware image name
1459 *
1460 * Cache firmware in kernel memory so that drivers can use it when
1461 * system isn't ready for them to request firmware image from userspace.
1462 * Once it returns successfully, driver can use request_firmware or its
1463 * nowait version to get the cached firmware without any interacting
1464 * with userspace
1465 *
1466 * Return 0 if the firmware image has been cached successfully
1467 * Return !0 otherwise
1468 *
1469 */
Ming Lei93232e42013-06-06 20:01:47 +08001470static int cache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001471{
1472 int ret;
1473 const struct firmware *fw;
1474
1475 pr_debug("%s: %s\n", __func__, fw_name);
1476
1477 ret = request_firmware(&fw, fw_name, NULL);
1478 if (!ret)
1479 kfree(fw);
1480
1481 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1482
1483 return ret;
1484}
1485
Ming Lei6a2c1232013-06-26 09:28:17 +08001486static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1487{
1488 struct firmware_buf *tmp;
1489 struct firmware_cache *fwc = &fw_cache;
1490
1491 spin_lock(&fwc->lock);
1492 tmp = __fw_lookup_buf(fw_name);
1493 spin_unlock(&fwc->lock);
1494
1495 return tmp;
1496}
1497
Ming Lei2887b392012-08-04 12:01:22 +08001498/**
1499 * uncache_firmware - remove one cached firmware image
1500 * @fw_name: the firmware image name
1501 *
1502 * Uncache one firmware image which has been cached successfully
1503 * before.
1504 *
1505 * Return 0 if the firmware cache has been removed successfully
1506 * Return !0 otherwise
1507 *
1508 */
Ming Lei93232e42013-06-06 20:01:47 +08001509static int uncache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001510{
1511 struct firmware_buf *buf;
1512 struct firmware fw;
1513
1514 pr_debug("%s: %s\n", __func__, fw_name);
1515
Stephen Boyda098ecd2016-08-02 14:04:28 -07001516 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
Ming Lei2887b392012-08-04 12:01:22 +08001517 return 0;
1518
1519 buf = fw_lookup_buf(fw_name);
1520 if (buf) {
1521 fw_free_buf(buf);
1522 return 0;
1523 }
1524
1525 return -EINVAL;
1526}
1527
Ming Lei37276a52012-08-04 12:01:27 +08001528static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1529{
1530 struct fw_cache_entry *fce;
1531
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001532 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
Ming Lei37276a52012-08-04 12:01:27 +08001533 if (!fce)
1534 goto exit;
1535
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001536 fce->name = kstrdup_const(name, GFP_ATOMIC);
1537 if (!fce->name) {
1538 kfree(fce);
1539 fce = NULL;
1540 goto exit;
1541 }
Ming Lei37276a52012-08-04 12:01:27 +08001542exit:
1543 return fce;
1544}
1545
Ming Lei373304f2012-10-09 12:01:01 +08001546static int __fw_entry_found(const char *name)
1547{
1548 struct firmware_cache *fwc = &fw_cache;
1549 struct fw_cache_entry *fce;
1550
1551 list_for_each_entry(fce, &fwc->fw_names, list) {
1552 if (!strcmp(fce->name, name))
1553 return 1;
1554 }
1555 return 0;
1556}
1557
Ming Leiac39b3e2012-08-20 19:04:16 +08001558static int fw_cache_piggyback_on_request(const char *name)
1559{
1560 struct firmware_cache *fwc = &fw_cache;
1561 struct fw_cache_entry *fce;
1562 int ret = 0;
1563
1564 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001565 if (__fw_entry_found(name))
1566 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001567
1568 fce = alloc_fw_cache_entry(name);
1569 if (fce) {
1570 ret = 1;
1571 list_add(&fce->list, &fwc->fw_names);
1572 pr_debug("%s: fw: %s\n", __func__, name);
1573 }
1574found:
1575 spin_unlock(&fwc->name_lock);
1576 return ret;
1577}
1578
Ming Lei37276a52012-08-04 12:01:27 +08001579static void free_fw_cache_entry(struct fw_cache_entry *fce)
1580{
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001581 kfree_const(fce->name);
Ming Lei37276a52012-08-04 12:01:27 +08001582 kfree(fce);
1583}
1584
1585static void __async_dev_cache_fw_image(void *fw_entry,
1586 async_cookie_t cookie)
1587{
1588 struct fw_cache_entry *fce = fw_entry;
1589 struct firmware_cache *fwc = &fw_cache;
1590 int ret;
1591
1592 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001593 if (ret) {
1594 spin_lock(&fwc->name_lock);
1595 list_del(&fce->list);
1596 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001597
Ming Leiac39b3e2012-08-20 19:04:16 +08001598 free_fw_cache_entry(fce);
1599 }
Ming Lei37276a52012-08-04 12:01:27 +08001600}
1601
1602/* called with dev->devres_lock held */
1603static void dev_create_fw_entry(struct device *dev, void *res,
1604 void *data)
1605{
1606 struct fw_name_devm *fwn = res;
1607 const char *fw_name = fwn->name;
1608 struct list_head *head = data;
1609 struct fw_cache_entry *fce;
1610
1611 fce = alloc_fw_cache_entry(fw_name);
1612 if (fce)
1613 list_add(&fce->list, head);
1614}
1615
1616static int devm_name_match(struct device *dev, void *res,
1617 void *match_data)
1618{
1619 struct fw_name_devm *fwn = res;
1620 return (fwn->magic == (unsigned long)match_data);
1621}
1622
Ming Leiab6dd8e2012-08-17 22:07:00 +08001623static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001624{
1625 LIST_HEAD(todo);
1626 struct fw_cache_entry *fce;
1627 struct fw_cache_entry *fce_next;
1628 struct firmware_cache *fwc = &fw_cache;
1629
1630 devres_for_each_res(dev, fw_name_devm_release,
1631 devm_name_match, &fw_cache,
1632 dev_create_fw_entry, &todo);
1633
1634 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1635 list_del(&fce->list);
1636
1637 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001638 /* only one cache entry for one firmware */
1639 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001640 list_add(&fce->list, &fwc->fw_names);
1641 } else {
1642 free_fw_cache_entry(fce);
1643 fce = NULL;
1644 }
Ming Lei37276a52012-08-04 12:01:27 +08001645 spin_unlock(&fwc->name_lock);
1646
Ming Lei373304f2012-10-09 12:01:01 +08001647 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001648 async_schedule_domain(__async_dev_cache_fw_image,
1649 (void *)fce,
1650 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001651 }
1652}
1653
1654static void __device_uncache_fw_images(void)
1655{
1656 struct firmware_cache *fwc = &fw_cache;
1657 struct fw_cache_entry *fce;
1658
1659 spin_lock(&fwc->name_lock);
1660 while (!list_empty(&fwc->fw_names)) {
1661 fce = list_entry(fwc->fw_names.next,
1662 struct fw_cache_entry, list);
1663 list_del(&fce->list);
1664 spin_unlock(&fwc->name_lock);
1665
1666 uncache_firmware(fce->name);
1667 free_fw_cache_entry(fce);
1668
1669 spin_lock(&fwc->name_lock);
1670 }
1671 spin_unlock(&fwc->name_lock);
1672}
1673
1674/**
1675 * device_cache_fw_images - cache devices' firmware
1676 *
1677 * If one device called request_firmware or its nowait version
1678 * successfully before, the firmware names are recored into the
1679 * device's devres link list, so device_cache_fw_images can call
1680 * cache_firmware() to cache these firmwares for the device,
1681 * then the device driver can load its firmwares easily at
1682 * time when system is not ready to complete loading firmware.
1683 */
1684static void device_cache_fw_images(void)
1685{
1686 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001687 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001688 DEFINE_WAIT(wait);
1689
1690 pr_debug("%s\n", __func__);
1691
Ming Lei373304f2012-10-09 12:01:01 +08001692 /* cancel uncache work */
1693 cancel_delayed_work_sync(&fwc->work);
1694
Ming Leiffe53f62012-08-04 12:01:28 +08001695 /*
1696 * use small loading timeout for caching devices' firmware
1697 * because all these firmware images have been loaded
1698 * successfully at lease once, also system is ready for
1699 * completing firmware loading now. The maximum size of
1700 * firmware in current distributions is about 2M bytes,
1701 * so 10 secs should be enough.
1702 */
1703 old_timeout = loading_timeout;
1704 loading_timeout = 10;
1705
Ming Leiac39b3e2012-08-20 19:04:16 +08001706 mutex_lock(&fw_lock);
1707 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001708 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001709 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001710
1711 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001712 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001713
1714 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001715}
1716
1717/**
1718 * device_uncache_fw_images - uncache devices' firmware
1719 *
1720 * uncache all firmwares which have been cached successfully
1721 * by device_uncache_fw_images earlier
1722 */
1723static void device_uncache_fw_images(void)
1724{
1725 pr_debug("%s\n", __func__);
1726 __device_uncache_fw_images();
1727}
1728
1729static void device_uncache_fw_images_work(struct work_struct *work)
1730{
1731 device_uncache_fw_images();
1732}
1733
1734/**
1735 * device_uncache_fw_images_delay - uncache devices firmwares
1736 * @delay: number of milliseconds to delay uncache device firmwares
1737 *
1738 * uncache all devices's firmwares which has been cached successfully
1739 * by device_cache_fw_images after @delay milliseconds.
1740 */
1741static void device_uncache_fw_images_delay(unsigned long delay)
1742{
Shaibal Duttabce66182014-01-31 15:44:58 -08001743 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1744 msecs_to_jiffies(delay));
Ming Lei37276a52012-08-04 12:01:27 +08001745}
1746
Ming Lei07646d92012-08-04 12:01:29 +08001747static int fw_pm_notify(struct notifier_block *notify_block,
1748 unsigned long mode, void *unused)
1749{
1750 switch (mode) {
1751 case PM_HIBERNATION_PREPARE:
1752 case PM_SUSPEND_PREPARE:
Sebastian Capellaf8d5b9e2014-02-18 17:52:08 -08001753 case PM_RESTORE_PREPARE:
Ming Leiaf5bc112013-05-27 18:30:04 +08001754 kill_requests_without_uevent();
Ming Lei07646d92012-08-04 12:01:29 +08001755 device_cache_fw_images();
1756 break;
1757
1758 case PM_POST_SUSPEND:
1759 case PM_POST_HIBERNATION:
1760 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001761 /*
1762 * In case that system sleep failed and syscore_suspend is
1763 * not called.
1764 */
1765 mutex_lock(&fw_lock);
1766 fw_cache.state = FW_LOADER_NO_CACHE;
1767 mutex_unlock(&fw_lock);
1768
Ming Lei07646d92012-08-04 12:01:29 +08001769 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1770 break;
1771 }
1772
1773 return 0;
1774}
Ming Lei07646d92012-08-04 12:01:29 +08001775
Ming Leiac39b3e2012-08-20 19:04:16 +08001776/* stop caching firmware once syscore_suspend is reached */
1777static int fw_suspend(void)
1778{
1779 fw_cache.state = FW_LOADER_NO_CACHE;
1780 return 0;
1781}
1782
1783static struct syscore_ops fw_syscore_ops = {
1784 .suspend = fw_suspend,
1785};
Ming Leicfe016b2012-09-08 17:32:30 +08001786#else
1787static int fw_cache_piggyback_on_request(const char *name)
1788{
1789 return 0;
1790}
1791#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001792
Ming Lei37276a52012-08-04 12:01:27 +08001793static void __init fw_cache_init(void)
1794{
1795 spin_lock_init(&fw_cache.lock);
1796 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001797 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001798
Ming Leicfe016b2012-09-08 17:32:30 +08001799#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001800 spin_lock_init(&fw_cache.name_lock);
1801 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001802
Ming Lei37276a52012-08-04 12:01:27 +08001803 INIT_DELAYED_WORK(&fw_cache.work,
1804 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001805
1806 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1807 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001808
1809 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001810#endif
Ming Lei37276a52012-08-04 12:01:27 +08001811}
1812
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001813static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Ming Lei1f2b7952012-08-04 12:01:21 +08001815 fw_cache_init();
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001816#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001817 register_reboot_notifier(&fw_shutdown_nb);
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001818 return class_register(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001819#else
1820 return 0;
1821#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001823
1824static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Ming Leicfe016b2012-09-08 17:32:30 +08001826#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001827 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001828 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001829#endif
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001830#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001831 unregister_reboot_notifier(&fw_shutdown_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 class_unregister(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001833#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
Shaohua Lia30a6a22006-09-27 01:50:52 -07001836fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837module_exit(firmware_class_exit);