]> git.ipfire.org Git - thirdparty/pciutils.git/blobdiff - lib/filter.c
lspci: Add ability to filter by class code
[thirdparty/pciutils.git] / lib / filter.c
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;
 }