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