]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/base/firmware_class.c
Merge tag 'drm-for-v4.13' of git://people.freedesktop.org/~airlied/linux
[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
8509adca
LR
558static int assign_firmware_buf(struct firmware *fw, struct device *device,
559 unsigned int opt_flags)
560{
561 struct firmware_buf *buf = fw->priv;
562
563 mutex_lock(&fw_lock);
564 if (!buf->size || fw_state_is_aborted(&buf->fw_st)) {
565 mutex_unlock(&fw_lock);
566 return -ENOENT;
567 }
568
569 /*
570 * add firmware name into devres list so that we can auto cache
571 * and uncache firmware for device.
572 *
573 * device may has been deleted already, but the problem
574 * should be fixed in devres or driver core.
575 */
576 /* don't cache firmware handled without uevent */
577 if (device && (opt_flags & FW_OPT_UEVENT) &&
578 !(opt_flags & FW_OPT_NOCACHE))
579 fw_add_devm_name(device, buf->fw_id);
580
581 /*
582 * After caching firmware image is started, let it piggyback
583 * on request firmware.
584 */
585 if (!(opt_flags & FW_OPT_NOCACHE) &&
586 buf->fwc->state == FW_LOADER_START_CACHE) {
587 if (fw_cache_piggyback_on_request(buf->fw_id))
588 kref_get(&buf->ref);
589 }
590
591 /* pass the pages buffer to driver at the last minute */
592 fw_set_page_data(buf, fw);
593 mutex_unlock(&fw_lock);
594 return 0;
595}
cd7239fa
TI
596
597/*
598 * user-mode helper code
599 */
7b1269f7 600#ifdef CONFIG_FW_LOADER_USER_HELPER
cd7239fa 601struct firmware_priv {
cd7239fa
TI
602 bool nowait;
603 struct device dev;
604 struct firmware_buf *buf;
605 struct firmware *fw;
606};
7b1269f7 607
f8a4bd34
DT
608static struct firmware_priv *to_firmware_priv(struct device *dev)
609{
610 return container_of(dev, struct firmware_priv, dev);
611}
612
7068cb07 613static void __fw_load_abort(struct firmware_buf *buf)
1da177e4 614{
87597936
ML
615 /*
616 * There is a small window in which user can write to 'loading'
617 * between loading done and disappearance of 'loading'
618 */
f52cc379 619 if (fw_state_is_done(&buf->fw_st))
87597936
ML
620 return;
621
fe304143 622 list_del_init(&buf->pending_list);
f52cc379 623 fw_state_aborted(&buf->fw_st);
1da177e4
LT
624}
625
7068cb07
GKH
626static void fw_load_abort(struct firmware_priv *fw_priv)
627{
628 struct firmware_buf *buf = fw_priv->buf;
629
630 __fw_load_abort(buf);
1da177e4
LT
631}
632
fe304143
TI
633static LIST_HEAD(pending_fw_head);
634
c4b76893 635static void kill_pending_fw_fallback_reqs(bool only_kill_custom)
6383331d
LR
636{
637 struct firmware_buf *buf;
638 struct firmware_buf *next;
639
640 mutex_lock(&fw_lock);
641 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
c4b76893 642 if (!buf->need_uevent || !only_kill_custom)
6383331d
LR
643 __fw_load_abort(buf);
644 }
645 mutex_unlock(&fw_lock);
646}
6383331d 647
14adbe53
GKH
648static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
649 char *buf)
1da177e4
LT
650{
651 return sprintf(buf, "%d\n", loading_timeout);
652}
653
654/**
eb8e3179
RD
655 * firmware_timeout_store - set number of seconds to wait for firmware
656 * @class: device class pointer
e59817bf 657 * @attr: device attribute pointer
eb8e3179
RD
658 * @buf: buffer to scan for timeout value
659 * @count: number of bytes in @buf
660 *
1da177e4 661 * Sets the number of seconds to wait for the firmware. Once
eb8e3179 662 * this expires an error will be returned to the driver and no
1da177e4
LT
663 * firmware will be provided.
664 *
eb8e3179 665 * Note: zero means 'wait forever'.
1da177e4 666 **/
14adbe53
GKH
667static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
668 const char *buf, size_t count)
1da177e4
LT
669{
670 loading_timeout = simple_strtol(buf, NULL, 10);
b92eac01
SG
671 if (loading_timeout < 0)
672 loading_timeout = 0;
f8a4bd34 673
1da177e4
LT
674 return count;
675}
3f214cff 676static CLASS_ATTR_RW(timeout);
1da177e4 677
3f214cff
GKH
678static struct attribute *firmware_class_attrs[] = {
679 &class_attr_timeout.attr,
680 NULL,
673fae90 681};
3f214cff 682ATTRIBUTE_GROUPS(firmware_class);
1da177e4 683
1244691c
ML
684static void fw_dev_release(struct device *dev)
685{
686 struct firmware_priv *fw_priv = to_firmware_priv(dev);
65710cb6 687
673fae90 688 kfree(fw_priv);
673fae90 689}
1da177e4 690
6f957724 691static int do_firmware_uevent(struct firmware_priv *fw_priv, struct kobj_uevent_env *env)
1da177e4 692{
1244691c 693 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
1da177e4 694 return -ENOMEM;
7eff2e7a 695 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
6897089c 696 return -ENOMEM;
e9045f91
JB
697 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
698 return -ENOMEM;
1da177e4
LT
699
700 return 0;
701}
702
6f957724
LT
703static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
704{
705 struct firmware_priv *fw_priv = to_firmware_priv(dev);
706 int err = 0;
707
708 mutex_lock(&fw_lock);
709 if (fw_priv->buf)
710 err = do_firmware_uevent(fw_priv, env);
711 mutex_unlock(&fw_lock);
712 return err;
713}
714
1b81d663
AB
715static struct class firmware_class = {
716 .name = "firmware",
3f214cff 717 .class_groups = firmware_class_groups,
e55c8790
GKH
718 .dev_uevent = firmware_uevent,
719 .dev_release = fw_dev_release,
1b81d663
AB
720};
721
e55c8790
GKH
722static ssize_t firmware_loading_show(struct device *dev,
723 struct device_attribute *attr, char *buf)
1da177e4 724{
f8a4bd34 725 struct firmware_priv *fw_priv = to_firmware_priv(dev);
87597936
ML
726 int loading = 0;
727
728 mutex_lock(&fw_lock);
729 if (fw_priv->buf)
f52cc379 730 loading = fw_state_is_loading(&fw_priv->buf->fw_st);
87597936 731 mutex_unlock(&fw_lock);
f8a4bd34 732
1da177e4
LT
733 return sprintf(buf, "%d\n", loading);
734}
735
6e03a201
DW
736/* Some architectures don't have PAGE_KERNEL_RO */
737#ifndef PAGE_KERNEL_RO
738#define PAGE_KERNEL_RO PAGE_KERNEL
739#endif
253c9240
ML
740
741/* one pages buffer should be mapped/unmapped only once */
742static int fw_map_pages_buf(struct firmware_buf *buf)
743{
cd7239fa 744 if (!buf->is_paged_buf)
746058f4
ML
745 return 0;
746
daa3d67f 747 vunmap(buf->data);
253c9240
ML
748 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
749 if (!buf->data)
750 return -ENOMEM;
751 return 0;
752}
753
1da177e4 754/**
eb8e3179 755 * firmware_loading_store - set value in the 'loading' control file
e55c8790 756 * @dev: device pointer
af9997e4 757 * @attr: device attribute pointer
eb8e3179
RD
758 * @buf: buffer to scan for loading control value
759 * @count: number of bytes in @buf
760 *
1da177e4
LT
761 * The relevant values are:
762 *
763 * 1: Start a load, discarding any previous partial load.
eb8e3179 764 * 0: Conclude the load and hand the data to the driver code.
1da177e4
LT
765 * -1: Conclude the load with an error and discard any written data.
766 **/
e55c8790
GKH
767static ssize_t firmware_loading_store(struct device *dev,
768 struct device_attribute *attr,
769 const char *buf, size_t count)
1da177e4 770{
f8a4bd34 771 struct firmware_priv *fw_priv = to_firmware_priv(dev);
87597936 772 struct firmware_buf *fw_buf;
6593d924 773 ssize_t written = count;
1da177e4 774 int loading = simple_strtol(buf, NULL, 10);
6e03a201 775 int i;
1da177e4 776
eea915bb 777 mutex_lock(&fw_lock);
87597936 778 fw_buf = fw_priv->buf;
191e885a 779 if (fw_state_is_aborted(&fw_buf->fw_st))
eea915bb
NH
780 goto out;
781
1da177e4
LT
782 switch (loading) {
783 case 1:
65710cb6 784 /* discarding any previous partial load */
f52cc379 785 if (!fw_state_is_done(&fw_buf->fw_st)) {
1244691c
ML
786 for (i = 0; i < fw_buf->nr_pages; i++)
787 __free_page(fw_buf->pages[i]);
10a3fbf1 788 vfree(fw_buf->pages);
1244691c
ML
789 fw_buf->pages = NULL;
790 fw_buf->page_array_size = 0;
791 fw_buf->nr_pages = 0;
f52cc379 792 fw_state_start(&fw_buf->fw_st);
28eefa75 793 }
1da177e4
LT
794 break;
795 case 0:
f52cc379 796 if (fw_state_is_loading(&fw_buf->fw_st)) {
6593d924
KC
797 int rc;
798
253c9240
ML
799 /*
800 * Several loading requests may be pending on
801 * one same firmware buf, so let all requests
802 * see the mapped 'buf->data' once the loading
803 * is completed.
804 * */
6593d924
KC
805 rc = fw_map_pages_buf(fw_buf);
806 if (rc)
2b1278cb 807 dev_err(dev, "%s: map pages failed\n",
808 __func__);
6593d924 809 else
e40ba6d5
MZ
810 rc = security_kernel_post_read_file(NULL,
811 fw_buf->data, fw_buf->size,
812 READING_FIRMWARE);
6593d924
KC
813
814 /*
815 * Same logic as fw_load_abort, only the DONE bit
816 * is ignored and we set ABORT only on failure.
817 */
fe304143 818 list_del_init(&fw_buf->pending_list);
6593d924 819 if (rc) {
f52cc379 820 fw_state_aborted(&fw_buf->fw_st);
6593d924 821 written = rc;
f52cc379
DW
822 } else {
823 fw_state_done(&fw_buf->fw_st);
6593d924 824 }
1da177e4
LT
825 break;
826 }
827 /* fallthrough */
828 default:
266a813c 829 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
1da177e4
LT
830 /* fallthrough */
831 case -1:
832 fw_load_abort(fw_priv);
833 break;
834 }
eea915bb
NH
835out:
836 mutex_unlock(&fw_lock);
6593d924 837 return written;
1da177e4
LT
838}
839
e55c8790 840static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
1da177e4 841
a098ecd2
SB
842static void firmware_rw_buf(struct firmware_buf *buf, char *buffer,
843 loff_t offset, size_t count, bool read)
844{
845 if (read)
846 memcpy(buffer, buf->data + offset, count);
847 else
848 memcpy(buf->data + offset, buffer, count);
849}
850
9ccf9811
SB
851static void firmware_rw(struct firmware_buf *buf, char *buffer,
852 loff_t offset, size_t count, bool read)
853{
854 while (count) {
855 void *page_data;
856 int page_nr = offset >> PAGE_SHIFT;
857 int page_ofs = offset & (PAGE_SIZE-1);
858 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
859
860 page_data = kmap(buf->pages[page_nr]);
861
862 if (read)
863 memcpy(buffer, page_data + page_ofs, page_cnt);
864 else
865 memcpy(page_data + page_ofs, buffer, page_cnt);
866
867 kunmap(buf->pages[page_nr]);
868 buffer += page_cnt;
869 offset += page_cnt;
870 count -= page_cnt;
871 }
872}
873
f8a4bd34
DT
874static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
875 struct bin_attribute *bin_attr,
876 char *buffer, loff_t offset, size_t count)
1da177e4 877{
b0d1f807 878 struct device *dev = kobj_to_dev(kobj);
f8a4bd34 879 struct firmware_priv *fw_priv = to_firmware_priv(dev);
1244691c 880 struct firmware_buf *buf;
f37e6617 881 ssize_t ret_count;
1da177e4 882
cad1e55d 883 mutex_lock(&fw_lock);
1244691c 884 buf = fw_priv->buf;
f52cc379 885 if (!buf || fw_state_is_done(&buf->fw_st)) {
1da177e4
LT
886 ret_count = -ENODEV;
887 goto out;
888 }
1244691c 889 if (offset > buf->size) {
308975fa
JS
890 ret_count = 0;
891 goto out;
892 }
1244691c
ML
893 if (count > buf->size - offset)
894 count = buf->size - offset;
6e03a201
DW
895
896 ret_count = count;
897
a098ecd2
SB
898 if (buf->data)
899 firmware_rw_buf(buf, buffer, offset, count, true);
900 else
901 firmware_rw(buf, buffer, offset, count, true);
6e03a201 902
1da177e4 903out:
cad1e55d 904 mutex_unlock(&fw_lock);
1da177e4
LT
905 return ret_count;
906}
eb8e3179 907
f8a4bd34 908static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
1da177e4 909{
1244691c 910 struct firmware_buf *buf = fw_priv->buf;
a76040d8 911 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
6e03a201
DW
912
913 /* If the array of pages is too small, grow it... */
1244691c 914 if (buf->page_array_size < pages_needed) {
6e03a201 915 int new_array_size = max(pages_needed,
1244691c 916 buf->page_array_size * 2);
6e03a201
DW
917 struct page **new_pages;
918
10a3fbf1 919 new_pages = vmalloc(new_array_size * sizeof(void *));
6e03a201
DW
920 if (!new_pages) {
921 fw_load_abort(fw_priv);
922 return -ENOMEM;
923 }
1244691c
ML
924 memcpy(new_pages, buf->pages,
925 buf->page_array_size * sizeof(void *));
926 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
927 (new_array_size - buf->page_array_size));
10a3fbf1 928 vfree(buf->pages);
1244691c
ML
929 buf->pages = new_pages;
930 buf->page_array_size = new_array_size;
6e03a201 931 }
1da177e4 932
1244691c
ML
933 while (buf->nr_pages < pages_needed) {
934 buf->pages[buf->nr_pages] =
6e03a201 935 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
1da177e4 936
1244691c 937 if (!buf->pages[buf->nr_pages]) {
6e03a201
DW
938 fw_load_abort(fw_priv);
939 return -ENOMEM;
940 }
1244691c 941 buf->nr_pages++;
1da177e4 942 }
1da177e4
LT
943 return 0;
944}
945
946/**
eb8e3179 947 * firmware_data_write - write method for firmware
2c3c8bea 948 * @filp: open sysfs file
e55c8790 949 * @kobj: kobject for the device
42e61f4a 950 * @bin_attr: bin_attr structure
eb8e3179
RD
951 * @buffer: buffer being written
952 * @offset: buffer offset for write in total data store area
953 * @count: buffer size
1da177e4 954 *
eb8e3179 955 * Data written to the 'data' attribute will be later handed to
1da177e4
LT
956 * the driver as a firmware image.
957 **/
f8a4bd34
DT
958static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
959 struct bin_attribute *bin_attr,
960 char *buffer, loff_t offset, size_t count)
1da177e4 961{
b0d1f807 962 struct device *dev = kobj_to_dev(kobj);
f8a4bd34 963 struct firmware_priv *fw_priv = to_firmware_priv(dev);
1244691c 964 struct firmware_buf *buf;
1da177e4
LT
965 ssize_t retval;
966
967 if (!capable(CAP_SYS_RAWIO))
968 return -EPERM;
b92eac01 969
cad1e55d 970 mutex_lock(&fw_lock);
1244691c 971 buf = fw_priv->buf;
f52cc379 972 if (!buf || fw_state_is_done(&buf->fw_st)) {
1da177e4
LT
973 retval = -ENODEV;
974 goto out;
975 }
65710cb6 976
a098ecd2
SB
977 if (buf->data) {
978 if (offset + count > buf->allocated_size) {
979 retval = -ENOMEM;
980 goto out;
981 }
982 firmware_rw_buf(buf, buffer, offset, count, false);
983 retval = count;
984 } else {
985 retval = fw_realloc_buffer(fw_priv, offset + count);
986 if (retval)
987 goto out;
1da177e4 988
a098ecd2
SB
989 retval = count;
990 firmware_rw(buf, buffer, offset, count, false);
991 }
6e03a201 992
9ccf9811 993 buf->size = max_t(size_t, offset + count, buf->size);
1da177e4 994out:
cad1e55d 995 mutex_unlock(&fw_lock);
1da177e4
LT
996 return retval;
997}
eb8e3179 998
0983ca2d
DT
999static struct bin_attribute firmware_attr_data = {
1000 .attr = { .name = "data", .mode = 0644 },
1da177e4
LT
1001 .size = 0,
1002 .read = firmware_data_read,
1003 .write = firmware_data_write,
1004};
1005
46239902
TI
1006static struct attribute *fw_dev_attrs[] = {
1007 &dev_attr_loading.attr,
1008 NULL
1009};
1010
1011static struct bin_attribute *fw_dev_bin_attrs[] = {
1012 &firmware_attr_data,
1013 NULL
1014};
1015
1016static const struct attribute_group fw_dev_attr_group = {
1017 .attrs = fw_dev_attrs,
1018 .bin_attrs = fw_dev_bin_attrs,
1019};
1020
1021static const struct attribute_group *fw_dev_attr_groups[] = {
1022 &fw_dev_attr_group,
1023 NULL
1024};
1025
f8a4bd34 1026static struct firmware_priv *
dddb5549 1027fw_create_instance(struct firmware *firmware, const char *fw_name,
14c4bae7 1028 struct device *device, unsigned int opt_flags)
1da177e4 1029{
f8a4bd34
DT
1030 struct firmware_priv *fw_priv;
1031 struct device *f_dev;
1da177e4 1032
1244691c 1033 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
f8a4bd34 1034 if (!fw_priv) {
1244691c
ML
1035 fw_priv = ERR_PTR(-ENOMEM);
1036 goto exit;
1037 }
1038
14c4bae7 1039 fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT);
1f2b7959 1040 fw_priv->fw = firmware;
f8a4bd34
DT
1041 f_dev = &fw_priv->dev;
1042
1043 device_initialize(f_dev);
99c2aa72 1044 dev_set_name(f_dev, "%s", fw_name);
e55c8790
GKH
1045 f_dev->parent = device;
1046 f_dev->class = &firmware_class;
46239902 1047 f_dev->groups = fw_dev_attr_groups;
1244691c 1048exit:
f8a4bd34 1049 return fw_priv;
1da177e4
LT
1050}
1051
cd7239fa 1052/* load a firmware via user helper */
14c4bae7
TI
1053static int _request_firmware_load(struct firmware_priv *fw_priv,
1054 unsigned int opt_flags, long timeout)
1f2b7959 1055{
cd7239fa
TI
1056 int retval = 0;
1057 struct device *f_dev = &fw_priv->dev;
1058 struct firmware_buf *buf = fw_priv->buf;
1f2b7959 1059
cd7239fa 1060 /* fall back on userspace loading */
a098ecd2
SB
1061 if (!buf->data)
1062 buf->is_paged_buf = true;
1f2b7959 1063
cd7239fa 1064 dev_set_uevent_suppress(f_dev, true);
f531f05a 1065
cd7239fa
TI
1066 retval = device_add(f_dev);
1067 if (retval) {
1068 dev_err(f_dev, "%s: device_register failed\n", __func__);
1069 goto err_put_dev;
1070 }
f531f05a 1071
1eeeef15
MB
1072 mutex_lock(&fw_lock);
1073 list_add(&buf->pending_list, &pending_fw_head);
1074 mutex_unlock(&fw_lock);
1075
14c4bae7 1076 if (opt_flags & FW_OPT_UEVENT) {
af5bc11e 1077 buf->need_uevent = true;
cd7239fa
TI
1078 dev_set_uevent_suppress(f_dev, false);
1079 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
cd7239fa 1080 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
68ff2a00
ML
1081 } else {
1082 timeout = MAX_JIFFY_OFFSET;
cd7239fa 1083 }
f531f05a 1084
5d47ec02
BA
1085 retval = fw_state_wait_timeout(&buf->fw_st, timeout);
1086 if (retval < 0) {
0cb64249
ML
1087 mutex_lock(&fw_lock);
1088 fw_load_abort(fw_priv);
1089 mutex_unlock(&fw_lock);
1090 }
f531f05a 1091
f52cc379 1092 if (fw_state_is_aborted(&buf->fw_st))
0542ad88 1093 retval = -EAGAIN;
a098ecd2 1094 else if (buf->is_paged_buf && !buf->data)
2b1278cb 1095 retval = -ENOMEM;
f531f05a 1096
cd7239fa
TI
1097 device_del(f_dev);
1098err_put_dev:
1099 put_device(f_dev);
1100 return retval;
f531f05a 1101}
cd7239fa
TI
1102
1103static int fw_load_from_user_helper(struct firmware *firmware,
1104 const char *name, struct device *device,
06a45a93 1105 unsigned int opt_flags)
cfe016b1 1106{
cd7239fa 1107 struct firmware_priv *fw_priv;
06a45a93
LR
1108 long timeout;
1109 int ret;
1110
1111 timeout = firmware_loading_timeout();
1112 if (opt_flags & FW_OPT_NOWAIT) {
1113 timeout = usermodehelper_read_lock_wait(timeout);
1114 if (!timeout) {
1115 dev_dbg(device, "firmware: %s loading timed out\n",
1116 name);
1117 return -EBUSY;
1118 }
1119 } else {
1120 ret = usermodehelper_read_trylock();
1121 if (WARN_ON(ret)) {
1122 dev_err(device, "firmware: %s will not be loaded\n",
1123 name);
1124 return ret;
1125 }
1126 }
cd7239fa 1127
14c4bae7 1128 fw_priv = fw_create_instance(firmware, name, device, opt_flags);
06a45a93
LR
1129 if (IS_ERR(fw_priv)) {
1130 ret = PTR_ERR(fw_priv);
1131 goto out_unlock;
1132 }
cd7239fa
TI
1133
1134 fw_priv->buf = firmware->priv;
06a45a93
LR
1135 ret = _request_firmware_load(fw_priv, opt_flags, timeout);
1136
1137 if (!ret)
1138 ret = assign_firmware_buf(firmware, device, opt_flags);
1139
1140out_unlock:
1141 usermodehelper_read_unlock();
1142
1143 return ret;
cfe016b1 1144}
ddf1f064 1145
cd7239fa
TI
1146#else /* CONFIG_FW_LOADER_USER_HELPER */
1147static inline int
1148fw_load_from_user_helper(struct firmware *firmware, const char *name,
06a45a93 1149 struct device *device, unsigned int opt_flags)
cd7239fa
TI
1150{
1151 return -ENOENT;
1152}
807be03c 1153
c4b76893 1154static inline void kill_pending_fw_fallback_reqs(bool only_kill_custom) { }
ddf1f064 1155
cd7239fa
TI
1156#endif /* CONFIG_FW_LOADER_USER_HELPER */
1157
4e0c92d0
TI
1158/* prepare firmware and firmware_buf structs;
1159 * return 0 if a firmware is already assigned, 1 if need to load one,
1160 * or a negative error code
1161 */
1162static int
1163_request_firmware_prepare(struct firmware **firmware_p, const char *name,
a098ecd2 1164 struct device *device, void *dbuf, size_t size)
1da177e4 1165{
1da177e4 1166 struct firmware *firmware;
1f2b7959
ML
1167 struct firmware_buf *buf;
1168 int ret;
1da177e4 1169
4aed0644 1170 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
1da177e4 1171 if (!firmware) {
266a813c
BH
1172 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1173 __func__);
4e0c92d0 1174 return -ENOMEM;
1da177e4 1175 }
1da177e4 1176
a098ecd2 1177 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
ed04630b 1178 dev_dbg(device, "using built-in %s\n", name);
4e0c92d0 1179 return 0; /* assigned */
5658c769
DW
1180 }
1181
a098ecd2 1182 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf, dbuf, size);
4e0c92d0
TI
1183
1184 /*
1185 * bind with 'buf' now to avoid warning in failure path
1186 * of requesting firmware.
1187 */
1188 firmware->priv = buf;
1189
1190 if (ret > 0) {
5b029624 1191 ret = fw_state_wait(&buf->fw_st);
4e0c92d0
TI
1192 if (!ret) {
1193 fw_set_page_data(buf, firmware);
1194 return 0; /* assigned */
1195 }
dddb5549 1196 }
811fa400 1197
4e0c92d0
TI
1198 if (ret < 0)
1199 return ret;
1200 return 1; /* need to load */
1201}
1202
4e0c92d0
TI
1203/* called from request_firmware() and request_firmware_work_func() */
1204static int
1205_request_firmware(const struct firmware **firmware_p, const char *name,
a098ecd2
SB
1206 struct device *device, void *buf, size_t size,
1207 unsigned int opt_flags)
4e0c92d0 1208{
715780ae 1209 struct firmware *fw = NULL;
4e0c92d0
TI
1210 int ret;
1211
1212 if (!firmware_p)
1213 return -EINVAL;
1214
715780ae
BN
1215 if (!name || name[0] == '\0') {
1216 ret = -EINVAL;
1217 goto out;
1218 }
471b095d 1219
a098ecd2 1220 ret = _request_firmware_prepare(&fw, name, device, buf, size);
4e0c92d0
TI
1221 if (ret <= 0) /* error or already assigned */
1222 goto out;
1223
81f95076
LR
1224 if (!firmware_enabled()) {
1225 WARN(1, "firmware request while host is not available\n");
1226 ret = -EHOSTDOWN;
1227 goto out;
1228 }
1229
3e358ac2
NH
1230 ret = fw_get_filesystem_firmware(device, fw->priv);
1231 if (ret) {
c868edf4 1232 if (!(opt_flags & FW_OPT_NO_WARN))
bba3a87e 1233 dev_warn(device,
c868edf4
LR
1234 "Direct firmware load for %s failed with error %d\n",
1235 name, ret);
1236 if (opt_flags & FW_OPT_USERHELPER) {
bba3a87e
TI
1237 dev_warn(device, "Falling back to user helper\n");
1238 ret = fw_load_from_user_helper(fw, name, device,
06a45a93 1239 opt_flags);
bba3a87e 1240 }
06a45a93 1241 } else
14c4bae7 1242 ret = assign_firmware_buf(fw, device, opt_flags);
4e0c92d0 1243
4e0c92d0
TI
1244 out:
1245 if (ret < 0) {
1246 release_firmware(fw);
1247 fw = NULL;
1248 }
1249
1250 *firmware_p = fw;
1251 return ret;
1252}
1253
6e3eaab0 1254/**
312c004d 1255 * request_firmware: - send firmware request and wait for it
eb8e3179
RD
1256 * @firmware_p: pointer to firmware image
1257 * @name: name of firmware file
1258 * @device: device for which firmware is being loaded
1259 *
1260 * @firmware_p will be used to return a firmware image by the name
6e3eaab0
AS
1261 * of @name for device @device.
1262 *
1263 * Should be called from user context where sleeping is allowed.
1264 *
312c004d 1265 * @name will be used as $FIRMWARE in the uevent environment and
6e3eaab0
AS
1266 * should be distinctive enough not to be confused with any other
1267 * firmware image for this or any other device.
0cfc1e1e
ML
1268 *
1269 * Caller must hold the reference count of @device.
6a927857
ML
1270 *
1271 * The function can be called safely inside device's suspend and
1272 * resume callback.
6e3eaab0
AS
1273 **/
1274int
1275request_firmware(const struct firmware **firmware_p, const char *name,
ea31003c 1276 struct device *device)
6e3eaab0 1277{
d6c8aa39
ML
1278 int ret;
1279
1280 /* Need to pin this module until return */
1281 __module_get(THIS_MODULE);
a098ecd2 1282 ret = _request_firmware(firmware_p, name, device, NULL, 0,
14c4bae7 1283 FW_OPT_UEVENT | FW_OPT_FALLBACK);
d6c8aa39
ML
1284 module_put(THIS_MODULE);
1285 return ret;
6e3eaab0 1286}
f494513f 1287EXPORT_SYMBOL(request_firmware);
6e3eaab0 1288
bba3a87e 1289/**
3c1556b2 1290 * request_firmware_direct: - load firmware directly without usermode helper
bba3a87e
TI
1291 * @firmware_p: pointer to firmware image
1292 * @name: name of firmware file
1293 * @device: device for which firmware is being loaded
1294 *
1295 * This function works pretty much like request_firmware(), but this doesn't
1296 * fall back to usermode helper even if the firmware couldn't be loaded
1297 * directly from fs. Hence it's useful for loading optional firmwares, which
1298 * aren't always present, without extra long timeouts of udev.
1299 **/
1300int request_firmware_direct(const struct firmware **firmware_p,
1301 const char *name, struct device *device)
1302{
1303 int ret;
ea31003c 1304
bba3a87e 1305 __module_get(THIS_MODULE);
a098ecd2 1306 ret = _request_firmware(firmware_p, name, device, NULL, 0,
c868edf4 1307 FW_OPT_UEVENT | FW_OPT_NO_WARN);
bba3a87e
TI
1308 module_put(THIS_MODULE);
1309 return ret;
1310}
1311EXPORT_SYMBOL_GPL(request_firmware_direct);
bba3a87e 1312
a098ecd2
SB
1313/**
1314 * request_firmware_into_buf - load firmware into a previously allocated buffer
1315 * @firmware_p: pointer to firmware image
1316 * @name: name of firmware file
1317 * @device: device for which firmware is being loaded and DMA region allocated
1318 * @buf: address of buffer to load firmware into
1319 * @size: size of buffer
1320 *
1321 * This function works pretty much like request_firmware(), but it doesn't
1322 * allocate a buffer to hold the firmware data. Instead, the firmware
1323 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
1324 * data member is pointed at @buf.
1325 *
1326 * This function doesn't cache firmware either.
1327 */
1328int
1329request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
1330 struct device *device, void *buf, size_t size)
1331{
1332 int ret;
1333
1334 __module_get(THIS_MODULE);
1335 ret = _request_firmware(firmware_p, name, device, buf, size,
1336 FW_OPT_UEVENT | FW_OPT_FALLBACK |
1337 FW_OPT_NOCACHE);
1338 module_put(THIS_MODULE);
1339 return ret;
1340}
1341EXPORT_SYMBOL(request_firmware_into_buf);
1342
1da177e4
LT
1343/**
1344 * release_firmware: - release the resource associated with a firmware image
eb8e3179 1345 * @fw: firmware resource to release
1da177e4 1346 **/
bcb9bd18 1347void release_firmware(const struct firmware *fw)
1da177e4
LT
1348{
1349 if (fw) {
bcb9bd18
DT
1350 if (!fw_is_builtin_firmware(fw))
1351 firmware_free_data(fw);
1da177e4
LT
1352 kfree(fw);
1353 }
1354}
f494513f 1355EXPORT_SYMBOL(release_firmware);
1da177e4 1356
1da177e4
LT
1357/* Async support */
1358struct firmware_work {
1359 struct work_struct work;
1360 struct module *module;
1361 const char *name;
1362 struct device *device;
1363 void *context;
1364 void (*cont)(const struct firmware *fw, void *context);
14c4bae7 1365 unsigned int opt_flags;
1da177e4
LT
1366};
1367
a36cf844 1368static void request_firmware_work_func(struct work_struct *work)
1da177e4 1369{
a36cf844 1370 struct firmware_work *fw_work;
1da177e4 1371 const struct firmware *fw;
f8a4bd34 1372
a36cf844 1373 fw_work = container_of(work, struct firmware_work, work);
811fa400 1374
a098ecd2 1375 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
14c4bae7 1376 fw_work->opt_flags);
9ebfbd45 1377 fw_work->cont(fw, fw_work->context);
4e0c92d0 1378 put_device(fw_work->device); /* taken in request_firmware_nowait() */
9ebfbd45 1379
1da177e4 1380 module_put(fw_work->module);
f9692b26 1381 kfree_const(fw_work->name);
1da177e4 1382 kfree(fw_work);
1da177e4
LT
1383}
1384
1385/**
3c31f07a 1386 * request_firmware_nowait - asynchronous version of request_firmware
eb8e3179 1387 * @module: module requesting the firmware
312c004d 1388 * @uevent: sends uevent to copy the firmware image if this flag
eb8e3179
RD
1389 * is non-zero else the firmware copy must be done manually.
1390 * @name: name of firmware file
1391 * @device: device for which firmware is being loaded
9ebfbd45 1392 * @gfp: allocation flags
eb8e3179
RD
1393 * @context: will be passed over to @cont, and
1394 * @fw may be %NULL if firmware request fails.
1395 * @cont: function will be called asynchronously when the firmware
1396 * request is over.
1da177e4 1397 *
0cfc1e1e
ML
1398 * Caller must hold the reference count of @device.
1399 *
6f21a62a
ML
1400 * Asynchronous variant of request_firmware() for user contexts:
1401 * - sleep for as small periods as possible since it may
88bcef50
SF
1402 * increase kernel boot time of built-in device drivers
1403 * requesting firmware in their ->probe() methods, if
1404 * @gfp is GFP_KERNEL.
6f21a62a
ML
1405 *
1406 * - can't sleep at all if @gfp is GFP_ATOMIC.
1da177e4
LT
1407 **/
1408int
1409request_firmware_nowait(
072fc8f0 1410 struct module *module, bool uevent,
9ebfbd45 1411 const char *name, struct device *device, gfp_t gfp, void *context,
1da177e4
LT
1412 void (*cont)(const struct firmware *fw, void *context))
1413{
f8a4bd34 1414 struct firmware_work *fw_work;
1da177e4 1415
ea31003c 1416 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
1da177e4
LT
1417 if (!fw_work)
1418 return -ENOMEM;
f8a4bd34
DT
1419
1420 fw_work->module = module;
f9692b26 1421 fw_work->name = kstrdup_const(name, gfp);
303cda0e
LR
1422 if (!fw_work->name) {
1423 kfree(fw_work);
f9692b26 1424 return -ENOMEM;
303cda0e 1425 }
f8a4bd34
DT
1426 fw_work->device = device;
1427 fw_work->context = context;
1428 fw_work->cont = cont;
14c4bae7 1429 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
5a1379e8 1430 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
f8a4bd34 1431
1da177e4 1432 if (!try_module_get(module)) {
f9692b26 1433 kfree_const(fw_work->name);
1da177e4
LT
1434 kfree(fw_work);
1435 return -EFAULT;
1436 }
1437
0cfc1e1e 1438 get_device(fw_work->device);
a36cf844
SB
1439 INIT_WORK(&fw_work->work, request_firmware_work_func);
1440 schedule_work(&fw_work->work);
1da177e4
LT
1441 return 0;
1442}
f494513f 1443EXPORT_SYMBOL(request_firmware_nowait);
1da177e4 1444
90f89081
ML
1445#ifdef CONFIG_PM_SLEEP
1446static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1447
2887b395
ML
1448/**
1449 * cache_firmware - cache one firmware image in kernel memory space
1450 * @fw_name: the firmware image name
1451 *
1452 * Cache firmware in kernel memory so that drivers can use it when
1453 * system isn't ready for them to request firmware image from userspace.
1454 * Once it returns successfully, driver can use request_firmware or its
1455 * nowait version to get the cached firmware without any interacting
1456 * with userspace
1457 *
1458 * Return 0 if the firmware image has been cached successfully
1459 * Return !0 otherwise
1460 *
1461 */
93232e46 1462static int cache_firmware(const char *fw_name)
2887b395
ML
1463{
1464 int ret;
1465 const struct firmware *fw;
1466
1467 pr_debug("%s: %s\n", __func__, fw_name);
1468
1469 ret = request_firmware(&fw, fw_name, NULL);
1470 if (!ret)
1471 kfree(fw);
1472
1473 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1474
1475 return ret;
1476}
1477
6a2c1234
ML
1478static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1479{
1480 struct firmware_buf *tmp;
1481 struct firmware_cache *fwc = &fw_cache;
1482
1483 spin_lock(&fwc->lock);
1484 tmp = __fw_lookup_buf(fw_name);
1485 spin_unlock(&fwc->lock);
1486
1487 return tmp;
1488}
1489
2887b395
ML
1490/**
1491 * uncache_firmware - remove one cached firmware image
1492 * @fw_name: the firmware image name
1493 *
1494 * Uncache one firmware image which has been cached successfully
1495 * before.
1496 *
1497 * Return 0 if the firmware cache has been removed successfully
1498 * Return !0 otherwise
1499 *
1500 */
93232e46 1501static int uncache_firmware(const char *fw_name)
2887b395
ML
1502{
1503 struct firmware_buf *buf;
1504 struct firmware fw;
1505
1506 pr_debug("%s: %s\n", __func__, fw_name);
1507
a098ecd2 1508 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
2887b395
ML
1509 return 0;
1510
1511 buf = fw_lookup_buf(fw_name);
1512 if (buf) {
1513 fw_free_buf(buf);
1514 return 0;
1515 }
1516
1517 return -EINVAL;
1518}
1519
37276a51
ML
1520static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1521{
1522 struct fw_cache_entry *fce;
1523
e0fd9b1d 1524 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
37276a51
ML
1525 if (!fce)
1526 goto exit;
1527
e0fd9b1d
LR
1528 fce->name = kstrdup_const(name, GFP_ATOMIC);
1529 if (!fce->name) {
1530 kfree(fce);
1531 fce = NULL;
1532 goto exit;
1533 }
37276a51
ML
1534exit:
1535 return fce;
1536}
1537
373304fe 1538static int __fw_entry_found(const char *name)
ac39b3ea
ML
1539{
1540 struct firmware_cache *fwc = &fw_cache;
1541 struct fw_cache_entry *fce;
ac39b3ea 1542
ac39b3ea
ML
1543 list_for_each_entry(fce, &fwc->fw_names, list) {
1544 if (!strcmp(fce->name, name))
373304fe 1545 return 1;
ac39b3ea 1546 }
373304fe
ML
1547 return 0;
1548}
1549
1550static int fw_cache_piggyback_on_request(const char *name)
1551{
1552 struct firmware_cache *fwc = &fw_cache;
1553 struct fw_cache_entry *fce;
1554 int ret = 0;
1555
1556 spin_lock(&fwc->name_lock);
1557 if (__fw_entry_found(name))
1558 goto found;
ac39b3ea
ML
1559
1560 fce = alloc_fw_cache_entry(name);
1561 if (fce) {
1562 ret = 1;
1563 list_add(&fce->list, &fwc->fw_names);
1564 pr_debug("%s: fw: %s\n", __func__, name);
1565 }
1566found:
1567 spin_unlock(&fwc->name_lock);
1568 return ret;
1569}
1570
37276a51
ML
1571static void free_fw_cache_entry(struct fw_cache_entry *fce)
1572{
e0fd9b1d 1573 kfree_const(fce->name);
37276a51
ML
1574 kfree(fce);
1575}
1576
1577static void __async_dev_cache_fw_image(void *fw_entry,
1578 async_cookie_t cookie)
1579{
1580 struct fw_cache_entry *fce = fw_entry;
1581 struct firmware_cache *fwc = &fw_cache;
1582 int ret;
1583
1584 ret = cache_firmware(fce->name);
ac39b3ea
ML
1585 if (ret) {
1586 spin_lock(&fwc->name_lock);
1587 list_del(&fce->list);
1588 spin_unlock(&fwc->name_lock);
37276a51 1589
ac39b3ea
ML
1590 free_fw_cache_entry(fce);
1591 }
37276a51
ML
1592}
1593
1594/* called with dev->devres_lock held */
1595static void dev_create_fw_entry(struct device *dev, void *res,
1596 void *data)
1597{
1598 struct fw_name_devm *fwn = res;
1599 const char *fw_name = fwn->name;
1600 struct list_head *head = data;
1601 struct fw_cache_entry *fce;
1602
1603 fce = alloc_fw_cache_entry(fw_name);
1604 if (fce)
1605 list_add(&fce->list, head);
1606}
1607
1608static int devm_name_match(struct device *dev, void *res,
1609 void *match_data)
1610{
1611 struct fw_name_devm *fwn = res;
1612 return (fwn->magic == (unsigned long)match_data);
1613}
1614
ab6dd8e5 1615static void dev_cache_fw_image(struct device *dev, void *data)
37276a51
ML
1616{
1617 LIST_HEAD(todo);
1618 struct fw_cache_entry *fce;
1619 struct fw_cache_entry *fce_next;
1620 struct firmware_cache *fwc = &fw_cache;
1621
1622 devres_for_each_res(dev, fw_name_devm_release,
1623 devm_name_match, &fw_cache,
1624 dev_create_fw_entry, &todo);
1625
1626 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1627 list_del(&fce->list);
1628
1629 spin_lock(&fwc->name_lock);
373304fe
ML
1630 /* only one cache entry for one firmware */
1631 if (!__fw_entry_found(fce->name)) {
373304fe
ML
1632 list_add(&fce->list, &fwc->fw_names);
1633 } else {
1634 free_fw_cache_entry(fce);
1635 fce = NULL;
1636 }
37276a51
ML
1637 spin_unlock(&fwc->name_lock);
1638
373304fe 1639 if (fce)
d28d3882
ML
1640 async_schedule_domain(__async_dev_cache_fw_image,
1641 (void *)fce,
1642 &fw_cache_domain);
37276a51
ML
1643 }
1644}
1645
1646static void __device_uncache_fw_images(void)
1647{
1648 struct firmware_cache *fwc = &fw_cache;
1649 struct fw_cache_entry *fce;
1650
1651 spin_lock(&fwc->name_lock);
1652 while (!list_empty(&fwc->fw_names)) {
1653 fce = list_entry(fwc->fw_names.next,
1654 struct fw_cache_entry, list);
1655 list_del(&fce->list);
1656 spin_unlock(&fwc->name_lock);
1657
1658 uncache_firmware(fce->name);
1659 free_fw_cache_entry(fce);
1660
1661 spin_lock(&fwc->name_lock);
1662 }
1663 spin_unlock(&fwc->name_lock);
1664}
1665
1666/**
1667 * device_cache_fw_images - cache devices' firmware
1668 *
1669 * If one device called request_firmware or its nowait version
1670 * successfully before, the firmware names are recored into the
1671 * device's devres link list, so device_cache_fw_images can call
1672 * cache_firmware() to cache these firmwares for the device,
1673 * then the device driver can load its firmwares easily at
1674 * time when system is not ready to complete loading firmware.
1675 */
1676static void device_cache_fw_images(void)
1677{
1678 struct firmware_cache *fwc = &fw_cache;
ffe53f6f 1679 int old_timeout;
37276a51
ML
1680 DEFINE_WAIT(wait);
1681
1682 pr_debug("%s\n", __func__);
1683
373304fe
ML
1684 /* cancel uncache work */
1685 cancel_delayed_work_sync(&fwc->work);
1686
ffe53f6f
ML
1687 /*
1688 * use small loading timeout for caching devices' firmware
1689 * because all these firmware images have been loaded
1690 * successfully at lease once, also system is ready for
1691 * completing firmware loading now. The maximum size of
1692 * firmware in current distributions is about 2M bytes,
1693 * so 10 secs should be enough.
1694 */
1695 old_timeout = loading_timeout;
1696 loading_timeout = 10;
1697
ac39b3ea
ML
1698 mutex_lock(&fw_lock);
1699 fwc->state = FW_LOADER_START_CACHE;
ab6dd8e5 1700 dpm_for_each_dev(NULL, dev_cache_fw_image);
ac39b3ea 1701 mutex_unlock(&fw_lock);
37276a51
ML
1702
1703 /* wait for completion of caching firmware for all devices */
d28d3882 1704 async_synchronize_full_domain(&fw_cache_domain);
ffe53f6f
ML
1705
1706 loading_timeout = old_timeout;
37276a51
ML
1707}
1708
1709/**
1710 * device_uncache_fw_images - uncache devices' firmware
1711 *
1712 * uncache all firmwares which have been cached successfully
1713 * by device_uncache_fw_images earlier
1714 */
1715static void device_uncache_fw_images(void)
1716{
1717 pr_debug("%s\n", __func__);
1718 __device_uncache_fw_images();
1719}
1720
1721static void device_uncache_fw_images_work(struct work_struct *work)
1722{
1723 device_uncache_fw_images();
1724}
1725
1726/**
1727 * device_uncache_fw_images_delay - uncache devices firmwares
1728 * @delay: number of milliseconds to delay uncache device firmwares
1729 *
1730 * uncache all devices's firmwares which has been cached successfully
1731 * by device_cache_fw_images after @delay milliseconds.
1732 */
1733static void device_uncache_fw_images_delay(unsigned long delay)
1734{
bce6618a
SD
1735 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1736 msecs_to_jiffies(delay));
37276a51
ML
1737}
1738
81f95076
LR
1739/**
1740 * fw_pm_notify - notifier for suspend/resume
1741 * @notify_block: unused
1742 * @mode: mode we are switching to
1743 * @unused: unused
1744 *
1745 * Used to modify the firmware_class state as we move in between states.
1746 * The firmware_class implements a firmware cache to enable device driver
1747 * to fetch firmware upon resume before the root filesystem is ready. We
1748 * disable API calls which do not use the built-in firmware or the firmware
1749 * cache when we know these calls will not work.
1750 *
1751 * The inner logic behind all this is a bit complex so it is worth summarizing
1752 * the kernel's own suspend/resume process with context and focus on how this
1753 * can impact the firmware API.
1754 *
1755 * First a review on how we go to suspend::
1756 *
1757 * pm_suspend() --> enter_state() -->
1758 * sys_sync()
1759 * suspend_prepare() -->
1760 * __pm_notifier_call_chain(PM_SUSPEND_PREPARE, ...);
1761 * suspend_freeze_processes() -->
1762 * freeze_processes() -->
1763 * __usermodehelper_set_disable_depth(UMH_DISABLED);
1764 * freeze all tasks ...
1765 * freeze_kernel_threads()
1766 * suspend_devices_and_enter() -->
1767 * dpm_suspend_start() -->
1768 * dpm_prepare()
1769 * dpm_suspend()
1770 * suspend_enter() -->
1771 * platform_suspend_prepare()
1772 * dpm_suspend_late()
1773 * freeze_enter()
1774 * syscore_suspend()
1775 *
1776 * When we resume we bail out of a loop from suspend_devices_and_enter() and
1777 * unwind back out to the caller enter_state() where we were before as follows::
1778 *
1779 * enter_state() -->
1780 * suspend_devices_and_enter() --> (bail from loop)
1781 * dpm_resume_end() -->
1782 * dpm_resume()
1783 * dpm_complete()
1784 * suspend_finish() -->
1785 * suspend_thaw_processes() -->
1786 * thaw_processes() -->
1787 * __usermodehelper_set_disable_depth(UMH_FREEZING);
1788 * thaw_workqueues();
1789 * thaw all processes ...
1790 * usermodehelper_enable();
1791 * pm_notifier_call_chain(PM_POST_SUSPEND);
1792 *
1793 * fw_pm_notify() works through pm_notifier_call_chain().
1794 */
07646d9c
ML
1795static int fw_pm_notify(struct notifier_block *notify_block,
1796 unsigned long mode, void *unused)
1797{
1798 switch (mode) {
1799 case PM_HIBERNATION_PREPARE:
1800 case PM_SUSPEND_PREPARE:
f8d5b9e9 1801 case PM_RESTORE_PREPARE:
c4b76893
LR
1802 /*
1803 * kill pending fallback requests with a custom fallback
1804 * to avoid stalling suspend.
1805 */
1806 kill_pending_fw_fallback_reqs(true);
07646d9c 1807 device_cache_fw_images();
81f95076 1808 disable_firmware();
07646d9c
ML
1809 break;
1810
1811 case PM_POST_SUSPEND:
1812 case PM_POST_HIBERNATION:
1813 case PM_POST_RESTORE:
ac39b3ea
ML
1814 /*
1815 * In case that system sleep failed and syscore_suspend is
1816 * not called.
1817 */
1818 mutex_lock(&fw_lock);
1819 fw_cache.state = FW_LOADER_NO_CACHE;
1820 mutex_unlock(&fw_lock);
81f95076 1821 enable_firmware();
ac39b3ea 1822
07646d9c
ML
1823 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1824 break;
1825 }
1826
1827 return 0;
1828}
07646d9c 1829
ac39b3ea
ML
1830/* stop caching firmware once syscore_suspend is reached */
1831static int fw_suspend(void)
1832{
1833 fw_cache.state = FW_LOADER_NO_CACHE;
1834 return 0;
1835}
1836
1837static struct syscore_ops fw_syscore_ops = {
1838 .suspend = fw_suspend,
1839};
cfe016b1
ML
1840#else
1841static int fw_cache_piggyback_on_request(const char *name)
1842{
1843 return 0;
1844}
1845#endif
ac39b3ea 1846
37276a51
ML
1847static void __init fw_cache_init(void)
1848{
1849 spin_lock_init(&fw_cache.lock);
1850 INIT_LIST_HEAD(&fw_cache.head);
cfe016b1 1851 fw_cache.state = FW_LOADER_NO_CACHE;
37276a51 1852
cfe016b1 1853#ifdef CONFIG_PM_SLEEP
37276a51
ML
1854 spin_lock_init(&fw_cache.name_lock);
1855 INIT_LIST_HEAD(&fw_cache.fw_names);
37276a51 1856
37276a51
ML
1857 INIT_DELAYED_WORK(&fw_cache.work,
1858 device_uncache_fw_images_work);
07646d9c
ML
1859
1860 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1861 register_pm_notifier(&fw_cache.pm_notify);
ac39b3ea
ML
1862
1863 register_syscore_ops(&fw_syscore_ops);
cfe016b1 1864#endif
37276a51
ML
1865}
1866
a669f04a
LR
1867static int fw_shutdown_notify(struct notifier_block *unused1,
1868 unsigned long unused2, void *unused3)
1869{
81f95076 1870 disable_firmware();
a669f04a
LR
1871 /*
1872 * Kill all pending fallback requests to avoid both stalling shutdown,
1873 * and avoid a deadlock with the usermode_lock.
1874 */
1875 kill_pending_fw_fallback_reqs(false);
1876
1877 return NOTIFY_DONE;
1878}
1879
1880static struct notifier_block fw_shutdown_nb = {
1881 .notifier_call = fw_shutdown_notify,
1882};
1883
673fae90 1884static int __init firmware_class_init(void)
1da177e4 1885{
81f95076 1886 enable_firmware();
1f2b7959 1887 fw_cache_init();
fe304143 1888 register_reboot_notifier(&fw_shutdown_nb);
a669f04a 1889#ifdef CONFIG_FW_LOADER_USER_HELPER
673fae90 1890 return class_register(&firmware_class);
7b1269f7
TI
1891#else
1892 return 0;
1893#endif
1da177e4 1894}
673fae90
DT
1895
1896static void __exit firmware_class_exit(void)
1da177e4 1897{
81f95076 1898 disable_firmware();
cfe016b1 1899#ifdef CONFIG_PM_SLEEP
ac39b3ea 1900 unregister_syscore_ops(&fw_syscore_ops);
07646d9c 1901 unregister_pm_notifier(&fw_cache.pm_notify);
cfe016b1 1902#endif
fe304143 1903 unregister_reboot_notifier(&fw_shutdown_nb);
a669f04a 1904#ifdef CONFIG_FW_LOADER_USER_HELPER
1da177e4 1905 class_unregister(&firmware_class);
7b1269f7 1906#endif
1da177e4
LT
1907}
1908
a30a6a2c 1909fs_initcall(firmware_class_init);
1da177e4 1910module_exit(firmware_class_exit);