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"
21 typedef struct ASCIIConsoleData
{
22 EventBufferHeader ebh
;
24 } QEMU_PACKED ASCIIConsoleData
;
26 /* max size for ASCII data in 4K SCCB page */
27 #define SIZE_BUFFER_VT220 4080
29 typedef struct SCLPConsole
{
33 uint8_t *iov
; /* iov buffer pointer */
34 uint8_t *iov_sclp
; /* pointer to SCLP read offset */
35 uint8_t *iov_bs
; /* pointer byte stream read offset */
36 uint32_t iov_data_len
; /* length of byte stream in buffer */
37 uint32_t iov_sclp_rest
; /* length of byte stream not read via SCLP */
38 qemu_irq irq_read_vt220
;
41 /* character layer call-back functions */
43 /* Return number of bytes that fit into iov buffer */
44 static int chr_can_read(void *opaque
)
47 SCLPConsole
*scon
= opaque
;
49 can_read
= SIZE_BUFFER_VT220
- scon
->iov_data_len
;
54 /* Receive n bytes from character layer, save in iov buffer,
55 * and set event pending */
56 static void receive_from_chr_layer(SCLPConsole
*scon
, const uint8_t *buf
,
61 /* read data must fit into current buffer */
62 assert(size
<= SIZE_BUFFER_VT220
- scon
->iov_data_len
);
64 /* put byte-stream from character layer into buffer */
65 memcpy(scon
->iov_bs
, buf
, size
);
66 scon
->iov_data_len
+= size
;
67 scon
->iov_sclp_rest
+= size
;
69 scon
->event
.event_pending
= true;
72 /* Send data from a char device over to the guest */
73 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
75 SCLPConsole
*scon
= opaque
;
79 receive_from_chr_layer(scon
, buf
, size
);
80 /* trigger SCLP read operation */
81 qemu_irq_raise(scon
->irq_read_vt220
);
84 static void chr_event(void *opaque
, int event
)
86 SCLPConsole
*scon
= opaque
;
89 case CHR_EVENT_OPENED
:
91 scon
->iov
= g_malloc0(SIZE_BUFFER_VT220
);
92 scon
->iov_sclp
= scon
->iov
;
93 scon
->iov_bs
= scon
->iov
;
94 scon
->iov_data_len
= 0;
95 scon
->iov_sclp_rest
= 0;
98 case CHR_EVENT_CLOSED
:
107 /* functions to be called by event facility */
109 static int event_type(void)
111 return SCLP_EVENT_ASCII_CONSOLE_DATA
;
114 static unsigned int send_mask(void)
116 return SCLP_EVENT_MASK_MSG_ASCII
;
119 static unsigned int receive_mask(void)
121 return SCLP_EVENT_MASK_MSG_ASCII
;
124 /* triggered by SCLP's read_event_data -
125 * copy console data byte-stream into provided (SCLP) buffer
127 static void get_console_data(SCLPEvent
*event
, uint8_t *buf
, size_t *size
,
130 SCLPConsole
*cons
= DO_UPCAST(SCLPConsole
, event
, event
);
132 /* first byte is hex 0 saying an ascii string follows */
135 /* if all data fit into provided SCLP buffer */
136 if (avail
>= cons
->iov_sclp_rest
) {
137 /* copy character byte-stream to SCLP buffer */
138 memcpy(buf
, cons
->iov_sclp
, cons
->iov_sclp_rest
);
139 *size
= cons
->iov_sclp_rest
+ 1;
140 cons
->iov_sclp
= cons
->iov
;
141 cons
->iov_bs
= cons
->iov
;
142 cons
->iov_data_len
= 0;
143 cons
->iov_sclp_rest
= 0;
144 event
->event_pending
= false;
145 /* data provided and no more data pending */
147 /* if provided buffer is too small, just copy part */
148 memcpy(buf
, cons
->iov_sclp
, avail
);
150 cons
->iov_sclp_rest
-= avail
;
151 cons
->iov_sclp
+= avail
;
152 /* more data pending */
156 static int read_event_data(SCLPEvent
*event
, EventBufferHeader
*evt_buf_hdr
,
162 ASCIIConsoleData
*acd
= (ASCIIConsoleData
*) evt_buf_hdr
;
164 if (!event
->event_pending
) {
165 /* no data pending */
169 to
= (uint8_t *)&acd
->data
;
170 avail
= *slen
- sizeof(ASCIIConsoleData
);
171 get_console_data(event
, to
, &src_len
, avail
);
173 acd
->ebh
.length
= cpu_to_be16(sizeof(ASCIIConsoleData
) + src_len
);
174 acd
->ebh
.type
= SCLP_EVENT_ASCII_CONSOLE_DATA
;
175 acd
->ebh
.flags
|= SCLP_EVENT_BUFFER_ACCEPTED
;
176 *slen
= avail
- src_len
;
181 /* triggered by SCLP's write_event_data
182 * - write console data into character layer
183 * returns < 0 if an error occured
185 static ssize_t
write_console_data(SCLPEvent
*event
, const uint8_t *buf
,
189 const uint8_t *iov_offset
;
190 SCLPConsole
*scon
= DO_UPCAST(SCLPConsole
, event
, event
);
193 /* If there's no backend, we can just say we consumed all data. */
199 ret
= qemu_chr_fe_write(scon
->chr
, buf
, len
);
201 /* a pty doesn't seem to be connected - no error */
203 } else if (ret
== -EAGAIN
|| (ret
> 0 && ret
< len
)) {
214 static int write_event_data(SCLPEvent
*event
, EventBufferHeader
*evt_buf_hdr
)
219 ASCIIConsoleData
*acd
= (ASCIIConsoleData
*) evt_buf_hdr
;
221 length
= be16_to_cpu(evt_buf_hdr
->length
) - sizeof(EventBufferHeader
);
222 written
= write_console_data(event
, (uint8_t *)acd
->data
, length
);
224 rc
= SCLP_RC_NORMAL_COMPLETION
;
225 /* set event buffer accepted flag */
226 evt_buf_hdr
->flags
|= SCLP_EVENT_BUFFER_ACCEPTED
;
228 /* written will be zero if a pty is not connected - don't treat as error */
230 /* event buffer not accepted due to error in character layer */
231 evt_buf_hdr
->flags
&= ~(SCLP_EVENT_BUFFER_ACCEPTED
);
232 rc
= SCLP_RC_CONTAINED_EQUIPMENT_CHECK
;
238 static void trigger_ascii_console_data(void *env
, int n
, int level
)
240 sclp_service_interrupt(0);
243 /* qemu object creation and initialization functions */
245 /* tell character layer our call-back functions */
246 static int console_init(SCLPEvent
*event
)
248 static bool console_available
;
250 SCLPConsole
*scon
= DO_UPCAST(SCLPConsole
, event
, event
);
252 if (console_available
) {
253 error_report("Multiple VT220 operator consoles are not supported");
256 console_available
= true;
257 event
->event_type
= SCLP_EVENT_ASCII_CONSOLE_DATA
;
259 qemu_chr_add_handlers(scon
->chr
, chr_can_read
,
260 chr_read
, chr_event
, scon
);
262 scon
->irq_read_vt220
= *qemu_allocate_irqs(trigger_ascii_console_data
,
268 static int console_exit(SCLPEvent
*event
)
273 static Property console_properties
[] = {
274 DEFINE_PROP_CHR("chardev", SCLPConsole
, chr
),
275 DEFINE_PROP_END_OF_LIST(),
278 static void console_class_init(ObjectClass
*klass
, void *data
)
280 DeviceClass
*dc
= DEVICE_CLASS(klass
);
281 SCLPEventClass
*ec
= SCLP_EVENT_CLASS(klass
);
283 dc
->props
= console_properties
;
284 ec
->init
= console_init
;
285 ec
->exit
= console_exit
;
286 ec
->get_send_mask
= send_mask
;
287 ec
->get_receive_mask
= receive_mask
;
288 ec
->event_type
= event_type
;
289 ec
->read_event_data
= read_event_data
;
290 ec
->write_event_data
= write_event_data
;
293 static TypeInfo sclp_console_info
= {
294 .name
= "sclpconsole",
295 .parent
= TYPE_SCLP_EVENT
,
296 .instance_size
= sizeof(SCLPConsole
),
297 .class_init
= console_class_init
,
298 .class_size
= sizeof(SCLPEventClass
),
301 static void register_types(void)
303 type_register_static(&sclp_console_info
);
306 type_init(register_types
)