]>
Commit | Line | Data |
---|---|---|
4dd39346 MM |
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 | |
7cb1afbe | 60 | #ifdef PCI_HAVE_PM_DARWIN_DEVICE |
66e0afd0 | 61 | &pm_darwin, |
7cb1afbe RY |
62 | #else |
63 | NULL, | |
64 | #endif | |
4dd39346 MM |
65 | }; |
66 | ||
4dd39346 MM |
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 | ||
fa182368 | 84 | char * |
ac357d3b | 85 | pci_strdup(struct pci_access *a, const char *s) |
fa182368 MM |
86 | { |
87 | int len = strlen(s) + 1; | |
88 | char *t = pci_malloc(a, len); | |
89 | memcpy(t, s, len); | |
90 | return t; | |
91 | } | |
92 | ||
4dd39346 MM |
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 | ||
9ff67879 MM |
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 | ||
d6dcc545 MM |
153 | struct pci_access * |
154 | pci_alloc(void) | |
fa182368 | 155 | { |
d6dcc545 MM |
156 | struct pci_access *a = malloc(sizeof(struct pci_access)); |
157 | int i; | |
fa182368 | 158 | |
d6dcc545 MM |
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; | |
ac357d3b MM |
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"); | |
d6dcc545 | 168 | #endif |
de7ef8bc | 169 | for (i=0; i<PCI_ACCESS_MAX; i++) |
d6dcc545 MM |
170 | if (pci_methods[i] && pci_methods[i]->config) |
171 | pci_methods[i]->config(a); | |
172 | return a; | |
fa182368 MM |
173 | } |
174 | ||
4dd39346 MM |
175 | void |
176 | pci_init(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; | |
de7ef8bc | 196 | for (i=0; i<PCI_ACCESS_MAX; i++) |
4dd39346 MM |
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 | void | |
217 | pci_cleanup(struct pci_access *a) | |
218 | { | |
219 | struct pci_dev *d, *e; | |
220 | ||
de7ef8bc | 221 | for (d=a->devices; d; d=e) |
4dd39346 MM |
222 | { |
223 | e = d->next; | |
224 | pci_free_dev(d); | |
225 | } | |
226 | if (a->methods) | |
227 | a->methods->cleanup(a); | |
228 | pci_free_name_list(a); | |
fa182368 | 229 | pci_free_params(a); |
4dd39346 | 230 | pci_set_name_list_path(a, NULL, 0); |
4dd39346 MM |
231 | pci_mfree(a); |
232 | } |