linux驱动编写

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

2021-08-14  本文已影响0人  Leon_Geo

1、早期注册字符设备使用的函数

int register_chrdev(unsigned int major, const char *name,const struct file_operations *fops);

(1)确定一个主设备号

(2)构造一个file_operations结构体, 然后放到chrdevs数组中

(3)注册:register_chrdev

然后当读写字符设备的时候,就会根据主设备号从chrdevs数组中取出相应的结构体,并调用相应的处理函数。

每注册个字符设备,都还会连续注册0~255个次设备号,使它们绑定在同一个file_operations操作方法结构体上,而我们在大多数情况下,都只会用到极少的次设备号,所以会造成资源的极大浪费。

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

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

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)](https://www.100ask.net/detail/v_5e5f51613df9f_uYnJqNVZ/3?from=p_5e5dbafac5a32_W4o2nJS1&type=8)
上一篇下一篇

猜你喜欢

热点阅读