blob: 3b506d375897c8491493246d9e511954aa1427fe [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>
Ming Lei37276a52012-08-04 12:01:27 +080035
Linus Torvaldsabb139e2012-10-03 15:58:32 -070036#include <generated/utsrelease.h>
37
Ming Lei37276a52012-08-04 12:01:27 +080038#include "base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Markus Rechberger87d37a42007-06-04 18:45:44 +020040MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070041MODULE_DESCRIPTION("Multi purpose firmware loading support");
42MODULE_LICENSE("GPL");
43
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010044enum fw_status {
45 FW_STATUS_UNKNOWN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 FW_STATUS_LOADING,
47 FW_STATUS_DONE,
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010048 FW_STATUS_ABORTED,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010051/*
52 * Concurrent request_firmware() for the same firmware need to be
53 * serialized. struct fw_state is simple state machine which hold the
54 * state of the firmware loading.
55 */
56struct fw_state {
Luis R. Rodrigueze44565f2017-07-20 13:13:09 -070057 struct completion completion;
Daniel Wagner0430caf2016-11-17 11:00:49 +010058 enum fw_status status;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010059};
60
Takashi Iwai14c4bae2013-12-02 15:38:18 +010061/* firmware behavior options */
62#define FW_OPT_UEVENT (1U << 0)
63#define FW_OPT_NOWAIT (1U << 1)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +010064#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwai5a1379e2014-06-04 17:48:15 +020065#define FW_OPT_USERHELPER (1U << 2)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +010066#else
Takashi Iwai5a1379e2014-06-04 17:48:15 +020067#define FW_OPT_USERHELPER 0
68#endif
69#ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK
70#define FW_OPT_FALLBACK FW_OPT_USERHELPER
71#else
72#define FW_OPT_FALLBACK 0
Takashi Iwai68aeeaa2013-12-02 15:38:19 +010073#endif
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -070074#define FW_OPT_NO_WARN (1U << 3)
Vikram Mulukutla0e742e92016-08-02 14:04:25 -070075#define FW_OPT_NOCACHE (1U << 4)
Takashi Iwai14c4bae2013-12-02 15:38:18 +010076
Ming Lei1f2b7952012-08-04 12:01:21 +080077struct firmware_cache {
78 /* firmware_buf instance will be added into the below list */
79 spinlock_t lock;
80 struct list_head head;
Ming Leicfe016b2012-09-08 17:32:30 +080081 int state;
Ming Lei37276a52012-08-04 12:01:27 +080082
Ming Leicfe016b2012-09-08 17:32:30 +080083#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +080084 /*
85 * Names of firmware images which have been cached successfully
86 * will be added into the below list so that device uncache
87 * helper can trace which firmware images have been cached
88 * before.
89 */
90 spinlock_t name_lock;
91 struct list_head fw_names;
92
Ming Lei37276a52012-08-04 12:01:27 +080093 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +080094
95 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +080096#endif
Ming Lei1f2b7952012-08-04 12:01:21 +080097};
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -080099struct fw_priv {
Ming Lei1f2b7952012-08-04 12:01:21 +0800100 struct kref ref;
101 struct list_head list;
Ming Lei1f2b7952012-08-04 12:01:21 +0800102 struct firmware_cache *fwc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100103 struct fw_state fw_st;
Ming Lei65710cb2012-08-04 12:01:16 +0800104 void *data;
105 size_t size;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700106 size_t allocated_size;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100107#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100108 bool is_paged_buf;
Ming Leiaf5bc112013-05-27 18:30:04 +0800109 bool need_uevent;
David Woodhouse6e03a202009-04-09 22:04:07 -0700110 struct page **pages;
111 int nr_pages;
112 int page_array_size;
Takashi Iwaife304142013-05-22 18:28:38 +0200113 struct list_head pending_list;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100114#endif
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800115 const char *fw_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116};
117
Ming Lei37276a52012-08-04 12:01:27 +0800118struct fw_cache_entry {
119 struct list_head list;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700120 const char *name;
Ming Lei37276a52012-08-04 12:01:27 +0800121};
122
Ming Leif531f05a2012-08-04 12:01:25 +0800123struct fw_name_devm {
124 unsigned long magic;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700125 const char *name;
Ming Leif531f05a2012-08-04 12:01:25 +0800126};
127
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800128#define to_fw_priv(d) container_of(d, struct fw_priv, ref)
Ming Lei1f2b7952012-08-04 12:01:21 +0800129
Ming Leiac39b3e2012-08-20 19:04:16 +0800130#define FW_LOADER_NO_CACHE 0
131#define FW_LOADER_START_CACHE 1
132
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800133/* fw_lock could be moved to 'struct fw_sysfs' but since it is just
Ming Lei1f2b7952012-08-04 12:01:21 +0800134 * guarding for corner cases a global lock should be OK */
135static DEFINE_MUTEX(fw_lock);
136
137static struct firmware_cache fw_cache;
138
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800139/* Builtin firmware support */
140
141#ifdef CONFIG_FW_LOADER
142
143extern struct builtin_fw __start_builtin_fw[];
144extern struct builtin_fw __end_builtin_fw[];
145
146static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
147 void *buf, size_t size)
148{
149 struct builtin_fw *b_fw;
150
151 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
152 if (strcmp(name, b_fw->name) == 0) {
153 fw->size = b_fw->size;
154 fw->data = b_fw->data;
155
156 if (buf && fw->size <= size)
157 memcpy(buf, fw->data, fw->size);
158 return true;
159 }
160 }
161
162 return false;
163}
164
165static bool fw_is_builtin_firmware(const struct firmware *fw)
166{
167 struct builtin_fw *b_fw;
168
169 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
170 if (fw->data == b_fw->data)
171 return true;
172
173 return false;
174}
175
176#else /* Module case - no builtin firmware support */
177
178static inline bool fw_get_builtin_firmware(struct firmware *fw,
179 const char *name, void *buf,
180 size_t size)
181{
182 return false;
183}
184
185static inline bool fw_is_builtin_firmware(const struct firmware *fw)
186{
187 return false;
188}
189#endif
190
191static int loading_timeout = 60; /* In seconds */
192
193static inline long firmware_loading_timeout(void)
194{
195 return loading_timeout > 0 ? loading_timeout * HZ : MAX_JIFFY_OFFSET;
196}
197
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800198static void fw_state_init(struct fw_priv *fw_priv)
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800199{
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800200 struct fw_state *fw_st = &fw_priv->fw_st;
201
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800202 init_completion(&fw_st->completion);
203 fw_st->status = FW_STATUS_UNKNOWN;
204}
205
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800206static int __fw_state_wait_common(struct fw_priv *fw_priv, long timeout)
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800207{
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800208 struct fw_state *fw_st = &fw_priv->fw_st;
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800209 long ret;
210
211 ret = wait_for_completion_killable_timeout(&fw_st->completion, timeout);
212 if (ret != 0 && fw_st->status == FW_STATUS_ABORTED)
213 return -ENOENT;
214 if (!ret)
215 return -ETIMEDOUT;
216
217 return ret < 0 ? ret : 0;
218}
219
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800220static void __fw_state_set(struct fw_priv *fw_priv,
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800221 enum fw_status status)
222{
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800223 struct fw_state *fw_st = &fw_priv->fw_st;
224
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800225 WRITE_ONCE(fw_st->status, status);
226
227 if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED)
228 complete_all(&fw_st->completion);
229}
230
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800231static inline void fw_state_start(struct fw_priv *fw_priv)
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800232{
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800233 __fw_state_set(fw_priv, FW_STATUS_LOADING);
234}
235
236static inline void fw_state_done(struct fw_priv *fw_priv)
237{
238 __fw_state_set(fw_priv, FW_STATUS_DONE);
239}
240
241static inline void fw_state_aborted(struct fw_priv *fw_priv)
242{
243 __fw_state_set(fw_priv, FW_STATUS_ABORTED);
244}
245
246static inline int fw_state_wait(struct fw_priv *fw_priv)
247{
248 return __fw_state_wait_common(fw_priv, MAX_SCHEDULE_TIMEOUT);
249}
250
251static bool __fw_state_check(struct fw_priv *fw_priv,
252 enum fw_status status)
253{
254 struct fw_state *fw_st = &fw_priv->fw_st;
255
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800256 return fw_st->status == status;
257}
258
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800259static inline bool fw_state_is_aborted(struct fw_priv *fw_priv)
260{
261 return __fw_state_check(fw_priv, FW_STATUS_ABORTED);
262}
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800263
264#ifdef CONFIG_FW_LOADER_USER_HELPER
265
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800266static inline bool fw_state_is_done(struct fw_priv *fw_priv)
267{
268 return __fw_state_check(fw_priv, FW_STATUS_DONE);
269}
270
271static inline bool fw_state_is_loading(struct fw_priv *fw_priv)
272{
273 return __fw_state_check(fw_priv, FW_STATUS_LOADING);
274}
275
276static inline int fw_state_wait_timeout(struct fw_priv *fw_priv, long timeout)
277{
278 return __fw_state_wait_common(fw_priv, timeout);
279}
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800280
281#endif /* CONFIG_FW_LOADER_USER_HELPER */
282
283static int fw_cache_piggyback_on_request(const char *name);
284
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800285static struct fw_priv *__allocate_fw_priv(const char *fw_name,
286 struct firmware_cache *fwc,
287 void *dbuf, size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800288{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800289 struct fw_priv *fw_priv;
Ming Lei1f2b7952012-08-04 12:01:21 +0800290
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800291 fw_priv = kzalloc(sizeof(*fw_priv), GFP_ATOMIC);
292 if (!fw_priv)
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700293 return NULL;
294
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800295 fw_priv->fw_name = kstrdup_const(fw_name, GFP_ATOMIC);
296 if (!fw_priv->fw_name) {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800297 kfree(fw_priv);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700298 return NULL;
299 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800300
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800301 kref_init(&fw_priv->ref);
302 fw_priv->fwc = fwc;
303 fw_priv->data = dbuf;
304 fw_priv->allocated_size = size;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800305 fw_state_init(fw_priv);
Takashi Iwaife304142013-05-22 18:28:38 +0200306#ifdef CONFIG_FW_LOADER_USER_HELPER
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800307 INIT_LIST_HEAD(&fw_priv->pending_list);
Takashi Iwaife304142013-05-22 18:28:38 +0200308#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800309
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800310 pr_debug("%s: fw-%s fw_priv=%p\n", __func__, fw_name, fw_priv);
Ming Lei1f2b7952012-08-04 12:01:21 +0800311
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800312 return fw_priv;
Ming Lei1f2b7952012-08-04 12:01:21 +0800313}
314
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800315static struct fw_priv *__lookup_fw_priv(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +0800316{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800317 struct fw_priv *tmp;
Ming Lei2887b392012-08-04 12:01:22 +0800318 struct firmware_cache *fwc = &fw_cache;
319
320 list_for_each_entry(tmp, &fwc->head, list)
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800321 if (!strcmp(tmp->fw_name, fw_name))
Ming Lei2887b392012-08-04 12:01:22 +0800322 return tmp;
323 return NULL;
324}
325
Luis R. Rodriguez30172be2017-07-20 13:13:41 -0700326/* Returns 1 for batching firmware requests with the same name */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800327static int alloc_lookup_fw_priv(const char *fw_name,
328 struct firmware_cache *fwc,
329 struct fw_priv **fw_priv, void *dbuf,
330 size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800331{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800332 struct fw_priv *tmp;
Ming Lei1f2b7952012-08-04 12:01:21 +0800333
334 spin_lock(&fwc->lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800335 tmp = __lookup_fw_priv(fw_name);
Ming Lei2887b392012-08-04 12:01:22 +0800336 if (tmp) {
337 kref_get(&tmp->ref);
338 spin_unlock(&fwc->lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800339 *fw_priv = tmp;
340 pr_debug("batched request - sharing the same struct fw_priv and lookup for multiple requests\n");
Ming Lei2887b392012-08-04 12:01:22 +0800341 return 1;
342 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800343 tmp = __allocate_fw_priv(fw_name, fwc, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +0800344 if (tmp)
345 list_add(&tmp->list, &fwc->head);
346 spin_unlock(&fwc->lock);
347
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800348 *fw_priv = tmp;
Ming Lei1f2b7952012-08-04 12:01:21 +0800349
350 return tmp ? 0 : -ENOMEM;
351}
352
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800353static void __free_fw_priv(struct kref *ref)
Bart Van Assche98233b22014-01-04 14:20:36 +0100354 __releases(&fwc->lock)
Ming Lei1f2b7952012-08-04 12:01:21 +0800355{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800356 struct fw_priv *fw_priv = to_fw_priv(ref);
357 struct firmware_cache *fwc = fw_priv->fwc;
Ming Lei1f2b7952012-08-04 12:01:21 +0800358
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800359 pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800360 __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800361 (unsigned int)fw_priv->size);
Ming Lei1f2b7952012-08-04 12:01:21 +0800362
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800363 list_del(&fw_priv->list);
Ming Lei1f2b7952012-08-04 12:01:21 +0800364 spin_unlock(&fwc->lock);
365
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100366#ifdef CONFIG_FW_LOADER_USER_HELPER
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800367 if (fw_priv->is_paged_buf) {
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100368 int i;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800369 vunmap(fw_priv->data);
370 for (i = 0; i < fw_priv->nr_pages; i++)
371 __free_page(fw_priv->pages[i]);
372 vfree(fw_priv->pages);
Ming Lei746058f2012-10-09 12:01:03 +0800373 } else
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100374#endif
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800375 if (!fw_priv->allocated_size)
376 vfree(fw_priv->data);
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800377 kfree_const(fw_priv->fw_name);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800378 kfree(fw_priv);
Ming Lei1f2b7952012-08-04 12:01:21 +0800379}
380
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800381static void free_fw_priv(struct fw_priv *fw_priv)
Ming Lei1f2b7952012-08-04 12:01:21 +0800382{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800383 struct firmware_cache *fwc = fw_priv->fwc;
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800384 spin_lock(&fwc->lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800385 if (!kref_put(&fw_priv->ref, __free_fw_priv))
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800386 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800387}
388
Ming Lei746058f2012-10-09 12:01:03 +0800389/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800390static char fw_path_para[256];
391static const char * const fw_path[] = {
392 fw_path_para,
Ming Lei746058f2012-10-09 12:01:03 +0800393 "/lib/firmware/updates/" UTS_RELEASE,
394 "/lib/firmware/updates",
395 "/lib/firmware/" UTS_RELEASE,
396 "/lib/firmware"
397};
398
Ming Lei27602842012-11-03 17:47:58 +0800399/*
400 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
401 * from kernel command line because firmware_class is generally built in
402 * kernel instead of module.
403 */
404module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
405MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
406
Stephen Boyda098ecd2016-08-02 14:04:28 -0700407static int
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800408fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv)
Ming Lei746058f2012-10-09 12:01:03 +0800409{
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500410 loff_t size;
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700411 int i, len;
Neil Horman3e358ac2013-09-06 15:36:08 -0400412 int rc = -ENOENT;
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700413 char *path;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700414 enum kernel_read_file_id id = READING_FIRMWARE;
415 size_t msize = INT_MAX;
416
417 /* Already populated data member means we're loading into a buffer */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800418 if (fw_priv->data) {
Stephen Boyda098ecd2016-08-02 14:04:28 -0700419 id = READING_FIRMWARE_PREALLOC_BUFFER;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800420 msize = fw_priv->allocated_size;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700421 }
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700422
423 path = __getname();
424 if (!path)
425 return -ENOMEM;
Ming Lei746058f2012-10-09 12:01:03 +0800426
427 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
Ming Lei27602842012-11-03 17:47:58 +0800428 /* skip the unset customized path */
429 if (!fw_path[i][0])
430 continue;
431
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700432 len = snprintf(path, PATH_MAX, "%s/%s",
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800433 fw_path[i], fw_priv->fw_name);
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700434 if (len >= PATH_MAX) {
435 rc = -ENAMETOOLONG;
436 break;
437 }
Ming Lei746058f2012-10-09 12:01:03 +0800438
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800439 fw_priv->size = 0;
440 rc = kernel_read_file_from_path(path, &fw_priv->data, &size,
441 msize, id);
Kees Cook4b2530d2016-02-04 13:15:02 -0800442 if (rc) {
Luis R. Rodriguez8e516aa2016-02-28 21:57:55 +0100443 if (rc == -ENOENT)
444 dev_dbg(device, "loading %s failed with error %d\n",
445 path, rc);
446 else
447 dev_warn(device, "loading %s failed with error %d\n",
448 path, rc);
Kees Cook4b2530d2016-02-04 13:15:02 -0800449 continue;
450 }
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800451 dev_dbg(device, "direct-loading %s\n", fw_priv->fw_name);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800452 fw_priv->size = size;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800453 fw_state_done(fw_priv);
Kees Cook4b2530d2016-02-04 13:15:02 -0800454 break;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100455 }
Kees Cook4b2530d2016-02-04 13:15:02 -0800456 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100457
Neil Horman3e358ac2013-09-06 15:36:08 -0400458 return rc;
Ming Lei746058f2012-10-09 12:01:03 +0800459}
460
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100461/* firmware holds the ownership of pages */
462static void firmware_free_data(const struct firmware *fw)
463{
464 /* Loaded directly? */
465 if (!fw->priv) {
466 vfree(fw->data);
467 return;
468 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800469 free_fw_priv(fw->priv);
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100470}
471
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100472/* store the pages buffer info firmware from buf */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800473static void fw_set_page_data(struct fw_priv *fw_priv, struct firmware *fw)
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100474{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800475 fw->priv = fw_priv;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100476#ifdef CONFIG_FW_LOADER_USER_HELPER
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800477 fw->pages = fw_priv->pages;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100478#endif
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800479 fw->size = fw_priv->size;
480 fw->data = fw_priv->data;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100481
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800482 pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800483 __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800484 (unsigned int)fw_priv->size);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100485}
486
487#ifdef CONFIG_PM_SLEEP
488static void fw_name_devm_release(struct device *dev, void *res)
489{
490 struct fw_name_devm *fwn = res;
491
492 if (fwn->magic == (unsigned long)&fw_cache)
493 pr_debug("%s: fw_name-%s devm-%p released\n",
494 __func__, fwn->name, res);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700495 kfree_const(fwn->name);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100496}
497
498static int fw_devm_match(struct device *dev, void *res,
499 void *match_data)
500{
501 struct fw_name_devm *fwn = res;
502
503 return (fwn->magic == (unsigned long)&fw_cache) &&
504 !strcmp(fwn->name, match_data);
505}
506
507static struct fw_name_devm *fw_find_devm_name(struct device *dev,
508 const char *name)
509{
510 struct fw_name_devm *fwn;
511
512 fwn = devres_find(dev, fw_name_devm_release,
513 fw_devm_match, (void *)name);
514 return fwn;
515}
516
517/* add firmware name into devres list */
518static int fw_add_devm_name(struct device *dev, const char *name)
519{
520 struct fw_name_devm *fwn;
521
522 fwn = fw_find_devm_name(dev, name);
523 if (fwn)
524 return 1;
525
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700526 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
527 GFP_KERNEL);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100528 if (!fwn)
529 return -ENOMEM;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700530 fwn->name = kstrdup_const(name, GFP_KERNEL);
531 if (!fwn->name) {
Vladimir Zapolskiya885de62015-07-29 23:26:28 +0300532 devres_free(fwn);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700533 return -ENOMEM;
534 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100535
536 fwn->magic = (unsigned long)&fw_cache;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100537 devres_add(dev, fwn);
538
539 return 0;
540}
541#else
542static int fw_add_devm_name(struct device *dev, const char *name)
543{
544 return 0;
545}
546#endif
547
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800548static int assign_fw(struct firmware *fw, struct device *device,
549 unsigned int opt_flags)
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700550{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800551 struct fw_priv *fw_priv = fw->priv;
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700552
553 mutex_lock(&fw_lock);
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800554 if (!fw_priv->size || fw_state_is_aborted(fw_priv)) {
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700555 mutex_unlock(&fw_lock);
556 return -ENOENT;
557 }
558
559 /*
560 * add firmware name into devres list so that we can auto cache
561 * and uncache firmware for device.
562 *
563 * device may has been deleted already, but the problem
564 * should be fixed in devres or driver core.
565 */
566 /* don't cache firmware handled without uevent */
567 if (device && (opt_flags & FW_OPT_UEVENT) &&
568 !(opt_flags & FW_OPT_NOCACHE))
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800569 fw_add_devm_name(device, fw_priv->fw_name);
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700570
571 /*
572 * After caching firmware image is started, let it piggyback
573 * on request firmware.
574 */
575 if (!(opt_flags & FW_OPT_NOCACHE) &&
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800576 fw_priv->fwc->state == FW_LOADER_START_CACHE) {
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800577 if (fw_cache_piggyback_on_request(fw_priv->fw_name))
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800578 kref_get(&fw_priv->ref);
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700579 }
580
581 /* pass the pages buffer to driver at the last minute */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800582 fw_set_page_data(fw_priv, fw);
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700583 mutex_unlock(&fw_lock);
584 return 0;
585}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100586
587/*
588 * user-mode helper code
589 */
590#ifdef CONFIG_FW_LOADER_USER_HELPER
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800591struct fw_sysfs {
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100592 bool nowait;
593 struct device dev;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800594 struct fw_priv *fw_priv;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100595 struct firmware *fw;
596};
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100597
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800598static struct fw_sysfs *to_fw_sysfs(struct device *dev)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700599{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800600 return container_of(dev, struct fw_sysfs, dev);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700601}
602
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800603static void __fw_load_abort(struct fw_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Ming Lei87597932013-06-15 16:36:38 +0800605 /*
606 * There is a small window in which user can write to 'loading'
607 * between loading done and disappearance of 'loading'
608 */
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800609 if (fw_state_is_done(fw_priv))
Ming Lei87597932013-06-15 16:36:38 +0800610 return;
611
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800612 list_del_init(&fw_priv->pending_list);
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800613 fw_state_aborted(fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800616static void fw_load_abort(struct fw_sysfs *fw_sysfs)
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700617{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800618 struct fw_priv *fw_priv = fw_sysfs->fw_priv;
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700619
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800620 __fw_load_abort(fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Takashi Iwaife304142013-05-22 18:28:38 +0200623static LIST_HEAD(pending_fw_head);
624
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -0700625static void kill_pending_fw_fallback_reqs(bool only_kill_custom)
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700626{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800627 struct fw_priv *fw_priv;
628 struct fw_priv *next;
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700629
630 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800631 list_for_each_entry_safe(fw_priv, next, &pending_fw_head,
632 pending_list) {
633 if (!fw_priv->need_uevent || !only_kill_custom)
634 __fw_load_abort(fw_priv);
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700635 }
636 mutex_unlock(&fw_lock);
637}
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700638
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700639static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
640 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 return sprintf(buf, "%d\n", loading_timeout);
643}
644
645/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800646 * firmware_timeout_store - set number of seconds to wait for firmware
647 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800648 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800649 * @buf: buffer to scan for timeout value
650 * @count: number of bytes in @buf
651 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800653 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 * firmware will be provided.
655 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800656 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 **/
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700658static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
659 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700662 if (loading_timeout < 0)
663 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return count;
666}
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100667static CLASS_ATTR_RW(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100669static struct attribute *firmware_class_attrs[] = {
670 &class_attr_timeout.attr,
671 NULL,
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800672};
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100673ATTRIBUTE_GROUPS(firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800675static void fw_dev_release(struct device *dev)
676{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800677 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800678
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800679 kfree(fw_sysfs);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800680}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800682static int do_firmware_uevent(struct fw_sysfs *fw_sysfs, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800684 if (add_uevent_var(env, "FIRMWARE=%s", fw_sysfs->fw_priv->fw_name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200686 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700687 return -ENOMEM;
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800688 if (add_uevent_var(env, "ASYNC=%d", fw_sysfs->nowait))
Johannes Berge9045f92010-03-29 17:57:20 +0200689 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 return 0;
692}
693
Linus Torvalds6f957722015-07-09 11:20:01 -0700694static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
695{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800696 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Linus Torvalds6f957722015-07-09 11:20:01 -0700697 int err = 0;
698
699 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800700 if (fw_sysfs->fw_priv)
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800701 err = do_firmware_uevent(fw_sysfs, env);
Linus Torvalds6f957722015-07-09 11:20:01 -0700702 mutex_unlock(&fw_lock);
703 return err;
704}
705
Adrian Bunk1b81d662006-05-20 15:00:16 -0700706static struct class firmware_class = {
707 .name = "firmware",
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100708 .class_groups = firmware_class_groups,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700709 .dev_uevent = firmware_uevent,
710 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700711};
712
Luis R. Rodriguez6bb9cf32017-11-20 09:45:33 -0800713static inline int register_sysfs_loader(void)
714{
715 return class_register(&firmware_class);
716}
717
718static inline void unregister_sysfs_loader(void)
719{
720 class_unregister(&firmware_class);
721}
722
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700723static ssize_t firmware_loading_show(struct device *dev,
724 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800726 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Ming Lei87597932013-06-15 16:36:38 +0800727 int loading = 0;
728
729 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800730 if (fw_sysfs->fw_priv)
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800731 loading = fw_state_is_loading(fw_sysfs->fw_priv);
Ming Lei87597932013-06-15 16:36:38 +0800732 mutex_unlock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return sprintf(buf, "%d\n", loading);
735}
736
David Woodhouse6e03a202009-04-09 22:04:07 -0700737/* Some architectures don't have PAGE_KERNEL_RO */
738#ifndef PAGE_KERNEL_RO
739#define PAGE_KERNEL_RO PAGE_KERNEL
740#endif
Ming Lei253c9242012-10-09 12:01:02 +0800741
742/* one pages buffer should be mapped/unmapped only once */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800743static int map_fw_priv_pages(struct fw_priv *fw_priv)
Ming Lei253c9242012-10-09 12:01:02 +0800744{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800745 if (!fw_priv->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800746 return 0;
747
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800748 vunmap(fw_priv->data);
749 fw_priv->data = vmap(fw_priv->pages, fw_priv->nr_pages, 0,
750 PAGE_KERNEL_RO);
751 if (!fw_priv->data)
Ming Lei253c9242012-10-09 12:01:02 +0800752 return -ENOMEM;
753 return 0;
754}
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800757 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700758 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800759 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800760 * @buf: buffer to scan for loading control value
761 * @count: number of bytes in @buf
762 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 * The relevant values are:
764 *
765 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800766 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 * -1: Conclude the load with an error and discard any written data.
768 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700769static ssize_t firmware_loading_store(struct device *dev,
770 struct device_attribute *attr,
771 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800773 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800774 struct fw_priv *fw_priv;
Kees Cook6593d922014-02-25 13:06:00 -0800775 ssize_t written = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700777 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Neil Hormaneea915b2012-01-02 15:31:23 -0500779 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800780 fw_priv = fw_sysfs->fw_priv;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800781 if (fw_state_is_aborted(fw_priv))
Neil Hormaneea915b2012-01-02 15:31:23 -0500782 goto out;
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 switch (loading) {
785 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800786 /* discarding any previous partial load */
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800787 if (!fw_state_is_done(fw_priv)) {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800788 for (i = 0; i < fw_priv->nr_pages; i++)
789 __free_page(fw_priv->pages[i]);
790 vfree(fw_priv->pages);
791 fw_priv->pages = NULL;
792 fw_priv->page_array_size = 0;
793 fw_priv->nr_pages = 0;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800794 fw_state_start(fw_priv);
Ming Lei28eefa72012-08-04 12:01:17 +0800795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 break;
797 case 0:
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800798 if (fw_state_is_loading(fw_priv)) {
Kees Cook6593d922014-02-25 13:06:00 -0800799 int rc;
800
Ming Lei253c9242012-10-09 12:01:02 +0800801 /*
802 * Several loading requests may be pending on
803 * one same firmware buf, so let all requests
804 * see the mapped 'buf->data' once the loading
805 * is completed.
806 * */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800807 rc = map_fw_priv_pages(fw_priv);
Kees Cook6593d922014-02-25 13:06:00 -0800808 if (rc)
zhang jun2b1278c2014-02-13 15:18:47 +0800809 dev_err(dev, "%s: map pages failed\n",
810 __func__);
Kees Cook6593d922014-02-25 13:06:00 -0800811 else
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500812 rc = security_kernel_post_read_file(NULL,
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800813 fw_priv->data, fw_priv->size,
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500814 READING_FIRMWARE);
Kees Cook6593d922014-02-25 13:06:00 -0800815
816 /*
817 * Same logic as fw_load_abort, only the DONE bit
818 * is ignored and we set ABORT only on failure.
819 */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800820 list_del_init(&fw_priv->pending_list);
Kees Cook6593d922014-02-25 13:06:00 -0800821 if (rc) {
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800822 fw_state_aborted(fw_priv);
Kees Cook6593d922014-02-25 13:06:00 -0800823 written = rc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100824 } else {
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800825 fw_state_done(fw_priv);
Kees Cook6593d922014-02-25 13:06:00 -0800826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 break;
828 }
829 /* fallthrough */
830 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700831 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /* fallthrough */
833 case -1:
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800834 fw_load_abort(fw_sysfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 break;
836 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500837out:
838 mutex_unlock(&fw_lock);
Kees Cook6593d922014-02-25 13:06:00 -0800839 return written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700842static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800844static void firmware_rw_data(struct fw_priv *fw_priv, char *buffer,
Stephen Boyda098ecd2016-08-02 14:04:28 -0700845 loff_t offset, size_t count, bool read)
846{
847 if (read)
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800848 memcpy(buffer, fw_priv->data + offset, count);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700849 else
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800850 memcpy(fw_priv->data + offset, buffer, count);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700851}
852
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800853static void firmware_rw(struct fw_priv *fw_priv, char *buffer,
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700854 loff_t offset, size_t count, bool read)
855{
856 while (count) {
857 void *page_data;
858 int page_nr = offset >> PAGE_SHIFT;
859 int page_ofs = offset & (PAGE_SIZE-1);
860 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
861
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800862 page_data = kmap(fw_priv->pages[page_nr]);
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700863
864 if (read)
865 memcpy(buffer, page_data + page_ofs, page_cnt);
866 else
867 memcpy(page_data + page_ofs, buffer, page_cnt);
868
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800869 kunmap(fw_priv->pages[page_nr]);
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700870 buffer += page_cnt;
871 offset += page_cnt;
872 count -= page_cnt;
873 }
874}
875
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700876static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
877 struct bin_attribute *bin_attr,
878 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200880 struct device *dev = kobj_to_dev(kobj);
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800881 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800882 struct fw_priv *fw_priv;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700883 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Laura Garciacad1e552006-05-23 23:22:38 +0200885 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800886 fw_priv = fw_sysfs->fw_priv;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800887 if (!fw_priv || fw_state_is_done(fw_priv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 ret_count = -ENODEV;
889 goto out;
890 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800891 if (offset > fw_priv->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200892 ret_count = 0;
893 goto out;
894 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800895 if (count > fw_priv->size - offset)
896 count = fw_priv->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700897
898 ret_count = count;
899
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800900 if (fw_priv->data)
901 firmware_rw_data(fw_priv, buffer, offset, count, true);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700902 else
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800903 firmware_rw(fw_priv, buffer, offset, count, true);
David Woodhouse6e03a202009-04-09 22:04:07 -0700904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905out:
Laura Garciacad1e552006-05-23 23:22:38 +0200906 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return ret_count;
908}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800909
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800910static int fw_realloc_pages(struct fw_sysfs *fw_sysfs, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800912 struct fw_priv *fw_priv= fw_sysfs->fw_priv;
Fabian Fredericka76040d2014-07-01 21:13:30 +0200913 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
David Woodhouse6e03a202009-04-09 22:04:07 -0700915 /* If the array of pages is too small, grow it... */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800916 if (fw_priv->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700917 int new_array_size = max(pages_needed,
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800918 fw_priv->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700919 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Chen Feng10a3fbf2015-12-16 17:03:15 +0800921 new_pages = vmalloc(new_array_size * sizeof(void *));
David Woodhouse6e03a202009-04-09 22:04:07 -0700922 if (!new_pages) {
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800923 fw_load_abort(fw_sysfs);
David Woodhouse6e03a202009-04-09 22:04:07 -0700924 return -ENOMEM;
925 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800926 memcpy(new_pages, fw_priv->pages,
927 fw_priv->page_array_size * sizeof(void *));
928 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
929 (new_array_size - fw_priv->page_array_size));
930 vfree(fw_priv->pages);
931 fw_priv->pages = new_pages;
932 fw_priv->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700934
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800935 while (fw_priv->nr_pages < pages_needed) {
936 fw_priv->pages[fw_priv->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700937 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
938
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800939 if (!fw_priv->pages[fw_priv->nr_pages]) {
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800940 fw_load_abort(fw_sysfs);
David Woodhouse6e03a202009-04-09 22:04:07 -0700941 return -ENOMEM;
942 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800943 fw_priv->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return 0;
946}
947
948/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800949 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700950 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700951 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700952 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800953 * @buffer: buffer being written
954 * @offset: buffer offset for write in total data store area
955 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800957 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 * the driver as a firmware image.
959 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700960static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
961 struct bin_attribute *bin_attr,
962 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200964 struct device *dev = kobj_to_dev(kobj);
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800965 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800966 struct fw_priv *fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 ssize_t retval;
968
969 if (!capable(CAP_SYS_RAWIO))
970 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700971
Laura Garciacad1e552006-05-23 23:22:38 +0200972 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800973 fw_priv = fw_sysfs->fw_priv;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800974 if (!fw_priv || fw_state_is_done(fw_priv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 retval = -ENODEV;
976 goto out;
977 }
Ming Lei65710cb2012-08-04 12:01:16 +0800978
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800979 if (fw_priv->data) {
980 if (offset + count > fw_priv->allocated_size) {
Stephen Boyda098ecd2016-08-02 14:04:28 -0700981 retval = -ENOMEM;
982 goto out;
983 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800984 firmware_rw_data(fw_priv, buffer, offset, count, false);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700985 retval = count;
986 } else {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800987 retval = fw_realloc_pages(fw_sysfs, offset + count);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700988 if (retval)
989 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Stephen Boyda098ecd2016-08-02 14:04:28 -0700991 retval = count;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800992 firmware_rw(fw_priv, buffer, offset, count, false);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700993 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700994
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800995 fw_priv->size = max_t(size_t, offset + count, fw_priv->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996out:
Laura Garciacad1e552006-05-23 23:22:38 +0200997 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return retval;
999}
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001000
Dmitry Torokhov0983ca22010-06-04 00:54:37 -07001001static struct bin_attribute firmware_attr_data = {
1002 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 .size = 0,
1004 .read = firmware_data_read,
1005 .write = firmware_data_write,
1006};
1007
Takashi Iwai46239902015-02-04 15:18:07 +01001008static struct attribute *fw_dev_attrs[] = {
1009 &dev_attr_loading.attr,
1010 NULL
1011};
1012
1013static struct bin_attribute *fw_dev_bin_attrs[] = {
1014 &firmware_attr_data,
1015 NULL
1016};
1017
1018static const struct attribute_group fw_dev_attr_group = {
1019 .attrs = fw_dev_attrs,
1020 .bin_attrs = fw_dev_bin_attrs,
1021};
1022
1023static const struct attribute_group *fw_dev_attr_groups[] = {
1024 &fw_dev_attr_group,
1025 NULL
1026};
1027
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001028static struct fw_sysfs *
Stephen Boyddddb5542012-03-28 23:30:43 +02001029fw_create_instance(struct firmware *firmware, const char *fw_name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001030 struct device *device, unsigned int opt_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001032 struct fw_sysfs *fw_sysfs;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001033 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001035 fw_sysfs = kzalloc(sizeof(*fw_sysfs), GFP_KERNEL);
1036 if (!fw_sysfs) {
1037 fw_sysfs = ERR_PTR(-ENOMEM);
Ming Lei12446912012-08-04 12:01:20 +08001038 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001041 fw_sysfs->nowait = !!(opt_flags & FW_OPT_NOWAIT);
1042 fw_sysfs->fw = firmware;
1043 f_dev = &fw_sysfs->dev;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001044
1045 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +08001046 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -07001047 f_dev->parent = device;
1048 f_dev->class = &firmware_class;
Takashi Iwai46239902015-02-04 15:18:07 +01001049 f_dev->groups = fw_dev_attr_groups;
Ming Lei12446912012-08-04 12:01:20 +08001050exit:
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001051 return fw_sysfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001053
1054/* load a firmware via user helper */
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001055static int _request_firmware_load(struct fw_sysfs *fw_sysfs,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001056 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001057{
1058 int retval = 0;
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001059 struct device *f_dev = &fw_sysfs->dev;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001060 struct fw_priv *fw_priv = fw_sysfs->fw_priv;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001061
1062 /* fall back on userspace loading */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001063 if (!fw_priv->data)
1064 fw_priv->is_paged_buf = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001065
1066 dev_set_uevent_suppress(f_dev, true);
1067
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001068 retval = device_add(f_dev);
1069 if (retval) {
1070 dev_err(f_dev, "%s: device_register failed\n", __func__);
1071 goto err_put_dev;
1072 }
1073
Maxime Bizon1eeeef12013-08-29 20:28:13 +02001074 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001075 list_add(&fw_priv->pending_list, &pending_fw_head);
Maxime Bizon1eeeef12013-08-29 20:28:13 +02001076 mutex_unlock(&fw_lock);
1077
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001078 if (opt_flags & FW_OPT_UEVENT) {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001079 fw_priv->need_uevent = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001080 dev_set_uevent_suppress(f_dev, false);
Luis R. Rodriguez31034f22017-11-20 10:23:49 -08001081 dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_name);
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001082 kobject_uevent(&fw_sysfs->dev.kobj, KOBJ_ADD);
Ming Lei68ff2a02015-01-13 00:02:01 +08001083 } else {
1084 timeout = MAX_JIFFY_OFFSET;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001085 }
1086
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -08001087 retval = fw_state_wait_timeout(fw_priv, timeout);
Bjorn Andersson5d47ec02016-12-06 17:01:45 -08001088 if (retval < 0) {
Ming Lei0cb64242015-01-13 00:01:35 +08001089 mutex_lock(&fw_lock);
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001090 fw_load_abort(fw_sysfs);
Ming Lei0cb64242015-01-13 00:01:35 +08001091 mutex_unlock(&fw_lock);
1092 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001093
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -08001094 if (fw_state_is_aborted(fw_priv)) {
Luis R. Rodriguez76098b32017-07-20 13:13:39 -07001095 if (retval == -ERESTARTSYS)
1096 retval = -EINTR;
1097 else
1098 retval = -EAGAIN;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001099 } else if (fw_priv->is_paged_buf && !fw_priv->data)
zhang jun2b1278c2014-02-13 15:18:47 +08001100 retval = -ENOMEM;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001101
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001102 device_del(f_dev);
1103err_put_dev:
1104 put_device(f_dev);
1105 return retval;
1106}
1107
1108static int fw_load_from_user_helper(struct firmware *firmware,
1109 const char *name, struct device *device,
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001110 unsigned int opt_flags)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001111{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001112 struct fw_sysfs *fw_sysfs;
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001113 long timeout;
1114 int ret;
1115
1116 timeout = firmware_loading_timeout();
1117 if (opt_flags & FW_OPT_NOWAIT) {
1118 timeout = usermodehelper_read_lock_wait(timeout);
1119 if (!timeout) {
1120 dev_dbg(device, "firmware: %s loading timed out\n",
1121 name);
1122 return -EBUSY;
1123 }
1124 } else {
1125 ret = usermodehelper_read_trylock();
1126 if (WARN_ON(ret)) {
1127 dev_err(device, "firmware: %s will not be loaded\n",
1128 name);
1129 return ret;
1130 }
1131 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001132
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001133 fw_sysfs = fw_create_instance(firmware, name, device, opt_flags);
1134 if (IS_ERR(fw_sysfs)) {
1135 ret = PTR_ERR(fw_sysfs);
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001136 goto out_unlock;
1137 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001138
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001139 fw_sysfs->fw_priv = firmware->priv;
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001140 ret = _request_firmware_load(fw_sysfs, opt_flags, timeout);
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001141
1142 if (!ret)
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001143 ret = assign_fw(firmware, device, opt_flags);
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001144
1145out_unlock:
1146 usermodehelper_read_unlock();
1147
1148 return ret;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001149}
Ming Leiddf1f062013-06-04 10:01:13 +08001150
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001151#else /* CONFIG_FW_LOADER_USER_HELPER */
1152static inline int
1153fw_load_from_user_helper(struct firmware *firmware, const char *name,
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001154 struct device *device, unsigned int opt_flags)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001155{
1156 return -ENOENT;
1157}
Takashi Iwai807be032013-01-31 11:13:57 +01001158
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -07001159static inline void kill_pending_fw_fallback_reqs(bool only_kill_custom) { }
Ming Leiddf1f062013-06-04 10:01:13 +08001160
Luis R. Rodriguez6bb9cf32017-11-20 09:45:33 -08001161static inline int register_sysfs_loader(void)
1162{
1163 return 0;
1164}
1165
1166static inline void unregister_sysfs_loader(void)
1167{
1168}
1169
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001170#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001172/* prepare firmware and firmware_buf structs;
1173 * return 0 if a firmware is already assigned, 1 if need to load one,
1174 * or a negative error code
1175 */
1176static int
1177_request_firmware_prepare(struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001178 struct device *device, void *dbuf, size_t size)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001179{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 struct firmware *firmware;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001181 struct fw_priv *fw_priv;
Ming Lei1f2b7952012-08-04 12:01:21 +08001182 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Jiri Slaby4aed0642005-09-13 01:25:01 -07001184 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -07001186 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1187 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001188 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Stephen Boyda098ecd2016-08-02 14:04:28 -07001191 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
Luis R. Rodriguezed046302015-04-29 16:30:43 -07001192 dev_dbg(device, "using built-in %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001193 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +01001194 }
1195
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001196 ret = alloc_lookup_fw_priv(name, &fw_cache, &fw_priv, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +08001197
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001198 /*
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001199 * bind with 'priv' now to avoid warning in failure path
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001200 * of requesting firmware.
1201 */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001202 firmware->priv = fw_priv;
Ming Lei1f2b7952012-08-04 12:01:21 +08001203
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001204 if (ret > 0) {
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -08001205 ret = fw_state_wait(fw_priv);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001206 if (!ret) {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001207 fw_set_page_data(fw_priv, firmware);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001208 return 0; /* assigned */
1209 }
Stephen Boyddddb5542012-03-28 23:30:43 +02001210 }
Ming Lei1f2b7952012-08-04 12:01:21 +08001211
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001212 if (ret < 0)
1213 return ret;
1214 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001215}
1216
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001217/*
1218 * Batched requests need only one wake, we need to do this step last due to the
1219 * fallback mechanism. The buf is protected with kref_get(), and it won't be
1220 * released until the last user calls release_firmware().
1221 *
1222 * Failed batched requests are possible as well, in such cases we just share
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001223 * the struct fw_priv and won't release it until all requests are woken
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001224 * and have gone through this same path.
1225 */
1226static void fw_abort_batch_reqs(struct firmware *fw)
1227{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001228 struct fw_priv *fw_priv;
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001229
1230 /* Loaded directly? */
1231 if (!fw || !fw->priv)
1232 return;
1233
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001234 fw_priv = fw->priv;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -08001235 if (!fw_state_is_aborted(fw_priv))
1236 fw_state_aborted(fw_priv);
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001237}
1238
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001239/* called from request_firmware() and request_firmware_work_func() */
1240static int
1241_request_firmware(const struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001242 struct device *device, void *buf, size_t size,
1243 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001244{
Brian Norris715780a2015-12-09 14:50:28 -08001245 struct firmware *fw = NULL;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001246 int ret;
1247
1248 if (!firmware_p)
1249 return -EINVAL;
1250
Brian Norris715780a2015-12-09 14:50:28 -08001251 if (!name || name[0] == '\0') {
1252 ret = -EINVAL;
1253 goto out;
1254 }
Kees Cook471b0952014-09-18 11:25:37 -07001255
Stephen Boyda098ecd2016-08-02 14:04:28 -07001256 ret = _request_firmware_prepare(&fw, name, device, buf, size);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001257 if (ret <= 0) /* error or already assigned */
1258 goto out;
1259
Neil Horman3e358ac2013-09-06 15:36:08 -04001260 ret = fw_get_filesystem_firmware(device, fw->priv);
1261 if (ret) {
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001262 if (!(opt_flags & FW_OPT_NO_WARN))
Takashi Iwaibba3a872013-12-02 15:38:16 +01001263 dev_warn(device,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001264 "Direct firmware load for %s failed with error %d\n",
1265 name, ret);
1266 if (opt_flags & FW_OPT_USERHELPER) {
Takashi Iwaibba3a872013-12-02 15:38:16 +01001267 dev_warn(device, "Falling back to user helper\n");
1268 ret = fw_load_from_user_helper(fw, name, device,
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001269 opt_flags);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001270 }
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001271 } else
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001272 ret = assign_fw(fw, device, opt_flags);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001273
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001274 out:
1275 if (ret < 0) {
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001276 fw_abort_batch_reqs(fw);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001277 release_firmware(fw);
1278 fw = NULL;
1279 }
1280
1281 *firmware_p = fw;
1282 return ret;
1283}
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285/**
Kay Sievers312c0042005-11-16 09:00:00 +01001286 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001287 * @firmware_p: pointer to firmware image
1288 * @name: name of firmware file
1289 * @device: device for which firmware is being loaded
1290 *
1291 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001292 * of @name for device @device.
1293 *
1294 * Should be called from user context where sleeping is allowed.
1295 *
Kay Sievers312c0042005-11-16 09:00:00 +01001296 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001297 * should be distinctive enough not to be confused with any other
1298 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001299 *
1300 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001301 *
1302 * The function can be called safely inside device's suspend and
1303 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001304 **/
1305int
1306request_firmware(const struct firmware **firmware_p, const char *name,
Andrei Opreaea310032015-03-08 12:41:15 +02001307 struct device *device)
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001308{
Ming Leid6c8aa32013-06-06 20:01:48 +08001309 int ret;
1310
1311 /* Need to pin this module until return */
1312 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001313 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001314 FW_OPT_UEVENT | FW_OPT_FALLBACK);
Ming Leid6c8aa32013-06-06 20:01:48 +08001315 module_put(THIS_MODULE);
1316 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001317}
Daniel Mackf4945132013-05-23 22:17:18 +02001318EXPORT_SYMBOL(request_firmware);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001319
Takashi Iwaibba3a872013-12-02 15:38:16 +01001320/**
Borislav Petkov3c1556b2014-12-03 22:46:37 +01001321 * request_firmware_direct: - load firmware directly without usermode helper
Takashi Iwaibba3a872013-12-02 15:38:16 +01001322 * @firmware_p: pointer to firmware image
1323 * @name: name of firmware file
1324 * @device: device for which firmware is being loaded
1325 *
1326 * This function works pretty much like request_firmware(), but this doesn't
1327 * fall back to usermode helper even if the firmware couldn't be loaded
1328 * directly from fs. Hence it's useful for loading optional firmwares, which
1329 * aren't always present, without extra long timeouts of udev.
1330 **/
1331int request_firmware_direct(const struct firmware **firmware_p,
1332 const char *name, struct device *device)
1333{
1334 int ret;
Andrei Opreaea310032015-03-08 12:41:15 +02001335
Takashi Iwaibba3a872013-12-02 15:38:16 +01001336 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001337 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001338 FW_OPT_UEVENT | FW_OPT_NO_WARN);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001339 module_put(THIS_MODULE);
1340 return ret;
1341}
1342EXPORT_SYMBOL_GPL(request_firmware_direct);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001343
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001344/**
Stephen Boyda098ecd2016-08-02 14:04:28 -07001345 * request_firmware_into_buf - load firmware into a previously allocated buffer
1346 * @firmware_p: pointer to firmware image
1347 * @name: name of firmware file
1348 * @device: device for which firmware is being loaded and DMA region allocated
1349 * @buf: address of buffer to load firmware into
1350 * @size: size of buffer
1351 *
1352 * This function works pretty much like request_firmware(), but it doesn't
1353 * allocate a buffer to hold the firmware data. Instead, the firmware
1354 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
1355 * data member is pointed at @buf.
1356 *
1357 * This function doesn't cache firmware either.
1358 */
1359int
1360request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
1361 struct device *device, void *buf, size_t size)
1362{
1363 int ret;
1364
1365 __module_get(THIS_MODULE);
1366 ret = _request_firmware(firmware_p, name, device, buf, size,
1367 FW_OPT_UEVENT | FW_OPT_FALLBACK |
1368 FW_OPT_NOCACHE);
1369 module_put(THIS_MODULE);
1370 return ret;
1371}
1372EXPORT_SYMBOL(request_firmware_into_buf);
1373
1374/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001376 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001378void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
1380 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001381 if (!fw_is_builtin_firmware(fw))
1382 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 kfree(fw);
1384 }
1385}
Daniel Mackf4945132013-05-23 22:17:18 +02001386EXPORT_SYMBOL(release_firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388/* Async support */
1389struct firmware_work {
1390 struct work_struct work;
1391 struct module *module;
1392 const char *name;
1393 struct device *device;
1394 void *context;
1395 void (*cont)(const struct firmware *fw, void *context);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001396 unsigned int opt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397};
1398
Stephen Boyda36cf842012-03-28 23:31:00 +02001399static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Stephen Boyda36cf842012-03-28 23:31:00 +02001401 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001403
Stephen Boyda36cf842012-03-28 23:31:00 +02001404 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001405
Stephen Boyda098ecd2016-08-02 14:04:28 -07001406 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001407 fw_work->opt_flags);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001408 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001409 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 module_put(fw_work->module);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001412 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
1416/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001417 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001418 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001419 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001420 * is non-zero else the firmware copy must be done manually.
1421 * @name: name of firmware file
1422 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001423 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001424 * @context: will be passed over to @cont, and
1425 * @fw may be %NULL if firmware request fails.
1426 * @cont: function will be called asynchronously when the firmware
1427 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001429 * Caller must hold the reference count of @device.
1430 *
Ming Lei6f21a622012-08-04 12:01:24 +08001431 * Asynchronous variant of request_firmware() for user contexts:
1432 * - sleep for as small periods as possible since it may
Silvio Fricke88bcef52016-11-25 15:59:47 +01001433 * increase kernel boot time of built-in device drivers
1434 * requesting firmware in their ->probe() methods, if
1435 * @gfp is GFP_KERNEL.
Ming Lei6f21a622012-08-04 12:01:24 +08001436 *
1437 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 **/
1439int
1440request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001441 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001442 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 void (*cont)(const struct firmware *fw, void *context))
1444{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001445 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Andrei Opreaea310032015-03-08 12:41:15 +02001447 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (!fw_work)
1449 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001450
1451 fw_work->module = module;
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001452 fw_work->name = kstrdup_const(name, gfp);
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001453 if (!fw_work->name) {
1454 kfree(fw_work);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001455 return -ENOMEM;
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001456 }
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001457 fw_work->device = device;
1458 fw_work->context = context;
1459 fw_work->cont = cont;
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001460 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
Takashi Iwai5a1379e2014-06-04 17:48:15 +02001461 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (!try_module_get(module)) {
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001464 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 kfree(fw_work);
1466 return -EFAULT;
1467 }
1468
Ming Lei0cfc1e12012-08-04 12:01:23 +08001469 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001470 INIT_WORK(&fw_work->work, request_firmware_work_func);
1471 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return 0;
1473}
Daniel Mackf4945132013-05-23 22:17:18 +02001474EXPORT_SYMBOL(request_firmware_nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Ming Lei90f890812013-06-20 12:30:16 +08001476#ifdef CONFIG_PM_SLEEP
1477static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1478
Ming Lei2887b392012-08-04 12:01:22 +08001479/**
1480 * cache_firmware - cache one firmware image in kernel memory space
1481 * @fw_name: the firmware image name
1482 *
1483 * Cache firmware in kernel memory so that drivers can use it when
1484 * system isn't ready for them to request firmware image from userspace.
1485 * Once it returns successfully, driver can use request_firmware or its
1486 * nowait version to get the cached firmware without any interacting
1487 * with userspace
1488 *
1489 * Return 0 if the firmware image has been cached successfully
1490 * Return !0 otherwise
1491 *
1492 */
Ming Lei93232e42013-06-06 20:01:47 +08001493static int cache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001494{
1495 int ret;
1496 const struct firmware *fw;
1497
1498 pr_debug("%s: %s\n", __func__, fw_name);
1499
1500 ret = request_firmware(&fw, fw_name, NULL);
1501 if (!ret)
1502 kfree(fw);
1503
1504 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1505
1506 return ret;
1507}
1508
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001509static struct fw_priv *lookup_fw_priv(const char *fw_name)
Ming Lei6a2c1232013-06-26 09:28:17 +08001510{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001511 struct fw_priv *tmp;
Ming Lei6a2c1232013-06-26 09:28:17 +08001512 struct firmware_cache *fwc = &fw_cache;
1513
1514 spin_lock(&fwc->lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001515 tmp = __lookup_fw_priv(fw_name);
Ming Lei6a2c1232013-06-26 09:28:17 +08001516 spin_unlock(&fwc->lock);
1517
1518 return tmp;
1519}
1520
Ming Lei2887b392012-08-04 12:01:22 +08001521/**
1522 * uncache_firmware - remove one cached firmware image
1523 * @fw_name: the firmware image name
1524 *
1525 * Uncache one firmware image which has been cached successfully
1526 * before.
1527 *
1528 * Return 0 if the firmware cache has been removed successfully
1529 * Return !0 otherwise
1530 *
1531 */
Ming Lei93232e42013-06-06 20:01:47 +08001532static int uncache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001533{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001534 struct fw_priv *fw_priv;
Ming Lei2887b392012-08-04 12:01:22 +08001535 struct firmware fw;
1536
1537 pr_debug("%s: %s\n", __func__, fw_name);
1538
Stephen Boyda098ecd2016-08-02 14:04:28 -07001539 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
Ming Lei2887b392012-08-04 12:01:22 +08001540 return 0;
1541
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001542 fw_priv = lookup_fw_priv(fw_name);
1543 if (fw_priv) {
1544 free_fw_priv(fw_priv);
Ming Lei2887b392012-08-04 12:01:22 +08001545 return 0;
1546 }
1547
1548 return -EINVAL;
1549}
1550
Ming Lei37276a52012-08-04 12:01:27 +08001551static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1552{
1553 struct fw_cache_entry *fce;
1554
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001555 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
Ming Lei37276a52012-08-04 12:01:27 +08001556 if (!fce)
1557 goto exit;
1558
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001559 fce->name = kstrdup_const(name, GFP_ATOMIC);
1560 if (!fce->name) {
1561 kfree(fce);
1562 fce = NULL;
1563 goto exit;
1564 }
Ming Lei37276a52012-08-04 12:01:27 +08001565exit:
1566 return fce;
1567}
1568
Ming Lei373304f2012-10-09 12:01:01 +08001569static int __fw_entry_found(const char *name)
1570{
1571 struct firmware_cache *fwc = &fw_cache;
1572 struct fw_cache_entry *fce;
1573
1574 list_for_each_entry(fce, &fwc->fw_names, list) {
1575 if (!strcmp(fce->name, name))
1576 return 1;
1577 }
1578 return 0;
1579}
1580
Ming Leiac39b3e2012-08-20 19:04:16 +08001581static int fw_cache_piggyback_on_request(const char *name)
1582{
1583 struct firmware_cache *fwc = &fw_cache;
1584 struct fw_cache_entry *fce;
1585 int ret = 0;
1586
1587 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001588 if (__fw_entry_found(name))
1589 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001590
1591 fce = alloc_fw_cache_entry(name);
1592 if (fce) {
1593 ret = 1;
1594 list_add(&fce->list, &fwc->fw_names);
1595 pr_debug("%s: fw: %s\n", __func__, name);
1596 }
1597found:
1598 spin_unlock(&fwc->name_lock);
1599 return ret;
1600}
1601
Ming Lei37276a52012-08-04 12:01:27 +08001602static void free_fw_cache_entry(struct fw_cache_entry *fce)
1603{
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001604 kfree_const(fce->name);
Ming Lei37276a52012-08-04 12:01:27 +08001605 kfree(fce);
1606}
1607
1608static void __async_dev_cache_fw_image(void *fw_entry,
1609 async_cookie_t cookie)
1610{
1611 struct fw_cache_entry *fce = fw_entry;
1612 struct firmware_cache *fwc = &fw_cache;
1613 int ret;
1614
1615 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001616 if (ret) {
1617 spin_lock(&fwc->name_lock);
1618 list_del(&fce->list);
1619 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001620
Ming Leiac39b3e2012-08-20 19:04:16 +08001621 free_fw_cache_entry(fce);
1622 }
Ming Lei37276a52012-08-04 12:01:27 +08001623}
1624
1625/* called with dev->devres_lock held */
1626static void dev_create_fw_entry(struct device *dev, void *res,
1627 void *data)
1628{
1629 struct fw_name_devm *fwn = res;
1630 const char *fw_name = fwn->name;
1631 struct list_head *head = data;
1632 struct fw_cache_entry *fce;
1633
1634 fce = alloc_fw_cache_entry(fw_name);
1635 if (fce)
1636 list_add(&fce->list, head);
1637}
1638
1639static int devm_name_match(struct device *dev, void *res,
1640 void *match_data)
1641{
1642 struct fw_name_devm *fwn = res;
1643 return (fwn->magic == (unsigned long)match_data);
1644}
1645
Ming Leiab6dd8e2012-08-17 22:07:00 +08001646static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001647{
1648 LIST_HEAD(todo);
1649 struct fw_cache_entry *fce;
1650 struct fw_cache_entry *fce_next;
1651 struct firmware_cache *fwc = &fw_cache;
1652
1653 devres_for_each_res(dev, fw_name_devm_release,
1654 devm_name_match, &fw_cache,
1655 dev_create_fw_entry, &todo);
1656
1657 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1658 list_del(&fce->list);
1659
1660 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001661 /* only one cache entry for one firmware */
1662 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001663 list_add(&fce->list, &fwc->fw_names);
1664 } else {
1665 free_fw_cache_entry(fce);
1666 fce = NULL;
1667 }
Ming Lei37276a52012-08-04 12:01:27 +08001668 spin_unlock(&fwc->name_lock);
1669
Ming Lei373304f2012-10-09 12:01:01 +08001670 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001671 async_schedule_domain(__async_dev_cache_fw_image,
1672 (void *)fce,
1673 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001674 }
1675}
1676
1677static void __device_uncache_fw_images(void)
1678{
1679 struct firmware_cache *fwc = &fw_cache;
1680 struct fw_cache_entry *fce;
1681
1682 spin_lock(&fwc->name_lock);
1683 while (!list_empty(&fwc->fw_names)) {
1684 fce = list_entry(fwc->fw_names.next,
1685 struct fw_cache_entry, list);
1686 list_del(&fce->list);
1687 spin_unlock(&fwc->name_lock);
1688
1689 uncache_firmware(fce->name);
1690 free_fw_cache_entry(fce);
1691
1692 spin_lock(&fwc->name_lock);
1693 }
1694 spin_unlock(&fwc->name_lock);
1695}
1696
1697/**
1698 * device_cache_fw_images - cache devices' firmware
1699 *
1700 * If one device called request_firmware or its nowait version
1701 * successfully before, the firmware names are recored into the
1702 * device's devres link list, so device_cache_fw_images can call
1703 * cache_firmware() to cache these firmwares for the device,
1704 * then the device driver can load its firmwares easily at
1705 * time when system is not ready to complete loading firmware.
1706 */
1707static void device_cache_fw_images(void)
1708{
1709 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001710 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001711 DEFINE_WAIT(wait);
1712
1713 pr_debug("%s\n", __func__);
1714
Ming Lei373304f2012-10-09 12:01:01 +08001715 /* cancel uncache work */
1716 cancel_delayed_work_sync(&fwc->work);
1717
Ming Leiffe53f62012-08-04 12:01:28 +08001718 /*
1719 * use small loading timeout for caching devices' firmware
1720 * because all these firmware images have been loaded
1721 * successfully at lease once, also system is ready for
1722 * completing firmware loading now. The maximum size of
1723 * firmware in current distributions is about 2M bytes,
1724 * so 10 secs should be enough.
1725 */
1726 old_timeout = loading_timeout;
1727 loading_timeout = 10;
1728
Ming Leiac39b3e2012-08-20 19:04:16 +08001729 mutex_lock(&fw_lock);
1730 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001731 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001732 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001733
1734 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001735 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001736
1737 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001738}
1739
1740/**
1741 * device_uncache_fw_images - uncache devices' firmware
1742 *
1743 * uncache all firmwares which have been cached successfully
1744 * by device_uncache_fw_images earlier
1745 */
1746static void device_uncache_fw_images(void)
1747{
1748 pr_debug("%s\n", __func__);
1749 __device_uncache_fw_images();
1750}
1751
1752static void device_uncache_fw_images_work(struct work_struct *work)
1753{
1754 device_uncache_fw_images();
1755}
1756
1757/**
1758 * device_uncache_fw_images_delay - uncache devices firmwares
1759 * @delay: number of milliseconds to delay uncache device firmwares
1760 *
1761 * uncache all devices's firmwares which has been cached successfully
1762 * by device_cache_fw_images after @delay milliseconds.
1763 */
1764static void device_uncache_fw_images_delay(unsigned long delay)
1765{
Shaibal Duttabce66182014-01-31 15:44:58 -08001766 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1767 msecs_to_jiffies(delay));
Ming Lei37276a52012-08-04 12:01:27 +08001768}
1769
Ming Lei07646d92012-08-04 12:01:29 +08001770static int fw_pm_notify(struct notifier_block *notify_block,
1771 unsigned long mode, void *unused)
1772{
1773 switch (mode) {
1774 case PM_HIBERNATION_PREPARE:
1775 case PM_SUSPEND_PREPARE:
Sebastian Capellaf8d5b9e2014-02-18 17:52:08 -08001776 case PM_RESTORE_PREPARE:
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -07001777 /*
1778 * kill pending fallback requests with a custom fallback
1779 * to avoid stalling suspend.
1780 */
1781 kill_pending_fw_fallback_reqs(true);
Ming Lei07646d92012-08-04 12:01:29 +08001782 device_cache_fw_images();
1783 break;
1784
1785 case PM_POST_SUSPEND:
1786 case PM_POST_HIBERNATION:
1787 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001788 /*
1789 * In case that system sleep failed and syscore_suspend is
1790 * not called.
1791 */
1792 mutex_lock(&fw_lock);
1793 fw_cache.state = FW_LOADER_NO_CACHE;
1794 mutex_unlock(&fw_lock);
1795
Ming Lei07646d92012-08-04 12:01:29 +08001796 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1797 break;
1798 }
1799
1800 return 0;
1801}
Ming Lei07646d92012-08-04 12:01:29 +08001802
Ming Leiac39b3e2012-08-20 19:04:16 +08001803/* stop caching firmware once syscore_suspend is reached */
1804static int fw_suspend(void)
1805{
1806 fw_cache.state = FW_LOADER_NO_CACHE;
1807 return 0;
1808}
1809
1810static struct syscore_ops fw_syscore_ops = {
1811 .suspend = fw_suspend,
1812};
Luis R. Rodrigueza67e5032017-11-20 09:45:31 -08001813
Luis R. Rodriguez59b6d852017-11-20 09:45:32 -08001814static int __init register_fw_pm_ops(void)
1815{
1816 int ret;
1817
1818 spin_lock_init(&fw_cache.name_lock);
1819 INIT_LIST_HEAD(&fw_cache.fw_names);
1820
1821 INIT_DELAYED_WORK(&fw_cache.work,
1822 device_uncache_fw_images_work);
1823
1824 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1825 ret = register_pm_notifier(&fw_cache.pm_notify);
1826 if (ret)
1827 return ret;
1828
1829 register_syscore_ops(&fw_syscore_ops);
1830
1831 return ret;
1832}
1833
Luis R. Rodrigueza67e5032017-11-20 09:45:31 -08001834static inline void unregister_fw_pm_ops(void)
1835{
1836 unregister_syscore_ops(&fw_syscore_ops);
1837 unregister_pm_notifier(&fw_cache.pm_notify);
1838}
Ming Leicfe016b2012-09-08 17:32:30 +08001839#else
1840static int fw_cache_piggyback_on_request(const char *name)
1841{
1842 return 0;
1843}
Luis R. Rodriguez59b6d852017-11-20 09:45:32 -08001844static inline int register_fw_pm_ops(void)
1845{
1846 return 0;
1847}
Luis R. Rodrigueza67e5032017-11-20 09:45:31 -08001848static inline void unregister_fw_pm_ops(void)
1849{
1850}
Ming Leicfe016b2012-09-08 17:32:30 +08001851#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001852
Ming Lei37276a52012-08-04 12:01:27 +08001853static void __init fw_cache_init(void)
1854{
1855 spin_lock_init(&fw_cache.lock);
1856 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001857 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001858}
1859
Luis R. Rodrigueza669f042017-05-02 01:31:04 -07001860static int fw_shutdown_notify(struct notifier_block *unused1,
1861 unsigned long unused2, void *unused3)
1862{
1863 /*
1864 * Kill all pending fallback requests to avoid both stalling shutdown,
1865 * and avoid a deadlock with the usermode_lock.
1866 */
1867 kill_pending_fw_fallback_reqs(false);
1868
1869 return NOTIFY_DONE;
1870}
1871
1872static struct notifier_block fw_shutdown_nb = {
1873 .notifier_call = fw_shutdown_notify,
1874};
1875
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001876static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Luis R. Rodriguez59b6d852017-11-20 09:45:32 -08001878 int ret;
1879
1880 /* No need to unfold these on exit */
Ming Lei1f2b7952012-08-04 12:01:21 +08001881 fw_cache_init();
Luis R. Rodriguez59b6d852017-11-20 09:45:32 -08001882
1883 ret = register_fw_pm_ops();
1884 if (ret)
1885 return ret;
1886
Luis R. Rodriguez561a10b2017-11-20 09:45:34 -08001887 ret = register_reboot_notifier(&fw_shutdown_nb);
1888 if (ret)
1889 goto out;
1890
Luis R. Rodriguez6bb9cf32017-11-20 09:45:33 -08001891 return register_sysfs_loader();
Luis R. Rodriguez561a10b2017-11-20 09:45:34 -08001892
1893out:
1894 unregister_fw_pm_ops();
1895 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001897
1898static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Luis R. Rodrigueza67e5032017-11-20 09:45:31 -08001900 unregister_fw_pm_ops();
Takashi Iwaife304142013-05-22 18:28:38 +02001901 unregister_reboot_notifier(&fw_shutdown_nb);
Luis R. Rodriguez6bb9cf32017-11-20 09:45:33 -08001902 unregister_sysfs_loader();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
1904
Shaohua Lia30a6a22006-09-27 01:50:52 -07001905fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906module_exit(firmware_class_exit);