+#ifdef PCI_USE_LIBKMOD
+
+#include <libkmod.h>
+
+static struct kmod_ctx *kmod_ctx;
+
+static int
+show_kernel_init(void)
+{
+ static int show_kernel_inited = -1;
+ if (show_kernel_inited >= 0)
+ return show_kernel_inited;
+
+ struct utsname uts;
+ if (uname(&uts) < 0)
+ die("uname() failed: %m");
+ char *name = alloca(64 + strlen(uts.release));
+ sprintf(name, "/lib/modules/%s", uts.release);
+
+ kmod_ctx = kmod_new(name, NULL);
+ if (!kmod_ctx)
+ {
+ fprintf(stderr, "lspci: Unable to initialize libkmod context\n");
+ goto failed;
+ }
+
+ int err;
+ if ((err = kmod_load_resources(kmod_ctx)) < 0)
+ {
+ fprintf(stderr, "lspci: Unable to load libkmod resources: error %d\n", err);
+ goto failed;
+ }
+
+ show_kernel_inited = 1;
+ return 1;
+
+failed:
+ show_kernel_inited = 0;
+ return 0;
+}
+
+void
+show_kernel_cleanup(void)
+{
+ if (kmod_ctx)
+ kmod_unref(kmod_ctx);
+}
+
+static const char *next_module(struct device *d)
+{
+ static struct kmod_list *klist, *kcurrent;
+ static struct kmod_module *kmodule;
+
+ if (kmodule)
+ {
+ kmod_module_unref(kmodule);
+ kmodule = NULL;
+ }
+
+ if (!klist)
+ {
+ pci_fill_info(d->dev, PCI_FILL_MODULE_ALIAS);
+ if (!d->dev->module_alias)
+ return NULL;
+ int err = kmod_module_new_from_lookup(kmod_ctx, d->dev->module_alias, &klist);
+ if (err < 0)
+ {
+ fprintf(stderr, "lspci: libkmod lookup failed: error %d\n", err);
+ return NULL;
+ }
+ kcurrent = klist;
+ }
+ else
+ kcurrent = kmod_list_next(klist, kcurrent);
+
+ if (kcurrent)
+ {
+ kmodule = kmod_module_get_module(kcurrent);
+ return kmod_module_get_name(kmodule);
+ }
+
+ kmod_module_unref_list(klist);
+ klist = NULL;
+ return NULL;
+}
+
+#else
+