1 /* 2 * The PCI Library -- Initialization and related things 3 * 4 * Copyright (c) 1997--2018 Martin Mares <[email protected]> 5 * 6 * Can be freely distributed and used under the terms of the GNU GPL. 7 */ 8 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <stdarg.h> 12 #include <string.h> 13 14 #include "internal.h" 15 16 #ifdef PCI_OS_WINDOWS 17 #include <windows.h> 18 #endif 19 20 static struct pci_methods *pci_methods[PCI_ACCESS_MAX] = { 21 NULL, 22 #ifdef PCI_HAVE_PM_LINUX_SYSFS 23 &pm_linux_sysfs, 24 #else 25 NULL, 26 #endif 27 #ifdef PCI_HAVE_PM_LINUX_PROC 28 &pm_linux_proc, 29 #else 30 NULL, 31 #endif 32 #ifdef PCI_HAVE_PM_INTEL_CONF 33 &pm_intel_conf1, 34 &pm_intel_conf2, 35 #else 36 NULL, 37 NULL, 38 #endif 39 #ifdef PCI_HAVE_PM_FBSD_DEVICE 40 &pm_fbsd_device, 41 #else 42 NULL, 43 #endif 44 #ifdef PCI_HAVE_PM_AIX_DEVICE 45 &pm_aix_device, 46 #else 47 NULL, 48 #endif 49 #ifdef PCI_HAVE_PM_NBSD_LIBPCI 50 &pm_nbsd_libpci, 51 #else 52 NULL, 53 #endif 54 #ifdef PCI_HAVE_PM_OBSD_DEVICE 55 &pm_obsd_device, 56 #else 57 NULL, 58 #endif 59 #ifdef PCI_HAVE_PM_DUMP 60 &pm_dump, 61 #else 62 NULL, 63 #endif 64 #ifdef PCI_HAVE_PM_DARWIN_DEVICE 65 &pm_darwin, 66 #else 67 NULL, 68 #endif 69 #ifdef PCI_HAVE_PM_SYLIXOS_DEVICE 70 &pm_sylixos_device, 71 #else 72 NULL, 73 #endif 74 #ifdef PCI_HAVE_PM_HURD_CONF 75 &pm_hurd, 76 #else 77 NULL, 78 #endif 79 #ifdef PCI_HAVE_PM_WIN32_CFGMGR32 80 &pm_win32_cfgmgr32, 81 #else 82 NULL, 83 #endif 84 #ifdef PCI_HAVE_PM_WIN32_KLDBG 85 &pm_win32_kldbg, 86 #else 87 NULL, 88 #endif 89 #ifdef PCI_HAVE_PM_WIN32_SYSDBG 90 &pm_win32_sysdbg, 91 #else 92 NULL, 93 #endif 94 #ifdef PCI_HAVE_PM_MMIO_CONF 95 &pm_mmio_conf1, 96 &pm_mmio_conf1_ext, 97 #else 98 NULL, 99 NULL, 100 #endif 101 }; 102 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, 112 PCI_ACCESS_DARWIN, 113 PCI_ACCESS_SYLIXOS_DEVICE, 114 PCI_ACCESS_HURD, 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, 123 -1, 124 }; 125 126 static void PCI_NONRET 127 pci_generic_error(char *msg, ...) 128 { 129 va_list args; 130 131 va_start(args, msg); 132 fputs("pcilib: ", stderr); 133 vfprintf(stderr, msg, args); 134 va_end(args); 135 fputc('\n', stderr); 136 exit(1); 137 } 138 139 static void 140 pci_generic_warn(char *msg, ...) 141 { 142 va_list args; 143 144 va_start(args, msg); 145 fputs("pcilib: ", stderr); 146 vfprintf(stderr, msg, args); 147 va_end(args); 148 fputc('\n', stderr); 149 } 150 151 static void 152 pci_generic_debug(char *msg, ...) 153 { 154 va_list args; 155 156 va_start(args, msg); 157 vfprintf(stdout, msg, args); 158 va_end(args); 159 } 160 161 static void 162 pci_null_debug(char *msg UNUSED, ...) 163 { 164 } 165 166 // Memory allocation functions are safe to call if pci_access is not fully initalized or even NULL 167 168 void * 169 pci_malloc(struct pci_access *a, int size) 170 { 171 void *x = malloc(size); 172 173 if (!x) 174 (a && a->error ? a->error : pci_generic_error)("Out of memory (allocation of %d bytes failed)", size); 175 return x; 176 } 177 178 void 179 pci_mfree(void *x) 180 { 181 if (x) 182 free(x); 183 } 184 185 char * 186 pci_strdup(struct pci_access *a, const char *s) 187 { 188 int len = strlen(s) + 1; 189 char *t = pci_malloc(a, len); 190 memcpy(t, s, len); 191 return t; 192 } 193 194 int 195 pci_lookup_method(char *name) 196 { 197 int i; 198 199 for (i=0; i<PCI_ACCESS_MAX; i++) 200 if (pci_methods[i] && !strcmp(pci_methods[i]->name, name)) 201 return i; 202 return -1; 203 } 204 205 char * 206 pci_get_method_name(int index) 207 { 208 if (index < 0 || index >= PCI_ACCESS_MAX) 209 return NULL; 210 else if (!pci_methods[index]) 211 return ""; 212 else 213 return pci_methods[index]->name; 214 } 215 216 #ifdef PCI_OS_WINDOWS 217 218 static void 219 pci_init_name_list_path(struct pci_access *a) 220 { 221 if ((PCI_PATH_IDS_DIR)[0]) 222 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "\\" PCI_IDS, 0); 223 else 224 { 225 char *path, *sep; 226 DWORD len; 227 228 path = pci_malloc(a, MAX_PATH+1); 229 len = GetModuleFileNameA(NULL, path, MAX_PATH+1); 230 sep = (len > 0) ? strrchr(path, '\\') : NULL; 231 if (len == 0 || len == MAX_PATH+1 || !sep || MAX_PATH-(size_t)(sep+1-path) < sizeof(PCI_IDS)) 232 { 233 free(path); 234 pci_set_name_list_path(a, PCI_IDS, 0); 235 } 236 else 237 { 238 memcpy(sep+1, PCI_IDS, sizeof(PCI_IDS)); 239 pci_set_name_list_path(a, path, 1); 240 } 241 } 242 } 243 244 #else 245 246 static void 247 pci_init_name_list_path(struct pci_access *a) 248 { 249 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0); 250 } 251 252 #endif 253 254 struct pci_access * 255 pci_alloc(void) 256 { 257 struct pci_access *a = pci_malloc(NULL, sizeof(struct pci_access)); 258 int i; 259 260 memset(a, 0, sizeof(*a)); 261 pci_init_name_list_path(a); 262 #ifdef PCI_USE_DNS 263 pci_define_param(a, "net.domain", PCI_ID_DOMAIN, "DNS domain used for resolving of ID's"); 264 pci_define_param(a, "net.cache_name", "~/.pciids-cache", "Name of the ID cache file"); 265 a->id_lookup_mode = PCI_LOOKUP_CACHE; 266 #endif 267 #ifdef PCI_HAVE_HWDB 268 pci_define_param(a, "hwdb.disable", "0", "Do not look up names in UDEV's HWDB if non-zero"); 269 #endif 270 for (i=0; i<PCI_ACCESS_MAX; i++) 271 if (pci_methods[i] && pci_methods[i]->config) 272 pci_methods[i]->config(a); 273 return a; 274 } 275 276 void 277 pci_init_v35(struct pci_access *a) 278 { 279 if (!a->error) 280 a->error = pci_generic_error; 281 if (!a->warning) 282 a->warning = pci_generic_warn; 283 if (!a->debug) 284 a->debug = pci_generic_debug; 285 if (!a->debugging) 286 a->debug = pci_null_debug; 287 288 if (a->method) 289 { 290 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method]) 291 a->error("This access method is not supported."); 292 a->methods = pci_methods[a->method]; 293 } 294 else 295 { 296 unsigned int i; 297 for (i=0; probe_sequence[i] >= 0; i++) 298 { 299 struct pci_methods *m = pci_methods[probe_sequence[i]]; 300 if (!m) 301 continue; 302 a->debug("Trying method %s...", m->name); 303 if (m->detect(a)) 304 { 305 a->debug("...OK\n"); 306 a->methods = m; 307 a->method = probe_sequence[i]; 308 break; 309 } 310 a->debug("...No.\n"); 311 } 312 if (!a->methods) 313 a->error("Cannot find any working access method."); 314 } 315 a->debug("Decided to use %s\n", a->methods->name); 316 a->methods->init(a); 317 } 318 319 STATIC_ALIAS(void pci_init(struct pci_access *a), pci_init_v35(a)); 320 DEFINE_ALIAS(void pci_init_v30(struct pci_access *a), pci_init_v35); 321 SYMBOL_VERSION(pci_init_v30, pci_init@LIBPCI_3.0); 322 SYMBOL_VERSION(pci_init_v35, pci_init@@LIBPCI_3.5); 323 324 void 325 pci_cleanup(struct pci_access *a) 326 { 327 struct pci_dev *d, *e; 328 329 for (d=a->devices; d; d=e) 330 { 331 e = d->next; 332 pci_free_dev(d); 333 } 334 if (a->methods) 335 a->methods->cleanup(a); 336 pci_free_name_list(a); 337 pci_free_params(a); 338 pci_set_name_list_path(a, NULL, 0); 339 pci_mfree(a); 340 } 341