insmod最主要就是执行*.ko里面__init修饰的函数,
一般在这个函数里面去注册driver,
注册dirver的函数会通过name匹配是否有device存在,
如果存在则执行probe函数。

具体意思是,以字符驱动为例,在.c文件中,会填充platform_driver结构体,里面指定name,

然后在linux\arch\mips\xxx-platform.c中,填充platform_device结构体,指定name。

可以阅读《linux 内核驱动–Platform Device和Platform_driver注册过程》
http://www.cnblogs.com/haimeng2010/p/3582403.html
http://www.cnblogs.com/leaven/archive/2010/01/06/1640177.html
http://blog.sina.com.cn/s/blog_720b34850101r2qc.html

platform_device和platform_driver的注册过程,及probe函数何时调用的分析

通过系统初始化里边调用platform_add_devices,把所有放置在板级platform_device数组中的所有platform_device逐次调用platform_device_register添加到系统中去,

platform_device_register中会调用platform_device_add(注意:这个同platform_add_devices有本质区别的),全部add到系统之后,便可以通过platform的操作接口来获取platform_device中的资源。

驱动模块insmod到系统时,驱动代码里边就可以通过上面函数来获取对应platform_device的resource,比如在module_init中我们会调用plarform_driver_register,这个会引用到platform_driver中的probe函数,而probe函数中则可以进行cdev的初始化及cdev_add的操作,在进行这些操作之前,可以通过get_resource来获取寄存器物理基地址,然后ioremap到kernel的虚拟空间来,这样驱动就可以正式操纵改设备的寄存器了。

驱动注册的时候 platform_driver_register()->driver_register()->bus_add_driver()->driver_attach()->bus_for_each_dev()– 对每个挂在虚拟的platform bus的设备作- __driver_attach()->driver_probe_device()->drv->bus->match()==platform_match()-比较strncmp(pdev->name, drv->name, BUS_ID_SIZE),如果相符就调用platform_drv_probe()->driver->probe(),如果probe成功则绑定该设备到该驱动。

以下代码源于linux3.1.9。

platform_driver_register( ) 是内核中非常著名的函数 。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;
。。。
}

int driver_attach(struct device_driver *drv)
{
return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach); //监测到bus设备,调用__driver_attach( )
}

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为设备结构体
。。。

}
。。。
}