linux操作系统中,外围的设备驱动,都是通过 bus + device + driver架构来管理的,其实也好理解,外设都是通过总线来与cpu通讯的。内核通过实现各种总线的规范,来进行设备管理、设备检测、驱动绑定等,对于驱动程序来说,只需要注册自己的驱动,实现对设备的读写控制即可。
涉及文件:linux/drivers/base/bus.c、platform.c、driver.c和linux/include/linux/platform_device.h等。
这类驱动通常是3个层次:总线子系统 + 设备模块 + 驱动模块,它的流程大概是:
1、platform_bus注册
platform是一种虚拟总线,platform机制将设备本身的资源注册进内核,对内核进行统一管理,在驱动程序使用这些资源时使用统一的接口,可这样提高了程序的可移植性。
路径:linux/drivers/base/platform.c
结构:
struct bus_type platform_bus_type = {
.name = "platform",
.dev_groups = platform_dev_groups,
.match = platform_match,
.uevent = platform_uevent,
.pm = &platform_dev_pm_ops,
};
字段解释:
name总线名字,总线注册后新建目录sys/bus/platform
match有驱动设备注册到platform总线时,内核自动调用match函数,判断设备和驱动的name是否一致。
/*通过bus_register注册platform_bus,bus_register定义在linux/drivers/base/bus.c中*/
int __init platform_bus_init(void)
{
int error;
early_platform_cleanup();
error = device_register(&platform_bus);
if (error)
return error;
error = bus_register(&platform_bus_type);
if (error)
device_unregister(&platform_bus);
of_platform_register_reconfig_notifier();
return error;
}
bus_register(*bus)?这个函数会生成两个list,用来保存设备和驱动。
/**
* bus_register - register a driver-core subsystem
* @bus: bus to register
*
* Once we have that, we register the bus with the kobject
* infrastructure, then register the children subsystems it has:
* the devices and drivers that belong to the subsystem.
*/
int bus_register(struct bus_type *bus)
{
int retval;
struct subsys_private *priv;
struct lock_class_key *key = &bus->lock_key;
priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
if (!priv)
return -ENOMEM;
...
...
...
priv->devices_kset = kset_create_and_add("devices", NULL,
&priv->subsys.kobj);
if (!priv->devices_kset) {
retval = -ENOMEM;
goto bus_devices_fail;
}
priv->drivers_kset = kset_create_and_add("drivers", NULL,
&priv->subsys.kobj);
if (!priv->drivers_kset) {
retval = -ENOMEM;
goto bus_drivers_fail;
}
INIT_LIST_HEAD(&priv->interfaces);
__mutex_init(&priv->mutex, "subsys mutex", key);
klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
klist_init(&priv->klist_drivers, NULL, NULL);
....
...
...
}
2、platform device注册
定义在linux/include/linux/platform_device.h,定义如下:
struct platform_device {
const char *name;
int id;
bool id_auto;
struct device dev;
u32 num_resources;
struct resource *resource;
const struct platform_device_id *id_entry;
char *driver_override; /* Driver name to force a match */
/* MFD cell pointer */
struct mfd_cell *mfd_cell;
/* arch specific additions */
struct pdev_archdata archdata;
};
字段解释:
dev:基本device结构
num_resources:资源数
*resource:资源
注册函数定义在linux/drivers/base/platform.c中,具体如下:
/**
* platform_device_register - add a platform-level device
* @pdev: platform device we're adding
*/
int platform_device_register(struct platform_device *pdev)
{
device_initialize(&pdev->dev);//初始化device中dev结构体
arch_setup_pdev_archdata(pdev);
return platform_device_add(pdev);//将device添加到platform_bus中
}
其中 platform_device_add,
/**
* platform_device_add - add a platform device to device hierarchy
* @pdev: platform device we're adding
*
* This is part 2 of platform_device_register(), though may be called
* separately _iff_ pdev was allocated by platform_device_alloc().
*/
int platform_device_add(struct platform_device *pdev)
{
int i, ret;
if (!pdev)
return -EINVAL;
if (!pdev->dev.parent)
pdev->dev.parent = &platform_bus; //dev父设备为platform_bus
pdev->dev.bus = &platform_bus_type;//dev的bus 类型为platform
switch (pdev->id) { //设置dev 名字
default:
dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
break;
case PLATFORM_DEVID_NONE:
dev_set_name(&pdev->dev, "%s", pdev->name);
break;
case PLATFORM_DEVID_AUTO:
/*
* Automatically allocated device ID. We mark it as such so
* that we remember it must be freed, and we append a suffix
* to avoid namespace collision with explicit IDs.
*/
ret = ida_simple_get(&platform_devid_ida, 0, 0, GFP_KERNEL);
if (ret < 0)
goto err_out;
pdev->id = ret;
pdev->id_auto = true;
dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
break;
}
for (i = 0; i < pdev->num_resources; i++) {//添加该设备的resource
struct resource *p, *r = &pdev->resource[i];
if (r->name == NULL)
r->name = dev_name(&pdev->dev);
p = r->parent;
if (!p) {
if (resource_type(r) == IORESOURCE_MEM)
p = &iomem_resource;
else if (resource_type(r) == IORESOURCE_IO)
p = &ioport_resource;
}
if (p && insert_resource(p, r)) {
dev_err(&pdev->dev, "failed to claim resource %d\n", i);
ret = -EBUSY;
goto failed;
}
}
pr_debug("Registering platform device '%s'. Parent at %s\n",
dev_name(&pdev->dev), dev_name(pdev->dev.parent));
ret = device_add(&pdev->dev);//添加该设备到platform总线
if (ret == 0)
return ret;
failed:
if (pdev->id_auto) {
ida_simple_remove(&platform_devid_ida, pdev->id);
pdev->id = PLATFORM_DEVID_AUTO;
}
while (--i >= 0) {
struct resource *r = &pdev->resource[i];
if (r->parent)
release_resource(r);
}
err_out:
return ret;
}
这里主要是通过device_add函数将device添加到platform,在函数里调用bus_probe_device调用总线上与之匹配的driver的probe函数。
在bus_probe_device函数中主要调用device_initial_probe,
void device_initial_probe(struct device *dev)
{
__device_attach(dev, true);
}
在__device_attach中主要的代码如下:
ret = bus_for_each_drv(dev->bus, NULL, &data,
__device_attach_driver);//遍历总线上的driver,并调用__device_attach_driver
在__device_attach_driver
中主要调用driver_match_device
判断driver和device是否match,如果match则调用driver_probe_device
来probe driver
static inline int driver_match_device(struct device_driver *drv,
struct device *dev)
{
return drv->bus->match ? drv->bus->match(dev, drv) : 1;//drv所属bus的match函数。
}
bus->match即platform bus的match 函数:
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);//比较driver和device的名字。
}
在driver_probe_device中主要调用really_probe,really_probe的主要代码如下。
if (dev->bus->probe) {//如果bus定义了probe,就调用bus的probe
ret = dev->bus->probe(dev);
if (ret)
goto probe_failed;
} else if (drv->probe) {//调用bus所属driver的probe函数。
ret = drv->probe(dev);
if (ret)
goto probe_failed;
}
3、platform driver注册
platform_drive同样定义在linux/include/linux/platform_device.h,这里其中就包含probe函数
struct platform_driver {
int (*probe)(struct platform_device *);
int (*remove)(struct platform_device *);
void (*shutdown)(struct platform_device *);
int (*suspend)(struct platform_device *, pm_message_t state);
int (*resume)(struct platform_device *);
struct device_driver driver;
const struct platform_device_id *id_table;
bool prevent_deferred_probe;
};
字段解析:
probe:platform driver注册时,如果总线上已有匹配的device,将调用对应的probe函数。
driver:基本的device driver结构,platform driver注册成功后,/sys/bus/platform/driver/目录下创建一个名字为driver->name的目录
int __platform_driver_register(struct platform_driver *drv,
struct module *owner)
{
drv->driver.owner = owner;
drv->driver.bus = &platform_bus_type;//driver的bus type为Platform bus
drv->driver.probe = platform_drv_probe;//driver的probe函数,如果platform driver定义了probe函数,里面调用的platform定义的probe
drv->driver.remove = platform_drv_remove;
drv->driver.shutdown = platform_drv_shutdown;
return driver_register(&drv->driver);//主要注册函数。
}
在driver_register函数中,通过driver_find查找driver是否已经注册,如果没有注册,则调用bus_add_driver将driver add到bus。
在bus_add_driver最主要的操作是driver 和匹配的device进行绑定。
int driver_attach(struct device_driver *drv)
{
return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);//遍历bus上所有的device,并调用__driver_attach
}
在__driver_attach函数中与上面__device_attach_driver过程类似,调用driver_match_device判断driver和device是否match,如果match则调用driver_probe_device来probe driver。如果bus上没有注册过与platform driver匹配的platform device,那platform driver的probe函数将不会调用到。
从上述注册过程可以看出platform device和platform driver注册并不需要区分先后。
设备挂接到总线上时,与总线上的所有驱动进行匹配(用bus_type.match进行匹配)。如果匹配成功,则调用bus_type.probe或者driver.probe初始化该设备;挂接到总线上如果匹配失败,则只是将该设备挂接到总线上。
驱动挂接到总线上时,与总线上的所有设备进行匹配(用bus_type.match进行匹配)。如果匹配成功,则调用bus_type.probe或者driver.probe初始化该设备;挂接到总线上如果匹配失败,则只是将该驱动挂接到总线上。
另外,还有两点值得研究
-
1、driver中probe是何时执行的呢?
https://blog.csdn.net/oqqHuTu12345678/article/details/78934576 -
2、DTS 架构下Device与Driver的匹配
https://www.dazhuanlan.com/timothyye/topics/987330
https://www.cnblogs.com/downey-blog/p/10485596.html
https://www.cnblogs.com/downey-blog/p/10486568.html