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