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.
17 #include <crt0.h> /* for __dos_argv0 */
24 /* Force usage of ANSI (char*) variant of GetModuleFileName() function */
26 #ifdef GetModuleFileName
27 #undef GetModuleFileName
29 #define GetModuleFileName GetModuleFileNameA
32 /* Define __ImageBase for all linkers */
34 /* GNU LD provides __ImageBase symbol since 2.19, in previous versions it is
35 * under name _image_base__, so add weak alias for compatibility. */
37 asm(".weak\t" PCI_STRINGIFY(__MINGW_USYMBOL(__ImageBase)) "\n\t"
38 ".set\t" PCI_STRINGIFY(__MINGW_USYMBOL(__ImageBase)) "," PCI_STRINGIFY(__MINGW_USYMBOL(_image_base__)));
41 * MSVC link.exe provides __ImageBase symbol since 12.00 (MSVC 6.0), for
42 * previous versions resolve it at runtime via GetModuleHandleA() which
43 * returns base for main executable or via VirtualQuery() for DLL builds.
45 #if defined(_MSC_VER) && _MSC_VER < 1200
47 get_current_module_handle(void)
50 MEMORY_BASIC_INFORMATION info;
51 size_t len = VirtualQuery(&get_current_module_handle, &info, sizeof(info));
52 if (len != sizeof(info))
54 return (HMODULE)info.AllocationBase;
56 return GetModuleHandleA(NULL);
59 #define __ImageBase (*(IMAGE_DOS_HEADER *)get_current_module_handle())
61 extern IMAGE_DOS_HEADER __ImageBase;
66 extern HINSTANCE _hModule;
67 #elif defined(_WINDOWS)
68 extern HINSTANCE _hInstance;
73 static struct pci_methods *pci_methods[PCI_ACCESS_MAX] = {
75 #ifdef PCI_HAVE_PM_LINUX_SYSFS
80 #ifdef PCI_HAVE_PM_LINUX_PROC
85 #ifdef PCI_HAVE_PM_INTEL_CONF
92 #ifdef PCI_HAVE_PM_FBSD_DEVICE
97 #ifdef PCI_HAVE_PM_AIX_DEVICE
102 #ifdef PCI_HAVE_PM_NBSD_LIBPCI
107 #ifdef PCI_HAVE_PM_OBSD_DEVICE
112 #ifdef PCI_HAVE_PM_DUMP
117 #ifdef PCI_HAVE_PM_DARWIN_DEVICE
122 #ifdef PCI_HAVE_PM_SYLIXOS_DEVICE
127 #ifdef PCI_HAVE_PM_HURD_CONF
132 #ifdef PCI_HAVE_PM_WIN32_CFGMGR32
137 #ifdef PCI_HAVE_PM_WIN32_KLDBG
142 #ifdef PCI_HAVE_PM_WIN32_SYSDBG
147 #ifdef PCI_HAVE_PM_MMIO_CONF
156 // If PCI_ACCESS_AUTO is selected, we probe the access methods in this order
157 static int probe_sequence[] = {
158 // System-specific methods
159 PCI_ACCESS_SYS_BUS_PCI,
160 PCI_ACCESS_PROC_BUS_PCI,
161 PCI_ACCESS_FBSD_DEVICE,
162 PCI_ACCESS_AIX_DEVICE,
163 PCI_ACCESS_NBSD_LIBPCI,
164 PCI_ACCESS_OBSD_DEVICE,
166 PCI_ACCESS_SYLIXOS_DEVICE,
168 PCI_ACCESS_WIN32_CFGMGR32,
169 PCI_ACCESS_WIN32_KLDBG,
170 PCI_ACCESS_WIN32_SYSDBG,
171 // Low-level methods poking the hardware directly
172 PCI_ACCESS_I386_TYPE1,
173 PCI_ACCESS_I386_TYPE2,
174 PCI_ACCESS_MMIO_TYPE1_EXT,
175 PCI_ACCESS_MMIO_TYPE1,
179 static void PCI_NONRET
180 pci_generic_error(char *msg, ...)
185 fputs("pcilib: ", stderr);
186 vfprintf(stderr, msg, args);
193 pci_generic_warn(char *msg, ...)
198 fputs("pcilib: ", stderr);
199 vfprintf(stderr, msg, args);
205 pci_generic_debug(char *msg, ...)
210 vfprintf(stdout, msg, args);
215 pci_null_debug(char *msg UNUSED, ...)
219 // Memory allocation functions are safe to call if pci_access is not fully initalized or even NULL
222 pci_malloc(struct pci_access *a, int size)
224 void *x = malloc(size);
227 (a && a->error ? a->error : pci_generic_error)("Out of memory (allocation of %d bytes failed)", size);
239 pci_strdup(struct pci_access *a, const char *s)
241 int len = strlen(s) + 1;
242 char *t = pci_malloc(a, len);
248 pci_lookup_method(char *name)
252 for (i=0; i<PCI_ACCESS_MAX; i++)
253 if (pci_methods[i] && !strcmp(pci_methods[i]->name, name))
259 pci_get_method_name(int index)
261 if (index < 0 || index >= PCI_ACCESS_MAX)
263 else if (!pci_methods[index])
266 return pci_methods[index]->name;
269 #if defined(PCI_OS_WINDOWS) || defined(PCI_OS_DJGPP)
272 pci_init_name_list_path(struct pci_access *a)
274 if ((PCI_PATH_IDS_DIR)[0])
275 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "\\" PCI_IDS, 0);
281 #if defined(PCI_OS_WINDOWS) && (defined(_WIN32) || defined(_WINDLL) || defined(_WINDOWS))
287 module = (HINSTANCE)&__ImageBase;
288 #elif defined(_WINDLL)
290 #elif defined(_WINDOWS)
295 * Module file name can have arbitrary length despite all MS examples say
296 * about MAX_PATH upper limit. This limit does not apply for example when
297 * executable is running from network disk with very long UNC paths or
298 * when using "\\??\\" prefix for specifying executable binary path.
299 * Function GetModuleFileName() returns passed size argument when passed
300 * buffer is too small and does not signal any error. In this case retry
301 * again with larger buffer.
303 size = 256; /* initial buffer size (more than sizeof(PCI_IDS)) */
305 path = pci_malloc(a, size);
306 len = GetModuleFileName(module, path, size-sizeof(PCI_IDS));
307 if (len >= size-sizeof(PCI_IDS))
316 #elif defined(PCI_OS_DJGPP) || defined(PCI_OS_WINDOWS)
318 const char *exe_path;
321 exe_path = __dos_argv0;
326 len = strlen(exe_path);
327 path = pci_malloc(a, len+sizeof(PCI_IDS));
328 memcpy(path, exe_path, len+1);
332 sep = strrchr(path, '\\');
336 * If current module path (current executable for static builds or
337 * current DLL library for shared build) cannot be determined then
338 * fallback to the current directory.
341 pci_set_name_list_path(a, PCI_IDS, 0);
345 memcpy(sep+1, PCI_IDS, sizeof(PCI_IDS));
346 pci_set_name_list_path(a, path, 1);
354 pci_init_name_list_path(struct pci_access *a)
356 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0);
364 struct pci_access *a = pci_malloc(NULL, sizeof(struct pci_access));
367 memset(a, 0, sizeof(*a));
368 pci_init_name_list_path(a);
370 pci_define_param(a, "net.domain", PCI_ID_DOMAIN, "DNS domain used for resolving of ID's");
371 pci_define_param(a, "net.cache_name", "~/.pciids-cache", "Name of the ID cache file");
372 a->id_lookup_mode = PCI_LOOKUP_CACHE;
375 pci_define_param(a, "hwdb.disable", "0", "Do not look up names in UDEV's HWDB if non-zero");
377 for (i=0; i<PCI_ACCESS_MAX; i++)
378 if (pci_methods[i] && pci_methods[i]->config)
379 pci_methods[i]->config(a);
384 pci_init_v35(struct pci_access *a)
387 a->error = pci_generic_error;
389 a->warning = pci_generic_warn;
391 a->debug = pci_generic_debug;
393 a->debug = pci_null_debug;
397 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
398 a->error("This access method is not supported.");
399 a->methods = pci_methods[a->method];
404 for (i=0; probe_sequence[i] >= 0; i++)
406 struct pci_methods *m = pci_methods[probe_sequence[i]];
409 a->debug("Trying method %s...", m->name);
414 a->method = probe_sequence[i];
417 a->debug("...No.\n");
420 a->error("Cannot find any working access method.");
422 a->debug("Decided to use %s\n", a->methods->name);
426 STATIC_ALIAS(void pci_init(struct pci_access *a), pci_init_v35(a));
427 DEFINE_ALIAS(void pci_init_v30(struct pci_access *a), pci_init_v35);
428 SYMBOL_VERSION(pci_init_v30, pci_init@LIBPCI_3.0);
429 SYMBOL_VERSION(pci_init_v35, pci_init@@LIBPCI_3.5);
432 pci_cleanup(struct pci_access *a)
434 struct pci_dev *d, *e;
436 for (d=a->devices; d; d=e)
442 a->methods->cleanup(a);
443 pci_free_name_list(a);
445 pci_set_name_list_path(a, NULL, 0);