/* * firmware_class.c - Multi purpose firmware loading support * * Copyright (c) 2003 Manuel Estrada Sainz * * Please see Documentation/firmware_class/ for more information. * */ #include <linux/capability.h>#include <linux/device.h>#include <linux/module.h>#include <linux/init.h>#include <linux/timer.h>#include <linux/vmalloc.h>#include <linux/interrupt.h>#include <linux/bitops.h>#include <linux/mutex.h>#include <linux/workqueue.h>#include <linux/highmem.h>#include <linux/firmware.h>#include <linux/slab.h>#include <linux/sched.h>#include <linux/file.h>#include <linux/list.h>#include <linux/async.h>#include <linux/pm.h>#include <linux/suspend.h>#include <linux/syscore_ops.h> #include <generated/utsrelease.h> #include "base.h" MODULE_AUTHOR("Manuel Estrada Sainz");MODULE_DESCRIPTION("Multi purpose firmware loading support");MODULE_LICENSE("GPL"); /* Builtin firmware support */ #ifdef CONFIG_FW_LOADER extern struct builtin_fw __start_builtin_fw[];extern struct builtin_fw __end_builtin_fw[]; static bool fw_get_builtin_firmware(struct firmware *fw, const char *name){ struct builtin_fw *b_fw; for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) { if (strcmp(name, b_fw->name) == 0) { fw->size = b_fw->size; fw->data = b_fw->data; return true; } } return false;} static bool fw_is_builtin_firmware(const struct firmware *fw){ struct builtin_fw *b_fw; for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) if (fw->data == b_fw->data) return true; return false;} #else /* Module case - no builtin firmware support */ static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name){ return false;} static inline bool fw_is_builtin_firmware(const struct firmware *fw){ return false;}#endif enum { FW_STATUS_LOADING, FW_STATUS_DONE, FW_STATUS_ABORT,}; enum fw_buf_fmt { VMALLOC_BUF, /* used in direct loading */ PAGE_BUF, /* used in loading via userspace */}; static int loading_timeout = 60; /* In seconds */ static inline long firmware_loading_timeout(void){ return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;} struct firmware_cache { /* firmware_buf instance will be added into the below list */ spinlock_t lock; struct list_head head; int state; #ifdef CONFIG_PM_SLEEP /* * Names of firmware images which have been cached successfully * will be added into the below list so that device uncache * helper can trace which firmware images have been cached * before. */ spinlock_t name_lock; struct list_head fw_names; struct delayed_work work; struct notifier_block pm_notify;#endif}; struct firmware_buf { struct kref ref; struct list_head list; struct completion completion; struct firmware_cache *fwc; unsigned long status; enum fw_buf_fmt fmt; void *data; size_t size; struct page **pages; int nr_pages; int page_array_size; char fw_id[];}; struct fw_cache_entry { struct list_head list; char name[];}; struct firmware_priv { struct timer_list timeout; bool nowait; struct device dev; struct firmware_buf *buf; struct firmware *fw;}; struct fw_name_devm { unsigned long magic; char name[];}; #define to_fwbuf(d) container_of(d, struct firmware_buf, ref) #define FW_LOADER_NO_CACHE 0#define FW_LOADER_START_CACHE 1 static int fw_cache_piggyback_on_request(const char *name); /* fw_lock could be moved to 'struct firmware_priv' but since it is just * guarding for corner cases a global lock should be OK */static DEFINE_MUTEX(fw_lock); static struct firmware_cache fw_cache; static struct firmware_buf *__allocate_fw_buf(const char *fw_name, struct firmware_cache *fwc){ struct firmware_buf *buf; buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC); if (!buf) return buf; kref_init(&buf->ref); strcpy(buf->fw_id, fw_name); buf->fwc = fwc; init_completion(&buf->completion); buf->fmt = VMALLOC_BUF; pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf); return buf;} static struct firmware_buf *__fw_lookup_buf(const char *fw_name){ struct firmware_buf *tmp; struct firmware_cache *fwc = &fw_cache; list_for_each_entry(tmp, &fwc->head, list) if (!strcmp(tmp->fw_id, fw_name)) return tmp; return NULL;} static int fw_lookup_and_allocate_buf(const char *fw_name, struct firmware_cache *fwc, struct firmware_buf **buf){ struct firmware_buf *tmp; spin_lock(&fwc->lock); tmp = __fw_lookup_buf(fw_name); if (tmp) { kref_get(&tmp->ref); spin_unlock(&fwc->lock); *buf = tmp; return 1; } tmp = __allocate_fw_buf(fw_name, fwc); if (tmp) list_add(&tmp->list, &fwc->head); spin_unlock(&fwc->lock); *buf = tmp; return tmp ? 0 : -ENOMEM;} static struct firmware_buf *fw_lookup_buf(const char *fw_name){ struct firmware_buf *tmp; struct firmware_cache *fwc = &fw_cache; spin_lock(&fwc->lock); tmp = __fw_lookup_buf(fw_name); spin_unlock(&fwc->lock); return tmp;} static void __fw_free_buf(struct kref *ref){ struct firmware_buf *buf = to_fwbuf(ref); struct firmware_cache *fwc = buf->fwc; int i; pr_debug("%s: fw-%s buf=%p data=%p size=%u\n", __func__, buf->fw_id, buf, buf->data, (unsigned int)buf->size); spin_lock(&fwc->lock); list_del(&buf->list); spin_unlock(&fwc->lock); if (buf->fmt == PAGE_BUF) { vunmap(buf->data); for (i = 0; i < buf->nr_pages; i++) __free_page(buf->pages[i]); kfree(buf->pages); } else vfree(buf->data); kfree(buf);} static void fw_free_buf(struct firmware_buf *buf){ kref_put(&buf->ref, __fw_free_buf);} /* direct firmware loading support */static const char *fw_path[] = { "/lib/firmware/updates/" UTS_RELEASE, "/lib/firmware/updates", "/lib/firmware/" UTS_RELEASE, "/lib/firmware"}; /* Don't inline this: 'struct kstat' is biggish */static noinline long fw_file_size(struct file *file){ struct kstat st; if (vfs_getattr(file->f_path.mnt, file->f_path.dentry, &st)) return -1; if (!S_ISREG(st.mode)) return -1; if (st.size != (long)st.size) return -1; return st.size;} static bool fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf){ long size; char *buf; size = fw_file_size(file); if (size < 0) return false; buf = vmalloc(size); if (!buf) return false; if (kernel_read(file, 0, buf, size) != size) { vfree(buf); return false; } fw_buf->data = buf; fw_buf->size = size; return true;} static bool fw_get_filesystem_firmware(struct firmware_buf *buf){ int i; bool success = false; char *path = __getname(); for (i = 0; i < ARRAY_SIZE(fw_path); i++) { struct file *file; snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id); file = filp_open(path, O_RDONLY, 0); if (IS_ERR(file)) continue; success = fw_read_file_contents(file, buf); fput(file); if (success) break; } __putname(path); return success;} static struct firmware_priv *to_firmware_priv(struct device *dev){ return container_of(dev, struct firmware_priv, dev);} static void fw_load_abort(struct firmware_priv *fw_priv){ struct firmware_buf *buf = fw_priv->buf; set_bit(FW_STATUS_ABORT, &buf->status); complete_all(&buf->completion);} static ssize_t firmware_timeout_show(struct class *class, struct class_attribute *attr, char *buf){ return sprintf(buf, "%d\n", loading_timeout);} /** * firmware_timeout_store - set number of seconds to wait for firmware * @class: device class pointer * @attr: device attribute pointer * @buf: buffer to scan for timeout value * @count: number of bytes in @buf * * Sets the number of seconds to wait for the firmware. Once * this expires an error will be returned to the driver and no * firmware will be provided. * * Note: zero means 'wait forever'. **/static ssize_t firmware_timeout_store(struct class *class, struct class_attribute *attr, const char *buf, size_t count){ loading_timeout = simple_strtol(buf, NULL, 10); if (loading_timeout < 0) loading_timeout = 0; return count;} static struct class_attribute firmware_class_attrs[] = { __ATTR(timeout, S_IWUSR | S_IRUGO, firmware_timeout_show, firmware_timeout_store), __ATTR_NULL}; static void fw_dev_release(struct device *dev){ struct firmware_priv *fw_priv = to_firmware_priv(dev); kfree(fw_priv); module_put(THIS_MODULE);} static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env){ struct firmware_priv *fw_priv = to_firmware_priv(dev); if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id)) return -ENOMEM; if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout)) return -ENOMEM; if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait)) return -ENOMEM; return 0;} static struct class firmware_class = { .name = "firmware", .class_attrs = firmware_class_attrs, .dev_uevent = firmware_uevent, .dev_release = fw_dev_release,}; static ssize_t firmware_loading_show(struct device *dev, struct device_attribute *attr, char *buf){ struct firmware_priv *fw_priv = to_firmware_priv(dev); int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status); return sprintf(buf, "%d\n", loading);} /* firmware holds the ownership of pages */static void firmware_free_data(const struct firmware *fw){ /* Loaded directly? */ if (!fw->priv) { vfree(fw->data); return; } fw_free_buf(fw->priv);} /* Some architectures don't have PAGE_KERNEL_RO */#ifndef PAGE_KERNEL_RO#define PAGE_KERNEL_RO PAGE_KERNEL#endif /* one pages buffer should be mapped/unmapped only once */static int fw_map_pages_buf(struct firmware_buf *buf){ if (buf->fmt != PAGE_BUF) return 0; if (buf->data) vunmap(buf->data); buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO); if (!buf->data) return -ENOMEM; return 0;} /** * firmware_loading_store - set value in the 'loading' control file * @dev: device pointer * @attr: device attribute pointer * @buf: buffer to scan for loading control value * @count: number of bytes in @buf * * The relevant values are: * * 1: Start a load, discarding any previous partial load. * 0: Conclude the load and hand the data to the driver code. * -1: Conclude the load with an error and discard any written data. **/static ssize_t firmware_loading_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count){ struct firmware_priv *fw_priv = to_firmware_priv(dev); struct firmware_buf *fw_buf = fw_priv->buf; int loading = simple_strtol(buf, NULL, 10); int i; mutex_lock(&fw_lock); if (!fw_buf) goto out; switch (loading) { case 1: /* discarding any previous partial load */ if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) { for (i = 0; i < fw_buf->nr_pages; i++) __free_page(fw_buf->pages[i]); kfree(fw_buf->pages); fw_buf->pages = NULL; fw_buf->page_array_size = 0; fw_buf->nr_pages = 0; set_bit(FW_STATUS_LOADING, &fw_buf->status); } break; case 0: if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) { set_bit(FW_STATUS_DONE, &fw_buf->status); clear_bit(FW_STATUS_LOADING, &fw_buf->status); /* * Several loading requests may be pending on * one same firmware buf, so let all requests * see the mapped 'buf->data' once the loading * is completed.Lines 1–500 of 1468. Load more
/* * firmware_class.c - Multi purpose firmware loading support * * Copyright (c) 2003 Manuel Estrada Sainz * * Please see Documentation/firmware_class/ for more information. * */ #include <linux/capability.h> #include <linux/device.h> #include <linux/module.h> #include <linux/init.h> #include <linux/timer.h> #include <linux/vmalloc.h> #include <linux/interrupt.h> #include <linux/bitops.h> #include <linux/mutex.h> #include <linux/workqueue.h> #include <linux/highmem.h> #include <linux/firmware.h> #include <linux/slab.h> #include <linux/sched.h> #include <linux/file.h> #include <linux/list.h> #include <linux/async.h> #include <linux/pm.h> #include <linux/suspend.h> #include <linux/syscore_ops.h> #include <generated/utsrelease.h> #include "base.h" MODULE_AUTHOR("Manuel Estrada Sainz"); MODULE_DESCRIPTION("Multi purpose firmware loading support"); MODULE_LICENSE("GPL"); /* Builtin firmware support */ #ifdef CONFIG_FW_LOADER extern struct builtin_fw __start_builtin_fw[]; extern struct builtin_fw __end_builtin_fw[]; static bool fw_get_builtin_firmware(struct firmware *fw, const char *name) { struct builtin_fw *b_fw; for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) { if (strcmp(name, b_fw->name) == 0) { fw->size = b_fw->size; fw->data = b_fw->data; return true; } } return false; } static bool fw_is_builtin_firmware(const struct firmware *fw) { struct builtin_fw *b_fw; for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) if (fw->data == b_fw->data) return true; return false; } #else /* Module case - no builtin firmware support */ static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name) { return false; } static inline bool fw_is_builtin_firmware(const struct firmware *fw) { return false; } #endif enum { FW_STATUS_LOADING, FW_STATUS_DONE, FW_STATUS_ABORT, }; enum fw_buf_fmt { VMALLOC_BUF, /* used in direct loading */ PAGE_BUF, /* used in loading via userspace */ }; static int loading_timeout = 60; /* In seconds */ static inline long firmware_loading_timeout(void) { return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT; } struct firmware_cache { /* firmware_buf instance will be added into the below list */ spinlock_t lock; struct list_head head; int state; #ifdef CONFIG_PM_SLEEP /* * Names of firmware images which have been cached successfully * will be added into the below list so that device uncache * helper can trace which firmware images have been cached * before. */ spinlock_t name_lock; struct list_head fw_names; struct delayed_work work; struct notifier_block pm_notify; #endif }; struct firmware_buf { struct kref ref; struct list_head list; struct completion completion; struct firmware_cache *fwc; unsigned long status; enum fw_buf_fmt fmt; void *data; size_t size; struct page **pages; int nr_pages; int page_array_size; char fw_id[]; }; struct fw_cache_entry { struct list_head list; char name[]; }; struct firmware_priv { struct timer_list timeout; bool nowait; struct device dev; struct firmware_buf *buf; struct firmware *fw; }; struct fw_name_devm { unsigned long magic; char name[]; }; #define to_fwbuf(d) container_of(d, struct firmware_buf, ref) #define FW_LOADER_NO_CACHE 0 #define FW_LOADER_START_CACHE 1 static int fw_cache_piggyback_on_request(const char *name); /* fw_lock could be moved to 'struct firmware_priv' but since it is just * guarding for corner cases a global lock should be OK */ static DEFINE_MUTEX(fw_lock); static struct firmware_cache fw_cache; static struct firmware_buf *__allocate_fw_buf(const char *fw_name, struct firmware_cache *fwc) { struct firmware_buf *buf; buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC); if (!buf) return buf; kref_init(&buf->ref); strcpy(buf->fw_id, fw_name); buf->fwc = fwc; init_completion(&buf->completion); buf->fmt = VMALLOC_BUF; pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf); return buf; } static struct firmware_buf *__fw_lookup_buf(const char *fw_name) { struct firmware_buf *tmp; struct firmware_cache *fwc = &fw_cache; list_for_each_entry(tmp, &fwc->head, list) if (!strcmp(tmp->fw_id, fw_name)) return tmp; return NULL; } static int fw_lookup_and_allocate_buf(const char *fw_name, struct firmware_cache *fwc, struct firmware_buf **buf) { struct firmware_buf *tmp; spin_lock(&fwc->lock); tmp = __fw_lookup_buf(fw_name); if (tmp) { kref_get(&tmp->ref); spin_unlock(&fwc->lock); *buf = tmp; return 1; } tmp = __allocate_fw_buf(fw_name, fwc); if (tmp) list_add(&tmp->list, &fwc->head); spin_unlock(&fwc->lock); *buf = tmp; return tmp ? 0 : -ENOMEM; } static struct firmware_buf *fw_lookup_buf(const char *fw_name) { struct firmware_buf *tmp; struct firmware_cache *fwc = &fw_cache; spin_lock(&fwc->lock); tmp = __fw_lookup_buf(fw_name); spin_unlock(&fwc->lock); return tmp; } static void __fw_free_buf(struct kref *ref) { struct firmware_buf *buf = to_fwbuf(ref); struct firmware_cache *fwc = buf->fwc; int i; pr_debug("%s: fw-%s buf=%p data=%p size=%u\n", __func__, buf->fw_id, buf, buf->data, (unsigned int)buf->size); spin_lock(&fwc->lock); list_del(&buf->list); spin_unlock(&fwc->lock); if (buf->fmt == PAGE_BUF) { vunmap(buf->data); for (i = 0; i < buf->nr_pages; i++) __free_page(buf->pages[i]); kfree(buf->pages); } else vfree(buf->data); kfree(buf); } static void fw_free_buf(struct firmware_buf *buf) { kref_put(&buf->ref, __fw_free_buf); } /* direct firmware loading support */ static const char *fw_path[] = { "/lib/firmware/updates/" UTS_RELEASE, "/lib/firmware/updates", "/lib/firmware/" UTS_RELEASE, "/lib/firmware" }; /* Don't inline this: 'struct kstat' is biggish */ static noinline long fw_file_size(struct file *file) { struct kstat st; if (vfs_getattr(file->f_path.mnt, file->f_path.dentry, &st)) return -1; if (!S_ISREG(st.mode)) return -1; if (st.size != (long)st.size) return -1; return st.size; } static bool fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf) { long size; char *buf; size = fw_file_size(file); if (size < 0) return false; buf = vmalloc(size); if (!buf) return false; if (kernel_read(file, 0, buf, size) != size) { vfree(buf); return false; } fw_buf->data = buf; fw_buf->size = size; return true; } static bool fw_get_filesystem_firmware(struct firmware_buf *buf) { int i; bool success = false; char *path = __getname(); for (i = 0; i < ARRAY_SIZE(fw_path); i++) { struct file *file; snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id); file = filp_open(path, O_RDONLY, 0); if (IS_ERR(file)) continue; success = fw_read_file_contents(file, buf); fput(file); if (success) break; } __putname(path); return success; } static struct firmware_priv *to_firmware_priv(struct device *dev) { return container_of(dev, struct firmware_priv, dev); } static void fw_load_abort(struct firmware_priv *fw_priv) { struct firmware_buf *buf = fw_priv->buf; set_bit(FW_STATUS_ABORT, &buf->status); complete_all(&buf->completion); } static ssize_t firmware_timeout_show(struct class *class, struct class_attribute *attr, char *buf) { return sprintf(buf, "%d\n", loading_timeout); } /** * firmware_timeout_store - set number of seconds to wait for firmware * @class: device class pointer * @attr: device attribute pointer * @buf: buffer to scan for timeout value * @count: number of bytes in @buf * * Sets the number of seconds to wait for the firmware. Once * this expires an error will be returned to the driver and no * firmware will be provided. * * Note: zero means 'wait forever'. **/ static ssize_t firmware_timeout_store(struct class *class, struct class_attribute *attr, const char *buf, size_t count) { loading_timeout = simple_strtol(buf, NULL, 10); if (loading_timeout < 0) loading_timeout = 0; return count; } static struct class_attribute firmware_class_attrs[] = { __ATTR(timeout, S_IWUSR | S_IRUGO, firmware_timeout_show, firmware_timeout_store), __ATTR_NULL }; static void fw_dev_release(struct device *dev) { struct firmware_priv *fw_priv = to_firmware_priv(dev); kfree(fw_priv); module_put(THIS_MODULE); } static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env) { struct firmware_priv *fw_priv = to_firmware_priv(dev); if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id)) return -ENOMEM; if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout)) return -ENOMEM; if (add_uevent_var(env, "ASYNC=%d", fw_priv->nowait)) return -ENOMEM; return 0; } static struct class firmware_class = { .name = "firmware", .class_attrs = firmware_class_attrs, .dev_uevent = firmware_uevent, .dev_release = fw_dev_release, }; static ssize_t firmware_loading_show(struct device *dev, struct device_attribute *attr, char *buf) { struct firmware_priv *fw_priv = to_firmware_priv(dev); int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status); return sprintf(buf, "%d\n", loading); } /* firmware holds the ownership of pages */ static void firmware_free_data(const struct firmware *fw) { /* Loaded directly? */ if (!fw->priv) { vfree(fw->data); return; } fw_free_buf(fw->priv); } /* Some architectures don't have PAGE_KERNEL_RO */ #ifndef PAGE_KERNEL_RO #define PAGE_KERNEL_RO PAGE_KERNEL #endif /* one pages buffer should be mapped/unmapped only once */ static int fw_map_pages_buf(struct firmware_buf *buf) { if (buf->fmt != PAGE_BUF) return 0; if (buf->data) vunmap(buf->data); buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO); if (!buf->data) return -ENOMEM; return 0; } /** * firmware_loading_store - set value in the 'loading' control file * @dev: device pointer * @attr: device attribute pointer * @buf: buffer to scan for loading control value * @count: number of bytes in @buf * * The relevant values are: * * 1: Start a load, discarding any previous partial load. * 0: Conclude the load and hand the data to the driver code. * -1: Conclude the load with an error and discard any written data. **/ static ssize_t firmware_loading_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { struct firmware_priv *fw_priv = to_firmware_priv(dev); struct firmware_buf *fw_buf = fw_priv->buf; int loading = simple_strtol(buf, NULL, 10); int i; mutex_lock(&fw_lock); if (!fw_buf) goto out; switch (loading) { case 1: /* discarding any previous partial load */ if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) { for (i = 0; i < fw_buf->nr_pages; i++) __free_page(fw_buf->pages[i]); kfree(fw_buf->pages); fw_buf->pages = NULL; fw_buf->page_array_size = 0; fw_buf->nr_pages = 0; set_bit(FW_STATUS_LOADING, &fw_buf->status); } break; case 0: if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) { set_bit(FW_STATUS_DONE, &fw_buf->status); clear_bit(FW_STATUS_LOADING, &fw_buf->status); /* * Several loading requests may be pending on * one same firmware buf, so let all requests * see the mapped 'buf->data' once the loading * is completed.