<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sat, Jan 30, 2016 at 2:41 PM, Bjørn Mork <span dir="ltr"><<a href="mailto:bjorn@mork.no" target="_blank">bjorn@mork.no</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class=""><div class="h5">Henrique Montenegro <<a href="mailto:typoon@gmail.com">typoon@gmail.com</a>> writes:<br>
<br>
> Hello list,<br>
><br>
> I am reading through the e1000 driver and trying to figure out how the<br>
> probe function on it gets called.<br>
><br>
> The driver initialization function calls pci_register_driver:<br>
><br>
> ----------------------------------------------------------------------------->8<br>
> static struct pci_driver e1000_driver = {<br>
> .name = e1000_driver_name,<br>
> .id_table = e1000_pci_tbl,<br>
> .probe = e1000_probe,<br>
> .remove = e1000_remove,<br>
> // ...<br>
> };<br>
><br>
> static int __init e1000_init_module(void)<br>
> {<br>
> // ...<br>
> ret = pci_register_driver(&e1000_driver);<br>
> // ...<br>
> }<br>
> ----------------------------------------------------------------------------->8<br>
><br>
> And pci_register_driver is defined as (on linux/pci.h):<br>
><br>
> ----------------------------------------------------------------------------->8<br>
> #define pci_register_driver(driver) \<br>
> __pci_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)<br>
> ----------------------------------------------------------------------------->8<br>
><br>
> Function __pci_register_driver is defined as (drivers/pci/pci-driver.c):<br>
><br>
> ----------------------------------------------------------------------------->8<br>
> int __pci_register_driver(struct pci_driver *drv, struct module *owner,<br>
> const char *mod_name)<br>
> {<br>
> /* initialize common driver fields */<br>
> drv-><a href="http://driver.name" rel="noreferrer" target="_blank">driver.name</a> = drv->name;<br>
> drv->driver.bus = &pci_bus_type;<br>
> drv->driver.owner = owner;<br>
> drv->driver.mod_name = mod_name;<br>
><br>
> spin_lock_init(&drv->dynids.lock);<br>
> INIT_LIST_HEAD(&drv->dynids.list);<br>
><br>
> /* register with core */<br>
> return driver_register(&drv->driver);<br>
> }<br>
> EXPORT_SYMBOL(__pci_register_driver);<br>
> ----------------------------------------------------------------------------->8<br>
><br>
> This is the point where I am getting lost. I can't figure out how the Kernel<br>
> will know about the functions defined in the e1000_driver struct mentioned<br>
> before, since it does not seem to pass a reference to it anywhere.<br>
><br>
> How does the kernel know where the probe function for this module is in this<br>
> case? To be honest, for any driver that calls pci_register_driver, how will<br>
> the<br>
> kernel know where the probe function is since it does not seem like it is<br>
> being passed to driver_register?<br>
<br>
</div></div>The magic is in the 'drv->driver.bus = &pci_bus_type;' assigment. This<br>
is where the driver core will look for functions knowing how to handle<br>
this specific driver. See Documentation/driver-model/bus.txt etc<br>
<br>
Look at the defintion of pci_bus_type in drivers/pci/pci-driver.c :<br>
<br>
struct bus_type pci_bus_type = {<br>
.name = "pci",<br>
.match = pci_bus_match,<br>
.uevent = pci_uevent,<br>
.probe = pci_device_probe,<br>
.remove = pci_device_remove,<br>
.shutdown = pci_device_shutdown,<br>
.dev_groups = pci_dev_groups,<br>
.bus_groups = pci_bus_groups,<br>
.drv_groups = pci_drv_groups,<br>
.pm = PCI_PM_OPS_PTR,<br>
};<br>
EXPORT_SYMBOL(pci_bus_type);<br>
<br>
And then look at the different callbacks. These explain how the generic<br>
&drv->driver above is turned back into a pci_driver on probing:<br>
<br>
static int pci_device_probe(struct device *dev)<br>
{<br>
int error;<br>
struct pci_dev *pci_dev = to_pci_dev(dev);<br>
struct pci_driver *drv = to_pci_driver(dev->driver);<br>
<br>
<br>
to_pci_dev() and to_pci_driver() are just macros simplifying the usual<br>
container_of trick. From include/linux/pci.h :<br>
<br>
#define to_pci_dev(n) container_of(n, struct pci_dev, dev)<br>
..<br>
#define to_pci_driver(drv) container_of(drv, struct pci_driver, driver)<br>
<br>
<br>
<br>
Hope this helps.<br>
<br>
<br>
Bjørn<br>
<br>
_______________________________________________<br>
Kernelnewbies mailing list<br>
<a href="mailto:Kernelnewbies@kernelnewbies.org">Kernelnewbies@kernelnewbies.org</a><br>
<a href="http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies" rel="noreferrer" target="_blank">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a></blockquote><div><br><div class=""><div class="h5"><div class="gmail_extra"><br></div></div></div><div><br></div>FWIW, when
unsure as to how driver functions such as probe() are called, I've
often found dump_stack() [1][2][3] to be extremely helpful. (Of course,
this assumes you're able to execute the driver...)<br><br>[1] <a href="http://kernelnewbies.org/KernelHacking-HOWTO/Debugging_Kernel">http://kernelnewbies.org/KernelHacking-HOWTO/Debugging_Kernel</a><br>[2] <a href="http://www.stlinux.com/devel/debug/backtrace">http://www.stlinux.com/devel/debug/backtrace</a><br>[3] <a href="http://processors.wiki.ti.com/index.php/Enabling_Stack_Dumping_in_Linux_Kernel">http://processors.wiki.ti.com/index.php/Enabling_Stack_Dumping_in_Linux_Kernel</a> <br></div></div><br></div></div>