blob: 813a191febd87928dce7df9b6c686d903bf55bb5 [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
Randy.Dunlapc59ede72006-01-11 12:17:46 -080010#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/device.h>
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/timer.h>
15#include <linux/vmalloc.h>
16#include <linux/interrupt.h>
17#include <linux/bitops.h>
Laura Garciacad1e552006-05-23 23:22:38 +020018#include <linux/mutex.h>
Stephen Boyda36cf842012-03-28 23:31:00 +020019#include <linux/workqueue.h>
David Woodhouse6e03a202009-04-09 22:04:07 -070020#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/firmware.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Stephen Boyda36cf842012-03-28 23:31:00 +020023#include <linux/sched.h>
Linus Torvaldsabb139e2012-10-03 15:58:32 -070024#include <linux/file.h>
Ming Lei1f2b7952012-08-04 12:01:21 +080025#include <linux/list.h>
Mimi Zohare40ba6d2015-11-19 12:39:22 -050026#include <linux/fs.h>
Ming Lei37276a52012-08-04 12:01:27 +080027#include <linux/async.h>
28#include <linux/pm.h>
Ming Lei07646d92012-08-04 12:01:29 +080029#include <linux/suspend.h>
Ming Leiac39b3e2012-08-20 19:04:16 +080030#include <linux/syscore_ops.h>
Takashi Iwaife304142013-05-22 18:28:38 +020031#include <linux/reboot.h>
Kees Cook6593d922014-02-25 13:06:00 -080032#include <linux/security.h>
Ming Lei37276a52012-08-04 12:01:27 +080033
Linus Torvaldsabb139e2012-10-03 15:58:32 -070034#include <generated/utsrelease.h>
35
Ming Lei37276a52012-08-04 12:01:27 +080036#include "base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Markus Rechberger87d37a42007-06-04 18:45:44 +020038MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070039MODULE_DESCRIPTION("Multi purpose firmware loading support");
40MODULE_LICENSE("GPL");
41
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080042/* Builtin firmware support */
43
44#ifdef CONFIG_FW_LOADER
45
46extern struct builtin_fw __start_builtin_fw[];
47extern struct builtin_fw __end_builtin_fw[];
48
Stephen Boyda098ecd2016-08-02 14:04:28 -070049static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
50 void *buf, size_t size)
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080051{
52 struct builtin_fw *b_fw;
53
54 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
55 if (strcmp(name, b_fw->name) == 0) {
56 fw->size = b_fw->size;
57 fw->data = b_fw->data;
Stephen Boyda098ecd2016-08-02 14:04:28 -070058
59 if (buf && fw->size <= size)
60 memcpy(buf, fw->data, fw->size);
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080061 return true;
62 }
63 }
64
65 return false;
66}
67
68static bool fw_is_builtin_firmware(const struct firmware *fw)
69{
70 struct builtin_fw *b_fw;
71
72 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
73 if (fw->data == b_fw->data)
74 return true;
75
76 return false;
77}
78
79#else /* Module case - no builtin firmware support */
80
Stephen Boyda098ecd2016-08-02 14:04:28 -070081static inline bool fw_get_builtin_firmware(struct firmware *fw,
82 const char *name, void *buf,
83 size_t size)
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -080084{
85 return false;
86}
87
88static inline bool fw_is_builtin_firmware(const struct firmware *fw)
89{
90 return false;
91}
92#endif
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094enum {
95 FW_STATUS_LOADING,
96 FW_STATUS_DONE,
97 FW_STATUS_ABORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
Dave Jones2f651682007-01-25 15:56:15 -0500100static int loading_timeout = 60; /* In seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200102static inline long firmware_loading_timeout(void)
103{
Ming Lei68ff2a02015-01-13 00:02:01 +0800104 return loading_timeout > 0 ? loading_timeout * HZ : MAX_JIFFY_OFFSET;
Rafael J. Wysocki9b78c1d2012-03-28 23:30:02 +0200105}
106
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100107/* firmware behavior options */
108#define FW_OPT_UEVENT (1U << 0)
109#define FW_OPT_NOWAIT (1U << 1)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100110#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200111#define FW_OPT_USERHELPER (1U << 2)
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100112#else
Takashi Iwai5a1379e2014-06-04 17:48:15 +0200113#define FW_OPT_USERHELPER 0
114#endif
115#ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK
116#define FW_OPT_FALLBACK FW_OPT_USERHELPER
117#else
118#define FW_OPT_FALLBACK 0
Takashi Iwai68aeeaa2013-12-02 15:38:19 +0100119#endif
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -0700120#define FW_OPT_NO_WARN (1U << 3)
Vikram Mulukutla0e742e92016-08-02 14:04:25 -0700121#define FW_OPT_NOCACHE (1U << 4)
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100122
Ming Lei1f2b7952012-08-04 12:01:21 +0800123struct firmware_cache {
124 /* firmware_buf instance will be added into the below list */
125 spinlock_t lock;
126 struct list_head head;
Ming Leicfe016b2012-09-08 17:32:30 +0800127 int state;
Ming Lei37276a52012-08-04 12:01:27 +0800128
Ming Leicfe016b2012-09-08 17:32:30 +0800129#ifdef CONFIG_PM_SLEEP
Ming Lei37276a52012-08-04 12:01:27 +0800130 /*
131 * Names of firmware images which have been cached successfully
132 * will be added into the below list so that device uncache
133 * helper can trace which firmware images have been cached
134 * before.
135 */
136 spinlock_t name_lock;
137 struct list_head fw_names;
138
Ming Lei37276a52012-08-04 12:01:27 +0800139 struct delayed_work work;
Ming Lei07646d92012-08-04 12:01:29 +0800140
141 struct notifier_block pm_notify;
Ming Leicfe016b2012-09-08 17:32:30 +0800142#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800143};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Ming Lei12446912012-08-04 12:01:20 +0800145struct firmware_buf {
Ming Lei1f2b7952012-08-04 12:01:21 +0800146 struct kref ref;
147 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 struct completion completion;
Ming Lei1f2b7952012-08-04 12:01:21 +0800149 struct firmware_cache *fwc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 unsigned long status;
Ming Lei65710cb2012-08-04 12:01:16 +0800151 void *data;
152 size_t size;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700153 size_t allocated_size;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100154#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100155 bool is_paged_buf;
Ming Leiaf5bc112013-05-27 18:30:04 +0800156 bool need_uevent;
David Woodhouse6e03a202009-04-09 22:04:07 -0700157 struct page **pages;
158 int nr_pages;
159 int page_array_size;
Takashi Iwaife304142013-05-22 18:28:38 +0200160 struct list_head pending_list;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100161#endif
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700162 const char *fw_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163};
164
Ming Lei37276a52012-08-04 12:01:27 +0800165struct fw_cache_entry {
166 struct list_head list;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700167 const char *name;
Ming Lei37276a52012-08-04 12:01:27 +0800168};
169
Ming Leif531f05a2012-08-04 12:01:25 +0800170struct fw_name_devm {
171 unsigned long magic;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700172 const char *name;
Ming Leif531f05a2012-08-04 12:01:25 +0800173};
174
Ming Lei1f2b7952012-08-04 12:01:21 +0800175#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
176
Ming Leiac39b3e2012-08-20 19:04:16 +0800177#define FW_LOADER_NO_CACHE 0
178#define FW_LOADER_START_CACHE 1
179
180static int fw_cache_piggyback_on_request(const char *name);
181
Ming Lei1f2b7952012-08-04 12:01:21 +0800182/* fw_lock could be moved to 'struct firmware_priv' but since it is just
183 * guarding for corner cases a global lock should be OK */
184static DEFINE_MUTEX(fw_lock);
185
186static struct firmware_cache fw_cache;
187
188static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
Stephen Boyda098ecd2016-08-02 14:04:28 -0700189 struct firmware_cache *fwc,
190 void *dbuf, size_t size)
Ming Lei1f2b7952012-08-04 12:01:21 +0800191{
192 struct firmware_buf *buf;
193
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700194 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
Ming Lei1f2b7952012-08-04 12:01:21 +0800195 if (!buf)
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700196 return NULL;
197
198 buf->fw_id = kstrdup_const(fw_name, GFP_ATOMIC);
199 if (!buf->fw_id) {
200 kfree(buf);
201 return NULL;
202 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800203
204 kref_init(&buf->ref);
Ming Lei1f2b7952012-08-04 12:01:21 +0800205 buf->fwc = fwc;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700206 buf->data = dbuf;
207 buf->allocated_size = size;
Ming Lei1f2b7952012-08-04 12:01:21 +0800208 init_completion(&buf->completion);
Kyle Yanc5336ef2017-05-24 17:18:15 -0700209 INIT_LIST_HEAD(&buf->list);
Takashi Iwaife304142013-05-22 18:28:38 +0200210#ifdef CONFIG_FW_LOADER_USER_HELPER
211 INIT_LIST_HEAD(&buf->pending_list);
212#endif
Ming Lei1f2b7952012-08-04 12:01:21 +0800213
214 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
215
216 return buf;
217}
218
Ming Lei2887b392012-08-04 12:01:22 +0800219static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
220{
221 struct firmware_buf *tmp;
222 struct firmware_cache *fwc = &fw_cache;
223
224 list_for_each_entry(tmp, &fwc->head, list)
225 if (!strcmp(tmp->fw_id, fw_name))
226 return tmp;
227 return NULL;
228}
229
Ming Lei1f2b7952012-08-04 12:01:21 +0800230static int fw_lookup_and_allocate_buf(const char *fw_name,
231 struct firmware_cache *fwc,
Stephen Boyda098ecd2016-08-02 14:04:28 -0700232 struct firmware_buf **buf, void *dbuf,
Vikram Mulukutlab19be882017-04-24 19:31:14 -0700233 size_t size, unsigned int opt_flags)
Ming Lei1f2b7952012-08-04 12:01:21 +0800234{
235 struct firmware_buf *tmp;
236
237 spin_lock(&fwc->lock);
Vikram Mulukutlab19be882017-04-24 19:31:14 -0700238 if (!(opt_flags & FW_OPT_NOCACHE)) {
239 tmp = __fw_lookup_buf(fw_name);
240 if (tmp) {
241 kref_get(&tmp->ref);
242 spin_unlock(&fwc->lock);
243 *buf = tmp;
244 return 1;
245 }
Ming Lei2887b392012-08-04 12:01:22 +0800246 }
Stephen Boyda098ecd2016-08-02 14:04:28 -0700247 tmp = __allocate_fw_buf(fw_name, fwc, dbuf, size);
Vikram Mulukutlab19be882017-04-24 19:31:14 -0700248 if (tmp && !(opt_flags & FW_OPT_NOCACHE))
Ming Lei1f2b7952012-08-04 12:01:21 +0800249 list_add(&tmp->list, &fwc->head);
250 spin_unlock(&fwc->lock);
251
252 *buf = tmp;
253
254 return tmp ? 0 : -ENOMEM;
255}
256
257static void __fw_free_buf(struct kref *ref)
Bart Van Assche98233b22014-01-04 14:20:36 +0100258 __releases(&fwc->lock)
Ming Lei1f2b7952012-08-04 12:01:21 +0800259{
260 struct firmware_buf *buf = to_fwbuf(ref);
261 struct firmware_cache *fwc = buf->fwc;
Ming Lei1f2b7952012-08-04 12:01:21 +0800262
263 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
264 __func__, buf->fw_id, buf, buf->data,
265 (unsigned int)buf->size);
266
Ming Lei1f2b7952012-08-04 12:01:21 +0800267 list_del(&buf->list);
268 spin_unlock(&fwc->lock);
269
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100270#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100271 if (buf->is_paged_buf) {
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100272 int i;
Ming Lei746058f2012-10-09 12:01:03 +0800273 vunmap(buf->data);
274 for (i = 0; i < buf->nr_pages; i++)
275 __free_page(buf->pages[i]);
Chen Feng10a3fbf2015-12-16 17:03:15 +0800276 vfree(buf->pages);
Ming Lei746058f2012-10-09 12:01:03 +0800277 } else
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100278#endif
Stephen Boyda098ecd2016-08-02 14:04:28 -0700279 if (!buf->allocated_size)
Ming Lei746058f2012-10-09 12:01:03 +0800280 vfree(buf->data);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700281 kfree_const(buf->fw_id);
Ming Lei1f2b7952012-08-04 12:01:21 +0800282 kfree(buf);
283}
284
285static void fw_free_buf(struct firmware_buf *buf)
286{
Chuansheng Liubd9eb7f2012-11-10 01:27:22 +0800287 struct firmware_cache *fwc = buf->fwc;
288 spin_lock(&fwc->lock);
289 if (!kref_put(&buf->ref, __fw_free_buf))
290 spin_unlock(&fwc->lock);
Ming Lei1f2b7952012-08-04 12:01:21 +0800291}
292
Ming Lei746058f2012-10-09 12:01:03 +0800293/* direct firmware loading support */
Ming Lei27602842012-11-03 17:47:58 +0800294static char fw_path_para[256];
295static const char * const fw_path[] = {
296 fw_path_para,
Ming Lei746058f2012-10-09 12:01:03 +0800297 "/lib/firmware/updates/" UTS_RELEASE,
298 "/lib/firmware/updates",
299 "/lib/firmware/" UTS_RELEASE,
Satya Durga Srinivasu Prabhala036c9b22017-02-27 11:27:10 -0800300 "/lib/firmware"
Ming Lei746058f2012-10-09 12:01:03 +0800301};
302
Ming Lei27602842012-11-03 17:47:58 +0800303/*
304 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
305 * from kernel command line because firmware_class is generally built in
306 * kernel instead of module.
307 */
308module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
309MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
310
Luis R. Rodriguez5275d192015-07-30 15:48:57 -0700311static void fw_finish_direct_load(struct device *device,
312 struct firmware_buf *buf)
313{
314 mutex_lock(&fw_lock);
315 set_bit(FW_STATUS_DONE, &buf->status);
316 complete_all(&buf->completion);
317 mutex_unlock(&fw_lock);
318}
319
Stephen Boyda098ecd2016-08-02 14:04:28 -0700320static int
321fw_get_filesystem_firmware(struct device *device, struct firmware_buf *buf)
Ming Lei746058f2012-10-09 12:01:03 +0800322{
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500323 loff_t size;
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700324 int i, len;
Neil Horman3e358ac2013-09-06 15:36:08 -0400325 int rc = -ENOENT;
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700326 char *path;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700327 enum kernel_read_file_id id = READING_FIRMWARE;
328 size_t msize = INT_MAX;
329
330 /* Already populated data member means we're loading into a buffer */
331 if (buf->data) {
332 id = READING_FIRMWARE_PREALLOC_BUFFER;
333 msize = buf->allocated_size;
334 }
Luis R. Rodriguezf5727b02015-05-12 14:49:40 -0700335
336 path = __getname();
337 if (!path)
338 return -ENOMEM;
Ming Lei746058f2012-10-09 12:01:03 +0800339
340 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
Ming Lei27602842012-11-03 17:47:58 +0800341 /* skip the unset customized path */
342 if (!fw_path[i][0])
343 continue;
344
Luis R. Rodriguez1ba4de12015-05-12 14:49:41 -0700345 len = snprintf(path, PATH_MAX, "%s/%s",
346 fw_path[i], buf->fw_id);
347 if (len >= PATH_MAX) {
348 rc = -ENAMETOOLONG;
349 break;
350 }
Ming Lei746058f2012-10-09 12:01:03 +0800351
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500352 buf->size = 0;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700353 rc = kernel_read_file_from_path(path, &buf->data, &size, msize,
354 id);
Kees Cook4b2530d2016-02-04 13:15:02 -0800355 if (rc) {
Luis R. Rodriguez8e516aa2016-02-28 21:57:55 +0100356 if (rc == -ENOENT)
357 dev_dbg(device, "loading %s failed with error %d\n",
358 path, rc);
359 else
360 dev_warn(device, "loading %s failed with error %d\n",
361 path, rc);
Kees Cook4b2530d2016-02-04 13:15:02 -0800362 continue;
363 }
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500364 dev_dbg(device, "direct-loading %s\n", buf->fw_id);
365 buf->size = size;
Luis R. Rodriguez5275d192015-07-30 15:48:57 -0700366 fw_finish_direct_load(device, buf);
Kees Cook4b2530d2016-02-04 13:15:02 -0800367 break;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100368 }
Kees Cook4b2530d2016-02-04 13:15:02 -0800369 __putname(path);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +0100370
Neil Horman3e358ac2013-09-06 15:36:08 -0400371 return rc;
Ming Lei746058f2012-10-09 12:01:03 +0800372}
373
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100374/* firmware holds the ownership of pages */
375static void firmware_free_data(const struct firmware *fw)
376{
377 /* Loaded directly? */
378 if (!fw->priv) {
379 vfree(fw->data);
380 return;
381 }
382 fw_free_buf(fw->priv);
383}
384
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100385/* store the pages buffer info firmware from buf */
386static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
387{
388 fw->priv = buf;
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100389#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100390 fw->pages = buf->pages;
391#endif
392 fw->size = buf->size;
393 fw->data = buf->data;
394
395 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
396 __func__, buf->fw_id, buf, buf->data,
397 (unsigned int)buf->size);
398}
399
400#ifdef CONFIG_PM_SLEEP
401static void fw_name_devm_release(struct device *dev, void *res)
402{
403 struct fw_name_devm *fwn = res;
404
405 if (fwn->magic == (unsigned long)&fw_cache)
406 pr_debug("%s: fw_name-%s devm-%p released\n",
407 __func__, fwn->name, res);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700408 kfree_const(fwn->name);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100409}
410
411static int fw_devm_match(struct device *dev, void *res,
412 void *match_data)
413{
414 struct fw_name_devm *fwn = res;
415
416 return (fwn->magic == (unsigned long)&fw_cache) &&
417 !strcmp(fwn->name, match_data);
418}
419
420static struct fw_name_devm *fw_find_devm_name(struct device *dev,
421 const char *name)
422{
423 struct fw_name_devm *fwn;
424
425 fwn = devres_find(dev, fw_name_devm_release,
426 fw_devm_match, (void *)name);
427 return fwn;
428}
429
430/* add firmware name into devres list */
431static int fw_add_devm_name(struct device *dev, const char *name)
432{
433 struct fw_name_devm *fwn;
434
435 fwn = fw_find_devm_name(dev, name);
436 if (fwn)
437 return 1;
438
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700439 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
440 GFP_KERNEL);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100441 if (!fwn)
442 return -ENOMEM;
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700443 fwn->name = kstrdup_const(name, GFP_KERNEL);
444 if (!fwn->name) {
Vladimir Zapolskiya885de62015-07-29 23:26:28 +0300445 devres_free(fwn);
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -0700446 return -ENOMEM;
447 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100448
449 fwn->magic = (unsigned long)&fw_cache;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100450 devres_add(dev, fwn);
451
452 return 0;
453}
454#else
455static int fw_add_devm_name(struct device *dev, const char *name)
456{
457 return 0;
458}
459#endif
460
461
462/*
463 * user-mode helper code
464 */
465#ifdef CONFIG_FW_LOADER_USER_HELPER
466struct firmware_priv {
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100467 bool nowait;
468 struct device dev;
469 struct firmware_buf *buf;
470 struct firmware *fw;
471};
Takashi Iwai7b1269f2013-01-31 11:13:55 +0100472
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700473static struct firmware_priv *to_firmware_priv(struct device *dev)
474{
475 return container_of(dev, struct firmware_priv, dev);
476}
477
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700478static void __fw_load_abort(struct firmware_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Ming Lei87597932013-06-15 16:36:38 +0800480 /*
481 * There is a small window in which user can write to 'loading'
482 * between loading done and disappearance of 'loading'
483 */
484 if (test_bit(FW_STATUS_DONE, &buf->status))
485 return;
486
Takashi Iwaife304142013-05-22 18:28:38 +0200487 list_del_init(&buf->pending_list);
Ming Lei12446912012-08-04 12:01:20 +0800488 set_bit(FW_STATUS_ABORT, &buf->status);
Ming Lei1f2b7952012-08-04 12:01:21 +0800489 complete_all(&buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700492static void fw_load_abort(struct firmware_priv *fw_priv)
493{
494 struct firmware_buf *buf = fw_priv->buf;
495
496 __fw_load_abort(buf);
Ming Lei87597932013-06-15 16:36:38 +0800497
498 /* avoid user action after loading abort */
499 fw_priv->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
Takashi Iwai807be032013-01-31 11:13:57 +0100502#define is_fw_load_aborted(buf) \
503 test_bit(FW_STATUS_ABORT, &(buf)->status)
504
Takashi Iwaife304142013-05-22 18:28:38 +0200505static LIST_HEAD(pending_fw_head);
506
507/* reboot notifier for avoid deadlock with usermode_lock */
508static int fw_shutdown_notify(struct notifier_block *unused1,
509 unsigned long unused2, void *unused3)
510{
511 mutex_lock(&fw_lock);
512 while (!list_empty(&pending_fw_head))
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -0700513 __fw_load_abort(list_first_entry(&pending_fw_head,
Takashi Iwaife304142013-05-22 18:28:38 +0200514 struct firmware_buf,
515 pending_list));
516 mutex_unlock(&fw_lock);
517 return NOTIFY_DONE;
518}
519
520static struct notifier_block fw_shutdown_nb = {
521 .notifier_call = fw_shutdown_notify,
522};
523
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700524static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
525 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 return sprintf(buf, "%d\n", loading_timeout);
528}
529
530/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800531 * firmware_timeout_store - set number of seconds to wait for firmware
532 * @class: device class pointer
Randy Dunlape59817b2010-03-10 11:47:58 -0800533 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800534 * @buf: buffer to scan for timeout value
535 * @count: number of bytes in @buf
536 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 * Sets the number of seconds to wait for the firmware. Once
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800538 * this expires an error will be returned to the driver and no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 * firmware will be provided.
540 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800541 * Note: zero means 'wait forever'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 **/
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700543static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
544 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 loading_timeout = simple_strtol(buf, NULL, 10);
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700547 if (loading_timeout < 0)
548 loading_timeout = 0;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return count;
551}
552
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800553static struct class_attribute firmware_class_attrs[] = {
Greg Kroah-Hartman14adbe52013-08-23 17:08:48 -0700554 __ATTR_RW(timeout),
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800555 __ATTR_NULL
556};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800558static void fw_dev_release(struct device *dev)
559{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700560 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei65710cb2012-08-04 12:01:16 +0800561
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800562 kfree(fw_priv);
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800563}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Linus Torvalds6f957722015-07-09 11:20:01 -0700565static int do_firmware_uevent(struct firmware_priv *fw_priv, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Ming Lei12446912012-08-04 12:01:20 +0800567 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return -ENOMEM;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200569 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
kay.sievers@vrfy.org68970892005-04-18 21:57:31 -0700570 return -ENOMEM;
Johannes Berge9045f92010-03-29 17:57:20 +0200571 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
572 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 return 0;
575}
576
Linus Torvalds6f957722015-07-09 11:20:01 -0700577static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
578{
579 struct firmware_priv *fw_priv = to_firmware_priv(dev);
580 int err = 0;
581
582 mutex_lock(&fw_lock);
583 if (fw_priv->buf)
584 err = do_firmware_uevent(fw_priv, env);
585 mutex_unlock(&fw_lock);
586 return err;
587}
588
Adrian Bunk1b81d662006-05-20 15:00:16 -0700589static struct class firmware_class = {
590 .name = "firmware",
Dmitry Torokhov673fae92010-03-13 23:49:13 -0800591 .class_attrs = firmware_class_attrs,
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700592 .dev_uevent = firmware_uevent,
593 .dev_release = fw_dev_release,
Adrian Bunk1b81d662006-05-20 15:00:16 -0700594};
595
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700596static ssize_t firmware_loading_show(struct device *dev,
597 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700599 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800600 int loading = 0;
601
602 mutex_lock(&fw_lock);
603 if (fw_priv->buf)
604 loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
605 mutex_unlock(&fw_lock);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return sprintf(buf, "%d\n", loading);
608}
609
David Woodhouse6e03a202009-04-09 22:04:07 -0700610/* Some architectures don't have PAGE_KERNEL_RO */
611#ifndef PAGE_KERNEL_RO
612#define PAGE_KERNEL_RO PAGE_KERNEL
613#endif
Ming Lei253c9242012-10-09 12:01:02 +0800614
615/* one pages buffer should be mapped/unmapped only once */
616static int fw_map_pages_buf(struct firmware_buf *buf)
617{
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100618 if (!buf->is_paged_buf)
Ming Lei746058f2012-10-09 12:01:03 +0800619 return 0;
620
Markus Elfringdaa3d672014-11-19 11:38:38 +0100621 vunmap(buf->data);
Ming Lei253c9242012-10-09 12:01:02 +0800622 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
623 if (!buf->data)
624 return -ENOMEM;
625 return 0;
626}
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800629 * firmware_loading_store - set value in the 'loading' control file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700630 * @dev: device pointer
Randy Dunlapaf9997e2006-12-22 01:06:52 -0800631 * @attr: device attribute pointer
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800632 * @buf: buffer to scan for loading control value
633 * @count: number of bytes in @buf
634 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 * The relevant values are:
636 *
637 * 1: Start a load, discarding any previous partial load.
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800638 * 0: Conclude the load and hand the data to the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 * -1: Conclude the load with an error and discard any written data.
640 **/
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700641static ssize_t firmware_loading_store(struct device *dev,
642 struct device_attribute *attr,
643 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700645 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei87597932013-06-15 16:36:38 +0800646 struct firmware_buf *fw_buf;
Kees Cook6593d922014-02-25 13:06:00 -0800647 ssize_t written = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 int loading = simple_strtol(buf, NULL, 10);
David Woodhouse6e03a202009-04-09 22:04:07 -0700649 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Neil Hormaneea915b2012-01-02 15:31:23 -0500651 mutex_lock(&fw_lock);
Ming Lei87597932013-06-15 16:36:38 +0800652 fw_buf = fw_priv->buf;
Ming Lei12446912012-08-04 12:01:20 +0800653 if (!fw_buf)
Neil Hormaneea915b2012-01-02 15:31:23 -0500654 goto out;
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 switch (loading) {
657 case 1:
Ming Lei65710cb2012-08-04 12:01:16 +0800658 /* discarding any previous partial load */
Ming Lei12446912012-08-04 12:01:20 +0800659 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
660 for (i = 0; i < fw_buf->nr_pages; i++)
661 __free_page(fw_buf->pages[i]);
Chen Feng10a3fbf2015-12-16 17:03:15 +0800662 vfree(fw_buf->pages);
Ming Lei12446912012-08-04 12:01:20 +0800663 fw_buf->pages = NULL;
664 fw_buf->page_array_size = 0;
665 fw_buf->nr_pages = 0;
666 set_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei28eefa72012-08-04 12:01:17 +0800667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 break;
669 case 0:
Ming Lei12446912012-08-04 12:01:20 +0800670 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
Kees Cook6593d922014-02-25 13:06:00 -0800671 int rc;
672
Ming Lei12446912012-08-04 12:01:20 +0800673 set_bit(FW_STATUS_DONE, &fw_buf->status);
674 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
Ming Lei253c9242012-10-09 12:01:02 +0800675
676 /*
677 * Several loading requests may be pending on
678 * one same firmware buf, so let all requests
679 * see the mapped 'buf->data' once the loading
680 * is completed.
681 * */
Kees Cook6593d922014-02-25 13:06:00 -0800682 rc = fw_map_pages_buf(fw_buf);
683 if (rc)
zhang jun2b1278c2014-02-13 15:18:47 +0800684 dev_err(dev, "%s: map pages failed\n",
685 __func__);
Kees Cook6593d922014-02-25 13:06:00 -0800686 else
Mimi Zohare40ba6d2015-11-19 12:39:22 -0500687 rc = security_kernel_post_read_file(NULL,
688 fw_buf->data, fw_buf->size,
689 READING_FIRMWARE);
Kees Cook6593d922014-02-25 13:06:00 -0800690
691 /*
692 * Same logic as fw_load_abort, only the DONE bit
693 * is ignored and we set ABORT only on failure.
694 */
Takashi Iwaife304142013-05-22 18:28:38 +0200695 list_del_init(&fw_buf->pending_list);
Kees Cook6593d922014-02-25 13:06:00 -0800696 if (rc) {
697 set_bit(FW_STATUS_ABORT, &fw_buf->status);
698 written = rc;
699 }
Ming Lei1f2b7952012-08-04 12:01:21 +0800700 complete_all(&fw_buf->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 break;
702 }
703 /* fallthrough */
704 default:
Bjorn Helgaas266a8132008-10-15 22:04:20 -0700705 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 /* fallthrough */
707 case -1:
708 fw_load_abort(fw_priv);
709 break;
710 }
Neil Hormaneea915b2012-01-02 15:31:23 -0500711out:
712 mutex_unlock(&fw_lock);
Kees Cook6593d922014-02-25 13:06:00 -0800713 return written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700716static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Stephen Boyda098ecd2016-08-02 14:04:28 -0700718static void firmware_rw_buf(struct firmware_buf *buf, char *buffer,
719 loff_t offset, size_t count, bool read)
720{
721 if (read)
722 memcpy(buffer, buf->data + offset, count);
723 else
724 memcpy(buf->data + offset, buffer, count);
725}
726
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700727static void firmware_rw(struct firmware_buf *buf, char *buffer,
728 loff_t offset, size_t count, bool read)
729{
730 while (count) {
731 void *page_data;
732 int page_nr = offset >> PAGE_SHIFT;
733 int page_ofs = offset & (PAGE_SIZE-1);
734 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
735
736 page_data = kmap(buf->pages[page_nr]);
737
738 if (read)
739 memcpy(buffer, page_data + page_ofs, page_cnt);
740 else
741 memcpy(page_data + page_ofs, buffer, page_cnt);
742
743 kunmap(buf->pages[page_nr]);
744 buffer += page_cnt;
745 offset += page_cnt;
746 count -= page_cnt;
747 }
748}
749
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700750static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
751 struct bin_attribute *bin_attr,
752 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200754 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700755 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800756 struct firmware_buf *buf;
Akinobu Mitaf37e6612008-07-25 01:48:23 -0700757 ssize_t ret_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Laura Garciacad1e552006-05-23 23:22:38 +0200759 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800760 buf = fw_priv->buf;
761 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 ret_count = -ENODEV;
763 goto out;
764 }
Ming Lei12446912012-08-04 12:01:20 +0800765 if (offset > buf->size) {
Jiri Slaby308975f2009-06-21 23:57:31 +0200766 ret_count = 0;
767 goto out;
768 }
Ming Lei12446912012-08-04 12:01:20 +0800769 if (count > buf->size - offset)
770 count = buf->size - offset;
David Woodhouse6e03a202009-04-09 22:04:07 -0700771
772 ret_count = count;
773
Stephen Boyda098ecd2016-08-02 14:04:28 -0700774 if (buf->data)
775 firmware_rw_buf(buf, buffer, offset, count, true);
776 else
777 firmware_rw(buf, buffer, offset, count, true);
David Woodhouse6e03a202009-04-09 22:04:07 -0700778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779out:
Laura Garciacad1e552006-05-23 23:22:38 +0200780 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return ret_count;
782}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800783
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700784static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Ming Lei12446912012-08-04 12:01:20 +0800786 struct firmware_buf *buf = fw_priv->buf;
Fabian Fredericka76040d2014-07-01 21:13:30 +0200787 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
David Woodhouse6e03a202009-04-09 22:04:07 -0700789 /* If the array of pages is too small, grow it... */
Ming Lei12446912012-08-04 12:01:20 +0800790 if (buf->page_array_size < pages_needed) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700791 int new_array_size = max(pages_needed,
Ming Lei12446912012-08-04 12:01:20 +0800792 buf->page_array_size * 2);
David Woodhouse6e03a202009-04-09 22:04:07 -0700793 struct page **new_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Chen Feng10a3fbf2015-12-16 17:03:15 +0800795 new_pages = vmalloc(new_array_size * sizeof(void *));
David Woodhouse6e03a202009-04-09 22:04:07 -0700796 if (!new_pages) {
797 fw_load_abort(fw_priv);
798 return -ENOMEM;
799 }
Ming Lei12446912012-08-04 12:01:20 +0800800 memcpy(new_pages, buf->pages,
801 buf->page_array_size * sizeof(void *));
802 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
803 (new_array_size - buf->page_array_size));
Chen Feng10a3fbf2015-12-16 17:03:15 +0800804 vfree(buf->pages);
Ming Lei12446912012-08-04 12:01:20 +0800805 buf->pages = new_pages;
806 buf->page_array_size = new_array_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700808
Ming Lei12446912012-08-04 12:01:20 +0800809 while (buf->nr_pages < pages_needed) {
810 buf->pages[buf->nr_pages] =
David Woodhouse6e03a202009-04-09 22:04:07 -0700811 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
812
Ming Lei12446912012-08-04 12:01:20 +0800813 if (!buf->pages[buf->nr_pages]) {
David Woodhouse6e03a202009-04-09 22:04:07 -0700814 fw_load_abort(fw_priv);
815 return -ENOMEM;
816 }
Ming Lei12446912012-08-04 12:01:20 +0800817 buf->nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return 0;
820}
821
822/**
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800823 * firmware_data_write - write method for firmware
Chris Wright2c3c8be2010-05-12 18:28:57 -0700824 * @filp: open sysfs file
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700825 * @kobj: kobject for the device
Randy Dunlap42e61f42007-07-23 21:42:11 -0700826 * @bin_attr: bin_attr structure
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800827 * @buffer: buffer being written
828 * @offset: buffer offset for write in total data store area
829 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 *
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800831 * Data written to the 'data' attribute will be later handed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 * the driver as a firmware image.
833 **/
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700834static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
835 struct bin_attribute *bin_attr,
836 char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200838 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700839 struct firmware_priv *fw_priv = to_firmware_priv(dev);
Ming Lei12446912012-08-04 12:01:20 +0800840 struct firmware_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 ssize_t retval;
842
843 if (!capable(CAP_SYS_RAWIO))
844 return -EPERM;
Stanislaw W. Gruszkab92eac02005-06-28 20:44:51 -0700845
Laura Garciacad1e552006-05-23 23:22:38 +0200846 mutex_lock(&fw_lock);
Ming Lei12446912012-08-04 12:01:20 +0800847 buf = fw_priv->buf;
848 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 retval = -ENODEV;
850 goto out;
851 }
Ming Lei65710cb2012-08-04 12:01:16 +0800852
Stephen Boyda098ecd2016-08-02 14:04:28 -0700853 if (buf->data) {
854 if (offset + count > buf->allocated_size) {
855 retval = -ENOMEM;
856 goto out;
857 }
858 firmware_rw_buf(buf, buffer, offset, count, false);
859 retval = count;
860 } else {
861 retval = fw_realloc_buffer(fw_priv, offset + count);
862 if (retval)
863 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Stephen Boyda098ecd2016-08-02 14:04:28 -0700865 retval = count;
866 firmware_rw(buf, buffer, offset, count, false);
867 }
David Woodhouse6e03a202009-04-09 22:04:07 -0700868
Stephen Boyd9ccf9812016-08-02 14:04:22 -0700869 buf->size = max_t(size_t, offset + count, buf->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870out:
Laura Garciacad1e552006-05-23 23:22:38 +0200871 mutex_unlock(&fw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return retval;
873}
Randy Dunlapeb8e3172005-10-30 15:03:01 -0800874
Dmitry Torokhov0983ca22010-06-04 00:54:37 -0700875static struct bin_attribute firmware_attr_data = {
876 .attr = { .name = "data", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 .size = 0,
878 .read = firmware_data_read,
879 .write = firmware_data_write,
880};
881
Takashi Iwai46239902015-02-04 15:18:07 +0100882static struct attribute *fw_dev_attrs[] = {
883 &dev_attr_loading.attr,
884 NULL
885};
886
887static struct bin_attribute *fw_dev_bin_attrs[] = {
888 &firmware_attr_data,
889 NULL
890};
891
892static const struct attribute_group fw_dev_attr_group = {
893 .attrs = fw_dev_attrs,
894 .bin_attrs = fw_dev_bin_attrs,
895};
896
897static const struct attribute_group *fw_dev_attr_groups[] = {
898 &fw_dev_attr_group,
899 NULL
900};
901
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700902static struct firmware_priv *
Stephen Boyddddb5542012-03-28 23:30:43 +0200903fw_create_instance(struct firmware *firmware, const char *fw_name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100904 struct device *device, unsigned int opt_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700906 struct firmware_priv *fw_priv;
907 struct device *f_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Ming Lei12446912012-08-04 12:01:20 +0800909 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700910 if (!fw_priv) {
Ming Lei12446912012-08-04 12:01:20 +0800911 fw_priv = ERR_PTR(-ENOMEM);
912 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100915 fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT);
Ming Lei1f2b7952012-08-04 12:01:21 +0800916 fw_priv->fw = firmware;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700917 f_dev = &fw_priv->dev;
918
919 device_initialize(f_dev);
Ming Lei99c2aa72012-08-04 12:01:19 +0800920 dev_set_name(f_dev, "%s", fw_name);
Greg Kroah-Hartmane55c8792006-09-14 07:30:59 -0700921 f_dev->parent = device;
922 f_dev->class = &firmware_class;
Takashi Iwai46239902015-02-04 15:18:07 +0100923 f_dev->groups = fw_dev_attr_groups;
Ming Lei12446912012-08-04 12:01:20 +0800924exit:
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -0700925 return fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100927
928/* load a firmware via user helper */
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100929static int _request_firmware_load(struct firmware_priv *fw_priv,
930 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100931{
932 int retval = 0;
933 struct device *f_dev = &fw_priv->dev;
934 struct firmware_buf *buf = fw_priv->buf;
935
936 /* fall back on userspace loading */
Stephen Boyda098ecd2016-08-02 14:04:28 -0700937 if (!buf->data)
938 buf->is_paged_buf = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100939
940 dev_set_uevent_suppress(f_dev, true);
941
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100942 retval = device_add(f_dev);
943 if (retval) {
944 dev_err(f_dev, "%s: device_register failed\n", __func__);
945 goto err_put_dev;
946 }
947
Maxime Bizon1eeeef12013-08-29 20:28:13 +0200948 mutex_lock(&fw_lock);
949 list_add(&buf->pending_list, &pending_fw_head);
950 mutex_unlock(&fw_lock);
951
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100952 if (opt_flags & FW_OPT_UEVENT) {
Ming Leiaf5bc112013-05-27 18:30:04 +0800953 buf->need_uevent = true;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100954 dev_set_uevent_suppress(f_dev, false);
955 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100956 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
Ming Lei68ff2a02015-01-13 00:02:01 +0800957 } else {
958 timeout = MAX_JIFFY_OFFSET;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100959 }
960
Kyle Yan5f4db9a2017-09-20 17:18:35 -0700961 timeout = wait_for_completion_killable_timeout(&buf->completion,
Ming Lei68ff2a02015-01-13 00:02:01 +0800962 timeout);
Yves-Alexis Perezb3854ce2016-11-11 11:28:40 -0800963 if (timeout == -ERESTARTSYS || !timeout) {
964 retval = timeout;
Ming Lei0cb64242015-01-13 00:01:35 +0800965 mutex_lock(&fw_lock);
966 fw_load_abort(fw_priv);
967 mutex_unlock(&fw_lock);
Yves-Alexis Perezb3854ce2016-11-11 11:28:40 -0800968 } else if (timeout > 0) {
Zahari Doychevef518cc2015-03-10 10:45:40 +0100969 retval = 0;
Ming Lei0cb64242015-01-13 00:01:35 +0800970 }
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100971
Shuah Khan0542ad82014-07-22 18:10:38 -0600972 if (is_fw_load_aborted(buf))
973 retval = -EAGAIN;
Stephen Boyda098ecd2016-08-02 14:04:28 -0700974 else if (buf->is_paged_buf && !buf->data)
zhang jun2b1278c2014-02-13 15:18:47 +0800975 retval = -ENOMEM;
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100976
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100977 device_del(f_dev);
978err_put_dev:
979 put_device(f_dev);
980 return retval;
981}
982
983static int fw_load_from_user_helper(struct firmware *firmware,
984 const char *name, struct device *device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100985 unsigned int opt_flags, long timeout)
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100986{
987 struct firmware_priv *fw_priv;
988
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100989 fw_priv = fw_create_instance(firmware, name, device, opt_flags);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100990 if (IS_ERR(fw_priv))
991 return PTR_ERR(fw_priv);
992
993 fw_priv->buf = firmware->priv;
Takashi Iwai14c4bae2013-12-02 15:38:18 +0100994 return _request_firmware_load(fw_priv, opt_flags, timeout);
Takashi Iwaicd7239f2013-01-31 11:13:56 +0100995}
Ming Leiddf1f062013-06-04 10:01:13 +0800996
Tim Murray57c7ee12016-10-20 13:53:58 -0700997#ifdef CONFIG_FW_CACHE
Ming Leiddf1f062013-06-04 10:01:13 +0800998/* kill pending requests without uevent to avoid blocking suspend */
999static void kill_requests_without_uevent(void)
1000{
1001 struct firmware_buf *buf;
1002 struct firmware_buf *next;
1003
1004 mutex_lock(&fw_lock);
1005 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
1006 if (!buf->need_uevent)
Greg Kroah-Hartman7068cb02013-06-19 20:26:43 -07001007 __fw_load_abort(buf);
Ming Leiddf1f062013-06-04 10:01:13 +08001008 }
1009 mutex_unlock(&fw_lock);
1010}
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001011#endif
Ming Leiddf1f062013-06-04 10:01:13 +08001012
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001013#else /* CONFIG_FW_LOADER_USER_HELPER */
1014static inline int
1015fw_load_from_user_helper(struct firmware *firmware, const char *name,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001016 struct device *device, unsigned int opt_flags,
Takashi Iwaicd7239f2013-01-31 11:13:56 +01001017 long timeout)
1018{
1019 return -ENOENT;
1020}
Takashi Iwai807be032013-01-31 11:13:57 +01001021
1022/* No abort during direct loading */
1023#define is_fw_load_aborted(buf) false
1024
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001025#ifdef CONFIG_PM_SLEEP
Ming Leiddf1f062013-06-04 10:01:13 +08001026static inline void kill_requests_without_uevent(void) { }
Ming Lei5b7cb7a2013-06-06 19:52:54 +08001027#endif
Ming Leiddf1f062013-06-04 10:01:13 +08001028
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001029#endif /* CONFIG_FW_LOADER_USER_HELPER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Ming Leif531f05a2012-08-04 12:01:25 +08001031
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001032/* wait until the shared firmware_buf becomes ready (or error) */
1033static int sync_cached_firmware_buf(struct firmware_buf *buf)
Ming Lei1f2b7952012-08-04 12:01:21 +08001034{
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001035 int ret = 0;
1036
1037 mutex_lock(&fw_lock);
1038 while (!test_bit(FW_STATUS_DONE, &buf->status)) {
Takashi Iwai807be032013-01-31 11:13:57 +01001039 if (is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001040 ret = -ENOENT;
1041 break;
1042 }
1043 mutex_unlock(&fw_lock);
Kweh, Hock Leong000deba2014-11-18 10:56:59 +08001044 ret = wait_for_completion_interruptible(&buf->completion);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001045 mutex_lock(&fw_lock);
1046 }
1047 mutex_unlock(&fw_lock);
1048 return ret;
Ming Lei1f2b7952012-08-04 12:01:21 +08001049}
1050
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001051/* prepare firmware and firmware_buf structs;
1052 * return 0 if a firmware is already assigned, 1 if need to load one,
1053 * or a negative error code
1054 */
1055static int
1056_request_firmware_prepare(struct firmware **firmware_p, const char *name,
Vikram Mulukutlab19be882017-04-24 19:31:14 -07001057 struct device *device, void *dbuf, size_t size,
1058 unsigned int opt_flags)
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001059{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 struct firmware *firmware;
Ming Lei1f2b7952012-08-04 12:01:21 +08001061 struct firmware_buf *buf;
1062 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Jiri Slaby4aed0642005-09-13 01:25:01 -07001064 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (!firmware) {
Bjorn Helgaas266a8132008-10-15 22:04:20 -07001066 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1067 __func__);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001068 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Stephen Boyda098ecd2016-08-02 14:04:28 -07001071 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
Luis R. Rodriguezed046302015-04-29 16:30:43 -07001072 dev_dbg(device, "using built-in %s\n", name);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001073 return 0; /* assigned */
David Woodhouse5658c762008-05-23 13:52:42 +01001074 }
1075
Vikram Mulukutlab19be882017-04-24 19:31:14 -07001076 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf, dbuf, size,
1077 opt_flags);
Ming Lei1f2b7952012-08-04 12:01:21 +08001078
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001079 /*
1080 * bind with 'buf' now to avoid warning in failure path
1081 * of requesting firmware.
1082 */
1083 firmware->priv = buf;
Ming Lei1f2b7952012-08-04 12:01:21 +08001084
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001085 if (ret > 0) {
1086 ret = sync_cached_firmware_buf(buf);
1087 if (!ret) {
1088 fw_set_page_data(buf, firmware);
1089 return 0; /* assigned */
1090 }
Stephen Boyddddb5542012-03-28 23:30:43 +02001091 }
Ming Lei1f2b7952012-08-04 12:01:21 +08001092
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001093 if (ret < 0)
1094 return ret;
1095 return 1; /* need to load */
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001096}
1097
Ming Leie771d1a2013-05-27 18:30:03 +08001098static int assign_firmware_buf(struct firmware *fw, struct device *device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001099 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001100{
1101 struct firmware_buf *buf = fw->priv;
1102
1103 mutex_lock(&fw_lock);
Takashi Iwai807be032013-01-31 11:13:57 +01001104 if (!buf->size || is_fw_load_aborted(buf)) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001105 mutex_unlock(&fw_lock);
1106 return -ENOENT;
1107 }
1108
1109 /*
1110 * add firmware name into devres list so that we can auto cache
1111 * and uncache firmware for device.
1112 *
1113 * device may has been deleted already, but the problem
1114 * should be fixed in devres or driver core.
1115 */
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001116 /* don't cache firmware handled without uevent */
Vikram Mulukutla0e742e92016-08-02 14:04:25 -07001117 if (device && (opt_flags & FW_OPT_UEVENT) &&
1118 !(opt_flags & FW_OPT_NOCACHE))
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001119 fw_add_devm_name(device, buf->fw_id);
1120
1121 /*
1122 * After caching firmware image is started, let it piggyback
1123 * on request firmware.
1124 */
Vikram Mulukutla0e742e92016-08-02 14:04:25 -07001125 if (!(opt_flags & FW_OPT_NOCACHE) &&
1126 buf->fwc->state == FW_LOADER_START_CACHE) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001127 if (fw_cache_piggyback_on_request(buf->fw_id))
1128 kref_get(&buf->ref);
1129 }
1130
1131 /* pass the pages buffer to driver at the last minute */
1132 fw_set_page_data(buf, fw);
1133 mutex_unlock(&fw_lock);
1134 return 0;
1135}
1136
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001137/* called from request_firmware() and request_firmware_work_func() */
1138static int
1139_request_firmware(const struct firmware **firmware_p, const char *name,
Stephen Boyda098ecd2016-08-02 14:04:28 -07001140 struct device *device, void *buf, size_t size,
1141 unsigned int opt_flags)
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001142{
Brian Norris715780a2015-12-09 14:50:28 -08001143 struct firmware *fw = NULL;
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001144 long timeout;
1145 int ret;
1146
1147 if (!firmware_p)
1148 return -EINVAL;
1149
Brian Norris715780a2015-12-09 14:50:28 -08001150 if (!name || name[0] == '\0') {
1151 ret = -EINVAL;
1152 goto out;
1153 }
Kees Cook471b0952014-09-18 11:25:37 -07001154
Vikram Mulukutlab19be882017-04-24 19:31:14 -07001155 ret = _request_firmware_prepare(&fw, name, device, buf, size,
1156 opt_flags);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001157 if (ret <= 0) /* error or already assigned */
1158 goto out;
1159
1160 ret = 0;
1161 timeout = firmware_loading_timeout();
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001162 if (opt_flags & FW_OPT_NOWAIT) {
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001163 timeout = usermodehelper_read_lock_wait(timeout);
1164 if (!timeout) {
1165 dev_dbg(device, "firmware: %s loading timed out\n",
1166 name);
1167 ret = -EBUSY;
1168 goto out;
1169 }
1170 } else {
1171 ret = usermodehelper_read_trylock();
1172 if (WARN_ON(ret)) {
1173 dev_err(device, "firmware: %s will not be loaded\n",
1174 name);
1175 goto out;
1176 }
1177 }
1178
Neil Horman3e358ac2013-09-06 15:36:08 -04001179 ret = fw_get_filesystem_firmware(device, fw->priv);
1180 if (ret) {
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001181 if (!(opt_flags & FW_OPT_NO_WARN))
Kyle Yan7e2a08d2017-05-09 15:40:17 -07001182 dev_dbg(device,
1183 "Firmware %s was not found in kernel paths. rc:%d\n",
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001184 name, ret);
1185 if (opt_flags & FW_OPT_USERHELPER) {
Kyle Yan7e2a08d2017-05-09 15:40:17 -07001186 dev_dbg(device, "Falling back to user helper\n");
Takashi Iwaibba3a872013-12-02 15:38:16 +01001187 ret = fw_load_from_user_helper(fw, name, device,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001188 opt_flags, timeout);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001189 }
Neil Horman3e358ac2013-09-06 15:36:08 -04001190 }
Ming Leie771d1a2013-05-27 18:30:03 +08001191
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001192 if (!ret)
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001193 ret = assign_firmware_buf(fw, device, opt_flags);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001194
1195 usermodehelper_read_unlock();
1196
1197 out:
1198 if (ret < 0) {
1199 release_firmware(fw);
1200 fw = NULL;
1201 }
1202
1203 *firmware_p = fw;
1204 return ret;
1205}
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207/**
Kay Sievers312c0042005-11-16 09:00:00 +01001208 * request_firmware: - send firmware request and wait for it
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001209 * @firmware_p: pointer to firmware image
1210 * @name: name of firmware file
1211 * @device: device for which firmware is being loaded
1212 *
1213 * @firmware_p will be used to return a firmware image by the name
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001214 * of @name for device @device.
1215 *
1216 * Should be called from user context where sleeping is allowed.
1217 *
Kay Sievers312c0042005-11-16 09:00:00 +01001218 * @name will be used as $FIRMWARE in the uevent environment and
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001219 * should be distinctive enough not to be confused with any other
1220 * firmware image for this or any other device.
Ming Lei0cfc1e12012-08-04 12:01:23 +08001221 *
1222 * Caller must hold the reference count of @device.
Ming Lei6a927852012-11-03 17:48:16 +08001223 *
1224 * The function can be called safely inside device's suspend and
1225 * resume callback.
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001226 **/
1227int
1228request_firmware(const struct firmware **firmware_p, const char *name,
Andrei Opreaea310032015-03-08 12:41:15 +02001229 struct device *device)
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001230{
Ming Leid6c8aa32013-06-06 20:01:48 +08001231 int ret;
1232
1233 /* Need to pin this module until return */
1234 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001235 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001236 FW_OPT_UEVENT | FW_OPT_FALLBACK);
Ming Leid6c8aa32013-06-06 20:01:48 +08001237 module_put(THIS_MODULE);
1238 return ret;
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001239}
Daniel Mackf4945132013-05-23 22:17:18 +02001240EXPORT_SYMBOL(request_firmware);
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001241
Takashi Iwaibba3a872013-12-02 15:38:16 +01001242/**
Borislav Petkov3c1556b2014-12-03 22:46:37 +01001243 * request_firmware_direct: - load firmware directly without usermode helper
Takashi Iwaibba3a872013-12-02 15:38:16 +01001244 * @firmware_p: pointer to firmware image
1245 * @name: name of firmware file
1246 * @device: device for which firmware is being loaded
1247 *
1248 * This function works pretty much like request_firmware(), but this doesn't
1249 * fall back to usermode helper even if the firmware couldn't be loaded
1250 * directly from fs. Hence it's useful for loading optional firmwares, which
1251 * aren't always present, without extra long timeouts of udev.
1252 **/
1253int request_firmware_direct(const struct firmware **firmware_p,
1254 const char *name, struct device *device)
1255{
1256 int ret;
Andrei Opreaea310032015-03-08 12:41:15 +02001257
Takashi Iwaibba3a872013-12-02 15:38:16 +01001258 __module_get(THIS_MODULE);
Stephen Boyda098ecd2016-08-02 14:04:28 -07001259 ret = _request_firmware(firmware_p, name, device, NULL, 0,
Luis R. Rodriguezc868edf2014-07-02 09:55:05 -07001260 FW_OPT_UEVENT | FW_OPT_NO_WARN);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001261 module_put(THIS_MODULE);
1262 return ret;
1263}
1264EXPORT_SYMBOL_GPL(request_firmware_direct);
Takashi Iwaibba3a872013-12-02 15:38:16 +01001265
Abhay Salunke6e3eaab2005-09-06 15:17:13 -07001266/**
Stephen Boyda098ecd2016-08-02 14:04:28 -07001267 * request_firmware_into_buf - load firmware into a previously allocated buffer
1268 * @firmware_p: pointer to firmware image
1269 * @name: name of firmware file
1270 * @device: device for which firmware is being loaded and DMA region allocated
1271 * @buf: address of buffer to load firmware into
1272 * @size: size of buffer
1273 *
1274 * This function works pretty much like request_firmware(), but it doesn't
1275 * allocate a buffer to hold the firmware data. Instead, the firmware
1276 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
1277 * data member is pointed at @buf.
1278 *
1279 * This function doesn't cache firmware either.
1280 */
1281int
1282request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
1283 struct device *device, void *buf, size_t size)
1284{
1285 int ret;
1286
1287 __module_get(THIS_MODULE);
1288 ret = _request_firmware(firmware_p, name, device, buf, size,
1289 FW_OPT_UEVENT | FW_OPT_FALLBACK |
1290 FW_OPT_NOCACHE);
1291 module_put(THIS_MODULE);
1292 return ret;
1293}
1294EXPORT_SYMBOL(request_firmware_into_buf);
1295
1296/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 * release_firmware: - release the resource associated with a firmware image
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001298 * @fw: firmware resource to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 **/
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001300void release_firmware(const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 if (fw) {
Dmitry Torokhovbcb9bd12010-03-13 23:49:18 -08001303 if (!fw_is_builtin_firmware(fw))
1304 firmware_free_data(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 kfree(fw);
1306 }
1307}
Daniel Mackf4945132013-05-23 22:17:18 +02001308EXPORT_SYMBOL(release_firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310/* Async support */
1311struct firmware_work {
1312 struct work_struct work;
1313 struct module *module;
1314 const char *name;
1315 struct device *device;
1316 void *context;
1317 void (*cont)(const struct firmware *fw, void *context);
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001318 unsigned int opt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319};
1320
Stephen Boyda36cf842012-03-28 23:31:00 +02001321static void request_firmware_work_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Stephen Boyda36cf842012-03-28 23:31:00 +02001323 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 const struct firmware *fw;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001325
Stephen Boyda36cf842012-03-28 23:31:00 +02001326 fw_work = container_of(work, struct firmware_work, work);
Rafael J. Wysocki811fa402012-03-28 23:29:55 +02001327
Stephen Boyda098ecd2016-08-02 14:04:28 -07001328 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001329 fw_work->opt_flags);
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001330 fw_work->cont(fw, fw_work->context);
Takashi Iwai4e0c92d2013-01-31 11:13:54 +01001331 put_device(fw_work->device); /* taken in request_firmware_nowait() */
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 module_put(fw_work->module);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001334 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 kfree(fw_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
1338/**
Ben Hutchings3c31f072010-02-14 14:18:53 +00001339 * request_firmware_nowait - asynchronous version of request_firmware
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001340 * @module: module requesting the firmware
Kay Sievers312c0042005-11-16 09:00:00 +01001341 * @uevent: sends uevent to copy the firmware image if this flag
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001342 * is non-zero else the firmware copy must be done manually.
1343 * @name: name of firmware file
1344 * @device: device for which firmware is being loaded
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001345 * @gfp: allocation flags
Randy Dunlapeb8e3172005-10-30 15:03:01 -08001346 * @context: will be passed over to @cont, and
1347 * @fw may be %NULL if firmware request fails.
1348 * @cont: function will be called asynchronously when the firmware
1349 * request is over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 *
Ming Lei0cfc1e12012-08-04 12:01:23 +08001351 * Caller must hold the reference count of @device.
1352 *
Ming Lei6f21a622012-08-04 12:01:24 +08001353 * Asynchronous variant of request_firmware() for user contexts:
1354 * - sleep for as small periods as possible since it may
1355 * increase kernel boot time of built-in device drivers
1356 * requesting firmware in their ->probe() methods, if
1357 * @gfp is GFP_KERNEL.
1358 *
1359 * - can't sleep at all if @gfp is GFP_ATOMIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 **/
1361int
1362request_firmware_nowait(
Bob Liu072fc8f2011-01-26 18:33:32 +08001363 struct module *module, bool uevent,
Johannes Berg9ebfbd42009-10-29 12:36:02 +01001364 const char *name, struct device *device, gfp_t gfp, void *context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 void (*cont)(const struct firmware *fw, void *context))
1366{
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001367 struct firmware_work *fw_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Andrei Opreaea310032015-03-08 12:41:15 +02001369 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (!fw_work)
1371 return -ENOMEM;
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001372
1373 fw_work->module = module;
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001374 fw_work->name = kstrdup_const(name, gfp);
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001375 if (!fw_work->name) {
1376 kfree(fw_work);
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001377 return -ENOMEM;
Luis R. Rodriguez303cda02015-05-28 17:46:42 -07001378 }
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001379 fw_work->device = device;
1380 fw_work->context = context;
1381 fw_work->cont = cont;
Takashi Iwai14c4bae2013-12-02 15:38:18 +01001382 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
Takashi Iwai5a1379e2014-06-04 17:48:15 +02001383 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
Dmitry Torokhovf8a4bd32010-06-04 00:54:43 -07001384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (!try_module_get(module)) {
Luis R. Rodriguezf9692b22015-05-12 14:49:42 -07001386 kfree_const(fw_work->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 kfree(fw_work);
1388 return -EFAULT;
1389 }
1390
Ming Lei0cfc1e12012-08-04 12:01:23 +08001391 get_device(fw_work->device);
Stephen Boyda36cf842012-03-28 23:31:00 +02001392 INIT_WORK(&fw_work->work, request_firmware_work_func);
1393 schedule_work(&fw_work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return 0;
1395}
Daniel Mackf4945132013-05-23 22:17:18 +02001396EXPORT_SYMBOL(request_firmware_nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Tim Murray57c7ee12016-10-20 13:53:58 -07001398#ifdef CONFIG_FW_CACHE
Ming Lei90f890812013-06-20 12:30:16 +08001399static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1400
Ming Lei2887b392012-08-04 12:01:22 +08001401/**
1402 * cache_firmware - cache one firmware image in kernel memory space
1403 * @fw_name: the firmware image name
1404 *
1405 * Cache firmware in kernel memory so that drivers can use it when
1406 * system isn't ready for them to request firmware image from userspace.
1407 * Once it returns successfully, driver can use request_firmware or its
1408 * nowait version to get the cached firmware without any interacting
1409 * with userspace
1410 *
1411 * Return 0 if the firmware image has been cached successfully
1412 * Return !0 otherwise
1413 *
1414 */
Ming Lei93232e42013-06-06 20:01:47 +08001415static int cache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001416{
1417 int ret;
1418 const struct firmware *fw;
1419
1420 pr_debug("%s: %s\n", __func__, fw_name);
1421
1422 ret = request_firmware(&fw, fw_name, NULL);
1423 if (!ret)
1424 kfree(fw);
1425
1426 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1427
1428 return ret;
1429}
1430
Ming Lei6a2c1232013-06-26 09:28:17 +08001431static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1432{
1433 struct firmware_buf *tmp;
1434 struct firmware_cache *fwc = &fw_cache;
1435
1436 spin_lock(&fwc->lock);
1437 tmp = __fw_lookup_buf(fw_name);
1438 spin_unlock(&fwc->lock);
1439
1440 return tmp;
1441}
1442
Ming Lei2887b392012-08-04 12:01:22 +08001443/**
1444 * uncache_firmware - remove one cached firmware image
1445 * @fw_name: the firmware image name
1446 *
1447 * Uncache one firmware image which has been cached successfully
1448 * before.
1449 *
1450 * Return 0 if the firmware cache has been removed successfully
1451 * Return !0 otherwise
1452 *
1453 */
Ming Lei93232e42013-06-06 20:01:47 +08001454static int uncache_firmware(const char *fw_name)
Ming Lei2887b392012-08-04 12:01:22 +08001455{
1456 struct firmware_buf *buf;
1457 struct firmware fw;
1458
1459 pr_debug("%s: %s\n", __func__, fw_name);
1460
Stephen Boyda098ecd2016-08-02 14:04:28 -07001461 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
Ming Lei2887b392012-08-04 12:01:22 +08001462 return 0;
1463
1464 buf = fw_lookup_buf(fw_name);
1465 if (buf) {
1466 fw_free_buf(buf);
1467 return 0;
1468 }
1469
1470 return -EINVAL;
1471}
1472
Ming Lei37276a52012-08-04 12:01:27 +08001473static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1474{
1475 struct fw_cache_entry *fce;
1476
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001477 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
Ming Lei37276a52012-08-04 12:01:27 +08001478 if (!fce)
1479 goto exit;
1480
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001481 fce->name = kstrdup_const(name, GFP_ATOMIC);
1482 if (!fce->name) {
1483 kfree(fce);
1484 fce = NULL;
1485 goto exit;
1486 }
Ming Lei37276a52012-08-04 12:01:27 +08001487exit:
1488 return fce;
1489}
1490
Ming Lei373304f2012-10-09 12:01:01 +08001491static int __fw_entry_found(const char *name)
1492{
1493 struct firmware_cache *fwc = &fw_cache;
1494 struct fw_cache_entry *fce;
1495
1496 list_for_each_entry(fce, &fwc->fw_names, list) {
1497 if (!strcmp(fce->name, name))
1498 return 1;
1499 }
1500 return 0;
1501}
1502
Ming Leiac39b3e2012-08-20 19:04:16 +08001503static int fw_cache_piggyback_on_request(const char *name)
1504{
1505 struct firmware_cache *fwc = &fw_cache;
1506 struct fw_cache_entry *fce;
1507 int ret = 0;
1508
1509 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001510 if (__fw_entry_found(name))
1511 goto found;
Ming Leiac39b3e2012-08-20 19:04:16 +08001512
1513 fce = alloc_fw_cache_entry(name);
1514 if (fce) {
1515 ret = 1;
1516 list_add(&fce->list, &fwc->fw_names);
1517 pr_debug("%s: fw: %s\n", __func__, name);
1518 }
1519found:
1520 spin_unlock(&fwc->name_lock);
1521 return ret;
1522}
1523
Ming Lei37276a52012-08-04 12:01:27 +08001524static void free_fw_cache_entry(struct fw_cache_entry *fce)
1525{
Luis R. Rodrigueze0fd9b12015-05-12 14:49:43 -07001526 kfree_const(fce->name);
Ming Lei37276a52012-08-04 12:01:27 +08001527 kfree(fce);
1528}
1529
1530static void __async_dev_cache_fw_image(void *fw_entry,
1531 async_cookie_t cookie)
1532{
1533 struct fw_cache_entry *fce = fw_entry;
1534 struct firmware_cache *fwc = &fw_cache;
1535 int ret;
1536
1537 ret = cache_firmware(fce->name);
Ming Leiac39b3e2012-08-20 19:04:16 +08001538 if (ret) {
1539 spin_lock(&fwc->name_lock);
1540 list_del(&fce->list);
1541 spin_unlock(&fwc->name_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001542
Ming Leiac39b3e2012-08-20 19:04:16 +08001543 free_fw_cache_entry(fce);
1544 }
Ming Lei37276a52012-08-04 12:01:27 +08001545}
1546
1547/* called with dev->devres_lock held */
1548static void dev_create_fw_entry(struct device *dev, void *res,
1549 void *data)
1550{
1551 struct fw_name_devm *fwn = res;
1552 const char *fw_name = fwn->name;
1553 struct list_head *head = data;
1554 struct fw_cache_entry *fce;
1555
1556 fce = alloc_fw_cache_entry(fw_name);
1557 if (fce)
1558 list_add(&fce->list, head);
1559}
1560
1561static int devm_name_match(struct device *dev, void *res,
1562 void *match_data)
1563{
1564 struct fw_name_devm *fwn = res;
1565 return (fwn->magic == (unsigned long)match_data);
1566}
1567
Ming Leiab6dd8e2012-08-17 22:07:00 +08001568static void dev_cache_fw_image(struct device *dev, void *data)
Ming Lei37276a52012-08-04 12:01:27 +08001569{
1570 LIST_HEAD(todo);
1571 struct fw_cache_entry *fce;
1572 struct fw_cache_entry *fce_next;
1573 struct firmware_cache *fwc = &fw_cache;
1574
1575 devres_for_each_res(dev, fw_name_devm_release,
1576 devm_name_match, &fw_cache,
1577 dev_create_fw_entry, &todo);
1578
1579 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1580 list_del(&fce->list);
1581
1582 spin_lock(&fwc->name_lock);
Ming Lei373304f2012-10-09 12:01:01 +08001583 /* only one cache entry for one firmware */
1584 if (!__fw_entry_found(fce->name)) {
Ming Lei373304f2012-10-09 12:01:01 +08001585 list_add(&fce->list, &fwc->fw_names);
1586 } else {
1587 free_fw_cache_entry(fce);
1588 fce = NULL;
1589 }
Ming Lei37276a52012-08-04 12:01:27 +08001590 spin_unlock(&fwc->name_lock);
1591
Ming Lei373304f2012-10-09 12:01:01 +08001592 if (fce)
Ming Leid28d3882012-10-09 12:01:04 +08001593 async_schedule_domain(__async_dev_cache_fw_image,
1594 (void *)fce,
1595 &fw_cache_domain);
Ming Lei37276a52012-08-04 12:01:27 +08001596 }
1597}
1598
1599static void __device_uncache_fw_images(void)
1600{
1601 struct firmware_cache *fwc = &fw_cache;
1602 struct fw_cache_entry *fce;
1603
1604 spin_lock(&fwc->name_lock);
1605 while (!list_empty(&fwc->fw_names)) {
1606 fce = list_entry(fwc->fw_names.next,
1607 struct fw_cache_entry, list);
1608 list_del(&fce->list);
1609 spin_unlock(&fwc->name_lock);
1610
1611 uncache_firmware(fce->name);
1612 free_fw_cache_entry(fce);
1613
1614 spin_lock(&fwc->name_lock);
1615 }
1616 spin_unlock(&fwc->name_lock);
1617}
1618
1619/**
1620 * device_cache_fw_images - cache devices' firmware
1621 *
1622 * If one device called request_firmware or its nowait version
1623 * successfully before, the firmware names are recored into the
1624 * device's devres link list, so device_cache_fw_images can call
1625 * cache_firmware() to cache these firmwares for the device,
1626 * then the device driver can load its firmwares easily at
1627 * time when system is not ready to complete loading firmware.
1628 */
1629static void device_cache_fw_images(void)
1630{
1631 struct firmware_cache *fwc = &fw_cache;
Ming Leiffe53f62012-08-04 12:01:28 +08001632 int old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001633 DEFINE_WAIT(wait);
1634
1635 pr_debug("%s\n", __func__);
1636
Ming Lei373304f2012-10-09 12:01:01 +08001637 /* cancel uncache work */
1638 cancel_delayed_work_sync(&fwc->work);
1639
Ming Leiffe53f62012-08-04 12:01:28 +08001640 /*
1641 * use small loading timeout for caching devices' firmware
1642 * because all these firmware images have been loaded
1643 * successfully at lease once, also system is ready for
1644 * completing firmware loading now. The maximum size of
1645 * firmware in current distributions is about 2M bytes,
1646 * so 10 secs should be enough.
1647 */
1648 old_timeout = loading_timeout;
1649 loading_timeout = 10;
1650
Ming Leiac39b3e2012-08-20 19:04:16 +08001651 mutex_lock(&fw_lock);
1652 fwc->state = FW_LOADER_START_CACHE;
Ming Leiab6dd8e2012-08-17 22:07:00 +08001653 dpm_for_each_dev(NULL, dev_cache_fw_image);
Ming Leiac39b3e2012-08-20 19:04:16 +08001654 mutex_unlock(&fw_lock);
Ming Lei37276a52012-08-04 12:01:27 +08001655
1656 /* wait for completion of caching firmware for all devices */
Ming Leid28d3882012-10-09 12:01:04 +08001657 async_synchronize_full_domain(&fw_cache_domain);
Ming Leiffe53f62012-08-04 12:01:28 +08001658
1659 loading_timeout = old_timeout;
Ming Lei37276a52012-08-04 12:01:27 +08001660}
1661
1662/**
1663 * device_uncache_fw_images - uncache devices' firmware
1664 *
1665 * uncache all firmwares which have been cached successfully
1666 * by device_uncache_fw_images earlier
1667 */
1668static void device_uncache_fw_images(void)
1669{
1670 pr_debug("%s\n", __func__);
1671 __device_uncache_fw_images();
1672}
1673
1674static void device_uncache_fw_images_work(struct work_struct *work)
1675{
1676 device_uncache_fw_images();
1677}
1678
1679/**
1680 * device_uncache_fw_images_delay - uncache devices firmwares
1681 * @delay: number of milliseconds to delay uncache device firmwares
1682 *
1683 * uncache all devices's firmwares which has been cached successfully
1684 * by device_cache_fw_images after @delay milliseconds.
1685 */
1686static void device_uncache_fw_images_delay(unsigned long delay)
1687{
Shaibal Duttabce66182014-01-31 15:44:58 -08001688 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1689 msecs_to_jiffies(delay));
Ming Lei37276a52012-08-04 12:01:27 +08001690}
1691
Ming Lei07646d92012-08-04 12:01:29 +08001692static int fw_pm_notify(struct notifier_block *notify_block,
1693 unsigned long mode, void *unused)
1694{
1695 switch (mode) {
1696 case PM_HIBERNATION_PREPARE:
1697 case PM_SUSPEND_PREPARE:
Sebastian Capellaf8d5b9e2014-02-18 17:52:08 -08001698 case PM_RESTORE_PREPARE:
Ming Leiaf5bc112013-05-27 18:30:04 +08001699 kill_requests_without_uevent();
Ming Lei07646d92012-08-04 12:01:29 +08001700 device_cache_fw_images();
1701 break;
1702
1703 case PM_POST_SUSPEND:
1704 case PM_POST_HIBERNATION:
1705 case PM_POST_RESTORE:
Ming Leiac39b3e2012-08-20 19:04:16 +08001706 /*
1707 * In case that system sleep failed and syscore_suspend is
1708 * not called.
1709 */
1710 mutex_lock(&fw_lock);
1711 fw_cache.state = FW_LOADER_NO_CACHE;
1712 mutex_unlock(&fw_lock);
1713
Ming Lei07646d92012-08-04 12:01:29 +08001714 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1715 break;
1716 }
1717
1718 return 0;
1719}
Ming Lei07646d92012-08-04 12:01:29 +08001720
Ming Leiac39b3e2012-08-20 19:04:16 +08001721/* stop caching firmware once syscore_suspend is reached */
1722static int fw_suspend(void)
1723{
1724 fw_cache.state = FW_LOADER_NO_CACHE;
1725 return 0;
1726}
1727
1728static struct syscore_ops fw_syscore_ops = {
1729 .suspend = fw_suspend,
1730};
Ming Leicfe016b2012-09-08 17:32:30 +08001731#else
1732static int fw_cache_piggyback_on_request(const char *name)
1733{
1734 return 0;
1735}
1736#endif
Ming Leiac39b3e2012-08-20 19:04:16 +08001737
Ming Lei37276a52012-08-04 12:01:27 +08001738static void __init fw_cache_init(void)
1739{
1740 spin_lock_init(&fw_cache.lock);
1741 INIT_LIST_HEAD(&fw_cache.head);
Ming Leicfe016b2012-09-08 17:32:30 +08001742 fw_cache.state = FW_LOADER_NO_CACHE;
Ming Lei37276a52012-08-04 12:01:27 +08001743
Tim Murray57c7ee12016-10-20 13:53:58 -07001744#ifdef CONFIG_FW_CACHE
Ming Lei37276a52012-08-04 12:01:27 +08001745 spin_lock_init(&fw_cache.name_lock);
1746 INIT_LIST_HEAD(&fw_cache.fw_names);
Ming Lei37276a52012-08-04 12:01:27 +08001747
Ming Lei37276a52012-08-04 12:01:27 +08001748 INIT_DELAYED_WORK(&fw_cache.work,
1749 device_uncache_fw_images_work);
Ming Lei07646d92012-08-04 12:01:29 +08001750
1751 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1752 register_pm_notifier(&fw_cache.pm_notify);
Ming Leiac39b3e2012-08-20 19:04:16 +08001753
1754 register_syscore_ops(&fw_syscore_ops);
Ming Leicfe016b2012-09-08 17:32:30 +08001755#endif
Ming Lei37276a52012-08-04 12:01:27 +08001756}
1757
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001758static int __init firmware_class_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Ming Lei1f2b7952012-08-04 12:01:21 +08001760 fw_cache_init();
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001761#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001762 register_reboot_notifier(&fw_shutdown_nb);
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001763 return class_register(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001764#else
1765 return 0;
1766#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767}
Dmitry Torokhov673fae92010-03-13 23:49:13 -08001768
1769static void __exit firmware_class_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Tim Murray57c7ee12016-10-20 13:53:58 -07001771#ifdef CONFIG_FW_CACHE
Ming Leiac39b3e2012-08-20 19:04:16 +08001772 unregister_syscore_ops(&fw_syscore_ops);
Ming Lei07646d92012-08-04 12:01:29 +08001773 unregister_pm_notifier(&fw_cache.pm_notify);
Ming Leicfe016b2012-09-08 17:32:30 +08001774#endif
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001775#ifdef CONFIG_FW_LOADER_USER_HELPER
Takashi Iwaife304142013-05-22 18:28:38 +02001776 unregister_reboot_notifier(&fw_shutdown_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 class_unregister(&firmware_class);
Takashi Iwai7b1269f2013-01-31 11:13:55 +01001778#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780
Shaohua Lia30a6a22006-09-27 01:50:52 -07001781fs_initcall(firmware_class_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782module_exit(firmware_class_exit);