0724_驱动1 字符设备驱动内部实现

一、字符设备驱动内部实现工作原理

二、分布实现字符设备驱动API接口

分配对象:

#include <linux/cdev.h>
struct cdev *cdev_alloc(void)
函数功能:分配对象struct cdev *结构体指针
参数:无
返回值:
    成功返回struct cdev *结构体指针首地址
    失败返回NULL

#include <linux/slab.h>                
void kfree(const void *block)    

对象初始化:

void cdev_init(struct cdev *cdev, const struct file_operations *fops)
函数功能:对分配成功的struct cdev *结构体指针进行初始化
参数:
    cdev:分配成功struct cdev *结构体指针首地址
    fops:操作方法结构体
返回值:
    无

静态指定设备号:

int register_chrdev_region(dev_t from, unsigned count, const char *name)
函数功能:静态指定设备号,不可以cat /proc/devices 重复
参数:
    from:设备号
        MAJOR(dev):根据设备号,获取主设备号的值
        MINOR(dev):根据设备号,获取次设备号的值
        MKDEV(ma,mi):根据主设备号和次设备号,合成设备号
    count:设备个数
    name:设备名字
返回值:
    成功返回0
    失败返回-1,置位错误码

动态分配设备号:

int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
            const char *name)
函数功能:动态分配设备号
参数:
    dev:分配到的设备号
    baseminor:次设备号的起始值
    count:设备个数
    name:设备名字
返回值:
    成功返回0
    失败返回-1,置位错误码    

注销设备号:

void unregister_chrdev_region(dev_t from, unsigned count)
函数功能:注销设备号
参数:
    from:设备号
    count:设备个数
返回值:

对象注册:

int cdev_add(struct cdev *p, dev_t dev, unsigned count)
函数功能:对象注册
参数:
    p:分配成功struct cdev *结构体指针首地址
    dev:设备号
    count:设备个数    
返回值:
    成功返回0
    失败返回-1,置位错误码   

对象注销:

void cdev_del(struct cdev *p)
函数功能:对象注销
参数:
    p:分配成功struct cdev *结构体指针首地址
返回值:无

三、编写代码

#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/fs.h>

#define CNAME "myled"
struct cdev *cdev;
#if 0
unsigned int major = 500; //静态指定设备号
#else
unsigned int major = 0; //动态分配设备号
#endif
unsigned int count = 3;
unsigned int minor = 0; 
struct class* cls;
struct device* device;

int myled_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
ssize_t myled_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
ssize_t myled_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0; 
}
int myled_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0; 
}

//操作方法结构体
const struct file_operations fops = {
    .open = myled_open,
    .read = myled_read,
    .write = myled_write,
    .release = myled_close,
};

//入口
static int __init demo_init(void)
{
    int ret;
    dev_t devno;
    int i  = 0;
    //分配对象
    cdev = cdev_alloc();
    if(cdev == NULL){
        printk("cdev alloc is error\n");
        ret = -EIO;
        goto ERR1;
    }
    //对象初始化
    cdev_init(cdev,&fops);

    if(major > 0)
    {
        //静态指定设备号
        ret = register_chrdev_region(MKDEV(major,minor), count, CNAME);
        if(ret){
            printk("register chrdev region is error\n");
            ret = -EIO;
            goto ERR2;
        }
    }else{ //动态指定设备号
        ret = alloc_chrdev_region(&devno, 0, count,CNAME);
        if(ret){
            printk("alloc chrdev region is error\n");
            ret = -EIO;
            goto ERR2;
        }    
        major = MAJOR(devno);//根据设备号,获取主设备号的值
        minor = MINOR(devno);//根据设备号,获取次设备号的值    
    }
        
    //对象注册
    ret = cdev_add(cdev,MKDEV(major,minor),count);
    if(ret){
        printk("dev add is error\n");
        ret = -EIO;
        goto ERR3;
    }  

    //三盏灯,自动创建三个设备节点 /dev/myled0 /dev/myled1 /dev/myled2
    cls = class_create(THIS_MODULE, CNAME);  //向上层提交目录信息
    if(IS_ERR(cls))
    {
        printk("class create is error\n");
        ret = EIO;
        goto ERR4;
    }
    for(i=0;i<count;i++) //向上层提交设备节点信息
    {
        device = device_create(cls, NULL, MKDEV(major,i),NULL, "myled%d", i);
        if(IS_ERR(device))
        {
            printk("device create is error\n");
            ret = EIO;
            goto ERR5;
        }
    }
    return 0; //!!!!!!!!!!!!!!!一定不能省略!!!!!!!!!!!!!!!

ERR5:
    //如果第一个设备节点和第二个设备节点创建成功,第三个设备节点创建失败,取消向上层提交第一个和第二个设备节点信息
    for(--i;i>=0;i--)
    {
        device_destroy(cls, MKDEV(major,i)); //取消向上层提交设备节点信息
    }
    class_destroy(cls); //取消向上层提交目录信息
ERR4:
    cdev_del(cdev); //对象注销
ERR3:
    unregister_chrdev_region(MKDEV(major,minor), count); //注销设备号
ERR2:
    kfree(cdev); //释放结构体指针
ERR1:
    return ret;
}

//出口
static void __exit demo_exit(void)
{
    int i = 0;
    for(i=0;i<count;i++)
    {
        device_destroy(cls, MKDEV(major,i)); //取消向上层提交设备节点信息
    }
    class_destroy(cls); //取消向上层提交目录信息
    cdev_del(cdev); //对象注销
    unregister_chrdev_region(MKDEV(major,minor), count); //注销设备号
    kfree(cdev); //释放结构体指针
}

module_init(demo_init); //指定入口地址

module_exit(demo_exit); //指定出口地址

MODULE_LICENSE("GPL");//许可证

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值