upgrade to linux 2.6.10-1.12_FC2
[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 (del_timer(&tp->timer))
128                         raw3270_put_view(&tp->view);
129                 return;
130         }
131         if (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         start_tty(tp->tty);
656         return 0;
657 }
658
659 static void
660 tty3270_deactivate(struct raw3270_view *view)
661 {
662         struct tty3270 *tp;
663
664         tp = (struct tty3270 *) view;
665         if (tp && tp->tty)
666                 stop_tty(tp->tty);
667 }
668
669 static int
670 tty3270_irq(struct tty3270 *tp, struct raw3270_request *rq, struct irb *irb)
671 {
672         /* Handle ATTN. Schedule tasklet to read aid. */
673         if (irb->scsw.dstat & DEV_STAT_ATTENTION) {
674                 if (!tp->throttle)
675                         tty3270_issue_read(tp, 0);
676                 else
677                         tp->attn = 1;
678         }
679
680         if (rq) {
681                 if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK)
682                         rq->rc = -EIO;
683                 else
684                         /* Normal end. Copy residual count. */
685                         rq->rescnt = irb->scsw.count;
686         }
687         return RAW3270_IO_DONE;
688 }
689
690 /*
691  * Allocate tty3270 structure.
692  */
693 static struct tty3270 *
694 tty3270_alloc_view(void)
695 {
696         struct tty3270 *tp;
697         int pages;
698
699         tp = kmalloc(sizeof(struct tty3270),GFP_KERNEL);
700         if (!tp)
701                 goto out_err;
702         memset(tp, 0, sizeof(struct tty3270));
703         tp->freemem_pages =
704                 kmalloc(sizeof(void *) * TTY3270_STRING_PAGES, GFP_KERNEL);
705         if (!tp->freemem_pages)
706                 goto out_tp;
707         INIT_LIST_HEAD(&tp->freemem);
708         for (pages = 0; pages < TTY3270_STRING_PAGES; pages++) {
709                 tp->freemem_pages[pages] = (void *)
710                         __get_free_pages(GFP_KERNEL|GFP_DMA, 0);
711                 if (!tp->freemem_pages[pages])
712                         goto out_pages;
713                 add_string_memory(&tp->freemem,
714                                   tp->freemem_pages[pages], PAGE_SIZE);
715         }
716         tp->write = raw3270_request_alloc(TTY3270_OUTPUT_BUFFER_SIZE);
717         if (!tp->write)
718                 goto out_pages;
719         tp->read = raw3270_request_alloc(0);
720         if (!tp->read)
721                 goto out_write;
722         tp->kreset = raw3270_request_alloc(1);
723         if (!tp->kreset)
724                 goto out_read;
725         tp->kbd = kbd_alloc();
726         if (!tp->kbd)
727                 goto out_reset;
728         return tp;
729
730 out_reset:
731         raw3270_request_free(tp->kreset);
732 out_read:
733         raw3270_request_free(tp->read);
734 out_write:
735         raw3270_request_free(tp->write);
736 out_pages:
737         while (pages--)
738                 free_pages((unsigned long) tp->freemem_pages[pages], 0);
739         kfree(tp->freemem_pages);
740 out_tp:
741         kfree(tp);
742 out_err:
743         return ERR_PTR(-ENOMEM);
744 }
745
746 /*
747  * Free tty3270 structure.
748  */
749 static void
750 tty3270_free_view(struct tty3270 *tp)
751 {
752         int pages;
753
754         kbd_free(tp->kbd);
755         raw3270_request_free(tp->kreset);
756         raw3270_request_free(tp->read);
757         raw3270_request_free(tp->write);
758         for (pages = 0; pages < TTY3270_STRING_PAGES; pages++)
759                 free_pages((unsigned long) tp->freemem_pages[pages], 0);
760         kfree(tp->freemem_pages);
761         kfree(tp);
762 }
763
764 /*
765  * Allocate tty3270 screen.
766  */
767 static int
768 tty3270_alloc_screen(struct tty3270 *tp)
769 {
770         unsigned long size;
771         int lines;
772
773         size = sizeof(struct tty3270_line) * (tp->view.rows - 2);
774         tp->screen = kmalloc(size, GFP_KERNEL);
775         if (!tp->screen)
776                 goto out_err;
777         memset(tp->screen, 0, size);
778         for (lines = 0; lines < tp->view.rows - 2; lines++) {
779                 size = sizeof(struct tty3270_cell) * tp->view.cols;
780                 tp->screen[lines].cells = kmalloc(size, GFP_KERNEL);
781                 if (!tp->screen[lines].cells)
782                         goto out_screen;
783                 memset(tp->screen[lines].cells, 0, size);
784         }
785         return 0;
786 out_screen:
787         while (lines--)
788                 kfree(tp->screen[lines].cells);
789         kfree(tp->screen);
790 out_err:
791         return -ENOMEM;
792 }
793
794 /*
795  * Free tty3270 screen.
796  */
797 static void
798 tty3270_free_screen(struct tty3270 *tp)
799 {
800         int lines;
801
802         for (lines = 0; lines < tp->view.rows - 2; lines++)
803                 kfree(tp->screen[lines].cells);
804         kfree(tp->screen);
805 }
806
807 /*
808  * Unlink tty3270 data structure from tty.
809  */
810 static void
811 tty3270_release(struct raw3270_view *view)
812 {
813         struct tty3270 *tp;
814         struct tty_struct *tty;
815
816         tp = (struct tty3270 *) view;
817         tty = tp->tty;
818         if (tty) {
819                 tty->driver_data = 0;
820                 tp->tty = tp->kbd->tty = 0;
821                 tty_hangup(tty);
822                 raw3270_put_view(&tp->view);
823         }
824 }
825
826 /*
827  * Free tty3270 data structure
828  */
829 static void
830 tty3270_free(struct raw3270_view *view)
831 {
832         tty3270_free_screen((struct tty3270 *) view);
833         tty3270_free_view((struct tty3270 *) view);
834 }
835
836 /*
837  * Delayed freeing of tty3270 views.
838  */
839 static void
840 tty3270_del_views(void)
841 {
842         struct tty3270 *tp;
843         int i;
844
845         for (i = 0; i < tty3270_max_index; i++) {
846                 tp = (struct tty3270 *) raw3270_find_view(&tty3270_fn, i);
847                 if (!IS_ERR(tp))
848                         raw3270_del_view(&tp->view);
849         }
850 }
851
852 struct raw3270_fn tty3270_fn = {
853         .activate = tty3270_activate,
854         .deactivate = tty3270_deactivate,
855         .intv = (void *) tty3270_irq,
856         .release = tty3270_release,
857         .free = tty3270_free
858 };
859
860 /*
861  * This routine is called whenever a 3270 tty is opened.
862  */
863 static int
864 tty3270_open(struct tty_struct *tty, struct file * filp)
865 {
866         struct tty3270 *tp;
867         int i, rc;
868
869         if (tty->count > 1)
870                 return 0;
871         /* Check if the tty3270 is already there. */
872         tp = (struct tty3270 *) raw3270_find_view(&tty3270_fn, tty->index);
873         if (!IS_ERR(tp)) {
874                 tty->driver_data = tp;
875                 tty->winsize.ws_row = tp->view.rows - 2;
876                 tty->winsize.ws_col = tp->view.cols;
877                 tty->low_latency = 0;
878                 tp->tty = tty;
879                 tp->kbd->tty = tty;
880                 tp->inattr = TF_INPUT;
881                 return 0;
882         }
883         if (tty3270_max_index < tty->index + 1)
884                 tty3270_max_index = tty->index + 1;
885
886         /* Quick exit if there is no device for tty->index. */
887         if (PTR_ERR(tp) == -ENODEV)
888                 return -ENODEV;
889
890         /* Allocate tty3270 structure on first open. */
891         tp = tty3270_alloc_view();
892         if (IS_ERR(tp))
893                 return PTR_ERR(tp);
894
895         INIT_LIST_HEAD(&tp->lines);
896         INIT_LIST_HEAD(&tp->update);
897         INIT_LIST_HEAD(&tp->rcl_lines);
898         tp->rcl_max = 20;
899         init_timer(&tp->timer);
900         tasklet_init(&tp->readlet, 
901                      (void (*)(unsigned long)) tty3270_read_tasklet,
902                      (unsigned long) tp->read);
903
904         rc = raw3270_add_view(&tp->view, &tty3270_fn, tty->index);
905         if (rc) {
906                 tty3270_free_view(tp);
907                 return rc;
908         }
909
910         rc = tty3270_alloc_screen(tp);
911         if (rc) {
912                 raw3270_del_view(&tp->view);
913                 raw3270_put_view(&tp->view);
914                 return rc;
915         }
916
917         tp->tty = tty;
918         tty->low_latency = 0;
919         tty->driver_data = tp;
920         tty->winsize.ws_row = tp->view.rows - 2;
921         tty->winsize.ws_col = tp->view.cols;
922
923         tty3270_create_prompt(tp);
924         tty3270_create_status(tp);
925         tty3270_update_status(tp);
926
927         /* Create blank line for every line in the tty output area. */
928         for (i = 0; i < tp->view.rows - 2; i++)
929                 tty3270_blank_line(tp);
930
931         tp->kbd->tty = tty;
932         tp->kbd->fn_handler[KVAL(K_INCRCONSOLE)] = tty3270_exit_tty;
933         tp->kbd->fn_handler[KVAL(K_SCROLLBACK)] = tty3270_scroll_backward;
934         tp->kbd->fn_handler[KVAL(K_SCROLLFORW)] = tty3270_scroll_forward;
935         tp->kbd->fn_handler[KVAL(K_CONS)] = tty3270_rcl_backward;
936         kbd_ascebc(tp->kbd, tp->view.ascebc);
937
938         raw3270_activate_view(&tp->view);
939         return 0;
940 }
941
942 /*
943  * This routine is called when the 3270 tty is closed. We wait
944  * for the remaining request to be completed. Then we clean up.
945  */
946 static void
947 tty3270_close(struct tty_struct *tty, struct file * filp)
948 {
949         struct tty3270 *tp;
950
951         if (tty->count > 1)
952                 return;
953         tp = (struct tty3270 *) tty->driver_data;
954         if (tp) {
955                 tty->driver_data = 0;
956                 tp->tty = tp->kbd->tty = 0;
957                 raw3270_put_view(&tp->view);
958         }
959 }
960
961 /*
962  * We always have room.
963  */
964 static int
965 tty3270_write_room(struct tty_struct *tty)
966 {
967         return INT_MAX;
968 }
969
970 /*
971  * Insert character into the screen at the current position with the
972  * current color and highlight. This function does NOT do cursor movement.
973  */
974 static void
975 tty3270_put_character(struct tty3270 *tp, char ch)
976 {
977         struct tty3270_line *line;
978         struct tty3270_cell *cell;
979
980         line = tp->screen + tp->cy;
981         if (line->len <= tp->cx) {
982                 while (line->len < tp->cx) {
983                         cell = line->cells + line->len;
984                         cell->character = tp->view.ascebc[' '];
985                         cell->highlight = tp->highlight;
986                         cell->f_color = tp->f_color;
987                         line->len++;
988                 }
989                 line->len++;
990         }
991         cell = line->cells + tp->cx;
992         cell->character = tp->view.ascebc[(unsigned int) ch];
993         cell->highlight = tp->highlight;
994         cell->f_color = tp->f_color;
995 }
996
997 /*
998  * Convert a tty3270_line to a 3270 data fragment usable for output.
999  */
1000 static void
1001 tty3270_convert_line(struct tty3270 *tp, int line_nr)
1002 {
1003         struct tty3270_line *line;
1004         struct tty3270_cell *cell;
1005         struct string *s, *n;
1006         unsigned char highlight;
1007         unsigned char f_color;
1008         char *cp;
1009         int flen, i;
1010
1011         /* Determine how long the fragment will be. */
1012         flen = 3;               /* Prefix (TO_SBA). */
1013         line = tp->screen + line_nr;
1014         flen += line->len;
1015         highlight = TAX_RESET;
1016         f_color = TAC_RESET;
1017         for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
1018                 if (cell->highlight != highlight) {
1019                         flen += 3;      /* TO_SA to switch highlight. */
1020                         highlight = cell->highlight;
1021                 }
1022                 if (cell->f_color != f_color) {
1023                         flen += 3;      /* TO_SA to switch color. */
1024                         f_color = cell->f_color;
1025                 }
1026         }
1027         if (highlight != TAX_RESET)
1028                 flen += 3;      /* TO_SA to reset hightlight. */
1029         if (f_color != TAC_RESET)
1030                 flen += 3;      /* TO_SA to reset color. */
1031         if (line->len < tp->view.cols)
1032                 flen += 4;      /* Postfix (TO_RA). */
1033
1034         /* Find the line in the list. */
1035         i = tp->view.rows - 2 - line_nr;
1036         list_for_each_entry_reverse(s, &tp->lines, list)
1037                 if (--i <= 0)
1038                         break;
1039         /*
1040          * Check if the line needs to get reallocated.
1041          */
1042         if (s->len != flen) {
1043                 /* Reallocate string. */
1044                 n = tty3270_alloc_string(tp, flen);
1045                 list_add(&n->list, &s->list);
1046                 list_del_init(&s->list);
1047                 if (!list_empty(&s->update))
1048                         list_del_init(&s->update);
1049                 free_string(&tp->freemem, s);
1050                 s = n;
1051         }
1052
1053         /* Write 3270 data fragment. */
1054         cp = s->string;
1055         *cp++ = TO_SBA;
1056         *cp++ = 0;
1057         *cp++ = 0;
1058
1059         highlight = TAX_RESET;
1060         f_color = TAC_RESET;
1061         for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
1062                 if (cell->highlight != highlight) {
1063                         *cp++ = TO_SA;
1064                         *cp++ = TAT_EXTHI;
1065                         *cp++ = cell->highlight;
1066                         highlight = cell->highlight;
1067                 }
1068                 if (cell->f_color != f_color) {
1069                         *cp++ = TO_SA;
1070                         *cp++ = TAT_COLOR;
1071                         *cp++ = cell->f_color;
1072                         f_color = cell->f_color;
1073                 }
1074                 *cp++ = cell->character;
1075         }
1076         if (highlight != TAX_RESET) {
1077                 *cp++ = TO_SA;
1078                 *cp++ = TAT_EXTHI;
1079                 *cp++ = TAX_RESET;
1080         }
1081         if (f_color != TAC_RESET) {
1082                 *cp++ = TO_SA;
1083                 *cp++ = TAT_COLOR;
1084                 *cp++ = TAC_RESET;
1085         }
1086         if (line->len < tp->view.cols) {
1087                 *cp++ = TO_RA;
1088                 *cp++ = 0;
1089                 *cp++ = 0;
1090                 *cp++ = 0;
1091         }
1092
1093         if (tp->nr_up + line_nr < tp->view.rows - 2) {
1094                 /* Line is currently visible on screen. */
1095                 tty3270_update_string(tp, s, line_nr);
1096                 /* Add line to update list. */
1097                 if (list_empty(&s->update)) {
1098                         list_add_tail(&s->update, &tp->update);
1099                         tp->update_flags |= TTY_UPDATE_LIST;
1100                 }
1101         }
1102 }
1103
1104 /*
1105  * Do carriage return.
1106  */
1107 static void
1108 tty3270_cr(struct tty3270 *tp)
1109 {
1110         tp->cx = 0;
1111 }
1112
1113 /*
1114  * Do line feed.
1115  */
1116 static void
1117 tty3270_lf(struct tty3270 *tp)
1118 {
1119         struct tty3270_line temp;
1120         int i;
1121
1122         tty3270_convert_line(tp, tp->cy);
1123         if (tp->cy < tp->view.rows - 3) {
1124                 tp->cy++;
1125                 return;
1126         }
1127         /* Last line just filled up. Add new, blank line. */
1128         tty3270_blank_line(tp);
1129         temp = tp->screen[0];
1130         temp.len = 0;
1131         for (i = 0; i < tp->view.rows - 3; i++)
1132                 tp->screen[i] = tp->screen[i+1];
1133         tp->screen[tp->view.rows - 3] = temp;
1134         tty3270_rebuild_update(tp);
1135 }
1136
1137 static void
1138 tty3270_ri(struct tty3270 *tp)
1139 {
1140         if (tp->cy > 0) {
1141             tty3270_convert_line(tp, tp->cy);
1142             tp->cy--;
1143         }
1144 }
1145
1146 /*
1147  * Insert characters at current position.
1148  */
1149 static void
1150 tty3270_insert_characters(struct tty3270 *tp, int n)
1151 {
1152         struct tty3270_line *line;
1153         int k;
1154
1155         line = tp->screen + tp->cy;
1156         while (line->len < tp->cx) {
1157                 line->cells[line->len].character = tp->view.ascebc[' '];
1158                 line->cells[line->len].highlight = TAX_RESET;
1159                 line->cells[line->len].f_color = TAC_RESET;
1160                 line->len++;
1161         }
1162         if (n > tp->view.cols - tp->cx)
1163                 n = tp->view.cols - tp->cx;
1164         k = min_t(int, line->len - tp->cx, tp->view.cols - tp->cx - n);
1165         while (k--)
1166                 line->cells[tp->cx + n + k] = line->cells[tp->cx + k];
1167         line->len += n;
1168         if (line->len > tp->view.cols)
1169                 line->len = tp->view.cols;
1170         while (n-- > 0) {
1171                 line->cells[tp->cx + n].character = tp->view.ascebc[' '];
1172                 line->cells[tp->cx + n].highlight = tp->highlight;
1173                 line->cells[tp->cx + n].f_color = tp->f_color;
1174         }
1175 }
1176
1177 /*
1178  * Delete characters at current position.
1179  */
1180 static void
1181 tty3270_delete_characters(struct tty3270 *tp, int n)
1182 {
1183         struct tty3270_line *line;
1184         int i;
1185
1186         line = tp->screen + tp->cy;
1187         if (line->len <= tp->cx)
1188                 return;
1189         if (line->len - tp->cx <= n) {
1190                 line->len = tp->cx;
1191                 return;
1192         }
1193         for (i = tp->cx; i + n < line->len; i++)
1194                 line->cells[i] = line->cells[i + n];
1195         line->len -= n;
1196 }
1197
1198 /*
1199  * Erase characters at current position.
1200  */
1201 static void
1202 tty3270_erase_characters(struct tty3270 *tp, int n)
1203 {
1204         struct tty3270_line *line;
1205         struct tty3270_cell *cell;
1206
1207         line = tp->screen + tp->cy;
1208         while (line->len > tp->cx && n-- > 0) {
1209                 cell = line->cells + tp->cx++;
1210                 cell->character = ' ';
1211                 cell->highlight = TAX_RESET;
1212                 cell->f_color = TAC_RESET;
1213         }
1214         tp->cx += n;
1215         tp->cx = min_t(int, tp->cx, tp->view.cols - 1);
1216 }
1217
1218 /*
1219  * Erase line, 3 different cases:
1220  *  Esc [ 0 K   Erase from current position to end of line inclusive
1221  *  Esc [ 1 K   Erase from beginning of line to current position inclusive
1222  *  Esc [ 2 K   Erase entire line (without moving cursor)
1223  */
1224 static void
1225 tty3270_erase_line(struct tty3270 *tp, int mode)
1226 {
1227         struct tty3270_line *line;
1228         struct tty3270_cell *cell;
1229         int i;
1230
1231         line = tp->screen + tp->cy;
1232         if (mode == 0)
1233                 line->len = tp->cx;
1234         else if (mode == 1) {
1235                 for (i = 0; i < tp->cx; i++) {
1236                         cell = line->cells + i;
1237                         cell->character = ' ';
1238                         cell->highlight = TAX_RESET;
1239                         cell->f_color = TAC_RESET;
1240                 }
1241                 if (line->len <= tp->cx)
1242                         line->len = tp->cx + 1;
1243         } else if (mode == 2)
1244                 line->len = 0;
1245         tty3270_convert_line(tp, tp->cy);
1246 }
1247
1248 /*
1249  * Erase display, 3 different cases:
1250  *  Esc [ 0 J   Erase from current position to bottom of screen inclusive
1251  *  Esc [ 1 J   Erase from top of screen to current position inclusive
1252  *  Esc [ 2 J   Erase entire screen (without moving the cursor)
1253  */
1254 static void
1255 tty3270_erase_display(struct tty3270 *tp, int mode)
1256 {
1257         int i;
1258
1259         if (mode == 0) {
1260                 tty3270_erase_line(tp, 0);
1261                 for (i = tp->cy + 1; i < tp->view.rows - 2; i++) {
1262                         tp->screen[i].len = 0;
1263                         tty3270_convert_line(tp, i);
1264                 }
1265         } else if (mode == 1) {
1266                 for (i = 0; i < tp->cy; i++) {
1267                         tp->screen[i].len = 0;
1268                         tty3270_convert_line(tp, i);
1269                 }
1270                 tty3270_erase_line(tp, 1);
1271         } else if (mode == 2) {
1272                 for (i = 0; i < tp->view.rows - 2; i++) {
1273                         tp->screen[i].len = 0;
1274                         tty3270_convert_line(tp, i);
1275                 }
1276         }
1277         tty3270_rebuild_update(tp);
1278 }
1279
1280 /*
1281  * Set attributes found in an escape sequence.
1282  *  Esc [ <attr> ; <attr> ; ... m
1283  */
1284 static void
1285 tty3270_set_attributes(struct tty3270 *tp)
1286 {
1287         static unsigned char f_colors[] = {
1288                 TAC_DEFAULT, TAC_RED, TAC_GREEN, TAC_YELLOW, TAC_BLUE,
1289                 TAC_PINK, TAC_TURQ, TAC_WHITE, 0, TAC_DEFAULT
1290         };
1291         int i, attr;
1292
1293         for (i = 0; i <= tp->esc_npar; i++) {
1294                 attr = tp->esc_par[i];
1295                 switch (attr) {
1296                 case 0:         /* Reset */
1297                         tp->highlight = TAX_RESET;
1298                         tp->f_color = TAC_RESET;
1299                         break;
1300                 /* Highlight. */
1301                 case 4:         /* Start underlining. */
1302                         tp->highlight = TAX_UNDER;
1303                         break;
1304                 case 5:         /* Start blink. */
1305                         tp->highlight = TAX_BLINK;
1306                         break;
1307                 case 7:         /* Start reverse. */
1308                         tp->highlight = TAX_REVER;
1309                         break;
1310                 case 24:        /* End underlining */
1311                         if (tp->highlight == TAX_UNDER)
1312                                 tp->highlight = TAX_RESET;
1313                         break;
1314                 case 25:        /* End blink. */
1315                         if (tp->highlight == TAX_BLINK)
1316                                 tp->highlight = TAX_RESET;
1317                         break;
1318                 case 27:        /* End reverse. */
1319                         if (tp->highlight == TAX_REVER)
1320                                 tp->highlight = TAX_RESET;
1321                         break;
1322                 /* Foreground color. */
1323                 case 30:        /* Black */
1324                 case 31:        /* Red */
1325                 case 32:        /* Green */
1326                 case 33:        /* Yellow */
1327                 case 34:        /* Blue */
1328                 case 35:        /* Magenta */
1329                 case 36:        /* Cyan */
1330                 case 37:        /* White */
1331                 case 39:        /* Black */
1332                         tp->f_color = f_colors[attr - 30];
1333                         break;
1334                 }
1335         }
1336 }
1337
1338 static inline int
1339 tty3270_getpar(struct tty3270 *tp, int ix)
1340 {
1341         return (tp->esc_par[ix] > 0) ? tp->esc_par[ix] : 1;
1342 }
1343
1344 static void
1345 tty3270_goto_xy(struct tty3270 *tp, int cx, int cy)
1346 {
1347         tp->cx = min_t(int, tp->view.cols - 1, max_t(int, 0, cx));
1348         cy = min_t(int, tp->view.rows - 3, max_t(int, 0, cy));
1349         if (cy != tp->cy) {
1350                 tty3270_convert_line(tp, tp->cy);
1351                 tp->cy = cy;
1352         }
1353 }
1354
1355 /*
1356  * Process escape sequences. Known sequences:
1357  *  Esc 7                       Save Cursor Position
1358  *  Esc 8                       Restore Cursor Position
1359  *  Esc [ Pn ; Pn ; .. m        Set attributes
1360  *  Esc [ Pn ; Pn H             Cursor Position
1361  *  Esc [ Pn ; Pn f             Cursor Position
1362  *  Esc [ Pn A                  Cursor Up
1363  *  Esc [ Pn B                  Cursor Down
1364  *  Esc [ Pn C                  Cursor Forward
1365  *  Esc [ Pn D                  Cursor Backward
1366  *  Esc [ Pn G                  Cursor Horizontal Absolute
1367  *  Esc [ Pn X                  Erase Characters
1368  *  Esc [ Ps J                  Erase in Display
1369  *  Esc [ Ps K                  Erase in Line
1370  * // FIXME: add all the new ones.
1371  *
1372  *  Pn is a numeric parameter, a string of zero or more decimal digits.
1373  *  Ps is a selective parameter.
1374  */
1375 static void
1376 tty3270_escape_sequence(struct tty3270 *tp, char ch)
1377 {
1378         enum { ESnormal, ESesc, ESsquare, ESgetpars };
1379
1380         if (tp->esc_state == ESnormal) {
1381                 if (ch == 0x1b)
1382                         /* Starting new escape sequence. */
1383                         tp->esc_state = ESesc;
1384                 return;
1385         }
1386         if (tp->esc_state == ESesc) {
1387                 tp->esc_state = ESnormal;
1388                 switch (ch) {
1389                 case '[':
1390                         tp->esc_state = ESsquare;
1391                         break;
1392                 case 'E':
1393                         tty3270_cr(tp);
1394                         tty3270_lf(tp);
1395                         break;
1396                 case 'M':
1397                         tty3270_ri(tp);
1398                         break;
1399                 case 'D':
1400                         tty3270_lf(tp);
1401                         break;
1402                 case 'Z':               /* Respond ID. */
1403                         kbd_puts_queue(tp->tty, "\033[?6c");
1404                         break;
1405                 case '7':               /* Save cursor position. */
1406                         tp->saved_cx = tp->cx;
1407                         tp->saved_cy = tp->cy;
1408                         tp->saved_highlight = tp->highlight;
1409                         tp->saved_f_color = tp->f_color;
1410                         break;
1411                 case '8':               /* Restore cursor position. */
1412                         tty3270_convert_line(tp, tp->cy);
1413                         tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
1414                         tp->highlight = tp->saved_highlight;
1415                         tp->f_color = tp->saved_f_color;
1416                         break;
1417                 case 'c':               /* Reset terminal. */
1418                         tp->cx = tp->saved_cx = 0;
1419                         tp->cy = tp->saved_cy = 0;
1420                         tp->highlight = tp->saved_highlight = TAX_RESET;
1421                         tp->f_color = tp->saved_f_color = TAC_RESET;
1422                         tty3270_erase_display(tp, 2);
1423                         break;
1424                 }
1425                 return;
1426         }
1427         if (tp->esc_state == ESsquare) {
1428                 tp->esc_state = ESgetpars;
1429                 memset(tp->esc_par, 0, sizeof(tp->esc_par));
1430                 tp->esc_npar = 0;
1431                 tp->esc_ques = (ch == '?');
1432                 if (tp->esc_ques)
1433                         return;
1434         }
1435         if (tp->esc_state == ESgetpars) {
1436                 if (ch == ';' && tp->esc_npar < ESCAPE_NPAR - 1) {
1437                         tp->esc_npar++;
1438                         return;
1439                 }
1440                 if (ch >= '0' && ch <= '9') {
1441                         tp->esc_par[tp->esc_npar] *= 10;
1442                         tp->esc_par[tp->esc_npar] += ch - '0';
1443                         return;
1444                 }
1445         }
1446         tp->esc_state = ESnormal;
1447         if (ch == 'n' && !tp->esc_ques) {
1448                 if (tp->esc_par[0] == 5)                /* Status report. */
1449                         kbd_puts_queue(tp->tty, "\033[0n");
1450                 else if (tp->esc_par[0] == 6) { /* Cursor report. */
1451                         char buf[40];
1452                         sprintf(buf, "\033[%d;%dR", tp->cy + 1, tp->cx + 1);
1453                         kbd_puts_queue(tp->tty, buf);
1454                 }
1455                 return;
1456         }
1457         if (tp->esc_ques)
1458                 return;
1459         switch (ch) {
1460         case 'm':
1461                 tty3270_set_attributes(tp);
1462                 break;
1463         case 'H':       /* Set cursor position. */
1464         case 'f':
1465                 tty3270_goto_xy(tp, tty3270_getpar(tp, 1) - 1,
1466                                 tty3270_getpar(tp, 0) - 1);
1467                 break;
1468         case 'd':       /* Set y position. */
1469                 tty3270_goto_xy(tp, tp->cx, tty3270_getpar(tp, 0) - 1);
1470                 break;
1471         case 'A':       /* Cursor up. */
1472         case 'F':
1473                 tty3270_goto_xy(tp, tp->cx, tp->cy - tty3270_getpar(tp, 0));
1474                 break;
1475         case 'B':       /* Cursor down. */
1476         case 'e':
1477         case 'E':
1478                 tty3270_goto_xy(tp, tp->cx, tp->cy + tty3270_getpar(tp, 0));
1479                 break;
1480         case 'C':       /* Cursor forward. */
1481         case 'a':
1482                 tty3270_goto_xy(tp, tp->cx + tty3270_getpar(tp, 0), tp->cy);
1483                 break;
1484         case 'D':       /* Cursor backward. */
1485                 tty3270_goto_xy(tp, tp->cx - tty3270_getpar(tp, 0), tp->cy);
1486                 break;
1487         case 'G':       /* Set x position. */
1488         case '`':
1489                 tty3270_goto_xy(tp, tty3270_getpar(tp, 0), tp->cy);
1490                 break;
1491         case 'X':       /* Erase Characters. */
1492                 tty3270_erase_characters(tp, tty3270_getpar(tp, 0));
1493                 break;
1494         case 'J':       /* Erase display. */
1495                 tty3270_erase_display(tp, tp->esc_par[0]);
1496                 break;
1497         case 'K':       /* Erase line. */
1498                 tty3270_erase_line(tp, tp->esc_par[0]);
1499                 break;
1500         case 'P':       /* Delete characters. */
1501                 tty3270_delete_characters(tp, tty3270_getpar(tp, 0));
1502                 break;
1503         case '@':       /* Insert characters. */
1504                 tty3270_insert_characters(tp, tty3270_getpar(tp, 0));
1505                 break;
1506         case 's':       /* Save cursor position. */
1507                 tp->saved_cx = tp->cx;
1508                 tp->saved_cy = tp->cy;
1509                 tp->saved_highlight = tp->highlight;
1510                 tp->saved_f_color = tp->f_color;
1511                 break;
1512         case 'u':       /* Restore cursor position. */
1513                 tty3270_convert_line(tp, tp->cy);
1514                 tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy);
1515                 tp->highlight = tp->saved_highlight;
1516                 tp->f_color = tp->saved_f_color;
1517                 break;
1518         }
1519 }
1520
1521 /*
1522  * String write routine for 3270 ttys
1523  */
1524 static void
1525 tty3270_do_write(struct tty3270 *tp, const unsigned char *buf, int count)
1526 {
1527         int i_msg, i;
1528
1529         spin_lock_bh(&tp->view.lock);
1530         for (i_msg = 0; !tp->tty->stopped && i_msg < count; i_msg++) {
1531                 if (tp->esc_state != 0) {
1532                         /* Continue escape sequence. */
1533                         tty3270_escape_sequence(tp, buf[i_msg]);
1534                         continue;
1535                 }
1536
1537                 switch (buf[i_msg]) {
1538                 case 0x07:              /* '\a' -- Alarm */
1539                         tp->wcc |= TW_PLUSALARM;
1540                         break;
1541                 case 0x08:              /* Backspace. */
1542                         if (tp->cx > 0) {
1543                                 tp->cx--;
1544                                 tty3270_put_character(tp, ' ');
1545                         }
1546                         break;
1547                 case 0x09:              /* '\t' -- Tabulate */
1548                         for (i = tp->cx % 8; i < 8; i++) {
1549                                 if (tp->cx >= tp->view.cols) {
1550                                         tty3270_cr(tp);
1551                                         tty3270_lf(tp);
1552                                         break;
1553                                 }
1554                                 tty3270_put_character(tp, ' ');
1555                                 tp->cx++;
1556                         }
1557                         break;
1558                 case 0x0a:              /* '\n' -- New Line */
1559                         tty3270_cr(tp);
1560                         tty3270_lf(tp);
1561                         break;
1562                 case 0x0c:              /* '\f' -- Form Feed */
1563                         tty3270_erase_display(tp, 2);
1564                         tp->cx = tp->cy = 0;
1565                         break;
1566                 case 0x0d:              /* '\r' -- Carriage Return */
1567                         tp->cx = 0;
1568                         break;
1569                 case 0x0f:              /* SuSE "exit alternate mode" */
1570                         break;
1571                 case 0x1b:              /* Start escape sequence. */
1572                         tty3270_escape_sequence(tp, buf[i_msg]);
1573                         break;
1574                 default:                /* Insert normal character. */
1575                         if (tp->cx >= tp->view.cols) {
1576                                 tty3270_cr(tp);
1577                                 tty3270_lf(tp);
1578                         }
1579                         tty3270_put_character(tp, buf[i_msg]);
1580                         tp->cx++;
1581                         break;
1582                 }
1583         }
1584         /* Convert current line to 3270 data fragment. */
1585         tty3270_convert_line(tp, tp->cy);
1586
1587         /* Setup timer to update display after 1/10 second */
1588         if (!timer_pending(&tp->timer))
1589                 tty3270_set_timer(tp, HZ/10);
1590
1591         spin_unlock_bh(&tp->view.lock);
1592 }
1593
1594 /*
1595  * String write routine for 3270 ttys
1596  */
1597 static int
1598 tty3270_write(struct tty_struct * tty,
1599               const unsigned char *buf, int count)
1600 {
1601         struct tty3270 *tp;
1602
1603         tp = tty->driver_data;
1604         if (!tp)
1605                 return 0;
1606         if (tp->char_count > 0) {
1607                 tty3270_do_write(tp, tp->char_buf, tp->char_count);
1608                 tp->char_count = 0;
1609         }
1610         tty3270_do_write(tp, buf, count);
1611         return count;
1612 }
1613
1614 /*
1615  * Put single characters to the ttys character buffer
1616  */
1617 static void
1618 tty3270_put_char(struct tty_struct *tty, unsigned char ch)
1619 {
1620         struct tty3270 *tp;
1621
1622         tp = tty->driver_data;
1623         if (!tp)
1624                 return;
1625         if (tp->char_count < TTY3270_CHAR_BUF_SIZE)
1626                 tp->char_buf[tp->char_count++] = ch;
1627 }
1628
1629 /*
1630  * Flush all characters from the ttys characeter buffer put there
1631  * by tty3270_put_char.
1632  */
1633 static void
1634 tty3270_flush_chars(struct tty_struct *tty)
1635 {
1636         struct tty3270 *tp;
1637
1638         tp = tty->driver_data;
1639         if (!tp)
1640                 return;
1641         if (tp->char_count > 0) {
1642                 tty3270_do_write(tp, tp->char_buf, tp->char_count);
1643                 tp->char_count = 0;
1644         }
1645 }
1646
1647 /*
1648  * Returns the number of characters in the output buffer. This is
1649  * used in tty_wait_until_sent to wait until all characters have
1650  * appeared on the screen.
1651  */
1652 static int
1653 tty3270_chars_in_buffer(struct tty_struct *tty)
1654 {
1655         return 0;
1656 }
1657
1658 static void
1659 tty3270_flush_buffer(struct tty_struct *tty)
1660 {
1661 }
1662
1663 /*
1664  * Check for visible/invisible input switches
1665  */
1666 static void
1667 tty3270_set_termios(struct tty_struct *tty, struct termios *old)
1668 {
1669         struct tty3270 *tp;
1670         int new;
1671
1672         tp = tty->driver_data;
1673         if (!tp)
1674                 return;
1675         spin_lock_bh(&tp->view.lock);
1676         if (L_ICANON(tty)) {
1677                 new = L_ECHO(tty) ? TF_INPUT: TF_INPUTN;
1678                 if (new != tp->inattr) {
1679                         tp->inattr = new;
1680                         tty3270_update_prompt(tp, 0, 0);
1681                         tty3270_set_timer(tp, 1);
1682                 }
1683         }
1684         spin_unlock_bh(&tp->view.lock);
1685 }
1686
1687 /*
1688  * Disable reading from a 3270 tty
1689  */
1690 static void
1691 tty3270_throttle(struct tty_struct * tty)
1692 {
1693         struct tty3270 *tp;
1694
1695         tp = tty->driver_data;
1696         if (!tp)
1697                 return;
1698         tp->throttle = 1;
1699 }
1700
1701 /*
1702  * Enable reading from a 3270 tty
1703  */
1704 static void
1705 tty3270_unthrottle(struct tty_struct * tty)
1706 {
1707         struct tty3270 *tp;
1708
1709         tp = tty->driver_data;
1710         if (!tp)
1711                 return;
1712         tp->throttle = 0;
1713         if (tp->attn)
1714                 tty3270_issue_read(tp, 1);
1715 }
1716
1717 /*
1718  * Hang up the tty device.
1719  */
1720 static void
1721 tty3270_hangup(struct tty_struct *tty)
1722 {
1723         // FIXME: implement
1724 }
1725
1726 static void
1727 tty3270_wait_until_sent(struct tty_struct *tty, int timeout)
1728 {
1729 }
1730
1731 static int
1732 tty3270_ioctl(struct tty_struct *tty, struct file *file,
1733               unsigned int cmd, unsigned long arg)
1734 {
1735         struct tty3270 *tp;
1736
1737         tp = tty->driver_data;
1738         if (!tp)
1739                 return -ENODEV;
1740         if (tty->flags & (1 << TTY_IO_ERROR))
1741                 return -EIO;
1742         return kbd_ioctl(tp->kbd, file, cmd, arg);
1743 }
1744
1745 static struct tty_operations tty3270_ops = {
1746         .open = tty3270_open,
1747         .close = tty3270_close,
1748         .write = tty3270_write,
1749         .put_char = tty3270_put_char,
1750         .flush_chars = tty3270_flush_chars,
1751         .write_room = tty3270_write_room,
1752         .chars_in_buffer = tty3270_chars_in_buffer,
1753         .flush_buffer = tty3270_flush_buffer,
1754         .throttle = tty3270_throttle,
1755         .unthrottle = tty3270_unthrottle,
1756         .hangup = tty3270_hangup,
1757         .wait_until_sent = tty3270_wait_until_sent,
1758         .ioctl = tty3270_ioctl,
1759         .set_termios = tty3270_set_termios
1760 };
1761
1762 void
1763 tty3270_notifier(int index, int active)
1764 {
1765         if (active)
1766                 tty_register_device(tty3270_driver, index, 0);
1767         else
1768                 tty_unregister_device(tty3270_driver, index);
1769 }
1770
1771 /*
1772  * 3270 tty registration code called from tty_init().
1773  * Most kernel services (incl. kmalloc) are available at this poimt.
1774  */
1775 int __init
1776 tty3270_init(void)
1777 {
1778         struct tty_driver *driver;
1779         int ret;
1780
1781         driver = alloc_tty_driver(256);
1782         if (!driver)
1783                 return -ENOMEM;
1784
1785         /*
1786          * Initialize the tty_driver structure
1787          * Entries in tty3270_driver that are NOT initialized:
1788          * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1789          */
1790         driver->owner = THIS_MODULE;
1791         driver->devfs_name = "ttyTUB/";
1792         driver->driver_name = "ttyTUB";
1793         driver->name = "ttyTUB";
1794         driver->major = IBM_TTY3270_MAJOR;
1795         driver->type = TTY_DRIVER_TYPE_SYSTEM;
1796         driver->subtype = SYSTEM_TYPE_TTY;
1797         driver->init_termios = tty_std_termios;
1798         driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_NO_DEVFS;
1799         tty_set_operations(driver, &tty3270_ops);
1800         ret = tty_register_driver(driver);
1801         if (ret) {
1802                 printk(KERN_ERR "tty3270 registration failed with %d\n", ret);
1803                 put_tty_driver(driver);
1804                 return ret;
1805         }
1806         tty3270_driver = driver;
1807         ret = raw3270_register_notifier(tty3270_notifier);
1808         if (ret) {
1809                 printk(KERN_ERR "tty3270 notifier registration failed "
1810                        "with %d\n", ret);
1811                 put_tty_driver(driver);
1812                 return ret;
1813
1814         }
1815         return 0;
1816 }
1817
1818 static void __exit
1819 tty3270_exit(void)
1820 {
1821         struct tty_driver *driver;
1822
1823         raw3270_unregister_notifier(tty3270_notifier);
1824         driver = tty3270_driver;
1825         tty3270_driver = 0;
1826         tty_unregister_driver(driver);
1827         tty3270_del_views();
1828 }
1829
1830 MODULE_LICENSE("GPL");
1831 MODULE_ALIAS_CHARDEV_MAJOR(IBM_TTY3270_MAJOR);
1832
1833 module_init(tty3270_init);
1834 module_exit(tty3270_exit);