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