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>
26 #include <linux/async.h>
28 #include <linux/suspend.h>
29 #include <linux/syscore_ops.h>
31 #include <generated/utsrelease.h>
35 MODULE_AUTHOR("Manuel Estrada Sainz");
36 MODULE_DESCRIPTION("Multi purpose firmware loading support");
37 MODULE_LICENSE("GPL");
39 /* Builtin firmware support */
41 #ifdef CONFIG_FW_LOADER
43 extern struct builtin_fw __start_builtin_fw
[];
44 extern struct builtin_fw __end_builtin_fw
[];
46 static bool fw_get_builtin_firmware(struct firmware
*fw
, const char *name
)
48 struct builtin_fw
*b_fw
;
50 for (b_fw
= __start_builtin_fw
; b_fw
!= __end_builtin_fw
; b_fw
++) {
51 if (strcmp(name
, b_fw
->name
) == 0) {
52 fw
->size
= b_fw
->size
;
53 fw
->data
= b_fw
->data
;
61 static bool fw_is_builtin_firmware(const struct firmware
*fw
)
63 struct builtin_fw
*b_fw
;
65 for (b_fw
= __start_builtin_fw
; b_fw
!= __end_builtin_fw
; b_fw
++)
66 if (fw
->data
== b_fw
->data
)
72 #else /* Module case - no builtin firmware support */
74 static inline bool fw_get_builtin_firmware(struct firmware
*fw
, const char *name
)
79 static inline bool fw_is_builtin_firmware(const struct firmware
*fw
)
91 static int loading_timeout
= 60; /* In seconds */
93 static inline long firmware_loading_timeout(void)
95 return loading_timeout
> 0 ? loading_timeout
* HZ
: MAX_SCHEDULE_TIMEOUT
;
98 struct firmware_cache
{
99 /* firmware_buf instance will be added into the below list */
101 struct list_head head
;
104 #ifdef CONFIG_PM_SLEEP
106 * Names of firmware images which have been cached successfully
107 * will be added into the below list so that device uncache
108 * helper can trace which firmware images have been cached
111 spinlock_t name_lock
;
112 struct list_head fw_names
;
114 struct delayed_work work
;
116 struct notifier_block pm_notify
;
120 struct firmware_buf
{
122 struct list_head list
;
123 struct completion completion
;
124 struct firmware_cache
*fwc
;
125 unsigned long status
;
128 #ifdef CONFIG_FW_LOADER_USER_HELPER
137 struct fw_cache_entry
{
138 struct list_head list
;
142 struct fw_name_devm
{
147 #define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
149 #define FW_LOADER_NO_CACHE 0
150 #define FW_LOADER_START_CACHE 1
152 static int fw_cache_piggyback_on_request(const char *name
);
154 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
155 * guarding for corner cases a global lock should be OK */
156 static DEFINE_MUTEX(fw_lock
);
158 static struct firmware_cache fw_cache
;
160 static struct firmware_buf
*__allocate_fw_buf(const char *fw_name
,
161 struct firmware_cache
*fwc
)
163 struct firmware_buf
*buf
;
165 buf
= kzalloc(sizeof(*buf
) + strlen(fw_name
) + 1 , GFP_ATOMIC
);
170 kref_init(&buf
->ref
);
171 strcpy(buf
->fw_id
, fw_name
);
173 init_completion(&buf
->completion
);
175 pr_debug("%s: fw-%s buf=%p\n", __func__
, fw_name
, buf
);
180 static struct firmware_buf
*__fw_lookup_buf(const char *fw_name
)
182 struct firmware_buf
*tmp
;
183 struct firmware_cache
*fwc
= &fw_cache
;
185 list_for_each_entry(tmp
, &fwc
->head
, list
)
186 if (!strcmp(tmp
->fw_id
, fw_name
))
191 static int fw_lookup_and_allocate_buf(const char *fw_name
,
192 struct firmware_cache
*fwc
,
193 struct firmware_buf
**buf
)
195 struct firmware_buf
*tmp
;
197 spin_lock(&fwc
->lock
);
198 tmp
= __fw_lookup_buf(fw_name
);
201 spin_unlock(&fwc
->lock
);
205 tmp
= __allocate_fw_buf(fw_name
, fwc
);
207 list_add(&tmp
->list
, &fwc
->head
);
208 spin_unlock(&fwc
->lock
);
212 return tmp
? 0 : -ENOMEM
;
215 static struct firmware_buf
*fw_lookup_buf(const char *fw_name
)
217 struct firmware_buf
*tmp
;
218 struct firmware_cache
*fwc
= &fw_cache
;
220 spin_lock(&fwc
->lock
);
221 tmp
= __fw_lookup_buf(fw_name
);
222 spin_unlock(&fwc
->lock
);
227 static void __fw_free_buf(struct kref
*ref
)
229 struct firmware_buf
*buf
= to_fwbuf(ref
);
230 struct firmware_cache
*fwc
= buf
->fwc
;
232 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
233 __func__
, buf
->fw_id
, buf
, buf
->data
,
234 (unsigned int)buf
->size
);
236 list_del(&buf
->list
);
237 spin_unlock(&fwc
->lock
);
239 #ifdef CONFIG_FW_LOADER_USER_HELPER
240 if (buf
->is_paged_buf
) {
243 for (i
= 0; i
< buf
->nr_pages
; i
++)
244 __free_page(buf
->pages
[i
]);
252 static void fw_free_buf(struct firmware_buf
*buf
)
254 struct firmware_cache
*fwc
= buf
->fwc
;
255 spin_lock(&fwc
->lock
);
256 if (!kref_put(&buf
->ref
, __fw_free_buf
))
257 spin_unlock(&fwc
->lock
);
260 /* direct firmware loading support */
261 static char fw_path_para
[256];
262 static const char * const fw_path
[] = {
264 "/lib/firmware/updates/" UTS_RELEASE
,
265 "/lib/firmware/updates",
266 "/lib/firmware/" UTS_RELEASE
,
271 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
272 * from kernel command line because firmware_class is generally built in
273 * kernel instead of module.
275 module_param_string(path
, fw_path_para
, sizeof(fw_path_para
), 0644);
276 MODULE_PARM_DESC(path
, "customized firmware image search path with a higher priority than default path");
278 /* Don't inline this: 'struct kstat' is biggish */
279 static noinline_for_stack
long fw_file_size(struct file
*file
)
282 if (vfs_getattr(&file
->f_path
, &st
))
284 if (!S_ISREG(st
.mode
))
286 if (st
.size
!= (long)st
.size
)
291 static bool fw_read_file_contents(struct file
*file
, struct firmware_buf
*fw_buf
)
296 size
= fw_file_size(file
);
302 if (kernel_read(file
, 0, buf
, size
) != size
) {
311 static bool fw_get_filesystem_firmware(struct device
*device
,
312 struct firmware_buf
*buf
)
315 bool success
= false;
316 char *path
= __getname();
318 for (i
= 0; i
< ARRAY_SIZE(fw_path
); i
++) {
321 /* skip the unset customized path */
325 snprintf(path
, PATH_MAX
, "%s/%s", fw_path
[i
], buf
->fw_id
);
327 file
= filp_open(path
, O_RDONLY
, 0);
330 success
= fw_read_file_contents(file
, buf
);
338 dev_dbg(device
, "firmware: direct-loading firmware %s\n",
340 mutex_lock(&fw_lock
);
341 set_bit(FW_STATUS_DONE
, &buf
->status
);
342 complete_all(&buf
->completion
);
343 mutex_unlock(&fw_lock
);
349 /* firmware holds the ownership of pages */
350 static void firmware_free_data(const struct firmware
*fw
)
352 /* Loaded directly? */
357 fw_free_buf(fw
->priv
);
360 /* store the pages buffer info firmware from buf */
361 static void fw_set_page_data(struct firmware_buf
*buf
, struct firmware
*fw
)
364 #ifdef CONFIG_FW_LOADER_USER_HELPER
365 fw
->pages
= buf
->pages
;
367 fw
->size
= buf
->size
;
368 fw
->data
= buf
->data
;
370 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
371 __func__
, buf
->fw_id
, buf
, buf
->data
,
372 (unsigned int)buf
->size
);
375 #ifdef CONFIG_PM_SLEEP
376 static void fw_name_devm_release(struct device
*dev
, void *res
)
378 struct fw_name_devm
*fwn
= res
;
380 if (fwn
->magic
== (unsigned long)&fw_cache
)
381 pr_debug("%s: fw_name-%s devm-%p released\n",
382 __func__
, fwn
->name
, res
);
385 static int fw_devm_match(struct device
*dev
, void *res
,
388 struct fw_name_devm
*fwn
= res
;
390 return (fwn
->magic
== (unsigned long)&fw_cache
) &&
391 !strcmp(fwn
->name
, match_data
);
394 static struct fw_name_devm
*fw_find_devm_name(struct device
*dev
,
397 struct fw_name_devm
*fwn
;
399 fwn
= devres_find(dev
, fw_name_devm_release
,
400 fw_devm_match
, (void *)name
);
404 /* add firmware name into devres list */
405 static int fw_add_devm_name(struct device
*dev
, const char *name
)
407 struct fw_name_devm
*fwn
;
409 fwn
= fw_find_devm_name(dev
, name
);
413 fwn
= devres_alloc(fw_name_devm_release
, sizeof(struct fw_name_devm
) +
414 strlen(name
) + 1, GFP_KERNEL
);
418 fwn
->magic
= (unsigned long)&fw_cache
;
419 strcpy(fwn
->name
, name
);
420 devres_add(dev
, fwn
);
425 static int fw_add_devm_name(struct device
*dev
, const char *name
)
433 * user-mode helper code
435 #ifdef CONFIG_FW_LOADER_USER_HELPER
436 struct firmware_priv
{
437 struct delayed_work timeout_work
;
440 struct firmware_buf
*buf
;
444 static struct firmware_priv
*to_firmware_priv(struct device
*dev
)
446 return container_of(dev
, struct firmware_priv
, dev
);
449 static void fw_load_abort(struct firmware_priv
*fw_priv
)
451 struct firmware_buf
*buf
= fw_priv
->buf
;
454 * There is a small window in which user can write to 'loading'
455 * between loading done and disappearance of 'loading'
457 if (test_bit(FW_STATUS_DONE
, &buf
->status
))
460 set_bit(FW_STATUS_ABORT
, &buf
->status
);
461 complete_all(&buf
->completion
);
463 /* avoid user action after loading abort */
467 #define is_fw_load_aborted(buf) \
468 test_bit(FW_STATUS_ABORT, &(buf)->status)
470 static ssize_t
firmware_timeout_show(struct class *class,
471 struct class_attribute
*attr
,
474 return sprintf(buf
, "%d\n", loading_timeout
);
478 * firmware_timeout_store - set number of seconds to wait for firmware
479 * @class: device class pointer
480 * @attr: device attribute pointer
481 * @buf: buffer to scan for timeout value
482 * @count: number of bytes in @buf
484 * Sets the number of seconds to wait for the firmware. Once
485 * this expires an error will be returned to the driver and no
486 * firmware will be provided.
488 * Note: zero means 'wait forever'.
490 static ssize_t
firmware_timeout_store(struct class *class,
491 struct class_attribute
*attr
,
492 const char *buf
, size_t count
)
494 loading_timeout
= simple_strtol(buf
, NULL
, 10);
495 if (loading_timeout
< 0)
501 static struct class_attribute firmware_class_attrs
[] = {
502 __ATTR(timeout
, S_IWUSR
| S_IRUGO
,
503 firmware_timeout_show
, firmware_timeout_store
),
507 static void fw_dev_release(struct device
*dev
)
509 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
513 module_put(THIS_MODULE
);
516 static int do_firmware_uevent(struct firmware_priv
*fw_priv
, struct kobj_uevent_env
*env
)
518 if (add_uevent_var(env
, "FIRMWARE=%s", fw_priv
->buf
->fw_id
))
520 if (add_uevent_var(env
, "TIMEOUT=%i", loading_timeout
))
522 if (add_uevent_var(env
, "ASYNC=%d", fw_priv
->nowait
))
528 static int firmware_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
530 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
533 mutex_lock(&fw_lock
);
535 err
= do_firmware_uevent(fw_priv
, env
);
536 mutex_unlock(&fw_lock
);
540 static struct class firmware_class
= {
542 .class_attrs
= firmware_class_attrs
,
543 .dev_uevent
= firmware_uevent
,
544 .dev_release
= fw_dev_release
,
547 static ssize_t
firmware_loading_show(struct device
*dev
,
548 struct device_attribute
*attr
, char *buf
)
550 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
553 mutex_lock(&fw_lock
);
555 loading
= test_bit(FW_STATUS_LOADING
, &fw_priv
->buf
->status
);
556 mutex_unlock(&fw_lock
);
558 return sprintf(buf
, "%d\n", loading
);
561 /* Some architectures don't have PAGE_KERNEL_RO */
562 #ifndef PAGE_KERNEL_RO
563 #define PAGE_KERNEL_RO PAGE_KERNEL
566 /* one pages buffer should be mapped/unmapped only once */
567 static int fw_map_pages_buf(struct firmware_buf
*buf
)
569 if (!buf
->is_paged_buf
)
574 buf
->data
= vmap(buf
->pages
, buf
->nr_pages
, 0, PAGE_KERNEL_RO
);
581 * firmware_loading_store - set value in the 'loading' control file
582 * @dev: device pointer
583 * @attr: device attribute pointer
584 * @buf: buffer to scan for loading control value
585 * @count: number of bytes in @buf
587 * The relevant values are:
589 * 1: Start a load, discarding any previous partial load.
590 * 0: Conclude the load and hand the data to the driver code.
591 * -1: Conclude the load with an error and discard any written data.
593 static ssize_t
firmware_loading_store(struct device
*dev
,
594 struct device_attribute
*attr
,
595 const char *buf
, size_t count
)
597 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
598 struct firmware_buf
*fw_buf
;
599 int loading
= simple_strtol(buf
, NULL
, 10);
602 mutex_lock(&fw_lock
);
603 fw_buf
= fw_priv
->buf
;
609 /* discarding any previous partial load */
610 if (!test_bit(FW_STATUS_DONE
, &fw_buf
->status
)) {
611 for (i
= 0; i
< fw_buf
->nr_pages
; i
++)
612 __free_page(fw_buf
->pages
[i
]);
613 kfree(fw_buf
->pages
);
614 fw_buf
->pages
= NULL
;
615 fw_buf
->page_array_size
= 0;
616 fw_buf
->nr_pages
= 0;
617 set_bit(FW_STATUS_LOADING
, &fw_buf
->status
);
621 if (test_bit(FW_STATUS_LOADING
, &fw_buf
->status
)) {
622 set_bit(FW_STATUS_DONE
, &fw_buf
->status
);
623 clear_bit(FW_STATUS_LOADING
, &fw_buf
->status
);
626 * Several loading requests may be pending on
627 * one same firmware buf, so let all requests
628 * see the mapped 'buf->data' once the loading
631 fw_map_pages_buf(fw_buf
);
632 complete_all(&fw_buf
->completion
);
637 dev_err(dev
, "%s: unexpected value (%d)\n", __func__
, loading
);
640 fw_load_abort(fw_priv
);
644 mutex_unlock(&fw_lock
);
648 static DEVICE_ATTR(loading
, 0644, firmware_loading_show
, firmware_loading_store
);
650 static ssize_t
firmware_data_read(struct file
*filp
, struct kobject
*kobj
,
651 struct bin_attribute
*bin_attr
,
652 char *buffer
, loff_t offset
, size_t count
)
654 struct device
*dev
= kobj_to_dev(kobj
);
655 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
656 struct firmware_buf
*buf
;
659 mutex_lock(&fw_lock
);
661 if (!buf
|| test_bit(FW_STATUS_DONE
, &buf
->status
)) {
665 if (offset
> buf
->size
) {
669 if (count
> buf
->size
- offset
)
670 count
= buf
->size
- offset
;
676 int page_nr
= offset
>> PAGE_SHIFT
;
677 int page_ofs
= offset
& (PAGE_SIZE
-1);
678 int page_cnt
= min_t(size_t, PAGE_SIZE
- page_ofs
, count
);
680 page_data
= kmap(buf
->pages
[page_nr
]);
682 memcpy(buffer
, page_data
+ page_ofs
, page_cnt
);
684 kunmap(buf
->pages
[page_nr
]);
690 mutex_unlock(&fw_lock
);
694 static int fw_realloc_buffer(struct firmware_priv
*fw_priv
, int min_size
)
696 struct firmware_buf
*buf
= fw_priv
->buf
;
697 int pages_needed
= ALIGN(min_size
, PAGE_SIZE
) >> PAGE_SHIFT
;
699 /* If the array of pages is too small, grow it... */
700 if (buf
->page_array_size
< pages_needed
) {
701 int new_array_size
= max(pages_needed
,
702 buf
->page_array_size
* 2);
703 struct page
**new_pages
;
705 new_pages
= kmalloc(new_array_size
* sizeof(void *),
708 fw_load_abort(fw_priv
);
711 memcpy(new_pages
, buf
->pages
,
712 buf
->page_array_size
* sizeof(void *));
713 memset(&new_pages
[buf
->page_array_size
], 0, sizeof(void *) *
714 (new_array_size
- buf
->page_array_size
));
716 buf
->pages
= new_pages
;
717 buf
->page_array_size
= new_array_size
;
720 while (buf
->nr_pages
< pages_needed
) {
721 buf
->pages
[buf
->nr_pages
] =
722 alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
724 if (!buf
->pages
[buf
->nr_pages
]) {
725 fw_load_abort(fw_priv
);
734 * firmware_data_write - write method for firmware
735 * @filp: open sysfs file
736 * @kobj: kobject for the device
737 * @bin_attr: bin_attr structure
738 * @buffer: buffer being written
739 * @offset: buffer offset for write in total data store area
740 * @count: buffer size
742 * Data written to the 'data' attribute will be later handed to
743 * the driver as a firmware image.
745 static ssize_t
firmware_data_write(struct file
*filp
, struct kobject
*kobj
,
746 struct bin_attribute
*bin_attr
,
747 char *buffer
, loff_t offset
, size_t count
)
749 struct device
*dev
= kobj_to_dev(kobj
);
750 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
751 struct firmware_buf
*buf
;
754 if (!capable(CAP_SYS_RAWIO
))
757 mutex_lock(&fw_lock
);
759 if (!buf
|| test_bit(FW_STATUS_DONE
, &buf
->status
)) {
764 retval
= fw_realloc_buffer(fw_priv
, offset
+ count
);
772 int page_nr
= offset
>> PAGE_SHIFT
;
773 int page_ofs
= offset
& (PAGE_SIZE
- 1);
774 int page_cnt
= min_t(size_t, PAGE_SIZE
- page_ofs
, count
);
776 page_data
= kmap(buf
->pages
[page_nr
]);
778 memcpy(page_data
+ page_ofs
, buffer
, page_cnt
);
780 kunmap(buf
->pages
[page_nr
]);
786 buf
->size
= max_t(size_t, offset
, buf
->size
);
788 mutex_unlock(&fw_lock
);
792 static struct bin_attribute firmware_attr_data
= {
793 .attr
= { .name
= "data", .mode
= 0644 },
795 .read
= firmware_data_read
,
796 .write
= firmware_data_write
,
799 static void firmware_class_timeout_work(struct work_struct
*work
)
801 struct firmware_priv
*fw_priv
= container_of(work
,
802 struct firmware_priv
, timeout_work
.work
);
804 mutex_lock(&fw_lock
);
805 fw_load_abort(fw_priv
);
806 mutex_unlock(&fw_lock
);
809 static struct firmware_priv
*
810 fw_create_instance(struct firmware
*firmware
, const char *fw_name
,
811 struct device
*device
, bool uevent
, bool nowait
)
813 struct firmware_priv
*fw_priv
;
814 struct device
*f_dev
;
816 fw_priv
= kzalloc(sizeof(*fw_priv
), GFP_KERNEL
);
818 dev_err(device
, "%s: kmalloc failed\n", __func__
);
819 fw_priv
= ERR_PTR(-ENOMEM
);
823 fw_priv
->nowait
= nowait
;
824 fw_priv
->fw
= firmware
;
825 INIT_DELAYED_WORK(&fw_priv
->timeout_work
,
826 firmware_class_timeout_work
);
828 f_dev
= &fw_priv
->dev
;
830 device_initialize(f_dev
);
831 dev_set_name(f_dev
, "%s", fw_name
);
832 f_dev
->parent
= device
;
833 f_dev
->class = &firmware_class
;
838 /* load a firmware via user helper */
839 static int _request_firmware_load(struct firmware_priv
*fw_priv
, bool uevent
,
843 struct device
*f_dev
= &fw_priv
->dev
;
844 struct firmware_buf
*buf
= fw_priv
->buf
;
846 /* fall back on userspace loading */
847 buf
->is_paged_buf
= true;
849 dev_set_uevent_suppress(f_dev
, true);
851 /* Need to pin this module until class device is destroyed */
852 __module_get(THIS_MODULE
);
854 retval
= device_add(f_dev
);
856 dev_err(f_dev
, "%s: device_register failed\n", __func__
);
860 retval
= device_create_bin_file(f_dev
, &firmware_attr_data
);
862 dev_err(f_dev
, "%s: sysfs_create_bin_file failed\n", __func__
);
866 retval
= device_create_file(f_dev
, &dev_attr_loading
);
868 dev_err(f_dev
, "%s: device_create_file failed\n", __func__
);
869 goto err_del_bin_attr
;
873 dev_set_uevent_suppress(f_dev
, false);
874 dev_dbg(f_dev
, "firmware: requesting %s\n", buf
->fw_id
);
875 if (timeout
!= MAX_SCHEDULE_TIMEOUT
)
876 schedule_delayed_work(&fw_priv
->timeout_work
, timeout
);
878 kobject_uevent(&fw_priv
->dev
.kobj
, KOBJ_ADD
);
881 wait_for_completion(&buf
->completion
);
883 cancel_delayed_work_sync(&fw_priv
->timeout_work
);
885 device_remove_file(f_dev
, &dev_attr_loading
);
887 device_remove_bin_file(f_dev
, &firmware_attr_data
);
895 static int fw_load_from_user_helper(struct firmware
*firmware
,
896 const char *name
, struct device
*device
,
897 bool uevent
, bool nowait
, long timeout
)
899 struct firmware_priv
*fw_priv
;
901 fw_priv
= fw_create_instance(firmware
, name
, device
, uevent
, nowait
);
903 return PTR_ERR(fw_priv
);
905 fw_priv
->buf
= firmware
->priv
;
906 return _request_firmware_load(fw_priv
, uevent
, timeout
);
908 #else /* CONFIG_FW_LOADER_USER_HELPER */
910 fw_load_from_user_helper(struct firmware
*firmware
, const char *name
,
911 struct device
*device
, bool uevent
, bool nowait
,
917 /* No abort during direct loading */
918 #define is_fw_load_aborted(buf) false
920 #endif /* CONFIG_FW_LOADER_USER_HELPER */
923 /* wait until the shared firmware_buf becomes ready (or error) */
924 static int sync_cached_firmware_buf(struct firmware_buf
*buf
)
928 mutex_lock(&fw_lock
);
929 while (!test_bit(FW_STATUS_DONE
, &buf
->status
)) {
930 if (is_fw_load_aborted(buf
)) {
934 mutex_unlock(&fw_lock
);
935 wait_for_completion(&buf
->completion
);
936 mutex_lock(&fw_lock
);
938 mutex_unlock(&fw_lock
);
942 /* prepare firmware and firmware_buf structs;
943 * return 0 if a firmware is already assigned, 1 if need to load one,
944 * or a negative error code
947 _request_firmware_prepare(struct firmware
**firmware_p
, const char *name
,
948 struct device
*device
)
950 struct firmware
*firmware
;
951 struct firmware_buf
*buf
;
954 *firmware_p
= firmware
= kzalloc(sizeof(*firmware
), GFP_KERNEL
);
956 dev_err(device
, "%s: kmalloc(struct firmware) failed\n",
961 if (fw_get_builtin_firmware(firmware
, name
)) {
962 dev_dbg(device
, "firmware: using built-in firmware %s\n", name
);
963 return 0; /* assigned */
966 ret
= fw_lookup_and_allocate_buf(name
, &fw_cache
, &buf
);
969 * bind with 'buf' now to avoid warning in failure path
970 * of requesting firmware.
972 firmware
->priv
= buf
;
975 ret
= sync_cached_firmware_buf(buf
);
977 fw_set_page_data(buf
, firmware
);
978 return 0; /* assigned */
984 return 1; /* need to load */
987 static int assign_firmware_buf(struct firmware
*fw
, struct device
*device
)
989 struct firmware_buf
*buf
= fw
->priv
;
991 mutex_lock(&fw_lock
);
992 if (!buf
->size
|| is_fw_load_aborted(buf
)) {
993 mutex_unlock(&fw_lock
);
998 * add firmware name into devres list so that we can auto cache
999 * and uncache firmware for device.
1001 * device may has been deleted already, but the problem
1002 * should be fixed in devres or driver core.
1005 fw_add_devm_name(device
, buf
->fw_id
);
1008 * After caching firmware image is started, let it piggyback
1009 * on request firmware.
1011 if (buf
->fwc
->state
== FW_LOADER_START_CACHE
) {
1012 if (fw_cache_piggyback_on_request(buf
->fw_id
))
1013 kref_get(&buf
->ref
);
1016 /* pass the pages buffer to driver at the last minute */
1017 fw_set_page_data(buf
, fw
);
1018 mutex_unlock(&fw_lock
);
1022 /* called from request_firmware() and request_firmware_work_func() */
1024 _request_firmware(const struct firmware
**firmware_p
, const char *name
,
1025 struct device
*device
, bool uevent
, bool nowait
)
1027 struct firmware
*fw
;
1034 if (!name
|| name
[0] == '\0')
1037 ret
= _request_firmware_prepare(&fw
, name
, device
);
1038 if (ret
<= 0) /* error or already assigned */
1042 timeout
= firmware_loading_timeout();
1044 timeout
= usermodehelper_read_lock_wait(timeout
);
1046 dev_dbg(device
, "firmware: %s loading timed out\n",
1052 ret
= usermodehelper_read_trylock();
1054 dev_err(device
, "firmware: %s will not be loaded\n",
1060 if (!fw_get_filesystem_firmware(device
, fw
->priv
))
1061 ret
= fw_load_from_user_helper(fw
, name
, device
,
1062 uevent
, nowait
, timeout
);
1064 ret
= assign_firmware_buf(fw
, device
);
1066 usermodehelper_read_unlock();
1070 release_firmware(fw
);
1079 * request_firmware: - send firmware request and wait for it
1080 * @firmware_p: pointer to firmware image
1081 * @name: name of firmware file
1082 * @device: device for which firmware is being loaded
1084 * @firmware_p will be used to return a firmware image by the name
1085 * of @name for device @device.
1087 * Should be called from user context where sleeping is allowed.
1089 * @name will be used as $FIRMWARE in the uevent environment and
1090 * should be distinctive enough not to be confused with any other
1091 * firmware image for this or any other device.
1093 * Caller must hold the reference count of @device.
1095 * The function can be called safely inside device's suspend and
1099 request_firmware(const struct firmware
**firmware_p
, const char *name
,
1100 struct device
*device
)
1102 return _request_firmware(firmware_p
, name
, device
, true, false);
1106 * release_firmware: - release the resource associated with a firmware image
1107 * @fw: firmware resource to release
1109 void release_firmware(const struct firmware
*fw
)
1112 if (!fw_is_builtin_firmware(fw
))
1113 firmware_free_data(fw
);
1119 struct firmware_work
{
1120 struct work_struct work
;
1121 struct module
*module
;
1123 struct device
*device
;
1125 void (*cont
)(const struct firmware
*fw
, void *context
);
1129 static void request_firmware_work_func(struct work_struct
*work
)
1131 struct firmware_work
*fw_work
;
1132 const struct firmware
*fw
;
1134 fw_work
= container_of(work
, struct firmware_work
, work
);
1136 _request_firmware(&fw
, fw_work
->name
, fw_work
->device
,
1137 fw_work
->uevent
, true);
1138 fw_work
->cont(fw
, fw_work
->context
);
1139 put_device(fw_work
->device
); /* taken in request_firmware_nowait() */
1141 module_put(fw_work
->module
);
1146 * request_firmware_nowait - asynchronous version of request_firmware
1147 * @module: module requesting the firmware
1148 * @uevent: sends uevent to copy the firmware image if this flag
1149 * is non-zero else the firmware copy must be done manually.
1150 * @name: name of firmware file
1151 * @device: device for which firmware is being loaded
1152 * @gfp: allocation flags
1153 * @context: will be passed over to @cont, and
1154 * @fw may be %NULL if firmware request fails.
1155 * @cont: function will be called asynchronously when the firmware
1158 * Caller must hold the reference count of @device.
1160 * Asynchronous variant of request_firmware() for user contexts:
1161 * - sleep for as small periods as possible since it may
1162 * increase kernel boot time of built-in device drivers
1163 * requesting firmware in their ->probe() methods, if
1164 * @gfp is GFP_KERNEL.
1166 * - can't sleep at all if @gfp is GFP_ATOMIC.
1169 request_firmware_nowait(
1170 struct module
*module
, bool uevent
,
1171 const char *name
, struct device
*device
, gfp_t gfp
, void *context
,
1172 void (*cont
)(const struct firmware
*fw
, void *context
))
1174 struct firmware_work
*fw_work
;
1176 fw_work
= kzalloc(sizeof (struct firmware_work
), gfp
);
1180 fw_work
->module
= module
;
1181 fw_work
->name
= name
;
1182 fw_work
->device
= device
;
1183 fw_work
->context
= context
;
1184 fw_work
->cont
= cont
;
1185 fw_work
->uevent
= uevent
;
1187 if (!try_module_get(module
)) {
1192 get_device(fw_work
->device
);
1193 INIT_WORK(&fw_work
->work
, request_firmware_work_func
);
1194 schedule_work(&fw_work
->work
);
1199 * cache_firmware - cache one firmware image in kernel memory space
1200 * @fw_name: the firmware image name
1202 * Cache firmware in kernel memory so that drivers can use it when
1203 * system isn't ready for them to request firmware image from userspace.
1204 * Once it returns successfully, driver can use request_firmware or its
1205 * nowait version to get the cached firmware without any interacting
1208 * Return 0 if the firmware image has been cached successfully
1209 * Return !0 otherwise
1212 int cache_firmware(const char *fw_name
)
1215 const struct firmware
*fw
;
1217 pr_debug("%s: %s\n", __func__
, fw_name
);
1219 ret
= request_firmware(&fw
, fw_name
, NULL
);
1223 pr_debug("%s: %s ret=%d\n", __func__
, fw_name
, ret
);
1229 * uncache_firmware - remove one cached firmware image
1230 * @fw_name: the firmware image name
1232 * Uncache one firmware image which has been cached successfully
1235 * Return 0 if the firmware cache has been removed successfully
1236 * Return !0 otherwise
1239 int uncache_firmware(const char *fw_name
)
1241 struct firmware_buf
*buf
;
1244 pr_debug("%s: %s\n", __func__
, fw_name
);
1246 if (fw_get_builtin_firmware(&fw
, fw_name
))
1249 buf
= fw_lookup_buf(fw_name
);
1258 #ifdef CONFIG_PM_SLEEP
1259 static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain
);
1261 static struct fw_cache_entry
*alloc_fw_cache_entry(const char *name
)
1263 struct fw_cache_entry
*fce
;
1265 fce
= kzalloc(sizeof(*fce
) + strlen(name
) + 1, GFP_ATOMIC
);
1269 strcpy(fce
->name
, name
);
1274 static int __fw_entry_found(const char *name
)
1276 struct firmware_cache
*fwc
= &fw_cache
;
1277 struct fw_cache_entry
*fce
;
1279 list_for_each_entry(fce
, &fwc
->fw_names
, list
) {
1280 if (!strcmp(fce
->name
, name
))
1286 static int fw_cache_piggyback_on_request(const char *name
)
1288 struct firmware_cache
*fwc
= &fw_cache
;
1289 struct fw_cache_entry
*fce
;
1292 spin_lock(&fwc
->name_lock
);
1293 if (__fw_entry_found(name
))
1296 fce
= alloc_fw_cache_entry(name
);
1299 list_add(&fce
->list
, &fwc
->fw_names
);
1300 pr_debug("%s: fw: %s\n", __func__
, name
);
1303 spin_unlock(&fwc
->name_lock
);
1307 static void free_fw_cache_entry(struct fw_cache_entry
*fce
)
1312 static void __async_dev_cache_fw_image(void *fw_entry
,
1313 async_cookie_t cookie
)
1315 struct fw_cache_entry
*fce
= fw_entry
;
1316 struct firmware_cache
*fwc
= &fw_cache
;
1319 ret
= cache_firmware(fce
->name
);
1321 spin_lock(&fwc
->name_lock
);
1322 list_del(&fce
->list
);
1323 spin_unlock(&fwc
->name_lock
);
1325 free_fw_cache_entry(fce
);
1329 /* called with dev->devres_lock held */
1330 static void dev_create_fw_entry(struct device
*dev
, void *res
,
1333 struct fw_name_devm
*fwn
= res
;
1334 const char *fw_name
= fwn
->name
;
1335 struct list_head
*head
= data
;
1336 struct fw_cache_entry
*fce
;
1338 fce
= alloc_fw_cache_entry(fw_name
);
1340 list_add(&fce
->list
, head
);
1343 static int devm_name_match(struct device
*dev
, void *res
,
1346 struct fw_name_devm
*fwn
= res
;
1347 return (fwn
->magic
== (unsigned long)match_data
);
1350 static void dev_cache_fw_image(struct device
*dev
, void *data
)
1353 struct fw_cache_entry
*fce
;
1354 struct fw_cache_entry
*fce_next
;
1355 struct firmware_cache
*fwc
= &fw_cache
;
1357 devres_for_each_res(dev
, fw_name_devm_release
,
1358 devm_name_match
, &fw_cache
,
1359 dev_create_fw_entry
, &todo
);
1361 list_for_each_entry_safe(fce
, fce_next
, &todo
, list
) {
1362 list_del(&fce
->list
);
1364 spin_lock(&fwc
->name_lock
);
1365 /* only one cache entry for one firmware */
1366 if (!__fw_entry_found(fce
->name
)) {
1367 list_add(&fce
->list
, &fwc
->fw_names
);
1369 free_fw_cache_entry(fce
);
1372 spin_unlock(&fwc
->name_lock
);
1375 async_schedule_domain(__async_dev_cache_fw_image
,
1381 static void __device_uncache_fw_images(void)
1383 struct firmware_cache
*fwc
= &fw_cache
;
1384 struct fw_cache_entry
*fce
;
1386 spin_lock(&fwc
->name_lock
);
1387 while (!list_empty(&fwc
->fw_names
)) {
1388 fce
= list_entry(fwc
->fw_names
.next
,
1389 struct fw_cache_entry
, list
);
1390 list_del(&fce
->list
);
1391 spin_unlock(&fwc
->name_lock
);
1393 uncache_firmware(fce
->name
);
1394 free_fw_cache_entry(fce
);
1396 spin_lock(&fwc
->name_lock
);
1398 spin_unlock(&fwc
->name_lock
);
1402 * device_cache_fw_images - cache devices' firmware
1404 * If one device called request_firmware or its nowait version
1405 * successfully before, the firmware names are recored into the
1406 * device's devres link list, so device_cache_fw_images can call
1407 * cache_firmware() to cache these firmwares for the device,
1408 * then the device driver can load its firmwares easily at
1409 * time when system is not ready to complete loading firmware.
1411 static void device_cache_fw_images(void)
1413 struct firmware_cache
*fwc
= &fw_cache
;
1417 pr_debug("%s\n", __func__
);
1419 /* cancel uncache work */
1420 cancel_delayed_work_sync(&fwc
->work
);
1423 * use small loading timeout for caching devices' firmware
1424 * because all these firmware images have been loaded
1425 * successfully at lease once, also system is ready for
1426 * completing firmware loading now. The maximum size of
1427 * firmware in current distributions is about 2M bytes,
1428 * so 10 secs should be enough.
1430 old_timeout
= loading_timeout
;
1431 loading_timeout
= 10;
1433 mutex_lock(&fw_lock
);
1434 fwc
->state
= FW_LOADER_START_CACHE
;
1435 dpm_for_each_dev(NULL
, dev_cache_fw_image
);
1436 mutex_unlock(&fw_lock
);
1438 /* wait for completion of caching firmware for all devices */
1439 async_synchronize_full_domain(&fw_cache_domain
);
1441 loading_timeout
= old_timeout
;
1445 * device_uncache_fw_images - uncache devices' firmware
1447 * uncache all firmwares which have been cached successfully
1448 * by device_uncache_fw_images earlier
1450 static void device_uncache_fw_images(void)
1452 pr_debug("%s\n", __func__
);
1453 __device_uncache_fw_images();
1456 static void device_uncache_fw_images_work(struct work_struct
*work
)
1458 device_uncache_fw_images();
1462 * device_uncache_fw_images_delay - uncache devices firmwares
1463 * @delay: number of milliseconds to delay uncache device firmwares
1465 * uncache all devices's firmwares which has been cached successfully
1466 * by device_cache_fw_images after @delay milliseconds.
1468 static void device_uncache_fw_images_delay(unsigned long delay
)
1470 schedule_delayed_work(&fw_cache
.work
,
1471 msecs_to_jiffies(delay
));
1474 static int fw_pm_notify(struct notifier_block
*notify_block
,
1475 unsigned long mode
, void *unused
)
1478 case PM_HIBERNATION_PREPARE
:
1479 case PM_SUSPEND_PREPARE
:
1480 device_cache_fw_images();
1483 case PM_POST_SUSPEND
:
1484 case PM_POST_HIBERNATION
:
1485 case PM_POST_RESTORE
:
1487 * In case that system sleep failed and syscore_suspend is
1490 mutex_lock(&fw_lock
);
1491 fw_cache
.state
= FW_LOADER_NO_CACHE
;
1492 mutex_unlock(&fw_lock
);
1494 device_uncache_fw_images_delay(10 * MSEC_PER_SEC
);
1501 /* stop caching firmware once syscore_suspend is reached */
1502 static int fw_suspend(void)
1504 fw_cache
.state
= FW_LOADER_NO_CACHE
;
1508 static struct syscore_ops fw_syscore_ops
= {
1509 .suspend
= fw_suspend
,
1512 static int fw_cache_piggyback_on_request(const char *name
)
1518 static void __init
fw_cache_init(void)
1520 spin_lock_init(&fw_cache
.lock
);
1521 INIT_LIST_HEAD(&fw_cache
.head
);
1522 fw_cache
.state
= FW_LOADER_NO_CACHE
;
1524 #ifdef CONFIG_PM_SLEEP
1525 spin_lock_init(&fw_cache
.name_lock
);
1526 INIT_LIST_HEAD(&fw_cache
.fw_names
);
1528 INIT_DELAYED_WORK(&fw_cache
.work
,
1529 device_uncache_fw_images_work
);
1531 fw_cache
.pm_notify
.notifier_call
= fw_pm_notify
;
1532 register_pm_notifier(&fw_cache
.pm_notify
);
1534 register_syscore_ops(&fw_syscore_ops
);
1538 static int __init
firmware_class_init(void)
1541 #ifdef CONFIG_FW_LOADER_USER_HELPER
1542 return class_register(&firmware_class
);
1548 static void __exit
firmware_class_exit(void)
1550 #ifdef CONFIG_PM_SLEEP
1551 unregister_syscore_ops(&fw_syscore_ops
);
1552 unregister_pm_notifier(&fw_cache
.pm_notify
);
1554 #ifdef CONFIG_FW_LOADER_USER_HELPER
1555 class_unregister(&firmware_class
);
1559 fs_initcall(firmware_class_init
);
1560 module_exit(firmware_class_exit
);
1562 EXPORT_SYMBOL(release_firmware
);
1563 EXPORT_SYMBOL(request_firmware
);
1564 EXPORT_SYMBOL(request_firmware_nowait
);
1565 EXPORT_SYMBOL_GPL(cache_firmware
);
1566 EXPORT_SYMBOL_GPL(uncache_firmware
);