2 * $Id: lspci.c,v 1.20 1999/01/24 21:38:47 mj Exp $
4 * Linux PCI Utilities -- List All PCI Devices
6 * Copyright (c) 1997--1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
8 * Can be freely distributed and used under the terms of the GNU GPL.
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 */
28 static char options[] = "nvbxs:d:ti:mg" GENERIC_OPTIONS ;
30 static char help_msg[] = "\
31 Usage: lspci [<switches>]\n\
34 -n\t\tShow numeric ID's\n\
35 -b\t\tBus-centric view (PCI addresses and IRQ's instead of those seen by the CPU)\n\
36 -x\t\tShow hex-dump of config space\n\
37 -s [[<bus>]:][<slot>][.[<func>]]\tShow only devices in selected slots\n\
38 -d [<vendor>]:[<device>]\tShow only selected devices\n\
39 -t\t\tShow bus tree\n\
40 -m\t\tProduce machine-readable output\n\
41 -i <file>\tUse specified ID database instead of %s\n"
45 /* Communication with libpci */
47 static struct pci_access *pacc;
49 /* Format strings used for IRQ numbers and memory addresses */
52 #define IRQ_FORMAT "%08x"
54 #define IRQ_FORMAT "%d"
57 #ifdef HAVE_64BIT_LONG_INT
58 #define LONG_FORMAT "%016lx"
60 #define LONG_FORMAT "%08lx"
63 /* Our view of the PCI bus */
68 unsigned int config_cnt;
72 static struct device *first_dev;
78 int how_much = (show_hex > 2) ? 256 : 64;
82 for(p=pacc->devices; p; p=p->next)
84 if (!pci_filter_match(&filter, p))
86 d = xmalloc(sizeof(struct device));
90 if (!pci_read_block(p, 0, d->config, how_much))
91 die("Unable to read %d bytes of configuration space.", how_much);
92 if (how_much < 128 && (d->config[PCI_HEADER_TYPE] & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
94 /* For cardbus bridges, we need to fetch 64 bytes more to get the full standard header... */
95 if (!pci_read_block(p, 0, d->config+64, 64))
96 die("Unable to read cardbus bridge extension data.");
99 d->config_cnt = how_much;
100 pci_setup_cache(p, d->config, d->config_cnt);
101 pci_fill_info(p, PCI_FILL_IDENT | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE);
108 static int is_root = -1;
111 is_root = !geteuid();
116 config_fetch(struct device *d, unsigned int pos, unsigned int len)
118 if (pos + len < d->config_cnt)
120 if (pacc->method != PCI_ACCESS_DUMP && !check_root())
122 return pci_read_block(d->dev, pos, d->config + pos, len);
125 /* Config space accesses */
128 get_conf_byte(struct device *d, unsigned int pos)
130 return d->config[pos];
134 get_conf_word(struct device *d, unsigned int pos)
136 return d->config[pos] | (d->config[pos+1] << 8);
140 get_conf_long(struct device *d, unsigned int pos)
142 return d->config[pos] |
143 (d->config[pos+1] << 8) |
144 (d->config[pos+2] << 16) |
145 (d->config[pos+3] << 24);
151 compare_them(const void *A, const void *B)
153 const struct pci_dev *a = (*(const struct device **)A)->dev;
154 const struct pci_dev *b = (*(const struct device **)B)->dev;
164 if (a->func < b->func)
166 if (a->func > b->func)
174 struct device **index, **h, **last_dev;
179 for(d=first_dev; d; d=d->next)
181 h = index = alloca(sizeof(struct device *) * cnt);
182 for(d=first_dev; d; d=d->next)
184 qsort(index, cnt, sizeof(struct device *), compare_them);
185 last_dev = &first_dev;
190 last_dev = &(*h)->next;
199 show_terse(struct device *d)
202 struct pci_dev *p = d->dev;
203 byte classbuf[128], devbuf[128];
205 printf("%02x:%02x.%x %s: %s",
209 pci_lookup_name(pacc, classbuf, sizeof(classbuf),
211 get_conf_word(d, PCI_CLASS_DEVICE), 0),
212 pci_lookup_name(pacc, devbuf, sizeof(devbuf),
213 PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
214 p->vendor_id, p->device_id));
215 if (c = get_conf_byte(d, PCI_REVISION_ID))
216 printf(" (rev %02x)", c);
217 if (verbose && (c = get_conf_byte(d, PCI_CLASS_PROG)))
218 printf(" (prog-if %02x)", c);
223 show_bases(struct device *d, int cnt)
225 struct pci_dev *p = d->dev;
226 word cmd = get_conf_word(d, PCI_COMMAND);
232 unsigned int flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
233 pos = p->base_addr[i];
234 if (flg == 0xffffffff)
239 printf("\tRegion %d: ", i);
242 if (pos && !flg) /* Reported by the OS, but not by the device */
244 printf("[virtual] ");
247 if (flg & PCI_BASE_ADDRESS_SPACE_IO)
249 unsigned long a = pos & PCI_BASE_ADDRESS_IO_MASK;
250 printf("I/O ports at ");
253 else if (flg & PCI_BASE_ADDRESS_IO_MASK)
256 printf("<unassigned>");
257 if (!(cmd & PCI_COMMAND_IO))
258 printf(" [disabled]");
262 int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
263 unsigned long a = pos & PCI_BASE_ADDRESS_MEM_MASK;
267 printf("Memory at ");
268 if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
272 printf("<invalid-64bit-slot>\n");
278 z = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
282 printf("%08x%08lx", z, a);
284 printf("<unassigned>");
292 printf(LONG_FORMAT, a);
294 printf(((flg & PCI_BASE_ADDRESS_MEM_MASK) || z) ? "<ignored>" : "<unassigned>");
296 printf(" (%s, %sprefetchable)",
297 (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
298 (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
299 (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M" : "type 3",
300 (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
301 if (!(cmd & PCI_COMMAND_MEMORY))
302 printf(" [disabled]");
309 show_htype0(struct device *d)
311 unsigned long rom = d->dev->rom_base_addr;
315 printf("\tExpansion ROM at %08lx%s\n", rom & PCI_ROM_ADDRESS_MASK,
316 (rom & PCI_ROM_ADDRESS_ENABLE) ? "" : " [disabled]");
317 if (get_conf_word(d, PCI_STATUS) & PCI_STATUS_CAP_LIST)
319 int where = get_conf_byte(d, PCI_CAPABILITY_LIST);
323 printf("\tCapabilities: ");
324 if (!config_fetch(d, where, 4))
326 puts("<available only to root>");
329 id = get_conf_byte(d, where);
330 next = get_conf_byte(d, where+1);
331 ver = get_conf_byte(d, where+2);
334 printf("<chain broken at %02x>\n", where);
345 printf(" version %x.%x at %02x\n", ver/16, ver%16, where);
352 show_htype1(struct device *d)
354 struct pci_dev *p = d->dev;
355 u32 io_base = get_conf_byte(d, PCI_IO_BASE);
356 u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
357 u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
358 u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
359 u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
360 u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
361 u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
362 u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
363 u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
364 unsigned long rom = p->rom_base_addr;
365 word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
368 printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
369 get_conf_byte(d, PCI_PRIMARY_BUS),
370 get_conf_byte(d, PCI_SECONDARY_BUS),
371 get_conf_byte(d, PCI_SUBORDINATE_BUS),
372 get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
374 if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
375 (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
376 printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
379 io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
380 io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
381 if (io_type == PCI_IO_RANGE_TYPE_32)
383 io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
384 io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
387 printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
390 if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
392 printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
395 mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
396 mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
397 printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
400 if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
401 (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
402 printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
405 pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
406 pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
407 if (pref_type == PCI_PREF_RANGE_TYPE_32)
408 printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit + 0xfffff);
410 printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
411 get_conf_long(d, PCI_PREF_BASE_UPPER32),
413 get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
417 if (get_conf_word(d, PCI_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
418 printf("\tSecondary status: SERR\n");
421 printf("\tExpansion ROM at %08lx%s\n", rom & PCI_ROM_ADDRESS_MASK,
422 (rom & PCI_ROM_ADDRESS_ENABLE) ? "" : " [disabled]");
425 printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
426 (brc & PCI_BRIDGE_CTL_PARITY) ? '+' : '-',
427 (brc & PCI_BRIDGE_CTL_SERR) ? '+' : '-',
428 (brc & PCI_BRIDGE_CTL_NO_ISA) ? '+' : '-',
429 (brc & PCI_BRIDGE_CTL_VGA) ? '+' : '-',
430 (brc & PCI_BRIDGE_CTL_MASTER_ABORT) ? '+' : '-',
431 (brc & PCI_BRIDGE_CTL_BUS_RESET) ? '+' : '-',
432 (brc & PCI_BRIDGE_CTL_FAST_BACK) ? '+' : '-');
436 show_htype2(struct device *d)
439 word cmd = get_conf_word(d, PCI_COMMAND);
440 word brc = get_conf_word(d, PCI_CB_BRIDGE_CONTROL);
441 word exca = get_conf_word(d, PCI_CB_LEGACY_MODE_BASE);
444 printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
445 get_conf_byte(d, PCI_CB_PRIMARY_BUS),
446 get_conf_byte(d, PCI_CB_CARD_BUS),
447 get_conf_byte(d, PCI_CB_SUBORDINATE_BUS),
448 get_conf_byte(d, PCI_CB_LATENCY_TIMER));
452 u32 base = get_conf_long(d, PCI_CB_MEMORY_BASE_0 + p);
453 u32 limit = get_conf_long(d, PCI_CB_MEMORY_LIMIT_0 + p);
455 printf("Memory window %d: %08x-%08x%s%s\n", i, base, limit,
456 (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]",
457 (brc & (PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 << i)) ? " (prefetchable)" : "");
462 u32 base = get_conf_long(d, PCI_CB_IO_BASE_0 + p);
463 u32 limit = get_conf_long(d, PCI_CB_IO_LIMIT_0 + p);
464 if (!(base & PCI_IO_RANGE_TYPE_32))
469 base &= PCI_CB_IO_RANGE_MASK;
472 limit = (limit & PCI_CB_IO_RANGE_MASK) + 3;
473 printf("I/O window %d: %08x-%08x%s\n", i, base, limit,
474 (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
477 if (get_conf_word(d, PCI_CB_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
478 printf("\tSecondary status: SERR\n");
480 printf("\tBridgeCtl: Parity%c SERR%c ISA%c VGA%c MAbort%c >Reset%c 16bInt%c PostWrite%c\n",
481 (brc & PCI_CB_BRIDGE_CTL_PARITY) ? '+' : '-',
482 (brc & PCI_CB_BRIDGE_CTL_SERR) ? '+' : '-',
483 (brc & PCI_CB_BRIDGE_CTL_ISA) ? '+' : '-',
484 (brc & PCI_CB_BRIDGE_CTL_VGA) ? '+' : '-',
485 (brc & PCI_CB_BRIDGE_CTL_MASTER_ABORT) ? '+' : '-',
486 (brc & PCI_CB_BRIDGE_CTL_CB_RESET) ? '+' : '-',
487 (brc & PCI_CB_BRIDGE_CTL_16BIT_INT) ? '+' : '-',
488 (brc & PCI_CB_BRIDGE_CTL_POST_WRITES) ? '+' : '-');
490 printf("\t16-bit legacy interface ports at %04x\n", exca);
494 show_verbose(struct device *d)
496 struct pci_dev *p = d->dev;
497 word status = get_conf_word(d, PCI_STATUS);
498 word cmd = get_conf_word(d, PCI_COMMAND);
499 word class = get_conf_word(d, PCI_CLASS_DEVICE);
500 byte bist = get_conf_byte(d, PCI_BIST);
501 byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
502 byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
503 byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
504 byte max_lat, min_gnt;
505 byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
506 unsigned int irq = p->irq;
507 word subsys_v, subsys_d;
514 case PCI_HEADER_TYPE_NORMAL:
515 if (class == PCI_CLASS_BRIDGE_PCI)
518 printf("\t!!! Header type %02x doesn't match class code %04x\n", htype, class);
521 max_lat = get_conf_byte(d, PCI_MAX_LAT);
522 min_gnt = get_conf_byte(d, PCI_MIN_GNT);
523 subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
524 subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
526 case PCI_HEADER_TYPE_BRIDGE:
527 if (class != PCI_CLASS_BRIDGE_PCI)
529 irq = int_pin = min_gnt = max_lat = 0;
530 subsys_v = subsys_d = 0;
532 case PCI_HEADER_TYPE_CARDBUS:
533 if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
535 min_gnt = max_lat = 0;
536 subsys_v = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
537 subsys_d = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
540 printf("\t!!! Unknown header type %02x\n", htype);
544 if (verbose && subsys_v && subsys_v != 0xffff)
545 printf("\tSubsystem: %s\n",
546 pci_lookup_name(pacc, ssnamebuf, sizeof(ssnamebuf),
547 PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
548 subsys_v, subsys_d));
552 printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
553 (cmd & PCI_COMMAND_IO) ? '+' : '-',
554 (cmd & PCI_COMMAND_MEMORY) ? '+' : '-',
555 (cmd & PCI_COMMAND_MASTER) ? '+' : '-',
556 (cmd & PCI_COMMAND_SPECIAL) ? '+' : '-',
557 (cmd & PCI_COMMAND_INVALIDATE) ? '+' : '-',
558 (cmd & PCI_COMMAND_VGA_PALETTE) ? '+' : '-',
559 (cmd & PCI_COMMAND_PARITY) ? '+' : '-',
560 (cmd & PCI_COMMAND_WAIT) ? '+' : '-',
561 (cmd & PCI_COMMAND_SERR) ? '+' : '-',
562 (cmd & PCI_COMMAND_FAST_BACK) ? '+' : '-');
563 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",
564 (status & PCI_STATUS_CAP_LIST) ? '+' : '-',
565 (status & PCI_STATUS_66MHZ) ? '+' : '-',
566 (status & PCI_STATUS_UDF) ? '+' : '-',
567 (status & PCI_STATUS_FAST_BACK) ? '+' : '-',
568 (status & PCI_STATUS_PARITY) ? '+' : '-',
569 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
570 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
571 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
572 (status & PCI_STATUS_SIG_TARGET_ABORT) ? '+' : '-',
573 (status & PCI_STATUS_REC_TARGET_ABORT) ? '+' : '-',
574 (status & PCI_STATUS_REC_MASTER_ABORT) ? '+' : '-',
575 (status & PCI_STATUS_SIG_SYSTEM_ERROR) ? '+' : '-',
576 (status & PCI_STATUS_DETECTED_PARITY) ? '+' : '-');
577 if (cmd & PCI_COMMAND_MASTER)
579 printf("\tLatency: ");
581 printf("%d min, ", min_gnt);
583 printf("%d max, ", max_lat);
584 printf("%d set", latency);
586 printf(", cache line size %02x", cache_line);
590 printf("\tInterrupt: pin %c routed to IRQ " IRQ_FORMAT "\n",
591 (int_pin ? 'A' + int_pin - 1 : '?'), irq);
596 if (cmd & PCI_COMMAND_MASTER)
597 printf("bus master, ");
598 if (cmd & PCI_COMMAND_VGA_PALETTE)
599 printf("VGA palette snoop, ");
600 if (cmd & PCI_COMMAND_WAIT)
601 printf("stepping, ");
602 if (cmd & PCI_COMMAND_FAST_BACK)
603 printf("fast Back2Back, ");
604 if (status & PCI_STATUS_66MHZ)
606 if (status & PCI_STATUS_UDF)
607 printf("user-definable features, ");
609 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
610 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
611 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
612 if (cmd & PCI_COMMAND_MASTER)
613 printf(", latency %d", latency);
615 printf(", IRQ " IRQ_FORMAT, irq);
619 if (bist & PCI_BIST_CAPABLE)
621 if (bist & PCI_BIST_START)
622 printf("\tBIST is running\n");
624 printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
629 case PCI_HEADER_TYPE_NORMAL:
632 case PCI_HEADER_TYPE_BRIDGE:
635 case PCI_HEADER_TYPE_CARDBUS:
642 show_hex_dump(struct device *d)
646 for(i=0; i<d->config_cnt; i++)
650 printf(" %02x", get_conf_byte(d, i));
657 show_machine(struct device *d)
659 struct pci_dev *p = d->dev;
661 word sv_id=0, sd_id=0;
662 char classbuf[128], vendbuf[128], devbuf[128], svbuf[128], sdbuf[128];
664 switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
666 case PCI_HEADER_TYPE_NORMAL:
667 sv_id = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
668 sd_id = get_conf_word(d, PCI_SUBSYSTEM_ID);
670 case PCI_HEADER_TYPE_CARDBUS:
671 sv_id = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
672 sd_id = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
678 printf("Device:\t%02x:%02x.%x\n", p->bus, p->dev, p->func);
679 printf("Class:\t%s\n",
680 pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, get_conf_word(d, PCI_CLASS_DEVICE), 0));
681 printf("Vendor:\t%s\n",
682 pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id));
683 printf("Device:\t%s\n",
684 pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id));
685 if (sv_id && sv_id != 0xffff)
687 printf("SVendor:\t%s\n",
688 pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id, sd_id));
689 printf("SDevice:\t%s\n",
690 pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, sv_id, sd_id));
692 if (c = get_conf_byte(d, PCI_REVISION_ID))
693 printf("Rev:\t%02x\n", c);
694 if (c = get_conf_byte(d, PCI_CLASS_PROG))
695 printf("ProgIf:\t%02x\n", c);
699 printf("%02x:%02x.%x ", p->bus, p->dev, p->func);
700 printf("\"%s\" \"%s\" \"%s\"",
701 pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS,
702 get_conf_word(d, PCI_CLASS_DEVICE), 0),
703 pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR,
704 p->vendor_id, p->device_id),
705 pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE,
706 p->vendor_id, p->device_id));
707 if (c = get_conf_byte(d, PCI_REVISION_ID))
708 printf(" -r%02x", c);
709 if (c = get_conf_byte(d, PCI_CLASS_PROG))
710 printf(" -p%02x", c);
711 if (sv_id && sv_id != 0xffff)
712 printf(" \"%s\" \"%s\"",
713 pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id, sd_id),
714 pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, sv_id, sd_id));
716 printf(" \"\" \"\"");
726 for(d=first_dev; d; d=d->next)
728 if (machine_readable)
736 if (verbose || show_hex)
744 struct bridge *chain; /* Single-linked list of bridges */
745 struct bridge *next, *child; /* Tree of bridges */
746 struct bus *first_bus; /* List of busses connected to this bridge */
747 unsigned int primary, secondary, subordinate; /* Bus numbers */
748 struct device *br_dev;
754 struct device *first_dev, **last_dev;
757 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, ~0, 0, ~0, NULL };
760 find_bus(struct bridge *b, unsigned int n)
764 for(bus=b->first_bus; bus; bus=bus->sibling)
765 if (bus->number == n)
771 new_bus(struct bridge *b, unsigned int n)
773 struct bus *bus = xmalloc(sizeof(struct bus));
775 bus = xmalloc(sizeof(struct bus));
777 bus->sibling = b->first_bus;
778 bus->first_dev = NULL;
779 bus->last_dev = &bus->first_dev;
785 insert_dev(struct device *d, struct bridge *b)
787 struct pci_dev *p = d->dev;
790 if (! (bus = find_bus(b, p->bus)))
793 for(c=b->child; c; c=c->next)
794 if (c->secondary <= p->bus && p->bus <= c->subordinate)
795 return insert_dev(d, c);
796 bus = new_bus(b, p->bus);
798 /* Simple insertion at the end _does_ guarantee the correct order as the
799 * original device list was sorted by (bus, devfn) lexicographically
800 * and all devices on the new list have the same bus number.
803 bus->last_dev = &d->next;
810 struct device *d, *d2;
811 struct bridge **last_br, *b;
813 /* Build list of bridges */
815 last_br = &host_bridge.chain;
816 for(d=first_dev; d; d=d->next)
818 word class = get_conf_word(d, PCI_CLASS_DEVICE);
819 byte ht = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
820 if (class == PCI_CLASS_BRIDGE_PCI &&
821 (ht == PCI_HEADER_TYPE_BRIDGE || ht == PCI_HEADER_TYPE_CARDBUS))
823 b = xmalloc(sizeof(struct bridge));
824 if (ht == PCI_HEADER_TYPE_BRIDGE)
826 b->primary = get_conf_byte(d, PCI_CB_PRIMARY_BUS);
827 b->secondary = get_conf_byte(d, PCI_CB_CARD_BUS);
828 b->subordinate = get_conf_byte(d, PCI_CB_SUBORDINATE_BUS);
832 b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
833 b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
834 b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
838 b->next = b->child = NULL;
845 /* Create a bridge tree */
847 for(b=&host_bridge; b; b=b->chain)
849 struct bridge *c, *best;
851 for(c=&host_bridge; c; c=c->chain)
852 if (c != b && b->primary >= c->secondary && b->primary <= c->subordinate &&
853 (!best || best->subordinate - best->primary > c->subordinate - c->primary))
857 b->next = best->child;
862 /* Insert secondary bus for each bridge */
864 for(b=&host_bridge; b; b=b->chain)
865 if (!find_bus(b, b->secondary))
866 new_bus(b, b->secondary);
868 /* Create bus structs and link devices */
873 insert_dev(d, &host_bridge);
879 print_it(byte *line, byte *p)
885 if (*p == '+' || *p == '|')
891 static void show_tree_bridge(struct bridge *, byte *, byte *);
894 show_tree_dev(struct device *d, byte *line, byte *p)
896 struct pci_dev *q = d->dev;
900 p += sprintf(p, "%02x.%x", q->dev, q->func);
901 for(b=&host_bridge; b; b=b->chain)
904 if (b->secondary == b->subordinate)
905 p += sprintf(p, "-[%02x]-", b->secondary);
907 p += sprintf(p, "-[%02x-%02x]-", b->secondary, b->subordinate);
908 show_tree_bridge(b, line, p);
912 p += sprintf(p, " %s",
913 pci_lookup_name(pacc, namebuf, sizeof(namebuf),
914 PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
915 q->vendor_id, q->device_id));
920 show_tree_bus(struct bus *b, byte *line, byte *p)
924 else if (!b->first_dev->next)
928 show_tree_dev(b->first_dev, line, p);
932 struct device *d = b->first_dev;
937 show_tree_dev(d, line, p+2);
942 show_tree_dev(d, line, p+2);
947 show_tree_bridge(struct bridge *b, byte *line, byte *p)
950 if (!b->first_bus->sibling)
952 if (b == &host_bridge)
953 p += sprintf(p, "[%02x]-", b->first_bus->number);
954 show_tree_bus(b->first_bus, line, p);
958 struct bus *u = b->first_bus;
963 k = p + sprintf(p, "+-[%02x]-", u->number);
964 show_tree_bus(u, line, k);
967 k = p + sprintf(p, "\\-[%02x]-", u->number);
968 show_tree_bus(u, line, k);
978 show_tree_bridge(&host_bridge, line, line);
984 main(int argc, char **argv)
989 if (argc == 2 && !strcmp(argv[1], "--version"))
991 puts("lspci version " PCIUTILS_VERSION);
997 pci_filter_init(pacc, &filter);
999 while ((i = getopt(argc, argv, options)) != -1)
1003 pacc->numeric_ids = 1;
1009 pacc->buscentric = 1;
1010 buscentric_view = 1;
1013 if (msg = pci_filter_parse_slot(&filter, optarg))
1017 if (msg = pci_filter_parse_id(&filter, optarg))
1027 pacc->id_file_name = optarg;
1033 if (parse_generic_option(i, pacc, optarg))
1036 fprintf(stderr, help_msg, pacc->id_file_name);