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