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.
20 static struct pci_methods *pci_methods[PCI_ACCESS_MAX] = {
22 #ifdef PCI_HAVE_PM_LINUX_SYSFS
27 #ifdef PCI_HAVE_PM_LINUX_PROC
32 #ifdef PCI_HAVE_PM_INTEL_CONF
39 #ifdef PCI_HAVE_PM_FBSD_DEVICE
44 #ifdef PCI_HAVE_PM_AIX_DEVICE
49 #ifdef PCI_HAVE_PM_NBSD_LIBPCI
54 #ifdef PCI_HAVE_PM_OBSD_DEVICE
59 #ifdef PCI_HAVE_PM_DUMP
64 #ifdef PCI_HAVE_PM_DARWIN_DEVICE
69 #ifdef PCI_HAVE_PM_SYLIXOS_DEVICE
74 #ifdef PCI_HAVE_PM_HURD_CONF
79 #ifdef PCI_HAVE_PM_WIN32_CFGMGR32
84 #ifdef PCI_HAVE_PM_WIN32_KLDBG
89 #ifdef PCI_HAVE_PM_WIN32_SYSDBG
94 #ifdef PCI_HAVE_PM_MMIO_CONF
103 // If PCI_ACCESS_AUTO is selected, we probe the access methods in this order
104 static int probe_sequence[] = {
105 // System-specific methods
106 PCI_ACCESS_SYS_BUS_PCI,
107 PCI_ACCESS_PROC_BUS_PCI,
108 PCI_ACCESS_FBSD_DEVICE,
109 PCI_ACCESS_AIX_DEVICE,
110 PCI_ACCESS_NBSD_LIBPCI,
111 PCI_ACCESS_OBSD_DEVICE,
113 PCI_ACCESS_SYLIXOS_DEVICE,
115 PCI_ACCESS_WIN32_CFGMGR32,
116 PCI_ACCESS_WIN32_KLDBG,
117 PCI_ACCESS_WIN32_SYSDBG,
118 // Low-level methods poking the hardware directly
119 PCI_ACCESS_I386_TYPE1,
120 PCI_ACCESS_I386_TYPE2,
121 PCI_ACCESS_MMIO_TYPE1_EXT,
122 PCI_ACCESS_MMIO_TYPE1,
126 static void PCI_NONRET
127 pci_generic_error(char *msg, ...)
132 fputs("pcilib: ", stderr);
133 vfprintf(stderr, msg, args);
140 pci_generic_warn(char *msg, ...)
145 fputs("pcilib: ", stderr);
146 vfprintf(stderr, msg, args);
152 pci_generic_debug(char *msg, ...)
157 vfprintf(stdout, msg, args);
162 pci_null_debug(char *msg UNUSED, ...)
166 // Memory allocation functions are safe to call if pci_access is not fully initalized or even NULL
169 pci_malloc(struct pci_access *a, int size)
171 void *x = malloc(size);
174 (a && a->error ? a->error : pci_generic_error)("Out of memory (allocation of %d bytes failed)", size);
186 pci_strdup(struct pci_access *a, const char *s)
188 int len = strlen(s) + 1;
189 char *t = pci_malloc(a, len);
195 pci_lookup_method(char *name)
199 for (i=0; i<PCI_ACCESS_MAX; i++)
200 if (pci_methods[i] && !strcmp(pci_methods[i]->name, name))
206 pci_get_method_name(int index)
208 if (index < 0 || index >= PCI_ACCESS_MAX)
210 else if (!pci_methods[index])
213 return pci_methods[index]->name;
216 #ifdef PCI_OS_WINDOWS
219 pci_init_name_list_path(struct pci_access *a)
221 if ((PCI_PATH_IDS_DIR)[0])
222 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "\\" PCI_IDS, 0);
230 * Module file name can have arbitrary length despite all MS examples say
231 * about MAX_PATH upper limit. This limit does not apply for example when
232 * executable is running from network disk with very long UNC paths or
233 * when using "\\??\\" prefix for specifying executable binary path.
234 * Function GetModuleFileName() returns passed size argument when passed
235 * buffer is too small and does not signal any error. In this case retry
236 * again with larger buffer.
238 size = 256; /* initial buffer size (more than sizeof(PCI_IDS)) */
240 path = pci_malloc(a, size);
241 len = GetModuleFileNameA(module, path, size-sizeof(PCI_IDS));
242 if (len >= size-sizeof(PCI_IDS))
251 sep = strrchr(path, '\\');
255 pci_set_name_list_path(a, PCI_IDS, 0);
259 memcpy(sep+1, PCI_IDS, sizeof(PCI_IDS));
260 pci_set_name_list_path(a, path, 1);
268 pci_init_name_list_path(struct pci_access *a)
270 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0);
278 struct pci_access *a = pci_malloc(NULL, sizeof(struct pci_access));
281 memset(a, 0, sizeof(*a));
282 pci_init_name_list_path(a);
284 pci_define_param(a, "net.domain", PCI_ID_DOMAIN, "DNS domain used for resolving of ID's");
285 pci_define_param(a, "net.cache_name", "~/.pciids-cache", "Name of the ID cache file");
286 a->id_lookup_mode = PCI_LOOKUP_CACHE;
289 pci_define_param(a, "hwdb.disable", "0", "Do not look up names in UDEV's HWDB if non-zero");
291 for (i=0; i<PCI_ACCESS_MAX; i++)
292 if (pci_methods[i] && pci_methods[i]->config)
293 pci_methods[i]->config(a);
298 pci_init_v35(struct pci_access *a)
301 a->error = pci_generic_error;
303 a->warning = pci_generic_warn;
305 a->debug = pci_generic_debug;
307 a->debug = pci_null_debug;
311 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
312 a->error("This access method is not supported.");
313 a->methods = pci_methods[a->method];
318 for (i=0; probe_sequence[i] >= 0; i++)
320 struct pci_methods *m = pci_methods[probe_sequence[i]];
323 a->debug("Trying method %s...", m->name);
328 a->method = probe_sequence[i];
331 a->debug("...No.\n");
334 a->error("Cannot find any working access method.");
336 a->debug("Decided to use %s\n", a->methods->name);
340 STATIC_ALIAS(void pci_init(struct pci_access *a), pci_init_v35(a));
341 DEFINE_ALIAS(void pci_init_v30(struct pci_access *a), pci_init_v35);
342 SYMBOL_VERSION(pci_init_v30, pci_init@LIBPCI_3.0);
343 SYMBOL_VERSION(pci_init_v35, pci_init@@LIBPCI_3.5);
346 pci_cleanup(struct pci_access *a)
348 struct pci_dev *d, *e;
350 for (d=a->devices; d; d=e)
356 a->methods->cleanup(a);
357 pci_free_name_list(a);
359 pci_set_name_list_path(a, NULL, 0);