]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/base/firmware_class.c
firmware: add sanity check on shutdown/suspend
[mirror_ubuntu-bionic-kernel.git] / drivers / base / firmware_class.c
CommitLineData
1da177e4
LT
1/*
2 * firmware_class.c - Multi purpose firmware loading support
3 *
87d37a4f 4 * Copyright (c) 2003 Manuel Estrada Sainz
1da177e4
LT
5 *
6 * Please see Documentation/firmware_class/ for more information.
7 *
8 */
9
c59ede7b 10#include <linux/capability.h>
1da177e4
LT
11#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>
cad1e55d 18#include <linux/mutex.h>
a36cf844 19#include <linux/workqueue.h>
6e03a201 20#include <linux/highmem.h>
1da177e4 21#include <linux/firmware.h>
5a0e3ad6 22#include <linux/slab.h>
a36cf844 23#include <linux/sched.h>
abb139e7 24#include <linux/file.h>
1f2b7959 25#include <linux/list.h>
e40ba6d5 26#include <linux/fs.h>
37276a51
ML
27#include <linux/async.h>
28#include <linux/pm.h>
07646d9c 29#include <linux/suspend.h>
ac39b3ea 30#include <linux/syscore_ops.h>
fe304143 31#include <linux/reboot.h>
6593d924 32#include <linux/security.h>
5b029624 33#include <linux/swait.h>
37276a51 34
abb139e7
LT
35#include <generated/utsrelease.h>
36
37276a51 37#include "base.h"
1da177e4 38
87d37a4f 39MODULE_AUTHOR("Manuel Estrada Sainz");
1da177e4
LT
40MODULE_DESCRIPTION("Multi purpose firmware loading support");
41MODULE_LICENSE("GPL");
42
bcb9bd18
DT
43/* Builtin firmware support */
44
45#ifdef CONFIG_FW_LOADER
46
47extern struct builtin_fw __start_builtin_fw[];
48extern struct builtin_fw __end_builtin_fw[];
49
a098ecd2
SB
50static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
51 void *buf, size_t size)
bcb9bd18
DT
52{
53 struct builtin_fw *b_fw;
54
55 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
56 if (strcmp(name, b_fw->name) == 0) {
57 fw->size = b_fw->size;
58 fw->data = b_fw->data;
a098ecd2
SB
59
60 if (buf && fw->size <= size)
61 memcpy(buf, fw->data, fw->size);
bcb9bd18
DT
62 return true;
63 }
64 }
65
66 return false;
67}
68
69static bool fw_is_builtin_firmware(const struct firmware *fw)
70{
71 struct builtin_fw *b_fw;
72
73 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
74 if (fw->data == b_fw->data)
75 return true;
76
77 return false;
78}
79
80#else /* Module case - no builtin firmware support */
81
a098ecd2
SB
82static inline bool fw_get_builtin_firmware(struct firmware *fw,
83 const char *name, void *buf,
84 size_t size)
bcb9bd18
DT
85{
86 return false;
87}
88
89static inline bool fw_is_builtin_firmware(const struct firmware *fw)
90{
91 return false;
92}
93#endif
94
f52cc379
DW
95enum fw_status {
96 FW_STATUS_UNKNOWN,
1da177e4
LT
97 FW_STATUS_LOADING,
98 FW_STATUS_DONE,
f52cc379 99 FW_STATUS_ABORTED,
1da177e4
LT
100};
101
2f65168d 102static int loading_timeout = 60; /* In seconds */
1da177e4 103
9b78c1da
RW
104static inline long firmware_loading_timeout(void)
105{
68ff2a00 106 return loading_timeout > 0 ? loading_timeout * HZ : MAX_JIFFY_OFFSET;
9b78c1da
RW
107}
108
f52cc379
DW
109/*
110 * Concurrent request_firmware() for the same firmware need to be
111 * serialized. struct fw_state is simple state machine which hold the
112 * state of the firmware loading.
113 */
114struct fw_state {
5b029624 115 struct swait_queue_head wq;
0430cafc 116 enum fw_status status;
f52cc379
DW
117};
118
119static void fw_state_init(struct fw_state *fw_st)
120{
5b029624 121 init_swait_queue_head(&fw_st->wq);
f52cc379
DW
122 fw_st->status = FW_STATUS_UNKNOWN;
123}
124
5b029624
DW
125static inline bool __fw_state_is_done(enum fw_status status)
126{
127 return status == FW_STATUS_DONE || status == FW_STATUS_ABORTED;
128}
129
5d47ec02 130static int __fw_state_wait_common(struct fw_state *fw_st, long timeout)
f52cc379
DW
131{
132 long ret;
133
5b029624
DW
134 ret = swait_event_interruptible_timeout(fw_st->wq,
135 __fw_state_is_done(READ_ONCE(fw_st->status)),
136 timeout);
137 if (ret != 0 && fw_st->status == FW_STATUS_ABORTED)
f52cc379 138 return -ENOENT;
5d47ec02
BA
139 if (!ret)
140 return -ETIMEDOUT;
f52cc379 141
5d47ec02 142 return ret < 0 ? ret : 0;
f52cc379
DW
143}
144
145static void __fw_state_set(struct fw_state *fw_st,
146 enum fw_status status)
147{
0430cafc 148 WRITE_ONCE(fw_st->status, status);
f52cc379 149
0430cafc 150 if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED)
5b029624 151 swake_up(&fw_st->wq);
f52cc379
DW
152}
153
154#define fw_state_start(fw_st) \
155 __fw_state_set(fw_st, FW_STATUS_LOADING)
156#define fw_state_done(fw_st) \
157 __fw_state_set(fw_st, FW_STATUS_DONE)
f52cc379
DW
158#define fw_state_wait(fw_st) \
159 __fw_state_wait_common(fw_st, MAX_SCHEDULE_TIMEOUT)
160
161#ifndef CONFIG_FW_LOADER_USER_HELPER
162
163#define fw_state_is_aborted(fw_st) false
164
165#else /* CONFIG_FW_LOADER_USER_HELPER */
166
fab82cb3
DW
167static int __fw_state_check(struct fw_state *fw_st, enum fw_status status)
168{
169 return fw_st->status == status;
170}
171
f52cc379
DW
172#define fw_state_aborted(fw_st) \
173 __fw_state_set(fw_st, FW_STATUS_ABORTED)
fab82cb3
DW
174#define fw_state_is_done(fw_st) \
175 __fw_state_check(fw_st, FW_STATUS_DONE)
f52cc379
DW
176#define fw_state_is_loading(fw_st) \
177 __fw_state_check(fw_st, FW_STATUS_LOADING)
178#define fw_state_is_aborted(fw_st) \
179 __fw_state_check(fw_st, FW_STATUS_ABORTED)
180#define fw_state_wait_timeout(fw_st, timeout) \
181 __fw_state_wait_common(fw_st, timeout)
182
183#endif /* CONFIG_FW_LOADER_USER_HELPER */
184
14c4bae7
TI
185/* firmware behavior options */
186#define FW_OPT_UEVENT (1U << 0)
187#define FW_OPT_NOWAIT (1U << 1)
68aeeaaa 188#ifdef CONFIG_FW_LOADER_USER_HELPER
5a1379e8 189#define FW_OPT_USERHELPER (1U << 2)
68aeeaaa 190#else
5a1379e8
TI
191#define FW_OPT_USERHELPER 0
192#endif
193#ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK
194#define FW_OPT_FALLBACK FW_OPT_USERHELPER
195#else
196#define FW_OPT_FALLBACK 0
68aeeaaa 197#endif
c868edf4 198#define FW_OPT_NO_WARN (1U << 3)
0e742e92 199#define FW_OPT_NOCACHE (1U << 4)
14c4bae7 200
1f2b7959
ML
201struct firmware_cache {
202 /* firmware_buf instance will be added into the below list */
203 spinlock_t lock;
204 struct list_head head;
cfe016b1 205 int state;
37276a51 206
cfe016b1 207#ifdef CONFIG_PM_SLEEP
37276a51
ML
208 /*
209 * Names of firmware images which have been cached successfully
210 * will be added into the below list so that device uncache
211 * helper can trace which firmware images have been cached
212 * before.
213 */
214 spinlock_t name_lock;
215 struct list_head fw_names;
216
37276a51 217 struct delayed_work work;
07646d9c
ML
218
219 struct notifier_block pm_notify;
cfe016b1 220#endif
1f2b7959 221};
1da177e4 222
1244691c 223struct firmware_buf {
1f2b7959
ML
224 struct kref ref;
225 struct list_head list;
1f2b7959 226 struct firmware_cache *fwc;
f52cc379 227 struct fw_state fw_st;
65710cb6
ML
228 void *data;
229 size_t size;
a098ecd2 230 size_t allocated_size;
7b1269f7 231#ifdef CONFIG_FW_LOADER_USER_HELPER
cd7239fa 232 bool is_paged_buf;
af5bc11e 233 bool need_uevent;
6e03a201
DW
234 struct page **pages;
235 int nr_pages;
236 int page_array_size;
fe304143 237 struct list_head pending_list;
7b1269f7 238#endif
e0fd9b1d 239 const char *fw_id;
1244691c
ML
240};
241
37276a51
ML
242struct fw_cache_entry {
243 struct list_head list;
e0fd9b1d 244 const char *name;
37276a51
ML
245};
246
f531f05a
ML
247struct fw_name_devm {
248 unsigned long magic;
e0fd9b1d 249 const char *name;
f531f05a
ML
250};
251
1f2b7959
ML
252#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
253
ac39b3ea
ML
254#define FW_LOADER_NO_CACHE 0
255#define FW_LOADER_START_CACHE 1
256
257static int fw_cache_piggyback_on_request(const char *name);
258
1f2b7959
ML
259/* fw_lock could be moved to 'struct firmware_priv' but since it is just
260 * guarding for corner cases a global lock should be OK */
261static DEFINE_MUTEX(fw_lock);
262
81f95076
LR
263static bool __enable_firmware = false;
264
265static void enable_firmware(void)
266{
267 mutex_lock(&fw_lock);
268 __enable_firmware = true;
269 mutex_unlock(&fw_lock);
270}
271
272static void disable_firmware(void)
273{
274 mutex_lock(&fw_lock);
275 __enable_firmware = false;
276 mutex_unlock(&fw_lock);
277}
278
279/*
280 * When disabled only the built-in firmware and the firmware cache will be
281 * used to look for firmware.
282 */
283static bool firmware_enabled(void)
284{
285 bool enabled = false;
286
287 mutex_lock(&fw_lock);
288 if (__enable_firmware)
289 enabled = true;
290 mutex_unlock(&fw_lock);
291
292 return enabled;
293}
294
1f2b7959
ML
295static struct firmware_cache fw_cache;
296
297static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
a098ecd2
SB
298 struct firmware_cache *fwc,
299 void *dbuf, size_t size)
1f2b7959
ML
300{
301 struct firmware_buf *buf;
302
e0fd9b1d 303 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
1f2b7959 304 if (!buf)
e0fd9b1d
LR
305 return NULL;
306
307 buf->fw_id = kstrdup_const(fw_name, GFP_ATOMIC);
308 if (!buf->fw_id) {
309 kfree(buf);
310 return NULL;
311 }
1f2b7959
ML
312
313 kref_init(&buf->ref);
1f2b7959 314 buf->fwc = fwc;
a098ecd2
SB
315 buf->data = dbuf;
316 buf->allocated_size = size;
f52cc379 317 fw_state_init(&buf->fw_st);
fe304143
TI
318#ifdef CONFIG_FW_LOADER_USER_HELPER
319 INIT_LIST_HEAD(&buf->pending_list);
320#endif
1f2b7959
ML
321
322 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
323
324 return buf;
325}
326
2887b395
ML
327static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
328{
329 struct firmware_buf *tmp;
330 struct firmware_cache *fwc = &fw_cache;
331
332 list_for_each_entry(tmp, &fwc->head, list)
333 if (!strcmp(tmp->fw_id, fw_name))
334 return tmp;
335 return NULL;
336}
337
1f2b7959
ML
338static int fw_lookup_and_allocate_buf(const char *fw_name,
339 struct firmware_cache *fwc,
a098ecd2
SB
340 struct firmware_buf **buf, void *dbuf,
341 size_t size)
1f2b7959
ML
342{
343 struct firmware_buf *tmp;
344
345 spin_lock(&fwc->lock);
2887b395
ML
346 tmp = __fw_lookup_buf(fw_name);
347 if (tmp) {
348 kref_get(&tmp->ref);
349 spin_unlock(&fwc->lock);
350 *buf = tmp;
351 return 1;
352 }
a098ecd2 353 tmp = __allocate_fw_buf(fw_name, fwc, dbuf, size);
1f2b7959
ML
354 if (tmp)
355 list_add(&tmp->list, &fwc->head);
356 spin_unlock(&fwc->lock);
357
358 *buf = tmp;
359
360 return tmp ? 0 : -ENOMEM;
361}
362
363static void __fw_free_buf(struct kref *ref)
98233b21 364 __releases(&fwc->lock)
1f2b7959
ML
365{
366 struct firmware_buf *buf = to_fwbuf(ref);
367 struct firmware_cache *fwc = buf->fwc;
1f2b7959
ML
368
369 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
370 __func__, buf->fw_id, buf, buf->data,
371 (unsigned int)buf->size);
372
1f2b7959
ML
373 list_del(&buf->list);
374 spin_unlock(&fwc->lock);
375
7b1269f7 376#ifdef CONFIG_FW_LOADER_USER_HELPER
cd7239fa 377 if (buf->is_paged_buf) {
7b1269f7 378 int i;
746058f4
ML
379 vunmap(buf->data);
380 for (i = 0; i < buf->nr_pages; i++)
381 __free_page(buf->pages[i]);
10a3fbf1 382 vfree(buf->pages);
746058f4 383 } else
7b1269f7 384#endif
a098ecd2 385 if (!buf->allocated_size)
746058f4 386 vfree(buf->data);
e0fd9b1d 387 kfree_const(buf->fw_id);
1f2b7959
ML
388 kfree(buf);
389}
390
391static void fw_free_buf(struct firmware_buf *buf)
392{
bd9eb7fb
CL
393 struct firmware_cache *fwc = buf->fwc;
394 spin_lock(&fwc->lock);
395 if (!kref_put(&buf->ref, __fw_free_buf))
396 spin_unlock(&fwc->lock);
1f2b7959
ML
397}
398
746058f4 399/* direct firmware loading support */
27602842
ML
400static char fw_path_para[256];
401static const char * const fw_path[] = {
402 fw_path_para,
746058f4
ML
403 "/lib/firmware/updates/" UTS_RELEASE,
404 "/lib/firmware/updates",
405 "/lib/firmware/" UTS_RELEASE,
406 "/lib/firmware"
407};
408
27602842
ML
409/*
410 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
411 * from kernel command line because firmware_class is generally built in
412 * kernel instead of module.
413 */
414module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
415MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
416
a098ecd2
SB
417static int
418fw_get_filesystem_firmware(struct device *device, struct firmware_buf *buf)
746058f4 419{
e40ba6d5 420 loff_t size;
1ba4de17 421 int i, len;
3e358ac2 422 int rc = -ENOENT;
f5727b05 423 char *path;
a098ecd2
SB
424 enum kernel_read_file_id id = READING_FIRMWARE;
425 size_t msize = INT_MAX;
426
427 /* Already populated data member means we're loading into a buffer */
428 if (buf->data) {
429 id = READING_FIRMWARE_PREALLOC_BUFFER;
430 msize = buf->allocated_size;
431 }
f5727b05
LR
432
433 path = __getname();
434 if (!path)
435 return -ENOMEM;
746058f4
ML
436
437 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
27602842
ML
438 /* skip the unset customized path */
439 if (!fw_path[i][0])
440 continue;
441
1ba4de17
LR
442 len = snprintf(path, PATH_MAX, "%s/%s",
443 fw_path[i], buf->fw_id);
444 if (len >= PATH_MAX) {
445 rc = -ENAMETOOLONG;
446 break;
447 }
746058f4 448
e40ba6d5 449 buf->size = 0;
a098ecd2
SB
450 rc = kernel_read_file_from_path(path, &buf->data, &size, msize,
451 id);
4b2530d8 452 if (rc) {
8e516aa5
LR
453 if (rc == -ENOENT)
454 dev_dbg(device, "loading %s failed with error %d\n",
455 path, rc);
456 else
457 dev_warn(device, "loading %s failed with error %d\n",
458 path, rc);
4b2530d8
KC
459 continue;
460 }
e40ba6d5
MZ
461 dev_dbg(device, "direct-loading %s\n", buf->fw_id);
462 buf->size = size;
5b029624 463 fw_state_done(&buf->fw_st);
4b2530d8 464 break;
4e0c92d0 465 }
4b2530d8 466 __putname(path);
4e0c92d0 467
3e358ac2 468 return rc;
746058f4
ML
469}
470
7b1269f7
TI
471/* firmware holds the ownership of pages */
472static void firmware_free_data(const struct firmware *fw)
473{
474 /* Loaded directly? */
475 if (!fw->priv) {
476 vfree(fw->data);
477 return;
478 }
479 fw_free_buf(fw->priv);
480}
481
cd7239fa
TI
482/* store the pages buffer info firmware from buf */
483static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
484{
485 fw->priv = buf;
486#ifdef CONFIG_FW_LOADER_USER_HELPER
487 fw->pages = buf->pages;
488#endif
489 fw->size = buf->size;
490 fw->data = buf->data;
491
492 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
493 __func__, buf->fw_id, buf, buf->data,
494 (unsigned int)buf->size);
495}
496
497#ifdef CONFIG_PM_SLEEP
498static void fw_name_devm_release(struct device *dev, void *res)
499{
500 struct fw_name_devm *fwn = res;
501
502 if (fwn->magic == (unsigned long)&fw_cache)
503 pr_debug("%s: fw_name-%s devm-%p released\n",
504 __func__, fwn->name, res);
e0fd9b1d 505 kfree_const(fwn->name);
cd7239fa
TI
506}
507
508static int fw_devm_match(struct device *dev, void *res,
509 void *match_data)
510{
511 struct fw_name_devm *fwn = res;
512
513 return (fwn->magic == (unsigned long)&fw_cache) &&
514 !strcmp(fwn->name, match_data);
515}
516
517static struct fw_name_devm *fw_find_devm_name(struct device *dev,
518 const char *name)
519{
520 struct fw_name_devm *fwn;
521
522 fwn = devres_find(dev, fw_name_devm_release,
523 fw_devm_match, (void *)name);
524 return fwn;
525}
526
527/* add firmware name into devres list */
528static int fw_add_devm_name(struct device *dev, const char *name)
529{
530 struct fw_name_devm *fwn;
531
532 fwn = fw_find_devm_name(dev, name);
533 if (fwn)
534 return 1;
535
e0fd9b1d
LR
536 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
537 GFP_KERNEL);
cd7239fa
TI
538 if (!fwn)
539 return -ENOMEM;
e0fd9b1d
LR
540 fwn->name = kstrdup_const(name, GFP_KERNEL);
541 if (!fwn->name) {
a885de67 542 devres_free(fwn);
e0fd9b1d
LR
543 return -ENOMEM;
544 }
cd7239fa
TI
545
546 fwn->magic = (unsigned long)&fw_cache;
cd7239fa
TI
547 devres_add(dev, fwn);
548
549 return 0;
550}
551#else
552static int fw_add_devm_name(struct device *dev, const char *name)
553{
554 return 0;
555}
556#endif
557
558
559/*
560 * user-mode helper code
561 */
7b1269f7 562#ifdef CONFIG_FW_LOADER_USER_HELPER
cd7239fa 563struct firmware_priv {
cd7239fa
TI
564 bool nowait;
565 struct device dev;
566 struct firmware_buf *buf;
567 struct firmware *fw;
568};
7b1269f7 569
f8a4bd34
DT
570static struct firmware_priv *to_firmware_priv(struct device *dev)
571{
572 return container_of(dev, struct firmware_priv, dev);
573}
574
7068cb07 575static void __fw_load_abort(struct firmware_buf *buf)
1da177e4 576{
87597936
ML
577 /*
578 * There is a small window in which user can write to 'loading'
579 * between loading done and disappearance of 'loading'
580 */
f52cc379 581 if (fw_state_is_done(&buf->fw_st))
87597936
ML
582 return;
583
fe304143 584 list_del_init(&buf->pending_list);
f52cc379 585 fw_state_aborted(&buf->fw_st);
1da177e4
LT
586}
587
7068cb07
GKH
588static void fw_load_abort(struct firmware_priv *fw_priv)
589{
590 struct firmware_buf *buf = fw_priv->buf;
591
592 __fw_load_abort(buf);
1da177e4
LT
593}
594
fe304143
TI
595static LIST_HEAD(pending_fw_head);
596
c4b76893 597static void kill_pending_fw_fallback_reqs(bool only_kill_custom)
6383331d
LR
598{
599 struct firmware_buf *buf;
600 struct firmware_buf *next;
601
602 mutex_lock(&fw_lock);
603 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
c4b76893 604 if (!buf->need_uevent || !only_kill_custom)
6383331d
LR
605 __fw_load_abort(buf);
606 }
607 mutex_unlock(&fw_lock);
608}
6383331d 609
14adbe53
GKH
610static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
611 char *buf)
1da177e4
LT
612{
613 return sprintf(buf, "%d\n", loading_timeout);
614}
615
616/**
eb8e3179
RD
617 * firmware_timeout_store - set number of seconds to wait for firmware
618 * @class: device class pointer
e59817bf 619 * @attr: device attribute pointer
eb8e3179
RD
620 * @buf: buffer to scan for timeout value
621 * @count: number of bytes in @buf
622 *
1da177e4 623 * Sets the number of seconds to wait for the firmware. Once
eb8e3179 624 * this expires an error will be returned to the driver and no
1da177e4
LT
625 * firmware will be provided.
626 *
eb8e3179 627 * Note: zero means 'wait forever'.
1da177e4 628 **/
14adbe53
GKH
629static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
630 const char *buf, size_t count)
1da177e4
LT
631{
632 loading_timeout = simple_strtol(buf, NULL, 10);
b92eac01
SG
633 if (loading_timeout < 0)
634 loading_timeout = 0;
f8a4bd34 635
1da177e4
LT
636 return count;
637}
3f214cff 638static CLASS_ATTR_RW(timeout);
1da177e4 639
3f214cff
GKH
640static struct attribute *firmware_class_attrs[] = {
641 &class_attr_timeout.attr,
642 NULL,
673fae90 643};
3f214cff 644ATTRIBUTE_GROUPS(firmware_class);
1da177e4 645
1244691c
ML
646static void fw_dev_release(struct device *dev)
647{
648 struct firmware_priv *fw_priv = to_firmware_priv(dev);
65710cb6 649
673fae90 650 kfree(fw_priv);
673fae90 651}
1da177e4 652
6f957724 653static int do_firmware_uevent(struct firmware_priv *fw_priv, struct kobj_uevent_env *env)
1da177e4 654{
1244691c 655 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
1da177e4 656 return -ENOMEM;
7eff2e7a 657 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
6897089c 658 return -ENOMEM;
e9045f91
JB
659 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
660 return -ENOMEM;
1da177e4
LT
661
662 return 0;
663}
664
6f957724
LT
665static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
666{
667 struct firmware_priv *fw_priv = to_firmware_priv(dev);
668 int err = 0;
669
670 mutex_lock(&fw_lock);
671 if (fw_priv->buf)
672 err = do_firmware_uevent(fw_priv, env);
673 mutex_unlock(&fw_lock);
674 return err;
675}
676
1b81d663
AB
677static struct class firmware_class = {
678 .name = "firmware",
3f214cff 679 .class_groups = firmware_class_groups,
e55c8790
GKH
680 .dev_uevent = firmware_uevent,
681 .dev_release = fw_dev_release,
1b81d663
AB
682};
683
e55c8790
GKH
684static ssize_t firmware_loading_show(struct device *dev,
685 struct device_attribute *attr, char *buf)
1da177e4 686{
f8a4bd34 687 struct firmware_priv *fw_priv = to_firmware_priv(dev);
87597936
ML
688 int loading = 0;
689
690 mutex_lock(&fw_lock);
691 if (fw_priv->buf)
f52cc379 692 loading = fw_state_is_loading(&fw_priv->buf->fw_st);
87597936 693 mutex_unlock(&fw_lock);
f8a4bd34 694
1da177e4
LT
695 return sprintf(buf, "%d\n", loading);
696}
697
6e03a201
DW
698/* Some architectures don't have PAGE_KERNEL_RO */
699#ifndef PAGE_KERNEL_RO
700#define PAGE_KERNEL_RO PAGE_KERNEL
701#endif
253c9240
ML
702
703/* one pages buffer should be mapped/unmapped only once */
704static int fw_map_pages_buf(struct firmware_buf *buf)
705{
cd7239fa 706 if (!buf->is_paged_buf)
746058f4
ML
707 return 0;
708
daa3d67f 709 vunmap(buf->data);
253c9240
ML
710 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
711 if (!buf->data)
712 return -ENOMEM;
713 return 0;
714}
715
1da177e4 716/**
eb8e3179 717 * firmware_loading_store - set value in the 'loading' control file
e55c8790 718 * @dev: device pointer
af9997e4 719 * @attr: device attribute pointer
eb8e3179
RD
720 * @buf: buffer to scan for loading control value
721 * @count: number of bytes in @buf
722 *
1da177e4
LT
723 * The relevant values are:
724 *
725 * 1: Start a load, discarding any previous partial load.
eb8e3179 726 * 0: Conclude the load and hand the data to the driver code.
1da177e4
LT
727 * -1: Conclude the load with an error and discard any written data.
728 **/
e55c8790
GKH
729static ssize_t firmware_loading_store(struct device *dev,
730 struct device_attribute *attr,
731 const char *buf, size_t count)
1da177e4 732{
f8a4bd34 733 struct firmware_priv *fw_priv = to_firmware_priv(dev);
87597936 734 struct firmware_buf *fw_buf;
6593d924 735 ssize_t written = count;
1da177e4 736 int loading = simple_strtol(buf, NULL, 10);
6e03a201 737 int i;
1da177e4 738
eea915bb 739 mutex_lock(&fw_lock);
87597936 740 fw_buf = fw_priv->buf;
191e885a 741 if (fw_state_is_aborted(&fw_buf->fw_st))
eea915bb
NH
742 goto out;
743
1da177e4
LT
744 switch (loading) {
745 case 1:
65710cb6 746 /* discarding any previous partial load */
f52cc379 747 if (!fw_state_is_done(&fw_buf->fw_st)) {
1244691c
ML
748 for (i = 0; i < fw_buf->nr_pages; i++)
749 __free_page(fw_buf->pages[i]);
10a3fbf1 750 vfree(fw_buf->pages);
1244691c
ML
751 fw_buf->pages = NULL;
752 fw_buf->page_array_size = 0;
753 fw_buf->nr_pages = 0;
f52cc379 754 fw_state_start(&fw_buf->fw_st);
28eefa75 755 }
1da177e4
LT
756 break;
757 case 0:
f52cc379 758 if (fw_state_is_loading(&fw_buf->fw_st)) {
6593d924
KC
759 int rc;
760
253c9240
ML
761 /*
762 * Several loading requests may be pending on
763 * one same firmware buf, so let all requests
764 * see the mapped 'buf->data' once the loading
765 * is completed.
766 * */
6593d924
KC
767 rc = fw_map_pages_buf(fw_buf);
768 if (rc)
2b1278cb 769 dev_err(dev, "%s: map pages failed\n",
770 __func__);
6593d924 771 else
e40ba6d5
MZ
772 rc = security_kernel_post_read_file(NULL,
773 fw_buf->data, fw_buf->size,
774 READING_FIRMWARE);
6593d924
KC
775
776 /*
777 * Same logic as fw_load_abort, only the DONE bit
778 * is ignored and we set ABORT only on failure.
779 */
fe304143 780 list_del_init(&fw_buf->pending_list);
6593d924 781 if (rc) {
f52cc379 782 fw_state_aborted(&fw_buf->fw_st);
6593d924 783 written = rc;
f52cc379
DW
784 } else {
785 fw_state_done(&fw_buf->fw_st);
6593d924 786 }
1da177e4
LT
787 break;
788 }
789 /* fallthrough */
790 default:
266a813c 791 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
1da177e4
LT
792 /* fallthrough */
793 case -1:
794 fw_load_abort(fw_priv);
795 break;
796 }
eea915bb
NH
797out:
798 mutex_unlock(&fw_lock);
6593d924 799 return written;
1da177e4
LT
800}
801
e55c8790 802static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
1da177e4 803
a098ecd2
SB
804static void firmware_rw_buf(struct firmware_buf *buf, char *buffer,
805 loff_t offset, size_t count, bool read)
806{
807 if (read)
808 memcpy(buffer, buf->data + offset, count);
809 else
810 memcpy(buf->data + offset, buffer, count);
811}
812
9ccf9811
SB
813static void firmware_rw(struct firmware_buf *buf, char *buffer,
814 loff_t offset, size_t count, bool read)
815{
816 while (count) {
817 void *page_data;
818 int page_nr = offset >> PAGE_SHIFT;
819 int page_ofs = offset & (PAGE_SIZE-1);
820 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
821
822 page_data = kmap(buf->pages[page_nr]);
823
824 if (read)
825 memcpy(buffer, page_data + page_ofs, page_cnt);
826 else
827 memcpy(page_data + page_ofs, buffer, page_cnt);
828
829 kunmap(buf->pages[page_nr]);
830 buffer += page_cnt;
831 offset += page_cnt;
832 count -= page_cnt;
833 }
834}
835
f8a4bd34
DT
836static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
837 struct bin_attribute *bin_attr,
838 char *buffer, loff_t offset, size_t count)
1da177e4 839{
b0d1f807 840 struct device *dev = kobj_to_dev(kobj);
f8a4bd34 841 struct firmware_priv *fw_priv = to_firmware_priv(dev);
1244691c 842 struct firmware_buf *buf;
f37e6617 843 ssize_t ret_count;
1da177e4 844
cad1e55d 845 mutex_lock(&fw_lock);
1244691c 846 buf = fw_priv->buf;
f52cc379 847 if (!buf || fw_state_is_done(&buf->fw_st)) {
1da177e4
LT
848 ret_count = -ENODEV;
849 goto out;
850 }
1244691c 851 if (offset > buf->size) {
308975fa
JS
852 ret_count = 0;
853 goto out;
854 }
1244691c
ML
855 if (count > buf->size - offset)
856 count = buf->size - offset;
6e03a201
DW
857
858 ret_count = count;
859
a098ecd2
SB
860 if (buf->data)
861 firmware_rw_buf(buf, buffer, offset, count, true);
862 else
863 firmware_rw(buf, buffer, offset, count, true);
6e03a201 864
1da177e4 865out:
cad1e55d 866 mutex_unlock(&fw_lock);
1da177e4
LT
867 return ret_count;
868}
eb8e3179 869
f8a4bd34 870static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
1da177e4 871{
1244691c 872 struct firmware_buf *buf = fw_priv->buf;
a76040d8 873 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
6e03a201
DW
874
875 /* If the array of pages is too small, grow it... */
1244691c 876 if (buf->page_array_size < pages_needed) {
6e03a201 877 int new_array_size = max(pages_needed,
1244691c 878 buf->page_array_size * 2);
6e03a201
DW
879 struct page **new_pages;
880
10a3fbf1 881 new_pages = vmalloc(new_array_size * sizeof(void *));
6e03a201
DW
882 if (!new_pages) {
883 fw_load_abort(fw_priv);
884 return -ENOMEM;
885 }
1244691c
ML
886 memcpy(new_pages, buf->pages,
887 buf->page_array_size * sizeof(void *));
888 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
889 (new_array_size - buf->page_array_size));
10a3fbf1 890 vfree(buf->pages);
1244691c
ML
891 buf->pages = new_pages;
892 buf->page_array_size = new_array_size;
6e03a201 893 }
1da177e4 894
1244691c
ML
895 while (buf->nr_pages < pages_needed) {
896 buf->pages[buf->nr_pages] =
6e03a201 897 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
1da177e4 898
1244691c 899 if (!buf->pages[buf->nr_pages]) {
6e03a201
DW
900 fw_load_abort(fw_priv);
901 return -ENOMEM;
902 }
1244691c 903 buf->nr_pages++;
1da177e4 904 }
1da177e4
LT
905 return 0;
906}
907
908/**
eb8e3179 909 * firmware_data_write - write method for firmware
2c3c8bea 910 * @filp: open sysfs file
e55c8790 911 * @kobj: kobject for the device
42e61f4a 912 * @bin_attr: bin_attr structure
eb8e3179
RD
913 * @buffer: buffer being written
914 * @offset: buffer offset for write in total data store area
915 * @count: buffer size
1da177e4 916 *
eb8e3179 917 * Data written to the 'data' attribute will be later handed to
1da177e4
LT
918 * the driver as a firmware image.
919 **/
f8a4bd34
DT
920static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
921 struct bin_attribute *bin_attr,
922 char *buffer, loff_t offset, size_t count)
1da177e4 923{
b0d1f807 924 struct device *dev = kobj_to_dev(kobj);
f8a4bd34 925 struct firmware_priv *fw_priv = to_firmware_priv(dev);
1244691c 926 struct firmware_buf *buf;
1da177e4
LT
927 ssize_t retval;
928
929 if (!capable(CAP_SYS_RAWIO))
930 return -EPERM;
b92eac01 931
cad1e55d 932 mutex_lock(&fw_lock);
1244691c 933 buf = fw_priv->buf;
f52cc379 934 if (!buf || fw_state_is_done(&buf->fw_st)) {
1da177e4
LT
935 retval = -ENODEV;
936 goto out;
937 }
65710cb6 938
a098ecd2
SB
939 if (buf->data) {
940 if (offset + count > buf->allocated_size) {
941 retval = -ENOMEM;
942 goto out;
943 }
944 firmware_rw_buf(buf, buffer, offset, count, false);
945 retval = count;
946 } else {
947 retval = fw_realloc_buffer(fw_priv, offset + count);
948 if (retval)
949 goto out;
1da177e4 950
a098ecd2
SB
951 retval = count;
952 firmware_rw(buf, buffer, offset, count, false);
953 }
6e03a201 954
9ccf9811 955 buf->size = max_t(size_t, offset + count, buf->size);
1da177e4 956out:
cad1e55d 957 mutex_unlock(&fw_lock);
1da177e4
LT
958 return retval;
959}
eb8e3179 960
0983ca2d
DT
961static struct bin_attribute firmware_attr_data = {
962 .attr = { .name = "data", .mode = 0644 },
1da177e4
LT
963 .size = 0,
964 .read = firmware_data_read,
965 .write = firmware_data_write,
966};
967
46239902
TI
968static struct attribute *fw_dev_attrs[] = {
969 &dev_attr_loading.attr,
970 NULL
971};
972
973static struct bin_attribute *fw_dev_bin_attrs[] = {
974 &firmware_attr_data,
975 NULL
976};
977
978static const struct attribute_group fw_dev_attr_group = {
979 .attrs = fw_dev_attrs,
980 .bin_attrs = fw_dev_bin_attrs,
981};
982
983static const struct attribute_group *fw_dev_attr_groups[] = {
984 &fw_dev_attr_group,
985 NULL
986};
987
f8a4bd34 988static struct firmware_priv *
dddb5549 989fw_create_instance(struct firmware *firmware, const char *fw_name,
14c4bae7 990 struct device *device, unsigned int opt_flags)
1da177e4 991{
f8a4bd34
DT
992 struct firmware_priv *fw_priv;
993 struct device *f_dev;
1da177e4 994
1244691c 995 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
f8a4bd34 996 if (!fw_priv) {
1244691c
ML
997 fw_priv = ERR_PTR(-ENOMEM);
998 goto exit;
999 }
1000
14c4bae7 1001 fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT);
1f2b7959 1002 fw_priv->fw = firmware;
f8a4bd34
DT
1003 f_dev = &fw_priv->dev;
1004
1005 device_initialize(f_dev);
99c2aa72 1006 dev_set_name(f_dev, "%s", fw_name);
e55c8790
GKH
1007 f_dev->parent = device;
1008 f_dev->class = &firmware_class;
46239902 1009 f_dev->groups = fw_dev_attr_groups;
1244691c 1010exit:
f8a4bd34 1011 return fw_priv;
1da177e4
LT
1012}
1013
cd7239fa 1014/* load a firmware via user helper */
14c4bae7
TI
1015static int _request_firmware_load(struct firmware_priv *fw_priv,
1016 unsigned int opt_flags, long timeout)
1f2b7959 1017{
cd7239fa
TI
1018 int retval = 0;
1019 struct device *f_dev = &fw_priv->dev;
1020 struct firmware_buf *buf = fw_priv->buf;
1f2b7959 1021
cd7239fa 1022 /* fall back on userspace loading */
a098ecd2
SB
1023 if (!buf->data)
1024 buf->is_paged_buf = true;
1f2b7959 1025
cd7239fa 1026 dev_set_uevent_suppress(f_dev, true);
f531f05a 1027
cd7239fa
TI
1028 retval = device_add(f_dev);
1029 if (retval) {
1030 dev_err(f_dev, "%s: device_register failed\n", __func__);
1031 goto err_put_dev;
1032 }
f531f05a 1033
1eeeef15
MB
1034 mutex_lock(&fw_lock);
1035 list_add(&buf->pending_list, &pending_fw_head);
1036 mutex_unlock(&fw_lock);
1037
14c4bae7 1038 if (opt_flags & FW_OPT_UEVENT) {
af5bc11e 1039 buf->need_uevent = true;
cd7239fa
TI
1040 dev_set_uevent_suppress(f_dev, false);
1041 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
cd7239fa 1042 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
68ff2a00
ML
1043 } else {
1044 timeout = MAX_JIFFY_OFFSET;
cd7239fa 1045 }
f531f05a 1046
5d47ec02
BA
1047 retval = fw_state_wait_timeout(&buf->fw_st, timeout);
1048 if (retval < 0) {
0cb64249
ML
1049 mutex_lock(&fw_lock);
1050 fw_load_abort(fw_priv);
1051 mutex_unlock(&fw_lock);
1052 }
f531f05a 1053
f52cc379 1054 if (fw_state_is_aborted(&buf->fw_st))
0542ad88 1055 retval = -EAGAIN;
a098ecd2 1056 else if (buf->is_paged_buf && !buf->data)
2b1278cb 1057 retval = -ENOMEM;
f531f05a 1058
cd7239fa
TI
1059 device_del(f_dev);
1060err_put_dev:
1061 put_device(f_dev);
1062 return retval;
f531f05a 1063}
cd7239fa
TI
1064
1065static int fw_load_from_user_helper(struct firmware *firmware,
1066 const char *name, struct device *device,
14c4bae7 1067 unsigned int opt_flags, long timeout)
cfe016b1 1068{
cd7239fa
TI
1069 struct firmware_priv *fw_priv;
1070
14c4bae7 1071 fw_priv = fw_create_instance(firmware, name, device, opt_flags);
cd7239fa
TI
1072 if (IS_ERR(fw_priv))
1073 return PTR_ERR(fw_priv);
1074
1075 fw_priv->buf = firmware->priv;
14c4bae7 1076 return _request_firmware_load(fw_priv, opt_flags, timeout);
cfe016b1 1077}
ddf1f064 1078
cd7239fa
TI
1079#else /* CONFIG_FW_LOADER_USER_HELPER */
1080static inline int
1081fw_load_from_user_helper(struct firmware *firmware, const char *name,
14c4bae7 1082 struct device *device, unsigned int opt_flags,
cd7239fa
TI
1083 long timeout)
1084{
1085 return -ENOENT;
1086}
807be03c 1087
c4b76893 1088static inline void kill_pending_fw_fallback_reqs(bool only_kill_custom) { }
ddf1f064 1089
cd7239fa
TI
1090#endif /* CONFIG_FW_LOADER_USER_HELPER */
1091
4e0c92d0
TI
1092/* prepare firmware and firmware_buf structs;
1093 * return 0 if a firmware is already assigned, 1 if need to load one,
1094 * or a negative error code
1095 */
1096static int
1097_request_firmware_prepare(struct firmware **firmware_p, const char *name,
a098ecd2 1098 struct device *device, void *dbuf, size_t size)
1da177e4 1099{
1da177e4 1100 struct firmware *firmware;
1f2b7959
ML
1101 struct firmware_buf *buf;
1102 int ret;
1da177e4 1103
4aed0644 1104 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
1da177e4 1105 if (!firmware) {
266a813c
BH
1106 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1107 __func__);
4e0c92d0 1108 return -ENOMEM;
1da177e4 1109 }
1da177e4 1110
a098ecd2 1111 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
ed04630b 1112 dev_dbg(device, "using built-in %s\n", name);
4e0c92d0 1113 return 0; /* assigned */
5658c769
DW
1114 }
1115
a098ecd2 1116 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf, dbuf, size);
4e0c92d0
TI
1117
1118 /*
1119 * bind with 'buf' now to avoid warning in failure path
1120 * of requesting firmware.
1121 */
1122 firmware->priv = buf;
1123
1124 if (ret > 0) {
5b029624 1125 ret = fw_state_wait(&buf->fw_st);
4e0c92d0
TI
1126 if (!ret) {
1127 fw_set_page_data(buf, firmware);
1128 return 0; /* assigned */
1129 }
dddb5549 1130 }
811fa400 1131
4e0c92d0
TI
1132 if (ret < 0)
1133 return ret;
1134 return 1; /* need to load */
1135}
1136
e771d1aa 1137static int assign_firmware_buf(struct firmware *fw, struct device *device,
14c4bae7 1138 unsigned int opt_flags)
4e0c92d0
TI
1139{
1140 struct firmware_buf *buf = fw->priv;
1141
1f2b7959 1142 mutex_lock(&fw_lock);
f52cc379 1143 if (!buf->size || fw_state_is_aborted(&buf->fw_st)) {
4e0c92d0
TI
1144 mutex_unlock(&fw_lock);
1145 return -ENOENT;
1f2b7959 1146 }
65710cb6 1147
4e0c92d0
TI
1148 /*
1149 * add firmware name into devres list so that we can auto cache
1150 * and uncache firmware for device.
1151 *
1152 * device may has been deleted already, but the problem
1153 * should be fixed in devres or driver core.
1154 */
14c4bae7 1155 /* don't cache firmware handled without uevent */
0e742e92
VM
1156 if (device && (opt_flags & FW_OPT_UEVENT) &&
1157 !(opt_flags & FW_OPT_NOCACHE))
4e0c92d0
TI
1158 fw_add_devm_name(device, buf->fw_id);
1159
1160 /*
1161 * After caching firmware image is started, let it piggyback
1162 * on request firmware.
1163 */
0e742e92
VM
1164 if (!(opt_flags & FW_OPT_NOCACHE) &&
1165 buf->fwc->state == FW_LOADER_START_CACHE) {
4e0c92d0
TI
1166 if (fw_cache_piggyback_on_request(buf->fw_id))
1167 kref_get(&buf->ref);
1168 }
1169
1170 /* pass the pages buffer to driver at the last minute */
1171 fw_set_page_data(buf, fw);
1f2b7959 1172 mutex_unlock(&fw_lock);
4e0c92d0 1173 return 0;
65710cb6
ML
1174}
1175
4e0c92d0
TI
1176/* called from request_firmware() and request_firmware_work_func() */
1177static int
1178_request_firmware(const struct firmware **firmware_p, const char *name,
a098ecd2
SB
1179 struct device *device, void *buf, size_t size,
1180 unsigned int opt_flags)
4e0c92d0 1181{
715780ae 1182 struct firmware *fw = NULL;
4e0c92d0
TI
1183 long timeout;
1184 int ret;
1185
1186 if (!firmware_p)
1187 return -EINVAL;
1188
715780ae
BN
1189 if (!name || name[0] == '\0') {
1190 ret = -EINVAL;
1191 goto out;
1192 }
471b095d 1193
a098ecd2 1194 ret = _request_firmware_prepare(&fw, name, device, buf, size);
4e0c92d0
TI
1195 if (ret <= 0) /* error or already assigned */
1196 goto out;
1197
81f95076
LR
1198 if (!firmware_enabled()) {
1199 WARN(1, "firmware request while host is not available\n");
1200 ret = -EHOSTDOWN;
1201 goto out;
1202 }
1203
4e0c92d0
TI
1204 ret = 0;
1205 timeout = firmware_loading_timeout();
14c4bae7 1206 if (opt_flags & FW_OPT_NOWAIT) {
4e0c92d0
TI
1207 timeout = usermodehelper_read_lock_wait(timeout);
1208 if (!timeout) {
1209 dev_dbg(device, "firmware: %s loading timed out\n",
1210 name);
1211 ret = -EBUSY;
1212 goto out;
1213 }
1214 } else {
1215 ret = usermodehelper_read_trylock();
1216 if (WARN_ON(ret)) {
1217 dev_err(device, "firmware: %s will not be loaded\n",
1218 name);
1219 goto out;
1220 }
1221 }
1222
3e358ac2
NH
1223 ret = fw_get_filesystem_firmware(device, fw->priv);
1224 if (ret) {
c868edf4 1225 if (!(opt_flags & FW_OPT_NO_WARN))
bba3a87e 1226 dev_warn(device,
c868edf4
LR
1227 "Direct firmware load for %s failed with error %d\n",
1228 name, ret);
1229 if (opt_flags & FW_OPT_USERHELPER) {
bba3a87e
TI
1230 dev_warn(device, "Falling back to user helper\n");
1231 ret = fw_load_from_user_helper(fw, name, device,
14c4bae7 1232 opt_flags, timeout);
bba3a87e 1233 }
3e358ac2 1234 }
e771d1aa 1235
4e0c92d0 1236 if (!ret)
14c4bae7 1237 ret = assign_firmware_buf(fw, device, opt_flags);
4e0c92d0
TI
1238
1239 usermodehelper_read_unlock();
1240
1241 out:
1242 if (ret < 0) {
1243 release_firmware(fw);
1244 fw = NULL;
1245 }
1246
1247 *firmware_p = fw;
1248 return ret;
1249}
1250
6e3eaab0 1251/**
312c004d 1252 * request_firmware: - send firmware request and wait for it
eb8e3179
RD
1253 * @firmware_p: pointer to firmware image
1254 * @name: name of firmware file
1255 * @device: device for which firmware is being loaded
1256 *
1257 * @firmware_p will be used to return a firmware image by the name
6e3eaab0
AS
1258 * of @name for device @device.
1259 *
1260 * Should be called from user context where sleeping is allowed.
1261 *
312c004d 1262 * @name will be used as $FIRMWARE in the uevent environment and
6e3eaab0
AS
1263 * should be distinctive enough not to be confused with any other
1264 * firmware image for this or any other device.
0cfc1e1e
ML
1265 *
1266 * Caller must hold the reference count of @device.
6a927857
ML
1267 *
1268 * The function can be called safely inside device's suspend and
1269 * resume callback.
6e3eaab0
AS
1270 **/
1271int
1272request_firmware(const struct firmware **firmware_p, const char *name,
ea31003c 1273 struct device *device)
6e3eaab0 1274{
d6c8aa39
ML
1275 int ret;
1276
1277 /* Need to pin this module until return */
1278 __module_get(THIS_MODULE);
a098ecd2 1279 ret = _request_firmware(firmware_p, name, device, NULL, 0,
14c4bae7 1280 FW_OPT_UEVENT | FW_OPT_FALLBACK);
d6c8aa39
ML
1281 module_put(THIS_MODULE);
1282 return ret;
6e3eaab0 1283}
f494513f 1284EXPORT_SYMBOL(request_firmware);
6e3eaab0 1285
bba3a87e 1286/**
3c1556b2 1287 * request_firmware_direct: - load firmware directly without usermode helper
bba3a87e
TI
1288 * @firmware_p: pointer to firmware image
1289 * @name: name of firmware file
1290 * @device: device for which firmware is being loaded
1291 *
1292 * This function works pretty much like request_firmware(), but this doesn't
1293 * fall back to usermode helper even if the firmware couldn't be loaded
1294 * directly from fs. Hence it's useful for loading optional firmwares, which
1295 * aren't always present, without extra long timeouts of udev.
1296 **/
1297int request_firmware_direct(const struct firmware **firmware_p,
1298 const char *name, struct device *device)
1299{
1300 int ret;
ea31003c 1301
bba3a87e 1302 __module_get(THIS_MODULE);
a098ecd2 1303 ret = _request_firmware(firmware_p, name, device, NULL, 0,
c868edf4 1304 FW_OPT_UEVENT | FW_OPT_NO_WARN);
bba3a87e
TI
1305 module_put(THIS_MODULE);
1306 return ret;
1307}
1308EXPORT_SYMBOL_GPL(request_firmware_direct);
bba3a87e 1309
a098ecd2
SB
1310/**
1311 * request_firmware_into_buf - load firmware into a previously allocated buffer
1312 * @firmware_p: pointer to firmware image
1313 * @name: name of firmware file
1314 * @device: device for which firmware is being loaded and DMA region allocated
1315 * @buf: address of buffer to load firmware into
1316 * @size: size of buffer
1317 *
1318 * This function works pretty much like request_firmware(), but it doesn't
1319 * allocate a buffer to hold the firmware data. Instead, the firmware
1320 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
1321 * data member is pointed at @buf.
1322 *
1323 * This function doesn't cache firmware either.
1324 */
1325int
1326request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
1327 struct device *device, void *buf, size_t size)
1328{
1329 int ret;
1330
1331 __module_get(THIS_MODULE);
1332 ret = _request_firmware(firmware_p, name, device, buf, size,
1333 FW_OPT_UEVENT | FW_OPT_FALLBACK |
1334 FW_OPT_NOCACHE);
1335 module_put(THIS_MODULE);
1336 return ret;
1337}
1338EXPORT_SYMBOL(request_firmware_into_buf);
1339
1da177e4
LT
1340/**
1341 * release_firmware: - release the resource associated with a firmware image
eb8e3179 1342 * @fw: firmware resource to release
1da177e4 1343 **/
bcb9bd18 1344void release_firmware(const struct firmware *fw)
1da177e4
LT
1345{
1346 if (fw) {
bcb9bd18
DT
1347 if (!fw_is_builtin_firmware(fw))
1348 firmware_free_data(fw);
1da177e4
LT
1349 kfree(fw);
1350 }
1351}
f494513f 1352EXPORT_SYMBOL(release_firmware);
1da177e4 1353
1da177e4
LT
1354/* Async support */
1355struct firmware_work {
1356 struct work_struct work;
1357 struct module *module;
1358 const char *name;
1359 struct device *device;
1360 void *context;
1361 void (*cont)(const struct firmware *fw, void *context);
14c4bae7 1362 unsigned int opt_flags;
1da177e4
LT
1363};
1364
a36cf844 1365static void request_firmware_work_func(struct work_struct *work)
1da177e4 1366{
a36cf844 1367 struct firmware_work *fw_work;
1da177e4 1368 const struct firmware *fw;
f8a4bd34 1369
a36cf844 1370 fw_work = container_of(work, struct firmware_work, work);
811fa400 1371
a098ecd2 1372 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
14c4bae7 1373 fw_work->opt_flags);
9ebfbd45 1374 fw_work->cont(fw, fw_work->context);
4e0c92d0 1375 put_device(fw_work->device); /* taken in request_firmware_nowait() */
9ebfbd45 1376
1da177e4 1377 module_put(fw_work->module);
f9692b26 1378 kfree_const(fw_work->name);
1da177e4 1379 kfree(fw_work);
1da177e4
LT
1380}
1381
1382/**
3c31f07a 1383 * request_firmware_nowait - asynchronous version of request_firmware
eb8e3179 1384 * @module: module requesting the firmware
312c004d 1385 * @uevent: sends uevent to copy the firmware image if this flag
eb8e3179
RD
1386 * is non-zero else the firmware copy must be done manually.
1387 * @name: name of firmware file
1388 * @device: device for which firmware is being loaded
9ebfbd45 1389 * @gfp: allocation flags
eb8e3179
RD
1390 * @context: will be passed over to @cont, and
1391 * @fw may be %NULL if firmware request fails.
1392 * @cont: function will be called asynchronously when the firmware
1393 * request is over.
1da177e4 1394 *
0cfc1e1e
ML
1395 * Caller must hold the reference count of @device.
1396 *
6f21a62a
ML
1397 * Asynchronous variant of request_firmware() for user contexts:
1398 * - sleep for as small periods as possible since it may
88bcef50
SF
1399 * increase kernel boot time of built-in device drivers
1400 * requesting firmware in their ->probe() methods, if
1401 * @gfp is GFP_KERNEL.
6f21a62a
ML
1402 *
1403 * - can't sleep at all if @gfp is GFP_ATOMIC.
1da177e4
LT
1404 **/
1405int
1406request_firmware_nowait(
072fc8f0 1407 struct module *module, bool uevent,
9ebfbd45 1408 const char *name, struct device *device, gfp_t gfp, void *context,
1da177e4
LT
1409 void (*cont)(const struct firmware *fw, void *context))
1410{
f8a4bd34 1411 struct firmware_work *fw_work;
1da177e4 1412
ea31003c 1413 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
1da177e4
LT
1414 if (!fw_work)
1415 return -ENOMEM;
f8a4bd34
DT
1416
1417 fw_work->module = module;
f9692b26 1418 fw_work->name = kstrdup_const(name, gfp);
303cda0e
LR
1419 if (!fw_work->name) {
1420 kfree(fw_work);
f9692b26 1421 return -ENOMEM;
303cda0e 1422 }
f8a4bd34
DT
1423 fw_work->device = device;
1424 fw_work->context = context;
1425 fw_work->cont = cont;
14c4bae7 1426 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
5a1379e8 1427 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
f8a4bd34 1428
1da177e4 1429 if (!try_module_get(module)) {
f9692b26 1430 kfree_const(fw_work->name);
1da177e4
LT
1431 kfree(fw_work);
1432 return -EFAULT;
1433 }
1434
0cfc1e1e 1435 get_device(fw_work->device);
a36cf844
SB
1436 INIT_WORK(&fw_work->work, request_firmware_work_func);
1437 schedule_work(&fw_work->work);
1da177e4
LT
1438 return 0;
1439}
f494513f 1440EXPORT_SYMBOL(request_firmware_nowait);
1da177e4 1441
90f89081
ML
1442#ifdef CONFIG_PM_SLEEP
1443static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1444
2887b395
ML
1445/**
1446 * cache_firmware - cache one firmware image in kernel memory space
1447 * @fw_name: the firmware image name
1448 *
1449 * Cache firmware in kernel memory so that drivers can use it when
1450 * system isn't ready for them to request firmware image from userspace.
1451 * Once it returns successfully, driver can use request_firmware or its
1452 * nowait version to get the cached firmware without any interacting
1453 * with userspace
1454 *
1455 * Return 0 if the firmware image has been cached successfully
1456 * Return !0 otherwise
1457 *
1458 */
93232e46 1459static int cache_firmware(const char *fw_name)
2887b395
ML
1460{
1461 int ret;
1462 const struct firmware *fw;
1463
1464 pr_debug("%s: %s\n", __func__, fw_name);
1465
1466 ret = request_firmware(&fw, fw_name, NULL);
1467 if (!ret)
1468 kfree(fw);
1469
1470 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1471
1472 return ret;
1473}
1474
6a2c1234
ML
1475static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1476{
1477 struct firmware_buf *tmp;
1478 struct firmware_cache *fwc = &fw_cache;
1479
1480 spin_lock(&fwc->lock);
1481 tmp = __fw_lookup_buf(fw_name);
1482 spin_unlock(&fwc->lock);
1483
1484 return tmp;
1485}
1486
2887b395
ML
1487/**
1488 * uncache_firmware - remove one cached firmware image
1489 * @fw_name: the firmware image name
1490 *
1491 * Uncache one firmware image which has been cached successfully
1492 * before.
1493 *
1494 * Return 0 if the firmware cache has been removed successfully
1495 * Return !0 otherwise
1496 *
1497 */
93232e46 1498static int uncache_firmware(const char *fw_name)
2887b395
ML
1499{
1500 struct firmware_buf *buf;
1501 struct firmware fw;
1502
1503 pr_debug("%s: %s\n", __func__, fw_name);
1504
a098ecd2 1505 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
2887b395
ML
1506 return 0;
1507
1508 buf = fw_lookup_buf(fw_name);
1509 if (buf) {
1510 fw_free_buf(buf);
1511 return 0;
1512 }
1513
1514 return -EINVAL;
1515}
1516
37276a51
ML
1517static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1518{
1519 struct fw_cache_entry *fce;
1520
e0fd9b1d 1521 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
37276a51
ML
1522 if (!fce)
1523 goto exit;
1524
e0fd9b1d
LR
1525 fce->name = kstrdup_const(name, GFP_ATOMIC);
1526 if (!fce->name) {
1527 kfree(fce);
1528 fce = NULL;
1529 goto exit;
1530 }
37276a51
ML
1531exit:
1532 return fce;
1533}
1534
373304fe 1535static int __fw_entry_found(const char *name)
ac39b3ea
ML
1536{
1537 struct firmware_cache *fwc = &fw_cache;
1538 struct fw_cache_entry *fce;
ac39b3ea 1539
ac39b3ea
ML
1540 list_for_each_entry(fce, &fwc->fw_names, list) {
1541 if (!strcmp(fce->name, name))
373304fe 1542 return 1;
ac39b3ea 1543 }
373304fe
ML
1544 return 0;
1545}
1546
1547static int fw_cache_piggyback_on_request(const char *name)
1548{
1549 struct firmware_cache *fwc = &fw_cache;
1550 struct fw_cache_entry *fce;
1551 int ret = 0;
1552
1553 spin_lock(&fwc->name_lock);
1554 if (__fw_entry_found(name))
1555 goto found;
ac39b3ea
ML
1556
1557 fce = alloc_fw_cache_entry(name);
1558 if (fce) {
1559 ret = 1;
1560 list_add(&fce->list, &fwc->fw_names);
1561 pr_debug("%s: fw: %s\n", __func__, name);
1562 }
1563found:
1564 spin_unlock(&fwc->name_lock);
1565 return ret;
1566}
1567
37276a51
ML
1568static void free_fw_cache_entry(struct fw_cache_entry *fce)
1569{
e0fd9b1d 1570 kfree_const(fce->name);
37276a51
ML
1571 kfree(fce);
1572}
1573
1574static void __async_dev_cache_fw_image(void *fw_entry,
1575 async_cookie_t cookie)
1576{
1577 struct fw_cache_entry *fce = fw_entry;
1578 struct firmware_cache *fwc = &fw_cache;
1579 int ret;
1580
1581 ret = cache_firmware(fce->name);
ac39b3ea
ML
1582 if (ret) {
1583 spin_lock(&fwc->name_lock);
1584 list_del(&fce->list);
1585 spin_unlock(&fwc->name_lock);
37276a51 1586
ac39b3ea
ML
1587 free_fw_cache_entry(fce);
1588 }
37276a51
ML
1589}
1590
1591/* called with dev->devres_lock held */
1592static void dev_create_fw_entry(struct device *dev, void *res,
1593 void *data)
1594{
1595 struct fw_name_devm *fwn = res;
1596 const char *fw_name = fwn->name;
1597 struct list_head *head = data;
1598 struct fw_cache_entry *fce;
1599
1600 fce = alloc_fw_cache_entry(fw_name);
1601 if (fce)
1602 list_add(&fce->list, head);
1603}
1604
1605static int devm_name_match(struct device *dev, void *res,
1606 void *match_data)
1607{
1608 struct fw_name_devm *fwn = res;
1609 return (fwn->magic == (unsigned long)match_data);
1610}
1611
ab6dd8e5 1612static void dev_cache_fw_image(struct device *dev, void *data)
37276a51
ML
1613{
1614 LIST_HEAD(todo);
1615 struct fw_cache_entry *fce;
1616 struct fw_cache_entry *fce_next;
1617 struct firmware_cache *fwc = &fw_cache;
1618
1619 devres_for_each_res(dev, fw_name_devm_release,
1620 devm_name_match, &fw_cache,
1621 dev_create_fw_entry, &todo);
1622
1623 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1624 list_del(&fce->list);
1625
1626 spin_lock(&fwc->name_lock);
373304fe
ML
1627 /* only one cache entry for one firmware */
1628 if (!__fw_entry_found(fce->name)) {
373304fe
ML
1629 list_add(&fce->list, &fwc->fw_names);
1630 } else {
1631 free_fw_cache_entry(fce);
1632 fce = NULL;
1633 }
37276a51
ML
1634 spin_unlock(&fwc->name_lock);
1635
373304fe 1636 if (fce)
d28d3882
ML
1637 async_schedule_domain(__async_dev_cache_fw_image,
1638 (void *)fce,
1639 &fw_cache_domain);
37276a51
ML
1640 }
1641}
1642
1643static void __device_uncache_fw_images(void)
1644{
1645 struct firmware_cache *fwc = &fw_cache;
1646 struct fw_cache_entry *fce;
1647
1648 spin_lock(&fwc->name_lock);
1649 while (!list_empty(&fwc->fw_names)) {
1650 fce = list_entry(fwc->fw_names.next,
1651 struct fw_cache_entry, list);
1652 list_del(&fce->list);
1653 spin_unlock(&fwc->name_lock);
1654
1655 uncache_firmware(fce->name);
1656 free_fw_cache_entry(fce);
1657
1658 spin_lock(&fwc->name_lock);
1659 }
1660 spin_unlock(&fwc->name_lock);
1661}
1662
1663/**
1664 * device_cache_fw_images - cache devices' firmware
1665 *
1666 * If one device called request_firmware or its nowait version
1667 * successfully before, the firmware names are recored into the
1668 * device's devres link list, so device_cache_fw_images can call
1669 * cache_firmware() to cache these firmwares for the device,
1670 * then the device driver can load its firmwares easily at
1671 * time when system is not ready to complete loading firmware.
1672 */
1673static void device_cache_fw_images(void)
1674{
1675 struct firmware_cache *fwc = &fw_cache;
ffe53f6f 1676 int old_timeout;
37276a51
ML
1677 DEFINE_WAIT(wait);
1678
1679 pr_debug("%s\n", __func__);
1680
373304fe
ML
1681 /* cancel uncache work */
1682 cancel_delayed_work_sync(&fwc->work);
1683
ffe53f6f
ML
1684 /*
1685 * use small loading timeout for caching devices' firmware
1686 * because all these firmware images have been loaded
1687 * successfully at lease once, also system is ready for
1688 * completing firmware loading now. The maximum size of
1689 * firmware in current distributions is about 2M bytes,
1690 * so 10 secs should be enough.
1691 */
1692 old_timeout = loading_timeout;
1693 loading_timeout = 10;
1694
ac39b3ea
ML
1695 mutex_lock(&fw_lock);
1696 fwc->state = FW_LOADER_START_CACHE;
ab6dd8e5 1697 dpm_for_each_dev(NULL, dev_cache_fw_image);
ac39b3ea 1698 mutex_unlock(&fw_lock);
37276a51
ML
1699
1700 /* wait for completion of caching firmware for all devices */
d28d3882 1701 async_synchronize_full_domain(&fw_cache_domain);
ffe53f6f
ML
1702
1703 loading_timeout = old_timeout;
37276a51
ML
1704}
1705
1706/**
1707 * device_uncache_fw_images - uncache devices' firmware
1708 *
1709 * uncache all firmwares which have been cached successfully
1710 * by device_uncache_fw_images earlier
1711 */
1712static void device_uncache_fw_images(void)
1713{
1714 pr_debug("%s\n", __func__);
1715 __device_uncache_fw_images();
1716}
1717
1718static void device_uncache_fw_images_work(struct work_struct *work)
1719{
1720 device_uncache_fw_images();
1721}
1722
1723/**
1724 * device_uncache_fw_images_delay - uncache devices firmwares
1725 * @delay: number of milliseconds to delay uncache device firmwares
1726 *
1727 * uncache all devices's firmwares which has been cached successfully
1728 * by device_cache_fw_images after @delay milliseconds.
1729 */
1730static void device_uncache_fw_images_delay(unsigned long delay)
1731{
bce6618a
SD
1732 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1733 msecs_to_jiffies(delay));
37276a51
ML
1734}
1735
81f95076
LR
1736/**
1737 * fw_pm_notify - notifier for suspend/resume
1738 * @notify_block: unused
1739 * @mode: mode we are switching to
1740 * @unused: unused
1741 *
1742 * Used to modify the firmware_class state as we move in between states.
1743 * The firmware_class implements a firmware cache to enable device driver
1744 * to fetch firmware upon resume before the root filesystem is ready. We
1745 * disable API calls which do not use the built-in firmware or the firmware
1746 * cache when we know these calls will not work.
1747 *
1748 * The inner logic behind all this is a bit complex so it is worth summarizing
1749 * the kernel's own suspend/resume process with context and focus on how this
1750 * can impact the firmware API.
1751 *
1752 * First a review on how we go to suspend::
1753 *
1754 * pm_suspend() --> enter_state() -->
1755 * sys_sync()
1756 * suspend_prepare() -->
1757 * __pm_notifier_call_chain(PM_SUSPEND_PREPARE, ...);
1758 * suspend_freeze_processes() -->
1759 * freeze_processes() -->
1760 * __usermodehelper_set_disable_depth(UMH_DISABLED);
1761 * freeze all tasks ...
1762 * freeze_kernel_threads()
1763 * suspend_devices_and_enter() -->
1764 * dpm_suspend_start() -->
1765 * dpm_prepare()
1766 * dpm_suspend()
1767 * suspend_enter() -->
1768 * platform_suspend_prepare()
1769 * dpm_suspend_late()
1770 * freeze_enter()
1771 * syscore_suspend()
1772 *
1773 * When we resume we bail out of a loop from suspend_devices_and_enter() and
1774 * unwind back out to the caller enter_state() where we were before as follows::
1775 *
1776 * enter_state() -->
1777 * suspend_devices_and_enter() --> (bail from loop)
1778 * dpm_resume_end() -->
1779 * dpm_resume()
1780 * dpm_complete()
1781 * suspend_finish() -->
1782 * suspend_thaw_processes() -->
1783 * thaw_processes() -->
1784 * __usermodehelper_set_disable_depth(UMH_FREEZING);
1785 * thaw_workqueues();
1786 * thaw all processes ...
1787 * usermodehelper_enable();
1788 * pm_notifier_call_chain(PM_POST_SUSPEND);
1789 *
1790 * fw_pm_notify() works through pm_notifier_call_chain().
1791 */
07646d9c
ML
1792static int fw_pm_notify(struct notifier_block *notify_block,
1793 unsigned long mode, void *unused)
1794{
1795 switch (mode) {
1796 case PM_HIBERNATION_PREPARE:
1797 case PM_SUSPEND_PREPARE:
f8d5b9e9 1798 case PM_RESTORE_PREPARE:
c4b76893
LR
1799 /*
1800 * kill pending fallback requests with a custom fallback
1801 * to avoid stalling suspend.
1802 */
1803 kill_pending_fw_fallback_reqs(true);
07646d9c 1804 device_cache_fw_images();
81f95076 1805 disable_firmware();
07646d9c
ML
1806 break;
1807
1808 case PM_POST_SUSPEND:
1809 case PM_POST_HIBERNATION:
1810 case PM_POST_RESTORE:
ac39b3ea
ML
1811 /*
1812 * In case that system sleep failed and syscore_suspend is
1813 * not called.
1814 */
1815 mutex_lock(&fw_lock);
1816 fw_cache.state = FW_LOADER_NO_CACHE;
1817 mutex_unlock(&fw_lock);
81f95076 1818 enable_firmware();
ac39b3ea 1819
07646d9c
ML
1820 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1821 break;
1822 }
1823
1824 return 0;
1825}
07646d9c 1826
ac39b3ea
ML
1827/* stop caching firmware once syscore_suspend is reached */
1828static int fw_suspend(void)
1829{
1830 fw_cache.state = FW_LOADER_NO_CACHE;
1831 return 0;
1832}
1833
1834static struct syscore_ops fw_syscore_ops = {
1835 .suspend = fw_suspend,
1836};
cfe016b1
ML
1837#else
1838static int fw_cache_piggyback_on_request(const char *name)
1839{
1840 return 0;
1841}
1842#endif
ac39b3ea 1843
37276a51
ML
1844static void __init fw_cache_init(void)
1845{
1846 spin_lock_init(&fw_cache.lock);
1847 INIT_LIST_HEAD(&fw_cache.head);
cfe016b1 1848 fw_cache.state = FW_LOADER_NO_CACHE;
37276a51 1849
cfe016b1 1850#ifdef CONFIG_PM_SLEEP
37276a51
ML
1851 spin_lock_init(&fw_cache.name_lock);
1852 INIT_LIST_HEAD(&fw_cache.fw_names);
37276a51 1853
37276a51
ML
1854 INIT_DELAYED_WORK(&fw_cache.work,
1855 device_uncache_fw_images_work);
07646d9c
ML
1856
1857 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1858 register_pm_notifier(&fw_cache.pm_notify);
ac39b3ea
ML
1859
1860 register_syscore_ops(&fw_syscore_ops);
cfe016b1 1861#endif
37276a51
ML
1862}
1863
a669f04a
LR
1864static int fw_shutdown_notify(struct notifier_block *unused1,
1865 unsigned long unused2, void *unused3)
1866{
81f95076 1867 disable_firmware();
a669f04a
LR
1868 /*
1869 * Kill all pending fallback requests to avoid both stalling shutdown,
1870 * and avoid a deadlock with the usermode_lock.
1871 */
1872 kill_pending_fw_fallback_reqs(false);
1873
1874 return NOTIFY_DONE;
1875}
1876
1877static struct notifier_block fw_shutdown_nb = {
1878 .notifier_call = fw_shutdown_notify,
1879};
1880
673fae90 1881static int __init firmware_class_init(void)
1da177e4 1882{
81f95076 1883 enable_firmware();
1f2b7959 1884 fw_cache_init();
fe304143 1885 register_reboot_notifier(&fw_shutdown_nb);
a669f04a 1886#ifdef CONFIG_FW_LOADER_USER_HELPER
673fae90 1887 return class_register(&firmware_class);
7b1269f7
TI
1888#else
1889 return 0;
1890#endif
1da177e4 1891}
673fae90
DT
1892
1893static void __exit firmware_class_exit(void)
1da177e4 1894{
81f95076 1895 disable_firmware();
cfe016b1 1896#ifdef CONFIG_PM_SLEEP
ac39b3ea 1897 unregister_syscore_ops(&fw_syscore_ops);
07646d9c 1898 unregister_pm_notifier(&fw_cache.pm_notify);
cfe016b1 1899#endif
fe304143 1900 unregister_reboot_notifier(&fw_shutdown_nb);
a669f04a 1901#ifdef CONFIG_FW_LOADER_USER_HELPER
1da177e4 1902 class_unregister(&firmware_class);
7b1269f7 1903#endif
1da177e4
LT
1904}
1905
a30a6a2c 1906fs_initcall(firmware_class_init);
1da177e4 1907module_exit(firmware_class_exit);