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