]> mj.ucw.cz Git - pciutils.git/blob - lib/access.c
lib/i386-ports.c, lib/i386-io-*: Moved the logic which keeps track of
[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   a->id_file_name = PCI_PATH_IDS;
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_mfree(a);
186 }
187
188 void
189 pci_scan_bus(struct pci_access *a)
190 {
191   a->methods->scan(a);
192 }
193
194 struct pci_dev *
195 pci_alloc_dev(struct pci_access *a)
196 {
197   struct pci_dev *d = pci_malloc(a, sizeof(struct pci_dev));
198
199   bzero(d, sizeof(*d));
200   d->access = a;
201   d->methods = a->methods;
202   d->hdrtype = -1;
203   if (d->methods->init_dev)
204     d->methods->init_dev(d);
205   return d;
206 }
207
208 int
209 pci_link_dev(struct pci_access *a, struct pci_dev *d)
210 {
211   d->next = a->devices;
212   a->devices = d;
213
214   return 1;
215 }
216
217 struct pci_dev *
218 pci_get_dev(struct pci_access *a, int domain, int bus, int dev, int func)
219 {
220   struct pci_dev *d = pci_alloc_dev(a);
221
222   d->domain = domain;
223   d->bus = bus;
224   d->dev = dev;
225   d->func = func;
226   return d;
227 }
228
229 void pci_free_dev(struct pci_dev *d)
230 {
231   if (d->methods->cleanup_dev)
232     d->methods->cleanup_dev(d);
233   pci_mfree(d);
234 }
235
236 static inline void
237 pci_read_data(struct pci_dev *d, void *buf, int pos, int len)
238 {
239   if (pos & (len-1))
240     d->access->error("Unaligned read: pos=%02x, len=%d", pos, len);
241   if (pos + len <= d->cache_len)
242     memcpy(buf, d->cache + pos, len);
243   else if (!d->methods->read(d, pos, buf, len))
244     memset(buf, 0xff, len);
245 }
246
247 byte
248 pci_read_byte(struct pci_dev *d, int pos)
249 {
250   byte buf;
251   pci_read_data(d, &buf, pos, 1);
252   return buf;
253 }
254
255 word
256 pci_read_word(struct pci_dev *d, int pos)
257 {
258   word buf;
259   pci_read_data(d, &buf, pos, 2);
260   return le16_to_cpu(buf);
261 }
262
263 u32
264 pci_read_long(struct pci_dev *d, int pos)
265 {
266   u32 buf;
267   pci_read_data(d, &buf, pos, 4);
268   return le32_to_cpu(buf);
269 }
270
271 int
272 pci_read_block(struct pci_dev *d, int pos, byte *buf, int len)
273 {
274   return d->methods->read(d, pos, buf, len);
275 }
276
277 static inline int
278 pci_write_data(struct pci_dev *d, void *buf, int pos, int len)
279 {
280   if (pos & (len-1))
281     d->access->error("Unaligned write: pos=%02x,len=%d", pos, len);
282   if (pos + len <= d->cache_len)
283     memcpy(d->cache + pos, buf, len);
284   return d->methods->write(d, pos, buf, len);
285 }
286
287 int
288 pci_write_byte(struct pci_dev *d, int pos, byte data)
289 {
290   return pci_write_data(d, &data, pos, 1);
291 }
292
293 int
294 pci_write_word(struct pci_dev *d, int pos, word data)
295 {
296   word buf = cpu_to_le16(data);
297   return pci_write_data(d, &buf, pos, 2);
298 }
299
300 int
301 pci_write_long(struct pci_dev *d, int pos, u32 data)
302 {
303   u32 buf = cpu_to_le32(data);
304   return pci_write_data(d, &buf, pos, 4);
305 }
306
307 int
308 pci_write_block(struct pci_dev *d, int pos, byte *buf, int len)
309 {
310   if (pos < d->cache_len)
311     {
312       int l = (pos + len >= d->cache_len) ? (d->cache_len - pos) : len;
313       memcpy(d->cache + pos, buf, l);
314     }
315   return d->methods->write(d, pos, buf, len);
316 }
317
318 int
319 pci_fill_info(struct pci_dev *d, int flags)
320 {
321   if (flags & PCI_FILL_RESCAN)
322     {
323       flags &= ~PCI_FILL_RESCAN;
324       d->known_fields = 0;
325     }
326   if (flags & ~d->known_fields)
327     d->known_fields |= d->methods->fill_info(d, flags & ~d->known_fields);
328   return d->known_fields;
329 }
330
331 void
332 pci_setup_cache(struct pci_dev *d, byte *cache, int len)
333 {
334   d->cache = cache;
335   d->cache_len = len;
336 }