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