Platform

驱动probe函数是如何被调用的

回到我们最常使用的platform_driver_register函数,这其实是一个宏定义,属于函数wrap。

/*
* use a macro to avoid include chaining to get THIS_MODULE
*/
#define platform_driver_register(drv) \
__platform_driver_register(drv, THIS_MODULE)

__platform_driver_register

__platform_driver_register函数定义如下:

/**
* __platform_driver_register - register a driver for platform-level devices
* @drv: platform driver structure
* @owner: owning module/driver
*/
int __platform_driver_register(struct platform_driver *drv,
struct module *owner)
{
drv->driver.owner = owner;
drv->driver.bus = &platform_bus_type;

return driver_register(&drv->driver);
}
EXPORT_SYMBOL_GPL(__platform_driver_register);

该函数设置了驱动的总线,归属权之后,之后进入了driver_register函数。

driver_register

int driver_register(struct device_driver *drv)
{
int ret;
struct device_driver *other;

if (!bus_is_registered(drv->bus)) {
pr_err("Driver '%s' was unable to register with bus_type '%s' because the bus was not initialized.\n",
drv->name, drv->bus->name);
return -EINVAL;
}

if ((drv->bus->probe && drv->probe) ||
(drv->bus->remove && drv->remove) ||
(drv->bus->shutdown && drv->shutdown))
pr_warn("Driver '%s' needs updating - please use "
"bus_type methods\n", drv->name);

other = driver_find(drv->name, drv->bus);
if (other) {
pr_err("Error: Driver '%s' is already registered, "
"aborting...\n", drv->name);
return -EBUSY;
}

ret = bus_add_driver(drv);
if (ret)
return ret;
ret = driver_add_groups(drv, drv->groups);
if (ret) {
bus_remove_driver(drv);
return ret;
}
kobject_uevent(&drv->p->kobj, KOBJ_ADD);
deferred_probe_extend_timeout();

return ret;
}
EXPORT_SYMBOL_GPL(driver_register);

driver_register函数首先就是错误检查,这些可以先跳过不看。

bus_add_driver

重点函数是bus_add_driver,这个函数将驱动挂载到总线上,然后进行匹配。

int bus_add_driver(struct device_driver *drv)
{
struct subsys_private *sp = bus_to_subsys(drv->bus);

...
if (sp->drivers_autoprobe) {
error = driver_attach(drv);
...
}

这里我们先不分析怎么将驱动挂载到总线上,只看如何匹配。

sp->drivers_autoprobe变量现在派上用场了,当注册驱动的时候,如果该变量设置为1,就会执行driver_attach

driver_attach

该函数定义如下:

int driver_attach(const struct device_driver *drv)
{
/* The (void *) will be put back to const * in __driver_attach() */
return bus_for_each_dev(drv->bus, NULL, (void *)drv, __driver_attach);
}
EXPORT_SYMBOL_GPL(driver_attach);

对于总线上的每一个设备,都执行__driver_attach函数,将drv作为参数传给__driver_attach函数。

__driver_attach

__driver_attach函数实现如下:

static int __driver_attach(struct device *dev, void *data)
{
const struct device_driver *drv = data;
bool async = false;
int ret;

/*
* Lock device and try to bind to it. We drop the error
* here and always return 0, because we need to keep trying
* to bind to devices and some drivers will return an error
* simply if it didn't support the device.
*
* driver_probe_device() will spit a warning if there
* is an error.
*/

ret = driver_match_device(drv, dev);
if (ret == 0) {
/* no match */
return 0;
} else if (ret == -EPROBE_DEFER) {
dev_dbg(dev, "Device match requests probe deferral\n");
dev->can_match = true;
driver_deferred_probe_add(dev);
/*
* Driver could not match with device, but may match with
* another device on the bus.
*/
return 0;
} else if (ret < 0) {
dev_dbg(dev, "Bus failed to match device: %d\n", ret);
/*
* Driver could not match with device, but may match with
* another device on the bus.
*/
return 0;
} /* ret > 0 means positive match */

...

__device_driver_lock(dev, dev->parent);
driver_probe_device(drv, dev);
__device_driver_unlock(dev, dev->parent);

return 0;
}

这里重点关注driver_match_devicedriver_probe_device函数。

driver_match_device

driver_match_device函数定义如下:

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

这里就是判断总线有没有实现自己的match函数,如果没有实现,默认返回1,也就是匹配成功。

还记得platform_bus的结构体吗?他是有实现自己的一套match函数的。

函数定义如下:

static int platform_match(struct device *dev, const 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);
}

首先将通用的device和driver都转换成platform下的设备和驱动。

然后再进行匹配,如果匹配成功,则返回1。

回到__driver_attach函数中,最后调用了driver_probe_device函数。

看名字就知道这个函数应该就跟probe有关了。

driver_probe_device

driver_probe_device函数定义如下

static int driver_probe_device(const struct device_driver *drv, struct device *dev)
{
int trigger_count = atomic_read(&deferred_trigger_count);
int ret;

atomic_inc(&probe_count);
ret = __driver_probe_device(drv, dev);
if (ret == -EPROBE_DEFER || ret == EPROBE_DEFER) {
driver_deferred_probe_add(dev);

/*
* Did a trigger occur while probing? Need to re-trigger if yes
*/
if (trigger_count != atomic_read(&deferred_trigger_count) &&
!defer_all_probes)
driver_deferred_probe_trigger();
}
atomic_dec(&probe_count);
wake_up_all(&probe_waitqueue);
return ret;
}

这里我们只关心__driver_probe_device函数的调用。

__driver_probe_device

其实现如下:

static int __driver_probe_device(const struct device_driver *drv, struct device *dev)
{
int ret = 0;

...
if (initcall_debug)
ret = really_probe_debug(dev, drv);
else
ret = really_probe(dev, drv);
...
}

可以看到这里调用了really_probe,至于带debug后缀的肯定就先不用关心了。

really_probe

really_probe函数定义如下:

static int really_probe(struct device *dev, const struct device_driver *drv)
{
...

ret = call_driver_probe(dev, drv);
if (ret) {
/*
* If fw_devlink_best_effort is active (denoted by -EAGAIN), the
* device might actually probe properly once some of its missing
* suppliers have probed. So, treat this as if the driver
* returned -EPROBE_DEFER.
*/
if (link_ret == -EAGAIN)
ret = -EPROBE_DEFER;

/*
* Return probe errors as positive values so that the callers
* can distinguish them from other errors.
*/
ret = -ret;
goto probe_failed;
}

...
}

与probe有关的只有call_driver_probe.

call_driver_probe

call_driver_probe函数定义如下:

static int call_driver_probe(struct device *dev, const struct device_driver *drv)
{
int ret = 0;

if (dev->bus->probe)
ret = dev->bus->probe(dev);
else if (drv->probe)
ret = drv->probe(dev);

switch (ret) {
case 0:
break;
case -EPROBE_DEFER:
/* Driver requested deferred probing */
dev_dbg(dev, "Driver %s requests probe deferral\n", drv->name);
break;
case -ENODEV:
case -ENXIO:
dev_dbg(dev, "probe with driver %s rejects match %d\n",
drv->name, ret);
break;
default:
/* driver matched but the probe failed */
dev_err(dev, "probe with driver %s failed with error %d\n",
drv->name, ret);
break;
}

return ret;
}

终于,看到了probe的调用,这里bus->probe的优先级是最高的,其次才是drv->probe.

bus->probe也就是platform总线的probe,看下具体源码:

static int platform_probe(struct device *_dev)
{
struct platform_driver *drv = to_platform_driver(_dev->driver);
struct platform_device *dev = to_platform_device(_dev);
int ret;

...

if (drv->probe) {
ret = drv->probe(dev);
}
...
}

经过了一系列处理之后,由platform_probe调用了drv->probe,也就是我们驱动程序中设置的probe。

总结

驱动注册时,probe调用顺序如下:

__platform_driver_register
=> driver_register
=> bus_add_driver
=> driver_attach
=> __driver_attach
=> driver_match_device => bus->match => platform_match
=> driver_probe_device
=> __driver_probe_device
=> really_probe
=> call_driver_probe
=> bus->probe => platform_probe
=> drv->probe