嵌入式软件的bus_type 结构中的一个成员 .match 成员,是一个函数。用来在现有的设备驱动中匹配新插入的设备。匹配方式有4种(设备树、ACPI,id_tables, name)。
以platform总线为例,platform_bus_type 实例的match 为 platform_match,
platform_match 的代码为,很简单的描述4种匹配方式。
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);
/* When driver_override is set, only bind to the matching driver */
if (pdev->driver_override)
return !strcmp(pdev->driver_override, drv->name);
/* 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_driver_match_device 函数的调用关系,以理解设备树匹配的流程,进而理解id_table,name匹配的部分流程。
of_driver_match_device 在设备树中发现有与驱动匹配的设备,就会返回1,进而match函数返回1.
调用关系为:
of_driver_match_device(struct device *dev, const struct device_driver *drv)
|-- of_match_device(struct of_device_id, struct device)
|-- of_match_node(const struct of_device_id , const struct device_node )
|–__of_match_node (const struct of_device_id , const struct device_node )
最终调用 __of_match_node 进行匹配函数的具体执行。该函数会循环对比所有的驱动程序,以选择score 最好的那个驱动作为该设备的驱动。对比程序为 of_device_is_compatible(const struct device_node ,
const char *compat, const char *type, const char *name)
该函数会根据驱动的compatible、type、name三个字段的某种组合,共11 种,返回 score 值。在of_match"_node 中选择最好的 score,它对应的match 即为最优驱动程序。
over。