Skip to content

Commit

Permalink
[ot] hw/opentitan: add a function to retrieve a CharDev by its id
Browse files Browse the repository at this point in the history
Signed-off-by: Emmanuel Blot <eblot@rivosinc.com>
  • Loading branch information
rivos-eblot committed Nov 27, 2023
1 parent 8090a1f commit b45f3b8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
35 changes: 35 additions & 0 deletions hw/opentitan/ot_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
13 changes: 13 additions & 0 deletions include/hw/opentitan/ot_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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=<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 */

0 comments on commit b45f3b8

Please sign in to comment.