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