1/*
2 * n_tty.c --- implements the N_TTY line discipline.
3 *
4 * This code used to be in tty_io.c, but things are getting hairy
5 * enough that it made sense to split things off.  (The N_TTY
6 * processing has changed so much that it's hardly recognizable,
7 * anyway...)
8 *
9 * Note that the open routine for N_TTY is guaranteed never to return
10 * an error.  This is because Linux will fall back to setting a line
11 * to N_TTY if it can not switch to any other line discipline.
12 *
13 * Written by Theodore Ts'o, Copyright 1994.
14 *
15 * This file also contains code originally written by Linus Torvalds,
16 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
17 *
18 * This file may be redistributed under the terms of the GNU General Public
19 * License.
20 *
21 * Reduced memory usage for older ARM systems  - Russell King.
22 *
23 * 2000/01/20   Fixed SMP locking on put_tty_queue using bits of
24 *		the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
25 *		who actually finally proved there really was a race.
26 *
27 * 2002/03/18   Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
28 *		waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
29 *		Also fixed a bug in BLOCKING mode where write_chan returns
30 *		EAGAIN
31 */
32
33#include <linux/types.h>
34#include <linux/major.h>
35#include <linux/errno.h>
36#include <linux/signal.h>
37#include <linux/fcntl.h>
38#include <linux/sched.h>
39#include <linux/interrupt.h>
40#include <linux/tty.h>
41#include <linux/timer.h>
42#include <linux/ctype.h>
43#include <linux/kd.h>
44#include <linux/mm.h>
45#include <linux/string.h>
46#include <linux/slab.h>
47#include <linux/poll.h>
48
49#include <asm/uaccess.h>
50#include <asm/system.h>
51#include <asm/bitops.h>
52
53#define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
54#define SYSCONS_DEV  MKDEV(TTYAUX_MAJOR,1)
55
56#ifndef MIN
57#define MIN(a,b)	((a) < (b) ? (a) : (b))
58#endif
59
60/* number of characters left in xmit buffer before select has we have room */
61#define WAKEUP_CHARS 256
62
63/*
64 * This defines the low- and high-watermarks for throttling and
65 * unthrottling the TTY driver.  These watermarks are used for
66 * controlling the space in the read buffer.
67 */
68#define TTY_THRESHOLD_THROTTLE		128 /* now based on remaining room */
69#define TTY_THRESHOLD_UNTHROTTLE 	128
70
71static inline unsigned char *alloc_buf(void)
72{
73	unsigned char *p;
74	int prio = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
75
76	if (PAGE_SIZE != N_TTY_BUF_SIZE) {
77		p = kmalloc(N_TTY_BUF_SIZE, prio);
78		if (p)
79			memset(p, 0, N_TTY_BUF_SIZE);
80	} else
81		p = (unsigned char *)get_zeroed_page(prio);
82
83	return p;
84}
85
86static inline void free_buf(unsigned char *buf)
87{
88	if (PAGE_SIZE != N_TTY_BUF_SIZE)
89		kfree(buf);
90	else
91		free_page((unsigned long) buf);
92}
93
94static inline void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
95{
96	if (tty->read_cnt < N_TTY_BUF_SIZE) {
97		tty->read_buf[tty->read_head] = c;
98		tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
99		tty->read_cnt++;
100	}
101}
102
103static inline void put_tty_queue(unsigned char c, struct tty_struct *tty)
104{
105	unsigned long flags;
106	/*
107	 *	The problem of stomping on the buffers ends here.
108	 *	Why didn't anyone see this one coming? --AJK
109	*/
110	spin_lock_irqsave(&tty->read_lock, flags);
111	put_tty_queue_nolock(c, tty);
112	spin_unlock_irqrestore(&tty->read_lock, flags);
113}
114
115/*
116 * Check whether to call the driver.unthrottle function.
117 * We test the TTY_THROTTLED bit first so that it always
118 * indicates the current state.
119 */
120static void check_unthrottle(struct tty_struct * tty)
121{
122	if (tty->count &&
123	    test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
124	    tty->driver.unthrottle)
125		tty->driver.unthrottle(tty);
126}
127
128/*
129 * Reset the read buffer counters, clear the flags,
130 * and make sure the driver is unthrottled. Called
131 * from n_tty_open() and n_tty_flush_buffer().
132 */
133static void reset_buffer_flags(struct tty_struct *tty)
134{
135	unsigned long flags;
136
137	spin_lock_irqsave(&tty->read_lock, flags);
138	tty->read_head = tty->read_tail = tty->read_cnt = 0;
139	spin_unlock_irqrestore(&tty->read_lock, flags);
140	tty->canon_head = tty->canon_data = tty->erasing = 0;
141	memset(&tty->read_flags, 0, sizeof tty->read_flags);
142	check_unthrottle(tty);
143}
144
145/*
146 * Flush the input buffer
147 */
148void n_tty_flush_buffer(struct tty_struct * tty)
149{
150	/* clear everything and unthrottle the driver */
151	reset_buffer_flags(tty);
152
153	if (!tty->link)
154		return;
155
156	if (tty->link->packet) {
157		tty->ctrl_status |= TIOCPKT_FLUSHREAD;
158		wake_up_interruptible(&tty->link->read_wait);
159	}
160}
161
162/*
163 * Return number of characters buffered to be delivered to user
164 */
165ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
166{
167	unsigned long flags;
168	ssize_t n = 0;
169
170	spin_lock_irqsave(&tty->read_lock, flags);
171	if (!tty->icanon) {
172		n = tty->read_cnt;
173	} else if (tty->canon_data) {
174		n = (tty->canon_head > tty->read_tail) ?
175			tty->canon_head - tty->read_tail :
176			tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
177	}
178	spin_unlock_irqrestore(&tty->read_lock, flags);
179	return n;
180}
181
182/*
183 * Perform OPOST processing.  Returns -1 when the output device is
184 * full and the character must be retried.
185 */
186static int opost(unsigned char c, struct tty_struct *tty)
187{
188	int	space, spaces;
189
190	space = tty->driver.write_room(tty);
191	if (!space)
192		return -1;
193
194	if (O_OPOST(tty)) {
195		switch (c) {
196		case '\n':
197			if (O_ONLRET(tty))
198				tty->column = 0;
199			if (O_ONLCR(tty)) {
200				if (space < 2)
201					return -1;
202				tty->driver.put_char(tty, '\r');
203				tty->column = 0;
204			}
205			tty->canon_column = tty->column;
206			break;
207		case '\r':
208			if (O_ONOCR(tty) && tty->column == 0)
209				return 0;
210			if (O_OCRNL(tty)) {
211				c = '\n';
212				if (O_ONLRET(tty))
213					tty->canon_column = tty->column = 0;
214				break;
215			}
216			tty->canon_column = tty->column = 0;
217			break;
218		case '\t':
219			spaces = 8 - (tty->column & 7);
220			if (O_TABDLY(tty) == XTABS) {
221				if (space < spaces)
222					return -1;
223				tty->column += spaces;
224				tty->driver.write(tty, 0, "        ", spaces);
225				return 0;
226			}
227			tty->column += spaces;
228			break;
229		case '\b':
230			if (tty->column > 0)
231				tty->column--;
232			break;
233		default:
234			if (O_OLCUC(tty))
235				c = toupper(c);
236			if (!iscntrl(c))
237				tty->column++;
238			break;
239		}
240	}
241	tty->driver.put_char(tty, c);
242	return 0;
243}
244
245/*
246 * opost_block --- to speed up block console writes, among other
247 * things.
248 */
249static ssize_t opost_block(struct tty_struct * tty,
250		       const unsigned char * inbuf, unsigned int nr)
251{
252	char	buf[80];
253	int	space;
254	int 	i;
255	char	*cp;
256
257	space = tty->driver.write_room(tty);
258	if (!space)
259		return 0;
260	if (nr > space)
261		nr = space;
262	if (nr > sizeof(buf))
263	    nr = sizeof(buf);
264
265	if (copy_from_user(buf, inbuf, nr))
266		return -EFAULT;
267
268	for (i = 0, cp = buf; i < nr; i++, cp++) {
269		switch (*cp) {
270		case '\n':
271			if (O_ONLRET(tty))
272				tty->column = 0;
273			if (O_ONLCR(tty))
274				goto break_out;
275			tty->canon_column = tty->column;
276			break;
277		case '\r':
278			if (O_ONOCR(tty) && tty->column == 0)
279				goto break_out;
280			if (O_OCRNL(tty)) {
281				*cp = '\n';
282				if (O_ONLRET(tty))
283					tty->canon_column = tty->column = 0;
284				break;
285			}
286			tty->canon_column = tty->column = 0;
287			break;
288		case '\t':
289			goto break_out;
290		case '\b':
291			if (tty->column > 0)
292				tty->column--;
293			break;
294		default:
295			if (O_OLCUC(tty))
296				*cp = toupper(*cp);
297			if (!iscntrl(*cp))
298				tty->column++;
299			break;
300		}
301	}
302break_out:
303	if (tty->driver.flush_chars)
304		tty->driver.flush_chars(tty);
305	i = tty->driver.write(tty, 0, buf, i);
306	return i;
307}
308
309
310
311static inline void put_char(unsigned char c, struct tty_struct *tty)
312{
313	tty->driver.put_char(tty, c);
314}
315
316/* Must be called only when L_ECHO(tty) is true. */
317
318static void echo_char(unsigned char c, struct tty_struct *tty)
319{
320	if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
321		put_char('^', tty);
322		put_char(c ^ 0100, tty);
323		tty->column += 2;
324	} else
325		opost(c, tty);
326}
327
328static inline void finish_erasing(struct tty_struct *tty)
329{
330	if (tty->erasing) {
331		put_char('/', tty);
332		tty->column += 2;
333		tty->erasing = 0;
334	}
335}
336
337static void eraser(unsigned char c, struct tty_struct *tty)
338{
339	enum { ERASE, WERASE, KILL } kill_type;
340	int head, seen_alnums;
341	unsigned long flags;
342
343	if (tty->read_head == tty->canon_head) {
344		/* opost('\a', tty); */		/* what do you think? */
345		return;
346	}
347	if (c == ERASE_CHAR(tty))
348		kill_type = ERASE;
349	else if (c == WERASE_CHAR(tty))
350		kill_type = WERASE;
351	else {
352		if (!L_ECHO(tty)) {
353			spin_lock_irqsave(&tty->read_lock, flags);
354			tty->read_cnt -= ((tty->read_head - tty->canon_head) &
355					  (N_TTY_BUF_SIZE - 1));
356			tty->read_head = tty->canon_head;
357			spin_unlock_irqrestore(&tty->read_lock, flags);
358			return;
359		}
360		if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
361			spin_lock_irqsave(&tty->read_lock, flags);
362			tty->read_cnt -= ((tty->read_head - tty->canon_head) &
363					  (N_TTY_BUF_SIZE - 1));
364			tty->read_head = tty->canon_head;
365			spin_unlock_irqrestore(&tty->read_lock, flags);
366			finish_erasing(tty);
367			echo_char(KILL_CHAR(tty), tty);
368			/* Add a newline if ECHOK is on and ECHOKE is off. */
369			if (L_ECHOK(tty))
370				opost('\n', tty);
371			return;
372		}
373		kill_type = KILL;
374	}
375
376	seen_alnums = 0;
377	while (tty->read_head != tty->canon_head) {
378		head = (tty->read_head - 1) & (N_TTY_BUF_SIZE-1);
379		c = tty->read_buf[head];
380		if (kill_type == WERASE) {
381			/* Equivalent to BSD's ALTWERASE. */
382			if (isalnum(c) || c == '_')
383				seen_alnums++;
384			else if (seen_alnums)
385				break;
386		}
387		spin_lock_irqsave(&tty->read_lock, flags);
388		tty->read_head = head;
389		tty->read_cnt--;
390		spin_unlock_irqrestore(&tty->read_lock, flags);
391		if (L_ECHO(tty)) {
392			if (L_ECHOPRT(tty)) {
393				if (!tty->erasing) {
394					put_char('\\', tty);
395					tty->column++;
396					tty->erasing = 1;
397				}
398				echo_char(c, tty);
399			} else if (kill_type == ERASE && !L_ECHOE(tty)) {
400				echo_char(ERASE_CHAR(tty), tty);
401			} else if (c == '\t') {
402				unsigned int col = tty->canon_column;
403				unsigned long tail = tty->canon_head;
404
405				/* Find the column of the last char. */
406				while (tail != tty->read_head) {
407					c = tty->read_buf[tail];
408					if (c == '\t')
409						col = (col | 7) + 1;
410					else if (iscntrl(c)) {
411						if (L_ECHOCTL(tty))
412							col += 2;
413					} else
414						col++;
415					tail = (tail+1) & (N_TTY_BUF_SIZE-1);
416				}
417
418				/* should never happen */
419				if (tty->column > 0x80000000)
420					tty->column = 0;
421
422				/* Now backup to that column. */
423				while (tty->column > col) {
424					/* Can't use opost here. */
425					put_char('\b', tty);
426					if (tty->column > 0)
427						tty->column--;
428				}
429			} else {
430				if (iscntrl(c) && L_ECHOCTL(tty)) {
431					put_char('\b', tty);
432					put_char(' ', tty);
433					put_char('\b', tty);
434					if (tty->column > 0)
435						tty->column--;
436				}
437				if (!iscntrl(c) || L_ECHOCTL(tty)) {
438					put_char('\b', tty);
439					put_char(' ', tty);
440					put_char('\b', tty);
441					if (tty->column > 0)
442						tty->column--;
443				}
444			}
445		}
446		if (kill_type == ERASE)
447			break;
448	}
449	if (tty->read_head == tty->canon_head)
450		finish_erasing(tty);
451}
452
453static inline void isig(int sig, struct tty_struct *tty, int flush)
454{
455	if (tty->pgrp > 0)
456		kill_pg(tty->pgrp, sig, 1);
457	if (flush || !L_NOFLSH(tty)) {
458		n_tty_flush_buffer(tty);
459		if (tty->driver.flush_buffer)
460			tty->driver.flush_buffer(tty);
461	}
462}
463
464static inline void n_tty_receive_break(struct tty_struct *tty)
465{
466	if (I_IGNBRK(tty))
467		return;
468	if (I_BRKINT(tty)) {
469		isig(SIGINT, tty, 1);
470		return;
471	}
472	if (I_PARMRK(tty)) {
473		put_tty_queue('\377', tty);
474		put_tty_queue('\0', tty);
475	}
476	put_tty_queue('\0', tty);
477	wake_up_interruptible(&tty->read_wait);
478}
479
480static inline void n_tty_receive_overrun(struct tty_struct *tty)
481{
482	char buf[64];
483
484	tty->num_overrun++;
485	if (time_before(tty->overrun_time, jiffies - HZ)) {
486		printk("%s: %d input overrun(s)\n", tty_name(tty, buf),
487		       tty->num_overrun);
488		tty->overrun_time = jiffies;
489		tty->num_overrun = 0;
490	}
491}
492
493static inline void n_tty_receive_parity_error(struct tty_struct *tty,
494					      unsigned char c)
495{
496	if (I_IGNPAR(tty)) {
497		return;
498	}
499	if (I_PARMRK(tty)) {
500		put_tty_queue('\377', tty);
501		put_tty_queue('\0', tty);
502		put_tty_queue(c, tty);
503	} else	if (I_INPCK(tty))
504		put_tty_queue('\0', tty);
505	else
506		put_tty_queue(c, tty);
507	wake_up_interruptible(&tty->read_wait);
508}
509
510static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
511{
512	unsigned long flags;
513
514	if (tty->raw) {
515		put_tty_queue(c, tty);
516		return;
517	}
518
519	if (tty->stopped && !tty->flow_stopped &&
520	    I_IXON(tty) && I_IXANY(tty)) {
521		start_tty(tty);
522		return;
523	}
524
525	if (I_ISTRIP(tty))
526		c &= 0x7f;
527	if (I_IUCLC(tty) && L_IEXTEN(tty))
528		c=tolower(c);
529
530	if (tty->closing) {
531		if (I_IXON(tty)) {
532			if (c == START_CHAR(tty))
533				start_tty(tty);
534			else if (c == STOP_CHAR(tty))
535				stop_tty(tty);
536		}
537		return;
538	}
539
540	/*
541	 * If the previous character was LNEXT, or we know that this
542	 * character is not one of the characters that we'll have to
543	 * handle specially, do shortcut processing to speed things
544	 * up.
545	 */
546	if (!test_bit(c, &tty->process_char_map) || tty->lnext) {
547		finish_erasing(tty);
548		tty->lnext = 0;
549		if (L_ECHO(tty)) {
550			if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
551				put_char('\a', tty); /* beep if no space */
552				return;
553			}
554			/* Record the column of first canon char. */
555			if (tty->canon_head == tty->read_head)
556				tty->canon_column = tty->column;
557			echo_char(c, tty);
558		}
559		if (I_PARMRK(tty) && c == (unsigned char) '\377')
560			put_tty_queue(c, tty);
561		put_tty_queue(c, tty);
562		return;
563	}
564
565	if (c == '\r') {
566		if (I_IGNCR(tty))
567			return;
568		if (I_ICRNL(tty))
569			c = '\n';
570	} else if (c == '\n' && I_INLCR(tty))
571		c = '\r';
572	if (I_IXON(tty)) {
573		if (c == START_CHAR(tty)) {
574			start_tty(tty);
575			return;
576		}
577		if (c == STOP_CHAR(tty)) {
578			stop_tty(tty);
579			return;
580		}
581	}
582	if (L_ISIG(tty)) {
583		int signal;
584		signal = SIGINT;
585		if (c == INTR_CHAR(tty))
586			goto send_signal;
587		signal = SIGQUIT;
588		if (c == QUIT_CHAR(tty))
589			goto send_signal;
590		signal = SIGTSTP;
591		if (c == SUSP_CHAR(tty)) {
592send_signal:
593			isig(signal, tty, 0);
594			return;
595		}
596	}
597	if (tty->icanon) {
598		if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
599		    (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
600			eraser(c, tty);
601			return;
602		}
603		if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
604			tty->lnext = 1;
605			if (L_ECHO(tty)) {
606				finish_erasing(tty);
607				if (L_ECHOCTL(tty)) {
608					put_char('^', tty);
609					put_char('\b', tty);
610				}
611			}
612			return;
613		}
614		if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
615		    L_IEXTEN(tty)) {
616			unsigned long tail = tty->canon_head;
617
618			finish_erasing(tty);
619			echo_char(c, tty);
620			opost('\n', tty);
621			while (tail != tty->read_head) {
622				echo_char(tty->read_buf[tail], tty);
623				tail = (tail+1) & (N_TTY_BUF_SIZE-1);
624			}
625			return;
626		}
627		if (c == '\n') {
628			if (L_ECHO(tty) || L_ECHONL(tty)) {
629				if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
630					put_char('\a', tty);
631					return;
632				}
633				opost('\n', tty);
634			}
635			goto handle_newline;
636		}
637		if (c == EOF_CHAR(tty)) {
638		        if (tty->canon_head != tty->read_head)
639			        set_bit(TTY_PUSH, &tty->flags);
640			c = __DISABLED_CHAR;
641			goto handle_newline;
642		}
643		if ((c == EOL_CHAR(tty)) ||
644		    (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
645			if (L_ECHO(tty)) {
646				if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
647					put_char('\a', tty);
648					return;
649				}
650				/* Record the column of first canon char. */
651				if (tty->canon_head == tty->read_head)
652					tty->canon_column = tty->column;
653				echo_char(c, tty);
654			}
655			if (I_PARMRK(tty) && c == (unsigned char) '\377')
656				put_tty_queue(c, tty);
657
658		handle_newline:
659			spin_lock_irqsave(&tty->read_lock, flags);
660			set_bit(tty->read_head, &tty->read_flags);
661			put_tty_queue_nolock(c, tty);
662			tty->canon_head = tty->read_head;
663			tty->canon_data++;
664			spin_unlock_irqrestore(&tty->read_lock, flags);
665			kill_fasync(&tty->fasync, SIGIO, POLL_IN);
666			if (waitqueue_active(&tty->read_wait))
667				wake_up_interruptible(&tty->read_wait);
668			return;
669		}
670	}
671
672	finish_erasing(tty);
673	if (L_ECHO(tty)) {
674		if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
675			put_char('\a', tty); /* beep if no space */
676			return;
677		}
678		if (c == '\n')
679			opost('\n', tty);
680		else {
681			/* Record the column of first canon char. */
682			if (tty->canon_head == tty->read_head)
683				tty->canon_column = tty->column;
684			echo_char(c, tty);
685		}
686	}
687
688	if (I_PARMRK(tty) && c == (unsigned char) '\377')
689		put_tty_queue(c, tty);
690
691	put_tty_queue(c, tty);
692}
693
694static int n_tty_receive_room(struct tty_struct *tty)
695{
696	int	left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
697
698	/*
699	 * If we are doing input canonicalization, and there are no
700	 * pending newlines, let characters through without limit, so
701	 * that erase characters will be handled.  Other excess
702	 * characters will be beeped.
703	 */
704	if (tty->icanon && !tty->canon_data)
705		return N_TTY_BUF_SIZE;
706
707	if (left > 0)
708		return left;
709	return 0;
710}
711
712/*
713 * Required for the ptys, serial driver etc. since processes
714 * that attach themselves to the master and rely on ASYNC
715 * IO must be woken up
716 */
717
718static void n_tty_write_wakeup(struct tty_struct *tty)
719{
720	if (tty->fasync)
721	{
722 		set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
723		kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
724	}
725	return;
726}
727
728static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
729			      char *fp, int count)
730{
731	const unsigned char *p;
732	char *f, flags = TTY_NORMAL;
733	int	i;
734	char	buf[64];
735	unsigned long cpuflags;
736
737	if (!tty->read_buf)
738		return;
739
740	if (tty->real_raw) {
741		spin_lock_irqsave(&tty->read_lock, cpuflags);
742		i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
743				   N_TTY_BUF_SIZE - tty->read_head));
744		memcpy(tty->read_buf + tty->read_head, cp, i);
745		tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
746		tty->read_cnt += i;
747		cp += i;
748		count -= i;
749
750		i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
751			       N_TTY_BUF_SIZE - tty->read_head));
752		memcpy(tty->read_buf + tty->read_head, cp, i);
753		tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
754		tty->read_cnt += i;
755		spin_unlock_irqrestore(&tty->read_lock, cpuflags);
756	} else {
757		for (i=count, p = cp, f = fp; i; i--, p++) {
758			if (f)
759				flags = *f++;
760			switch (flags) {
761			case TTY_NORMAL:
762				n_tty_receive_char(tty, *p);
763				break;
764			case TTY_BREAK:
765				n_tty_receive_break(tty);
766				break;
767			case TTY_PARITY:
768			case TTY_FRAME:
769				n_tty_receive_parity_error(tty, *p);
770				break;
771			case TTY_OVERRUN:
772				n_tty_receive_overrun(tty);
773				break;
774			default:
775				printk("%s: unknown flag %d\n",
776				       tty_name(tty, buf), flags);
777				break;
778			}
779		}
780		if (tty->driver.flush_chars)
781			tty->driver.flush_chars(tty);
782	}
783
784	if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
785		kill_fasync(&tty->fasync, SIGIO, POLL_IN);
786		if (waitqueue_active(&tty->read_wait))
787			wake_up_interruptible(&tty->read_wait);
788	}
789
790	/*
791	 * Check the remaining room for the input canonicalization
792	 * mode.  We don't want to throttle the driver if we're in
793	 * canonical mode and don't have a newline yet!
794	 */
795	if (n_tty_receive_room(tty) < TTY_THRESHOLD_THROTTLE) {
796		/* check TTY_THROTTLED first so it indicates our state */
797		if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
798		    tty->driver.throttle)
799			tty->driver.throttle(tty);
800	}
801}
802
803int is_ignored(int sig)
804{
805	return (sigismember(&current->blocked, sig) ||
806	        current->sig->action[sig-1].sa.sa_handler == SIG_IGN);
807}
808
809static void n_tty_set_termios(struct tty_struct *tty, struct termios * old)
810{
811	if (!tty)
812		return;
813
814	tty->icanon = (L_ICANON(tty) != 0);
815	if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
816		tty->raw = 1;
817		tty->real_raw = 1;
818		return;
819	}
820	if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
821	    I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
822	    I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
823	    I_PARMRK(tty)) {
824		cli();
825		memset(tty->process_char_map, 0, 256/8);
826
827		if (I_IGNCR(tty) || I_ICRNL(tty))
828			set_bit('\r', &tty->process_char_map);
829		if (I_INLCR(tty))
830			set_bit('\n', &tty->process_char_map);
831
832		if (L_ICANON(tty)) {
833			set_bit(ERASE_CHAR(tty), &tty->process_char_map);
834			set_bit(KILL_CHAR(tty), &tty->process_char_map);
835			set_bit(EOF_CHAR(tty), &tty->process_char_map);
836			set_bit('\n', &tty->process_char_map);
837			set_bit(EOL_CHAR(tty), &tty->process_char_map);
838			if (L_IEXTEN(tty)) {
839				set_bit(WERASE_CHAR(tty),
840					&tty->process_char_map);
841				set_bit(LNEXT_CHAR(tty),
842					&tty->process_char_map);
843				set_bit(EOL2_CHAR(tty),
844					&tty->process_char_map);
845				if (L_ECHO(tty))
846					set_bit(REPRINT_CHAR(tty),
847						&tty->process_char_map);
848			}
849		}
850		if (I_IXON(tty)) {
851			set_bit(START_CHAR(tty), &tty->process_char_map);
852			set_bit(STOP_CHAR(tty), &tty->process_char_map);
853		}
854		if (L_ISIG(tty)) {
855			set_bit(INTR_CHAR(tty), &tty->process_char_map);
856			set_bit(QUIT_CHAR(tty), &tty->process_char_map);
857			set_bit(SUSP_CHAR(tty), &tty->process_char_map);
858		}
859		clear_bit(__DISABLED_CHAR, &tty->process_char_map);
860		sti();
861		tty->raw = 0;
862		tty->real_raw = 0;
863	} else {
864		tty->raw = 1;
865		if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
866		    (I_IGNPAR(tty) || !I_INPCK(tty)) &&
867		    (tty->driver.flags & TTY_DRIVER_REAL_RAW))
868			tty->real_raw = 1;
869		else
870			tty->real_raw = 0;
871	}
872}
873
874static void n_tty_close(struct tty_struct *tty)
875{
876	n_tty_flush_buffer(tty);
877	if (tty->read_buf) {
878		free_buf(tty->read_buf);
879		tty->read_buf = 0;
880	}
881}
882
883static int n_tty_open(struct tty_struct *tty)
884{
885	if (!tty)
886		return -EINVAL;
887
888	if (!tty->read_buf) {
889		tty->read_buf = alloc_buf();
890		if (!tty->read_buf)
891			return -ENOMEM;
892	}
893	memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
894	reset_buffer_flags(tty);
895	tty->column = 0;
896	n_tty_set_termios(tty, 0);
897	tty->minimum_to_wake = 1;
898	tty->closing = 0;
899	return 0;
900}
901
902static inline int input_available_p(struct tty_struct *tty, int amt)
903{
904	if (tty->icanon) {
905		if (tty->canon_data)
906			return 1;
907	} else if (tty->read_cnt >= (amt ? amt : 1))
908		return 1;
909
910	return 0;
911}
912
913/*
914 * Helper function to speed up read_chan.  It is only called when
915 * ICANON is off; it copies characters straight from the tty queue to
916 * user space directly.  It can be profitably called twice; once to
917 * drain the space from the tail pointer to the (physical) end of the
918 * buffer, and once to drain the space from the (physical) beginning of
919 * the buffer to head pointer.
920 */
921static inline int copy_from_read_buf(struct tty_struct *tty,
922				      unsigned char **b,
923				      size_t *nr)
924
925{
926	int retval;
927	ssize_t n;
928	unsigned long flags;
929
930	retval = 0;
931	spin_lock_irqsave(&tty->read_lock, flags);
932	n = MIN(*nr, MIN(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail));
933	spin_unlock_irqrestore(&tty->read_lock, flags);
934	if (n) {
935		mb();
936		retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
937		n -= retval;
938		spin_lock_irqsave(&tty->read_lock, flags);
939		tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
940		tty->read_cnt -= n;
941		spin_unlock_irqrestore(&tty->read_lock, flags);
942		*b += n;
943		*nr -= n;
944	}
945	return retval;
946}
947
948static ssize_t read_chan(struct tty_struct *tty, struct file *file,
949			 unsigned char *buf, size_t nr)
950{
951	unsigned char *b = buf;
952	DECLARE_WAITQUEUE(wait, current);
953	int c;
954	int minimum, time;
955	ssize_t retval = 0;
956	ssize_t size;
957	long timeout;
958	unsigned long flags;
959
960do_it_again:
961
962	if (!tty->read_buf) {
963		printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
964		return -EIO;
965	}
966
967	/* Job control check -- must be done at start and after
968	   every sleep (POSIX.1 7.1.1.4). */
969	/* NOTE: not yet done after every sleep pending a thorough
970	   check of the logic of this change. -- jlc */
971	/* don't stop on /dev/console */
972	if (file->f_dentry->d_inode->i_rdev != CONSOLE_DEV &&
973	    file->f_dentry->d_inode->i_rdev != SYSCONS_DEV &&
974	    current->tty == tty) {
975		if (tty->pgrp <= 0)
976			printk("read_chan: tty->pgrp <= 0!\n");
977		else if (current->pgrp != tty->pgrp) {
978			if (is_ignored(SIGTTIN) ||
979			    is_orphaned_pgrp(current->pgrp))
980				return -EIO;
981			kill_pg(current->pgrp, SIGTTIN, 1);
982			return -ERESTARTSYS;
983		}
984	}
985
986	minimum = time = 0;
987	timeout = MAX_SCHEDULE_TIMEOUT;
988	if (!tty->icanon) {
989		time = (HZ / 10) * TIME_CHAR(tty);
990		minimum = MIN_CHAR(tty);
991		if (minimum) {
992			if (time)
993				tty->minimum_to_wake = 1;
994			else if (!waitqueue_active(&tty->read_wait) ||
995				 (tty->minimum_to_wake > minimum))
996				tty->minimum_to_wake = minimum;
997		} else {
998			timeout = 0;
999			if (time) {
1000				timeout = time;
1001				time = 0;
1002			}
1003			tty->minimum_to_wake = minimum = 1;
1004		}
1005	}
1006
1007	if (file->f_flags & O_NONBLOCK) {
1008		if (down_trylock(&tty->atomic_read))
1009			return -EAGAIN;
1010	}
1011	else {
1012		if (down_interruptible(&tty->atomic_read))
1013			return -ERESTARTSYS;
1014	}
1015
1016	add_wait_queue(&tty->read_wait, &wait);
1017	set_bit(TTY_DONT_FLIP, &tty->flags);
1018	while (nr) {
1019		/* First test for status change. */
1020		if (tty->packet && tty->link->ctrl_status) {
1021			unsigned char cs;
1022			if (b != buf)
1023				break;
1024			cs = tty->link->ctrl_status;
1025			tty->link->ctrl_status = 0;
1026			put_user(cs, b++);
1027			nr--;
1028			break;
1029		}
1030		/* This statement must be first before checking for input
1031		   so that any interrupt will set the state back to
1032		   TASK_RUNNING. */
1033		set_current_state(TASK_INTERRUPTIBLE);
1034
1035		if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1036		    ((minimum - (b - buf)) >= 1))
1037			tty->minimum_to_wake = (minimum - (b - buf));
1038
1039		if (!input_available_p(tty, 0)) {
1040			if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1041				retval = -EIO;
1042				break;
1043			}
1044			if (tty_hung_up_p(file))
1045				break;
1046			if (!timeout)
1047				break;
1048			if (file->f_flags & O_NONBLOCK) {
1049				retval = -EAGAIN;
1050				break;
1051			}
1052			if (signal_pending(current)) {
1053				retval = -ERESTARTSYS;
1054				break;
1055			}
1056			clear_bit(TTY_DONT_FLIP, &tty->flags);
1057			timeout = schedule_timeout(timeout);
1058			set_bit(TTY_DONT_FLIP, &tty->flags);
1059			continue;
1060		}
1061		current->state = TASK_RUNNING;
1062
1063		/* Deal with packet mode. */
1064		if (tty->packet && b == buf) {
1065			put_user(TIOCPKT_DATA, b++);
1066			nr--;
1067		}
1068
1069		if (tty->icanon) {
1070			/* N.B. avoid overrun if nr == 0 */
1071			while (nr && tty->read_cnt) {
1072 				int eol;
1073
1074				eol = test_and_clear_bit(tty->read_tail,
1075						&tty->read_flags);
1076				c = tty->read_buf[tty->read_tail];
1077				spin_lock_irqsave(&tty->read_lock, flags);
1078				tty->read_tail = ((tty->read_tail+1) &
1079						  (N_TTY_BUF_SIZE-1));
1080				tty->read_cnt--;
1081				if (eol) {
1082					/* this test should be redundant:
1083					 * we shouldn't be reading data if
1084					 * canon_data is 0
1085					 */
1086					if (--tty->canon_data < 0)
1087						tty->canon_data = 0;
1088				}
1089				spin_unlock_irqrestore(&tty->read_lock, flags);
1090
1091				if (!eol || (c != __DISABLED_CHAR)) {
1092					put_user(c, b++);
1093					nr--;
1094				}
1095				if (eol)
1096					break;
1097			}
1098		} else {
1099			int uncopied;
1100			uncopied = copy_from_read_buf(tty, &b, &nr);
1101			uncopied += copy_from_read_buf(tty, &b, &nr);
1102			if (uncopied) {
1103				retval = -EFAULT;
1104				break;
1105			}
1106		}
1107
1108		/* If there is enough space in the read buffer now, let the
1109		 * low-level driver know. We use n_tty_chars_in_buffer() to
1110		 * check the buffer, as it now knows about canonical mode.
1111		 * Otherwise, if the driver is throttled and the line is
1112		 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1113		 * we won't get any more characters.
1114		 */
1115		if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE)
1116			check_unthrottle(tty);
1117
1118		if (b - buf >= minimum)
1119			break;
1120		if (time)
1121			timeout = time;
1122	}
1123	clear_bit(TTY_DONT_FLIP, &tty->flags);
1124	up(&tty->atomic_read);
1125	remove_wait_queue(&tty->read_wait, &wait);
1126
1127	if (!waitqueue_active(&tty->read_wait))
1128		tty->minimum_to_wake = minimum;
1129
1130	current->state = TASK_RUNNING;
1131	size = b - buf;
1132	if (size) {
1133		retval = size;
1134		if (nr)
1135	       		clear_bit(TTY_PUSH, &tty->flags);
1136	} else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1137		 goto do_it_again;
1138
1139	return retval;
1140}
1141
1142static ssize_t write_chan(struct tty_struct * tty, struct file * file,
1143			  const unsigned char * buf, size_t nr)
1144{
1145	const unsigned char *b = buf;
1146	DECLARE_WAITQUEUE(wait, current);
1147	int c;
1148	ssize_t retval = 0;
1149
1150	/* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1151	if (L_TOSTOP(tty) &&
1152	    file->f_dentry->d_inode->i_rdev != CONSOLE_DEV &&
1153	    file->f_dentry->d_inode->i_rdev != SYSCONS_DEV) {
1154		retval = tty_check_change(tty);
1155		if (retval)
1156			return retval;
1157	}
1158
1159	add_wait_queue(&tty->write_wait, &wait);
1160	while (1) {
1161		set_current_state(TASK_INTERRUPTIBLE);
1162		if (signal_pending(current)) {
1163			retval = -ERESTARTSYS;
1164			break;
1165		}
1166		if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1167			retval = -EIO;
1168			break;
1169		}
1170		if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1171			while (nr > 0) {
1172				ssize_t num = opost_block(tty, b, nr);
1173				if (num < 0) {
1174					if (num == -EAGAIN)
1175						break;
1176					retval = num;
1177					goto break_out;
1178				}
1179				b += num;
1180				nr -= num;
1181				if (nr == 0)
1182					break;
1183				get_user(c, b);
1184				if (opost(c, tty) < 0)
1185					break;
1186				b++; nr--;
1187			}
1188			if (tty->driver.flush_chars)
1189				tty->driver.flush_chars(tty);
1190		} else {
1191			c = tty->driver.write(tty, 1, b, nr);
1192			if (c < 0) {
1193				retval = c;
1194				goto break_out;
1195			}
1196			b += c;
1197			nr -= c;
1198		}
1199		if (!nr)
1200			break;
1201		if (file->f_flags & O_NONBLOCK) {
1202			retval = -EAGAIN;
1203			break;
1204		}
1205		schedule();
1206	}
1207break_out:
1208	current->state = TASK_RUNNING;
1209	remove_wait_queue(&tty->write_wait, &wait);
1210	return (b - buf) ? b - buf : retval;
1211}
1212
1213/* Called without the kernel lock held - fine */
1214static unsigned int normal_poll(struct tty_struct * tty, struct file * file, poll_table *wait)
1215{
1216	unsigned int mask = 0;
1217
1218	poll_wait(file, &tty->read_wait, wait);
1219	poll_wait(file, &tty->write_wait, wait);
1220	if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
1221		mask |= POLLIN | POLLRDNORM;
1222	if (tty->packet && tty->link->ctrl_status)
1223		mask |= POLLPRI | POLLIN | POLLRDNORM;
1224	if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
1225		mask |= POLLHUP;
1226	if (tty_hung_up_p(file))
1227		mask |= POLLHUP;
1228	if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
1229		if (MIN_CHAR(tty) && !TIME_CHAR(tty))
1230			tty->minimum_to_wake = MIN_CHAR(tty);
1231		else
1232			tty->minimum_to_wake = 1;
1233	}
1234	if (tty->driver.chars_in_buffer(tty) < WAKEUP_CHARS)
1235		mask |= POLLOUT | POLLWRNORM;
1236	return mask;
1237}
1238
1239struct tty_ldisc tty_ldisc_N_TTY = {
1240	TTY_LDISC_MAGIC,	/* magic */
1241	"n_tty",		/* name */
1242	0,			/* num */
1243	0,			/* flags */
1244	n_tty_open,		/* open */
1245	n_tty_close,		/* close */
1246	n_tty_flush_buffer,	/* flush_buffer */
1247	n_tty_chars_in_buffer,	/* chars_in_buffer */
1248	read_chan,		/* read */
1249	write_chan,		/* write */
1250	n_tty_ioctl,		/* ioctl */
1251	n_tty_set_termios,	/* set_termios */
1252	normal_poll,		/* poll */
1253	n_tty_receive_buf,	/* receive_buf */
1254	n_tty_receive_room,	/* receive_room */
1255	n_tty_write_wakeup	/* write_wakeup */
1256};
1257
1258