2 * $Id: access.c,v 1.8 2002/03/30 15:39:25 mj Exp $
4 * The PCI Library -- User Access
6 * Copyright (c) 1997--1999 Martin Mares <mj@ucw.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_INTEL_CONF
37 #ifdef HAVE_PM_FBSD_DEVICE
42 #ifdef HAVE_PM_AIX_DEVICE
57 struct pci_access *a = malloc(sizeof(struct pci_access));
61 a->id_file_name = PATH_PCI_IDS;
62 for(i=0; i<PCI_ACCESS_MAX; i++)
63 if (pci_methods[i] && pci_methods[i]->config)
64 pci_methods[i]->config(a);
69 pci_malloc(struct pci_access *a, int size)
71 void *x = malloc(size);
74 a->error("Out of memory (allocation of %d bytes failed)", size);
86 pci_generic_error(char *msg, ...)
91 fputs("pcilib: ", stderr);
92 vfprintf(stderr, msg, args);
98 pci_generic_warn(char *msg, ...)
103 fputs("pcilib: ", stderr);
104 vfprintf(stderr, msg, args);
109 pci_generic_debug(char *msg, ...)
114 vfprintf(stdout, msg, args);
119 pci_null_debug(char * UNUSED msg, ...)
124 pci_init(struct pci_access *a)
127 a->error = pci_generic_error;
129 a->warning = pci_generic_warn;
131 a->debug = pci_generic_debug;
133 a->debug = pci_null_debug;
137 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
138 a->error("This access method is not supported.");
139 a->methods = pci_methods[a->method];
144 for(i=0; i<PCI_ACCESS_MAX; i++)
147 a->debug("Trying method %d...", i);
148 if (pci_methods[i]->detect(a))
151 a->methods = pci_methods[i];
155 a->debug("...No.\n");
158 a->error("Cannot find any working access method.");
160 a->debug("Decided to use %s\n", a->methods->name);
165 pci_cleanup(struct pci_access *a)
167 struct pci_dev *d, *e;
169 for(d=a->devices; d; d=e)
175 a->methods->cleanup(a);
176 pci_free_name_list(a);
181 pci_scan_bus(struct pci_access *a)
187 pci_alloc_dev(struct pci_access *a)
189 struct pci_dev *d = pci_malloc(a, sizeof(struct pci_dev));
191 bzero(d, sizeof(*d));
193 d->methods = a->methods;
194 if (d->methods->init_dev)
195 d->methods->init_dev(d);
200 pci_link_dev(struct pci_access *a, struct pci_dev *d)
202 d->next = a->devices;
209 pci_get_dev(struct pci_access *a, int bus, int dev, int func)
211 struct pci_dev *d = pci_alloc_dev(a);
219 void pci_free_dev(struct pci_dev *d)
221 if (d->methods->cleanup_dev)
222 d->methods->cleanup_dev(d);
227 pci_read_data(struct pci_dev *d, void *buf, int pos, int len)
230 d->access->error("Unaligned read: pos=%02x, len=%d", pos, len);
231 if (pos + len <= d->cache_len)
232 memcpy(buf, d->cache + pos, len);
233 else if (!d->methods->read(d, pos, buf, len))
234 memset(buf, 0xff, len);
238 pci_read_byte(struct pci_dev *d, int pos)
241 pci_read_data(d, &buf, pos, 1);
246 pci_read_word(struct pci_dev *d, int pos)
249 pci_read_data(d, &buf, pos, 2);
250 return le16_to_cpu(buf);
254 pci_read_long(struct pci_dev *d, int pos)
257 pci_read_data(d, &buf, pos, 4);
258 return le32_to_cpu(buf);
262 pci_read_block(struct pci_dev *d, int pos, byte *buf, int len)
264 return d->methods->read(d, pos, buf, len);
268 pci_write_data(struct pci_dev *d, void *buf, int pos, int len)
271 d->access->error("Unaligned write: pos=%02x,len=%d", pos, len);
272 if (pos + len <= d->cache_len)
273 memcpy(d->cache + pos, buf, len);
274 return d->methods->write(d, pos, buf, len);
278 pci_write_byte(struct pci_dev *d, int pos, byte data)
280 return pci_write_data(d, &data, pos, 1);
284 pci_write_word(struct pci_dev *d, int pos, word data)
286 word buf = cpu_to_le16(data);
287 return pci_write_data(d, &buf, pos, 2);
291 pci_write_long(struct pci_dev *d, int pos, u32 data)
293 u32 buf = cpu_to_le32(data);
294 return pci_write_data(d, &buf, pos, 4);
298 pci_write_block(struct pci_dev *d, int pos, byte *buf, int len)
300 if (pos < d->cache_len)
302 int l = (pos + len >= d->cache_len) ? (d->cache_len - pos) : len;
303 memcpy(d->cache + pos, buf, l);
305 return d->methods->write(d, pos, buf, len);
309 pci_fill_info(struct pci_dev *d, int flags)
311 if (flags & PCI_FILL_RESCAN)
313 flags &= ~PCI_FILL_RESCAN;
316 if (flags & ~d->known_fields)
317 d->known_fields |= d->methods->fill_info(d, flags & ~d->known_fields);
318 return d->known_fields;
322 pci_setup_cache(struct pci_dev *d, byte *cache, int len)