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