專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

總線設(shè)備驅(qū)動(dòng)模型總結(jié)

作者:龔平   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2014年03月14日   【字體:

我的環(huán)境:
主機(jī)開發(fā)環(huán)境:Fedora14
開發(fā)板:         TQ2440
編譯器: arm-linux-gcc-4.3.2


總線設(shè)備驅(qū)動(dòng)模型其實(shí)現(xiàn)主要是基于Kobject和sysfs等機(jī)制,對于驅(qū)動(dòng)模型程序開發(fā)主要是理解三個(gè)元素:總線、設(shè)備、驅(qū)動(dòng)的關(guān)系。三者之間因?yàn)橐欢ǖ穆?lián)系性實(shí)現(xiàn)對設(shè)備的控制。

首先是總線,總線是三者聯(lián)系起來的基礎(chǔ),通過一種總線類型,將設(shè)備和驅(qū)動(dòng)聯(lián)系起來�?偩€類型中的match函數(shù)用來匹配設(shè)備和驅(qū)動(dòng)。當(dāng)匹配操作晚餐之后就會(huì)控制驅(qū)動(dòng)程序中的probe函數(shù)。

總線設(shè)備驅(qū)動(dòng)模型的設(shè)計(jì)主要包括三個(gè)元素的注冊,將三個(gè)元素加載到內(nèi)核中,然后通過內(nèi)核的內(nèi)部機(jī)制將三者聯(lián)系起來。


首先,總線類型的注冊,包括屬性文件的添加,總線也是一種設(shè)備,也需要將總線設(shè)備注冊。

其次,完成設(shè)備的注冊和添加以及對設(shè)備添加設(shè)備屬性文件,同時(shí)填充最基本的函數(shù)操作。

最后,完成驅(qū)動(dòng)的注冊和天極以及對設(shè)備驅(qū)動(dòng)添加屬性文件,同時(shí)填充最基本的函數(shù)操作。


1、總線


總線類型是通過結(jié)構(gòu)體bus_type表示的。其源碼如下所示:

    struct bus_type {

        /*總線名*/

        const char        *name;

        /*總線、設(shè)備、驅(qū)動(dòng)屬性*/

        struct bus_attribute    *bus_attrs;

        struct device_attribute    *dev_attrs;

        struct driver_attribute    *drv_attrs;

        /*總線支持的函數(shù)操作*/

        /*匹配函數(shù),主要用來識別相應(yīng)的設(shè)備和驅(qū)動(dòng),是兩者直接形成關(guān)聯(lián)

          用來判斷指定的驅(qū)動(dòng)程序能否處理指定的設(shè)備

        */

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

        /*在進(jìn)行熱插拔事件之前,為設(shè)備配置環(huán)境變量操作函數(shù)*/

        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;

    };

其中的int (*match)(struct device * dev, struct device_driver * drv)是必須實(shí)現(xiàn)的函數(shù),因?yàn)檫@個(gè)函數(shù)主要是實(shí)現(xiàn)設(shè)備和驅(qū)動(dòng)之間的匹配管理。其中匹配的具體邏輯關(guān)系需要驅(qū)動(dòng)設(shè)計(jì)著設(shè)定。


int (*uevent)(struct device *dev, char **envp, int num_envp,char *buffer, int buffer_size)則在熱插拔事件之前,允許總線為設(shè)備添加環(huán)境變量。


通常創(chuàng)建一種總線類型的過程中只要完成總線類型結(jié)構(gòu)體的填充,然后完成相應(yīng)的注冊、屬性文件創(chuàng)建即可實(shí)現(xiàn)總線類型的添加。并不需要對bus_type類型中的所有變量進(jìn)行賦值,只要將其中的name,bus_attribute,match實(shí)現(xiàn)即可。


最后不要忘了總線也是設(shè)備,需要將總線設(shè)備添加到內(nèi)核中(注冊函數(shù))。

關(guān)于總線類型的屬性設(shè)置,實(shí)質(zhì)上就是完成一個(gè)結(jié)構(gòu)體的操作。

如下源碼所示:

    struct bus_attribute {

        /*屬性結(jié)構(gòu)體*/

        struct attribute    attr;

        /*屬性讀操作函數(shù),即顯示函數(shù)*/

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

        /*屬性寫操作函數(shù),也就是存儲到結(jié)構(gòu)體中*/

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

    };

 /*可以通過宏命令定義一個(gè)總線結(jié)構(gòu)體,但是需要自己實(shí)現(xiàn)屬性讀寫操作函數(shù),如果沒有,可設(shè)置為NULL*/

 /*總線屬性定義宏*/

 #define BUS_ATTR(_name, _mode, _show, _store) \

    struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)

 

 /*__ATTR的宏實(shí)現(xiàn)如下所示:*/

 #define __ATTR(_name,_mode,_show,_store) { \

.attr = {.name = __stringify(_name), .mode = _mode }, \

.show = _show, \

.store = _store, \

 }

由于通常情況下需要查找總線的版本信息,可以將版本信息添加到屬性的讀屬性操作函數(shù)中,這樣就能顯示具體的版本信息。

在宏定義中##是指鏈接符的作用,相當(dāng)于將兩個(gè)部分鏈接起來,比如bus_attr_##name = bus_attr_name。這是在宏定義中比較常用的定義方式之一。


總線類型的注冊和總線類型屬性文件創(chuàng)建,以及總線設(shè)備的注冊主要是依據(jù)下面幾個(gè)函數(shù)來實(shí)現(xiàn):

    /*總線類型注冊函數(shù),由于可能會(huì)出錯(cuò),因此必須對返回值進(jìn)行檢查*/

    int __must_check bus_register(struct bus_type *bus);

    /*總線類型釋放函數(shù)*/

    void bus_unregister(struct bus_type *bus);

  /*總線文件屬性創(chuàng)建函數(shù),將相關(guān)的文件屬性添加給總線類型,同時(shí)也必須檢查返回值是否正確*/

    int __must_check bus_create_file(struct bus_type *,struct bus_attribute *);

    /*總線類型文件屬性刪除函數(shù),將兩者之間的關(guān)聯(lián)性切斷*/

    void bus_remove_file(struct bus_type *, struct bus_attribute *);

最后需要將總線設(shè)備添加到系統(tǒng)中,主要采用設(shè)備注冊函數(shù);

設(shè)備注冊函數(shù):

int __must_check device_register(struct device *dev);

設(shè)備釋放函數(shù):

void device_unregister(struct device *dev);

2、設(shè)備

 

設(shè)備的實(shí)現(xiàn)主要是依靠struct device函數(shù)實(shí)現(xiàn)的,設(shè)備的實(shí)現(xiàn)主要是對結(jié)構(gòu)體的填充。實(shí)現(xiàn)相應(yīng)的函數(shù)即可。

    struct device {

        /*父設(shè)備,通常就是總線設(shè)備,這也是為什么需要將總線作為設(shè)備添加的原因*/

        struct device        *parent;

        struct device_private    *p;

        struct kobject kobj;

        /*init_name是新添加的,替代了原來的bus_id,但是init_name不能直接被讀寫操作*/

        const char        *init_name; /* initial name of the device */

        struct device_type    *type;

        struct semaphore    sem;    /* semaphore to synchronize calls to

                         * its driver.

                         */

        /*總線類型,主要是關(guān)聯(lián)總線類型,這是前面添加的總線類型,通過相同的總線類型關(guān)聯(lián)設(shè)備和驅(qū)動(dòng)*/

        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_info    power;

    #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_head    dma_pools;    /* dma pools (if dma'ble) */

        struct dma_coherent_mem    *dma_mem; /* internal for coherent mem

                         override */

        /* arch specific additions */

        struct dev_archdata    archdata;

        dev_t            devt;    /* dev_t, creates the sysfs "dev" */

        spinlock_t        devres_lock;

        struct list_head    devres_head;

        struct klist_node    knode_class;

        struct class        *class;

        struct attribute_group    **groups;    /* optional groups */

       

        /*必須實(shí)現(xiàn)的release函數(shù)*/

        void    (*release)(struct device *dev);

    };

    /*由于init_name 不能直接讀寫,只能通過*dev_name來讀寫設(shè)備名*/

    static inline const char *dev_name(const struct device *dev)

    {

        return kobject_name(&dev->kobj);

    }

    /*實(shí)現(xiàn)對設(shè)備名的設(shè)置*/

    int dev_set_name(struct device *dev, const char *name, ...)

                __attribute__((format(printf, 2, 3)));

    /*設(shè)備文件屬性結(jié)構(gòu)體,必須注意的改變點(diǎn)*/

    struct device_attribute {

        /*屬性值*/

        struct attribute    attr;

        /*設(shè)備屬性讀函數(shù),必須注意是三個(gè)參數(shù),不再是兩個(gè)參數(shù)*/

        ssize_t (*show)(struct device *dev, struct device_attribute *attr,char *buf);

        /*設(shè)備屬性寫操作,必須注意是四個(gè)參數(shù),不是三個(gè)參數(shù)*/

        ssize_t (*store)(struct device *dev, struct device_attribute *attr,const char *buf, size_t count);

    };

  /*設(shè)備屬性宏定義,主要用來實(shí)現(xiàn)設(shè)備文件屬性*/

    #define DEVICE_ATTR(_name, _mode, _show, _store) \

    struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)

  /*創(chuàng)建設(shè)備文件屬性函數(shù),必須檢查返回值*/

    int __must_check device_create_file(struct device *device,struct device_attribute *entry);

    /*刪除設(shè)備文件屬性函數(shù)*/

    void device_remove_file(struct device *dev,struct device_attribute *attr);

    /*設(shè)備注冊函數(shù),必須檢查返回值*/

    int __must_check device_register(struct device *dev);

    /*設(shè)備釋放函數(shù)*/

    void device_unregister(struct device *dev);


需要注意的是linux-2.6.30內(nèi)核以前,沒有init_name元素,而是元素bus_id,這個(gè)主要是實(shí)現(xiàn)設(shè)備名的填充,但是linux-2.6.30內(nèi)核之后的在struct device中init_name代替了bus_id,但是需要注意的是init_name不能直接被讀寫,當(dāng)需要讀寫設(shè)備名時(shí)只能采用特定的函數(shù)實(shí)現(xiàn):dev_name(),set_dev_name()。當(dāng)直接讀寫init_name會(huì)導(dǎo)致內(nèi)核錯(cuò)誤,出現(xiàn)Unable to handle kernel NULL pointer dereference at virtual address 00000000的錯(cuò)誤。

最后注意device_attribute中的show,store函數(shù)不同于其他類型(總線、驅(qū)動(dòng))的函數(shù),device_attribute中的show和store函數(shù)中的參數(shù)數(shù)量多了一個(gè)。


3、驅(qū)動(dòng)


驅(qū)動(dòng)管理一定的設(shè)備,其中的關(guān)系主要是內(nèi)核的內(nèi)部機(jī)制實(shí)現(xiàn)的,但是實(shí)現(xiàn)的具體邏輯需要在bus_type中的match函數(shù)中具體設(shè)計(jì)。通常是一定的設(shè)備名和驅(qū)動(dòng)名匹配,當(dāng)然也可以有其他的邏輯,具體的只需要設(shè)計(jì)好bus_type中的match函數(shù)。


驅(qū)動(dòng)是由驅(qū)動(dòng)結(jié)構(gòu)體實(shí)現(xiàn)的。具體如下所示:

    /*驅(qū)動(dòng)結(jié)構(gòu)體*/

    struct device_driver {

        /*驅(qū)動(dòng)名,通常用來匹配設(shè)備*/

        const char        *name;

        /*關(guān)聯(lián)的總線類型,總線、設(shè)備、驅(qū)動(dòng)關(guān)聯(lián)的總線類型*/

        struct bus_type        *bus;

        struct module        *owner;

        const char         *mod_name;    /* used for built-in modules */

        /*驅(qū)動(dòng)中最應(yīng)該實(shí)現(xiàn)的操作函數(shù)主要包括probe和remove函數(shù)*/

        /*當(dāng)匹配完成以后的,入口函數(shù)*/

        int (*probe) (struct device *dev);

        /*驅(qū)動(dòng)卸載時(shí)操作的相關(guān)函數(shù),退出函數(shù)*/

        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;

    };

    /*驅(qū)動(dòng)注冊函數(shù),返回值必須檢測*/

    int __must_check driver_register(struct device_driver *drv);

    /*驅(qū)動(dòng)釋放函數(shù)*/

    void driver_unregister(struct device_driver *drv);

    /*驅(qū)動(dòng)屬性結(jié)構(gòu)體*/

    struct driver_attribute {

        /*屬性值*/

        struct attribute attr;

        /*屬性讀操作函數(shù)*/

        ssize_t (*show)(struct device_driver *driver, char *buf);

        /*屬性寫操作函數(shù)*/

        ssize_t (*store)(struct device_driver *driver, const char *buf,

                 size_t count);

    };

    /*驅(qū)動(dòng)屬性定義宏命令*/

    #define DRIVER_ATTR(_name, _mode, _show, _store)    \

    struct driver_attribute driver_attr_##_name =        \

        __ATTR(_name, _mode, _show, _store)

 /*驅(qū)動(dòng)屬性文件創(chuàng)建函數(shù),返回值必須檢測*/

    int __must_check driver_create_file(struct device_driver *driver,struct driver_attribute *attr);

    /*驅(qū)動(dòng)屬性文件移除函數(shù)*/

    void driver_remove_file(struct device_driver *driver,struct driver_attribute *attr);

驅(qū)動(dòng)結(jié)構(gòu)體的定義不需要完成所有元素的賦值,只需要完成主要的幾個(gè)變量的賦值即可,其中主要的元素包含name,bus,以及probe和remove函數(shù)的實(shí)現(xiàn)。

其中的probe函數(shù)是當(dāng)總線中的match完成匹配操作以后,進(jìn)入驅(qū)動(dòng)的入口函數(shù),因此必須實(shí)現(xiàn)。remove我認(rèn)為就是對應(yīng)的退出函數(shù),因此也有必要實(shí)現(xiàn)。

驅(qū)動(dòng)的注冊,釋放也有相關(guān)的函數(shù)來操作,主要是driver_register()和driver_unregister()。


總結(jié):

1、在總線驅(qū)動(dòng)模型中我認(rèn)為最主要的是搞清楚三個(gè)不同的結(jié)構(gòu)體,分別是總線、驅(qū)動(dòng)、設(shè)備。了解三個(gè)元素對應(yīng)的屬性結(jié)構(gòu)體以及相應(yīng)的屬性操作函數(shù)的差異性。

2、不同驅(qū)動(dòng)設(shè)計(jì)的關(guān)鍵主要是完成不同結(jié)構(gòu)體的填充過程,但是并不需要對結(jié)構(gòu)體中所有的對象進(jìn)行賦值,只需要完成重要的幾個(gè)元素的值。

3、總線是一種類型,同時(shí)也是一種設(shè)備,在總線的相關(guān)處理中需要首先添加總線類型,然后添加總線設(shè)備,這是需要注意的。由于總線類型關(guān)聯(lián)驅(qū)動(dòng)和設(shè)備,因此需要導(dǎo)出總線類型變量。由于總線設(shè)備是設(shè)備的父設(shè)備,因此也需要將總線設(shè)備變量導(dǎo)出。同樣在驅(qū)動(dòng)和設(shè)備中也要導(dǎo)出相關(guān)的結(jié)構(gòu)體變量,便于總線中的match函數(shù)實(shí)現(xiàn)驅(qū)動(dòng)和設(shè)備的匹配操作。

4、XXX_attr結(jié)構(gòu)體基本相同,都是一個(gè)屬性結(jié)構(gòu)體和函數(shù)show()、stroe()。但是不同的XXX可能會(huì)導(dǎo)致show、stroe函數(shù)的參數(shù)發(fā)生變化。這需要對照源碼。

5、struct device中的init_name是一個(gè)特殊的量,不能直接讀寫操作,只能采用函數(shù)device_name()和set_device_name來設(shè)置設(shè)備名。

6、xxx_register()之類的函數(shù),需要對返回值進(jìn)行檢查。因?yàn)楹苡锌赡懿怀晒?/p>

關(guān)閉窗口

相關(guān)文章