3 * Ascii Console Data (VT220 Console)
5 * Copyright IBM, Corp. 2012
8 * Heinz Graalfs <graalfs@de.ibm.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or (at your
11 * option) any later version. See the COPYING file in the top-level directory.
16 #include "qemu/thread.h"
19 #include "event-facility.h"
20 #include "qemu-char.h"
22 typedef struct ASCIIConsoleData {
23 EventBufferHeader ebh;
25 } QEMU_PACKED ASCIIConsoleData;
27 /* max size for ASCII data in 4K SCCB page */
28 #define SIZE_BUFFER_VT220 4080
30 typedef struct SCLPConsole {
34 uint8_t *iov; /* iov buffer pointer */
35 uint8_t *iov_sclp; /* pointer to SCLP read offset */
36 uint8_t *iov_bs; /* pointer byte stream read offset */
37 uint32_t iov_data_len; /* length of byte stream in buffer */
38 uint32_t iov_sclp_rest; /* length of byte stream not read via SCLP */
39 qemu_irq irq_read_vt220;
42 /* character layer call-back functions */
44 /* Return number of bytes that fit into iov buffer */
45 static int chr_can_read(void *opaque)
48 SCLPConsole *scon = opaque;
50 can_read = SIZE_BUFFER_VT220 - scon->iov_data_len;
55 /* Receive n bytes from character layer, save in iov buffer,
56 * and set event pending */
57 static void receive_from_chr_layer(SCLPConsole *scon, const uint8_t *buf,
62 /* read data must fit into current buffer */
63 assert(size <= SIZE_BUFFER_VT220 - scon->iov_data_len);
65 /* put byte-stream from character layer into buffer */
66 memcpy(scon->iov_bs, buf, size);
67 scon->iov_data_len += size;
68 scon->iov_sclp_rest += size;
70 scon->event.event_pending = true;
73 /* Send data from a char device over to the guest */
74 static void chr_read(void *opaque, const uint8_t *buf, int size)
76 SCLPConsole *scon = opaque;
80 receive_from_chr_layer(scon, buf, size);
81 /* trigger SCLP read operation */
82 qemu_irq_raise(scon->irq_read_vt220);
85 static void chr_event(void *opaque, int event)
87 SCLPConsole *scon = opaque;
90 case CHR_EVENT_OPENED:
92 scon->iov = g_malloc0(SIZE_BUFFER_VT220);
93 scon->iov_sclp = scon->iov;
94 scon->iov_bs = scon->iov;
95 scon->iov_data_len = 0;
96 scon->iov_sclp_rest = 0;
99 case CHR_EVENT_CLOSED:
108 /* functions to be called by event facility */
110 static int event_type(void)
112 return SCLP_EVENT_ASCII_CONSOLE_DATA;
115 static unsigned int send_mask(void)
117 return SCLP_EVENT_MASK_MSG_ASCII;
120 static unsigned int receive_mask(void)
122 return SCLP_EVENT_MASK_MSG_ASCII;
125 /* triggered by SCLP's read_event_data -
126 * copy console data byte-stream into provided (SCLP) buffer
128 static void get_console_data(SCLPEvent *event, uint8_t *buf, size_t *size,
131 SCLPConsole *cons = DO_UPCAST(SCLPConsole, event, event);
133 /* first byte is hex 0 saying an ascii string follows */
136 /* if all data fit into provided SCLP buffer */
137 if (avail >= cons->iov_sclp_rest) {
138 /* copy character byte-stream to SCLP buffer */
139 memcpy(buf, cons->iov_sclp, cons->iov_sclp_rest);
140 *size = cons->iov_sclp_rest + 1;
141 cons->iov_sclp = cons->iov;
142 cons->iov_bs = cons->iov;
143 cons->iov_data_len = 0;
144 cons->iov_sclp_rest = 0;
145 event->event_pending = false;
146 /* data provided and no more data pending */
148 /* if provided buffer is too small, just copy part */
149 memcpy(buf, cons->iov_sclp, avail);
151 cons->iov_sclp_rest -= avail;
152 cons->iov_sclp += avail;
153 /* more data pending */
157 static int read_event_data(SCLPEvent *event, EventBufferHeader *evt_buf_hdr,
163 ASCIIConsoleData *acd = (ASCIIConsoleData *) evt_buf_hdr;
165 if (!event->event_pending) {
166 /* no data pending */
170 to = (uint8_t *)&acd->data;
171 avail = *slen - sizeof(ASCIIConsoleData);
172 get_console_data(event, to, &src_len, avail);
174 acd->ebh.length = cpu_to_be16(sizeof(ASCIIConsoleData) + src_len);
175 acd->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
176 acd->ebh.flags |= SCLP_EVENT_BUFFER_ACCEPTED;
177 *slen = avail - src_len;
182 /* triggered by SCLP's write_event_data
183 * - write console data to character layer
184 * returns < 0 if an error occurred
186 static ssize_t write_console_data(SCLPEvent *event, const uint8_t *buf,
190 const uint8_t *iov_offset;
191 SCLPConsole *scon = DO_UPCAST(SCLPConsole, event, event);
194 /* If there's no backend, we can just say we consumed all data. */
200 ret = qemu_chr_fe_write(scon->chr, buf, len);
202 /* a pty doesn't seem to be connected - no error */
204 } else if (ret == -EAGAIN || (ret > 0 && ret < len)) {
215 static int write_event_data(SCLPEvent *event, EventBufferHeader *evt_buf_hdr)
220 ASCIIConsoleData *acd = (ASCIIConsoleData *) evt_buf_hdr;
222 length = be16_to_cpu(evt_buf_hdr->length) - sizeof(EventBufferHeader);
223 written = write_console_data(event, (uint8_t *)acd->data, length);
225 rc = SCLP_RC_NORMAL_COMPLETION;
226 /* set event buffer accepted flag */
227 evt_buf_hdr->flags |= SCLP_EVENT_BUFFER_ACCEPTED;
229 /* written will be zero if a pty is not connected - don't treat as error */
231 /* event buffer not accepted due to error in character layer */
232 evt_buf_hdr->flags &= ~(SCLP_EVENT_BUFFER_ACCEPTED);
233 rc = SCLP_RC_CONTAINED_EQUIPMENT_CHECK;
239 static void trigger_ascii_console_data(void *env, int n, int level)
241 sclp_service_interrupt(0);
244 /* qemu object creation and initialization functions */
246 /* tell character layer our call-back functions */
247 static int console_init(SCLPEvent *event)
249 static bool console_available;
251 SCLPConsole *scon = DO_UPCAST(SCLPConsole, event, event);
253 if (console_available) {
254 error_report("Multiple VT220 operator consoles are not supported");
257 console_available = true;
258 event->event_type = SCLP_EVENT_ASCII_CONSOLE_DATA;
260 qemu_chr_add_handlers(scon->chr, chr_can_read,
261 chr_read, chr_event, scon);
263 scon->irq_read_vt220 = *qemu_allocate_irqs(trigger_ascii_console_data,
269 static int console_exit(SCLPEvent *event)
274 static Property console_properties[] = {
275 DEFINE_PROP_CHR("chardev", SCLPConsole, chr),
276 DEFINE_PROP_END_OF_LIST(),
279 static void console_class_init(ObjectClass *klass, void *data)
281 DeviceClass *dc = DEVICE_CLASS(klass);
282 SCLPEventClass *ec = SCLP_EVENT_CLASS(klass);
284 dc->props = console_properties;
285 ec->init = console_init;
286 ec->exit = console_exit;
287 ec->get_send_mask = send_mask;
288 ec->get_receive_mask = receive_mask;
289 ec->event_type = event_type;
290 ec->read_event_data = read_event_data;
291 ec->write_event_data = write_event_data;
294 static TypeInfo sclp_console_info = {
295 .name = "sclpconsole",
296 .parent = TYPE_SCLP_EVENT,
297 .instance_size = sizeof(SCLPConsole),
298 .class_init = console_class_init,
299 .class_size = sizeof(SCLPEventClass),
302 static void register_types(void)
304 type_register_static(&sclp_console_info);
307 type_init(register_types)