]> git.ipfire.org Git - thirdparty/pciutils.git/commitdiff
lspci: Add ability to filter by class code
authorMatthew Wilcox <willy@linux.intel.com>
Tue, 30 Sep 2014 18:02:52 +0000 (14:02 -0400)
committerMartin Mares <mj@ucw.cz>
Sun, 2 Nov 2014 10:36:19 +0000 (11:36 +0100)
Extend the 'filter by device ID' functionality to allow optional
specification of a device ID.  For example, to list all USB controllers
in the system made by Intel, specify:

lspci -d 8086::0c03

Signed-off-by: Matthew Wilcox <matthew.r.wilcox@intel.com>
lib/filter.c
lib/pci.h
lspci.c
lspci.man

index f321b0f57b7bf007ac12626d4ce4611e8434a8fd..fab0025bd7322ad7f836a5ec459f911f736614d3 100644 (file)
@@ -15,7 +15,7 @@ void
 pci_filter_init(struct pci_access *a UNUSED, struct pci_filter *f)
 {
   f->domain = f->bus = f->slot = f->func = -1;
-  f->vendor = f->device = -1;
+  f->vendor = f->device = f->class = -1;
 }
 
 /* Slot filter syntax: [[[domain]:][bus]:][slot][.[func]] */
@@ -74,12 +74,12 @@ pci_filter_parse_slot(struct pci_filter *f, char *str)
   return NULL;
 }
 
-/* ID filter syntax: [vendor]:[device] */
+/* ID filter syntax: [vendor]:[device][:class] */
 
 char *
 pci_filter_parse_id(struct pci_filter *f, char *str)
 {
-  char *s, *e;
+  char *s, *c, *e;
 
   if (!*str)
     return NULL;
@@ -94,6 +94,9 @@ pci_filter_parse_id(struct pci_filter *f, char *str)
        return "Invalid vendor ID";
       f->vendor = x;
     }
+  c = strchr(s, ':');
+  if (c)
+    *c++ = 0;
   if (s[0] && strcmp(s, "*"))
     {
       long int x = strtol(s, &e, 16);
@@ -101,6 +104,13 @@ pci_filter_parse_id(struct pci_filter *f, char *str)
        return "Invalid device ID";
       f->device = x;
     }
+  if (c && c[0] && strcmp(s, "*"))
+    {
+      long int x = strtol(c, &e, 16);
+      if ((e && *e) || (x < 0 || x > 0xffff))
+       return "Invalid class code";
+      f->class = x;
+    }
   return NULL;
 }
 
@@ -119,5 +129,11 @@ pci_filter_match(struct pci_filter *f, struct pci_dev *d)
          (f->vendor >= 0 && f->vendor != d->vendor_id))
        return 0;
     }
+  if (f->class >= 0)
+    {
+      pci_fill_info(d, PCI_FILL_CLASS);
+      if (f->class != d->device_class)
+       return 0;
+    }
   return 1;
 }
index 8b1d02454657339b318e17099274bdc469c71d9e..0c57fddb589f7c110daafd36842a92c7d616e461 100644 (file)
--- a/lib/pci.h
+++ b/lib/pci.h
@@ -198,7 +198,7 @@ struct pci_cap *pci_find_cap(struct pci_dev *, unsigned int id, unsigned int typ
 
 struct pci_filter {
   int domain, bus, slot, func;                 /* -1 = ANY */
-  int vendor, device;
+  int vendor, device, class;
 };
 
 void pci_filter_init(struct pci_access *, struct pci_filter *) PCI_ABI;
diff --git a/lspci.c b/lspci.c
index da0aac7e2aa4c4517e60928854f7a1300d69fb13..6f161421d64766dbda934491645d22d23c1ba592 100644 (file)
--- a/lspci.c
+++ b/lspci.c
@@ -60,7 +60,7 @@ static char help_msg[] =
 "\n"
 "Selection of devices:\n"
 "-s [[[[<domain>]:]<bus>]:][<slot>][.[<func>]]\tShow only devices in selected slots\n"
-"-d [<vendor>]:[<device>]\t\t\tShow only devices with specified ID's\n"
+"-d [<vendor>]:[<device>][:<class>]\t\tShow only devices with specified ID's\n"
 "\n"
 "Other options:\n"
 "-i <file>\tUse specified ID database instead of %s\n"
index 0703222b81ac4afea9b7d382f9a9a95dc89dae18..81abc60976451e509f2f5fb9d7c32d84f0f0ce99 100644 (file)
--- a/lspci.man
+++ b/lspci.man
@@ -135,9 +135,10 @@ hexadecimal.  E.g., "0:" means all devices on bus 0, "0" means all functions of
 on any bus, "0.3" selects third function of device 0 on all buses and ".4" shows only
 the fourth function of each device.
 .TP
-.B -d [<vendor>]:[<device>]
-Show only devices with specified vendor and device ID. Both ID's are given in
-hexadecimal and may be omitted or given as "*", both meaning "any value".
+.B -d [<vendor>]:[<device>][:<class>]
+Show only devices with specified vendor, device and class ID. The ID's are
+given in hexadecimal and may be omitted or given as "*", both meaning
+"any value".
 
 .SS Other options
 .TP