]> mj.ucw.cz Git - pciutils.git/blob - lib/access.c
Added support for compressed pci.ids.
[pciutils.git] / lib / access.c
1 /*
2  *      The PCI Library -- User Access
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 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stdarg.h>
12 #include <string.h>
13
14 #include "internal.h"
15
16 static struct pci_methods *pci_methods[PCI_ACCESS_MAX] = {
17   NULL,
18 #ifdef PCI_HAVE_PM_LINUX_SYSFS
19   &pm_linux_sysfs,
20 #else
21   NULL,
22 #endif
23 #ifdef PCI_HAVE_PM_LINUX_PROC
24   &pm_linux_proc,
25 #else
26   NULL,
27 #endif
28 #ifdef PCI_HAVE_PM_INTEL_CONF
29   &pm_intel_conf1,
30   &pm_intel_conf2,
31 #else
32   NULL,
33   NULL,
34 #endif
35 #ifdef PCI_HAVE_PM_FBSD_DEVICE
36   &pm_fbsd_device,
37 #else
38   NULL,
39 #endif
40 #ifdef PCI_HAVE_PM_AIX_DEVICE
41   &pm_aix_device,
42 #else
43   NULL,
44 #endif
45 #ifdef PCI_HAVE_PM_NBSD_LIBPCI
46   &pm_nbsd_libpci,
47 #else
48   NULL,
49 #endif
50 #ifdef PCI_HAVE_PM_OBSD_DEVICE
51   &pm_obsd_device,
52 #else
53   NULL,
54 #endif
55 #ifdef PCI_HAVE_PM_DUMP
56   &pm_dump,
57 #else
58   NULL,
59 #endif
60 };
61
62 struct pci_access *
63 pci_alloc(void)
64 {
65   struct pci_access *a = malloc(sizeof(struct pci_access));
66   int i;
67
68   bzero(a, sizeof(*a));
69   pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0);
70   for(i=0; i<PCI_ACCESS_MAX; i++)
71     if (pci_methods[i] && pci_methods[i]->config)
72       pci_methods[i]->config(a);
73   return a;
74 }
75
76 void *
77 pci_malloc(struct pci_access *a, int size)
78 {
79   void *x = malloc(size);
80
81   if (!x)
82     a->error("Out of memory (allocation of %d bytes failed)", size);
83   return x;
84 }
85
86 void
87 pci_mfree(void *x)
88 {
89   if (x)
90     free(x);
91 }
92
93 static void
94 pci_generic_error(char *msg, ...)
95 {
96   va_list args;
97
98   va_start(args, msg);
99   fputs("pcilib: ", stderr);
100   vfprintf(stderr, msg, args);
101   fputc('\n', stderr);
102   exit(1);
103 }
104
105 static void
106 pci_generic_warn(char *msg, ...)
107 {
108   va_list args;
109
110   va_start(args, msg);
111   fputs("pcilib: ", stderr);
112   vfprintf(stderr, msg, args);
113   fputc('\n', stderr);
114 }
115
116 static void
117 pci_generic_debug(char *msg, ...)
118 {
119   va_list args;
120
121   va_start(args, msg);
122   vfprintf(stdout, msg, args);
123   va_end(args);
124 }
125
126 static void
127 pci_null_debug(char *msg UNUSED, ...)
128 {
129 }
130
131 void
132 pci_init(struct pci_access *a)
133 {
134   if (!a->error)
135     a->error = pci_generic_error;
136   if (!a->warning)
137     a->warning = pci_generic_warn;
138   if (!a->debug)
139     a->debug = pci_generic_debug;
140   if (!a->debugging)
141     a->debug = pci_null_debug;
142
143   if (a->method)
144     {
145       if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
146         a->error("This access method is not supported.");
147       a->methods = pci_methods[a->method];
148     }
149   else
150     {
151       unsigned int i;
152       for(i=0; i<PCI_ACCESS_MAX; i++)
153         if (pci_methods[i])
154           {
155             a->debug("Trying method %d...", i);
156             if (pci_methods[i]->detect(a))
157               {
158                 a->debug("...OK\n");
159                 a->methods = pci_methods[i];
160                 a->method = i;
161                 break;
162               }
163             a->debug("...No.\n");
164           }
165       if (!a->methods)
166         a->error("Cannot find any working access method.");
167     }
168   a->debug("Decided to use %s\n", a->methods->name);
169   a->methods->init(a);
170 }
171
172 void
173 pci_cleanup(struct pci_access *a)
174 {
175   struct pci_dev *d, *e;
176
177   for(d=a->devices; d; d=e)
178     {
179       e = d->next;
180       pci_free_dev(d);
181     }
182   if (a->methods)
183     a->methods->cleanup(a);
184   pci_free_name_list(a);
185   pci_set_name_list_path(a, NULL, 0);
186   pci_mfree(a);
187 }
188
189 void
190 pci_scan_bus(struct pci_access *a)
191 {
192   a->methods->scan(a);
193 }
194
195 struct pci_dev *
196 pci_alloc_dev(struct pci_access *a)
197 {
198   struct pci_dev *d = pci_malloc(a, sizeof(struct pci_dev));
199
200   bzero(d, sizeof(*d));
201   d->access = a;
202   d->methods = a->methods;
203   d->hdrtype = -1;
204   if (d->methods->init_dev)
205     d->methods->init_dev(d);
206   return d;
207 }
208
209 int
210 pci_link_dev(struct pci_access *a, struct pci_dev *d)
211 {
212   d->next = a->devices;
213   a->devices = d;
214
215   return 1;
216 }
217
218 struct pci_dev *
219 pci_get_dev(struct pci_access *a, int domain, int bus, int dev, int func)
220 {
221   struct pci_dev *d = pci_alloc_dev(a);
222
223   d->domain = domain;
224   d->bus = bus;
225   d->dev = dev;
226   d->func = func;
227   return d;
228 }
229
230 void pci_free_dev(struct pci_dev *d)
231 {
232   if (d->methods->cleanup_dev)
233     d->methods->cleanup_dev(d);
234   pci_mfree(d);
235 }
236
237 static inline void
238 pci_read_data(struct pci_dev *d, void *buf, int pos, int len)
239 {
240   if (pos & (len-1))
241     d->access->error("Unaligned read: pos=%02x, len=%d", pos, len);
242   if (pos + len <= d->cache_len)
243     memcpy(buf, d->cache + pos, len);
244   else if (!d->methods->read(d, pos, buf, len))
245     memset(buf, 0xff, len);
246 }
247
248 byte
249 pci_read_byte(struct pci_dev *d, int pos)
250 {
251   byte buf;
252   pci_read_data(d, &buf, pos, 1);
253   return buf;
254 }
255
256 word
257 pci_read_word(struct pci_dev *d, int pos)
258 {
259   word buf;
260   pci_read_data(d, &buf, pos, 2);
261   return le16_to_cpu(buf);
262 }
263
264 u32
265 pci_read_long(struct pci_dev *d, int pos)
266 {
267   u32 buf;
268   pci_read_data(d, &buf, pos, 4);
269   return le32_to_cpu(buf);
270 }
271
272 int
273 pci_read_block(struct pci_dev *d, int pos, byte *buf, int len)
274 {
275   return d->methods->read(d, pos, buf, len);
276 }
277
278 static inline int
279 pci_write_data(struct pci_dev *d, void *buf, int pos, int len)
280 {
281   if (pos & (len-1))
282     d->access->error("Unaligned write: pos=%02x,len=%d", pos, len);
283   if (pos + len <= d->cache_len)
284     memcpy(d->cache + pos, buf, len);
285   return d->methods->write(d, pos, buf, len);
286 }
287
288 int
289 pci_write_byte(struct pci_dev *d, int pos, byte data)
290 {
291   return pci_write_data(d, &data, pos, 1);
292 }
293
294 int
295 pci_write_word(struct pci_dev *d, int pos, word data)
296 {
297   word buf = cpu_to_le16(data);
298   return pci_write_data(d, &buf, pos, 2);
299 }
300
301 int
302 pci_write_long(struct pci_dev *d, int pos, u32 data)
303 {
304   u32 buf = cpu_to_le32(data);
305   return pci_write_data(d, &buf, pos, 4);
306 }
307
308 int
309 pci_write_block(struct pci_dev *d, int pos, byte *buf, int len)
310 {
311   if (pos < d->cache_len)
312     {
313       int l = (pos + len >= d->cache_len) ? (d->cache_len - pos) : len;
314       memcpy(d->cache + pos, buf, l);
315     }
316   return d->methods->write(d, pos, buf, len);
317 }
318
319 int
320 pci_fill_info(struct pci_dev *d, int flags)
321 {
322   if (flags & PCI_FILL_RESCAN)
323     {
324       flags &= ~PCI_FILL_RESCAN;
325       d->known_fields = 0;
326     }
327   if (flags & ~d->known_fields)
328     d->known_fields |= d->methods->fill_info(d, flags & ~d->known_fields);
329   return d->known_fields;
330 }
331
332 void
333 pci_setup_cache(struct pci_dev *d, byte *cache, int len)
334 {
335   d->cache = cache;
336   d->cache_len = len;
337 }