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 /* firmware behavior options */
100 #define FW_OPT_UEVENT (1U << 0)
101 #define FW_OPT_NOWAIT (1U << 1)
102 #ifdef CONFIG_FW_LOADER_USER_HELPER
103 #define FW_OPT_FALLBACK (1U << 2)
105 #define FW_OPT_FALLBACK 0
108 struct firmware_cache {
109 /* firmware_buf instance will be added into the below list */
111 struct list_head head;
114 #ifdef CONFIG_PM_SLEEP
116 * Names of firmware images which have been cached successfully
117 * will be added into the below list so that device uncache
118 * helper can trace which firmware images have been cached
121 spinlock_t name_lock;
122 struct list_head fw_names;
124 struct delayed_work work;
126 struct notifier_block pm_notify;
130 struct firmware_buf {
132 struct list_head list;
133 struct completion completion;
134 struct firmware_cache *fwc;
135 unsigned long status;
138 #ifdef CONFIG_FW_LOADER_USER_HELPER
144 struct list_head pending_list;
149 struct fw_cache_entry {
150 struct list_head list;
154 struct fw_name_devm {
159 #define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
161 #define FW_LOADER_NO_CACHE 0
162 #define FW_LOADER_START_CACHE 1
164 static int fw_cache_piggyback_on_request(const char *name);
166 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
167 * guarding for corner cases a global lock should be OK */
168 static DEFINE_MUTEX(fw_lock);
170 static struct firmware_cache fw_cache;
172 static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
173 struct firmware_cache *fwc)
175 struct firmware_buf *buf;
177 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
182 kref_init(&buf->ref);
183 strcpy(buf->fw_id, fw_name);
185 init_completion(&buf->completion);
186 #ifdef CONFIG_FW_LOADER_USER_HELPER
187 INIT_LIST_HEAD(&buf->pending_list);
190 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
195 static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
197 struct firmware_buf *tmp;
198 struct firmware_cache *fwc = &fw_cache;
200 list_for_each_entry(tmp, &fwc->head, list)
201 if (!strcmp(tmp->fw_id, fw_name))
206 static int fw_lookup_and_allocate_buf(const char *fw_name,
207 struct firmware_cache *fwc,
208 struct firmware_buf **buf)
210 struct firmware_buf *tmp;
212 spin_lock(&fwc->lock);
213 tmp = __fw_lookup_buf(fw_name);
216 spin_unlock(&fwc->lock);
220 tmp = __allocate_fw_buf(fw_name, fwc);
222 list_add(&tmp->list, &fwc->head);
223 spin_unlock(&fwc->lock);
227 return tmp ? 0 : -ENOMEM;
230 static void __fw_free_buf(struct kref *ref)
232 struct firmware_buf *buf = to_fwbuf(ref);
233 struct firmware_cache *fwc = buf->fwc;
235 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
236 __func__, buf->fw_id, buf, buf->data,
237 (unsigned int)buf->size);
239 list_del(&buf->list);
240 spin_unlock(&fwc->lock);
242 #ifdef CONFIG_FW_LOADER_USER_HELPER
243 if (buf->is_paged_buf) {
246 for (i = 0; i < buf->nr_pages; i++)
247 __free_page(buf->pages[i]);
255 static void fw_free_buf(struct firmware_buf *buf)
257 struct firmware_cache *fwc = buf->fwc;
258 spin_lock(&fwc->lock);
259 if (!kref_put(&buf->ref, __fw_free_buf))
260 spin_unlock(&fwc->lock);
263 /* direct firmware loading support */
264 static char fw_path_para[256];
265 static const char * const fw_path[] = {
267 "/lib/firmware/updates/" UTS_RELEASE,
268 "/lib/firmware/updates",
269 "/lib/firmware/" UTS_RELEASE,
274 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
275 * from kernel command line because firmware_class is generally built in
276 * kernel instead of module.
278 module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
279 MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
281 /* Don't inline this: 'struct kstat' is biggish */
282 static noinline_for_stack long fw_file_size(struct file *file)
285 if (vfs_getattr(&file->f_path, &st))
287 if (!S_ISREG(st.mode))
289 if (st.size != (long)st.size)
294 static int fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
300 size = fw_file_size(file);
306 rc = kernel_read(file, 0, buf, size);
318 static int fw_get_filesystem_firmware(struct device *device,
319 struct firmware_buf *buf)
323 char *path = __getname();
325 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
328 /* skip the unset customized path */
332 snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id);
334 file = filp_open(path, O_RDONLY, 0);
337 rc = fw_read_file_contents(file, buf);
340 dev_warn(device, "firmware, attempted to load %s, but failed with error %d\n",
348 dev_dbg(device, "firmware: direct-loading firmware %s\n",
350 mutex_lock(&fw_lock);
351 set_bit(FW_STATUS_DONE, &buf->status);
352 complete_all(&buf->completion);
353 mutex_unlock(&fw_lock);
359 /* firmware holds the ownership of pages */
360 static void firmware_free_data(const struct firmware *fw)
362 /* Loaded directly? */
367 fw_free_buf(fw->priv);
370 /* store the pages buffer info firmware from buf */
371 static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
374 #ifdef CONFIG_FW_LOADER_USER_HELPER
375 fw->pages = buf->pages;
377 fw->size = buf->size;
378 fw->data = buf->data;
380 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
381 __func__, buf->fw_id, buf, buf->data,
382 (unsigned int)buf->size);
385 #ifdef CONFIG_PM_SLEEP
386 static void fw_name_devm_release(struct device *dev, void *res)
388 struct fw_name_devm *fwn = res;
390 if (fwn->magic == (unsigned long)&fw_cache)
391 pr_debug("%s: fw_name-%s devm-%p released\n",
392 __func__, fwn->name, res);
395 static int fw_devm_match(struct device *dev, void *res,
398 struct fw_name_devm *fwn = res;
400 return (fwn->magic == (unsigned long)&fw_cache) &&
401 !strcmp(fwn->name, match_data);
404 static struct fw_name_devm *fw_find_devm_name(struct device *dev,
407 struct fw_name_devm *fwn;
409 fwn = devres_find(dev, fw_name_devm_release,
410 fw_devm_match, (void *)name);
414 /* add firmware name into devres list */
415 static int fw_add_devm_name(struct device *dev, const char *name)
417 struct fw_name_devm *fwn;
419 fwn = fw_find_devm_name(dev, name);
423 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
424 strlen(name) + 1, GFP_KERNEL);
428 fwn->magic = (unsigned long)&fw_cache;
429 strcpy(fwn->name, name);
430 devres_add(dev, fwn);
435 static int fw_add_devm_name(struct device *dev, const char *name)
443 * user-mode helper code
445 #ifdef CONFIG_FW_LOADER_USER_HELPER
446 struct firmware_priv {
447 struct delayed_work timeout_work;
450 struct firmware_buf *buf;
454 static struct firmware_priv *to_firmware_priv(struct device *dev)
456 return container_of(dev, struct firmware_priv, dev);
459 static void __fw_load_abort(struct firmware_buf *buf)
462 * There is a small window in which user can write to 'loading'
463 * between loading done and disappearance of 'loading'
465 if (test_bit(FW_STATUS_DONE, &buf->status))
468 list_del_init(&buf->pending_list);
469 set_bit(FW_STATUS_ABORT, &buf->status);
470 complete_all(&buf->completion);
473 static void fw_load_abort(struct firmware_priv *fw_priv)
475 struct firmware_buf *buf = fw_priv->buf;
477 __fw_load_abort(buf);
479 /* avoid user action after loading abort */
483 #define is_fw_load_aborted(buf) \
484 test_bit(FW_STATUS_ABORT, &(buf)->status)
486 static LIST_HEAD(pending_fw_head);
488 /* reboot notifier for avoid deadlock with usermode_lock */
489 static int fw_shutdown_notify(struct notifier_block *unused1,
490 unsigned long unused2, void *unused3)
492 mutex_lock(&fw_lock);
493 while (!list_empty(&pending_fw_head))
494 __fw_load_abort(list_first_entry(&pending_fw_head,
497 mutex_unlock(&fw_lock);
501 static struct notifier_block fw_shutdown_nb = {
502 .notifier_call = fw_shutdown_notify,
505 static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
508 return sprintf(buf, "%d\n", loading_timeout);
512 * firmware_timeout_store - set number of seconds to wait for firmware
513 * @class: device class pointer
514 * @attr: device attribute pointer
515 * @buf: buffer to scan for timeout value
516 * @count: number of bytes in @buf
518 * Sets the number of seconds to wait for the firmware. Once
519 * this expires an error will be returned to the driver and no
520 * firmware will be provided.
522 * Note: zero means 'wait forever'.
524 static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
525 const char *buf, size_t count)
527 loading_timeout = simple_strtol(buf, NULL, 10);
528 if (loading_timeout < 0)
534 static struct class_attribute firmware_class_attrs[] = {
539 static void fw_dev_release(struct device *dev)
541 struct firmware_priv *fw_priv = to_firmware_priv(dev);
546 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
548 struct firmware_priv *fw_priv = to_firmware_priv(dev);
550 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
552 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
554 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
560 static struct class firmware_class = {
562 .class_attrs = firmware_class_attrs,
563 .dev_uevent = firmware_uevent,
564 .dev_release = fw_dev_release,
567 static ssize_t firmware_loading_show(struct device *dev,
568 struct device_attribute *attr, char *buf)
570 struct firmware_priv *fw_priv = to_firmware_priv(dev);
573 mutex_lock(&fw_lock);
575 loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
576 mutex_unlock(&fw_lock);
578 return sprintf(buf, "%d\n", loading);
581 /* Some architectures don't have PAGE_KERNEL_RO */
582 #ifndef PAGE_KERNEL_RO
583 #define PAGE_KERNEL_RO PAGE_KERNEL
586 /* one pages buffer should be mapped/unmapped only once */
587 static int fw_map_pages_buf(struct firmware_buf *buf)
589 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 int loading = simple_strtol(buf, NULL, 10);
622 mutex_lock(&fw_lock);
623 fw_buf = fw_priv->buf;
629 /* discarding any previous partial load */
630 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
631 for (i = 0; i < fw_buf->nr_pages; i++)
632 __free_page(fw_buf->pages[i]);
633 kfree(fw_buf->pages);
634 fw_buf->pages = NULL;
635 fw_buf->page_array_size = 0;
636 fw_buf->nr_pages = 0;
637 set_bit(FW_STATUS_LOADING, &fw_buf->status);
641 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
642 set_bit(FW_STATUS_DONE, &fw_buf->status);
643 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
646 * Several loading requests may be pending on
647 * one same firmware buf, so let all requests
648 * see the mapped 'buf->data' once the loading
651 fw_map_pages_buf(fw_buf);
652 list_del_init(&fw_buf->pending_list);
653 complete_all(&fw_buf->completion);
658 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
661 fw_load_abort(fw_priv);
665 mutex_unlock(&fw_lock);
669 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
671 static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
672 struct bin_attribute *bin_attr,
673 char *buffer, loff_t offset, size_t count)
675 struct device *dev = kobj_to_dev(kobj);
676 struct firmware_priv *fw_priv = to_firmware_priv(dev);
677 struct firmware_buf *buf;
680 mutex_lock(&fw_lock);
682 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
686 if (offset > buf->size) {
690 if (count > buf->size - offset)
691 count = buf->size - offset;
697 int page_nr = offset >> PAGE_SHIFT;
698 int page_ofs = offset & (PAGE_SIZE-1);
699 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
701 page_data = kmap(buf->pages[page_nr]);
703 memcpy(buffer, page_data + page_ofs, page_cnt);
705 kunmap(buf->pages[page_nr]);
711 mutex_unlock(&fw_lock);
715 static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
717 struct firmware_buf *buf = fw_priv->buf;
718 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
720 /* If the array of pages is too small, grow it... */
721 if (buf->page_array_size < pages_needed) {
722 int new_array_size = max(pages_needed,
723 buf->page_array_size * 2);
724 struct page **new_pages;
726 new_pages = kmalloc(new_array_size * sizeof(void *),
729 fw_load_abort(fw_priv);
732 memcpy(new_pages, buf->pages,
733 buf->page_array_size * sizeof(void *));
734 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
735 (new_array_size - buf->page_array_size));
737 buf->pages = new_pages;
738 buf->page_array_size = new_array_size;
741 while (buf->nr_pages < pages_needed) {
742 buf->pages[buf->nr_pages] =
743 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
745 if (!buf->pages[buf->nr_pages]) {
746 fw_load_abort(fw_priv);
755 * firmware_data_write - write method for firmware
756 * @filp: open sysfs file
757 * @kobj: kobject for the device
758 * @bin_attr: bin_attr structure
759 * @buffer: buffer being written
760 * @offset: buffer offset for write in total data store area
761 * @count: buffer size
763 * Data written to the 'data' attribute will be later handed to
764 * the driver as a firmware image.
766 static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
767 struct bin_attribute *bin_attr,
768 char *buffer, loff_t offset, size_t count)
770 struct device *dev = kobj_to_dev(kobj);
771 struct firmware_priv *fw_priv = to_firmware_priv(dev);
772 struct firmware_buf *buf;
775 if (!capable(CAP_SYS_RAWIO))
778 mutex_lock(&fw_lock);
780 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
785 retval = fw_realloc_buffer(fw_priv, offset + count);
793 int page_nr = offset >> PAGE_SHIFT;
794 int page_ofs = offset & (PAGE_SIZE - 1);
795 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
797 page_data = kmap(buf->pages[page_nr]);
799 memcpy(page_data + page_ofs, buffer, page_cnt);
801 kunmap(buf->pages[page_nr]);
807 buf->size = max_t(size_t, offset, buf->size);
809 mutex_unlock(&fw_lock);
813 static struct bin_attribute firmware_attr_data = {
814 .attr = { .name = "data", .mode = 0644 },
816 .read = firmware_data_read,
817 .write = firmware_data_write,
820 static void firmware_class_timeout_work(struct work_struct *work)
822 struct firmware_priv *fw_priv = container_of(work,
823 struct firmware_priv, timeout_work.work);
825 mutex_lock(&fw_lock);
826 fw_load_abort(fw_priv);
827 mutex_unlock(&fw_lock);
830 static struct firmware_priv *
831 fw_create_instance(struct firmware *firmware, const char *fw_name,
832 struct device *device, unsigned int opt_flags)
834 struct firmware_priv *fw_priv;
835 struct device *f_dev;
837 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
839 dev_err(device, "%s: kmalloc failed\n", __func__);
840 fw_priv = ERR_PTR(-ENOMEM);
844 fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT);
845 fw_priv->fw = firmware;
846 INIT_DELAYED_WORK(&fw_priv->timeout_work,
847 firmware_class_timeout_work);
849 f_dev = &fw_priv->dev;
851 device_initialize(f_dev);
852 dev_set_name(f_dev, "%s", fw_name);
853 f_dev->parent = device;
854 f_dev->class = &firmware_class;
859 /* load a firmware via user helper */
860 static int _request_firmware_load(struct firmware_priv *fw_priv,
861 unsigned int opt_flags, long timeout)
864 struct device *f_dev = &fw_priv->dev;
865 struct firmware_buf *buf = fw_priv->buf;
867 /* fall back on userspace loading */
868 buf->is_paged_buf = true;
870 dev_set_uevent_suppress(f_dev, true);
872 retval = device_add(f_dev);
874 dev_err(f_dev, "%s: device_register failed\n", __func__);
878 retval = device_create_bin_file(f_dev, &firmware_attr_data);
880 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
884 mutex_lock(&fw_lock);
885 list_add(&buf->pending_list, &pending_fw_head);
886 mutex_unlock(&fw_lock);
888 retval = device_create_file(f_dev, &dev_attr_loading);
890 mutex_lock(&fw_lock);
891 list_del_init(&buf->pending_list);
892 mutex_unlock(&fw_lock);
893 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
894 goto err_del_bin_attr;
897 if (opt_flags & FW_OPT_UEVENT) {
898 buf->need_uevent = true;
899 dev_set_uevent_suppress(f_dev, false);
900 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
901 if (timeout != MAX_SCHEDULE_TIMEOUT)
902 schedule_delayed_work(&fw_priv->timeout_work, timeout);
904 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
907 wait_for_completion(&buf->completion);
909 cancel_delayed_work_sync(&fw_priv->timeout_work);
911 device_remove_file(f_dev, &dev_attr_loading);
913 device_remove_bin_file(f_dev, &firmware_attr_data);
921 static int fw_load_from_user_helper(struct firmware *firmware,
922 const char *name, struct device *device,
923 unsigned int opt_flags, long timeout)
925 struct firmware_priv *fw_priv;
927 fw_priv = fw_create_instance(firmware, name, device, opt_flags);
929 return PTR_ERR(fw_priv);
931 fw_priv->buf = firmware->priv;
932 return _request_firmware_load(fw_priv, opt_flags, timeout);
935 #ifdef CONFIG_PM_SLEEP
936 /* kill pending requests without uevent to avoid blocking suspend */
937 static void kill_requests_without_uevent(void)
939 struct firmware_buf *buf;
940 struct firmware_buf *next;
942 mutex_lock(&fw_lock);
943 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
944 if (!buf->need_uevent)
945 __fw_load_abort(buf);
947 mutex_unlock(&fw_lock);
951 #else /* CONFIG_FW_LOADER_USER_HELPER */
953 fw_load_from_user_helper(struct firmware *firmware, const char *name,
954 struct device *device, unsigned int opt_flags,
960 /* No abort during direct loading */
961 #define is_fw_load_aborted(buf) false
963 #ifdef CONFIG_PM_SLEEP
964 static inline void kill_requests_without_uevent(void) { }
967 #endif /* CONFIG_FW_LOADER_USER_HELPER */
970 /* wait until the shared firmware_buf becomes ready (or error) */
971 static int sync_cached_firmware_buf(struct firmware_buf *buf)
975 mutex_lock(&fw_lock);
976 while (!test_bit(FW_STATUS_DONE, &buf->status)) {
977 if (is_fw_load_aborted(buf)) {
981 mutex_unlock(&fw_lock);
982 wait_for_completion(&buf->completion);
983 mutex_lock(&fw_lock);
985 mutex_unlock(&fw_lock);
989 /* prepare firmware and firmware_buf structs;
990 * return 0 if a firmware is already assigned, 1 if need to load one,
991 * or a negative error code
994 _request_firmware_prepare(struct firmware **firmware_p, const char *name,
995 struct device *device)
997 struct firmware *firmware;
998 struct firmware_buf *buf;
1001 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
1003 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1008 if (fw_get_builtin_firmware(firmware, name)) {
1009 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
1010 return 0; /* assigned */
1013 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
1016 * bind with 'buf' now to avoid warning in failure path
1017 * of requesting firmware.
1019 firmware->priv = buf;
1022 ret = sync_cached_firmware_buf(buf);
1024 fw_set_page_data(buf, firmware);
1025 return 0; /* assigned */
1031 return 1; /* need to load */
1034 static int assign_firmware_buf(struct firmware *fw, struct device *device,
1035 unsigned int opt_flags)
1037 struct firmware_buf *buf = fw->priv;
1039 mutex_lock(&fw_lock);
1040 if (!buf->size || is_fw_load_aborted(buf)) {
1041 mutex_unlock(&fw_lock);
1046 * add firmware name into devres list so that we can auto cache
1047 * and uncache firmware for device.
1049 * device may has been deleted already, but the problem
1050 * should be fixed in devres or driver core.
1052 /* don't cache firmware handled without uevent */
1053 if (device && (opt_flags & FW_OPT_UEVENT))
1054 fw_add_devm_name(device, buf->fw_id);
1057 * After caching firmware image is started, let it piggyback
1058 * on request firmware.
1060 if (buf->fwc->state == FW_LOADER_START_CACHE) {
1061 if (fw_cache_piggyback_on_request(buf->fw_id))
1062 kref_get(&buf->ref);
1065 /* pass the pages buffer to driver at the last minute */
1066 fw_set_page_data(buf, fw);
1067 mutex_unlock(&fw_lock);
1071 /* called from request_firmware() and request_firmware_work_func() */
1073 _request_firmware(const struct firmware **firmware_p, const char *name,
1074 struct device *device, unsigned int opt_flags)
1076 struct firmware *fw;
1083 ret = _request_firmware_prepare(&fw, name, device);
1084 if (ret <= 0) /* error or already assigned */
1088 timeout = firmware_loading_timeout();
1089 if (opt_flags & FW_OPT_NOWAIT) {
1090 timeout = usermodehelper_read_lock_wait(timeout);
1092 dev_dbg(device, "firmware: %s loading timed out\n",
1098 ret = usermodehelper_read_trylock();
1100 dev_err(device, "firmware: %s will not be loaded\n",
1106 ret = fw_get_filesystem_firmware(device, fw->priv);
1108 if (opt_flags & FW_OPT_FALLBACK) {
1110 "Direct firmware load failed with error %d\n",
1112 dev_warn(device, "Falling back to user helper\n");
1113 ret = fw_load_from_user_helper(fw, name, device,
1114 opt_flags, timeout);
1119 ret = assign_firmware_buf(fw, device, opt_flags);
1121 usermodehelper_read_unlock();
1125 release_firmware(fw);
1134 * request_firmware: - send firmware request and wait for it
1135 * @firmware_p: pointer to firmware image
1136 * @name: name of firmware file
1137 * @device: device for which firmware is being loaded
1139 * @firmware_p will be used to return a firmware image by the name
1140 * of @name for device @device.
1142 * Should be called from user context where sleeping is allowed.
1144 * @name will be used as $FIRMWARE in the uevent environment and
1145 * should be distinctive enough not to be confused with any other
1146 * firmware image for this or any other device.
1148 * Caller must hold the reference count of @device.
1150 * The function can be called safely inside device's suspend and
1154 request_firmware(const struct firmware **firmware_p, const char *name,
1155 struct device *device)
1159 /* Need to pin this module until return */
1160 __module_get(THIS_MODULE);
1161 ret = _request_firmware(firmware_p, name, device,
1162 FW_OPT_UEVENT | FW_OPT_FALLBACK);
1163 module_put(THIS_MODULE);
1166 EXPORT_SYMBOL(request_firmware);
1168 #ifdef CONFIG_FW_LOADER_USER_HELPER
1170 * request_firmware: - load firmware directly without usermode helper
1171 * @firmware_p: pointer to firmware image
1172 * @name: name of firmware file
1173 * @device: device for which firmware is being loaded
1175 * This function works pretty much like request_firmware(), but this doesn't
1176 * fall back to usermode helper even if the firmware couldn't be loaded
1177 * directly from fs. Hence it's useful for loading optional firmwares, which
1178 * aren't always present, without extra long timeouts of udev.
1180 int request_firmware_direct(const struct firmware **firmware_p,
1181 const char *name, struct device *device)
1184 __module_get(THIS_MODULE);
1185 ret = _request_firmware(firmware_p, name, device, FW_OPT_UEVENT);
1186 module_put(THIS_MODULE);
1189 EXPORT_SYMBOL_GPL(request_firmware_direct);
1193 * release_firmware: - release the resource associated with a firmware image
1194 * @fw: firmware resource to release
1196 void release_firmware(const struct firmware *fw)
1199 if (!fw_is_builtin_firmware(fw))
1200 firmware_free_data(fw);
1204 EXPORT_SYMBOL(release_firmware);
1207 struct firmware_work {
1208 struct work_struct work;
1209 struct module *module;
1211 struct device *device;
1213 void (*cont)(const struct firmware *fw, void *context);
1214 unsigned int opt_flags;
1217 static void request_firmware_work_func(struct work_struct *work)
1219 struct firmware_work *fw_work;
1220 const struct firmware *fw;
1222 fw_work = container_of(work, struct firmware_work, work);
1224 _request_firmware(&fw, fw_work->name, fw_work->device,
1225 fw_work->opt_flags);
1226 fw_work->cont(fw, fw_work->context);
1227 put_device(fw_work->device); /* taken in request_firmware_nowait() */
1229 module_put(fw_work->module);
1234 * request_firmware_nowait - asynchronous version of request_firmware
1235 * @module: module requesting the firmware
1236 * @uevent: sends uevent to copy the firmware image if this flag
1237 * is non-zero else the firmware copy must be done manually.
1238 * @name: name of firmware file
1239 * @device: device for which firmware is being loaded
1240 * @gfp: allocation flags
1241 * @context: will be passed over to @cont, and
1242 * @fw may be %NULL if firmware request fails.
1243 * @cont: function will be called asynchronously when the firmware
1246 * Caller must hold the reference count of @device.
1248 * Asynchronous variant of request_firmware() for user contexts:
1249 * - sleep for as small periods as possible since it may
1250 * increase kernel boot time of built-in device drivers
1251 * requesting firmware in their ->probe() methods, if
1252 * @gfp is GFP_KERNEL.
1254 * - can't sleep at all if @gfp is GFP_ATOMIC.
1257 request_firmware_nowait(
1258 struct module *module, bool uevent,
1259 const char *name, struct device *device, gfp_t gfp, void *context,
1260 void (*cont)(const struct firmware *fw, void *context))
1262 struct firmware_work *fw_work;
1264 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
1268 fw_work->module = module;
1269 fw_work->name = name;
1270 fw_work->device = device;
1271 fw_work->context = context;
1272 fw_work->cont = cont;
1273 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
1274 (uevent ? FW_OPT_UEVENT : 0);
1276 if (!try_module_get(module)) {
1281 get_device(fw_work->device);
1282 INIT_WORK(&fw_work->work, request_firmware_work_func);
1283 schedule_work(&fw_work->work);
1286 EXPORT_SYMBOL(request_firmware_nowait);
1288 #ifdef CONFIG_PM_SLEEP
1289 static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1292 * cache_firmware - cache one firmware image in kernel memory space
1293 * @fw_name: the firmware image name
1295 * Cache firmware in kernel memory so that drivers can use it when
1296 * system isn't ready for them to request firmware image from userspace.
1297 * Once it returns successfully, driver can use request_firmware or its
1298 * nowait version to get the cached firmware without any interacting
1301 * Return 0 if the firmware image has been cached successfully
1302 * Return !0 otherwise
1305 static int cache_firmware(const char *fw_name)
1308 const struct firmware *fw;
1310 pr_debug("%s: %s\n", __func__, fw_name);
1312 ret = request_firmware(&fw, fw_name, NULL);
1316 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1321 static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1323 struct firmware_buf *tmp;
1324 struct firmware_cache *fwc = &fw_cache;
1326 spin_lock(&fwc->lock);
1327 tmp = __fw_lookup_buf(fw_name);
1328 spin_unlock(&fwc->lock);
1334 * uncache_firmware - remove one cached firmware image
1335 * @fw_name: the firmware image name
1337 * Uncache one firmware image which has been cached successfully
1340 * Return 0 if the firmware cache has been removed successfully
1341 * Return !0 otherwise
1344 static int uncache_firmware(const char *fw_name)
1346 struct firmware_buf *buf;
1349 pr_debug("%s: %s\n", __func__, fw_name);
1351 if (fw_get_builtin_firmware(&fw, fw_name))
1354 buf = fw_lookup_buf(fw_name);
1363 static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1365 struct fw_cache_entry *fce;
1367 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1371 strcpy(fce->name, name);
1376 static int __fw_entry_found(const char *name)
1378 struct firmware_cache *fwc = &fw_cache;
1379 struct fw_cache_entry *fce;
1381 list_for_each_entry(fce, &fwc->fw_names, list) {
1382 if (!strcmp(fce->name, name))
1388 static int fw_cache_piggyback_on_request(const char *name)
1390 struct firmware_cache *fwc = &fw_cache;
1391 struct fw_cache_entry *fce;
1394 spin_lock(&fwc->name_lock);
1395 if (__fw_entry_found(name))
1398 fce = alloc_fw_cache_entry(name);
1401 list_add(&fce->list, &fwc->fw_names);
1402 pr_debug("%s: fw: %s\n", __func__, name);
1405 spin_unlock(&fwc->name_lock);
1409 static void free_fw_cache_entry(struct fw_cache_entry *fce)
1414 static void __async_dev_cache_fw_image(void *fw_entry,
1415 async_cookie_t cookie)
1417 struct fw_cache_entry *fce = fw_entry;
1418 struct firmware_cache *fwc = &fw_cache;
1421 ret = cache_firmware(fce->name);
1423 spin_lock(&fwc->name_lock);
1424 list_del(&fce->list);
1425 spin_unlock(&fwc->name_lock);
1427 free_fw_cache_entry(fce);
1431 /* called with dev->devres_lock held */
1432 static void dev_create_fw_entry(struct device *dev, void *res,
1435 struct fw_name_devm *fwn = res;
1436 const char *fw_name = fwn->name;
1437 struct list_head *head = data;
1438 struct fw_cache_entry *fce;
1440 fce = alloc_fw_cache_entry(fw_name);
1442 list_add(&fce->list, head);
1445 static int devm_name_match(struct device *dev, void *res,
1448 struct fw_name_devm *fwn = res;
1449 return (fwn->magic == (unsigned long)match_data);
1452 static void dev_cache_fw_image(struct device *dev, void *data)
1455 struct fw_cache_entry *fce;
1456 struct fw_cache_entry *fce_next;
1457 struct firmware_cache *fwc = &fw_cache;
1459 devres_for_each_res(dev, fw_name_devm_release,
1460 devm_name_match, &fw_cache,
1461 dev_create_fw_entry, &todo);
1463 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1464 list_del(&fce->list);
1466 spin_lock(&fwc->name_lock);
1467 /* only one cache entry for one firmware */
1468 if (!__fw_entry_found(fce->name)) {
1469 list_add(&fce->list, &fwc->fw_names);
1471 free_fw_cache_entry(fce);
1474 spin_unlock(&fwc->name_lock);
1477 async_schedule_domain(__async_dev_cache_fw_image,
1483 static void __device_uncache_fw_images(void)
1485 struct firmware_cache *fwc = &fw_cache;
1486 struct fw_cache_entry *fce;
1488 spin_lock(&fwc->name_lock);
1489 while (!list_empty(&fwc->fw_names)) {
1490 fce = list_entry(fwc->fw_names.next,
1491 struct fw_cache_entry, list);
1492 list_del(&fce->list);
1493 spin_unlock(&fwc->name_lock);
1495 uncache_firmware(fce->name);
1496 free_fw_cache_entry(fce);
1498 spin_lock(&fwc->name_lock);
1500 spin_unlock(&fwc->name_lock);
1504 * device_cache_fw_images - cache devices' firmware
1506 * If one device called request_firmware or its nowait version
1507 * successfully before, the firmware names are recored into the
1508 * device's devres link list, so device_cache_fw_images can call
1509 * cache_firmware() to cache these firmwares for the device,
1510 * then the device driver can load its firmwares easily at
1511 * time when system is not ready to complete loading firmware.
1513 static void device_cache_fw_images(void)
1515 struct firmware_cache *fwc = &fw_cache;
1519 pr_debug("%s\n", __func__);
1521 /* cancel uncache work */
1522 cancel_delayed_work_sync(&fwc->work);
1525 * use small loading timeout for caching devices' firmware
1526 * because all these firmware images have been loaded
1527 * successfully at lease once, also system is ready for
1528 * completing firmware loading now. The maximum size of
1529 * firmware in current distributions is about 2M bytes,
1530 * so 10 secs should be enough.
1532 old_timeout = loading_timeout;
1533 loading_timeout = 10;
1535 mutex_lock(&fw_lock);
1536 fwc->state = FW_LOADER_START_CACHE;
1537 dpm_for_each_dev(NULL, dev_cache_fw_image);
1538 mutex_unlock(&fw_lock);
1540 /* wait for completion of caching firmware for all devices */
1541 async_synchronize_full_domain(&fw_cache_domain);
1543 loading_timeout = old_timeout;
1547 * device_uncache_fw_images - uncache devices' firmware
1549 * uncache all firmwares which have been cached successfully
1550 * by device_uncache_fw_images earlier
1552 static void device_uncache_fw_images(void)
1554 pr_debug("%s\n", __func__);
1555 __device_uncache_fw_images();
1558 static void device_uncache_fw_images_work(struct work_struct *work)
1560 device_uncache_fw_images();
1564 * device_uncache_fw_images_delay - uncache devices firmwares
1565 * @delay: number of milliseconds to delay uncache device firmwares
1567 * uncache all devices's firmwares which has been cached successfully
1568 * by device_cache_fw_images after @delay milliseconds.
1570 static void device_uncache_fw_images_delay(unsigned long delay)
1572 schedule_delayed_work(&fw_cache.work,
1573 msecs_to_jiffies(delay));
1576 static int fw_pm_notify(struct notifier_block *notify_block,
1577 unsigned long mode, void *unused)
1580 case PM_HIBERNATION_PREPARE:
1581 case PM_SUSPEND_PREPARE:
1582 kill_requests_without_uevent();
1583 device_cache_fw_images();
1586 case PM_POST_SUSPEND:
1587 case PM_POST_HIBERNATION:
1588 case PM_POST_RESTORE:
1590 * In case that system sleep failed and syscore_suspend is
1593 mutex_lock(&fw_lock);
1594 fw_cache.state = FW_LOADER_NO_CACHE;
1595 mutex_unlock(&fw_lock);
1597 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1604 /* stop caching firmware once syscore_suspend is reached */
1605 static int fw_suspend(void)
1607 fw_cache.state = FW_LOADER_NO_CACHE;
1611 static struct syscore_ops fw_syscore_ops = {
1612 .suspend = fw_suspend,
1615 static int fw_cache_piggyback_on_request(const char *name)
1621 static void __init fw_cache_init(void)
1623 spin_lock_init(&fw_cache.lock);
1624 INIT_LIST_HEAD(&fw_cache.head);
1625 fw_cache.state = FW_LOADER_NO_CACHE;
1627 #ifdef CONFIG_PM_SLEEP
1628 spin_lock_init(&fw_cache.name_lock);
1629 INIT_LIST_HEAD(&fw_cache.fw_names);
1631 INIT_DELAYED_WORK(&fw_cache.work,
1632 device_uncache_fw_images_work);
1634 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1635 register_pm_notifier(&fw_cache.pm_notify);
1637 register_syscore_ops(&fw_syscore_ops);
1641 static int __init firmware_class_init(void)
1644 #ifdef CONFIG_FW_LOADER_USER_HELPER
1645 register_reboot_notifier(&fw_shutdown_nb);
1646 return class_register(&firmware_class);
1652 static void __exit firmware_class_exit(void)
1654 #ifdef CONFIG_PM_SLEEP
1655 unregister_syscore_ops(&fw_syscore_ops);
1656 unregister_pm_notifier(&fw_cache.pm_notify);
1658 #ifdef CONFIG_FW_LOADER_USER_HELPER
1659 unregister_reboot_notifier(&fw_shutdown_nb);
1660 class_unregister(&firmware_class);
1664 fs_initcall(firmware_class_init);
1665 module_exit(firmware_class_exit);