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