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