1.11 字符设备驱动模型
在任何一种驱动模型中,设备都会用内核中的一种结构来描述。字符设备在内核中使用struct cdev来描述。
struct cdev{
struct kobject kobj;
struct module *owner;
const struct file_operations *ops;//设备操作函数集
struct list_head list;
dev_t dev;//设备号
unsigned int count;//设备数
};
字符设备驱动模型的整体框架:
1.11.1 设备号
1,主设备号和次设备号
字符设备文件与字符驱动程序通过主设备号建立起对应关系;驱动程序通过次设备号区分同类型的设备。
2,分配设备号
(1)静态申请
开发者自己选择一个数字作为主设备号,然后通过函数register_chrdev_region向内核申请使用。缺点:如果申请使用的设备号已经被内核中的其他驱动使用了,则申请失败。
int alloc_chrdev_region(dev_t *dev,unsigned baseminor,unsigned count,const char*name)
//参数
//dev:存放分配的设备号
//baseminor:次设备号的起始值
//count:设备数
//name:设备名
(2)动态申请
使用alloc_chrdev_region由内核分配一个可用的主设备号。优点:因为内核知道哪些号已经被使用了,所以不会导致分配到已经被使用的号。
int alloc_chrdev_region(dev_t *dev,unsigned baseminor,unsigned count,const char *name)
//参数:
//dev:设备号
//baseminor:次设备起始值
//count:设备数
//name:设备名
3,操作设备号
linux内核中使用dev_t类型来定义设备号,dev_t这种类型其实质为32位的unsigned int ,其中高12位为主设备号,低20位次设备号。
(1)由主设备号和次设备号组合成dev_t(类型)
dev_t dev=MKDEV(主设备号,次设备号)
(2)从dev_t中分解出主设备号
主设备号=MAJOR(dev_t dev)
(3)从dev_t中分解出次设备号
次设备号=MINOR(dev_t dev)
4,注销设备号
不论使用什么方法分配设备号,都应该在驱动退出时,使用unregister_chrdev_region函数释放这些设备号。
1.11.2 操作函数集
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 *);
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 *, 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 **);
long (*fallocate)(struct file *file, int mode, loff_t offset, loff_t len);
};
结构中的函数指针指向驱动程序中的函数,这些函数实现一个针对设备的操作,对于不支持的操作则设置函数指针为NULL。例如
struct file_operation dev_fops={
.llseek=NULL,
.read=dev_read,
.write=dev_write,
.ioctl=dev_ioctl,
.open=dev_open,
.release=dev_release,
};
1.11.3 字符设备初始化
1,分配描述结构cdev
(1)静态分配
struct cdev mdev;//直接定义一个struct cdev类型的变量。
(2)动态分配
struct cdev *pdev=cdev_alloc();//使用cdev_alloc()动态分配
2,初始化描述结构 cdev
struct cdv的初始化使用cdev_init函数来完成。
cdev_init(struct cdev *cdev,const struct file_opertations *fops)
//参数:
//cdev:待初始化的cdev结构 ; fops:设备对应的操作函数集
3,注册描述结构cdev
字符设备的注册使用cdev_add函数来完成。
cdev_add(struct cdev*p,dev_t dev,unsigned count)
//参数:
//p:待添加到内核的字符设备结构
//dev:设备号
//count:该类设备的设备个数
1.11.4 实现设备操作
struct file:在linux系统中,每一个打开的文件,在内核中都会关联一个struct file,它由内核在打开文件时创建,在文件关闭后释放。
loff_t f_pos;//文件读写指针
struct file_operation *f_op;//该文件所对应的操作
struct inode:每一个存在于文件系统里面的文件都会关联一个inode结构,该结构主要用来记录文件物理上的信息。因此,它和代表打开文件的file结构是不同的。一个文件没有被打开时不会关联file结构,但是却会关联一个inode结构。
dev_t i_rdev;//设备号
注意:(1)从应用程序提供的地址中取出数据;(2)将数据写入设备中;
要使用专门的函数:(1)int copy_from_user(void *to,const void __user *from,int n)
(2)int copy_to_user(void __user*to,const void *from,int n)
1.11.5 驱动注销
当我们从内核中卸载驱动程序时候,需要使用cdev_del函数来完成字符设备的注销
1.11.6 使用字符驱动程序
1,编译/安装字符设备
在Linux系统中,驱动程序通常采用内核模块的程序结构来进行编码。因此,编译/安装一个驱动程序,其实质就是编译/安装一个内核模块。
2, 字符设备文件
通过字符设备文件,应用程序可以使用相应的字符设备驱动程序来控制字符设备。创建字符设备文件的方法一般有两种:
(1)使用mknod命令
mknod /dev/文件名 c 主设备号 次设备号
(2)使用函数在驱动程序中创建
1.11.7 内存操作驱动实例
/**********************************/
/* */
/* 驱动程序 */
/* */
/**********************************/
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
int dev1_registers[5];
int dev2_registers[5];
struct cdev cdev;
dev_t devno;
/*文件打开函数*/
int mem_open(struct inode *inode, struct file *filp)
{
/*获取次设备号*/
int num = MINOR(inode->i_rdev);
if (num==0)
filp->private_data = dev1_registers;
else if(num == 1)
filp->private_data = dev2_registers;
else
return -ENODEV; //无效的次设备号
return 0;
}
/*文件释放函数*/
int mem_release(struct inode *inode, struct file *filp)
{
return 0;
}
/*读函数*/
static ssize_t mem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
{
unsigned long p = *ppos;
unsigned int count = size;
int ret = 0;
int *register_addr = filp->private_data; /*获取设备的寄存器基地址*/
/*判断读位置是否有效*/
if (p >= 5*sizeof(int))
return 0;
if (count > 5*sizeof(int) - p)
count = 5*sizeof(int) - p;
/*读数据到用户空间*/
if (copy_to_user(buf, register_addr+p, count))
{
ret = -EFAULT;
}
else
{
*ppos += count;
ret = count;
}
return ret;
}
/*写函数*/
static ssize_t mem_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;
int *register_addr = filp->private_data; /*获取设备的寄存器地址*/
/*分析和获取有效的写长度*/
if (p >= 5*sizeof(int))
return 0;
if (count > 5*sizeof(int) - p)
count = 5*sizeof(int) - p;
/*从用户空间写入数据*/
if (copy_from_user(register_addr + p, buf, count))
ret = -EFAULT;
else
{
*ppos += count;
ret = count;
}
return ret;
}
/* seek文件定位函数 */
static loff_t mem_llseek(struct file *filp, loff_t offset, int whence)
{
loff_t newpos;
switch(whence) {
case SEEK_SET:
newpos = offset;
break;
case SEEK_CUR:
newpos = filp->f_pos + offset;
break;
case SEEK_END:
newpos = 5*sizeof(int)-1 + offset;
break;
default:
return -EINVAL;
}
if ((newpos<0) || (newpos>5*sizeof(int)))
return -EINVAL;
filp->f_pos = newpos;
return newpos;
}
/*文件操作结构体*/
static const struct file_operations mem_fops =
{
.llseek = mem_llseek,
.read = mem_read,
.write = mem_write,
.open = mem_open,
.release = mem_release,
};
/*设备驱动模块加载函数*/
static int memdev_init(void)
{
/*初始化cdev结构*/
cdev_init(&cdev, &mem_fops);
/* 注册字符设备 */
alloc_chrdev_region(&devno, 0, 2, "memdev");
cdev_add(&cdev, devno, 2);
}
/*模块卸载函数*/
static void memdev_exit(void)
{
cdev_del(&cdev); /*注销设备*/
unregister_chrdev_region(devno, 2); /*释放设备号*/
}
MODULE_LICENSE("GPL");
module_init(memdev_init);
module_exit(memdev_exit);
/**********************************/
/* */
/* 应用程序 */
/* */
/**********************************/
//write.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd = 0;
int src = 2013;
/*打开设备文件*/
fd = open("/dev/memdev0",O_RDWR);
/*写入数据*/
write(fd, &src, sizeof(int));
/*关闭设备*/
close(fd);
return 0;
}
//read.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd = 0;
int dst = 0;
/*打开设备文件*/
fd = open("/dev/memdev0",O_RDWR);
/*写入数据*/
read(fd, &dst, sizeof(int));
printf("dst is %d\n",dst);
/*关闭设备*/
close(fd);
return 0;
}