linux-pci.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Matthew Wilcox <matthew.r.wilcox@intel.com>
To: Martin Mares <mj@ucw.cz>, linux-pci@vger.kernel.org
Cc: Matthew Wilcox <willy@linux.intel.com>,
	Matthew Wilcox <matthew.r.wilcox@intel.com>
Subject: [PATCH] lspci: Add ability to filter by class code
Date: Tue, 30 Sep 2014 13:50:36 -0400	[thread overview]
Message-ID: <1412099436-18899-1-git-send-email-matthew.r.wilcox@intel.com> (raw)

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 | 22 +++++++++++++++++++---
 lib/pci.h    |  2 +-
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/lib/filter.c b/lib/filter.c
index f321b0f..fab0025 100644
--- a/lib/filter.c
+++ b/lib/filter.c
@@ -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;
 }
diff --git a/lib/pci.h b/lib/pci.h
index 38e2e99..b0bf1aa 100644
--- a/lib/pci.h
+++ b/lib/pci.h
@@ -193,7 +193,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;
-- 
2.1.0


             reply	other threads:[~2014-09-30 17:51 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-30 17:50 Matthew Wilcox [this message]
2014-09-30 18:02 ` [PATCH] lspci: Add ability to filter by class code Matthew Wilcox
2014-11-01 16:25   ` Martin Mares
2014-11-02 11:14     ` Martin Mares

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1412099436-18899-1-git-send-email-matthew.r.wilcox@intel.com \
    --to=matthew.r.wilcox@intel.com \
    --cc=linux-pci@vger.kernel.org \
    --cc=mj@ucw.cz \
    --cc=willy@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).