]> mj.ucw.cz Git - pciutils.git/blob - lspci.c
- Minor tweaks for first publicly distributed beta-version.
[pciutils.git] / lspci.c
1 /*
2  *      $Id: lspci.c,v 1.3 1997/12/27 11:57:12 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 (verbose > 1)
277         printf("\tRegion %d: ", i);
278       else
279         putchar('\t');
280       if (flg & PCI_BASE_ADDRESS_SPACE_IO)
281         {
282           if (cmd & PCI_COMMAND_IO)
283             printf("I/O ports at %04lx\n", pos & PCI_BASE_ADDRESS_IO_MASK);
284         }
285       else if (cmd & PCI_COMMAND_MEMORY)
286         {
287           int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
288           printf("Memory at ");
289           if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
290             {
291               if (i < cnt - 1)
292                 {
293                   i++;
294                   if (!buscentric_view)
295                     printf("%08x", get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i));
296                 }
297               else
298                 printf("????????");
299             }
300           printf("%08lx (%s, %sprefetchable)\n",
301                  pos & PCI_BASE_ADDRESS_MEM_MASK,
302                  (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
303                  (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
304                  (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M 32-bit" : "???",
305                  (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
306         }
307     }
308 }
309
310 static void
311 show_htype0(struct device *d)
312 {
313   u32 rom = get_conf_long(d, PCI_ROM_ADDRESS);
314
315   show_bases(d, 6);
316
317   if (rom & 1)
318     {
319       word cmd = get_conf_word(d, PCI_COMMAND);
320       printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
321              (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
322     }
323 }
324
325 static void
326 show_htype1(struct device *d)
327 {
328   u32 io_base = get_conf_byte(d, PCI_IO_BASE);
329   u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
330   u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
331   u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
332   u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
333   u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
334   u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
335   u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
336   u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
337   u32 rom = get_conf_long(d, PCI_ROM_ADDRESS1);
338   word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
339
340   show_bases(d, 2);
341   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
342          get_conf_byte(d, PCI_PRIMARY_BUS),
343          get_conf_byte(d, PCI_SECONDARY_BUS),
344          get_conf_byte(d, PCI_SUBORDINATE_BUS),
345          get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
346
347   if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
348       (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
349     printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
350   else
351     {
352       io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
353       io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
354       if (io_type == PCI_IO_RANGE_TYPE_32)
355         {
356           io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
357           io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
358         }
359       if (io_base)
360         printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
361     }
362
363   if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
364       mem_type)
365     printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
366   else if (mem_base)
367     {
368       mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
369       mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
370       printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
371     }
372
373   if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
374       (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
375     printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
376   else if (pref_base)
377     {
378       pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
379       pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
380       if (pref_type == PCI_PREF_RANGE_TYPE_32)
381         printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit);
382       else
383         printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
384                get_conf_long(d, PCI_PREF_BASE_UPPER32),
385                pref_base,
386                get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
387                pref_limit);
388     }
389
390   if (get_conf_word(d, PCI_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
391     printf("\tSecondary status: SERR\n");
392
393   if (rom & 1)
394     {
395       word cmd = get_conf_word(d, PCI_COMMAND);
396       printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
397              (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
398     }
399
400   if (verbose > 1)
401     printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
402            (brc & PCI_BRIDGE_CTL_PARITY) ? '+' : '-',
403            (brc & PCI_BRIDGE_CTL_SERR) ? '+' : '-',
404            (brc & PCI_BRIDGE_CTL_NO_ISA) ? '+' : '-',
405            (brc & PCI_BRIDGE_CTL_VGA) ? '+' : '-',
406            (brc & PCI_BRIDGE_CTL_MASTER_ABORT) ? '+' : '-',
407            (brc & PCI_BRIDGE_CTL_BUS_RESET) ? '+' : '-',
408            (brc & PCI_BRIDGE_CTL_FAST_BACK) ? '+' : '-');
409 }
410
411 static void
412 show_verbose(struct device *d)
413 {
414   word status = get_conf_word(d, PCI_STATUS);
415   word cmd = get_conf_word(d, PCI_COMMAND);
416   word class = get_conf_word(d, PCI_CLASS_DEVICE);
417   byte bist = get_conf_byte(d, PCI_BIST);
418   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
419   byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
420   byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
421   byte max_lat, min_gnt;
422   byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
423   byte int_line = get_conf_byte(d, PCI_INTERRUPT_LINE);
424   unsigned int irq, ex_htype;
425   word subsys_v, subsys_d;
426
427   show_terse(d);
428
429   switch (class)
430     {
431     case PCI_CLASS_BRIDGE_PCI:
432       ex_htype = 1;
433       break;
434     default:
435       ex_htype = 0;
436     }
437   if (ex_htype != htype)
438     {
439       printf("\t!!! Header type %02x doesn't match class code %04x\n", htype, class);
440       return;
441     }
442
443   switch (htype)
444     {
445     case 0:
446       max_lat = get_conf_byte(d, PCI_MAX_LAT);
447       min_gnt = get_conf_byte(d, PCI_MIN_GNT);
448       subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
449       subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
450       break;
451     case 1:
452       irq = int_line = int_pin = min_gnt = max_lat = 0;
453       subsys_v = subsys_d = 0;
454       break;
455     default:
456       printf("\t!!! Unknown header type %02x\n", htype);
457       return;
458     }
459
460   if (buscentric_view)
461     irq = int_line;
462   else
463     irq = d->kernel_irq;
464
465   if (verbose > 1)
466     {
467       if (subsys_v)
468         printf("\tSubsystem ID: %04x:%04x\n", subsys_v, subsys_d);
469       printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
470              (cmd & PCI_COMMAND_IO) ? '+' : '-',
471              (cmd & PCI_COMMAND_MEMORY) ? '+' : '-',
472              (cmd & PCI_COMMAND_MASTER) ? '+' : '-',
473              (cmd & PCI_COMMAND_SPECIAL) ? '+' : '-',
474              (cmd & PCI_COMMAND_INVALIDATE) ? '+' : '-',
475              (cmd & PCI_COMMAND_VGA_PALETTE) ? '+' : '-',
476              (cmd & PCI_COMMAND_PARITY) ? '+' : '-',
477              (cmd & PCI_COMMAND_WAIT) ? '+' : '-',
478              (cmd & PCI_COMMAND_SERR) ? '+' : '-',
479              (cmd & PCI_COMMAND_FAST_BACK) ? '+' : '-');
480       printf("\tStatus: 66Mhz%c UDF%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c >SERR%c <PERR%c\n",
481              (status & PCI_STATUS_66MHZ) ? '+' : '-',
482              (status & PCI_STATUS_UDF) ? '+' : '-',
483              (status & PCI_STATUS_FAST_BACK) ? '+' : '-',
484              (status & PCI_STATUS_PARITY) ? '+' : '-',
485              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
486              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
487              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
488              (status & PCI_STATUS_SIG_TARGET_ABORT) ? '+' : '-',
489              (status & PCI_STATUS_REC_TARGET_ABORT) ? '+' : '-',
490              (status & PCI_STATUS_REC_MASTER_ABORT) ? '+' : '-',
491              (status & PCI_STATUS_SIG_SYSTEM_ERROR) ? '+' : '-',
492              (status & PCI_STATUS_DETECTED_PARITY) ? '+' : '-');
493       if (cmd & PCI_COMMAND_MASTER)
494         {
495           printf("\tLatency: ");
496           if (min_gnt)
497             printf("%d min, ", min_gnt);
498           if (max_lat)
499             printf("%d max, ", max_lat);
500           printf("%d set", latency);
501           if (cache_line)
502             printf(", cache line size %02x", cache_line);
503           putchar('\n');
504         }
505       if (int_pin)
506         printf("\tInterrupt: pin %c routed to IRQ " IRQ_FORMAT "\n", 'A' + int_pin - 1, irq);
507     }
508   else
509     {
510       printf("\tFlags: ");
511       if (cmd & PCI_COMMAND_MASTER)
512         printf("bus master, ");
513       if (cmd & PCI_COMMAND_VGA_PALETTE)
514         printf("VGA palette snoop, ");
515       if (cmd & PCI_COMMAND_WAIT)
516         printf("stepping, ");
517       if (cmd & PCI_COMMAND_FAST_BACK)
518         printf("fast Back2Back, ");
519       if (status & PCI_STATUS_66MHZ)
520         printf("66Mhz, ");
521       if (status & PCI_STATUS_UDF)
522         printf("user-definable features, ");
523       printf("%s devsel",
524              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
525              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
526              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
527       if (cmd & PCI_COMMAND_MASTER)
528         printf(", latency %d", latency);
529       if (int_pin)
530         if (d->kernel_irq)
531           printf(", IRQ " IRQ_FORMAT, irq);
532         else
533           printf(", IRQ ?");
534       putchar('\n');
535     }
536
537   if (bist & PCI_BIST_CAPABLE)
538     {
539       if (bist & PCI_BIST_START)
540         printf("\tBIST is running\n");
541       else
542         printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
543     }
544
545   switch (htype)
546     {
547     case 0:
548       show_htype0(d);
549       break;
550     case 1:
551       show_htype1(d);
552       break;
553     }
554 }
555
556 static void
557 show_hex_dump(struct device *d)
558 {
559   int i;
560   int limit = (show_hex > 1) ? 256 : 64;
561
562   for(i=0; i<limit; i++)
563     {
564       if (! (i & 15))
565         printf("%02x:", i);
566       printf(" %02x", get_conf_byte(d, i));
567       if ((i & 15) == 15)
568         putchar('\n');
569     }
570 }
571
572 static void
573 show(void)
574 {
575   struct device *d;
576
577   for(d=first_dev; d; d=d->next)
578     {
579       if (verbose)
580         show_verbose(d);
581       else
582         show_terse(d);
583       if (show_hex)
584         show_hex_dump(d);
585       if (verbose || show_hex)
586         putchar('\n');
587     }
588 }
589
590 /* Tree output */
591
592 struct bridge {
593   struct bridge *chain;                 /* Single-linked list of bridges */
594   struct bridge *next, *child;          /* Tree of bridges */
595   struct bus *first_bus;                /* List of busses connected to this bridge */
596   unsigned int primary, secondary, subordinate; /* Bus numbers */
597   struct device *br_dev;
598 };
599
600 struct bus {
601   unsigned int number;
602   struct bus *sibling;
603   struct device *first_dev, **last_dev;
604 };
605
606 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, ~0, 0, ~0, NULL };
607
608 static struct bus *
609 find_bus(struct bridge *b, unsigned int n)
610 {
611   struct bus *bus;
612
613   for(bus=b->first_bus; bus; bus=bus->sibling)
614     if (bus->number == n)
615       break;
616   return bus;
617 }
618
619 static struct bus *
620 new_bus(struct bridge *b, unsigned int n)
621 {
622   struct bus *bus = xmalloc(sizeof(struct bus));
623
624   bus = xmalloc(sizeof(struct bus));
625   bus->number = n;
626   bus->sibling = b->first_bus;
627   bus->first_dev = NULL;
628   bus->last_dev = &bus->first_dev;
629   b->first_bus = bus;
630   return bus;
631 }
632
633 static void
634 insert_dev(struct device *d, struct bridge *b)
635 {
636   struct bus *bus;
637
638   if (! (bus = find_bus(b, d->bus)))
639     {
640       struct bridge *c;
641       for(c=b->child; c; c=c->next)
642         if (c->secondary <= d->bus && d->bus <= c->subordinate)
643           return insert_dev(d, c);
644       bus = new_bus(b, d->bus);
645     }
646   /* Simple insertion at the end _does_ guarantee the correct order as the
647    * original device list was sorted by (bus, devfn) lexicographically
648    * and all devices on the new list have the same bus number.
649    */
650   *bus->last_dev = d;
651   bus->last_dev = &d->next;
652   d->next = NULL;
653 }
654
655 static void
656 grow_tree(void)
657 {
658   struct device *d, *d2;
659   struct bridge *first_br, *b;
660
661   /* Build list of bridges */
662
663   first_br = &host_bridge;
664   for(d=first_dev; d; d=d->next)
665     {
666       word class = get_conf_word(d, PCI_CLASS_DEVICE);
667       if (class == PCI_CLASS_BRIDGE_PCI && (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f) == 1)
668         {
669           b = xmalloc(sizeof(struct bridge));
670           b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
671           b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
672           b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
673           b->chain = first_br;
674           first_br = b;
675           b->next = b->child = NULL;
676           b->first_bus = NULL;
677           b->br_dev = d;
678         }
679     }
680
681   /* Create a bridge tree */
682
683   for(b=first_br; b; b=b->chain)
684     {
685       struct bridge *c, *best;
686       best = NULL;
687       for(c=first_br; c; c=c->chain)
688         if (c != b && b->primary >= c->secondary && b->primary <= c->subordinate &&
689             (!best || best->subordinate - best->primary > c->subordinate - c->primary))
690           best = c;
691       if (best)
692         {
693           b->next = best->child;
694           best->child = b;
695         }
696     }
697
698   /* Insert secondary bus for each bridge */
699
700   for(b=first_br; b; b=b->chain)
701     if (!find_bus(b, b->secondary))
702       new_bus(b, b->secondary);
703
704   /* Create bus structs and link devices */
705
706   for(d=first_dev; d;)
707     {
708       d2 = d->next;
709       insert_dev(d, &host_bridge);
710       d = d2;
711     }
712 }
713
714 static void
715 print_it(byte *line, byte *p)
716 {
717   *p++ = '\n';
718   *p = 0;
719   fputs(line, stdout);
720   for(p=line; *p; p++)
721     if (*p == '+')
722       *p = '|';
723     else
724       *p = ' ';
725 }
726
727 static void show_tree_bridge(struct bridge *, byte *, byte *);
728
729 static void
730 show_tree_dev(struct device *d, byte *line, byte *p)
731 {
732   struct bridge *b;
733
734   p += sprintf(p, "%02x.%x", PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
735   for(b=&host_bridge; b; b=b->chain)
736     if (b->br_dev == d)
737       {
738         p += sprintf(p, "-[%02x-%02x]-", b->secondary, b->subordinate);
739         show_tree_bridge(b, line, p);
740         return;
741       }
742   print_it(line, p);
743 }
744
745 static void
746 show_tree_bus(struct bus *b, byte *line, byte *p)
747 {
748   if (!b->first_dev)
749     print_it(line, p);
750   else if (!b->first_dev->next)
751     {
752       *p++ = '-';
753       *p++ = '-';
754       show_tree_dev(b->first_dev, line, p);
755     }
756   else
757     {
758       struct device *d = b->first_dev;
759       while (d->next)
760         {
761           p[0] = '+';
762           p[1] = '-';
763           show_tree_dev(d, line, p+2);
764           d = d->next;
765         }
766       p[0] = '\\';
767       p[1] = '-';
768       show_tree_dev(d, line, p+2);
769     }
770 }
771
772 static void
773 show_tree_bridge(struct bridge *b, byte *line, byte *p)
774 {
775   *p++ = '-';
776   if (!b->first_bus->sibling)
777     {
778       if (b == &host_bridge)
779         p += sprintf(p, "[%02x]-", b->first_bus->number);
780       show_tree_bus(b->first_bus, line, p);
781     }
782   else
783     {
784       struct bus *u = b->first_bus;
785       byte *k;
786
787       while (u->sibling)
788         {
789           k = p + sprintf(p, "+-[%02x]-", u->number);
790           show_tree_bus(u, line, k);
791           u = u->sibling;
792         }
793       k = p + sprintf(p, "\\-[%02x]-", u->number);
794       show_tree_bus(u, line, k);
795     }
796 }
797
798 static void
799 show_forest(void)
800 {
801   char line[256];
802
803   grow_tree();
804   show_tree_bridge(&host_bridge, line, line);
805 }
806
807 /* Main */
808
809 int
810 main(int argc, char **argv)
811 {
812   int i;
813
814   while ((i = getopt(argc, argv, options)) != -1)
815     switch (i)
816       {
817       case 'n':
818         show_numeric_ids = 1;
819         break;
820       case 'v':
821         verbose++;
822         break;
823       case 'b':
824         buscentric_view = 1;
825         break;
826       case 'B':
827         bus_filter = strtol(optarg, NULL, 16);
828         break;
829       case 'S':
830         slot_filter = strtol(optarg, NULL, 16);
831         break;
832       case 'F':
833         func_filter = strtol(optarg, NULL, 16);
834         break;
835       case 'V':
836         vend_filter = strtol(optarg, NULL, 16);
837         break;
838       case 'D':
839         dev_filter = strtol(optarg, NULL, 16);
840         break;
841       case 'x':
842         show_hex++;
843         break;
844       case 't':
845         show_tree++;
846         break;
847       default:
848       bad:
849         fprintf(stderr, help_msg);
850         return 1;
851       }
852   if (optind < argc)
853     goto bad;
854
855   scan_proc();
856   sort_them();
857   if (show_tree)
858     show_forest();
859   else
860     show();
861
862   return 0;
863 }