日期:2014-05-16  浏览次数:20815 次

linux设备驱动入门---globalmem字符设备驱动
/* globalmem字符设备驱动 
 * 作者:liwei.cai
 * 日期:2012-08-03
 * globalmem.c
 */
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>

#define GLOBALMEM_SIZE 0x1000 /* 全局内存大小:4KB */
#define MEM_CLEAR 0x01 /* 清零全局内存 */
#define GLOBALMEM_MAJOR 250 /* 预设的globalmem 的主设备号 */

static int globalmem_major = GLOBALMEM_MAJOR;

/* globalmem设备结构体 */
struct globalmem_dev
{
	struct cdev cdev; /* cdev结构体 */
	unsigned char mem[GLOBALMEM_SIZE]; /* 全局变量 */
};

//struct globalmem_dev dev; /* 设备结构体实例 */
struct globalmem_dev *globalmem_devp; /* 设备结构体指针 */

/* 文件打开函数 */
int globalmem_open(struct inode *inode, struct file *filp)
{
	/* 将设备结构体指针赋值给文件私有数据指针  */
	filp->private_data = globalmem_devp;
	return 0;
}

/* 文件释放函数 */
int globalmem_release(struct inode *inode, struct file *filp)
{
	return 0;
}

/* globalmem 设备驱动的读函数 */
static ssize_t globalmem_read(struct file *filp, 
		char __user *buf, size_t size, loff_t *ppos)
{
	unsigned long p = *ppos;
	unsigned int count = size;
	int ret = 0;
	struct globalmem_dev *dev = filp->private_data;//获得设备结构体指针

	/* 分析和获取有效的读长度 */
	if(p >= GLOBALMEM_SIZE) //要读的偏移位置越界
	{
		return count ? -ENXIO : 0;
	}
	if (count > GLOBALMEM_SIZE - p) //要读的字节太大
	{
		count = GLOBALMEM_SIZE - p;
	}

        /* 内核控件-->用户空间 */
	if (copy_to_user(buf, (void*)(dev->mem + p), count))
	{
		ret = -EFAULT;
	}
	else
	{
		*ppos += count;
		ret = count;

		printk(KERN_INFO "read %d bytes(s) from %ld \n", count, p);
	}

	return ret;
}

/* globalmem 设备驱动的写函数 */
static ssize_t globalmem_write(struct file *filp, 
		const char __user *buf, size_t size, loff_t *ppos)
{
	unsigned long p = *ppos;
	unsigned int count = size;
	int ret = 0;
	struct globalmem_dev *dev = filp->private_data;//获取设备结构体指针

	/* 分析和获取有效的写长度 */
	if (p >= GLOBALMEM_SIZE) // 要写的偏移位置越界
	{
		return count ? -ENXIO : 0;
	}
	if (count > GLOBALMEM_SIZE - p) // 要写的字节数太多
	{
		count = GLOBALMEM_SIZE - p;
	}

	/* 用户空间 --> 内核空间 */
	if (copy_from_user(dev->mem + p, buf, count))
	{
		ret = -EFAULT;
	}
	else
	{
		*ppos += count;
		ret = count;

		printk(KERN_INFO "written %d bytes(s) from %ld \n", count, p);
	}
	return ret;
}

/* seek()函数对文件定位的起始位置可以是文件开头(SEEK_SET, 0)、
 * 当前位置(SEEK_CUR,1)和文件尾(SEEK_END,2)
 */
static loff_t globalmem_llseek(struct file *filp,
		loff_t offset, int orig)
{
	loff_t ret;
	switch(orig)
	{
		case 0: //从文件开头开始偏移
			if (offset < 0)
			{
				ret = -EINVAL;
				break;
			}
			if ((unsigned int)offset > GLOBALMEM_SIZE)// 偏移越界
			{
				ret = -EINVAL;
				break;
			}
			filp->f_pos = (unsigned int)offset;
			ret = filp->f_pos;
			break;
		case 1: //从当前位置开始偏移
			if((filp->f_pos + offset) > GLOBALMEM_SIZE) //偏移越界
			{
				ret = -EINVAL;
				break;
			}
			if ((filp->f_pos + offset) < 0)
			{
				ret = -EINVAL;
				break;
			}
			filp->f_pos += offset;
			ret = filp->f_pos;
			break;
		default:
			ret = -EINVAL;
	}
	return ret;
}

/* ioctl()函数接受MEM_CLEAR命令,这个命令会将全局内存的有效数据长度清零
 * 对于设备不支持的命令,返回-EINCVAL
 */
static int globalmem_ioctl(struct inode *inodep, struct file *filp, 
		unsigned int cmd, unsigned long arg)
{
	struct globalmem_dev *dev = filp->private_data; /* 获得设备结构体指针 */
	switch(cmd)
	{
		case MEM_CLEAR://清除全局内存
			memset(dev->mem, 0, GLOBALMEM_SIZE);
			printk(KERN_INFO "globalmem is set to zero\n");
			break;

		default:
			return -EINVAL;
	}
	return 0;
}

/* 与globalmem的cdev关联的file_operations结构体 */
static const struct file_operations globalmem_fops =
{
	.owner = THIS_MODULE,
	.llseek = globalmem_llseek,
	.read = globalmem_read,
	.write = globalmem_write,
	.ioctl = globalmem_ioctl,
	.open =globalmem_open,
	.release =