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