1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
9 #include "libudev-list-internal.h"
12 * SECTION:libudev-hwdb
13 * @short_description: retrieve properties from the hardware database
15 * Libudev hardware database interface.
21 * Opaque object representing the hardware database.
26 struct udev_list properties_list
;
31 * @udev: udev library context (unused)
33 * Create a hardware database context to query properties for devices.
35 * Returns: a hwdb context.
37 _public_
struct udev_hwdb
*udev_hwdb_new(struct udev
*udev
) {
38 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb_internal
= NULL
;
39 struct udev_hwdb
*hwdb
;
42 r
= sd_hwdb_new(&hwdb_internal
);
44 return_with_errno(NULL
, r
);
46 hwdb
= new(struct udev_hwdb
, 1);
48 return_with_errno(NULL
, ENOMEM
);
50 *hwdb
= (struct udev_hwdb
) {
52 .hwdb
= TAKE_PTR(hwdb_internal
),
55 udev_list_init(&hwdb
->properties_list
, true);
60 static struct udev_hwdb
*udev_hwdb_free(struct udev_hwdb
*hwdb
) {
63 sd_hwdb_unref(hwdb
->hwdb
);
64 udev_list_cleanup(&hwdb
->properties_list
);
72 * Take a reference of a hwdb context.
74 * Returns: the passed enumeration context
81 * Drop a reference of a hwdb context. If the refcount reaches zero,
82 * all resources of the hwdb context will be released.
86 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(struct udev_hwdb
, udev_hwdb
, udev_hwdb_free
);
89 * udev_hwdb_get_properties_list_entry:
91 * @modalias: modalias string
94 * Lookup a matching device in the hardware database. The lookup key is a
95 * modalias string, whose formats are defined for the Linux kernel modules.
96 * Examples are: pci:v00008086d00001C2D*, usb:v04F2pB221*. The first entry
97 * of a list of retrieved properties is returned.
99 * Returns: a udev_list_entry.
101 _public_
struct udev_list_entry
*udev_hwdb_get_properties_list_entry(struct udev_hwdb
*hwdb
, const char *modalias
, unsigned flags
) {
102 const char *key
, *value
;
103 struct udev_list_entry
*e
;
105 assert_return_errno(hwdb
, NULL
, EINVAL
);
106 assert_return_errno(modalias
, NULL
, EINVAL
);
108 udev_list_cleanup(&hwdb
->properties_list
);
110 SD_HWDB_FOREACH_PROPERTY(hwdb
->hwdb
, modalias
, key
, value
)
111 if (!udev_list_entry_add(&hwdb
->properties_list
, key
, value
))
112 return_with_errno(NULL
, ENOMEM
);
114 e
= udev_list_get_entry(&hwdb
->properties_list
);
116 return_with_errno(NULL
, ENODATA
);