blob: 2d819875348d7e91a468406caa6467e71d03a803 [file] [log] [blame]
Greg Kroah-Hartman989d42e2017-11-07 17:30:07 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * firmware_class.c - Multi purpose firmware loading support
4 *
Markus Rechberger87d37a42007-06-04 18:45:44 +02005 * Copyright (c) 2003 Manuel Estrada Sainz
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Please see Documentation/firmware_class/ for more information.
8 *
9 */
10
Luis R. Rodriguez73da4b42017-07-20 13:13:40 -070011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Randy.Dunlapc59ede72006-01-11 12:17:46 -080013#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/device.h>
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/timer.h>
18#include <linux/vmalloc.h>
19#include <linux/interrupt.h>
20#include <linux/bitops.h>
Laura Garciacad1e552006-05-23 23:22:38 +020021#include <linux/mutex.h>
Stephen Boyda36cf842012-03-28 23:31:00 +020022#include <linux/workqueue.h>
David Woodhouse6e03a202009-04-09 22:04:07 -070023#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/firmware.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Stephen Boyda36cf842012-03-28 23:31:00 +020026#include <linux/sched.h>
Linus Torvaldsabb139e2012-10-03 15:58:32 -070027#include <linux/file.h>
Ming Lei1f2b7952012-08-04 12:01:21 +080028#include <linux/list.h>
Mimi Zohare40ba6d2015-11-19 12:39:22 -050029#include <linux/fs.h>
Ming Lei37276a52012-08-04 12:01:27 +080030#include <linux/async.h>
31#include <linux/pm.h>
Ming Lei07646d92012-08-04 12:01:29 +080032#include <linux/suspend.h>
Ming Leiac39b3e2012-08-20 19:04:16 +080033#include <linux/syscore_ops.h>
Takashi Iwaife304142013-05-22 18:28:38 +020034#include <linux/reboot.h>
Kees Cook6593d922014-02-25 13:06:00 -080035#include <linux/security.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
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010045enum fw_status {
46 FW_STATUS_UNKNOWN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 FW_STATUS_LOADING,
48 FW_STATUS_DONE,
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010049 FW_STATUS_ABORTED,
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010052/*
53 * Concurrent request_firmware() for the same firmware need to be
54 * serialized. struct fw_state is simple state machine which hold the
55 * state of the firmware loading.
56 */
57struct fw_state {
Luis R. Rodrigueze44565f2017-07-20 13:13:09 -070058 struct completion completion;
Daniel Wagner0430caf2016-11-17 11:00:49 +010059 enum fw_status status;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010060};
61
Takashi Iwai14c4bae2013-12-02 15:38:18 +010062/* firmware behavior options */
63#define FW_OPT_UEVENT (1U << 0)
64#define FW_OPT_NOWAIT (1U << 1)
Takashi Iwai5a1379e2014-06-04 17:48:15 +020065#define FW_OPT_USERHELPER (1U << 2)
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -070066#define FW_OPT_NO_WARN (1U << 3)
Vikram Mulukutla0e742e92016-08-02 14:04:25 -070067#define FW_OPT_NOCACHE (1U << 4)
Luis R. Rodriguez3f722712017-11-20 10:23:58 -080068#define FW_OPT_NOFALLBACK (1U << 5)
Takashi Iwai14c4bae2013-12-02 15:38:18 +010069
Ming Lei1f2b7952012-08-04 12:01:21 +080070struct firmware_cache {
71 /* firmware_buf instance will be added into the below list */
72 spinlock_t lock;
73 struct list_head head;
Ming Leicfe016b2012-09-08 17:32:30 +080074 int state;
Ming Lei37276a52012-08-04 12:01:27 +080075
Ming Leicfe016b2012-09-08 17:32:30 +080076#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +080077 /*
78 * Names of firmware images which have been cached successfully
79 * will be added into the below list so that device uncache
80 * helper can trace which firmware images have been cached
81 * before.
82 */
83 spinlock_t name_lock;
84 struct list_head fw_names;
85
Ming Lei37276a52012-08-04 12:01:27 +080086 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +080087
88 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +080089#endif
Ming Lei1f2b7952012-08-04 12:01:21 +080090};
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -080092struct fw_priv {
Ming Lei1f2b7952012-08-04 12:01:21 +080093 struct kref ref;
94 struct list_head list;
Ming Lei1f2b7952012-08-04 12:01:21 +080095 struct firmware_cache *fwc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010096 struct fw_state fw_st;
Ming Lei65710cb2012-08-04 12:01:16 +080097 void *data;
98 size_t size;
Stephen Boyda098ecd2016-08-02 14:04:28 -070099 size_t allocated_size;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100100#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100101 bool is_paged_buf;
Ming Leiaf5bc112013-05-27 18:30:04 +0800102 bool need_uevent;
David Woodhouse6e03a202009-04-09 22:04:07 -0700103 struct page **pages;
104 int nr_pages;
105 int page_array_size;
Takashi Iwaife304142013-05-22 18:28:38 +0200106 struct list_head pending_list;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100107#endif
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800108 const char *fw_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109};
110
Ming Lei37276a52012-08-04 12:01:27 +0800111struct fw_cache_entry {
112 struct list_head list;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700113 const char *name;
Ming Lei37276a52012-08-04 12:01:27 +0800114};
115
Ming Leif531f05a2012-08-04 12:01:25 +0800116struct fw_name_devm {
117 unsigned long magic;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700118 const char *name;
Ming Leif531f05a2012-08-04 12:01:25 +0800119};
120
Luis R. Rodriguez942e7432017-11-20 10:23:55 -0800121static inline struct fw_priv *to_fw_priv(struct kref *ref)
122{
123 return container_of(ref, struct fw_priv, ref);
124}
Ming Lei1f2b7952012-08-04 12:01:21 +0800125
Ming Leiac39b3e2012-08-20 19:04:16 +0800126#define FW_LOADER_NO_CACHE 0
127#define FW_LOADER_START_CACHE 1
128
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800129/* fw_lock could be moved to 'struct fw_sysfs' but since it is just
Ming Lei1f2b7952012-08-04 12:01:21 +0800130 * guarding for corner cases a global lock should be OK */
131static DEFINE_MUTEX(fw_lock);
132
133static struct firmware_cache fw_cache;
134
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800135/* Builtin firmware support */
136
137#ifdef CONFIG_FW_LOADER
138
139extern struct builtin_fw __start_builtin_fw[];
140extern struct builtin_fw __end_builtin_fw[];
141
Luis R. Rodriguez5711ae62017-11-20 10:23:56 -0800142static void fw_copy_to_prealloc_buf(struct firmware *fw,
143 void *buf, size_t size)
144{
145 if (!buf || size < fw->size)
146 return;
147 memcpy(buf, fw->data, fw->size);
148}
149
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800150static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
151 void *buf, size_t size)
152{
153 struct builtin_fw *b_fw;
154
155 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
156 if (strcmp(name, b_fw->name) == 0) {
157 fw->size = b_fw->size;
158 fw->data = b_fw->data;
Luis R. Rodriguez5711ae62017-11-20 10:23:56 -0800159 fw_copy_to_prealloc_buf(fw, buf, size);
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800160
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800161 return true;
162 }
163 }
164
165 return false;
166}
167
168static bool fw_is_builtin_firmware(const struct firmware *fw)
169{
170 struct builtin_fw *b_fw;
171
172 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
173 if (fw->data == b_fw->data)
174 return true;
175
176 return false;
177}
178
179#else /* Module case - no builtin firmware support */
180
181static inline bool fw_get_builtin_firmware(struct firmware *fw,
182 const char *name, void *buf,
183 size_t size)
184{
185 return false;
186}
187
188static inline bool fw_is_builtin_firmware(const struct firmware *fw)
189{
190 return false;
191}
192#endif
193
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800194static void fw_state_init(struct fw_priv *fw_priv)
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800195{
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800196 struct fw_state *fw_st = &fw_priv->fw_st;
197
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800198 init_completion(&fw_st->completion);
199 fw_st->status = FW_STATUS_UNKNOWN;
200}
201
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800202static int __fw_state_wait_common(struct fw_priv *fw_priv, long timeout)
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800203{
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800204 struct fw_state *fw_st = &fw_priv->fw_st;
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800205 long ret;
206
207 ret = wait_for_completion_killable_timeout(&fw_st->completion, timeout);
208 if (ret != 0 && fw_st->status == FW_STATUS_ABORTED)
209 return -ENOENT;
210 if (!ret)
211 return -ETIMEDOUT;
212
213 return ret < 0 ? ret : 0;
214}
215
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800216static void __fw_state_set(struct fw_priv *fw_priv,
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800217 enum fw_status status)
218{
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800219 struct fw_state *fw_st = &fw_priv->fw_st;
220
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800221 WRITE_ONCE(fw_st->status, status);
222
223 if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED)
224 complete_all(&fw_st->completion);
225}
226
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800227static inline void fw_state_start(struct fw_priv *fw_priv)
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800228{
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800229 __fw_state_set(fw_priv, FW_STATUS_LOADING);
230}
231
232static inline void fw_state_done(struct fw_priv *fw_priv)
233{
234 __fw_state_set(fw_priv, FW_STATUS_DONE);
235}
236
237static inline void fw_state_aborted(struct fw_priv *fw_priv)
238{
239 __fw_state_set(fw_priv, FW_STATUS_ABORTED);
240}
241
242static inline int fw_state_wait(struct fw_priv *fw_priv)
243{
244 return __fw_state_wait_common(fw_priv, MAX_SCHEDULE_TIMEOUT);
245}
246
247static bool __fw_state_check(struct fw_priv *fw_priv,
248 enum fw_status status)
249{
250 struct fw_state *fw_st = &fw_priv->fw_st;
251
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800252 return fw_st->status == status;
253}
254
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800255static inline bool fw_state_is_aborted(struct fw_priv *fw_priv)
256{
257 return __fw_state_check(fw_priv, FW_STATUS_ABORTED);
258}
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800259
260#ifdef CONFIG_FW_LOADER_USER_HELPER
261
Luis R. Rodriguezb2e9a852018-03-10 06:14:46 -0800262/**
263 * struct firmware_fallback_config - firmware fallback configuratioon settings
264 *
265 * Helps describe and fine tune the fallback mechanism.
266 *
267 * @force_sysfs_fallback: force the sysfs fallback mechanism to be used
268 * as if one had enabled CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y.
269 */
270struct firmware_fallback_config {
271 bool force_sysfs_fallback;
272};
273
274static const struct firmware_fallback_config fw_fallback_config = {
275 .force_sysfs_fallback = IS_ENABLED(CONFIG_FW_LOADER_USER_HELPER_FALLBACK),
276};
277
Luis R. Rodriguez5d9566b2018-03-10 06:14:47 -0800278static int old_timeout;
279static int loading_timeout = 60; /* In seconds */
280
281static inline long firmware_loading_timeout(void)
282{
283 return loading_timeout > 0 ? loading_timeout * HZ : MAX_JIFFY_OFFSET;
284}
285
286/*
287 * use small loading timeout for caching devices' firmware because all these
288 * firmware images have been loaded successfully at lease once, also system is
289 * ready for completing firmware loading now. The maximum size of firmware in
290 * current distributions is about 2M bytes, so 10 secs should be enough.
291 */
292static void fw_fallback_set_cache_timeout(void)
293{
294 old_timeout = loading_timeout;
295 loading_timeout = 10;
296}
297
298/* Restores the timeout to the value last configured during normal operation */
299static void fw_fallback_set_default_timeout(void)
300{
301 loading_timeout = old_timeout;
302}
303
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800304static inline bool fw_sysfs_done(struct fw_priv *fw_priv)
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800305{
306 return __fw_state_check(fw_priv, FW_STATUS_DONE);
307}
308
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800309static inline bool fw_sysfs_loading(struct fw_priv *fw_priv)
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800310{
311 return __fw_state_check(fw_priv, FW_STATUS_LOADING);
312}
313
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800314static inline int fw_sysfs_wait_timeout(struct fw_priv *fw_priv, long timeout)
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800315{
316 return __fw_state_wait_common(fw_priv, timeout);
317}
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800318
319#endif /* CONFIG_FW_LOADER_USER_HELPER */
320
321static int fw_cache_piggyback_on_request(const char *name);
322
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800323static struct fw_priv *__allocate_fw_priv(const char *fw_name,
324 struct firmware_cache *fwc,
325 void *dbuf, size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800326{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800327 struct fw_priv *fw_priv;
Ming Lei1f2b7952012-08-04 12:01:21 +0800328
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800329 fw_priv = kzalloc(sizeof(*fw_priv), GFP_ATOMIC);
330 if (!fw_priv)
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700331 return NULL;
332
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800333 fw_priv->fw_name = kstrdup_const(fw_name, GFP_ATOMIC);
334 if (!fw_priv->fw_name) {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800335 kfree(fw_priv);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700336 return NULL;
337 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800338
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800339 kref_init(&fw_priv->ref);
340 fw_priv->fwc = fwc;
341 fw_priv->data = dbuf;
342 fw_priv->allocated_size = size;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800343 fw_state_init(fw_priv);
Takashi Iwaife304142013-05-22 18:28:38 +0200344#ifdef CONFIG_FW_LOADER_USER_HELPER
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800345 INIT_LIST_HEAD(&fw_priv->pending_list);
Takashi Iwaife304142013-05-22 18:28:38 +0200346#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800347
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800348 pr_debug("%s: fw-%s fw_priv=%p\n", __func__, fw_name, fw_priv);
Ming Lei1f2b7952012-08-04 12:01:21 +0800349
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800350 return fw_priv;
Ming Lei1f2b7952012-08-04 12:01:21 +0800351}
352
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800353static struct fw_priv *__lookup_fw_priv(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +0800354{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800355 struct fw_priv *tmp;
Ming Lei2887b392012-08-04 12:01:22 +0800356 struct firmware_cache *fwc = &fw_cache;
357
358 list_for_each_entry(tmp, &fwc->head, list)
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800359 if (!strcmp(tmp->fw_name, fw_name))
Ming Lei2887b392012-08-04 12:01:22 +0800360 return tmp;
361 return NULL;
362}
363
Luis R. Rodriguez30172be2017-07-20 13:13:41 -0700364/* Returns 1 for batching firmware requests with the same name */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800365static int alloc_lookup_fw_priv(const char *fw_name,
366 struct firmware_cache *fwc,
367 struct fw_priv **fw_priv, void *dbuf,
368 size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800369{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800370 struct fw_priv *tmp;
Ming Lei1f2b7952012-08-04 12:01:21 +0800371
372 spin_lock(&fwc->lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800373 tmp = __lookup_fw_priv(fw_name);
Ming Lei2887b392012-08-04 12:01:22 +0800374 if (tmp) {
375 kref_get(&tmp->ref);
376 spin_unlock(&fwc->lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800377 *fw_priv = tmp;
378 pr_debug("batched request - sharing the same struct fw_priv and lookup for multiple requests\n");
Ming Lei2887b392012-08-04 12:01:22 +0800379 return 1;
380 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800381 tmp = __allocate_fw_priv(fw_name, fwc, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +0800382 if (tmp)
383 list_add(&tmp->list, &fwc->head);
384 spin_unlock(&fwc->lock);
385
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800386 *fw_priv = tmp;
Ming Lei1f2b7952012-08-04 12:01:21 +0800387
388 return tmp ? 0 : -ENOMEM;
389}
390
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800391static void __free_fw_priv(struct kref *ref)
Bart Van Assche98233b22014-01-04 14:20:36 +0100392 __releases(&fwc->lock)
Ming Lei1f2b7952012-08-04 12:01:21 +0800393{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800394 struct fw_priv *fw_priv = to_fw_priv(ref);
395 struct firmware_cache *fwc = fw_priv->fwc;
Ming Lei1f2b7952012-08-04 12:01:21 +0800396
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800397 pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800398 __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800399 (unsigned int)fw_priv->size);
Ming Lei1f2b7952012-08-04 12:01:21 +0800400
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800401 list_del(&fw_priv->list);
Ming Lei1f2b7952012-08-04 12:01:21 +0800402 spin_unlock(&fwc->lock);
403
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100404#ifdef CONFIG_FW_LOADER_USER_HELPER
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800405 if (fw_priv->is_paged_buf) {
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100406 int i;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800407 vunmap(fw_priv->data);
408 for (i = 0; i < fw_priv->nr_pages; i++)
409 __free_page(fw_priv->pages[i]);
410 vfree(fw_priv->pages);
Ming Lei746058f2012-10-09 12:01:03 +0800411 } else
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100412#endif
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800413 if (!fw_priv->allocated_size)
414 vfree(fw_priv->data);
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800415 kfree_const(fw_priv->fw_name);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800416 kfree(fw_priv);
Ming Lei1f2b7952012-08-04 12:01:21 +0800417}
418
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800419static void free_fw_priv(struct fw_priv *fw_priv)
Ming Lei1f2b7952012-08-04 12:01:21 +0800420{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800421 struct firmware_cache *fwc = fw_priv->fwc;
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800422 spin_lock(&fwc->lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800423 if (!kref_put(&fw_priv->ref, __free_fw_priv))
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800424 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800425}
426
Ming Lei746058f2012-10-09 12:01:03 +0800427/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800428static char fw_path_para[256];
429static const char * const fw_path[] = {
430 fw_path_para,
Ming Lei746058f2012-10-09 12:01:03 +0800431 "/lib/firmware/updates/" UTS_RELEASE,
432 "/lib/firmware/updates",
433 "/lib/firmware/" UTS_RELEASE,
434 "/lib/firmware"
435};
436
Ming Lei27602842012-11-03 17:47:58 +0800437/*
438 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
439 * from kernel command line because firmware_class is generally built in
440 * kernel instead of module.
441 */
442module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
443MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
444
Stephen Boyda098ecd2016-08-02 14:04:28 -0700445static int
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800446fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv)
Ming Lei746058f2012-10-09 12:01:03 +0800447{
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500448 loff_t size;
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700449 int i, len;
Neil Horman3e358ac2013-09-06 15:36:08 -0400450 int rc = -ENOENT;
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700451 char *path;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700452 enum kernel_read_file_id id = READING_FIRMWARE;
453 size_t msize = INT_MAX;
454
455 /* Already populated data member means we're loading into a buffer */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800456 if (fw_priv->data) {
Stephen Boyda098ecd2016-08-02 14:04:28 -0700457 id = READING_FIRMWARE_PREALLOC_BUFFER;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800458 msize = fw_priv->allocated_size;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700459 }
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700460
461 path = __getname();
462 if (!path)
463 return -ENOMEM;
Ming Lei746058f2012-10-09 12:01:03 +0800464
465 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
Ming Lei27602842012-11-03 17:47:58 +0800466 /* skip the unset customized path */
467 if (!fw_path[i][0])
468 continue;
469
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700470 len = snprintf(path, PATH_MAX, "%s/%s",
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800471 fw_path[i], fw_priv->fw_name);
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700472 if (len >= PATH_MAX) {
473 rc = -ENAMETOOLONG;
474 break;
475 }
Ming Lei746058f2012-10-09 12:01:03 +0800476
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800477 fw_priv->size = 0;
478 rc = kernel_read_file_from_path(path, &fw_priv->data, &size,
479 msize, id);
Kees Cook4b2530d2016-02-04 13:15:02 -0800480 if (rc) {
Luis R. Rodriguez8e516aa2016-02-28 21:57:55 +0100481 if (rc == -ENOENT)
482 dev_dbg(device, "loading %s failed with error %d\n",
483 path, rc);
484 else
485 dev_warn(device, "loading %s failed with error %d\n",
486 path, rc);
Kees Cook4b2530d2016-02-04 13:15:02 -0800487 continue;
488 }
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800489 dev_dbg(device, "direct-loading %s\n", fw_priv->fw_name);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800490 fw_priv->size = size;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800491 fw_state_done(fw_priv);
Kees Cook4b2530d2016-02-04 13:15:02 -0800492 break;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100493 }
Kees Cook4b2530d2016-02-04 13:15:02 -0800494 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100495
Neil Horman3e358ac2013-09-06 15:36:08 -0400496 return rc;
Ming Lei746058f2012-10-09 12:01:03 +0800497}
498
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100499/* firmware holds the ownership of pages */
500static void firmware_free_data(const struct firmware *fw)
501{
502 /* Loaded directly? */
503 if (!fw->priv) {
504 vfree(fw->data);
505 return;
506 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800507 free_fw_priv(fw->priv);
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100508}
509
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100510/* store the pages buffer info firmware from buf */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800511static void fw_set_page_data(struct fw_priv *fw_priv, struct firmware *fw)
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100512{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800513 fw->priv = fw_priv;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100514#ifdef CONFIG_FW_LOADER_USER_HELPER
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800515 fw->pages = fw_priv->pages;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100516#endif
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800517 fw->size = fw_priv->size;
518 fw->data = fw_priv->data;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100519
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800520 pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800521 __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800522 (unsigned int)fw_priv->size);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100523}
524
525#ifdef CONFIG_PM_SLEEP
526static void fw_name_devm_release(struct device *dev, void *res)
527{
528 struct fw_name_devm *fwn = res;
529
530 if (fwn->magic == (unsigned long)&fw_cache)
531 pr_debug("%s: fw_name-%s devm-%p released\n",
532 __func__, fwn->name, res);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700533 kfree_const(fwn->name);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100534}
535
536static int fw_devm_match(struct device *dev, void *res,
537 void *match_data)
538{
539 struct fw_name_devm *fwn = res;
540
541 return (fwn->magic == (unsigned long)&fw_cache) &&
542 !strcmp(fwn->name, match_data);
543}
544
545static struct fw_name_devm *fw_find_devm_name(struct device *dev,
546 const char *name)
547{
548 struct fw_name_devm *fwn;
549
550 fwn = devres_find(dev, fw_name_devm_release,
551 fw_devm_match, (void *)name);
552 return fwn;
553}
554
555/* add firmware name into devres list */
556static int fw_add_devm_name(struct device *dev, const char *name)
557{
558 struct fw_name_devm *fwn;
559
560 fwn = fw_find_devm_name(dev, name);
561 if (fwn)
562 return 1;
563
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700564 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
565 GFP_KERNEL);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100566 if (!fwn)
567 return -ENOMEM;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700568 fwn->name = kstrdup_const(name, GFP_KERNEL);
569 if (!fwn->name) {
Vladimir Zapolskiya885de62015-07-29 23:26:28 +0300570 devres_free(fwn);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700571 return -ENOMEM;
572 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100573
574 fwn->magic = (unsigned long)&fw_cache;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100575 devres_add(dev, fwn);
576
577 return 0;
578}
579#else
580static int fw_add_devm_name(struct device *dev, const char *name)
581{
582 return 0;
583}
584#endif
585
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800586static int assign_fw(struct firmware *fw, struct device *device,
587 unsigned int opt_flags)
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700588{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800589 struct fw_priv *fw_priv = fw->priv;
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700590
591 mutex_lock(&fw_lock);
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800592 if (!fw_priv->size || fw_state_is_aborted(fw_priv)) {
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700593 mutex_unlock(&fw_lock);
594 return -ENOENT;
595 }
596
597 /*
598 * add firmware name into devres list so that we can auto cache
599 * and uncache firmware for device.
600 *
601 * device may has been deleted already, but the problem
602 * should be fixed in devres or driver core.
603 */
604 /* don't cache firmware handled without uevent */
605 if (device && (opt_flags & FW_OPT_UEVENT) &&
606 !(opt_flags & FW_OPT_NOCACHE))
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800607 fw_add_devm_name(device, fw_priv->fw_name);
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700608
609 /*
610 * After caching firmware image is started, let it piggyback
611 * on request firmware.
612 */
613 if (!(opt_flags & FW_OPT_NOCACHE) &&
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800614 fw_priv->fwc->state == FW_LOADER_START_CACHE) {
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800615 if (fw_cache_piggyback_on_request(fw_priv->fw_name))
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800616 kref_get(&fw_priv->ref);
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700617 }
618
619 /* pass the pages buffer to driver at the last minute */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800620 fw_set_page_data(fw_priv, fw);
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700621 mutex_unlock(&fw_lock);
622 return 0;
623}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100624
625/*
626 * user-mode helper code
627 */
628#ifdef CONFIG_FW_LOADER_USER_HELPER
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800629struct fw_sysfs {
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100630 bool nowait;
631 struct device dev;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800632 struct fw_priv *fw_priv;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100633 struct firmware *fw;
634};
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100635
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800636static struct fw_sysfs *to_fw_sysfs(struct device *dev)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700637{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800638 return container_of(dev, struct fw_sysfs, dev);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700639}
640
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800641static void __fw_load_abort(struct fw_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Ming Lei87597932013-06-15 16:36:38 +0800643 /*
644 * There is a small window in which user can write to 'loading'
645 * between loading done and disappearance of 'loading'
646 */
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800647 if (fw_sysfs_done(fw_priv))
Ming Lei87597932013-06-15 16:36:38 +0800648 return;
649
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800650 list_del_init(&fw_priv->pending_list);
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800651 fw_state_aborted(fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800654static void fw_load_abort(struct fw_sysfs *fw_sysfs)
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700655{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800656 struct fw_priv *fw_priv = fw_sysfs->fw_priv;
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700657
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800658 __fw_load_abort(fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Takashi Iwaife304142013-05-22 18:28:38 +0200661static LIST_HEAD(pending_fw_head);
662
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -0700663static void kill_pending_fw_fallback_reqs(bool only_kill_custom)
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700664{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800665 struct fw_priv *fw_priv;
666 struct fw_priv *next;
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700667
668 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800669 list_for_each_entry_safe(fw_priv, next, &pending_fw_head,
670 pending_list) {
671 if (!fw_priv->need_uevent || !only_kill_custom)
672 __fw_load_abort(fw_priv);
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700673 }
674 mutex_unlock(&fw_lock);
675}
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700676
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700677static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
678 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 return sprintf(buf, "%d\n", loading_timeout);
681}
682
683/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800684 * firmware_timeout_store - set number of seconds to wait for firmware
685 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800686 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800687 * @buf: buffer to scan for timeout value
688 * @count: number of bytes in @buf
689 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800691 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 * firmware will be provided.
693 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800694 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 **/
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700696static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
697 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700700 if (loading_timeout < 0)
701 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return count;
704}
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100705static CLASS_ATTR_RW(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100707static struct attribute *firmware_class_attrs[] = {
708 &class_attr_timeout.attr,
709 NULL,
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800710};
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100711ATTRIBUTE_GROUPS(firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800713static void fw_dev_release(struct device *dev)
714{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800715 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800716
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800717 kfree(fw_sysfs);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800718}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800720static int do_firmware_uevent(struct fw_sysfs *fw_sysfs, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800722 if (add_uevent_var(env, "FIRMWARE=%s", fw_sysfs->fw_priv->fw_name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200724 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700725 return -ENOMEM;
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800726 if (add_uevent_var(env, "ASYNC=%d", fw_sysfs->nowait))
Johannes Berge9045f92010-03-29 17:57:20 +0200727 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 return 0;
730}
731
Linus Torvalds6f957722015-07-09 11:20:01 -0700732static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
733{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800734 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Linus Torvalds6f957722015-07-09 11:20:01 -0700735 int err = 0;
736
737 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800738 if (fw_sysfs->fw_priv)
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800739 err = do_firmware_uevent(fw_sysfs, env);
Linus Torvalds6f957722015-07-09 11:20:01 -0700740 mutex_unlock(&fw_lock);
741 return err;
742}
743
Adrian Bunk1b81d662006-05-20 15:00:16 -0700744static struct class firmware_class = {
745 .name = "firmware",
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100746 .class_groups = firmware_class_groups,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700747 .dev_uevent = firmware_uevent,
748 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700749};
750
Luis R. Rodriguez6bb9cf32017-11-20 09:45:33 -0800751static inline int register_sysfs_loader(void)
752{
753 return class_register(&firmware_class);
754}
755
756static inline void unregister_sysfs_loader(void)
757{
758 class_unregister(&firmware_class);
759}
760
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700761static ssize_t firmware_loading_show(struct device *dev,
762 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800764 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Ming Lei87597932013-06-15 16:36:38 +0800765 int loading = 0;
766
767 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800768 if (fw_sysfs->fw_priv)
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800769 loading = fw_sysfs_loading(fw_sysfs->fw_priv);
Ming Lei87597932013-06-15 16:36:38 +0800770 mutex_unlock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return sprintf(buf, "%d\n", loading);
773}
774
David Woodhouse6e03a202009-04-09 22:04:07 -0700775/* Some architectures don't have PAGE_KERNEL_RO */
776#ifndef PAGE_KERNEL_RO
777#define PAGE_KERNEL_RO PAGE_KERNEL
778#endif
Ming Lei253c9242012-10-09 12:01:02 +0800779
780/* one pages buffer should be mapped/unmapped only once */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800781static int map_fw_priv_pages(struct fw_priv *fw_priv)
Ming Lei253c9242012-10-09 12:01:02 +0800782{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800783 if (!fw_priv->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800784 return 0;
785
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800786 vunmap(fw_priv->data);
787 fw_priv->data = vmap(fw_priv->pages, fw_priv->nr_pages, 0,
788 PAGE_KERNEL_RO);
789 if (!fw_priv->data)
Ming Lei253c9242012-10-09 12:01:02 +0800790 return -ENOMEM;
791 return 0;
792}
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800795 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700796 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800797 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800798 * @buf: buffer to scan for loading control value
799 * @count: number of bytes in @buf
800 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 * The relevant values are:
802 *
803 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800804 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 * -1: Conclude the load with an error and discard any written data.
806 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700807static ssize_t firmware_loading_store(struct device *dev,
808 struct device_attribute *attr,
809 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800811 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800812 struct fw_priv *fw_priv;
Kees Cook6593d922014-02-25 13:06:00 -0800813 ssize_t written = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700815 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Neil Hormaneea915b2012-01-02 15:31:23 -0500817 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800818 fw_priv = fw_sysfs->fw_priv;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800819 if (fw_state_is_aborted(fw_priv))
Neil Hormaneea915b2012-01-02 15:31:23 -0500820 goto out;
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 switch (loading) {
823 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800824 /* discarding any previous partial load */
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800825 if (!fw_sysfs_done(fw_priv)) {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800826 for (i = 0; i < fw_priv->nr_pages; i++)
827 __free_page(fw_priv->pages[i]);
828 vfree(fw_priv->pages);
829 fw_priv->pages = NULL;
830 fw_priv->page_array_size = 0;
831 fw_priv->nr_pages = 0;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800832 fw_state_start(fw_priv);
Ming Lei28eefa72012-08-04 12:01:17 +0800833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 break;
835 case 0:
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800836 if (fw_sysfs_loading(fw_priv)) {
Kees Cook6593d922014-02-25 13:06:00 -0800837 int rc;
838
Ming Lei253c9242012-10-09 12:01:02 +0800839 /*
840 * Several loading requests may be pending on
841 * one same firmware buf, so let all requests
842 * see the mapped 'buf->data' once the loading
843 * is completed.
844 * */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800845 rc = map_fw_priv_pages(fw_priv);
Kees Cook6593d922014-02-25 13:06:00 -0800846 if (rc)
zhang jun2b1278c2014-02-13 15:18:47 +0800847 dev_err(dev, "%s: map pages failed\n",
848 __func__);
Kees Cook6593d922014-02-25 13:06:00 -0800849 else
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500850 rc = security_kernel_post_read_file(NULL,
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800851 fw_priv->data, fw_priv->size,
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500852 READING_FIRMWARE);
Kees Cook6593d922014-02-25 13:06:00 -0800853
854 /*
855 * Same logic as fw_load_abort, only the DONE bit
856 * is ignored and we set ABORT only on failure.
857 */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800858 list_del_init(&fw_priv->pending_list);
Kees Cook6593d922014-02-25 13:06:00 -0800859 if (rc) {
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800860 fw_state_aborted(fw_priv);
Kees Cook6593d922014-02-25 13:06:00 -0800861 written = rc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100862 } else {
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800863 fw_state_done(fw_priv);
Kees Cook6593d922014-02-25 13:06:00 -0800864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 break;
866 }
867 /* fallthrough */
868 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700869 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 /* fallthrough */
871 case -1:
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800872 fw_load_abort(fw_sysfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 break;
874 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500875out:
876 mutex_unlock(&fw_lock);
Kees Cook6593d922014-02-25 13:06:00 -0800877 return written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700880static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800882static void firmware_rw_data(struct fw_priv *fw_priv, char *buffer,
Stephen Boyda098ecd2016-08-02 14:04:28 -0700883 loff_t offset, size_t count, bool read)
884{
885 if (read)
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800886 memcpy(buffer, fw_priv->data + offset, count);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700887 else
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800888 memcpy(fw_priv->data + offset, buffer, count);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700889}
890
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800891static void firmware_rw(struct fw_priv *fw_priv, char *buffer,
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700892 loff_t offset, size_t count, bool read)
893{
894 while (count) {
895 void *page_data;
896 int page_nr = offset >> PAGE_SHIFT;
897 int page_ofs = offset & (PAGE_SIZE-1);
898 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
899
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800900 page_data = kmap(fw_priv->pages[page_nr]);
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700901
902 if (read)
903 memcpy(buffer, page_data + page_ofs, page_cnt);
904 else
905 memcpy(page_data + page_ofs, buffer, page_cnt);
906
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800907 kunmap(fw_priv->pages[page_nr]);
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700908 buffer += page_cnt;
909 offset += page_cnt;
910 count -= page_cnt;
911 }
912}
913
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700914static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
915 struct bin_attribute *bin_attr,
916 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200918 struct device *dev = kobj_to_dev(kobj);
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800919 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800920 struct fw_priv *fw_priv;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700921 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Laura Garciacad1e552006-05-23 23:22:38 +0200923 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800924 fw_priv = fw_sysfs->fw_priv;
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800925 if (!fw_priv || fw_sysfs_done(fw_priv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 ret_count = -ENODEV;
927 goto out;
928 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800929 if (offset > fw_priv->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200930 ret_count = 0;
931 goto out;
932 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800933 if (count > fw_priv->size - offset)
934 count = fw_priv->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700935
936 ret_count = count;
937
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800938 if (fw_priv->data)
939 firmware_rw_data(fw_priv, buffer, offset, count, true);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700940 else
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800941 firmware_rw(fw_priv, buffer, offset, count, true);
David Woodhouse6e03a202009-04-09 22:04:07 -0700942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943out:
Laura Garciacad1e552006-05-23 23:22:38 +0200944 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return ret_count;
946}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800947
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800948static int fw_realloc_pages(struct fw_sysfs *fw_sysfs, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800950 struct fw_priv *fw_priv= fw_sysfs->fw_priv;
Fabian Fredericka76040d2014-07-01 21:13:30 +0200951 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
David Woodhouse6e03a202009-04-09 22:04:07 -0700953 /* If the array of pages is too small, grow it... */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800954 if (fw_priv->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700955 int new_array_size = max(pages_needed,
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800956 fw_priv->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700957 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Chen Feng10a3fbf2015-12-16 17:03:15 +0800959 new_pages = vmalloc(new_array_size * sizeof(void *));
David Woodhouse6e03a202009-04-09 22:04:07 -0700960 if (!new_pages) {
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800961 fw_load_abort(fw_sysfs);
David Woodhouse6e03a202009-04-09 22:04:07 -0700962 return -ENOMEM;
963 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800964 memcpy(new_pages, fw_priv->pages,
965 fw_priv->page_array_size * sizeof(void *));
966 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
967 (new_array_size - fw_priv->page_array_size));
968 vfree(fw_priv->pages);
969 fw_priv->pages = new_pages;
970 fw_priv->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700972
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800973 while (fw_priv->nr_pages < pages_needed) {
974 fw_priv->pages[fw_priv->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700975 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
976
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800977 if (!fw_priv->pages[fw_priv->nr_pages]) {
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800978 fw_load_abort(fw_sysfs);
David Woodhouse6e03a202009-04-09 22:04:07 -0700979 return -ENOMEM;
980 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800981 fw_priv->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return 0;
984}
985
986/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800987 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700988 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700989 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700990 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800991 * @buffer: buffer being written
992 * @offset: buffer offset for write in total data store area
993 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800995 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 * the driver as a firmware image.
997 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700998static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
999 struct bin_attribute *bin_attr,
1000 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001002 struct device *dev = kobj_to_dev(kobj);
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001003 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001004 struct fw_priv *fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 ssize_t retval;
1006
1007 if (!capable(CAP_SYS_RAWIO))
1008 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -07001009
Laura Garciacad1e552006-05-23 23:22:38 +02001010 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001011 fw_priv = fw_sysfs->fw_priv;
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -08001012 if (!fw_priv || fw_sysfs_done(fw_priv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 retval = -ENODEV;
1014 goto out;
1015 }
Ming Lei65710cb2012-08-04 12:01:16 +08001016
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001017 if (fw_priv->data) {
1018 if (offset + count > fw_priv->allocated_size) {
Stephen Boyda098ecd2016-08-02 14:04:28 -07001019 retval = -ENOMEM;
1020 goto out;
1021 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001022 firmware_rw_data(fw_priv, buffer, offset, count, false);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001023 retval = count;
1024 } else {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001025 retval = fw_realloc_pages(fw_sysfs, offset + count);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001026 if (retval)
1027 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Stephen Boyda098ecd2016-08-02 14:04:28 -07001029 retval = count;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001030 firmware_rw(fw_priv, buffer, offset, count, false);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001031 }
David Woodhouse6e03a202009-04-09 22:04:07 -07001032
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001033 fw_priv->size = max_t(size_t, offset + count, fw_priv->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034out:
Laura Garciacad1e552006-05-23 23:22:38 +02001035 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return retval;
1037}
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001038
Dmitry Torokhov0983ca22010-06-04 00:54:37 -07001039static struct bin_attribute firmware_attr_data = {
1040 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 .size = 0,
1042 .read = firmware_data_read,
1043 .write = firmware_data_write,
1044};
1045
Takashi Iwai46239902015-02-04 15:18:07 +01001046static struct attribute *fw_dev_attrs[] = {
1047 &dev_attr_loading.attr,
1048 NULL
1049};
1050
1051static struct bin_attribute *fw_dev_bin_attrs[] = {
1052 &firmware_attr_data,
1053 NULL
1054};
1055
1056static const struct attribute_group fw_dev_attr_group = {
1057 .attrs = fw_dev_attrs,
1058 .bin_attrs = fw_dev_bin_attrs,
1059};
1060
1061static const struct attribute_group *fw_dev_attr_groups[] = {
1062 &fw_dev_attr_group,
1063 NULL
1064};
1065
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001066static struct fw_sysfs *
Stephen Boyddddb5542012-03-28 23:30:43 +02001067fw_create_instance(struct firmware *firmware, const char *fw_name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001068 struct device *device, unsigned int opt_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001070 struct fw_sysfs *fw_sysfs;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001071 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001073 fw_sysfs = kzalloc(sizeof(*fw_sysfs), GFP_KERNEL);
1074 if (!fw_sysfs) {
1075 fw_sysfs = ERR_PTR(-ENOMEM);
Ming Lei12446912012-08-04 12:01:20 +08001076 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001079 fw_sysfs->nowait = !!(opt_flags & FW_OPT_NOWAIT);
1080 fw_sysfs->fw = firmware;
1081 f_dev = &fw_sysfs->dev;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001082
1083 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +08001084 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -07001085 f_dev->parent = device;
1086 f_dev->class = &firmware_class;
Takashi Iwai46239902015-02-04 15:18:07 +01001087 f_dev->groups = fw_dev_attr_groups;
Ming Lei12446912012-08-04 12:01:20 +08001088exit:
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001089 return fw_sysfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001091
1092/* load a firmware via user helper */
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001093static int _request_firmware_load(struct fw_sysfs *fw_sysfs,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001094 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001095{
1096 int retval = 0;
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001097 struct device *f_dev = &fw_sysfs->dev;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001098 struct fw_priv *fw_priv = fw_sysfs->fw_priv;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001099
1100 /* fall back on userspace loading */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001101 if (!fw_priv->data)
1102 fw_priv->is_paged_buf = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001103
1104 dev_set_uevent_suppress(f_dev, true);
1105
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001106 retval = device_add(f_dev);
1107 if (retval) {
1108 dev_err(f_dev, "%s: device_register failed\n", __func__);
1109 goto err_put_dev;
1110 }
1111
Maxime Bizon1eeeef12013-08-29 20:28:13 +02001112 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001113 list_add(&fw_priv->pending_list, &pending_fw_head);
Maxime Bizon1eeeef12013-08-29 20:28:13 +02001114 mutex_unlock(&fw_lock);
1115
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001116 if (opt_flags & FW_OPT_UEVENT) {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001117 fw_priv->need_uevent = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001118 dev_set_uevent_suppress(f_dev, false);
Luis R. Rodriguez31034f22017-11-20 10:23:49 -08001119 dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_name);
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001120 kobject_uevent(&fw_sysfs->dev.kobj, KOBJ_ADD);
Ming Lei68ff2a02015-01-13 00:02:01 +08001121 } else {
1122 timeout = MAX_JIFFY_OFFSET;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001123 }
1124
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -08001125 retval = fw_sysfs_wait_timeout(fw_priv, timeout);
Bjorn Andersson5d47ec02016-12-06 17:01:45 -08001126 if (retval < 0) {
Ming Lei0cb64242015-01-13 00:01:35 +08001127 mutex_lock(&fw_lock);
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001128 fw_load_abort(fw_sysfs);
Ming Lei0cb64242015-01-13 00:01:35 +08001129 mutex_unlock(&fw_lock);
1130 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001131
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -08001132 if (fw_state_is_aborted(fw_priv)) {
Luis R. Rodriguez76098b32017-07-20 13:13:39 -07001133 if (retval == -ERESTARTSYS)
1134 retval = -EINTR;
1135 else
1136 retval = -EAGAIN;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001137 } else if (fw_priv->is_paged_buf && !fw_priv->data)
zhang jun2b1278c2014-02-13 15:18:47 +08001138 retval = -ENOMEM;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001139
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001140 device_del(f_dev);
1141err_put_dev:
1142 put_device(f_dev);
1143 return retval;
1144}
1145
1146static int fw_load_from_user_helper(struct firmware *firmware,
1147 const char *name, struct device *device,
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001148 unsigned int opt_flags)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001149{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001150 struct fw_sysfs *fw_sysfs;
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001151 long timeout;
1152 int ret;
1153
1154 timeout = firmware_loading_timeout();
1155 if (opt_flags & FW_OPT_NOWAIT) {
1156 timeout = usermodehelper_read_lock_wait(timeout);
1157 if (!timeout) {
1158 dev_dbg(device, "firmware: %s loading timed out\n",
1159 name);
1160 return -EBUSY;
1161 }
1162 } else {
1163 ret = usermodehelper_read_trylock();
1164 if (WARN_ON(ret)) {
1165 dev_err(device, "firmware: %s will not be loaded\n",
1166 name);
1167 return ret;
1168 }
1169 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001170
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001171 fw_sysfs = fw_create_instance(firmware, name, device, opt_flags);
1172 if (IS_ERR(fw_sysfs)) {
1173 ret = PTR_ERR(fw_sysfs);
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001174 goto out_unlock;
1175 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001176
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001177 fw_sysfs->fw_priv = firmware->priv;
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001178 ret = _request_firmware_load(fw_sysfs, opt_flags, timeout);
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001179
1180 if (!ret)
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001181 ret = assign_fw(firmware, device, opt_flags);
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001182
1183out_unlock:
1184 usermodehelper_read_unlock();
1185
1186 return ret;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001187}
Ming Leiddf1f062013-06-04 10:01:13 +08001188
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001189static bool fw_force_sysfs_fallback(unsigned int opt_flags)
1190{
Luis R. Rodriguezb2e9a852018-03-10 06:14:46 -08001191 if (fw_fallback_config.force_sysfs_fallback)
1192 return true;
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001193 if (!(opt_flags & FW_OPT_USERHELPER))
1194 return false;
1195 return true;
1196}
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001197
1198static bool fw_run_sysfs_fallback(unsigned int opt_flags)
1199{
1200 if ((opt_flags & FW_OPT_NOFALLBACK))
1201 return false;
1202
1203 return fw_force_sysfs_fallback(opt_flags);
1204}
1205
Luis R. Rodriguez25d39132017-11-20 10:23:57 -08001206static int fw_sysfs_fallback(struct firmware *fw, const char *name,
1207 struct device *device,
1208 unsigned int opt_flags,
1209 int ret)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001210{
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001211 if (!fw_run_sysfs_fallback(opt_flags))
Luis R. Rodriguez25d39132017-11-20 10:23:57 -08001212 return ret;
1213
1214 dev_warn(device, "Falling back to user helper\n");
1215 return fw_load_from_user_helper(fw, name, device, opt_flags);
1216}
1217#else /* CONFIG_FW_LOADER_USER_HELPER */
1218static int fw_sysfs_fallback(struct firmware *fw, const char *name,
1219 struct device *device,
1220 unsigned int opt_flags,
1221 int ret)
1222{
1223 /* Keep carrying over the same error */
1224 return ret;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001225}
Takashi Iwai807be032013-01-31 11:13:57 +01001226
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -07001227static inline void kill_pending_fw_fallback_reqs(bool only_kill_custom) { }
Luis R. Rodriguez5d9566b2018-03-10 06:14:47 -08001228static inline void fw_fallback_set_cache_timeout(void) { }
1229static inline void fw_fallback_set_default_timeout(void) { }
Ming Leiddf1f062013-06-04 10:01:13 +08001230
Luis R. Rodriguez6bb9cf32017-11-20 09:45:33 -08001231static inline int register_sysfs_loader(void)
1232{
1233 return 0;
1234}
1235
1236static inline void unregister_sysfs_loader(void)
1237{
1238}
1239
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001240#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001242/* prepare firmware and firmware_buf structs;
1243 * return 0 if a firmware is already assigned, 1 if need to load one,
1244 * or a negative error code
1245 */
1246static int
1247_request_firmware_prepare(struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001248 struct device *device, void *dbuf, size_t size)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001249{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 struct firmware *firmware;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001251 struct fw_priv *fw_priv;
Ming Lei1f2b7952012-08-04 12:01:21 +08001252 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Jiri Slaby4aed0642005-09-13 01:25:01 -07001254 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -07001256 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1257 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001258 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Stephen Boyda098ecd2016-08-02 14:04:28 -07001261 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
Luis R. Rodriguezed046302015-04-29 16:30:43 -07001262 dev_dbg(device, "using built-in %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001263 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +01001264 }
1265
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001266 ret = alloc_lookup_fw_priv(name, &fw_cache, &fw_priv, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +08001267
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001268 /*
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001269 * bind with 'priv' now to avoid warning in failure path
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001270 * of requesting firmware.
1271 */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001272 firmware->priv = fw_priv;
Ming Lei1f2b7952012-08-04 12:01:21 +08001273
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001274 if (ret > 0) {
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -08001275 ret = fw_state_wait(fw_priv);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001276 if (!ret) {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001277 fw_set_page_data(fw_priv, firmware);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001278 return 0; /* assigned */
1279 }
Stephen Boyddddb5542012-03-28 23:30:43 +02001280 }
Ming Lei1f2b7952012-08-04 12:01:21 +08001281
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001282 if (ret < 0)
1283 return ret;
1284 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001285}
1286
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001287/*
1288 * Batched requests need only one wake, we need to do this step last due to the
1289 * fallback mechanism. The buf is protected with kref_get(), and it won't be
1290 * released until the last user calls release_firmware().
1291 *
1292 * Failed batched requests are possible as well, in such cases we just share
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001293 * the struct fw_priv and won't release it until all requests are woken
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001294 * and have gone through this same path.
1295 */
1296static void fw_abort_batch_reqs(struct firmware *fw)
1297{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001298 struct fw_priv *fw_priv;
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001299
1300 /* Loaded directly? */
1301 if (!fw || !fw->priv)
1302 return;
1303
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001304 fw_priv = fw->priv;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -08001305 if (!fw_state_is_aborted(fw_priv))
1306 fw_state_aborted(fw_priv);
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001307}
1308
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001309/* called from request_firmware() and request_firmware_work_func() */
1310static int
1311_request_firmware(const struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001312 struct device *device, void *buf, size_t size,
1313 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001314{
Brian Norris715780a2015-12-09 14:50:28 -08001315 struct firmware *fw = NULL;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001316 int ret;
1317
1318 if (!firmware_p)
1319 return -EINVAL;
1320
Brian Norris715780a2015-12-09 14:50:28 -08001321 if (!name || name[0] == '\0') {
1322 ret = -EINVAL;
1323 goto out;
1324 }
Kees Cook471b0952014-09-18 11:25:37 -07001325
Stephen Boyda098ecd2016-08-02 14:04:28 -07001326 ret = _request_firmware_prepare(&fw, name, device, buf, size);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001327 if (ret <= 0) /* error or already assigned */
1328 goto out;
1329
Neil Horman3e358ac2013-09-06 15:36:08 -04001330 ret = fw_get_filesystem_firmware(device, fw->priv);
1331 if (ret) {
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001332 if (!(opt_flags & FW_OPT_NO_WARN))
Takashi Iwaibba3a872013-12-02 15:38:16 +01001333 dev_warn(device,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001334 "Direct firmware load for %s failed with error %d\n",
1335 name, ret);
Luis R. Rodriguez25d39132017-11-20 10:23:57 -08001336 ret = fw_sysfs_fallback(fw, name, device, opt_flags, ret);
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001337 } else
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001338 ret = assign_fw(fw, device, opt_flags);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001339
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001340 out:
1341 if (ret < 0) {
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001342 fw_abort_batch_reqs(fw);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001343 release_firmware(fw);
1344 fw = NULL;
1345 }
1346
1347 *firmware_p = fw;
1348 return ret;
1349}
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351/**
Kay Sievers312c0042005-11-16 09:00:00 +01001352 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001353 * @firmware_p: pointer to firmware image
1354 * @name: name of firmware file
1355 * @device: device for which firmware is being loaded
1356 *
1357 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001358 * of @name for device @device.
1359 *
1360 * Should be called from user context where sleeping is allowed.
1361 *
Kay Sievers312c0042005-11-16 09:00:00 +01001362 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001363 * should be distinctive enough not to be confused with any other
1364 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001365 *
1366 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001367 *
1368 * The function can be called safely inside device's suspend and
1369 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001370 **/
1371int
1372request_firmware(const struct firmware **firmware_p, const char *name,
Andrei Opreaea310032015-03-08 12:41:15 +02001373 struct device *device)
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001374{
Ming Leid6c8aa32013-06-06 20:01:48 +08001375 int ret;
1376
1377 /* Need to pin this module until return */
1378 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001379 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001380 FW_OPT_UEVENT);
Ming Leid6c8aa32013-06-06 20:01:48 +08001381 module_put(THIS_MODULE);
1382 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001383}
Daniel Mackf4945132013-05-23 22:17:18 +02001384EXPORT_SYMBOL(request_firmware);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001385
Takashi Iwaibba3a872013-12-02 15:38:16 +01001386/**
Borislav Petkov3c1556b2014-12-03 22:46:37 +01001387 * request_firmware_direct: - load firmware directly without usermode helper
Takashi Iwaibba3a872013-12-02 15:38:16 +01001388 * @firmware_p: pointer to firmware image
1389 * @name: name of firmware file
1390 * @device: device for which firmware is being loaded
1391 *
1392 * This function works pretty much like request_firmware(), but this doesn't
1393 * fall back to usermode helper even if the firmware couldn't be loaded
1394 * directly from fs. Hence it's useful for loading optional firmwares, which
1395 * aren't always present, without extra long timeouts of udev.
1396 **/
1397int request_firmware_direct(const struct firmware **firmware_p,
1398 const char *name, struct device *device)
1399{
1400 int ret;
Andrei Opreaea310032015-03-08 12:41:15 +02001401
Takashi Iwaibba3a872013-12-02 15:38:16 +01001402 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001403 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001404 FW_OPT_UEVENT | FW_OPT_NO_WARN |
1405 FW_OPT_NOFALLBACK);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001406 module_put(THIS_MODULE);
1407 return ret;
1408}
1409EXPORT_SYMBOL_GPL(request_firmware_direct);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001410
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001411/**
Stephen Boyda098ecd2016-08-02 14:04:28 -07001412 * request_firmware_into_buf - load firmware into a previously allocated buffer
1413 * @firmware_p: pointer to firmware image
1414 * @name: name of firmware file
1415 * @device: device for which firmware is being loaded and DMA region allocated
1416 * @buf: address of buffer to load firmware into
1417 * @size: size of buffer
1418 *
1419 * This function works pretty much like request_firmware(), but it doesn't
1420 * allocate a buffer to hold the firmware data. Instead, the firmware
1421 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
1422 * data member is pointed at @buf.
1423 *
1424 * This function doesn't cache firmware either.
1425 */
1426int
1427request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
1428 struct device *device, void *buf, size_t size)
1429{
1430 int ret;
1431
1432 __module_get(THIS_MODULE);
1433 ret = _request_firmware(firmware_p, name, device, buf, size,
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001434 FW_OPT_UEVENT | FW_OPT_NOCACHE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001435 module_put(THIS_MODULE);
1436 return ret;
1437}
1438EXPORT_SYMBOL(request_firmware_into_buf);
1439
1440/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001442 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001444void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001447 if (!fw_is_builtin_firmware(fw))
1448 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 kfree(fw);
1450 }
1451}
Daniel Mackf4945132013-05-23 22:17:18 +02001452EXPORT_SYMBOL(release_firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454/* Async support */
1455struct firmware_work {
1456 struct work_struct work;
1457 struct module *module;
1458 const char *name;
1459 struct device *device;
1460 void *context;
1461 void (*cont)(const struct firmware *fw, void *context);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001462 unsigned int opt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463};
1464
Stephen Boyda36cf842012-03-28 23:31:00 +02001465static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
Stephen Boyda36cf842012-03-28 23:31:00 +02001467 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001469
Stephen Boyda36cf842012-03-28 23:31:00 +02001470 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001471
Stephen Boyda098ecd2016-08-02 14:04:28 -07001472 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001473 fw_work->opt_flags);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001474 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001475 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 module_put(fw_work->module);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001478 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
1482/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001483 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001484 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001485 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001486 * is non-zero else the firmware copy must be done manually.
1487 * @name: name of firmware file
1488 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001489 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001490 * @context: will be passed over to @cont, and
1491 * @fw may be %NULL if firmware request fails.
1492 * @cont: function will be called asynchronously when the firmware
1493 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001495 * Caller must hold the reference count of @device.
1496 *
Ming Lei6f21a622012-08-04 12:01:24 +08001497 * Asynchronous variant of request_firmware() for user contexts:
1498 * - sleep for as small periods as possible since it may
Silvio Fricke88bcef52016-11-25 15:59:47 +01001499 * increase kernel boot time of built-in device drivers
1500 * requesting firmware in their ->probe() methods, if
1501 * @gfp is GFP_KERNEL.
Ming Lei6f21a622012-08-04 12:01:24 +08001502 *
1503 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 **/
1505int
1506request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001507 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001508 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 void (*cont)(const struct firmware *fw, void *context))
1510{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001511 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Andrei Opreaea310032015-03-08 12:41:15 +02001513 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (!fw_work)
1515 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001516
1517 fw_work->module = module;
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001518 fw_work->name = kstrdup_const(name, gfp);
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001519 if (!fw_work->name) {
1520 kfree(fw_work);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001521 return -ENOMEM;
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001522 }
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001523 fw_work->device = device;
1524 fw_work->context = context;
1525 fw_work->cont = cont;
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001526 fw_work->opt_flags = FW_OPT_NOWAIT |
Takashi Iwai5a1379e2014-06-04 17:48:15 +02001527 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (!try_module_get(module)) {
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001530 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 kfree(fw_work);
1532 return -EFAULT;
1533 }
1534
Ming Lei0cfc1e12012-08-04 12:01:23 +08001535 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001536 INIT_WORK(&fw_work->work, request_firmware_work_func);
1537 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return 0;
1539}
Daniel Mackf4945132013-05-23 22:17:18 +02001540EXPORT_SYMBOL(request_firmware_nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Ming Lei90f890812013-06-20 12:30:16 +08001542#ifdef CONFIG_PM_SLEEP
1543static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1544
Ming Lei2887b392012-08-04 12:01:22 +08001545/**
1546 * cache_firmware - cache one firmware image in kernel memory space
1547 * @fw_name: the firmware image name
1548 *
1549 * Cache firmware in kernel memory so that drivers can use it when
1550 * system isn't ready for them to request firmware image from userspace.
1551 * Once it returns successfully, driver can use request_firmware or its
1552 * nowait version to get the cached firmware without any interacting
1553 * with userspace
1554 *
1555 * Return 0 if the firmware image has been cached successfully
1556 * Return !0 otherwise
1557 *
1558 */
Ming Lei93232e42013-06-06 20:01:47 +08001559static int cache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001560{
1561 int ret;
1562 const struct firmware *fw;
1563
1564 pr_debug("%s: %s\n", __func__, fw_name);
1565
1566 ret = request_firmware(&fw, fw_name, NULL);
1567 if (!ret)
1568 kfree(fw);
1569
1570 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1571
1572 return ret;
1573}
1574
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001575static struct fw_priv *lookup_fw_priv(const char *fw_name)
Ming Lei6a2c1232013-06-26 09:28:17 +08001576{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001577 struct fw_priv *tmp;
Ming Lei6a2c1232013-06-26 09:28:17 +08001578 struct firmware_cache *fwc = &fw_cache;
1579
1580 spin_lock(&fwc->lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001581 tmp = __lookup_fw_priv(fw_name);
Ming Lei6a2c1232013-06-26 09:28:17 +08001582 spin_unlock(&fwc->lock);
1583
1584 return tmp;
1585}
1586
Ming Lei2887b392012-08-04 12:01:22 +08001587/**
1588 * uncache_firmware - remove one cached firmware image
1589 * @fw_name: the firmware image name
1590 *
1591 * Uncache one firmware image which has been cached successfully
1592 * before.
1593 *
1594 * Return 0 if the firmware cache has been removed successfully
1595 * Return !0 otherwise
1596 *
1597 */
Ming Lei93232e42013-06-06 20:01:47 +08001598static int uncache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001599{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001600 struct fw_priv *fw_priv;
Ming Lei2887b392012-08-04 12:01:22 +08001601 struct firmware fw;
1602
1603 pr_debug("%s: %s\n", __func__, fw_name);
1604
Stephen Boyda098ecd2016-08-02 14:04:28 -07001605 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
Ming Lei2887b392012-08-04 12:01:22 +08001606 return 0;
1607
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001608 fw_priv = lookup_fw_priv(fw_name);
1609 if (fw_priv) {
1610 free_fw_priv(fw_priv);
Ming Lei2887b392012-08-04 12:01:22 +08001611 return 0;
1612 }
1613
1614 return -EINVAL;
1615}
1616
Ming Lei37276a52012-08-04 12:01:27 +08001617static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1618{
1619 struct fw_cache_entry *fce;
1620
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001621 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
Ming Lei37276a52012-08-04 12:01:27 +08001622 if (!fce)
1623 goto exit;
1624
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001625 fce->name = kstrdup_const(name, GFP_ATOMIC);
1626 if (!fce->name) {
1627 kfree(fce);
1628 fce = NULL;
1629 goto exit;
1630 }
Ming Lei37276a52012-08-04 12:01:27 +08001631exit:
1632 return fce;
1633}
1634
Ming Lei373304f2012-10-09 12:01:01 +08001635static int __fw_entry_found(const char *name)
1636{
1637 struct firmware_cache *fwc = &fw_cache;
1638 struct fw_cache_entry *fce;
1639
1640 list_for_each_entry(fce, &fwc->fw_names, list) {
1641 if (!strcmp(fce->name, name))
1642 return 1;
1643 }
1644 return 0;
1645}
1646
Ming Leiac39b3e2012-08-20 19:04:16 +08001647static int fw_cache_piggyback_on_request(const char *name)
1648{
1649 struct firmware_cache *fwc = &fw_cache;
1650 struct fw_cache_entry *fce;
1651 int ret = 0;
1652
1653 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001654 if (__fw_entry_found(name))
1655 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001656
1657 fce = alloc_fw_cache_entry(name);
1658 if (fce) {
1659 ret = 1;
1660 list_add(&fce->list, &fwc->fw_names);
1661 pr_debug("%s: fw: %s\n", __func__, name);
1662 }
1663found:
1664 spin_unlock(&fwc->name_lock);
1665 return ret;
1666}
1667
Ming Lei37276a52012-08-04 12:01:27 +08001668static void free_fw_cache_entry(struct fw_cache_entry *fce)
1669{
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001670 kfree_const(fce->name);
Ming Lei37276a52012-08-04 12:01:27 +08001671 kfree(fce);
1672}
1673
1674static void __async_dev_cache_fw_image(void *fw_entry,
1675 async_cookie_t cookie)
1676{
1677 struct fw_cache_entry *fce = fw_entry;
1678 struct firmware_cache *fwc = &fw_cache;
1679 int ret;
1680
1681 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001682 if (ret) {
1683 spin_lock(&fwc->name_lock);
1684 list_del(&fce->list);
1685 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001686
Ming Leiac39b3e2012-08-20 19:04:16 +08001687 free_fw_cache_entry(fce);
1688 }
Ming Lei37276a52012-08-04 12:01:27 +08001689}
1690
1691/* called with dev->devres_lock held */
1692static void dev_create_fw_entry(struct device *dev, void *res,
1693 void *data)
1694{
1695 struct fw_name_devm *fwn = res;
1696 const char *fw_name = fwn->name;
1697 struct list_head *head = data;
1698 struct fw_cache_entry *fce;
1699
1700 fce = alloc_fw_cache_entry(fw_name);
1701 if (fce)
1702 list_add(&fce->list, head);
1703}
1704
1705static int devm_name_match(struct device *dev, void *res,
1706 void *match_data)
1707{
1708 struct fw_name_devm *fwn = res;
1709 return (fwn->magic == (unsigned long)match_data);
1710}
1711
Ming Leiab6dd8e2012-08-17 22:07:00 +08001712static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001713{
1714 LIST_HEAD(todo);
1715 struct fw_cache_entry *fce;
1716 struct fw_cache_entry *fce_next;
1717 struct firmware_cache *fwc = &fw_cache;
1718
1719 devres_for_each_res(dev, fw_name_devm_release,
1720 devm_name_match, &fw_cache,
1721 dev_create_fw_entry, &todo);
1722
1723 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1724 list_del(&fce->list);
1725
1726 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001727 /* only one cache entry for one firmware */
1728 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001729 list_add(&fce->list, &fwc->fw_names);
1730 } else {
1731 free_fw_cache_entry(fce);
1732 fce = NULL;
1733 }
Ming Lei37276a52012-08-04 12:01:27 +08001734 spin_unlock(&fwc->name_lock);
1735
Ming Lei373304f2012-10-09 12:01:01 +08001736 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001737 async_schedule_domain(__async_dev_cache_fw_image,
1738 (void *)fce,
1739 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001740 }
1741}
1742
1743static void __device_uncache_fw_images(void)
1744{
1745 struct firmware_cache *fwc = &fw_cache;
1746 struct fw_cache_entry *fce;
1747
1748 spin_lock(&fwc->name_lock);
1749 while (!list_empty(&fwc->fw_names)) {
1750 fce = list_entry(fwc->fw_names.next,
1751 struct fw_cache_entry, list);
1752 list_del(&fce->list);
1753 spin_unlock(&fwc->name_lock);
1754
1755 uncache_firmware(fce->name);
1756 free_fw_cache_entry(fce);
1757
1758 spin_lock(&fwc->name_lock);
1759 }
1760 spin_unlock(&fwc->name_lock);
1761}
1762
1763/**
1764 * device_cache_fw_images - cache devices' firmware
1765 *
1766 * If one device called request_firmware or its nowait version
1767 * successfully before, the firmware names are recored into the
1768 * device's devres link list, so device_cache_fw_images can call
1769 * cache_firmware() to cache these firmwares for the device,
1770 * then the device driver can load its firmwares easily at
1771 * time when system is not ready to complete loading firmware.
1772 */
1773static void device_cache_fw_images(void)
1774{
1775 struct firmware_cache *fwc = &fw_cache;
Ming Lei37276a52012-08-04 12:01:27 +08001776 DEFINE_WAIT(wait);
1777
1778 pr_debug("%s\n", __func__);
1779
Ming Lei373304f2012-10-09 12:01:01 +08001780 /* cancel uncache work */
1781 cancel_delayed_work_sync(&fwc->work);
1782
Luis R. Rodriguez5d9566b2018-03-10 06:14:47 -08001783 fw_fallback_set_cache_timeout();
Ming Leiffe53f62012-08-04 12:01:28 +08001784
Ming Leiac39b3e2012-08-20 19:04:16 +08001785 mutex_lock(&fw_lock);
1786 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001787 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001788 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001789
1790 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001791 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001792
Luis R. Rodriguez5d9566b2018-03-10 06:14:47 -08001793 fw_fallback_set_default_timeout();
Ming Lei37276a52012-08-04 12:01:27 +08001794}
1795
1796/**
1797 * device_uncache_fw_images - uncache devices' firmware
1798 *
1799 * uncache all firmwares which have been cached successfully
1800 * by device_uncache_fw_images earlier
1801 */
1802static void device_uncache_fw_images(void)
1803{
1804 pr_debug("%s\n", __func__);
1805 __device_uncache_fw_images();
1806}
1807
1808static void device_uncache_fw_images_work(struct work_struct *work)
1809{
1810 device_uncache_fw_images();
1811}
1812
1813/**
1814 * device_uncache_fw_images_delay - uncache devices firmwares
1815 * @delay: number of milliseconds to delay uncache device firmwares
1816 *
1817 * uncache all devices's firmwares which has been cached successfully
1818 * by device_cache_fw_images after @delay milliseconds.
1819 */
1820static void device_uncache_fw_images_delay(unsigned long delay)
1821{
Shaibal Duttabce66182014-01-31 15:44:58 -08001822 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1823 msecs_to_jiffies(delay));
Ming Lei37276a52012-08-04 12:01:27 +08001824}
1825
Ming Lei07646d92012-08-04 12:01:29 +08001826static int fw_pm_notify(struct notifier_block *notify_block,
1827 unsigned long mode, void *unused)
1828{
1829 switch (mode) {
1830 case PM_HIBERNATION_PREPARE:
1831 case PM_SUSPEND_PREPARE:
Sebastian Capellaf8d5b9e2014-02-18 17:52:08 -08001832 case PM_RESTORE_PREPARE:
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -07001833 /*
1834 * kill pending fallback requests with a custom fallback
1835 * to avoid stalling suspend.
1836 */
1837 kill_pending_fw_fallback_reqs(true);
Ming Lei07646d92012-08-04 12:01:29 +08001838 device_cache_fw_images();
1839 break;
1840
1841 case PM_POST_SUSPEND:
1842 case PM_POST_HIBERNATION:
1843 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001844 /*
1845 * In case that system sleep failed and syscore_suspend is
1846 * not called.
1847 */
1848 mutex_lock(&fw_lock);
1849 fw_cache.state = FW_LOADER_NO_CACHE;
1850 mutex_unlock(&fw_lock);
1851
Ming Lei07646d92012-08-04 12:01:29 +08001852 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1853 break;
1854 }
1855
1856 return 0;
1857}
Ming Lei07646d92012-08-04 12:01:29 +08001858
Ming Leiac39b3e2012-08-20 19:04:16 +08001859/* stop caching firmware once syscore_suspend is reached */
1860static int fw_suspend(void)
1861{
1862 fw_cache.state = FW_LOADER_NO_CACHE;
1863 return 0;
1864}
1865
1866static struct syscore_ops fw_syscore_ops = {
1867 .suspend = fw_suspend,
1868};
Luis R. Rodrigueza67e5032017-11-20 09:45:31 -08001869
Luis R. Rodriguez59b6d852017-11-20 09:45:32 -08001870static int __init register_fw_pm_ops(void)
1871{
1872 int ret;
1873
1874 spin_lock_init(&fw_cache.name_lock);
1875 INIT_LIST_HEAD(&fw_cache.fw_names);
1876
1877 INIT_DELAYED_WORK(&fw_cache.work,
1878 device_uncache_fw_images_work);
1879
1880 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1881 ret = register_pm_notifier(&fw_cache.pm_notify);
1882 if (ret)
1883 return ret;
1884
1885 register_syscore_ops(&fw_syscore_ops);
1886
1887 return ret;
1888}
1889
Luis R. Rodrigueza67e5032017-11-20 09:45:31 -08001890static inline void unregister_fw_pm_ops(void)
1891{
1892 unregister_syscore_ops(&fw_syscore_ops);
1893 unregister_pm_notifier(&fw_cache.pm_notify);
1894}
Ming Leicfe016b2012-09-08 17:32:30 +08001895#else
1896static int fw_cache_piggyback_on_request(const char *name)
1897{
1898 return 0;
1899}
Luis R. Rodriguez59b6d852017-11-20 09:45:32 -08001900static inline int register_fw_pm_ops(void)
1901{
1902 return 0;
1903}
Luis R. Rodrigueza67e5032017-11-20 09:45:31 -08001904static inline void unregister_fw_pm_ops(void)
1905{
1906}
Ming Leicfe016b2012-09-08 17:32:30 +08001907#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001908
Ming Lei37276a52012-08-04 12:01:27 +08001909static void __init fw_cache_init(void)
1910{
1911 spin_lock_init(&fw_cache.lock);
1912 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001913 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001914}
1915
Luis R. Rodrigueza669f042017-05-02 01:31:04 -07001916static int fw_shutdown_notify(struct notifier_block *unused1,
1917 unsigned long unused2, void *unused3)
1918{
1919 /*
1920 * Kill all pending fallback requests to avoid both stalling shutdown,
1921 * and avoid a deadlock with the usermode_lock.
1922 */
1923 kill_pending_fw_fallback_reqs(false);
1924
1925 return NOTIFY_DONE;
1926}
1927
1928static struct notifier_block fw_shutdown_nb = {
1929 .notifier_call = fw_shutdown_notify,
1930};
1931
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001932static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
Luis R. Rodriguez59b6d852017-11-20 09:45:32 -08001934 int ret;
1935
1936 /* No need to unfold these on exit */
Ming Lei1f2b7952012-08-04 12:01:21 +08001937 fw_cache_init();
Luis R. Rodriguez59b6d852017-11-20 09:45:32 -08001938
1939 ret = register_fw_pm_ops();
1940 if (ret)
1941 return ret;
1942
Luis R. Rodriguez561a10b2017-11-20 09:45:34 -08001943 ret = register_reboot_notifier(&fw_shutdown_nb);
1944 if (ret)
1945 goto out;
1946
Luis R. Rodriguez6bb9cf32017-11-20 09:45:33 -08001947 return register_sysfs_loader();
Luis R. Rodriguez561a10b2017-11-20 09:45:34 -08001948
1949out:
1950 unregister_fw_pm_ops();
1951 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001953
1954static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
Luis R. Rodrigueza67e5032017-11-20 09:45:31 -08001956 unregister_fw_pm_ops();
Takashi Iwaife304142013-05-22 18:28:38 +02001957 unregister_reboot_notifier(&fw_shutdown_nb);
Luis R. Rodriguez6bb9cf32017-11-20 09:45:33 -08001958 unregister_sysfs_loader();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
1960
Shaohua Lia30a6a22006-09-27 01:50:52 -07001961fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962module_exit(firmware_class_exit);