blob: 9757f9fff01e281dd2b21a68d6d888eb376b79ec [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.
Luis R. Rodrigueze05cb732018-03-10 06:14:48 -0800269 * @old_timeout: for internal use
270 * @loading_timeout: the timeout to wait for the fallback mechanism before
271 * giving up, in seconds.
Luis R. Rodriguezb2e9a852018-03-10 06:14:46 -0800272 */
273struct firmware_fallback_config {
Luis R. Rodrigueze05cb732018-03-10 06:14:48 -0800274 const bool force_sysfs_fallback;
275 int old_timeout;
276 int loading_timeout;
Luis R. Rodriguezb2e9a852018-03-10 06:14:46 -0800277};
278
Luis R. Rodrigueze05cb732018-03-10 06:14:48 -0800279static struct firmware_fallback_config fw_fallback_config = {
Luis R. Rodriguezb2e9a852018-03-10 06:14:46 -0800280 .force_sysfs_fallback = IS_ENABLED(CONFIG_FW_LOADER_USER_HELPER_FALLBACK),
Luis R. Rodrigueze05cb732018-03-10 06:14:48 -0800281 .loading_timeout = 60,
282 .old_timeout = 60,
Luis R. Rodriguezb2e9a852018-03-10 06:14:46 -0800283};
284
Luis R. Rodrigueze05cb732018-03-10 06:14:48 -0800285/* These getters are vetted to use int properly */
286static inline int __firmware_loading_timeout(void)
287{
288 return fw_fallback_config.loading_timeout;
289}
290
291/* These setters are vetted to use int properly */
292static void __fw_fallback_set_timeout(int timeout)
293{
294 fw_fallback_config.loading_timeout = timeout;
295}
Luis R. Rodriguez5d9566b2018-03-10 06:14:47 -0800296
297static inline long firmware_loading_timeout(void)
298{
Luis R. Rodrigueze05cb732018-03-10 06:14:48 -0800299 return __firmware_loading_timeout() > 0 ?
300 __firmware_loading_timeout() * HZ : MAX_JIFFY_OFFSET;
Luis R. Rodriguez5d9566b2018-03-10 06:14:47 -0800301}
302
303/*
304 * use small loading timeout for caching devices' firmware because all these
305 * firmware images have been loaded successfully at lease once, also system is
306 * ready for completing firmware loading now. The maximum size of firmware in
307 * current distributions is about 2M bytes, so 10 secs should be enough.
308 */
309static void fw_fallback_set_cache_timeout(void)
310{
Luis R. Rodrigueze05cb732018-03-10 06:14:48 -0800311 fw_fallback_config.old_timeout = __firmware_loading_timeout();
312 __fw_fallback_set_timeout(10);
Luis R. Rodriguez5d9566b2018-03-10 06:14:47 -0800313}
314
315/* Restores the timeout to the value last configured during normal operation */
316static void fw_fallback_set_default_timeout(void)
317{
Luis R. Rodrigueze05cb732018-03-10 06:14:48 -0800318 __fw_fallback_set_timeout(fw_fallback_config.old_timeout);
Luis R. Rodriguez5d9566b2018-03-10 06:14:47 -0800319}
320
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800321static inline bool fw_sysfs_done(struct fw_priv *fw_priv)
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800322{
323 return __fw_state_check(fw_priv, FW_STATUS_DONE);
324}
325
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800326static inline bool fw_sysfs_loading(struct fw_priv *fw_priv)
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800327{
328 return __fw_state_check(fw_priv, FW_STATUS_LOADING);
329}
330
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800331static inline int fw_sysfs_wait_timeout(struct fw_priv *fw_priv, long timeout)
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800332{
333 return __fw_state_wait_common(fw_priv, timeout);
334}
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800335
336#endif /* CONFIG_FW_LOADER_USER_HELPER */
337
338static int fw_cache_piggyback_on_request(const char *name);
339
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800340static struct fw_priv *__allocate_fw_priv(const char *fw_name,
341 struct firmware_cache *fwc,
342 void *dbuf, size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800343{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800344 struct fw_priv *fw_priv;
Ming Lei1f2b7952012-08-04 12:01:21 +0800345
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800346 fw_priv = kzalloc(sizeof(*fw_priv), GFP_ATOMIC);
347 if (!fw_priv)
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700348 return NULL;
349
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800350 fw_priv->fw_name = kstrdup_const(fw_name, GFP_ATOMIC);
351 if (!fw_priv->fw_name) {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800352 kfree(fw_priv);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700353 return NULL;
354 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800355
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800356 kref_init(&fw_priv->ref);
357 fw_priv->fwc = fwc;
358 fw_priv->data = dbuf;
359 fw_priv->allocated_size = size;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800360 fw_state_init(fw_priv);
Takashi Iwaife304142013-05-22 18:28:38 +0200361#ifdef CONFIG_FW_LOADER_USER_HELPER
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800362 INIT_LIST_HEAD(&fw_priv->pending_list);
Takashi Iwaife304142013-05-22 18:28:38 +0200363#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800364
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800365 pr_debug("%s: fw-%s fw_priv=%p\n", __func__, fw_name, fw_priv);
Ming Lei1f2b7952012-08-04 12:01:21 +0800366
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800367 return fw_priv;
Ming Lei1f2b7952012-08-04 12:01:21 +0800368}
369
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800370static struct fw_priv *__lookup_fw_priv(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +0800371{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800372 struct fw_priv *tmp;
Ming Lei2887b392012-08-04 12:01:22 +0800373 struct firmware_cache *fwc = &fw_cache;
374
375 list_for_each_entry(tmp, &fwc->head, list)
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800376 if (!strcmp(tmp->fw_name, fw_name))
Ming Lei2887b392012-08-04 12:01:22 +0800377 return tmp;
378 return NULL;
379}
380
Luis R. Rodriguez30172be2017-07-20 13:13:41 -0700381/* Returns 1 for batching firmware requests with the same name */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800382static int alloc_lookup_fw_priv(const char *fw_name,
383 struct firmware_cache *fwc,
384 struct fw_priv **fw_priv, void *dbuf,
385 size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800386{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800387 struct fw_priv *tmp;
Ming Lei1f2b7952012-08-04 12:01:21 +0800388
389 spin_lock(&fwc->lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800390 tmp = __lookup_fw_priv(fw_name);
Ming Lei2887b392012-08-04 12:01:22 +0800391 if (tmp) {
392 kref_get(&tmp->ref);
393 spin_unlock(&fwc->lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800394 *fw_priv = tmp;
395 pr_debug("batched request - sharing the same struct fw_priv and lookup for multiple requests\n");
Ming Lei2887b392012-08-04 12:01:22 +0800396 return 1;
397 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800398 tmp = __allocate_fw_priv(fw_name, fwc, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +0800399 if (tmp)
400 list_add(&tmp->list, &fwc->head);
401 spin_unlock(&fwc->lock);
402
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800403 *fw_priv = tmp;
Ming Lei1f2b7952012-08-04 12:01:21 +0800404
405 return tmp ? 0 : -ENOMEM;
406}
407
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800408static void __free_fw_priv(struct kref *ref)
Bart Van Assche98233b22014-01-04 14:20:36 +0100409 __releases(&fwc->lock)
Ming Lei1f2b7952012-08-04 12:01:21 +0800410{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800411 struct fw_priv *fw_priv = to_fw_priv(ref);
412 struct firmware_cache *fwc = fw_priv->fwc;
Ming Lei1f2b7952012-08-04 12:01:21 +0800413
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800414 pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800415 __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800416 (unsigned int)fw_priv->size);
Ming Lei1f2b7952012-08-04 12:01:21 +0800417
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800418 list_del(&fw_priv->list);
Ming Lei1f2b7952012-08-04 12:01:21 +0800419 spin_unlock(&fwc->lock);
420
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100421#ifdef CONFIG_FW_LOADER_USER_HELPER
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800422 if (fw_priv->is_paged_buf) {
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100423 int i;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800424 vunmap(fw_priv->data);
425 for (i = 0; i < fw_priv->nr_pages; i++)
426 __free_page(fw_priv->pages[i]);
427 vfree(fw_priv->pages);
Ming Lei746058f2012-10-09 12:01:03 +0800428 } else
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100429#endif
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800430 if (!fw_priv->allocated_size)
431 vfree(fw_priv->data);
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800432 kfree_const(fw_priv->fw_name);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800433 kfree(fw_priv);
Ming Lei1f2b7952012-08-04 12:01:21 +0800434}
435
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800436static void free_fw_priv(struct fw_priv *fw_priv)
Ming Lei1f2b7952012-08-04 12:01:21 +0800437{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800438 struct firmware_cache *fwc = fw_priv->fwc;
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800439 spin_lock(&fwc->lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800440 if (!kref_put(&fw_priv->ref, __free_fw_priv))
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800441 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800442}
443
Ming Lei746058f2012-10-09 12:01:03 +0800444/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800445static char fw_path_para[256];
446static const char * const fw_path[] = {
447 fw_path_para,
Ming Lei746058f2012-10-09 12:01:03 +0800448 "/lib/firmware/updates/" UTS_RELEASE,
449 "/lib/firmware/updates",
450 "/lib/firmware/" UTS_RELEASE,
451 "/lib/firmware"
452};
453
Ming Lei27602842012-11-03 17:47:58 +0800454/*
455 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
456 * from kernel command line because firmware_class is generally built in
457 * kernel instead of module.
458 */
459module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
460MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
461
Stephen Boyda098ecd2016-08-02 14:04:28 -0700462static int
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800463fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv)
Ming Lei746058f2012-10-09 12:01:03 +0800464{
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500465 loff_t size;
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700466 int i, len;
Neil Horman3e358ac2013-09-06 15:36:08 -0400467 int rc = -ENOENT;
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700468 char *path;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700469 enum kernel_read_file_id id = READING_FIRMWARE;
470 size_t msize = INT_MAX;
471
472 /* Already populated data member means we're loading into a buffer */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800473 if (fw_priv->data) {
Stephen Boyda098ecd2016-08-02 14:04:28 -0700474 id = READING_FIRMWARE_PREALLOC_BUFFER;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800475 msize = fw_priv->allocated_size;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700476 }
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700477
478 path = __getname();
479 if (!path)
480 return -ENOMEM;
Ming Lei746058f2012-10-09 12:01:03 +0800481
482 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
Ming Lei27602842012-11-03 17:47:58 +0800483 /* skip the unset customized path */
484 if (!fw_path[i][0])
485 continue;
486
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700487 len = snprintf(path, PATH_MAX, "%s/%s",
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800488 fw_path[i], fw_priv->fw_name);
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700489 if (len >= PATH_MAX) {
490 rc = -ENAMETOOLONG;
491 break;
492 }
Ming Lei746058f2012-10-09 12:01:03 +0800493
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800494 fw_priv->size = 0;
495 rc = kernel_read_file_from_path(path, &fw_priv->data, &size,
496 msize, id);
Kees Cook4b2530d2016-02-04 13:15:02 -0800497 if (rc) {
Luis R. Rodriguez8e516aa2016-02-28 21:57:55 +0100498 if (rc == -ENOENT)
499 dev_dbg(device, "loading %s failed with error %d\n",
500 path, rc);
501 else
502 dev_warn(device, "loading %s failed with error %d\n",
503 path, rc);
Kees Cook4b2530d2016-02-04 13:15:02 -0800504 continue;
505 }
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800506 dev_dbg(device, "direct-loading %s\n", fw_priv->fw_name);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800507 fw_priv->size = size;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800508 fw_state_done(fw_priv);
Kees Cook4b2530d2016-02-04 13:15:02 -0800509 break;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100510 }
Kees Cook4b2530d2016-02-04 13:15:02 -0800511 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100512
Neil Horman3e358ac2013-09-06 15:36:08 -0400513 return rc;
Ming Lei746058f2012-10-09 12:01:03 +0800514}
515
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100516/* firmware holds the ownership of pages */
517static void firmware_free_data(const struct firmware *fw)
518{
519 /* Loaded directly? */
520 if (!fw->priv) {
521 vfree(fw->data);
522 return;
523 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800524 free_fw_priv(fw->priv);
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100525}
526
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100527/* store the pages buffer info firmware from buf */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800528static void fw_set_page_data(struct fw_priv *fw_priv, struct firmware *fw)
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100529{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800530 fw->priv = fw_priv;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100531#ifdef CONFIG_FW_LOADER_USER_HELPER
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800532 fw->pages = fw_priv->pages;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100533#endif
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800534 fw->size = fw_priv->size;
535 fw->data = fw_priv->data;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100536
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800537 pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800538 __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800539 (unsigned int)fw_priv->size);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100540}
541
542#ifdef CONFIG_PM_SLEEP
543static void fw_name_devm_release(struct device *dev, void *res)
544{
545 struct fw_name_devm *fwn = res;
546
547 if (fwn->magic == (unsigned long)&fw_cache)
548 pr_debug("%s: fw_name-%s devm-%p released\n",
549 __func__, fwn->name, res);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700550 kfree_const(fwn->name);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100551}
552
553static int fw_devm_match(struct device *dev, void *res,
554 void *match_data)
555{
556 struct fw_name_devm *fwn = res;
557
558 return (fwn->magic == (unsigned long)&fw_cache) &&
559 !strcmp(fwn->name, match_data);
560}
561
562static struct fw_name_devm *fw_find_devm_name(struct device *dev,
563 const char *name)
564{
565 struct fw_name_devm *fwn;
566
567 fwn = devres_find(dev, fw_name_devm_release,
568 fw_devm_match, (void *)name);
569 return fwn;
570}
571
572/* add firmware name into devres list */
573static int fw_add_devm_name(struct device *dev, const char *name)
574{
575 struct fw_name_devm *fwn;
576
577 fwn = fw_find_devm_name(dev, name);
578 if (fwn)
579 return 1;
580
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700581 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
582 GFP_KERNEL);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100583 if (!fwn)
584 return -ENOMEM;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700585 fwn->name = kstrdup_const(name, GFP_KERNEL);
586 if (!fwn->name) {
Vladimir Zapolskiya885de62015-07-29 23:26:28 +0300587 devres_free(fwn);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700588 return -ENOMEM;
589 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100590
591 fwn->magic = (unsigned long)&fw_cache;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100592 devres_add(dev, fwn);
593
594 return 0;
595}
596#else
597static int fw_add_devm_name(struct device *dev, const char *name)
598{
599 return 0;
600}
601#endif
602
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800603static int assign_fw(struct firmware *fw, struct device *device,
604 unsigned int opt_flags)
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700605{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800606 struct fw_priv *fw_priv = fw->priv;
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700607
608 mutex_lock(&fw_lock);
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800609 if (!fw_priv->size || fw_state_is_aborted(fw_priv)) {
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700610 mutex_unlock(&fw_lock);
611 return -ENOENT;
612 }
613
614 /*
615 * add firmware name into devres list so that we can auto cache
616 * and uncache firmware for device.
617 *
618 * device may has been deleted already, but the problem
619 * should be fixed in devres or driver core.
620 */
621 /* don't cache firmware handled without uevent */
622 if (device && (opt_flags & FW_OPT_UEVENT) &&
623 !(opt_flags & FW_OPT_NOCACHE))
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800624 fw_add_devm_name(device, fw_priv->fw_name);
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700625
626 /*
627 * After caching firmware image is started, let it piggyback
628 * on request firmware.
629 */
630 if (!(opt_flags & FW_OPT_NOCACHE) &&
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800631 fw_priv->fwc->state == FW_LOADER_START_CACHE) {
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800632 if (fw_cache_piggyback_on_request(fw_priv->fw_name))
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800633 kref_get(&fw_priv->ref);
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700634 }
635
636 /* pass the pages buffer to driver at the last minute */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800637 fw_set_page_data(fw_priv, fw);
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700638 mutex_unlock(&fw_lock);
639 return 0;
640}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100641
642/*
643 * user-mode helper code
644 */
645#ifdef CONFIG_FW_LOADER_USER_HELPER
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800646struct fw_sysfs {
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100647 bool nowait;
648 struct device dev;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800649 struct fw_priv *fw_priv;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100650 struct firmware *fw;
651};
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100652
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800653static struct fw_sysfs *to_fw_sysfs(struct device *dev)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700654{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800655 return container_of(dev, struct fw_sysfs, dev);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700656}
657
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800658static void __fw_load_abort(struct fw_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Ming Lei87597932013-06-15 16:36:38 +0800660 /*
661 * There is a small window in which user can write to 'loading'
662 * between loading done and disappearance of 'loading'
663 */
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800664 if (fw_sysfs_done(fw_priv))
Ming Lei87597932013-06-15 16:36:38 +0800665 return;
666
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800667 list_del_init(&fw_priv->pending_list);
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800668 fw_state_aborted(fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800671static void fw_load_abort(struct fw_sysfs *fw_sysfs)
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700672{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800673 struct fw_priv *fw_priv = fw_sysfs->fw_priv;
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700674
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800675 __fw_load_abort(fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
Takashi Iwaife304142013-05-22 18:28:38 +0200678static LIST_HEAD(pending_fw_head);
679
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -0700680static void kill_pending_fw_fallback_reqs(bool only_kill_custom)
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700681{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800682 struct fw_priv *fw_priv;
683 struct fw_priv *next;
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700684
685 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800686 list_for_each_entry_safe(fw_priv, next, &pending_fw_head,
687 pending_list) {
688 if (!fw_priv->need_uevent || !only_kill_custom)
689 __fw_load_abort(fw_priv);
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700690 }
691 mutex_unlock(&fw_lock);
692}
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700693
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700694static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
695 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Luis R. Rodrigueze05cb732018-03-10 06:14:48 -0800697 return sprintf(buf, "%d\n", __firmware_loading_timeout());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
700/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800701 * firmware_timeout_store - set number of seconds to wait for firmware
702 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800703 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800704 * @buf: buffer to scan for timeout value
705 * @count: number of bytes in @buf
706 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800708 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 * firmware will be provided.
710 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800711 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 **/
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700713static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
714 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Luis R. Rodrigueze05cb732018-03-10 06:14:48 -0800716 int tmp_loading_timeout = simple_strtol(buf, NULL, 10);
717
718 if (tmp_loading_timeout < 0)
719 tmp_loading_timeout = 0;
720
721 __fw_fallback_set_timeout(tmp_loading_timeout);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return count;
724}
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100725static CLASS_ATTR_RW(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100727static struct attribute *firmware_class_attrs[] = {
728 &class_attr_timeout.attr,
729 NULL,
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800730};
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100731ATTRIBUTE_GROUPS(firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800733static void fw_dev_release(struct device *dev)
734{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800735 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800736
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800737 kfree(fw_sysfs);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800738}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800740static int do_firmware_uevent(struct fw_sysfs *fw_sysfs, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800742 if (add_uevent_var(env, "FIRMWARE=%s", fw_sysfs->fw_priv->fw_name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return -ENOMEM;
Luis R. Rodrigueze05cb732018-03-10 06:14:48 -0800744 if (add_uevent_var(env, "TIMEOUT=%i", __firmware_loading_timeout()))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700745 return -ENOMEM;
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800746 if (add_uevent_var(env, "ASYNC=%d", fw_sysfs->nowait))
Johannes Berge9045f92010-03-29 17:57:20 +0200747 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 return 0;
750}
751
Linus Torvalds6f957722015-07-09 11:20:01 -0700752static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
753{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800754 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Linus Torvalds6f957722015-07-09 11:20:01 -0700755 int err = 0;
756
757 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800758 if (fw_sysfs->fw_priv)
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800759 err = do_firmware_uevent(fw_sysfs, env);
Linus Torvalds6f957722015-07-09 11:20:01 -0700760 mutex_unlock(&fw_lock);
761 return err;
762}
763
Adrian Bunk1b81d662006-05-20 15:00:16 -0700764static struct class firmware_class = {
765 .name = "firmware",
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100766 .class_groups = firmware_class_groups,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700767 .dev_uevent = firmware_uevent,
768 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700769};
770
Luis R. Rodriguez6bb9cf32017-11-20 09:45:33 -0800771static inline int register_sysfs_loader(void)
772{
773 return class_register(&firmware_class);
774}
775
776static inline void unregister_sysfs_loader(void)
777{
778 class_unregister(&firmware_class);
779}
780
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700781static ssize_t firmware_loading_show(struct device *dev,
782 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800784 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Ming Lei87597932013-06-15 16:36:38 +0800785 int loading = 0;
786
787 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800788 if (fw_sysfs->fw_priv)
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800789 loading = fw_sysfs_loading(fw_sysfs->fw_priv);
Ming Lei87597932013-06-15 16:36:38 +0800790 mutex_unlock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return sprintf(buf, "%d\n", loading);
793}
794
David Woodhouse6e03a202009-04-09 22:04:07 -0700795/* Some architectures don't have PAGE_KERNEL_RO */
796#ifndef PAGE_KERNEL_RO
797#define PAGE_KERNEL_RO PAGE_KERNEL
798#endif
Ming Lei253c9242012-10-09 12:01:02 +0800799
800/* one pages buffer should be mapped/unmapped only once */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800801static int map_fw_priv_pages(struct fw_priv *fw_priv)
Ming Lei253c9242012-10-09 12:01:02 +0800802{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800803 if (!fw_priv->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800804 return 0;
805
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800806 vunmap(fw_priv->data);
807 fw_priv->data = vmap(fw_priv->pages, fw_priv->nr_pages, 0,
808 PAGE_KERNEL_RO);
809 if (!fw_priv->data)
Ming Lei253c9242012-10-09 12:01:02 +0800810 return -ENOMEM;
811 return 0;
812}
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800815 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700816 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800817 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800818 * @buf: buffer to scan for loading control value
819 * @count: number of bytes in @buf
820 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 * The relevant values are:
822 *
823 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800824 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 * -1: Conclude the load with an error and discard any written data.
826 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700827static ssize_t firmware_loading_store(struct device *dev,
828 struct device_attribute *attr,
829 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800831 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800832 struct fw_priv *fw_priv;
Kees Cook6593d922014-02-25 13:06:00 -0800833 ssize_t written = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700835 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Neil Hormaneea915b2012-01-02 15:31:23 -0500837 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800838 fw_priv = fw_sysfs->fw_priv;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800839 if (fw_state_is_aborted(fw_priv))
Neil Hormaneea915b2012-01-02 15:31:23 -0500840 goto out;
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 switch (loading) {
843 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800844 /* discarding any previous partial load */
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800845 if (!fw_sysfs_done(fw_priv)) {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800846 for (i = 0; i < fw_priv->nr_pages; i++)
847 __free_page(fw_priv->pages[i]);
848 vfree(fw_priv->pages);
849 fw_priv->pages = NULL;
850 fw_priv->page_array_size = 0;
851 fw_priv->nr_pages = 0;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800852 fw_state_start(fw_priv);
Ming Lei28eefa72012-08-04 12:01:17 +0800853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 break;
855 case 0:
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800856 if (fw_sysfs_loading(fw_priv)) {
Kees Cook6593d922014-02-25 13:06:00 -0800857 int rc;
858
Ming Lei253c9242012-10-09 12:01:02 +0800859 /*
860 * Several loading requests may be pending on
861 * one same firmware buf, so let all requests
862 * see the mapped 'buf->data' once the loading
863 * is completed.
864 * */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800865 rc = map_fw_priv_pages(fw_priv);
Kees Cook6593d922014-02-25 13:06:00 -0800866 if (rc)
zhang jun2b1278c2014-02-13 15:18:47 +0800867 dev_err(dev, "%s: map pages failed\n",
868 __func__);
Kees Cook6593d922014-02-25 13:06:00 -0800869 else
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500870 rc = security_kernel_post_read_file(NULL,
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800871 fw_priv->data, fw_priv->size,
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500872 READING_FIRMWARE);
Kees Cook6593d922014-02-25 13:06:00 -0800873
874 /*
875 * Same logic as fw_load_abort, only the DONE bit
876 * is ignored and we set ABORT only on failure.
877 */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800878 list_del_init(&fw_priv->pending_list);
Kees Cook6593d922014-02-25 13:06:00 -0800879 if (rc) {
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800880 fw_state_aborted(fw_priv);
Kees Cook6593d922014-02-25 13:06:00 -0800881 written = rc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100882 } else {
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800883 fw_state_done(fw_priv);
Kees Cook6593d922014-02-25 13:06:00 -0800884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 break;
886 }
887 /* fallthrough */
888 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700889 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /* fallthrough */
891 case -1:
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800892 fw_load_abort(fw_sysfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 break;
894 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500895out:
896 mutex_unlock(&fw_lock);
Kees Cook6593d922014-02-25 13:06:00 -0800897 return written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700900static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800902static void firmware_rw_data(struct fw_priv *fw_priv, char *buffer,
Stephen Boyda098ecd2016-08-02 14:04:28 -0700903 loff_t offset, size_t count, bool read)
904{
905 if (read)
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800906 memcpy(buffer, fw_priv->data + offset, count);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700907 else
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800908 memcpy(fw_priv->data + offset, buffer, count);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700909}
910
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800911static void firmware_rw(struct fw_priv *fw_priv, char *buffer,
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700912 loff_t offset, size_t count, bool read)
913{
914 while (count) {
915 void *page_data;
916 int page_nr = offset >> PAGE_SHIFT;
917 int page_ofs = offset & (PAGE_SIZE-1);
918 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
919
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800920 page_data = kmap(fw_priv->pages[page_nr]);
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700921
922 if (read)
923 memcpy(buffer, page_data + page_ofs, page_cnt);
924 else
925 memcpy(page_data + page_ofs, buffer, page_cnt);
926
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800927 kunmap(fw_priv->pages[page_nr]);
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700928 buffer += page_cnt;
929 offset += page_cnt;
930 count -= page_cnt;
931 }
932}
933
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700934static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
935 struct bin_attribute *bin_attr,
936 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200938 struct device *dev = kobj_to_dev(kobj);
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800939 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800940 struct fw_priv *fw_priv;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700941 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Laura Garciacad1e552006-05-23 23:22:38 +0200943 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800944 fw_priv = fw_sysfs->fw_priv;
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800945 if (!fw_priv || fw_sysfs_done(fw_priv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 ret_count = -ENODEV;
947 goto out;
948 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800949 if (offset > fw_priv->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200950 ret_count = 0;
951 goto out;
952 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800953 if (count > fw_priv->size - offset)
954 count = fw_priv->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700955
956 ret_count = count;
957
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800958 if (fw_priv->data)
959 firmware_rw_data(fw_priv, buffer, offset, count, true);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700960 else
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800961 firmware_rw(fw_priv, buffer, offset, count, true);
David Woodhouse6e03a202009-04-09 22:04:07 -0700962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963out:
Laura Garciacad1e552006-05-23 23:22:38 +0200964 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return ret_count;
966}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800967
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800968static int fw_realloc_pages(struct fw_sysfs *fw_sysfs, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800970 struct fw_priv *fw_priv= fw_sysfs->fw_priv;
Fabian Fredericka76040d2014-07-01 21:13:30 +0200971 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
David Woodhouse6e03a202009-04-09 22:04:07 -0700973 /* If the array of pages is too small, grow it... */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800974 if (fw_priv->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700975 int new_array_size = max(pages_needed,
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800976 fw_priv->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700977 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Chen Feng10a3fbf2015-12-16 17:03:15 +0800979 new_pages = vmalloc(new_array_size * sizeof(void *));
David Woodhouse6e03a202009-04-09 22:04:07 -0700980 if (!new_pages) {
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800981 fw_load_abort(fw_sysfs);
David Woodhouse6e03a202009-04-09 22:04:07 -0700982 return -ENOMEM;
983 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800984 memcpy(new_pages, fw_priv->pages,
985 fw_priv->page_array_size * sizeof(void *));
986 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
987 (new_array_size - fw_priv->page_array_size));
988 vfree(fw_priv->pages);
989 fw_priv->pages = new_pages;
990 fw_priv->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700992
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800993 while (fw_priv->nr_pages < pages_needed) {
994 fw_priv->pages[fw_priv->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700995 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
996
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800997 if (!fw_priv->pages[fw_priv->nr_pages]) {
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800998 fw_load_abort(fw_sysfs);
David Woodhouse6e03a202009-04-09 22:04:07 -0700999 return -ENOMEM;
1000 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001001 fw_priv->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return 0;
1004}
1005
1006/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001007 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -07001008 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -07001009 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -07001010 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001011 * @buffer: buffer being written
1012 * @offset: buffer offset for write in total data store area
1013 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001015 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 * the driver as a firmware image.
1017 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001018static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
1019 struct bin_attribute *bin_attr,
1020 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001022 struct device *dev = kobj_to_dev(kobj);
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001023 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001024 struct fw_priv *fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 ssize_t retval;
1026
1027 if (!capable(CAP_SYS_RAWIO))
1028 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -07001029
Laura Garciacad1e552006-05-23 23:22:38 +02001030 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001031 fw_priv = fw_sysfs->fw_priv;
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -08001032 if (!fw_priv || fw_sysfs_done(fw_priv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 retval = -ENODEV;
1034 goto out;
1035 }
Ming Lei65710cb2012-08-04 12:01:16 +08001036
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001037 if (fw_priv->data) {
1038 if (offset + count > fw_priv->allocated_size) {
Stephen Boyda098ecd2016-08-02 14:04:28 -07001039 retval = -ENOMEM;
1040 goto out;
1041 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001042 firmware_rw_data(fw_priv, buffer, offset, count, false);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001043 retval = count;
1044 } else {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001045 retval = fw_realloc_pages(fw_sysfs, offset + count);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001046 if (retval)
1047 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Stephen Boyda098ecd2016-08-02 14:04:28 -07001049 retval = count;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001050 firmware_rw(fw_priv, buffer, offset, count, false);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001051 }
David Woodhouse6e03a202009-04-09 22:04:07 -07001052
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001053 fw_priv->size = max_t(size_t, offset + count, fw_priv->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054out:
Laura Garciacad1e552006-05-23 23:22:38 +02001055 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return retval;
1057}
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001058
Dmitry Torokhov0983ca22010-06-04 00:54:37 -07001059static struct bin_attribute firmware_attr_data = {
1060 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 .size = 0,
1062 .read = firmware_data_read,
1063 .write = firmware_data_write,
1064};
1065
Takashi Iwai46239902015-02-04 15:18:07 +01001066static struct attribute *fw_dev_attrs[] = {
1067 &dev_attr_loading.attr,
1068 NULL
1069};
1070
1071static struct bin_attribute *fw_dev_bin_attrs[] = {
1072 &firmware_attr_data,
1073 NULL
1074};
1075
1076static const struct attribute_group fw_dev_attr_group = {
1077 .attrs = fw_dev_attrs,
1078 .bin_attrs = fw_dev_bin_attrs,
1079};
1080
1081static const struct attribute_group *fw_dev_attr_groups[] = {
1082 &fw_dev_attr_group,
1083 NULL
1084};
1085
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001086static struct fw_sysfs *
Stephen Boyddddb5542012-03-28 23:30:43 +02001087fw_create_instance(struct firmware *firmware, const char *fw_name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001088 struct device *device, unsigned int opt_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001090 struct fw_sysfs *fw_sysfs;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001091 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001093 fw_sysfs = kzalloc(sizeof(*fw_sysfs), GFP_KERNEL);
1094 if (!fw_sysfs) {
1095 fw_sysfs = ERR_PTR(-ENOMEM);
Ming Lei12446912012-08-04 12:01:20 +08001096 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001099 fw_sysfs->nowait = !!(opt_flags & FW_OPT_NOWAIT);
1100 fw_sysfs->fw = firmware;
1101 f_dev = &fw_sysfs->dev;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001102
1103 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +08001104 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -07001105 f_dev->parent = device;
1106 f_dev->class = &firmware_class;
Takashi Iwai46239902015-02-04 15:18:07 +01001107 f_dev->groups = fw_dev_attr_groups;
Ming Lei12446912012-08-04 12:01:20 +08001108exit:
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001109 return fw_sysfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001111
1112/* load a firmware via user helper */
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001113static int _request_firmware_load(struct fw_sysfs *fw_sysfs,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001114 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001115{
1116 int retval = 0;
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001117 struct device *f_dev = &fw_sysfs->dev;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001118 struct fw_priv *fw_priv = fw_sysfs->fw_priv;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001119
1120 /* fall back on userspace loading */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001121 if (!fw_priv->data)
1122 fw_priv->is_paged_buf = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001123
1124 dev_set_uevent_suppress(f_dev, true);
1125
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001126 retval = device_add(f_dev);
1127 if (retval) {
1128 dev_err(f_dev, "%s: device_register failed\n", __func__);
1129 goto err_put_dev;
1130 }
1131
Maxime Bizon1eeeef12013-08-29 20:28:13 +02001132 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001133 list_add(&fw_priv->pending_list, &pending_fw_head);
Maxime Bizon1eeeef12013-08-29 20:28:13 +02001134 mutex_unlock(&fw_lock);
1135
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001136 if (opt_flags & FW_OPT_UEVENT) {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001137 fw_priv->need_uevent = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001138 dev_set_uevent_suppress(f_dev, false);
Luis R. Rodriguez31034f22017-11-20 10:23:49 -08001139 dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_name);
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001140 kobject_uevent(&fw_sysfs->dev.kobj, KOBJ_ADD);
Ming Lei68ff2a02015-01-13 00:02:01 +08001141 } else {
1142 timeout = MAX_JIFFY_OFFSET;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001143 }
1144
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -08001145 retval = fw_sysfs_wait_timeout(fw_priv, timeout);
Bjorn Andersson5d47ec02016-12-06 17:01:45 -08001146 if (retval < 0) {
Ming Lei0cb64242015-01-13 00:01:35 +08001147 mutex_lock(&fw_lock);
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001148 fw_load_abort(fw_sysfs);
Ming Lei0cb64242015-01-13 00:01:35 +08001149 mutex_unlock(&fw_lock);
1150 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001151
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -08001152 if (fw_state_is_aborted(fw_priv)) {
Luis R. Rodriguez76098b32017-07-20 13:13:39 -07001153 if (retval == -ERESTARTSYS)
1154 retval = -EINTR;
1155 else
1156 retval = -EAGAIN;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001157 } else if (fw_priv->is_paged_buf && !fw_priv->data)
zhang jun2b1278c2014-02-13 15:18:47 +08001158 retval = -ENOMEM;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001159
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001160 device_del(f_dev);
1161err_put_dev:
1162 put_device(f_dev);
1163 return retval;
1164}
1165
1166static int fw_load_from_user_helper(struct firmware *firmware,
1167 const char *name, struct device *device,
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001168 unsigned int opt_flags)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001169{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001170 struct fw_sysfs *fw_sysfs;
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001171 long timeout;
1172 int ret;
1173
1174 timeout = firmware_loading_timeout();
1175 if (opt_flags & FW_OPT_NOWAIT) {
1176 timeout = usermodehelper_read_lock_wait(timeout);
1177 if (!timeout) {
1178 dev_dbg(device, "firmware: %s loading timed out\n",
1179 name);
1180 return -EBUSY;
1181 }
1182 } else {
1183 ret = usermodehelper_read_trylock();
1184 if (WARN_ON(ret)) {
1185 dev_err(device, "firmware: %s will not be loaded\n",
1186 name);
1187 return ret;
1188 }
1189 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001190
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001191 fw_sysfs = fw_create_instance(firmware, name, device, opt_flags);
1192 if (IS_ERR(fw_sysfs)) {
1193 ret = PTR_ERR(fw_sysfs);
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001194 goto out_unlock;
1195 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001196
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001197 fw_sysfs->fw_priv = firmware->priv;
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001198 ret = _request_firmware_load(fw_sysfs, opt_flags, timeout);
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001199
1200 if (!ret)
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001201 ret = assign_fw(firmware, device, opt_flags);
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001202
1203out_unlock:
1204 usermodehelper_read_unlock();
1205
1206 return ret;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001207}
Ming Leiddf1f062013-06-04 10:01:13 +08001208
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001209static bool fw_force_sysfs_fallback(unsigned int opt_flags)
1210{
Luis R. Rodriguezb2e9a852018-03-10 06:14:46 -08001211 if (fw_fallback_config.force_sysfs_fallback)
1212 return true;
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001213 if (!(opt_flags & FW_OPT_USERHELPER))
1214 return false;
1215 return true;
1216}
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001217
1218static bool fw_run_sysfs_fallback(unsigned int opt_flags)
1219{
1220 if ((opt_flags & FW_OPT_NOFALLBACK))
1221 return false;
1222
1223 return fw_force_sysfs_fallback(opt_flags);
1224}
1225
Luis R. Rodriguez25d39132017-11-20 10:23:57 -08001226static int fw_sysfs_fallback(struct firmware *fw, const char *name,
1227 struct device *device,
1228 unsigned int opt_flags,
1229 int ret)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001230{
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001231 if (!fw_run_sysfs_fallback(opt_flags))
Luis R. Rodriguez25d39132017-11-20 10:23:57 -08001232 return ret;
1233
1234 dev_warn(device, "Falling back to user helper\n");
1235 return fw_load_from_user_helper(fw, name, device, opt_flags);
1236}
1237#else /* CONFIG_FW_LOADER_USER_HELPER */
1238static int fw_sysfs_fallback(struct firmware *fw, const char *name,
1239 struct device *device,
1240 unsigned int opt_flags,
1241 int ret)
1242{
1243 /* Keep carrying over the same error */
1244 return ret;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001245}
Takashi Iwai807be032013-01-31 11:13:57 +01001246
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -07001247static inline void kill_pending_fw_fallback_reqs(bool only_kill_custom) { }
Luis R. Rodriguez5d9566b2018-03-10 06:14:47 -08001248static inline void fw_fallback_set_cache_timeout(void) { }
1249static inline void fw_fallback_set_default_timeout(void) { }
Ming Leiddf1f062013-06-04 10:01:13 +08001250
Luis R. Rodriguez6bb9cf32017-11-20 09:45:33 -08001251static inline int register_sysfs_loader(void)
1252{
1253 return 0;
1254}
1255
1256static inline void unregister_sysfs_loader(void)
1257{
1258}
1259
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001260#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001262/* prepare firmware and firmware_buf structs;
1263 * return 0 if a firmware is already assigned, 1 if need to load one,
1264 * or a negative error code
1265 */
1266static int
1267_request_firmware_prepare(struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001268 struct device *device, void *dbuf, size_t size)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001269{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 struct firmware *firmware;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001271 struct fw_priv *fw_priv;
Ming Lei1f2b7952012-08-04 12:01:21 +08001272 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Jiri Slaby4aed0642005-09-13 01:25:01 -07001274 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -07001276 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1277 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001278 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Stephen Boyda098ecd2016-08-02 14:04:28 -07001281 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
Luis R. Rodriguezed046302015-04-29 16:30:43 -07001282 dev_dbg(device, "using built-in %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001283 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +01001284 }
1285
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001286 ret = alloc_lookup_fw_priv(name, &fw_cache, &fw_priv, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +08001287
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001288 /*
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001289 * bind with 'priv' now to avoid warning in failure path
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001290 * of requesting firmware.
1291 */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001292 firmware->priv = fw_priv;
Ming Lei1f2b7952012-08-04 12:01:21 +08001293
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001294 if (ret > 0) {
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -08001295 ret = fw_state_wait(fw_priv);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001296 if (!ret) {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001297 fw_set_page_data(fw_priv, firmware);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001298 return 0; /* assigned */
1299 }
Stephen Boyddddb5542012-03-28 23:30:43 +02001300 }
Ming Lei1f2b7952012-08-04 12:01:21 +08001301
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001302 if (ret < 0)
1303 return ret;
1304 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001305}
1306
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001307/*
1308 * Batched requests need only one wake, we need to do this step last due to the
1309 * fallback mechanism. The buf is protected with kref_get(), and it won't be
1310 * released until the last user calls release_firmware().
1311 *
1312 * Failed batched requests are possible as well, in such cases we just share
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001313 * the struct fw_priv and won't release it until all requests are woken
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001314 * and have gone through this same path.
1315 */
1316static void fw_abort_batch_reqs(struct firmware *fw)
1317{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001318 struct fw_priv *fw_priv;
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001319
1320 /* Loaded directly? */
1321 if (!fw || !fw->priv)
1322 return;
1323
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001324 fw_priv = fw->priv;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -08001325 if (!fw_state_is_aborted(fw_priv))
1326 fw_state_aborted(fw_priv);
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001327}
1328
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001329/* called from request_firmware() and request_firmware_work_func() */
1330static int
1331_request_firmware(const struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001332 struct device *device, void *buf, size_t size,
1333 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001334{
Brian Norris715780a2015-12-09 14:50:28 -08001335 struct firmware *fw = NULL;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001336 int ret;
1337
1338 if (!firmware_p)
1339 return -EINVAL;
1340
Brian Norris715780a2015-12-09 14:50:28 -08001341 if (!name || name[0] == '\0') {
1342 ret = -EINVAL;
1343 goto out;
1344 }
Kees Cook471b0952014-09-18 11:25:37 -07001345
Stephen Boyda098ecd2016-08-02 14:04:28 -07001346 ret = _request_firmware_prepare(&fw, name, device, buf, size);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001347 if (ret <= 0) /* error or already assigned */
1348 goto out;
1349
Neil Horman3e358ac2013-09-06 15:36:08 -04001350 ret = fw_get_filesystem_firmware(device, fw->priv);
1351 if (ret) {
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001352 if (!(opt_flags & FW_OPT_NO_WARN))
Takashi Iwaibba3a872013-12-02 15:38:16 +01001353 dev_warn(device,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001354 "Direct firmware load for %s failed with error %d\n",
1355 name, ret);
Luis R. Rodriguez25d39132017-11-20 10:23:57 -08001356 ret = fw_sysfs_fallback(fw, name, device, opt_flags, ret);
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001357 } else
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001358 ret = assign_fw(fw, device, opt_flags);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001359
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001360 out:
1361 if (ret < 0) {
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001362 fw_abort_batch_reqs(fw);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001363 release_firmware(fw);
1364 fw = NULL;
1365 }
1366
1367 *firmware_p = fw;
1368 return ret;
1369}
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371/**
Kay Sievers312c0042005-11-16 09:00:00 +01001372 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001373 * @firmware_p: pointer to firmware image
1374 * @name: name of firmware file
1375 * @device: device for which firmware is being loaded
1376 *
1377 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001378 * of @name for device @device.
1379 *
1380 * Should be called from user context where sleeping is allowed.
1381 *
Kay Sievers312c0042005-11-16 09:00:00 +01001382 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001383 * should be distinctive enough not to be confused with any other
1384 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001385 *
1386 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001387 *
1388 * The function can be called safely inside device's suspend and
1389 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001390 **/
1391int
1392request_firmware(const struct firmware **firmware_p, const char *name,
Andrei Opreaea310032015-03-08 12:41:15 +02001393 struct device *device)
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001394{
Ming Leid6c8aa32013-06-06 20:01:48 +08001395 int ret;
1396
1397 /* Need to pin this module until return */
1398 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001399 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001400 FW_OPT_UEVENT);
Ming Leid6c8aa32013-06-06 20:01:48 +08001401 module_put(THIS_MODULE);
1402 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001403}
Daniel Mackf4945132013-05-23 22:17:18 +02001404EXPORT_SYMBOL(request_firmware);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001405
Takashi Iwaibba3a872013-12-02 15:38:16 +01001406/**
Borislav Petkov3c1556b2014-12-03 22:46:37 +01001407 * request_firmware_direct: - load firmware directly without usermode helper
Takashi Iwaibba3a872013-12-02 15:38:16 +01001408 * @firmware_p: pointer to firmware image
1409 * @name: name of firmware file
1410 * @device: device for which firmware is being loaded
1411 *
1412 * This function works pretty much like request_firmware(), but this doesn't
1413 * fall back to usermode helper even if the firmware couldn't be loaded
1414 * directly from fs. Hence it's useful for loading optional firmwares, which
1415 * aren't always present, without extra long timeouts of udev.
1416 **/
1417int request_firmware_direct(const struct firmware **firmware_p,
1418 const char *name, struct device *device)
1419{
1420 int ret;
Andrei Opreaea310032015-03-08 12:41:15 +02001421
Takashi Iwaibba3a872013-12-02 15:38:16 +01001422 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001423 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001424 FW_OPT_UEVENT | FW_OPT_NO_WARN |
1425 FW_OPT_NOFALLBACK);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001426 module_put(THIS_MODULE);
1427 return ret;
1428}
1429EXPORT_SYMBOL_GPL(request_firmware_direct);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001430
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001431/**
Stephen Boyda098ecd2016-08-02 14:04:28 -07001432 * request_firmware_into_buf - load firmware into a previously allocated buffer
1433 * @firmware_p: pointer to firmware image
1434 * @name: name of firmware file
1435 * @device: device for which firmware is being loaded and DMA region allocated
1436 * @buf: address of buffer to load firmware into
1437 * @size: size of buffer
1438 *
1439 * This function works pretty much like request_firmware(), but it doesn't
1440 * allocate a buffer to hold the firmware data. Instead, the firmware
1441 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
1442 * data member is pointed at @buf.
1443 *
1444 * This function doesn't cache firmware either.
1445 */
1446int
1447request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
1448 struct device *device, void *buf, size_t size)
1449{
1450 int ret;
1451
1452 __module_get(THIS_MODULE);
1453 ret = _request_firmware(firmware_p, name, device, buf, size,
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001454 FW_OPT_UEVENT | FW_OPT_NOCACHE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001455 module_put(THIS_MODULE);
1456 return ret;
1457}
1458EXPORT_SYMBOL(request_firmware_into_buf);
1459
1460/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001462 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001464void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001467 if (!fw_is_builtin_firmware(fw))
1468 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 kfree(fw);
1470 }
1471}
Daniel Mackf4945132013-05-23 22:17:18 +02001472EXPORT_SYMBOL(release_firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474/* Async support */
1475struct firmware_work {
1476 struct work_struct work;
1477 struct module *module;
1478 const char *name;
1479 struct device *device;
1480 void *context;
1481 void (*cont)(const struct firmware *fw, void *context);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001482 unsigned int opt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483};
1484
Stephen Boyda36cf842012-03-28 23:31:00 +02001485static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Stephen Boyda36cf842012-03-28 23:31:00 +02001487 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001489
Stephen Boyda36cf842012-03-28 23:31:00 +02001490 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001491
Stephen Boyda098ecd2016-08-02 14:04:28 -07001492 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001493 fw_work->opt_flags);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001494 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001495 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 module_put(fw_work->module);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001498 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
1502/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001503 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001504 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001505 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001506 * is non-zero else the firmware copy must be done manually.
1507 * @name: name of firmware file
1508 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001509 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001510 * @context: will be passed over to @cont, and
1511 * @fw may be %NULL if firmware request fails.
1512 * @cont: function will be called asynchronously when the firmware
1513 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001515 * Caller must hold the reference count of @device.
1516 *
Ming Lei6f21a622012-08-04 12:01:24 +08001517 * Asynchronous variant of request_firmware() for user contexts:
1518 * - sleep for as small periods as possible since it may
Silvio Fricke88bcef52016-11-25 15:59:47 +01001519 * increase kernel boot time of built-in device drivers
1520 * requesting firmware in their ->probe() methods, if
1521 * @gfp is GFP_KERNEL.
Ming Lei6f21a622012-08-04 12:01:24 +08001522 *
1523 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 **/
1525int
1526request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001527 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001528 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 void (*cont)(const struct firmware *fw, void *context))
1530{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001531 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Andrei Opreaea310032015-03-08 12:41:15 +02001533 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (!fw_work)
1535 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001536
1537 fw_work->module = module;
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001538 fw_work->name = kstrdup_const(name, gfp);
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001539 if (!fw_work->name) {
1540 kfree(fw_work);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001541 return -ENOMEM;
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001542 }
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001543 fw_work->device = device;
1544 fw_work->context = context;
1545 fw_work->cont = cont;
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001546 fw_work->opt_flags = FW_OPT_NOWAIT |
Takashi Iwai5a1379e2014-06-04 17:48:15 +02001547 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 if (!try_module_get(module)) {
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001550 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 kfree(fw_work);
1552 return -EFAULT;
1553 }
1554
Ming Lei0cfc1e12012-08-04 12:01:23 +08001555 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001556 INIT_WORK(&fw_work->work, request_firmware_work_func);
1557 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return 0;
1559}
Daniel Mackf4945132013-05-23 22:17:18 +02001560EXPORT_SYMBOL(request_firmware_nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Ming Lei90f890812013-06-20 12:30:16 +08001562#ifdef CONFIG_PM_SLEEP
1563static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1564
Ming Lei2887b392012-08-04 12:01:22 +08001565/**
1566 * cache_firmware - cache one firmware image in kernel memory space
1567 * @fw_name: the firmware image name
1568 *
1569 * Cache firmware in kernel memory so that drivers can use it when
1570 * system isn't ready for them to request firmware image from userspace.
1571 * Once it returns successfully, driver can use request_firmware or its
1572 * nowait version to get the cached firmware without any interacting
1573 * with userspace
1574 *
1575 * Return 0 if the firmware image has been cached successfully
1576 * Return !0 otherwise
1577 *
1578 */
Ming Lei93232e42013-06-06 20:01:47 +08001579static int cache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001580{
1581 int ret;
1582 const struct firmware *fw;
1583
1584 pr_debug("%s: %s\n", __func__, fw_name);
1585
1586 ret = request_firmware(&fw, fw_name, NULL);
1587 if (!ret)
1588 kfree(fw);
1589
1590 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1591
1592 return ret;
1593}
1594
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001595static struct fw_priv *lookup_fw_priv(const char *fw_name)
Ming Lei6a2c1232013-06-26 09:28:17 +08001596{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001597 struct fw_priv *tmp;
Ming Lei6a2c1232013-06-26 09:28:17 +08001598 struct firmware_cache *fwc = &fw_cache;
1599
1600 spin_lock(&fwc->lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001601 tmp = __lookup_fw_priv(fw_name);
Ming Lei6a2c1232013-06-26 09:28:17 +08001602 spin_unlock(&fwc->lock);
1603
1604 return tmp;
1605}
1606
Ming Lei2887b392012-08-04 12:01:22 +08001607/**
1608 * uncache_firmware - remove one cached firmware image
1609 * @fw_name: the firmware image name
1610 *
1611 * Uncache one firmware image which has been cached successfully
1612 * before.
1613 *
1614 * Return 0 if the firmware cache has been removed successfully
1615 * Return !0 otherwise
1616 *
1617 */
Ming Lei93232e42013-06-06 20:01:47 +08001618static int uncache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001619{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001620 struct fw_priv *fw_priv;
Ming Lei2887b392012-08-04 12:01:22 +08001621 struct firmware fw;
1622
1623 pr_debug("%s: %s\n", __func__, fw_name);
1624
Stephen Boyda098ecd2016-08-02 14:04:28 -07001625 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
Ming Lei2887b392012-08-04 12:01:22 +08001626 return 0;
1627
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001628 fw_priv = lookup_fw_priv(fw_name);
1629 if (fw_priv) {
1630 free_fw_priv(fw_priv);
Ming Lei2887b392012-08-04 12:01:22 +08001631 return 0;
1632 }
1633
1634 return -EINVAL;
1635}
1636
Ming Lei37276a52012-08-04 12:01:27 +08001637static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1638{
1639 struct fw_cache_entry *fce;
1640
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001641 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
Ming Lei37276a52012-08-04 12:01:27 +08001642 if (!fce)
1643 goto exit;
1644
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001645 fce->name = kstrdup_const(name, GFP_ATOMIC);
1646 if (!fce->name) {
1647 kfree(fce);
1648 fce = NULL;
1649 goto exit;
1650 }
Ming Lei37276a52012-08-04 12:01:27 +08001651exit:
1652 return fce;
1653}
1654
Ming Lei373304f2012-10-09 12:01:01 +08001655static int __fw_entry_found(const char *name)
1656{
1657 struct firmware_cache *fwc = &fw_cache;
1658 struct fw_cache_entry *fce;
1659
1660 list_for_each_entry(fce, &fwc->fw_names, list) {
1661 if (!strcmp(fce->name, name))
1662 return 1;
1663 }
1664 return 0;
1665}
1666
Ming Leiac39b3e2012-08-20 19:04:16 +08001667static int fw_cache_piggyback_on_request(const char *name)
1668{
1669 struct firmware_cache *fwc = &fw_cache;
1670 struct fw_cache_entry *fce;
1671 int ret = 0;
1672
1673 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001674 if (__fw_entry_found(name))
1675 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001676
1677 fce = alloc_fw_cache_entry(name);
1678 if (fce) {
1679 ret = 1;
1680 list_add(&fce->list, &fwc->fw_names);
1681 pr_debug("%s: fw: %s\n", __func__, name);
1682 }
1683found:
1684 spin_unlock(&fwc->name_lock);
1685 return ret;
1686}
1687
Ming Lei37276a52012-08-04 12:01:27 +08001688static void free_fw_cache_entry(struct fw_cache_entry *fce)
1689{
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001690 kfree_const(fce->name);
Ming Lei37276a52012-08-04 12:01:27 +08001691 kfree(fce);
1692}
1693
1694static void __async_dev_cache_fw_image(void *fw_entry,
1695 async_cookie_t cookie)
1696{
1697 struct fw_cache_entry *fce = fw_entry;
1698 struct firmware_cache *fwc = &fw_cache;
1699 int ret;
1700
1701 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001702 if (ret) {
1703 spin_lock(&fwc->name_lock);
1704 list_del(&fce->list);
1705 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001706
Ming Leiac39b3e2012-08-20 19:04:16 +08001707 free_fw_cache_entry(fce);
1708 }
Ming Lei37276a52012-08-04 12:01:27 +08001709}
1710
1711/* called with dev->devres_lock held */
1712static void dev_create_fw_entry(struct device *dev, void *res,
1713 void *data)
1714{
1715 struct fw_name_devm *fwn = res;
1716 const char *fw_name = fwn->name;
1717 struct list_head *head = data;
1718 struct fw_cache_entry *fce;
1719
1720 fce = alloc_fw_cache_entry(fw_name);
1721 if (fce)
1722 list_add(&fce->list, head);
1723}
1724
1725static int devm_name_match(struct device *dev, void *res,
1726 void *match_data)
1727{
1728 struct fw_name_devm *fwn = res;
1729 return (fwn->magic == (unsigned long)match_data);
1730}
1731
Ming Leiab6dd8e2012-08-17 22:07:00 +08001732static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001733{
1734 LIST_HEAD(todo);
1735 struct fw_cache_entry *fce;
1736 struct fw_cache_entry *fce_next;
1737 struct firmware_cache *fwc = &fw_cache;
1738
1739 devres_for_each_res(dev, fw_name_devm_release,
1740 devm_name_match, &fw_cache,
1741 dev_create_fw_entry, &todo);
1742
1743 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1744 list_del(&fce->list);
1745
1746 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001747 /* only one cache entry for one firmware */
1748 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001749 list_add(&fce->list, &fwc->fw_names);
1750 } else {
1751 free_fw_cache_entry(fce);
1752 fce = NULL;
1753 }
Ming Lei37276a52012-08-04 12:01:27 +08001754 spin_unlock(&fwc->name_lock);
1755
Ming Lei373304f2012-10-09 12:01:01 +08001756 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001757 async_schedule_domain(__async_dev_cache_fw_image,
1758 (void *)fce,
1759 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001760 }
1761}
1762
1763static void __device_uncache_fw_images(void)
1764{
1765 struct firmware_cache *fwc = &fw_cache;
1766 struct fw_cache_entry *fce;
1767
1768 spin_lock(&fwc->name_lock);
1769 while (!list_empty(&fwc->fw_names)) {
1770 fce = list_entry(fwc->fw_names.next,
1771 struct fw_cache_entry, list);
1772 list_del(&fce->list);
1773 spin_unlock(&fwc->name_lock);
1774
1775 uncache_firmware(fce->name);
1776 free_fw_cache_entry(fce);
1777
1778 spin_lock(&fwc->name_lock);
1779 }
1780 spin_unlock(&fwc->name_lock);
1781}
1782
1783/**
1784 * device_cache_fw_images - cache devices' firmware
1785 *
1786 * If one device called request_firmware or its nowait version
1787 * successfully before, the firmware names are recored into the
1788 * device's devres link list, so device_cache_fw_images can call
1789 * cache_firmware() to cache these firmwares for the device,
1790 * then the device driver can load its firmwares easily at
1791 * time when system is not ready to complete loading firmware.
1792 */
1793static void device_cache_fw_images(void)
1794{
1795 struct firmware_cache *fwc = &fw_cache;
Ming Lei37276a52012-08-04 12:01:27 +08001796 DEFINE_WAIT(wait);
1797
1798 pr_debug("%s\n", __func__);
1799
Ming Lei373304f2012-10-09 12:01:01 +08001800 /* cancel uncache work */
1801 cancel_delayed_work_sync(&fwc->work);
1802
Luis R. Rodriguez5d9566b2018-03-10 06:14:47 -08001803 fw_fallback_set_cache_timeout();
Ming Leiffe53f62012-08-04 12:01:28 +08001804
Ming Leiac39b3e2012-08-20 19:04:16 +08001805 mutex_lock(&fw_lock);
1806 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001807 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001808 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001809
1810 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001811 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001812
Luis R. Rodriguez5d9566b2018-03-10 06:14:47 -08001813 fw_fallback_set_default_timeout();
Ming Lei37276a52012-08-04 12:01:27 +08001814}
1815
1816/**
1817 * device_uncache_fw_images - uncache devices' firmware
1818 *
1819 * uncache all firmwares which have been cached successfully
1820 * by device_uncache_fw_images earlier
1821 */
1822static void device_uncache_fw_images(void)
1823{
1824 pr_debug("%s\n", __func__);
1825 __device_uncache_fw_images();
1826}
1827
1828static void device_uncache_fw_images_work(struct work_struct *work)
1829{
1830 device_uncache_fw_images();
1831}
1832
1833/**
1834 * device_uncache_fw_images_delay - uncache devices firmwares
1835 * @delay: number of milliseconds to delay uncache device firmwares
1836 *
1837 * uncache all devices's firmwares which has been cached successfully
1838 * by device_cache_fw_images after @delay milliseconds.
1839 */
1840static void device_uncache_fw_images_delay(unsigned long delay)
1841{
Shaibal Duttabce66182014-01-31 15:44:58 -08001842 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1843 msecs_to_jiffies(delay));
Ming Lei37276a52012-08-04 12:01:27 +08001844}
1845
Ming Lei07646d92012-08-04 12:01:29 +08001846static int fw_pm_notify(struct notifier_block *notify_block,
1847 unsigned long mode, void *unused)
1848{
1849 switch (mode) {
1850 case PM_HIBERNATION_PREPARE:
1851 case PM_SUSPEND_PREPARE:
Sebastian Capellaf8d5b9e2014-02-18 17:52:08 -08001852 case PM_RESTORE_PREPARE:
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -07001853 /*
1854 * kill pending fallback requests with a custom fallback
1855 * to avoid stalling suspend.
1856 */
1857 kill_pending_fw_fallback_reqs(true);
Ming Lei07646d92012-08-04 12:01:29 +08001858 device_cache_fw_images();
1859 break;
1860
1861 case PM_POST_SUSPEND:
1862 case PM_POST_HIBERNATION:
1863 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001864 /*
1865 * In case that system sleep failed and syscore_suspend is
1866 * not called.
1867 */
1868 mutex_lock(&fw_lock);
1869 fw_cache.state = FW_LOADER_NO_CACHE;
1870 mutex_unlock(&fw_lock);
1871
Ming Lei07646d92012-08-04 12:01:29 +08001872 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1873 break;
1874 }
1875
1876 return 0;
1877}
Ming Lei07646d92012-08-04 12:01:29 +08001878
Ming Leiac39b3e2012-08-20 19:04:16 +08001879/* stop caching firmware once syscore_suspend is reached */
1880static int fw_suspend(void)
1881{
1882 fw_cache.state = FW_LOADER_NO_CACHE;
1883 return 0;
1884}
1885
1886static struct syscore_ops fw_syscore_ops = {
1887 .suspend = fw_suspend,
1888};
Luis R. Rodrigueza67e5032017-11-20 09:45:31 -08001889
Luis R. Rodriguez59b6d852017-11-20 09:45:32 -08001890static int __init register_fw_pm_ops(void)
1891{
1892 int ret;
1893
1894 spin_lock_init(&fw_cache.name_lock);
1895 INIT_LIST_HEAD(&fw_cache.fw_names);
1896
1897 INIT_DELAYED_WORK(&fw_cache.work,
1898 device_uncache_fw_images_work);
1899
1900 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1901 ret = register_pm_notifier(&fw_cache.pm_notify);
1902 if (ret)
1903 return ret;
1904
1905 register_syscore_ops(&fw_syscore_ops);
1906
1907 return ret;
1908}
1909
Luis R. Rodrigueza67e5032017-11-20 09:45:31 -08001910static inline void unregister_fw_pm_ops(void)
1911{
1912 unregister_syscore_ops(&fw_syscore_ops);
1913 unregister_pm_notifier(&fw_cache.pm_notify);
1914}
Ming Leicfe016b2012-09-08 17:32:30 +08001915#else
1916static int fw_cache_piggyback_on_request(const char *name)
1917{
1918 return 0;
1919}
Luis R. Rodriguez59b6d852017-11-20 09:45:32 -08001920static inline int register_fw_pm_ops(void)
1921{
1922 return 0;
1923}
Luis R. Rodrigueza67e5032017-11-20 09:45:31 -08001924static inline void unregister_fw_pm_ops(void)
1925{
1926}
Ming Leicfe016b2012-09-08 17:32:30 +08001927#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001928
Ming Lei37276a52012-08-04 12:01:27 +08001929static void __init fw_cache_init(void)
1930{
1931 spin_lock_init(&fw_cache.lock);
1932 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001933 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001934}
1935
Luis R. Rodrigueza669f042017-05-02 01:31:04 -07001936static int fw_shutdown_notify(struct notifier_block *unused1,
1937 unsigned long unused2, void *unused3)
1938{
1939 /*
1940 * Kill all pending fallback requests to avoid both stalling shutdown,
1941 * and avoid a deadlock with the usermode_lock.
1942 */
1943 kill_pending_fw_fallback_reqs(false);
1944
1945 return NOTIFY_DONE;
1946}
1947
1948static struct notifier_block fw_shutdown_nb = {
1949 .notifier_call = fw_shutdown_notify,
1950};
1951
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001952static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Luis R. Rodriguez59b6d852017-11-20 09:45:32 -08001954 int ret;
1955
1956 /* No need to unfold these on exit */
Ming Lei1f2b7952012-08-04 12:01:21 +08001957 fw_cache_init();
Luis R. Rodriguez59b6d852017-11-20 09:45:32 -08001958
1959 ret = register_fw_pm_ops();
1960 if (ret)
1961 return ret;
1962
Luis R. Rodriguez561a10b2017-11-20 09:45:34 -08001963 ret = register_reboot_notifier(&fw_shutdown_nb);
1964 if (ret)
1965 goto out;
1966
Luis R. Rodriguez6bb9cf32017-11-20 09:45:33 -08001967 return register_sysfs_loader();
Luis R. Rodriguez561a10b2017-11-20 09:45:34 -08001968
1969out:
1970 unregister_fw_pm_ops();
1971 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001973
1974static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
Luis R. Rodrigueza67e5032017-11-20 09:45:31 -08001976 unregister_fw_pm_ops();
Takashi Iwaife304142013-05-22 18:28:38 +02001977 unregister_reboot_notifier(&fw_shutdown_nb);
Luis R. Rodriguez6bb9cf32017-11-20 09:45:33 -08001978 unregister_sysfs_loader();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
Shaohua Lia30a6a22006-09-27 01:50:52 -07001981fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982module_exit(firmware_class_exit);