https://www.cnblogs.com/lifexy/p/7827559.html
1、之前注册字符设备用的如下函数注册字符设备驱动:
int register_chrdev(unsigned int major, const char *name, const struct file_operations *fops)
但其实这个函数是linux版本2.4之前的注册方式,它的原理是:
(1)确定一个主设备号
(2)构造一个file_operations结构体,然后放在chrdevs数组中
(3)注册:register_chrdev
然后当读写字符设备的时候,就会根据主设备号从chrdevs数组中取出相应的结构体,并调用相应的处理函数。
它会有个很大的缺点:
每注册一个字符设备的时候,还会连续注册0~255个次设备号,使它们绑定在同一个file_operations操作方法结构体上,在大多数情况下,都只用极少的次设备号,所以会浪费很多资源。
2、所以在2.4版本后,内核里就加入了以下几个函数也可以来实现注册字符设备:
分为:静态注册(指定设备编号来注册)、动态分配(不指定设备编号来注册),以及有连续注册的次设备编号范围区间,避免了register_chrdev()浪费资源的缺点
2.1:指定设备编号来静态注册一个字符设备
int register_chrdev_region(dev_t from, unsigned count, const char *name)
from:注册的指定起始设备编号,比如:MKDEV(100, 0),表示起始主设备号100,起始次设备号为0
count:需要连续注册的次设备编号个数,比如:起始次设备号为0,count=100,表示0~99的次设备号都要绑定在同一个file_operations操作方法结构体上
*name:字符设备名称
当返回值小于0,表示注册失败
2.2 动态分配一个字符设备,注册成功并将分配到的主次设备号放入*dev里
int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name)
*dev:存放起始设备编号的指针,当注册成功,*dev就会等于分配到的起始设备编号,可以通过MAJOR()和MINOR()函数来提取主次设备号
baseminor:次设备号基地址,也就是起始次设备号
count:需要连续注册的次设备编号个数,比如:起始次设备(baseminot)为0,baseminor=2,表示0~1的此设备号都要绑定在同一个file_operations操作方法结构体上
*name:字符设备名称
当返回值小于0,表示注册失败
2.3 初始化cdev结构体,并将file_operations结构体放入cdev->ops里
void cdev_init(struct cdev *cdev, const struct file_operations *fops)
其中cdev结构体的成员,如下所示:
struct cdev {
struct kobject kobj; //内嵌的kobject对象
struct module *owner; //所属模块
const struct file_operations *ops; //操作方法结构体
struct list_head list; //与cdev对应的字符设备文件的inode->i_devices的链表头
dev_t dev; //起始设备编号,可以通过MAJOR(),MINOR()来提取主次设备号
unsigned int count; //连续注册的次设备号个数
};
2.4 将cdev结构体添加到系统中,并将dev(注册好的设备编号)放入cdev->dev里,count(次设备编号个数)放入cdev->count里
int cdev_add(struct cdev *p, dev_t dev, unsigned count)
2.5 将系统中的cdev结构体删除掉
void cdev_del(struct cdev *p)
2.6 注销字符设备
void unregister_chrdev_region(dev_t from, unsigned count)
from:注销的指定起始设备编号,比如:MKDEV(100, 0),表示起始主设备号为100,起始设备号为0
count:需要连续注销的次设备编号个数,比如:起始次设备号为0,baseminor=100,表示注销0~99的次设备号
3、接下来,我们便来写一个字符设备驱动
里面调用两次上面的函数,构造两个不同的file_operations操作结构体,
次设备号0~1 对应 第一个file_operations,
次设备号2 对应 第二个file_operations,
然后再/dev/目录下,通过次设备号(0~3)创建4个设备节点,利用测试应用程序打开这5个文件,看有什么现象
3.1 驱动代码如下:hello.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/poll.h>
#include <linux/cdev.h>
/* 1. 主设备号 */
static int major;
static int hello_open (struct inode *inode, struct file *file)
{
printk("hello_open\n");
return 0;
}
static int hello2_open (struct inode *inode, struct file *file)
{
printk("hello2_open\n");
return 0;
}
/* 2. 构造file_operations */
//操作结构体1
static struct file_operations hello_fops = {
.owner = THIS_MODULE,
.open = hello_open,
};
//操作结构体2
static struct file_operations hello2_fops = {
.owner = THIS_MODULE,
.open = hello2_open,
};
#define HELLO_CNT 2 //主设备
static struct cdev hello_cdev; //保存hello1_fops操作结构体的字符设备
static struct cdev hello2_cdev; //保存hello2_fops操作结构体的字符设备
static struct class *cls;//class让系统自动创建设备节点
static int hello_init(void)
{
dev_t devid;
#if 0
/* 一个驱动程序就占据了256个次设备号 */
major = register_chrdev(0, "hello", &file_operations);/* (major, 0), (major, 1), ..., (major, 255)都对应hello_fops */
#else
/* 3. 告诉内核 */
if (major) {
devid = MKDEV(major, 0);
register_chrdev_region(devid, HELLO_CNT, "hello"); /* (major, 0~1)对应hello_fops, (major, 2~255)都不对应hello_fops */
} else {
alloc_chrdev_region(&devid, 0, HELLO_CNT, "hello"); /* (major, 0~1)对应hello_fops, (major, 2~255)都不对应hello_fops */
major = MAJOR(devid);
}
//初始化cdev
cdev_init(&hello_cdev, &hello_fops);
//注册cdev
cdev_add(&hello_cdev, devid, HELLO_CNT);
//测试另外一个file_operations
devid = MKDEV(major, 2);
register_chrdev_region(devid, 1, "hello2"); /* (major, 0~1)对应hello_fops, (major, 2~255)都不对应hello_fops */
//初始化cdev
cdev_init(&hello2_cdev, &hello2_fops);
//注册cdev
cdev_add(&hello2_cdev, devid, 1);
#endif
//创建一个类
cls = class_create(THIS_MODULE, "hello");
//在这个类下面再创建一个设备(设备节点)
//mdev是udev的一个简化版本
//mdev应用程序,就会被内核调用,会根据类和类下面的设备这些信息
class_device_create(cls, NULL, MKDEV(major, 0), NULL, "hello0");/* /dev/hello0 */
class_device_create(cls, NULL, MKDEV(major, 1), NULL, "hello1");/* /dev/hello1 */
//open hello2肯定打不开,因为注册的区域(次设备号)0~1,2用不了
class_device_create(cls, NULL, MKDEV(major, 2), NULL, "hello2");/* /dev/hello2 */
class_device_create(cls, NULL, MKDEV(major, 3), NULL, "hello3");/* /dev/hello3 */
return 0;
}
static void hello_exit(void)
{
class_device_destroy(cls, MKDEV(major, 0));
class_device_destroy(cls, MKDEV(major, 1));
class_device_destroy(cls, MKDEV(major, 2));
class_device_destroy(cls, MKDEV(major, 3));
class_destroy(cls);
cdev_del(&hello_cdev);
unregister_chrdev_region(MKDEV(major, 0), HELLO_CNT);
cdev_del(&hello2_cdev);
unregister_chrdev_region(MKDEV(major, 2), 1);
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
3.2 测试代码如下所示
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
/*
* hello_test /dev/hello0
*/
void print_usaeg(char *file) //打印使用帮助信息
{
printf("%s <dev>\n", file);
}
int main(int argc, char **argv)
{
int fd;
if(argc != 2)
{
print_usaeg(argv[0]);
return 0;
}
fd = open(argv[1], O_RDWR);
if(fd < 0)
printf("can't open %s\n", argv[1]);
else
printf("can open %s\n", argv[1]);
return 0;
}
4、运行测试:
如下图,挂在驱动后,通过ls /dev/hello* -l ,看到创建了4个字符设备节点
接下来开始测试驱动,如下图所示,
打开/dev/hello0时,调用的是驱动代码的操作结构体hello_fops里的.open(),
打开/dev/hello1时,调用的是驱动代码的操作结构体hello_fops里的.open(),
打开/dev/hello2时,调用的是驱动代码的操作结构体hello2_fops里的.open(),
打开/dev/hello3时,打开无效,因为在驱动代码里没有分配次设备号3的操作结构体,
总结一下:
使用register_chrdev_region()等函数来注册字符设备,里面可以存放多个不同的file_operations操作结构体,实现各种不同的功能