我正在阅读围绕总线、设备和驱动程序构建的 Linux 设备模型
.我能理解一些关于设备和驱动程序匹配是如何发生的,但不清楚总线在这里的作用,总线如何与设备匹配。
我对平台设备从哪里得名还有一个疑问。
“平台总线,只是将每个设备的名称与每个驱动程序的名称进行比较;如果它们相同,则设备匹配驱动程序。”
现在我真的不能理解上面的一点。我相信首先在dts文件中定义设备名称,然后在平台驱动程序代码中定义相应的驱动程序名称。
如果这两个名称匹配,则从驱动程序代码调用探测器,这将确认设备确实存在。
有人可以让我特别从巴士的角度了解整个过程。
请您参考如下方法:
添加到@Federico 的回答中,它很好地描述了一般情况,平台设备可以使用四件事(优先级)与平台驱动程序匹配。这是match function平台“巴士”:
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 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风格搭配
使用设备树 (
of_driver_match_device ) 进行匹配。如果你还不知道设备树的概念,
go read about it .在这个数据结构中,每个设备在代表系统的树中都有自己的节点。每个设备还有一个
compatible属性是一个字符串列表。如果任何平台驱动程序声明了
compatible 之一字符串被支持,将有一个匹配,驱动程序的探测器将被调用。
这是一个 example of a node :
gpio0: gpio@44e07000 {
compatible = "ti,omap4-gpio";
ti,hwmods = "gpio1";
gpio-controller;
#gpio-cells = <2>;
interrupt-controller;
#interrupt-cells = <1>;
reg = <0x44e07000 0x1000>;
interrupts = <96>;
};
这描述了 GPIO Controller 。它只有一个兼容字符串,即
ti,omap4-gpio .任何声明相同兼容字符串的注册平台驱动程序都将被探测。这是
its driver :
static const struct of_device_id omap_gpio_match[] = {
{
.compatible = "ti,omap4-gpio",
.data = &omap4_pdata,
},
{
.compatible = "ti,omap3-gpio",
.data = &omap3_pdata,
},
{
.compatible = "ti,omap2-gpio",
.data = &omap2_pdata,
},
{ },
};
MODULE_DEVICE_TABLE(of, omap_gpio_match);
static struct platform_driver omap_gpio_driver = {
.probe = omap_gpio_probe,
.driver = {
.name = "omap_gpio",
.pm = &gpio_pm_ops,
.of_match_table = of_match_ptr(omap_gpio_match),
},
};
该驱动程序能够驱动三种类型的 GPIO,包括前面提到的一种。
请注意,平台设备不会神奇地添加到平台总线中。架构/板初始化将调用
platform_device_add或
platform_add_devices ,在这种情况下,借助 OF 函数来扫描树。
姓名匹配
如果你看
platform_match ,您将看到匹配回退到名称匹配。在驱动程序名称和设备名称之间进行简单的字符串比较。这就是旧平台驱动程序的工作方式。他们中的一些人仍然这样做,例如
this one here :
static struct platform_driver imx_ssi_driver = {
.probe = imx_ssi_probe,
.remove = imx_ssi_remove,
.driver = {
.name = "imx-ssi",
.owner = THIS_MODULE,
},
};
module_platform_driver(imx_ssi_driver);
同样,板特定的初始化必须调用
platform_device_add或
platform_add_devices添加平台设备,在名称匹配的情况下,这些设备完全在 C 中静态创建(名称在 C 中给出,资源如 IRQ 和基地址等)。




