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.
15 #include "qemu/osdep.h"
17 #include "qemu/thread.h"
18 #include "qemu/error-report.h"
20 #include "hw/s390x/sclp.h"
21 #include "hw/s390x/event-facility.h"
22 #include "sysemu/char.h"
24 typedef struct ASCIIConsoleData
{
25 EventBufferHeader ebh
;
27 } QEMU_PACKED ASCIIConsoleData
;
29 /* max size for ASCII data in 4K SCCB page */
30 #define SIZE_BUFFER_VT220 4080
32 typedef struct SCLPConsole
{
35 uint8_t iov
[SIZE_BUFFER_VT220
];
36 uint32_t iov_sclp
; /* offset in buf for SCLP read operation */
37 uint32_t iov_bs
; /* offset in buf for char layer read operation */
38 uint32_t iov_data_len
; /* length of byte stream in buffer */
39 uint32_t iov_sclp_rest
; /* length of byte stream not read via SCLP */
40 bool notify
; /* qemu_notify_event() req'd if true */
43 /* character layer call-back functions */
45 /* Return number of bytes that fit into iov buffer */
46 static int chr_can_read(void *opaque
)
48 SCLPConsole
*scon
= opaque
;
49 int avail
= SIZE_BUFFER_VT220
- scon
->iov_data_len
;
57 /* Send data from a char device over to the guest */
58 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
60 SCLPConsole
*scon
= opaque
;
63 /* read data must fit into current buffer */
64 assert(size
<= SIZE_BUFFER_VT220
- scon
->iov_data_len
);
66 /* put byte-stream from character layer into buffer */
67 memcpy(&scon
->iov
[scon
->iov_bs
], buf
, size
);
68 scon
->iov_data_len
+= size
;
69 scon
->iov_sclp_rest
+= size
;
71 scon
->event
.event_pending
= true;
72 sclp_service_interrupt(0);
75 /* functions to be called by event facility */
77 static bool can_handle_event(uint8_t type
)
79 return type
== SCLP_EVENT_ASCII_CONSOLE_DATA
;
82 static unsigned int send_mask(void)
84 return SCLP_EVENT_MASK_MSG_ASCII
;
87 static unsigned int receive_mask(void)
89 return SCLP_EVENT_MASK_MSG_ASCII
;
92 /* triggered by SCLP's read_event_data -
93 * copy console data byte-stream into provided (SCLP) buffer
95 static void get_console_data(SCLPEvent
*event
, uint8_t *buf
, size_t *size
,
98 SCLPConsole
*cons
= DO_UPCAST(SCLPConsole
, event
, event
);
100 /* first byte is hex 0 saying an ascii string follows */
103 /* if all data fit into provided SCLP buffer */
104 if (avail
>= cons
->iov_sclp_rest
) {
105 /* copy character byte-stream to SCLP buffer */
106 memcpy(buf
, &cons
->iov
[cons
->iov_sclp
], cons
->iov_sclp_rest
);
107 *size
= cons
->iov_sclp_rest
+ 1;
110 cons
->iov_data_len
= 0;
111 cons
->iov_sclp_rest
= 0;
112 event
->event_pending
= false;
113 /* data provided and no more data pending */
115 /* if provided buffer is too small, just copy part */
116 memcpy(buf
, &cons
->iov
[cons
->iov_sclp
], avail
);
118 cons
->iov_sclp_rest
-= avail
;
119 cons
->iov_sclp
+= avail
;
120 /* more data pending */
123 cons
->notify
= false;
128 static int read_event_data(SCLPEvent
*event
, EventBufferHeader
*evt_buf_hdr
,
134 ASCIIConsoleData
*acd
= (ASCIIConsoleData
*) evt_buf_hdr
;
136 if (!event
->event_pending
) {
137 /* no data pending */
141 to
= (uint8_t *)&acd
->data
;
142 avail
= *slen
- sizeof(ASCIIConsoleData
);
143 get_console_data(event
, to
, &src_len
, avail
);
145 acd
->ebh
.length
= cpu_to_be16(sizeof(ASCIIConsoleData
) + src_len
);
146 acd
->ebh
.type
= SCLP_EVENT_ASCII_CONSOLE_DATA
;
147 acd
->ebh
.flags
|= SCLP_EVENT_BUFFER_ACCEPTED
;
148 *slen
= avail
- src_len
;
153 /* triggered by SCLP's write_event_data
154 * - write console data to character layer
155 * returns < 0 if an error occurred
157 static ssize_t
write_console_data(SCLPEvent
*event
, const uint8_t *buf
,
160 SCLPConsole
*scon
= DO_UPCAST(SCLPConsole
, event
, event
);
163 /* If there's no backend, we can just say we consumed all data. */
167 return qemu_chr_fe_write_all(scon
->chr
, buf
, len
);
170 static int write_event_data(SCLPEvent
*event
, EventBufferHeader
*evt_buf_hdr
)
175 ASCIIConsoleData
*acd
= (ASCIIConsoleData
*) evt_buf_hdr
;
177 length
= be16_to_cpu(evt_buf_hdr
->length
) - sizeof(EventBufferHeader
);
178 written
= write_console_data(event
, (uint8_t *)acd
->data
, length
);
180 rc
= SCLP_RC_NORMAL_COMPLETION
;
181 /* set event buffer accepted flag */
182 evt_buf_hdr
->flags
|= SCLP_EVENT_BUFFER_ACCEPTED
;
184 /* written will be zero if a pty is not connected - don't treat as error */
186 /* event buffer not accepted due to error in character layer */
187 evt_buf_hdr
->flags
&= ~(SCLP_EVENT_BUFFER_ACCEPTED
);
188 rc
= SCLP_RC_CONTAINED_EQUIPMENT_CHECK
;
194 static const VMStateDescription vmstate_sclpconsole
= {
195 .name
= "sclpconsole",
197 .minimum_version_id
= 0,
198 .fields
= (VMStateField
[]) {
199 VMSTATE_BOOL(event
.event_pending
, SCLPConsole
),
200 VMSTATE_UINT8_ARRAY(iov
, SCLPConsole
, SIZE_BUFFER_VT220
),
201 VMSTATE_UINT32(iov_sclp
, SCLPConsole
),
202 VMSTATE_UINT32(iov_bs
, SCLPConsole
),
203 VMSTATE_UINT32(iov_data_len
, SCLPConsole
),
204 VMSTATE_UINT32(iov_sclp_rest
, SCLPConsole
),
205 VMSTATE_END_OF_LIST()
209 /* qemu object creation and initialization functions */
211 /* tell character layer our call-back functions */
213 static int console_init(SCLPEvent
*event
)
215 static bool console_available
;
217 SCLPConsole
*scon
= DO_UPCAST(SCLPConsole
, event
, event
);
219 if (console_available
) {
220 error_report("Multiple VT220 operator consoles are not supported");
223 console_available
= true;
225 qemu_chr_add_handlers(scon
->chr
, chr_can_read
,
226 chr_read
, NULL
, scon
);
232 static void console_reset(DeviceState
*dev
)
234 SCLPEvent
*event
= SCLP_EVENT(dev
);
235 SCLPConsole
*scon
= DO_UPCAST(SCLPConsole
, event
, event
);
237 event
->event_pending
= false;
240 scon
->iov_data_len
= 0;
241 scon
->iov_sclp_rest
= 0;
242 scon
->notify
= false;
245 static int console_exit(SCLPEvent
*event
)
250 static Property console_properties
[] = {
251 DEFINE_PROP_CHR("chardev", SCLPConsole
, chr
),
252 DEFINE_PROP_END_OF_LIST(),
255 static void console_class_init(ObjectClass
*klass
, void *data
)
257 DeviceClass
*dc
= DEVICE_CLASS(klass
);
258 SCLPEventClass
*ec
= SCLP_EVENT_CLASS(klass
);
260 dc
->props
= console_properties
;
261 dc
->reset
= console_reset
;
262 dc
->vmsd
= &vmstate_sclpconsole
;
263 ec
->init
= console_init
;
264 ec
->exit
= console_exit
;
265 ec
->get_send_mask
= send_mask
;
266 ec
->get_receive_mask
= receive_mask
;
267 ec
->can_handle_event
= can_handle_event
;
268 ec
->read_event_data
= read_event_data
;
269 ec
->write_event_data
= write_event_data
;
270 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
273 static const TypeInfo sclp_console_info
= {
274 .name
= "sclpconsole",
275 .parent
= TYPE_SCLP_EVENT
,
276 .instance_size
= sizeof(SCLPConsole
),
277 .class_init
= console_class_init
,
278 .class_size
= sizeof(SCLPEventClass
),
281 static void register_types(void)
283 type_register_static(&sclp_console_info
);
286 type_init(register_types
)