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