关于“字符设备注册函数”的总结

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.4版本后,内核里就加入了以下几个函数来实现注册字符设备:
  • 静态注册(指定设备编号来注册);
  • 动态分配(不指定设备编号来注册);
  • 有连续注册的次设备编号范围区间。

2、2.4内核版本后的注册函数

2.1 静态注册字符设备

int register_chrdev_region(dev_t devid, unsigned count, const char *name);   

devid: 注册的指定起始设备编号,比如:**MKDEV(100, 0)**表示起始主设备号100, 起始次设备号为0

**count:**需要连续注册的次设备编号个数,比如: 起始次设备号为0,count=100,表示0~99的次设备号都要绑定在同一个file_operations操作方法结构体上

***name:**字符设备名称

当返回值小于0表示注册失败。

2.2 动态分配字符设备

/*注册成功并将分配到的主次设备号放入*dev里*/
int alloc_chrdev_region(dev_t *devid, unsigned baseminor, unsigned count,const char *name);

devid: 存放起始设备编号的指针。当注册成功, *devid就会等于分配到的起始设备编号,可以通过MAJOR()和MINNOR()函数来提取主次设备号。

**baseminor:**次设备号基地址,也就是起始次设备号

**count:**需要连续注册的次设备编号个数,比如: 起始次设备号(baseminor)为0,baseminor=2,表示0~1的此设备号都要绑定在同一个file_operations操作方法结构体上

***name:**字符设备名称

当返回值小于0,表示注册失败。

2.3 cdev_init/cdev_add/cdev_del

  • 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;                   //连续注册的次设备号个数
};

1. cdev_init

 /*初始化cdev结构体,并将file_operations结构体放入cdev-> ops 里*/
void cdev_init(struct cdev *cdev, const struct file_operations *fops);

2. cdev_add

/*将cdev结构体添加到系统中,并将dev(注册好的设备编号)放入cdev-> dev里,  count(次设备编号个数)放入cdev->count里*/
int cdev_add(struct cdev *p, dev_t dev, unsigned count);
  • 通过查看内核2.6的include/linux/kdev_t.h文件得知,次设备号用20位表示,主设备号用12位表示,所以,在2.6版本之后,内核共可以表示2^12 * 2^20 = 4G个驱动程序。

3. cdev_del

/*将系统中的cdev结构体删除掉*/
void cdev_del(struct cdev *p);

2.4 注销字符设备

 /*注销字符设备*/
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~3对应第二个file_operations,

然后在/dev/下,通过次设备号(0~4)创建5个设备节点, 利用应用程序打开这5个文件,看有什么现象

3.1 驱动代码

#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 hello1_open(struct inode *inode, struct file *file)
{
	printk("hello1_open\n");
	return 0;
}

static int hello2_open(struct inode *inode, struct file *file)
{
	printk("hello2_open\n");
	return 0;
}


/* 2. 构造file_operations */
static struct file_operations hello1_fops = {
	.owner = THIS_MODULE,
	.open  = hello1_open,
};

static struct file_operations hello2_fops = {
	.owner = THIS_MODULE,
	.open  = hello2_open,
};


#define HELLO_CNT   2

static struct cdev hello1_cdev;
static struct cdev hello2_cdev;
static struct class *cls;

static int hello_init(void)
{
	dev_t devid;
	
	/* 3. 告诉内核 */
#if 0
	major = register_chrdev(0, "hello", &hello_fops); /* (major,  0), (major, 1), ..., (major, 255)都对应hello_fops */
#else
	if (major) {
		devid = MKDEV(major, 0);
		register_chrdev_region(devid, 2, "hello");  /* (major,0~1) 对应 hello1_fops, (major, 2~255)都不对应hello_fops */
	} else {
		alloc_chrdev_region(&devid, 0, 2, "hello"); /* (major,0~1) 对应 hello1_fops, (major, 2~255)都不对应hello_fops */
		major = MAJOR(devid);                     
	}
	
	cdev_init(&hello1_cdev, &hello1_fops);
	cdev_add(&hello1_cdev, devid, 2);

	devid = MKDEV(major, 2);
	register_chrdev_region(devid, 1, "hello2");	/* (major,2) 对应 hello2_fops*/
	cdev_init(&hello2_cdev, &hello2_fops);
	cdev_add(&hello2_cdev, devid, 1);
	
#endif

	cls = class_create(THIS_MODULE, "hello");
	class_device_create(cls, NULL, MKDEV(major, 0), NULL, "hello0"); /* /dev/hello0 */
	class_device_create(cls, NULL, MKDEV(major, 1), NULL, "hello1"); /* /dev/hello1 */
	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(&hello2_cdev);
	unregister_chrdev_region(MKDEV(major, 2), 1);
    
	cdev_del(&hello1_cdev);
    unregister_chrdev_region(MKDEV(major, 0), 2);
	
}

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>

void print_useg(char arg[])    //打印使用帮助信息
{
         printf("useg:  \n");
         printf("%s   [dev]\n",arg);
}

int main(int argc,char **argv)
{

  int fd;
  if(argc!=2)
    {
        print_useg(argv[0]);
        return -1;
    }

  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;
}

3.3 编译测试

通过 ls /dev/hello* -l ,可以看到创建了4个字符设备节点。

打开/dev/hello0时,调用的是驱动代码的操作结构体hello1_fops里的.open(),

打开/dev/hello1时,调用的是驱动代码的操作结构体hello1_fops里的.open(),

打开/dev/hello2时,调用的是驱动代码的操作结构体hello2_fops里的.open(),

打开/dev/hello3时打开无效,因为在驱动代码里没有分配次设备号3的操作结构体。

# insmod hello.ko
# ls /dev/hello* -l
crw-rw----	1 0		0	251,	0 Jan	1 03:40 /dev/hello0
crw-rw----	1 0		0	251,	1 Jan	1 03:40 /dev/hello1
crw-rw----	1 0		0	251,	2 Jan	1 03:40 /dev/hello2
crw-rw----	1 0		0	251,	2 Jan	1 03:40 /dev/hello3
#
# ./hello_test /dev/hello0
hello1_open
can open /dev/hello0
#
# ./hello_test /dev/hello1
hello1_open
can open /dev/hello1
#
# ./hello_test /dev/hello2
hello2_open
can open /dev/hello2
#
# ./hello_test /dev/hello3
can't open /dev/hello3

总结:

使用register_chrdev_region()等函数来注册字符设备,里面可以存放多个不同的file_oprations操作结构体,实现各种不同的功能(驱动)。

参考资料:

1_字符设备驱动程序概念纠正之另一种写法 (100ask.net)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Leon_George

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值