内核添加dts后,device和device_driver的match匹配的变动:通过compatible属性进行匹配【转】

本文转载自:http://blog.csdn.net/ruanjianruanjianruan/article/details/61622053

内核添加dts后,device和device_driver的match匹配的变动:

先看platform总线:

/driver/base/platform.c文件:

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))//通过这个OpenFirmware的匹配方式(of就是OpenFirmware的缩写)
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);

}

of_driver_match_device这个函数最终调用到__of_match_node()函数,在这个函数里,通过把device_driver的of_match_table(of_device_id结构体的数组)和device里的of_node(device_node结构体)进行匹配,匹配方式是分别比较两者的name、type、和compatible字符串,三者要同时相同(一般name、和type为空,只比较compatible字符串,比较compatible时,是比较整个字符串,不管字符串里的逗号的,直接compare整个字符串)。所以,可以看出,对dts的OpenFirmware device的支持是从最根本的driver-model上支持的,在device和device_driver两者上都加了of_device的成员,也在bus_type的match函数里添加了of_device的匹配语句。所以driver-model的device、device_driver、bus_type都加了支持。

现在内核解析dtb文件,然后创建platform设备时,大部分platform设备是没有名字的,我还纠结那它们怎么和驱动match,原来bus_type的match函数添加了of_driver_match_device()这个匹配方式。他们大部分是通过compatible这个属性匹配成功的(这个compatible也对应dts里的compatible字符串)。

再来看下其他总线的match函数,比如i2c的,也有of_device的匹配方式:

static int i2c_device_match(struct device *dev, struct device_driver *drv)
{
struct i2c_client*client = i2c_verify_client(dev);
struct i2c_driver*driver;

if (!client)
return 0;

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

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

driver = to_i2c_driver(drv);
/* match on an id table if there is one */
if (driver->id_table)
return i2c_match_id(driver->id_table, client) != NULL;

return 0;
}

原文地址:https://www.cnblogs.com/zzb-Dream-90Time/p/8082228.html