]> mj.ucw.cz Git - pciutils.git/blob - lib/pci.h
fbf41d424b19bec7f573187a6ca073c57b117c05
[pciutils.git] / lib / pci.h
1 /*
2  *      $Id: pci.h,v 1.6 1999/07/20 14:01:35 mj Exp $
3  *
4  *      The PCI Library
5  *
6  *      Copyright (c) 1997--1999 Martin Mares <mj@atrey.karlin.mff.cuni.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 HAVE_LONG_ADDRESS
47 typedef unsigned long long pciaddr_t;
48 #else
49 typedef unsigned long pciaddr_t;
50 #endif
51
52 /*
53  *      PCI Access Structure
54  */
55
56 struct pci_methods;
57 struct nl_entry;
58
59 #define PCI_ACCESS_AUTO                 0       /* Autodetection (params: none) */
60 #define PCI_ACCESS_PROC_BUS_PCI         1       /* Linux /proc/bus/pci (params: path) */
61 #define PCI_ACCESS_SYSCALLS             2       /* pciconfig_read() syscalls (params: none) */
62 #define PCI_ACCESS_I386_TYPE1           3       /* i386 ports, type 1 (params: none) */
63 #define PCI_ACCESS_I386_TYPE2           4       /* i386 ports, type 2 (params: none) */
64 #define PCI_ACCESS_FBSD_DEVICE          5       /* FreeBSD /dev/pci (params: path) */
65 #define PCI_ACCESS_DUMP                 6       /* Dump file (params: filename) */
66 #define PCI_ACCESS_MAX                  7
67
68 struct pci_access {
69   /* Options you can change: */
70   unsigned int method;                  /* Access method */
71   char *method_params[PCI_ACCESS_MAX];  /* Parameters for the methods */
72   int writeable;                        /* Open in read/write mode */
73   int buscentric;                       /* Bus-centric view of the world */
74   char *id_file_name;                   /* Name of ID list file */
75   int numeric_ids;                      /* Don't resolve device IDs to names */
76   int debugging;                        /* Turn on debugging messages */
77
78   /* Functions you can override: */
79   void (*error)(char *msg, ...);        /* Write error message and quit */
80   void (*warning)(char *msg, ...);      /* Write a warning message */
81   void (*debug)(char *msg, ...);        /* Write a debugging message */
82
83   struct pci_dev *devices;              /* Devices found on this bus */
84
85   /* Fields used internally: */
86   struct pci_methods *methods;
87   char *nl_list;                        /* Name list cache */
88   struct nl_entry **nl_hash;
89   int fd;                               /* proc: fd */
90   int fd_rw;                            /* proc: fd opened read-write */
91   struct pci_dev *cached_dev;           /* proc: device the fd is for */
92   int fd_pos;                           /* proc: current position */
93 };
94
95 /* Initialize PCI access */
96 struct pci_access *pci_alloc(void);
97 void pci_init(struct pci_access *);
98 void pci_cleanup(struct pci_access *);
99
100 /* Scanning of devices */
101 void pci_scan_bus(struct pci_access *acc);
102 struct pci_dev *pci_get_dev(struct pci_access *acc, int bus, int dev, int func); /* Raw access to specified device */
103 void pci_free_dev(struct pci_dev *);
104
105 /*
106  *      Devices
107  */
108
109 struct pci_dev {
110   struct pci_dev *next;                 /* Next device in the chain */
111   word bus;                             /* Higher byte can select host bridges */
112   byte dev, func;                       /* Device and function */
113
114   /* These fields are set by pci_fill_info() */
115   int known_fields;                     /* Set of info fields already known */
116   word vendor_id, device_id;            /* Identity of the device */
117   int irq;                              /* IRQ number */
118   pciaddr_t base_addr[6];               /* Base addresses */
119   pciaddr_t size[6];                    /* Region sizes */
120   pciaddr_t rom_base_addr;              /* Expansion ROM base address */
121   pciaddr_t rom_size;                   /* Expansion ROM size */
122
123   /* Fields used internally: */
124   struct pci_access *access;
125   struct pci_methods *methods;
126   byte *cache;                          /* Cached information */
127   int cache_len;
128   int hdrtype;                          /* Direct methods: header type */
129   void *aux;                            /* Auxillary data */
130 };
131
132 #define PCI_ADDR_IO_MASK (~(pciaddr_t) 0x3)
133 #define PCI_ADDR_MEM_MASK (~(pciaddr_t) 0xf)
134
135 byte pci_read_byte(struct pci_dev *, int pos); /* Access to configuration space */
136 word pci_read_word(struct pci_dev *, int pos);
137 u32  pci_read_long(struct pci_dev *, int pos);
138 int pci_read_block(struct pci_dev *, int pos, byte *buf, int len);
139 int pci_write_byte(struct pci_dev *, int pos, byte data);
140 int pci_write_word(struct pci_dev *, int pos, word data);
141 int pci_write_long(struct pci_dev *, int pos, u32 data);
142 int pci_write_block(struct pci_dev *, int pos, byte *buf, int len);
143
144 int pci_fill_info(struct pci_dev *, int flags); /* Fill in device information */
145
146 #define PCI_FILL_IDENT          1
147 #define PCI_FILL_IRQ            2
148 #define PCI_FILL_BASES          4
149 #define PCI_FILL_ROM_BASE       8
150 #define PCI_FILL_SIZES          16
151 #define PCI_FILL_RESCAN         0x10000
152
153 void pci_setup_cache(struct pci_dev *, byte *cache, int len);
154
155 /*
156  *      Filters
157  */
158
159 struct pci_filter {
160   int bus, slot, func;                  /* -1 = ANY */
161   int vendor, device;
162 };
163
164 void pci_filter_init(struct pci_access *, struct pci_filter *);
165 char *pci_filter_parse_slot(struct pci_filter *, char *);
166 char *pci_filter_parse_id(struct pci_filter *, char *);
167 int pci_filter_match(struct pci_filter *, struct pci_dev *);
168
169 /*
170  *      Device names
171  */
172
173 char *pci_lookup_name(struct pci_access *a, char *buf, int size, int flags, u32 arg1, u32 arg2);
174 void pci_free_name_list(struct pci_access *a);
175
176 #define PCI_LOOKUP_VENDOR 1
177 #define PCI_LOOKUP_DEVICE 2
178 #define PCI_LOOKUP_CLASS 4
179 #define PCI_LOOKUP_SUBSYSTEM 8
180 #define PCI_LOOKUP_NUMERIC 0x10000
181
182 #endif