1627 lines · 37.6 KB · C
RawDownload
/* * 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 <linux/reboot.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,}; 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;	void *data;	size_t size;#ifdef CONFIG_FW_LOADER_USER_HELPER	bool is_paged_buf;	bool need_uevent;	struct page **pages;	int nr_pages;	int page_array_size;	struct list_head pending_list;#endif	char fw_id[];}; struct fw_cache_entry {	struct list_head list;	char name[];}; 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);#ifdef CONFIG_FW_LOADER_USER_HELPER	INIT_LIST_HEAD(&buf->pending_list);#endif 	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 void __fw_free_buf(struct kref *ref){	struct firmware_buf *buf = to_fwbuf(ref);	struct firmware_cache *fwc = buf->fwc; 	pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",		 __func__, buf->fw_id, buf, buf->data,		 (unsigned int)buf->size); 	list_del(&buf->list);	spin_unlock(&fwc->lock); #ifdef CONFIG_FW_LOADER_USER_HELPER	if (buf->is_paged_buf) {		int i;		vunmap(buf->data);		for (i = 0; i < buf->nr_pages; i++)			__free_page(buf->pages[i]);		kfree(buf->pages);	} else#endif		vfree(buf->data);	kfree(buf);} static void fw_free_buf(struct firmware_buf *buf){	struct firmware_cache *fwc = buf->fwc;	spin_lock(&fwc->lock);	if (!kref_put(&buf->ref, __fw_free_buf))		spin_unlock(&fwc->lock);} /* direct firmware loading support */static char fw_path_para[256];static const char * const fw_path[] = {	fw_path_para,	"/lib/firmware/updates/" UTS_RELEASE,	"/lib/firmware/updates",	"/lib/firmware/" UTS_RELEASE,	"/lib/firmware"}; /* * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH' * from kernel command line because firmware_class is generally built in * kernel instead of module. */module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path"); /* Don't inline this: 'struct kstat' is biggish */static noinline_for_stack long fw_file_size(struct file *file){	struct kstat st;	if (vfs_getattr(&file->f_path, &st))		return -1;	if (!S_ISREG(st.mode))		return -1;	if (st.size != (long)st.size)		return -1;	return st.size;} static int fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf){	long size;	char *buf;	int rc; 	size = fw_file_size(file);	if (size <= 0)		return -EINVAL;	buf = vmalloc(size);	if (!buf)		return -ENOMEM;	rc = kernel_read(file, 0, buf, size);	if (rc != size) {		if (rc > 0)			rc = -EIO;		vfree(buf);		return rc;	}	fw_buf->data = buf;	fw_buf->size = size;	return 0;} static int fw_get_filesystem_firmware(struct device *device,				       struct firmware_buf *buf){	int i;	int rc = -ENOENT;	char *path = __getname(); 	for (i = 0; i < ARRAY_SIZE(fw_path); i++) {		struct file *file; 		/* skip the unset customized path */		if (!fw_path[i][0])			continue; 		snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id); 		file = filp_open(path, O_RDONLY, 0);		if (IS_ERR(file))			continue;		rc = fw_read_file_contents(file, buf);		fput(file);		if (rc)			dev_warn(device, "firmware, attempted to load %s, but failed with error %d\n",				path, rc);		else			break;	}	__putname(path); 	if (!rc) {		dev_dbg(device, "firmware: direct-loading firmware %s\n",			buf->fw_id);		mutex_lock(&fw_lock);		set_bit(FW_STATUS_DONE, &buf->status);		complete_all(&buf->completion);		mutex_unlock(&fw_lock);	} 	return rc;} /* 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);} /* store the pages buffer info firmware from buf */static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw){	fw->priv = buf;#ifdef CONFIG_FW_LOADER_USER_HELPER	fw->pages = buf->pages;#endif	fw->size = buf->size;	fw->data = buf->data; 	pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",		 __func__, buf->fw_id, buf, buf->data,		 (unsigned int)buf->size);} #ifdef CONFIG_PM_SLEEPstatic void fw_name_devm_release(struct device *dev, void *res){	struct fw_name_devm *fwn = res; 	if (fwn->magic == (unsigned long)&fw_cache)		pr_debug("%s: fw_name-%s devm-%p released\n",				__func__, fwn->name, res);} static int fw_devm_match(struct device *dev, void *res,		void *match_data){	struct fw_name_devm *fwn = res; 	return (fwn->magic == (unsigned long)&fw_cache) &&		!strcmp(fwn->name, match_data);} static struct fw_name_devm *fw_find_devm_name(struct device *dev,		const char *name){	struct fw_name_devm *fwn; 	fwn = devres_find(dev, fw_name_devm_release,			  fw_devm_match, (void *)name);	return fwn;} /* add firmware name into devres list */static int fw_add_devm_name(struct device *dev, const char *name){	struct fw_name_devm *fwn; 	fwn = fw_find_devm_name(dev, name);	if (fwn)		return 1; 	fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +			   strlen(name) + 1, GFP_KERNEL);	if (!fwn)		return -ENOMEM; 	fwn->magic = (unsigned long)&fw_cache;	strcpy(fwn->name, name);	devres_add(dev, fwn); 	return 0;}#elsestatic int fw_add_devm_name(struct device *dev, const char *name){	return 0;}#endif  /* * user-mode helper code */#ifdef CONFIG_FW_LOADER_USER_HELPERstruct firmware_priv {	struct delayed_work timeout_work;	bool nowait;	struct device dev;	struct firmware_buf *buf;	struct firmware *fw;}; 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_buf *buf){	/*	 * There is a small window in which user can write to 'loading'	 * between loading done and disappearance of 'loading'	 */	if (test_bit(FW_STATUS_DONE, &buf->status))		return; 	list_del_init(&buf->pending_list);	set_bit(FW_STATUS_ABORT, &buf->status);	complete_all(&buf->completion);} static void fw_load_abort(struct firmware_priv *fw_priv){	struct firmware_buf *buf = fw_priv->buf; 	__fw_load_abort(buf); 	/* avoid user action after loading abort */	fw_priv->buf = NULL;} #define is_fw_load_aborted(buf)	\	test_bit(FW_STATUS_ABORT, &(buf)->status) static LIST_HEAD(pending_fw_head); /* reboot notifier for avoid deadlock with usermode_lock */static int fw_shutdown_notify(struct notifier_block *unused1,			      unsigned long unused2, void *unused3){	mutex_lock(&fw_lock);	while (!list_empty(&pending_fw_head))		__fw_load_abort(list_first_entry(&pending_fw_head,					       struct firmware_buf,					       pending_list));	mutex_unlock(&fw_lock);	return NOTIFY_DONE;} static struct notifier_block fw_shutdown_nb = {	.notifier_call = fw_shutdown_notify,}; static ssize_t timeout_show(struct class *class, struct class_attribute *attr,			    char *buf){	return sprintf(buf, "%d\n", loading_timeout);}

Lines 1–500 of 1627.