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