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/config.h>
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/kdev_t.h>
15 #include <linux/tty.h>
16 #include <linux/vt_kern.h>
17 #include <linux/init.h>
18 #include <linux/console.h>
19 #include <linux/interrupt.h>
21 #include <linux/slab.h>
22 #include <linux/bootmem.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 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;
64 struct tty_struct *tty; /* Pointer to tty structure */
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 15
118 static void tty3270_update(struct tty3270 *);
121 * Setup timeout for a device. On timeout trigger an update.
124 tty3270_set_timer(struct tty3270 *tp, int expires)
127 if (timer_pending(&tp->timer)) {
128 raw3270_put_view(&tp->view);
129 del_timer(&tp->timer);
133 if (timer_pending(&tp->timer)) {
134 if (mod_timer(&tp->timer, jiffies + expires))
137 raw3270_get_view(&tp->view);
138 tp->timer.function = (void (*)(unsigned long)) tty3270_update;
139 tp->timer.data = (unsigned long) tp;
140 tp->timer.expires = jiffies + expires;
141 add_timer(&tp->timer);
145 * The input line are the two last lines of the screen.
148 tty3270_update_prompt(struct tty3270 *tp, char *input, int count)
155 line->string[5] = TF_INMDT;
157 line->string[5] = tp->inattr;
158 if (count > tp->view.cols * 2 - 11)
159 count = tp->view.cols * 2 - 11;
160 memcpy(line->string + 6, input, count);
161 line->string[6 + count] = TO_IC;
162 /* Clear to end of input line. */
163 if (count < tp->view.cols * 2 - 11) {
164 line->string[7 + count] = TO_RA;
165 line->string[10 + count] = 0;
166 off = tp->view.cols * tp->view.rows - 9;
167 raw3270_buffer_address(tp->view.dev, line->string+count+8, off);
168 line->len = 11 + count;
170 line->len = 7 + count;
171 tp->update_flags |= TTY_UPDATE_INPUT;
175 tty3270_create_prompt(struct tty3270 *tp)
177 static const unsigned char blueprint[] =
178 { TO_SBA, 0, 0, 0x6e, TO_SF, TF_INPUT,
179 /* empty input string */
180 TO_IC, TO_RA, 0, 0, 0 };
184 line = alloc_string(&tp->freemem,
185 sizeof(blueprint) + tp->view.cols * 2 - 9);
187 tp->inattr = TF_INPUT;
188 /* Copy blueprint to status line */
189 memcpy(line->string, blueprint, sizeof(blueprint));
190 line->len = sizeof(blueprint);
191 /* Set output offsets. */
192 offset = tp->view.cols * (tp->view.rows - 2);
193 raw3270_buffer_address(tp->view.dev, line->string + 1, offset);
194 offset = tp->view.cols * tp->view.rows - 9;
195 raw3270_buffer_address(tp->view.dev, line->string + 8, offset);
197 /* Allocate input string for reading. */
198 tp->input = alloc_string(&tp->freemem, tp->view.cols * 2 - 9 + 6);
202 * The status line is the last line of the screen. It shows the string
203 * "Running"/"Holding" in the lower right corner of the screen.
206 tty3270_update_status(struct tty3270 * tp)
210 str = (tp->nr_up != 0) ? "History" : "Running";
211 memcpy(tp->status->string + 8, str, 7);
212 codepage_convert(tp->view.ascebc, tp->status->string + 8, 7);
213 tp->update_flags |= TTY_UPDATE_STATUS;
217 tty3270_create_status(struct tty3270 * tp)
219 static const unsigned char blueprint[] =
220 { TO_SBA, 0, 0, TO_SF, TF_LOG, TO_SA, TAT_COLOR, TAC_GREEN,
221 0, 0, 0, 0, 0, 0, 0, TO_SF, TF_LOG, TO_SA, TAT_COLOR,
226 line = alloc_string(&tp->freemem,sizeof(blueprint));
228 /* Copy blueprint to status line */
229 memcpy(line->string, blueprint, sizeof(blueprint));
230 /* Set address to start of status string (= last 9 characters). */
231 offset = tp->view.cols * tp->view.rows - 9;
232 raw3270_buffer_address(tp->view.dev, line->string + 1, offset);
236 * Set output offsets to 3270 datastream fragment of a tty string.
237 * (TO_SBA offset at the start and TO_RA offset at the end of the string)
240 tty3270_update_string(struct tty3270 *tp, struct string *line, int nr)
244 raw3270_buffer_address(tp->view.dev, line->string + 1,
246 cp = line->string + line->len - 4;
248 raw3270_buffer_address(tp->view.dev, cp + 1,
249 tp->view.cols * (nr + 1));
253 * Rebuild update list to print all lines.
256 tty3270_rebuild_update(struct tty3270 *tp)
258 struct string *s, *n;
262 * Throw away update list and create a new one,
263 * containing all lines that will fit on the screen.
265 list_for_each_entry_safe(s, n, &tp->update, update)
266 list_del_init(&s->update);
267 line = tp->view.rows - 3;
269 list_for_each_entry_reverse(s, &tp->lines, list) {
274 tty3270_update_string(tp, s, line);
275 list_add(&s->update, &tp->update);
279 tp->update_flags |= TTY_UPDATE_LIST;
283 * Alloc string for size bytes. If there is not enough room in
284 * freemem, free strings until there is room.
286 static struct string *
287 tty3270_alloc_string(struct tty3270 *tp, size_t size)
289 struct string *s, *n;
291 s = alloc_string(&tp->freemem, size);
294 list_for_each_entry_safe(s, n, &tp->lines, list) {
295 BUG_ON(tp->nr_lines <= tp->view.rows - 2);
297 if (!list_empty(&s->update))
298 list_del(&s->update);
300 if (free_string(&tp->freemem, s) >= size)
303 s = alloc_string(&tp->freemem, size);
305 if (tp->nr_up != 0 &&
306 tp->nr_up + tp->view.rows - 2 >= tp->nr_lines) {
307 tp->nr_up = tp->nr_lines - tp->view.rows + 2;
308 tty3270_rebuild_update(tp);
309 tty3270_update_status(tp);
315 * Add an empty line to the list.
318 tty3270_blank_line(struct tty3270 *tp)
320 static const unsigned char blueprint[] =
321 { TO_SBA, 0, 0, TO_SA, TAT_EXTHI, TAX_RESET,
322 TO_SA, TAT_COLOR, TAC_RESET, TO_RA, 0, 0, 0 };
325 s = tty3270_alloc_string(tp, sizeof(blueprint));
326 memcpy(s->string, blueprint, sizeof(blueprint));
327 s->len = sizeof(blueprint);
328 list_add_tail(&s->list, &tp->lines);
335 * Write request completion callback.
338 tty3270_write_callback(struct raw3270_request *rq, void *data)
342 tp = (struct tty3270 *) rq->view;
344 /* Write wasn't successfull. Refresh all. */
345 tty3270_rebuild_update(tp);
346 tp->update_flags = TTY_UPDATE_ALL;
347 tty3270_set_timer(tp, 1);
349 raw3270_request_reset(rq);
350 xchg(&tp->write, rq);
354 * Update 3270 display.
357 tty3270_update(struct tty3270 *tp)
359 static char invalid_sba[2] = { 0xff, 0xff };
360 struct raw3270_request *wrq;
361 unsigned long updated;
362 struct string *s, *n;
366 wrq = xchg(&tp->write, 0);
368 tty3270_set_timer(tp, 1);
372 spin_lock(&tp->view.lock);
374 if (tp->update_flags & TTY_UPDATE_ERASE) {
375 /* Use erase write alternate to erase display. */
376 raw3270_request_set_cmd(wrq, TC_EWRITEA);
377 updated |= TTY_UPDATE_ERASE;
379 raw3270_request_set_cmd(wrq, TC_WRITE);
381 raw3270_request_add_data(wrq, &tp->wcc, 1);
385 * Update status line.
387 if (tp->update_flags & TTY_UPDATE_STATUS)
388 if (raw3270_request_add_data(wrq, tp->status->string,
389 tp->status->len) == 0)
390 updated |= TTY_UPDATE_STATUS;
395 if (tp->update_flags & TTY_UPDATE_INPUT)
396 if (raw3270_request_add_data(wrq, tp->prompt->string,
397 tp->prompt->len) == 0)
398 updated |= TTY_UPDATE_INPUT;
402 if (tp->update_flags & TTY_UPDATE_LIST) {
403 /* Write strings in the update list to the screen. */
404 list_for_each_entry_safe(s, n, &tp->update, update) {
408 * Skip TO_SBA at the start of the string if the
409 * last output position matches the start address
412 if (s->string[1] == sba[0] && s->string[2] == sba[1])
414 if (raw3270_request_add_data(wrq, str, len) != 0)
416 list_del_init(&s->update);
417 sba = s->string + s->len - 3;
419 if (list_empty(&tp->update))
420 updated |= TTY_UPDATE_LIST;
422 wrq->callback = tty3270_write_callback;
423 rc = raw3270_start(&tp->view, wrq);
425 tp->update_flags &= ~updated;
426 if (tp->update_flags)
427 tty3270_set_timer(tp, 1);
429 raw3270_request_reset(wrq);
430 xchg(&tp->write, wrq);
432 spin_unlock(&tp->view.lock);
433 raw3270_put_view(&tp->view);
440 tty3270_rcl_add(struct tty3270 *tp, char *input, int len)
447 if (tp->rcl_nr >= tp->rcl_max) {
448 s = list_entry(tp->rcl_lines.next, struct string, list);
450 free_string(&tp->freemem, s);
453 s = tty3270_alloc_string(tp, len);
454 memcpy(s->string, input, len);
455 list_add_tail(&s->list, &tp->rcl_lines);
460 tty3270_rcl_backward(struct kbd_data *kbd)
465 tp = kbd->tty->driver_data;
466 spin_lock_bh(&tp->view.lock);
467 if (tp->inattr == TF_INPUT) {
468 if (tp->rcl_walk && tp->rcl_walk->prev != &tp->rcl_lines)
469 tp->rcl_walk = tp->rcl_walk->prev;
470 else if (!list_empty(&tp->rcl_lines))
471 tp->rcl_walk = tp->rcl_lines.prev;
473 list_entry(tp->rcl_walk, struct string, list) : 0;
475 s = list_entry(tp->rcl_walk, struct string, list);
476 tty3270_update_prompt(tp, s->string, s->len);
478 tty3270_update_prompt(tp, 0, 0);
479 tty3270_set_timer(tp, 1);
481 spin_unlock_bh(&tp->view.lock);
485 * Deactivate tty view.
488 tty3270_exit_tty(struct kbd_data *kbd)
492 tp = kbd->tty->driver_data;
493 raw3270_deactivate_view(&tp->view);
497 * Scroll forward in history.
500 tty3270_scroll_forward(struct kbd_data *kbd)
505 tp = kbd->tty->driver_data;
506 spin_lock_bh(&tp->view.lock);
507 nr_up = tp->nr_up - tp->view.rows + 2;
510 if (nr_up != tp->nr_up) {
512 tty3270_rebuild_update(tp);
513 tty3270_update_status(tp);
514 tty3270_set_timer(tp, 1);
516 spin_unlock_bh(&tp->view.lock);
520 * Scroll backward in history.
523 tty3270_scroll_backward(struct kbd_data *kbd)
528 tp = kbd->tty->driver_data;
529 spin_lock_bh(&tp->view.lock);
530 nr_up = tp->nr_up + tp->view.rows - 2;
531 if (nr_up + tp->view.rows - 2 > tp->nr_lines)
532 nr_up = tp->nr_lines - tp->view.rows + 2;
533 if (nr_up != tp->nr_up) {
535 tty3270_rebuild_update(tp);
536 tty3270_update_status(tp);
537 tty3270_set_timer(tp, 1);
539 spin_unlock_bh(&tp->view.lock);
543 * Pass input line to tty.
546 tty3270_read_tasklet(struct raw3270_request *rrq)
548 static char kreset_data = TW_KR;
553 tp = (struct tty3270 *) rrq->view;
554 spin_lock_bh(&tp->view.lock);
556 * Two AID keys are special: For 0x7d (enter) the input line
557 * has to be emitted to the tty and for 0x6d the screen
558 * needs to be redrawn.
562 if (tp->input->string[0] == 0x7d) {
563 /* Enter: write input to tty. */
564 input = tp->input->string + 6;
565 len = tp->input->len - 6 - rrq->rescnt;
566 if (tp->inattr != TF_INPUTN)
567 tty3270_rcl_add(tp, input, len);
570 tty3270_rebuild_update(tp);
571 tty3270_update_status(tp);
573 /* Clear input area. */
574 tty3270_update_prompt(tp, 0, 0);
575 tty3270_set_timer(tp, 1);
576 } else if (tp->input->string[0] == 0x6d) {
577 /* Display has been cleared. Redraw. */
578 tty3270_rebuild_update(tp);
579 tp->update_flags = TTY_UPDATE_ALL;
580 tty3270_set_timer(tp, 1);
582 spin_unlock_bh(&tp->view.lock);
584 /* Start keyboard reset command. */
585 raw3270_request_reset(tp->kreset);
586 raw3270_request_set_cmd(tp->kreset, TC_WRITE);
587 raw3270_request_add_data(tp->kreset, &kreset_data, 1);
588 raw3270_start(&tp->view, tp->kreset);
590 /* Emit input string. */
593 kbd_keycode(tp->kbd, *input++);
594 /* Emit keycode for AID byte. */
595 kbd_keycode(tp->kbd, 256 + tp->input->string[0]);
598 raw3270_request_reset(rrq);
599 xchg(&tp->read, rrq);
600 raw3270_put_view(&tp->view);
604 * Read request completion callback.
607 tty3270_read_callback(struct raw3270_request *rq, void *data)
609 raw3270_get_view(rq->view);
610 /* Schedule tasklet to pass input to tty. */
611 tasklet_schedule(&((struct tty3270 *) rq->view)->readlet);
615 * Issue a read request. Call with device lock.
618 tty3270_issue_read(struct tty3270 *tp, int lock)
620 struct raw3270_request *rrq;
623 rrq = xchg(&tp->read, 0);
625 /* Read already scheduled. */
627 rrq->callback = tty3270_read_callback;
628 rrq->callback_data = tp;
629 raw3270_request_set_cmd(rrq, TC_READMOD);
630 raw3270_request_set_data(rrq, tp->input->string, tp->input->len);
631 /* Issue the read modified request. */
633 rc = raw3270_start(&tp->view, rrq);
635 rc = raw3270_start_irq(&tp->view, rrq);
637 raw3270_request_reset(rrq);
638 xchg(&tp->read, rrq);
643 * Switch to the tty view.
646 tty3270_activate(struct raw3270_view *view)
651 tp = (struct tty3270 *) view;
652 spin_lock_irqsave(&tp->view.lock, flags);
654 tty3270_rebuild_update(tp);
655 tty3270_update_status(tp);
656 tp->update_flags = TTY_UPDATE_ALL;
657 tty3270_set_timer(tp, 1);
658 spin_unlock_irqrestore(&tp->view.lock, flags);
664 tty3270_deactivate(struct raw3270_view *view)
668 tp = (struct tty3270 *) view;
674 tty3270_irq(struct tty3270 *tp, struct raw3270_request *rq, struct irb *irb)
676 /* Handle ATTN. Schedule tasklet to read aid. */
677 if (irb->scsw.dstat & DEV_STAT_ATTENTION) {
679 tty3270_issue_read(tp, 0);
685 if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK)
688 /* Normal end. Copy residual count. */
689 rq->rescnt = irb->scsw.count;
691 return RAW3270_IO_DONE;
695 * Allocate tty3270 structure.
697 static struct tty3270 *
698 tty3270_alloc_view(void)
703 tp = kmalloc(sizeof(struct tty3270),GFP_KERNEL);
706 memset(tp, 0, sizeof(struct tty3270));
708 kmalloc(sizeof(void *) * TTY3270_STRING_PAGES, GFP_KERNEL);
709 if (!tp->freemem_pages)
711 INIT_LIST_HEAD(&tp->freemem);
712 for (pages = 0; pages < TTY3270_STRING_PAGES; pages++) {
713 tp->freemem_pages[pages] = (void *)
714 __get_free_pages(GFP_KERNEL|GFP_DMA, 0);
715 if (!tp->freemem_pages[pages])
717 add_string_memory(&tp->freemem,
718 tp->freemem_pages[pages], PAGE_SIZE);
720 tp->write = raw3270_request_alloc(TTY3270_OUTPUT_BUFFER_SIZE);
723 tp->read = raw3270_request_alloc(0);
726 tp->kreset = raw3270_request_alloc(1);
729 tp->kbd = kbd_alloc();
735 raw3270_request_free(tp->kreset);
737 raw3270_request_free(tp->read);
739 raw3270_request_free(tp->write);
742 free_pages((unsigned long) tp->freemem_pages[pages], 0);
743 kfree(tp->freemem_pages);
747 return ERR_PTR(-ENOMEM);
751 * Free tty3270 structure.
754 tty3270_free_view(struct tty3270 *tp)
759 raw3270_request_free(tp->kreset);
760 raw3270_request_free(tp->read);
761 raw3270_request_free(tp->write);
762 for (pages = 0; pages < TTY3270_STRING_PAGES; pages++)
763 free_pages((unsigned long) tp->freemem_pages[pages], 0);
764 kfree(tp->freemem_pages);
769 * Allocate tty3270 screen.
772 tty3270_alloc_screen(struct tty3270 *tp)
777 size = sizeof(struct tty3270_line) * (tp->view.rows - 2);
778 tp->screen = kmalloc(size, GFP_KERNEL);
781 memset(tp->screen, 0, size);
782 for (lines = 0; lines < tp->view.rows - 2; lines++) {
783 size = sizeof(struct tty3270_cell) * tp->view.cols;
784 tp->screen[lines].cells = kmalloc(size, GFP_KERNEL);
785 if (!tp->screen[lines].cells)
787 memset(tp->screen[lines].cells, 0, size);
792 kfree(tp->screen[lines].cells);
799 * Free tty3270 screen.
802 tty3270_free_screen(struct tty3270 *tp)
806 for (lines = 0; lines < tp->view.rows - 2; lines++)
807 kfree(tp->screen[lines].cells);
812 * Unlink tty3270 data structure from tty.
815 tty3270_release(struct raw3270_view *view)
818 struct tty_struct *tty;
820 tp = (struct tty3270 *) view;
823 tty->driver_data = 0;
824 tp->tty = tp->kbd->tty = 0;
826 raw3270_put_view(&tp->view);
831 * Free tty3270 data structure
834 tty3270_free(struct raw3270_view *view)
836 tty3270_free_screen((struct tty3270 *) view);
837 tty3270_free_view((struct tty3270 *) view);
841 * Delayed freeing of tty3270 views.
844 tty3270_del_views(void)
849 for (i = 0; i < tty3270_max_index; i++) {
850 tp = (struct tty3270 *) raw3270_find_view(&tty3270_fn, i);
852 raw3270_del_view(&tp->view);
856 struct raw3270_fn tty3270_fn = {
857 .activate = tty3270_activate,
858 .deactivate = tty3270_deactivate,
859 .intv = (void *) tty3270_irq,
860 .release = tty3270_release,
865 * This routine is called whenever a 3270 tty is opened.
868 tty3270_open(struct tty_struct *tty, struct file * filp)
875 /* Check if the tty3270 is already there. */
876 tp = (struct tty3270 *) raw3270_find_view(&tty3270_fn, tty->index);
878 tty->driver_data = tp;
879 tty->winsize.ws_row = tp->view.rows - 2;
880 tty->winsize.ws_col = tp->view.cols;
881 tty->low_latency = 0;
884 tp->inattr = TF_INPUT;
887 if (tty3270_max_index < tty->index + 1)
888 tty3270_max_index = tty->index + 1;
890 /* Quick exit if there is no device for tty->index. */
891 if (PTR_ERR(tp) == -ENODEV)
894 /* Allocate tty3270 structure on first open. */
895 tp = tty3270_alloc_view();
899 INIT_LIST_HEAD(&tp->lines);
900 INIT_LIST_HEAD(&tp->update);
901 INIT_LIST_HEAD(&tp->rcl_lines);
903 init_timer(&tp->timer);
904 tasklet_init(&tp->readlet,
905 (void (*)(unsigned long)) tty3270_read_tasklet,
906 (unsigned long) tp->read);
908 rc = raw3270_add_view(&tp->view, &tty3270_fn, tty->index);
910 tty3270_free_view(tp);
914 rc = tty3270_alloc_screen(tp);
916 raw3270_del_view(&tp->view);
917 raw3270_put_view(&tp->view);
922 tty->low_latency = 0;
923 tty->driver_data = tp;
924 tty->winsize.ws_row = tp->view.rows - 2;
925 tty->winsize.ws_col = tp->view.cols;
927 tty3270_create_prompt(tp);
928 tty3270_create_status(tp);
929 tty3270_update_status(tp);
931 /* Create blank line for every line in the tty output area. */
932 for (i = 0; i < tp->view.rows - 2; i++)
933 tty3270_blank_line(tp);
936 tp->kbd->fn_handler[KVAL(K_INCRCONSOLE)] = tty3270_exit_tty;
937 tp->kbd->fn_handler[KVAL(K_SCROLLBACK)] = tty3270_scroll_backward;
938 tp->kbd->fn_handler[KVAL(K_SCROLLFORW)] = tty3270_scroll_forward;
939 tp->kbd->fn_handler[KVAL(K_CONS)] = tty3270_rcl_backward;
940 kbd_ascebc(tp->kbd, tp->view.ascebc);
942 raw3270_activate_view(&tp->view);
947 * This routine is called when the 3270 tty is closed. We wait
948 * for the remaining request to be completed. Then we clean up.
951 tty3270_close(struct tty_struct *tty, struct file * filp)
957 tp = (struct tty3270 *) tty->driver_data;
959 tty->driver_data = 0;
960 tp->tty = tp->kbd->tty = 0;
961 raw3270_put_view(&tp->view);
966 * We always have room.
969 tty3270_write_room(struct tty_struct *tty)
975 * Insert character into the screen at the current position with the
976 * current color and highlight. This function does NOT do cursor movement.
979 tty3270_put_character(struct tty3270 *tp, char ch)
981 struct tty3270_line *line;
982 struct tty3270_cell *cell;
984 line = tp->screen + tp->cy;
985 if (line->len <= tp->cx) {
986 while (line->len < tp->cx) {
987 cell = line->cells + line->len;
988 cell->character = tp->view.ascebc[' '];
989 cell->highlight = tp->highlight;
990 cell->f_color = tp->f_color;
995 cell = line->cells + tp->cx;
996 cell->character = tp->view.ascebc[(unsigned int) ch];
997 cell->highlight = tp->highlight;
998 cell->f_color = tp->f_color;
1002 * Convert a tty3270_line to a 3270 data fragment usable for output.
1005 tty3270_convert_line(struct tty3270 *tp, int line_nr)
1007 struct tty3270_line *line;
1008 struct tty3270_cell *cell;
1009 struct string *s, *n;
1010 unsigned char highlight;
1011 unsigned char f_color;
1015 /* Determine how long the fragment will be. */
1016 flen = 3; /* Prefix (TO_SBA). */
1017 line = tp->screen + line_nr;
1019 highlight = TAX_RESET;
1020 f_color = TAC_RESET;
1021 for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
1022 if (cell->highlight != highlight) {
1023 flen += 3; /* TO_SA to switch highlight. */
1024 highlight = cell->highlight;
1026 if (cell->f_color != f_color) {
1027 flen += 3; /* TO_SA to switch color. */
1028 f_color = cell->f_color;
1031 if (highlight != TAX_RESET)
1032 flen += 3; /* TO_SA to reset hightlight. */
1033 if (f_color != TAC_RESET)
1034 flen += 3; /* TO_SA to reset color. */
1035 if (line->len < tp->view.cols)
1036 flen += 4; /* Postfix (TO_RA). */
1038 /* Find the line in the list. */
1039 i = tp->view.rows - 2 - line_nr;
1040 list_for_each_entry_reverse(s, &tp->lines, list)
1044 * Check if the line needs to get reallocated.
1046 if (s->len != flen) {
1047 /* Reallocate string. */
1048 n = tty3270_alloc_string(tp, flen);
1049 list_add(&n->list, &s->list);
1050 list_del_init(&s->list);
1051 if (!list_empty(&s->update))
1052 list_del_init(&s->update);
1053 free_string(&tp->freemem, s);
1057 /* Write 3270 data fragment. */
1063 highlight = TAX_RESET;
1064 f_color = TAC_RESET;
1065 for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
1066 if (cell->highlight != highlight) {
1069 *cp++ = cell->highlight;
1070 highlight = cell->highlight;
1072 if (cell->f_color != f_color) {
1075 *cp++ = cell->f_color;
1076 f_color = cell->f_color;
1078 *cp++ = cell->character;
1080 if (highlight != TAX_RESET) {
1085 if (f_color != TAC_RESET) {
1090 if (line->len < tp->view.cols) {
1097 if (tp->nr_up + line_nr < tp->view.rows - 2) {
1098 /* Line is currently visible on screen. */
1099 tty3270_update_string(tp, s, line_nr);
1100 /* Add line to update list. */
1101 if (list_empty(&s->update)) {
1102 list_add_tail(&s->update, &tp->update);
1103 tp->update_flags |= TTY_UPDATE_LIST;
1109 * Do carriage return.
1112 tty3270_cr(struct tty3270 *tp)
1121 tty3270_lf(struct tty3270 *tp)
1123 struct tty3270_line temp;
1126 tty3270_convert_line(tp, tp->cy);
1127 if (tp->cy < tp->view.rows - 3) {
1131 /* Last line just filled up. Add new, blank line. */
1132 tty3270_blank_line(tp);
1133 temp = tp->screen[0];
1135 for (i = 0; i < tp->view.rows - 3; i++)
1136 tp->screen[i] = tp->screen[i+1];
1137 tp->screen[tp->view.rows - 3] = temp;
1138 tty3270_rebuild_update(tp);
1142 tty3270_ri(struct tty3270 *tp)
1145 tty3270_convert_line(tp, tp->cy);
1151 * Insert characters at current position.
1154 tty3270_insert_characters(struct tty3270 *tp, int n)
1156 struct tty3270_line *line;
1159 line = tp->screen + tp->cy;
1160 while (line->len < tp->cx) {
1161 line->cells[line->len].character = tp->view.ascebc[' '];
1162 line->cells[line->len].highlight = TAX_RESET;
1163 line->cells[line->len].f_color = TAC_RESET;
1166 if (n > tp->view.cols - tp->cx)
1167 n = tp->view.cols - tp->cx;
1168 k = min_t(int, line->len - tp->cx, tp->view.cols - tp->cx - n);
1170 line->cells[tp->cx + n + k] = line->cells[tp->cx + k];
1172 if (line->len > tp->view.cols)
1173 line->len = tp->view.cols;
1175 line->cells[tp->cx + n].character = tp->view.ascebc[' '];
1176 line->cells[tp->cx + n].highlight = tp->highlight;
1177 line->cells[tp->cx + n].f_color = tp->f_color;
1182 * Delete characters at current position.
1185 tty3270_delete_characters(struct tty3270 *tp, int n)
1187 struct tty3270_line *line;
1190 line = tp->screen + tp->cy;
1191 if (line->len <= tp->cx)
1193 if (line->len - tp->cx <= n) {
1197 for (i = tp->cx; i + n < line->len; i++)
1198 line->cells[i] = line->cells[i + n];
1203 * Erase characters at current position.
1206 tty3270_erase_characters(struct tty3270 *tp, int n)
1208 struct tty3270_line *line;
1209 struct tty3270_cell *cell;
1211 line = tp->screen + tp->cy;
1212 while (line->len > tp->cx && n-- > 0) {
1213 cell = line->cells + tp->cx++;
1214 cell->character = ' ';
1215 cell->highlight = TAX_RESET;
1216 cell->f_color = TAC_RESET;
1219 tp->cx = min_t(int, tp->cx, tp->view.cols - 1);
1223 * Erase line, 3 different cases:
1224 * Esc [ 0 K Erase from current position to end of line inclusive
1225 * Esc [ 1 K Erase from beginning of line to current position inclusive
1226 * Esc [ 2 K Erase entire line (without moving cursor)
1229 tty3270_erase_line(struct tty3270 *tp, int mode)
1231 struct tty3270_line *line;
1232 struct tty3270_cell *cell;
1235 line = tp->screen + tp->cy;
1238 else if (mode == 1) {
1239 for (i = 0; i < tp->cx; i++) {
1240 cell = line->cells + i;
1241 cell->character = ' ';
1242 cell->highlight = TAX_RESET;
1243 cell->f_color = TAC_RESET;
1245 if (line->len <= tp->cx)
1246 line->len = tp->cx + 1;
1247 } else if (mode == 2)
1249 tty3270_convert_line(tp, tp->cy);
1253 * Erase display, 3 different cases:
1254 * Esc [ 0 J Erase from current position to bottom of screen inclusive
1255 * Esc [ 1 J Erase from top of screen to current position inclusive
1256 * Esc [ 2 J Erase entire screen (without moving the cursor)
1259 tty3270_erase_display(struct tty3270 *tp, int mode)
1264 tty3270_erase_line(tp, 0);
1265 for (i = tp->cy + 1; i < tp->view.rows - 2; i++) {
1266 tp->screen[i].len = 0;
1267 tty3270_convert_line(tp, i);
1269 } else if (mode == 1) {
1270 for (i = 0; i < tp->cy; i++) {
1271 tp->screen[i].len = 0;
1272 tty3270_convert_line(tp, i);
1274 tty3270_erase_line(tp, 1);
1275 } else if (mode == 2) {
1276 for (i = 0; i < tp->view.rows - 2; i++) {
1277 tp->screen[i].len = 0;
1278 tty3270_convert_line(tp, i);
1281 tty3270_rebuild_update(tp);
1285 * Set attributes found in an escape sequence.
1286 * Esc [ <attr> ; <attr> ; ... m
1289 tty3270_set_attributes(struct tty3270 *tp)
1291 static unsigned char f_colors[] = {
1292 TAC_DEFAULT, TAC_RED, TAC_GREEN, TAC_YELLOW, TAC_BLUE,
1293 TAC_PINK, TAC_TURQ, TAC_WHITE, 0, TAC_DEFAULT
1297 for (i = 0; i <= tp->esc_npar; i++) {
1298 attr = tp->esc_par[i];
1301 tp->highlight = TAX_RESET;
1302 tp->f_color = TAC_RESET;
1305 case 4: /* Start underlining. */
1306 tp->highlight = TAX_UNDER;
1308 case 5: /* Start blink. */
1309 tp->highlight = TAX_BLINK;
1311 case 7: /* Start reverse. */
1312 tp->highlight = TAX_REVER;
1314 case 24: /* End underlining */
1315 if (tp->highlight == TAX_UNDER)
1316 tp->highlight = TAX_RESET;
1318 case 25: /* End blink. */
1319 if (tp->highlight == TAX_BLINK)
1320 tp->highlight = TAX_RESET;
1322 case 27: /* End reverse. */
1323 if (tp->highlight == TAX_REVER)
1324 tp->highlight = TAX_RESET;
1326 /* Foreground color. */
1327 case 30: /* Black */
1329 case 32: /* Green */
1330 case 33: /* Yellow */
1332 case 35: /* Magenta */
1334 case 37: /* White */
1335 case 39: /* Black */
1336 tp->f_color = f_colors[attr - 30];
1343 tty3270_getpar(struct tty3270 *tp, int ix)
1345 return (tp->esc_par[ix] > 0) ? tp->esc_par[ix] : 1;
1349 tty3270_goto_xy(struct tty3270 *tp, int cx, int cy)
1351 tp->cx = min_t(int, tp->view.cols - 1, max_t(int, 0, cx));
1352 cy = min_t(int, tp->view.rows - 3, max_t(int, 0, cy));
1354 tty3270_convert_line(tp, tp->cy);
1360 * Process escape sequences. Known sequences:
1361 * Esc 7 Save Cursor Position
1362 * Esc 8 Restore Cursor Position
1363 * Esc [ Pn ; Pn ; .. m Set attributes
1364 * Esc [ Pn ; Pn H Cursor Position
1365 * Esc [ Pn ; Pn f Cursor Position
1366 * Esc [ Pn A Cursor Up
1367 * Esc [ Pn B Cursor Down
1368 * Esc [ Pn C Cursor Forward
1369 * Esc [ Pn D Cursor Backward
1370 * Esc [ Pn G Cursor Horizontal Absolute
1371 * Esc [ Pn X Erase Characters
1372 * Esc [ Ps J Erase in Display
1373 * Esc [ Ps K Erase in Line
1374 * // FIXME: add all the new ones.
1376 * Pn is a numeric parameter, a string of zero or more decimal digits.
1377 * Ps is a selective parameter.
1380 tty3270_escape_sequence(struct tty3270 *tp, char ch)
1382 enum { ESnormal, ESesc, ESsquare, ESgetpars };
1384 if (tp->esc_state == ESnormal) {
1386 /* Starting new escape sequence. */
1387 tp->esc_state = ESesc;
1390 if (tp->esc_state == ESesc) {
1391 tp->esc_state = ESnormal;
1394 tp->esc_state = ESsquare;
1406 case 'Z': /* Respond ID. */
1407 kbd_puts_queue(tp->tty, "\033[?6c");
1409 case '7': /* Save cursor position. */
1410 tp->saved_cx = tp->cx;
1411 tp->saved_cy = tp->cy;
1412 tp->saved_highlight = tp->highlight;
1413 tp->saved_f_color = tp->f_color;
1415 case '8': /* Restore cursor position. */
1416 tty3270_convert_line(tp, tp->cy);
1417 tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
1418 tp->highlight = tp->saved_highlight;
1419 tp->f_color = tp->saved_f_color;
1421 case 'c': /* Reset terminal. */
1422 tp->cx = tp->saved_cx = 0;
1423 tp->cy = tp->saved_cy = 0;
1424 tp->highlight = tp->saved_highlight = TAX_RESET;
1425 tp->f_color = tp->saved_f_color = TAC_RESET;
1426 tty3270_erase_display(tp, 2);
1431 if (tp->esc_state == ESsquare) {
1432 tp->esc_state = ESgetpars;
1433 memset(tp->esc_par, 0, sizeof(tp->esc_par));
1435 tp->esc_ques = (ch == '?');
1439 if (tp->esc_state == ESgetpars) {
1440 if (ch == ';' && tp->esc_npar < ESCAPE_NPAR - 1) {
1444 if (ch >= '0' && ch <= '9') {
1445 tp->esc_par[tp->esc_npar] *= 10;
1446 tp->esc_par[tp->esc_npar] += ch - '0';
1450 tp->esc_state = ESnormal;
1451 if (ch == 'n' && !tp->esc_ques) {
1452 if (tp->esc_par[0] == 5) /* Status report. */
1453 kbd_puts_queue(tp->tty, "\033[0n");
1454 else if (tp->esc_par[0] == 6) { /* Cursor report. */
1456 sprintf(buf, "\033[%d;%dR", tp->cy + 1, tp->cx + 1);
1457 kbd_puts_queue(tp->tty, buf);
1465 tty3270_set_attributes(tp);
1467 case 'H': /* Set cursor position. */
1469 tty3270_goto_xy(tp, tty3270_getpar(tp, 1) - 1,
1470 tty3270_getpar(tp, 0) - 1);
1472 case 'd': /* Set y position. */
1473 tty3270_goto_xy(tp, tp->cx, tty3270_getpar(tp, 0) - 1);
1475 case 'A': /* Cursor up. */
1477 tty3270_goto_xy(tp, tp->cx, tp->cy - tty3270_getpar(tp, 0));
1479 case 'B': /* Cursor down. */
1482 tty3270_goto_xy(tp, tp->cx, tp->cy + tty3270_getpar(tp, 0));
1484 case 'C': /* Cursor forward. */
1486 tty3270_goto_xy(tp, tp->cx + tty3270_getpar(tp, 0), tp->cy);
1488 case 'D': /* Cursor backward. */
1489 tty3270_goto_xy(tp, tp->cx - tty3270_getpar(tp, 0), tp->cy);
1491 case 'G': /* Set x position. */
1493 tty3270_goto_xy(tp, tty3270_getpar(tp, 0), tp->cy);
1495 case 'X': /* Erase Characters. */
1496 tty3270_erase_characters(tp, tty3270_getpar(tp, 0));
1498 case 'J': /* Erase display. */
1499 tty3270_erase_display(tp, tp->esc_par[0]);
1501 case 'K': /* Erase line. */
1502 tty3270_erase_line(tp, tp->esc_par[0]);
1504 case 'P': /* Delete characters. */
1505 tty3270_delete_characters(tp, tty3270_getpar(tp, 0));
1507 case '@': /* Insert characters. */
1508 tty3270_insert_characters(tp, tty3270_getpar(tp, 0));
1510 case 's': /* Save cursor position. */
1511 tp->saved_cx = tp->cx;
1512 tp->saved_cy = tp->cy;
1513 tp->saved_highlight = tp->highlight;
1514 tp->saved_f_color = tp->f_color;
1516 case 'u': /* Restore cursor position. */
1517 tty3270_convert_line(tp, tp->cy);
1518 tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
1519 tp->highlight = tp->saved_highlight;
1520 tp->f_color = tp->saved_f_color;
1526 * String write routine for 3270 ttys
1529 tty3270_do_write(struct tty3270 *tp, const unsigned char *buf, int count)
1533 spin_lock_bh(&tp->view.lock);
1534 for (i_msg = 0; !tp->tty->stopped && i_msg < count; i_msg++) {
1535 if (tp->esc_state != 0) {
1536 /* Continue escape sequence. */
1537 tty3270_escape_sequence(tp, buf[i_msg]);
1541 switch (buf[i_msg]) {
1542 case 0x07: /* '\a' -- Alarm */
1543 tp->wcc |= TW_PLUSALARM;
1545 case 0x08: /* Backspace. */
1548 tty3270_put_character(tp, ' ');
1551 case 0x09: /* '\t' -- Tabulate */
1552 for (i = tp->cx % 8; i < 8; i++) {
1553 if (tp->cx >= tp->view.cols) {
1558 tty3270_put_character(tp, ' ');
1562 case 0x0a: /* '\n' -- New Line */
1566 case 0x0c: /* '\f' -- Form Feed */
1567 tty3270_erase_display(tp, 2);
1568 tp->cx = tp->cy = 0;
1570 case 0x0d: /* '\r' -- Carriage Return */
1573 case 0x0f: /* SuSE "exit alternate mode" */
1575 case 0x1b: /* Start escape sequence. */
1576 tty3270_escape_sequence(tp, buf[i_msg]);
1578 default: /* Insert normal character. */
1579 if (tp->cx >= tp->view.cols) {
1583 tty3270_put_character(tp, buf[i_msg]);
1588 /* Convert current line to 3270 data fragment. */
1589 tty3270_convert_line(tp, tp->cy);
1591 /* Setup timer to update display after 1/10 second */
1592 if (!timer_pending(&tp->timer))
1593 tty3270_set_timer(tp, HZ/10);
1595 spin_unlock_bh(&tp->view.lock);
1599 * String write routine for 3270 ttys
1602 tty3270_write(struct tty_struct * tty, int from_user,
1603 const unsigned char *buf, int count)
1608 tp = tty->driver_data;
1611 if (tp->char_count > 0) {
1612 tty3270_do_write(tp, tp->char_buf, tp->char_count);
1616 tty3270_do_write(tp, buf, count);
1621 length = count < TTY3270_CHAR_BUF_SIZE ?
1622 count : TTY3270_CHAR_BUF_SIZE;
1623 length -= copy_from_user(tp->char_buf, buf, length);
1629 tty3270_do_write(tp, tp->char_buf, count);
1638 * Put single characters to the ttys character buffer
1641 tty3270_put_char(struct tty_struct *tty, unsigned char ch)
1645 tp = tty->driver_data;
1648 if (tp->char_count < TTY3270_CHAR_BUF_SIZE)
1649 tp->char_buf[tp->char_count++] = ch;
1653 * Flush all characters from the ttys characeter buffer put there
1654 * by tty3270_put_char.
1657 tty3270_flush_chars(struct tty_struct *tty)
1661 tp = tty->driver_data;
1664 if (tp->char_count > 0) {
1665 tty3270_do_write(tp, tp->char_buf, tp->char_count);
1671 * Returns the number of characters in the output buffer. This is
1672 * used in tty_wait_until_sent to wait until all characters have
1673 * appeared on the screen.
1676 tty3270_chars_in_buffer(struct tty_struct *tty)
1682 tty3270_flush_buffer(struct tty_struct *tty)
1687 * Check for visible/invisible input switches
1690 tty3270_set_termios(struct tty_struct *tty, struct termios *old)
1695 tp = tty->driver_data;
1698 spin_lock_bh(&tp->view.lock);
1699 if (L_ICANON(tty)) {
1700 new = L_ECHO(tty) ? TF_INPUT: TF_INPUTN;
1701 if (new != tp->inattr) {
1703 tty3270_update_prompt(tp, 0, 0);
1704 tty3270_set_timer(tp, 1);
1707 spin_unlock_bh(&tp->view.lock);
1711 * Disable reading from a 3270 tty
1714 tty3270_throttle(struct tty_struct * tty)
1718 tp = tty->driver_data;
1725 * Enable reading from a 3270 tty
1728 tty3270_unthrottle(struct tty_struct * tty)
1732 tp = tty->driver_data;
1737 tty3270_issue_read(tp, 1);
1741 * Hang up the tty device.
1744 tty3270_hangup(struct tty_struct *tty)
1750 tty3270_wait_until_sent(struct tty_struct *tty, int timeout)
1755 tty3270_ioctl(struct tty_struct *tty, struct file *file,
1756 unsigned int cmd, unsigned long arg)
1760 tp = tty->driver_data;
1763 if (tty->flags & (1 << TTY_IO_ERROR))
1765 return kbd_ioctl(tp->kbd, file, cmd, arg);
1768 static struct tty_operations tty3270_ops = {
1769 .open = tty3270_open,
1770 .close = tty3270_close,
1771 .write = tty3270_write,
1772 .put_char = tty3270_put_char,
1773 .flush_chars = tty3270_flush_chars,
1774 .write_room = tty3270_write_room,
1775 .chars_in_buffer = tty3270_chars_in_buffer,
1776 .flush_buffer = tty3270_flush_buffer,
1777 .throttle = tty3270_throttle,
1778 .unthrottle = tty3270_unthrottle,
1779 .hangup = tty3270_hangup,
1780 .wait_until_sent = tty3270_wait_until_sent,
1781 .ioctl = tty3270_ioctl,
1782 .set_termios = tty3270_set_termios
1786 tty3270_notifier(int index, int active)
1789 tty_register_device(tty3270_driver, index, 0);
1791 tty_unregister_device(tty3270_driver, index);
1795 * 3270 tty registration code called from tty_init().
1796 * Most kernel services (incl. kmalloc) are available at this poimt.
1801 struct tty_driver *driver;
1804 driver = alloc_tty_driver(256);
1809 * Initialize the tty_driver structure
1810 * Entries in tty3270_driver that are NOT initialized:
1811 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1813 driver->owner = THIS_MODULE;
1814 driver->devfs_name = "ttyTUB/";
1815 driver->driver_name = "ttyTUB";
1816 driver->name = "ttyTUB";
1817 driver->major = IBM_TTY3270_MAJOR;
1818 driver->type = TTY_DRIVER_TYPE_SYSTEM;
1819 driver->subtype = SYSTEM_TYPE_TTY;
1820 driver->init_termios = tty_std_termios;
1821 driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_NO_DEVFS;
1822 tty_set_operations(driver, &tty3270_ops);
1823 ret = tty_register_driver(driver);
1825 printk(KERN_ERR "tty3270 registration failed with %d\n", ret);
1826 put_tty_driver(driver);
1829 tty3270_driver = driver;
1830 ret = raw3270_register_notifier(tty3270_notifier);
1832 printk(KERN_ERR "tty3270 notifier registration failed "
1834 put_tty_driver(driver);
1844 struct tty_driver *driver;
1846 raw3270_unregister_notifier(tty3270_notifier);
1847 driver = tty3270_driver;
1849 tty_unregister_driver(driver);
1850 tty3270_del_views();
1853 MODULE_LICENSE("GPL");
1854 MODULE_ALIAS_CHARDEV_MAJOR(IBM_TTY3270_MAJOR);
1856 module_init(tty3270_init);
1857 module_exit(tty3270_exit);