]> mj.ucw.cz Git - pciutils.git/blob - lspci.c
Capability list parser now recognizes all AGP and all PCI Power Management
[pciutils.git] / lspci.c
1 /*
2  *      $Id: lspci.c,v 1.22 1999/01/28 20:16:46 mj Exp $
3  *
4  *      Linux PCI Utilities -- List All PCI Devices
5  *
6  *      Copyright (c) 1997--1999 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 <stdarg.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 struct pci_filter filter;        /* Device filter */
25 static int show_tree;                   /* Show bus tree */
26 static int machine_readable;            /* Generate machine-readable output */
27 static int map_mode;                    /* Bus mapping mode enabled */
28
29 static char options[] = "nvbxs:d:ti:mgM" GENERIC_OPTIONS ;
30
31 static char help_msg[] = "\
32 Usage: lspci [<switches>]\n\
33 \n\
34 -v\t\tBe verbose\n\
35 -n\t\tShow numeric ID's\n\
36 -b\t\tBus-centric view (PCI addresses and IRQ's instead of those seen by the CPU)\n\
37 -x\t\tShow hex-dump of config space\n\
38 -s [[<bus>]:][<slot>][.[<func>]]\tShow only devices in selected slots\n\
39 -d [<vendor>]:[<device>]\tShow only selected devices\n\
40 -t\t\tShow bus tree\n\
41 -m\t\tProduce machine-readable output\n\
42 -i <file>\tUse specified ID database instead of %s\n\
43 -M\t\tEnable `bus mapping' mode (dangerous; root only)\n"
44 GENERIC_HELP
45 ;
46
47 /* Communication with libpci */
48
49 static struct pci_access *pacc;
50
51 /* Format strings used for IRQ numbers and memory addresses */
52
53 #ifdef ARCH_SPARC64
54 #define IRQ_FORMAT "%08x"
55 #else
56 #define IRQ_FORMAT "%d"
57 #endif
58
59 #ifdef HAVE_64BIT_LONG_INT
60 #define LONG_FORMAT "%016lx"
61 #else
62 #define LONG_FORMAT "%08lx"
63 #endif
64
65 /* Our view of the PCI bus */
66
67 struct device {
68   struct device *next;
69   struct pci_dev *dev;
70   unsigned int config_cnt;
71   byte config[256];
72 };
73
74 static struct device *first_dev;
75
76 static struct device *
77 scan_device(struct pci_dev *p)
78 {
79   int how_much = (show_hex > 2) ? 256 : 64;
80   struct device *d;
81
82   if (!pci_filter_match(&filter, p))
83     return NULL;
84   d = xmalloc(sizeof(struct device));
85   bzero(d, sizeof(*d));
86   d->dev = p;
87   if (!pci_read_block(p, 0, d->config, how_much))
88     die("Unable to read %d bytes of configuration space.", how_much);
89   if (how_much < 128 && (d->config[PCI_HEADER_TYPE] & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
90     {
91       /* For cardbus bridges, we need to fetch 64 bytes more to get the full standard header... */
92       if (!pci_read_block(p, 0, d->config+64, 64))
93         die("Unable to read cardbus bridge extension data.");
94       how_much = 128;
95     }
96   d->config_cnt = how_much;
97   pci_setup_cache(p, d->config, d->config_cnt);
98   pci_fill_info(p, PCI_FILL_IDENT | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE);
99   return d;
100 }
101
102 static void
103 scan_devices(void)
104 {
105   struct device *d;
106   struct pci_dev *p;
107
108   pci_scan_bus(pacc);
109   for(p=pacc->devices; p; p=p->next)
110     if (d = scan_device(p))
111       {
112         d->next = first_dev;
113         first_dev = d;
114       }
115 }
116
117 static int
118 check_root(void)
119 {
120   static int is_root = -1;
121
122   if (is_root < 0)
123     is_root = !geteuid();
124   return is_root;
125 }
126
127 static int
128 config_fetch(struct device *d, unsigned int pos, unsigned int len)
129 {
130   if (pos + len < d->config_cnt)
131     return 1;
132   if (pacc->method != PCI_ACCESS_DUMP && !check_root())
133     return 0;
134   return pci_read_block(d->dev, pos, d->config + pos, len);
135 }
136
137 /* Config space accesses */
138
139 static inline byte
140 get_conf_byte(struct device *d, unsigned int pos)
141 {
142   return d->config[pos];
143 }
144
145 static word
146 get_conf_word(struct device *d, unsigned int pos)
147 {
148   return d->config[pos] | (d->config[pos+1] << 8);
149 }
150
151 static u32
152 get_conf_long(struct device *d, unsigned int pos)
153 {
154   return d->config[pos] |
155     (d->config[pos+1] << 8) |
156     (d->config[pos+2] << 16) |
157     (d->config[pos+3] << 24);
158 }
159
160 /* Sorting */
161
162 static int
163 compare_them(const void *A, const void *B)
164 {
165   const struct pci_dev *a = (*(const struct device **)A)->dev;
166   const struct pci_dev *b = (*(const struct device **)B)->dev;
167
168   if (a->bus < b->bus)
169     return -1;
170   if (a->bus > b->bus)
171     return 1;
172   if (a->dev < b->dev)
173     return -1;
174   if (a->dev > b->dev)
175     return 1;
176   if (a->func < b->func)
177     return -1;
178   if (a->func > b->func)
179     return 1;
180   return 0;
181 }
182
183 static void
184 sort_them(void)
185 {
186   struct device **index, **h, **last_dev;
187   int cnt;
188   struct device *d;
189
190   cnt = 0;
191   for(d=first_dev; d; d=d->next)
192     cnt++;
193   h = index = alloca(sizeof(struct device *) * cnt);
194   for(d=first_dev; d; d=d->next)
195     *h++ = d;
196   qsort(index, cnt, sizeof(struct device *), compare_them);
197   last_dev = &first_dev;
198   h = index;
199   while (cnt--)
200     {
201       *last_dev = *h;
202       last_dev = &(*h)->next;
203       h++;
204     }
205   *last_dev = NULL;
206 }
207
208 /* Normal output */
209
210 #define FLAG(x,y) ((x & y) ? '+' : '-')
211
212 static void
213 show_terse(struct device *d)
214 {
215   int c;
216   struct pci_dev *p = d->dev;
217   byte classbuf[128], devbuf[128];
218
219   printf("%02x:%02x.%x %s: %s",
220          p->bus,
221          p->dev,
222          p->func,
223          pci_lookup_name(pacc, classbuf, sizeof(classbuf),
224                          PCI_LOOKUP_CLASS,
225                          get_conf_word(d, PCI_CLASS_DEVICE), 0),
226          pci_lookup_name(pacc, devbuf, sizeof(devbuf),
227                          PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
228                          p->vendor_id, p->device_id));
229   if (c = get_conf_byte(d, PCI_REVISION_ID))
230     printf(" (rev %02x)", c);
231   if (verbose && (c = get_conf_byte(d, PCI_CLASS_PROG)))
232     printf(" (prog-if %02x)", c);
233   putchar('\n');
234 }
235
236 static void
237 show_bases(struct device *d, int cnt)
238 {
239   struct pci_dev *p = d->dev;
240   word cmd = get_conf_word(d, PCI_COMMAND);
241   int i;
242
243   for(i=0; i<cnt; i++)
244     {
245       unsigned long pos;
246       unsigned int flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
247       pos = p->base_addr[i];
248       if (flg == 0xffffffff)
249         flg = 0;
250       if (!pos && !flg)
251         continue;
252       if (verbose > 1)
253         printf("\tRegion %d: ", i);
254       else
255         putchar('\t');
256       if (pos && !flg)                  /* Reported by the OS, but not by the device */
257         {
258           printf("[virtual] ");
259           flg = pos;
260         }
261       if (flg & PCI_BASE_ADDRESS_SPACE_IO)
262         {
263           unsigned long a = pos & PCI_BASE_ADDRESS_IO_MASK;
264           printf("I/O ports at ");
265           if (a)
266             printf("%04lx", a);
267           else if (flg & PCI_BASE_ADDRESS_IO_MASK)
268             printf("<ignored>");
269           else
270             printf("<unassigned>");
271           if (!(cmd & PCI_COMMAND_IO))
272             printf(" [disabled]");
273         }
274       else
275         {
276           int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
277           unsigned long a = pos & PCI_BASE_ADDRESS_MEM_MASK;
278           int done = 0;
279           u32 z = 0;
280
281           printf("Memory at ");
282           if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
283             {
284               if (i >= cnt - 1)
285                 {
286                   printf("<invalid-64bit-slot>\n");
287                   done = 1;
288                 }
289               else
290                 {
291                   i++;
292                   z = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
293                   if (buscentric_view)
294                     {
295                       if (a || z)
296                         printf("%08x%08lx", z, a);
297                       else
298                         printf("<unassigned>");
299                       done = 1;
300                     }
301                 }
302             }
303           if (!done)
304             {
305               if (a)
306                 printf(LONG_FORMAT, a);
307               else
308                 printf(((flg & PCI_BASE_ADDRESS_MEM_MASK) || z) ? "<ignored>" : "<unassigned>");
309             }
310           printf(" (%s, %sprefetchable)",
311                  (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
312                  (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
313                  (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M" : "type 3",
314                  (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
315           if (!(cmd & PCI_COMMAND_MEMORY))
316             printf(" [disabled]");
317         }
318       putchar('\n');
319     }
320 }
321
322 static void
323 show_pm(struct device *d, int where, int cap)
324 {
325   int t;
326
327   printf("Power Management version %d\n", cap & PCI_PM_CAP_VER_MASK);
328   if (verbose < 2)
329     return;
330   printf("\t\tFlags: PMEClk%c AuxPwr%c DSI%c D1%c D2%c PME%c\n",
331          FLAG(cap, PCI_PM_CAP_PME_CLOCK),
332          FLAG(cap, PCI_PM_CAP_AUX_POWER),
333          FLAG(cap, PCI_PM_CAP_DSI),
334          FLAG(cap, PCI_PM_CAP_D1),
335          FLAG(cap, PCI_PM_CAP_D2),
336          FLAG(cap, PCI_PM_CAP_PME));
337   config_fetch(d, where + PCI_PM_CTRL, PCI_PM_SIZEOF - PCI_PM_CTRL);
338   t = get_conf_word(d, where + PCI_PM_CTRL);
339   printf("\t\tStatus: D%d PME-Enable%c DSel=%x DScale=%x PME%c\n",
340          t & PCI_PM_CTRL_STATE_MASK,
341          FLAG(t, PCI_PM_CTRL_PME_ENABLE),
342          (t & PCI_PM_CTRL_DATA_SEL_MASK) >> 9,
343          (t & PCI_PM_CTRL_DATA_SCALE_MASK) >> 13,
344          FLAG(t, PCI_PM_CTRL_PME_STATUS));
345 }
346
347 static void
348 show_agp(struct device *d, int where, int cap)
349 {
350   u32 t;
351
352   t = cap & 0xff;
353   printf("AGP version %x.%x\n", cap/16, cap%16);
354   if (verbose < 2)
355     return;
356   config_fetch(d, where + PCI_AGP_STATUS, PCI_AGP_SIZEOF - PCI_AGP_STATUS);
357   t = get_conf_long(d, where + PCI_AGP_STATUS);
358   printf("\t\tStatus: RQ=%d SBA%c 64bit%c FW%c Rate=%s%s%s\n",
359          (t & PCI_AGP_STATUS_RQ_MASK) >> 24U,
360          FLAG(t, PCI_AGP_STATUS_SBA),
361          FLAG(t, PCI_AGP_STATUS_64BIT),
362          FLAG(t, PCI_AGP_STATUS_FW),
363          (t & PCI_AGP_STATUS_RATE4) ? "4" : "",
364          (t & PCI_AGP_STATUS_RATE2) ? "2" : "",
365          (t & PCI_AGP_STATUS_RATE1) ? "1" : "");
366   printf("\t\tCommand: RQ=%d SBA%c AGP%c 64bit%c FW%c Rate=%s%s%s\n",
367          (t & PCI_AGP_COMMAND_RQ_MASK) >> 24U,
368          FLAG(t, PCI_AGP_COMMAND_SBA),
369          FLAG(t, PCI_AGP_COMMAND_AGP),
370          FLAG(t, PCI_AGP_COMMAND_64BIT),
371          FLAG(t, PCI_AGP_COMMAND_FW),
372          (t & PCI_AGP_COMMAND_RATE4) ? "4" : "",
373          (t & PCI_AGP_COMMAND_RATE2) ? "2" : "",
374          (t & PCI_AGP_COMMAND_RATE1) ? "1" : "");
375 }
376
377 static void
378 show_htype0(struct device *d)
379 {
380   unsigned long rom = d->dev->rom_base_addr;
381
382   show_bases(d, 6);
383   if (rom & 1)
384     printf("\tExpansion ROM at %08lx%s\n", rom & PCI_ROM_ADDRESS_MASK,
385            (rom & PCI_ROM_ADDRESS_ENABLE) ? "" : " [disabled]");
386   if (get_conf_word(d, PCI_STATUS) & PCI_STATUS_CAP_LIST)
387     {
388       int where = get_conf_byte(d, PCI_CAPABILITY_LIST);
389       while (where)
390         {
391           int id, next, cap;
392           printf("\tCapabilities: ");
393           if (!config_fetch(d, where, 4))
394             {
395               puts("<available only to root>");
396               break;
397             }
398           id = get_conf_byte(d, where + PCI_CAP_LIST_ID);
399           next = get_conf_byte(d, where + PCI_CAP_LIST_NEXT);
400           cap = get_conf_word(d, where + PCI_CAP_FLAGS);
401           printf("[%02x] ", where);
402           if (id == 0xff)
403             {
404               printf("<chain broken>\n");
405               break;
406             }
407           switch (id)
408             {
409             case PCI_CAP_ID_PM:
410               show_pm(d, where, cap);
411               break;
412             case PCI_CAP_ID_AGP:
413               show_agp(d, where, cap);
414               break;
415             default:
416               printf("#%02x [%04x]", id, cap);
417             }
418           where = next;
419         }
420     }
421 }
422
423 static void
424 show_htype1(struct device *d)
425 {
426   struct pci_dev *p = d->dev;
427   u32 io_base = get_conf_byte(d, PCI_IO_BASE);
428   u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
429   u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
430   u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
431   u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
432   u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
433   u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
434   u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
435   u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
436   unsigned long rom = p->rom_base_addr;
437   word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
438
439   show_bases(d, 2);
440   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
441          get_conf_byte(d, PCI_PRIMARY_BUS),
442          get_conf_byte(d, PCI_SECONDARY_BUS),
443          get_conf_byte(d, PCI_SUBORDINATE_BUS),
444          get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
445
446   if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
447       (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
448     printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
449   else
450     {
451       io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
452       io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
453       if (io_type == PCI_IO_RANGE_TYPE_32)
454         {
455           io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
456           io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
457         }
458       if (io_base)
459         printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
460     }
461
462   if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
463       mem_type)
464     printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
465   else if (mem_base)
466     {
467       mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
468       mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
469       printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
470     }
471
472   if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
473       (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
474     printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
475   else if (pref_base)
476     {
477       pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
478       pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
479       if (pref_type == PCI_PREF_RANGE_TYPE_32)
480         printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit + 0xfffff);
481       else
482         printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
483                get_conf_long(d, PCI_PREF_BASE_UPPER32),
484                pref_base,
485                get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
486                pref_limit);
487     }
488
489   if (get_conf_word(d, PCI_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
490     printf("\tSecondary status: SERR\n");
491
492   if (rom & 1)
493     printf("\tExpansion ROM at %08lx%s\n", rom & PCI_ROM_ADDRESS_MASK,
494            (rom & PCI_ROM_ADDRESS_ENABLE) ? "" : " [disabled]");
495
496   if (verbose > 1)
497     printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
498            FLAG(brc, PCI_BRIDGE_CTL_PARITY),
499            FLAG(brc, PCI_BRIDGE_CTL_SERR),
500            FLAG(brc, PCI_BRIDGE_CTL_NO_ISA),
501            FLAG(brc, PCI_BRIDGE_CTL_VGA),
502            FLAG(brc, PCI_BRIDGE_CTL_MASTER_ABORT),
503            FLAG(brc, PCI_BRIDGE_CTL_BUS_RESET),
504            FLAG(brc, PCI_BRIDGE_CTL_FAST_BACK));
505 }
506
507 static void
508 show_htype2(struct device *d)
509 {
510   int i;
511   word cmd = get_conf_word(d, PCI_COMMAND);
512   word brc = get_conf_word(d, PCI_CB_BRIDGE_CONTROL);
513   word exca = get_conf_word(d, PCI_CB_LEGACY_MODE_BASE);
514
515   show_bases(d, 1);
516   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
517          get_conf_byte(d, PCI_CB_PRIMARY_BUS),
518          get_conf_byte(d, PCI_CB_CARD_BUS),
519          get_conf_byte(d, PCI_CB_SUBORDINATE_BUS),
520          get_conf_byte(d, PCI_CB_LATENCY_TIMER));
521   for(i=0; i<2; i++)
522     {
523       int p = 8*i;
524       u32 base = get_conf_long(d, PCI_CB_MEMORY_BASE_0 + p);
525       u32 limit = get_conf_long(d, PCI_CB_MEMORY_LIMIT_0 + p);
526       if (limit > base)
527         printf("Memory window %d: %08x-%08x%s%s\n", i, base, limit,
528                (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]",
529                (brc & (PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 << i)) ? " (prefetchable)" : "");
530     }
531   for(i=0; i<2; i++)
532     {
533       int p = 8*i;
534       u32 base = get_conf_long(d, PCI_CB_IO_BASE_0 + p);
535       u32 limit = get_conf_long(d, PCI_CB_IO_LIMIT_0 + p);
536       if (!(base & PCI_IO_RANGE_TYPE_32))
537         {
538           base &= 0xffff;
539           limit &= 0xffff;
540         }
541       base &= PCI_CB_IO_RANGE_MASK;
542       if (!base)
543         continue;
544       limit = (limit & PCI_CB_IO_RANGE_MASK) + 3;
545       printf("I/O window %d: %08x-%08x%s\n", i, base, limit,
546              (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
547     }
548
549   if (get_conf_word(d, PCI_CB_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
550     printf("\tSecondary status: SERR\n");
551   if (verbose > 1)
552     printf("\tBridgeCtl: Parity%c SERR%c ISA%c VGA%c MAbort%c >Reset%c 16bInt%c PostWrite%c\n",
553            FLAG(brc, PCI_CB_BRIDGE_CTL_PARITY),
554            FLAG(brc, PCI_CB_BRIDGE_CTL_SERR),
555            FLAG(brc, PCI_CB_BRIDGE_CTL_ISA),
556            FLAG(brc, PCI_CB_BRIDGE_CTL_VGA),
557            FLAG(brc, PCI_CB_BRIDGE_CTL_MASTER_ABORT),
558            FLAG(brc, PCI_CB_BRIDGE_CTL_CB_RESET),
559            FLAG(brc, PCI_CB_BRIDGE_CTL_16BIT_INT),
560            FLAG(brc, PCI_CB_BRIDGE_CTL_POST_WRITES));
561   if (exca)
562     printf("\t16-bit legacy interface ports at %04x\n", exca);
563 }
564
565 static void
566 show_verbose(struct device *d)
567 {
568   struct pci_dev *p = d->dev;
569   word status = get_conf_word(d, PCI_STATUS);
570   word cmd = get_conf_word(d, PCI_COMMAND);
571   word class = get_conf_word(d, PCI_CLASS_DEVICE);
572   byte bist = get_conf_byte(d, PCI_BIST);
573   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
574   byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
575   byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
576   byte max_lat, min_gnt;
577   byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
578   unsigned int irq = p->irq;
579   word subsys_v, subsys_d;
580   char ssnamebuf[256];
581
582   show_terse(d);
583
584   switch (htype)
585     {
586     case PCI_HEADER_TYPE_NORMAL:
587       if (class == PCI_CLASS_BRIDGE_PCI)
588         {
589         badhdr:
590           printf("\t!!! Header type %02x doesn't match class code %04x\n", htype, class);
591           return;
592         }
593       max_lat = get_conf_byte(d, PCI_MAX_LAT);
594       min_gnt = get_conf_byte(d, PCI_MIN_GNT);
595       subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
596       subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
597       break;
598     case PCI_HEADER_TYPE_BRIDGE:
599       if (class != PCI_CLASS_BRIDGE_PCI)
600         goto badhdr;
601       irq = int_pin = min_gnt = max_lat = 0;
602       subsys_v = subsys_d = 0;
603       break;
604     case PCI_HEADER_TYPE_CARDBUS:
605       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
606         goto badhdr;
607       min_gnt = max_lat = 0;
608       subsys_v = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
609       subsys_d = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
610       break;
611     default:
612       printf("\t!!! Unknown header type %02x\n", htype);
613       return;
614     }
615
616   if (verbose && subsys_v && subsys_v != 0xffff)
617     printf("\tSubsystem: %s\n",
618            pci_lookup_name(pacc, ssnamebuf, sizeof(ssnamebuf),
619                            PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
620                            subsys_v, subsys_d));
621
622   if (verbose > 1)
623     {
624       printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
625              FLAG(cmd, PCI_COMMAND_IO),
626              FLAG(cmd, PCI_COMMAND_MEMORY),
627              FLAG(cmd, PCI_COMMAND_MASTER),
628              FLAG(cmd, PCI_COMMAND_SPECIAL),
629              FLAG(cmd, PCI_COMMAND_INVALIDATE),
630              FLAG(cmd, PCI_COMMAND_VGA_PALETTE),
631              FLAG(cmd, PCI_COMMAND_PARITY),
632              FLAG(cmd, PCI_COMMAND_WAIT),
633              FLAG(cmd, PCI_COMMAND_SERR),
634              FLAG(cmd, PCI_COMMAND_FAST_BACK));
635       printf("\tStatus: Cap%c 66Mhz%c UDF%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c >SERR%c <PERR%c\n",
636              FLAG(status, PCI_STATUS_CAP_LIST),
637              FLAG(status, PCI_STATUS_66MHZ),
638              FLAG(status, PCI_STATUS_UDF),
639              FLAG(status, PCI_STATUS_FAST_BACK),
640              FLAG(status, PCI_STATUS_PARITY),
641              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
642              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
643              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
644              FLAG(status, PCI_STATUS_SIG_TARGET_ABORT),
645              FLAG(status, PCI_STATUS_REC_TARGET_ABORT),
646              FLAG(status, PCI_STATUS_REC_MASTER_ABORT),
647              FLAG(status, PCI_STATUS_SIG_SYSTEM_ERROR),
648              FLAG(status, PCI_STATUS_DETECTED_PARITY));
649       if (cmd & PCI_COMMAND_MASTER)
650         {
651           printf("\tLatency: ");
652           if (min_gnt)
653             printf("%d min, ", min_gnt);
654           if (max_lat)
655             printf("%d max, ", max_lat);
656           printf("%d set", latency);
657           if (cache_line)
658             printf(", cache line size %02x", cache_line);
659           putchar('\n');
660         }
661       if (int_pin || irq)
662         printf("\tInterrupt: pin %c routed to IRQ " IRQ_FORMAT "\n",
663                (int_pin ? 'A' + int_pin - 1 : '?'), irq);
664     }
665   else
666     {
667       printf("\tFlags: ");
668       if (cmd & PCI_COMMAND_MASTER)
669         printf("bus master, ");
670       if (cmd & PCI_COMMAND_VGA_PALETTE)
671         printf("VGA palette snoop, ");
672       if (cmd & PCI_COMMAND_WAIT)
673         printf("stepping, ");
674       if (cmd & PCI_COMMAND_FAST_BACK)
675         printf("fast Back2Back, ");
676       if (status & PCI_STATUS_66MHZ)
677         printf("66Mhz, ");
678       if (status & PCI_STATUS_UDF)
679         printf("user-definable features, ");
680       printf("%s devsel",
681              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
682              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
683              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
684       if (cmd & PCI_COMMAND_MASTER)
685         printf(", latency %d", latency);
686       if (irq)
687         printf(", IRQ " IRQ_FORMAT, irq);
688       putchar('\n');
689     }
690
691   if (bist & PCI_BIST_CAPABLE)
692     {
693       if (bist & PCI_BIST_START)
694         printf("\tBIST is running\n");
695       else
696         printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
697     }
698
699   switch (htype)
700     {
701     case PCI_HEADER_TYPE_NORMAL:
702       show_htype0(d);
703       break;
704     case PCI_HEADER_TYPE_BRIDGE:
705       show_htype1(d);
706       break;
707     case PCI_HEADER_TYPE_CARDBUS:
708       show_htype2(d);
709       break;
710     }
711 }
712
713 static void
714 show_hex_dump(struct device *d)
715 {
716   unsigned int i;
717
718   for(i=0; i<d->config_cnt; i++)
719     {
720       if (! (i & 15))
721         printf("%02x:", i);
722       printf(" %02x", get_conf_byte(d, i));
723       if ((i & 15) == 15)
724         putchar('\n');
725     }
726 }
727
728 static void
729 show_machine(struct device *d)
730 {
731   struct pci_dev *p = d->dev;
732   int c;
733   word sv_id=0, sd_id=0;
734   char classbuf[128], vendbuf[128], devbuf[128], svbuf[128], sdbuf[128];
735
736   switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
737     {
738     case PCI_HEADER_TYPE_NORMAL:
739       sv_id = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
740       sd_id = get_conf_word(d, PCI_SUBSYSTEM_ID);
741       break;
742     case PCI_HEADER_TYPE_CARDBUS:
743       sv_id = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
744       sd_id = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
745       break;
746     }
747
748   if (verbose)
749     {
750       printf("Device:\t%02x:%02x.%x\n", p->bus, p->dev, p->func);
751       printf("Class:\t%s\n",
752              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, get_conf_word(d, PCI_CLASS_DEVICE), 0));
753       printf("Vendor:\t%s\n",
754              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id));
755       printf("Device:\t%s\n",
756              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id));
757       if (sv_id && sv_id != 0xffff)
758         {
759           printf("SVendor:\t%s\n",
760                  pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id, sd_id));
761           printf("SDevice:\t%s\n",
762                  pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, sv_id, sd_id));
763         }
764       if (c = get_conf_byte(d, PCI_REVISION_ID))
765         printf("Rev:\t%02x\n", c);
766       if (c = get_conf_byte(d, PCI_CLASS_PROG))
767         printf("ProgIf:\t%02x\n", c);
768     }
769   else
770     {
771       printf("%02x:%02x.%x ", p->bus, p->dev, p->func);
772       printf("\"%s\" \"%s\" \"%s\"",
773              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS,
774                              get_conf_word(d, PCI_CLASS_DEVICE), 0),
775              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR,
776                              p->vendor_id, p->device_id),
777              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE,
778                              p->vendor_id, p->device_id));
779       if (c = get_conf_byte(d, PCI_REVISION_ID))
780         printf(" -r%02x", c);
781       if (c = get_conf_byte(d, PCI_CLASS_PROG))
782         printf(" -p%02x", c);
783       if (sv_id && sv_id != 0xffff)
784         printf(" \"%s\" \"%s\"",
785                pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id, sd_id),
786                pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, sv_id, sd_id));
787       else
788         printf(" \"\" \"\"");
789       putchar('\n');
790     }
791 }
792
793 static void
794 show_device(struct device *d)
795 {
796   if (machine_readable)
797     show_machine(d);
798   else if (verbose)
799     show_verbose(d);
800   else
801     show_terse(d);
802   if (show_hex)
803     show_hex_dump(d);
804   if (verbose || show_hex)
805     putchar('\n');
806 }
807
808 static void
809 show(void)
810 {
811   struct device *d;
812
813   for(d=first_dev; d; d=d->next)
814     show_device(d);
815 }
816
817 /* Tree output */
818
819 struct bridge {
820   struct bridge *chain;                 /* Single-linked list of bridges */
821   struct bridge *next, *child;          /* Tree of bridges */
822   struct bus *first_bus;                /* List of busses connected to this bridge */
823   unsigned int primary, secondary, subordinate; /* Bus numbers */
824   struct device *br_dev;
825 };
826
827 struct bus {
828   unsigned int number;
829   struct bus *sibling;
830   struct device *first_dev, **last_dev;
831 };
832
833 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, ~0, 0, ~0, NULL };
834
835 static struct bus *
836 find_bus(struct bridge *b, unsigned int n)
837 {
838   struct bus *bus;
839
840   for(bus=b->first_bus; bus; bus=bus->sibling)
841     if (bus->number == n)
842       break;
843   return bus;
844 }
845
846 static struct bus *
847 new_bus(struct bridge *b, unsigned int n)
848 {
849   struct bus *bus = xmalloc(sizeof(struct bus));
850
851   bus = xmalloc(sizeof(struct bus));
852   bus->number = n;
853   bus->sibling = b->first_bus;
854   bus->first_dev = NULL;
855   bus->last_dev = &bus->first_dev;
856   b->first_bus = bus;
857   return bus;
858 }
859
860 static void
861 insert_dev(struct device *d, struct bridge *b)
862 {
863   struct pci_dev *p = d->dev;
864   struct bus *bus;
865
866   if (! (bus = find_bus(b, p->bus)))
867     {
868       struct bridge *c;
869       for(c=b->child; c; c=c->next)
870         if (c->secondary <= p->bus && p->bus <= c->subordinate)
871           return insert_dev(d, c);
872       bus = new_bus(b, p->bus);
873     }
874   /* Simple insertion at the end _does_ guarantee the correct order as the
875    * original device list was sorted by (bus, devfn) lexicographically
876    * and all devices on the new list have the same bus number.
877    */
878   *bus->last_dev = d;
879   bus->last_dev = &d->next;
880   d->next = NULL;
881 }
882
883 static void
884 grow_tree(void)
885 {
886   struct device *d, *d2;
887   struct bridge **last_br, *b;
888
889   /* Build list of bridges */
890
891   last_br = &host_bridge.chain;
892   for(d=first_dev; d; d=d->next)
893     {
894       word class = get_conf_word(d, PCI_CLASS_DEVICE);
895       byte ht = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
896       if (class == PCI_CLASS_BRIDGE_PCI &&
897           (ht == PCI_HEADER_TYPE_BRIDGE || ht == PCI_HEADER_TYPE_CARDBUS))
898         {
899           b = xmalloc(sizeof(struct bridge));
900           if (ht == PCI_HEADER_TYPE_BRIDGE)
901             {
902               b->primary = get_conf_byte(d, PCI_CB_PRIMARY_BUS);
903               b->secondary = get_conf_byte(d, PCI_CB_CARD_BUS);
904               b->subordinate = get_conf_byte(d, PCI_CB_SUBORDINATE_BUS);
905             }
906           else
907             {
908               b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
909               b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
910               b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
911             }
912           *last_br = b;
913           last_br = &b->chain;
914           b->next = b->child = NULL;
915           b->first_bus = NULL;
916           b->br_dev = d;
917         }
918     }
919   *last_br = NULL;
920
921   /* Create a bridge tree */
922
923   for(b=&host_bridge; b; b=b->chain)
924     {
925       struct bridge *c, *best;
926       best = NULL;
927       for(c=&host_bridge; c; c=c->chain)
928         if (c != b && b->primary >= c->secondary && b->primary <= c->subordinate &&
929             (!best || best->subordinate - best->primary > c->subordinate - c->primary))
930           best = c;
931       if (best)
932         {
933           b->next = best->child;
934           best->child = b;
935         }
936     }
937
938   /* Insert secondary bus for each bridge */
939
940   for(b=&host_bridge; b; b=b->chain)
941     if (!find_bus(b, b->secondary))
942       new_bus(b, b->secondary);
943
944   /* Create bus structs and link devices */
945
946   for(d=first_dev; d;)
947     {
948       d2 = d->next;
949       insert_dev(d, &host_bridge);
950       d = d2;
951     }
952 }
953
954 static void
955 print_it(byte *line, byte *p)
956 {
957   *p++ = '\n';
958   *p = 0;
959   fputs(line, stdout);
960   for(p=line; *p; p++)
961     if (*p == '+' || *p == '|')
962       *p = '|';
963     else
964       *p = ' ';
965 }
966
967 static void show_tree_bridge(struct bridge *, byte *, byte *);
968
969 static void
970 show_tree_dev(struct device *d, byte *line, byte *p)
971 {
972   struct pci_dev *q = d->dev;
973   struct bridge *b;
974   char namebuf[256];
975
976   p += sprintf(p, "%02x.%x", q->dev, q->func);
977   for(b=&host_bridge; b; b=b->chain)
978     if (b->br_dev == d)
979       {
980         if (b->secondary == b->subordinate)
981           p += sprintf(p, "-[%02x]-", b->secondary);
982         else
983           p += sprintf(p, "-[%02x-%02x]-", b->secondary, b->subordinate);
984         show_tree_bridge(b, line, p);
985         return;
986       }
987   if (verbose)
988     p += sprintf(p, "  %s",
989                  pci_lookup_name(pacc, namebuf, sizeof(namebuf),
990                                  PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
991                                  q->vendor_id, q->device_id));
992   print_it(line, p);
993 }
994
995 static void
996 show_tree_bus(struct bus *b, byte *line, byte *p)
997 {
998   if (!b->first_dev)
999     print_it(line, p);
1000   else if (!b->first_dev->next)
1001     {
1002       *p++ = '-';
1003       *p++ = '-';
1004       show_tree_dev(b->first_dev, line, p);
1005     }
1006   else
1007     {
1008       struct device *d = b->first_dev;
1009       while (d->next)
1010         {
1011           p[0] = '+';
1012           p[1] = '-';
1013           show_tree_dev(d, line, p+2);
1014           d = d->next;
1015         }
1016       p[0] = '\\';
1017       p[1] = '-';
1018       show_tree_dev(d, line, p+2);
1019     }
1020 }
1021
1022 static void
1023 show_tree_bridge(struct bridge *b, byte *line, byte *p)
1024 {
1025   *p++ = '-';
1026   if (!b->first_bus->sibling)
1027     {
1028       if (b == &host_bridge)
1029         p += sprintf(p, "[%02x]-", b->first_bus->number);
1030       show_tree_bus(b->first_bus, line, p);
1031     }
1032   else
1033     {
1034       struct bus *u = b->first_bus;
1035       byte *k;
1036
1037       while (u->sibling)
1038         {
1039           k = p + sprintf(p, "+-[%02x]-", u->number);
1040           show_tree_bus(u, line, k);
1041           u = u->sibling;
1042         }
1043       k = p + sprintf(p, "\\-[%02x]-", u->number);
1044       show_tree_bus(u, line, k);
1045     }
1046 }
1047
1048 static void
1049 show_forest(void)
1050 {
1051   char line[256];
1052
1053   grow_tree();
1054   show_tree_bridge(&host_bridge, line, line);
1055 }
1056
1057 /* Bus mapping mode */
1058
1059 struct bus_bridge {
1060   struct bus_bridge *next;
1061   byte this, dev, func, first, last, bug;
1062 };
1063
1064 struct bus_info {
1065   byte exists;
1066   byte guestbook;
1067   struct bus_bridge *bridges, *via;
1068 };
1069
1070 static struct bus_info *bus_info;
1071
1072 static void
1073 map_bridge(struct bus_info *bi, struct device *d, int np, int ns, int nl)
1074 {
1075   struct bus_bridge *b = xmalloc(sizeof(struct bus_bridge));
1076   struct pci_dev *p = d->dev;
1077
1078   b->next = bi->bridges;
1079   bi->bridges = b;
1080   b->this = get_conf_byte(d, np);
1081   b->dev = p->dev;
1082   b->func = p->func;
1083   b->first = get_conf_byte(d, ns);
1084   b->last = get_conf_byte(d, nl);
1085   printf("## %02x.%02x:%d is a bridge from %02x to %02x-%02x\n",
1086          p->bus, p->dev, p->func, b->this, b->first, b->last);
1087   if (b->this != p->bus)
1088     printf("!!! Bridge points to invalid primary bus.\n");
1089   if (b->first > b->last)
1090     {
1091       printf("!!! Bridge points to invalid bus range.\n");
1092       b->last = b->first;
1093     }
1094 }
1095
1096 static void
1097 do_map_bus(int bus)
1098 {
1099   int dev, func;
1100   int verbose = pacc->debugging;
1101   struct bus_info *bi = bus_info + bus;
1102   struct device *d;
1103
1104   if (verbose)
1105     printf("Mapping bus %02x\n", bus);
1106   for(dev = 0; dev < 32; dev++)
1107     if (filter.slot < 0 || filter.slot == dev)
1108       {
1109         for(func = 0; func < 8; func++)
1110           if (filter.func < 0 || filter.func == func)
1111             {
1112               struct pci_dev *p = pci_get_dev(pacc, bus, dev, func);
1113               u16 vendor = pci_read_word(p, PCI_VENDOR_ID);
1114               if (vendor && vendor != 0xffff)
1115                 {
1116                   if (verbose)
1117                     printf("Discovered device %02x:%02x.%d\n", bus, dev, func);
1118                   bi->exists = 1;
1119                   if (d = scan_device(p))
1120                     {
1121                       show_device(d);
1122                       switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
1123                         {
1124                         case PCI_HEADER_TYPE_BRIDGE:
1125                           map_bridge(bi, d, PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS);
1126                           break;
1127                         case PCI_HEADER_TYPE_CARDBUS:
1128                           map_bridge(bi, d, PCI_CB_PRIMARY_BUS, PCI_CB_CARD_BUS, PCI_CB_SUBORDINATE_BUS);
1129                           break;
1130                         }
1131                       free(d);
1132                     }
1133                   else if (verbose)
1134                     printf("But it was filtered out.\n");
1135                 }
1136               pci_free_dev(p);
1137             }
1138       }
1139 }
1140
1141 static void
1142 do_map_bridges(int bus, int min, int max)
1143 {
1144   struct bus_info *bi = bus_info + bus;
1145   struct bus_bridge *b;
1146
1147   bi->guestbook = 1;
1148   for(b=bi->bridges; b; b=b->next)
1149     {
1150       if (bus_info[b->first].guestbook)
1151         b->bug = 1;
1152       else if (b->first < min || b->last > max)
1153         b->bug = 2;
1154       else
1155         {
1156           bus_info[b->first].via = b;
1157           do_map_bridges(b->first, b->first, b->last);
1158         }
1159     }
1160 }
1161
1162 static void
1163 map_bridges(void)
1164 {
1165   int i;
1166
1167   printf("\nSummary of buses:\n\n");
1168   for(i=0; i<256; i++)
1169     if (bus_info[i].exists && !bus_info[i].guestbook)
1170       do_map_bridges(i, 0, 255);
1171   for(i=0; i<256; i++)
1172     {
1173       struct bus_info *bi = bus_info + i;
1174       struct bus_bridge *b = bi->via;
1175
1176       if (bi->exists)
1177         {
1178           printf("%02x: ", i);
1179           if (b)
1180             printf("Entered via %02x:%02x.%d\n", b->this, b->dev, b->func);
1181           else if (!i)
1182             printf("Primary host bus\n");
1183           else
1184             printf("Secondary host bus (?)\n");
1185         }
1186       for(b=bi->bridges; b; b=b->next)
1187         {
1188           printf("\t%02x.%d Bridge to %02x-%02x", b->dev, b->func, b->first, b->last);
1189           switch (b->bug)
1190             {
1191             case 1:
1192               printf(" <overlap bug>");
1193               break;
1194             case 2:
1195               printf(" <crossing bug>");
1196               break;
1197             }
1198           putchar('\n');
1199         }
1200     }
1201 }
1202
1203 static void
1204 map_the_bus(void)
1205 {
1206   if (pacc->method == PCI_ACCESS_PROC_BUS_PCI ||
1207       pacc->method == PCI_ACCESS_DUMP)
1208     printf("WARNING: Bus mapping can be reliable only with direct hardware access enabled.\n\n");
1209   else if (!check_root())
1210     die("Only root can map the bus.");
1211   bus_info = xmalloc(sizeof(struct bus_info) * 256);
1212   bzero(bus_info, sizeof(struct bus_info) * 256);
1213   if (filter.bus >= 0)
1214     do_map_bus(filter.bus);
1215   else
1216     {
1217       int bus;
1218       for(bus=0; bus<256; bus++)
1219         do_map_bus(bus);
1220     }
1221   map_bridges();
1222 }
1223
1224 /* Main */
1225
1226 int
1227 main(int argc, char **argv)
1228 {
1229   int i;
1230   char *msg;
1231
1232   if (argc == 2 && !strcmp(argv[1], "--version"))
1233     {
1234       puts("lspci version " PCIUTILS_VERSION);
1235       return 0;
1236     }
1237
1238   pacc = pci_alloc();
1239   pacc->error = die;
1240   pci_filter_init(pacc, &filter);
1241
1242   while ((i = getopt(argc, argv, options)) != -1)
1243     switch (i)
1244       {
1245       case 'n':
1246         pacc->numeric_ids = 1;
1247         break;
1248       case 'v':
1249         verbose++;
1250         break;
1251       case 'b':
1252         pacc->buscentric = 1;
1253         buscentric_view = 1;
1254         break;
1255       case 's':
1256         if (msg = pci_filter_parse_slot(&filter, optarg))
1257           die("-f: %s", msg);
1258         break;
1259       case 'd':
1260         if (msg = pci_filter_parse_id(&filter, optarg))
1261           die("-d: %s", msg);
1262         break;
1263       case 'x':
1264         show_hex++;
1265         break;
1266       case 't':
1267         show_tree++;
1268         break;
1269       case 'i':
1270         pacc->id_file_name = optarg;
1271         break;
1272       case 'm':
1273         machine_readable++;
1274         break;
1275       case 'M':
1276         map_mode++;
1277         break;
1278       default:
1279         if (parse_generic_option(i, pacc, optarg))
1280           break;
1281       bad:
1282         fprintf(stderr, help_msg, pacc->id_file_name);
1283         return 1;
1284       }
1285   if (optind < argc)
1286     goto bad;
1287
1288   pci_init(pacc);
1289   if (map_mode)
1290     map_the_bus();
1291   else
1292     {
1293       scan_devices();
1294       sort_them();
1295       if (show_tree)
1296         show_forest();
1297       else
1298         show();
1299     }
1300   pci_cleanup(pacc);
1301
1302   return 0;
1303 }