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 struct delayed_work work;
121 struct notifier_block pm_notify;
125 struct firmware_buf {
127 struct list_head list;
128 struct completion completion;
129 struct firmware_cache *fwc;
130 unsigned long status;
140 struct fw_cache_entry {
141 struct list_head list;
145 struct firmware_priv {
146 struct delayed_work timeout_work;
149 struct firmware_buf *buf;
153 struct fw_name_devm {
158 #define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
160 #define FW_LOADER_NO_CACHE 0
161 #define FW_LOADER_START_CACHE 1
163 static int fw_cache_piggyback_on_request(const char *name);
165 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
166 * guarding for corner cases a global lock should be OK */
167 static DEFINE_MUTEX(fw_lock);
169 static struct firmware_cache fw_cache;
171 static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
172 struct firmware_cache *fwc)
174 struct firmware_buf *buf;
176 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
181 kref_init(&buf->ref);
182 strcpy(buf->fw_id, fw_name);
184 init_completion(&buf->completion);
185 buf->fmt = VMALLOC_BUF;
187 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
192 static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
194 struct firmware_buf *tmp;
195 struct firmware_cache *fwc = &fw_cache;
197 list_for_each_entry(tmp, &fwc->head, list)
198 if (!strcmp(tmp->fw_id, fw_name))
203 static int fw_lookup_and_allocate_buf(const char *fw_name,
204 struct firmware_cache *fwc,
205 struct firmware_buf **buf)
207 struct firmware_buf *tmp;
209 spin_lock(&fwc->lock);
210 tmp = __fw_lookup_buf(fw_name);
213 spin_unlock(&fwc->lock);
217 tmp = __allocate_fw_buf(fw_name, fwc);
219 list_add(&tmp->list, &fwc->head);
220 spin_unlock(&fwc->lock);
224 return tmp ? 0 : -ENOMEM;
227 static struct firmware_buf *fw_lookup_buf(const char *fw_name)
229 struct firmware_buf *tmp;
230 struct firmware_cache *fwc = &fw_cache;
232 spin_lock(&fwc->lock);
233 tmp = __fw_lookup_buf(fw_name);
234 spin_unlock(&fwc->lock);
239 static void __fw_free_buf(struct kref *ref)
241 struct firmware_buf *buf = to_fwbuf(ref);
242 struct firmware_cache *fwc = buf->fwc;
245 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
246 __func__, buf->fw_id, buf, buf->data,
247 (unsigned int)buf->size);
249 list_del(&buf->list);
250 spin_unlock(&fwc->lock);
253 if (buf->fmt == PAGE_BUF) {
255 for (i = 0; i < buf->nr_pages; i++)
256 __free_page(buf->pages[i]);
263 static void fw_free_buf(struct firmware_buf *buf)
265 struct firmware_cache *fwc = buf->fwc;
266 spin_lock(&fwc->lock);
267 if (!kref_put(&buf->ref, __fw_free_buf))
268 spin_unlock(&fwc->lock);
271 /* direct firmware loading support */
272 static char fw_path_para[256];
273 static const char * const fw_path[] = {
275 "/lib/firmware/updates/" UTS_RELEASE,
276 "/lib/firmware/updates",
277 "/lib/firmware/" UTS_RELEASE,
282 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
283 * from kernel command line because firmware_class is generally built in
284 * kernel instead of module.
286 module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
287 MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
289 /* Don't inline this: 'struct kstat' is biggish */
290 static noinline_for_stack long fw_file_size(struct file *file)
293 if (vfs_getattr(file->f_path.mnt, file->f_path.dentry, &st))
295 if (!S_ISREG(st.mode))
297 if (st.size != (long)st.size)
302 static bool fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
307 size = fw_file_size(file);
313 if (kernel_read(file, 0, buf, size) != size) {
322 static bool fw_get_filesystem_firmware(struct firmware_buf *buf)
325 bool success = false;
326 char *path = __getname();
328 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
331 /* skip the unset customized path */
335 snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id);
337 file = filp_open(path, O_RDONLY, 0);
340 success = fw_read_file_contents(file, buf);
349 static struct firmware_priv *to_firmware_priv(struct device *dev)
351 return container_of(dev, struct firmware_priv, dev);
354 static void fw_load_abort(struct firmware_priv *fw_priv)
356 struct firmware_buf *buf = fw_priv->buf;
358 set_bit(FW_STATUS_ABORT, &buf->status);
359 complete_all(&buf->completion);
362 static ssize_t firmware_timeout_show(struct class *class,
363 struct class_attribute *attr,
366 return sprintf(buf, "%d\n", loading_timeout);
370 * firmware_timeout_store - set number of seconds to wait for firmware
371 * @class: device class pointer
372 * @attr: device attribute pointer
373 * @buf: buffer to scan for timeout value
374 * @count: number of bytes in @buf
376 * Sets the number of seconds to wait for the firmware. Once
377 * this expires an error will be returned to the driver and no
378 * firmware will be provided.
380 * Note: zero means 'wait forever'.
382 static ssize_t firmware_timeout_store(struct class *class,
383 struct class_attribute *attr,
384 const char *buf, size_t count)
386 loading_timeout = simple_strtol(buf, NULL, 10);
387 if (loading_timeout < 0)
393 static struct class_attribute firmware_class_attrs[] = {
394 __ATTR(timeout, S_IWUSR | S_IRUGO,
395 firmware_timeout_show, firmware_timeout_store),
399 static void fw_dev_release(struct device *dev)
401 struct firmware_priv *fw_priv = to_firmware_priv(dev);
405 module_put(THIS_MODULE);
408 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
410 struct firmware_priv *fw_priv = to_firmware_priv(dev);
412 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
414 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
416 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
422 static struct class firmware_class = {
424 .class_attrs = firmware_class_attrs,
425 .dev_uevent = firmware_uevent,
426 .dev_release = fw_dev_release,
429 static ssize_t firmware_loading_show(struct device *dev,
430 struct device_attribute *attr, char *buf)
432 struct firmware_priv *fw_priv = to_firmware_priv(dev);
433 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
435 return sprintf(buf, "%d\n", loading);
438 /* firmware holds the ownership of pages */
439 static void firmware_free_data(const struct firmware *fw)
441 /* Loaded directly? */
446 fw_free_buf(fw->priv);
449 /* Some architectures don't have PAGE_KERNEL_RO */
450 #ifndef PAGE_KERNEL_RO
451 #define PAGE_KERNEL_RO PAGE_KERNEL
454 /* one pages buffer should be mapped/unmapped only once */
455 static int fw_map_pages_buf(struct firmware_buf *buf)
457 if (buf->fmt != PAGE_BUF)
462 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
469 * firmware_loading_store - set value in the 'loading' control file
470 * @dev: device pointer
471 * @attr: device attribute pointer
472 * @buf: buffer to scan for loading control value
473 * @count: number of bytes in @buf
475 * The relevant values are:
477 * 1: Start a load, discarding any previous partial load.
478 * 0: Conclude the load and hand the data to the driver code.
479 * -1: Conclude the load with an error and discard any written data.
481 static ssize_t firmware_loading_store(struct device *dev,
482 struct device_attribute *attr,
483 const char *buf, size_t count)
485 struct firmware_priv *fw_priv = to_firmware_priv(dev);
486 struct firmware_buf *fw_buf = fw_priv->buf;
487 int loading = simple_strtol(buf, NULL, 10);
490 mutex_lock(&fw_lock);
497 /* discarding any previous partial load */
498 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
499 for (i = 0; i < fw_buf->nr_pages; i++)
500 __free_page(fw_buf->pages[i]);
501 kfree(fw_buf->pages);
502 fw_buf->pages = NULL;
503 fw_buf->page_array_size = 0;
504 fw_buf->nr_pages = 0;
505 set_bit(FW_STATUS_LOADING, &fw_buf->status);
509 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
510 set_bit(FW_STATUS_DONE, &fw_buf->status);
511 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
514 * Several loading requests may be pending on
515 * one same firmware buf, so let all requests
516 * see the mapped 'buf->data' once the loading
519 fw_map_pages_buf(fw_buf);
520 complete_all(&fw_buf->completion);
525 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
528 fw_load_abort(fw_priv);
532 mutex_unlock(&fw_lock);
536 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
538 static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
539 struct bin_attribute *bin_attr,
540 char *buffer, loff_t offset, size_t count)
542 struct device *dev = kobj_to_dev(kobj);
543 struct firmware_priv *fw_priv = to_firmware_priv(dev);
544 struct firmware_buf *buf;
547 mutex_lock(&fw_lock);
549 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
553 if (offset > buf->size) {
557 if (count > buf->size - offset)
558 count = buf->size - offset;
564 int page_nr = offset >> PAGE_SHIFT;
565 int page_ofs = offset & (PAGE_SIZE-1);
566 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
568 page_data = kmap(buf->pages[page_nr]);
570 memcpy(buffer, page_data + page_ofs, page_cnt);
572 kunmap(buf->pages[page_nr]);
578 mutex_unlock(&fw_lock);
582 static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
584 struct firmware_buf *buf = fw_priv->buf;
585 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
587 /* If the array of pages is too small, grow it... */
588 if (buf->page_array_size < pages_needed) {
589 int new_array_size = max(pages_needed,
590 buf->page_array_size * 2);
591 struct page **new_pages;
593 new_pages = kmalloc(new_array_size * sizeof(void *),
596 fw_load_abort(fw_priv);
599 memcpy(new_pages, buf->pages,
600 buf->page_array_size * sizeof(void *));
601 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
602 (new_array_size - buf->page_array_size));
604 buf->pages = new_pages;
605 buf->page_array_size = new_array_size;
608 while (buf->nr_pages < pages_needed) {
609 buf->pages[buf->nr_pages] =
610 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
612 if (!buf->pages[buf->nr_pages]) {
613 fw_load_abort(fw_priv);
622 * firmware_data_write - write method for firmware
623 * @filp: open sysfs file
624 * @kobj: kobject for the device
625 * @bin_attr: bin_attr structure
626 * @buffer: buffer being written
627 * @offset: buffer offset for write in total data store area
628 * @count: buffer size
630 * Data written to the 'data' attribute will be later handed to
631 * the driver as a firmware image.
633 static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
634 struct bin_attribute *bin_attr,
635 char *buffer, loff_t offset, size_t count)
637 struct device *dev = kobj_to_dev(kobj);
638 struct firmware_priv *fw_priv = to_firmware_priv(dev);
639 struct firmware_buf *buf;
642 if (!capable(CAP_SYS_RAWIO))
645 mutex_lock(&fw_lock);
647 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
652 retval = fw_realloc_buffer(fw_priv, offset + count);
660 int page_nr = offset >> PAGE_SHIFT;
661 int page_ofs = offset & (PAGE_SIZE - 1);
662 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
664 page_data = kmap(buf->pages[page_nr]);
666 memcpy(page_data + page_ofs, buffer, page_cnt);
668 kunmap(buf->pages[page_nr]);
674 buf->size = max_t(size_t, offset, buf->size);
676 mutex_unlock(&fw_lock);
680 static struct bin_attribute firmware_attr_data = {
681 .attr = { .name = "data", .mode = 0644 },
683 .read = firmware_data_read,
684 .write = firmware_data_write,
687 static void firmware_class_timeout_work(struct work_struct *work)
689 struct firmware_priv *fw_priv = container_of(work,
690 struct firmware_priv, timeout_work.work);
692 mutex_lock(&fw_lock);
693 if (test_bit(FW_STATUS_DONE, &(fw_priv->buf->status))) {
694 mutex_unlock(&fw_lock);
697 fw_load_abort(fw_priv);
698 mutex_unlock(&fw_lock);
701 static struct firmware_priv *
702 fw_create_instance(struct firmware *firmware, const char *fw_name,
703 struct device *device, bool uevent, bool nowait)
705 struct firmware_priv *fw_priv;
706 struct device *f_dev;
708 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
710 dev_err(device, "%s: kmalloc failed\n", __func__);
711 fw_priv = ERR_PTR(-ENOMEM);
715 fw_priv->nowait = nowait;
716 fw_priv->fw = firmware;
717 INIT_DELAYED_WORK(&fw_priv->timeout_work,
718 firmware_class_timeout_work);
720 f_dev = &fw_priv->dev;
722 device_initialize(f_dev);
723 dev_set_name(f_dev, "%s", fw_name);
724 f_dev->parent = device;
725 f_dev->class = &firmware_class;
730 /* store the pages buffer info firmware from buf */
731 static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
734 fw->pages = buf->pages;
735 fw->size = buf->size;
736 fw->data = buf->data;
738 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
739 __func__, buf->fw_id, buf, buf->data,
740 (unsigned int)buf->size);
743 #ifdef CONFIG_PM_SLEEP
744 static void fw_name_devm_release(struct device *dev, void *res)
746 struct fw_name_devm *fwn = res;
748 if (fwn->magic == (unsigned long)&fw_cache)
749 pr_debug("%s: fw_name-%s devm-%p released\n",
750 __func__, fwn->name, res);
753 static int fw_devm_match(struct device *dev, void *res,
756 struct fw_name_devm *fwn = res;
758 return (fwn->magic == (unsigned long)&fw_cache) &&
759 !strcmp(fwn->name, match_data);
762 static struct fw_name_devm *fw_find_devm_name(struct device *dev,
765 struct fw_name_devm *fwn;
767 fwn = devres_find(dev, fw_name_devm_release,
768 fw_devm_match, (void *)name);
772 /* add firmware name into devres list */
773 static int fw_add_devm_name(struct device *dev, const char *name)
775 struct fw_name_devm *fwn;
777 fwn = fw_find_devm_name(dev, name);
781 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
782 strlen(name) + 1, GFP_KERNEL);
786 fwn->magic = (unsigned long)&fw_cache;
787 strcpy(fwn->name, name);
788 devres_add(dev, fwn);
793 static int fw_add_devm_name(struct device *dev, const char *name)
799 static void _request_firmware_cleanup(const struct firmware **firmware_p)
801 release_firmware(*firmware_p);
805 static struct firmware_priv *
806 _request_firmware_prepare(const struct firmware **firmware_p, const char *name,
807 struct device *device, bool uevent, bool nowait)
809 struct firmware *firmware;
810 struct firmware_priv *fw_priv = NULL;
811 struct firmware_buf *buf;
815 return ERR_PTR(-EINVAL);
817 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
819 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
821 return ERR_PTR(-ENOMEM);
824 if (fw_get_builtin_firmware(firmware, name)) {
825 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
829 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
831 fw_priv = fw_create_instance(firmware, name, device,
834 if (IS_ERR(fw_priv) || ret < 0) {
837 return ERR_PTR(-ENOMEM);
838 } else if (fw_priv) {
842 * bind with 'buf' now to avoid warning in failure path
843 * of requesting firmware.
845 firmware->priv = buf;
849 /* share the cached buf, which is inprogessing or completed */
851 mutex_lock(&fw_lock);
852 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
853 fw_priv = ERR_PTR(-ENOENT);
854 firmware->priv = buf;
855 _request_firmware_cleanup(firmware_p);
857 } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
859 fw_set_page_data(buf, firmware);
862 mutex_unlock(&fw_lock);
863 wait_for_completion(&buf->completion);
867 mutex_unlock(&fw_lock);
871 static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
875 struct device *f_dev = &fw_priv->dev;
876 struct firmware_buf *buf = fw_priv->buf;
877 struct firmware_cache *fwc = &fw_cache;
880 /* try direct loading from fs first */
881 if (fw_get_filesystem_firmware(buf)) {
882 dev_dbg(f_dev->parent, "firmware: direct-loading"
883 " firmware %s\n", buf->fw_id);
885 mutex_lock(&fw_lock);
886 set_bit(FW_STATUS_DONE, &buf->status);
887 mutex_unlock(&fw_lock);
888 complete_all(&buf->completion);
893 /* fall back on userspace loading */
896 dev_set_uevent_suppress(f_dev, true);
898 /* Need to pin this module until class device is destroyed */
899 __module_get(THIS_MODULE);
901 retval = device_add(f_dev);
903 dev_err(f_dev, "%s: device_register failed\n", __func__);
907 retval = device_create_bin_file(f_dev, &firmware_attr_data);
909 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
913 retval = device_create_file(f_dev, &dev_attr_loading);
915 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
916 goto err_del_bin_attr;
920 dev_set_uevent_suppress(f_dev, false);
921 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
922 if (timeout != MAX_SCHEDULE_TIMEOUT)
923 schedule_delayed_work(&fw_priv->timeout_work, timeout);
925 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
928 wait_for_completion(&buf->completion);
930 cancel_delayed_work_sync(&fw_priv->timeout_work);
933 mutex_lock(&fw_lock);
934 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
938 * add firmware name into devres list so that we can auto cache
939 * and uncache firmware for device.
941 * f_dev->parent may has been deleted already, but the problem
942 * should be fixed in devres or driver core.
944 if (!retval && f_dev->parent)
945 fw_add_devm_name(f_dev->parent, buf->fw_id);
948 * After caching firmware image is started, let it piggyback
949 * on request firmware.
951 if (!retval && fwc->state == FW_LOADER_START_CACHE) {
952 if (fw_cache_piggyback_on_request(buf->fw_id))
956 /* pass the pages buffer to driver at the last minute */
957 fw_set_page_data(buf, fw_priv->fw);
960 mutex_unlock(&fw_lock);
965 device_remove_file(f_dev, &dev_attr_loading);
967 device_remove_bin_file(f_dev, &firmware_attr_data);
976 * request_firmware: - send firmware request and wait for it
977 * @firmware_p: pointer to firmware image
978 * @name: name of firmware file
979 * @device: device for which firmware is being loaded
981 * @firmware_p will be used to return a firmware image by the name
982 * of @name for device @device.
984 * Should be called from user context where sleeping is allowed.
986 * @name will be used as $FIRMWARE in the uevent environment and
987 * should be distinctive enough not to be confused with any other
988 * firmware image for this or any other device.
990 * Caller must hold the reference count of @device.
993 request_firmware(const struct firmware **firmware_p, const char *name,
994 struct device *device)
996 struct firmware_priv *fw_priv;
999 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
1001 if (IS_ERR_OR_NULL(fw_priv))
1002 return PTR_RET(fw_priv);
1004 ret = usermodehelper_read_trylock();
1006 dev_err(device, "firmware: %s will not be loaded\n", name);
1008 ret = _request_firmware_load(fw_priv, true,
1009 firmware_loading_timeout());
1010 usermodehelper_read_unlock();
1013 _request_firmware_cleanup(firmware_p);
1019 * release_firmware: - release the resource associated with a firmware image
1020 * @fw: firmware resource to release
1022 void release_firmware(const struct firmware *fw)
1025 if (!fw_is_builtin_firmware(fw))
1026 firmware_free_data(fw);
1032 struct firmware_work {
1033 struct work_struct work;
1034 struct module *module;
1036 struct device *device;
1038 void (*cont)(const struct firmware *fw, void *context);
1042 static void request_firmware_work_func(struct work_struct *work)
1044 struct firmware_work *fw_work;
1045 const struct firmware *fw;
1046 struct firmware_priv *fw_priv;
1050 fw_work = container_of(work, struct firmware_work, work);
1051 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
1052 fw_work->uevent, true);
1053 if (IS_ERR_OR_NULL(fw_priv)) {
1054 ret = PTR_RET(fw_priv);
1058 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
1060 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
1061 usermodehelper_read_unlock();
1063 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
1068 _request_firmware_cleanup(&fw);
1071 fw_work->cont(fw, fw_work->context);
1072 put_device(fw_work->device);
1074 module_put(fw_work->module);
1079 * request_firmware_nowait - asynchronous version of request_firmware
1080 * @module: module requesting the firmware
1081 * @uevent: sends uevent to copy the firmware image if this flag
1082 * is non-zero else the firmware copy must be done manually.
1083 * @name: name of firmware file
1084 * @device: device for which firmware is being loaded
1085 * @gfp: allocation flags
1086 * @context: will be passed over to @cont, and
1087 * @fw may be %NULL if firmware request fails.
1088 * @cont: function will be called asynchronously when the firmware
1091 * Caller must hold the reference count of @device.
1093 * Asynchronous variant of request_firmware() for user contexts:
1094 * - sleep for as small periods as possible since it may
1095 * increase kernel boot time of built-in device drivers
1096 * requesting firmware in their ->probe() methods, if
1097 * @gfp is GFP_KERNEL.
1099 * - can't sleep at all if @gfp is GFP_ATOMIC.
1102 request_firmware_nowait(
1103 struct module *module, bool uevent,
1104 const char *name, struct device *device, gfp_t gfp, void *context,
1105 void (*cont)(const struct firmware *fw, void *context))
1107 struct firmware_work *fw_work;
1109 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
1113 fw_work->module = module;
1114 fw_work->name = name;
1115 fw_work->device = device;
1116 fw_work->context = context;
1117 fw_work->cont = cont;
1118 fw_work->uevent = uevent;
1120 if (!try_module_get(module)) {
1125 get_device(fw_work->device);
1126 INIT_WORK(&fw_work->work, request_firmware_work_func);
1127 schedule_work(&fw_work->work);
1132 * cache_firmware - cache one firmware image in kernel memory space
1133 * @fw_name: the firmware image name
1135 * Cache firmware in kernel memory so that drivers can use it when
1136 * system isn't ready for them to request firmware image from userspace.
1137 * Once it returns successfully, driver can use request_firmware or its
1138 * nowait version to get the cached firmware without any interacting
1141 * Return 0 if the firmware image has been cached successfully
1142 * Return !0 otherwise
1145 int cache_firmware(const char *fw_name)
1148 const struct firmware *fw;
1150 pr_debug("%s: %s\n", __func__, fw_name);
1152 ret = request_firmware(&fw, fw_name, NULL);
1156 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1162 * uncache_firmware - remove one cached firmware image
1163 * @fw_name: the firmware image name
1165 * Uncache one firmware image which has been cached successfully
1168 * Return 0 if the firmware cache has been removed successfully
1169 * Return !0 otherwise
1172 int uncache_firmware(const char *fw_name)
1174 struct firmware_buf *buf;
1177 pr_debug("%s: %s\n", __func__, fw_name);
1179 if (fw_get_builtin_firmware(&fw, fw_name))
1182 buf = fw_lookup_buf(fw_name);
1191 #ifdef CONFIG_PM_SLEEP
1192 static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
1194 static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1196 struct fw_cache_entry *fce;
1198 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1202 strcpy(fce->name, name);
1207 static int __fw_entry_found(const char *name)
1209 struct firmware_cache *fwc = &fw_cache;
1210 struct fw_cache_entry *fce;
1212 list_for_each_entry(fce, &fwc->fw_names, list) {
1213 if (!strcmp(fce->name, name))
1219 static int fw_cache_piggyback_on_request(const char *name)
1221 struct firmware_cache *fwc = &fw_cache;
1222 struct fw_cache_entry *fce;
1225 spin_lock(&fwc->name_lock);
1226 if (__fw_entry_found(name))
1229 fce = alloc_fw_cache_entry(name);
1232 list_add(&fce->list, &fwc->fw_names);
1233 pr_debug("%s: fw: %s\n", __func__, name);
1236 spin_unlock(&fwc->name_lock);
1240 static void free_fw_cache_entry(struct fw_cache_entry *fce)
1245 static void __async_dev_cache_fw_image(void *fw_entry,
1246 async_cookie_t cookie)
1248 struct fw_cache_entry *fce = fw_entry;
1249 struct firmware_cache *fwc = &fw_cache;
1252 ret = cache_firmware(fce->name);
1254 spin_lock(&fwc->name_lock);
1255 list_del(&fce->list);
1256 spin_unlock(&fwc->name_lock);
1258 free_fw_cache_entry(fce);
1262 /* called with dev->devres_lock held */
1263 static void dev_create_fw_entry(struct device *dev, void *res,
1266 struct fw_name_devm *fwn = res;
1267 const char *fw_name = fwn->name;
1268 struct list_head *head = data;
1269 struct fw_cache_entry *fce;
1271 fce = alloc_fw_cache_entry(fw_name);
1273 list_add(&fce->list, head);
1276 static int devm_name_match(struct device *dev, void *res,
1279 struct fw_name_devm *fwn = res;
1280 return (fwn->magic == (unsigned long)match_data);
1283 static void dev_cache_fw_image(struct device *dev, void *data)
1286 struct fw_cache_entry *fce;
1287 struct fw_cache_entry *fce_next;
1288 struct firmware_cache *fwc = &fw_cache;
1290 devres_for_each_res(dev, fw_name_devm_release,
1291 devm_name_match, &fw_cache,
1292 dev_create_fw_entry, &todo);
1294 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1295 list_del(&fce->list);
1297 spin_lock(&fwc->name_lock);
1298 /* only one cache entry for one firmware */
1299 if (!__fw_entry_found(fce->name)) {
1300 list_add(&fce->list, &fwc->fw_names);
1302 free_fw_cache_entry(fce);
1305 spin_unlock(&fwc->name_lock);
1308 async_schedule_domain(__async_dev_cache_fw_image,
1314 static void __device_uncache_fw_images(void)
1316 struct firmware_cache *fwc = &fw_cache;
1317 struct fw_cache_entry *fce;
1319 spin_lock(&fwc->name_lock);
1320 while (!list_empty(&fwc->fw_names)) {
1321 fce = list_entry(fwc->fw_names.next,
1322 struct fw_cache_entry, list);
1323 list_del(&fce->list);
1324 spin_unlock(&fwc->name_lock);
1326 uncache_firmware(fce->name);
1327 free_fw_cache_entry(fce);
1329 spin_lock(&fwc->name_lock);
1331 spin_unlock(&fwc->name_lock);
1335 * device_cache_fw_images - cache devices' firmware
1337 * If one device called request_firmware or its nowait version
1338 * successfully before, the firmware names are recored into the
1339 * device's devres link list, so device_cache_fw_images can call
1340 * cache_firmware() to cache these firmwares for the device,
1341 * then the device driver can load its firmwares easily at
1342 * time when system is not ready to complete loading firmware.
1344 static void device_cache_fw_images(void)
1346 struct firmware_cache *fwc = &fw_cache;
1350 pr_debug("%s\n", __func__);
1352 /* cancel uncache work */
1353 cancel_delayed_work_sync(&fwc->work);
1356 * use small loading timeout for caching devices' firmware
1357 * because all these firmware images have been loaded
1358 * successfully at lease once, also system is ready for
1359 * completing firmware loading now. The maximum size of
1360 * firmware in current distributions is about 2M bytes,
1361 * so 10 secs should be enough.
1363 old_timeout = loading_timeout;
1364 loading_timeout = 10;
1366 mutex_lock(&fw_lock);
1367 fwc->state = FW_LOADER_START_CACHE;
1368 dpm_for_each_dev(NULL, dev_cache_fw_image);
1369 mutex_unlock(&fw_lock);
1371 /* wait for completion of caching firmware for all devices */
1372 async_synchronize_full_domain(&fw_cache_domain);
1374 loading_timeout = old_timeout;
1378 * device_uncache_fw_images - uncache devices' firmware
1380 * uncache all firmwares which have been cached successfully
1381 * by device_uncache_fw_images earlier
1383 static void device_uncache_fw_images(void)
1385 pr_debug("%s\n", __func__);
1386 __device_uncache_fw_images();
1389 static void device_uncache_fw_images_work(struct work_struct *work)
1391 device_uncache_fw_images();
1395 * device_uncache_fw_images_delay - uncache devices firmwares
1396 * @delay: number of milliseconds to delay uncache device firmwares
1398 * uncache all devices's firmwares which has been cached successfully
1399 * by device_cache_fw_images after @delay milliseconds.
1401 static void device_uncache_fw_images_delay(unsigned long delay)
1403 schedule_delayed_work(&fw_cache.work,
1404 msecs_to_jiffies(delay));
1407 static int fw_pm_notify(struct notifier_block *notify_block,
1408 unsigned long mode, void *unused)
1411 case PM_HIBERNATION_PREPARE:
1412 case PM_SUSPEND_PREPARE:
1413 device_cache_fw_images();
1416 case PM_POST_SUSPEND:
1417 case PM_POST_HIBERNATION:
1418 case PM_POST_RESTORE:
1420 * In case that system sleep failed and syscore_suspend is
1423 mutex_lock(&fw_lock);
1424 fw_cache.state = FW_LOADER_NO_CACHE;
1425 mutex_unlock(&fw_lock);
1427 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1434 /* stop caching firmware once syscore_suspend is reached */
1435 static int fw_suspend(void)
1437 fw_cache.state = FW_LOADER_NO_CACHE;
1441 static struct syscore_ops fw_syscore_ops = {
1442 .suspend = fw_suspend,
1445 static int fw_cache_piggyback_on_request(const char *name)
1451 static void __init fw_cache_init(void)
1453 spin_lock_init(&fw_cache.lock);
1454 INIT_LIST_HEAD(&fw_cache.head);
1455 fw_cache.state = FW_LOADER_NO_CACHE;
1457 #ifdef CONFIG_PM_SLEEP
1458 spin_lock_init(&fw_cache.name_lock);
1459 INIT_LIST_HEAD(&fw_cache.fw_names);
1461 INIT_DELAYED_WORK(&fw_cache.work,
1462 device_uncache_fw_images_work);
1464 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1465 register_pm_notifier(&fw_cache.pm_notify);
1467 register_syscore_ops(&fw_syscore_ops);
1471 static int __init firmware_class_init(void)
1474 return class_register(&firmware_class);
1477 static void __exit firmware_class_exit(void)
1479 #ifdef CONFIG_PM_SLEEP
1480 unregister_syscore_ops(&fw_syscore_ops);
1481 unregister_pm_notifier(&fw_cache.pm_notify);
1483 class_unregister(&firmware_class);
1486 fs_initcall(firmware_class_init);
1487 module_exit(firmware_class_exit);
1489 EXPORT_SYMBOL(release_firmware);
1490 EXPORT_SYMBOL(request_firmware);
1491 EXPORT_SYMBOL(request_firmware_nowait);
1492 EXPORT_SYMBOL_GPL(cache_firmware);
1493 EXPORT_SYMBOL_GPL(uncache_firmware);