s390/tty3270: convert lines during output
[linux-block.git] / drivers / s390 / char / con3270.c
CommitLineData
6f05e69e 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
1da177e4
LT
3 * IBM/3270 Driver - tty functions.
4 *
5 * Author(s):
6 * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
7 * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
a53c8fab 8 * -- Copyright IBM Corp. 2003
1da177e4
LT
9 */
10
1da177e4
LT
11#include <linux/module.h>
12#include <linux/types.h>
13#include <linux/kdev_t.h>
14#include <linux/tty.h>
15#include <linux/vt_kern.h>
16#include <linux/init.h>
17#include <linux/console.h>
18#include <linux/interrupt.h>
4d334fd1 19#include <linux/workqueue.h>
c17fe081
SS
20#include <linux/panic_notifier.h>
21#include <linux/reboot.h>
1da177e4 22#include <linux/slab.h>
57c8a661 23#include <linux/memblock.h>
9d4bfd41 24#include <linux/compat.h>
1da177e4
LT
25
26#include <asm/ccwdev.h>
27#include <asm/cio.h>
28#include <asm/ebcdic.h>
c17fe081 29#include <asm/cpcmd.h>
7c0f6ba6 30#include <linux/uaccess.h>
1da177e4 31
1da177e4
LT
32#include "raw3270.h"
33#include "keyboard.h"
34
35#define TTY3270_CHAR_BUF_SIZE 256
9c138af9 36#define TTY3270_OUTPUT_BUFFER_SIZE 4096
1da177e4
LT
37#define TTY3270_STRING_PAGES 5
38
b2057c87
SS
39#define TTY3270_SCREEN_PAGES 8 /* has to be power-of-two */
40
c17fe081 41static struct tty_driver *tty3270_driver;
1da177e4 42static int tty3270_max_index;
c17fe081 43static struct tty3270 *condev;
2b67fc46 44static struct raw3270_fn tty3270_fn;
1da177e4 45
c2e9375e 46struct tty3270_attribute {
94dbb0a7 47 unsigned char alternate_charset:1; /* Graphics charset */
4043ea22
SS
48 unsigned char highlight; /* Blink/reverse/underscore */
49 unsigned char f_color; /* Foreground color */
50 unsigned char b_color; /* Background color */
c2e9375e
SS
51};
52
1da177e4
LT
53struct tty3270_cell {
54 unsigned char character;
c2e9375e 55 struct tty3270_attribute attributes;
1da177e4
LT
56};
57
58struct tty3270_line {
59 struct tty3270_cell *cells;
60 int len;
b2057c87 61 int dirty;
1da177e4
LT
62};
63
cbb36313
SS
64static const unsigned char sfq_read_partition[] = {
65 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81
66};
67
1da177e4
LT
68#define ESCAPE_NPAR 8
69
70/*
71 * The main tty view data structure.
72 * FIXME:
73 * 1) describe line orientation & lines list concept against screen
74 * 2) describe conversion of screen to lines
75 * 3) describe line format.
76 */
77struct tty3270 {
78 struct raw3270_view view;
ba186e7d 79 struct tty_port port;
1da177e4
LT
80 void **freemem_pages; /* Array of pages used for freemem. */
81 struct list_head freemem; /* List of free memory for strings. */
82
83 /* Output stuff. */
84 struct list_head lines; /* List of lines. */
1da177e4
LT
85 unsigned char wcc; /* Write control character. */
86 int nr_lines; /* # lines in list. */
87 int nr_up; /* # lines up in history. */
88 unsigned long update_flags; /* Update indication bits. */
1da177e4
LT
89 struct raw3270_request *write; /* Single write request. */
90 struct timer_list timer; /* Output delay timer. */
ec1b0a33 91 char *converted_line; /* RAW 3270 data stream */
9c138af9
SS
92 unsigned int line_view_start; /* Start of visible area */
93 unsigned int line_write_start; /* current write position */
1da177e4
LT
94
95 /* Current tty screen. */
96 unsigned int cx, cy; /* Current output position. */
c2e9375e
SS
97 struct tty3270_attribute attributes;
98 struct tty3270_attribute saved_attributes;
b2057c87 99 int allocated_lines;
1da177e4
LT
100 struct tty3270_line *screen;
101
102 /* Input stuff. */
103 struct string *prompt; /* Output string for input area. */
104 struct string *input; /* Input string for read request. */
105 struct raw3270_request *read; /* Single read request. */
106 struct raw3270_request *kreset; /* Single keyboard reset request. */
cbb36313 107 struct raw3270_request *readpartreq;
1da177e4
LT
108 unsigned char inattr; /* Visible/invisible input. */
109 int throttle, attn; /* tty throttle/unthrottle. */
110 struct tasklet_struct readlet; /* Tasklet to issue read request. */
0c756914 111 struct tasklet_struct hanglet; /* Tasklet to hang up the tty. */
1da177e4
LT
112 struct kbd_data *kbd; /* key_maps stuff. */
113
114 /* Escape sequence parsing. */
115 int esc_state, esc_ques, esc_npar;
116 int esc_par[ESCAPE_NPAR];
117 unsigned int saved_cx, saved_cy;
1da177e4
LT
118
119 /* Command recalling. */
120 struct list_head rcl_lines; /* List of recallable lines. */
121 struct list_head *rcl_walk; /* Point in rcl_lines list. */
122 int rcl_nr, rcl_max; /* Number/max number of rcl_lines. */
123
124 /* Character array for put_char/flush_chars. */
125 unsigned int char_count;
126 char char_buf[TTY3270_CHAR_BUF_SIZE];
127};
128
129/* tty3270->update_flags. See tty3270_update for details. */
130#define TTY_UPDATE_ERASE 1 /* Use EWRITEA instead of WRITE. */
1da177e4
LT
131#define TTY_UPDATE_INPUT 4 /* Update input line. */
132#define TTY_UPDATE_STATUS 8 /* Update status line. */
205d7ab9 133#define TTY_UPDATE_ALL 16 /* Recreate screen. */
1da177e4 134
9eb99b94 135#define TTY3270_INPUT_AREA_ROWS 2
f77f936a 136
1da177e4
LT
137/*
138 * Setup timeout for a device. On timeout trigger an update.
139 */
2b67fc46 140static void tty3270_set_timer(struct tty3270 *tp, int expires)
1da177e4 141{
03439e7d 142 mod_timer(&tp->timer, jiffies + expires);
1da177e4
LT
143}
144
9eb99b94
SS
145static int tty3270_tty_rows(struct tty3270 *tp)
146{
147 return tp->view.rows - TTY3270_INPUT_AREA_ROWS;
148}
149
ae657244
SS
150static char *tty3270_add_ba(struct tty3270 *tp, char *cp, char order, int x, int y)
151{
152 *cp++ = order;
153 raw3270_buffer_address(tp->view.dev, cp, x, y);
154 return cp + 2;
155}
156
157static char *tty3270_add_ra(struct tty3270 *tp, char *cp, int x, int y, char c)
158{
159 cp = tty3270_add_ba(tp, cp, TO_RA, x, y);
160 *cp++ = c;
161 return cp;
162}
163
164static char *tty3270_add_sa(struct tty3270 *tp, char *cp, char attr, char value)
165{
166 *cp++ = TO_SA;
167 *cp++ = attr;
168 *cp++ = value;
169 return cp;
170}
171
172static char *tty3270_add_ge(struct tty3270 *tp, char *cp, char c)
173{
174 *cp++ = TO_GE;
175 *cp++ = c;
176 return cp;
177}
178
ec1b0a33
SS
179static char *tty3270_add_sf(struct tty3270 *tp, char *cp, char type)
180{
181 *cp++ = TO_SF;
182 *cp++ = type;
183 return cp;
184}
185
9c138af9
SS
186static int tty3270_line_increment(struct tty3270 *tp, unsigned int line, unsigned int incr)
187{
188 return (line + incr) & (tp->allocated_lines - 1);
189}
190
191static struct tty3270_line *tty3270_get_write_line(struct tty3270 *tp, unsigned int num)
192{
193 return tp->screen + tty3270_line_increment(tp, tp->line_write_start, num);
194}
195
196static struct tty3270_line *tty3270_get_view_line(struct tty3270 *tp, unsigned int num)
197{
198 return tp->screen + tty3270_line_increment(tp, tp->line_view_start, num - tp->nr_up);
199}
200
1da177e4
LT
201/*
202 * The input line are the two last lines of the screen.
203 */
e6d98bb8 204static void tty3270_update_prompt(struct tty3270 *tp, char *input, int count)
1da177e4
LT
205{
206 struct string *line;
1da177e4
LT
207
208 line = tp->prompt;
209 if (count != 0)
210 line->string[5] = TF_INMDT;
211 else
212 line->string[5] = tp->inattr;
213 if (count > tp->view.cols * 2 - 11)
214 count = tp->view.cols * 2 - 11;
215 memcpy(line->string + 6, input, count);
216 line->string[6 + count] = TO_IC;
217 /* Clear to end of input line. */
218 if (count < tp->view.cols * 2 - 11) {
ae657244 219 tty3270_add_ra(tp, line->string + count + 7, -9, -1, 0);
1da177e4
LT
220 line->len = 11 + count;
221 } else
222 line->len = 7 + count;
223 tp->update_flags |= TTY_UPDATE_INPUT;
224}
225
e6d98bb8 226static void tty3270_create_prompt(struct tty3270 *tp)
1da177e4
LT
227{
228 static const unsigned char blueprint[] =
229 { TO_SBA, 0, 0, 0x6e, TO_SF, TF_INPUT,
230 /* empty input string */
231 TO_IC, TO_RA, 0, 0, 0 };
232 struct string *line;
1da177e4
LT
233
234 line = alloc_string(&tp->freemem,
235 sizeof(blueprint) + tp->view.cols * 2 - 9);
236 tp->prompt = line;
237 tp->inattr = TF_INPUT;
238 /* Copy blueprint to status line */
239 memcpy(line->string, blueprint, sizeof(blueprint));
240 line->len = sizeof(blueprint);
241 /* Set output offsets. */
f77f936a
SS
242
243 raw3270_buffer_address(tp->view.dev, line->string + 1, 0, -2);
244 raw3270_buffer_address(tp->view.dev, line->string + 8, -9, -1);
1da177e4
LT
245
246 /* Allocate input string for reading. */
247 tp->input = alloc_string(&tp->freemem, tp->view.cols * 2 - 9 + 6);
248}
249
ae657244
SS
250/*
251 * The status line is the last line of the screen. It shows the string
252 * "Running"/"Holding" in the lower right corner of the screen.
253 */
ec1b0a33 254static int tty3270_add_status(struct tty3270 *tp)
1da177e4 255{
ec1b0a33
SS
256 char *cp = tp->converted_line;
257 int len;
1da177e4 258
ec1b0a33
SS
259 cp = tty3270_add_ba(tp, cp, TO_SBA, -9, -1);
260 cp = tty3270_add_sf(tp, cp, TF_LOG);
261 cp = tty3270_add_sa(tp, cp, TAT_FGCOLOR, TAC_GREEN);
262 len = sprintf(cp, tp->nr_up ? "History" : "Running");
263 codepage_convert(tp->view.ascebc, cp, len);
264 cp += len;
265 cp = tty3270_add_sf(tp, cp, TF_LOG);
266 cp = tty3270_add_sa(tp, cp, TAT_FGCOLOR, TAC_RESET);
267 return cp - (char *)tp->converted_line;
1da177e4
LT
268}
269
270/*
271 * Set output offsets to 3270 datastream fragment of a tty string.
272 * (TO_SBA offset at the start and TO_RA offset at the end of the string)
273 */
9c138af9 274static void tty3270_update_string(struct tty3270 *tp, char *line, int len, int nr)
1da177e4
LT
275{
276 unsigned char *cp;
9c138af9
SS
277 raw3270_buffer_address(tp->view.dev, line + 1, 0, nr);
278 cp = line + len - 4;
1da177e4 279 if (*cp == TO_RA)
f77f936a 280 raw3270_buffer_address(tp->view.dev, cp + 1, 0, nr + 1);
1da177e4
LT
281}
282
1da177e4
LT
283/*
284 * Alloc string for size bytes. If there is not enough room in
285 * freemem, free strings until there is room.
286 */
e6d98bb8 287static struct string *tty3270_alloc_string(struct tty3270 *tp, size_t size)
1da177e4
LT
288{
289 struct string *s, *n;
290
291 s = alloc_string(&tp->freemem, size);
292 if (s)
293 return s;
294 list_for_each_entry_safe(s, n, &tp->lines, list) {
1da177e4 295 list_del(&s->list);
1da177e4
LT
296 if (free_string(&tp->freemem, s) >= size)
297 break;
298 }
9c138af9 299 return alloc_string(&tp->freemem, size);
1da177e4
LT
300}
301
e6d98bb8 302static void tty3270_blank_screen(struct tty3270 *tp)
7e36eff1 303{
9c138af9 304 struct tty3270_line *line;
7e36eff1
MS
305 int i;
306
9c138af9
SS
307 for (i = 0; i < tty3270_tty_rows(tp); i++) {
308 line = tty3270_get_write_line(tp, i);
309 line->len = 0;
310 line->dirty = 1;
7e36eff1 311 }
9c138af9 312 tp->nr_up = 0;
7e36eff1
MS
313}
314
1da177e4
LT
315/*
316 * Write request completion callback.
317 */
e6d98bb8 318static void tty3270_write_callback(struct raw3270_request *rq, void *data)
1da177e4 319{
881e18f9 320 struct tty3270 *tp = container_of(rq->view, struct tty3270, view);
1da177e4 321
1da177e4 322 if (rq->rc != 0) {
25985edc 323 /* Write wasn't successful. Refresh all. */
1da177e4
LT
324 tp->update_flags = TTY_UPDATE_ALL;
325 tty3270_set_timer(tp, 1);
326 }
327 raw3270_request_reset(rq);
328 xchg(&tp->write, rq);
329}
330
9c138af9 331static int tty3270_required_length(struct tty3270 *tp, struct tty3270_line *line)
2b62ba58
SS
332{
333 unsigned char f_color, b_color, highlight;
2b62ba58
SS
334 struct tty3270_cell *cell;
335 int i, flen = 3; /* Prefix (TO_SBA). */
336
2b62ba58
SS
337 flen += line->len;
338 highlight = TAX_RESET;
339 f_color = TAC_RESET;
340 b_color = TAC_RESET;
341
342 for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
343 if (cell->attributes.highlight != highlight) {
344 flen += 3; /* TO_SA to switch highlight. */
345 highlight = cell->attributes.highlight;
346 }
347 if (cell->attributes.f_color != f_color) {
348 flen += 3; /* TO_SA to switch color. */
349 f_color = cell->attributes.f_color;
350 }
351 if (cell->attributes.b_color != b_color) {
352 flen += 3; /* TO_SA to switch color. */
353 b_color = cell->attributes.b_color;
354 }
355 if (cell->attributes.alternate_charset)
356 flen += 1; /* TO_GE to switch to graphics extensions */
357 }
358 if (highlight != TAX_RESET)
359 flen += 3; /* TO_SA to reset hightlight. */
360 if (f_color != TAC_RESET)
361 flen += 3; /* TO_SA to reset color. */
362 if (b_color != TAC_RESET)
363 flen += 3; /* TO_SA to reset color. */
364 if (line->len < tp->view.cols)
365 flen += 4; /* Postfix (TO_RA). */
366
367 return flen;
368}
369
370static char *tty3270_add_reset_attributes(struct tty3270 *tp, struct tty3270_line *line,
371 char *cp, struct tty3270_attribute *attr)
372{
ae657244
SS
373 if (attr->highlight != TAX_RESET)
374 cp = tty3270_add_sa(tp, cp, TAT_EXTHI, TAX_RESET);
375 if (attr->f_color != TAC_RESET)
376 cp = tty3270_add_sa(tp, cp, TAT_FGCOLOR, TAX_RESET);
377 if (attr->b_color != TAC_RESET)
378 cp = tty3270_add_sa(tp, cp, TAT_BGCOLOR, TAX_RESET);
379 if (line->len < tp->view.cols)
380 cp = tty3270_add_ra(tp, cp, 0, 0, 0);
2b62ba58
SS
381 return cp;
382}
383
6e49017c
SS
384static char tty3270_graphics_translate(struct tty3270 *tp, char ch)
385{
386 switch (ch) {
387 case 'q': /* - */
388 return 0xa2;
389 case 'x': /* '|' */
390 return 0x85;
391 case 'l': /* |- */
392 return 0xc5;
393 case 't': /* |_ */
394 return 0xc6;
395 case 'u': /* _| */
396 return 0xd6;
397 case 'k': /* -| */
398 return 0xd5;
399 case 'j':
400 return 0xd4;
401 case 'm':
402 return 0xc4;
403 case 'n': /* + */
404 return 0xd3;
405 case 'v':
406 return 0xc7;
407 case 'w':
408 return 0xd7;
409 default:
410 return ch;
411 }
412}
413
414static char *tty3270_add_attributes(struct tty3270 *tp, struct tty3270_line *line,
415 struct tty3270_attribute *attr, char *cp)
2b62ba58
SS
416{
417 struct tty3270_cell *cell;
ae657244 418 int c, i;
2b62ba58 419
ae657244 420 cp = tty3270_add_ba(tp, cp, TO_SBA, 0, 0);
2b62ba58
SS
421
422 for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
423 if (cell->attributes.highlight != attr->highlight) {
2b62ba58 424 attr->highlight = cell->attributes.highlight;
ae657244 425 cp = tty3270_add_sa(tp, cp, TAT_EXTHI, attr->highlight);
2b62ba58
SS
426 }
427 if (cell->attributes.f_color != attr->f_color) {
2b62ba58 428 attr->f_color = cell->attributes.f_color;
ae657244 429 cp = tty3270_add_sa(tp, cp, TAT_FGCOLOR, attr->f_color);
2b62ba58
SS
430 }
431 if (cell->attributes.b_color != attr->b_color) {
2b62ba58 432 attr->b_color = cell->attributes.b_color;
ae657244 433 cp = tty3270_add_sa(tp, cp, TAT_BGCOLOR, attr->b_color);
2b62ba58 434 }
ae657244
SS
435 c = cell->character;
436 if (cell->attributes.alternate_charset)
437 cp = tty3270_add_ge(tp, cp, tty3270_graphics_translate(tp, c));
438 else
439 *cp++ = tp->view.ascebc[c];
2b62ba58
SS
440 }
441 return cp;
442}
443
444static void tty3270_reset_attributes(struct tty3270_attribute *attr)
445{
446 attr->highlight = TAX_RESET;
447 attr->f_color = TAC_RESET;
448 attr->b_color = TAC_RESET;
449}
450
2b62ba58
SS
451/*
452 * Convert a tty3270_line to a 3270 data fragment usable for output.
453 */
9c138af9 454static unsigned int tty3270_convert_line(struct tty3270 *tp, struct tty3270_line *line)
2b62ba58 455{
2b62ba58 456 struct tty3270_attribute attr;
9c138af9 457 int flen;
2b62ba58
SS
458 char *cp;
459
460 /* Determine how long the fragment will be. */
9c138af9
SS
461 flen = tty3270_required_length(tp, line);
462 if (flen > PAGE_SIZE)
463 return 0;
2b62ba58
SS
464 /* Write 3270 data fragment. */
465 tty3270_reset_attributes(&attr);
9c138af9 466 cp = tty3270_add_attributes(tp, line, &attr, tp->converted_line);
2b62ba58 467 cp = tty3270_add_reset_attributes(tp, line, cp, &attr);
9c138af9 468 return cp - (char *)tp->converted_line;
2b62ba58
SS
469}
470
1da177e4
LT
471/*
472 * Update 3270 display.
473 */
e6d98bb8 474static void tty3270_update(struct timer_list *t)
1da177e4 475{
c9602ee7 476 struct tty3270 *tp = from_timer(tp, t, timer);
1da177e4 477 struct raw3270_request *wrq;
9c138af9 478 struct tty3270_line *line;
1da177e4 479 unsigned long updated;
9c138af9 480 int i, rc, len;
1da177e4
LT
481
482 wrq = xchg(&tp->write, 0);
483 if (!wrq) {
484 tty3270_set_timer(tp, 1);
485 return;
486 }
487
c17fe081 488 spin_lock_irq(&tp->view.lock);
1da177e4 489 updated = 0;
205d7ab9 490 if (tp->update_flags & TTY_UPDATE_ALL) {
9c138af9 491 tp->update_flags = TTY_UPDATE_ERASE |
205d7ab9
MS
492 TTY_UPDATE_INPUT | TTY_UPDATE_STATUS;
493 }
1da177e4
LT
494 if (tp->update_flags & TTY_UPDATE_ERASE) {
495 /* Use erase write alternate to erase display. */
496 raw3270_request_set_cmd(wrq, TC_EWRITEA);
497 updated |= TTY_UPDATE_ERASE;
498 } else
499 raw3270_request_set_cmd(wrq, TC_WRITE);
500
501 raw3270_request_add_data(wrq, &tp->wcc, 1);
502 tp->wcc = TW_NONE;
503
504 /*
505 * Update status line.
506 */
ec1b0a33
SS
507 if (tp->update_flags & TTY_UPDATE_STATUS) {
508 len = tty3270_add_status(tp);
509 if (raw3270_request_add_data(wrq, tp->converted_line, len) == 0)
1da177e4 510 updated |= TTY_UPDATE_STATUS;
ec1b0a33 511 }
1da177e4
LT
512
513 /*
514 * Write input line.
515 */
516 if (tp->update_flags & TTY_UPDATE_INPUT)
517 if (raw3270_request_add_data(wrq, tp->prompt->string,
518 tp->prompt->len) == 0)
519 updated |= TTY_UPDATE_INPUT;
520
9c138af9
SS
521 for (i = 0; i < tty3270_tty_rows(tp); i++) {
522 line = tty3270_get_view_line(tp, i);
523 if (!line->dirty)
524 continue;
525 len = tty3270_convert_line(tp, line);
526 tty3270_update_string(tp, tp->converted_line, len, i);
527 if (raw3270_request_add_data(wrq, tp->converted_line, len))
528 break;
529 line->dirty = 0;
1da177e4 530 }
9c138af9
SS
531
532 if (i < tty3270_tty_rows(tp) - 1)
533 tty3270_set_timer(tp, 1);
534
1da177e4
LT
535 wrq->callback = tty3270_write_callback;
536 rc = raw3270_start(&tp->view, wrq);
537 if (rc == 0) {
538 tp->update_flags &= ~updated;
539 if (tp->update_flags)
540 tty3270_set_timer(tp, 1);
541 } else {
542 raw3270_request_reset(wrq);
543 xchg(&tp->write, wrq);
544 }
c17fe081 545 spin_unlock_irq(&tp->view.lock);
1da177e4
LT
546}
547
548/*
549 * Command recalling.
550 */
e6d98bb8 551static void tty3270_rcl_add(struct tty3270 *tp, char *input, int len)
1da177e4
LT
552{
553 struct string *s;
554
d2c993d8 555 tp->rcl_walk = NULL;
1da177e4
LT
556 if (len <= 0)
557 return;
558 if (tp->rcl_nr >= tp->rcl_max) {
559 s = list_entry(tp->rcl_lines.next, struct string, list);
560 list_del(&s->list);
561 free_string(&tp->freemem, s);
562 tp->rcl_nr--;
563 }
564 s = tty3270_alloc_string(tp, len);
9c138af9
SS
565 if (!s)
566 return;
1da177e4
LT
567 memcpy(s->string, input, len);
568 list_add_tail(&s->list, &tp->rcl_lines);
569 tp->rcl_nr++;
570}
571
e6d98bb8 572static void tty3270_rcl_backward(struct kbd_data *kbd)
1da177e4 573{
ba186e7d 574 struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
1da177e4
LT
575 struct string *s;
576
c17fe081 577 spin_lock_irq(&tp->view.lock);
1da177e4
LT
578 if (tp->inattr == TF_INPUT) {
579 if (tp->rcl_walk && tp->rcl_walk->prev != &tp->rcl_lines)
580 tp->rcl_walk = tp->rcl_walk->prev;
581 else if (!list_empty(&tp->rcl_lines))
582 tp->rcl_walk = tp->rcl_lines.prev;
583 s = tp->rcl_walk ?
d2c993d8 584 list_entry(tp->rcl_walk, struct string, list) : NULL;
1da177e4
LT
585 if (tp->rcl_walk) {
586 s = list_entry(tp->rcl_walk, struct string, list);
587 tty3270_update_prompt(tp, s->string, s->len);
588 } else
d2c993d8 589 tty3270_update_prompt(tp, NULL, 0);
1da177e4
LT
590 tty3270_set_timer(tp, 1);
591 }
c17fe081 592 spin_unlock_irq(&tp->view.lock);
1da177e4
LT
593}
594
595/*
596 * Deactivate tty view.
597 */
e6d98bb8 598static void tty3270_exit_tty(struct kbd_data *kbd)
1da177e4 599{
ba186e7d 600 struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
1da177e4 601
1da177e4
LT
602 raw3270_deactivate_view(&tp->view);
603}
604
9c138af9
SS
605static void tty3270_redraw(struct tty3270 *tp)
606{
607 int i;
608
609 for (i = 0; i < tty3270_tty_rows(tp); i++)
610 tty3270_get_view_line(tp, i)->dirty = 1;
611 tp->update_flags = TTY_UPDATE_ALL;
612 tty3270_set_timer(tp, 1);
613}
1da177e4
LT
614/*
615 * Scroll forward in history.
616 */
e6d98bb8 617static void tty3270_scroll_forward(struct kbd_data *kbd)
1da177e4 618{
ba186e7d 619 struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
1da177e4 620
c17fe081 621 spin_lock_irq(&tp->view.lock);
9c138af9
SS
622
623 if (tp->nr_up >= tty3270_tty_rows(tp))
624 tp->nr_up -= tty3270_tty_rows(tp) / 2;
625 else
626 tp->nr_up = 0;
627 tty3270_redraw(tp);
c17fe081 628 spin_unlock_irq(&tp->view.lock);
1da177e4
LT
629}
630
631/*
632 * Scroll backward in history.
633 */
e6d98bb8 634static void tty3270_scroll_backward(struct kbd_data *kbd)
1da177e4 635{
ba186e7d 636 struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
1da177e4 637
c17fe081 638 spin_lock_irq(&tp->view.lock);
9c138af9
SS
639 tp->nr_up += tty3270_tty_rows(tp) / 2;
640 if (tp->nr_up > tp->allocated_lines - tty3270_tty_rows(tp))
641 tp->nr_up = tp->allocated_lines - tty3270_tty_rows(tp);
642 tty3270_redraw(tp);
c17fe081 643 spin_unlock_irq(&tp->view.lock);
1da177e4
LT
644}
645
646/*
647 * Pass input line to tty.
648 */
e6d98bb8 649static void tty3270_read_tasklet(unsigned long data)
1da177e4 650{
5cdfbdce 651 struct raw3270_request *rrq = (struct raw3270_request *)data;
1da177e4 652 static char kreset_data = TW_KR;
881e18f9 653 struct tty3270 *tp = container_of(rrq->view, struct tty3270, view);
1da177e4
LT
654 char *input;
655 int len;
656
c17fe081 657 spin_lock_irq(&tp->view.lock);
1da177e4
LT
658 /*
659 * Two AID keys are special: For 0x7d (enter) the input line
660 * has to be emitted to the tty and for 0x6d the screen
661 * needs to be redrawn.
662 */
d2c993d8 663 input = NULL;
1da177e4 664 len = 0;
e22de7d7
SS
665 switch (tp->input->string[0]) {
666 case AID_ENTER:
1da177e4
LT
667 /* Enter: write input to tty. */
668 input = tp->input->string + 6;
669 len = tp->input->len - 6 - rrq->rescnt;
670 if (tp->inattr != TF_INPUTN)
671 tty3270_rcl_add(tp, input, len);
9c138af9 672 if (tp->nr_up > 0)
1da177e4 673 tp->nr_up = 0;
1da177e4 674 /* Clear input area. */
d2c993d8 675 tty3270_update_prompt(tp, NULL, 0);
1da177e4 676 tty3270_set_timer(tp, 1);
e22de7d7
SS
677 break;
678 case AID_CLEAR:
1da177e4 679 /* Display has been cleared. Redraw. */
1da177e4
LT
680 tp->update_flags = TTY_UPDATE_ALL;
681 tty3270_set_timer(tp, 1);
cbb36313
SS
682 if (!list_empty(&tp->readpartreq->list))
683 break;
684 raw3270_start_request(&tp->view, tp->readpartreq, TC_WRITESF,
685 (char *)sfq_read_partition, sizeof(sfq_read_partition));
686 break;
687 case AID_READ_PARTITION:
688 raw3270_read_modified_cb(tp->readpartreq, tp->input->string);
689 break;
e22de7d7
SS
690 default:
691 break;
1da177e4 692 }
c17fe081 693 spin_unlock_irq(&tp->view.lock);
1da177e4
LT
694
695 /* Start keyboard reset command. */
f08e3155 696 raw3270_start_request(&tp->view, tp->kreset, TC_WRITE, &kreset_data, 1);
1da177e4 697
ba186e7d
JS
698 while (len-- > 0)
699 kbd_keycode(tp->kbd, *input++);
700 /* Emit keycode for AID byte. */
701 kbd_keycode(tp->kbd, 256 + tp->input->string[0]);
1da177e4
LT
702
703 raw3270_request_reset(rrq);
704 xchg(&tp->read, rrq);
705 raw3270_put_view(&tp->view);
706}
707
708/*
709 * Read request completion callback.
710 */
e6d98bb8 711static void tty3270_read_callback(struct raw3270_request *rq, void *data)
1da177e4 712{
881e18f9 713 struct tty3270 *tp = container_of(rq->view, struct tty3270, view);
1da177e4
LT
714 raw3270_get_view(rq->view);
715 /* Schedule tasklet to pass input to tty. */
881e18f9 716 tasklet_schedule(&tp->readlet);
1da177e4
LT
717}
718
719/*
720 * Issue a read request. Call with device lock.
721 */
e6d98bb8 722static void tty3270_issue_read(struct tty3270 *tp, int lock)
1da177e4
LT
723{
724 struct raw3270_request *rrq;
725 int rc;
726
727 rrq = xchg(&tp->read, 0);
728 if (!rrq)
729 /* Read already scheduled. */
730 return;
731 rrq->callback = tty3270_read_callback;
732 rrq->callback_data = tp;
733 raw3270_request_set_cmd(rrq, TC_READMOD);
734 raw3270_request_set_data(rrq, tp->input->string, tp->input->len);
735 /* Issue the read modified request. */
736 if (lock) {
737 rc = raw3270_start(&tp->view, rrq);
738 } else
739 rc = raw3270_start_irq(&tp->view, rrq);
740 if (rc) {
741 raw3270_request_reset(rrq);
742 xchg(&tp->read, rrq);
743 }
744}
745
0c756914
MS
746/*
747 * Hang up the tty
748 */
e6d98bb8 749static void tty3270_hangup_tasklet(unsigned long data)
0c756914 750{
5cdfbdce 751 struct tty3270 *tp = (struct tty3270 *)data;
0c756914
MS
752 tty_port_tty_hangup(&tp->port, true);
753 raw3270_put_view(&tp->view);
754}
755
1da177e4
LT
756/*
757 * Switch to the tty view.
758 */
e6d98bb8 759static int tty3270_activate(struct raw3270_view *view)
1da177e4 760{
881e18f9 761 struct tty3270 *tp = container_of(view, struct tty3270, view);
1da177e4 762
1da177e4
LT
763 tp->update_flags = TTY_UPDATE_ALL;
764 tty3270_set_timer(tp, 1);
1da177e4
LT
765 return 0;
766}
767
e6d98bb8 768static void tty3270_deactivate(struct raw3270_view *view)
1da177e4 769{
881e18f9 770 struct tty3270 *tp = container_of(view, struct tty3270, view);
205d7ab9 771
205d7ab9 772 del_timer(&tp->timer);
1da177e4
LT
773}
774
e6d98bb8 775static void tty3270_irq(struct tty3270 *tp, struct raw3270_request *rq, struct irb *irb)
1da177e4
LT
776{
777 /* Handle ATTN. Schedule tasklet to read aid. */
23d805b6 778 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
1da177e4
LT
779 if (!tp->throttle)
780 tty3270_issue_read(tp, 0);
781 else
782 tp->attn = 1;
783 }
784
785 if (rq) {
0c756914 786 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
1da177e4 787 rq->rc = -EIO;
0c756914
MS
788 raw3270_get_view(&tp->view);
789 tasklet_schedule(&tp->hanglet);
790 } else {
1da177e4 791 /* Normal end. Copy residual count. */
23d805b6 792 rq->rescnt = irb->scsw.cmd.count;
0c756914 793 }
05bfd70b
MS
794 } else if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
795 /* Interrupt without an outstanding request -> update all */
796 tp->update_flags = TTY_UPDATE_ALL;
797 tty3270_set_timer(tp, 1);
1da177e4 798 }
1da177e4
LT
799}
800
801/*
802 * Allocate tty3270 structure.
803 */
e6d98bb8 804static struct tty3270 *tty3270_alloc_view(void)
1da177e4
LT
805{
806 struct tty3270 *tp;
807 int pages;
808
88abaab4 809 tp = kzalloc(sizeof(struct tty3270), GFP_KERNEL);
1da177e4
LT
810 if (!tp)
811 goto out_err;
1da177e4 812 tp->freemem_pages =
6da2ec56
KC
813 kmalloc_array(TTY3270_STRING_PAGES, sizeof(void *),
814 GFP_KERNEL);
1da177e4
LT
815 if (!tp->freemem_pages)
816 goto out_tp;
817 INIT_LIST_HEAD(&tp->freemem);
9d2ae233 818 INIT_LIST_HEAD(&tp->lines);
9d2ae233
JS
819 INIT_LIST_HEAD(&tp->rcl_lines);
820 tp->rcl_max = 20;
9d2ae233 821
1da177e4
LT
822 for (pages = 0; pages < TTY3270_STRING_PAGES; pages++) {
823 tp->freemem_pages[pages] = (void *)
824 __get_free_pages(GFP_KERNEL|GFP_DMA, 0);
825 if (!tp->freemem_pages[pages])
826 goto out_pages;
827 add_string_memory(&tp->freemem,
828 tp->freemem_pages[pages], PAGE_SIZE);
829 }
830 tp->write = raw3270_request_alloc(TTY3270_OUTPUT_BUFFER_SIZE);
ed3cb6f0 831 if (IS_ERR(tp->write))
1da177e4
LT
832 goto out_pages;
833 tp->read = raw3270_request_alloc(0);
ed3cb6f0 834 if (IS_ERR(tp->read))
1da177e4
LT
835 goto out_write;
836 tp->kreset = raw3270_request_alloc(1);
ed3cb6f0 837 if (IS_ERR(tp->kreset))
1da177e4 838 goto out_read;
cbb36313
SS
839 tp->readpartreq = raw3270_request_alloc(sizeof(sfq_read_partition));
840 if (IS_ERR(tp->readpartreq))
841 goto out_reset;
1da177e4
LT
842 tp->kbd = kbd_alloc();
843 if (!tp->kbd)
cbb36313 844 goto out_readpartreq;
57985d7e
MS
845
846 tty_port_init(&tp->port);
c9602ee7 847 timer_setup(&tp->timer, tty3270_update, 0);
5cdfbdce 848 tasklet_init(&tp->readlet, tty3270_read_tasklet,
57985d7e 849 (unsigned long) tp->read);
5cdfbdce 850 tasklet_init(&tp->hanglet, tty3270_hangup_tasklet,
0c756914 851 (unsigned long) tp);
1da177e4
LT
852 return tp;
853
cbb36313
SS
854out_readpartreq:
855 raw3270_request_free(tp->readpartreq);
1da177e4
LT
856out_reset:
857 raw3270_request_free(tp->kreset);
858out_read:
859 raw3270_request_free(tp->read);
860out_write:
861 raw3270_request_free(tp->write);
862out_pages:
863 while (pages--)
864 free_pages((unsigned long) tp->freemem_pages[pages], 0);
865 kfree(tp->freemem_pages);
191c5f10 866 tty_port_destroy(&tp->port);
1da177e4
LT
867out_tp:
868 kfree(tp);
869out_err:
870 return ERR_PTR(-ENOMEM);
871}
872
873/*
874 * Free tty3270 structure.
875 */
e6d98bb8 876static void tty3270_free_view(struct tty3270 *tp)
1da177e4
LT
877{
878 int pages;
879
880 kbd_free(tp->kbd);
881 raw3270_request_free(tp->kreset);
882 raw3270_request_free(tp->read);
883 raw3270_request_free(tp->write);
884 for (pages = 0; pages < TTY3270_STRING_PAGES; pages++)
885 free_pages((unsigned long) tp->freemem_pages[pages], 0);
886 kfree(tp->freemem_pages);
9c138af9 887 free_page((unsigned long)tp->converted_line);
191c5f10 888 tty_port_destroy(&tp->port);
1da177e4
LT
889 kfree(tp);
890}
891
892/*
893 * Allocate tty3270 screen.
894 */
b2057c87
SS
895static struct tty3270_line *tty3270_alloc_screen(struct tty3270 *tp, unsigned int rows,
896 unsigned int cols, int *allocated_out)
1da177e4 897{
4d334fd1 898 struct tty3270_line *screen;
b2057c87 899 int allocated, lines;
1da177e4 900
b2057c87
SS
901 allocated = __roundup_pow_of_two(rows) * TTY3270_SCREEN_PAGES;
902 screen = kcalloc(allocated, sizeof(struct tty3270_line), GFP_KERNEL);
4d334fd1 903 if (!screen)
1da177e4 904 goto out_err;
b2057c87
SS
905 for (lines = 0; lines < allocated; lines++) {
906 screen[lines].cells = kcalloc(cols, sizeof(struct tty3270_cell), GFP_KERNEL);
4d334fd1 907 if (!screen[lines].cells)
1da177e4 908 goto out_screen;
1da177e4 909 }
b2057c87 910 *allocated_out = allocated;
4d334fd1 911 return screen;
1da177e4
LT
912out_screen:
913 while (lines--)
4d334fd1
MS
914 kfree(screen[lines].cells);
915 kfree(screen);
1da177e4 916out_err:
4d334fd1 917 return ERR_PTR(-ENOMEM);
1da177e4
LT
918}
919
920/*
921 * Free tty3270 screen.
922 */
b2057c87 923static void tty3270_free_screen(struct tty3270_line *screen, int old_lines)
1da177e4
LT
924{
925 int lines;
926
b2057c87 927 for (lines = 0; lines < old_lines; lines++)
4d334fd1
MS
928 kfree(screen[lines].cells);
929 kfree(screen);
930}
931
932/*
933 * Resize tty3270 screen
934 */
91621ba7
SS
935static void tty3270_resize(struct raw3270_view *view,
936 int new_model, int new_rows, int new_cols,
937 int old_model, int old_rows, int old_cols)
4d334fd1 938{
91621ba7 939 struct tty3270 *tp = container_of(view, struct tty3270, view);
4d334fd1
MS
940 struct tty3270_line *screen, *oscreen;
941 struct tty_struct *tty;
4d334fd1 942 struct winsize ws;
b2057c87 943 int new_allocated, old_allocated = tp->allocated_lines;
4d334fd1 944
91621ba7
SS
945 if (old_model == new_model &&
946 old_cols == new_cols &&
947 old_rows == new_rows) {
948 spin_lock_irq(&tp->view.lock);
9c138af9 949 tty3270_redraw(tp);
91621ba7
SS
950 spin_unlock_irq(&tp->view.lock);
951 return;
952 }
b2057c87 953 screen = tty3270_alloc_screen(tp, new_rows, new_cols, &new_allocated);
8f0ba630 954 if (IS_ERR(screen))
4d334fd1
MS
955 return;
956 /* Switch to new output size */
c17fe081 957 spin_lock_irq(&tp->view.lock);
7e36eff1 958 tty3270_blank_screen(tp);
4d334fd1 959 oscreen = tp->screen;
4d334fd1 960 tp->screen = screen;
b2057c87 961 tp->allocated_lines = new_allocated;
91621ba7
SS
962 tp->view.rows = new_rows;
963 tp->view.cols = new_cols;
964 tp->view.model = new_model;
965
4d334fd1 966 free_string(&tp->freemem, tp->prompt);
4d334fd1 967 tty3270_create_prompt(tp);
4d334fd1 968 tp->update_flags = TTY_UPDATE_ALL;
c17fe081 969 spin_unlock_irq(&tp->view.lock);
b2057c87 970 tty3270_free_screen(oscreen, old_allocated);
4d334fd1
MS
971 tty3270_set_timer(tp, 1);
972 /* Informat tty layer about new size */
973 tty = tty_port_tty_get(&tp->port);
974 if (!tty)
975 return;
9eb99b94 976 ws.ws_row = tty3270_tty_rows(tp);
4d334fd1
MS
977 ws.ws_col = tp->view.cols;
978 tty_do_resize(tty, &ws);
5ff04fe5 979 tty_kref_put(tty);
4d334fd1
MS
980}
981
1da177e4
LT
982/*
983 * Unlink tty3270 data structure from tty.
984 */
e6d98bb8 985static void tty3270_release(struct raw3270_view *view)
1da177e4 986{
881e18f9 987 struct tty3270 *tp = container_of(view, struct tty3270, view);
ba186e7d 988 struct tty_struct *tty = tty_port_tty_get(&tp->port);
1da177e4 989
1da177e4 990 if (tty) {
d2c993d8 991 tty->driver_data = NULL;
ba186e7d 992 tty_port_tty_set(&tp->port, NULL);
1da177e4
LT
993 tty_hangup(tty);
994 raw3270_put_view(&tp->view);
ba186e7d 995 tty_kref_put(tty);
1da177e4
LT
996 }
997}
998
999/*
1000 * Free tty3270 data structure
1001 */
e6d98bb8 1002static void tty3270_free(struct raw3270_view *view)
1da177e4 1003{
881e18f9 1004 struct tty3270 *tp = container_of(view, struct tty3270, view);
4d334fd1 1005
03439e7d 1006 del_timer_sync(&tp->timer);
b2057c87 1007 tty3270_free_screen(tp->screen, tp->allocated_lines);
ec1b0a33 1008 free_page((unsigned long)tp->converted_line);
881e18f9 1009 tty3270_free_view(tp);
1da177e4
LT
1010}
1011
1012/*
1013 * Delayed freeing of tty3270 views.
1014 */
e6d98bb8 1015static void tty3270_del_views(void)
1da177e4 1016{
1da177e4
LT
1017 int i;
1018
c95571e6
MS
1019 for (i = RAW3270_FIRSTMINOR; i <= tty3270_max_index; i++) {
1020 struct raw3270_view *view = raw3270_find_view(&tty3270_fn, i);
881e18f9
JS
1021 if (!IS_ERR(view))
1022 raw3270_del_view(view);
1da177e4
LT
1023 }
1024}
1025
2b67fc46 1026static struct raw3270_fn tty3270_fn = {
1da177e4
LT
1027 .activate = tty3270_activate,
1028 .deactivate = tty3270_deactivate,
1029 .intv = (void *) tty3270_irq,
1030 .release = tty3270_release,
4d334fd1
MS
1031 .free = tty3270_free,
1032 .resize = tty3270_resize
1da177e4
LT
1033};
1034
a21e962e
SS
1035static int
1036tty3270_create_view(int index, struct tty3270 **newtp)
1da177e4
LT
1037{
1038 struct tty3270 *tp;
9c138af9 1039 int rc;
1da177e4 1040
a21e962e
SS
1041 if (tty3270_max_index < index + 1)
1042 tty3270_max_index = index + 1;
1da177e4
LT
1043
1044 /* Allocate tty3270 structure on first open. */
1045 tp = tty3270_alloc_view();
1046 if (IS_ERR(tp))
1047 return PTR_ERR(tp);
1048
1fcbba3d 1049 rc = raw3270_add_view(&tp->view, &tty3270_fn,
a21e962e 1050 index + RAW3270_FIRSTMINOR,
c17fe081 1051 RAW3270_VIEW_LOCK_IRQ);
ec1b0a33
SS
1052 if (rc)
1053 goto out_free_view;
1da177e4 1054
b2057c87
SS
1055 tp->screen = tty3270_alloc_screen(tp, tp->view.rows, tp->view.cols,
1056 &tp->allocated_lines);
4d334fd1
MS
1057 if (IS_ERR(tp->screen)) {
1058 rc = PTR_ERR(tp->screen);
ec1b0a33
SS
1059 goto out_put_view;
1060 }
1061
1062 tp->converted_line = (void *)__get_free_page(GFP_KERNEL);
1063 if (!tp->converted_line) {
1064 rc = -ENOMEM;
1065 goto out_free_screen;
1da177e4
LT
1066 }
1067
1da177e4 1068 tty3270_create_prompt(tp);
1da177e4
LT
1069
1070 /* Create blank line for every line in the tty output area. */
9c138af9 1071 tty3270_blank_screen(tp);
1da177e4 1072
ba186e7d 1073 tp->kbd->port = &tp->port;
1da177e4
LT
1074 tp->kbd->fn_handler[KVAL(K_INCRCONSOLE)] = tty3270_exit_tty;
1075 tp->kbd->fn_handler[KVAL(K_SCROLLBACK)] = tty3270_scroll_backward;
1076 tp->kbd->fn_handler[KVAL(K_SCROLLFORW)] = tty3270_scroll_forward;
1077 tp->kbd->fn_handler[KVAL(K_CONS)] = tty3270_rcl_backward;
1078 kbd_ascebc(tp->kbd, tp->view.ascebc);
1079
1080 raw3270_activate_view(&tp->view);
a21e962e
SS
1081 raw3270_put_view(&tp->view);
1082 *newtp = tp;
1083 return 0;
ec1b0a33
SS
1084
1085out_free_screen:
1086 tty3270_free_screen(tp->screen, tp->view.rows);
1087out_put_view:
1088 raw3270_put_view(&tp->view);
1089 raw3270_del_view(&tp->view);
1090out_free_view:
1091 tty3270_free_view(tp);
1092 return rc;
a21e962e
SS
1093}
1094
1095/*
1096 * This routine is called whenever a 3270 tty is opened first time.
1097 */
1098static int
1099tty3270_install(struct tty_driver *driver, struct tty_struct *tty)
1100{
1101 struct raw3270_view *view;
1102 struct tty3270 *tp;
1103 int rc;
20cda6f2 1104
a21e962e
SS
1105 /* Check if the tty3270 is already there. */
1106 view = raw3270_find_view(&tty3270_fn, tty->index + RAW3270_FIRSTMINOR);
1107 if (IS_ERR(view)) {
1108 rc = tty3270_create_view(tty->index, &tp);
1109 if (rc)
1110 return rc;
1111 } else {
1112 tp = container_of(view, struct tty3270, view);
1113 tty->driver_data = tp;
1114 tp->inattr = TF_INPUT;
1115 }
1116
9eb99b94 1117 tty->winsize.ws_row = tty3270_tty_rows(tp);
a21e962e 1118 tty->winsize.ws_col = tp->view.cols;
20cda6f2
JS
1119 rc = tty_port_install(&tp->port, driver, tty);
1120 if (rc) {
1121 raw3270_put_view(&tp->view);
1122 return rc;
1123 }
20cda6f2 1124 tty->driver_data = tp;
1da177e4
LT
1125 return 0;
1126}
1127
736c9fd2
MS
1128/*
1129 * This routine is called whenever a 3270 tty is opened.
1130 */
e6d98bb8 1131static int tty3270_open(struct tty_struct *tty, struct file *filp)
736c9fd2
MS
1132{
1133 struct tty3270 *tp = tty->driver_data;
1134 struct tty_port *port = &tp->port;
1135
1136 port->count++;
1137 tty_port_tty_set(port, tty);
1138 return 0;
1139}
1140
1da177e4
LT
1141/*
1142 * This routine is called when the 3270 tty is closed. We wait
1143 * for the remaining request to be completed. Then we clean up.
1144 */
e6d98bb8 1145static void tty3270_close(struct tty_struct *tty, struct file *filp)
1da177e4 1146{
881e18f9 1147 struct tty3270 *tp = tty->driver_data;
1da177e4
LT
1148
1149 if (tty->count > 1)
1150 return;
b512353c 1151 if (tp)
ba186e7d 1152 tty_port_tty_set(&tp->port, NULL);
1da177e4
LT
1153}
1154
20cda6f2
JS
1155static void tty3270_cleanup(struct tty_struct *tty)
1156{
1157 struct tty3270 *tp = tty->driver_data;
1158
b512353c
MS
1159 if (tp) {
1160 tty->driver_data = NULL;
20cda6f2 1161 raw3270_put_view(&tp->view);
b512353c 1162 }
20cda6f2
JS
1163}
1164
1da177e4
LT
1165/*
1166 * We always have room.
1167 */
e6d98bb8 1168static unsigned int tty3270_write_room(struct tty_struct *tty)
1da177e4
LT
1169{
1170 return INT_MAX;
1171}
1172
1173/*
1174 * Insert character into the screen at the current position with the
1175 * current color and highlight. This function does NOT do cursor movement.
1176 */
74c76c84 1177static void tty3270_put_character(struct tty3270 *tp, char ch)
1da177e4
LT
1178{
1179 struct tty3270_line *line;
1180 struct tty3270_cell *cell;
1181
9c138af9 1182 line = tty3270_get_write_line(tp, tp->cy);
1da177e4
LT
1183 if (line->len <= tp->cx) {
1184 while (line->len < tp->cx) {
1185 cell = line->cells + line->len;
6e49017c 1186 cell->character = ' ';
c2e9375e 1187 cell->attributes = tp->attributes;
1da177e4
LT
1188 line->len++;
1189 }
1190 line->len++;
1191 }
1192 cell = line->cells + tp->cx;
6e49017c 1193 cell->character = ch;
c2e9375e 1194 cell->attributes = tp->attributes;
9c138af9 1195 line->dirty = 1;
1da177e4
LT
1196}
1197
1da177e4
LT
1198/*
1199 * Do carriage return.
1200 */
e6d98bb8 1201static void tty3270_cr(struct tty3270 *tp)
1da177e4
LT
1202{
1203 tp->cx = 0;
1204}
1205
1206/*
1207 * Do line feed.
1208 */
e6d98bb8 1209static void tty3270_lf(struct tty3270 *tp)
1da177e4 1210{
9c138af9 1211 struct tty3270_line *line;
1da177e4
LT
1212 int i;
1213
9eb99b94 1214 if (tp->cy < tty3270_tty_rows(tp) - 1) {
1da177e4 1215 tp->cy++;
9c138af9
SS
1216 } else {
1217 tp->line_view_start = tty3270_line_increment(tp, tp->line_view_start, 1);
1218 tp->line_write_start = tty3270_line_increment(tp, tp->line_write_start, 1);
1219 for (i = 0; i < tty3270_tty_rows(tp); i++)
1220 tty3270_get_view_line(tp, i)->dirty = 1;
1da177e4 1221 }
9c138af9
SS
1222
1223 line = tty3270_get_write_line(tp, tp->cy);
1224 line->len = 0;
1225 line->dirty = 1;
1da177e4
LT
1226}
1227
e6d98bb8 1228static void tty3270_ri(struct tty3270 *tp)
1da177e4 1229{
9c138af9
SS
1230 if (tp->cy > 0)
1231 tp->cy--;
1da177e4
LT
1232}
1233
c2e9375e
SS
1234static void tty3270_reset_cell(struct tty3270 *tp, struct tty3270_cell *cell)
1235{
6e49017c 1236 cell->character = ' ';
c2e9375e
SS
1237 tty3270_reset_attributes(&cell->attributes);
1238}
1239
1da177e4
LT
1240/*
1241 * Insert characters at current position.
1242 */
e6d98bb8 1243static void tty3270_insert_characters(struct tty3270 *tp, int n)
1da177e4
LT
1244{
1245 struct tty3270_line *line;
1246 int k;
1247
9c138af9 1248 line = tty3270_get_write_line(tp, tp->cy);
c2e9375e
SS
1249 while (line->len < tp->cx)
1250 tty3270_reset_cell(tp, &line->cells[line->len++]);
1da177e4
LT
1251 if (n > tp->view.cols - tp->cx)
1252 n = tp->view.cols - tp->cx;
1253 k = min_t(int, line->len - tp->cx, tp->view.cols - tp->cx - n);
1254 while (k--)
1255 line->cells[tp->cx + n + k] = line->cells[tp->cx + k];
1256 line->len += n;
1257 if (line->len > tp->view.cols)
1258 line->len = tp->view.cols;
1259 while (n-- > 0) {
6e49017c 1260 line->cells[tp->cx + n].character = ' ';
c2e9375e 1261 line->cells[tp->cx + n].attributes = tp->attributes;
1da177e4
LT
1262 }
1263}
1264
1265/*
1266 * Delete characters at current position.
1267 */
e6d98bb8 1268static void tty3270_delete_characters(struct tty3270 *tp, int n)
1da177e4
LT
1269{
1270 struct tty3270_line *line;
1271 int i;
1272
9c138af9 1273 line = tty3270_get_write_line(tp, tp->cy);
1da177e4
LT
1274 if (line->len <= tp->cx)
1275 return;
1276 if (line->len - tp->cx <= n) {
1277 line->len = tp->cx;
1278 return;
1279 }
1280 for (i = tp->cx; i + n < line->len; i++)
1281 line->cells[i] = line->cells[i + n];
1282 line->len -= n;
1283}
1284
1285/*
1286 * Erase characters at current position.
1287 */
e6d98bb8 1288static void tty3270_erase_characters(struct tty3270 *tp, int n)
1da177e4
LT
1289{
1290 struct tty3270_line *line;
1291 struct tty3270_cell *cell;
1292
9c138af9 1293 line = tty3270_get_write_line(tp, tp->cy);
1da177e4
LT
1294 while (line->len > tp->cx && n-- > 0) {
1295 cell = line->cells + tp->cx++;
c2e9375e 1296 tty3270_reset_cell(tp, cell);
1da177e4
LT
1297 }
1298 tp->cx += n;
1299 tp->cx = min_t(int, tp->cx, tp->view.cols - 1);
1300}
1301
1302/*
1303 * Erase line, 3 different cases:
1304 * Esc [ 0 K Erase from current position to end of line inclusive
1305 * Esc [ 1 K Erase from beginning of line to current position inclusive
1306 * Esc [ 2 K Erase entire line (without moving cursor)
1307 */
e6d98bb8 1308static void tty3270_erase_line(struct tty3270 *tp, int mode)
1da177e4
LT
1309{
1310 struct tty3270_line *line;
1311 struct tty3270_cell *cell;
4043ea22 1312 int i, start, end;
1da177e4 1313
9c138af9 1314 line = tty3270_get_write_line(tp, tp->cy);
4043ea22 1315
815f3eee
SS
1316 switch (mode) {
1317 case 0:
4043ea22
SS
1318 start = tp->cx;
1319 end = tp->view.cols;
815f3eee
SS
1320 break;
1321 case 1:
4043ea22
SS
1322 start = 0;
1323 end = tp->cx;
815f3eee
SS
1324 break;
1325 case 2:
4043ea22
SS
1326 start = 0;
1327 end = tp->view.cols;
815f3eee
SS
1328 break;
1329 default:
1330 return;
1331 }
4043ea22
SS
1332
1333 for (i = start; i < end; i++) {
1334 cell = line->cells + i;
1335 tty3270_reset_cell(tp, cell);
1336 cell->attributes.b_color = tp->attributes.b_color;
1337 }
1338
1339 if (line->len <= end)
1340 line->len = end;
1da177e4
LT
1341}
1342
1343/*
1344 * Erase display, 3 different cases:
1345 * Esc [ 0 J Erase from current position to bottom of screen inclusive
1346 * Esc [ 1 J Erase from top of screen to current position inclusive
1347 * Esc [ 2 J Erase entire screen (without moving the cursor)
1348 */
e6d98bb8 1349static void tty3270_erase_display(struct tty3270 *tp, int mode)
1da177e4 1350{
9c138af9 1351 struct tty3270_line *line;
65b77ccb 1352 int i, start, end;
1da177e4 1353
65b77ccb
SS
1354 switch (mode) {
1355 case 0:
1da177e4 1356 tty3270_erase_line(tp, 0);
65b77ccb 1357 start = tp->cy + 1;
9eb99b94 1358 end = tty3270_tty_rows(tp);
65b77ccb
SS
1359 break;
1360 case 1:
1361 start = 0;
1362 end = tp->cy;
1da177e4 1363 tty3270_erase_line(tp, 1);
65b77ccb
SS
1364 break;
1365 case 2:
1366 start = 0;
9eb99b94 1367 end = tty3270_tty_rows(tp);
65b77ccb
SS
1368 break;
1369 default:
1370 return;
1371 }
1372 for (i = start; i < end; i++) {
9c138af9
SS
1373 line = tty3270_get_write_line(tp, i);
1374 line->len = 0;
1375 line->dirty = 1;
1da177e4 1376 }
1da177e4
LT
1377}
1378
1379/*
1380 * Set attributes found in an escape sequence.
1381 * Esc [ <attr> ; <attr> ; ... m
1382 */
e6d98bb8 1383static void tty3270_set_attributes(struct tty3270 *tp)
1da177e4 1384{
4043ea22 1385 static unsigned char colors[] = {
1da177e4
LT
1386 TAC_DEFAULT, TAC_RED, TAC_GREEN, TAC_YELLOW, TAC_BLUE,
1387 TAC_PINK, TAC_TURQ, TAC_WHITE, 0, TAC_DEFAULT
1388 };
1389 int i, attr;
1390
1391 for (i = 0; i <= tp->esc_npar; i++) {
1392 attr = tp->esc_par[i];
1393 switch (attr) {
1394 case 0: /* Reset */
c2e9375e 1395 tty3270_reset_attributes(&tp->attributes);
1da177e4
LT
1396 break;
1397 /* Highlight. */
1398 case 4: /* Start underlining. */
c2e9375e 1399 tp->attributes.highlight = TAX_UNDER;
1da177e4
LT
1400 break;
1401 case 5: /* Start blink. */
c2e9375e 1402 tp->attributes.highlight = TAX_BLINK;
1da177e4
LT
1403 break;
1404 case 7: /* Start reverse. */
c2e9375e 1405 tp->attributes.highlight = TAX_REVER;
1da177e4
LT
1406 break;
1407 case 24: /* End underlining */
c2e9375e
SS
1408 if (tp->attributes.highlight == TAX_UNDER)
1409 tp->attributes.highlight = TAX_RESET;
1da177e4
LT
1410 break;
1411 case 25: /* End blink. */
c2e9375e
SS
1412 if (tp->attributes.highlight == TAX_BLINK)
1413 tp->attributes.highlight = TAX_RESET;
1da177e4
LT
1414 break;
1415 case 27: /* End reverse. */
c2e9375e
SS
1416 if (tp->attributes.highlight == TAX_REVER)
1417 tp->attributes.highlight = TAX_RESET;
1da177e4
LT
1418 break;
1419 /* Foreground color. */
1420 case 30: /* Black */
1421 case 31: /* Red */
1422 case 32: /* Green */
1423 case 33: /* Yellow */
1424 case 34: /* Blue */
1425 case 35: /* Magenta */
1426 case 36: /* Cyan */
1427 case 37: /* White */
1428 case 39: /* Black */
4043ea22
SS
1429 tp->attributes.f_color = colors[attr - 30];
1430 break;
1431 /* Background color. */
1432 case 40: /* Black */
1433 case 41: /* Red */
1434 case 42: /* Green */
1435 case 43: /* Yellow */
1436 case 44: /* Blue */
1437 case 45: /* Magenta */
1438 case 46: /* Cyan */
1439 case 47: /* White */
1440 case 49: /* Black */
1441 tp->attributes.b_color = colors[attr - 40];
1da177e4
LT
1442 break;
1443 }
1444 }
1445}
1446
e6d98bb8 1447static inline int tty3270_getpar(struct tty3270 *tp, int ix)
1da177e4
LT
1448{
1449 return (tp->esc_par[ix] > 0) ? tp->esc_par[ix] : 1;
1450}
1451
e6d98bb8 1452static void tty3270_goto_xy(struct tty3270 *tp, int cx, int cy)
1da177e4 1453{
4043ea22
SS
1454 struct tty3270_line *line;
1455 struct tty3270_cell *cell;
364c8558
HC
1456 int max_cx = max(0, cx);
1457 int max_cy = max(0, cy);
1458
1459 tp->cx = min_t(int, tp->view.cols - 1, max_cx);
9c138af9 1460 line = tty3270_get_write_line(tp, tp->cy);
4043ea22
SS
1461 while (line->len < tp->cx) {
1462 cell = line->cells + line->len;
1463 cell->character = ' ';
1464 cell->attributes = tp->attributes;
1465 line->len++;
1466 }
9c138af9 1467 tp->cy = min_t(int, tty3270_tty_rows(tp) - 1, max_cy);
1da177e4
LT
1468}
1469
1470/*
1471 * Process escape sequences. Known sequences:
1472 * Esc 7 Save Cursor Position
1473 * Esc 8 Restore Cursor Position
1474 * Esc [ Pn ; Pn ; .. m Set attributes
1475 * Esc [ Pn ; Pn H Cursor Position
1476 * Esc [ Pn ; Pn f Cursor Position
1477 * Esc [ Pn A Cursor Up
1478 * Esc [ Pn B Cursor Down
1479 * Esc [ Pn C Cursor Forward
1480 * Esc [ Pn D Cursor Backward
1481 * Esc [ Pn G Cursor Horizontal Absolute
1482 * Esc [ Pn X Erase Characters
1483 * Esc [ Ps J Erase in Display
1484 * Esc [ Ps K Erase in Line
1485 * // FIXME: add all the new ones.
1486 *
1487 * Pn is a numeric parameter, a string of zero or more decimal digits.
1488 * Ps is a selective parameter.
1489 */
e6d98bb8 1490static void tty3270_escape_sequence(struct tty3270 *tp, char ch)
1da177e4 1491{
e4b57b93 1492 enum { ESnormal, ESesc, ESsquare, ESparen, ESgetpars };
1da177e4
LT
1493
1494 if (tp->esc_state == ESnormal) {
1495 if (ch == 0x1b)
1496 /* Starting new escape sequence. */
1497 tp->esc_state = ESesc;
1498 return;
1499 }
1500 if (tp->esc_state == ESesc) {
1501 tp->esc_state = ESnormal;
1502 switch (ch) {
1503 case '[':
1504 tp->esc_state = ESsquare;
1505 break;
e4b57b93
SS
1506 case '(':
1507 tp->esc_state = ESparen;
1508 break;
1da177e4
LT
1509 case 'E':
1510 tty3270_cr(tp);
1511 tty3270_lf(tp);
1512 break;
1513 case 'M':
1514 tty3270_ri(tp);
1515 break;
1516 case 'D':
1517 tty3270_lf(tp);
1518 break;
1519 case 'Z': /* Respond ID. */
ba186e7d 1520 kbd_puts_queue(&tp->port, "\033[?6c");
1da177e4
LT
1521 break;
1522 case '7': /* Save cursor position. */
1523 tp->saved_cx = tp->cx;
1524 tp->saved_cy = tp->cy;
c2e9375e 1525 tp->saved_attributes = tp->attributes;
1da177e4
LT
1526 break;
1527 case '8': /* Restore cursor position. */
1da177e4 1528 tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
c2e9375e 1529 tp->attributes = tp->saved_attributes;
1da177e4
LT
1530 break;
1531 case 'c': /* Reset terminal. */
1532 tp->cx = tp->saved_cx = 0;
1533 tp->cy = tp->saved_cy = 0;
c2e9375e
SS
1534 tty3270_reset_attributes(&tp->attributes);
1535 tty3270_reset_attributes(&tp->saved_attributes);
1da177e4
LT
1536 tty3270_erase_display(tp, 2);
1537 break;
1538 }
1539 return;
1540 }
e4b57b93
SS
1541
1542 switch (tp->esc_state) {
1543 case ESparen:
1544 tp->esc_state = ESnormal;
1545 switch (ch) {
1546 case 'B':
1547 tp->attributes.alternate_charset = 0;
1548 break;
1549 case '0':
1550 tp->attributes.alternate_charset = 1;
1551 break;
1552 }
1553 return;
1554 case ESsquare:
1da177e4
LT
1555 tp->esc_state = ESgetpars;
1556 memset(tp->esc_par, 0, sizeof(tp->esc_par));
1557 tp->esc_npar = 0;
1558 tp->esc_ques = (ch == '?');
1559 if (tp->esc_ques)
1560 return;
e4b57b93
SS
1561 fallthrough;
1562 case ESgetpars:
1da177e4
LT
1563 if (ch == ';' && tp->esc_npar < ESCAPE_NPAR - 1) {
1564 tp->esc_npar++;
1565 return;
1566 }
1567 if (ch >= '0' && ch <= '9') {
1568 tp->esc_par[tp->esc_npar] *= 10;
1569 tp->esc_par[tp->esc_npar] += ch - '0';
1570 return;
1571 }
e4b57b93
SS
1572 break;
1573 default:
1574 break;
1da177e4
LT
1575 }
1576 tp->esc_state = ESnormal;
1577 if (ch == 'n' && !tp->esc_ques) {
1578 if (tp->esc_par[0] == 5) /* Status report. */
ba186e7d 1579 kbd_puts_queue(&tp->port, "\033[0n");
1da177e4
LT
1580 else if (tp->esc_par[0] == 6) { /* Cursor report. */
1581 char buf[40];
1582 sprintf(buf, "\033[%d;%dR", tp->cy + 1, tp->cx + 1);
ba186e7d 1583 kbd_puts_queue(&tp->port, buf);
1da177e4
LT
1584 }
1585 return;
1586 }
1587 if (tp->esc_ques)
1588 return;
1589 switch (ch) {
1590 case 'm':
1591 tty3270_set_attributes(tp);
1592 break;
1593 case 'H': /* Set cursor position. */
1594 case 'f':
1595 tty3270_goto_xy(tp, tty3270_getpar(tp, 1) - 1,
1596 tty3270_getpar(tp, 0) - 1);
1597 break;
1598 case 'd': /* Set y position. */
1599 tty3270_goto_xy(tp, tp->cx, tty3270_getpar(tp, 0) - 1);
1600 break;
1601 case 'A': /* Cursor up. */
1602 case 'F':
1603 tty3270_goto_xy(tp, tp->cx, tp->cy - tty3270_getpar(tp, 0));
1604 break;
1605 case 'B': /* Cursor down. */
1606 case 'e':
1607 case 'E':
1608 tty3270_goto_xy(tp, tp->cx, tp->cy + tty3270_getpar(tp, 0));
1609 break;
1610 case 'C': /* Cursor forward. */
1611 case 'a':
1612 tty3270_goto_xy(tp, tp->cx + tty3270_getpar(tp, 0), tp->cy);
1613 break;
1614 case 'D': /* Cursor backward. */
1615 tty3270_goto_xy(tp, tp->cx - tty3270_getpar(tp, 0), tp->cy);
1616 break;
1617 case 'G': /* Set x position. */
1618 case '`':
1619 tty3270_goto_xy(tp, tty3270_getpar(tp, 0), tp->cy);
1620 break;
1621 case 'X': /* Erase Characters. */
1622 tty3270_erase_characters(tp, tty3270_getpar(tp, 0));
1623 break;
1624 case 'J': /* Erase display. */
1625 tty3270_erase_display(tp, tp->esc_par[0]);
1626 break;
1627 case 'K': /* Erase line. */
1628 tty3270_erase_line(tp, tp->esc_par[0]);
1629 break;
1630 case 'P': /* Delete characters. */
1631 tty3270_delete_characters(tp, tty3270_getpar(tp, 0));
1632 break;
1633 case '@': /* Insert characters. */
1634 tty3270_insert_characters(tp, tty3270_getpar(tp, 0));
1635 break;
1636 case 's': /* Save cursor position. */
1637 tp->saved_cx = tp->cx;
1638 tp->saved_cy = tp->cy;
c2e9375e 1639 tp->saved_attributes = tp->attributes;
1da177e4
LT
1640 break;
1641 case 'u': /* Restore cursor position. */
1da177e4 1642 tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
c2e9375e 1643 tp->attributes = tp->saved_attributes;
1da177e4
LT
1644 break;
1645 }
1646}
1647
1648/*
1649 * String write routine for 3270 ttys
1650 */
e6d98bb8
SS
1651static void tty3270_do_write(struct tty3270 *tp, struct tty_struct *tty,
1652 const unsigned char *buf, int count)
1da177e4
LT
1653{
1654 int i_msg, i;
1655
c17fe081 1656 spin_lock_irq(&tp->view.lock);
6e94dbc7 1657 for (i_msg = 0; !tty->flow.stopped && i_msg < count; i_msg++) {
1da177e4
LT
1658 if (tp->esc_state != 0) {
1659 /* Continue escape sequence. */
1660 tty3270_escape_sequence(tp, buf[i_msg]);
1661 continue;
1662 }
1663
1664 switch (buf[i_msg]) {
970cf9a9
SS
1665 case 0x00:
1666 break;
1da177e4
LT
1667 case 0x07: /* '\a' -- Alarm */
1668 tp->wcc |= TW_PLUSALARM;
1669 break;
1670 case 0x08: /* Backspace. */
1671 if (tp->cx > 0) {
1672 tp->cx--;
1673 tty3270_put_character(tp, ' ');
1674 }
1675 break;
1676 case 0x09: /* '\t' -- Tabulate */
1677 for (i = tp->cx % 8; i < 8; i++) {
1678 if (tp->cx >= tp->view.cols) {
1679 tty3270_cr(tp);
1680 tty3270_lf(tp);
1681 break;
1682 }
1683 tty3270_put_character(tp, ' ');
1684 tp->cx++;
1685 }
1686 break;
1687 case 0x0a: /* '\n' -- New Line */
1688 tty3270_cr(tp);
1689 tty3270_lf(tp);
1690 break;
1691 case 0x0c: /* '\f' -- Form Feed */
1692 tty3270_erase_display(tp, 2);
1693 tp->cx = tp->cy = 0;
1694 break;
1695 case 0x0d: /* '\r' -- Carriage Return */
1696 tp->cx = 0;
1697 break;
94dbb0a7
SS
1698 case 0x0e:
1699 tp->attributes.alternate_charset = 1;
1700 break;
1da177e4 1701 case 0x0f: /* SuSE "exit alternate mode" */
94dbb0a7 1702 tp->attributes.alternate_charset = 0;
1da177e4
LT
1703 break;
1704 case 0x1b: /* Start escape sequence. */
1705 tty3270_escape_sequence(tp, buf[i_msg]);
1706 break;
1707 default: /* Insert normal character. */
1708 if (tp->cx >= tp->view.cols) {
1709 tty3270_cr(tp);
1710 tty3270_lf(tp);
1711 }
1712 tty3270_put_character(tp, buf[i_msg]);
1713 tp->cx++;
1714 break;
1715 }
1716 }
1da177e4
LT
1717 /* Setup timer to update display after 1/10 second */
1718 if (!timer_pending(&tp->timer))
1719 tty3270_set_timer(tp, HZ/10);
1720
c17fe081 1721 spin_unlock_irq(&tp->view.lock);
1da177e4
LT
1722}
1723
1724/*
1725 * String write routine for 3270 ttys
1726 */
e6d98bb8
SS
1727static int tty3270_write(struct tty_struct *tty,
1728 const unsigned char *buf, int count)
1da177e4
LT
1729{
1730 struct tty3270 *tp;
1731
1732 tp = tty->driver_data;
1733 if (!tp)
1734 return 0;
1735 if (tp->char_count > 0) {
20acdfa8 1736 tty3270_do_write(tp, tty, tp->char_buf, tp->char_count);
1da177e4
LT
1737 tp->char_count = 0;
1738 }
20acdfa8 1739 tty3270_do_write(tp, tty, buf, count);
1da177e4
LT
1740 return count;
1741}
1742
1743/*
1744 * Put single characters to the ttys character buffer
1745 */
74c76c84 1746static int tty3270_put_char(struct tty_struct *tty, unsigned char ch)
1da177e4
LT
1747{
1748 struct tty3270 *tp;
1749
1750 tp = tty->driver_data;
74c76c84
HC
1751 if (!tp || tp->char_count >= TTY3270_CHAR_BUF_SIZE)
1752 return 0;
1753 tp->char_buf[tp->char_count++] = ch;
1754 return 1;
1da177e4
LT
1755}
1756
1757/*
1758 * Flush all characters from the ttys characeter buffer put there
1759 * by tty3270_put_char.
1760 */
e6d98bb8 1761static void tty3270_flush_chars(struct tty_struct *tty)
1da177e4
LT
1762{
1763 struct tty3270 *tp;
1764
1765 tp = tty->driver_data;
1766 if (!tp)
1767 return;
1768 if (tp->char_count > 0) {
20acdfa8 1769 tty3270_do_write(tp, tty, tp->char_buf, tp->char_count);
1da177e4
LT
1770 tp->char_count = 0;
1771 }
1772}
1773
1da177e4
LT
1774/*
1775 * Check for visible/invisible input switches
1776 */
e6d98bb8 1777static void tty3270_set_termios(struct tty_struct *tty, const struct ktermios *old)
1da177e4
LT
1778{
1779 struct tty3270 *tp;
1780 int new;
1781
1782 tp = tty->driver_data;
1783 if (!tp)
1784 return;
c17fe081 1785 spin_lock_irq(&tp->view.lock);
1da177e4
LT
1786 if (L_ICANON(tty)) {
1787 new = L_ECHO(tty) ? TF_INPUT: TF_INPUTN;
1788 if (new != tp->inattr) {
1789 tp->inattr = new;
d2c993d8 1790 tty3270_update_prompt(tp, NULL, 0);
1da177e4
LT
1791 tty3270_set_timer(tp, 1);
1792 }
1793 }
c17fe081 1794 spin_unlock_irq(&tp->view.lock);
1da177e4
LT
1795}
1796
1797/*
1798 * Disable reading from a 3270 tty
1799 */
e6d98bb8 1800static void tty3270_throttle(struct tty_struct *tty)
1da177e4
LT
1801{
1802 struct tty3270 *tp;
1803
1804 tp = tty->driver_data;
1805 if (!tp)
1806 return;
1807 tp->throttle = 1;
1808}
1809
1810/*
1811 * Enable reading from a 3270 tty
1812 */
e6d98bb8 1813static void tty3270_unthrottle(struct tty_struct *tty)
1da177e4
LT
1814{
1815 struct tty3270 *tp;
1816
1817 tp = tty->driver_data;
1818 if (!tp)
1819 return;
1820 tp->throttle = 0;
1821 if (tp->attn)
1822 tty3270_issue_read(tp, 1);
1823}
1824
1825/*
1826 * Hang up the tty device.
1827 */
e6d98bb8 1828static void tty3270_hangup(struct tty_struct *tty)
1da177e4 1829{
0c756914
MS
1830 struct tty3270 *tp;
1831
1832 tp = tty->driver_data;
1833 if (!tp)
1834 return;
c17fe081 1835 spin_lock_irq(&tp->view.lock);
0c756914
MS
1836 tp->cx = tp->saved_cx = 0;
1837 tp->cy = tp->saved_cy = 0;
c2e9375e
SS
1838 tty3270_reset_attributes(&tp->attributes);
1839 tty3270_reset_attributes(&tp->saved_attributes);
0c756914 1840 tty3270_blank_screen(tp);
0c756914 1841 tp->update_flags = TTY_UPDATE_ALL;
c17fe081 1842 spin_unlock_irq(&tp->view.lock);
0c756914 1843 tty3270_set_timer(tp, 1);
1da177e4
LT
1844}
1845
e6d98bb8 1846static void tty3270_wait_until_sent(struct tty_struct *tty, int timeout)
1da177e4
LT
1847{
1848}
1849
65c56e07
HC
1850static int tty3270_ioctl(struct tty_struct *tty, unsigned int cmd,
1851 unsigned long arg)
1da177e4
LT
1852{
1853 struct tty3270 *tp;
1854
1855 tp = tty->driver_data;
1856 if (!tp)
1857 return -ENODEV;
18900ca6 1858 if (tty_io_error(tty))
1da177e4 1859 return -EIO;
65c56e07 1860 return kbd_ioctl(tp->kbd, cmd, arg);
1da177e4
LT
1861}
1862
9d4bfd41 1863#ifdef CONFIG_COMPAT
65c56e07
HC
1864static long tty3270_compat_ioctl(struct tty_struct *tty,
1865 unsigned int cmd, unsigned long arg)
9d4bfd41
AB
1866{
1867 struct tty3270 *tp;
1868
1869 tp = tty->driver_data;
1870 if (!tp)
1871 return -ENODEV;
18900ca6 1872 if (tty_io_error(tty))
9d4bfd41 1873 return -EIO;
65c56e07 1874 return kbd_ioctl(tp->kbd, cmd, (unsigned long)compat_ptr(arg));
9d4bfd41
AB
1875}
1876#endif
1877
b68e31d0 1878static const struct tty_operations tty3270_ops = {
20cda6f2
JS
1879 .install = tty3270_install,
1880 .cleanup = tty3270_cleanup,
736c9fd2 1881 .open = tty3270_open,
1da177e4
LT
1882 .close = tty3270_close,
1883 .write = tty3270_write,
1884 .put_char = tty3270_put_char,
1885 .flush_chars = tty3270_flush_chars,
1886 .write_room = tty3270_write_room,
1da177e4
LT
1887 .throttle = tty3270_throttle,
1888 .unthrottle = tty3270_unthrottle,
1889 .hangup = tty3270_hangup,
1890 .wait_until_sent = tty3270_wait_until_sent,
1891 .ioctl = tty3270_ioctl,
9d4bfd41
AB
1892#ifdef CONFIG_COMPAT
1893 .compat_ioctl = tty3270_compat_ioctl,
1894#endif
1da177e4
LT
1895 .set_termios = tty3270_set_termios
1896};
1897
63df41d6 1898static void tty3270_create_cb(int minor)
c95571e6 1899{
1fcbba3d 1900 tty_register_device(tty3270_driver, minor - RAW3270_FIRSTMINOR, NULL);
c95571e6
MS
1901}
1902
63df41d6 1903static void tty3270_destroy_cb(int minor)
c95571e6 1904{
1fcbba3d 1905 tty_unregister_device(tty3270_driver, minor - RAW3270_FIRSTMINOR);
c95571e6
MS
1906}
1907
63df41d6 1908static struct raw3270_notifier tty3270_notifier =
c95571e6
MS
1909{
1910 .create = tty3270_create_cb,
1911 .destroy = tty3270_destroy_cb,
1912};
1913
1da177e4
LT
1914/*
1915 * 3270 tty registration code called from tty_init().
1916 * Most kernel services (incl. kmalloc) are available at this poimt.
1917 */
2b67fc46 1918static int __init tty3270_init(void)
1da177e4
LT
1919{
1920 struct tty_driver *driver;
1921 int ret;
1922
c95571e6
MS
1923 driver = tty_alloc_driver(RAW3270_MAXDEVS,
1924 TTY_DRIVER_REAL_RAW |
1925 TTY_DRIVER_DYNAMIC_DEV |
1926 TTY_DRIVER_RESET_TERMIOS);
1927 if (IS_ERR(driver))
1928 return PTR_ERR(driver);
1da177e4
LT
1929
1930 /*
1931 * Initialize the tty_driver structure
1932 * Entries in tty3270_driver that are NOT initialized:
1933 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1934 */
c95571e6
MS
1935 driver->driver_name = "tty3270";
1936 driver->name = "3270/tty";
1da177e4 1937 driver->major = IBM_TTY3270_MAJOR;
1fcbba3d
MS
1938 driver->minor_start = RAW3270_FIRSTMINOR;
1939 driver->name_base = RAW3270_FIRSTMINOR;
1da177e4
LT
1940 driver->type = TTY_DRIVER_TYPE_SYSTEM;
1941 driver->subtype = SYSTEM_TYPE_TTY;
1942 driver->init_termios = tty_std_termios;
1da177e4
LT
1943 tty_set_operations(driver, &tty3270_ops);
1944 ret = tty_register_driver(driver);
1945 if (ret) {
9f90a4dd 1946 tty_driver_kref_put(driver);
1da177e4
LT
1947 return ret;
1948 }
1949 tty3270_driver = driver;
c95571e6 1950 raw3270_register_notifier(&tty3270_notifier);
1da177e4
LT
1951 return 0;
1952}
1953
e6d98bb8 1954static void __exit tty3270_exit(void)
1da177e4
LT
1955{
1956 struct tty_driver *driver;
1957
c95571e6 1958 raw3270_unregister_notifier(&tty3270_notifier);
1da177e4 1959 driver = tty3270_driver;
d2c993d8 1960 tty3270_driver = NULL;
1da177e4 1961 tty_unregister_driver(driver);
9f90a4dd 1962 tty_driver_kref_put(driver);
1da177e4
LT
1963 tty3270_del_views();
1964}
1965
c17fe081
SS
1966#if IS_ENABLED(CONFIG_TN3270_CONSOLE)
1967static void
1968con3270_write(struct console *co, const char *str, unsigned int count)
1969{
1970 struct tty3270 *tp = co->data;
1971 unsigned long flags;
1972 char c;
1973
1974 spin_lock_irqsave(&tp->view.lock, flags);
1975 while (count--) {
1976 c = *str++;
1977 if (c == 0x0a) {
1978 tty3270_cr(tp);
1979 tty3270_lf(tp);
1980 } else {
1981 if (tp->cx >= tp->view.cols) {
1982 tty3270_cr(tp);
1983 tty3270_lf(tp);
1984 }
1985 tty3270_put_character(tp, c);
1986 tp->cx++;
1987 }
1988 }
1989 spin_unlock_irqrestore(&tp->view.lock, flags);
1990}
1991
1992static struct tty_driver *
1993con3270_device(struct console *c, int *index)
1994{
1995 *index = c->index;
1996 return tty3270_driver;
1997}
1998
1999static void
2000con3270_wait_write(struct tty3270 *tp)
2001{
2002 while (!tp->write) {
2003 raw3270_wait_cons_dev(tp->view.dev);
2004 barrier();
2005 }
2006}
2007
2008/*
2009 * The below function is called as a panic/reboot notifier before the
2010 * system enters a disabled, endless loop.
2011 *
2012 * Notice we must use the spin_trylock() alternative, to prevent lockups
2013 * in atomic context (panic routine runs with secondary CPUs, local IRQs
2014 * and preemption disabled).
2015 */
2016static int con3270_notify(struct notifier_block *self,
2017 unsigned long event, void *data)
2018{
2019 struct tty3270 *tp;
2020 unsigned long flags;
2021
2022 tp = condev;
2023 if (!tp->view.dev)
2024 return NOTIFY_DONE;
2025 if (!raw3270_view_lock_unavailable(&tp->view))
2026 raw3270_activate_view(&tp->view);
2027 if (!spin_trylock_irqsave(&tp->view.lock, flags))
2028 return NOTIFY_DONE;
2029 con3270_wait_write(tp);
2030 tp->nr_up = 0;
2031 while (tp->update_flags != 0) {
2032 spin_unlock_irqrestore(&tp->view.lock, flags);
2033 tty3270_update(&tp->timer);
2034 spin_lock_irqsave(&tp->view.lock, flags);
2035 con3270_wait_write(tp);
2036 }
2037 spin_unlock_irqrestore(&tp->view.lock, flags);
2038 return NOTIFY_DONE;
2039}
2040
2041static struct notifier_block on_panic_nb = {
2042 .notifier_call = con3270_notify,
2043 .priority = INT_MIN + 1, /* run the callback late */
2044};
2045
2046static struct notifier_block on_reboot_nb = {
2047 .notifier_call = con3270_notify,
2048 .priority = INT_MIN + 1, /* run the callback late */
2049};
2050
2051static struct console con3270 = {
2052 .name = "tty3270",
2053 .write = con3270_write,
2054 .device = con3270_device,
2055 .flags = CON_PRINTBUFFER,
2056};
2057
2058static int __init
2059con3270_init(void)
2060{
2061 struct raw3270_view *view;
2062 struct raw3270 *rp;
2063 struct tty3270 *tp;
2064 int rc;
2065
2066 /* Check if 3270 is to be the console */
2067 if (!CONSOLE_IS_3270)
2068 return -ENODEV;
2069
2070 /* Set the console mode for VM */
2071 if (MACHINE_IS_VM) {
2072 cpcmd("TERM CONMODE 3270", NULL, 0, NULL);
2073 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
2074 }
2075
2076 rp = raw3270_setup_console();
2077 if (IS_ERR(rp))
2078 return PTR_ERR(rp);
2079
2080 /* Check if the tty3270 is already there. */
2081 view = raw3270_find_view(&tty3270_fn, RAW3270_FIRSTMINOR);
2082 if (IS_ERR(view)) {
2083 rc = tty3270_create_view(0, &tp);
2084 if (rc)
2085 return rc;
2086 } else {
2087 tp = container_of(view, struct tty3270, view);
2088 tp->inattr = TF_INPUT;
2089 }
2090 con3270.data = tp;
2091 condev = tp;
2092 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
2093 register_reboot_notifier(&on_reboot_nb);
2094 register_console(&con3270);
2095 return 0;
2096}
2097console_initcall(con3270_init);
2098#endif
2099
1da177e4
LT
2100MODULE_LICENSE("GPL");
2101MODULE_ALIAS_CHARDEV_MAJOR(IBM_TTY3270_MAJOR);
2102
2103module_init(tty3270_init);
2104module_exit(tty3270_exit);