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>
28 #include <linux/syscore_ops.h>
32 MODULE_AUTHOR("Manuel Estrada Sainz");
33 MODULE_DESCRIPTION("Multi purpose firmware loading support");
34 MODULE_LICENSE("GPL");
36 /* Builtin firmware support */
38 #ifdef CONFIG_FW_LOADER
40 extern struct builtin_fw __start_builtin_fw[];
41 extern struct builtin_fw __end_builtin_fw[];
43 static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
45 struct builtin_fw *b_fw;
47 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
48 if (strcmp(name, b_fw->name) == 0) {
49 fw->size = b_fw->size;
50 fw->data = b_fw->data;
58 static bool fw_is_builtin_firmware(const struct firmware *fw)
60 struct builtin_fw *b_fw;
62 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
63 if (fw->data == b_fw->data)
69 #else /* Module case - no builtin firmware support */
71 static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
76 static inline bool fw_is_builtin_firmware(const struct firmware *fw)
88 static int loading_timeout = 60; /* In seconds */
90 static inline long firmware_loading_timeout(void)
92 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
95 struct firmware_cache {
96 /* firmware_buf instance will be added into the below list */
98 struct list_head head;
101 #ifdef CONFIG_PM_SLEEP
103 * Names of firmware images which have been cached successfully
104 * will be added into the below list so that device uncache
105 * helper can trace which firmware images have been cached
108 spinlock_t name_lock;
109 struct list_head fw_names;
111 wait_queue_head_t wait_queue;
113 struct delayed_work work;
115 struct notifier_block pm_notify;
119 struct firmware_buf {
121 struct list_head list;
122 struct completion completion;
123 struct firmware_cache *fwc;
124 unsigned long status;
133 struct fw_cache_entry {
134 struct list_head list;
138 struct firmware_priv {
139 struct timer_list timeout;
142 struct firmware_buf *buf;
146 struct fw_name_devm {
151 #define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
153 #define FW_LOADER_NO_CACHE 0
154 #define FW_LOADER_START_CACHE 1
156 static int fw_cache_piggyback_on_request(const char *name);
158 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
159 * guarding for corner cases a global lock should be OK */
160 static DEFINE_MUTEX(fw_lock);
162 static struct firmware_cache fw_cache;
164 static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
165 struct firmware_cache *fwc)
167 struct firmware_buf *buf;
169 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
174 kref_init(&buf->ref);
175 strcpy(buf->fw_id, fw_name);
177 init_completion(&buf->completion);
179 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
184 static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
186 struct firmware_buf *tmp;
187 struct firmware_cache *fwc = &fw_cache;
189 list_for_each_entry(tmp, &fwc->head, list)
190 if (!strcmp(tmp->fw_id, fw_name))
195 static int fw_lookup_and_allocate_buf(const char *fw_name,
196 struct firmware_cache *fwc,
197 struct firmware_buf **buf)
199 struct firmware_buf *tmp;
201 spin_lock(&fwc->lock);
202 tmp = __fw_lookup_buf(fw_name);
205 spin_unlock(&fwc->lock);
209 tmp = __allocate_fw_buf(fw_name, fwc);
211 list_add(&tmp->list, &fwc->head);
212 spin_unlock(&fwc->lock);
216 return tmp ? 0 : -ENOMEM;
219 static struct firmware_buf *fw_lookup_buf(const char *fw_name)
221 struct firmware_buf *tmp;
222 struct firmware_cache *fwc = &fw_cache;
224 spin_lock(&fwc->lock);
225 tmp = __fw_lookup_buf(fw_name);
226 spin_unlock(&fwc->lock);
231 static void __fw_free_buf(struct kref *ref)
233 struct firmware_buf *buf = to_fwbuf(ref);
234 struct firmware_cache *fwc = buf->fwc;
237 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
238 __func__, buf->fw_id, buf, buf->data,
239 (unsigned int)buf->size);
241 spin_lock(&fwc->lock);
242 list_del(&buf->list);
243 spin_unlock(&fwc->lock);
246 for (i = 0; i < buf->nr_pages; i++)
247 __free_page(buf->pages[i]);
252 static void fw_free_buf(struct firmware_buf *buf)
254 kref_put(&buf->ref, __fw_free_buf);
257 static struct firmware_priv *to_firmware_priv(struct device *dev)
259 return container_of(dev, struct firmware_priv, dev);
262 static void fw_load_abort(struct firmware_priv *fw_priv)
264 struct firmware_buf *buf = fw_priv->buf;
266 set_bit(FW_STATUS_ABORT, &buf->status);
267 complete_all(&buf->completion);
270 static ssize_t firmware_timeout_show(struct class *class,
271 struct class_attribute *attr,
274 return sprintf(buf, "%d\n", loading_timeout);
278 * firmware_timeout_store - set number of seconds to wait for firmware
279 * @class: device class pointer
280 * @attr: device attribute pointer
281 * @buf: buffer to scan for timeout value
282 * @count: number of bytes in @buf
284 * Sets the number of seconds to wait for the firmware. Once
285 * this expires an error will be returned to the driver and no
286 * firmware will be provided.
288 * Note: zero means 'wait forever'.
290 static ssize_t firmware_timeout_store(struct class *class,
291 struct class_attribute *attr,
292 const char *buf, size_t count)
294 loading_timeout = simple_strtol(buf, NULL, 10);
295 if (loading_timeout < 0)
301 static struct class_attribute firmware_class_attrs[] = {
302 __ATTR(timeout, S_IWUSR | S_IRUGO,
303 firmware_timeout_show, firmware_timeout_store),
307 static void fw_dev_release(struct device *dev)
309 struct firmware_priv *fw_priv = to_firmware_priv(dev);
313 module_put(THIS_MODULE);
316 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
318 struct firmware_priv *fw_priv = to_firmware_priv(dev);
320 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
322 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
324 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
330 static struct class firmware_class = {
332 .class_attrs = firmware_class_attrs,
333 .dev_uevent = firmware_uevent,
334 .dev_release = fw_dev_release,
337 static ssize_t firmware_loading_show(struct device *dev,
338 struct device_attribute *attr, char *buf)
340 struct firmware_priv *fw_priv = to_firmware_priv(dev);
341 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
343 return sprintf(buf, "%d\n", loading);
346 /* firmware holds the ownership of pages */
347 static void firmware_free_data(const struct firmware *fw)
350 fw_free_buf(fw->priv);
353 /* Some architectures don't have PAGE_KERNEL_RO */
354 #ifndef PAGE_KERNEL_RO
355 #define PAGE_KERNEL_RO PAGE_KERNEL
358 * firmware_loading_store - set value in the 'loading' control file
359 * @dev: device pointer
360 * @attr: device attribute pointer
361 * @buf: buffer to scan for loading control value
362 * @count: number of bytes in @buf
364 * The relevant values are:
366 * 1: Start a load, discarding any previous partial load.
367 * 0: Conclude the load and hand the data to the driver code.
368 * -1: Conclude the load with an error and discard any written data.
370 static ssize_t firmware_loading_store(struct device *dev,
371 struct device_attribute *attr,
372 const char *buf, size_t count)
374 struct firmware_priv *fw_priv = to_firmware_priv(dev);
375 struct firmware_buf *fw_buf = fw_priv->buf;
376 int loading = simple_strtol(buf, NULL, 10);
379 mutex_lock(&fw_lock);
386 /* discarding any previous partial load */
387 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
388 for (i = 0; i < fw_buf->nr_pages; i++)
389 __free_page(fw_buf->pages[i]);
390 kfree(fw_buf->pages);
391 fw_buf->pages = NULL;
392 fw_buf->page_array_size = 0;
393 fw_buf->nr_pages = 0;
394 set_bit(FW_STATUS_LOADING, &fw_buf->status);
398 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
399 set_bit(FW_STATUS_DONE, &fw_buf->status);
400 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
401 complete_all(&fw_buf->completion);
406 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
409 fw_load_abort(fw_priv);
413 mutex_unlock(&fw_lock);
417 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
419 static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
420 struct bin_attribute *bin_attr,
421 char *buffer, loff_t offset, size_t count)
423 struct device *dev = kobj_to_dev(kobj);
424 struct firmware_priv *fw_priv = to_firmware_priv(dev);
425 struct firmware_buf *buf;
428 mutex_lock(&fw_lock);
430 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
434 if (offset > buf->size) {
438 if (count > buf->size - offset)
439 count = buf->size - offset;
445 int page_nr = offset >> PAGE_SHIFT;
446 int page_ofs = offset & (PAGE_SIZE-1);
447 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
449 page_data = kmap(buf->pages[page_nr]);
451 memcpy(buffer, page_data + page_ofs, page_cnt);
453 kunmap(buf->pages[page_nr]);
459 mutex_unlock(&fw_lock);
463 static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
465 struct firmware_buf *buf = fw_priv->buf;
466 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
468 /* If the array of pages is too small, grow it... */
469 if (buf->page_array_size < pages_needed) {
470 int new_array_size = max(pages_needed,
471 buf->page_array_size * 2);
472 struct page **new_pages;
474 new_pages = kmalloc(new_array_size * sizeof(void *),
477 fw_load_abort(fw_priv);
480 memcpy(new_pages, buf->pages,
481 buf->page_array_size * sizeof(void *));
482 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
483 (new_array_size - buf->page_array_size));
485 buf->pages = new_pages;
486 buf->page_array_size = new_array_size;
489 while (buf->nr_pages < pages_needed) {
490 buf->pages[buf->nr_pages] =
491 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
493 if (!buf->pages[buf->nr_pages]) {
494 fw_load_abort(fw_priv);
503 * firmware_data_write - write method for firmware
504 * @filp: open sysfs file
505 * @kobj: kobject for the device
506 * @bin_attr: bin_attr structure
507 * @buffer: buffer being written
508 * @offset: buffer offset for write in total data store area
509 * @count: buffer size
511 * Data written to the 'data' attribute will be later handed to
512 * the driver as a firmware image.
514 static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
515 struct bin_attribute *bin_attr,
516 char *buffer, loff_t offset, size_t count)
518 struct device *dev = kobj_to_dev(kobj);
519 struct firmware_priv *fw_priv = to_firmware_priv(dev);
520 struct firmware_buf *buf;
523 if (!capable(CAP_SYS_RAWIO))
526 mutex_lock(&fw_lock);
528 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
533 retval = fw_realloc_buffer(fw_priv, offset + count);
541 int page_nr = offset >> PAGE_SHIFT;
542 int page_ofs = offset & (PAGE_SIZE - 1);
543 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
545 page_data = kmap(buf->pages[page_nr]);
547 memcpy(page_data + page_ofs, buffer, page_cnt);
549 kunmap(buf->pages[page_nr]);
555 buf->size = max_t(size_t, offset, buf->size);
557 mutex_unlock(&fw_lock);
561 static struct bin_attribute firmware_attr_data = {
562 .attr = { .name = "data", .mode = 0644 },
564 .read = firmware_data_read,
565 .write = firmware_data_write,
568 static void firmware_class_timeout(u_long data)
570 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
572 fw_load_abort(fw_priv);
575 static struct firmware_priv *
576 fw_create_instance(struct firmware *firmware, const char *fw_name,
577 struct device *device, bool uevent, bool nowait)
579 struct firmware_priv *fw_priv;
580 struct device *f_dev;
582 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
584 dev_err(device, "%s: kmalloc failed\n", __func__);
585 fw_priv = ERR_PTR(-ENOMEM);
589 fw_priv->nowait = nowait;
590 fw_priv->fw = firmware;
591 setup_timer(&fw_priv->timeout,
592 firmware_class_timeout, (u_long) fw_priv);
594 f_dev = &fw_priv->dev;
596 device_initialize(f_dev);
597 dev_set_name(f_dev, "%s", fw_name);
598 f_dev->parent = device;
599 f_dev->class = &firmware_class;
604 /* one pages buffer is mapped/unmapped only once */
605 static int fw_map_pages_buf(struct firmware_buf *buf)
607 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
613 /* store the pages buffer info firmware from buf */
614 static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
617 fw->pages = buf->pages;
618 fw->size = buf->size;
619 fw->data = buf->data;
621 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
622 __func__, buf->fw_id, buf, buf->data,
623 (unsigned int)buf->size);
626 #ifdef CONFIG_PM_SLEEP
627 static void fw_name_devm_release(struct device *dev, void *res)
629 struct fw_name_devm *fwn = res;
631 if (fwn->magic == (unsigned long)&fw_cache)
632 pr_debug("%s: fw_name-%s devm-%p released\n",
633 __func__, fwn->name, res);
636 static int fw_devm_match(struct device *dev, void *res,
639 struct fw_name_devm *fwn = res;
641 return (fwn->magic == (unsigned long)&fw_cache) &&
642 !strcmp(fwn->name, match_data);
645 static struct fw_name_devm *fw_find_devm_name(struct device *dev,
648 struct fw_name_devm *fwn;
650 fwn = devres_find(dev, fw_name_devm_release,
651 fw_devm_match, (void *)name);
655 /* add firmware name into devres list */
656 static int fw_add_devm_name(struct device *dev, const char *name)
658 struct fw_name_devm *fwn;
660 fwn = fw_find_devm_name(dev, name);
664 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
665 strlen(name) + 1, GFP_KERNEL);
669 fwn->magic = (unsigned long)&fw_cache;
670 strcpy(fwn->name, name);
671 devres_add(dev, fwn);
676 static int fw_add_devm_name(struct device *dev, const char *name)
682 static void _request_firmware_cleanup(const struct firmware **firmware_p)
684 release_firmware(*firmware_p);
688 static struct firmware_priv *
689 _request_firmware_prepare(const struct firmware **firmware_p, const char *name,
690 struct device *device, bool uevent, bool nowait)
692 struct firmware *firmware;
693 struct firmware_priv *fw_priv = NULL;
694 struct firmware_buf *buf;
698 return ERR_PTR(-EINVAL);
700 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
702 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
704 return ERR_PTR(-ENOMEM);
707 if (fw_get_builtin_firmware(firmware, name)) {
708 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
712 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
714 fw_priv = fw_create_instance(firmware, name, device,
717 if (IS_ERR(fw_priv) || ret < 0) {
720 return ERR_PTR(-ENOMEM);
721 } else if (fw_priv) {
725 * bind with 'buf' now to avoid warning in failure path
726 * of requesting firmware.
728 firmware->priv = buf;
732 /* share the cached buf, which is inprogessing or completed */
734 mutex_lock(&fw_lock);
735 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
736 fw_priv = ERR_PTR(-ENOENT);
737 firmware->priv = buf;
738 _request_firmware_cleanup(firmware_p);
740 } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
742 fw_set_page_data(buf, firmware);
745 mutex_unlock(&fw_lock);
746 wait_for_completion(&buf->completion);
750 mutex_unlock(&fw_lock);
754 static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
758 struct device *f_dev = &fw_priv->dev;
759 struct firmware_buf *buf = fw_priv->buf;
760 struct firmware_cache *fwc = &fw_cache;
762 dev_set_uevent_suppress(f_dev, true);
764 /* Need to pin this module until class device is destroyed */
765 __module_get(THIS_MODULE);
767 retval = device_add(f_dev);
769 dev_err(f_dev, "%s: device_register failed\n", __func__);
773 retval = device_create_bin_file(f_dev, &firmware_attr_data);
775 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
779 retval = device_create_file(f_dev, &dev_attr_loading);
781 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
782 goto err_del_bin_attr;
786 dev_set_uevent_suppress(f_dev, false);
787 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
788 if (timeout != MAX_SCHEDULE_TIMEOUT)
789 mod_timer(&fw_priv->timeout,
790 round_jiffies_up(jiffies + timeout));
792 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
795 wait_for_completion(&buf->completion);
797 del_timer_sync(&fw_priv->timeout);
799 mutex_lock(&fw_lock);
800 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
804 * add firmware name into devres list so that we can auto cache
805 * and uncache firmware for device.
807 * f_dev->parent may has been deleted already, but the problem
808 * should be fixed in devres or driver core.
810 if (!retval && f_dev->parent)
811 fw_add_devm_name(f_dev->parent, buf->fw_id);
814 retval = fw_map_pages_buf(buf);
817 * After caching firmware image is started, let it piggyback
818 * on request firmware.
820 if (!retval && fwc->state == FW_LOADER_START_CACHE) {
821 if (fw_cache_piggyback_on_request(buf->fw_id))
825 /* pass the pages buffer to driver at the last minute */
826 fw_set_page_data(buf, fw_priv->fw);
829 mutex_unlock(&fw_lock);
831 device_remove_file(f_dev, &dev_attr_loading);
833 device_remove_bin_file(f_dev, &firmware_attr_data);
842 * request_firmware: - send firmware request and wait for it
843 * @firmware_p: pointer to firmware image
844 * @name: name of firmware file
845 * @device: device for which firmware is being loaded
847 * @firmware_p will be used to return a firmware image by the name
848 * of @name for device @device.
850 * Should be called from user context where sleeping is allowed.
852 * @name will be used as $FIRMWARE in the uevent environment and
853 * should be distinctive enough not to be confused with any other
854 * firmware image for this or any other device.
856 * Caller must hold the reference count of @device.
859 request_firmware(const struct firmware **firmware_p, const char *name,
860 struct device *device)
862 struct firmware_priv *fw_priv;
865 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
867 if (IS_ERR_OR_NULL(fw_priv))
868 return PTR_RET(fw_priv);
870 ret = usermodehelper_read_trylock();
872 dev_err(device, "firmware: %s will not be loaded\n", name);
874 ret = _request_firmware_load(fw_priv, true,
875 firmware_loading_timeout());
876 usermodehelper_read_unlock();
879 _request_firmware_cleanup(firmware_p);
885 * release_firmware: - release the resource associated with a firmware image
886 * @fw: firmware resource to release
888 void release_firmware(const struct firmware *fw)
891 if (!fw_is_builtin_firmware(fw))
892 firmware_free_data(fw);
898 struct firmware_work {
899 struct work_struct work;
900 struct module *module;
902 struct device *device;
904 void (*cont)(const struct firmware *fw, void *context);
908 static void request_firmware_work_func(struct work_struct *work)
910 struct firmware_work *fw_work;
911 const struct firmware *fw;
912 struct firmware_priv *fw_priv;
916 fw_work = container_of(work, struct firmware_work, work);
917 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
918 fw_work->uevent, true);
919 if (IS_ERR_OR_NULL(fw_priv)) {
920 ret = PTR_RET(fw_priv);
924 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
926 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
927 usermodehelper_read_unlock();
929 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
934 _request_firmware_cleanup(&fw);
937 fw_work->cont(fw, fw_work->context);
938 put_device(fw_work->device);
940 module_put(fw_work->module);
945 * request_firmware_nowait - asynchronous version of request_firmware
946 * @module: module requesting the firmware
947 * @uevent: sends uevent to copy the firmware image if this flag
948 * is non-zero else the firmware copy must be done manually.
949 * @name: name of firmware file
950 * @device: device for which firmware is being loaded
951 * @gfp: allocation flags
952 * @context: will be passed over to @cont, and
953 * @fw may be %NULL if firmware request fails.
954 * @cont: function will be called asynchronously when the firmware
957 * Caller must hold the reference count of @device.
959 * Asynchronous variant of request_firmware() for user contexts:
960 * - sleep for as small periods as possible since it may
961 * increase kernel boot time of built-in device drivers
962 * requesting firmware in their ->probe() methods, if
963 * @gfp is GFP_KERNEL.
965 * - can't sleep at all if @gfp is GFP_ATOMIC.
968 request_firmware_nowait(
969 struct module *module, bool uevent,
970 const char *name, struct device *device, gfp_t gfp, void *context,
971 void (*cont)(const struct firmware *fw, void *context))
973 struct firmware_work *fw_work;
975 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
979 fw_work->module = module;
980 fw_work->name = name;
981 fw_work->device = device;
982 fw_work->context = context;
983 fw_work->cont = cont;
984 fw_work->uevent = uevent;
986 if (!try_module_get(module)) {
991 get_device(fw_work->device);
992 INIT_WORK(&fw_work->work, request_firmware_work_func);
993 schedule_work(&fw_work->work);
998 * cache_firmware - cache one firmware image in kernel memory space
999 * @fw_name: the firmware image name
1001 * Cache firmware in kernel memory so that drivers can use it when
1002 * system isn't ready for them to request firmware image from userspace.
1003 * Once it returns successfully, driver can use request_firmware or its
1004 * nowait version to get the cached firmware without any interacting
1007 * Return 0 if the firmware image has been cached successfully
1008 * Return !0 otherwise
1011 int cache_firmware(const char *fw_name)
1014 const struct firmware *fw;
1016 pr_debug("%s: %s\n", __func__, fw_name);
1018 ret = request_firmware(&fw, fw_name, NULL);
1022 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1028 * uncache_firmware - remove one cached firmware image
1029 * @fw_name: the firmware image name
1031 * Uncache one firmware image which has been cached successfully
1034 * Return 0 if the firmware cache has been removed successfully
1035 * Return !0 otherwise
1038 int uncache_firmware(const char *fw_name)
1040 struct firmware_buf *buf;
1043 pr_debug("%s: %s\n", __func__, fw_name);
1045 if (fw_get_builtin_firmware(&fw, fw_name))
1048 buf = fw_lookup_buf(fw_name);
1057 #ifdef CONFIG_PM_SLEEP
1058 static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1060 struct fw_cache_entry *fce;
1062 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1066 strcpy(fce->name, name);
1071 static int fw_cache_piggyback_on_request(const char *name)
1073 struct firmware_cache *fwc = &fw_cache;
1074 struct fw_cache_entry *fce;
1077 spin_lock(&fwc->name_lock);
1078 list_for_each_entry(fce, &fwc->fw_names, list) {
1079 if (!strcmp(fce->name, name))
1083 fce = alloc_fw_cache_entry(name);
1086 list_add(&fce->list, &fwc->fw_names);
1087 pr_debug("%s: fw: %s\n", __func__, name);
1090 spin_unlock(&fwc->name_lock);
1094 static void free_fw_cache_entry(struct fw_cache_entry *fce)
1099 static void __async_dev_cache_fw_image(void *fw_entry,
1100 async_cookie_t cookie)
1102 struct fw_cache_entry *fce = fw_entry;
1103 struct firmware_cache *fwc = &fw_cache;
1106 ret = cache_firmware(fce->name);
1108 spin_lock(&fwc->name_lock);
1109 list_del(&fce->list);
1110 spin_unlock(&fwc->name_lock);
1112 free_fw_cache_entry(fce);
1115 spin_lock(&fwc->name_lock);
1117 spin_unlock(&fwc->name_lock);
1119 wake_up(&fwc->wait_queue);
1122 /* called with dev->devres_lock held */
1123 static void dev_create_fw_entry(struct device *dev, void *res,
1126 struct fw_name_devm *fwn = res;
1127 const char *fw_name = fwn->name;
1128 struct list_head *head = data;
1129 struct fw_cache_entry *fce;
1131 fce = alloc_fw_cache_entry(fw_name);
1133 list_add(&fce->list, head);
1136 static int devm_name_match(struct device *dev, void *res,
1139 struct fw_name_devm *fwn = res;
1140 return (fwn->magic == (unsigned long)match_data);
1143 static void dev_cache_fw_image(struct device *dev, void *data)
1146 struct fw_cache_entry *fce;
1147 struct fw_cache_entry *fce_next;
1148 struct firmware_cache *fwc = &fw_cache;
1150 devres_for_each_res(dev, fw_name_devm_release,
1151 devm_name_match, &fw_cache,
1152 dev_create_fw_entry, &todo);
1154 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1155 list_del(&fce->list);
1157 spin_lock(&fwc->name_lock);
1159 list_add(&fce->list, &fwc->fw_names);
1160 spin_unlock(&fwc->name_lock);
1162 async_schedule(__async_dev_cache_fw_image, (void *)fce);
1166 static void __device_uncache_fw_images(void)
1168 struct firmware_cache *fwc = &fw_cache;
1169 struct fw_cache_entry *fce;
1171 spin_lock(&fwc->name_lock);
1172 while (!list_empty(&fwc->fw_names)) {
1173 fce = list_entry(fwc->fw_names.next,
1174 struct fw_cache_entry, list);
1175 list_del(&fce->list);
1176 spin_unlock(&fwc->name_lock);
1178 uncache_firmware(fce->name);
1179 free_fw_cache_entry(fce);
1181 spin_lock(&fwc->name_lock);
1183 spin_unlock(&fwc->name_lock);
1187 * device_cache_fw_images - cache devices' firmware
1189 * If one device called request_firmware or its nowait version
1190 * successfully before, the firmware names are recored into the
1191 * device's devres link list, so device_cache_fw_images can call
1192 * cache_firmware() to cache these firmwares for the device,
1193 * then the device driver can load its firmwares easily at
1194 * time when system is not ready to complete loading firmware.
1196 static void device_cache_fw_images(void)
1198 struct firmware_cache *fwc = &fw_cache;
1202 pr_debug("%s\n", __func__);
1205 * use small loading timeout for caching devices' firmware
1206 * because all these firmware images have been loaded
1207 * successfully at lease once, also system is ready for
1208 * completing firmware loading now. The maximum size of
1209 * firmware in current distributions is about 2M bytes,
1210 * so 10 secs should be enough.
1212 old_timeout = loading_timeout;
1213 loading_timeout = 10;
1215 mutex_lock(&fw_lock);
1216 fwc->state = FW_LOADER_START_CACHE;
1217 dpm_for_each_dev(NULL, dev_cache_fw_image);
1218 mutex_unlock(&fw_lock);
1220 /* wait for completion of caching firmware for all devices */
1221 spin_lock(&fwc->name_lock);
1223 prepare_to_wait(&fwc->wait_queue, &wait,
1224 TASK_UNINTERRUPTIBLE);
1228 spin_unlock(&fwc->name_lock);
1232 spin_lock(&fwc->name_lock);
1234 spin_unlock(&fwc->name_lock);
1235 finish_wait(&fwc->wait_queue, &wait);
1237 loading_timeout = old_timeout;
1241 * device_uncache_fw_images - uncache devices' firmware
1243 * uncache all firmwares which have been cached successfully
1244 * by device_uncache_fw_images earlier
1246 static void device_uncache_fw_images(void)
1248 pr_debug("%s\n", __func__);
1249 __device_uncache_fw_images();
1252 static void device_uncache_fw_images_work(struct work_struct *work)
1254 device_uncache_fw_images();
1258 * device_uncache_fw_images_delay - uncache devices firmwares
1259 * @delay: number of milliseconds to delay uncache device firmwares
1261 * uncache all devices's firmwares which has been cached successfully
1262 * by device_cache_fw_images after @delay milliseconds.
1264 static void device_uncache_fw_images_delay(unsigned long delay)
1266 schedule_delayed_work(&fw_cache.work,
1267 msecs_to_jiffies(delay));
1270 static int fw_pm_notify(struct notifier_block *notify_block,
1271 unsigned long mode, void *unused)
1274 case PM_HIBERNATION_PREPARE:
1275 case PM_SUSPEND_PREPARE:
1276 device_cache_fw_images();
1279 case PM_POST_SUSPEND:
1280 case PM_POST_HIBERNATION:
1281 case PM_POST_RESTORE:
1283 * In case that system sleep failed and syscore_suspend is
1286 mutex_lock(&fw_lock);
1287 fw_cache.state = FW_LOADER_NO_CACHE;
1288 mutex_unlock(&fw_lock);
1290 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1297 /* stop caching firmware once syscore_suspend is reached */
1298 static int fw_suspend(void)
1300 fw_cache.state = FW_LOADER_NO_CACHE;
1304 static struct syscore_ops fw_syscore_ops = {
1305 .suspend = fw_suspend,
1308 static int fw_cache_piggyback_on_request(const char *name)
1314 static void __init fw_cache_init(void)
1316 spin_lock_init(&fw_cache.lock);
1317 INIT_LIST_HEAD(&fw_cache.head);
1318 fw_cache.state = FW_LOADER_NO_CACHE;
1320 #ifdef CONFIG_PM_SLEEP
1321 spin_lock_init(&fw_cache.name_lock);
1322 INIT_LIST_HEAD(&fw_cache.fw_names);
1325 init_waitqueue_head(&fw_cache.wait_queue);
1326 INIT_DELAYED_WORK(&fw_cache.work,
1327 device_uncache_fw_images_work);
1329 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1330 register_pm_notifier(&fw_cache.pm_notify);
1332 register_syscore_ops(&fw_syscore_ops);
1336 static int __init firmware_class_init(void)
1339 return class_register(&firmware_class);
1342 static void __exit firmware_class_exit(void)
1344 #ifdef CONFIG_PM_SLEEP
1345 unregister_syscore_ops(&fw_syscore_ops);
1346 unregister_pm_notifier(&fw_cache.pm_notify);
1348 class_unregister(&firmware_class);
1351 fs_initcall(firmware_class_init);
1352 module_exit(firmware_class_exit);
1354 EXPORT_SYMBOL(release_firmware);
1355 EXPORT_SYMBOL(request_firmware);
1356 EXPORT_SYMBOL(request_firmware_nowait);
1357 EXPORT_SYMBOL_GPL(cache_firmware);
1358 EXPORT_SYMBOL_GPL(uncache_firmware);