1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright Tom Gundersen <teg@jklm.no>
10 #include "alloc-util.h"
11 #include "hwdb-util.h"
12 #include "libudev-private.h"
15 * SECTION:libudev-hwdb
16 * @short_description: retrieve properties from the hardware database
18 * Libudev hardware database interface.
24 * Opaque object representing the hardware database.
32 struct udev_list properties_list
;
37 * @udev: udev library context
39 * Create a hardware database context to query properties for devices.
41 * Returns: a hwdb context.
43 _public_
struct udev_hwdb
*udev_hwdb_new(struct udev
*udev
) {
44 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb_internal
= NULL
;
45 struct udev_hwdb
*hwdb
;
48 assert_return_errno(udev
, NULL
, EINVAL
);
50 r
= sd_hwdb_new(&hwdb_internal
);
56 hwdb
= new0(struct udev_hwdb
, 1);
63 hwdb
->hwdb
= TAKE_PTR(hwdb_internal
);
65 udev_list_init(udev
, &hwdb
->properties_list
, true);
74 * Take a reference of a hwdb context.
76 * Returns: the passed enumeration context
78 _public_
struct udev_hwdb
*udev_hwdb_ref(struct udev_hwdb
*hwdb
) {
89 * Drop a reference of a hwdb context. If the refcount reaches zero,
90 * all resources of the hwdb context will be released.
94 _public_
struct udev_hwdb
*udev_hwdb_unref(struct udev_hwdb
*hwdb
) {
98 if (hwdb
->refcount
> 0)
100 sd_hwdb_unref(hwdb
->hwdb
);
101 udev_list_cleanup(&hwdb
->properties_list
);
106 * udev_hwdb_get_properties_list_entry:
108 * @modalias: modalias string
111 * Lookup a matching device in the hardware database. The lookup key is a
112 * modalias string, whose formats are defined for the Linux kernel modules.
113 * Examples are: pci:v00008086d00001C2D*, usb:v04F2pB221*. The first entry
114 * of a list of retrieved properties is returned.
116 * Returns: a udev_list_entry.
118 _public_
struct udev_list_entry
*udev_hwdb_get_properties_list_entry(struct udev_hwdb
*hwdb
, const char *modalias
, unsigned int flags
) {
119 const char *key
, *value
;
120 struct udev_list_entry
*e
;
122 if (!hwdb
|| !modalias
) {
127 udev_list_cleanup(&hwdb
->properties_list
);
129 SD_HWDB_FOREACH_PROPERTY(hwdb
->hwdb
, modalias
, key
, value
) {
130 if (udev_list_entry_add(&hwdb
->properties_list
, key
, value
) == NULL
) {
136 e
= udev_list_get_entry(&hwdb
->properties_list
);