]> git.ipfire.org Git - thirdparty/pciutils.git/blame - lspci.c
Releasing as 1.06. All glibc 2.1 troubles should be gone ;-)
[thirdparty/pciutils.git] / lspci.c
CommitLineData
98e39e09 1/*
496d4021 2 * $Id: lspci.c,v 1.12 1998/06/08 07:54:37 mj Exp $
98e39e09
MM
3 *
4 * Linux PCI Utilities -- List All PCI Devices
5 *
18928b91 6 * Copyright (c) 1997, 1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
98e39e09
MM
7 *
8 * Can be freely distributed and used under the terms of the GNU GPL.
9 */
10
11#include <stdio.h>
12#include <string.h>
13#include <stdlib.h>
14#include <fcntl.h>
15#include <unistd.h>
98e39e09
MM
16
17#include "pciutils.h"
18
19/* Options */
20
21static int verbose; /* Show detailed information */
22static int buscentric_view; /* Show bus addresses/IRQ's instead of CPU-visible ones */
23static int show_hex; /* Show contents of config space as hexadecimal numbers */
e4842ff3 24static struct pci_filter filter; /* Device filter */
6d0dc0fd 25static int show_tree; /* Show bus tree */
0a33d0ec 26static int machine_readable; /* Generate machine-readable output */
18928b91 27static char *pci_dir = PROC_BUS_PCI;
98e39e09 28
e4842ff3 29static char options[] = "nvbxs:d:ti:p:m";
98e39e09
MM
30
31static char help_msg[] = "\
32Usage: lspci [<switches>]\n\
33\n\
e4842ff3
MM
34-v\t\tBe verbose\n\
35-n\t\tShow numeric ID's\n\
36-b\t\tBus-centric view (PCI addresses and IRQ's instead of those seen by the CPU)\n\
37-x\t\tShow hex-dump of config space (-xx shows full 256 bytes)\n\
38-s [[<bus>]:][<slot>][.[<func>]]\tShow only devices in selected slots\n\
39-d [<vendor>]:[<device>]\tShow only selected devices\n\
40-t\t\tShow bus tree\n\
41-m\t\tProduce machine-readable output\n\
18928b91
MM
42-i <file>\tUse specified ID database instead of " ETC_PCI_IDS "\n\
43-p <dir>\tUse specified bus directory instead of " PROC_BUS_PCI "\n\
98e39e09
MM
44";
45
f17b962b
MM
46/* Format strings used for IRQ numbers */
47
2f48f637 48#ifdef ARCH_SPARC64
f17b962b
MM
49#define IRQ_FORMAT "%08x"
50#else
51#define IRQ_FORMAT "%d"
52#endif
53
98e39e09
MM
54/* Our view of the PCI bus */
55
56struct device {
57 struct device *next;
58 byte bus, devfn;
59 word vendid, devid;
60 unsigned int kernel_irq;
b2c9b373 61 unsigned long kernel_base_addr[6], kernel_rom_base_addr;
98e39e09
MM
62 byte config[256];
63};
64
65static struct device *first_dev, **last_dev = &first_dev;
66
67/* Miscellaneous routines */
68
69void *
70xmalloc(unsigned int howmuch)
71{
72 void *p = malloc(howmuch);
73 if (!p)
74 {
75 fprintf(stderr, "lspci: Unable to allocate %d bytes of memory\n", howmuch);
76 exit(1);
77 }
78 return p;
79}
80
98e39e09
MM
81/* Interface for /proc/bus/pci */
82
83static void
84scan_dev_list(void)
85{
86 FILE *f;
87 byte line[256];
18928b91 88 byte name[256];
98e39e09 89
18928b91
MM
90 sprintf(name, "%s/devices", pci_dir);
91 if (! (f = fopen(name, "r")))
98e39e09 92 {
18928b91 93 perror(name);
98e39e09
MM
94 exit(1);
95 }
96 while (fgets(line, sizeof(line), f))
97 {
98 struct device *d = xmalloc(sizeof(struct device));
99 unsigned int dfn, vend;
100
b2c9b373
MM
101 bzero(d, sizeof(*d));
102 sscanf(line, "%x %x %x %lx %lx %lx %lx %lx %lx %lx",
98e39e09
MM
103 &dfn,
104 &vend,
105 &d->kernel_irq,
106 &d->kernel_base_addr[0],
107 &d->kernel_base_addr[1],
108 &d->kernel_base_addr[2],
109 &d->kernel_base_addr[3],
110 &d->kernel_base_addr[4],
b2c9b373
MM
111 &d->kernel_base_addr[5],
112 &d->kernel_rom_base_addr);
98e39e09
MM
113 d->bus = dfn >> 8U;
114 d->devfn = dfn & 0xff;
115 d->vendid = vend >> 16U;
116 d->devid = vend & 0xffff;
e4842ff3 117 if (filter_match(&filter, d->bus, d->devfn, d->vendid, d->devid))
98e39e09
MM
118 {
119 *last_dev = d;
120 last_dev = &d->next;
121 d->next = NULL;
122 }
123 }
124 fclose(f);
125}
126
127static inline void
128make_proc_pci_name(struct device *d, char *p)
129{
18928b91
MM
130 sprintf(p, "%s/%02x/%02x.%x",
131 pci_dir, d->bus, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
98e39e09
MM
132}
133
134static void
135scan_config(void)
136{
137 struct device *d;
138 char name[64];
2f48f637 139 int fd, res;
98e39e09
MM
140 int how_much = (show_hex > 1) ? 256 : 64;
141
142 for(d=first_dev; d; d=d->next)
143 {
144 make_proc_pci_name(d, name);
145 if ((fd = open(name, O_RDONLY)) < 0)
146 {
147 fprintf(stderr, "lspci: Unable to open %s: %m\n", name);
148 exit(1);
149 }
2f48f637
MM
150 res = read(fd, d->config, how_much);
151 if (res < 0)
98e39e09
MM
152 {
153 fprintf(stderr, "lspci: Error reading %s: %m\n", name);
154 exit(1);
155 }
2f48f637
MM
156 if (res != how_much)
157 {
158 fprintf(stderr, "lspci: Only %d bytes of config space available to you\n", res);
159 exit(1);
160 }
98e39e09
MM
161 close(fd);
162 }
163}
164
165static void
166scan_proc(void)
167{
168 scan_dev_list();
169 scan_config();
170}
171
172/* Config space accesses */
173
174static inline byte
175get_conf_byte(struct device *d, unsigned int pos)
176{
177 return d->config[pos];
178}
179
180static word
181get_conf_word(struct device *d, unsigned int pos)
182{
183 return d->config[pos] | (d->config[pos+1] << 8);
184}
185
186static u32
187get_conf_long(struct device *d, unsigned int pos)
188{
189 return d->config[pos] |
190 (d->config[pos+1] << 8) |
191 (d->config[pos+2] << 16) |
192 (d->config[pos+3] << 24);
193}
194
195/* Sorting */
196
197static int
198compare_them(const void *A, const void *B)
199{
200 const struct device *a = *(const struct device **)A;
201 const struct device *b = *(const struct device **)B;
202
203 if (a->bus < b->bus)
204 return -1;
205 if (a->bus > b->bus)
206 return 1;
207 if (a->devfn < b->devfn)
208 return -1;
209 if (a->devfn > b->devfn)
210 return 1;
211 return 0;
212}
213
214static void
215sort_them(void)
216{
217 struct device **index, **h;
218 int cnt;
219 struct device *d;
220
221 cnt = 0;
222 for(d=first_dev; d; d=d->next)
223 cnt++;
224 h = index = alloca(sizeof(struct device *) * cnt);
225 for(d=first_dev; d; d=d->next)
226 *h++ = d;
227 qsort(index, cnt, sizeof(struct device *), compare_them);
228 last_dev = &first_dev;
229 h = index;
230 while (cnt--)
231 {
232 *last_dev = *h;
233 last_dev = &(*h)->next;
234 h++;
235 }
236 *last_dev = NULL;
237}
238
6d0dc0fd 239/* Normal output */
98e39e09
MM
240
241static void
242show_terse(struct device *d)
243{
244 int c;
245
246 printf("%02x:%02x.%x %s: %s",
247 d->bus,
248 PCI_SLOT(d->devfn),
249 PCI_FUNC(d->devfn),
250 lookup_class(get_conf_word(d, PCI_CLASS_DEVICE)),
251 lookup_device_full(d->vendid, d->devid));
252 if (c = get_conf_byte(d, PCI_REVISION_ID))
253 printf(" (rev %02x)", c);
254 if (verbose && (c = get_conf_byte(d, PCI_CLASS_PROG)))
255 printf(" (prog-if %02x)", c);
256 putchar('\n');
257}
258
259static void
260show_bases(struct device *d, int cnt)
261{
262 word cmd = get_conf_word(d, PCI_COMMAND);
263 int i;
264
265 for(i=0; i<6; i++)
266 {
267 unsigned long pos;
268 unsigned int flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
269 if (buscentric_view)
270 pos = flg;
271 else
272 pos = d->kernel_base_addr[i];
273 if (!pos || pos == 0xffffffff)
274 continue;
98e39e09 275 if (flg & PCI_BASE_ADDRESS_SPACE_IO)
f17b962b
MM
276 {
277 if (cmd & PCI_COMMAND_IO)
a3690506
MM
278 {
279 if (verbose > 1)
280 printf("\tRegion %d: ", i);
281 else
282 putchar('\t');
283 printf("I/O ports at %04lx\n", pos & PCI_BASE_ADDRESS_IO_MASK);
284 }
f17b962b
MM
285 }
286 else if (cmd & PCI_COMMAND_MEMORY)
98e39e09
MM
287 {
288 int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
a3690506
MM
289 if (verbose > 1)
290 printf("\tRegion %d: ", i);
291 else
292 putchar('\t');
98e39e09
MM
293 printf("Memory at ");
294 if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
295 {
296 if (i < cnt - 1)
297 {
298 i++;
299 if (!buscentric_view)
300 printf("%08x", get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i));
301 }
302 else
303 printf("????????");
304 }
f17b962b 305 printf("%08lx (%s, %sprefetchable)\n",
98e39e09
MM
306 pos & PCI_BASE_ADDRESS_MEM_MASK,
307 (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
308 (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
309 (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M 32-bit" : "???",
f17b962b 310 (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
98e39e09
MM
311 }
312 }
313}
314
315static void
316show_htype0(struct device *d)
317{
b2c9b373 318 unsigned long rom = buscentric_view ? get_conf_long(d, PCI_ROM_ADDRESS) : d->kernel_rom_base_addr;
98e39e09
MM
319
320 show_bases(d, 6);
321
322 if (rom & 1)
b2c9b373
MM
323 printf("\tExpansion ROM at %08lx%s\n", rom & PCI_ROM_ADDRESS_MASK,
324 (rom & PCI_ROM_ADDRESS_ENABLE) ? "" : " [disabled]");
98e39e09
MM
325}
326
327static void
328show_htype1(struct device *d)
329{
330 u32 io_base = get_conf_byte(d, PCI_IO_BASE);
331 u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
332 u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
333 u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
334 u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
335 u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
336 u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
337 u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
338 u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
b2c9b373 339 unsigned long rom = buscentric_view ? get_conf_long(d, PCI_ROM_ADDRESS) : d->kernel_rom_base_addr;
98e39e09
MM
340 word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
341
342 show_bases(d, 2);
343 printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
344 get_conf_byte(d, PCI_PRIMARY_BUS),
345 get_conf_byte(d, PCI_SECONDARY_BUS),
346 get_conf_byte(d, PCI_SUBORDINATE_BUS),
347 get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
348
349 if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
350 (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
351 printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
352 else
353 {
354 io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
355 io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
356 if (io_type == PCI_IO_RANGE_TYPE_32)
357 {
358 io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
359 io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
360 }
361 if (io_base)
362 printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
363 }
364
365 if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
366 mem_type)
367 printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
368 else if (mem_base)
369 {
370 mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
371 mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
372 printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
373 }
374
375 if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
376 (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
377 printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
378 else if (pref_base)
379 {
380 pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
381 pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
382 if (pref_type == PCI_PREF_RANGE_TYPE_32)
383 printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit);
384 else
385 printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
386 get_conf_long(d, PCI_PREF_BASE_UPPER32),
387 pref_base,
388 get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
389 pref_limit);
390 }
391
392 if (get_conf_word(d, PCI_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
393 printf("\tSecondary status: SERR\n");
394
395 if (rom & 1)
b2c9b373
MM
396 printf("\tExpansion ROM at %08lx%s\n", rom & PCI_ROM_ADDRESS_MASK,
397 (rom & PCI_ROM_ADDRESS_ENABLE) ? "" : " [disabled]");
98e39e09
MM
398
399 if (verbose > 1)
400 printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
401 (brc & PCI_BRIDGE_CTL_PARITY) ? '+' : '-',
402 (brc & PCI_BRIDGE_CTL_SERR) ? '+' : '-',
403 (brc & PCI_BRIDGE_CTL_NO_ISA) ? '+' : '-',
404 (brc & PCI_BRIDGE_CTL_VGA) ? '+' : '-',
405 (brc & PCI_BRIDGE_CTL_MASTER_ABORT) ? '+' : '-',
406 (brc & PCI_BRIDGE_CTL_BUS_RESET) ? '+' : '-',
407 (brc & PCI_BRIDGE_CTL_FAST_BACK) ? '+' : '-');
408}
409
2f48f637
MM
410static void
411show_htype2(struct device *d)
412{
413}
414
98e39e09
MM
415static void
416show_verbose(struct device *d)
417{
418 word status = get_conf_word(d, PCI_STATUS);
419 word cmd = get_conf_word(d, PCI_COMMAND);
420 word class = get_conf_word(d, PCI_CLASS_DEVICE);
421 byte bist = get_conf_byte(d, PCI_BIST);
422 byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
423 byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
424 byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
425 byte max_lat, min_gnt;
426 byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
427 byte int_line = get_conf_byte(d, PCI_INTERRUPT_LINE);
2f48f637 428 unsigned int irq;
98e39e09
MM
429 word subsys_v, subsys_d;
430
431 show_terse(d);
432
98e39e09
MM
433 switch (htype)
434 {
2f48f637
MM
435 case PCI_HEADER_TYPE_NORMAL:
436 if (class == PCI_CLASS_BRIDGE_PCI)
437 {
438 badhdr:
439 printf("\t!!! Header type %02x doesn't match class code %04x\n", htype, class);
440 return;
441 }
98e39e09
MM
442 max_lat = get_conf_byte(d, PCI_MAX_LAT);
443 min_gnt = get_conf_byte(d, PCI_MIN_GNT);
444 subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
445 subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
446 break;
2f48f637
MM
447 case PCI_HEADER_TYPE_BRIDGE:
448 if (class != PCI_CLASS_BRIDGE_PCI)
449 goto badhdr;
450 irq = int_line = int_pin = min_gnt = max_lat = 0;
451 subsys_v = subsys_d = 0;
452 break;
453 case PCI_HEADER_TYPE_CARDBUS:
454 if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
455 goto badhdr;
98e39e09
MM
456 irq = int_line = int_pin = min_gnt = max_lat = 0;
457 subsys_v = subsys_d = 0;
458 break;
459 default:
460 printf("\t!!! Unknown header type %02x\n", htype);
461 return;
462 }
463
464 if (buscentric_view)
465 irq = int_line;
466 else
467 irq = d->kernel_irq;
468
469 if (verbose > 1)
470 {
471 if (subsys_v)
472 printf("\tSubsystem ID: %04x:%04x\n", subsys_v, subsys_d);
473 printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
474 (cmd & PCI_COMMAND_IO) ? '+' : '-',
475 (cmd & PCI_COMMAND_MEMORY) ? '+' : '-',
476 (cmd & PCI_COMMAND_MASTER) ? '+' : '-',
477 (cmd & PCI_COMMAND_SPECIAL) ? '+' : '-',
478 (cmd & PCI_COMMAND_INVALIDATE) ? '+' : '-',
479 (cmd & PCI_COMMAND_VGA_PALETTE) ? '+' : '-',
480 (cmd & PCI_COMMAND_PARITY) ? '+' : '-',
481 (cmd & PCI_COMMAND_WAIT) ? '+' : '-',
482 (cmd & PCI_COMMAND_SERR) ? '+' : '-',
483 (cmd & PCI_COMMAND_FAST_BACK) ? '+' : '-');
484 printf("\tStatus: 66Mhz%c UDF%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c >SERR%c <PERR%c\n",
485 (status & PCI_STATUS_66MHZ) ? '+' : '-',
486 (status & PCI_STATUS_UDF) ? '+' : '-',
487 (status & PCI_STATUS_FAST_BACK) ? '+' : '-',
488 (status & PCI_STATUS_PARITY) ? '+' : '-',
489 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
490 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
491 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
492 (status & PCI_STATUS_SIG_TARGET_ABORT) ? '+' : '-',
493 (status & PCI_STATUS_REC_TARGET_ABORT) ? '+' : '-',
494 (status & PCI_STATUS_REC_MASTER_ABORT) ? '+' : '-',
495 (status & PCI_STATUS_SIG_SYSTEM_ERROR) ? '+' : '-',
496 (status & PCI_STATUS_DETECTED_PARITY) ? '+' : '-');
497 if (cmd & PCI_COMMAND_MASTER)
498 {
499 printf("\tLatency: ");
500 if (min_gnt)
501 printf("%d min, ", min_gnt);
502 if (max_lat)
503 printf("%d max, ", max_lat);
504 printf("%d set", latency);
505 if (cache_line)
506 printf(", cache line size %02x", cache_line);
507 putchar('\n');
508 }
509 if (int_pin)
f17b962b 510 printf("\tInterrupt: pin %c routed to IRQ " IRQ_FORMAT "\n", 'A' + int_pin - 1, irq);
98e39e09
MM
511 }
512 else
513 {
514 printf("\tFlags: ");
515 if (cmd & PCI_COMMAND_MASTER)
516 printf("bus master, ");
517 if (cmd & PCI_COMMAND_VGA_PALETTE)
518 printf("VGA palette snoop, ");
519 if (cmd & PCI_COMMAND_WAIT)
520 printf("stepping, ");
521 if (cmd & PCI_COMMAND_FAST_BACK)
522 printf("fast Back2Back, ");
523 if (status & PCI_STATUS_66MHZ)
524 printf("66Mhz, ");
525 if (status & PCI_STATUS_UDF)
526 printf("user-definable features, ");
527 printf("%s devsel",
528 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
529 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
530 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
531 if (cmd & PCI_COMMAND_MASTER)
532 printf(", latency %d", latency);
533 if (int_pin)
534 if (d->kernel_irq)
f17b962b 535 printf(", IRQ " IRQ_FORMAT, irq);
98e39e09
MM
536 else
537 printf(", IRQ ?");
538 putchar('\n');
539 }
540
541 if (bist & PCI_BIST_CAPABLE)
542 {
543 if (bist & PCI_BIST_START)
544 printf("\tBIST is running\n");
545 else
546 printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
547 }
548
549 switch (htype)
550 {
2f48f637 551 case PCI_HEADER_TYPE_NORMAL:
98e39e09
MM
552 show_htype0(d);
553 break;
2f48f637 554 case PCI_HEADER_TYPE_BRIDGE:
98e39e09
MM
555 show_htype1(d);
556 break;
2f48f637
MM
557 case PCI_HEADER_TYPE_CARDBUS:
558 show_htype2(d);
559 break;
98e39e09
MM
560 }
561}
562
563static void
564show_hex_dump(struct device *d)
565{
566 int i;
567 int limit = (show_hex > 1) ? 256 : 64;
568
569 for(i=0; i<limit; i++)
570 {
571 if (! (i & 15))
572 printf("%02x:", i);
573 printf(" %02x", get_conf_byte(d, i));
574 if ((i & 15) == 15)
575 putchar('\n');
576 }
577}
578
0a33d0ec
MM
579static void
580show_machine(struct device *d)
581{
582 int c;
583
584 if (verbose)
585 {
586 printf("Device:\t%02x:%02x.%x\n", d->bus, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
587 printf("Class:\t%s\n", lookup_class(get_conf_word(d, PCI_CLASS_DEVICE)));
588 printf("Vendor:\t%s\n", lookup_vendor(d->vendid));
589 printf("Device:\t%s\n", lookup_device(d->vendid, d->devid));
590 if (c = get_conf_byte(d, PCI_REVISION_ID))
591 printf("Rev:\t%02x\n", c);
592 if (c = get_conf_byte(d, PCI_CLASS_PROG))
593 printf("ProgIf:\t%02x\n", c);
594 }
595 else
596 {
597 printf("%02x:%02x.%x ", d->bus, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
598 printf("\"%s\" \"%s\" \"%s\"",
599 lookup_class(get_conf_word(d, PCI_CLASS_DEVICE)),
600 lookup_vendor(d->vendid),
601 lookup_device(d->vendid, d->devid));
602 if (c = get_conf_byte(d, PCI_REVISION_ID))
603 printf(" -r%02x", c);
604 if (c = get_conf_byte(d, PCI_CLASS_PROG))
605 printf(" -p%02x", c);
606 putchar('\n');
607 }
608}
609
98e39e09
MM
610static void
611show(void)
612{
613 struct device *d;
614
615 for(d=first_dev; d; d=d->next)
616 {
0a33d0ec
MM
617 if (machine_readable)
618 show_machine(d);
619 else if (verbose)
98e39e09
MM
620 show_verbose(d);
621 else
622 show_terse(d);
623 if (show_hex)
624 show_hex_dump(d);
625 if (verbose || show_hex)
626 putchar('\n');
627 }
628}
629
6d0dc0fd
MM
630/* Tree output */
631
632struct bridge {
633 struct bridge *chain; /* Single-linked list of bridges */
634 struct bridge *next, *child; /* Tree of bridges */
635 struct bus *first_bus; /* List of busses connected to this bridge */
636 unsigned int primary, secondary, subordinate; /* Bus numbers */
637 struct device *br_dev;
638};
639
640struct bus {
641 unsigned int number;
642 struct bus *sibling;
643 struct device *first_dev, **last_dev;
644};
645
646static struct bridge host_bridge = { NULL, NULL, NULL, NULL, ~0, 0, ~0, NULL };
647
648static struct bus *
649find_bus(struct bridge *b, unsigned int n)
650{
651 struct bus *bus;
652
653 for(bus=b->first_bus; bus; bus=bus->sibling)
654 if (bus->number == n)
655 break;
656 return bus;
657}
658
659static struct bus *
660new_bus(struct bridge *b, unsigned int n)
661{
662 struct bus *bus = xmalloc(sizeof(struct bus));
663
664 bus = xmalloc(sizeof(struct bus));
665 bus->number = n;
666 bus->sibling = b->first_bus;
667 bus->first_dev = NULL;
668 bus->last_dev = &bus->first_dev;
669 b->first_bus = bus;
670 return bus;
671}
672
673static void
674insert_dev(struct device *d, struct bridge *b)
675{
676 struct bus *bus;
677
678 if (! (bus = find_bus(b, d->bus)))
679 {
680 struct bridge *c;
681 for(c=b->child; c; c=c->next)
682 if (c->secondary <= d->bus && d->bus <= c->subordinate)
683 return insert_dev(d, c);
684 bus = new_bus(b, d->bus);
685 }
686 /* Simple insertion at the end _does_ guarantee the correct order as the
687 * original device list was sorted by (bus, devfn) lexicographically
688 * and all devices on the new list have the same bus number.
689 */
690 *bus->last_dev = d;
691 bus->last_dev = &d->next;
692 d->next = NULL;
693}
694
695static void
696grow_tree(void)
697{
698 struct device *d, *d2;
008407bd 699 struct bridge **last_br, *b;
6d0dc0fd
MM
700
701 /* Build list of bridges */
702
008407bd 703 last_br = &host_bridge.chain;
6d0dc0fd
MM
704 for(d=first_dev; d; d=d->next)
705 {
706 word class = get_conf_word(d, PCI_CLASS_DEVICE);
707 if (class == PCI_CLASS_BRIDGE_PCI && (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f) == 1)
708 {
709 b = xmalloc(sizeof(struct bridge));
710 b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
711 b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
712 b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
008407bd
MM
713 *last_br = b;
714 last_br = &b->chain;
6d0dc0fd
MM
715 b->next = b->child = NULL;
716 b->first_bus = NULL;
717 b->br_dev = d;
718 }
719 }
008407bd 720 *last_br = NULL;
6d0dc0fd
MM
721
722 /* Create a bridge tree */
723
008407bd 724 for(b=&host_bridge; b; b=b->chain)
6d0dc0fd
MM
725 {
726 struct bridge *c, *best;
727 best = NULL;
008407bd 728 for(c=&host_bridge; c; c=c->chain)
6d0dc0fd
MM
729 if (c != b && b->primary >= c->secondary && b->primary <= c->subordinate &&
730 (!best || best->subordinate - best->primary > c->subordinate - c->primary))
731 best = c;
732 if (best)
733 {
734 b->next = best->child;
735 best->child = b;
736 }
737 }
738
739 /* Insert secondary bus for each bridge */
740
008407bd 741 for(b=&host_bridge; b; b=b->chain)
6d0dc0fd
MM
742 if (!find_bus(b, b->secondary))
743 new_bus(b, b->secondary);
744
745 /* Create bus structs and link devices */
746
747 for(d=first_dev; d;)
748 {
749 d2 = d->next;
750 insert_dev(d, &host_bridge);
751 d = d2;
752 }
753}
754
755static void
756print_it(byte *line, byte *p)
757{
758 *p++ = '\n';
759 *p = 0;
760 fputs(line, stdout);
761 for(p=line; *p; p++)
008407bd 762 if (*p == '+' || *p == '|')
6d0dc0fd
MM
763 *p = '|';
764 else
765 *p = ' ';
766}
767
768static void show_tree_bridge(struct bridge *, byte *, byte *);
769
770static void
771show_tree_dev(struct device *d, byte *line, byte *p)
772{
773 struct bridge *b;
774
775 p += sprintf(p, "%02x.%x", PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
776 for(b=&host_bridge; b; b=b->chain)
777 if (b->br_dev == d)
778 {
008407bd
MM
779 if (b->secondary == b->subordinate)
780 p += sprintf(p, "-[%02x]-", b->secondary);
781 else
782 p += sprintf(p, "-[%02x-%02x]-", b->secondary, b->subordinate);
6d0dc0fd
MM
783 show_tree_bridge(b, line, p);
784 return;
785 }
18928b91
MM
786 if (verbose)
787 p += sprintf(p, " %s", lookup_device_full(d->vendid, d->devid));
6d0dc0fd
MM
788 print_it(line, p);
789}
790
791static void
792show_tree_bus(struct bus *b, byte *line, byte *p)
793{
794 if (!b->first_dev)
795 print_it(line, p);
796 else if (!b->first_dev->next)
797 {
798 *p++ = '-';
799 *p++ = '-';
800 show_tree_dev(b->first_dev, line, p);
801 }
802 else
803 {
804 struct device *d = b->first_dev;
805 while (d->next)
806 {
807 p[0] = '+';
808 p[1] = '-';
809 show_tree_dev(d, line, p+2);
810 d = d->next;
811 }
812 p[0] = '\\';
813 p[1] = '-';
814 show_tree_dev(d, line, p+2);
815 }
816}
817
818static void
819show_tree_bridge(struct bridge *b, byte *line, byte *p)
820{
821 *p++ = '-';
822 if (!b->first_bus->sibling)
823 {
824 if (b == &host_bridge)
825 p += sprintf(p, "[%02x]-", b->first_bus->number);
826 show_tree_bus(b->first_bus, line, p);
827 }
828 else
829 {
830 struct bus *u = b->first_bus;
831 byte *k;
832
833 while (u->sibling)
834 {
835 k = p + sprintf(p, "+-[%02x]-", u->number);
836 show_tree_bus(u, line, k);
837 u = u->sibling;
838 }
839 k = p + sprintf(p, "\\-[%02x]-", u->number);
840 show_tree_bus(u, line, k);
841 }
842}
843
844static void
845show_forest(void)
846{
847 char line[256];
848
849 grow_tree();
850 show_tree_bridge(&host_bridge, line, line);
851}
852
98e39e09
MM
853/* Main */
854
855int
856main(int argc, char **argv)
857{
858 int i;
e4842ff3 859 char *msg;
98e39e09 860
496d4021
MM
861 if (argc == 2 && !strcmp(argv[1], "--version"))
862 {
863 puts("lspci version " PCIUTILS_VERSION);
864 return 0;
865 }
e4842ff3 866 filter_init(&filter);
98e39e09
MM
867 while ((i = getopt(argc, argv, options)) != -1)
868 switch (i)
869 {
870 case 'n':
871 show_numeric_ids = 1;
872 break;
873 case 'v':
874 verbose++;
875 break;
876 case 'b':
877 buscentric_view = 1;
878 break;
e4842ff3
MM
879 case 's':
880 if (msg = filter_parse_slot(&filter, optarg))
881 {
882 fprintf(stderr, "lspci: -f: %s\n", msg);
883 return 1;
884 }
98e39e09 885 break;
e4842ff3
MM
886 case 'd':
887 if (msg = filter_parse_id(&filter, optarg))
888 {
889 fprintf(stderr, "lspci: -d: %s\n", msg);
890 return 1;
891 }
98e39e09
MM
892 break;
893 case 'x':
894 show_hex++;
895 break;
6d0dc0fd
MM
896 case 't':
897 show_tree++;
898 break;
18928b91
MM
899 case 'i':
900 pci_ids = optarg;
901 break;
902 case 'p':
903 pci_dir = optarg;
904 break;
0a33d0ec
MM
905 case 'm':
906 machine_readable++;
907 break;
98e39e09
MM
908 default:
909 bad:
910 fprintf(stderr, help_msg);
911 return 1;
912 }
913 if (optind < argc)
914 goto bad;
915
916 scan_proc();
917 sort_them();
6d0dc0fd
MM
918 if (show_tree)
919 show_forest();
920 else
921 show();
98e39e09
MM
922
923 return 0;
924}