]> mj.ucw.cz Git - pciutils.git/blob - lib/access.c
55060b5dd79f6c0e0ec6d1fdf6d11cde1e927dc0
[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 HAVE_PM_LINUX_SYSFS
19   &pm_linux_sysfs,
20 #else
21   NULL,
22 #endif
23 #ifdef HAVE_PM_LINUX_PROC
24   &pm_linux_proc,
25 #else
26   NULL,
27 #endif
28 #ifdef HAVE_PM_INTEL_CONF
29   &pm_intel_conf1,
30   &pm_intel_conf2,
31 #else
32   NULL,
33   NULL,
34 #endif
35 #ifdef HAVE_PM_FBSD_DEVICE
36   &pm_fbsd_device,
37 #else
38   NULL,
39 #endif
40 #ifdef HAVE_PM_AIX_DEVICE
41   &pm_aix_device,
42 #else
43   NULL,
44 #endif
45 #ifdef HAVE_PM_NBSD_LIBPCI
46   &pm_nbsd_libpci,
47 #else
48   NULL,
49 #endif
50 #ifdef 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 = PATH_PCI_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   if (d->methods->init_dev)
198     d->methods->init_dev(d);
199   return d;
200 }
201
202 int
203 pci_link_dev(struct pci_access *a, struct pci_dev *d)
204 {
205   d->next = a->devices;
206   a->devices = d;
207
208   return 1;
209 }
210
211 struct pci_dev *
212 pci_get_dev(struct pci_access *a, int bus, int dev, int func)
213 {
214   struct pci_dev *d = pci_alloc_dev(a);
215
216   d->bus = bus;
217   d->dev = dev;
218   d->func = func;
219   return d;
220 }
221
222 void pci_free_dev(struct pci_dev *d)
223 {
224   if (d->methods->cleanup_dev)
225     d->methods->cleanup_dev(d);
226   pci_mfree(d);
227 }
228
229 static inline void
230 pci_read_data(struct pci_dev *d, void *buf, int pos, int len)
231 {
232   if (pos & (len-1))
233     d->access->error("Unaligned read: pos=%02x, len=%d", pos, len);
234   if (pos + len <= d->cache_len)
235     memcpy(buf, d->cache + pos, len);
236   else if (!d->methods->read(d, pos, buf, len))
237     memset(buf, 0xff, len);
238 }
239
240 byte
241 pci_read_byte(struct pci_dev *d, int pos)
242 {
243   byte buf;
244   pci_read_data(d, &buf, pos, 1);
245   return buf;
246 }
247
248 word
249 pci_read_word(struct pci_dev *d, int pos)
250 {
251   word buf;
252   pci_read_data(d, &buf, pos, 2);
253   return le16_to_cpu(buf);
254 }
255
256 u32
257 pci_read_long(struct pci_dev *d, int pos)
258 {
259   u32 buf;
260   pci_read_data(d, &buf, pos, 4);
261   return le32_to_cpu(buf);
262 }
263
264 int
265 pci_read_block(struct pci_dev *d, int pos, byte *buf, int len)
266 {
267   return d->methods->read(d, pos, buf, len);
268 }
269
270 static inline int
271 pci_write_data(struct pci_dev *d, void *buf, int pos, int len)
272 {
273   if (pos & (len-1))
274     d->access->error("Unaligned write: pos=%02x,len=%d", pos, len);
275   if (pos + len <= d->cache_len)
276     memcpy(d->cache + pos, buf, len);
277   return d->methods->write(d, pos, buf, len);
278 }
279
280 int
281 pci_write_byte(struct pci_dev *d, int pos, byte data)
282 {
283   return pci_write_data(d, &data, pos, 1);
284 }
285
286 int
287 pci_write_word(struct pci_dev *d, int pos, word data)
288 {
289   word buf = cpu_to_le16(data);
290   return pci_write_data(d, &buf, pos, 2);
291 }
292
293 int
294 pci_write_long(struct pci_dev *d, int pos, u32 data)
295 {
296   u32 buf = cpu_to_le32(data);
297   return pci_write_data(d, &buf, pos, 4);
298 }
299
300 int
301 pci_write_block(struct pci_dev *d, int pos, byte *buf, int len)
302 {
303   if (pos < d->cache_len)
304     {
305       int l = (pos + len >= d->cache_len) ? (d->cache_len - pos) : len;
306       memcpy(d->cache + pos, buf, l);
307     }
308   return d->methods->write(d, pos, buf, len);
309 }
310
311 int
312 pci_fill_info(struct pci_dev *d, int flags)
313 {
314   if (flags & PCI_FILL_RESCAN)
315     {
316       flags &= ~PCI_FILL_RESCAN;
317       d->known_fields = 0;
318     }
319   if (flags & ~d->known_fields)
320     d->known_fields |= d->methods->fill_info(d, flags & ~d->known_fields);
321   return d->known_fields;
322 }
323
324 void
325 pci_setup_cache(struct pci_dev *d, byte *cache, int len)
326 {
327   d->cache = cache;
328   d->cache_len = len;
329 }