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.
16 static struct pci_methods *pci_methods[PCI_ACCESS_MAX] = {
18 #ifdef PCI_HAVE_PM_LINUX_SYSFS
23 #ifdef PCI_HAVE_PM_LINUX_PROC
28 #ifdef PCI_HAVE_PM_INTEL_CONF
35 #ifdef PCI_HAVE_PM_FBSD_DEVICE
40 #ifdef PCI_HAVE_PM_AIX_DEVICE
45 #ifdef PCI_HAVE_PM_NBSD_LIBPCI
50 #ifdef PCI_HAVE_PM_OBSD_DEVICE
55 #ifdef PCI_HAVE_PM_DUMP
60 #ifdef PCI_HAVE_PM_DARWIN_DEVICE
65 #ifdef PCI_HAVE_PM_SYLIXOS_DEVICE
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,
82 PCI_ACCESS_SYLIXOS_DEVICE,
83 // Low-level methods poking the hardware directly
84 PCI_ACCESS_I386_TYPE1,
85 PCI_ACCESS_I386_TYPE2,
90 pci_malloc(struct pci_access *a, int size)
92 void *x = malloc(size);
95 a->error("Out of memory (allocation of %d bytes failed)", size);
107 pci_strdup(struct pci_access *a, const char *s)
109 int len = strlen(s) + 1;
110 char *t = pci_malloc(a, len);
116 pci_generic_error(char *msg, ...)
121 fputs("pcilib: ", stderr);
122 vfprintf(stderr, msg, args);
128 pci_generic_warn(char *msg, ...)
133 fputs("pcilib: ", stderr);
134 vfprintf(stderr, msg, args);
139 pci_generic_debug(char *msg, ...)
144 vfprintf(stdout, msg, args);
149 pci_null_debug(char *msg UNUSED, ...)
154 pci_lookup_method(char *name)
158 for (i=0; i<PCI_ACCESS_MAX; i++)
159 if (pci_methods[i] && !strcmp(pci_methods[i]->name, name))
165 pci_get_method_name(int index)
167 if (index < 0 || index >= PCI_ACCESS_MAX)
169 else if (!pci_methods[index])
172 return pci_methods[index]->name;
178 struct pci_access *a = malloc(sizeof(struct pci_access));
181 memset(a, 0, sizeof(*a));
182 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0);
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;
189 pci_define_param(a, "hwdb.disable", "0", "Do not look up names in UDEV's HWDB if non-zero");
191 for (i=0; i<PCI_ACCESS_MAX; i++)
192 if (pci_methods[i] && pci_methods[i]->config)
193 pci_methods[i]->config(a);
198 pci_init_v35(struct pci_access *a)
201 a->error = pci_generic_error;
203 a->warning = pci_generic_warn;
205 a->debug = pci_generic_debug;
207 a->debug = pci_null_debug;
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];
218 for (i=0; probe_sequence[i] >= 0; i++)
220 struct pci_methods *m = pci_methods[probe_sequence[i]];
223 a->debug("Trying method %s...", m->name);
228 a->method = probe_sequence[i];
231 a->debug("...No.\n");
234 a->error("Cannot find any working access method.");
236 a->debug("Decided to use %s\n", a->methods->name);
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);
246 pci_cleanup(struct pci_access *a)
248 struct pci_dev *d, *e;
250 for (d=a->devices; d; d=e)
256 a->methods->cleanup(a);
257 pci_free_name_list(a);
259 pci_set_name_list_path(a, NULL, 0);