2 * drivers/s390/char/sclp_tty.c
3 * SCLP line mode terminal driver.
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>
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>
29 #define SCLP_TTY_PRINT_HEADER "sclp tty driver: "
32 * size of a buffer that collects single characters coming in
33 * via sclp_tty_put_char()
35 #define SCLP_TTY_BUF_SIZE 512
38 * There is exactly one SCLP terminal, so we can keep things simple
39 * and allocate all variables statically.
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;
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;
61 struct tty_driver *sclp_tty_driver;
63 static struct sclp_ioctls sclp_ioctls;
64 static struct sclp_ioctls sclp_ioctls_init =
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 */
77 /* This routine is called whenever we try to open a SCLP terminal. */
79 sclp_tty_open(struct tty_struct *tty, struct file *filp)
82 tty->driver_data = NULL;
87 /* This routine is called when the SCLP terminal is closed. */
89 sclp_tty_close(struct tty_struct *tty, struct file *filp)
96 /* execute commands to control the i/o behaviour of the SCLP tty at runtime */
98 sclp_tty_ioctl(struct tty_struct *tty, struct file * file,
99 unsigned int cmd, unsigned long arg)
106 if (tty->flags & (1 << TTY_IO_ERROR))
112 /* set width of horizontal tab */
113 if (get_user(sclp_ioctls.htab, (unsigned short __user *) arg))
119 /* get width of horizontal tab */
120 if (put_user(sclp_ioctls.htab, (unsigned short __user *) arg))
124 /* enable/disable echo of input */
125 if (get_user(sclp_ioctls.echo, (unsigned char __user *) arg))
129 /* Is echo of input enabled ? */
130 if (put_user(sclp_ioctls.echo, (unsigned char __user *) arg))
134 /* set number of columns for output */
135 if (get_user(sclp_ioctls.columns, (unsigned short __user *) arg))
141 /* get number of columns for output */
142 if (put_user(sclp_ioctls.columns, (unsigned short __user *) arg))
146 /* enable/disable writing without final new line character */
147 if (get_user(sclp_ioctls.final_nl, (signed char __user *) arg))
151 /* Is writing without final new line character enabled ? */
152 if (put_user(sclp_ioctls.final_nl, (signed char __user *) arg))
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
161 if (get_user(obuf, (unsigned int __user *) arg) == 0) {
163 sclp_ioctls.max_sccb = (obuf >> 12) + 1;
165 sclp_ioctls.max_sccb = (obuf >> 12);
170 /* get the maximum buffers size for output */
171 obuf = sclp_ioctls.max_sccb << 12;
172 if (put_user(obuf, (unsigned int __user *) arg))
176 /* get the number of buffers got from kernel at startup */
177 if (put_user(sclp_ioctls.kmem_sccb, (unsigned short __user *) arg))
181 /* enable/disable conversion from upper to lower case */
182 if (get_user(sclp_ioctls.tolower, (unsigned char __user *) arg))
186 /* Is conversion from upper to lower case of input enabled? */
187 if (put_user(sclp_ioctls.tolower, (unsigned char __user *) arg))
192 * set special character used for separating upper and
193 * lower case, 0x00 disables this feature
195 if (get_user(sclp_ioctls.delim, (unsigned char __user *) arg))
200 * get special character used for separating upper and
201 * lower case, 0x00 disables this feature
203 if (put_user(sclp_ioctls.delim, (unsigned char __user *) arg))
207 /* set initial (default) sclp ioctls */
208 sclp_ioctls = sclp_ioctls_init;
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);
221 spin_unlock_irqrestore(&sclp_tty_lock, flags);
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
236 sclp_tty_write_room (struct tty_struct *tty)
242 spin_lock_irqsave(&sclp_tty_lock, flags);
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);
253 sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc)
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. */
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);
280 __sclp_ttybuf_emit(struct sclp_buffer *buffer)
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);
292 rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback);
294 sclp_ttybuf_callback(buffer, rc);
298 * When this routine is called from the timer then we flush the
299 * temporary write buffer.
302 sclp_tty_timeout(unsigned long data)
305 struct sclp_buffer *buf;
307 spin_lock_irqsave(&sclp_tty_lock, flags);
310 spin_unlock_irqrestore(&sclp_tty_lock, flags);
313 __sclp_ttybuf_emit(buf);
318 * Write a string to the sclp tty.
321 sclp_tty_write_string(const unsigned char *str, int count)
326 struct sclp_buffer *buf;
330 spin_lock_irqsave(&sclp_tty_lock, flags);
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);
339 wait_event(sclp_tty_waitq,
340 !list_empty(&sclp_tty_pages));
341 spin_lock_irqsave(&sclp_tty_lock, flags);
343 page = sclp_tty_pages.next;
344 list_del((struct list_head *) page);
345 sclp_ttybuf = sclp_make_buffer(page,
349 /* try to write the string to the current output buffer */
350 written = sclp_write(sclp_ttybuf, str, count);
351 if (written == count)
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.
360 spin_unlock_irqrestore(&sclp_tty_lock, flags);
361 __sclp_ttybuf_emit(buf);
362 spin_lock_irqsave(&sclp_tty_lock, flags);
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);
378 if (sclp_ttybuf != NULL &&
379 sclp_chars_in_buffer(sclp_ttybuf) != 0) {
382 spin_unlock_irqrestore(&sclp_tty_lock, flags);
383 __sclp_ttybuf_emit(buf);
384 spin_lock_irqsave(&sclp_tty_lock, flags);
387 spin_unlock_irqrestore(&sclp_tty_lock, flags);
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.
396 sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
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;
402 sclp_tty_write_string(buf, count);
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.
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.
417 sclp_tty_put_char(struct tty_struct *tty, unsigned char ch)
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;
427 * This routine is called by the kernel after it has written a series of
428 * characters to the tty device using put_char().
431 sclp_tty_flush_chars(struct tty_struct *tty)
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;
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).
447 sclp_tty_chars_in_buffer(struct tty_struct *tty)
451 struct sclp_buffer *t;
454 spin_lock_irqsave(&sclp_tty_lock, flags);
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);
462 spin_unlock_irqrestore(&sclp_tty_lock, flags);
467 * removes all content from buffers of low level driver
470 sclp_tty_flush_buffer(struct tty_struct *tty)
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;
482 sclp_tty_input(unsigned char* buf, unsigned int count)
487 * If this tty driver is currently closed
488 * then throw the received input away.
490 if (sclp_tty == NULL)
492 cchar = ctrlchar_handle(buf, count, sclp_tty);
493 switch (cchar & CTRLCHAR_MASK) {
497 tty_insert_flip_char(sclp_tty, cchar, TTY_NORMAL);
498 tty_flip_buffer_push(sclp_tty);
501 /* send (normal) input to line discipline */
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);
509 tty_insert_flip_string(sclp_tty, buf, count - 2);
510 tty_flip_buffer_push(sclp_tty);
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
522 sclp_switch_cases(unsigned char *buf, int count,
523 unsigned char delim, int tolower)
525 unsigned char *ip, *op;
528 /* initially changing case is off */
531 while (count-- > 0) {
532 /* compare with special character */
534 /* followed by another special character? */
535 if (count && ip[1] == delim) {
537 * ... then put a single copy of the special
538 * character to the output string
544 * ... special character follower by a normal
545 * character toggles the case change behaviour
548 /* skip special character */
551 /* not the special character */
553 /* but case switching is on */
555 /* switch to uppercase */
556 *op++ = _ebc_toupper[(int) *ip++];
558 /* switch to lowercase */
559 *op++ = _ebc_tolower[(int) *ip++];
561 /* no case switching, copy the character */
564 /* return length of reformatted string. */
569 sclp_get_input(unsigned char *start, unsigned char *end)
575 * if set in ioctl convert EBCDIC to lower case
576 * (modify original input in SCCB)
578 if (sclp_ioctls.tolower)
579 EBC_TOLOWER(start, count);
582 * if set in ioctl find out characters in lower or upper case
583 * (depends on current case) separated by a special character,
586 if (sclp_ioctls.delim)
587 count = sclp_switch_cases(start, count,
589 sclp_ioctls.tolower);
591 /* convert EBCDIC to ASCII (modify original input in SCCB) */
592 sclp_ebcasc_str(start, count);
594 /* if set in ioctl write operators input to console */
595 if (sclp_ioctls.echo)
596 sclp_tty_write(sclp_tty, start, count);
598 /* transfer input to high level driver */
599 sclp_tty_input(start, count);
602 static inline struct gds_vector *
603 find_gds_vector(struct gds_vector *start, struct gds_vector *end, u16 id)
605 struct gds_vector *vec;
607 for (vec = start; vec < end; vec = (void *) vec + vec->length)
608 if (vec->gds_id == id)
613 static inline struct gds_subvector *
614 find_gds_subvector(struct gds_subvector *start,
615 struct gds_subvector *end, u8 key)
617 struct gds_subvector *subvec;
619 for (subvec = start; subvec < end;
620 subvec = (void *) subvec + subvec->length)
621 if (subvec->key == key)
627 sclp_eval_selfdeftextmsg(struct gds_subvector *start,
628 struct gds_subvector *end)
630 struct gds_subvector *subvec;
633 while (subvec < end) {
634 subvec = find_gds_subvector(subvec, end, 0x30);
637 sclp_get_input((unsigned char *)(subvec + 1),
638 (unsigned char *) subvec + subvec->length);
639 subvec = (void *) subvec + subvec->length;
644 sclp_eval_textcmd(struct gds_subvector *start,
645 struct gds_subvector *end)
647 struct gds_subvector *subvec;
650 while (subvec < end) {
651 subvec = find_gds_subvector(subvec, end,
652 GDS_KEY_SelfDefTextMsg);
655 sclp_eval_selfdeftextmsg((struct gds_subvector *)(subvec + 1),
656 (void *)subvec + subvec->length);
657 subvec = (void *) subvec + subvec->length;
662 sclp_eval_cpmsu(struct gds_vector *start, struct gds_vector *end)
664 struct gds_vector *vec;
668 vec = find_gds_vector(vec, end, GDS_ID_TextCmd);
671 sclp_eval_textcmd((struct gds_subvector *)(vec + 1),
672 (void *) vec + vec->length);
673 vec = (void *) vec + vec->length;
679 sclp_eval_mdsmu(struct gds_vector *start, void *end)
681 struct gds_vector *vec;
683 vec = find_gds_vector(start, end, GDS_ID_CPMSU);
685 sclp_eval_cpmsu(vec + 1, (void *) vec + vec->length);
689 sclp_tty_receiver(struct evbuf_header *evbuf)
691 struct gds_vector *start, *end, *vec;
693 start = (struct gds_vector *)(evbuf + 1);
694 end = (void *) evbuf + evbuf->length;
695 vec = find_gds_vector(start, end, GDS_ID_MDSMU);
697 sclp_eval_mdsmu(vec + 1, (void *) vec + vec->length);
701 sclp_tty_state_change(struct sclp_register *reg)
705 static struct sclp_register sclp_input_event =
707 .receive_mask = EvTyp_OpCmd_Mask | EvTyp_PMsgCmd_Mask,
708 .state_change_fn = sclp_tty_state_change,
709 .receiver_fn = sclp_tty_receiver
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,
727 struct tty_driver *driver;
732 if (!CONSOLE_IS_SCLP)
734 driver = alloc_tty_driver(1);
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);
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);
751 put_tty_driver(driver);
754 list_add_tail((struct list_head *) page, &sclp_tty_pages);
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);
761 sclp_tty_buffer_count = 0;
764 * save 4 characters for the CPU number
765 * written at start of each line by VM/CP
767 sclp_ioctls_init.columns = 76;
768 /* case input lines to lowercase */
769 sclp_ioctls_init.tolower = 1;
771 sclp_ioctls = sclp_ioctls_init;
772 sclp_tty_chars_count = 0;
775 rc = sclp_register(&sclp_input_event);
777 put_tty_driver(driver);
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);
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);
802 sclp_tty_driver = driver;
805 module_init(sclp_tty_init);