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