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