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 /* Force usage of ANSI (char*) variant of GetModuleFileName() function */
22 #ifdef GetModuleFileName
23 #undef GetModuleFileName
25 #define GetModuleFileName GetModuleFileNameA
28 /* Define __ImageBase for all linkers */
30 /* GNU LD provides __ImageBase symbol since 2.19, in previous versions it is
31 * under name _image_base__, so add weak alias for compatibility. */
33 asm(".weak\t" PCI_STRINGIFY(__MINGW_USYMBOL(__ImageBase)) "\n\t"
34 ".set\t" PCI_STRINGIFY(__MINGW_USYMBOL(__ImageBase)) "," PCI_STRINGIFY(__MINGW_USYMBOL(_image_base__)));
37 * MSVC link.exe provides __ImageBase symbol since 12.00 (MSVC 6.0), for
38 * previous versions resolve it at runtime via GetModuleHandleA() which
39 * returns base for main executable or via VirtualQuery() for DLL builds.
41 #if defined(_MSC_VER) && _MSC_VER < 1200
43 get_current_module_handle(void)
46 MEMORY_BASIC_INFORMATION info;
47 size_t len = VirtualQuery(&get_current_module_handle, &info, sizeof(info));
48 if (len != sizeof(info))
50 return (HMODULE)info.AllocationBase;
52 return GetModuleHandleA(NULL);
55 #define __ImageBase (*(IMAGE_DOS_HEADER *)get_current_module_handle())
57 extern IMAGE_DOS_HEADER __ImageBase;
62 extern HINSTANCE _hModule;
63 #elif defined(_WINDOWS)
64 extern HINSTANCE _hInstance;
69 static struct pci_methods *pci_methods[PCI_ACCESS_MAX] = {
71 #ifdef PCI_HAVE_PM_LINUX_SYSFS
76 #ifdef PCI_HAVE_PM_LINUX_PROC
81 #ifdef PCI_HAVE_PM_INTEL_CONF
88 #ifdef PCI_HAVE_PM_FBSD_DEVICE
93 #ifdef PCI_HAVE_PM_AIX_DEVICE
98 #ifdef PCI_HAVE_PM_NBSD_LIBPCI
103 #ifdef PCI_HAVE_PM_OBSD_DEVICE
108 #ifdef PCI_HAVE_PM_DUMP
113 #ifdef PCI_HAVE_PM_DARWIN_DEVICE
118 #ifdef PCI_HAVE_PM_SYLIXOS_DEVICE
123 #ifdef PCI_HAVE_PM_HURD_CONF
128 #ifdef PCI_HAVE_PM_WIN32_CFGMGR32
133 #ifdef PCI_HAVE_PM_WIN32_KLDBG
138 #ifdef PCI_HAVE_PM_WIN32_SYSDBG
143 #ifdef PCI_HAVE_PM_MMIO_CONF
152 // If PCI_ACCESS_AUTO is selected, we probe the access methods in this order
153 static int probe_sequence[] = {
154 // System-specific methods
155 PCI_ACCESS_SYS_BUS_PCI,
156 PCI_ACCESS_PROC_BUS_PCI,
157 PCI_ACCESS_FBSD_DEVICE,
158 PCI_ACCESS_AIX_DEVICE,
159 PCI_ACCESS_NBSD_LIBPCI,
160 PCI_ACCESS_OBSD_DEVICE,
162 PCI_ACCESS_SYLIXOS_DEVICE,
164 PCI_ACCESS_WIN32_CFGMGR32,
165 PCI_ACCESS_WIN32_KLDBG,
166 PCI_ACCESS_WIN32_SYSDBG,
167 // Low-level methods poking the hardware directly
168 PCI_ACCESS_I386_TYPE1,
169 PCI_ACCESS_I386_TYPE2,
170 PCI_ACCESS_MMIO_TYPE1_EXT,
171 PCI_ACCESS_MMIO_TYPE1,
175 static void PCI_NONRET
176 pci_generic_error(char *msg, ...)
181 fputs("pcilib: ", stderr);
182 vfprintf(stderr, msg, args);
189 pci_generic_warn(char *msg, ...)
194 fputs("pcilib: ", stderr);
195 vfprintf(stderr, msg, args);
201 pci_generic_debug(char *msg, ...)
206 vfprintf(stdout, msg, args);
211 pci_null_debug(char *msg UNUSED, ...)
215 // Memory allocation functions are safe to call if pci_access is not fully initalized or even NULL
218 pci_malloc(struct pci_access *a, int size)
220 void *x = malloc(size);
223 (a && a->error ? a->error : pci_generic_error)("Out of memory (allocation of %d bytes failed)", size);
235 pci_strdup(struct pci_access *a, const char *s)
237 int len = strlen(s) + 1;
238 char *t = pci_malloc(a, len);
244 pci_lookup_method(char *name)
248 for (i=0; i<PCI_ACCESS_MAX; i++)
249 if (pci_methods[i] && !strcmp(pci_methods[i]->name, name))
255 pci_get_method_name(int index)
257 if (index < 0 || index >= PCI_ACCESS_MAX)
259 else if (!pci_methods[index])
262 return pci_methods[index]->name;
265 #ifdef PCI_OS_WINDOWS
268 pci_init_name_list_path(struct pci_access *a)
270 if ((PCI_PATH_IDS_DIR)[0])
271 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "\\" PCI_IDS, 0);
277 #if defined(_WIN32) || defined(_WINDLL) || defined(_WINDOWS)
283 module = (HINSTANCE)&__ImageBase;
284 #elif defined(_WINDLL)
286 #elif defined(_WINDOWS)
291 * Module file name can have arbitrary length despite all MS examples say
292 * about MAX_PATH upper limit. This limit does not apply for example when
293 * executable is running from network disk with very long UNC paths or
294 * when using "\\??\\" prefix for specifying executable binary path.
295 * Function GetModuleFileName() returns passed size argument when passed
296 * buffer is too small and does not signal any error. In this case retry
297 * again with larger buffer.
299 size = 256; /* initial buffer size (more than sizeof(PCI_IDS)) */
301 path = pci_malloc(a, size);
302 len = GetModuleFileName(module, path, size-sizeof(PCI_IDS));
303 if (len >= size-sizeof(PCI_IDS))
314 const char *exe_path;
318 len = strlen(exe_path);
319 path = pci_malloc(a, len+sizeof(PCI_IDS));
320 memcpy(path, exe_path, len+1);
324 sep = strrchr(path, '\\');
328 * If current module path (current executable for static builds or
329 * current DLL library for shared build) cannot be determined then
330 * fallback to the current directory.
333 pci_set_name_list_path(a, PCI_IDS, 0);
337 memcpy(sep+1, PCI_IDS, sizeof(PCI_IDS));
338 pci_set_name_list_path(a, path, 1);
346 pci_init_name_list_path(struct pci_access *a)
348 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0);
356 struct pci_access *a = pci_malloc(NULL, sizeof(struct pci_access));
359 memset(a, 0, sizeof(*a));
360 pci_init_name_list_path(a);
362 pci_define_param(a, "net.domain", PCI_ID_DOMAIN, "DNS domain used for resolving of ID's");
363 pci_define_param(a, "net.cache_name", "~/.pciids-cache", "Name of the ID cache file");
364 a->id_lookup_mode = PCI_LOOKUP_CACHE;
367 pci_define_param(a, "hwdb.disable", "0", "Do not look up names in UDEV's HWDB if non-zero");
369 for (i=0; i<PCI_ACCESS_MAX; i++)
370 if (pci_methods[i] && pci_methods[i]->config)
371 pci_methods[i]->config(a);
376 pci_init_v35(struct pci_access *a)
379 a->error = pci_generic_error;
381 a->warning = pci_generic_warn;
383 a->debug = pci_generic_debug;
385 a->debug = pci_null_debug;
389 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
390 a->error("This access method is not supported.");
391 a->methods = pci_methods[a->method];
396 for (i=0; probe_sequence[i] >= 0; i++)
398 struct pci_methods *m = pci_methods[probe_sequence[i]];
401 a->debug("Trying method %s...", m->name);
406 a->method = probe_sequence[i];
409 a->debug("...No.\n");
412 a->error("Cannot find any working access method.");
414 a->debug("Decided to use %s\n", a->methods->name);
418 STATIC_ALIAS(void pci_init(struct pci_access *a), pci_init_v35(a));
419 DEFINE_ALIAS(void pci_init_v30(struct pci_access *a), pci_init_v35);
420 SYMBOL_VERSION(pci_init_v30, pci_init@LIBPCI_3.0);
421 SYMBOL_VERSION(pci_init_v35, pci_init@@LIBPCI_3.5);
424 pci_cleanup(struct pci_access *a)
426 struct pci_dev *d, *e;
428 for (d=a->devices; d; d=e)
434 a->methods->cleanup(a);
435 pci_free_name_list(a);
437 pci_set_name_list_path(a, NULL, 0);