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