2 * firmware_class.c - Multi purpose firmware loading support
4 * Copyright (c) 2003 Manuel Estrada Sainz
6 * Please see Documentation/firmware_class/ for more information.
10 #include <linux/capability.h>
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/timer.h>
15 #include <linux/vmalloc.h>
16 #include <linux/interrupt.h>
17 #include <linux/bitops.h>
18 #include <linux/mutex.h>
19 #include <linux/workqueue.h>
20 #include <linux/highmem.h>
21 #include <linux/firmware.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <linux/file.h>
25 #include <linux/list.h>
27 #include <linux/async.h>
29 #include <linux/suspend.h>
30 #include <linux/syscore_ops.h>
31 #include <linux/reboot.h>
32 #include <linux/security.h>
34 #include <generated/utsrelease.h>
38 MODULE_AUTHOR("Manuel Estrada Sainz");
39 MODULE_DESCRIPTION("Multi purpose firmware loading support");
40 MODULE_LICENSE("GPL");
42 /* Builtin firmware support */
44 #ifdef CONFIG_FW_LOADER
46 extern struct builtin_fw __start_builtin_fw
[];
47 extern struct builtin_fw __end_builtin_fw
[];
49 static bool fw_get_builtin_firmware(struct firmware
*fw
, const char *name
,
50 void *buf
, size_t size
)
52 struct builtin_fw
*b_fw
;
54 for (b_fw
= __start_builtin_fw
; b_fw
!= __end_builtin_fw
; b_fw
++) {
55 if (strcmp(name
, b_fw
->name
) == 0) {
56 fw
->size
= b_fw
->size
;
57 fw
->data
= b_fw
->data
;
59 if (buf
&& fw
->size
<= size
)
60 memcpy(buf
, fw
->data
, fw
->size
);
68 static bool fw_is_builtin_firmware(const struct firmware
*fw
)
70 struct builtin_fw
*b_fw
;
72 for (b_fw
= __start_builtin_fw
; b_fw
!= __end_builtin_fw
; b_fw
++)
73 if (fw
->data
== b_fw
->data
)
79 #else /* Module case - no builtin firmware support */
81 static inline bool fw_get_builtin_firmware(struct firmware
*fw
,
82 const char *name
, void *buf
,
88 static inline bool fw_is_builtin_firmware(const struct firmware
*fw
)
101 static int loading_timeout
= 60; /* In seconds */
103 static inline long firmware_loading_timeout(void)
105 return loading_timeout
> 0 ? loading_timeout
* HZ
: MAX_JIFFY_OFFSET
;
109 * Concurrent request_firmware() for the same firmware need to be
110 * serialized. struct fw_state is simple state machine which hold the
111 * state of the firmware loading.
114 struct completion completion
;
115 enum fw_status status
;
118 static void fw_state_init(struct fw_state
*fw_st
)
120 init_completion(&fw_st
->completion
);
121 fw_st
->status
= FW_STATUS_UNKNOWN
;
124 static inline bool __fw_state_is_done(enum fw_status status
)
126 return status
== FW_STATUS_DONE
|| status
== FW_STATUS_ABORTED
;
129 static int __fw_state_wait_common(struct fw_state
*fw_st
, long timeout
)
133 ret
= wait_for_completion_killable_timeout(&fw_st
->completion
, timeout
);
134 if (ret
!= 0 && fw_st
->status
== FW_STATUS_ABORTED
)
139 return ret
< 0 ? ret
: 0;
142 static void __fw_state_set(struct fw_state
*fw_st
,
143 enum fw_status status
)
145 WRITE_ONCE(fw_st
->status
, status
);
147 if (status
== FW_STATUS_DONE
|| status
== FW_STATUS_ABORTED
)
148 complete_all(&fw_st
->completion
);
151 #define fw_state_start(fw_st) \
152 __fw_state_set(fw_st, FW_STATUS_LOADING)
153 #define fw_state_done(fw_st) \
154 __fw_state_set(fw_st, FW_STATUS_DONE)
155 #define fw_state_aborted(fw_st) \
156 __fw_state_set(fw_st, FW_STATUS_ABORTED)
157 #define fw_state_wait(fw_st) \
158 __fw_state_wait_common(fw_st, MAX_SCHEDULE_TIMEOUT)
160 static int __fw_state_check(struct fw_state
*fw_st
, enum fw_status status
)
162 return fw_st
->status
== status
;
165 #define fw_state_is_aborted(fw_st) \
166 __fw_state_check(fw_st, FW_STATUS_ABORTED)
168 #ifdef CONFIG_FW_LOADER_USER_HELPER
170 #define fw_state_aborted(fw_st) \
171 __fw_state_set(fw_st, FW_STATUS_ABORTED)
172 #define fw_state_is_done(fw_st) \
173 __fw_state_check(fw_st, FW_STATUS_DONE)
174 #define fw_state_is_loading(fw_st) \
175 __fw_state_check(fw_st, FW_STATUS_LOADING)
176 #define fw_state_wait_timeout(fw_st, timeout) \
177 __fw_state_wait_common(fw_st, timeout)
179 #endif /* CONFIG_FW_LOADER_USER_HELPER */
181 /* firmware behavior options */
182 #define FW_OPT_UEVENT (1U << 0)
183 #define FW_OPT_NOWAIT (1U << 1)
184 #ifdef CONFIG_FW_LOADER_USER_HELPER
185 #define FW_OPT_USERHELPER (1U << 2)
187 #define FW_OPT_USERHELPER 0
189 #ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK
190 #define FW_OPT_FALLBACK FW_OPT_USERHELPER
192 #define FW_OPT_FALLBACK 0
194 #define FW_OPT_NO_WARN (1U << 3)
195 #define FW_OPT_NOCACHE (1U << 4)
197 struct firmware_cache
{
198 /* firmware_buf instance will be added into the below list */
200 struct list_head head
;
203 #ifdef CONFIG_PM_SLEEP
205 * Names of firmware images which have been cached successfully
206 * will be added into the below list so that device uncache
207 * helper can trace which firmware images have been cached
210 spinlock_t name_lock
;
211 struct list_head fw_names
;
213 struct delayed_work work
;
215 struct notifier_block pm_notify
;
219 struct firmware_buf
{
221 struct list_head list
;
222 struct firmware_cache
*fwc
;
223 struct fw_state fw_st
;
226 size_t allocated_size
;
227 #ifdef CONFIG_FW_LOADER_USER_HELPER
233 struct list_head pending_list
;
238 struct fw_cache_entry
{
239 struct list_head list
;
243 struct fw_name_devm
{
248 #define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
250 #define FW_LOADER_NO_CACHE 0
251 #define FW_LOADER_START_CACHE 1
253 static int fw_cache_piggyback_on_request(const char *name
);
255 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
256 * guarding for corner cases a global lock should be OK */
257 static DEFINE_MUTEX(fw_lock
);
259 static bool __enable_firmware
= false;
261 static void enable_firmware(void)
263 mutex_lock(&fw_lock
);
264 __enable_firmware
= true;
265 mutex_unlock(&fw_lock
);
268 static void disable_firmware(void)
270 mutex_lock(&fw_lock
);
271 __enable_firmware
= false;
272 mutex_unlock(&fw_lock
);
276 * When disabled only the built-in firmware and the firmware cache will be
277 * used to look for firmware.
279 static bool firmware_enabled(void)
281 bool enabled
= false;
283 mutex_lock(&fw_lock
);
284 if (__enable_firmware
)
286 mutex_unlock(&fw_lock
);
291 static struct firmware_cache fw_cache
;
293 static struct firmware_buf
*__allocate_fw_buf(const char *fw_name
,
294 struct firmware_cache
*fwc
,
295 void *dbuf
, size_t size
)
297 struct firmware_buf
*buf
;
299 buf
= kzalloc(sizeof(*buf
), GFP_ATOMIC
);
303 buf
->fw_id
= kstrdup_const(fw_name
, GFP_ATOMIC
);
309 kref_init(&buf
->ref
);
312 buf
->allocated_size
= size
;
313 fw_state_init(&buf
->fw_st
);
314 #ifdef CONFIG_FW_LOADER_USER_HELPER
315 INIT_LIST_HEAD(&buf
->pending_list
);
318 pr_debug("%s: fw-%s buf=%p\n", __func__
, fw_name
, buf
);
323 static struct firmware_buf
*__fw_lookup_buf(const char *fw_name
)
325 struct firmware_buf
*tmp
;
326 struct firmware_cache
*fwc
= &fw_cache
;
328 list_for_each_entry(tmp
, &fwc
->head
, list
)
329 if (!strcmp(tmp
->fw_id
, fw_name
))
334 static int fw_lookup_and_allocate_buf(const char *fw_name
,
335 struct firmware_cache
*fwc
,
336 struct firmware_buf
**buf
, void *dbuf
,
339 struct firmware_buf
*tmp
;
341 spin_lock(&fwc
->lock
);
342 tmp
= __fw_lookup_buf(fw_name
);
345 spin_unlock(&fwc
->lock
);
349 tmp
= __allocate_fw_buf(fw_name
, fwc
, dbuf
, size
);
351 list_add(&tmp
->list
, &fwc
->head
);
352 spin_unlock(&fwc
->lock
);
356 return tmp
? 0 : -ENOMEM
;
359 static void __fw_free_buf(struct kref
*ref
)
360 __releases(&fwc
->lock
)
362 struct firmware_buf
*buf
= to_fwbuf(ref
);
363 struct firmware_cache
*fwc
= buf
->fwc
;
365 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
366 __func__
, buf
->fw_id
, buf
, buf
->data
,
367 (unsigned int)buf
->size
);
369 list_del(&buf
->list
);
370 spin_unlock(&fwc
->lock
);
372 #ifdef CONFIG_FW_LOADER_USER_HELPER
373 if (buf
->is_paged_buf
) {
376 for (i
= 0; i
< buf
->nr_pages
; i
++)
377 __free_page(buf
->pages
[i
]);
381 if (!buf
->allocated_size
)
383 kfree_const(buf
->fw_id
);
387 static void fw_free_buf(struct firmware_buf
*buf
)
389 struct firmware_cache
*fwc
= buf
->fwc
;
390 spin_lock(&fwc
->lock
);
391 if (!kref_put(&buf
->ref
, __fw_free_buf
))
392 spin_unlock(&fwc
->lock
);
395 /* direct firmware loading support */
396 static char fw_path_para
[256];
397 static const char * const fw_path
[] = {
399 "/lib/firmware/updates/" UTS_RELEASE
,
400 "/lib/firmware/updates",
401 "/lib/firmware/" UTS_RELEASE
,
406 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
407 * from kernel command line because firmware_class is generally built in
408 * kernel instead of module.
410 module_param_string(path
, fw_path_para
, sizeof(fw_path_para
), 0644);
411 MODULE_PARM_DESC(path
, "customized firmware image search path with a higher priority than default path");
414 fw_get_filesystem_firmware(struct device
*device
, struct firmware_buf
*buf
)
420 enum kernel_read_file_id id
= READING_FIRMWARE
;
421 size_t msize
= INT_MAX
;
423 /* Already populated data member means we're loading into a buffer */
425 id
= READING_FIRMWARE_PREALLOC_BUFFER
;
426 msize
= buf
->allocated_size
;
433 for (i
= 0; i
< ARRAY_SIZE(fw_path
); i
++) {
434 /* skip the unset customized path */
438 len
= snprintf(path
, PATH_MAX
, "%s/%s",
439 fw_path
[i
], buf
->fw_id
);
440 if (len
>= PATH_MAX
) {
446 rc
= kernel_read_file_from_path(path
, &buf
->data
, &size
, msize
,
450 dev_dbg(device
, "loading %s failed with error %d\n",
453 dev_warn(device
, "loading %s failed with error %d\n",
457 dev_dbg(device
, "direct-loading %s\n", buf
->fw_id
);
459 fw_state_done(&buf
->fw_st
);
467 /* firmware holds the ownership of pages */
468 static void firmware_free_data(const struct firmware
*fw
)
470 /* Loaded directly? */
475 fw_free_buf(fw
->priv
);
478 /* store the pages buffer info firmware from buf */
479 static void fw_set_page_data(struct firmware_buf
*buf
, struct firmware
*fw
)
482 #ifdef CONFIG_FW_LOADER_USER_HELPER
483 fw
->pages
= buf
->pages
;
485 fw
->size
= buf
->size
;
486 fw
->data
= buf
->data
;
488 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
489 __func__
, buf
->fw_id
, buf
, buf
->data
,
490 (unsigned int)buf
->size
);
493 #ifdef CONFIG_PM_SLEEP
494 static void fw_name_devm_release(struct device
*dev
, void *res
)
496 struct fw_name_devm
*fwn
= res
;
498 if (fwn
->magic
== (unsigned long)&fw_cache
)
499 pr_debug("%s: fw_name-%s devm-%p released\n",
500 __func__
, fwn
->name
, res
);
501 kfree_const(fwn
->name
);
504 static int fw_devm_match(struct device
*dev
, void *res
,
507 struct fw_name_devm
*fwn
= res
;
509 return (fwn
->magic
== (unsigned long)&fw_cache
) &&
510 !strcmp(fwn
->name
, match_data
);
513 static struct fw_name_devm
*fw_find_devm_name(struct device
*dev
,
516 struct fw_name_devm
*fwn
;
518 fwn
= devres_find(dev
, fw_name_devm_release
,
519 fw_devm_match
, (void *)name
);
523 /* add firmware name into devres list */
524 static int fw_add_devm_name(struct device
*dev
, const char *name
)
526 struct fw_name_devm
*fwn
;
528 fwn
= fw_find_devm_name(dev
, name
);
532 fwn
= devres_alloc(fw_name_devm_release
, sizeof(struct fw_name_devm
),
536 fwn
->name
= kstrdup_const(name
, GFP_KERNEL
);
542 fwn
->magic
= (unsigned long)&fw_cache
;
543 devres_add(dev
, fwn
);
548 static int fw_add_devm_name(struct device
*dev
, const char *name
)
554 static int assign_firmware_buf(struct firmware
*fw
, struct device
*device
,
555 unsigned int opt_flags
)
557 struct firmware_buf
*buf
= fw
->priv
;
559 mutex_lock(&fw_lock
);
560 if (!buf
->size
|| fw_state_is_aborted(&buf
->fw_st
)) {
561 mutex_unlock(&fw_lock
);
566 * add firmware name into devres list so that we can auto cache
567 * and uncache firmware for device.
569 * device may has been deleted already, but the problem
570 * should be fixed in devres or driver core.
572 /* don't cache firmware handled without uevent */
573 if (device
&& (opt_flags
& FW_OPT_UEVENT
) &&
574 !(opt_flags
& FW_OPT_NOCACHE
))
575 fw_add_devm_name(device
, buf
->fw_id
);
578 * After caching firmware image is started, let it piggyback
579 * on request firmware.
581 if (!(opt_flags
& FW_OPT_NOCACHE
) &&
582 buf
->fwc
->state
== FW_LOADER_START_CACHE
) {
583 if (fw_cache_piggyback_on_request(buf
->fw_id
))
587 /* pass the pages buffer to driver at the last minute */
588 fw_set_page_data(buf
, fw
);
589 mutex_unlock(&fw_lock
);
594 * user-mode helper code
596 #ifdef CONFIG_FW_LOADER_USER_HELPER
597 struct firmware_priv
{
600 struct firmware_buf
*buf
;
604 static struct firmware_priv
*to_firmware_priv(struct device
*dev
)
606 return container_of(dev
, struct firmware_priv
, dev
);
609 static void __fw_load_abort(struct firmware_buf
*buf
)
612 * There is a small window in which user can write to 'loading'
613 * between loading done and disappearance of 'loading'
615 if (fw_state_is_done(&buf
->fw_st
))
618 list_del_init(&buf
->pending_list
);
619 fw_state_aborted(&buf
->fw_st
);
622 static void fw_load_abort(struct firmware_priv
*fw_priv
)
624 struct firmware_buf
*buf
= fw_priv
->buf
;
626 __fw_load_abort(buf
);
629 static LIST_HEAD(pending_fw_head
);
631 static void kill_pending_fw_fallback_reqs(bool only_kill_custom
)
633 struct firmware_buf
*buf
;
634 struct firmware_buf
*next
;
636 mutex_lock(&fw_lock
);
637 list_for_each_entry_safe(buf
, next
, &pending_fw_head
, pending_list
) {
638 if (!buf
->need_uevent
|| !only_kill_custom
)
639 __fw_load_abort(buf
);
641 mutex_unlock(&fw_lock
);
644 static ssize_t
timeout_show(struct class *class, struct class_attribute
*attr
,
647 return sprintf(buf
, "%d\n", loading_timeout
);
651 * firmware_timeout_store - set number of seconds to wait for firmware
652 * @class: device class pointer
653 * @attr: device attribute pointer
654 * @buf: buffer to scan for timeout value
655 * @count: number of bytes in @buf
657 * Sets the number of seconds to wait for the firmware. Once
658 * this expires an error will be returned to the driver and no
659 * firmware will be provided.
661 * Note: zero means 'wait forever'.
663 static ssize_t
timeout_store(struct class *class, struct class_attribute
*attr
,
664 const char *buf
, size_t count
)
666 loading_timeout
= simple_strtol(buf
, NULL
, 10);
667 if (loading_timeout
< 0)
672 static CLASS_ATTR_RW(timeout
);
674 static struct attribute
*firmware_class_attrs
[] = {
675 &class_attr_timeout
.attr
,
678 ATTRIBUTE_GROUPS(firmware_class
);
680 static void fw_dev_release(struct device
*dev
)
682 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
687 static int do_firmware_uevent(struct firmware_priv
*fw_priv
, struct kobj_uevent_env
*env
)
689 if (add_uevent_var(env
, "FIRMWARE=%s", fw_priv
->buf
->fw_id
))
691 if (add_uevent_var(env
, "TIMEOUT=%i", loading_timeout
))
693 if (add_uevent_var(env
, "ASYNC=%d", fw_priv
->nowait
))
699 static int firmware_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
701 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
704 mutex_lock(&fw_lock
);
706 err
= do_firmware_uevent(fw_priv
, env
);
707 mutex_unlock(&fw_lock
);
711 static struct class firmware_class
= {
713 .class_groups
= firmware_class_groups
,
714 .dev_uevent
= firmware_uevent
,
715 .dev_release
= fw_dev_release
,
718 static ssize_t
firmware_loading_show(struct device
*dev
,
719 struct device_attribute
*attr
, char *buf
)
721 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
724 mutex_lock(&fw_lock
);
726 loading
= fw_state_is_loading(&fw_priv
->buf
->fw_st
);
727 mutex_unlock(&fw_lock
);
729 return sprintf(buf
, "%d\n", loading
);
732 /* Some architectures don't have PAGE_KERNEL_RO */
733 #ifndef PAGE_KERNEL_RO
734 #define PAGE_KERNEL_RO PAGE_KERNEL
737 /* one pages buffer should be mapped/unmapped only once */
738 static int fw_map_pages_buf(struct firmware_buf
*buf
)
740 if (!buf
->is_paged_buf
)
744 buf
->data
= vmap(buf
->pages
, buf
->nr_pages
, 0, PAGE_KERNEL_RO
);
751 * firmware_loading_store - set value in the 'loading' control file
752 * @dev: device pointer
753 * @attr: device attribute pointer
754 * @buf: buffer to scan for loading control value
755 * @count: number of bytes in @buf
757 * The relevant values are:
759 * 1: Start a load, discarding any previous partial load.
760 * 0: Conclude the load and hand the data to the driver code.
761 * -1: Conclude the load with an error and discard any written data.
763 static ssize_t
firmware_loading_store(struct device
*dev
,
764 struct device_attribute
*attr
,
765 const char *buf
, size_t count
)
767 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
768 struct firmware_buf
*fw_buf
;
769 ssize_t written
= count
;
770 int loading
= simple_strtol(buf
, NULL
, 10);
773 mutex_lock(&fw_lock
);
774 fw_buf
= fw_priv
->buf
;
775 if (fw_state_is_aborted(&fw_buf
->fw_st
))
780 /* discarding any previous partial load */
781 if (!fw_state_is_done(&fw_buf
->fw_st
)) {
782 for (i
= 0; i
< fw_buf
->nr_pages
; i
++)
783 __free_page(fw_buf
->pages
[i
]);
784 vfree(fw_buf
->pages
);
785 fw_buf
->pages
= NULL
;
786 fw_buf
->page_array_size
= 0;
787 fw_buf
->nr_pages
= 0;
788 fw_state_start(&fw_buf
->fw_st
);
792 if (fw_state_is_loading(&fw_buf
->fw_st
)) {
796 * Several loading requests may be pending on
797 * one same firmware buf, so let all requests
798 * see the mapped 'buf->data' once the loading
801 rc
= fw_map_pages_buf(fw_buf
);
803 dev_err(dev
, "%s: map pages failed\n",
806 rc
= security_kernel_post_read_file(NULL
,
807 fw_buf
->data
, fw_buf
->size
,
811 * Same logic as fw_load_abort, only the DONE bit
812 * is ignored and we set ABORT only on failure.
814 list_del_init(&fw_buf
->pending_list
);
816 fw_state_aborted(&fw_buf
->fw_st
);
819 fw_state_done(&fw_buf
->fw_st
);
825 dev_err(dev
, "%s: unexpected value (%d)\n", __func__
, loading
);
828 fw_load_abort(fw_priv
);
832 mutex_unlock(&fw_lock
);
836 static DEVICE_ATTR(loading
, 0644, firmware_loading_show
, firmware_loading_store
);
838 static void firmware_rw_buf(struct firmware_buf
*buf
, char *buffer
,
839 loff_t offset
, size_t count
, bool read
)
842 memcpy(buffer
, buf
->data
+ offset
, count
);
844 memcpy(buf
->data
+ offset
, buffer
, count
);
847 static void firmware_rw(struct firmware_buf
*buf
, char *buffer
,
848 loff_t offset
, size_t count
, bool read
)
852 int page_nr
= offset
>> PAGE_SHIFT
;
853 int page_ofs
= offset
& (PAGE_SIZE
-1);
854 int page_cnt
= min_t(size_t, PAGE_SIZE
- page_ofs
, count
);
856 page_data
= kmap(buf
->pages
[page_nr
]);
859 memcpy(buffer
, page_data
+ page_ofs
, page_cnt
);
861 memcpy(page_data
+ page_ofs
, buffer
, page_cnt
);
863 kunmap(buf
->pages
[page_nr
]);
870 static ssize_t
firmware_data_read(struct file
*filp
, struct kobject
*kobj
,
871 struct bin_attribute
*bin_attr
,
872 char *buffer
, loff_t offset
, size_t count
)
874 struct device
*dev
= kobj_to_dev(kobj
);
875 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
876 struct firmware_buf
*buf
;
879 mutex_lock(&fw_lock
);
881 if (!buf
|| fw_state_is_done(&buf
->fw_st
)) {
885 if (offset
> buf
->size
) {
889 if (count
> buf
->size
- offset
)
890 count
= buf
->size
- offset
;
895 firmware_rw_buf(buf
, buffer
, offset
, count
, true);
897 firmware_rw(buf
, buffer
, offset
, count
, true);
900 mutex_unlock(&fw_lock
);
904 static int fw_realloc_buffer(struct firmware_priv
*fw_priv
, int min_size
)
906 struct firmware_buf
*buf
= fw_priv
->buf
;
907 int pages_needed
= PAGE_ALIGN(min_size
) >> PAGE_SHIFT
;
909 /* If the array of pages is too small, grow it... */
910 if (buf
->page_array_size
< pages_needed
) {
911 int new_array_size
= max(pages_needed
,
912 buf
->page_array_size
* 2);
913 struct page
**new_pages
;
915 new_pages
= vmalloc(new_array_size
* sizeof(void *));
917 fw_load_abort(fw_priv
);
920 memcpy(new_pages
, buf
->pages
,
921 buf
->page_array_size
* sizeof(void *));
922 memset(&new_pages
[buf
->page_array_size
], 0, sizeof(void *) *
923 (new_array_size
- buf
->page_array_size
));
925 buf
->pages
= new_pages
;
926 buf
->page_array_size
= new_array_size
;
929 while (buf
->nr_pages
< pages_needed
) {
930 buf
->pages
[buf
->nr_pages
] =
931 alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
933 if (!buf
->pages
[buf
->nr_pages
]) {
934 fw_load_abort(fw_priv
);
943 * firmware_data_write - write method for firmware
944 * @filp: open sysfs file
945 * @kobj: kobject for the device
946 * @bin_attr: bin_attr structure
947 * @buffer: buffer being written
948 * @offset: buffer offset for write in total data store area
949 * @count: buffer size
951 * Data written to the 'data' attribute will be later handed to
952 * the driver as a firmware image.
954 static ssize_t
firmware_data_write(struct file
*filp
, struct kobject
*kobj
,
955 struct bin_attribute
*bin_attr
,
956 char *buffer
, loff_t offset
, size_t count
)
958 struct device
*dev
= kobj_to_dev(kobj
);
959 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
960 struct firmware_buf
*buf
;
963 if (!capable(CAP_SYS_RAWIO
))
966 mutex_lock(&fw_lock
);
968 if (!buf
|| fw_state_is_done(&buf
->fw_st
)) {
974 if (offset
+ count
> buf
->allocated_size
) {
978 firmware_rw_buf(buf
, buffer
, offset
, count
, false);
981 retval
= fw_realloc_buffer(fw_priv
, offset
+ count
);
986 firmware_rw(buf
, buffer
, offset
, count
, false);
989 buf
->size
= max_t(size_t, offset
+ count
, buf
->size
);
991 mutex_unlock(&fw_lock
);
995 static struct bin_attribute firmware_attr_data
= {
996 .attr
= { .name
= "data", .mode
= 0644 },
998 .read
= firmware_data_read
,
999 .write
= firmware_data_write
,
1002 static struct attribute
*fw_dev_attrs
[] = {
1003 &dev_attr_loading
.attr
,
1007 static struct bin_attribute
*fw_dev_bin_attrs
[] = {
1008 &firmware_attr_data
,
1012 static const struct attribute_group fw_dev_attr_group
= {
1013 .attrs
= fw_dev_attrs
,
1014 .bin_attrs
= fw_dev_bin_attrs
,
1017 static const struct attribute_group
*fw_dev_attr_groups
[] = {
1022 static struct firmware_priv
*
1023 fw_create_instance(struct firmware
*firmware
, const char *fw_name
,
1024 struct device
*device
, unsigned int opt_flags
)
1026 struct firmware_priv
*fw_priv
;
1027 struct device
*f_dev
;
1029 fw_priv
= kzalloc(sizeof(*fw_priv
), GFP_KERNEL
);
1031 fw_priv
= ERR_PTR(-ENOMEM
);
1035 fw_priv
->nowait
= !!(opt_flags
& FW_OPT_NOWAIT
);
1036 fw_priv
->fw
= firmware
;
1037 f_dev
= &fw_priv
->dev
;
1039 device_initialize(f_dev
);
1040 dev_set_name(f_dev
, "%s", fw_name
);
1041 f_dev
->parent
= device
;
1042 f_dev
->class = &firmware_class
;
1043 f_dev
->groups
= fw_dev_attr_groups
;
1048 /* load a firmware via user helper */
1049 static int _request_firmware_load(struct firmware_priv
*fw_priv
,
1050 unsigned int opt_flags
, long timeout
)
1053 struct device
*f_dev
= &fw_priv
->dev
;
1054 struct firmware_buf
*buf
= fw_priv
->buf
;
1056 /* fall back on userspace loading */
1058 buf
->is_paged_buf
= true;
1060 dev_set_uevent_suppress(f_dev
, true);
1062 retval
= device_add(f_dev
);
1064 dev_err(f_dev
, "%s: device_register failed\n", __func__
);
1068 mutex_lock(&fw_lock
);
1069 list_add(&buf
->pending_list
, &pending_fw_head
);
1070 mutex_unlock(&fw_lock
);
1072 if (opt_flags
& FW_OPT_UEVENT
) {
1073 buf
->need_uevent
= true;
1074 dev_set_uevent_suppress(f_dev
, false);
1075 dev_dbg(f_dev
, "firmware: requesting %s\n", buf
->fw_id
);
1076 kobject_uevent(&fw_priv
->dev
.kobj
, KOBJ_ADD
);
1078 timeout
= MAX_JIFFY_OFFSET
;
1081 retval
= fw_state_wait_timeout(&buf
->fw_st
, timeout
);
1083 mutex_lock(&fw_lock
);
1084 fw_load_abort(fw_priv
);
1085 mutex_unlock(&fw_lock
);
1088 if (fw_state_is_aborted(&buf
->fw_st
))
1090 else if (buf
->is_paged_buf
&& !buf
->data
)
1099 static int fw_load_from_user_helper(struct firmware
*firmware
,
1100 const char *name
, struct device
*device
,
1101 unsigned int opt_flags
)
1103 struct firmware_priv
*fw_priv
;
1107 timeout
= firmware_loading_timeout();
1108 if (opt_flags
& FW_OPT_NOWAIT
) {
1109 timeout
= usermodehelper_read_lock_wait(timeout
);
1111 dev_dbg(device
, "firmware: %s loading timed out\n",
1116 ret
= usermodehelper_read_trylock();
1118 dev_err(device
, "firmware: %s will not be loaded\n",
1124 fw_priv
= fw_create_instance(firmware
, name
, device
, opt_flags
);
1125 if (IS_ERR(fw_priv
)) {
1126 ret
= PTR_ERR(fw_priv
);
1130 fw_priv
->buf
= firmware
->priv
;
1131 ret
= _request_firmware_load(fw_priv
, opt_flags
, timeout
);
1134 ret
= assign_firmware_buf(firmware
, device
, opt_flags
);
1137 usermodehelper_read_unlock();
1142 #else /* CONFIG_FW_LOADER_USER_HELPER */
1144 fw_load_from_user_helper(struct firmware
*firmware
, const char *name
,
1145 struct device
*device
, unsigned int opt_flags
)
1150 static inline void kill_pending_fw_fallback_reqs(bool only_kill_custom
) { }
1152 #endif /* CONFIG_FW_LOADER_USER_HELPER */
1154 /* prepare firmware and firmware_buf structs;
1155 * return 0 if a firmware is already assigned, 1 if need to load one,
1156 * or a negative error code
1159 _request_firmware_prepare(struct firmware
**firmware_p
, const char *name
,
1160 struct device
*device
, void *dbuf
, size_t size
)
1162 struct firmware
*firmware
;
1163 struct firmware_buf
*buf
;
1166 *firmware_p
= firmware
= kzalloc(sizeof(*firmware
), GFP_KERNEL
);
1168 dev_err(device
, "%s: kmalloc(struct firmware) failed\n",
1173 if (fw_get_builtin_firmware(firmware
, name
, dbuf
, size
)) {
1174 dev_dbg(device
, "using built-in %s\n", name
);
1175 return 0; /* assigned */
1178 ret
= fw_lookup_and_allocate_buf(name
, &fw_cache
, &buf
, dbuf
, size
);
1181 * bind with 'buf' now to avoid warning in failure path
1182 * of requesting firmware.
1184 firmware
->priv
= buf
;
1187 ret
= fw_state_wait(&buf
->fw_st
);
1189 fw_set_page_data(buf
, firmware
);
1190 return 0; /* assigned */
1196 return 1; /* need to load */
1200 * Batched requests need only one wake, we need to do this step last due to the
1201 * fallback mechanism. The buf is protected with kref_get(), and it won't be
1202 * released until the last user calls release_firmware().
1204 * Failed batched requests are possible as well, in such cases we just share
1205 * the struct firmware_buf and won't release it until all requests are woken
1206 * and have gone through this same path.
1208 static void fw_abort_batch_reqs(struct firmware
*fw
)
1210 struct firmware_buf
*buf
;
1212 /* Loaded directly? */
1213 if (!fw
|| !fw
->priv
)
1217 if (!fw_state_is_aborted(&buf
->fw_st
))
1218 fw_state_aborted(&buf
->fw_st
);
1221 /* called from request_firmware() and request_firmware_work_func() */
1223 _request_firmware(const struct firmware
**firmware_p
, const char *name
,
1224 struct device
*device
, void *buf
, size_t size
,
1225 unsigned int opt_flags
)
1227 struct firmware
*fw
= NULL
;
1233 if (!name
|| name
[0] == '\0') {
1238 ret
= _request_firmware_prepare(&fw
, name
, device
, buf
, size
);
1239 if (ret
<= 0) /* error or already assigned */
1242 if (!firmware_enabled()) {
1243 WARN(1, "firmware request while host is not available\n");
1248 ret
= fw_get_filesystem_firmware(device
, fw
->priv
);
1250 if (!(opt_flags
& FW_OPT_NO_WARN
))
1252 "Direct firmware load for %s failed with error %d\n",
1254 if (opt_flags
& FW_OPT_USERHELPER
) {
1255 dev_warn(device
, "Falling back to user helper\n");
1256 ret
= fw_load_from_user_helper(fw
, name
, device
,
1260 ret
= assign_firmware_buf(fw
, device
, opt_flags
);
1264 fw_abort_batch_reqs(fw
);
1265 release_firmware(fw
);
1274 * request_firmware: - send firmware request and wait for it
1275 * @firmware_p: pointer to firmware image
1276 * @name: name of firmware file
1277 * @device: device for which firmware is being loaded
1279 * @firmware_p will be used to return a firmware image by the name
1280 * of @name for device @device.
1282 * Should be called from user context where sleeping is allowed.
1284 * @name will be used as $FIRMWARE in the uevent environment and
1285 * should be distinctive enough not to be confused with any other
1286 * firmware image for this or any other device.
1288 * Caller must hold the reference count of @device.
1290 * The function can be called safely inside device's suspend and
1294 request_firmware(const struct firmware
**firmware_p
, const char *name
,
1295 struct device
*device
)
1299 /* Need to pin this module until return */
1300 __module_get(THIS_MODULE
);
1301 ret
= _request_firmware(firmware_p
, name
, device
, NULL
, 0,
1302 FW_OPT_UEVENT
| FW_OPT_FALLBACK
);
1303 module_put(THIS_MODULE
);
1306 EXPORT_SYMBOL(request_firmware
);
1309 * request_firmware_direct: - load firmware directly without usermode helper
1310 * @firmware_p: pointer to firmware image
1311 * @name: name of firmware file
1312 * @device: device for which firmware is being loaded
1314 * This function works pretty much like request_firmware(), but this doesn't
1315 * fall back to usermode helper even if the firmware couldn't be loaded
1316 * directly from fs. Hence it's useful for loading optional firmwares, which
1317 * aren't always present, without extra long timeouts of udev.
1319 int request_firmware_direct(const struct firmware
**firmware_p
,
1320 const char *name
, struct device
*device
)
1324 __module_get(THIS_MODULE
);
1325 ret
= _request_firmware(firmware_p
, name
, device
, NULL
, 0,
1326 FW_OPT_UEVENT
| FW_OPT_NO_WARN
);
1327 module_put(THIS_MODULE
);
1330 EXPORT_SYMBOL_GPL(request_firmware_direct
);
1333 * request_firmware_into_buf - load firmware into a previously allocated buffer
1334 * @firmware_p: pointer to firmware image
1335 * @name: name of firmware file
1336 * @device: device for which firmware is being loaded and DMA region allocated
1337 * @buf: address of buffer to load firmware into
1338 * @size: size of buffer
1340 * This function works pretty much like request_firmware(), but it doesn't
1341 * allocate a buffer to hold the firmware data. Instead, the firmware
1342 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
1343 * data member is pointed at @buf.
1345 * This function doesn't cache firmware either.
1348 request_firmware_into_buf(const struct firmware
**firmware_p
, const char *name
,
1349 struct device
*device
, void *buf
, size_t size
)
1353 __module_get(THIS_MODULE
);
1354 ret
= _request_firmware(firmware_p
, name
, device
, buf
, size
,
1355 FW_OPT_UEVENT
| FW_OPT_FALLBACK
|
1357 module_put(THIS_MODULE
);
1360 EXPORT_SYMBOL(request_firmware_into_buf
);
1363 * release_firmware: - release the resource associated with a firmware image
1364 * @fw: firmware resource to release
1366 void release_firmware(const struct firmware
*fw
)
1369 if (!fw_is_builtin_firmware(fw
))
1370 firmware_free_data(fw
);
1374 EXPORT_SYMBOL(release_firmware
);
1377 struct firmware_work
{
1378 struct work_struct work
;
1379 struct module
*module
;
1381 struct device
*device
;
1383 void (*cont
)(const struct firmware
*fw
, void *context
);
1384 unsigned int opt_flags
;
1387 static void request_firmware_work_func(struct work_struct
*work
)
1389 struct firmware_work
*fw_work
;
1390 const struct firmware
*fw
;
1392 fw_work
= container_of(work
, struct firmware_work
, work
);
1394 _request_firmware(&fw
, fw_work
->name
, fw_work
->device
, NULL
, 0,
1395 fw_work
->opt_flags
);
1396 fw_work
->cont(fw
, fw_work
->context
);
1397 put_device(fw_work
->device
); /* taken in request_firmware_nowait() */
1399 module_put(fw_work
->module
);
1400 kfree_const(fw_work
->name
);
1405 * request_firmware_nowait - asynchronous version of request_firmware
1406 * @module: module requesting the firmware
1407 * @uevent: sends uevent to copy the firmware image if this flag
1408 * is non-zero else the firmware copy must be done manually.
1409 * @name: name of firmware file
1410 * @device: device for which firmware is being loaded
1411 * @gfp: allocation flags
1412 * @context: will be passed over to @cont, and
1413 * @fw may be %NULL if firmware request fails.
1414 * @cont: function will be called asynchronously when the firmware
1417 * Caller must hold the reference count of @device.
1419 * Asynchronous variant of request_firmware() for user contexts:
1420 * - sleep for as small periods as possible since it may
1421 * increase kernel boot time of built-in device drivers
1422 * requesting firmware in their ->probe() methods, if
1423 * @gfp is GFP_KERNEL.
1425 * - can't sleep at all if @gfp is GFP_ATOMIC.
1428 request_firmware_nowait(
1429 struct module
*module
, bool uevent
,
1430 const char *name
, struct device
*device
, gfp_t gfp
, void *context
,
1431 void (*cont
)(const struct firmware
*fw
, void *context
))
1433 struct firmware_work
*fw_work
;
1435 fw_work
= kzalloc(sizeof(struct firmware_work
), gfp
);
1439 fw_work
->module
= module
;
1440 fw_work
->name
= kstrdup_const(name
, gfp
);
1441 if (!fw_work
->name
) {
1445 fw_work
->device
= device
;
1446 fw_work
->context
= context
;
1447 fw_work
->cont
= cont
;
1448 fw_work
->opt_flags
= FW_OPT_NOWAIT
| FW_OPT_FALLBACK
|
1449 (uevent
? FW_OPT_UEVENT
: FW_OPT_USERHELPER
);
1451 if (!try_module_get(module
)) {
1452 kfree_const(fw_work
->name
);
1457 get_device(fw_work
->device
);
1458 INIT_WORK(&fw_work
->work
, request_firmware_work_func
);
1459 schedule_work(&fw_work
->work
);
1462 EXPORT_SYMBOL(request_firmware_nowait
);
1464 #ifdef CONFIG_PM_SLEEP
1465 static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain
);
1468 * cache_firmware - cache one firmware image in kernel memory space
1469 * @fw_name: the firmware image name
1471 * Cache firmware in kernel memory so that drivers can use it when
1472 * system isn't ready for them to request firmware image from userspace.
1473 * Once it returns successfully, driver can use request_firmware or its
1474 * nowait version to get the cached firmware without any interacting
1477 * Return 0 if the firmware image has been cached successfully
1478 * Return !0 otherwise
1481 static int cache_firmware(const char *fw_name
)
1484 const struct firmware
*fw
;
1486 pr_debug("%s: %s\n", __func__
, fw_name
);
1488 ret
= request_firmware(&fw
, fw_name
, NULL
);
1492 pr_debug("%s: %s ret=%d\n", __func__
, fw_name
, ret
);
1497 static struct firmware_buf
*fw_lookup_buf(const char *fw_name
)
1499 struct firmware_buf
*tmp
;
1500 struct firmware_cache
*fwc
= &fw_cache
;
1502 spin_lock(&fwc
->lock
);
1503 tmp
= __fw_lookup_buf(fw_name
);
1504 spin_unlock(&fwc
->lock
);
1510 * uncache_firmware - remove one cached firmware image
1511 * @fw_name: the firmware image name
1513 * Uncache one firmware image which has been cached successfully
1516 * Return 0 if the firmware cache has been removed successfully
1517 * Return !0 otherwise
1520 static int uncache_firmware(const char *fw_name
)
1522 struct firmware_buf
*buf
;
1525 pr_debug("%s: %s\n", __func__
, fw_name
);
1527 if (fw_get_builtin_firmware(&fw
, fw_name
, NULL
, 0))
1530 buf
= fw_lookup_buf(fw_name
);
1539 static struct fw_cache_entry
*alloc_fw_cache_entry(const char *name
)
1541 struct fw_cache_entry
*fce
;
1543 fce
= kzalloc(sizeof(*fce
), GFP_ATOMIC
);
1547 fce
->name
= kstrdup_const(name
, GFP_ATOMIC
);
1557 static int __fw_entry_found(const char *name
)
1559 struct firmware_cache
*fwc
= &fw_cache
;
1560 struct fw_cache_entry
*fce
;
1562 list_for_each_entry(fce
, &fwc
->fw_names
, list
) {
1563 if (!strcmp(fce
->name
, name
))
1569 static int fw_cache_piggyback_on_request(const char *name
)
1571 struct firmware_cache
*fwc
= &fw_cache
;
1572 struct fw_cache_entry
*fce
;
1575 spin_lock(&fwc
->name_lock
);
1576 if (__fw_entry_found(name
))
1579 fce
= alloc_fw_cache_entry(name
);
1582 list_add(&fce
->list
, &fwc
->fw_names
);
1583 pr_debug("%s: fw: %s\n", __func__
, name
);
1586 spin_unlock(&fwc
->name_lock
);
1590 static void free_fw_cache_entry(struct fw_cache_entry
*fce
)
1592 kfree_const(fce
->name
);
1596 static void __async_dev_cache_fw_image(void *fw_entry
,
1597 async_cookie_t cookie
)
1599 struct fw_cache_entry
*fce
= fw_entry
;
1600 struct firmware_cache
*fwc
= &fw_cache
;
1603 ret
= cache_firmware(fce
->name
);
1605 spin_lock(&fwc
->name_lock
);
1606 list_del(&fce
->list
);
1607 spin_unlock(&fwc
->name_lock
);
1609 free_fw_cache_entry(fce
);
1613 /* called with dev->devres_lock held */
1614 static void dev_create_fw_entry(struct device
*dev
, void *res
,
1617 struct fw_name_devm
*fwn
= res
;
1618 const char *fw_name
= fwn
->name
;
1619 struct list_head
*head
= data
;
1620 struct fw_cache_entry
*fce
;
1622 fce
= alloc_fw_cache_entry(fw_name
);
1624 list_add(&fce
->list
, head
);
1627 static int devm_name_match(struct device
*dev
, void *res
,
1630 struct fw_name_devm
*fwn
= res
;
1631 return (fwn
->magic
== (unsigned long)match_data
);
1634 static void dev_cache_fw_image(struct device
*dev
, void *data
)
1637 struct fw_cache_entry
*fce
;
1638 struct fw_cache_entry
*fce_next
;
1639 struct firmware_cache
*fwc
= &fw_cache
;
1641 devres_for_each_res(dev
, fw_name_devm_release
,
1642 devm_name_match
, &fw_cache
,
1643 dev_create_fw_entry
, &todo
);
1645 list_for_each_entry_safe(fce
, fce_next
, &todo
, list
) {
1646 list_del(&fce
->list
);
1648 spin_lock(&fwc
->name_lock
);
1649 /* only one cache entry for one firmware */
1650 if (!__fw_entry_found(fce
->name
)) {
1651 list_add(&fce
->list
, &fwc
->fw_names
);
1653 free_fw_cache_entry(fce
);
1656 spin_unlock(&fwc
->name_lock
);
1659 async_schedule_domain(__async_dev_cache_fw_image
,
1665 static void __device_uncache_fw_images(void)
1667 struct firmware_cache
*fwc
= &fw_cache
;
1668 struct fw_cache_entry
*fce
;
1670 spin_lock(&fwc
->name_lock
);
1671 while (!list_empty(&fwc
->fw_names
)) {
1672 fce
= list_entry(fwc
->fw_names
.next
,
1673 struct fw_cache_entry
, list
);
1674 list_del(&fce
->list
);
1675 spin_unlock(&fwc
->name_lock
);
1677 uncache_firmware(fce
->name
);
1678 free_fw_cache_entry(fce
);
1680 spin_lock(&fwc
->name_lock
);
1682 spin_unlock(&fwc
->name_lock
);
1686 * device_cache_fw_images - cache devices' firmware
1688 * If one device called request_firmware or its nowait version
1689 * successfully before, the firmware names are recored into the
1690 * device's devres link list, so device_cache_fw_images can call
1691 * cache_firmware() to cache these firmwares for the device,
1692 * then the device driver can load its firmwares easily at
1693 * time when system is not ready to complete loading firmware.
1695 static void device_cache_fw_images(void)
1697 struct firmware_cache
*fwc
= &fw_cache
;
1701 pr_debug("%s\n", __func__
);
1703 /* cancel uncache work */
1704 cancel_delayed_work_sync(&fwc
->work
);
1707 * use small loading timeout for caching devices' firmware
1708 * because all these firmware images have been loaded
1709 * successfully at lease once, also system is ready for
1710 * completing firmware loading now. The maximum size of
1711 * firmware in current distributions is about 2M bytes,
1712 * so 10 secs should be enough.
1714 old_timeout
= loading_timeout
;
1715 loading_timeout
= 10;
1717 mutex_lock(&fw_lock
);
1718 fwc
->state
= FW_LOADER_START_CACHE
;
1719 dpm_for_each_dev(NULL
, dev_cache_fw_image
);
1720 mutex_unlock(&fw_lock
);
1722 /* wait for completion of caching firmware for all devices */
1723 async_synchronize_full_domain(&fw_cache_domain
);
1725 loading_timeout
= old_timeout
;
1729 * device_uncache_fw_images - uncache devices' firmware
1731 * uncache all firmwares which have been cached successfully
1732 * by device_uncache_fw_images earlier
1734 static void device_uncache_fw_images(void)
1736 pr_debug("%s\n", __func__
);
1737 __device_uncache_fw_images();
1740 static void device_uncache_fw_images_work(struct work_struct
*work
)
1742 device_uncache_fw_images();
1746 * device_uncache_fw_images_delay - uncache devices firmwares
1747 * @delay: number of milliseconds to delay uncache device firmwares
1749 * uncache all devices's firmwares which has been cached successfully
1750 * by device_cache_fw_images after @delay milliseconds.
1752 static void device_uncache_fw_images_delay(unsigned long delay
)
1754 queue_delayed_work(system_power_efficient_wq
, &fw_cache
.work
,
1755 msecs_to_jiffies(delay
));
1759 * fw_pm_notify - notifier for suspend/resume
1760 * @notify_block: unused
1761 * @mode: mode we are switching to
1764 * Used to modify the firmware_class state as we move in between states.
1765 * The firmware_class implements a firmware cache to enable device driver
1766 * to fetch firmware upon resume before the root filesystem is ready. We
1767 * disable API calls which do not use the built-in firmware or the firmware
1768 * cache when we know these calls will not work.
1770 * The inner logic behind all this is a bit complex so it is worth summarizing
1771 * the kernel's own suspend/resume process with context and focus on how this
1772 * can impact the firmware API.
1774 * First a review on how we go to suspend::
1776 * pm_suspend() --> enter_state() -->
1778 * suspend_prepare() -->
1779 * __pm_notifier_call_chain(PM_SUSPEND_PREPARE, ...);
1780 * suspend_freeze_processes() -->
1781 * freeze_processes() -->
1782 * __usermodehelper_set_disable_depth(UMH_DISABLED);
1783 * freeze all tasks ...
1784 * freeze_kernel_threads()
1785 * suspend_devices_and_enter() -->
1786 * dpm_suspend_start() -->
1789 * suspend_enter() -->
1790 * platform_suspend_prepare()
1791 * dpm_suspend_late()
1795 * When we resume we bail out of a loop from suspend_devices_and_enter() and
1796 * unwind back out to the caller enter_state() where we were before as follows::
1799 * suspend_devices_and_enter() --> (bail from loop)
1800 * dpm_resume_end() -->
1803 * suspend_finish() -->
1804 * suspend_thaw_processes() -->
1805 * thaw_processes() -->
1806 * __usermodehelper_set_disable_depth(UMH_FREEZING);
1807 * thaw_workqueues();
1808 * thaw all processes ...
1809 * usermodehelper_enable();
1810 * pm_notifier_call_chain(PM_POST_SUSPEND);
1812 * fw_pm_notify() works through pm_notifier_call_chain().
1814 static int fw_pm_notify(struct notifier_block
*notify_block
,
1815 unsigned long mode
, void *unused
)
1818 case PM_HIBERNATION_PREPARE
:
1819 case PM_SUSPEND_PREPARE
:
1820 case PM_RESTORE_PREPARE
:
1822 * kill pending fallback requests with a custom fallback
1823 * to avoid stalling suspend.
1825 kill_pending_fw_fallback_reqs(true);
1826 device_cache_fw_images();
1830 case PM_POST_SUSPEND
:
1831 case PM_POST_HIBERNATION
:
1832 case PM_POST_RESTORE
:
1834 * In case that system sleep failed and syscore_suspend is
1837 mutex_lock(&fw_lock
);
1838 fw_cache
.state
= FW_LOADER_NO_CACHE
;
1839 mutex_unlock(&fw_lock
);
1842 device_uncache_fw_images_delay(10 * MSEC_PER_SEC
);
1849 /* stop caching firmware once syscore_suspend is reached */
1850 static int fw_suspend(void)
1852 fw_cache
.state
= FW_LOADER_NO_CACHE
;
1856 static struct syscore_ops fw_syscore_ops
= {
1857 .suspend
= fw_suspend
,
1860 static int fw_cache_piggyback_on_request(const char *name
)
1866 static void __init
fw_cache_init(void)
1868 spin_lock_init(&fw_cache
.lock
);
1869 INIT_LIST_HEAD(&fw_cache
.head
);
1870 fw_cache
.state
= FW_LOADER_NO_CACHE
;
1872 #ifdef CONFIG_PM_SLEEP
1873 spin_lock_init(&fw_cache
.name_lock
);
1874 INIT_LIST_HEAD(&fw_cache
.fw_names
);
1876 INIT_DELAYED_WORK(&fw_cache
.work
,
1877 device_uncache_fw_images_work
);
1879 fw_cache
.pm_notify
.notifier_call
= fw_pm_notify
;
1880 register_pm_notifier(&fw_cache
.pm_notify
);
1882 register_syscore_ops(&fw_syscore_ops
);
1886 static int fw_shutdown_notify(struct notifier_block
*unused1
,
1887 unsigned long unused2
, void *unused3
)
1891 * Kill all pending fallback requests to avoid both stalling shutdown,
1892 * and avoid a deadlock with the usermode_lock.
1894 kill_pending_fw_fallback_reqs(false);
1899 static struct notifier_block fw_shutdown_nb
= {
1900 .notifier_call
= fw_shutdown_notify
,
1903 static int __init
firmware_class_init(void)
1907 register_reboot_notifier(&fw_shutdown_nb
);
1908 #ifdef CONFIG_FW_LOADER_USER_HELPER
1909 return class_register(&firmware_class
);
1915 static void __exit
firmware_class_exit(void)
1918 #ifdef CONFIG_PM_SLEEP
1919 unregister_syscore_ops(&fw_syscore_ops
);
1920 unregister_pm_notifier(&fw_cache
.pm_notify
);
1922 unregister_reboot_notifier(&fw_shutdown_nb
);
1923 #ifdef CONFIG_FW_LOADER_USER_HELPER
1924 class_unregister(&firmware_class
);
1928 fs_initcall(firmware_class_init
);
1929 module_exit(firmware_class_exit
);