1
2
3
4
5
6
7
8
9
10
11
12
13
14#include "qemu/osdep.h"
15#include "qapi/error.h"
16#include "chardev/char-fe.h"
17#include "hw/s390x/3270-ccw.h"
18
19
20#define INPUT_BUFFER_SIZE 1000
21
22
23
24
25#define OUTPUT_BUFFER_SIZE 2051
26
27typedef struct Terminal3270 {
28 EmulatedCcw3270Device cdev;
29 CharBackend chr;
30 uint8_t inv[INPUT_BUFFER_SIZE];
31 uint8_t outv[OUTPUT_BUFFER_SIZE];
32 int in_len;
33 int out_len;
34 bool handshake_done;
35 guint timer_tag;
36} Terminal3270;
37
38#define TYPE_TERMINAL_3270 "x-terminal3270"
39#define TERMINAL_3270(obj) \
40 OBJECT_CHECK(Terminal3270, (obj), TYPE_TERMINAL_3270)
41
42static int terminal_can_read(void *opaque)
43{
44 Terminal3270 *t = opaque;
45
46 return INPUT_BUFFER_SIZE - t->in_len;
47}
48
49
50
51
52
53static void TN3270_handshake_done(Terminal3270 *t)
54{
55 CcwDevice *ccw_dev = CCW_DEVICE(t);
56 SubchDev *sch = ccw_dev->sch;
57
58 t->handshake_done = true;
59 sch->curr_status.scsw.dstat = SCSW_DSTAT_DEVICE_END;
60 css_conditional_io_interrupt(sch);
61}
62
63
64
65
66
67static gboolean send_timing_mark_cb(gpointer opaque)
68{
69 Terminal3270 *t = opaque;
70 const uint8_t timing[] = {0xff, 0xfd, 0x06};
71
72 qemu_chr_fe_write_all(&t->chr, timing, sizeof(timing));
73 return true;
74}
75
76
77
78
79
80
81
82
83
84
85static void terminal_read(void *opaque, const uint8_t *buf, int size)
86{
87 Terminal3270 *t = opaque;
88 CcwDevice *ccw_dev = CCW_DEVICE(t);
89 SubchDev *sch = ccw_dev->sch;
90 int end;
91
92 assert(size <= (INPUT_BUFFER_SIZE - t->in_len));
93
94 if (t->timer_tag) {
95 g_source_remove(t->timer_tag);
96 t->timer_tag = 0;
97 }
98 t->timer_tag = g_timeout_add_seconds(600, send_timing_mark_cb, t);
99
100 memcpy(&t->inv[t->in_len], buf, size);
101 t->in_len += size;
102 if (t->in_len < 2) {
103 return;
104 }
105
106 if (!t->handshake_done) {
107
108
109
110
111
112
113 if (t->in_len > 6 && t->inv[2] == 0x18 && t->inv[3] == 0x0 &&
114 t->inv[t->in_len - 2] == IAC && t->inv[t->in_len - 1] == IAC_SE) {
115 TN3270_handshake_done(t);
116 t->in_len = 0;
117 }
118 return;
119 }
120
121 for (end = 0; end < t->in_len - 1; end++) {
122 if (t->inv[end] == IAC && t->inv[end + 1] == IAC_EOR) {
123 break;
124 }
125 }
126 if (end == t->in_len - 2) {
127
128 t->in_len -= 2;
129 sch->curr_status.scsw.dstat = SCSW_DSTAT_ATTENTION;
130 css_conditional_io_interrupt(sch);
131 } else if (end < t->in_len - 2) {
132
133 qemu_chr_fe_disconnect(&t->chr);
134 } else {
135
136 return;
137 }
138}
139
140static void chr_event(void *opaque, int event)
141{
142 Terminal3270 *t = opaque;
143 CcwDevice *ccw_dev = CCW_DEVICE(t);
144 SubchDev *sch = ccw_dev->sch;
145
146
147 t->in_len = 0;
148 t->out_len = 0;
149 t->handshake_done = false;
150 if (t->timer_tag) {
151 g_source_remove(t->timer_tag);
152 t->timer_tag = 0;
153 }
154
155 switch (event) {
156 case CHR_EVENT_OPENED:
157
158
159
160
161
162 t->timer_tag = g_timeout_add_seconds(600, send_timing_mark_cb, t);
163 break;
164 case CHR_EVENT_CLOSED:
165 sch->curr_status.scsw.dstat = SCSW_DSTAT_DEVICE_END;
166 css_conditional_io_interrupt(sch);
167 break;
168 }
169}
170
171static void terminal_init(EmulatedCcw3270Device *dev, Error **errp)
172{
173 Terminal3270 *t = TERMINAL_3270(dev);
174 static bool terminal_available;
175
176 if (terminal_available) {
177 error_setg(errp, "Multiple 3270 terminals are not supported.");
178 return;
179 }
180 terminal_available = true;
181 qemu_chr_fe_set_handlers(&t->chr, terminal_can_read,
182 terminal_read, chr_event, NULL, t, NULL, true);
183}
184
185static int read_payload_3270(EmulatedCcw3270Device *dev, uint32_t cda,
186 uint16_t count)
187{
188 Terminal3270 *t = TERMINAL_3270(dev);
189 int len;
190
191 len = MIN(count, t->in_len);
192 cpu_physical_memory_write(cda, t->inv, len);
193 t->in_len -= len;
194
195 return len;
196}
197
198
199static int insert_IAC_escape_char(uint8_t *outv, int out_len)
200{
201 int IAC_num = 0, new_out_len, i, j;
202
203 for (i = 0; i < out_len; i++) {
204 if (outv[i] == IAC) {
205 IAC_num++;
206 }
207 }
208 if (IAC_num == 0) {
209 return out_len;
210 }
211 new_out_len = out_len + IAC_num;
212 for (i = out_len - 1, j = new_out_len - 1; j > i && i >= 0; i--, j--) {
213 outv[j] = outv[i];
214 if (outv[i] == IAC) {
215 outv[--j] = IAC;
216 }
217 }
218 return new_out_len;
219}
220
221
222
223
224
225static int write_payload_3270(EmulatedCcw3270Device *dev, uint8_t cmd,
226 uint32_t cda, uint16_t count)
227{
228 Terminal3270 *t = TERMINAL_3270(dev);
229 int retval = 0;
230
231 assert(count <= (OUTPUT_BUFFER_SIZE - 3) / 2);
232
233 if (!t->handshake_done) {
234 if (!(t->outv[0] == IAC && t->outv[1] != IAC)) {
235
236
237
238
239 return 0;
240 }
241 }
242 if (!qemu_chr_fe_backend_connected(&t->chr)) {
243
244 return count;
245 }
246 t->outv[0] = cmd;
247 cpu_physical_memory_read(cda, &t->outv[1], count);
248 t->out_len = count + 1;
249
250 t->out_len = insert_IAC_escape_char(t->outv, t->out_len);
251 t->outv[t->out_len++] = IAC;
252 t->outv[t->out_len++] = IAC_EOR;
253
254 retval = qemu_chr_fe_write_all(&t->chr, t->outv, t->out_len);
255 return (retval <= 0) ? 0 : (retval - 3);
256}
257
258static Property terminal_properties[] = {
259 DEFINE_PROP_CHR("chardev", Terminal3270, chr),
260 DEFINE_PROP_END_OF_LIST(),
261};
262
263static const VMStateDescription terminal3270_vmstate = {
264 .name = TYPE_TERMINAL_3270,
265 .unmigratable = 1,
266};
267
268static void terminal_class_init(ObjectClass *klass, void *data)
269{
270 DeviceClass *dc = DEVICE_CLASS(klass);
271 EmulatedCcw3270Class *ck = EMULATED_CCW_3270_CLASS(klass);
272
273 dc->props = terminal_properties;
274 dc->vmsd = &terminal3270_vmstate;
275 ck->init = terminal_init;
276 ck->read_payload_3270 = read_payload_3270;
277 ck->write_payload_3270 = write_payload_3270;
278}
279
280static const TypeInfo ccw_terminal_info = {
281 .name = TYPE_TERMINAL_3270,
282 .parent = TYPE_EMULATED_CCW_3270,
283 .instance_size = sizeof(Terminal3270),
284 .class_init = terminal_class_init,
285 .class_size = sizeof(EmulatedCcw3270Class),
286};
287
288static void register_types(void)
289{
290 type_register_static(&ccw_terminal_info);
291}
292
293type_init(register_types)
294