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_SYSDBG 85 &pm_win32_sysdbg, 86 #else 87 NULL, 88 #endif 89 #ifdef PCI_HAVE_PM_MMIO_CONF 90 &pm_mmio_conf1, 91 #else 92 NULL, 93 #endif 94 }; 95 96 // If PCI_ACCESS_AUTO is selected, we probe the access methods in this order 97 static int probe_sequence[] = { 98 // System-specific methods 99 PCI_ACCESS_SYS_BUS_PCI, 100 PCI_ACCESS_PROC_BUS_PCI, 101 PCI_ACCESS_FBSD_DEVICE, 102 PCI_ACCESS_AIX_DEVICE, 103 PCI_ACCESS_NBSD_LIBPCI, 104 PCI_ACCESS_OBSD_DEVICE, 105 PCI_ACCESS_DARWIN, 106 PCI_ACCESS_SYLIXOS_DEVICE, 107 PCI_ACCESS_HURD, 108 PCI_ACCESS_WIN32_CFGMGR32, 109 PCI_ACCESS_WIN32_SYSDBG, 110 // Low-level methods poking the hardware directly 111 PCI_ACCESS_I386_TYPE1, 112 PCI_ACCESS_I386_TYPE2, 113 PCI_ACCESS_MMIO_TYPE1, 114 -1, 115 }; 116 117 static void PCI_NONRET 118 pci_generic_error(char *msg, ...) 119 { 120 va_list args; 121 122 va_start(args, msg); 123 fputs("pcilib: ", stderr); 124 vfprintf(stderr, msg, args); 125 va_end(args); 126 fputc('\n', stderr); 127 exit(1); 128 } 129 130 static void 131 pci_generic_warn(char *msg, ...) 132 { 133 va_list args; 134 135 va_start(args, msg); 136 fputs("pcilib: ", stderr); 137 vfprintf(stderr, msg, args); 138 va_end(args); 139 fputc('\n', stderr); 140 } 141 142 static void 143 pci_generic_debug(char *msg, ...) 144 { 145 va_list args; 146 147 va_start(args, msg); 148 vfprintf(stdout, msg, args); 149 va_end(args); 150 } 151 152 static void 153 pci_null_debug(char *msg UNUSED, ...) 154 { 155 } 156 157 // Memory allocation functions are safe to call if pci_access is not fully initalized or even NULL 158 159 void * 160 pci_malloc(struct pci_access *a, int size) 161 { 162 void *x = malloc(size); 163 164 if (!x) 165 (a && a->error ? a->error : pci_generic_error)("Out of memory (allocation of %d bytes failed)", size); 166 return x; 167 } 168 169 void 170 pci_mfree(void *x) 171 { 172 if (x) 173 free(x); 174 } 175 176 char * 177 pci_strdup(struct pci_access *a, const char *s) 178 { 179 int len = strlen(s) + 1; 180 char *t = pci_malloc(a, len); 181 memcpy(t, s, len); 182 return t; 183 } 184 185 int 186 pci_lookup_method(char *name) 187 { 188 int i; 189 190 for (i=0; i<PCI_ACCESS_MAX; i++) 191 if (pci_methods[i] && !strcmp(pci_methods[i]->name, name)) 192 return i; 193 return -1; 194 } 195 196 char * 197 pci_get_method_name(int index) 198 { 199 if (index < 0 || index >= PCI_ACCESS_MAX) 200 return NULL; 201 else if (!pci_methods[index]) 202 return ""; 203 else 204 return pci_methods[index]->name; 205 } 206 207 #ifdef PCI_OS_WINDOWS 208 209 static void 210 pci_init_name_list_path(struct pci_access *a) 211 { 212 if ((PCI_PATH_IDS_DIR)[0]) 213 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "\\" PCI_IDS, 0); 214 else 215 { 216 char *path, *sep; 217 DWORD len; 218 219 path = pci_malloc(a, MAX_PATH+1); 220 len = GetModuleFileNameA(NULL, path, MAX_PATH+1); 221 sep = (len > 0) ? strrchr(path, '\\') : NULL; 222 if (len == 0 || len == MAX_PATH+1 || !sep || MAX_PATH-(size_t)(sep+1-path) < sizeof(PCI_IDS)) 223 { 224 free(path); 225 pci_set_name_list_path(a, PCI_IDS, 0); 226 } 227 else 228 { 229 memcpy(sep+1, PCI_IDS, sizeof(PCI_IDS)); 230 pci_set_name_list_path(a, path, 1); 231 } 232 } 233 } 234 235 #else 236 237 static void 238 pci_init_name_list_path(struct pci_access *a) 239 { 240 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0); 241 } 242 243 #endif 244 245 struct pci_access * 246 pci_alloc(void) 247 { 248 struct pci_access *a = pci_malloc(NULL, sizeof(struct pci_access)); 249 int i; 250 251 memset(a, 0, sizeof(*a)); 252 pci_init_name_list_path(a); 253 #ifdef PCI_USE_DNS 254 pci_define_param(a, "net.domain", PCI_ID_DOMAIN, "DNS domain used for resolving of ID's"); 255 pci_define_param(a, "net.cache_name", "~/.pciids-cache", "Name of the ID cache file"); 256 a->id_lookup_mode = PCI_LOOKUP_CACHE; 257 #endif 258 #ifdef PCI_HAVE_HWDB 259 pci_define_param(a, "hwdb.disable", "0", "Do not look up names in UDEV's HWDB if non-zero"); 260 #endif 261 for (i=0; i<PCI_ACCESS_MAX; i++) 262 if (pci_methods[i] && pci_methods[i]->config) 263 pci_methods[i]->config(a); 264 return a; 265 } 266 267 void 268 pci_init_v35(struct pci_access *a) 269 { 270 if (!a->error) 271 a->error = pci_generic_error; 272 if (!a->warning) 273 a->warning = pci_generic_warn; 274 if (!a->debug) 275 a->debug = pci_generic_debug; 276 if (!a->debugging) 277 a->debug = pci_null_debug; 278 279 if (a->method) 280 { 281 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method]) 282 a->error("This access method is not supported."); 283 a->methods = pci_methods[a->method]; 284 } 285 else 286 { 287 unsigned int i; 288 for (i=0; probe_sequence[i] >= 0; i++) 289 { 290 struct pci_methods *m = pci_methods[probe_sequence[i]]; 291 if (!m) 292 continue; 293 a->debug("Trying method %s...", m->name); 294 if (m->detect(a)) 295 { 296 a->debug("...OK\n"); 297 a->methods = m; 298 a->method = probe_sequence[i]; 299 break; 300 } 301 a->debug("...No.\n"); 302 } 303 if (!a->methods) 304 a->error("Cannot find any working access method."); 305 } 306 a->debug("Decided to use %s\n", a->methods->name); 307 a->methods->init(a); 308 } 309 310 STATIC_ALIAS(void pci_init(struct pci_access *a), pci_init_v35(a)); 311 DEFINE_ALIAS(void pci_init_v30(struct pci_access *a), pci_init_v35); 312 SYMBOL_VERSION(pci_init_v30, pci_init@LIBPCI_3.0); 313 SYMBOL_VERSION(pci_init_v35, pci_init@@LIBPCI_3.5); 314 315 void 316 pci_cleanup(struct pci_access *a) 317 { 318 struct pci_dev *d, *e; 319 320 for (d=a->devices; d; d=e) 321 { 322 e = d->next; 323 pci_free_dev(d); 324 } 325 if (a->methods) 326 a->methods->cleanup(a); 327 pci_free_name_list(a); 328 pci_free_params(a); 329 pci_set_name_list_path(a, NULL, 0); 330 pci_mfree(a); 331 } 332