]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libudev/libudev-hwdb.c
libudev: coding style fixes
[thirdparty/systemd.git] / src / libudev / libudev-hwdb.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
2001208c 2
5ea78a39
YW
3#include <errno.h>
4
8b516fde 5#include "sd-hwdb.h"
07630cea 6
b5efdb8a 7#include "alloc-util.h"
8b516fde 8#include "hwdb-util.h"
5ea78a39 9#include "libudev-list-internal.h"
2001208c
KS
10
11/**
12 * SECTION:libudev-hwdb
13 * @short_description: retrieve properties from the hardware database
14 *
f2d433e1 15 * Libudev hardware database interface.
2001208c
KS
16 */
17
18/**
19 * udev_hwdb:
20 *
21 * Opaque object representing the hardware database.
22 */
23struct udev_hwdb {
3c6ac219 24 unsigned n_ref;
8b516fde 25 sd_hwdb *hwdb;
2001208c
KS
26 struct udev_list properties_list;
27};
28
2001208c
KS
29/**
30 * udev_hwdb_new:
b485aa58 31 * @udev: udev library context (unused)
2001208c
KS
32 *
33 * Create a hardware database context to query properties for devices.
34 *
35 * Returns: a hwdb context.
36 **/
37_public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) {
4afd3348 38 _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb_internal = NULL;
2001208c 39 struct udev_hwdb *hwdb;
8b516fde 40 int r;
2001208c 41
8b516fde 42 r = sd_hwdb_new(&hwdb_internal);
309f631d
LP
43 if (r < 0) {
44 errno = -r;
2001208c 45 return NULL;
309f631d 46 }
2001208c 47
68b80b85 48 hwdb = new(struct udev_hwdb, 1);
309f631d
LP
49 if (!hwdb) {
50 errno = ENOMEM;
2001208c 51 return NULL;
309f631d 52 }
2001208c 53
68b80b85
YW
54 *hwdb = (struct udev_hwdb) {
55 .n_ref = 1,
56 .hwdb = TAKE_PTR(hwdb_internal),
57 };
2001208c 58
8b516fde 59 udev_list_init(udev, &hwdb->properties_list, true);
2001208c 60
2001208c
KS
61 return hwdb;
62}
63
3c6ac219
YW
64static struct udev_hwdb *udev_hwdb_free(struct udev_hwdb *hwdb) {
65 assert(hwdb);
66
67 sd_hwdb_unref(hwdb->hwdb);
68 udev_list_cleanup(&hwdb->properties_list);
69 return mfree(hwdb);
70}
71
2001208c
KS
72/**
73 * udev_hwdb_ref:
74 * @hwdb: context
75 *
76 * Take a reference of a hwdb context.
77 *
78 * Returns: the passed enumeration context
79 **/
2001208c
KS
80
81/**
82 * udev_hwdb_unref:
83 * @hwdb: context
84 *
85 * Drop a reference of a hwdb context. If the refcount reaches zero,
86 * all resources of the hwdb context will be released.
87 *
725d7e6c 88 * Returns: #NULL
2001208c 89 **/
3c6ac219 90DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(struct udev_hwdb, udev_hwdb, udev_hwdb_free);
2001208c 91
2001208c
KS
92/**
93 * udev_hwdb_get_properties_list_entry:
94 * @hwdb: context
95 * @modalias: modalias string
96 * @flags: (unused)
97 *
98 * Lookup a matching device in the hardware database. The lookup key is a
99 * modalias string, whose formats are defined for the Linux kernel modules.
100 * Examples are: pci:v00008086d00001C2D*, usb:v04F2pB221*. The first entry
101 * of a list of retrieved properties is returned.
102 *
103 * Returns: a udev_list_entry.
104 */
14cb109d 105_public_ struct udev_list_entry *udev_hwdb_get_properties_list_entry(struct udev_hwdb *hwdb, const char *modalias, unsigned flags) {
8b516fde 106 const char *key, *value;
309f631d 107 struct udev_list_entry *e;
2001208c 108
2b19953a
YW
109 assert_return_errno(hwdb, NULL, EINVAL);
110 assert_return_errno(modalias, NULL, EINVAL);
2001208c 111
9485d98d 112 udev_list_cleanup(&hwdb->properties_list);
8b516fde 113
0b931571 114 SD_HWDB_FOREACH_PROPERTY(hwdb->hwdb, modalias, key, value) {
f921b457 115 if (!udev_list_entry_add(&hwdb->properties_list, key, value)) {
8b516fde
TG
116 errno = ENOMEM;
117 return NULL;
118 }
2001208c 119 }
8b516fde 120
309f631d
LP
121 e = udev_list_get_entry(&hwdb->properties_list);
122 if (!e)
123 errno = ENODATA;
124
125 return e;
2001208c 126}