Linux什么时候调用PCI驱动的probe函数?

Mar*_*ark 6 linux linux-device-driver linux-kernel pci

在注册 PCI 驱动程序之前,我们必须对其进行初始化struct pci_driver并将其传递给 pci_register_driver. 该结构的字段之一是指向驱动程序probe函数的指针。

我的问题是 - 当内核调用驱动程序的探测例程时。是否保证在调用后立即pci_register_driver发生或可能发生在任何其他时间?是什么决定了这种行为?

UPDATE pci_register_driver是一个扩展为 的宏__pci_register_driver,它依次调用driver_registerdriver_register调用bus_add_driver.

中有以下代码bus_add_driver

if (drv->bus->p->drivers_autoprobe) {
        error = driver_attach(drv);
        if (error)
            goto out_unregister;
}
Run Code Online (Sandbox Code Playgroud)

driver_attachbus_for_each_dev使用参数__driver_attach调用,它将调用driver_probe_device

driver_probe_device最终调用really_probe

if (dev->bus->probe) {
    ret = dev->bus->probe(dev);
Run Code Online (Sandbox Code Playgroud)

我不确定的一件事是,是否drivers_autoprobe为 pci_bus 设置了标志。

whh*_*000 6

在您的 Linux 内核中的 PCI 内核在链接训练阶段枚举您的设备后(默认情况下在启动时发生),它将收集有关连接到它的端点设备的信息,这包括供应商 ID 和设备 ID。然后,PCI 核心将遍历所有已使用函数“pci_register_driver”注册到它的驱动程序,并查看驱动程序是否支持此供应商/设备组合。

驱动程序使用结构体的struct pci_device_id id_table字段标识它支持该供应商/设备组合pci_driver

一个典型的实现看起来像这样:

#define VENDOR 0xBEEF // vendor of EP device
#define DEVICE 0x1111 // device id of EP

static struct pci_device_id module_dev_table[] = {
    { PCI_DEVICE(VENDOR, DEVICE)},
    {0, },
};

// PCI driver structure used to register this driver with the kernel
static struct pci_driver fpga_driver = {
    .id_table   = module_dev_table,
    .probe      = module_probe,
    .remove     = module_remove,
    .suspend    = module_suspend,
    .resume     = module_resume,
};
Run Code Online (Sandbox Code Playgroud)

当 PCI 内核将您的驱动程序识别为支持总线上设备的驱动程序时,您的探测函数将被调用。

因此,回答您的问题,不,不能保证在您注册驱动程序后立即调用您的探测函数,而且几乎可以肯定不会。在 PCI 核心枚举/链接训练识别您的驱动程序支持的设备后,将立即调用您的探测函数。


Anu*_*ari 1

当内核检测到 PCI 总线上的 PCI 设备时,内核根据设备树获取设备名称。此内核扫描注册驱动程序列表后是否有任何驱动程序处理该设备。如果是,那么内核将调用该特定驱动程序的探针。

  • 在驱动程序的 init() 中,您将把您的驱动程序注册到特定的总线。您的司机有必要搭乘特定的巴士。该总线驱动程序然后调用新添加的驱动程序的探针。 (2认同)