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