1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright Tom Gundersen <teg@jklm.no>
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "alloc-util.h"
24 #include "hwdb-util.h"
25 #include "libudev-private.h"
28 * SECTION:libudev-hwdb
29 * @short_description: retrieve properties from the hardware database
31 * Libudev hardware database interface.
37 * Opaque object representing the hardware database.
45 struct udev_list properties_list
;
50 * @udev: udev library context
52 * Create a hardware database context to query properties for devices.
54 * Returns: a hwdb context.
56 _public_
struct udev_hwdb
*udev_hwdb_new(struct udev
*udev
) {
57 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb_internal
= NULL
;
58 struct udev_hwdb
*hwdb
;
61 assert_return_errno(udev
, NULL
, EINVAL
);
63 r
= sd_hwdb_new(&hwdb_internal
);
69 hwdb
= new0(struct udev_hwdb
, 1);
76 hwdb
->hwdb
= TAKE_PTR(hwdb_internal
);
78 udev_list_init(udev
, &hwdb
->properties_list
, true);
87 * Take a reference of a hwdb context.
89 * Returns: the passed enumeration context
91 _public_
struct udev_hwdb
*udev_hwdb_ref(struct udev_hwdb
*hwdb
) {
102 * Drop a reference of a hwdb context. If the refcount reaches zero,
103 * all resources of the hwdb context will be released.
107 _public_
struct udev_hwdb
*udev_hwdb_unref(struct udev_hwdb
*hwdb
) {
111 if (hwdb
->refcount
> 0)
113 sd_hwdb_unref(hwdb
->hwdb
);
114 udev_list_cleanup(&hwdb
->properties_list
);
119 * udev_hwdb_get_properties_list_entry:
121 * @modalias: modalias string
124 * Lookup a matching device in the hardware database. The lookup key is a
125 * modalias string, whose formats are defined for the Linux kernel modules.
126 * Examples are: pci:v00008086d00001C2D*, usb:v04F2pB221*. The first entry
127 * of a list of retrieved properties is returned.
129 * Returns: a udev_list_entry.
131 _public_
struct udev_list_entry
*udev_hwdb_get_properties_list_entry(struct udev_hwdb
*hwdb
, const char *modalias
, unsigned int flags
) {
132 const char *key
, *value
;
133 struct udev_list_entry
*e
;
135 if (!hwdb
|| !modalias
) {
140 udev_list_cleanup(&hwdb
->properties_list
);
142 SD_HWDB_FOREACH_PROPERTY(hwdb
->hwdb
, modalias
, key
, value
) {
143 if (udev_list_entry_add(&hwdb
->properties_list
, key
, value
) == NULL
) {
149 e
= udev_list_get_entry(&hwdb
->properties_list
);