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