TTY: switch tty_insert_flip_string
[deliverable/linux.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;
ba186e7d 686 tty_port_init(&tp->port);
9d2ae233
JS
687 setup_timer(&tp->timer, (void (*)(unsigned long)) tty3270_update,
688 (unsigned long) tp);
689 tasklet_init(&tp->readlet,
690 (void (*)(unsigned long)) tty3270_read_tasklet,
691 (unsigned long) tp->read);
692
1da177e4
LT
693 for (pages = 0; pages < TTY3270_STRING_PAGES; pages++) {
694 tp->freemem_pages[pages] = (void *)
695 __get_free_pages(GFP_KERNEL|GFP_DMA, 0);
696 if (!tp->freemem_pages[pages])
697 goto out_pages;
698 add_string_memory(&tp->freemem,
699 tp->freemem_pages[pages], PAGE_SIZE);
700 }
701 tp->write = raw3270_request_alloc(TTY3270_OUTPUT_BUFFER_SIZE);
ed3cb6f0 702 if (IS_ERR(tp->write))
1da177e4
LT
703 goto out_pages;
704 tp->read = raw3270_request_alloc(0);
ed3cb6f0 705 if (IS_ERR(tp->read))
1da177e4
LT
706 goto out_write;
707 tp->kreset = raw3270_request_alloc(1);
ed3cb6f0 708 if (IS_ERR(tp->kreset))
1da177e4
LT
709 goto out_read;
710 tp->kbd = kbd_alloc();
711 if (!tp->kbd)
712 goto out_reset;
713 return tp;
714
715out_reset:
716 raw3270_request_free(tp->kreset);
717out_read:
718 raw3270_request_free(tp->read);
719out_write:
720 raw3270_request_free(tp->write);
721out_pages:
722 while (pages--)
723 free_pages((unsigned long) tp->freemem_pages[pages], 0);
724 kfree(tp->freemem_pages);
191c5f10 725 tty_port_destroy(&tp->port);
1da177e4
LT
726out_tp:
727 kfree(tp);
728out_err:
729 return ERR_PTR(-ENOMEM);
730}
731
732/*
733 * Free tty3270 structure.
734 */
735static void
736tty3270_free_view(struct tty3270 *tp)
737{
738 int pages;
739
205d7ab9 740 del_timer_sync(&tp->timer);
1da177e4
LT
741 kbd_free(tp->kbd);
742 raw3270_request_free(tp->kreset);
743 raw3270_request_free(tp->read);
744 raw3270_request_free(tp->write);
745 for (pages = 0; pages < TTY3270_STRING_PAGES; pages++)
746 free_pages((unsigned long) tp->freemem_pages[pages], 0);
747 kfree(tp->freemem_pages);
191c5f10 748 tty_port_destroy(&tp->port);
1da177e4
LT
749 kfree(tp);
750}
751
752/*
753 * Allocate tty3270 screen.
754 */
755static int
756tty3270_alloc_screen(struct tty3270 *tp)
757{
758 unsigned long size;
759 int lines;
760
761 size = sizeof(struct tty3270_line) * (tp->view.rows - 2);
88abaab4 762 tp->screen = kzalloc(size, GFP_KERNEL);
1da177e4
LT
763 if (!tp->screen)
764 goto out_err;
1da177e4
LT
765 for (lines = 0; lines < tp->view.rows - 2; lines++) {
766 size = sizeof(struct tty3270_cell) * tp->view.cols;
88abaab4 767 tp->screen[lines].cells = kzalloc(size, GFP_KERNEL);
1da177e4
LT
768 if (!tp->screen[lines].cells)
769 goto out_screen;
1da177e4
LT
770 }
771 return 0;
772out_screen:
773 while (lines--)
774 kfree(tp->screen[lines].cells);
775 kfree(tp->screen);
776out_err:
777 return -ENOMEM;
778}
779
780/*
781 * Free tty3270 screen.
782 */
783static void
784tty3270_free_screen(struct tty3270 *tp)
785{
786 int lines;
787
788 for (lines = 0; lines < tp->view.rows - 2; lines++)
789 kfree(tp->screen[lines].cells);
790 kfree(tp->screen);
791}
792
793/*
794 * Unlink tty3270 data structure from tty.
795 */
796static void
797tty3270_release(struct raw3270_view *view)
798{
881e18f9 799 struct tty3270 *tp = container_of(view, struct tty3270, view);
ba186e7d 800 struct tty_struct *tty = tty_port_tty_get(&tp->port);
1da177e4 801
1da177e4 802 if (tty) {
d2c993d8 803 tty->driver_data = NULL;
ba186e7d 804 tty_port_tty_set(&tp->port, NULL);
1da177e4
LT
805 tty_hangup(tty);
806 raw3270_put_view(&tp->view);
ba186e7d 807 tty_kref_put(tty);
1da177e4
LT
808 }
809}
810
811/*
812 * Free tty3270 data structure
813 */
814static void
815tty3270_free(struct raw3270_view *view)
816{
881e18f9
JS
817 struct tty3270 *tp = container_of(view, struct tty3270, view);
818 tty3270_free_screen(tp);
819 tty3270_free_view(tp);
1da177e4
LT
820}
821
822/*
823 * Delayed freeing of tty3270 views.
824 */
825static void
826tty3270_del_views(void)
827{
1da177e4
LT
828 int i;
829
830 for (i = 0; i < tty3270_max_index; i++) {
881e18f9 831 struct raw3270_view *view =
ed3cb6f0 832 raw3270_find_view(&tty3270_fn, i + RAW3270_FIRSTMINOR);
881e18f9
JS
833 if (!IS_ERR(view))
834 raw3270_del_view(view);
1da177e4
LT
835 }
836}
837
2b67fc46 838static struct raw3270_fn tty3270_fn = {
1da177e4
LT
839 .activate = tty3270_activate,
840 .deactivate = tty3270_deactivate,
841 .intv = (void *) tty3270_irq,
842 .release = tty3270_release,
843 .free = tty3270_free
844};
845
846/*
20cda6f2 847 * This routine is called whenever a 3270 tty is opened first time.
1da177e4 848 */
20cda6f2 849static int tty3270_install(struct tty_driver *driver, struct tty_struct *tty)
1da177e4 850{
881e18f9 851 struct raw3270_view *view;
1da177e4
LT
852 struct tty3270 *tp;
853 int i, rc;
854
1da177e4 855 /* Check if the tty3270 is already there. */
881e18f9 856 view = raw3270_find_view(&tty3270_fn,
ed3cb6f0 857 tty->index + RAW3270_FIRSTMINOR);
881e18f9
JS
858 if (!IS_ERR(view)) {
859 tp = container_of(view, struct tty3270, view);
1da177e4
LT
860 tty->driver_data = tp;
861 tty->winsize.ws_row = tp->view.rows - 2;
862 tty->winsize.ws_col = tp->view.cols;
863 tty->low_latency = 0;
ba186e7d
JS
864 /* why to reassign? */
865 tty_port_tty_set(&tp->port, tty);
1da177e4 866 tp->inattr = TF_INPUT;
20cda6f2 867 return tty_port_install(&tp->port, driver, tty);
1da177e4
LT
868 }
869 if (tty3270_max_index < tty->index + 1)
870 tty3270_max_index = tty->index + 1;
871
872 /* Quick exit if there is no device for tty->index. */
881e18f9 873 if (PTR_ERR(view) == -ENODEV)
1da177e4
LT
874 return -ENODEV;
875
876 /* Allocate tty3270 structure on first open. */
877 tp = tty3270_alloc_view();
878 if (IS_ERR(tp))
879 return PTR_ERR(tp);
880
ed3cb6f0
RH
881 rc = raw3270_add_view(&tp->view, &tty3270_fn,
882 tty->index + RAW3270_FIRSTMINOR);
1da177e4
LT
883 if (rc) {
884 tty3270_free_view(tp);
885 return rc;
886 }
887
888 rc = tty3270_alloc_screen(tp);
889 if (rc) {
1da177e4 890 raw3270_put_view(&tp->view);
ed3cb6f0 891 raw3270_del_view(&tp->view);
1da177e4
LT
892 return rc;
893 }
894
ba186e7d 895 tty_port_tty_set(&tp->port, tty);
1da177e4 896 tty->low_latency = 0;
1da177e4
LT
897 tty->winsize.ws_row = tp->view.rows - 2;
898 tty->winsize.ws_col = tp->view.cols;
899
900 tty3270_create_prompt(tp);
901 tty3270_create_status(tp);
902 tty3270_update_status(tp);
903
904 /* Create blank line for every line in the tty output area. */
905 for (i = 0; i < tp->view.rows - 2; i++)
906 tty3270_blank_line(tp);
907
ba186e7d 908 tp->kbd->port = &tp->port;
1da177e4
LT
909 tp->kbd->fn_handler[KVAL(K_INCRCONSOLE)] = tty3270_exit_tty;
910 tp->kbd->fn_handler[KVAL(K_SCROLLBACK)] = tty3270_scroll_backward;
911 tp->kbd->fn_handler[KVAL(K_SCROLLFORW)] = tty3270_scroll_forward;
912 tp->kbd->fn_handler[KVAL(K_CONS)] = tty3270_rcl_backward;
913 kbd_ascebc(tp->kbd, tp->view.ascebc);
914
915 raw3270_activate_view(&tp->view);
20cda6f2
JS
916
917 rc = tty_port_install(&tp->port, driver, tty);
918 if (rc) {
919 raw3270_put_view(&tp->view);
920 return rc;
921 }
922
923 tty->driver_data = tp;
924
1da177e4
LT
925 return 0;
926}
927
928/*
929 * This routine is called when the 3270 tty is closed. We wait
930 * for the remaining request to be completed. Then we clean up.
931 */
932static void
933tty3270_close(struct tty_struct *tty, struct file * filp)
934{
881e18f9 935 struct tty3270 *tp = tty->driver_data;
1da177e4
LT
936
937 if (tty->count > 1)
938 return;
1da177e4 939 if (tp) {
d2c993d8 940 tty->driver_data = NULL;
ba186e7d 941 tty_port_tty_set(&tp->port, NULL);
1da177e4
LT
942 }
943}
944
20cda6f2
JS
945static void tty3270_cleanup(struct tty_struct *tty)
946{
947 struct tty3270 *tp = tty->driver_data;
948
949 if (tp)
950 raw3270_put_view(&tp->view);
951}
952
1da177e4
LT
953/*
954 * We always have room.
955 */
956static int
957tty3270_write_room(struct tty_struct *tty)
958{
959 return INT_MAX;
960}
961
962/*
963 * Insert character into the screen at the current position with the
964 * current color and highlight. This function does NOT do cursor movement.
965 */
74c76c84 966static void tty3270_put_character(struct tty3270 *tp, char ch)
1da177e4
LT
967{
968 struct tty3270_line *line;
969 struct tty3270_cell *cell;
970
971 line = tp->screen + tp->cy;
972 if (line->len <= tp->cx) {
973 while (line->len < tp->cx) {
974 cell = line->cells + line->len;
975 cell->character = tp->view.ascebc[' '];
976 cell->highlight = tp->highlight;
977 cell->f_color = tp->f_color;
978 line->len++;
979 }
980 line->len++;
981 }
982 cell = line->cells + tp->cx;
983 cell->character = tp->view.ascebc[(unsigned int) ch];
984 cell->highlight = tp->highlight;
985 cell->f_color = tp->f_color;
986}
987
988/*
989 * Convert a tty3270_line to a 3270 data fragment usable for output.
990 */
991static void
992tty3270_convert_line(struct tty3270 *tp, int line_nr)
993{
994 struct tty3270_line *line;
995 struct tty3270_cell *cell;
996 struct string *s, *n;
997 unsigned char highlight;
998 unsigned char f_color;
999 char *cp;
1000 int flen, i;
1001
1002 /* Determine how long the fragment will be. */
1003 flen = 3; /* Prefix (TO_SBA). */
1004 line = tp->screen + line_nr;
1005 flen += line->len;
1006 highlight = TAX_RESET;
1007 f_color = TAC_RESET;
1008 for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
1009 if (cell->highlight != highlight) {
1010 flen += 3; /* TO_SA to switch highlight. */
1011 highlight = cell->highlight;
1012 }
1013 if (cell->f_color != f_color) {
1014 flen += 3; /* TO_SA to switch color. */
1015 f_color = cell->f_color;
1016 }
1017 }
1018 if (highlight != TAX_RESET)
1019 flen += 3; /* TO_SA to reset hightlight. */
1020 if (f_color != TAC_RESET)
1021 flen += 3; /* TO_SA to reset color. */
1022 if (line->len < tp->view.cols)
1023 flen += 4; /* Postfix (TO_RA). */
1024
1025 /* Find the line in the list. */
1026 i = tp->view.rows - 2 - line_nr;
1027 list_for_each_entry_reverse(s, &tp->lines, list)
1028 if (--i <= 0)
1029 break;
1030 /*
1031 * Check if the line needs to get reallocated.
1032 */
1033 if (s->len != flen) {
1034 /* Reallocate string. */
1035 n = tty3270_alloc_string(tp, flen);
1036 list_add(&n->list, &s->list);
1037 list_del_init(&s->list);
1038 if (!list_empty(&s->update))
1039 list_del_init(&s->update);
1040 free_string(&tp->freemem, s);
1041 s = n;
1042 }
1043
1044 /* Write 3270 data fragment. */
1045 cp = s->string;
1046 *cp++ = TO_SBA;
1047 *cp++ = 0;
1048 *cp++ = 0;
1049
1050 highlight = TAX_RESET;
1051 f_color = TAC_RESET;
1052 for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
1053 if (cell->highlight != highlight) {
1054 *cp++ = TO_SA;
1055 *cp++ = TAT_EXTHI;
1056 *cp++ = cell->highlight;
1057 highlight = cell->highlight;
1058 }
1059 if (cell->f_color != f_color) {
1060 *cp++ = TO_SA;
1061 *cp++ = TAT_COLOR;
1062 *cp++ = cell->f_color;
1063 f_color = cell->f_color;
1064 }
1065 *cp++ = cell->character;
1066 }
1067 if (highlight != TAX_RESET) {
1068 *cp++ = TO_SA;
1069 *cp++ = TAT_EXTHI;
1070 *cp++ = TAX_RESET;
1071 }
1072 if (f_color != TAC_RESET) {
1073 *cp++ = TO_SA;
1074 *cp++ = TAT_COLOR;
1075 *cp++ = TAC_RESET;
1076 }
1077 if (line->len < tp->view.cols) {
1078 *cp++ = TO_RA;
1079 *cp++ = 0;
1080 *cp++ = 0;
1081 *cp++ = 0;
1082 }
1083
1084 if (tp->nr_up + line_nr < tp->view.rows - 2) {
1085 /* Line is currently visible on screen. */
1086 tty3270_update_string(tp, s, line_nr);
1087 /* Add line to update list. */
1088 if (list_empty(&s->update)) {
1089 list_add_tail(&s->update, &tp->update);
1090 tp->update_flags |= TTY_UPDATE_LIST;
1091 }
1092 }
1093}
1094
1095/*
1096 * Do carriage return.
1097 */
1098static void
1099tty3270_cr(struct tty3270 *tp)
1100{
1101 tp->cx = 0;
1102}
1103
1104/*
1105 * Do line feed.
1106 */
1107static void
1108tty3270_lf(struct tty3270 *tp)
1109{
1110 struct tty3270_line temp;
1111 int i;
1112
1113 tty3270_convert_line(tp, tp->cy);
1114 if (tp->cy < tp->view.rows - 3) {
1115 tp->cy++;
1116 return;
1117 }
1118 /* Last line just filled up. Add new, blank line. */
1119 tty3270_blank_line(tp);
1120 temp = tp->screen[0];
1121 temp.len = 0;
1122 for (i = 0; i < tp->view.rows - 3; i++)
1123 tp->screen[i] = tp->screen[i+1];
1124 tp->screen[tp->view.rows - 3] = temp;
1125 tty3270_rebuild_update(tp);
1126}
1127
1128static void
1129tty3270_ri(struct tty3270 *tp)
1130{
1131 if (tp->cy > 0) {
1132 tty3270_convert_line(tp, tp->cy);
1133 tp->cy--;
1134 }
1135}
1136
1137/*
1138 * Insert characters at current position.
1139 */
1140static void
1141tty3270_insert_characters(struct tty3270 *tp, int n)
1142{
1143 struct tty3270_line *line;
1144 int k;
1145
1146 line = tp->screen + tp->cy;
1147 while (line->len < tp->cx) {
1148 line->cells[line->len].character = tp->view.ascebc[' '];
1149 line->cells[line->len].highlight = TAX_RESET;
1150 line->cells[line->len].f_color = TAC_RESET;
1151 line->len++;
1152 }
1153 if (n > tp->view.cols - tp->cx)
1154 n = tp->view.cols - tp->cx;
1155 k = min_t(int, line->len - tp->cx, tp->view.cols - tp->cx - n);
1156 while (k--)
1157 line->cells[tp->cx + n + k] = line->cells[tp->cx + k];
1158 line->len += n;
1159 if (line->len > tp->view.cols)
1160 line->len = tp->view.cols;
1161 while (n-- > 0) {
1162 line->cells[tp->cx + n].character = tp->view.ascebc[' '];
1163 line->cells[tp->cx + n].highlight = tp->highlight;
1164 line->cells[tp->cx + n].f_color = tp->f_color;
1165 }
1166}
1167
1168/*
1169 * Delete characters at current position.
1170 */
1171static void
1172tty3270_delete_characters(struct tty3270 *tp, int n)
1173{
1174 struct tty3270_line *line;
1175 int i;
1176
1177 line = tp->screen + tp->cy;
1178 if (line->len <= tp->cx)
1179 return;
1180 if (line->len - tp->cx <= n) {
1181 line->len = tp->cx;
1182 return;
1183 }
1184 for (i = tp->cx; i + n < line->len; i++)
1185 line->cells[i] = line->cells[i + n];
1186 line->len -= n;
1187}
1188
1189/*
1190 * Erase characters at current position.
1191 */
1192static void
1193tty3270_erase_characters(struct tty3270 *tp, int n)
1194{
1195 struct tty3270_line *line;
1196 struct tty3270_cell *cell;
1197
1198 line = tp->screen + tp->cy;
1199 while (line->len > tp->cx && n-- > 0) {
1200 cell = line->cells + tp->cx++;
1201 cell->character = ' ';
1202 cell->highlight = TAX_RESET;
1203 cell->f_color = TAC_RESET;
1204 }
1205 tp->cx += n;
1206 tp->cx = min_t(int, tp->cx, tp->view.cols - 1);
1207}
1208
1209/*
1210 * Erase line, 3 different cases:
1211 * Esc [ 0 K Erase from current position to end of line inclusive
1212 * Esc [ 1 K Erase from beginning of line to current position inclusive
1213 * Esc [ 2 K Erase entire line (without moving cursor)
1214 */
1215static void
1216tty3270_erase_line(struct tty3270 *tp, int mode)
1217{
1218 struct tty3270_line *line;
1219 struct tty3270_cell *cell;
1220 int i;
1221
1222 line = tp->screen + tp->cy;
1223 if (mode == 0)
1224 line->len = tp->cx;
1225 else if (mode == 1) {
1226 for (i = 0; i < tp->cx; i++) {
1227 cell = line->cells + i;
1228 cell->character = ' ';
1229 cell->highlight = TAX_RESET;
1230 cell->f_color = TAC_RESET;
1231 }
1232 if (line->len <= tp->cx)
1233 line->len = tp->cx + 1;
1234 } else if (mode == 2)
1235 line->len = 0;
1236 tty3270_convert_line(tp, tp->cy);
1237}
1238
1239/*
1240 * Erase display, 3 different cases:
1241 * Esc [ 0 J Erase from current position to bottom of screen inclusive
1242 * Esc [ 1 J Erase from top of screen to current position inclusive
1243 * Esc [ 2 J Erase entire screen (without moving the cursor)
1244 */
1245static void
1246tty3270_erase_display(struct tty3270 *tp, int mode)
1247{
1248 int i;
1249
1250 if (mode == 0) {
1251 tty3270_erase_line(tp, 0);
1252 for (i = tp->cy + 1; i < tp->view.rows - 2; i++) {
1253 tp->screen[i].len = 0;
1254 tty3270_convert_line(tp, i);
1255 }
1256 } else if (mode == 1) {
1257 for (i = 0; i < tp->cy; i++) {
1258 tp->screen[i].len = 0;
1259 tty3270_convert_line(tp, i);
1260 }
1261 tty3270_erase_line(tp, 1);
1262 } else if (mode == 2) {
1263 for (i = 0; i < tp->view.rows - 2; i++) {
1264 tp->screen[i].len = 0;
1265 tty3270_convert_line(tp, i);
1266 }
1267 }
1268 tty3270_rebuild_update(tp);
1269}
1270
1271/*
1272 * Set attributes found in an escape sequence.
1273 * Esc [ <attr> ; <attr> ; ... m
1274 */
1275static void
1276tty3270_set_attributes(struct tty3270 *tp)
1277{
1278 static unsigned char f_colors[] = {
1279 TAC_DEFAULT, TAC_RED, TAC_GREEN, TAC_YELLOW, TAC_BLUE,
1280 TAC_PINK, TAC_TURQ, TAC_WHITE, 0, TAC_DEFAULT
1281 };
1282 int i, attr;
1283
1284 for (i = 0; i <= tp->esc_npar; i++) {
1285 attr = tp->esc_par[i];
1286 switch (attr) {
1287 case 0: /* Reset */
1288 tp->highlight = TAX_RESET;
1289 tp->f_color = TAC_RESET;
1290 break;
1291 /* Highlight. */
1292 case 4: /* Start underlining. */
1293 tp->highlight = TAX_UNDER;
1294 break;
1295 case 5: /* Start blink. */
1296 tp->highlight = TAX_BLINK;
1297 break;
1298 case 7: /* Start reverse. */
1299 tp->highlight = TAX_REVER;
1300 break;
1301 case 24: /* End underlining */
1302 if (tp->highlight == TAX_UNDER)
1303 tp->highlight = TAX_RESET;
1304 break;
1305 case 25: /* End blink. */
1306 if (tp->highlight == TAX_BLINK)
1307 tp->highlight = TAX_RESET;
1308 break;
1309 case 27: /* End reverse. */
1310 if (tp->highlight == TAX_REVER)
1311 tp->highlight = TAX_RESET;
1312 break;
1313 /* Foreground color. */
1314 case 30: /* Black */
1315 case 31: /* Red */
1316 case 32: /* Green */
1317 case 33: /* Yellow */
1318 case 34: /* Blue */
1319 case 35: /* Magenta */
1320 case 36: /* Cyan */
1321 case 37: /* White */
1322 case 39: /* Black */
1323 tp->f_color = f_colors[attr - 30];
1324 break;
1325 }
1326 }
1327}
1328
1329static inline int
1330tty3270_getpar(struct tty3270 *tp, int ix)
1331{
1332 return (tp->esc_par[ix] > 0) ? tp->esc_par[ix] : 1;
1333}
1334
1335static void
1336tty3270_goto_xy(struct tty3270 *tp, int cx, int cy)
1337{
364c8558
HC
1338 int max_cx = max(0, cx);
1339 int max_cy = max(0, cy);
1340
1341 tp->cx = min_t(int, tp->view.cols - 1, max_cx);
1342 cy = min_t(int, tp->view.rows - 3, max_cy);
1da177e4
LT
1343 if (cy != tp->cy) {
1344 tty3270_convert_line(tp, tp->cy);
1345 tp->cy = cy;
1346 }
1347}
1348
1349/*
1350 * Process escape sequences. Known sequences:
1351 * Esc 7 Save Cursor Position
1352 * Esc 8 Restore Cursor Position
1353 * Esc [ Pn ; Pn ; .. m Set attributes
1354 * Esc [ Pn ; Pn H Cursor Position
1355 * Esc [ Pn ; Pn f Cursor Position
1356 * Esc [ Pn A Cursor Up
1357 * Esc [ Pn B Cursor Down
1358 * Esc [ Pn C Cursor Forward
1359 * Esc [ Pn D Cursor Backward
1360 * Esc [ Pn G Cursor Horizontal Absolute
1361 * Esc [ Pn X Erase Characters
1362 * Esc [ Ps J Erase in Display
1363 * Esc [ Ps K Erase in Line
1364 * // FIXME: add all the new ones.
1365 *
1366 * Pn is a numeric parameter, a string of zero or more decimal digits.
1367 * Ps is a selective parameter.
1368 */
1369static void
1370tty3270_escape_sequence(struct tty3270 *tp, char ch)
1371{
1372 enum { ESnormal, ESesc, ESsquare, ESgetpars };
1373
1374 if (tp->esc_state == ESnormal) {
1375 if (ch == 0x1b)
1376 /* Starting new escape sequence. */
1377 tp->esc_state = ESesc;
1378 return;
1379 }
1380 if (tp->esc_state == ESesc) {
1381 tp->esc_state = ESnormal;
1382 switch (ch) {
1383 case '[':
1384 tp->esc_state = ESsquare;
1385 break;
1386 case 'E':
1387 tty3270_cr(tp);
1388 tty3270_lf(tp);
1389 break;
1390 case 'M':
1391 tty3270_ri(tp);
1392 break;
1393 case 'D':
1394 tty3270_lf(tp);
1395 break;
1396 case 'Z': /* Respond ID. */
ba186e7d 1397 kbd_puts_queue(&tp->port, "\033[?6c");
1da177e4
LT
1398 break;
1399 case '7': /* Save cursor position. */
1400 tp->saved_cx = tp->cx;
1401 tp->saved_cy = tp->cy;
1402 tp->saved_highlight = tp->highlight;
1403 tp->saved_f_color = tp->f_color;
1404 break;
1405 case '8': /* Restore cursor position. */
1406 tty3270_convert_line(tp, tp->cy);
1407 tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
1408 tp->highlight = tp->saved_highlight;
1409 tp->f_color = tp->saved_f_color;
1410 break;
1411 case 'c': /* Reset terminal. */
1412 tp->cx = tp->saved_cx = 0;
1413 tp->cy = tp->saved_cy = 0;
1414 tp->highlight = tp->saved_highlight = TAX_RESET;
1415 tp->f_color = tp->saved_f_color = TAC_RESET;
1416 tty3270_erase_display(tp, 2);
1417 break;
1418 }
1419 return;
1420 }
1421 if (tp->esc_state == ESsquare) {
1422 tp->esc_state = ESgetpars;
1423 memset(tp->esc_par, 0, sizeof(tp->esc_par));
1424 tp->esc_npar = 0;
1425 tp->esc_ques = (ch == '?');
1426 if (tp->esc_ques)
1427 return;
1428 }
1429 if (tp->esc_state == ESgetpars) {
1430 if (ch == ';' && tp->esc_npar < ESCAPE_NPAR - 1) {
1431 tp->esc_npar++;
1432 return;
1433 }
1434 if (ch >= '0' && ch <= '9') {
1435 tp->esc_par[tp->esc_npar] *= 10;
1436 tp->esc_par[tp->esc_npar] += ch - '0';
1437 return;
1438 }
1439 }
1440 tp->esc_state = ESnormal;
1441 if (ch == 'n' && !tp->esc_ques) {
1442 if (tp->esc_par[0] == 5) /* Status report. */
ba186e7d 1443 kbd_puts_queue(&tp->port, "\033[0n");
1da177e4
LT
1444 else if (tp->esc_par[0] == 6) { /* Cursor report. */
1445 char buf[40];
1446 sprintf(buf, "\033[%d;%dR", tp->cy + 1, tp->cx + 1);
ba186e7d 1447 kbd_puts_queue(&tp->port, buf);
1da177e4
LT
1448 }
1449 return;
1450 }
1451 if (tp->esc_ques)
1452 return;
1453 switch (ch) {
1454 case 'm':
1455 tty3270_set_attributes(tp);
1456 break;
1457 case 'H': /* Set cursor position. */
1458 case 'f':
1459 tty3270_goto_xy(tp, tty3270_getpar(tp, 1) - 1,
1460 tty3270_getpar(tp, 0) - 1);
1461 break;
1462 case 'd': /* Set y position. */
1463 tty3270_goto_xy(tp, tp->cx, tty3270_getpar(tp, 0) - 1);
1464 break;
1465 case 'A': /* Cursor up. */
1466 case 'F':
1467 tty3270_goto_xy(tp, tp->cx, tp->cy - tty3270_getpar(tp, 0));
1468 break;
1469 case 'B': /* Cursor down. */
1470 case 'e':
1471 case 'E':
1472 tty3270_goto_xy(tp, tp->cx, tp->cy + tty3270_getpar(tp, 0));
1473 break;
1474 case 'C': /* Cursor forward. */
1475 case 'a':
1476 tty3270_goto_xy(tp, tp->cx + tty3270_getpar(tp, 0), tp->cy);
1477 break;
1478 case 'D': /* Cursor backward. */
1479 tty3270_goto_xy(tp, tp->cx - tty3270_getpar(tp, 0), tp->cy);
1480 break;
1481 case 'G': /* Set x position. */
1482 case '`':
1483 tty3270_goto_xy(tp, tty3270_getpar(tp, 0), tp->cy);
1484 break;
1485 case 'X': /* Erase Characters. */
1486 tty3270_erase_characters(tp, tty3270_getpar(tp, 0));
1487 break;
1488 case 'J': /* Erase display. */
1489 tty3270_erase_display(tp, tp->esc_par[0]);
1490 break;
1491 case 'K': /* Erase line. */
1492 tty3270_erase_line(tp, tp->esc_par[0]);
1493 break;
1494 case 'P': /* Delete characters. */
1495 tty3270_delete_characters(tp, tty3270_getpar(tp, 0));
1496 break;
1497 case '@': /* Insert characters. */
1498 tty3270_insert_characters(tp, tty3270_getpar(tp, 0));
1499 break;
1500 case 's': /* Save cursor position. */
1501 tp->saved_cx = tp->cx;
1502 tp->saved_cy = tp->cy;
1503 tp->saved_highlight = tp->highlight;
1504 tp->saved_f_color = tp->f_color;
1505 break;
1506 case 'u': /* Restore cursor position. */
1507 tty3270_convert_line(tp, tp->cy);
1508 tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
1509 tp->highlight = tp->saved_highlight;
1510 tp->f_color = tp->saved_f_color;
1511 break;
1512 }
1513}
1514
1515/*
1516 * String write routine for 3270 ttys
1517 */
1518static void
20acdfa8
JS
1519tty3270_do_write(struct tty3270 *tp, struct tty_struct *tty,
1520 const unsigned char *buf, int count)
1da177e4
LT
1521{
1522 int i_msg, i;
1523
1524 spin_lock_bh(&tp->view.lock);
20acdfa8 1525 for (i_msg = 0; !tty->stopped && i_msg < count; i_msg++) {
1da177e4
LT
1526 if (tp->esc_state != 0) {
1527 /* Continue escape sequence. */
1528 tty3270_escape_sequence(tp, buf[i_msg]);
1529 continue;
1530 }
1531
1532 switch (buf[i_msg]) {
1533 case 0x07: /* '\a' -- Alarm */
1534 tp->wcc |= TW_PLUSALARM;
1535 break;
1536 case 0x08: /* Backspace. */
1537 if (tp->cx > 0) {
1538 tp->cx--;
1539 tty3270_put_character(tp, ' ');
1540 }
1541 break;
1542 case 0x09: /* '\t' -- Tabulate */
1543 for (i = tp->cx % 8; i < 8; i++) {
1544 if (tp->cx >= tp->view.cols) {
1545 tty3270_cr(tp);
1546 tty3270_lf(tp);
1547 break;
1548 }
1549 tty3270_put_character(tp, ' ');
1550 tp->cx++;
1551 }
1552 break;
1553 case 0x0a: /* '\n' -- New Line */
1554 tty3270_cr(tp);
1555 tty3270_lf(tp);
1556 break;
1557 case 0x0c: /* '\f' -- Form Feed */
1558 tty3270_erase_display(tp, 2);
1559 tp->cx = tp->cy = 0;
1560 break;
1561 case 0x0d: /* '\r' -- Carriage Return */
1562 tp->cx = 0;
1563 break;
1564 case 0x0f: /* SuSE "exit alternate mode" */
1565 break;
1566 case 0x1b: /* Start escape sequence. */
1567 tty3270_escape_sequence(tp, buf[i_msg]);
1568 break;
1569 default: /* Insert normal character. */
1570 if (tp->cx >= tp->view.cols) {
1571 tty3270_cr(tp);
1572 tty3270_lf(tp);
1573 }
1574 tty3270_put_character(tp, buf[i_msg]);
1575 tp->cx++;
1576 break;
1577 }
1578 }
1579 /* Convert current line to 3270 data fragment. */
1580 tty3270_convert_line(tp, tp->cy);
1581
1582 /* Setup timer to update display after 1/10 second */
1583 if (!timer_pending(&tp->timer))
1584 tty3270_set_timer(tp, HZ/10);
1585
1586 spin_unlock_bh(&tp->view.lock);
1587}
1588
1589/*
1590 * String write routine for 3270 ttys
1591 */
1592static int
1593tty3270_write(struct tty_struct * tty,
1594 const unsigned char *buf, int count)
1595{
1596 struct tty3270 *tp;
1597
1598 tp = tty->driver_data;
1599 if (!tp)
1600 return 0;
1601 if (tp->char_count > 0) {
20acdfa8 1602 tty3270_do_write(tp, tty, tp->char_buf, tp->char_count);
1da177e4
LT
1603 tp->char_count = 0;
1604 }
20acdfa8 1605 tty3270_do_write(tp, tty, buf, count);
1da177e4
LT
1606 return count;
1607}
1608
1609/*
1610 * Put single characters to the ttys character buffer
1611 */
74c76c84 1612static int tty3270_put_char(struct tty_struct *tty, unsigned char ch)
1da177e4
LT
1613{
1614 struct tty3270 *tp;
1615
1616 tp = tty->driver_data;
74c76c84
HC
1617 if (!tp || tp->char_count >= TTY3270_CHAR_BUF_SIZE)
1618 return 0;
1619 tp->char_buf[tp->char_count++] = ch;
1620 return 1;
1da177e4
LT
1621}
1622
1623/*
1624 * Flush all characters from the ttys characeter buffer put there
1625 * by tty3270_put_char.
1626 */
1627static void
1628tty3270_flush_chars(struct tty_struct *tty)
1629{
1630 struct tty3270 *tp;
1631
1632 tp = tty->driver_data;
1633 if (!tp)
1634 return;
1635 if (tp->char_count > 0) {
20acdfa8 1636 tty3270_do_write(tp, tty, tp->char_buf, tp->char_count);
1da177e4
LT
1637 tp->char_count = 0;
1638 }
1639}
1640
1641/*
1642 * Returns the number of characters in the output buffer. This is
1643 * used in tty_wait_until_sent to wait until all characters have
1644 * appeared on the screen.
1645 */
1646static int
1647tty3270_chars_in_buffer(struct tty_struct *tty)
1648{
1649 return 0;
1650}
1651
1652static void
1653tty3270_flush_buffer(struct tty_struct *tty)
1654{
1655}
1656
1657/*
1658 * Check for visible/invisible input switches
1659 */
1660static void
606d099c 1661tty3270_set_termios(struct tty_struct *tty, struct ktermios *old)
1da177e4
LT
1662{
1663 struct tty3270 *tp;
1664 int new;
1665
1666 tp = tty->driver_data;
1667 if (!tp)
1668 return;
1669 spin_lock_bh(&tp->view.lock);
1670 if (L_ICANON(tty)) {
1671 new = L_ECHO(tty) ? TF_INPUT: TF_INPUTN;
1672 if (new != tp->inattr) {
1673 tp->inattr = new;
d2c993d8 1674 tty3270_update_prompt(tp, NULL, 0);
1da177e4
LT
1675 tty3270_set_timer(tp, 1);
1676 }
1677 }
1678 spin_unlock_bh(&tp->view.lock);
1679}
1680
1681/*
1682 * Disable reading from a 3270 tty
1683 */
1684static void
1685tty3270_throttle(struct tty_struct * tty)
1686{
1687 struct tty3270 *tp;
1688
1689 tp = tty->driver_data;
1690 if (!tp)
1691 return;
1692 tp->throttle = 1;
1693}
1694
1695/*
1696 * Enable reading from a 3270 tty
1697 */
1698static void
1699tty3270_unthrottle(struct tty_struct * tty)
1700{
1701 struct tty3270 *tp;
1702
1703 tp = tty->driver_data;
1704 if (!tp)
1705 return;
1706 tp->throttle = 0;
1707 if (tp->attn)
1708 tty3270_issue_read(tp, 1);
1709}
1710
1711/*
1712 * Hang up the tty device.
1713 */
1714static void
1715tty3270_hangup(struct tty_struct *tty)
1716{
1717 // FIXME: implement
1718}
1719
1720static void
1721tty3270_wait_until_sent(struct tty_struct *tty, int timeout)
1722{
1723}
1724
65c56e07
HC
1725static int tty3270_ioctl(struct tty_struct *tty, unsigned int cmd,
1726 unsigned long arg)
1da177e4
LT
1727{
1728 struct tty3270 *tp;
1729
1730 tp = tty->driver_data;
1731 if (!tp)
1732 return -ENODEV;
1733 if (tty->flags & (1 << TTY_IO_ERROR))
1734 return -EIO;
65c56e07 1735 return kbd_ioctl(tp->kbd, cmd, arg);
1da177e4
LT
1736}
1737
9d4bfd41 1738#ifdef CONFIG_COMPAT
65c56e07
HC
1739static long tty3270_compat_ioctl(struct tty_struct *tty,
1740 unsigned int cmd, unsigned long arg)
9d4bfd41
AB
1741{
1742 struct tty3270 *tp;
1743
1744 tp = tty->driver_data;
1745 if (!tp)
1746 return -ENODEV;
1747 if (tty->flags & (1 << TTY_IO_ERROR))
1748 return -EIO;
65c56e07 1749 return kbd_ioctl(tp->kbd, cmd, (unsigned long)compat_ptr(arg));
9d4bfd41
AB
1750}
1751#endif
1752
b68e31d0 1753static const struct tty_operations tty3270_ops = {
20cda6f2
JS
1754 .install = tty3270_install,
1755 .cleanup = tty3270_cleanup,
1da177e4
LT
1756 .close = tty3270_close,
1757 .write = tty3270_write,
1758 .put_char = tty3270_put_char,
1759 .flush_chars = tty3270_flush_chars,
1760 .write_room = tty3270_write_room,
1761 .chars_in_buffer = tty3270_chars_in_buffer,
1762 .flush_buffer = tty3270_flush_buffer,
1763 .throttle = tty3270_throttle,
1764 .unthrottle = tty3270_unthrottle,
1765 .hangup = tty3270_hangup,
1766 .wait_until_sent = tty3270_wait_until_sent,
1767 .ioctl = tty3270_ioctl,
9d4bfd41
AB
1768#ifdef CONFIG_COMPAT
1769 .compat_ioctl = tty3270_compat_ioctl,
1770#endif
1da177e4
LT
1771 .set_termios = tty3270_set_termios
1772};
1773
1da177e4
LT
1774/*
1775 * 3270 tty registration code called from tty_init().
1776 * Most kernel services (incl. kmalloc) are available at this poimt.
1777 */
2b67fc46 1778static int __init tty3270_init(void)
1da177e4
LT
1779{
1780 struct tty_driver *driver;
1781 int ret;
1782
ed3cb6f0 1783 driver = alloc_tty_driver(RAW3270_MAXDEVS);
1da177e4
LT
1784 if (!driver)
1785 return -ENOMEM;
1786
1787 /*
1788 * Initialize the tty_driver structure
1789 * Entries in tty3270_driver that are NOT initialized:
1790 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1791 */
1da177e4
LT
1792 driver->driver_name = "ttyTUB";
1793 driver->name = "ttyTUB";
1794 driver->major = IBM_TTY3270_MAJOR;
ed3cb6f0 1795 driver->minor_start = RAW3270_FIRSTMINOR;
1da177e4
LT
1796 driver->type = TTY_DRIVER_TYPE_SYSTEM;
1797 driver->subtype = SYSTEM_TYPE_TTY;
1798 driver->init_termios = tty_std_termios;
c4d6ebeb 1799 driver->flags = TTY_DRIVER_RESET_TERMIOS;
1da177e4
LT
1800 tty_set_operations(driver, &tty3270_ops);
1801 ret = tty_register_driver(driver);
1802 if (ret) {
1da177e4
LT
1803 put_tty_driver(driver);
1804 return ret;
1805 }
1806 tty3270_driver = driver;
1da177e4
LT
1807 return 0;
1808}
1809
1810static void __exit
1811tty3270_exit(void)
1812{
1813 struct tty_driver *driver;
1814
1da177e4 1815 driver = tty3270_driver;
d2c993d8 1816 tty3270_driver = NULL;
1da177e4 1817 tty_unregister_driver(driver);
2312e4f3 1818 put_tty_driver(driver);
1da177e4
LT
1819 tty3270_del_views();
1820}
1821
1822MODULE_LICENSE("GPL");
1823MODULE_ALIAS_CHARDEV_MAJOR(IBM_TTY3270_MAJOR);
1824
1825module_init(tty3270_init);
1826module_exit(tty3270_exit);
This page took 1.141154 seconds and 5 git commands to generate.