]> mj.ucw.cz Git - pciutils.git/blob - lib/pci.h
bea893cf3bdcc8fa62378b325ee746b2297e8c69
[pciutils.git] / lib / pci.h
1 /*
2  *      $Id: pci.h,v 1.10 2002/12/26 19:28:33 mj Exp $
3  *
4  *      The PCI Library
5  *
6  *      Copyright (c) 1997--1999 Martin Mares <mj@ucw.cz>
7  *
8  *      Can be freely distributed and used under the terms of the GNU GPL.
9  */
10
11 #ifndef _PCI_LIB_H
12 #define _PCI_LIB_H
13
14 #include "config.h"
15
16 #ifdef HAVE_OWN_HEADER_H
17 #include "header.h"
18 #else
19 #include <linux/pci.h>
20 #endif
21
22 /*
23  *      Types
24  */
25
26 #ifdef OS_LINUX
27 #include <linux/types.h>
28
29 typedef __u8 byte;
30 typedef __u8 u8;
31 typedef __u16 word;
32 typedef __u16 u16;
33 typedef __u32 u32;
34 #endif
35
36 #ifdef OS_FREEBSD
37 #include <sys/types.h>
38
39 typedef u_int8_t byte;
40 typedef u_int8_t u8;
41 typedef u_int16_t word;
42 typedef u_int16_t u16;
43 typedef u_int32_t u32;
44 #endif
45
46 #ifdef OS_NETBSD
47 #include <sys/types.h>
48
49 typedef u_int8_t byte;
50 typedef u_int8_t u8;
51 typedef u_int16_t word;
52 typedef u_int16_t u16;
53 typedef u_int32_t u32;
54 #endif
55
56 #ifdef OS_AIX
57 #include <sys/param.h>
58
59 typedef u_int8_t byte;
60 typedef u_int8_t u8;
61 typedef u_int16_t word;
62 typedef u_int16_t u16;
63 typedef u_int32_t u32;
64 #endif
65
66 #ifdef HAVE_LONG_ADDRESS
67 typedef unsigned long long pciaddr_t;
68 #else
69 typedef unsigned long pciaddr_t;
70 #endif
71
72 /*
73  *      PCI Access Structure
74  */
75
76 struct pci_methods;
77 struct nl_entry;
78
79 #define PCI_ACCESS_AUTO                 0       /* Autodetection (params: none) */
80 #define PCI_ACCESS_PROC_BUS_PCI         1       /* Linux /proc/bus/pci (params: path) */
81 #define PCI_ACCESS_SYSCALLS             2       /* pciconfig_read() syscalls (params: none) */
82 #define PCI_ACCESS_I386_TYPE1           3       /* i386 ports, type 1 (params: none) */
83 #define PCI_ACCESS_I386_TYPE2           4       /* i386 ports, type 2 (params: none) */
84 #define PCI_ACCESS_FBSD_DEVICE          5       /* FreeBSD /dev/pci (params: path) */
85 #define PCI_ACCESS_AIX_DEVICE           6       /* /dev/pci0, /dev/bus0, etc. */
86 #define PCI_ACCESS_NBSD_LIBPCI          7
87 #define PCI_ACCESS_DUMP                 8       /* Dump file (params: filename) */
88 #define PCI_ACCESS_MAX                  9
89
90 struct pci_access {
91   /* Options you can change: */
92   unsigned int method;                  /* Access method */
93   char *method_params[PCI_ACCESS_MAX];  /* Parameters for the methods */
94   int writeable;                        /* Open in read/write mode */
95   int buscentric;                       /* Bus-centric view of the world */
96   char *id_file_name;                   /* Name of ID list file */
97   int numeric_ids;                      /* Don't resolve device IDs to names */
98   int debugging;                        /* Turn on debugging messages */
99
100   /* Functions you can override: */
101   void (*error)(char *msg, ...);        /* Write error message and quit */
102   void (*warning)(char *msg, ...);      /* Write a warning message */
103   void (*debug)(char *msg, ...);        /* Write a debugging message */
104
105   struct pci_dev *devices;              /* Devices found on this bus */
106
107   /* Fields used internally: */
108   struct pci_methods *methods;
109   char *nl_list;                        /* Name list cache */
110   struct nl_entry **nl_hash;
111   int fd;                               /* proc: fd */
112   int fd_rw;                            /* proc: fd opened read-write */
113   struct pci_dev *cached_dev;           /* proc: device the fd is for */
114   int fd_pos;                           /* proc: current position */
115 };
116
117 /* Initialize PCI access */
118 struct pci_access *pci_alloc(void);
119 void pci_init(struct pci_access *);
120 void pci_cleanup(struct pci_access *);
121
122 /* Scanning of devices */
123 void pci_scan_bus(struct pci_access *acc);
124 struct pci_dev *pci_get_dev(struct pci_access *acc, int bus, int dev, int func); /* Raw access to specified device */
125 void pci_free_dev(struct pci_dev *);
126
127 /*
128  *      Devices
129  */
130
131 struct pci_dev {
132   struct pci_dev *next;                 /* Next device in the chain */
133   word bus;                             /* Higher byte can select host bridges */
134   byte dev, func;                       /* Device and function */
135
136   /* These fields are set by pci_fill_info() */
137   int known_fields;                     /* Set of info fields already known */
138   word vendor_id, device_id;            /* Identity of the device */
139   int irq;                              /* IRQ number */
140   pciaddr_t base_addr[6];               /* Base addresses */
141   pciaddr_t size[6];                    /* Region sizes */
142   pciaddr_t rom_base_addr;              /* Expansion ROM base address */
143   pciaddr_t rom_size;                   /* Expansion ROM size */
144
145   /* Fields used internally: */
146   struct pci_access *access;
147   struct pci_methods *methods;
148   byte *cache;                          /* Cached information */
149   int cache_len;
150   int hdrtype;                          /* Direct methods: header type */
151   void *aux;                            /* Auxillary data */
152 };
153
154 #define PCI_ADDR_IO_MASK (~(pciaddr_t) 0x3)
155 #define PCI_ADDR_MEM_MASK (~(pciaddr_t) 0xf)
156
157 byte pci_read_byte(struct pci_dev *, int pos); /* Access to configuration space */
158 word pci_read_word(struct pci_dev *, int pos);
159 u32  pci_read_long(struct pci_dev *, int pos);
160 int pci_read_block(struct pci_dev *, int pos, byte *buf, int len);
161 int pci_write_byte(struct pci_dev *, int pos, byte data);
162 int pci_write_word(struct pci_dev *, int pos, word data);
163 int pci_write_long(struct pci_dev *, int pos, u32 data);
164 int pci_write_block(struct pci_dev *, int pos, byte *buf, int len);
165
166 int pci_fill_info(struct pci_dev *, int flags); /* Fill in device information */
167
168 #define PCI_FILL_IDENT          1
169 #define PCI_FILL_IRQ            2
170 #define PCI_FILL_BASES          4
171 #define PCI_FILL_ROM_BASE       8
172 #define PCI_FILL_SIZES          16
173 #define PCI_FILL_RESCAN         0x10000
174
175 void pci_setup_cache(struct pci_dev *, byte *cache, int len);
176
177 /*
178  *      Filters
179  */
180
181 struct pci_filter {
182   int bus, slot, func;                  /* -1 = ANY */
183   int vendor, device;
184 };
185
186 void pci_filter_init(struct pci_access *, struct pci_filter *);
187 char *pci_filter_parse_slot(struct pci_filter *, char *);
188 char *pci_filter_parse_id(struct pci_filter *, char *);
189 int pci_filter_match(struct pci_filter *, struct pci_dev *);
190
191 /*
192  *      Device names
193  */
194
195 char *pci_lookup_name(struct pci_access *a, char *buf, int size, int flags, u32 arg1, u32 arg2, u32 arg3, u32 arg4);
196 void pci_free_name_list(struct pci_access *a);
197
198 #define PCI_LOOKUP_VENDOR 1
199 #define PCI_LOOKUP_DEVICE 2
200 #define PCI_LOOKUP_CLASS 4
201 #define PCI_LOOKUP_SUBSYSTEM 8
202 #define PCI_LOOKUP_PROGIF 16
203 #define PCI_LOOKUP_NUMERIC 0x10000
204
205 #endif