]> mj.ucw.cz Git - pciutils.git/blob - lib/pci.h
c92df1491f8d69227155dadf28ba9adc278203f6
[pciutils.git] / lib / pci.h
1 /*
2  *      The PCI Library
3  *
4  *      Copyright (c) 1997--2005 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL.
7  */
8
9 #ifndef _PCI_LIB_H
10 #define _PCI_LIB_H
11
12 #include "config.h"
13 #include "header.h"
14 #include "types.h"
15
16 /*
17  *      PCI Access Structure
18  */
19
20 struct pci_methods;
21
22 enum pci_access_type {
23   /* Known access methods, remember to update access.c as well */
24   PCI_ACCESS_AUTO,                      /* Autodetection (params: none) */
25   PCI_ACCESS_SYS_BUS_PCI,               /* Linux /sys/bus/pci (params: path) */
26   PCI_ACCESS_PROC_BUS_PCI,              /* Linux /proc/bus/pci (params: path) */
27   PCI_ACCESS_I386_TYPE1,                /* i386 ports, type 1 (params: none) */
28   PCI_ACCESS_I386_TYPE2,                /* i386 ports, type 2 (params: none) */
29   PCI_ACCESS_FBSD_DEVICE,               /* FreeBSD /dev/pci (params: path) */
30   PCI_ACCESS_AIX_DEVICE,                /* /dev/pci0, /dev/bus0, etc. */
31   PCI_ACCESS_NBSD_LIBPCI,               /* NetBSD libpci */
32   PCI_ACCESS_DUMP,                      /* Dump file (params: filename) */
33   PCI_ACCESS_MAX
34 };
35
36 struct pci_access {
37   /* Options you can change: */
38   unsigned int method;                  /* Access method */
39   char *method_params[PCI_ACCESS_MAX];  /* Parameters for the methods */
40   int writeable;                        /* Open in read/write mode */
41   int buscentric;                       /* Bus-centric view of the world */
42   char *id_file_name;                   /* Name of ID list file */
43   int numeric_ids;                      /* Don't resolve device IDs to names */
44   int debugging;                        /* Turn on debugging messages */
45
46   /* Functions you can override: */
47   void (*error)(char *msg, ...);        /* Write error message and quit */
48   void (*warning)(char *msg, ...);      /* Write a warning message */
49   void (*debug)(char *msg, ...);        /* Write a debugging message */
50
51   struct pci_dev *devices;              /* Devices found on this bus */
52
53   /* Fields used internally: */
54   struct pci_methods *methods;
55   struct id_entry **id_hash;            /* names.c */
56   struct id_bucket *current_id_bucket;
57   int fd;                               /* proc: fd */
58   int fd_rw;                            /* proc: fd opened read-write */
59   struct pci_dev *cached_dev;           /* proc: device the fd is for */
60   int fd_pos;                           /* proc: current position */
61 };
62
63 /* Initialize PCI access */
64 struct pci_access *pci_alloc(void);
65 void pci_init(struct pci_access *);
66 void pci_cleanup(struct pci_access *);
67
68 /* Scanning of devices */
69 void pci_scan_bus(struct pci_access *acc);
70 struct pci_dev *pci_get_dev(struct pci_access *acc, int domain, int bus, int dev, int func); /* Raw access to specified device */
71 void pci_free_dev(struct pci_dev *);
72
73 /*
74  *      Devices
75  */
76
77 struct pci_dev {
78   struct pci_dev *next;                 /* Next device in the chain */
79   u16 domain;                           /* PCI domain (host bridge) */
80   u8 bus, dev, func;                    /* Bus inside domain, device and function */
81
82   /* These fields are set by pci_fill_info() */
83   int known_fields;                     /* Set of info fields already known */
84   u16 vendor_id, device_id;             /* Identity of the device */
85   int irq;                              /* IRQ number */
86   pciaddr_t base_addr[6];               /* Base addresses */
87   pciaddr_t size[6];                    /* Region sizes */
88   pciaddr_t rom_base_addr;              /* Expansion ROM base address */
89   pciaddr_t rom_size;                   /* Expansion ROM size */
90
91   /* Fields used internally: */
92   struct pci_access *access;
93   struct pci_methods *methods;
94   u8 *cache;                            /* Cached config registers */
95   int cache_len;
96   int hdrtype;                          /* Cached low 7 bits of header type, -1 if unknown */
97   void *aux;                            /* Auxillary data */
98 };
99
100 #define PCI_ADDR_IO_MASK (~(pciaddr_t) 0x3)
101 #define PCI_ADDR_MEM_MASK (~(pciaddr_t) 0xf)
102
103 u8 pci_read_byte(struct pci_dev *, int pos); /* Access to configuration space */
104 u16 pci_read_word(struct pci_dev *, int pos);
105 u32  pci_read_long(struct pci_dev *, int pos);
106 int pci_read_block(struct pci_dev *, int pos, u8 *buf, int len);
107 int pci_write_byte(struct pci_dev *, int pos, u8 data);
108 int pci_write_word(struct pci_dev *, int pos, u16 data);
109 int pci_write_long(struct pci_dev *, int pos, u32 data);
110 int pci_write_block(struct pci_dev *, int pos, u8 *buf, int len);
111
112 int pci_fill_info(struct pci_dev *, int flags); /* Fill in device information */
113
114 #define PCI_FILL_IDENT          1
115 #define PCI_FILL_IRQ            2
116 #define PCI_FILL_BASES          4
117 #define PCI_FILL_ROM_BASE       8
118 #define PCI_FILL_SIZES          16
119 #define PCI_FILL_RESCAN         0x10000
120
121 void pci_setup_cache(struct pci_dev *, u8 *cache, int len);
122
123 /*
124  *      Filters
125  */
126
127 struct pci_filter {
128   int domain, bus, slot, func;                  /* -1 = ANY */
129   int vendor, device;
130 };
131
132 void pci_filter_init(struct pci_access *, struct pci_filter *);
133 char *pci_filter_parse_slot(struct pci_filter *, char *);
134 char *pci_filter_parse_id(struct pci_filter *, char *);
135 int pci_filter_match(struct pci_filter *, struct pci_dev *);
136
137 /*
138  *      Conversion of PCI ID's to names (according to the pci.ids file)
139  *
140  *      Call pci_lookup_name() to identify different types of ID's:
141  *
142  *      VENDOR                          (vendorID) -> vendor
143  *      DEVICE                          (vendorID, deviceID) -> device
144  *      VENDOR | DEVICE                 (vendorID, deviceID) -> combined vendor and device
145  *      SUBSYSTEM | VENDOR              (subvendorID) -> subsystem vendor
146  *      SUBSYSTEM | DEVICE              (vendorID, deviceID, subvendorID, subdevID) -> subsystem device
147  *      SUBSYSTEM | VENDOR | DEVICE     (vendorID, deviceID, subvendorID, subdevID) -> combined subsystem v+d
148  *      SUBSYSTEM | ...                 (-1, -1, subvendorID, subdevID) -> generic subsystem
149  *      CLASS                           (classID) -> class
150  *      PROGIF                          (classID, progif) -> programming interface
151  */
152
153 char *pci_lookup_name(struct pci_access *a, char *buf, int size, int flags, ...);
154
155 int pci_load_name_list(struct pci_access *a);   /* Called automatically by pci_lookup_*() when needed; returns success */
156 void pci_free_name_list(struct pci_access *a);  /* Called automatically by pci_cleanup() */
157
158 enum pci_lookup_mode {
159   PCI_LOOKUP_VENDOR = 1,                /* Vendor name (args: vendorID) */
160   PCI_LOOKUP_DEVICE = 2,                /* Device name (args: vendorID, deviceID) */
161   PCI_LOOKUP_CLASS = 4,                 /* Device class (args: classID) */
162   PCI_LOOKUP_SUBSYSTEM = 8,
163   PCI_LOOKUP_PROGIF = 16,               /* Programming interface (args: classID, prog_if) */
164   PCI_LOOKUP_NUMERIC = 0x10000,         /* Want only formatted numbers; default if access->numeric_ids is set */
165   PCI_LOOKUP_NO_NUMBERS = 0x20000       /* Return NULL if not found in the database; default is to print numerically */
166 };
167
168 #endif