]>
Commit | Line | Data |
---|---|---|
98b19252 AS |
1 | /* |
2 | * Virtio Console and Generic Serial Port Devices | |
3 | * | |
4 | * Copyright Red Hat, Inc. 2009 | |
5 | * | |
6 | * Authors: | |
7 | * Amit Shah <[email protected]> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
10 | * the COPYING file in the top-level directory. | |
11 | */ | |
12 | ||
13 | #include "qemu-char.h" | |
14 | #include "virtio-serial.h" | |
15 | ||
16 | typedef struct VirtConsole { | |
17 | VirtIOSerialPort port; | |
18 | CharDriverState *chr; | |
19 | } VirtConsole; | |
20 | ||
21 | ||
22 | /* Callback function that's called when the guest sends us data */ | |
23 | static size_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len) | |
24 | { | |
25 | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); | |
26 | ssize_t ret; | |
27 | ||
28 | ret = qemu_chr_write(vcon->chr, buf, len); | |
29 | ||
30 | return ret < 0 ? 0 : ret; | |
31 | } | |
32 | ||
33 | /* Readiness of the guest to accept data on a port */ | |
34 | static int chr_can_read(void *opaque) | |
35 | { | |
36 | VirtConsole *vcon = opaque; | |
37 | ||
38 | return virtio_serial_guest_ready(&vcon->port); | |
39 | } | |
40 | ||
41 | /* Send data from a char device over to the guest */ | |
42 | static void chr_read(void *opaque, const uint8_t *buf, int size) | |
43 | { | |
44 | VirtConsole *vcon = opaque; | |
45 | ||
46 | virtio_serial_write(&vcon->port, buf, size); | |
47 | } | |
48 | ||
49 | static void chr_event(void *opaque, int event) | |
50 | { | |
51 | VirtConsole *vcon = opaque; | |
52 | ||
53 | switch (event) { | |
54 | case CHR_EVENT_OPENED: { | |
55 | virtio_serial_open(&vcon->port); | |
56 | break; | |
57 | } | |
58 | case CHR_EVENT_CLOSED: | |
59 | virtio_serial_close(&vcon->port); | |
60 | break; | |
61 | } | |
62 | } | |
63 | ||
64 | /* Virtio Console Ports */ | |
65 | static int virtconsole_initfn(VirtIOSerialDevice *dev) | |
66 | { | |
67 | VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev); | |
68 | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); | |
69 | ||
70 | port->info = dev->info; | |
71 | ||
72 | port->is_console = true; | |
73 | ||
74 | if (vcon->chr) { | |
75 | qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event, | |
76 | vcon); | |
77 | port->info->have_data = flush_buf; | |
78 | } | |
79 | return 0; | |
80 | } | |
81 | ||
82 | static int virtconsole_exitfn(VirtIOSerialDevice *dev) | |
83 | { | |
84 | VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev); | |
85 | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); | |
86 | ||
87 | if (vcon->chr) { | |
88 | port->info->have_data = NULL; | |
89 | qemu_chr_close(vcon->chr); | |
90 | } | |
91 | ||
92 | return 0; | |
93 | } | |
94 | ||
95 | static VirtIOSerialPortInfo virtconsole_info = { | |
96 | .qdev.name = "virtconsole", | |
97 | .qdev.size = sizeof(VirtConsole), | |
98 | .init = virtconsole_initfn, | |
99 | .exit = virtconsole_exitfn, | |
100 | .qdev.props = (Property[]) { | |
101 | DEFINE_PROP_UINT8("is_console", VirtConsole, port.is_console, 1), | |
102 | DEFINE_PROP_CHR("chardev", VirtConsole, chr), | |
160600fd | 103 | DEFINE_PROP_STRING("name", VirtConsole, port.name), |
98b19252 AS |
104 | DEFINE_PROP_END_OF_LIST(), |
105 | }, | |
106 | }; | |
107 | ||
108 | static void virtconsole_register(void) | |
109 | { | |
110 | virtio_serial_port_qdev_register(&virtconsole_info); | |
111 | } | |
112 | device_init(virtconsole_register) |