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/tty_flip.h>
17 #include <linux/sched.h>
18 #include <linux/wait.h>
19 #include <linux/slab.h>
20 #include <linux/err.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
23 #include <asm/uaccess.h>
30 #define SCLP_TTY_PRINT_HEADER "sclp tty driver: "
33 * size of a buffer that collects single characters coming in
34 * via sclp_tty_put_char()
36 #define SCLP_TTY_BUF_SIZE 512
39 * There is exactly one SCLP terminal, so we can keep things simple
40 * and allocate all variables statically.
43 /* Lock to guard over changes to global variables. */
44 static spinlock_t sclp_tty_lock
;
45 /* List of free pages that can be used for console output buffering. */
46 static struct list_head sclp_tty_pages
;
47 /* List of full struct sclp_buffer structures ready for output. */
48 static struct list_head sclp_tty_outqueue
;
49 /* Counter how many buffers are emitted. */
50 static int sclp_tty_buffer_count
;
51 /* Pointer to current console buffer. */
52 static struct sclp_buffer
*sclp_ttybuf
;
53 /* Timer for delayed output of console messages. */
54 static struct timer_list sclp_tty_timer
;
55 /* Waitqueue to wait for buffers to get empty. */
56 static wait_queue_head_t sclp_tty_waitq
;
58 static struct tty_struct
*sclp_tty
;
59 static unsigned char sclp_tty_chars
[SCLP_TTY_BUF_SIZE
];
60 static unsigned short int sclp_tty_chars_count
;
62 struct tty_driver
*sclp_tty_driver
;
64 extern struct termios tty_std_termios
;
66 static struct sclp_ioctls sclp_ioctls
;
67 static struct sclp_ioctls sclp_ioctls_init
=
69 8, /* 1 hor. tab. = 8 spaces */
70 0, /* no echo of input by this driver */
71 80, /* 80 characters/line */
72 1, /* write after 1/10 s without final new line */
73 MAX_KMEM_PAGES
, /* quick fix: avoid __alloc_pages */
74 MAX_KMEM_PAGES
, /* take 32/64 pages from kernel memory, */
75 0, /* do not convert to lower case */
76 0x6c /* to seprate upper and lower case */
80 /* This routine is called whenever we try to open a SCLP terminal. */
82 sclp_tty_open(struct tty_struct
*tty
, struct file
*filp
)
85 tty
->driver_data
= NULL
;
90 /* This routine is called when the SCLP terminal is closed. */
92 sclp_tty_close(struct tty_struct
*tty
, struct file
*filp
)
99 /* execute commands to control the i/o behaviour of the SCLP tty at runtime */
101 sclp_tty_ioctl(struct tty_struct
*tty
, struct file
* file
,
102 unsigned int cmd
, unsigned long arg
)
109 if (tty
->flags
& (1 << TTY_IO_ERROR
))
115 /* set width of horizontal tab */
116 if (get_user(sclp_ioctls
.htab
, (unsigned short __user
*) arg
))
122 /* get width of horizontal tab */
123 if (put_user(sclp_ioctls
.htab
, (unsigned short __user
*) arg
))
127 /* enable/disable echo of input */
128 if (get_user(sclp_ioctls
.echo
, (unsigned char __user
*) arg
))
132 /* Is echo of input enabled ? */
133 if (put_user(sclp_ioctls
.echo
, (unsigned char __user
*) arg
))
137 /* set number of columns for output */
138 if (get_user(sclp_ioctls
.columns
, (unsigned short __user
*) arg
))
144 /* get number of columns for output */
145 if (put_user(sclp_ioctls
.columns
, (unsigned short __user
*) arg
))
149 /* enable/disable writing without final new line character */
150 if (get_user(sclp_ioctls
.final_nl
, (signed char __user
*) arg
))
154 /* Is writing without final new line character enabled ? */
155 if (put_user(sclp_ioctls
.final_nl
, (signed char __user
*) arg
))
160 * set the maximum buffers size for output, will be rounded
161 * up to next 4kB boundary and stored as number of SCCBs
162 * (4kB Buffers) limitation: 256 x 4kB
164 if (get_user(obuf
, (unsigned int __user
*) arg
) == 0) {
166 sclp_ioctls
.max_sccb
= (obuf
>> 12) + 1;
168 sclp_ioctls
.max_sccb
= (obuf
>> 12);
173 /* get the maximum buffers size for output */
174 obuf
= sclp_ioctls
.max_sccb
<< 12;
175 if (put_user(obuf
, (unsigned int __user
*) arg
))
179 /* get the number of buffers got from kernel at startup */
180 if (put_user(sclp_ioctls
.kmem_sccb
, (unsigned short __user
*) arg
))
184 /* enable/disable conversion from upper to lower case */
185 if (get_user(sclp_ioctls
.tolower
, (unsigned char __user
*) arg
))
189 /* Is conversion from upper to lower case of input enabled? */
190 if (put_user(sclp_ioctls
.tolower
, (unsigned char __user
*) arg
))
195 * set special character used for separating upper and
196 * lower case, 0x00 disables this feature
198 if (get_user(sclp_ioctls
.delim
, (unsigned char __user
*) arg
))
203 * get special character used for separating upper and
204 * lower case, 0x00 disables this feature
206 if (put_user(sclp_ioctls
.delim
, (unsigned char __user
*) arg
))
210 /* set initial (default) sclp ioctls */
211 sclp_ioctls
= sclp_ioctls_init
;
219 spin_lock_irqsave(&sclp_tty_lock
, flags
);
220 if (sclp_ttybuf
!= NULL
) {
221 sclp_set_htab(sclp_ttybuf
, sclp_ioctls
.htab
);
222 sclp_set_columns(sclp_ttybuf
, sclp_ioctls
.columns
);
224 spin_unlock_irqrestore(&sclp_tty_lock
, flags
);
230 * This routine returns the numbers of characters the tty driver
231 * will accept for queuing to be written. This number is subject
232 * to change as output buffers get emptied, or if the output flow
233 * control is acted. This is not an exact number because not every
234 * character needs the same space in the sccb. The worst case is
235 * a string of newlines. Every newlines creates a new mto which
239 sclp_tty_write_room (struct tty_struct
*tty
)
245 spin_lock_irqsave(&sclp_tty_lock
, flags
);
247 if (sclp_ttybuf
!= NULL
)
248 count
= sclp_buffer_space(sclp_ttybuf
) / sizeof(struct mto
);
249 list_for_each(l
, &sclp_tty_pages
)
250 count
+= NR_EMPTY_MTO_PER_SCCB
;
251 spin_unlock_irqrestore(&sclp_tty_lock
, flags
);
256 sclp_ttybuf_callback(struct sclp_buffer
*buffer
, int rc
)
262 page
= sclp_unmake_buffer(buffer
);
263 spin_lock_irqsave(&sclp_tty_lock
, flags
);
264 /* Remove buffer from outqueue */
265 list_del(&buffer
->list
);
266 sclp_tty_buffer_count
--;
267 list_add_tail((struct list_head
*) page
, &sclp_tty_pages
);
268 /* Check if there is a pending buffer on the out queue. */
270 if (!list_empty(&sclp_tty_outqueue
))
271 buffer
= list_entry(sclp_tty_outqueue
.next
,
272 struct sclp_buffer
, list
);
273 spin_unlock_irqrestore(&sclp_tty_lock
, flags
);
274 } while (buffer
&& sclp_emit_buffer(buffer
, sclp_ttybuf_callback
));
275 wake_up(&sclp_tty_waitq
);
276 /* check if the tty needs a wake up call */
277 if (sclp_tty
!= NULL
) {
278 tty_wakeup(sclp_tty
);
283 __sclp_ttybuf_emit(struct sclp_buffer
*buffer
)
289 spin_lock_irqsave(&sclp_tty_lock
, flags
);
290 list_add_tail(&buffer
->list
, &sclp_tty_outqueue
);
291 count
= sclp_tty_buffer_count
++;
292 spin_unlock_irqrestore(&sclp_tty_lock
, flags
);
295 rc
= sclp_emit_buffer(buffer
, sclp_ttybuf_callback
);
297 sclp_ttybuf_callback(buffer
, rc
);
301 * When this routine is called from the timer then we flush the
302 * temporary write buffer.
305 sclp_tty_timeout(unsigned long data
)
308 struct sclp_buffer
*buf
;
310 spin_lock_irqsave(&sclp_tty_lock
, flags
);
313 spin_unlock_irqrestore(&sclp_tty_lock
, flags
);
316 __sclp_ttybuf_emit(buf
);
321 * Write a string to the sclp tty.
324 sclp_tty_write_string(const unsigned char *str
, int count
)
329 struct sclp_buffer
*buf
;
333 spin_lock_irqsave(&sclp_tty_lock
, flags
);
335 /* Create a sclp output buffer if none exists yet */
336 if (sclp_ttybuf
== NULL
) {
337 while (list_empty(&sclp_tty_pages
)) {
338 spin_unlock_irqrestore(&sclp_tty_lock
, flags
);
342 wait_event(sclp_tty_waitq
,
343 !list_empty(&sclp_tty_pages
));
344 spin_lock_irqsave(&sclp_tty_lock
, flags
);
346 page
= sclp_tty_pages
.next
;
347 list_del((struct list_head
*) page
);
348 sclp_ttybuf
= sclp_make_buffer(page
,
352 /* try to write the string to the current output buffer */
353 written
= sclp_write(sclp_ttybuf
, str
, count
);
354 if (written
== count
)
357 * Not all characters could be written to the current
358 * output buffer. Emit the buffer, create a new buffer
359 * and then output the rest of the string.
363 spin_unlock_irqrestore(&sclp_tty_lock
, flags
);
364 __sclp_ttybuf_emit(buf
);
365 spin_lock_irqsave(&sclp_tty_lock
, flags
);
369 /* Setup timer to output current console buffer after 1/10 second */
370 if (sclp_ioctls
.final_nl
) {
371 if (sclp_ttybuf
!= NULL
&&
372 sclp_chars_in_buffer(sclp_ttybuf
) != 0 &&
373 !timer_pending(&sclp_tty_timer
)) {
374 init_timer(&sclp_tty_timer
);
375 sclp_tty_timer
.function
= sclp_tty_timeout
;
376 sclp_tty_timer
.data
= 0UL;
377 sclp_tty_timer
.expires
= jiffies
+ HZ
/10;
378 add_timer(&sclp_tty_timer
);
381 if (sclp_ttybuf
!= NULL
&&
382 sclp_chars_in_buffer(sclp_ttybuf
) != 0) {
385 spin_unlock_irqrestore(&sclp_tty_lock
, flags
);
386 __sclp_ttybuf_emit(buf
);
387 spin_lock_irqsave(&sclp_tty_lock
, flags
);
390 spin_unlock_irqrestore(&sclp_tty_lock
, flags
);
394 * This routine is called by the kernel to write a series of characters to the
395 * tty device. The characters may come from user space or kernel space. This
396 * routine will return the number of characters actually accepted for writing.
399 sclp_tty_write(struct tty_struct
*tty
, const unsigned char *buf
, int count
)
401 if (sclp_tty_chars_count
> 0) {
402 sclp_tty_write_string(sclp_tty_chars
, sclp_tty_chars_count
);
403 sclp_tty_chars_count
= 0;
405 sclp_tty_write_string(buf
, count
);
410 * This routine is called by the kernel to write a single character to the tty
411 * device. If the kernel uses this routine, it must call the flush_chars()
412 * routine (if defined) when it is done stuffing characters into the driver.
414 * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver.
415 * If the given character is a '\n' the contents of the SCLP write buffer
416 * - including previous characters from sclp_tty_put_char() and strings from
417 * sclp_write() without final '\n' - will be written.
420 sclp_tty_put_char(struct tty_struct
*tty
, unsigned char ch
)
422 sclp_tty_chars
[sclp_tty_chars_count
++] = ch
;
423 if (ch
== '\n' || sclp_tty_chars_count
>= SCLP_TTY_BUF_SIZE
) {
424 sclp_tty_write_string(sclp_tty_chars
, sclp_tty_chars_count
);
425 sclp_tty_chars_count
= 0;
430 * This routine is called by the kernel after it has written a series of
431 * characters to the tty device using put_char().
434 sclp_tty_flush_chars(struct tty_struct
*tty
)
436 if (sclp_tty_chars_count
> 0) {
437 sclp_tty_write_string(sclp_tty_chars
, sclp_tty_chars_count
);
438 sclp_tty_chars_count
= 0;
443 * This routine returns the number of characters in the write buffer of the
444 * SCLP driver. The provided number includes all characters that are stored
445 * in the SCCB (will be written next time the SCLP is not busy) as well as
446 * characters in the write buffer (will not be written as long as there is a
447 * final line feed missing).
450 sclp_tty_chars_in_buffer(struct tty_struct
*tty
)
454 struct sclp_buffer
*t
;
457 spin_lock_irqsave(&sclp_tty_lock
, flags
);
459 if (sclp_ttybuf
!= NULL
)
460 count
= sclp_chars_in_buffer(sclp_ttybuf
);
461 list_for_each(l
, &sclp_tty_outqueue
) {
462 t
= list_entry(l
, struct sclp_buffer
, list
);
463 count
+= sclp_chars_in_buffer(t
);
465 spin_unlock_irqrestore(&sclp_tty_lock
, flags
);
470 * removes all content from buffers of low level driver
473 sclp_tty_flush_buffer(struct tty_struct
*tty
)
475 if (sclp_tty_chars_count
> 0) {
476 sclp_tty_write_string(sclp_tty_chars
, sclp_tty_chars_count
);
477 sclp_tty_chars_count
= 0;
485 sclp_tty_input(unsigned char* buf
, unsigned int count
)
490 * If this tty driver is currently closed
491 * then throw the received input away.
493 if (sclp_tty
== NULL
)
495 cchar
= ctrlchar_handle(buf
, count
, sclp_tty
);
496 switch (cchar
& CTRLCHAR_MASK
) {
500 tty_insert_flip_char(sclp_tty
, cchar
, TTY_NORMAL
);
501 tty_flip_buffer_push(sclp_tty
);
504 /* send (normal) input to line discipline */
506 (strncmp((const char *) buf
+ count
- 2, "^n", 2) &&
507 strncmp((const char *) buf
+ count
- 2, "\252n", 2))) {
508 /* add the auto \n */
509 tty_insert_flip_string(sclp_tty
, buf
, count
);
510 tty_insert_flip_char(sclp_tty
, '\n', TTY_NORMAL
);
512 tty_insert_flip_string(sclp_tty
, buf
, count
- 2);
513 tty_flip_buffer_push(sclp_tty
);
519 * get a EBCDIC string in upper/lower case,
520 * find out characters in lower/upper case separated by a special character,
521 * modifiy original string,
522 * returns length of resulting string
525 sclp_switch_cases(unsigned char *buf
, int count
,
526 unsigned char delim
, int tolower
)
528 unsigned char *ip
, *op
;
531 /* initially changing case is off */
534 while (count
-- > 0) {
535 /* compare with special character */
537 /* followed by another special character? */
538 if (count
&& ip
[1] == delim
) {
540 * ... then put a single copy of the special
541 * character to the output string
547 * ... special character follower by a normal
548 * character toggles the case change behaviour
551 /* skip special character */
554 /* not the special character */
556 /* but case switching is on */
558 /* switch to uppercase */
559 *op
++ = _ebc_toupper
[(int) *ip
++];
561 /* switch to lowercase */
562 *op
++ = _ebc_tolower
[(int) *ip
++];
564 /* no case switching, copy the character */
567 /* return length of reformatted string. */
572 sclp_get_input(unsigned char *start
, unsigned char *end
)
578 * if set in ioctl convert EBCDIC to lower case
579 * (modify original input in SCCB)
581 if (sclp_ioctls
.tolower
)
582 EBC_TOLOWER(start
, count
);
585 * if set in ioctl find out characters in lower or upper case
586 * (depends on current case) separated by a special character,
589 if (sclp_ioctls
.delim
)
590 count
= sclp_switch_cases(start
, count
,
592 sclp_ioctls
.tolower
);
594 /* convert EBCDIC to ASCII (modify original input in SCCB) */
595 sclp_ebcasc_str(start
, count
);
597 /* if set in ioctl write operators input to console */
598 if (sclp_ioctls
.echo
)
599 sclp_tty_write(sclp_tty
, start
, count
);
601 /* transfer input to high level driver */
602 sclp_tty_input(start
, count
);
605 static inline struct gds_vector
*
606 find_gds_vector(struct gds_vector
*start
, struct gds_vector
*end
, u16 id
)
608 struct gds_vector
*vec
;
610 for (vec
= start
; vec
< end
; vec
= (void *) vec
+ vec
->length
)
611 if (vec
->gds_id
== id
)
616 static inline struct gds_subvector
*
617 find_gds_subvector(struct gds_subvector
*start
,
618 struct gds_subvector
*end
, u8 key
)
620 struct gds_subvector
*subvec
;
622 for (subvec
= start
; subvec
< end
;
623 subvec
= (void *) subvec
+ subvec
->length
)
624 if (subvec
->key
== key
)
630 sclp_eval_selfdeftextmsg(struct gds_subvector
*start
,
631 struct gds_subvector
*end
)
633 struct gds_subvector
*subvec
;
636 while (subvec
< end
) {
637 subvec
= find_gds_subvector(subvec
, end
, 0x30);
640 sclp_get_input((unsigned char *)(subvec
+ 1),
641 (unsigned char *) subvec
+ subvec
->length
);
642 subvec
= (void *) subvec
+ subvec
->length
;
647 sclp_eval_textcmd(struct gds_subvector
*start
,
648 struct gds_subvector
*end
)
650 struct gds_subvector
*subvec
;
653 while (subvec
< end
) {
654 subvec
= find_gds_subvector(subvec
, end
,
655 GDS_KEY_SelfDefTextMsg
);
658 sclp_eval_selfdeftextmsg((struct gds_subvector
*)(subvec
+ 1),
659 (void *)subvec
+ subvec
->length
);
660 subvec
= (void *) subvec
+ subvec
->length
;
665 sclp_eval_cpmsu(struct gds_vector
*start
, struct gds_vector
*end
)
667 struct gds_vector
*vec
;
671 vec
= find_gds_vector(vec
, end
, GDS_ID_TextCmd
);
674 sclp_eval_textcmd((struct gds_subvector
*)(vec
+ 1),
675 (void *) vec
+ vec
->length
);
676 vec
= (void *) vec
+ vec
->length
;
682 sclp_eval_mdsmu(struct gds_vector
*start
, void *end
)
684 struct gds_vector
*vec
;
686 vec
= find_gds_vector(start
, end
, GDS_ID_CPMSU
);
688 sclp_eval_cpmsu(vec
+ 1, (void *) vec
+ vec
->length
);
692 sclp_tty_receiver(struct evbuf_header
*evbuf
)
694 struct gds_vector
*start
, *end
, *vec
;
696 start
= (struct gds_vector
*)(evbuf
+ 1);
697 end
= (void *) evbuf
+ evbuf
->length
;
698 vec
= find_gds_vector(start
, end
, GDS_ID_MDSMU
);
700 sclp_eval_mdsmu(vec
+ 1, (void *) vec
+ vec
->length
);
704 sclp_tty_state_change(struct sclp_register
*reg
)
708 static struct sclp_register sclp_input_event
=
710 .receive_mask
= EvTyp_OpCmd_Mask
| EvTyp_PMsgCmd_Mask
,
711 .state_change_fn
= sclp_tty_state_change
,
712 .receiver_fn
= sclp_tty_receiver
715 static struct tty_operations sclp_ops
= {
716 .open
= sclp_tty_open
,
717 .close
= sclp_tty_close
,
718 .write
= sclp_tty_write
,
719 .put_char
= sclp_tty_put_char
,
720 .flush_chars
= sclp_tty_flush_chars
,
721 .write_room
= sclp_tty_write_room
,
722 .chars_in_buffer
= sclp_tty_chars_in_buffer
,
723 .flush_buffer
= sclp_tty_flush_buffer
,
724 .ioctl
= sclp_tty_ioctl
,
730 struct tty_driver
*driver
;
735 if (!CONSOLE_IS_SCLP
)
737 driver
= alloc_tty_driver(1);
743 printk(KERN_ERR SCLP_TTY_PRINT_HEADER
744 "could not register tty - "
745 "sclp_rw_init returned %d\n", rc
);
746 put_tty_driver(driver
);
749 /* Allocate pages for output buffering */
750 INIT_LIST_HEAD(&sclp_tty_pages
);
751 for (i
= 0; i
< MAX_KMEM_PAGES
; i
++) {
752 page
= (void *) get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
754 put_tty_driver(driver
);
757 list_add_tail((struct list_head
*) page
, &sclp_tty_pages
);
759 INIT_LIST_HEAD(&sclp_tty_outqueue
);
760 spin_lock_init(&sclp_tty_lock
);
761 init_waitqueue_head(&sclp_tty_waitq
);
762 init_timer(&sclp_tty_timer
);
764 sclp_tty_buffer_count
= 0;
767 * save 4 characters for the CPU number
768 * written at start of each line by VM/CP
770 sclp_ioctls_init
.columns
= 76;
771 /* case input lines to lowercase */
772 sclp_ioctls_init
.tolower
= 1;
774 sclp_ioctls
= sclp_ioctls_init
;
775 sclp_tty_chars_count
= 0;
778 rc
= sclp_register(&sclp_input_event
);
780 put_tty_driver(driver
);
784 driver
->owner
= THIS_MODULE
;
785 driver
->driver_name
= "sclp_line";
786 driver
->name
= "sclp_line";
787 driver
->major
= TTY_MAJOR
;
788 driver
->minor_start
= 64;
789 driver
->type
= TTY_DRIVER_TYPE_SYSTEM
;
790 driver
->subtype
= SYSTEM_TYPE_TTY
;
791 driver
->init_termios
= tty_std_termios
;
792 driver
->init_termios
.c_iflag
= IGNBRK
| IGNPAR
;
793 driver
->init_termios
.c_oflag
= ONLCR
| XTABS
;
794 driver
->init_termios
.c_lflag
= ISIG
| ECHO
;
795 driver
->flags
= TTY_DRIVER_REAL_RAW
;
796 tty_set_operations(driver
, &sclp_ops
);
797 rc
= tty_register_driver(driver
);
799 printk(KERN_ERR SCLP_TTY_PRINT_HEADER
800 "could not register tty - "
801 "tty_register_driver returned %d\n", rc
);
802 put_tty_driver(driver
);
805 sclp_tty_driver
= driver
;
808 module_init(sclp_tty_init
);