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