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