1. 程式人生 > >Linux驅動之平臺設備驅動模型簡析(驅動分離分層概念的建立)

Linux驅動之平臺設備驅動模型簡析(驅動分離分層概念的建立)

技術 描述 rst 操作 mem iou 系統 簡單 reg

Linux設備模型的目的:為內核建立一個統一的設備模型,從而有一個對系統結構的一般性抽象描述。換句話說,Linux設備模型提取了設備操作的共同屬性,進行抽象,並將這部分共同的屬性在內核中實現,而為需要新添加設備或驅動提供一般性的統一接口,這使得驅動程序的開發變得更簡單了,而程序員只需要去學習接口就行了。

對於整個設備總線驅動模型的樣子,如下圖。簡單來說,bus 負責維護註冊進來的devcie 與 driver,每註冊進來一個device 或者 driver 都會調用 Bus->match 函數 將device 與 driver 進行配對,並將它們加入鏈表,如果配對成功,調用Bus->probe或者driver->probe函數。註意:一個device 只能配對一個driver;而一個driver可以對應多個device。其它諸如devices_kset、kobject_uevent這裏不關心,這個涉及的內容比較深入,暫時不去分析。

技術分享圖片

platform平臺設備驅動是基於設備總線驅動模型的,如下圖,這篇主要是記錄平臺設備的驅動與設備的註冊匹配過程

技術分享圖片

以上參考自

platform_bus提供platform_device_register、platform_driver_register等函數供platform_device層與platform_driver調用。

platform_driver屬於驅動層,會在裏面提供file_operations結構體供應用層調用、創建設備節點文件對應相應的驅動

platform_device屬於設備層,會在裏面提供resource資源文件供驅動層調用

下面是一個例子,分別編寫了Led_dev.c設備文件和Led_drv.c驅動文件。列出程序源碼,再根據源碼分析Led_dev與Led_drv註冊與匹配過程

Led_dev.c的程序源碼:

#include <linux/module.h>
#include <linux/version.h>

#include <linux/init.h>

#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/timer.h>
#include <linux/init.h>
#include 
<linux/serial_core.h> #include <linux/platform_device.h> /* 分配/設置/註冊一個platform_device */ static struct resource led_resource[] = { [0] = { .start = 0x56000050, .end = 0x56000050 + 8 - 1, .flags = IORESOURCE_MEM, }, [1] = { .start = 5, .end = 5, .flags = IORESOURCE_IRQ, } }; static void led_release(struct device * dev) { } static struct platform_device led_dev = { .name = "myled", .id = -1, .num_resources = ARRAY_SIZE(led_resource), .resource = led_resource, .dev = { .release = led_release, }, }; static int led_dev_init(void) { platform_device_register(&led_dev); return 0; } static void led_dev_exit(void) { platform_device_unregister(&led_dev); } module_init(led_dev_init); module_exit(led_dev_exit); MODULE_LICENSE("GPL");

Led_drv.c的程序源碼:

/* 分配/設置/註冊一個platform_driver */

#include <linux/module.h>
#include <linux/version.h>

#include <linux/init.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/sched.h>
#include <linux/pm.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/io.h>

static int major;


static struct class *cls;
static volatile unsigned long *gpio_con;
static volatile unsigned long *gpio_dat;
static int pin;

static int led_open(struct inode *inode, struct file *file)
{
    //printk("first_drv_open\n");
    /* 配置為輸出 */
    *gpio_con &= ~(0x3<<(pin*2));
    *gpio_con |= (0x1<<(pin*2));
    return 0;    
}

static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
    int val;

    //printk("first_drv_write\n");

    copy_from_user(&val, buf, count); //    copy_to_user();

    if (val == 1)
    {
        // 點燈
        *gpio_dat &= ~(1<<pin);
    }
    else
    {
        // 滅燈
        *gpio_dat |= (1<<pin);
    }
    
    return 0;
}


static struct file_operations led_fops = {
    .owner  =   THIS_MODULE,    /* 這是一個宏,推向編譯模塊時自動創建的__this_module變量 */
    .open   =   led_open,     
    .write    =    led_write,       
};

static int led_probe(struct platform_device *pdev)
{
    struct resource        *res;

    /* 根據platform_device的資源進行ioremap */
    res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    gpio_con = ioremap(res->start, res->end - res->start + 1);
    gpio_dat = gpio_con + 1;

    res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
    pin = res->start;

    /* 註冊字符設備驅動程序 */

    printk("led_probe, found led\n");

    major = register_chrdev(0, "myled", &led_fops);

    cls = class_create(THIS_MODULE, "myled");

    class_device_create(cls, NULL, MKDEV(major, 0), NULL, "led"); /* /dev/led */
    
    return 0;
}

static int led_remove(struct platform_device *pdev)
{
    /* 卸載字符設備驅動程序 */
    /* iounmap */
    printk("led_remove, remove led\n");

    class_device_destroy(cls, MKDEV(major, 0));
    class_destroy(cls);
    unregister_chrdev(major, "myled");
    iounmap(gpio_con);
    
    return 0;
}


struct platform_driver led_drv = {
    .probe        = led_probe,
    .remove        = led_remove,
    .driver        = {
        .name    = "myled",
    }
};


static int led_drv_init(void)
{
    platform_driver_register(&led_drv);
    return 0;
}

static void led_drv_exit(void)
{
    platform_driver_unregister(&led_drv);
}

module_init(led_drv_init);
module_exit(led_drv_exit);

MODULE_LICENSE("GPL");

將上面兩個程序編譯後得到Led_dev.ko和Led_drv.ko兩個模塊,假設先加載Led_dev.ko文件:insmod Led_drv.ko。

列出執行加載操作後的程序流程:加載後會先調用led_drv_init函數,這個函數只是調用platform_driver_register函數。

platform_driver_register(&led_drv);
    led_drv->driver.bus = &platform_bus_type;//platform_bus_type裏面含有platform_match匹配函數
    led_drv->driver.probe = platform_drv_probe;
    led_drv->driver.remove = platform_drv_remove;
    driver_register(&led_drv->driver);
        bus_add_driver(&led_drv->driver);
            driver_attach(&led_drv->driver);
                bus_for_each_dev(led_drv->driver->bus, NULL, &led_drv->driver, __driver_attach);
                    while ((dev = next_device(&i)) && !error)
                    {
                             __driver_attach(dev, led_drv->driver);
                                     driver_probe_device(drv, dev);
                                         drv->bus->match(&led_dev->dev, drv);//最終調用到這個函數匹配,找到這個函數其實是platform_match函數
                                         really_probe(&led_dev->dev, drv);//匹配成功調用
                            dev->driver = drv;//匹配驅動
                                             drv->probe(dev);//調用led_drv->driver.probe函數
                    }

首先platform_driver_register函數會初始化driver.bus、driver.probe、driver.remove等變量

int platform_driver_register(struct platform_driver *drv)
{
    drv->driver.bus = &platform_bus_type;
    if (drv->probe)
        drv->driver.probe = platform_drv_probe;
    if (drv->remove)
        drv->driver.remove = platform_drv_remove;
    if (drv->shutdown)
        drv->driver.shutdown = platform_drv_shutdown;
    if (drv->suspend)
        drv->driver.suspend = platform_drv_suspend;
    if (drv->resume)
        drv->driver.resume = platform_drv_resume;
    return driver_register(&drv->driver);
}

其中platform_bus_type裏含有platform_match函數,這個函數最終會被調用用來匹配Led_dev與Led_drv。

struct bus_type platform_bus_type = {
    .name        = "platform",
    .dev_attrs    = platform_dev_attrs,
    .match        = platform_match,
    .uevent        = platform_uevent,
    .suspend    = platform_suspend,
    .suspend_late    = platform_suspend_late,
    .resume_early    = platform_resume_early,
    .resume        = platform_resume,
};
static int platform_match(struct device * dev, struct device_driver * drv)
{
    struct platform_device *pdev = container_of(dev, struct platform_device, dev);

    return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);//根據名稱匹配dev與drv
}

接著看到driver_register函數,通過層層調用,最終會從dev鏈表搜索,通過platform_match函數找到與Led_drv匹配的Led_dev。找到後調用really_probe函數,really_probe函數先是執行dev->driver = drv;這樣就將dev與drv聯系起來了,接著調用led_probe函數,初始化驅動。因為這時候還沒有註冊Led_dev.ko所以不會匹配成功。

接著加載Led_dev.ko文件:insmod Led_dev.ko。列出執行加載操作後的程序流程:加載後會先調用led_dev_init函數,這個函數只是調用platform_device_register函數。

platform_device_register(&led_dev);
    platform_device_add(&led_dev)
        device_add(&led_dev->dev);
            bus_attach_device(&led_dev->dev);
                device_attach(&led_dev->dev);
                        if (dev->driver) {//如果設備的驅動程序已經存在
                        {
                            device_bind_driver( &led_dev->dev);//試著鏈接驅動
                        }
                    bus_for_each_drv(led_dev->dev->bus, NULL, &led_dev->dev, __device_attach);
                        while ((drv = next_driver(&i)) && !error)
                        {
                            __device_attach(drv, &led_dev->dev);
                                driver_probe_device(drv, &led_dev->dev);
                                    drv->bus->match(&led_dev->dev, drv);//最終調用到這個函數匹配,找到這個函數其實是platform_match函數
                                    really_probe(&led_dev->dev, drv);//匹配成功調用
                                        rv->bus->match(&led_dev->dev, drv);//最終調用到這個函數匹配,找到這個函數其實是platform_match函數
                                      really_probe(&led_dev->dev, drv);//匹配成功調用
                        } 

通過層層調用,最終定位到device_attach函數與Led_drv不同的是,Led_dev首先會確認自己是否已經有驅動存在,如果不存在才會從drv鏈表搜索,通過platform_match函數找到與Led_dev匹配的Led_drv。找到後調用really_probe函數,really_probe函數先是執行dev->driver = drv;這樣就將dev與drv聯系起來了,接著調用led_probe函數,初始化驅動。

static int led_probe(struct platform_device *pdev)
{
    struct resource        *res;

    /* 根據platform_device的資源進行ioremap */
    res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    gpio_con = ioremap(res->start, res->end - res->start + 1);
    gpio_dat = gpio_con + 1;

    res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
    pin = res->start;

    /* 註冊字符設備驅動程序 */

    printk("led_probe, found led\n");

    major = register_chrdev(0, "myled", &led_fops);

    cls = class_create(THIS_MODULE, "myled");

    class_device_create(cls, NULL, MKDEV(major, 0), NULL, "led"); /* /dev/led */
    
    return 0;
}

在led_probe中,首先會從Led_dev的led_probe中獲得相應的led_resource信息來配置IO端口,接著註冊字符設備,然後創建設備描述符文件。這就是平臺設備驅動模型整個註冊匹配的過程。

與註冊過程相反rmmod Led_drv最終會調用led_remove函數

static int led_remove(struct platform_device *pdev)
{
    /* 卸載字符設備驅動程序 */
    /* iounmap */
    printk("led_remove, remove led\n");

    class_device_destroy(cls, MKDEV(major, 0));
    class_destroy(cls);
    unregister_chrdev(major, "myled");
    iounmap(gpio_con);
    
    return 0;
}

而執行rmmod Led_dev最終會通過層層調用到led_release函數,所以led_release函數必不可少,即使它是空的函數,什麽也沒做

static void led_release(struct device * dev)
{
}

以上全部就是對驅動的分離分層的實現。通過平臺設備驅動模型實現。

Linux驅動之平臺設備驅動模型簡析(驅動分離分層概念的建立)