我看到一个场景,其中“struct of_device_id”未在驱动程序中定义,但在设备树(dts)中为同一设备条目添加了文件兼容字符串。
以下是芯片的示例设备树条目。
&i2c1 {
...
adv7ex: adv7ex@4a {
compatible = "adv7ex";
reg = <0x4a>;
};
...
};
以下是芯片驱动程序的示例代码段,该代码片段注册为I2C驱动程序。
static struct i2c_device_id adv7ex_id[] = {
{ "adv7ex", ADV7EX },
{ }
};
MODULE_DEVICE_TABLE(i2c, adv7ex_id);
static struct i2c_driver adv7ex_driver = {
.driver = {
.owner = THIS_MODULE,
.name = "adv7ex",
},
.probe = adv7ex_probe,
.remove = adv7ex_remove,
.id_table = adv7ex_id,
};
module_i2c_driver(adv7ex_driver);
由于驱动程序中没有“of_device_id”结构定义,请您帮助我理解设备到驱动程序绑定是如何发生的。
答案 0 :(得分:1)
实际上,不是内核会加载你的驱动程序,而是用户空间工具:
MODULE_DEVICE_TABLE(i2c, adv7ex_id);
此宏在最终编译模块(即:.ko文件)中添加特定符号名称,这些名称将由depmod工具解析,然后添加&#34;引用&#34;到module.alias中的驱动程序,最后你的驱动程序将由你的用户hotplug工具加载。
答案 1 :(得分:1)
我有一个类似的案例,终于找到了解释: 设备树i2c设备绑定中似乎存在未记录的扭曲。
让我们看看i2c_device_match()(i2c-core-base.c):
/* Attempt an OF style match */
if (i2c_of_match_device(drv->of_match_table, client))
return 1;
在i2c_of_match_device()(i2c-core-of.c)中到底发生了什么?
*i2c_of_match_device(const struct of_device_id *matches,
struct i2c_client *client){
const struct of_device_id *match;
if (!(client && matches))
return NULL;
match = of_match_device(matches, &client->dev);
if (match)
return match;
return i2c_of_match_device_sysfs(matches, client); }
嗯,我们首先尝试使用兼容字段进行开放固件风格的匹配,但是如果失败,我们仍然调用i2c_of_match_device_sysfs()。 它是做什么的?
i2c_of_match_device_sysfs(const struct of_device_id *matches,
struct i2c_client *client) {
const char *name;
for (; matches->compatible[0]; matches++) {
/*
* Adding devices through the i2c sysfs interface provides us
* a string to match which may be compatible with the device
* tree compatible strings, however with no actual of_node the
* of_match_device() will not match
*/
if (sysfs_streq(client->name, matches->compatible))
return matches;
name = strchr(matches->compatible, ',');
if (!name)
name = matches->compatible;
else
name++;
if (sysfs_streq(client->name, name))
return matches;
}
return NULL; }
宾果! 从代码中可以看到,i2c_of_match_device_sysfs()将设备树中的 compatible 字符串与驱动程序 i2c_device_id 的 name 字段进行比较。 另外,如果 compatible 字段中有逗号,则会对逗号后面的部分进行匹配。
因此,在您的情况下,设备树数据
compatible = "adv7ex"
与
中的“ adv7ex” 匹配static struct i2c_device_id adv7ex_id[] = {
{ "adv7ex", ADV7EX },
{ } };
MODULE_DEVICE_TABLE(i2c, adv7ex_id);
即使您的兼容是“ acme-inc,adv7ex”(如“设备树”的推荐表示法一样),它仍然会匹配。
答案 2 :(得分:0)
正如您所见here,i2c_device_match()
函数首先尝试通过compatible
字符串匹配设备(OF样式,即设备树)。如果失败,则尝试按id表匹配设备。