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