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 static const char *fw_path[] = {
40 "/lib/firmware/updates/" UTS_RELEASE,
41 "/lib/firmware/updates",
42 "/lib/firmware/" UTS_RELEASE,
46 /* Don't inline this: 'struct kstat' is biggish */
47 static noinline long fw_file_size(struct file *file)
50 if (vfs_getattr(file->f_path.mnt, file->f_path.dentry, &st))
52 if (!S_ISREG(st.mode))
54 if (st.size != (long)st.size)
59 static bool fw_read_file_contents(struct file *file, struct firmware *fw)
65 size = fw_file_size(file);
72 if (vfs_read(file, buf, size, &pos) != size) {
81 static bool fw_get_filesystem_firmware(struct firmware *fw, const char *name)
85 char *path = __getname();
87 for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
89 snprintf(path, PATH_MAX, "%s/%s", fw_path[i], name);
91 file = filp_open(path, O_RDONLY, 0);
94 success = fw_read_file_contents(file, fw);
103 /* Builtin firmware support */
105 #ifdef CONFIG_FW_LOADER
107 extern struct builtin_fw __start_builtin_fw[];
108 extern struct builtin_fw __end_builtin_fw[];
110 static bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
112 struct builtin_fw *b_fw;
114 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
115 if (strcmp(name, b_fw->name) == 0) {
116 fw->size = b_fw->size;
117 fw->data = b_fw->data;
125 static bool fw_is_builtin_firmware(const struct firmware *fw)
127 struct builtin_fw *b_fw;
129 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
130 if (fw->data == b_fw->data)
136 #else /* Module case - no builtin firmware support */
138 static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name)
143 static inline bool fw_is_builtin_firmware(const struct firmware *fw)
155 static int loading_timeout = 60; /* In seconds */
157 static inline long firmware_loading_timeout(void)
159 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
162 struct firmware_cache {
163 /* firmware_buf instance will be added into the below list */
165 struct list_head head;
168 #ifdef CONFIG_PM_SLEEP
170 * Names of firmware images which have been cached successfully
171 * will be added into the below list so that device uncache
172 * helper can trace which firmware images have been cached
175 spinlock_t name_lock;
176 struct list_head fw_names;
178 wait_queue_head_t wait_queue;
180 struct delayed_work work;
182 struct notifier_block pm_notify;
186 struct firmware_buf {
188 struct list_head list;
189 struct completion completion;
190 struct firmware_cache *fwc;
191 unsigned long status;
200 struct fw_cache_entry {
201 struct list_head list;
205 struct firmware_priv {
206 struct timer_list timeout;
209 struct firmware_buf *buf;
213 struct fw_name_devm {
218 #define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
220 #define FW_LOADER_NO_CACHE 0
221 #define FW_LOADER_START_CACHE 1
223 static int fw_cache_piggyback_on_request(const char *name);
225 /* fw_lock could be moved to 'struct firmware_priv' but since it is just
226 * guarding for corner cases a global lock should be OK */
227 static DEFINE_MUTEX(fw_lock);
229 static struct firmware_cache fw_cache;
231 static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
232 struct firmware_cache *fwc)
234 struct firmware_buf *buf;
236 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
241 kref_init(&buf->ref);
242 strcpy(buf->fw_id, fw_name);
244 init_completion(&buf->completion);
246 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
251 static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
253 struct firmware_buf *tmp;
254 struct firmware_cache *fwc = &fw_cache;
256 list_for_each_entry(tmp, &fwc->head, list)
257 if (!strcmp(tmp->fw_id, fw_name))
262 static int fw_lookup_and_allocate_buf(const char *fw_name,
263 struct firmware_cache *fwc,
264 struct firmware_buf **buf)
266 struct firmware_buf *tmp;
268 spin_lock(&fwc->lock);
269 tmp = __fw_lookup_buf(fw_name);
272 spin_unlock(&fwc->lock);
276 tmp = __allocate_fw_buf(fw_name, fwc);
278 list_add(&tmp->list, &fwc->head);
279 spin_unlock(&fwc->lock);
283 return tmp ? 0 : -ENOMEM;
286 static struct firmware_buf *fw_lookup_buf(const char *fw_name)
288 struct firmware_buf *tmp;
289 struct firmware_cache *fwc = &fw_cache;
291 spin_lock(&fwc->lock);
292 tmp = __fw_lookup_buf(fw_name);
293 spin_unlock(&fwc->lock);
298 static void __fw_free_buf(struct kref *ref)
300 struct firmware_buf *buf = to_fwbuf(ref);
301 struct firmware_cache *fwc = buf->fwc;
304 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
305 __func__, buf->fw_id, buf, buf->data,
306 (unsigned int)buf->size);
308 spin_lock(&fwc->lock);
309 list_del(&buf->list);
310 spin_unlock(&fwc->lock);
313 for (i = 0; i < buf->nr_pages; i++)
314 __free_page(buf->pages[i]);
319 static void fw_free_buf(struct firmware_buf *buf)
321 kref_put(&buf->ref, __fw_free_buf);
324 static struct firmware_priv *to_firmware_priv(struct device *dev)
326 return container_of(dev, struct firmware_priv, dev);
329 static void fw_load_abort(struct firmware_priv *fw_priv)
331 struct firmware_buf *buf = fw_priv->buf;
333 set_bit(FW_STATUS_ABORT, &buf->status);
334 complete_all(&buf->completion);
337 static ssize_t firmware_timeout_show(struct class *class,
338 struct class_attribute *attr,
341 return sprintf(buf, "%d\n", loading_timeout);
345 * firmware_timeout_store - set number of seconds to wait for firmware
346 * @class: device class pointer
347 * @attr: device attribute pointer
348 * @buf: buffer to scan for timeout value
349 * @count: number of bytes in @buf
351 * Sets the number of seconds to wait for the firmware. Once
352 * this expires an error will be returned to the driver and no
353 * firmware will be provided.
355 * Note: zero means 'wait forever'.
357 static ssize_t firmware_timeout_store(struct class *class,
358 struct class_attribute *attr,
359 const char *buf, size_t count)
361 loading_timeout = simple_strtol(buf, NULL, 10);
362 if (loading_timeout < 0)
368 static struct class_attribute firmware_class_attrs[] = {
369 __ATTR(timeout, S_IWUSR | S_IRUGO,
370 firmware_timeout_show, firmware_timeout_store),
374 static void fw_dev_release(struct device *dev)
376 struct firmware_priv *fw_priv = to_firmware_priv(dev);
380 module_put(THIS_MODULE);
383 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
385 struct firmware_priv *fw_priv = to_firmware_priv(dev);
387 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
389 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
391 if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait))
397 static struct class firmware_class = {
399 .class_attrs = firmware_class_attrs,
400 .dev_uevent = firmware_uevent,
401 .dev_release = fw_dev_release,
404 static ssize_t firmware_loading_show(struct device *dev,
405 struct device_attribute *attr, char *buf)
407 struct firmware_priv *fw_priv = to_firmware_priv(dev);
408 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
410 return sprintf(buf, "%d\n", loading);
413 /* firmware holds the ownership of pages */
414 static void firmware_free_data(const struct firmware *fw)
416 /* Loaded directly? */
421 fw_free_buf(fw->priv);
424 /* Some architectures don't have PAGE_KERNEL_RO */
425 #ifndef PAGE_KERNEL_RO
426 #define PAGE_KERNEL_RO PAGE_KERNEL
429 * firmware_loading_store - set value in the 'loading' control file
430 * @dev: device pointer
431 * @attr: device attribute pointer
432 * @buf: buffer to scan for loading control value
433 * @count: number of bytes in @buf
435 * The relevant values are:
437 * 1: Start a load, discarding any previous partial load.
438 * 0: Conclude the load and hand the data to the driver code.
439 * -1: Conclude the load with an error and discard any written data.
441 static ssize_t firmware_loading_store(struct device *dev,
442 struct device_attribute *attr,
443 const char *buf, size_t count)
445 struct firmware_priv *fw_priv = to_firmware_priv(dev);
446 struct firmware_buf *fw_buf = fw_priv->buf;
447 int loading = simple_strtol(buf, NULL, 10);
450 mutex_lock(&fw_lock);
457 /* discarding any previous partial load */
458 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
459 for (i = 0; i < fw_buf->nr_pages; i++)
460 __free_page(fw_buf->pages[i]);
461 kfree(fw_buf->pages);
462 fw_buf->pages = NULL;
463 fw_buf->page_array_size = 0;
464 fw_buf->nr_pages = 0;
465 set_bit(FW_STATUS_LOADING, &fw_buf->status);
469 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
470 set_bit(FW_STATUS_DONE, &fw_buf->status);
471 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
472 complete_all(&fw_buf->completion);
477 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
480 fw_load_abort(fw_priv);
484 mutex_unlock(&fw_lock);
488 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
490 static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
491 struct bin_attribute *bin_attr,
492 char *buffer, loff_t offset, size_t count)
494 struct device *dev = kobj_to_dev(kobj);
495 struct firmware_priv *fw_priv = to_firmware_priv(dev);
496 struct firmware_buf *buf;
499 mutex_lock(&fw_lock);
501 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
505 if (offset > buf->size) {
509 if (count > buf->size - offset)
510 count = buf->size - offset;
516 int page_nr = offset >> PAGE_SHIFT;
517 int page_ofs = offset & (PAGE_SIZE-1);
518 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
520 page_data = kmap(buf->pages[page_nr]);
522 memcpy(buffer, page_data + page_ofs, page_cnt);
524 kunmap(buf->pages[page_nr]);
530 mutex_unlock(&fw_lock);
534 static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
536 struct firmware_buf *buf = fw_priv->buf;
537 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
539 /* If the array of pages is too small, grow it... */
540 if (buf->page_array_size < pages_needed) {
541 int new_array_size = max(pages_needed,
542 buf->page_array_size * 2);
543 struct page **new_pages;
545 new_pages = kmalloc(new_array_size * sizeof(void *),
548 fw_load_abort(fw_priv);
551 memcpy(new_pages, buf->pages,
552 buf->page_array_size * sizeof(void *));
553 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
554 (new_array_size - buf->page_array_size));
556 buf->pages = new_pages;
557 buf->page_array_size = new_array_size;
560 while (buf->nr_pages < pages_needed) {
561 buf->pages[buf->nr_pages] =
562 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
564 if (!buf->pages[buf->nr_pages]) {
565 fw_load_abort(fw_priv);
574 * firmware_data_write - write method for firmware
575 * @filp: open sysfs file
576 * @kobj: kobject for the device
577 * @bin_attr: bin_attr structure
578 * @buffer: buffer being written
579 * @offset: buffer offset for write in total data store area
580 * @count: buffer size
582 * Data written to the 'data' attribute will be later handed to
583 * the driver as a firmware image.
585 static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
586 struct bin_attribute *bin_attr,
587 char *buffer, loff_t offset, size_t count)
589 struct device *dev = kobj_to_dev(kobj);
590 struct firmware_priv *fw_priv = to_firmware_priv(dev);
591 struct firmware_buf *buf;
594 if (!capable(CAP_SYS_RAWIO))
597 mutex_lock(&fw_lock);
599 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
604 retval = fw_realloc_buffer(fw_priv, offset + count);
612 int page_nr = offset >> PAGE_SHIFT;
613 int page_ofs = offset & (PAGE_SIZE - 1);
614 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
616 page_data = kmap(buf->pages[page_nr]);
618 memcpy(page_data + page_ofs, buffer, page_cnt);
620 kunmap(buf->pages[page_nr]);
626 buf->size = max_t(size_t, offset, buf->size);
628 mutex_unlock(&fw_lock);
632 static struct bin_attribute firmware_attr_data = {
633 .attr = { .name = "data", .mode = 0644 },
635 .read = firmware_data_read,
636 .write = firmware_data_write,
639 static void firmware_class_timeout(u_long data)
641 struct firmware_priv *fw_priv = (struct firmware_priv *) data;
643 fw_load_abort(fw_priv);
646 static struct firmware_priv *
647 fw_create_instance(struct firmware *firmware, const char *fw_name,
648 struct device *device, bool uevent, bool nowait)
650 struct firmware_priv *fw_priv;
651 struct device *f_dev;
653 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
655 dev_err(device, "%s: kmalloc failed\n", __func__);
656 fw_priv = ERR_PTR(-ENOMEM);
660 fw_priv->nowait = nowait;
661 fw_priv->fw = firmware;
662 setup_timer(&fw_priv->timeout,
663 firmware_class_timeout, (u_long) fw_priv);
665 f_dev = &fw_priv->dev;
667 device_initialize(f_dev);
668 dev_set_name(f_dev, "%s", fw_name);
669 f_dev->parent = device;
670 f_dev->class = &firmware_class;
675 /* one pages buffer is mapped/unmapped only once */
676 static int fw_map_pages_buf(struct firmware_buf *buf)
678 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
684 /* store the pages buffer info firmware from buf */
685 static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
688 fw->pages = buf->pages;
689 fw->size = buf->size;
690 fw->data = buf->data;
692 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
693 __func__, buf->fw_id, buf, buf->data,
694 (unsigned int)buf->size);
697 #ifdef CONFIG_PM_SLEEP
698 static void fw_name_devm_release(struct device *dev, void *res)
700 struct fw_name_devm *fwn = res;
702 if (fwn->magic == (unsigned long)&fw_cache)
703 pr_debug("%s: fw_name-%s devm-%p released\n",
704 __func__, fwn->name, res);
707 static int fw_devm_match(struct device *dev, void *res,
710 struct fw_name_devm *fwn = res;
712 return (fwn->magic == (unsigned long)&fw_cache) &&
713 !strcmp(fwn->name, match_data);
716 static struct fw_name_devm *fw_find_devm_name(struct device *dev,
719 struct fw_name_devm *fwn;
721 fwn = devres_find(dev, fw_name_devm_release,
722 fw_devm_match, (void *)name);
726 /* add firmware name into devres list */
727 static int fw_add_devm_name(struct device *dev, const char *name)
729 struct fw_name_devm *fwn;
731 fwn = fw_find_devm_name(dev, name);
735 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
736 strlen(name) + 1, GFP_KERNEL);
740 fwn->magic = (unsigned long)&fw_cache;
741 strcpy(fwn->name, name);
742 devres_add(dev, fwn);
747 static int fw_add_devm_name(struct device *dev, const char *name)
753 static void _request_firmware_cleanup(const struct firmware **firmware_p)
755 release_firmware(*firmware_p);
759 static struct firmware_priv *
760 _request_firmware_prepare(const struct firmware **firmware_p, const char *name,
761 struct device *device, bool uevent, bool nowait)
763 struct firmware *firmware;
764 struct firmware_priv *fw_priv = NULL;
765 struct firmware_buf *buf;
769 return ERR_PTR(-EINVAL);
771 *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
773 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
775 return ERR_PTR(-ENOMEM);
778 if (fw_get_builtin_firmware(firmware, name)) {
779 dev_dbg(device, "firmware: using built-in firmware %s\n", name);
783 if (fw_get_filesystem_firmware(firmware, name)) {
784 dev_dbg(device, "firmware: direct-loading firmware %s\n", name);
788 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
790 fw_priv = fw_create_instance(firmware, name, device,
793 if (IS_ERR(fw_priv) || ret < 0) {
796 return ERR_PTR(-ENOMEM);
797 } else if (fw_priv) {
801 * bind with 'buf' now to avoid warning in failure path
802 * of requesting firmware.
804 firmware->priv = buf;
808 /* share the cached buf, which is inprogessing or completed */
810 mutex_lock(&fw_lock);
811 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
812 fw_priv = ERR_PTR(-ENOENT);
813 firmware->priv = buf;
814 _request_firmware_cleanup(firmware_p);
816 } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
818 fw_set_page_data(buf, firmware);
821 mutex_unlock(&fw_lock);
822 wait_for_completion(&buf->completion);
826 mutex_unlock(&fw_lock);
830 static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
834 struct device *f_dev = &fw_priv->dev;
835 struct firmware_buf *buf = fw_priv->buf;
836 struct firmware_cache *fwc = &fw_cache;
838 dev_set_uevent_suppress(f_dev, true);
840 /* Need to pin this module until class device is destroyed */
841 __module_get(THIS_MODULE);
843 retval = device_add(f_dev);
845 dev_err(f_dev, "%s: device_register failed\n", __func__);
849 retval = device_create_bin_file(f_dev, &firmware_attr_data);
851 dev_err(f_dev, "%s: sysfs_create_bin_file failed\n", __func__);
855 retval = device_create_file(f_dev, &dev_attr_loading);
857 dev_err(f_dev, "%s: device_create_file failed\n", __func__);
858 goto err_del_bin_attr;
862 dev_set_uevent_suppress(f_dev, false);
863 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
864 if (timeout != MAX_SCHEDULE_TIMEOUT)
865 mod_timer(&fw_priv->timeout,
866 round_jiffies_up(jiffies + timeout));
868 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
871 wait_for_completion(&buf->completion);
873 del_timer_sync(&fw_priv->timeout);
875 mutex_lock(&fw_lock);
876 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
880 * add firmware name into devres list so that we can auto cache
881 * and uncache firmware for device.
883 * f_dev->parent may has been deleted already, but the problem
884 * should be fixed in devres or driver core.
886 if (!retval && f_dev->parent)
887 fw_add_devm_name(f_dev->parent, buf->fw_id);
890 retval = fw_map_pages_buf(buf);
893 * After caching firmware image is started, let it piggyback
894 * on request firmware.
896 if (!retval && fwc->state == FW_LOADER_START_CACHE) {
897 if (fw_cache_piggyback_on_request(buf->fw_id))
901 /* pass the pages buffer to driver at the last minute */
902 fw_set_page_data(buf, fw_priv->fw);
905 mutex_unlock(&fw_lock);
907 device_remove_file(f_dev, &dev_attr_loading);
909 device_remove_bin_file(f_dev, &firmware_attr_data);
918 * request_firmware: - send firmware request and wait for it
919 * @firmware_p: pointer to firmware image
920 * @name: name of firmware file
921 * @device: device for which firmware is being loaded
923 * @firmware_p will be used to return a firmware image by the name
924 * of @name for device @device.
926 * Should be called from user context where sleeping is allowed.
928 * @name will be used as $FIRMWARE in the uevent environment and
929 * should be distinctive enough not to be confused with any other
930 * firmware image for this or any other device.
932 * Caller must hold the reference count of @device.
935 request_firmware(const struct firmware **firmware_p, const char *name,
936 struct device *device)
938 struct firmware_priv *fw_priv;
941 fw_priv = _request_firmware_prepare(firmware_p, name, device, true,
943 if (IS_ERR_OR_NULL(fw_priv))
944 return PTR_RET(fw_priv);
946 ret = usermodehelper_read_trylock();
948 dev_err(device, "firmware: %s will not be loaded\n", name);
950 ret = _request_firmware_load(fw_priv, true,
951 firmware_loading_timeout());
952 usermodehelper_read_unlock();
955 _request_firmware_cleanup(firmware_p);
961 * release_firmware: - release the resource associated with a firmware image
962 * @fw: firmware resource to release
964 void release_firmware(const struct firmware *fw)
967 if (!fw_is_builtin_firmware(fw))
968 firmware_free_data(fw);
974 struct firmware_work {
975 struct work_struct work;
976 struct module *module;
978 struct device *device;
980 void (*cont)(const struct firmware *fw, void *context);
984 static void request_firmware_work_func(struct work_struct *work)
986 struct firmware_work *fw_work;
987 const struct firmware *fw;
988 struct firmware_priv *fw_priv;
992 fw_work = container_of(work, struct firmware_work, work);
993 fw_priv = _request_firmware_prepare(&fw, fw_work->name, fw_work->device,
994 fw_work->uevent, true);
995 if (IS_ERR_OR_NULL(fw_priv)) {
996 ret = PTR_RET(fw_priv);
1000 timeout = usermodehelper_read_lock_wait(firmware_loading_timeout());
1002 ret = _request_firmware_load(fw_priv, fw_work->uevent, timeout);
1003 usermodehelper_read_unlock();
1005 dev_dbg(fw_work->device, "firmware: %s loading timed out\n",
1010 _request_firmware_cleanup(&fw);
1013 fw_work->cont(fw, fw_work->context);
1014 put_device(fw_work->device);
1016 module_put(fw_work->module);
1021 * request_firmware_nowait - asynchronous version of request_firmware
1022 * @module: module requesting the firmware
1023 * @uevent: sends uevent to copy the firmware image if this flag
1024 * is non-zero else the firmware copy must be done manually.
1025 * @name: name of firmware file
1026 * @device: device for which firmware is being loaded
1027 * @gfp: allocation flags
1028 * @context: will be passed over to @cont, and
1029 * @fw may be %NULL if firmware request fails.
1030 * @cont: function will be called asynchronously when the firmware
1033 * Caller must hold the reference count of @device.
1035 * Asynchronous variant of request_firmware() for user contexts:
1036 * - sleep for as small periods as possible since it may
1037 * increase kernel boot time of built-in device drivers
1038 * requesting firmware in their ->probe() methods, if
1039 * @gfp is GFP_KERNEL.
1041 * - can't sleep at all if @gfp is GFP_ATOMIC.
1044 request_firmware_nowait(
1045 struct module *module, bool uevent,
1046 const char *name, struct device *device, gfp_t gfp, void *context,
1047 void (*cont)(const struct firmware *fw, void *context))
1049 struct firmware_work *fw_work;
1051 fw_work = kzalloc(sizeof (struct firmware_work), gfp);
1055 fw_work->module = module;
1056 fw_work->name = name;
1057 fw_work->device = device;
1058 fw_work->context = context;
1059 fw_work->cont = cont;
1060 fw_work->uevent = uevent;
1062 if (!try_module_get(module)) {
1067 get_device(fw_work->device);
1068 INIT_WORK(&fw_work->work, request_firmware_work_func);
1069 schedule_work(&fw_work->work);
1074 * cache_firmware - cache one firmware image in kernel memory space
1075 * @fw_name: the firmware image name
1077 * Cache firmware in kernel memory so that drivers can use it when
1078 * system isn't ready for them to request firmware image from userspace.
1079 * Once it returns successfully, driver can use request_firmware or its
1080 * nowait version to get the cached firmware without any interacting
1083 * Return 0 if the firmware image has been cached successfully
1084 * Return !0 otherwise
1087 int cache_firmware(const char *fw_name)
1090 const struct firmware *fw;
1092 pr_debug("%s: %s\n", __func__, fw_name);
1094 ret = request_firmware(&fw, fw_name, NULL);
1098 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1104 * uncache_firmware - remove one cached firmware image
1105 * @fw_name: the firmware image name
1107 * Uncache one firmware image which has been cached successfully
1110 * Return 0 if the firmware cache has been removed successfully
1111 * Return !0 otherwise
1114 int uncache_firmware(const char *fw_name)
1116 struct firmware_buf *buf;
1119 pr_debug("%s: %s\n", __func__, fw_name);
1121 if (fw_get_builtin_firmware(&fw, fw_name))
1124 buf = fw_lookup_buf(fw_name);
1133 #ifdef CONFIG_PM_SLEEP
1134 static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1136 struct fw_cache_entry *fce;
1138 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1142 strcpy(fce->name, name);
1147 static int fw_cache_piggyback_on_request(const char *name)
1149 struct firmware_cache *fwc = &fw_cache;
1150 struct fw_cache_entry *fce;
1153 spin_lock(&fwc->name_lock);
1154 list_for_each_entry(fce, &fwc->fw_names, list) {
1155 if (!strcmp(fce->name, name))
1159 fce = alloc_fw_cache_entry(name);
1162 list_add(&fce->list, &fwc->fw_names);
1163 pr_debug("%s: fw: %s\n", __func__, name);
1166 spin_unlock(&fwc->name_lock);
1170 static void free_fw_cache_entry(struct fw_cache_entry *fce)
1175 static void __async_dev_cache_fw_image(void *fw_entry,
1176 async_cookie_t cookie)
1178 struct fw_cache_entry *fce = fw_entry;
1179 struct firmware_cache *fwc = &fw_cache;
1182 ret = cache_firmware(fce->name);
1184 spin_lock(&fwc->name_lock);
1185 list_del(&fce->list);
1186 spin_unlock(&fwc->name_lock);
1188 free_fw_cache_entry(fce);
1191 spin_lock(&fwc->name_lock);
1193 spin_unlock(&fwc->name_lock);
1195 wake_up(&fwc->wait_queue);
1198 /* called with dev->devres_lock held */
1199 static void dev_create_fw_entry(struct device *dev, void *res,
1202 struct fw_name_devm *fwn = res;
1203 const char *fw_name = fwn->name;
1204 struct list_head *head = data;
1205 struct fw_cache_entry *fce;
1207 fce = alloc_fw_cache_entry(fw_name);
1209 list_add(&fce->list, head);
1212 static int devm_name_match(struct device *dev, void *res,
1215 struct fw_name_devm *fwn = res;
1216 return (fwn->magic == (unsigned long)match_data);
1219 static void dev_cache_fw_image(struct device *dev, void *data)
1222 struct fw_cache_entry *fce;
1223 struct fw_cache_entry *fce_next;
1224 struct firmware_cache *fwc = &fw_cache;
1226 devres_for_each_res(dev, fw_name_devm_release,
1227 devm_name_match, &fw_cache,
1228 dev_create_fw_entry, &todo);
1230 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1231 list_del(&fce->list);
1233 spin_lock(&fwc->name_lock);
1235 list_add(&fce->list, &fwc->fw_names);
1236 spin_unlock(&fwc->name_lock);
1238 async_schedule(__async_dev_cache_fw_image, (void *)fce);
1242 static void __device_uncache_fw_images(void)
1244 struct firmware_cache *fwc = &fw_cache;
1245 struct fw_cache_entry *fce;
1247 spin_lock(&fwc->name_lock);
1248 while (!list_empty(&fwc->fw_names)) {
1249 fce = list_entry(fwc->fw_names.next,
1250 struct fw_cache_entry, list);
1251 list_del(&fce->list);
1252 spin_unlock(&fwc->name_lock);
1254 uncache_firmware(fce->name);
1255 free_fw_cache_entry(fce);
1257 spin_lock(&fwc->name_lock);
1259 spin_unlock(&fwc->name_lock);
1263 * device_cache_fw_images - cache devices' firmware
1265 * If one device called request_firmware or its nowait version
1266 * successfully before, the firmware names are recored into the
1267 * device's devres link list, so device_cache_fw_images can call
1268 * cache_firmware() to cache these firmwares for the device,
1269 * then the device driver can load its firmwares easily at
1270 * time when system is not ready to complete loading firmware.
1272 static void device_cache_fw_images(void)
1274 struct firmware_cache *fwc = &fw_cache;
1278 pr_debug("%s\n", __func__);
1281 * use small loading timeout for caching devices' firmware
1282 * because all these firmware images have been loaded
1283 * successfully at lease once, also system is ready for
1284 * completing firmware loading now. The maximum size of
1285 * firmware in current distributions is about 2M bytes,
1286 * so 10 secs should be enough.
1288 old_timeout = loading_timeout;
1289 loading_timeout = 10;
1291 mutex_lock(&fw_lock);
1292 fwc->state = FW_LOADER_START_CACHE;
1293 dpm_for_each_dev(NULL, dev_cache_fw_image);
1294 mutex_unlock(&fw_lock);
1296 /* wait for completion of caching firmware for all devices */
1297 spin_lock(&fwc->name_lock);
1299 prepare_to_wait(&fwc->wait_queue, &wait,
1300 TASK_UNINTERRUPTIBLE);
1304 spin_unlock(&fwc->name_lock);
1308 spin_lock(&fwc->name_lock);
1310 spin_unlock(&fwc->name_lock);
1311 finish_wait(&fwc->wait_queue, &wait);
1313 loading_timeout = old_timeout;
1317 * device_uncache_fw_images - uncache devices' firmware
1319 * uncache all firmwares which have been cached successfully
1320 * by device_uncache_fw_images earlier
1322 static void device_uncache_fw_images(void)
1324 pr_debug("%s\n", __func__);
1325 __device_uncache_fw_images();
1328 static void device_uncache_fw_images_work(struct work_struct *work)
1330 device_uncache_fw_images();
1334 * device_uncache_fw_images_delay - uncache devices firmwares
1335 * @delay: number of milliseconds to delay uncache device firmwares
1337 * uncache all devices's firmwares which has been cached successfully
1338 * by device_cache_fw_images after @delay milliseconds.
1340 static void device_uncache_fw_images_delay(unsigned long delay)
1342 schedule_delayed_work(&fw_cache.work,
1343 msecs_to_jiffies(delay));
1346 static int fw_pm_notify(struct notifier_block *notify_block,
1347 unsigned long mode, void *unused)
1350 case PM_HIBERNATION_PREPARE:
1351 case PM_SUSPEND_PREPARE:
1352 device_cache_fw_images();
1355 case PM_POST_SUSPEND:
1356 case PM_POST_HIBERNATION:
1357 case PM_POST_RESTORE:
1359 * In case that system sleep failed and syscore_suspend is
1362 mutex_lock(&fw_lock);
1363 fw_cache.state = FW_LOADER_NO_CACHE;
1364 mutex_unlock(&fw_lock);
1366 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1373 /* stop caching firmware once syscore_suspend is reached */
1374 static int fw_suspend(void)
1376 fw_cache.state = FW_LOADER_NO_CACHE;
1380 static struct syscore_ops fw_syscore_ops = {
1381 .suspend = fw_suspend,
1384 static int fw_cache_piggyback_on_request(const char *name)
1390 static void __init fw_cache_init(void)
1392 spin_lock_init(&fw_cache.lock);
1393 INIT_LIST_HEAD(&fw_cache.head);
1394 fw_cache.state = FW_LOADER_NO_CACHE;
1396 #ifdef CONFIG_PM_SLEEP
1397 spin_lock_init(&fw_cache.name_lock);
1398 INIT_LIST_HEAD(&fw_cache.fw_names);
1401 init_waitqueue_head(&fw_cache.wait_queue);
1402 INIT_DELAYED_WORK(&fw_cache.work,
1403 device_uncache_fw_images_work);
1405 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1406 register_pm_notifier(&fw_cache.pm_notify);
1408 register_syscore_ops(&fw_syscore_ops);
1412 static int __init firmware_class_init(void)
1415 return class_register(&firmware_class);
1418 static void __exit firmware_class_exit(void)
1420 #ifdef CONFIG_PM_SLEEP
1421 unregister_syscore_ops(&fw_syscore_ops);
1422 unregister_pm_notifier(&fw_cache.pm_notify);
1424 class_unregister(&firmware_class);
1427 fs_initcall(firmware_class_init);
1428 module_exit(firmware_class_exit);
1430 EXPORT_SYMBOL(release_firmware);
1431 EXPORT_SYMBOL(request_firmware);
1432 EXPORT_SYMBOL(request_firmware_nowait);
1433 EXPORT_SYMBOL_GPL(cache_firmware);
1434 EXPORT_SYMBOL_GPL(uncache_firmware);