参考:https://my.oschina.net/felixliang/blog/80741
platform_driver_register( )负责注册平台驱动程序,如果在内核中找到了使用驱动程序的设备,调用probe( )。刨去参数检查、错误处理,platform_driver_register的主要过程如下:
...
{
...
return platform_driver_register(&bcm2708_i2c_driver);
...
}
int platform_driver_register(struct platform_driver *drv)
{
...
return driver_register(&drv->driver);
}
int driver_register(struct device_driver *drv)
{
...
ret = bus_add_driver(drv); //platform是一个虚拟总线
...
}
int bus_add_driver(struct device_driver *drv)
{
...
if (drv->bus->p->drivers_autoprobe) {
error = driver_attach(drv);
if (error)
goto out_unregister;
}
...
kobject_uevent(&priv->kobj, KOBJ_ADD); //发送uevent消息
return 0;
...
}
/**内核驱动通过kobject_uevent()/kobject_uevent_env()发送uevent到用户空间,主要包括两部分工作:
一是通过netlink_broadcast_filtered()发送netlink消息;另一是通过
call_usermodehelper_setup()/call_usermodehelper_exec()调用用户空间程序处理uevent消息。
**/
int driver_attach(struct device_driver *drv) //driver_attach() 驱动自动匹配设备
{
return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach); //监测到bus设备,调用
__driver_attach( )
}
//调用该函数,那么drv驱动程序会和drv所在总线上连接了的物理设备进行一一匹配
static int __driver_attach(struct device *dev, void *data) //dev 为使用驱动程式的设备结构体
{
...
if (!dev->driver)
driver_probe_device(drv, dev);
...
return 0;
}
int driver_probe_device(struct device_driver *drv, struct device *dev)
{
...
ret = really_probe(dev, drv);
...
}
static int really_probe(struct device *dev, struct device_driver *drv)
{
...
dev->driver = drv;
if (driver_sysfs_add(dev)) { //在sysfs中添加设备的Kobject目录和熟悉文件
...
}
if (dev->bus->probe) {
...
} else if (drv->probe) {
ret = drv->probe(dev); //调用driver的probe( ),dev为设备结构体
...
}
...
}
补充:
1、uevent()函数参考:https://www.cnblogs.com/sky-heaven/p/14042128.html
2、driver_attach() 驱动自动匹配设备