struct pci_device_id

The struct pci_device_id is a fundamental structure in Linux kernel programming, particularly for PCI device drivers. Its primary purpose is to hold PCI device identifiers, which are used to match the driver with specific PCI devices. This structure is essential for the kernel to determine which devices a particular driver can handle.

Key Points

  • Purpose:

    The struct pci_device_id is used to define a table of PCI device IDs that the driver supports. This table is used by the kernel to match the driver with the appropriate PCI devices.

  • Fields:

    • vendor: The vendor ID of the PCI device.

    • device: The device ID of the PCI device.

    • subvendor, subdevice: Subsystem vendor and device IDs (optional).

    • class, class_mask: Class code and mask for class-based matching (optional).

    • driver_data: Private data for the driver (optional).

  • Usage:

    • The pci_device_id structure is typically used in conjunction with the MODULE_DEVICE_TABLE macro, which exports the device ID table to user space, allowing tools like modprobe to load the appropriate driver for a given device.

    • The id_table field in the pci_driver structure points to this table, enabling the kernel to match the driver with the correct devices.

  • Example:

    static struct pci_device_id pci_watcher_ids[] = {
        { PCI_DEVICE(VENDOR_ID, DEVICE_ID) },
        {0, }
    };
    
    • In this example, pci_watcher_ids is an array of pci_device_id structures. The PCI_DEVICE macro is used to define a specific vendor and device ID pair. The array is terminated with an all-zero entry, which is a common practice to mark the end of the table.

  • Macros:

    • PCI_DEVICE(vendor, device): A macro that simplifies the creation of a pci_device_id entry by specifying the vendor and device IDs.

    • MODULE_DEVICE_TABLE(pci, pci_watcher_ids): This macro exports the device ID table to user space, allowing the kernel to associate the driver with the specified PCI devices.

Summary

The struct pci_device_id is a crucial part of PCI device driver development. It allows the driver to declare which PCI devices it supports, enabling the kernel to match the driver with the appropriate hardware. The pci_watcher_ids array in the provided code is an example of how this structure is used to define a list of supported devices.