fs/kernel_read_file: Add file_size output argument
[platform/kernel/linux-starfive.git] / drivers / base / firmware_loader / main.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * main.c - Multi purpose firmware loading support
4  *
5  * Copyright (c) 2003 Manuel Estrada Sainz
6  *
7  * Please see Documentation/driver-api/firmware/ for more information.
8  *
9  */
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #include <linux/capability.h>
14 #include <linux/device.h>
15 #include <linux/kernel_read_file.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/timer.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/bitops.h>
22 #include <linux/mutex.h>
23 #include <linux/workqueue.h>
24 #include <linux/highmem.h>
25 #include <linux/firmware.h>
26 #include <linux/slab.h>
27 #include <linux/sched.h>
28 #include <linux/file.h>
29 #include <linux/list.h>
30 #include <linux/fs.h>
31 #include <linux/async.h>
32 #include <linux/pm.h>
33 #include <linux/suspend.h>
34 #include <linux/syscore_ops.h>
35 #include <linux/reboot.h>
36 #include <linux/security.h>
37 #include <linux/xz.h>
38
39 #include <generated/utsrelease.h>
40
41 #include "../base.h"
42 #include "firmware.h"
43 #include "fallback.h"
44
45 MODULE_AUTHOR("Manuel Estrada Sainz");
46 MODULE_DESCRIPTION("Multi purpose firmware loading support");
47 MODULE_LICENSE("GPL");
48
49 struct firmware_cache {
50         /* firmware_buf instance will be added into the below list */
51         spinlock_t lock;
52         struct list_head head;
53         int state;
54
55 #ifdef CONFIG_FW_CACHE
56         /*
57          * Names of firmware images which have been cached successfully
58          * will be added into the below list so that device uncache
59          * helper can trace which firmware images have been cached
60          * before.
61          */
62         spinlock_t name_lock;
63         struct list_head fw_names;
64
65         struct delayed_work work;
66
67         struct notifier_block   pm_notify;
68 #endif
69 };
70
71 struct fw_cache_entry {
72         struct list_head list;
73         const char *name;
74 };
75
76 struct fw_name_devm {
77         unsigned long magic;
78         const char *name;
79 };
80
81 static inline struct fw_priv *to_fw_priv(struct kref *ref)
82 {
83         return container_of(ref, struct fw_priv, ref);
84 }
85
86 #define FW_LOADER_NO_CACHE      0
87 #define FW_LOADER_START_CACHE   1
88
89 /* fw_lock could be moved to 'struct fw_sysfs' but since it is just
90  * guarding for corner cases a global lock should be OK */
91 DEFINE_MUTEX(fw_lock);
92
93 static struct firmware_cache fw_cache;
94
95 /* Builtin firmware support */
96
97 #ifdef CONFIG_FW_LOADER
98
99 extern struct builtin_fw __start_builtin_fw[];
100 extern struct builtin_fw __end_builtin_fw[];
101
102 static void fw_copy_to_prealloc_buf(struct firmware *fw,
103                                     void *buf, size_t size)
104 {
105         if (!buf || size < fw->size)
106                 return;
107         memcpy(buf, fw->data, fw->size);
108 }
109
110 static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
111                                     void *buf, size_t size)
112 {
113         struct builtin_fw *b_fw;
114
115         for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
116                 if (strcmp(name, b_fw->name) == 0) {
117                         fw->size = b_fw->size;
118                         fw->data = b_fw->data;
119                         fw_copy_to_prealloc_buf(fw, buf, size);
120
121                         return true;
122                 }
123         }
124
125         return false;
126 }
127
128 static bool fw_is_builtin_firmware(const struct firmware *fw)
129 {
130         struct builtin_fw *b_fw;
131
132         for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
133                 if (fw->data == b_fw->data)
134                         return true;
135
136         return false;
137 }
138
139 #else /* Module case - no builtin firmware support */
140
141 static inline bool fw_get_builtin_firmware(struct firmware *fw,
142                                            const char *name, void *buf,
143                                            size_t size)
144 {
145         return false;
146 }
147
148 static inline bool fw_is_builtin_firmware(const struct firmware *fw)
149 {
150         return false;
151 }
152 #endif
153
154 static void fw_state_init(struct fw_priv *fw_priv)
155 {
156         struct fw_state *fw_st = &fw_priv->fw_st;
157
158         init_completion(&fw_st->completion);
159         fw_st->status = FW_STATUS_UNKNOWN;
160 }
161
162 static inline int fw_state_wait(struct fw_priv *fw_priv)
163 {
164         return __fw_state_wait_common(fw_priv, MAX_SCHEDULE_TIMEOUT);
165 }
166
167 static int fw_cache_piggyback_on_request(const char *name);
168
169 static struct fw_priv *__allocate_fw_priv(const char *fw_name,
170                                           struct firmware_cache *fwc,
171                                           void *dbuf, size_t size)
172 {
173         struct fw_priv *fw_priv;
174
175         fw_priv = kzalloc(sizeof(*fw_priv), GFP_ATOMIC);
176         if (!fw_priv)
177                 return NULL;
178
179         fw_priv->fw_name = kstrdup_const(fw_name, GFP_ATOMIC);
180         if (!fw_priv->fw_name) {
181                 kfree(fw_priv);
182                 return NULL;
183         }
184
185         kref_init(&fw_priv->ref);
186         fw_priv->fwc = fwc;
187         fw_priv->data = dbuf;
188         fw_priv->allocated_size = size;
189         fw_state_init(fw_priv);
190 #ifdef CONFIG_FW_LOADER_USER_HELPER
191         INIT_LIST_HEAD(&fw_priv->pending_list);
192 #endif
193
194         pr_debug("%s: fw-%s fw_priv=%p\n", __func__, fw_name, fw_priv);
195
196         return fw_priv;
197 }
198
199 static struct fw_priv *__lookup_fw_priv(const char *fw_name)
200 {
201         struct fw_priv *tmp;
202         struct firmware_cache *fwc = &fw_cache;
203
204         list_for_each_entry(tmp, &fwc->head, list)
205                 if (!strcmp(tmp->fw_name, fw_name))
206                         return tmp;
207         return NULL;
208 }
209
210 /* Returns 1 for batching firmware requests with the same name */
211 static int alloc_lookup_fw_priv(const char *fw_name,
212                                 struct firmware_cache *fwc,
213                                 struct fw_priv **fw_priv, void *dbuf,
214                                 size_t size, u32 opt_flags)
215 {
216         struct fw_priv *tmp;
217
218         spin_lock(&fwc->lock);
219         if (!(opt_flags & FW_OPT_NOCACHE)) {
220                 tmp = __lookup_fw_priv(fw_name);
221                 if (tmp) {
222                         kref_get(&tmp->ref);
223                         spin_unlock(&fwc->lock);
224                         *fw_priv = tmp;
225                         pr_debug("batched request - sharing the same struct fw_priv and lookup for multiple requests\n");
226                         return 1;
227                 }
228         }
229
230         tmp = __allocate_fw_priv(fw_name, fwc, dbuf, size);
231         if (tmp) {
232                 INIT_LIST_HEAD(&tmp->list);
233                 if (!(opt_flags & FW_OPT_NOCACHE))
234                         list_add(&tmp->list, &fwc->head);
235         }
236         spin_unlock(&fwc->lock);
237
238         *fw_priv = tmp;
239
240         return tmp ? 0 : -ENOMEM;
241 }
242
243 static void __free_fw_priv(struct kref *ref)
244         __releases(&fwc->lock)
245 {
246         struct fw_priv *fw_priv = to_fw_priv(ref);
247         struct firmware_cache *fwc = fw_priv->fwc;
248
249         pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
250                  __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
251                  (unsigned int)fw_priv->size);
252
253         list_del(&fw_priv->list);
254         spin_unlock(&fwc->lock);
255
256         if (fw_is_paged_buf(fw_priv))
257                 fw_free_paged_buf(fw_priv);
258         else if (!fw_priv->allocated_size)
259                 vfree(fw_priv->data);
260
261         kfree_const(fw_priv->fw_name);
262         kfree(fw_priv);
263 }
264
265 static void free_fw_priv(struct fw_priv *fw_priv)
266 {
267         struct firmware_cache *fwc = fw_priv->fwc;
268         spin_lock(&fwc->lock);
269         if (!kref_put(&fw_priv->ref, __free_fw_priv))
270                 spin_unlock(&fwc->lock);
271 }
272
273 #ifdef CONFIG_FW_LOADER_PAGED_BUF
274 bool fw_is_paged_buf(struct fw_priv *fw_priv)
275 {
276         return fw_priv->is_paged_buf;
277 }
278
279 void fw_free_paged_buf(struct fw_priv *fw_priv)
280 {
281         int i;
282
283         if (!fw_priv->pages)
284                 return;
285
286         vunmap(fw_priv->data);
287
288         for (i = 0; i < fw_priv->nr_pages; i++)
289                 __free_page(fw_priv->pages[i]);
290         kvfree(fw_priv->pages);
291         fw_priv->pages = NULL;
292         fw_priv->page_array_size = 0;
293         fw_priv->nr_pages = 0;
294 }
295
296 int fw_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed)
297 {
298         /* If the array of pages is too small, grow it */
299         if (fw_priv->page_array_size < pages_needed) {
300                 int new_array_size = max(pages_needed,
301                                          fw_priv->page_array_size * 2);
302                 struct page **new_pages;
303
304                 new_pages = kvmalloc_array(new_array_size, sizeof(void *),
305                                            GFP_KERNEL);
306                 if (!new_pages)
307                         return -ENOMEM;
308                 memcpy(new_pages, fw_priv->pages,
309                        fw_priv->page_array_size * sizeof(void *));
310                 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
311                        (new_array_size - fw_priv->page_array_size));
312                 kvfree(fw_priv->pages);
313                 fw_priv->pages = new_pages;
314                 fw_priv->page_array_size = new_array_size;
315         }
316
317         while (fw_priv->nr_pages < pages_needed) {
318                 fw_priv->pages[fw_priv->nr_pages] =
319                         alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
320
321                 if (!fw_priv->pages[fw_priv->nr_pages])
322                         return -ENOMEM;
323                 fw_priv->nr_pages++;
324         }
325
326         return 0;
327 }
328
329 int fw_map_paged_buf(struct fw_priv *fw_priv)
330 {
331         /* one pages buffer should be mapped/unmapped only once */
332         if (!fw_priv->pages)
333                 return 0;
334
335         vunmap(fw_priv->data);
336         fw_priv->data = vmap(fw_priv->pages, fw_priv->nr_pages, 0,
337                              PAGE_KERNEL_RO);
338         if (!fw_priv->data)
339                 return -ENOMEM;
340
341         return 0;
342 }
343 #endif
344
345 /*
346  * XZ-compressed firmware support
347  */
348 #ifdef CONFIG_FW_LOADER_COMPRESS
349 /* show an error and return the standard error code */
350 static int fw_decompress_xz_error(struct device *dev, enum xz_ret xz_ret)
351 {
352         if (xz_ret != XZ_STREAM_END) {
353                 dev_warn(dev, "xz decompression failed (xz_ret=%d)\n", xz_ret);
354                 return xz_ret == XZ_MEM_ERROR ? -ENOMEM : -EINVAL;
355         }
356         return 0;
357 }
358
359 /* single-shot decompression onto the pre-allocated buffer */
360 static int fw_decompress_xz_single(struct device *dev, struct fw_priv *fw_priv,
361                                    size_t in_size, const void *in_buffer)
362 {
363         struct xz_dec *xz_dec;
364         struct xz_buf xz_buf;
365         enum xz_ret xz_ret;
366
367         xz_dec = xz_dec_init(XZ_SINGLE, (u32)-1);
368         if (!xz_dec)
369                 return -ENOMEM;
370
371         xz_buf.in_size = in_size;
372         xz_buf.in = in_buffer;
373         xz_buf.in_pos = 0;
374         xz_buf.out_size = fw_priv->allocated_size;
375         xz_buf.out = fw_priv->data;
376         xz_buf.out_pos = 0;
377
378         xz_ret = xz_dec_run(xz_dec, &xz_buf);
379         xz_dec_end(xz_dec);
380
381         fw_priv->size = xz_buf.out_pos;
382         return fw_decompress_xz_error(dev, xz_ret);
383 }
384
385 /* decompression on paged buffer and map it */
386 static int fw_decompress_xz_pages(struct device *dev, struct fw_priv *fw_priv,
387                                   size_t in_size, const void *in_buffer)
388 {
389         struct xz_dec *xz_dec;
390         struct xz_buf xz_buf;
391         enum xz_ret xz_ret;
392         struct page *page;
393         int err = 0;
394
395         xz_dec = xz_dec_init(XZ_DYNALLOC, (u32)-1);
396         if (!xz_dec)
397                 return -ENOMEM;
398
399         xz_buf.in_size = in_size;
400         xz_buf.in = in_buffer;
401         xz_buf.in_pos = 0;
402
403         fw_priv->is_paged_buf = true;
404         fw_priv->size = 0;
405         do {
406                 if (fw_grow_paged_buf(fw_priv, fw_priv->nr_pages + 1)) {
407                         err = -ENOMEM;
408                         goto out;
409                 }
410
411                 /* decompress onto the new allocated page */
412                 page = fw_priv->pages[fw_priv->nr_pages - 1];
413                 xz_buf.out = kmap(page);
414                 xz_buf.out_pos = 0;
415                 xz_buf.out_size = PAGE_SIZE;
416                 xz_ret = xz_dec_run(xz_dec, &xz_buf);
417                 kunmap(page);
418                 fw_priv->size += xz_buf.out_pos;
419                 /* partial decompression means either end or error */
420                 if (xz_buf.out_pos != PAGE_SIZE)
421                         break;
422         } while (xz_ret == XZ_OK);
423
424         err = fw_decompress_xz_error(dev, xz_ret);
425         if (!err)
426                 err = fw_map_paged_buf(fw_priv);
427
428  out:
429         xz_dec_end(xz_dec);
430         return err;
431 }
432
433 static int fw_decompress_xz(struct device *dev, struct fw_priv *fw_priv,
434                             size_t in_size, const void *in_buffer)
435 {
436         /* if the buffer is pre-allocated, we can perform in single-shot mode */
437         if (fw_priv->data)
438                 return fw_decompress_xz_single(dev, fw_priv, in_size, in_buffer);
439         else
440                 return fw_decompress_xz_pages(dev, fw_priv, in_size, in_buffer);
441 }
442 #endif /* CONFIG_FW_LOADER_COMPRESS */
443
444 /* direct firmware loading support */
445 static char fw_path_para[256];
446 static const char * const fw_path[] = {
447         fw_path_para,
448         "/lib/firmware/updates/" UTS_RELEASE,
449         "/lib/firmware/updates",
450         "/lib/firmware/" UTS_RELEASE,
451         "/lib/firmware"
452 };
453
454 /*
455  * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
456  * from kernel command line because firmware_class is generally built in
457  * kernel instead of module.
458  */
459 module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
460 MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
461
462 static int
463 fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv,
464                            const char *suffix,
465                            int (*decompress)(struct device *dev,
466                                              struct fw_priv *fw_priv,
467                                              size_t in_size,
468                                              const void *in_buffer))
469 {
470         size_t size;
471         int i, len;
472         int rc = -ENOENT;
473         char *path;
474         size_t msize = INT_MAX;
475         void *buffer = NULL;
476
477         /* Already populated data member means we're loading into a buffer */
478         if (!decompress && fw_priv->data) {
479                 buffer = fw_priv->data;
480                 msize = fw_priv->allocated_size;
481         }
482
483         path = __getname();
484         if (!path)
485                 return -ENOMEM;
486
487         for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
488                 /* skip the unset customized path */
489                 if (!fw_path[i][0])
490                         continue;
491
492                 len = snprintf(path, PATH_MAX, "%s/%s%s",
493                                fw_path[i], fw_priv->fw_name, suffix);
494                 if (len >= PATH_MAX) {
495                         rc = -ENAMETOOLONG;
496                         break;
497                 }
498
499                 fw_priv->size = 0;
500
501                 /* load firmware files from the mount namespace of init */
502                 rc = kernel_read_file_from_path_initns(path, &buffer, msize,
503                                                        NULL,
504                                                        READING_FIRMWARE);
505                 if (rc < 0) {
506                         if (rc != -ENOENT)
507                                 dev_warn(device, "loading %s failed with error %d\n",
508                                          path, rc);
509                         else
510                                 dev_dbg(device, "loading %s failed for no such file or directory.\n",
511                                          path);
512                         continue;
513                 }
514                 size = rc;
515                 rc = 0;
516
517                 dev_dbg(device, "Loading firmware from %s\n", path);
518                 if (decompress) {
519                         dev_dbg(device, "f/w decompressing %s\n",
520                                 fw_priv->fw_name);
521                         rc = decompress(device, fw_priv, size, buffer);
522                         /* discard the superfluous original content */
523                         vfree(buffer);
524                         buffer = NULL;
525                         if (rc) {
526                                 fw_free_paged_buf(fw_priv);
527                                 continue;
528                         }
529                 } else {
530                         dev_dbg(device, "direct-loading %s\n",
531                                 fw_priv->fw_name);
532                         if (!fw_priv->data)
533                                 fw_priv->data = buffer;
534                         fw_priv->size = size;
535                 }
536                 fw_state_done(fw_priv);
537                 break;
538         }
539         __putname(path);
540
541         return rc;
542 }
543
544 /* firmware holds the ownership of pages */
545 static void firmware_free_data(const struct firmware *fw)
546 {
547         /* Loaded directly? */
548         if (!fw->priv) {
549                 vfree(fw->data);
550                 return;
551         }
552         free_fw_priv(fw->priv);
553 }
554
555 /* store the pages buffer info firmware from buf */
556 static void fw_set_page_data(struct fw_priv *fw_priv, struct firmware *fw)
557 {
558         fw->priv = fw_priv;
559         fw->size = fw_priv->size;
560         fw->data = fw_priv->data;
561
562         pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
563                  __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
564                  (unsigned int)fw_priv->size);
565 }
566
567 #ifdef CONFIG_FW_CACHE
568 static void fw_name_devm_release(struct device *dev, void *res)
569 {
570         struct fw_name_devm *fwn = res;
571
572         if (fwn->magic == (unsigned long)&fw_cache)
573                 pr_debug("%s: fw_name-%s devm-%p released\n",
574                                 __func__, fwn->name, res);
575         kfree_const(fwn->name);
576 }
577
578 static int fw_devm_match(struct device *dev, void *res,
579                 void *match_data)
580 {
581         struct fw_name_devm *fwn = res;
582
583         return (fwn->magic == (unsigned long)&fw_cache) &&
584                 !strcmp(fwn->name, match_data);
585 }
586
587 static struct fw_name_devm *fw_find_devm_name(struct device *dev,
588                 const char *name)
589 {
590         struct fw_name_devm *fwn;
591
592         fwn = devres_find(dev, fw_name_devm_release,
593                           fw_devm_match, (void *)name);
594         return fwn;
595 }
596
597 static bool fw_cache_is_setup(struct device *dev, const char *name)
598 {
599         struct fw_name_devm *fwn;
600
601         fwn = fw_find_devm_name(dev, name);
602         if (fwn)
603                 return true;
604
605         return false;
606 }
607
608 /* add firmware name into devres list */
609 static int fw_add_devm_name(struct device *dev, const char *name)
610 {
611         struct fw_name_devm *fwn;
612
613         if (fw_cache_is_setup(dev, name))
614                 return 0;
615
616         fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
617                            GFP_KERNEL);
618         if (!fwn)
619                 return -ENOMEM;
620         fwn->name = kstrdup_const(name, GFP_KERNEL);
621         if (!fwn->name) {
622                 devres_free(fwn);
623                 return -ENOMEM;
624         }
625
626         fwn->magic = (unsigned long)&fw_cache;
627         devres_add(dev, fwn);
628
629         return 0;
630 }
631 #else
632 static bool fw_cache_is_setup(struct device *dev, const char *name)
633 {
634         return false;
635 }
636
637 static int fw_add_devm_name(struct device *dev, const char *name)
638 {
639         return 0;
640 }
641 #endif
642
643 int assign_fw(struct firmware *fw, struct device *device, u32 opt_flags)
644 {
645         struct fw_priv *fw_priv = fw->priv;
646         int ret;
647
648         mutex_lock(&fw_lock);
649         if (!fw_priv->size || fw_state_is_aborted(fw_priv)) {
650                 mutex_unlock(&fw_lock);
651                 return -ENOENT;
652         }
653
654         /*
655          * add firmware name into devres list so that we can auto cache
656          * and uncache firmware for device.
657          *
658          * device may has been deleted already, but the problem
659          * should be fixed in devres or driver core.
660          */
661         /* don't cache firmware handled without uevent */
662         if (device && (opt_flags & FW_OPT_UEVENT) &&
663             !(opt_flags & FW_OPT_NOCACHE)) {
664                 ret = fw_add_devm_name(device, fw_priv->fw_name);
665                 if (ret) {
666                         mutex_unlock(&fw_lock);
667                         return ret;
668                 }
669         }
670
671         /*
672          * After caching firmware image is started, let it piggyback
673          * on request firmware.
674          */
675         if (!(opt_flags & FW_OPT_NOCACHE) &&
676             fw_priv->fwc->state == FW_LOADER_START_CACHE) {
677                 if (fw_cache_piggyback_on_request(fw_priv->fw_name))
678                         kref_get(&fw_priv->ref);
679         }
680
681         /* pass the pages buffer to driver at the last minute */
682         fw_set_page_data(fw_priv, fw);
683         mutex_unlock(&fw_lock);
684         return 0;
685 }
686
687 /* prepare firmware and firmware_buf structs;
688  * return 0 if a firmware is already assigned, 1 if need to load one,
689  * or a negative error code
690  */
691 static int
692 _request_firmware_prepare(struct firmware **firmware_p, const char *name,
693                           struct device *device, void *dbuf, size_t size,
694                           u32 opt_flags)
695 {
696         struct firmware *firmware;
697         struct fw_priv *fw_priv;
698         int ret;
699
700         *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
701         if (!firmware) {
702                 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
703                         __func__);
704                 return -ENOMEM;
705         }
706
707         if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
708                 dev_dbg(device, "using built-in %s\n", name);
709                 return 0; /* assigned */
710         }
711
712         ret = alloc_lookup_fw_priv(name, &fw_cache, &fw_priv, dbuf, size,
713                                   opt_flags);
714
715         /*
716          * bind with 'priv' now to avoid warning in failure path
717          * of requesting firmware.
718          */
719         firmware->priv = fw_priv;
720
721         if (ret > 0) {
722                 ret = fw_state_wait(fw_priv);
723                 if (!ret) {
724                         fw_set_page_data(fw_priv, firmware);
725                         return 0; /* assigned */
726                 }
727         }
728
729         if (ret < 0)
730                 return ret;
731         return 1; /* need to load */
732 }
733
734 /*
735  * Batched requests need only one wake, we need to do this step last due to the
736  * fallback mechanism. The buf is protected with kref_get(), and it won't be
737  * released until the last user calls release_firmware().
738  *
739  * Failed batched requests are possible as well, in such cases we just share
740  * the struct fw_priv and won't release it until all requests are woken
741  * and have gone through this same path.
742  */
743 static void fw_abort_batch_reqs(struct firmware *fw)
744 {
745         struct fw_priv *fw_priv;
746
747         /* Loaded directly? */
748         if (!fw || !fw->priv)
749                 return;
750
751         fw_priv = fw->priv;
752         if (!fw_state_is_aborted(fw_priv))
753                 fw_state_aborted(fw_priv);
754 }
755
756 /* called from request_firmware() and request_firmware_work_func() */
757 static int
758 _request_firmware(const struct firmware **firmware_p, const char *name,
759                   struct device *device, void *buf, size_t size,
760                   u32 opt_flags)
761 {
762         struct firmware *fw = NULL;
763         int ret;
764
765         if (!firmware_p)
766                 return -EINVAL;
767
768         if (!name || name[0] == '\0') {
769                 ret = -EINVAL;
770                 goto out;
771         }
772
773         ret = _request_firmware_prepare(&fw, name, device, buf, size,
774                                         opt_flags);
775         if (ret <= 0) /* error or already assigned */
776                 goto out;
777
778         ret = fw_get_filesystem_firmware(device, fw->priv, "", NULL);
779 #ifdef CONFIG_FW_LOADER_COMPRESS
780         if (ret == -ENOENT)
781                 ret = fw_get_filesystem_firmware(device, fw->priv, ".xz",
782                                                  fw_decompress_xz);
783 #endif
784
785         if (ret == -ENOENT)
786                 ret = firmware_fallback_platform(fw->priv, opt_flags);
787
788         if (ret) {
789                 if (!(opt_flags & FW_OPT_NO_WARN))
790                         dev_warn(device,
791                                  "Direct firmware load for %s failed with error %d\n",
792                                  name, ret);
793                 ret = firmware_fallback_sysfs(fw, name, device, opt_flags, ret);
794         } else
795                 ret = assign_fw(fw, device, opt_flags);
796
797  out:
798         if (ret < 0) {
799                 fw_abort_batch_reqs(fw);
800                 release_firmware(fw);
801                 fw = NULL;
802         }
803
804         *firmware_p = fw;
805         return ret;
806 }
807
808 /**
809  * request_firmware() - send firmware request and wait for it
810  * @firmware_p: pointer to firmware image
811  * @name: name of firmware file
812  * @device: device for which firmware is being loaded
813  *
814  *      @firmware_p will be used to return a firmware image by the name
815  *      of @name for device @device.
816  *
817  *      Should be called from user context where sleeping is allowed.
818  *
819  *      @name will be used as $FIRMWARE in the uevent environment and
820  *      should be distinctive enough not to be confused with any other
821  *      firmware image for this or any other device.
822  *
823  *      Caller must hold the reference count of @device.
824  *
825  *      The function can be called safely inside device's suspend and
826  *      resume callback.
827  **/
828 int
829 request_firmware(const struct firmware **firmware_p, const char *name,
830                  struct device *device)
831 {
832         int ret;
833
834         /* Need to pin this module until return */
835         __module_get(THIS_MODULE);
836         ret = _request_firmware(firmware_p, name, device, NULL, 0,
837                                 FW_OPT_UEVENT);
838         module_put(THIS_MODULE);
839         return ret;
840 }
841 EXPORT_SYMBOL(request_firmware);
842
843 /**
844  * firmware_request_nowarn() - request for an optional fw module
845  * @firmware: pointer to firmware image
846  * @name: name of firmware file
847  * @device: device for which firmware is being loaded
848  *
849  * This function is similar in behaviour to request_firmware(), except it
850  * doesn't produce warning messages when the file is not found. The sysfs
851  * fallback mechanism is enabled if direct filesystem lookup fails. However,
852  * failures to find the firmware file with it are still suppressed. It is
853  * therefore up to the driver to check for the return value of this call and to
854  * decide when to inform the users of errors.
855  **/
856 int firmware_request_nowarn(const struct firmware **firmware, const char *name,
857                             struct device *device)
858 {
859         int ret;
860
861         /* Need to pin this module until return */
862         __module_get(THIS_MODULE);
863         ret = _request_firmware(firmware, name, device, NULL, 0,
864                                 FW_OPT_UEVENT | FW_OPT_NO_WARN);
865         module_put(THIS_MODULE);
866         return ret;
867 }
868 EXPORT_SYMBOL_GPL(firmware_request_nowarn);
869
870 /**
871  * request_firmware_direct() - load firmware directly without usermode helper
872  * @firmware_p: pointer to firmware image
873  * @name: name of firmware file
874  * @device: device for which firmware is being loaded
875  *
876  * This function works pretty much like request_firmware(), but this doesn't
877  * fall back to usermode helper even if the firmware couldn't be loaded
878  * directly from fs.  Hence it's useful for loading optional firmwares, which
879  * aren't always present, without extra long timeouts of udev.
880  **/
881 int request_firmware_direct(const struct firmware **firmware_p,
882                             const char *name, struct device *device)
883 {
884         int ret;
885
886         __module_get(THIS_MODULE);
887         ret = _request_firmware(firmware_p, name, device, NULL, 0,
888                                 FW_OPT_UEVENT | FW_OPT_NO_WARN |
889                                 FW_OPT_NOFALLBACK_SYSFS);
890         module_put(THIS_MODULE);
891         return ret;
892 }
893 EXPORT_SYMBOL_GPL(request_firmware_direct);
894
895 /**
896  * firmware_request_platform() - request firmware with platform-fw fallback
897  * @firmware: pointer to firmware image
898  * @name: name of firmware file
899  * @device: device for which firmware is being loaded
900  *
901  * This function is similar in behaviour to request_firmware, except that if
902  * direct filesystem lookup fails, it will fallback to looking for a copy of the
903  * requested firmware embedded in the platform's main (e.g. UEFI) firmware.
904  **/
905 int firmware_request_platform(const struct firmware **firmware,
906                               const char *name, struct device *device)
907 {
908         int ret;
909
910         /* Need to pin this module until return */
911         __module_get(THIS_MODULE);
912         ret = _request_firmware(firmware, name, device, NULL, 0,
913                                 FW_OPT_UEVENT | FW_OPT_FALLBACK_PLATFORM);
914         module_put(THIS_MODULE);
915         return ret;
916 }
917 EXPORT_SYMBOL_GPL(firmware_request_platform);
918
919 /**
920  * firmware_request_cache() - cache firmware for suspend so resume can use it
921  * @name: name of firmware file
922  * @device: device for which firmware should be cached for
923  *
924  * There are some devices with an optimization that enables the device to not
925  * require loading firmware on system reboot. This optimization may still
926  * require the firmware present on resume from suspend. This routine can be
927  * used to ensure the firmware is present on resume from suspend in these
928  * situations. This helper is not compatible with drivers which use
929  * request_firmware_into_buf() or request_firmware_nowait() with no uevent set.
930  **/
931 int firmware_request_cache(struct device *device, const char *name)
932 {
933         int ret;
934
935         mutex_lock(&fw_lock);
936         ret = fw_add_devm_name(device, name);
937         mutex_unlock(&fw_lock);
938
939         return ret;
940 }
941 EXPORT_SYMBOL_GPL(firmware_request_cache);
942
943 /**
944  * request_firmware_into_buf() - load firmware into a previously allocated buffer
945  * @firmware_p: pointer to firmware image
946  * @name: name of firmware file
947  * @device: device for which firmware is being loaded and DMA region allocated
948  * @buf: address of buffer to load firmware into
949  * @size: size of buffer
950  *
951  * This function works pretty much like request_firmware(), but it doesn't
952  * allocate a buffer to hold the firmware data. Instead, the firmware
953  * is loaded directly into the buffer pointed to by @buf and the @firmware_p
954  * data member is pointed at @buf.
955  *
956  * This function doesn't cache firmware either.
957  */
958 int
959 request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
960                           struct device *device, void *buf, size_t size)
961 {
962         int ret;
963
964         if (fw_cache_is_setup(device, name))
965                 return -EOPNOTSUPP;
966
967         __module_get(THIS_MODULE);
968         ret = _request_firmware(firmware_p, name, device, buf, size,
969                                 FW_OPT_UEVENT | FW_OPT_NOCACHE);
970         module_put(THIS_MODULE);
971         return ret;
972 }
973 EXPORT_SYMBOL(request_firmware_into_buf);
974
975 /**
976  * release_firmware() - release the resource associated with a firmware image
977  * @fw: firmware resource to release
978  **/
979 void release_firmware(const struct firmware *fw)
980 {
981         if (fw) {
982                 if (!fw_is_builtin_firmware(fw))
983                         firmware_free_data(fw);
984                 kfree(fw);
985         }
986 }
987 EXPORT_SYMBOL(release_firmware);
988
989 /* Async support */
990 struct firmware_work {
991         struct work_struct work;
992         struct module *module;
993         const char *name;
994         struct device *device;
995         void *context;
996         void (*cont)(const struct firmware *fw, void *context);
997         u32 opt_flags;
998 };
999
1000 static void request_firmware_work_func(struct work_struct *work)
1001 {
1002         struct firmware_work *fw_work;
1003         const struct firmware *fw;
1004
1005         fw_work = container_of(work, struct firmware_work, work);
1006
1007         _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
1008                           fw_work->opt_flags);
1009         fw_work->cont(fw, fw_work->context);
1010         put_device(fw_work->device); /* taken in request_firmware_nowait() */
1011
1012         module_put(fw_work->module);
1013         kfree_const(fw_work->name);
1014         kfree(fw_work);
1015 }
1016
1017 /**
1018  * request_firmware_nowait() - asynchronous version of request_firmware
1019  * @module: module requesting the firmware
1020  * @uevent: sends uevent to copy the firmware image if this flag
1021  *      is non-zero else the firmware copy must be done manually.
1022  * @name: name of firmware file
1023  * @device: device for which firmware is being loaded
1024  * @gfp: allocation flags
1025  * @context: will be passed over to @cont, and
1026  *      @fw may be %NULL if firmware request fails.
1027  * @cont: function will be called asynchronously when the firmware
1028  *      request is over.
1029  *
1030  *      Caller must hold the reference count of @device.
1031  *
1032  *      Asynchronous variant of request_firmware() for user contexts:
1033  *              - sleep for as small periods as possible since it may
1034  *                increase kernel boot time of built-in device drivers
1035  *                requesting firmware in their ->probe() methods, if
1036  *                @gfp is GFP_KERNEL.
1037  *
1038  *              - can't sleep at all if @gfp is GFP_ATOMIC.
1039  **/
1040 int
1041 request_firmware_nowait(
1042         struct module *module, bool uevent,
1043         const char *name, struct device *device, gfp_t gfp, void *context,
1044         void (*cont)(const struct firmware *fw, void *context))
1045 {
1046         struct firmware_work *fw_work;
1047
1048         fw_work = kzalloc(sizeof(struct firmware_work), gfp);
1049         if (!fw_work)
1050                 return -ENOMEM;
1051
1052         fw_work->module = module;
1053         fw_work->name = kstrdup_const(name, gfp);
1054         if (!fw_work->name) {
1055                 kfree(fw_work);
1056                 return -ENOMEM;
1057         }
1058         fw_work->device = device;
1059         fw_work->context = context;
1060         fw_work->cont = cont;
1061         fw_work->opt_flags = FW_OPT_NOWAIT |
1062                 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
1063
1064         if (!uevent && fw_cache_is_setup(device, name)) {
1065                 kfree_const(fw_work->name);
1066                 kfree(fw_work);
1067                 return -EOPNOTSUPP;
1068         }
1069
1070         if (!try_module_get(module)) {
1071                 kfree_const(fw_work->name);
1072                 kfree(fw_work);
1073                 return -EFAULT;
1074         }
1075
1076         get_device(fw_work->device);
1077         INIT_WORK(&fw_work->work, request_firmware_work_func);
1078         schedule_work(&fw_work->work);
1079         return 0;
1080 }
1081 EXPORT_SYMBOL(request_firmware_nowait);
1082
1083 #ifdef CONFIG_FW_CACHE
1084 static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1085
1086 /**
1087  * cache_firmware() - cache one firmware image in kernel memory space
1088  * @fw_name: the firmware image name
1089  *
1090  * Cache firmware in kernel memory so that drivers can use it when
1091  * system isn't ready for them to request firmware image from userspace.
1092  * Once it returns successfully, driver can use request_firmware or its
1093  * nowait version to get the cached firmware without any interacting
1094  * with userspace
1095  *
1096  * Return 0 if the firmware image has been cached successfully
1097  * Return !0 otherwise
1098  *
1099  */
1100 static int cache_firmware(const char *fw_name)
1101 {
1102         int ret;
1103         const struct firmware *fw;
1104
1105         pr_debug("%s: %s\n", __func__, fw_name);
1106
1107         ret = request_firmware(&fw, fw_name, NULL);
1108         if (!ret)
1109                 kfree(fw);
1110
1111         pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1112
1113         return ret;
1114 }
1115
1116 static struct fw_priv *lookup_fw_priv(const char *fw_name)
1117 {
1118         struct fw_priv *tmp;
1119         struct firmware_cache *fwc = &fw_cache;
1120
1121         spin_lock(&fwc->lock);
1122         tmp = __lookup_fw_priv(fw_name);
1123         spin_unlock(&fwc->lock);
1124
1125         return tmp;
1126 }
1127
1128 /**
1129  * uncache_firmware() - remove one cached firmware image
1130  * @fw_name: the firmware image name
1131  *
1132  * Uncache one firmware image which has been cached successfully
1133  * before.
1134  *
1135  * Return 0 if the firmware cache has been removed successfully
1136  * Return !0 otherwise
1137  *
1138  */
1139 static int uncache_firmware(const char *fw_name)
1140 {
1141         struct fw_priv *fw_priv;
1142         struct firmware fw;
1143
1144         pr_debug("%s: %s\n", __func__, fw_name);
1145
1146         if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
1147                 return 0;
1148
1149         fw_priv = lookup_fw_priv(fw_name);
1150         if (fw_priv) {
1151                 free_fw_priv(fw_priv);
1152                 return 0;
1153         }
1154
1155         return -EINVAL;
1156 }
1157
1158 static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1159 {
1160         struct fw_cache_entry *fce;
1161
1162         fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
1163         if (!fce)
1164                 goto exit;
1165
1166         fce->name = kstrdup_const(name, GFP_ATOMIC);
1167         if (!fce->name) {
1168                 kfree(fce);
1169                 fce = NULL;
1170                 goto exit;
1171         }
1172 exit:
1173         return fce;
1174 }
1175
1176 static int __fw_entry_found(const char *name)
1177 {
1178         struct firmware_cache *fwc = &fw_cache;
1179         struct fw_cache_entry *fce;
1180
1181         list_for_each_entry(fce, &fwc->fw_names, list) {
1182                 if (!strcmp(fce->name, name))
1183                         return 1;
1184         }
1185         return 0;
1186 }
1187
1188 static int fw_cache_piggyback_on_request(const char *name)
1189 {
1190         struct firmware_cache *fwc = &fw_cache;
1191         struct fw_cache_entry *fce;
1192         int ret = 0;
1193
1194         spin_lock(&fwc->name_lock);
1195         if (__fw_entry_found(name))
1196                 goto found;
1197
1198         fce = alloc_fw_cache_entry(name);
1199         if (fce) {
1200                 ret = 1;
1201                 list_add(&fce->list, &fwc->fw_names);
1202                 pr_debug("%s: fw: %s\n", __func__, name);
1203         }
1204 found:
1205         spin_unlock(&fwc->name_lock);
1206         return ret;
1207 }
1208
1209 static void free_fw_cache_entry(struct fw_cache_entry *fce)
1210 {
1211         kfree_const(fce->name);
1212         kfree(fce);
1213 }
1214
1215 static void __async_dev_cache_fw_image(void *fw_entry,
1216                                        async_cookie_t cookie)
1217 {
1218         struct fw_cache_entry *fce = fw_entry;
1219         struct firmware_cache *fwc = &fw_cache;
1220         int ret;
1221
1222         ret = cache_firmware(fce->name);
1223         if (ret) {
1224                 spin_lock(&fwc->name_lock);
1225                 list_del(&fce->list);
1226                 spin_unlock(&fwc->name_lock);
1227
1228                 free_fw_cache_entry(fce);
1229         }
1230 }
1231
1232 /* called with dev->devres_lock held */
1233 static void dev_create_fw_entry(struct device *dev, void *res,
1234                                 void *data)
1235 {
1236         struct fw_name_devm *fwn = res;
1237         const char *fw_name = fwn->name;
1238         struct list_head *head = data;
1239         struct fw_cache_entry *fce;
1240
1241         fce = alloc_fw_cache_entry(fw_name);
1242         if (fce)
1243                 list_add(&fce->list, head);
1244 }
1245
1246 static int devm_name_match(struct device *dev, void *res,
1247                            void *match_data)
1248 {
1249         struct fw_name_devm *fwn = res;
1250         return (fwn->magic == (unsigned long)match_data);
1251 }
1252
1253 static void dev_cache_fw_image(struct device *dev, void *data)
1254 {
1255         LIST_HEAD(todo);
1256         struct fw_cache_entry *fce;
1257         struct fw_cache_entry *fce_next;
1258         struct firmware_cache *fwc = &fw_cache;
1259
1260         devres_for_each_res(dev, fw_name_devm_release,
1261                             devm_name_match, &fw_cache,
1262                             dev_create_fw_entry, &todo);
1263
1264         list_for_each_entry_safe(fce, fce_next, &todo, list) {
1265                 list_del(&fce->list);
1266
1267                 spin_lock(&fwc->name_lock);
1268                 /* only one cache entry for one firmware */
1269                 if (!__fw_entry_found(fce->name)) {
1270                         list_add(&fce->list, &fwc->fw_names);
1271                 } else {
1272                         free_fw_cache_entry(fce);
1273                         fce = NULL;
1274                 }
1275                 spin_unlock(&fwc->name_lock);
1276
1277                 if (fce)
1278                         async_schedule_domain(__async_dev_cache_fw_image,
1279                                               (void *)fce,
1280                                               &fw_cache_domain);
1281         }
1282 }
1283
1284 static void __device_uncache_fw_images(void)
1285 {
1286         struct firmware_cache *fwc = &fw_cache;
1287         struct fw_cache_entry *fce;
1288
1289         spin_lock(&fwc->name_lock);
1290         while (!list_empty(&fwc->fw_names)) {
1291                 fce = list_entry(fwc->fw_names.next,
1292                                 struct fw_cache_entry, list);
1293                 list_del(&fce->list);
1294                 spin_unlock(&fwc->name_lock);
1295
1296                 uncache_firmware(fce->name);
1297                 free_fw_cache_entry(fce);
1298
1299                 spin_lock(&fwc->name_lock);
1300         }
1301         spin_unlock(&fwc->name_lock);
1302 }
1303
1304 /**
1305  * device_cache_fw_images() - cache devices' firmware
1306  *
1307  * If one device called request_firmware or its nowait version
1308  * successfully before, the firmware names are recored into the
1309  * device's devres link list, so device_cache_fw_images can call
1310  * cache_firmware() to cache these firmwares for the device,
1311  * then the device driver can load its firmwares easily at
1312  * time when system is not ready to complete loading firmware.
1313  */
1314 static void device_cache_fw_images(void)
1315 {
1316         struct firmware_cache *fwc = &fw_cache;
1317         DEFINE_WAIT(wait);
1318
1319         pr_debug("%s\n", __func__);
1320
1321         /* cancel uncache work */
1322         cancel_delayed_work_sync(&fwc->work);
1323
1324         fw_fallback_set_cache_timeout();
1325
1326         mutex_lock(&fw_lock);
1327         fwc->state = FW_LOADER_START_CACHE;
1328         dpm_for_each_dev(NULL, dev_cache_fw_image);
1329         mutex_unlock(&fw_lock);
1330
1331         /* wait for completion of caching firmware for all devices */
1332         async_synchronize_full_domain(&fw_cache_domain);
1333
1334         fw_fallback_set_default_timeout();
1335 }
1336
1337 /**
1338  * device_uncache_fw_images() - uncache devices' firmware
1339  *
1340  * uncache all firmwares which have been cached successfully
1341  * by device_uncache_fw_images earlier
1342  */
1343 static void device_uncache_fw_images(void)
1344 {
1345         pr_debug("%s\n", __func__);
1346         __device_uncache_fw_images();
1347 }
1348
1349 static void device_uncache_fw_images_work(struct work_struct *work)
1350 {
1351         device_uncache_fw_images();
1352 }
1353
1354 /**
1355  * device_uncache_fw_images_delay() - uncache devices firmwares
1356  * @delay: number of milliseconds to delay uncache device firmwares
1357  *
1358  * uncache all devices's firmwares which has been cached successfully
1359  * by device_cache_fw_images after @delay milliseconds.
1360  */
1361 static void device_uncache_fw_images_delay(unsigned long delay)
1362 {
1363         queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1364                            msecs_to_jiffies(delay));
1365 }
1366
1367 static int fw_pm_notify(struct notifier_block *notify_block,
1368                         unsigned long mode, void *unused)
1369 {
1370         switch (mode) {
1371         case PM_HIBERNATION_PREPARE:
1372         case PM_SUSPEND_PREPARE:
1373         case PM_RESTORE_PREPARE:
1374                 /*
1375                  * kill pending fallback requests with a custom fallback
1376                  * to avoid stalling suspend.
1377                  */
1378                 kill_pending_fw_fallback_reqs(true);
1379                 device_cache_fw_images();
1380                 break;
1381
1382         case PM_POST_SUSPEND:
1383         case PM_POST_HIBERNATION:
1384         case PM_POST_RESTORE:
1385                 /*
1386                  * In case that system sleep failed and syscore_suspend is
1387                  * not called.
1388                  */
1389                 mutex_lock(&fw_lock);
1390                 fw_cache.state = FW_LOADER_NO_CACHE;
1391                 mutex_unlock(&fw_lock);
1392
1393                 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1394                 break;
1395         }
1396
1397         return 0;
1398 }
1399
1400 /* stop caching firmware once syscore_suspend is reached */
1401 static int fw_suspend(void)
1402 {
1403         fw_cache.state = FW_LOADER_NO_CACHE;
1404         return 0;
1405 }
1406
1407 static struct syscore_ops fw_syscore_ops = {
1408         .suspend = fw_suspend,
1409 };
1410
1411 static int __init register_fw_pm_ops(void)
1412 {
1413         int ret;
1414
1415         spin_lock_init(&fw_cache.name_lock);
1416         INIT_LIST_HEAD(&fw_cache.fw_names);
1417
1418         INIT_DELAYED_WORK(&fw_cache.work,
1419                           device_uncache_fw_images_work);
1420
1421         fw_cache.pm_notify.notifier_call = fw_pm_notify;
1422         ret = register_pm_notifier(&fw_cache.pm_notify);
1423         if (ret)
1424                 return ret;
1425
1426         register_syscore_ops(&fw_syscore_ops);
1427
1428         return ret;
1429 }
1430
1431 static inline void unregister_fw_pm_ops(void)
1432 {
1433         unregister_syscore_ops(&fw_syscore_ops);
1434         unregister_pm_notifier(&fw_cache.pm_notify);
1435 }
1436 #else
1437 static int fw_cache_piggyback_on_request(const char *name)
1438 {
1439         return 0;
1440 }
1441 static inline int register_fw_pm_ops(void)
1442 {
1443         return 0;
1444 }
1445 static inline void unregister_fw_pm_ops(void)
1446 {
1447 }
1448 #endif
1449
1450 static void __init fw_cache_init(void)
1451 {
1452         spin_lock_init(&fw_cache.lock);
1453         INIT_LIST_HEAD(&fw_cache.head);
1454         fw_cache.state = FW_LOADER_NO_CACHE;
1455 }
1456
1457 static int fw_shutdown_notify(struct notifier_block *unused1,
1458                               unsigned long unused2, void *unused3)
1459 {
1460         /*
1461          * Kill all pending fallback requests to avoid both stalling shutdown,
1462          * and avoid a deadlock with the usermode_lock.
1463          */
1464         kill_pending_fw_fallback_reqs(false);
1465
1466         return NOTIFY_DONE;
1467 }
1468
1469 static struct notifier_block fw_shutdown_nb = {
1470         .notifier_call = fw_shutdown_notify,
1471 };
1472
1473 static int __init firmware_class_init(void)
1474 {
1475         int ret;
1476
1477         /* No need to unfold these on exit */
1478         fw_cache_init();
1479
1480         ret = register_fw_pm_ops();
1481         if (ret)
1482                 return ret;
1483
1484         ret = register_reboot_notifier(&fw_shutdown_nb);
1485         if (ret)
1486                 goto out;
1487
1488         return register_sysfs_loader();
1489
1490 out:
1491         unregister_fw_pm_ops();
1492         return ret;
1493 }
1494
1495 static void __exit firmware_class_exit(void)
1496 {
1497         unregister_fw_pm_ops();
1498         unregister_reboot_notifier(&fw_shutdown_nb);
1499         unregister_sysfs_loader();
1500 }
1501
1502 fs_initcall(firmware_class_init);
1503 module_exit(firmware_class_exit);