xref: /linux/drivers/s390/char/sclp_tty.c (revision 38fe0e0156c037c060f81fe4e36549fae760322d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    SCLP line mode terminal driver.
4  *
5  *  S390 version
6  *    Copyright IBM Corp. 1999
7  *    Author(s): Martin Peschke <mpeschke@de.ibm.com>
8  *		 Martin Schwidefsky <schwidefsky@de.ibm.com>
9  */
10 
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>
20 
21 #include "ctrlchar.h"
22 #include "sclp.h"
23 #include "sclp_rw.h"
24 #include "sclp_tty.h"
25 
26 /*
27  * size of a buffer that collects single characters coming in
28  * via sclp_tty_put_char()
29  */
30 #define SCLP_TTY_BUF_SIZE 512
31 
32 /*
33  * There is exactly one SCLP terminal, so we can keep things simple
34  * and allocate all variables statically.
35  */
36 
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;
49 
50 static struct tty_port sclp_port;
51 static unsigned char sclp_tty_chars[SCLP_TTY_BUF_SIZE];
52 static unsigned short int sclp_tty_chars_count;
53 
54 struct tty_driver *sclp_tty_driver;
55 
56 static int sclp_tty_tolower;
57 
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) */
61 
62 /* This routine is called whenever we try to open a SCLP terminal. */
63 static int
64 sclp_tty_open(struct tty_struct *tty, struct file *filp)
65 {
66 	tty_port_tty_set(&sclp_port, tty);
67 	tty->driver_data = NULL;
68 	return 0;
69 }
70 
71 /* This routine is called when the SCLP terminal is closed. */
72 static void
73 sclp_tty_close(struct tty_struct *tty, struct file *filp)
74 {
75 	if (tty->count > 1)
76 		return;
77 	tty_port_tty_set(&sclp_port, NULL);
78 }
79 
80 /*
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
87  * needs 82 bytes.
88  */
89 static int
90 sclp_tty_write_room (struct tty_struct *tty)
91 {
92 	unsigned long flags;
93 	struct list_head *l;
94 	int count;
95 
96 	spin_lock_irqsave(&sclp_tty_lock, flags);
97 	count = 0;
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);
103 	return count;
104 }
105 
106 static void
107 sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc)
108 {
109 	unsigned long flags;
110 	void *page;
111 
112 	do {
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. */
120 		buffer = NULL;
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));
126 
127 	tty_port_tty_wakeup(&sclp_port);
128 }
129 
130 static inline void
131 __sclp_ttybuf_emit(struct sclp_buffer *buffer)
132 {
133 	unsigned long flags;
134 	int count;
135 	int rc;
136 
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);
141 	if (count)
142 		return;
143 	rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback);
144 	if (rc)
145 		sclp_ttybuf_callback(buffer, rc);
146 }
147 
148 /*
149  * When this routine is called from the timer then we flush the
150  * temporary write buffer.
151  */
152 static void
153 sclp_tty_timeout(struct timer_list *unused)
154 {
155 	unsigned long flags;
156 	struct sclp_buffer *buf;
157 
158 	spin_lock_irqsave(&sclp_tty_lock, flags);
159 	buf = sclp_ttybuf;
160 	sclp_ttybuf = NULL;
161 	spin_unlock_irqrestore(&sclp_tty_lock, flags);
162 
163 	if (buf != NULL) {
164 		__sclp_ttybuf_emit(buf);
165 	}
166 }
167 
168 /*
169  * Write a string to the sclp tty.
170  */
171 static int sclp_tty_write_string(const unsigned char *str, int count, int may_fail)
172 {
173 	unsigned long flags;
174 	void *page;
175 	int written;
176 	int overall_written;
177 	struct sclp_buffer *buf;
178 
179 	if (count <= 0)
180 		return 0;
181 	overall_written = 0;
182 	spin_lock_irqsave(&sclp_tty_lock, flags);
183 	do {
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);
188 				if (may_fail)
189 					goto out;
190 				else
191 					sclp_sync_wait();
192 				spin_lock_irqsave(&sclp_tty_lock, flags);
193 			}
194 			page = sclp_tty_pages.next;
195 			list_del((struct list_head *) page);
196 			sclp_ttybuf = sclp_make_buffer(page, SCLP_TTY_COLUMNS,
197 						       SPACES_PER_TAB);
198 		}
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)
203 			break;
204 		/*
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.
208 		 */
209 		buf = sclp_ttybuf;
210 		sclp_ttybuf = NULL;
211 		spin_unlock_irqrestore(&sclp_tty_lock, flags);
212 		__sclp_ttybuf_emit(buf);
213 		spin_lock_irqsave(&sclp_tty_lock, flags);
214 		str += written;
215 		count -= written;
216 	} while (count > 0);
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);
221 	}
222 	spin_unlock_irqrestore(&sclp_tty_lock, flags);
223 out:
224 	return overall_written;
225 }
226 
227 /*
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.
231  */
232 static int
233 sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
234 {
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;
238 	}
239 	return sclp_tty_write_string(buf, count, 1);
240 }
241 
242 /*
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.
246  *
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.
251  */
252 static int
253 sclp_tty_put_char(struct tty_struct *tty, unsigned char ch)
254 {
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;
259 	}
260 	return 1;
261 }
262 
263 /*
264  * This routine is called by the kernel after it has written a series of
265  * characters to the tty device using put_char().
266  */
267 static void
268 sclp_tty_flush_chars(struct tty_struct *tty)
269 {
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;
273 	}
274 }
275 
276 /*
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).
282  */
283 static int
284 sclp_tty_chars_in_buffer(struct tty_struct *tty)
285 {
286 	unsigned long flags;
287 	struct sclp_buffer *t;
288 	int count;
289 
290 	spin_lock_irqsave(&sclp_tty_lock, flags);
291 	count = 0;
292 	if (sclp_ttybuf != NULL)
293 		count = sclp_chars_in_buffer(sclp_ttybuf);
294 	list_for_each_entry(t, &sclp_tty_outqueue, list) {
295 		count += sclp_chars_in_buffer(t);
296 	}
297 	spin_unlock_irqrestore(&sclp_tty_lock, flags);
298 	return count;
299 }
300 
301 /*
302  * removes all content from buffers of low level driver
303  */
304 static void
305 sclp_tty_flush_buffer(struct tty_struct *tty)
306 {
307 	if (sclp_tty_chars_count > 0) {
308 		sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
309 		sclp_tty_chars_count = 0;
310 	}
311 }
312 
313 /*
314  * push input to tty
315  */
316 static void
317 sclp_tty_input(unsigned char* buf, unsigned int count)
318 {
319 	struct tty_struct *tty = tty_port_tty_get(&sclp_port);
320 	unsigned int cchar;
321 
322 	/*
323 	 * If this tty driver is currently closed
324 	 * then throw the received input away.
325 	 */
326 	if (tty == NULL)
327 		return;
328 	cchar = ctrlchar_handle(buf, count, tty);
329 	switch (cchar & CTRLCHAR_MASK) {
330 	case CTRLCHAR_SYSRQ:
331 		break;
332 	case CTRLCHAR_CTRL:
333 		tty_insert_flip_char(&sclp_port, cchar, TTY_NORMAL);
334 		tty_flip_buffer_push(&sclp_port);
335 		break;
336 	case CTRLCHAR_NONE:
337 		/* send (normal) input to line discipline */
338 		if (count < 2 ||
339 		    (strncmp((const char *) buf + count - 2, "^n", 2) &&
340 		     strncmp((const char *) buf + count - 2, "\252n", 2))) {
341 			/* add the auto \n */
342 			tty_insert_flip_string(&sclp_port, buf, count);
343 			tty_insert_flip_char(&sclp_port, '\n', TTY_NORMAL);
344 		} else
345 			tty_insert_flip_string(&sclp_port, buf, count - 2);
346 		tty_flip_buffer_push(&sclp_port);
347 		break;
348 	}
349 	tty_kref_put(tty);
350 }
351 
352 /*
353  * get a EBCDIC string in upper/lower case,
354  * find out characters in lower/upper case separated by a special character,
355  * modifiy original string,
356  * returns length of resulting string
357  */
358 static int sclp_switch_cases(unsigned char *buf, int count)
359 {
360 	unsigned char *ip, *op;
361 	int toggle;
362 
363 	/* initially changing case is off */
364 	toggle = 0;
365 	ip = op = buf;
366 	while (count-- > 0) {
367 		/* compare with special character */
368 		if (*ip == CASE_DELIMITER) {
369 			/* followed by another special character? */
370 			if (count && ip[1] == CASE_DELIMITER) {
371 				/*
372 				 * ... then put a single copy of the special
373 				 * character to the output string
374 				 */
375 				*op++ = *ip++;
376 				count--;
377 			} else
378 				/*
379 				 * ... special character follower by a normal
380 				 * character toggles the case change behaviour
381 				 */
382 				toggle = ~toggle;
383 			/* skip special character */
384 			ip++;
385 		} else
386 			/* not the special character */
387 			if (toggle)
388 				/* but case switching is on */
389 				if (sclp_tty_tolower)
390 					/* switch to uppercase */
391 					*op++ = _ebc_toupper[(int) *ip++];
392 				else
393 					/* switch to lowercase */
394 					*op++ = _ebc_tolower[(int) *ip++];
395 			else
396 				/* no case switching, copy the character */
397 				*op++ = *ip++;
398 	}
399 	/* return length of reformatted string. */
400 	return op - buf;
401 }
402 
403 static void sclp_get_input(struct gds_subvector *sv)
404 {
405 	unsigned char *str;
406 	int count;
407 
408 	str = (unsigned char *) (sv + 1);
409 	count = sv->length - sizeof(*sv);
410 	if (sclp_tty_tolower)
411 		EBC_TOLOWER(str, count);
412 	count = sclp_switch_cases(str, count);
413 	/* convert EBCDIC to ASCII (modify original input in SCCB) */
414 	sclp_ebcasc_str(str, count);
415 
416 	/* transfer input to high level driver */
417 	sclp_tty_input(str, count);
418 }
419 
420 static inline void sclp_eval_selfdeftextmsg(struct gds_subvector *sv)
421 {
422 	void *end;
423 
424 	end = (void *) sv + sv->length;
425 	for (sv = sv + 1; (void *) sv < end; sv = (void *) sv + sv->length)
426 		if (sv->key == 0x30)
427 			sclp_get_input(sv);
428 }
429 
430 static inline void sclp_eval_textcmd(struct gds_vector *v)
431 {
432 	struct gds_subvector *sv;
433 	void *end;
434 
435 	end = (void *) v + v->length;
436 	for (sv = (struct gds_subvector *) (v + 1);
437 	     (void *) sv < end; sv = (void *) sv + sv->length)
438 		if (sv->key == GDS_KEY_SELFDEFTEXTMSG)
439 			sclp_eval_selfdeftextmsg(sv);
440 
441 }
442 
443 static inline void sclp_eval_cpmsu(struct gds_vector *v)
444 {
445 	void *end;
446 
447 	end = (void *) v + v->length;
448 	for (v = v + 1; (void *) v < end; v = (void *) v + v->length)
449 		if (v->gds_id == GDS_ID_TEXTCMD)
450 			sclp_eval_textcmd(v);
451 }
452 
453 
454 static inline void sclp_eval_mdsmu(struct gds_vector *v)
455 {
456 	v = sclp_find_gds_vector(v + 1, (void *) v + v->length, GDS_ID_CPMSU);
457 	if (v)
458 		sclp_eval_cpmsu(v);
459 }
460 
461 static void sclp_tty_receiver(struct evbuf_header *evbuf)
462 {
463 	struct gds_vector *v;
464 
465 	v = sclp_find_gds_vector(evbuf + 1, (void *) evbuf + evbuf->length,
466 				 GDS_ID_MDSMU);
467 	if (v)
468 		sclp_eval_mdsmu(v);
469 }
470 
471 static void
472 sclp_tty_state_change(struct sclp_register *reg)
473 {
474 }
475 
476 static struct sclp_register sclp_input_event =
477 {
478 	.receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK,
479 	.state_change_fn = sclp_tty_state_change,
480 	.receiver_fn = sclp_tty_receiver
481 };
482 
483 static const struct tty_operations sclp_ops = {
484 	.open = sclp_tty_open,
485 	.close = sclp_tty_close,
486 	.write = sclp_tty_write,
487 	.put_char = sclp_tty_put_char,
488 	.flush_chars = sclp_tty_flush_chars,
489 	.write_room = sclp_tty_write_room,
490 	.chars_in_buffer = sclp_tty_chars_in_buffer,
491 	.flush_buffer = sclp_tty_flush_buffer,
492 };
493 
494 static int __init
495 sclp_tty_init(void)
496 {
497 	struct tty_driver *driver;
498 	void *page;
499 	int i;
500 	int rc;
501 
502 	/* z/VM multiplexes the line mode output on the 32xx screen */
503 	if (MACHINE_IS_VM && !CONSOLE_IS_SCLP)
504 		return 0;
505 	if (!sclp.has_linemode)
506 		return 0;
507 	driver = alloc_tty_driver(1);
508 	if (!driver)
509 		return -ENOMEM;
510 
511 	rc = sclp_rw_init();
512 	if (rc) {
513 		put_tty_driver(driver);
514 		return rc;
515 	}
516 	/* Allocate pages for output buffering */
517 	for (i = 0; i < MAX_KMEM_PAGES; i++) {
518 		page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
519 		if (page == NULL) {
520 			put_tty_driver(driver);
521 			return -ENOMEM;
522 		}
523 		list_add_tail((struct list_head *) page, &sclp_tty_pages);
524 	}
525 	timer_setup(&sclp_tty_timer, sclp_tty_timeout, 0);
526 	sclp_ttybuf = NULL;
527 	sclp_tty_buffer_count = 0;
528 	if (MACHINE_IS_VM) {
529 		/* case input lines to lowercase */
530 		sclp_tty_tolower = 1;
531 	}
532 	sclp_tty_chars_count = 0;
533 
534 	rc = sclp_register(&sclp_input_event);
535 	if (rc) {
536 		put_tty_driver(driver);
537 		return rc;
538 	}
539 
540 	tty_port_init(&sclp_port);
541 
542 	driver->driver_name = "sclp_line";
543 	driver->name = "sclp_line";
544 	driver->major = TTY_MAJOR;
545 	driver->minor_start = 64;
546 	driver->type = TTY_DRIVER_TYPE_SYSTEM;
547 	driver->subtype = SYSTEM_TYPE_TTY;
548 	driver->init_termios = tty_std_termios;
549 	driver->init_termios.c_iflag = IGNBRK | IGNPAR;
550 	driver->init_termios.c_oflag = ONLCR;
551 	driver->init_termios.c_lflag = ISIG | ECHO;
552 	driver->flags = TTY_DRIVER_REAL_RAW;
553 	tty_set_operations(driver, &sclp_ops);
554 	tty_port_link_device(&sclp_port, driver, 0);
555 	rc = tty_register_driver(driver);
556 	if (rc) {
557 		put_tty_driver(driver);
558 		tty_port_destroy(&sclp_port);
559 		return rc;
560 	}
561 	sclp_tty_driver = driver;
562 	return 0;
563 }
564 device_initcall(sclp_tty_init);
565