Resource waste problem when registering character device driver

Table of contents

1. Reasons

2. Countermeasures

2.1 Registration process and API

2.2 Logout process and API

3. Character device driver step-by-step registration\unregistration example


Internal implementation principle of character device driver https://blog.csdn.net/liu319293960_/article/details/131234447?spm=1001.2014.3001.5501

When a character device driver registers with the kernel, using the register_chrdev() function causes a waste of device resources.

1. Reasons

The register_chrdev() function will automatically apply for 256 device resources when registering. In actual development, so many resources may not be used, so it will cause a waste of resources.

2. Countermeasures

Adopt a step-by-step registration method to specify the number of equipment resources to apply for

字符设备驱动结构体:
struct cdev {
    struct kobject kobj;
    struct module *owner;//THIS_MODULE
    const struct file_operations *ops;//操作方法结构体
    struct list_head list;//构成链表
    dev_t dev;//设备号
    unsigned int count;//设备数量
};

2.1 Registration process and API

        1. Apply for space for the character device driver structure

#include <linux/cdev.h>
struct cdev *cdev_alloc(void);//手动申请字符设备驱动对象空间
返回值:成功返回申请空间首地址,失败返回NULL

// 实例: struct cdev *cdev=cdev_alloc();

        2. Initialization of some members of the character device driver structure

#include <linux/cdev.h>
void cdev_init(struct cdev *cdev, const struct file_operations *fops);
功能:实现字符设备驱动对象的部分初始化
参数:
    cdev:字符设备驱动结构体指针
    fops:操作方法结构体指针
返回值:无

        3. Apply for device number


int register_chrdev_region(dev_t from, unsigned count, const char *name);
功能:静态指定设备号
参数:
     from:要申请的设备号
     count:要申请的设备资源的数量
     name:驱动名字
返回值:成功返回0,失败返回 错误码


int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name);
功能:动态申请一定范围内的设备号
参数:
     dev:申请的设备号填充在这个变量中
     baseminor:次设备号的起始值        
     count:要申请的设备资源的数量
     name:驱动名字
返回值:成功返回0,失败返回 错误码

        4. Register the character device driver structure into the kernel

int cdev_add(struct cdev *cdev, dev_t dev, unsigned count);
    参数:
        cdev:字符设备驱动对象指针
        dev:申请的设备号的起始值
        count:设备数量
    返回值:成功返回0,失败返回错误码

2.2 Logout process and API

        1. Unregister the character device driver structure from the kernel

void cdev_del(struct cdev *);
功能:字符设备驱动结构体的注销
参数:字符设备驱动结构体指针
返回值:无

        2. Release the requested device number

void unregister_chrdev_region(dev_t from, unsigned count)
功能:释放申请的设备号
参数:
    from:要释放的设备号
    count:设备的数量
返回值:无

        3. Release character device driver structure space

void kfree(void *p)
参数:
    p:字符设备驱动结构体空间首地址

3. Character device driver step-by-step registration\unregistration example

#include <linux/init.h>
#include <linux/module.h>
#include<linux/fs.h>
#include<linux/io.h>
#include<linux/device.h>
#include<linux/cdev.h>
#include<linux/slab.h>
#include"led.h"
struct cdev *cdev;
unsigned int major=500;
unsigned int minor=0;
dev_t devno;
struct class *cls;
struct device * dev;
int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
//定义操作方法结构体变量并赋值
struct file_operations fops={

    .open=mycdev_open,
   .unlocked_ioctl=mycdev_ioctl,
    .release=mycdev_close,
};

static int __init mycdev_init(void)
{
    int ret,i;
    //1.分配对象空间
    cdev=cdev_alloc();
    if(cdev==NULL)
    {
        printk("分配字符设备驱动对象失败\n");
        ret=-EFAULT;
        goto OUT1;
    }
    printk("分配对象空间成功\n");
    //2.初始化对象
    cdev_init(cdev,&fops);
    //3.申请设备号
    if(major>0)//静态指定设备号
    {
        ret= register_chrdev_region(MKDEV(major,minor),3,"myled");
        if(ret)
        {
            printk("静态指定设备号失败\n");
            goto OUT2;
        }
    }
    else if(major==0)//动态申请设备号
    {
        ret=alloc_chrdev_region(&devno,minor,3,"myled");
        if(ret)
        {
            printk("动态申请设备号失败\n");
            goto OUT2;
        }
        //获取主设备号和次设备号
        major=MAJOR(devno);
        minor=MINOR(devno);
    }
    printk("申请设备号成功\n");
    //4.注册字符设备驱动对象
    ret=cdev_add(cdev,MKDEV(major,minor),3);
    if(ret)
    {
        printk("注册字符设备驱动对象失败\n");
        goto OUT3;
    }
    printk("注册字符设备驱动对象成功\n");
    //向上提交目录
    cls=class_create(THIS_MODULE,"myled");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        ret=-PTR_ERR(cls);
        goto OUT4;
    }
    printk("向上提交目录成功\n");
    //向上提交设备节点信息
    for(i=0;i<3;i++)
    {
         dev=device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);
         if(IS_ERR(dev))
         {
            ret=-PTR_ERR(dev);
            goto OUT5;
         }
    }
    printk("向上提交设备节点成功\n");
    //完成硬件寄存器地址的映射以及初始化
    return 0;
OUT5:
    //释放已经申请的设备节点信息
    for(--i;i>=0;i--)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    //释放目录空间
    class_destroy(cls);
OUT4:
    //注销字符设备驱动对象
    cdev_del(cdev);
OUT3:
    //释放设备号
    unregister_chrdev_region(MKDEV(major,minor),3);
OUT2:
//释放对象空间
    kfree(cdev);
OUT1:
    return ret;
}
static void __exit mycdev_exit(void)
{
    //销毁设备节点
    int i;
    for(i=0;i<3;i++)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    //释放目录空间
    class_destroy(cls);
    //1.注销字符设备驱动对象
     cdev_del(cdev);
    //2.释放设备号
     unregister_chrdev_region(MKDEV(major,minor),3);
    //3.释放对象空间
     kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

Guess you like

Origin blog.csdn.net/liu319293960_/article/details/131234616