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