]> git.ipfire.org Git - thirdparty/pciutils.git/blame - ls-kernel.c
Fix stripping in cross-compiling mode
[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>
c7a34993
MM
11
12#include "lspci.h"
13
14#ifdef PCI_OS_LINUX
15
16#include <sys/utsname.h>
17
17ec7e70
MM
18#ifdef PCI_USE_LIBKMOD
19
20#include <libkmod.h>
21
22static struct kmod_ctx *kmod_ctx;
23
24static int
25show_kernel_init(void)
26{
27 static int show_kernel_inited = -1;
28 if (show_kernel_inited >= 0)
29 return show_kernel_inited;
30
cc840156 31 kmod_ctx = kmod_new(NULL, NULL);
17ec7e70
MM
32 if (!kmod_ctx)
33 {
34 fprintf(stderr, "lspci: Unable to initialize libkmod context\n");
35 goto failed;
36 }
37
38 int err;
39 if ((err = kmod_load_resources(kmod_ctx)) < 0)
40 {
41 fprintf(stderr, "lspci: Unable to load libkmod resources: error %d\n", err);
42 goto failed;
43 }
44
45 show_kernel_inited = 1;
46 return 1;
47
48failed:
49 show_kernel_inited = 0;
50 return 0;
51}
52
53void
54show_kernel_cleanup(void)
55{
56 if (kmod_ctx)
57 kmod_unref(kmod_ctx);
58}
59
60static const char *next_module(struct device *d)
61{
62 static struct kmod_list *klist, *kcurrent;
63 static struct kmod_module *kmodule;
64
65 if (kmodule)
66 {
67 kmod_module_unref(kmodule);
68 kmodule = NULL;
69 }
70
71 if (!klist)
72 {
73 pci_fill_info(d->dev, PCI_FILL_MODULE_ALIAS);
74 if (!d->dev->module_alias)
75 return NULL;
76 int err = kmod_module_new_from_lookup(kmod_ctx, d->dev->module_alias, &klist);
77 if (err < 0)
78 {
79 fprintf(stderr, "lspci: libkmod lookup failed: error %d\n", err);
80 return NULL;
81 }
82 kcurrent = klist;
83 }
84 else
85 kcurrent = kmod_list_next(klist, kcurrent);
86
87 if (kcurrent)
88 {
89 kmodule = kmod_module_get_module(kcurrent);
90 return kmod_module_get_name(kmodule);
91 }
92
93 kmod_module_unref_list(klist);
94 klist = NULL;
95 return NULL;
96}
97
98#else
99
c7a34993
MM
100struct pcimap_entry {
101 struct pcimap_entry *next;
102 unsigned int vendor, device;
103 unsigned int subvendor, subdevice;
104 unsigned int class, class_mask;
105 char module[1];
106};
107
108static struct pcimap_entry *pcimap_head;
109
17ec7e70
MM
110static int
111show_kernel_init(void)
c7a34993
MM
112{
113 static int tried_pcimap;
114 struct utsname uts;
115 char *name, line[1024];
116 FILE *f;
117
118 if (tried_pcimap)
17ec7e70 119 return 1;
c7a34993
MM
120 tried_pcimap = 1;
121
122 if (name = opt_pcimap)
123 {
124 f = fopen(name, "r");
125 if (!f)
126 die("Cannot open pcimap file %s: %m", name);
127 }
128 else
129 {
130 if (uname(&uts) < 0)
131 die("uname() failed: %m");
132 name = alloca(64 + strlen(uts.release));
133 sprintf(name, "/lib/modules/%s/modules.pcimap", uts.release);
134 f = fopen(name, "r");
135 if (!f)
17ec7e70 136 return 1;
c7a34993
MM
137 }
138
139 while (fgets(line, sizeof(line), f))
140 {
141 char *c = strchr(line, '\n');
142 struct pcimap_entry *e;
143
144 if (!c)
145 die("Unterminated or too long line in %s", name);
146 *c = 0;
147 if (!line[0] || line[0] == '#')
148 continue;
149
150 c = line;
151 while (*c && *c != ' ' && *c != '\t')
152 c++;
153 if (!*c)
154 continue; /* FIXME: Emit warnings! */
155 *c++ = 0;
156
157 e = xmalloc(sizeof(*e) + strlen(line));
158 if (sscanf(c, "%i%i%i%i%i%i",
159 &e->vendor, &e->device,
160 &e->subvendor, &e->subdevice,
161 &e->class, &e->class_mask) != 6)
162 continue;
163 e->next = pcimap_head;
164 pcimap_head = e;
165 strcpy(e->module, line);
166 }
167 fclose(f);
17ec7e70
MM
168
169 return 1;
c7a34993
MM
170}
171
172static int
173match_pcimap(struct device *d, struct pcimap_entry *e)
174{
175 struct pci_dev *dev = d->dev;
fb570ee3 176 unsigned int class = (((unsigned int)dev->device_class << 8) | dev->prog_if);
c7a34993
MM
177
178#define MATCH(x, y) ((y) > 0xffff || (x) == (y))
c7a34993
MM
179 return
180 MATCH(dev->vendor_id, e->vendor) &&
181 MATCH(dev->device_id, e->device) &&
fb570ee3
PR
182 MATCH(dev->subsys_vendor_id, e->subvendor) &&
183 MATCH(dev->subsys_id, e->subdevice) &&
c7a34993
MM
184 (class & e->class_mask) == e->class;
185#undef MATCH
186}
187
17ec7e70
MM
188static const char *next_module(struct device *d)
189{
b069b79a 190 static struct pcimap_entry *current;
17ec7e70
MM
191
192 if (!current)
b069b79a 193 current = pcimap_head;
17ec7e70
MM
194 else
195 current = current->next;
196
197 while (current)
198 {
b069b79a 199 if (match_pcimap(d, current))
17ec7e70
MM
200 return current->module;
201 current = current->next;
202 }
203
204 return NULL;
205}
206
207void
208show_kernel_cleanup(void)
209{
210}
211
212#endif
213
b069b79a
MM
214static const char *
215next_module_filtered(struct device *d)
216{
217 static char prev_module[256];
218 const char *module;
219
220 while (module = next_module(d))
221 {
222 if (strcmp(module, prev_module))
223 {
224 strncpy(prev_module, module, sizeof(prev_module));
225 prev_module[sizeof(prev_module) - 1] = 0;
226 return module;
227 }
228 }
229 prev_module[0] = 0;
230 return NULL;
231}
232
c7a34993
MM
233void
234show_kernel(struct device *d)
235{
17ec7e70 236 const char *driver, *module;
c7a34993 237
d994587f
PR
238 pci_fill_info(d->dev, PCI_FILL_DRIVER);
239 if (driver = pci_get_string_property(d->dev, PCI_FILL_DRIVER))
c7a34993
MM
240 printf("\tKernel driver in use: %s\n", driver);
241
17ec7e70
MM
242 if (!show_kernel_init())
243 return;
244
245 int cnt = 0;
b069b79a 246 while (module = next_module_filtered(d))
17ec7e70
MM
247 printf("%s %s", (cnt++ ? "," : "\tKernel modules:"), module);
248 if (cnt)
c7a34993
MM
249 putchar('\n');
250}
251
252void
253show_kernel_machine(struct device *d)
254{
17ec7e70 255 const char *driver, *module;
c7a34993 256
d994587f
PR
257 pci_fill_info(d->dev, PCI_FILL_DRIVER);
258 if (driver = pci_get_string_property(d->dev, PCI_FILL_DRIVER))
c7a34993
MM
259 printf("Driver:\t%s\n", driver);
260
17ec7e70
MM
261 if (!show_kernel_init())
262 return;
263
b069b79a 264 while (module = next_module_filtered(d))
17ec7e70 265 printf("Module:\t%s\n", module);
c7a34993
MM
266}
267
268#else
269
270void
d994587f 271show_kernel(struct device *d)
c7a34993 272{
d994587f
PR
273 const char *driver;
274
275 pci_fill_info(d->dev, PCI_FILL_DRIVER);
276 if (driver = pci_get_string_property(d->dev, PCI_FILL_DRIVER))
277 printf("\tDriver in use: %s\n", driver);
c7a34993
MM
278}
279
280void
d994587f 281show_kernel_machine(struct device *d)
c7a34993 282{
d994587f
PR
283 const char *driver;
284
285 pci_fill_info(d->dev, PCI_FILL_DRIVER);
286 if (driver = pci_get_string_property(d->dev, PCI_FILL_DRIVER))
287 printf("Driver:\t%s\n", driver);
c7a34993
MM
288}
289
17ec7e70
MM
290void
291show_kernel_cleanup(void)
292{
293}
294
c7a34993
MM
295#endif
296