<div dir="ltr">Thanks <span style="font-size:12.8px;white-space:nowrap">Bjørn! I will investigate this better and see if I can understand it better. I really appreciate the pointers!</span><div><span style="font-size:12.8px;white-space:nowrap"><br></span></div><div><span style="font-size:12.8px;white-space:nowrap">Henrique</span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jan 30, 2016 at 2:41 PM, Bjørn Mork <span dir="ltr">&lt;<a href="mailto:bjorn@mork.no" target="_blank">bjorn@mork.no</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">Henrique Montenegro &lt;<a href="mailto:typoon@gmail.com">typoon@gmail.com</a>&gt; writes:<br>
<br>
&gt; Hello list,<br>
&gt;<br>
&gt; I am reading through the e1000 driver and trying to figure out how the<br>
&gt; probe function on it gets called.<br>
&gt;<br>
&gt; The driver initialization function calls pci_register_driver:<br>
&gt;<br>
&gt; -----------------------------------------------------------------------------&gt;8<br>
&gt; static struct pci_driver e1000_driver = {<br>
&gt; .name     = e1000_driver_name,<br>
&gt; .id_table = e1000_pci_tbl,<br>
&gt; .probe    = e1000_probe,<br>
&gt; .remove   = e1000_remove,<br>
&gt;     // ...<br>
&gt; };<br>
&gt;<br>
&gt; static int __init e1000_init_module(void)<br>
&gt; {<br>
&gt;     // ...<br>
&gt; ret = pci_register_driver(&amp;e1000_driver);<br>
&gt;     // ...<br>
&gt; }<br>
&gt; -----------------------------------------------------------------------------&gt;8<br>
&gt;<br>
&gt; And pci_register_driver is defined as (on linux/pci.h):<br>
&gt;<br>
&gt; -----------------------------------------------------------------------------&gt;8<br>
&gt; #define pci_register_driver(driver)             \<br>
&gt;          __pci_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)<br>
&gt; -----------------------------------------------------------------------------&gt;8<br>
&gt;<br>
&gt; Function __pci_register_driver is defined as (drivers/pci/pci-driver.c):<br>
&gt;<br>
&gt; -----------------------------------------------------------------------------&gt;8<br>
&gt; int __pci_register_driver(struct pci_driver *drv, struct module *owner,<br>
&gt;                           const char *mod_name)<br>
&gt; {<br>
&gt;         /* initialize common driver fields */<br>
&gt;         drv-&gt;<a href="http://driver.name" rel="noreferrer" target="_blank">driver.name</a> = drv-&gt;name;<br>
&gt;         drv-&gt;driver.bus = &amp;pci_bus_type;<br>
&gt;         drv-&gt;driver.owner = owner;<br>
&gt;         drv-&gt;driver.mod_name = mod_name;<br>
&gt;<br>
&gt;         spin_lock_init(&amp;drv-&gt;dynids.lock);<br>
&gt;         INIT_LIST_HEAD(&amp;drv-&gt;dynids.list);<br>
&gt;<br>
&gt;         /* register with core */<br>
&gt;         return driver_register(&amp;drv-&gt;driver);<br>
&gt; }<br>
&gt; EXPORT_SYMBOL(__pci_register_driver);<br>
&gt; -----------------------------------------------------------------------------&gt;8<br>
&gt;<br>
&gt; This is the point where I am getting lost. I can&#39;t figure out how the Kernel<br>
&gt; will know about the functions defined in the e1000_driver struct mentioned<br>
&gt; before, since it does not seem to pass a reference to it anywhere.<br>
&gt;<br>
&gt; How does the kernel know where the probe function for this module is in this<br>
&gt; case? To be honest, for any driver that calls pci_register_driver, how will<br>
&gt; the<br>
&gt; kernel know where the probe function is since it does not seem like it is<br>
&gt; being passed to driver_register?<br>
<br>
</div></div>The magic is in the &#39;drv-&gt;driver.bus = &amp;pci_bus_type;&#39; 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           = &quot;pci&quot;,<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>
&amp;drv-&gt;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-&gt;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>
<span class="HOEnZb"><font color="#888888"><br>
<br>
Bjørn<br>
</font></span></blockquote></div><br></div>