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.
16 #include <sys/utsname.h>
18 #ifdef PCI_USE_LIBKMOD
22 static struct kmod_ctx *kmod_ctx;
25 show_kernel_init(void)
27 static int show_kernel_inited = -1;
28 if (show_kernel_inited >= 0)
29 return show_kernel_inited;
31 kmod_ctx = kmod_new(NULL, NULL);
34 fprintf(stderr, "lspci: Unable to initialize libkmod context\n");
39 if ((err = kmod_load_resources(kmod_ctx)) < 0)
41 fprintf(stderr, "lspci: Unable to load libkmod resources: error %d\n", err);
45 show_kernel_inited = 1;
49 show_kernel_inited = 0;
54 show_kernel_cleanup(void)
60 static const char *next_module(struct device *d)
62 static struct kmod_list *klist, *kcurrent;
63 static struct kmod_module *kmodule;
67 kmod_module_unref(kmodule);
73 pci_fill_info(d->dev, PCI_FILL_MODULE_ALIAS);
74 if (!d->dev->module_alias)
76 int err = kmod_module_new_from_lookup(kmod_ctx, d->dev->module_alias, &klist);
79 fprintf(stderr, "lspci: libkmod lookup failed: error %d\n", err);
85 kcurrent = kmod_list_next(klist, kcurrent);
89 kmodule = kmod_module_get_module(kcurrent);
90 return kmod_module_get_name(kmodule);
93 kmod_module_unref_list(klist);
100 struct pcimap_entry {
101 struct pcimap_entry *next;
102 unsigned int vendor, device;
103 unsigned int subvendor, subdevice;
104 unsigned int class, class_mask;
108 static struct pcimap_entry *pcimap_head;
111 show_kernel_init(void)
113 static int tried_pcimap;
115 char *name, line[1024];
122 if (name = opt_pcimap)
124 f = fopen(name, "r");
126 die("Cannot open pcimap file %s: %m", name);
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");
139 while (fgets(line, sizeof(line), f))
141 char *c = strchr(line, '\n');
142 struct pcimap_entry *e;
145 die("Unterminated or too long line in %s", name);
147 if (!line[0] || line[0] == '#')
151 while (*c && *c != ' ' && *c != '\t')
154 continue; /* FIXME: Emit warnings! */
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)
163 e->next = pcimap_head;
165 strcpy(e->module, line);
173 match_pcimap(struct device *d, struct pcimap_entry *e)
175 struct pci_dev *dev = d->dev;
176 unsigned int class = get_conf_long(d, PCI_REVISION_ID) >> 8;
179 #define MATCH(x, y) ((y) > 0xffff || (x) == (y))
180 get_subid(d, &subv, &subd);
182 MATCH(dev->vendor_id, e->vendor) &&
183 MATCH(dev->device_id, e->device) &&
184 MATCH(subv, e->subvendor) &&
185 MATCH(subd, 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)
216 #define DRIVER_BUF_SIZE 1024
219 find_driver(struct device *d, char *buf)
221 struct pci_dev *dev = d->dev;
222 char name[1024], *drv, *base;
225 if (dev->access->method != PCI_ACCESS_SYS_BUS_PCI)
228 base = pci_get_param(dev->access, "sysfs.path");
229 if (!base || !base[0])
232 n = snprintf(name, sizeof(name), "%s/devices/%04x:%02x:%02x.%d/driver",
233 base, dev->domain, dev->bus, dev->dev, dev->func);
234 if (n < 0 || n >= (int)sizeof(name))
235 die("show_driver: sysfs device name too long, why?");
237 n = readlink(name, buf, DRIVER_BUF_SIZE);
240 if (n >= DRIVER_BUF_SIZE)
241 return "<name-too-long>";
244 if (drv = strrchr(buf, '/'))
251 next_module_filtered(struct device *d)
253 static char prev_module[256];
256 while (module = next_module(d))
258 if (strcmp(module, prev_module))
260 strncpy(prev_module, module, sizeof(prev_module));
261 prev_module[sizeof(prev_module) - 1] = 0;
270 show_kernel(struct device *d)
272 char buf[DRIVER_BUF_SIZE];
273 const char *driver, *module;
275 if (driver = find_driver(d, buf))
276 printf("\tKernel driver in use: %s\n", driver);
278 if (!show_kernel_init())
282 while (module = next_module_filtered(d))
283 printf("%s %s", (cnt++ ? "," : "\tKernel modules:"), module);
289 show_kernel_machine(struct device *d)
291 char buf[DRIVER_BUF_SIZE];
292 const char *driver, *module;
294 if (driver = find_driver(d, buf))
295 printf("Driver:\t%s\n", driver);
297 if (!show_kernel_init())
300 while (module = next_module_filtered(d))
301 printf("Module:\t%s\n", module);
307 show_kernel(struct device *d UNUSED)
312 show_kernel_machine(struct device *d UNUSED)
317 show_kernel_cleanup(void)