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