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/wait.h>
17 #include <linux/slab.h>
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <asm/uaccess.h>
28 #define SCLP_TTY_PRINT_HEADER "sclp tty driver: "
31 * size of a buffer that collects single characters coming in
32 * via sclp_tty_put_char()
34 #define SCLP_TTY_BUF_SIZE 512
37 * There is exactly one SCLP terminal, so we can keep things simple
38 * and allocate all variables statically.
41 /* Lock to guard over changes to global variables. */
42 static spinlock_t sclp_tty_lock;
43 /* List of free pages that can be used for console output buffering. */
44 static struct list_head sclp_tty_pages;
45 /* List of full struct sclp_buffer structures ready for output. */
46 static struct list_head sclp_tty_outqueue;
47 /* Counter how many buffers are emitted. */
48 static int sclp_tty_buffer_count;
49 /* Pointer to current console buffer. */
50 static struct sclp_buffer *sclp_ttybuf;
51 /* Timer for delayed output of console messages. */
52 static struct timer_list sclp_tty_timer;
53 /* Waitqueue to wait for buffers to get empty. */
54 static wait_queue_head_t sclp_tty_waitq;
56 static struct tty_struct *sclp_tty;
57 static unsigned char sclp_tty_chars[SCLP_TTY_BUF_SIZE];
58 static unsigned short int sclp_tty_chars_count;
60 struct tty_driver *sclp_tty_driver;
62 static struct sclp_ioctls sclp_ioctls;
63 static struct sclp_ioctls sclp_ioctls_init =
65 8, /* 1 hor. tab. = 8 spaces */
66 0, /* no echo of input by this driver */
67 80, /* 80 characters/line */
68 1, /* write after 1/10 s without final new line */
69 MAX_KMEM_PAGES, /* quick fix: avoid __alloc_pages */
70 MAX_KMEM_PAGES, /* take 32/64 pages from kernel memory, */
71 0, /* do not convert to lower case */
72 0x6c /* to seprate upper and lower case */
76 /* This routine is called whenever we try to open a SCLP terminal. */
78 sclp_tty_open(struct tty_struct *tty, struct file *filp)
81 tty->driver_data = NULL;
86 /* This routine is called when the SCLP terminal is closed. */
88 sclp_tty_close(struct tty_struct *tty, struct file *filp)
95 /* execute commands to control the i/o behaviour of the SCLP tty at runtime */
97 sclp_tty_ioctl(struct tty_struct *tty, struct file * file,
98 unsigned int cmd, unsigned long arg)
105 if (tty->flags & (1 << TTY_IO_ERROR))
111 /* set width of horizontal tab */
112 if (get_user(sclp_ioctls.htab, (unsigned short __user *) arg))
118 /* get width of horizontal tab */
119 if (put_user(sclp_ioctls.htab, (unsigned short __user *) arg))
123 /* enable/disable echo of input */
124 if (get_user(sclp_ioctls.echo, (unsigned char __user *) arg))
128 /* Is echo of input enabled ? */
129 if (put_user(sclp_ioctls.echo, (unsigned char __user *) arg))
133 /* set number of columns for output */
134 if (get_user(sclp_ioctls.columns, (unsigned short __user *) arg))
140 /* get number of columns for output */
141 if (put_user(sclp_ioctls.columns, (unsigned short __user *) arg))
145 /* enable/disable writing without final new line character */
146 if (get_user(sclp_ioctls.final_nl, (signed char __user *) arg))
150 /* Is writing without final new line character enabled ? */
151 if (put_user(sclp_ioctls.final_nl, (signed char __user *) arg))
156 * set the maximum buffers size for output, will be rounded
157 * up to next 4kB boundary and stored as number of SCCBs
158 * (4kB Buffers) limitation: 256 x 4kB
160 if (get_user(obuf, (unsigned int __user *) arg) == 0) {
162 sclp_ioctls.max_sccb = (obuf >> 12) + 1;
164 sclp_ioctls.max_sccb = (obuf >> 12);
169 /* get the maximum buffers size for output */
170 obuf = sclp_ioctls.max_sccb << 12;
171 if (put_user(obuf, (unsigned int __user *) arg))
175 /* get the number of buffers got from kernel at startup */
176 if (put_user(sclp_ioctls.kmem_sccb, (unsigned short __user *) arg))
180 /* enable/disable conversion from upper to lower case */
181 if (get_user(sclp_ioctls.tolower, (unsigned char __user *) arg))
185 /* Is conversion from upper to lower case of input enabled? */
186 if (put_user(sclp_ioctls.tolower, (unsigned char __user *) arg))
191 * set special character used for separating upper and
192 * lower case, 0x00 disables this feature
194 if (get_user(sclp_ioctls.delim, (unsigned char __user *) arg))
199 * get special character used for separating upper and
200 * lower case, 0x00 disables this feature
202 if (put_user(sclp_ioctls.delim, (unsigned char __user *) arg))
206 /* set initial (default) sclp ioctls */
207 sclp_ioctls = sclp_ioctls_init;
215 spin_lock_irqsave(&sclp_tty_lock, flags);
216 if (sclp_ttybuf != NULL) {
217 sclp_set_htab(sclp_ttybuf, sclp_ioctls.htab);
218 sclp_set_columns(sclp_ttybuf, sclp_ioctls.columns);
220 spin_unlock_irqrestore(&sclp_tty_lock, flags);
226 * This routine returns the numbers of characters the tty driver
227 * will accept for queuing to be written. This number is subject
228 * to change as output buffers get emptied, or if the output flow
229 * control is acted. This is not an exact number because not every
230 * character needs the same space in the sccb. The worst case is
231 * a string of newlines. Every newlines creates a new mto which
235 sclp_tty_write_room (struct tty_struct *tty)
241 spin_lock_irqsave(&sclp_tty_lock, flags);
243 if (sclp_ttybuf != NULL)
244 count = sclp_buffer_space(sclp_ttybuf) / sizeof(struct mto);
245 list_for_each(l, &sclp_tty_pages)
246 count += NR_EMPTY_MTO_PER_SCCB;
247 spin_unlock_irqrestore(&sclp_tty_lock, flags);
252 sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc)
258 page = sclp_unmake_buffer(buffer);
259 spin_lock_irqsave(&sclp_tty_lock, flags);
260 /* Remove buffer from outqueue */
261 list_del(&buffer->list);
262 sclp_tty_buffer_count--;
263 list_add_tail((struct list_head *) page, &sclp_tty_pages);
264 /* Check if there is a pending buffer on the out queue. */
266 if (!list_empty(&sclp_tty_outqueue))
267 buffer = list_entry(sclp_tty_outqueue.next,
268 struct sclp_buffer, list);
269 spin_unlock_irqrestore(&sclp_tty_lock, flags);
270 } while (buffer && sclp_emit_buffer(buffer, sclp_ttybuf_callback));
271 wake_up(&sclp_tty_waitq);
272 /* check if the tty needs a wake up call */
273 if (sclp_tty != NULL) {
274 tty_wakeup(sclp_tty);
279 __sclp_ttybuf_emit(struct sclp_buffer *buffer)
285 spin_lock_irqsave(&sclp_tty_lock, flags);
286 list_add_tail(&buffer->list, &sclp_tty_outqueue);
287 count = sclp_tty_buffer_count++;
288 spin_unlock_irqrestore(&sclp_tty_lock, flags);
291 rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback);
293 sclp_ttybuf_callback(buffer, rc);
297 * When this routine is called from the timer then we flush the
298 * temporary write buffer.
301 sclp_tty_timeout(unsigned long data)
304 struct sclp_buffer *buf;
306 spin_lock_irqsave(&sclp_tty_lock, flags);
309 spin_unlock_irqrestore(&sclp_tty_lock, flags);
312 __sclp_ttybuf_emit(buf);
317 * Write a string to the sclp tty.
320 sclp_tty_write_string(const unsigned char *str, int count)
325 struct sclp_buffer *buf;
329 spin_lock_irqsave(&sclp_tty_lock, flags);
331 /* Create a sclp output buffer if none exists yet */
332 if (sclp_ttybuf == NULL) {
333 while (list_empty(&sclp_tty_pages)) {
334 spin_unlock_irqrestore(&sclp_tty_lock, flags);
338 wait_event(sclp_tty_waitq,
339 !list_empty(&sclp_tty_pages));
340 spin_lock_irqsave(&sclp_tty_lock, flags);
342 page = sclp_tty_pages.next;
343 list_del((struct list_head *) page);
344 sclp_ttybuf = sclp_make_buffer(page,
348 /* try to write the string to the current output buffer */
349 written = sclp_write(sclp_ttybuf, str, count);
350 if (written == count)
353 * Not all characters could be written to the current
354 * output buffer. Emit the buffer, create a new buffer
355 * and then output the rest of the string.
359 spin_unlock_irqrestore(&sclp_tty_lock, flags);
360 __sclp_ttybuf_emit(buf);
361 spin_lock_irqsave(&sclp_tty_lock, flags);
365 /* Setup timer to output current console buffer after 1/10 second */
366 if (sclp_ioctls.final_nl) {
367 if (sclp_ttybuf != NULL &&
368 sclp_chars_in_buffer(sclp_ttybuf) != 0 &&
369 !timer_pending(&sclp_tty_timer)) {
370 init_timer(&sclp_tty_timer);
371 sclp_tty_timer.function = sclp_tty_timeout;
372 sclp_tty_timer.data = 0UL;
373 sclp_tty_timer.expires = jiffies + HZ/10;
374 add_timer(&sclp_tty_timer);
377 if (sclp_ttybuf != NULL &&
378 sclp_chars_in_buffer(sclp_ttybuf) != 0) {
381 spin_unlock_irqrestore(&sclp_tty_lock, flags);
382 __sclp_ttybuf_emit(buf);
383 spin_lock_irqsave(&sclp_tty_lock, flags);
386 spin_unlock_irqrestore(&sclp_tty_lock, flags);
390 * This routine is called by the kernel to write a series of characters to the
391 * tty device. The characters may come from user space or kernel space. This
392 * routine will return the number of characters actually accepted for writing.
395 sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
397 if (sclp_tty_chars_count > 0) {
398 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
399 sclp_tty_chars_count = 0;
401 sclp_tty_write_string(buf, count);
406 * This routine is called by the kernel to write a single character to the tty
407 * device. If the kernel uses this routine, it must call the flush_chars()
408 * routine (if defined) when it is done stuffing characters into the driver.
410 * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver.
411 * If the given character is a '\n' the contents of the SCLP write buffer
412 * - including previous characters from sclp_tty_put_char() and strings from
413 * sclp_write() without final '\n' - will be written.
416 sclp_tty_put_char(struct tty_struct *tty, unsigned char ch)
418 sclp_tty_chars[sclp_tty_chars_count++] = ch;
419 if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) {
420 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
421 sclp_tty_chars_count = 0;
426 * This routine is called by the kernel after it has written a series of
427 * characters to the tty device using put_char().
430 sclp_tty_flush_chars(struct tty_struct *tty)
432 if (sclp_tty_chars_count > 0) {
433 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
434 sclp_tty_chars_count = 0;
439 * This routine returns the number of characters in the write buffer of the
440 * SCLP driver. The provided number includes all characters that are stored
441 * in the SCCB (will be written next time the SCLP is not busy) as well as
442 * characters in the write buffer (will not be written as long as there is a
443 * final line feed missing).
446 sclp_tty_chars_in_buffer(struct tty_struct *tty)
450 struct sclp_buffer *t;
453 spin_lock_irqsave(&sclp_tty_lock, flags);
455 if (sclp_ttybuf != NULL)
456 count = sclp_chars_in_buffer(sclp_ttybuf);
457 list_for_each(l, &sclp_tty_outqueue) {
458 t = list_entry(l, struct sclp_buffer, list);
459 count += sclp_chars_in_buffer(t);
461 spin_unlock_irqrestore(&sclp_tty_lock, flags);
466 * removes all content from buffers of low level driver
469 sclp_tty_flush_buffer(struct tty_struct *tty)
471 if (sclp_tty_chars_count > 0) {
472 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
473 sclp_tty_chars_count = 0;
481 sclp_tty_input(unsigned char* buf, unsigned int count)
486 * If this tty driver is currently closed
487 * then throw the received input away.
489 if (sclp_tty == NULL)
491 cchar = ctrlchar_handle(buf, count, sclp_tty);
492 switch (cchar & CTRLCHAR_MASK) {
496 tty_insert_flip_char(sclp_tty, cchar, TTY_NORMAL);
497 tty_flip_buffer_push(sclp_tty);
500 /* send (normal) input to line discipline */
502 (strncmp((const char *) buf + count - 2, "^n", 2) &&
503 strncmp((const char *) buf + count - 2, "\252n", 2))) {
504 /* add the auto \n */
505 tty_insert_flip_string(sclp_tty, buf, count);
506 tty_insert_flip_char(sclp_tty, '\n', TTY_NORMAL);
508 tty_insert_flip_string(sclp_tty, buf, count - 2);
509 tty_flip_buffer_push(sclp_tty);
515 * get a EBCDIC string in upper/lower case,
516 * find out characters in lower/upper case separated by a special character,
517 * modifiy original string,
518 * returns length of resulting string
521 sclp_switch_cases(unsigned char *buf, int count,
522 unsigned char delim, int tolower)
524 unsigned char *ip, *op;
527 /* initially changing case is off */
530 while (count-- > 0) {
531 /* compare with special character */
533 /* followed by another special character? */
534 if (count && ip[1] == delim) {
536 * ... then put a single copy of the special
537 * character to the output string
543 * ... special character follower by a normal
544 * character toggles the case change behaviour
547 /* skip special character */
550 /* not the special character */
552 /* but case switching is on */
554 /* switch to uppercase */
555 *op++ = _ebc_toupper[(int) *ip++];
557 /* switch to lowercase */
558 *op++ = _ebc_tolower[(int) *ip++];
560 /* no case switching, copy the character */
563 /* return length of reformatted string. */
568 sclp_get_input(unsigned char *start, unsigned char *end)
574 * if set in ioctl convert EBCDIC to lower case
575 * (modify original input in SCCB)
577 if (sclp_ioctls.tolower)
578 EBC_TOLOWER(start, count);
581 * if set in ioctl find out characters in lower or upper case
582 * (depends on current case) separated by a special character,
585 if (sclp_ioctls.delim)
586 count = sclp_switch_cases(start, count,
588 sclp_ioctls.tolower);
590 /* convert EBCDIC to ASCII (modify original input in SCCB) */
591 sclp_ebcasc_str(start, count);
593 /* if set in ioctl write operators input to console */
594 if (sclp_ioctls.echo)
595 sclp_tty_write(sclp_tty, start, count);
597 /* transfer input to high level driver */
598 sclp_tty_input(start, count);
601 static inline struct gds_vector *
602 find_gds_vector(struct gds_vector *start, struct gds_vector *end, u16 id)
604 struct gds_vector *vec;
606 for (vec = start; vec < end; vec = (void *) vec + vec->length)
607 if (vec->gds_id == id)
612 static inline struct gds_subvector *
613 find_gds_subvector(struct gds_subvector *start,
614 struct gds_subvector *end, u8 key)
616 struct gds_subvector *subvec;
618 for (subvec = start; subvec < end;
619 subvec = (void *) subvec + subvec->length)
620 if (subvec->key == key)
626 sclp_eval_selfdeftextmsg(struct gds_subvector *start,
627 struct gds_subvector *end)
629 struct gds_subvector *subvec;
632 while (subvec < end) {
633 subvec = find_gds_subvector(subvec, end, 0x30);
636 sclp_get_input((unsigned char *)(subvec + 1),
637 (unsigned char *) subvec + subvec->length);
638 subvec = (void *) subvec + subvec->length;
643 sclp_eval_textcmd(struct gds_subvector *start,
644 struct gds_subvector *end)
646 struct gds_subvector *subvec;
649 while (subvec < end) {
650 subvec = find_gds_subvector(subvec, end,
651 GDS_KEY_SELFDEFTEXTMSG);
654 sclp_eval_selfdeftextmsg((struct gds_subvector *)(subvec + 1),
655 (void *)subvec + subvec->length);
656 subvec = (void *) subvec + subvec->length;
661 sclp_eval_cpmsu(struct gds_vector *start, struct gds_vector *end)
663 struct gds_vector *vec;
667 vec = find_gds_vector(vec, end, GDS_ID_TEXTCMD);
670 sclp_eval_textcmd((struct gds_subvector *)(vec + 1),
671 (void *) vec + vec->length);
672 vec = (void *) vec + vec->length;
678 sclp_eval_mdsmu(struct gds_vector *start, void *end)
680 struct gds_vector *vec;
682 vec = find_gds_vector(start, end, GDS_ID_CPMSU);
684 sclp_eval_cpmsu(vec + 1, (void *) vec + vec->length);
688 sclp_tty_receiver(struct evbuf_header *evbuf)
690 struct gds_vector *start, *end, *vec;
692 start = (struct gds_vector *)(evbuf + 1);
693 end = (void *) evbuf + evbuf->length;
694 vec = find_gds_vector(start, end, GDS_ID_MDSMU);
696 sclp_eval_mdsmu(vec + 1, (void *) vec + vec->length);
700 sclp_tty_state_change(struct sclp_register *reg)
704 static struct sclp_register sclp_input_event =
706 .receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK,
707 .state_change_fn = sclp_tty_state_change,
708 .receiver_fn = sclp_tty_receiver
711 static const struct tty_operations sclp_ops = {
712 .open = sclp_tty_open,
713 .close = sclp_tty_close,
714 .write = sclp_tty_write,
715 .put_char = sclp_tty_put_char,
716 .flush_chars = sclp_tty_flush_chars,
717 .write_room = sclp_tty_write_room,
718 .chars_in_buffer = sclp_tty_chars_in_buffer,
719 .flush_buffer = sclp_tty_flush_buffer,
720 .ioctl = sclp_tty_ioctl,
726 struct tty_driver *driver;
731 if (!CONSOLE_IS_SCLP)
733 driver = alloc_tty_driver(1);
739 printk(KERN_ERR SCLP_TTY_PRINT_HEADER
740 "could not register tty - "
741 "sclp_rw_init returned %d\n", rc);
742 put_tty_driver(driver);
745 /* Allocate pages for output buffering */
746 INIT_LIST_HEAD(&sclp_tty_pages);
747 for (i = 0; i < MAX_KMEM_PAGES; i++) {
748 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
750 put_tty_driver(driver);
753 list_add_tail((struct list_head *) page, &sclp_tty_pages);
755 INIT_LIST_HEAD(&sclp_tty_outqueue);
756 spin_lock_init(&sclp_tty_lock);
757 init_waitqueue_head(&sclp_tty_waitq);
758 init_timer(&sclp_tty_timer);
760 sclp_tty_buffer_count = 0;
763 * save 4 characters for the CPU number
764 * written at start of each line by VM/CP
766 sclp_ioctls_init.columns = 76;
767 /* case input lines to lowercase */
768 sclp_ioctls_init.tolower = 1;
770 sclp_ioctls = sclp_ioctls_init;
771 sclp_tty_chars_count = 0;
774 rc = sclp_register(&sclp_input_event);
776 put_tty_driver(driver);
780 driver->owner = THIS_MODULE;
781 driver->driver_name = "sclp_line";
782 driver->name = "sclp_line";
783 driver->major = TTY_MAJOR;
784 driver->minor_start = 64;
785 driver->type = TTY_DRIVER_TYPE_SYSTEM;
786 driver->subtype = SYSTEM_TYPE_TTY;
787 driver->init_termios = tty_std_termios;
788 driver->init_termios.c_iflag = IGNBRK | IGNPAR;
789 driver->init_termios.c_oflag = ONLCR | XTABS;
790 driver->init_termios.c_lflag = ISIG | ECHO;
791 driver->flags = TTY_DRIVER_REAL_RAW;
792 tty_set_operations(driver, &sclp_ops);
793 rc = tty_register_driver(driver);
795 printk(KERN_ERR SCLP_TTY_PRINT_HEADER
796 "could not register tty - "
797 "tty_register_driver returned %d\n", rc);
798 put_tty_driver(driver);
801 sclp_tty_driver = driver;
804 module_init(sclp_tty_init);