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/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>
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 extern struct termios tty_std_termios;
65 static struct sclp_ioctls sclp_ioctls;
66 static struct sclp_ioctls sclp_ioctls_init =
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 */
79 /* This routine is called whenever we try to open a SCLP terminal. */
81 sclp_tty_open(struct tty_struct *tty, struct file *filp)
84 tty->driver_data = NULL;
89 /* This routine is called when the SCLP terminal is closed. */
91 sclp_tty_close(struct tty_struct *tty, struct file *filp)
98 /* execute commands to control the i/o behaviour of the SCLP tty at runtime */
100 sclp_tty_ioctl(struct tty_struct *tty, struct file * file,
101 unsigned int cmd, unsigned long arg)
108 if (tty->flags & (1 << TTY_IO_ERROR))
114 /* set width of horizontal tab */
115 if (get_user(sclp_ioctls.htab, (unsigned short *) arg))
121 /* get width of horizontal tab */
122 if (put_user(sclp_ioctls.htab, (unsigned short *) arg))
126 /* enable/disable echo of input */
127 if (get_user(sclp_ioctls.echo, (unsigned char *) arg))
131 /* Is echo of input enabled ? */
132 if (put_user(sclp_ioctls.echo, (unsigned char *) arg))
136 /* set number of columns for output */
137 if (get_user(sclp_ioctls.columns, (unsigned short *) arg))
143 /* get number of columns for output */
144 if (put_user(sclp_ioctls.columns, (unsigned short *) arg))
148 /* enable/disable writing without final new line character */
149 if (get_user(sclp_ioctls.final_nl, (signed char *) arg))
153 /* Is writing without final new line character enabled ? */
154 if (put_user(sclp_ioctls.final_nl, (signed char *) arg))
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
163 if (get_user(obuf, (unsigned int *) arg) == 0) {
165 sclp_ioctls.max_sccb = (obuf >> 12) + 1;
167 sclp_ioctls.max_sccb = (obuf >> 12);
172 /* get the maximum buffers size for output */
173 obuf = sclp_ioctls.max_sccb << 12;
174 if (put_user(obuf, (unsigned int *) arg))
178 /* get the number of buffers got from kernel at startup */
179 if (put_user(sclp_ioctls.kmem_sccb, (unsigned short *) arg))
183 /* enable/disable conversion from upper to lower case */
184 if (get_user(sclp_ioctls.tolower, (unsigned char *) arg))
188 /* Is conversion from upper to lower case of input enabled? */
189 if (put_user(sclp_ioctls.tolower, (unsigned char *) arg))
194 * set special character used for separating upper and
195 * lower case, 0x00 disables this feature
197 if (get_user(sclp_ioctls.delim, (unsigned char *) arg))
202 * get special character used for separating upper and
203 * lower case, 0x00 disables this feature
205 if (put_user(sclp_ioctls.delim, (unsigned char *) arg))
209 /* set initial (default) sclp ioctls */
210 sclp_ioctls = sclp_ioctls_init;
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);
223 spin_unlock_irqrestore(&sclp_tty_lock, flags);
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
238 sclp_tty_write_room (struct tty_struct *tty)
244 spin_lock_irqsave(&sclp_tty_lock, flags);
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);
255 sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc)
258 struct sclp_buffer *next;
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. */
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);
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);
288 __sclp_ttybuf_emit(struct sclp_buffer *buffer)
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);
299 sclp_emit_buffer(buffer, sclp_ttybuf_callback);
303 * When this routine is called from the timer then we flush the
304 * temporary write buffer.
307 sclp_tty_timeout(unsigned long data)
310 struct sclp_buffer *buf;
312 spin_lock_irqsave(&sclp_tty_lock, flags);
315 spin_unlock_irqrestore(&sclp_tty_lock, flags);
318 __sclp_ttybuf_emit(buf);
323 * Write a string to the sclp tty.
326 sclp_tty_write_string(const unsigned char *str, int count)
331 struct sclp_buffer *buf;
335 spin_lock_irqsave(&sclp_tty_lock, flags);
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);
344 wait_event(sclp_tty_waitq,
345 !list_empty(&sclp_tty_pages));
346 spin_lock_irqsave(&sclp_tty_lock, flags);
348 page = sclp_tty_pages.next;
349 list_del((struct list_head *) page);
350 sclp_ttybuf = sclp_make_buffer(page,
354 /* try to write the string to the current output buffer */
355 written = sclp_write(sclp_ttybuf, str, count);
356 if (written == count)
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.
365 spin_unlock_irqrestore(&sclp_tty_lock, flags);
366 __sclp_ttybuf_emit(buf);
367 spin_lock_irqsave(&sclp_tty_lock, flags);
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);
383 if (sclp_ttybuf != NULL &&
384 sclp_chars_in_buffer(sclp_ttybuf) != 0) {
387 spin_unlock_irqrestore(&sclp_tty_lock, flags);
388 __sclp_ttybuf_emit(buf);
389 spin_lock_irqsave(&sclp_tty_lock, flags);
392 spin_unlock_irqrestore(&sclp_tty_lock, flags);
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.
401 sclp_tty_write(struct tty_struct *tty, int from_user,
402 const unsigned char *buf, int count)
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;
411 sclp_tty_write_string(buf, count);
416 length = count < SCLP_TTY_BUF_SIZE ?
417 count : SCLP_TTY_BUF_SIZE;
418 length -= copy_from_user(sclp_tty_chars, buf, length);
424 sclp_tty_write_string(sclp_tty_chars, length);
433 * This routine is called by the kernel to write a single character to the tty
434 * device. If the kernel uses this routine, it must call the flush_chars()
435 * routine (if defined) when it is done stuffing characters into the driver.
437 * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver.
438 * If the given character is a '\n' the contents of the SCLP write buffer
439 * - including previous characters from sclp_tty_put_char() and strings from
440 * sclp_write() without final '\n' - will be written.
443 sclp_tty_put_char(struct tty_struct *tty, unsigned char ch)
445 sclp_tty_chars[sclp_tty_chars_count++] = ch;
446 if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) {
447 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
448 sclp_tty_chars_count = 0;
453 * This routine is called by the kernel after it has written a series of
454 * characters to the tty device using put_char().
457 sclp_tty_flush_chars(struct tty_struct *tty)
459 if (sclp_tty_chars_count > 0) {
460 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
461 sclp_tty_chars_count = 0;
466 * This routine returns the number of characters in the write buffer of the
467 * SCLP driver. The provided number includes all characters that are stored
468 * in the SCCB (will be written next time the SCLP is not busy) as well as
469 * characters in the write buffer (will not be written as long as there is a
470 * final line feed missing).
473 sclp_tty_chars_in_buffer(struct tty_struct *tty)
477 struct sclp_buffer *t;
480 spin_lock_irqsave(&sclp_tty_lock, flags);
482 if (sclp_ttybuf != NULL)
483 count = sclp_chars_in_buffer(sclp_ttybuf);
484 list_for_each(l, &sclp_tty_outqueue) {
485 t = list_entry(l, struct sclp_buffer, list);
486 count += sclp_chars_in_buffer(t);
488 spin_unlock_irqrestore(&sclp_tty_lock, flags);
493 * removes all content from buffers of low level driver
496 sclp_tty_flush_buffer(struct tty_struct *tty)
498 if (sclp_tty_chars_count > 0) {
499 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
500 sclp_tty_chars_count = 0;
508 sclp_tty_input(unsigned char* buf, unsigned int count)
513 * If this tty driver is currently closed
514 * then throw the received input away.
516 if (sclp_tty == NULL)
518 cchar = ctrlchar_handle(buf, count, sclp_tty);
519 switch (cchar & CTRLCHAR_MASK) {
523 sclp_tty->flip.count++;
524 *sclp_tty->flip.flag_buf_ptr++ = TTY_NORMAL;
525 *sclp_tty->flip.char_buf_ptr++ = cchar;
526 tty_flip_buffer_push(sclp_tty);
529 /* send (normal) input to line discipline */
530 memcpy(sclp_tty->flip.char_buf_ptr, buf, count);
532 (strncmp ((const char *) buf + count - 2, "^n", 2) &&
533 strncmp ((const char *) buf + count - 2, "\0252n", 2))) {
534 sclp_tty->flip.char_buf_ptr[count] = '\n';
538 memset(sclp_tty->flip.flag_buf_ptr, TTY_NORMAL, count);
539 sclp_tty->flip.char_buf_ptr += count;
540 sclp_tty->flip.flag_buf_ptr += count;
541 sclp_tty->flip.count += count;
542 tty_flip_buffer_push(sclp_tty);
548 * get a EBCDIC string in upper/lower case,
549 * find out characters in lower/upper case separated by a special character,
550 * modifiy original string,
551 * returns length of resulting string
554 sclp_switch_cases(unsigned char *buf, int count,
555 unsigned char delim, int tolower)
557 unsigned char *ip, *op;
560 /* initially changing case is off */
563 while (count-- > 0) {
564 /* compare with special character */
566 /* followed by another special character? */
567 if (count && ip[1] == delim) {
569 * ... then put a single copy of the special
570 * character to the output string
576 * ... special character follower by a normal
577 * character toggles the case change behaviour
580 /* skip special character */
583 /* not the special character */
585 /* but case switching is on */
587 /* switch to uppercase */
588 *op++ = _ebc_toupper[(int) *ip++];
590 /* switch to lowercase */
591 *op++ = _ebc_tolower[(int) *ip++];
593 /* no case switching, copy the character */
596 /* return length of reformatted string. */
601 sclp_get_input(unsigned char *start, unsigned char *end)
607 * if set in ioctl convert EBCDIC to lower case
608 * (modify original input in SCCB)
610 if (sclp_ioctls.tolower)
611 EBC_TOLOWER(start, count);
614 * if set in ioctl find out characters in lower or upper case
615 * (depends on current case) separated by a special character,
618 if (sclp_ioctls.delim)
619 count = sclp_switch_cases(start, count,
621 sclp_ioctls.tolower);
623 /* convert EBCDIC to ASCII (modify original input in SCCB) */
624 sclp_ebcasc_str(start, count);
626 /* if set in ioctl write operators input to console */
627 if (sclp_ioctls.echo)
628 sclp_tty_write(sclp_tty, 0, start, count);
630 /* transfer input to high level driver */
631 sclp_tty_input(start, count);
634 static inline struct gds_vector *
635 find_gds_vector(struct gds_vector *start, struct gds_vector *end, u16 id)
637 struct gds_vector *vec;
639 for (vec = start; vec < end; vec = (void *) vec + vec->length)
640 if (vec->gds_id == id)
645 static inline struct gds_subvector *
646 find_gds_subvector(struct gds_subvector *start,
647 struct gds_subvector *end, u8 key)
649 struct gds_subvector *subvec;
651 for (subvec = start; subvec < end;
652 subvec = (void *) subvec + subvec->length)
653 if (subvec->key == key)
659 sclp_eval_selfdeftextmsg(struct gds_subvector *start,
660 struct gds_subvector *end)
662 struct gds_subvector *subvec;
665 while (subvec < end) {
666 subvec = find_gds_subvector(subvec, end, 0x30);
669 sclp_get_input((unsigned char *)(subvec + 1),
670 (unsigned char *) subvec + subvec->length);
671 subvec = (void *) subvec + subvec->length;
676 sclp_eval_textcmd(struct gds_subvector *start,
677 struct gds_subvector *end)
679 struct gds_subvector *subvec;
682 while (subvec < end) {
683 subvec = find_gds_subvector(subvec, end,
684 GDS_KEY_SelfDefTextMsg);
687 sclp_eval_selfdeftextmsg((struct gds_subvector *)(subvec + 1),
688 (void *)subvec + subvec->length);
689 subvec = (void *) subvec + subvec->length;
694 sclp_eval_cpmsu(struct gds_vector *start, struct gds_vector *end)
696 struct gds_vector *vec;
700 vec = find_gds_vector(vec, end, GDS_ID_TextCmd);
703 sclp_eval_textcmd((struct gds_subvector *)(vec + 1),
704 (void *) vec + vec->length);
705 vec = (void *) vec + vec->length;
711 sclp_eval_mdsmu(struct gds_vector *start, void *end)
713 struct gds_vector *vec;
715 vec = find_gds_vector(start, end, GDS_ID_CPMSU);
717 sclp_eval_cpmsu(vec + 1, (void *) vec + vec->length);
721 sclp_tty_receiver(struct evbuf_header *evbuf)
723 struct gds_vector *start, *end, *vec;
725 start = (struct gds_vector *)(evbuf + 1);
726 end = (void *) evbuf + evbuf->length;
727 vec = find_gds_vector(start, end, GDS_ID_MDSMU);
729 sclp_eval_mdsmu(vec + 1, (void *) vec + vec->length);
733 sclp_tty_state_change(struct sclp_register *reg)
737 static struct sclp_register sclp_input_event =
739 .receive_mask = EvTyp_OpCmd_Mask | EvTyp_PMsgCmd_Mask,
740 .state_change_fn = sclp_tty_state_change,
741 .receiver_fn = sclp_tty_receiver
744 static struct tty_operations sclp_ops = {
745 .open = sclp_tty_open,
746 .close = sclp_tty_close,
747 .write = sclp_tty_write,
748 .put_char = sclp_tty_put_char,
749 .flush_chars = sclp_tty_flush_chars,
750 .write_room = sclp_tty_write_room,
751 .chars_in_buffer = sclp_tty_chars_in_buffer,
752 .flush_buffer = sclp_tty_flush_buffer,
753 .ioctl = sclp_tty_ioctl,
759 struct tty_driver *driver;
764 if (!CONSOLE_IS_SCLP)
766 driver = alloc_tty_driver(1);
772 printk(KERN_ERR SCLP_TTY_PRINT_HEADER
773 "could not register tty - "
774 "sclp_rw_init returned %d\n", rc);
775 put_tty_driver(driver);
778 /* Allocate pages for output buffering */
779 INIT_LIST_HEAD(&sclp_tty_pages);
780 for (i = 0; i < MAX_KMEM_PAGES; i++) {
781 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
783 put_tty_driver(driver);
786 list_add_tail((struct list_head *) page, &sclp_tty_pages);
788 INIT_LIST_HEAD(&sclp_tty_outqueue);
789 spin_lock_init(&sclp_tty_lock);
790 init_waitqueue_head(&sclp_tty_waitq);
791 init_timer(&sclp_tty_timer);
793 sclp_tty_buffer_count = 0;
796 * save 4 characters for the CPU number
797 * written at start of each line by VM/CP
799 sclp_ioctls_init.columns = 76;
800 /* case input lines to lowercase */
801 sclp_ioctls_init.tolower = 1;
803 sclp_ioctls = sclp_ioctls_init;
804 sclp_tty_chars_count = 0;
807 rc = sclp_register(&sclp_input_event);
809 put_tty_driver(driver);
813 driver->owner = THIS_MODULE;
814 driver->driver_name = "sclp_line";
815 driver->name = "sclp_line";
816 driver->major = TTY_MAJOR;
817 driver->minor_start = 64;
818 driver->type = TTY_DRIVER_TYPE_SYSTEM;
819 driver->subtype = SYSTEM_TYPE_TTY;
820 driver->init_termios = tty_std_termios;
821 driver->init_termios.c_iflag = IGNBRK | IGNPAR;
822 driver->init_termios.c_oflag = ONLCR | XTABS;
823 driver->init_termios.c_lflag = ISIG | ECHO;
824 driver->flags = TTY_DRIVER_REAL_RAW;
825 tty_set_operations(driver, &sclp_ops);
826 rc = tty_register_driver(driver);
828 printk(KERN_ERR SCLP_TTY_PRINT_HEADER
829 "could not register tty - "
830 "tty_register_driver returned %d\n", rc);
831 put_tty_driver(driver);
834 sclp_tty_driver = driver;
837 module_init(sclp_tty_init);