From: Kay Sievers Date: Tue, 27 Dec 2011 16:02:52 +0000 (+0100) Subject: builtin: move usb-db, pci-db to builtins X-Git-Tag: 176~27 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3cf5266b029c1ec4522edc6b42d874d80407a246;p=systemd builtin: move usb-db, pci-db to builtins --- diff --git a/Makefile.am b/Makefile.am index a7f29676..d95a3619 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 diff --git a/configure.ac b/configure.ac index a9ba74c1..497a74fc 100644 --- a/configure.ac +++ b/configure.ac @@ -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 index 861a8382..00000000 --- a/extras/usb-db/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -usb-db -pci-db diff --git a/rules/rules.d/75-net-description.rules b/rules/rules.d/75-net-description.rules index 0ffce2c3..ce57d48e 100644 --- a/rules/rules.d/75-net-description.rules +++ b/rules/rules.d/75-net-description.rules @@ -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" diff --git a/rules/rules.d/75-tty-description.rules b/rules/rules.d/75-tty-description.rules index b67c857d..2e63e140 100644 --- a/rules/rules.d/75-tty-description.rules +++ b/rules/rules.d/75-tty-description.rules @@ -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" diff --git a/rules/rules.d/78-sound-card.rules b/rules/rules.d/78-sound-card.rules index e3a13b59..cca88f1e 100644 --- a/rules/rules.d/78-sound-card.rules +++ b/rules/rules.d/78-sound-card.rules @@ -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" diff --git a/extras/usb-db/usb-db.c b/udev/udev-builtin-hwdb.c similarity index 56% rename from extras/usb-db/usb-db.c rename to udev/udev-builtin-hwdb.c index fe563c2e..e38c6b9d 100644 --- a/extras/usb-db/usb-db.c +++ b/udev/udev-builtin-hwdb.c @@ -1,15 +1,21 @@ /* + * usb-db, pci-db - lookup vendor/product database + * * Copyright (C) 2009 Lennart Poettering + * Copyright (C) 2011 Kay Sievers + * + * 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 . */ #include @@ -19,23 +25,7 @@ #include #include -#include - -#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, +}; diff --git a/udev/udev-builtin.c b/udev/udev-builtin.c index 9804c50f..8beac8a6 100644 --- a/udev/udev-builtin.c +++ b/udev/udev-builtin.c @@ -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, }; diff --git a/udev/udev.h b/udev/udev.h index a289f35c..4f2886c6 100644 --- a/udev/udev.h +++ b/udev/udev.h @@ -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);