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
72 // If PCI_ACCESS_AUTO is selected, we probe the access methods in this order
73 static int probe_sequence[] = {
74 // System-specific methods
75 PCI_ACCESS_SYS_BUS_PCI,
76 PCI_ACCESS_PROC_BUS_PCI,
77 PCI_ACCESS_FBSD_DEVICE,
78 PCI_ACCESS_AIX_DEVICE,
79 PCI_ACCESS_NBSD_LIBPCI,
80 PCI_ACCESS_OBSD_DEVICE,
82 PCI_ACCESS_SYLIXOS_DEVICE,
83 // Low-level methods poking the hardware directly
84 PCI_ACCESS_I386_TYPE1,
85 PCI_ACCESS_I386_TYPE2,
90 pci_malloc(struct pci_access *a, int size)
92 void *x = malloc(size);
95 a->error("Out of memory (allocation of %d bytes failed)", size);
107 pci_strdup(struct pci_access *a, const char *s)
109 int len = strlen(s) + 1;
110 char *t = pci_malloc(a, len);
116 pci_generic_error(char *msg, ...)
121 fputs("pcilib: ", stderr);
122 vfprintf(stderr, msg, args);
129 pci_generic_warn(char *msg, ...)
134 fputs("pcilib: ", stderr);
135 vfprintf(stderr, msg, args);
141 pci_generic_debug(char *msg, ...)
146 vfprintf(stdout, msg, args);
151 pci_null_debug(char *msg UNUSED, ...)
156 pci_lookup_method(char *name)
160 for (i=0; i<PCI_ACCESS_MAX; i++)
161 if (pci_methods[i] && !strcmp(pci_methods[i]->name, name))
167 pci_get_method_name(int index)
169 if (index < 0 || index >= PCI_ACCESS_MAX)
171 else if (!pci_methods[index])
174 return pci_methods[index]->name;
180 struct pci_access *a = malloc(sizeof(struct pci_access));
183 memset(a, 0, sizeof(*a));
184 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0);
186 pci_define_param(a, "net.domain", PCI_ID_DOMAIN, "DNS domain used for resolving of ID's");
187 pci_define_param(a, "net.cache_name", "~/.pciids-cache", "Name of the ID cache file");
188 a->id_lookup_mode = PCI_LOOKUP_CACHE;
191 pci_define_param(a, "hwdb.disable", "0", "Do not look up names in UDEV's HWDB if non-zero");
193 for (i=0; i<PCI_ACCESS_MAX; i++)
194 if (pci_methods[i] && pci_methods[i]->config)
195 pci_methods[i]->config(a);
200 pci_init_v35(struct pci_access *a)
203 a->error = pci_generic_error;
205 a->warning = pci_generic_warn;
207 a->debug = pci_generic_debug;
209 a->debug = pci_null_debug;
213 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
214 a->error("This access method is not supported.");
215 a->methods = pci_methods[a->method];
220 for (i=0; probe_sequence[i] >= 0; i++)
222 struct pci_methods *m = pci_methods[probe_sequence[i]];
225 a->debug("Trying method %s...", m->name);
230 a->method = probe_sequence[i];
233 a->debug("...No.\n");
236 a->error("Cannot find any working access method.");
238 a->debug("Decided to use %s\n", a->methods->name);
242 STATIC_ALIAS(void pci_init(struct pci_access *a), pci_init_v35(a));
243 DEFINE_ALIAS(void pci_init_v30(struct pci_access *a), pci_init_v35);
244 SYMBOL_VERSION(pci_init_v30, pci_init@LIBPCI_3.0);
245 SYMBOL_VERSION(pci_init_v35, pci_init@@LIBPCI_3.5);
248 pci_cleanup(struct pci_access *a)
250 struct pci_dev *d, *e;
252 for (d=a->devices; d; d=e)
258 a->methods->cleanup(a);
259 pci_free_name_list(a);
261 pci_set_name_list_path(a, NULL, 0);