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
)
51 struct builtin_fw
*b_fw
;
53 for (b_fw
= __start_builtin_fw
; b_fw
!= __end_builtin_fw
; b_fw
++) {
54 if (strcmp(name
, b_fw
->name
) == 0) {
55 fw
->size
= b_fw
->size
;
56 fw
->data
= b_fw
->data
;
64 static bool fw_is_builtin_firmware(const struct firmware
*fw
)
66 struct builtin_fw
*b_fw
;
68 for (b_fw
= __start_builtin_fw
; b_fw
!= __end_builtin_fw
; b_fw
++)
69 if (fw
->data
== b_fw
->data
)
75 #else /* Module case - no builtin firmware support */
77 static inline bool fw_get_builtin_firmware(struct firmware
*fw
, const char *name
)
82 static inline bool fw_is_builtin_firmware(const struct firmware
*fw
)
94 static int loading_timeout
= 60; /* In seconds */
96 static inline long firmware_loading_timeout(void)
98 return loading_timeout
> 0 ? loading_timeout
* HZ
: MAX_JIFFY_OFFSET
;
101 /* firmware behavior options */
102 #define FW_OPT_UEVENT (1U << 0)
103 #define FW_OPT_NOWAIT (1U << 1)
104 #ifdef CONFIG_FW_LOADER_USER_HELPER
105 #define FW_OPT_USERHELPER (1U << 2)
107 #define FW_OPT_USERHELPER 0
109 #ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK
110 #define FW_OPT_FALLBACK FW_OPT_USERHELPER
112 #define FW_OPT_FALLBACK 0
114 #define FW_OPT_NO_WARN (1U << 3)
116 struct firmware_cache
{
117 /* firmware_buf instance will be added into the below list */
119 struct list_head head
;
122 #ifdef CONFIG_PM_SLEEP
124 * Names of firmware images which have been cached successfully
125 * will be added into the below list so that device uncache
126 * helper can trace which firmware images have been cached
129 spinlock_t name_lock
;
130 struct list_head fw_names
;
132 struct delayed_work work
;
134 struct notifier_block pm_notify
;
138 struct firmware_buf
{
140 struct list_head list
;
141 struct completion completion
;
142 struct firmware_cache
*fwc
;
143 unsigned long status
;
146 #ifdef CONFIG_FW_LOADER_USER_HELPER
152 struct list_head pending_list
;
157 struct fw_cache_entry
{
158 struct list_head list
;
162 struct fw_name_devm
{
167 #define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
169 #define FW_LOADER_NO_CACHE 0
170 #define FW_LOADER_START_CACHE 1
172 static int fw_cache_piggyback_on_request(const char *name
);
174 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
175 * guarding for corner cases a global lock should be OK */
176 static DEFINE_MUTEX(fw_lock
);
178 static struct firmware_cache fw_cache
;
180 static struct firmware_buf
*__allocate_fw_buf(const char *fw_name
,
181 struct firmware_cache
*fwc
)
183 struct firmware_buf
*buf
;
185 buf
= kzalloc(sizeof(*buf
), GFP_ATOMIC
);
189 buf
->fw_id
= kstrdup_const(fw_name
, GFP_ATOMIC
);
195 kref_init(&buf
->ref
);
197 init_completion(&buf
->completion
);
198 #ifdef CONFIG_FW_LOADER_USER_HELPER
199 INIT_LIST_HEAD(&buf
->pending_list
);
202 pr_debug("%s: fw-%s buf=%p\n", __func__
, fw_name
, buf
);
207 static struct firmware_buf
*__fw_lookup_buf(const char *fw_name
)
209 struct firmware_buf
*tmp
;
210 struct firmware_cache
*fwc
= &fw_cache
;
212 list_for_each_entry(tmp
, &fwc
->head
, list
)
213 if (!strcmp(tmp
->fw_id
, fw_name
))
218 static int fw_lookup_and_allocate_buf(const char *fw_name
,
219 struct firmware_cache
*fwc
,
220 struct firmware_buf
**buf
)
222 struct firmware_buf
*tmp
;
224 spin_lock(&fwc
->lock
);
225 tmp
= __fw_lookup_buf(fw_name
);
228 spin_unlock(&fwc
->lock
);
232 tmp
= __allocate_fw_buf(fw_name
, fwc
);
234 list_add(&tmp
->list
, &fwc
->head
);
235 spin_unlock(&fwc
->lock
);
239 return tmp
? 0 : -ENOMEM
;
242 static void __fw_free_buf(struct kref
*ref
)
243 __releases(&fwc
->lock
)
245 struct firmware_buf
*buf
= to_fwbuf(ref
);
246 struct firmware_cache
*fwc
= buf
->fwc
;
248 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
249 __func__
, buf
->fw_id
, buf
, buf
->data
,
250 (unsigned int)buf
->size
);
252 list_del(&buf
->list
);
253 spin_unlock(&fwc
->lock
);
255 #ifdef CONFIG_FW_LOADER_USER_HELPER
256 if (buf
->is_paged_buf
) {
259 for (i
= 0; i
< buf
->nr_pages
; i
++)
260 __free_page(buf
->pages
[i
]);
265 kfree_const(buf
->fw_id
);
269 static void fw_free_buf(struct firmware_buf
*buf
)
271 struct firmware_cache
*fwc
= buf
->fwc
;
272 spin_lock(&fwc
->lock
);
273 if (!kref_put(&buf
->ref
, __fw_free_buf
))
274 spin_unlock(&fwc
->lock
);
277 /* direct firmware loading support */
278 static char fw_path_para
[256];
279 static const char * const fw_path
[] = {
281 "/lib/firmware/updates/" UTS_RELEASE
,
282 "/lib/firmware/updates",
283 "/lib/firmware/" UTS_RELEASE
,
288 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
289 * from kernel command line because firmware_class is generally built in
290 * kernel instead of module.
292 module_param_string(path
, fw_path_para
, sizeof(fw_path_para
), 0644);
293 MODULE_PARM_DESC(path
, "customized firmware image search path with a higher priority than default path");
295 static void fw_finish_direct_load(struct device
*device
,
296 struct firmware_buf
*buf
)
298 mutex_lock(&fw_lock
);
299 set_bit(FW_STATUS_DONE
, &buf
->status
);
300 complete_all(&buf
->completion
);
301 mutex_unlock(&fw_lock
);
304 static int fw_get_filesystem_firmware(struct device
*device
,
305 struct firmware_buf
*buf
)
316 for (i
= 0; i
< ARRAY_SIZE(fw_path
); i
++) {
317 /* skip the unset customized path */
321 len
= snprintf(path
, PATH_MAX
, "%s/%s",
322 fw_path
[i
], buf
->fw_id
);
323 if (len
>= PATH_MAX
) {
329 rc
= kernel_read_file_from_path(path
, &buf
->data
, &size
,
330 INT_MAX
, READING_FIRMWARE
);
332 dev_warn(device
, "loading %s failed with error %d\n",
336 dev_dbg(device
, "direct-loading %s\n", buf
->fw_id
);
338 fw_finish_direct_load(device
, buf
);
346 /* firmware holds the ownership of pages */
347 static void firmware_free_data(const struct firmware
*fw
)
349 /* Loaded directly? */
354 fw_free_buf(fw
->priv
);
357 /* store the pages buffer info firmware from buf */
358 static void fw_set_page_data(struct firmware_buf
*buf
, struct firmware
*fw
)
361 #ifdef CONFIG_FW_LOADER_USER_HELPER
362 fw
->pages
= buf
->pages
;
364 fw
->size
= buf
->size
;
365 fw
->data
= buf
->data
;
367 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
368 __func__
, buf
->fw_id
, buf
, buf
->data
,
369 (unsigned int)buf
->size
);
372 #ifdef CONFIG_PM_SLEEP
373 static void fw_name_devm_release(struct device
*dev
, void *res
)
375 struct fw_name_devm
*fwn
= res
;
377 if (fwn
->magic
== (unsigned long)&fw_cache
)
378 pr_debug("%s: fw_name-%s devm-%p released\n",
379 __func__
, fwn
->name
, res
);
380 kfree_const(fwn
->name
);
383 static int fw_devm_match(struct device
*dev
, void *res
,
386 struct fw_name_devm
*fwn
= res
;
388 return (fwn
->magic
== (unsigned long)&fw_cache
) &&
389 !strcmp(fwn
->name
, match_data
);
392 static struct fw_name_devm
*fw_find_devm_name(struct device
*dev
,
395 struct fw_name_devm
*fwn
;
397 fwn
= devres_find(dev
, fw_name_devm_release
,
398 fw_devm_match
, (void *)name
);
402 /* add firmware name into devres list */
403 static int fw_add_devm_name(struct device
*dev
, const char *name
)
405 struct fw_name_devm
*fwn
;
407 fwn
= fw_find_devm_name(dev
, name
);
411 fwn
= devres_alloc(fw_name_devm_release
, sizeof(struct fw_name_devm
),
415 fwn
->name
= kstrdup_const(name
, GFP_KERNEL
);
421 fwn
->magic
= (unsigned long)&fw_cache
;
422 devres_add(dev
, fwn
);
427 static int fw_add_devm_name(struct device
*dev
, const char *name
)
435 * user-mode helper code
437 #ifdef CONFIG_FW_LOADER_USER_HELPER
438 struct firmware_priv
{
441 struct firmware_buf
*buf
;
445 static struct firmware_priv
*to_firmware_priv(struct device
*dev
)
447 return container_of(dev
, struct firmware_priv
, dev
);
450 static void __fw_load_abort(struct firmware_buf
*buf
)
453 * There is a small window in which user can write to 'loading'
454 * between loading done and disappearance of 'loading'
456 if (test_bit(FW_STATUS_DONE
, &buf
->status
))
459 list_del_init(&buf
->pending_list
);
460 set_bit(FW_STATUS_ABORT
, &buf
->status
);
461 complete_all(&buf
->completion
);
464 static void fw_load_abort(struct firmware_priv
*fw_priv
)
466 struct firmware_buf
*buf
= fw_priv
->buf
;
468 __fw_load_abort(buf
);
470 /* avoid user action after loading abort */
474 #define is_fw_load_aborted(buf) \
475 test_bit(FW_STATUS_ABORT, &(buf)->status)
477 static LIST_HEAD(pending_fw_head
);
479 /* reboot notifier for avoid deadlock with usermode_lock */
480 static int fw_shutdown_notify(struct notifier_block
*unused1
,
481 unsigned long unused2
, void *unused3
)
483 mutex_lock(&fw_lock
);
484 while (!list_empty(&pending_fw_head
))
485 __fw_load_abort(list_first_entry(&pending_fw_head
,
488 mutex_unlock(&fw_lock
);
492 static struct notifier_block fw_shutdown_nb
= {
493 .notifier_call
= fw_shutdown_notify
,
496 static ssize_t
timeout_show(struct class *class, struct class_attribute
*attr
,
499 return sprintf(buf
, "%d\n", loading_timeout
);
503 * firmware_timeout_store - set number of seconds to wait for firmware
504 * @class: device class pointer
505 * @attr: device attribute pointer
506 * @buf: buffer to scan for timeout value
507 * @count: number of bytes in @buf
509 * Sets the number of seconds to wait for the firmware. Once
510 * this expires an error will be returned to the driver and no
511 * firmware will be provided.
513 * Note: zero means 'wait forever'.
515 static ssize_t
timeout_store(struct class *class, struct class_attribute
*attr
,
516 const char *buf
, size_t count
)
518 loading_timeout
= simple_strtol(buf
, NULL
, 10);
519 if (loading_timeout
< 0)
525 static struct class_attribute firmware_class_attrs
[] = {
530 static void fw_dev_release(struct device
*dev
)
532 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
537 static int do_firmware_uevent(struct firmware_priv
*fw_priv
, struct kobj_uevent_env
*env
)
539 if (add_uevent_var(env
, "FIRMWARE=%s", fw_priv
->buf
->fw_id
))
541 if (add_uevent_var(env
, "TIMEOUT=%i", loading_timeout
))
543 if (add_uevent_var(env
, "ASYNC=%d", fw_priv
->nowait
))
549 static int firmware_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
551 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
554 mutex_lock(&fw_lock
);
556 err
= do_firmware_uevent(fw_priv
, env
);
557 mutex_unlock(&fw_lock
);
561 static struct class firmware_class
= {
563 .class_attrs
= firmware_class_attrs
,
564 .dev_uevent
= firmware_uevent
,
565 .dev_release
= fw_dev_release
,
568 static ssize_t
firmware_loading_show(struct device
*dev
,
569 struct device_attribute
*attr
, char *buf
)
571 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
574 mutex_lock(&fw_lock
);
576 loading
= test_bit(FW_STATUS_LOADING
, &fw_priv
->buf
->status
);
577 mutex_unlock(&fw_lock
);
579 return sprintf(buf
, "%d\n", loading
);
582 /* Some architectures don't have PAGE_KERNEL_RO */
583 #ifndef PAGE_KERNEL_RO
584 #define PAGE_KERNEL_RO PAGE_KERNEL
587 /* one pages buffer should be mapped/unmapped only once */
588 static int fw_map_pages_buf(struct firmware_buf
*buf
)
590 if (!buf
->is_paged_buf
)
594 buf
->data
= vmap(buf
->pages
, buf
->nr_pages
, 0, PAGE_KERNEL_RO
);
601 * firmware_loading_store - set value in the 'loading' control file
602 * @dev: device pointer
603 * @attr: device attribute pointer
604 * @buf: buffer to scan for loading control value
605 * @count: number of bytes in @buf
607 * The relevant values are:
609 * 1: Start a load, discarding any previous partial load.
610 * 0: Conclude the load and hand the data to the driver code.
611 * -1: Conclude the load with an error and discard any written data.
613 static ssize_t
firmware_loading_store(struct device
*dev
,
614 struct device_attribute
*attr
,
615 const char *buf
, size_t count
)
617 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
618 struct firmware_buf
*fw_buf
;
619 ssize_t written
= count
;
620 int loading
= simple_strtol(buf
, NULL
, 10);
623 mutex_lock(&fw_lock
);
624 fw_buf
= fw_priv
->buf
;
630 /* discarding any previous partial load */
631 if (!test_bit(FW_STATUS_DONE
, &fw_buf
->status
)) {
632 for (i
= 0; i
< fw_buf
->nr_pages
; i
++)
633 __free_page(fw_buf
->pages
[i
]);
634 kfree(fw_buf
->pages
);
635 fw_buf
->pages
= NULL
;
636 fw_buf
->page_array_size
= 0;
637 fw_buf
->nr_pages
= 0;
638 set_bit(FW_STATUS_LOADING
, &fw_buf
->status
);
642 if (test_bit(FW_STATUS_LOADING
, &fw_buf
->status
)) {
645 set_bit(FW_STATUS_DONE
, &fw_buf
->status
);
646 clear_bit(FW_STATUS_LOADING
, &fw_buf
->status
);
649 * Several loading requests may be pending on
650 * one same firmware buf, so let all requests
651 * see the mapped 'buf->data' once the loading
654 rc
= fw_map_pages_buf(fw_buf
);
656 dev_err(dev
, "%s: map pages failed\n",
659 rc
= security_kernel_post_read_file(NULL
,
660 fw_buf
->data
, fw_buf
->size
,
664 * Same logic as fw_load_abort, only the DONE bit
665 * is ignored and we set ABORT only on failure.
667 list_del_init(&fw_buf
->pending_list
);
669 set_bit(FW_STATUS_ABORT
, &fw_buf
->status
);
672 complete_all(&fw_buf
->completion
);
677 dev_err(dev
, "%s: unexpected value (%d)\n", __func__
, loading
);
680 fw_load_abort(fw_priv
);
684 mutex_unlock(&fw_lock
);
688 static DEVICE_ATTR(loading
, 0644, firmware_loading_show
, firmware_loading_store
);
690 static ssize_t
firmware_data_read(struct file
*filp
, struct kobject
*kobj
,
691 struct bin_attribute
*bin_attr
,
692 char *buffer
, loff_t offset
, size_t count
)
694 struct device
*dev
= kobj_to_dev(kobj
);
695 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
696 struct firmware_buf
*buf
;
699 mutex_lock(&fw_lock
);
701 if (!buf
|| test_bit(FW_STATUS_DONE
, &buf
->status
)) {
705 if (offset
> buf
->size
) {
709 if (count
> buf
->size
- offset
)
710 count
= buf
->size
- offset
;
716 int page_nr
= offset
>> PAGE_SHIFT
;
717 int page_ofs
= offset
& (PAGE_SIZE
-1);
718 int page_cnt
= min_t(size_t, PAGE_SIZE
- page_ofs
, count
);
720 page_data
= kmap(buf
->pages
[page_nr
]);
722 memcpy(buffer
, page_data
+ page_ofs
, page_cnt
);
724 kunmap(buf
->pages
[page_nr
]);
730 mutex_unlock(&fw_lock
);
734 static int fw_realloc_buffer(struct firmware_priv
*fw_priv
, int min_size
)
736 struct firmware_buf
*buf
= fw_priv
->buf
;
737 int pages_needed
= PAGE_ALIGN(min_size
) >> PAGE_SHIFT
;
739 /* If the array of pages is too small, grow it... */
740 if (buf
->page_array_size
< pages_needed
) {
741 int new_array_size
= max(pages_needed
,
742 buf
->page_array_size
* 2);
743 struct page
**new_pages
;
745 new_pages
= kmalloc(new_array_size
* sizeof(void *),
748 fw_load_abort(fw_priv
);
751 memcpy(new_pages
, buf
->pages
,
752 buf
->page_array_size
* sizeof(void *));
753 memset(&new_pages
[buf
->page_array_size
], 0, sizeof(void *) *
754 (new_array_size
- buf
->page_array_size
));
756 buf
->pages
= new_pages
;
757 buf
->page_array_size
= new_array_size
;
760 while (buf
->nr_pages
< pages_needed
) {
761 buf
->pages
[buf
->nr_pages
] =
762 alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
764 if (!buf
->pages
[buf
->nr_pages
]) {
765 fw_load_abort(fw_priv
);
774 * firmware_data_write - write method for firmware
775 * @filp: open sysfs file
776 * @kobj: kobject for the device
777 * @bin_attr: bin_attr structure
778 * @buffer: buffer being written
779 * @offset: buffer offset for write in total data store area
780 * @count: buffer size
782 * Data written to the 'data' attribute will be later handed to
783 * the driver as a firmware image.
785 static ssize_t
firmware_data_write(struct file
*filp
, struct kobject
*kobj
,
786 struct bin_attribute
*bin_attr
,
787 char *buffer
, loff_t offset
, size_t count
)
789 struct device
*dev
= kobj_to_dev(kobj
);
790 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
791 struct firmware_buf
*buf
;
794 if (!capable(CAP_SYS_RAWIO
))
797 mutex_lock(&fw_lock
);
799 if (!buf
|| test_bit(FW_STATUS_DONE
, &buf
->status
)) {
804 retval
= fw_realloc_buffer(fw_priv
, offset
+ count
);
812 int page_nr
= offset
>> PAGE_SHIFT
;
813 int page_ofs
= offset
& (PAGE_SIZE
- 1);
814 int page_cnt
= min_t(size_t, PAGE_SIZE
- page_ofs
, count
);
816 page_data
= kmap(buf
->pages
[page_nr
]);
818 memcpy(page_data
+ page_ofs
, buffer
, page_cnt
);
820 kunmap(buf
->pages
[page_nr
]);
826 buf
->size
= max_t(size_t, offset
, buf
->size
);
828 mutex_unlock(&fw_lock
);
832 static struct bin_attribute firmware_attr_data
= {
833 .attr
= { .name
= "data", .mode
= 0644 },
835 .read
= firmware_data_read
,
836 .write
= firmware_data_write
,
839 static struct attribute
*fw_dev_attrs
[] = {
840 &dev_attr_loading
.attr
,
844 static struct bin_attribute
*fw_dev_bin_attrs
[] = {
849 static const struct attribute_group fw_dev_attr_group
= {
850 .attrs
= fw_dev_attrs
,
851 .bin_attrs
= fw_dev_bin_attrs
,
854 static const struct attribute_group
*fw_dev_attr_groups
[] = {
859 static struct firmware_priv
*
860 fw_create_instance(struct firmware
*firmware
, const char *fw_name
,
861 struct device
*device
, unsigned int opt_flags
)
863 struct firmware_priv
*fw_priv
;
864 struct device
*f_dev
;
866 fw_priv
= kzalloc(sizeof(*fw_priv
), GFP_KERNEL
);
868 fw_priv
= ERR_PTR(-ENOMEM
);
872 fw_priv
->nowait
= !!(opt_flags
& FW_OPT_NOWAIT
);
873 fw_priv
->fw
= firmware
;
874 f_dev
= &fw_priv
->dev
;
876 device_initialize(f_dev
);
877 dev_set_name(f_dev
, "%s", fw_name
);
878 f_dev
->parent
= device
;
879 f_dev
->class = &firmware_class
;
880 f_dev
->groups
= fw_dev_attr_groups
;
885 /* load a firmware via user helper */
886 static int _request_firmware_load(struct firmware_priv
*fw_priv
,
887 unsigned int opt_flags
, long timeout
)
890 struct device
*f_dev
= &fw_priv
->dev
;
891 struct firmware_buf
*buf
= fw_priv
->buf
;
893 /* fall back on userspace loading */
894 buf
->is_paged_buf
= true;
896 dev_set_uevent_suppress(f_dev
, true);
898 retval
= device_add(f_dev
);
900 dev_err(f_dev
, "%s: device_register failed\n", __func__
);
904 mutex_lock(&fw_lock
);
905 list_add(&buf
->pending_list
, &pending_fw_head
);
906 mutex_unlock(&fw_lock
);
908 if (opt_flags
& FW_OPT_UEVENT
) {
909 buf
->need_uevent
= true;
910 dev_set_uevent_suppress(f_dev
, false);
911 dev_dbg(f_dev
, "firmware: requesting %s\n", buf
->fw_id
);
912 kobject_uevent(&fw_priv
->dev
.kobj
, KOBJ_ADD
);
914 timeout
= MAX_JIFFY_OFFSET
;
917 retval
= wait_for_completion_interruptible_timeout(&buf
->completion
,
919 if (retval
== -ERESTARTSYS
|| !retval
) {
920 mutex_lock(&fw_lock
);
921 fw_load_abort(fw_priv
);
922 mutex_unlock(&fw_lock
);
923 } else if (retval
> 0) {
927 if (is_fw_load_aborted(buf
))
938 static int fw_load_from_user_helper(struct firmware
*firmware
,
939 const char *name
, struct device
*device
,
940 unsigned int opt_flags
, long timeout
)
942 struct firmware_priv
*fw_priv
;
944 fw_priv
= fw_create_instance(firmware
, name
, device
, opt_flags
);
946 return PTR_ERR(fw_priv
);
948 fw_priv
->buf
= firmware
->priv
;
949 return _request_firmware_load(fw_priv
, opt_flags
, timeout
);
952 #ifdef CONFIG_PM_SLEEP
953 /* kill pending requests without uevent to avoid blocking suspend */
954 static void kill_requests_without_uevent(void)
956 struct firmware_buf
*buf
;
957 struct firmware_buf
*next
;
959 mutex_lock(&fw_lock
);
960 list_for_each_entry_safe(buf
, next
, &pending_fw_head
, pending_list
) {
961 if (!buf
->need_uevent
)
962 __fw_load_abort(buf
);
964 mutex_unlock(&fw_lock
);
968 #else /* CONFIG_FW_LOADER_USER_HELPER */
970 fw_load_from_user_helper(struct firmware
*firmware
, const char *name
,
971 struct device
*device
, unsigned int opt_flags
,
977 /* No abort during direct loading */
978 #define is_fw_load_aborted(buf) false
980 #ifdef CONFIG_PM_SLEEP
981 static inline void kill_requests_without_uevent(void) { }
984 #endif /* CONFIG_FW_LOADER_USER_HELPER */
987 /* wait until the shared firmware_buf becomes ready (or error) */
988 static int sync_cached_firmware_buf(struct firmware_buf
*buf
)
992 mutex_lock(&fw_lock
);
993 while (!test_bit(FW_STATUS_DONE
, &buf
->status
)) {
994 if (is_fw_load_aborted(buf
)) {
998 mutex_unlock(&fw_lock
);
999 ret
= wait_for_completion_interruptible(&buf
->completion
);
1000 mutex_lock(&fw_lock
);
1002 mutex_unlock(&fw_lock
);
1006 /* prepare firmware and firmware_buf structs;
1007 * return 0 if a firmware is already assigned, 1 if need to load one,
1008 * or a negative error code
1011 _request_firmware_prepare(struct firmware
**firmware_p
, const char *name
,
1012 struct device
*device
)
1014 struct firmware
*firmware
;
1015 struct firmware_buf
*buf
;
1018 *firmware_p
= firmware
= kzalloc(sizeof(*firmware
), GFP_KERNEL
);
1020 dev_err(device
, "%s: kmalloc(struct firmware) failed\n",
1025 if (fw_get_builtin_firmware(firmware
, name
)) {
1026 dev_dbg(device
, "using built-in %s\n", name
);
1027 return 0; /* assigned */
1030 ret
= fw_lookup_and_allocate_buf(name
, &fw_cache
, &buf
);
1033 * bind with 'buf' now to avoid warning in failure path
1034 * of requesting firmware.
1036 firmware
->priv
= buf
;
1039 ret
= sync_cached_firmware_buf(buf
);
1041 fw_set_page_data(buf
, firmware
);
1042 return 0; /* assigned */
1048 return 1; /* need to load */
1051 static int assign_firmware_buf(struct firmware
*fw
, struct device
*device
,
1052 unsigned int opt_flags
)
1054 struct firmware_buf
*buf
= fw
->priv
;
1056 mutex_lock(&fw_lock
);
1057 if (!buf
->size
|| is_fw_load_aborted(buf
)) {
1058 mutex_unlock(&fw_lock
);
1063 * add firmware name into devres list so that we can auto cache
1064 * and uncache firmware for device.
1066 * device may has been deleted already, but the problem
1067 * should be fixed in devres or driver core.
1069 /* don't cache firmware handled without uevent */
1070 if (device
&& (opt_flags
& FW_OPT_UEVENT
))
1071 fw_add_devm_name(device
, buf
->fw_id
);
1074 * After caching firmware image is started, let it piggyback
1075 * on request firmware.
1077 if (buf
->fwc
->state
== FW_LOADER_START_CACHE
) {
1078 if (fw_cache_piggyback_on_request(buf
->fw_id
))
1079 kref_get(&buf
->ref
);
1082 /* pass the pages buffer to driver at the last minute */
1083 fw_set_page_data(buf
, fw
);
1084 mutex_unlock(&fw_lock
);
1088 /* called from request_firmware() and request_firmware_work_func() */
1090 _request_firmware(const struct firmware
**firmware_p
, const char *name
,
1091 struct device
*device
, unsigned int opt_flags
)
1093 struct firmware
*fw
= NULL
;
1100 if (!name
|| name
[0] == '\0') {
1105 ret
= _request_firmware_prepare(&fw
, name
, device
);
1106 if (ret
<= 0) /* error or already assigned */
1110 timeout
= firmware_loading_timeout();
1111 if (opt_flags
& FW_OPT_NOWAIT
) {
1112 timeout
= usermodehelper_read_lock_wait(timeout
);
1114 dev_dbg(device
, "firmware: %s loading timed out\n",
1120 ret
= usermodehelper_read_trylock();
1122 dev_err(device
, "firmware: %s will not be loaded\n",
1128 ret
= fw_get_filesystem_firmware(device
, fw
->priv
);
1130 if (!(opt_flags
& FW_OPT_NO_WARN
))
1132 "Direct firmware load for %s failed with error %d\n",
1134 if (opt_flags
& FW_OPT_USERHELPER
) {
1135 dev_warn(device
, "Falling back to user helper\n");
1136 ret
= fw_load_from_user_helper(fw
, name
, device
,
1137 opt_flags
, timeout
);
1142 ret
= assign_firmware_buf(fw
, device
, opt_flags
);
1144 usermodehelper_read_unlock();
1148 release_firmware(fw
);
1157 * request_firmware: - send firmware request and wait for it
1158 * @firmware_p: pointer to firmware image
1159 * @name: name of firmware file
1160 * @device: device for which firmware is being loaded
1162 * @firmware_p will be used to return a firmware image by the name
1163 * of @name for device @device.
1165 * Should be called from user context where sleeping is allowed.
1167 * @name will be used as $FIRMWARE in the uevent environment and
1168 * should be distinctive enough not to be confused with any other
1169 * firmware image for this or any other device.
1171 * Caller must hold the reference count of @device.
1173 * The function can be called safely inside device's suspend and
1177 request_firmware(const struct firmware
**firmware_p
, const char *name
,
1178 struct device
*device
)
1182 /* Need to pin this module until return */
1183 __module_get(THIS_MODULE
);
1184 ret
= _request_firmware(firmware_p
, name
, device
,
1185 FW_OPT_UEVENT
| FW_OPT_FALLBACK
);
1186 module_put(THIS_MODULE
);
1189 EXPORT_SYMBOL(request_firmware
);
1192 * request_firmware_direct: - load firmware directly without usermode helper
1193 * @firmware_p: pointer to firmware image
1194 * @name: name of firmware file
1195 * @device: device for which firmware is being loaded
1197 * This function works pretty much like request_firmware(), but this doesn't
1198 * fall back to usermode helper even if the firmware couldn't be loaded
1199 * directly from fs. Hence it's useful for loading optional firmwares, which
1200 * aren't always present, without extra long timeouts of udev.
1202 int request_firmware_direct(const struct firmware
**firmware_p
,
1203 const char *name
, struct device
*device
)
1207 __module_get(THIS_MODULE
);
1208 ret
= _request_firmware(firmware_p
, name
, device
,
1209 FW_OPT_UEVENT
| FW_OPT_NO_WARN
);
1210 module_put(THIS_MODULE
);
1213 EXPORT_SYMBOL_GPL(request_firmware_direct
);
1216 * release_firmware: - release the resource associated with a firmware image
1217 * @fw: firmware resource to release
1219 void release_firmware(const struct firmware
*fw
)
1222 if (!fw_is_builtin_firmware(fw
))
1223 firmware_free_data(fw
);
1227 EXPORT_SYMBOL(release_firmware
);
1230 struct firmware_work
{
1231 struct work_struct work
;
1232 struct module
*module
;
1234 struct device
*device
;
1236 void (*cont
)(const struct firmware
*fw
, void *context
);
1237 unsigned int opt_flags
;
1240 static void request_firmware_work_func(struct work_struct
*work
)
1242 struct firmware_work
*fw_work
;
1243 const struct firmware
*fw
;
1245 fw_work
= container_of(work
, struct firmware_work
, work
);
1247 _request_firmware(&fw
, fw_work
->name
, fw_work
->device
,
1248 fw_work
->opt_flags
);
1249 fw_work
->cont(fw
, fw_work
->context
);
1250 put_device(fw_work
->device
); /* taken in request_firmware_nowait() */
1252 module_put(fw_work
->module
);
1253 kfree_const(fw_work
->name
);
1258 * request_firmware_nowait - asynchronous version of request_firmware
1259 * @module: module requesting the firmware
1260 * @uevent: sends uevent to copy the firmware image if this flag
1261 * is non-zero else the firmware copy must be done manually.
1262 * @name: name of firmware file
1263 * @device: device for which firmware is being loaded
1264 * @gfp: allocation flags
1265 * @context: will be passed over to @cont, and
1266 * @fw may be %NULL if firmware request fails.
1267 * @cont: function will be called asynchronously when the firmware
1270 * Caller must hold the reference count of @device.
1272 * Asynchronous variant of request_firmware() for user contexts:
1273 * - sleep for as small periods as possible since it may
1274 * increase kernel boot time of built-in device drivers
1275 * requesting firmware in their ->probe() methods, if
1276 * @gfp is GFP_KERNEL.
1278 * - can't sleep at all if @gfp is GFP_ATOMIC.
1281 request_firmware_nowait(
1282 struct module
*module
, bool uevent
,
1283 const char *name
, struct device
*device
, gfp_t gfp
, void *context
,
1284 void (*cont
)(const struct firmware
*fw
, void *context
))
1286 struct firmware_work
*fw_work
;
1288 fw_work
= kzalloc(sizeof(struct firmware_work
), gfp
);
1292 fw_work
->module
= module
;
1293 fw_work
->name
= kstrdup_const(name
, gfp
);
1294 if (!fw_work
->name
) {
1298 fw_work
->device
= device
;
1299 fw_work
->context
= context
;
1300 fw_work
->cont
= cont
;
1301 fw_work
->opt_flags
= FW_OPT_NOWAIT
| FW_OPT_FALLBACK
|
1302 (uevent
? FW_OPT_UEVENT
: FW_OPT_USERHELPER
);
1304 if (!try_module_get(module
)) {
1305 kfree_const(fw_work
->name
);
1310 get_device(fw_work
->device
);
1311 INIT_WORK(&fw_work
->work
, request_firmware_work_func
);
1312 schedule_work(&fw_work
->work
);
1315 EXPORT_SYMBOL(request_firmware_nowait
);
1317 #ifdef CONFIG_PM_SLEEP
1318 static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain
);
1321 * cache_firmware - cache one firmware image in kernel memory space
1322 * @fw_name: the firmware image name
1324 * Cache firmware in kernel memory so that drivers can use it when
1325 * system isn't ready for them to request firmware image from userspace.
1326 * Once it returns successfully, driver can use request_firmware or its
1327 * nowait version to get the cached firmware without any interacting
1330 * Return 0 if the firmware image has been cached successfully
1331 * Return !0 otherwise
1334 static int cache_firmware(const char *fw_name
)
1337 const struct firmware
*fw
;
1339 pr_debug("%s: %s\n", __func__
, fw_name
);
1341 ret
= request_firmware(&fw
, fw_name
, NULL
);
1345 pr_debug("%s: %s ret=%d\n", __func__
, fw_name
, ret
);
1350 static struct firmware_buf
*fw_lookup_buf(const char *fw_name
)
1352 struct firmware_buf
*tmp
;
1353 struct firmware_cache
*fwc
= &fw_cache
;
1355 spin_lock(&fwc
->lock
);
1356 tmp
= __fw_lookup_buf(fw_name
);
1357 spin_unlock(&fwc
->lock
);
1363 * uncache_firmware - remove one cached firmware image
1364 * @fw_name: the firmware image name
1366 * Uncache one firmware image which has been cached successfully
1369 * Return 0 if the firmware cache has been removed successfully
1370 * Return !0 otherwise
1373 static int uncache_firmware(const char *fw_name
)
1375 struct firmware_buf
*buf
;
1378 pr_debug("%s: %s\n", __func__
, fw_name
);
1380 if (fw_get_builtin_firmware(&fw
, fw_name
))
1383 buf
= fw_lookup_buf(fw_name
);
1392 static struct fw_cache_entry
*alloc_fw_cache_entry(const char *name
)
1394 struct fw_cache_entry
*fce
;
1396 fce
= kzalloc(sizeof(*fce
), GFP_ATOMIC
);
1400 fce
->name
= kstrdup_const(name
, GFP_ATOMIC
);
1410 static int __fw_entry_found(const char *name
)
1412 struct firmware_cache
*fwc
= &fw_cache
;
1413 struct fw_cache_entry
*fce
;
1415 list_for_each_entry(fce
, &fwc
->fw_names
, list
) {
1416 if (!strcmp(fce
->name
, name
))
1422 static int fw_cache_piggyback_on_request(const char *name
)
1424 struct firmware_cache
*fwc
= &fw_cache
;
1425 struct fw_cache_entry
*fce
;
1428 spin_lock(&fwc
->name_lock
);
1429 if (__fw_entry_found(name
))
1432 fce
= alloc_fw_cache_entry(name
);
1435 list_add(&fce
->list
, &fwc
->fw_names
);
1436 pr_debug("%s: fw: %s\n", __func__
, name
);
1439 spin_unlock(&fwc
->name_lock
);
1443 static void free_fw_cache_entry(struct fw_cache_entry
*fce
)
1445 kfree_const(fce
->name
);
1449 static void __async_dev_cache_fw_image(void *fw_entry
,
1450 async_cookie_t cookie
)
1452 struct fw_cache_entry
*fce
= fw_entry
;
1453 struct firmware_cache
*fwc
= &fw_cache
;
1456 ret
= cache_firmware(fce
->name
);
1458 spin_lock(&fwc
->name_lock
);
1459 list_del(&fce
->list
);
1460 spin_unlock(&fwc
->name_lock
);
1462 free_fw_cache_entry(fce
);
1466 /* called with dev->devres_lock held */
1467 static void dev_create_fw_entry(struct device
*dev
, void *res
,
1470 struct fw_name_devm
*fwn
= res
;
1471 const char *fw_name
= fwn
->name
;
1472 struct list_head
*head
= data
;
1473 struct fw_cache_entry
*fce
;
1475 fce
= alloc_fw_cache_entry(fw_name
);
1477 list_add(&fce
->list
, head
);
1480 static int devm_name_match(struct device
*dev
, void *res
,
1483 struct fw_name_devm
*fwn
= res
;
1484 return (fwn
->magic
== (unsigned long)match_data
);
1487 static void dev_cache_fw_image(struct device
*dev
, void *data
)
1490 struct fw_cache_entry
*fce
;
1491 struct fw_cache_entry
*fce_next
;
1492 struct firmware_cache
*fwc
= &fw_cache
;
1494 devres_for_each_res(dev
, fw_name_devm_release
,
1495 devm_name_match
, &fw_cache
,
1496 dev_create_fw_entry
, &todo
);
1498 list_for_each_entry_safe(fce
, fce_next
, &todo
, list
) {
1499 list_del(&fce
->list
);
1501 spin_lock(&fwc
->name_lock
);
1502 /* only one cache entry for one firmware */
1503 if (!__fw_entry_found(fce
->name
)) {
1504 list_add(&fce
->list
, &fwc
->fw_names
);
1506 free_fw_cache_entry(fce
);
1509 spin_unlock(&fwc
->name_lock
);
1512 async_schedule_domain(__async_dev_cache_fw_image
,
1518 static void __device_uncache_fw_images(void)
1520 struct firmware_cache
*fwc
= &fw_cache
;
1521 struct fw_cache_entry
*fce
;
1523 spin_lock(&fwc
->name_lock
);
1524 while (!list_empty(&fwc
->fw_names
)) {
1525 fce
= list_entry(fwc
->fw_names
.next
,
1526 struct fw_cache_entry
, list
);
1527 list_del(&fce
->list
);
1528 spin_unlock(&fwc
->name_lock
);
1530 uncache_firmware(fce
->name
);
1531 free_fw_cache_entry(fce
);
1533 spin_lock(&fwc
->name_lock
);
1535 spin_unlock(&fwc
->name_lock
);
1539 * device_cache_fw_images - cache devices' firmware
1541 * If one device called request_firmware or its nowait version
1542 * successfully before, the firmware names are recored into the
1543 * device's devres link list, so device_cache_fw_images can call
1544 * cache_firmware() to cache these firmwares for the device,
1545 * then the device driver can load its firmwares easily at
1546 * time when system is not ready to complete loading firmware.
1548 static void device_cache_fw_images(void)
1550 struct firmware_cache
*fwc
= &fw_cache
;
1554 pr_debug("%s\n", __func__
);
1556 /* cancel uncache work */
1557 cancel_delayed_work_sync(&fwc
->work
);
1560 * use small loading timeout for caching devices' firmware
1561 * because all these firmware images have been loaded
1562 * successfully at lease once, also system is ready for
1563 * completing firmware loading now. The maximum size of
1564 * firmware in current distributions is about 2M bytes,
1565 * so 10 secs should be enough.
1567 old_timeout
= loading_timeout
;
1568 loading_timeout
= 10;
1570 mutex_lock(&fw_lock
);
1571 fwc
->state
= FW_LOADER_START_CACHE
;
1572 dpm_for_each_dev(NULL
, dev_cache_fw_image
);
1573 mutex_unlock(&fw_lock
);
1575 /* wait for completion of caching firmware for all devices */
1576 async_synchronize_full_domain(&fw_cache_domain
);
1578 loading_timeout
= old_timeout
;
1582 * device_uncache_fw_images - uncache devices' firmware
1584 * uncache all firmwares which have been cached successfully
1585 * by device_uncache_fw_images earlier
1587 static void device_uncache_fw_images(void)
1589 pr_debug("%s\n", __func__
);
1590 __device_uncache_fw_images();
1593 static void device_uncache_fw_images_work(struct work_struct
*work
)
1595 device_uncache_fw_images();
1599 * device_uncache_fw_images_delay - uncache devices firmwares
1600 * @delay: number of milliseconds to delay uncache device firmwares
1602 * uncache all devices's firmwares which has been cached successfully
1603 * by device_cache_fw_images after @delay milliseconds.
1605 static void device_uncache_fw_images_delay(unsigned long delay
)
1607 queue_delayed_work(system_power_efficient_wq
, &fw_cache
.work
,
1608 msecs_to_jiffies(delay
));
1611 static int fw_pm_notify(struct notifier_block
*notify_block
,
1612 unsigned long mode
, void *unused
)
1615 case PM_HIBERNATION_PREPARE
:
1616 case PM_SUSPEND_PREPARE
:
1617 case PM_RESTORE_PREPARE
:
1618 kill_requests_without_uevent();
1619 device_cache_fw_images();
1622 case PM_POST_SUSPEND
:
1623 case PM_POST_HIBERNATION
:
1624 case PM_POST_RESTORE
:
1626 * In case that system sleep failed and syscore_suspend is
1629 mutex_lock(&fw_lock
);
1630 fw_cache
.state
= FW_LOADER_NO_CACHE
;
1631 mutex_unlock(&fw_lock
);
1633 device_uncache_fw_images_delay(10 * MSEC_PER_SEC
);
1640 /* stop caching firmware once syscore_suspend is reached */
1641 static int fw_suspend(void)
1643 fw_cache
.state
= FW_LOADER_NO_CACHE
;
1647 static struct syscore_ops fw_syscore_ops
= {
1648 .suspend
= fw_suspend
,
1651 static int fw_cache_piggyback_on_request(const char *name
)
1657 static void __init
fw_cache_init(void)
1659 spin_lock_init(&fw_cache
.lock
);
1660 INIT_LIST_HEAD(&fw_cache
.head
);
1661 fw_cache
.state
= FW_LOADER_NO_CACHE
;
1663 #ifdef CONFIG_PM_SLEEP
1664 spin_lock_init(&fw_cache
.name_lock
);
1665 INIT_LIST_HEAD(&fw_cache
.fw_names
);
1667 INIT_DELAYED_WORK(&fw_cache
.work
,
1668 device_uncache_fw_images_work
);
1670 fw_cache
.pm_notify
.notifier_call
= fw_pm_notify
;
1671 register_pm_notifier(&fw_cache
.pm_notify
);
1673 register_syscore_ops(&fw_syscore_ops
);
1677 static int __init
firmware_class_init(void)
1680 #ifdef CONFIG_FW_LOADER_USER_HELPER
1681 register_reboot_notifier(&fw_shutdown_nb
);
1682 return class_register(&firmware_class
);
1688 static void __exit
firmware_class_exit(void)
1690 #ifdef CONFIG_PM_SLEEP
1691 unregister_syscore_ops(&fw_syscore_ops
);
1692 unregister_pm_notifier(&fw_cache
.pm_notify
);
1694 #ifdef CONFIG_FW_LOADER_USER_HELPER
1695 unregister_reboot_notifier(&fw_shutdown_nb
);
1696 class_unregister(&firmware_class
);
1700 fs_initcall(firmware_class_init
);
1701 module_exit(firmware_class_exit
);