2 * The PCI Library -- User Access
4 * Copyright (c) 1997--2008 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 pci_set_net_domain(a, PCI_ID_DOMAIN, 0);
71 a->id_lookup_mode = PCI_LOOKUP_CACHE;
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);
188 pci_set_net_domain(a, NULL, 0);
189 pci_set_id_cache(a, NULL, 0);
194 pci_scan_bus(struct pci_access *a)
200 pci_alloc_dev(struct pci_access *a)
202 struct pci_dev *d = pci_malloc(a, sizeof(struct pci_dev));
204 memset(d, 0, sizeof(*d));
206 d->methods = a->methods;
208 if (d->methods->init_dev)
209 d->methods->init_dev(d);
214 pci_link_dev(struct pci_access *a, struct pci_dev *d)
216 d->next = a->devices;
223 pci_get_dev(struct pci_access *a, int domain, int bus, int dev, int func)
225 struct pci_dev *d = pci_alloc_dev(a);
234 void pci_free_dev(struct pci_dev *d)
236 if (d->methods->cleanup_dev)
237 d->methods->cleanup_dev(d);
242 pci_read_data(struct pci_dev *d, void *buf, int pos, int len)
245 d->access->error("Unaligned read: pos=%02x, len=%d", pos, len);
246 if (pos + len <= d->cache_len)
247 memcpy(buf, d->cache + pos, len);
248 else if (!d->methods->read(d, pos, buf, len))
249 memset(buf, 0xff, len);
253 pci_read_byte(struct pci_dev *d, int pos)
256 pci_read_data(d, &buf, pos, 1);
261 pci_read_word(struct pci_dev *d, int pos)
264 pci_read_data(d, &buf, pos, 2);
265 return le16_to_cpu(buf);
269 pci_read_long(struct pci_dev *d, int pos)
272 pci_read_data(d, &buf, pos, 4);
273 return le32_to_cpu(buf);
277 pci_read_block(struct pci_dev *d, int pos, byte *buf, int len)
279 return d->methods->read(d, pos, buf, len);
283 pci_write_data(struct pci_dev *d, void *buf, int pos, int len)
286 d->access->error("Unaligned write: pos=%02x,len=%d", pos, len);
287 if (pos + len <= d->cache_len)
288 memcpy(d->cache + pos, buf, len);
289 return d->methods->write(d, pos, buf, len);
293 pci_write_byte(struct pci_dev *d, int pos, byte data)
295 return pci_write_data(d, &data, pos, 1);
299 pci_write_word(struct pci_dev *d, int pos, word data)
301 word buf = cpu_to_le16(data);
302 return pci_write_data(d, &buf, pos, 2);
306 pci_write_long(struct pci_dev *d, int pos, u32 data)
308 u32 buf = cpu_to_le32(data);
309 return pci_write_data(d, &buf, pos, 4);
313 pci_write_block(struct pci_dev *d, int pos, byte *buf, int len)
315 if (pos < d->cache_len)
317 int l = (pos + len >= d->cache_len) ? (d->cache_len - pos) : len;
318 memcpy(d->cache + pos, buf, l);
320 return d->methods->write(d, pos, buf, len);
324 pci_fill_info(struct pci_dev *d, int flags)
326 if (flags & PCI_FILL_RESCAN)
328 flags &= ~PCI_FILL_RESCAN;
331 if (flags & ~d->known_fields)
332 d->known_fields |= d->methods->fill_info(d, flags & ~d->known_fields);
333 return d->known_fields;
337 pci_setup_cache(struct pci_dev *d, byte *cache, int len)