2 * firmware_class.c - Multi purpose firmware loading support
4 * Copyright (c) 2003 Manuel Estrada Sainz
6 * Please see Documentation/firmware_class/ for more information.
10 #include <linux/capability.h>
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/timer.h>
15 #include <linux/vmalloc.h>
16 #include <linux/interrupt.h>
17 #include <linux/bitops.h>
18 #include <linux/mutex.h>
19 #include <linux/workqueue.h>
20 #include <linux/highmem.h>
21 #include <linux/firmware.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <linux/file.h>
25 #include <linux/list.h>
26 #include <linux/async.h>
28 #include <linux/suspend.h>
29 #include <linux/syscore_ops.h>
31 #include <generated/utsrelease.h>
35 MODULE_AUTHOR("Manuel Estrada Sainz");
36 MODULE_DESCRIPTION("Multi purpose firmware loading support");
37 MODULE_LICENSE("GPL");
39 /* Builtin firmware support */
41 #ifdef CONFIG_FW_LOADER
43 extern struct builtin_fw __start_builtin_fw[];
44 extern struct builtin_fw __end_builtin_fw[];
46 static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
48 struct builtin_fw *b_fw;
50 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
51 if (strcmp(name, b_fw->name) == 0) {
52 fw->size = b_fw->size;
53 fw->data = b_fw->data;
61 static bool fw_is_builtin_firmware(const struct firmware *fw)
63 struct builtin_fw *b_fw;
65 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
66 if (fw->data == b_fw->data)
72 #else /* Module case - no builtin firmware support */
74 static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
79 static inline bool fw_is_builtin_firmware(const struct firmware *fw)
92 VMALLOC_BUF, /* used in direct loading */
93 PAGE_BUF, /* used in loading via userspace */
96 static int loading_timeout = 60; /* In seconds */
98 static inline long firmware_loading_timeout(void)
100 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
103 struct firmware_cache {
104 /* firmware_buf instance will be added into the below list */
106 struct list_head head;
109 #ifdef CONFIG_PM_SLEEP
111 * Names of firmware images which have been cached successfully
112 * will be added into the below list so that device uncache
113 * helper can trace which firmware images have been cached
116 spinlock_t name_lock;
117 struct list_head fw_names;
119 wait_queue_head_t wait_queue;
121 struct delayed_work work;
123 struct notifier_block pm_notify;
127 struct firmware_buf {
129 struct list_head list;
130 struct completion completion;
131 struct firmware_cache *fwc;
132 unsigned long status;
142 struct fw_cache_entry {
143 struct list_head list;
147 struct firmware_priv {
148 struct timer_list timeout;
151 struct firmware_buf *buf;
155 struct fw_name_devm {
160 #define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
162 #define FW_LOADER_NO_CACHE 0
163 #define FW_LOADER_START_CACHE 1
165 static int fw_cache_piggyback_on_request(const char *name);
167 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
168 * guarding for corner cases a global lock should be OK */
169 static DEFINE_MUTEX(fw_lock);
171 static struct firmware_cache fw_cache;
173 static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
174 struct firmware_cache *fwc)
176 struct firmware_buf *buf;
178 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
183 kref_init(&buf->ref);
184 strcpy(buf->fw_id, fw_name);
186 init_completion(&buf->completion);
187 buf->fmt = VMALLOC_BUF;
189 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
194 static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
196 struct firmware_buf *tmp;
197 struct firmware_cache *fwc = &fw_cache;
199 list_for_each_entry(tmp, &fwc->head, list)
200 if (!strcmp(tmp->fw_id, fw_name))
205 static int fw_lookup_and_allocate_buf(const char *fw_name,
206 struct firmware_cache *fwc,
207 struct firmware_buf **buf)
209 struct firmware_buf *tmp;
211 spin_lock(&fwc->lock);
212 tmp = __fw_lookup_buf(fw_name);
215 spin_unlock(&fwc->lock);
219 tmp = __allocate_fw_buf(fw_name, fwc);
221 list_add(&tmp->list, &fwc->head);
222 spin_unlock(&fwc->lock);
226 return tmp ? 0 : -ENOMEM;
229 static struct firmware_buf *fw_lookup_buf(const char *fw_name)
231 struct firmware_buf *tmp;
232 struct firmware_cache *fwc = &fw_cache;
234 spin_lock(&fwc->lock);
235 tmp = __fw_lookup_buf(fw_name);
236 spin_unlock(&fwc->lock);
241 static void __fw_free_buf(struct kref *ref)
243 struct firmware_buf *buf = to_fwbuf(ref);
244 struct firmware_cache *fwc = buf->fwc;
247 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
248 __func__, buf->fw_id, buf, buf->data,
249 (unsigned int)buf->size);
251 spin_lock(&fwc->lock);
252 list_del(&buf->list);
253 spin_unlock(&fwc->lock);
256 if (buf->fmt == PAGE_BUF) {
258 for (i = 0; i < buf->nr_pages; i++)
259 __free_page(buf->pages[i]);
266 static void fw_free_buf(struct firmware_buf *buf)
268 kref_put(&buf->ref, __fw_free_buf);
271 /* direct firmware loading support */
272 static const char *fw_path[] = {
273 "/lib/firmware/updates/" UTS_RELEASE,
274 "/lib/firmware/updates",
275 "/lib/firmware/" UTS_RELEASE,
279 /* Don't inline this: 'struct kstat' is biggish */
280 static noinline long fw_file_size(struct file *file)
283 if (vfs_getattr(file->f_path.mnt, file->f_path.dentry, &st))
285 if (!S_ISREG(st.mode))
287 if (st.size != (long)st.size)
292 static bool fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
297 size = fw_file_size(file);
303 if (kernel_read(file, 0, buf, size) != size) {
312 static bool fw_get_filesystem_firmware(struct firmware_buf *buf)
315 bool success = false;
316 char *path = __getname();
318 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
320 snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id);
322 file = filp_open(path, O_RDONLY, 0);
325 success = fw_read_file_contents(file, buf);
334 static struct firmware_priv *to_firmware_priv(struct device *dev)
336 return container_of(dev, struct firmware_priv, dev);
339 static void fw_load_abort(struct firmware_priv *fw_priv)
341 struct firmware_buf *buf = fw_priv->buf;
343 set_bit(FW_STATUS_ABORT, &buf->status);
344 complete_all(&buf->completion);
347 static ssize_t firmware_timeout_show(struct class *class,
348 struct class_attribute *attr,
351 return sprintf(buf, "%d\n", loading_timeout);
355 * firmware_timeout_store - set number of seconds to wait for firmware
356 * @class: device class pointer
357 * @attr: device attribute pointer
358 * @buf: buffer to scan for timeout value
359 * @count: number of bytes in @buf
361 * Sets the number of seconds to wait for the firmware. Once
362 * this expires an error will be returned to the driver and no
363 * firmware will be provided.
365 * Note: zero means 'wait forever'.
367 static ssize_t firmware_timeout_store(struct class *class,
368 struct class_attribute *attr,
369 const char *buf, size_t count)
371 loading_timeout = simple_strtol(buf, NULL, 10);
372 if (loading_timeout < 0)
378 static struct class_attribute firmware_class_attrs[] = {
379 __ATTR(timeout, S_IWUSR | S_IRUGO,
380 firmware_timeout_show, firmware_timeout_store),
384 static void fw_dev_release(struct device *dev)
386 struct firmware_priv *fw_priv = to_firmware_priv(dev);
390 module_put(THIS_MODULE);
393 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
395 struct firmware_priv *fw_priv = to_firmware_priv(dev);
397 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
399 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
401 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
407 static struct class firmware_class = {
409 .class_attrs = firmware_class_attrs,
410 .dev_uevent = firmware_uevent,
411 .dev_release = fw_dev_release,
414 static ssize_t firmware_loading_show(struct device *dev,
415 struct device_attribute *attr, char *buf)
417 struct firmware_priv *fw_priv = to_firmware_priv(dev);
418 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
420 return sprintf(buf, "%d\n", loading);
423 /* firmware holds the ownership of pages */
424 static void firmware_free_data(const struct firmware *fw)
426 /* Loaded directly? */
431 fw_free_buf(fw->priv);
434 /* Some architectures don't have PAGE_KERNEL_RO */
435 #ifndef PAGE_KERNEL_RO
436 #define PAGE_KERNEL_RO PAGE_KERNEL
439 /* one pages buffer should be mapped/unmapped only once */
440 static int fw_map_pages_buf(struct firmware_buf *buf)
442 if (buf->fmt != PAGE_BUF)
447 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
454 * firmware_loading_store - set value in the 'loading' control file
455 * @dev: device pointer
456 * @attr: device attribute pointer
457 * @buf: buffer to scan for loading control value
458 * @count: number of bytes in @buf
460 * The relevant values are:
462 * 1: Start a load, discarding any previous partial load.
463 * 0: Conclude the load and hand the data to the driver code.
464 * -1: Conclude the load with an error and discard any written data.
466 static ssize_t firmware_loading_store(struct device *dev,
467 struct device_attribute *attr,
468 const char *buf, size_t count)
470 struct firmware_priv *fw_priv = to_firmware_priv(dev);
471 struct firmware_buf *fw_buf = fw_priv->buf;
472 int loading = simple_strtol(buf, NULL, 10);
475 mutex_lock(&fw_lock);
482 /* discarding any previous partial load */
483 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
484 for (i = 0; i < fw_buf->nr_pages; i++)
485 __free_page(fw_buf->pages[i]);
486 kfree(fw_buf->pages);
487 fw_buf->pages = NULL;
488 fw_buf->page_array_size = 0;
489 fw_buf->nr_pages = 0;
490 set_bit(FW_STATUS_LOADING, &fw_buf->status);
494 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
495 set_bit(FW_STATUS_DONE, &fw_buf->status);
496 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
499 * Several loading requests may be pending on
500 * one same firmware buf, so let all requests
501 * see the mapped 'buf->data' once the loading
504 fw_map_pages_buf(fw_buf);
505 complete_all(&fw_buf->completion);
510 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
513 fw_load_abort(fw_priv);
517 mutex_unlock(&fw_lock);
521 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
523 static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
524 struct bin_attribute *bin_attr,
525 char *buffer, loff_t offset, size_t count)
527 struct device *dev = kobj_to_dev(kobj);
528 struct firmware_priv *fw_priv = to_firmware_priv(dev);
529 struct firmware_buf *buf;
532 mutex_lock(&fw_lock);
534 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
538 if (offset > buf->size) {
542 if (count > buf->size - offset)
543 count = buf->size - offset;
549 int page_nr = offset >> PAGE_SHIFT;
550 int page_ofs = offset & (PAGE_SIZE-1);
551 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
553 page_data = kmap(buf->pages[page_nr]);
555 memcpy(buffer, page_data + page_ofs, page_cnt);
557 kunmap(buf->pages[page_nr]);
563 mutex_unlock(&fw_lock);
567 static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
569 struct firmware_buf *buf = fw_priv->buf;
570 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
572 /* If the array of pages is too small, grow it... */
573 if (buf->page_array_size < pages_needed) {
574 int new_array_size = max(pages_needed,
575 buf->page_array_size * 2);
576 struct page **new_pages;
578 new_pages = kmalloc(new_array_size * sizeof(void *),
581 fw_load_abort(fw_priv);
584 memcpy(new_pages, buf->pages,
585 buf->page_array_size * sizeof(void *));
586 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
587 (new_array_size - buf->page_array_size));
589 buf->pages = new_pages;
590 buf->page_array_size = new_array_size;
593 while (buf->nr_pages < pages_needed) {
594 buf->pages[buf->nr_pages] =
595 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
597 if (!buf->pages[buf->nr_pages]) {
598 fw_load_abort(fw_priv);
607 * firmware_data_write - write method for firmware
608 * @filp: open sysfs file
609 * @kobj: kobject for the device
610 * @bin_attr: bin_attr structure
611 * @buffer: buffer being written
612 * @offset: buffer offset for write in total data store area
613 * @count: buffer size
615 * Data written to the 'data' attribute will be later handed to
616 * the driver as a firmware image.
618 static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
619 struct bin_attribute *bin_attr,
620 char *buffer, loff_t offset, size_t count)
622 struct device *dev = kobj_to_dev(kobj);
623 struct firmware_priv *fw_priv = to_firmware_priv(dev);
624 struct firmware_buf *buf;
627 if (!capable(CAP_SYS_RAWIO))
630 mutex_lock(&fw_lock);
632 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
637 retval = fw_realloc_buffer(fw_priv, offset + count);
645 int page_nr = offset >> PAGE_SHIFT;
646 int page_ofs = offset & (PAGE_SIZE - 1);
647 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
649 page_data = kmap(buf->pages[page_nr]);
651 memcpy(page_data + page_ofs, buffer, page_cnt);
653 kunmap(buf->pages[page_nr]);
659 buf->size = max_t(size_t, offset, buf->size);
661 mutex_unlock(&fw_lock);
665 static struct bin_attribute firmware_attr_data = {
666 .attr = { .name = "data", .mode = 0644 },
668 .read = firmware_data_read,
669 .write = firmware_data_write,
672 static void firmware_class_timeout(u_long data)
674 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
676 fw_load_abort(fw_priv);
679 static struct firmware_priv *
680 fw_create_instance(struct firmware *firmware, const char *fw_name,
681 struct device *device, bool uevent, bool nowait)
683 struct firmware_priv *fw_priv;
684 struct device *f_dev;
686 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
688 dev_err(device, "%s: kmalloc failed\n", __func__);
689 fw_priv = ERR_PTR(-ENOMEM);
693 fw_priv->nowait = nowait;
694 fw_priv->fw = firmware;
695 setup_timer(&fw_priv->timeout,
696 firmware_class_timeout, (u_long) fw_priv);
698 f_dev = &fw_priv->dev;
700 device_initialize(f_dev);
701 dev_set_name(f_dev, "%s", fw_name);
702 f_dev->parent = device;
703 f_dev->class = &firmware_class;
708 /* store the pages buffer info firmware from buf */
709 static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
712 fw->pages = buf->pages;
713 fw->size = buf->size;
714 fw->data = buf->data;
716 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
717 __func__, buf->fw_id, buf, buf->data,
718 (unsigned int)buf->size);
721 #ifdef CONFIG_PM_SLEEP
722 static void fw_name_devm_release(struct device *dev, void *res)
724 struct fw_name_devm *fwn = res;
726 if (fwn->magic == (unsigned long)&fw_cache)
727 pr_debug("%s: fw_name-%s devm-%p released\n",
728 __func__, fwn->name, res);
731 static int fw_devm_match(struct device *dev, void *res,
734 struct fw_name_devm *fwn = res;
736 return (fwn->magic == (unsigned long)&fw_cache) &&
737 !strcmp(fwn->name, match_data);
740 static struct fw_name_devm *fw_find_devm_name(struct device *dev,
743 struct fw_name_devm *fwn;
745 fwn = devres_find(dev, fw_name_devm_release,
746 fw_devm_match, (void *)name);
750 /* add firmware name into devres list */
751 static int fw_add_devm_name(struct device *dev, const char *name)
753 struct fw_name_devm *fwn;
755 fwn = fw_find_devm_name(dev, name);
759 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
760 strlen(name) + 1, GFP_KERNEL);
764 fwn->magic = (unsigned long)&fw_cache;
765 strcpy(fwn->name, name);
766 devres_add(dev, fwn);
771 static int fw_add_devm_name(struct device *dev, const char *name)
777 static void _request_firmware_cleanup(const struct firmware **firmware_p)
779 release_firmware(*firmware_p);
783 static struct firmware_priv *
784 _request_firmware_prepare(const struct firmware **firmware_p, const char *name,
785 struct device *device, bool uevent, bool nowait)
787 struct firmware *firmware;
788 struct firmware_priv *fw_priv = NULL;
789 struct firmware_buf *buf;
793 return ERR_PTR(-EINVAL);
795 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
797 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
799 return ERR_PTR(-ENOMEM);
802 if (fw_get_builtin_firmware(firmware, name)) {
803 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
807 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
809 fw_priv = fw_create_instance(firmware, name, device,
812 if (IS_ERR(fw_priv) || ret < 0) {
815 return ERR_PTR(-ENOMEM);
816 } else if (fw_priv) {
820 * bind with 'buf' now to avoid warning in failure path
821 * of requesting firmware.
823 firmware->priv = buf;
827 /* share the cached buf, which is inprogessing or completed */
829 mutex_lock(&fw_lock);
830 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
831 fw_priv = ERR_PTR(-ENOENT);
832 firmware->priv = buf;
833 _request_firmware_cleanup(firmware_p);
835 } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
837 fw_set_page_data(buf, firmware);
840 mutex_unlock(&fw_lock);
841 wait_for_completion(&buf->completion);
845 mutex_unlock(&fw_lock);
849 static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
853 struct device *f_dev = &fw_priv->dev;
854 struct firmware_buf *buf = fw_priv->buf;
855 struct firmware_cache *fwc = &fw_cache;
858 /* try direct loading from fs first */
859 if (fw_get_filesystem_firmware(buf)) {
860 dev_dbg(f_dev->parent, "firmware: direct-loading"
861 " firmware %s\n", buf->fw_id);
863 set_bit(FW_STATUS_DONE, &buf->status);
864 complete_all(&buf->completion);
869 /* fall back on userspace loading */
872 dev_set_uevent_suppress(f_dev, true);
874 /* Need to pin this module until class device is destroyed */
875 __module_get(THIS_MODULE);
877 retval = device_add(f_dev);
879 dev_err(f_dev, "%s: device_register failed\n", __func__);
883 retval = device_create_bin_file(f_dev, &firmware_attr_data);
885 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
889 retval = device_create_file(f_dev, &dev_attr_loading);
891 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
892 goto err_del_bin_attr;
896 dev_set_uevent_suppress(f_dev, false);
897 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
898 if (timeout != MAX_SCHEDULE_TIMEOUT)
899 mod_timer(&fw_priv->timeout,
900 round_jiffies_up(jiffies + timeout));
902 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
905 wait_for_completion(&buf->completion);
907 del_timer_sync(&fw_priv->timeout);
910 mutex_lock(&fw_lock);
911 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
915 * add firmware name into devres list so that we can auto cache
916 * and uncache firmware for device.
918 * f_dev->parent may has been deleted already, but the problem
919 * should be fixed in devres or driver core.
921 if (!retval && f_dev->parent)
922 fw_add_devm_name(f_dev->parent, buf->fw_id);
925 * After caching firmware image is started, let it piggyback
926 * on request firmware.
928 if (!retval && fwc->state == FW_LOADER_START_CACHE) {
929 if (fw_cache_piggyback_on_request(buf->fw_id))
933 /* pass the pages buffer to driver at the last minute */
934 fw_set_page_data(buf, fw_priv->fw);
937 mutex_unlock(&fw_lock);
942 device_remove_file(f_dev, &dev_attr_loading);
944 device_remove_bin_file(f_dev, &firmware_attr_data);
953 * request_firmware: - send firmware request and wait for it
954 * @firmware_p: pointer to firmware image
955 * @name: name of firmware file
956 * @device: device for which firmware is being loaded
958 * @firmware_p will be used to return a firmware image by the name
959 * of @name for device @device.
961 * Should be called from user context where sleeping is allowed.
963 * @name will be used as $FIRMWARE in the uevent environment and
964 * should be distinctive enough not to be confused with any other
965 * firmware image for this or any other device.
967 * Caller must hold the reference count of @device.
970 request_firmware(const struct firmware **firmware_p, const char *name,
971 struct device *device)
973 struct firmware_priv *fw_priv;
976 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
978 if (IS_ERR_OR_NULL(fw_priv))
979 return PTR_RET(fw_priv);
981 ret = usermodehelper_read_trylock();
983 dev_err(device, "firmware: %s will not be loaded\n", name);
985 ret = _request_firmware_load(fw_priv, true,
986 firmware_loading_timeout());
987 usermodehelper_read_unlock();
990 _request_firmware_cleanup(firmware_p);
996 * release_firmware: - release the resource associated with a firmware image
997 * @fw: firmware resource to release
999 void release_firmware(const struct firmware *fw)
1002 if (!fw_is_builtin_firmware(fw))
1003 firmware_free_data(fw);
1009 struct firmware_work {
1010 struct work_struct work;
1011 struct module *module;
1013 struct device *device;
1015 void (*cont)(const struct firmware *fw, void *context);
1019 static void request_firmware_work_func(struct work_struct *work)
1021 struct firmware_work *fw_work;
1022 const struct firmware *fw;
1023 struct firmware_priv *fw_priv;
1027 fw_work = container_of(work, struct firmware_work, work);
1028 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
1029 fw_work->uevent, true);
1030 if (IS_ERR_OR_NULL(fw_priv)) {
1031 ret = PTR_RET(fw_priv);
1035 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
1037 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
1038 usermodehelper_read_unlock();
1040 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
1045 _request_firmware_cleanup(&fw);
1048 fw_work->cont(fw, fw_work->context);
1049 put_device(fw_work->device);
1051 module_put(fw_work->module);
1056 * request_firmware_nowait - asynchronous version of request_firmware
1057 * @module: module requesting the firmware
1058 * @uevent: sends uevent to copy the firmware image if this flag
1059 * is non-zero else the firmware copy must be done manually.
1060 * @name: name of firmware file
1061 * @device: device for which firmware is being loaded
1062 * @gfp: allocation flags
1063 * @context: will be passed over to @cont, and
1064 * @fw may be %NULL if firmware request fails.
1065 * @cont: function will be called asynchronously when the firmware
1068 * Caller must hold the reference count of @device.
1070 * Asynchronous variant of request_firmware() for user contexts:
1071 * - sleep for as small periods as possible since it may
1072 * increase kernel boot time of built-in device drivers
1073 * requesting firmware in their ->probe() methods, if
1074 * @gfp is GFP_KERNEL.
1076 * - can't sleep at all if @gfp is GFP_ATOMIC.
1079 request_firmware_nowait(
1080 struct module *module, bool uevent,
1081 const char *name, struct device *device, gfp_t gfp, void *context,
1082 void (*cont)(const struct firmware *fw, void *context))
1084 struct firmware_work *fw_work;
1086 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
1090 fw_work->module = module;
1091 fw_work->name = name;
1092 fw_work->device = device;
1093 fw_work->context = context;
1094 fw_work->cont = cont;
1095 fw_work->uevent = uevent;
1097 if (!try_module_get(module)) {
1102 get_device(fw_work->device);
1103 INIT_WORK(&fw_work->work, request_firmware_work_func);
1104 schedule_work(&fw_work->work);
1109 * cache_firmware - cache one firmware image in kernel memory space
1110 * @fw_name: the firmware image name
1112 * Cache firmware in kernel memory so that drivers can use it when
1113 * system isn't ready for them to request firmware image from userspace.
1114 * Once it returns successfully, driver can use request_firmware or its
1115 * nowait version to get the cached firmware without any interacting
1118 * Return 0 if the firmware image has been cached successfully
1119 * Return !0 otherwise
1122 int cache_firmware(const char *fw_name)
1125 const struct firmware *fw;
1127 pr_debug("%s: %s\n", __func__, fw_name);
1129 ret = request_firmware(&fw, fw_name, NULL);
1133 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1139 * uncache_firmware - remove one cached firmware image
1140 * @fw_name: the firmware image name
1142 * Uncache one firmware image which has been cached successfully
1145 * Return 0 if the firmware cache has been removed successfully
1146 * Return !0 otherwise
1149 int uncache_firmware(const char *fw_name)
1151 struct firmware_buf *buf;
1154 pr_debug("%s: %s\n", __func__, fw_name);
1156 if (fw_get_builtin_firmware(&fw, fw_name))
1159 buf = fw_lookup_buf(fw_name);
1168 #ifdef CONFIG_PM_SLEEP
1169 static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1171 struct fw_cache_entry *fce;
1173 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1177 strcpy(fce->name, name);
1182 static int __fw_entry_found(const char *name)
1184 struct firmware_cache *fwc = &fw_cache;
1185 struct fw_cache_entry *fce;
1187 list_for_each_entry(fce, &fwc->fw_names, list) {
1188 if (!strcmp(fce->name, name))
1194 static int fw_cache_piggyback_on_request(const char *name)
1196 struct firmware_cache *fwc = &fw_cache;
1197 struct fw_cache_entry *fce;
1200 spin_lock(&fwc->name_lock);
1201 if (__fw_entry_found(name))
1204 fce = alloc_fw_cache_entry(name);
1207 list_add(&fce->list, &fwc->fw_names);
1208 pr_debug("%s: fw: %s\n", __func__, name);
1211 spin_unlock(&fwc->name_lock);
1215 static void free_fw_cache_entry(struct fw_cache_entry *fce)
1220 static void __async_dev_cache_fw_image(void *fw_entry,
1221 async_cookie_t cookie)
1223 struct fw_cache_entry *fce = fw_entry;
1224 struct firmware_cache *fwc = &fw_cache;
1227 ret = cache_firmware(fce->name);
1229 spin_lock(&fwc->name_lock);
1230 list_del(&fce->list);
1231 spin_unlock(&fwc->name_lock);
1233 free_fw_cache_entry(fce);
1236 spin_lock(&fwc->name_lock);
1238 spin_unlock(&fwc->name_lock);
1240 wake_up(&fwc->wait_queue);
1243 /* called with dev->devres_lock held */
1244 static void dev_create_fw_entry(struct device *dev, void *res,
1247 struct fw_name_devm *fwn = res;
1248 const char *fw_name = fwn->name;
1249 struct list_head *head = data;
1250 struct fw_cache_entry *fce;
1252 fce = alloc_fw_cache_entry(fw_name);
1254 list_add(&fce->list, head);
1257 static int devm_name_match(struct device *dev, void *res,
1260 struct fw_name_devm *fwn = res;
1261 return (fwn->magic == (unsigned long)match_data);
1264 static void dev_cache_fw_image(struct device *dev, void *data)
1267 struct fw_cache_entry *fce;
1268 struct fw_cache_entry *fce_next;
1269 struct firmware_cache *fwc = &fw_cache;
1271 devres_for_each_res(dev, fw_name_devm_release,
1272 devm_name_match, &fw_cache,
1273 dev_create_fw_entry, &todo);
1275 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1276 list_del(&fce->list);
1278 spin_lock(&fwc->name_lock);
1279 /* only one cache entry for one firmware */
1280 if (!__fw_entry_found(fce->name)) {
1282 list_add(&fce->list, &fwc->fw_names);
1284 free_fw_cache_entry(fce);
1287 spin_unlock(&fwc->name_lock);
1290 async_schedule(__async_dev_cache_fw_image,
1295 static void __device_uncache_fw_images(void)
1297 struct firmware_cache *fwc = &fw_cache;
1298 struct fw_cache_entry *fce;
1300 spin_lock(&fwc->name_lock);
1301 while (!list_empty(&fwc->fw_names)) {
1302 fce = list_entry(fwc->fw_names.next,
1303 struct fw_cache_entry, list);
1304 list_del(&fce->list);
1305 spin_unlock(&fwc->name_lock);
1307 uncache_firmware(fce->name);
1308 free_fw_cache_entry(fce);
1310 spin_lock(&fwc->name_lock);
1312 spin_unlock(&fwc->name_lock);
1316 * device_cache_fw_images - cache devices' firmware
1318 * If one device called request_firmware or its nowait version
1319 * successfully before, the firmware names are recored into the
1320 * device's devres link list, so device_cache_fw_images can call
1321 * cache_firmware() to cache these firmwares for the device,
1322 * then the device driver can load its firmwares easily at
1323 * time when system is not ready to complete loading firmware.
1325 static void device_cache_fw_images(void)
1327 struct firmware_cache *fwc = &fw_cache;
1331 pr_debug("%s\n", __func__);
1333 /* cancel uncache work */
1334 cancel_delayed_work_sync(&fwc->work);
1337 * use small loading timeout for caching devices' firmware
1338 * because all these firmware images have been loaded
1339 * successfully at lease once, also system is ready for
1340 * completing firmware loading now. The maximum size of
1341 * firmware in current distributions is about 2M bytes,
1342 * so 10 secs should be enough.
1344 old_timeout = loading_timeout;
1345 loading_timeout = 10;
1347 mutex_lock(&fw_lock);
1348 fwc->state = FW_LOADER_START_CACHE;
1349 dpm_for_each_dev(NULL, dev_cache_fw_image);
1350 mutex_unlock(&fw_lock);
1352 /* wait for completion of caching firmware for all devices */
1353 spin_lock(&fwc->name_lock);
1355 prepare_to_wait(&fwc->wait_queue, &wait,
1356 TASK_UNINTERRUPTIBLE);
1360 spin_unlock(&fwc->name_lock);
1364 spin_lock(&fwc->name_lock);
1366 spin_unlock(&fwc->name_lock);
1367 finish_wait(&fwc->wait_queue, &wait);
1369 loading_timeout = old_timeout;
1373 * device_uncache_fw_images - uncache devices' firmware
1375 * uncache all firmwares which have been cached successfully
1376 * by device_uncache_fw_images earlier
1378 static void device_uncache_fw_images(void)
1380 pr_debug("%s\n", __func__);
1381 __device_uncache_fw_images();
1384 static void device_uncache_fw_images_work(struct work_struct *work)
1386 device_uncache_fw_images();
1390 * device_uncache_fw_images_delay - uncache devices firmwares
1391 * @delay: number of milliseconds to delay uncache device firmwares
1393 * uncache all devices's firmwares which has been cached successfully
1394 * by device_cache_fw_images after @delay milliseconds.
1396 static void device_uncache_fw_images_delay(unsigned long delay)
1398 schedule_delayed_work(&fw_cache.work,
1399 msecs_to_jiffies(delay));
1402 static int fw_pm_notify(struct notifier_block *notify_block,
1403 unsigned long mode, void *unused)
1406 case PM_HIBERNATION_PREPARE:
1407 case PM_SUSPEND_PREPARE:
1408 device_cache_fw_images();
1411 case PM_POST_SUSPEND:
1412 case PM_POST_HIBERNATION:
1413 case PM_POST_RESTORE:
1415 * In case that system sleep failed and syscore_suspend is
1418 mutex_lock(&fw_lock);
1419 fw_cache.state = FW_LOADER_NO_CACHE;
1420 mutex_unlock(&fw_lock);
1422 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1429 /* stop caching firmware once syscore_suspend is reached */
1430 static int fw_suspend(void)
1432 fw_cache.state = FW_LOADER_NO_CACHE;
1436 static struct syscore_ops fw_syscore_ops = {
1437 .suspend = fw_suspend,
1440 static int fw_cache_piggyback_on_request(const char *name)
1446 static void __init fw_cache_init(void)
1448 spin_lock_init(&fw_cache.lock);
1449 INIT_LIST_HEAD(&fw_cache.head);
1450 fw_cache.state = FW_LOADER_NO_CACHE;
1452 #ifdef CONFIG_PM_SLEEP
1453 spin_lock_init(&fw_cache.name_lock);
1454 INIT_LIST_HEAD(&fw_cache.fw_names);
1457 init_waitqueue_head(&fw_cache.wait_queue);
1458 INIT_DELAYED_WORK(&fw_cache.work,
1459 device_uncache_fw_images_work);
1461 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1462 register_pm_notifier(&fw_cache.pm_notify);
1464 register_syscore_ops(&fw_syscore_ops);
1468 static int __init firmware_class_init(void)
1471 return class_register(&firmware_class);
1474 static void __exit firmware_class_exit(void)
1476 #ifdef CONFIG_PM_SLEEP
1477 unregister_syscore_ops(&fw_syscore_ops);
1478 unregister_pm_notifier(&fw_cache.pm_notify);
1480 class_unregister(&firmware_class);
1483 fs_initcall(firmware_class_init);
1484 module_exit(firmware_class_exit);
1486 EXPORT_SYMBOL(release_firmware);
1487 EXPORT_SYMBOL(request_firmware);
1488 EXPORT_SYMBOL(request_firmware_nowait);
1489 EXPORT_SYMBOL_GPL(cache_firmware);
1490 EXPORT_SYMBOL_GPL(uncache_firmware);