1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2012 Kay Sievers <kay@vrfy.org>
13 #include "alloc-util.h"
14 #include "hwdb-util.h"
15 #include "parse-util.h"
16 #include "string-util.h"
17 #include "udev-util.h"
22 int udev_builtin_hwdb_lookup(struct udev_device
*dev
,
23 const char *prefix
, const char *modalias
,
24 const char *filter
, bool test
) {
25 _cleanup_free_
char *lookup
= NULL
;
26 const char *key
, *value
;
33 lookup
= strjoin(prefix
, modalias
);
39 SD_HWDB_FOREACH_PROPERTY(hwdb
, modalias
, key
, value
) {
40 if (filter
&& fnmatch(filter
, key
, FNM_NOESCAPE
) != 0)
43 if (udev_builtin_add_property(dev
, test
, key
, value
) < 0)
50 static const char *modalias_usb(struct udev_device
*dev
, char *s
, size_t size
) {
54 v
= udev_device_get_sysattr_value(dev
, "idVendor");
57 p
= udev_device_get_sysattr_value(dev
, "idProduct");
60 if (safe_atoux16(v
, &vn
) < 0)
62 if (safe_atoux16(p
, &pn
) < 0)
64 snprintf(s
, size
, "usb:v%04Xp%04X*", vn
, pn
);
68 static int udev_builtin_hwdb_search(struct udev_device
*dev
, struct udev_device
*srcdev
,
69 const char *subsystem
, const char *prefix
,
70 const char *filter
, bool test
) {
71 struct udev_device
*d
;
81 for (d
= srcdev
; d
&& !last
; d
= udev_device_get_parent(d
)) {
83 const char *modalias
= NULL
;
85 dsubsys
= udev_device_get_subsystem(d
);
89 /* look only at devices of a specific subsystem */
90 if (subsystem
&& !streq(dsubsys
, subsystem
))
93 modalias
= udev_device_get_property_value(d
, "MODALIAS");
95 if (streq(dsubsys
, "usb") && streq_ptr(udev_device_get_devtype(d
), "usb_device")) {
96 /* if the usb_device does not have a modalias, compose one */
98 modalias
= modalias_usb(d
, s
, sizeof(s
));
100 /* avoid looking at any parent device, they are usually just a USB hub */
107 r
= udev_builtin_hwdb_lookup(dev
, prefix
, modalias
, filter
, test
);
115 static int builtin_hwdb(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
116 static const struct option options
[] = {
117 { "filter", required_argument
, NULL
, 'f' },
118 { "device", required_argument
, NULL
, 'd' },
119 { "subsystem", required_argument
, NULL
, 's' },
120 { "lookup-prefix", required_argument
, NULL
, 'p' },
123 const char *filter
= NULL
;
124 const char *device
= NULL
;
125 const char *subsystem
= NULL
;
126 const char *prefix
= NULL
;
127 _cleanup_(udev_device_unrefp
) struct udev_device
*srcdev
= NULL
;
135 option
= getopt_long(argc
, argv
, "f:d:s:p:", options
, NULL
);
158 /* query a specific key given as argument */
160 if (udev_builtin_hwdb_lookup(dev
, prefix
, argv
[optind
], filter
, test
) > 0)
165 /* read data from another device than the device we will store the data */
167 srcdev
= udev_device_new_from_device_id(udev_device_get_udev(dev
), device
);
172 if (udev_builtin_hwdb_search(dev
, srcdev
, subsystem
, prefix
, filter
, test
) > 0)
177 /* called at udev startup and reload */
178 static int builtin_hwdb_init(struct udev
*udev
) {
184 r
= sd_hwdb_new(&hwdb
);
191 /* called on udev shutdown and reload request */
192 static void builtin_hwdb_exit(struct udev
*udev
) {
193 hwdb
= sd_hwdb_unref(hwdb
);
196 /* called every couple of seconds during event activity; 'true' if config has changed */
197 static bool builtin_hwdb_validate(struct udev
*udev
) {
198 return hwdb_validate(hwdb
);
201 const struct udev_builtin udev_builtin_hwdb
= {
204 .init
= builtin_hwdb_init
,
205 .exit
= builtin_hwdb_exit
,
206 .validate
= builtin_hwdb_validate
,
207 .help
= "Hardware database",