驅動之路-設備模型(下)上層模型

一、重要知識點:

         設備模型由總線、設備、驅動三要素組成。

底層模型決定上層模型,在總線,設備,驅動的結構體中你總是可以看到它們間接或者直接的包含了kobject結構或kset結構。

         1.總線

         總線是處理器和設備之間的通道,在設備模型中,所有設備都通過總線相連,甚至內部的虛擬“platform”總線。在linux設備模型中,總線由bus_type結構表示。定義在<linux/device>中。

         總線描述

 

  •         struct bus_type {  
  •          const char                  *name;//總線名稱  
  •          struct bus_attribute        *bus_attrs//總線屬性  
  •          struct device_attribute   *dev_attrs;//設備屬性  
  •          struct driver_attribute    *drv_attrs;//驅動屬性  
  •          int (*match)(struct device *dev, struct device_driver *drv);  
  •          int (*uevent)(struct device *dev, struct kobj_uevent_env *env);  
  •          int (*probe)(struct device *dev);  
  •          int (*remove)(struct device *dev);  
  •          void (*shutdown)(struct device *dev);  
  •          int (*suspend)(struct device *dev, pm_message_t state);  
  •          int (*suspend_late)(struct device *dev, pm_message_t state);  
  •          int (*resume_early)(struct device *dev);  
  •          int (*resume)(struct device *dev);  
  •          struct dev_pm_ops *pm;  
  •          struct bus_type_private *p;  
  • };  

  •  

    bus_type的bus_type_private 成員

     

  • struct bus_type_private {  
  • struct kset subsys;  
  • struct kset *drivers_kset;  
  • struct kset *devices_kset;  
  • struct klist klist_devices;  
  • struct klist klist_drivers;  
  • struct blocking_notifier_head bus_notifier;  
  • unsigned int drivers_autoprobe:1;  
  • struct bus_type *bus;  
  • };  


  •  

    可以看出該結構體中包含了有device和dirver的kset對象。

    總線的註冊:

    int bus_register(structbus_type *bus);

    如果成功,新的總線將被添加到系統,在/sys/bus目錄下可以看到。

    我們在深入看這個函數

     

  • int bus_register(struct bus_type *bus)  
  • {  
  • int retval;  
  • struct bus_type_private *priv;  
  • priv = kzalloc(sizeof(struct bus_type_private), GFP_KERNEL);  
  • if (!priv)  
  • return -ENOMEM;  
  • priv->busbus = bus;  
  • bus->p = priv;  
  • BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);  
  • retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);  
  • if (retval)  
  • goto out;  
  •   
  • priv->subsys.kobj.kset = bus_kset;  
  • priv->subsys.kobj.ktype = &bus_ktype;  
  • priv->drivers_autoprobe = 1;  
  •   
  • retval = kset_register(&priv->subsys);  
  • if (retval)  
  • goto out;  
  •   
  • retval = bus_create_file(bus, &bus_attr_uevent);  
  • if (retval)  
  • goto bus_uevent_fail;  
  •   
  •   
  • priv->devices_kset = kset_create_and_add("devices", NULL,  
  • &priv->subsys.kobj);  
  • if (!priv->devices_kset) {  
  • retval = -ENOMEM;  
  • goto bus_devices_fail;  
  • }  
  •   
  •   
  • priv->drivers_kset = kset_create_and_add("drivers", NULL,  
  • &priv->subsys.kobj);  
  • if (!priv->drivers_kset) {  
  • retval = -ENOMEM;  
  • goto bus_drivers_fail;  
  • }  
  •   
  •   
  • klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);  
  • klist_init(&priv->klist_drivers, NULL, NULL);  
  •   
  • retval = add_probe_files(bus);  
  • if (retval)  
  • goto bus_probe_files_fail;  
  •   
  • retval = bus_add_attrs(bus);  
  • if (retval)  
  • goto bus_attrs_fail;  
  •   
  • pr_debug("bus: '%s': registered\n", bus->name);  
  • return 0;  
  •   
  • bus_attrs_fail:  
  • remove_probe_files(bus);  
  • bus_probe_files_fail:  
  • kset_unregister(bus->p->drivers_kset);  
  • bus_drivers_fail:  
  • kset_unregister(bus->p->devices_kset);  
  • bus_devices_fail:  
  • bus_remove_file(bus, &bus_attr_uevent);  
  • bus_uevent_fail:  
  • kset_unregister(&bus->p->subsys);  
  • kfree(bus->p);  
  • out:  
  • return retval;  
  • }  


  •  

    從這裏

    priv->subsys.kobj.kset = bus_kset;
    priv->subsys.kobj.ktype = &bus_ktype;

    可以看得出總線初始化了自己的kset對象。

    總線的刪除

    void bus_unregister(struct bus_type *bus);

    總線的match方法

    int (*match)(struct device *dev, struct device_driver *drv);

    當一個新設備或者驅動被添加到這個總線時,該方法被調用,用於判斷指定的驅動程序是否能處理指定的設備。若可以,則返回非0值。下面的測試程序採用匹配設備的dev->bus_id字符串和驅動的drv->name字符串是否相同來判斷驅動是否能處理該設備。

    總線uevent方法

    int  (*uevent)(structdevice *dev, char **envp, int num_envp)

    在爲用戶產生熱插拔事件之前,這個方法允許總線添加環境變量。

    總線屬性由結構bus_attribute描述:

     

    struct bus_attribute {

             struct attribute        attr;

             ssize_t (*show)(struct bus_type *bus,char *buf);

             ssize_t (*store)(struct bus_type *bus,const char *buf, size_t count);

    };

    BUS_ATTR(name,mode, show, store)

    在編譯時創建和初始化bus_attribute結構,它將bus_attr_作爲給定前綴來創建總線的真正名稱。

    如:static BUS_ATTR(version, S_IRUGO, show_bus_version, NULL);

    將創建一個bus_attr_version結構體對象。

     

    int bus_create_file(struct bus_type *bus, struct bus_attribute *attr)

    創建屬性文件

    void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)

    刪除屬性文件

     

    2.設備

     

  • struct device {  
  • struct klist klist_children;  
  • struct klist_nodeknode_parent;/* node in sibling list */  
  • struct klist_nodeknode_driver;  
  • struct klist_nodeknode_bus;  
  • struct device *parent;  
  • struct kobject kobj;  
  • char bus_id[BUS_ID_SIZE];/* position on parent bus */  
  • unsigned uevent_suppress:1;  
  • const char *init_name; /* initial name of the device */  
  • struct device_type*type;  
  •   
  • struct semaphoresem;/* semaphore to synchronize calls to  
  • * its driver.  
  • */  
  • struct bus_type*bus;/* type of bus device is on */  
  • struct device_driver *driver;/* which driver has allocated this  
  •   device */  
  • void *driver_data;/* data private to the driver */  
  • void *platform_data;/* Platform specific data, device  
  •   core doesn't touch it */  
  • struct dev_pm_infopower;  
  •   
  •   
  • #ifdef CONFIG_NUMA  
  • int numa_node;/* NUMA node this device is close to */  
  • #endif  
  • u64 *dma_mask;/* dma mask (if dma'able device) */  
  • u64 coherent_dma_mask;/* Like dma_mask, but for  
  •     alloc_coherent mappings as  
  •     not all hardware supports  
  •     64 bit addresses for consistent  
  •     allocations such descriptors. */  
  •   
  •   
  • struct device_dma_parameters *dma_parms;  
  •   
  •   
  • struct list_headdma_pools;/* dma pools (if dma'ble) */  
  •   
  •   
  • struct dma_coherent_mem*dma_mem; /* internal for coherent mem  
  •     override */  
  • /* arch specific additions */  
  • struct dev_archdataarchdata;  
  •   
  • dev_t devt;/* dev_t, creates the sysfs "dev" */  
  •   
  • spinlock_t devres_lock;  
  • struct list_headdevres_head;  
  •   
  •   
  • struct klist_nodeknode_class;  
  • struct class *class;  
  • struct attribute_group**groups;/* optional groups */  
  •   
  • void (*release)(struct device *dev);  
  • };  


  • 可以看出device結構體中也包含了一個kobject對象

     

    intdevice_register(struct device *dev);

    註冊設備

    vioddevice_unregister(struct device *dev)

    註銷設備

    設備屬性由struct device_attribute描述

    structdevice_attribute {

             struct attribute        attr;

             ssize_t (*show)(struct device *dev,struct device_attribute *attr,

                                char *buf);

             ssize_t (*store)(struct device *dev,struct device_attribute *attr,

                                 const char *buf, size_t count);

    };

    int device_create_file(struct device *dev, struct device_attibute *entry)

    創建屬性文件

    void device_remove_file(struct device *dev, struct device_attibute *entry)

    刪除屬性文件

    3.驅動

    驅動程序由struct device_driver描述:

     

  • struct device_driver {  
  • const char *name;  
  • struct bus_type*bus;  
  •   
  •   
  • struct module *owner;  
  • const char *mod_name;/* used for built-in modules */  
  •   
  •   
  • int (*probe) (struct device *dev);  
  • int (*remove) (struct device *dev);  
  • void (*shutdown) (struct device *dev);  
  • int (*suspend) (struct device *dev, pm_message_t state);  
  • int (*resume) (struct device *dev);  
  • struct attribute_group **groups;  
  •   
  •   
  • struct dev_pm_ops *pm;  
  •   
  •   
  • struct driver_private *p;  
  • };  


  • 在看device_dirver的driver_private 對象

    struct driver_private {
    struct kobject kobj;
    struct klist klist_devices;
    struct klist_node knode_bus;
    struct module_kobject *mkobj;
    struct device_driver *driver;
    };

     可以看出driver_private結構體中也包含了一個kobject對象。和連接設備的鏈表。

    當總線的match返回非0值,也就是總線找到與驅動相匹配的設備時,驅動的probe的函數將被調用。

    當設備從系統總刪除是remove被調用。

    當系統關機的時候shutdown被調用。

    int driver_register(structdevice_driver *drv)

    在總線上註冊驅動

    void driver_unregister(struct device_driver *drv)

    載總線上註銷驅動

             驅動的屬性使用structdriver_attribute來描述

             structdriver_attribute{

                       structattribue attr;

                       ssize_t(*show)(struct device_driver *drv, const char *buf);

                       ssize_t(*store)(struct device_driver *drv, const char *buf, size_t count);

             }

             int dirver_create_file(struct device_driver *drv, struct driver_attribute *attr);

             創建屬性文件

             void driver_remove_file struct device_driver *drv, struct driver_attribute *attr);

             刪除屬性文件


     

    二、測試模塊

             1.BUS

  • #include <linux/device.h>  
  • #include <linux/module.h>  
  • #include <linux/kernel.h>  
  • #include <linux/init.h>  
  • #include <linux/string.h>  
  •   
  • MODULE_AUTHOR("David Xie");  
  • MODULE_LICENSE("Dual BSD/GPL");  
  •   
  • static char *Version = "$Revision: 1.9 {1}quot;;  
  •   
  • static int my_match(struct device *dev, struct device_driver *driver)  
  • {  
  •     return !strncmp(dev->bus_id, driver->name, strlen(driver->name));  
  • }  
  •   
  • static void my_bus_release(struct device *dev)  
  • {  
  •     printk(KERN_DEBUG "my bus release\n");  
  • }  
  •       
  • struct device my_bus = {  
  •     .bus_id   = "my_bus0",  
  •     .release  = my_bus_release  
  • };  
  •   
  •   
  • struct bus_type my_bus_type = {  
  •     .name = "my_bus",  
  •     .match = my_match,  
  • };  
  •   
  • EXPORT_SYMBOL(my_bus);  
  • EXPORT_SYMBOL(my_bus_type);  
  •   
  •   
  • /*  
  •  * Export a simple attribute.  
  •  */  
  • static ssize_t show_bus_version(struct bus_type *bus, char *buf)  
  • {  
  •     return snprintf(buf, PAGE_SIZE, "%s\n", Version);  
  • }  
  •   
  • static BUS_ATTR(version, S_IRUGO, show_bus_version, NULL);  
  •   
  •   
  • static int __init my_bus_init(void)  
  • {  
  •     int ret;  
  •           
  •         /*註冊總線*/  
  •     ret = bus_register(&my_bus_type);  
  •     if (ret)  
  •         return ret;  
  •           
  •     /*創建屬性文件*/    
  •     if (bus_create_file(&my_bus_type, &bus_attr_version))  
  •         printk(KERN_NOTICE "Fail to create version attribute!\n");  
  •       
  •     /*註冊總線設備*/  
  •     ret = device_register(&my_bus);  
  •     if (ret)  
  •         printk(KERN_NOTICE "Fail to register device:my_bus!\n");  
  •           
  •     return ret;  
  • }  
  •   
  • static void my_bus_exit(void)  
  • {  
  •     device_unregister(&my_bus);  
  •     bus_unregister(&my_bus_type);  
  • }  
  •   
  • module_init(my_bus_init);  
  • module_exit(my_bus_exit);  
  • 創建一條名爲my_bus_type的總線和一個名爲my_bus的總線設備,注意總線也是一個設備,也需要註冊。

    測試結果:



     

    2.DEVICE

  • #include <linux/device.h>  
  • #include <linux/module.h>  
  • #include <linux/kernel.h>  
  • #include <linux/init.h>  
  • #include <linux/string.h>  
  •   
  • MODULE_AUTHOR("David Xie");  
  • MODULE_LICENSE("Dual BSD/GPL");  
  •   
  • extern struct device my_bus;   
  • extern struct bus_type my_bus_type;  
  •   
  • /* Why need this ?*/  
  • static void my_dev_release(struct device *dev)  
  • {   
  •       
  • }  
  •   
  • struct device my_dev = {  
  •     .bus = &my_bus_type,  
  •     .parent = &my_bus,  
  •     .release = my_dev_release,  
  • };  
  •   
  • /*  
  •  * Export a simple attribute.  
  •  */  
  • static ssize_t mydev_show(struct device *dev, char *buf)  
  • {  
  •     return sprintf(buf, "%s\n", "This is my device!");  
  • }  
  •   
  • static DEVICE_ATTR(dev, S_IRUGO, mydev_show, NULL);  
  •   
  • static int __init my_device_init(void)  
  • {  
  •     int ret = 0;  
  •           
  •         /* 初始化設備 */  
  •     strncpy(my_dev.bus_id, "my_dev", BUS_ID_SIZE);  
  •           
  •         /*註冊設備*/  
  •     device_register(&my_dev);  
  •           
  •     /*創建屬性文件*/  
  •     device_create_file(&my_dev, &dev_attr_dev);  
  •       
  •     return ret;   
  •   
  • }  
  •   
  • static void my_device_exit(void)  
  • {  
  •     device_unregister(&my_dev);  
  • }  
  •   
  • module_init(my_device_init);  
  • module_exit(my_device_exit);  

  • 註冊一個bus_id即名字爲my_dev的設備,該設備的bus成員指向上一步創建的my_bus_type總線,parent成員指向上一步創建的my_bus總線設備。

             測試結果:


     

    3.DRIVER

  • #include <linux/device.h>  
  • #include <linux/module.h>  
  • #include <linux/kernel.h>  
  • #include <linux/init.h>  
  • #include <linux/string.h>  
  •   
  • MODULE_AUTHOR("David Xie");  
  • MODULE_LICENSE("Dual BSD/GPL");  
  •   
  • extern struct bus_type my_bus_type;  
  •   
  • static int my_probe(struct device *dev)  
  • {  
  •     printk("Driver found device which my driver can handle!\n");  
  •     return 0;  
  • }  
  •   
  • static int my_remove(struct device *dev)  
  • {  
  •     printk("Driver found device unpluged!\n");  
  •     return 0;  
  • }  
  •   
  • struct device_driver my_driver = {  
  •     .name = "my_dev",  
  •     .bus = &my_bus_type,  
  •     .probe = my_probe,  
  •         .remove = my_remove,  
  • };  
  •   
  • /*  
  •  * Export a simple attribute.  
  •  */  
  • static ssize_t mydriver_show(struct device_driver *driver, char *buf)  
  • {  
  •     return sprintf(buf, "%s\n", "This is my driver!");  
  • }  
  •   
  • static DRIVER_ATTR(drv, S_IRUGO, mydriver_show, NULL);  
  •   
  • static int __init my_driver_init(void)  
  • {  
  •     int ret = 0;  
  •           
  •         /*註冊驅動*/  
  •     driver_register(&my_driver);  
  •           
  •     /*創建屬性文件*/  
  •     driver_create_file(&my_driver, &driver_attr_drv);  
  •       
  •     return ret;   
  •   
  • }  
  •   
  • static void my_driver_exit(void)  
  • {  
  •     driver_unregister(&my_driver);  
  • }  
  •   
  • module_init(my_driver_init);  
  • module_exit(my_driver_exit);  
  • 創建一個名爲“bus_dev”的驅動,並將bus成員指向第一步創建的my_bus_type總線

     

    測試結果:


    發表評論
    所有評論
    還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
    相關文章