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>
23 #include <asm/ccwdev.h>
25 #include <asm/ebcdic.h>
26 #include <asm/uaccess.h>
32 #define TTY3270_CHAR_BUF_SIZE 256
33 #define TTY3270_OUTPUT_BUFFER_SIZE 1024
34 #define TTY3270_STRING_PAGES 5
36 struct tty_driver *tty3270_driver;
37 static int tty3270_max_index;
39 struct raw3270_fn tty3270_fn;
42 unsigned char character;
43 unsigned char highlight;
44 unsigned char f_color;
48 struct tty3270_cell *cells;
55 * The main tty view data structure.
57 * 1) describe line orientation & lines list concept against screen
58 * 2) describe conversion of screen to lines
59 * 3) describe line format.
62 struct raw3270_view view;
63 struct tty_struct *tty; /* Pointer to tty structure */
64 void **freemem_pages; /* Array of pages used for freemem. */
65 struct list_head freemem; /* List of free memory for strings. */
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. */
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;
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. */
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;
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. */
105 /* Character array for put_char/flush_chars. */
106 unsigned int char_count;
107 char char_buf[TTY3270_CHAR_BUF_SIZE];
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. */
115 #define TTY_UPDATE_ALL 15
117 static void tty3270_update(struct tty3270 *);
120 * Setup timeout for a device. On timeout trigger an update.
123 tty3270_set_timer(struct tty3270 *tp, int expires)
126 if (timer_pending(&tp->timer) && del_timer(&tp->timer))
127 raw3270_put_view(&tp->view);
130 if (timer_pending(&tp->timer) &&
131 mod_timer(&tp->timer, jiffies + expires))
133 raw3270_get_view(&tp->view);
134 tp->timer.function = (void (*)(unsigned long)) tty3270_update;
135 tp->timer.data = (unsigned long) tp;
136 tp->timer.expires = jiffies + expires;
137 add_timer(&tp->timer);
141 * The input line are the two last lines of the screen.
144 tty3270_update_prompt(struct tty3270 *tp, char *input, int count)
151 line->string[5] = TF_INMDT;
153 line->string[5] = tp->inattr;
154 if (count > tp->view.cols * 2 - 11)
155 count = tp->view.cols * 2 - 11;
156 memcpy(line->string + 6, input, count);
157 line->string[6 + count] = TO_IC;
158 /* Clear to end of input line. */
159 if (count < tp->view.cols * 2 - 11) {
160 line->string[7 + count] = TO_RA;
161 line->string[10 + count] = 0;
162 off = tp->view.cols * tp->view.rows - 9;
163 raw3270_buffer_address(tp->view.dev, line->string+count+8, off);
164 line->len = 11 + count;
166 line->len = 7 + count;
167 tp->update_flags |= TTY_UPDATE_INPUT;
171 tty3270_create_prompt(struct tty3270 *tp)
173 static const unsigned char blueprint[] =
174 { TO_SBA, 0, 0, 0x6e, TO_SF, TF_INPUT,
175 /* empty input string */
176 TO_IC, TO_RA, 0, 0, 0 };
180 line = alloc_string(&tp->freemem,
181 sizeof(blueprint) + tp->view.cols * 2 - 9);
183 tp->inattr = TF_INPUT;
184 /* Copy blueprint to status line */
185 memcpy(line->string, blueprint, sizeof(blueprint));
186 line->len = sizeof(blueprint);
187 /* Set output offsets. */
188 offset = tp->view.cols * (tp->view.rows - 2);
189 raw3270_buffer_address(tp->view.dev, line->string + 1, offset);
190 offset = tp->view.cols * tp->view.rows - 9;
191 raw3270_buffer_address(tp->view.dev, line->string + 8, offset);
193 /* Allocate input string for reading. */
194 tp->input = alloc_string(&tp->freemem, tp->view.cols * 2 - 9 + 6);
198 * The status line is the last line of the screen. It shows the string
199 * "Running"/"Holding" in the lower right corner of the screen.
202 tty3270_update_status(struct tty3270 * tp)
206 str = (tp->nr_up != 0) ? "History" : "Running";
207 memcpy(tp->status->string + 8, str, 7);
208 codepage_convert(tp->view.ascebc, tp->status->string + 8, 7);
209 tp->update_flags |= TTY_UPDATE_STATUS;
213 tty3270_create_status(struct tty3270 * tp)
215 static const unsigned char blueprint[] =
216 { TO_SBA, 0, 0, TO_SF, TF_LOG, TO_SA, TAT_COLOR, TAC_GREEN,
217 0, 0, 0, 0, 0, 0, 0, TO_SF, TF_LOG, TO_SA, TAT_COLOR,
222 line = alloc_string(&tp->freemem,sizeof(blueprint));
224 /* Copy blueprint to status line */
225 memcpy(line->string, blueprint, sizeof(blueprint));
226 /* Set address to start of status string (= last 9 characters). */
227 offset = tp->view.cols * tp->view.rows - 9;
228 raw3270_buffer_address(tp->view.dev, line->string + 1, offset);
232 * Set output offsets to 3270 datastream fragment of a tty string.
233 * (TO_SBA offset at the start and TO_RA offset at the end of the string)
236 tty3270_update_string(struct tty3270 *tp, struct string *line, int nr)
240 raw3270_buffer_address(tp->view.dev, line->string + 1,
242 cp = line->string + line->len - 4;
244 raw3270_buffer_address(tp->view.dev, cp + 1,
245 tp->view.cols * (nr + 1));
249 * Rebuild update list to print all lines.
252 tty3270_rebuild_update(struct tty3270 *tp)
254 struct string *s, *n;
258 * Throw away update list and create a new one,
259 * containing all lines that will fit on the screen.
261 list_for_each_entry_safe(s, n, &tp->update, update)
262 list_del_init(&s->update);
263 line = tp->view.rows - 3;
265 list_for_each_entry_reverse(s, &tp->lines, list) {
270 tty3270_update_string(tp, s, line);
271 list_add(&s->update, &tp->update);
275 tp->update_flags |= TTY_UPDATE_LIST;
279 * Alloc string for size bytes. If there is not enough room in
280 * freemem, free strings until there is room.
282 static struct string *
283 tty3270_alloc_string(struct tty3270 *tp, size_t size)
285 struct string *s, *n;
287 s = alloc_string(&tp->freemem, size);
290 list_for_each_entry_safe(s, n, &tp->lines, list) {
291 BUG_ON(tp->nr_lines <= tp->view.rows - 2);
293 if (!list_empty(&s->update))
294 list_del(&s->update);
296 if (free_string(&tp->freemem, s) >= size)
299 s = alloc_string(&tp->freemem, size);
301 if (tp->nr_up != 0 &&
302 tp->nr_up + tp->view.rows - 2 >= tp->nr_lines) {
303 tp->nr_up = tp->nr_lines - tp->view.rows + 2;
304 tty3270_rebuild_update(tp);
305 tty3270_update_status(tp);
311 * Add an empty line to the list.
314 tty3270_blank_line(struct tty3270 *tp)
316 static const unsigned char blueprint[] =
317 { TO_SBA, 0, 0, TO_SA, TAT_EXTHI, TAX_RESET,
318 TO_SA, TAT_COLOR, TAC_RESET, TO_RA, 0, 0, 0 };
321 s = tty3270_alloc_string(tp, sizeof(blueprint));
322 memcpy(s->string, blueprint, sizeof(blueprint));
323 s->len = sizeof(blueprint);
324 list_add_tail(&s->list, &tp->lines);
331 * Write request completion callback.
334 tty3270_write_callback(struct raw3270_request *rq, void *data)
338 tp = (struct tty3270 *) rq->view;
340 /* Write wasn't successfull. Refresh all. */
341 tty3270_rebuild_update(tp);
342 tp->update_flags = TTY_UPDATE_ALL;
343 tty3270_set_timer(tp, 1);
345 raw3270_request_reset(rq);
346 xchg(&tp->write, rq);
350 * Update 3270 display.
353 tty3270_update(struct tty3270 *tp)
355 static char invalid_sba[2] = { 0xff, 0xff };
356 struct raw3270_request *wrq;
357 unsigned long updated;
358 struct string *s, *n;
362 wrq = xchg(&tp->write, 0);
364 tty3270_set_timer(tp, 1);
368 spin_lock(&tp->view.lock);
370 if (tp->update_flags & TTY_UPDATE_ERASE) {
371 /* Use erase write alternate to erase display. */
372 raw3270_request_set_cmd(wrq, TC_EWRITEA);
373 updated |= TTY_UPDATE_ERASE;
375 raw3270_request_set_cmd(wrq, TC_WRITE);
377 raw3270_request_add_data(wrq, &tp->wcc, 1);
381 * Update status line.
383 if (tp->update_flags & TTY_UPDATE_STATUS)
384 if (raw3270_request_add_data(wrq, tp->status->string,
385 tp->status->len) == 0)
386 updated |= TTY_UPDATE_STATUS;
391 if (tp->update_flags & TTY_UPDATE_INPUT)
392 if (raw3270_request_add_data(wrq, tp->prompt->string,
393 tp->prompt->len) == 0)
394 updated |= TTY_UPDATE_INPUT;
398 if (tp->update_flags & TTY_UPDATE_LIST) {
399 /* Write strings in the update list to the screen. */
400 list_for_each_entry_safe(s, n, &tp->update, update) {
404 * Skip TO_SBA at the start of the string if the
405 * last output position matches the start address
408 if (s->string[1] == sba[0] && s->string[2] == sba[1])
410 if (raw3270_request_add_data(wrq, str, len) != 0)
412 list_del_init(&s->update);
413 sba = s->string + s->len - 3;
415 if (list_empty(&tp->update))
416 updated |= TTY_UPDATE_LIST;
418 wrq->callback = tty3270_write_callback;
419 rc = raw3270_start(&tp->view, wrq);
421 tp->update_flags &= ~updated;
422 if (tp->update_flags)
423 tty3270_set_timer(tp, 1);
425 raw3270_request_reset(wrq);
426 xchg(&tp->write, wrq);
428 spin_unlock(&tp->view.lock);
429 raw3270_put_view(&tp->view);
436 tty3270_rcl_add(struct tty3270 *tp, char *input, int len)
443 if (tp->rcl_nr >= tp->rcl_max) {
444 s = list_entry(tp->rcl_lines.next, struct string, list);
446 free_string(&tp->freemem, s);
449 s = tty3270_alloc_string(tp, len);
450 memcpy(s->string, input, len);
451 list_add_tail(&s->list, &tp->rcl_lines);
456 tty3270_rcl_backward(struct kbd_data *kbd)
461 tp = kbd->tty->driver_data;
462 spin_lock_bh(&tp->view.lock);
463 if (tp->inattr == TF_INPUT) {
464 if (tp->rcl_walk && tp->rcl_walk->prev != &tp->rcl_lines)
465 tp->rcl_walk = tp->rcl_walk->prev;
466 else if (!list_empty(&tp->rcl_lines))
467 tp->rcl_walk = tp->rcl_lines.prev;
469 list_entry(tp->rcl_walk, struct string, list) : NULL;
471 s = list_entry(tp->rcl_walk, struct string, list);
472 tty3270_update_prompt(tp, s->string, s->len);
474 tty3270_update_prompt(tp, NULL, 0);
475 tty3270_set_timer(tp, 1);
477 spin_unlock_bh(&tp->view.lock);
481 * Deactivate tty view.
484 tty3270_exit_tty(struct kbd_data *kbd)
488 tp = kbd->tty->driver_data;
489 raw3270_deactivate_view(&tp->view);
493 * Scroll forward in history.
496 tty3270_scroll_forward(struct kbd_data *kbd)
501 tp = kbd->tty->driver_data;
502 spin_lock_bh(&tp->view.lock);
503 nr_up = tp->nr_up - tp->view.rows + 2;
506 if (nr_up != tp->nr_up) {
508 tty3270_rebuild_update(tp);
509 tty3270_update_status(tp);
510 tty3270_set_timer(tp, 1);
512 spin_unlock_bh(&tp->view.lock);
516 * Scroll backward in history.
519 tty3270_scroll_backward(struct kbd_data *kbd)
524 tp = kbd->tty->driver_data;
525 spin_lock_bh(&tp->view.lock);
526 nr_up = tp->nr_up + tp->view.rows - 2;
527 if (nr_up + tp->view.rows - 2 > tp->nr_lines)
528 nr_up = tp->nr_lines - tp->view.rows + 2;
529 if (nr_up != tp->nr_up) {
531 tty3270_rebuild_update(tp);
532 tty3270_update_status(tp);
533 tty3270_set_timer(tp, 1);
535 spin_unlock_bh(&tp->view.lock);
539 * Pass input line to tty.
542 tty3270_read_tasklet(struct raw3270_request *rrq)
544 static char kreset_data = TW_KR;
549 tp = (struct tty3270 *) rrq->view;
550 spin_lock_bh(&tp->view.lock);
552 * Two AID keys are special: For 0x7d (enter) the input line
553 * has to be emitted to the tty and for 0x6d the screen
554 * needs to be redrawn.
558 if (tp->input->string[0] == 0x7d) {
559 /* Enter: write input to tty. */
560 input = tp->input->string + 6;
561 len = tp->input->len - 6 - rrq->rescnt;
562 if (tp->inattr != TF_INPUTN)
563 tty3270_rcl_add(tp, input, len);
566 tty3270_rebuild_update(tp);
567 tty3270_update_status(tp);
569 /* Clear input area. */
570 tty3270_update_prompt(tp, NULL, 0);
571 tty3270_set_timer(tp, 1);
572 } else if (tp->input->string[0] == 0x6d) {
573 /* Display has been cleared. Redraw. */
574 tty3270_rebuild_update(tp);
575 tp->update_flags = TTY_UPDATE_ALL;
576 tty3270_set_timer(tp, 1);
578 spin_unlock_bh(&tp->view.lock);
580 /* Start keyboard reset command. */
581 raw3270_request_reset(tp->kreset);
582 raw3270_request_set_cmd(tp->kreset, TC_WRITE);
583 raw3270_request_add_data(tp->kreset, &kreset_data, 1);
584 raw3270_start(&tp->view, tp->kreset);
586 /* Emit input string. */
589 kbd_keycode(tp->kbd, *input++);
590 /* Emit keycode for AID byte. */
591 kbd_keycode(tp->kbd, 256 + tp->input->string[0]);
594 raw3270_request_reset(rrq);
595 xchg(&tp->read, rrq);
596 raw3270_put_view(&tp->view);
600 * Read request completion callback.
603 tty3270_read_callback(struct raw3270_request *rq, void *data)
605 raw3270_get_view(rq->view);
606 /* Schedule tasklet to pass input to tty. */
607 tasklet_schedule(&((struct tty3270 *) rq->view)->readlet);
611 * Issue a read request. Call with device lock.
614 tty3270_issue_read(struct tty3270 *tp, int lock)
616 struct raw3270_request *rrq;
619 rrq = xchg(&tp->read, 0);
621 /* Read already scheduled. */
623 rrq->callback = tty3270_read_callback;
624 rrq->callback_data = tp;
625 raw3270_request_set_cmd(rrq, TC_READMOD);
626 raw3270_request_set_data(rrq, tp->input->string, tp->input->len);
627 /* Issue the read modified request. */
629 rc = raw3270_start(&tp->view, rrq);
631 rc = raw3270_start_irq(&tp->view, rrq);
633 raw3270_request_reset(rrq);
634 xchg(&tp->read, rrq);
639 * Switch to the tty view.
642 tty3270_activate(struct raw3270_view *view)
647 tp = (struct tty3270 *) view;
648 spin_lock_irqsave(&tp->view.lock, flags);
650 tty3270_rebuild_update(tp);
651 tty3270_update_status(tp);
652 tp->update_flags = TTY_UPDATE_ALL;
653 tty3270_set_timer(tp, 1);
654 spin_unlock_irqrestore(&tp->view.lock, flags);
659 tty3270_deactivate(struct raw3270_view *view)
664 tty3270_irq(struct tty3270 *tp, struct raw3270_request *rq, struct irb *irb)
666 /* Handle ATTN. Schedule tasklet to read aid. */
667 if (irb->scsw.dstat & DEV_STAT_ATTENTION) {
669 tty3270_issue_read(tp, 0);
675 if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK)
678 /* Normal end. Copy residual count. */
679 rq->rescnt = irb->scsw.count;
681 return RAW3270_IO_DONE;
685 * Allocate tty3270 structure.
687 static struct tty3270 *
688 tty3270_alloc_view(void)
693 tp = kzalloc(sizeof(struct tty3270), GFP_KERNEL);
697 kmalloc(sizeof(void *) * TTY3270_STRING_PAGES, GFP_KERNEL);
698 if (!tp->freemem_pages)
700 INIT_LIST_HEAD(&tp->freemem);
701 for (pages = 0; pages < TTY3270_STRING_PAGES; pages++) {
702 tp->freemem_pages[pages] = (void *)
703 __get_free_pages(GFP_KERNEL|GFP_DMA, 0);
704 if (!tp->freemem_pages[pages])
706 add_string_memory(&tp->freemem,
707 tp->freemem_pages[pages], PAGE_SIZE);
709 tp->write = raw3270_request_alloc(TTY3270_OUTPUT_BUFFER_SIZE);
710 if (IS_ERR(tp->write))
712 tp->read = raw3270_request_alloc(0);
713 if (IS_ERR(tp->read))
715 tp->kreset = raw3270_request_alloc(1);
716 if (IS_ERR(tp->kreset))
718 tp->kbd = kbd_alloc();
724 raw3270_request_free(tp->kreset);
726 raw3270_request_free(tp->read);
728 raw3270_request_free(tp->write);
731 free_pages((unsigned long) tp->freemem_pages[pages], 0);
732 kfree(tp->freemem_pages);
736 return ERR_PTR(-ENOMEM);
740 * Free tty3270 structure.
743 tty3270_free_view(struct tty3270 *tp)
748 raw3270_request_free(tp->kreset);
749 raw3270_request_free(tp->read);
750 raw3270_request_free(tp->write);
751 for (pages = 0; pages < TTY3270_STRING_PAGES; pages++)
752 free_pages((unsigned long) tp->freemem_pages[pages], 0);
753 kfree(tp->freemem_pages);
758 * Allocate tty3270 screen.
761 tty3270_alloc_screen(struct tty3270 *tp)
766 size = sizeof(struct tty3270_line) * (tp->view.rows - 2);
767 tp->screen = kzalloc(size, GFP_KERNEL);
770 for (lines = 0; lines < tp->view.rows - 2; lines++) {
771 size = sizeof(struct tty3270_cell) * tp->view.cols;
772 tp->screen[lines].cells = kzalloc(size, GFP_KERNEL);
773 if (!tp->screen[lines].cells)
779 kfree(tp->screen[lines].cells);
786 * Free tty3270 screen.
789 tty3270_free_screen(struct tty3270 *tp)
793 for (lines = 0; lines < tp->view.rows - 2; lines++)
794 kfree(tp->screen[lines].cells);
799 * Unlink tty3270 data structure from tty.
802 tty3270_release(struct raw3270_view *view)
805 struct tty_struct *tty;
807 tp = (struct tty3270 *) view;
810 tty->driver_data = NULL;
811 tp->tty = tp->kbd->tty = NULL;
813 raw3270_put_view(&tp->view);
818 * Free tty3270 data structure
821 tty3270_free(struct raw3270_view *view)
823 tty3270_free_screen((struct tty3270 *) view);
824 tty3270_free_view((struct tty3270 *) view);
828 * Delayed freeing of tty3270 views.
831 tty3270_del_views(void)
836 for (i = 0; i < tty3270_max_index; i++) {
837 tp = (struct tty3270 *)
838 raw3270_find_view(&tty3270_fn, i + RAW3270_FIRSTMINOR);
840 raw3270_del_view(&tp->view);
844 struct raw3270_fn tty3270_fn = {
845 .activate = tty3270_activate,
846 .deactivate = tty3270_deactivate,
847 .intv = (void *) tty3270_irq,
848 .release = tty3270_release,
853 * This routine is called whenever a 3270 tty is opened.
856 tty3270_open(struct tty_struct *tty, struct file * filp)
863 /* Check if the tty3270 is already there. */
864 tp = (struct tty3270 *)
865 raw3270_find_view(&tty3270_fn,
866 tty->index + RAW3270_FIRSTMINOR);
868 tty->driver_data = tp;
869 tty->winsize.ws_row = tp->view.rows - 2;
870 tty->winsize.ws_col = tp->view.cols;
871 tty->low_latency = 0;
874 tp->inattr = TF_INPUT;
877 if (tty3270_max_index < tty->index + 1)
878 tty3270_max_index = tty->index + 1;
880 /* Quick exit if there is no device for tty->index. */
881 if (PTR_ERR(tp) == -ENODEV)
884 /* Allocate tty3270 structure on first open. */
885 tp = tty3270_alloc_view();
889 INIT_LIST_HEAD(&tp->lines);
890 INIT_LIST_HEAD(&tp->update);
891 INIT_LIST_HEAD(&tp->rcl_lines);
893 init_timer(&tp->timer);
894 tasklet_init(&tp->readlet,
895 (void (*)(unsigned long)) tty3270_read_tasklet,
896 (unsigned long) tp->read);
898 rc = raw3270_add_view(&tp->view, &tty3270_fn,
899 tty->index + RAW3270_FIRSTMINOR);
901 tty3270_free_view(tp);
905 rc = tty3270_alloc_screen(tp);
907 raw3270_put_view(&tp->view);
908 raw3270_del_view(&tp->view);
913 tty->low_latency = 0;
914 tty->driver_data = tp;
915 tty->winsize.ws_row = tp->view.rows - 2;
916 tty->winsize.ws_col = tp->view.cols;
918 tty3270_create_prompt(tp);
919 tty3270_create_status(tp);
920 tty3270_update_status(tp);
922 /* Create blank line for every line in the tty output area. */
923 for (i = 0; i < tp->view.rows - 2; i++)
924 tty3270_blank_line(tp);
927 tp->kbd->fn_handler[KVAL(K_INCRCONSOLE)] = tty3270_exit_tty;
928 tp->kbd->fn_handler[KVAL(K_SCROLLBACK)] = tty3270_scroll_backward;
929 tp->kbd->fn_handler[KVAL(K_SCROLLFORW)] = tty3270_scroll_forward;
930 tp->kbd->fn_handler[KVAL(K_CONS)] = tty3270_rcl_backward;
931 kbd_ascebc(tp->kbd, tp->view.ascebc);
933 raw3270_activate_view(&tp->view);
938 * This routine is called when the 3270 tty is closed. We wait
939 * for the remaining request to be completed. Then we clean up.
942 tty3270_close(struct tty_struct *tty, struct file * filp)
948 tp = (struct tty3270 *) tty->driver_data;
950 tty->driver_data = NULL;
951 tp->tty = tp->kbd->tty = NULL;
952 raw3270_put_view(&tp->view);
957 * We always have room.
960 tty3270_write_room(struct tty_struct *tty)
966 * Insert character into the screen at the current position with the
967 * current color and highlight. This function does NOT do cursor movement.
970 tty3270_put_character(struct tty3270 *tp, char ch)
972 struct tty3270_line *line;
973 struct tty3270_cell *cell;
975 line = tp->screen + tp->cy;
976 if (line->len <= tp->cx) {
977 while (line->len < tp->cx) {
978 cell = line->cells + line->len;
979 cell->character = tp->view.ascebc[' '];
980 cell->highlight = tp->highlight;
981 cell->f_color = tp->f_color;
986 cell = line->cells + tp->cx;
987 cell->character = tp->view.ascebc[(unsigned int) ch];
988 cell->highlight = tp->highlight;
989 cell->f_color = tp->f_color;
993 * Convert a tty3270_line to a 3270 data fragment usable for output.
996 tty3270_convert_line(struct tty3270 *tp, int line_nr)
998 struct tty3270_line *line;
999 struct tty3270_cell *cell;
1000 struct string *s, *n;
1001 unsigned char highlight;
1002 unsigned char f_color;
1006 /* Determine how long the fragment will be. */
1007 flen = 3; /* Prefix (TO_SBA). */
1008 line = tp->screen + line_nr;
1010 highlight = TAX_RESET;
1011 f_color = TAC_RESET;
1012 for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
1013 if (cell->highlight != highlight) {
1014 flen += 3; /* TO_SA to switch highlight. */
1015 highlight = cell->highlight;
1017 if (cell->f_color != f_color) {
1018 flen += 3; /* TO_SA to switch color. */
1019 f_color = cell->f_color;
1022 if (highlight != TAX_RESET)
1023 flen += 3; /* TO_SA to reset hightlight. */
1024 if (f_color != TAC_RESET)
1025 flen += 3; /* TO_SA to reset color. */
1026 if (line->len < tp->view.cols)
1027 flen += 4; /* Postfix (TO_RA). */
1029 /* Find the line in the list. */
1030 i = tp->view.rows - 2 - line_nr;
1031 list_for_each_entry_reverse(s, &tp->lines, list)
1035 * Check if the line needs to get reallocated.
1037 if (s->len != flen) {
1038 /* Reallocate string. */
1039 n = tty3270_alloc_string(tp, flen);
1040 list_add(&n->list, &s->list);
1041 list_del_init(&s->list);
1042 if (!list_empty(&s->update))
1043 list_del_init(&s->update);
1044 free_string(&tp->freemem, s);
1048 /* Write 3270 data fragment. */
1054 highlight = TAX_RESET;
1055 f_color = TAC_RESET;
1056 for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
1057 if (cell->highlight != highlight) {
1060 *cp++ = cell->highlight;
1061 highlight = cell->highlight;
1063 if (cell->f_color != f_color) {
1066 *cp++ = cell->f_color;
1067 f_color = cell->f_color;
1069 *cp++ = cell->character;
1071 if (highlight != TAX_RESET) {
1076 if (f_color != TAC_RESET) {
1081 if (line->len < tp->view.cols) {
1088 if (tp->nr_up + line_nr < tp->view.rows - 2) {
1089 /* Line is currently visible on screen. */
1090 tty3270_update_string(tp, s, line_nr);
1091 /* Add line to update list. */
1092 if (list_empty(&s->update)) {
1093 list_add_tail(&s->update, &tp->update);
1094 tp->update_flags |= TTY_UPDATE_LIST;
1100 * Do carriage return.
1103 tty3270_cr(struct tty3270 *tp)
1112 tty3270_lf(struct tty3270 *tp)
1114 struct tty3270_line temp;
1117 tty3270_convert_line(tp, tp->cy);
1118 if (tp->cy < tp->view.rows - 3) {
1122 /* Last line just filled up. Add new, blank line. */
1123 tty3270_blank_line(tp);
1124 temp = tp->screen[0];
1126 for (i = 0; i < tp->view.rows - 3; i++)
1127 tp->screen[i] = tp->screen[i+1];
1128 tp->screen[tp->view.rows - 3] = temp;
1129 tty3270_rebuild_update(tp);
1133 tty3270_ri(struct tty3270 *tp)
1136 tty3270_convert_line(tp, tp->cy);
1142 * Insert characters at current position.
1145 tty3270_insert_characters(struct tty3270 *tp, int n)
1147 struct tty3270_line *line;
1150 line = tp->screen + tp->cy;
1151 while (line->len < tp->cx) {
1152 line->cells[line->len].character = tp->view.ascebc[' '];
1153 line->cells[line->len].highlight = TAX_RESET;
1154 line->cells[line->len].f_color = TAC_RESET;
1157 if (n > tp->view.cols - tp->cx)
1158 n = tp->view.cols - tp->cx;
1159 k = min_t(int, line->len - tp->cx, tp->view.cols - tp->cx - n);
1161 line->cells[tp->cx + n + k] = line->cells[tp->cx + k];
1163 if (line->len > tp->view.cols)
1164 line->len = tp->view.cols;
1166 line->cells[tp->cx + n].character = tp->view.ascebc[' '];
1167 line->cells[tp->cx + n].highlight = tp->highlight;
1168 line->cells[tp->cx + n].f_color = tp->f_color;
1173 * Delete characters at current position.
1176 tty3270_delete_characters(struct tty3270 *tp, int n)
1178 struct tty3270_line *line;
1181 line = tp->screen + tp->cy;
1182 if (line->len <= tp->cx)
1184 if (line->len - tp->cx <= n) {
1188 for (i = tp->cx; i + n < line->len; i++)
1189 line->cells[i] = line->cells[i + n];
1194 * Erase characters at current position.
1197 tty3270_erase_characters(struct tty3270 *tp, int n)
1199 struct tty3270_line *line;
1200 struct tty3270_cell *cell;
1202 line = tp->screen + tp->cy;
1203 while (line->len > tp->cx && n-- > 0) {
1204 cell = line->cells + tp->cx++;
1205 cell->character = ' ';
1206 cell->highlight = TAX_RESET;
1207 cell->f_color = TAC_RESET;
1210 tp->cx = min_t(int, tp->cx, tp->view.cols - 1);
1214 * Erase line, 3 different cases:
1215 * Esc [ 0 K Erase from current position to end of line inclusive
1216 * Esc [ 1 K Erase from beginning of line to current position inclusive
1217 * Esc [ 2 K Erase entire line (without moving cursor)
1220 tty3270_erase_line(struct tty3270 *tp, int mode)
1222 struct tty3270_line *line;
1223 struct tty3270_cell *cell;
1226 line = tp->screen + tp->cy;
1229 else if (mode == 1) {
1230 for (i = 0; i < tp->cx; i++) {
1231 cell = line->cells + i;
1232 cell->character = ' ';
1233 cell->highlight = TAX_RESET;
1234 cell->f_color = TAC_RESET;
1236 if (line->len <= tp->cx)
1237 line->len = tp->cx + 1;
1238 } else if (mode == 2)
1240 tty3270_convert_line(tp, tp->cy);
1244 * Erase display, 3 different cases:
1245 * Esc [ 0 J Erase from current position to bottom of screen inclusive
1246 * Esc [ 1 J Erase from top of screen to current position inclusive
1247 * Esc [ 2 J Erase entire screen (without moving the cursor)
1250 tty3270_erase_display(struct tty3270 *tp, int mode)
1255 tty3270_erase_line(tp, 0);
1256 for (i = tp->cy + 1; i < tp->view.rows - 2; i++) {
1257 tp->screen[i].len = 0;
1258 tty3270_convert_line(tp, i);
1260 } else if (mode == 1) {
1261 for (i = 0; i < tp->cy; i++) {
1262 tp->screen[i].len = 0;
1263 tty3270_convert_line(tp, i);
1265 tty3270_erase_line(tp, 1);
1266 } else if (mode == 2) {
1267 for (i = 0; i < tp->view.rows - 2; i++) {
1268 tp->screen[i].len = 0;
1269 tty3270_convert_line(tp, i);
1272 tty3270_rebuild_update(tp);
1276 * Set attributes found in an escape sequence.
1277 * Esc [ <attr> ; <attr> ; ... m
1280 tty3270_set_attributes(struct tty3270 *tp)
1282 static unsigned char f_colors[] = {
1283 TAC_DEFAULT, TAC_RED, TAC_GREEN, TAC_YELLOW, TAC_BLUE,
1284 TAC_PINK, TAC_TURQ, TAC_WHITE, 0, TAC_DEFAULT
1288 for (i = 0; i <= tp->esc_npar; i++) {
1289 attr = tp->esc_par[i];
1292 tp->highlight = TAX_RESET;
1293 tp->f_color = TAC_RESET;
1296 case 4: /* Start underlining. */
1297 tp->highlight = TAX_UNDER;
1299 case 5: /* Start blink. */
1300 tp->highlight = TAX_BLINK;
1302 case 7: /* Start reverse. */
1303 tp->highlight = TAX_REVER;
1305 case 24: /* End underlining */
1306 if (tp->highlight == TAX_UNDER)
1307 tp->highlight = TAX_RESET;
1309 case 25: /* End blink. */
1310 if (tp->highlight == TAX_BLINK)
1311 tp->highlight = TAX_RESET;
1313 case 27: /* End reverse. */
1314 if (tp->highlight == TAX_REVER)
1315 tp->highlight = TAX_RESET;
1317 /* Foreground color. */
1318 case 30: /* Black */
1320 case 32: /* Green */
1321 case 33: /* Yellow */
1323 case 35: /* Magenta */
1325 case 37: /* White */
1326 case 39: /* Black */
1327 tp->f_color = f_colors[attr - 30];
1334 tty3270_getpar(struct tty3270 *tp, int ix)
1336 return (tp->esc_par[ix] > 0) ? tp->esc_par[ix] : 1;
1340 tty3270_goto_xy(struct tty3270 *tp, int cx, int cy)
1342 tp->cx = min_t(int, tp->view.cols - 1, max_t(int, 0, cx));
1343 cy = min_t(int, tp->view.rows - 3, max_t(int, 0, cy));
1345 tty3270_convert_line(tp, tp->cy);
1351 * Process escape sequences. Known sequences:
1352 * Esc 7 Save Cursor Position
1353 * Esc 8 Restore Cursor Position
1354 * Esc [ Pn ; Pn ; .. m Set attributes
1355 * Esc [ Pn ; Pn H Cursor Position
1356 * Esc [ Pn ; Pn f Cursor Position
1357 * Esc [ Pn A Cursor Up
1358 * Esc [ Pn B Cursor Down
1359 * Esc [ Pn C Cursor Forward
1360 * Esc [ Pn D Cursor Backward
1361 * Esc [ Pn G Cursor Horizontal Absolute
1362 * Esc [ Pn X Erase Characters
1363 * Esc [ Ps J Erase in Display
1364 * Esc [ Ps K Erase in Line
1365 * // FIXME: add all the new ones.
1367 * Pn is a numeric parameter, a string of zero or more decimal digits.
1368 * Ps is a selective parameter.
1371 tty3270_escape_sequence(struct tty3270 *tp, char ch)
1373 enum { ESnormal, ESesc, ESsquare, ESgetpars };
1375 if (tp->esc_state == ESnormal) {
1377 /* Starting new escape sequence. */
1378 tp->esc_state = ESesc;
1381 if (tp->esc_state == ESesc) {
1382 tp->esc_state = ESnormal;
1385 tp->esc_state = ESsquare;
1397 case 'Z': /* Respond ID. */
1398 kbd_puts_queue(tp->tty, "\033[?6c");
1400 case '7': /* Save cursor position. */
1401 tp->saved_cx = tp->cx;
1402 tp->saved_cy = tp->cy;
1403 tp->saved_highlight = tp->highlight;
1404 tp->saved_f_color = tp->f_color;
1406 case '8': /* Restore cursor position. */
1407 tty3270_convert_line(tp, tp->cy);
1408 tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
1409 tp->highlight = tp->saved_highlight;
1410 tp->f_color = tp->saved_f_color;
1412 case 'c': /* Reset terminal. */
1413 tp->cx = tp->saved_cx = 0;
1414 tp->cy = tp->saved_cy = 0;
1415 tp->highlight = tp->saved_highlight = TAX_RESET;
1416 tp->f_color = tp->saved_f_color = TAC_RESET;
1417 tty3270_erase_display(tp, 2);
1422 if (tp->esc_state == ESsquare) {
1423 tp->esc_state = ESgetpars;
1424 memset(tp->esc_par, 0, sizeof(tp->esc_par));
1426 tp->esc_ques = (ch == '?');
1430 if (tp->esc_state == ESgetpars) {
1431 if (ch == ';' && tp->esc_npar < ESCAPE_NPAR - 1) {
1435 if (ch >= '0' && ch <= '9') {
1436 tp->esc_par[tp->esc_npar] *= 10;
1437 tp->esc_par[tp->esc_npar] += ch - '0';
1441 tp->esc_state = ESnormal;
1442 if (ch == 'n' && !tp->esc_ques) {
1443 if (tp->esc_par[0] == 5) /* Status report. */
1444 kbd_puts_queue(tp->tty, "\033[0n");
1445 else if (tp->esc_par[0] == 6) { /* Cursor report. */
1447 sprintf(buf, "\033[%d;%dR", tp->cy + 1, tp->cx + 1);
1448 kbd_puts_queue(tp->tty, buf);
1456 tty3270_set_attributes(tp);
1458 case 'H': /* Set cursor position. */
1460 tty3270_goto_xy(tp, tty3270_getpar(tp, 1) - 1,
1461 tty3270_getpar(tp, 0) - 1);
1463 case 'd': /* Set y position. */
1464 tty3270_goto_xy(tp, tp->cx, tty3270_getpar(tp, 0) - 1);
1466 case 'A': /* Cursor up. */
1468 tty3270_goto_xy(tp, tp->cx, tp->cy - tty3270_getpar(tp, 0));
1470 case 'B': /* Cursor down. */
1473 tty3270_goto_xy(tp, tp->cx, tp->cy + tty3270_getpar(tp, 0));
1475 case 'C': /* Cursor forward. */
1477 tty3270_goto_xy(tp, tp->cx + tty3270_getpar(tp, 0), tp->cy);
1479 case 'D': /* Cursor backward. */
1480 tty3270_goto_xy(tp, tp->cx - tty3270_getpar(tp, 0), tp->cy);
1482 case 'G': /* Set x position. */
1484 tty3270_goto_xy(tp, tty3270_getpar(tp, 0), tp->cy);
1486 case 'X': /* Erase Characters. */
1487 tty3270_erase_characters(tp, tty3270_getpar(tp, 0));
1489 case 'J': /* Erase display. */
1490 tty3270_erase_display(tp, tp->esc_par[0]);
1492 case 'K': /* Erase line. */
1493 tty3270_erase_line(tp, tp->esc_par[0]);
1495 case 'P': /* Delete characters. */
1496 tty3270_delete_characters(tp, tty3270_getpar(tp, 0));
1498 case '@': /* Insert characters. */
1499 tty3270_insert_characters(tp, tty3270_getpar(tp, 0));
1501 case 's': /* Save cursor position. */
1502 tp->saved_cx = tp->cx;
1503 tp->saved_cy = tp->cy;
1504 tp->saved_highlight = tp->highlight;
1505 tp->saved_f_color = tp->f_color;
1507 case 'u': /* Restore cursor position. */
1508 tty3270_convert_line(tp, tp->cy);
1509 tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
1510 tp->highlight = tp->saved_highlight;
1511 tp->f_color = tp->saved_f_color;
1517 * String write routine for 3270 ttys
1520 tty3270_do_write(struct tty3270 *tp, const unsigned char *buf, int count)
1524 spin_lock_bh(&tp->view.lock);
1525 for (i_msg = 0; !tp->tty->stopped && i_msg < count; i_msg++) {
1526 if (tp->esc_state != 0) {
1527 /* Continue escape sequence. */
1528 tty3270_escape_sequence(tp, buf[i_msg]);
1532 switch (buf[i_msg]) {
1533 case 0x07: /* '\a' -- Alarm */
1534 tp->wcc |= TW_PLUSALARM;
1536 case 0x08: /* Backspace. */
1539 tty3270_put_character(tp, ' ');
1542 case 0x09: /* '\t' -- Tabulate */
1543 for (i = tp->cx % 8; i < 8; i++) {
1544 if (tp->cx >= tp->view.cols) {
1549 tty3270_put_character(tp, ' ');
1553 case 0x0a: /* '\n' -- New Line */
1557 case 0x0c: /* '\f' -- Form Feed */
1558 tty3270_erase_display(tp, 2);
1559 tp->cx = tp->cy = 0;
1561 case 0x0d: /* '\r' -- Carriage Return */
1564 case 0x0f: /* SuSE "exit alternate mode" */
1566 case 0x1b: /* Start escape sequence. */
1567 tty3270_escape_sequence(tp, buf[i_msg]);
1569 default: /* Insert normal character. */
1570 if (tp->cx >= tp->view.cols) {
1574 tty3270_put_character(tp, buf[i_msg]);
1579 /* Convert current line to 3270 data fragment. */
1580 tty3270_convert_line(tp, tp->cy);
1582 /* Setup timer to update display after 1/10 second */
1583 if (!timer_pending(&tp->timer))
1584 tty3270_set_timer(tp, HZ/10);
1586 spin_unlock_bh(&tp->view.lock);
1590 * String write routine for 3270 ttys
1593 tty3270_write(struct tty_struct * tty,
1594 const unsigned char *buf, int count)
1598 tp = tty->driver_data;
1601 if (tp->char_count > 0) {
1602 tty3270_do_write(tp, tp->char_buf, tp->char_count);
1605 tty3270_do_write(tp, buf, count);
1610 * Put single characters to the ttys character buffer
1613 tty3270_put_char(struct tty_struct *tty, unsigned char ch)
1617 tp = tty->driver_data;
1620 if (tp->char_count < TTY3270_CHAR_BUF_SIZE)
1621 tp->char_buf[tp->char_count++] = ch;
1625 * Flush all characters from the ttys characeter buffer put there
1626 * by tty3270_put_char.
1629 tty3270_flush_chars(struct tty_struct *tty)
1633 tp = tty->driver_data;
1636 if (tp->char_count > 0) {
1637 tty3270_do_write(tp, tp->char_buf, tp->char_count);
1643 * Returns the number of characters in the output buffer. This is
1644 * used in tty_wait_until_sent to wait until all characters have
1645 * appeared on the screen.
1648 tty3270_chars_in_buffer(struct tty_struct *tty)
1654 tty3270_flush_buffer(struct tty_struct *tty)
1659 * Check for visible/invisible input switches
1662 tty3270_set_termios(struct tty_struct *tty, struct termios *old)
1667 tp = tty->driver_data;
1670 spin_lock_bh(&tp->view.lock);
1671 if (L_ICANON(tty)) {
1672 new = L_ECHO(tty) ? TF_INPUT: TF_INPUTN;
1673 if (new != tp->inattr) {
1675 tty3270_update_prompt(tp, NULL, 0);
1676 tty3270_set_timer(tp, 1);
1679 spin_unlock_bh(&tp->view.lock);
1683 * Disable reading from a 3270 tty
1686 tty3270_throttle(struct tty_struct * tty)
1690 tp = tty->driver_data;
1697 * Enable reading from a 3270 tty
1700 tty3270_unthrottle(struct tty_struct * tty)
1704 tp = tty->driver_data;
1709 tty3270_issue_read(tp, 1);
1713 * Hang up the tty device.
1716 tty3270_hangup(struct tty_struct *tty)
1722 tty3270_wait_until_sent(struct tty_struct *tty, int timeout)
1727 tty3270_ioctl(struct tty_struct *tty, struct file *file,
1728 unsigned int cmd, unsigned long arg)
1732 tp = tty->driver_data;
1735 if (tty->flags & (1 << TTY_IO_ERROR))
1737 return kbd_ioctl(tp->kbd, file, cmd, 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 .set_termios = tty3270_set_termios
1758 tty3270_notifier(int index, int active)
1761 tty_register_device(tty3270_driver, index, NULL);
1763 tty_unregister_device(tty3270_driver, index);
1767 * 3270 tty registration code called from tty_init().
1768 * Most kernel services (incl. kmalloc) are available at this poimt.
1773 struct tty_driver *driver;
1776 driver = alloc_tty_driver(RAW3270_MAXDEVS);
1781 * Initialize the tty_driver structure
1782 * Entries in tty3270_driver that are NOT initialized:
1783 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1785 driver->owner = THIS_MODULE;
1786 driver->driver_name = "ttyTUB";
1787 driver->name = "ttyTUB";
1788 driver->major = IBM_TTY3270_MAJOR;
1789 driver->minor_start = RAW3270_FIRSTMINOR;
1790 driver->type = TTY_DRIVER_TYPE_SYSTEM;
1791 driver->subtype = SYSTEM_TYPE_TTY;
1792 driver->init_termios = tty_std_termios;
1793 driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_DYNAMIC_DEV;
1794 tty_set_operations(driver, &tty3270_ops);
1795 ret = tty_register_driver(driver);
1797 printk(KERN_ERR "tty3270 registration failed with %d\n", ret);
1798 put_tty_driver(driver);
1801 tty3270_driver = driver;
1802 ret = raw3270_register_notifier(tty3270_notifier);
1804 printk(KERN_ERR "tty3270 notifier registration failed "
1806 put_tty_driver(driver);
1816 struct tty_driver *driver;
1818 raw3270_unregister_notifier(tty3270_notifier);
1819 driver = tty3270_driver;
1820 tty3270_driver = NULL;
1821 tty_unregister_driver(driver);
1822 tty3270_del_views();
1825 MODULE_LICENSE("GPL");
1826 MODULE_ALIAS_CHARDEV_MAJOR(IBM_TTY3270_MAJOR);
1828 module_init(tty3270_init);
1829 module_exit(tty3270_exit);