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