田海立@CSDN
2011/08/26
Linux 2.6的设备驱动模型中,所有的device都是通过Bus相连。device_register() / driver_register()执行时通过枚举BUS上的Driver/Device来实现绑定,本文详解这一过程。这是整个LINUX设备驱动的基础,PLATFORM设备,I2C上的设备等诸设备的注册最终也是调用本文讲述的注册函数来实现的。
Linux Device的注册最终都是通过device_register()实现,Driver的注册最终都是通过driver_register()实现。下图对照说明了Device和Driver的注册过程。
上面的图解一目了然,详细过程不再赘述。注意以下几点说明:
Device一般是先于Driver注册,但也不全是这样的顺序。Linux的Device和Driver的注册过程分别枚举挂在该BUS上所有的Driver和Device实现了这种时序无关性。
int driver_register( struct device_driver * drv) { klist_init( & drv -> klist_devices, klist_devices_get, klist_devices_put); init_completion( & drv -> unloaded); return bus_add_driver(drv); }
void driver_attach( struct device_driver * drv) { bus_for_each_dev(drv -> bus, NULL, drv, __driver_attach); }
static int pci_bus_match( struct device * dev, struct device_driver * drv) { struct pci_dev * pci_dev = to_pci_dev(dev); struct pci_driver * pci_drv = to_pci_driver(drv); const struct pci_device_id * found_id; found_id = pci_match_device(pci_drv, pci_dev); if (found_id) return 1 ; return 0 ; }
-------------------------------另解-----------------------------------------------------------------------------------------------
从driver_register看起,此处我的这里是:
int driver_register( struct device_driver * drv) { if ((drv -> bus -> probe && drv -> probe) || (drv -> bus -> remove && drv -> remove) || (drv -> bus -> shutdown && drv -> shutdown)) { printk(KERN_WARNING " Driver '%s' needs updating - please use bus_type methods\n " , drv -> name); } klist_init( & drv -> klist_devices, NULL, NULL); return bus_add_driver(drv); }
klist_init不相关,不用管他,具体再去看bus_add_driver:
int bus_add_driver( struct device_driver * drv) { // 1.先kobject_set_name(&drv->kobj, "%s", drv->name); // 2.再kobject_register(&drv->kobj) // 3.然后调用了:driver_attach(drv) }
int driver_attach( struct device_driver * drv) { return bus_for_each_dev(drv -> bus, NULL, drv, __driver_attach); }
真正起作用的是__driver_attach:
static int __driver_attach( struct device * dev, void * data) { ... if ( ! dev -> driver) driver_probe_device(drv, dev); ... } int driver_probe_device( struct device_driver * drv, struct device * dev) { ... // 1.先是判断bus是否match: if (drv -> bus -> match && ! drv -> bus -> match(dev, drv)) goto done; // 2.再具体执行probe: ret = really_probe(dev, drv); ... }
really_probe才是我们要找的函数:
static int really_probe( struct device * dev, struct device_driver * drv) { ... // 1.先是调用的驱动所属总线的probe函数: if (dev -> bus -> probe) { ret = dev -> bus -> probe(dev); if (ret) goto probe_failed; } else if (drv -> probe) { // 2.再调用你的驱动中的probe函数: ret = drv -> probe(dev); if (ret) goto probe_failed; } ... }
其中,drv->probe(dev),才是真正调用你的驱动实现的具体的probe函数。
也就是对应此文标题所问的,probe函数此时被调用。
声明:以下主要内容参考自:
关于struct device_driver结构中的probe探测函数的调用
http://blog.chinaunix.net/u2/71164/showart.php?id=1361188