2 * IBM/3270 Driver - tty functions.
5 * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
6 * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * -- Copyright IBM Corp. 2003
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/kdev_t.h>
13 #include <linux/tty.h>
14 #include <linux/vt_kern.h>
15 #include <linux/init.h>
16 #include <linux/console.h>
17 #include <linux/interrupt.h>
19 #include <linux/slab.h>
20 #include <linux/bootmem.h>
21 #include <linux/compat.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 static 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;
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 16 /* Recreate screen. */
117 static void tty3270_update(struct tty3270 *);
120 * Setup timeout for a device. On timeout trigger an update.
122 static void tty3270_set_timer(struct tty3270 *tp, int expires)
125 del_timer(&tp->timer);
127 mod_timer(&tp->timer, jiffies + expires);
131 * The input line are the two last lines of the screen.
134 tty3270_update_prompt(struct tty3270 *tp, char *input, int count)
141 line->string[5] = TF_INMDT;
143 line->string[5] = tp->inattr;
144 if (count > tp->view.cols * 2 - 11)
145 count = tp->view.cols * 2 - 11;
146 memcpy(line->string + 6, input, count);
147 line->string[6 + count] = TO_IC;
148 /* Clear to end of input line. */
149 if (count < tp->view.cols * 2 - 11) {
150 line->string[7 + count] = TO_RA;
151 line->string[10 + count] = 0;
152 off = tp->view.cols * tp->view.rows - 9;
153 raw3270_buffer_address(tp->view.dev, line->string+count+8, off);
154 line->len = 11 + count;
156 line->len = 7 + count;
157 tp->update_flags |= TTY_UPDATE_INPUT;
161 tty3270_create_prompt(struct tty3270 *tp)
163 static const unsigned char blueprint[] =
164 { TO_SBA, 0, 0, 0x6e, TO_SF, TF_INPUT,
165 /* empty input string */
166 TO_IC, TO_RA, 0, 0, 0 };
170 line = alloc_string(&tp->freemem,
171 sizeof(blueprint) + tp->view.cols * 2 - 9);
173 tp->inattr = TF_INPUT;
174 /* Copy blueprint to status line */
175 memcpy(line->string, blueprint, sizeof(blueprint));
176 line->len = sizeof(blueprint);
177 /* Set output offsets. */
178 offset = tp->view.cols * (tp->view.rows - 2);
179 raw3270_buffer_address(tp->view.dev, line->string + 1, offset);
180 offset = tp->view.cols * tp->view.rows - 9;
181 raw3270_buffer_address(tp->view.dev, line->string + 8, offset);
183 /* Allocate input string for reading. */
184 tp->input = alloc_string(&tp->freemem, tp->view.cols * 2 - 9 + 6);
188 * The status line is the last line of the screen. It shows the string
189 * "Running"/"Holding" in the lower right corner of the screen.
192 tty3270_update_status(struct tty3270 * tp)
196 str = (tp->nr_up != 0) ? "History" : "Running";
197 memcpy(tp->status->string + 8, str, 7);
198 codepage_convert(tp->view.ascebc, tp->status->string + 8, 7);
199 tp->update_flags |= TTY_UPDATE_STATUS;
203 tty3270_create_status(struct tty3270 * tp)
205 static const unsigned char blueprint[] =
206 { TO_SBA, 0, 0, TO_SF, TF_LOG, TO_SA, TAT_COLOR, TAC_GREEN,
207 0, 0, 0, 0, 0, 0, 0, TO_SF, TF_LOG, TO_SA, TAT_COLOR,
212 line = alloc_string(&tp->freemem,sizeof(blueprint));
214 /* Copy blueprint to status line */
215 memcpy(line->string, blueprint, sizeof(blueprint));
216 /* Set address to start of status string (= last 9 characters). */
217 offset = tp->view.cols * tp->view.rows - 9;
218 raw3270_buffer_address(tp->view.dev, line->string + 1, offset);
222 * Set output offsets to 3270 datastream fragment of a tty string.
223 * (TO_SBA offset at the start and TO_RA offset at the end of the string)
226 tty3270_update_string(struct tty3270 *tp, struct string *line, int nr)
230 raw3270_buffer_address(tp->view.dev, line->string + 1,
232 cp = line->string + line->len - 4;
234 raw3270_buffer_address(tp->view.dev, cp + 1,
235 tp->view.cols * (nr + 1));
239 * Rebuild update list to print all lines.
242 tty3270_rebuild_update(struct tty3270 *tp)
244 struct string *s, *n;
248 * Throw away update list and create a new one,
249 * containing all lines that will fit on the screen.
251 list_for_each_entry_safe(s, n, &tp->update, update)
252 list_del_init(&s->update);
253 line = tp->view.rows - 3;
255 list_for_each_entry_reverse(s, &tp->lines, list) {
260 tty3270_update_string(tp, s, line);
261 list_add(&s->update, &tp->update);
265 tp->update_flags |= TTY_UPDATE_LIST;
269 * Alloc string for size bytes. If there is not enough room in
270 * freemem, free strings until there is room.
272 static struct string *
273 tty3270_alloc_string(struct tty3270 *tp, size_t size)
275 struct string *s, *n;
277 s = alloc_string(&tp->freemem, size);
280 list_for_each_entry_safe(s, n, &tp->lines, list) {
281 BUG_ON(tp->nr_lines <= tp->view.rows - 2);
283 if (!list_empty(&s->update))
284 list_del(&s->update);
286 if (free_string(&tp->freemem, s) >= size)
289 s = alloc_string(&tp->freemem, size);
291 if (tp->nr_up != 0 &&
292 tp->nr_up + tp->view.rows - 2 >= tp->nr_lines) {
293 tp->nr_up = tp->nr_lines - tp->view.rows + 2;
294 tty3270_rebuild_update(tp);
295 tty3270_update_status(tp);
301 * Add an empty line to the list.
304 tty3270_blank_line(struct tty3270 *tp)
306 static const unsigned char blueprint[] =
307 { TO_SBA, 0, 0, TO_SA, TAT_EXTHI, TAX_RESET,
308 TO_SA, TAT_COLOR, TAC_RESET, TO_RA, 0, 0, 0 };
311 s = tty3270_alloc_string(tp, sizeof(blueprint));
312 memcpy(s->string, blueprint, sizeof(blueprint));
313 s->len = sizeof(blueprint);
314 list_add_tail(&s->list, &tp->lines);
321 * Write request completion callback.
324 tty3270_write_callback(struct raw3270_request *rq, void *data)
326 struct tty3270 *tp = container_of(rq->view, struct tty3270, view);
329 /* Write wasn't successful. Refresh all. */
330 tp->update_flags = TTY_UPDATE_ALL;
331 tty3270_set_timer(tp, 1);
333 raw3270_request_reset(rq);
334 xchg(&tp->write, rq);
338 * Update 3270 display.
341 tty3270_update(struct tty3270 *tp)
343 static char invalid_sba[2] = { 0xff, 0xff };
344 struct raw3270_request *wrq;
345 unsigned long updated;
346 struct string *s, *n;
350 wrq = xchg(&tp->write, 0);
352 tty3270_set_timer(tp, 1);
356 spin_lock(&tp->view.lock);
358 if (tp->update_flags & TTY_UPDATE_ALL) {
359 tty3270_rebuild_update(tp);
360 tty3270_update_status(tp);
361 tp->update_flags = TTY_UPDATE_ERASE | TTY_UPDATE_LIST |
362 TTY_UPDATE_INPUT | TTY_UPDATE_STATUS;
364 if (tp->update_flags & TTY_UPDATE_ERASE) {
365 /* Use erase write alternate to erase display. */
366 raw3270_request_set_cmd(wrq, TC_EWRITEA);
367 updated |= TTY_UPDATE_ERASE;
369 raw3270_request_set_cmd(wrq, TC_WRITE);
371 raw3270_request_add_data(wrq, &tp->wcc, 1);
375 * Update status line.
377 if (tp->update_flags & TTY_UPDATE_STATUS)
378 if (raw3270_request_add_data(wrq, tp->status->string,
379 tp->status->len) == 0)
380 updated |= TTY_UPDATE_STATUS;
385 if (tp->update_flags & TTY_UPDATE_INPUT)
386 if (raw3270_request_add_data(wrq, tp->prompt->string,
387 tp->prompt->len) == 0)
388 updated |= TTY_UPDATE_INPUT;
392 if (tp->update_flags & TTY_UPDATE_LIST) {
393 /* Write strings in the update list to the screen. */
394 list_for_each_entry_safe(s, n, &tp->update, update) {
398 * Skip TO_SBA at the start of the string if the
399 * last output position matches the start address
402 if (s->string[1] == sba[0] && s->string[2] == sba[1])
404 if (raw3270_request_add_data(wrq, str, len) != 0)
406 list_del_init(&s->update);
407 sba = s->string + s->len - 3;
409 if (list_empty(&tp->update))
410 updated |= TTY_UPDATE_LIST;
412 wrq->callback = tty3270_write_callback;
413 rc = raw3270_start(&tp->view, wrq);
415 tp->update_flags &= ~updated;
416 if (tp->update_flags)
417 tty3270_set_timer(tp, 1);
419 raw3270_request_reset(wrq);
420 xchg(&tp->write, wrq);
422 spin_unlock(&tp->view.lock);
429 tty3270_rcl_add(struct tty3270 *tp, char *input, int len)
436 if (tp->rcl_nr >= tp->rcl_max) {
437 s = list_entry(tp->rcl_lines.next, struct string, list);
439 free_string(&tp->freemem, s);
442 s = tty3270_alloc_string(tp, len);
443 memcpy(s->string, input, len);
444 list_add_tail(&s->list, &tp->rcl_lines);
449 tty3270_rcl_backward(struct kbd_data *kbd)
451 struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
454 spin_lock_bh(&tp->view.lock);
455 if (tp->inattr == TF_INPUT) {
456 if (tp->rcl_walk && tp->rcl_walk->prev != &tp->rcl_lines)
457 tp->rcl_walk = tp->rcl_walk->prev;
458 else if (!list_empty(&tp->rcl_lines))
459 tp->rcl_walk = tp->rcl_lines.prev;
461 list_entry(tp->rcl_walk, struct string, list) : NULL;
463 s = list_entry(tp->rcl_walk, struct string, list);
464 tty3270_update_prompt(tp, s->string, s->len);
466 tty3270_update_prompt(tp, NULL, 0);
467 tty3270_set_timer(tp, 1);
469 spin_unlock_bh(&tp->view.lock);
473 * Deactivate tty view.
476 tty3270_exit_tty(struct kbd_data *kbd)
478 struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
480 raw3270_deactivate_view(&tp->view);
484 * Scroll forward in history.
487 tty3270_scroll_forward(struct kbd_data *kbd)
489 struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
492 spin_lock_bh(&tp->view.lock);
493 nr_up = tp->nr_up - tp->view.rows + 2;
496 if (nr_up != tp->nr_up) {
498 tty3270_rebuild_update(tp);
499 tty3270_update_status(tp);
500 tty3270_set_timer(tp, 1);
502 spin_unlock_bh(&tp->view.lock);
506 * Scroll backward in history.
509 tty3270_scroll_backward(struct kbd_data *kbd)
511 struct tty3270 *tp = container_of(kbd->port, struct tty3270, port);
514 spin_lock_bh(&tp->view.lock);
515 nr_up = tp->nr_up + tp->view.rows - 2;
516 if (nr_up + tp->view.rows - 2 > tp->nr_lines)
517 nr_up = tp->nr_lines - tp->view.rows + 2;
518 if (nr_up != tp->nr_up) {
520 tty3270_rebuild_update(tp);
521 tty3270_update_status(tp);
522 tty3270_set_timer(tp, 1);
524 spin_unlock_bh(&tp->view.lock);
528 * Pass input line to tty.
531 tty3270_read_tasklet(struct raw3270_request *rrq)
533 static char kreset_data = TW_KR;
534 struct tty3270 *tp = container_of(rrq->view, struct tty3270, view);
538 spin_lock_bh(&tp->view.lock);
540 * Two AID keys are special: For 0x7d (enter) the input line
541 * has to be emitted to the tty and for 0x6d the screen
542 * needs to be redrawn.
546 if (tp->input->string[0] == 0x7d) {
547 /* Enter: write input to tty. */
548 input = tp->input->string + 6;
549 len = tp->input->len - 6 - rrq->rescnt;
550 if (tp->inattr != TF_INPUTN)
551 tty3270_rcl_add(tp, input, len);
554 tty3270_rebuild_update(tp);
555 tty3270_update_status(tp);
557 /* Clear input area. */
558 tty3270_update_prompt(tp, NULL, 0);
559 tty3270_set_timer(tp, 1);
560 } else if (tp->input->string[0] == 0x6d) {
561 /* Display has been cleared. Redraw. */
562 tp->update_flags = TTY_UPDATE_ALL;
563 tty3270_set_timer(tp, 1);
565 spin_unlock_bh(&tp->view.lock);
567 /* Start keyboard reset command. */
568 raw3270_request_reset(tp->kreset);
569 raw3270_request_set_cmd(tp->kreset, TC_WRITE);
570 raw3270_request_add_data(tp->kreset, &kreset_data, 1);
571 raw3270_start(&tp->view, tp->kreset);
574 kbd_keycode(tp->kbd, *input++);
575 /* Emit keycode for AID byte. */
576 kbd_keycode(tp->kbd, 256 + tp->input->string[0]);
578 raw3270_request_reset(rrq);
579 xchg(&tp->read, rrq);
580 raw3270_put_view(&tp->view);
584 * Read request completion callback.
587 tty3270_read_callback(struct raw3270_request *rq, void *data)
589 struct tty3270 *tp = container_of(rq->view, struct tty3270, view);
590 raw3270_get_view(rq->view);
591 /* Schedule tasklet to pass input to tty. */
592 tasklet_schedule(&tp->readlet);
596 * Issue a read request. Call with device lock.
599 tty3270_issue_read(struct tty3270 *tp, int lock)
601 struct raw3270_request *rrq;
604 rrq = xchg(&tp->read, 0);
606 /* Read already scheduled. */
608 rrq->callback = tty3270_read_callback;
609 rrq->callback_data = tp;
610 raw3270_request_set_cmd(rrq, TC_READMOD);
611 raw3270_request_set_data(rrq, tp->input->string, tp->input->len);
612 /* Issue the read modified request. */
614 rc = raw3270_start(&tp->view, rrq);
616 rc = raw3270_start_irq(&tp->view, rrq);
618 raw3270_request_reset(rrq);
619 xchg(&tp->read, rrq);
624 * Switch to the tty view.
627 tty3270_activate(struct raw3270_view *view)
629 struct tty3270 *tp = container_of(view, struct tty3270, view);
631 tp->update_flags = TTY_UPDATE_ALL;
632 tty3270_set_timer(tp, 1);
637 tty3270_deactivate(struct raw3270_view *view)
639 struct tty3270 *tp = container_of(view, struct tty3270, view);
641 del_timer(&tp->timer);
645 tty3270_irq(struct tty3270 *tp, struct raw3270_request *rq, struct irb *irb)
647 /* Handle ATTN. Schedule tasklet to read aid. */
648 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
650 tty3270_issue_read(tp, 0);
656 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
659 /* Normal end. Copy residual count. */
660 rq->rescnt = irb->scsw.cmd.count;
662 return RAW3270_IO_DONE;
666 * Allocate tty3270 structure.
668 static struct tty3270 *
669 tty3270_alloc_view(void)
674 tp = kzalloc(sizeof(struct tty3270), GFP_KERNEL);
678 kmalloc(sizeof(void *) * TTY3270_STRING_PAGES, GFP_KERNEL);
679 if (!tp->freemem_pages)
681 INIT_LIST_HEAD(&tp->freemem);
682 INIT_LIST_HEAD(&tp->lines);
683 INIT_LIST_HEAD(&tp->update);
684 INIT_LIST_HEAD(&tp->rcl_lines);
686 tty_port_init(&tp->port);
687 setup_timer(&tp->timer, (void (*)(unsigned long)) tty3270_update,
689 tasklet_init(&tp->readlet,
690 (void (*)(unsigned long)) tty3270_read_tasklet,
691 (unsigned long) tp->read);
693 for (pages = 0; pages < TTY3270_STRING_PAGES; pages++) {
694 tp->freemem_pages[pages] = (void *)
695 __get_free_pages(GFP_KERNEL|GFP_DMA, 0);
696 if (!tp->freemem_pages[pages])
698 add_string_memory(&tp->freemem,
699 tp->freemem_pages[pages], PAGE_SIZE);
701 tp->write = raw3270_request_alloc(TTY3270_OUTPUT_BUFFER_SIZE);
702 if (IS_ERR(tp->write))
704 tp->read = raw3270_request_alloc(0);
705 if (IS_ERR(tp->read))
707 tp->kreset = raw3270_request_alloc(1);
708 if (IS_ERR(tp->kreset))
710 tp->kbd = kbd_alloc();
716 raw3270_request_free(tp->kreset);
718 raw3270_request_free(tp->read);
720 raw3270_request_free(tp->write);
723 free_pages((unsigned long) tp->freemem_pages[pages], 0);
724 kfree(tp->freemem_pages);
728 return ERR_PTR(-ENOMEM);
732 * Free tty3270 structure.
735 tty3270_free_view(struct tty3270 *tp)
739 del_timer_sync(&tp->timer);
741 raw3270_request_free(tp->kreset);
742 raw3270_request_free(tp->read);
743 raw3270_request_free(tp->write);
744 for (pages = 0; pages < TTY3270_STRING_PAGES; pages++)
745 free_pages((unsigned long) tp->freemem_pages[pages], 0);
746 kfree(tp->freemem_pages);
751 * Allocate tty3270 screen.
754 tty3270_alloc_screen(struct tty3270 *tp)
759 size = sizeof(struct tty3270_line) * (tp->view.rows - 2);
760 tp->screen = kzalloc(size, GFP_KERNEL);
763 for (lines = 0; lines < tp->view.rows - 2; lines++) {
764 size = sizeof(struct tty3270_cell) * tp->view.cols;
765 tp->screen[lines].cells = kzalloc(size, GFP_KERNEL);
766 if (!tp->screen[lines].cells)
772 kfree(tp->screen[lines].cells);
779 * Free tty3270 screen.
782 tty3270_free_screen(struct tty3270 *tp)
786 for (lines = 0; lines < tp->view.rows - 2; lines++)
787 kfree(tp->screen[lines].cells);
792 * Unlink tty3270 data structure from tty.
795 tty3270_release(struct raw3270_view *view)
797 struct tty3270 *tp = container_of(view, struct tty3270, view);
798 struct tty_struct *tty = tty_port_tty_get(&tp->port);
801 tty->driver_data = NULL;
802 tty_port_tty_set(&tp->port, NULL);
804 raw3270_put_view(&tp->view);
810 * Free tty3270 data structure
813 tty3270_free(struct raw3270_view *view)
815 struct tty3270 *tp = container_of(view, struct tty3270, view);
816 tty3270_free_screen(tp);
817 tty3270_free_view(tp);
821 * Delayed freeing of tty3270 views.
824 tty3270_del_views(void)
828 for (i = 0; i < tty3270_max_index; i++) {
829 struct raw3270_view *view =
830 raw3270_find_view(&tty3270_fn, i + RAW3270_FIRSTMINOR);
832 raw3270_del_view(view);
836 static struct raw3270_fn tty3270_fn = {
837 .activate = tty3270_activate,
838 .deactivate = tty3270_deactivate,
839 .intv = (void *) tty3270_irq,
840 .release = tty3270_release,
845 * This routine is called whenever a 3270 tty is opened.
848 tty3270_open(struct tty_struct *tty, struct file * filp)
850 struct raw3270_view *view;
856 /* Check if the tty3270 is already there. */
857 view = raw3270_find_view(&tty3270_fn,
858 tty->index + RAW3270_FIRSTMINOR);
860 tp = container_of(view, struct tty3270, view);
861 tty->driver_data = tp;
862 tty->winsize.ws_row = tp->view.rows - 2;
863 tty->winsize.ws_col = tp->view.cols;
864 tty->low_latency = 0;
865 /* why to reassign? */
866 tty_port_tty_set(&tp->port, tty);
867 tp->inattr = TF_INPUT;
870 if (tty3270_max_index < tty->index + 1)
871 tty3270_max_index = tty->index + 1;
873 /* Quick exit if there is no device for tty->index. */
874 if (PTR_ERR(view) == -ENODEV)
877 /* Allocate tty3270 structure on first open. */
878 tp = tty3270_alloc_view();
882 rc = raw3270_add_view(&tp->view, &tty3270_fn,
883 tty->index + RAW3270_FIRSTMINOR);
885 tty3270_free_view(tp);
889 rc = tty3270_alloc_screen(tp);
891 raw3270_put_view(&tp->view);
892 raw3270_del_view(&tp->view);
896 tty_port_tty_set(&tp->port, tty);
897 tty->low_latency = 0;
898 tty->driver_data = tp;
899 tty->winsize.ws_row = tp->view.rows - 2;
900 tty->winsize.ws_col = tp->view.cols;
902 tty3270_create_prompt(tp);
903 tty3270_create_status(tp);
904 tty3270_update_status(tp);
906 /* Create blank line for every line in the tty output area. */
907 for (i = 0; i < tp->view.rows - 2; i++)
908 tty3270_blank_line(tp);
910 tp->kbd->port = &tp->port;
911 tp->kbd->fn_handler[KVAL(K_INCRCONSOLE)] = tty3270_exit_tty;
912 tp->kbd->fn_handler[KVAL(K_SCROLLBACK)] = tty3270_scroll_backward;
913 tp->kbd->fn_handler[KVAL(K_SCROLLFORW)] = tty3270_scroll_forward;
914 tp->kbd->fn_handler[KVAL(K_CONS)] = tty3270_rcl_backward;
915 kbd_ascebc(tp->kbd, tp->view.ascebc);
917 raw3270_activate_view(&tp->view);
922 * This routine is called when the 3270 tty is closed. We wait
923 * for the remaining request to be completed. Then we clean up.
926 tty3270_close(struct tty_struct *tty, struct file * filp)
928 struct tty3270 *tp = tty->driver_data;
933 tty->driver_data = NULL;
934 tty_port_tty_set(&tp->port, NULL);
935 raw3270_put_view(&tp->view);
940 * We always have room.
943 tty3270_write_room(struct tty_struct *tty)
949 * Insert character into the screen at the current position with the
950 * current color and highlight. This function does NOT do cursor movement.
952 static void tty3270_put_character(struct tty3270 *tp, char ch)
954 struct tty3270_line *line;
955 struct tty3270_cell *cell;
957 line = tp->screen + tp->cy;
958 if (line->len <= tp->cx) {
959 while (line->len < tp->cx) {
960 cell = line->cells + line->len;
961 cell->character = tp->view.ascebc[' '];
962 cell->highlight = tp->highlight;
963 cell->f_color = tp->f_color;
968 cell = line->cells + tp->cx;
969 cell->character = tp->view.ascebc[(unsigned int) ch];
970 cell->highlight = tp->highlight;
971 cell->f_color = tp->f_color;
975 * Convert a tty3270_line to a 3270 data fragment usable for output.
978 tty3270_convert_line(struct tty3270 *tp, int line_nr)
980 struct tty3270_line *line;
981 struct tty3270_cell *cell;
982 struct string *s, *n;
983 unsigned char highlight;
984 unsigned char f_color;
988 /* Determine how long the fragment will be. */
989 flen = 3; /* Prefix (TO_SBA). */
990 line = tp->screen + line_nr;
992 highlight = TAX_RESET;
994 for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
995 if (cell->highlight != highlight) {
996 flen += 3; /* TO_SA to switch highlight. */
997 highlight = cell->highlight;
999 if (cell->f_color != f_color) {
1000 flen += 3; /* TO_SA to switch color. */
1001 f_color = cell->f_color;
1004 if (highlight != TAX_RESET)
1005 flen += 3; /* TO_SA to reset hightlight. */
1006 if (f_color != TAC_RESET)
1007 flen += 3; /* TO_SA to reset color. */
1008 if (line->len < tp->view.cols)
1009 flen += 4; /* Postfix (TO_RA). */
1011 /* Find the line in the list. */
1012 i = tp->view.rows - 2 - line_nr;
1013 list_for_each_entry_reverse(s, &tp->lines, list)
1017 * Check if the line needs to get reallocated.
1019 if (s->len != flen) {
1020 /* Reallocate string. */
1021 n = tty3270_alloc_string(tp, flen);
1022 list_add(&n->list, &s->list);
1023 list_del_init(&s->list);
1024 if (!list_empty(&s->update))
1025 list_del_init(&s->update);
1026 free_string(&tp->freemem, s);
1030 /* Write 3270 data fragment. */
1036 highlight = TAX_RESET;
1037 f_color = TAC_RESET;
1038 for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
1039 if (cell->highlight != highlight) {
1042 *cp++ = cell->highlight;
1043 highlight = cell->highlight;
1045 if (cell->f_color != f_color) {
1048 *cp++ = cell->f_color;
1049 f_color = cell->f_color;
1051 *cp++ = cell->character;
1053 if (highlight != TAX_RESET) {
1058 if (f_color != TAC_RESET) {
1063 if (line->len < tp->view.cols) {
1070 if (tp->nr_up + line_nr < tp->view.rows - 2) {
1071 /* Line is currently visible on screen. */
1072 tty3270_update_string(tp, s, line_nr);
1073 /* Add line to update list. */
1074 if (list_empty(&s->update)) {
1075 list_add_tail(&s->update, &tp->update);
1076 tp->update_flags |= TTY_UPDATE_LIST;
1082 * Do carriage return.
1085 tty3270_cr(struct tty3270 *tp)
1094 tty3270_lf(struct tty3270 *tp)
1096 struct tty3270_line temp;
1099 tty3270_convert_line(tp, tp->cy);
1100 if (tp->cy < tp->view.rows - 3) {
1104 /* Last line just filled up. Add new, blank line. */
1105 tty3270_blank_line(tp);
1106 temp = tp->screen[0];
1108 for (i = 0; i < tp->view.rows - 3; i++)
1109 tp->screen[i] = tp->screen[i+1];
1110 tp->screen[tp->view.rows - 3] = temp;
1111 tty3270_rebuild_update(tp);
1115 tty3270_ri(struct tty3270 *tp)
1118 tty3270_convert_line(tp, tp->cy);
1124 * Insert characters at current position.
1127 tty3270_insert_characters(struct tty3270 *tp, int n)
1129 struct tty3270_line *line;
1132 line = tp->screen + tp->cy;
1133 while (line->len < tp->cx) {
1134 line->cells[line->len].character = tp->view.ascebc[' '];
1135 line->cells[line->len].highlight = TAX_RESET;
1136 line->cells[line->len].f_color = TAC_RESET;
1139 if (n > tp->view.cols - tp->cx)
1140 n = tp->view.cols - tp->cx;
1141 k = min_t(int, line->len - tp->cx, tp->view.cols - tp->cx - n);
1143 line->cells[tp->cx + n + k] = line->cells[tp->cx + k];
1145 if (line->len > tp->view.cols)
1146 line->len = tp->view.cols;
1148 line->cells[tp->cx + n].character = tp->view.ascebc[' '];
1149 line->cells[tp->cx + n].highlight = tp->highlight;
1150 line->cells[tp->cx + n].f_color = tp->f_color;
1155 * Delete characters at current position.
1158 tty3270_delete_characters(struct tty3270 *tp, int n)
1160 struct tty3270_line *line;
1163 line = tp->screen + tp->cy;
1164 if (line->len <= tp->cx)
1166 if (line->len - tp->cx <= n) {
1170 for (i = tp->cx; i + n < line->len; i++)
1171 line->cells[i] = line->cells[i + n];
1176 * Erase characters at current position.
1179 tty3270_erase_characters(struct tty3270 *tp, int n)
1181 struct tty3270_line *line;
1182 struct tty3270_cell *cell;
1184 line = tp->screen + tp->cy;
1185 while (line->len > tp->cx && n-- > 0) {
1186 cell = line->cells + tp->cx++;
1187 cell->character = ' ';
1188 cell->highlight = TAX_RESET;
1189 cell->f_color = TAC_RESET;
1192 tp->cx = min_t(int, tp->cx, tp->view.cols - 1);
1196 * Erase line, 3 different cases:
1197 * Esc [ 0 K Erase from current position to end of line inclusive
1198 * Esc [ 1 K Erase from beginning of line to current position inclusive
1199 * Esc [ 2 K Erase entire line (without moving cursor)
1202 tty3270_erase_line(struct tty3270 *tp, int mode)
1204 struct tty3270_line *line;
1205 struct tty3270_cell *cell;
1208 line = tp->screen + tp->cy;
1211 else if (mode == 1) {
1212 for (i = 0; i < tp->cx; i++) {
1213 cell = line->cells + i;
1214 cell->character = ' ';
1215 cell->highlight = TAX_RESET;
1216 cell->f_color = TAC_RESET;
1218 if (line->len <= tp->cx)
1219 line->len = tp->cx + 1;
1220 } else if (mode == 2)
1222 tty3270_convert_line(tp, tp->cy);
1226 * Erase display, 3 different cases:
1227 * Esc [ 0 J Erase from current position to bottom of screen inclusive
1228 * Esc [ 1 J Erase from top of screen to current position inclusive
1229 * Esc [ 2 J Erase entire screen (without moving the cursor)
1232 tty3270_erase_display(struct tty3270 *tp, int mode)
1237 tty3270_erase_line(tp, 0);
1238 for (i = tp->cy + 1; i < tp->view.rows - 2; i++) {
1239 tp->screen[i].len = 0;
1240 tty3270_convert_line(tp, i);
1242 } else if (mode == 1) {
1243 for (i = 0; i < tp->cy; i++) {
1244 tp->screen[i].len = 0;
1245 tty3270_convert_line(tp, i);
1247 tty3270_erase_line(tp, 1);
1248 } else if (mode == 2) {
1249 for (i = 0; i < tp->view.rows - 2; i++) {
1250 tp->screen[i].len = 0;
1251 tty3270_convert_line(tp, i);
1254 tty3270_rebuild_update(tp);
1258 * Set attributes found in an escape sequence.
1259 * Esc [ <attr> ; <attr> ; ... m
1262 tty3270_set_attributes(struct tty3270 *tp)
1264 static unsigned char f_colors[] = {
1265 TAC_DEFAULT, TAC_RED, TAC_GREEN, TAC_YELLOW, TAC_BLUE,
1266 TAC_PINK, TAC_TURQ, TAC_WHITE, 0, TAC_DEFAULT
1270 for (i = 0; i <= tp->esc_npar; i++) {
1271 attr = tp->esc_par[i];
1274 tp->highlight = TAX_RESET;
1275 tp->f_color = TAC_RESET;
1278 case 4: /* Start underlining. */
1279 tp->highlight = TAX_UNDER;
1281 case 5: /* Start blink. */
1282 tp->highlight = TAX_BLINK;
1284 case 7: /* Start reverse. */
1285 tp->highlight = TAX_REVER;
1287 case 24: /* End underlining */
1288 if (tp->highlight == TAX_UNDER)
1289 tp->highlight = TAX_RESET;
1291 case 25: /* End blink. */
1292 if (tp->highlight == TAX_BLINK)
1293 tp->highlight = TAX_RESET;
1295 case 27: /* End reverse. */
1296 if (tp->highlight == TAX_REVER)
1297 tp->highlight = TAX_RESET;
1299 /* Foreground color. */
1300 case 30: /* Black */
1302 case 32: /* Green */
1303 case 33: /* Yellow */
1305 case 35: /* Magenta */
1307 case 37: /* White */
1308 case 39: /* Black */
1309 tp->f_color = f_colors[attr - 30];
1316 tty3270_getpar(struct tty3270 *tp, int ix)
1318 return (tp->esc_par[ix] > 0) ? tp->esc_par[ix] : 1;
1322 tty3270_goto_xy(struct tty3270 *tp, int cx, int cy)
1324 int max_cx = max(0, cx);
1325 int max_cy = max(0, cy);
1327 tp->cx = min_t(int, tp->view.cols - 1, max_cx);
1328 cy = min_t(int, tp->view.rows - 3, max_cy);
1330 tty3270_convert_line(tp, tp->cy);
1336 * Process escape sequences. Known sequences:
1337 * Esc 7 Save Cursor Position
1338 * Esc 8 Restore Cursor Position
1339 * Esc [ Pn ; Pn ; .. m Set attributes
1340 * Esc [ Pn ; Pn H Cursor Position
1341 * Esc [ Pn ; Pn f Cursor Position
1342 * Esc [ Pn A Cursor Up
1343 * Esc [ Pn B Cursor Down
1344 * Esc [ Pn C Cursor Forward
1345 * Esc [ Pn D Cursor Backward
1346 * Esc [ Pn G Cursor Horizontal Absolute
1347 * Esc [ Pn X Erase Characters
1348 * Esc [ Ps J Erase in Display
1349 * Esc [ Ps K Erase in Line
1350 * // FIXME: add all the new ones.
1352 * Pn is a numeric parameter, a string of zero or more decimal digits.
1353 * Ps is a selective parameter.
1356 tty3270_escape_sequence(struct tty3270 *tp, char ch)
1358 enum { ESnormal, ESesc, ESsquare, ESgetpars };
1360 if (tp->esc_state == ESnormal) {
1362 /* Starting new escape sequence. */
1363 tp->esc_state = ESesc;
1366 if (tp->esc_state == ESesc) {
1367 tp->esc_state = ESnormal;
1370 tp->esc_state = ESsquare;
1382 case 'Z': /* Respond ID. */
1383 kbd_puts_queue(&tp->port, "\033[?6c");
1385 case '7': /* Save cursor position. */
1386 tp->saved_cx = tp->cx;
1387 tp->saved_cy = tp->cy;
1388 tp->saved_highlight = tp->highlight;
1389 tp->saved_f_color = tp->f_color;
1391 case '8': /* Restore cursor position. */
1392 tty3270_convert_line(tp, tp->cy);
1393 tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
1394 tp->highlight = tp->saved_highlight;
1395 tp->f_color = tp->saved_f_color;
1397 case 'c': /* Reset terminal. */
1398 tp->cx = tp->saved_cx = 0;
1399 tp->cy = tp->saved_cy = 0;
1400 tp->highlight = tp->saved_highlight = TAX_RESET;
1401 tp->f_color = tp->saved_f_color = TAC_RESET;
1402 tty3270_erase_display(tp, 2);
1407 if (tp->esc_state == ESsquare) {
1408 tp->esc_state = ESgetpars;
1409 memset(tp->esc_par, 0, sizeof(tp->esc_par));
1411 tp->esc_ques = (ch == '?');
1415 if (tp->esc_state == ESgetpars) {
1416 if (ch == ';' && tp->esc_npar < ESCAPE_NPAR - 1) {
1420 if (ch >= '0' && ch <= '9') {
1421 tp->esc_par[tp->esc_npar] *= 10;
1422 tp->esc_par[tp->esc_npar] += ch - '0';
1426 tp->esc_state = ESnormal;
1427 if (ch == 'n' && !tp->esc_ques) {
1428 if (tp->esc_par[0] == 5) /* Status report. */
1429 kbd_puts_queue(&tp->port, "\033[0n");
1430 else if (tp->esc_par[0] == 6) { /* Cursor report. */
1432 sprintf(buf, "\033[%d;%dR", tp->cy + 1, tp->cx + 1);
1433 kbd_puts_queue(&tp->port, buf);
1441 tty3270_set_attributes(tp);
1443 case 'H': /* Set cursor position. */
1445 tty3270_goto_xy(tp, tty3270_getpar(tp, 1) - 1,
1446 tty3270_getpar(tp, 0) - 1);
1448 case 'd': /* Set y position. */
1449 tty3270_goto_xy(tp, tp->cx, tty3270_getpar(tp, 0) - 1);
1451 case 'A': /* Cursor up. */
1453 tty3270_goto_xy(tp, tp->cx, tp->cy - tty3270_getpar(tp, 0));
1455 case 'B': /* Cursor down. */
1458 tty3270_goto_xy(tp, tp->cx, tp->cy + tty3270_getpar(tp, 0));
1460 case 'C': /* Cursor forward. */
1462 tty3270_goto_xy(tp, tp->cx + tty3270_getpar(tp, 0), tp->cy);
1464 case 'D': /* Cursor backward. */
1465 tty3270_goto_xy(tp, tp->cx - tty3270_getpar(tp, 0), tp->cy);
1467 case 'G': /* Set x position. */
1469 tty3270_goto_xy(tp, tty3270_getpar(tp, 0), tp->cy);
1471 case 'X': /* Erase Characters. */
1472 tty3270_erase_characters(tp, tty3270_getpar(tp, 0));
1474 case 'J': /* Erase display. */
1475 tty3270_erase_display(tp, tp->esc_par[0]);
1477 case 'K': /* Erase line. */
1478 tty3270_erase_line(tp, tp->esc_par[0]);
1480 case 'P': /* Delete characters. */
1481 tty3270_delete_characters(tp, tty3270_getpar(tp, 0));
1483 case '@': /* Insert characters. */
1484 tty3270_insert_characters(tp, tty3270_getpar(tp, 0));
1486 case 's': /* Save cursor position. */
1487 tp->saved_cx = tp->cx;
1488 tp->saved_cy = tp->cy;
1489 tp->saved_highlight = tp->highlight;
1490 tp->saved_f_color = tp->f_color;
1492 case 'u': /* Restore cursor position. */
1493 tty3270_convert_line(tp, tp->cy);
1494 tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
1495 tp->highlight = tp->saved_highlight;
1496 tp->f_color = tp->saved_f_color;
1502 * String write routine for 3270 ttys
1505 tty3270_do_write(struct tty3270 *tp, struct tty_struct *tty,
1506 const unsigned char *buf, int count)
1510 spin_lock_bh(&tp->view.lock);
1511 for (i_msg = 0; !tty->stopped && i_msg < count; i_msg++) {
1512 if (tp->esc_state != 0) {
1513 /* Continue escape sequence. */
1514 tty3270_escape_sequence(tp, buf[i_msg]);
1518 switch (buf[i_msg]) {
1519 case 0x07: /* '\a' -- Alarm */
1520 tp->wcc |= TW_PLUSALARM;
1522 case 0x08: /* Backspace. */
1525 tty3270_put_character(tp, ' ');
1528 case 0x09: /* '\t' -- Tabulate */
1529 for (i = tp->cx % 8; i < 8; i++) {
1530 if (tp->cx >= tp->view.cols) {
1535 tty3270_put_character(tp, ' ');
1539 case 0x0a: /* '\n' -- New Line */
1543 case 0x0c: /* '\f' -- Form Feed */
1544 tty3270_erase_display(tp, 2);
1545 tp->cx = tp->cy = 0;
1547 case 0x0d: /* '\r' -- Carriage Return */
1550 case 0x0f: /* SuSE "exit alternate mode" */
1552 case 0x1b: /* Start escape sequence. */
1553 tty3270_escape_sequence(tp, buf[i_msg]);
1555 default: /* Insert normal character. */
1556 if (tp->cx >= tp->view.cols) {
1560 tty3270_put_character(tp, buf[i_msg]);
1565 /* Convert current line to 3270 data fragment. */
1566 tty3270_convert_line(tp, tp->cy);
1568 /* Setup timer to update display after 1/10 second */
1569 if (!timer_pending(&tp->timer))
1570 tty3270_set_timer(tp, HZ/10);
1572 spin_unlock_bh(&tp->view.lock);
1576 * String write routine for 3270 ttys
1579 tty3270_write(struct tty_struct * tty,
1580 const unsigned char *buf, int count)
1584 tp = tty->driver_data;
1587 if (tp->char_count > 0) {
1588 tty3270_do_write(tp, tty, tp->char_buf, tp->char_count);
1591 tty3270_do_write(tp, tty, buf, count);
1596 * Put single characters to the ttys character buffer
1598 static int tty3270_put_char(struct tty_struct *tty, unsigned char ch)
1602 tp = tty->driver_data;
1603 if (!tp || tp->char_count >= TTY3270_CHAR_BUF_SIZE)
1605 tp->char_buf[tp->char_count++] = ch;
1610 * Flush all characters from the ttys characeter buffer put there
1611 * by tty3270_put_char.
1614 tty3270_flush_chars(struct tty_struct *tty)
1618 tp = tty->driver_data;
1621 if (tp->char_count > 0) {
1622 tty3270_do_write(tp, tty, tp->char_buf, tp->char_count);
1628 * Returns the number of characters in the output buffer. This is
1629 * used in tty_wait_until_sent to wait until all characters have
1630 * appeared on the screen.
1633 tty3270_chars_in_buffer(struct tty_struct *tty)
1639 tty3270_flush_buffer(struct tty_struct *tty)
1644 * Check for visible/invisible input switches
1647 tty3270_set_termios(struct tty_struct *tty, struct ktermios *old)
1652 tp = tty->driver_data;
1655 spin_lock_bh(&tp->view.lock);
1656 if (L_ICANON(tty)) {
1657 new = L_ECHO(tty) ? TF_INPUT: TF_INPUTN;
1658 if (new != tp->inattr) {
1660 tty3270_update_prompt(tp, NULL, 0);
1661 tty3270_set_timer(tp, 1);
1664 spin_unlock_bh(&tp->view.lock);
1668 * Disable reading from a 3270 tty
1671 tty3270_throttle(struct tty_struct * tty)
1675 tp = tty->driver_data;
1682 * Enable reading from a 3270 tty
1685 tty3270_unthrottle(struct tty_struct * tty)
1689 tp = tty->driver_data;
1694 tty3270_issue_read(tp, 1);
1698 * Hang up the tty device.
1701 tty3270_hangup(struct tty_struct *tty)
1707 tty3270_wait_until_sent(struct tty_struct *tty, int timeout)
1711 static int tty3270_ioctl(struct tty_struct *tty, unsigned int cmd,
1716 tp = tty->driver_data;
1719 if (tty->flags & (1 << TTY_IO_ERROR))
1721 return kbd_ioctl(tp->kbd, cmd, arg);
1724 #ifdef CONFIG_COMPAT
1725 static long tty3270_compat_ioctl(struct tty_struct *tty,
1726 unsigned int cmd, unsigned long arg)
1730 tp = tty->driver_data;
1733 if (tty->flags & (1 << TTY_IO_ERROR))
1735 return kbd_ioctl(tp->kbd, cmd, (unsigned long)compat_ptr(arg));
1739 static const struct tty_operations tty3270_ops = {
1740 .open = tty3270_open,
1741 .close = tty3270_close,
1742 .write = tty3270_write,
1743 .put_char = tty3270_put_char,
1744 .flush_chars = tty3270_flush_chars,
1745 .write_room = tty3270_write_room,
1746 .chars_in_buffer = tty3270_chars_in_buffer,
1747 .flush_buffer = tty3270_flush_buffer,
1748 .throttle = tty3270_throttle,
1749 .unthrottle = tty3270_unthrottle,
1750 .hangup = tty3270_hangup,
1751 .wait_until_sent = tty3270_wait_until_sent,
1752 .ioctl = tty3270_ioctl,
1753 #ifdef CONFIG_COMPAT
1754 .compat_ioctl = tty3270_compat_ioctl,
1756 .set_termios = tty3270_set_termios
1760 * 3270 tty registration code called from tty_init().
1761 * Most kernel services (incl. kmalloc) are available at this poimt.
1763 static int __init tty3270_init(void)
1765 struct tty_driver *driver;
1768 driver = alloc_tty_driver(RAW3270_MAXDEVS);
1773 * Initialize the tty_driver structure
1774 * Entries in tty3270_driver that are NOT initialized:
1775 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1777 driver->driver_name = "ttyTUB";
1778 driver->name = "ttyTUB";
1779 driver->major = IBM_TTY3270_MAJOR;
1780 driver->minor_start = RAW3270_FIRSTMINOR;
1781 driver->type = TTY_DRIVER_TYPE_SYSTEM;
1782 driver->subtype = SYSTEM_TYPE_TTY;
1783 driver->init_termios = tty_std_termios;
1784 driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_DYNAMIC_DEV;
1785 tty_set_operations(driver, &tty3270_ops);
1786 ret = tty_register_driver(driver);
1788 put_tty_driver(driver);
1791 tty3270_driver = driver;
1798 struct tty_driver *driver;
1800 driver = tty3270_driver;
1801 tty3270_driver = NULL;
1802 tty_unregister_driver(driver);
1803 tty3270_del_views();
1806 MODULE_LICENSE("GPL");
1807 MODULE_ALIAS_CHARDEV_MAJOR(IBM_TTY3270_MAJOR);
1809 module_init(tty3270_init);
1810 module_exit(tty3270_exit);