chiark / gitweb /
builtin: move usb-db, pci-db to builtins
authorKay Sievers <kay.sievers@vrfy.org>
Tue, 27 Dec 2011 16:02:52 +0000 (17:02 +0100)
committerKay Sievers <kay.sievers@vrfy.org>
Tue, 27 Dec 2011 16:02:52 +0000 (17:02 +0100)
Makefile.am
configure.ac
extras/usb-db/.gitignore [deleted file]
rules/rules.d/75-net-description.rules
rules/rules.d/75-tty-description.rules
rules/rules.d/78-sound-card.rules
udev/udev-builtin-hwdb.c [moved from extras/usb-db/usb-db.c with 56% similarity]
udev/udev-builtin.c
udev/udev.h

index a7f29676a2a51d58de0b91fff1b7046aac760dcc..d95a3619a9b97ca81f232ce12edd5be769556f1c 100644 (file)
@@ -151,6 +151,9 @@ dist_udevrules_DATA = \
        rules/rules.d/60-persistent-input.rules \
        rules/rules.d/60-persistent-alsa.rules \
        rules/rules.d/60-persistent-storage.rules \
+       rules/rules.d/75-net-description.rules \
+       rules/rules.d/75-tty-description.rules \
+       rules/rules.d/78-sound-card.rules \
        rules/rules.d/80-drivers.rules \
        rules/rules.d/95-udev-late.rules
 
@@ -209,6 +212,7 @@ udev_common_sources = \
        udev/udev-builtin.c \
        udev/udev-builtin-blkid.c \
        udev/udev-builtin-firmware.c \
+       udev/udev-builtin-hwdb.c \
        udev/udev-builtin-input_id.c \
        udev/udev-builtin-kmod.c \
        udev/udev-builtin-path_id.c \
@@ -225,7 +229,8 @@ udev_common_LDADD = \
 
 udev_common_CPPFLAGS = \
        $(AM_CPPFLAGS) \
-       -DFIRMWARE_PATH="$(FIRMWARE_PATH)"
+       -DFIRMWARE_PATH="$(FIRMWARE_PATH)" \
+       -DUSB_DATABASE=\"$(USB_DATABASE)\" -DPCI_DATABASE=\"$(PCI_DATABASE)\"
 
 udev_udevd_SOURCES = \
        $(udev_common_sources) \
@@ -505,25 +510,6 @@ INSTALL_EXEC_HOOKS += libgudev-install-move-hook
 UNINSTALL_EXEC_HOOKS += libgudev-uninstall-move-hook
 endif
 
-if ENABLE_HWDB
-# ------------------------------------------------------------------------------
-# usb/pci-db - read vendor/device string database
-# ------------------------------------------------------------------------------
-extras_usb_db_usb_db_SOURCES = extras/usb-db/usb-db.c
-extras_usb_db_usb_db_CPPFLAGS = $(AM_CPPFLAGS) -DUSB_DATABASE=\"$(USB_DATABASE)\" -DBUILD_FOR_USB
-extras_usb_db_usb_db_LDADD = libudev/libudev-private.la
-libexec_PROGRAMS += extras/usb-db/usb-db
-dist_udevrules_DATA += \
-       rules/rules.d/75-net-description.rules \
-       rules/rules.d/75-tty-description.rules \
-       rules/rules.d/78-sound-card.rules
-
-extras_usb_db_pci_db_SOURCES = extras/usb-db/usb-db.c
-extras_usb_db_pci_db_CPPFLAGS = $(AM_CPPFLAGS) -DPCI_DATABASE=\"$(PCI_DATABASE)\" -DBUILD_FOR_PCI
-extras_usb_db_pci_db_LDADD = libudev/libudev-private.la
-libexec_PROGRAMS += extras/usb-db/pci-db
-endif
-
 if ENABLE_KEYMAP
 # ------------------------------------------------------------------------------
 # keymap - map custom hardware's multimedia keys
index a9ba74c1f6442dde1ae33c40a7a9be4929b72e7d..497a74fc0af188fbc6b56e09914f2024bfce20cc 100644 (file)
@@ -20,9 +20,42 @@ AC_PREFIX_DEFAULT([/usr])
 AC_PATH_PROG([XSLTPROC], [xsltproc])
 
 AC_SEARCH_LIBS([clock_gettime], [rt], [], [AC_MSG_ERROR([POSIX RT library not found])])
+
 PKG_CHECK_MODULES(BLKID, blkid >= 2.20)
+
 PKG_CHECK_MODULES(KMOD, libkmod >= 2)
 
+if test "x$cross_compiling" = "xno" ; then
+       AC_CHECK_FILES([/usr/share/pci.ids], [pciids=/usr/share/pci.ids])
+       AC_CHECK_FILES([/usr/share/hwdata/pci.ids], [pciids=/usr/share/hwdata/pci.ids])
+       AC_CHECK_FILES([/usr/share/misc/pci.ids], [pciids=/usr/share/misc/pci.ids])
+fi
+
+AC_ARG_WITH(usb-ids-path,
+       [AS_HELP_STRING([--with-usb-ids-path=DIR], [Path to usb.ids file])],
+       [USB_DATABASE=${withval}],
+       [if test -n "$usbids" ; then
+               USB_DATABASE="$usbids"
+       else
+               PKG_CHECK_MODULES(USBUTILS, usbutils >= 0.82)
+               AC_SUBST([USB_DATABASE], [$($PKG_CONFIG --variable=usbids usbutils)])
+       fi])
+AC_MSG_CHECKING([for USB database location])
+AC_MSG_RESULT([$USB_DATABASE])
+AC_SUBST(USB_DATABASE)
+
+AC_ARG_WITH(pci-ids-path,
+       [AS_HELP_STRING([--with-pci-ids-path=DIR], [Path to pci.ids file])],
+       [PCI_DATABASE=${withval}],
+       [if test -n "$pciids" ; then
+               PCI_DATABASE="$pciids"
+       else
+               AC_MSG_ERROR([pci.ids not found, try --with-pci-ids-path=])
+       fi])
+AC_MSG_CHECKING([for PCI database location])
+AC_MSG_RESULT([$PCI_DATABASE])
+AC_SUBST(PCI_DATABASE)
+
 AC_ARG_WITH([rootprefix],
        AS_HELP_STRING([--with-rootprefix=DIR], [rootfs directory prefix for config files and kernel modules]),
        [], [with_rootprefix=$prefix])
@@ -103,46 +136,6 @@ AS_IF([test "x$enable_introspection" = "xyes"], [
 AM_CONDITIONAL([ENABLE_INTROSPECTION], [test "x$enable_introspection" = "xyes"])
 AM_CONDITIONAL([ENABLE_GUDEV], [test "x$enable_gudev" = "xyes"])
 
-# ------------------------------------------------------------------------------
-# usb/pci-db - read vendor/device string database
-# ------------------------------------------------------------------------------
-AC_ARG_ENABLE([hwdb],
-       AS_HELP_STRING([--disable-hwdb], [disable hardware database support @<:@default=enabled@:>@]),
-       [], [enable_hwdb=yes])
-AS_IF([test "x$enable_hwdb" = "xyes"], [
-       if test "x$cross_compiling" = "xno" ; then
-               AC_CHECK_FILES([/usr/share/pci.ids], [pciids=/usr/share/pci.ids])
-               AC_CHECK_FILES([/usr/share/hwdata/pci.ids], [pciids=/usr/share/hwdata/pci.ids])
-               AC_CHECK_FILES([/usr/share/misc/pci.ids], [pciids=/usr/share/misc/pci.ids])
-       fi
-
-       AC_ARG_WITH(usb-ids-path,
-               [AS_HELP_STRING([--with-usb-ids-path=DIR], [Path to usb.ids file])],
-               [USB_DATABASE=${withval}],
-               [if test -n "$usbids" ; then
-                       USB_DATABASE="$usbids"
-               else
-                       PKG_CHECK_MODULES(USBUTILS, usbutils >= 0.82)
-                       AC_SUBST([USB_DATABASE], [$($PKG_CONFIG --variable=usbids usbutils)])
-               fi])
-       AC_MSG_CHECKING([for USB database location])
-       AC_MSG_RESULT([$USB_DATABASE])
-       AC_SUBST(USB_DATABASE)
-
-       AC_ARG_WITH(pci-ids-path,
-               [AS_HELP_STRING([--with-pci-ids-path=DIR], [Path to pci.ids file])],
-               [PCI_DATABASE=${withval}],
-               [if test -n "$pciids" ; then
-                       PCI_DATABASE="$pciids"
-               else
-                       AC_MSG_ERROR([pci.ids not found, try --with-pci-ids-path=])
-               fi])
-       AC_MSG_CHECKING([for PCI database location])
-       AC_MSG_RESULT([$PCI_DATABASE])
-       AC_SUBST(PCI_DATABASE)
-])
-AM_CONDITIONAL([ENABLE_HWDB], [test "x$enable_hwdb" = "xyes"])
-
 # ------------------------------------------------------------------------------
 # keymap - map custom hardware's multimedia keys
 # ------------------------------------------------------------------------------
@@ -241,6 +234,8 @@ AC_MSG_RESULT([
        include_prefix:         ${INCLUDE_PREFIX}
        systemdsystemunitdir:   ${systemdsystemunitdir}
        firmware path:          ${FIRMWARE_PATH}
+       usb.ids:                ${USB_DATABASE}
+       pci.ids:                ${PCI_DATABASE}
 
        compiler:               ${CC}
        cflags:                 ${CFLAGS}
@@ -255,9 +250,6 @@ AC_MSG_RESULT([
        gudev:                  ${enable_gudev}
        gintrospection:         ${enable_introspection}
        keymap:                 ${enable_keymap}
-       hwdb:                   ${enable_hwdb}
-         usb.ids:              ${USB_DATABASE}
-         pci.ids:              ${PCI_DATABASE}
        mtd_probe:              ${enable_mtd_probe}
        rule_generator:         ${enable_rule_generator}
        udev_acl:               ${enable_udev_acl}
diff --git a/extras/usb-db/.gitignore b/extras/usb-db/.gitignore
deleted file mode 100644 (file)
index 861a838..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-usb-db
-pci-db
index 0ffce2c3487951dd51c94bb83d63fa4c76e8ce62..ce57d48e868906ce63c63ed06f757da10c6c1925 100644 (file)
@@ -4,11 +4,11 @@ ACTION=="remove", GOTO="net_end"
 SUBSYSTEM!="net", GOTO="net_end"
 
 SUBSYSTEMS=="usb", ENV{ID_MODEL}=="", IMPORT{builtin}="usb_id"
-SUBSYSTEMS=="usb", ENV{ID_MODEL_FROM_DATABASE}=="", IMPORT{program}="usb-db %p"
+SUBSYSTEMS=="usb", IMPORT{builtin}="usb-db"
 SUBSYSTEMS=="usb", ATTRS{idVendor}!="", ATTRS{idProduct}!="", ENV{ID_VENDOR_ID}="$attr{idVendor}", ENV{ID_MODEL_ID}="$attr{idProduct}"
 SUBSYSTEMS=="usb", GOTO="net_end"
 
-SUBSYSTEMS=="pci", ENV{ID_MODEL_FROM_DATABASE}=="", IMPORT{program}="pci-db %p"
+SUBSYSTEMS=="pci", IMPORT{builtin}="pci-db"
 SUBSYSTEMS=="pci", ENV{ID_BUS}="pci", ENV{ID_VENDOR_ID}="$attr{vendor}", ENV{ID_MODEL_ID}="$attr{device}"
 
 LABEL="net_end"
index b67c857dfdbd066e0f19eead07ec4c7173232a26..2e63e140cb1fb6d6d4aed10e1fb3300f89778499 100644 (file)
@@ -4,11 +4,11 @@ ACTION=="remove", GOTO="tty_end"
 SUBSYSTEM!="tty", GOTO="tty_end"
 
 SUBSYSTEMS=="usb", ENV{ID_MODEL}=="", IMPORT{builtin}="usb_id"
-SUBSYSTEMS=="usb", ENV{ID_MODEL_FROM_DATABASE}=="", IMPORT{program}="usb-db %p"
+SUBSYSTEMS=="usb", IMPORT{builtin}="usb-db"
 SUBSYSTEMS=="usb", ATTRS{idVendor}!="", ATTRS{idProduct}!="", ENV{ID_VENDOR_ID}="$attr{idVendor}", ENV{ID_MODEL_ID}="$attr{idProduct}"
 SUBSYSTEMS=="usb", GOTO="tty_end"
 
-SUBSYSTEMS=="pci", ENV{ID_MODEL_FROM_DATABASE}=="", IMPORT{program}="pci-db %p"
+SUBSYSTEMS=="pci", IMPORT{builtin}="pci-db"
 SUBSYSTEMS=="pci", ENV{ID_BUS}="pci", ENV{ID_VENDOR_ID}="$attr{vendor}", ENV{ID_MODEL_ID}="$attr{device}"
 
 LABEL="tty_end"
index e3a13b598d94315c8d87729ea54489d797b1dda3..cca88f1ec35a8f008e6dfe79739c919502dd38a4 100644 (file)
@@ -38,10 +38,10 @@ KERNEL!="card*", GOTO="sound_end"
 ENV{SOUND_INITIALIZED}="1"
 
 SUBSYSTEMS=="usb", IMPORT{builtin}="usb_id"
-SUBSYSTEMS=="usb", ENV{ID_VENDOR_FROM_DATABASE}=="", IMPORT{program}="usb-db %p"
+SUBSYSTEMS=="usb", IMPORT{builtin}="usb-db"
 SUBSYSTEMS=="usb", GOTO="skip_pci"
 
-SUBSYSTEMS=="pci", ENV{ID_VENDOR_FROM_DATABASE}=="", IMPORT{program}="pci-db %p"
+SUBSYSTEMS=="pci", IMPORT{builtin}="pci-db"
 SUBSYSTEMS=="pci", ENV{ID_BUS}="pci", ENV{ID_VENDOR_ID}="$attr{vendor}", ENV{ID_MODEL_ID}="$attr{device}"
 
 LABEL="skip_pci"
similarity index 56%
rename from extras/usb-db/usb-db.c
rename to udev/udev-builtin-hwdb.c
index fe563c2e758f70d2cec5d91e6dbbc78f77414e3e..e38c6b9d268c3afd5767873430d708cff4bb00b4 100644 (file)
@@ -1,15 +1,21 @@
 /*
+ * usb-db, pci-db - lookup vendor/product database
+ *
  * Copyright (C) 2009 Lennart Poettering <lennart@poettering.net>
+ * Copyright (C) 2011 Kay Sievers <kay.sievers@vrfy.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of the
- * License, or (at your option) any later version.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
  *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details:
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 #include <stdio.h>
 #include <ctype.h>
 #include <stdlib.h>
 
-#include <libudev.h>
-
-#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
+#include "udev.h"
 
 static int get_id_attr(
        struct udev_device *parent,
@@ -64,17 +54,19 @@ static int get_id_attr(
 
 static int get_vid_pid(
        struct udev_device *parent,
+       const char *vendor_attr,
+       const char *product_attr,
        uint16_t *vid,
        uint16_t *pid) {
 
-       if (get_id_attr(parent, VENDOR_ATTR, vid) < 0)
+       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)
+       if (get_id_attr(parent, product_attr, pid) < 0)
                return -1;
 
        return 0;
@@ -89,12 +81,10 @@ static void rstrip(char *n) {
 
 #define HEXCHARS "0123456789abcdefABCDEF"
 #define WHITESPACE " \t\n\r"
-
-static int lookup_vid_pid(
-       uint16_t vid,
-       uint16_t pid,
-       char **vendor,
-       char **product) {
+static int lookup_vid_pid(const char *database,
+                         uint16_t vid, uint16_t pid,
+                         char **vendor, char **product)
+{
 
        FILE *f;
        int ret = -1;
@@ -103,8 +93,8 @@ static int lookup_vid_pid(
 
        *vendor = *product = NULL;
 
-       if (!(f = fopen(DATABASE, "r"))) {
-               fprintf(stderr, "Failed to open database file "DATABASE": %s\n", strerror(errno));
+       if (!(f = fopen(database, "rme"))) {
+               fprintf(stderr, "Failed to open database file '%s': %s\n", database, strerror(errno));
                return -1;
        }
 
@@ -201,64 +191,62 @@ static struct udev_device *find_device(struct udev_device *dev, const char *subs
        }
        return dev;
 try_parent:
-       return udev_device_get_parent_with_subsystem_devtype(dev, SUBSYSTEM, DEVTYPE);
+       return udev_device_get_parent_with_subsystem_devtype(dev, subsys, devtype);
 }
 
-int main(int argc, char*argv[]) {
 
-       struct udev *udev = NULL;
-       int ret = 1;
-       char *sp;
-       struct udev_device *dev = NULL, *parent = NULL;
+static int builtin_db(struct udev_device *dev, bool test,
+                     const char *database,
+                     const char *vendor_attr, const char *product_attr,
+                     const char *subsys, const char *devtype)
+{
+       struct udev_device *parent;
        uint16_t vid = 0, pid = 0;
        char *vendor = NULL, *product = NULL;
 
-       if (argc < 2) {
-               fprintf(stderr, "Need to pass sysfs path.\n");
-               goto finish;
-       }
-
-       if (!(udev = udev_new()))
-               goto finish;
-
-       if (asprintf(&sp, "%s%s", udev_get_sys_path(udev), argv[1]) < 0) {
-               fprintf(stderr, "Failed to allocate sysfs path.\n");
-               goto finish;
-       }
-
-       dev = udev_device_new_from_syspath(udev, sp);
-       free(sp);
-
-       if (!dev) {
-               fprintf(stderr, "Failed to access %s.\n", argv[1]);
-               goto finish;
-       }
-
-       parent = find_device(dev, SUBSYSTEM, DEVTYPE);
+       parent = find_device(dev, subsys, devtype);
        if (!parent) {
                fprintf(stderr, "Failed to find device.\n");
                goto finish;
        }
 
-       if (get_vid_pid(parent, &vid, &pid) < 0)
+       if (get_vid_pid(parent, vendor_attr, product_attr, &vid, &pid) < 0)
                goto finish;
 
-       if (lookup_vid_pid(vid, pid, &vendor, &product) < 0)
+       if (lookup_vid_pid(database, vid, pid, &vendor, &product) < 0)
                goto finish;
 
        if (vendor)
-               printf("ID_VENDOR_FROM_DATABASE=%s\n", vendor);
-
+               udev_builtin_add_property(dev, test, "ID_VENDOR_FROM_DATABASE", vendor);
        if (product)
-               printf("ID_MODEL_FROM_DATABASE=%s\n", product);
-
-       ret = 0;
+               udev_builtin_add_property(dev, test, "ID_MODEL_FROM_DATABASE", product);
 
 finish:
-       udev_device_unref(dev);
-       udev_unref(udev);
        free(vendor);
        free(product);
+       return 0;
+}
 
-       return ret;
+static int builtin_usb_db(struct udev_device *dev, int argc, char *argv[], bool test)
+{
+       return builtin_db(dev, test, USB_DATABASE, "idVendor", "idProduct", "usb", "usb_device");
 }
+
+static int builtin_pci_db(struct udev_device *dev, int argc, char *argv[], bool test)
+{
+       return builtin_db(dev, test, PCI_DATABASE, "vendor", "device", "pci", NULL);
+}
+
+const struct udev_builtin udev_builtin_usb_db = {
+       .name = "usb-db",
+       .cmd = builtin_usb_db,
+       .help = "USB vendor/product database",
+       .run_once = true,
+};
+
+const struct udev_builtin udev_builtin_pci_db = {
+       .name = "pci-db",
+       .cmd = builtin_pci_db,
+       .help = "PCI vendor/product database",
+       .run_once = true,
+};
index 9804c50faecc8cff4abdc4920c24232639cabbf2..8beac8a67872676d8408fb448a103c8c5c45dcd9 100644 (file)
@@ -31,6 +31,8 @@ static const struct udev_builtin *builtins[] = {
        [UDEV_BUILTIN_INPUT_ID] = &udev_builtin_input_id,
        [UDEV_BUILTIN_KMOD] = &udev_builtin_kmod,
        [UDEV_BUILTIN_PATH_ID] = &udev_builtin_path_id,
+       [UDEV_BUILTIN_PCI_DB] = &udev_builtin_pci_db,
+       [UDEV_BUILTIN_USB_DB] = &udev_builtin_usb_db,
        [UDEV_BUILTIN_USB_ID] = &udev_builtin_usb_id,
 };
 
index a289f35c4d8ba897534a32d4f20b2999319fa697..4f2886c69cd423701eba3f4bf30594ee831ae51d 100644 (file)
@@ -138,6 +138,8 @@ enum udev_builtin_cmd {
        UDEV_BUILTIN_INPUT_ID,
        UDEV_BUILTIN_KMOD,
        UDEV_BUILTIN_PATH_ID,
+       UDEV_BUILTIN_PCI_DB,
+       UDEV_BUILTIN_USB_DB,
        UDEV_BUILTIN_USB_ID,
        UDEV_BUILTIN_MAX
 };
@@ -155,6 +157,8 @@ extern const struct udev_builtin udev_builtin_firmware;
 extern const struct udev_builtin udev_builtin_input_id;
 extern const struct udev_builtin udev_builtin_kmod;
 extern const struct udev_builtin udev_builtin_path_id;
+extern const struct udev_builtin udev_builtin_pci_db;
+extern const struct udev_builtin udev_builtin_usb_db;
 extern const struct udev_builtin udev_builtin_usb_id;
 int udev_builtin_init(struct udev *udev);
 void udev_builtin_exit(struct udev *udev);