xref: /pciutils/example.c (revision de7ef8bc)
1bc46bc39SMartin Mares /*
2bc46bc39SMartin Mares  *	The PCI Library -- Example of use (simplistic lister of PCI devices)
3bc46bc39SMartin Mares  *
4bc46bc39SMartin Mares  *	Written by Martin Mares and put to public domain. You can do
5bc46bc39SMartin Mares  *	with it anything you want, but I don't give you any warranty.
6bc46bc39SMartin Mares  */
7bc46bc39SMartin Mares 
8bc46bc39SMartin Mares #include <stdio.h>
9bc46bc39SMartin Mares 
10bc46bc39SMartin Mares #include "lib/pci.h"
11bc46bc39SMartin Mares 
main(void)12bc46bc39SMartin Mares int main(void)
13bc46bc39SMartin Mares {
14bc46bc39SMartin Mares   struct pci_access *pacc;
15bc46bc39SMartin Mares   struct pci_dev *dev;
16bc46bc39SMartin Mares   unsigned int c;
17*91d4213fSMartin Mares   char namebuf[1024], *name;
18bc46bc39SMartin Mares 
19bc46bc39SMartin Mares   pacc = pci_alloc();		/* Get the pci_access structure */
20bc46bc39SMartin Mares   /* Set all options you want -- here we stick with the defaults */
21bc46bc39SMartin Mares   pci_init(pacc);		/* Initialize the PCI library */
22bc46bc39SMartin Mares   pci_scan_bus(pacc);		/* We want to get the list of devices */
23bc46bc39SMartin Mares   for (dev=pacc->devices; dev; dev=dev->next)	/* Iterate over all devices */
24bc46bc39SMartin Mares     {
25bc46bc39SMartin Mares       pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES | PCI_FILL_CLASS);	/* Fill in header info we need */
26bc46bc39SMartin Mares       c = pci_read_byte(dev, PCI_INTERRUPT_PIN);				/* Read config register directly */
27*91d4213fSMartin Mares       printf("%04x:%02x:%02x.%d vendor=%04x device=%04x class=%04x irq=%d (pin %d) base0=%lx",
288d2739f8SMartin Mares 	     dev->domain, dev->bus, dev->dev, dev->func, dev->vendor_id, dev->device_id,
29501f8d88SMartin Mares 	     dev->device_class, dev->irq, c, (long) dev->base_addr[0]);
30*91d4213fSMartin Mares 
31*91d4213fSMartin Mares       /* Look up and print the full name of the device */
32*91d4213fSMartin Mares       name = pci_lookup_name(pacc, namebuf, sizeof(namebuf), PCI_LOOKUP_DEVICE, dev->vendor_id, dev->device_id);
33*91d4213fSMartin Mares       printf(" (%s)\n", name);
34bc46bc39SMartin Mares     }
35bc46bc39SMartin Mares   pci_cleanup(pacc);		/* Close everything */
36bc46bc39SMartin Mares   return 0;
37bc46bc39SMartin Mares }
38