本文轉載自: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;
}