summaryrefslogtreecommitdiff
path: root/ls-kernel.c
blob: b90a469f7056f69c5d3adc72b1674002b7aecedf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 *	The PCI Utilities -- Show Kernel Drivers
 *
 *	Copyright (c) 1997--2013 Martin Mares <mj@ucw.cz>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include <stdio.h>
#include <string.h>

#include "lspci.h"

#ifdef PCI_OS_LINUX

#include <sys/utsname.h>

#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;

  kmod_ctx = kmod_new(NULL, 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

struct pcimap_entry {
  struct pcimap_entry *next;
  unsigned int vendor, device;
  unsigned int subvendor, subdevice;
  unsigned int class, class_mask;
  char module[1];
};

static struct pcimap_entry *pcimap_head;

static int
show_kernel_init(void)
{
  static int tried_pcimap;
  struct utsname uts;
  char *name, line[1024];
  FILE *f;

  if (tried_pcimap)
    return 1;
  tried_pcimap = 1;

  if (name = opt_pcimap)
    {
      f = fopen(name, "r");
      if (!f)
	die("Cannot open pcimap file %s: %m", name);
    }
  else
    {
      if (uname(&uts) < 0)
	die("uname() failed: %m");
      name = alloca(64 + strlen(uts.release));
      sprintf(name, "/lib/modules/%s/modules.pcimap", uts.release);
      f = fopen(name, "r");
      if (!f)
	return 1;
    }

  while (fgets(line, sizeof(line), f))
    {
      char *c = strchr(line, '\n');
      struct pcimap_entry *e;

      if (!c)
	die("Unterminated or too long line in %s", name);
      *c = 0;
      if (!line[0] || line[0] == '#')
	continue;

      c = line;
      while (*c && *c != ' ' && *c != '\t')
	c++;
      if (!*c)
	continue;	/* FIXME: Emit warnings! */
      *c++ = 0;

      e = xmalloc(sizeof(*e) + strlen(line));
      if (sscanf(c, "%i%i%i%i%i%i",
		 &e->vendor, &e->device,
		 &e->subvendor, &e->subdevice,
		 &e->class, &e->class_mask) != 6)
	continue;
      e->next = pcimap_head;
      pcimap_head = e;
      strcpy(e->module, line);
    }
  fclose(f);

  return 1;
}

static int
match_pcimap(struct device *d, struct pcimap_entry *e)
{
  struct pci_dev *dev = d->dev;
  unsigned int class = (((unsigned int)dev->device_class << 8) | dev->prog_if);

#define MATCH(x, y) ((y) > 0xffff || (x) == (y))
  return
    MATCH(dev->vendor_id, e->vendor) &&
    MATCH(dev->device_id, e->device) &&
    MATCH(dev->subsys_vendor_id, e->subvendor) &&
    MATCH(dev->subsys_id, e->subdevice) &&
    (class & e->class_mask) == e->class;
#undef MATCH
}

static const char *next_module(struct device *d)
{
  static struct pcimap_entry *current;

  if (!current)
    current = pcimap_head;
  else
    current = current->next;

  while (current)
    {
      if (match_pcimap(d, current))
	return current->module;
      current = current->next;
    }

  return NULL;
}

void
show_kernel_cleanup(void)
{
}

#endif

static const char *
next_module_filtered(struct device *d)
{
  static char prev_module[256];
  const char *module;

  while (module = next_module(d))
    {
      if (strcmp(module, prev_module))
	{
	  strncpy(prev_module, module, sizeof(prev_module));
	  prev_module[sizeof(prev_module) - 1] = 0;
	  return module;
	}
    }
  prev_module[0] = 0;
  return NULL;
}

void
show_kernel(struct device *d)
{
  const char *driver, *module;

  pci_fill_info(d->dev, PCI_FILL_DRIVER);
  if (driver = pci_get_string_property(d->dev, PCI_FILL_DRIVER))
    printf("\tKernel driver in use: %s\n", driver);

  if (!show_kernel_init())
    return;

  int cnt = 0;
  while (module = next_module_filtered(d))
    printf("%s %s", (cnt++ ? "," : "\tKernel modules:"), module);
  if (cnt)
    putchar('\n');
}

void
show_kernel_machine(struct device *d)
{
  const char *driver, *module;

  pci_fill_info(d->dev, PCI_FILL_DRIVER);
  if (driver = pci_get_string_property(d->dev, PCI_FILL_DRIVER))
    printf("Driver:\t%s\n", driver);

  if (!show_kernel_init())
    return;

  while (module = next_module_filtered(d))
    printf("Module:\t%s\n", module);
}

#else

void
show_kernel(struct device *d)
{
  const char *driver;

  pci_fill_info(d->dev, PCI_FILL_DRIVER);
  if (driver = pci_get_string_property(d->dev, PCI_FILL_DRIVER))
    printf("\tDriver in use: %s\n", driver);
}

void
show_kernel_machine(struct device *d)
{
  const char *driver;

  pci_fill_info(d->dev, PCI_FILL_DRIVER);
  if (driver = pci_get_string_property(d->dev, PCI_FILL_DRIVER))
    printf("Driver:\t%s\n", driver);
}

void
show_kernel_cleanup(void)
{
}

#endif