2 * The PCI Utilities -- Show Kernel Drivers
4 * Copyright (c) 1997--2013 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL v2+.
8 * SPDX-License-Identifier: GPL-2.0-or-later
18 #include <sys/utsname.h>
20 #ifdef PCI_USE_LIBKMOD
24 static struct kmod_ctx *kmod_ctx;
27 show_kernel_init(void)
29 static int show_kernel_inited = -1;
30 if (show_kernel_inited >= 0)
31 return show_kernel_inited;
33 kmod_ctx = kmod_new(NULL, NULL);
36 fprintf(stderr, "lspci: Unable to initialize libkmod context\n");
41 if ((err = kmod_load_resources(kmod_ctx)) < 0)
43 fprintf(stderr, "lspci: Unable to load libkmod resources: error %d\n", err);
47 show_kernel_inited = 1;
51 show_kernel_inited = 0;
56 show_kernel_cleanup(void)
62 static const char *next_module(struct device *d)
64 static struct kmod_list *klist, *kcurrent;
65 static struct kmod_module *kmodule;
69 kmod_module_unref(kmodule);
75 pci_fill_info(d->dev, PCI_FILL_MODULE_ALIAS);
76 if (!d->dev->module_alias)
78 int err = kmod_module_new_from_lookup(kmod_ctx, d->dev->module_alias, &klist);
81 fprintf(stderr, "lspci: libkmod lookup failed: error %d\n", err);
87 kcurrent = kmod_list_next(klist, kcurrent);
91 kmodule = kmod_module_get_module(kcurrent);
92 return kmod_module_get_name(kmodule);
95 kmod_module_unref_list(klist);
102 struct pcimap_entry {
103 struct pcimap_entry *next;
104 unsigned int vendor, device;
105 unsigned int subvendor, subdevice;
106 unsigned int class, class_mask;
110 static struct pcimap_entry *pcimap_head;
113 show_kernel_init(void)
115 static int tried_pcimap;
117 char *name, line[1024];
124 if (name = opt_pcimap)
126 f = fopen(name, "r");
128 die("Cannot open pcimap file %s: %m", name);
133 die("uname() failed: %m");
134 name = alloca(64 + strlen(uts.release));
135 sprintf(name, "/lib/modules/%s/modules.pcimap", uts.release);
136 f = fopen(name, "r");
141 while (fgets(line, sizeof(line), f))
143 char *c = strchr(line, '\n');
144 struct pcimap_entry *e;
147 die("Unterminated or too long line in %s", name);
149 if (!line[0] || line[0] == '#')
153 while (*c && *c != ' ' && *c != '\t')
156 continue; /* FIXME: Emit warnings! */
159 e = xmalloc(sizeof(*e) + strlen(line));
160 if (sscanf(c, "%i%i%i%i%i%i",
161 &e->vendor, &e->device,
162 &e->subvendor, &e->subdevice,
163 &e->class, &e->class_mask) != 6)
165 e->next = pcimap_head;
167 strcpy(e->module, line);
175 match_pcimap(struct device *d, struct pcimap_entry *e)
177 struct pci_dev *dev = d->dev;
178 unsigned int class = (((unsigned int)dev->device_class << 8) | dev->prog_if);
180 #define MATCH(x, y) ((y) > 0xffff || (x) == (y))
182 MATCH(dev->vendor_id, e->vendor) &&
183 MATCH(dev->device_id, e->device) &&
184 MATCH(dev->subsys_vendor_id, e->subvendor) &&
185 MATCH(dev->subsys_id, e->subdevice) &&
186 (class & e->class_mask) == e->class;
190 static const char *next_module(struct device *d)
192 static struct pcimap_entry *current;
195 current = pcimap_head;
197 current = current->next;
201 if (match_pcimap(d, current))
202 return current->module;
203 current = current->next;
210 show_kernel_cleanup(void)
217 next_module_filtered(struct device *d)
219 static char prev_module[256];
222 while (module = next_module(d))
224 if (strcmp(module, prev_module))
226 strncpy(prev_module, module, sizeof(prev_module));
227 prev_module[sizeof(prev_module) - 1] = 0;
236 show_kernel(struct device *d)
238 const char *driver, *module;
240 pci_fill_info(d->dev, PCI_FILL_DRIVER);
241 if (driver = pci_get_string_property(d->dev, PCI_FILL_DRIVER))
242 printf("\tKernel driver in use: %s\n", driver);
244 if (!show_kernel_init())
248 while (module = next_module_filtered(d))
249 printf("%s %s", (cnt++ ? "," : "\tKernel modules:"), module);
255 show_kernel_machine(struct device *d)
257 const char *driver, *module;
259 pci_fill_info(d->dev, PCI_FILL_DRIVER);
260 if (driver = pci_get_string_property(d->dev, PCI_FILL_DRIVER))
261 printf("Driver:\t%s\n", driver);
263 if (!show_kernel_init())
266 while (module = next_module_filtered(d))
267 printf("Module:\t%s\n", module);
273 show_kernel(struct device *d)
277 pci_fill_info(d->dev, PCI_FILL_DRIVER);
278 if (driver = pci_get_string_property(d->dev, PCI_FILL_DRIVER))
279 printf("\tDriver in use: %s\n", driver);
283 show_kernel_machine(struct device *d)
287 pci_fill_info(d->dev, PCI_FILL_DRIVER);
288 if (driver = pci_get_string_property(d->dev, PCI_FILL_DRIVER))
289 printf("Driver:\t%s\n", driver);
293 show_kernel_cleanup(void)