日期:2014-05-16  浏览次数:20701 次

关于linux platform驱动中设备和驱动谁需先注册
我想知道,device_register和driver_register该谁先来执行。请教
我看的是2.6.10的源码
在driver_registe中会执行到driver_attach函数
[code=C/C++][/code]void driver_attach(struct device_driver * drv)
{
struct bus_type * bus = drv->bus;
struct list_head * entry;
int error;

if (!bus->match)
return;

list_for_each(entry, &bus->devices.list) {
struct device * dev = container_of(entry, struct device, bus_list);
if (!dev->driver) { //如果遍历到的设备没有指定驱动则执行
error = driver_probe_device(drv, dev); //探测设备和驱动是否可以连接
if (error && (error != -ENODEV))
/* driver matched but the probe failed */
printk(KERN_WARNING
"%s: probe of %s failed with error %d\n",
drv->name, dev->bus_id, error);
}
}
}
在这里,我觉得如果没有注册对应的设备时,应该dev->driver肯定为真(因为别的设备已经都完成对应的驱动),所以从这里看我觉得有必要一定得先设备注册来着。不然连error = driver_probe_device(drv, dev);这个最基本的函数都不能执行了啊

------解决方案--------------------
注意看device_attach函数:
284 /**
285 * device_attach - try to attach device to a driver.
286 * @dev: device.
287 *
288 * Walk the list of drivers that the bus has and call bus_match()
289 * for each pair. If a compatible pair is found, break out and return.
290 */