linux字符设备驱动

linux字符设备驱动是linux三大驱动程序中比较简单的类型,本文将简单介绍字符设备驱动程序中的常用数据结构和编程方法。

1) cdev 结构体介绍

struct cdev {
	struct kobject kobj;
	struct module *owner;
	const struct file_operations *ops;
	struct list_head list;
	dev_t dev;
	unsigned int count;
};

linux 内核中使用 cdev结构体描述一个字符设备,该结构体的代码如上所示。

可以函数动态申请一个cdev结构的内存

struct cdev *cdev_alloc(void)

2) 操作函数集介绍

struct file_operations *ops; 是字符设备驱动的操作函数集,该函数是字符设备驱动程序的主体设计内容,最终被linux 系统中的open(),read(),write(),ioctl(),close()系统调用函数调用,所以操作函数集是应用程序和内核驱动程序的控制纽带。

struct file_operations结构体在内核中的定义为

struct file_operations {
	struct module *owner;
	loff_t (*llseek) (struct file *, loff_t, int);
	ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
	ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
	ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
	ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
	int (*readdir) (struct file *, void *, filldir_t);
	unsigned int (*poll) (struct file *, struct poll_table_struct *);
	int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
	long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
	long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
	int (*mmap) (struct file *, struct vm_area_struct *);
	int (*open) (struct inode *, struct file *);
	int (*flush) (struct file *, fl_owner_t id);
	int (*release) (struct inode *, struct file *);
	int (*fsync) (struct file *, struct dentry *, int datasync);
	int (*aio_fsync) (struct kiocb *, int datasync);
	int (*fasync) (int, struct file *, int);
	int (*lock) (struct file *, int, struct file_lock *);
	ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
	unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
	int (*check_flags)(int);
	int (*flock) (struct file *, int, struct file_lock *);
	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
	ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
	int (*setlease)(struct file *, long, struct file_lock **);
};
这里重点介绍几个操作函数集中成员函数的使用方法:

ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);

 驱动设备的读函数,struct file *类型的变量是文件结构体指针,char __user *类型的变量表示用户空间中的地址,用于保存驱动程序读取的数据,loff_t *类型的变量表示读取的字节数。由于用户空间不能直接访问内核空间的内存,故char __user *类型的变量不能直接保存读取到的数据。内核中提供了copy_form_user()  和 copy_to_user() 这两个函数实现了用户空间和内核空间的数据复制。两个函数的原型为:

copy_from_user(void *to, const void __user *from, unsigned long n)
copy_to_user(void __user *to, const void *from, unsigned long n)

ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);

 驱动设备的函数与读函数使用方法类似,需要注意的也是用户空间和内核空间的数据复制的问题。

  驱动中的读写函数分别与系统调用read()和write()对应 成功时返回读到或写入的字节数,返回0 表示EOF,返回负值表示错误信息。

  

 long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
  提供了设备控制函数,与系统调用中的ioctl ( ) 函数对应,该函数中 第二个参数表示命令字,该命令字可以通过下列宏生成


_IO(type,nr)
_IOR(type,nr,size)
_IOW(type,nr,size)
_IORW(type,nr,size)
其中

设备类型(type)序列号(nr)方向数据尺寸(size)
8位8位2位13/14位
通常设备类型定位为“幻数” MAGIC  可以是一个字符类型的值,内核中已经定义和使用了一些幻数,定义时应避免发生冲突。


驱动程序中需要定义一个操作函数集合实例,该实例的初始化就是把驱动设备具体的操作函数赋值给struct file_operations的成员。

static struct file_operations xxx_fops = 
{
	.open = xxx_open,
	.unlocked_ioctl = xxx_ioctl,	
};
在驱动函数中 需要使用   void cdev_init(struct cdev *cdev, const struct file_operations *fops)  函数建立 cdev 和   struct file_operations 的联系。


3) 字符设别号的申请

cdev结构中的 32位 dev 成员变量定义的字符设备的设备号,其中高12位为主设备号,低20位为次设备号,一般用不同类型设备有不同的主设备号,同类型的多个设备用次设备号来进行区分。

可以用主设备号和次设备号使用下列宏生成设备号

MKDEV(int major, int minor);

  也可以利用下列宏获得设备的 主设备号和次设备号

MAJOR(dev_t dev)
MINOR(dev_t dev)

设备号的申请方式有两种

int register_chrdev_region(dev_t from, unsigned count, const char *name)
int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name)
register_chrdev_region 是在 用于在已知起始设别号的情况下,用于静态向内核申请设别号

alloc_chrdev_region 可以向内核动态申请一个未被使用的设别号,防止设别号冲突的情况发生。

当然在字符设备注销后,申请的设备好也应该被释放,使用的函数为:

void unregister_chrdev_region(dev_t from, unsigned count)

4)字符设备注册  cdev_add

当申请到设备号并且使用cdev_init() 对 cdev 进行了初始化 后便可以向内核中注册字符设备了。注册函数为:

int cdev_add(struct cdev *p, dev_t dev, unsigned count)

在驱动模块的卸载函数中也有对应销毁字符设备的函数:

cdev_del(struct cdev *p)

下面给出一个字符设备驱动简单编程模版

struct cdev  cdev_xxx;
static struct file_operations led_fops = 
{
<span style="white-space:pre">	</span>.open = ...,
<span style="white-space:pre">	</span>.unlocked_ioctl = ...,<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>...
};
static int xxx_init()
{
	cdev_init(&cdev_xxx, &lxxx_fops);
	alloc_chrdev_region(&devno, 0, 1, "name");
	cdev_add(&cdev_xxx, devno, 1);
	
	return 0;
}


void led_exit()
{
	cdev_del(&cdev_xxx);
	unregister_chrdev_region(devno, 1);
	
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值