blob: 402eff3c1634710b90f7c9acf0ac9c08b1890d97 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * IBM/3270 Driver - tty functions.
3 *
4 * Author(s):
5 * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
6 * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02007 * -- Copyright IBM Corp. 2003
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/types.h>
12#include <linux/kdev_t.h>
13#include <linux/tty.h>
14#include <linux/vt_kern.h>
15#include <linux/init.h>
16#include <linux/console.h>
17#include <linux/interrupt.h>
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +010018#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <linux/slab.h>
21#include <linux/bootmem.h>
Arnd Bergmann9d4bfd42009-12-07 12:52:13 +010022#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/ccwdev.h>
25#include <asm/cio.h>
26#include <asm/ebcdic.h>
27#include <asm/uaccess.h>
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "raw3270.h"
Heiko Carstens364c8552007-10-12 16:11:35 +020030#include "tty3270.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "keyboard.h"
32
33#define TTY3270_CHAR_BUF_SIZE 256
34#define TTY3270_OUTPUT_BUFFER_SIZE 1024
35#define TTY3270_STRING_PAGES 5
36
37struct tty_driver *tty3270_driver;
38static int tty3270_max_index;
39
Heiko Carstens2b67fc42007-02-05 21:16:47 +010040static struct raw3270_fn tty3270_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42struct tty3270_cell {
43 unsigned char character;
44 unsigned char highlight;
45 unsigned char f_color;
46};
47
48struct tty3270_line {
49 struct tty3270_cell *cells;
50 int len;
51};
52
53#define ESCAPE_NPAR 8
54
55/*
56 * The main tty view data structure.
57 * FIXME:
58 * 1) describe line orientation & lines list concept against screen
59 * 2) describe conversion of screen to lines
60 * 3) describe line format.
61 */
62struct tty3270 {
63 struct raw3270_view view;
Jiri Slabyba186e72012-04-02 13:54:18 +020064 struct tty_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 void **freemem_pages; /* Array of pages used for freemem. */
66 struct list_head freemem; /* List of free memory for strings. */
67
68 /* Output stuff. */
69 struct list_head lines; /* List of lines. */
70 struct list_head update; /* List of lines to update. */
71 unsigned char wcc; /* Write control character. */
72 int nr_lines; /* # lines in list. */
73 int nr_up; /* # lines up in history. */
74 unsigned long update_flags; /* Update indication bits. */
75 struct string *status; /* Lower right of display. */
76 struct raw3270_request *write; /* Single write request. */
77 struct timer_list timer; /* Output delay timer. */
78
79 /* Current tty screen. */
80 unsigned int cx, cy; /* Current output position. */
81 unsigned int highlight; /* Blink/reverse/underscore */
82 unsigned int f_color; /* Foreground color */
83 struct tty3270_line *screen;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +010084 unsigned int n_model, n_cols, n_rows; /* New model & size */
85 struct work_struct resize_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 /* Input stuff. */
88 struct string *prompt; /* Output string for input area. */
89 struct string *input; /* Input string for read request. */
90 struct raw3270_request *read; /* Single read request. */
91 struct raw3270_request *kreset; /* Single keyboard reset request. */
92 unsigned char inattr; /* Visible/invisible input. */
93 int throttle, attn; /* tty throttle/unthrottle. */
94 struct tasklet_struct readlet; /* Tasklet to issue read request. */
95 struct kbd_data *kbd; /* key_maps stuff. */
96
97 /* Escape sequence parsing. */
98 int esc_state, esc_ques, esc_npar;
99 int esc_par[ESCAPE_NPAR];
100 unsigned int saved_cx, saved_cy;
101 unsigned int saved_highlight, saved_f_color;
102
103 /* Command recalling. */
104 struct list_head rcl_lines; /* List of recallable lines. */
105 struct list_head *rcl_walk; /* Point in rcl_lines list. */
106 int rcl_nr, rcl_max; /* Number/max number of rcl_lines. */
107
108 /* Character array for put_char/flush_chars. */
109 unsigned int char_count;
110 char char_buf[TTY3270_CHAR_BUF_SIZE];
111};
112
113/* tty3270->update_flags. See tty3270_update for details. */
114#define TTY_UPDATE_ERASE 1 /* Use EWRITEA instead of WRITE. */
115#define TTY_UPDATE_LIST 2 /* Update lines in tty3270->update. */
116#define TTY_UPDATE_INPUT 4 /* Update input line. */
117#define TTY_UPDATE_STATUS 8 /* Update status line. */
Martin Schwidefsky205d7ab2009-06-12 10:26:31 +0200118#define TTY_UPDATE_ALL 16 /* Recreate screen. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120static void tty3270_update(struct tty3270 *);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100121static void tty3270_resize_work(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123/*
124 * Setup timeout for a device. On timeout trigger an update.
125 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100126static void tty3270_set_timer(struct tty3270 *tp, int expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Martin Schwidefsky03439e72013-12-04 14:29:11 +0100128 mod_timer(&tp->timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131/*
132 * The input line are the two last lines of the screen.
133 */
134static void
135tty3270_update_prompt(struct tty3270 *tp, char *input, int count)
136{
137 struct string *line;
138 unsigned int off;
139
140 line = tp->prompt;
141 if (count != 0)
142 line->string[5] = TF_INMDT;
143 else
144 line->string[5] = tp->inattr;
145 if (count > tp->view.cols * 2 - 11)
146 count = tp->view.cols * 2 - 11;
147 memcpy(line->string + 6, input, count);
148 line->string[6 + count] = TO_IC;
149 /* Clear to end of input line. */
150 if (count < tp->view.cols * 2 - 11) {
151 line->string[7 + count] = TO_RA;
152 line->string[10 + count] = 0;
153 off = tp->view.cols * tp->view.rows - 9;
154 raw3270_buffer_address(tp->view.dev, line->string+count+8, off);
155 line->len = 11 + count;
156 } else
157 line->len = 7 + count;
158 tp->update_flags |= TTY_UPDATE_INPUT;
159}
160
161static void
162tty3270_create_prompt(struct tty3270 *tp)
163{
164 static const unsigned char blueprint[] =
165 { TO_SBA, 0, 0, 0x6e, TO_SF, TF_INPUT,
166 /* empty input string */
167 TO_IC, TO_RA, 0, 0, 0 };
168 struct string *line;
169 unsigned int offset;
170
171 line = alloc_string(&tp->freemem,
172 sizeof(blueprint) + tp->view.cols * 2 - 9);
173 tp->prompt = line;
174 tp->inattr = TF_INPUT;
175 /* Copy blueprint to status line */
176 memcpy(line->string, blueprint, sizeof(blueprint));
177 line->len = sizeof(blueprint);
178 /* Set output offsets. */
179 offset = tp->view.cols * (tp->view.rows - 2);
180 raw3270_buffer_address(tp->view.dev, line->string + 1, offset);
181 offset = tp->view.cols * tp->view.rows - 9;
182 raw3270_buffer_address(tp->view.dev, line->string + 8, offset);
183
184 /* Allocate input string for reading. */
185 tp->input = alloc_string(&tp->freemem, tp->view.cols * 2 - 9 + 6);
186}
187
188/*
189 * The status line is the last line of the screen. It shows the string
190 * "Running"/"Holding" in the lower right corner of the screen.
191 */
192static void
193tty3270_update_status(struct tty3270 * tp)
194{
195 char *str;
196
197 str = (tp->nr_up != 0) ? "History" : "Running";
198 memcpy(tp->status->string + 8, str, 7);
199 codepage_convert(tp->view.ascebc, tp->status->string + 8, 7);
200 tp->update_flags |= TTY_UPDATE_STATUS;
201}
202
203static void
204tty3270_create_status(struct tty3270 * tp)
205{
206 static const unsigned char blueprint[] =
207 { TO_SBA, 0, 0, TO_SF, TF_LOG, TO_SA, TAT_COLOR, TAC_GREEN,
208 0, 0, 0, 0, 0, 0, 0, TO_SF, TF_LOG, TO_SA, TAT_COLOR,
209 TAC_RESET };
210 struct string *line;
211 unsigned int offset;
212
213 line = alloc_string(&tp->freemem,sizeof(blueprint));
214 tp->status = line;
215 /* Copy blueprint to status line */
216 memcpy(line->string, blueprint, sizeof(blueprint));
217 /* Set address to start of status string (= last 9 characters). */
218 offset = tp->view.cols * tp->view.rows - 9;
219 raw3270_buffer_address(tp->view.dev, line->string + 1, offset);
220}
221
222/*
223 * Set output offsets to 3270 datastream fragment of a tty string.
224 * (TO_SBA offset at the start and TO_RA offset at the end of the string)
225 */
226static void
227tty3270_update_string(struct tty3270 *tp, struct string *line, int nr)
228{
229 unsigned char *cp;
230
231 raw3270_buffer_address(tp->view.dev, line->string + 1,
232 tp->view.cols * nr);
233 cp = line->string + line->len - 4;
234 if (*cp == TO_RA)
235 raw3270_buffer_address(tp->view.dev, cp + 1,
236 tp->view.cols * (nr + 1));
237}
238
239/*
240 * Rebuild update list to print all lines.
241 */
242static void
243tty3270_rebuild_update(struct tty3270 *tp)
244{
245 struct string *s, *n;
246 int line, nr_up;
247
248 /*
249 * Throw away update list and create a new one,
250 * containing all lines that will fit on the screen.
251 */
252 list_for_each_entry_safe(s, n, &tp->update, update)
253 list_del_init(&s->update);
254 line = tp->view.rows - 3;
255 nr_up = tp->nr_up;
256 list_for_each_entry_reverse(s, &tp->lines, list) {
257 if (nr_up > 0) {
258 nr_up--;
259 continue;
260 }
261 tty3270_update_string(tp, s, line);
262 list_add(&s->update, &tp->update);
263 if (--line < 0)
264 break;
265 }
266 tp->update_flags |= TTY_UPDATE_LIST;
267}
268
269/*
270 * Alloc string for size bytes. If there is not enough room in
271 * freemem, free strings until there is room.
272 */
273static struct string *
274tty3270_alloc_string(struct tty3270 *tp, size_t size)
275{
276 struct string *s, *n;
277
278 s = alloc_string(&tp->freemem, size);
279 if (s)
280 return s;
281 list_for_each_entry_safe(s, n, &tp->lines, list) {
282 BUG_ON(tp->nr_lines <= tp->view.rows - 2);
283 list_del(&s->list);
284 if (!list_empty(&s->update))
285 list_del(&s->update);
286 tp->nr_lines--;
287 if (free_string(&tp->freemem, s) >= size)
288 break;
289 }
290 s = alloc_string(&tp->freemem, size);
291 BUG_ON(!s);
292 if (tp->nr_up != 0 &&
293 tp->nr_up + tp->view.rows - 2 >= tp->nr_lines) {
294 tp->nr_up = tp->nr_lines - tp->view.rows + 2;
295 tty3270_rebuild_update(tp);
296 tty3270_update_status(tp);
297 }
298 return s;
299}
300
301/*
302 * Add an empty line to the list.
303 */
304static void
305tty3270_blank_line(struct tty3270 *tp)
306{
307 static const unsigned char blueprint[] =
308 { TO_SBA, 0, 0, TO_SA, TAT_EXTHI, TAX_RESET,
309 TO_SA, TAT_COLOR, TAC_RESET, TO_RA, 0, 0, 0 };
310 struct string *s;
311
312 s = tty3270_alloc_string(tp, sizeof(blueprint));
313 memcpy(s->string, blueprint, sizeof(blueprint));
314 s->len = sizeof(blueprint);
315 list_add_tail(&s->list, &tp->lines);
316 tp->nr_lines++;
317 if (tp->nr_up != 0)
318 tp->nr_up++;
319}
320
321/*
Martin Schwidefsky7e36eff2016-05-02 15:07:00 +0200322 * Create a blank screen and remove all lines from the history.
323 */
324static void
325tty3270_blank_screen(struct tty3270 *tp)
326{
327 struct string *s, *n;
328 int i;
329
330 for (i = 0; i < tp->view.rows - 2; i++)
331 tp->screen[i].len = 0;
332 tp->nr_up = 0;
333 list_for_each_entry_safe(s, n, &tp->lines, list) {
334 list_del(&s->list);
335 if (!list_empty(&s->update))
336 list_del(&s->update);
337 tp->nr_lines--;
338 free_string(&tp->freemem, s);
339 }
340}
341
342/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 * Write request completion callback.
344 */
345static void
346tty3270_write_callback(struct raw3270_request *rq, void *data)
347{
Jiri Slaby881e18f2012-04-02 13:54:16 +0200348 struct tty3270 *tp = container_of(rq->view, struct tty3270, view);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (rq->rc != 0) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300351 /* Write wasn't successful. Refresh all. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 tp->update_flags = TTY_UPDATE_ALL;
353 tty3270_set_timer(tp, 1);
354 }
355 raw3270_request_reset(rq);
356 xchg(&tp->write, rq);
357}
358
359/*
360 * Update 3270 display.
361 */
362static void
363tty3270_update(struct tty3270 *tp)
364{
365 static char invalid_sba[2] = { 0xff, 0xff };
366 struct raw3270_request *wrq;
367 unsigned long updated;
368 struct string *s, *n;
369 char *sba, *str;
370 int rc, len;
371
372 wrq = xchg(&tp->write, 0);
373 if (!wrq) {
374 tty3270_set_timer(tp, 1);
375 return;
376 }
377
378 spin_lock(&tp->view.lock);
379 updated = 0;
Martin Schwidefsky205d7ab2009-06-12 10:26:31 +0200380 if (tp->update_flags & TTY_UPDATE_ALL) {
381 tty3270_rebuild_update(tp);
382 tty3270_update_status(tp);
383 tp->update_flags = TTY_UPDATE_ERASE | TTY_UPDATE_LIST |
384 TTY_UPDATE_INPUT | TTY_UPDATE_STATUS;
385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (tp->update_flags & TTY_UPDATE_ERASE) {
387 /* Use erase write alternate to erase display. */
388 raw3270_request_set_cmd(wrq, TC_EWRITEA);
389 updated |= TTY_UPDATE_ERASE;
390 } else
391 raw3270_request_set_cmd(wrq, TC_WRITE);
392
393 raw3270_request_add_data(wrq, &tp->wcc, 1);
394 tp->wcc = TW_NONE;
395
396 /*
397 * Update status line.
398 */
399 if (tp->update_flags & TTY_UPDATE_STATUS)
400 if (raw3270_request_add_data(wrq, tp->status->string,
401 tp->status->len) == 0)
402 updated |= TTY_UPDATE_STATUS;
403
404 /*
405 * Write input line.
406 */
407 if (tp->update_flags & TTY_UPDATE_INPUT)
408 if (raw3270_request_add_data(wrq, tp->prompt->string,
409 tp->prompt->len) == 0)
410 updated |= TTY_UPDATE_INPUT;
411
412 sba = invalid_sba;
413
414 if (tp->update_flags & TTY_UPDATE_LIST) {
415 /* Write strings in the update list to the screen. */
416 list_for_each_entry_safe(s, n, &tp->update, update) {
417 str = s->string;
418 len = s->len;
419 /*
420 * Skip TO_SBA at the start of the string if the
421 * last output position matches the start address
422 * of this line.
423 */
424 if (s->string[1] == sba[0] && s->string[2] == sba[1])
425 str += 3, len -= 3;
426 if (raw3270_request_add_data(wrq, str, len) != 0)
427 break;
428 list_del_init(&s->update);
Martin Schwidefsky2e63a3a2016-04-28 16:32:13 +0200429 if (s->string[s->len - 4] == TO_RA)
430 sba = s->string + s->len - 3;
431 else
432 sba = invalid_sba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434 if (list_empty(&tp->update))
435 updated |= TTY_UPDATE_LIST;
436 }
437 wrq->callback = tty3270_write_callback;
438 rc = raw3270_start(&tp->view, wrq);
439 if (rc == 0) {
440 tp->update_flags &= ~updated;
441 if (tp->update_flags)
442 tty3270_set_timer(tp, 1);
443 } else {
444 raw3270_request_reset(wrq);
445 xchg(&tp->write, wrq);
446 }
447 spin_unlock(&tp->view.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450/*
451 * Command recalling.
452 */
453static void
454tty3270_rcl_add(struct tty3270 *tp, char *input, int len)
455{
456 struct string *s;
457
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200458 tp->rcl_walk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (len <= 0)
460 return;
461 if (tp->rcl_nr >= tp->rcl_max) {
462 s = list_entry(tp->rcl_lines.next, struct string, list);
463 list_del(&s->list);
464 free_string(&tp->freemem, s);
465 tp->rcl_nr--;
466 }
467 s = tty3270_alloc_string(tp, len);
468 memcpy(s->string, input, len);
469 list_add_tail(&s->list, &tp->rcl_lines);
470 tp->rcl_nr++;
471}
472
473static void
474tty3270_rcl_backward(struct kbd_data *kbd)
475{
Jiri Slabyba186e72012-04-02 13:54:18 +0200476 struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 struct string *s;
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 spin_lock_bh(&tp->view.lock);
480 if (tp->inattr == TF_INPUT) {
481 if (tp->rcl_walk && tp->rcl_walk->prev != &tp->rcl_lines)
482 tp->rcl_walk = tp->rcl_walk->prev;
483 else if (!list_empty(&tp->rcl_lines))
484 tp->rcl_walk = tp->rcl_lines.prev;
485 s = tp->rcl_walk ?
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200486 list_entry(tp->rcl_walk, struct string, list) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (tp->rcl_walk) {
488 s = list_entry(tp->rcl_walk, struct string, list);
489 tty3270_update_prompt(tp, s->string, s->len);
490 } else
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200491 tty3270_update_prompt(tp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 tty3270_set_timer(tp, 1);
493 }
494 spin_unlock_bh(&tp->view.lock);
495}
496
497/*
498 * Deactivate tty view.
499 */
500static void
501tty3270_exit_tty(struct kbd_data *kbd)
502{
Jiri Slabyba186e72012-04-02 13:54:18 +0200503 struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 raw3270_deactivate_view(&tp->view);
506}
507
508/*
509 * Scroll forward in history.
510 */
511static void
512tty3270_scroll_forward(struct kbd_data *kbd)
513{
Jiri Slabyba186e72012-04-02 13:54:18 +0200514 struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 int nr_up;
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 spin_lock_bh(&tp->view.lock);
518 nr_up = tp->nr_up - tp->view.rows + 2;
519 if (nr_up < 0)
520 nr_up = 0;
521 if (nr_up != tp->nr_up) {
522 tp->nr_up = nr_up;
523 tty3270_rebuild_update(tp);
524 tty3270_update_status(tp);
525 tty3270_set_timer(tp, 1);
526 }
527 spin_unlock_bh(&tp->view.lock);
528}
529
530/*
531 * Scroll backward in history.
532 */
533static void
534tty3270_scroll_backward(struct kbd_data *kbd)
535{
Jiri Slabyba186e72012-04-02 13:54:18 +0200536 struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int nr_up;
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 spin_lock_bh(&tp->view.lock);
540 nr_up = tp->nr_up + tp->view.rows - 2;
541 if (nr_up + tp->view.rows - 2 > tp->nr_lines)
542 nr_up = tp->nr_lines - tp->view.rows + 2;
543 if (nr_up != tp->nr_up) {
544 tp->nr_up = nr_up;
545 tty3270_rebuild_update(tp);
546 tty3270_update_status(tp);
547 tty3270_set_timer(tp, 1);
548 }
549 spin_unlock_bh(&tp->view.lock);
550}
551
552/*
553 * Pass input line to tty.
554 */
555static void
556tty3270_read_tasklet(struct raw3270_request *rrq)
557{
558 static char kreset_data = TW_KR;
Jiri Slaby881e18f2012-04-02 13:54:16 +0200559 struct tty3270 *tp = container_of(rrq->view, struct tty3270, view);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 char *input;
561 int len;
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 spin_lock_bh(&tp->view.lock);
564 /*
565 * Two AID keys are special: For 0x7d (enter) the input line
566 * has to be emitted to the tty and for 0x6d the screen
567 * needs to be redrawn.
568 */
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200569 input = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 len = 0;
571 if (tp->input->string[0] == 0x7d) {
572 /* Enter: write input to tty. */
573 input = tp->input->string + 6;
574 len = tp->input->len - 6 - rrq->rescnt;
575 if (tp->inattr != TF_INPUTN)
576 tty3270_rcl_add(tp, input, len);
577 if (tp->nr_up > 0) {
578 tp->nr_up = 0;
579 tty3270_rebuild_update(tp);
580 tty3270_update_status(tp);
581 }
582 /* Clear input area. */
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200583 tty3270_update_prompt(tp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 tty3270_set_timer(tp, 1);
585 } else if (tp->input->string[0] == 0x6d) {
586 /* Display has been cleared. Redraw. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 tp->update_flags = TTY_UPDATE_ALL;
588 tty3270_set_timer(tp, 1);
589 }
590 spin_unlock_bh(&tp->view.lock);
591
592 /* Start keyboard reset command. */
593 raw3270_request_reset(tp->kreset);
594 raw3270_request_set_cmd(tp->kreset, TC_WRITE);
595 raw3270_request_add_data(tp->kreset, &kreset_data, 1);
596 raw3270_start(&tp->view, tp->kreset);
597
Jiri Slabyba186e72012-04-02 13:54:18 +0200598 while (len-- > 0)
599 kbd_keycode(tp->kbd, *input++);
600 /* Emit keycode for AID byte. */
601 kbd_keycode(tp->kbd, 256 + tp->input->string[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 raw3270_request_reset(rrq);
604 xchg(&tp->read, rrq);
605 raw3270_put_view(&tp->view);
606}
607
608/*
609 * Read request completion callback.
610 */
611static void
612tty3270_read_callback(struct raw3270_request *rq, void *data)
613{
Jiri Slaby881e18f2012-04-02 13:54:16 +0200614 struct tty3270 *tp = container_of(rq->view, struct tty3270, view);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 raw3270_get_view(rq->view);
616 /* Schedule tasklet to pass input to tty. */
Jiri Slaby881e18f2012-04-02 13:54:16 +0200617 tasklet_schedule(&tp->readlet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
620/*
621 * Issue a read request. Call with device lock.
622 */
623static void
624tty3270_issue_read(struct tty3270 *tp, int lock)
625{
626 struct raw3270_request *rrq;
627 int rc;
628
629 rrq = xchg(&tp->read, 0);
630 if (!rrq)
631 /* Read already scheduled. */
632 return;
633 rrq->callback = tty3270_read_callback;
634 rrq->callback_data = tp;
635 raw3270_request_set_cmd(rrq, TC_READMOD);
636 raw3270_request_set_data(rrq, tp->input->string, tp->input->len);
637 /* Issue the read modified request. */
638 if (lock) {
639 rc = raw3270_start(&tp->view, rrq);
640 } else
641 rc = raw3270_start_irq(&tp->view, rrq);
642 if (rc) {
643 raw3270_request_reset(rrq);
644 xchg(&tp->read, rrq);
645 }
646}
647
648/*
649 * Switch to the tty view.
650 */
651static int
652tty3270_activate(struct raw3270_view *view)
653{
Jiri Slaby881e18f2012-04-02 13:54:16 +0200654 struct tty3270 *tp = container_of(view, struct tty3270, view);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 tp->update_flags = TTY_UPDATE_ALL;
657 tty3270_set_timer(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return 0;
659}
660
661static void
662tty3270_deactivate(struct raw3270_view *view)
663{
Jiri Slaby881e18f2012-04-02 13:54:16 +0200664 struct tty3270 *tp = container_of(view, struct tty3270, view);
Martin Schwidefsky205d7ab2009-06-12 10:26:31 +0200665
Martin Schwidefsky205d7ab2009-06-12 10:26:31 +0200666 del_timer(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
Martin Schwidefsky8340ab62016-05-02 14:53:29 +0200669static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670tty3270_irq(struct tty3270 *tp, struct raw3270_request *rq, struct irb *irb)
671{
672 /* Handle ATTN. Schedule tasklet to read aid. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200673 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (!tp->throttle)
675 tty3270_issue_read(tp, 0);
676 else
677 tp->attn = 1;
678 }
679
680 if (rq) {
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200681 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 rq->rc = -EIO;
683 else
684 /* Normal end. Copy residual count. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200685 rq->rescnt = irb->scsw.cmd.count;
Martin Schwidefsky05bfd702015-08-19 16:50:10 +0200686 } else if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
687 /* Interrupt without an outstanding request -> update all */
688 tp->update_flags = TTY_UPDATE_ALL;
689 tty3270_set_timer(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
693/*
694 * Allocate tty3270 structure.
695 */
696static struct tty3270 *
697tty3270_alloc_view(void)
698{
699 struct tty3270 *tp;
700 int pages;
701
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800702 tp = kzalloc(sizeof(struct tty3270), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (!tp)
704 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 tp->freemem_pages =
706 kmalloc(sizeof(void *) * TTY3270_STRING_PAGES, GFP_KERNEL);
707 if (!tp->freemem_pages)
708 goto out_tp;
709 INIT_LIST_HEAD(&tp->freemem);
Jiri Slaby9d2ae232012-04-02 13:54:15 +0200710 INIT_LIST_HEAD(&tp->lines);
711 INIT_LIST_HEAD(&tp->update);
712 INIT_LIST_HEAD(&tp->rcl_lines);
713 tp->rcl_max = 20;
Jiri Slaby9d2ae232012-04-02 13:54:15 +0200714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 for (pages = 0; pages < TTY3270_STRING_PAGES; pages++) {
716 tp->freemem_pages[pages] = (void *)
717 __get_free_pages(GFP_KERNEL|GFP_DMA, 0);
718 if (!tp->freemem_pages[pages])
719 goto out_pages;
720 add_string_memory(&tp->freemem,
721 tp->freemem_pages[pages], PAGE_SIZE);
722 }
723 tp->write = raw3270_request_alloc(TTY3270_OUTPUT_BUFFER_SIZE);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800724 if (IS_ERR(tp->write))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 goto out_pages;
726 tp->read = raw3270_request_alloc(0);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800727 if (IS_ERR(tp->read))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 goto out_write;
729 tp->kreset = raw3270_request_alloc(1);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800730 if (IS_ERR(tp->kreset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 goto out_read;
732 tp->kbd = kbd_alloc();
733 if (!tp->kbd)
734 goto out_reset;
Martin Schwidefsky57985d72013-01-08 15:20:05 +0100735
736 tty_port_init(&tp->port);
737 setup_timer(&tp->timer, (void (*)(unsigned long)) tty3270_update,
738 (unsigned long) tp);
739 tasklet_init(&tp->readlet,
740 (void (*)(unsigned long)) tty3270_read_tasklet,
741 (unsigned long) tp->read);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100742 INIT_WORK(&tp->resize_work, tty3270_resize_work);
Martin Schwidefsky57985d72013-01-08 15:20:05 +0100743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return tp;
745
746out_reset:
747 raw3270_request_free(tp->kreset);
748out_read:
749 raw3270_request_free(tp->read);
750out_write:
751 raw3270_request_free(tp->write);
752out_pages:
753 while (pages--)
754 free_pages((unsigned long) tp->freemem_pages[pages], 0);
755 kfree(tp->freemem_pages);
Jiri Slaby191c5f12012-11-15 09:49:56 +0100756 tty_port_destroy(&tp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757out_tp:
758 kfree(tp);
759out_err:
760 return ERR_PTR(-ENOMEM);
761}
762
763/*
764 * Free tty3270 structure.
765 */
766static void
767tty3270_free_view(struct tty3270 *tp)
768{
769 int pages;
770
771 kbd_free(tp->kbd);
772 raw3270_request_free(tp->kreset);
773 raw3270_request_free(tp->read);
774 raw3270_request_free(tp->write);
775 for (pages = 0; pages < TTY3270_STRING_PAGES; pages++)
776 free_pages((unsigned long) tp->freemem_pages[pages], 0);
777 kfree(tp->freemem_pages);
Jiri Slaby191c5f12012-11-15 09:49:56 +0100778 tty_port_destroy(&tp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 kfree(tp);
780}
781
782/*
783 * Allocate tty3270 screen.
784 */
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100785static struct tty3270_line *
786tty3270_alloc_screen(unsigned int rows, unsigned int cols)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100788 struct tty3270_line *screen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 unsigned long size;
790 int lines;
791
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100792 size = sizeof(struct tty3270_line) * (rows - 2);
793 screen = kzalloc(size, GFP_KERNEL);
794 if (!screen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 goto out_err;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100796 for (lines = 0; lines < rows - 2; lines++) {
797 size = sizeof(struct tty3270_cell) * cols;
798 screen[lines].cells = kzalloc(size, GFP_KERNEL);
799 if (!screen[lines].cells)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 goto out_screen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100802 return screen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803out_screen:
804 while (lines--)
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100805 kfree(screen[lines].cells);
806 kfree(screen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807out_err:
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100808 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
811/*
812 * Free tty3270 screen.
813 */
814static void
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100815tty3270_free_screen(struct tty3270_line *screen, unsigned int rows)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 int lines;
818
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100819 for (lines = 0; lines < rows - 2; lines++)
820 kfree(screen[lines].cells);
821 kfree(screen);
822}
823
824/*
825 * Resize tty3270 screen
826 */
827static void tty3270_resize_work(struct work_struct *work)
828{
829 struct tty3270 *tp = container_of(work, struct tty3270, resize_work);
830 struct tty3270_line *screen, *oscreen;
831 struct tty_struct *tty;
832 unsigned int orows;
833 struct winsize ws;
834
835 screen = tty3270_alloc_screen(tp->n_rows, tp->n_cols);
Wei Yongjun8f0ba632013-09-11 06:49:20 +0800836 if (IS_ERR(screen))
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100837 return;
838 /* Switch to new output size */
839 spin_lock_bh(&tp->view.lock);
Martin Schwidefsky7e36eff2016-05-02 15:07:00 +0200840 tty3270_blank_screen(tp);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100841 oscreen = tp->screen;
842 orows = tp->view.rows;
843 tp->view.model = tp->n_model;
844 tp->view.rows = tp->n_rows;
845 tp->view.cols = tp->n_cols;
846 tp->screen = screen;
847 free_string(&tp->freemem, tp->prompt);
848 free_string(&tp->freemem, tp->status);
849 tty3270_create_prompt(tp);
850 tty3270_create_status(tp);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100851 while (tp->nr_lines < tp->view.rows - 2)
852 tty3270_blank_line(tp);
853 tp->update_flags = TTY_UPDATE_ALL;
854 spin_unlock_bh(&tp->view.lock);
855 tty3270_free_screen(oscreen, orows);
856 tty3270_set_timer(tp, 1);
857 /* Informat tty layer about new size */
858 tty = tty_port_tty_get(&tp->port);
859 if (!tty)
860 return;
861 ws.ws_row = tp->view.rows - 2;
862 ws.ws_col = tp->view.cols;
863 tty_do_resize(tty, &ws);
Martin Schwidefsky5ff04fe2016-04-27 14:32:06 +0200864 tty_kref_put(tty);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100865}
866
867static void
868tty3270_resize(struct raw3270_view *view, int model, int rows, int cols)
869{
870 struct tty3270 *tp = container_of(view, struct tty3270, view);
871
Martin Schwidefsky7e36eff2016-05-02 15:07:00 +0200872 if (tp->n_model == model && tp->n_rows == rows && tp->n_cols == cols)
873 return;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100874 tp->n_model = model;
875 tp->n_rows = rows;
876 tp->n_cols = cols;
877 schedule_work(&tp->resize_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
880/*
881 * Unlink tty3270 data structure from tty.
882 */
883static void
884tty3270_release(struct raw3270_view *view)
885{
Jiri Slaby881e18f2012-04-02 13:54:16 +0200886 struct tty3270 *tp = container_of(view, struct tty3270, view);
Jiri Slabyba186e72012-04-02 13:54:18 +0200887 struct tty_struct *tty = tty_port_tty_get(&tp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (tty) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200890 tty->driver_data = NULL;
Jiri Slabyba186e72012-04-02 13:54:18 +0200891 tty_port_tty_set(&tp->port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 tty_hangup(tty);
893 raw3270_put_view(&tp->view);
Jiri Slabyba186e72012-04-02 13:54:18 +0200894 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896}
897
898/*
899 * Free tty3270 data structure
900 */
901static void
902tty3270_free(struct raw3270_view *view)
903{
Jiri Slaby881e18f2012-04-02 13:54:16 +0200904 struct tty3270 *tp = container_of(view, struct tty3270, view);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100905
Martin Schwidefsky03439e72013-12-04 14:29:11 +0100906 del_timer_sync(&tp->timer);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100907 tty3270_free_screen(tp->screen, tp->view.rows);
Jiri Slaby881e18f2012-04-02 13:54:16 +0200908 tty3270_free_view(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
911/*
912 * Delayed freeing of tty3270 views.
913 */
914static void
915tty3270_del_views(void)
916{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 int i;
918
Martin Schwidefskyc95571e2013-01-08 15:31:11 +0100919 for (i = RAW3270_FIRSTMINOR; i <= tty3270_max_index; i++) {
920 struct raw3270_view *view = raw3270_find_view(&tty3270_fn, i);
Jiri Slaby881e18f2012-04-02 13:54:16 +0200921 if (!IS_ERR(view))
922 raw3270_del_view(view);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924}
925
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100926static struct raw3270_fn tty3270_fn = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 .activate = tty3270_activate,
928 .deactivate = tty3270_deactivate,
929 .intv = (void *) tty3270_irq,
930 .release = tty3270_release,
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100931 .free = tty3270_free,
932 .resize = tty3270_resize
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933};
934
935/*
Jiri Slaby20cda6f2012-08-07 21:48:03 +0200936 * This routine is called whenever a 3270 tty is opened first time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 */
Jiri Slaby20cda6f2012-08-07 21:48:03 +0200938static int tty3270_install(struct tty_driver *driver, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Jiri Slaby881e18f2012-04-02 13:54:16 +0200940 struct raw3270_view *view;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 struct tty3270 *tp;
942 int i, rc;
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /* Check if the tty3270 is already there. */
Martin Schwidefsky1fcbba32013-03-21 13:07:18 +0100945 view = raw3270_find_view(&tty3270_fn, tty->index + RAW3270_FIRSTMINOR);
Jiri Slaby881e18f2012-04-02 13:54:16 +0200946 if (!IS_ERR(view)) {
947 tp = container_of(view, struct tty3270, view);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 tty->driver_data = tp;
949 tty->winsize.ws_row = tp->view.rows - 2;
950 tty->winsize.ws_col = tp->view.cols;
Jiri Slabyd6c53c02013-01-03 15:53:05 +0100951 tp->port.low_latency = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 tp->inattr = TF_INPUT;
Martin Schwidefskyb5123532016-05-02 13:49:28 +0200953 goto port_install;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
Martin Schwidefsky1fcbba32013-03-21 13:07:18 +0100955 if (tty3270_max_index < tty->index + 1)
956 tty3270_max_index = tty->index + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 /* Allocate tty3270 structure on first open. */
959 tp = tty3270_alloc_view();
960 if (IS_ERR(tp))
961 return PTR_ERR(tp);
962
Martin Schwidefsky1fcbba32013-03-21 13:07:18 +0100963 rc = raw3270_add_view(&tp->view, &tty3270_fn,
964 tty->index + RAW3270_FIRSTMINOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (rc) {
966 tty3270_free_view(tp);
967 return rc;
968 }
969
Martin Schwidefsky36d9f4d2013-12-18 14:36:18 +0100970 tp->screen = tty3270_alloc_screen(tp->view.rows, tp->view.cols);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100971 if (IS_ERR(tp->screen)) {
972 rc = PTR_ERR(tp->screen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 raw3270_put_view(&tp->view);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800974 raw3270_del_view(&tp->view);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100975 tty3270_free_view(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return rc;
977 }
978
Jiri Slabyd6c53c02013-01-03 15:53:05 +0100979 tp->port.low_latency = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 tty->winsize.ws_row = tp->view.rows - 2;
981 tty->winsize.ws_col = tp->view.cols;
982
983 tty3270_create_prompt(tp);
984 tty3270_create_status(tp);
985 tty3270_update_status(tp);
986
987 /* Create blank line for every line in the tty output area. */
988 for (i = 0; i < tp->view.rows - 2; i++)
989 tty3270_blank_line(tp);
990
Jiri Slabyba186e72012-04-02 13:54:18 +0200991 tp->kbd->port = &tp->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 tp->kbd->fn_handler[KVAL(K_INCRCONSOLE)] = tty3270_exit_tty;
993 tp->kbd->fn_handler[KVAL(K_SCROLLBACK)] = tty3270_scroll_backward;
994 tp->kbd->fn_handler[KVAL(K_SCROLLFORW)] = tty3270_scroll_forward;
995 tp->kbd->fn_handler[KVAL(K_CONS)] = tty3270_rcl_backward;
996 kbd_ascebc(tp->kbd, tp->view.ascebc);
997
998 raw3270_activate_view(&tp->view);
Jiri Slaby20cda6f2012-08-07 21:48:03 +0200999
Martin Schwidefskyb5123532016-05-02 13:49:28 +02001000port_install:
Jiri Slaby20cda6f2012-08-07 21:48:03 +02001001 rc = tty_port_install(&tp->port, driver, tty);
1002 if (rc) {
1003 raw3270_put_view(&tp->view);
1004 return rc;
1005 }
1006
1007 tty->driver_data = tp;
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return 0;
1010}
1011
1012/*
Martin Schwidefsky736c9fd2013-01-08 15:15:12 +01001013 * This routine is called whenever a 3270 tty is opened.
1014 */
1015static int
1016tty3270_open(struct tty_struct *tty, struct file *filp)
1017{
1018 struct tty3270 *tp = tty->driver_data;
1019 struct tty_port *port = &tp->port;
1020
1021 port->count++;
1022 tty_port_tty_set(port, tty);
1023 return 0;
1024}
1025
1026/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 * This routine is called when the 3270 tty is closed. We wait
1028 * for the remaining request to be completed. Then we clean up.
1029 */
1030static void
1031tty3270_close(struct tty_struct *tty, struct file * filp)
1032{
Jiri Slaby881e18f2012-04-02 13:54:16 +02001033 struct tty3270 *tp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 if (tty->count > 1)
1036 return;
Martin Schwidefskyb5123532016-05-02 13:49:28 +02001037 if (tp)
Jiri Slabyba186e72012-04-02 13:54:18 +02001038 tty_port_tty_set(&tp->port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
Jiri Slaby20cda6f2012-08-07 21:48:03 +02001041static void tty3270_cleanup(struct tty_struct *tty)
1042{
1043 struct tty3270 *tp = tty->driver_data;
1044
Martin Schwidefskyb5123532016-05-02 13:49:28 +02001045 if (tp) {
1046 tty->driver_data = NULL;
Jiri Slaby20cda6f2012-08-07 21:48:03 +02001047 raw3270_put_view(&tp->view);
Martin Schwidefskyb5123532016-05-02 13:49:28 +02001048 }
Jiri Slaby20cda6f2012-08-07 21:48:03 +02001049}
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051/*
1052 * We always have room.
1053 */
1054static int
1055tty3270_write_room(struct tty_struct *tty)
1056{
1057 return INT_MAX;
1058}
1059
1060/*
1061 * Insert character into the screen at the current position with the
1062 * current color and highlight. This function does NOT do cursor movement.
1063 */
Heiko Carstens74c76c82008-05-07 09:22:58 +02001064static void tty3270_put_character(struct tty3270 *tp, char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 struct tty3270_line *line;
1067 struct tty3270_cell *cell;
1068
1069 line = tp->screen + tp->cy;
1070 if (line->len <= tp->cx) {
1071 while (line->len < tp->cx) {
1072 cell = line->cells + line->len;
1073 cell->character = tp->view.ascebc[' '];
1074 cell->highlight = tp->highlight;
1075 cell->f_color = tp->f_color;
1076 line->len++;
1077 }
1078 line->len++;
1079 }
1080 cell = line->cells + tp->cx;
1081 cell->character = tp->view.ascebc[(unsigned int) ch];
1082 cell->highlight = tp->highlight;
1083 cell->f_color = tp->f_color;
1084}
1085
1086/*
1087 * Convert a tty3270_line to a 3270 data fragment usable for output.
1088 */
1089static void
1090tty3270_convert_line(struct tty3270 *tp, int line_nr)
1091{
1092 struct tty3270_line *line;
1093 struct tty3270_cell *cell;
1094 struct string *s, *n;
1095 unsigned char highlight;
1096 unsigned char f_color;
1097 char *cp;
1098 int flen, i;
1099
1100 /* Determine how long the fragment will be. */
1101 flen = 3; /* Prefix (TO_SBA). */
1102 line = tp->screen + line_nr;
1103 flen += line->len;
1104 highlight = TAX_RESET;
1105 f_color = TAC_RESET;
1106 for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
1107 if (cell->highlight != highlight) {
1108 flen += 3; /* TO_SA to switch highlight. */
1109 highlight = cell->highlight;
1110 }
1111 if (cell->f_color != f_color) {
1112 flen += 3; /* TO_SA to switch color. */
1113 f_color = cell->f_color;
1114 }
1115 }
1116 if (highlight != TAX_RESET)
1117 flen += 3; /* TO_SA to reset hightlight. */
1118 if (f_color != TAC_RESET)
1119 flen += 3; /* TO_SA to reset color. */
1120 if (line->len < tp->view.cols)
1121 flen += 4; /* Postfix (TO_RA). */
1122
1123 /* Find the line in the list. */
1124 i = tp->view.rows - 2 - line_nr;
1125 list_for_each_entry_reverse(s, &tp->lines, list)
1126 if (--i <= 0)
1127 break;
1128 /*
1129 * Check if the line needs to get reallocated.
1130 */
1131 if (s->len != flen) {
1132 /* Reallocate string. */
1133 n = tty3270_alloc_string(tp, flen);
1134 list_add(&n->list, &s->list);
1135 list_del_init(&s->list);
1136 if (!list_empty(&s->update))
1137 list_del_init(&s->update);
1138 free_string(&tp->freemem, s);
1139 s = n;
1140 }
1141
1142 /* Write 3270 data fragment. */
1143 cp = s->string;
1144 *cp++ = TO_SBA;
1145 *cp++ = 0;
1146 *cp++ = 0;
1147
1148 highlight = TAX_RESET;
1149 f_color = TAC_RESET;
1150 for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
1151 if (cell->highlight != highlight) {
1152 *cp++ = TO_SA;
1153 *cp++ = TAT_EXTHI;
1154 *cp++ = cell->highlight;
1155 highlight = cell->highlight;
1156 }
1157 if (cell->f_color != f_color) {
1158 *cp++ = TO_SA;
1159 *cp++ = TAT_COLOR;
1160 *cp++ = cell->f_color;
1161 f_color = cell->f_color;
1162 }
1163 *cp++ = cell->character;
1164 }
1165 if (highlight != TAX_RESET) {
1166 *cp++ = TO_SA;
1167 *cp++ = TAT_EXTHI;
1168 *cp++ = TAX_RESET;
1169 }
1170 if (f_color != TAC_RESET) {
1171 *cp++ = TO_SA;
1172 *cp++ = TAT_COLOR;
1173 *cp++ = TAC_RESET;
1174 }
1175 if (line->len < tp->view.cols) {
1176 *cp++ = TO_RA;
1177 *cp++ = 0;
1178 *cp++ = 0;
1179 *cp++ = 0;
1180 }
1181
1182 if (tp->nr_up + line_nr < tp->view.rows - 2) {
1183 /* Line is currently visible on screen. */
1184 tty3270_update_string(tp, s, line_nr);
1185 /* Add line to update list. */
1186 if (list_empty(&s->update)) {
1187 list_add_tail(&s->update, &tp->update);
1188 tp->update_flags |= TTY_UPDATE_LIST;
1189 }
1190 }
1191}
1192
1193/*
1194 * Do carriage return.
1195 */
1196static void
1197tty3270_cr(struct tty3270 *tp)
1198{
1199 tp->cx = 0;
1200}
1201
1202/*
1203 * Do line feed.
1204 */
1205static void
1206tty3270_lf(struct tty3270 *tp)
1207{
1208 struct tty3270_line temp;
1209 int i;
1210
1211 tty3270_convert_line(tp, tp->cy);
1212 if (tp->cy < tp->view.rows - 3) {
1213 tp->cy++;
1214 return;
1215 }
1216 /* Last line just filled up. Add new, blank line. */
1217 tty3270_blank_line(tp);
1218 temp = tp->screen[0];
1219 temp.len = 0;
1220 for (i = 0; i < tp->view.rows - 3; i++)
1221 tp->screen[i] = tp->screen[i+1];
1222 tp->screen[tp->view.rows - 3] = temp;
1223 tty3270_rebuild_update(tp);
1224}
1225
1226static void
1227tty3270_ri(struct tty3270 *tp)
1228{
1229 if (tp->cy > 0) {
1230 tty3270_convert_line(tp, tp->cy);
1231 tp->cy--;
1232 }
1233}
1234
1235/*
1236 * Insert characters at current position.
1237 */
1238static void
1239tty3270_insert_characters(struct tty3270 *tp, int n)
1240{
1241 struct tty3270_line *line;
1242 int k;
1243
1244 line = tp->screen + tp->cy;
1245 while (line->len < tp->cx) {
1246 line->cells[line->len].character = tp->view.ascebc[' '];
1247 line->cells[line->len].highlight = TAX_RESET;
1248 line->cells[line->len].f_color = TAC_RESET;
1249 line->len++;
1250 }
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) {
1260 line->cells[tp->cx + n].character = tp->view.ascebc[' '];
1261 line->cells[tp->cx + n].highlight = tp->highlight;
1262 line->cells[tp->cx + n].f_color = tp->f_color;
1263 }
1264}
1265
1266/*
1267 * Delete characters at current position.
1268 */
1269static void
1270tty3270_delete_characters(struct tty3270 *tp, int n)
1271{
1272 struct tty3270_line *line;
1273 int i;
1274
1275 line = tp->screen + tp->cy;
1276 if (line->len <= tp->cx)
1277 return;
1278 if (line->len - tp->cx <= n) {
1279 line->len = tp->cx;
1280 return;
1281 }
1282 for (i = tp->cx; i + n < line->len; i++)
1283 line->cells[i] = line->cells[i + n];
1284 line->len -= n;
1285}
1286
1287/*
1288 * Erase characters at current position.
1289 */
1290static void
1291tty3270_erase_characters(struct tty3270 *tp, int n)
1292{
1293 struct tty3270_line *line;
1294 struct tty3270_cell *cell;
1295
1296 line = tp->screen + tp->cy;
1297 while (line->len > tp->cx && n-- > 0) {
1298 cell = line->cells + tp->cx++;
1299 cell->character = ' ';
1300 cell->highlight = TAX_RESET;
1301 cell->f_color = TAC_RESET;
1302 }
1303 tp->cx += n;
1304 tp->cx = min_t(int, tp->cx, tp->view.cols - 1);
1305}
1306
1307/*
1308 * Erase line, 3 different cases:
1309 * Esc [ 0 K Erase from current position to end of line inclusive
1310 * Esc [ 1 K Erase from beginning of line to current position inclusive
1311 * Esc [ 2 K Erase entire line (without moving cursor)
1312 */
1313static void
1314tty3270_erase_line(struct tty3270 *tp, int mode)
1315{
1316 struct tty3270_line *line;
1317 struct tty3270_cell *cell;
1318 int i;
1319
1320 line = tp->screen + tp->cy;
1321 if (mode == 0)
1322 line->len = tp->cx;
1323 else if (mode == 1) {
1324 for (i = 0; i < tp->cx; i++) {
1325 cell = line->cells + i;
1326 cell->character = ' ';
1327 cell->highlight = TAX_RESET;
1328 cell->f_color = TAC_RESET;
1329 }
1330 if (line->len <= tp->cx)
1331 line->len = tp->cx + 1;
1332 } else if (mode == 2)
1333 line->len = 0;
1334 tty3270_convert_line(tp, tp->cy);
1335}
1336
1337/*
1338 * Erase display, 3 different cases:
1339 * Esc [ 0 J Erase from current position to bottom of screen inclusive
1340 * Esc [ 1 J Erase from top of screen to current position inclusive
1341 * Esc [ 2 J Erase entire screen (without moving the cursor)
1342 */
1343static void
1344tty3270_erase_display(struct tty3270 *tp, int mode)
1345{
1346 int i;
1347
1348 if (mode == 0) {
1349 tty3270_erase_line(tp, 0);
1350 for (i = tp->cy + 1; i < tp->view.rows - 2; i++) {
1351 tp->screen[i].len = 0;
1352 tty3270_convert_line(tp, i);
1353 }
1354 } else if (mode == 1) {
1355 for (i = 0; i < tp->cy; i++) {
1356 tp->screen[i].len = 0;
1357 tty3270_convert_line(tp, i);
1358 }
1359 tty3270_erase_line(tp, 1);
1360 } else if (mode == 2) {
1361 for (i = 0; i < tp->view.rows - 2; i++) {
1362 tp->screen[i].len = 0;
1363 tty3270_convert_line(tp, i);
1364 }
1365 }
1366 tty3270_rebuild_update(tp);
1367}
1368
1369/*
1370 * Set attributes found in an escape sequence.
1371 * Esc [ <attr> ; <attr> ; ... m
1372 */
1373static void
1374tty3270_set_attributes(struct tty3270 *tp)
1375{
1376 static unsigned char f_colors[] = {
1377 TAC_DEFAULT, TAC_RED, TAC_GREEN, TAC_YELLOW, TAC_BLUE,
1378 TAC_PINK, TAC_TURQ, TAC_WHITE, 0, TAC_DEFAULT
1379 };
1380 int i, attr;
1381
1382 for (i = 0; i <= tp->esc_npar; i++) {
1383 attr = tp->esc_par[i];
1384 switch (attr) {
1385 case 0: /* Reset */
1386 tp->highlight = TAX_RESET;
1387 tp->f_color = TAC_RESET;
1388 break;
1389 /* Highlight. */
1390 case 4: /* Start underlining. */
1391 tp->highlight = TAX_UNDER;
1392 break;
1393 case 5: /* Start blink. */
1394 tp->highlight = TAX_BLINK;
1395 break;
1396 case 7: /* Start reverse. */
1397 tp->highlight = TAX_REVER;
1398 break;
1399 case 24: /* End underlining */
1400 if (tp->highlight == TAX_UNDER)
1401 tp->highlight = TAX_RESET;
1402 break;
1403 case 25: /* End blink. */
1404 if (tp->highlight == TAX_BLINK)
1405 tp->highlight = TAX_RESET;
1406 break;
1407 case 27: /* End reverse. */
1408 if (tp->highlight == TAX_REVER)
1409 tp->highlight = TAX_RESET;
1410 break;
1411 /* Foreground color. */
1412 case 30: /* Black */
1413 case 31: /* Red */
1414 case 32: /* Green */
1415 case 33: /* Yellow */
1416 case 34: /* Blue */
1417 case 35: /* Magenta */
1418 case 36: /* Cyan */
1419 case 37: /* White */
1420 case 39: /* Black */
1421 tp->f_color = f_colors[attr - 30];
1422 break;
1423 }
1424 }
1425}
1426
1427static inline int
1428tty3270_getpar(struct tty3270 *tp, int ix)
1429{
1430 return (tp->esc_par[ix] > 0) ? tp->esc_par[ix] : 1;
1431}
1432
1433static void
1434tty3270_goto_xy(struct tty3270 *tp, int cx, int cy)
1435{
Heiko Carstens364c8552007-10-12 16:11:35 +02001436 int max_cx = max(0, cx);
1437 int max_cy = max(0, cy);
1438
1439 tp->cx = min_t(int, tp->view.cols - 1, max_cx);
1440 cy = min_t(int, tp->view.rows - 3, max_cy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (cy != tp->cy) {
1442 tty3270_convert_line(tp, tp->cy);
1443 tp->cy = cy;
1444 }
1445}
1446
1447/*
1448 * Process escape sequences. Known sequences:
1449 * Esc 7 Save Cursor Position
1450 * Esc 8 Restore Cursor Position
1451 * Esc [ Pn ; Pn ; .. m Set attributes
1452 * Esc [ Pn ; Pn H Cursor Position
1453 * Esc [ Pn ; Pn f Cursor Position
1454 * Esc [ Pn A Cursor Up
1455 * Esc [ Pn B Cursor Down
1456 * Esc [ Pn C Cursor Forward
1457 * Esc [ Pn D Cursor Backward
1458 * Esc [ Pn G Cursor Horizontal Absolute
1459 * Esc [ Pn X Erase Characters
1460 * Esc [ Ps J Erase in Display
1461 * Esc [ Ps K Erase in Line
1462 * // FIXME: add all the new ones.
1463 *
1464 * Pn is a numeric parameter, a string of zero or more decimal digits.
1465 * Ps is a selective parameter.
1466 */
1467static void
1468tty3270_escape_sequence(struct tty3270 *tp, char ch)
1469{
1470 enum { ESnormal, ESesc, ESsquare, ESgetpars };
1471
1472 if (tp->esc_state == ESnormal) {
1473 if (ch == 0x1b)
1474 /* Starting new escape sequence. */
1475 tp->esc_state = ESesc;
1476 return;
1477 }
1478 if (tp->esc_state == ESesc) {
1479 tp->esc_state = ESnormal;
1480 switch (ch) {
1481 case '[':
1482 tp->esc_state = ESsquare;
1483 break;
1484 case 'E':
1485 tty3270_cr(tp);
1486 tty3270_lf(tp);
1487 break;
1488 case 'M':
1489 tty3270_ri(tp);
1490 break;
1491 case 'D':
1492 tty3270_lf(tp);
1493 break;
1494 case 'Z': /* Respond ID. */
Jiri Slabyba186e72012-04-02 13:54:18 +02001495 kbd_puts_queue(&tp->port, "\033[?6c");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 break;
1497 case '7': /* Save cursor position. */
1498 tp->saved_cx = tp->cx;
1499 tp->saved_cy = tp->cy;
1500 tp->saved_highlight = tp->highlight;
1501 tp->saved_f_color = tp->f_color;
1502 break;
1503 case '8': /* Restore cursor position. */
1504 tty3270_convert_line(tp, tp->cy);
1505 tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
1506 tp->highlight = tp->saved_highlight;
1507 tp->f_color = tp->saved_f_color;
1508 break;
1509 case 'c': /* Reset terminal. */
1510 tp->cx = tp->saved_cx = 0;
1511 tp->cy = tp->saved_cy = 0;
1512 tp->highlight = tp->saved_highlight = TAX_RESET;
1513 tp->f_color = tp->saved_f_color = TAC_RESET;
1514 tty3270_erase_display(tp, 2);
1515 break;
1516 }
1517 return;
1518 }
1519 if (tp->esc_state == ESsquare) {
1520 tp->esc_state = ESgetpars;
1521 memset(tp->esc_par, 0, sizeof(tp->esc_par));
1522 tp->esc_npar = 0;
1523 tp->esc_ques = (ch == '?');
1524 if (tp->esc_ques)
1525 return;
1526 }
1527 if (tp->esc_state == ESgetpars) {
1528 if (ch == ';' && tp->esc_npar < ESCAPE_NPAR - 1) {
1529 tp->esc_npar++;
1530 return;
1531 }
1532 if (ch >= '0' && ch <= '9') {
1533 tp->esc_par[tp->esc_npar] *= 10;
1534 tp->esc_par[tp->esc_npar] += ch - '0';
1535 return;
1536 }
1537 }
1538 tp->esc_state = ESnormal;
1539 if (ch == 'n' && !tp->esc_ques) {
1540 if (tp->esc_par[0] == 5) /* Status report. */
Jiri Slabyba186e72012-04-02 13:54:18 +02001541 kbd_puts_queue(&tp->port, "\033[0n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 else if (tp->esc_par[0] == 6) { /* Cursor report. */
1543 char buf[40];
1544 sprintf(buf, "\033[%d;%dR", tp->cy + 1, tp->cx + 1);
Jiri Slabyba186e72012-04-02 13:54:18 +02001545 kbd_puts_queue(&tp->port, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
1547 return;
1548 }
1549 if (tp->esc_ques)
1550 return;
1551 switch (ch) {
1552 case 'm':
1553 tty3270_set_attributes(tp);
1554 break;
1555 case 'H': /* Set cursor position. */
1556 case 'f':
1557 tty3270_goto_xy(tp, tty3270_getpar(tp, 1) - 1,
1558 tty3270_getpar(tp, 0) - 1);
1559 break;
1560 case 'd': /* Set y position. */
1561 tty3270_goto_xy(tp, tp->cx, tty3270_getpar(tp, 0) - 1);
1562 break;
1563 case 'A': /* Cursor up. */
1564 case 'F':
1565 tty3270_goto_xy(tp, tp->cx, tp->cy - tty3270_getpar(tp, 0));
1566 break;
1567 case 'B': /* Cursor down. */
1568 case 'e':
1569 case 'E':
1570 tty3270_goto_xy(tp, tp->cx, tp->cy + tty3270_getpar(tp, 0));
1571 break;
1572 case 'C': /* Cursor forward. */
1573 case 'a':
1574 tty3270_goto_xy(tp, tp->cx + tty3270_getpar(tp, 0), tp->cy);
1575 break;
1576 case 'D': /* Cursor backward. */
1577 tty3270_goto_xy(tp, tp->cx - tty3270_getpar(tp, 0), tp->cy);
1578 break;
1579 case 'G': /* Set x position. */
1580 case '`':
1581 tty3270_goto_xy(tp, tty3270_getpar(tp, 0), tp->cy);
1582 break;
1583 case 'X': /* Erase Characters. */
1584 tty3270_erase_characters(tp, tty3270_getpar(tp, 0));
1585 break;
1586 case 'J': /* Erase display. */
1587 tty3270_erase_display(tp, tp->esc_par[0]);
1588 break;
1589 case 'K': /* Erase line. */
1590 tty3270_erase_line(tp, tp->esc_par[0]);
1591 break;
1592 case 'P': /* Delete characters. */
1593 tty3270_delete_characters(tp, tty3270_getpar(tp, 0));
1594 break;
1595 case '@': /* Insert characters. */
1596 tty3270_insert_characters(tp, tty3270_getpar(tp, 0));
1597 break;
1598 case 's': /* Save cursor position. */
1599 tp->saved_cx = tp->cx;
1600 tp->saved_cy = tp->cy;
1601 tp->saved_highlight = tp->highlight;
1602 tp->saved_f_color = tp->f_color;
1603 break;
1604 case 'u': /* Restore cursor position. */
1605 tty3270_convert_line(tp, tp->cy);
1606 tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
1607 tp->highlight = tp->saved_highlight;
1608 tp->f_color = tp->saved_f_color;
1609 break;
1610 }
1611}
1612
1613/*
1614 * String write routine for 3270 ttys
1615 */
1616static void
Jiri Slaby20acdfa2012-04-02 13:54:17 +02001617tty3270_do_write(struct tty3270 *tp, struct tty_struct *tty,
1618 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
1620 int i_msg, i;
1621
1622 spin_lock_bh(&tp->view.lock);
Jiri Slaby20acdfa2012-04-02 13:54:17 +02001623 for (i_msg = 0; !tty->stopped && i_msg < count; i_msg++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (tp->esc_state != 0) {
1625 /* Continue escape sequence. */
1626 tty3270_escape_sequence(tp, buf[i_msg]);
1627 continue;
1628 }
1629
1630 switch (buf[i_msg]) {
1631 case 0x07: /* '\a' -- Alarm */
1632 tp->wcc |= TW_PLUSALARM;
1633 break;
1634 case 0x08: /* Backspace. */
1635 if (tp->cx > 0) {
1636 tp->cx--;
1637 tty3270_put_character(tp, ' ');
1638 }
1639 break;
1640 case 0x09: /* '\t' -- Tabulate */
1641 for (i = tp->cx % 8; i < 8; i++) {
1642 if (tp->cx >= tp->view.cols) {
1643 tty3270_cr(tp);
1644 tty3270_lf(tp);
1645 break;
1646 }
1647 tty3270_put_character(tp, ' ');
1648 tp->cx++;
1649 }
1650 break;
1651 case 0x0a: /* '\n' -- New Line */
1652 tty3270_cr(tp);
1653 tty3270_lf(tp);
1654 break;
1655 case 0x0c: /* '\f' -- Form Feed */
1656 tty3270_erase_display(tp, 2);
1657 tp->cx = tp->cy = 0;
1658 break;
1659 case 0x0d: /* '\r' -- Carriage Return */
1660 tp->cx = 0;
1661 break;
1662 case 0x0f: /* SuSE "exit alternate mode" */
1663 break;
1664 case 0x1b: /* Start escape sequence. */
1665 tty3270_escape_sequence(tp, buf[i_msg]);
1666 break;
1667 default: /* Insert normal character. */
1668 if (tp->cx >= tp->view.cols) {
1669 tty3270_cr(tp);
1670 tty3270_lf(tp);
1671 }
1672 tty3270_put_character(tp, buf[i_msg]);
1673 tp->cx++;
1674 break;
1675 }
1676 }
1677 /* Convert current line to 3270 data fragment. */
1678 tty3270_convert_line(tp, tp->cy);
1679
1680 /* Setup timer to update display after 1/10 second */
1681 if (!timer_pending(&tp->timer))
1682 tty3270_set_timer(tp, HZ/10);
1683
1684 spin_unlock_bh(&tp->view.lock);
1685}
1686
1687/*
1688 * String write routine for 3270 ttys
1689 */
1690static int
1691tty3270_write(struct tty_struct * tty,
1692 const unsigned char *buf, int count)
1693{
1694 struct tty3270 *tp;
1695
1696 tp = tty->driver_data;
1697 if (!tp)
1698 return 0;
1699 if (tp->char_count > 0) {
Jiri Slaby20acdfa2012-04-02 13:54:17 +02001700 tty3270_do_write(tp, tty, tp->char_buf, tp->char_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 tp->char_count = 0;
1702 }
Jiri Slaby20acdfa2012-04-02 13:54:17 +02001703 tty3270_do_write(tp, tty, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return count;
1705}
1706
1707/*
1708 * Put single characters to the ttys character buffer
1709 */
Heiko Carstens74c76c82008-05-07 09:22:58 +02001710static int tty3270_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
1712 struct tty3270 *tp;
1713
1714 tp = tty->driver_data;
Heiko Carstens74c76c82008-05-07 09:22:58 +02001715 if (!tp || tp->char_count >= TTY3270_CHAR_BUF_SIZE)
1716 return 0;
1717 tp->char_buf[tp->char_count++] = ch;
1718 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
1720
1721/*
1722 * Flush all characters from the ttys characeter buffer put there
1723 * by tty3270_put_char.
1724 */
1725static void
1726tty3270_flush_chars(struct tty_struct *tty)
1727{
1728 struct tty3270 *tp;
1729
1730 tp = tty->driver_data;
1731 if (!tp)
1732 return;
1733 if (tp->char_count > 0) {
Jiri Slaby20acdfa2012-04-02 13:54:17 +02001734 tty3270_do_write(tp, tty, tp->char_buf, tp->char_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 tp->char_count = 0;
1736 }
1737}
1738
1739/*
1740 * Returns the number of characters in the output buffer. This is
1741 * used in tty_wait_until_sent to wait until all characters have
1742 * appeared on the screen.
1743 */
1744static int
1745tty3270_chars_in_buffer(struct tty_struct *tty)
1746{
1747 return 0;
1748}
1749
1750static void
1751tty3270_flush_buffer(struct tty_struct *tty)
1752{
1753}
1754
1755/*
1756 * Check for visible/invisible input switches
1757 */
1758static void
Alan Cox606d0992006-12-08 02:38:45 -08001759tty3270_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
1761 struct tty3270 *tp;
1762 int new;
1763
1764 tp = tty->driver_data;
1765 if (!tp)
1766 return;
1767 spin_lock_bh(&tp->view.lock);
1768 if (L_ICANON(tty)) {
1769 new = L_ECHO(tty) ? TF_INPUT: TF_INPUTN;
1770 if (new != tp->inattr) {
1771 tp->inattr = new;
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001772 tty3270_update_prompt(tp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 tty3270_set_timer(tp, 1);
1774 }
1775 }
1776 spin_unlock_bh(&tp->view.lock);
1777}
1778
1779/*
1780 * Disable reading from a 3270 tty
1781 */
1782static void
1783tty3270_throttle(struct tty_struct * tty)
1784{
1785 struct tty3270 *tp;
1786
1787 tp = tty->driver_data;
1788 if (!tp)
1789 return;
1790 tp->throttle = 1;
1791}
1792
1793/*
1794 * Enable reading from a 3270 tty
1795 */
1796static void
1797tty3270_unthrottle(struct tty_struct * tty)
1798{
1799 struct tty3270 *tp;
1800
1801 tp = tty->driver_data;
1802 if (!tp)
1803 return;
1804 tp->throttle = 0;
1805 if (tp->attn)
1806 tty3270_issue_read(tp, 1);
1807}
1808
1809/*
1810 * Hang up the tty device.
1811 */
1812static void
1813tty3270_hangup(struct tty_struct *tty)
1814{
1815 // FIXME: implement
1816}
1817
1818static void
1819tty3270_wait_until_sent(struct tty_struct *tty, int timeout)
1820{
1821}
1822
Heiko Carstens65c56e02011-02-25 14:28:30 +01001823static int tty3270_ioctl(struct tty_struct *tty, unsigned int cmd,
1824 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
1826 struct tty3270 *tp;
1827
1828 tp = tty->driver_data;
1829 if (!tp)
1830 return -ENODEV;
1831 if (tty->flags & (1 << TTY_IO_ERROR))
1832 return -EIO;
Heiko Carstens65c56e02011-02-25 14:28:30 +01001833 return kbd_ioctl(tp->kbd, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
Arnd Bergmann9d4bfd42009-12-07 12:52:13 +01001836#ifdef CONFIG_COMPAT
Heiko Carstens65c56e02011-02-25 14:28:30 +01001837static long tty3270_compat_ioctl(struct tty_struct *tty,
1838 unsigned int cmd, unsigned long arg)
Arnd Bergmann9d4bfd42009-12-07 12:52:13 +01001839{
1840 struct tty3270 *tp;
1841
1842 tp = tty->driver_data;
1843 if (!tp)
1844 return -ENODEV;
1845 if (tty->flags & (1 << TTY_IO_ERROR))
1846 return -EIO;
Heiko Carstens65c56e02011-02-25 14:28:30 +01001847 return kbd_ioctl(tp->kbd, cmd, (unsigned long)compat_ptr(arg));
Arnd Bergmann9d4bfd42009-12-07 12:52:13 +01001848}
1849#endif
1850
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001851static const struct tty_operations tty3270_ops = {
Jiri Slaby20cda6f2012-08-07 21:48:03 +02001852 .install = tty3270_install,
1853 .cleanup = tty3270_cleanup,
Martin Schwidefsky736c9fd2013-01-08 15:15:12 +01001854 .open = tty3270_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 .close = tty3270_close,
1856 .write = tty3270_write,
1857 .put_char = tty3270_put_char,
1858 .flush_chars = tty3270_flush_chars,
1859 .write_room = tty3270_write_room,
1860 .chars_in_buffer = tty3270_chars_in_buffer,
1861 .flush_buffer = tty3270_flush_buffer,
1862 .throttle = tty3270_throttle,
1863 .unthrottle = tty3270_unthrottle,
1864 .hangup = tty3270_hangup,
1865 .wait_until_sent = tty3270_wait_until_sent,
1866 .ioctl = tty3270_ioctl,
Arnd Bergmann9d4bfd42009-12-07 12:52:13 +01001867#ifdef CONFIG_COMPAT
1868 .compat_ioctl = tty3270_compat_ioctl,
1869#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 .set_termios = tty3270_set_termios
1871};
1872
Heiko Carstens63df41d62013-09-06 19:10:48 +02001873static void tty3270_create_cb(int minor)
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001874{
Martin Schwidefsky1fcbba32013-03-21 13:07:18 +01001875 tty_register_device(tty3270_driver, minor - RAW3270_FIRSTMINOR, NULL);
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001876}
1877
Heiko Carstens63df41d62013-09-06 19:10:48 +02001878static void tty3270_destroy_cb(int minor)
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001879{
Martin Schwidefsky1fcbba32013-03-21 13:07:18 +01001880 tty_unregister_device(tty3270_driver, minor - RAW3270_FIRSTMINOR);
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001881}
1882
Heiko Carstens63df41d62013-09-06 19:10:48 +02001883static struct raw3270_notifier tty3270_notifier =
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001884{
1885 .create = tty3270_create_cb,
1886 .destroy = tty3270_destroy_cb,
1887};
1888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889/*
1890 * 3270 tty registration code called from tty_init().
1891 * Most kernel services (incl. kmalloc) are available at this poimt.
1892 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +01001893static int __init tty3270_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
1895 struct tty_driver *driver;
1896 int ret;
1897
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001898 driver = tty_alloc_driver(RAW3270_MAXDEVS,
1899 TTY_DRIVER_REAL_RAW |
1900 TTY_DRIVER_DYNAMIC_DEV |
1901 TTY_DRIVER_RESET_TERMIOS);
1902 if (IS_ERR(driver))
1903 return PTR_ERR(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 /*
1906 * Initialize the tty_driver structure
1907 * Entries in tty3270_driver that are NOT initialized:
1908 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1909 */
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001910 driver->driver_name = "tty3270";
1911 driver->name = "3270/tty";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 driver->major = IBM_TTY3270_MAJOR;
Martin Schwidefsky1fcbba32013-03-21 13:07:18 +01001913 driver->minor_start = RAW3270_FIRSTMINOR;
1914 driver->name_base = RAW3270_FIRSTMINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 driver->type = TTY_DRIVER_TYPE_SYSTEM;
1916 driver->subtype = SYSTEM_TYPE_TTY;
1917 driver->init_termios = tty_std_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 tty_set_operations(driver, &tty3270_ops);
1919 ret = tty_register_driver(driver);
1920 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 put_tty_driver(driver);
1922 return ret;
1923 }
1924 tty3270_driver = driver;
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001925 raw3270_register_notifier(&tty3270_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return 0;
1927}
1928
1929static void __exit
1930tty3270_exit(void)
1931{
1932 struct tty_driver *driver;
1933
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001934 raw3270_unregister_notifier(&tty3270_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 driver = tty3270_driver;
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001936 tty3270_driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 tty_unregister_driver(driver);
Jiri Slaby2312e4f2012-08-07 21:47:41 +02001938 put_tty_driver(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 tty3270_del_views();
1940}
1941
1942MODULE_LICENSE("GPL");
1943MODULE_ALIAS_CHARDEV_MAJOR(IBM_TTY3270_MAJOR);
1944
1945module_init(tty3270_init);
1946module_exit(tty3270_exit);