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>
29 * size of a buffer that collects single characters coming in
30 * via sclp_tty_put_char()
32 #define SCLP_TTY_BUF_SIZE 512
35 * There is exactly one SCLP terminal, so we can keep things simple
36 * and allocate all variables statically.
39 /* Lock to guard over changes to global variables. */
40 static spinlock_t sclp_tty_lock;
41 /* List of free pages that can be used for console output buffering. */
42 static struct list_head sclp_tty_pages;
43 /* List of full struct sclp_buffer structures ready for output. */
44 static struct list_head sclp_tty_outqueue;
45 /* Counter how many buffers are emitted. */
46 static int sclp_tty_buffer_count;
47 /* Pointer to current console buffer. */
48 static struct sclp_buffer *sclp_ttybuf;
49 /* Timer for delayed output of console messages. */
50 static struct timer_list sclp_tty_timer;
51 /* Waitqueue to wait for buffers to get empty. */
52 static wait_queue_head_t sclp_tty_waitq;
54 static struct tty_struct *sclp_tty;
55 static unsigned char sclp_tty_chars[SCLP_TTY_BUF_SIZE];
56 static unsigned short int sclp_tty_chars_count;
58 struct tty_driver *sclp_tty_driver;
60 static int sclp_tty_tolower;
61 static int sclp_tty_columns = 80;
63 #define SPACES_PER_TAB 8
64 #define CASE_DELIMITER 0x6c /* to separate upper and lower case (% in EBCDIC) */
66 /* This routine is called whenever we try to open a SCLP terminal. */
68 sclp_tty_open(struct tty_struct *tty, struct file *filp)
71 tty->driver_data = NULL;
76 /* This routine is called when the SCLP terminal is closed. */
78 sclp_tty_close(struct tty_struct *tty, struct file *filp)
86 * This routine returns the numbers of characters the tty driver
87 * will accept for queuing to be written. This number is subject
88 * to change as output buffers get emptied, or if the output flow
89 * control is acted. This is not an exact number because not every
90 * character needs the same space in the sccb. The worst case is
91 * a string of newlines. Every newlines creates a new mto which
95 sclp_tty_write_room (struct tty_struct *tty)
101 spin_lock_irqsave(&sclp_tty_lock, flags);
103 if (sclp_ttybuf != NULL)
104 count = sclp_buffer_space(sclp_ttybuf) / sizeof(struct mto);
105 list_for_each(l, &sclp_tty_pages)
106 count += NR_EMPTY_MTO_PER_SCCB;
107 spin_unlock_irqrestore(&sclp_tty_lock, flags);
112 sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc)
118 page = sclp_unmake_buffer(buffer);
119 spin_lock_irqsave(&sclp_tty_lock, flags);
120 /* Remove buffer from outqueue */
121 list_del(&buffer->list);
122 sclp_tty_buffer_count--;
123 list_add_tail((struct list_head *) page, &sclp_tty_pages);
124 /* Check if there is a pending buffer on the out queue. */
126 if (!list_empty(&sclp_tty_outqueue))
127 buffer = list_entry(sclp_tty_outqueue.next,
128 struct sclp_buffer, list);
129 spin_unlock_irqrestore(&sclp_tty_lock, flags);
130 } while (buffer && sclp_emit_buffer(buffer, sclp_ttybuf_callback));
131 wake_up(&sclp_tty_waitq);
132 /* check if the tty needs a wake up call */
133 if (sclp_tty != NULL) {
134 tty_wakeup(sclp_tty);
139 __sclp_ttybuf_emit(struct sclp_buffer *buffer)
145 spin_lock_irqsave(&sclp_tty_lock, flags);
146 list_add_tail(&buffer->list, &sclp_tty_outqueue);
147 count = sclp_tty_buffer_count++;
148 spin_unlock_irqrestore(&sclp_tty_lock, flags);
151 rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback);
153 sclp_ttybuf_callback(buffer, rc);
157 * When this routine is called from the timer then we flush the
158 * temporary write buffer.
161 sclp_tty_timeout(unsigned long data)
164 struct sclp_buffer *buf;
166 spin_lock_irqsave(&sclp_tty_lock, flags);
169 spin_unlock_irqrestore(&sclp_tty_lock, flags);
172 __sclp_ttybuf_emit(buf);
177 * Write a string to the sclp tty.
180 sclp_tty_write_string(const unsigned char *str, int count)
185 struct sclp_buffer *buf;
189 spin_lock_irqsave(&sclp_tty_lock, flags);
191 /* Create a sclp output buffer if none exists yet */
192 if (sclp_ttybuf == NULL) {
193 while (list_empty(&sclp_tty_pages)) {
194 spin_unlock_irqrestore(&sclp_tty_lock, flags);
198 wait_event(sclp_tty_waitq,
199 !list_empty(&sclp_tty_pages));
200 spin_lock_irqsave(&sclp_tty_lock, flags);
202 page = sclp_tty_pages.next;
203 list_del((struct list_head *) page);
204 sclp_ttybuf = sclp_make_buffer(page, sclp_tty_columns,
207 /* try to write the string to the current output buffer */
208 written = sclp_write(sclp_ttybuf, str, count);
209 if (written == count)
212 * Not all characters could be written to the current
213 * output buffer. Emit the buffer, create a new buffer
214 * and then output the rest of the string.
218 spin_unlock_irqrestore(&sclp_tty_lock, flags);
219 __sclp_ttybuf_emit(buf);
220 spin_lock_irqsave(&sclp_tty_lock, flags);
224 /* Setup timer to output current console buffer after 1/10 second */
225 if (sclp_ttybuf && sclp_chars_in_buffer(sclp_ttybuf) &&
226 !timer_pending(&sclp_tty_timer)) {
227 init_timer(&sclp_tty_timer);
228 sclp_tty_timer.function = sclp_tty_timeout;
229 sclp_tty_timer.data = 0UL;
230 sclp_tty_timer.expires = jiffies + HZ/10;
231 add_timer(&sclp_tty_timer);
233 spin_unlock_irqrestore(&sclp_tty_lock, flags);
237 * This routine is called by the kernel to write a series of characters to the
238 * tty device. The characters may come from user space or kernel space. This
239 * routine will return the number of characters actually accepted for writing.
242 sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
244 if (sclp_tty_chars_count > 0) {
245 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
246 sclp_tty_chars_count = 0;
248 sclp_tty_write_string(buf, count);
253 * This routine is called by the kernel to write a single character to the tty
254 * device. If the kernel uses this routine, it must call the flush_chars()
255 * routine (if defined) when it is done stuffing characters into the driver.
257 * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver.
258 * If the given character is a '\n' the contents of the SCLP write buffer
259 * - including previous characters from sclp_tty_put_char() and strings from
260 * sclp_write() without final '\n' - will be written.
263 sclp_tty_put_char(struct tty_struct *tty, unsigned char ch)
265 sclp_tty_chars[sclp_tty_chars_count++] = ch;
266 if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) {
267 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
268 sclp_tty_chars_count = 0;
273 * This routine is called by the kernel after it has written a series of
274 * characters to the tty device using put_char().
277 sclp_tty_flush_chars(struct tty_struct *tty)
279 if (sclp_tty_chars_count > 0) {
280 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
281 sclp_tty_chars_count = 0;
286 * This routine returns the number of characters in the write buffer of the
287 * SCLP driver. The provided number includes all characters that are stored
288 * in the SCCB (will be written next time the SCLP is not busy) as well as
289 * characters in the write buffer (will not be written as long as there is a
290 * final line feed missing).
293 sclp_tty_chars_in_buffer(struct tty_struct *tty)
297 struct sclp_buffer *t;
300 spin_lock_irqsave(&sclp_tty_lock, flags);
302 if (sclp_ttybuf != NULL)
303 count = sclp_chars_in_buffer(sclp_ttybuf);
304 list_for_each(l, &sclp_tty_outqueue) {
305 t = list_entry(l, struct sclp_buffer, list);
306 count += sclp_chars_in_buffer(t);
308 spin_unlock_irqrestore(&sclp_tty_lock, flags);
313 * removes all content from buffers of low level driver
316 sclp_tty_flush_buffer(struct tty_struct *tty)
318 if (sclp_tty_chars_count > 0) {
319 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
320 sclp_tty_chars_count = 0;
328 sclp_tty_input(unsigned char* buf, unsigned int count)
333 * If this tty driver is currently closed
334 * then throw the received input away.
336 if (sclp_tty == NULL)
338 cchar = ctrlchar_handle(buf, count, sclp_tty);
339 switch (cchar & CTRLCHAR_MASK) {
343 tty_insert_flip_char(sclp_tty, cchar, TTY_NORMAL);
344 tty_flip_buffer_push(sclp_tty);
347 /* send (normal) input to line discipline */
349 (strncmp((const char *) buf + count - 2, "^n", 2) &&
350 strncmp((const char *) buf + count - 2, "\252n", 2))) {
351 /* add the auto \n */
352 tty_insert_flip_string(sclp_tty, buf, count);
353 tty_insert_flip_char(sclp_tty, '\n', TTY_NORMAL);
355 tty_insert_flip_string(sclp_tty, buf, count - 2);
356 tty_flip_buffer_push(sclp_tty);
362 * get a EBCDIC string in upper/lower case,
363 * find out characters in lower/upper case separated by a special character,
364 * modifiy original string,
365 * returns length of resulting string
367 static int sclp_switch_cases(unsigned char *buf, int count)
369 unsigned char *ip, *op;
372 /* initially changing case is off */
375 while (count-- > 0) {
376 /* compare with special character */
377 if (*ip == CASE_DELIMITER) {
378 /* followed by another special character? */
379 if (count && ip[1] == CASE_DELIMITER) {
381 * ... then put a single copy of the special
382 * character to the output string
388 * ... special character follower by a normal
389 * character toggles the case change behaviour
392 /* skip special character */
395 /* not the special character */
397 /* but case switching is on */
398 if (sclp_tty_tolower)
399 /* switch to uppercase */
400 *op++ = _ebc_toupper[(int) *ip++];
402 /* switch to lowercase */
403 *op++ = _ebc_tolower[(int) *ip++];
405 /* no case switching, copy the character */
408 /* return length of reformatted string. */
413 sclp_get_input(unsigned char *start, unsigned char *end)
418 if (sclp_tty_tolower)
419 EBC_TOLOWER(start, count);
420 count = sclp_switch_cases(start, count);
421 /* convert EBCDIC to ASCII (modify original input in SCCB) */
422 sclp_ebcasc_str(start, count);
424 /* transfer input to high level driver */
425 sclp_tty_input(start, count);
428 static inline struct gds_vector *
429 find_gds_vector(struct gds_vector *start, struct gds_vector *end, u16 id)
431 struct gds_vector *vec;
433 for (vec = start; vec < end; vec = (void *) vec + vec->length)
434 if (vec->gds_id == id)
439 static inline struct gds_subvector *
440 find_gds_subvector(struct gds_subvector *start,
441 struct gds_subvector *end, u8 key)
443 struct gds_subvector *subvec;
445 for (subvec = start; subvec < end;
446 subvec = (void *) subvec + subvec->length)
447 if (subvec->key == key)
453 sclp_eval_selfdeftextmsg(struct gds_subvector *start,
454 struct gds_subvector *end)
456 struct gds_subvector *subvec;
459 while (subvec < end) {
460 subvec = find_gds_subvector(subvec, end, 0x30);
463 sclp_get_input((unsigned char *)(subvec + 1),
464 (unsigned char *) subvec + subvec->length);
465 subvec = (void *) subvec + subvec->length;
470 sclp_eval_textcmd(struct gds_subvector *start,
471 struct gds_subvector *end)
473 struct gds_subvector *subvec;
476 while (subvec < end) {
477 subvec = find_gds_subvector(subvec, end,
478 GDS_KEY_SELFDEFTEXTMSG);
481 sclp_eval_selfdeftextmsg((struct gds_subvector *)(subvec + 1),
482 (void *)subvec + subvec->length);
483 subvec = (void *) subvec + subvec->length;
488 sclp_eval_cpmsu(struct gds_vector *start, struct gds_vector *end)
490 struct gds_vector *vec;
494 vec = find_gds_vector(vec, end, GDS_ID_TEXTCMD);
497 sclp_eval_textcmd((struct gds_subvector *)(vec + 1),
498 (void *) vec + vec->length);
499 vec = (void *) vec + vec->length;
505 sclp_eval_mdsmu(struct gds_vector *start, void *end)
507 struct gds_vector *vec;
509 vec = find_gds_vector(start, end, GDS_ID_CPMSU);
511 sclp_eval_cpmsu(vec + 1, (void *) vec + vec->length);
515 sclp_tty_receiver(struct evbuf_header *evbuf)
517 struct gds_vector *start, *end, *vec;
519 start = (struct gds_vector *)(evbuf + 1);
520 end = (void *) evbuf + evbuf->length;
521 vec = find_gds_vector(start, end, GDS_ID_MDSMU);
523 sclp_eval_mdsmu(vec + 1, (void *) vec + vec->length);
527 sclp_tty_state_change(struct sclp_register *reg)
531 static struct sclp_register sclp_input_event =
533 .receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK,
534 .state_change_fn = sclp_tty_state_change,
535 .receiver_fn = sclp_tty_receiver
538 static const struct tty_operations sclp_ops = {
539 .open = sclp_tty_open,
540 .close = sclp_tty_close,
541 .write = sclp_tty_write,
542 .put_char = sclp_tty_put_char,
543 .flush_chars = sclp_tty_flush_chars,
544 .write_room = sclp_tty_write_room,
545 .chars_in_buffer = sclp_tty_chars_in_buffer,
546 .flush_buffer = sclp_tty_flush_buffer,
552 struct tty_driver *driver;
557 if (!CONSOLE_IS_SCLP)
559 driver = alloc_tty_driver(1);
565 put_tty_driver(driver);
568 /* Allocate pages for output buffering */
569 INIT_LIST_HEAD(&sclp_tty_pages);
570 for (i = 0; i < MAX_KMEM_PAGES; i++) {
571 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
573 put_tty_driver(driver);
576 list_add_tail((struct list_head *) page, &sclp_tty_pages);
578 INIT_LIST_HEAD(&sclp_tty_outqueue);
579 spin_lock_init(&sclp_tty_lock);
580 init_waitqueue_head(&sclp_tty_waitq);
581 init_timer(&sclp_tty_timer);
583 sclp_tty_buffer_count = 0;
586 * save 4 characters for the CPU number
587 * written at start of each line by VM/CP
589 sclp_tty_columns = 76;
590 /* case input lines to lowercase */
591 sclp_tty_tolower = 1;
593 sclp_tty_chars_count = 0;
596 rc = sclp_register(&sclp_input_event);
598 put_tty_driver(driver);
602 driver->owner = THIS_MODULE;
603 driver->driver_name = "sclp_line";
604 driver->name = "sclp_line";
605 driver->major = TTY_MAJOR;
606 driver->minor_start = 64;
607 driver->type = TTY_DRIVER_TYPE_SYSTEM;
608 driver->subtype = SYSTEM_TYPE_TTY;
609 driver->init_termios = tty_std_termios;
610 driver->init_termios.c_iflag = IGNBRK | IGNPAR;
611 driver->init_termios.c_oflag = ONLCR | XTABS;
612 driver->init_termios.c_lflag = ISIG | ECHO;
613 driver->flags = TTY_DRIVER_REAL_RAW;
614 tty_set_operations(driver, &sclp_ops);
615 rc = tty_register_driver(driver);
617 put_tty_driver(driver);
620 sclp_tty_driver = driver;
623 module_init(sclp_tty_init);