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