【问题标题】:when Linux calls PCI driver's probe function?Linux什么时候调用PCI驱动的probe函数?
【发布时间】:2023-03-21 20:53:01
【问题描述】:

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

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

更新 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;
}

driver_attach 将使用参数__driver_attach 调用bus_for_each_dev,这将调用driver_probe_device

driver_probe_device 最终调用really_probe

if (dev->bus->probe) {
    ret = dev->bus->probe(dev);

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

【问题讨论】:

标签: linux linux-kernel linux-device-driver pci


【解决方案1】:

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

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

典型的实现如下所示:

#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,
};

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 内核在启动时检测到PCI总线上的设备,但驱动程序尚未注册;稍后我们insmod驱动程序,内核将如何拾取驱动程序的探测功能进行?有专门的线程吗?
    • 在驱动程序的 init() 中,您将把驱动程序注册到特定总线。您的驱动程序有必要连接到特定的总线。该总线驱动程序然后调用新添加的驱动程序的探针。
    猜你喜欢
    • 1970-01-01
    • 2011-11-26
    • 2021-10-26
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-04
    相关资源
    最近更新 更多