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
67 // If PCI_ACCESS_AUTO is selected, we probe the access methods in this order
68 static int probe_sequence[] = {
69 // System-specific methods
70 PCI_ACCESS_SYS_BUS_PCI,
71 PCI_ACCESS_PROC_BUS_PCI,
72 PCI_ACCESS_FBSD_DEVICE,
73 PCI_ACCESS_AIX_DEVICE,
74 PCI_ACCESS_NBSD_LIBPCI,
75 PCI_ACCESS_OBSD_DEVICE,
77 // Low-level methods poking the hardware directly
78 PCI_ACCESS_I386_TYPE1,
79 PCI_ACCESS_I386_TYPE2,
84 pci_malloc(struct pci_access *a, int size)
86 void *x = malloc(size);
89 a->error("Out of memory (allocation of %d bytes failed)", size);
101 pci_strdup(struct pci_access *a, const char *s)
103 int len = strlen(s) + 1;
104 char *t = pci_malloc(a, len);
110 pci_generic_error(char *msg, ...)
115 fputs("pcilib: ", stderr);
116 vfprintf(stderr, msg, args);
122 pci_generic_warn(char *msg, ...)
127 fputs("pcilib: ", stderr);
128 vfprintf(stderr, msg, args);
133 pci_generic_debug(char *msg, ...)
138 vfprintf(stdout, msg, args);
143 pci_null_debug(char *msg UNUSED, ...)
148 pci_lookup_method(char *name)
152 for (i=0; i<PCI_ACCESS_MAX; i++)
153 if (pci_methods[i] && !strcmp(pci_methods[i]->name, name))
159 pci_get_method_name(int index)
161 if (index < 0 || index >= PCI_ACCESS_MAX)
163 else if (!pci_methods[index])
166 return pci_methods[index]->name;
172 struct pci_access *a = malloc(sizeof(struct pci_access));
175 memset(a, 0, sizeof(*a));
176 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0);
178 pci_define_param(a, "net.domain", PCI_ID_DOMAIN, "DNS domain used for resolving of ID's");
179 pci_define_param(a, "net.cache_name", "~/.pciids-cache", "Name of the ID cache file");
180 a->id_lookup_mode = PCI_LOOKUP_CACHE;
183 pci_define_param(a, "hwdb.disable", "0", "Do not look up names in UDEV's HWDB if non-zero");
185 for (i=0; i<PCI_ACCESS_MAX; i++)
186 if (pci_methods[i] && pci_methods[i]->config)
187 pci_methods[i]->config(a);
192 pci_init_v35(struct pci_access *a)
195 a->error = pci_generic_error;
197 a->warning = pci_generic_warn;
199 a->debug = pci_generic_debug;
201 a->debug = pci_null_debug;
205 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
206 a->error("This access method is not supported.");
207 a->methods = pci_methods[a->method];
212 for (i=0; probe_sequence[i] >= 0; i++)
214 struct pci_methods *m = pci_methods[probe_sequence[i]];
217 a->debug("Trying method %s...", m->name);
222 a->method = probe_sequence[i];
225 a->debug("...No.\n");
228 a->error("Cannot find any working access method.");
230 a->debug("Decided to use %s\n", a->methods->name);
234 STATIC_ALIAS(void pci_init(struct pci_access *a), pci_init_v35(a));
235 DEFINE_ALIAS(void pci_init_v30(struct pci_access *a), pci_init_v35);
236 SYMBOL_VERSION(pci_init_v30, pci_init@LIBPCI_3.0);
237 SYMBOL_VERSION(pci_init_v35, pci_init@@LIBPCI_3.5);
240 pci_cleanup(struct pci_access *a)
242 struct pci_dev *d, *e;
244 for (d=a->devices; d; d=e)
250 a->methods->cleanup(a);
251 pci_free_name_list(a);
253 pci_set_name_list_path(a, NULL, 0);