1544 lines · 35.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 <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;	struct page **pages;	int nr_pages;	int page_array_size;#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); 	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; 	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 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 device *device,				       struct firmware_buf *buf){	int i;	bool success = false;	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;		success = fw_read_file_contents(file, buf);		fput(file);		if (success)			break;	}	__putname(path); 	if (success) {		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 success;} /* 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_priv *fw_priv){	struct firmware_buf *buf = fw_priv->buf; 	set_bit(FW_STATUS_ABORT, &buf->status);	complete_all(&buf->completion);} #define is_fw_load_aborted(buf)	\	test_bit(FW_STATUS_ABORT, &(buf)->status) 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); 

Lines 1–500 of 1544.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t