2 * The PCI Library -- User Access
4 * Copyright (c) 1997--2003 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
16 static struct pci_methods *pci_methods[PCI_ACCESS_MAX] = {
18 #ifdef HAVE_PM_LINUX_SYSFS
23 #ifdef HAVE_PM_LINUX_PROC
28 #ifdef HAVE_PM_INTEL_CONF
35 #ifdef HAVE_PM_FBSD_DEVICE
40 #ifdef HAVE_PM_AIX_DEVICE
45 #ifdef HAVE_PM_NBSD_LIBPCI
60 struct pci_access *a = malloc(sizeof(struct pci_access));
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);
72 pci_malloc(struct pci_access *a, int size)
74 void *x = malloc(size);
77 a->error("Out of memory (allocation of %d bytes failed)", size);
89 pci_generic_error(char *msg, ...)
94 fputs("pcilib: ", stderr);
95 vfprintf(stderr, msg, args);
101 pci_generic_warn(char *msg, ...)
106 fputs("pcilib: ", stderr);
107 vfprintf(stderr, msg, args);
112 pci_generic_debug(char *msg, ...)
117 vfprintf(stdout, msg, args);
122 pci_null_debug(char *msg UNUSED, ...)
127 pci_init(struct pci_access *a)
130 a->error = pci_generic_error;
132 a->warning = pci_generic_warn;
134 a->debug = pci_generic_debug;
136 a->debug = pci_null_debug;
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];
147 for(i=0; i<PCI_ACCESS_MAX; i++)
150 a->debug("Trying method %d...", i);
151 if (pci_methods[i]->detect(a))
154 a->methods = pci_methods[i];
158 a->debug("...No.\n");
161 a->error("Cannot find any working access method.");
163 a->debug("Decided to use %s\n", a->methods->name);
168 pci_cleanup(struct pci_access *a)
170 struct pci_dev *d, *e;
172 for(d=a->devices; d; d=e)
178 a->methods->cleanup(a);
179 pci_free_name_list(a);
184 pci_scan_bus(struct pci_access *a)
190 pci_alloc_dev(struct pci_access *a)
192 struct pci_dev *d = pci_malloc(a, sizeof(struct pci_dev));
194 bzero(d, sizeof(*d));
196 d->methods = a->methods;
197 if (d->methods->init_dev)
198 d->methods->init_dev(d);
203 pci_link_dev(struct pci_access *a, struct pci_dev *d)
205 d->next = a->devices;
212 pci_get_dev(struct pci_access *a, int bus, int dev, int func)
214 struct pci_dev *d = pci_alloc_dev(a);
222 void pci_free_dev(struct pci_dev *d)
224 if (d->methods->cleanup_dev)
225 d->methods->cleanup_dev(d);
230 pci_read_data(struct pci_dev *d, void *buf, int pos, int len)
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);
241 pci_read_byte(struct pci_dev *d, int pos)
244 pci_read_data(d, &buf, pos, 1);
249 pci_read_word(struct pci_dev *d, int pos)
252 pci_read_data(d, &buf, pos, 2);
253 return le16_to_cpu(buf);
257 pci_read_long(struct pci_dev *d, int pos)
260 pci_read_data(d, &buf, pos, 4);
261 return le32_to_cpu(buf);
265 pci_read_block(struct pci_dev *d, int pos, byte *buf, int len)
267 return d->methods->read(d, pos, buf, len);
271 pci_write_data(struct pci_dev *d, void *buf, int pos, int len)
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);
281 pci_write_byte(struct pci_dev *d, int pos, byte data)
283 return pci_write_data(d, &data, pos, 1);
287 pci_write_word(struct pci_dev *d, int pos, word data)
289 word buf = cpu_to_le16(data);
290 return pci_write_data(d, &buf, pos, 2);
294 pci_write_long(struct pci_dev *d, int pos, u32 data)
296 u32 buf = cpu_to_le32(data);
297 return pci_write_data(d, &buf, pos, 4);
301 pci_write_block(struct pci_dev *d, int pos, byte *buf, int len)
303 if (pos < d->cache_len)
305 int l = (pos + len >= d->cache_len) ? (d->cache_len - pos) : len;
306 memcpy(d->cache + pos, buf, l);
308 return d->methods->write(d, pos, buf, len);
312 pci_fill_info(struct pci_dev *d, int flags)
314 if (flags & PCI_FILL_RESCAN)
316 flags &= ~PCI_FILL_RESCAN;
319 if (flags & ~d->known_fields)
320 d->known_fields |= d->methods->fill_info(d, flags & ~d->known_fields);
321 return d->known_fields;
325 pci_setup_cache(struct pci_dev *d, byte *cache, int len)