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"
17 #include "qemu/error-report.h"
19 #include "hw/s390x/sclp.h"
20 #include "hw/s390x/event-facility.h"
21 #include "sysemu/char.h"
23 typedef struct ASCIIConsoleData
{
24 EventBufferHeader ebh
;
26 } QEMU_PACKED ASCIIConsoleData
;
28 /* max size for ASCII data in 4K SCCB page */
29 #define SIZE_BUFFER_VT220 4080
31 typedef struct SCLPConsole
{
34 uint8_t iov
[SIZE_BUFFER_VT220
];
35 uint32_t iov_sclp
; /* offset in buf for SCLP read operation */
36 uint32_t iov_bs
; /* offset in buf for char layer read operation */
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 */
41 /* character layer call-back functions */
43 /* Return number of bytes that fit into iov buffer */
44 static int chr_can_read(void *opaque
)
46 SCLPConsole
*scon
= opaque
;
48 return SIZE_BUFFER_VT220
- scon
->iov_data_len
;
51 /* Send data from a char device over to the guest */
52 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
54 SCLPConsole
*scon
= opaque
;
57 /* read data must fit into current buffer */
58 assert(size
<= SIZE_BUFFER_VT220
- scon
->iov_data_len
);
60 /* put byte-stream from character layer into buffer */
61 memcpy(&scon
->iov
[scon
->iov_bs
], buf
, size
);
62 scon
->iov_data_len
+= size
;
63 scon
->iov_sclp_rest
+= size
;
65 scon
->event
.event_pending
= true;
66 sclp_service_interrupt(0);
69 /* functions to be called by event facility */
71 static bool can_handle_event(uint8_t type
)
73 return type
== SCLP_EVENT_ASCII_CONSOLE_DATA
;
76 static unsigned int send_mask(void)
78 return SCLP_EVENT_MASK_MSG_ASCII
;
81 static unsigned int receive_mask(void)
83 return SCLP_EVENT_MASK_MSG_ASCII
;
86 /* triggered by SCLP's read_event_data -
87 * copy console data byte-stream into provided (SCLP) buffer
89 static void get_console_data(SCLPEvent
*event
, uint8_t *buf
, size_t *size
,
92 SCLPConsole
*cons
= DO_UPCAST(SCLPConsole
, event
, event
);
94 /* first byte is hex 0 saying an ascii string follows */
97 /* if all data fit into provided SCLP buffer */
98 if (avail
>= cons
->iov_sclp_rest
) {
99 /* copy character byte-stream to SCLP buffer */
100 memcpy(buf
, &cons
->iov
[cons
->iov_sclp
], cons
->iov_sclp_rest
);
101 *size
= cons
->iov_sclp_rest
+ 1;
104 cons
->iov_data_len
= 0;
105 cons
->iov_sclp_rest
= 0;
106 event
->event_pending
= false;
107 /* data provided and no more data pending */
109 /* if provided buffer is too small, just copy part */
110 memcpy(buf
, &cons
->iov
[cons
->iov_sclp
], avail
);
112 cons
->iov_sclp_rest
-= avail
;
113 cons
->iov_sclp
+= avail
;
114 /* more data pending */
118 static int read_event_data(SCLPEvent
*event
, EventBufferHeader
*evt_buf_hdr
,
124 ASCIIConsoleData
*acd
= (ASCIIConsoleData
*) evt_buf_hdr
;
126 if (!event
->event_pending
) {
127 /* no data pending */
131 to
= (uint8_t *)&acd
->data
;
132 avail
= *slen
- sizeof(ASCIIConsoleData
);
133 get_console_data(event
, to
, &src_len
, avail
);
135 acd
->ebh
.length
= cpu_to_be16(sizeof(ASCIIConsoleData
) + src_len
);
136 acd
->ebh
.type
= SCLP_EVENT_ASCII_CONSOLE_DATA
;
137 acd
->ebh
.flags
|= SCLP_EVENT_BUFFER_ACCEPTED
;
138 *slen
= avail
- src_len
;
143 /* triggered by SCLP's write_event_data
144 * - write console data to character layer
145 * returns < 0 if an error occurred
147 static ssize_t
write_console_data(SCLPEvent
*event
, const uint8_t *buf
,
150 SCLPConsole
*scon
= DO_UPCAST(SCLPConsole
, event
, event
);
153 /* If there's no backend, we can just say we consumed all data. */
157 return qemu_chr_fe_write_all(scon
->chr
, buf
, len
);
160 static int write_event_data(SCLPEvent
*event
, EventBufferHeader
*evt_buf_hdr
)
165 ASCIIConsoleData
*acd
= (ASCIIConsoleData
*) evt_buf_hdr
;
167 length
= be16_to_cpu(evt_buf_hdr
->length
) - sizeof(EventBufferHeader
);
168 written
= write_console_data(event
, (uint8_t *)acd
->data
, length
);
170 rc
= SCLP_RC_NORMAL_COMPLETION
;
171 /* set event buffer accepted flag */
172 evt_buf_hdr
->flags
|= SCLP_EVENT_BUFFER_ACCEPTED
;
174 /* written will be zero if a pty is not connected - don't treat as error */
176 /* event buffer not accepted due to error in character layer */
177 evt_buf_hdr
->flags
&= ~(SCLP_EVENT_BUFFER_ACCEPTED
);
178 rc
= SCLP_RC_CONTAINED_EQUIPMENT_CHECK
;
184 static const VMStateDescription vmstate_sclpconsole
= {
185 .name
= "sclpconsole",
187 .minimum_version_id
= 0,
188 .minimum_version_id_old
= 0,
189 .fields
= (VMStateField
[]) {
190 VMSTATE_BOOL(event
.event_pending
, SCLPConsole
),
191 VMSTATE_UINT8_ARRAY(iov
, SCLPConsole
, SIZE_BUFFER_VT220
),
192 VMSTATE_UINT32(iov_sclp
, SCLPConsole
),
193 VMSTATE_UINT32(iov_bs
, SCLPConsole
),
194 VMSTATE_UINT32(iov_data_len
, SCLPConsole
),
195 VMSTATE_UINT32(iov_sclp_rest
, SCLPConsole
),
196 VMSTATE_END_OF_LIST()
200 /* qemu object creation and initialization functions */
202 /* tell character layer our call-back functions */
204 static int console_init(SCLPEvent
*event
)
206 static bool console_available
;
208 SCLPConsole
*scon
= DO_UPCAST(SCLPConsole
, event
, event
);
210 if (console_available
) {
211 error_report("Multiple VT220 operator consoles are not supported");
214 console_available
= true;
216 qemu_chr_add_handlers(scon
->chr
, chr_can_read
,
217 chr_read
, NULL
, scon
);
223 static void console_reset(DeviceState
*dev
)
225 SCLPEvent
*event
= SCLP_EVENT(dev
);
226 SCLPConsole
*scon
= DO_UPCAST(SCLPConsole
, event
, event
);
228 event
->event_pending
= false;
231 scon
->iov_data_len
= 0;
232 scon
->iov_sclp_rest
= 0;
235 static int console_exit(SCLPEvent
*event
)
240 static Property console_properties
[] = {
241 DEFINE_PROP_CHR("chardev", SCLPConsole
, chr
),
242 DEFINE_PROP_END_OF_LIST(),
245 static void console_class_init(ObjectClass
*klass
, void *data
)
247 DeviceClass
*dc
= DEVICE_CLASS(klass
);
248 SCLPEventClass
*ec
= SCLP_EVENT_CLASS(klass
);
250 dc
->props
= console_properties
;
251 dc
->reset
= console_reset
;
252 dc
->vmsd
= &vmstate_sclpconsole
;
253 ec
->init
= console_init
;
254 ec
->exit
= console_exit
;
255 ec
->get_send_mask
= send_mask
;
256 ec
->get_receive_mask
= receive_mask
;
257 ec
->can_handle_event
= can_handle_event
;
258 ec
->read_event_data
= read_event_data
;
259 ec
->write_event_data
= write_event_data
;
262 static const TypeInfo sclp_console_info
= {
263 .name
= "sclpconsole",
264 .parent
= TYPE_SCLP_EVENT
,
265 .instance_size
= sizeof(SCLPConsole
),
266 .class_init
= console_class_init
,
267 .class_size
= sizeof(SCLPEventClass
),
270 static void register_types(void)
272 type_register_static(&sclp_console_info
);
275 type_init(register_types
)