Skip to content
Snippets Groups Projects
firmware_class.c 33.6 KiB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
/*
 * firmware_class.c - Multi purpose firmware loading support
 *
 * Copyright (c) 2003 Manuel Estrada Sainz
Linus Torvalds's avatar
Linus Torvalds committed
 *
 * Please see Documentation/firmware_class/ for more information.
 *
 */

#include <linux/capability.h>
Linus Torvalds's avatar
Linus Torvalds committed
#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>
Linus Torvalds's avatar
Linus Torvalds committed
#include <linux/firmware.h>
#include <linux/list.h>
#include <linux/async.h>
#include <linux/pm.h>
#include <linux/syscore_ops.h>
#include "base.h"
Linus Torvalds's avatar
Linus Torvalds committed

MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds's avatar
Linus Torvalds committed
MODULE_DESCRIPTION("Multi purpose firmware loading support");
MODULE_LICENSE("GPL");

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 *fw)
{
	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->data = buf;
	fw->size = size;
	return true;
}

static bool fw_get_filesystem_firmware(struct firmware *fw, const char *name)
{
	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], name);

		file = filp_open(path, O_RDONLY, 0);
		if (IS_ERR(file))
			continue;
		success = fw_read_file_contents(file, fw);
		fput(file);
		if (success)
			break;
	}
	__putname(path);
	return success;
}

/* 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

Linus Torvalds's avatar
Linus Torvalds committed
enum {
	FW_STATUS_LOADING,
	FW_STATUS_DONE,
	FW_STATUS_ABORT,
};

static int loading_timeout = 60;	/* In seconds */
Linus Torvalds's avatar
Linus Torvalds committed

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;
#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;

	wait_queue_head_t wait_queue;
	int cnt;
	struct delayed_work work;

	struct notifier_block   pm_notify;
Linus Torvalds's avatar
Linus Torvalds committed

struct firmware_buf {
	struct kref ref;
	struct list_head list;
Linus Torvalds's avatar
Linus Torvalds committed
	struct completion completion;
	struct firmware_cache *fwc;
Linus Torvalds's avatar
Linus Torvalds committed
	unsigned long status;
	void *data;
	size_t size;
	struct page **pages;
	int nr_pages;
	int page_array_size;
struct fw_cache_entry {
	struct list_head list;
	char name[];
Loading
Loading full blame...