Andrei Shpakouski
Andrei Shpakouski

Reputation: 148

How to get Vendor ID and Device ID of all PCI Devices?

I need to get Vendor ID and Device ID of all PCI bus Devices from Linux using C/C++ (inline asm allowed), but I can't even understand from what to start.

Please, give me some pieces of advice or code parts.

Upvotes: 4

Views: 31012

Answers (1)

KamilCuk
KamilCuk

Reputation: 141698

How to get Vendor ID and Device ID of all PCI Devices?

In short, you have to write a C program that does:

grep PCI_ID /sys/bus/pci/devices/*/uevent

Sample output lines:

/sys/bus/pci/devices/0000:00:00.0/uevent:PCI_ID=1022:14E8
/sys/bus/pci/devices/0000:00:00.2/uevent:PCI_ID=1022:14E9
/sys/bus/pci/devices/0000:00:01.0/uevent:PCI_ID=1022:14EA
/sys/bus/pci/devices/0000:00:02.0/uevent:PCI_ID=1022:14EA

And extract relevant data after = and after :.

So what you have to do is:

  • iterate over directories in /sys/bus/pci/devices with readdir_r
  • for each directory
    • open the uevent file from inside that directory
    • read lines from the file until PCI_ID is found
    • if found - basically match the line with sscanf(line, "PCI_ID=%4x:%4x\n", &vendor_id, &device_id)

I couldn't find any documentation about uevent inside /sys/bus/pci/devices. This answer is based on reverse engineering busybox lspci.c sources.

Upvotes: 6

Related Questions