]> mj.ucw.cz Git - pciutils.git/blob - lspci.c
Added support for printing of subsystem IDs.
[pciutils.git] / lspci.c
1 /*
2  *      $Id: lspci.c,v 1.14 1998/07/17 08:57:14 mj Exp $
3  *
4  *      Linux PCI Utilities -- List All PCI Devices
5  *
6  *      Copyright (c) 1997, 1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
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>
16
17 #include "pciutils.h"
18
19 /* Options */
20
21 static int verbose;                     /* Show detailed information */
22 static int buscentric_view;             /* Show bus addresses/IRQ's instead of CPU-visible ones */
23 static int show_hex;                    /* Show contents of config space as hexadecimal numbers */
24 static struct pci_filter filter;        /* Device filter */
25 static int show_tree;                   /* Show bus tree */
26 static int machine_readable;            /* Generate machine-readable output */
27 static char *pci_dir = PROC_BUS_PCI;
28
29 static char options[] = "nvbxs:d:ti:p:m";
30
31 static char help_msg[] = "\
32 Usage: lspci [<switches>]\n\
33 \n\
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\
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\
44 ";
45
46 /* Format strings used for IRQ numbers */
47
48 #ifdef ARCH_SPARC64
49 #define IRQ_FORMAT "%08x"
50 #else
51 #define IRQ_FORMAT "%d"
52 #endif
53
54 /* Our view of the PCI bus */
55
56 struct device {
57   struct device *next;
58   byte bus, devfn;
59   word vendid, devid;
60   unsigned int kernel_irq;
61   unsigned long kernel_base_addr[6], kernel_rom_base_addr;
62   byte config[256];
63 };
64
65 static struct device *first_dev, **last_dev = &first_dev;
66
67 /* Miscellaneous routines */
68
69 void *
70 xmalloc(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
81 /* Interface for /proc/bus/pci */
82
83 static void
84 scan_dev_list(void)
85 {
86   FILE *f;
87   byte line[256];
88   byte name[256];
89
90   sprintf(name, "%s/devices", pci_dir);
91   if (! (f = fopen(name, "r")))
92     {
93       perror(name);
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
101       bzero(d, sizeof(*d));
102       sscanf(line, "%x %x %x %lx %lx %lx %lx %lx %lx %lx",
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],
111              &d->kernel_base_addr[5],
112              &d->kernel_rom_base_addr);
113       d->bus = dfn >> 8U;
114       d->devfn = dfn & 0xff;
115       d->vendid = vend >> 16U;
116       d->devid = vend & 0xffff;
117       if (filter_match(&filter, d->bus, d->devfn, d->vendid, d->devid))
118         {
119           *last_dev = d;
120           last_dev = &d->next;
121           d->next = NULL;
122         }
123     }
124   fclose(f);
125 }
126
127 static inline void
128 make_proc_pci_name(struct device *d, char *p)
129 {
130   sprintf(p, "%s/%02x/%02x.%x",
131           pci_dir, d->bus, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
132 }
133
134 static void
135 scan_config(void)
136 {
137   struct device *d;
138   char name[64];
139   int fd, res;
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         }
150       res = read(fd, d->config, how_much);
151       if (res < 0)
152         {
153           fprintf(stderr, "lspci: Error reading %s: %m\n", name);
154           exit(1);
155         }
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         }
161       close(fd);
162     }
163 }
164
165 static void
166 scan_proc(void)
167 {
168   scan_dev_list();
169   scan_config();
170 }
171
172 /* Config space accesses */
173
174 static inline byte
175 get_conf_byte(struct device *d, unsigned int pos)
176 {
177   return d->config[pos];
178 }
179
180 static word
181 get_conf_word(struct device *d, unsigned int pos)
182 {
183   return d->config[pos] | (d->config[pos+1] << 8);
184 }
185
186 static u32
187 get_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
197 static int
198 compare_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
214 static void
215 sort_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
239 /* Normal output */
240
241 static void
242 show_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
259 static void
260 show_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<cnt; 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;
275       if (flg & PCI_BASE_ADDRESS_SPACE_IO)
276         {
277           if (cmd & PCI_COMMAND_IO)
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             }
285         }
286       else if (cmd & PCI_COMMAND_MEMORY)
287         {
288           int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
289           if (verbose > 1)
290             printf("\tRegion %d: ", i);
291           else
292             putchar('\t');
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             }
305           printf("%08lx (%s, %sprefetchable)\n",
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" : "???",
310                  (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
311         }
312     }
313 }
314
315 static void
316 show_htype0(struct device *d)
317 {
318   unsigned long rom = buscentric_view ? get_conf_long(d, PCI_ROM_ADDRESS) : d->kernel_rom_base_addr;
319
320   show_bases(d, 6);
321
322   if (rom & 1)
323     printf("\tExpansion ROM at %08lx%s\n", rom & PCI_ROM_ADDRESS_MASK,
324            (rom & PCI_ROM_ADDRESS_ENABLE) ? "" : " [disabled]");
325 }
326
327 static void
328 show_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;
339   unsigned long rom = buscentric_view ? get_conf_long(d, PCI_ROM_ADDRESS) : d->kernel_rom_base_addr;
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)
396     printf("\tExpansion ROM at %08lx%s\n", rom & PCI_ROM_ADDRESS_MASK,
397            (rom & PCI_ROM_ADDRESS_ENABLE) ? "" : " [disabled]");
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
410 static void
411 show_htype2(struct device *d)
412 {
413   int i;
414   word cmd = get_conf_word(d, PCI_COMMAND);
415   word brc = get_conf_word(d, PCI_CB_BRIDGE_CONTROL);
416   word exca = get_conf_word(d, PCI_CB_LEGACY_MODE_BASE);
417
418   show_bases(d, 1);
419   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
420          get_conf_byte(d, PCI_CB_PRIMARY_BUS),
421          get_conf_byte(d, PCI_CB_CARD_BUS),
422          get_conf_byte(d, PCI_CB_SUBORDINATE_BUS),
423          get_conf_byte(d, PCI_CB_LATENCY_TIMER));
424   for(i=0; i<2; i++)
425     {
426       int p = 8*i;
427       u32 base = get_conf_long(d, PCI_CB_MEMORY_BASE_0 + p);
428       u32 limit = get_conf_long(d, PCI_CB_MEMORY_LIMIT_0 + p);
429       if (limit > base)
430         printf("Memory window %d: %08x-%08x%s%s\n", i, base, limit,
431                (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]",
432                (brc & (PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 << i)) ? " (prefetchable)" : "");
433     }
434   for(i=0; i<2; i++)
435     {
436       int p = 8*i;
437       u32 base = get_conf_long(d, PCI_CB_IO_BASE_0 + p);
438       u32 limit = get_conf_long(d, PCI_CB_IO_LIMIT_0 + p);
439       if (!(base & PCI_IO_RANGE_TYPE_32))
440         {
441           base &= 0xffff;
442           limit &= 0xffff;
443         }
444       base &= PCI_CB_IO_RANGE_MASK;
445       if (!base)
446         continue;
447       limit = (limit & PCI_CB_IO_RANGE_MASK) + 3;
448       printf("I/O window %d: %08x-%08x%s\n", i, base, limit,
449              (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
450     }
451
452   if (get_conf_word(d, PCI_CB_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
453     printf("\tSecondary status: SERR\n");
454   if (verbose > 1)
455     printf("\tBridgeCtl: Parity%c SERR%c ISA%c VGA%c MAbort%c >Reset%c 16bInt%c PostWrite%c\n",
456            (brc & PCI_CB_BRIDGE_CTL_PARITY) ? '+' : '-',
457            (brc & PCI_CB_BRIDGE_CTL_SERR) ? '+' : '-',
458            (brc & PCI_CB_BRIDGE_CTL_ISA) ? '+' : '-',
459            (brc & PCI_CB_BRIDGE_CTL_VGA) ? '+' : '-',
460            (brc & PCI_CB_BRIDGE_CTL_MASTER_ABORT) ? '+' : '-',
461            (brc & PCI_CB_BRIDGE_CTL_CB_RESET) ? '+' : '-',
462            (brc & PCI_CB_BRIDGE_CTL_16BIT_INT) ? '+' : '-',
463            (brc & PCI_CB_BRIDGE_CTL_POST_WRITES) ? '+' : '-');
464   if (exca)
465     printf("\t16-bit legacy interface ports at %04x\n", exca);
466 }
467
468 static void
469 show_verbose(struct device *d)
470 {
471   word status = get_conf_word(d, PCI_STATUS);
472   word cmd = get_conf_word(d, PCI_COMMAND);
473   word class = get_conf_word(d, PCI_CLASS_DEVICE);
474   byte bist = get_conf_byte(d, PCI_BIST);
475   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
476   byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
477   byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
478   byte max_lat, min_gnt;
479   byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
480   byte int_line = get_conf_byte(d, PCI_INTERRUPT_LINE);
481   unsigned int irq;
482   word subsys_v, subsys_d;
483
484   show_terse(d);
485
486   switch (htype)
487     {
488     case PCI_HEADER_TYPE_NORMAL:
489       if (class == PCI_CLASS_BRIDGE_PCI)
490         {
491         badhdr:
492           printf("\t!!! Header type %02x doesn't match class code %04x\n", htype, class);
493           return;
494         }
495       max_lat = get_conf_byte(d, PCI_MAX_LAT);
496       min_gnt = get_conf_byte(d, PCI_MIN_GNT);
497       subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
498       subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
499       break;
500     case PCI_HEADER_TYPE_BRIDGE:
501       if (class != PCI_CLASS_BRIDGE_PCI)
502         goto badhdr;
503       irq = int_line = int_pin = min_gnt = max_lat = 0;
504       subsys_v = subsys_d = 0;
505       break;
506     case PCI_HEADER_TYPE_CARDBUS:
507       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
508         goto badhdr;
509       min_gnt = max_lat = 0;
510       subsys_v = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
511       subsys_d = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
512       break;
513     default:
514       printf("\t!!! Unknown header type %02x\n", htype);
515       return;
516     }
517
518   if (buscentric_view)
519     irq = int_line;
520   else
521     irq = d->kernel_irq;
522
523   if (verbose && subsys_v && subsys_v != 0xffff)
524     printf("\tSubsystem: %s\n", lookup_subsys_device_full(subsys_v, subsys_d));
525
526   if (verbose > 1)
527     {
528       printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
529              (cmd & PCI_COMMAND_IO) ? '+' : '-',
530              (cmd & PCI_COMMAND_MEMORY) ? '+' : '-',
531              (cmd & PCI_COMMAND_MASTER) ? '+' : '-',
532              (cmd & PCI_COMMAND_SPECIAL) ? '+' : '-',
533              (cmd & PCI_COMMAND_INVALIDATE) ? '+' : '-',
534              (cmd & PCI_COMMAND_VGA_PALETTE) ? '+' : '-',
535              (cmd & PCI_COMMAND_PARITY) ? '+' : '-',
536              (cmd & PCI_COMMAND_WAIT) ? '+' : '-',
537              (cmd & PCI_COMMAND_SERR) ? '+' : '-',
538              (cmd & PCI_COMMAND_FAST_BACK) ? '+' : '-');
539       printf("\tStatus: 66Mhz%c UDF%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c >SERR%c <PERR%c\n",
540              (status & PCI_STATUS_66MHZ) ? '+' : '-',
541              (status & PCI_STATUS_UDF) ? '+' : '-',
542              (status & PCI_STATUS_FAST_BACK) ? '+' : '-',
543              (status & PCI_STATUS_PARITY) ? '+' : '-',
544              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
545              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
546              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
547              (status & PCI_STATUS_SIG_TARGET_ABORT) ? '+' : '-',
548              (status & PCI_STATUS_REC_TARGET_ABORT) ? '+' : '-',
549              (status & PCI_STATUS_REC_MASTER_ABORT) ? '+' : '-',
550              (status & PCI_STATUS_SIG_SYSTEM_ERROR) ? '+' : '-',
551              (status & PCI_STATUS_DETECTED_PARITY) ? '+' : '-');
552       if (cmd & PCI_COMMAND_MASTER)
553         {
554           printf("\tLatency: ");
555           if (min_gnt)
556             printf("%d min, ", min_gnt);
557           if (max_lat)
558             printf("%d max, ", max_lat);
559           printf("%d set", latency);
560           if (cache_line)
561             printf(", cache line size %02x", cache_line);
562           putchar('\n');
563         }
564       if (int_pin)
565         printf("\tInterrupt: pin %c routed to IRQ " IRQ_FORMAT "\n", 'A' + int_pin - 1, irq);
566     }
567   else
568     {
569       printf("\tFlags: ");
570       if (cmd & PCI_COMMAND_MASTER)
571         printf("bus master, ");
572       if (cmd & PCI_COMMAND_VGA_PALETTE)
573         printf("VGA palette snoop, ");
574       if (cmd & PCI_COMMAND_WAIT)
575         printf("stepping, ");
576       if (cmd & PCI_COMMAND_FAST_BACK)
577         printf("fast Back2Back, ");
578       if (status & PCI_STATUS_66MHZ)
579         printf("66Mhz, ");
580       if (status & PCI_STATUS_UDF)
581         printf("user-definable features, ");
582       printf("%s devsel",
583              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
584              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
585              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
586       if (cmd & PCI_COMMAND_MASTER)
587         printf(", latency %d", latency);
588       if (int_pin)
589         if (d->kernel_irq)
590           printf(", IRQ " IRQ_FORMAT, irq);
591         else
592           printf(", IRQ ?");
593       putchar('\n');
594     }
595
596   if (bist & PCI_BIST_CAPABLE)
597     {
598       if (bist & PCI_BIST_START)
599         printf("\tBIST is running\n");
600       else
601         printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
602     }
603
604   switch (htype)
605     {
606     case PCI_HEADER_TYPE_NORMAL:
607       show_htype0(d);
608       break;
609     case PCI_HEADER_TYPE_BRIDGE:
610       show_htype1(d);
611       break;
612     case PCI_HEADER_TYPE_CARDBUS:
613       show_htype2(d);
614       break;
615     }
616 }
617
618 static void
619 show_hex_dump(struct device *d)
620 {
621   int i;
622   int limit = (show_hex > 1) ? 256 : 64;
623
624   for(i=0; i<limit; i++)
625     {
626       if (! (i & 15))
627         printf("%02x:", i);
628       printf(" %02x", get_conf_byte(d, i));
629       if ((i & 15) == 15)
630         putchar('\n');
631     }
632 }
633
634 static void
635 show_machine(struct device *d)
636 {
637   int c;
638   word sv_id=0, sd_id=0;
639
640   switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
641     {
642     case PCI_HEADER_TYPE_NORMAL:
643       sv_id = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
644       sd_id = get_conf_word(d, PCI_SUBSYSTEM_ID);
645       break;
646     case PCI_HEADER_TYPE_CARDBUS:
647       sv_id = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
648       sd_id = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
649       break;
650     }
651
652   if (verbose)
653     {
654       printf("Device:\t%02x:%02x.%x\n", d->bus, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
655       printf("Class:\t%s\n", lookup_class(get_conf_word(d, PCI_CLASS_DEVICE)));
656       printf("Vendor:\t%s\n", lookup_vendor(d->vendid));
657       printf("Device:\t%s\n", lookup_device(d->vendid, d->devid));
658       if (sv_id && sv_id != 0xffff)
659         {
660           printf("SVendor:\t%s\n", lookup_subsys_vendor(sv_id));
661           printf("SDevice:\t%s\n", lookup_subsys_device(sv_id, sd_id));
662         }
663       if (c = get_conf_byte(d, PCI_REVISION_ID))
664         printf("Rev:\t%02x\n", c);
665       if (c = get_conf_byte(d, PCI_CLASS_PROG))
666         printf("ProgIf:\t%02x\n", c);
667     }
668   else
669     {
670       printf("%02x:%02x.%x ", d->bus, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
671       printf("\"%s\" \"%s\" \"%s\"",
672              lookup_class(get_conf_word(d, PCI_CLASS_DEVICE)),
673              lookup_vendor(d->vendid),
674              lookup_device(d->vendid, d->devid));
675       if (c = get_conf_byte(d, PCI_REVISION_ID))
676         printf(" -r%02x", c);
677       if (c = get_conf_byte(d, PCI_CLASS_PROG))
678         printf(" -p%02x", c);
679       if (sv_id && sv_id != 0xffff)
680         printf(" \"%s\" \"%s\"", lookup_subsys_vendor(sv_id), lookup_subsys_device(sv_id, sd_id));
681       else
682         printf(" \"\" \"\"");
683       putchar('\n');
684     }
685 }
686
687 static void
688 show(void)
689 {
690   struct device *d;
691
692   for(d=first_dev; d; d=d->next)
693     {
694       if (machine_readable)
695         show_machine(d);
696       else if (verbose)
697         show_verbose(d);
698       else
699         show_terse(d);
700       if (show_hex)
701         show_hex_dump(d);
702       if (verbose || show_hex)
703         putchar('\n');
704     }
705 }
706
707 /* Tree output */
708
709 struct bridge {
710   struct bridge *chain;                 /* Single-linked list of bridges */
711   struct bridge *next, *child;          /* Tree of bridges */
712   struct bus *first_bus;                /* List of busses connected to this bridge */
713   unsigned int primary, secondary, subordinate; /* Bus numbers */
714   struct device *br_dev;
715 };
716
717 struct bus {
718   unsigned int number;
719   struct bus *sibling;
720   struct device *first_dev, **last_dev;
721 };
722
723 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, ~0, 0, ~0, NULL };
724
725 static struct bus *
726 find_bus(struct bridge *b, unsigned int n)
727 {
728   struct bus *bus;
729
730   for(bus=b->first_bus; bus; bus=bus->sibling)
731     if (bus->number == n)
732       break;
733   return bus;
734 }
735
736 static struct bus *
737 new_bus(struct bridge *b, unsigned int n)
738 {
739   struct bus *bus = xmalloc(sizeof(struct bus));
740
741   bus = xmalloc(sizeof(struct bus));
742   bus->number = n;
743   bus->sibling = b->first_bus;
744   bus->first_dev = NULL;
745   bus->last_dev = &bus->first_dev;
746   b->first_bus = bus;
747   return bus;
748 }
749
750 static void
751 insert_dev(struct device *d, struct bridge *b)
752 {
753   struct bus *bus;
754
755   if (! (bus = find_bus(b, d->bus)))
756     {
757       struct bridge *c;
758       for(c=b->child; c; c=c->next)
759         if (c->secondary <= d->bus && d->bus <= c->subordinate)
760           return insert_dev(d, c);
761       bus = new_bus(b, d->bus);
762     }
763   /* Simple insertion at the end _does_ guarantee the correct order as the
764    * original device list was sorted by (bus, devfn) lexicographically
765    * and all devices on the new list have the same bus number.
766    */
767   *bus->last_dev = d;
768   bus->last_dev = &d->next;
769   d->next = NULL;
770 }
771
772 static void
773 grow_tree(void)
774 {
775   struct device *d, *d2;
776   struct bridge **last_br, *b;
777
778   /* Build list of bridges */
779
780   last_br = &host_bridge.chain;
781   for(d=first_dev; d; d=d->next)
782     {
783       word class = get_conf_word(d, PCI_CLASS_DEVICE);
784       byte ht = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
785       if (class == PCI_CLASS_BRIDGE_PCI &&
786           (ht == PCI_HEADER_TYPE_BRIDGE || ht == PCI_HEADER_TYPE_CARDBUS))
787         {
788           b = xmalloc(sizeof(struct bridge));
789           if (ht == PCI_HEADER_TYPE_BRIDGE)
790             {
791               b->primary = get_conf_byte(d, PCI_CB_PRIMARY_BUS);
792               b->secondary = get_conf_byte(d, PCI_CB_CARD_BUS);
793               b->subordinate = get_conf_byte(d, PCI_CB_SUBORDINATE_BUS);
794             }
795           else
796             {
797               b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
798               b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
799               b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
800             }
801           *last_br = b;
802           last_br = &b->chain;
803           b->next = b->child = NULL;
804           b->first_bus = NULL;
805           b->br_dev = d;
806         }
807     }
808   *last_br = NULL;
809
810   /* Create a bridge tree */
811
812   for(b=&host_bridge; b; b=b->chain)
813     {
814       struct bridge *c, *best;
815       best = NULL;
816       for(c=&host_bridge; c; c=c->chain)
817         if (c != b && b->primary >= c->secondary && b->primary <= c->subordinate &&
818             (!best || best->subordinate - best->primary > c->subordinate - c->primary))
819           best = c;
820       if (best)
821         {
822           b->next = best->child;
823           best->child = b;
824         }
825     }
826
827   /* Insert secondary bus for each bridge */
828
829   for(b=&host_bridge; b; b=b->chain)
830     if (!find_bus(b, b->secondary))
831       new_bus(b, b->secondary);
832
833   /* Create bus structs and link devices */
834
835   for(d=first_dev; d;)
836     {
837       d2 = d->next;
838       insert_dev(d, &host_bridge);
839       d = d2;
840     }
841 }
842
843 static void
844 print_it(byte *line, byte *p)
845 {
846   *p++ = '\n';
847   *p = 0;
848   fputs(line, stdout);
849   for(p=line; *p; p++)
850     if (*p == '+' || *p == '|')
851       *p = '|';
852     else
853       *p = ' ';
854 }
855
856 static void show_tree_bridge(struct bridge *, byte *, byte *);
857
858 static void
859 show_tree_dev(struct device *d, byte *line, byte *p)
860 {
861   struct bridge *b;
862
863   p += sprintf(p, "%02x.%x", PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
864   for(b=&host_bridge; b; b=b->chain)
865     if (b->br_dev == d)
866       {
867         if (b->secondary == b->subordinate)
868           p += sprintf(p, "-[%02x]-", b->secondary);
869         else
870           p += sprintf(p, "-[%02x-%02x]-", b->secondary, b->subordinate);
871         show_tree_bridge(b, line, p);
872         return;
873       }
874   if (verbose)
875     p += sprintf(p, "  %s", lookup_device_full(d->vendid, d->devid));
876   print_it(line, p);
877 }
878
879 static void
880 show_tree_bus(struct bus *b, byte *line, byte *p)
881 {
882   if (!b->first_dev)
883     print_it(line, p);
884   else if (!b->first_dev->next)
885     {
886       *p++ = '-';
887       *p++ = '-';
888       show_tree_dev(b->first_dev, line, p);
889     }
890   else
891     {
892       struct device *d = b->first_dev;
893       while (d->next)
894         {
895           p[0] = '+';
896           p[1] = '-';
897           show_tree_dev(d, line, p+2);
898           d = d->next;
899         }
900       p[0] = '\\';
901       p[1] = '-';
902       show_tree_dev(d, line, p+2);
903     }
904 }
905
906 static void
907 show_tree_bridge(struct bridge *b, byte *line, byte *p)
908 {
909   *p++ = '-';
910   if (!b->first_bus->sibling)
911     {
912       if (b == &host_bridge)
913         p += sprintf(p, "[%02x]-", b->first_bus->number);
914       show_tree_bus(b->first_bus, line, p);
915     }
916   else
917     {
918       struct bus *u = b->first_bus;
919       byte *k;
920
921       while (u->sibling)
922         {
923           k = p + sprintf(p, "+-[%02x]-", u->number);
924           show_tree_bus(u, line, k);
925           u = u->sibling;
926         }
927       k = p + sprintf(p, "\\-[%02x]-", u->number);
928       show_tree_bus(u, line, k);
929     }
930 }
931
932 static void
933 show_forest(void)
934 {
935   char line[256];
936
937   grow_tree();
938   show_tree_bridge(&host_bridge, line, line);
939 }
940
941 /* Main */
942
943 int
944 main(int argc, char **argv)
945 {
946   int i;
947   char *msg;
948
949   if (argc == 2 && !strcmp(argv[1], "--version"))
950     {
951       puts("lspci version " PCIUTILS_VERSION);
952       return 0;
953     }
954   filter_init(&filter);
955   while ((i = getopt(argc, argv, options)) != -1)
956     switch (i)
957       {
958       case 'n':
959         show_numeric_ids = 1;
960         break;
961       case 'v':
962         verbose++;
963         break;
964       case 'b':
965         buscentric_view = 1;
966         break;
967       case 's':
968         if (msg = filter_parse_slot(&filter, optarg))
969           {
970             fprintf(stderr, "lspci: -f: %s\n", msg);
971             return 1;
972           }
973         break;
974       case 'd':
975         if (msg = filter_parse_id(&filter, optarg))
976           {
977             fprintf(stderr, "lspci: -d: %s\n", msg);
978             return 1;
979           }
980         break;
981       case 'x':
982         show_hex++;
983         break;
984       case 't':
985         show_tree++;
986         break;
987       case 'i':
988         pci_ids = optarg;
989         break;
990       case 'p':
991         pci_dir = optarg;
992         break;
993       case 'm':
994         machine_readable++;
995         break;
996       default:
997       bad:
998         fprintf(stderr, help_msg);
999         return 1;
1000       }
1001   if (optind < argc)
1002     goto bad;
1003
1004   scan_proc();
1005   sort_them();
1006   if (show_tree)
1007     show_forest();
1008   else
1009     show();
1010
1011   return 0;
1012 }