Linux裝置驅動工程師之路——裝置模型(下)上層模型

來源:互聯網
上載者:User

Linux裝置驅動工程師之路——裝置模型(下)上層模型

K-Style

轉載請註明來自于衡陽師範學院08電2  Y-Kee http://blog.csdn.net/ayangke,QQ:843308498

 

一、重要知識點:

         裝置模型由匯流排、裝置、驅動三要素組成。

底層模型決定上層模型,在匯流排,裝置,驅動的結構體中你總是可以看到它們間接或者直接的包含了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->bus = 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_NUMAint numa_node;/* NUMA node this device is close to */#endifu64 *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匯流排

 

測試結果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.