Http://linux.chinaunix.net/bbs/viewthread.php? Tid = 899458
Recently, we have seen the device driver model of Linux. It is not clear about kobject, kset, and so on. When I saw the structure of struct device_driver, I thought of a question: Where can I call its initialization function? Previously engaged in PCI driver with PCI driver registration function can call it, engage in S3C2410 driver as long as the struct platform_device * smdk2410_devices in the mach-smdk2410.c added to the device will also be called. But I have never thought about the specific process of driver registration and probe calling.
So I opened sourceinsight and tracked it:
From driver_register:
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 );
}
Klist_init and init_completion do not care about it. It may be the work of this device model of 2.6. Intuition tells me to go to bus_add_driver.
Bus_add_driver:
All are kobject, klist, ATTR, etc. It is also related to the device model. However, there is one sentence:
Driver_attach (DRV );
The name of a single listener is similar:
Void driver_attach (struct device_driver * DRV)
{
Bus_for_each_dev (DRV-> bus, null, DRV, _ driver_attach );
}
This is familiar with traversing the devices on the bus and using _ driver_attach.
In _ driver_attach, it is mainly like this:
Driver_probe_device (DRV, Dev );
Go to driver_probe_device and check it out:
There is a very important section:
If (DRV-> bus-> match &&! DRV-> bus-> match (Dev, DRV ))
Goto done;
Obviously, it is the match function on the bus that calls the driver. If 1 is returned, continue; otherwise, done is returned.
If the inheritance is executed:
If (DRV-> probe ){
Ret = DRV-> probe (Dev );
If (RET ){
Dev-> driver = NULL;
Goto probefailed;
}
If probe exists, it is called. So far, the probe call has been completed.
The key to this process chain is DRV-> bus-> match, because the registration fails if an error occurs in other places. As long as the registration fails and match returns 1, then it will definitely call the probe of the drive. You can register a bus type and bus, and always return 1 in match. You will find that the probe function is always called as long as the bus type in struct device_driver is correct.
The PCI device has its own bus model, and it is estimated that there is a condition for judgment in its match.
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;
}
Next, we will find that the data is mainly based on the id_table that we are familiar.