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