Linux内核中常使用cdev结构体描述一个字符设备,cdev结构定义如下:
struct cdev {
struct kobject kobj; 内嵌的kobject 对象
struct module *owner: 所属模块*
struct file_operations *ops;文件操作结构体
struct list_head list;
dev_t dev;设备号 为32位 其中12位为主设备号,20位为次设备号通过MAJOR(dev_t dev)获取主设备号MINOR(dev_t dev)获取次设备号 MKDEV(int major,int minor)通过主设备号和次设备号生成dev_t
unsigned int count;
}
file_operations定义了字符设备驱动提供给虚拟文件系统的接口函数
例如:
void cdev init (struct cdev *, struct file_operations *);用于初始化cdev的成员,并建立cdev和fileoperations之间的联系
struct cdev *cdev_alloc (void);用于动态申请一个cdev内存。
void cdev put (struct cdev *p) ;
int cdev_add (struct odev *, dev_t, unsigned);
void cdev_del (struct cdev *);
add和del函数用于添加和删除一个cdev完成字符设备的注册和注销
分配和释放设备号
在调用add函数时,首先调用register_chrdev_region()向系统申请设备号。register_chrdev_region()函数用于已知起始设备的设备号的情况,而 alloc_chrdev_region))用于设备号未知,向系统动态申请未被占用的设备号的情况,函数调用成功之后,会把得到的设备号放入第一个参数dev中。alloc_chrdev_region()相比于register_chrdev_region()的优点在于它会自动避开设备号重复的冲突。相应地,在调用cdev_del()函数从系统注销字符设备之后, unregister_chrdev_region()应该被调用以释放原先申请的设备号,这个函数的原型为:void unregister_chrdev_region (dev_t from, unsigned count);
file_operations结构体是字符设备驱动程序设计的主体内容。核心内容,相关kpi可网上查询。
字符设备驱动模块加载与卸载函数模块
/*设备结构体
struct xxx_dev_t (
struct cdev cdev;
...
xxx_dev
/*设备驱动模块加载函数
static intinit xxx_init (void){
cdev_init (&xxx_dev.odev, &xxx_fops) //初始化cdev
xxx_dev.cdev.owner THIS_MODULE;
/*获取字符设备号*/
if (xxx_major) {
register_chrdev_region (xxx_dev_no, 1, DEV_NAME);
}else {
alloc_chrdev_region (&xxx_dev_no, 0, 1, DEV_NAME) ;
}
ret = cdev_add (&xxx_dev,cdev, xxx_dev_no, 1) :/*注册设备*/
...
}
//设备驱动模块卸载函数
static void __ exit xxx_exit(void){
unregister_chrdev_region(xxx_dev_no,1);//释放占用的设备号
cdev_del(&xxx_dev.cdev);//注册设备
}
字符设备驱动的file_operations结构体中的成员函数
该结构体的成员函数是字符设备驱动与内核虚拟文件系统的接口,是用户对linux进行系统调用最终的落实者,大多数字符设备驱动会实话read(),write()和ioctl()函数。常用模块如下:
/*读设备*/
ssize_t xxx_read (struct file *filp, char user *buf, size_t count,loff_t*f_pos)
{
copy_to_user (buf, .... ...)
}
/*写设备*/
ssize_t xxx_write (struct file *filp, const char user *buf, size_t count,loff_t *f_pos)
{
copy_from_user(.... buf, ...);
}
/* ioctla数*/
long xxx_ioctl (struct file *filp, unsigned int cmd, unsigned long arg)
{
switch (cmd)
{
case xXX CMD1:
•0break;
case XXX_CMD2:
break;
default:
/*不能支持的命令*/
return -ENOTTY:
}
return 0
}