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