linux 驱动-匹配1 (platform_bus_type)

目录

匹配入口

[匹配顺序(platform_match )](#匹配顺序(platform_match ))

方式1:

方式2(重点)

方式3

方式4 (重点)


匹配入口

driver_match_device 实际调用platform_match

cpp 复制代码
driver_match_device(drv, dev);

static inline int driver_match_device(struct device_driver *drv,
				      struct device *dev)
{
	return drv->bus->match ? drv->bus->match(dev, drv) : 1;
}

匹配顺序(platform_match )

static struct platform_driver bcm2835_rng_driver = {

.driver = {

.name = "bcm2835-rng", 方式1 /方式5

.of_match_table = bcm2835_rng_of_match, 方式2 /方式3()

},

.probe = bcm2835_rng_probe,

.id_table = bcm2835_rng_devtype, 方式4

};

cpp 复制代码
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)    	//方式1: 根据device_driver 的name
		return !strcmp(pdev->driver_override, drv->name);

	/* Attempt an OF style match first */
	if (of_driver_match_device(dev, drv))  //方式2: 根据device_driver的of_match_table
		return 1;                              去匹配device_node的compatibale/type/name

	/* Then try ACPI style match */
	if (acpi_driver_match_device(dev, drv))//方式3: 根据 device_driver的drv->acpi_match_table, drv->of_match_table
		return 1;

	/* Then try to match against the id table */
	if (pdrv->id_table)			 //方式4:根据platform_driver驱动的id_table(id->name 与pdev->name)
		return platform_match_id(pdrv->id_table, pdev) != NULL;

	/* fall-back to driver name match */
	return (strcmp(pdev->name, drv->name) == 0);
						//方式5: 根据device_driver的name
}

方式1:

说明:driver_override 主要用于调试驱动,强制指定一个驱动来绑定到这个设备

操作: 用户空间通过特定的接口(如 sysfs)来设置这个值

实例: echo "my_driver" > /sys/bus/platform/devices/my_device/driver_override

方式2(重点)

根据of_device_id 的 compatible, type, name顺序去匹配device_node的compatible, type, name

方式3

acpi 方式,暂略

方式4 (重点)

static struct platform_device_id bcm2835_rng_devtype\[\] = {

{ .name = "bcm2835-rng" },

{ .name = "bcm63xx-rng" },

{ /* sentinel */ }

};

struct platform_device_id {

char namePLATFORM_NAME_SIZE;

kernel_ulong_t driver_data;

};

static const struct platform_device_id *platform_match_id(

const struct platform_device_id *id, struct platform_device *pdev)

{

while (id->name0) {

if (strcmp(pdev->name, id->name) == 0) {

pdev->id_entry = id; 填充platform_device 的id_entry

return id;

}

id++;

}

return NULL;

}

方式5:

strcmp(pdev->name, drv->name) == 0

相关推荐
新时代牛马34 分钟前
Linux 驱动中断与定时完整篇:从 request_threaded_irq 到hrtimer 选型与排障
linux·运维·服务器
HRTOS2 小时前
HRTOS 4.0 驱动库:06_Storage 存储设备驱动——24C02 EEPROM与I²C驱动开发
c语言·驱动开发·嵌入式硬件·51单片机
分支预测失败3 小时前
RISC-V 核间中断 IPI 实战:从 MSIP 寄存器到 IMSIC 消息投递
linux·后端
GeW4 小时前
RHCE考试避坑指南"90%考生容易忽略的细节
linux
键盘会跳舞5 小时前
【C++多线程】死锁诊断工具实战:从 Windows 到 Linux 的全链路排查
linux·c++·windows·死锁
拂拉氏6 小时前
【知识讲解】 Linux环境变量的认识与使用
linux·环境变量
zhangrelay6 小时前
答疑-是否软件源问题都必须更换为国内呢-
linux·笔记·学习·ubuntu·ros2
zhangrelay6 小时前
《ROS 机器人程序设计》课程教学大纲-2026- kinetic-lyrical
linux·笔记·学习·ubuntu·机器人
KFCgrandpahhh6 小时前
rk3576点灯
linux·驱动开发
HRTOS6 小时前
HRTOS 4.0 正式发布:Driver Library 基础外设驱动模块介绍
驱动开发·单片机·嵌入式硬件·51单片机