Linux c Get PCI device name and vendor name __linux

Source: Internet
Author: User

One, interface

Char *pci_lookup_name (struct pci_access *a, char *buf, intsize, int flags, ...) Pci_abi;

(/usr/include/pci/pci.h)

Second, describe


The VendorID of PCI devices (obtained from vendor files) can be read below the/sys/bus/pci/devices/<pci address> directory, DeviceId (obtained from device files), But the text description of the vendor and the text description of the device are not.

1 at the beginning of the time using LSPCI to obtain vendor and device text description, but low efficiency;


2) now use the Pci_lookup_name () interface to obtain the text description of vendor and device;

Pacc->id_lookup_mode = 0;

The default installed PCI development library must be initialized to 0 before calling Pci_lookup_name (). Otherwise, Pci_lookup_name () segment fault if the/usr/share/hwdata/pci.ids file does not exist, or if the description of the vendor ID (device ID) is not found in the Pci.ids file.

struct pci_access *pacc;
PACC = Pci_alloc ();
Pacc->id_lookup_mode = 0;
if (!pci_lookup_name (PACC, name, 256,
         Pci_lookup_vendor,
         vendor,device))
{
    fprintf (stderr, "fail to Get PCI vendor\n ");
    return-1;
}
printf ("Vendor:%s\n", name);

if (!pci_lookup_name (PACC, name, 256,
        Pci_lookup_device,
        vendor,device))
{
    fprintf, stderr, " Fail to get PCI device\n ");
    return-1;
}
printf ("Device:%s\n", name);

Pci_cleanup (PACC);

Third, sample output


Four, sample code

http://download.csdn.net/detail/qq123386926/9426785

Five, note

1) When compiling, add-lpci;

2) When the g++ is compiled, the header file is introduced

#ifdef __cplusplus

extern "C"  {

#endif

#include <pci/pci.h>

#ifdef __cplusplus

}

#endif

Otherwise, the undefine symbol error will be reported when the interface inside the pci.h is invoked at runtime, even if the compilation is passed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.