platform总线的probe函数调用

    咱们知道,platform总线提供了设备和驱动的mach函数,当设备和驱动匹配完成后,就会执行驱动的probe函数,可是这个probe函数是如何被调用的呢。ide

    probe函数在设备驱动注册最后收尾工做,当设备的device 和其对应的driver 在总线上完成配对以后,系统就调用platform设备的probe函数完成驱动注册最后工做。资源、中断调用函数以及其余相关工做。下面是probe被调用的一些程序流程。函数

1:从注册函数platform_driver_register()函数开始spa

int platform_driver_register(struct platform_driver *drv)
{
    drv->driver.bus = &platform_bus_type;
    if (drv->probe)
        drv->driver.probe = platform_drv_probe;
    if (drv->remove)
        drv->driver.remove = platform_drv_remove;
    if (drv->shutdown)
        drv->driver.shutdown = platform_drv_shutdown;

    return driver_register(&drv->driver);
}

这个函数首先是对驱动进行填充,而后调用driver_register()函数,这个函数是向内核注册驱动的函数,不一样的总线最终都是调用这个函数向内核进行驱动的注册。指针

driver_register(&drv->driver);orm

    bus_add_driver(drv);资源

         driver_attach(drv);rem

            bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);get

                __driver_attachit

__driver_attach函数以下form

static int __driver_attach(struct device *dev, void *data)
{
    struct device_driver *drv = data;

    /*
     * 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.
     */

    if (!driver_match_device(drv, dev))
        return 0;

    if (dev->parent)    /* Needed for USB */
        device_lock(dev->parent);
    device_lock(dev);
    if (!dev->driver)
        driver_probe_device(drv, dev);
    device_unlock(dev);
    if (dev->parent)
        device_unlock(dev->parent);

    return 0;
}

分析可知,首先是调用driver_mach_device函数进行设备和驱动的匹配(这里应该根据具体的总线来调用相应的mach函数),若是匹配失败则直接return 0,若是匹配成功,则进行下一步,probe函数的调用,probe函数的调用经过driver_probe_device()函数来引出。调用层次以下

driver_probe_device(drv, dev);   

    really_probe(dev, drv);

really_probe()函数的部分代码以下

if (dev->bus->probe) {
        ret = dev->bus->probe(dev);
        if (ret)
            goto probe_failed;
    } else if (drv->probe) {
        ret = drv->probe(dev);
        if (ret)
            goto probe_failed;
    }

分析可知,在驱动和设备匹配成功后,首先会判断总线的的probe指针是否为空,若是不为空,则执行总线的prboe函数,若是总线的prboe函数为空,则进一步判断驱动的probe函数是否为空,若是不为空,则执行驱动的probe函数

相关文章
相关标签/搜索