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 static struct pci_methods *pci_methods[PCI_ACCESS_MAX] = { 17 NULL, 18 #ifdef PCI_HAVE_PM_LINUX_SYSFS 19 &pm_linux_sysfs, 20 #else 21 NULL, 22 #endif 23 #ifdef PCI_HAVE_PM_LINUX_PROC 24 &pm_linux_proc, 25 #else 26 NULL, 27 #endif 28 #ifdef PCI_HAVE_PM_INTEL_CONF 29 &pm_intel_conf1, 30 &pm_intel_conf2, 31 #else 32 NULL, 33 NULL, 34 #endif 35 #ifdef PCI_HAVE_PM_FBSD_DEVICE 36 &pm_fbsd_device, 37 #else 38 NULL, 39 #endif 40 #ifdef PCI_HAVE_PM_AIX_DEVICE 41 &pm_aix_device, 42 #else 43 NULL, 44 #endif 45 #ifdef PCI_HAVE_PM_NBSD_LIBPCI 46 &pm_nbsd_libpci, 47 #else 48 NULL, 49 #endif 50 #ifdef PCI_HAVE_PM_OBSD_DEVICE 51 &pm_obsd_device, 52 #else 53 NULL, 54 #endif 55 #ifdef PCI_HAVE_PM_DUMP 56 &pm_dump, 57 #else 58 NULL, 59 #endif 60 #ifdef PCI_HAVE_PM_DARWIN_DEVICE 61 &pm_darwin, 62 #else 63 NULL, 64 #endif 65 #ifdef PCI_HAVE_PM_SYLIXOS_DEVICE 66 &pm_sylixos_device, 67 #else 68 NULL, 69 #endif 70 }; 71 72 // If PCI_ACCESS_AUTO is selected, we probe the access methods in this order 73 static int probe_sequence[] = { 74 // System-specific methods 75 PCI_ACCESS_SYS_BUS_PCI, 76 PCI_ACCESS_PROC_BUS_PCI, 77 PCI_ACCESS_FBSD_DEVICE, 78 PCI_ACCESS_AIX_DEVICE, 79 PCI_ACCESS_NBSD_LIBPCI, 80 PCI_ACCESS_OBSD_DEVICE, 81 PCI_ACCESS_DARWIN, 82 PCI_ACCESS_SYLIXOS_DEVICE, 83 // Low-level methods poking the hardware directly 84 PCI_ACCESS_I386_TYPE1, 85 PCI_ACCESS_I386_TYPE2, 86 -1, 87 }; 88 89 void * 90 pci_malloc(struct pci_access *a, int size) 91 { 92 void *x = malloc(size); 93 94 if (!x) 95 a->error("Out of memory (allocation of %d bytes failed)", size); 96 return x; 97 } 98 99 void 100 pci_mfree(void *x) 101 { 102 if (x) 103 free(x); 104 } 105 106 char * 107 pci_strdup(struct pci_access *a, const char *s) 108 { 109 int len = strlen(s) + 1; 110 char *t = pci_malloc(a, len); 111 memcpy(t, s, len); 112 return t; 113 } 114 115 static void 116 pci_generic_error(char *msg, ...) 117 { 118 va_list args; 119 120 va_start(args, msg); 121 fputs("pcilib: ", stderr); 122 vfprintf(stderr, msg, args); 123 fputc('\n', stderr); 124 exit(1); 125 } 126 127 static void 128 pci_generic_warn(char *msg, ...) 129 { 130 va_list args; 131 132 va_start(args, msg); 133 fputs("pcilib: ", stderr); 134 vfprintf(stderr, msg, args); 135 fputc('\n', stderr); 136 } 137 138 static void 139 pci_generic_debug(char *msg, ...) 140 { 141 va_list args; 142 143 va_start(args, msg); 144 vfprintf(stdout, msg, args); 145 va_end(args); 146 } 147 148 static void 149 pci_null_debug(char *msg UNUSED, ...) 150 { 151 } 152 153 int 154 pci_lookup_method(char *name) 155 { 156 int i; 157 158 for (i=0; i<PCI_ACCESS_MAX; i++) 159 if (pci_methods[i] && !strcmp(pci_methods[i]->name, name)) 160 return i; 161 return -1; 162 } 163 164 char * 165 pci_get_method_name(int index) 166 { 167 if (index < 0 || index >= PCI_ACCESS_MAX) 168 return NULL; 169 else if (!pci_methods[index]) 170 return ""; 171 else 172 return pci_methods[index]->name; 173 } 174 175 struct pci_access * 176 pci_alloc(void) 177 { 178 struct pci_access *a = malloc(sizeof(struct pci_access)); 179 int i; 180 181 memset(a, 0, sizeof(*a)); 182 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0); 183 #ifdef PCI_USE_DNS 184 pci_define_param(a, "net.domain", PCI_ID_DOMAIN, "DNS domain used for resolving of ID's"); 185 pci_define_param(a, "net.cache_name", "~/.pciids-cache", "Name of the ID cache file"); 186 a->id_lookup_mode = PCI_LOOKUP_CACHE; 187 #endif 188 #ifdef PCI_HAVE_HWDB 189 pci_define_param(a, "hwdb.disable", "0", "Do not look up names in UDEV's HWDB if non-zero"); 190 #endif 191 for (i=0; i<PCI_ACCESS_MAX; i++) 192 if (pci_methods[i] && pci_methods[i]->config) 193 pci_methods[i]->config(a); 194 return a; 195 } 196 197 void 198 pci_init_v35(struct pci_access *a) 199 { 200 if (!a->error) 201 a->error = pci_generic_error; 202 if (!a->warning) 203 a->warning = pci_generic_warn; 204 if (!a->debug) 205 a->debug = pci_generic_debug; 206 if (!a->debugging) 207 a->debug = pci_null_debug; 208 209 if (a->method) 210 { 211 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method]) 212 a->error("This access method is not supported."); 213 a->methods = pci_methods[a->method]; 214 } 215 else 216 { 217 unsigned int i; 218 for (i=0; probe_sequence[i] >= 0; i++) 219 { 220 struct pci_methods *m = pci_methods[probe_sequence[i]]; 221 if (!m) 222 continue; 223 a->debug("Trying method %s...", m->name); 224 if (m->detect(a)) 225 { 226 a->debug("...OK\n"); 227 a->methods = m; 228 a->method = probe_sequence[i]; 229 break; 230 } 231 a->debug("...No.\n"); 232 } 233 if (!a->methods) 234 a->error("Cannot find any working access method."); 235 } 236 a->debug("Decided to use %s\n", a->methods->name); 237 a->methods->init(a); 238 } 239 240 STATIC_ALIAS(void pci_init(struct pci_access *a), pci_init_v35(a)); 241 DEFINE_ALIAS(void pci_init_v30(struct pci_access *a), pci_init_v35); 242 SYMBOL_VERSION(pci_init_v30, pci_init@LIBPCI_3.0); 243 SYMBOL_VERSION(pci_init_v35, pci_init@@LIBPCI_3.5); 244 245 void 246 pci_cleanup(struct pci_access *a) 247 { 248 struct pci_dev *d, *e; 249 250 for (d=a->devices; d; d=e) 251 { 252 e = d->next; 253 pci_free_dev(d); 254 } 255 if (a->methods) 256 a->methods->cleanup(a); 257 pci_free_name_list(a); 258 pci_free_params(a); 259 pci_set_name_list_path(a, NULL, 0); 260 pci_mfree(a); 261 } 262