Linux总线设备驱动

时间:2021-11-18 17:57:21
static const struct sysfs_ops dev_sysfs_ops= {
    .show    = dev_attr_show,
    .store    = dev_attr_store,

};
static struct kobj_type device_ktype = {
    .release    = device_release,

    .sysfs_ops    = &
dev_sysfs_ops,
    .namespace    = device_namespace,

};
struct bus_type platform_bus_type = {
    .name        = "platform",
    .dev_attrs    = platform_dev_attrs,
    .match        =
platform_match,
    .uevent        = platform_uevent,
    .pm        = &platform_dev_pm_ops,

};
struct device_attribute {
    struct attribute attr;
    ssize_t (*show)(structdevice* dev, struct device_attribute* attr,char* buf);
    ssize_t (*store)(structdevice* dev, struct device_attribute* attr, const char* buf, size_t count);
};
static struct device_attributeplatform_dev_attrs[] = {
    __ATTR_RO(modalias),
    __ATTR_NULL,
};


platform_device_register
    -->device_initialize(&pdev->dev);

            -->...
            -->dev->kobj.kset = devices_kset;

            -->kobject_init(&dev->kobj, &device_ktype);
            -->...

    -->return platform_device_add(pdev);

            -->...

            -->pdev->dev.bus = &platform_bus_type;
//match()被注册
            -->
ret = device_add(&pdev->dev);
                   -->...
 
                  -->error = device_create_file(dev, &uevent_attr);

                   -->...

 
                  -->error = device_add_attrs(dev);

                   -->...

                   -->error = bus_add_device(dev);
                   -->...

                   -->kobject_uevent(&dev->kobj, KOBJ_ADD);

                   -->bus_probe_device(dev);//见device_registerbus_probe_device
 


 

device
_register
    ->bus_probe_device
        -->device_attach
            -->bus_for_each_drv(dev->bus, NULL,dev,__device_attach);
                -->__device_attach
                    -->driver_match_device
                        -->drv->bus->match//调用platform_bus_type的match()
                    -->driver_probe_device
                        -->really_probe
                            -->drv->probe(dev)//调用probe()




platform_driver_register
    -->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
    -->bus_add_driver
        -->driver_attach
            --> bus_for_each_dev(drv->bus, NULL,drv,__driver_attach );
                --> __driver_attach
                    -->driver_match_device
                        -->drv->bus->match//调用platform_bus_type的match()
                    -->driver_probe_device
                        -->really_probe
                            -->drv->probe(dev)//调用probe()



由此可见:
      
不管是先注册device还是先注册driver,最后都是调用drv->probe(dev)