2 * firmware_class.c - Multi purpose firmware loading support
4 * Copyright (c) 2003 Manuel Estrada Sainz
6 * Please see Documentation/firmware_class/ for more information.
10 #include <linux/capability.h>
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/timer.h>
15 #include <linux/vmalloc.h>
16 #include <linux/interrupt.h>
17 #include <linux/bitops.h>
18 #include <linux/mutex.h>
19 #include <linux/workqueue.h>
20 #include <linux/highmem.h>
21 #include <linux/firmware.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <linux/file.h>
25 #include <linux/list.h>
27 #include <linux/async.h>
29 #include <linux/suspend.h>
30 #include <linux/syscore_ops.h>
31 #include <linux/reboot.h>
32 #include <linux/security.h>
34 #include <generated/utsrelease.h>
38 MODULE_AUTHOR("Manuel Estrada Sainz");
39 MODULE_DESCRIPTION("Multi purpose firmware loading support");
40 MODULE_LICENSE("GPL");
42 /* Builtin firmware support */
44 #ifdef CONFIG_FW_LOADER
46 extern struct builtin_fw __start_builtin_fw[];
47 extern struct builtin_fw __end_builtin_fw[];
49 static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
50 void *buf, size_t size)
52 struct builtin_fw *b_fw;
54 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
55 if (strcmp(name, b_fw->name) == 0) {
56 fw->size = b_fw->size;
57 fw->data = b_fw->data;
59 if (buf && fw->size <= size)
60 memcpy(buf, fw->data, fw->size);
68 static bool fw_is_builtin_firmware(const struct firmware *fw)
70 struct builtin_fw *b_fw;
72 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
73 if (fw->data == b_fw->data)
79 #else /* Module case - no builtin firmware support */
81 static inline bool fw_get_builtin_firmware(struct firmware *fw,
82 const char *name, void *buf,
88 static inline bool fw_is_builtin_firmware(const struct firmware *fw)
101 static int loading_timeout = 60; /* In seconds */
103 static inline long firmware_loading_timeout(void)
105 return loading_timeout > 0 ? loading_timeout * HZ : MAX_JIFFY_OFFSET;
109 * Concurrent request_firmware() for the same firmware need to be
110 * serialized. struct fw_state is simple state machine which hold the
111 * state of the firmware loading.
114 struct completion completion;
115 enum fw_status status;
118 static void fw_state_init(struct fw_state *fw_st)
120 init_completion(&fw_st->completion);
121 fw_st->status = FW_STATUS_UNKNOWN;
124 static inline bool __fw_state_is_done(enum fw_status status)
126 return status == FW_STATUS_DONE || status == FW_STATUS_ABORTED;
129 static int __fw_state_wait_common(struct fw_state *fw_st, long timeout)
133 ret = wait_for_completion_interruptible_timeout(&fw_st->completion,
135 if (ret != 0 && fw_st->status == FW_STATUS_ABORTED)
140 return ret < 0 ? ret : 0;
143 static void __fw_state_set(struct fw_state *fw_st,
144 enum fw_status status)
146 WRITE_ONCE(fw_st->status, status);
148 if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED)
149 complete_all(&fw_st->completion);
152 #define fw_state_start(fw_st) \
153 __fw_state_set(fw_st, FW_STATUS_LOADING)
154 #define fw_state_done(fw_st) \
155 __fw_state_set(fw_st, FW_STATUS_DONE)
156 #define fw_state_wait(fw_st) \
157 __fw_state_wait_common(fw_st, MAX_SCHEDULE_TIMEOUT)
159 #ifndef CONFIG_FW_LOADER_USER_HELPER
161 #define fw_state_is_aborted(fw_st) false
163 #else /* CONFIG_FW_LOADER_USER_HELPER */
165 static int __fw_state_check(struct fw_state *fw_st, enum fw_status status)
167 return fw_st->status == status;
170 #define fw_state_aborted(fw_st) \
171 __fw_state_set(fw_st, FW_STATUS_ABORTED)
172 #define fw_state_is_done(fw_st) \
173 __fw_state_check(fw_st, FW_STATUS_DONE)
174 #define fw_state_is_loading(fw_st) \
175 __fw_state_check(fw_st, FW_STATUS_LOADING)
176 #define fw_state_is_aborted(fw_st) \
177 __fw_state_check(fw_st, FW_STATUS_ABORTED)
178 #define fw_state_wait_timeout(fw_st, timeout) \
179 __fw_state_wait_common(fw_st, timeout)
181 #endif /* CONFIG_FW_LOADER_USER_HELPER */
183 /* firmware behavior options */
184 #define FW_OPT_UEVENT (1U << 0)
185 #define FW_OPT_NOWAIT (1U << 1)
186 #ifdef CONFIG_FW_LOADER_USER_HELPER
187 #define FW_OPT_USERHELPER (1U << 2)
189 #define FW_OPT_USERHELPER 0
191 #ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK
192 #define FW_OPT_FALLBACK FW_OPT_USERHELPER
194 #define FW_OPT_FALLBACK 0
196 #define FW_OPT_NO_WARN (1U << 3)
197 #define FW_OPT_NOCACHE (1U << 4)
199 struct firmware_cache {
200 /* firmware_buf instance will be added into the below list */
202 struct list_head head;
205 #ifdef CONFIG_PM_SLEEP
207 * Names of firmware images which have been cached successfully
208 * will be added into the below list so that device uncache
209 * helper can trace which firmware images have been cached
212 spinlock_t name_lock;
213 struct list_head fw_names;
215 struct delayed_work work;
217 struct notifier_block pm_notify;
221 struct firmware_buf {
223 struct list_head list;
224 struct firmware_cache *fwc;
225 struct fw_state fw_st;
228 size_t allocated_size;
229 #ifdef CONFIG_FW_LOADER_USER_HELPER
235 struct list_head pending_list;
240 struct fw_cache_entry {
241 struct list_head list;
245 struct fw_name_devm {
250 #define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
252 #define FW_LOADER_NO_CACHE 0
253 #define FW_LOADER_START_CACHE 1
255 static int fw_cache_piggyback_on_request(const char *name);
257 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
258 * guarding for corner cases a global lock should be OK */
259 static DEFINE_MUTEX(fw_lock);
261 static bool __enable_firmware = false;
263 static void enable_firmware(void)
265 mutex_lock(&fw_lock);
266 __enable_firmware = true;
267 mutex_unlock(&fw_lock);
270 static void disable_firmware(void)
272 mutex_lock(&fw_lock);
273 __enable_firmware = false;
274 mutex_unlock(&fw_lock);
278 * When disabled only the built-in firmware and the firmware cache will be
279 * used to look for firmware.
281 static bool firmware_enabled(void)
283 bool enabled = false;
285 mutex_lock(&fw_lock);
286 if (__enable_firmware)
288 mutex_unlock(&fw_lock);
293 static struct firmware_cache fw_cache;
295 static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
296 struct firmware_cache *fwc,
297 void *dbuf, size_t size)
299 struct firmware_buf *buf;
301 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
305 buf->fw_id = kstrdup_const(fw_name, GFP_ATOMIC);
311 kref_init(&buf->ref);
314 buf->allocated_size = size;
315 fw_state_init(&buf->fw_st);
316 #ifdef CONFIG_FW_LOADER_USER_HELPER
317 INIT_LIST_HEAD(&buf->pending_list);
320 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
325 static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
327 struct firmware_buf *tmp;
328 struct firmware_cache *fwc = &fw_cache;
330 list_for_each_entry(tmp, &fwc->head, list)
331 if (!strcmp(tmp->fw_id, fw_name))
336 static int fw_lookup_and_allocate_buf(const char *fw_name,
337 struct firmware_cache *fwc,
338 struct firmware_buf **buf, void *dbuf,
341 struct firmware_buf *tmp;
343 spin_lock(&fwc->lock);
344 tmp = __fw_lookup_buf(fw_name);
347 spin_unlock(&fwc->lock);
351 tmp = __allocate_fw_buf(fw_name, fwc, dbuf, size);
353 list_add(&tmp->list, &fwc->head);
354 spin_unlock(&fwc->lock);
358 return tmp ? 0 : -ENOMEM;
361 static void __fw_free_buf(struct kref *ref)
362 __releases(&fwc->lock)
364 struct firmware_buf *buf = to_fwbuf(ref);
365 struct firmware_cache *fwc = buf->fwc;
367 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
368 __func__, buf->fw_id, buf, buf->data,
369 (unsigned int)buf->size);
371 list_del(&buf->list);
372 spin_unlock(&fwc->lock);
374 #ifdef CONFIG_FW_LOADER_USER_HELPER
375 if (buf->is_paged_buf) {
378 for (i = 0; i < buf->nr_pages; i++)
379 __free_page(buf->pages[i]);
383 if (!buf->allocated_size)
385 kfree_const(buf->fw_id);
389 static void fw_free_buf(struct firmware_buf *buf)
391 struct firmware_cache *fwc = buf->fwc;
392 spin_lock(&fwc->lock);
393 if (!kref_put(&buf->ref, __fw_free_buf))
394 spin_unlock(&fwc->lock);
397 /* direct firmware loading support */
398 static char fw_path_para[256];
399 static const char * const fw_path[] = {
401 "/lib/firmware/updates/" UTS_RELEASE,
402 "/lib/firmware/updates",
403 "/lib/firmware/" UTS_RELEASE,
408 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
409 * from kernel command line because firmware_class is generally built in
410 * kernel instead of module.
412 module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
413 MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
416 fw_get_filesystem_firmware(struct device *device, struct firmware_buf *buf)
422 enum kernel_read_file_id id = READING_FIRMWARE;
423 size_t msize = INT_MAX;
425 /* Already populated data member means we're loading into a buffer */
427 id = READING_FIRMWARE_PREALLOC_BUFFER;
428 msize = buf->allocated_size;
435 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
436 /* skip the unset customized path */
440 len = snprintf(path, PATH_MAX, "%s/%s",
441 fw_path[i], buf->fw_id);
442 if (len >= PATH_MAX) {
448 rc = kernel_read_file_from_path(path, &buf->data, &size, msize,
452 dev_dbg(device, "loading %s failed with error %d\n",
455 dev_warn(device, "loading %s failed with error %d\n",
459 dev_dbg(device, "direct-loading %s\n", buf->fw_id);
461 fw_state_done(&buf->fw_st);
469 /* firmware holds the ownership of pages */
470 static void firmware_free_data(const struct firmware *fw)
472 /* Loaded directly? */
477 fw_free_buf(fw->priv);
480 /* store the pages buffer info firmware from buf */
481 static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
484 #ifdef CONFIG_FW_LOADER_USER_HELPER
485 fw->pages = buf->pages;
487 fw->size = buf->size;
488 fw->data = buf->data;
490 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
491 __func__, buf->fw_id, buf, buf->data,
492 (unsigned int)buf->size);
495 #ifdef CONFIG_PM_SLEEP
496 static void fw_name_devm_release(struct device *dev, void *res)
498 struct fw_name_devm *fwn = res;
500 if (fwn->magic == (unsigned long)&fw_cache)
501 pr_debug("%s: fw_name-%s devm-%p released\n",
502 __func__, fwn->name, res);
503 kfree_const(fwn->name);
506 static int fw_devm_match(struct device *dev, void *res,
509 struct fw_name_devm *fwn = res;
511 return (fwn->magic == (unsigned long)&fw_cache) &&
512 !strcmp(fwn->name, match_data);
515 static struct fw_name_devm *fw_find_devm_name(struct device *dev,
518 struct fw_name_devm *fwn;
520 fwn = devres_find(dev, fw_name_devm_release,
521 fw_devm_match, (void *)name);
525 /* add firmware name into devres list */
526 static int fw_add_devm_name(struct device *dev, const char *name)
528 struct fw_name_devm *fwn;
530 fwn = fw_find_devm_name(dev, name);
534 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
538 fwn->name = kstrdup_const(name, GFP_KERNEL);
544 fwn->magic = (unsigned long)&fw_cache;
545 devres_add(dev, fwn);
550 static int fw_add_devm_name(struct device *dev, const char *name)
556 static int assign_firmware_buf(struct firmware *fw, struct device *device,
557 unsigned int opt_flags)
559 struct firmware_buf *buf = fw->priv;
561 mutex_lock(&fw_lock);
562 if (!buf->size || fw_state_is_aborted(&buf->fw_st)) {
563 mutex_unlock(&fw_lock);
568 * add firmware name into devres list so that we can auto cache
569 * and uncache firmware for device.
571 * device may has been deleted already, but the problem
572 * should be fixed in devres or driver core.
574 /* don't cache firmware handled without uevent */
575 if (device && (opt_flags & FW_OPT_UEVENT) &&
576 !(opt_flags & FW_OPT_NOCACHE))
577 fw_add_devm_name(device, buf->fw_id);
580 * After caching firmware image is started, let it piggyback
581 * on request firmware.
583 if (!(opt_flags & FW_OPT_NOCACHE) &&
584 buf->fwc->state == FW_LOADER_START_CACHE) {
585 if (fw_cache_piggyback_on_request(buf->fw_id))
589 /* pass the pages buffer to driver at the last minute */
590 fw_set_page_data(buf, fw);
591 mutex_unlock(&fw_lock);
596 * user-mode helper code
598 #ifdef CONFIG_FW_LOADER_USER_HELPER
599 struct firmware_priv {
602 struct firmware_buf *buf;
606 static struct firmware_priv *to_firmware_priv(struct device *dev)
608 return container_of(dev, struct firmware_priv, dev);
611 static void __fw_load_abort(struct firmware_buf *buf)
614 * There is a small window in which user can write to 'loading'
615 * between loading done and disappearance of 'loading'
617 if (fw_state_is_done(&buf->fw_st))
620 list_del_init(&buf->pending_list);
621 fw_state_aborted(&buf->fw_st);
624 static void fw_load_abort(struct firmware_priv *fw_priv)
626 struct firmware_buf *buf = fw_priv->buf;
628 __fw_load_abort(buf);
631 static LIST_HEAD(pending_fw_head);
633 static void kill_pending_fw_fallback_reqs(bool only_kill_custom)
635 struct firmware_buf *buf;
636 struct firmware_buf *next;
638 mutex_lock(&fw_lock);
639 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
640 if (!buf->need_uevent || !only_kill_custom)
641 __fw_load_abort(buf);
643 mutex_unlock(&fw_lock);
646 static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
649 return sprintf(buf, "%d\n", loading_timeout);
653 * firmware_timeout_store - set number of seconds to wait for firmware
654 * @class: device class pointer
655 * @attr: device attribute pointer
656 * @buf: buffer to scan for timeout value
657 * @count: number of bytes in @buf
659 * Sets the number of seconds to wait for the firmware. Once
660 * this expires an error will be returned to the driver and no
661 * firmware will be provided.
663 * Note: zero means 'wait forever'.
665 static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
666 const char *buf, size_t count)
668 loading_timeout = simple_strtol(buf, NULL, 10);
669 if (loading_timeout < 0)
674 static CLASS_ATTR_RW(timeout);
676 static struct attribute *firmware_class_attrs[] = {
677 &class_attr_timeout.attr,
680 ATTRIBUTE_GROUPS(firmware_class);
682 static void fw_dev_release(struct device *dev)
684 struct firmware_priv *fw_priv = to_firmware_priv(dev);
689 static int do_firmware_uevent(struct firmware_priv *fw_priv, struct kobj_uevent_env *env)
691 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
693 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
695 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
701 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
703 struct firmware_priv *fw_priv = to_firmware_priv(dev);
706 mutex_lock(&fw_lock);
708 err = do_firmware_uevent(fw_priv, env);
709 mutex_unlock(&fw_lock);
713 static struct class firmware_class = {
715 .class_groups = firmware_class_groups,
716 .dev_uevent = firmware_uevent,
717 .dev_release = fw_dev_release,
720 static ssize_t firmware_loading_show(struct device *dev,
721 struct device_attribute *attr, char *buf)
723 struct firmware_priv *fw_priv = to_firmware_priv(dev);
726 mutex_lock(&fw_lock);
728 loading = fw_state_is_loading(&fw_priv->buf->fw_st);
729 mutex_unlock(&fw_lock);
731 return sprintf(buf, "%d\n", loading);
734 /* Some architectures don't have PAGE_KERNEL_RO */
735 #ifndef PAGE_KERNEL_RO
736 #define PAGE_KERNEL_RO PAGE_KERNEL
739 /* one pages buffer should be mapped/unmapped only once */
740 static int fw_map_pages_buf(struct firmware_buf *buf)
742 if (!buf->is_paged_buf)
746 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
753 * firmware_loading_store - set value in the 'loading' control file
754 * @dev: device pointer
755 * @attr: device attribute pointer
756 * @buf: buffer to scan for loading control value
757 * @count: number of bytes in @buf
759 * The relevant values are:
761 * 1: Start a load, discarding any previous partial load.
762 * 0: Conclude the load and hand the data to the driver code.
763 * -1: Conclude the load with an error and discard any written data.
765 static ssize_t firmware_loading_store(struct device *dev,
766 struct device_attribute *attr,
767 const char *buf, size_t count)
769 struct firmware_priv *fw_priv = to_firmware_priv(dev);
770 struct firmware_buf *fw_buf;
771 ssize_t written = count;
772 int loading = simple_strtol(buf, NULL, 10);
775 mutex_lock(&fw_lock);
776 fw_buf = fw_priv->buf;
777 if (fw_state_is_aborted(&fw_buf->fw_st))
782 /* discarding any previous partial load */
783 if (!fw_state_is_done(&fw_buf->fw_st)) {
784 for (i = 0; i < fw_buf->nr_pages; i++)
785 __free_page(fw_buf->pages[i]);
786 vfree(fw_buf->pages);
787 fw_buf->pages = NULL;
788 fw_buf->page_array_size = 0;
789 fw_buf->nr_pages = 0;
790 fw_state_start(&fw_buf->fw_st);
794 if (fw_state_is_loading(&fw_buf->fw_st)) {
798 * Several loading requests may be pending on
799 * one same firmware buf, so let all requests
800 * see the mapped 'buf->data' once the loading
803 rc = fw_map_pages_buf(fw_buf);
805 dev_err(dev, "%s: map pages failed\n",
808 rc = security_kernel_post_read_file(NULL,
809 fw_buf->data, fw_buf->size,
813 * Same logic as fw_load_abort, only the DONE bit
814 * is ignored and we set ABORT only on failure.
816 list_del_init(&fw_buf->pending_list);
818 fw_state_aborted(&fw_buf->fw_st);
821 fw_state_done(&fw_buf->fw_st);
827 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
830 fw_load_abort(fw_priv);
834 mutex_unlock(&fw_lock);
838 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
840 static void firmware_rw_buf(struct firmware_buf *buf, char *buffer,
841 loff_t offset, size_t count, bool read)
844 memcpy(buffer, buf->data + offset, count);
846 memcpy(buf->data + offset, buffer, count);
849 static void firmware_rw(struct firmware_buf *buf, char *buffer,
850 loff_t offset, size_t count, bool read)
854 int page_nr = offset >> PAGE_SHIFT;
855 int page_ofs = offset & (PAGE_SIZE-1);
856 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
858 page_data = kmap(buf->pages[page_nr]);
861 memcpy(buffer, page_data + page_ofs, page_cnt);
863 memcpy(page_data + page_ofs, buffer, page_cnt);
865 kunmap(buf->pages[page_nr]);
872 static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
873 struct bin_attribute *bin_attr,
874 char *buffer, loff_t offset, size_t count)
876 struct device *dev = kobj_to_dev(kobj);
877 struct firmware_priv *fw_priv = to_firmware_priv(dev);
878 struct firmware_buf *buf;
881 mutex_lock(&fw_lock);
883 if (!buf || fw_state_is_done(&buf->fw_st)) {
887 if (offset > buf->size) {
891 if (count > buf->size - offset)
892 count = buf->size - offset;
897 firmware_rw_buf(buf, buffer, offset, count, true);
899 firmware_rw(buf, buffer, offset, count, true);
902 mutex_unlock(&fw_lock);
906 static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
908 struct firmware_buf *buf = fw_priv->buf;
909 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
911 /* If the array of pages is too small, grow it... */
912 if (buf->page_array_size < pages_needed) {
913 int new_array_size = max(pages_needed,
914 buf->page_array_size * 2);
915 struct page **new_pages;
917 new_pages = vmalloc(new_array_size * sizeof(void *));
919 fw_load_abort(fw_priv);
922 memcpy(new_pages, buf->pages,
923 buf->page_array_size * sizeof(void *));
924 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
925 (new_array_size - buf->page_array_size));
927 buf->pages = new_pages;
928 buf->page_array_size = new_array_size;
931 while (buf->nr_pages < pages_needed) {
932 buf->pages[buf->nr_pages] =
933 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
935 if (!buf->pages[buf->nr_pages]) {
936 fw_load_abort(fw_priv);
945 * firmware_data_write - write method for firmware
946 * @filp: open sysfs file
947 * @kobj: kobject for the device
948 * @bin_attr: bin_attr structure
949 * @buffer: buffer being written
950 * @offset: buffer offset for write in total data store area
951 * @count: buffer size
953 * Data written to the 'data' attribute will be later handed to
954 * the driver as a firmware image.
956 static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
957 struct bin_attribute *bin_attr,
958 char *buffer, loff_t offset, size_t count)
960 struct device *dev = kobj_to_dev(kobj);
961 struct firmware_priv *fw_priv = to_firmware_priv(dev);
962 struct firmware_buf *buf;
965 if (!capable(CAP_SYS_RAWIO))
968 mutex_lock(&fw_lock);
970 if (!buf || fw_state_is_done(&buf->fw_st)) {
976 if (offset + count > buf->allocated_size) {
980 firmware_rw_buf(buf, buffer, offset, count, false);
983 retval = fw_realloc_buffer(fw_priv, offset + count);
988 firmware_rw(buf, buffer, offset, count, false);
991 buf->size = max_t(size_t, offset + count, buf->size);
993 mutex_unlock(&fw_lock);
997 static struct bin_attribute firmware_attr_data = {
998 .attr = { .name = "data", .mode = 0644 },
1000 .read = firmware_data_read,
1001 .write = firmware_data_write,
1004 static struct attribute *fw_dev_attrs[] = {
1005 &dev_attr_loading.attr,
1009 static struct bin_attribute *fw_dev_bin_attrs[] = {
1010 &firmware_attr_data,
1014 static const struct attribute_group fw_dev_attr_group = {
1015 .attrs = fw_dev_attrs,
1016 .bin_attrs = fw_dev_bin_attrs,
1019 static const struct attribute_group *fw_dev_attr_groups[] = {
1024 static struct firmware_priv *
1025 fw_create_instance(struct firmware *firmware, const char *fw_name,
1026 struct device *device, unsigned int opt_flags)
1028 struct firmware_priv *fw_priv;
1029 struct device *f_dev;
1031 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
1033 fw_priv = ERR_PTR(-ENOMEM);
1037 fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT);
1038 fw_priv->fw = firmware;
1039 f_dev = &fw_priv->dev;
1041 device_initialize(f_dev);
1042 dev_set_name(f_dev, "%s", fw_name);
1043 f_dev->parent = device;
1044 f_dev->class = &firmware_class;
1045 f_dev->groups = fw_dev_attr_groups;
1050 /* load a firmware via user helper */
1051 static int _request_firmware_load(struct firmware_priv *fw_priv,
1052 unsigned int opt_flags, long timeout)
1055 struct device *f_dev = &fw_priv->dev;
1056 struct firmware_buf *buf = fw_priv->buf;
1058 /* fall back on userspace loading */
1060 buf->is_paged_buf = true;
1062 dev_set_uevent_suppress(f_dev, true);
1064 retval = device_add(f_dev);
1066 dev_err(f_dev, "%s: device_register failed\n", __func__);
1070 mutex_lock(&fw_lock);
1071 list_add(&buf->pending_list, &pending_fw_head);
1072 mutex_unlock(&fw_lock);
1074 if (opt_flags & FW_OPT_UEVENT) {
1075 buf->need_uevent = true;
1076 dev_set_uevent_suppress(f_dev, false);
1077 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
1078 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
1080 timeout = MAX_JIFFY_OFFSET;
1083 retval = fw_state_wait_timeout(&buf->fw_st, timeout);
1085 mutex_lock(&fw_lock);
1086 fw_load_abort(fw_priv);
1087 mutex_unlock(&fw_lock);
1090 if (fw_state_is_aborted(&buf->fw_st))
1092 else if (buf->is_paged_buf && !buf->data)
1101 static int fw_load_from_user_helper(struct firmware *firmware,
1102 const char *name, struct device *device,
1103 unsigned int opt_flags)
1105 struct firmware_priv *fw_priv;
1109 timeout = firmware_loading_timeout();
1110 if (opt_flags & FW_OPT_NOWAIT) {
1111 timeout = usermodehelper_read_lock_wait(timeout);
1113 dev_dbg(device, "firmware: %s loading timed out\n",
1118 ret = usermodehelper_read_trylock();
1120 dev_err(device, "firmware: %s will not be loaded\n",
1126 fw_priv = fw_create_instance(firmware, name, device, opt_flags);
1127 if (IS_ERR(fw_priv)) {
1128 ret = PTR_ERR(fw_priv);
1132 fw_priv->buf = firmware->priv;
1133 ret = _request_firmware_load(fw_priv, opt_flags, timeout);
1136 ret = assign_firmware_buf(firmware, device, opt_flags);
1139 usermodehelper_read_unlock();
1144 #else /* CONFIG_FW_LOADER_USER_HELPER */
1146 fw_load_from_user_helper(struct firmware *firmware, const char *name,
1147 struct device *device, unsigned int opt_flags)
1152 static inline void kill_pending_fw_fallback_reqs(bool only_kill_custom) { }
1154 #endif /* CONFIG_FW_LOADER_USER_HELPER */
1156 /* prepare firmware and firmware_buf structs;
1157 * return 0 if a firmware is already assigned, 1 if need to load one,
1158 * or a negative error code
1161 _request_firmware_prepare(struct firmware **firmware_p, const char *name,
1162 struct device *device, void *dbuf, size_t size)
1164 struct firmware *firmware;
1165 struct firmware_buf *buf;
1168 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
1170 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1175 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
1176 dev_dbg(device, "using built-in %s\n", name);
1177 return 0; /* assigned */
1180 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf, dbuf, size);
1183 * bind with 'buf' now to avoid warning in failure path
1184 * of requesting firmware.
1186 firmware->priv = buf;
1189 ret = fw_state_wait(&buf->fw_st);
1191 fw_set_page_data(buf, firmware);
1192 return 0; /* assigned */
1198 return 1; /* need to load */
1201 /* called from request_firmware() and request_firmware_work_func() */
1203 _request_firmware(const struct firmware **firmware_p, const char *name,
1204 struct device *device, void *buf, size_t size,
1205 unsigned int opt_flags)
1207 struct firmware *fw = NULL;
1213 if (!name || name[0] == '\0') {
1218 ret = _request_firmware_prepare(&fw, name, device, buf, size);
1219 if (ret <= 0) /* error or already assigned */
1222 if (!firmware_enabled()) {
1223 WARN(1, "firmware request while host is not available\n");
1228 ret = fw_get_filesystem_firmware(device, fw->priv);
1230 if (!(opt_flags & FW_OPT_NO_WARN))
1232 "Direct firmware load for %s failed with error %d\n",
1234 if (opt_flags & FW_OPT_USERHELPER) {
1235 dev_warn(device, "Falling back to user helper\n");
1236 ret = fw_load_from_user_helper(fw, name, device,
1240 ret = assign_firmware_buf(fw, device, opt_flags);
1244 release_firmware(fw);
1253 * request_firmware: - send firmware request and wait for it
1254 * @firmware_p: pointer to firmware image
1255 * @name: name of firmware file
1256 * @device: device for which firmware is being loaded
1258 * @firmware_p will be used to return a firmware image by the name
1259 * of @name for device @device.
1261 * Should be called from user context where sleeping is allowed.
1263 * @name will be used as $FIRMWARE in the uevent environment and
1264 * should be distinctive enough not to be confused with any other
1265 * firmware image for this or any other device.
1267 * Caller must hold the reference count of @device.
1269 * The function can be called safely inside device's suspend and
1273 request_firmware(const struct firmware **firmware_p, const char *name,
1274 struct device *device)
1278 /* Need to pin this module until return */
1279 __module_get(THIS_MODULE);
1280 ret = _request_firmware(firmware_p, name, device, NULL, 0,
1281 FW_OPT_UEVENT | FW_OPT_FALLBACK);
1282 module_put(THIS_MODULE);
1285 EXPORT_SYMBOL(request_firmware);
1288 * request_firmware_direct: - load firmware directly without usermode helper
1289 * @firmware_p: pointer to firmware image
1290 * @name: name of firmware file
1291 * @device: device for which firmware is being loaded
1293 * This function works pretty much like request_firmware(), but this doesn't
1294 * fall back to usermode helper even if the firmware couldn't be loaded
1295 * directly from fs. Hence it's useful for loading optional firmwares, which
1296 * aren't always present, without extra long timeouts of udev.
1298 int request_firmware_direct(const struct firmware **firmware_p,
1299 const char *name, struct device *device)
1303 __module_get(THIS_MODULE);
1304 ret = _request_firmware(firmware_p, name, device, NULL, 0,
1305 FW_OPT_UEVENT | FW_OPT_NO_WARN);
1306 module_put(THIS_MODULE);
1309 EXPORT_SYMBOL_GPL(request_firmware_direct);
1312 * request_firmware_into_buf - load firmware into a previously allocated buffer
1313 * @firmware_p: pointer to firmware image
1314 * @name: name of firmware file
1315 * @device: device for which firmware is being loaded and DMA region allocated
1316 * @buf: address of buffer to load firmware into
1317 * @size: size of buffer
1319 * This function works pretty much like request_firmware(), but it doesn't
1320 * allocate a buffer to hold the firmware data. Instead, the firmware
1321 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
1322 * data member is pointed at @buf.
1324 * This function doesn't cache firmware either.
1327 request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
1328 struct device *device, void *buf, size_t size)
1332 __module_get(THIS_MODULE);
1333 ret = _request_firmware(firmware_p, name, device, buf, size,
1334 FW_OPT_UEVENT | FW_OPT_FALLBACK |
1336 module_put(THIS_MODULE);
1339 EXPORT_SYMBOL(request_firmware_into_buf);
1342 * release_firmware: - release the resource associated with a firmware image
1343 * @fw: firmware resource to release
1345 void release_firmware(const struct firmware *fw)
1348 if (!fw_is_builtin_firmware(fw))
1349 firmware_free_data(fw);
1353 EXPORT_SYMBOL(release_firmware);
1356 struct firmware_work {
1357 struct work_struct work;
1358 struct module *module;
1360 struct device *device;
1362 void (*cont)(const struct firmware *fw, void *context);
1363 unsigned int opt_flags;
1366 static void request_firmware_work_func(struct work_struct *work)
1368 struct firmware_work *fw_work;
1369 const struct firmware *fw;
1371 fw_work = container_of(work, struct firmware_work, work);
1373 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
1374 fw_work->opt_flags);
1375 fw_work->cont(fw, fw_work->context);
1376 put_device(fw_work->device); /* taken in request_firmware_nowait() */
1378 module_put(fw_work->module);
1379 kfree_const(fw_work->name);
1384 * request_firmware_nowait - asynchronous version of request_firmware
1385 * @module: module requesting the firmware
1386 * @uevent: sends uevent to copy the firmware image if this flag
1387 * is non-zero else the firmware copy must be done manually.
1388 * @name: name of firmware file
1389 * @device: device for which firmware is being loaded
1390 * @gfp: allocation flags
1391 * @context: will be passed over to @cont, and
1392 * @fw may be %NULL if firmware request fails.
1393 * @cont: function will be called asynchronously when the firmware
1396 * Caller must hold the reference count of @device.
1398 * Asynchronous variant of request_firmware() for user contexts:
1399 * - sleep for as small periods as possible since it may
1400 * increase kernel boot time of built-in device drivers
1401 * requesting firmware in their ->probe() methods, if
1402 * @gfp is GFP_KERNEL.
1404 * - can't sleep at all if @gfp is GFP_ATOMIC.
1407 request_firmware_nowait(
1408 struct module *module, bool uevent,
1409 const char *name, struct device *device, gfp_t gfp, void *context,
1410 void (*cont)(const struct firmware *fw, void *context))
1412 struct firmware_work *fw_work;
1414 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
1418 fw_work->module = module;
1419 fw_work->name = kstrdup_const(name, gfp);
1420 if (!fw_work->name) {
1424 fw_work->device = device;
1425 fw_work->context = context;
1426 fw_work->cont = cont;
1427 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
1428 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
1430 if (!try_module_get(module)) {
1431 kfree_const(fw_work->name);
1436 get_device(fw_work->device);
1437 INIT_WORK(&fw_work->work, request_firmware_work_func);
1438 schedule_work(&fw_work->work);
1441 EXPORT_SYMBOL(request_firmware_nowait);
1443 #ifdef CONFIG_PM_SLEEP
1444 static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1447 * cache_firmware - cache one firmware image in kernel memory space
1448 * @fw_name: the firmware image name
1450 * Cache firmware in kernel memory so that drivers can use it when
1451 * system isn't ready for them to request firmware image from userspace.
1452 * Once it returns successfully, driver can use request_firmware or its
1453 * nowait version to get the cached firmware without any interacting
1456 * Return 0 if the firmware image has been cached successfully
1457 * Return !0 otherwise
1460 static int cache_firmware(const char *fw_name)
1463 const struct firmware *fw;
1465 pr_debug("%s: %s\n", __func__, fw_name);
1467 ret = request_firmware(&fw, fw_name, NULL);
1471 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1476 static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1478 struct firmware_buf *tmp;
1479 struct firmware_cache *fwc = &fw_cache;
1481 spin_lock(&fwc->lock);
1482 tmp = __fw_lookup_buf(fw_name);
1483 spin_unlock(&fwc->lock);
1489 * uncache_firmware - remove one cached firmware image
1490 * @fw_name: the firmware image name
1492 * Uncache one firmware image which has been cached successfully
1495 * Return 0 if the firmware cache has been removed successfully
1496 * Return !0 otherwise
1499 static int uncache_firmware(const char *fw_name)
1501 struct firmware_buf *buf;
1504 pr_debug("%s: %s\n", __func__, fw_name);
1506 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
1509 buf = fw_lookup_buf(fw_name);
1518 static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1520 struct fw_cache_entry *fce;
1522 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
1526 fce->name = kstrdup_const(name, GFP_ATOMIC);
1536 static int __fw_entry_found(const char *name)
1538 struct firmware_cache *fwc = &fw_cache;
1539 struct fw_cache_entry *fce;
1541 list_for_each_entry(fce, &fwc->fw_names, list) {
1542 if (!strcmp(fce->name, name))
1548 static int fw_cache_piggyback_on_request(const char *name)
1550 struct firmware_cache *fwc = &fw_cache;
1551 struct fw_cache_entry *fce;
1554 spin_lock(&fwc->name_lock);
1555 if (__fw_entry_found(name))
1558 fce = alloc_fw_cache_entry(name);
1561 list_add(&fce->list, &fwc->fw_names);
1562 pr_debug("%s: fw: %s\n", __func__, name);
1565 spin_unlock(&fwc->name_lock);
1569 static void free_fw_cache_entry(struct fw_cache_entry *fce)
1571 kfree_const(fce->name);
1575 static void __async_dev_cache_fw_image(void *fw_entry,
1576 async_cookie_t cookie)
1578 struct fw_cache_entry *fce = fw_entry;
1579 struct firmware_cache *fwc = &fw_cache;
1582 ret = cache_firmware(fce->name);
1584 spin_lock(&fwc->name_lock);
1585 list_del(&fce->list);
1586 spin_unlock(&fwc->name_lock);
1588 free_fw_cache_entry(fce);
1592 /* called with dev->devres_lock held */
1593 static void dev_create_fw_entry(struct device *dev, void *res,
1596 struct fw_name_devm *fwn = res;
1597 const char *fw_name = fwn->name;
1598 struct list_head *head = data;
1599 struct fw_cache_entry *fce;
1601 fce = alloc_fw_cache_entry(fw_name);
1603 list_add(&fce->list, head);
1606 static int devm_name_match(struct device *dev, void *res,
1609 struct fw_name_devm *fwn = res;
1610 return (fwn->magic == (unsigned long)match_data);
1613 static void dev_cache_fw_image(struct device *dev, void *data)
1616 struct fw_cache_entry *fce;
1617 struct fw_cache_entry *fce_next;
1618 struct firmware_cache *fwc = &fw_cache;
1620 devres_for_each_res(dev, fw_name_devm_release,
1621 devm_name_match, &fw_cache,
1622 dev_create_fw_entry, &todo);
1624 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1625 list_del(&fce->list);
1627 spin_lock(&fwc->name_lock);
1628 /* only one cache entry for one firmware */
1629 if (!__fw_entry_found(fce->name)) {
1630 list_add(&fce->list, &fwc->fw_names);
1632 free_fw_cache_entry(fce);
1635 spin_unlock(&fwc->name_lock);
1638 async_schedule_domain(__async_dev_cache_fw_image,
1644 static void __device_uncache_fw_images(void)
1646 struct firmware_cache *fwc = &fw_cache;
1647 struct fw_cache_entry *fce;
1649 spin_lock(&fwc->name_lock);
1650 while (!list_empty(&fwc->fw_names)) {
1651 fce = list_entry(fwc->fw_names.next,
1652 struct fw_cache_entry, list);
1653 list_del(&fce->list);
1654 spin_unlock(&fwc->name_lock);
1656 uncache_firmware(fce->name);
1657 free_fw_cache_entry(fce);
1659 spin_lock(&fwc->name_lock);
1661 spin_unlock(&fwc->name_lock);
1665 * device_cache_fw_images - cache devices' firmware
1667 * If one device called request_firmware or its nowait version
1668 * successfully before, the firmware names are recored into the
1669 * device's devres link list, so device_cache_fw_images can call
1670 * cache_firmware() to cache these firmwares for the device,
1671 * then the device driver can load its firmwares easily at
1672 * time when system is not ready to complete loading firmware.
1674 static void device_cache_fw_images(void)
1676 struct firmware_cache *fwc = &fw_cache;
1680 pr_debug("%s\n", __func__);
1682 /* cancel uncache work */
1683 cancel_delayed_work_sync(&fwc->work);
1686 * use small loading timeout for caching devices' firmware
1687 * because all these firmware images have been loaded
1688 * successfully at lease once, also system is ready for
1689 * completing firmware loading now. The maximum size of
1690 * firmware in current distributions is about 2M bytes,
1691 * so 10 secs should be enough.
1693 old_timeout = loading_timeout;
1694 loading_timeout = 10;
1696 mutex_lock(&fw_lock);
1697 fwc->state = FW_LOADER_START_CACHE;
1698 dpm_for_each_dev(NULL, dev_cache_fw_image);
1699 mutex_unlock(&fw_lock);
1701 /* wait for completion of caching firmware for all devices */
1702 async_synchronize_full_domain(&fw_cache_domain);
1704 loading_timeout = old_timeout;
1708 * device_uncache_fw_images - uncache devices' firmware
1710 * uncache all firmwares which have been cached successfully
1711 * by device_uncache_fw_images earlier
1713 static void device_uncache_fw_images(void)
1715 pr_debug("%s\n", __func__);
1716 __device_uncache_fw_images();
1719 static void device_uncache_fw_images_work(struct work_struct *work)
1721 device_uncache_fw_images();
1725 * device_uncache_fw_images_delay - uncache devices firmwares
1726 * @delay: number of milliseconds to delay uncache device firmwares
1728 * uncache all devices's firmwares which has been cached successfully
1729 * by device_cache_fw_images after @delay milliseconds.
1731 static void device_uncache_fw_images_delay(unsigned long delay)
1733 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1734 msecs_to_jiffies(delay));
1738 * fw_pm_notify - notifier for suspend/resume
1739 * @notify_block: unused
1740 * @mode: mode we are switching to
1743 * Used to modify the firmware_class state as we move in between states.
1744 * The firmware_class implements a firmware cache to enable device driver
1745 * to fetch firmware upon resume before the root filesystem is ready. We
1746 * disable API calls which do not use the built-in firmware or the firmware
1747 * cache when we know these calls will not work.
1749 * The inner logic behind all this is a bit complex so it is worth summarizing
1750 * the kernel's own suspend/resume process with context and focus on how this
1751 * can impact the firmware API.
1753 * First a review on how we go to suspend::
1755 * pm_suspend() --> enter_state() -->
1757 * suspend_prepare() -->
1758 * __pm_notifier_call_chain(PM_SUSPEND_PREPARE, ...);
1759 * suspend_freeze_processes() -->
1760 * freeze_processes() -->
1761 * __usermodehelper_set_disable_depth(UMH_DISABLED);
1762 * freeze all tasks ...
1763 * freeze_kernel_threads()
1764 * suspend_devices_and_enter() -->
1765 * dpm_suspend_start() -->
1768 * suspend_enter() -->
1769 * platform_suspend_prepare()
1770 * dpm_suspend_late()
1774 * When we resume we bail out of a loop from suspend_devices_and_enter() and
1775 * unwind back out to the caller enter_state() where we were before as follows::
1778 * suspend_devices_and_enter() --> (bail from loop)
1779 * dpm_resume_end() -->
1782 * suspend_finish() -->
1783 * suspend_thaw_processes() -->
1784 * thaw_processes() -->
1785 * __usermodehelper_set_disable_depth(UMH_FREEZING);
1786 * thaw_workqueues();
1787 * thaw all processes ...
1788 * usermodehelper_enable();
1789 * pm_notifier_call_chain(PM_POST_SUSPEND);
1791 * fw_pm_notify() works through pm_notifier_call_chain().
1793 static int fw_pm_notify(struct notifier_block *notify_block,
1794 unsigned long mode, void *unused)
1797 case PM_HIBERNATION_PREPARE:
1798 case PM_SUSPEND_PREPARE:
1799 case PM_RESTORE_PREPARE:
1801 * kill pending fallback requests with a custom fallback
1802 * to avoid stalling suspend.
1804 kill_pending_fw_fallback_reqs(true);
1805 device_cache_fw_images();
1809 case PM_POST_SUSPEND:
1810 case PM_POST_HIBERNATION:
1811 case PM_POST_RESTORE:
1813 * In case that system sleep failed and syscore_suspend is
1816 mutex_lock(&fw_lock);
1817 fw_cache.state = FW_LOADER_NO_CACHE;
1818 mutex_unlock(&fw_lock);
1821 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1828 /* stop caching firmware once syscore_suspend is reached */
1829 static int fw_suspend(void)
1831 fw_cache.state = FW_LOADER_NO_CACHE;
1835 static struct syscore_ops fw_syscore_ops = {
1836 .suspend = fw_suspend,
1839 static int fw_cache_piggyback_on_request(const char *name)
1845 static void __init fw_cache_init(void)
1847 spin_lock_init(&fw_cache.lock);
1848 INIT_LIST_HEAD(&fw_cache.head);
1849 fw_cache.state = FW_LOADER_NO_CACHE;
1851 #ifdef CONFIG_PM_SLEEP
1852 spin_lock_init(&fw_cache.name_lock);
1853 INIT_LIST_HEAD(&fw_cache.fw_names);
1855 INIT_DELAYED_WORK(&fw_cache.work,
1856 device_uncache_fw_images_work);
1858 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1859 register_pm_notifier(&fw_cache.pm_notify);
1861 register_syscore_ops(&fw_syscore_ops);
1865 static int fw_shutdown_notify(struct notifier_block *unused1,
1866 unsigned long unused2, void *unused3)
1870 * Kill all pending fallback requests to avoid both stalling shutdown,
1871 * and avoid a deadlock with the usermode_lock.
1873 kill_pending_fw_fallback_reqs(false);
1878 static struct notifier_block fw_shutdown_nb = {
1879 .notifier_call = fw_shutdown_notify,
1882 static int __init firmware_class_init(void)
1886 register_reboot_notifier(&fw_shutdown_nb);
1887 #ifdef CONFIG_FW_LOADER_USER_HELPER
1888 return class_register(&firmware_class);
1894 static void __exit firmware_class_exit(void)
1897 #ifdef CONFIG_PM_SLEEP
1898 unregister_syscore_ops(&fw_syscore_ops);
1899 unregister_pm_notifier(&fw_cache.pm_notify);
1901 unregister_reboot_notifier(&fw_shutdown_nb);
1902 #ifdef CONFIG_FW_LOADER_USER_HELPER
1903 class_unregister(&firmware_class);
1907 fs_initcall(firmware_class_init);
1908 module_exit(firmware_class_exit);