blob: 68726c8e830659b0d215cd4ea0daa0ad2f8efc73 [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
Luis R. Rodriguez73da4b42017-07-20 13:13:40 -070010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Randy.Dunlapc59ede72006-01-11 12:17:46 -080012#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/device.h>
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/timer.h>
17#include <linux/vmalloc.h>
18#include <linux/interrupt.h>
19#include <linux/bitops.h>
Laura Garciacad1e552006-05-23 23:22:38 +020020#include <linux/mutex.h>
Stephen Boyda36cf842012-03-28 23:31:00 +020021#include <linux/workqueue.h>
David Woodhouse6e03a202009-04-09 22:04:07 -070022#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/firmware.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Stephen Boyda36cf842012-03-28 23:31:00 +020025#include <linux/sched.h>
Linus Torvaldsabb139e2012-10-03 15:58:32 -070026#include <linux/file.h>
Ming Lei1f2b7952012-08-04 12:01:21 +080027#include <linux/list.h>
Mimi Zohare40ba6d2015-11-19 12:39:22 -050028#include <linux/fs.h>
Ming Lei37276a52012-08-04 12:01:27 +080029#include <linux/async.h>
30#include <linux/pm.h>
Ming Lei07646d92012-08-04 12:01:29 +080031#include <linux/suspend.h>
Ming Leiac39b3e2012-08-20 19:04:16 +080032#include <linux/syscore_ops.h>
Takashi Iwaife304142013-05-22 18:28:38 +020033#include <linux/reboot.h>
Kees Cook6593d922014-02-25 13:06:00 -080034#include <linux/security.h>
Daniel Wagner5b029622016-11-17 11:00:50 +010035#include <linux/swait.h>
Ming Lei37276a52012-08-04 12:01:27 +080036
Linus Torvaldsabb139e2012-10-03 15:58:32 -070037#include <generated/utsrelease.h>
38
Ming Lei37276a52012-08-04 12:01:27 +080039#include "base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Markus Rechberger87d37a42007-06-04 18:45:44 +020041MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070042MODULE_DESCRIPTION("Multi purpose firmware loading support");
43MODULE_LICENSE("GPL");
44
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080045/* Builtin firmware support */
46
47#ifdef CONFIG_FW_LOADER
48
49extern struct builtin_fw __start_builtin_fw[];
50extern struct builtin_fw __end_builtin_fw[];
51
Stephen Boyda098ecd2016-08-02 14:04:28 -070052static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
53 void *buf, size_t size)
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080054{
55 struct builtin_fw *b_fw;
56
57 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
58 if (strcmp(name, b_fw->name) == 0) {
59 fw->size = b_fw->size;
60 fw->data = b_fw->data;
Stephen Boyda098ecd2016-08-02 14:04:28 -070061
62 if (buf && fw->size <= size)
63 memcpy(buf, fw->data, fw->size);
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080064 return true;
65 }
66 }
67
68 return false;
69}
70
71static bool fw_is_builtin_firmware(const struct firmware *fw)
72{
73 struct builtin_fw *b_fw;
74
75 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
76 if (fw->data == b_fw->data)
77 return true;
78
79 return false;
80}
81
82#else /* Module case - no builtin firmware support */
83
Stephen Boyda098ecd2016-08-02 14:04:28 -070084static inline bool fw_get_builtin_firmware(struct firmware *fw,
85 const char *name, void *buf,
86 size_t size)
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080087{
88 return false;
89}
90
91static inline bool fw_is_builtin_firmware(const struct firmware *fw)
92{
93 return false;
94}
95#endif
96
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010097enum fw_status {
98 FW_STATUS_UNKNOWN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 FW_STATUS_LOADING,
100 FW_STATUS_DONE,
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100101 FW_STATUS_ABORTED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
Dave Jones2f651682007-01-25 15:56:15 -0500104static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200106static inline long firmware_loading_timeout(void)
107{
Ming Lei68ff2a02015-01-13 00:02:01 +0800108 return loading_timeout > 0 ? loading_timeout * HZ : MAX_JIFFY_OFFSET;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200109}
110
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100111/*
112 * Concurrent request_firmware() for the same firmware need to be
113 * serialized. struct fw_state is simple state machine which hold the
114 * state of the firmware loading.
115 */
116struct fw_state {
Daniel Wagner5b029622016-11-17 11:00:50 +0100117 struct swait_queue_head wq;
Daniel Wagner0430caf2016-11-17 11:00:49 +0100118 enum fw_status status;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100119};
120
121static void fw_state_init(struct fw_state *fw_st)
122{
Daniel Wagner5b029622016-11-17 11:00:50 +0100123 init_swait_queue_head(&fw_st->wq);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100124 fw_st->status = FW_STATUS_UNKNOWN;
125}
126
Daniel Wagner5b029622016-11-17 11:00:50 +0100127static inline bool __fw_state_is_done(enum fw_status status)
128{
129 return status == FW_STATUS_DONE || status == FW_STATUS_ABORTED;
130}
131
Bjorn Andersson5d47ec02016-12-06 17:01:45 -0800132static int __fw_state_wait_common(struct fw_state *fw_st, long timeout)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100133{
134 long ret;
135
Daniel Wagner5b029622016-11-17 11:00:50 +0100136 ret = swait_event_interruptible_timeout(fw_st->wq,
137 __fw_state_is_done(READ_ONCE(fw_st->status)),
138 timeout);
139 if (ret != 0 && fw_st->status == FW_STATUS_ABORTED)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100140 return -ENOENT;
Bjorn Andersson5d47ec02016-12-06 17:01:45 -0800141 if (!ret)
142 return -ETIMEDOUT;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100143
Bjorn Andersson5d47ec02016-12-06 17:01:45 -0800144 return ret < 0 ? ret : 0;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100145}
146
147static void __fw_state_set(struct fw_state *fw_st,
148 enum fw_status status)
149{
Daniel Wagner0430caf2016-11-17 11:00:49 +0100150 WRITE_ONCE(fw_st->status, status);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100151
Daniel Wagner0430caf2016-11-17 11:00:49 +0100152 if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED)
Daniel Wagner5b029622016-11-17 11:00:50 +0100153 swake_up(&fw_st->wq);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100154}
155
156#define fw_state_start(fw_st) \
157 __fw_state_set(fw_st, FW_STATUS_LOADING)
158#define fw_state_done(fw_st) \
159 __fw_state_set(fw_st, FW_STATUS_DONE)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100160#define fw_state_wait(fw_st) \
161 __fw_state_wait_common(fw_st, MAX_SCHEDULE_TIMEOUT)
162
163#ifndef CONFIG_FW_LOADER_USER_HELPER
164
165#define fw_state_is_aborted(fw_st) false
166
167#else /* CONFIG_FW_LOADER_USER_HELPER */
168
Daniel Wagnerfab82cb2016-11-17 11:00:51 +0100169static int __fw_state_check(struct fw_state *fw_st, enum fw_status status)
170{
171 return fw_st->status == status;
172}
173
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100174#define fw_state_aborted(fw_st) \
175 __fw_state_set(fw_st, FW_STATUS_ABORTED)
Daniel Wagnerfab82cb2016-11-17 11:00:51 +0100176#define fw_state_is_done(fw_st) \
177 __fw_state_check(fw_st, FW_STATUS_DONE)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100178#define fw_state_is_loading(fw_st) \
179 __fw_state_check(fw_st, FW_STATUS_LOADING)
180#define fw_state_is_aborted(fw_st) \
181 __fw_state_check(fw_st, FW_STATUS_ABORTED)
182#define fw_state_wait_timeout(fw_st, timeout) \
183 __fw_state_wait_common(fw_st, timeout)
184
185#endif /* CONFIG_FW_LOADER_USER_HELPER */
186
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100187/* firmware behavior options */
188#define FW_OPT_UEVENT (1U << 0)
189#define FW_OPT_NOWAIT (1U << 1)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100190#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200191#define FW_OPT_USERHELPER (1U << 2)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100192#else
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200193#define FW_OPT_USERHELPER 0
194#endif
195#ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK
196#define FW_OPT_FALLBACK FW_OPT_USERHELPER
197#else
198#define FW_OPT_FALLBACK 0
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100199#endif
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -0700200#define FW_OPT_NO_WARN (1U << 3)
Vikram Mulukutla0e742e92016-08-02 14:04:25 -0700201#define FW_OPT_NOCACHE (1U << 4)
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100202
Ming Lei1f2b7952012-08-04 12:01:21 +0800203struct firmware_cache {
204 /* firmware_buf instance will be added into the below list */
205 spinlock_t lock;
206 struct list_head head;
Ming Leicfe016b2012-09-08 17:32:30 +0800207 int state;
Ming Lei37276a52012-08-04 12:01:27 +0800208
Ming Leicfe016b2012-09-08 17:32:30 +0800209#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +0800210 /*
211 * Names of firmware images which have been cached successfully
212 * will be added into the below list so that device uncache
213 * helper can trace which firmware images have been cached
214 * before.
215 */
216 spinlock_t name_lock;
217 struct list_head fw_names;
218
Ming Lei37276a52012-08-04 12:01:27 +0800219 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +0800220
221 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +0800222#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800223};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Ming Lei12446912012-08-04 12:01:20 +0800225struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800226 struct kref ref;
227 struct list_head list;
Ming Lei1f2b7952012-08-04 12:01:21 +0800228 struct firmware_cache *fwc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100229 struct fw_state fw_st;
Ming Lei65710cb2012-08-04 12:01:16 +0800230 void *data;
231 size_t size;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700232 size_t allocated_size;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100233#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100234 bool is_paged_buf;
Ming Leiaf5bc112013-05-27 18:30:04 +0800235 bool need_uevent;
David Woodhouse6e03a202009-04-09 22:04:07 -0700236 struct page **pages;
237 int nr_pages;
238 int page_array_size;
Takashi Iwaife304142013-05-22 18:28:38 +0200239 struct list_head pending_list;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100240#endif
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700241 const char *fw_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242};
243
Ming Lei37276a52012-08-04 12:01:27 +0800244struct fw_cache_entry {
245 struct list_head list;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700246 const char *name;
Ming Lei37276a52012-08-04 12:01:27 +0800247};
248
Ming Leif531f05a2012-08-04 12:01:25 +0800249struct fw_name_devm {
250 unsigned long magic;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700251 const char *name;
Ming Leif531f05a2012-08-04 12:01:25 +0800252};
253
Ming Lei1f2b7952012-08-04 12:01:21 +0800254#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
255
Ming Leiac39b3e2012-08-20 19:04:16 +0800256#define FW_LOADER_NO_CACHE 0
257#define FW_LOADER_START_CACHE 1
258
259static int fw_cache_piggyback_on_request(const char *name);
260
Ming Lei1f2b7952012-08-04 12:01:21 +0800261/* fw_lock could be moved to 'struct firmware_priv' but since it is just
262 * guarding for corner cases a global lock should be OK */
263static DEFINE_MUTEX(fw_lock);
264
Luis R. Rodriguez81f95072017-05-02 01:31:05 -0700265static bool __enable_firmware = false;
266
267static void enable_firmware(void)
268{
269 mutex_lock(&fw_lock);
270 __enable_firmware = true;
271 mutex_unlock(&fw_lock);
272}
273
274static void disable_firmware(void)
275{
276 mutex_lock(&fw_lock);
277 __enable_firmware = false;
278 mutex_unlock(&fw_lock);
279}
280
281/*
282 * When disabled only the built-in firmware and the firmware cache will be
283 * used to look for firmware.
284 */
285static bool firmware_enabled(void)
286{
287 bool enabled = false;
288
289 mutex_lock(&fw_lock);
290 if (__enable_firmware)
291 enabled = true;
292 mutex_unlock(&fw_lock);
293
294 return enabled;
295}
296
Ming Lei1f2b7952012-08-04 12:01:21 +0800297static struct firmware_cache fw_cache;
298
299static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
Stephen Boyda098ecd2016-08-02 14:04:28 -0700300 struct firmware_cache *fwc,
301 void *dbuf, size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800302{
303 struct firmware_buf *buf;
304
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700305 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
Ming Lei1f2b7952012-08-04 12:01:21 +0800306 if (!buf)
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700307 return NULL;
308
309 buf->fw_id = kstrdup_const(fw_name, GFP_ATOMIC);
310 if (!buf->fw_id) {
311 kfree(buf);
312 return NULL;
313 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800314
315 kref_init(&buf->ref);
Ming Lei1f2b7952012-08-04 12:01:21 +0800316 buf->fwc = fwc;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700317 buf->data = dbuf;
318 buf->allocated_size = size;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100319 fw_state_init(&buf->fw_st);
Takashi Iwaife304142013-05-22 18:28:38 +0200320#ifdef CONFIG_FW_LOADER_USER_HELPER
321 INIT_LIST_HEAD(&buf->pending_list);
322#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800323
324 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
325
326 return buf;
327}
328
Ming Lei2887b392012-08-04 12:01:22 +0800329static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
330{
331 struct firmware_buf *tmp;
332 struct firmware_cache *fwc = &fw_cache;
333
334 list_for_each_entry(tmp, &fwc->head, list)
335 if (!strcmp(tmp->fw_id, fw_name))
336 return tmp;
337 return NULL;
338}
339
Luis R. Rodriguez30172be2017-07-20 13:13:41 -0700340/* Returns 1 for batching firmware requests with the same name */
Ming Lei1f2b7952012-08-04 12:01:21 +0800341static int fw_lookup_and_allocate_buf(const char *fw_name,
342 struct firmware_cache *fwc,
Stephen Boyda098ecd2016-08-02 14:04:28 -0700343 struct firmware_buf **buf, void *dbuf,
344 size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800345{
346 struct firmware_buf *tmp;
347
348 spin_lock(&fwc->lock);
Ming Lei2887b392012-08-04 12:01:22 +0800349 tmp = __fw_lookup_buf(fw_name);
350 if (tmp) {
351 kref_get(&tmp->ref);
352 spin_unlock(&fwc->lock);
353 *buf = tmp;
Luis R. Rodriguez30172be2017-07-20 13:13:41 -0700354 pr_debug("batched request - sharing the same struct firmware_buf and lookup for multiple requests\n");
Ming Lei2887b392012-08-04 12:01:22 +0800355 return 1;
356 }
Stephen Boyda098ecd2016-08-02 14:04:28 -0700357 tmp = __allocate_fw_buf(fw_name, fwc, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +0800358 if (tmp)
359 list_add(&tmp->list, &fwc->head);
360 spin_unlock(&fwc->lock);
361
362 *buf = tmp;
363
364 return tmp ? 0 : -ENOMEM;
365}
366
367static void __fw_free_buf(struct kref *ref)
Bart Van Assche98233b22014-01-04 14:20:36 +0100368 __releases(&fwc->lock)
Ming Lei1f2b7952012-08-04 12:01:21 +0800369{
370 struct firmware_buf *buf = to_fwbuf(ref);
371 struct firmware_cache *fwc = buf->fwc;
Ming Lei1f2b7952012-08-04 12:01:21 +0800372
373 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
374 __func__, buf->fw_id, buf, buf->data,
375 (unsigned int)buf->size);
376
Ming Lei1f2b7952012-08-04 12:01:21 +0800377 list_del(&buf->list);
378 spin_unlock(&fwc->lock);
379
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100380#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100381 if (buf->is_paged_buf) {
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100382 int i;
Ming Lei746058f2012-10-09 12:01:03 +0800383 vunmap(buf->data);
384 for (i = 0; i < buf->nr_pages; i++)
385 __free_page(buf->pages[i]);
Chen Feng10a3fbf2015-12-16 17:03:15 +0800386 vfree(buf->pages);
Ming Lei746058f2012-10-09 12:01:03 +0800387 } else
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100388#endif
Stephen Boyda098ecd2016-08-02 14:04:28 -0700389 if (!buf->allocated_size)
Ming Lei746058f2012-10-09 12:01:03 +0800390 vfree(buf->data);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700391 kfree_const(buf->fw_id);
Ming Lei1f2b7952012-08-04 12:01:21 +0800392 kfree(buf);
393}
394
395static void fw_free_buf(struct firmware_buf *buf)
396{
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800397 struct firmware_cache *fwc = buf->fwc;
398 spin_lock(&fwc->lock);
399 if (!kref_put(&buf->ref, __fw_free_buf))
400 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800401}
402
Ming Lei746058f2012-10-09 12:01:03 +0800403/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800404static char fw_path_para[256];
405static const char * const fw_path[] = {
406 fw_path_para,
Ming Lei746058f2012-10-09 12:01:03 +0800407 "/lib/firmware/updates/" UTS_RELEASE,
408 "/lib/firmware/updates",
409 "/lib/firmware/" UTS_RELEASE,
410 "/lib/firmware"
411};
412
Ming Lei27602842012-11-03 17:47:58 +0800413/*
414 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
415 * from kernel command line because firmware_class is generally built in
416 * kernel instead of module.
417 */
418module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
419MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
420
Stephen Boyda098ecd2016-08-02 14:04:28 -0700421static int
422fw_get_filesystem_firmware(struct device *device, struct firmware_buf *buf)
Ming Lei746058f2012-10-09 12:01:03 +0800423{
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500424 loff_t size;
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700425 int i, len;
Neil Horman3e358ac2013-09-06 15:36:08 -0400426 int rc = -ENOENT;
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700427 char *path;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700428 enum kernel_read_file_id id = READING_FIRMWARE;
429 size_t msize = INT_MAX;
430
431 /* Already populated data member means we're loading into a buffer */
432 if (buf->data) {
433 id = READING_FIRMWARE_PREALLOC_BUFFER;
434 msize = buf->allocated_size;
435 }
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700436
437 path = __getname();
438 if (!path)
439 return -ENOMEM;
Ming Lei746058f2012-10-09 12:01:03 +0800440
441 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
Ming Lei27602842012-11-03 17:47:58 +0800442 /* skip the unset customized path */
443 if (!fw_path[i][0])
444 continue;
445
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700446 len = snprintf(path, PATH_MAX, "%s/%s",
447 fw_path[i], buf->fw_id);
448 if (len >= PATH_MAX) {
449 rc = -ENAMETOOLONG;
450 break;
451 }
Ming Lei746058f2012-10-09 12:01:03 +0800452
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500453 buf->size = 0;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700454 rc = kernel_read_file_from_path(path, &buf->data, &size, msize,
455 id);
Kees Cook4b2530d2016-02-04 13:15:02 -0800456 if (rc) {
Luis R. Rodriguez8e516aa2016-02-28 21:57:55 +0100457 if (rc == -ENOENT)
458 dev_dbg(device, "loading %s failed with error %d\n",
459 path, rc);
460 else
461 dev_warn(device, "loading %s failed with error %d\n",
462 path, rc);
Kees Cook4b2530d2016-02-04 13:15:02 -0800463 continue;
464 }
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500465 dev_dbg(device, "direct-loading %s\n", buf->fw_id);
466 buf->size = size;
Daniel Wagner5b029622016-11-17 11:00:50 +0100467 fw_state_done(&buf->fw_st);
Kees Cook4b2530d2016-02-04 13:15:02 -0800468 break;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100469 }
Kees Cook4b2530d2016-02-04 13:15:02 -0800470 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100471
Neil Horman3e358ac2013-09-06 15:36:08 -0400472 return rc;
Ming Lei746058f2012-10-09 12:01:03 +0800473}
474
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100475/* firmware holds the ownership of pages */
476static void firmware_free_data(const struct firmware *fw)
477{
478 /* Loaded directly? */
479 if (!fw->priv) {
480 vfree(fw->data);
481 return;
482 }
483 fw_free_buf(fw->priv);
484}
485
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100486/* store the pages buffer info firmware from buf */
487static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
488{
489 fw->priv = buf;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100490#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100491 fw->pages = buf->pages;
492#endif
493 fw->size = buf->size;
494 fw->data = buf->data;
495
496 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
497 __func__, buf->fw_id, buf, buf->data,
498 (unsigned int)buf->size);
499}
500
501#ifdef CONFIG_PM_SLEEP
502static void fw_name_devm_release(struct device *dev, void *res)
503{
504 struct fw_name_devm *fwn = res;
505
506 if (fwn->magic == (unsigned long)&fw_cache)
507 pr_debug("%s: fw_name-%s devm-%p released\n",
508 __func__, fwn->name, res);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700509 kfree_const(fwn->name);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100510}
511
512static int fw_devm_match(struct device *dev, void *res,
513 void *match_data)
514{
515 struct fw_name_devm *fwn = res;
516
517 return (fwn->magic == (unsigned long)&fw_cache) &&
518 !strcmp(fwn->name, match_data);
519}
520
521static struct fw_name_devm *fw_find_devm_name(struct device *dev,
522 const char *name)
523{
524 struct fw_name_devm *fwn;
525
526 fwn = devres_find(dev, fw_name_devm_release,
527 fw_devm_match, (void *)name);
528 return fwn;
529}
530
531/* add firmware name into devres list */
532static int fw_add_devm_name(struct device *dev, const char *name)
533{
534 struct fw_name_devm *fwn;
535
536 fwn = fw_find_devm_name(dev, name);
537 if (fwn)
538 return 1;
539
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700540 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
541 GFP_KERNEL);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100542 if (!fwn)
543 return -ENOMEM;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700544 fwn->name = kstrdup_const(name, GFP_KERNEL);
545 if (!fwn->name) {
Vladimir Zapolskiya885de62015-07-29 23:26:28 +0300546 devres_free(fwn);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700547 return -ENOMEM;
548 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100549
550 fwn->magic = (unsigned long)&fw_cache;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100551 devres_add(dev, fwn);
552
553 return 0;
554}
555#else
556static int fw_add_devm_name(struct device *dev, const char *name)
557{
558 return 0;
559}
560#endif
561
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700562static int assign_firmware_buf(struct firmware *fw, struct device *device,
563 unsigned int opt_flags)
564{
565 struct firmware_buf *buf = fw->priv;
566
567 mutex_lock(&fw_lock);
568 if (!buf->size || fw_state_is_aborted(&buf->fw_st)) {
569 mutex_unlock(&fw_lock);
570 return -ENOENT;
571 }
572
573 /*
574 * add firmware name into devres list so that we can auto cache
575 * and uncache firmware for device.
576 *
577 * device may has been deleted already, but the problem
578 * should be fixed in devres or driver core.
579 */
580 /* don't cache firmware handled without uevent */
581 if (device && (opt_flags & FW_OPT_UEVENT) &&
582 !(opt_flags & FW_OPT_NOCACHE))
583 fw_add_devm_name(device, buf->fw_id);
584
585 /*
586 * After caching firmware image is started, let it piggyback
587 * on request firmware.
588 */
589 if (!(opt_flags & FW_OPT_NOCACHE) &&
590 buf->fwc->state == FW_LOADER_START_CACHE) {
591 if (fw_cache_piggyback_on_request(buf->fw_id))
592 kref_get(&buf->ref);
593 }
594
595 /* pass the pages buffer to driver at the last minute */
596 fw_set_page_data(buf, fw);
597 mutex_unlock(&fw_lock);
598 return 0;
599}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100600
601/*
602 * user-mode helper code
603 */
604#ifdef CONFIG_FW_LOADER_USER_HELPER
605struct firmware_priv {
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100606 bool nowait;
607 struct device dev;
608 struct firmware_buf *buf;
609 struct firmware *fw;
610};
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100611
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700612static struct firmware_priv *to_firmware_priv(struct device *dev)
613{
614 return container_of(dev, struct firmware_priv, dev);
615}
616
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700617static void __fw_load_abort(struct firmware_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Ming Lei87597932013-06-15 16:36:38 +0800619 /*
620 * There is a small window in which user can write to 'loading'
621 * between loading done and disappearance of 'loading'
622 */
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100623 if (fw_state_is_done(&buf->fw_st))
Ming Lei87597932013-06-15 16:36:38 +0800624 return;
625
Takashi Iwaife304142013-05-22 18:28:38 +0200626 list_del_init(&buf->pending_list);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100627 fw_state_aborted(&buf->fw_st);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700630static void fw_load_abort(struct firmware_priv *fw_priv)
631{
632 struct firmware_buf *buf = fw_priv->buf;
633
634 __fw_load_abort(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Takashi Iwaife304142013-05-22 18:28:38 +0200637static LIST_HEAD(pending_fw_head);
638
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -0700639static void kill_pending_fw_fallback_reqs(bool only_kill_custom)
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700640{
641 struct firmware_buf *buf;
642 struct firmware_buf *next;
643
644 mutex_lock(&fw_lock);
645 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -0700646 if (!buf->need_uevent || !only_kill_custom)
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700647 __fw_load_abort(buf);
648 }
649 mutex_unlock(&fw_lock);
650}
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700651
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700652static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
653 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 return sprintf(buf, "%d\n", loading_timeout);
656}
657
658/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800659 * firmware_timeout_store - set number of seconds to wait for firmware
660 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800661 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800662 * @buf: buffer to scan for timeout value
663 * @count: number of bytes in @buf
664 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800666 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 * firmware will be provided.
668 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800669 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 **/
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700671static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
672 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700675 if (loading_timeout < 0)
676 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return count;
679}
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100680static CLASS_ATTR_RW(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100682static struct attribute *firmware_class_attrs[] = {
683 &class_attr_timeout.attr,
684 NULL,
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800685};
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100686ATTRIBUTE_GROUPS(firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800688static void fw_dev_release(struct device *dev)
689{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700690 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800691
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800692 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800693}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Linus Torvalds6f957722015-07-09 11:20:01 -0700695static int do_firmware_uevent(struct firmware_priv *fw_priv, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Ming Lei12446912012-08-04 12:01:20 +0800697 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200699 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700700 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200701 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
702 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 return 0;
705}
706
Linus Torvalds6f957722015-07-09 11:20:01 -0700707static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
708{
709 struct firmware_priv *fw_priv = to_firmware_priv(dev);
710 int err = 0;
711
712 mutex_lock(&fw_lock);
713 if (fw_priv->buf)
714 err = do_firmware_uevent(fw_priv, env);
715 mutex_unlock(&fw_lock);
716 return err;
717}
718
Adrian Bunk1b81d662006-05-20 15:00:16 -0700719static struct class firmware_class = {
720 .name = "firmware",
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100721 .class_groups = firmware_class_groups,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700722 .dev_uevent = firmware_uevent,
723 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700724};
725
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700726static ssize_t firmware_loading_show(struct device *dev,
727 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700729 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800730 int loading = 0;
731
732 mutex_lock(&fw_lock);
733 if (fw_priv->buf)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100734 loading = fw_state_is_loading(&fw_priv->buf->fw_st);
Ming Lei87597932013-06-15 16:36:38 +0800735 mutex_unlock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return sprintf(buf, "%d\n", loading);
738}
739
David Woodhouse6e03a202009-04-09 22:04:07 -0700740/* Some architectures don't have PAGE_KERNEL_RO */
741#ifndef PAGE_KERNEL_RO
742#define PAGE_KERNEL_RO PAGE_KERNEL
743#endif
Ming Lei253c9242012-10-09 12:01:02 +0800744
745/* one pages buffer should be mapped/unmapped only once */
746static int fw_map_pages_buf(struct firmware_buf *buf)
747{
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100748 if (!buf->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800749 return 0;
750
Markus Elfringdaa3d672014-11-19 11:38:38 +0100751 vunmap(buf->data);
Ming Lei253c9242012-10-09 12:01:02 +0800752 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
753 if (!buf->data)
754 return -ENOMEM;
755 return 0;
756}
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800759 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700760 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800761 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800762 * @buf: buffer to scan for loading control value
763 * @count: number of bytes in @buf
764 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 * The relevant values are:
766 *
767 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800768 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 * -1: Conclude the load with an error and discard any written data.
770 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700771static ssize_t firmware_loading_store(struct device *dev,
772 struct device_attribute *attr,
773 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700775 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800776 struct firmware_buf *fw_buf;
Kees Cook6593d922014-02-25 13:06:00 -0800777 ssize_t written = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700779 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Neil Hormaneea915b2012-01-02 15:31:23 -0500781 mutex_lock(&fw_lock);
Ming Lei87597932013-06-15 16:36:38 +0800782 fw_buf = fw_priv->buf;
Luis R. Rodriguez191e8852017-01-25 10:31:52 -0800783 if (fw_state_is_aborted(&fw_buf->fw_st))
Neil Hormaneea915b2012-01-02 15:31:23 -0500784 goto out;
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 switch (loading) {
787 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800788 /* discarding any previous partial load */
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100789 if (!fw_state_is_done(&fw_buf->fw_st)) {
Ming Lei12446912012-08-04 12:01:20 +0800790 for (i = 0; i < fw_buf->nr_pages; i++)
791 __free_page(fw_buf->pages[i]);
Chen Feng10a3fbf2015-12-16 17:03:15 +0800792 vfree(fw_buf->pages);
Ming Lei12446912012-08-04 12:01:20 +0800793 fw_buf->pages = NULL;
794 fw_buf->page_array_size = 0;
795 fw_buf->nr_pages = 0;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100796 fw_state_start(&fw_buf->fw_st);
Ming Lei28eefa72012-08-04 12:01:17 +0800797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 break;
799 case 0:
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100800 if (fw_state_is_loading(&fw_buf->fw_st)) {
Kees Cook6593d922014-02-25 13:06:00 -0800801 int rc;
802
Ming Lei253c9242012-10-09 12:01:02 +0800803 /*
804 * Several loading requests may be pending on
805 * one same firmware buf, so let all requests
806 * see the mapped 'buf->data' once the loading
807 * is completed.
808 * */
Kees Cook6593d922014-02-25 13:06:00 -0800809 rc = fw_map_pages_buf(fw_buf);
810 if (rc)
zhang jun2b1278c2014-02-13 15:18:47 +0800811 dev_err(dev, "%s: map pages failed\n",
812 __func__);
Kees Cook6593d922014-02-25 13:06:00 -0800813 else
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500814 rc = security_kernel_post_read_file(NULL,
815 fw_buf->data, fw_buf->size,
816 READING_FIRMWARE);
Kees Cook6593d922014-02-25 13:06:00 -0800817
818 /*
819 * Same logic as fw_load_abort, only the DONE bit
820 * is ignored and we set ABORT only on failure.
821 */
Takashi Iwaife304142013-05-22 18:28:38 +0200822 list_del_init(&fw_buf->pending_list);
Kees Cook6593d922014-02-25 13:06:00 -0800823 if (rc) {
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100824 fw_state_aborted(&fw_buf->fw_st);
Kees Cook6593d922014-02-25 13:06:00 -0800825 written = rc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100826 } else {
827 fw_state_done(&fw_buf->fw_st);
Kees Cook6593d922014-02-25 13:06:00 -0800828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 break;
830 }
831 /* fallthrough */
832 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700833 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /* fallthrough */
835 case -1:
836 fw_load_abort(fw_priv);
837 break;
838 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500839out:
840 mutex_unlock(&fw_lock);
Kees Cook6593d922014-02-25 13:06:00 -0800841 return written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700844static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Stephen Boyda098ecd2016-08-02 14:04:28 -0700846static void firmware_rw_buf(struct firmware_buf *buf, char *buffer,
847 loff_t offset, size_t count, bool read)
848{
849 if (read)
850 memcpy(buffer, buf->data + offset, count);
851 else
852 memcpy(buf->data + offset, buffer, count);
853}
854
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700855static void firmware_rw(struct firmware_buf *buf, char *buffer,
856 loff_t offset, size_t count, bool read)
857{
858 while (count) {
859 void *page_data;
860 int page_nr = offset >> PAGE_SHIFT;
861 int page_ofs = offset & (PAGE_SIZE-1);
862 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
863
864 page_data = kmap(buf->pages[page_nr]);
865
866 if (read)
867 memcpy(buffer, page_data + page_ofs, page_cnt);
868 else
869 memcpy(page_data + page_ofs, buffer, page_cnt);
870
871 kunmap(buf->pages[page_nr]);
872 buffer += page_cnt;
873 offset += page_cnt;
874 count -= page_cnt;
875 }
876}
877
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700878static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
879 struct bin_attribute *bin_attr,
880 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200882 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700883 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800884 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700885 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Laura Garciacad1e552006-05-23 23:22:38 +0200887 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800888 buf = fw_priv->buf;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100889 if (!buf || fw_state_is_done(&buf->fw_st)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 ret_count = -ENODEV;
891 goto out;
892 }
Ming Lei12446912012-08-04 12:01:20 +0800893 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200894 ret_count = 0;
895 goto out;
896 }
Ming Lei12446912012-08-04 12:01:20 +0800897 if (count > buf->size - offset)
898 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700899
900 ret_count = count;
901
Stephen Boyda098ecd2016-08-02 14:04:28 -0700902 if (buf->data)
903 firmware_rw_buf(buf, buffer, offset, count, true);
904 else
905 firmware_rw(buf, buffer, offset, count, true);
David Woodhouse6e03a202009-04-09 22:04:07 -0700906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907out:
Laura Garciacad1e552006-05-23 23:22:38 +0200908 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return ret_count;
910}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800911
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700912static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Ming Lei12446912012-08-04 12:01:20 +0800914 struct firmware_buf *buf = fw_priv->buf;
Fabian Fredericka76040d2014-07-01 21:13:30 +0200915 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
David Woodhouse6e03a202009-04-09 22:04:07 -0700917 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800918 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700919 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800920 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700921 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Chen Feng10a3fbf2015-12-16 17:03:15 +0800923 new_pages = vmalloc(new_array_size * sizeof(void *));
David Woodhouse6e03a202009-04-09 22:04:07 -0700924 if (!new_pages) {
925 fw_load_abort(fw_priv);
926 return -ENOMEM;
927 }
Ming Lei12446912012-08-04 12:01:20 +0800928 memcpy(new_pages, buf->pages,
929 buf->page_array_size * sizeof(void *));
930 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
931 (new_array_size - buf->page_array_size));
Chen Feng10a3fbf2015-12-16 17:03:15 +0800932 vfree(buf->pages);
Ming Lei12446912012-08-04 12:01:20 +0800933 buf->pages = new_pages;
934 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700936
Ming Lei12446912012-08-04 12:01:20 +0800937 while (buf->nr_pages < pages_needed) {
938 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700939 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
940
Ming Lei12446912012-08-04 12:01:20 +0800941 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700942 fw_load_abort(fw_priv);
943 return -ENOMEM;
944 }
Ming Lei12446912012-08-04 12:01:20 +0800945 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return 0;
948}
949
950/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800951 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700952 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700953 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700954 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800955 * @buffer: buffer being written
956 * @offset: buffer offset for write in total data store area
957 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800959 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 * the driver as a firmware image.
961 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700962static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
963 struct bin_attribute *bin_attr,
964 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200966 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700967 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800968 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 ssize_t retval;
970
971 if (!capable(CAP_SYS_RAWIO))
972 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700973
Laura Garciacad1e552006-05-23 23:22:38 +0200974 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800975 buf = fw_priv->buf;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100976 if (!buf || fw_state_is_done(&buf->fw_st)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 retval = -ENODEV;
978 goto out;
979 }
Ming Lei65710cb2012-08-04 12:01:16 +0800980
Stephen Boyda098ecd2016-08-02 14:04:28 -0700981 if (buf->data) {
982 if (offset + count > buf->allocated_size) {
983 retval = -ENOMEM;
984 goto out;
985 }
986 firmware_rw_buf(buf, buffer, offset, count, false);
987 retval = count;
988 } else {
989 retval = fw_realloc_buffer(fw_priv, offset + count);
990 if (retval)
991 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Stephen Boyda098ecd2016-08-02 14:04:28 -0700993 retval = count;
994 firmware_rw(buf, buffer, offset, count, false);
995 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700996
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700997 buf->size = max_t(size_t, offset + count, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998out:
Laura Garciacad1e552006-05-23 23:22:38 +0200999 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return retval;
1001}
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001002
Dmitry Torokhov0983ca22010-06-04 00:54:37 -07001003static struct bin_attribute firmware_attr_data = {
1004 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 .size = 0,
1006 .read = firmware_data_read,
1007 .write = firmware_data_write,
1008};
1009
Takashi Iwai46239902015-02-04 15:18:07 +01001010static struct attribute *fw_dev_attrs[] = {
1011 &dev_attr_loading.attr,
1012 NULL
1013};
1014
1015static struct bin_attribute *fw_dev_bin_attrs[] = {
1016 &firmware_attr_data,
1017 NULL
1018};
1019
1020static const struct attribute_group fw_dev_attr_group = {
1021 .attrs = fw_dev_attrs,
1022 .bin_attrs = fw_dev_bin_attrs,
1023};
1024
1025static const struct attribute_group *fw_dev_attr_groups[] = {
1026 &fw_dev_attr_group,
1027 NULL
1028};
1029
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001030static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +02001031fw_create_instance(struct firmware *firmware, const char *fw_name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001032 struct device *device, unsigned int opt_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001034 struct firmware_priv *fw_priv;
1035 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Ming Lei12446912012-08-04 12:01:20 +08001037 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001038 if (!fw_priv) {
Ming Lei12446912012-08-04 12:01:20 +08001039 fw_priv = ERR_PTR(-ENOMEM);
1040 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001043 fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT);
Ming Lei1f2b7952012-08-04 12:01:21 +08001044 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001045 f_dev = &fw_priv->dev;
1046
1047 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +08001048 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -07001049 f_dev->parent = device;
1050 f_dev->class = &firmware_class;
Takashi Iwai46239902015-02-04 15:18:07 +01001051 f_dev->groups = fw_dev_attr_groups;
Ming Lei12446912012-08-04 12:01:20 +08001052exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001053 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001055
1056/* load a firmware via user helper */
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001057static int _request_firmware_load(struct firmware_priv *fw_priv,
1058 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001059{
1060 int retval = 0;
1061 struct device *f_dev = &fw_priv->dev;
1062 struct firmware_buf *buf = fw_priv->buf;
1063
1064 /* fall back on userspace loading */
Stephen Boyda098ecd2016-08-02 14:04:28 -07001065 if (!buf->data)
1066 buf->is_paged_buf = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001067
1068 dev_set_uevent_suppress(f_dev, true);
1069
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001070 retval = device_add(f_dev);
1071 if (retval) {
1072 dev_err(f_dev, "%s: device_register failed\n", __func__);
1073 goto err_put_dev;
1074 }
1075
Maxime Bizon1eeeef12013-08-29 20:28:13 +02001076 mutex_lock(&fw_lock);
1077 list_add(&buf->pending_list, &pending_fw_head);
1078 mutex_unlock(&fw_lock);
1079
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001080 if (opt_flags & FW_OPT_UEVENT) {
Ming Leiaf5bc112013-05-27 18:30:04 +08001081 buf->need_uevent = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001082 dev_set_uevent_suppress(f_dev, false);
1083 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001084 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
Ming Lei68ff2a02015-01-13 00:02:01 +08001085 } else {
1086 timeout = MAX_JIFFY_OFFSET;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001087 }
1088
Bjorn Andersson5d47ec02016-12-06 17:01:45 -08001089 retval = fw_state_wait_timeout(&buf->fw_st, timeout);
1090 if (retval < 0) {
Ming Lei0cb64242015-01-13 00:01:35 +08001091 mutex_lock(&fw_lock);
1092 fw_load_abort(fw_priv);
1093 mutex_unlock(&fw_lock);
1094 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001095
Luis R. Rodriguez76098b32017-07-20 13:13:39 -07001096 if (fw_state_is_aborted(&buf->fw_st)) {
1097 if (retval == -ERESTARTSYS)
1098 retval = -EINTR;
1099 else
1100 retval = -EAGAIN;
1101 } else if (buf->is_paged_buf && !buf->data)
zhang jun2b1278c2014-02-13 15:18:47 +08001102 retval = -ENOMEM;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001103
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001104 device_del(f_dev);
1105err_put_dev:
1106 put_device(f_dev);
1107 return retval;
1108}
1109
1110static int fw_load_from_user_helper(struct firmware *firmware,
1111 const char *name, struct device *device,
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001112 unsigned int opt_flags)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001113{
1114 struct firmware_priv *fw_priv;
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001115 long timeout;
1116 int ret;
1117
1118 timeout = firmware_loading_timeout();
1119 if (opt_flags & FW_OPT_NOWAIT) {
1120 timeout = usermodehelper_read_lock_wait(timeout);
1121 if (!timeout) {
1122 dev_dbg(device, "firmware: %s loading timed out\n",
1123 name);
1124 return -EBUSY;
1125 }
1126 } else {
1127 ret = usermodehelper_read_trylock();
1128 if (WARN_ON(ret)) {
1129 dev_err(device, "firmware: %s will not be loaded\n",
1130 name);
1131 return ret;
1132 }
1133 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001134
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001135 fw_priv = fw_create_instance(firmware, name, device, opt_flags);
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001136 if (IS_ERR(fw_priv)) {
1137 ret = PTR_ERR(fw_priv);
1138 goto out_unlock;
1139 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001140
1141 fw_priv->buf = firmware->priv;
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001142 ret = _request_firmware_load(fw_priv, opt_flags, timeout);
1143
1144 if (!ret)
1145 ret = assign_firmware_buf(firmware, device, opt_flags);
1146
1147out_unlock:
1148 usermodehelper_read_unlock();
1149
1150 return ret;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001151}
Ming Leiddf1f062013-06-04 10:01:13 +08001152
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001153#else /* CONFIG_FW_LOADER_USER_HELPER */
1154static inline int
1155fw_load_from_user_helper(struct firmware *firmware, const char *name,
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001156 struct device *device, unsigned int opt_flags)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001157{
1158 return -ENOENT;
1159}
Takashi Iwai807be032013-01-31 11:13:57 +01001160
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -07001161static inline void kill_pending_fw_fallback_reqs(bool only_kill_custom) { }
Ming Leiddf1f062013-06-04 10:01:13 +08001162
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001163#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001165/* prepare firmware and firmware_buf structs;
1166 * return 0 if a firmware is already assigned, 1 if need to load one,
1167 * or a negative error code
1168 */
1169static int
1170_request_firmware_prepare(struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001171 struct device *device, void *dbuf, size_t size)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001172{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +08001174 struct firmware_buf *buf;
1175 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Jiri Slaby4aed0642005-09-13 01:25:01 -07001177 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -07001179 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1180 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001181 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Stephen Boyda098ecd2016-08-02 14:04:28 -07001184 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
Luis R. Rodriguezed046302015-04-29 16:30:43 -07001185 dev_dbg(device, "using built-in %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001186 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +01001187 }
1188
Stephen Boyda098ecd2016-08-02 14:04:28 -07001189 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +08001190
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001191 /*
1192 * bind with 'buf' now to avoid warning in failure path
1193 * of requesting firmware.
1194 */
1195 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +08001196
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001197 if (ret > 0) {
Daniel Wagner5b029622016-11-17 11:00:50 +01001198 ret = fw_state_wait(&buf->fw_st);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001199 if (!ret) {
1200 fw_set_page_data(buf, firmware);
1201 return 0; /* assigned */
1202 }
Stephen Boyddddb5542012-03-28 23:30:43 +02001203 }
Ming Lei1f2b7952012-08-04 12:01:21 +08001204
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001205 if (ret < 0)
1206 return ret;
1207 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001208}
1209
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001210/* called from request_firmware() and request_firmware_work_func() */
1211static int
1212_request_firmware(const struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001213 struct device *device, void *buf, size_t size,
1214 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001215{
Brian Norris715780a2015-12-09 14:50:28 -08001216 struct firmware *fw = NULL;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001217 int ret;
1218
1219 if (!firmware_p)
1220 return -EINVAL;
1221
Brian Norris715780a2015-12-09 14:50:28 -08001222 if (!name || name[0] == '\0') {
1223 ret = -EINVAL;
1224 goto out;
1225 }
Kees Cook471b0952014-09-18 11:25:37 -07001226
Stephen Boyda098ecd2016-08-02 14:04:28 -07001227 ret = _request_firmware_prepare(&fw, name, device, buf, size);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001228 if (ret <= 0) /* error or already assigned */
1229 goto out;
1230
Luis R. Rodriguez81f95072017-05-02 01:31:05 -07001231 if (!firmware_enabled()) {
1232 WARN(1, "firmware request while host is not available\n");
1233 ret = -EHOSTDOWN;
1234 goto out;
1235 }
1236
Neil Horman3e358ac2013-09-06 15:36:08 -04001237 ret = fw_get_filesystem_firmware(device, fw->priv);
1238 if (ret) {
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001239 if (!(opt_flags & FW_OPT_NO_WARN))
Takashi Iwaibba3a872013-12-02 15:38:16 +01001240 dev_warn(device,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001241 "Direct firmware load for %s failed with error %d\n",
1242 name, ret);
1243 if (opt_flags & FW_OPT_USERHELPER) {
Takashi Iwaibba3a872013-12-02 15:38:16 +01001244 dev_warn(device, "Falling back to user helper\n");
1245 ret = fw_load_from_user_helper(fw, name, device,
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001246 opt_flags);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001247 }
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001248 } else
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001249 ret = assign_firmware_buf(fw, device, opt_flags);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001250
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001251 out:
1252 if (ret < 0) {
1253 release_firmware(fw);
1254 fw = NULL;
1255 }
1256
1257 *firmware_p = fw;
1258 return ret;
1259}
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261/**
Kay Sievers312c0042005-11-16 09:00:00 +01001262 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001263 * @firmware_p: pointer to firmware image
1264 * @name: name of firmware file
1265 * @device: device for which firmware is being loaded
1266 *
1267 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001268 * of @name for device @device.
1269 *
1270 * Should be called from user context where sleeping is allowed.
1271 *
Kay Sievers312c0042005-11-16 09:00:00 +01001272 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001273 * should be distinctive enough not to be confused with any other
1274 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001275 *
1276 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001277 *
1278 * The function can be called safely inside device's suspend and
1279 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001280 **/
1281int
1282request_firmware(const struct firmware **firmware_p, const char *name,
Andrei Opreaea310032015-03-08 12:41:15 +02001283 struct device *device)
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001284{
Ming Leid6c8aa32013-06-06 20:01:48 +08001285 int ret;
1286
1287 /* Need to pin this module until return */
1288 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001289 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001290 FW_OPT_UEVENT | FW_OPT_FALLBACK);
Ming Leid6c8aa32013-06-06 20:01:48 +08001291 module_put(THIS_MODULE);
1292 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001293}
Daniel Mackf4945132013-05-23 22:17:18 +02001294EXPORT_SYMBOL(request_firmware);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001295
Takashi Iwaibba3a872013-12-02 15:38:16 +01001296/**
Borislav Petkov3c1556b2014-12-03 22:46:37 +01001297 * request_firmware_direct: - load firmware directly without usermode helper
Takashi Iwaibba3a872013-12-02 15:38:16 +01001298 * @firmware_p: pointer to firmware image
1299 * @name: name of firmware file
1300 * @device: device for which firmware is being loaded
1301 *
1302 * This function works pretty much like request_firmware(), but this doesn't
1303 * fall back to usermode helper even if the firmware couldn't be loaded
1304 * directly from fs. Hence it's useful for loading optional firmwares, which
1305 * aren't always present, without extra long timeouts of udev.
1306 **/
1307int request_firmware_direct(const struct firmware **firmware_p,
1308 const char *name, struct device *device)
1309{
1310 int ret;
Andrei Opreaea310032015-03-08 12:41:15 +02001311
Takashi Iwaibba3a872013-12-02 15:38:16 +01001312 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001313 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001314 FW_OPT_UEVENT | FW_OPT_NO_WARN);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001315 module_put(THIS_MODULE);
1316 return ret;
1317}
1318EXPORT_SYMBOL_GPL(request_firmware_direct);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001319
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001320/**
Stephen Boyda098ecd2016-08-02 14:04:28 -07001321 * request_firmware_into_buf - load firmware into a previously allocated buffer
1322 * @firmware_p: pointer to firmware image
1323 * @name: name of firmware file
1324 * @device: device for which firmware is being loaded and DMA region allocated
1325 * @buf: address of buffer to load firmware into
1326 * @size: size of buffer
1327 *
1328 * This function works pretty much like request_firmware(), but it doesn't
1329 * allocate a buffer to hold the firmware data. Instead, the firmware
1330 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
1331 * data member is pointed at @buf.
1332 *
1333 * This function doesn't cache firmware either.
1334 */
1335int
1336request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
1337 struct device *device, void *buf, size_t size)
1338{
1339 int ret;
1340
1341 __module_get(THIS_MODULE);
1342 ret = _request_firmware(firmware_p, name, device, buf, size,
1343 FW_OPT_UEVENT | FW_OPT_FALLBACK |
1344 FW_OPT_NOCACHE);
1345 module_put(THIS_MODULE);
1346 return ret;
1347}
1348EXPORT_SYMBOL(request_firmware_into_buf);
1349
1350/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001352 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001354void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
1356 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001357 if (!fw_is_builtin_firmware(fw))
1358 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 kfree(fw);
1360 }
1361}
Daniel Mackf4945132013-05-23 22:17:18 +02001362EXPORT_SYMBOL(release_firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364/* Async support */
1365struct firmware_work {
1366 struct work_struct work;
1367 struct module *module;
1368 const char *name;
1369 struct device *device;
1370 void *context;
1371 void (*cont)(const struct firmware *fw, void *context);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001372 unsigned int opt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373};
1374
Stephen Boyda36cf842012-03-28 23:31:00 +02001375static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Stephen Boyda36cf842012-03-28 23:31:00 +02001377 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001379
Stephen Boyda36cf842012-03-28 23:31:00 +02001380 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001381
Stephen Boyda098ecd2016-08-02 14:04:28 -07001382 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001383 fw_work->opt_flags);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001384 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001385 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 module_put(fw_work->module);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001388 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
1392/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001393 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001394 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001395 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001396 * is non-zero else the firmware copy must be done manually.
1397 * @name: name of firmware file
1398 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001399 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001400 * @context: will be passed over to @cont, and
1401 * @fw may be %NULL if firmware request fails.
1402 * @cont: function will be called asynchronously when the firmware
1403 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001405 * Caller must hold the reference count of @device.
1406 *
Ming Lei6f21a622012-08-04 12:01:24 +08001407 * Asynchronous variant of request_firmware() for user contexts:
1408 * - sleep for as small periods as possible since it may
Silvio Fricke88bcef52016-11-25 15:59:47 +01001409 * increase kernel boot time of built-in device drivers
1410 * requesting firmware in their ->probe() methods, if
1411 * @gfp is GFP_KERNEL.
Ming Lei6f21a622012-08-04 12:01:24 +08001412 *
1413 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 **/
1415int
1416request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001417 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001418 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 void (*cont)(const struct firmware *fw, void *context))
1420{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001421 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Andrei Opreaea310032015-03-08 12:41:15 +02001423 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (!fw_work)
1425 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001426
1427 fw_work->module = module;
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001428 fw_work->name = kstrdup_const(name, gfp);
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001429 if (!fw_work->name) {
1430 kfree(fw_work);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001431 return -ENOMEM;
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001432 }
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001433 fw_work->device = device;
1434 fw_work->context = context;
1435 fw_work->cont = cont;
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001436 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
Takashi Iwai5a1379e2014-06-04 17:48:15 +02001437 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (!try_module_get(module)) {
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001440 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 kfree(fw_work);
1442 return -EFAULT;
1443 }
1444
Ming Lei0cfc1e12012-08-04 12:01:23 +08001445 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001446 INIT_WORK(&fw_work->work, request_firmware_work_func);
1447 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return 0;
1449}
Daniel Mackf4945132013-05-23 22:17:18 +02001450EXPORT_SYMBOL(request_firmware_nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Ming Lei90f890812013-06-20 12:30:16 +08001452#ifdef CONFIG_PM_SLEEP
1453static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1454
Ming Lei2887b392012-08-04 12:01:22 +08001455/**
1456 * cache_firmware - cache one firmware image in kernel memory space
1457 * @fw_name: the firmware image name
1458 *
1459 * Cache firmware in kernel memory so that drivers can use it when
1460 * system isn't ready for them to request firmware image from userspace.
1461 * Once it returns successfully, driver can use request_firmware or its
1462 * nowait version to get the cached firmware without any interacting
1463 * with userspace
1464 *
1465 * Return 0 if the firmware image has been cached successfully
1466 * Return !0 otherwise
1467 *
1468 */
Ming Lei93232e42013-06-06 20:01:47 +08001469static int cache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001470{
1471 int ret;
1472 const struct firmware *fw;
1473
1474 pr_debug("%s: %s\n", __func__, fw_name);
1475
1476 ret = request_firmware(&fw, fw_name, NULL);
1477 if (!ret)
1478 kfree(fw);
1479
1480 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1481
1482 return ret;
1483}
1484
Ming Lei6a2c1232013-06-26 09:28:17 +08001485static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1486{
1487 struct firmware_buf *tmp;
1488 struct firmware_cache *fwc = &fw_cache;
1489
1490 spin_lock(&fwc->lock);
1491 tmp = __fw_lookup_buf(fw_name);
1492 spin_unlock(&fwc->lock);
1493
1494 return tmp;
1495}
1496
Ming Lei2887b392012-08-04 12:01:22 +08001497/**
1498 * uncache_firmware - remove one cached firmware image
1499 * @fw_name: the firmware image name
1500 *
1501 * Uncache one firmware image which has been cached successfully
1502 * before.
1503 *
1504 * Return 0 if the firmware cache has been removed successfully
1505 * Return !0 otherwise
1506 *
1507 */
Ming Lei93232e42013-06-06 20:01:47 +08001508static int uncache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001509{
1510 struct firmware_buf *buf;
1511 struct firmware fw;
1512
1513 pr_debug("%s: %s\n", __func__, fw_name);
1514
Stephen Boyda098ecd2016-08-02 14:04:28 -07001515 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
Ming Lei2887b392012-08-04 12:01:22 +08001516 return 0;
1517
1518 buf = fw_lookup_buf(fw_name);
1519 if (buf) {
1520 fw_free_buf(buf);
1521 return 0;
1522 }
1523
1524 return -EINVAL;
1525}
1526
Ming Lei37276a52012-08-04 12:01:27 +08001527static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1528{
1529 struct fw_cache_entry *fce;
1530
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001531 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
Ming Lei37276a52012-08-04 12:01:27 +08001532 if (!fce)
1533 goto exit;
1534
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001535 fce->name = kstrdup_const(name, GFP_ATOMIC);
1536 if (!fce->name) {
1537 kfree(fce);
1538 fce = NULL;
1539 goto exit;
1540 }
Ming Lei37276a52012-08-04 12:01:27 +08001541exit:
1542 return fce;
1543}
1544
Ming Lei373304f2012-10-09 12:01:01 +08001545static int __fw_entry_found(const char *name)
1546{
1547 struct firmware_cache *fwc = &fw_cache;
1548 struct fw_cache_entry *fce;
1549
1550 list_for_each_entry(fce, &fwc->fw_names, list) {
1551 if (!strcmp(fce->name, name))
1552 return 1;
1553 }
1554 return 0;
1555}
1556
Ming Leiac39b3e2012-08-20 19:04:16 +08001557static int fw_cache_piggyback_on_request(const char *name)
1558{
1559 struct firmware_cache *fwc = &fw_cache;
1560 struct fw_cache_entry *fce;
1561 int ret = 0;
1562
1563 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001564 if (__fw_entry_found(name))
1565 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001566
1567 fce = alloc_fw_cache_entry(name);
1568 if (fce) {
1569 ret = 1;
1570 list_add(&fce->list, &fwc->fw_names);
1571 pr_debug("%s: fw: %s\n", __func__, name);
1572 }
1573found:
1574 spin_unlock(&fwc->name_lock);
1575 return ret;
1576}
1577
Ming Lei37276a52012-08-04 12:01:27 +08001578static void free_fw_cache_entry(struct fw_cache_entry *fce)
1579{
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001580 kfree_const(fce->name);
Ming Lei37276a52012-08-04 12:01:27 +08001581 kfree(fce);
1582}
1583
1584static void __async_dev_cache_fw_image(void *fw_entry,
1585 async_cookie_t cookie)
1586{
1587 struct fw_cache_entry *fce = fw_entry;
1588 struct firmware_cache *fwc = &fw_cache;
1589 int ret;
1590
1591 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001592 if (ret) {
1593 spin_lock(&fwc->name_lock);
1594 list_del(&fce->list);
1595 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001596
Ming Leiac39b3e2012-08-20 19:04:16 +08001597 free_fw_cache_entry(fce);
1598 }
Ming Lei37276a52012-08-04 12:01:27 +08001599}
1600
1601/* called with dev->devres_lock held */
1602static void dev_create_fw_entry(struct device *dev, void *res,
1603 void *data)
1604{
1605 struct fw_name_devm *fwn = res;
1606 const char *fw_name = fwn->name;
1607 struct list_head *head = data;
1608 struct fw_cache_entry *fce;
1609
1610 fce = alloc_fw_cache_entry(fw_name);
1611 if (fce)
1612 list_add(&fce->list, head);
1613}
1614
1615static int devm_name_match(struct device *dev, void *res,
1616 void *match_data)
1617{
1618 struct fw_name_devm *fwn = res;
1619 return (fwn->magic == (unsigned long)match_data);
1620}
1621
Ming Leiab6dd8e2012-08-17 22:07:00 +08001622static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001623{
1624 LIST_HEAD(todo);
1625 struct fw_cache_entry *fce;
1626 struct fw_cache_entry *fce_next;
1627 struct firmware_cache *fwc = &fw_cache;
1628
1629 devres_for_each_res(dev, fw_name_devm_release,
1630 devm_name_match, &fw_cache,
1631 dev_create_fw_entry, &todo);
1632
1633 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1634 list_del(&fce->list);
1635
1636 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001637 /* only one cache entry for one firmware */
1638 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001639 list_add(&fce->list, &fwc->fw_names);
1640 } else {
1641 free_fw_cache_entry(fce);
1642 fce = NULL;
1643 }
Ming Lei37276a52012-08-04 12:01:27 +08001644 spin_unlock(&fwc->name_lock);
1645
Ming Lei373304f2012-10-09 12:01:01 +08001646 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001647 async_schedule_domain(__async_dev_cache_fw_image,
1648 (void *)fce,
1649 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001650 }
1651}
1652
1653static void __device_uncache_fw_images(void)
1654{
1655 struct firmware_cache *fwc = &fw_cache;
1656 struct fw_cache_entry *fce;
1657
1658 spin_lock(&fwc->name_lock);
1659 while (!list_empty(&fwc->fw_names)) {
1660 fce = list_entry(fwc->fw_names.next,
1661 struct fw_cache_entry, list);
1662 list_del(&fce->list);
1663 spin_unlock(&fwc->name_lock);
1664
1665 uncache_firmware(fce->name);
1666 free_fw_cache_entry(fce);
1667
1668 spin_lock(&fwc->name_lock);
1669 }
1670 spin_unlock(&fwc->name_lock);
1671}
1672
1673/**
1674 * device_cache_fw_images - cache devices' firmware
1675 *
1676 * If one device called request_firmware or its nowait version
1677 * successfully before, the firmware names are recored into the
1678 * device's devres link list, so device_cache_fw_images can call
1679 * cache_firmware() to cache these firmwares for the device,
1680 * then the device driver can load its firmwares easily at
1681 * time when system is not ready to complete loading firmware.
1682 */
1683static void device_cache_fw_images(void)
1684{
1685 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001686 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001687 DEFINE_WAIT(wait);
1688
1689 pr_debug("%s\n", __func__);
1690
Ming Lei373304f2012-10-09 12:01:01 +08001691 /* cancel uncache work */
1692 cancel_delayed_work_sync(&fwc->work);
1693
Ming Leiffe53f62012-08-04 12:01:28 +08001694 /*
1695 * use small loading timeout for caching devices' firmware
1696 * because all these firmware images have been loaded
1697 * successfully at lease once, also system is ready for
1698 * completing firmware loading now. The maximum size of
1699 * firmware in current distributions is about 2M bytes,
1700 * so 10 secs should be enough.
1701 */
1702 old_timeout = loading_timeout;
1703 loading_timeout = 10;
1704
Ming Leiac39b3e2012-08-20 19:04:16 +08001705 mutex_lock(&fw_lock);
1706 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001707 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001708 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001709
1710 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001711 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001712
1713 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001714}
1715
1716/**
1717 * device_uncache_fw_images - uncache devices' firmware
1718 *
1719 * uncache all firmwares which have been cached successfully
1720 * by device_uncache_fw_images earlier
1721 */
1722static void device_uncache_fw_images(void)
1723{
1724 pr_debug("%s\n", __func__);
1725 __device_uncache_fw_images();
1726}
1727
1728static void device_uncache_fw_images_work(struct work_struct *work)
1729{
1730 device_uncache_fw_images();
1731}
1732
1733/**
1734 * device_uncache_fw_images_delay - uncache devices firmwares
1735 * @delay: number of milliseconds to delay uncache device firmwares
1736 *
1737 * uncache all devices's firmwares which has been cached successfully
1738 * by device_cache_fw_images after @delay milliseconds.
1739 */
1740static void device_uncache_fw_images_delay(unsigned long delay)
1741{
Shaibal Duttabce66182014-01-31 15:44:58 -08001742 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1743 msecs_to_jiffies(delay));
Ming Lei37276a52012-08-04 12:01:27 +08001744}
1745
Luis R. Rodriguez81f95072017-05-02 01:31:05 -07001746/**
1747 * fw_pm_notify - notifier for suspend/resume
1748 * @notify_block: unused
1749 * @mode: mode we are switching to
1750 * @unused: unused
1751 *
1752 * Used to modify the firmware_class state as we move in between states.
1753 * The firmware_class implements a firmware cache to enable device driver
1754 * to fetch firmware upon resume before the root filesystem is ready. We
1755 * disable API calls which do not use the built-in firmware or the firmware
1756 * cache when we know these calls will not work.
1757 *
1758 * The inner logic behind all this is a bit complex so it is worth summarizing
1759 * the kernel's own suspend/resume process with context and focus on how this
1760 * can impact the firmware API.
1761 *
1762 * First a review on how we go to suspend::
1763 *
1764 * pm_suspend() --> enter_state() -->
1765 * sys_sync()
1766 * suspend_prepare() -->
1767 * __pm_notifier_call_chain(PM_SUSPEND_PREPARE, ...);
1768 * suspend_freeze_processes() -->
1769 * freeze_processes() -->
1770 * __usermodehelper_set_disable_depth(UMH_DISABLED);
1771 * freeze all tasks ...
1772 * freeze_kernel_threads()
1773 * suspend_devices_and_enter() -->
1774 * dpm_suspend_start() -->
1775 * dpm_prepare()
1776 * dpm_suspend()
1777 * suspend_enter() -->
1778 * platform_suspend_prepare()
1779 * dpm_suspend_late()
1780 * freeze_enter()
1781 * syscore_suspend()
1782 *
1783 * When we resume we bail out of a loop from suspend_devices_and_enter() and
1784 * unwind back out to the caller enter_state() where we were before as follows::
1785 *
1786 * enter_state() -->
1787 * suspend_devices_and_enter() --> (bail from loop)
1788 * dpm_resume_end() -->
1789 * dpm_resume()
1790 * dpm_complete()
1791 * suspend_finish() -->
1792 * suspend_thaw_processes() -->
1793 * thaw_processes() -->
1794 * __usermodehelper_set_disable_depth(UMH_FREEZING);
1795 * thaw_workqueues();
1796 * thaw all processes ...
1797 * usermodehelper_enable();
1798 * pm_notifier_call_chain(PM_POST_SUSPEND);
1799 *
1800 * fw_pm_notify() works through pm_notifier_call_chain().
1801 */
Ming Lei07646d92012-08-04 12:01:29 +08001802static int fw_pm_notify(struct notifier_block *notify_block,
1803 unsigned long mode, void *unused)
1804{
1805 switch (mode) {
1806 case PM_HIBERNATION_PREPARE:
1807 case PM_SUSPEND_PREPARE:
Sebastian Capellaf8d5b9e2014-02-18 17:52:08 -08001808 case PM_RESTORE_PREPARE:
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -07001809 /*
1810 * kill pending fallback requests with a custom fallback
1811 * to avoid stalling suspend.
1812 */
1813 kill_pending_fw_fallback_reqs(true);
Ming Lei07646d92012-08-04 12:01:29 +08001814 device_cache_fw_images();
Luis R. Rodriguez81f95072017-05-02 01:31:05 -07001815 disable_firmware();
Ming Lei07646d92012-08-04 12:01:29 +08001816 break;
1817
1818 case PM_POST_SUSPEND:
1819 case PM_POST_HIBERNATION:
1820 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001821 /*
1822 * In case that system sleep failed and syscore_suspend is
1823 * not called.
1824 */
1825 mutex_lock(&fw_lock);
1826 fw_cache.state = FW_LOADER_NO_CACHE;
1827 mutex_unlock(&fw_lock);
Luis R. Rodriguez81f95072017-05-02 01:31:05 -07001828 enable_firmware();
Ming Leiac39b3e2012-08-20 19:04:16 +08001829
Ming Lei07646d92012-08-04 12:01:29 +08001830 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1831 break;
1832 }
1833
1834 return 0;
1835}
Ming Lei07646d92012-08-04 12:01:29 +08001836
Ming Leiac39b3e2012-08-20 19:04:16 +08001837/* stop caching firmware once syscore_suspend is reached */
1838static int fw_suspend(void)
1839{
1840 fw_cache.state = FW_LOADER_NO_CACHE;
1841 return 0;
1842}
1843
1844static struct syscore_ops fw_syscore_ops = {
1845 .suspend = fw_suspend,
1846};
Ming Leicfe016b2012-09-08 17:32:30 +08001847#else
1848static int fw_cache_piggyback_on_request(const char *name)
1849{
1850 return 0;
1851}
1852#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001853
Ming Lei37276a52012-08-04 12:01:27 +08001854static void __init fw_cache_init(void)
1855{
1856 spin_lock_init(&fw_cache.lock);
1857 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001858 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001859
Ming Leicfe016b2012-09-08 17:32:30 +08001860#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +08001861 spin_lock_init(&fw_cache.name_lock);
1862 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001863
Ming Lei37276a52012-08-04 12:01:27 +08001864 INIT_DELAYED_WORK(&fw_cache.work,
1865 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001866
1867 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1868 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001869
1870 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001871#endif
Ming Lei37276a52012-08-04 12:01:27 +08001872}
1873
Luis R. Rodrigueza669f042017-05-02 01:31:04 -07001874static int fw_shutdown_notify(struct notifier_block *unused1,
1875 unsigned long unused2, void *unused3)
1876{
Luis R. Rodriguez81f95072017-05-02 01:31:05 -07001877 disable_firmware();
Luis R. Rodrigueza669f042017-05-02 01:31:04 -07001878 /*
1879 * Kill all pending fallback requests to avoid both stalling shutdown,
1880 * and avoid a deadlock with the usermode_lock.
1881 */
1882 kill_pending_fw_fallback_reqs(false);
1883
1884 return NOTIFY_DONE;
1885}
1886
1887static struct notifier_block fw_shutdown_nb = {
1888 .notifier_call = fw_shutdown_notify,
1889};
1890
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001891static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Luis R. Rodriguez81f95072017-05-02 01:31:05 -07001893 enable_firmware();
Ming Lei1f2b7952012-08-04 12:01:21 +08001894 fw_cache_init();
Takashi Iwaife304142013-05-22 18:28:38 +02001895 register_reboot_notifier(&fw_shutdown_nb);
Luis R. Rodrigueza669f042017-05-02 01:31:04 -07001896#ifdef CONFIG_FW_LOADER_USER_HELPER
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001897 return class_register(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001898#else
1899 return 0;
1900#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001902
1903static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
Luis R. Rodriguez81f95072017-05-02 01:31:05 -07001905 disable_firmware();
Ming Leicfe016b2012-09-08 17:32:30 +08001906#ifdef CONFIG_PM_SLEEP
Ming Leiac39b3e2012-08-20 19:04:16 +08001907 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001908 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001909#endif
Takashi Iwaife304142013-05-22 18:28:38 +02001910 unregister_reboot_notifier(&fw_shutdown_nb);
Luis R. Rodrigueza669f042017-05-02 01:31:04 -07001911#ifdef CONFIG_FW_LOADER_USER_HELPER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 class_unregister(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001913#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
1915
Shaohua Lia30a6a22006-09-27 01:50:52 -07001916fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917module_exit(firmware_class_exit);