]>
Commit | Line | Data |
---|---|---|
c7a34993 MM |
1 | /* |
2 | * The PCI Utilities -- Show Kernel Drivers | |
3 | * | |
17ec7e70 | 4 | * Copyright (c) 1997--2013 Martin Mares <mj@ucw.cz> |
c7a34993 MM |
5 | * |
6 | * Can be freely distributed and used under the terms of the GNU GPL. | |
7 | */ | |
8 | ||
9 | #include <stdio.h> | |
10 | #include <string.h> | |
11 | #include <unistd.h> | |
12 | ||
13 | #include "lspci.h" | |
14 | ||
15 | #ifdef PCI_OS_LINUX | |
16 | ||
17 | #include <sys/utsname.h> | |
18 | ||
17ec7e70 MM |
19 | #ifdef PCI_USE_LIBKMOD |
20 | ||
21 | #include <libkmod.h> | |
22 | ||
23 | static struct kmod_ctx *kmod_ctx; | |
24 | ||
25 | static int | |
26 | show_kernel_init(void) | |
27 | { | |
28 | static int show_kernel_inited = -1; | |
29 | if (show_kernel_inited >= 0) | |
30 | return show_kernel_inited; | |
31 | ||
32 | struct utsname uts; | |
33 | if (uname(&uts) < 0) | |
34 | die("uname() failed: %m"); | |
35 | char *name = alloca(64 + strlen(uts.release)); | |
36 | sprintf(name, "/lib/modules/%s", uts.release); | |
37 | ||
38 | kmod_ctx = kmod_new(name, NULL); | |
39 | if (!kmod_ctx) | |
40 | { | |
41 | fprintf(stderr, "lspci: Unable to initialize libkmod context\n"); | |
42 | goto failed; | |
43 | } | |
44 | ||
45 | int err; | |
46 | if ((err = kmod_load_resources(kmod_ctx)) < 0) | |
47 | { | |
48 | fprintf(stderr, "lspci: Unable to load libkmod resources: error %d\n", err); | |
49 | goto failed; | |
50 | } | |
51 | ||
52 | show_kernel_inited = 1; | |
53 | return 1; | |
54 | ||
55 | failed: | |
56 | show_kernel_inited = 0; | |
57 | return 0; | |
58 | } | |
59 | ||
60 | void | |
61 | show_kernel_cleanup(void) | |
62 | { | |
63 | if (kmod_ctx) | |
64 | kmod_unref(kmod_ctx); | |
65 | } | |
66 | ||
67 | static const char *next_module(struct device *d) | |
68 | { | |
69 | static struct kmod_list *klist, *kcurrent; | |
70 | static struct kmod_module *kmodule; | |
71 | ||
72 | if (kmodule) | |
73 | { | |
74 | kmod_module_unref(kmodule); | |
75 | kmodule = NULL; | |
76 | } | |
77 | ||
78 | if (!klist) | |
79 | { | |
80 | pci_fill_info(d->dev, PCI_FILL_MODULE_ALIAS); | |
81 | if (!d->dev->module_alias) | |
82 | return NULL; | |
83 | int err = kmod_module_new_from_lookup(kmod_ctx, d->dev->module_alias, &klist); | |
84 | if (err < 0) | |
85 | { | |
86 | fprintf(stderr, "lspci: libkmod lookup failed: error %d\n", err); | |
87 | return NULL; | |
88 | } | |
89 | kcurrent = klist; | |
90 | } | |
91 | else | |
92 | kcurrent = kmod_list_next(klist, kcurrent); | |
93 | ||
94 | if (kcurrent) | |
95 | { | |
96 | kmodule = kmod_module_get_module(kcurrent); | |
97 | return kmod_module_get_name(kmodule); | |
98 | } | |
99 | ||
100 | kmod_module_unref_list(klist); | |
101 | klist = NULL; | |
102 | return NULL; | |
103 | } | |
104 | ||
105 | #else | |
106 | ||
c7a34993 MM |
107 | struct pcimap_entry { |
108 | struct pcimap_entry *next; | |
109 | unsigned int vendor, device; | |
110 | unsigned int subvendor, subdevice; | |
111 | unsigned int class, class_mask; | |
112 | char module[1]; | |
113 | }; | |
114 | ||
115 | static struct pcimap_entry *pcimap_head; | |
116 | ||
17ec7e70 MM |
117 | static int |
118 | show_kernel_init(void) | |
c7a34993 MM |
119 | { |
120 | static int tried_pcimap; | |
121 | struct utsname uts; | |
122 | char *name, line[1024]; | |
123 | FILE *f; | |
124 | ||
125 | if (tried_pcimap) | |
17ec7e70 | 126 | return 1; |
c7a34993 MM |
127 | tried_pcimap = 1; |
128 | ||
129 | if (name = opt_pcimap) | |
130 | { | |
131 | f = fopen(name, "r"); | |
132 | if (!f) | |
133 | die("Cannot open pcimap file %s: %m", name); | |
134 | } | |
135 | else | |
136 | { | |
137 | if (uname(&uts) < 0) | |
138 | die("uname() failed: %m"); | |
139 | name = alloca(64 + strlen(uts.release)); | |
140 | sprintf(name, "/lib/modules/%s/modules.pcimap", uts.release); | |
141 | f = fopen(name, "r"); | |
142 | if (!f) | |
17ec7e70 | 143 | return 1; |
c7a34993 MM |
144 | } |
145 | ||
146 | while (fgets(line, sizeof(line), f)) | |
147 | { | |
148 | char *c = strchr(line, '\n'); | |
149 | struct pcimap_entry *e; | |
150 | ||
151 | if (!c) | |
152 | die("Unterminated or too long line in %s", name); | |
153 | *c = 0; | |
154 | if (!line[0] || line[0] == '#') | |
155 | continue; | |
156 | ||
157 | c = line; | |
158 | while (*c && *c != ' ' && *c != '\t') | |
159 | c++; | |
160 | if (!*c) | |
161 | continue; /* FIXME: Emit warnings! */ | |
162 | *c++ = 0; | |
163 | ||
164 | e = xmalloc(sizeof(*e) + strlen(line)); | |
165 | if (sscanf(c, "%i%i%i%i%i%i", | |
166 | &e->vendor, &e->device, | |
167 | &e->subvendor, &e->subdevice, | |
168 | &e->class, &e->class_mask) != 6) | |
169 | continue; | |
170 | e->next = pcimap_head; | |
171 | pcimap_head = e; | |
172 | strcpy(e->module, line); | |
173 | } | |
174 | fclose(f); | |
17ec7e70 MM |
175 | |
176 | return 1; | |
c7a34993 MM |
177 | } |
178 | ||
179 | static int | |
180 | match_pcimap(struct device *d, struct pcimap_entry *e) | |
181 | { | |
182 | struct pci_dev *dev = d->dev; | |
183 | unsigned int class = get_conf_long(d, PCI_REVISION_ID) >> 8; | |
184 | word subv, subd; | |
185 | ||
186 | #define MATCH(x, y) ((y) > 0xffff || (x) == (y)) | |
187 | get_subid(d, &subv, &subd); | |
188 | return | |
189 | MATCH(dev->vendor_id, e->vendor) && | |
190 | MATCH(dev->device_id, e->device) && | |
191 | MATCH(subv, e->subvendor) && | |
192 | MATCH(subd, e->subdevice) && | |
193 | (class & e->class_mask) == e->class; | |
194 | #undef MATCH | |
195 | } | |
196 | ||
17ec7e70 MM |
197 | static const char *next_module(struct device *d) |
198 | { | |
b069b79a | 199 | static struct pcimap_entry *current; |
17ec7e70 MM |
200 | |
201 | if (!current) | |
b069b79a | 202 | current = pcimap_head; |
17ec7e70 MM |
203 | else |
204 | current = current->next; | |
205 | ||
206 | while (current) | |
207 | { | |
b069b79a | 208 | if (match_pcimap(d, current)) |
17ec7e70 MM |
209 | return current->module; |
210 | current = current->next; | |
211 | } | |
212 | ||
213 | return NULL; | |
214 | } | |
215 | ||
216 | void | |
217 | show_kernel_cleanup(void) | |
218 | { | |
219 | } | |
220 | ||
221 | #endif | |
222 | ||
c7a34993 MM |
223 | #define DRIVER_BUF_SIZE 1024 |
224 | ||
225 | static char * | |
226 | find_driver(struct device *d, char *buf) | |
227 | { | |
228 | struct pci_dev *dev = d->dev; | |
229 | char name[1024], *drv, *base; | |
230 | int n; | |
231 | ||
232 | if (dev->access->method != PCI_ACCESS_SYS_BUS_PCI) | |
233 | return NULL; | |
234 | ||
235 | base = pci_get_param(dev->access, "sysfs.path"); | |
236 | if (!base || !base[0]) | |
237 | return NULL; | |
238 | ||
239 | n = snprintf(name, sizeof(name), "%s/devices/%04x:%02x:%02x.%d/driver", | |
240 | base, dev->domain, dev->bus, dev->dev, dev->func); | |
241 | if (n < 0 || n >= (int)sizeof(name)) | |
242 | die("show_driver: sysfs device name too long, why?"); | |
243 | ||
244 | n = readlink(name, buf, DRIVER_BUF_SIZE); | |
245 | if (n < 0) | |
246 | return NULL; | |
247 | if (n >= DRIVER_BUF_SIZE) | |
248 | return "<name-too-long>"; | |
249 | buf[n] = 0; | |
250 | ||
251 | if (drv = strrchr(buf, '/')) | |
252 | return drv+1; | |
253 | else | |
254 | return buf; | |
255 | } | |
256 | ||
b069b79a MM |
257 | static const char * |
258 | next_module_filtered(struct device *d) | |
259 | { | |
260 | static char prev_module[256]; | |
261 | const char *module; | |
262 | ||
263 | while (module = next_module(d)) | |
264 | { | |
265 | if (strcmp(module, prev_module)) | |
266 | { | |
267 | strncpy(prev_module, module, sizeof(prev_module)); | |
268 | prev_module[sizeof(prev_module) - 1] = 0; | |
269 | return module; | |
270 | } | |
271 | } | |
272 | prev_module[0] = 0; | |
273 | return NULL; | |
274 | } | |
275 | ||
c7a34993 MM |
276 | void |
277 | show_kernel(struct device *d) | |
278 | { | |
279 | char buf[DRIVER_BUF_SIZE]; | |
17ec7e70 | 280 | const char *driver, *module; |
c7a34993 MM |
281 | |
282 | if (driver = find_driver(d, buf)) | |
283 | printf("\tKernel driver in use: %s\n", driver); | |
284 | ||
17ec7e70 MM |
285 | if (!show_kernel_init()) |
286 | return; | |
287 | ||
288 | int cnt = 0; | |
b069b79a | 289 | while (module = next_module_filtered(d)) |
17ec7e70 MM |
290 | printf("%s %s", (cnt++ ? "," : "\tKernel modules:"), module); |
291 | if (cnt) | |
c7a34993 MM |
292 | putchar('\n'); |
293 | } | |
294 | ||
295 | void | |
296 | show_kernel_machine(struct device *d) | |
297 | { | |
298 | char buf[DRIVER_BUF_SIZE]; | |
17ec7e70 | 299 | const char *driver, *module; |
c7a34993 MM |
300 | |
301 | if (driver = find_driver(d, buf)) | |
302 | printf("Driver:\t%s\n", driver); | |
303 | ||
17ec7e70 MM |
304 | if (!show_kernel_init()) |
305 | return; | |
306 | ||
b069b79a | 307 | while (module = next_module_filtered(d)) |
17ec7e70 | 308 | printf("Module:\t%s\n", module); |
c7a34993 MM |
309 | } |
310 | ||
311 | #else | |
312 | ||
313 | void | |
314 | show_kernel(struct device *d UNUSED) | |
315 | { | |
316 | } | |
317 | ||
318 | void | |
319 | show_kernel_machine(struct device *d UNUSED) | |
320 | { | |
321 | } | |
322 | ||
17ec7e70 MM |
323 | void |
324 | show_kernel_cleanup(void) | |
325 | { | |
326 | } | |
327 | ||
c7a34993 MM |
328 | #endif |
329 |