2 * The PCI Library -- User Access
4 * Copyright (c) 1997--1999 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_PROC
23 #ifdef HAVE_PM_INTEL_CONF
30 #ifdef HAVE_PM_FBSD_DEVICE
35 #ifdef HAVE_PM_AIX_DEVICE
40 #ifdef HAVE_PM_NBSD_LIBPCI
55 struct pci_access *a = malloc(sizeof(struct pci_access));
59 a->id_file_name = PATH_PCI_IDS;
60 for(i=0; i<PCI_ACCESS_MAX; i++)
61 if (pci_methods[i] && pci_methods[i]->config)
62 pci_methods[i]->config(a);
67 pci_malloc(struct pci_access *a, int size)
69 void *x = malloc(size);
72 a->error("Out of memory (allocation of %d bytes failed)", size);
84 pci_generic_error(char *msg, ...)
89 fputs("pcilib: ", stderr);
90 vfprintf(stderr, msg, args);
96 pci_generic_warn(char *msg, ...)
101 fputs("pcilib: ", stderr);
102 vfprintf(stderr, msg, args);
107 pci_generic_debug(char *msg, ...)
112 vfprintf(stdout, msg, args);
117 pci_null_debug(char * UNUSED msg, ...)
122 pci_init(struct pci_access *a)
125 a->error = pci_generic_error;
127 a->warning = pci_generic_warn;
129 a->debug = pci_generic_debug;
131 a->debug = pci_null_debug;
135 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
136 a->error("This access method is not supported.");
137 a->methods = pci_methods[a->method];
142 for(i=0; i<PCI_ACCESS_MAX; i++)
145 a->debug("Trying method %d...", i);
146 if (pci_methods[i]->detect(a))
149 a->methods = pci_methods[i];
153 a->debug("...No.\n");
156 a->error("Cannot find any working access method.");
158 a->debug("Decided to use %s\n", a->methods->name);
163 pci_cleanup(struct pci_access *a)
165 struct pci_dev *d, *e;
167 for(d=a->devices; d; d=e)
173 a->methods->cleanup(a);
174 pci_free_name_list(a);
179 pci_scan_bus(struct pci_access *a)
185 pci_alloc_dev(struct pci_access *a)
187 struct pci_dev *d = pci_malloc(a, sizeof(struct pci_dev));
189 bzero(d, sizeof(*d));
191 d->methods = a->methods;
192 if (d->methods->init_dev)
193 d->methods->init_dev(d);
198 pci_link_dev(struct pci_access *a, struct pci_dev *d)
200 d->next = a->devices;
207 pci_get_dev(struct pci_access *a, int bus, int dev, int func)
209 struct pci_dev *d = pci_alloc_dev(a);
217 void pci_free_dev(struct pci_dev *d)
219 if (d->methods->cleanup_dev)
220 d->methods->cleanup_dev(d);
225 pci_read_data(struct pci_dev *d, void *buf, int pos, int len)
228 d->access->error("Unaligned read: pos=%02x, len=%d", pos, len);
229 if (pos + len <= d->cache_len)
230 memcpy(buf, d->cache + pos, len);
231 else if (!d->methods->read(d, pos, buf, len))
232 memset(buf, 0xff, len);
236 pci_read_byte(struct pci_dev *d, int pos)
239 pci_read_data(d, &buf, pos, 1);
244 pci_read_word(struct pci_dev *d, int pos)
247 pci_read_data(d, &buf, pos, 2);
248 return le16_to_cpu(buf);
252 pci_read_long(struct pci_dev *d, int pos)
255 pci_read_data(d, &buf, pos, 4);
256 return le32_to_cpu(buf);
260 pci_read_block(struct pci_dev *d, int pos, byte *buf, int len)
262 return d->methods->read(d, pos, buf, len);
266 pci_write_data(struct pci_dev *d, void *buf, int pos, int len)
269 d->access->error("Unaligned write: pos=%02x,len=%d", pos, len);
270 if (pos + len <= d->cache_len)
271 memcpy(d->cache + pos, buf, len);
272 return d->methods->write(d, pos, buf, len);
276 pci_write_byte(struct pci_dev *d, int pos, byte data)
278 return pci_write_data(d, &data, pos, 1);
282 pci_write_word(struct pci_dev *d, int pos, word data)
284 word buf = cpu_to_le16(data);
285 return pci_write_data(d, &buf, pos, 2);
289 pci_write_long(struct pci_dev *d, int pos, u32 data)
291 u32 buf = cpu_to_le32(data);
292 return pci_write_data(d, &buf, pos, 4);
296 pci_write_block(struct pci_dev *d, int pos, byte *buf, int len)
298 if (pos < d->cache_len)
300 int l = (pos + len >= d->cache_len) ? (d->cache_len - pos) : len;
301 memcpy(d->cache + pos, buf, l);
303 return d->methods->write(d, pos, buf, len);
307 pci_fill_info(struct pci_dev *d, int flags)
309 if (flags & PCI_FILL_RESCAN)
311 flags &= ~PCI_FILL_RESCAN;
314 if (flags & ~d->known_fields)
315 d->known_fields |= d->methods->fill_info(d, flags & ~d->known_fields);
316 return d->known_fields;
320 pci_setup_cache(struct pci_dev *d, byte *cache, int len)