我在哪里可以找到在 Debian Stretch 上构建网络 driver 的 pci_find_device() 声明?

Where can I find the pci_find_device() declaration building a network driver on Debian Stretch?

我正在尝试建立网络 driver。但是,当我尝试构建它时说:

error: implicit declaration of function ‘pci_find_device

这是我的 driver:

#define REALTEK_VENDER_ID  0x10EC
#define REALTEK_DEVICE_ID   0x8139

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/stddef.h>
#include <linux/pci.h>

int init_module(void)
{
    struct pci_dev *pdev;
    pdev = pci_find_device(REALTEK_VENDER_ID, REALTEK_DEVICE_ID, NULL);
    if(!pdev)
        printk("<1>Device not found\n");
    else
        printk("<1>Device found\n");
    return 0;
}
MODULE_LICENSE("GPL");

这是我的 Makefile

obj-m += ethDriver.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

构建时出现以下错误:

ethDriver.c: In function ‘init_module’:
/home/xxx/ethDriver/ethDriver.c:13:12: error: implicit declaration of function ‘pci_find_device’ [-Werror=implicit-function-declaration]
     pdev = pci_find_device(REALTEK_VENDER_ID, REALTEK_DEVICE_ID, NULL);
            ^~~~~~~~~~~~~~~
/home/xxx/ethDriver/ethDriver.c:13:10: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
     pdev = pci_find_device(REALTEK_VENDER_ID, REALTEK_DEVICE_ID, NULL);
          ^
cc1: some warnings being treated as errors
/usr/src/linux-headers-4.9.0-3-common/scripts/Makefile.build:315: recipe for target '/home/xxx/ethDriver/ethDriver.o' failed
make[4]: *** [/home/xxx/ethDriver/ethDriver.o] Error 1
/usr/src/linux-headers-4.9.0-3-common/Makefile:1507: recipe for target '_module_/home/xxx/ethDriver' failed
make[3]: *** [_module_/home/xxx/ethDriver] Error 2
Makefile:150: recipe for target 'sub-make' failed
make[2]: *** [sub-make] Error 2
Makefile:8: recipe for target 'all' failed
make[1]: *** [all] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.9.0-3-amd64'
Makefile:4: recipe for target 'all' failed
make: *** [all] Error 2

内核 v2.6.34 中删除了 pci_find_device() 函数。

来自 Documentation/PCI/pci.rst:

Obsolete functions

There are several functions which you might come across when trying to port an old driver to the new PCI interface. They are no longer present in the kernel as they aren't compatible with hotplug or PCI domains or having sane locking.

pci_find_device() Superseded by pci_get_device()
pci_find_subsys() Superseded by pci_get_subsys()
pci_find_slot()   Superseded by pci_get_domain_bus_and_slot()
pci_get_slot()    Superseded by pci_get_domain_bus_and_slot()

您需要使用 pci_get_device(),它也应该包含在 <linux/pci.h> 中。

/**
 * pci_get_device - begin or continue searching for a PCI device by vendor/device id
 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
 * @from: Previous PCI device found in search, or %NULL for new search.
 *
 * Iterates through the list of known PCI devices.  If a PCI device is
 * found with a matching @vendor and @device, the reference count to the
 * device is incremented and a pointer to its device structure is returned.
 * Otherwise, %NULL is returned.  A new search is initiated by passing %NULL
 * as the @from argument.  Otherwise if @from is not %NULL, searches continue
 * from next device on the global list.  The reference count for @from is
 * always decremented if it is not %NULL.
 */
struct pci_dev *pci_get_device(unsigned int vendor, unsigned int device,
                   struct pci_dev *from)
{
    return pci_get_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
}
EXPORT_SYMBOL(pci_get_device);

PS:你的#define打错了,应该是VENDOR,而不是VENDER.