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