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