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