2 * $Id: access.c,v 1.5 1999/07/20 14:01:28 mj Exp $
4 * The PCI Library -- User Access
6 * Copyright (c) 1997--1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
8 * Can be freely distributed and used under the terms of the GNU GPL.
18 static struct pci_methods *pci_methods[PCI_ACCESS_MAX] = {
20 #ifdef HAVE_PM_LINUX_PROC
25 #ifdef HAVE_PM_SYSCALLS
30 #ifdef HAVE_PM_FBSD_DEVICE
35 #ifdef HAVE_PM_INTEL_CONF
52 struct pci_access *a = malloc(sizeof(struct pci_access));
56 a->id_file_name = PATH_PCI_IDS;
57 for(i=0; i<PCI_ACCESS_MAX; i++)
58 if (pci_methods[i] && pci_methods[i]->config)
59 pci_methods[i]->config(a);
64 pci_malloc(struct pci_access *a, int size)
66 void *x = malloc(size);
69 a->error("Out of memory (allocation of %d bytes failed)", size);
81 pci_generic_error(char *msg, ...)
86 fputs("pcilib: ", stderr);
87 vfprintf(stderr, msg, args);
93 pci_generic_warn(char *msg, ...)
98 fputs("pcilib: ", stderr);
99 vfprintf(stderr, msg, args);
104 pci_generic_debug(char *msg, ...)
109 vfprintf(stdout, msg, args);
114 pci_null_debug(char * UNUSED msg, ...)
119 pci_init(struct pci_access *a)
122 a->error = pci_generic_error;
124 a->warning = pci_generic_warn;
126 a->debug = pci_generic_debug;
128 a->debug = pci_null_debug;
132 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
133 a->error("This access method is not supported.");
134 a->methods = pci_methods[a->method];
139 for(i=0; i<PCI_ACCESS_MAX; i++)
142 a->debug("Trying method %d...", i);
143 if (pci_methods[i]->detect(a))
146 a->methods = pci_methods[i];
150 a->debug("...No.\n");
153 a->error("Cannot find any working access method.");
155 a->debug("Decided to use %s\n", a->methods->name);
160 pci_cleanup(struct pci_access *a)
162 struct pci_dev *d, *e;
164 for(d=a->devices; d; d=e)
170 a->methods->cleanup(a);
171 pci_free_name_list(a);
176 pci_scan_bus(struct pci_access *a)
182 pci_alloc_dev(struct pci_access *a)
184 struct pci_dev *d = pci_malloc(a, sizeof(struct pci_dev));
186 bzero(d, sizeof(*d));
188 d->methods = a->methods;
189 if (d->methods->init_dev)
190 d->methods->init_dev(d);
195 pci_link_dev(struct pci_access *a, struct pci_dev *d)
197 d->next = a->devices;
204 pci_get_dev(struct pci_access *a, int bus, int dev, int func)
206 struct pci_dev *d = pci_alloc_dev(a);
214 void pci_free_dev(struct pci_dev *d)
216 if (d->methods->cleanup_dev)
217 d->methods->cleanup_dev(d);
222 pci_read_data(struct pci_dev *d, void *buf, int pos, int len)
225 d->access->error("Unaligned read: pos=%02x, len=%d", pos, len);
226 if (pos + len <= d->cache_len)
227 memcpy(buf, d->cache + pos, len);
228 else if (!d->methods->read(d, pos, buf, len))
229 memset(buf, 0xff, len);
233 pci_read_byte(struct pci_dev *d, int pos)
236 pci_read_data(d, &buf, pos, 1);
241 pci_read_word(struct pci_dev *d, int pos)
244 pci_read_data(d, &buf, pos, 2);
245 return le16_to_cpu(buf);
249 pci_read_long(struct pci_dev *d, int pos)
252 pci_read_data(d, &buf, pos, 4);
253 return le32_to_cpu(buf);
257 pci_read_block(struct pci_dev *d, int pos, byte *buf, int len)
259 return d->methods->read(d, pos, buf, len);
263 pci_write_data(struct pci_dev *d, void *buf, int pos, int len)
266 d->access->error("Unaligned write: pos=%02x,len=%d", pos, len);
267 if (pos + len <= d->cache_len)
268 memcpy(d->cache + pos, buf, len);
269 return d->methods->write(d, pos, buf, len);
273 pci_write_byte(struct pci_dev *d, int pos, byte data)
275 return pci_write_data(d, &data, pos, 1);
279 pci_write_word(struct pci_dev *d, int pos, word data)
281 word buf = cpu_to_le16(data);
282 return pci_write_data(d, &buf, pos, 2);
286 pci_write_long(struct pci_dev *d, int pos, u32 data)
288 u32 buf = cpu_to_le32(data);
289 return pci_write_data(d, &buf, pos, 4);
293 pci_write_block(struct pci_dev *d, int pos, byte *buf, int len)
295 if (pos < d->cache_len)
297 int l = (pos + len >= d->cache_len) ? (d->cache_len - pos) : len;
298 memcpy(d->cache + pos, buf, l);
300 return d->methods->write(d, pos, buf, len);
304 pci_fill_info(struct pci_dev *d, int flags)
306 if (flags & PCI_FILL_RESCAN)
308 flags &= ~PCI_FILL_RESCAN;
311 if (flags & ~d->known_fields)
312 d->known_fields |= d->methods->fill_info(d, flags & ~d->known_fields);
313 return d->known_fields;
317 pci_setup_cache(struct pci_dev *d, byte *cache, int len)