Linux platform system

来源:互联网 发布:杭州哪里淘宝店多 编辑:程序博客网 时间:2024/05/16 06:26

Linux platform system

platform是Linux内的一种虚拟总线,称为platform总线,相应的设备称为platform_device,而驱动称为platform_driver。platform总线、设备和驱动这3个实体,总线将设备和驱动绑定,在系统每注册一个设备的时候,会寻找与之匹配的驱动;相反的,在系统每注册一个驱动的时候,会寻找与之匹配的设备,而匹配由总线完成。

先来看下platform的框架:
这里写图片描述
platform框架其实比较直观,就总线/设备/驱动三部分,所以platform的实现一般就为三个步骤,先对platform设备进行注册,再编写platform驱动,最后设备和驱动进行匹配,匹配成功则执行probe探测函数,probe函数里面对具体的功能实现,下面对这三步的内容进行简单分析。

1.platform device

platform设备对应的结构体paltform_device,位于linux/platform_device.h中,如下:

struct platform_device {    const char  * name;    //设备的名字,这将代替device->dev_id,用作sys/device下显示的目录名    int     id;            //设备id,用于给插入给该总线并且具有相同name的设备编号,如果只有一个设备的话填-1。    struct device   dev;   //结构体中内嵌的device结构体。    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封装了指定的名字name、id、内嵌device。

platform_device的注册一般在平台设备里面实现,这边举例说明下:

1.先要为设备提供platform_device结构体,并赋值,这边以i2c为例。

static struct resource s3c_i2c_resource[] = {    [0] = {        .start = S3C_PA_IIC,        .end   = S3C_PA_IIC + SZ_4K - 1,        .flags = IORESOURCE_MEM,    },    [1] = {        .start = IRQ_IIC,        .end   = IRQ_IIC,        .flags = IORESOURCE_IRQ,    },};struct platform_device s3c_device_i2c0 = {    .name         = "s3c-i2c",    .id       = -1,    .num_resources    = ARRAY_SIZE(s3c_i2c_resource),    .resource     = s3c_i2c_resource,};

2.有了各设备的platform_device后,一般会统一放到用来注册的platform_device,里面会包含各自platform设备,如下:

static struct platform_device *s3c_devices[] __initdata = {    &s3c_device_i2c0,    &s3c_device_spi,    &s3c_device_hsmmc0,    &s3c_device_hsmmc1,}

3.最后使用platform_add_devices进行将device添加到platform总线上。

platform_add_devices(s3c_devices, ARRAY_SIZE(s3c_devices));

该函数位于drivers/base/platform.c中,如下:

int platform_add_devices(struct platform_device **devs, int num){    int i, ret = 0;    for (i = 0; i < num; i++) {        ret = platform_device_register(devs[i]);        if (ret) {            while (--i >= 0)                platform_device_unregister(devs[i]);            break;        }    }    return ret;}

可以看到platform_device会扫描s3c_devices里面的每一个设备,并为它们进行注册,这边的注册和注销使用platform_device_register()platform_device_unregister()函数。

注册后,同样会在/sys/device/目录下创建一个以name命名的目录,并且创建软连接到/sys/bus/platform/device下。

2.platform driver

platform驱动对应的结构体paltform_driver,位于linux/platform_device.h中,如下:

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;};

与device类似,driver里面嵌入的是device_driver,不过driver里面有实现函数probe、remove等。
platform_driver的实现也比较简单,这边也以i2c为例,如下:

static struct platform_driver s3c_i2c_driver = {    .probe      = s3c_i2c_probe,    .remove     = s3c_i2c_remove,    .id_table   = s3c_driver_ids,    .driver     = {        .owner  = THIS_MODULE,        .name   = "s3c-i2c",        .pm = S3C_DEV_PM_OPS,    },};static int __init i2c_adap_s3c_init(void){    return platform_driver_register(&s3c24xx_i2c_driver);}static void __exit i2c_adap_s3c_exit(void){    platform_driver_unregister(&s3c_i2c_driver);}module_init(i2c_adap_s3c_init);module_exit(i2c_adap_s3c_exit);

platform驱动的注册和注销分别用platform_driver_register()platform_driver_unregister()函数。

3.platform match

platform driver编写完成后,就是要执行driver的probe函数。probe函数能否执行的关键在于,device中的name与driver中的name是否相等,相等时说明匹配成功,就可以执行probe函数实现具体的功能了。

这边也追踪下如何执行到match函数,驱动会调用platform_driver_register进行注册,该函数会将总线指向platform总线,而platform总线的结构体如下:

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

可以看到platform_matchplatform_bus的一个成员,platform_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 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);}

platform_match会调用platform_match_id()函数,匹配函数platform_match_id()会去搜索所有已经注册到platform总线上的设备,如果匹配到则返回id,没找到则返回NULL如下:

static const struct platform_device_id *platform_match_id(            const struct platform_device_id *id,            struct platform_device *pdev){    while (id->name[0]) {        printk("pdev->name:%s\n",pdev->name);        printk("id->name:%s\n",id->name);        if (strcmp(pdev->name, id->name) == 0) {            pdev->id_entry = id;            return id;        }        id++;    }    return NULL;}

这边两句printk调试信息是我自己加上去的,在调试的时候可以很直观的观察到两个name是否相等,在Linux驱动中很多设备都是通过这一原理来进行device和driver的匹配,如下面几个模块:
1.drivers/i2c/i2c-core.c中的i2c_match_id

static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,                        const struct i2c_client *client){    while (id->name[0]) {        if (strcmp(client->name, id->name) == 0)            return id;        id++;    }    return NULL;}

2.drivers/spi/spi.c中的spi_match_id

static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,                        const struct spi_device *sdev){    while (id->name[0]) {        if (!strcmp(sdev->modalias, id->name))            return id;        id++;    }    return NULL;}

3.drivers/pci/pci-driver.c中的pci_match_id

const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,                     struct pci_dev *dev){    if (ids) {        while (ids->vendor || ids->subvendor || ids->class_mask) {            if (pci_match_one_device(ids, dev))                return ids;            ids++;        }    }    return NULL;}

4.drivers/usb/core/driver.c中的usb_match_id

const struct usb_device_id *usb_match_id(struct usb_interface *interface,                     const struct usb_device_id *id){    if (id == NULL)        return NULL;    for (; id->idVendor || id->idProduct || id->bDeviceClass ||           id->bInterfaceClass || id->driver_info; id++) {        if (usb_match_one_id(interface, id))            return id;    }    return NULL;}

5.drivers/hid/hid-core.c中的hid_match_id

static const struct hid_device_id *hid_match_id(struct hid_device *hdev,        const struct hid_device_id *id){    for (; id->bus; id++)        if (hid_match_one_id(hdev, id))            return id;    return NULL;}

如果发现打印的name不一样或有一个没有打印出来,这是候就要去检测对应的device和driver,可能就是总线没注册上或驱动id、name不一致。

Linux platform system的分析就到这边,有感悟时会持续会更新。

注:以上内容都是本人在学习过程积累的一些心得,难免会有参考到其他文章的一些知识,如有侵权,请及时通知我,我将及时删除或标注内容出处,如有错误之处也请指出,进行探讨学习。文章只是起一个引导作用,详细的数据解析内容还请查看Linux相关教程,感谢您的查阅。

1 0