]> mj.ucw.cz Git - pciutils.git/blobdiff - ls-tree.c
lspci: Use mangled vendor/device ID when examining vendor caps
[pciutils.git] / ls-tree.c
index af1e659105c8613696a1ff2e290806a908a6721d..5189930e70f2cb1dd6f7628ca7cf656cf3fd9c64 100644 (file)
--- a/ls-tree.c
+++ b/ls-tree.c
@@ -3,7 +3,9 @@
  *
  *     Copyright (c) 1997--2021 Martin Mares <mj@ucw.cz>
  *
- *     Can be freely distributed and used under the terms of the GNU GPL.
+ *     Can be freely distributed and used under the terms of the GNU GPL v2+.
+ *
+ *     SPDX-License-Identifier: GPL-2.0-or-later
  */
 
 #include <stdarg.h>
@@ -12,7 +14,7 @@
 
 #include "lspci.h"
 
-struct bridge host_bridge = { NULL, NULL, NULL, NULL, NULL, 0, ~0, 0, ~0, NULL };
+struct bridge host_bridge = { NULL, NULL, NULL, NULL, NULL, NULL, ~0, ~0, ~0, ~0, NULL };
 
 static struct bus *
 find_bus(struct bridge *b, unsigned int domain, unsigned int n)
@@ -73,10 +75,19 @@ insert_dev(struct device *d, struct bridge *b)
         bus = new_bus(parent->bridge, p->domain, p->bus);
     }
 
+  if (!bus && b == &host_bridge)
+    {
+      for (b=b->child; b; b=b->prev)
+        if (b->domain == (unsigned)p->domain)
+          break;
+      if (!b)
+        b = &host_bridge;
+    }
+
   if (!bus && ! (bus = find_bus(b, p->domain, p->bus)))
     {
       struct bridge *c;
-      for (c=b->child; c; c=c->next)
+      for (c=b->child; c; c=c->prev)
        if (c->domain == (unsigned)p->domain && c->secondary <= p->bus && p->bus <= c->subordinate)
           {
             insert_dev(d, c);
@@ -100,9 +111,34 @@ grow_tree(void)
   struct device *d;
   struct bridge **last_br, *b;
 
+  last_br = &host_bridge.chain;
+
+  /* Build list of top level domain bridges */
+
+  for (d=first_dev; d; d=d->next)
+    {
+      for (b=host_bridge.chain; b; b=b->chain)
+        if (b->domain == (unsigned)d->dev->domain)
+          break;
+      if (b)
+        continue;
+      b = xmalloc(sizeof(struct bridge));
+      b->domain = d->dev->domain;
+      b->primary = ~0;
+      b->secondary = 0;
+      b->subordinate = ~0;
+      *last_br = b;
+      last_br = &b->chain;
+      b->prev = b->next = b->child = NULL;
+      b->first_bus = NULL;
+      b->last_bus = NULL;
+      b->br_dev = NULL;
+      b->chain = NULL;
+      pacc->debug("Tree: domain %04x\n", b->domain);
+    }
+
   /* Build list of bridges */
 
-  last_br = &host_bridge.chain;
   for (d=first_dev; d; d=d->next)
     {
       struct pci_dev *dd = d->dev;
@@ -113,21 +149,20 @@ grow_tree(void)
        {
          b = xmalloc(sizeof(struct bridge));
          b->domain = dd->domain;
+         b->primary = dd->bus;
          if (ht == PCI_HEADER_TYPE_BRIDGE)
            {
-             b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
              b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
              b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
            }
          else
            {
-             b->primary = get_conf_byte(d, PCI_CB_PRIMARY_BUS);
              b->secondary = get_conf_byte(d, PCI_CB_CARD_BUS);
              b->subordinate = get_conf_byte(d, PCI_CB_SUBORDINATE_BUS);
            }
          *last_br = b;
          last_br = &b->chain;
-         b->next = b->child = NULL;
+         b->prev = b->next = b->child = NULL;
          b->first_bus = NULL;
          b->last_bus = NULL;
          b->br_dev = d;
@@ -155,7 +190,7 @@ grow_tree(void)
       b->subordinate = b->secondary;
       *last_br = b;
       last_br = &b->chain;
-      b->next = b->child = NULL;
+      b->prev = b->next = b->child = NULL;
       b->first_bus = NULL;
       b->last_bus = NULL;
       b->br_dev = parent;
@@ -167,7 +202,7 @@ grow_tree(void)
 
   /* Create a bridge tree */
 
-  for (b=&host_bridge; b; b=b->chain)
+  for (b=host_bridge.chain; b; b=b->chain)
     {
       struct device *br_dev = b->br_dev;
       struct bridge *c, *best = NULL;
@@ -181,19 +216,19 @@ grow_tree(void)
       for (c=&host_bridge; c; c=c->chain)
        if (c != b && (c == &host_bridge || b->domain == c->domain) &&
            b->primary >= c->secondary && b->primary <= c->subordinate &&
-           (!best || best->subordinate - best->primary > c->subordinate - c->primary))
+           (!best || best == &host_bridge || best->subordinate - best->primary > c->subordinate - c->primary))
          best = c;
       if (best)
        {
-         b->next = best->child;
+         b->prev = best->child;
          best->child = b;
        }
     }
 
   /* Insert secondary bus for each bridge */
 
-  for (b=&host_bridge; b; b=b->chain)
-    if (!find_bus(b, b->domain, b->secondary))
+  for (b=host_bridge.chain; b; b=b->chain)
+    if (b->br_dev && !find_bus(b, b->domain, b->secondary))
       new_bus(b, b->domain, b->secondary);
 
   /* Create bus structs and link devices */
@@ -259,7 +294,7 @@ show_tree_dev(struct pci_filter *filter, struct device *d, char *line, char *p)
   char namebuf[256];
 
   p = tree_printf(line, p, "%02x.%x", q->dev, q->func);
-  for (b=&host_bridge; b; b=b->chain)
+  for (b=host_bridge.chain; b; b=b->chain)
     if (b->br_dev == d)
       {
        if (b->secondary == 0)
@@ -279,6 +314,18 @@ show_tree_dev(struct pci_filter *filter, struct device *d, char *line, char *p)
   print_it(line, p);
 }
 
+static struct pci_filter *
+get_filter_for_child(struct pci_filter *filter, struct device *d)
+{
+  if (!filter)
+    return NULL;
+
+  if (pci_filter_match(filter, d->dev))
+    return NULL;
+
+  return filter;
+}
+
 static int
 check_bus_filter(struct pci_filter *filter, struct bus *b);
 
@@ -294,7 +341,7 @@ check_dev_filter(struct pci_filter *filter, struct device *d)
   if (pci_filter_match(filter, d->dev))
     return 1;
 
-  for (br = &host_bridge; br; br = br->chain)
+  for (br = host_bridge.chain; br; br = br->chain)
     if (br->br_dev == d)
       {
         for (b = br->first_bus; b; b = b->sibling)
@@ -331,32 +378,34 @@ show_tree_bus(struct pci_filter *filter, struct bus *b, char *line, char *p)
       if (check_dev_filter(filter, b->first_dev))
         {
           p = tree_printf(line, p, "--");
-          show_tree_dev(filter, b->first_dev, line, p);
+          show_tree_dev(get_filter_for_child(filter, b->first_dev), b->first_dev, line, p);
         }
       else
         print_it(line, p);
     }
   else
     {
-      int empty = 1;
+      int i, count = 0;
       struct device *d = b->first_dev;
-      while (d->bus_next)
-       {
-         if (check_dev_filter(filter, d))
-           {
-             char *p2 = tree_printf(line, p, "+-");
-             show_tree_dev(filter, d, line, p2);
-             empty = 0;
-           }
-         d = d->bus_next;
-       }
-      if (check_dev_filter(filter, d))
+
+      do
         {
-          p = tree_printf(line, p, "\\-");
-          show_tree_dev(filter, d, line, p);
-          empty = 0;
+          if (check_dev_filter(filter, d))
+            count++;
+          d = d->bus_next;
         }
-      if (empty)
+      while (d);
+
+      for (i = 0, d = b->first_dev; d; d = d->bus_next)
+        {
+          if (!check_dev_filter(filter, d))
+            continue;
+          char *p2 = tree_printf(line, p, count == 1 ? "--" : count == i+1 ? "\\-" : "+-");
+          show_tree_dev(get_filter_for_child(filter, d), d, line, p2);
+          i++;
+        }
+
+      if (count == 0)
         print_it(line, p);
     }
 }
@@ -369,8 +418,8 @@ show_tree_bridge(struct pci_filter *filter, struct bridge *b, char *line, char *
     {
       if (check_bus_filter(filter, b->first_bus))
         {
-          if (b == &host_bridge)
-            p = tree_printf(line, p, "[%04x:%02x]-", b->domain, b->first_bus->number);
+          if (!b->br_dev)
+            p = tree_printf(line, p, "[%04x:%02x]-", b->first_bus->domain, b->first_bus->number);
           show_tree_bus(filter, b->first_bus, line, p);
         }
       else
@@ -378,27 +427,28 @@ show_tree_bridge(struct pci_filter *filter, struct bridge *b, char *line, char *
     }
   else
     {
-      int empty = 1;
+      int i, count = 0;
       struct bus *u = b->first_bus;
       char *k;
 
-      while (u->sibling)
+      do
         {
           if (check_bus_filter(filter, u))
-            {
-              k = tree_printf(line, p, "+-[%04x:%02x]-", u->domain, u->number);
-              show_tree_bus(filter, u, line, k);
-              empty = 0;
-            }
+            count++;
           u = u->sibling;
         }
-      if (check_bus_filter(filter, u))
+      while (u);
+
+      for (i = 0, u = b->first_bus; u; u = u->sibling)
         {
-          k = tree_printf(line, p, "\\-[%04x:%02x]-", u->domain, u->number);
+          if (!check_bus_filter(filter, u))
+            continue;
+          k = tree_printf(line, p, count == 1 ? "[%04x:%02x]-" : count == i+1 ? "\\-[%04x:%02x]-" : "+-[%04x:%02x]-", u->domain, u->number);
           show_tree_bus(filter, u, line, k);
-          empty = 0;
+          i++;
         }
-      if (empty)
+
+      if (count == 0)
         print_it(line, p);
     }
 }
@@ -407,5 +457,12 @@ void
 show_forest(struct pci_filter *filter)
 {
   char line[LINE_BUF_SIZE];
-  show_tree_bridge(filter, &host_bridge, line, line);
+  struct bridge *b;
+  if (host_bridge.child)
+    {
+      for (b=host_bridge.child; b->prev; b=b->prev)
+        b->prev->next = b;
+      for (; b; b=b->next)
+        show_tree_bridge(filter, b, line, line);
+    }
 }