blob: 43b97a8137f7f3757b9ba4c28e05c54a7bc43595 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * firmware_class.c - Multi purpose firmware loading support
3 *
Markus Rechberger87d37a42007-06-04 18:45:44 +02004 * Copyright (c) 2003 Manuel Estrada Sainz
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Please see Documentation/firmware_class/ for more information.
7 *
8 */
9
Luis R. Rodriguez73da4b42017-07-20 13:13:40 -070010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Randy.Dunlapc59ede72006-01-11 12:17:46 -080012#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/device.h>
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/timer.h>
17#include <linux/vmalloc.h>
18#include <linux/interrupt.h>
19#include <linux/bitops.h>
Laura Garciacad1e552006-05-23 23:22:38 +020020#include <linux/mutex.h>
Stephen Boyda36cf842012-03-28 23:31:00 +020021#include <linux/workqueue.h>
David Woodhouse6e03a202009-04-09 22:04:07 -070022#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/firmware.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Stephen Boyda36cf842012-03-28 23:31:00 +020025#include <linux/sched.h>
Linus Torvaldsabb139e2012-10-03 15:58:32 -070026#include <linux/file.h>
Ming Lei1f2b7952012-08-04 12:01:21 +080027#include <linux/list.h>
Mimi Zohare40ba6d2015-11-19 12:39:22 -050028#include <linux/fs.h>
Ming Lei37276a52012-08-04 12:01:27 +080029#include <linux/async.h>
30#include <linux/pm.h>
Ming Lei07646d92012-08-04 12:01:29 +080031#include <linux/suspend.h>
Ming Leiac39b3e2012-08-20 19:04:16 +080032#include <linux/syscore_ops.h>
Takashi Iwaife304142013-05-22 18:28:38 +020033#include <linux/reboot.h>
Kees Cook6593d922014-02-25 13:06:00 -080034#include <linux/security.h>
Ming Lei37276a52012-08-04 12:01:27 +080035
Linus Torvaldsabb139e2012-10-03 15:58:32 -070036#include <generated/utsrelease.h>
37
Ming Lei37276a52012-08-04 12:01:27 +080038#include "base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Markus Rechberger87d37a42007-06-04 18:45:44 +020040MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070041MODULE_DESCRIPTION("Multi purpose firmware loading support");
42MODULE_LICENSE("GPL");
43
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010044enum fw_status {
45 FW_STATUS_UNKNOWN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 FW_STATUS_LOADING,
47 FW_STATUS_DONE,
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010048 FW_STATUS_ABORTED,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010051/*
52 * Concurrent request_firmware() for the same firmware need to be
53 * serialized. struct fw_state is simple state machine which hold the
54 * state of the firmware loading.
55 */
56struct fw_state {
Luis R. Rodrigueze44565f2017-07-20 13:13:09 -070057 struct completion completion;
Daniel Wagner0430caf2016-11-17 11:00:49 +010058 enum fw_status status;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010059};
60
Takashi Iwai14c4bae2013-12-02 15:38:18 +010061/* firmware behavior options */
62#define FW_OPT_UEVENT (1U << 0)
63#define FW_OPT_NOWAIT (1U << 1)
Takashi Iwai5a1379e2014-06-04 17:48:15 +020064#define FW_OPT_USERHELPER (1U << 2)
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -070065#define FW_OPT_NO_WARN (1U << 3)
Vikram Mulukutla0e742e92016-08-02 14:04:25 -070066#define FW_OPT_NOCACHE (1U << 4)
Luis R. Rodriguez3f722712017-11-20 10:23:58 -080067#define FW_OPT_NOFALLBACK (1U << 5)
Takashi Iwai14c4bae2013-12-02 15:38:18 +010068
Ming Lei1f2b7952012-08-04 12:01:21 +080069struct firmware_cache {
70 /* firmware_buf instance will be added into the below list */
71 spinlock_t lock;
72 struct list_head head;
Ming Leicfe016b2012-09-08 17:32:30 +080073 int state;
Ming Lei37276a52012-08-04 12:01:27 +080074
Ming Leicfe016b2012-09-08 17:32:30 +080075#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +080076 /*
77 * Names of firmware images which have been cached successfully
78 * will be added into the below list so that device uncache
79 * helper can trace which firmware images have been cached
80 * before.
81 */
82 spinlock_t name_lock;
83 struct list_head fw_names;
84
Ming Lei37276a52012-08-04 12:01:27 +080085 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +080086
87 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +080088#endif
Ming Lei1f2b7952012-08-04 12:01:21 +080089};
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -080091struct fw_priv {
Ming Lei1f2b7952012-08-04 12:01:21 +080092 struct kref ref;
93 struct list_head list;
Ming Lei1f2b7952012-08-04 12:01:21 +080094 struct firmware_cache *fwc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010095 struct fw_state fw_st;
Ming Lei65710cb2012-08-04 12:01:16 +080096 void *data;
97 size_t size;
Stephen Boyda098ecd2016-08-02 14:04:28 -070098 size_t allocated_size;
Takashi Iwai7b1269f2013-01-31 11:13:55 +010099#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100100 bool is_paged_buf;
Ming Leiaf5bc112013-05-27 18:30:04 +0800101 bool need_uevent;
David Woodhouse6e03a202009-04-09 22:04:07 -0700102 struct page **pages;
103 int nr_pages;
104 int page_array_size;
Takashi Iwaife304142013-05-22 18:28:38 +0200105 struct list_head pending_list;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100106#endif
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800107 const char *fw_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
Ming Lei37276a52012-08-04 12:01:27 +0800110struct fw_cache_entry {
111 struct list_head list;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700112 const char *name;
Ming Lei37276a52012-08-04 12:01:27 +0800113};
114
Ming Leif531f05a2012-08-04 12:01:25 +0800115struct fw_name_devm {
116 unsigned long magic;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700117 const char *name;
Ming Leif531f05a2012-08-04 12:01:25 +0800118};
119
Luis R. Rodriguez942e7432017-11-20 10:23:55 -0800120static inline struct fw_priv *to_fw_priv(struct kref *ref)
121{
122 return container_of(ref, struct fw_priv, ref);
123}
Ming Lei1f2b7952012-08-04 12:01:21 +0800124
Ming Leiac39b3e2012-08-20 19:04:16 +0800125#define FW_LOADER_NO_CACHE 0
126#define FW_LOADER_START_CACHE 1
127
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800128/* fw_lock could be moved to 'struct fw_sysfs' but since it is just
Ming Lei1f2b7952012-08-04 12:01:21 +0800129 * guarding for corner cases a global lock should be OK */
130static DEFINE_MUTEX(fw_lock);
131
132static struct firmware_cache fw_cache;
133
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800134/* Builtin firmware support */
135
136#ifdef CONFIG_FW_LOADER
137
138extern struct builtin_fw __start_builtin_fw[];
139extern struct builtin_fw __end_builtin_fw[];
140
Luis R. Rodriguez5711ae62017-11-20 10:23:56 -0800141static void fw_copy_to_prealloc_buf(struct firmware *fw,
142 void *buf, size_t size)
143{
144 if (!buf || size < fw->size)
145 return;
146 memcpy(buf, fw->data, fw->size);
147}
148
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800149static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
150 void *buf, size_t size)
151{
152 struct builtin_fw *b_fw;
153
154 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
155 if (strcmp(name, b_fw->name) == 0) {
156 fw->size = b_fw->size;
157 fw->data = b_fw->data;
Luis R. Rodriguez5711ae62017-11-20 10:23:56 -0800158 fw_copy_to_prealloc_buf(fw, buf, size);
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800159
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800160 return true;
161 }
162 }
163
164 return false;
165}
166
167static bool fw_is_builtin_firmware(const struct firmware *fw)
168{
169 struct builtin_fw *b_fw;
170
171 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
172 if (fw->data == b_fw->data)
173 return true;
174
175 return false;
176}
177
178#else /* Module case - no builtin firmware support */
179
180static inline bool fw_get_builtin_firmware(struct firmware *fw,
181 const char *name, void *buf,
182 size_t size)
183{
184 return false;
185}
186
187static inline bool fw_is_builtin_firmware(const struct firmware *fw)
188{
189 return false;
190}
191#endif
192
193static int loading_timeout = 60; /* In seconds */
194
195static inline long firmware_loading_timeout(void)
196{
197 return loading_timeout > 0 ? loading_timeout * HZ : MAX_JIFFY_OFFSET;
198}
199
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800200static void fw_state_init(struct fw_priv *fw_priv)
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800201{
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800202 struct fw_state *fw_st = &fw_priv->fw_st;
203
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800204 init_completion(&fw_st->completion);
205 fw_st->status = FW_STATUS_UNKNOWN;
206}
207
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800208static int __fw_state_wait_common(struct fw_priv *fw_priv, long timeout)
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800209{
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800210 struct fw_state *fw_st = &fw_priv->fw_st;
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800211 long ret;
212
213 ret = wait_for_completion_killable_timeout(&fw_st->completion, timeout);
214 if (ret != 0 && fw_st->status == FW_STATUS_ABORTED)
215 return -ENOENT;
216 if (!ret)
217 return -ETIMEDOUT;
218
219 return ret < 0 ? ret : 0;
220}
221
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800222static void __fw_state_set(struct fw_priv *fw_priv,
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800223 enum fw_status status)
224{
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800225 struct fw_state *fw_st = &fw_priv->fw_st;
226
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800227 WRITE_ONCE(fw_st->status, status);
228
229 if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED)
230 complete_all(&fw_st->completion);
231}
232
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800233static inline void fw_state_start(struct fw_priv *fw_priv)
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800234{
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800235 __fw_state_set(fw_priv, FW_STATUS_LOADING);
236}
237
238static inline void fw_state_done(struct fw_priv *fw_priv)
239{
240 __fw_state_set(fw_priv, FW_STATUS_DONE);
241}
242
243static inline void fw_state_aborted(struct fw_priv *fw_priv)
244{
245 __fw_state_set(fw_priv, FW_STATUS_ABORTED);
246}
247
248static inline int fw_state_wait(struct fw_priv *fw_priv)
249{
250 return __fw_state_wait_common(fw_priv, MAX_SCHEDULE_TIMEOUT);
251}
252
253static bool __fw_state_check(struct fw_priv *fw_priv,
254 enum fw_status status)
255{
256 struct fw_state *fw_st = &fw_priv->fw_st;
257
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800258 return fw_st->status == status;
259}
260
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800261static inline bool fw_state_is_aborted(struct fw_priv *fw_priv)
262{
263 return __fw_state_check(fw_priv, FW_STATUS_ABORTED);
264}
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800265
266#ifdef CONFIG_FW_LOADER_USER_HELPER
267
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800268static inline bool fw_sysfs_done(struct fw_priv *fw_priv)
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800269{
270 return __fw_state_check(fw_priv, FW_STATUS_DONE);
271}
272
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800273static inline bool fw_sysfs_loading(struct fw_priv *fw_priv)
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800274{
275 return __fw_state_check(fw_priv, FW_STATUS_LOADING);
276}
277
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800278static inline int fw_sysfs_wait_timeout(struct fw_priv *fw_priv, long timeout)
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800279{
280 return __fw_state_wait_common(fw_priv, timeout);
281}
Luis R. Rodriguezbe8d4622017-11-20 10:23:50 -0800282
283#endif /* CONFIG_FW_LOADER_USER_HELPER */
284
285static int fw_cache_piggyback_on_request(const char *name);
286
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800287static struct fw_priv *__allocate_fw_priv(const char *fw_name,
288 struct firmware_cache *fwc,
289 void *dbuf, size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800290{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800291 struct fw_priv *fw_priv;
Ming Lei1f2b7952012-08-04 12:01:21 +0800292
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800293 fw_priv = kzalloc(sizeof(*fw_priv), GFP_ATOMIC);
294 if (!fw_priv)
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700295 return NULL;
296
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800297 fw_priv->fw_name = kstrdup_const(fw_name, GFP_ATOMIC);
298 if (!fw_priv->fw_name) {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800299 kfree(fw_priv);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700300 return NULL;
301 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800302
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800303 kref_init(&fw_priv->ref);
304 fw_priv->fwc = fwc;
305 fw_priv->data = dbuf;
306 fw_priv->allocated_size = size;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800307 fw_state_init(fw_priv);
Takashi Iwaife304142013-05-22 18:28:38 +0200308#ifdef CONFIG_FW_LOADER_USER_HELPER
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800309 INIT_LIST_HEAD(&fw_priv->pending_list);
Takashi Iwaife304142013-05-22 18:28:38 +0200310#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800311
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800312 pr_debug("%s: fw-%s fw_priv=%p\n", __func__, fw_name, fw_priv);
Ming Lei1f2b7952012-08-04 12:01:21 +0800313
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800314 return fw_priv;
Ming Lei1f2b7952012-08-04 12:01:21 +0800315}
316
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800317static struct fw_priv *__lookup_fw_priv(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +0800318{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800319 struct fw_priv *tmp;
Ming Lei2887b392012-08-04 12:01:22 +0800320 struct firmware_cache *fwc = &fw_cache;
321
322 list_for_each_entry(tmp, &fwc->head, list)
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800323 if (!strcmp(tmp->fw_name, fw_name))
Ming Lei2887b392012-08-04 12:01:22 +0800324 return tmp;
325 return NULL;
326}
327
Luis R. Rodriguez30172be2017-07-20 13:13:41 -0700328/* Returns 1 for batching firmware requests with the same name */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800329static int alloc_lookup_fw_priv(const char *fw_name,
330 struct firmware_cache *fwc,
331 struct fw_priv **fw_priv, void *dbuf,
332 size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800333{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800334 struct fw_priv *tmp;
Ming Lei1f2b7952012-08-04 12:01:21 +0800335
336 spin_lock(&fwc->lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800337 tmp = __lookup_fw_priv(fw_name);
Ming Lei2887b392012-08-04 12:01:22 +0800338 if (tmp) {
339 kref_get(&tmp->ref);
340 spin_unlock(&fwc->lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800341 *fw_priv = tmp;
342 pr_debug("batched request - sharing the same struct fw_priv and lookup for multiple requests\n");
Ming Lei2887b392012-08-04 12:01:22 +0800343 return 1;
344 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800345 tmp = __allocate_fw_priv(fw_name, fwc, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +0800346 if (tmp)
347 list_add(&tmp->list, &fwc->head);
348 spin_unlock(&fwc->lock);
349
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800350 *fw_priv = tmp;
Ming Lei1f2b7952012-08-04 12:01:21 +0800351
352 return tmp ? 0 : -ENOMEM;
353}
354
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800355static void __free_fw_priv(struct kref *ref)
Bart Van Assche98233b22014-01-04 14:20:36 +0100356 __releases(&fwc->lock)
Ming Lei1f2b7952012-08-04 12:01:21 +0800357{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800358 struct fw_priv *fw_priv = to_fw_priv(ref);
359 struct firmware_cache *fwc = fw_priv->fwc;
Ming Lei1f2b7952012-08-04 12:01:21 +0800360
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800361 pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800362 __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800363 (unsigned int)fw_priv->size);
Ming Lei1f2b7952012-08-04 12:01:21 +0800364
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800365 list_del(&fw_priv->list);
Ming Lei1f2b7952012-08-04 12:01:21 +0800366 spin_unlock(&fwc->lock);
367
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100368#ifdef CONFIG_FW_LOADER_USER_HELPER
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800369 if (fw_priv->is_paged_buf) {
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100370 int i;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800371 vunmap(fw_priv->data);
372 for (i = 0; i < fw_priv->nr_pages; i++)
373 __free_page(fw_priv->pages[i]);
374 vfree(fw_priv->pages);
Ming Lei746058f2012-10-09 12:01:03 +0800375 } else
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100376#endif
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800377 if (!fw_priv->allocated_size)
378 vfree(fw_priv->data);
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800379 kfree_const(fw_priv->fw_name);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800380 kfree(fw_priv);
Ming Lei1f2b7952012-08-04 12:01:21 +0800381}
382
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800383static void free_fw_priv(struct fw_priv *fw_priv)
Ming Lei1f2b7952012-08-04 12:01:21 +0800384{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800385 struct firmware_cache *fwc = fw_priv->fwc;
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800386 spin_lock(&fwc->lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800387 if (!kref_put(&fw_priv->ref, __free_fw_priv))
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800388 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800389}
390
Ming Lei746058f2012-10-09 12:01:03 +0800391/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800392static char fw_path_para[256];
393static const char * const fw_path[] = {
394 fw_path_para,
Ming Lei746058f2012-10-09 12:01:03 +0800395 "/lib/firmware/updates/" UTS_RELEASE,
396 "/lib/firmware/updates",
397 "/lib/firmware/" UTS_RELEASE,
398 "/lib/firmware"
399};
400
Ming Lei27602842012-11-03 17:47:58 +0800401/*
402 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
403 * from kernel command line because firmware_class is generally built in
404 * kernel instead of module.
405 */
406module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
407MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
408
Stephen Boyda098ecd2016-08-02 14:04:28 -0700409static int
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800410fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv)
Ming Lei746058f2012-10-09 12:01:03 +0800411{
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500412 loff_t size;
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700413 int i, len;
Neil Horman3e358ac2013-09-06 15:36:08 -0400414 int rc = -ENOENT;
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700415 char *path;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700416 enum kernel_read_file_id id = READING_FIRMWARE;
417 size_t msize = INT_MAX;
418
419 /* Already populated data member means we're loading into a buffer */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800420 if (fw_priv->data) {
Stephen Boyda098ecd2016-08-02 14:04:28 -0700421 id = READING_FIRMWARE_PREALLOC_BUFFER;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800422 msize = fw_priv->allocated_size;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700423 }
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700424
425 path = __getname();
426 if (!path)
427 return -ENOMEM;
Ming Lei746058f2012-10-09 12:01:03 +0800428
429 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
Ming Lei27602842012-11-03 17:47:58 +0800430 /* skip the unset customized path */
431 if (!fw_path[i][0])
432 continue;
433
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700434 len = snprintf(path, PATH_MAX, "%s/%s",
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800435 fw_path[i], fw_priv->fw_name);
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700436 if (len >= PATH_MAX) {
437 rc = -ENAMETOOLONG;
438 break;
439 }
Ming Lei746058f2012-10-09 12:01:03 +0800440
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800441 fw_priv->size = 0;
442 rc = kernel_read_file_from_path(path, &fw_priv->data, &size,
443 msize, id);
Kees Cook4b2530d2016-02-04 13:15:02 -0800444 if (rc) {
Luis R. Rodriguez8e516aa2016-02-28 21:57:55 +0100445 if (rc == -ENOENT)
446 dev_dbg(device, "loading %s failed with error %d\n",
447 path, rc);
448 else
449 dev_warn(device, "loading %s failed with error %d\n",
450 path, rc);
Kees Cook4b2530d2016-02-04 13:15:02 -0800451 continue;
452 }
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800453 dev_dbg(device, "direct-loading %s\n", fw_priv->fw_name);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800454 fw_priv->size = size;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800455 fw_state_done(fw_priv);
Kees Cook4b2530d2016-02-04 13:15:02 -0800456 break;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100457 }
Kees Cook4b2530d2016-02-04 13:15:02 -0800458 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100459
Neil Horman3e358ac2013-09-06 15:36:08 -0400460 return rc;
Ming Lei746058f2012-10-09 12:01:03 +0800461}
462
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100463/* firmware holds the ownership of pages */
464static void firmware_free_data(const struct firmware *fw)
465{
466 /* Loaded directly? */
467 if (!fw->priv) {
468 vfree(fw->data);
469 return;
470 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800471 free_fw_priv(fw->priv);
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100472}
473
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100474/* store the pages buffer info firmware from buf */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800475static void fw_set_page_data(struct fw_priv *fw_priv, struct firmware *fw)
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100476{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800477 fw->priv = fw_priv;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100478#ifdef CONFIG_FW_LOADER_USER_HELPER
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800479 fw->pages = fw_priv->pages;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100480#endif
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800481 fw->size = fw_priv->size;
482 fw->data = fw_priv->data;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100483
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800484 pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800485 __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800486 (unsigned int)fw_priv->size);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100487}
488
489#ifdef CONFIG_PM_SLEEP
490static void fw_name_devm_release(struct device *dev, void *res)
491{
492 struct fw_name_devm *fwn = res;
493
494 if (fwn->magic == (unsigned long)&fw_cache)
495 pr_debug("%s: fw_name-%s devm-%p released\n",
496 __func__, fwn->name, res);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700497 kfree_const(fwn->name);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100498}
499
500static int fw_devm_match(struct device *dev, void *res,
501 void *match_data)
502{
503 struct fw_name_devm *fwn = res;
504
505 return (fwn->magic == (unsigned long)&fw_cache) &&
506 !strcmp(fwn->name, match_data);
507}
508
509static struct fw_name_devm *fw_find_devm_name(struct device *dev,
510 const char *name)
511{
512 struct fw_name_devm *fwn;
513
514 fwn = devres_find(dev, fw_name_devm_release,
515 fw_devm_match, (void *)name);
516 return fwn;
517}
518
519/* add firmware name into devres list */
520static int fw_add_devm_name(struct device *dev, const char *name)
521{
522 struct fw_name_devm *fwn;
523
524 fwn = fw_find_devm_name(dev, name);
525 if (fwn)
526 return 1;
527
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700528 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
529 GFP_KERNEL);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100530 if (!fwn)
531 return -ENOMEM;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700532 fwn->name = kstrdup_const(name, GFP_KERNEL);
533 if (!fwn->name) {
Vladimir Zapolskiya885de62015-07-29 23:26:28 +0300534 devres_free(fwn);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700535 return -ENOMEM;
536 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100537
538 fwn->magic = (unsigned long)&fw_cache;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100539 devres_add(dev, fwn);
540
541 return 0;
542}
543#else
544static int fw_add_devm_name(struct device *dev, const char *name)
545{
546 return 0;
547}
548#endif
549
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800550static int assign_fw(struct firmware *fw, struct device *device,
551 unsigned int opt_flags)
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700552{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800553 struct fw_priv *fw_priv = fw->priv;
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700554
555 mutex_lock(&fw_lock);
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800556 if (!fw_priv->size || fw_state_is_aborted(fw_priv)) {
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700557 mutex_unlock(&fw_lock);
558 return -ENOENT;
559 }
560
561 /*
562 * add firmware name into devres list so that we can auto cache
563 * and uncache firmware for device.
564 *
565 * device may has been deleted already, but the problem
566 * should be fixed in devres or driver core.
567 */
568 /* don't cache firmware handled without uevent */
569 if (device && (opt_flags & FW_OPT_UEVENT) &&
570 !(opt_flags & FW_OPT_NOCACHE))
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800571 fw_add_devm_name(device, fw_priv->fw_name);
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700572
573 /*
574 * After caching firmware image is started, let it piggyback
575 * on request firmware.
576 */
577 if (!(opt_flags & FW_OPT_NOCACHE) &&
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800578 fw_priv->fwc->state == FW_LOADER_START_CACHE) {
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800579 if (fw_cache_piggyback_on_request(fw_priv->fw_name))
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800580 kref_get(&fw_priv->ref);
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700581 }
582
583 /* pass the pages buffer to driver at the last minute */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800584 fw_set_page_data(fw_priv, fw);
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700585 mutex_unlock(&fw_lock);
586 return 0;
587}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100588
589/*
590 * user-mode helper code
591 */
592#ifdef CONFIG_FW_LOADER_USER_HELPER
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800593struct fw_sysfs {
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100594 bool nowait;
595 struct device dev;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800596 struct fw_priv *fw_priv;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100597 struct firmware *fw;
598};
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100599
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800600static struct fw_sysfs *to_fw_sysfs(struct device *dev)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700601{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800602 return container_of(dev, struct fw_sysfs, dev);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700603}
604
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800605static void __fw_load_abort(struct fw_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Ming Lei87597932013-06-15 16:36:38 +0800607 /*
608 * There is a small window in which user can write to 'loading'
609 * between loading done and disappearance of 'loading'
610 */
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800611 if (fw_sysfs_done(fw_priv))
Ming Lei87597932013-06-15 16:36:38 +0800612 return;
613
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800614 list_del_init(&fw_priv->pending_list);
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800615 fw_state_aborted(fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800618static void fw_load_abort(struct fw_sysfs *fw_sysfs)
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700619{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800620 struct fw_priv *fw_priv = fw_sysfs->fw_priv;
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700621
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800622 __fw_load_abort(fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Takashi Iwaife304142013-05-22 18:28:38 +0200625static LIST_HEAD(pending_fw_head);
626
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -0700627static void kill_pending_fw_fallback_reqs(bool only_kill_custom)
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700628{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800629 struct fw_priv *fw_priv;
630 struct fw_priv *next;
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700631
632 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800633 list_for_each_entry_safe(fw_priv, next, &pending_fw_head,
634 pending_list) {
635 if (!fw_priv->need_uevent || !only_kill_custom)
636 __fw_load_abort(fw_priv);
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700637 }
638 mutex_unlock(&fw_lock);
639}
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700640
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700641static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
642 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 return sprintf(buf, "%d\n", loading_timeout);
645}
646
647/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800648 * firmware_timeout_store - set number of seconds to wait for firmware
649 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800650 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800651 * @buf: buffer to scan for timeout value
652 * @count: number of bytes in @buf
653 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800655 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 * firmware will be provided.
657 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800658 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 **/
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700660static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
661 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700664 if (loading_timeout < 0)
665 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return count;
668}
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100669static CLASS_ATTR_RW(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100671static struct attribute *firmware_class_attrs[] = {
672 &class_attr_timeout.attr,
673 NULL,
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800674};
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100675ATTRIBUTE_GROUPS(firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800677static void fw_dev_release(struct device *dev)
678{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800679 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800680
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800681 kfree(fw_sysfs);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800682}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800684static int do_firmware_uevent(struct fw_sysfs *fw_sysfs, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800686 if (add_uevent_var(env, "FIRMWARE=%s", fw_sysfs->fw_priv->fw_name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200688 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700689 return -ENOMEM;
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800690 if (add_uevent_var(env, "ASYNC=%d", fw_sysfs->nowait))
Johannes Berge9045f92010-03-29 17:57:20 +0200691 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 return 0;
694}
695
Linus Torvalds6f957722015-07-09 11:20:01 -0700696static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
697{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800698 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Linus Torvalds6f957722015-07-09 11:20:01 -0700699 int err = 0;
700
701 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800702 if (fw_sysfs->fw_priv)
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800703 err = do_firmware_uevent(fw_sysfs, env);
Linus Torvalds6f957722015-07-09 11:20:01 -0700704 mutex_unlock(&fw_lock);
705 return err;
706}
707
Adrian Bunk1b81d662006-05-20 15:00:16 -0700708static struct class firmware_class = {
709 .name = "firmware",
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100710 .class_groups = firmware_class_groups,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700711 .dev_uevent = firmware_uevent,
712 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700713};
714
Luis R. Rodriguez6bb9cf32017-11-20 09:45:33 -0800715static inline int register_sysfs_loader(void)
716{
717 return class_register(&firmware_class);
718}
719
720static inline void unregister_sysfs_loader(void)
721{
722 class_unregister(&firmware_class);
723}
724
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700725static ssize_t firmware_loading_show(struct device *dev,
726 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800728 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Ming Lei87597932013-06-15 16:36:38 +0800729 int loading = 0;
730
731 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800732 if (fw_sysfs->fw_priv)
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800733 loading = fw_sysfs_loading(fw_sysfs->fw_priv);
Ming Lei87597932013-06-15 16:36:38 +0800734 mutex_unlock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return sprintf(buf, "%d\n", loading);
737}
738
David Woodhouse6e03a202009-04-09 22:04:07 -0700739/* Some architectures don't have PAGE_KERNEL_RO */
740#ifndef PAGE_KERNEL_RO
741#define PAGE_KERNEL_RO PAGE_KERNEL
742#endif
Ming Lei253c9242012-10-09 12:01:02 +0800743
744/* one pages buffer should be mapped/unmapped only once */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800745static int map_fw_priv_pages(struct fw_priv *fw_priv)
Ming Lei253c9242012-10-09 12:01:02 +0800746{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800747 if (!fw_priv->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800748 return 0;
749
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800750 vunmap(fw_priv->data);
751 fw_priv->data = vmap(fw_priv->pages, fw_priv->nr_pages, 0,
752 PAGE_KERNEL_RO);
753 if (!fw_priv->data)
Ming Lei253c9242012-10-09 12:01:02 +0800754 return -ENOMEM;
755 return 0;
756}
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800759 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700760 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800761 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800762 * @buf: buffer to scan for loading control value
763 * @count: number of bytes in @buf
764 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 * The relevant values are:
766 *
767 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800768 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 * -1: Conclude the load with an error and discard any written data.
770 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700771static ssize_t firmware_loading_store(struct device *dev,
772 struct device_attribute *attr,
773 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800775 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800776 struct fw_priv *fw_priv;
Kees Cook6593d922014-02-25 13:06:00 -0800777 ssize_t written = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700779 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Neil Hormaneea915b2012-01-02 15:31:23 -0500781 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800782 fw_priv = fw_sysfs->fw_priv;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800783 if (fw_state_is_aborted(fw_priv))
Neil Hormaneea915b2012-01-02 15:31:23 -0500784 goto out;
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 switch (loading) {
787 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800788 /* discarding any previous partial load */
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800789 if (!fw_sysfs_done(fw_priv)) {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800790 for (i = 0; i < fw_priv->nr_pages; i++)
791 __free_page(fw_priv->pages[i]);
792 vfree(fw_priv->pages);
793 fw_priv->pages = NULL;
794 fw_priv->page_array_size = 0;
795 fw_priv->nr_pages = 0;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800796 fw_state_start(fw_priv);
Ming Lei28eefa72012-08-04 12:01:17 +0800797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 break;
799 case 0:
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800800 if (fw_sysfs_loading(fw_priv)) {
Kees Cook6593d922014-02-25 13:06:00 -0800801 int rc;
802
Ming Lei253c9242012-10-09 12:01:02 +0800803 /*
804 * Several loading requests may be pending on
805 * one same firmware buf, so let all requests
806 * see the mapped 'buf->data' once the loading
807 * is completed.
808 * */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800809 rc = map_fw_priv_pages(fw_priv);
Kees Cook6593d922014-02-25 13:06:00 -0800810 if (rc)
zhang jun2b1278c2014-02-13 15:18:47 +0800811 dev_err(dev, "%s: map pages failed\n",
812 __func__);
Kees Cook6593d922014-02-25 13:06:00 -0800813 else
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500814 rc = security_kernel_post_read_file(NULL,
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800815 fw_priv->data, fw_priv->size,
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500816 READING_FIRMWARE);
Kees Cook6593d922014-02-25 13:06:00 -0800817
818 /*
819 * Same logic as fw_load_abort, only the DONE bit
820 * is ignored and we set ABORT only on failure.
821 */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800822 list_del_init(&fw_priv->pending_list);
Kees Cook6593d922014-02-25 13:06:00 -0800823 if (rc) {
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800824 fw_state_aborted(fw_priv);
Kees Cook6593d922014-02-25 13:06:00 -0800825 written = rc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100826 } else {
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -0800827 fw_state_done(fw_priv);
Kees Cook6593d922014-02-25 13:06:00 -0800828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 break;
830 }
831 /* fallthrough */
832 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700833 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /* fallthrough */
835 case -1:
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800836 fw_load_abort(fw_sysfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 break;
838 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500839out:
840 mutex_unlock(&fw_lock);
Kees Cook6593d922014-02-25 13:06:00 -0800841 return written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700844static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800846static void firmware_rw_data(struct fw_priv *fw_priv, char *buffer,
Stephen Boyda098ecd2016-08-02 14:04:28 -0700847 loff_t offset, size_t count, bool read)
848{
849 if (read)
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800850 memcpy(buffer, fw_priv->data + offset, count);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700851 else
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800852 memcpy(fw_priv->data + offset, buffer, count);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700853}
854
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800855static void firmware_rw(struct fw_priv *fw_priv, char *buffer,
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700856 loff_t offset, size_t count, bool read)
857{
858 while (count) {
859 void *page_data;
860 int page_nr = offset >> PAGE_SHIFT;
861 int page_ofs = offset & (PAGE_SIZE-1);
862 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
863
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800864 page_data = kmap(fw_priv->pages[page_nr]);
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700865
866 if (read)
867 memcpy(buffer, page_data + page_ofs, page_cnt);
868 else
869 memcpy(page_data + page_ofs, buffer, page_cnt);
870
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800871 kunmap(fw_priv->pages[page_nr]);
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700872 buffer += page_cnt;
873 offset += page_cnt;
874 count -= page_cnt;
875 }
876}
877
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700878static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
879 struct bin_attribute *bin_attr,
880 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200882 struct device *dev = kobj_to_dev(kobj);
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800883 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800884 struct fw_priv *fw_priv;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700885 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Laura Garciacad1e552006-05-23 23:22:38 +0200887 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800888 fw_priv = fw_sysfs->fw_priv;
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800889 if (!fw_priv || fw_sysfs_done(fw_priv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 ret_count = -ENODEV;
891 goto out;
892 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800893 if (offset > fw_priv->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200894 ret_count = 0;
895 goto out;
896 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800897 if (count > fw_priv->size - offset)
898 count = fw_priv->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700899
900 ret_count = count;
901
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800902 if (fw_priv->data)
903 firmware_rw_data(fw_priv, buffer, offset, count, true);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700904 else
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800905 firmware_rw(fw_priv, buffer, offset, count, true);
David Woodhouse6e03a202009-04-09 22:04:07 -0700906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907out:
Laura Garciacad1e552006-05-23 23:22:38 +0200908 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return ret_count;
910}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800911
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800912static int fw_realloc_pages(struct fw_sysfs *fw_sysfs, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800914 struct fw_priv *fw_priv= fw_sysfs->fw_priv;
Fabian Fredericka76040d2014-07-01 21:13:30 +0200915 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
David Woodhouse6e03a202009-04-09 22:04:07 -0700917 /* If the array of pages is too small, grow it... */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800918 if (fw_priv->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700919 int new_array_size = max(pages_needed,
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800920 fw_priv->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700921 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Chen Feng10a3fbf2015-12-16 17:03:15 +0800923 new_pages = vmalloc(new_array_size * sizeof(void *));
David Woodhouse6e03a202009-04-09 22:04:07 -0700924 if (!new_pages) {
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800925 fw_load_abort(fw_sysfs);
David Woodhouse6e03a202009-04-09 22:04:07 -0700926 return -ENOMEM;
927 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800928 memcpy(new_pages, fw_priv->pages,
929 fw_priv->page_array_size * sizeof(void *));
930 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
931 (new_array_size - fw_priv->page_array_size));
932 vfree(fw_priv->pages);
933 fw_priv->pages = new_pages;
934 fw_priv->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700936
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800937 while (fw_priv->nr_pages < pages_needed) {
938 fw_priv->pages[fw_priv->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700939 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
940
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800941 if (!fw_priv->pages[fw_priv->nr_pages]) {
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800942 fw_load_abort(fw_sysfs);
David Woodhouse6e03a202009-04-09 22:04:07 -0700943 return -ENOMEM;
944 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800945 fw_priv->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return 0;
948}
949
950/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800951 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700952 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700953 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700954 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800955 * @buffer: buffer being written
956 * @offset: buffer offset for write in total data store area
957 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800959 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 * the driver as a firmware image.
961 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700962static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
963 struct bin_attribute *bin_attr,
964 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200966 struct device *dev = kobj_to_dev(kobj);
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800967 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800968 struct fw_priv *fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 ssize_t retval;
970
971 if (!capable(CAP_SYS_RAWIO))
972 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700973
Laura Garciacad1e552006-05-23 23:22:38 +0200974 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800975 fw_priv = fw_sysfs->fw_priv;
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -0800976 if (!fw_priv || fw_sysfs_done(fw_priv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 retval = -ENODEV;
978 goto out;
979 }
Ming Lei65710cb2012-08-04 12:01:16 +0800980
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800981 if (fw_priv->data) {
982 if (offset + count > fw_priv->allocated_size) {
Stephen Boyda098ecd2016-08-02 14:04:28 -0700983 retval = -ENOMEM;
984 goto out;
985 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800986 firmware_rw_data(fw_priv, buffer, offset, count, false);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700987 retval = count;
988 } else {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800989 retval = fw_realloc_pages(fw_sysfs, offset + count);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700990 if (retval)
991 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Stephen Boyda098ecd2016-08-02 14:04:28 -0700993 retval = count;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800994 firmware_rw(fw_priv, buffer, offset, count, false);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700995 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700996
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800997 fw_priv->size = max_t(size_t, offset + count, fw_priv->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998out:
Laura Garciacad1e552006-05-23 23:22:38 +0200999 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return retval;
1001}
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001002
Dmitry Torokhov0983ca22010-06-04 00:54:37 -07001003static struct bin_attribute firmware_attr_data = {
1004 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 .size = 0,
1006 .read = firmware_data_read,
1007 .write = firmware_data_write,
1008};
1009
Takashi Iwai46239902015-02-04 15:18:07 +01001010static struct attribute *fw_dev_attrs[] = {
1011 &dev_attr_loading.attr,
1012 NULL
1013};
1014
1015static struct bin_attribute *fw_dev_bin_attrs[] = {
1016 &firmware_attr_data,
1017 NULL
1018};
1019
1020static const struct attribute_group fw_dev_attr_group = {
1021 .attrs = fw_dev_attrs,
1022 .bin_attrs = fw_dev_bin_attrs,
1023};
1024
1025static const struct attribute_group *fw_dev_attr_groups[] = {
1026 &fw_dev_attr_group,
1027 NULL
1028};
1029
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001030static struct fw_sysfs *
Stephen Boyddddb5542012-03-28 23:30:43 +02001031fw_create_instance(struct firmware *firmware, const char *fw_name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001032 struct device *device, unsigned int opt_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001034 struct fw_sysfs *fw_sysfs;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001035 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001037 fw_sysfs = kzalloc(sizeof(*fw_sysfs), GFP_KERNEL);
1038 if (!fw_sysfs) {
1039 fw_sysfs = ERR_PTR(-ENOMEM);
Ming Lei12446912012-08-04 12:01:20 +08001040 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001043 fw_sysfs->nowait = !!(opt_flags & FW_OPT_NOWAIT);
1044 fw_sysfs->fw = firmware;
1045 f_dev = &fw_sysfs->dev;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001046
1047 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +08001048 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -07001049 f_dev->parent = device;
1050 f_dev->class = &firmware_class;
Takashi Iwai46239902015-02-04 15:18:07 +01001051 f_dev->groups = fw_dev_attr_groups;
Ming Lei12446912012-08-04 12:01:20 +08001052exit:
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001053 return fw_sysfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001055
1056/* load a firmware via user helper */
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001057static int _request_firmware_load(struct fw_sysfs *fw_sysfs,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001058 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001059{
1060 int retval = 0;
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001061 struct device *f_dev = &fw_sysfs->dev;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001062 struct fw_priv *fw_priv = fw_sysfs->fw_priv;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001063
1064 /* fall back on userspace loading */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001065 if (!fw_priv->data)
1066 fw_priv->is_paged_buf = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001067
1068 dev_set_uevent_suppress(f_dev, true);
1069
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001070 retval = device_add(f_dev);
1071 if (retval) {
1072 dev_err(f_dev, "%s: device_register failed\n", __func__);
1073 goto err_put_dev;
1074 }
1075
Maxime Bizon1eeeef12013-08-29 20:28:13 +02001076 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001077 list_add(&fw_priv->pending_list, &pending_fw_head);
Maxime Bizon1eeeef12013-08-29 20:28:13 +02001078 mutex_unlock(&fw_lock);
1079
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001080 if (opt_flags & FW_OPT_UEVENT) {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001081 fw_priv->need_uevent = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001082 dev_set_uevent_suppress(f_dev, false);
Luis R. Rodriguez31034f22017-11-20 10:23:49 -08001083 dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_name);
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001084 kobject_uevent(&fw_sysfs->dev.kobj, KOBJ_ADD);
Ming Lei68ff2a02015-01-13 00:02:01 +08001085 } else {
1086 timeout = MAX_JIFFY_OFFSET;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001087 }
1088
Luis R. Rodriguez33a5b182017-11-20 10:23:54 -08001089 retval = fw_sysfs_wait_timeout(fw_priv, timeout);
Bjorn Andersson5d47ec02016-12-06 17:01:45 -08001090 if (retval < 0) {
Ming Lei0cb64242015-01-13 00:01:35 +08001091 mutex_lock(&fw_lock);
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001092 fw_load_abort(fw_sysfs);
Ming Lei0cb64242015-01-13 00:01:35 +08001093 mutex_unlock(&fw_lock);
1094 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001095
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -08001096 if (fw_state_is_aborted(fw_priv)) {
Luis R. Rodriguez76098b32017-07-20 13:13:39 -07001097 if (retval == -ERESTARTSYS)
1098 retval = -EINTR;
1099 else
1100 retval = -EAGAIN;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001101 } else if (fw_priv->is_paged_buf && !fw_priv->data)
zhang jun2b1278c2014-02-13 15:18:47 +08001102 retval = -ENOMEM;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001103
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001104 device_del(f_dev);
1105err_put_dev:
1106 put_device(f_dev);
1107 return retval;
1108}
1109
1110static int fw_load_from_user_helper(struct firmware *firmware,
1111 const char *name, struct device *device,
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001112 unsigned int opt_flags)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001113{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001114 struct fw_sysfs *fw_sysfs;
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001115 long timeout;
1116 int ret;
1117
1118 timeout = firmware_loading_timeout();
1119 if (opt_flags & FW_OPT_NOWAIT) {
1120 timeout = usermodehelper_read_lock_wait(timeout);
1121 if (!timeout) {
1122 dev_dbg(device, "firmware: %s loading timed out\n",
1123 name);
1124 return -EBUSY;
1125 }
1126 } else {
1127 ret = usermodehelper_read_trylock();
1128 if (WARN_ON(ret)) {
1129 dev_err(device, "firmware: %s will not be loaded\n",
1130 name);
1131 return ret;
1132 }
1133 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001134
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001135 fw_sysfs = fw_create_instance(firmware, name, device, opt_flags);
1136 if (IS_ERR(fw_sysfs)) {
1137 ret = PTR_ERR(fw_sysfs);
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001138 goto out_unlock;
1139 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001140
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001141 fw_sysfs->fw_priv = firmware->priv;
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001142 ret = _request_firmware_load(fw_sysfs, opt_flags, timeout);
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001143
1144 if (!ret)
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001145 ret = assign_fw(firmware, device, opt_flags);
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001146
1147out_unlock:
1148 usermodehelper_read_unlock();
1149
1150 return ret;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001151}
Ming Leiddf1f062013-06-04 10:01:13 +08001152
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001153#ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK
1154static bool fw_force_sysfs_fallback(unsigned int opt_flags)
1155{
1156 return true;
1157}
1158#else
1159static bool fw_force_sysfs_fallback(unsigned int opt_flags)
1160{
1161 if (!(opt_flags & FW_OPT_USERHELPER))
1162 return false;
1163 return true;
1164}
1165#endif
1166
1167static bool fw_run_sysfs_fallback(unsigned int opt_flags)
1168{
1169 if ((opt_flags & FW_OPT_NOFALLBACK))
1170 return false;
1171
1172 return fw_force_sysfs_fallback(opt_flags);
1173}
1174
Luis R. Rodriguez25d39132017-11-20 10:23:57 -08001175static int fw_sysfs_fallback(struct firmware *fw, const char *name,
1176 struct device *device,
1177 unsigned int opt_flags,
1178 int ret)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001179{
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001180 if (!fw_run_sysfs_fallback(opt_flags))
Luis R. Rodriguez25d39132017-11-20 10:23:57 -08001181 return ret;
1182
1183 dev_warn(device, "Falling back to user helper\n");
1184 return fw_load_from_user_helper(fw, name, device, opt_flags);
1185}
1186#else /* CONFIG_FW_LOADER_USER_HELPER */
1187static int fw_sysfs_fallback(struct firmware *fw, const char *name,
1188 struct device *device,
1189 unsigned int opt_flags,
1190 int ret)
1191{
1192 /* Keep carrying over the same error */
1193 return ret;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001194}
Takashi Iwai807be032013-01-31 11:13:57 +01001195
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -07001196static inline void kill_pending_fw_fallback_reqs(bool only_kill_custom) { }
Ming Leiddf1f062013-06-04 10:01:13 +08001197
Luis R. Rodriguez6bb9cf32017-11-20 09:45:33 -08001198static inline int register_sysfs_loader(void)
1199{
1200 return 0;
1201}
1202
1203static inline void unregister_sysfs_loader(void)
1204{
1205}
1206
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001207#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001209/* prepare firmware and firmware_buf structs;
1210 * return 0 if a firmware is already assigned, 1 if need to load one,
1211 * or a negative error code
1212 */
1213static int
1214_request_firmware_prepare(struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001215 struct device *device, void *dbuf, size_t size)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001216{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 struct firmware *firmware;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001218 struct fw_priv *fw_priv;
Ming Lei1f2b7952012-08-04 12:01:21 +08001219 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Jiri Slaby4aed0642005-09-13 01:25:01 -07001221 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -07001223 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1224 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001225 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Stephen Boyda098ecd2016-08-02 14:04:28 -07001228 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
Luis R. Rodriguezed046302015-04-29 16:30:43 -07001229 dev_dbg(device, "using built-in %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001230 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +01001231 }
1232
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001233 ret = alloc_lookup_fw_priv(name, &fw_cache, &fw_priv, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +08001234
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001235 /*
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001236 * bind with 'priv' now to avoid warning in failure path
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001237 * of requesting firmware.
1238 */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001239 firmware->priv = fw_priv;
Ming Lei1f2b7952012-08-04 12:01:21 +08001240
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001241 if (ret > 0) {
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -08001242 ret = fw_state_wait(fw_priv);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001243 if (!ret) {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001244 fw_set_page_data(fw_priv, firmware);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001245 return 0; /* assigned */
1246 }
Stephen Boyddddb5542012-03-28 23:30:43 +02001247 }
Ming Lei1f2b7952012-08-04 12:01:21 +08001248
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001249 if (ret < 0)
1250 return ret;
1251 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001252}
1253
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001254/*
1255 * Batched requests need only one wake, we need to do this step last due to the
1256 * fallback mechanism. The buf is protected with kref_get(), and it won't be
1257 * released until the last user calls release_firmware().
1258 *
1259 * Failed batched requests are possible as well, in such cases we just share
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001260 * the struct fw_priv and won't release it until all requests are woken
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001261 * and have gone through this same path.
1262 */
1263static void fw_abort_batch_reqs(struct firmware *fw)
1264{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001265 struct fw_priv *fw_priv;
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001266
1267 /* Loaded directly? */
1268 if (!fw || !fw->priv)
1269 return;
1270
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001271 fw_priv = fw->priv;
Luis R. Rodriguezf1b9cf32017-11-20 10:23:53 -08001272 if (!fw_state_is_aborted(fw_priv))
1273 fw_state_aborted(fw_priv);
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001274}
1275
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001276/* called from request_firmware() and request_firmware_work_func() */
1277static int
1278_request_firmware(const struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001279 struct device *device, void *buf, size_t size,
1280 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001281{
Brian Norris715780a2015-12-09 14:50:28 -08001282 struct firmware *fw = NULL;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001283 int ret;
1284
1285 if (!firmware_p)
1286 return -EINVAL;
1287
Brian Norris715780a2015-12-09 14:50:28 -08001288 if (!name || name[0] == '\0') {
1289 ret = -EINVAL;
1290 goto out;
1291 }
Kees Cook471b0952014-09-18 11:25:37 -07001292
Stephen Boyda098ecd2016-08-02 14:04:28 -07001293 ret = _request_firmware_prepare(&fw, name, device, buf, size);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001294 if (ret <= 0) /* error or already assigned */
1295 goto out;
1296
Neil Horman3e358ac2013-09-06 15:36:08 -04001297 ret = fw_get_filesystem_firmware(device, fw->priv);
1298 if (ret) {
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001299 if (!(opt_flags & FW_OPT_NO_WARN))
Takashi Iwaibba3a872013-12-02 15:38:16 +01001300 dev_warn(device,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001301 "Direct firmware load for %s failed with error %d\n",
1302 name, ret);
Luis R. Rodriguez25d39132017-11-20 10:23:57 -08001303 ret = fw_sysfs_fallback(fw, name, device, opt_flags, ret);
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001304 } else
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001305 ret = assign_fw(fw, device, opt_flags);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001306
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001307 out:
1308 if (ret < 0) {
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001309 fw_abort_batch_reqs(fw);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001310 release_firmware(fw);
1311 fw = NULL;
1312 }
1313
1314 *firmware_p = fw;
1315 return ret;
1316}
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318/**
Kay Sievers312c0042005-11-16 09:00:00 +01001319 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001320 * @firmware_p: pointer to firmware image
1321 * @name: name of firmware file
1322 * @device: device for which firmware is being loaded
1323 *
1324 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001325 * of @name for device @device.
1326 *
1327 * Should be called from user context where sleeping is allowed.
1328 *
Kay Sievers312c0042005-11-16 09:00:00 +01001329 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001330 * should be distinctive enough not to be confused with any other
1331 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001332 *
1333 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001334 *
1335 * The function can be called safely inside device's suspend and
1336 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001337 **/
1338int
1339request_firmware(const struct firmware **firmware_p, const char *name,
Andrei Opreaea310032015-03-08 12:41:15 +02001340 struct device *device)
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001341{
Ming Leid6c8aa32013-06-06 20:01:48 +08001342 int ret;
1343
1344 /* Need to pin this module until return */
1345 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001346 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001347 FW_OPT_UEVENT);
Ming Leid6c8aa32013-06-06 20:01:48 +08001348 module_put(THIS_MODULE);
1349 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001350}
Daniel Mackf4945132013-05-23 22:17:18 +02001351EXPORT_SYMBOL(request_firmware);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001352
Takashi Iwaibba3a872013-12-02 15:38:16 +01001353/**
Borislav Petkov3c1556b2014-12-03 22:46:37 +01001354 * request_firmware_direct: - load firmware directly without usermode helper
Takashi Iwaibba3a872013-12-02 15:38:16 +01001355 * @firmware_p: pointer to firmware image
1356 * @name: name of firmware file
1357 * @device: device for which firmware is being loaded
1358 *
1359 * This function works pretty much like request_firmware(), but this doesn't
1360 * fall back to usermode helper even if the firmware couldn't be loaded
1361 * directly from fs. Hence it's useful for loading optional firmwares, which
1362 * aren't always present, without extra long timeouts of udev.
1363 **/
1364int request_firmware_direct(const struct firmware **firmware_p,
1365 const char *name, struct device *device)
1366{
1367 int ret;
Andrei Opreaea310032015-03-08 12:41:15 +02001368
Takashi Iwaibba3a872013-12-02 15:38:16 +01001369 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001370 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001371 FW_OPT_UEVENT | FW_OPT_NO_WARN |
1372 FW_OPT_NOFALLBACK);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001373 module_put(THIS_MODULE);
1374 return ret;
1375}
1376EXPORT_SYMBOL_GPL(request_firmware_direct);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001377
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001378/**
Stephen Boyda098ecd2016-08-02 14:04:28 -07001379 * request_firmware_into_buf - load firmware into a previously allocated buffer
1380 * @firmware_p: pointer to firmware image
1381 * @name: name of firmware file
1382 * @device: device for which firmware is being loaded and DMA region allocated
1383 * @buf: address of buffer to load firmware into
1384 * @size: size of buffer
1385 *
1386 * This function works pretty much like request_firmware(), but it doesn't
1387 * allocate a buffer to hold the firmware data. Instead, the firmware
1388 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
1389 * data member is pointed at @buf.
1390 *
1391 * This function doesn't cache firmware either.
1392 */
1393int
1394request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
1395 struct device *device, void *buf, size_t size)
1396{
1397 int ret;
1398
1399 __module_get(THIS_MODULE);
1400 ret = _request_firmware(firmware_p, name, device, buf, size,
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001401 FW_OPT_UEVENT | FW_OPT_NOCACHE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001402 module_put(THIS_MODULE);
1403 return ret;
1404}
1405EXPORT_SYMBOL(request_firmware_into_buf);
1406
1407/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001409 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001411void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
1413 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001414 if (!fw_is_builtin_firmware(fw))
1415 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 kfree(fw);
1417 }
1418}
Daniel Mackf4945132013-05-23 22:17:18 +02001419EXPORT_SYMBOL(release_firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421/* Async support */
1422struct firmware_work {
1423 struct work_struct work;
1424 struct module *module;
1425 const char *name;
1426 struct device *device;
1427 void *context;
1428 void (*cont)(const struct firmware *fw, void *context);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001429 unsigned int opt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430};
1431
Stephen Boyda36cf842012-03-28 23:31:00 +02001432static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Stephen Boyda36cf842012-03-28 23:31:00 +02001434 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001436
Stephen Boyda36cf842012-03-28 23:31:00 +02001437 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001438
Stephen Boyda098ecd2016-08-02 14:04:28 -07001439 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001440 fw_work->opt_flags);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001441 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001442 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 module_put(fw_work->module);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001445 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
1449/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001450 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001451 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001452 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001453 * is non-zero else the firmware copy must be done manually.
1454 * @name: name of firmware file
1455 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001456 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001457 * @context: will be passed over to @cont, and
1458 * @fw may be %NULL if firmware request fails.
1459 * @cont: function will be called asynchronously when the firmware
1460 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001462 * Caller must hold the reference count of @device.
1463 *
Ming Lei6f21a622012-08-04 12:01:24 +08001464 * Asynchronous variant of request_firmware() for user contexts:
1465 * - sleep for as small periods as possible since it may
Silvio Fricke88bcef52016-11-25 15:59:47 +01001466 * increase kernel boot time of built-in device drivers
1467 * requesting firmware in their ->probe() methods, if
1468 * @gfp is GFP_KERNEL.
Ming Lei6f21a622012-08-04 12:01:24 +08001469 *
1470 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 **/
1472int
1473request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001474 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001475 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 void (*cont)(const struct firmware *fw, void *context))
1477{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001478 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Andrei Opreaea310032015-03-08 12:41:15 +02001480 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 if (!fw_work)
1482 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001483
1484 fw_work->module = module;
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001485 fw_work->name = kstrdup_const(name, gfp);
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001486 if (!fw_work->name) {
1487 kfree(fw_work);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001488 return -ENOMEM;
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001489 }
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001490 fw_work->device = device;
1491 fw_work->context = context;
1492 fw_work->cont = cont;
Luis R. Rodriguez3f722712017-11-20 10:23:58 -08001493 fw_work->opt_flags = FW_OPT_NOWAIT |
Takashi Iwai5a1379e2014-06-04 17:48:15 +02001494 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (!try_module_get(module)) {
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001497 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 kfree(fw_work);
1499 return -EFAULT;
1500 }
1501
Ming Lei0cfc1e12012-08-04 12:01:23 +08001502 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001503 INIT_WORK(&fw_work->work, request_firmware_work_func);
1504 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return 0;
1506}
Daniel Mackf4945132013-05-23 22:17:18 +02001507EXPORT_SYMBOL(request_firmware_nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Ming Lei90f890812013-06-20 12:30:16 +08001509#ifdef CONFIG_PM_SLEEP
1510static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1511
Ming Lei2887b392012-08-04 12:01:22 +08001512/**
1513 * cache_firmware - cache one firmware image in kernel memory space
1514 * @fw_name: the firmware image name
1515 *
1516 * Cache firmware in kernel memory so that drivers can use it when
1517 * system isn't ready for them to request firmware image from userspace.
1518 * Once it returns successfully, driver can use request_firmware or its
1519 * nowait version to get the cached firmware without any interacting
1520 * with userspace
1521 *
1522 * Return 0 if the firmware image has been cached successfully
1523 * Return !0 otherwise
1524 *
1525 */
Ming Lei93232e42013-06-06 20:01:47 +08001526static int cache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001527{
1528 int ret;
1529 const struct firmware *fw;
1530
1531 pr_debug("%s: %s\n", __func__, fw_name);
1532
1533 ret = request_firmware(&fw, fw_name, NULL);
1534 if (!ret)
1535 kfree(fw);
1536
1537 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1538
1539 return ret;
1540}
1541
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001542static struct fw_priv *lookup_fw_priv(const char *fw_name)
Ming Lei6a2c1232013-06-26 09:28:17 +08001543{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001544 struct fw_priv *tmp;
Ming Lei6a2c1232013-06-26 09:28:17 +08001545 struct firmware_cache *fwc = &fw_cache;
1546
1547 spin_lock(&fwc->lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001548 tmp = __lookup_fw_priv(fw_name);
Ming Lei6a2c1232013-06-26 09:28:17 +08001549 spin_unlock(&fwc->lock);
1550
1551 return tmp;
1552}
1553
Ming Lei2887b392012-08-04 12:01:22 +08001554/**
1555 * uncache_firmware - remove one cached firmware image
1556 * @fw_name: the firmware image name
1557 *
1558 * Uncache one firmware image which has been cached successfully
1559 * before.
1560 *
1561 * Return 0 if the firmware cache has been removed successfully
1562 * Return !0 otherwise
1563 *
1564 */
Ming Lei93232e42013-06-06 20:01:47 +08001565static int uncache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001566{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001567 struct fw_priv *fw_priv;
Ming Lei2887b392012-08-04 12:01:22 +08001568 struct firmware fw;
1569
1570 pr_debug("%s: %s\n", __func__, fw_name);
1571
Stephen Boyda098ecd2016-08-02 14:04:28 -07001572 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
Ming Lei2887b392012-08-04 12:01:22 +08001573 return 0;
1574
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001575 fw_priv = lookup_fw_priv(fw_name);
1576 if (fw_priv) {
1577 free_fw_priv(fw_priv);
Ming Lei2887b392012-08-04 12:01:22 +08001578 return 0;
1579 }
1580
1581 return -EINVAL;
1582}
1583
Ming Lei37276a52012-08-04 12:01:27 +08001584static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1585{
1586 struct fw_cache_entry *fce;
1587
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001588 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
Ming Lei37276a52012-08-04 12:01:27 +08001589 if (!fce)
1590 goto exit;
1591
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001592 fce->name = kstrdup_const(name, GFP_ATOMIC);
1593 if (!fce->name) {
1594 kfree(fce);
1595 fce = NULL;
1596 goto exit;
1597 }
Ming Lei37276a52012-08-04 12:01:27 +08001598exit:
1599 return fce;
1600}
1601
Ming Lei373304f2012-10-09 12:01:01 +08001602static int __fw_entry_found(const char *name)
1603{
1604 struct firmware_cache *fwc = &fw_cache;
1605 struct fw_cache_entry *fce;
1606
1607 list_for_each_entry(fce, &fwc->fw_names, list) {
1608 if (!strcmp(fce->name, name))
1609 return 1;
1610 }
1611 return 0;
1612}
1613
Ming Leiac39b3e2012-08-20 19:04:16 +08001614static int fw_cache_piggyback_on_request(const char *name)
1615{
1616 struct firmware_cache *fwc = &fw_cache;
1617 struct fw_cache_entry *fce;
1618 int ret = 0;
1619
1620 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001621 if (__fw_entry_found(name))
1622 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001623
1624 fce = alloc_fw_cache_entry(name);
1625 if (fce) {
1626 ret = 1;
1627 list_add(&fce->list, &fwc->fw_names);
1628 pr_debug("%s: fw: %s\n", __func__, name);
1629 }
1630found:
1631 spin_unlock(&fwc->name_lock);
1632 return ret;
1633}
1634
Ming Lei37276a52012-08-04 12:01:27 +08001635static void free_fw_cache_entry(struct fw_cache_entry *fce)
1636{
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001637 kfree_const(fce->name);
Ming Lei37276a52012-08-04 12:01:27 +08001638 kfree(fce);
1639}
1640
1641static void __async_dev_cache_fw_image(void *fw_entry,
1642 async_cookie_t cookie)
1643{
1644 struct fw_cache_entry *fce = fw_entry;
1645 struct firmware_cache *fwc = &fw_cache;
1646 int ret;
1647
1648 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001649 if (ret) {
1650 spin_lock(&fwc->name_lock);
1651 list_del(&fce->list);
1652 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001653
Ming Leiac39b3e2012-08-20 19:04:16 +08001654 free_fw_cache_entry(fce);
1655 }
Ming Lei37276a52012-08-04 12:01:27 +08001656}
1657
1658/* called with dev->devres_lock held */
1659static void dev_create_fw_entry(struct device *dev, void *res,
1660 void *data)
1661{
1662 struct fw_name_devm *fwn = res;
1663 const char *fw_name = fwn->name;
1664 struct list_head *head = data;
1665 struct fw_cache_entry *fce;
1666
1667 fce = alloc_fw_cache_entry(fw_name);
1668 if (fce)
1669 list_add(&fce->list, head);
1670}
1671
1672static int devm_name_match(struct device *dev, void *res,
1673 void *match_data)
1674{
1675 struct fw_name_devm *fwn = res;
1676 return (fwn->magic == (unsigned long)match_data);
1677}
1678
Ming Leiab6dd8e2012-08-17 22:07:00 +08001679static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001680{
1681 LIST_HEAD(todo);
1682 struct fw_cache_entry *fce;
1683 struct fw_cache_entry *fce_next;
1684 struct firmware_cache *fwc = &fw_cache;
1685
1686 devres_for_each_res(dev, fw_name_devm_release,
1687 devm_name_match, &fw_cache,
1688 dev_create_fw_entry, &todo);
1689
1690 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1691 list_del(&fce->list);
1692
1693 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001694 /* only one cache entry for one firmware */
1695 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001696 list_add(&fce->list, &fwc->fw_names);
1697 } else {
1698 free_fw_cache_entry(fce);
1699 fce = NULL;
1700 }
Ming Lei37276a52012-08-04 12:01:27 +08001701 spin_unlock(&fwc->name_lock);
1702
Ming Lei373304f2012-10-09 12:01:01 +08001703 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001704 async_schedule_domain(__async_dev_cache_fw_image,
1705 (void *)fce,
1706 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001707 }
1708}
1709
1710static void __device_uncache_fw_images(void)
1711{
1712 struct firmware_cache *fwc = &fw_cache;
1713 struct fw_cache_entry *fce;
1714
1715 spin_lock(&fwc->name_lock);
1716 while (!list_empty(&fwc->fw_names)) {
1717 fce = list_entry(fwc->fw_names.next,
1718 struct fw_cache_entry, list);
1719 list_del(&fce->list);
1720 spin_unlock(&fwc->name_lock);
1721
1722 uncache_firmware(fce->name);
1723 free_fw_cache_entry(fce);
1724
1725 spin_lock(&fwc->name_lock);
1726 }
1727 spin_unlock(&fwc->name_lock);
1728}
1729
1730/**
1731 * device_cache_fw_images - cache devices' firmware
1732 *
1733 * If one device called request_firmware or its nowait version
1734 * successfully before, the firmware names are recored into the
1735 * device's devres link list, so device_cache_fw_images can call
1736 * cache_firmware() to cache these firmwares for the device,
1737 * then the device driver can load its firmwares easily at
1738 * time when system is not ready to complete loading firmware.
1739 */
1740static void device_cache_fw_images(void)
1741{
1742 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001743 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001744 DEFINE_WAIT(wait);
1745
1746 pr_debug("%s\n", __func__);
1747
Ming Lei373304f2012-10-09 12:01:01 +08001748 /* cancel uncache work */
1749 cancel_delayed_work_sync(&fwc->work);
1750
Ming Leiffe53f62012-08-04 12:01:28 +08001751 /*
1752 * use small loading timeout for caching devices' firmware
1753 * because all these firmware images have been loaded
1754 * successfully at lease once, also system is ready for
1755 * completing firmware loading now. The maximum size of
1756 * firmware in current distributions is about 2M bytes,
1757 * so 10 secs should be enough.
1758 */
1759 old_timeout = loading_timeout;
1760 loading_timeout = 10;
1761
Ming Leiac39b3e2012-08-20 19:04:16 +08001762 mutex_lock(&fw_lock);
1763 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001764 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001765 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001766
1767 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001768 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001769
1770 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001771}
1772
1773/**
1774 * device_uncache_fw_images - uncache devices' firmware
1775 *
1776 * uncache all firmwares which have been cached successfully
1777 * by device_uncache_fw_images earlier
1778 */
1779static void device_uncache_fw_images(void)
1780{
1781 pr_debug("%s\n", __func__);
1782 __device_uncache_fw_images();
1783}
1784
1785static void device_uncache_fw_images_work(struct work_struct *work)
1786{
1787 device_uncache_fw_images();
1788}
1789
1790/**
1791 * device_uncache_fw_images_delay - uncache devices firmwares
1792 * @delay: number of milliseconds to delay uncache device firmwares
1793 *
1794 * uncache all devices's firmwares which has been cached successfully
1795 * by device_cache_fw_images after @delay milliseconds.
1796 */
1797static void device_uncache_fw_images_delay(unsigned long delay)
1798{
Shaibal Duttabce66182014-01-31 15:44:58 -08001799 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1800 msecs_to_jiffies(delay));
Ming Lei37276a52012-08-04 12:01:27 +08001801}
1802
Ming Lei07646d92012-08-04 12:01:29 +08001803static int fw_pm_notify(struct notifier_block *notify_block,
1804 unsigned long mode, void *unused)
1805{
1806 switch (mode) {
1807 case PM_HIBERNATION_PREPARE:
1808 case PM_SUSPEND_PREPARE:
Sebastian Capellaf8d5b9e2014-02-18 17:52:08 -08001809 case PM_RESTORE_PREPARE:
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -07001810 /*
1811 * kill pending fallback requests with a custom fallback
1812 * to avoid stalling suspend.
1813 */
1814 kill_pending_fw_fallback_reqs(true);
Ming Lei07646d92012-08-04 12:01:29 +08001815 device_cache_fw_images();
1816 break;
1817
1818 case PM_POST_SUSPEND:
1819 case PM_POST_HIBERNATION:
1820 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001821 /*
1822 * In case that system sleep failed and syscore_suspend is
1823 * not called.
1824 */
1825 mutex_lock(&fw_lock);
1826 fw_cache.state = FW_LOADER_NO_CACHE;
1827 mutex_unlock(&fw_lock);
1828
Ming Lei07646d92012-08-04 12:01:29 +08001829 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1830 break;
1831 }
1832
1833 return 0;
1834}
Ming Lei07646d92012-08-04 12:01:29 +08001835
Ming Leiac39b3e2012-08-20 19:04:16 +08001836/* stop caching firmware once syscore_suspend is reached */
1837static int fw_suspend(void)
1838{
1839 fw_cache.state = FW_LOADER_NO_CACHE;
1840 return 0;
1841}
1842
1843static struct syscore_ops fw_syscore_ops = {
1844 .suspend = fw_suspend,
1845};
Luis R. Rodrigueza67e5032017-11-20 09:45:31 -08001846
Luis R. Rodriguez59b6d852017-11-20 09:45:32 -08001847static int __init register_fw_pm_ops(void)
1848{
1849 int ret;
1850
1851 spin_lock_init(&fw_cache.name_lock);
1852 INIT_LIST_HEAD(&fw_cache.fw_names);
1853
1854 INIT_DELAYED_WORK(&fw_cache.work,
1855 device_uncache_fw_images_work);
1856
1857 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1858 ret = register_pm_notifier(&fw_cache.pm_notify);
1859 if (ret)
1860 return ret;
1861
1862 register_syscore_ops(&fw_syscore_ops);
1863
1864 return ret;
1865}
1866
Luis R. Rodrigueza67e5032017-11-20 09:45:31 -08001867static inline void unregister_fw_pm_ops(void)
1868{
1869 unregister_syscore_ops(&fw_syscore_ops);
1870 unregister_pm_notifier(&fw_cache.pm_notify);
1871}
Ming Leicfe016b2012-09-08 17:32:30 +08001872#else
1873static int fw_cache_piggyback_on_request(const char *name)
1874{
1875 return 0;
1876}
Luis R. Rodriguez59b6d852017-11-20 09:45:32 -08001877static inline int register_fw_pm_ops(void)
1878{
1879 return 0;
1880}
Luis R. Rodrigueza67e5032017-11-20 09:45:31 -08001881static inline void unregister_fw_pm_ops(void)
1882{
1883}
Ming Leicfe016b2012-09-08 17:32:30 +08001884#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001885
Ming Lei37276a52012-08-04 12:01:27 +08001886static void __init fw_cache_init(void)
1887{
1888 spin_lock_init(&fw_cache.lock);
1889 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001890 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001891}
1892
Luis R. Rodrigueza669f042017-05-02 01:31:04 -07001893static int fw_shutdown_notify(struct notifier_block *unused1,
1894 unsigned long unused2, void *unused3)
1895{
1896 /*
1897 * Kill all pending fallback requests to avoid both stalling shutdown,
1898 * and avoid a deadlock with the usermode_lock.
1899 */
1900 kill_pending_fw_fallback_reqs(false);
1901
1902 return NOTIFY_DONE;
1903}
1904
1905static struct notifier_block fw_shutdown_nb = {
1906 .notifier_call = fw_shutdown_notify,
1907};
1908
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001909static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Luis R. Rodriguez59b6d852017-11-20 09:45:32 -08001911 int ret;
1912
1913 /* No need to unfold these on exit */
Ming Lei1f2b7952012-08-04 12:01:21 +08001914 fw_cache_init();
Luis R. Rodriguez59b6d852017-11-20 09:45:32 -08001915
1916 ret = register_fw_pm_ops();
1917 if (ret)
1918 return ret;
1919
Luis R. Rodriguez561a10b2017-11-20 09:45:34 -08001920 ret = register_reboot_notifier(&fw_shutdown_nb);
1921 if (ret)
1922 goto out;
1923
Luis R. Rodriguez6bb9cf32017-11-20 09:45:33 -08001924 return register_sysfs_loader();
Luis R. Rodriguez561a10b2017-11-20 09:45:34 -08001925
1926out:
1927 unregister_fw_pm_ops();
1928 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001930
1931static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
Luis R. Rodrigueza67e5032017-11-20 09:45:31 -08001933 unregister_fw_pm_ops();
Takashi Iwaife304142013-05-22 18:28:38 +02001934 unregister_reboot_notifier(&fw_shutdown_nb);
Luis R. Rodriguez6bb9cf32017-11-20 09:45:33 -08001935 unregister_sysfs_loader();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
Shaohua Lia30a6a22006-09-27 01:50:52 -07001938fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939module_exit(firmware_class_exit);