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/err.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/gfp.h>
20 #include <asm/uaccess.h>
28 * size of a buffer that collects single characters coming in
29 * via sclp_tty_put_char()
31 #define SCLP_TTY_BUF_SIZE 512
34 * There is exactly one SCLP terminal, so we can keep things simple
35 * and allocate all variables statically.
38 /* Lock to guard over changes to global variables. */
39 static spinlock_t sclp_tty_lock;
40 /* List of free pages that can be used for console output buffering. */
41 static struct list_head sclp_tty_pages;
42 /* List of full struct sclp_buffer structures ready for output. */
43 static struct list_head sclp_tty_outqueue;
44 /* Counter how many buffers are emitted. */
45 static int sclp_tty_buffer_count;
46 /* Pointer to current console buffer. */
47 static struct sclp_buffer *sclp_ttybuf;
48 /* Timer for delayed output of console messages. */
49 static struct timer_list sclp_tty_timer;
51 static struct tty_port sclp_port;
52 static unsigned char sclp_tty_chars[SCLP_TTY_BUF_SIZE];
53 static unsigned short int sclp_tty_chars_count;
55 struct tty_driver *sclp_tty_driver;
57 static int sclp_tty_tolower;
58 static int sclp_tty_columns = 80;
60 #define SPACES_PER_TAB 8
61 #define CASE_DELIMITER 0x6c /* to separate upper and lower case (% in EBCDIC) */
63 /* This routine is called whenever we try to open a SCLP terminal. */
65 sclp_tty_open(struct tty_struct *tty, struct file *filp)
67 tty_port_tty_set(&sclp_port, tty);
68 tty->driver_data = NULL;
73 /* This routine is called when the SCLP terminal is closed. */
75 sclp_tty_close(struct tty_struct *tty, struct file *filp)
79 tty_port_tty_set(&sclp_port, NULL);
83 * This routine returns the numbers of characters the tty driver
84 * will accept for queuing to be written. This number is subject
85 * to change as output buffers get emptied, or if the output flow
86 * control is acted. This is not an exact number because not every
87 * character needs the same space in the sccb. The worst case is
88 * a string of newlines. Every newlines creates a new mto which
92 sclp_tty_write_room (struct tty_struct *tty)
98 spin_lock_irqsave(&sclp_tty_lock, flags);
100 if (sclp_ttybuf != NULL)
101 count = sclp_buffer_space(sclp_ttybuf) / sizeof(struct mto);
102 list_for_each(l, &sclp_tty_pages)
103 count += NR_EMPTY_MTO_PER_SCCB;
104 spin_unlock_irqrestore(&sclp_tty_lock, flags);
109 sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc)
111 struct tty_struct *tty;
116 page = sclp_unmake_buffer(buffer);
117 spin_lock_irqsave(&sclp_tty_lock, flags);
118 /* Remove buffer from outqueue */
119 list_del(&buffer->list);
120 sclp_tty_buffer_count--;
121 list_add_tail((struct list_head *) page, &sclp_tty_pages);
122 /* Check if there is a pending buffer on the out queue. */
124 if (!list_empty(&sclp_tty_outqueue))
125 buffer = list_entry(sclp_tty_outqueue.next,
126 struct sclp_buffer, list);
127 spin_unlock_irqrestore(&sclp_tty_lock, flags);
128 } while (buffer && sclp_emit_buffer(buffer, sclp_ttybuf_callback));
129 /* check if the tty needs a wake up call */
130 tty = tty_port_tty_get(&sclp_port);
138 __sclp_ttybuf_emit(struct sclp_buffer *buffer)
144 spin_lock_irqsave(&sclp_tty_lock, flags);
145 list_add_tail(&buffer->list, &sclp_tty_outqueue);
146 count = sclp_tty_buffer_count++;
147 spin_unlock_irqrestore(&sclp_tty_lock, flags);
150 rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback);
152 sclp_ttybuf_callback(buffer, rc);
156 * When this routine is called from the timer then we flush the
157 * temporary write buffer.
160 sclp_tty_timeout(unsigned long data)
163 struct sclp_buffer *buf;
165 spin_lock_irqsave(&sclp_tty_lock, flags);
168 spin_unlock_irqrestore(&sclp_tty_lock, flags);
171 __sclp_ttybuf_emit(buf);
176 * Write a string to the sclp tty.
178 static int sclp_tty_write_string(const unsigned char *str, int count, int may_fail)
184 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);
199 spin_lock_irqsave(&sclp_tty_lock, flags);
201 page = sclp_tty_pages.next;
202 list_del((struct list_head *) page);
203 sclp_ttybuf = sclp_make_buffer(page, sclp_tty_columns,
206 /* try to write the string to the current output buffer */
207 written = sclp_write(sclp_ttybuf, str, count);
208 overall_written += written;
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);
235 return overall_written;
239 * This routine is called by the kernel to write a series of characters to the
240 * tty device. The characters may come from user space or kernel space. This
241 * routine will return the number of characters actually accepted for writing.
244 sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
246 if (sclp_tty_chars_count > 0) {
247 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
248 sclp_tty_chars_count = 0;
250 return sclp_tty_write_string(buf, count, 1);
254 * This routine is called by the kernel to write a single character to the tty
255 * device. If the kernel uses this routine, it must call the flush_chars()
256 * routine (if defined) when it is done stuffing characters into the driver.
258 * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver.
259 * If the given character is a '\n' the contents of the SCLP write buffer
260 * - including previous characters from sclp_tty_put_char() and strings from
261 * sclp_write() without final '\n' - will be written.
264 sclp_tty_put_char(struct tty_struct *tty, unsigned char ch)
266 sclp_tty_chars[sclp_tty_chars_count++] = ch;
267 if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) {
268 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
269 sclp_tty_chars_count = 0;
275 * This routine is called by the kernel after it has written a series of
276 * characters to the tty device using put_char().
279 sclp_tty_flush_chars(struct tty_struct *tty)
281 if (sclp_tty_chars_count > 0) {
282 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
283 sclp_tty_chars_count = 0;
288 * This routine returns the number of characters in the write buffer of the
289 * SCLP driver. The provided number includes all characters that are stored
290 * in the SCCB (will be written next time the SCLP is not busy) as well as
291 * characters in the write buffer (will not be written as long as there is a
292 * final line feed missing).
295 sclp_tty_chars_in_buffer(struct tty_struct *tty)
299 struct sclp_buffer *t;
302 spin_lock_irqsave(&sclp_tty_lock, flags);
304 if (sclp_ttybuf != NULL)
305 count = sclp_chars_in_buffer(sclp_ttybuf);
306 list_for_each(l, &sclp_tty_outqueue) {
307 t = list_entry(l, struct sclp_buffer, list);
308 count += sclp_chars_in_buffer(t);
310 spin_unlock_irqrestore(&sclp_tty_lock, flags);
315 * removes all content from buffers of low level driver
318 sclp_tty_flush_buffer(struct tty_struct *tty)
320 if (sclp_tty_chars_count > 0) {
321 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
322 sclp_tty_chars_count = 0;
330 sclp_tty_input(unsigned char* buf, unsigned int count)
332 struct tty_struct *tty = tty_port_tty_get(&sclp_port);
336 * If this tty driver is currently closed
337 * then throw the received input away.
341 cchar = ctrlchar_handle(buf, count, tty);
342 switch (cchar & CTRLCHAR_MASK) {
346 tty_insert_flip_char(tty, cchar, TTY_NORMAL);
347 tty_flip_buffer_push(tty);
350 /* send (normal) input to line discipline */
352 (strncmp((const char *) buf + count - 2, "^n", 2) &&
353 strncmp((const char *) buf + count - 2, "\252n", 2))) {
354 /* add the auto \n */
355 tty_insert_flip_string(tty, buf, count);
356 tty_insert_flip_char(tty, '\n', TTY_NORMAL);
358 tty_insert_flip_string(tty, buf, count - 2);
359 tty_flip_buffer_push(tty);
366 * get a EBCDIC string in upper/lower case,
367 * find out characters in lower/upper case separated by a special character,
368 * modifiy original string,
369 * returns length of resulting string
371 static int sclp_switch_cases(unsigned char *buf, int count)
373 unsigned char *ip, *op;
376 /* initially changing case is off */
379 while (count-- > 0) {
380 /* compare with special character */
381 if (*ip == CASE_DELIMITER) {
382 /* followed by another special character? */
383 if (count && ip[1] == CASE_DELIMITER) {
385 * ... then put a single copy of the special
386 * character to the output string
392 * ... special character follower by a normal
393 * character toggles the case change behaviour
396 /* skip special character */
399 /* not the special character */
401 /* but case switching is on */
402 if (sclp_tty_tolower)
403 /* switch to uppercase */
404 *op++ = _ebc_toupper[(int) *ip++];
406 /* switch to lowercase */
407 *op++ = _ebc_tolower[(int) *ip++];
409 /* no case switching, copy the character */
412 /* return length of reformatted string. */
416 static void sclp_get_input(struct gds_subvector *sv)
421 str = (unsigned char *) (sv + 1);
422 count = sv->length - sizeof(*sv);
423 if (sclp_tty_tolower)
424 EBC_TOLOWER(str, count);
425 count = sclp_switch_cases(str, count);
426 /* convert EBCDIC to ASCII (modify original input in SCCB) */
427 sclp_ebcasc_str(str, count);
429 /* transfer input to high level driver */
430 sclp_tty_input(str, count);
433 static inline void sclp_eval_selfdeftextmsg(struct gds_subvector *sv)
437 end = (void *) sv + sv->length;
438 for (sv = sv + 1; (void *) sv < end; sv = (void *) sv + sv->length)
443 static inline void sclp_eval_textcmd(struct gds_vector *v)
445 struct gds_subvector *sv;
448 end = (void *) v + v->length;
449 for (sv = (struct gds_subvector *) (v + 1);
450 (void *) sv < end; sv = (void *) sv + sv->length)
451 if (sv->key == GDS_KEY_SELFDEFTEXTMSG)
452 sclp_eval_selfdeftextmsg(sv);
456 static inline void sclp_eval_cpmsu(struct gds_vector *v)
460 end = (void *) v + v->length;
461 for (v = v + 1; (void *) v < end; v = (void *) v + v->length)
462 if (v->gds_id == GDS_ID_TEXTCMD)
463 sclp_eval_textcmd(v);
467 static inline void sclp_eval_mdsmu(struct gds_vector *v)
469 v = sclp_find_gds_vector(v + 1, (void *) v + v->length, GDS_ID_CPMSU);
474 static void sclp_tty_receiver(struct evbuf_header *evbuf)
476 struct gds_vector *v;
478 v = sclp_find_gds_vector(evbuf + 1, (void *) evbuf + evbuf->length,
485 sclp_tty_state_change(struct sclp_register *reg)
489 static struct sclp_register sclp_input_event =
491 .receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK,
492 .state_change_fn = sclp_tty_state_change,
493 .receiver_fn = sclp_tty_receiver
496 static const struct tty_operations sclp_ops = {
497 .open = sclp_tty_open,
498 .close = sclp_tty_close,
499 .write = sclp_tty_write,
500 .put_char = sclp_tty_put_char,
501 .flush_chars = sclp_tty_flush_chars,
502 .write_room = sclp_tty_write_room,
503 .chars_in_buffer = sclp_tty_chars_in_buffer,
504 .flush_buffer = sclp_tty_flush_buffer,
510 struct tty_driver *driver;
515 if (!CONSOLE_IS_SCLP)
517 driver = alloc_tty_driver(1);
523 put_tty_driver(driver);
526 /* Allocate pages for output buffering */
527 INIT_LIST_HEAD(&sclp_tty_pages);
528 for (i = 0; i < MAX_KMEM_PAGES; i++) {
529 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
531 put_tty_driver(driver);
534 list_add_tail((struct list_head *) page, &sclp_tty_pages);
536 INIT_LIST_HEAD(&sclp_tty_outqueue);
537 spin_lock_init(&sclp_tty_lock);
538 init_timer(&sclp_tty_timer);
540 sclp_tty_buffer_count = 0;
543 * save 4 characters for the CPU number
544 * written at start of each line by VM/CP
546 sclp_tty_columns = 76;
547 /* case input lines to lowercase */
548 sclp_tty_tolower = 1;
550 sclp_tty_chars_count = 0;
551 tty_port_init(&sclp_port);
553 rc = sclp_register(&sclp_input_event);
555 put_tty_driver(driver);
559 driver->driver_name = "sclp_line";
560 driver->name = "sclp_line";
561 driver->major = TTY_MAJOR;
562 driver->minor_start = 64;
563 driver->type = TTY_DRIVER_TYPE_SYSTEM;
564 driver->subtype = SYSTEM_TYPE_TTY;
565 driver->init_termios = tty_std_termios;
566 driver->init_termios.c_iflag = IGNBRK | IGNPAR;
567 driver->init_termios.c_oflag = ONLCR | XTABS;
568 driver->init_termios.c_lflag = ISIG | ECHO;
569 driver->flags = TTY_DRIVER_REAL_RAW;
570 tty_set_operations(driver, &sclp_ops);
571 rc = tty_register_driver(driver);
573 put_tty_driver(driver);
576 sclp_tty_driver = driver;
579 module_init(sclp_tty_init);