Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 */