ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / s390 / char / tty3270.c
1 /*
2  *  drivers/s390/char/tty3270.c
3  *    IBM/3270 Driver - tty functions.
4  *
5  *  Author(s):
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
9  */
10
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>
20
21 #include <linux/slab.h>
22 #include <linux/bootmem.h>
23
24 #include <asm/ccwdev.h>
25 #include <asm/cio.h>
26 #include <asm/ebcdic.h>
27 #include <asm/uaccess.h>
28
29
30 #include "raw3270.h"
31 #include "keyboard.h"
32
33 #define TTY3270_CHAR_BUF_SIZE 256
34 #define TTY3270_OUTPUT_BUFFER_SIZE 1024
35 #define TTY3270_STRING_PAGES 5
36
37 struct tty_driver *tty3270_driver;
38
39 struct raw3270_fn tty3270_fn;
40
41 struct tty3270_cell {
42         unsigned char character;
43         unsigned char highlight;
44         unsigned char f_color;
45 };
46
47 struct tty3270_line {
48         struct tty3270_cell *cells;
49         int len;
50 };
51
52 #define ESCAPE_NPAR 8
53
54 /*
55  * The main tty view data structure.
56  * FIXME:
57  * 1) describe line orientation & lines list concept against screen
58  * 2) describe conversion of screen to lines
59  * 3) describe line format.
60  */
61 struct tty3270 {
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. */
66
67         /* Output stuff. */
68         struct list_head lines;         /* List of lines. */
69         struct list_head update;        /* List of lines to update. */
70         unsigned char wcc;              /* Write control character. */
71         int nr_lines;                   /* # lines in list. */
72         int nr_up;                      /* # lines up in history. */
73         unsigned long update_flags;     /* Update indication bits. */
74         struct string *status;          /* Lower right of display. */
75         struct raw3270_request *write;  /* Single write request. */
76         struct timer_list timer;        /* Output delay timer. */
77
78         /* Current tty screen. */
79         unsigned int cx, cy;            /* Current output position. */
80         unsigned int highlight;         /* Blink/reverse/underscore */
81         unsigned int f_color;           /* Foreground color */
82         struct tty3270_line *screen;
83
84         /* Input stuff. */
85         struct string *prompt;          /* Output string for input area. */
86         struct string *input;           /* Input string for read request. */
87         struct raw3270_request *read;   /* Single read request. */
88         struct raw3270_request *kreset; /* Single keyboard reset request. */
89         unsigned char inattr;           /* Visible/invisible input. */
90         int throttle, attn;             /* tty throttle/unthrottle. */
91         struct tasklet_struct readlet;  /* Tasklet to issue read request. */
92         struct kbd_data *kbd;           /* key_maps stuff. */
93
94         /* Escape sequence parsing. */
95         int esc_state, esc_ques, esc_npar;
96         int esc_par[ESCAPE_NPAR];
97         unsigned int saved_cx, saved_cy;
98         unsigned int saved_highlight, saved_f_color;
99
100         /* Command recalling. */
101         struct list_head rcl_lines;     /* List of recallable lines. */
102         struct list_head *rcl_walk;     /* Point in rcl_lines list. */
103         int rcl_nr, rcl_max;            /* Number/max number of rcl_lines. */
104
105         /* Character array for put_char/flush_chars. */
106         unsigned int char_count;
107         char char_buf[TTY3270_CHAR_BUF_SIZE];
108 };
109
110 /* tty3270->update_flags. See tty3270_update for details. */
111 #define TTY_UPDATE_ERASE        1       /* Use EWRITEA instead of WRITE. */
112 #define TTY_UPDATE_LIST         2       /* Update lines in tty3270->update. */
113 #define TTY_UPDATE_INPUT        4       /* Update input line. */
114 #define TTY_UPDATE_STATUS       8       /* Update status line. */
115 #define TTY_UPDATE_ALL          15
116
117 static void tty3270_update(struct tty3270 *);
118
119 /*
120  * Setup timeout for a device. On timeout trigger an update.
121  */
122 void
123 tty3270_set_timer(struct tty3270 *tp, int expires)
124 {
125         if (expires == 0) {
126                 if (timer_pending(&tp->timer)) {
127                         raw3270_put_view(&tp->view);
128                         del_timer(&tp->timer);
129                 }
130                 return;
131         }
132         if (timer_pending(&tp->timer)) {
133                 if (mod_timer(&tp->timer, jiffies + expires))
134                         return;
135         }
136         raw3270_get_view(&tp->view);
137         tp->timer.function = (void (*)(unsigned long)) tty3270_update;
138         tp->timer.data = (unsigned long) tp;
139         tp->timer.expires = jiffies + expires;
140         add_timer(&tp->timer);
141 }
142
143 /*
144  * The input line are the two last lines of the screen.
145  */
146 static void
147 tty3270_update_prompt(struct tty3270 *tp, char *input, int count)
148 {
149         struct string *line;
150         unsigned int off;
151
152         line = tp->prompt;
153         if (count != 0)
154                 line->string[5] = TF_INMDT;
155         else
156                 line->string[5] = tp->inattr;
157         if (count > tp->view.cols * 2 - 11)
158                 count = tp->view.cols * 2 - 11;
159         memcpy(line->string + 6, input, count);
160         line->string[6 + count] = TO_IC;
161         /* Clear to end of input line. */
162         if (count < tp->view.cols * 2 - 11) {
163                 line->string[7 + count] = TO_RA;
164                 line->string[10 + count] = 0;
165                 off = tp->view.cols * tp->view.rows - 9;
166                 raw3270_buffer_address(tp->view.dev, line->string+count+8, off);
167                 line->len = 11 + count;
168         } else
169                 line->len = 7 + count;
170         tp->update_flags |= TTY_UPDATE_INPUT;
171 }
172
173 static void
174 tty3270_create_prompt(struct tty3270 *tp)
175 {
176         static const unsigned char blueprint[] =
177                 { TO_SBA, 0, 0, 0x6e, TO_SF, TF_INPUT,
178                   /* empty input string */
179                   TO_IC, TO_RA, 0, 0, 0 };
180         struct string *line;
181         unsigned int offset;
182
183         line = alloc_string(&tp->freemem,
184                             sizeof(blueprint) + tp->view.cols * 2 - 9);
185         tp->prompt = line;
186         tp->inattr = TF_INPUT;
187         /* Copy blueprint to status line */
188         memcpy(line->string, blueprint, sizeof(blueprint));
189         line->len = sizeof(blueprint);
190         /* Set output offsets. */
191         offset = tp->view.cols * (tp->view.rows - 2);
192         raw3270_buffer_address(tp->view.dev, line->string + 1, offset);
193         offset = tp->view.cols * tp->view.rows - 9;
194         raw3270_buffer_address(tp->view.dev, line->string + 8, offset);
195
196         /* Allocate input string for reading. */
197         tp->input = alloc_string(&tp->freemem, tp->view.cols * 2 - 9 + 6);
198 }
199
200 /*
201  * The status line is the last line of the screen. It shows the string
202  * "Running"/"Holding" in the lower right corner of the screen.
203  */
204 static void
205 tty3270_update_status(struct tty3270 * tp)
206 {
207         char *str;
208
209         str = (tp->nr_up != 0) ? "History" : "Running";
210         memcpy(tp->status->string + 8, str, 7);
211         codepage_convert(tp->view.ascebc, tp->status->string + 8, 7);
212         tp->update_flags |= TTY_UPDATE_STATUS;
213 }
214
215 static void
216 tty3270_create_status(struct tty3270 * tp)
217 {
218         static const unsigned char blueprint[] =
219                 { TO_SBA, 0, 0, TO_SF, TF_LOG, TO_SA, TAT_COLOR, TAC_GREEN,
220                   0, 0, 0, 0, 0, 0, 0, TO_SF, TF_LOG, TO_SA, TAT_COLOR,
221                   TAC_RESET };
222         struct string *line;
223         unsigned int offset;
224
225         line = alloc_string(&tp->freemem,sizeof(blueprint));
226         tp->status = line;
227         /* Copy blueprint to status line */
228         memcpy(line->string, blueprint, sizeof(blueprint));
229         /* Set address to start of status string (= last 9 characters). */
230         offset = tp->view.cols * tp->view.rows - 9;
231         raw3270_buffer_address(tp->view.dev, line->string + 1, offset);
232 }
233
234 /*
235  * Set output offsets to 3270 datastream fragment of a tty string.
236  * (TO_SBA offset at the start and TO_RA offset at the end of the string)
237  */
238 static void
239 tty3270_update_string(struct tty3270 *tp, struct string *line, int nr)
240 {
241         unsigned char *cp;
242
243         raw3270_buffer_address(tp->view.dev, line->string + 1,
244                                tp->view.cols * nr);
245         cp = line->string + line->len - 4;
246         if (*cp == TO_RA)
247                 raw3270_buffer_address(tp->view.dev, cp + 1,
248                                        tp->view.cols * (nr + 1));
249 }
250
251 /*
252  * Rebuild update list to print all lines.
253  */
254 static void
255 tty3270_rebuild_update(struct tty3270 *tp)
256 {
257         struct string *s, *n;
258         int line, nr_up;
259
260         /* 
261          * Throw away update list and create a new one,
262          * containing all lines that will fit on the screen.
263          */
264         list_for_each_entry_safe(s, n, &tp->update, update)
265                 list_del_init(&s->update);
266         line = tp->view.rows - 3;
267         nr_up = tp->nr_up;
268         list_for_each_entry_reverse(s, &tp->lines, list) {
269                 if (nr_up > 0) {
270                         nr_up--;
271                         continue;
272                 }
273                 tty3270_update_string(tp, s, line);
274                 list_add(&s->update, &tp->update);
275                 if (--line < 0)
276                         break;
277         }
278         tp->update_flags |= TTY_UPDATE_LIST;
279 }
280
281 /*
282  * Alloc string for size bytes. If there is not enough room in
283  * freemem, free strings until there is room.
284  */
285 static struct string *
286 tty3270_alloc_string(struct tty3270 *tp, size_t size)
287 {
288         struct string *s, *n;
289
290         s = alloc_string(&tp->freemem, size);
291         if (s)
292                 return s;
293         list_for_each_entry_safe(s, n, &tp->lines, list) {
294                 BUG_ON(tp->nr_lines <= tp->view.rows - 2);
295                 list_del(&s->list);
296                 if (!list_empty(&s->update))
297                         list_del(&s->update);
298                 tp->nr_lines--;
299                 if (free_string(&tp->freemem, s) >= size)
300                         break;
301         }
302         s = alloc_string(&tp->freemem, size);
303         BUG_ON(!s);
304         if (tp->nr_up != 0 &&
305             tp->nr_up + tp->view.rows - 2 >= tp->nr_lines) {
306                 tp->nr_up = tp->nr_lines - tp->view.rows + 2;
307                 tty3270_rebuild_update(tp);
308                 tty3270_update_status(tp);
309         }
310         return s;
311 }
312
313 /*
314  * Add an empty line to the list.
315  */
316 static void
317 tty3270_blank_line(struct tty3270 *tp)
318 {
319         static const unsigned char blueprint[] =
320                 { TO_SBA, 0, 0, TO_SA, TAT_EXTHI, TAX_RESET,
321                   TO_SA, TAT_COLOR, TAC_RESET, TO_RA, 0, 0, 0 };
322         struct string *s;
323
324         s = tty3270_alloc_string(tp, sizeof(blueprint));
325         memcpy(s->string, blueprint, sizeof(blueprint));
326         s->len = sizeof(blueprint);
327         list_add_tail(&s->list, &tp->lines);
328         tp->nr_lines++;
329         if (tp->nr_up != 0)
330                 tp->nr_up++;
331 }
332
333 /*
334  * Write request completion callback.
335  */
336 static void
337 tty3270_write_callback(struct raw3270_request *rq, void *data)
338 {
339         struct tty3270 *tp;
340
341         tp = (struct tty3270 *) rq->view;
342         if (rq->rc != 0) {
343                 /* Write wasn't successfull. Refresh all. */
344                 tty3270_rebuild_update(tp);
345                 tp->update_flags = TTY_UPDATE_ALL;
346                 tty3270_set_timer(tp, 1);
347         }
348         raw3270_request_reset(rq);
349         xchg(&tp->write, rq);
350 }
351
352 /*
353  * Update 3270 display.
354  */
355 static void
356 tty3270_update(struct tty3270 *tp)
357 {
358         static char invalid_sba[2] = { 0xff, 0xff };
359         struct raw3270_request *wrq;
360         unsigned long updated;
361         struct string *s, *n;
362         char *sba, *str;
363         int rc, len;
364
365         wrq = xchg(&tp->write, 0);
366         if (!wrq) {
367                 tty3270_set_timer(tp, 1);
368                 return;
369         }
370
371         spin_lock(&tp->view.lock);
372         updated = 0;
373         if (tp->update_flags & TTY_UPDATE_ERASE) {
374                 /* Use erase write alternate to erase display. */
375                 raw3270_request_set_cmd(wrq, TC_EWRITEA);
376                 updated |= TTY_UPDATE_ERASE;
377         } else
378                 raw3270_request_set_cmd(wrq, TC_WRITE);
379
380         raw3270_request_add_data(wrq, &tp->wcc, 1);
381         tp->wcc = TW_NONE;
382
383         /*
384          * Update status line.
385          */
386         if (tp->update_flags & TTY_UPDATE_STATUS)
387                 if (raw3270_request_add_data(wrq, tp->status->string,
388                                              tp->status->len) == 0)
389                         updated |= TTY_UPDATE_STATUS;
390
391         /*
392          * Write input line.
393          */
394         if (tp->update_flags & TTY_UPDATE_INPUT)
395                 if (raw3270_request_add_data(wrq, tp->prompt->string,
396                                              tp->prompt->len) == 0)
397                         updated |= TTY_UPDATE_INPUT;
398
399         sba = invalid_sba;
400         
401         if (tp->update_flags & TTY_UPDATE_LIST) {
402                 /* Write strings in the update list to the screen. */
403                 list_for_each_entry_safe(s, n, &tp->update, update) {
404                         str = s->string;
405                         len = s->len;
406                         /*
407                          * Skip TO_SBA at the start of the string if the
408                          * last output position matches the start address
409                          * of this line.
410                          */
411                         if (s->string[1] == sba[0] && s->string[2] == sba[1])
412                                 str += 3, len -= 3;
413                         if (raw3270_request_add_data(wrq, str, len) != 0)
414                                 break;
415                         list_del_init(&s->update);
416                         sba = s->string + s->len - 3;
417                 }
418                 if (list_empty(&tp->update))
419                         updated |= TTY_UPDATE_LIST;
420         }
421         wrq->callback = tty3270_write_callback;
422         rc = raw3270_start(&tp->view, wrq);
423         if (rc == 0) {
424                 tp->update_flags &= ~updated;
425                 if (tp->update_flags)
426                         tty3270_set_timer(tp, 1);
427         } else {
428                 raw3270_request_reset(wrq);
429                 xchg(&tp->write, wrq);
430         }
431         spin_unlock(&tp->view.lock);
432         raw3270_put_view(&tp->view);
433 }
434
435 /*
436  * Command recalling.
437  */
438 static void
439 tty3270_rcl_add(struct tty3270 *tp, char *input, int len)
440 {
441         struct string *s;
442
443         tp->rcl_walk = 0;
444         if (len <= 0)
445                 return;
446         if (tp->rcl_nr >= tp->rcl_max) {
447                 s = list_entry(tp->rcl_lines.next, struct string, list);
448                 list_del(&s->list);
449                 free_string(&tp->freemem, s);
450                 tp->rcl_nr--;
451         }
452         s = tty3270_alloc_string(tp, len);
453         memcpy(s->string, input, len);
454         list_add_tail(&s->list, &tp->rcl_lines);
455         tp->rcl_nr++;
456 }
457
458 static void
459 tty3270_rcl_backward(struct kbd_data *kbd)
460 {
461         struct tty3270 *tp;
462         struct string *s;
463
464         tp = kbd->tty->driver_data;
465         spin_lock_bh(&tp->view.lock);
466         if (tp->inattr == TF_INPUT) {
467                 if (tp->rcl_walk && tp->rcl_walk->prev != &tp->rcl_lines)
468                         tp->rcl_walk = tp->rcl_walk->prev;
469                 else if (!list_empty(&tp->rcl_lines))
470                         tp->rcl_walk = tp->rcl_lines.prev;
471                 s = tp->rcl_walk ? 
472                         list_entry(tp->rcl_walk, struct string, list) : 0;
473                 if (tp->rcl_walk) {
474                         s = list_entry(tp->rcl_walk, struct string, list);
475                         tty3270_update_prompt(tp, s->string, s->len);
476                 } else
477                         tty3270_update_prompt(tp, 0, 0);
478                 tty3270_set_timer(tp, 1);
479         }
480         spin_unlock_bh(&tp->view.lock);
481 }
482
483 /*
484  * Deactivate tty view.
485  */
486 static void
487 tty3270_exit_tty(struct kbd_data *kbd)
488 {
489         struct tty3270 *tp;
490
491         tp = kbd->tty->driver_data;
492         raw3270_deactivate_view(&tp->view);
493 }
494
495 /*
496  * Scroll forward in history.
497  */
498 static void
499 tty3270_scroll_forward(struct kbd_data *kbd)
500 {
501         struct tty3270 *tp;
502         int nr_up;
503
504         tp = kbd->tty->driver_data;
505         spin_lock_bh(&tp->view.lock);
506         nr_up = tp->nr_up - tp->view.rows + 2;
507         if (nr_up < 0)
508                 nr_up = 0;
509         if (nr_up != tp->nr_up) {
510                 tp->nr_up = nr_up;
511                 tty3270_rebuild_update(tp);
512                 tty3270_update_status(tp);
513                 tty3270_set_timer(tp, 1);
514         }
515         spin_unlock_bh(&tp->view.lock);
516 }
517
518 /*
519  * Scroll backward in history.
520  */
521 static void
522 tty3270_scroll_backward(struct kbd_data *kbd)
523 {
524         struct tty3270 *tp;
525         int nr_up;
526
527         tp = kbd->tty->driver_data;
528         spin_lock_bh(&tp->view.lock);
529         nr_up = tp->nr_up + tp->view.rows - 2;
530         if (nr_up + tp->view.rows - 2 > tp->nr_lines)
531                 nr_up = tp->nr_lines - tp->view.rows + 2;
532         if (nr_up != tp->nr_up) {
533                 tp->nr_up = nr_up;
534                 tty3270_rebuild_update(tp);
535                 tty3270_update_status(tp);
536                 tty3270_set_timer(tp, 1);
537         }
538         spin_unlock_bh(&tp->view.lock);
539 }
540
541 /*
542  * Pass input line to tty.
543  */
544 static void
545 tty3270_read_tasklet(struct raw3270_request *rrq)
546 {
547         static char kreset_data = TW_KR;
548         struct tty3270 *tp;
549         char *input;
550         int len;
551
552         tp = (struct tty3270 *) rrq->view;
553         spin_lock_bh(&tp->view.lock);
554         /*
555          * Two AID keys are special: For 0x7d (enter) the input line
556          * has to be emitted to the tty and for 0x6d the screen
557          * needs to be redrawn.
558          */
559         input = 0;
560         len = 0;
561         if (tp->input->string[0] == 0x7d) {
562                 /* Enter: write input to tty. */
563                 input = tp->input->string + 6;
564                 len = tp->input->len - 6 - rrq->rescnt;
565                 if (tp->inattr != TF_INPUTN)
566                         tty3270_rcl_add(tp, input, len);
567                 if (tp->nr_up > 0) {
568                         tp->nr_up = 0;
569                         tty3270_rebuild_update(tp);
570                         tty3270_update_status(tp);
571                 }
572                 /* Clear input area. */
573                 tty3270_update_prompt(tp, 0, 0);
574                 tty3270_set_timer(tp, 1);
575         } else if (tp->input->string[0] == 0x6d) {
576                 /* Display has been cleared. Redraw. */
577                 tty3270_rebuild_update(tp);
578                 tp->update_flags = TTY_UPDATE_ALL;
579                 tty3270_set_timer(tp, 1);
580         }
581         spin_unlock_bh(&tp->view.lock);
582
583         /* Start keyboard reset command. */
584         raw3270_request_reset(tp->kreset);
585         raw3270_request_set_cmd(tp->kreset, TC_WRITE);
586         raw3270_request_add_data(tp->kreset, &kreset_data, 1);
587         raw3270_start(&tp->view, tp->kreset);
588
589         /* Emit input string. */
590         if (tp->tty) {
591                 while (len-- > 0)
592                         kbd_keycode(tp->kbd, *input++);
593                 /* Emit keycode for AID byte. */
594                 kbd_keycode(tp->kbd, 256 + tp->input->string[0]);
595         }
596
597         raw3270_request_reset(rrq);
598         xchg(&tp->read, rrq);
599         raw3270_put_view(&tp->view);
600 }
601
602 /*
603  * Read request completion callback.
604  */
605 static void
606 tty3270_read_callback(struct raw3270_request *rq, void *data)
607 {
608         raw3270_get_view(rq->view);
609         /* Schedule tasklet to pass input to tty. */
610         tasklet_schedule(&((struct tty3270 *) rq->view)->readlet);
611 }
612
613 /*
614  * Issue a read request. Call with device lock.
615  */
616 static void
617 tty3270_issue_read(struct tty3270 *tp, int lock)
618 {
619         struct raw3270_request *rrq;
620         int rc;
621
622         rrq = xchg(&tp->read, 0);
623         if (!rrq)
624                 /* Read already scheduled. */
625                 return;
626         rrq->callback = tty3270_read_callback;
627         rrq->callback_data = tp;
628         raw3270_request_set_cmd(rrq, TC_READMOD);
629         raw3270_request_set_data(rrq, tp->input->string, tp->input->len);
630         /* Issue the read modified request. */
631         if (lock) {
632                 rc = raw3270_start(&tp->view, rrq);
633         } else
634                 rc = raw3270_start_irq(&tp->view, rrq);
635         if (rc) {
636                 raw3270_request_reset(rrq);
637                 xchg(&tp->read, rrq);
638         }
639 }
640
641 /*
642  * Switch to the tty view.
643  */
644 static int
645 tty3270_activate(struct raw3270_view *view)
646 {
647         struct tty3270 *tp;
648         unsigned long flags;
649
650         tp = (struct tty3270 *) view;
651         spin_lock_irqsave(&tp->view.lock, flags);
652         tp->nr_up = 0;
653         tty3270_rebuild_update(tp);
654         tty3270_update_status(tp);
655         tp->update_flags = TTY_UPDATE_ALL;
656         tty3270_set_timer(tp, 1);
657         spin_unlock_irqrestore(&tp->view.lock, flags);
658         start_tty(tp->tty);
659         return 0;
660 }
661
662 static void
663 tty3270_deactivate(struct raw3270_view *view)
664 {
665         struct tty3270 *tp;
666
667         tp = (struct tty3270 *) view;
668         if (tp && tp->tty)
669                 stop_tty(tp->tty);
670 }
671
672 static int
673 tty3270_irq(struct tty3270 *tp, struct raw3270_request *rq, struct irb *irb)
674 {
675         /* Handle ATTN. Schedule tasklet to read aid. */
676         if (irb->scsw.dstat & DEV_STAT_ATTENTION) {
677                 if (!tp->throttle)
678                         tty3270_issue_read(tp, 0);
679                 else
680                         tp->attn = 1;
681         }
682
683         if (rq) {
684                 if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK)
685                         rq->rc = -EIO;
686                 else
687                         /* Normal end. Copy residual count. */
688                         rq->rescnt = irb->scsw.count;
689         }
690         return RAW3270_IO_DONE;
691 }
692
693 /*
694  * Allocate tty3270 structure.
695  */
696 static struct tty3270 *
697 tty3270_alloc_view(void)
698 {
699         struct tty3270 *tp;
700         int pages;
701
702         tp = kmalloc(sizeof(struct tty3270),GFP_KERNEL);
703         if (!tp)
704                 goto out_err;
705         memset(tp, 0, sizeof(struct tty3270));
706         tp->freemem_pages =
707                 kmalloc(sizeof(void *) * TTY3270_STRING_PAGES, GFP_KERNEL);
708         if (!tp->freemem_pages)
709                 goto out_tp;
710         INIT_LIST_HEAD(&tp->freemem);
711         for (pages = 0; pages < TTY3270_STRING_PAGES; pages++) {
712                 tp->freemem_pages[pages] = (void *)
713                         __get_free_pages(GFP_KERNEL|GFP_DMA, 0);
714                 if (!tp->freemem_pages[pages])
715                         goto out_pages;
716                 add_string_memory(&tp->freemem,
717                                   tp->freemem_pages[pages], PAGE_SIZE);
718         }
719         tp->write = raw3270_request_alloc(TTY3270_OUTPUT_BUFFER_SIZE);
720         if (!tp->write)
721                 goto out_pages;
722         tp->read = raw3270_request_alloc(0);
723         if (!tp->read)
724                 goto out_write;
725         tp->kreset = raw3270_request_alloc(1);
726         if (!tp->kreset)
727                 goto out_read;
728         tp->kbd = kbd_alloc();
729         if (!tp->kbd)
730                 goto out_reset;
731         return tp;
732
733 out_reset:
734         raw3270_request_free(tp->kreset);
735 out_read:
736         raw3270_request_free(tp->read);
737 out_write:
738         raw3270_request_free(tp->write);
739 out_pages:
740         while (pages--)
741                 free_pages((unsigned long) tp->freemem_pages[pages], 0);
742         kfree(tp->freemem_pages);
743 out_tp:
744         kfree(tp);
745 out_err:
746         return ERR_PTR(-ENOMEM);
747 }
748
749 /*
750  * Free tty3270 structure.
751  */
752 static void
753 tty3270_free_view(struct tty3270 *tp)
754 {
755         int pages;
756
757         kbd_free(tp->kbd);
758         raw3270_request_free(tp->kreset);
759         raw3270_request_free(tp->read);
760         raw3270_request_free(tp->write);
761         for (pages = 0; pages < TTY3270_STRING_PAGES; pages++)
762                 free_pages((unsigned long) tp->freemem_pages[pages], 0);
763         kfree(tp->freemem_pages);
764         kfree(tp);
765 }
766
767 /*
768  * Allocate tty3270 screen.
769  */
770 static int
771 tty3270_alloc_screen(struct tty3270 *tp)
772 {
773         unsigned long size;
774         int lines;
775
776         size = sizeof(struct tty3270_line) * (tp->view.rows - 2);
777         tp->screen = kmalloc(size, GFP_KERNEL);
778         if (!tp->screen)
779                 goto out_err;
780         memset(tp->screen, 0, size);
781         for (lines = 0; lines < tp->view.rows - 2; lines++) {
782                 size = sizeof(struct tty3270_cell) * tp->view.cols;
783                 tp->screen[lines].cells = kmalloc(size, GFP_KERNEL);
784                 if (!tp->screen[lines].cells)
785                         goto out_screen;
786                 memset(tp->screen[lines].cells, 0, size);
787         }
788         return 0;
789 out_screen:
790         while (lines--)
791                 kfree(tp->screen[lines].cells);
792         kfree(tp->screen);
793 out_err:
794         return -ENOMEM;
795 }
796
797 /*
798  * Free tty3270 screen.
799  */
800 static void
801 tty3270_free_screen(struct tty3270 *tp)
802 {
803         int lines;
804
805         for (lines = 0; lines < tp->view.rows - 2; lines++)
806                 kfree(tp->screen[lines].cells);
807         kfree(tp->screen);
808 }
809
810 /*
811  * Unlink tty3270 data structure from tty.
812  */
813 static void
814 tty3270_release(struct raw3270_view *view)
815 {
816         struct tty3270 *tp;
817         struct tty_struct *tty;
818
819         tp = (struct tty3270 *) view;
820         tty = tp->tty;
821         if (tty) {
822                 tty->driver_data = 0;
823                 tp->tty = tp->kbd->tty = 0;
824                 tty_hangup(tty);
825                 raw3270_put_view(&tp->view);
826         }
827 }
828
829 /*
830  * Free tty3270 data structure
831  */
832 static void
833 tty3270_free(struct raw3270_view *view)
834 {
835         tty3270_free_screen((struct tty3270 *) view);
836         tty3270_free_view((struct tty3270 *) view);
837 }
838
839 struct raw3270_fn tty3270_fn = {
840         .activate = tty3270_activate,
841         .deactivate = tty3270_deactivate,
842         .intv = (void *) tty3270_irq,
843         .release = tty3270_release,
844         .free = tty3270_free
845 };
846
847 /*
848  * This routine is called whenever a 3270 tty is opened.
849  */
850 static int
851 tty3270_open(struct tty_struct *tty, struct file * filp)
852 {
853         struct tty3270 *tp;
854         int i, rc;
855
856         if (tty->count > 1)
857                 return 0;
858         /* Check if the tty3270 is already there. */
859         tp = (struct tty3270 *) raw3270_find_view(&tty3270_fn, tty->index);
860         if (!IS_ERR(tp)) {
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                 tp->tty = tty;
866                 tp->kbd->tty = tty;
867                 tp->inattr = TF_INPUT;
868                 return 0;
869         }
870
871         /* Allocate tty3270 structure on first open. */
872         tp = tty3270_alloc_view();
873         if (IS_ERR(tp))
874                 return PTR_ERR(tp);
875
876         INIT_LIST_HEAD(&tp->lines);
877         INIT_LIST_HEAD(&tp->update);
878         INIT_LIST_HEAD(&tp->rcl_lines);
879         tp->rcl_max = 20;
880         init_timer(&tp->timer);
881         tasklet_init(&tp->readlet, 
882                      (void (*)(unsigned long)) tty3270_read_tasklet,
883                      (unsigned long) tp->read);
884
885         rc = raw3270_add_view(&tp->view, &tty3270_fn, tty->index);
886         if (rc) {
887                 tty3270_free_view(tp);
888                 return rc;
889         }
890
891         rc = tty3270_alloc_screen(tp);
892         if (rc) {
893                 raw3270_del_view(&tp->view);
894                 raw3270_put_view(&tp->view);
895                 return rc;
896         }
897
898         tp->tty = tty;
899         tty->low_latency = 0;
900         tty->driver_data = tp;
901         tty->winsize.ws_row = tp->view.rows - 2;
902         tty->winsize.ws_col = tp->view.cols;
903
904         tty3270_create_prompt(tp);
905         tty3270_create_status(tp);
906         tty3270_update_status(tp);
907
908         /* Create blank line for every line in the tty output area. */
909         for (i = 0; i < tp->view.rows - 2; i++)
910                 tty3270_blank_line(tp);
911
912         tp->kbd->tty = tty;
913         tp->kbd->fn_handler[KVAL(K_INCRCONSOLE)] = tty3270_exit_tty;
914         tp->kbd->fn_handler[KVAL(K_SCROLLBACK)] = tty3270_scroll_backward;
915         tp->kbd->fn_handler[KVAL(K_SCROLLFORW)] = tty3270_scroll_forward;
916         tp->kbd->fn_handler[KVAL(K_CONS)] = tty3270_rcl_backward;
917         kbd_ascebc(tp->kbd, tp->view.ascebc);
918
919         raw3270_activate_view(&tp->view);
920         return 0;
921 }
922
923 /*
924  * This routine is called when the 3270 tty is closed. We wait
925  * for the remaining request to be completed. Then we clean up.
926  */
927 static void
928 tty3270_close(struct tty_struct *tty, struct file * filp)
929 {
930         struct tty3270 *tp;
931
932         if (tty->count > 1)
933                 return;
934         tp = (struct tty3270 *) tty->driver_data;
935         if (tp) {
936                 tty->driver_data = 0;
937                 tp->tty = tp->kbd->tty = 0;
938                 raw3270_put_view(&tp->view);
939         }
940 }
941
942 /*
943  * We always have room.
944  */
945 static int
946 tty3270_write_room(struct tty_struct *tty)
947 {
948         return INT_MAX;
949 }
950
951 /*
952  * Insert character into the screen at the current position with the
953  * current color and highlight. This function does NOT do cursor movement.
954  */
955 static void
956 tty3270_put_character(struct tty3270 *tp, char ch)
957 {
958         struct tty3270_line *line;
959         struct tty3270_cell *cell;
960
961         line = tp->screen + tp->cy;
962         if (line->len <= tp->cx) {
963                 while (line->len < tp->cx) {
964                         cell = line->cells + line->len;
965                         cell->character = tp->view.ascebc[' '];
966                         cell->highlight = tp->highlight;
967                         cell->f_color = tp->f_color;
968                         line->len++;
969                 }
970                 line->len++;
971         }
972         cell = line->cells + tp->cx;
973         cell->character = tp->view.ascebc[(unsigned int) ch];
974         cell->highlight = tp->highlight;
975         cell->f_color = tp->f_color;
976 }
977
978 /*
979  * Convert a tty3270_line to a 3270 data fragment usable for output.
980  */
981 static void
982 tty3270_convert_line(struct tty3270 *tp, int line_nr)
983 {
984         struct tty3270_line *line;
985         struct tty3270_cell *cell;
986         struct string *s, *n;
987         unsigned char highlight;
988         unsigned char f_color;
989         char *cp;
990         int flen, i;
991
992         /* Determine how long the fragment will be. */
993         flen = 3;               /* Prefix (TO_SBA). */
994         line = tp->screen + line_nr;
995         flen += line->len;
996         highlight = TAX_RESET;
997         f_color = TAC_RESET;
998         for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
999                 if (cell->highlight != highlight) {
1000                         flen += 3;      /* TO_SA to switch highlight. */
1001                         highlight = cell->highlight;
1002                 }
1003                 if (cell->f_color != f_color) {
1004                         flen += 3;      /* TO_SA to switch color. */
1005                         f_color = cell->f_color;
1006                 }
1007         }
1008         if (highlight != TAX_RESET)
1009                 flen += 3;      /* TO_SA to reset hightlight. */
1010         if (f_color != TAC_RESET)
1011                 flen += 3;      /* TO_SA to reset color. */
1012         if (line->len < tp->view.cols)
1013                 flen += 4;      /* Postfix (TO_RA). */
1014
1015         /* Find the line in the list. */
1016         i = tp->view.rows - 2 - line_nr;
1017         list_for_each_entry_reverse(s, &tp->lines, list)
1018                 if (--i <= 0)
1019                         break;
1020         /*
1021          * Check if the line needs to get reallocated.
1022          */
1023         if (s->len != flen) {
1024                 /* Reallocate string. */
1025                 n = tty3270_alloc_string(tp, flen);
1026                 list_add(&n->list, &s->list);
1027                 list_del_init(&s->list);
1028                 if (!list_empty(&s->update))
1029                         list_del_init(&s->update);
1030                 free_string(&tp->freemem, s);
1031                 s = n;
1032         }
1033
1034         /* Write 3270 data fragment. */
1035         cp = s->string;
1036         *cp++ = TO_SBA;
1037         *cp++ = 0;
1038         *cp++ = 0;
1039
1040         highlight = TAX_RESET;
1041         f_color = TAC_RESET;
1042         for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
1043                 if (cell->highlight != highlight) {
1044                         *cp++ = TO_SA;
1045                         *cp++ = TAT_EXTHI;
1046                         *cp++ = cell->highlight;
1047                         highlight = cell->highlight;
1048                 }
1049                 if (cell->f_color != f_color) {
1050                         *cp++ = TO_SA;
1051                         *cp++ = TAT_COLOR;
1052                         *cp++ = cell->f_color;
1053                         f_color = cell->f_color;
1054                 }
1055                 *cp++ = cell->character;
1056         }
1057         if (highlight != TAX_RESET) {
1058                 *cp++ = TO_SA;
1059                 *cp++ = TAT_EXTHI;
1060                 *cp++ = TAX_RESET;
1061         }
1062         if (f_color != TAC_RESET) {
1063                 *cp++ = TO_SA;
1064                 *cp++ = TAT_COLOR;
1065                 *cp++ = TAC_RESET;
1066         }
1067         if (line->len < tp->view.cols) {
1068                 *cp++ = TO_RA;
1069                 *cp++ = 0;
1070                 *cp++ = 0;
1071                 *cp++ = 0;
1072         }
1073
1074         if (tp->nr_up + line_nr < tp->view.rows - 2) {
1075                 /* Line is currently visible on screen. */
1076                 tty3270_update_string(tp, s, line_nr);
1077                 /* Add line to update list. */
1078                 if (list_empty(&s->update)) {
1079                         list_add_tail(&s->update, &tp->update);
1080                         tp->update_flags |= TTY_UPDATE_LIST;
1081                 }
1082         }
1083 }
1084
1085 /*
1086  * Do carriage return.
1087  */
1088 static void
1089 tty3270_cr(struct tty3270 *tp)
1090 {
1091         tp->cx = 0;
1092 }
1093
1094 /*
1095  * Do line feed.
1096  */
1097 static void
1098 tty3270_lf(struct tty3270 *tp)
1099 {
1100         struct tty3270_line temp;
1101         int i;
1102
1103         tty3270_convert_line(tp, tp->cy);
1104         if (tp->cy < tp->view.rows - 3) {
1105                 tp->cy++;
1106                 return;
1107         }
1108         /* Last line just filled up. Add new, blank line. */
1109         tty3270_blank_line(tp);
1110         temp = tp->screen[0];
1111         temp.len = 0;
1112         for (i = 0; i < tp->view.rows - 3; i++)
1113                 tp->screen[i] = tp->screen[i+1];
1114         tp->screen[tp->view.rows - 3] = temp;
1115         tty3270_rebuild_update(tp);
1116 }
1117
1118 static void
1119 tty3270_ri(struct tty3270 *tp)
1120 {
1121         if (tp->cy > 0) {
1122             tty3270_convert_line(tp, tp->cy);
1123             tp->cy--;
1124         }
1125 }
1126
1127 /*
1128  * Insert characters at current position.
1129  */
1130 static void
1131 tty3270_insert_characters(struct tty3270 *tp, int n)
1132 {
1133         struct tty3270_line *line;
1134         int k;
1135
1136         line = tp->screen + tp->cy;
1137         while (line->len < tp->cx) {
1138                 line->cells[line->len].character = tp->view.ascebc[' '];
1139                 line->cells[line->len].highlight = TAX_RESET;
1140                 line->cells[line->len].f_color = TAC_RESET;
1141                 line->len++;
1142         }
1143         if (n > tp->view.cols - tp->cx)
1144                 n = tp->view.cols - tp->cx;
1145         k = min_t(int, line->len - tp->cx, tp->view.cols - tp->cx - n);
1146         while (k--)
1147                 line->cells[tp->cx + n + k] = line->cells[tp->cx + k];
1148         line->len += n;
1149         if (line->len > tp->view.cols)
1150                 line->len = tp->view.cols;
1151         while (n-- > 0) {
1152                 line->cells[tp->cx + n].character = tp->view.ascebc[' '];
1153                 line->cells[tp->cx + n].highlight = tp->highlight;
1154                 line->cells[tp->cx + n].f_color = tp->f_color;
1155         }
1156 }
1157
1158 /*
1159  * Delete characters at current position.
1160  */
1161 static void
1162 tty3270_delete_characters(struct tty3270 *tp, int n)
1163 {
1164         struct tty3270_line *line;
1165         int i;
1166
1167         line = tp->screen + tp->cy;
1168         if (line->len <= tp->cx)
1169                 return;
1170         if (line->len - tp->cx <= n) {
1171                 line->len = tp->cx;
1172                 return;
1173         }
1174         for (i = tp->cx; i + n < line->len; i++)
1175                 line->cells[i] = line->cells[i + n];
1176         line->len -= n;
1177 }
1178
1179 /*
1180  * Erase characters at current position.
1181  */
1182 static void
1183 tty3270_erase_characters(struct tty3270 *tp, int n)
1184 {
1185         struct tty3270_line *line;
1186         struct tty3270_cell *cell;
1187
1188         line = tp->screen + tp->cy;
1189         while (line->len > tp->cx && n-- > 0) {
1190                 cell = line->cells + tp->cx++;
1191                 cell->character = ' ';
1192                 cell->highlight = TAX_RESET;
1193                 cell->f_color = TAC_RESET;
1194         }
1195         tp->cx += n;
1196         tp->cx = min_t(int, tp->cx, tp->view.cols - 1);
1197 }
1198
1199 /*
1200  * Erase line, 3 different cases:
1201  *  Esc [ 0 K   Erase from current position to end of line inclusive
1202  *  Esc [ 1 K   Erase from beginning of line to current position inclusive
1203  *  Esc [ 2 K   Erase entire line (without moving cursor)
1204  */
1205 static void
1206 tty3270_erase_line(struct tty3270 *tp, int mode)
1207 {
1208         struct tty3270_line *line;
1209         struct tty3270_cell *cell;
1210         int i;
1211
1212         line = tp->screen + tp->cy;
1213         if (mode == 0)
1214                 line->len = tp->cx;
1215         else if (mode == 1) {
1216                 for (i = 0; i < tp->cx; i++) {
1217                         cell = line->cells + i;
1218                         cell->character = ' ';
1219                         cell->highlight = TAX_RESET;
1220                         cell->f_color = TAC_RESET;
1221                 }
1222                 if (line->len <= tp->cx)
1223                         line->len = tp->cx + 1;
1224         } else if (mode == 2)
1225                 line->len = 0;
1226         tty3270_convert_line(tp, tp->cy);
1227 }
1228
1229 /*
1230  * Erase display, 3 different cases:
1231  *  Esc [ 0 J   Erase from current position to bottom of screen inclusive
1232  *  Esc [ 1 J   Erase from top of screen to current position inclusive
1233  *  Esc [ 2 J   Erase entire screen (without moving the cursor)
1234  */
1235 static void
1236 tty3270_erase_display(struct tty3270 *tp, int mode)
1237 {
1238         int i;
1239
1240         if (mode == 0) {
1241                 tty3270_erase_line(tp, 0);
1242                 for (i = tp->cy + 1; i < tp->view.rows - 2; i++) {
1243                         tp->screen[i].len = 0;
1244                         tty3270_convert_line(tp, i);
1245                 }
1246         } else if (mode == 1) {
1247                 for (i = 0; i < tp->cy; i++) {
1248                         tp->screen[i].len = 0;
1249                         tty3270_convert_line(tp, i);
1250                 }
1251                 tty3270_erase_line(tp, 1);
1252         } else if (mode == 2) {
1253                 for (i = 0; i < tp->view.rows - 2; i++) {
1254                         tp->screen[i].len = 0;
1255                         tty3270_convert_line(tp, i);
1256                 }
1257         }
1258         tty3270_rebuild_update(tp);
1259 }
1260
1261 /*
1262  * Set attributes found in an escape sequence.
1263  *  Esc [ <attr> ; <attr> ; ... m
1264  */
1265 static void
1266 tty3270_set_attributes(struct tty3270 *tp)
1267 {
1268         static unsigned char f_colors[] = {
1269                 TAC_DEFAULT, TAC_RED, TAC_GREEN, TAC_YELLOW, TAC_BLUE,
1270                 TAC_PINK, TAC_TURQ, TAC_WHITE, 0, TAC_DEFAULT
1271         };
1272         int i, attr;
1273
1274         for (i = 0; i <= tp->esc_npar; i++) {
1275                 attr = tp->esc_par[i];
1276                 switch (attr) {
1277                 case 0:         /* Reset */
1278                         tp->highlight = TAX_RESET;
1279                         tp->f_color = TAC_RESET;
1280                         break;
1281                 /* Highlight. */
1282                 case 4:         /* Start underlining. */
1283                         tp->highlight = TAX_UNDER;
1284                         break;
1285                 case 5:         /* Start blink. */
1286                         tp->highlight = TAX_BLINK;
1287                         break;
1288                 case 7:         /* Start reverse. */
1289                         tp->highlight = TAX_REVER;
1290                         break;
1291                 case 24:        /* End underlining */
1292                         if (tp->highlight == TAX_UNDER)
1293                                 tp->highlight = TAX_RESET;
1294                         break;
1295                 case 25:        /* End blink. */
1296                         if (tp->highlight == TAX_BLINK)
1297                                 tp->highlight = TAX_RESET;
1298                         break;
1299                 case 27:        /* End reverse. */
1300                         if (tp->highlight == TAX_REVER)
1301                                 tp->highlight = TAX_RESET;
1302                         break;
1303                 /* Foreground color. */
1304                 case 30:        /* Black */
1305                 case 31:        /* Red */
1306                 case 32:        /* Green */
1307                 case 33:        /* Yellow */
1308                 case 34:        /* Blue */
1309                 case 35:        /* Magenta */
1310                 case 36:        /* Cyan */
1311                 case 37:        /* White */
1312                 case 39:        /* Black */
1313                         tp->f_color = f_colors[attr - 30];
1314                         break;
1315                 }
1316         }
1317 }
1318
1319 static inline int
1320 tty3270_getpar(struct tty3270 *tp, int ix)
1321 {
1322         return (tp->esc_par[ix] > 0) ? tp->esc_par[ix] : 1;
1323 }
1324
1325 static void
1326 tty3270_goto_xy(struct tty3270 *tp, int cx, int cy)
1327 {
1328         tp->cx = min_t(int, tp->view.cols - 1, max_t(int, 0, cx));
1329         cy = min_t(int, tp->view.rows - 3, max_t(int, 0, cy));
1330         if (cy != tp->cy) {
1331                 tty3270_convert_line(tp, tp->cy);
1332                 tp->cy = cy;
1333         }
1334 }
1335
1336 /*
1337  * Process escape sequences. Known sequences:
1338  *  Esc 7                       Save Cursor Position
1339  *  Esc 8                       Restore Cursor Position
1340  *  Esc [ Pn ; Pn ; .. m        Set attributes
1341  *  Esc [ Pn ; Pn H             Cursor Position
1342  *  Esc [ Pn ; Pn f             Cursor Position
1343  *  Esc [ Pn A                  Cursor Up
1344  *  Esc [ Pn B                  Cursor Down
1345  *  Esc [ Pn C                  Cursor Forward
1346  *  Esc [ Pn D                  Cursor Backward
1347  *  Esc [ Pn G                  Cursor Horizontal Absolute
1348  *  Esc [ Pn X                  Erase Characters
1349  *  Esc [ Ps J                  Erase in Display
1350  *  Esc [ Ps K                  Erase in Line
1351  * // FIXME: add all the new ones.
1352  *
1353  *  Pn is a numeric parameter, a string of zero or more decimal digits.
1354  *  Ps is a selective parameter.
1355  */
1356 static void
1357 tty3270_escape_sequence(struct tty3270 *tp, char ch)
1358 {
1359         enum { ESnormal, ESesc, ESsquare, ESgetpars };
1360
1361         if (tp->esc_state == ESnormal) {
1362                 if (ch == 0x1b)
1363                         /* Starting new escape sequence. */
1364                         tp->esc_state = ESesc;
1365                 return;
1366         }
1367         if (tp->esc_state == ESesc) {
1368                 tp->esc_state = ESnormal;
1369                 switch (ch) {
1370                 case '[':
1371                         tp->esc_state = ESsquare;
1372                         break;
1373                 case 'E':
1374                         tty3270_cr(tp);
1375                         tty3270_lf(tp);
1376                         break;
1377                 case 'M':
1378                         tty3270_ri(tp);
1379                         break;
1380                 case 'D':
1381                         tty3270_lf(tp);
1382                         break;
1383                 case 'Z':               /* Respond ID. */
1384                         kbd_puts_queue(tp->tty, "\033[?6c");
1385                         break;
1386                 case '7':               /* Save cursor position. */
1387                         tp->saved_cx = tp->cx;
1388                         tp->saved_cy = tp->cy;
1389                         tp->saved_highlight = tp->highlight;
1390                         tp->saved_f_color = tp->f_color;
1391                         break;
1392                 case '8':               /* Restore cursor position. */
1393                         tty3270_convert_line(tp, tp->cy);
1394                         tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
1395                         tp->highlight = tp->saved_highlight;
1396                         tp->f_color = tp->saved_f_color;
1397                         break;
1398                 case 'c':               /* Reset terminal. */
1399                         tp->cx = tp->saved_cx = 0;
1400                         tp->cy = tp->saved_cy = 0;
1401                         tp->highlight = tp->saved_highlight = TAX_RESET;
1402                         tp->f_color = tp->saved_f_color = TAC_RESET;
1403                         tty3270_erase_display(tp, 2);
1404                         break;
1405                 }
1406                 return;
1407         }
1408         if (tp->esc_state == ESsquare) {
1409                 tp->esc_state = ESgetpars;
1410                 memset(tp->esc_par, 0, sizeof(tp->esc_par));
1411                 tp->esc_npar = 0;
1412                 tp->esc_ques = (ch == '?');
1413                 if (tp->esc_ques)
1414                         return;
1415         }
1416         if (tp->esc_state == ESgetpars) {
1417                 if (ch == ';' && tp->esc_npar < ESCAPE_NPAR - 1) {
1418                         tp->esc_npar++;
1419                         return;
1420                 }
1421                 if (ch >= '0' && ch <= '9') {
1422                         tp->esc_par[tp->esc_npar] *= 10;
1423                         tp->esc_par[tp->esc_npar] += ch - '0';
1424                         return;
1425                 }
1426         }
1427         tp->esc_state = ESnormal;
1428         if (ch == 'n' && !tp->esc_ques) {
1429                 if (tp->esc_par[0] == 5)                /* Status report. */
1430                         kbd_puts_queue(tp->tty, "\033[0n");
1431                 else if (tp->esc_par[0] == 6) { /* Cursor report. */
1432                         char buf[40];
1433                         sprintf(buf, "\033[%d;%dR", tp->cy + 1, tp->cx + 1);
1434                         kbd_puts_queue(tp->tty, buf);
1435                 }
1436                 return;
1437         }
1438         if (tp->esc_ques)
1439                 return;
1440         switch (ch) {
1441         case 'm':
1442                 tty3270_set_attributes(tp);
1443                 break;
1444         case 'H':       /* Set cursor position. */
1445         case 'f':
1446                 tty3270_goto_xy(tp, tty3270_getpar(tp, 1) - 1,
1447                                 tty3270_getpar(tp, 0) - 1);
1448                 break;
1449         case 'd':       /* Set y position. */
1450                 tty3270_goto_xy(tp, tp->cx, tty3270_getpar(tp, 0) - 1);
1451                 break;
1452         case 'A':       /* Cursor up. */
1453         case 'F':
1454                 tty3270_goto_xy(tp, tp->cx, tp->cy - tty3270_getpar(tp, 0));
1455                 break;
1456         case 'B':       /* Cursor down. */
1457         case 'e':
1458         case 'E':
1459                 tty3270_goto_xy(tp, tp->cx, tp->cy + tty3270_getpar(tp, 0));
1460                 break;
1461         case 'C':       /* Cursor forward. */
1462         case 'a':
1463                 tty3270_goto_xy(tp, tp->cx + tty3270_getpar(tp, 0), tp->cy);
1464                 break;
1465         case 'D':       /* Cursor backward. */
1466                 tty3270_goto_xy(tp, tp->cx - tty3270_getpar(tp, 0), tp->cy);
1467                 break;
1468         case 'G':       /* Set x position. */
1469         case '`':
1470                 tty3270_goto_xy(tp, tty3270_getpar(tp, 0), tp->cy);
1471                 break;
1472         case 'X':       /* Erase Characters. */
1473                 tty3270_erase_characters(tp, tty3270_getpar(tp, 0));
1474                 break;
1475         case 'J':       /* Erase display. */
1476                 tty3270_erase_display(tp, tp->esc_par[0]);
1477                 break;
1478         case 'K':       /* Erase line. */
1479                 tty3270_erase_line(tp, tp->esc_par[0]);
1480                 break;
1481         case 'P':       /* Delete characters. */
1482                 tty3270_delete_characters(tp, tty3270_getpar(tp, 0));
1483                 break;
1484         case '@':       /* Insert characters. */
1485                 tty3270_insert_characters(tp, tty3270_getpar(tp, 0));
1486                 break;
1487         case 's':       /* Save cursor position. */
1488                 tp->saved_cx = tp->cx;
1489                 tp->saved_cy = tp->cy;
1490                 tp->saved_highlight = tp->highlight;
1491                 tp->saved_f_color = tp->f_color;
1492                 break;
1493         case 'u':       /* Restore cursor position. */
1494                 tty3270_convert_line(tp, tp->cy);
1495                 tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
1496                 tp->highlight = tp->saved_highlight;
1497                 tp->f_color = tp->saved_f_color;
1498                 break;
1499         }
1500 }
1501
1502 /*
1503  * String write routine for 3270 ttys
1504  */
1505 static void
1506 tty3270_do_write(struct tty3270 *tp, const unsigned char *buf, int count)
1507 {
1508         int i_msg, i;
1509
1510         spin_lock_bh(&tp->view.lock);
1511         for (i_msg = 0; !tp->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]);
1515                         continue;
1516                 }
1517
1518                 switch (buf[i_msg]) {
1519                 case 0x07:              /* '\a' -- Alarm */
1520                         tp->wcc |= TW_PLUSALARM;
1521                         break;
1522                 case 0x08:              /* Backspace. */
1523                         if (tp->cx > 0) {
1524                                 tp->cx--;
1525                                 tty3270_put_character(tp, ' ');
1526                         }
1527                         break;
1528                 case 0x09:              /* '\t' -- Tabulate */
1529                         for (i = tp->cx % 8; i < 8; i++) {
1530                                 if (tp->cx >= tp->view.cols) {
1531                                         tty3270_cr(tp);
1532                                         tty3270_lf(tp);
1533                                         break;
1534                                 }
1535                                 tty3270_put_character(tp, ' ');
1536                                 tp->cx++;
1537                         }
1538                         break;
1539                 case 0x0a:              /* '\n' -- New Line */
1540                         tty3270_cr(tp);
1541                         tty3270_lf(tp);
1542                         break;
1543                 case 0x0c:              /* '\f' -- Form Feed */
1544                         tty3270_erase_display(tp, 2);
1545                         tp->cx = tp->cy = 0;
1546                         break;
1547                 case 0x0d:              /* '\r' -- Carriage Return */
1548                         tp->cx = 0;
1549                         break;
1550                 case 0x0f:              /* SuSE "exit alternate mode" */
1551                         break;
1552                 case 0x1b:              /* Start escape sequence. */
1553                         tty3270_escape_sequence(tp, buf[i_msg]);
1554                         break;
1555                 default:                /* Insert normal character. */
1556                         if (tp->cx >= tp->view.cols) {
1557                                 tty3270_cr(tp);
1558                                 tty3270_lf(tp);
1559                         }
1560                         tty3270_put_character(tp, buf[i_msg]);
1561                         tp->cx++;
1562                         break;
1563                 }
1564         }
1565         /* Convert current line to 3270 data fragment. */
1566         tty3270_convert_line(tp, tp->cy);
1567
1568         /* Setup timer to update display after 1/10 second */
1569         if (!timer_pending(&tp->timer))
1570                 tty3270_set_timer(tp, HZ/10);
1571
1572         spin_unlock_bh(&tp->view.lock);
1573 }
1574
1575 /*
1576  * String write routine for 3270 ttys
1577  */
1578 static int
1579 tty3270_write(struct tty_struct * tty, int from_user,
1580               const unsigned char *buf, int count)
1581 {
1582         struct tty3270 *tp;
1583         int length, ret;
1584
1585         tp = tty->driver_data;
1586         if (!tp)
1587                 return 0;
1588         if (tp->char_count > 0) {
1589                 tty3270_do_write(tp, tp->char_buf, tp->char_count);
1590                 tp->char_count = 0;
1591         }
1592         if (!from_user) {
1593                 tty3270_do_write(tp, buf, count);
1594                 return count;
1595         }
1596         ret = 0;
1597         while (count > 0) {
1598                 length = count < TTY3270_CHAR_BUF_SIZE ?
1599                         count : TTY3270_CHAR_BUF_SIZE;
1600                 length -= copy_from_user(tp->char_buf, buf, length);
1601                 if (length == 0) {
1602                         if (!ret)
1603                                 ret = -EFAULT;
1604                         break;
1605                 }
1606                 tty3270_do_write(tp, tp->char_buf, count);
1607                 buf += length;
1608                 count -= length;
1609                 ret += length;
1610         }
1611         return ret;
1612 }
1613
1614 /*
1615  * Put single characters to the ttys character buffer
1616  */
1617 static void
1618 tty3270_put_char(struct tty_struct *tty, unsigned char ch)
1619 {
1620         struct tty3270 *tp;
1621
1622         tp = tty->driver_data;
1623         if (!tp)
1624                 return;
1625         if (tp->char_count < TTY3270_CHAR_BUF_SIZE)
1626                 tp->char_buf[tp->char_count++] = ch;
1627 }
1628
1629 /*
1630  * Flush all characters from the ttys characeter buffer put there
1631  * by tty3270_put_char.
1632  */
1633 static void
1634 tty3270_flush_chars(struct tty_struct *tty)
1635 {
1636         struct tty3270 *tp;
1637
1638         tp = tty->driver_data;
1639         if (!tp)
1640                 return;
1641         if (tp->char_count > 0) {
1642                 tty3270_do_write(tp, tp->char_buf, tp->char_count);
1643                 tp->char_count = 0;
1644         }
1645 }
1646
1647 /*
1648  * Returns the number of characters in the output buffer. This is
1649  * used in tty_wait_until_sent to wait until all characters have
1650  * appeared on the screen.
1651  */
1652 static int
1653 tty3270_chars_in_buffer(struct tty_struct *tty)
1654 {
1655         return 0;
1656 }
1657
1658 static void
1659 tty3270_flush_buffer(struct tty_struct *tty)
1660 {
1661 }
1662
1663 /*
1664  * Check for visible/invisible input switches
1665  */
1666 static void
1667 tty3270_set_termios(struct tty_struct *tty, struct termios *old)
1668 {
1669         struct tty3270 *tp;
1670         int new;
1671
1672         tp = tty->driver_data;
1673         if (!tp)
1674                 return;
1675         spin_lock_bh(&tp->view.lock);
1676         if (L_ICANON(tty)) {
1677                 new = L_ECHO(tty) ? TF_INPUT: TF_INPUTN;
1678                 if (new != tp->inattr) {
1679                         tp->inattr = new;
1680                         tty3270_update_prompt(tp, 0, 0);
1681                         tty3270_set_timer(tp, 1);
1682                 }
1683         }
1684         spin_unlock_bh(&tp->view.lock);
1685 }
1686
1687 /*
1688  * Disable reading from a 3270 tty
1689  */
1690 static void
1691 tty3270_throttle(struct tty_struct * tty)
1692 {
1693         struct tty3270 *tp;
1694
1695         tp = tty->driver_data;
1696         if (!tp)
1697                 return;
1698         tp->throttle = 1;
1699 }
1700
1701 /*
1702  * Enable reading from a 3270 tty
1703  */
1704 static void
1705 tty3270_unthrottle(struct tty_struct * tty)
1706 {
1707         struct tty3270 *tp;
1708
1709         tp = tty->driver_data;
1710         if (!tp)
1711                 return;
1712         tp->throttle = 0;
1713         if (tp->attn)
1714                 tty3270_issue_read(tp, 1);
1715 }
1716
1717 /*
1718  * Hang up the tty device.
1719  */
1720 static void
1721 tty3270_hangup(struct tty_struct *tty)
1722 {
1723         // FIXME: implement
1724 }
1725
1726 static void
1727 tty3270_wait_until_sent(struct tty_struct *tty, int timeout)
1728 {
1729 }
1730
1731 static int
1732 tty3270_ioctl(struct tty_struct *tty, struct file *file,
1733               unsigned int cmd, unsigned long arg)
1734 {
1735         struct tty3270 *tp;
1736
1737         tp = tty->driver_data;
1738         if (!tp)
1739                 return -ENODEV;
1740         if (tty->flags & (1 << TTY_IO_ERROR))
1741                 return -EIO;
1742         return kbd_ioctl(tp->kbd, file, cmd, arg);
1743 }
1744
1745 static struct tty_operations tty3270_ops = {
1746         .open = tty3270_open,
1747         .close = tty3270_close,
1748         .write = tty3270_write,
1749         .put_char = tty3270_put_char,
1750         .flush_chars = tty3270_flush_chars,
1751         .write_room = tty3270_write_room,
1752         .chars_in_buffer = tty3270_chars_in_buffer,
1753         .flush_buffer = tty3270_flush_buffer,
1754         .throttle = tty3270_throttle,
1755         .unthrottle = tty3270_unthrottle,
1756         .hangup = tty3270_hangup,
1757         .wait_until_sent = tty3270_wait_until_sent,
1758         .ioctl = tty3270_ioctl,
1759         .set_termios = tty3270_set_termios
1760 };
1761
1762 void
1763 tty3270_notifier(int index, int active)
1764 {
1765         if (active)
1766                 tty_register_device(tty3270_driver, index, 0);
1767         else
1768                 tty_unregister_device(tty3270_driver, index);
1769 }
1770
1771 /*
1772  * 3270 tty registration code called from tty_init().
1773  * Most kernel services (incl. kmalloc) are available at this poimt.
1774  */
1775 int __init
1776 tty3270_init(void)
1777 {
1778         struct tty_driver *driver;
1779         int ret;
1780
1781         ret = raw3270_init();
1782         if (ret)
1783                 return ret;
1784         driver = alloc_tty_driver(256);
1785         if (!driver)
1786                 return -ENOMEM;
1787
1788         /*
1789          * Initialize the tty_driver structure
1790          * Entries in tty3270_driver that are NOT initialized:
1791          * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1792          */
1793         driver->owner = THIS_MODULE;
1794         driver->devfs_name = "ttyTUB/";
1795         driver->driver_name = "ttyTUB";
1796         driver->name = "ttyTUB";
1797         driver->major = IBM_TTY3270_MAJOR;
1798         driver->type = TTY_DRIVER_TYPE_SYSTEM;
1799         driver->subtype = SYSTEM_TYPE_TTY;
1800         driver->init_termios = tty_std_termios;
1801         driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_NO_DEVFS;
1802         tty_set_operations(driver, &tty3270_ops);
1803         ret = tty_register_driver(driver);
1804         if (ret) {
1805                 printk(KERN_ERR "tty3270 registration failed with %d\n", ret);
1806                 put_tty_driver(driver);
1807                 return ret;
1808         }
1809         tty3270_driver = driver;
1810         return 0;
1811 }
1812
1813 static void __exit
1814 tty3270_exit(void)
1815 {
1816         struct tty_driver *driver;
1817
1818         driver = tty3270_driver;
1819         tty3270_driver = 0;
1820         tty_unregister_driver(driver);
1821         raw3270_exit();
1822 }
1823
1824 MODULE_LICENSE("GPL");
1825 MODULE_ALIAS_CHARDEV_MAJOR(IBM_TTY3270_MAJOR);
1826
1827 module_init(tty3270_init);
1828 module_exit(tty3270_exit);