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));
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);
79 pci_malloc(struct pci_access *a, int size)
81 void *x = malloc(size);
84 a->error("Out of memory (allocation of %d bytes failed)", size);
96 pci_generic_error(char *msg, ...)
101 fputs("pcilib: ", stderr);
102 vfprintf(stderr, msg, args);
108 pci_generic_warn(char *msg, ...)
113 fputs("pcilib: ", stderr);
114 vfprintf(stderr, msg, args);
119 pci_generic_debug(char *msg, ...)
124 vfprintf(stdout, msg, args);
129 pci_null_debug(char *msg UNUSED, ...)
134 pci_init(struct pci_access *a)
137 a->error = pci_generic_error;
139 a->warning = pci_generic_warn;
141 a->debug = pci_generic_debug;
143 a->debug = pci_null_debug;
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];
154 for(i=0; i<PCI_ACCESS_MAX; i++)
157 a->debug("Trying method %d...", i);
158 if (pci_methods[i]->detect(a))
161 a->methods = pci_methods[i];
165 a->debug("...No.\n");
168 a->error("Cannot find any working access method.");
170 a->debug("Decided to use %s\n", a->methods->name);
175 pci_cleanup(struct pci_access *a)
177 struct pci_dev *d, *e;
179 for(d=a->devices; d; d=e)
185 a->methods->cleanup(a);
186 pci_free_name_list(a);
187 pci_set_name_list_path(a, NULL, 0);
192 pci_scan_bus(struct pci_access *a)
198 pci_alloc_dev(struct pci_access *a)
200 struct pci_dev *d = pci_malloc(a, sizeof(struct pci_dev));
202 memset(d, 0, sizeof(*d));
204 d->methods = a->methods;
206 if (d->methods->init_dev)
207 d->methods->init_dev(d);
212 pci_link_dev(struct pci_access *a, struct pci_dev *d)
214 d->next = a->devices;
221 pci_get_dev(struct pci_access *a, int domain, int bus, int dev, int func)
223 struct pci_dev *d = pci_alloc_dev(a);
232 void pci_free_dev(struct pci_dev *d)
234 if (d->methods->cleanup_dev)
235 d->methods->cleanup_dev(d);
240 pci_read_data(struct pci_dev *d, void *buf, int pos, int len)
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);
251 pci_read_byte(struct pci_dev *d, int pos)
254 pci_read_data(d, &buf, pos, 1);
259 pci_read_word(struct pci_dev *d, int pos)
262 pci_read_data(d, &buf, pos, 2);
263 return le16_to_cpu(buf);
267 pci_read_long(struct pci_dev *d, int pos)
270 pci_read_data(d, &buf, pos, 4);
271 return le32_to_cpu(buf);
275 pci_read_block(struct pci_dev *d, int pos, byte *buf, int len)
277 return d->methods->read(d, pos, buf, len);
281 pci_write_data(struct pci_dev *d, void *buf, int pos, int len)
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);
291 pci_write_byte(struct pci_dev *d, int pos, byte data)
293 return pci_write_data(d, &data, pos, 1);
297 pci_write_word(struct pci_dev *d, int pos, word data)
299 word buf = cpu_to_le16(data);
300 return pci_write_data(d, &buf, pos, 2);
304 pci_write_long(struct pci_dev *d, int pos, u32 data)
306 u32 buf = cpu_to_le32(data);
307 return pci_write_data(d, &buf, pos, 4);
311 pci_write_block(struct pci_dev *d, int pos, byte *buf, int len)
313 if (pos < d->cache_len)
315 int l = (pos + len >= d->cache_len) ? (d->cache_len - pos) : len;
316 memcpy(d->cache + pos, buf, l);
318 return d->methods->write(d, pos, buf, len);
322 pci_fill_info(struct pci_dev *d, int flags)
324 if (flags & PCI_FILL_RESCAN)
326 flags &= ~PCI_FILL_RESCAN;
329 if (flags & ~d->known_fields)
330 d->known_fields |= d->methods->fill_info(d, flags & ~d->known_fields);
331 return d->known_fields;
335 pci_setup_cache(struct pci_dev *d, byte *cache, int len)