2 * $Id: access.c,v 1.1 1999/01/22 21:05:12 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_INTEL_CONF
47 struct pci_access *a = malloc(sizeof(struct pci_access));
51 a->id_file_name = PATH_PCI_IDS;
52 for(i=0; i<PCI_ACCESS_MAX; i++)
53 if (pci_methods[i] && pci_methods[i]->config)
54 pci_methods[i]->config(a);
59 pci_malloc(struct pci_access *a, int size)
61 void *x = malloc(size);
64 a->error("Out of memory (allocation of %d bytes failed)", size);
76 pci_generic_error(char *msg, ...)
81 fputs("pcilib: ", stderr);
82 vfprintf(stderr, msg, args);
88 pci_generic_warn(char *msg, ...)
93 fputs("pcilib: ", stderr);
94 vfprintf(stderr, msg, args);
99 pci_generic_debug(char *msg, ...)
104 vfprintf(stdout, msg, args);
109 pci_null_debug(char * UNUSED msg, ...)
114 pci_init(struct pci_access *a)
117 a->error = pci_generic_error;
119 a->warning = pci_generic_warn;
121 a->debug = pci_generic_debug;
123 a->debug = pci_null_debug;
127 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
128 a->error("This access method is not supported.");
129 a->methods = pci_methods[a->method];
134 for(i=0; i<PCI_ACCESS_MAX; i++)
137 a->debug("Trying method %d...", i);
138 if (pci_methods[i]->detect(a))
141 a->methods = pci_methods[i];
144 a->debug("...No.\n");
147 a->error("Cannot find any working access method.");
149 a->debug("Decided to use %s\n", a->methods->name);
154 pci_cleanup(struct pci_access *a)
156 struct pci_dev *d, *e;
158 for(d=a->devices; d; d=e)
164 a->methods->cleanup(a);
165 pci_free_name_list(a);
170 pci_scan_bus(struct pci_access *a)
176 pci_alloc_dev(struct pci_access *a)
178 struct pci_dev *d = pci_malloc(a, sizeof(struct pci_dev));
180 bzero(d, sizeof(*d));
182 d->methods = a->methods;
183 if (d->methods->init_dev)
184 d->methods->init_dev(d);
189 pci_link_dev(struct pci_access *a, struct pci_dev *d)
191 d->next = a->devices;
198 pci_get_dev(struct pci_access *a, int bus, int dev, int func)
200 struct pci_dev *d = pci_alloc_dev(a);
208 void pci_free_dev(struct pci_dev *d)
210 if (d->methods->cleanup_dev)
211 d->methods->cleanup_dev(d);
216 pci_read_data(struct pci_dev *d, void *buf, int pos, int len)
219 d->access->error("Unaligned read: pos=%02x, len=%d", pos, len);
220 if (!d->methods->read(d, pos, buf, len))
221 memset(buf, 0xff, len);
225 pci_read_byte(struct pci_dev *d, int pos)
228 pci_read_data(d, &buf, pos, 1);
233 pci_read_word(struct pci_dev *d, int pos)
236 pci_read_data(d, &buf, pos, 2);
237 return le16_to_cpu(buf);
241 pci_read_long(struct pci_dev *d, int pos)
244 pci_read_data(d, &buf, pos, 4);
245 return le32_to_cpu(buf);
249 pci_read_block(struct pci_dev *d, int pos, byte *buf, int len)
251 return d->methods->read(d, pos, buf, len);
255 pci_write_data(struct pci_dev *d, void *buf, int pos, int len)
258 d->access->error("Unaligned write: pos=%02x,len=%d", pos, len);
259 return d->methods->write(d, pos, buf, len);
263 pci_write_byte(struct pci_dev *d, int pos, byte data)
265 return pci_write_data(d, &data, pos, 1);
269 pci_write_word(struct pci_dev *d, int pos, word data)
271 word buf = cpu_to_le16(data);
272 return pci_write_data(d, &buf, pos, 2);
276 pci_write_long(struct pci_dev *d, int pos, u32 data)
278 u32 buf = cpu_to_le32(data);
279 return pci_write_data(d, &buf, pos, 4);
283 pci_write_block(struct pci_dev *d, int pos, byte *buf, int len)
285 return d->methods->write(d, pos, buf, len);
289 pci_fill_info(struct pci_dev *d, int flags)
291 if (flags & PCI_FILL_RESCAN)
293 flags &= ~PCI_FILL_RESCAN;
296 if (flags & ~d->known_fields)
297 d->methods->fill_info(d, flags & ~d->known_fields);
298 d->known_fields |= flags;