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_malloc(struct pci_access *a, int size)
98 void *x = malloc(size);
101 a->error("Out of memory (allocation of %d bytes failed)", size);
113 pci_strdup(struct pci_access *a, const char *s)
115 int len = strlen(s) + 1;
116 char *t = pci_malloc(a, len);
122 pci_generic_error(char *msg, ...)
127 fputs("pcilib: ", stderr);
128 vfprintf(stderr, msg, args);
135 pci_generic_warn(char *msg, ...)
140 fputs("pcilib: ", stderr);
141 vfprintf(stderr, msg, args);
147 pci_generic_debug(char *msg, ...)
152 vfprintf(stdout, msg, args);
157 pci_null_debug(char *msg UNUSED, ...)
162 pci_lookup_method(char *name)
166 for (i=0; i<PCI_ACCESS_MAX; i++)
167 if (pci_methods[i] && !strcmp(pci_methods[i]->name, name))
173 pci_get_method_name(int index)
175 if (index < 0 || index >= PCI_ACCESS_MAX)
177 else if (!pci_methods[index])
180 return pci_methods[index]->name;
186 struct pci_access *a = malloc(sizeof(struct pci_access));
189 memset(a, 0, sizeof(*a));
190 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0);
192 pci_define_param(a, "net.domain", PCI_ID_DOMAIN, "DNS domain used for resolving of ID's");
193 pci_define_param(a, "net.cache_name", "~/.pciids-cache", "Name of the ID cache file");
194 a->id_lookup_mode = PCI_LOOKUP_CACHE;
197 pci_define_param(a, "hwdb.disable", "0", "Do not look up names in UDEV's HWDB if non-zero");
199 for (i=0; i<PCI_ACCESS_MAX; i++)
200 if (pci_methods[i] && pci_methods[i]->config)
201 pci_methods[i]->config(a);
206 pci_init_v35(struct pci_access *a)
209 a->error = pci_generic_error;
211 a->warning = pci_generic_warn;
213 a->debug = pci_generic_debug;
215 a->debug = pci_null_debug;
219 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
220 a->error("This access method is not supported.");
221 a->methods = pci_methods[a->method];
226 for (i=0; probe_sequence[i] >= 0; i++)
228 struct pci_methods *m = pci_methods[probe_sequence[i]];
231 a->debug("Trying method %s...", m->name);
236 a->method = probe_sequence[i];
239 a->debug("...No.\n");
242 a->error("Cannot find any working access method.");
244 a->debug("Decided to use %s\n", a->methods->name);
248 STATIC_ALIAS(void pci_init(struct pci_access *a), pci_init_v35(a));
249 DEFINE_ALIAS(void pci_init_v30(struct pci_access *a), pci_init_v35);
250 SYMBOL_VERSION(pci_init_v30, pci_init@LIBPCI_3.0);
251 SYMBOL_VERSION(pci_init_v35, pci_init@@LIBPCI_3.5);
254 pci_cleanup(struct pci_access *a)
256 struct pci_dev *d, *e;
258 for (d=a->devices; d; d=e)
264 a->methods->cleanup(a);
265 pci_free_name_list(a);
267 pci_set_name_list_path(a, NULL, 0);