summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2009-02-26 03:17:39 +0100
committerLennart Poettering <lennart@poettering.net>2009-02-26 03:51:29 +0100
commit90a007f6a34f3cff729ed5fbc8020085bd4f7a8b (patch)
tree3e397ddba8025eb68e551fd943ca7985d00cc1a0
parent4cbf05876a0dd309e7e617bc2a14a463d083fefa (diff)
Add pci-db tool that works just like usb-db but for USB
-rw-r--r--usb-db/.gitignore1
-rw-r--r--usb-db/Makefile.am10
-rw-r--r--usb-db/usb-db.c75
3 files changed, 60 insertions, 26 deletions
diff --git a/usb-db/.gitignore b/usb-db/.gitignore
index 156b3fc..861a838 100644
--- a/usb-db/.gitignore
+++ b/usb-db/.gitignore
@@ -1 +1,2 @@
usb-db
+pci-db
diff --git a/usb-db/Makefile.am b/usb-db/Makefile.am
index 46c5abb..82d8fe6 100644
--- a/usb-db/Makefile.am
+++ b/usb-db/Makefile.am
@@ -1,11 +1,17 @@
include $(top_srcdir)/Makefile.am.inc
udevhomedir = $(udev_prefix)/lib/udev
-udevhome_PROGRAMS = usb-db
+udevhome_PROGRAMS = usb-db pci-db
+
+COMMON_CPPFLAGS = $(AM_CPPFLAGS) $(LIBUDEV_CFLAGS)
usb_db_SOURCES = usb-db.c
-usb_db_CPPFLAGS = $(AM_CPPFLAGS) $(LIBUDEV_CFLAGS) -DUSB_DATABASE=\"/usr/share/hwdata/usb.ids\"
+usb_db_CPPFLAGS = $(COMMON_CPPFLAGS) -DUSB_DATABASE=\"/usr/share/hwdata/usb.ids\" -DBUILD_FOR_USB
usb_db_LDADD = $(LIBUDEV_LIBS)
+pci_db_SOURCES = $(usb_db_SOURCES)
+pci_db_CPPFLAGS = $(COMMON_CPPFLAGS) -DPCI_DATABASE=\"/usr/share/hwdata/pci.ids\" -DBUILD_FOR_PCI
+pci_db_LDADD = $(usb_db_LDADD)
+
#udevrulesdir = $(udev_prefix)/lib/udev/rules.d
#dist_udevrules_DATA = 77-probe-modem-capabilities.rules
diff --git a/usb-db/usb-db.c b/usb-db/usb-db.c
index 58713ee..b53e4e5 100644
--- a/usb-db/usb-db.c
+++ b/usb-db/usb-db.c
@@ -23,35 +23,62 @@
#include <libudev.h>
-static int get_vid_pid(
- struct udev_device *usb,
- uint16_t *vid,
- uint16_t *pid) {
+#if defined(BUILD_FOR_USB)
+# define DATABASE USB_DATABASE
+# define SUBSYSTEM "usb"
+# define DEVTYPE "usb_device"
+# define VENDOR_ATTR "idVendor"
+# define PRODUCT_ATTR "idProduct"
+#elif defined(BUILD_FOR_PCI)
+# define DATABASE PCI_DATABASE
+# define SUBSYSTEM "pci"
+# define DEVTYPE NULL
+# define VENDOR_ATTR "vendor"
+# define PRODUCT_ATTR "device"
+#else
+# error "Are you havin' a laugh?"
+#endif
+
+static int get_id_attr(
+ struct udev_device *parent,
+ const char *name,
+ uint16_t *value) {
const char *t;
+ unsigned u;
- if ((t = udev_device_get_sysattr_value(usb, "idVendor"))) {
- unsigned u;
+ if (!(t = udev_device_get_sysattr_value(parent, name))) {
+ fprintf(stderr, "%s lacks %s.\n", udev_device_get_syspath(parent), name);
+ return -1;
+ }
- if (sscanf(t, "%04x", &u) != 1 || u <= 0 || u > 0xFFFFU) {
- fprintf(stderr, "Failed to parse idVendor on %s.\n", udev_device_get_syspath(usb));
- return -1;
- }
+ if (!strncmp(t, "0x", 2))
+ t += 2;
- *vid = (uint16_t) u;
+ if (sscanf(t, "%04x", &u) != 1 || u > 0xFFFFU) {
+ fprintf(stderr, "Failed to parse %s on %s.\n", name, udev_device_get_syspath(parent));
+ return -1;
}
- if ((t = udev_device_get_sysattr_value(usb, "idProduct"))) {
- unsigned u;
+ *value = (uint16_t) u;
+ return 0;
+}
- if (sscanf(t, "%04x", &u) != 1 || u > 0xFFFFU) {
- fprintf(stderr, "Failed to parse idProduct on %s.\n", udev_device_get_syspath(usb));
- return -1;
- }
+static int get_vid_pid(
+ struct udev_device *parent,
+ uint16_t *vid,
+ uint16_t *pid) {
- *pid = (uint16_t) u;
+ if (get_id_attr(parent, VENDOR_ATTR, vid) < 0)
+ return -1;
+ else if (*vid <= 0) {
+ fprintf(stderr, "Invalid vendor id.\n");
+ return -1;
}
+ if (get_id_attr(parent, PRODUCT_ATTR, pid) < 0)
+ return -1;
+
return 0;
}
@@ -78,8 +105,8 @@ static int lookup_vid_pid(
*vendor = *product = NULL;
- if (!(f = fopen(USB_DATABASE, "r"))) {
- fprintf(stderr, "Failed to open database file "USB_DATABASE": %s", strerror(errno));
+ if (!(f = fopen(DATABASE, "r"))) {
+ fprintf(stderr, "Failed to open database file "DATABASE": %s\n", strerror(errno));
return -1;
}
@@ -163,7 +190,7 @@ int main(int argc, char*argv[]) {
struct udev *udev = NULL;
int ret = 1;
char *sp;
- struct udev_device *dev = NULL, *usb = NULL;
+ struct udev_device *dev = NULL, *parent = NULL;
uint16_t vid = 0, pid = 0;
char *vendor = NULL, *product = NULL;
@@ -188,12 +215,12 @@ int main(int argc, char*argv[]) {
goto finish;
}
- if (!(usb = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device"))) {
- fprintf(stderr, "Failed to find usb_device.\n");
+ if (!(parent = udev_device_get_parent_with_subsystem_devtype(dev, SUBSYSTEM, DEVTYPE))) {
+ fprintf(stderr, "Failed to find parent device.\n");
goto finish;
}
- if (get_vid_pid(usb, &vid, &pid) < 0)
+ if (get_vid_pid(parent, &vid, &pid) < 0)
goto finish;
if (lookup_vid_pid(vid, pid, &vendor, &product) < 0)