2 This file is part of systemd.
4 Copyright 2012 Kay Sievers <kay@vrfy.org>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "hwdb-util.h"
32 int udev_builtin_hwdb_lookup(struct udev_device *dev,
33 const char *prefix, const char *modalias,
34 const char *filter, bool test) {
35 _cleanup_free_ const char *lookup = NULL;
36 const char *key, *value;
43 lookup = strjoin(prefix, modalias, NULL);
49 SD_HWDB_FOREACH_PROPERTY(hwdb, modalias, key, value) {
50 if (filter && fnmatch(filter, key, FNM_NOESCAPE) != 0)
53 if (udev_builtin_add_property(dev, test, key, value) < 0)
60 static const char *modalias_usb(struct udev_device *dev, char *s, size_t size) {
64 v = udev_device_get_sysattr_value(dev, "idVendor");
67 p = udev_device_get_sysattr_value(dev, "idProduct");
70 vn = strtol(v, NULL, 16);
73 pn = strtol(p, NULL, 16);
76 snprintf(s, size, "usb:v%04Xp%04X*", vn, pn);
80 static int udev_builtin_hwdb_search(struct udev_device *dev, struct udev_device *srcdev,
81 const char *subsystem, const char *prefix,
82 const char *filter, bool test) {
83 struct udev_device *d;
88 for (d = srcdev; d && !last; d = udev_device_get_parent(d)) {
90 const char *modalias = NULL;
92 dsubsys = udev_device_get_subsystem(d);
96 /* look only at devices of a specific subsystem */
97 if (subsystem && !streq(dsubsys, subsystem))
100 modalias = udev_device_get_property_value(d, "MODALIAS");
102 if (streq(dsubsys, "usb") && streq_ptr(udev_device_get_devtype(d), "usb_device")) {
103 /* if the usb_device does not have a modalias, compose one */
105 modalias = modalias_usb(d, s, sizeof(s));
107 /* avoid looking at any parent device, they are usually just a USB hub */
114 r = udev_builtin_hwdb_lookup(dev, prefix, modalias, filter, test);
122 static int builtin_hwdb(struct udev_device *dev, int argc, char *argv[], bool test) {
123 static const struct option options[] = {
124 { "filter", required_argument, NULL, 'f' },
125 { "device", required_argument, NULL, 'd' },
126 { "subsystem", required_argument, NULL, 's' },
127 { "lookup-prefix", required_argument, NULL, 'p' },
130 const char *filter = NULL;
131 const char *device = NULL;
132 const char *subsystem = NULL;
133 const char *prefix = NULL;
134 struct udev_device *srcdev;
142 option = getopt_long(argc, argv, "f:d:s:p:", options, NULL);
165 /* query a specific key given as argument */
167 if (udev_builtin_hwdb_lookup(dev, prefix, argv[optind], filter, test) > 0)
172 /* read data from another device than the device we will store the data */
174 srcdev = udev_device_new_from_device_id(udev_device_get_udev(dev), device);
180 if (udev_builtin_hwdb_search(dev, srcdev, subsystem, prefix, filter, test) > 0)
185 /* called at udev startup and reload */
186 static int builtin_hwdb_init(struct udev *udev) {
192 r = sd_hwdb_new(&hwdb);
199 /* called on udev shutdown and reload request */
200 static void builtin_hwdb_exit(struct udev *udev) {
201 hwdb = sd_hwdb_unref(hwdb);
204 /* called every couple of seconds during event activity; 'true' if config has changed */
205 static bool builtin_hwdb_validate(struct udev *udev) {
206 return hwdb_validate(hwdb);
209 const struct udev_builtin udev_builtin_hwdb = {
212 .init = builtin_hwdb_init,
213 .exit = builtin_hwdb_exit,
214 .validate = builtin_hwdb_validate,
215 .help = "Hardware database",