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/>.
31 static struct udev_hwdb
*hwdb
;
33 int udev_builtin_hwdb_lookup(struct udev_device
*dev
,
34 const char *prefix
, const char *modalias
,
35 const char *filter
, bool test
) {
36 struct udev_list_entry
*list
;
37 struct udev_list_entry
*entry
;
44 _cleanup_free_
const char *lookup
;
46 lookup
= strjoin(prefix
, modalias
, NULL
);
49 list
= udev_hwdb_get_properties_list_entry(hwdb
, lookup
, 0);
51 list
= udev_hwdb_get_properties_list_entry(hwdb
, modalias
, 0);
53 udev_list_entry_foreach(entry
, list
) {
54 if (filter
&& fnmatch(filter
, udev_list_entry_get_name(entry
), FNM_NOESCAPE
) != 0)
57 if (udev_builtin_add_property(dev
, test
,
58 udev_list_entry_get_name(entry
),
59 udev_list_entry_get_value(entry
)) < 0)
66 static const char *modalias_usb(struct udev_device
*dev
, char *s
, size_t size
) {
70 v
= udev_device_get_sysattr_value(dev
, "idVendor");
73 p
= udev_device_get_sysattr_value(dev
, "idProduct");
76 vn
= strtol(v
, NULL
, 16);
79 pn
= strtol(p
, NULL
, 16);
82 snprintf(s
, size
, "usb:v%04Xp%04X*", vn
, pn
);
86 static int udev_builtin_hwdb_search(struct udev_device
*dev
, struct udev_device
*srcdev
,
87 const char *subsystem
, const char *prefix
,
88 const char *filter
, bool test
) {
89 struct udev_device
*d
;
94 for (d
= srcdev
; d
&& !last
; d
= udev_device_get_parent(d
)) {
96 const char *modalias
= NULL
;
98 dsubsys
= udev_device_get_subsystem(d
);
102 /* look only at devices of a specific subsystem */
103 if (subsystem
&& !streq(dsubsys
, subsystem
))
106 modalias
= udev_device_get_property_value(d
, "MODALIAS");
108 if (streq(dsubsys
, "usb") && streq_ptr(udev_device_get_devtype(d
), "usb_device")) {
109 /* if the usb_device does not have a modalias, compose one */
111 modalias
= modalias_usb(d
, s
, sizeof(s
));
113 /* avoid looking at any parent device, they are usually just a USB hub */
120 r
= udev_builtin_hwdb_lookup(dev
, prefix
, modalias
, filter
, test
);
128 static int builtin_hwdb(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
129 static const struct option options
[] = {
130 { "filter", required_argument
, NULL
, 'f' },
131 { "device", required_argument
, NULL
, 'd' },
132 { "subsystem", required_argument
, NULL
, 's' },
133 { "lookup-prefix", required_argument
, NULL
, 'p' },
136 const char *filter
= NULL
;
137 const char *device
= NULL
;
138 const char *subsystem
= NULL
;
139 const char *prefix
= NULL
;
140 struct udev_device
*srcdev
;
148 option
= getopt_long(argc
, argv
, "f:d:s:p:", options
, NULL
);
171 /* query a specific key given as argument */
173 if (udev_builtin_hwdb_lookup(dev
, prefix
, argv
[optind
], filter
, test
) > 0)
178 /* read data from another device than the device we will store the data */
180 srcdev
= udev_device_new_from_device_id(udev_device_get_udev(dev
), device
);
186 if (udev_builtin_hwdb_search(dev
, srcdev
, subsystem
, prefix
, filter
, test
) > 0)
191 /* called at udev startup and reload */
192 static int builtin_hwdb_init(struct udev
*udev
) {
195 hwdb
= udev_hwdb_new(udev
);
201 /* called on udev shutdown and reload request */
202 static void builtin_hwdb_exit(struct udev
*udev
) {
203 hwdb
= udev_hwdb_unref(hwdb
);
206 /* called every couple of seconds during event activity; 'true' if config has changed */
207 static bool builtin_hwdb_validate(struct udev
*udev
) {
208 return udev_hwdb_validate(hwdb
);
211 const struct udev_builtin udev_builtin_hwdb
= {
214 .init
= builtin_hwdb_init
,
215 .exit
= builtin_hwdb_exit
,
216 .validate
= builtin_hwdb_validate
,
217 .help
= "hardware database",