From b45f3b8133fa8a552e30dc1bb7b66e7dccd1d0e5 Mon Sep 17 00:00:00 2001 From: Emmanuel Blot Date: Fri, 24 Nov 2023 11:44:48 +0100 Subject: [PATCH] [ot] hw/opentitan: add a function to retrieve a CharDev by its id Signed-off-by: Emmanuel Blot --- hw/opentitan/ot_common.c | 35 ++++++++++++++++++++++++++++++++ include/hw/opentitan/ot_common.h | 13 ++++++++++++ 2 files changed, 48 insertions(+) diff --git a/hw/opentitan/ot_common.c b/hw/opentitan/ot_common.c index f0502050f1f1..e86a66cb9599 100644 --- a/hw/opentitan/ot_common.c +++ b/hw/opentitan/ot_common.c @@ -27,6 +27,7 @@ #include "qemu/osdep.h" #include "qom/object.h" +#include "chardev/chardev-internal.h" #include "hw/opentitan/ot_common.h" typedef struct { @@ -107,3 +108,37 @@ void ot_common_ignore_chr_status_lines(CharBackend *chr) tcsetattr(fioc->fd, TCSANOW, &tty); #endif } + +typedef struct { + Chardev *chr; + const char *label; +} OtCommonChrMatch; + +static int ot_common_match_chardev(Object *child, void *opaque) +{ + OtCommonChrMatch *match = opaque; + Chardev *chr = CHARDEV(child); + + if (strcmp(match->label, chr->label) != 0) { + return 0; + } + + match->chr = chr; + return 1; +} + +Chardev *ot_common_get_chardev_by_id(const char *chrid) +{ + OtCommonChrMatch match = { + .chr = NULL, + .label = chrid, + }; + + /* "chardev-internal.h" inclusion is required for get_chardevs_root() */ + if (!object_child_foreach(get_chardevs_root(), &ot_common_match_chardev, + &match)) { + return NULL; + } + + return match.chr; +} diff --git a/include/hw/opentitan/ot_common.h b/include/hw/opentitan/ot_common.h index c81dea8917e3..20af1fbab831 100644 --- a/include/hw/opentitan/ot_common.h +++ b/include/hw/opentitan/ot_common.h @@ -142,6 +142,19 @@ AddressSpace *ot_common_get_local_address_space(DeviceState *s); /* CharDev utilities */ /* ------------------------------------------------------------------------ */ +/** + * Configure a (PTY) char backend to ignore status lines. + * + * @chr the character backend to configure. + */ void ot_common_ignore_chr_status_lines(CharBackend *chr); +/** + * Find a char device by its id, e.g. "-chardev type,id=,...`" + * + * @chrid the id of the char device + * @return the char device if found, @c NULL otherwise. + */ +Chardev *ot_common_get_chardev_by_id(const char *chrid); + #endif /* HW_OPENTITAN_OT_COMMON_H */