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/mm.h>
44#include <linux/string.h>
45#include <linux/slab.h>
46#include <linux/poll.h>
47#include <linux/bitops.h>
48
49#include <asm/uaccess.h>
50#include <asm/system.h>
51
52/* number of characters left in xmit buffer before select has we have room */
53#define WAKEUP_CHARS 256
54
55/*
56 * This defines the low- and high-watermarks for throttling and
57 * unthrottling the TTY driver.  These watermarks are used for
58 * controlling the space in the read buffer.
59 */
60#define TTY_THRESHOLD_THROTTLE		128 /* now based on remaining room */
61#define TTY_THRESHOLD_UNTHROTTLE 	128
62
63static inline unsigned char *alloc_buf(void)
64{
65	gfp_t prio = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
66
67	if (PAGE_SIZE != N_TTY_BUF_SIZE)
68		return kmalloc(N_TTY_BUF_SIZE, prio);
69	else
70		return (unsigned char *)__get_free_page(prio);
71}
72
73static inline void free_buf(unsigned char *buf)
74{
75	if (PAGE_SIZE != N_TTY_BUF_SIZE)
76		kfree(buf);
77	else
78		free_page((unsigned long) buf);
79}
80
81/**
82 *	n_tty_set__room	-	receive space
83 *	@tty: terminal
84 *
85 *	Called by the driver to find out how much data it is
86 *	permitted to feed to the line discipline without any being lost
87 *	and thus to manage flow control. Not serialized. Answers for the
88 *	"instant".
89 */
90
91static void n_tty_set_room(struct tty_struct *tty)
92{
93	int	left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
94
95	/*
96	 * If we are doing input canonicalization, and there are no
97	 * pending newlines, let characters through without limit, so
98	 * that erase characters will be handled.  Other excess
99	 * characters will be beeped.
100	 */
101	if (left <= 0)
102		left = tty->icanon && !tty->canon_data;
103	tty->receive_room = left;
104}
105
106static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
107{
108	if (tty->read_cnt < N_TTY_BUF_SIZE) {
109		tty->read_buf[tty->read_head] = c;
110		tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
111		tty->read_cnt++;
112	}
113}
114
115static void put_tty_queue(unsigned char c, struct tty_struct *tty)
116{
117	unsigned long flags;
118	/*
119	 *	The problem of stomping on the buffers ends here.
120	 *	Why didn't anyone see this one coming? --AJK
121	*/
122	spin_lock_irqsave(&tty->read_lock, flags);
123	put_tty_queue_nolock(c, tty);
124	spin_unlock_irqrestore(&tty->read_lock, flags);
125}
126
127/**
128 *	check_unthrottle	-	allow new receive data
129 *	@tty; tty device
130 *
131 *	Check whether to call the driver.unthrottle function.
132 *	We test the TTY_THROTTLED bit first so that it always
133 *	indicates the current state. The decision about whether
134 *	it is worth allowing more input has been taken by the caller.
135 *	Can sleep, may be called under the atomic_read_lock mutex but
136 *	this is not guaranteed.
137 */
138
139static void check_unthrottle(struct tty_struct * tty)
140{
141	if (tty->count &&
142	    test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
143	    tty->driver->unthrottle)
144		tty->driver->unthrottle(tty);
145}
146
147/**
148 *	reset_buffer_flags	-	reset buffer state
149 *	@tty: terminal to reset
150 *
151 *	Reset the read buffer counters, clear the flags,
152 *	and make sure the driver is unthrottled. Called
153 *	from n_tty_open() and n_tty_flush_buffer().
154 */
155static void reset_buffer_flags(struct tty_struct *tty)
156{
157	unsigned long flags;
158
159	spin_lock_irqsave(&tty->read_lock, flags);
160	tty->read_head = tty->read_tail = tty->read_cnt = 0;
161	spin_unlock_irqrestore(&tty->read_lock, flags);
162	tty->canon_head = tty->canon_data = tty->erasing = 0;
163	memset(&tty->read_flags, 0, sizeof tty->read_flags);
164	n_tty_set_room(tty);
165	check_unthrottle(tty);
166}
167
168
169static void n_tty_flush_buffer(struct tty_struct * tty)
170{
171	/* clear everything and unthrottle the driver */
172	reset_buffer_flags(tty);
173
174	if (!tty->link)
175		return;
176
177	if (tty->link->packet) {
178		tty->ctrl_status |= TIOCPKT_FLUSHREAD;
179		wake_up_interruptible(&tty->link->read_wait);
180	}
181}
182
183/**
184 *	n_tty_chars_in_buffer	-	report available bytes
185 *	@tty: tty device
186 *
187 *	Report the number of characters buffered to be delivered to user
188 *	at this instant in time.
189 */
190
191static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
192{
193	unsigned long flags;
194	ssize_t n = 0;
195
196	spin_lock_irqsave(&tty->read_lock, flags);
197	if (!tty->icanon) {
198		n = tty->read_cnt;
199	} else if (tty->canon_data) {
200		n = (tty->canon_head > tty->read_tail) ?
201			tty->canon_head - tty->read_tail :
202			tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
203	}
204	spin_unlock_irqrestore(&tty->read_lock, flags);
205	return n;
206}
207
208/**
209 *	is_utf8_continuation	-	utf8 multibyte check
210 *	@c: byte to check
211 *
212 *	Returns true if the utf8 character 'c' is a multibyte continuation
213 *	character. We use this to correctly compute the on screen size
214 *	of the character when printing
215 */
216
217static inline int is_utf8_continuation(unsigned char c)
218{
219	return (c & 0xc0) == 0x80;
220}
221
222/**
223 *	is_continuation		-	multibyte check
224 *	@c: byte to check
225 *
226 *	Returns true if the utf8 character 'c' is a multibyte continuation
227 *	character and the terminal is in unicode mode.
228 */
229
230static inline int is_continuation(unsigned char c, struct tty_struct *tty)
231{
232	return I_IUTF8(tty) && is_utf8_continuation(c);
233}
234
235/**
236 *	opost			-	output post processor
237 *	@c: character (or partial unicode symbol)
238 *	@tty: terminal device
239 *
240 *	Perform OPOST processing.  Returns -1 when the output device is
241 *	full and the character must be retried. Note that Linux currently
242 *	ignores TABDLY, CRDLY, VTDLY, FFDLY and NLDLY. They simply aren't
243 *	relevant in the world today. If you ever need them, add them here.
244 *
245 *	Called from both the receive and transmit sides and can be called
246 *	re-entrantly. Relies on lock_kernel() still.
247 */
248
249static int opost(unsigned char c, struct tty_struct *tty)
250{
251	int	space, spaces;
252
253	space = tty->driver->write_room(tty);
254	if (!space)
255		return -1;
256
257	if (O_OPOST(tty)) {
258		switch (c) {
259		case '\n':
260			if (O_ONLRET(tty))
261				tty->column = 0;
262			if (O_ONLCR(tty)) {
263				if (space < 2)
264					return -1;
265				tty->driver->put_char(tty, '\r');
266				tty->column = 0;
267			}
268			tty->canon_column = tty->column;
269			break;
270		case '\r':
271			if (O_ONOCR(tty) && tty->column == 0)
272				return 0;
273			if (O_OCRNL(tty)) {
274				c = '\n';
275				if (O_ONLRET(tty))
276					tty->canon_column = tty->column = 0;
277				break;
278			}
279			tty->canon_column = tty->column = 0;
280			break;
281		case '\t':
282			spaces = 8 - (tty->column & 7);
283			if (O_TABDLY(tty) == XTABS) {
284				if (space < spaces)
285					return -1;
286				tty->column += spaces;
287				tty->driver->write(tty, "        ", spaces);
288				return 0;
289			}
290			tty->column += spaces;
291			break;
292		case '\b':
293			if (tty->column > 0)
294				tty->column--;
295			break;
296		default:
297			if (O_OLCUC(tty))
298				c = toupper(c);
299			if (!iscntrl(c) && !is_continuation(c, tty))
300				tty->column++;
301			break;
302		}
303	}
304	tty->driver->put_char(tty, c);
305	return 0;
306}
307
308/**
309 *	opost_block		-	block postprocess
310 *	@tty: terminal device
311 *	@inbuf: user buffer
312 *	@nr: number of bytes
313 *
314 *	This path is used to speed up block console writes, among other
315 *	things when processing blocks of output data. It handles only
316 *	the simple cases normally found and helps to generate blocks of
317 *	symbols for the console driver and thus improve performance.
318 *
319 *	Called from write_chan under the tty layer write lock.
320 */
321
322static ssize_t opost_block(struct tty_struct * tty,
323		       const unsigned char * buf, unsigned int nr)
324{
325	int	space;
326	int 	i;
327	const unsigned char *cp;
328
329	space = tty->driver->write_room(tty);
330	if (!space)
331		return 0;
332	if (nr > space)
333		nr = space;
334
335	for (i = 0, cp = buf; i < nr; i++, cp++) {
336		switch (*cp) {
337		case '\n':
338			if (O_ONLRET(tty))
339				tty->column = 0;
340			if (O_ONLCR(tty))
341				goto break_out;
342			tty->canon_column = tty->column;
343			break;
344		case '\r':
345			if (O_ONOCR(tty) && tty->column == 0)
346				goto break_out;
347			if (O_OCRNL(tty))
348				goto break_out;
349			tty->canon_column = tty->column = 0;
350			break;
351		case '\t':
352			goto break_out;
353		case '\b':
354			if (tty->column > 0)
355				tty->column--;
356			break;
357		default:
358			if (O_OLCUC(tty))
359				goto break_out;
360			if (!iscntrl(*cp))
361				tty->column++;
362			break;
363		}
364	}
365break_out:
366	if (tty->driver->flush_chars)
367		tty->driver->flush_chars(tty);
368	i = tty->driver->write(tty, buf, i);
369	return i;
370}
371
372
373/**
374 *	put_char	-	write character to driver
375 *	@c: character (or part of unicode symbol)
376 *	@tty: terminal device
377 *
378 *	Queue a byte to the driver layer for output
379 */
380
381static inline void put_char(unsigned char c, struct tty_struct *tty)
382{
383	tty->driver->put_char(tty, c);
384}
385
386/**
387 *	echo_char	-	echo characters
388 *	@c: unicode byte to echo
389 *	@tty: terminal device
390 *
391 *	Echo user input back onto the screen. This must be called only when
392 *	L_ECHO(tty) is true. Called from the driver receive_buf path.
393 */
394
395static void echo_char(unsigned char c, struct tty_struct *tty)
396{
397	if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
398		put_char('^', tty);
399		put_char(c ^ 0100, tty);
400		tty->column += 2;
401	} else
402		opost(c, tty);
403}
404
405static inline void finish_erasing(struct tty_struct *tty)
406{
407	if (tty->erasing) {
408		put_char('/', tty);
409		tty->column++;
410		tty->erasing = 0;
411	}
412}
413
414/**
415 *	eraser		-	handle erase function
416 *	@c: character input
417 *	@tty: terminal device
418 *
419 *	Perform erase and neccessary output when an erase character is
420 *	present in the stream from the driver layer. Handles the complexities
421 *	of UTF-8 multibyte symbols.
422 */
423
424static void eraser(unsigned char c, struct tty_struct *tty)
425{
426	enum { ERASE, WERASE, KILL } kill_type;
427	int head, seen_alnums, cnt;
428	unsigned long flags;
429
430	if (tty->read_head == tty->canon_head) {
431		/* opost('\a', tty); */		/* what do you think? */
432		return;
433	}
434	if (c == ERASE_CHAR(tty))
435		kill_type = ERASE;
436	else if (c == WERASE_CHAR(tty))
437		kill_type = WERASE;
438	else {
439		if (!L_ECHO(tty)) {
440			spin_lock_irqsave(&tty->read_lock, flags);
441			tty->read_cnt -= ((tty->read_head - tty->canon_head) &
442					  (N_TTY_BUF_SIZE - 1));
443			tty->read_head = tty->canon_head;
444			spin_unlock_irqrestore(&tty->read_lock, flags);
445			return;
446		}
447		if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
448			spin_lock_irqsave(&tty->read_lock, flags);
449			tty->read_cnt -= ((tty->read_head - tty->canon_head) &
450					  (N_TTY_BUF_SIZE - 1));
451			tty->read_head = tty->canon_head;
452			spin_unlock_irqrestore(&tty->read_lock, flags);
453			finish_erasing(tty);
454			echo_char(KILL_CHAR(tty), tty);
455			/* Add a newline if ECHOK is on and ECHOKE is off. */
456			if (L_ECHOK(tty))
457				opost('\n', tty);
458			return;
459		}
460		kill_type = KILL;
461	}
462
463	seen_alnums = 0;
464	while (tty->read_head != tty->canon_head) {
465		head = tty->read_head;
466
467		/* erase a single possibly multibyte character */
468		do {
469			head = (head - 1) & (N_TTY_BUF_SIZE-1);
470			c = tty->read_buf[head];
471		} while (is_continuation(c, tty) && head != tty->canon_head);
472
473		/* do not partially erase */
474		if (is_continuation(c, tty))
475			break;
476
477		if (kill_type == WERASE) {
478			/* Equivalent to BSD's ALTWERASE. */
479			if (isalnum(c) || c == '_')
480				seen_alnums++;
481			else if (seen_alnums)
482				break;
483		}
484		cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
485		spin_lock_irqsave(&tty->read_lock, flags);
486		tty->read_head = head;
487		tty->read_cnt -= cnt;
488		spin_unlock_irqrestore(&tty->read_lock, flags);
489		if (L_ECHO(tty)) {
490			if (L_ECHOPRT(tty)) {
491				if (!tty->erasing) {
492					put_char('\\', tty);
493					tty->column++;
494					tty->erasing = 1;
495				}
496				/* if cnt > 1, output a multi-byte character */
497				echo_char(c, tty);
498				while (--cnt > 0) {
499					head = (head+1) & (N_TTY_BUF_SIZE-1);
500					put_char(tty->read_buf[head], tty);
501				}
502			} else if (kill_type == ERASE && !L_ECHOE(tty)) {
503				echo_char(ERASE_CHAR(tty), tty);
504			} else if (c == '\t') {
505				unsigned int col = tty->canon_column;
506				unsigned long tail = tty->canon_head;
507
508				/* Find the column of the last char. */
509				while (tail != tty->read_head) {
510					c = tty->read_buf[tail];
511					if (c == '\t')
512						col = (col | 7) + 1;
513					else if (iscntrl(c)) {
514						if (L_ECHOCTL(tty))
515							col += 2;
516					} else if (!is_continuation(c, tty))
517						col++;
518					tail = (tail+1) & (N_TTY_BUF_SIZE-1);
519				}
520
521				/* should never happen */
522				if (tty->column > 0x80000000)
523					tty->column = 0;
524
525				/* Now backup to that column. */
526				while (tty->column > col) {
527					/* Can't use opost here. */
528					put_char('\b', tty);
529					if (tty->column > 0)
530						tty->column--;
531				}
532			} else {
533				if (iscntrl(c) && L_ECHOCTL(tty)) {
534					put_char('\b', tty);
535					put_char(' ', tty);
536					put_char('\b', tty);
537					if (tty->column > 0)
538						tty->column--;
539				}
540				if (!iscntrl(c) || L_ECHOCTL(tty)) {
541					put_char('\b', tty);
542					put_char(' ', tty);
543					put_char('\b', tty);
544					if (tty->column > 0)
545						tty->column--;
546				}
547			}
548		}
549		if (kill_type == ERASE)
550			break;
551	}
552	if (tty->read_head == tty->canon_head)
553		finish_erasing(tty);
554}
555
556/**
557 *	isig		-	handle the ISIG optio
558 *	@sig: signal
559 *	@tty: terminal
560 *	@flush: force flush
561 *
562 *	Called when a signal is being sent due to terminal input. This
563 *	may caus terminal flushing to take place according to the termios
564 *	settings and character used. Called from the driver receive_buf
565 *	path so serialized.
566 */
567
568static inline void isig(int sig, struct tty_struct *tty, int flush)
569{
570	if (tty->pgrp)
571		kill_pgrp(tty->pgrp, sig, 1);
572	if (flush || !L_NOFLSH(tty)) {
573		n_tty_flush_buffer(tty);
574		if (tty->driver->flush_buffer)
575			tty->driver->flush_buffer(tty);
576	}
577}
578
579/**
580 *	n_tty_receive_break	-	handle break
581 *	@tty: terminal
582 *
583 *	An RS232 break event has been hit in the incoming bitstream. This
584 *	can cause a variety of events depending upon the termios settings.
585 *
586 *	Called from the receive_buf path so single threaded.
587 */
588
589static inline void n_tty_receive_break(struct tty_struct *tty)
590{
591	if (I_IGNBRK(tty))
592		return;
593	if (I_BRKINT(tty)) {
594		isig(SIGINT, tty, 1);
595		return;
596	}
597	if (I_PARMRK(tty)) {
598		put_tty_queue('\377', tty);
599		put_tty_queue('\0', tty);
600	}
601	put_tty_queue('\0', tty);
602	wake_up_interruptible(&tty->read_wait);
603}
604
605/**
606 *	n_tty_receive_overrun	-	handle overrun reporting
607 *	@tty: terminal
608 *
609 *	Data arrived faster than we could process it. While the tty
610 *	driver has flagged this the bits that were missed are gone
611 *	forever.
612 *
613 *	Called from the receive_buf path so single threaded. Does not
614 *	need locking as num_overrun and overrun_time are function
615 *	private.
616 */
617
618static inline void n_tty_receive_overrun(struct tty_struct *tty)
619{
620	char buf[64];
621
622	tty->num_overrun++;
623	if (time_before(tty->overrun_time, jiffies - HZ) ||
624			time_after(tty->overrun_time, jiffies)) {
625		printk(KERN_WARNING "%s: %d input overrun(s)\n",
626			tty_name(tty, buf),
627			tty->num_overrun);
628		tty->overrun_time = jiffies;
629		tty->num_overrun = 0;
630	}
631}
632
633/**
634 *	n_tty_receive_parity_error	-	error notifier
635 *	@tty: terminal device
636 *	@c: character
637 *
638 *	Process a parity error and queue the right data to indicate
639 *	the error case if neccessary. Locking as per n_tty_receive_buf.
640 */
641static inline void n_tty_receive_parity_error(struct tty_struct *tty,
642					      unsigned char c)
643{
644	if (I_IGNPAR(tty)) {
645		return;
646	}
647	if (I_PARMRK(tty)) {
648		put_tty_queue('\377', tty);
649		put_tty_queue('\0', tty);
650		put_tty_queue(c, tty);
651	} else	if (I_INPCK(tty))
652		put_tty_queue('\0', tty);
653	else
654		put_tty_queue(c, tty);
655	wake_up_interruptible(&tty->read_wait);
656}
657
658/**
659 *	n_tty_receive_char	-	perform processing
660 *	@tty: terminal device
661 *	@c: character
662 *
663 *	Process an individual character of input received from the driver.
664 *	This is serialized with respect to itself by the rules for the
665 *	driver above.
666 */
667
668static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
669{
670	unsigned long flags;
671
672	if (tty->raw) {
673		put_tty_queue(c, tty);
674		return;
675	}
676
677	if (tty->stopped && !tty->flow_stopped &&
678	    I_IXON(tty) && I_IXANY(tty)) {
679		start_tty(tty);
680		return;
681	}
682
683	if (I_ISTRIP(tty))
684		c &= 0x7f;
685	if (I_IUCLC(tty) && L_IEXTEN(tty))
686		c=tolower(c);
687
688	if (tty->closing) {
689		if (I_IXON(tty)) {
690			if (c == START_CHAR(tty))
691				start_tty(tty);
692			else if (c == STOP_CHAR(tty))
693				stop_tty(tty);
694		}
695		return;
696	}
697
698	/*
699	 * If the previous character was LNEXT, or we know that this
700	 * character is not one of the characters that we'll have to
701	 * handle specially, do shortcut processing to speed things
702	 * up.
703	 */
704	if (!test_bit(c, tty->process_char_map) || tty->lnext) {
705		finish_erasing(tty);
706		tty->lnext = 0;
707		if (L_ECHO(tty)) {
708			if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
709				put_char('\a', tty); /* beep if no space */
710				return;
711			}
712			/* Record the column of first canon char. */
713			if (tty->canon_head == tty->read_head)
714				tty->canon_column = tty->column;
715			echo_char(c, tty);
716		}
717		if (I_PARMRK(tty) && c == (unsigned char) '\377')
718			put_tty_queue(c, tty);
719		put_tty_queue(c, tty);
720		return;
721	}
722
723	if (c == '\r') {
724		if (I_IGNCR(tty))
725			return;
726		if (I_ICRNL(tty))
727			c = '\n';
728	} else if (c == '\n' && I_INLCR(tty))
729		c = '\r';
730	if (I_IXON(tty)) {
731		if (c == START_CHAR(tty)) {
732			start_tty(tty);
733			return;
734		}
735		if (c == STOP_CHAR(tty)) {
736			stop_tty(tty);
737			return;
738		}
739	}
740	if (L_ISIG(tty)) {
741		int signal;
742		signal = SIGINT;
743		if (c == INTR_CHAR(tty))
744			goto send_signal;
745		signal = SIGQUIT;
746		if (c == QUIT_CHAR(tty))
747			goto send_signal;
748		signal = SIGTSTP;
749		if (c == SUSP_CHAR(tty)) {
750send_signal:
751			isig(signal, tty, 0);
752			return;
753		}
754	}
755	if (tty->icanon) {
756		if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
757		    (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
758			eraser(c, tty);
759			return;
760		}
761		if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
762			tty->lnext = 1;
763			if (L_ECHO(tty)) {
764				finish_erasing(tty);
765				if (L_ECHOCTL(tty)) {
766					put_char('^', tty);
767					put_char('\b', tty);
768				}
769			}
770			return;
771		}
772		if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
773		    L_IEXTEN(tty)) {
774			unsigned long tail = tty->canon_head;
775
776			finish_erasing(tty);
777			echo_char(c, tty);
778			opost('\n', tty);
779			while (tail != tty->read_head) {
780				echo_char(tty->read_buf[tail], tty);
781				tail = (tail+1) & (N_TTY_BUF_SIZE-1);
782			}
783			return;
784		}
785		if (c == '\n') {
786			if (L_ECHO(tty) || L_ECHONL(tty)) {
787				if (tty->read_cnt >= N_TTY_BUF_SIZE-1)
788					put_char('\a', tty);
789				opost('\n', tty);
790			}
791			goto handle_newline;
792		}
793		if (c == EOF_CHAR(tty)) {
794		        if (tty->canon_head != tty->read_head)
795			        set_bit(TTY_PUSH, &tty->flags);
796			c = __DISABLED_CHAR;
797			goto handle_newline;
798		}
799		if ((c == EOL_CHAR(tty)) ||
800		    (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
801			if (L_ECHO(tty)) {
802				if (tty->read_cnt >= N_TTY_BUF_SIZE-1)
803					put_char('\a', tty);
804				/* Record the column of first canon char. */
805				if (tty->canon_head == tty->read_head)
806					tty->canon_column = tty->column;
807				echo_char(c, tty);
808			}
809			if (I_PARMRK(tty) && c == (unsigned char) '\377')
810				put_tty_queue(c, tty);
811
812		handle_newline:
813			spin_lock_irqsave(&tty->read_lock, flags);
814			set_bit(tty->read_head, tty->read_flags);
815			put_tty_queue_nolock(c, tty);
816			tty->canon_head = tty->read_head;
817			tty->canon_data++;
818			spin_unlock_irqrestore(&tty->read_lock, flags);
819			kill_fasync(&tty->fasync, SIGIO, POLL_IN);
820			if (waitqueue_active(&tty->read_wait))
821				wake_up_interruptible(&tty->read_wait);
822			return;
823		}
824	}
825
826	finish_erasing(tty);
827	if (L_ECHO(tty)) {
828		if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
829			put_char('\a', tty); /* beep if no space */
830			return;
831		}
832		if (c == '\n')
833			opost('\n', tty);
834		else {
835			/* Record the column of first canon char. */
836			if (tty->canon_head == tty->read_head)
837				tty->canon_column = tty->column;
838			echo_char(c, tty);
839		}
840	}
841
842	if (I_PARMRK(tty) && c == (unsigned char) '\377')
843		put_tty_queue(c, tty);
844
845	put_tty_queue(c, tty);
846}
847
848
849/**
850 *	n_tty_write_wakeup	-	asynchronous I/O notifier
851 *	@tty: tty device
852 *
853 *	Required for the ptys, serial driver etc. since processes
854 *	that attach themselves to the master and rely on ASYNC
855 *	IO must be woken up
856 */
857
858static void n_tty_write_wakeup(struct tty_struct *tty)
859{
860	if (tty->fasync)
861	{
862 		set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
863		kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
864	}
865	return;
866}
867
868/**
869 *	n_tty_receive_buf	-	data receive
870 *	@tty: terminal device
871 *	@cp: buffer
872 *	@fp: flag buffer
873 *	@count: characters
874 *
875 *	Called by the terminal driver when a block of characters has
876 *	been received. This function must be called from soft contexts
877 *	not from interrupt context. The driver is responsible for making
878 *	calls one at a time and in order (or using flush_to_ldisc)
879 */
880
881static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
882			      char *fp, int count)
883{
884	const unsigned char *p;
885	char *f, flags = TTY_NORMAL;
886	int	i;
887	char	buf[64];
888	unsigned long cpuflags;
889
890	if (!tty->read_buf)
891		return;
892
893	if (tty->real_raw) {
894		spin_lock_irqsave(&tty->read_lock, cpuflags);
895		i = min(N_TTY_BUF_SIZE - tty->read_cnt,
896			N_TTY_BUF_SIZE - tty->read_head);
897		i = min(count, i);
898		memcpy(tty->read_buf + tty->read_head, cp, i);
899		tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
900		tty->read_cnt += i;
901		cp += i;
902		count -= i;
903
904		i = min(N_TTY_BUF_SIZE - tty->read_cnt,
905			N_TTY_BUF_SIZE - tty->read_head);
906		i = min(count, i);
907		memcpy(tty->read_buf + tty->read_head, cp, i);
908		tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
909		tty->read_cnt += i;
910		spin_unlock_irqrestore(&tty->read_lock, cpuflags);
911	} else {
912		for (i=count, p = cp, f = fp; i; i--, p++) {
913			if (f)
914				flags = *f++;
915			switch (flags) {
916			case TTY_NORMAL:
917				n_tty_receive_char(tty, *p);
918				break;
919			case TTY_BREAK:
920				n_tty_receive_break(tty);
921				break;
922			case TTY_PARITY:
923			case TTY_FRAME:
924				n_tty_receive_parity_error(tty, *p);
925				break;
926			case TTY_OVERRUN:
927				n_tty_receive_overrun(tty);
928				break;
929			default:
930				printk("%s: unknown flag %d\n",
931				       tty_name(tty, buf), flags);
932				break;
933			}
934		}
935		if (tty->driver->flush_chars)
936			tty->driver->flush_chars(tty);
937	}
938
939	n_tty_set_room(tty);
940
941	if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
942		kill_fasync(&tty->fasync, SIGIO, POLL_IN);
943		if (waitqueue_active(&tty->read_wait))
944			wake_up_interruptible(&tty->read_wait);
945	}
946
947	/*
948	 * Check the remaining room for the input canonicalization
949	 * mode.  We don't want to throttle the driver if we're in
950	 * canonical mode and don't have a newline yet!
951	 */
952	if (tty->receive_room < TTY_THRESHOLD_THROTTLE) {
953		/* check TTY_THROTTLED first so it indicates our state */
954		if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
955		    tty->driver->throttle)
956			tty->driver->throttle(tty);
957	}
958}
959
960int is_ignored(int sig)
961{
962	return (sigismember(&current->blocked, sig) ||
963	        current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
964}
965
966/**
967 *	n_tty_set_termios	-	termios data changed
968 *	@tty: terminal
969 *	@old: previous data
970 *
971 *	Called by the tty layer when the user changes termios flags so
972 *	that the line discipline can plan ahead. This function cannot sleep
973 *	and is protected from re-entry by the tty layer. The user is
974 *	guaranteed that this function will not be re-entered or in progress
975 *	when the ldisc is closed.
976 */
977
978static void n_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
979{
980	if (!tty)
981		return;
982
983	tty->icanon = (L_ICANON(tty) != 0);
984	if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
985		tty->raw = 1;
986		tty->real_raw = 1;
987		n_tty_set_room(tty);
988		return;
989	}
990	if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
991	    I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
992	    I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
993	    I_PARMRK(tty)) {
994		memset(tty->process_char_map, 0, 256/8);
995
996		if (I_IGNCR(tty) || I_ICRNL(tty))
997			set_bit('\r', tty->process_char_map);
998		if (I_INLCR(tty))
999			set_bit('\n', tty->process_char_map);
1000
1001		if (L_ICANON(tty)) {
1002			set_bit(ERASE_CHAR(tty), tty->process_char_map);
1003			set_bit(KILL_CHAR(tty), tty->process_char_map);
1004			set_bit(EOF_CHAR(tty), tty->process_char_map);
1005			set_bit('\n', tty->process_char_map);
1006			set_bit(EOL_CHAR(tty), tty->process_char_map);
1007			if (L_IEXTEN(tty)) {
1008				set_bit(WERASE_CHAR(tty),
1009					tty->process_char_map);
1010				set_bit(LNEXT_CHAR(tty),
1011					tty->process_char_map);
1012				set_bit(EOL2_CHAR(tty),
1013					tty->process_char_map);
1014				if (L_ECHO(tty))
1015					set_bit(REPRINT_CHAR(tty),
1016						tty->process_char_map);
1017			}
1018		}
1019		if (I_IXON(tty)) {
1020			set_bit(START_CHAR(tty), tty->process_char_map);
1021			set_bit(STOP_CHAR(tty), tty->process_char_map);
1022		}
1023		if (L_ISIG(tty)) {
1024			set_bit(INTR_CHAR(tty), tty->process_char_map);
1025			set_bit(QUIT_CHAR(tty), tty->process_char_map);
1026			set_bit(SUSP_CHAR(tty), tty->process_char_map);
1027		}
1028		clear_bit(__DISABLED_CHAR, tty->process_char_map);
1029		tty->raw = 0;
1030		tty->real_raw = 0;
1031	} else {
1032		tty->raw = 1;
1033		if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1034		    (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1035		    (tty->driver->flags & TTY_DRIVER_REAL_RAW))
1036			tty->real_raw = 1;
1037		else
1038			tty->real_raw = 0;
1039	}
1040	n_tty_set_room(tty);
1041}
1042
1043/**
1044 *	n_tty_close		-	close the ldisc for this tty
1045 *	@tty: device
1046 *
1047 *	Called from the terminal layer when this line discipline is
1048 *	being shut down, either because of a close or becsuse of a
1049 *	discipline change. The function will not be called while other
1050 *	ldisc methods are in progress.
1051 */
1052
1053static void n_tty_close(struct tty_struct *tty)
1054{
1055	n_tty_flush_buffer(tty);
1056	if (tty->read_buf) {
1057		free_buf(tty->read_buf);
1058		tty->read_buf = NULL;
1059	}
1060}
1061
1062/**
1063 *	n_tty_open		-	open an ldisc
1064 *	@tty: terminal to open
1065 *
1066 *	Called when this line discipline is being attached to the
1067 *	terminal device. Can sleep. Called serialized so that no
1068 *	other events will occur in parallel. No further open will occur
1069 *	until a close.
1070 */
1071
1072static int n_tty_open(struct tty_struct *tty)
1073{
1074	if (!tty)
1075		return -EINVAL;
1076
1077	/* This one is ugly. Currently a malloc failure here can panic */
1078	if (!tty->read_buf) {
1079		tty->read_buf = alloc_buf();
1080		if (!tty->read_buf)
1081			return -ENOMEM;
1082	}
1083	memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
1084	reset_buffer_flags(tty);
1085	tty->column = 0;
1086	n_tty_set_termios(tty, NULL);
1087	tty->minimum_to_wake = 1;
1088	tty->closing = 0;
1089	return 0;
1090}
1091
1092static inline int input_available_p(struct tty_struct *tty, int amt)
1093{
1094	if (tty->icanon) {
1095		if (tty->canon_data)
1096			return 1;
1097	} else if (tty->read_cnt >= (amt ? amt : 1))
1098		return 1;
1099
1100	return 0;
1101}
1102
1103/**
1104 * 	copy_from_read_buf	-	copy read data directly
1105 *	@tty: terminal device
1106 *	@b: user data
1107 *	@nr: size of data
1108 *
1109 *	Helper function to speed up read_chan.  It is only called when
1110 *	ICANON is off; it copies characters straight from the tty queue to
1111 *	user space directly.  It can be profitably called twice; once to
1112 *	drain the space from the tail pointer to the (physical) end of the
1113 *	buffer, and once to drain the space from the (physical) beginning of
1114 *	the buffer to head pointer.
1115 *
1116 *	Called under the tty->atomic_read_lock sem
1117 *
1118 */
1119
1120static int copy_from_read_buf(struct tty_struct *tty,
1121				      unsigned char __user **b,
1122				      size_t *nr)
1123
1124{
1125	int retval;
1126	size_t n;
1127	unsigned long flags;
1128
1129	retval = 0;
1130	spin_lock_irqsave(&tty->read_lock, flags);
1131	n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
1132	n = min(*nr, n);
1133	spin_unlock_irqrestore(&tty->read_lock, flags);
1134	if (n) {
1135		retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
1136		n -= retval;
1137		spin_lock_irqsave(&tty->read_lock, flags);
1138		tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
1139		tty->read_cnt -= n;
1140		spin_unlock_irqrestore(&tty->read_lock, flags);
1141		*b += n;
1142		*nr -= n;
1143	}
1144	return retval;
1145}
1146
1147extern ssize_t redirected_tty_write(struct file *,const char *,size_t,loff_t *);
1148
1149/**
1150 *	job_control		-	check job control
1151 *	@tty: tty
1152 *	@file: file handle
1153 *
1154 *	Perform job control management checks on this file/tty descriptor
1155 *	and if appropriate send any needed signals and return a negative
1156 *	error code if action should be taken.
1157 */
1158
1159static int job_control(struct tty_struct *tty, struct file *file)
1160{
1161	/* Job control check -- must be done at start and after
1162	   every sleep (POSIX.1 7.1.1.4). */
1163	/* NOTE: not yet done after every sleep pending a thorough
1164	   check of the logic of this change. -- jlc */
1165	/* don't stop on /dev/console */
1166	if (file->f_op->write != redirected_tty_write &&
1167	    current->signal->tty == tty) {
1168		if (!tty->pgrp)
1169			printk("read_chan: no tty->pgrp!\n");
1170		else if (task_pgrp(current) != tty->pgrp) {
1171			if (is_ignored(SIGTTIN) ||
1172			    is_current_pgrp_orphaned())
1173				return -EIO;
1174			kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1175			set_thread_flag(TIF_SIGPENDING);
1176			return -ERESTARTSYS;
1177		}
1178	}
1179	return 0;
1180}
1181
1182
1183/**
1184 *	read_chan		-	read function for tty
1185 *	@tty: tty device
1186 *	@file: file object
1187 *	@buf: userspace buffer pointer
1188 *	@nr: size of I/O
1189 *
1190 *	Perform reads for the line discipline. We are guaranteed that the
1191 *	line discipline will not be closed under us but we may get multiple
1192 *	parallel readers and must handle this ourselves. We may also get
1193 *	a hangup. Always called in user context, may sleep.
1194 *
1195 *	This code must be sure never to sleep through a hangup.
1196 */
1197
1198static ssize_t read_chan(struct tty_struct *tty, struct file *file,
1199			 unsigned char __user *buf, size_t nr)
1200{
1201	unsigned char __user *b = buf;
1202	DECLARE_WAITQUEUE(wait, current);
1203	int c;
1204	int minimum, time;
1205	ssize_t retval = 0;
1206	ssize_t size;
1207	long timeout;
1208	unsigned long flags;
1209
1210do_it_again:
1211
1212	if (!tty->read_buf) {
1213		printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
1214		return -EIO;
1215	}
1216
1217	c = job_control(tty, file);
1218	if(c < 0)
1219		return c;
1220
1221	minimum = time = 0;
1222	timeout = MAX_SCHEDULE_TIMEOUT;
1223	if (!tty->icanon) {
1224		time = (HZ / 10) * TIME_CHAR(tty);
1225		minimum = MIN_CHAR(tty);
1226		if (minimum) {
1227			if (time)
1228				tty->minimum_to_wake = 1;
1229			else if (!waitqueue_active(&tty->read_wait) ||
1230				 (tty->minimum_to_wake > minimum))
1231				tty->minimum_to_wake = minimum;
1232		} else {
1233			timeout = 0;
1234			if (time) {
1235				timeout = time;
1236				time = 0;
1237			}
1238			tty->minimum_to_wake = minimum = 1;
1239		}
1240	}
1241
1242	/*
1243	 *	Internal serialization of reads.
1244	 */
1245	if (file->f_flags & O_NONBLOCK) {
1246		if (!mutex_trylock(&tty->atomic_read_lock))
1247			return -EAGAIN;
1248	}
1249	else {
1250		if (mutex_lock_interruptible(&tty->atomic_read_lock))
1251			return -ERESTARTSYS;
1252	}
1253
1254	add_wait_queue(&tty->read_wait, &wait);
1255	while (nr) {
1256		/* First test for status change. */
1257		if (tty->packet && tty->link->ctrl_status) {
1258			unsigned char cs;
1259			if (b != buf)
1260				break;
1261			cs = tty->link->ctrl_status;
1262			tty->link->ctrl_status = 0;
1263			if (put_user(cs, b++)) {
1264				retval = -EFAULT;
1265				b--;
1266				break;
1267			}
1268			nr--;
1269			break;
1270		}
1271		/* This statement must be first before checking for input
1272		   so that any interrupt will set the state back to
1273		   TASK_RUNNING. */
1274		set_current_state(TASK_INTERRUPTIBLE);
1275
1276		if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1277		    ((minimum - (b - buf)) >= 1))
1278			tty->minimum_to_wake = (minimum - (b - buf));
1279
1280		if (!input_available_p(tty, 0)) {
1281			if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1282				retval = -EIO;
1283				break;
1284			}
1285			if (tty_hung_up_p(file))
1286				break;
1287			if (!timeout)
1288				break;
1289			if (file->f_flags & O_NONBLOCK) {
1290				retval = -EAGAIN;
1291				break;
1292			}
1293			if (signal_pending(current)) {
1294				retval = -ERESTARTSYS;
1295				break;
1296			}
1297			n_tty_set_room(tty);
1298			timeout = schedule_timeout(timeout);
1299			continue;
1300		}
1301		__set_current_state(TASK_RUNNING);
1302
1303		/* Deal with packet mode. */
1304		if (tty->packet && b == buf) {
1305			if (put_user(TIOCPKT_DATA, b++)) {
1306				retval = -EFAULT;
1307				b--;
1308				break;
1309			}
1310			nr--;
1311		}
1312
1313		if (tty->icanon) {
1314			/* N.B. avoid overrun if nr == 0 */
1315			while (nr && tty->read_cnt) {
1316 				int eol;
1317
1318				eol = test_and_clear_bit(tty->read_tail,
1319						tty->read_flags);
1320				c = tty->read_buf[tty->read_tail];
1321				spin_lock_irqsave(&tty->read_lock, flags);
1322				tty->read_tail = ((tty->read_tail+1) &
1323						  (N_TTY_BUF_SIZE-1));
1324				tty->read_cnt--;
1325				if (eol) {
1326					/* this test should be redundant:
1327					 * we shouldn't be reading data if
1328					 * canon_data is 0
1329					 */
1330					if (--tty->canon_data < 0)
1331						tty->canon_data = 0;
1332				}
1333				spin_unlock_irqrestore(&tty->read_lock, flags);
1334
1335				if (!eol || (c != __DISABLED_CHAR)) {
1336					if (put_user(c, b++)) {
1337						retval = -EFAULT;
1338						b--;
1339						break;
1340					}
1341					nr--;
1342				}
1343				if (eol)
1344					break;
1345			}
1346			if (retval)
1347				break;
1348		} else {
1349			int uncopied;
1350			uncopied = copy_from_read_buf(tty, &b, &nr);
1351			uncopied += copy_from_read_buf(tty, &b, &nr);
1352			if (uncopied) {
1353				retval = -EFAULT;
1354				break;
1355			}
1356		}
1357
1358		/* If there is enough space in the read buffer now, let the
1359		 * low-level driver know. We use n_tty_chars_in_buffer() to
1360		 * check the buffer, as it now knows about canonical mode.
1361		 * Otherwise, if the driver is throttled and the line is
1362		 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1363		 * we won't get any more characters.
1364		 */
1365		if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) {
1366			n_tty_set_room(tty);
1367			check_unthrottle(tty);
1368		}
1369
1370		if (b - buf >= minimum)
1371			break;
1372		if (time)
1373			timeout = time;
1374	}
1375	mutex_unlock(&tty->atomic_read_lock);
1376	remove_wait_queue(&tty->read_wait, &wait);
1377
1378	if (!waitqueue_active(&tty->read_wait))
1379		tty->minimum_to_wake = minimum;
1380
1381	__set_current_state(TASK_RUNNING);
1382	size = b - buf;
1383	if (size) {
1384		retval = size;
1385		if (nr)
1386	       		clear_bit(TTY_PUSH, &tty->flags);
1387	} else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1388		 goto do_it_again;
1389
1390	n_tty_set_room(tty);
1391
1392	return retval;
1393}
1394
1395/**
1396 *	write_chan		-	write function for tty
1397 *	@tty: tty device
1398 *	@file: file object
1399 *	@buf: userspace buffer pointer
1400 *	@nr: size of I/O
1401 *
1402 *	Write function of the terminal device. This is serialized with
1403 *	respect to other write callers but not to termios changes, reads
1404 *	and other such events. We must be careful with N_TTY as the receive
1405 *	code will echo characters, thus calling driver write methods.
1406 *
1407 *	This code must be sure never to sleep through a hangup.
1408 */
1409
1410static ssize_t write_chan(struct tty_struct * tty, struct file * file,
1411			  const unsigned char * buf, size_t nr)
1412{
1413	const unsigned char *b = buf;
1414	DECLARE_WAITQUEUE(wait, current);
1415	int c;
1416	ssize_t retval = 0;
1417
1418	/* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1419	if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
1420		retval = tty_check_change(tty);
1421		if (retval)
1422			return retval;
1423	}
1424
1425	add_wait_queue(&tty->write_wait, &wait);
1426	while (1) {
1427		set_current_state(TASK_INTERRUPTIBLE);
1428		if (signal_pending(current)) {
1429			retval = -ERESTARTSYS;
1430			break;
1431		}
1432		if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1433			retval = -EIO;
1434			break;
1435		}
1436		if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1437			while (nr > 0) {
1438				ssize_t num = opost_block(tty, b, nr);
1439				if (num < 0) {
1440					if (num == -EAGAIN)
1441						break;
1442					retval = num;
1443					goto break_out;
1444				}
1445				b += num;
1446				nr -= num;
1447				if (nr == 0)
1448					break;
1449				c = *b;
1450				if (opost(c, tty) < 0)
1451					break;
1452				b++; nr--;
1453			}
1454			if (tty->driver->flush_chars)
1455				tty->driver->flush_chars(tty);
1456		} else {
1457			while (nr > 0) {
1458				c = tty->driver->write(tty, b, nr);
1459				if (c < 0) {
1460					retval = c;
1461					goto break_out;
1462				}
1463				if (!c)
1464					break;
1465				b += c;
1466				nr -= c;
1467			}
1468		}
1469		if (!nr)
1470			break;
1471		if (file->f_flags & O_NONBLOCK) {
1472			retval = -EAGAIN;
1473			break;
1474		}
1475		schedule();
1476	}
1477break_out:
1478	__set_current_state(TASK_RUNNING);
1479	remove_wait_queue(&tty->write_wait, &wait);
1480	return (b - buf) ? b - buf : retval;
1481}
1482
1483
1484static unsigned int normal_poll(struct tty_struct * tty, struct file * file, poll_table *wait)
1485{
1486	unsigned int mask = 0;
1487
1488	poll_wait(file, &tty->read_wait, wait);
1489	poll_wait(file, &tty->write_wait, wait);
1490	if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
1491		mask |= POLLIN | POLLRDNORM;
1492	if (tty->packet && tty->link->ctrl_status)
1493		mask |= POLLPRI | POLLIN | POLLRDNORM;
1494	if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
1495		mask |= POLLHUP;
1496	if (tty_hung_up_p(file))
1497		mask |= POLLHUP;
1498	if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
1499		if (MIN_CHAR(tty) && !TIME_CHAR(tty))
1500			tty->minimum_to_wake = MIN_CHAR(tty);
1501		else
1502			tty->minimum_to_wake = 1;
1503	}
1504	if (tty->driver->chars_in_buffer(tty) < WAKEUP_CHARS &&
1505			tty->driver->write_room(tty) > 0)
1506		mask |= POLLOUT | POLLWRNORM;
1507	return mask;
1508}
1509
1510struct tty_ldisc tty_ldisc_N_TTY = {
1511	.magic           = TTY_LDISC_MAGIC,
1512	.name            = "n_tty",
1513	.open            = n_tty_open,
1514	.close           = n_tty_close,
1515	.flush_buffer    = n_tty_flush_buffer,
1516	.chars_in_buffer = n_tty_chars_in_buffer,
1517	.read            = read_chan,
1518	.write           = write_chan,
1519	.ioctl           = n_tty_ioctl,
1520	.set_termios     = n_tty_set_termios,
1521	.poll            = normal_poll,
1522	.receive_buf     = n_tty_receive_buf,
1523	.write_wakeup    = n_tty_write_wakeup
1524};
1525