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/>.
30 static struct udev_hwdb *hwdb;
32 int udev_builtin_hwdb_lookup(struct udev_device *dev, const char *modalias, bool test) {
33 struct udev_list_entry *entry;
39 udev_list_entry_foreach(entry, udev_hwdb_get_properties_list_entry(hwdb, modalias, 0)) {
40 if (udev_builtin_add_property(dev, test,
41 udev_list_entry_get_name(entry),
42 udev_list_entry_get_value(entry)) < 0)
49 static const char *modalias_usb(struct udev_device *dev, char *s, size_t size) {
53 v = udev_device_get_sysattr_value(dev, "idVendor");
56 p = udev_device_get_sysattr_value(dev, "idProduct");
59 vn = strtol(v, NULL, 16);
62 pn = strtol(p, NULL, 16);
65 snprintf(s, size, "usb:v%04Xp%04X*", vn, pn);
69 static int udev_builtin_hwdb_search(struct udev_device *dev, const char *subsystem, bool test) {
70 struct udev_device *d;
74 for (d = dev; d; d = udev_device_get_parent(d)) {
76 const char *modalias = NULL;
78 dsubsys = udev_device_get_subsystem(d);
82 /* look only at devices of a specific subsystem */
83 if (subsystem && !streq(dsubsys, subsystem))
86 /* the usb_device does not have a modalias, compose one */
87 if (streq(dsubsys, "usb"))
88 modalias = modalias_usb(dev, s, sizeof(s));
91 modalias = udev_device_get_property_value(d, "MODALIAS");
95 n = udev_builtin_hwdb_lookup(dev, modalias, test);
103 static int builtin_hwdb(struct udev_device *dev, int argc, char *argv[], bool test) {
104 static const struct option options[] = {
105 { "subsystem", required_argument, NULL, 's' },
108 const char *subsystem = NULL;
116 option = getopt_long(argc, argv, "s", options, NULL);
127 if (udev_builtin_hwdb_search(dev, subsystem, test) < 0)
132 /* called at udev startup and reload */
133 static int builtin_hwdb_init(struct udev *udev)
137 hwdb = udev_hwdb_new(udev);
143 /* called on udev shutdown and reload request */
144 static void builtin_hwdb_exit(struct udev *udev)
146 hwdb = udev_hwdb_unref(hwdb);
149 /* called every couple of seconds during event activity; 'true' if config has changed */
150 static bool builtin_hwdb_validate(struct udev *udev)
152 return udev_hwdb_validate(hwdb);
155 const struct udev_builtin udev_builtin_hwdb = {
158 .init = builtin_hwdb_init,
159 .exit = builtin_hwdb_exit,
160 .validate = builtin_hwdb_validate,
161 .help = "hardware database",