2 * Terminal 3270 implementation
4 * Copyright 2017 IBM Corp.
6 * Authors: Yang Chen <bjcyang@linux.vnet.ibm.com>
7 * Jing Liu <liujbjl@linux.vnet.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or (at
10 * your option) any later version. See the COPYING file in the top-level
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "sysemu/char.h"
17 #include "hw/s390x/3270-ccw.h"
19 /* Enough spaces for different window sizes. */
20 #define INPUT_BUFFER_SIZE 1000
22 * 1 for header, 1024*2 for datastream, 2 for tail
23 * Reserve enough spaces for telnet IAC escape.
25 #define OUTPUT_BUFFER_SIZE 2051
27 typedef struct Terminal3270
{
28 EmulatedCcw3270Device cdev
;
30 uint8_t inv
[INPUT_BUFFER_SIZE
];
31 uint8_t outv
[OUTPUT_BUFFER_SIZE
];
37 #define TYPE_TERMINAL_3270 "x-terminal3270"
38 #define TERMINAL_3270(obj) \
39 OBJECT_CHECK(Terminal3270, (obj), TYPE_TERMINAL_3270)
41 static int terminal_can_read(void *opaque
)
43 Terminal3270
*t
= opaque
;
45 return INPUT_BUFFER_SIZE
- t
->in_len
;
49 * Protocol handshake done,
50 * signal guest by an unsolicited DE irq.
52 static void TN3270_handshake_done(Terminal3270
*t
)
54 CcwDevice
*ccw_dev
= CCW_DEVICE(t
);
55 SubchDev
*sch
= ccw_dev
->sch
;
57 t
->handshake_done
= true;
58 sch
->curr_status
.scsw
.dstat
= SCSW_DSTAT_DEVICE_END
;
59 css_conditional_io_interrupt(sch
);
63 * Receive inbound data from socket.
64 * For data given to guest, drop the data boundary IAC, IAC_EOR.
66 * Using "Reset" key on x3270 may result multiple commands in one packet.
67 * This usually happens when the user meets a poor traffic of the network.
68 * As of now, for such case, we simply terminate the connection,
69 * and we should come back here later with a better solution.
71 static void terminal_read(void *opaque
, const uint8_t *buf
, int size
)
73 Terminal3270
*t
= opaque
;
74 CcwDevice
*ccw_dev
= CCW_DEVICE(t
);
75 SubchDev
*sch
= ccw_dev
->sch
;
78 assert(size
<= (INPUT_BUFFER_SIZE
- t
->in_len
));
80 memcpy(&t
->inv
[t
->in_len
], buf
, size
);
86 if (!t
->handshake_done
) {
88 * Receiving Terminal Type is the last step of handshake.
89 * The data format: IAC SB Terminal-Type IS <terminal type> IAC SE
90 * The code for Terminal-Type is 0x18, for IS is 0.
91 * Simply check the data format and mark handshake_done.
93 if (t
->in_len
> 6 && t
->inv
[2] == 0x18 && t
->inv
[3] == 0x0 &&
94 t
->inv
[t
->in_len
- 2] == IAC
&& t
->inv
[t
->in_len
- 1] == IAC_SE
) {
95 TN3270_handshake_done(t
);
101 for (end
= 0; end
< t
->in_len
- 1; end
++) {
102 if (t
->inv
[end
] == IAC
&& t
->inv
[end
+ 1] == IAC_EOR
) {
106 if (end
== t
->in_len
- 2) {
107 /* Data is valid for consuming. */
109 sch
->curr_status
.scsw
.dstat
= SCSW_DSTAT_ATTENTION
;
110 css_conditional_io_interrupt(sch
);
111 } else if (end
< t
->in_len
- 2) {
112 /* "Reset" key is used. */
113 qemu_chr_fe_disconnect(&t
->chr
);
115 /* Gathering data. */
120 static void terminal_init(EmulatedCcw3270Device
*dev
, Error
**errp
)
122 Terminal3270
*t
= TERMINAL_3270(dev
);
123 static bool terminal_available
;
125 if (terminal_available
) {
126 error_setg(errp
, "Multiple 3270 terminals are not supported.");
129 terminal_available
= true;
130 qemu_chr_fe_set_handlers(&t
->chr
, terminal_can_read
,
131 terminal_read
, NULL
, t
, NULL
, true);
134 static int read_payload_3270(EmulatedCcw3270Device
*dev
, uint32_t cda
,
137 Terminal3270
*t
= TERMINAL_3270(dev
);
140 len
= MIN(count
, t
->in_len
);
141 cpu_physical_memory_write(cda
, t
->inv
, len
);
147 /* TN3270 uses binary transmission, which needs escape IAC to IAC IAC */
148 static int insert_IAC_escape_char(uint8_t *outv
, int out_len
)
150 int IAC_num
= 0, new_out_len
, i
, j
;
152 for (i
= 0; i
< out_len
; i
++) {
153 if (outv
[i
] == IAC
) {
160 new_out_len
= out_len
+ IAC_num
;
161 for (i
= out_len
- 1, j
= new_out_len
- 1; j
> i
&& i
>= 0; i
--, j
--) {
163 if (outv
[i
] == IAC
) {
171 * Write 3270 outbound to socket.
172 * Return the count of 3270 data field if succeeded, zero if failed.
174 static int write_payload_3270(EmulatedCcw3270Device
*dev
, uint8_t cmd
,
175 uint32_t cda
, uint16_t count
)
177 Terminal3270
*t
= TERMINAL_3270(dev
);
180 assert(count
<= (OUTPUT_BUFFER_SIZE
- 3) / 2);
182 if (!t
->handshake_done
) {
184 * Before having finished 3270 negotiation,
185 * sending outbound data is prohibited.
189 if (!qemu_chr_fe_get_driver(&t
->chr
)) {
190 /* We just say we consumed all data if there's no backend. */
194 cpu_physical_memory_read(cda
, &t
->outv
[1], count
);
195 t
->out_len
= count
+ 1;
197 t
->out_len
= insert_IAC_escape_char(t
->outv
, t
->out_len
);
198 t
->outv
[t
->out_len
++] = IAC
;
199 t
->outv
[t
->out_len
++] = IAC_EOR
;
201 retval
= qemu_chr_fe_write_all(&t
->chr
, t
->outv
, t
->out_len
);
202 return (retval
<= 0) ? 0 : (retval
- 3);
205 static Property terminal_properties
[] = {
206 DEFINE_PROP_CHR("chardev", Terminal3270
, chr
),
207 DEFINE_PROP_END_OF_LIST(),
210 static void terminal_class_init(ObjectClass
*klass
, void *data
)
212 DeviceClass
*dc
= DEVICE_CLASS(klass
);
213 EmulatedCcw3270Class
*ck
= EMULATED_CCW_3270_CLASS(klass
);
215 dc
->props
= terminal_properties
;
216 ck
->init
= terminal_init
;
217 ck
->read_payload_3270
= read_payload_3270
;
218 ck
->write_payload_3270
= write_payload_3270
;
221 static const TypeInfo ccw_terminal_info
= {
222 .name
= TYPE_TERMINAL_3270
,
223 .parent
= TYPE_EMULATED_CCW_3270
,
224 .instance_size
= sizeof(Terminal3270
),
225 .class_init
= terminal_class_init
,
226 .class_size
= sizeof(EmulatedCcw3270Class
),
229 static void register_types(void)
231 type_register_static(&ccw_terminal_info
);
234 type_init(register_types
)