Linux driver, when the probe function is called

  

Recently saw the Linux device driver model, about Kobject, Kset, etc. is not very clear. When I saw the structure of struct device_driver, I thought of a question: Where is its initialization function called? In the past, when playing PCI driver, you can call it with pci driver registration function. When you install s3c2410 driver, you only need to add device in struct platform_device *smdk2410_devices {} in mach-smdk2410.c. But never thought about the specific driver registration and calling the process of the probe.

So turn on SourceInsight and trace 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 have not taken care of it, it may be some work done by this device model of 2.6. Intuition tells me to go to bus_add_driver.

bus_add_driver: Both Kobject and klist, attr, etc. Still related to the device model. But there is one sentence: driver_attach(drv); listening to a name is very similar to: void driver_attach(struct device_driver * drv){ bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);} This familiar, traversing the device on the bus And set __driver_attach. In __driver_attach, this is mainly the case: driver_probe_device(drv, dev); go to driver_probe_device to see: there is a very important: if (drv->bus->match && !drv->bus ->match(dev, drv))goto Done; Obviously, it is the match function on the bus of the calling driver. If it returns 1, it can continue, otherwise it will be Done. Inheritance execution: if (drv->probe) {ret = drv->probe(dev);if (ret) {dev->driver = NULL;goto ProbeFailed;} Called whenever the probe exists. This completes the call to the probe.

The key to this process chain is still drv->bus->match, because the rest of the error is the registration failure, and as long as the registration does not fail and the match returns 1, then the call will be called The probe is gone. You can register a bus type and bus, and always return 1 in the match. You will find that the probe function is always called as long as the bus type in the struct device_driver is correct. The PCI device has its own bus model, estimated in it. There is a condition for judgment in 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;} and then down to know that it is mainly based on the id_table we are familiar with. ------------------------------- Another solution ----------------- -------------------------------------------------- ----------------------------

From the driver_register, my here is here:

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\ ", drv->name); }klist_init(&drv->klist_devices, NULL, NULL);return bus_add_driver(drv);} klist_init is irrelevant, leave him alone, see bus_add_driver:

int bus_add_driver(struct device_driver *drv ) {//1. first kobject_set_name(&drv->kobj, "%s", drv->name); //2. then kobject_register(&drv->kobj)//3. Then Called: driver_attach(drv)}int driver_attach(struct device_driver * drv){return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);} What really works is __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. First determine whether the bus is match: if (drv->bus->match && !drv->bus->match(dev, drv)) goto done;//2. Again The body executes probe:ret = really_probe(dev, drv);...}really_probe is the function we are looking for:

static int really_probe(struct device *dev, struct device_driver *drv){... //1. First the probe function of the bus to which the driver belongs: if (dev->bus->probe) { ret = dev->bus->probe(dev); if (ret) goto probe_failed;} Else if (drv->probe) {//2. Call the probe function in your driver: ret = drv->probe(dev); if (ret) goto probe_failed;}...} where drv ->probe(dev) is the concrete probe function that actually calls your driver implementation. That is, corresponding to the title of this article, the probe function is called at this time.



Copyright © Windows knowledge All Rights Reserved