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/list.h>
25 #include <linux/async.h>
27 #include <linux/suspend.h>
31 MODULE_AUTHOR("Manuel Estrada Sainz");
32 MODULE_DESCRIPTION("Multi purpose firmware loading support");
33 MODULE_LICENSE("GPL");
35 /* Builtin firmware support */
37 #ifdef CONFIG_FW_LOADER
39 extern struct builtin_fw __start_builtin_fw[];
40 extern struct builtin_fw __end_builtin_fw[];
42 static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
44 struct builtin_fw *b_fw;
46 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
47 if (strcmp(name, b_fw->name) == 0) {
48 fw->size = b_fw->size;
49 fw->data = b_fw->data;
57 static bool fw_is_builtin_firmware(const struct firmware *fw)
59 struct builtin_fw *b_fw;
61 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
62 if (fw->data == b_fw->data)
68 #else /* Module case - no builtin firmware support */
70 static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
75 static inline bool fw_is_builtin_firmware(const struct firmware *fw)
87 static int loading_timeout = 60; /* In seconds */
89 static inline long firmware_loading_timeout(void)
91 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
94 struct firmware_cache {
95 /* firmware_buf instance will be added into the below list */
97 struct list_head head;
100 * Names of firmware images which have been cached successfully
101 * will be added into the below list so that device uncache
102 * helper can trace which firmware images have been cached
105 spinlock_t name_lock;
106 struct list_head fw_names;
108 wait_queue_head_t wait_queue;
110 struct delayed_work work;
112 struct notifier_block pm_notify;
115 struct firmware_buf {
117 struct list_head list;
118 struct completion completion;
119 struct firmware_cache *fwc;
120 unsigned long status;
129 struct fw_cache_entry {
130 struct list_head list;
134 struct firmware_priv {
135 struct timer_list timeout;
138 struct firmware_buf *buf;
142 struct fw_name_devm {
147 #define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
149 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
150 * guarding for corner cases a global lock should be OK */
151 static DEFINE_MUTEX(fw_lock);
153 static struct firmware_cache fw_cache;
155 static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
156 struct firmware_cache *fwc)
158 struct firmware_buf *buf;
160 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
165 kref_init(&buf->ref);
166 strcpy(buf->fw_id, fw_name);
168 init_completion(&buf->completion);
170 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
175 static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
177 struct firmware_buf *tmp;
178 struct firmware_cache *fwc = &fw_cache;
180 list_for_each_entry(tmp, &fwc->head, list)
181 if (!strcmp(tmp->fw_id, fw_name))
186 static int fw_lookup_and_allocate_buf(const char *fw_name,
187 struct firmware_cache *fwc,
188 struct firmware_buf **buf)
190 struct firmware_buf *tmp;
192 spin_lock(&fwc->lock);
193 tmp = __fw_lookup_buf(fw_name);
196 spin_unlock(&fwc->lock);
200 tmp = __allocate_fw_buf(fw_name, fwc);
202 list_add(&tmp->list, &fwc->head);
203 spin_unlock(&fwc->lock);
207 return tmp ? 0 : -ENOMEM;
210 static struct firmware_buf *fw_lookup_buf(const char *fw_name)
212 struct firmware_buf *tmp;
213 struct firmware_cache *fwc = &fw_cache;
215 spin_lock(&fwc->lock);
216 tmp = __fw_lookup_buf(fw_name);
217 spin_unlock(&fwc->lock);
222 static void __fw_free_buf(struct kref *ref)
224 struct firmware_buf *buf = to_fwbuf(ref);
225 struct firmware_cache *fwc = buf->fwc;
228 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
229 __func__, buf->fw_id, buf, buf->data,
230 (unsigned int)buf->size);
232 spin_lock(&fwc->lock);
233 list_del(&buf->list);
234 spin_unlock(&fwc->lock);
237 for (i = 0; i < buf->nr_pages; i++)
238 __free_page(buf->pages[i]);
243 static void fw_free_buf(struct firmware_buf *buf)
245 kref_put(&buf->ref, __fw_free_buf);
248 static struct firmware_priv *to_firmware_priv(struct device *dev)
250 return container_of(dev, struct firmware_priv, dev);
253 static void fw_load_abort(struct firmware_priv *fw_priv)
255 struct firmware_buf *buf = fw_priv->buf;
257 set_bit(FW_STATUS_ABORT, &buf->status);
258 complete_all(&buf->completion);
261 static ssize_t firmware_timeout_show(struct class *class,
262 struct class_attribute *attr,
265 return sprintf(buf, "%d\n", loading_timeout);
269 * firmware_timeout_store - set number of seconds to wait for firmware
270 * @class: device class pointer
271 * @attr: device attribute pointer
272 * @buf: buffer to scan for timeout value
273 * @count: number of bytes in @buf
275 * Sets the number of seconds to wait for the firmware. Once
276 * this expires an error will be returned to the driver and no
277 * firmware will be provided.
279 * Note: zero means 'wait forever'.
281 static ssize_t firmware_timeout_store(struct class *class,
282 struct class_attribute *attr,
283 const char *buf, size_t count)
285 loading_timeout = simple_strtol(buf, NULL, 10);
286 if (loading_timeout < 0)
292 static struct class_attribute firmware_class_attrs[] = {
293 __ATTR(timeout, S_IWUSR | S_IRUGO,
294 firmware_timeout_show, firmware_timeout_store),
298 static void fw_dev_release(struct device *dev)
300 struct firmware_priv *fw_priv = to_firmware_priv(dev);
304 module_put(THIS_MODULE);
307 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
309 struct firmware_priv *fw_priv = to_firmware_priv(dev);
311 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
313 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
315 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
321 static struct class firmware_class = {
323 .class_attrs = firmware_class_attrs,
324 .dev_uevent = firmware_uevent,
325 .dev_release = fw_dev_release,
328 static ssize_t firmware_loading_show(struct device *dev,
329 struct device_attribute *attr, char *buf)
331 struct firmware_priv *fw_priv = to_firmware_priv(dev);
332 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
334 return sprintf(buf, "%d\n", loading);
337 /* firmware holds the ownership of pages */
338 static void firmware_free_data(const struct firmware *fw)
341 fw_free_buf(fw->priv);
344 /* Some architectures don't have PAGE_KERNEL_RO */
345 #ifndef PAGE_KERNEL_RO
346 #define PAGE_KERNEL_RO PAGE_KERNEL
349 * firmware_loading_store - set value in the 'loading' control file
350 * @dev: device pointer
351 * @attr: device attribute pointer
352 * @buf: buffer to scan for loading control value
353 * @count: number of bytes in @buf
355 * The relevant values are:
357 * 1: Start a load, discarding any previous partial load.
358 * 0: Conclude the load and hand the data to the driver code.
359 * -1: Conclude the load with an error and discard any written data.
361 static ssize_t firmware_loading_store(struct device *dev,
362 struct device_attribute *attr,
363 const char *buf, size_t count)
365 struct firmware_priv *fw_priv = to_firmware_priv(dev);
366 struct firmware_buf *fw_buf = fw_priv->buf;
367 int loading = simple_strtol(buf, NULL, 10);
370 mutex_lock(&fw_lock);
377 /* discarding any previous partial load */
378 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
379 for (i = 0; i < fw_buf->nr_pages; i++)
380 __free_page(fw_buf->pages[i]);
381 kfree(fw_buf->pages);
382 fw_buf->pages = NULL;
383 fw_buf->page_array_size = 0;
384 fw_buf->nr_pages = 0;
385 set_bit(FW_STATUS_LOADING, &fw_buf->status);
389 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
390 set_bit(FW_STATUS_DONE, &fw_buf->status);
391 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
392 complete_all(&fw_buf->completion);
397 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
400 fw_load_abort(fw_priv);
404 mutex_unlock(&fw_lock);
408 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
410 static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
411 struct bin_attribute *bin_attr,
412 char *buffer, loff_t offset, size_t count)
414 struct device *dev = kobj_to_dev(kobj);
415 struct firmware_priv *fw_priv = to_firmware_priv(dev);
416 struct firmware_buf *buf;
419 mutex_lock(&fw_lock);
421 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
425 if (offset > buf->size) {
429 if (count > buf->size - offset)
430 count = buf->size - offset;
436 int page_nr = offset >> PAGE_SHIFT;
437 int page_ofs = offset & (PAGE_SIZE-1);
438 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
440 page_data = kmap(buf->pages[page_nr]);
442 memcpy(buffer, page_data + page_ofs, page_cnt);
444 kunmap(buf->pages[page_nr]);
450 mutex_unlock(&fw_lock);
454 static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
456 struct firmware_buf *buf = fw_priv->buf;
457 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
459 /* If the array of pages is too small, grow it... */
460 if (buf->page_array_size < pages_needed) {
461 int new_array_size = max(pages_needed,
462 buf->page_array_size * 2);
463 struct page **new_pages;
465 new_pages = kmalloc(new_array_size * sizeof(void *),
468 fw_load_abort(fw_priv);
471 memcpy(new_pages, buf->pages,
472 buf->page_array_size * sizeof(void *));
473 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
474 (new_array_size - buf->page_array_size));
476 buf->pages = new_pages;
477 buf->page_array_size = new_array_size;
480 while (buf->nr_pages < pages_needed) {
481 buf->pages[buf->nr_pages] =
482 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
484 if (!buf->pages[buf->nr_pages]) {
485 fw_load_abort(fw_priv);
494 * firmware_data_write - write method for firmware
495 * @filp: open sysfs file
496 * @kobj: kobject for the device
497 * @bin_attr: bin_attr structure
498 * @buffer: buffer being written
499 * @offset: buffer offset for write in total data store area
500 * @count: buffer size
502 * Data written to the 'data' attribute will be later handed to
503 * the driver as a firmware image.
505 static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
506 struct bin_attribute *bin_attr,
507 char *buffer, loff_t offset, size_t count)
509 struct device *dev = kobj_to_dev(kobj);
510 struct firmware_priv *fw_priv = to_firmware_priv(dev);
511 struct firmware_buf *buf;
514 if (!capable(CAP_SYS_RAWIO))
517 mutex_lock(&fw_lock);
519 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
524 retval = fw_realloc_buffer(fw_priv, offset + count);
532 int page_nr = offset >> PAGE_SHIFT;
533 int page_ofs = offset & (PAGE_SIZE - 1);
534 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
536 page_data = kmap(buf->pages[page_nr]);
538 memcpy(page_data + page_ofs, buffer, page_cnt);
540 kunmap(buf->pages[page_nr]);
546 buf->size = max_t(size_t, offset, buf->size);
548 mutex_unlock(&fw_lock);
552 static struct bin_attribute firmware_attr_data = {
553 .attr = { .name = "data", .mode = 0644 },
555 .read = firmware_data_read,
556 .write = firmware_data_write,
559 static void firmware_class_timeout(u_long data)
561 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
563 fw_load_abort(fw_priv);
566 static struct firmware_priv *
567 fw_create_instance(struct firmware *firmware, const char *fw_name,
568 struct device *device, bool uevent, bool nowait)
570 struct firmware_priv *fw_priv;
571 struct device *f_dev;
573 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
575 dev_err(device, "%s: kmalloc failed\n", __func__);
576 fw_priv = ERR_PTR(-ENOMEM);
580 fw_priv->nowait = nowait;
581 fw_priv->fw = firmware;
582 setup_timer(&fw_priv->timeout,
583 firmware_class_timeout, (u_long) fw_priv);
585 f_dev = &fw_priv->dev;
587 device_initialize(f_dev);
588 dev_set_name(f_dev, "%s", fw_name);
589 f_dev->parent = device;
590 f_dev->class = &firmware_class;
595 /* one pages buffer is mapped/unmapped only once */
596 static int fw_map_pages_buf(struct firmware_buf *buf)
598 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
604 /* store the pages buffer info firmware from buf */
605 static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
608 fw->pages = buf->pages;
609 fw->size = buf->size;
610 fw->data = buf->data;
612 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
613 __func__, buf->fw_id, buf, buf->data,
614 (unsigned int)buf->size);
617 static void fw_name_devm_release(struct device *dev, void *res)
619 struct fw_name_devm *fwn = res;
621 if (fwn->magic == (unsigned long)&fw_cache)
622 pr_debug("%s: fw_name-%s devm-%p released\n",
623 __func__, fwn->name, res);
626 static int fw_devm_match(struct device *dev, void *res,
629 struct fw_name_devm *fwn = res;
631 return (fwn->magic == (unsigned long)&fw_cache) &&
632 !strcmp(fwn->name, match_data);
635 static struct fw_name_devm *fw_find_devm_name(struct device *dev,
638 struct fw_name_devm *fwn;
640 fwn = devres_find(dev, fw_name_devm_release,
641 fw_devm_match, (void *)name);
645 /* add firmware name into devres list */
646 static int fw_add_devm_name(struct device *dev, const char *name)
648 struct fw_name_devm *fwn;
650 fwn = fw_find_devm_name(dev, name);
654 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
655 strlen(name) + 1, GFP_KERNEL);
659 fwn->magic = (unsigned long)&fw_cache;
660 strcpy(fwn->name, name);
661 devres_add(dev, fwn);
666 static void _request_firmware_cleanup(const struct firmware **firmware_p)
668 release_firmware(*firmware_p);
672 static struct firmware_priv *
673 _request_firmware_prepare(const struct firmware **firmware_p, const char *name,
674 struct device *device, bool uevent, bool nowait)
676 struct firmware *firmware;
677 struct firmware_priv *fw_priv = NULL;
678 struct firmware_buf *buf;
682 return ERR_PTR(-EINVAL);
684 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
686 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
688 return ERR_PTR(-ENOMEM);
691 if (fw_get_builtin_firmware(firmware, name)) {
692 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
696 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
698 fw_priv = fw_create_instance(firmware, name, device,
701 if (IS_ERR(fw_priv) || ret < 0) {
704 return ERR_PTR(-ENOMEM);
705 } else if (fw_priv) {
709 * bind with 'buf' now to avoid warning in failure path
710 * of requesting firmware.
712 firmware->priv = buf;
716 /* share the cached buf, which is inprogessing or completed */
718 mutex_lock(&fw_lock);
719 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
720 fw_priv = ERR_PTR(-ENOENT);
721 firmware->priv = buf;
722 _request_firmware_cleanup(firmware_p);
724 } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
726 fw_set_page_data(buf, firmware);
729 mutex_unlock(&fw_lock);
730 wait_for_completion(&buf->completion);
734 mutex_unlock(&fw_lock);
738 static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
742 struct device *f_dev = &fw_priv->dev;
743 struct firmware_buf *buf = fw_priv->buf;
745 dev_set_uevent_suppress(f_dev, true);
747 /* Need to pin this module until class device is destroyed */
748 __module_get(THIS_MODULE);
750 retval = device_add(f_dev);
752 dev_err(f_dev, "%s: device_register failed\n", __func__);
756 retval = device_create_bin_file(f_dev, &firmware_attr_data);
758 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
762 retval = device_create_file(f_dev, &dev_attr_loading);
764 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
765 goto err_del_bin_attr;
769 dev_set_uevent_suppress(f_dev, false);
770 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
771 if (timeout != MAX_SCHEDULE_TIMEOUT)
772 mod_timer(&fw_priv->timeout,
773 round_jiffies_up(jiffies + timeout));
775 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
778 wait_for_completion(&buf->completion);
780 del_timer_sync(&fw_priv->timeout);
782 mutex_lock(&fw_lock);
783 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
787 * add firmware name into devres list so that we can auto cache
788 * and uncache firmware for device.
790 * f_dev->parent may has been deleted already, but the problem
791 * should be fixed in devres or driver core.
793 if (!retval && f_dev->parent)
794 fw_add_devm_name(f_dev->parent, buf->fw_id);
797 retval = fw_map_pages_buf(buf);
799 /* pass the pages buffer to driver at the last minute */
800 fw_set_page_data(buf, fw_priv->fw);
803 mutex_unlock(&fw_lock);
805 device_remove_file(f_dev, &dev_attr_loading);
807 device_remove_bin_file(f_dev, &firmware_attr_data);
816 * request_firmware: - send firmware request and wait for it
817 * @firmware_p: pointer to firmware image
818 * @name: name of firmware file
819 * @device: device for which firmware is being loaded
821 * @firmware_p will be used to return a firmware image by the name
822 * of @name for device @device.
824 * Should be called from user context where sleeping is allowed.
826 * @name will be used as $FIRMWARE in the uevent environment and
827 * should be distinctive enough not to be confused with any other
828 * firmware image for this or any other device.
830 * Caller must hold the reference count of @device.
833 request_firmware(const struct firmware **firmware_p, const char *name,
834 struct device *device)
836 struct firmware_priv *fw_priv;
839 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
841 if (IS_ERR_OR_NULL(fw_priv))
842 return PTR_RET(fw_priv);
844 ret = usermodehelper_read_trylock();
846 dev_err(device, "firmware: %s will not be loaded\n", name);
848 ret = _request_firmware_load(fw_priv, true,
849 firmware_loading_timeout());
850 usermodehelper_read_unlock();
853 _request_firmware_cleanup(firmware_p);
859 * release_firmware: - release the resource associated with a firmware image
860 * @fw: firmware resource to release
862 void release_firmware(const struct firmware *fw)
865 if (!fw_is_builtin_firmware(fw))
866 firmware_free_data(fw);
872 struct firmware_work {
873 struct work_struct work;
874 struct module *module;
876 struct device *device;
878 void (*cont)(const struct firmware *fw, void *context);
882 static void request_firmware_work_func(struct work_struct *work)
884 struct firmware_work *fw_work;
885 const struct firmware *fw;
886 struct firmware_priv *fw_priv;
890 fw_work = container_of(work, struct firmware_work, work);
891 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
892 fw_work->uevent, true);
893 if (IS_ERR_OR_NULL(fw_priv)) {
894 ret = PTR_RET(fw_priv);
898 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
900 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
901 usermodehelper_read_unlock();
903 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
908 _request_firmware_cleanup(&fw);
911 fw_work->cont(fw, fw_work->context);
912 put_device(fw_work->device);
914 module_put(fw_work->module);
919 * request_firmware_nowait - asynchronous version of request_firmware
920 * @module: module requesting the firmware
921 * @uevent: sends uevent to copy the firmware image if this flag
922 * is non-zero else the firmware copy must be done manually.
923 * @name: name of firmware file
924 * @device: device for which firmware is being loaded
925 * @gfp: allocation flags
926 * @context: will be passed over to @cont, and
927 * @fw may be %NULL if firmware request fails.
928 * @cont: function will be called asynchronously when the firmware
931 * Caller must hold the reference count of @device.
933 * Asynchronous variant of request_firmware() for user contexts:
934 * - sleep for as small periods as possible since it may
935 * increase kernel boot time of built-in device drivers
936 * requesting firmware in their ->probe() methods, if
937 * @gfp is GFP_KERNEL.
939 * - can't sleep at all if @gfp is GFP_ATOMIC.
942 request_firmware_nowait(
943 struct module *module, bool uevent,
944 const char *name, struct device *device, gfp_t gfp, void *context,
945 void (*cont)(const struct firmware *fw, void *context))
947 struct firmware_work *fw_work;
949 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
953 fw_work->module = module;
954 fw_work->name = name;
955 fw_work->device = device;
956 fw_work->context = context;
957 fw_work->cont = cont;
958 fw_work->uevent = uevent;
960 if (!try_module_get(module)) {
965 get_device(fw_work->device);
966 INIT_WORK(&fw_work->work, request_firmware_work_func);
967 schedule_work(&fw_work->work);
972 * cache_firmware - cache one firmware image in kernel memory space
973 * @fw_name: the firmware image name
975 * Cache firmware in kernel memory so that drivers can use it when
976 * system isn't ready for them to request firmware image from userspace.
977 * Once it returns successfully, driver can use request_firmware or its
978 * nowait version to get the cached firmware without any interacting
981 * Return 0 if the firmware image has been cached successfully
982 * Return !0 otherwise
985 int cache_firmware(const char *fw_name)
988 const struct firmware *fw;
990 pr_debug("%s: %s\n", __func__, fw_name);
992 ret = request_firmware(&fw, fw_name, NULL);
996 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1002 * uncache_firmware - remove one cached firmware image
1003 * @fw_name: the firmware image name
1005 * Uncache one firmware image which has been cached successfully
1008 * Return 0 if the firmware cache has been removed successfully
1009 * Return !0 otherwise
1012 int uncache_firmware(const char *fw_name)
1014 struct firmware_buf *buf;
1017 pr_debug("%s: %s\n", __func__, fw_name);
1019 if (fw_get_builtin_firmware(&fw, fw_name))
1022 buf = fw_lookup_buf(fw_name);
1031 static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1033 struct fw_cache_entry *fce;
1035 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1039 strcpy(fce->name, name);
1044 static void free_fw_cache_entry(struct fw_cache_entry *fce)
1049 static void __async_dev_cache_fw_image(void *fw_entry,
1050 async_cookie_t cookie)
1052 struct fw_cache_entry *fce = fw_entry;
1053 struct firmware_cache *fwc = &fw_cache;
1056 ret = cache_firmware(fce->name);
1060 spin_lock(&fwc->name_lock);
1061 list_add(&fce->list, &fwc->fw_names);
1062 spin_unlock(&fwc->name_lock);
1066 free_fw_cache_entry(fce);
1068 spin_lock(&fwc->name_lock);
1070 spin_unlock(&fwc->name_lock);
1072 wake_up(&fwc->wait_queue);
1075 /* called with dev->devres_lock held */
1076 static void dev_create_fw_entry(struct device *dev, void *res,
1079 struct fw_name_devm *fwn = res;
1080 const char *fw_name = fwn->name;
1081 struct list_head *head = data;
1082 struct fw_cache_entry *fce;
1084 fce = alloc_fw_cache_entry(fw_name);
1086 list_add(&fce->list, head);
1089 static int devm_name_match(struct device *dev, void *res,
1092 struct fw_name_devm *fwn = res;
1093 return (fwn->magic == (unsigned long)match_data);
1096 static void dev_cache_fw_image(struct device *dev, void *data)
1099 struct fw_cache_entry *fce;
1100 struct fw_cache_entry *fce_next;
1101 struct firmware_cache *fwc = &fw_cache;
1103 devres_for_each_res(dev, fw_name_devm_release,
1104 devm_name_match, &fw_cache,
1105 dev_create_fw_entry, &todo);
1107 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1108 list_del(&fce->list);
1110 spin_lock(&fwc->name_lock);
1112 spin_unlock(&fwc->name_lock);
1114 async_schedule(__async_dev_cache_fw_image, (void *)fce);
1118 static void __device_uncache_fw_images(void)
1120 struct firmware_cache *fwc = &fw_cache;
1121 struct fw_cache_entry *fce;
1123 spin_lock(&fwc->name_lock);
1124 while (!list_empty(&fwc->fw_names)) {
1125 fce = list_entry(fwc->fw_names.next,
1126 struct fw_cache_entry, list);
1127 list_del(&fce->list);
1128 spin_unlock(&fwc->name_lock);
1130 uncache_firmware(fce->name);
1131 free_fw_cache_entry(fce);
1133 spin_lock(&fwc->name_lock);
1135 spin_unlock(&fwc->name_lock);
1139 * device_cache_fw_images - cache devices' firmware
1141 * If one device called request_firmware or its nowait version
1142 * successfully before, the firmware names are recored into the
1143 * device's devres link list, so device_cache_fw_images can call
1144 * cache_firmware() to cache these firmwares for the device,
1145 * then the device driver can load its firmwares easily at
1146 * time when system is not ready to complete loading firmware.
1148 static void device_cache_fw_images(void)
1150 struct firmware_cache *fwc = &fw_cache;
1154 pr_debug("%s\n", __func__);
1157 * use small loading timeout for caching devices' firmware
1158 * because all these firmware images have been loaded
1159 * successfully at lease once, also system is ready for
1160 * completing firmware loading now. The maximum size of
1161 * firmware in current distributions is about 2M bytes,
1162 * so 10 secs should be enough.
1164 old_timeout = loading_timeout;
1165 loading_timeout = 10;
1167 dpm_for_each_dev(NULL, dev_cache_fw_image);
1169 /* wait for completion of caching firmware for all devices */
1170 spin_lock(&fwc->name_lock);
1172 prepare_to_wait(&fwc->wait_queue, &wait,
1173 TASK_UNINTERRUPTIBLE);
1177 spin_unlock(&fwc->name_lock);
1181 spin_lock(&fwc->name_lock);
1183 spin_unlock(&fwc->name_lock);
1184 finish_wait(&fwc->wait_queue, &wait);
1186 loading_timeout = old_timeout;
1190 * device_uncache_fw_images - uncache devices' firmware
1192 * uncache all firmwares which have been cached successfully
1193 * by device_uncache_fw_images earlier
1195 static void device_uncache_fw_images(void)
1197 pr_debug("%s\n", __func__);
1198 __device_uncache_fw_images();
1201 static void device_uncache_fw_images_work(struct work_struct *work)
1203 device_uncache_fw_images();
1207 * device_uncache_fw_images_delay - uncache devices firmwares
1208 * @delay: number of milliseconds to delay uncache device firmwares
1210 * uncache all devices's firmwares which has been cached successfully
1211 * by device_cache_fw_images after @delay milliseconds.
1213 static void device_uncache_fw_images_delay(unsigned long delay)
1215 schedule_delayed_work(&fw_cache.work,
1216 msecs_to_jiffies(delay));
1220 static int fw_pm_notify(struct notifier_block *notify_block,
1221 unsigned long mode, void *unused)
1224 case PM_HIBERNATION_PREPARE:
1225 case PM_SUSPEND_PREPARE:
1226 device_cache_fw_images();
1229 case PM_POST_SUSPEND:
1230 case PM_POST_HIBERNATION:
1231 case PM_POST_RESTORE:
1232 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1239 static int fw_pm_notify(struct notifier_block *notify_block,
1240 unsigned long mode, void *unused)
1246 static void __init fw_cache_init(void)
1248 spin_lock_init(&fw_cache.lock);
1249 INIT_LIST_HEAD(&fw_cache.head);
1251 spin_lock_init(&fw_cache.name_lock);
1252 INIT_LIST_HEAD(&fw_cache.fw_names);
1255 init_waitqueue_head(&fw_cache.wait_queue);
1256 INIT_DELAYED_WORK(&fw_cache.work,
1257 device_uncache_fw_images_work);
1259 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1260 register_pm_notifier(&fw_cache.pm_notify);
1263 static int __init firmware_class_init(void)
1266 return class_register(&firmware_class);
1269 static void __exit firmware_class_exit(void)
1271 unregister_pm_notifier(&fw_cache.pm_notify);
1272 class_unregister(&firmware_class);
1275 fs_initcall(firmware_class_init);
1276 module_exit(firmware_class_exit);
1278 EXPORT_SYMBOL(release_firmware);
1279 EXPORT_SYMBOL(request_firmware);
1280 EXPORT_SYMBOL(request_firmware_nowait);
1281 EXPORT_SYMBOL_GPL(cache_firmware);
1282 EXPORT_SYMBOL_GPL(uncache_firmware);