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

内核添加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;
}

  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值