1 // SPDX-License-Identifier: GPL-2.0
3 * SCLP line mode terminal driver.
6 * Copyright IBM Corp. 1999
7 * Author(s): Martin Peschke <mpeschke@de.ibm.com>
8 * Martin Schwidefsky <schwidefsky@de.ibm.com>
11 #include <linux/kmod.h>
12 #include <linux/tty.h>
13 #include <linux/tty_driver.h>
14 #include <linux/tty_flip.h>
15 #include <linux/err.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/gfp.h>
19 #include <linux/uaccess.h>
27 * size of a buffer that collects single characters coming in
28 * via sclp_tty_put_char()
30 #define SCLP_TTY_BUF_SIZE 512
33 * There is exactly one SCLP terminal, so we can keep things simple
34 * and allocate all variables statically.
37 /* Lock to guard over changes to global variables. */
38 static DEFINE_SPINLOCK(sclp_tty_lock
);
39 /* List of free pages that can be used for console output buffering. */
40 static LIST_HEAD(sclp_tty_pages
);
41 /* List of full struct sclp_buffer structures ready for output. */
42 static LIST_HEAD(sclp_tty_outqueue
);
43 /* Counter how many buffers are emitted. */
44 static int sclp_tty_buffer_count
;
45 /* Pointer to current console buffer. */
46 static struct sclp_buffer
*sclp_ttybuf
;
47 /* Timer for delayed output of console messages. */
48 static struct timer_list sclp_tty_timer
;
50 static struct tty_port sclp_port
;
51 static u8 sclp_tty_chars
[SCLP_TTY_BUF_SIZE
];
52 static unsigned short int sclp_tty_chars_count
;
54 struct tty_driver
*sclp_tty_driver
;
56 static int sclp_tty_tolower
;
58 #define SCLP_TTY_COLUMNS 320
59 #define SPACES_PER_TAB 8
60 #define CASE_DELIMITER 0x6c /* to separate upper and lower case (% in EBCDIC) */
62 /* This routine is called whenever we try to open a SCLP terminal. */
64 sclp_tty_open(struct tty_struct
*tty
, struct file
*filp
)
66 tty_port_tty_set(&sclp_port
, tty
);
67 tty
->driver_data
= NULL
;
71 /* This routine is called when the SCLP terminal is closed. */
73 sclp_tty_close(struct tty_struct
*tty
, struct file
*filp
)
77 tty_port_tty_set(&sclp_port
, NULL
);
81 * This routine returns the numbers of characters the tty driver
82 * will accept for queuing to be written. This number is subject
83 * to change as output buffers get emptied, or if the output flow
84 * control is acted. This is not an exact number because not every
85 * character needs the same space in the sccb. The worst case is
86 * a string of newlines. Every newline creates a new message which
90 sclp_tty_write_room (struct tty_struct
*tty
)
96 spin_lock_irqsave(&sclp_tty_lock
, flags
);
98 if (sclp_ttybuf
!= NULL
)
99 count
= sclp_buffer_space(sclp_ttybuf
) / sizeof(struct msg_buf
);
100 list_for_each(l
, &sclp_tty_pages
)
101 count
+= NR_EMPTY_MSG_PER_SCCB
;
102 spin_unlock_irqrestore(&sclp_tty_lock
, flags
);
107 sclp_ttybuf_callback(struct sclp_buffer
*buffer
, int rc
)
113 page
= sclp_unmake_buffer(buffer
);
114 spin_lock_irqsave(&sclp_tty_lock
, flags
);
115 /* Remove buffer from outqueue */
116 list_del(&buffer
->list
);
117 sclp_tty_buffer_count
--;
118 list_add_tail((struct list_head
*) page
, &sclp_tty_pages
);
119 /* Check if there is a pending buffer on the out queue. */
121 if (!list_empty(&sclp_tty_outqueue
))
122 buffer
= list_entry(sclp_tty_outqueue
.next
,
123 struct sclp_buffer
, list
);
124 spin_unlock_irqrestore(&sclp_tty_lock
, flags
);
125 } while (buffer
&& sclp_emit_buffer(buffer
, sclp_ttybuf_callback
));
127 tty_port_tty_wakeup(&sclp_port
);
131 __sclp_ttybuf_emit(struct sclp_buffer
*buffer
)
137 spin_lock_irqsave(&sclp_tty_lock
, flags
);
138 list_add_tail(&buffer
->list
, &sclp_tty_outqueue
);
139 count
= sclp_tty_buffer_count
++;
140 spin_unlock_irqrestore(&sclp_tty_lock
, flags
);
143 rc
= sclp_emit_buffer(buffer
, sclp_ttybuf_callback
);
145 sclp_ttybuf_callback(buffer
, rc
);
149 * When this routine is called from the timer then we flush the
150 * temporary write buffer.
153 sclp_tty_timeout(struct timer_list
*unused
)
156 struct sclp_buffer
*buf
;
158 spin_lock_irqsave(&sclp_tty_lock
, flags
);
161 spin_unlock_irqrestore(&sclp_tty_lock
, flags
);
164 __sclp_ttybuf_emit(buf
);
169 * Write a string to the sclp tty.
171 static int sclp_tty_write_string(const u8
*str
, int count
, int may_fail
)
177 struct sclp_buffer
*buf
;
182 spin_lock_irqsave(&sclp_tty_lock
, flags
);
184 /* Create a sclp output buffer if none exists yet */
185 if (sclp_ttybuf
== NULL
) {
186 while (list_empty(&sclp_tty_pages
)) {
187 spin_unlock_irqrestore(&sclp_tty_lock
, flags
);
192 spin_lock_irqsave(&sclp_tty_lock
, flags
);
194 page
= sclp_tty_pages
.next
;
195 list_del((struct list_head
*) page
);
196 sclp_ttybuf
= sclp_make_buffer(page
, SCLP_TTY_COLUMNS
,
199 /* try to write the string to the current output buffer */
200 written
= sclp_write(sclp_ttybuf
, str
, count
);
201 overall_written
+= written
;
202 if (written
== count
)
205 * Not all characters could be written to the current
206 * output buffer. Emit the buffer, create a new buffer
207 * and then output the rest of the string.
211 spin_unlock_irqrestore(&sclp_tty_lock
, flags
);
212 __sclp_ttybuf_emit(buf
);
213 spin_lock_irqsave(&sclp_tty_lock
, flags
);
217 /* Setup timer to output current console buffer after 1/10 second */
218 if (sclp_ttybuf
&& sclp_chars_in_buffer(sclp_ttybuf
) &&
219 !timer_pending(&sclp_tty_timer
)) {
220 mod_timer(&sclp_tty_timer
, jiffies
+ HZ
/ 10);
222 spin_unlock_irqrestore(&sclp_tty_lock
, flags
);
224 return overall_written
;
228 * This routine is called by the kernel to write a series of characters to the
229 * tty device. The characters may come from user space or kernel space. This
230 * routine will return the number of characters actually accepted for writing.
233 sclp_tty_write(struct tty_struct
*tty
, const u8
*buf
, size_t count
)
235 if (sclp_tty_chars_count
> 0) {
236 sclp_tty_write_string(sclp_tty_chars
, sclp_tty_chars_count
, 0);
237 sclp_tty_chars_count
= 0;
239 return sclp_tty_write_string(buf
, count
, 1);
243 * This routine is called by the kernel to write a single character to the tty
244 * device. If the kernel uses this routine, it must call the flush_chars()
245 * routine (if defined) when it is done stuffing characters into the driver.
247 * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver.
248 * If the given character is a '\n' the contents of the SCLP write buffer
249 * - including previous characters from sclp_tty_put_char() and strings from
250 * sclp_write() without final '\n' - will be written.
253 sclp_tty_put_char(struct tty_struct
*tty
, u8 ch
)
255 sclp_tty_chars
[sclp_tty_chars_count
++] = ch
;
256 if (ch
== '\n' || sclp_tty_chars_count
>= SCLP_TTY_BUF_SIZE
) {
257 sclp_tty_write_string(sclp_tty_chars
, sclp_tty_chars_count
, 0);
258 sclp_tty_chars_count
= 0;
264 * This routine is called by the kernel after it has written a series of
265 * characters to the tty device using put_char().
268 sclp_tty_flush_chars(struct tty_struct
*tty
)
270 if (sclp_tty_chars_count
> 0) {
271 sclp_tty_write_string(sclp_tty_chars
, sclp_tty_chars_count
, 0);
272 sclp_tty_chars_count
= 0;
277 * This routine returns the number of characters in the write buffer of the
278 * SCLP driver. The provided number includes all characters that are stored
279 * in the SCCB (will be written next time the SCLP is not busy) as well as
280 * characters in the write buffer (will not be written as long as there is a
281 * final line feed missing).
284 sclp_tty_chars_in_buffer(struct tty_struct
*tty
)
287 struct sclp_buffer
*t
;
288 unsigned int count
= 0;
290 spin_lock_irqsave(&sclp_tty_lock
, flags
);
291 if (sclp_ttybuf
!= NULL
)
292 count
= sclp_chars_in_buffer(sclp_ttybuf
);
293 list_for_each_entry(t
, &sclp_tty_outqueue
, list
) {
294 count
+= sclp_chars_in_buffer(t
);
296 spin_unlock_irqrestore(&sclp_tty_lock
, flags
);
301 * removes all content from buffers of low level driver
304 sclp_tty_flush_buffer(struct tty_struct
*tty
)
306 if (sclp_tty_chars_count
> 0) {
307 sclp_tty_write_string(sclp_tty_chars
, sclp_tty_chars_count
, 0);
308 sclp_tty_chars_count
= 0;
316 sclp_tty_input(unsigned char* buf
, unsigned int count
)
318 struct tty_struct
*tty
= tty_port_tty_get(&sclp_port
);
322 * If this tty driver is currently closed
323 * then throw the received input away.
327 cchar
= ctrlchar_handle(buf
, count
, tty
);
328 switch (cchar
& CTRLCHAR_MASK
) {
332 tty_insert_flip_char(&sclp_port
, cchar
, TTY_NORMAL
);
333 tty_flip_buffer_push(&sclp_port
);
336 /* send (normal) input to line discipline */
338 (strncmp((const char *) buf
+ count
- 2, "^n", 2) &&
339 strncmp((const char *) buf
+ count
- 2, "\252n", 2))) {
340 /* add the auto \n */
341 tty_insert_flip_string(&sclp_port
, buf
, count
);
342 tty_insert_flip_char(&sclp_port
, '\n', TTY_NORMAL
);
344 tty_insert_flip_string(&sclp_port
, buf
, count
- 2);
345 tty_flip_buffer_push(&sclp_port
);
352 * get a EBCDIC string in upper/lower case,
353 * find out characters in lower/upper case separated by a special character,
354 * modifiy original string,
355 * returns length of resulting string
357 static int sclp_switch_cases(unsigned char *buf
, int count
)
359 unsigned char *ip
, *op
;
362 /* initially changing case is off */
365 while (count
-- > 0) {
366 /* compare with special character */
367 if (*ip
== CASE_DELIMITER
) {
368 /* followed by another special character? */
369 if (count
&& ip
[1] == CASE_DELIMITER
) {
371 * ... then put a single copy of the special
372 * character to the output string
378 * ... special character follower by a normal
379 * character toggles the case change behaviour
382 /* skip special character */
385 /* not the special character */
387 /* but case switching is on */
388 if (sclp_tty_tolower
)
389 /* switch to uppercase */
390 *op
++ = _ebc_toupper
[(int) *ip
++];
392 /* switch to lowercase */
393 *op
++ = _ebc_tolower
[(int) *ip
++];
395 /* no case switching, copy the character */
398 /* return length of reformatted string. */
402 static void sclp_get_input(struct gds_subvector
*sv
)
407 str
= (unsigned char *) (sv
+ 1);
408 count
= sv
->length
- sizeof(*sv
);
409 if (sclp_tty_tolower
)
410 EBC_TOLOWER(str
, count
);
411 count
= sclp_switch_cases(str
, count
);
412 /* convert EBCDIC to ASCII (modify original input in SCCB) */
413 sclp_ebcasc_str(str
, count
);
415 /* transfer input to high level driver */
416 sclp_tty_input(str
, count
);
419 static inline void sclp_eval_selfdeftextmsg(struct gds_subvector
*sv
)
423 end
= (void *) sv
+ sv
->length
;
424 for (sv
= sv
+ 1; (void *) sv
< end
; sv
= (void *) sv
+ sv
->length
)
429 static inline void sclp_eval_textcmd(struct gds_vector
*v
)
431 struct gds_subvector
*sv
;
434 end
= (void *) v
+ v
->length
;
435 for (sv
= (struct gds_subvector
*) (v
+ 1);
436 (void *) sv
< end
; sv
= (void *) sv
+ sv
->length
)
437 if (sv
->key
== GDS_KEY_SELFDEFTEXTMSG
)
438 sclp_eval_selfdeftextmsg(sv
);
442 static inline void sclp_eval_cpmsu(struct gds_vector
*v
)
446 end
= (void *) v
+ v
->length
;
447 for (v
= v
+ 1; (void *) v
< end
; v
= (void *) v
+ v
->length
)
448 if (v
->gds_id
== GDS_ID_TEXTCMD
)
449 sclp_eval_textcmd(v
);
453 static inline void sclp_eval_mdsmu(struct gds_vector
*v
)
455 v
= sclp_find_gds_vector(v
+ 1, (void *) v
+ v
->length
, GDS_ID_CPMSU
);
460 static void sclp_tty_receiver(struct evbuf_header
*evbuf
)
462 struct gds_vector
*v
;
464 v
= sclp_find_gds_vector(evbuf
+ 1, (void *) evbuf
+ evbuf
->length
,
471 sclp_tty_state_change(struct sclp_register
*reg
)
475 static struct sclp_register sclp_input_event
=
477 .receive_mask
= EVTYP_OPCMD_MASK
| EVTYP_PMSGCMD_MASK
,
478 .state_change_fn
= sclp_tty_state_change
,
479 .receiver_fn
= sclp_tty_receiver
482 static const struct tty_operations sclp_ops
= {
483 .open
= sclp_tty_open
,
484 .close
= sclp_tty_close
,
485 .write
= sclp_tty_write
,
486 .put_char
= sclp_tty_put_char
,
487 .flush_chars
= sclp_tty_flush_chars
,
488 .write_room
= sclp_tty_write_room
,
489 .chars_in_buffer
= sclp_tty_chars_in_buffer
,
490 .flush_buffer
= sclp_tty_flush_buffer
,
496 struct tty_driver
*driver
;
501 /* z/VM multiplexes the line mode output on the 32xx screen */
502 if (MACHINE_IS_VM
&& !CONSOLE_IS_SCLP
)
504 if (!sclp
.has_linemode
)
506 driver
= tty_alloc_driver(1, TTY_DRIVER_REAL_RAW
);
508 return PTR_ERR(driver
);
512 tty_driver_kref_put(driver
);
515 /* Allocate pages for output buffering */
516 for (i
= 0; i
< MAX_KMEM_PAGES
; i
++) {
517 page
= (void *) get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
519 tty_driver_kref_put(driver
);
522 list_add_tail((struct list_head
*) page
, &sclp_tty_pages
);
524 timer_setup(&sclp_tty_timer
, sclp_tty_timeout
, 0);
526 sclp_tty_buffer_count
= 0;
528 /* case input lines to lowercase */
529 sclp_tty_tolower
= 1;
531 sclp_tty_chars_count
= 0;
533 rc
= sclp_register(&sclp_input_event
);
535 tty_driver_kref_put(driver
);
539 tty_port_init(&sclp_port
);
541 driver
->driver_name
= "sclp_line";
542 driver
->name
= "sclp_line";
543 driver
->major
= TTY_MAJOR
;
544 driver
->minor_start
= 64;
545 driver
->type
= TTY_DRIVER_TYPE_SYSTEM
;
546 driver
->subtype
= SYSTEM_TYPE_TTY
;
547 driver
->init_termios
= tty_std_termios
;
548 driver
->init_termios
.c_iflag
= IGNBRK
| IGNPAR
;
549 driver
->init_termios
.c_oflag
= ONLCR
;
550 driver
->init_termios
.c_lflag
= ISIG
| ECHO
;
551 tty_set_operations(driver
, &sclp_ops
);
552 tty_port_link_device(&sclp_port
, driver
, 0);
553 rc
= tty_register_driver(driver
);
555 tty_driver_kref_put(driver
);
556 tty_port_destroy(&sclp_port
);
559 sclp_tty_driver
= driver
;
562 device_initcall(sclp_tty_init
);