2 * The PCI Library -- Initialization and related things
4 * Copyright (c) 1997--2018 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
60 #ifdef PCI_HAVE_PM_DARWIN_DEVICE
65 #ifdef PCI_HAVE_PM_SYLIXOS_DEVICE
70 #ifdef PCI_HAVE_PM_HURD_CONF
77 // If PCI_ACCESS_AUTO is selected, we probe the access methods in this order
78 static int probe_sequence[] = {
79 // System-specific methods
80 PCI_ACCESS_SYS_BUS_PCI,
81 PCI_ACCESS_PROC_BUS_PCI,
82 PCI_ACCESS_FBSD_DEVICE,
83 PCI_ACCESS_AIX_DEVICE,
84 PCI_ACCESS_NBSD_LIBPCI,
85 PCI_ACCESS_OBSD_DEVICE,
87 PCI_ACCESS_SYLIXOS_DEVICE,
89 // Low-level methods poking the hardware directly
90 PCI_ACCESS_I386_TYPE1,
91 PCI_ACCESS_I386_TYPE2,
96 pci_generic_error(char *msg, ...)
101 fputs("pcilib: ", stderr);
102 vfprintf(stderr, msg, args);
109 pci_generic_warn(char *msg, ...)
114 fputs("pcilib: ", stderr);
115 vfprintf(stderr, msg, args);
121 pci_generic_debug(char *msg, ...)
126 vfprintf(stdout, msg, args);
131 pci_null_debug(char *msg UNUSED, ...)
135 // Memory allocation functions are safe to call if pci_access is not fully initalized or even NULL
138 pci_malloc(struct pci_access *a, int size)
140 void *x = malloc(size);
143 (a && a->error ? a->error : pci_generic_error)("Out of memory (allocation of %d bytes failed)", size);
155 pci_strdup(struct pci_access *a, const char *s)
157 int len = strlen(s) + 1;
158 char *t = pci_malloc(a, len);
164 pci_lookup_method(char *name)
168 for (i=0; i<PCI_ACCESS_MAX; i++)
169 if (pci_methods[i] && !strcmp(pci_methods[i]->name, name))
175 pci_get_method_name(int index)
177 if (index < 0 || index >= PCI_ACCESS_MAX)
179 else if (!pci_methods[index])
182 return pci_methods[index]->name;
188 struct pci_access *a = pci_malloc(NULL, sizeof(struct pci_access));
191 memset(a, 0, sizeof(*a));
192 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0);
194 pci_define_param(a, "net.domain", PCI_ID_DOMAIN, "DNS domain used for resolving of ID's");
195 pci_define_param(a, "net.cache_name", "~/.pciids-cache", "Name of the ID cache file");
196 a->id_lookup_mode = PCI_LOOKUP_CACHE;
199 pci_define_param(a, "hwdb.disable", "0", "Do not look up names in UDEV's HWDB if non-zero");
201 for (i=0; i<PCI_ACCESS_MAX; i++)
202 if (pci_methods[i] && pci_methods[i]->config)
203 pci_methods[i]->config(a);
208 pci_init_v35(struct pci_access *a)
211 a->error = pci_generic_error;
213 a->warning = pci_generic_warn;
215 a->debug = pci_generic_debug;
217 a->debug = pci_null_debug;
221 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
222 a->error("This access method is not supported.");
223 a->methods = pci_methods[a->method];
228 for (i=0; probe_sequence[i] >= 0; i++)
230 struct pci_methods *m = pci_methods[probe_sequence[i]];
233 a->debug("Trying method %s...", m->name);
238 a->method = probe_sequence[i];
241 a->debug("...No.\n");
244 a->error("Cannot find any working access method.");
246 a->debug("Decided to use %s\n", a->methods->name);
250 STATIC_ALIAS(void pci_init(struct pci_access *a), pci_init_v35(a));
251 DEFINE_ALIAS(void pci_init_v30(struct pci_access *a), pci_init_v35);
252 SYMBOL_VERSION(pci_init_v30, pci_init@LIBPCI_3.0);
253 SYMBOL_VERSION(pci_init_v35, pci_init@@LIBPCI_3.5);
256 pci_cleanup(struct pci_access *a)
258 struct pci_dev *d, *e;
260 for (d=a->devices; d; d=e)
266 a->methods->cleanup(a);
267 pci_free_name_list(a);
269 pci_set_name_list_path(a, NULL, 0);