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