1 // SPDX-License-Identifier: GPL-2.0
3 * main.c - Multi purpose firmware loading support
5 * Copyright (c) 2003 Manuel Estrada Sainz
7 * Please see Documentation/driver-api/firmware/ for more information.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
31 #include <linux/async.h>
33 #include <linux/suspend.h>
34 #include <linux/syscore_ops.h>
35 #include <linux/reboot.h>
36 #include <linux/security.h>
39 #include <generated/utsrelease.h>
45 MODULE_AUTHOR("Manuel Estrada Sainz");
46 MODULE_DESCRIPTION("Multi purpose firmware loading support");
47 MODULE_LICENSE("GPL");
49 struct firmware_cache
{
50 /* firmware_buf instance will be added into the below list */
52 struct list_head head
;
55 #ifdef CONFIG_FW_CACHE
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
63 struct list_head fw_names
;
65 struct delayed_work work
;
67 struct notifier_block pm_notify
;
71 struct fw_cache_entry
{
72 struct list_head list
;
81 static inline struct fw_priv
*to_fw_priv(struct kref
*ref
)
83 return container_of(ref
, struct fw_priv
, ref
);
86 #define FW_LOADER_NO_CACHE 0
87 #define FW_LOADER_START_CACHE 1
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
);
93 static struct firmware_cache fw_cache
;
95 /* Builtin firmware support */
97 #ifdef CONFIG_FW_LOADER
99 extern struct builtin_fw __start_builtin_fw
[];
100 extern struct builtin_fw __end_builtin_fw
[];
102 static void fw_copy_to_prealloc_buf(struct firmware
*fw
,
103 void *buf
, size_t size
)
105 if (!buf
|| size
< fw
->size
)
107 memcpy(buf
, fw
->data
, fw
->size
);
110 static bool fw_get_builtin_firmware(struct firmware
*fw
, const char *name
,
111 void *buf
, size_t size
)
113 struct builtin_fw
*b_fw
;
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
);
128 static bool fw_is_builtin_firmware(const struct firmware
*fw
)
130 struct builtin_fw
*b_fw
;
132 for (b_fw
= __start_builtin_fw
; b_fw
!= __end_builtin_fw
; b_fw
++)
133 if (fw
->data
== b_fw
->data
)
139 #else /* Module case - no builtin firmware support */
141 static inline bool fw_get_builtin_firmware(struct firmware
*fw
,
142 const char *name
, void *buf
,
148 static inline bool fw_is_builtin_firmware(const struct firmware
*fw
)
154 static void fw_state_init(struct fw_priv
*fw_priv
)
156 struct fw_state
*fw_st
= &fw_priv
->fw_st
;
158 init_completion(&fw_st
->completion
);
159 fw_st
->status
= FW_STATUS_UNKNOWN
;
162 static inline int fw_state_wait(struct fw_priv
*fw_priv
)
164 return __fw_state_wait_common(fw_priv
, MAX_SCHEDULE_TIMEOUT
);
167 static int fw_cache_piggyback_on_request(const char *name
);
169 static struct fw_priv
*__allocate_fw_priv(const char *fw_name
,
170 struct firmware_cache
*fwc
,
176 struct fw_priv
*fw_priv
;
178 /* For a partial read, the buffer must be preallocated. */
179 if ((opt_flags
& FW_OPT_PARTIAL
) && !dbuf
)
182 /* Only partial reads are allowed to use an offset. */
183 if (offset
!= 0 && !(opt_flags
& FW_OPT_PARTIAL
))
186 fw_priv
= kzalloc(sizeof(*fw_priv
), GFP_ATOMIC
);
190 fw_priv
->fw_name
= kstrdup_const(fw_name
, GFP_ATOMIC
);
191 if (!fw_priv
->fw_name
) {
196 kref_init(&fw_priv
->ref
);
198 fw_priv
->data
= dbuf
;
199 fw_priv
->allocated_size
= size
;
200 fw_priv
->offset
= offset
;
201 fw_priv
->opt_flags
= opt_flags
;
202 fw_state_init(fw_priv
);
203 #ifdef CONFIG_FW_LOADER_USER_HELPER
204 INIT_LIST_HEAD(&fw_priv
->pending_list
);
207 pr_debug("%s: fw-%s fw_priv=%p\n", __func__
, fw_name
, fw_priv
);
212 static struct fw_priv
*__lookup_fw_priv(const char *fw_name
)
215 struct firmware_cache
*fwc
= &fw_cache
;
217 list_for_each_entry(tmp
, &fwc
->head
, list
)
218 if (!strcmp(tmp
->fw_name
, fw_name
))
223 /* Returns 1 for batching firmware requests with the same name */
224 static int alloc_lookup_fw_priv(const char *fw_name
,
225 struct firmware_cache
*fwc
,
226 struct fw_priv
**fw_priv
,
234 spin_lock(&fwc
->lock
);
236 * Do not merge requests that are marked to be non-cached or
237 * are performing partial reads.
239 if (!(opt_flags
& (FW_OPT_NOCACHE
| FW_OPT_PARTIAL
))) {
240 tmp
= __lookup_fw_priv(fw_name
);
243 spin_unlock(&fwc
->lock
);
245 pr_debug("batched request - sharing the same struct fw_priv and lookup for multiple requests\n");
250 tmp
= __allocate_fw_priv(fw_name
, fwc
, dbuf
, size
, offset
, opt_flags
);
252 INIT_LIST_HEAD(&tmp
->list
);
253 if (!(opt_flags
& FW_OPT_NOCACHE
))
254 list_add(&tmp
->list
, &fwc
->head
);
256 spin_unlock(&fwc
->lock
);
260 return tmp
? 0 : -ENOMEM
;
263 static void __free_fw_priv(struct kref
*ref
)
264 __releases(&fwc
->lock
)
266 struct fw_priv
*fw_priv
= to_fw_priv(ref
);
267 struct firmware_cache
*fwc
= fw_priv
->fwc
;
269 pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
270 __func__
, fw_priv
->fw_name
, fw_priv
, fw_priv
->data
,
271 (unsigned int)fw_priv
->size
);
273 list_del(&fw_priv
->list
);
274 spin_unlock(&fwc
->lock
);
276 if (fw_is_paged_buf(fw_priv
))
277 fw_free_paged_buf(fw_priv
);
278 else if (!fw_priv
->allocated_size
)
279 vfree(fw_priv
->data
);
281 kfree_const(fw_priv
->fw_name
);
285 static void free_fw_priv(struct fw_priv
*fw_priv
)
287 struct firmware_cache
*fwc
= fw_priv
->fwc
;
288 spin_lock(&fwc
->lock
);
289 if (!kref_put(&fw_priv
->ref
, __free_fw_priv
))
290 spin_unlock(&fwc
->lock
);
293 #ifdef CONFIG_FW_LOADER_PAGED_BUF
294 bool fw_is_paged_buf(struct fw_priv
*fw_priv
)
296 return fw_priv
->is_paged_buf
;
299 void fw_free_paged_buf(struct fw_priv
*fw_priv
)
306 vunmap(fw_priv
->data
);
308 for (i
= 0; i
< fw_priv
->nr_pages
; i
++)
309 __free_page(fw_priv
->pages
[i
]);
310 kvfree(fw_priv
->pages
);
311 fw_priv
->pages
= NULL
;
312 fw_priv
->page_array_size
= 0;
313 fw_priv
->nr_pages
= 0;
316 int fw_grow_paged_buf(struct fw_priv
*fw_priv
, int pages_needed
)
318 /* If the array of pages is too small, grow it */
319 if (fw_priv
->page_array_size
< pages_needed
) {
320 int new_array_size
= max(pages_needed
,
321 fw_priv
->page_array_size
* 2);
322 struct page
**new_pages
;
324 new_pages
= kvmalloc_array(new_array_size
, sizeof(void *),
328 memcpy(new_pages
, fw_priv
->pages
,
329 fw_priv
->page_array_size
* sizeof(void *));
330 memset(&new_pages
[fw_priv
->page_array_size
], 0, sizeof(void *) *
331 (new_array_size
- fw_priv
->page_array_size
));
332 kvfree(fw_priv
->pages
);
333 fw_priv
->pages
= new_pages
;
334 fw_priv
->page_array_size
= new_array_size
;
337 while (fw_priv
->nr_pages
< pages_needed
) {
338 fw_priv
->pages
[fw_priv
->nr_pages
] =
339 alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
341 if (!fw_priv
->pages
[fw_priv
->nr_pages
])
349 int fw_map_paged_buf(struct fw_priv
*fw_priv
)
351 /* one pages buffer should be mapped/unmapped only once */
355 vunmap(fw_priv
->data
);
356 fw_priv
->data
= vmap(fw_priv
->pages
, fw_priv
->nr_pages
, 0,
366 * XZ-compressed firmware support
368 #ifdef CONFIG_FW_LOADER_COMPRESS
369 /* show an error and return the standard error code */
370 static int fw_decompress_xz_error(struct device
*dev
, enum xz_ret xz_ret
)
372 if (xz_ret
!= XZ_STREAM_END
) {
373 dev_warn(dev
, "xz decompression failed (xz_ret=%d)\n", xz_ret
);
374 return xz_ret
== XZ_MEM_ERROR
? -ENOMEM
: -EINVAL
;
379 /* single-shot decompression onto the pre-allocated buffer */
380 static int fw_decompress_xz_single(struct device
*dev
, struct fw_priv
*fw_priv
,
381 size_t in_size
, const void *in_buffer
)
383 struct xz_dec
*xz_dec
;
384 struct xz_buf xz_buf
;
387 xz_dec
= xz_dec_init(XZ_SINGLE
, (u32
)-1);
391 xz_buf
.in_size
= in_size
;
392 xz_buf
.in
= in_buffer
;
394 xz_buf
.out_size
= fw_priv
->allocated_size
;
395 xz_buf
.out
= fw_priv
->data
;
398 xz_ret
= xz_dec_run(xz_dec
, &xz_buf
);
401 fw_priv
->size
= xz_buf
.out_pos
;
402 return fw_decompress_xz_error(dev
, xz_ret
);
405 /* decompression on paged buffer and map it */
406 static int fw_decompress_xz_pages(struct device
*dev
, struct fw_priv
*fw_priv
,
407 size_t in_size
, const void *in_buffer
)
409 struct xz_dec
*xz_dec
;
410 struct xz_buf xz_buf
;
415 xz_dec
= xz_dec_init(XZ_DYNALLOC
, (u32
)-1);
419 xz_buf
.in_size
= in_size
;
420 xz_buf
.in
= in_buffer
;
423 fw_priv
->is_paged_buf
= true;
426 if (fw_grow_paged_buf(fw_priv
, fw_priv
->nr_pages
+ 1)) {
431 /* decompress onto the new allocated page */
432 page
= fw_priv
->pages
[fw_priv
->nr_pages
- 1];
433 xz_buf
.out
= kmap(page
);
435 xz_buf
.out_size
= PAGE_SIZE
;
436 xz_ret
= xz_dec_run(xz_dec
, &xz_buf
);
438 fw_priv
->size
+= xz_buf
.out_pos
;
439 /* partial decompression means either end or error */
440 if (xz_buf
.out_pos
!= PAGE_SIZE
)
442 } while (xz_ret
== XZ_OK
);
444 err
= fw_decompress_xz_error(dev
, xz_ret
);
446 err
= fw_map_paged_buf(fw_priv
);
453 static int fw_decompress_xz(struct device
*dev
, struct fw_priv
*fw_priv
,
454 size_t in_size
, const void *in_buffer
)
456 /* if the buffer is pre-allocated, we can perform in single-shot mode */
458 return fw_decompress_xz_single(dev
, fw_priv
, in_size
, in_buffer
);
460 return fw_decompress_xz_pages(dev
, fw_priv
, in_size
, in_buffer
);
462 #endif /* CONFIG_FW_LOADER_COMPRESS */
464 /* direct firmware loading support */
465 static char fw_path_para
[256];
466 static const char * const fw_path
[] = {
468 "/lib/firmware/updates/" UTS_RELEASE
,
469 "/lib/firmware/updates",
470 "/lib/firmware/" UTS_RELEASE
,
475 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
476 * from kernel command line because firmware_class is generally built in
477 * kernel instead of module.
479 module_param_string(path
, fw_path_para
, sizeof(fw_path_para
), 0644);
480 MODULE_PARM_DESC(path
, "customized firmware image search path with a higher priority than default path");
483 fw_get_filesystem_firmware(struct device
*device
, struct fw_priv
*fw_priv
,
485 int (*decompress
)(struct device
*dev
,
486 struct fw_priv
*fw_priv
,
488 const void *in_buffer
))
494 size_t msize
= INT_MAX
;
497 /* Already populated data member means we're loading into a buffer */
498 if (!decompress
&& fw_priv
->data
) {
499 buffer
= fw_priv
->data
;
500 msize
= fw_priv
->allocated_size
;
507 for (i
= 0; i
< ARRAY_SIZE(fw_path
); i
++) {
508 size_t file_size
= 0;
509 size_t *file_size_ptr
= NULL
;
511 /* skip the unset customized path */
515 len
= snprintf(path
, PATH_MAX
, "%s/%s%s",
516 fw_path
[i
], fw_priv
->fw_name
, suffix
);
517 if (len
>= PATH_MAX
) {
525 * The total file size is only examined when doing a partial
526 * read; the "full read" case needs to fail if the whole
527 * firmware was not completely loaded.
529 if ((fw_priv
->opt_flags
& FW_OPT_PARTIAL
) && buffer
)
530 file_size_ptr
= &file_size
;
532 /* load firmware files from the mount namespace of init */
533 rc
= kernel_read_file_from_path_initns(path
, fw_priv
->offset
,
539 dev_warn(device
, "loading %s failed with error %d\n",
542 dev_dbg(device
, "loading %s failed for no such file or directory.\n",
549 dev_dbg(device
, "Loading firmware from %s\n", path
);
551 dev_dbg(device
, "f/w decompressing %s\n",
553 rc
= decompress(device
, fw_priv
, size
, buffer
);
554 /* discard the superfluous original content */
558 fw_free_paged_buf(fw_priv
);
562 dev_dbg(device
, "direct-loading %s\n",
565 fw_priv
->data
= buffer
;
566 fw_priv
->size
= size
;
568 fw_state_done(fw_priv
);
576 /* firmware holds the ownership of pages */
577 static void firmware_free_data(const struct firmware
*fw
)
579 /* Loaded directly? */
584 free_fw_priv(fw
->priv
);
587 /* store the pages buffer info firmware from buf */
588 static void fw_set_page_data(struct fw_priv
*fw_priv
, struct firmware
*fw
)
591 fw
->size
= fw_priv
->size
;
592 fw
->data
= fw_priv
->data
;
594 pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
595 __func__
, fw_priv
->fw_name
, fw_priv
, fw_priv
->data
,
596 (unsigned int)fw_priv
->size
);
599 #ifdef CONFIG_FW_CACHE
600 static void fw_name_devm_release(struct device
*dev
, void *res
)
602 struct fw_name_devm
*fwn
= res
;
604 if (fwn
->magic
== (unsigned long)&fw_cache
)
605 pr_debug("%s: fw_name-%s devm-%p released\n",
606 __func__
, fwn
->name
, res
);
607 kfree_const(fwn
->name
);
610 static int fw_devm_match(struct device
*dev
, void *res
,
613 struct fw_name_devm
*fwn
= res
;
615 return (fwn
->magic
== (unsigned long)&fw_cache
) &&
616 !strcmp(fwn
->name
, match_data
);
619 static struct fw_name_devm
*fw_find_devm_name(struct device
*dev
,
622 struct fw_name_devm
*fwn
;
624 fwn
= devres_find(dev
, fw_name_devm_release
,
625 fw_devm_match
, (void *)name
);
629 static bool fw_cache_is_setup(struct device
*dev
, const char *name
)
631 struct fw_name_devm
*fwn
;
633 fwn
= fw_find_devm_name(dev
, name
);
640 /* add firmware name into devres list */
641 static int fw_add_devm_name(struct device
*dev
, const char *name
)
643 struct fw_name_devm
*fwn
;
645 if (fw_cache_is_setup(dev
, name
))
648 fwn
= devres_alloc(fw_name_devm_release
, sizeof(struct fw_name_devm
),
652 fwn
->name
= kstrdup_const(name
, GFP_KERNEL
);
658 fwn
->magic
= (unsigned long)&fw_cache
;
659 devres_add(dev
, fwn
);
664 static bool fw_cache_is_setup(struct device
*dev
, const char *name
)
669 static int fw_add_devm_name(struct device
*dev
, const char *name
)
675 int assign_fw(struct firmware
*fw
, struct device
*device
)
677 struct fw_priv
*fw_priv
= fw
->priv
;
680 mutex_lock(&fw_lock
);
681 if (!fw_priv
->size
|| fw_state_is_aborted(fw_priv
)) {
682 mutex_unlock(&fw_lock
);
687 * add firmware name into devres list so that we can auto cache
688 * and uncache firmware for device.
690 * device may has been deleted already, but the problem
691 * should be fixed in devres or driver core.
693 /* don't cache firmware handled without uevent */
694 if (device
&& (fw_priv
->opt_flags
& FW_OPT_UEVENT
) &&
695 !(fw_priv
->opt_flags
& FW_OPT_NOCACHE
)) {
696 ret
= fw_add_devm_name(device
, fw_priv
->fw_name
);
698 mutex_unlock(&fw_lock
);
704 * After caching firmware image is started, let it piggyback
705 * on request firmware.
707 if (!(fw_priv
->opt_flags
& FW_OPT_NOCACHE
) &&
708 fw_priv
->fwc
->state
== FW_LOADER_START_CACHE
) {
709 if (fw_cache_piggyback_on_request(fw_priv
->fw_name
))
710 kref_get(&fw_priv
->ref
);
713 /* pass the pages buffer to driver at the last minute */
714 fw_set_page_data(fw_priv
, fw
);
715 mutex_unlock(&fw_lock
);
719 /* prepare firmware and firmware_buf structs;
720 * return 0 if a firmware is already assigned, 1 if need to load one,
721 * or a negative error code
724 _request_firmware_prepare(struct firmware
**firmware_p
, const char *name
,
725 struct device
*device
, void *dbuf
, size_t size
,
726 size_t offset
, u32 opt_flags
)
728 struct firmware
*firmware
;
729 struct fw_priv
*fw_priv
;
732 *firmware_p
= firmware
= kzalloc(sizeof(*firmware
), GFP_KERNEL
);
734 dev_err(device
, "%s: kmalloc(struct firmware) failed\n",
739 if (fw_get_builtin_firmware(firmware
, name
, dbuf
, size
)) {
740 dev_dbg(device
, "using built-in %s\n", name
);
741 return 0; /* assigned */
744 ret
= alloc_lookup_fw_priv(name
, &fw_cache
, &fw_priv
, dbuf
, size
,
748 * bind with 'priv' now to avoid warning in failure path
749 * of requesting firmware.
751 firmware
->priv
= fw_priv
;
754 ret
= fw_state_wait(fw_priv
);
756 fw_set_page_data(fw_priv
, firmware
);
757 return 0; /* assigned */
763 return 1; /* need to load */
767 * Batched requests need only one wake, we need to do this step last due to the
768 * fallback mechanism. The buf is protected with kref_get(), and it won't be
769 * released until the last user calls release_firmware().
771 * Failed batched requests are possible as well, in such cases we just share
772 * the struct fw_priv and won't release it until all requests are woken
773 * and have gone through this same path.
775 static void fw_abort_batch_reqs(struct firmware
*fw
)
777 struct fw_priv
*fw_priv
;
779 /* Loaded directly? */
780 if (!fw
|| !fw
->priv
)
784 if (!fw_state_is_aborted(fw_priv
))
785 fw_state_aborted(fw_priv
);
788 /* called from request_firmware() and request_firmware_work_func() */
790 _request_firmware(const struct firmware
**firmware_p
, const char *name
,
791 struct device
*device
, void *buf
, size_t size
,
792 size_t offset
, u32 opt_flags
)
794 struct firmware
*fw
= NULL
;
795 bool nondirect
= false;
801 if (!name
|| name
[0] == '\0') {
806 ret
= _request_firmware_prepare(&fw
, name
, device
, buf
, size
,
808 if (ret
<= 0) /* error or already assigned */
811 ret
= fw_get_filesystem_firmware(device
, fw
->priv
, "", NULL
);
813 /* Only full reads can support decompression, platform, and sysfs. */
814 if (!(opt_flags
& FW_OPT_PARTIAL
))
817 #ifdef CONFIG_FW_LOADER_COMPRESS
818 if (ret
== -ENOENT
&& nondirect
)
819 ret
= fw_get_filesystem_firmware(device
, fw
->priv
, ".xz",
822 if (ret
== -ENOENT
&& nondirect
)
823 ret
= firmware_fallback_platform(fw
->priv
);
826 if (!(opt_flags
& FW_OPT_NO_WARN
))
828 "Direct firmware load for %s failed with error %d\n",
831 ret
= firmware_fallback_sysfs(fw
, name
, device
,
834 ret
= assign_fw(fw
, device
);
838 fw_abort_batch_reqs(fw
);
839 release_firmware(fw
);
848 * request_firmware() - send firmware request and wait for it
849 * @firmware_p: pointer to firmware image
850 * @name: name of firmware file
851 * @device: device for which firmware is being loaded
853 * @firmware_p will be used to return a firmware image by the name
854 * of @name for device @device.
856 * Should be called from user context where sleeping is allowed.
858 * @name will be used as $FIRMWARE in the uevent environment and
859 * should be distinctive enough not to be confused with any other
860 * firmware image for this or any other device.
862 * Caller must hold the reference count of @device.
864 * The function can be called safely inside device's suspend and
868 request_firmware(const struct firmware
**firmware_p
, const char *name
,
869 struct device
*device
)
873 /* Need to pin this module until return */
874 __module_get(THIS_MODULE
);
875 ret
= _request_firmware(firmware_p
, name
, device
, NULL
, 0, 0,
877 module_put(THIS_MODULE
);
880 EXPORT_SYMBOL(request_firmware
);
883 * firmware_request_nowarn() - request for an optional fw module
884 * @firmware: pointer to firmware image
885 * @name: name of firmware file
886 * @device: device for which firmware is being loaded
888 * This function is similar in behaviour to request_firmware(), except it
889 * doesn't produce warning messages when the file is not found. The sysfs
890 * fallback mechanism is enabled if direct filesystem lookup fails. However,
891 * failures to find the firmware file with it are still suppressed. It is
892 * therefore up to the driver to check for the return value of this call and to
893 * decide when to inform the users of errors.
895 int firmware_request_nowarn(const struct firmware
**firmware
, const char *name
,
896 struct device
*device
)
900 /* Need to pin this module until return */
901 __module_get(THIS_MODULE
);
902 ret
= _request_firmware(firmware
, name
, device
, NULL
, 0, 0,
903 FW_OPT_UEVENT
| FW_OPT_NO_WARN
);
904 module_put(THIS_MODULE
);
907 EXPORT_SYMBOL_GPL(firmware_request_nowarn
);
910 * request_firmware_direct() - load firmware directly without usermode helper
911 * @firmware_p: pointer to firmware image
912 * @name: name of firmware file
913 * @device: device for which firmware is being loaded
915 * This function works pretty much like request_firmware(), but this doesn't
916 * fall back to usermode helper even if the firmware couldn't be loaded
917 * directly from fs. Hence it's useful for loading optional firmwares, which
918 * aren't always present, without extra long timeouts of udev.
920 int request_firmware_direct(const struct firmware
**firmware_p
,
921 const char *name
, struct device
*device
)
925 __module_get(THIS_MODULE
);
926 ret
= _request_firmware(firmware_p
, name
, device
, NULL
, 0, 0,
927 FW_OPT_UEVENT
| FW_OPT_NO_WARN
|
928 FW_OPT_NOFALLBACK_SYSFS
);
929 module_put(THIS_MODULE
);
932 EXPORT_SYMBOL_GPL(request_firmware_direct
);
935 * firmware_request_platform() - request firmware with platform-fw fallback
936 * @firmware: pointer to firmware image
937 * @name: name of firmware file
938 * @device: device for which firmware is being loaded
940 * This function is similar in behaviour to request_firmware, except that if
941 * direct filesystem lookup fails, it will fallback to looking for a copy of the
942 * requested firmware embedded in the platform's main (e.g. UEFI) firmware.
944 int firmware_request_platform(const struct firmware
**firmware
,
945 const char *name
, struct device
*device
)
949 /* Need to pin this module until return */
950 __module_get(THIS_MODULE
);
951 ret
= _request_firmware(firmware
, name
, device
, NULL
, 0, 0,
952 FW_OPT_UEVENT
| FW_OPT_FALLBACK_PLATFORM
);
953 module_put(THIS_MODULE
);
956 EXPORT_SYMBOL_GPL(firmware_request_platform
);
959 * firmware_request_cache() - cache firmware for suspend so resume can use it
960 * @name: name of firmware file
961 * @device: device for which firmware should be cached for
963 * There are some devices with an optimization that enables the device to not
964 * require loading firmware on system reboot. This optimization may still
965 * require the firmware present on resume from suspend. This routine can be
966 * used to ensure the firmware is present on resume from suspend in these
967 * situations. This helper is not compatible with drivers which use
968 * request_firmware_into_buf() or request_firmware_nowait() with no uevent set.
970 int firmware_request_cache(struct device
*device
, const char *name
)
974 mutex_lock(&fw_lock
);
975 ret
= fw_add_devm_name(device
, name
);
976 mutex_unlock(&fw_lock
);
980 EXPORT_SYMBOL_GPL(firmware_request_cache
);
983 * request_firmware_into_buf() - load firmware into a previously allocated buffer
984 * @firmware_p: pointer to firmware image
985 * @name: name of firmware file
986 * @device: device for which firmware is being loaded and DMA region allocated
987 * @buf: address of buffer to load firmware into
988 * @size: size of buffer
990 * This function works pretty much like request_firmware(), but it doesn't
991 * allocate a buffer to hold the firmware data. Instead, the firmware
992 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
993 * data member is pointed at @buf.
995 * This function doesn't cache firmware either.
998 request_firmware_into_buf(const struct firmware
**firmware_p
, const char *name
,
999 struct device
*device
, void *buf
, size_t size
)
1003 if (fw_cache_is_setup(device
, name
))
1006 __module_get(THIS_MODULE
);
1007 ret
= _request_firmware(firmware_p
, name
, device
, buf
, size
, 0,
1008 FW_OPT_UEVENT
| FW_OPT_NOCACHE
);
1009 module_put(THIS_MODULE
);
1012 EXPORT_SYMBOL(request_firmware_into_buf
);
1015 * request_partial_firmware_into_buf() - load partial firmware into a previously allocated buffer
1016 * @firmware_p: pointer to firmware image
1017 * @name: name of firmware file
1018 * @device: device for which firmware is being loaded and DMA region allocated
1019 * @buf: address of buffer to load firmware into
1020 * @size: size of buffer
1021 * @offset: offset into file to read
1023 * This function works pretty much like request_firmware_into_buf except
1024 * it allows a partial read of the file.
1027 request_partial_firmware_into_buf(const struct firmware
**firmware_p
,
1028 const char *name
, struct device
*device
,
1029 void *buf
, size_t size
, size_t offset
)
1033 if (fw_cache_is_setup(device
, name
))
1036 __module_get(THIS_MODULE
);
1037 ret
= _request_firmware(firmware_p
, name
, device
, buf
, size
, offset
,
1038 FW_OPT_UEVENT
| FW_OPT_NOCACHE
|
1040 module_put(THIS_MODULE
);
1043 EXPORT_SYMBOL(request_partial_firmware_into_buf
);
1046 * release_firmware() - release the resource associated with a firmware image
1047 * @fw: firmware resource to release
1049 void release_firmware(const struct firmware
*fw
)
1052 if (!fw_is_builtin_firmware(fw
))
1053 firmware_free_data(fw
);
1057 EXPORT_SYMBOL(release_firmware
);
1060 struct firmware_work
{
1061 struct work_struct work
;
1062 struct module
*module
;
1064 struct device
*device
;
1066 void (*cont
)(const struct firmware
*fw
, void *context
);
1070 static void request_firmware_work_func(struct work_struct
*work
)
1072 struct firmware_work
*fw_work
;
1073 const struct firmware
*fw
;
1075 fw_work
= container_of(work
, struct firmware_work
, work
);
1077 _request_firmware(&fw
, fw_work
->name
, fw_work
->device
, NULL
, 0, 0,
1078 fw_work
->opt_flags
);
1079 fw_work
->cont(fw
, fw_work
->context
);
1080 put_device(fw_work
->device
); /* taken in request_firmware_nowait() */
1082 module_put(fw_work
->module
);
1083 kfree_const(fw_work
->name
);
1088 * request_firmware_nowait() - asynchronous version of request_firmware
1089 * @module: module requesting the firmware
1090 * @uevent: sends uevent to copy the firmware image if this flag
1091 * is non-zero else the firmware copy must be done manually.
1092 * @name: name of firmware file
1093 * @device: device for which firmware is being loaded
1094 * @gfp: allocation flags
1095 * @context: will be passed over to @cont, and
1096 * @fw may be %NULL if firmware request fails.
1097 * @cont: function will be called asynchronously when the firmware
1100 * Caller must hold the reference count of @device.
1102 * Asynchronous variant of request_firmware() for user contexts:
1103 * - sleep for as small periods as possible since it may
1104 * increase kernel boot time of built-in device drivers
1105 * requesting firmware in their ->probe() methods, if
1106 * @gfp is GFP_KERNEL.
1108 * - can't sleep at all if @gfp is GFP_ATOMIC.
1111 request_firmware_nowait(
1112 struct module
*module
, bool uevent
,
1113 const char *name
, struct device
*device
, gfp_t gfp
, void *context
,
1114 void (*cont
)(const struct firmware
*fw
, void *context
))
1116 struct firmware_work
*fw_work
;
1118 fw_work
= kzalloc(sizeof(struct firmware_work
), gfp
);
1122 fw_work
->module
= module
;
1123 fw_work
->name
= kstrdup_const(name
, gfp
);
1124 if (!fw_work
->name
) {
1128 fw_work
->device
= device
;
1129 fw_work
->context
= context
;
1130 fw_work
->cont
= cont
;
1131 fw_work
->opt_flags
= FW_OPT_NOWAIT
|
1132 (uevent
? FW_OPT_UEVENT
: FW_OPT_USERHELPER
);
1134 if (!uevent
&& fw_cache_is_setup(device
, name
)) {
1135 kfree_const(fw_work
->name
);
1140 if (!try_module_get(module
)) {
1141 kfree_const(fw_work
->name
);
1146 get_device(fw_work
->device
);
1147 INIT_WORK(&fw_work
->work
, request_firmware_work_func
);
1148 schedule_work(&fw_work
->work
);
1151 EXPORT_SYMBOL(request_firmware_nowait
);
1153 #ifdef CONFIG_FW_CACHE
1154 static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain
);
1157 * cache_firmware() - cache one firmware image in kernel memory space
1158 * @fw_name: the firmware image name
1160 * Cache firmware in kernel memory so that drivers can use it when
1161 * system isn't ready for them to request firmware image from userspace.
1162 * Once it returns successfully, driver can use request_firmware or its
1163 * nowait version to get the cached firmware without any interacting
1166 * Return 0 if the firmware image has been cached successfully
1167 * Return !0 otherwise
1170 static int cache_firmware(const char *fw_name
)
1173 const struct firmware
*fw
;
1175 pr_debug("%s: %s\n", __func__
, fw_name
);
1177 ret
= request_firmware(&fw
, fw_name
, NULL
);
1181 pr_debug("%s: %s ret=%d\n", __func__
, fw_name
, ret
);
1186 static struct fw_priv
*lookup_fw_priv(const char *fw_name
)
1188 struct fw_priv
*tmp
;
1189 struct firmware_cache
*fwc
= &fw_cache
;
1191 spin_lock(&fwc
->lock
);
1192 tmp
= __lookup_fw_priv(fw_name
);
1193 spin_unlock(&fwc
->lock
);
1199 * uncache_firmware() - remove one cached firmware image
1200 * @fw_name: the firmware image name
1202 * Uncache one firmware image which has been cached successfully
1205 * Return 0 if the firmware cache has been removed successfully
1206 * Return !0 otherwise
1209 static int uncache_firmware(const char *fw_name
)
1211 struct fw_priv
*fw_priv
;
1214 pr_debug("%s: %s\n", __func__
, fw_name
);
1216 if (fw_get_builtin_firmware(&fw
, fw_name
, NULL
, 0))
1219 fw_priv
= lookup_fw_priv(fw_name
);
1221 free_fw_priv(fw_priv
);
1228 static struct fw_cache_entry
*alloc_fw_cache_entry(const char *name
)
1230 struct fw_cache_entry
*fce
;
1232 fce
= kzalloc(sizeof(*fce
), GFP_ATOMIC
);
1236 fce
->name
= kstrdup_const(name
, GFP_ATOMIC
);
1246 static int __fw_entry_found(const char *name
)
1248 struct firmware_cache
*fwc
= &fw_cache
;
1249 struct fw_cache_entry
*fce
;
1251 list_for_each_entry(fce
, &fwc
->fw_names
, list
) {
1252 if (!strcmp(fce
->name
, name
))
1258 static int fw_cache_piggyback_on_request(const char *name
)
1260 struct firmware_cache
*fwc
= &fw_cache
;
1261 struct fw_cache_entry
*fce
;
1264 spin_lock(&fwc
->name_lock
);
1265 if (__fw_entry_found(name
))
1268 fce
= alloc_fw_cache_entry(name
);
1271 list_add(&fce
->list
, &fwc
->fw_names
);
1272 pr_debug("%s: fw: %s\n", __func__
, name
);
1275 spin_unlock(&fwc
->name_lock
);
1279 static void free_fw_cache_entry(struct fw_cache_entry
*fce
)
1281 kfree_const(fce
->name
);
1285 static void __async_dev_cache_fw_image(void *fw_entry
,
1286 async_cookie_t cookie
)
1288 struct fw_cache_entry
*fce
= fw_entry
;
1289 struct firmware_cache
*fwc
= &fw_cache
;
1292 ret
= cache_firmware(fce
->name
);
1294 spin_lock(&fwc
->name_lock
);
1295 list_del(&fce
->list
);
1296 spin_unlock(&fwc
->name_lock
);
1298 free_fw_cache_entry(fce
);
1302 /* called with dev->devres_lock held */
1303 static void dev_create_fw_entry(struct device
*dev
, void *res
,
1306 struct fw_name_devm
*fwn
= res
;
1307 const char *fw_name
= fwn
->name
;
1308 struct list_head
*head
= data
;
1309 struct fw_cache_entry
*fce
;
1311 fce
= alloc_fw_cache_entry(fw_name
);
1313 list_add(&fce
->list
, head
);
1316 static int devm_name_match(struct device
*dev
, void *res
,
1319 struct fw_name_devm
*fwn
= res
;
1320 return (fwn
->magic
== (unsigned long)match_data
);
1323 static void dev_cache_fw_image(struct device
*dev
, void *data
)
1326 struct fw_cache_entry
*fce
;
1327 struct fw_cache_entry
*fce_next
;
1328 struct firmware_cache
*fwc
= &fw_cache
;
1330 devres_for_each_res(dev
, fw_name_devm_release
,
1331 devm_name_match
, &fw_cache
,
1332 dev_create_fw_entry
, &todo
);
1334 list_for_each_entry_safe(fce
, fce_next
, &todo
, list
) {
1335 list_del(&fce
->list
);
1337 spin_lock(&fwc
->name_lock
);
1338 /* only one cache entry for one firmware */
1339 if (!__fw_entry_found(fce
->name
)) {
1340 list_add(&fce
->list
, &fwc
->fw_names
);
1342 free_fw_cache_entry(fce
);
1345 spin_unlock(&fwc
->name_lock
);
1348 async_schedule_domain(__async_dev_cache_fw_image
,
1354 static void __device_uncache_fw_images(void)
1356 struct firmware_cache
*fwc
= &fw_cache
;
1357 struct fw_cache_entry
*fce
;
1359 spin_lock(&fwc
->name_lock
);
1360 while (!list_empty(&fwc
->fw_names
)) {
1361 fce
= list_entry(fwc
->fw_names
.next
,
1362 struct fw_cache_entry
, list
);
1363 list_del(&fce
->list
);
1364 spin_unlock(&fwc
->name_lock
);
1366 uncache_firmware(fce
->name
);
1367 free_fw_cache_entry(fce
);
1369 spin_lock(&fwc
->name_lock
);
1371 spin_unlock(&fwc
->name_lock
);
1375 * device_cache_fw_images() - cache devices' firmware
1377 * If one device called request_firmware or its nowait version
1378 * successfully before, the firmware names are recored into the
1379 * device's devres link list, so device_cache_fw_images can call
1380 * cache_firmware() to cache these firmwares for the device,
1381 * then the device driver can load its firmwares easily at
1382 * time when system is not ready to complete loading firmware.
1384 static void device_cache_fw_images(void)
1386 struct firmware_cache
*fwc
= &fw_cache
;
1389 pr_debug("%s\n", __func__
);
1391 /* cancel uncache work */
1392 cancel_delayed_work_sync(&fwc
->work
);
1394 fw_fallback_set_cache_timeout();
1396 mutex_lock(&fw_lock
);
1397 fwc
->state
= FW_LOADER_START_CACHE
;
1398 dpm_for_each_dev(NULL
, dev_cache_fw_image
);
1399 mutex_unlock(&fw_lock
);
1401 /* wait for completion of caching firmware for all devices */
1402 async_synchronize_full_domain(&fw_cache_domain
);
1404 fw_fallback_set_default_timeout();
1408 * device_uncache_fw_images() - uncache devices' firmware
1410 * uncache all firmwares which have been cached successfully
1411 * by device_uncache_fw_images earlier
1413 static void device_uncache_fw_images(void)
1415 pr_debug("%s\n", __func__
);
1416 __device_uncache_fw_images();
1419 static void device_uncache_fw_images_work(struct work_struct
*work
)
1421 device_uncache_fw_images();
1425 * device_uncache_fw_images_delay() - uncache devices firmwares
1426 * @delay: number of milliseconds to delay uncache device firmwares
1428 * uncache all devices's firmwares which has been cached successfully
1429 * by device_cache_fw_images after @delay milliseconds.
1431 static void device_uncache_fw_images_delay(unsigned long delay
)
1433 queue_delayed_work(system_power_efficient_wq
, &fw_cache
.work
,
1434 msecs_to_jiffies(delay
));
1437 static int fw_pm_notify(struct notifier_block
*notify_block
,
1438 unsigned long mode
, void *unused
)
1441 case PM_HIBERNATION_PREPARE
:
1442 case PM_SUSPEND_PREPARE
:
1443 case PM_RESTORE_PREPARE
:
1445 * kill pending fallback requests with a custom fallback
1446 * to avoid stalling suspend.
1448 kill_pending_fw_fallback_reqs(true);
1449 device_cache_fw_images();
1452 case PM_POST_SUSPEND
:
1453 case PM_POST_HIBERNATION
:
1454 case PM_POST_RESTORE
:
1456 * In case that system sleep failed and syscore_suspend is
1459 mutex_lock(&fw_lock
);
1460 fw_cache
.state
= FW_LOADER_NO_CACHE
;
1461 mutex_unlock(&fw_lock
);
1463 device_uncache_fw_images_delay(10 * MSEC_PER_SEC
);
1470 /* stop caching firmware once syscore_suspend is reached */
1471 static int fw_suspend(void)
1473 fw_cache
.state
= FW_LOADER_NO_CACHE
;
1477 static struct syscore_ops fw_syscore_ops
= {
1478 .suspend
= fw_suspend
,
1481 static int __init
register_fw_pm_ops(void)
1485 spin_lock_init(&fw_cache
.name_lock
);
1486 INIT_LIST_HEAD(&fw_cache
.fw_names
);
1488 INIT_DELAYED_WORK(&fw_cache
.work
,
1489 device_uncache_fw_images_work
);
1491 fw_cache
.pm_notify
.notifier_call
= fw_pm_notify
;
1492 ret
= register_pm_notifier(&fw_cache
.pm_notify
);
1496 register_syscore_ops(&fw_syscore_ops
);
1501 static inline void unregister_fw_pm_ops(void)
1503 unregister_syscore_ops(&fw_syscore_ops
);
1504 unregister_pm_notifier(&fw_cache
.pm_notify
);
1507 static int fw_cache_piggyback_on_request(const char *name
)
1511 static inline int register_fw_pm_ops(void)
1515 static inline void unregister_fw_pm_ops(void)
1520 static void __init
fw_cache_init(void)
1522 spin_lock_init(&fw_cache
.lock
);
1523 INIT_LIST_HEAD(&fw_cache
.head
);
1524 fw_cache
.state
= FW_LOADER_NO_CACHE
;
1527 static int fw_shutdown_notify(struct notifier_block
*unused1
,
1528 unsigned long unused2
, void *unused3
)
1531 * Kill all pending fallback requests to avoid both stalling shutdown,
1532 * and avoid a deadlock with the usermode_lock.
1534 kill_pending_fw_fallback_reqs(false);
1539 static struct notifier_block fw_shutdown_nb
= {
1540 .notifier_call
= fw_shutdown_notify
,
1543 static int __init
firmware_class_init(void)
1547 /* No need to unfold these on exit */
1550 ret
= register_fw_pm_ops();
1554 ret
= register_reboot_notifier(&fw_shutdown_nb
);
1558 return register_sysfs_loader();
1561 unregister_fw_pm_ops();
1565 static void __exit
firmware_class_exit(void)
1567 unregister_fw_pm_ops();
1568 unregister_reboot_notifier(&fw_shutdown_nb
);
1569 unregister_sysfs_loader();
1572 fs_initcall(firmware_class_init
);
1573 module_exit(firmware_class_exit
);