2 * drivers/s390/char/tty3270.c
3 * IBM/3270 Driver - tty functions.
6 * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
7 * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
8 * -- Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/kdev_t.h>
14 #include <linux/tty.h>
15 #include <linux/vt_kern.h>
16 #include <linux/init.h>
17 #include <linux/console.h>
18 #include <linux/interrupt.h>
20 #include <linux/slab.h>
21 #include <linux/bootmem.h>
22 #include <linux/compat.h>
24 #include <asm/ccwdev.h>
26 #include <asm/ebcdic.h>
27 #include <asm/uaccess.h>
33 #define TTY3270_CHAR_BUF_SIZE 256
34 #define TTY3270_OUTPUT_BUFFER_SIZE 1024
35 #define TTY3270_STRING_PAGES 5
37 struct tty_driver *tty3270_driver;
38 static int tty3270_max_index;
40 static struct raw3270_fn tty3270_fn;
43 unsigned char character;
44 unsigned char highlight;
45 unsigned char f_color;
49 struct tty3270_cell *cells;
56 * The main tty view data structure.
58 * 1) describe line orientation & lines list concept against screen
59 * 2) describe conversion of screen to lines
60 * 3) describe line format.
63 struct raw3270_view view;
65 void **freemem_pages; /* Array of pages used for freemem. */
66 struct list_head freemem; /* List of free memory for strings. */
69 struct list_head lines; /* List of lines. */
70 struct list_head update; /* List of lines to update. */
71 unsigned char wcc; /* Write control character. */
72 int nr_lines; /* # lines in list. */
73 int nr_up; /* # lines up in history. */
74 unsigned long update_flags; /* Update indication bits. */
75 struct string *status; /* Lower right of display. */
76 struct raw3270_request *write; /* Single write request. */
77 struct timer_list timer; /* Output delay timer. */
79 /* Current tty screen. */
80 unsigned int cx, cy; /* Current output position. */
81 unsigned int highlight; /* Blink/reverse/underscore */
82 unsigned int f_color; /* Foreground color */
83 struct tty3270_line *screen;
86 struct string *prompt; /* Output string for input area. */
87 struct string *input; /* Input string for read request. */
88 struct raw3270_request *read; /* Single read request. */
89 struct raw3270_request *kreset; /* Single keyboard reset request. */
90 unsigned char inattr; /* Visible/invisible input. */
91 int throttle, attn; /* tty throttle/unthrottle. */
92 struct tasklet_struct readlet; /* Tasklet to issue read request. */
93 struct kbd_data *kbd; /* key_maps stuff. */
95 /* Escape sequence parsing. */
96 int esc_state, esc_ques, esc_npar;
97 int esc_par[ESCAPE_NPAR];
98 unsigned int saved_cx, saved_cy;
99 unsigned int saved_highlight, saved_f_color;
101 /* Command recalling. */
102 struct list_head rcl_lines; /* List of recallable lines. */
103 struct list_head *rcl_walk; /* Point in rcl_lines list. */
104 int rcl_nr, rcl_max; /* Number/max number of rcl_lines. */
106 /* Character array for put_char/flush_chars. */
107 unsigned int char_count;
108 char char_buf[TTY3270_CHAR_BUF_SIZE];
111 /* tty3270->update_flags. See tty3270_update for details. */
112 #define TTY_UPDATE_ERASE 1 /* Use EWRITEA instead of WRITE. */
113 #define TTY_UPDATE_LIST 2 /* Update lines in tty3270->update. */
114 #define TTY_UPDATE_INPUT 4 /* Update input line. */
115 #define TTY_UPDATE_STATUS 8 /* Update status line. */
116 #define TTY_UPDATE_ALL 16 /* Recreate screen. */
118 static void tty3270_update(struct tty3270 *);
121 * Setup timeout for a device. On timeout trigger an update.
123 static void tty3270_set_timer(struct tty3270 *tp, int expires)
126 del_timer(&tp->timer);
128 mod_timer(&tp->timer, jiffies + expires);
132 * The input line are the two last lines of the screen.
135 tty3270_update_prompt(struct tty3270 *tp, char *input, int count)
142 line->string[5] = TF_INMDT;
144 line->string[5] = tp->inattr;
145 if (count > tp->view.cols * 2 - 11)
146 count = tp->view.cols * 2 - 11;
147 memcpy(line->string + 6, input, count);
148 line->string[6 + count] = TO_IC;
149 /* Clear to end of input line. */
150 if (count < tp->view.cols * 2 - 11) {
151 line->string[7 + count] = TO_RA;
152 line->string[10 + count] = 0;
153 off = tp->view.cols * tp->view.rows - 9;
154 raw3270_buffer_address(tp->view.dev, line->string+count+8, off);
155 line->len = 11 + count;
157 line->len = 7 + count;
158 tp->update_flags |= TTY_UPDATE_INPUT;
162 tty3270_create_prompt(struct tty3270 *tp)
164 static const unsigned char blueprint[] =
165 { TO_SBA, 0, 0, 0x6e, TO_SF, TF_INPUT,
166 /* empty input string */
167 TO_IC, TO_RA, 0, 0, 0 };
171 line = alloc_string(&tp->freemem,
172 sizeof(blueprint) + tp->view.cols * 2 - 9);
174 tp->inattr = TF_INPUT;
175 /* Copy blueprint to status line */
176 memcpy(line->string, blueprint, sizeof(blueprint));
177 line->len = sizeof(blueprint);
178 /* Set output offsets. */
179 offset = tp->view.cols * (tp->view.rows - 2);
180 raw3270_buffer_address(tp->view.dev, line->string + 1, offset);
181 offset = tp->view.cols * tp->view.rows - 9;
182 raw3270_buffer_address(tp->view.dev, line->string + 8, offset);
184 /* Allocate input string for reading. */
185 tp->input = alloc_string(&tp->freemem, tp->view.cols * 2 - 9 + 6);
189 * The status line is the last line of the screen. It shows the string
190 * "Running"/"Holding" in the lower right corner of the screen.
193 tty3270_update_status(struct tty3270 * tp)
197 str = (tp->nr_up != 0) ? "History" : "Running";
198 memcpy(tp->status->string + 8, str, 7);
199 codepage_convert(tp->view.ascebc, tp->status->string + 8, 7);
200 tp->update_flags |= TTY_UPDATE_STATUS;
204 tty3270_create_status(struct tty3270 * tp)
206 static const unsigned char blueprint[] =
207 { TO_SBA, 0, 0, TO_SF, TF_LOG, TO_SA, TAT_COLOR, TAC_GREEN,
208 0, 0, 0, 0, 0, 0, 0, TO_SF, TF_LOG, TO_SA, TAT_COLOR,
213 line = alloc_string(&tp->freemem,sizeof(blueprint));
215 /* Copy blueprint to status line */
216 memcpy(line->string, blueprint, sizeof(blueprint));
217 /* Set address to start of status string (= last 9 characters). */
218 offset = tp->view.cols * tp->view.rows - 9;
219 raw3270_buffer_address(tp->view.dev, line->string + 1, offset);
223 * Set output offsets to 3270 datastream fragment of a tty string.
224 * (TO_SBA offset at the start and TO_RA offset at the end of the string)
227 tty3270_update_string(struct tty3270 *tp, struct string *line, int nr)
231 raw3270_buffer_address(tp->view.dev, line->string + 1,
233 cp = line->string + line->len - 4;
235 raw3270_buffer_address(tp->view.dev, cp + 1,
236 tp->view.cols * (nr + 1));
240 * Rebuild update list to print all lines.
243 tty3270_rebuild_update(struct tty3270 *tp)
245 struct string *s, *n;
249 * Throw away update list and create a new one,
250 * containing all lines that will fit on the screen.
252 list_for_each_entry_safe(s, n, &tp->update, update)
253 list_del_init(&s->update);
254 line = tp->view.rows - 3;
256 list_for_each_entry_reverse(s, &tp->lines, list) {
261 tty3270_update_string(tp, s, line);
262 list_add(&s->update, &tp->update);
266 tp->update_flags |= TTY_UPDATE_LIST;
270 * Alloc string for size bytes. If there is not enough room in
271 * freemem, free strings until there is room.
273 static struct string *
274 tty3270_alloc_string(struct tty3270 *tp, size_t size)
276 struct string *s, *n;
278 s = alloc_string(&tp->freemem, size);
281 list_for_each_entry_safe(s, n, &tp->lines, list) {
282 BUG_ON(tp->nr_lines <= tp->view.rows - 2);
284 if (!list_empty(&s->update))
285 list_del(&s->update);
287 if (free_string(&tp->freemem, s) >= size)
290 s = alloc_string(&tp->freemem, size);
292 if (tp->nr_up != 0 &&
293 tp->nr_up + tp->view.rows - 2 >= tp->nr_lines) {
294 tp->nr_up = tp->nr_lines - tp->view.rows + 2;
295 tty3270_rebuild_update(tp);
296 tty3270_update_status(tp);
302 * Add an empty line to the list.
305 tty3270_blank_line(struct tty3270 *tp)
307 static const unsigned char blueprint[] =
308 { TO_SBA, 0, 0, TO_SA, TAT_EXTHI, TAX_RESET,
309 TO_SA, TAT_COLOR, TAC_RESET, TO_RA, 0, 0, 0 };
312 s = tty3270_alloc_string(tp, sizeof(blueprint));
313 memcpy(s->string, blueprint, sizeof(blueprint));
314 s->len = sizeof(blueprint);
315 list_add_tail(&s->list, &tp->lines);
322 * Write request completion callback.
325 tty3270_write_callback(struct raw3270_request *rq, void *data)
327 struct tty3270 *tp = container_of(rq->view, struct tty3270, view);
330 /* Write wasn't successful. Refresh all. */
331 tp->update_flags = TTY_UPDATE_ALL;
332 tty3270_set_timer(tp, 1);
334 raw3270_request_reset(rq);
335 xchg(&tp->write, rq);
339 * Update 3270 display.
342 tty3270_update(struct tty3270 *tp)
344 static char invalid_sba[2] = { 0xff, 0xff };
345 struct raw3270_request *wrq;
346 unsigned long updated;
347 struct string *s, *n;
351 wrq = xchg(&tp->write, 0);
353 tty3270_set_timer(tp, 1);
357 spin_lock(&tp->view.lock);
359 if (tp->update_flags & TTY_UPDATE_ALL) {
360 tty3270_rebuild_update(tp);
361 tty3270_update_status(tp);
362 tp->update_flags = TTY_UPDATE_ERASE | TTY_UPDATE_LIST |
363 TTY_UPDATE_INPUT | TTY_UPDATE_STATUS;
365 if (tp->update_flags & TTY_UPDATE_ERASE) {
366 /* Use erase write alternate to erase display. */
367 raw3270_request_set_cmd(wrq, TC_EWRITEA);
368 updated |= TTY_UPDATE_ERASE;
370 raw3270_request_set_cmd(wrq, TC_WRITE);
372 raw3270_request_add_data(wrq, &tp->wcc, 1);
376 * Update status line.
378 if (tp->update_flags & TTY_UPDATE_STATUS)
379 if (raw3270_request_add_data(wrq, tp->status->string,
380 tp->status->len) == 0)
381 updated |= TTY_UPDATE_STATUS;
386 if (tp->update_flags & TTY_UPDATE_INPUT)
387 if (raw3270_request_add_data(wrq, tp->prompt->string,
388 tp->prompt->len) == 0)
389 updated |= TTY_UPDATE_INPUT;
393 if (tp->update_flags & TTY_UPDATE_LIST) {
394 /* Write strings in the update list to the screen. */
395 list_for_each_entry_safe(s, n, &tp->update, update) {
399 * Skip TO_SBA at the start of the string if the
400 * last output position matches the start address
403 if (s->string[1] == sba[0] && s->string[2] == sba[1])
405 if (raw3270_request_add_data(wrq, str, len) != 0)
407 list_del_init(&s->update);
408 sba = s->string + s->len - 3;
410 if (list_empty(&tp->update))
411 updated |= TTY_UPDATE_LIST;
413 wrq->callback = tty3270_write_callback;
414 rc = raw3270_start(&tp->view, wrq);
416 tp->update_flags &= ~updated;
417 if (tp->update_flags)
418 tty3270_set_timer(tp, 1);
420 raw3270_request_reset(wrq);
421 xchg(&tp->write, wrq);
423 spin_unlock(&tp->view.lock);
430 tty3270_rcl_add(struct tty3270 *tp, char *input, int len)
437 if (tp->rcl_nr >= tp->rcl_max) {
438 s = list_entry(tp->rcl_lines.next, struct string, list);
440 free_string(&tp->freemem, s);
443 s = tty3270_alloc_string(tp, len);
444 memcpy(s->string, input, len);
445 list_add_tail(&s->list, &tp->rcl_lines);
450 tty3270_rcl_backward(struct kbd_data *kbd)
452 struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
455 spin_lock_bh(&tp->view.lock);
456 if (tp->inattr == TF_INPUT) {
457 if (tp->rcl_walk && tp->rcl_walk->prev != &tp->rcl_lines)
458 tp->rcl_walk = tp->rcl_walk->prev;
459 else if (!list_empty(&tp->rcl_lines))
460 tp->rcl_walk = tp->rcl_lines.prev;
462 list_entry(tp->rcl_walk, struct string, list) : NULL;
464 s = list_entry(tp->rcl_walk, struct string, list);
465 tty3270_update_prompt(tp, s->string, s->len);
467 tty3270_update_prompt(tp, NULL, 0);
468 tty3270_set_timer(tp, 1);
470 spin_unlock_bh(&tp->view.lock);
474 * Deactivate tty view.
477 tty3270_exit_tty(struct kbd_data *kbd)
479 struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
481 raw3270_deactivate_view(&tp->view);
485 * Scroll forward in history.
488 tty3270_scroll_forward(struct kbd_data *kbd)
490 struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
493 spin_lock_bh(&tp->view.lock);
494 nr_up = tp->nr_up - tp->view.rows + 2;
497 if (nr_up != tp->nr_up) {
499 tty3270_rebuild_update(tp);
500 tty3270_update_status(tp);
501 tty3270_set_timer(tp, 1);
503 spin_unlock_bh(&tp->view.lock);
507 * Scroll backward in history.
510 tty3270_scroll_backward(struct kbd_data *kbd)
512 struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
515 spin_lock_bh(&tp->view.lock);
516 nr_up = tp->nr_up + tp->view.rows - 2;
517 if (nr_up + tp->view.rows - 2 > tp->nr_lines)
518 nr_up = tp->nr_lines - tp->view.rows + 2;
519 if (nr_up != tp->nr_up) {
521 tty3270_rebuild_update(tp);
522 tty3270_update_status(tp);
523 tty3270_set_timer(tp, 1);
525 spin_unlock_bh(&tp->view.lock);
529 * Pass input line to tty.
532 tty3270_read_tasklet(struct raw3270_request *rrq)
534 static char kreset_data = TW_KR;
535 struct tty3270 *tp = container_of(rrq->view, struct tty3270, view);
539 spin_lock_bh(&tp->view.lock);
541 * Two AID keys are special: For 0x7d (enter) the input line
542 * has to be emitted to the tty and for 0x6d the screen
543 * needs to be redrawn.
547 if (tp->input->string[0] == 0x7d) {
548 /* Enter: write input to tty. */
549 input = tp->input->string + 6;
550 len = tp->input->len - 6 - rrq->rescnt;
551 if (tp->inattr != TF_INPUTN)
552 tty3270_rcl_add(tp, input, len);
555 tty3270_rebuild_update(tp);
556 tty3270_update_status(tp);
558 /* Clear input area. */
559 tty3270_update_prompt(tp, NULL, 0);
560 tty3270_set_timer(tp, 1);
561 } else if (tp->input->string[0] == 0x6d) {
562 /* Display has been cleared. Redraw. */
563 tp->update_flags = TTY_UPDATE_ALL;
564 tty3270_set_timer(tp, 1);
566 spin_unlock_bh(&tp->view.lock);
568 /* Start keyboard reset command. */
569 raw3270_request_reset(tp->kreset);
570 raw3270_request_set_cmd(tp->kreset, TC_WRITE);
571 raw3270_request_add_data(tp->kreset, &kreset_data, 1);
572 raw3270_start(&tp->view, tp->kreset);
575 kbd_keycode(tp->kbd, *input++);
576 /* Emit keycode for AID byte. */
577 kbd_keycode(tp->kbd, 256 + tp->input->string[0]);
579 raw3270_request_reset(rrq);
580 xchg(&tp->read, rrq);
581 raw3270_put_view(&tp->view);
585 * Read request completion callback.
588 tty3270_read_callback(struct raw3270_request *rq, void *data)
590 struct tty3270 *tp = container_of(rq->view, struct tty3270, view);
591 raw3270_get_view(rq->view);
592 /* Schedule tasklet to pass input to tty. */
593 tasklet_schedule(&tp->readlet);
597 * Issue a read request. Call with device lock.
600 tty3270_issue_read(struct tty3270 *tp, int lock)
602 struct raw3270_request *rrq;
605 rrq = xchg(&tp->read, 0);
607 /* Read already scheduled. */
609 rrq->callback = tty3270_read_callback;
610 rrq->callback_data = tp;
611 raw3270_request_set_cmd(rrq, TC_READMOD);
612 raw3270_request_set_data(rrq, tp->input->string, tp->input->len);
613 /* Issue the read modified request. */
615 rc = raw3270_start(&tp->view, rrq);
617 rc = raw3270_start_irq(&tp->view, rrq);
619 raw3270_request_reset(rrq);
620 xchg(&tp->read, rrq);
625 * Switch to the tty view.
628 tty3270_activate(struct raw3270_view *view)
630 struct tty3270 *tp = container_of(view, struct tty3270, view);
632 tp->update_flags = TTY_UPDATE_ALL;
633 tty3270_set_timer(tp, 1);
638 tty3270_deactivate(struct raw3270_view *view)
640 struct tty3270 *tp = container_of(view, struct tty3270, view);
642 del_timer(&tp->timer);
646 tty3270_irq(struct tty3270 *tp, struct raw3270_request *rq, struct irb *irb)
648 /* Handle ATTN. Schedule tasklet to read aid. */
649 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
651 tty3270_issue_read(tp, 0);
657 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
660 /* Normal end. Copy residual count. */
661 rq->rescnt = irb->scsw.cmd.count;
663 return RAW3270_IO_DONE;
667 * Allocate tty3270 structure.
669 static struct tty3270 *
670 tty3270_alloc_view(void)
675 tp = kzalloc(sizeof(struct tty3270), GFP_KERNEL);
679 kmalloc(sizeof(void *) * TTY3270_STRING_PAGES, GFP_KERNEL);
680 if (!tp->freemem_pages)
682 INIT_LIST_HEAD(&tp->freemem);
683 INIT_LIST_HEAD(&tp->lines);
684 INIT_LIST_HEAD(&tp->update);
685 INIT_LIST_HEAD(&tp->rcl_lines);
687 tty_port_init(&tp->port);
688 setup_timer(&tp->timer, (void (*)(unsigned long)) tty3270_update,
690 tasklet_init(&tp->readlet,
691 (void (*)(unsigned long)) tty3270_read_tasklet,
692 (unsigned long) tp->read);
694 for (pages = 0; pages < TTY3270_STRING_PAGES; pages++) {
695 tp->freemem_pages[pages] = (void *)
696 __get_free_pages(GFP_KERNEL|GFP_DMA, 0);
697 if (!tp->freemem_pages[pages])
699 add_string_memory(&tp->freemem,
700 tp->freemem_pages[pages], PAGE_SIZE);
702 tp->write = raw3270_request_alloc(TTY3270_OUTPUT_BUFFER_SIZE);
703 if (IS_ERR(tp->write))
705 tp->read = raw3270_request_alloc(0);
706 if (IS_ERR(tp->read))
708 tp->kreset = raw3270_request_alloc(1);
709 if (IS_ERR(tp->kreset))
711 tp->kbd = kbd_alloc();
717 raw3270_request_free(tp->kreset);
719 raw3270_request_free(tp->read);
721 raw3270_request_free(tp->write);
724 free_pages((unsigned long) tp->freemem_pages[pages], 0);
725 kfree(tp->freemem_pages);
729 return ERR_PTR(-ENOMEM);
733 * Free tty3270 structure.
736 tty3270_free_view(struct tty3270 *tp)
740 del_timer_sync(&tp->timer);
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);
752 * Allocate tty3270 screen.
755 tty3270_alloc_screen(struct tty3270 *tp)
760 size = sizeof(struct tty3270_line) * (tp->view.rows - 2);
761 tp->screen = kzalloc(size, GFP_KERNEL);
764 for (lines = 0; lines < tp->view.rows - 2; lines++) {
765 size = sizeof(struct tty3270_cell) * tp->view.cols;
766 tp->screen[lines].cells = kzalloc(size, GFP_KERNEL);
767 if (!tp->screen[lines].cells)
773 kfree(tp->screen[lines].cells);
780 * Free tty3270 screen.
783 tty3270_free_screen(struct tty3270 *tp)
787 for (lines = 0; lines < tp->view.rows - 2; lines++)
788 kfree(tp->screen[lines].cells);
793 * Unlink tty3270 data structure from tty.
796 tty3270_release(struct raw3270_view *view)
798 struct tty3270 *tp = container_of(view, struct tty3270, view);
799 struct tty_struct *tty = tty_port_tty_get(&tp->port);
802 tty->driver_data = NULL;
803 tty_port_tty_set(&tp->port, NULL);
805 raw3270_put_view(&tp->view);
811 * Free tty3270 data structure
814 tty3270_free(struct raw3270_view *view)
816 struct tty3270 *tp = container_of(view, struct tty3270, view);
817 tty3270_free_screen(tp);
818 tty3270_free_view(tp);
822 * Delayed freeing of tty3270 views.
825 tty3270_del_views(void)
829 for (i = 0; i < tty3270_max_index; i++) {
830 struct raw3270_view *view =
831 raw3270_find_view(&tty3270_fn, i + RAW3270_FIRSTMINOR);
833 raw3270_del_view(view);
837 static struct raw3270_fn tty3270_fn = {
838 .activate = tty3270_activate,
839 .deactivate = tty3270_deactivate,
840 .intv = (void *) tty3270_irq,
841 .release = tty3270_release,
846 * This routine is called whenever a 3270 tty is opened.
849 tty3270_open(struct tty_struct *tty, struct file * filp)
851 struct raw3270_view *view;
857 /* Check if the tty3270 is already there. */
858 view = raw3270_find_view(&tty3270_fn,
859 tty->index + RAW3270_FIRSTMINOR);
861 tp = container_of(view, struct tty3270, view);
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;
866 /* why to reassign? */
867 tty_port_tty_set(&tp->port, tty);
868 tp->inattr = TF_INPUT;
871 if (tty3270_max_index < tty->index + 1)
872 tty3270_max_index = tty->index + 1;
874 /* Quick exit if there is no device for tty->index. */
875 if (PTR_ERR(view) == -ENODEV)
878 /* Allocate tty3270 structure on first open. */
879 tp = tty3270_alloc_view();
883 rc = raw3270_add_view(&tp->view, &tty3270_fn,
884 tty->index + RAW3270_FIRSTMINOR);
886 tty3270_free_view(tp);
890 rc = tty3270_alloc_screen(tp);
892 raw3270_put_view(&tp->view);
893 raw3270_del_view(&tp->view);
897 tty_port_tty_set(&tp->port, tty);
898 tty->low_latency = 0;
899 tty->driver_data = tp;
900 tty->winsize.ws_row = tp->view.rows - 2;
901 tty->winsize.ws_col = tp->view.cols;
903 tty3270_create_prompt(tp);
904 tty3270_create_status(tp);
905 tty3270_update_status(tp);
907 /* Create blank line for every line in the tty output area. */
908 for (i = 0; i < tp->view.rows - 2; i++)
909 tty3270_blank_line(tp);
911 tp->kbd->port = &tp->port;
912 tp->kbd->fn_handler[KVAL(K_INCRCONSOLE)] = tty3270_exit_tty;
913 tp->kbd->fn_handler[KVAL(K_SCROLLBACK)] = tty3270_scroll_backward;
914 tp->kbd->fn_handler[KVAL(K_SCROLLFORW)] = tty3270_scroll_forward;
915 tp->kbd->fn_handler[KVAL(K_CONS)] = tty3270_rcl_backward;
916 kbd_ascebc(tp->kbd, tp->view.ascebc);
918 raw3270_activate_view(&tp->view);
923 * This routine is called when the 3270 tty is closed. We wait
924 * for the remaining request to be completed. Then we clean up.
927 tty3270_close(struct tty_struct *tty, struct file * filp)
929 struct tty3270 *tp = tty->driver_data;
934 tty->driver_data = NULL;
935 tty_port_tty_set(&tp->port, NULL);
936 raw3270_put_view(&tp->view);
941 * We always have room.
944 tty3270_write_room(struct tty_struct *tty)
950 * Insert character into the screen at the current position with the
951 * current color and highlight. This function does NOT do cursor movement.
953 static void tty3270_put_character(struct tty3270 *tp, char ch)
955 struct tty3270_line *line;
956 struct tty3270_cell *cell;
958 line = tp->screen + tp->cy;
959 if (line->len <= tp->cx) {
960 while (line->len < tp->cx) {
961 cell = line->cells + line->len;
962 cell->character = tp->view.ascebc[' '];
963 cell->highlight = tp->highlight;
964 cell->f_color = tp->f_color;
969 cell = line->cells + tp->cx;
970 cell->character = tp->view.ascebc[(unsigned int) ch];
971 cell->highlight = tp->highlight;
972 cell->f_color = tp->f_color;
976 * Convert a tty3270_line to a 3270 data fragment usable for output.
979 tty3270_convert_line(struct tty3270 *tp, int line_nr)
981 struct tty3270_line *line;
982 struct tty3270_cell *cell;
983 struct string *s, *n;
984 unsigned char highlight;
985 unsigned char f_color;
989 /* Determine how long the fragment will be. */
990 flen = 3; /* Prefix (TO_SBA). */
991 line = tp->screen + line_nr;
993 highlight = TAX_RESET;
995 for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
996 if (cell->highlight != highlight) {
997 flen += 3; /* TO_SA to switch highlight. */
998 highlight = cell->highlight;
1000 if (cell->f_color != f_color) {
1001 flen += 3; /* TO_SA to switch color. */
1002 f_color = cell->f_color;
1005 if (highlight != TAX_RESET)
1006 flen += 3; /* TO_SA to reset hightlight. */
1007 if (f_color != TAC_RESET)
1008 flen += 3; /* TO_SA to reset color. */
1009 if (line->len < tp->view.cols)
1010 flen += 4; /* Postfix (TO_RA). */
1012 /* Find the line in the list. */
1013 i = tp->view.rows - 2 - line_nr;
1014 list_for_each_entry_reverse(s, &tp->lines, list)
1018 * Check if the line needs to get reallocated.
1020 if (s->len != flen) {
1021 /* Reallocate string. */
1022 n = tty3270_alloc_string(tp, flen);
1023 list_add(&n->list, &s->list);
1024 list_del_init(&s->list);
1025 if (!list_empty(&s->update))
1026 list_del_init(&s->update);
1027 free_string(&tp->freemem, s);
1031 /* Write 3270 data fragment. */
1037 highlight = TAX_RESET;
1038 f_color = TAC_RESET;
1039 for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
1040 if (cell->highlight != highlight) {
1043 *cp++ = cell->highlight;
1044 highlight = cell->highlight;
1046 if (cell->f_color != f_color) {
1049 *cp++ = cell->f_color;
1050 f_color = cell->f_color;
1052 *cp++ = cell->character;
1054 if (highlight != TAX_RESET) {
1059 if (f_color != TAC_RESET) {
1064 if (line->len < tp->view.cols) {
1071 if (tp->nr_up + line_nr < tp->view.rows - 2) {
1072 /* Line is currently visible on screen. */
1073 tty3270_update_string(tp, s, line_nr);
1074 /* Add line to update list. */
1075 if (list_empty(&s->update)) {
1076 list_add_tail(&s->update, &tp->update);
1077 tp->update_flags |= TTY_UPDATE_LIST;
1083 * Do carriage return.
1086 tty3270_cr(struct tty3270 *tp)
1095 tty3270_lf(struct tty3270 *tp)
1097 struct tty3270_line temp;
1100 tty3270_convert_line(tp, tp->cy);
1101 if (tp->cy < tp->view.rows - 3) {
1105 /* Last line just filled up. Add new, blank line. */
1106 tty3270_blank_line(tp);
1107 temp = tp->screen[0];
1109 for (i = 0; i < tp->view.rows - 3; i++)
1110 tp->screen[i] = tp->screen[i+1];
1111 tp->screen[tp->view.rows - 3] = temp;
1112 tty3270_rebuild_update(tp);
1116 tty3270_ri(struct tty3270 *tp)
1119 tty3270_convert_line(tp, tp->cy);
1125 * Insert characters at current position.
1128 tty3270_insert_characters(struct tty3270 *tp, int n)
1130 struct tty3270_line *line;
1133 line = tp->screen + tp->cy;
1134 while (line->len < tp->cx) {
1135 line->cells[line->len].character = tp->view.ascebc[' '];
1136 line->cells[line->len].highlight = TAX_RESET;
1137 line->cells[line->len].f_color = TAC_RESET;
1140 if (n > tp->view.cols - tp->cx)
1141 n = tp->view.cols - tp->cx;
1142 k = min_t(int, line->len - tp->cx, tp->view.cols - tp->cx - n);
1144 line->cells[tp->cx + n + k] = line->cells[tp->cx + k];
1146 if (line->len > tp->view.cols)
1147 line->len = tp->view.cols;
1149 line->cells[tp->cx + n].character = tp->view.ascebc[' '];
1150 line->cells[tp->cx + n].highlight = tp->highlight;
1151 line->cells[tp->cx + n].f_color = tp->f_color;
1156 * Delete characters at current position.
1159 tty3270_delete_characters(struct tty3270 *tp, int n)
1161 struct tty3270_line *line;
1164 line = tp->screen + tp->cy;
1165 if (line->len <= tp->cx)
1167 if (line->len - tp->cx <= n) {
1171 for (i = tp->cx; i + n < line->len; i++)
1172 line->cells[i] = line->cells[i + n];
1177 * Erase characters at current position.
1180 tty3270_erase_characters(struct tty3270 *tp, int n)
1182 struct tty3270_line *line;
1183 struct tty3270_cell *cell;
1185 line = tp->screen + tp->cy;
1186 while (line->len > tp->cx && n-- > 0) {
1187 cell = line->cells + tp->cx++;
1188 cell->character = ' ';
1189 cell->highlight = TAX_RESET;
1190 cell->f_color = TAC_RESET;
1193 tp->cx = min_t(int, tp->cx, tp->view.cols - 1);
1197 * Erase line, 3 different cases:
1198 * Esc [ 0 K Erase from current position to end of line inclusive
1199 * Esc [ 1 K Erase from beginning of line to current position inclusive
1200 * Esc [ 2 K Erase entire line (without moving cursor)
1203 tty3270_erase_line(struct tty3270 *tp, int mode)
1205 struct tty3270_line *line;
1206 struct tty3270_cell *cell;
1209 line = tp->screen + tp->cy;
1212 else if (mode == 1) {
1213 for (i = 0; i < tp->cx; i++) {
1214 cell = line->cells + i;
1215 cell->character = ' ';
1216 cell->highlight = TAX_RESET;
1217 cell->f_color = TAC_RESET;
1219 if (line->len <= tp->cx)
1220 line->len = tp->cx + 1;
1221 } else if (mode == 2)
1223 tty3270_convert_line(tp, tp->cy);
1227 * Erase display, 3 different cases:
1228 * Esc [ 0 J Erase from current position to bottom of screen inclusive
1229 * Esc [ 1 J Erase from top of screen to current position inclusive
1230 * Esc [ 2 J Erase entire screen (without moving the cursor)
1233 tty3270_erase_display(struct tty3270 *tp, int mode)
1238 tty3270_erase_line(tp, 0);
1239 for (i = tp->cy + 1; i < tp->view.rows - 2; i++) {
1240 tp->screen[i].len = 0;
1241 tty3270_convert_line(tp, i);
1243 } else if (mode == 1) {
1244 for (i = 0; i < tp->cy; i++) {
1245 tp->screen[i].len = 0;
1246 tty3270_convert_line(tp, i);
1248 tty3270_erase_line(tp, 1);
1249 } else if (mode == 2) {
1250 for (i = 0; i < tp->view.rows - 2; i++) {
1251 tp->screen[i].len = 0;
1252 tty3270_convert_line(tp, i);
1255 tty3270_rebuild_update(tp);
1259 * Set attributes found in an escape sequence.
1260 * Esc [ <attr> ; <attr> ; ... m
1263 tty3270_set_attributes(struct tty3270 *tp)
1265 static unsigned char f_colors[] = {
1266 TAC_DEFAULT, TAC_RED, TAC_GREEN, TAC_YELLOW, TAC_BLUE,
1267 TAC_PINK, TAC_TURQ, TAC_WHITE, 0, TAC_DEFAULT
1271 for (i = 0; i <= tp->esc_npar; i++) {
1272 attr = tp->esc_par[i];
1275 tp->highlight = TAX_RESET;
1276 tp->f_color = TAC_RESET;
1279 case 4: /* Start underlining. */
1280 tp->highlight = TAX_UNDER;
1282 case 5: /* Start blink. */
1283 tp->highlight = TAX_BLINK;
1285 case 7: /* Start reverse. */
1286 tp->highlight = TAX_REVER;
1288 case 24: /* End underlining */
1289 if (tp->highlight == TAX_UNDER)
1290 tp->highlight = TAX_RESET;
1292 case 25: /* End blink. */
1293 if (tp->highlight == TAX_BLINK)
1294 tp->highlight = TAX_RESET;
1296 case 27: /* End reverse. */
1297 if (tp->highlight == TAX_REVER)
1298 tp->highlight = TAX_RESET;
1300 /* Foreground color. */
1301 case 30: /* Black */
1303 case 32: /* Green */
1304 case 33: /* Yellow */
1306 case 35: /* Magenta */
1308 case 37: /* White */
1309 case 39: /* Black */
1310 tp->f_color = f_colors[attr - 30];
1317 tty3270_getpar(struct tty3270 *tp, int ix)
1319 return (tp->esc_par[ix] > 0) ? tp->esc_par[ix] : 1;
1323 tty3270_goto_xy(struct tty3270 *tp, int cx, int cy)
1325 int max_cx = max(0, cx);
1326 int max_cy = max(0, cy);
1328 tp->cx = min_t(int, tp->view.cols - 1, max_cx);
1329 cy = min_t(int, tp->view.rows - 3, max_cy);
1331 tty3270_convert_line(tp, tp->cy);
1337 * Process escape sequences. Known sequences:
1338 * Esc 7 Save Cursor Position
1339 * Esc 8 Restore Cursor Position
1340 * Esc [ Pn ; Pn ; .. m Set attributes
1341 * Esc [ Pn ; Pn H Cursor Position
1342 * Esc [ Pn ; Pn f Cursor Position
1343 * Esc [ Pn A Cursor Up
1344 * Esc [ Pn B Cursor Down
1345 * Esc [ Pn C Cursor Forward
1346 * Esc [ Pn D Cursor Backward
1347 * Esc [ Pn G Cursor Horizontal Absolute
1348 * Esc [ Pn X Erase Characters
1349 * Esc [ Ps J Erase in Display
1350 * Esc [ Ps K Erase in Line
1351 * // FIXME: add all the new ones.
1353 * Pn is a numeric parameter, a string of zero or more decimal digits.
1354 * Ps is a selective parameter.
1357 tty3270_escape_sequence(struct tty3270 *tp, char ch)
1359 enum { ESnormal, ESesc, ESsquare, ESgetpars };
1361 if (tp->esc_state == ESnormal) {
1363 /* Starting new escape sequence. */
1364 tp->esc_state = ESesc;
1367 if (tp->esc_state == ESesc) {
1368 tp->esc_state = ESnormal;
1371 tp->esc_state = ESsquare;
1383 case 'Z': /* Respond ID. */
1384 kbd_puts_queue(&tp->port, "\033[?6c");
1386 case '7': /* Save cursor position. */
1387 tp->saved_cx = tp->cx;
1388 tp->saved_cy = tp->cy;
1389 tp->saved_highlight = tp->highlight;
1390 tp->saved_f_color = tp->f_color;
1392 case '8': /* Restore cursor position. */
1393 tty3270_convert_line(tp, tp->cy);
1394 tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
1395 tp->highlight = tp->saved_highlight;
1396 tp->f_color = tp->saved_f_color;
1398 case 'c': /* Reset terminal. */
1399 tp->cx = tp->saved_cx = 0;
1400 tp->cy = tp->saved_cy = 0;
1401 tp->highlight = tp->saved_highlight = TAX_RESET;
1402 tp->f_color = tp->saved_f_color = TAC_RESET;
1403 tty3270_erase_display(tp, 2);
1408 if (tp->esc_state == ESsquare) {
1409 tp->esc_state = ESgetpars;
1410 memset(tp->esc_par, 0, sizeof(tp->esc_par));
1412 tp->esc_ques = (ch == '?');
1416 if (tp->esc_state == ESgetpars) {
1417 if (ch == ';' && tp->esc_npar < ESCAPE_NPAR - 1) {
1421 if (ch >= '0' && ch <= '9') {
1422 tp->esc_par[tp->esc_npar] *= 10;
1423 tp->esc_par[tp->esc_npar] += ch - '0';
1427 tp->esc_state = ESnormal;
1428 if (ch == 'n' && !tp->esc_ques) {
1429 if (tp->esc_par[0] == 5) /* Status report. */
1430 kbd_puts_queue(&tp->port, "\033[0n");
1431 else if (tp->esc_par[0] == 6) { /* Cursor report. */
1433 sprintf(buf, "\033[%d;%dR", tp->cy + 1, tp->cx + 1);
1434 kbd_puts_queue(&tp->port, buf);
1442 tty3270_set_attributes(tp);
1444 case 'H': /* Set cursor position. */
1446 tty3270_goto_xy(tp, tty3270_getpar(tp, 1) - 1,
1447 tty3270_getpar(tp, 0) - 1);
1449 case 'd': /* Set y position. */
1450 tty3270_goto_xy(tp, tp->cx, tty3270_getpar(tp, 0) - 1);
1452 case 'A': /* Cursor up. */
1454 tty3270_goto_xy(tp, tp->cx, tp->cy - tty3270_getpar(tp, 0));
1456 case 'B': /* Cursor down. */
1459 tty3270_goto_xy(tp, tp->cx, tp->cy + tty3270_getpar(tp, 0));
1461 case 'C': /* Cursor forward. */
1463 tty3270_goto_xy(tp, tp->cx + tty3270_getpar(tp, 0), tp->cy);
1465 case 'D': /* Cursor backward. */
1466 tty3270_goto_xy(tp, tp->cx - tty3270_getpar(tp, 0), tp->cy);
1468 case 'G': /* Set x position. */
1470 tty3270_goto_xy(tp, tty3270_getpar(tp, 0), tp->cy);
1472 case 'X': /* Erase Characters. */
1473 tty3270_erase_characters(tp, tty3270_getpar(tp, 0));
1475 case 'J': /* Erase display. */
1476 tty3270_erase_display(tp, tp->esc_par[0]);
1478 case 'K': /* Erase line. */
1479 tty3270_erase_line(tp, tp->esc_par[0]);
1481 case 'P': /* Delete characters. */
1482 tty3270_delete_characters(tp, tty3270_getpar(tp, 0));
1484 case '@': /* Insert characters. */
1485 tty3270_insert_characters(tp, tty3270_getpar(tp, 0));
1487 case 's': /* Save cursor position. */
1488 tp->saved_cx = tp->cx;
1489 tp->saved_cy = tp->cy;
1490 tp->saved_highlight = tp->highlight;
1491 tp->saved_f_color = tp->f_color;
1493 case 'u': /* Restore cursor position. */
1494 tty3270_convert_line(tp, tp->cy);
1495 tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
1496 tp->highlight = tp->saved_highlight;
1497 tp->f_color = tp->saved_f_color;
1503 * String write routine for 3270 ttys
1506 tty3270_do_write(struct tty3270 *tp, struct tty_struct *tty,
1507 const unsigned char *buf, int count)
1511 spin_lock_bh(&tp->view.lock);
1512 for (i_msg = 0; !tty->stopped && i_msg < count; i_msg++) {
1513 if (tp->esc_state != 0) {
1514 /* Continue escape sequence. */
1515 tty3270_escape_sequence(tp, buf[i_msg]);
1519 switch (buf[i_msg]) {
1520 case 0x07: /* '\a' -- Alarm */
1521 tp->wcc |= TW_PLUSALARM;
1523 case 0x08: /* Backspace. */
1526 tty3270_put_character(tp, ' ');
1529 case 0x09: /* '\t' -- Tabulate */
1530 for (i = tp->cx % 8; i < 8; i++) {
1531 if (tp->cx >= tp->view.cols) {
1536 tty3270_put_character(tp, ' ');
1540 case 0x0a: /* '\n' -- New Line */
1544 case 0x0c: /* '\f' -- Form Feed */
1545 tty3270_erase_display(tp, 2);
1546 tp->cx = tp->cy = 0;
1548 case 0x0d: /* '\r' -- Carriage Return */
1551 case 0x0f: /* SuSE "exit alternate mode" */
1553 case 0x1b: /* Start escape sequence. */
1554 tty3270_escape_sequence(tp, buf[i_msg]);
1556 default: /* Insert normal character. */
1557 if (tp->cx >= tp->view.cols) {
1561 tty3270_put_character(tp, buf[i_msg]);
1566 /* Convert current line to 3270 data fragment. */
1567 tty3270_convert_line(tp, tp->cy);
1569 /* Setup timer to update display after 1/10 second */
1570 if (!timer_pending(&tp->timer))
1571 tty3270_set_timer(tp, HZ/10);
1573 spin_unlock_bh(&tp->view.lock);
1577 * String write routine for 3270 ttys
1580 tty3270_write(struct tty_struct * tty,
1581 const unsigned char *buf, int count)
1585 tp = tty->driver_data;
1588 if (tp->char_count > 0) {
1589 tty3270_do_write(tp, tty, tp->char_buf, tp->char_count);
1592 tty3270_do_write(tp, tty, buf, count);
1597 * Put single characters to the ttys character buffer
1599 static int tty3270_put_char(struct tty_struct *tty, unsigned char ch)
1603 tp = tty->driver_data;
1604 if (!tp || tp->char_count >= TTY3270_CHAR_BUF_SIZE)
1606 tp->char_buf[tp->char_count++] = ch;
1611 * Flush all characters from the ttys characeter buffer put there
1612 * by tty3270_put_char.
1615 tty3270_flush_chars(struct tty_struct *tty)
1619 tp = tty->driver_data;
1622 if (tp->char_count > 0) {
1623 tty3270_do_write(tp, tty, tp->char_buf, tp->char_count);
1629 * Returns the number of characters in the output buffer. This is
1630 * used in tty_wait_until_sent to wait until all characters have
1631 * appeared on the screen.
1634 tty3270_chars_in_buffer(struct tty_struct *tty)
1640 tty3270_flush_buffer(struct tty_struct *tty)
1645 * Check for visible/invisible input switches
1648 tty3270_set_termios(struct tty_struct *tty, struct ktermios *old)
1653 tp = tty->driver_data;
1656 spin_lock_bh(&tp->view.lock);
1657 if (L_ICANON(tty)) {
1658 new = L_ECHO(tty) ? TF_INPUT: TF_INPUTN;
1659 if (new != tp->inattr) {
1661 tty3270_update_prompt(tp, NULL, 0);
1662 tty3270_set_timer(tp, 1);
1665 spin_unlock_bh(&tp->view.lock);
1669 * Disable reading from a 3270 tty
1672 tty3270_throttle(struct tty_struct * tty)
1676 tp = tty->driver_data;
1683 * Enable reading from a 3270 tty
1686 tty3270_unthrottle(struct tty_struct * tty)
1690 tp = tty->driver_data;
1695 tty3270_issue_read(tp, 1);
1699 * Hang up the tty device.
1702 tty3270_hangup(struct tty_struct *tty)
1708 tty3270_wait_until_sent(struct tty_struct *tty, int timeout)
1712 static int tty3270_ioctl(struct tty_struct *tty, unsigned int cmd,
1717 tp = tty->driver_data;
1720 if (tty->flags & (1 << TTY_IO_ERROR))
1722 return kbd_ioctl(tp->kbd, cmd, arg);
1725 #ifdef CONFIG_COMPAT
1726 static long tty3270_compat_ioctl(struct tty_struct *tty,
1727 unsigned int cmd, unsigned long arg)
1731 tp = tty->driver_data;
1734 if (tty->flags & (1 << TTY_IO_ERROR))
1736 return kbd_ioctl(tp->kbd, cmd, (unsigned long)compat_ptr(arg));
1740 static const struct tty_operations tty3270_ops = {
1741 .open = tty3270_open,
1742 .close = tty3270_close,
1743 .write = tty3270_write,
1744 .put_char = tty3270_put_char,
1745 .flush_chars = tty3270_flush_chars,
1746 .write_room = tty3270_write_room,
1747 .chars_in_buffer = tty3270_chars_in_buffer,
1748 .flush_buffer = tty3270_flush_buffer,
1749 .throttle = tty3270_throttle,
1750 .unthrottle = tty3270_unthrottle,
1751 .hangup = tty3270_hangup,
1752 .wait_until_sent = tty3270_wait_until_sent,
1753 .ioctl = tty3270_ioctl,
1754 #ifdef CONFIG_COMPAT
1755 .compat_ioctl = tty3270_compat_ioctl,
1757 .set_termios = tty3270_set_termios
1761 * 3270 tty registration code called from tty_init().
1762 * Most kernel services (incl. kmalloc) are available at this poimt.
1764 static int __init tty3270_init(void)
1766 struct tty_driver *driver;
1769 driver = alloc_tty_driver(RAW3270_MAXDEVS);
1774 * Initialize the tty_driver structure
1775 * Entries in tty3270_driver that are NOT initialized:
1776 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1778 driver->driver_name = "ttyTUB";
1779 driver->name = "ttyTUB";
1780 driver->major = IBM_TTY3270_MAJOR;
1781 driver->minor_start = RAW3270_FIRSTMINOR;
1782 driver->type = TTY_DRIVER_TYPE_SYSTEM;
1783 driver->subtype = SYSTEM_TYPE_TTY;
1784 driver->init_termios = tty_std_termios;
1785 driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_DYNAMIC_DEV;
1786 tty_set_operations(driver, &tty3270_ops);
1787 ret = tty_register_driver(driver);
1789 put_tty_driver(driver);
1792 tty3270_driver = driver;
1799 struct tty_driver *driver;
1801 driver = tty3270_driver;
1802 tty3270_driver = NULL;
1803 tty_unregister_driver(driver);
1804 tty3270_del_views();
1807 MODULE_LICENSE("GPL");
1808 MODULE_ALIAS_CHARDEV_MAJOR(IBM_TTY3270_MAJOR);
1810 module_init(tty3270_init);
1811 module_exit(tty3270_exit);