2 * The PCI Library -- Initialization and related things
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
63 pci_malloc(struct pci_access *a, int size)
65 void *x = malloc(size);
68 a->error("Out of memory (allocation of %d bytes failed)", size);
80 pci_strdup(struct pci_access *a, char *s)
82 int len = strlen(s) + 1;
83 char *t = pci_malloc(a, len);
89 pci_generic_error(char *msg, ...)
94 fputs("pcilib: ", stderr);
95 vfprintf(stderr, msg, args);
101 pci_generic_warn(char *msg, ...)
106 fputs("pcilib: ", stderr);
107 vfprintf(stderr, msg, args);
112 pci_generic_debug(char *msg, ...)
117 vfprintf(stdout, msg, args);
122 pci_null_debug(char *msg UNUSED, ...)
127 pci_lookup_method(char *name)
131 for (i=0; i<PCI_ACCESS_MAX; i++)
132 if (pci_methods[i] && !strcmp(pci_methods[i]->name, name))
138 pci_get_method_name(int index)
140 if (index < 0 || index >= PCI_ACCESS_MAX)
142 else if (!pci_methods[index])
145 return pci_methods[index]->name;
151 struct pci_access *a = malloc(sizeof(struct pci_access));
154 memset(a, 0, sizeof(*a));
155 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0);
157 pci_define_param(a, "net.domain", PCI_ID_DOMAIN, "DNS domain used for resolving of ID's");
158 pci_define_param(a, "net.cache_name", "~/.pciids-cache", "Name of the ID cache file");
159 a->id_lookup_mode = PCI_LOOKUP_CACHE;
161 for(i=0; i<PCI_ACCESS_MAX; i++)
162 if (pci_methods[i] && pci_methods[i]->config)
163 pci_methods[i]->config(a);
168 pci_init(struct pci_access *a)
171 a->error = pci_generic_error;
173 a->warning = pci_generic_warn;
175 a->debug = pci_generic_debug;
177 a->debug = pci_null_debug;
181 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
182 a->error("This access method is not supported.");
183 a->methods = pci_methods[a->method];
188 for(i=0; i<PCI_ACCESS_MAX; i++)
191 a->debug("Trying method %d...", i);
192 if (pci_methods[i]->detect(a))
195 a->methods = pci_methods[i];
199 a->debug("...No.\n");
202 a->error("Cannot find any working access method.");
204 a->debug("Decided to use %s\n", a->methods->name);
209 pci_cleanup(struct pci_access *a)
211 struct pci_dev *d, *e;
213 for(d=a->devices; d; d=e)
219 a->methods->cleanup(a);
220 pci_free_name_list(a);
222 pci_set_name_list_path(a, NULL, 0);