Merge branch 'for-linus' of git://git390.osdl.marist.edu/pub/scm/linux-2.6
[linux-2.6] / 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/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 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         void *page;
259
260         do {
261                 page = sclp_unmake_buffer(buffer);
262                 spin_lock_irqsave(&sclp_tty_lock, flags);
263                 /* Remove buffer from outqueue */
264                 list_del(&buffer->list);
265                 sclp_tty_buffer_count--;
266                 list_add_tail((struct list_head *) page, &sclp_tty_pages);
267                 /* Check if there is a pending buffer on the out queue. */
268                 buffer = NULL;
269                 if (!list_empty(&sclp_tty_outqueue))
270                         buffer = list_entry(sclp_tty_outqueue.next,
271                                             struct sclp_buffer, list);
272                 spin_unlock_irqrestore(&sclp_tty_lock, flags);
273         } while (buffer && sclp_emit_buffer(buffer, sclp_ttybuf_callback));
274         wake_up(&sclp_tty_waitq);
275         /* check if the tty needs a wake up call */
276         if (sclp_tty != NULL) {
277                 tty_wakeup(sclp_tty);
278         }
279 }
280
281 static inline void
282 __sclp_ttybuf_emit(struct sclp_buffer *buffer)
283 {
284         unsigned long flags;
285         int count;
286         int rc;
287
288         spin_lock_irqsave(&sclp_tty_lock, flags);
289         list_add_tail(&buffer->list, &sclp_tty_outqueue);
290         count = sclp_tty_buffer_count++;
291         spin_unlock_irqrestore(&sclp_tty_lock, flags);
292         if (count)
293                 return;
294         rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback);
295         if (rc)
296                 sclp_ttybuf_callback(buffer, rc);
297 }
298
299 /*
300  * When this routine is called from the timer then we flush the
301  * temporary write buffer.
302  */
303 static void
304 sclp_tty_timeout(unsigned long data)
305 {
306         unsigned long flags;
307         struct sclp_buffer *buf;
308
309         spin_lock_irqsave(&sclp_tty_lock, flags);
310         buf = sclp_ttybuf;
311         sclp_ttybuf = NULL;
312         spin_unlock_irqrestore(&sclp_tty_lock, flags);
313
314         if (buf != NULL) {
315                 __sclp_ttybuf_emit(buf);
316         }
317 }
318
319 /*
320  * Write a string to the sclp tty.
321  */
322 static void
323 sclp_tty_write_string(const unsigned char *str, int count)
324 {
325         unsigned long flags;
326         void *page;
327         int written;
328         struct sclp_buffer *buf;
329
330         if (count <= 0)
331                 return;
332         spin_lock_irqsave(&sclp_tty_lock, flags);
333         do {
334                 /* Create a sclp output buffer if none exists yet */
335                 if (sclp_ttybuf == NULL) {
336                         while (list_empty(&sclp_tty_pages)) {
337                                 spin_unlock_irqrestore(&sclp_tty_lock, flags);
338                                 if (in_interrupt())
339                                         sclp_sync_wait();
340                                 else
341                                         wait_event(sclp_tty_waitq,
342                                                 !list_empty(&sclp_tty_pages));
343                                 spin_lock_irqsave(&sclp_tty_lock, flags);
344                         }
345                         page = sclp_tty_pages.next;
346                         list_del((struct list_head *) page);
347                         sclp_ttybuf = sclp_make_buffer(page,
348                                                        sclp_ioctls.columns,
349                                                        sclp_ioctls.htab);
350                 }
351                 /* try to write the string to the current output buffer */
352                 written = sclp_write(sclp_ttybuf, str, count);
353                 if (written == count)
354                         break;
355                 /*
356                  * Not all characters could be written to the current
357                  * output buffer. Emit the buffer, create a new buffer
358                  * and then output the rest of the string.
359                  */
360                 buf = sclp_ttybuf;
361                 sclp_ttybuf = NULL;
362                 spin_unlock_irqrestore(&sclp_tty_lock, flags);
363                 __sclp_ttybuf_emit(buf);
364                 spin_lock_irqsave(&sclp_tty_lock, flags);
365                 str += written;
366                 count -= written;
367         } while (count > 0);
368         /* Setup timer to output current console buffer after 1/10 second */
369         if (sclp_ioctls.final_nl) {
370                 if (sclp_ttybuf != NULL &&
371                     sclp_chars_in_buffer(sclp_ttybuf) != 0 &&
372                     !timer_pending(&sclp_tty_timer)) {
373                         init_timer(&sclp_tty_timer);
374                         sclp_tty_timer.function = sclp_tty_timeout;
375                         sclp_tty_timer.data = 0UL;
376                         sclp_tty_timer.expires = jiffies + HZ/10;
377                         add_timer(&sclp_tty_timer);
378                 }
379         } else {
380                 if (sclp_ttybuf != NULL &&
381                     sclp_chars_in_buffer(sclp_ttybuf) != 0) {
382                         buf = sclp_ttybuf;
383                         sclp_ttybuf = NULL;
384                         spin_unlock_irqrestore(&sclp_tty_lock, flags);
385                         __sclp_ttybuf_emit(buf);
386                         spin_lock_irqsave(&sclp_tty_lock, flags);
387                 }
388         }
389         spin_unlock_irqrestore(&sclp_tty_lock, flags);
390 }
391
392 /*
393  * This routine is called by the kernel to write a series of characters to the
394  * tty device. The characters may come from user space or kernel space. This
395  * routine will return the number of characters actually accepted for writing.
396  */
397 static int
398 sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
399 {
400         if (sclp_tty_chars_count > 0) {
401                 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
402                 sclp_tty_chars_count = 0;
403         }
404         sclp_tty_write_string(buf, count);
405         return count;
406 }
407
408 /*
409  * This routine is called by the kernel to write a single character to the tty
410  * device. If the kernel uses this routine, it must call the flush_chars()
411  * routine (if defined) when it is done stuffing characters into the driver.
412  *
413  * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver.
414  * If the given character is a '\n' the contents of the SCLP write buffer
415  * - including previous characters from sclp_tty_put_char() and strings from
416  * sclp_write() without final '\n' - will be written.
417  */
418 static void
419 sclp_tty_put_char(struct tty_struct *tty, unsigned char ch)
420 {
421         sclp_tty_chars[sclp_tty_chars_count++] = ch;
422         if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) {
423                 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
424                 sclp_tty_chars_count = 0;
425         }
426 }
427
428 /*
429  * This routine is called by the kernel after it has written a series of
430  * characters to the tty device using put_char().
431  */
432 static void
433 sclp_tty_flush_chars(struct tty_struct *tty)
434 {
435         if (sclp_tty_chars_count > 0) {
436                 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
437                 sclp_tty_chars_count = 0;
438         }
439 }
440
441 /*
442  * This routine returns the number of characters in the write buffer of the
443  * SCLP driver. The provided number includes all characters that are stored
444  * in the SCCB (will be written next time the SCLP is not busy) as well as
445  * characters in the write buffer (will not be written as long as there is a
446  * final line feed missing).
447  */
448 static int
449 sclp_tty_chars_in_buffer(struct tty_struct *tty)
450 {
451         unsigned long flags;
452         struct list_head *l;
453         struct sclp_buffer *t;
454         int count;
455
456         spin_lock_irqsave(&sclp_tty_lock, flags);
457         count = 0;
458         if (sclp_ttybuf != NULL)
459                 count = sclp_chars_in_buffer(sclp_ttybuf);
460         list_for_each(l, &sclp_tty_outqueue) {
461                 t = list_entry(l, struct sclp_buffer, list);
462                 count += sclp_chars_in_buffer(t);
463         }
464         spin_unlock_irqrestore(&sclp_tty_lock, flags);
465         return count;
466 }
467
468 /*
469  * removes all content from buffers of low level driver
470  */
471 static void
472 sclp_tty_flush_buffer(struct tty_struct *tty)
473 {
474         if (sclp_tty_chars_count > 0) {
475                 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
476                 sclp_tty_chars_count = 0;
477         }
478 }
479
480 /*
481  * push input to tty
482  */
483 static void
484 sclp_tty_input(unsigned char* buf, unsigned int count)
485 {
486         unsigned int cchar;
487
488         /*
489          * If this tty driver is currently closed
490          * then throw the received input away.
491          */
492         if (sclp_tty == NULL)
493                 return;
494         cchar = ctrlchar_handle(buf, count, sclp_tty);
495         switch (cchar & CTRLCHAR_MASK) {
496         case CTRLCHAR_SYSRQ:
497                 break;
498         case CTRLCHAR_CTRL:
499                 tty_insert_flip_char(sclp_tty, cchar, TTY_NORMAL);
500                 tty_flip_buffer_push(sclp_tty);
501                 break;
502         case CTRLCHAR_NONE:
503                 /* send (normal) input to line discipline */
504                 if (count < 2 ||
505                     (strncmp((const char *) buf + count - 2, "^n", 2) &&
506                      strncmp((const char *) buf + count - 2, "\252n", 2))) {
507                         /* add the auto \n */
508                         tty_insert_flip_string(sclp_tty, buf, count);
509                         tty_insert_flip_char(sclp_tty, '\n', TTY_NORMAL);
510                 } else
511                         tty_insert_flip_string(sclp_tty, buf, count - 2);
512                 tty_flip_buffer_push(sclp_tty);
513                 break;
514         }
515 }
516
517 /*
518  * get a EBCDIC string in upper/lower case,
519  * find out characters in lower/upper case separated by a special character,
520  * modifiy original string,
521  * returns length of resulting string
522  */
523 static int
524 sclp_switch_cases(unsigned char *buf, int count,
525                   unsigned char delim, int tolower)
526 {
527         unsigned char *ip, *op;
528         int toggle;
529
530         /* initially changing case is off */
531         toggle = 0;
532         ip = op = buf;
533         while (count-- > 0) {
534                 /* compare with special character */
535                 if (*ip == delim) {
536                         /* followed by another special character? */
537                         if (count && ip[1] == delim) {
538                                 /*
539                                  * ... then put a single copy of the special
540                                  * character to the output string
541                                  */
542                                 *op++ = *ip++;
543                                 count--;
544                         } else
545                                 /*
546                                  * ... special character follower by a normal
547                                  * character toggles the case change behaviour
548                                  */
549                                 toggle = ~toggle;
550                         /* skip special character */
551                         ip++;
552                 } else
553                         /* not the special character */
554                         if (toggle)
555                                 /* but case switching is on */
556                                 if (tolower)
557                                         /* switch to uppercase */
558                                         *op++ = _ebc_toupper[(int) *ip++];
559                                 else
560                                         /* switch to lowercase */
561                                         *op++ = _ebc_tolower[(int) *ip++];
562                         else
563                                 /* no case switching, copy the character */
564                                 *op++ = *ip++;
565         }
566         /* return length of reformatted string. */
567         return op - buf;
568 }
569
570 static void
571 sclp_get_input(unsigned char *start, unsigned char *end)
572 {
573         int count;
574
575         count = end - start;
576         /*
577          * if set in ioctl convert EBCDIC to lower case
578          * (modify original input in SCCB)
579          */
580         if (sclp_ioctls.tolower)
581                 EBC_TOLOWER(start, count);
582
583         /*
584          * if set in ioctl find out characters in lower or upper case
585          * (depends on current case) separated by a special character,
586          * works on EBCDIC
587          */
588         if (sclp_ioctls.delim)
589                 count = sclp_switch_cases(start, count,
590                                           sclp_ioctls.delim,
591                                           sclp_ioctls.tolower);
592
593         /* convert EBCDIC to ASCII (modify original input in SCCB) */
594         sclp_ebcasc_str(start, count);
595
596         /* if set in ioctl write operators input to console  */
597         if (sclp_ioctls.echo)
598                 sclp_tty_write(sclp_tty, start, count);
599
600         /* transfer input to high level driver */
601         sclp_tty_input(start, count);
602 }
603
604 static inline struct gds_vector *
605 find_gds_vector(struct gds_vector *start, struct gds_vector *end, u16 id)
606 {
607         struct gds_vector *vec;
608
609         for (vec = start; vec < end; vec = (void *) vec + vec->length)
610                 if (vec->gds_id == id)
611                         return vec;
612         return NULL;
613 }
614
615 static inline struct gds_subvector *
616 find_gds_subvector(struct gds_subvector *start,
617                    struct gds_subvector *end, u8 key)
618 {
619         struct gds_subvector *subvec;
620
621         for (subvec = start; subvec < end;
622              subvec = (void *) subvec + subvec->length)
623                 if (subvec->key == key)
624                         return subvec;
625         return NULL;
626 }
627
628 static inline void
629 sclp_eval_selfdeftextmsg(struct gds_subvector *start,
630                          struct gds_subvector *end)
631 {
632         struct gds_subvector *subvec;
633
634         subvec = start;
635         while (subvec < end) {
636                 subvec = find_gds_subvector(subvec, end, 0x30);
637                 if (!subvec)
638                         break;
639                 sclp_get_input((unsigned char *)(subvec + 1),
640                                (unsigned char *) subvec + subvec->length);
641                 subvec = (void *) subvec + subvec->length;
642         }
643 }
644
645 static inline void
646 sclp_eval_textcmd(struct gds_subvector *start,
647                   struct gds_subvector *end)
648 {
649         struct gds_subvector *subvec;
650
651         subvec = start;
652         while (subvec < end) {
653                 subvec = find_gds_subvector(subvec, end,
654                                             GDS_KEY_SelfDefTextMsg);
655                 if (!subvec)
656                         break;
657                 sclp_eval_selfdeftextmsg((struct gds_subvector *)(subvec + 1),
658                                          (void *)subvec + subvec->length);
659                 subvec = (void *) subvec + subvec->length;
660         }
661 }
662
663 static inline void
664 sclp_eval_cpmsu(struct gds_vector *start, struct gds_vector *end)
665 {
666         struct gds_vector *vec;
667
668         vec = start;
669         while (vec < end) {
670                 vec = find_gds_vector(vec, end, GDS_ID_TextCmd);
671                 if (!vec)
672                         break;
673                 sclp_eval_textcmd((struct gds_subvector *)(vec + 1),
674                                   (void *) vec + vec->length);
675                 vec = (void *) vec + vec->length;
676         }
677 }
678
679
680 static inline void
681 sclp_eval_mdsmu(struct gds_vector *start, void *end)
682 {
683         struct gds_vector *vec;
684
685         vec = find_gds_vector(start, end, GDS_ID_CPMSU);
686         if (vec)
687                 sclp_eval_cpmsu(vec + 1, (void *) vec + vec->length);
688 }
689
690 static void
691 sclp_tty_receiver(struct evbuf_header *evbuf)
692 {
693         struct gds_vector *start, *end, *vec;
694
695         start = (struct gds_vector *)(evbuf + 1);
696         end = (void *) evbuf + evbuf->length;
697         vec = find_gds_vector(start, end, GDS_ID_MDSMU);
698         if (vec)
699                 sclp_eval_mdsmu(vec + 1, (void *) vec + vec->length);
700 }
701
702 static void
703 sclp_tty_state_change(struct sclp_register *reg)
704 {
705 }
706
707 static struct sclp_register sclp_input_event =
708 {
709         .receive_mask = EvTyp_OpCmd_Mask | EvTyp_PMsgCmd_Mask,
710         .state_change_fn = sclp_tty_state_change,
711         .receiver_fn = sclp_tty_receiver
712 };
713
714 static const struct tty_operations sclp_ops = {
715         .open = sclp_tty_open,
716         .close = sclp_tty_close,
717         .write = sclp_tty_write,
718         .put_char = sclp_tty_put_char,
719         .flush_chars = sclp_tty_flush_chars,
720         .write_room = sclp_tty_write_room,
721         .chars_in_buffer = sclp_tty_chars_in_buffer,
722         .flush_buffer = sclp_tty_flush_buffer,
723         .ioctl = sclp_tty_ioctl,
724 };
725
726 int __init
727 sclp_tty_init(void)
728 {
729         struct tty_driver *driver;
730         void *page;
731         int i;
732         int rc;
733
734         if (!CONSOLE_IS_SCLP)
735                 return 0;
736         driver = alloc_tty_driver(1);
737         if (!driver)
738                 return -ENOMEM;
739
740         rc = sclp_rw_init();
741         if (rc) {
742                 printk(KERN_ERR SCLP_TTY_PRINT_HEADER
743                        "could not register tty - "
744                        "sclp_rw_init returned %d\n", rc);
745                 put_tty_driver(driver);
746                 return rc;
747         }
748         /* Allocate pages for output buffering */
749         INIT_LIST_HEAD(&sclp_tty_pages);
750         for (i = 0; i < MAX_KMEM_PAGES; i++) {
751                 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
752                 if (page == NULL) {
753                         put_tty_driver(driver);
754                         return -ENOMEM;
755                 }
756                 list_add_tail((struct list_head *) page, &sclp_tty_pages);
757         }
758         INIT_LIST_HEAD(&sclp_tty_outqueue);
759         spin_lock_init(&sclp_tty_lock);
760         init_waitqueue_head(&sclp_tty_waitq);
761         init_timer(&sclp_tty_timer);
762         sclp_ttybuf = NULL;
763         sclp_tty_buffer_count = 0;
764         if (MACHINE_IS_VM) {
765                 /*
766                  * save 4 characters for the CPU number
767                  * written at start of each line by VM/CP
768                  */
769                 sclp_ioctls_init.columns = 76;
770                 /* case input lines to lowercase */
771                 sclp_ioctls_init.tolower = 1;
772         }
773         sclp_ioctls = sclp_ioctls_init;
774         sclp_tty_chars_count = 0;
775         sclp_tty = NULL;
776
777         rc = sclp_register(&sclp_input_event);
778         if (rc) {
779                 put_tty_driver(driver);
780                 return rc;
781         }
782
783         driver->owner = THIS_MODULE;
784         driver->driver_name = "sclp_line";
785         driver->name = "sclp_line";
786         driver->major = TTY_MAJOR;
787         driver->minor_start = 64;
788         driver->type = TTY_DRIVER_TYPE_SYSTEM;
789         driver->subtype = SYSTEM_TYPE_TTY;
790         driver->init_termios = tty_std_termios;
791         driver->init_termios.c_iflag = IGNBRK | IGNPAR;
792         driver->init_termios.c_oflag = ONLCR | XTABS;
793         driver->init_termios.c_lflag = ISIG | ECHO;
794         driver->flags = TTY_DRIVER_REAL_RAW;
795         tty_set_operations(driver, &sclp_ops);
796         rc = tty_register_driver(driver);
797         if (rc) {
798                 printk(KERN_ERR SCLP_TTY_PRINT_HEADER
799                        "could not register tty - "
800                        "tty_register_driver returned %d\n", rc);
801                 put_tty_driver(driver);
802                 return rc;
803         }
804         sclp_tty_driver = driver;
805         return 0;
806 }
807 module_init(sclp_tty_init);