Linux Audio

Check our new training course

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