Skip to content

Commit

Permalink
char: move callbacks in CharDriver
Browse files Browse the repository at this point in the history
This makes the code more declarative, and avoids duplicating the
information on all instances.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
  • Loading branch information
elmarco authored and bonzini committed Jan 27, 2017
1 parent a1698bf commit b68e956
Show file tree
Hide file tree
Showing 10 changed files with 381 additions and 260 deletions.
11 changes: 6 additions & 5 deletions backends/baum.c
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,8 @@ static void baum_free(struct CharDriverState *chr)
g_free(baum);
}

static CharDriverState *chr_baum_init(const char *id,
static CharDriverState *chr_baum_init(const CharDriver *driver,
const char *id,
ChardevBackend *backend,
ChardevReturn *ret,
bool *be_opened,
Expand All @@ -633,17 +634,14 @@ static CharDriverState *chr_baum_init(const char *id,
CharDriverState *chr;
brlapi_handle_t *handle;

chr = qemu_chr_alloc(common, errp);
chr = qemu_chr_alloc(driver, common, errp);
if (!chr) {
return NULL;
}
baum = g_malloc0(sizeof(BaumDriverState));
baum->chr = chr;

chr->opaque = baum;
chr->chr_write = baum_write;
chr->chr_accept_input = baum_accept_input;
chr->chr_free = baum_free;

handle = g_malloc0(brlapi_getHandleSize());
baum->brlapi = handle;
Expand Down Expand Up @@ -674,6 +672,9 @@ static void register_types(void)
static const CharDriver driver = {
.kind = CHARDEV_BACKEND_KIND_BRAILLE,
.create = chr_baum_init,
.chr_write = baum_write,
.chr_accept_input = baum_accept_input,
.chr_free = baum_free,
};

register_char_driver(&driver);
Expand Down
11 changes: 6 additions & 5 deletions backends/msmouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ static QemuInputHandler msmouse_handler = {
.sync = msmouse_input_sync,
};

static CharDriverState *qemu_chr_open_msmouse(const char *id,
static CharDriverState *qemu_chr_open_msmouse(const CharDriver *driver,
const char *id,
ChardevBackend *backend,
ChardevReturn *ret,
bool *be_opened,
Expand All @@ -158,13 +159,10 @@ static CharDriverState *qemu_chr_open_msmouse(const char *id,
MouseState *mouse;
CharDriverState *chr;

chr = qemu_chr_alloc(common, errp);
chr = qemu_chr_alloc(driver, common, errp);
if (!chr) {
return NULL;
}
chr->chr_write = msmouse_chr_write;
chr->chr_free = msmouse_chr_free;
chr->chr_accept_input = msmouse_chr_accept_input;
*be_opened = false;

mouse = g_new0(MouseState, 1);
Expand All @@ -182,6 +180,9 @@ static void register_types(void)
static const CharDriver driver = {
.kind = CHARDEV_BACKEND_KIND_MSMOUSE,
.create = qemu_chr_open_msmouse,
.chr_write = msmouse_chr_write,
.chr_accept_input = msmouse_chr_accept_input,
.chr_free = msmouse_chr_free,
};
register_char_driver(&driver);
}
Expand Down
8 changes: 5 additions & 3 deletions backends/testdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ static void testdev_free(struct CharDriverState *chr)
g_free(testdev);
}

static CharDriverState *chr_testdev_init(const char *id,
static CharDriverState *chr_testdev_init(const CharDriver *driver,
const char *id,
ChardevBackend *backend,
ChardevReturn *ret,
bool *be_opened,
Expand All @@ -121,9 +122,8 @@ static CharDriverState *chr_testdev_init(const char *id,
testdev = g_new0(TestdevCharState, 1);
testdev->chr = chr = g_new0(CharDriverState, 1);

chr->driver = driver;
chr->opaque = testdev;
chr->chr_write = testdev_write;
chr->chr_free = testdev_free;

return chr;
}
Expand All @@ -133,6 +133,8 @@ static void register_types(void)
static const CharDriver driver = {
.kind = CHARDEV_BACKEND_KIND_TESTDEV,
.create = chr_testdev_init,
.chr_write = testdev_write,
.chr_free = testdev_free,
};
register_char_driver(&driver);
}
Expand Down
7 changes: 5 additions & 2 deletions gdbstub.c
Original file line number Diff line number Diff line change
Expand Up @@ -1732,6 +1732,10 @@ int gdbserver_start(const char *device)
CharDriverState *chr = NULL;
CharDriverState *mon_chr;
ChardevCommon common = { 0 };
static const CharDriver driver = {
.kind = -1,
.chr_write = gdb_monitor_write,
};

if (!first_cpu) {
error_report("gdbstub: meaningless to attach gdb to a "
Expand Down Expand Up @@ -1770,8 +1774,7 @@ int gdbserver_start(const char *device)
qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);

/* Initialize a monitor terminal for gdb */
mon_chr = qemu_chr_alloc(&common, &error_abort);
mon_chr->chr_write = gdb_monitor_write;
mon_chr = qemu_chr_alloc(&driver, &common, &error_abort);
monitor_init(mon_chr, 0);
} else {
if (qemu_chr_fe_get_driver(&s->chr)) {
Expand Down
8 changes: 6 additions & 2 deletions hw/bt/hci-csr.c
Original file line number Diff line number Diff line change
Expand Up @@ -462,12 +462,16 @@ qemu_irq *csrhci_pins_get(CharDriverState *chr)

CharDriverState *uart_hci_init(void)
{
static const CharDriver hci_driver = {
.kind = -1,
.chr_write = csrhci_write,
.chr_ioctl = csrhci_ioctl,
};
struct csrhci_s *s = (struct csrhci_s *)
g_malloc0(sizeof(struct csrhci_s));

s->chr.opaque = s;
s->chr.chr_write = csrhci_write;
s->chr.chr_ioctl = csrhci_ioctl;
s->chr.driver = &hci_driver;

s->hci = qemu_next_hci();
s->hci->opaque = s;
Expand Down
46 changes: 26 additions & 20 deletions include/sysemu/char.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,11 @@ typedef struct CharBackend {
int fe_open;
} CharBackend;

typedef struct CharDriver CharDriver;

struct CharDriverState {
const CharDriver *driver;
QemuMutex chr_write_lock;
int (*chr_write)(struct CharDriverState *s, const uint8_t *buf, int len);
int (*chr_sync_read)(struct CharDriverState *s,
const uint8_t *buf, int len);
GSource *(*chr_add_watch)(struct CharDriverState *s, GIOCondition cond);
void (*chr_update_read_handler)(struct CharDriverState *s,
GMainContext *context);
int (*chr_ioctl)(struct CharDriverState *s, int cmd, void *arg);
int (*get_msgfds)(struct CharDriverState *s, int* fds, int num);
int (*set_msgfds)(struct CharDriverState *s, int *fds, int num);
int (*chr_add_client)(struct CharDriverState *chr, int fd);
int (*chr_wait_connected)(struct CharDriverState *chr, Error **errp);
void (*chr_free)(struct CharDriverState *chr);
void (*chr_disconnect)(struct CharDriverState *chr);
void (*chr_accept_input)(struct CharDriverState *chr);
void (*chr_set_echo)(struct CharDriverState *chr, bool echo);
void (*chr_set_fe_open)(struct CharDriverState *chr, int fe_open);
CharBackend *be;
void *opaque;
char *label;
Expand All @@ -125,7 +112,8 @@ struct CharDriverState {
*
* Returns: a newly allocated CharDriverState, or NULL on error.
*/
CharDriverState *qemu_chr_alloc(ChardevCommon *backend, Error **errp);
CharDriverState *qemu_chr_alloc(const CharDriver *driver,
ChardevCommon *backend, Error **errp);

/**
* @qemu_chr_new_from_opts:
Expand Down Expand Up @@ -475,15 +463,33 @@ void qemu_chr_set_feature(CharDriverState *chr,
CharDriverFeature feature);
QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename);

typedef struct CharDriver {
struct CharDriver {
ChardevBackendKind kind;
const char *alias;
void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
CharDriverState *(*create)(const char *id,
CharDriverState *(*create)(const CharDriver *driver,
const char *id,
ChardevBackend *backend,
ChardevReturn *ret, bool *be_opened,
Error **errp);
} CharDriver;

int (*chr_write)(struct CharDriverState *s, const uint8_t *buf, int len);
int (*chr_sync_read)(struct CharDriverState *s,
const uint8_t *buf, int len);
GSource *(*chr_add_watch)(struct CharDriverState *s, GIOCondition cond);
void (*chr_update_read_handler)(struct CharDriverState *s,
GMainContext *context);
int (*chr_ioctl)(struct CharDriverState *s, int cmd, void *arg);
int (*get_msgfds)(struct CharDriverState *s, int* fds, int num);
int (*set_msgfds)(struct CharDriverState *s, int *fds, int num);
int (*chr_add_client)(struct CharDriverState *chr, int fd);
int (*chr_wait_connected)(struct CharDriverState *chr, Error **errp);
void (*chr_free)(struct CharDriverState *chr);
void (*chr_disconnect)(struct CharDriverState *chr);
void (*chr_accept_input)(struct CharDriverState *chr);
void (*chr_set_echo)(struct CharDriverState *chr, bool echo);
void (*chr_set_fe_open)(struct CharDriverState *chr, int fe_open);
};

void register_char_driver(const CharDriver *driver);

Expand Down
Loading

0 comments on commit b68e956

Please sign in to comment.