xref: /openbmc/linux/drivers/tty/n_tty.c (revision bc000245)
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 n_tty_write 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 #include <linux/audit.h>
49 #include <linux/file.h>
50 #include <linux/uaccess.h>
51 #include <linux/module.h>
52 #include <linux/ratelimit.h>
53 #include <linux/vmalloc.h>
54 
55 
56 /* number of characters left in xmit buffer before select has we have room */
57 #define WAKEUP_CHARS 256
58 
59 /*
60  * This defines the low- and high-watermarks for throttling and
61  * unthrottling the TTY driver.  These watermarks are used for
62  * controlling the space in the read buffer.
63  */
64 #define TTY_THRESHOLD_THROTTLE		128 /* now based on remaining room */
65 #define TTY_THRESHOLD_UNTHROTTLE	128
66 
67 /*
68  * Special byte codes used in the echo buffer to represent operations
69  * or special handling of characters.  Bytes in the echo buffer that
70  * are not part of such special blocks are treated as normal character
71  * codes.
72  */
73 #define ECHO_OP_START 0xff
74 #define ECHO_OP_MOVE_BACK_COL 0x80
75 #define ECHO_OP_SET_CANON_COL 0x81
76 #define ECHO_OP_ERASE_TAB 0x82
77 
78 #define ECHO_COMMIT_WATERMARK	256
79 #define ECHO_BLOCK		256
80 #define ECHO_DISCARD_WATERMARK	N_TTY_BUF_SIZE - (ECHO_BLOCK + 32)
81 
82 
83 #undef N_TTY_TRACE
84 #ifdef N_TTY_TRACE
85 # define n_tty_trace(f, args...)	trace_printk(f, ##args)
86 #else
87 # define n_tty_trace(f, args...)
88 #endif
89 
90 struct n_tty_data {
91 	/* producer-published */
92 	size_t read_head;
93 	size_t canon_head;
94 	size_t echo_head;
95 	size_t echo_commit;
96 	DECLARE_BITMAP(char_map, 256);
97 
98 	/* private to n_tty_receive_overrun (single-threaded) */
99 	unsigned long overrun_time;
100 	int num_overrun;
101 
102 	/* non-atomic */
103 	bool no_room;
104 
105 	/* must hold exclusive termios_rwsem to reset these */
106 	unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
107 
108 	/* shared by producer and consumer */
109 	char read_buf[N_TTY_BUF_SIZE];
110 	DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
111 	unsigned char echo_buf[N_TTY_BUF_SIZE];
112 
113 	int minimum_to_wake;
114 
115 	/* consumer-published */
116 	size_t read_tail;
117 	size_t line_start;
118 
119 	/* protected by output lock */
120 	unsigned int column;
121 	unsigned int canon_column;
122 	size_t echo_tail;
123 
124 	struct mutex atomic_read_lock;
125 	struct mutex output_lock;
126 };
127 
128 static inline size_t read_cnt(struct n_tty_data *ldata)
129 {
130 	return ldata->read_head - ldata->read_tail;
131 }
132 
133 static inline unsigned char read_buf(struct n_tty_data *ldata, size_t i)
134 {
135 	return ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
136 }
137 
138 static inline unsigned char *read_buf_addr(struct n_tty_data *ldata, size_t i)
139 {
140 	return &ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
141 }
142 
143 static inline unsigned char echo_buf(struct n_tty_data *ldata, size_t i)
144 {
145 	return ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
146 }
147 
148 static inline unsigned char *echo_buf_addr(struct n_tty_data *ldata, size_t i)
149 {
150 	return &ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
151 }
152 
153 static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
154 			       unsigned char __user *ptr)
155 {
156 	struct n_tty_data *ldata = tty->disc_data;
157 
158 	tty_audit_add_data(tty, &x, 1, ldata->icanon);
159 	return put_user(x, ptr);
160 }
161 
162 static int receive_room(struct tty_struct *tty)
163 {
164 	struct n_tty_data *ldata = tty->disc_data;
165 	int left;
166 
167 	if (I_PARMRK(tty)) {
168 		/* Multiply read_cnt by 3, since each byte might take up to
169 		 * three times as many spaces when PARMRK is set (depending on
170 		 * its flags, e.g. parity error). */
171 		left = N_TTY_BUF_SIZE - read_cnt(ldata) * 3 - 1;
172 	} else
173 		left = N_TTY_BUF_SIZE - read_cnt(ldata) - 1;
174 
175 	/*
176 	 * If we are doing input canonicalization, and there are no
177 	 * pending newlines, let characters through without limit, so
178 	 * that erase characters will be handled.  Other excess
179 	 * characters will be beeped.
180 	 */
181 	if (left <= 0)
182 		left = ldata->icanon && ldata->canon_head == ldata->read_tail;
183 
184 	return left;
185 }
186 
187 /**
188  *	n_tty_set_room	-	receive space
189  *	@tty: terminal
190  *
191  *	Re-schedules the flip buffer work if space just became available.
192  *
193  *	Caller holds exclusive termios_rwsem
194  *	   or
195  *	n_tty_read()/consumer path:
196  *		holds non-exclusive termios_rwsem
197  */
198 
199 static void n_tty_set_room(struct tty_struct *tty)
200 {
201 	struct n_tty_data *ldata = tty->disc_data;
202 
203 	/* Did this open up the receive buffer? We may need to flip */
204 	if (unlikely(ldata->no_room) && receive_room(tty)) {
205 		ldata->no_room = 0;
206 
207 		WARN_RATELIMIT(tty->port->itty == NULL,
208 				"scheduling with invalid itty\n");
209 		/* see if ldisc has been killed - if so, this means that
210 		 * even though the ldisc has been halted and ->buf.work
211 		 * cancelled, ->buf.work is about to be rescheduled
212 		 */
213 		WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
214 			       "scheduling buffer work for halted ldisc\n");
215 		queue_work(system_unbound_wq, &tty->port->buf.work);
216 	}
217 }
218 
219 static ssize_t chars_in_buffer(struct tty_struct *tty)
220 {
221 	struct n_tty_data *ldata = tty->disc_data;
222 	ssize_t n = 0;
223 
224 	if (!ldata->icanon)
225 		n = read_cnt(ldata);
226 	else
227 		n = ldata->canon_head - ldata->read_tail;
228 	return n;
229 }
230 
231 /**
232  *	n_tty_write_wakeup	-	asynchronous I/O notifier
233  *	@tty: tty device
234  *
235  *	Required for the ptys, serial driver etc. since processes
236  *	that attach themselves to the master and rely on ASYNC
237  *	IO must be woken up
238  */
239 
240 static void n_tty_write_wakeup(struct tty_struct *tty)
241 {
242 	if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
243 		kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
244 }
245 
246 static void n_tty_check_throttle(struct tty_struct *tty)
247 {
248 	if (tty->driver->type == TTY_DRIVER_TYPE_PTY)
249 		return;
250 	/*
251 	 * Check the remaining room for the input canonicalization
252 	 * mode.  We don't want to throttle the driver if we're in
253 	 * canonical mode and don't have a newline yet!
254 	 */
255 	while (1) {
256 		int throttled;
257 		tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
258 		if (receive_room(tty) >= TTY_THRESHOLD_THROTTLE)
259 			break;
260 		throttled = tty_throttle_safe(tty);
261 		if (!throttled)
262 			break;
263 	}
264 	__tty_set_flow_change(tty, 0);
265 }
266 
267 static void n_tty_check_unthrottle(struct tty_struct *tty)
268 {
269 	if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
270 	    tty->link->ldisc->ops->write_wakeup == n_tty_write_wakeup) {
271 		if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
272 			return;
273 		if (!tty->count)
274 			return;
275 		n_tty_set_room(tty);
276 		n_tty_write_wakeup(tty->link);
277 		if (waitqueue_active(&tty->link->write_wait))
278 			wake_up_interruptible_poll(&tty->link->write_wait, POLLOUT);
279 		return;
280 	}
281 
282 	/* If there is enough space in the read buffer now, let the
283 	 * low-level driver know. We use chars_in_buffer() to
284 	 * check the buffer, as it now knows about canonical mode.
285 	 * Otherwise, if the driver is throttled and the line is
286 	 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
287 	 * we won't get any more characters.
288 	 */
289 
290 	while (1) {
291 		int unthrottled;
292 		tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
293 		if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
294 			break;
295 		if (!tty->count)
296 			break;
297 		n_tty_set_room(tty);
298 		unthrottled = tty_unthrottle_safe(tty);
299 		if (!unthrottled)
300 			break;
301 	}
302 	__tty_set_flow_change(tty, 0);
303 }
304 
305 /**
306  *	put_tty_queue		-	add character to tty
307  *	@c: character
308  *	@ldata: n_tty data
309  *
310  *	Add a character to the tty read_buf queue.
311  *
312  *	n_tty_receive_buf()/producer path:
313  *		caller holds non-exclusive termios_rwsem
314  *		modifies read_head
315  *
316  *	read_head is only considered 'published' if canonical mode is
317  *	not active.
318  */
319 
320 static inline void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
321 {
322 	*read_buf_addr(ldata, ldata->read_head++) = c;
323 }
324 
325 /**
326  *	reset_buffer_flags	-	reset buffer state
327  *	@tty: terminal to reset
328  *
329  *	Reset the read buffer counters and clear the flags.
330  *	Called from n_tty_open() and n_tty_flush_buffer().
331  *
332  *	Locking: caller holds exclusive termios_rwsem
333  *		 (or locking is not required)
334  */
335 
336 static void reset_buffer_flags(struct n_tty_data *ldata)
337 {
338 	ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
339 	ldata->echo_head = ldata->echo_tail = ldata->echo_commit = 0;
340 	ldata->line_start = 0;
341 
342 	ldata->erasing = 0;
343 	bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
344 }
345 
346 static void n_tty_packet_mode_flush(struct tty_struct *tty)
347 {
348 	unsigned long flags;
349 
350 	spin_lock_irqsave(&tty->ctrl_lock, flags);
351 	if (tty->link->packet) {
352 		tty->ctrl_status |= TIOCPKT_FLUSHREAD;
353 		if (waitqueue_active(&tty->link->read_wait))
354 			wake_up_interruptible(&tty->link->read_wait);
355 	}
356 	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
357 }
358 
359 /**
360  *	n_tty_flush_buffer	-	clean input queue
361  *	@tty:	terminal device
362  *
363  *	Flush the input buffer. Called when the tty layer wants the
364  *	buffer flushed (eg at hangup) or when the N_TTY line discipline
365  *	internally has to clean the pending queue (for example some signals).
366  *
367  *	Holds termios_rwsem to exclude producer/consumer while
368  *	buffer indices are reset.
369  *
370  *	Locking: ctrl_lock, exclusive termios_rwsem
371  */
372 
373 static void n_tty_flush_buffer(struct tty_struct *tty)
374 {
375 	down_write(&tty->termios_rwsem);
376 	reset_buffer_flags(tty->disc_data);
377 	n_tty_set_room(tty);
378 
379 	if (tty->link)
380 		n_tty_packet_mode_flush(tty);
381 	up_write(&tty->termios_rwsem);
382 }
383 
384 /**
385  *	n_tty_chars_in_buffer	-	report available bytes
386  *	@tty: tty device
387  *
388  *	Report the number of characters buffered to be delivered to user
389  *	at this instant in time.
390  *
391  *	Locking: exclusive termios_rwsem
392  */
393 
394 static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
395 {
396 	ssize_t n;
397 
398 	WARN_ONCE(1, "%s is deprecated and scheduled for removal.", __func__);
399 
400 	down_write(&tty->termios_rwsem);
401 	n = chars_in_buffer(tty);
402 	up_write(&tty->termios_rwsem);
403 	return n;
404 }
405 
406 /**
407  *	is_utf8_continuation	-	utf8 multibyte check
408  *	@c: byte to check
409  *
410  *	Returns true if the utf8 character 'c' is a multibyte continuation
411  *	character. We use this to correctly compute the on screen size
412  *	of the character when printing
413  */
414 
415 static inline int is_utf8_continuation(unsigned char c)
416 {
417 	return (c & 0xc0) == 0x80;
418 }
419 
420 /**
421  *	is_continuation		-	multibyte check
422  *	@c: byte to check
423  *
424  *	Returns true if the utf8 character 'c' is a multibyte continuation
425  *	character and the terminal is in unicode mode.
426  */
427 
428 static inline int is_continuation(unsigned char c, struct tty_struct *tty)
429 {
430 	return I_IUTF8(tty) && is_utf8_continuation(c);
431 }
432 
433 /**
434  *	do_output_char			-	output one character
435  *	@c: character (or partial unicode symbol)
436  *	@tty: terminal device
437  *	@space: space available in tty driver write buffer
438  *
439  *	This is a helper function that handles one output character
440  *	(including special characters like TAB, CR, LF, etc.),
441  *	doing OPOST processing and putting the results in the
442  *	tty driver's write buffer.
443  *
444  *	Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
445  *	and NLDLY.  They simply aren't relevant in the world today.
446  *	If you ever need them, add them here.
447  *
448  *	Returns the number of bytes of buffer space used or -1 if
449  *	no space left.
450  *
451  *	Locking: should be called under the output_lock to protect
452  *		 the column state and space left in the buffer
453  */
454 
455 static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
456 {
457 	struct n_tty_data *ldata = tty->disc_data;
458 	int	spaces;
459 
460 	if (!space)
461 		return -1;
462 
463 	switch (c) {
464 	case '\n':
465 		if (O_ONLRET(tty))
466 			ldata->column = 0;
467 		if (O_ONLCR(tty)) {
468 			if (space < 2)
469 				return -1;
470 			ldata->canon_column = ldata->column = 0;
471 			tty->ops->write(tty, "\r\n", 2);
472 			return 2;
473 		}
474 		ldata->canon_column = ldata->column;
475 		break;
476 	case '\r':
477 		if (O_ONOCR(tty) && ldata->column == 0)
478 			return 0;
479 		if (O_OCRNL(tty)) {
480 			c = '\n';
481 			if (O_ONLRET(tty))
482 				ldata->canon_column = ldata->column = 0;
483 			break;
484 		}
485 		ldata->canon_column = ldata->column = 0;
486 		break;
487 	case '\t':
488 		spaces = 8 - (ldata->column & 7);
489 		if (O_TABDLY(tty) == XTABS) {
490 			if (space < spaces)
491 				return -1;
492 			ldata->column += spaces;
493 			tty->ops->write(tty, "        ", spaces);
494 			return spaces;
495 		}
496 		ldata->column += spaces;
497 		break;
498 	case '\b':
499 		if (ldata->column > 0)
500 			ldata->column--;
501 		break;
502 	default:
503 		if (!iscntrl(c)) {
504 			if (O_OLCUC(tty))
505 				c = toupper(c);
506 			if (!is_continuation(c, tty))
507 				ldata->column++;
508 		}
509 		break;
510 	}
511 
512 	tty_put_char(tty, c);
513 	return 1;
514 }
515 
516 /**
517  *	process_output			-	output post processor
518  *	@c: character (or partial unicode symbol)
519  *	@tty: terminal device
520  *
521  *	Output one character with OPOST processing.
522  *	Returns -1 when the output device is full and the character
523  *	must be retried.
524  *
525  *	Locking: output_lock to protect column state and space left
526  *		 (also, this is called from n_tty_write under the
527  *		  tty layer write lock)
528  */
529 
530 static int process_output(unsigned char c, struct tty_struct *tty)
531 {
532 	struct n_tty_data *ldata = tty->disc_data;
533 	int	space, retval;
534 
535 	mutex_lock(&ldata->output_lock);
536 
537 	space = tty_write_room(tty);
538 	retval = do_output_char(c, tty, space);
539 
540 	mutex_unlock(&ldata->output_lock);
541 	if (retval < 0)
542 		return -1;
543 	else
544 		return 0;
545 }
546 
547 /**
548  *	process_output_block		-	block post processor
549  *	@tty: terminal device
550  *	@buf: character buffer
551  *	@nr: number of bytes to output
552  *
553  *	Output a block of characters with OPOST processing.
554  *	Returns the number of characters output.
555  *
556  *	This path is used to speed up block console writes, among other
557  *	things when processing blocks of output data. It handles only
558  *	the simple cases normally found and helps to generate blocks of
559  *	symbols for the console driver and thus improve performance.
560  *
561  *	Locking: output_lock to protect column state and space left
562  *		 (also, this is called from n_tty_write under the
563  *		  tty layer write lock)
564  */
565 
566 static ssize_t process_output_block(struct tty_struct *tty,
567 				    const unsigned char *buf, unsigned int nr)
568 {
569 	struct n_tty_data *ldata = tty->disc_data;
570 	int	space;
571 	int	i;
572 	const unsigned char *cp;
573 
574 	mutex_lock(&ldata->output_lock);
575 
576 	space = tty_write_room(tty);
577 	if (!space) {
578 		mutex_unlock(&ldata->output_lock);
579 		return 0;
580 	}
581 	if (nr > space)
582 		nr = space;
583 
584 	for (i = 0, cp = buf; i < nr; i++, cp++) {
585 		unsigned char c = *cp;
586 
587 		switch (c) {
588 		case '\n':
589 			if (O_ONLRET(tty))
590 				ldata->column = 0;
591 			if (O_ONLCR(tty))
592 				goto break_out;
593 			ldata->canon_column = ldata->column;
594 			break;
595 		case '\r':
596 			if (O_ONOCR(tty) && ldata->column == 0)
597 				goto break_out;
598 			if (O_OCRNL(tty))
599 				goto break_out;
600 			ldata->canon_column = ldata->column = 0;
601 			break;
602 		case '\t':
603 			goto break_out;
604 		case '\b':
605 			if (ldata->column > 0)
606 				ldata->column--;
607 			break;
608 		default:
609 			if (!iscntrl(c)) {
610 				if (O_OLCUC(tty))
611 					goto break_out;
612 				if (!is_continuation(c, tty))
613 					ldata->column++;
614 			}
615 			break;
616 		}
617 	}
618 break_out:
619 	i = tty->ops->write(tty, buf, i);
620 
621 	mutex_unlock(&ldata->output_lock);
622 	return i;
623 }
624 
625 /**
626  *	process_echoes	-	write pending echo characters
627  *	@tty: terminal device
628  *
629  *	Write previously buffered echo (and other ldisc-generated)
630  *	characters to the tty.
631  *
632  *	Characters generated by the ldisc (including echoes) need to
633  *	be buffered because the driver's write buffer can fill during
634  *	heavy program output.  Echoing straight to the driver will
635  *	often fail under these conditions, causing lost characters and
636  *	resulting mismatches of ldisc state information.
637  *
638  *	Since the ldisc state must represent the characters actually sent
639  *	to the driver at the time of the write, operations like certain
640  *	changes in column state are also saved in the buffer and executed
641  *	here.
642  *
643  *	A circular fifo buffer is used so that the most recent characters
644  *	are prioritized.  Also, when control characters are echoed with a
645  *	prefixed "^", the pair is treated atomically and thus not separated.
646  *
647  *	Locking: callers must hold output_lock
648  */
649 
650 static size_t __process_echoes(struct tty_struct *tty)
651 {
652 	struct n_tty_data *ldata = tty->disc_data;
653 	int	space, old_space;
654 	size_t tail;
655 	unsigned char c;
656 
657 	old_space = space = tty_write_room(tty);
658 
659 	tail = ldata->echo_tail;
660 	while (ldata->echo_commit != tail) {
661 		c = echo_buf(ldata, tail);
662 		if (c == ECHO_OP_START) {
663 			unsigned char op;
664 			int no_space_left = 0;
665 
666 			/*
667 			 * If the buffer byte is the start of a multi-byte
668 			 * operation, get the next byte, which is either the
669 			 * op code or a control character value.
670 			 */
671 			op = echo_buf(ldata, tail + 1);
672 
673 			switch (op) {
674 				unsigned int num_chars, num_bs;
675 
676 			case ECHO_OP_ERASE_TAB:
677 				num_chars = echo_buf(ldata, tail + 2);
678 
679 				/*
680 				 * Determine how many columns to go back
681 				 * in order to erase the tab.
682 				 * This depends on the number of columns
683 				 * used by other characters within the tab
684 				 * area.  If this (modulo 8) count is from
685 				 * the start of input rather than from a
686 				 * previous tab, we offset by canon column.
687 				 * Otherwise, tab spacing is normal.
688 				 */
689 				if (!(num_chars & 0x80))
690 					num_chars += ldata->canon_column;
691 				num_bs = 8 - (num_chars & 7);
692 
693 				if (num_bs > space) {
694 					no_space_left = 1;
695 					break;
696 				}
697 				space -= num_bs;
698 				while (num_bs--) {
699 					tty_put_char(tty, '\b');
700 					if (ldata->column > 0)
701 						ldata->column--;
702 				}
703 				tail += 3;
704 				break;
705 
706 			case ECHO_OP_SET_CANON_COL:
707 				ldata->canon_column = ldata->column;
708 				tail += 2;
709 				break;
710 
711 			case ECHO_OP_MOVE_BACK_COL:
712 				if (ldata->column > 0)
713 					ldata->column--;
714 				tail += 2;
715 				break;
716 
717 			case ECHO_OP_START:
718 				/* This is an escaped echo op start code */
719 				if (!space) {
720 					no_space_left = 1;
721 					break;
722 				}
723 				tty_put_char(tty, ECHO_OP_START);
724 				ldata->column++;
725 				space--;
726 				tail += 2;
727 				break;
728 
729 			default:
730 				/*
731 				 * If the op is not a special byte code,
732 				 * it is a ctrl char tagged to be echoed
733 				 * as "^X" (where X is the letter
734 				 * representing the control char).
735 				 * Note that we must ensure there is
736 				 * enough space for the whole ctrl pair.
737 				 *
738 				 */
739 				if (space < 2) {
740 					no_space_left = 1;
741 					break;
742 				}
743 				tty_put_char(tty, '^');
744 				tty_put_char(tty, op ^ 0100);
745 				ldata->column += 2;
746 				space -= 2;
747 				tail += 2;
748 			}
749 
750 			if (no_space_left)
751 				break;
752 		} else {
753 			if (O_OPOST(tty)) {
754 				int retval = do_output_char(c, tty, space);
755 				if (retval < 0)
756 					break;
757 				space -= retval;
758 			} else {
759 				if (!space)
760 					break;
761 				tty_put_char(tty, c);
762 				space -= 1;
763 			}
764 			tail += 1;
765 		}
766 	}
767 
768 	/* If the echo buffer is nearly full (so that the possibility exists
769 	 * of echo overrun before the next commit), then discard enough
770 	 * data at the tail to prevent a subsequent overrun */
771 	while (ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) {
772 		if (echo_buf(ldata, tail) == ECHO_OP_START) {
773 			if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB)
774 				tail += 3;
775 			else
776 				tail += 2;
777 		} else
778 			tail++;
779 	}
780 
781 	ldata->echo_tail = tail;
782 	return old_space - space;
783 }
784 
785 static void commit_echoes(struct tty_struct *tty)
786 {
787 	struct n_tty_data *ldata = tty->disc_data;
788 	size_t nr, old, echoed;
789 	size_t head;
790 
791 	head = ldata->echo_head;
792 	old = ldata->echo_commit - ldata->echo_tail;
793 
794 	/* Process committed echoes if the accumulated # of bytes
795 	 * is over the threshold (and try again each time another
796 	 * block is accumulated) */
797 	nr = head - ldata->echo_tail;
798 	if (nr < ECHO_COMMIT_WATERMARK || (nr % ECHO_BLOCK > old % ECHO_BLOCK))
799 		return;
800 
801 	mutex_lock(&ldata->output_lock);
802 	ldata->echo_commit = head;
803 	echoed = __process_echoes(tty);
804 	mutex_unlock(&ldata->output_lock);
805 
806 	if (echoed && tty->ops->flush_chars)
807 		tty->ops->flush_chars(tty);
808 }
809 
810 static void process_echoes(struct tty_struct *tty)
811 {
812 	struct n_tty_data *ldata = tty->disc_data;
813 	size_t echoed;
814 
815 	if ((!L_ECHO(tty) && !L_ECHONL(tty)) ||
816 	    ldata->echo_commit == ldata->echo_tail)
817 		return;
818 
819 	mutex_lock(&ldata->output_lock);
820 	echoed = __process_echoes(tty);
821 	mutex_unlock(&ldata->output_lock);
822 
823 	if (echoed && tty->ops->flush_chars)
824 		tty->ops->flush_chars(tty);
825 }
826 
827 static void flush_echoes(struct tty_struct *tty)
828 {
829 	struct n_tty_data *ldata = tty->disc_data;
830 
831 	if ((!L_ECHO(tty) && !L_ECHONL(tty)) ||
832 	    ldata->echo_commit == ldata->echo_head)
833 		return;
834 
835 	mutex_lock(&ldata->output_lock);
836 	ldata->echo_commit = ldata->echo_head;
837 	__process_echoes(tty);
838 	mutex_unlock(&ldata->output_lock);
839 }
840 
841 /**
842  *	add_echo_byte	-	add a byte to the echo buffer
843  *	@c: unicode byte to echo
844  *	@ldata: n_tty data
845  *
846  *	Add a character or operation byte to the echo buffer.
847  */
848 
849 static inline void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
850 {
851 	*echo_buf_addr(ldata, ldata->echo_head++) = c;
852 }
853 
854 /**
855  *	echo_move_back_col	-	add operation to move back a column
856  *	@ldata: n_tty data
857  *
858  *	Add an operation to the echo buffer to move back one column.
859  */
860 
861 static void echo_move_back_col(struct n_tty_data *ldata)
862 {
863 	add_echo_byte(ECHO_OP_START, ldata);
864 	add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
865 }
866 
867 /**
868  *	echo_set_canon_col	-	add operation to set the canon column
869  *	@ldata: n_tty data
870  *
871  *	Add an operation to the echo buffer to set the canon column
872  *	to the current column.
873  */
874 
875 static void echo_set_canon_col(struct n_tty_data *ldata)
876 {
877 	add_echo_byte(ECHO_OP_START, ldata);
878 	add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
879 }
880 
881 /**
882  *	echo_erase_tab	-	add operation to erase a tab
883  *	@num_chars: number of character columns already used
884  *	@after_tab: true if num_chars starts after a previous tab
885  *	@ldata: n_tty data
886  *
887  *	Add an operation to the echo buffer to erase a tab.
888  *
889  *	Called by the eraser function, which knows how many character
890  *	columns have been used since either a previous tab or the start
891  *	of input.  This information will be used later, along with
892  *	canon column (if applicable), to go back the correct number
893  *	of columns.
894  */
895 
896 static void echo_erase_tab(unsigned int num_chars, int after_tab,
897 			   struct n_tty_data *ldata)
898 {
899 	add_echo_byte(ECHO_OP_START, ldata);
900 	add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
901 
902 	/* We only need to know this modulo 8 (tab spacing) */
903 	num_chars &= 7;
904 
905 	/* Set the high bit as a flag if num_chars is after a previous tab */
906 	if (after_tab)
907 		num_chars |= 0x80;
908 
909 	add_echo_byte(num_chars, ldata);
910 }
911 
912 /**
913  *	echo_char_raw	-	echo a character raw
914  *	@c: unicode byte to echo
915  *	@tty: terminal device
916  *
917  *	Echo user input back onto the screen. This must be called only when
918  *	L_ECHO(tty) is true. Called from the driver receive_buf path.
919  *
920  *	This variant does not treat control characters specially.
921  */
922 
923 static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
924 {
925 	if (c == ECHO_OP_START) {
926 		add_echo_byte(ECHO_OP_START, ldata);
927 		add_echo_byte(ECHO_OP_START, ldata);
928 	} else {
929 		add_echo_byte(c, ldata);
930 	}
931 }
932 
933 /**
934  *	echo_char	-	echo a character
935  *	@c: unicode byte to echo
936  *	@tty: terminal device
937  *
938  *	Echo user input back onto the screen. This must be called only when
939  *	L_ECHO(tty) is true. Called from the driver receive_buf path.
940  *
941  *	This variant tags control characters to be echoed as "^X"
942  *	(where X is the letter representing the control char).
943  */
944 
945 static void echo_char(unsigned char c, struct tty_struct *tty)
946 {
947 	struct n_tty_data *ldata = tty->disc_data;
948 
949 	if (c == ECHO_OP_START) {
950 		add_echo_byte(ECHO_OP_START, ldata);
951 		add_echo_byte(ECHO_OP_START, ldata);
952 	} else {
953 		if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
954 			add_echo_byte(ECHO_OP_START, ldata);
955 		add_echo_byte(c, ldata);
956 	}
957 }
958 
959 /**
960  *	finish_erasing		-	complete erase
961  *	@ldata: n_tty data
962  */
963 
964 static inline void finish_erasing(struct n_tty_data *ldata)
965 {
966 	if (ldata->erasing) {
967 		echo_char_raw('/', ldata);
968 		ldata->erasing = 0;
969 	}
970 }
971 
972 /**
973  *	eraser		-	handle erase function
974  *	@c: character input
975  *	@tty: terminal device
976  *
977  *	Perform erase and necessary output when an erase character is
978  *	present in the stream from the driver layer. Handles the complexities
979  *	of UTF-8 multibyte symbols.
980  *
981  *	n_tty_receive_buf()/producer path:
982  *		caller holds non-exclusive termios_rwsem
983  *		modifies read_head
984  *
985  *	Modifying the read_head is not considered a publish in this context
986  *	because canonical mode is active -- only canon_head publishes
987  */
988 
989 static void eraser(unsigned char c, struct tty_struct *tty)
990 {
991 	struct n_tty_data *ldata = tty->disc_data;
992 	enum { ERASE, WERASE, KILL } kill_type;
993 	size_t head;
994 	size_t cnt;
995 	int seen_alnums;
996 
997 	if (ldata->read_head == ldata->canon_head) {
998 		/* process_output('\a', tty); */ /* what do you think? */
999 		return;
1000 	}
1001 	if (c == ERASE_CHAR(tty))
1002 		kill_type = ERASE;
1003 	else if (c == WERASE_CHAR(tty))
1004 		kill_type = WERASE;
1005 	else {
1006 		if (!L_ECHO(tty)) {
1007 			ldata->read_head = ldata->canon_head;
1008 			return;
1009 		}
1010 		if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
1011 			ldata->read_head = ldata->canon_head;
1012 			finish_erasing(ldata);
1013 			echo_char(KILL_CHAR(tty), tty);
1014 			/* Add a newline if ECHOK is on and ECHOKE is off. */
1015 			if (L_ECHOK(tty))
1016 				echo_char_raw('\n', ldata);
1017 			return;
1018 		}
1019 		kill_type = KILL;
1020 	}
1021 
1022 	seen_alnums = 0;
1023 	while (ldata->read_head != ldata->canon_head) {
1024 		head = ldata->read_head;
1025 
1026 		/* erase a single possibly multibyte character */
1027 		do {
1028 			head--;
1029 			c = read_buf(ldata, head);
1030 		} while (is_continuation(c, tty) && head != ldata->canon_head);
1031 
1032 		/* do not partially erase */
1033 		if (is_continuation(c, tty))
1034 			break;
1035 
1036 		if (kill_type == WERASE) {
1037 			/* Equivalent to BSD's ALTWERASE. */
1038 			if (isalnum(c) || c == '_')
1039 				seen_alnums++;
1040 			else if (seen_alnums)
1041 				break;
1042 		}
1043 		cnt = ldata->read_head - head;
1044 		ldata->read_head = head;
1045 		if (L_ECHO(tty)) {
1046 			if (L_ECHOPRT(tty)) {
1047 				if (!ldata->erasing) {
1048 					echo_char_raw('\\', ldata);
1049 					ldata->erasing = 1;
1050 				}
1051 				/* if cnt > 1, output a multi-byte character */
1052 				echo_char(c, tty);
1053 				while (--cnt > 0) {
1054 					head++;
1055 					echo_char_raw(read_buf(ldata, head), ldata);
1056 					echo_move_back_col(ldata);
1057 				}
1058 			} else if (kill_type == ERASE && !L_ECHOE(tty)) {
1059 				echo_char(ERASE_CHAR(tty), tty);
1060 			} else if (c == '\t') {
1061 				unsigned int num_chars = 0;
1062 				int after_tab = 0;
1063 				size_t tail = ldata->read_head;
1064 
1065 				/*
1066 				 * Count the columns used for characters
1067 				 * since the start of input or after a
1068 				 * previous tab.
1069 				 * This info is used to go back the correct
1070 				 * number of columns.
1071 				 */
1072 				while (tail != ldata->canon_head) {
1073 					tail--;
1074 					c = read_buf(ldata, tail);
1075 					if (c == '\t') {
1076 						after_tab = 1;
1077 						break;
1078 					} else if (iscntrl(c)) {
1079 						if (L_ECHOCTL(tty))
1080 							num_chars += 2;
1081 					} else if (!is_continuation(c, tty)) {
1082 						num_chars++;
1083 					}
1084 				}
1085 				echo_erase_tab(num_chars, after_tab, ldata);
1086 			} else {
1087 				if (iscntrl(c) && L_ECHOCTL(tty)) {
1088 					echo_char_raw('\b', ldata);
1089 					echo_char_raw(' ', ldata);
1090 					echo_char_raw('\b', ldata);
1091 				}
1092 				if (!iscntrl(c) || L_ECHOCTL(tty)) {
1093 					echo_char_raw('\b', ldata);
1094 					echo_char_raw(' ', ldata);
1095 					echo_char_raw('\b', ldata);
1096 				}
1097 			}
1098 		}
1099 		if (kill_type == ERASE)
1100 			break;
1101 	}
1102 	if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
1103 		finish_erasing(ldata);
1104 }
1105 
1106 /**
1107  *	isig		-	handle the ISIG optio
1108  *	@sig: signal
1109  *	@tty: terminal
1110  *
1111  *	Called when a signal is being sent due to terminal input.
1112  *	Called from the driver receive_buf path so serialized.
1113  *
1114  *	Locking: ctrl_lock
1115  */
1116 
1117 static void isig(int sig, struct tty_struct *tty)
1118 {
1119 	struct pid *tty_pgrp = tty_get_pgrp(tty);
1120 	if (tty_pgrp) {
1121 		kill_pgrp(tty_pgrp, sig, 1);
1122 		put_pid(tty_pgrp);
1123 	}
1124 }
1125 
1126 /**
1127  *	n_tty_receive_break	-	handle break
1128  *	@tty: terminal
1129  *
1130  *	An RS232 break event has been hit in the incoming bitstream. This
1131  *	can cause a variety of events depending upon the termios settings.
1132  *
1133  *	n_tty_receive_buf()/producer path:
1134  *		caller holds non-exclusive termios_rwsem
1135  *		publishes read_head via put_tty_queue()
1136  *
1137  *	Note: may get exclusive termios_rwsem if flushing input buffer
1138  */
1139 
1140 static void n_tty_receive_break(struct tty_struct *tty)
1141 {
1142 	struct n_tty_data *ldata = tty->disc_data;
1143 
1144 	if (I_IGNBRK(tty))
1145 		return;
1146 	if (I_BRKINT(tty)) {
1147 		isig(SIGINT, tty);
1148 		if (!L_NOFLSH(tty)) {
1149 			/* flushing needs exclusive termios_rwsem */
1150 			up_read(&tty->termios_rwsem);
1151 			n_tty_flush_buffer(tty);
1152 			tty_driver_flush_buffer(tty);
1153 			down_read(&tty->termios_rwsem);
1154 		}
1155 		return;
1156 	}
1157 	if (I_PARMRK(tty)) {
1158 		put_tty_queue('\377', ldata);
1159 		put_tty_queue('\0', ldata);
1160 	}
1161 	put_tty_queue('\0', ldata);
1162 	if (waitqueue_active(&tty->read_wait))
1163 		wake_up_interruptible(&tty->read_wait);
1164 }
1165 
1166 /**
1167  *	n_tty_receive_overrun	-	handle overrun reporting
1168  *	@tty: terminal
1169  *
1170  *	Data arrived faster than we could process it. While the tty
1171  *	driver has flagged this the bits that were missed are gone
1172  *	forever.
1173  *
1174  *	Called from the receive_buf path so single threaded. Does not
1175  *	need locking as num_overrun and overrun_time are function
1176  *	private.
1177  */
1178 
1179 static void n_tty_receive_overrun(struct tty_struct *tty)
1180 {
1181 	struct n_tty_data *ldata = tty->disc_data;
1182 	char buf[64];
1183 
1184 	ldata->num_overrun++;
1185 	if (time_after(jiffies, ldata->overrun_time + HZ) ||
1186 			time_after(ldata->overrun_time, jiffies)) {
1187 		printk(KERN_WARNING "%s: %d input overrun(s)\n",
1188 			tty_name(tty, buf),
1189 			ldata->num_overrun);
1190 		ldata->overrun_time = jiffies;
1191 		ldata->num_overrun = 0;
1192 	}
1193 }
1194 
1195 /**
1196  *	n_tty_receive_parity_error	-	error notifier
1197  *	@tty: terminal device
1198  *	@c: character
1199  *
1200  *	Process a parity error and queue the right data to indicate
1201  *	the error case if necessary.
1202  *
1203  *	n_tty_receive_buf()/producer path:
1204  *		caller holds non-exclusive termios_rwsem
1205  *		publishes read_head via put_tty_queue()
1206  */
1207 static void n_tty_receive_parity_error(struct tty_struct *tty, unsigned char c)
1208 {
1209 	struct n_tty_data *ldata = tty->disc_data;
1210 
1211 	if (I_IGNPAR(tty))
1212 		return;
1213 	if (I_PARMRK(tty)) {
1214 		put_tty_queue('\377', ldata);
1215 		put_tty_queue('\0', ldata);
1216 		put_tty_queue(c, ldata);
1217 	} else	if (I_INPCK(tty))
1218 		put_tty_queue('\0', ldata);
1219 	else
1220 		put_tty_queue(c, ldata);
1221 	if (waitqueue_active(&tty->read_wait))
1222 		wake_up_interruptible(&tty->read_wait);
1223 }
1224 
1225 static void
1226 n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
1227 {
1228 	if (!L_NOFLSH(tty)) {
1229 		/* flushing needs exclusive termios_rwsem */
1230 		up_read(&tty->termios_rwsem);
1231 		n_tty_flush_buffer(tty);
1232 		tty_driver_flush_buffer(tty);
1233 		down_read(&tty->termios_rwsem);
1234 	}
1235 	if (I_IXON(tty))
1236 		start_tty(tty);
1237 	if (L_ECHO(tty)) {
1238 		echo_char(c, tty);
1239 		commit_echoes(tty);
1240 	}
1241 	isig(signal, tty);
1242 	return;
1243 }
1244 
1245 /**
1246  *	n_tty_receive_char	-	perform processing
1247  *	@tty: terminal device
1248  *	@c: character
1249  *
1250  *	Process an individual character of input received from the driver.
1251  *	This is serialized with respect to itself by the rules for the
1252  *	driver above.
1253  *
1254  *	n_tty_receive_buf()/producer path:
1255  *		caller holds non-exclusive termios_rwsem
1256  *		publishes canon_head if canonical mode is active
1257  *		otherwise, publishes read_head via put_tty_queue()
1258  *
1259  *	Returns 1 if LNEXT was received, else returns 0
1260  */
1261 
1262 static int
1263 n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
1264 {
1265 	struct n_tty_data *ldata = tty->disc_data;
1266 
1267 	if (I_IXON(tty)) {
1268 		if (c == START_CHAR(tty)) {
1269 			start_tty(tty);
1270 			commit_echoes(tty);
1271 			return 0;
1272 		}
1273 		if (c == STOP_CHAR(tty)) {
1274 			stop_tty(tty);
1275 			return 0;
1276 		}
1277 	}
1278 
1279 	if (L_ISIG(tty)) {
1280 		if (c == INTR_CHAR(tty)) {
1281 			n_tty_receive_signal_char(tty, SIGINT, c);
1282 			return 0;
1283 		} else if (c == QUIT_CHAR(tty)) {
1284 			n_tty_receive_signal_char(tty, SIGQUIT, c);
1285 			return 0;
1286 		} else if (c == SUSP_CHAR(tty)) {
1287 			n_tty_receive_signal_char(tty, SIGTSTP, c);
1288 			return 0;
1289 		}
1290 	}
1291 
1292 	if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1293 		start_tty(tty);
1294 		process_echoes(tty);
1295 	}
1296 
1297 	if (c == '\r') {
1298 		if (I_IGNCR(tty))
1299 			return 0;
1300 		if (I_ICRNL(tty))
1301 			c = '\n';
1302 	} else if (c == '\n' && I_INLCR(tty))
1303 		c = '\r';
1304 
1305 	if (ldata->icanon) {
1306 		if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1307 		    (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1308 			eraser(c, tty);
1309 			commit_echoes(tty);
1310 			return 0;
1311 		}
1312 		if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
1313 			ldata->lnext = 1;
1314 			if (L_ECHO(tty)) {
1315 				finish_erasing(ldata);
1316 				if (L_ECHOCTL(tty)) {
1317 					echo_char_raw('^', ldata);
1318 					echo_char_raw('\b', ldata);
1319 					commit_echoes(tty);
1320 				}
1321 			}
1322 			return 1;
1323 		}
1324 		if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) {
1325 			size_t tail = ldata->canon_head;
1326 
1327 			finish_erasing(ldata);
1328 			echo_char(c, tty);
1329 			echo_char_raw('\n', ldata);
1330 			while (tail != ldata->read_head) {
1331 				echo_char(read_buf(ldata, tail), tty);
1332 				tail++;
1333 			}
1334 			commit_echoes(tty);
1335 			return 0;
1336 		}
1337 		if (c == '\n') {
1338 			if (L_ECHO(tty) || L_ECHONL(tty)) {
1339 				echo_char_raw('\n', ldata);
1340 				commit_echoes(tty);
1341 			}
1342 			goto handle_newline;
1343 		}
1344 		if (c == EOF_CHAR(tty)) {
1345 			c = __DISABLED_CHAR;
1346 			goto handle_newline;
1347 		}
1348 		if ((c == EOL_CHAR(tty)) ||
1349 		    (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1350 			/*
1351 			 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1352 			 */
1353 			if (L_ECHO(tty)) {
1354 				/* Record the column of first canon char. */
1355 				if (ldata->canon_head == ldata->read_head)
1356 					echo_set_canon_col(ldata);
1357 				echo_char(c, tty);
1358 				commit_echoes(tty);
1359 			}
1360 			/*
1361 			 * XXX does PARMRK doubling happen for
1362 			 * EOL_CHAR and EOL2_CHAR?
1363 			 */
1364 			if (c == (unsigned char) '\377' && I_PARMRK(tty))
1365 				put_tty_queue(c, ldata);
1366 
1367 handle_newline:
1368 			set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags);
1369 			put_tty_queue(c, ldata);
1370 			ldata->canon_head = ldata->read_head;
1371 			kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1372 			if (waitqueue_active(&tty->read_wait))
1373 				wake_up_interruptible(&tty->read_wait);
1374 			return 0;
1375 		}
1376 	}
1377 
1378 	if (L_ECHO(tty)) {
1379 		finish_erasing(ldata);
1380 		if (c == '\n')
1381 			echo_char_raw('\n', ldata);
1382 		else {
1383 			/* Record the column of first canon char. */
1384 			if (ldata->canon_head == ldata->read_head)
1385 				echo_set_canon_col(ldata);
1386 			echo_char(c, tty);
1387 		}
1388 		commit_echoes(tty);
1389 	}
1390 
1391 	/* PARMRK doubling check */
1392 	if (c == (unsigned char) '\377' && I_PARMRK(tty))
1393 		put_tty_queue(c, ldata);
1394 
1395 	put_tty_queue(c, ldata);
1396 	return 0;
1397 }
1398 
1399 static inline void
1400 n_tty_receive_char_inline(struct tty_struct *tty, unsigned char c)
1401 {
1402 	struct n_tty_data *ldata = tty->disc_data;
1403 
1404 	if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1405 		start_tty(tty);
1406 		process_echoes(tty);
1407 	}
1408 	if (L_ECHO(tty)) {
1409 		finish_erasing(ldata);
1410 		/* Record the column of first canon char. */
1411 		if (ldata->canon_head == ldata->read_head)
1412 			echo_set_canon_col(ldata);
1413 		echo_char(c, tty);
1414 		commit_echoes(tty);
1415 	}
1416 	/* PARMRK doubling check */
1417 	if (c == (unsigned char) '\377' && I_PARMRK(tty))
1418 		put_tty_queue(c, ldata);
1419 	put_tty_queue(c, ldata);
1420 }
1421 
1422 static void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1423 {
1424 	n_tty_receive_char_inline(tty, c);
1425 }
1426 
1427 static inline void
1428 n_tty_receive_char_fast(struct tty_struct *tty, unsigned char c)
1429 {
1430 	struct n_tty_data *ldata = tty->disc_data;
1431 
1432 	if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1433 		start_tty(tty);
1434 		process_echoes(tty);
1435 	}
1436 	if (L_ECHO(tty)) {
1437 		finish_erasing(ldata);
1438 		/* Record the column of first canon char. */
1439 		if (ldata->canon_head == ldata->read_head)
1440 			echo_set_canon_col(ldata);
1441 		echo_char(c, tty);
1442 		commit_echoes(tty);
1443 	}
1444 	put_tty_queue(c, ldata);
1445 }
1446 
1447 static void n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c)
1448 {
1449 	if (I_ISTRIP(tty))
1450 		c &= 0x7f;
1451 	if (I_IUCLC(tty) && L_IEXTEN(tty))
1452 		c = tolower(c);
1453 
1454 	if (I_IXON(tty)) {
1455 		if (c == STOP_CHAR(tty))
1456 			stop_tty(tty);
1457 		else if (c == START_CHAR(tty) ||
1458 			 (tty->stopped && !tty->flow_stopped && I_IXANY(tty) &&
1459 			  c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
1460 			  c != SUSP_CHAR(tty))) {
1461 			start_tty(tty);
1462 			process_echoes(tty);
1463 		}
1464 	}
1465 }
1466 
1467 static void
1468 n_tty_receive_char_flagged(struct tty_struct *tty, unsigned char c, char flag)
1469 {
1470 	char buf[64];
1471 
1472 	switch (flag) {
1473 	case TTY_BREAK:
1474 		n_tty_receive_break(tty);
1475 		break;
1476 	case TTY_PARITY:
1477 	case TTY_FRAME:
1478 		n_tty_receive_parity_error(tty, c);
1479 		break;
1480 	case TTY_OVERRUN:
1481 		n_tty_receive_overrun(tty);
1482 		break;
1483 	default:
1484 		printk(KERN_ERR "%s: unknown flag %d\n",
1485 		       tty_name(tty, buf), flag);
1486 		break;
1487 	}
1488 }
1489 
1490 static void
1491 n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
1492 {
1493 	struct n_tty_data *ldata = tty->disc_data;
1494 
1495 	ldata->lnext = 0;
1496 	if (likely(flag == TTY_NORMAL)) {
1497 		if (I_ISTRIP(tty))
1498 			c &= 0x7f;
1499 		if (I_IUCLC(tty) && L_IEXTEN(tty))
1500 			c = tolower(c);
1501 		n_tty_receive_char(tty, c);
1502 	} else
1503 		n_tty_receive_char_flagged(tty, c, flag);
1504 }
1505 
1506 /**
1507  *	n_tty_receive_buf	-	data receive
1508  *	@tty: terminal device
1509  *	@cp: buffer
1510  *	@fp: flag buffer
1511  *	@count: characters
1512  *
1513  *	Called by the terminal driver when a block of characters has
1514  *	been received. This function must be called from soft contexts
1515  *	not from interrupt context. The driver is responsible for making
1516  *	calls one at a time and in order (or using flush_to_ldisc)
1517  *
1518  *	n_tty_receive_buf()/producer path:
1519  *		claims non-exclusive termios_rwsem
1520  *		publishes read_head and canon_head
1521  */
1522 
1523 static void
1524 n_tty_receive_buf_real_raw(struct tty_struct *tty, const unsigned char *cp,
1525 			   char *fp, int count)
1526 {
1527 	struct n_tty_data *ldata = tty->disc_data;
1528 	size_t n, head;
1529 
1530 	head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
1531 	n = N_TTY_BUF_SIZE - max(read_cnt(ldata), head);
1532 	n = min_t(size_t, count, n);
1533 	memcpy(read_buf_addr(ldata, head), cp, n);
1534 	ldata->read_head += n;
1535 	cp += n;
1536 	count -= n;
1537 
1538 	head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
1539 	n = N_TTY_BUF_SIZE - max(read_cnt(ldata), head);
1540 	n = min_t(size_t, count, n);
1541 	memcpy(read_buf_addr(ldata, head), cp, n);
1542 	ldata->read_head += n;
1543 }
1544 
1545 static void
1546 n_tty_receive_buf_raw(struct tty_struct *tty, const unsigned char *cp,
1547 		      char *fp, int count)
1548 {
1549 	struct n_tty_data *ldata = tty->disc_data;
1550 	char flag = TTY_NORMAL;
1551 
1552 	while (count--) {
1553 		if (fp)
1554 			flag = *fp++;
1555 		if (likely(flag == TTY_NORMAL))
1556 			put_tty_queue(*cp++, ldata);
1557 		else
1558 			n_tty_receive_char_flagged(tty, *cp++, flag);
1559 	}
1560 }
1561 
1562 static void
1563 n_tty_receive_buf_closing(struct tty_struct *tty, const unsigned char *cp,
1564 			  char *fp, int count)
1565 {
1566 	char flag = TTY_NORMAL;
1567 
1568 	while (count--) {
1569 		if (fp)
1570 			flag = *fp++;
1571 		if (likely(flag == TTY_NORMAL))
1572 			n_tty_receive_char_closing(tty, *cp++);
1573 		else
1574 			n_tty_receive_char_flagged(tty, *cp++, flag);
1575 	}
1576 }
1577 
1578 static void
1579 n_tty_receive_buf_standard(struct tty_struct *tty, const unsigned char *cp,
1580 			  char *fp, int count)
1581 {
1582 	struct n_tty_data *ldata = tty->disc_data;
1583 	char flag = TTY_NORMAL;
1584 
1585 	while (count--) {
1586 		if (fp)
1587 			flag = *fp++;
1588 		if (likely(flag == TTY_NORMAL)) {
1589 			unsigned char c = *cp++;
1590 
1591 			if (I_ISTRIP(tty))
1592 				c &= 0x7f;
1593 			if (I_IUCLC(tty) && L_IEXTEN(tty))
1594 				c = tolower(c);
1595 			if (L_EXTPROC(tty)) {
1596 				put_tty_queue(c, ldata);
1597 				continue;
1598 			}
1599 			if (!test_bit(c, ldata->char_map))
1600 				n_tty_receive_char_inline(tty, c);
1601 			else if (n_tty_receive_char_special(tty, c) && count) {
1602 				if (fp)
1603 					flag = *fp++;
1604 				n_tty_receive_char_lnext(tty, *cp++, flag);
1605 				count--;
1606 			}
1607 		} else
1608 			n_tty_receive_char_flagged(tty, *cp++, flag);
1609 	}
1610 }
1611 
1612 static void
1613 n_tty_receive_buf_fast(struct tty_struct *tty, const unsigned char *cp,
1614 		       char *fp, int count)
1615 {
1616 	struct n_tty_data *ldata = tty->disc_data;
1617 	char flag = TTY_NORMAL;
1618 
1619 	while (count--) {
1620 		if (fp)
1621 			flag = *fp++;
1622 		if (likely(flag == TTY_NORMAL)) {
1623 			unsigned char c = *cp++;
1624 
1625 			if (!test_bit(c, ldata->char_map))
1626 				n_tty_receive_char_fast(tty, c);
1627 			else if (n_tty_receive_char_special(tty, c) && count) {
1628 				if (fp)
1629 					flag = *fp++;
1630 				n_tty_receive_char_lnext(tty, *cp++, flag);
1631 				count--;
1632 			}
1633 		} else
1634 			n_tty_receive_char_flagged(tty, *cp++, flag);
1635 	}
1636 }
1637 
1638 static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1639 			  char *fp, int count)
1640 {
1641 	struct n_tty_data *ldata = tty->disc_data;
1642 	bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
1643 
1644 	if (ldata->real_raw)
1645 		n_tty_receive_buf_real_raw(tty, cp, fp, count);
1646 	else if (ldata->raw || (L_EXTPROC(tty) && !preops))
1647 		n_tty_receive_buf_raw(tty, cp, fp, count);
1648 	else if (tty->closing && !L_EXTPROC(tty))
1649 		n_tty_receive_buf_closing(tty, cp, fp, count);
1650 	else {
1651 		if (ldata->lnext) {
1652 			char flag = TTY_NORMAL;
1653 
1654 			if (fp)
1655 				flag = *fp++;
1656 			n_tty_receive_char_lnext(tty, *cp++, flag);
1657 			count--;
1658 		}
1659 
1660 		if (!preops && !I_PARMRK(tty))
1661 			n_tty_receive_buf_fast(tty, cp, fp, count);
1662 		else
1663 			n_tty_receive_buf_standard(tty, cp, fp, count);
1664 
1665 		flush_echoes(tty);
1666 		if (tty->ops->flush_chars)
1667 			tty->ops->flush_chars(tty);
1668 	}
1669 
1670 	if ((!ldata->icanon && (read_cnt(ldata) >= ldata->minimum_to_wake)) ||
1671 		L_EXTPROC(tty)) {
1672 		kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1673 		if (waitqueue_active(&tty->read_wait))
1674 			wake_up_interruptible(&tty->read_wait);
1675 	}
1676 }
1677 
1678 static int
1679 n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp,
1680 			 char *fp, int count, int flow)
1681 {
1682 	struct n_tty_data *ldata = tty->disc_data;
1683 	int room, n, rcvd = 0;
1684 
1685 	down_read(&tty->termios_rwsem);
1686 
1687 	while (1) {
1688 		room = receive_room(tty);
1689 		n = min(count, room);
1690 		if (!n) {
1691 			if (flow && !room)
1692 				ldata->no_room = 1;
1693 			break;
1694 		}
1695 		__receive_buf(tty, cp, fp, n);
1696 		cp += n;
1697 		if (fp)
1698 			fp += n;
1699 		count -= n;
1700 		rcvd += n;
1701 	}
1702 
1703 	tty->receive_room = room;
1704 	n_tty_check_throttle(tty);
1705 	up_read(&tty->termios_rwsem);
1706 
1707 	return rcvd;
1708 }
1709 
1710 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1711 			      char *fp, int count)
1712 {
1713 	n_tty_receive_buf_common(tty, cp, fp, count, 0);
1714 }
1715 
1716 static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
1717 			      char *fp, int count)
1718 {
1719 	return n_tty_receive_buf_common(tty, cp, fp, count, 1);
1720 }
1721 
1722 int is_ignored(int sig)
1723 {
1724 	return (sigismember(&current->blocked, sig) ||
1725 		current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
1726 }
1727 
1728 /**
1729  *	n_tty_set_termios	-	termios data changed
1730  *	@tty: terminal
1731  *	@old: previous data
1732  *
1733  *	Called by the tty layer when the user changes termios flags so
1734  *	that the line discipline can plan ahead. This function cannot sleep
1735  *	and is protected from re-entry by the tty layer. The user is
1736  *	guaranteed that this function will not be re-entered or in progress
1737  *	when the ldisc is closed.
1738  *
1739  *	Locking: Caller holds tty->termios_rwsem
1740  */
1741 
1742 static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1743 {
1744 	struct n_tty_data *ldata = tty->disc_data;
1745 
1746 	if (!old || (old->c_lflag ^ tty->termios.c_lflag) & ICANON) {
1747 		bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
1748 		ldata->line_start = ldata->canon_head = ldata->read_tail;
1749 		ldata->erasing = 0;
1750 		ldata->lnext = 0;
1751 	}
1752 
1753 	ldata->icanon = (L_ICANON(tty) != 0);
1754 
1755 	if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1756 	    I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1757 	    I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1758 	    I_PARMRK(tty)) {
1759 		bitmap_zero(ldata->char_map, 256);
1760 
1761 		if (I_IGNCR(tty) || I_ICRNL(tty))
1762 			set_bit('\r', ldata->char_map);
1763 		if (I_INLCR(tty))
1764 			set_bit('\n', ldata->char_map);
1765 
1766 		if (L_ICANON(tty)) {
1767 			set_bit(ERASE_CHAR(tty), ldata->char_map);
1768 			set_bit(KILL_CHAR(tty), ldata->char_map);
1769 			set_bit(EOF_CHAR(tty), ldata->char_map);
1770 			set_bit('\n', ldata->char_map);
1771 			set_bit(EOL_CHAR(tty), ldata->char_map);
1772 			if (L_IEXTEN(tty)) {
1773 				set_bit(WERASE_CHAR(tty), ldata->char_map);
1774 				set_bit(LNEXT_CHAR(tty), ldata->char_map);
1775 				set_bit(EOL2_CHAR(tty), ldata->char_map);
1776 				if (L_ECHO(tty))
1777 					set_bit(REPRINT_CHAR(tty),
1778 						ldata->char_map);
1779 			}
1780 		}
1781 		if (I_IXON(tty)) {
1782 			set_bit(START_CHAR(tty), ldata->char_map);
1783 			set_bit(STOP_CHAR(tty), ldata->char_map);
1784 		}
1785 		if (L_ISIG(tty)) {
1786 			set_bit(INTR_CHAR(tty), ldata->char_map);
1787 			set_bit(QUIT_CHAR(tty), ldata->char_map);
1788 			set_bit(SUSP_CHAR(tty), ldata->char_map);
1789 		}
1790 		clear_bit(__DISABLED_CHAR, ldata->char_map);
1791 		ldata->raw = 0;
1792 		ldata->real_raw = 0;
1793 	} else {
1794 		ldata->raw = 1;
1795 		if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1796 		    (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1797 		    (tty->driver->flags & TTY_DRIVER_REAL_RAW))
1798 			ldata->real_raw = 1;
1799 		else
1800 			ldata->real_raw = 0;
1801 	}
1802 	n_tty_set_room(tty);
1803 	/*
1804 	 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1805 	 * been stopped by STOP_CHAR(tty) before it.
1806 	 */
1807 	if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped)
1808 		start_tty(tty);
1809 
1810 	/* The termios change make the tty ready for I/O */
1811 	if (waitqueue_active(&tty->write_wait))
1812 		wake_up_interruptible(&tty->write_wait);
1813 	if (waitqueue_active(&tty->read_wait))
1814 		wake_up_interruptible(&tty->read_wait);
1815 }
1816 
1817 /**
1818  *	n_tty_close		-	close the ldisc for this tty
1819  *	@tty: device
1820  *
1821  *	Called from the terminal layer when this line discipline is
1822  *	being shut down, either because of a close or becsuse of a
1823  *	discipline change. The function will not be called while other
1824  *	ldisc methods are in progress.
1825  */
1826 
1827 static void n_tty_close(struct tty_struct *tty)
1828 {
1829 	struct n_tty_data *ldata = tty->disc_data;
1830 
1831 	if (tty->link)
1832 		n_tty_packet_mode_flush(tty);
1833 
1834 	vfree(ldata);
1835 	tty->disc_data = NULL;
1836 }
1837 
1838 /**
1839  *	n_tty_open		-	open an ldisc
1840  *	@tty: terminal to open
1841  *
1842  *	Called when this line discipline is being attached to the
1843  *	terminal device. Can sleep. Called serialized so that no
1844  *	other events will occur in parallel. No further open will occur
1845  *	until a close.
1846  */
1847 
1848 static int n_tty_open(struct tty_struct *tty)
1849 {
1850 	struct n_tty_data *ldata;
1851 
1852 	/* Currently a malloc failure here can panic */
1853 	ldata = vmalloc(sizeof(*ldata));
1854 	if (!ldata)
1855 		goto err;
1856 
1857 	ldata->overrun_time = jiffies;
1858 	mutex_init(&ldata->atomic_read_lock);
1859 	mutex_init(&ldata->output_lock);
1860 
1861 	tty->disc_data = ldata;
1862 	reset_buffer_flags(tty->disc_data);
1863 	ldata->column = 0;
1864 	ldata->canon_column = 0;
1865 	ldata->minimum_to_wake = 1;
1866 	ldata->num_overrun = 0;
1867 	ldata->no_room = 0;
1868 	ldata->lnext = 0;
1869 	tty->closing = 0;
1870 	/* indicate buffer work may resume */
1871 	clear_bit(TTY_LDISC_HALTED, &tty->flags);
1872 	n_tty_set_termios(tty, NULL);
1873 	tty_unthrottle(tty);
1874 
1875 	return 0;
1876 err:
1877 	return -ENOMEM;
1878 }
1879 
1880 static inline int input_available_p(struct tty_struct *tty, int poll)
1881 {
1882 	struct n_tty_data *ldata = tty->disc_data;
1883 	int amt = poll && !TIME_CHAR(tty) ? MIN_CHAR(tty) : 1;
1884 
1885 	if (ldata->icanon && !L_EXTPROC(tty)) {
1886 		if (ldata->canon_head != ldata->read_tail)
1887 			return 1;
1888 	} else if (read_cnt(ldata) >= amt)
1889 		return 1;
1890 
1891 	return 0;
1892 }
1893 
1894 /**
1895  *	copy_from_read_buf	-	copy read data directly
1896  *	@tty: terminal device
1897  *	@b: user data
1898  *	@nr: size of data
1899  *
1900  *	Helper function to speed up n_tty_read.  It is only called when
1901  *	ICANON is off; it copies characters straight from the tty queue to
1902  *	user space directly.  It can be profitably called twice; once to
1903  *	drain the space from the tail pointer to the (physical) end of the
1904  *	buffer, and once to drain the space from the (physical) beginning of
1905  *	the buffer to head pointer.
1906  *
1907  *	Called under the ldata->atomic_read_lock sem
1908  *
1909  *	n_tty_read()/consumer path:
1910  *		caller holds non-exclusive termios_rwsem
1911  *		read_tail published
1912  */
1913 
1914 static int copy_from_read_buf(struct tty_struct *tty,
1915 				      unsigned char __user **b,
1916 				      size_t *nr)
1917 
1918 {
1919 	struct n_tty_data *ldata = tty->disc_data;
1920 	int retval;
1921 	size_t n;
1922 	bool is_eof;
1923 	size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
1924 
1925 	retval = 0;
1926 	n = min(read_cnt(ldata), N_TTY_BUF_SIZE - tail);
1927 	n = min(*nr, n);
1928 	if (n) {
1929 		retval = copy_to_user(*b, read_buf_addr(ldata, tail), n);
1930 		n -= retval;
1931 		is_eof = n == 1 && read_buf(ldata, tail) == EOF_CHAR(tty);
1932 		tty_audit_add_data(tty, read_buf_addr(ldata, tail), n,
1933 				ldata->icanon);
1934 		ldata->read_tail += n;
1935 		/* Turn single EOF into zero-length read */
1936 		if (L_EXTPROC(tty) && ldata->icanon && is_eof && !read_cnt(ldata))
1937 			n = 0;
1938 		*b += n;
1939 		*nr -= n;
1940 	}
1941 	return retval;
1942 }
1943 
1944 /**
1945  *	canon_copy_from_read_buf	-	copy read data in canonical mode
1946  *	@tty: terminal device
1947  *	@b: user data
1948  *	@nr: size of data
1949  *
1950  *	Helper function for n_tty_read.  It is only called when ICANON is on;
1951  *	it copies one line of input up to and including the line-delimiting
1952  *	character into the user-space buffer.
1953  *
1954  *	Called under the atomic_read_lock mutex
1955  *
1956  *	n_tty_read()/consumer path:
1957  *		caller holds non-exclusive termios_rwsem
1958  *		read_tail published
1959  */
1960 
1961 static int canon_copy_from_read_buf(struct tty_struct *tty,
1962 				    unsigned char __user **b,
1963 				    size_t *nr)
1964 {
1965 	struct n_tty_data *ldata = tty->disc_data;
1966 	size_t n, size, more, c;
1967 	size_t eol;
1968 	size_t tail;
1969 	int ret, found = 0;
1970 	bool eof_push = 0;
1971 
1972 	/* N.B. avoid overrun if nr == 0 */
1973 	n = min(*nr, read_cnt(ldata));
1974 	if (!n)
1975 		return 0;
1976 
1977 	tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
1978 	size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
1979 
1980 	n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
1981 		    __func__, *nr, tail, n, size);
1982 
1983 	eol = find_next_bit(ldata->read_flags, size, tail);
1984 	more = n - (size - tail);
1985 	if (eol == N_TTY_BUF_SIZE && more) {
1986 		/* scan wrapped without finding set bit */
1987 		eol = find_next_bit(ldata->read_flags, more, 0);
1988 		if (eol != more)
1989 			found = 1;
1990 	} else if (eol != size)
1991 		found = 1;
1992 
1993 	size = N_TTY_BUF_SIZE - tail;
1994 	n = eol - tail;
1995 	if (n > 4096)
1996 		n += 4096;
1997 	n += found;
1998 	c = n;
1999 
2000 	if (found && read_buf(ldata, eol) == __DISABLED_CHAR) {
2001 		n--;
2002 		eof_push = !n && ldata->read_tail != ldata->line_start;
2003 	}
2004 
2005 	n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu size:%zu more:%zu\n",
2006 		    __func__, eol, found, n, c, size, more);
2007 
2008 	if (n > size) {
2009 		ret = copy_to_user(*b, read_buf_addr(ldata, tail), size);
2010 		if (ret)
2011 			return -EFAULT;
2012 		ret = copy_to_user(*b + size, ldata->read_buf, n - size);
2013 	} else
2014 		ret = copy_to_user(*b, read_buf_addr(ldata, tail), n);
2015 
2016 	if (ret)
2017 		return -EFAULT;
2018 	*b += n;
2019 	*nr -= n;
2020 
2021 	if (found)
2022 		clear_bit(eol, ldata->read_flags);
2023 	smp_mb__after_clear_bit();
2024 	ldata->read_tail += c;
2025 
2026 	if (found) {
2027 		ldata->line_start = ldata->read_tail;
2028 		tty_audit_push(tty);
2029 	}
2030 	return eof_push ? -EAGAIN : 0;
2031 }
2032 
2033 extern ssize_t redirected_tty_write(struct file *, const char __user *,
2034 							size_t, loff_t *);
2035 
2036 /**
2037  *	job_control		-	check job control
2038  *	@tty: tty
2039  *	@file: file handle
2040  *
2041  *	Perform job control management checks on this file/tty descriptor
2042  *	and if appropriate send any needed signals and return a negative
2043  *	error code if action should be taken.
2044  *
2045  *	Locking: redirected write test is safe
2046  *		 current->signal->tty check is safe
2047  *		 ctrl_lock to safely reference tty->pgrp
2048  */
2049 
2050 static int job_control(struct tty_struct *tty, struct file *file)
2051 {
2052 	/* Job control check -- must be done at start and after
2053 	   every sleep (POSIX.1 7.1.1.4). */
2054 	/* NOTE: not yet done after every sleep pending a thorough
2055 	   check of the logic of this change. -- jlc */
2056 	/* don't stop on /dev/console */
2057 	if (file->f_op->write == redirected_tty_write ||
2058 	    current->signal->tty != tty)
2059 		return 0;
2060 
2061 	spin_lock_irq(&tty->ctrl_lock);
2062 	if (!tty->pgrp)
2063 		printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
2064 	else if (task_pgrp(current) != tty->pgrp) {
2065 		spin_unlock_irq(&tty->ctrl_lock);
2066 		if (is_ignored(SIGTTIN) || is_current_pgrp_orphaned())
2067 			return -EIO;
2068 		kill_pgrp(task_pgrp(current), SIGTTIN, 1);
2069 		set_thread_flag(TIF_SIGPENDING);
2070 		return -ERESTARTSYS;
2071 	}
2072 	spin_unlock_irq(&tty->ctrl_lock);
2073 	return 0;
2074 }
2075 
2076 
2077 /**
2078  *	n_tty_read		-	read function for tty
2079  *	@tty: tty device
2080  *	@file: file object
2081  *	@buf: userspace buffer pointer
2082  *	@nr: size of I/O
2083  *
2084  *	Perform reads for the line discipline. We are guaranteed that the
2085  *	line discipline will not be closed under us but we may get multiple
2086  *	parallel readers and must handle this ourselves. We may also get
2087  *	a hangup. Always called in user context, may sleep.
2088  *
2089  *	This code must be sure never to sleep through a hangup.
2090  *
2091  *	n_tty_read()/consumer path:
2092  *		claims non-exclusive termios_rwsem
2093  *		publishes read_tail
2094  */
2095 
2096 static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
2097 			 unsigned char __user *buf, size_t nr)
2098 {
2099 	struct n_tty_data *ldata = tty->disc_data;
2100 	unsigned char __user *b = buf;
2101 	DECLARE_WAITQUEUE(wait, current);
2102 	int c;
2103 	int minimum, time;
2104 	ssize_t retval = 0;
2105 	long timeout;
2106 	unsigned long flags;
2107 	int packet;
2108 
2109 	c = job_control(tty, file);
2110 	if (c < 0)
2111 		return c;
2112 
2113 	/*
2114 	 *	Internal serialization of reads.
2115 	 */
2116 	if (file->f_flags & O_NONBLOCK) {
2117 		if (!mutex_trylock(&ldata->atomic_read_lock))
2118 			return -EAGAIN;
2119 	} else {
2120 		if (mutex_lock_interruptible(&ldata->atomic_read_lock))
2121 			return -ERESTARTSYS;
2122 	}
2123 
2124 	down_read(&tty->termios_rwsem);
2125 
2126 	minimum = time = 0;
2127 	timeout = MAX_SCHEDULE_TIMEOUT;
2128 	if (!ldata->icanon) {
2129 		minimum = MIN_CHAR(tty);
2130 		if (minimum) {
2131 			time = (HZ / 10) * TIME_CHAR(tty);
2132 			if (time)
2133 				ldata->minimum_to_wake = 1;
2134 			else if (!waitqueue_active(&tty->read_wait) ||
2135 				 (ldata->minimum_to_wake > minimum))
2136 				ldata->minimum_to_wake = minimum;
2137 		} else {
2138 			timeout = (HZ / 10) * TIME_CHAR(tty);
2139 			ldata->minimum_to_wake = minimum = 1;
2140 		}
2141 	}
2142 
2143 	packet = tty->packet;
2144 
2145 	add_wait_queue(&tty->read_wait, &wait);
2146 	while (nr) {
2147 		/* First test for status change. */
2148 		if (packet && tty->link->ctrl_status) {
2149 			unsigned char cs;
2150 			if (b != buf)
2151 				break;
2152 			spin_lock_irqsave(&tty->link->ctrl_lock, flags);
2153 			cs = tty->link->ctrl_status;
2154 			tty->link->ctrl_status = 0;
2155 			spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
2156 			if (tty_put_user(tty, cs, b++)) {
2157 				retval = -EFAULT;
2158 				b--;
2159 				break;
2160 			}
2161 			nr--;
2162 			break;
2163 		}
2164 		/* This statement must be first before checking for input
2165 		   so that any interrupt will set the state back to
2166 		   TASK_RUNNING. */
2167 		set_current_state(TASK_INTERRUPTIBLE);
2168 
2169 		if (((minimum - (b - buf)) < ldata->minimum_to_wake) &&
2170 		    ((minimum - (b - buf)) >= 1))
2171 			ldata->minimum_to_wake = (minimum - (b - buf));
2172 
2173 		if (!input_available_p(tty, 0)) {
2174 			if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
2175 				up_read(&tty->termios_rwsem);
2176 				tty_flush_to_ldisc(tty);
2177 				down_read(&tty->termios_rwsem);
2178 				if (!input_available_p(tty, 0)) {
2179 					retval = -EIO;
2180 					break;
2181 				}
2182 			} else {
2183 				if (tty_hung_up_p(file))
2184 					break;
2185 				if (!timeout)
2186 					break;
2187 				if (file->f_flags & O_NONBLOCK) {
2188 					retval = -EAGAIN;
2189 					break;
2190 				}
2191 				if (signal_pending(current)) {
2192 					retval = -ERESTARTSYS;
2193 					break;
2194 				}
2195 				n_tty_set_room(tty);
2196 				up_read(&tty->termios_rwsem);
2197 
2198 				timeout = schedule_timeout(timeout);
2199 
2200 				down_read(&tty->termios_rwsem);
2201 				continue;
2202 			}
2203 		}
2204 		__set_current_state(TASK_RUNNING);
2205 
2206 		/* Deal with packet mode. */
2207 		if (packet && b == buf) {
2208 			if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
2209 				retval = -EFAULT;
2210 				b--;
2211 				break;
2212 			}
2213 			nr--;
2214 		}
2215 
2216 		if (ldata->icanon && !L_EXTPROC(tty)) {
2217 			retval = canon_copy_from_read_buf(tty, &b, &nr);
2218 			if (retval == -EAGAIN) {
2219 				retval = 0;
2220 				continue;
2221 			} else if (retval)
2222 				break;
2223 		} else {
2224 			int uncopied;
2225 			/* The copy function takes the read lock and handles
2226 			   locking internally for this case */
2227 			uncopied = copy_from_read_buf(tty, &b, &nr);
2228 			uncopied += copy_from_read_buf(tty, &b, &nr);
2229 			if (uncopied) {
2230 				retval = -EFAULT;
2231 				break;
2232 			}
2233 		}
2234 
2235 		n_tty_check_unthrottle(tty);
2236 
2237 		if (b - buf >= minimum)
2238 			break;
2239 		if (time)
2240 			timeout = time;
2241 	}
2242 	n_tty_set_room(tty);
2243 	up_read(&tty->termios_rwsem);
2244 
2245 	remove_wait_queue(&tty->read_wait, &wait);
2246 	if (!waitqueue_active(&tty->read_wait))
2247 		ldata->minimum_to_wake = minimum;
2248 
2249 	mutex_unlock(&ldata->atomic_read_lock);
2250 
2251 	__set_current_state(TASK_RUNNING);
2252 	if (b - buf)
2253 		retval = b - buf;
2254 
2255 	return retval;
2256 }
2257 
2258 /**
2259  *	n_tty_write		-	write function for tty
2260  *	@tty: tty device
2261  *	@file: file object
2262  *	@buf: userspace buffer pointer
2263  *	@nr: size of I/O
2264  *
2265  *	Write function of the terminal device.  This is serialized with
2266  *	respect to other write callers but not to termios changes, reads
2267  *	and other such events.  Since the receive code will echo characters,
2268  *	thus calling driver write methods, the output_lock is used in
2269  *	the output processing functions called here as well as in the
2270  *	echo processing function to protect the column state and space
2271  *	left in the buffer.
2272  *
2273  *	This code must be sure never to sleep through a hangup.
2274  *
2275  *	Locking: output_lock to protect column state and space left
2276  *		 (note that the process_output*() functions take this
2277  *		  lock themselves)
2278  */
2279 
2280 static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
2281 			   const unsigned char *buf, size_t nr)
2282 {
2283 	const unsigned char *b = buf;
2284 	DECLARE_WAITQUEUE(wait, current);
2285 	int c;
2286 	ssize_t retval = 0;
2287 
2288 	/* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2289 	if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2290 		retval = tty_check_change(tty);
2291 		if (retval)
2292 			return retval;
2293 	}
2294 
2295 	down_read(&tty->termios_rwsem);
2296 
2297 	/* Write out any echoed characters that are still pending */
2298 	process_echoes(tty);
2299 
2300 	add_wait_queue(&tty->write_wait, &wait);
2301 	while (1) {
2302 		set_current_state(TASK_INTERRUPTIBLE);
2303 		if (signal_pending(current)) {
2304 			retval = -ERESTARTSYS;
2305 			break;
2306 		}
2307 		if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2308 			retval = -EIO;
2309 			break;
2310 		}
2311 		if (O_OPOST(tty)) {
2312 			while (nr > 0) {
2313 				ssize_t num = process_output_block(tty, b, nr);
2314 				if (num < 0) {
2315 					if (num == -EAGAIN)
2316 						break;
2317 					retval = num;
2318 					goto break_out;
2319 				}
2320 				b += num;
2321 				nr -= num;
2322 				if (nr == 0)
2323 					break;
2324 				c = *b;
2325 				if (process_output(c, tty) < 0)
2326 					break;
2327 				b++; nr--;
2328 			}
2329 			if (tty->ops->flush_chars)
2330 				tty->ops->flush_chars(tty);
2331 		} else {
2332 			while (nr > 0) {
2333 				c = tty->ops->write(tty, b, nr);
2334 				if (c < 0) {
2335 					retval = c;
2336 					goto break_out;
2337 				}
2338 				if (!c)
2339 					break;
2340 				b += c;
2341 				nr -= c;
2342 			}
2343 		}
2344 		if (!nr)
2345 			break;
2346 		if (file->f_flags & O_NONBLOCK) {
2347 			retval = -EAGAIN;
2348 			break;
2349 		}
2350 		up_read(&tty->termios_rwsem);
2351 
2352 		schedule();
2353 
2354 		down_read(&tty->termios_rwsem);
2355 	}
2356 break_out:
2357 	__set_current_state(TASK_RUNNING);
2358 	remove_wait_queue(&tty->write_wait, &wait);
2359 	if (b - buf != nr && tty->fasync)
2360 		set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2361 	up_read(&tty->termios_rwsem);
2362 	return (b - buf) ? b - buf : retval;
2363 }
2364 
2365 /**
2366  *	n_tty_poll		-	poll method for N_TTY
2367  *	@tty: terminal device
2368  *	@file: file accessing it
2369  *	@wait: poll table
2370  *
2371  *	Called when the line discipline is asked to poll() for data or
2372  *	for special events. This code is not serialized with respect to
2373  *	other events save open/close.
2374  *
2375  *	This code must be sure never to sleep through a hangup.
2376  *	Called without the kernel lock held - fine
2377  */
2378 
2379 static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
2380 							poll_table *wait)
2381 {
2382 	struct n_tty_data *ldata = tty->disc_data;
2383 	unsigned int mask = 0;
2384 
2385 	poll_wait(file, &tty->read_wait, wait);
2386 	poll_wait(file, &tty->write_wait, wait);
2387 	if (input_available_p(tty, 1))
2388 		mask |= POLLIN | POLLRDNORM;
2389 	if (tty->packet && tty->link->ctrl_status)
2390 		mask |= POLLPRI | POLLIN | POLLRDNORM;
2391 	if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2392 		mask |= POLLHUP;
2393 	if (tty_hung_up_p(file))
2394 		mask |= POLLHUP;
2395 	if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2396 		if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2397 			ldata->minimum_to_wake = MIN_CHAR(tty);
2398 		else
2399 			ldata->minimum_to_wake = 1;
2400 	}
2401 	if (tty->ops->write && !tty_is_writelocked(tty) &&
2402 			tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2403 			tty_write_room(tty) > 0)
2404 		mask |= POLLOUT | POLLWRNORM;
2405 	return mask;
2406 }
2407 
2408 static unsigned long inq_canon(struct n_tty_data *ldata)
2409 {
2410 	size_t nr, head, tail;
2411 
2412 	if (ldata->canon_head == ldata->read_tail)
2413 		return 0;
2414 	head = ldata->canon_head;
2415 	tail = ldata->read_tail;
2416 	nr = head - tail;
2417 	/* Skip EOF-chars.. */
2418 	while (head != tail) {
2419 		if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) &&
2420 		    read_buf(ldata, tail) == __DISABLED_CHAR)
2421 			nr--;
2422 		tail++;
2423 	}
2424 	return nr;
2425 }
2426 
2427 static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2428 		       unsigned int cmd, unsigned long arg)
2429 {
2430 	struct n_tty_data *ldata = tty->disc_data;
2431 	int retval;
2432 
2433 	switch (cmd) {
2434 	case TIOCOUTQ:
2435 		return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2436 	case TIOCINQ:
2437 		down_write(&tty->termios_rwsem);
2438 		if (L_ICANON(tty))
2439 			retval = inq_canon(ldata);
2440 		else
2441 			retval = read_cnt(ldata);
2442 		up_write(&tty->termios_rwsem);
2443 		return put_user(retval, (unsigned int __user *) arg);
2444 	default:
2445 		return n_tty_ioctl_helper(tty, file, cmd, arg);
2446 	}
2447 }
2448 
2449 static void n_tty_fasync(struct tty_struct *tty, int on)
2450 {
2451 	struct n_tty_data *ldata = tty->disc_data;
2452 
2453 	if (!waitqueue_active(&tty->read_wait)) {
2454 		if (on)
2455 			ldata->minimum_to_wake = 1;
2456 		else if (!tty->fasync)
2457 			ldata->minimum_to_wake = N_TTY_BUF_SIZE;
2458 	}
2459 }
2460 
2461 struct tty_ldisc_ops tty_ldisc_N_TTY = {
2462 	.magic           = TTY_LDISC_MAGIC,
2463 	.name            = "n_tty",
2464 	.open            = n_tty_open,
2465 	.close           = n_tty_close,
2466 	.flush_buffer    = n_tty_flush_buffer,
2467 	.chars_in_buffer = n_tty_chars_in_buffer,
2468 	.read            = n_tty_read,
2469 	.write           = n_tty_write,
2470 	.ioctl           = n_tty_ioctl,
2471 	.set_termios     = n_tty_set_termios,
2472 	.poll            = n_tty_poll,
2473 	.receive_buf     = n_tty_receive_buf,
2474 	.write_wakeup    = n_tty_write_wakeup,
2475 	.fasync		 = n_tty_fasync,
2476 	.receive_buf2	 = n_tty_receive_buf2,
2477 };
2478 
2479 /**
2480  *	n_tty_inherit_ops	-	inherit N_TTY methods
2481  *	@ops: struct tty_ldisc_ops where to save N_TTY methods
2482  *
2483  *	Enables a 'subclass' line discipline to 'inherit' N_TTY
2484  *	methods.
2485  */
2486 
2487 void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2488 {
2489 	*ops = tty_ldisc_N_TTY;
2490 	ops->owner = NULL;
2491 	ops->refcount = ops->flags = 0;
2492 }
2493 EXPORT_SYMBOL_GPL(n_tty_inherit_ops);
2494