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)
65 int prio = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
67 if (PAGE_SIZE != N_TTY_BUF_SIZE)
68 return kmalloc(N_TTY_BUF_SIZE, prio);
70 return (unsigned char *)__get_free_page(prio);
73 static inline void free_buf(unsigned char *buf)
75 if (PAGE_SIZE != N_TTY_BUF_SIZE)
78 free_page((unsigned long) buf);
81 static inline void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
83 if (tty->read_cnt < N_TTY_BUF_SIZE) {
84 tty->read_buf[tty->read_head] = c;
85 tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
90 static inline void put_tty_queue(unsigned char c, struct tty_struct *tty)
94 * The problem of stomping on the buffers ends here.
95 * Why didn't anyone see this one coming? --AJK
97 spin_lock_irqsave(&tty->read_lock, flags);
98 put_tty_queue_nolock(c, tty);
99 spin_unlock_irqrestore(&tty->read_lock, flags);
103 * Check whether to call the driver.unthrottle function.
104 * We test the TTY_THROTTLED bit first so that it always
105 * indicates the current state.
107 static void check_unthrottle(struct tty_struct * tty)
110 test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
111 tty->driver->unthrottle)
112 tty->driver->unthrottle(tty);
116 * Reset the read buffer counters, clear the flags,
117 * and make sure the driver is unthrottled. Called
118 * from n_tty_open() and n_tty_flush_buffer().
120 static void reset_buffer_flags(struct tty_struct *tty)
124 spin_lock_irqsave(&tty->read_lock, flags);
125 tty->read_head = tty->read_tail = tty->read_cnt = 0;
126 spin_unlock_irqrestore(&tty->read_lock, flags);
127 tty->canon_head = tty->canon_data = tty->erasing = 0;
128 memset(&tty->read_flags, 0, sizeof tty->read_flags);
129 check_unthrottle(tty);
133 * Flush the input buffer
135 void n_tty_flush_buffer(struct tty_struct * tty)
137 /* clear everything and unthrottle the driver */
138 reset_buffer_flags(tty);
143 if (tty->link->packet) {
144 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
145 wake_up_interruptible(&tty->link->read_wait);
150 * Return number of characters buffered to be delivered to user
152 ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
157 spin_lock_irqsave(&tty->read_lock, flags);
160 } else if (tty->canon_data) {
161 n = (tty->canon_head > tty->read_tail) ?
162 tty->canon_head - tty->read_tail :
163 tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
165 spin_unlock_irqrestore(&tty->read_lock, flags);
169 static inline int is_utf8_continuation(unsigned char c)
171 return (c & 0xc0) == 0x80;
174 static inline int is_continuation(unsigned char c, struct tty_struct *tty)
176 return I_IUTF8(tty) && is_utf8_continuation(c);
180 * Perform OPOST processing. Returns -1 when the output device is
181 * full and the character must be retried.
183 static int opost(unsigned char c, struct tty_struct *tty)
187 space = tty->driver->write_room(tty);
199 tty->driver->put_char(tty, '\r');
202 tty->canon_column = tty->column;
205 if (O_ONOCR(tty) && tty->column == 0)
210 tty->canon_column = tty->column = 0;
213 tty->canon_column = tty->column = 0;
216 spaces = 8 - (tty->column & 7);
217 if (O_TABDLY(tty) == XTABS) {
220 tty->column += spaces;
221 tty->driver->write(tty, 0, " ", spaces);
224 tty->column += spaces;
233 if (!iscntrl(c) && !is_continuation(c, tty))
238 tty->driver->put_char(tty, c);
243 * opost_block --- to speed up block console writes, among other
246 static ssize_t opost_block(struct tty_struct * tty,
247 const unsigned char __user * inbuf, unsigned int nr)
254 space = tty->driver->write_room(tty);
259 if (nr > sizeof(buf))
262 if (copy_from_user(buf, inbuf, nr))
265 for (i = 0, cp = buf; i < nr; i++, cp++) {
272 tty->canon_column = tty->column;
275 if (O_ONOCR(tty) && tty->column == 0)
280 tty->canon_column = tty->column = 0;
283 tty->canon_column = tty->column = 0;
300 if (tty->driver->flush_chars)
301 tty->driver->flush_chars(tty);
302 i = tty->driver->write(tty, 0, buf, i);
308 static inline void put_char(unsigned char c, struct tty_struct *tty)
310 tty->driver->put_char(tty, c);
313 /* Must be called only when L_ECHO(tty) is true. */
315 static void echo_char(unsigned char c, struct tty_struct *tty)
317 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
319 put_char(c ^ 0100, tty);
325 static inline void finish_erasing(struct tty_struct *tty)
334 static void eraser(unsigned char c, struct tty_struct *tty)
336 enum { ERASE, WERASE, KILL } kill_type;
337 int head, seen_alnums, cnt;
340 if (tty->read_head == tty->canon_head) {
341 /* opost('\a', tty); */ /* what do you think? */
344 if (c == ERASE_CHAR(tty))
346 else if (c == WERASE_CHAR(tty))
350 spin_lock_irqsave(&tty->read_lock, flags);
351 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
352 (N_TTY_BUF_SIZE - 1));
353 tty->read_head = tty->canon_head;
354 spin_unlock_irqrestore(&tty->read_lock, flags);
357 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
358 spin_lock_irqsave(&tty->read_lock, flags);
359 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
360 (N_TTY_BUF_SIZE - 1));
361 tty->read_head = tty->canon_head;
362 spin_unlock_irqrestore(&tty->read_lock, flags);
364 echo_char(KILL_CHAR(tty), tty);
365 /* Add a newline if ECHOK is on and ECHOKE is off. */
374 while (tty->read_head != tty->canon_head) {
375 head = tty->read_head;
377 /* erase a single possibly multibyte character */
379 head = (head - 1) & (N_TTY_BUF_SIZE-1);
380 c = tty->read_buf[head];
381 } while (is_continuation(c, tty) && head != tty->canon_head);
383 /* do not partially erase */
384 if (is_continuation(c, tty))
387 if (kill_type == WERASE) {
388 /* Equivalent to BSD's ALTWERASE. */
389 if (isalnum(c) || c == '_')
391 else if (seen_alnums)
394 cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
395 spin_lock_irqsave(&tty->read_lock, flags);
396 tty->read_head = head;
397 tty->read_cnt -= cnt;
398 spin_unlock_irqrestore(&tty->read_lock, flags);
400 if (L_ECHOPRT(tty)) {
406 /* if cnt > 1, output a multi-byte character */
409 head = (head+1) & (N_TTY_BUF_SIZE-1);
410 put_char(tty->read_buf[head], tty);
412 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
413 echo_char(ERASE_CHAR(tty), tty);
414 } else if (c == '\t') {
415 unsigned int col = tty->canon_column;
416 unsigned long tail = tty->canon_head;
418 /* Find the column of the last char. */
419 while (tail != tty->read_head) {
420 c = tty->read_buf[tail];
423 else if (iscntrl(c)) {
426 } else if (!is_continuation(c, tty))
428 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
431 /* should never happen */
432 if (tty->column > 0x80000000)
435 /* Now backup to that column. */
436 while (tty->column > col) {
437 /* Can't use opost here. */
443 if (iscntrl(c) && L_ECHOCTL(tty)) {
450 if (!iscntrl(c) || L_ECHOCTL(tty)) {
459 if (kill_type == ERASE)
462 if (tty->read_head == tty->canon_head)
466 static inline void isig(int sig, struct tty_struct *tty, int flush)
469 kill_pg(tty->pgrp, sig, 1);
470 if (flush || !L_NOFLSH(tty)) {
471 n_tty_flush_buffer(tty);
472 if (tty->driver->flush_buffer)
473 tty->driver->flush_buffer(tty);
477 static inline void n_tty_receive_break(struct tty_struct *tty)
482 isig(SIGINT, tty, 1);
486 put_tty_queue('\377', tty);
487 put_tty_queue('\0', tty);
489 put_tty_queue('\0', tty);
490 wake_up_interruptible(&tty->read_wait);
493 static inline void n_tty_receive_overrun(struct tty_struct *tty)
498 if (time_before(tty->overrun_time, jiffies - HZ)) {
499 printk("%s: %d input overrun(s)\n", tty_name(tty, buf),
501 tty->overrun_time = jiffies;
502 tty->num_overrun = 0;
506 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
513 put_tty_queue('\377', tty);
514 put_tty_queue('\0', tty);
515 put_tty_queue(c, tty);
516 } else if (I_INPCK(tty))
517 put_tty_queue('\0', tty);
519 put_tty_queue(c, tty);
520 wake_up_interruptible(&tty->read_wait);
523 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
528 put_tty_queue(c, tty);
532 if (tty->stopped && !tty->flow_stopped &&
533 I_IXON(tty) && I_IXANY(tty)) {
540 if (I_IUCLC(tty) && L_IEXTEN(tty))
545 if (c == START_CHAR(tty))
547 else if (c == STOP_CHAR(tty))
554 * If the previous character was LNEXT, or we know that this
555 * character is not one of the characters that we'll have to
556 * handle specially, do shortcut processing to speed things
559 if (!test_bit(c, tty->process_char_map) || tty->lnext) {
563 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
564 put_char('\a', tty); /* beep if no space */
567 /* Record the column of first canon char. */
568 if (tty->canon_head == tty->read_head)
569 tty->canon_column = tty->column;
572 if (I_PARMRK(tty) && c == (unsigned char) '\377')
573 put_tty_queue(c, tty);
574 put_tty_queue(c, tty);
583 } else if (c == '\n' && I_INLCR(tty))
586 if (c == START_CHAR(tty)) {
590 if (c == STOP_CHAR(tty)) {
598 if (c == INTR_CHAR(tty))
601 if (c == QUIT_CHAR(tty))
604 if (c == SUSP_CHAR(tty)) {
606 isig(signal, tty, 0);
611 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
612 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
616 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
620 if (L_ECHOCTL(tty)) {
627 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
629 unsigned long tail = tty->canon_head;
634 while (tail != tty->read_head) {
635 echo_char(tty->read_buf[tail], tty);
636 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
641 if (L_ECHO(tty) || L_ECHONL(tty)) {
642 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
650 if (c == EOF_CHAR(tty)) {
651 if (tty->canon_head != tty->read_head)
652 set_bit(TTY_PUSH, &tty->flags);
656 if ((c == EOL_CHAR(tty)) ||
657 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
659 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
662 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
666 /* Record the column of first canon char. */
667 if (tty->canon_head == tty->read_head)
668 tty->canon_column = tty->column;
672 * XXX does PARMRK doubling happen for
673 * EOL_CHAR and EOL2_CHAR?
675 if (I_PARMRK(tty) && c == (unsigned char) '\377')
676 put_tty_queue(c, tty);
679 spin_lock_irqsave(&tty->read_lock, flags);
680 set_bit(tty->read_head, tty->read_flags);
681 put_tty_queue_nolock(c, tty);
682 tty->canon_head = tty->read_head;
684 spin_unlock_irqrestore(&tty->read_lock, flags);
685 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
686 if (waitqueue_active(&tty->read_wait))
687 wake_up_interruptible(&tty->read_wait);
694 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
695 put_char('\a', tty); /* beep if no space */
701 /* Record the column of first canon char. */
702 if (tty->canon_head == tty->read_head)
703 tty->canon_column = tty->column;
708 if (I_PARMRK(tty) && c == (unsigned char) '\377')
709 put_tty_queue(c, tty);
711 put_tty_queue(c, tty);
714 static int n_tty_receive_room(struct tty_struct *tty)
716 int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
719 * If we are doing input canonicalization, and there are no
720 * pending newlines, let characters through without limit, so
721 * that erase characters will be handled. Other excess
722 * characters will be beeped.
724 if (tty->icanon && !tty->canon_data)
725 return N_TTY_BUF_SIZE;
733 * Required for the ptys, serial driver etc. since processes
734 * that attach themselves to the master and rely on ASYNC
735 * IO must be woken up
738 static void n_tty_write_wakeup(struct tty_struct *tty)
742 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
743 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
748 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
751 const unsigned char *p;
752 char *f, flags = TTY_NORMAL;
755 unsigned long cpuflags;
761 spin_lock_irqsave(&tty->read_lock, cpuflags);
762 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
763 N_TTY_BUF_SIZE - tty->read_head);
765 memcpy(tty->read_buf + tty->read_head, cp, i);
766 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
771 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
772 N_TTY_BUF_SIZE - tty->read_head);
774 memcpy(tty->read_buf + tty->read_head, cp, i);
775 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
777 spin_unlock_irqrestore(&tty->read_lock, cpuflags);
779 for (i=count, p = cp, f = fp; i; i--, p++) {
784 n_tty_receive_char(tty, *p);
787 n_tty_receive_break(tty);
791 n_tty_receive_parity_error(tty, *p);
794 n_tty_receive_overrun(tty);
797 printk("%s: unknown flag %d\n",
798 tty_name(tty, buf), flags);
802 if (tty->driver->flush_chars)
803 tty->driver->flush_chars(tty);
806 if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
807 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
808 if (waitqueue_active(&tty->read_wait))
809 wake_up_interruptible(&tty->read_wait);
813 * Check the remaining room for the input canonicalization
814 * mode. We don't want to throttle the driver if we're in
815 * canonical mode and don't have a newline yet!
817 if (n_tty_receive_room(tty) < TTY_THRESHOLD_THROTTLE) {
818 /* check TTY_THROTTLED first so it indicates our state */
819 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
820 tty->driver->throttle)
821 tty->driver->throttle(tty);
825 int is_ignored(int sig)
827 return (sigismember(¤t->blocked, sig) ||
828 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
831 static void n_tty_set_termios(struct tty_struct *tty, struct termios * old)
836 tty->icanon = (L_ICANON(tty) != 0);
837 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
842 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
843 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
844 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
846 local_irq_disable(); // FIXME: is this safe?
847 memset(tty->process_char_map, 0, 256/8);
849 if (I_IGNCR(tty) || I_ICRNL(tty))
850 set_bit('\r', tty->process_char_map);
852 set_bit('\n', tty->process_char_map);
855 set_bit(ERASE_CHAR(tty), tty->process_char_map);
856 set_bit(KILL_CHAR(tty), tty->process_char_map);
857 set_bit(EOF_CHAR(tty), tty->process_char_map);
858 set_bit('\n', tty->process_char_map);
859 set_bit(EOL_CHAR(tty), tty->process_char_map);
861 set_bit(WERASE_CHAR(tty),
862 tty->process_char_map);
863 set_bit(LNEXT_CHAR(tty),
864 tty->process_char_map);
865 set_bit(EOL2_CHAR(tty),
866 tty->process_char_map);
868 set_bit(REPRINT_CHAR(tty),
869 tty->process_char_map);
873 set_bit(START_CHAR(tty), tty->process_char_map);
874 set_bit(STOP_CHAR(tty), tty->process_char_map);
877 set_bit(INTR_CHAR(tty), tty->process_char_map);
878 set_bit(QUIT_CHAR(tty), tty->process_char_map);
879 set_bit(SUSP_CHAR(tty), tty->process_char_map);
881 clear_bit(__DISABLED_CHAR, tty->process_char_map);
882 local_irq_enable(); // FIXME: is this safe?
887 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
888 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
889 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
896 static void n_tty_close(struct tty_struct *tty)
898 n_tty_flush_buffer(tty);
900 free_buf(tty->read_buf);
901 tty->read_buf = NULL;
905 static int n_tty_open(struct tty_struct *tty)
910 if (!tty->read_buf) {
911 tty->read_buf = alloc_buf();
915 memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
916 reset_buffer_flags(tty);
918 n_tty_set_termios(tty, NULL);
919 tty->minimum_to_wake = 1;
924 static inline int input_available_p(struct tty_struct *tty, int amt)
929 } else if (tty->read_cnt >= (amt ? amt : 1))
936 * Helper function to speed up read_chan. It is only called when
937 * ICANON is off; it copies characters straight from the tty queue to
938 * user space directly. It can be profitably called twice; once to
939 * drain the space from the tail pointer to the (physical) end of the
940 * buffer, and once to drain the space from the (physical) beginning of
941 * the buffer to head pointer.
943 static inline int copy_from_read_buf(struct tty_struct *tty,
944 unsigned char __user **b,
953 spin_lock_irqsave(&tty->read_lock, flags);
954 n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
955 n = min((ssize_t)*nr, n);
956 spin_unlock_irqrestore(&tty->read_lock, flags);
959 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
961 spin_lock_irqsave(&tty->read_lock, flags);
962 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
964 spin_unlock_irqrestore(&tty->read_lock, flags);
971 extern ssize_t redirected_tty_write(struct file *,const char *,size_t,loff_t *);
973 static ssize_t read_chan(struct tty_struct *tty, struct file *file,
974 unsigned char __user *buf, size_t nr)
976 unsigned char __user *b = buf;
977 DECLARE_WAITQUEUE(wait, current);
987 if (!tty->read_buf) {
988 printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
992 /* Job control check -- must be done at start and after
993 every sleep (POSIX.1 7.1.1.4). */
994 /* NOTE: not yet done after every sleep pending a thorough
995 check of the logic of this change. -- jlc */
996 /* don't stop on /dev/console */
997 if (file->f_op->write != redirected_tty_write &&
998 current->signal->tty == tty) {
1000 printk("read_chan: tty->pgrp <= 0!\n");
1001 else if (process_group(current) != tty->pgrp) {
1002 if (is_ignored(SIGTTIN) ||
1003 is_orphaned_pgrp(process_group(current)))
1005 kill_pg(process_group(current), SIGTTIN, 1);
1006 return -ERESTARTSYS;
1011 timeout = MAX_SCHEDULE_TIMEOUT;
1013 time = (HZ / 10) * TIME_CHAR(tty);
1014 minimum = MIN_CHAR(tty);
1017 tty->minimum_to_wake = 1;
1018 else if (!waitqueue_active(&tty->read_wait) ||
1019 (tty->minimum_to_wake > minimum))
1020 tty->minimum_to_wake = minimum;
1027 tty->minimum_to_wake = minimum = 1;
1031 if (file->f_flags & O_NONBLOCK) {
1032 if (down_trylock(&tty->atomic_read))
1036 if (down_interruptible(&tty->atomic_read))
1037 return -ERESTARTSYS;
1040 add_wait_queue(&tty->read_wait, &wait);
1041 set_bit(TTY_DONT_FLIP, &tty->flags);
1043 /* First test for status change. */
1044 if (tty->packet && tty->link->ctrl_status) {
1048 cs = tty->link->ctrl_status;
1049 tty->link->ctrl_status = 0;
1050 if (put_user(cs, b++)) {
1058 /* This statement must be first before checking for input
1059 so that any interrupt will set the state back to
1061 set_current_state(TASK_INTERRUPTIBLE);
1063 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1064 ((minimum - (b - buf)) >= 1))
1065 tty->minimum_to_wake = (minimum - (b - buf));
1067 if (!input_available_p(tty, 0)) {
1068 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1072 if (tty_hung_up_p(file))
1076 if (file->f_flags & O_NONBLOCK) {
1080 if (signal_pending(current)) {
1081 retval = -ERESTARTSYS;
1084 clear_bit(TTY_DONT_FLIP, &tty->flags);
1085 timeout = schedule_timeout(timeout);
1086 set_bit(TTY_DONT_FLIP, &tty->flags);
1089 __set_current_state(TASK_RUNNING);
1091 /* Deal with packet mode. */
1092 if (tty->packet && b == buf) {
1093 if (put_user(TIOCPKT_DATA, b++)) {
1102 /* N.B. avoid overrun if nr == 0 */
1103 while (nr && tty->read_cnt) {
1106 eol = test_and_clear_bit(tty->read_tail,
1108 c = tty->read_buf[tty->read_tail];
1109 spin_lock_irqsave(&tty->read_lock, flags);
1110 tty->read_tail = ((tty->read_tail+1) &
1111 (N_TTY_BUF_SIZE-1));
1114 /* this test should be redundant:
1115 * we shouldn't be reading data if
1118 if (--tty->canon_data < 0)
1119 tty->canon_data = 0;
1121 spin_unlock_irqrestore(&tty->read_lock, flags);
1123 if (!eol || (c != __DISABLED_CHAR)) {
1124 if (put_user(c, b++)) {
1138 uncopied = copy_from_read_buf(tty, &b, &nr);
1139 uncopied += copy_from_read_buf(tty, &b, &nr);
1146 /* If there is enough space in the read buffer now, let the
1147 * low-level driver know. We use n_tty_chars_in_buffer() to
1148 * check the buffer, as it now knows about canonical mode.
1149 * Otherwise, if the driver is throttled and the line is
1150 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1151 * we won't get any more characters.
1153 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE)
1154 check_unthrottle(tty);
1156 if (b - buf >= minimum)
1161 clear_bit(TTY_DONT_FLIP, &tty->flags);
1162 up(&tty->atomic_read);
1163 remove_wait_queue(&tty->read_wait, &wait);
1165 if (!waitqueue_active(&tty->read_wait))
1166 tty->minimum_to_wake = minimum;
1168 __set_current_state(TASK_RUNNING);
1173 clear_bit(TTY_PUSH, &tty->flags);
1174 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1180 static ssize_t write_chan(struct tty_struct * tty, struct file * file,
1181 const unsigned char __user * buf, size_t nr)
1183 const unsigned char __user *b = buf;
1184 DECLARE_WAITQUEUE(wait, current);
1188 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1189 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
1190 retval = tty_check_change(tty);
1195 add_wait_queue(&tty->write_wait, &wait);
1197 set_current_state(TASK_INTERRUPTIBLE);
1198 if (signal_pending(current)) {
1199 retval = -ERESTARTSYS;
1202 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1206 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1208 ssize_t num = opost_block(tty, b, nr);
1220 if (opost(c, tty) < 0)
1224 if (tty->driver->flush_chars)
1225 tty->driver->flush_chars(tty);
1227 c = tty->driver->write(tty, 1, b, nr);
1237 if (file->f_flags & O_NONBLOCK) {
1244 __set_current_state(TASK_RUNNING);
1245 remove_wait_queue(&tty->write_wait, &wait);
1246 return (b - buf) ? b - buf : retval;
1249 /* Called without the kernel lock held - fine */
1250 static unsigned int normal_poll(struct tty_struct * tty, struct file * file, poll_table *wait)
1252 unsigned int mask = 0;
1254 poll_wait(file, &tty->read_wait, wait);
1255 poll_wait(file, &tty->write_wait, wait);
1256 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
1257 mask |= POLLIN | POLLRDNORM;
1258 if (tty->packet && tty->link->ctrl_status)
1259 mask |= POLLPRI | POLLIN | POLLRDNORM;
1260 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
1262 if (tty_hung_up_p(file))
1264 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
1265 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
1266 tty->minimum_to_wake = MIN_CHAR(tty);
1268 tty->minimum_to_wake = 1;
1270 if (tty->driver->chars_in_buffer(tty) < WAKEUP_CHARS &&
1271 tty->driver->write_room(tty) > 0)
1272 mask |= POLLOUT | POLLWRNORM;
1276 struct tty_ldisc tty_ldisc_N_TTY = {
1277 TTY_LDISC_MAGIC, /* magic */
1281 n_tty_open, /* open */
1282 n_tty_close, /* close */
1283 n_tty_flush_buffer, /* flush_buffer */
1284 n_tty_chars_in_buffer, /* chars_in_buffer */
1285 read_chan, /* read */
1286 write_chan, /* write */
1287 n_tty_ioctl, /* ioctl */
1288 n_tty_set_termios, /* set_termios */
1289 normal_poll, /* poll */
1290 n_tty_receive_buf, /* receive_buf */
1291 n_tty_receive_room, /* receive_room */
1292 n_tty_write_wakeup /* write_wakeup */