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