xref: /linux/drivers/s390/char/sclp_tty.c (revision f3d9478b2ce468c3115b02ecae7e975990697f15)
1 /*
2  *  drivers/s390/char/sclp_tty.c
3  *    SCLP line mode terminal driver.
4  *
5  *  S390 version
6  *    Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7  *    Author(s): Martin Peschke <mpeschke@de.ibm.com>
8  *		 Martin Schwidefsky <schwidefsky@de.ibm.com>
9  */
10 
11 #include <linux/config.h>
12 #include <linux/module.h>
13 #include <linux/kmod.h>
14 #include <linux/tty.h>
15 #include <linux/tty_driver.h>
16 #include <linux/tty_flip.h>
17 #include <linux/sched.h>
18 #include <linux/wait.h>
19 #include <linux/slab.h>
20 #include <linux/err.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
23 #include <asm/uaccess.h>
24 
25 #include "ctrlchar.h"
26 #include "sclp.h"
27 #include "sclp_rw.h"
28 #include "sclp_tty.h"
29 
30 #define SCLP_TTY_PRINT_HEADER "sclp tty driver: "
31 
32 /*
33  * size of a buffer that collects single characters coming in
34  * via sclp_tty_put_char()
35  */
36 #define SCLP_TTY_BUF_SIZE 512
37 
38 /*
39  * There is exactly one SCLP terminal, so we can keep things simple
40  * and allocate all variables statically.
41  */
42 
43 /* Lock to guard over changes to global variables. */
44 static spinlock_t sclp_tty_lock;
45 /* List of free pages that can be used for console output buffering. */
46 static struct list_head sclp_tty_pages;
47 /* List of full struct sclp_buffer structures ready for output. */
48 static struct list_head sclp_tty_outqueue;
49 /* Counter how many buffers are emitted. */
50 static int sclp_tty_buffer_count;
51 /* Pointer to current console buffer. */
52 static struct sclp_buffer *sclp_ttybuf;
53 /* Timer for delayed output of console messages. */
54 static struct timer_list sclp_tty_timer;
55 /* Waitqueue to wait for buffers to get empty. */
56 static wait_queue_head_t sclp_tty_waitq;
57 
58 static struct tty_struct *sclp_tty;
59 static unsigned char sclp_tty_chars[SCLP_TTY_BUF_SIZE];
60 static unsigned short int sclp_tty_chars_count;
61 
62 struct tty_driver *sclp_tty_driver;
63 
64 extern struct termios  tty_std_termios;
65 
66 static struct sclp_ioctls sclp_ioctls;
67 static struct sclp_ioctls sclp_ioctls_init =
68 {
69 	8,			/* 1 hor. tab. = 8 spaces */
70 	0,			/* no echo of input by this driver */
71 	80,			/* 80 characters/line */
72 	1,			/* write after 1/10 s without final new line */
73 	MAX_KMEM_PAGES,		/* quick fix: avoid __alloc_pages */
74 	MAX_KMEM_PAGES,		/* take 32/64 pages from kernel memory, */
75 	0,			/* do not convert to lower case */
76 	0x6c			/* to seprate upper and lower case */
77 				/* ('%' in EBCDIC) */
78 };
79 
80 /* This routine is called whenever we try to open a SCLP terminal. */
81 static int
82 sclp_tty_open(struct tty_struct *tty, struct file *filp)
83 {
84 	sclp_tty = tty;
85 	tty->driver_data = NULL;
86 	tty->low_latency = 0;
87 	return 0;
88 }
89 
90 /* This routine is called when the SCLP terminal is closed. */
91 static void
92 sclp_tty_close(struct tty_struct *tty, struct file *filp)
93 {
94 	if (tty->count > 1)
95 		return;
96 	sclp_tty = NULL;
97 }
98 
99 /* execute commands to control the i/o behaviour of the SCLP tty at runtime */
100 static int
101 sclp_tty_ioctl(struct tty_struct *tty, struct file * file,
102 	       unsigned int cmd, unsigned long arg)
103 {
104 	unsigned long flags;
105 	unsigned int obuf;
106 	int check;
107 	int rc;
108 
109 	if (tty->flags & (1 << TTY_IO_ERROR))
110 		return -EIO;
111 	rc = 0;
112 	check = 0;
113 	switch (cmd) {
114 	case TIOCSCLPSHTAB:
115 		/* set width of horizontal tab	*/
116 		if (get_user(sclp_ioctls.htab, (unsigned short __user *) arg))
117 			rc = -EFAULT;
118 		else
119 			check = 1;
120 		break;
121 	case TIOCSCLPGHTAB:
122 		/* get width of horizontal tab	*/
123 		if (put_user(sclp_ioctls.htab, (unsigned short __user *) arg))
124 			rc = -EFAULT;
125 		break;
126 	case TIOCSCLPSECHO:
127 		/* enable/disable echo of input */
128 		if (get_user(sclp_ioctls.echo, (unsigned char __user *) arg))
129 			rc = -EFAULT;
130 		break;
131 	case TIOCSCLPGECHO:
132 		/* Is echo of input enabled ?  */
133 		if (put_user(sclp_ioctls.echo, (unsigned char __user *) arg))
134 			rc = -EFAULT;
135 		break;
136 	case TIOCSCLPSCOLS:
137 		/* set number of columns for output  */
138 		if (get_user(sclp_ioctls.columns, (unsigned short __user *) arg))
139 			rc = -EFAULT;
140 		else
141 			check = 1;
142 		break;
143 	case TIOCSCLPGCOLS:
144 		/* get number of columns for output  */
145 		if (put_user(sclp_ioctls.columns, (unsigned short __user *) arg))
146 			rc = -EFAULT;
147 		break;
148 	case TIOCSCLPSNL:
149 		/* enable/disable writing without final new line character  */
150 		if (get_user(sclp_ioctls.final_nl, (signed char __user *) arg))
151 			rc = -EFAULT;
152 		break;
153 	case TIOCSCLPGNL:
154 		/* Is writing without final new line character enabled ?  */
155 		if (put_user(sclp_ioctls.final_nl, (signed char __user *) arg))
156 			rc = -EFAULT;
157 		break;
158 	case TIOCSCLPSOBUF:
159 		/*
160 		 * set the maximum buffers size for output, will be rounded
161 		 * up to next 4kB boundary and stored as number of SCCBs
162 		 * (4kB Buffers) limitation: 256 x 4kB
163 		 */
164 		if (get_user(obuf, (unsigned int __user *) arg) == 0) {
165 			if (obuf & 0xFFF)
166 				sclp_ioctls.max_sccb = (obuf >> 12) + 1;
167 			else
168 				sclp_ioctls.max_sccb = (obuf >> 12);
169 		} else
170 			rc = -EFAULT;
171 		break;
172 	case TIOCSCLPGOBUF:
173 		/* get the maximum buffers size for output  */
174 		obuf = sclp_ioctls.max_sccb << 12;
175 		if (put_user(obuf, (unsigned int __user *) arg))
176 			rc = -EFAULT;
177 		break;
178 	case TIOCSCLPGKBUF:
179 		/* get the number of buffers got from kernel at startup */
180 		if (put_user(sclp_ioctls.kmem_sccb, (unsigned short __user *) arg))
181 			rc = -EFAULT;
182 		break;
183 	case TIOCSCLPSCASE:
184 		/* enable/disable conversion from upper to lower case */
185 		if (get_user(sclp_ioctls.tolower, (unsigned char __user *) arg))
186 			rc = -EFAULT;
187 		break;
188 	case TIOCSCLPGCASE:
189 		/* Is conversion from upper to lower case of input enabled? */
190 		if (put_user(sclp_ioctls.tolower, (unsigned char __user *) arg))
191 			rc = -EFAULT;
192 		break;
193 	case TIOCSCLPSDELIM:
194 		/*
195 		 * set special character used for separating upper and
196 		 * lower case, 0x00 disables this feature
197 		 */
198 		if (get_user(sclp_ioctls.delim, (unsigned char __user *) arg))
199 			rc = -EFAULT;
200 		break;
201 	case TIOCSCLPGDELIM:
202 		/*
203 		 * get special character used for separating upper and
204 		 * lower case, 0x00 disables this feature
205 		 */
206 		if (put_user(sclp_ioctls.delim, (unsigned char __user *) arg))
207 			rc = -EFAULT;
208 		break;
209 	case TIOCSCLPSINIT:
210 		/* set initial (default) sclp ioctls  */
211 		sclp_ioctls = sclp_ioctls_init;
212 		check = 1;
213 		break;
214 	default:
215 		rc = -ENOIOCTLCMD;
216 		break;
217 	}
218 	if (check) {
219 		spin_lock_irqsave(&sclp_tty_lock, flags);
220 		if (sclp_ttybuf != NULL) {
221 			sclp_set_htab(sclp_ttybuf, sclp_ioctls.htab);
222 			sclp_set_columns(sclp_ttybuf, sclp_ioctls.columns);
223 		}
224 		spin_unlock_irqrestore(&sclp_tty_lock, flags);
225 	}
226 	return rc;
227 }
228 
229 /*
230  * This routine returns the numbers of characters the tty driver
231  * will accept for queuing to be written.  This number is subject
232  * to change as output buffers get emptied, or if the output flow
233  * control is acted. This is not an exact number because not every
234  * character needs the same space in the sccb. The worst case is
235  * a string of newlines. Every newlines creates a new mto which
236  * needs 8 bytes.
237  */
238 static int
239 sclp_tty_write_room (struct tty_struct *tty)
240 {
241 	unsigned long flags;
242 	struct list_head *l;
243 	int count;
244 
245 	spin_lock_irqsave(&sclp_tty_lock, flags);
246 	count = 0;
247 	if (sclp_ttybuf != NULL)
248 		count = sclp_buffer_space(sclp_ttybuf) / sizeof(struct mto);
249 	list_for_each(l, &sclp_tty_pages)
250 		count += NR_EMPTY_MTO_PER_SCCB;
251 	spin_unlock_irqrestore(&sclp_tty_lock, flags);
252 	return count;
253 }
254 
255 static void
256 sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc)
257 {
258 	unsigned long flags;
259 	void *page;
260 
261 	do {
262 		page = sclp_unmake_buffer(buffer);
263 		spin_lock_irqsave(&sclp_tty_lock, flags);
264 		/* Remove buffer from outqueue */
265 		list_del(&buffer->list);
266 		sclp_tty_buffer_count--;
267 		list_add_tail((struct list_head *) page, &sclp_tty_pages);
268 		/* Check if there is a pending buffer on the out queue. */
269 		buffer = NULL;
270 		if (!list_empty(&sclp_tty_outqueue))
271 			buffer = list_entry(sclp_tty_outqueue.next,
272 					    struct sclp_buffer, list);
273 		spin_unlock_irqrestore(&sclp_tty_lock, flags);
274 	} while (buffer && sclp_emit_buffer(buffer, sclp_ttybuf_callback));
275 	wake_up(&sclp_tty_waitq);
276 	/* check if the tty needs a wake up call */
277 	if (sclp_tty != NULL) {
278 		tty_wakeup(sclp_tty);
279 	}
280 }
281 
282 static inline void
283 __sclp_ttybuf_emit(struct sclp_buffer *buffer)
284 {
285 	unsigned long flags;
286 	int count;
287 	int rc;
288 
289 	spin_lock_irqsave(&sclp_tty_lock, flags);
290 	list_add_tail(&buffer->list, &sclp_tty_outqueue);
291 	count = sclp_tty_buffer_count++;
292 	spin_unlock_irqrestore(&sclp_tty_lock, flags);
293 	if (count)
294 		return;
295 	rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback);
296 	if (rc)
297 		sclp_ttybuf_callback(buffer, rc);
298 }
299 
300 /*
301  * When this routine is called from the timer then we flush the
302  * temporary write buffer.
303  */
304 static void
305 sclp_tty_timeout(unsigned long data)
306 {
307 	unsigned long flags;
308 	struct sclp_buffer *buf;
309 
310 	spin_lock_irqsave(&sclp_tty_lock, flags);
311 	buf = sclp_ttybuf;
312 	sclp_ttybuf = NULL;
313 	spin_unlock_irqrestore(&sclp_tty_lock, flags);
314 
315 	if (buf != NULL) {
316 		__sclp_ttybuf_emit(buf);
317 	}
318 }
319 
320 /*
321  * Write a string to the sclp tty.
322  */
323 static void
324 sclp_tty_write_string(const unsigned char *str, int count)
325 {
326 	unsigned long flags;
327 	void *page;
328 	int written;
329 	struct sclp_buffer *buf;
330 
331 	if (count <= 0)
332 		return;
333 	spin_lock_irqsave(&sclp_tty_lock, flags);
334 	do {
335 		/* Create a sclp output buffer if none exists yet */
336 		if (sclp_ttybuf == NULL) {
337 			while (list_empty(&sclp_tty_pages)) {
338 				spin_unlock_irqrestore(&sclp_tty_lock, flags);
339 				if (in_interrupt())
340 					sclp_sync_wait();
341 				else
342 					wait_event(sclp_tty_waitq,
343 						!list_empty(&sclp_tty_pages));
344 				spin_lock_irqsave(&sclp_tty_lock, flags);
345 			}
346 			page = sclp_tty_pages.next;
347 			list_del((struct list_head *) page);
348 			sclp_ttybuf = sclp_make_buffer(page,
349 						       sclp_ioctls.columns,
350 						       sclp_ioctls.htab);
351 		}
352 		/* try to write the string to the current output buffer */
353 		written = sclp_write(sclp_ttybuf, str, count);
354 		if (written == count)
355 			break;
356 		/*
357 		 * Not all characters could be written to the current
358 		 * output buffer. Emit the buffer, create a new buffer
359 		 * and then output the rest of the string.
360 		 */
361 		buf = sclp_ttybuf;
362 		sclp_ttybuf = NULL;
363 		spin_unlock_irqrestore(&sclp_tty_lock, flags);
364 		__sclp_ttybuf_emit(buf);
365 		spin_lock_irqsave(&sclp_tty_lock, flags);
366 		str += written;
367 		count -= written;
368 	} while (count > 0);
369 	/* Setup timer to output current console buffer after 1/10 second */
370 	if (sclp_ioctls.final_nl) {
371 		if (sclp_ttybuf != NULL &&
372 		    sclp_chars_in_buffer(sclp_ttybuf) != 0 &&
373 		    !timer_pending(&sclp_tty_timer)) {
374 			init_timer(&sclp_tty_timer);
375 			sclp_tty_timer.function = sclp_tty_timeout;
376 			sclp_tty_timer.data = 0UL;
377 			sclp_tty_timer.expires = jiffies + HZ/10;
378 			add_timer(&sclp_tty_timer);
379 		}
380 	} else {
381 		if (sclp_ttybuf != NULL &&
382 		    sclp_chars_in_buffer(sclp_ttybuf) != 0) {
383 			buf = sclp_ttybuf;
384 			sclp_ttybuf = NULL;
385 			spin_unlock_irqrestore(&sclp_tty_lock, flags);
386 			__sclp_ttybuf_emit(buf);
387 			spin_lock_irqsave(&sclp_tty_lock, flags);
388 		}
389 	}
390 	spin_unlock_irqrestore(&sclp_tty_lock, flags);
391 }
392 
393 /*
394  * This routine is called by the kernel to write a series of characters to the
395  * tty device. The characters may come from user space or kernel space. This
396  * routine will return the number of characters actually accepted for writing.
397  */
398 static int
399 sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
400 {
401 	if (sclp_tty_chars_count > 0) {
402 		sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
403 		sclp_tty_chars_count = 0;
404 	}
405 	sclp_tty_write_string(buf, count);
406 	return count;
407 }
408 
409 /*
410  * This routine is called by the kernel to write a single character to the tty
411  * device. If the kernel uses this routine, it must call the flush_chars()
412  * routine (if defined) when it is done stuffing characters into the driver.
413  *
414  * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver.
415  * If the given character is a '\n' the contents of the SCLP write buffer
416  * - including previous characters from sclp_tty_put_char() and strings from
417  * sclp_write() without final '\n' - will be written.
418  */
419 static void
420 sclp_tty_put_char(struct tty_struct *tty, unsigned char ch)
421 {
422 	sclp_tty_chars[sclp_tty_chars_count++] = ch;
423 	if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) {
424 		sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
425 		sclp_tty_chars_count = 0;
426 	}
427 }
428 
429 /*
430  * This routine is called by the kernel after it has written a series of
431  * characters to the tty device using put_char().
432  */
433 static void
434 sclp_tty_flush_chars(struct tty_struct *tty)
435 {
436 	if (sclp_tty_chars_count > 0) {
437 		sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
438 		sclp_tty_chars_count = 0;
439 	}
440 }
441 
442 /*
443  * This routine returns the number of characters in the write buffer of the
444  * SCLP driver. The provided number includes all characters that are stored
445  * in the SCCB (will be written next time the SCLP is not busy) as well as
446  * characters in the write buffer (will not be written as long as there is a
447  * final line feed missing).
448  */
449 static int
450 sclp_tty_chars_in_buffer(struct tty_struct *tty)
451 {
452 	unsigned long flags;
453 	struct list_head *l;
454 	struct sclp_buffer *t;
455 	int count;
456 
457 	spin_lock_irqsave(&sclp_tty_lock, flags);
458 	count = 0;
459 	if (sclp_ttybuf != NULL)
460 		count = sclp_chars_in_buffer(sclp_ttybuf);
461 	list_for_each(l, &sclp_tty_outqueue) {
462 		t = list_entry(l, struct sclp_buffer, list);
463 		count += sclp_chars_in_buffer(t);
464 	}
465 	spin_unlock_irqrestore(&sclp_tty_lock, flags);
466 	return count;
467 }
468 
469 /*
470  * removes all content from buffers of low level driver
471  */
472 static void
473 sclp_tty_flush_buffer(struct tty_struct *tty)
474 {
475 	if (sclp_tty_chars_count > 0) {
476 		sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
477 		sclp_tty_chars_count = 0;
478 	}
479 }
480 
481 /*
482  * push input to tty
483  */
484 static void
485 sclp_tty_input(unsigned char* buf, unsigned int count)
486 {
487 	unsigned int cchar;
488 
489 	/*
490 	 * If this tty driver is currently closed
491 	 * then throw the received input away.
492 	 */
493 	if (sclp_tty == NULL)
494 		return;
495 	cchar = ctrlchar_handle(buf, count, sclp_tty);
496 	switch (cchar & CTRLCHAR_MASK) {
497 	case CTRLCHAR_SYSRQ:
498 		break;
499 	case CTRLCHAR_CTRL:
500 		tty_insert_flip_char(sclp_tty, cchar, TTY_NORMAL);
501 		tty_flip_buffer_push(sclp_tty);
502 		break;
503 	case CTRLCHAR_NONE:
504 		/* send (normal) input to line discipline */
505 		if (count < 2 ||
506 		    (strncmp((const char *) buf + count - 2, "^n", 2) &&
507 		     strncmp((const char *) buf + count - 2, "\252n", 2))) {
508 			/* add the auto \n */
509 			tty_insert_flip_string(sclp_tty, buf, count);
510 			tty_insert_flip_char(sclp_tty, '\n', TTY_NORMAL);
511 		} else
512 			tty_insert_flip_string(sclp_tty, buf, count - 2);
513 		tty_flip_buffer_push(sclp_tty);
514 		break;
515 	}
516 }
517 
518 /*
519  * get a EBCDIC string in upper/lower case,
520  * find out characters in lower/upper case separated by a special character,
521  * modifiy original string,
522  * returns length of resulting string
523  */
524 static int
525 sclp_switch_cases(unsigned char *buf, int count,
526 		  unsigned char delim, int tolower)
527 {
528 	unsigned char *ip, *op;
529 	int toggle;
530 
531 	/* initially changing case is off */
532 	toggle = 0;
533 	ip = op = buf;
534 	while (count-- > 0) {
535 		/* compare with special character */
536 		if (*ip == delim) {
537 			/* followed by another special character? */
538 			if (count && ip[1] == delim) {
539 				/*
540 				 * ... then put a single copy of the special
541 				 * character to the output string
542 				 */
543 				*op++ = *ip++;
544 				count--;
545 			} else
546 				/*
547 				 * ... special character follower by a normal
548 				 * character toggles the case change behaviour
549 				 */
550 				toggle = ~toggle;
551 			/* skip special character */
552 			ip++;
553 		} else
554 			/* not the special character */
555 			if (toggle)
556 				/* but case switching is on */
557 				if (tolower)
558 					/* switch to uppercase */
559 					*op++ = _ebc_toupper[(int) *ip++];
560 				else
561 					/* switch to lowercase */
562 					*op++ = _ebc_tolower[(int) *ip++];
563 			else
564 				/* no case switching, copy the character */
565 				*op++ = *ip++;
566 	}
567 	/* return length of reformatted string. */
568 	return op - buf;
569 }
570 
571 static void
572 sclp_get_input(unsigned char *start, unsigned char *end)
573 {
574 	int count;
575 
576 	count = end - start;
577 	/*
578 	 * if set in ioctl convert EBCDIC to lower case
579 	 * (modify original input in SCCB)
580 	 */
581 	if (sclp_ioctls.tolower)
582 		EBC_TOLOWER(start, count);
583 
584 	/*
585 	 * if set in ioctl find out characters in lower or upper case
586 	 * (depends on current case) separated by a special character,
587 	 * works on EBCDIC
588 	 */
589 	if (sclp_ioctls.delim)
590 		count = sclp_switch_cases(start, count,
591 					  sclp_ioctls.delim,
592 					  sclp_ioctls.tolower);
593 
594 	/* convert EBCDIC to ASCII (modify original input in SCCB) */
595 	sclp_ebcasc_str(start, count);
596 
597 	/* if set in ioctl write operators input to console  */
598 	if (sclp_ioctls.echo)
599 		sclp_tty_write(sclp_tty, start, count);
600 
601 	/* transfer input to high level driver */
602 	sclp_tty_input(start, count);
603 }
604 
605 static inline struct gds_vector *
606 find_gds_vector(struct gds_vector *start, struct gds_vector *end, u16 id)
607 {
608 	struct gds_vector *vec;
609 
610 	for (vec = start; vec < end; vec = (void *) vec + vec->length)
611 		if (vec->gds_id == id)
612 			return vec;
613 	return NULL;
614 }
615 
616 static inline struct gds_subvector *
617 find_gds_subvector(struct gds_subvector *start,
618 		   struct gds_subvector *end, u8 key)
619 {
620 	struct gds_subvector *subvec;
621 
622 	for (subvec = start; subvec < end;
623 	     subvec = (void *) subvec + subvec->length)
624 		if (subvec->key == key)
625 			return subvec;
626 	return NULL;
627 }
628 
629 static inline void
630 sclp_eval_selfdeftextmsg(struct gds_subvector *start,
631 			 struct gds_subvector *end)
632 {
633 	struct gds_subvector *subvec;
634 
635 	subvec = start;
636 	while (subvec < end) {
637 		subvec = find_gds_subvector(subvec, end, 0x30);
638 		if (!subvec)
639 			break;
640 		sclp_get_input((unsigned char *)(subvec + 1),
641 			       (unsigned char *) subvec + subvec->length);
642 		subvec = (void *) subvec + subvec->length;
643 	}
644 }
645 
646 static inline void
647 sclp_eval_textcmd(struct gds_subvector *start,
648 		  struct gds_subvector *end)
649 {
650 	struct gds_subvector *subvec;
651 
652 	subvec = start;
653 	while (subvec < end) {
654 		subvec = find_gds_subvector(subvec, end,
655 					    GDS_KEY_SelfDefTextMsg);
656 		if (!subvec)
657 			break;
658 		sclp_eval_selfdeftextmsg((struct gds_subvector *)(subvec + 1),
659 					 (void *)subvec + subvec->length);
660 		subvec = (void *) subvec + subvec->length;
661 	}
662 }
663 
664 static inline void
665 sclp_eval_cpmsu(struct gds_vector *start, struct gds_vector *end)
666 {
667 	struct gds_vector *vec;
668 
669 	vec = start;
670 	while (vec < end) {
671 		vec = find_gds_vector(vec, end, GDS_ID_TextCmd);
672 		if (!vec)
673 			break;
674 		sclp_eval_textcmd((struct gds_subvector *)(vec + 1),
675 				  (void *) vec + vec->length);
676 		vec = (void *) vec + vec->length;
677 	}
678 }
679 
680 
681 static inline void
682 sclp_eval_mdsmu(struct gds_vector *start, void *end)
683 {
684 	struct gds_vector *vec;
685 
686 	vec = find_gds_vector(start, end, GDS_ID_CPMSU);
687 	if (vec)
688 		sclp_eval_cpmsu(vec + 1, (void *) vec + vec->length);
689 }
690 
691 static void
692 sclp_tty_receiver(struct evbuf_header *evbuf)
693 {
694 	struct gds_vector *start, *end, *vec;
695 
696 	start = (struct gds_vector *)(evbuf + 1);
697 	end = (void *) evbuf + evbuf->length;
698 	vec = find_gds_vector(start, end, GDS_ID_MDSMU);
699 	if (vec)
700 		sclp_eval_mdsmu(vec + 1, (void *) vec + vec->length);
701 }
702 
703 static void
704 sclp_tty_state_change(struct sclp_register *reg)
705 {
706 }
707 
708 static struct sclp_register sclp_input_event =
709 {
710 	.receive_mask = EvTyp_OpCmd_Mask | EvTyp_PMsgCmd_Mask,
711 	.state_change_fn = sclp_tty_state_change,
712 	.receiver_fn = sclp_tty_receiver
713 };
714 
715 static struct tty_operations sclp_ops = {
716 	.open = sclp_tty_open,
717 	.close = sclp_tty_close,
718 	.write = sclp_tty_write,
719 	.put_char = sclp_tty_put_char,
720 	.flush_chars = sclp_tty_flush_chars,
721 	.write_room = sclp_tty_write_room,
722 	.chars_in_buffer = sclp_tty_chars_in_buffer,
723 	.flush_buffer = sclp_tty_flush_buffer,
724 	.ioctl = sclp_tty_ioctl,
725 };
726 
727 int __init
728 sclp_tty_init(void)
729 {
730 	struct tty_driver *driver;
731 	void *page;
732 	int i;
733 	int rc;
734 
735 	if (!CONSOLE_IS_SCLP)
736 		return 0;
737 	driver = alloc_tty_driver(1);
738 	if (!driver)
739 		return -ENOMEM;
740 
741 	rc = sclp_rw_init();
742 	if (rc) {
743 		printk(KERN_ERR SCLP_TTY_PRINT_HEADER
744 		       "could not register tty - "
745 		       "sclp_rw_init returned %d\n", rc);
746 		put_tty_driver(driver);
747 		return rc;
748 	}
749 	/* Allocate pages for output buffering */
750 	INIT_LIST_HEAD(&sclp_tty_pages);
751 	for (i = 0; i < MAX_KMEM_PAGES; i++) {
752 		page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
753 		if (page == NULL) {
754 			put_tty_driver(driver);
755 			return -ENOMEM;
756 		}
757 		list_add_tail((struct list_head *) page, &sclp_tty_pages);
758 	}
759 	INIT_LIST_HEAD(&sclp_tty_outqueue);
760 	spin_lock_init(&sclp_tty_lock);
761 	init_waitqueue_head(&sclp_tty_waitq);
762 	init_timer(&sclp_tty_timer);
763 	sclp_ttybuf = NULL;
764 	sclp_tty_buffer_count = 0;
765 	if (MACHINE_IS_VM) {
766 		/*
767 		 * save 4 characters for the CPU number
768 		 * written at start of each line by VM/CP
769 		 */
770 		sclp_ioctls_init.columns = 76;
771 		/* case input lines to lowercase */
772 		sclp_ioctls_init.tolower = 1;
773 	}
774 	sclp_ioctls = sclp_ioctls_init;
775 	sclp_tty_chars_count = 0;
776 	sclp_tty = NULL;
777 
778 	rc = sclp_register(&sclp_input_event);
779 	if (rc) {
780 		put_tty_driver(driver);
781 		return rc;
782 	}
783 
784 	driver->owner = THIS_MODULE;
785 	driver->driver_name = "sclp_line";
786 	driver->name = "sclp_line";
787 	driver->major = TTY_MAJOR;
788 	driver->minor_start = 64;
789 	driver->type = TTY_DRIVER_TYPE_SYSTEM;
790 	driver->subtype = SYSTEM_TYPE_TTY;
791 	driver->init_termios = tty_std_termios;
792 	driver->init_termios.c_iflag = IGNBRK | IGNPAR;
793 	driver->init_termios.c_oflag = ONLCR | XTABS;
794 	driver->init_termios.c_lflag = ISIG | ECHO;
795 	driver->flags = TTY_DRIVER_REAL_RAW;
796 	tty_set_operations(driver, &sclp_ops);
797 	rc = tty_register_driver(driver);
798 	if (rc) {
799 		printk(KERN_ERR SCLP_TTY_PRINT_HEADER
800 		       "could not register tty - "
801 		       "tty_register_driver returned %d\n", rc);
802 		put_tty_driver(driver);
803 		return rc;
804 	}
805 	sclp_tty_driver = driver;
806 	return 0;
807 }
808 module_init(sclp_tty_init);
809