Linux字符设备驱动-完整版

之前转载的那篇字符设备驱动程序有个bug,就是模块安装后,不能够正常卸载,卸载完成后cat /proc/devices,则出现段错误。

以下代码是在redhat9 (内核为Linux-2.4.20-8)下面编译通过的程序


/**********字符设备驱动的实现**********/

#define DEVICE_NAME "rwbuf"//字符设备名称

#define REQ_CLEAR 0x909090 //清除设备中写入的字符串
#define REQ_GETLEN 0x808080 //返回设备中有效数据的长度
#define REQ_GETCOUNT 0x707070 //返回设备被写入的次数

#define RWBUF_SIZE 1024 //字符设备中字符串长度的最大值

#include <linux/kernel.h> //内核程序使用
#include <linux/module.h>//模块使用
#include <linux/fs.h> // struct file_operations
#include <asm/uaccess.h> // copy_to_user

MODULE_LICENSE("GPL");

//全局变量
static char rwbuf[RWBUF_SIZE+1]="xidian"; //虚拟的字符数组设备
static int rwlen=6; 
static int inuse=0; //同一时间段内只允许一个进程使用
static int writeCount=0;//累计写入的次数

//函数声明
static int rwbuf_open(struct inode *inode, struct file *filep);
static int rwbuf_close(struct inode *inode, struct file *filep);
static ssize_t rwbuf_read(struct file *filep, char *buf, size_t count, loff_t *ppos);
static ssize_t rwbuf_write(struct file *filep, const char *buf, size_t, loff_t *ppos);
static int rwbuf_ioctl(struct inode *inode, struct file *filep, unsigned int cum, unsigned long arg);

//不是标准C支持的格式,是GUN的扩展语法
static struct file_operations rwbuf_fops=
{
	open: rwbuf_open,
	release: rwbuf_close,
	read: rwbuf_read,
	write: rwbuf_write,
	ioctl: rwbuf_ioctl,
};

//加载模块
int init_module()
{
	printk("Hello world\n");
	if(register_chrdev(253, DEVICE_NAME, &rwbuf_fops))
	{
		printk("register error \n");
		return -1;
	}
	printk("register OK\n");
	return 0;
}


//卸载模块
void cleanup_module()
{
	if(unregister_chrdev(253, DEVICE_NAME)!=0)
		printk("unreg err\n");
	else
		printk("unreg ok\n");
	printk("bye\n");
}


//打开字符设备
static int rwbuf_open(struct inode *inode, struct file *filep)
{
	if(inuse==1)
		return -1;
	inuse=1;

	MOD_INC_USE_COUNT;
	return 0;
}


//关闭字符设备
static int rwbuf_close(struct inode *inode, struct file *filep)
{
	inuse=0;
	MOD_DEC_USE_COUNT;
	return 0;
}



//写字符设备
static ssize_t rwbuf_write(struct file *filep, const char *buf, size_t count, loff_t *ppos)
{
	//判断写入的长度是否有效
	if(count>RWBUF_SIZE)
	{
		count=RWBUF_SIZE;
	}

	copy_from_user(rwbuf, buf, count);

	if(count==RWBUF_SIZE)
	{
		rwbuf[RWBUF_SIZE]='\0';
	}

	rwlen =count;

	writeCount++;

	return count;
}


//读字符设备
static ssize_t rwbuf_read(struct file *filep, char *buf, size_t count, loff_t *ppos)
{
	//判断读取的长度是否有效
	if(count>=RWBUF_SIZE)
	{
		count=RWBUF_SIZE;
		buf[RWBUF_SIZE]='\0';
	}
	copy_to_user(buf, rwbuf, count);//从内核空间复制到用户空间

	return count;
}


//控制字符设备
static int rwbuf_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg)
{
	if(cmd==REQ_CLEAR)
	{
		rwlen=0; //将字符串的长度设为0,以表示清空字符串
		printk("rwbuf in kernel zero-ed\n");
	}

	if(cmd==REQ_GETLEN)
	{
		return rwlen;
	}

	if(cmd==REQ_GETCOUNT)
	{
		return writeCount;
	}
	return 0;
}

将以上代码保存为myCharDev.c 文件。

编译采用命令:gcc -c myCharDev.c -I/usr/src/linux-2.4.20-8/include -D__KERNEL__ -DMODULE -Wall

生成的内核模块为:myCharDev.o

加载模块: insmod myCharDev.o

查看设备号是否分配成功:cat /proc/devices 如果 rwbuf 对应的设备号是253,则表示设备号分配成功。

创建设备文档:mknod /dev/rwbuf c 253 0

如此,字符设备成功驱动加载完成……


卸载命令:rmmod myCharDev

然后应该把设备文档也删除:rm -f /dev/rwbuf



测试程序稍候给出~~





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值