linux 字符设备驱动程序框架

字符设备驱动程序:
1、注册字符设备(分配设备号,设置fop结构体,将设备与fop绑定)fop即为file_operations结构体
2、创建设备节点

设备编号的内部表达:
内核中,dev_t类型用来保存设备编号——包括主设备号和次设备号。它是一个32位数,其中12位用来表示主设备号,而其余的20位用来表示此设备号,注:现代的linux内核中允许多个驱动程序共享主设备号,但我们看到的大多数设备仍然按照“一个主设备号对应一个驱动程序”的原则组织。

主设备号是与驱动对应的概念,同一类设备一般使用相同的主设备号,不同类的设备一般使用不同的主设备号。因为同一驱动可支持多个同类设备,因此用次设备号来描述使用该驱动的设备的序号,序号一般从0开始

/* 根据dev_t获得主设备号和次设备号 */
MAJOR(dev_t dev);
MINOR(dev_t dev);
/* 将主设备号和次设备号转化成dev_t类型 */
MKDEV(int  major, int minor);

一、linux版本2.4之前的注册字符设备驱动的方式 :

/* major 设备主设备号   name 驱动程序的名称  fops 对应的file_operations结构体指针 */
register_chrdev(unsigned int major, const char *name,const struct file_operations *fops);

/*(1)确定一个主设备号
 *(2)构造一个file_operations结构体, 然后放在chrdevs数组中
 *(3)注册:register_chrdev
 * 然后当读写字符设备的时候,就会根据主设备号从chrdevs数组中取出相应的结构体,并调用相应的处理函数。
 * 这意味着一个主设备号就只能对应一个fop结构体、即一组字符设备操作函数。
 */
 /* 移除字符设备 */
int unregister_chrdev(unsigned int major , const char *name)

说明:register_chrdev的调用将为给定的主设备号注册0~255作为次设备号,,但是他们都和同一个fop绑定着。

二、2.4版本过后的注册字符设备:(将register_chrdev函数的操作过程分成几个步骤了)
(不会默认注册256个次设备号,反之可以指定需要注册的连续次设备号区间,让他们使用同一个fop结构体)

1、分配设备编号:
1.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:字符设备名称

2.2动态分配设备号:

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

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

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

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

*name:字符设备名称

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

2、字符设备注册
2.1分配cdev结构体
如:static struct cdev hello1_cdev; //保存 hello1_fops操作结构体的字符设备
static struct cdev hello2_cdev; //保存 hello2_fops操作结构体的字符设备
2.2 初始化cdev结构体

 /*初始化cdev结构体,并将file_operations结构体放入cdev-> ops 里, cdev_add再需要关联的设备编号放入cdev结构体,从而把fop结构体和设备编号联系起来*/
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.3 将cdev结构体添加到系统中、告知内核

/*将cdev结构体添加到系统中,并将dev(注册好的设备编号)放入cdev-> dev里,  count(次设备编号个数)放入cdev->count里*/
int cdev_add(struct cdev *p, dev_t dev, unsigned count);

3、移除字符设备

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

4、释放设备编号:

   /*注销字符设备*/
    void unregister_chrdev_region(dev_t from, unsigned count);

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

count:需要连续注销的次设备编号个数,比如: 起始次设备号为0,baseminor=100,表示注销掉0~99的次设备号

三、编写字符设备驱动示例:
构造两个不同的fop,次设备号0~1和次设备号2-3分别对应一个file_operations结构体
然后在/dev/下,通过次设备号(0~4)创建5个设备节点, 利用应用程序打开这5个文件,观察现象

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/list.h>
#include <linux/cdev.h>

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

 

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

 
  /*  操作结构体1   */
static struct file_operations hello1_fops={
        .owner=THIS_MODULE,
        .open =hello_fops1_open,
};

  /*  操作结构体2   */
static struct file_operations hello2_fops={
        .owner=THIS_MODULE,
        .open =hello_fops2_open,
};

 
static int major;                                 //主设备
static struct cdev hello1_cdev;        //保存 hello1_fops操作结构体的字符设备 
static struct cdev hello2_cdev;         //保存 hello2_fops操作结构体的字符设备 
static struct class *cls;

static int chrdev_ragion_init(void)
{
     dev_t  devid;  

     alloc_chrdev_region(&devid, 0, 4,"hello");    //动态分配字符设备: (major,0)  (major,1)   (major,2)  (major,3)

     major=MAJOR(devid);     //计算出系统自动分配的主设备号

     cdev_init(&hello1_cdev, &hello1_fops);  // 将hello1_fops结构体添加到hello1_cdev中
     cdev_add(&hello1_cdev, MKDEV(major,0), 2);           //(major,0) (major,1)将这两个设备添加到hello1_cdev中,完成绑定


     cdev_init(&hello2_cdev, &hello2_fops);
     cdev_add(&hello2_cdev,MKDEV(major,2), 2);           //(major,2) (major,3)     

     cls=class_create(THIS_MODULE, "hello");
     /*创建一类字符设备节点*/
     class_device_create(cls,0, MKDEV(major,0), 0, "hello0");   //对应hello_fops1操作结构体
     class_device_create(cls,0, MKDEV(major,1), 0, "hello1");   //对应hello_fops1操作结构体
     class_device_create(cls,0, MKDEV(major,2), 0, "hello2");   //对应hello_fops2操作结构体
     class_device_create(cls,0, MKDEV(major,3), 0, "hello3");   //对应hello_fops2操作结构体
     class_device_create(cls,0, MKDEV(major,4), 0, "hello4");   //对应空
        return 0;
}

void chrdev_ragion_exit(void)
{
   class_device_destroy(cls, MKDEV(major,4));
   class_device_destroy(cls, MKDEV(major,3));
   class_device_destroy(cls, MKDEV(major,2));
   class_device_destroy(cls, MKDEV(major,1));
   class_device_destroy(cls, MKDEV(major,0));

   class_destroy(cls);


   cdev_del(&hello1_cdev);     
   cdev_del(&hello2_cdev); 
   unregister_chrdev_region(MKDEV(major,0), 4);     //注销(major,0)~(major,3)
} 

module_init(chrdev_ragion_init);
module_exit(chrdev_ragion_exit);
MODULE_LICENSE("GPL");

测试代码如下:

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

4.运行测试:

如下图,挂载驱动后,通过 ls /dev/hello* -l ,看到创建了5个字符设备节点

在这里插入图片描述

接下来开始测试驱动,如下图所示,

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

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

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

在这里插入图片描述

总结:

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

参考文章:https://www.cnblogs.com/lifexy/p/7827559.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值