]> git.ipfire.org Git - thirdparty/pciutils.git/blame - ls-kernel.c
On newer Linux systems, use libkmod to look up kernel modules
[thirdparty/pciutils.git] / ls-kernel.c
CommitLineData
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
23static struct kmod_ctx *kmod_ctx;
24
25static int
26show_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
55failed:
56 show_kernel_inited = 0;
57 return 0;
58}
59
60void
61show_kernel_cleanup(void)
62{
63 if (kmod_ctx)
64 kmod_unref(kmod_ctx);
65}
66
67static 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
107struct 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
115static struct pcimap_entry *pcimap_head;
116
17ec7e70
MM
117static int
118show_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
179static int
180match_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
197static const char *next_module(struct device *d)
198{
199 static struct pcimap_entry *current, *last_printed;
200
201 if (!current)
202 {
203 current = pcimap_head;
204 last_printed = NULL;
205 }
206 else
207 current = current->next;
208
209 while (current)
210 {
211 if (match_pcimap(d, current) && (!last_printed || strcmp(last_printed->module, current->module)))
212 return current->module;
213 current = current->next;
214 }
215
216 return NULL;
217}
218
219void
220show_kernel_cleanup(void)
221{
222}
223
224#endif
225
c7a34993
MM
226#define DRIVER_BUF_SIZE 1024
227
228static char *
229find_driver(struct device *d, char *buf)
230{
231 struct pci_dev *dev = d->dev;
232 char name[1024], *drv, *base;
233 int n;
234
235 if (dev->access->method != PCI_ACCESS_SYS_BUS_PCI)
236 return NULL;
237
238 base = pci_get_param(dev->access, "sysfs.path");
239 if (!base || !base[0])
240 return NULL;
241
242 n = snprintf(name, sizeof(name), "%s/devices/%04x:%02x:%02x.%d/driver",
243 base, dev->domain, dev->bus, dev->dev, dev->func);
244 if (n < 0 || n >= (int)sizeof(name))
245 die("show_driver: sysfs device name too long, why?");
246
247 n = readlink(name, buf, DRIVER_BUF_SIZE);
248 if (n < 0)
249 return NULL;
250 if (n >= DRIVER_BUF_SIZE)
251 return "<name-too-long>";
252 buf[n] = 0;
253
254 if (drv = strrchr(buf, '/'))
255 return drv+1;
256 else
257 return buf;
258}
259
260void
261show_kernel(struct device *d)
262{
263 char buf[DRIVER_BUF_SIZE];
17ec7e70 264 const char *driver, *module;
c7a34993
MM
265
266 if (driver = find_driver(d, buf))
267 printf("\tKernel driver in use: %s\n", driver);
268
17ec7e70
MM
269 if (!show_kernel_init())
270 return;
271
272 int cnt = 0;
273 while (module = next_module(d))
274 printf("%s %s", (cnt++ ? "," : "\tKernel modules:"), module);
275 if (cnt)
c7a34993
MM
276 putchar('\n');
277}
278
279void
280show_kernel_machine(struct device *d)
281{
282 char buf[DRIVER_BUF_SIZE];
17ec7e70 283 const char *driver, *module;
c7a34993
MM
284
285 if (driver = find_driver(d, buf))
286 printf("Driver:\t%s\n", driver);
287
17ec7e70
MM
288 if (!show_kernel_init())
289 return;
290
291 while (module = next_module(d))
292 printf("Module:\t%s\n", module);
c7a34993
MM
293}
294
295#else
296
297void
298show_kernel(struct device *d UNUSED)
299{
300}
301
302void
303show_kernel_machine(struct device *d UNUSED)
304{
305}
306
17ec7e70
MM
307void
308show_kernel_cleanup(void)
309{
310}
311
c7a34993
MM
312#endif
313