1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright Tom Gundersen <teg@jklm.no>
8 #include "alloc-util.h"
10 #include "libudev-private.h"
13 * SECTION:libudev-hwdb
14 * @short_description: retrieve properties from the hardware database
16 * Libudev hardware database interface.
22 * Opaque object representing the hardware database.
30 struct udev_list properties_list
;
35 * @udev: udev library context
37 * Create a hardware database context to query properties for devices.
39 * Returns: a hwdb context.
41 _public_
struct udev_hwdb
*udev_hwdb_new(struct udev
*udev
) {
42 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb_internal
= NULL
;
43 struct udev_hwdb
*hwdb
;
46 assert_return_errno(udev
, NULL
, EINVAL
);
48 r
= sd_hwdb_new(&hwdb_internal
);
54 hwdb
= new0(struct udev_hwdb
, 1);
61 hwdb
->hwdb
= TAKE_PTR(hwdb_internal
);
63 udev_list_init(udev
, &hwdb
->properties_list
, true);
72 * Take a reference of a hwdb context.
74 * Returns: the passed enumeration context
76 _public_
struct udev_hwdb
*udev_hwdb_ref(struct udev_hwdb
*hwdb
) {
87 * Drop a reference of a hwdb context. If the refcount reaches zero,
88 * all resources of the hwdb context will be released.
92 _public_
struct udev_hwdb
*udev_hwdb_unref(struct udev_hwdb
*hwdb
) {
96 if (hwdb
->refcount
> 0)
98 sd_hwdb_unref(hwdb
->hwdb
);
99 udev_list_cleanup(&hwdb
->properties_list
);
104 * udev_hwdb_get_properties_list_entry:
106 * @modalias: modalias string
109 * Lookup a matching device in the hardware database. The lookup key is a
110 * modalias string, whose formats are defined for the Linux kernel modules.
111 * Examples are: pci:v00008086d00001C2D*, usb:v04F2pB221*. The first entry
112 * of a list of retrieved properties is returned.
114 * Returns: a udev_list_entry.
116 _public_
struct udev_list_entry
*udev_hwdb_get_properties_list_entry(struct udev_hwdb
*hwdb
, const char *modalias
, unsigned int flags
) {
117 const char *key
, *value
;
118 struct udev_list_entry
*e
;
120 if (!hwdb
|| !modalias
) {
125 udev_list_cleanup(&hwdb
->properties_list
);
127 SD_HWDB_FOREACH_PROPERTY(hwdb
->hwdb
, modalias
, key
, value
) {
128 if (udev_list_entry_add(&hwdb
->properties_list
, key
, value
) == NULL
) {
134 e
= udev_list_get_entry(&hwdb
->properties_list
);