美文网首页
5. 驱动程序分层分离概念-总线设备驱动模型

5. 驱动程序分层分离概念-总线设备驱动模型

作者: JalynFong | 来源:发表于2019-01-07 07:39 被阅读0次

    在输入子系统框架学习中,可以看到将其分为上下两层,和左右两边;这就是我们今天要引入的另一个概念,驱动程序的分离分层。


    分离分层概念

    一、什么是总线设备驱动模型

    可以从三个概念,来理解。

    • 总线

           一个总线是处理器和一个或多个设备之间的通道. 为设备模型的目的, 所有的设备都通过一个总线连接, 甚至它是一个内部虚拟的"平台"总线。
           在 Linux 内核中, 总线由 bus_type 结构表示, 定义在 <linux/device.h> 。

    struct bus_type {
        const char *name; /*总线名称*/
        int (*match)  (struct device *dev, struct
        device_driver *drv);    /*驱动与设备的匹配函数*/
        ………
    }
    
    • 设备

    代表着一个实际的硬件。
    在 Linux内核中, 设备由struct device结构表示。

    struct device {
    {
      const char *init name;  /*设备的名字*/
      struct bus_type *bus;  /*设备所在的总线*/
      ………
    }
    
    • 驱动

    代表具体操作设备的方式和流程。
    在 Linux内核中, 驱动由device_driver结构表示:

    struct device_driver {
    {
        const char *name; /*驱动名称*/
        struct bus_type *bus;  /*驱动程序所在的总线*/
        int (*probe) (struct device *dev);
        ………
    }
    

    二、为什么使用总线设备驱动模型

    随着技术的不断进步,系统的拓扑结构也越来越复杂,对热插拔,跨平台移植性的要求也越来越高,2.4内核已经难以满足这些需求。为适应这种形势的需要,从Linux 2.6内核开始提供了全新的设备模型。其优势在于采用了总线的模型对设备与驱动进行了管理,提高了程序的可移植性。

    三、实例

    • 1.0 总线由bus_type定义(位于<linux/device.h>
    struct bus_type {
        const char *name; /*总线名称*/
        /*总线属性*/
        struct bus_attribute    *bus_attrs;
        int (*match)  (struct device *dev, struct
        device_driver *drv);    /*驱动与设备的匹配函数*/
        /*为用户空间产生热插拔事件之前,这个方法允许总线添加环境变量*/
        int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
        ………
    }
    
      1. 1 总线的注册

    bus_register(struct bus_type *bus)
    若成功,新的总线将被添加进系统,并可在/sys/bus 下看到相应的目录。

    • 1.2总线的注销

    void bus_unregister(struct bus_type *bus)

    • 1.3总线的匹配

    参照bus_type:
    int (*match)(struct device * dev, struct device_driver * drv)
    当一个新设备或者新驱动被添加到这个总线时,该函数被调用。用于判断指定的驱动程序是否能处理指定的设备。若可以,则返回非零。

    创建一条总线:

    #include <linux/init.h>
    #include <linux/module.h>
    #include <linux/kernel.h>
    #include <linux/device.h>
    
    MODULE_LICENSE("GPL");
    MODULE_AUTHOR("Jalyn Fang");
    
    /*比较设备的bus_id与驱动的名字是否匹配,
    匹配一致则在insmod驱动 时候调用probe函数*/
    int my_match(struct device *dev, struct device_driver *drv)
    {
        return !strncmp(dev->kobj.name,drv->name,strlen(drv->name));
    }  
    
    struct bus_type my_bus_type = {
        .name = "my_bus",
        .match = my_match,
    };
        
    int my_bus_init()
    {
        int ret;
    
        /*注册总线*/
        ret = bus_register(&my_bus_type);
        
        return ret;
    
    }
    
    void my_bus_exit()
    {
        bus_unregister(&my_bus_type);
    }
    
     /*符号导出
      * Export a simple attribute.
     */
    EXPORT_SYMBOL(my_bus_type);
    module_init(my_bus_init);
    module_exit(my_bus_exit);
    

    • 2.0 驱动device_driver定义,当加载驱动时候,会在总线上找到它能够处理的设备。
      1. 1 驱动的注册

    int driver_register(struct device_driver *drv)

    • 2.2驱动的注销

    void driver_unregister(struct device_driver *drv)

    在总线上挂载驱动:

    #include <linux/device.h>
    #include <linux/module.h>
    #include <linux/kernel.h>
    #include <linux/init.h>
    
    MODULE_LICENSE("GPL");
    MODULE_AUTHOR("Jalyn Fang");
    
    extern struct bus_type my_bus_type;
    
    int my_probe(struct device *dev)
    {
        printk("driver found the device it can handle!\n");
        return 0;
    }
    
    struct device_driver my_driver = {
        .name = "my_dev",
          /*指明这个驱动程序是属于my_bus_type这条总线上的设备*/
        .bus = &my_bus_type,    
         /*在my_bus_type总线上找到它能够处理的设备,
        就调用my_probe;删除设备调用my_remove*/
        .probe = my_probe,
       /*能够处理的设备? 
          1、什么时候驱动程序从总线找能够处理的设备;
          答:驱动注册时候
          2、凭什么说能够处理呢?(或者说标准是什么);
          答:驱动与设备都是属于总线上的,利用总线的
          结构bus_type中match函数
       */
    };
    
    int my_driver_init()
    {
        int ret;
        
        ret = driver_register(&my_driver);
        
        return ret;
    }
    
    void my_driver_exit()
    {
        driver_unregister(&my_driver);  
    }
    
    module_init(my_driver_init);
    module_exit(my_driver_exit);
    
    • 3.0
      1. 1 设备的注册

    int device_register(struct device *dev)

      1. 2 设备的注销

    void device_unregister(struct device *dev)

    在总线上挂载设备:

    #include <linux/device.h>
    #include <linux/module.h>
    #include <linux/kernel.h>
    #include <linux/init.h>
    
    MODULE_LICENSE("GPL");
    
    extern struct bus_type my_bus_type;
    
    struct device my_dev = {
         .init_name = "my_dev",
         .bus = &my_bus_type,   
         .release = my_dev_release,
    };
    
    int my_device_init()
    {
        int ret;
         ret = device_register(&my_dev);
         return ret;
         
    }
    
    
    void my_device_exit()
    {
        device_unregister(&my_dev);
    }
    
    module_init(my_device_init);
    module_exit(my_device_exit);
    



    参考:
    https://blog.csdn.net/wh_19910525/article/details/7398051

    相关文章

      网友评论

          本文标题:5. 驱动程序分层分离概念-总线设备驱动模型

          本文链接:https://www.haomeiwen.com/subject/trryrqtx.html