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/>.
32 #include "hwdb-util.h"
36 int udev_builtin_hwdb_lookup(struct udev_device
*dev
,
37 const char *prefix
, const char *modalias
,
38 const char *filter
, bool test
) {
39 _cleanup_free_
const char *lookup
= NULL
;
40 const char *key
, *value
;
47 lookup
= strjoin(prefix
, modalias
, NULL
);
53 SD_HWDB_FOREACH_PROPERTY(hwdb
, modalias
, key
, value
) {
54 if (filter
&& fnmatch(filter
, key
, FNM_NOESCAPE
) != 0)
57 if (udev_builtin_add_property(dev
, test
, key
, value
) < 0)
64 static const char *modalias_usb(struct udev_device
*dev
, char *s
, size_t size
) {
68 v
= udev_device_get_sysattr_value(dev
, "idVendor");
71 p
= udev_device_get_sysattr_value(dev
, "idProduct");
74 vn
= strtol(v
, NULL
, 16);
77 pn
= strtol(p
, NULL
, 16);
80 snprintf(s
, size
, "usb:v%04Xp%04X*", vn
, pn
);
84 static int udev_builtin_hwdb_search(struct udev_device
*dev
, struct udev_device
*srcdev
,
85 const char *subsystem
, const char *prefix
,
86 const char *filter
, bool test
) {
87 struct udev_device
*d
;
92 for (d
= srcdev
; d
&& !last
; d
= udev_device_get_parent(d
)) {
94 const char *modalias
= NULL
;
96 dsubsys
= udev_device_get_subsystem(d
);
100 /* look only at devices of a specific subsystem */
101 if (subsystem
&& !streq(dsubsys
, subsystem
))
104 modalias
= udev_device_get_property_value(d
, "MODALIAS");
106 if (streq(dsubsys
, "usb") && streq_ptr(udev_device_get_devtype(d
), "usb_device")) {
107 /* if the usb_device does not have a modalias, compose one */
109 modalias
= modalias_usb(d
, s
, sizeof(s
));
111 /* avoid looking at any parent device, they are usually just a USB hub */
118 r
= udev_builtin_hwdb_lookup(dev
, prefix
, modalias
, filter
, test
);
126 static int builtin_hwdb(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
127 static const struct option options
[] = {
128 { "filter", required_argument
, NULL
, 'f' },
129 { "device", required_argument
, NULL
, 'd' },
130 { "subsystem", required_argument
, NULL
, 's' },
131 { "lookup-prefix", required_argument
, NULL
, 'p' },
134 const char *filter
= NULL
;
135 const char *device
= NULL
;
136 const char *subsystem
= NULL
;
137 const char *prefix
= NULL
;
138 struct udev_device
*srcdev
;
146 option
= getopt_long(argc
, argv
, "f:d:s:p:", options
, NULL
);
169 /* query a specific key given as argument */
171 if (udev_builtin_hwdb_lookup(dev
, prefix
, argv
[optind
], filter
, test
) > 0)
176 /* read data from another device than the device we will store the data */
178 srcdev
= udev_device_new_from_device_id(udev_device_get_udev(dev
), device
);
184 if (udev_builtin_hwdb_search(dev
, srcdev
, subsystem
, prefix
, filter
, test
) > 0)
189 /* called at udev startup and reload */
190 static int builtin_hwdb_init(struct udev
*udev
) {
196 r
= sd_hwdb_new(&hwdb
);
203 /* called on udev shutdown and reload request */
204 static void builtin_hwdb_exit(struct udev
*udev
) {
205 hwdb
= sd_hwdb_unref(hwdb
);
208 /* called every couple of seconds during event activity; 'true' if config has changed */
209 static bool builtin_hwdb_validate(struct udev
*udev
) {
210 return hwdb_validate(hwdb
);
213 const struct udev_builtin udev_builtin_hwdb
= {
216 .init
= builtin_hwdb_init
,
217 .exit
= builtin_hwdb_exit
,
218 .validate
= builtin_hwdb_validate
,
219 .help
= "Hardware database",