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>
30 #include <linux/reboot.h>
32 #include <generated/utsrelease.h>
36 MODULE_AUTHOR("Manuel Estrada Sainz");
37 MODULE_DESCRIPTION("Multi purpose firmware loading support");
38 MODULE_LICENSE("GPL");
40 /* Builtin firmware support */
42 #ifdef CONFIG_FW_LOADER
44 extern struct builtin_fw __start_builtin_fw
[];
45 extern struct builtin_fw __end_builtin_fw
[];
47 static bool fw_get_builtin_firmware(struct firmware
*fw
, const char *name
)
49 struct builtin_fw
*b_fw
;
51 for (b_fw
= __start_builtin_fw
; b_fw
!= __end_builtin_fw
; b_fw
++) {
52 if (strcmp(name
, b_fw
->name
) == 0) {
53 fw
->size
= b_fw
->size
;
54 fw
->data
= b_fw
->data
;
62 static bool fw_is_builtin_firmware(const struct firmware
*fw
)
64 struct builtin_fw
*b_fw
;
66 for (b_fw
= __start_builtin_fw
; b_fw
!= __end_builtin_fw
; b_fw
++)
67 if (fw
->data
== b_fw
->data
)
73 #else /* Module case - no builtin firmware support */
75 static inline bool fw_get_builtin_firmware(struct firmware
*fw
, const char *name
)
80 static inline bool fw_is_builtin_firmware(const struct firmware
*fw
)
92 static int loading_timeout
= 60; /* In seconds */
94 static inline long firmware_loading_timeout(void)
96 return loading_timeout
> 0 ? loading_timeout
* HZ
: MAX_SCHEDULE_TIMEOUT
;
99 struct firmware_cache
{
100 /* firmware_buf instance will be added into the below list */
102 struct list_head head
;
105 #ifdef CONFIG_PM_SLEEP
107 * Names of firmware images which have been cached successfully
108 * will be added into the below list so that device uncache
109 * helper can trace which firmware images have been cached
112 spinlock_t name_lock
;
113 struct list_head fw_names
;
115 struct delayed_work work
;
117 struct notifier_block pm_notify
;
121 struct firmware_buf
{
123 struct list_head list
;
124 struct completion completion
;
125 struct firmware_cache
*fwc
;
126 unsigned long status
;
129 #ifdef CONFIG_FW_LOADER_USER_HELPER
135 struct list_head pending_list
;
140 struct fw_cache_entry
{
141 struct list_head list
;
145 struct fw_name_devm
{
150 #define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
152 #define FW_LOADER_NO_CACHE 0
153 #define FW_LOADER_START_CACHE 1
155 static int fw_cache_piggyback_on_request(const char *name
);
157 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
158 * guarding for corner cases a global lock should be OK */
159 static DEFINE_MUTEX(fw_lock
);
161 static struct firmware_cache fw_cache
;
163 static struct firmware_buf
*__allocate_fw_buf(const char *fw_name
,
164 struct firmware_cache
*fwc
)
166 struct firmware_buf
*buf
;
168 buf
= kzalloc(sizeof(*buf
) + strlen(fw_name
) + 1 , GFP_ATOMIC
);
173 kref_init(&buf
->ref
);
174 strcpy(buf
->fw_id
, fw_name
);
176 init_completion(&buf
->completion
);
177 #ifdef CONFIG_FW_LOADER_USER_HELPER
178 INIT_LIST_HEAD(&buf
->pending_list
);
181 pr_debug("%s: fw-%s buf=%p\n", __func__
, fw_name
, buf
);
186 static struct firmware_buf
*__fw_lookup_buf(const char *fw_name
)
188 struct firmware_buf
*tmp
;
189 struct firmware_cache
*fwc
= &fw_cache
;
191 list_for_each_entry(tmp
, &fwc
->head
, list
)
192 if (!strcmp(tmp
->fw_id
, fw_name
))
197 static int fw_lookup_and_allocate_buf(const char *fw_name
,
198 struct firmware_cache
*fwc
,
199 struct firmware_buf
**buf
)
201 struct firmware_buf
*tmp
;
203 spin_lock(&fwc
->lock
);
204 tmp
= __fw_lookup_buf(fw_name
);
207 spin_unlock(&fwc
->lock
);
211 tmp
= __allocate_fw_buf(fw_name
, fwc
);
213 list_add(&tmp
->list
, &fwc
->head
);
214 spin_unlock(&fwc
->lock
);
218 return tmp
? 0 : -ENOMEM
;
221 static void __fw_free_buf(struct kref
*ref
)
223 struct firmware_buf
*buf
= to_fwbuf(ref
);
224 struct firmware_cache
*fwc
= buf
->fwc
;
226 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
227 __func__
, buf
->fw_id
, buf
, buf
->data
,
228 (unsigned int)buf
->size
);
230 list_del(&buf
->list
);
231 spin_unlock(&fwc
->lock
);
233 #ifdef CONFIG_FW_LOADER_USER_HELPER
234 if (buf
->is_paged_buf
) {
237 for (i
= 0; i
< buf
->nr_pages
; i
++)
238 __free_page(buf
->pages
[i
]);
246 static void fw_free_buf(struct firmware_buf
*buf
)
248 struct firmware_cache
*fwc
= buf
->fwc
;
249 spin_lock(&fwc
->lock
);
250 if (!kref_put(&buf
->ref
, __fw_free_buf
))
251 spin_unlock(&fwc
->lock
);
254 /* direct firmware loading support */
255 static char fw_path_para
[256];
256 static const char * const fw_path
[] = {
258 "/lib/firmware/updates/" UTS_RELEASE
,
259 "/lib/firmware/updates",
260 "/lib/firmware/" UTS_RELEASE
,
265 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
266 * from kernel command line because firmware_class is generally built in
267 * kernel instead of module.
269 module_param_string(path
, fw_path_para
, sizeof(fw_path_para
), 0644);
270 MODULE_PARM_DESC(path
, "customized firmware image search path with a higher priority than default path");
272 /* Don't inline this: 'struct kstat' is biggish */
273 static noinline_for_stack
long fw_file_size(struct file
*file
)
276 if (vfs_getattr(&file
->f_path
, &st
))
278 if (!S_ISREG(st
.mode
))
280 if (st
.size
!= (long)st
.size
)
285 static int fw_read_file_contents(struct file
*file
, struct firmware_buf
*fw_buf
)
291 size
= fw_file_size(file
);
297 rc
= kernel_read(file
, 0, buf
, size
);
309 static int fw_get_filesystem_firmware(struct device
*device
,
310 struct firmware_buf
*buf
)
314 char *path
= __getname();
316 for (i
= 0; i
< ARRAY_SIZE(fw_path
); i
++) {
319 /* skip the unset customized path */
323 snprintf(path
, PATH_MAX
, "%s/%s", fw_path
[i
], buf
->fw_id
);
325 file
= filp_open(path
, O_RDONLY
, 0);
328 rc
= fw_read_file_contents(file
, buf
);
331 dev_warn(device
, "firmware, attempted to load %s, but failed with error %d\n",
339 dev_dbg(device
, "firmware: direct-loading firmware %s\n",
341 mutex_lock(&fw_lock
);
342 set_bit(FW_STATUS_DONE
, &buf
->status
);
343 complete_all(&buf
->completion
);
344 mutex_unlock(&fw_lock
);
350 /* firmware holds the ownership of pages */
351 static void firmware_free_data(const struct firmware
*fw
)
353 /* Loaded directly? */
358 fw_free_buf(fw
->priv
);
361 /* store the pages buffer info firmware from buf */
362 static void fw_set_page_data(struct firmware_buf
*buf
, struct firmware
*fw
)
365 #ifdef CONFIG_FW_LOADER_USER_HELPER
366 fw
->pages
= buf
->pages
;
368 fw
->size
= buf
->size
;
369 fw
->data
= buf
->data
;
371 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
372 __func__
, buf
->fw_id
, buf
, buf
->data
,
373 (unsigned int)buf
->size
);
376 #ifdef CONFIG_PM_SLEEP
377 static void fw_name_devm_release(struct device
*dev
, void *res
)
379 struct fw_name_devm
*fwn
= res
;
381 if (fwn
->magic
== (unsigned long)&fw_cache
)
382 pr_debug("%s: fw_name-%s devm-%p released\n",
383 __func__
, fwn
->name
, res
);
386 static int fw_devm_match(struct device
*dev
, void *res
,
389 struct fw_name_devm
*fwn
= res
;
391 return (fwn
->magic
== (unsigned long)&fw_cache
) &&
392 !strcmp(fwn
->name
, match_data
);
395 static struct fw_name_devm
*fw_find_devm_name(struct device
*dev
,
398 struct fw_name_devm
*fwn
;
400 fwn
= devres_find(dev
, fw_name_devm_release
,
401 fw_devm_match
, (void *)name
);
405 /* add firmware name into devres list */
406 static int fw_add_devm_name(struct device
*dev
, const char *name
)
408 struct fw_name_devm
*fwn
;
410 fwn
= fw_find_devm_name(dev
, name
);
414 fwn
= devres_alloc(fw_name_devm_release
, sizeof(struct fw_name_devm
) +
415 strlen(name
) + 1, GFP_KERNEL
);
419 fwn
->magic
= (unsigned long)&fw_cache
;
420 strcpy(fwn
->name
, name
);
421 devres_add(dev
, fwn
);
426 static int fw_add_devm_name(struct device
*dev
, const char *name
)
434 * user-mode helper code
436 #ifdef CONFIG_FW_LOADER_USER_HELPER
437 struct firmware_priv
{
438 struct delayed_work timeout_work
;
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 firmware_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
539 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
541 if (add_uevent_var(env
, "FIRMWARE=%s", fw_priv
->buf
->fw_id
))
543 if (add_uevent_var(env
, "TIMEOUT=%i", loading_timeout
))
545 if (add_uevent_var(env
, "ASYNC=%d", fw_priv
->nowait
))
551 static struct class firmware_class
= {
553 .class_attrs
= firmware_class_attrs
,
554 .dev_uevent
= firmware_uevent
,
555 .dev_release
= fw_dev_release
,
558 static ssize_t
firmware_loading_show(struct device
*dev
,
559 struct device_attribute
*attr
, char *buf
)
561 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
564 mutex_lock(&fw_lock
);
566 loading
= test_bit(FW_STATUS_LOADING
, &fw_priv
->buf
->status
);
567 mutex_unlock(&fw_lock
);
569 return sprintf(buf
, "%d\n", loading
);
572 /* Some architectures don't have PAGE_KERNEL_RO */
573 #ifndef PAGE_KERNEL_RO
574 #define PAGE_KERNEL_RO PAGE_KERNEL
577 /* one pages buffer should be mapped/unmapped only once */
578 static int fw_map_pages_buf(struct firmware_buf
*buf
)
580 if (!buf
->is_paged_buf
)
585 buf
->data
= vmap(buf
->pages
, buf
->nr_pages
, 0, PAGE_KERNEL_RO
);
592 * firmware_loading_store - set value in the 'loading' control file
593 * @dev: device pointer
594 * @attr: device attribute pointer
595 * @buf: buffer to scan for loading control value
596 * @count: number of bytes in @buf
598 * The relevant values are:
600 * 1: Start a load, discarding any previous partial load.
601 * 0: Conclude the load and hand the data to the driver code.
602 * -1: Conclude the load with an error and discard any written data.
604 static ssize_t
firmware_loading_store(struct device
*dev
,
605 struct device_attribute
*attr
,
606 const char *buf
, size_t count
)
608 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
609 struct firmware_buf
*fw_buf
;
610 int loading
= simple_strtol(buf
, NULL
, 10);
613 mutex_lock(&fw_lock
);
614 fw_buf
= fw_priv
->buf
;
620 /* discarding any previous partial load */
621 if (!test_bit(FW_STATUS_DONE
, &fw_buf
->status
)) {
622 for (i
= 0; i
< fw_buf
->nr_pages
; i
++)
623 __free_page(fw_buf
->pages
[i
]);
624 kfree(fw_buf
->pages
);
625 fw_buf
->pages
= NULL
;
626 fw_buf
->page_array_size
= 0;
627 fw_buf
->nr_pages
= 0;
628 set_bit(FW_STATUS_LOADING
, &fw_buf
->status
);
632 if (test_bit(FW_STATUS_LOADING
, &fw_buf
->status
)) {
633 set_bit(FW_STATUS_DONE
, &fw_buf
->status
);
634 clear_bit(FW_STATUS_LOADING
, &fw_buf
->status
);
637 * Several loading requests may be pending on
638 * one same firmware buf, so let all requests
639 * see the mapped 'buf->data' once the loading
642 fw_map_pages_buf(fw_buf
);
643 list_del_init(&fw_buf
->pending_list
);
644 complete_all(&fw_buf
->completion
);
649 dev_err(dev
, "%s: unexpected value (%d)\n", __func__
, loading
);
652 fw_load_abort(fw_priv
);
656 mutex_unlock(&fw_lock
);
660 static DEVICE_ATTR(loading
, 0644, firmware_loading_show
, firmware_loading_store
);
662 static ssize_t
firmware_data_read(struct file
*filp
, struct kobject
*kobj
,
663 struct bin_attribute
*bin_attr
,
664 char *buffer
, loff_t offset
, size_t count
)
666 struct device
*dev
= kobj_to_dev(kobj
);
667 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
668 struct firmware_buf
*buf
;
671 mutex_lock(&fw_lock
);
673 if (!buf
|| test_bit(FW_STATUS_DONE
, &buf
->status
)) {
677 if (offset
> buf
->size
) {
681 if (count
> buf
->size
- offset
)
682 count
= buf
->size
- offset
;
688 int page_nr
= offset
>> PAGE_SHIFT
;
689 int page_ofs
= offset
& (PAGE_SIZE
-1);
690 int page_cnt
= min_t(size_t, PAGE_SIZE
- page_ofs
, count
);
692 page_data
= kmap(buf
->pages
[page_nr
]);
694 memcpy(buffer
, page_data
+ page_ofs
, page_cnt
);
696 kunmap(buf
->pages
[page_nr
]);
702 mutex_unlock(&fw_lock
);
706 static int fw_realloc_buffer(struct firmware_priv
*fw_priv
, int min_size
)
708 struct firmware_buf
*buf
= fw_priv
->buf
;
709 int pages_needed
= ALIGN(min_size
, PAGE_SIZE
) >> PAGE_SHIFT
;
711 /* If the array of pages is too small, grow it... */
712 if (buf
->page_array_size
< pages_needed
) {
713 int new_array_size
= max(pages_needed
,
714 buf
->page_array_size
* 2);
715 struct page
**new_pages
;
717 new_pages
= kmalloc(new_array_size
* sizeof(void *),
720 fw_load_abort(fw_priv
);
723 memcpy(new_pages
, buf
->pages
,
724 buf
->page_array_size
* sizeof(void *));
725 memset(&new_pages
[buf
->page_array_size
], 0, sizeof(void *) *
726 (new_array_size
- buf
->page_array_size
));
728 buf
->pages
= new_pages
;
729 buf
->page_array_size
= new_array_size
;
732 while (buf
->nr_pages
< pages_needed
) {
733 buf
->pages
[buf
->nr_pages
] =
734 alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
736 if (!buf
->pages
[buf
->nr_pages
]) {
737 fw_load_abort(fw_priv
);
746 * firmware_data_write - write method for firmware
747 * @filp: open sysfs file
748 * @kobj: kobject for the device
749 * @bin_attr: bin_attr structure
750 * @buffer: buffer being written
751 * @offset: buffer offset for write in total data store area
752 * @count: buffer size
754 * Data written to the 'data' attribute will be later handed to
755 * the driver as a firmware image.
757 static ssize_t
firmware_data_write(struct file
*filp
, struct kobject
*kobj
,
758 struct bin_attribute
*bin_attr
,
759 char *buffer
, loff_t offset
, size_t count
)
761 struct device
*dev
= kobj_to_dev(kobj
);
762 struct firmware_priv
*fw_priv
= to_firmware_priv(dev
);
763 struct firmware_buf
*buf
;
766 if (!capable(CAP_SYS_RAWIO
))
769 mutex_lock(&fw_lock
);
771 if (!buf
|| test_bit(FW_STATUS_DONE
, &buf
->status
)) {
776 retval
= fw_realloc_buffer(fw_priv
, offset
+ count
);
784 int page_nr
= offset
>> PAGE_SHIFT
;
785 int page_ofs
= offset
& (PAGE_SIZE
- 1);
786 int page_cnt
= min_t(size_t, PAGE_SIZE
- page_ofs
, count
);
788 page_data
= kmap(buf
->pages
[page_nr
]);
790 memcpy(page_data
+ page_ofs
, buffer
, page_cnt
);
792 kunmap(buf
->pages
[page_nr
]);
798 buf
->size
= max_t(size_t, offset
, buf
->size
);
800 mutex_unlock(&fw_lock
);
804 static struct bin_attribute firmware_attr_data
= {
805 .attr
= { .name
= "data", .mode
= 0644 },
807 .read
= firmware_data_read
,
808 .write
= firmware_data_write
,
811 static void firmware_class_timeout_work(struct work_struct
*work
)
813 struct firmware_priv
*fw_priv
= container_of(work
,
814 struct firmware_priv
, timeout_work
.work
);
816 mutex_lock(&fw_lock
);
817 fw_load_abort(fw_priv
);
818 mutex_unlock(&fw_lock
);
821 static struct firmware_priv
*
822 fw_create_instance(struct firmware
*firmware
, const char *fw_name
,
823 struct device
*device
, bool uevent
, bool nowait
)
825 struct firmware_priv
*fw_priv
;
826 struct device
*f_dev
;
828 fw_priv
= kzalloc(sizeof(*fw_priv
), GFP_KERNEL
);
830 dev_err(device
, "%s: kmalloc failed\n", __func__
);
831 fw_priv
= ERR_PTR(-ENOMEM
);
835 fw_priv
->nowait
= nowait
;
836 fw_priv
->fw
= firmware
;
837 INIT_DELAYED_WORK(&fw_priv
->timeout_work
,
838 firmware_class_timeout_work
);
840 f_dev
= &fw_priv
->dev
;
842 device_initialize(f_dev
);
843 dev_set_name(f_dev
, "%s", fw_name
);
844 f_dev
->parent
= device
;
845 f_dev
->class = &firmware_class
;
850 /* load a firmware via user helper */
851 static int _request_firmware_load(struct firmware_priv
*fw_priv
, bool uevent
,
855 struct device
*f_dev
= &fw_priv
->dev
;
856 struct firmware_buf
*buf
= fw_priv
->buf
;
858 /* fall back on userspace loading */
859 buf
->is_paged_buf
= true;
861 dev_set_uevent_suppress(f_dev
, true);
863 retval
= device_add(f_dev
);
865 dev_err(f_dev
, "%s: device_register failed\n", __func__
);
869 retval
= device_create_bin_file(f_dev
, &firmware_attr_data
);
871 dev_err(f_dev
, "%s: sysfs_create_bin_file failed\n", __func__
);
875 mutex_lock(&fw_lock
);
876 list_add(&buf
->pending_list
, &pending_fw_head
);
877 mutex_unlock(&fw_lock
);
879 retval
= device_create_file(f_dev
, &dev_attr_loading
);
881 mutex_lock(&fw_lock
);
882 list_del_init(&buf
->pending_list
);
883 mutex_unlock(&fw_lock
);
884 dev_err(f_dev
, "%s: device_create_file failed\n", __func__
);
885 goto err_del_bin_attr
;
889 buf
->need_uevent
= true;
890 dev_set_uevent_suppress(f_dev
, false);
891 dev_dbg(f_dev
, "firmware: requesting %s\n", buf
->fw_id
);
892 if (timeout
!= MAX_SCHEDULE_TIMEOUT
)
893 schedule_delayed_work(&fw_priv
->timeout_work
, timeout
);
895 kobject_uevent(&fw_priv
->dev
.kobj
, KOBJ_ADD
);
898 wait_for_completion(&buf
->completion
);
900 cancel_delayed_work_sync(&fw_priv
->timeout_work
);
902 device_remove_file(f_dev
, &dev_attr_loading
);
904 device_remove_bin_file(f_dev
, &firmware_attr_data
);
912 static int fw_load_from_user_helper(struct firmware
*firmware
,
913 const char *name
, struct device
*device
,
914 bool uevent
, bool nowait
, long timeout
)
916 struct firmware_priv
*fw_priv
;
918 fw_priv
= fw_create_instance(firmware
, name
, device
, uevent
, nowait
);
920 return PTR_ERR(fw_priv
);
922 fw_priv
->buf
= firmware
->priv
;
923 return _request_firmware_load(fw_priv
, uevent
, timeout
);
926 #ifdef CONFIG_PM_SLEEP
927 /* kill pending requests without uevent to avoid blocking suspend */
928 static void kill_requests_without_uevent(void)
930 struct firmware_buf
*buf
;
931 struct firmware_buf
*next
;
933 mutex_lock(&fw_lock
);
934 list_for_each_entry_safe(buf
, next
, &pending_fw_head
, pending_list
) {
935 if (!buf
->need_uevent
)
936 __fw_load_abort(buf
);
938 mutex_unlock(&fw_lock
);
942 #else /* CONFIG_FW_LOADER_USER_HELPER */
944 fw_load_from_user_helper(struct firmware
*firmware
, const char *name
,
945 struct device
*device
, bool uevent
, bool nowait
,
951 /* No abort during direct loading */
952 #define is_fw_load_aborted(buf) false
954 #ifdef CONFIG_PM_SLEEP
955 static inline void kill_requests_without_uevent(void) { }
958 #endif /* CONFIG_FW_LOADER_USER_HELPER */
961 /* wait until the shared firmware_buf becomes ready (or error) */
962 static int sync_cached_firmware_buf(struct firmware_buf
*buf
)
966 mutex_lock(&fw_lock
);
967 while (!test_bit(FW_STATUS_DONE
, &buf
->status
)) {
968 if (is_fw_load_aborted(buf
)) {
972 mutex_unlock(&fw_lock
);
973 wait_for_completion(&buf
->completion
);
974 mutex_lock(&fw_lock
);
976 mutex_unlock(&fw_lock
);
980 /* prepare firmware and firmware_buf structs;
981 * return 0 if a firmware is already assigned, 1 if need to load one,
982 * or a negative error code
985 _request_firmware_prepare(struct firmware
**firmware_p
, const char *name
,
986 struct device
*device
)
988 struct firmware
*firmware
;
989 struct firmware_buf
*buf
;
992 *firmware_p
= firmware
= kzalloc(sizeof(*firmware
), GFP_KERNEL
);
994 dev_err(device
, "%s: kmalloc(struct firmware) failed\n",
999 if (fw_get_builtin_firmware(firmware
, name
)) {
1000 dev_dbg(device
, "firmware: using built-in firmware %s\n", name
);
1001 return 0; /* assigned */
1004 ret
= fw_lookup_and_allocate_buf(name
, &fw_cache
, &buf
);
1007 * bind with 'buf' now to avoid warning in failure path
1008 * of requesting firmware.
1010 firmware
->priv
= buf
;
1013 ret
= sync_cached_firmware_buf(buf
);
1015 fw_set_page_data(buf
, firmware
);
1016 return 0; /* assigned */
1022 return 1; /* need to load */
1025 static int assign_firmware_buf(struct firmware
*fw
, struct device
*device
,
1028 struct firmware_buf
*buf
= fw
->priv
;
1030 mutex_lock(&fw_lock
);
1031 if (!buf
->size
|| is_fw_load_aborted(buf
)) {
1032 mutex_unlock(&fw_lock
);
1037 * add firmware name into devres list so that we can auto cache
1038 * and uncache firmware for device.
1040 * device may has been deleted already, but the problem
1041 * should be fixed in devres or driver core.
1043 if (device
&& !skip_cache
)
1044 fw_add_devm_name(device
, buf
->fw_id
);
1047 * After caching firmware image is started, let it piggyback
1048 * on request firmware.
1050 if (buf
->fwc
->state
== FW_LOADER_START_CACHE
) {
1051 if (fw_cache_piggyback_on_request(buf
->fw_id
))
1052 kref_get(&buf
->ref
);
1055 /* pass the pages buffer to driver at the last minute */
1056 fw_set_page_data(buf
, fw
);
1057 mutex_unlock(&fw_lock
);
1061 /* called from request_firmware() and request_firmware_work_func() */
1063 _request_firmware(const struct firmware
**firmware_p
, const char *name
,
1064 struct device
*device
, bool uevent
, bool nowait
)
1066 struct firmware
*fw
;
1073 ret
= _request_firmware_prepare(&fw
, name
, device
);
1074 if (ret
<= 0) /* error or already assigned */
1078 timeout
= firmware_loading_timeout();
1080 timeout
= usermodehelper_read_lock_wait(timeout
);
1082 dev_dbg(device
, "firmware: %s loading timed out\n",
1088 ret
= usermodehelper_read_trylock();
1090 dev_err(device
, "firmware: %s will not be loaded\n",
1096 ret
= fw_get_filesystem_firmware(device
, fw
->priv
);
1098 dev_warn(device
, "Direct firmware load failed with error %d\n",
1100 dev_warn(device
, "Falling back to user helper\n");
1101 ret
= fw_load_from_user_helper(fw
, name
, device
,
1102 uevent
, nowait
, timeout
);
1105 /* don't cache firmware handled without uevent */
1107 ret
= assign_firmware_buf(fw
, device
, !uevent
);
1109 usermodehelper_read_unlock();
1113 release_firmware(fw
);
1122 * request_firmware: - send firmware request and wait for it
1123 * @firmware_p: pointer to firmware image
1124 * @name: name of firmware file
1125 * @device: device for which firmware is being loaded
1127 * @firmware_p will be used to return a firmware image by the name
1128 * of @name for device @device.
1130 * Should be called from user context where sleeping is allowed.
1132 * @name will be used as $FIRMWARE in the uevent environment and
1133 * should be distinctive enough not to be confused with any other
1134 * firmware image for this or any other device.
1136 * Caller must hold the reference count of @device.
1138 * The function can be called safely inside device's suspend and
1142 request_firmware(const struct firmware
**firmware_p
, const char *name
,
1143 struct device
*device
)
1147 /* Need to pin this module until return */
1148 __module_get(THIS_MODULE
);
1149 ret
= _request_firmware(firmware_p
, name
, device
, true, false);
1150 module_put(THIS_MODULE
);
1153 EXPORT_SYMBOL(request_firmware
);
1156 * release_firmware: - release the resource associated with a firmware image
1157 * @fw: firmware resource to release
1159 void release_firmware(const struct firmware
*fw
)
1162 if (!fw_is_builtin_firmware(fw
))
1163 firmware_free_data(fw
);
1167 EXPORT_SYMBOL(release_firmware
);
1170 struct firmware_work
{
1171 struct work_struct work
;
1172 struct module
*module
;
1174 struct device
*device
;
1176 void (*cont
)(const struct firmware
*fw
, void *context
);
1180 static void request_firmware_work_func(struct work_struct
*work
)
1182 struct firmware_work
*fw_work
;
1183 const struct firmware
*fw
;
1185 fw_work
= container_of(work
, struct firmware_work
, work
);
1187 _request_firmware(&fw
, fw_work
->name
, fw_work
->device
,
1188 fw_work
->uevent
, true);
1189 fw_work
->cont(fw
, fw_work
->context
);
1190 put_device(fw_work
->device
); /* taken in request_firmware_nowait() */
1192 module_put(fw_work
->module
);
1197 * request_firmware_nowait - asynchronous version of request_firmware
1198 * @module: module requesting the firmware
1199 * @uevent: sends uevent to copy the firmware image if this flag
1200 * is non-zero else the firmware copy must be done manually.
1201 * @name: name of firmware file
1202 * @device: device for which firmware is being loaded
1203 * @gfp: allocation flags
1204 * @context: will be passed over to @cont, and
1205 * @fw may be %NULL if firmware request fails.
1206 * @cont: function will be called asynchronously when the firmware
1209 * Caller must hold the reference count of @device.
1211 * Asynchronous variant of request_firmware() for user contexts:
1212 * - sleep for as small periods as possible since it may
1213 * increase kernel boot time of built-in device drivers
1214 * requesting firmware in their ->probe() methods, if
1215 * @gfp is GFP_KERNEL.
1217 * - can't sleep at all if @gfp is GFP_ATOMIC.
1220 request_firmware_nowait(
1221 struct module
*module
, bool uevent
,
1222 const char *name
, struct device
*device
, gfp_t gfp
, void *context
,
1223 void (*cont
)(const struct firmware
*fw
, void *context
))
1225 struct firmware_work
*fw_work
;
1227 fw_work
= kzalloc(sizeof (struct firmware_work
), gfp
);
1231 fw_work
->module
= module
;
1232 fw_work
->name
= name
;
1233 fw_work
->device
= device
;
1234 fw_work
->context
= context
;
1235 fw_work
->cont
= cont
;
1236 fw_work
->uevent
= uevent
;
1238 if (!try_module_get(module
)) {
1243 get_device(fw_work
->device
);
1244 INIT_WORK(&fw_work
->work
, request_firmware_work_func
);
1245 schedule_work(&fw_work
->work
);
1248 EXPORT_SYMBOL(request_firmware_nowait
);
1250 #ifdef CONFIG_PM_SLEEP
1251 static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain
);
1254 * cache_firmware - cache one firmware image in kernel memory space
1255 * @fw_name: the firmware image name
1257 * Cache firmware in kernel memory so that drivers can use it when
1258 * system isn't ready for them to request firmware image from userspace.
1259 * Once it returns successfully, driver can use request_firmware or its
1260 * nowait version to get the cached firmware without any interacting
1263 * Return 0 if the firmware image has been cached successfully
1264 * Return !0 otherwise
1267 static int cache_firmware(const char *fw_name
)
1270 const struct firmware
*fw
;
1272 pr_debug("%s: %s\n", __func__
, fw_name
);
1274 ret
= request_firmware(&fw
, fw_name
, NULL
);
1278 pr_debug("%s: %s ret=%d\n", __func__
, fw_name
, ret
);
1283 static struct firmware_buf
*fw_lookup_buf(const char *fw_name
)
1285 struct firmware_buf
*tmp
;
1286 struct firmware_cache
*fwc
= &fw_cache
;
1288 spin_lock(&fwc
->lock
);
1289 tmp
= __fw_lookup_buf(fw_name
);
1290 spin_unlock(&fwc
->lock
);
1296 * uncache_firmware - remove one cached firmware image
1297 * @fw_name: the firmware image name
1299 * Uncache one firmware image which has been cached successfully
1302 * Return 0 if the firmware cache has been removed successfully
1303 * Return !0 otherwise
1306 static int uncache_firmware(const char *fw_name
)
1308 struct firmware_buf
*buf
;
1311 pr_debug("%s: %s\n", __func__
, fw_name
);
1313 if (fw_get_builtin_firmware(&fw
, fw_name
))
1316 buf
= fw_lookup_buf(fw_name
);
1325 static struct fw_cache_entry
*alloc_fw_cache_entry(const char *name
)
1327 struct fw_cache_entry
*fce
;
1329 fce
= kzalloc(sizeof(*fce
) + strlen(name
) + 1, GFP_ATOMIC
);
1333 strcpy(fce
->name
, name
);
1338 static int __fw_entry_found(const char *name
)
1340 struct firmware_cache
*fwc
= &fw_cache
;
1341 struct fw_cache_entry
*fce
;
1343 list_for_each_entry(fce
, &fwc
->fw_names
, list
) {
1344 if (!strcmp(fce
->name
, name
))
1350 static int fw_cache_piggyback_on_request(const char *name
)
1352 struct firmware_cache
*fwc
= &fw_cache
;
1353 struct fw_cache_entry
*fce
;
1356 spin_lock(&fwc
->name_lock
);
1357 if (__fw_entry_found(name
))
1360 fce
= alloc_fw_cache_entry(name
);
1363 list_add(&fce
->list
, &fwc
->fw_names
);
1364 pr_debug("%s: fw: %s\n", __func__
, name
);
1367 spin_unlock(&fwc
->name_lock
);
1371 static void free_fw_cache_entry(struct fw_cache_entry
*fce
)
1376 static void __async_dev_cache_fw_image(void *fw_entry
,
1377 async_cookie_t cookie
)
1379 struct fw_cache_entry
*fce
= fw_entry
;
1380 struct firmware_cache
*fwc
= &fw_cache
;
1383 ret
= cache_firmware(fce
->name
);
1385 spin_lock(&fwc
->name_lock
);
1386 list_del(&fce
->list
);
1387 spin_unlock(&fwc
->name_lock
);
1389 free_fw_cache_entry(fce
);
1393 /* called with dev->devres_lock held */
1394 static void dev_create_fw_entry(struct device
*dev
, void *res
,
1397 struct fw_name_devm
*fwn
= res
;
1398 const char *fw_name
= fwn
->name
;
1399 struct list_head
*head
= data
;
1400 struct fw_cache_entry
*fce
;
1402 fce
= alloc_fw_cache_entry(fw_name
);
1404 list_add(&fce
->list
, head
);
1407 static int devm_name_match(struct device
*dev
, void *res
,
1410 struct fw_name_devm
*fwn
= res
;
1411 return (fwn
->magic
== (unsigned long)match_data
);
1414 static void dev_cache_fw_image(struct device
*dev
, void *data
)
1417 struct fw_cache_entry
*fce
;
1418 struct fw_cache_entry
*fce_next
;
1419 struct firmware_cache
*fwc
= &fw_cache
;
1421 devres_for_each_res(dev
, fw_name_devm_release
,
1422 devm_name_match
, &fw_cache
,
1423 dev_create_fw_entry
, &todo
);
1425 list_for_each_entry_safe(fce
, fce_next
, &todo
, list
) {
1426 list_del(&fce
->list
);
1428 spin_lock(&fwc
->name_lock
);
1429 /* only one cache entry for one firmware */
1430 if (!__fw_entry_found(fce
->name
)) {
1431 list_add(&fce
->list
, &fwc
->fw_names
);
1433 free_fw_cache_entry(fce
);
1436 spin_unlock(&fwc
->name_lock
);
1439 async_schedule_domain(__async_dev_cache_fw_image
,
1445 static void __device_uncache_fw_images(void)
1447 struct firmware_cache
*fwc
= &fw_cache
;
1448 struct fw_cache_entry
*fce
;
1450 spin_lock(&fwc
->name_lock
);
1451 while (!list_empty(&fwc
->fw_names
)) {
1452 fce
= list_entry(fwc
->fw_names
.next
,
1453 struct fw_cache_entry
, list
);
1454 list_del(&fce
->list
);
1455 spin_unlock(&fwc
->name_lock
);
1457 uncache_firmware(fce
->name
);
1458 free_fw_cache_entry(fce
);
1460 spin_lock(&fwc
->name_lock
);
1462 spin_unlock(&fwc
->name_lock
);
1466 * device_cache_fw_images - cache devices' firmware
1468 * If one device called request_firmware or its nowait version
1469 * successfully before, the firmware names are recored into the
1470 * device's devres link list, so device_cache_fw_images can call
1471 * cache_firmware() to cache these firmwares for the device,
1472 * then the device driver can load its firmwares easily at
1473 * time when system is not ready to complete loading firmware.
1475 static void device_cache_fw_images(void)
1477 struct firmware_cache
*fwc
= &fw_cache
;
1481 pr_debug("%s\n", __func__
);
1483 /* cancel uncache work */
1484 cancel_delayed_work_sync(&fwc
->work
);
1487 * use small loading timeout for caching devices' firmware
1488 * because all these firmware images have been loaded
1489 * successfully at lease once, also system is ready for
1490 * completing firmware loading now. The maximum size of
1491 * firmware in current distributions is about 2M bytes,
1492 * so 10 secs should be enough.
1494 old_timeout
= loading_timeout
;
1495 loading_timeout
= 10;
1497 mutex_lock(&fw_lock
);
1498 fwc
->state
= FW_LOADER_START_CACHE
;
1499 dpm_for_each_dev(NULL
, dev_cache_fw_image
);
1500 mutex_unlock(&fw_lock
);
1502 /* wait for completion of caching firmware for all devices */
1503 async_synchronize_full_domain(&fw_cache_domain
);
1505 loading_timeout
= old_timeout
;
1509 * device_uncache_fw_images - uncache devices' firmware
1511 * uncache all firmwares which have been cached successfully
1512 * by device_uncache_fw_images earlier
1514 static void device_uncache_fw_images(void)
1516 pr_debug("%s\n", __func__
);
1517 __device_uncache_fw_images();
1520 static void device_uncache_fw_images_work(struct work_struct
*work
)
1522 device_uncache_fw_images();
1526 * device_uncache_fw_images_delay - uncache devices firmwares
1527 * @delay: number of milliseconds to delay uncache device firmwares
1529 * uncache all devices's firmwares which has been cached successfully
1530 * by device_cache_fw_images after @delay milliseconds.
1532 static void device_uncache_fw_images_delay(unsigned long delay
)
1534 schedule_delayed_work(&fw_cache
.work
,
1535 msecs_to_jiffies(delay
));
1538 static int fw_pm_notify(struct notifier_block
*notify_block
,
1539 unsigned long mode
, void *unused
)
1542 case PM_HIBERNATION_PREPARE
:
1543 case PM_SUSPEND_PREPARE
:
1544 kill_requests_without_uevent();
1545 device_cache_fw_images();
1548 case PM_POST_SUSPEND
:
1549 case PM_POST_HIBERNATION
:
1550 case PM_POST_RESTORE
:
1552 * In case that system sleep failed and syscore_suspend is
1555 mutex_lock(&fw_lock
);
1556 fw_cache
.state
= FW_LOADER_NO_CACHE
;
1557 mutex_unlock(&fw_lock
);
1559 device_uncache_fw_images_delay(10 * MSEC_PER_SEC
);
1566 /* stop caching firmware once syscore_suspend is reached */
1567 static int fw_suspend(void)
1569 fw_cache
.state
= FW_LOADER_NO_CACHE
;
1573 static struct syscore_ops fw_syscore_ops
= {
1574 .suspend
= fw_suspend
,
1577 static int fw_cache_piggyback_on_request(const char *name
)
1583 static void __init
fw_cache_init(void)
1585 spin_lock_init(&fw_cache
.lock
);
1586 INIT_LIST_HEAD(&fw_cache
.head
);
1587 fw_cache
.state
= FW_LOADER_NO_CACHE
;
1589 #ifdef CONFIG_PM_SLEEP
1590 spin_lock_init(&fw_cache
.name_lock
);
1591 INIT_LIST_HEAD(&fw_cache
.fw_names
);
1593 INIT_DELAYED_WORK(&fw_cache
.work
,
1594 device_uncache_fw_images_work
);
1596 fw_cache
.pm_notify
.notifier_call
= fw_pm_notify
;
1597 register_pm_notifier(&fw_cache
.pm_notify
);
1599 register_syscore_ops(&fw_syscore_ops
);
1603 static int __init
firmware_class_init(void)
1606 #ifdef CONFIG_FW_LOADER_USER_HELPER
1607 register_reboot_notifier(&fw_shutdown_nb
);
1608 return class_register(&firmware_class
);
1614 static void __exit
firmware_class_exit(void)
1616 #ifdef CONFIG_PM_SLEEP
1617 unregister_syscore_ops(&fw_syscore_ops
);
1618 unregister_pm_notifier(&fw_cache
.pm_notify
);
1620 #ifdef CONFIG_FW_LOADER_USER_HELPER
1621 unregister_reboot_notifier(&fw_shutdown_nb
);
1622 class_unregister(&firmware_class
);
1626 fs_initcall(firmware_class_init
);
1627 module_exit(firmware_class_exit
);