除了I2c、spi、I2s、pci、sata、uart、usb总线类型的设备,其中在soc上无法删除的设备都被称为平台设备(platform设备)
一、平台驱动程序platform_driver
static struct platform_driver mypdrv = {
.probe = my_probe,
.remove = my_remove,
.driver = { .name = "my_platform_driver", .owner=THIS_MODULE, },
};
/*probe函数的原型:
static int my_probe(struct platform_device *pdev);
remove函数的原型:
static int my_remove(struct platform_device *pdev);
*/
module_platform_driver(&mypdrv);
/* module_platform_driver(&mypdrv);代替如下流程:
static init __init mypdrv_init(void) {
platform_driver_register(&mydrv);
return 0;
}
static void __exit mydrv_exit(void) {
platform_driver_unregister(&mydrv);
}
module_init(mypdrv_init);
module_exit(mydrv_exit);
*/
类似的module_xxx_driver宏,还有module_sip_driver(struct spi_driver), module_i2c_driver(struct i2c_driver), module_mdio_driver(struct mdio_driver), module_pci_driver(struct pci_driver), module_usb_driver(struct usb_driver)
二、平台设备struct platform_device{}
结构体struct platform_device{}
struct platform_device {
const char* name;
u32 id;
struct resource *resource;
u32 num_resources;
struct device dev;
};
在旧的驱动代码中仍然有使用platform_device_register()注册的情况,现在基本都采用设备自动probe的方式。
三、设备、驱动程序和总线的匹配
在匹配发生之前,linux内核会调用platform_match(struct device *dev, struct device_driver *drv)。平台设备通过字符串和驱动程序匹配。每个总线都维护一个注册的驱动程序和设备列表。总线驱动程序负责设备和驱动程序的匹配。每当连接新设备或者向总线添加新的驱动程序时,总线都会启动匹配循环。
1 设备注册时寻址驱动的方式
假设I2C核心提供的函数注册新的I2C设备。内核通过以下方式I2C总线匹配循环:调用由I2C总线驱动程序注册的I2C核心匹配函数,以检查是否有已注册的驱动程序与该设备匹配。如果没有匹配,则什么都不会发生。如果发现匹配,则内核通知(通过netlink套接口通信机制)设备管理器(udev/mdev),由它加载(如果模块尚未加载)对应的驱动程序模块。一旦驱动程序加载完成,其probe函数就立刻执行。所有的总线的匹配机制都于此雷同。
2 平台设备和平台驱动程序模块匹配方式
宏MODULE_DEVICE_TABLE()会公开设备的id,该表描述驱动程序模块支持哪些设备id。
同时在驱动程序编译成模块时,要求driver.name必须与模块名保持一致,在编译时,内核会从所有驱动程序中提取信息,构建设备表。当特别的设备被加载时,会遍历设备表,如果找到对应的模块,则会加载特定的模块驱动程序,此处会引起新一轮的match过程。
3 platform总线的match匹配流程
static int platform_match(struct device *dev, struct device_driver *drv) {
struct platform_device *pdev = to_platform_device(dev);
struct platfrom_driver *pdrv = to_platform_driver(drv);
/*省略无关内容*/
/*尝试DT方式的匹配*/
if (of_driver_match_device(dev, drv)) {
return 1;
}
/*尝试apci匹配模式*/
if (acpi_driver_match_device(dev, drv)) return 1;
/*尝试匹配ID表*/
if (pdrv->id_table) return platform_match_id(pdrv->id_table, pdev) != NULL;
/*驱动程序名称匹配*/
return (strcmp(pdev->name, drv->name) == 0);
}