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