linux - Linux 何时调用 PCI 驱动程序的探测函数?

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

在注册 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);

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

最佳答案

在您的 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 核心枚举/链接训练识别您的驱动程序支持的设备后,您的探测功能将立即被调用。

关于linux - Linux 何时调用 PCI 驱动程序的探测函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31330039/

相关文章:

linux - 使内核头文件对用户空间可用

linux - 用户空间打印、控制台和 tty 之间的关系

mysql - 如何在 CentOS 中加密现有的 mariadb 表?

linux - lua redis 处理错误

php - 从/var/www 文件夹运行 php 程序

c - 如何在内核中记录大量数据

JAVA可以在linux平台调用.so文件吗?

linux - linux进程描述符存储在哪里,什么可以访问它?

c - 没有目录如/usr/src/linux-2.x/include/asm-i386/unistd.h

c - 了解代码 Linux 设备驱动程序 m25p80.c