blob: 3a79231c2cf5ab81bebc4c9071be0bbb5e5cac24 [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
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080044/* Builtin firmware support */
45
46#ifdef CONFIG_FW_LOADER
47
48extern struct builtin_fw __start_builtin_fw[];
49extern struct builtin_fw __end_builtin_fw[];
50
Stephen Boyda098ecd2016-08-02 14:04:28 -070051static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
52 void *buf, size_t size)
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080053{
54 struct builtin_fw *b_fw;
55
56 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
57 if (strcmp(name, b_fw->name) == 0) {
58 fw->size = b_fw->size;
59 fw->data = b_fw->data;
Stephen Boyda098ecd2016-08-02 14:04:28 -070060
61 if (buf && fw->size <= size)
62 memcpy(buf, fw->data, fw->size);
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080063 return true;
64 }
65 }
66
67 return false;
68}
69
70static bool fw_is_builtin_firmware(const struct firmware *fw)
71{
72 struct builtin_fw *b_fw;
73
74 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
75 if (fw->data == b_fw->data)
76 return true;
77
78 return false;
79}
80
81#else /* Module case - no builtin firmware support */
82
Stephen Boyda098ecd2016-08-02 14:04:28 -070083static inline bool fw_get_builtin_firmware(struct firmware *fw,
84 const char *name, void *buf,
85 size_t size)
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080086{
87 return false;
88}
89
90static inline bool fw_is_builtin_firmware(const struct firmware *fw)
91{
92 return false;
93}
94#endif
95
Daniel Wagnerf52cc3792016-11-17 11:00:48 +010096enum fw_status {
97 FW_STATUS_UNKNOWN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 FW_STATUS_LOADING,
99 FW_STATUS_DONE,
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100100 FW_STATUS_ABORTED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
Dave Jones2f651682007-01-25 15:56:15 -0500103static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200105static inline long firmware_loading_timeout(void)
106{
Ming Lei68ff2a02015-01-13 00:02:01 +0800107 return loading_timeout > 0 ? loading_timeout * HZ : MAX_JIFFY_OFFSET;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200108}
109
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100110/*
111 * Concurrent request_firmware() for the same firmware need to be
112 * serialized. struct fw_state is simple state machine which hold the
113 * state of the firmware loading.
114 */
115struct fw_state {
Luis R. Rodrigueze44565f2017-07-20 13:13:09 -0700116 struct completion completion;
Daniel Wagner0430caf2016-11-17 11:00:49 +0100117 enum fw_status status;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100118};
119
120static void fw_state_init(struct fw_state *fw_st)
121{
Luis R. Rodrigueze44565f2017-07-20 13:13:09 -0700122 init_completion(&fw_st->completion);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100123 fw_st->status = FW_STATUS_UNKNOWN;
124}
125
Daniel Wagner5b029622016-11-17 11:00:50 +0100126static inline bool __fw_state_is_done(enum fw_status status)
127{
128 return status == FW_STATUS_DONE || status == FW_STATUS_ABORTED;
129}
130
Bjorn Andersson5d47ec02016-12-06 17:01:45 -0800131static int __fw_state_wait_common(struct fw_state *fw_st, long timeout)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100132{
133 long ret;
134
Luis R. Rodriguez260d9f22017-07-20 13:13:11 -0700135 ret = wait_for_completion_killable_timeout(&fw_st->completion, timeout);
Daniel Wagner5b029622016-11-17 11:00:50 +0100136 if (ret != 0 && fw_st->status == FW_STATUS_ABORTED)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100137 return -ENOENT;
Bjorn Andersson5d47ec02016-12-06 17:01:45 -0800138 if (!ret)
139 return -ETIMEDOUT;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100140
Bjorn Andersson5d47ec02016-12-06 17:01:45 -0800141 return ret < 0 ? ret : 0;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100142}
143
144static void __fw_state_set(struct fw_state *fw_st,
145 enum fw_status status)
146{
Daniel Wagner0430caf2016-11-17 11:00:49 +0100147 WRITE_ONCE(fw_st->status, status);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100148
Daniel Wagner0430caf2016-11-17 11:00:49 +0100149 if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED)
Luis R. Rodrigueze44565f2017-07-20 13:13:09 -0700150 complete_all(&fw_st->completion);
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100151}
152
153#define fw_state_start(fw_st) \
154 __fw_state_set(fw_st, FW_STATUS_LOADING)
155#define fw_state_done(fw_st) \
156 __fw_state_set(fw_st, FW_STATUS_DONE)
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -0700157#define fw_state_aborted(fw_st) \
158 __fw_state_set(fw_st, FW_STATUS_ABORTED)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100159#define fw_state_wait(fw_st) \
160 __fw_state_wait_common(fw_st, MAX_SCHEDULE_TIMEOUT)
161
Daniel Wagnerfab82cb2016-11-17 11:00:51 +0100162static int __fw_state_check(struct fw_state *fw_st, enum fw_status status)
163{
164 return fw_st->status == status;
165}
166
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -0700167#define fw_state_is_aborted(fw_st) \
168 __fw_state_check(fw_st, FW_STATUS_ABORTED)
169
170#ifdef CONFIG_FW_LOADER_USER_HELPER
171
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100172#define fw_state_aborted(fw_st) \
173 __fw_state_set(fw_st, FW_STATUS_ABORTED)
Daniel Wagnerfab82cb2016-11-17 11:00:51 +0100174#define fw_state_is_done(fw_st) \
175 __fw_state_check(fw_st, FW_STATUS_DONE)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100176#define fw_state_is_loading(fw_st) \
177 __fw_state_check(fw_st, FW_STATUS_LOADING)
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100178#define fw_state_wait_timeout(fw_st, timeout) \
179 __fw_state_wait_common(fw_st, timeout)
180
181#endif /* CONFIG_FW_LOADER_USER_HELPER */
182
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100183/* firmware behavior options */
184#define FW_OPT_UEVENT (1U << 0)
185#define FW_OPT_NOWAIT (1U << 1)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100186#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200187#define FW_OPT_USERHELPER (1U << 2)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100188#else
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200189#define FW_OPT_USERHELPER 0
190#endif
191#ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK
192#define FW_OPT_FALLBACK FW_OPT_USERHELPER
193#else
194#define FW_OPT_FALLBACK 0
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100195#endif
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -0700196#define FW_OPT_NO_WARN (1U << 3)
Vikram Mulukutla0e742e92016-08-02 14:04:25 -0700197#define FW_OPT_NOCACHE (1U << 4)
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100198
Ming Lei1f2b7952012-08-04 12:01:21 +0800199struct firmware_cache {
200 /* firmware_buf instance will be added into the below list */
201 spinlock_t lock;
202 struct list_head head;
Ming Leicfe016b2012-09-08 17:32:30 +0800203 int state;
Ming Lei37276a52012-08-04 12:01:27 +0800204
Ming Leicfe016b2012-09-08 17:32:30 +0800205#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +0800206 /*
207 * Names of firmware images which have been cached successfully
208 * will be added into the below list so that device uncache
209 * helper can trace which firmware images have been cached
210 * before.
211 */
212 spinlock_t name_lock;
213 struct list_head fw_names;
214
Ming Lei37276a52012-08-04 12:01:27 +0800215 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +0800216
217 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +0800218#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800219};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800221struct fw_priv {
Ming Lei1f2b7952012-08-04 12:01:21 +0800222 struct kref ref;
223 struct list_head list;
Ming Lei1f2b7952012-08-04 12:01:21 +0800224 struct firmware_cache *fwc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100225 struct fw_state fw_st;
Ming Lei65710cb2012-08-04 12:01:16 +0800226 void *data;
227 size_t size;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700228 size_t allocated_size;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100229#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100230 bool is_paged_buf;
Ming Leiaf5bc112013-05-27 18:30:04 +0800231 bool need_uevent;
David Woodhouse6e03a202009-04-09 22:04:07 -0700232 struct page **pages;
233 int nr_pages;
234 int page_array_size;
Takashi Iwaife304142013-05-22 18:28:38 +0200235 struct list_head pending_list;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100236#endif
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800237 const char *fw_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238};
239
Ming Lei37276a52012-08-04 12:01:27 +0800240struct fw_cache_entry {
241 struct list_head list;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700242 const char *name;
Ming Lei37276a52012-08-04 12:01:27 +0800243};
244
Ming Leif531f05a2012-08-04 12:01:25 +0800245struct fw_name_devm {
246 unsigned long magic;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700247 const char *name;
Ming Leif531f05a2012-08-04 12:01:25 +0800248};
249
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800250#define to_fw_priv(d) container_of(d, struct fw_priv, ref)
Ming Lei1f2b7952012-08-04 12:01:21 +0800251
Ming Leiac39b3e2012-08-20 19:04:16 +0800252#define FW_LOADER_NO_CACHE 0
253#define FW_LOADER_START_CACHE 1
254
255static int fw_cache_piggyback_on_request(const char *name);
256
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800257/* fw_lock could be moved to 'struct fw_sysfs' but since it is just
Ming Lei1f2b7952012-08-04 12:01:21 +0800258 * guarding for corner cases a global lock should be OK */
259static DEFINE_MUTEX(fw_lock);
260
261static struct firmware_cache fw_cache;
262
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800263static struct fw_priv *__allocate_fw_priv(const char *fw_name,
264 struct firmware_cache *fwc,
265 void *dbuf, size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800266{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800267 struct fw_priv *fw_priv;
Ming Lei1f2b7952012-08-04 12:01:21 +0800268
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800269 fw_priv = kzalloc(sizeof(*fw_priv), GFP_ATOMIC);
270 if (!fw_priv)
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700271 return NULL;
272
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800273 fw_priv->fw_name = kstrdup_const(fw_name, GFP_ATOMIC);
274 if (!fw_priv->fw_name) {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800275 kfree(fw_priv);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700276 return NULL;
277 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800278
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800279 kref_init(&fw_priv->ref);
280 fw_priv->fwc = fwc;
281 fw_priv->data = dbuf;
282 fw_priv->allocated_size = size;
283 fw_state_init(&fw_priv->fw_st);
Takashi Iwaife304142013-05-22 18:28:38 +0200284#ifdef CONFIG_FW_LOADER_USER_HELPER
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800285 INIT_LIST_HEAD(&fw_priv->pending_list);
Takashi Iwaife304142013-05-22 18:28:38 +0200286#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800287
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800288 pr_debug("%s: fw-%s fw_priv=%p\n", __func__, fw_name, fw_priv);
Ming Lei1f2b7952012-08-04 12:01:21 +0800289
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800290 return fw_priv;
Ming Lei1f2b7952012-08-04 12:01:21 +0800291}
292
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800293static struct fw_priv *__lookup_fw_priv(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +0800294{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800295 struct fw_priv *tmp;
Ming Lei2887b392012-08-04 12:01:22 +0800296 struct firmware_cache *fwc = &fw_cache;
297
298 list_for_each_entry(tmp, &fwc->head, list)
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800299 if (!strcmp(tmp->fw_name, fw_name))
Ming Lei2887b392012-08-04 12:01:22 +0800300 return tmp;
301 return NULL;
302}
303
Luis R. Rodriguez30172be2017-07-20 13:13:41 -0700304/* Returns 1 for batching firmware requests with the same name */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800305static int alloc_lookup_fw_priv(const char *fw_name,
306 struct firmware_cache *fwc,
307 struct fw_priv **fw_priv, void *dbuf,
308 size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800309{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800310 struct fw_priv *tmp;
Ming Lei1f2b7952012-08-04 12:01:21 +0800311
312 spin_lock(&fwc->lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800313 tmp = __lookup_fw_priv(fw_name);
Ming Lei2887b392012-08-04 12:01:22 +0800314 if (tmp) {
315 kref_get(&tmp->ref);
316 spin_unlock(&fwc->lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800317 *fw_priv = tmp;
318 pr_debug("batched request - sharing the same struct fw_priv and lookup for multiple requests\n");
Ming Lei2887b392012-08-04 12:01:22 +0800319 return 1;
320 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800321 tmp = __allocate_fw_priv(fw_name, fwc, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +0800322 if (tmp)
323 list_add(&tmp->list, &fwc->head);
324 spin_unlock(&fwc->lock);
325
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800326 *fw_priv = tmp;
Ming Lei1f2b7952012-08-04 12:01:21 +0800327
328 return tmp ? 0 : -ENOMEM;
329}
330
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800331static void __free_fw_priv(struct kref *ref)
Bart Van Assche98233b22014-01-04 14:20:36 +0100332 __releases(&fwc->lock)
Ming Lei1f2b7952012-08-04 12:01:21 +0800333{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800334 struct fw_priv *fw_priv = to_fw_priv(ref);
335 struct firmware_cache *fwc = fw_priv->fwc;
Ming Lei1f2b7952012-08-04 12:01:21 +0800336
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800337 pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800338 __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800339 (unsigned int)fw_priv->size);
Ming Lei1f2b7952012-08-04 12:01:21 +0800340
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800341 list_del(&fw_priv->list);
Ming Lei1f2b7952012-08-04 12:01:21 +0800342 spin_unlock(&fwc->lock);
343
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100344#ifdef CONFIG_FW_LOADER_USER_HELPER
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800345 if (fw_priv->is_paged_buf) {
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100346 int i;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800347 vunmap(fw_priv->data);
348 for (i = 0; i < fw_priv->nr_pages; i++)
349 __free_page(fw_priv->pages[i]);
350 vfree(fw_priv->pages);
Ming Lei746058f2012-10-09 12:01:03 +0800351 } else
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100352#endif
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800353 if (!fw_priv->allocated_size)
354 vfree(fw_priv->data);
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800355 kfree_const(fw_priv->fw_name);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800356 kfree(fw_priv);
Ming Lei1f2b7952012-08-04 12:01:21 +0800357}
358
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800359static void free_fw_priv(struct fw_priv *fw_priv)
Ming Lei1f2b7952012-08-04 12:01:21 +0800360{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800361 struct firmware_cache *fwc = fw_priv->fwc;
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800362 spin_lock(&fwc->lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800363 if (!kref_put(&fw_priv->ref, __free_fw_priv))
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800364 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800365}
366
Ming Lei746058f2012-10-09 12:01:03 +0800367/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800368static char fw_path_para[256];
369static const char * const fw_path[] = {
370 fw_path_para,
Ming Lei746058f2012-10-09 12:01:03 +0800371 "/lib/firmware/updates/" UTS_RELEASE,
372 "/lib/firmware/updates",
373 "/lib/firmware/" UTS_RELEASE,
374 "/lib/firmware"
375};
376
Ming Lei27602842012-11-03 17:47:58 +0800377/*
378 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
379 * from kernel command line because firmware_class is generally built in
380 * kernel instead of module.
381 */
382module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
383MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
384
Stephen Boyda098ecd2016-08-02 14:04:28 -0700385static int
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800386fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv)
Ming Lei746058f2012-10-09 12:01:03 +0800387{
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500388 loff_t size;
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700389 int i, len;
Neil Horman3e358ac2013-09-06 15:36:08 -0400390 int rc = -ENOENT;
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700391 char *path;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700392 enum kernel_read_file_id id = READING_FIRMWARE;
393 size_t msize = INT_MAX;
394
395 /* Already populated data member means we're loading into a buffer */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800396 if (fw_priv->data) {
Stephen Boyda098ecd2016-08-02 14:04:28 -0700397 id = READING_FIRMWARE_PREALLOC_BUFFER;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800398 msize = fw_priv->allocated_size;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700399 }
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700400
401 path = __getname();
402 if (!path)
403 return -ENOMEM;
Ming Lei746058f2012-10-09 12:01:03 +0800404
405 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
Ming Lei27602842012-11-03 17:47:58 +0800406 /* skip the unset customized path */
407 if (!fw_path[i][0])
408 continue;
409
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700410 len = snprintf(path, PATH_MAX, "%s/%s",
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800411 fw_path[i], fw_priv->fw_name);
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700412 if (len >= PATH_MAX) {
413 rc = -ENAMETOOLONG;
414 break;
415 }
Ming Lei746058f2012-10-09 12:01:03 +0800416
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800417 fw_priv->size = 0;
418 rc = kernel_read_file_from_path(path, &fw_priv->data, &size,
419 msize, id);
Kees Cook4b2530d2016-02-04 13:15:02 -0800420 if (rc) {
Luis R. Rodriguez8e516aa2016-02-28 21:57:55 +0100421 if (rc == -ENOENT)
422 dev_dbg(device, "loading %s failed with error %d\n",
423 path, rc);
424 else
425 dev_warn(device, "loading %s failed with error %d\n",
426 path, rc);
Kees Cook4b2530d2016-02-04 13:15:02 -0800427 continue;
428 }
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800429 dev_dbg(device, "direct-loading %s\n", fw_priv->fw_name);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800430 fw_priv->size = size;
431 fw_state_done(&fw_priv->fw_st);
Kees Cook4b2530d2016-02-04 13:15:02 -0800432 break;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100433 }
Kees Cook4b2530d2016-02-04 13:15:02 -0800434 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100435
Neil Horman3e358ac2013-09-06 15:36:08 -0400436 return rc;
Ming Lei746058f2012-10-09 12:01:03 +0800437}
438
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100439/* firmware holds the ownership of pages */
440static void firmware_free_data(const struct firmware *fw)
441{
442 /* Loaded directly? */
443 if (!fw->priv) {
444 vfree(fw->data);
445 return;
446 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800447 free_fw_priv(fw->priv);
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100448}
449
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100450/* store the pages buffer info firmware from buf */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800451static void fw_set_page_data(struct fw_priv *fw_priv, struct firmware *fw)
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100452{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800453 fw->priv = fw_priv;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100454#ifdef CONFIG_FW_LOADER_USER_HELPER
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800455 fw->pages = fw_priv->pages;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100456#endif
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800457 fw->size = fw_priv->size;
458 fw->data = fw_priv->data;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100459
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800460 pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800461 __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800462 (unsigned int)fw_priv->size);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100463}
464
465#ifdef CONFIG_PM_SLEEP
466static void fw_name_devm_release(struct device *dev, void *res)
467{
468 struct fw_name_devm *fwn = res;
469
470 if (fwn->magic == (unsigned long)&fw_cache)
471 pr_debug("%s: fw_name-%s devm-%p released\n",
472 __func__, fwn->name, res);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700473 kfree_const(fwn->name);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100474}
475
476static int fw_devm_match(struct device *dev, void *res,
477 void *match_data)
478{
479 struct fw_name_devm *fwn = res;
480
481 return (fwn->magic == (unsigned long)&fw_cache) &&
482 !strcmp(fwn->name, match_data);
483}
484
485static struct fw_name_devm *fw_find_devm_name(struct device *dev,
486 const char *name)
487{
488 struct fw_name_devm *fwn;
489
490 fwn = devres_find(dev, fw_name_devm_release,
491 fw_devm_match, (void *)name);
492 return fwn;
493}
494
495/* add firmware name into devres list */
496static int fw_add_devm_name(struct device *dev, const char *name)
497{
498 struct fw_name_devm *fwn;
499
500 fwn = fw_find_devm_name(dev, name);
501 if (fwn)
502 return 1;
503
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700504 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
505 GFP_KERNEL);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100506 if (!fwn)
507 return -ENOMEM;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700508 fwn->name = kstrdup_const(name, GFP_KERNEL);
509 if (!fwn->name) {
Vladimir Zapolskiya885de62015-07-29 23:26:28 +0300510 devres_free(fwn);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700511 return -ENOMEM;
512 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100513
514 fwn->magic = (unsigned long)&fw_cache;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100515 devres_add(dev, fwn);
516
517 return 0;
518}
519#else
520static int fw_add_devm_name(struct device *dev, const char *name)
521{
522 return 0;
523}
524#endif
525
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800526static int assign_fw(struct firmware *fw, struct device *device,
527 unsigned int opt_flags)
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700528{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800529 struct fw_priv *fw_priv = fw->priv;
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700530
531 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800532 if (!fw_priv->size || fw_state_is_aborted(&fw_priv->fw_st)) {
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700533 mutex_unlock(&fw_lock);
534 return -ENOENT;
535 }
536
537 /*
538 * add firmware name into devres list so that we can auto cache
539 * and uncache firmware for device.
540 *
541 * device may has been deleted already, but the problem
542 * should be fixed in devres or driver core.
543 */
544 /* don't cache firmware handled without uevent */
545 if (device && (opt_flags & FW_OPT_UEVENT) &&
546 !(opt_flags & FW_OPT_NOCACHE))
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800547 fw_add_devm_name(device, fw_priv->fw_name);
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700548
549 /*
550 * After caching firmware image is started, let it piggyback
551 * on request firmware.
552 */
553 if (!(opt_flags & FW_OPT_NOCACHE) &&
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800554 fw_priv->fwc->state == FW_LOADER_START_CACHE) {
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800555 if (fw_cache_piggyback_on_request(fw_priv->fw_name))
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800556 kref_get(&fw_priv->ref);
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700557 }
558
559 /* pass the pages buffer to driver at the last minute */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800560 fw_set_page_data(fw_priv, fw);
Luis R. Rodriguez8509adc2017-05-02 01:31:06 -0700561 mutex_unlock(&fw_lock);
562 return 0;
563}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100564
565/*
566 * user-mode helper code
567 */
568#ifdef CONFIG_FW_LOADER_USER_HELPER
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800569struct fw_sysfs {
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100570 bool nowait;
571 struct device dev;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800572 struct fw_priv *fw_priv;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100573 struct firmware *fw;
574};
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100575
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800576static struct fw_sysfs *to_fw_sysfs(struct device *dev)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700577{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800578 return container_of(dev, struct fw_sysfs, dev);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700579}
580
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800581static void __fw_load_abort(struct fw_priv *fw_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Ming Lei87597932013-06-15 16:36:38 +0800583 /*
584 * There is a small window in which user can write to 'loading'
585 * between loading done and disappearance of 'loading'
586 */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800587 if (fw_state_is_done(&fw_priv->fw_st))
Ming Lei87597932013-06-15 16:36:38 +0800588 return;
589
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800590 list_del_init(&fw_priv->pending_list);
591 fw_state_aborted(&fw_priv->fw_st);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800594static void fw_load_abort(struct fw_sysfs *fw_sysfs)
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700595{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800596 struct fw_priv *fw_priv = fw_sysfs->fw_priv;
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700597
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800598 __fw_load_abort(fw_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
Takashi Iwaife304142013-05-22 18:28:38 +0200601static LIST_HEAD(pending_fw_head);
602
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -0700603static void kill_pending_fw_fallback_reqs(bool only_kill_custom)
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700604{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800605 struct fw_priv *fw_priv;
606 struct fw_priv *next;
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700607
608 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800609 list_for_each_entry_safe(fw_priv, next, &pending_fw_head,
610 pending_list) {
611 if (!fw_priv->need_uevent || !only_kill_custom)
612 __fw_load_abort(fw_priv);
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700613 }
614 mutex_unlock(&fw_lock);
615}
Luis R. Rodriguez63833312017-05-02 01:31:02 -0700616
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700617static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
618 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 return sprintf(buf, "%d\n", loading_timeout);
621}
622
623/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800624 * firmware_timeout_store - set number of seconds to wait for firmware
625 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800626 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800627 * @buf: buffer to scan for timeout value
628 * @count: number of bytes in @buf
629 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800631 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 * firmware will be provided.
633 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800634 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 **/
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700636static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
637 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700640 if (loading_timeout < 0)
641 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return count;
644}
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100645static CLASS_ATTR_RW(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100647static struct attribute *firmware_class_attrs[] = {
648 &class_attr_timeout.attr,
649 NULL,
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800650};
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100651ATTRIBUTE_GROUPS(firmware_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800653static void fw_dev_release(struct device *dev)
654{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800655 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800656
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800657 kfree(fw_sysfs);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800658}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800660static int do_firmware_uevent(struct fw_sysfs *fw_sysfs, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Luis R. Rodriguez31034f22017-11-20 10:23:49 -0800662 if (add_uevent_var(env, "FIRMWARE=%s", fw_sysfs->fw_priv->fw_name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200664 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700665 return -ENOMEM;
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800666 if (add_uevent_var(env, "ASYNC=%d", fw_sysfs->nowait))
Johannes Berge9045f92010-03-29 17:57:20 +0200667 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 return 0;
670}
671
Linus Torvalds6f957722015-07-09 11:20:01 -0700672static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
673{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800674 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Linus Torvalds6f957722015-07-09 11:20:01 -0700675 int err = 0;
676
677 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800678 if (fw_sysfs->fw_priv)
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800679 err = do_firmware_uevent(fw_sysfs, env);
Linus Torvalds6f957722015-07-09 11:20:01 -0700680 mutex_unlock(&fw_lock);
681 return err;
682}
683
Adrian Bunk1b81d662006-05-20 15:00:16 -0700684static struct class firmware_class = {
685 .name = "firmware",
Greg Kroah-Hartman3f214cf2016-11-28 16:42:30 +0100686 .class_groups = firmware_class_groups,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700687 .dev_uevent = firmware_uevent,
688 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700689};
690
Luis R. Rodriguez6bb9cf32017-11-20 09:45:33 -0800691static inline int register_sysfs_loader(void)
692{
693 return class_register(&firmware_class);
694}
695
696static inline void unregister_sysfs_loader(void)
697{
698 class_unregister(&firmware_class);
699}
700
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700701static ssize_t firmware_loading_show(struct device *dev,
702 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800704 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Ming Lei87597932013-06-15 16:36:38 +0800705 int loading = 0;
706
707 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800708 if (fw_sysfs->fw_priv)
709 loading = fw_state_is_loading(&fw_sysfs->fw_priv->fw_st);
Ming Lei87597932013-06-15 16:36:38 +0800710 mutex_unlock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return sprintf(buf, "%d\n", loading);
713}
714
David Woodhouse6e03a202009-04-09 22:04:07 -0700715/* Some architectures don't have PAGE_KERNEL_RO */
716#ifndef PAGE_KERNEL_RO
717#define PAGE_KERNEL_RO PAGE_KERNEL
718#endif
Ming Lei253c9242012-10-09 12:01:02 +0800719
720/* one pages buffer should be mapped/unmapped only once */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800721static int map_fw_priv_pages(struct fw_priv *fw_priv)
Ming Lei253c9242012-10-09 12:01:02 +0800722{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800723 if (!fw_priv->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800724 return 0;
725
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800726 vunmap(fw_priv->data);
727 fw_priv->data = vmap(fw_priv->pages, fw_priv->nr_pages, 0,
728 PAGE_KERNEL_RO);
729 if (!fw_priv->data)
Ming Lei253c9242012-10-09 12:01:02 +0800730 return -ENOMEM;
731 return 0;
732}
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800735 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700736 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800737 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800738 * @buf: buffer to scan for loading control value
739 * @count: number of bytes in @buf
740 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 * The relevant values are:
742 *
743 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800744 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 * -1: Conclude the load with an error and discard any written data.
746 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700747static ssize_t firmware_loading_store(struct device *dev,
748 struct device_attribute *attr,
749 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800751 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800752 struct fw_priv *fw_priv;
Kees Cook6593d922014-02-25 13:06:00 -0800753 ssize_t written = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700755 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Neil Hormaneea915b2012-01-02 15:31:23 -0500757 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800758 fw_priv = fw_sysfs->fw_priv;
759 if (fw_state_is_aborted(&fw_priv->fw_st))
Neil Hormaneea915b2012-01-02 15:31:23 -0500760 goto out;
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 switch (loading) {
763 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800764 /* discarding any previous partial load */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800765 if (!fw_state_is_done(&fw_priv->fw_st)) {
766 for (i = 0; i < fw_priv->nr_pages; i++)
767 __free_page(fw_priv->pages[i]);
768 vfree(fw_priv->pages);
769 fw_priv->pages = NULL;
770 fw_priv->page_array_size = 0;
771 fw_priv->nr_pages = 0;
772 fw_state_start(&fw_priv->fw_st);
Ming Lei28eefa72012-08-04 12:01:17 +0800773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 break;
775 case 0:
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800776 if (fw_state_is_loading(&fw_priv->fw_st)) {
Kees Cook6593d922014-02-25 13:06:00 -0800777 int rc;
778
Ming Lei253c9242012-10-09 12:01:02 +0800779 /*
780 * Several loading requests may be pending on
781 * one same firmware buf, so let all requests
782 * see the mapped 'buf->data' once the loading
783 * is completed.
784 * */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800785 rc = map_fw_priv_pages(fw_priv);
Kees Cook6593d922014-02-25 13:06:00 -0800786 if (rc)
zhang jun2b1278c2014-02-13 15:18:47 +0800787 dev_err(dev, "%s: map pages failed\n",
788 __func__);
Kees Cook6593d922014-02-25 13:06:00 -0800789 else
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500790 rc = security_kernel_post_read_file(NULL,
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800791 fw_priv->data, fw_priv->size,
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500792 READING_FIRMWARE);
Kees Cook6593d922014-02-25 13:06:00 -0800793
794 /*
795 * Same logic as fw_load_abort, only the DONE bit
796 * is ignored and we set ABORT only on failure.
797 */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800798 list_del_init(&fw_priv->pending_list);
Kees Cook6593d922014-02-25 13:06:00 -0800799 if (rc) {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800800 fw_state_aborted(&fw_priv->fw_st);
Kees Cook6593d922014-02-25 13:06:00 -0800801 written = rc;
Daniel Wagnerf52cc3792016-11-17 11:00:48 +0100802 } else {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800803 fw_state_done(&fw_priv->fw_st);
Kees Cook6593d922014-02-25 13:06:00 -0800804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 break;
806 }
807 /* fallthrough */
808 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700809 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 /* fallthrough */
811 case -1:
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800812 fw_load_abort(fw_sysfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 break;
814 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500815out:
816 mutex_unlock(&fw_lock);
Kees Cook6593d922014-02-25 13:06:00 -0800817 return written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700820static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800822static void firmware_rw_data(struct fw_priv *fw_priv, char *buffer,
Stephen Boyda098ecd2016-08-02 14:04:28 -0700823 loff_t offset, size_t count, bool read)
824{
825 if (read)
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800826 memcpy(buffer, fw_priv->data + offset, count);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700827 else
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800828 memcpy(fw_priv->data + offset, buffer, count);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700829}
830
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800831static void firmware_rw(struct fw_priv *fw_priv, char *buffer,
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700832 loff_t offset, size_t count, bool read)
833{
834 while (count) {
835 void *page_data;
836 int page_nr = offset >> PAGE_SHIFT;
837 int page_ofs = offset & (PAGE_SIZE-1);
838 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
839
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800840 page_data = kmap(fw_priv->pages[page_nr]);
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700841
842 if (read)
843 memcpy(buffer, page_data + page_ofs, page_cnt);
844 else
845 memcpy(page_data + page_ofs, buffer, page_cnt);
846
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800847 kunmap(fw_priv->pages[page_nr]);
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700848 buffer += page_cnt;
849 offset += page_cnt;
850 count -= page_cnt;
851 }
852}
853
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700854static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
855 struct bin_attribute *bin_attr,
856 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200858 struct device *dev = kobj_to_dev(kobj);
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800859 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800860 struct fw_priv *fw_priv;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700861 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Laura Garciacad1e552006-05-23 23:22:38 +0200863 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800864 fw_priv = fw_sysfs->fw_priv;
865 if (!fw_priv || fw_state_is_done(&fw_priv->fw_st)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 ret_count = -ENODEV;
867 goto out;
868 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800869 if (offset > fw_priv->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200870 ret_count = 0;
871 goto out;
872 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800873 if (count > fw_priv->size - offset)
874 count = fw_priv->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700875
876 ret_count = count;
877
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800878 if (fw_priv->data)
879 firmware_rw_data(fw_priv, buffer, offset, count, true);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700880 else
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800881 firmware_rw(fw_priv, buffer, offset, count, true);
David Woodhouse6e03a202009-04-09 22:04:07 -0700882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883out:
Laura Garciacad1e552006-05-23 23:22:38 +0200884 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return ret_count;
886}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800887
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800888static int fw_realloc_pages(struct fw_sysfs *fw_sysfs, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800890 struct fw_priv *fw_priv= fw_sysfs->fw_priv;
Fabian Fredericka76040d2014-07-01 21:13:30 +0200891 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
David Woodhouse6e03a202009-04-09 22:04:07 -0700893 /* If the array of pages is too small, grow it... */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800894 if (fw_priv->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700895 int new_array_size = max(pages_needed,
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800896 fw_priv->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700897 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Chen Feng10a3fbf2015-12-16 17:03:15 +0800899 new_pages = vmalloc(new_array_size * sizeof(void *));
David Woodhouse6e03a202009-04-09 22:04:07 -0700900 if (!new_pages) {
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800901 fw_load_abort(fw_sysfs);
David Woodhouse6e03a202009-04-09 22:04:07 -0700902 return -ENOMEM;
903 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800904 memcpy(new_pages, fw_priv->pages,
905 fw_priv->page_array_size * sizeof(void *));
906 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
907 (new_array_size - fw_priv->page_array_size));
908 vfree(fw_priv->pages);
909 fw_priv->pages = new_pages;
910 fw_priv->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700912
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800913 while (fw_priv->nr_pages < pages_needed) {
914 fw_priv->pages[fw_priv->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700915 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
916
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800917 if (!fw_priv->pages[fw_priv->nr_pages]) {
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800918 fw_load_abort(fw_sysfs);
David Woodhouse6e03a202009-04-09 22:04:07 -0700919 return -ENOMEM;
920 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800921 fw_priv->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return 0;
924}
925
926/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800927 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700928 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700929 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700930 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800931 * @buffer: buffer being written
932 * @offset: buffer offset for write in total data store area
933 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800935 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 * the driver as a firmware image.
937 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700938static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
939 struct bin_attribute *bin_attr,
940 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200942 struct device *dev = kobj_to_dev(kobj);
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -0800943 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800944 struct fw_priv *fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 ssize_t retval;
946
947 if (!capable(CAP_SYS_RAWIO))
948 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700949
Laura Garciacad1e552006-05-23 23:22:38 +0200950 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800951 fw_priv = fw_sysfs->fw_priv;
952 if (!fw_priv || fw_state_is_done(&fw_priv->fw_st)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 retval = -ENODEV;
954 goto out;
955 }
Ming Lei65710cb2012-08-04 12:01:16 +0800956
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800957 if (fw_priv->data) {
958 if (offset + count > fw_priv->allocated_size) {
Stephen Boyda098ecd2016-08-02 14:04:28 -0700959 retval = -ENOMEM;
960 goto out;
961 }
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800962 firmware_rw_data(fw_priv, buffer, offset, count, false);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700963 retval = count;
964 } else {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800965 retval = fw_realloc_pages(fw_sysfs, offset + count);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700966 if (retval)
967 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Stephen Boyda098ecd2016-08-02 14:04:28 -0700969 retval = count;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800970 firmware_rw(fw_priv, buffer, offset, count, false);
Stephen Boyda098ecd2016-08-02 14:04:28 -0700971 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700972
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -0800973 fw_priv->size = max_t(size_t, offset + count, fw_priv->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974out:
Laura Garciacad1e552006-05-23 23:22:38 +0200975 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return retval;
977}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800978
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700979static struct bin_attribute firmware_attr_data = {
980 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 .size = 0,
982 .read = firmware_data_read,
983 .write = firmware_data_write,
984};
985
Takashi Iwai46239902015-02-04 15:18:07 +0100986static struct attribute *fw_dev_attrs[] = {
987 &dev_attr_loading.attr,
988 NULL
989};
990
991static struct bin_attribute *fw_dev_bin_attrs[] = {
992 &firmware_attr_data,
993 NULL
994};
995
996static const struct attribute_group fw_dev_attr_group = {
997 .attrs = fw_dev_attrs,
998 .bin_attrs = fw_dev_bin_attrs,
999};
1000
1001static const struct attribute_group *fw_dev_attr_groups[] = {
1002 &fw_dev_attr_group,
1003 NULL
1004};
1005
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001006static struct fw_sysfs *
Stephen Boyddddb5542012-03-28 23:30:43 +02001007fw_create_instance(struct firmware *firmware, const char *fw_name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001008 struct device *device, unsigned int opt_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001010 struct fw_sysfs *fw_sysfs;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001011 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001013 fw_sysfs = kzalloc(sizeof(*fw_sysfs), GFP_KERNEL);
1014 if (!fw_sysfs) {
1015 fw_sysfs = ERR_PTR(-ENOMEM);
Ming Lei12446912012-08-04 12:01:20 +08001016 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001019 fw_sysfs->nowait = !!(opt_flags & FW_OPT_NOWAIT);
1020 fw_sysfs->fw = firmware;
1021 f_dev = &fw_sysfs->dev;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001022
1023 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +08001024 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -07001025 f_dev->parent = device;
1026 f_dev->class = &firmware_class;
Takashi Iwai46239902015-02-04 15:18:07 +01001027 f_dev->groups = fw_dev_attr_groups;
Ming Lei12446912012-08-04 12:01:20 +08001028exit:
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001029 return fw_sysfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001031
1032/* load a firmware via user helper */
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001033static int _request_firmware_load(struct fw_sysfs *fw_sysfs,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001034 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001035{
1036 int retval = 0;
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001037 struct device *f_dev = &fw_sysfs->dev;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001038 struct fw_priv *fw_priv = fw_sysfs->fw_priv;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001039
1040 /* fall back on userspace loading */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001041 if (!fw_priv->data)
1042 fw_priv->is_paged_buf = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001043
1044 dev_set_uevent_suppress(f_dev, true);
1045
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001046 retval = device_add(f_dev);
1047 if (retval) {
1048 dev_err(f_dev, "%s: device_register failed\n", __func__);
1049 goto err_put_dev;
1050 }
1051
Maxime Bizon1eeeef12013-08-29 20:28:13 +02001052 mutex_lock(&fw_lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001053 list_add(&fw_priv->pending_list, &pending_fw_head);
Maxime Bizon1eeeef12013-08-29 20:28:13 +02001054 mutex_unlock(&fw_lock);
1055
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001056 if (opt_flags & FW_OPT_UEVENT) {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001057 fw_priv->need_uevent = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001058 dev_set_uevent_suppress(f_dev, false);
Luis R. Rodriguez31034f22017-11-20 10:23:49 -08001059 dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_name);
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001060 kobject_uevent(&fw_sysfs->dev.kobj, KOBJ_ADD);
Ming Lei68ff2a02015-01-13 00:02:01 +08001061 } else {
1062 timeout = MAX_JIFFY_OFFSET;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001063 }
1064
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001065 retval = fw_state_wait_timeout(&fw_priv->fw_st, timeout);
Bjorn Andersson5d47ec02016-12-06 17:01:45 -08001066 if (retval < 0) {
Ming Lei0cb64242015-01-13 00:01:35 +08001067 mutex_lock(&fw_lock);
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001068 fw_load_abort(fw_sysfs);
Ming Lei0cb64242015-01-13 00:01:35 +08001069 mutex_unlock(&fw_lock);
1070 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001071
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001072 if (fw_state_is_aborted(&fw_priv->fw_st)) {
Luis R. Rodriguez76098b32017-07-20 13:13:39 -07001073 if (retval == -ERESTARTSYS)
1074 retval = -EINTR;
1075 else
1076 retval = -EAGAIN;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001077 } else if (fw_priv->is_paged_buf && !fw_priv->data)
zhang jun2b1278c2014-02-13 15:18:47 +08001078 retval = -ENOMEM;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001079
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001080 device_del(f_dev);
1081err_put_dev:
1082 put_device(f_dev);
1083 return retval;
1084}
1085
1086static int fw_load_from_user_helper(struct firmware *firmware,
1087 const char *name, struct device *device,
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001088 unsigned int opt_flags)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001089{
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001090 struct fw_sysfs *fw_sysfs;
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001091 long timeout;
1092 int ret;
1093
1094 timeout = firmware_loading_timeout();
1095 if (opt_flags & FW_OPT_NOWAIT) {
1096 timeout = usermodehelper_read_lock_wait(timeout);
1097 if (!timeout) {
1098 dev_dbg(device, "firmware: %s loading timed out\n",
1099 name);
1100 return -EBUSY;
1101 }
1102 } else {
1103 ret = usermodehelper_read_trylock();
1104 if (WARN_ON(ret)) {
1105 dev_err(device, "firmware: %s will not be loaded\n",
1106 name);
1107 return ret;
1108 }
1109 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001110
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001111 fw_sysfs = fw_create_instance(firmware, name, device, opt_flags);
1112 if (IS_ERR(fw_sysfs)) {
1113 ret = PTR_ERR(fw_sysfs);
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001114 goto out_unlock;
1115 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001116
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001117 fw_sysfs->fw_priv = firmware->priv;
Luis R. Rodriguezaa6969c2017-11-20 10:23:47 -08001118 ret = _request_firmware_load(fw_sysfs, opt_flags, timeout);
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001119
1120 if (!ret)
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001121 ret = assign_fw(firmware, device, opt_flags);
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001122
1123out_unlock:
1124 usermodehelper_read_unlock();
1125
1126 return ret;
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001127}
Ming Leiddf1f062013-06-04 10:01:13 +08001128
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001129#else /* CONFIG_FW_LOADER_USER_HELPER */
1130static inline int
1131fw_load_from_user_helper(struct firmware *firmware, const char *name,
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001132 struct device *device, unsigned int opt_flags)
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001133{
1134 return -ENOENT;
1135}
Takashi Iwai807be032013-01-31 11:13:57 +01001136
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -07001137static inline void kill_pending_fw_fallback_reqs(bool only_kill_custom) { }
Ming Leiddf1f062013-06-04 10:01:13 +08001138
Luis R. Rodriguez6bb9cf32017-11-20 09:45:33 -08001139static inline int register_sysfs_loader(void)
1140{
1141 return 0;
1142}
1143
1144static inline void unregister_sysfs_loader(void)
1145{
1146}
1147
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001148#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001150/* prepare firmware and firmware_buf structs;
1151 * return 0 if a firmware is already assigned, 1 if need to load one,
1152 * or a negative error code
1153 */
1154static int
1155_request_firmware_prepare(struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001156 struct device *device, void *dbuf, size_t size)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001157{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 struct firmware *firmware;
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001159 struct fw_priv *fw_priv;
Ming Lei1f2b7952012-08-04 12:01:21 +08001160 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Jiri Slaby4aed0642005-09-13 01:25:01 -07001162 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -07001164 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1165 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001166 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Stephen Boyda098ecd2016-08-02 14:04:28 -07001169 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
Luis R. Rodriguezed046302015-04-29 16:30:43 -07001170 dev_dbg(device, "using built-in %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001171 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +01001172 }
1173
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001174 ret = alloc_lookup_fw_priv(name, &fw_cache, &fw_priv, dbuf, size);
Ming Lei1f2b7952012-08-04 12:01:21 +08001175
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001176 /*
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001177 * bind with 'priv' now to avoid warning in failure path
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001178 * of requesting firmware.
1179 */
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001180 firmware->priv = fw_priv;
Ming Lei1f2b7952012-08-04 12:01:21 +08001181
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001182 if (ret > 0) {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001183 ret = fw_state_wait(&fw_priv->fw_st);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001184 if (!ret) {
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001185 fw_set_page_data(fw_priv, firmware);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001186 return 0; /* assigned */
1187 }
Stephen Boyddddb5542012-03-28 23:30:43 +02001188 }
Ming Lei1f2b7952012-08-04 12:01:21 +08001189
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001190 if (ret < 0)
1191 return ret;
1192 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001193}
1194
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001195/*
1196 * Batched requests need only one wake, we need to do this step last due to the
1197 * fallback mechanism. The buf is protected with kref_get(), and it won't be
1198 * released until the last user calls release_firmware().
1199 *
1200 * Failed batched requests are possible as well, in such cases we just share
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001201 * the struct fw_priv and won't release it until all requests are woken
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001202 * and have gone through this same path.
1203 */
1204static void fw_abort_batch_reqs(struct firmware *fw)
1205{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001206 struct fw_priv *fw_priv;
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001207
1208 /* Loaded directly? */
1209 if (!fw || !fw->priv)
1210 return;
1211
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001212 fw_priv = fw->priv;
1213 if (!fw_state_is_aborted(&fw_priv->fw_st))
1214 fw_state_aborted(&fw_priv->fw_st);
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001215}
1216
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001217/* called from request_firmware() and request_firmware_work_func() */
1218static int
1219_request_firmware(const struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001220 struct device *device, void *buf, size_t size,
1221 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001222{
Brian Norris715780a2015-12-09 14:50:28 -08001223 struct firmware *fw = NULL;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001224 int ret;
1225
1226 if (!firmware_p)
1227 return -EINVAL;
1228
Brian Norris715780a2015-12-09 14:50:28 -08001229 if (!name || name[0] == '\0') {
1230 ret = -EINVAL;
1231 goto out;
1232 }
Kees Cook471b0952014-09-18 11:25:37 -07001233
Stephen Boyda098ecd2016-08-02 14:04:28 -07001234 ret = _request_firmware_prepare(&fw, name, device, buf, size);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001235 if (ret <= 0) /* error or already assigned */
1236 goto out;
1237
Neil Horman3e358ac2013-09-06 15:36:08 -04001238 ret = fw_get_filesystem_firmware(device, fw->priv);
1239 if (ret) {
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001240 if (!(opt_flags & FW_OPT_NO_WARN))
Takashi Iwaibba3a872013-12-02 15:38:16 +01001241 dev_warn(device,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001242 "Direct firmware load for %s failed with error %d\n",
1243 name, ret);
1244 if (opt_flags & FW_OPT_USERHELPER) {
Takashi Iwaibba3a872013-12-02 15:38:16 +01001245 dev_warn(device, "Falling back to user helper\n");
1246 ret = fw_load_from_user_helper(fw, name, device,
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001247 opt_flags);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001248 }
Luis R. Rodriguez06a45a92017-05-02 01:31:07 -07001249 } else
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001250 ret = assign_fw(fw, device, opt_flags);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001251
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001252 out:
1253 if (ret < 0) {
Luis R. Rodriguez90d41e72017-07-20 13:13:10 -07001254 fw_abort_batch_reqs(fw);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001255 release_firmware(fw);
1256 fw = NULL;
1257 }
1258
1259 *firmware_p = fw;
1260 return ret;
1261}
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263/**
Kay Sievers312c0042005-11-16 09:00:00 +01001264 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001265 * @firmware_p: pointer to firmware image
1266 * @name: name of firmware file
1267 * @device: device for which firmware is being loaded
1268 *
1269 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001270 * of @name for device @device.
1271 *
1272 * Should be called from user context where sleeping is allowed.
1273 *
Kay Sievers312c0042005-11-16 09:00:00 +01001274 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001275 * should be distinctive enough not to be confused with any other
1276 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001277 *
1278 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001279 *
1280 * The function can be called safely inside device's suspend and
1281 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001282 **/
1283int
1284request_firmware(const struct firmware **firmware_p, const char *name,
Andrei Opreaea310032015-03-08 12:41:15 +02001285 struct device *device)
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001286{
Ming Leid6c8aa32013-06-06 20:01:48 +08001287 int ret;
1288
1289 /* Need to pin this module until return */
1290 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001291 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001292 FW_OPT_UEVENT | FW_OPT_FALLBACK);
Ming Leid6c8aa32013-06-06 20:01:48 +08001293 module_put(THIS_MODULE);
1294 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001295}
Daniel Mackf4945132013-05-23 22:17:18 +02001296EXPORT_SYMBOL(request_firmware);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001297
Takashi Iwaibba3a872013-12-02 15:38:16 +01001298/**
Borislav Petkov3c1556b2014-12-03 22:46:37 +01001299 * request_firmware_direct: - load firmware directly without usermode helper
Takashi Iwaibba3a872013-12-02 15:38:16 +01001300 * @firmware_p: pointer to firmware image
1301 * @name: name of firmware file
1302 * @device: device for which firmware is being loaded
1303 *
1304 * This function works pretty much like request_firmware(), but this doesn't
1305 * fall back to usermode helper even if the firmware couldn't be loaded
1306 * directly from fs. Hence it's useful for loading optional firmwares, which
1307 * aren't always present, without extra long timeouts of udev.
1308 **/
1309int request_firmware_direct(const struct firmware **firmware_p,
1310 const char *name, struct device *device)
1311{
1312 int ret;
Andrei Opreaea310032015-03-08 12:41:15 +02001313
Takashi Iwaibba3a872013-12-02 15:38:16 +01001314 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001315 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001316 FW_OPT_UEVENT | FW_OPT_NO_WARN);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001317 module_put(THIS_MODULE);
1318 return ret;
1319}
1320EXPORT_SYMBOL_GPL(request_firmware_direct);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001321
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001322/**
Stephen Boyda098ecd2016-08-02 14:04:28 -07001323 * request_firmware_into_buf - load firmware into a previously allocated buffer
1324 * @firmware_p: pointer to firmware image
1325 * @name: name of firmware file
1326 * @device: device for which firmware is being loaded and DMA region allocated
1327 * @buf: address of buffer to load firmware into
1328 * @size: size of buffer
1329 *
1330 * This function works pretty much like request_firmware(), but it doesn't
1331 * allocate a buffer to hold the firmware data. Instead, the firmware
1332 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
1333 * data member is pointed at @buf.
1334 *
1335 * This function doesn't cache firmware either.
1336 */
1337int
1338request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
1339 struct device *device, void *buf, size_t size)
1340{
1341 int ret;
1342
1343 __module_get(THIS_MODULE);
1344 ret = _request_firmware(firmware_p, name, device, buf, size,
1345 FW_OPT_UEVENT | FW_OPT_FALLBACK |
1346 FW_OPT_NOCACHE);
1347 module_put(THIS_MODULE);
1348 return ret;
1349}
1350EXPORT_SYMBOL(request_firmware_into_buf);
1351
1352/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001354 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001356void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
1358 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001359 if (!fw_is_builtin_firmware(fw))
1360 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 kfree(fw);
1362 }
1363}
Daniel Mackf4945132013-05-23 22:17:18 +02001364EXPORT_SYMBOL(release_firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366/* Async support */
1367struct firmware_work {
1368 struct work_struct work;
1369 struct module *module;
1370 const char *name;
1371 struct device *device;
1372 void *context;
1373 void (*cont)(const struct firmware *fw, void *context);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001374 unsigned int opt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375};
1376
Stephen Boyda36cf842012-03-28 23:31:00 +02001377static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Stephen Boyda36cf842012-03-28 23:31:00 +02001379 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001381
Stephen Boyda36cf842012-03-28 23:31:00 +02001382 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001383
Stephen Boyda098ecd2016-08-02 14:04:28 -07001384 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001385 fw_work->opt_flags);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001386 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001387 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 module_put(fw_work->module);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001390 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392}
1393
1394/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001395 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001396 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001397 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001398 * is non-zero else the firmware copy must be done manually.
1399 * @name: name of firmware file
1400 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001401 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001402 * @context: will be passed over to @cont, and
1403 * @fw may be %NULL if firmware request fails.
1404 * @cont: function will be called asynchronously when the firmware
1405 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001407 * Caller must hold the reference count of @device.
1408 *
Ming Lei6f21a622012-08-04 12:01:24 +08001409 * Asynchronous variant of request_firmware() for user contexts:
1410 * - sleep for as small periods as possible since it may
Silvio Fricke88bcef52016-11-25 15:59:47 +01001411 * increase kernel boot time of built-in device drivers
1412 * requesting firmware in their ->probe() methods, if
1413 * @gfp is GFP_KERNEL.
Ming Lei6f21a622012-08-04 12:01:24 +08001414 *
1415 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 **/
1417int
1418request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001419 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001420 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 void (*cont)(const struct firmware *fw, void *context))
1422{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001423 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Andrei Opreaea310032015-03-08 12:41:15 +02001425 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (!fw_work)
1427 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001428
1429 fw_work->module = module;
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001430 fw_work->name = kstrdup_const(name, gfp);
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001431 if (!fw_work->name) {
1432 kfree(fw_work);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001433 return -ENOMEM;
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001434 }
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001435 fw_work->device = device;
1436 fw_work->context = context;
1437 fw_work->cont = cont;
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001438 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
Takashi Iwai5a1379e2014-06-04 17:48:15 +02001439 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (!try_module_get(module)) {
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001442 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 kfree(fw_work);
1444 return -EFAULT;
1445 }
1446
Ming Lei0cfc1e12012-08-04 12:01:23 +08001447 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001448 INIT_WORK(&fw_work->work, request_firmware_work_func);
1449 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return 0;
1451}
Daniel Mackf4945132013-05-23 22:17:18 +02001452EXPORT_SYMBOL(request_firmware_nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Ming Lei90f890812013-06-20 12:30:16 +08001454#ifdef CONFIG_PM_SLEEP
1455static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1456
Ming Lei2887b392012-08-04 12:01:22 +08001457/**
1458 * cache_firmware - cache one firmware image in kernel memory space
1459 * @fw_name: the firmware image name
1460 *
1461 * Cache firmware in kernel memory so that drivers can use it when
1462 * system isn't ready for them to request firmware image from userspace.
1463 * Once it returns successfully, driver can use request_firmware or its
1464 * nowait version to get the cached firmware without any interacting
1465 * with userspace
1466 *
1467 * Return 0 if the firmware image has been cached successfully
1468 * Return !0 otherwise
1469 *
1470 */
Ming Lei93232e42013-06-06 20:01:47 +08001471static int cache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001472{
1473 int ret;
1474 const struct firmware *fw;
1475
1476 pr_debug("%s: %s\n", __func__, fw_name);
1477
1478 ret = request_firmware(&fw, fw_name, NULL);
1479 if (!ret)
1480 kfree(fw);
1481
1482 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1483
1484 return ret;
1485}
1486
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001487static struct fw_priv *lookup_fw_priv(const char *fw_name)
Ming Lei6a2c1232013-06-26 09:28:17 +08001488{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001489 struct fw_priv *tmp;
Ming Lei6a2c1232013-06-26 09:28:17 +08001490 struct firmware_cache *fwc = &fw_cache;
1491
1492 spin_lock(&fwc->lock);
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001493 tmp = __lookup_fw_priv(fw_name);
Ming Lei6a2c1232013-06-26 09:28:17 +08001494 spin_unlock(&fwc->lock);
1495
1496 return tmp;
1497}
1498
Ming Lei2887b392012-08-04 12:01:22 +08001499/**
1500 * uncache_firmware - remove one cached firmware image
1501 * @fw_name: the firmware image name
1502 *
1503 * Uncache one firmware image which has been cached successfully
1504 * before.
1505 *
1506 * Return 0 if the firmware cache has been removed successfully
1507 * Return !0 otherwise
1508 *
1509 */
Ming Lei93232e42013-06-06 20:01:47 +08001510static int uncache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001511{
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001512 struct fw_priv *fw_priv;
Ming Lei2887b392012-08-04 12:01:22 +08001513 struct firmware fw;
1514
1515 pr_debug("%s: %s\n", __func__, fw_name);
1516
Stephen Boyda098ecd2016-08-02 14:04:28 -07001517 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
Ming Lei2887b392012-08-04 12:01:22 +08001518 return 0;
1519
Luis R. Rodriguezcd5322b2017-11-20 10:23:48 -08001520 fw_priv = lookup_fw_priv(fw_name);
1521 if (fw_priv) {
1522 free_fw_priv(fw_priv);
Ming Lei2887b392012-08-04 12:01:22 +08001523 return 0;
1524 }
1525
1526 return -EINVAL;
1527}
1528
Ming Lei37276a52012-08-04 12:01:27 +08001529static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1530{
1531 struct fw_cache_entry *fce;
1532
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001533 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
Ming Lei37276a52012-08-04 12:01:27 +08001534 if (!fce)
1535 goto exit;
1536
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001537 fce->name = kstrdup_const(name, GFP_ATOMIC);
1538 if (!fce->name) {
1539 kfree(fce);
1540 fce = NULL;
1541 goto exit;
1542 }
Ming Lei37276a52012-08-04 12:01:27 +08001543exit:
1544 return fce;
1545}
1546
Ming Lei373304f2012-10-09 12:01:01 +08001547static int __fw_entry_found(const char *name)
1548{
1549 struct firmware_cache *fwc = &fw_cache;
1550 struct fw_cache_entry *fce;
1551
1552 list_for_each_entry(fce, &fwc->fw_names, list) {
1553 if (!strcmp(fce->name, name))
1554 return 1;
1555 }
1556 return 0;
1557}
1558
Ming Leiac39b3e2012-08-20 19:04:16 +08001559static int fw_cache_piggyback_on_request(const char *name)
1560{
1561 struct firmware_cache *fwc = &fw_cache;
1562 struct fw_cache_entry *fce;
1563 int ret = 0;
1564
1565 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001566 if (__fw_entry_found(name))
1567 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001568
1569 fce = alloc_fw_cache_entry(name);
1570 if (fce) {
1571 ret = 1;
1572 list_add(&fce->list, &fwc->fw_names);
1573 pr_debug("%s: fw: %s\n", __func__, name);
1574 }
1575found:
1576 spin_unlock(&fwc->name_lock);
1577 return ret;
1578}
1579
Ming Lei37276a52012-08-04 12:01:27 +08001580static void free_fw_cache_entry(struct fw_cache_entry *fce)
1581{
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001582 kfree_const(fce->name);
Ming Lei37276a52012-08-04 12:01:27 +08001583 kfree(fce);
1584}
1585
1586static void __async_dev_cache_fw_image(void *fw_entry,
1587 async_cookie_t cookie)
1588{
1589 struct fw_cache_entry *fce = fw_entry;
1590 struct firmware_cache *fwc = &fw_cache;
1591 int ret;
1592
1593 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001594 if (ret) {
1595 spin_lock(&fwc->name_lock);
1596 list_del(&fce->list);
1597 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001598
Ming Leiac39b3e2012-08-20 19:04:16 +08001599 free_fw_cache_entry(fce);
1600 }
Ming Lei37276a52012-08-04 12:01:27 +08001601}
1602
1603/* called with dev->devres_lock held */
1604static void dev_create_fw_entry(struct device *dev, void *res,
1605 void *data)
1606{
1607 struct fw_name_devm *fwn = res;
1608 const char *fw_name = fwn->name;
1609 struct list_head *head = data;
1610 struct fw_cache_entry *fce;
1611
1612 fce = alloc_fw_cache_entry(fw_name);
1613 if (fce)
1614 list_add(&fce->list, head);
1615}
1616
1617static int devm_name_match(struct device *dev, void *res,
1618 void *match_data)
1619{
1620 struct fw_name_devm *fwn = res;
1621 return (fwn->magic == (unsigned long)match_data);
1622}
1623
Ming Leiab6dd8e2012-08-17 22:07:00 +08001624static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001625{
1626 LIST_HEAD(todo);
1627 struct fw_cache_entry *fce;
1628 struct fw_cache_entry *fce_next;
1629 struct firmware_cache *fwc = &fw_cache;
1630
1631 devres_for_each_res(dev, fw_name_devm_release,
1632 devm_name_match, &fw_cache,
1633 dev_create_fw_entry, &todo);
1634
1635 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1636 list_del(&fce->list);
1637
1638 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001639 /* only one cache entry for one firmware */
1640 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001641 list_add(&fce->list, &fwc->fw_names);
1642 } else {
1643 free_fw_cache_entry(fce);
1644 fce = NULL;
1645 }
Ming Lei37276a52012-08-04 12:01:27 +08001646 spin_unlock(&fwc->name_lock);
1647
Ming Lei373304f2012-10-09 12:01:01 +08001648 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001649 async_schedule_domain(__async_dev_cache_fw_image,
1650 (void *)fce,
1651 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001652 }
1653}
1654
1655static void __device_uncache_fw_images(void)
1656{
1657 struct firmware_cache *fwc = &fw_cache;
1658 struct fw_cache_entry *fce;
1659
1660 spin_lock(&fwc->name_lock);
1661 while (!list_empty(&fwc->fw_names)) {
1662 fce = list_entry(fwc->fw_names.next,
1663 struct fw_cache_entry, list);
1664 list_del(&fce->list);
1665 spin_unlock(&fwc->name_lock);
1666
1667 uncache_firmware(fce->name);
1668 free_fw_cache_entry(fce);
1669
1670 spin_lock(&fwc->name_lock);
1671 }
1672 spin_unlock(&fwc->name_lock);
1673}
1674
1675/**
1676 * device_cache_fw_images - cache devices' firmware
1677 *
1678 * If one device called request_firmware or its nowait version
1679 * successfully before, the firmware names are recored into the
1680 * device's devres link list, so device_cache_fw_images can call
1681 * cache_firmware() to cache these firmwares for the device,
1682 * then the device driver can load its firmwares easily at
1683 * time when system is not ready to complete loading firmware.
1684 */
1685static void device_cache_fw_images(void)
1686{
1687 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001688 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001689 DEFINE_WAIT(wait);
1690
1691 pr_debug("%s\n", __func__);
1692
Ming Lei373304f2012-10-09 12:01:01 +08001693 /* cancel uncache work */
1694 cancel_delayed_work_sync(&fwc->work);
1695
Ming Leiffe53f62012-08-04 12:01:28 +08001696 /*
1697 * use small loading timeout for caching devices' firmware
1698 * because all these firmware images have been loaded
1699 * successfully at lease once, also system is ready for
1700 * completing firmware loading now. The maximum size of
1701 * firmware in current distributions is about 2M bytes,
1702 * so 10 secs should be enough.
1703 */
1704 old_timeout = loading_timeout;
1705 loading_timeout = 10;
1706
Ming Leiac39b3e2012-08-20 19:04:16 +08001707 mutex_lock(&fw_lock);
1708 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001709 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001710 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001711
1712 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001713 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001714
1715 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001716}
1717
1718/**
1719 * device_uncache_fw_images - uncache devices' firmware
1720 *
1721 * uncache all firmwares which have been cached successfully
1722 * by device_uncache_fw_images earlier
1723 */
1724static void device_uncache_fw_images(void)
1725{
1726 pr_debug("%s\n", __func__);
1727 __device_uncache_fw_images();
1728}
1729
1730static void device_uncache_fw_images_work(struct work_struct *work)
1731{
1732 device_uncache_fw_images();
1733}
1734
1735/**
1736 * device_uncache_fw_images_delay - uncache devices firmwares
1737 * @delay: number of milliseconds to delay uncache device firmwares
1738 *
1739 * uncache all devices's firmwares which has been cached successfully
1740 * by device_cache_fw_images after @delay milliseconds.
1741 */
1742static void device_uncache_fw_images_delay(unsigned long delay)
1743{
Shaibal Duttabce66182014-01-31 15:44:58 -08001744 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1745 msecs_to_jiffies(delay));
Ming Lei37276a52012-08-04 12:01:27 +08001746}
1747
Ming Lei07646d92012-08-04 12:01:29 +08001748static int fw_pm_notify(struct notifier_block *notify_block,
1749 unsigned long mode, void *unused)
1750{
1751 switch (mode) {
1752 case PM_HIBERNATION_PREPARE:
1753 case PM_SUSPEND_PREPARE:
Sebastian Capellaf8d5b9e2014-02-18 17:52:08 -08001754 case PM_RESTORE_PREPARE:
Luis R. Rodriguezc4b76892017-05-02 01:31:03 -07001755 /*
1756 * kill pending fallback requests with a custom fallback
1757 * to avoid stalling suspend.
1758 */
1759 kill_pending_fw_fallback_reqs(true);
Ming Lei07646d92012-08-04 12:01:29 +08001760 device_cache_fw_images();
1761 break;
1762
1763 case PM_POST_SUSPEND:
1764 case PM_POST_HIBERNATION:
1765 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001766 /*
1767 * In case that system sleep failed and syscore_suspend is
1768 * not called.
1769 */
1770 mutex_lock(&fw_lock);
1771 fw_cache.state = FW_LOADER_NO_CACHE;
1772 mutex_unlock(&fw_lock);
1773
Ming Lei07646d92012-08-04 12:01:29 +08001774 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1775 break;
1776 }
1777
1778 return 0;
1779}
Ming Lei07646d92012-08-04 12:01:29 +08001780
Ming Leiac39b3e2012-08-20 19:04:16 +08001781/* stop caching firmware once syscore_suspend is reached */
1782static int fw_suspend(void)
1783{
1784 fw_cache.state = FW_LOADER_NO_CACHE;
1785 return 0;
1786}
1787
1788static struct syscore_ops fw_syscore_ops = {
1789 .suspend = fw_suspend,
1790};
Luis R. Rodrigueza67e5032017-11-20 09:45:31 -08001791
Luis R. Rodriguez59b6d852017-11-20 09:45:32 -08001792static int __init register_fw_pm_ops(void)
1793{
1794 int ret;
1795
1796 spin_lock_init(&fw_cache.name_lock);
1797 INIT_LIST_HEAD(&fw_cache.fw_names);
1798
1799 INIT_DELAYED_WORK(&fw_cache.work,
1800 device_uncache_fw_images_work);
1801
1802 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1803 ret = register_pm_notifier(&fw_cache.pm_notify);
1804 if (ret)
1805 return ret;
1806
1807 register_syscore_ops(&fw_syscore_ops);
1808
1809 return ret;
1810}
1811
Luis R. Rodrigueza67e5032017-11-20 09:45:31 -08001812static inline void unregister_fw_pm_ops(void)
1813{
1814 unregister_syscore_ops(&fw_syscore_ops);
1815 unregister_pm_notifier(&fw_cache.pm_notify);
1816}
Ming Leicfe016b2012-09-08 17:32:30 +08001817#else
1818static int fw_cache_piggyback_on_request(const char *name)
1819{
1820 return 0;
1821}
Luis R. Rodriguez59b6d852017-11-20 09:45:32 -08001822static inline int register_fw_pm_ops(void)
1823{
1824 return 0;
1825}
Luis R. Rodrigueza67e5032017-11-20 09:45:31 -08001826static inline void unregister_fw_pm_ops(void)
1827{
1828}
Ming Leicfe016b2012-09-08 17:32:30 +08001829#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001830
Ming Lei37276a52012-08-04 12:01:27 +08001831static void __init fw_cache_init(void)
1832{
1833 spin_lock_init(&fw_cache.lock);
1834 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001835 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001836}
1837
Luis R. Rodrigueza669f042017-05-02 01:31:04 -07001838static int fw_shutdown_notify(struct notifier_block *unused1,
1839 unsigned long unused2, void *unused3)
1840{
1841 /*
1842 * Kill all pending fallback requests to avoid both stalling shutdown,
1843 * and avoid a deadlock with the usermode_lock.
1844 */
1845 kill_pending_fw_fallback_reqs(false);
1846
1847 return NOTIFY_DONE;
1848}
1849
1850static struct notifier_block fw_shutdown_nb = {
1851 .notifier_call = fw_shutdown_notify,
1852};
1853
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001854static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
Luis R. Rodriguez59b6d852017-11-20 09:45:32 -08001856 int ret;
1857
1858 /* No need to unfold these on exit */
Ming Lei1f2b7952012-08-04 12:01:21 +08001859 fw_cache_init();
Luis R. Rodriguez59b6d852017-11-20 09:45:32 -08001860
1861 ret = register_fw_pm_ops();
1862 if (ret)
1863 return ret;
1864
Luis R. Rodriguez561a10b2017-11-20 09:45:34 -08001865 ret = register_reboot_notifier(&fw_shutdown_nb);
1866 if (ret)
1867 goto out;
1868
Luis R. Rodriguez6bb9cf32017-11-20 09:45:33 -08001869 return register_sysfs_loader();
Luis R. Rodriguez561a10b2017-11-20 09:45:34 -08001870
1871out:
1872 unregister_fw_pm_ops();
1873 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001875
1876static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Luis R. Rodrigueza67e5032017-11-20 09:45:31 -08001878 unregister_fw_pm_ops();
Takashi Iwaife304142013-05-22 18:28:38 +02001879 unregister_reboot_notifier(&fw_shutdown_nb);
Luis R. Rodriguez6bb9cf32017-11-20 09:45:33 -08001880 unregister_sysfs_loader();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
Shaohua Lia30a6a22006-09-27 01:50:52 -07001883fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884module_exit(firmware_class_exit);