xref: /linux/drivers/s390/char/sclp_tty.c (revision 43347d56c8d9dd732cee2f8efd384ad21dd1f6c4)
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 spinlock_t sclp_tty_lock;
39 /* List of free pages that can be used for console output buffering. */
40 static struct list_head sclp_tty_pages;
41 /* List of full struct sclp_buffer structures ready for output. */
42 static struct 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 static int sclp_tty_columns = 80;
58 
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 	sclp_port.low_latency = 0;
69 	return 0;
70 }
71 
72 /* This routine is called when the SCLP terminal is closed. */
73 static void
74 sclp_tty_close(struct tty_struct *tty, struct file *filp)
75 {
76 	if (tty->count > 1)
77 		return;
78 	tty_port_tty_set(&sclp_port, NULL);
79 }
80 
81 /*
82  * This routine returns the numbers of characters the tty driver
83  * will accept for queuing to be written.  This number is subject
84  * to change as output buffers get emptied, or if the output flow
85  * control is acted. This is not an exact number because not every
86  * character needs the same space in the sccb. The worst case is
87  * a string of newlines. Every newline creates a new message which
88  * needs 82 bytes.
89  */
90 static int
91 sclp_tty_write_room (struct tty_struct *tty)
92 {
93 	unsigned long flags;
94 	struct list_head *l;
95 	int count;
96 
97 	spin_lock_irqsave(&sclp_tty_lock, flags);
98 	count = 0;
99 	if (sclp_ttybuf != NULL)
100 		count = sclp_buffer_space(sclp_ttybuf) / sizeof(struct msg_buf);
101 	list_for_each(l, &sclp_tty_pages)
102 		count += NR_EMPTY_MSG_PER_SCCB;
103 	spin_unlock_irqrestore(&sclp_tty_lock, flags);
104 	return count;
105 }
106 
107 static void
108 sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc)
109 {
110 	unsigned long flags;
111 	void *page;
112 
113 	do {
114 		page = sclp_unmake_buffer(buffer);
115 		spin_lock_irqsave(&sclp_tty_lock, flags);
116 		/* Remove buffer from outqueue */
117 		list_del(&buffer->list);
118 		sclp_tty_buffer_count--;
119 		list_add_tail((struct list_head *) page, &sclp_tty_pages);
120 		/* Check if there is a pending buffer on the out queue. */
121 		buffer = NULL;
122 		if (!list_empty(&sclp_tty_outqueue))
123 			buffer = list_entry(sclp_tty_outqueue.next,
124 					    struct sclp_buffer, list);
125 		spin_unlock_irqrestore(&sclp_tty_lock, flags);
126 	} while (buffer && sclp_emit_buffer(buffer, sclp_ttybuf_callback));
127 
128 	tty_port_tty_wakeup(&sclp_port);
129 }
130 
131 static inline void
132 __sclp_ttybuf_emit(struct sclp_buffer *buffer)
133 {
134 	unsigned long flags;
135 	int count;
136 	int rc;
137 
138 	spin_lock_irqsave(&sclp_tty_lock, flags);
139 	list_add_tail(&buffer->list, &sclp_tty_outqueue);
140 	count = sclp_tty_buffer_count++;
141 	spin_unlock_irqrestore(&sclp_tty_lock, flags);
142 	if (count)
143 		return;
144 	rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback);
145 	if (rc)
146 		sclp_ttybuf_callback(buffer, rc);
147 }
148 
149 /*
150  * When this routine is called from the timer then we flush the
151  * temporary write buffer.
152  */
153 static void
154 sclp_tty_timeout(unsigned long data)
155 {
156 	unsigned long flags;
157 	struct sclp_buffer *buf;
158 
159 	spin_lock_irqsave(&sclp_tty_lock, flags);
160 	buf = sclp_ttybuf;
161 	sclp_ttybuf = NULL;
162 	spin_unlock_irqrestore(&sclp_tty_lock, flags);
163 
164 	if (buf != NULL) {
165 		__sclp_ttybuf_emit(buf);
166 	}
167 }
168 
169 /*
170  * Write a string to the sclp tty.
171  */
172 static int sclp_tty_write_string(const unsigned char *str, int count, int may_fail)
173 {
174 	unsigned long flags;
175 	void *page;
176 	int written;
177 	int overall_written;
178 	struct sclp_buffer *buf;
179 
180 	if (count <= 0)
181 		return 0;
182 	overall_written = 0;
183 	spin_lock_irqsave(&sclp_tty_lock, flags);
184 	do {
185 		/* Create a sclp output buffer if none exists yet */
186 		if (sclp_ttybuf == NULL) {
187 			while (list_empty(&sclp_tty_pages)) {
188 				spin_unlock_irqrestore(&sclp_tty_lock, flags);
189 				if (may_fail)
190 					goto out;
191 				else
192 					sclp_sync_wait();
193 				spin_lock_irqsave(&sclp_tty_lock, flags);
194 			}
195 			page = sclp_tty_pages.next;
196 			list_del((struct list_head *) page);
197 			sclp_ttybuf = sclp_make_buffer(page, sclp_tty_columns,
198 						       SPACES_PER_TAB);
199 		}
200 		/* try to write the string to the current output buffer */
201 		written = sclp_write(sclp_ttybuf, str, count);
202 		overall_written += written;
203 		if (written == count)
204 			break;
205 		/*
206 		 * Not all characters could be written to the current
207 		 * output buffer. Emit the buffer, create a new buffer
208 		 * and then output the rest of the string.
209 		 */
210 		buf = sclp_ttybuf;
211 		sclp_ttybuf = NULL;
212 		spin_unlock_irqrestore(&sclp_tty_lock, flags);
213 		__sclp_ttybuf_emit(buf);
214 		spin_lock_irqsave(&sclp_tty_lock, flags);
215 		str += written;
216 		count -= written;
217 	} while (count > 0);
218 	/* Setup timer to output current console buffer after 1/10 second */
219 	if (sclp_ttybuf && sclp_chars_in_buffer(sclp_ttybuf) &&
220 	    !timer_pending(&sclp_tty_timer)) {
221 		setup_timer(&sclp_tty_timer, sclp_tty_timeout, 0UL);
222 		mod_timer(&sclp_tty_timer, jiffies + HZ / 10);
223 	}
224 	spin_unlock_irqrestore(&sclp_tty_lock, flags);
225 out:
226 	return overall_written;
227 }
228 
229 /*
230  * This routine is called by the kernel to write a series of characters to the
231  * tty device. The characters may come from user space or kernel space. This
232  * routine will return the number of characters actually accepted for writing.
233  */
234 static int
235 sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
236 {
237 	if (sclp_tty_chars_count > 0) {
238 		sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
239 		sclp_tty_chars_count = 0;
240 	}
241 	return sclp_tty_write_string(buf, count, 1);
242 }
243 
244 /*
245  * This routine is called by the kernel to write a single character to the tty
246  * device. If the kernel uses this routine, it must call the flush_chars()
247  * routine (if defined) when it is done stuffing characters into the driver.
248  *
249  * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver.
250  * If the given character is a '\n' the contents of the SCLP write buffer
251  * - including previous characters from sclp_tty_put_char() and strings from
252  * sclp_write() without final '\n' - will be written.
253  */
254 static int
255 sclp_tty_put_char(struct tty_struct *tty, unsigned char ch)
256 {
257 	sclp_tty_chars[sclp_tty_chars_count++] = ch;
258 	if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) {
259 		sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
260 		sclp_tty_chars_count = 0;
261 	}
262 	return 1;
263 }
264 
265 /*
266  * This routine is called by the kernel after it has written a series of
267  * characters to the tty device using put_char().
268  */
269 static void
270 sclp_tty_flush_chars(struct tty_struct *tty)
271 {
272 	if (sclp_tty_chars_count > 0) {
273 		sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
274 		sclp_tty_chars_count = 0;
275 	}
276 }
277 
278 /*
279  * This routine returns the number of characters in the write buffer of the
280  * SCLP driver. The provided number includes all characters that are stored
281  * in the SCCB (will be written next time the SCLP is not busy) as well as
282  * characters in the write buffer (will not be written as long as there is a
283  * final line feed missing).
284  */
285 static int
286 sclp_tty_chars_in_buffer(struct tty_struct *tty)
287 {
288 	unsigned long flags;
289 	struct list_head *l;
290 	struct sclp_buffer *t;
291 	int count;
292 
293 	spin_lock_irqsave(&sclp_tty_lock, flags);
294 	count = 0;
295 	if (sclp_ttybuf != NULL)
296 		count = sclp_chars_in_buffer(sclp_ttybuf);
297 	list_for_each(l, &sclp_tty_outqueue) {
298 		t = list_entry(l, struct sclp_buffer, list);
299 		count += sclp_chars_in_buffer(t);
300 	}
301 	spin_unlock_irqrestore(&sclp_tty_lock, flags);
302 	return count;
303 }
304 
305 /*
306  * removes all content from buffers of low level driver
307  */
308 static void
309 sclp_tty_flush_buffer(struct tty_struct *tty)
310 {
311 	if (sclp_tty_chars_count > 0) {
312 		sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
313 		sclp_tty_chars_count = 0;
314 	}
315 }
316 
317 /*
318  * push input to tty
319  */
320 static void
321 sclp_tty_input(unsigned char* buf, unsigned int count)
322 {
323 	struct tty_struct *tty = tty_port_tty_get(&sclp_port);
324 	unsigned int cchar;
325 
326 	/*
327 	 * If this tty driver is currently closed
328 	 * then throw the received input away.
329 	 */
330 	if (tty == NULL)
331 		return;
332 	cchar = ctrlchar_handle(buf, count, tty);
333 	switch (cchar & CTRLCHAR_MASK) {
334 	case CTRLCHAR_SYSRQ:
335 		break;
336 	case CTRLCHAR_CTRL:
337 		tty_insert_flip_char(&sclp_port, cchar, TTY_NORMAL);
338 		tty_flip_buffer_push(&sclp_port);
339 		break;
340 	case CTRLCHAR_NONE:
341 		/* send (normal) input to line discipline */
342 		if (count < 2 ||
343 		    (strncmp((const char *) buf + count - 2, "^n", 2) &&
344 		     strncmp((const char *) buf + count - 2, "\252n", 2))) {
345 			/* add the auto \n */
346 			tty_insert_flip_string(&sclp_port, buf, count);
347 			tty_insert_flip_char(&sclp_port, '\n', TTY_NORMAL);
348 		} else
349 			tty_insert_flip_string(&sclp_port, buf, count - 2);
350 		tty_flip_buffer_push(&sclp_port);
351 		break;
352 	}
353 	tty_kref_put(tty);
354 }
355 
356 /*
357  * get a EBCDIC string in upper/lower case,
358  * find out characters in lower/upper case separated by a special character,
359  * modifiy original string,
360  * returns length of resulting string
361  */
362 static int sclp_switch_cases(unsigned char *buf, int count)
363 {
364 	unsigned char *ip, *op;
365 	int toggle;
366 
367 	/* initially changing case is off */
368 	toggle = 0;
369 	ip = op = buf;
370 	while (count-- > 0) {
371 		/* compare with special character */
372 		if (*ip == CASE_DELIMITER) {
373 			/* followed by another special character? */
374 			if (count && ip[1] == CASE_DELIMITER) {
375 				/*
376 				 * ... then put a single copy of the special
377 				 * character to the output string
378 				 */
379 				*op++ = *ip++;
380 				count--;
381 			} else
382 				/*
383 				 * ... special character follower by a normal
384 				 * character toggles the case change behaviour
385 				 */
386 				toggle = ~toggle;
387 			/* skip special character */
388 			ip++;
389 		} else
390 			/* not the special character */
391 			if (toggle)
392 				/* but case switching is on */
393 				if (sclp_tty_tolower)
394 					/* switch to uppercase */
395 					*op++ = _ebc_toupper[(int) *ip++];
396 				else
397 					/* switch to lowercase */
398 					*op++ = _ebc_tolower[(int) *ip++];
399 			else
400 				/* no case switching, copy the character */
401 				*op++ = *ip++;
402 	}
403 	/* return length of reformatted string. */
404 	return op - buf;
405 }
406 
407 static void sclp_get_input(struct gds_subvector *sv)
408 {
409 	unsigned char *str;
410 	int count;
411 
412 	str = (unsigned char *) (sv + 1);
413 	count = sv->length - sizeof(*sv);
414 	if (sclp_tty_tolower)
415 		EBC_TOLOWER(str, count);
416 	count = sclp_switch_cases(str, count);
417 	/* convert EBCDIC to ASCII (modify original input in SCCB) */
418 	sclp_ebcasc_str(str, count);
419 
420 	/* transfer input to high level driver */
421 	sclp_tty_input(str, count);
422 }
423 
424 static inline void sclp_eval_selfdeftextmsg(struct gds_subvector *sv)
425 {
426 	void *end;
427 
428 	end = (void *) sv + sv->length;
429 	for (sv = sv + 1; (void *) sv < end; sv = (void *) sv + sv->length)
430 		if (sv->key == 0x30)
431 			sclp_get_input(sv);
432 }
433 
434 static inline void sclp_eval_textcmd(struct gds_vector *v)
435 {
436 	struct gds_subvector *sv;
437 	void *end;
438 
439 	end = (void *) v + v->length;
440 	for (sv = (struct gds_subvector *) (v + 1);
441 	     (void *) sv < end; sv = (void *) sv + sv->length)
442 		if (sv->key == GDS_KEY_SELFDEFTEXTMSG)
443 			sclp_eval_selfdeftextmsg(sv);
444 
445 }
446 
447 static inline void sclp_eval_cpmsu(struct gds_vector *v)
448 {
449 	void *end;
450 
451 	end = (void *) v + v->length;
452 	for (v = v + 1; (void *) v < end; v = (void *) v + v->length)
453 		if (v->gds_id == GDS_ID_TEXTCMD)
454 			sclp_eval_textcmd(v);
455 }
456 
457 
458 static inline void sclp_eval_mdsmu(struct gds_vector *v)
459 {
460 	v = sclp_find_gds_vector(v + 1, (void *) v + v->length, GDS_ID_CPMSU);
461 	if (v)
462 		sclp_eval_cpmsu(v);
463 }
464 
465 static void sclp_tty_receiver(struct evbuf_header *evbuf)
466 {
467 	struct gds_vector *v;
468 
469 	v = sclp_find_gds_vector(evbuf + 1, (void *) evbuf + evbuf->length,
470 				 GDS_ID_MDSMU);
471 	if (v)
472 		sclp_eval_mdsmu(v);
473 }
474 
475 static void
476 sclp_tty_state_change(struct sclp_register *reg)
477 {
478 }
479 
480 static struct sclp_register sclp_input_event =
481 {
482 	.receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK,
483 	.state_change_fn = sclp_tty_state_change,
484 	.receiver_fn = sclp_tty_receiver
485 };
486 
487 static const struct tty_operations sclp_ops = {
488 	.open = sclp_tty_open,
489 	.close = sclp_tty_close,
490 	.write = sclp_tty_write,
491 	.put_char = sclp_tty_put_char,
492 	.flush_chars = sclp_tty_flush_chars,
493 	.write_room = sclp_tty_write_room,
494 	.chars_in_buffer = sclp_tty_chars_in_buffer,
495 	.flush_buffer = sclp_tty_flush_buffer,
496 };
497 
498 static int __init
499 sclp_tty_init(void)
500 {
501 	struct tty_driver *driver;
502 	void *page;
503 	int i;
504 	int rc;
505 
506 	if (!CONSOLE_IS_SCLP)
507 		return 0;
508 	driver = alloc_tty_driver(1);
509 	if (!driver)
510 		return -ENOMEM;
511 
512 	rc = sclp_rw_init();
513 	if (rc) {
514 		put_tty_driver(driver);
515 		return rc;
516 	}
517 	/* Allocate pages for output buffering */
518 	INIT_LIST_HEAD(&sclp_tty_pages);
519 	for (i = 0; i < MAX_KMEM_PAGES; i++) {
520 		page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
521 		if (page == NULL) {
522 			put_tty_driver(driver);
523 			return -ENOMEM;
524 		}
525 		list_add_tail((struct list_head *) page, &sclp_tty_pages);
526 	}
527 	INIT_LIST_HEAD(&sclp_tty_outqueue);
528 	spin_lock_init(&sclp_tty_lock);
529 	init_timer(&sclp_tty_timer);
530 	sclp_ttybuf = NULL;
531 	sclp_tty_buffer_count = 0;
532 	if (MACHINE_IS_VM) {
533 		/*
534 		 * save 4 characters for the CPU number
535 		 * written at start of each line by VM/CP
536 		 */
537 		sclp_tty_columns = 76;
538 		/* case input lines to lowercase */
539 		sclp_tty_tolower = 1;
540 	}
541 	sclp_tty_chars_count = 0;
542 
543 	rc = sclp_register(&sclp_input_event);
544 	if (rc) {
545 		put_tty_driver(driver);
546 		return rc;
547 	}
548 
549 	tty_port_init(&sclp_port);
550 
551 	driver->driver_name = "sclp_line";
552 	driver->name = "sclp_line";
553 	driver->major = TTY_MAJOR;
554 	driver->minor_start = 64;
555 	driver->type = TTY_DRIVER_TYPE_SYSTEM;
556 	driver->subtype = SYSTEM_TYPE_TTY;
557 	driver->init_termios = tty_std_termios;
558 	driver->init_termios.c_iflag = IGNBRK | IGNPAR;
559 	driver->init_termios.c_oflag = ONLCR;
560 	driver->init_termios.c_lflag = ISIG | ECHO;
561 	driver->flags = TTY_DRIVER_REAL_RAW;
562 	tty_set_operations(driver, &sclp_ops);
563 	tty_port_link_device(&sclp_port, driver, 0);
564 	rc = tty_register_driver(driver);
565 	if (rc) {
566 		put_tty_driver(driver);
567 		tty_port_destroy(&sclp_port);
568 		return rc;
569 	}
570 	sclp_tty_driver = driver;
571 	return 0;
572 }
573 device_initcall(sclp_tty_init);
574