linux驅動開發:總線,設備,驅動三要素

平臺總線和IIC,SPI,IIS都是總線類型,一般的,總線下,掛載對應的設備。但實際上,設備要正常運轉,是需要驅動程序來未知提供驅動的。所以linux內核也把驅動掛載在對應的總線下。總線,驅動,設備三者缺一不可.
相應的,內核衍生出來的平臺總線,那麼便衍生出來了平臺設備和憑條驅動。他們均有自己的專屬函數來註冊,註銷。這個是成一套體系結構的.在以後的驅動開發中,很是常見與重要.

一)內核中的總線,設備,驅動
1)總線

struct bus_type {
    const char      *name;
    const char      *dev_name;
    struct device       *dev_root;
    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 (*resume)(struct device *dev);

    const struct dev_pm_ops *pm;//電源管理

    struct iommu_ops *iommu_ops;

    struct subsys_private *p;//將bus device sysfs聯繫起來
    struct lock_class_key lock_key;
};

註冊:bus_register(struct bus_type *bus);
註銷:bus_unregister(struct bus_type *bus);

2)設備

struct device {
    struct device       *parent;

    struct device_private   *p;

    struct kobject kobj;
    const char      *init_name; /* initial name of the device */
    const struct device_type *type;

    struct mutex        mutex;  /* mutex 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        *platform_data; /* Platform specific data, device
                       core doesn't touch it */
    struct dev_pm_info  power;
    struct dev_pm_domain    *pm_domain;

#ifdef CONFIG_PINCTRL
    struct dev_pin_info *pins;
#endif

#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 */
#ifdef CONFIG_CMA
    struct cma *cma_area;       /* contiguous memory area for dma
                       allocations */
#endif
    /* arch specific additions */
    struct dev_archdata archdata;

    struct device_node  *of_node; /* associated device tree node */
    struct acpi_dev_node    acpi_node; /* associated ACPI device node */

    dev_t           devt;   /* dev_t, creates the sysfs "dev" */
    u32         id; /* device instance */

    spinlock_t      devres_lock;
    struct list_head    devres_head;

    struct klist_node   knode_class;
    struct class        *class;
    const struct attribute_group **groups;  /* optional groups */

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

設備註冊:int device_register(struct device *dev)
設備註銷:void device_unregister(struct device *dev)

3)設備驅動

struct device_driver {
    const char      *name;
    struct bus_type     *bus;//掛載的總線,所屬的總線

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

    bool suppress_bind_attrs;   /* disables bind/unbind via sysfs */

    const struct of_device_id   *of_match_table;
    const struct acpi_device_id *acpi_match_table;

    int (*probe) (struct device *dev);// 設備match到驅動,則調用對應驅動的probe函數,驅動設備
    int (*remove) (struct device *dev);//當驅動或者設備被卸載,則調用對應驅動的remove函數
    void (*shutdown) (struct device *dev);
    int (*suspend) (struct device *dev, pm_message_t state);
    int (*resume) (struct device *dev);
    const struct attribute_group **groups;

    const struct dev_pm_ops *pm;

    struct driver_private *p;
};

驅動註冊:int driver_register(struct device_driver *drv)
驅動註銷:void driver_unregister(struct device_driver *drv)

二)平臺設備總線
總線:

struct bus_type platform_bus_type = {
    .name       = "platform",
    .dev_attrs  = platform_dev_attrs,
    .match      = platform_match,
    .uevent     = platform_uevent,
    .pm     = &platform_dev_pm_ops,
};

設備:

struct platform_device {
    const char  *name;
    int     id;
    bool        id_auto;
    struct device   dev;
    u32     num_resources;
    struct resource *resource;

    const struct platform_device_id *id_entry;

    /* MFD cell pointer */
    struct mfd_cell *mfd_cell;

    /* arch specific additions */
    struct pdev_archdata    archdata;
};
平臺設備的註冊:platform_device_register(struct platform_device * pdev)
平臺設備的註銷:platform_device_unregister(struct platform_device * pdev)

驅動:

struct platform_driver {
    int (*probe)(struct platform_device *);
    int (*remove)(struct platform_device *);
    void (*shutdown)(struct platform_device *);
    int (*suspend)(struct platform_device *, pm_message_t state);
    int (*resume)(struct platform_device *);
    struct device_driver driver;
    const struct platform_device_id *id_table;
};
平臺驅動的註冊:int platform_driver_register(struct platform_driver *drv)
平臺驅動的註銷:void platform_driver_unregister(struct platform_driver *drv)

平臺設備匹配的流程:

先插入一個函數,match

static int platform_match(struct device *dev, struct device_driver *drv)
{
    struct platform_device *pdev = to_platform_device(dev);
    struct platform_driver *pdrv = to_platform_driver(drv);

    /* Attempt an OF style match first */
    if (of_driver_match_device(dev, drv))
        return 1;

    /* Then try ACPI style match */
    if (acpi_driver_match_device(dev, drv))
        return 1;

    /* Then try to match against the id table */
    if (pdrv->id_table)
        return platform_match_id(pdrv->id_table, pdev) != NULL;

    /* fall-back to driver name match */
    return (strcmp(pdev->name, drv->name) == 0);
}
//match 的方式:不同的內核,可能有所不同:我的內核版本:3.10.46

1.OF style match first
2.ACPI style match
3.match against the id table
4.driver name match

大概的匹配流程:
1)平臺設備註冊流程:

platform_device_register()
{
--->platform_device_add()
    {
        --->device_add()
        {
            --->device_add()
            {
                --->bus_probe_device()
                {

                    --->device_attach()
                    {
                        --->bus_for_each_drv(dev->bus, NULL, dev, __device_attach)
                        {
                            --->__device_attach()
                            {
                                --->driver_match_device()//platform_match()
                                //當match到驅動後,繼續下面步驟:
                                --->driver_probe_device()
                                {
                                    --->really_probe()
                                    {
                                        --->drv->probe()//調用對應驅動的probe函數
                                    }

                                }

                            }
                        }
                    }


                }
            }
        }
    }

}

2)平臺驅動註冊流程:

platform_driver_register()
{
    --->driver_register()
    {
        --->bus_add_driver()
        {
            --->driver_attach()
            {
                --->bus_for_each_dev(drv->bus, NULL, drv, __driver_attach)
                {
                    --->__driver_attach()
                    {
                        --->driver_match_device()//platform_match()
                        //當match到對應的設備後,繼續
                        --->driver_probe_device()
                        {
                            --->really_probe()
                            {
                                --->drv->probe()//調用當前註冊驅動的probe函數
                            }
                        }
                    }
                }
            }
        }
    }
}

這裏寫圖片描述

1.設備向對應平臺bus註冊設備,設備掛載到平臺bus下面的dev list鏈表中.
2.驅動向對應平臺bus註冊驅動,驅動掛載到平臺bus下面的driver list鏈表中.
3.設備和對應總線下的driver list鏈表中每個driver進行匹配(match).匹配成功則調用對應driver的probe函數,否則匹配失敗,則設備無法被驅動,設備無法工作.

match函數已經在上面列出。它有自己的匹配規則,上一篇的實例是按照match name來進行匹配的.
當設備或者驅動被卸載時,驅動的remove函數都會被調用.

平臺設備總線的意義:移植性強。通過我們是不需要寫平臺設備文件的,只需要寫對應的驅動文件。而驅動文件一般移植時並不需要修改。只需要修改對應的platform_data 平臺數據和resource 平臺資源列表便可.
這樣修改一個驅動或者移植一個驅動到另一個內核便變得很簡單。

發佈了37 篇原創文章 · 獲贊 5 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章