How Linux C calls the PCI Lib function

Source: Internet
Author: User

how Linux C calls the PCI Lib function

Under Linux, you can access the configuration space of a PCI device through the "Setpci" and "SETPCI" commands, so can you use a program to access the PCI configuration space? The answer is certainly yes, there are multiple PCI libraries available under Linux for application access. The following is the most common example, from the installation, use and compilation of the angle to explain separately.


installation
In CentOS, with Superuser privileges, the following commands can be used to see which libraries are associated with PCI access, including:
Libpciaccess.i686:PCI Access Library
Libpciaccess.x86_64:PCI Access Library
LIBPCIACCESS-DEVEL.I686:PCI Access Library Development Package
LIBPCIACCESS-DEVEL.X86_64:PCI Access Library Development Package
Pciutils.x86_64:PCI Bus related utilities
Pciutils-devel.i686:linux PCI Development Library
Pciutils-devel.x86_64:linux PCI Development Library
Pciutils-devel-static.i686:linux PCI Static Library
Pciutils-devel-static.x86_64:linux PCI Static Library
Pciutils-libs.i686:linux PCI Library
Pciutils-libs.x86_64:linux PCI Library

Since the actual system is 64bit and is used in the form of a static library, you can install the PCI Lib library with the following command:
Yum Install libpciaccess.x86_64 libpciaccess-devel.x86_64 pciutils-devel-static.x86_64
After installation, you can see that the corresponding header file has been the most/usr/include below:
[[email protected] include]# Find/-name "*pci*"
./linux/pci.h
./linux/pci_regs.h
./linux/virtio_pci.h
./sys/pci.h
./pci
./pci/pci.h
./pciaccess.h
and the static library already exists:
[[email protected] lib64]# Find/-name "*pci*"
./kde4/kcm_pci.so
./pkgconfig/libpci.pc
./pkgconfig/pciaccess.pc
./libpci.so.3
./libpci.so.3.2.1
./libpciaccess.so.0
./libpciaccess.so.0.11.1
./libpci.so
./libpci.a
./libpciaccess.so
As you can see from the above output, libpciaccess only has the form of a dynamic library, and LIBPCI has both a dynamic library and a static library, which we enter as "Yum install libpciaccess.x86_64 libpciaccess-devel.x86_64 pciutils-devel-static.x86_64 "command is consistent.


Use

To open/usr/include/pci/pci.h, we can find the function we need:
U8 pci_read_byte (struct Pci_dev *, int pos) Pci_abi; /* Access to configuration space */
U16 Pci_read_word (struct Pci_dev *, int pos) Pci_abi;
U32 Pci_read_long (struct Pci_dev *, int pos) Pci_abi;
int Pci_read_block (struct pci_dev *, int pos, U8 *buf, int len) Pci_abi;
int PCI_READ_VPD (struct pci_dev *d, int pos, U8 *buf, int len) Pci_abi;
int pci_write_byte (struct pci_dev *, int pos, U8 data) Pci_abi;
int Pci_write_word (struct pci_dev *, int pos, u16 data) Pci_abi;
int Pci_write_long (struct pci_dev *, int pos, u32 data) Pci_abi;
int Pci_write_block (struct pci_dev *, int pos, U8 *buf, int len) Pci_abi;

Open/usr/include/pciaccess.h, there are also functions that can access the configuration space:
struct Pci_io_handle *pci_device_open_io (struct pci_device *dev, pciaddr_t base,
pciaddr_t size);
struct Pci_io_handle *pci_legacy_open_io (struct pci_device *dev, pciaddr_t base,
pciaddr_t size);
void Pci_device_close_io (struct pci_device *dev, struct pci_io_handle *handle);
uint32_t pci_io_read32 (struct pci_io_handle *handle, uint32_t reg);
uint16_t pci_io_read16 (struct pci_io_handle *handle, uint32_t reg);
uint8_t Pci_io_read8 (struct pci_io_handle *handle, uint32_t reg);
void Pci_io_write32 (struct pci_io_handle *handle, uint32_t Reg, uint32_t data);
void Pci_io_write16 (struct pci_io_handle *handle, uint32_t Reg, uint16_t data);
void Pci_io_write8 (struct pci_io_handle *handle, uint32_t Reg, uint8_t data);
From the two sets of function interfaces above, you can see that pciutils needs to initialize the struct Pci_dev, and the Pciasscess library needs to initialize the struct Pci_io_handle *handle. So how do you use the Pciutils static library, and by continuing to read/usr/include/pci/pci.h, you can see the following function declarations and comments:
/* Initialize PCI Access */
struct pci_access *pci_alloc (void) Pci_abi;
void Pci_init (struct pci_access *) Pci_abi;
void Pci_cleanup (struct pci_access *) Pci_abi;

/* Scanning of devices */
void Pci_scan_bus (struct pci_access *acc) Pci_abi;
struct Pci_dev *pci_get_dev (struct pci_access *acc, int domain, int bus, int dev, int func) Pci_abi; /* RAW access to specified device */
void Pci_free_dev (struct pci_dev *) Pci_abi;
Thus it is not difficult to guess the code flow using Pciutils and implement a test code TEST_PCILIB.C according to the header file description:
int main (void)
{
int i = 0;
int ret = 0;

struct pci_access * myaccess;
struct Pci_dev * MYDEV;

Myaccess = Pci_alloc ();
Pci_init (myaccess);

Mydev = Pci_get_dev (myaccess, 0, 6, 0, 0);

for (i = 0; i < i++) {
ret = Pci_read_byte (Mydev, i);
printf ("%d:%02x\n", I, ret);
}

Pci_free_dev (Mydev);

Pci_cleanup (myaccess);

return 0;
}


Compile


Since the above code is already known to be dependent on the LIBPCI.A static library, it can be compiled simply with the following GCC command:
[Email protected] testcases]# gcc-c-o test_pci.o test_pcilib.c
[Email protected] testcases]# Gcc-wall-o TEST_PCI TEST_PCI.O/USR/LIB64/LIBPCI.A
[Email protected] testcases]#./test_pci
0:b5
1:10
2:a0
3:87
4:07
5:04
6:10
7:00
8:ca
9:00
10:80
11:06
12:10
.....

This article from "Storage Chef" blog, reproduced please contact the author!

How Linux C calls the PCI Lib function

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.