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 PCI_HAVE_PM_LINUX_SYSFS
23 #ifdef PCI_HAVE_PM_LINUX_PROC
28 #ifdef PCI_HAVE_PM_INTEL_CONF
35 #ifdef PCI_HAVE_PM_FBSD_DEVICE
40 #ifdef PCI_HAVE_PM_AIX_DEVICE
45 #ifdef PCI_HAVE_PM_NBSD_LIBPCI
50 #ifdef PCI_HAVE_PM_OBSD_DEVICE
55 #ifdef PCI_HAVE_PM_DUMP
65 struct pci_access *a = malloc(sizeof(struct pci_access));
69 a->id_file_name = PCI_PATH_IDS;
70 for(i=0; i<PCI_ACCESS_MAX; i++)
71 if (pci_methods[i] && pci_methods[i]->config)
72 pci_methods[i]->config(a);
77 pci_malloc(struct pci_access *a, int size)
79 void *x = malloc(size);
82 a->error("Out of memory (allocation of %d bytes failed)", size);
94 pci_generic_error(char *msg, ...)
99 fputs("pcilib: ", stderr);
100 vfprintf(stderr, msg, args);
106 pci_generic_warn(char *msg, ...)
111 fputs("pcilib: ", stderr);
112 vfprintf(stderr, msg, args);
117 pci_generic_debug(char *msg, ...)
122 vfprintf(stdout, msg, args);
127 pci_null_debug(char *msg UNUSED, ...)
132 pci_init(struct pci_access *a)
135 a->error = pci_generic_error;
137 a->warning = pci_generic_warn;
139 a->debug = pci_generic_debug;
141 a->debug = pci_null_debug;
145 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
146 a->error("This access method is not supported.");
147 a->methods = pci_methods[a->method];
152 for(i=0; i<PCI_ACCESS_MAX; i++)
155 a->debug("Trying method %d...", i);
156 if (pci_methods[i]->detect(a))
159 a->methods = pci_methods[i];
163 a->debug("...No.\n");
166 a->error("Cannot find any working access method.");
168 a->debug("Decided to use %s\n", a->methods->name);
173 pci_cleanup(struct pci_access *a)
175 struct pci_dev *d, *e;
177 for(d=a->devices; d; d=e)
183 a->methods->cleanup(a);
184 pci_free_name_list(a);
189 pci_scan_bus(struct pci_access *a)
195 pci_alloc_dev(struct pci_access *a)
197 struct pci_dev *d = pci_malloc(a, sizeof(struct pci_dev));
199 bzero(d, sizeof(*d));
201 d->methods = a->methods;
203 if (d->methods->init_dev)
204 d->methods->init_dev(d);
209 pci_link_dev(struct pci_access *a, struct pci_dev *d)
211 d->next = a->devices;
218 pci_get_dev(struct pci_access *a, int domain, int bus, int dev, int func)
220 struct pci_dev *d = pci_alloc_dev(a);
229 void pci_free_dev(struct pci_dev *d)
231 if (d->methods->cleanup_dev)
232 d->methods->cleanup_dev(d);
237 pci_read_data(struct pci_dev *d, void *buf, int pos, int len)
240 d->access->error("Unaligned read: pos=%02x, len=%d", pos, len);
241 if (pos + len <= d->cache_len)
242 memcpy(buf, d->cache + pos, len);
243 else if (!d->methods->read(d, pos, buf, len))
244 memset(buf, 0xff, len);
248 pci_read_byte(struct pci_dev *d, int pos)
251 pci_read_data(d, &buf, pos, 1);
256 pci_read_word(struct pci_dev *d, int pos)
259 pci_read_data(d, &buf, pos, 2);
260 return le16_to_cpu(buf);
264 pci_read_long(struct pci_dev *d, int pos)
267 pci_read_data(d, &buf, pos, 4);
268 return le32_to_cpu(buf);
272 pci_read_block(struct pci_dev *d, int pos, byte *buf, int len)
274 return d->methods->read(d, pos, buf, len);
278 pci_write_data(struct pci_dev *d, void *buf, int pos, int len)
281 d->access->error("Unaligned write: pos=%02x,len=%d", pos, len);
282 if (pos + len <= d->cache_len)
283 memcpy(d->cache + pos, buf, len);
284 return d->methods->write(d, pos, buf, len);
288 pci_write_byte(struct pci_dev *d, int pos, byte data)
290 return pci_write_data(d, &data, pos, 1);
294 pci_write_word(struct pci_dev *d, int pos, word data)
296 word buf = cpu_to_le16(data);
297 return pci_write_data(d, &buf, pos, 2);
301 pci_write_long(struct pci_dev *d, int pos, u32 data)
303 u32 buf = cpu_to_le32(data);
304 return pci_write_data(d, &buf, pos, 4);
308 pci_write_block(struct pci_dev *d, int pos, byte *buf, int len)
310 if (pos < d->cache_len)
312 int l = (pos + len >= d->cache_len) ? (d->cache_len - pos) : len;
313 memcpy(d->cache + pos, buf, l);
315 return d->methods->write(d, pos, buf, len);
319 pci_fill_info(struct pci_dev *d, int flags)
321 if (flags & PCI_FILL_RESCAN)
323 flags &= ~PCI_FILL_RESCAN;
326 if (flags & ~d->known_fields)
327 d->known_fields |= d->methods->fill_info(d, flags & ~d->known_fields);
328 return d->known_fields;
332 pci_setup_cache(struct pci_dev *d, byte *cache, int len)