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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/capability.h>
13 #include <linux/device.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/timer.h>
17 #include <linux/vmalloc.h>
18 #include <linux/interrupt.h>
19 #include <linux/bitops.h>
20 #include <linux/mutex.h>
21 #include <linux/workqueue.h>
22 #include <linux/highmem.h>
23 #include <linux/firmware.h>
24 #include <linux/slab.h>
25 #include <linux/sched.h>
26 #include <linux/file.h>
27 #include <linux/list.h>
29 #include <linux/async.h>
31 #include <linux/suspend.h>
32 #include <linux/syscore_ops.h>
33 #include <linux/reboot.h>
34 #include <linux/security.h>
35 #include <linux/root_dev.h>
37 #include <generated/utsrelease.h>
41 MODULE_AUTHOR("Manuel Estrada Sainz");
42 MODULE_DESCRIPTION("Multi purpose firmware loading support");
43 MODULE_LICENSE("GPL");
45 /* Builtin firmware support */
47 #ifdef CONFIG_FW_LOADER
49 extern struct builtin_fw __start_builtin_fw[];
50 extern struct builtin_fw __end_builtin_fw[];
52 static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
53 void *buf, size_t size)
55 struct builtin_fw *b_fw;
57 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
58 if (strcmp(name, b_fw->name) == 0) {
59 fw->size = b_fw->size;
60 fw->data = b_fw->data;
62 if (buf && fw->size <= size)
63 memcpy(buf, fw->data, fw->size);
71 static bool fw_is_builtin_firmware(const struct firmware *fw)
73 struct builtin_fw *b_fw;
75 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
76 if (fw->data == b_fw->data)
82 #else /* Module case - no builtin firmware support */
84 static inline bool fw_get_builtin_firmware(struct firmware *fw,
85 const char *name, void *buf,
91 static inline bool fw_is_builtin_firmware(const struct firmware *fw)
104 static int loading_timeout = 60; /* In seconds */
106 static inline long firmware_loading_timeout(void)
108 return loading_timeout > 0 ? loading_timeout * HZ : MAX_JIFFY_OFFSET;
112 * Concurrent request_firmware() for the same firmware need to be
113 * serialized. struct fw_state is simple state machine which hold the
114 * state of the firmware loading.
117 struct completion completion;
118 enum fw_status status;
121 static void fw_state_init(struct fw_state *fw_st)
123 init_completion(&fw_st->completion);
124 fw_st->status = FW_STATUS_UNKNOWN;
127 static inline bool __fw_state_is_done(enum fw_status status)
129 return status == FW_STATUS_DONE || status == FW_STATUS_ABORTED;
132 static int __fw_state_wait_common(struct fw_state *fw_st, long timeout)
136 ret = wait_for_completion_killable_timeout(&fw_st->completion, timeout);
137 if (ret != 0 && fw_st->status == FW_STATUS_ABORTED)
142 return ret < 0 ? ret : 0;
145 static void __fw_state_set(struct fw_state *fw_st,
146 enum fw_status status)
148 WRITE_ONCE(fw_st->status, status);
150 if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED)
151 complete_all(&fw_st->completion);
154 #define fw_state_start(fw_st) \
155 __fw_state_set(fw_st, FW_STATUS_LOADING)
156 #define fw_state_done(fw_st) \
157 __fw_state_set(fw_st, FW_STATUS_DONE)
158 #define fw_state_aborted(fw_st) \
159 __fw_state_set(fw_st, FW_STATUS_ABORTED)
160 #define fw_state_wait(fw_st) \
161 __fw_state_wait_common(fw_st, MAX_SCHEDULE_TIMEOUT)
163 static int __fw_state_check(struct fw_state *fw_st, enum fw_status status)
165 return fw_st->status == status;
168 #define fw_state_is_aborted(fw_st) \
169 __fw_state_check(fw_st, FW_STATUS_ABORTED)
171 #ifdef CONFIG_FW_LOADER_USER_HELPER
173 #define fw_state_aborted(fw_st) \
174 __fw_state_set(fw_st, FW_STATUS_ABORTED)
175 #define fw_state_is_done(fw_st) \
176 __fw_state_check(fw_st, FW_STATUS_DONE)
177 #define fw_state_is_loading(fw_st) \
178 __fw_state_check(fw_st, FW_STATUS_LOADING)
179 #define fw_state_wait_timeout(fw_st, timeout) \
180 __fw_state_wait_common(fw_st, timeout)
182 #endif /* CONFIG_FW_LOADER_USER_HELPER */
184 /* firmware behavior options */
185 #define FW_OPT_UEVENT (1U << 0)
186 #define FW_OPT_NOWAIT (1U << 1)
187 #ifdef CONFIG_FW_LOADER_USER_HELPER
188 #define FW_OPT_USERHELPER (1U << 2)
190 #define FW_OPT_USERHELPER 0
192 #ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK
193 #define FW_OPT_FALLBACK FW_OPT_USERHELPER
195 #define FW_OPT_FALLBACK 0
197 #define FW_OPT_NO_WARN (1U << 3)
198 #define FW_OPT_NOCACHE (1U << 4)
200 struct firmware_cache {
201 /* firmware_buf instance will be added into the below list */
203 struct list_head head;
206 #ifdef CONFIG_PM_SLEEP
208 * Names of firmware images which have been cached successfully
209 * will be added into the below list so that device uncache
210 * helper can trace which firmware images have been cached
213 spinlock_t name_lock;
214 struct list_head fw_names;
216 struct delayed_work work;
218 struct notifier_block pm_notify;
222 struct firmware_buf {
224 struct list_head list;
225 struct firmware_cache *fwc;
226 struct fw_state fw_st;
229 size_t allocated_size;
230 #ifdef CONFIG_FW_LOADER_USER_HELPER
236 struct list_head pending_list;
241 struct fw_cache_entry {
242 struct list_head list;
246 struct fw_name_devm {
251 #define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
253 #define FW_LOADER_NO_CACHE 0
254 #define FW_LOADER_START_CACHE 1
256 static int fw_cache_piggyback_on_request(const char *name);
258 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
259 * guarding for corner cases a global lock should be OK */
260 static DEFINE_MUTEX(fw_lock);
262 static struct firmware_cache fw_cache;
264 static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
265 struct firmware_cache *fwc,
266 void *dbuf, size_t size)
268 struct firmware_buf *buf;
270 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
274 buf->fw_id = kstrdup_const(fw_name, GFP_ATOMIC);
280 kref_init(&buf->ref);
283 buf->allocated_size = size;
284 fw_state_init(&buf->fw_st);
285 #ifdef CONFIG_FW_LOADER_USER_HELPER
286 INIT_LIST_HEAD(&buf->pending_list);
289 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
294 static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
296 struct firmware_buf *tmp;
297 struct firmware_cache *fwc = &fw_cache;
299 list_for_each_entry(tmp, &fwc->head, list)
300 if (!strcmp(tmp->fw_id, fw_name))
305 /* Returns 1 for batching firmware requests with the same name */
306 static int fw_lookup_and_allocate_buf(const char *fw_name,
307 struct firmware_cache *fwc,
308 struct firmware_buf **buf, void *dbuf,
311 struct firmware_buf *tmp;
313 spin_lock(&fwc->lock);
314 tmp = __fw_lookup_buf(fw_name);
317 spin_unlock(&fwc->lock);
319 pr_debug("batched request - sharing the same struct firmware_buf and lookup for multiple requests\n");
322 tmp = __allocate_fw_buf(fw_name, fwc, dbuf, size);
324 list_add(&tmp->list, &fwc->head);
325 spin_unlock(&fwc->lock);
329 return tmp ? 0 : -ENOMEM;
332 static void __fw_free_buf(struct kref *ref)
333 __releases(&fwc->lock)
335 struct firmware_buf *buf = to_fwbuf(ref);
336 struct firmware_cache *fwc = buf->fwc;
338 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
339 __func__, buf->fw_id, buf, buf->data,
340 (unsigned int)buf->size);
342 list_del(&buf->list);
343 spin_unlock(&fwc->lock);
345 #ifdef CONFIG_FW_LOADER_USER_HELPER
346 if (buf->is_paged_buf) {
349 for (i = 0; i < buf->nr_pages; i++)
350 __free_page(buf->pages[i]);
354 if (!buf->allocated_size)
356 kfree_const(buf->fw_id);
360 static void fw_free_buf(struct firmware_buf *buf)
362 struct firmware_cache *fwc = buf->fwc;
363 spin_lock(&fwc->lock);
364 if (!kref_put(&buf->ref, __fw_free_buf))
365 spin_unlock(&fwc->lock);
368 /* direct firmware loading support */
369 static char fw_path_para[256];
370 static const char * const fw_path[] = {
372 "/lib/firmware/updates/" UTS_RELEASE,
373 "/lib/firmware/updates",
374 "/lib/firmware/" UTS_RELEASE,
379 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
380 * from kernel command line because firmware_class is generally built in
381 * kernel instead of module.
383 module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
384 MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
387 fw_get_filesystem_firmware(struct device *device, struct firmware_buf *buf)
393 enum kernel_read_file_id id = READING_FIRMWARE;
394 size_t msize = INT_MAX;
397 return -EPROBE_DEFER;
399 /* Already populated data member means we're loading into a buffer */
401 id = READING_FIRMWARE_PREALLOC_BUFFER;
402 msize = buf->allocated_size;
409 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
410 /* skip the unset customized path */
414 len = snprintf(path, PATH_MAX, "%s/%s",
415 fw_path[i], buf->fw_id);
416 if (len >= PATH_MAX) {
422 rc = kernel_read_file_from_path(path, &buf->data, &size, msize,
426 dev_dbg(device, "loading %s failed with error %d\n",
429 dev_warn(device, "loading %s failed with error %d\n",
433 dev_dbg(device, "direct-loading %s\n", buf->fw_id);
435 fw_state_done(&buf->fw_st);
443 /* firmware holds the ownership of pages */
444 static void firmware_free_data(const struct firmware *fw)
446 /* Loaded directly? */
451 fw_free_buf(fw->priv);
454 /* store the pages buffer info firmware from buf */
455 static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
458 #ifdef CONFIG_FW_LOADER_USER_HELPER
459 fw->pages = buf->pages;
461 fw->size = buf->size;
462 fw->data = buf->data;
464 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
465 __func__, buf->fw_id, buf, buf->data,
466 (unsigned int)buf->size);
469 #ifdef CONFIG_PM_SLEEP
470 static void fw_name_devm_release(struct device *dev, void *res)
472 struct fw_name_devm *fwn = res;
474 if (fwn->magic == (unsigned long)&fw_cache)
475 pr_debug("%s: fw_name-%s devm-%p released\n",
476 __func__, fwn->name, res);
477 kfree_const(fwn->name);
480 static int fw_devm_match(struct device *dev, void *res,
483 struct fw_name_devm *fwn = res;
485 return (fwn->magic == (unsigned long)&fw_cache) &&
486 !strcmp(fwn->name, match_data);
489 static struct fw_name_devm *fw_find_devm_name(struct device *dev,
492 struct fw_name_devm *fwn;
494 fwn = devres_find(dev, fw_name_devm_release,
495 fw_devm_match, (void *)name);
499 /* add firmware name into devres list */
500 static int fw_add_devm_name(struct device *dev, const char *name)
502 struct fw_name_devm *fwn;
504 fwn = fw_find_devm_name(dev, name);
508 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
512 fwn->name = kstrdup_const(name, GFP_KERNEL);
518 fwn->magic = (unsigned long)&fw_cache;
519 devres_add(dev, fwn);
524 static int fw_add_devm_name(struct device *dev, const char *name)
530 static int assign_firmware_buf(struct firmware *fw, struct device *device,
531 unsigned int opt_flags)
533 struct firmware_buf *buf = fw->priv;
535 mutex_lock(&fw_lock);
536 if (!buf->size || fw_state_is_aborted(&buf->fw_st)) {
537 mutex_unlock(&fw_lock);
542 * add firmware name into devres list so that we can auto cache
543 * and uncache firmware for device.
545 * device may has been deleted already, but the problem
546 * should be fixed in devres or driver core.
548 /* don't cache firmware handled without uevent */
549 if (device && (opt_flags & FW_OPT_UEVENT) &&
550 !(opt_flags & FW_OPT_NOCACHE))
551 fw_add_devm_name(device, buf->fw_id);
554 * After caching firmware image is started, let it piggyback
555 * on request firmware.
557 if (!(opt_flags & FW_OPT_NOCACHE) &&
558 buf->fwc->state == FW_LOADER_START_CACHE) {
559 if (fw_cache_piggyback_on_request(buf->fw_id))
563 /* pass the pages buffer to driver at the last minute */
564 fw_set_page_data(buf, fw);
565 mutex_unlock(&fw_lock);
570 * user-mode helper code
572 #ifdef CONFIG_FW_LOADER_USER_HELPER
573 struct firmware_priv {
576 struct firmware_buf *buf;
580 static struct firmware_priv *to_firmware_priv(struct device *dev)
582 return container_of(dev, struct firmware_priv, dev);
585 static void __fw_load_abort(struct firmware_buf *buf)
588 * There is a small window in which user can write to 'loading'
589 * between loading done and disappearance of 'loading'
591 if (fw_state_is_done(&buf->fw_st))
594 list_del_init(&buf->pending_list);
595 fw_state_aborted(&buf->fw_st);
598 static void fw_load_abort(struct firmware_priv *fw_priv)
600 struct firmware_buf *buf = fw_priv->buf;
602 __fw_load_abort(buf);
605 static LIST_HEAD(pending_fw_head);
607 static void kill_pending_fw_fallback_reqs(bool only_kill_custom)
609 struct firmware_buf *buf;
610 struct firmware_buf *next;
612 mutex_lock(&fw_lock);
613 list_for_each_entry_safe(buf, next, &pending_fw_head, pending_list) {
614 if (!buf->need_uevent || !only_kill_custom)
615 __fw_load_abort(buf);
617 mutex_unlock(&fw_lock);
620 static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
623 return sprintf(buf, "%d\n", loading_timeout);
627 * firmware_timeout_store - set number of seconds to wait for firmware
628 * @class: device class pointer
629 * @attr: device attribute pointer
630 * @buf: buffer to scan for timeout value
631 * @count: number of bytes in @buf
633 * Sets the number of seconds to wait for the firmware. Once
634 * this expires an error will be returned to the driver and no
635 * firmware will be provided.
637 * Note: zero means 'wait forever'.
639 static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
640 const char *buf, size_t count)
642 loading_timeout = simple_strtol(buf, NULL, 10);
643 if (loading_timeout < 0)
648 static CLASS_ATTR_RW(timeout);
650 static struct attribute *firmware_class_attrs[] = {
651 &class_attr_timeout.attr,
654 ATTRIBUTE_GROUPS(firmware_class);
656 static void fw_dev_release(struct device *dev)
658 struct firmware_priv *fw_priv = to_firmware_priv(dev);
663 static int do_firmware_uevent(struct firmware_priv *fw_priv, struct kobj_uevent_env *env)
665 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
667 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
669 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
675 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
677 struct firmware_priv *fw_priv = to_firmware_priv(dev);
680 mutex_lock(&fw_lock);
682 err = do_firmware_uevent(fw_priv, env);
683 mutex_unlock(&fw_lock);
687 static struct class firmware_class = {
689 .class_groups = firmware_class_groups,
690 .dev_uevent = firmware_uevent,
691 .dev_release = fw_dev_release,
694 static ssize_t firmware_loading_show(struct device *dev,
695 struct device_attribute *attr, char *buf)
697 struct firmware_priv *fw_priv = to_firmware_priv(dev);
700 mutex_lock(&fw_lock);
702 loading = fw_state_is_loading(&fw_priv->buf->fw_st);
703 mutex_unlock(&fw_lock);
705 return sprintf(buf, "%d\n", loading);
708 /* Some architectures don't have PAGE_KERNEL_RO */
709 #ifndef PAGE_KERNEL_RO
710 #define PAGE_KERNEL_RO PAGE_KERNEL
713 /* one pages buffer should be mapped/unmapped only once */
714 static int fw_map_pages_buf(struct firmware_buf *buf)
716 if (!buf->is_paged_buf)
720 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
727 * firmware_loading_store - set value in the 'loading' control file
728 * @dev: device pointer
729 * @attr: device attribute pointer
730 * @buf: buffer to scan for loading control value
731 * @count: number of bytes in @buf
733 * The relevant values are:
735 * 1: Start a load, discarding any previous partial load.
736 * 0: Conclude the load and hand the data to the driver code.
737 * -1: Conclude the load with an error and discard any written data.
739 static ssize_t firmware_loading_store(struct device *dev,
740 struct device_attribute *attr,
741 const char *buf, size_t count)
743 struct firmware_priv *fw_priv = to_firmware_priv(dev);
744 struct firmware_buf *fw_buf;
745 ssize_t written = count;
746 int loading = simple_strtol(buf, NULL, 10);
749 mutex_lock(&fw_lock);
750 fw_buf = fw_priv->buf;
751 if (fw_state_is_aborted(&fw_buf->fw_st))
756 /* discarding any previous partial load */
757 if (!fw_state_is_done(&fw_buf->fw_st)) {
758 for (i = 0; i < fw_buf->nr_pages; i++)
759 __free_page(fw_buf->pages[i]);
760 vfree(fw_buf->pages);
761 fw_buf->pages = NULL;
762 fw_buf->page_array_size = 0;
763 fw_buf->nr_pages = 0;
764 fw_state_start(&fw_buf->fw_st);
768 if (fw_state_is_loading(&fw_buf->fw_st)) {
772 * Several loading requests may be pending on
773 * one same firmware buf, so let all requests
774 * see the mapped 'buf->data' once the loading
777 rc = fw_map_pages_buf(fw_buf);
779 dev_err(dev, "%s: map pages failed\n",
782 rc = security_kernel_post_read_file(NULL,
783 fw_buf->data, fw_buf->size,
787 * Same logic as fw_load_abort, only the DONE bit
788 * is ignored and we set ABORT only on failure.
790 list_del_init(&fw_buf->pending_list);
792 fw_state_aborted(&fw_buf->fw_st);
795 fw_state_done(&fw_buf->fw_st);
801 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
804 fw_load_abort(fw_priv);
808 mutex_unlock(&fw_lock);
812 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
814 static void firmware_rw_buf(struct firmware_buf *buf, char *buffer,
815 loff_t offset, size_t count, bool read)
818 memcpy(buffer, buf->data + offset, count);
820 memcpy(buf->data + offset, buffer, count);
823 static void firmware_rw(struct firmware_buf *buf, char *buffer,
824 loff_t offset, size_t count, bool read)
828 int page_nr = offset >> PAGE_SHIFT;
829 int page_ofs = offset & (PAGE_SIZE-1);
830 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
832 page_data = kmap(buf->pages[page_nr]);
835 memcpy(buffer, page_data + page_ofs, page_cnt);
837 memcpy(page_data + page_ofs, buffer, page_cnt);
839 kunmap(buf->pages[page_nr]);
846 static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
847 struct bin_attribute *bin_attr,
848 char *buffer, loff_t offset, size_t count)
850 struct device *dev = kobj_to_dev(kobj);
851 struct firmware_priv *fw_priv = to_firmware_priv(dev);
852 struct firmware_buf *buf;
855 mutex_lock(&fw_lock);
857 if (!buf || fw_state_is_done(&buf->fw_st)) {
861 if (offset > buf->size) {
865 if (count > buf->size - offset)
866 count = buf->size - offset;
871 firmware_rw_buf(buf, buffer, offset, count, true);
873 firmware_rw(buf, buffer, offset, count, true);
876 mutex_unlock(&fw_lock);
880 static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
882 struct firmware_buf *buf = fw_priv->buf;
883 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
885 /* If the array of pages is too small, grow it... */
886 if (buf->page_array_size < pages_needed) {
887 int new_array_size = max(pages_needed,
888 buf->page_array_size * 2);
889 struct page **new_pages;
891 new_pages = vmalloc(new_array_size * sizeof(void *));
893 fw_load_abort(fw_priv);
896 memcpy(new_pages, buf->pages,
897 buf->page_array_size * sizeof(void *));
898 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
899 (new_array_size - buf->page_array_size));
901 buf->pages = new_pages;
902 buf->page_array_size = new_array_size;
905 while (buf->nr_pages < pages_needed) {
906 buf->pages[buf->nr_pages] =
907 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
909 if (!buf->pages[buf->nr_pages]) {
910 fw_load_abort(fw_priv);
919 * firmware_data_write - write method for firmware
920 * @filp: open sysfs file
921 * @kobj: kobject for the device
922 * @bin_attr: bin_attr structure
923 * @buffer: buffer being written
924 * @offset: buffer offset for write in total data store area
925 * @count: buffer size
927 * Data written to the 'data' attribute will be later handed to
928 * the driver as a firmware image.
930 static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
931 struct bin_attribute *bin_attr,
932 char *buffer, loff_t offset, size_t count)
934 struct device *dev = kobj_to_dev(kobj);
935 struct firmware_priv *fw_priv = to_firmware_priv(dev);
936 struct firmware_buf *buf;
939 if (!capable(CAP_SYS_RAWIO))
942 mutex_lock(&fw_lock);
944 if (!buf || fw_state_is_done(&buf->fw_st)) {
950 if (offset + count > buf->allocated_size) {
954 firmware_rw_buf(buf, buffer, offset, count, false);
957 retval = fw_realloc_buffer(fw_priv, offset + count);
962 firmware_rw(buf, buffer, offset, count, false);
965 buf->size = max_t(size_t, offset + count, buf->size);
967 mutex_unlock(&fw_lock);
971 static struct bin_attribute firmware_attr_data = {
972 .attr = { .name = "data", .mode = 0644 },
974 .read = firmware_data_read,
975 .write = firmware_data_write,
978 static struct attribute *fw_dev_attrs[] = {
979 &dev_attr_loading.attr,
983 static struct bin_attribute *fw_dev_bin_attrs[] = {
988 static const struct attribute_group fw_dev_attr_group = {
989 .attrs = fw_dev_attrs,
990 .bin_attrs = fw_dev_bin_attrs,
993 static const struct attribute_group *fw_dev_attr_groups[] = {
998 static struct firmware_priv *
999 fw_create_instance(struct firmware *firmware, const char *fw_name,
1000 struct device *device, unsigned int opt_flags)
1002 struct firmware_priv *fw_priv;
1003 struct device *f_dev;
1005 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
1007 fw_priv = ERR_PTR(-ENOMEM);
1011 fw_priv->nowait = !!(opt_flags & FW_OPT_NOWAIT);
1012 fw_priv->fw = firmware;
1013 f_dev = &fw_priv->dev;
1015 device_initialize(f_dev);
1016 dev_set_name(f_dev, "%s", fw_name);
1017 f_dev->parent = device;
1018 f_dev->class = &firmware_class;
1019 f_dev->groups = fw_dev_attr_groups;
1024 /* load a firmware via user helper */
1025 static int _request_firmware_load(struct firmware_priv *fw_priv,
1026 unsigned int opt_flags, long timeout)
1029 struct device *f_dev = &fw_priv->dev;
1030 struct firmware_buf *buf = fw_priv->buf;
1032 /* fall back on userspace loading */
1034 buf->is_paged_buf = true;
1036 dev_set_uevent_suppress(f_dev, true);
1038 retval = device_add(f_dev);
1040 dev_err(f_dev, "%s: device_register failed\n", __func__);
1044 mutex_lock(&fw_lock);
1045 list_add(&buf->pending_list, &pending_fw_head);
1046 mutex_unlock(&fw_lock);
1048 if (opt_flags & FW_OPT_UEVENT) {
1049 buf->need_uevent = true;
1050 dev_set_uevent_suppress(f_dev, false);
1051 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
1052 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
1054 timeout = MAX_JIFFY_OFFSET;
1057 retval = fw_state_wait_timeout(&buf->fw_st, timeout);
1059 mutex_lock(&fw_lock);
1060 fw_load_abort(fw_priv);
1061 mutex_unlock(&fw_lock);
1064 if (fw_state_is_aborted(&buf->fw_st)) {
1065 if (retval == -ERESTARTSYS)
1069 } else if (buf->is_paged_buf && !buf->data)
1078 static int fw_load_from_user_helper(struct firmware *firmware,
1079 const char *name, struct device *device,
1080 unsigned int opt_flags)
1082 struct firmware_priv *fw_priv;
1086 timeout = firmware_loading_timeout();
1087 if (opt_flags & FW_OPT_NOWAIT) {
1088 timeout = usermodehelper_read_lock_wait(timeout);
1090 dev_dbg(device, "firmware: %s loading timed out\n",
1095 ret = usermodehelper_read_trylock();
1097 dev_err(device, "firmware: %s will not be loaded\n",
1103 fw_priv = fw_create_instance(firmware, name, device, opt_flags);
1104 if (IS_ERR(fw_priv)) {
1105 ret = PTR_ERR(fw_priv);
1109 fw_priv->buf = firmware->priv;
1110 ret = _request_firmware_load(fw_priv, opt_flags, timeout);
1113 ret = assign_firmware_buf(firmware, device, opt_flags);
1116 usermodehelper_read_unlock();
1121 #else /* CONFIG_FW_LOADER_USER_HELPER */
1123 fw_load_from_user_helper(struct firmware *firmware, const char *name,
1124 struct device *device, unsigned int opt_flags)
1129 static inline void kill_pending_fw_fallback_reqs(bool only_kill_custom) { }
1131 #endif /* CONFIG_FW_LOADER_USER_HELPER */
1133 /* prepare firmware and firmware_buf structs;
1134 * return 0 if a firmware is already assigned, 1 if need to load one,
1135 * or a negative error code
1138 _request_firmware_prepare(struct firmware **firmware_p, const char *name,
1139 struct device *device, void *dbuf, size_t size)
1141 struct firmware *firmware;
1142 struct firmware_buf *buf;
1145 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
1147 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
1152 if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
1153 dev_dbg(device, "using built-in %s\n", name);
1154 return 0; /* assigned */
1157 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf, dbuf, size);
1160 * bind with 'buf' now to avoid warning in failure path
1161 * of requesting firmware.
1163 firmware->priv = buf;
1166 ret = fw_state_wait(&buf->fw_st);
1168 fw_set_page_data(buf, firmware);
1169 return 0; /* assigned */
1175 return 1; /* need to load */
1179 * Batched requests need only one wake, we need to do this step last due to the
1180 * fallback mechanism. The buf is protected with kref_get(), and it won't be
1181 * released until the last user calls release_firmware().
1183 * Failed batched requests are possible as well, in such cases we just share
1184 * the struct firmware_buf and won't release it until all requests are woken
1185 * and have gone through this same path.
1187 static void fw_abort_batch_reqs(struct firmware *fw)
1189 struct firmware_buf *buf;
1191 /* Loaded directly? */
1192 if (!fw || !fw->priv)
1196 if (!fw_state_is_aborted(&buf->fw_st))
1197 fw_state_aborted(&buf->fw_st);
1200 /* called from request_firmware() and request_firmware_work_func() */
1202 _request_firmware(const struct firmware **firmware_p, const char *name,
1203 struct device *device, void *buf, size_t size,
1204 unsigned int opt_flags)
1206 struct firmware *fw = NULL;
1212 if (!name || name[0] == '\0') {
1217 ret = _request_firmware_prepare(&fw, name, device, buf, size);
1218 if (ret <= 0) /* error or already assigned */
1221 ret = fw_get_filesystem_firmware(device, fw->priv);
1223 if (!(opt_flags & FW_OPT_NO_WARN))
1225 "Direct firmware load for %s failed with error %d\n",
1227 if (opt_flags & FW_OPT_USERHELPER) {
1228 dev_warn(device, "Falling back to user helper\n");
1229 ret = fw_load_from_user_helper(fw, name, device,
1233 ret = assign_firmware_buf(fw, device, opt_flags);
1237 fw_abort_batch_reqs(fw);
1238 release_firmware(fw);
1247 * request_firmware: - send firmware request and wait for it
1248 * @firmware_p: pointer to firmware image
1249 * @name: name of firmware file
1250 * @device: device for which firmware is being loaded
1252 * @firmware_p will be used to return a firmware image by the name
1253 * of @name for device @device.
1255 * Should be called from user context where sleeping is allowed.
1257 * @name will be used as $FIRMWARE in the uevent environment and
1258 * should be distinctive enough not to be confused with any other
1259 * firmware image for this or any other device.
1261 * Caller must hold the reference count of @device.
1263 * The function can be called safely inside device's suspend and
1267 request_firmware(const struct firmware **firmware_p, const char *name,
1268 struct device *device)
1272 /* Need to pin this module until return */
1273 __module_get(THIS_MODULE);
1274 ret = _request_firmware(firmware_p, name, device, NULL, 0,
1275 FW_OPT_UEVENT | FW_OPT_FALLBACK);
1276 module_put(THIS_MODULE);
1279 EXPORT_SYMBOL(request_firmware);
1282 * request_firmware_direct: - load firmware directly without usermode helper
1283 * @firmware_p: pointer to firmware image
1284 * @name: name of firmware file
1285 * @device: device for which firmware is being loaded
1287 * This function works pretty much like request_firmware(), but this doesn't
1288 * fall back to usermode helper even if the firmware couldn't be loaded
1289 * directly from fs. Hence it's useful for loading optional firmwares, which
1290 * aren't always present, without extra long timeouts of udev.
1292 int request_firmware_direct(const struct firmware **firmware_p,
1293 const char *name, struct device *device)
1297 __module_get(THIS_MODULE);
1298 ret = _request_firmware(firmware_p, name, device, NULL, 0,
1299 FW_OPT_UEVENT | FW_OPT_NO_WARN);
1300 module_put(THIS_MODULE);
1303 EXPORT_SYMBOL_GPL(request_firmware_direct);
1306 * request_firmware_into_buf - load firmware into a previously allocated buffer
1307 * @firmware_p: pointer to firmware image
1308 * @name: name of firmware file
1309 * @device: device for which firmware is being loaded and DMA region allocated
1310 * @buf: address of buffer to load firmware into
1311 * @size: size of buffer
1313 * This function works pretty much like request_firmware(), but it doesn't
1314 * allocate a buffer to hold the firmware data. Instead, the firmware
1315 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
1316 * data member is pointed at @buf.
1318 * This function doesn't cache firmware either.
1321 request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
1322 struct device *device, void *buf, size_t size)
1326 __module_get(THIS_MODULE);
1327 ret = _request_firmware(firmware_p, name, device, buf, size,
1328 FW_OPT_UEVENT | FW_OPT_FALLBACK |
1330 module_put(THIS_MODULE);
1333 EXPORT_SYMBOL(request_firmware_into_buf);
1336 * release_firmware: - release the resource associated with a firmware image
1337 * @fw: firmware resource to release
1339 void release_firmware(const struct firmware *fw)
1342 if (!fw_is_builtin_firmware(fw))
1343 firmware_free_data(fw);
1347 EXPORT_SYMBOL(release_firmware);
1350 struct firmware_work {
1351 struct work_struct work;
1352 struct module *module;
1354 struct device *device;
1356 void (*cont)(const struct firmware *fw, void *context);
1357 unsigned int opt_flags;
1360 static void request_firmware_work_func(struct work_struct *work)
1362 struct firmware_work *fw_work;
1363 const struct firmware *fw;
1365 fw_work = container_of(work, struct firmware_work, work);
1367 _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
1368 fw_work->opt_flags);
1369 fw_work->cont(fw, fw_work->context);
1370 put_device(fw_work->device); /* taken in request_firmware_nowait() */
1372 module_put(fw_work->module);
1373 kfree_const(fw_work->name);
1378 * request_firmware_nowait - asynchronous version of request_firmware
1379 * @module: module requesting the firmware
1380 * @uevent: sends uevent to copy the firmware image if this flag
1381 * is non-zero else the firmware copy must be done manually.
1382 * @name: name of firmware file
1383 * @device: device for which firmware is being loaded
1384 * @gfp: allocation flags
1385 * @context: will be passed over to @cont, and
1386 * @fw may be %NULL if firmware request fails.
1387 * @cont: function will be called asynchronously when the firmware
1390 * Caller must hold the reference count of @device.
1392 * Asynchronous variant of request_firmware() for user contexts:
1393 * - sleep for as small periods as possible since it may
1394 * increase kernel boot time of built-in device drivers
1395 * requesting firmware in their ->probe() methods, if
1396 * @gfp is GFP_KERNEL.
1398 * - can't sleep at all if @gfp is GFP_ATOMIC.
1401 request_firmware_nowait(
1402 struct module *module, bool uevent,
1403 const char *name, struct device *device, gfp_t gfp, void *context,
1404 void (*cont)(const struct firmware *fw, void *context))
1406 struct firmware_work *fw_work;
1409 return -EPROBE_DEFER;
1411 fw_work = kzalloc(sizeof(struct firmware_work), gfp);
1415 fw_work->module = module;
1416 fw_work->name = kstrdup_const(name, gfp);
1417 if (!fw_work->name) {
1421 fw_work->device = device;
1422 fw_work->context = context;
1423 fw_work->cont = cont;
1424 fw_work->opt_flags = FW_OPT_NOWAIT | FW_OPT_FALLBACK |
1425 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
1427 if (!try_module_get(module)) {
1428 kfree_const(fw_work->name);
1433 get_device(fw_work->device);
1434 INIT_WORK(&fw_work->work, request_firmware_work_func);
1435 schedule_work(&fw_work->work);
1438 EXPORT_SYMBOL(request_firmware_nowait);
1440 #ifdef CONFIG_PM_SLEEP
1441 static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1444 * cache_firmware - cache one firmware image in kernel memory space
1445 * @fw_name: the firmware image name
1447 * Cache firmware in kernel memory so that drivers can use it when
1448 * system isn't ready for them to request firmware image from userspace.
1449 * Once it returns successfully, driver can use request_firmware or its
1450 * nowait version to get the cached firmware without any interacting
1453 * Return 0 if the firmware image has been cached successfully
1454 * Return !0 otherwise
1457 static int cache_firmware(const char *fw_name)
1460 const struct firmware *fw;
1462 pr_debug("%s: %s\n", __func__, fw_name);
1464 ret = request_firmware(&fw, fw_name, NULL);
1468 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1473 static struct firmware_buf *fw_lookup_buf(const char *fw_name)
1475 struct firmware_buf *tmp;
1476 struct firmware_cache *fwc = &fw_cache;
1478 spin_lock(&fwc->lock);
1479 tmp = __fw_lookup_buf(fw_name);
1480 spin_unlock(&fwc->lock);
1486 * uncache_firmware - remove one cached firmware image
1487 * @fw_name: the firmware image name
1489 * Uncache one firmware image which has been cached successfully
1492 * Return 0 if the firmware cache has been removed successfully
1493 * Return !0 otherwise
1496 static int uncache_firmware(const char *fw_name)
1498 struct firmware_buf *buf;
1501 pr_debug("%s: %s\n", __func__, fw_name);
1503 if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
1506 buf = fw_lookup_buf(fw_name);
1515 static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1517 struct fw_cache_entry *fce;
1519 fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
1523 fce->name = kstrdup_const(name, GFP_ATOMIC);
1533 static int __fw_entry_found(const char *name)
1535 struct firmware_cache *fwc = &fw_cache;
1536 struct fw_cache_entry *fce;
1538 list_for_each_entry(fce, &fwc->fw_names, list) {
1539 if (!strcmp(fce->name, name))
1545 static int fw_cache_piggyback_on_request(const char *name)
1547 struct firmware_cache *fwc = &fw_cache;
1548 struct fw_cache_entry *fce;
1551 spin_lock(&fwc->name_lock);
1552 if (__fw_entry_found(name))
1555 fce = alloc_fw_cache_entry(name);
1558 list_add(&fce->list, &fwc->fw_names);
1559 pr_debug("%s: fw: %s\n", __func__, name);
1562 spin_unlock(&fwc->name_lock);
1566 static void free_fw_cache_entry(struct fw_cache_entry *fce)
1568 kfree_const(fce->name);
1572 static void __async_dev_cache_fw_image(void *fw_entry,
1573 async_cookie_t cookie)
1575 struct fw_cache_entry *fce = fw_entry;
1576 struct firmware_cache *fwc = &fw_cache;
1579 ret = cache_firmware(fce->name);
1581 spin_lock(&fwc->name_lock);
1582 list_del(&fce->list);
1583 spin_unlock(&fwc->name_lock);
1585 free_fw_cache_entry(fce);
1589 /* called with dev->devres_lock held */
1590 static void dev_create_fw_entry(struct device *dev, void *res,
1593 struct fw_name_devm *fwn = res;
1594 const char *fw_name = fwn->name;
1595 struct list_head *head = data;
1596 struct fw_cache_entry *fce;
1598 fce = alloc_fw_cache_entry(fw_name);
1600 list_add(&fce->list, head);
1603 static int devm_name_match(struct device *dev, void *res,
1606 struct fw_name_devm *fwn = res;
1607 return (fwn->magic == (unsigned long)match_data);
1610 static void dev_cache_fw_image(struct device *dev, void *data)
1613 struct fw_cache_entry *fce;
1614 struct fw_cache_entry *fce_next;
1615 struct firmware_cache *fwc = &fw_cache;
1617 devres_for_each_res(dev, fw_name_devm_release,
1618 devm_name_match, &fw_cache,
1619 dev_create_fw_entry, &todo);
1621 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1622 list_del(&fce->list);
1624 spin_lock(&fwc->name_lock);
1625 /* only one cache entry for one firmware */
1626 if (!__fw_entry_found(fce->name)) {
1627 list_add(&fce->list, &fwc->fw_names);
1629 free_fw_cache_entry(fce);
1632 spin_unlock(&fwc->name_lock);
1635 async_schedule_domain(__async_dev_cache_fw_image,
1641 static void __device_uncache_fw_images(void)
1643 struct firmware_cache *fwc = &fw_cache;
1644 struct fw_cache_entry *fce;
1646 spin_lock(&fwc->name_lock);
1647 while (!list_empty(&fwc->fw_names)) {
1648 fce = list_entry(fwc->fw_names.next,
1649 struct fw_cache_entry, list);
1650 list_del(&fce->list);
1651 spin_unlock(&fwc->name_lock);
1653 uncache_firmware(fce->name);
1654 free_fw_cache_entry(fce);
1656 spin_lock(&fwc->name_lock);
1658 spin_unlock(&fwc->name_lock);
1662 * device_cache_fw_images - cache devices' firmware
1664 * If one device called request_firmware or its nowait version
1665 * successfully before, the firmware names are recored into the
1666 * device's devres link list, so device_cache_fw_images can call
1667 * cache_firmware() to cache these firmwares for the device,
1668 * then the device driver can load its firmwares easily at
1669 * time when system is not ready to complete loading firmware.
1671 static void device_cache_fw_images(void)
1673 struct firmware_cache *fwc = &fw_cache;
1677 pr_debug("%s\n", __func__);
1679 /* cancel uncache work */
1680 cancel_delayed_work_sync(&fwc->work);
1683 * use small loading timeout for caching devices' firmware
1684 * because all these firmware images have been loaded
1685 * successfully at lease once, also system is ready for
1686 * completing firmware loading now. The maximum size of
1687 * firmware in current distributions is about 2M bytes,
1688 * so 10 secs should be enough.
1690 old_timeout = loading_timeout;
1691 loading_timeout = 10;
1693 mutex_lock(&fw_lock);
1694 fwc->state = FW_LOADER_START_CACHE;
1695 dpm_for_each_dev(NULL, dev_cache_fw_image);
1696 mutex_unlock(&fw_lock);
1698 /* wait for completion of caching firmware for all devices */
1699 async_synchronize_full_domain(&fw_cache_domain);
1701 loading_timeout = old_timeout;
1705 * device_uncache_fw_images - uncache devices' firmware
1707 * uncache all firmwares which have been cached successfully
1708 * by device_uncache_fw_images earlier
1710 static void device_uncache_fw_images(void)
1712 pr_debug("%s\n", __func__);
1713 __device_uncache_fw_images();
1716 static void device_uncache_fw_images_work(struct work_struct *work)
1718 device_uncache_fw_images();
1722 * device_uncache_fw_images_delay - uncache devices firmwares
1723 * @delay: number of milliseconds to delay uncache device firmwares
1725 * uncache all devices's firmwares which has been cached successfully
1726 * by device_cache_fw_images after @delay milliseconds.
1728 static void device_uncache_fw_images_delay(unsigned long delay)
1730 queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1731 msecs_to_jiffies(delay));
1734 static int fw_pm_notify(struct notifier_block *notify_block,
1735 unsigned long mode, void *unused)
1738 case PM_HIBERNATION_PREPARE:
1739 case PM_SUSPEND_PREPARE:
1740 case PM_RESTORE_PREPARE:
1742 * kill pending fallback requests with a custom fallback
1743 * to avoid stalling suspend.
1745 kill_pending_fw_fallback_reqs(true);
1746 device_cache_fw_images();
1749 case PM_POST_SUSPEND:
1750 case PM_POST_HIBERNATION:
1751 case PM_POST_RESTORE:
1753 * In case that system sleep failed and syscore_suspend is
1756 mutex_lock(&fw_lock);
1757 fw_cache.state = FW_LOADER_NO_CACHE;
1758 mutex_unlock(&fw_lock);
1760 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1767 /* stop caching firmware once syscore_suspend is reached */
1768 static int fw_suspend(void)
1770 fw_cache.state = FW_LOADER_NO_CACHE;
1774 static struct syscore_ops fw_syscore_ops = {
1775 .suspend = fw_suspend,
1778 static int fw_cache_piggyback_on_request(const char *name)
1784 static void __init fw_cache_init(void)
1786 spin_lock_init(&fw_cache.lock);
1787 INIT_LIST_HEAD(&fw_cache.head);
1788 fw_cache.state = FW_LOADER_NO_CACHE;
1790 #ifdef CONFIG_PM_SLEEP
1791 spin_lock_init(&fw_cache.name_lock);
1792 INIT_LIST_HEAD(&fw_cache.fw_names);
1794 INIT_DELAYED_WORK(&fw_cache.work,
1795 device_uncache_fw_images_work);
1797 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1798 register_pm_notifier(&fw_cache.pm_notify);
1800 register_syscore_ops(&fw_syscore_ops);
1804 static int fw_shutdown_notify(struct notifier_block *unused1,
1805 unsigned long unused2, void *unused3)
1808 * Kill all pending fallback requests to avoid both stalling shutdown,
1809 * and avoid a deadlock with the usermode_lock.
1811 kill_pending_fw_fallback_reqs(false);
1816 static struct notifier_block fw_shutdown_nb = {
1817 .notifier_call = fw_shutdown_notify,
1820 static int __init firmware_class_init(void)
1823 register_reboot_notifier(&fw_shutdown_nb);
1824 #ifdef CONFIG_FW_LOADER_USER_HELPER
1825 return class_register(&firmware_class);
1831 static void __exit firmware_class_exit(void)
1833 #ifdef CONFIG_PM_SLEEP
1834 unregister_syscore_ops(&fw_syscore_ops);
1835 unregister_pm_notifier(&fw_cache.pm_notify);
1837 unregister_reboot_notifier(&fw_shutdown_nb);
1838 #ifdef CONFIG_FW_LOADER_USER_HELPER
1839 class_unregister(&firmware_class);
1843 fs_initcall(firmware_class_init);
1844 module_exit(firmware_class_exit);