2 * n_tty.c --- implements the N_TTY line discipline.
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,
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.
13 * Written by Theodore Ts'o, Copyright 1994.
15 * This file also contains code originally written by Linus Torvalds,
16 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
18 * This file may be redistributed under the terms of the GNU General Public
21 * Reduced memory usage for older ARM systems - Russell King.
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.
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
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>
44 #include <linux/string.h>
45 #include <linux/slab.h>
46 #include <linux/poll.h>
48 #include <asm/uaccess.h>
49 #include <asm/system.h>
50 #include <asm/bitops.h>
52 /* number of characters left in xmit buffer before select has we have room */
53 #define WAKEUP_CHARS 256
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.
60 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
61 #define TTY_THRESHOLD_UNTHROTTLE 128
63 static inline unsigned char *alloc_buf(void)
66 int prio = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
68 if (PAGE_SIZE != N_TTY_BUF_SIZE) {
69 p = kmalloc(N_TTY_BUF_SIZE, prio);
71 memset(p, 0, N_TTY_BUF_SIZE);
73 p = (unsigned char *)get_zeroed_page(prio);
78 static inline void free_buf(unsigned char *buf)
80 if (PAGE_SIZE != N_TTY_BUF_SIZE)
83 free_page((unsigned long) buf);
86 static inline void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
88 if (tty->read_cnt < N_TTY_BUF_SIZE) {
89 tty->read_buf[tty->read_head] = c;
90 tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
95 static inline void put_tty_queue(unsigned char c, struct tty_struct *tty)
99 * The problem of stomping on the buffers ends here.
100 * Why didn't anyone see this one coming? --AJK
102 spin_lock_irqsave(&tty->read_lock, flags);
103 put_tty_queue_nolock(c, tty);
104 spin_unlock_irqrestore(&tty->read_lock, flags);
108 * Check whether to call the driver.unthrottle function.
109 * We test the TTY_THROTTLED bit first so that it always
110 * indicates the current state.
112 static void check_unthrottle(struct tty_struct * tty)
115 test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
116 tty->driver->unthrottle)
117 tty->driver->unthrottle(tty);
121 * Reset the read buffer counters, clear the flags,
122 * and make sure the driver is unthrottled. Called
123 * from n_tty_open() and n_tty_flush_buffer().
125 static void reset_buffer_flags(struct tty_struct *tty)
129 spin_lock_irqsave(&tty->read_lock, flags);
130 tty->read_head = tty->read_tail = tty->read_cnt = 0;
131 spin_unlock_irqrestore(&tty->read_lock, flags);
132 tty->canon_head = tty->canon_data = tty->erasing = 0;
133 memset(&tty->read_flags, 0, sizeof tty->read_flags);
134 check_unthrottle(tty);
138 * Flush the input buffer
140 void n_tty_flush_buffer(struct tty_struct * tty)
142 /* clear everything and unthrottle the driver */
143 reset_buffer_flags(tty);
148 if (tty->link->packet) {
149 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
150 wake_up_interruptible(&tty->link->read_wait);
155 * Return number of characters buffered to be delivered to user
157 ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
162 spin_lock_irqsave(&tty->read_lock, flags);
165 } else if (tty->canon_data) {
166 n = (tty->canon_head > tty->read_tail) ?
167 tty->canon_head - tty->read_tail :
168 tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
170 spin_unlock_irqrestore(&tty->read_lock, flags);
174 static inline int is_utf8_continuation(unsigned char c)
176 return (c & 0xc0) == 0x80;
179 static inline int is_continuation(unsigned char c, struct tty_struct *tty)
181 return I_IUTF8(tty) && is_utf8_continuation(c);
185 * Perform OPOST processing. Returns -1 when the output device is
186 * full and the character must be retried.
188 static int opost(unsigned char c, struct tty_struct *tty)
192 space = tty->driver->write_room(tty);
204 tty->driver->put_char(tty, '\r');
207 tty->canon_column = tty->column;
210 if (O_ONOCR(tty) && tty->column == 0)
215 tty->canon_column = tty->column = 0;
218 tty->canon_column = tty->column = 0;
221 spaces = 8 - (tty->column & 7);
222 if (O_TABDLY(tty) == XTABS) {
225 tty->column += spaces;
226 tty->driver->write(tty, 0, " ", spaces);
229 tty->column += spaces;
238 if (!iscntrl(c) && !is_continuation(c, tty))
243 tty->driver->put_char(tty, c);
248 * opost_block --- to speed up block console writes, among other
251 static ssize_t opost_block(struct tty_struct * tty,
252 const unsigned char __user * inbuf, unsigned int nr)
259 space = tty->driver->write_room(tty);
264 if (nr > sizeof(buf))
267 if (copy_from_user(buf, inbuf, nr))
270 for (i = 0, cp = buf; i < nr; i++, cp++) {
277 tty->canon_column = tty->column;
280 if (O_ONOCR(tty) && tty->column == 0)
285 tty->canon_column = tty->column = 0;
288 tty->canon_column = tty->column = 0;
305 if (tty->driver->flush_chars)
306 tty->driver->flush_chars(tty);
307 i = tty->driver->write(tty, 0, buf, i);
313 static inline void put_char(unsigned char c, struct tty_struct *tty)
315 tty->driver->put_char(tty, c);
318 /* Must be called only when L_ECHO(tty) is true. */
320 static void echo_char(unsigned char c, struct tty_struct *tty)
322 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
324 put_char(c ^ 0100, tty);
330 static inline void finish_erasing(struct tty_struct *tty)
339 static void eraser(unsigned char c, struct tty_struct *tty)
341 enum { ERASE, WERASE, KILL } kill_type;
342 int head, seen_alnums, cnt;
345 if (tty->read_head == tty->canon_head) {
346 /* opost('\a', tty); */ /* what do you think? */
349 if (c == ERASE_CHAR(tty))
351 else if (c == WERASE_CHAR(tty))
355 spin_lock_irqsave(&tty->read_lock, flags);
356 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
357 (N_TTY_BUF_SIZE - 1));
358 tty->read_head = tty->canon_head;
359 spin_unlock_irqrestore(&tty->read_lock, flags);
362 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
363 spin_lock_irqsave(&tty->read_lock, flags);
364 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
365 (N_TTY_BUF_SIZE - 1));
366 tty->read_head = tty->canon_head;
367 spin_unlock_irqrestore(&tty->read_lock, flags);
369 echo_char(KILL_CHAR(tty), tty);
370 /* Add a newline if ECHOK is on and ECHOKE is off. */
379 while (tty->read_head != tty->canon_head) {
380 head = tty->read_head;
382 /* erase a single possibly multibyte character */
384 head = (head - 1) & (N_TTY_BUF_SIZE-1);
385 c = tty->read_buf[head];
386 } while (is_continuation(c, tty) && head != tty->canon_head);
388 /* do not partially erase */
389 if (is_continuation(c, tty))
392 if (kill_type == WERASE) {
393 /* Equivalent to BSD's ALTWERASE. */
394 if (isalnum(c) || c == '_')
396 else if (seen_alnums)
399 cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
400 spin_lock_irqsave(&tty->read_lock, flags);
401 tty->read_head = head;
402 tty->read_cnt -= cnt;
403 spin_unlock_irqrestore(&tty->read_lock, flags);
405 if (L_ECHOPRT(tty)) {
411 /* if cnt > 1, output a multi-byte character */
414 head = (head+1) & (N_TTY_BUF_SIZE-1);
415 put_char(tty->read_buf[head], tty);
417 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
418 echo_char(ERASE_CHAR(tty), tty);
419 } else if (c == '\t') {
420 unsigned int col = tty->canon_column;
421 unsigned long tail = tty->canon_head;
423 /* Find the column of the last char. */
424 while (tail != tty->read_head) {
425 c = tty->read_buf[tail];
428 else if (iscntrl(c)) {
431 } else if (!is_continuation(c, tty))
433 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
436 /* should never happen */
437 if (tty->column > 0x80000000)
440 /* Now backup to that column. */
441 while (tty->column > col) {
442 /* Can't use opost here. */
448 if (iscntrl(c) && L_ECHOCTL(tty)) {
455 if (!iscntrl(c) || L_ECHOCTL(tty)) {
464 if (kill_type == ERASE)
467 if (tty->read_head == tty->canon_head)
471 static inline void isig(int sig, struct tty_struct *tty, int flush)
474 kill_pg(tty->pgrp, sig, 1);
475 if (flush || !L_NOFLSH(tty)) {
476 n_tty_flush_buffer(tty);
477 if (tty->driver->flush_buffer)
478 tty->driver->flush_buffer(tty);
482 static inline void n_tty_receive_break(struct tty_struct *tty)
487 isig(SIGINT, tty, 1);
491 put_tty_queue('\377', tty);
492 put_tty_queue('\0', tty);
494 put_tty_queue('\0', tty);
495 wake_up_interruptible(&tty->read_wait);
498 static inline void n_tty_receive_overrun(struct tty_struct *tty)
503 if (time_before(tty->overrun_time, jiffies - HZ)) {
504 printk("%s: %d input overrun(s)\n", tty_name(tty, buf),
506 tty->overrun_time = jiffies;
507 tty->num_overrun = 0;
511 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
518 put_tty_queue('\377', tty);
519 put_tty_queue('\0', tty);
520 put_tty_queue(c, tty);
521 } else if (I_INPCK(tty))
522 put_tty_queue('\0', tty);
524 put_tty_queue(c, tty);
525 wake_up_interruptible(&tty->read_wait);
528 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
533 put_tty_queue(c, tty);
537 if (tty->stopped && !tty->flow_stopped &&
538 I_IXON(tty) && I_IXANY(tty)) {
545 if (I_IUCLC(tty) && L_IEXTEN(tty))
550 if (c == START_CHAR(tty))
552 else if (c == STOP_CHAR(tty))
559 * If the previous character was LNEXT, or we know that this
560 * character is not one of the characters that we'll have to
561 * handle specially, do shortcut processing to speed things
564 if (!test_bit(c, tty->process_char_map) || tty->lnext) {
568 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
569 put_char('\a', tty); /* beep if no space */
572 /* Record the column of first canon char. */
573 if (tty->canon_head == tty->read_head)
574 tty->canon_column = tty->column;
577 if (I_PARMRK(tty) && c == (unsigned char) '\377')
578 put_tty_queue(c, tty);
579 put_tty_queue(c, tty);
588 } else if (c == '\n' && I_INLCR(tty))
591 if (c == START_CHAR(tty)) {
595 if (c == STOP_CHAR(tty)) {
603 if (c == INTR_CHAR(tty))
606 if (c == QUIT_CHAR(tty))
609 if (c == SUSP_CHAR(tty)) {
611 isig(signal, tty, 0);
616 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
617 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
621 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
625 if (L_ECHOCTL(tty)) {
632 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
634 unsigned long tail = tty->canon_head;
639 while (tail != tty->read_head) {
640 echo_char(tty->read_buf[tail], tty);
641 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
646 if (L_ECHO(tty) || L_ECHONL(tty)) {
647 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
655 if (c == EOF_CHAR(tty)) {
656 if (tty->canon_head != tty->read_head)
657 set_bit(TTY_PUSH, &tty->flags);
661 if ((c == EOL_CHAR(tty)) ||
662 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
664 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
667 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
671 /* Record the column of first canon char. */
672 if (tty->canon_head == tty->read_head)
673 tty->canon_column = tty->column;
677 * XXX does PARMRK doubling happen for
678 * EOL_CHAR and EOL2_CHAR?
680 if (I_PARMRK(tty) && c == (unsigned char) '\377')
681 put_tty_queue(c, tty);
684 spin_lock_irqsave(&tty->read_lock, flags);
685 set_bit(tty->read_head, tty->read_flags);
686 put_tty_queue_nolock(c, tty);
687 tty->canon_head = tty->read_head;
689 spin_unlock_irqrestore(&tty->read_lock, flags);
690 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
691 if (waitqueue_active(&tty->read_wait))
692 wake_up_interruptible(&tty->read_wait);
699 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
700 put_char('\a', tty); /* beep if no space */
706 /* Record the column of first canon char. */
707 if (tty->canon_head == tty->read_head)
708 tty->canon_column = tty->column;
713 if (I_PARMRK(tty) && c == (unsigned char) '\377')
714 put_tty_queue(c, tty);
716 put_tty_queue(c, tty);
719 static int n_tty_receive_room(struct tty_struct *tty)
721 int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
724 * If we are doing input canonicalization, and there are no
725 * pending newlines, let characters through without limit, so
726 * that erase characters will be handled. Other excess
727 * characters will be beeped.
729 if (tty->icanon && !tty->canon_data)
730 return N_TTY_BUF_SIZE;
738 * Required for the ptys, serial driver etc. since processes
739 * that attach themselves to the master and rely on ASYNC
740 * IO must be woken up
743 static void n_tty_write_wakeup(struct tty_struct *tty)
747 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
748 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
753 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
756 const unsigned char *p;
757 char *f, flags = TTY_NORMAL;
760 unsigned long cpuflags;
766 spin_lock_irqsave(&tty->read_lock, cpuflags);
767 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
768 N_TTY_BUF_SIZE - tty->read_head);
770 memcpy(tty->read_buf + tty->read_head, cp, i);
771 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
776 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
777 N_TTY_BUF_SIZE - tty->read_head);
779 memcpy(tty->read_buf + tty->read_head, cp, i);
780 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
782 spin_unlock_irqrestore(&tty->read_lock, cpuflags);
784 for (i=count, p = cp, f = fp; i; i--, p++) {
789 n_tty_receive_char(tty, *p);
792 n_tty_receive_break(tty);
796 n_tty_receive_parity_error(tty, *p);
799 n_tty_receive_overrun(tty);
802 printk("%s: unknown flag %d\n",
803 tty_name(tty, buf), flags);
807 if (tty->driver->flush_chars)
808 tty->driver->flush_chars(tty);
811 if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
812 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
813 if (waitqueue_active(&tty->read_wait))
814 wake_up_interruptible(&tty->read_wait);
818 * Check the remaining room for the input canonicalization
819 * mode. We don't want to throttle the driver if we're in
820 * canonical mode and don't have a newline yet!
822 if (n_tty_receive_room(tty) < TTY_THRESHOLD_THROTTLE) {
823 /* check TTY_THROTTLED first so it indicates our state */
824 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
825 tty->driver->throttle)
826 tty->driver->throttle(tty);
830 int is_ignored(int sig)
832 return (sigismember(¤t->blocked, sig) ||
833 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
836 static void n_tty_set_termios(struct tty_struct *tty, struct termios * old)
841 tty->icanon = (L_ICANON(tty) != 0);
842 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
847 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
848 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
849 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
851 local_irq_disable(); // FIXME: is this safe?
852 memset(tty->process_char_map, 0, 256/8);
854 if (I_IGNCR(tty) || I_ICRNL(tty))
855 set_bit('\r', tty->process_char_map);
857 set_bit('\n', tty->process_char_map);
860 set_bit(ERASE_CHAR(tty), tty->process_char_map);
861 set_bit(KILL_CHAR(tty), tty->process_char_map);
862 set_bit(EOF_CHAR(tty), tty->process_char_map);
863 set_bit('\n', tty->process_char_map);
864 set_bit(EOL_CHAR(tty), tty->process_char_map);
866 set_bit(WERASE_CHAR(tty),
867 tty->process_char_map);
868 set_bit(LNEXT_CHAR(tty),
869 tty->process_char_map);
870 set_bit(EOL2_CHAR(tty),
871 tty->process_char_map);
873 set_bit(REPRINT_CHAR(tty),
874 tty->process_char_map);
878 set_bit(START_CHAR(tty), tty->process_char_map);
879 set_bit(STOP_CHAR(tty), tty->process_char_map);
882 set_bit(INTR_CHAR(tty), tty->process_char_map);
883 set_bit(QUIT_CHAR(tty), tty->process_char_map);
884 set_bit(SUSP_CHAR(tty), tty->process_char_map);
886 clear_bit(__DISABLED_CHAR, tty->process_char_map);
887 local_irq_enable(); // FIXME: is this safe?
892 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
893 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
894 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
901 static void n_tty_close(struct tty_struct *tty)
903 n_tty_flush_buffer(tty);
905 free_buf(tty->read_buf);
910 static int n_tty_open(struct tty_struct *tty)
915 if (!tty->read_buf) {
916 tty->read_buf = alloc_buf();
920 memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
921 reset_buffer_flags(tty);
923 n_tty_set_termios(tty, 0);
924 tty->minimum_to_wake = 1;
929 static inline int input_available_p(struct tty_struct *tty, int amt)
934 } else if (tty->read_cnt >= (amt ? amt : 1))
941 * Helper function to speed up read_chan. It is only called when
942 * ICANON is off; it copies characters straight from the tty queue to
943 * user space directly. It can be profitably called twice; once to
944 * drain the space from the tail pointer to the (physical) end of the
945 * buffer, and once to drain the space from the (physical) beginning of
946 * the buffer to head pointer.
948 static inline int copy_from_read_buf(struct tty_struct *tty,
949 unsigned char __user **b,
958 spin_lock_irqsave(&tty->read_lock, flags);
959 n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
960 n = min((ssize_t)*nr, n);
961 spin_unlock_irqrestore(&tty->read_lock, flags);
964 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
966 spin_lock_irqsave(&tty->read_lock, flags);
967 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
969 spin_unlock_irqrestore(&tty->read_lock, flags);
976 extern ssize_t redirected_tty_write(struct file *,const char *,size_t,loff_t *);
978 static ssize_t read_chan(struct tty_struct *tty, struct file *file,
979 unsigned char __user *buf, size_t nr)
981 unsigned char __user *b = buf;
982 DECLARE_WAITQUEUE(wait, current);
992 if (!tty->read_buf) {
993 printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
997 /* Job control check -- must be done at start and after
998 every sleep (POSIX.1 7.1.1.4). */
999 /* NOTE: not yet done after every sleep pending a thorough
1000 check of the logic of this change. -- jlc */
1001 /* don't stop on /dev/console */
1002 if (file->f_op->write != redirected_tty_write &&
1003 current->signal->tty == tty) {
1005 printk("read_chan: tty->pgrp <= 0!\n");
1006 else if (process_group(current) != tty->pgrp) {
1007 if (is_ignored(SIGTTIN) ||
1008 is_orphaned_pgrp(process_group(current)))
1010 kill_pg(process_group(current), SIGTTIN, 1);
1011 return -ERESTARTSYS;
1016 timeout = MAX_SCHEDULE_TIMEOUT;
1018 time = (HZ / 10) * TIME_CHAR(tty);
1019 minimum = MIN_CHAR(tty);
1022 tty->minimum_to_wake = 1;
1023 else if (!waitqueue_active(&tty->read_wait) ||
1024 (tty->minimum_to_wake > minimum))
1025 tty->minimum_to_wake = minimum;
1032 tty->minimum_to_wake = minimum = 1;
1036 if (file->f_flags & O_NONBLOCK) {
1037 if (down_trylock(&tty->atomic_read))
1041 if (down_interruptible(&tty->atomic_read))
1042 return -ERESTARTSYS;
1045 add_wait_queue(&tty->read_wait, &wait);
1046 set_bit(TTY_DONT_FLIP, &tty->flags);
1048 /* First test for status change. */
1049 if (tty->packet && tty->link->ctrl_status) {
1053 cs = tty->link->ctrl_status;
1054 tty->link->ctrl_status = 0;
1055 if (put_user(cs, b++)) {
1063 /* This statement must be first before checking for input
1064 so that any interrupt will set the state back to
1066 set_current_state(TASK_INTERRUPTIBLE);
1068 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1069 ((minimum - (b - buf)) >= 1))
1070 tty->minimum_to_wake = (minimum - (b - buf));
1072 if (!input_available_p(tty, 0)) {
1073 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1077 if (tty_hung_up_p(file))
1081 if (file->f_flags & O_NONBLOCK) {
1085 if (signal_pending(current)) {
1086 retval = -ERESTARTSYS;
1089 clear_bit(TTY_DONT_FLIP, &tty->flags);
1090 timeout = schedule_timeout(timeout);
1091 set_bit(TTY_DONT_FLIP, &tty->flags);
1094 __set_current_state(TASK_RUNNING);
1096 /* Deal with packet mode. */
1097 if (tty->packet && b == buf) {
1098 if (put_user(TIOCPKT_DATA, b++)) {
1107 /* N.B. avoid overrun if nr == 0 */
1108 while (nr && tty->read_cnt) {
1111 eol = test_and_clear_bit(tty->read_tail,
1113 c = tty->read_buf[tty->read_tail];
1114 spin_lock_irqsave(&tty->read_lock, flags);
1115 tty->read_tail = ((tty->read_tail+1) &
1116 (N_TTY_BUF_SIZE-1));
1119 /* this test should be redundant:
1120 * we shouldn't be reading data if
1123 if (--tty->canon_data < 0)
1124 tty->canon_data = 0;
1126 spin_unlock_irqrestore(&tty->read_lock, flags);
1128 if (!eol || (c != __DISABLED_CHAR)) {
1129 if (put_user(c, b++)) {
1143 uncopied = copy_from_read_buf(tty, &b, &nr);
1144 uncopied += copy_from_read_buf(tty, &b, &nr);
1151 /* If there is enough space in the read buffer now, let the
1152 * low-level driver know. We use n_tty_chars_in_buffer() to
1153 * check the buffer, as it now knows about canonical mode.
1154 * Otherwise, if the driver is throttled and the line is
1155 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1156 * we won't get any more characters.
1158 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE)
1159 check_unthrottle(tty);
1161 if (b - buf >= minimum)
1166 clear_bit(TTY_DONT_FLIP, &tty->flags);
1167 up(&tty->atomic_read);
1168 remove_wait_queue(&tty->read_wait, &wait);
1170 if (!waitqueue_active(&tty->read_wait))
1171 tty->minimum_to_wake = minimum;
1173 __set_current_state(TASK_RUNNING);
1178 clear_bit(TTY_PUSH, &tty->flags);
1179 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1185 static ssize_t write_chan(struct tty_struct * tty, struct file * file,
1186 const unsigned char __user * buf, size_t nr)
1188 const unsigned char __user *b = buf;
1189 DECLARE_WAITQUEUE(wait, current);
1193 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1194 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
1195 retval = tty_check_change(tty);
1200 add_wait_queue(&tty->write_wait, &wait);
1202 set_current_state(TASK_INTERRUPTIBLE);
1203 if (signal_pending(current)) {
1204 retval = -ERESTARTSYS;
1207 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1211 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1213 ssize_t num = opost_block(tty, b, nr);
1225 if (opost(c, tty) < 0)
1229 if (tty->driver->flush_chars)
1230 tty->driver->flush_chars(tty);
1232 c = tty->driver->write(tty, 1, b, nr);
1242 if (file->f_flags & O_NONBLOCK) {
1249 __set_current_state(TASK_RUNNING);
1250 remove_wait_queue(&tty->write_wait, &wait);
1251 return (b - buf) ? b - buf : retval;
1254 /* Called without the kernel lock held - fine */
1255 static unsigned int normal_poll(struct tty_struct * tty, struct file * file, poll_table *wait)
1257 unsigned int mask = 0;
1259 poll_wait(file, &tty->read_wait, wait);
1260 poll_wait(file, &tty->write_wait, wait);
1261 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
1262 mask |= POLLIN | POLLRDNORM;
1263 if (tty->packet && tty->link->ctrl_status)
1264 mask |= POLLPRI | POLLIN | POLLRDNORM;
1265 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
1267 if (tty_hung_up_p(file))
1269 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
1270 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
1271 tty->minimum_to_wake = MIN_CHAR(tty);
1273 tty->minimum_to_wake = 1;
1275 if (tty->driver->chars_in_buffer(tty) < WAKEUP_CHARS &&
1276 tty->driver->write_room(tty) > 0)
1277 mask |= POLLOUT | POLLWRNORM;
1281 struct tty_ldisc tty_ldisc_N_TTY = {
1282 TTY_LDISC_MAGIC, /* magic */
1286 n_tty_open, /* open */
1287 n_tty_close, /* close */
1288 n_tty_flush_buffer, /* flush_buffer */
1289 n_tty_chars_in_buffer, /* chars_in_buffer */
1290 read_chan, /* read */
1291 write_chan, /* write */
1292 n_tty_ioctl, /* ioctl */
1293 n_tty_set_termios, /* set_termios */
1294 normal_poll, /* poll */
1295 n_tty_receive_buf, /* receive_buf */
1296 n_tty_receive_room, /* receive_room */
1297 n_tty_write_wakeup /* write_wakeup */