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 Public
22 #include <linux/types.h>
23 #include <linux/major.h>
24 #include <linux/errno.h>
25 #include <linux/signal.h>
26 #include <linux/fcntl.h>
27 #include <linux/sched.h>
28 #include <linux/interrupt.h>
29 #include <linux/tty.h>
30 #include <linux/timer.h>
31 #include <linux/ctype.h>
34 #include <linux/string.h>
35 #include <linux/malloc.h>
36 #include <linux/poll.h>
38 #include <asm/uaccess.h>
39 #include <asm/system.h>
40 #include <asm/bitops.h>
42 #define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
43 #define SYSCONS_DEV MKDEV(TTYAUX_MAJOR,1)
46 #define MIN(a,b) ((a) < (b) ? (a) : (b))
49 /* number of characters left in xmit buffer before select has we have room */
50 #define WAKEUP_CHARS 256
53 * This defines the low- and high-watermarks for throttling and
54 * unthrottling the TTY driver. These watermarks are used for
55 * controlling the space in the read buffer.
57 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
58 #define TTY_THRESHOLD_UNTHROTTLE 128
60 static inline void put_tty_queue(unsigned char c
, struct tty_struct
*tty
)
62 if (tty
->read_cnt
< N_TTY_BUF_SIZE
) {
63 tty
->read_buf
[tty
->read_head
] = c
;
64 tty
->read_head
= (tty
->read_head
+ 1) & (N_TTY_BUF_SIZE
-1);
70 * Check whether to call the driver.unthrottle function.
71 * We test the TTY_THROTTLED bit first so that it always
72 * indicates the current state.
74 static void check_unthrottle(struct tty_struct
* tty
)
77 test_and_clear_bit(TTY_THROTTLED
, &tty
->flags
) &&
78 tty
->driver
.unthrottle
)
79 tty
->driver
.unthrottle(tty
);
83 * Reset the read buffer counters, clear the flags,
84 * and make sure the driver is unthrottled. Called
85 * from n_tty_open() and n_tty_flush_buffer().
87 static void reset_buffer_flags(struct tty_struct
*tty
)
89 tty
->read_head
= tty
->read_tail
= tty
->read_cnt
= 0;
90 tty
->canon_head
= tty
->canon_data
= tty
->erasing
= 0;
91 memset(&tty
->read_flags
, 0, sizeof tty
->read_flags
);
92 check_unthrottle(tty
);
96 * Flush the input buffer
98 void n_tty_flush_buffer(struct tty_struct
* tty
)
100 /* clear everything and unthrottle the driver */
101 reset_buffer_flags(tty
);
106 if (tty
->link
->packet
) {
107 tty
->ctrl_status
|= TIOCPKT_FLUSHREAD
;
108 wake_up_interruptible(&tty
->link
->read_wait
);
113 * Return number of characters buffered to be delivered to user
115 ssize_t
n_tty_chars_in_buffer(struct tty_struct
*tty
)
118 if (!tty
->canon_data
) return 0;
120 return (tty
->canon_head
> tty
->read_tail
) ?
121 tty
->canon_head
- tty
->read_tail
:
122 tty
->canon_head
+ (N_TTY_BUF_SIZE
- tty
->read_tail
);
124 return tty
->read_cnt
;
128 * Perform OPOST processing. Returns -1 when the output device is
129 * full and the character must be retried.
131 static int opost(unsigned char c
, struct tty_struct
*tty
)
135 space
= tty
->driver
.write_room(tty
);
147 tty
->driver
.put_char(tty
, '\r');
150 tty
->canon_column
= tty
->column
;
153 if (O_ONOCR(tty
) && tty
->column
== 0)
158 tty
->canon_column
= tty
->column
= 0;
161 tty
->canon_column
= tty
->column
= 0;
164 spaces
= 8 - (tty
->column
& 7);
165 if (O_TABDLY(tty
) == XTABS
) {
168 tty
->column
+= spaces
;
169 tty
->driver
.write(tty
, 0, " ", spaces
);
172 tty
->column
+= spaces
;
186 tty
->driver
.put_char(tty
, c
);
191 * opost_block --- to speed up block console writes, among other
194 static ssize_t
opost_block(struct tty_struct
* tty
,
195 const unsigned char * inbuf
, unsigned int nr
)
202 space
= tty
->driver
.write_room(tty
);
207 if (nr
> sizeof(buf
))
209 nr
-= copy_from_user(buf
, inbuf
, nr
);
213 for (i
= 0, cp
= buf
; i
< nr
; i
++, cp
++) {
220 tty
->canon_column
= tty
->column
;
223 if (O_ONOCR(tty
) && tty
->column
== 0)
228 tty
->canon_column
= tty
->column
= 0;
231 tty
->canon_column
= tty
->column
= 0;
248 if (tty
->driver
.flush_chars
)
249 tty
->driver
.flush_chars(tty
);
250 i
= tty
->driver
.write(tty
, 0, buf
, i
);
256 static inline void put_char(unsigned char c
, struct tty_struct
*tty
)
258 tty
->driver
.put_char(tty
, c
);
261 /* Must be called only when L_ECHO(tty) is true. */
263 static void echo_char(unsigned char c
, struct tty_struct
*tty
)
265 if (L_ECHOCTL(tty
) && iscntrl(c
) && c
!= '\t') {
267 put_char(c
^ 0100, tty
);
273 static inline void finish_erasing(struct tty_struct
*tty
)
282 static void eraser(unsigned char c
, struct tty_struct
*tty
)
284 enum { ERASE
, WERASE
, KILL
} kill_type
;
285 int head
, seen_alnums
;
287 if (tty
->read_head
== tty
->canon_head
) {
288 /* opost('\a', tty); */ /* what do you think? */
291 if (c
== ERASE_CHAR(tty
))
293 else if (c
== WERASE_CHAR(tty
))
297 tty
->read_cnt
-= ((tty
->read_head
- tty
->canon_head
) &
298 (N_TTY_BUF_SIZE
- 1));
299 tty
->read_head
= tty
->canon_head
;
302 if (!L_ECHOK(tty
) || !L_ECHOKE(tty
) || !L_ECHOE(tty
)) {
303 tty
->read_cnt
-= ((tty
->read_head
- tty
->canon_head
) &
304 (N_TTY_BUF_SIZE
- 1));
305 tty
->read_head
= tty
->canon_head
;
307 echo_char(KILL_CHAR(tty
), tty
);
308 /* Add a newline if ECHOK is on and ECHOKE is off. */
317 while (tty
->read_head
!= tty
->canon_head
) {
318 head
= (tty
->read_head
- 1) & (N_TTY_BUF_SIZE
-1);
319 c
= tty
->read_buf
[head
];
320 if (kill_type
== WERASE
) {
321 /* Equivalent to BSD's ALTWERASE. */
322 if (isalnum(c
) || c
== '_')
324 else if (seen_alnums
)
327 tty
->read_head
= head
;
330 if (L_ECHOPRT(tty
)) {
337 } else if (kill_type
== ERASE
&& !L_ECHOE(tty
)) {
338 echo_char(ERASE_CHAR(tty
), tty
);
339 } else if (c
== '\t') {
340 unsigned int col
= tty
->canon_column
;
341 unsigned long tail
= tty
->canon_head
;
343 /* Find the column of the last char. */
344 while (tail
!= tty
->read_head
) {
345 c
= tty
->read_buf
[tail
];
348 else if (iscntrl(c
)) {
353 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
356 /* should never happen */
357 if (tty
->column
> 0x80000000)
360 /* Now backup to that column. */
361 while (tty
->column
> col
) {
362 /* Can't use opost here. */
368 if (iscntrl(c
) && L_ECHOCTL(tty
)) {
375 if (!iscntrl(c
) || L_ECHOCTL(tty
)) {
384 if (kill_type
== ERASE
)
387 if (tty
->read_head
== tty
->canon_head
)
391 static inline void isig(int sig
, struct tty_struct
*tty
, int flush
)
394 kill_pg(tty
->pgrp
, sig
, 1);
395 if (flush
|| !L_NOFLSH(tty
)) {
396 n_tty_flush_buffer(tty
);
397 if (tty
->driver
.flush_buffer
)
398 tty
->driver
.flush_buffer(tty
);
402 static inline void n_tty_receive_break(struct tty_struct
*tty
)
407 isig(SIGINT
, tty
, 1);
411 put_tty_queue('\377', tty
);
412 put_tty_queue('\0', tty
);
414 put_tty_queue('\0', tty
);
415 wake_up_interruptible(&tty
->read_wait
);
418 static inline void n_tty_receive_overrun(struct tty_struct
*tty
)
423 if (time_before(tty
->overrun_time
, jiffies
- HZ
)) {
424 printk("%s: %d input overrun(s)\n", tty_name(tty
, buf
),
426 tty
->overrun_time
= jiffies
;
427 tty
->num_overrun
= 0;
431 static inline void n_tty_receive_parity_error(struct tty_struct
*tty
,
438 put_tty_queue('\377', tty
);
439 put_tty_queue('\0', tty
);
440 put_tty_queue(c
, tty
);
441 } else if (I_INPCK(tty
))
442 put_tty_queue('\0', tty
);
444 put_tty_queue(c
, tty
);
445 wake_up_interruptible(&tty
->read_wait
);
448 static inline void n_tty_receive_char(struct tty_struct
*tty
, unsigned char c
)
451 put_tty_queue(c
, tty
);
455 if (tty
->stopped
&& !tty
->flow_stopped
&&
456 I_IXON(tty
) && I_IXANY(tty
)) {
463 if (I_IUCLC(tty
) && L_IEXTEN(tty
))
468 if (c
== START_CHAR(tty
))
470 else if (c
== STOP_CHAR(tty
))
477 * If the previous character was LNEXT, or we know that this
478 * character is not one of the characters that we'll have to
479 * handle specially, do shortcut processing to speed things
482 if (!test_bit(c
, &tty
->process_char_map
) || tty
->lnext
) {
486 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
487 put_char('\a', tty
); /* beep if no space */
490 /* Record the column of first canon char. */
491 if (tty
->canon_head
== tty
->read_head
)
492 tty
->canon_column
= tty
->column
;
495 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
496 put_tty_queue(c
, tty
);
497 put_tty_queue(c
, tty
);
506 } else if (c
== '\n' && I_INLCR(tty
))
509 if (c
== START_CHAR(tty
)) {
513 if (c
== STOP_CHAR(tty
)) {
521 if (c
== INTR_CHAR(tty
))
524 if (c
== QUIT_CHAR(tty
))
527 if (c
== SUSP_CHAR(tty
)) {
529 isig(signal
, tty
, 0);
534 if (c
== ERASE_CHAR(tty
) || c
== KILL_CHAR(tty
) ||
535 (c
== WERASE_CHAR(tty
) && L_IEXTEN(tty
))) {
539 if (c
== LNEXT_CHAR(tty
) && L_IEXTEN(tty
)) {
543 if (L_ECHOCTL(tty
)) {
550 if (c
== REPRINT_CHAR(tty
) && L_ECHO(tty
) &&
552 unsigned long tail
= tty
->canon_head
;
557 while (tail
!= tty
->read_head
) {
558 echo_char(tty
->read_buf
[tail
], tty
);
559 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
564 if (L_ECHO(tty
) || L_ECHONL(tty
)) {
565 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
573 if (c
== EOF_CHAR(tty
)) {
574 if (tty
->canon_head
!= tty
->read_head
)
575 set_bit(TTY_PUSH
, &tty
->flags
);
579 if ((c
== EOL_CHAR(tty
)) ||
580 (c
== EOL2_CHAR(tty
) && L_IEXTEN(tty
))) {
582 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
585 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
589 /* Record the column of first canon char. */
590 if (tty
->canon_head
== tty
->read_head
)
591 tty
->canon_column
= tty
->column
;
595 * XXX does PARMRK doubling happen for
596 * EOL_CHAR and EOL2_CHAR?
598 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
599 put_tty_queue(c
, tty
);
602 set_bit(tty
->read_head
, &tty
->read_flags
);
603 put_tty_queue(c
, tty
);
604 tty
->canon_head
= tty
->read_head
;
607 kill_fasync(tty
->fasync
, SIGIO
);
608 if (waitqueue_active(&tty
->read_wait
))
609 wake_up_interruptible(&tty
->read_wait
);
616 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
617 put_char('\a', tty
); /* beep if no space */
623 /* Record the column of first canon char. */
624 if (tty
->canon_head
== tty
->read_head
)
625 tty
->canon_column
= tty
->column
;
630 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
631 put_tty_queue(c
, tty
);
633 put_tty_queue(c
, tty
);
636 static int n_tty_receive_room(struct tty_struct
*tty
)
638 int left
= N_TTY_BUF_SIZE
- tty
->read_cnt
- 1;
641 * If we are doing input canonicalization, and there are no
642 * pending newlines, let characters through without limit, so
643 * that erase characters will be handled. Other excess
644 * characters will be beeped.
646 if (tty
->icanon
&& !tty
->canon_data
)
647 return N_TTY_BUF_SIZE
;
654 static void n_tty_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
657 const unsigned char *p
;
658 char *f
, flags
= TTY_NORMAL
;
666 i
= MIN(count
, MIN(N_TTY_BUF_SIZE
- tty
->read_cnt
,
667 N_TTY_BUF_SIZE
- tty
->read_head
));
668 memcpy(tty
->read_buf
+ tty
->read_head
, cp
, i
);
669 tty
->read_head
= (tty
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
674 i
= MIN(count
, MIN(N_TTY_BUF_SIZE
- tty
->read_cnt
,
675 N_TTY_BUF_SIZE
- tty
->read_head
));
676 memcpy(tty
->read_buf
+ tty
->read_head
, cp
, i
);
677 tty
->read_head
= (tty
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
680 for (i
=count
, p
= cp
, f
= fp
; i
; i
--, p
++) {
685 n_tty_receive_char(tty
, *p
);
688 n_tty_receive_break(tty
);
692 n_tty_receive_parity_error(tty
, *p
);
695 n_tty_receive_overrun(tty
);
698 printk("%s: unknown flag %d\n",
699 tty_name(tty
, buf
), flags
);
703 if (tty
->driver
.flush_chars
)
704 tty
->driver
.flush_chars(tty
);
707 if (!tty
->icanon
&& (tty
->read_cnt
>= tty
->minimum_to_wake
)) {
709 kill_fasync(tty
->fasync
, SIGIO
);
710 if (waitqueue_active(&tty
->read_wait
))
711 wake_up_interruptible(&tty
->read_wait
);
715 * Check the remaining room for the input canonicalization
716 * mode. We don't want to throttle the driver if we're in
717 * canonical mode and don't have a newline yet!
719 if (n_tty_receive_room(tty
) < TTY_THRESHOLD_THROTTLE
) {
720 /* check TTY_THROTTLED first so it indicates our state */
721 if (!test_and_set_bit(TTY_THROTTLED
, &tty
->flags
) &&
722 tty
->driver
.throttle
)
723 tty
->driver
.throttle(tty
);
727 int is_ignored(int sig
)
729 return (sigismember(¤t
->blocked
, sig
) ||
730 current
->sig
->action
[sig
-1].sa
.sa_handler
== SIG_IGN
);
733 static void n_tty_set_termios(struct tty_struct
*tty
, struct termios
* old
)
738 tty
->icanon
= (L_ICANON(tty
) != 0);
739 if (test_bit(TTY_HW_COOK_IN
, &tty
->flags
)) {
744 if (I_ISTRIP(tty
) || I_IUCLC(tty
) || I_IGNCR(tty
) ||
745 I_ICRNL(tty
) || I_INLCR(tty
) || L_ICANON(tty
) ||
746 I_IXON(tty
) || L_ISIG(tty
) || L_ECHO(tty
) ||
749 memset(tty
->process_char_map
, 0, 256/8);
751 if (I_IGNCR(tty
) || I_ICRNL(tty
))
752 set_bit('\r', &tty
->process_char_map
);
754 set_bit('\n', &tty
->process_char_map
);
757 set_bit(ERASE_CHAR(tty
), &tty
->process_char_map
);
758 set_bit(KILL_CHAR(tty
), &tty
->process_char_map
);
759 set_bit(EOF_CHAR(tty
), &tty
->process_char_map
);
760 set_bit('\n', &tty
->process_char_map
);
761 set_bit(EOL_CHAR(tty
), &tty
->process_char_map
);
763 set_bit(WERASE_CHAR(tty
),
764 &tty
->process_char_map
);
765 set_bit(LNEXT_CHAR(tty
),
766 &tty
->process_char_map
);
767 set_bit(EOL2_CHAR(tty
),
768 &tty
->process_char_map
);
770 set_bit(REPRINT_CHAR(tty
),
771 &tty
->process_char_map
);
775 set_bit(START_CHAR(tty
), &tty
->process_char_map
);
776 set_bit(STOP_CHAR(tty
), &tty
->process_char_map
);
779 set_bit(INTR_CHAR(tty
), &tty
->process_char_map
);
780 set_bit(QUIT_CHAR(tty
), &tty
->process_char_map
);
781 set_bit(SUSP_CHAR(tty
), &tty
->process_char_map
);
783 clear_bit(__DISABLED_CHAR
, &tty
->process_char_map
);
789 if ((I_IGNBRK(tty
) || (!I_BRKINT(tty
) && !I_PARMRK(tty
))) &&
790 (I_IGNPAR(tty
) || !I_INPCK(tty
)) &&
791 (tty
->driver
.flags
& TTY_DRIVER_REAL_RAW
))
798 static void n_tty_close(struct tty_struct
*tty
)
800 n_tty_flush_buffer(tty
);
802 free_page((unsigned long) tty
->read_buf
);
807 static int n_tty_open(struct tty_struct
*tty
)
812 if (!tty
->read_buf
) {
813 tty
->read_buf
= (unsigned char *)
814 get_free_page(in_interrupt() ? GFP_ATOMIC
: GFP_KERNEL
);
818 memset(tty
->read_buf
, 0, N_TTY_BUF_SIZE
);
819 reset_buffer_flags(tty
);
821 n_tty_set_termios(tty
, 0);
822 tty
->minimum_to_wake
= 1;
827 static inline int input_available_p(struct tty_struct
*tty
, int amt
)
832 } else if (tty
->read_cnt
>= (amt
? amt
: 1))
839 * Helper function to speed up read_chan. It is only called when
840 * ICANON is off; it copies characters straight from the tty queue to
841 * user space directly. It can be profitably called twice; once to
842 * drain the space from the tail pointer to the (physical) end of the
843 * buffer, and once to drain the space from the (physical) beginning of
844 * the buffer to head pointer.
846 static inline int copy_from_read_buf(struct tty_struct
*tty
,
855 n
= MIN(*nr
, MIN(tty
->read_cnt
, N_TTY_BUF_SIZE
- tty
->read_tail
));
857 retval
= copy_to_user(*b
, &tty
->read_buf
[tty
->read_tail
], n
);
859 tty
->read_tail
= (tty
->read_tail
+ n
) & (N_TTY_BUF_SIZE
-1);
867 static ssize_t
read_chan(struct tty_struct
*tty
, struct file
*file
,
868 unsigned char *buf
, size_t nr
)
870 unsigned char *b
= buf
;
871 DECLARE_WAITQUEUE(wait
, current
);
880 if (!tty
->read_buf
) {
881 printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
885 /* Job control check -- must be done at start and after
886 every sleep (POSIX.1 7.1.1.4). */
887 /* NOTE: not yet done after every sleep pending a thorough
888 check of the logic of this change. -- jlc */
889 /* don't stop on /dev/console */
890 if (file
->f_dentry
->d_inode
->i_rdev
!= CONSOLE_DEV
&&
891 file
->f_dentry
->d_inode
->i_rdev
!= SYSCONS_DEV
&&
892 current
->tty
== tty
) {
894 printk("read_chan: tty->pgrp <= 0!\n");
895 else if (current
->pgrp
!= tty
->pgrp
) {
896 if (is_ignored(SIGTTIN
) ||
897 is_orphaned_pgrp(current
->pgrp
))
899 kill_pg(current
->pgrp
, SIGTTIN
, 1);
905 timeout
= MAX_SCHEDULE_TIMEOUT
;
907 time
= (HZ
/ 10) * TIME_CHAR(tty
);
908 minimum
= MIN_CHAR(tty
);
911 tty
->minimum_to_wake
= 1;
912 else if (!waitqueue_active(&tty
->read_wait
) ||
913 (tty
->minimum_to_wake
> minimum
))
914 tty
->minimum_to_wake
= minimum
;
921 tty
->minimum_to_wake
= minimum
= 1;
925 if (file
->f_flags
& O_NONBLOCK
) {
926 if (down_trylock(&tty
->atomic_read
))
930 if (down_interruptible(&tty
->atomic_read
))
934 add_wait_queue(&tty
->read_wait
, &wait
);
935 set_bit(TTY_DONT_FLIP
, &tty
->flags
);
937 /* First test for status change. */
938 if (tty
->packet
&& tty
->link
->ctrl_status
) {
942 cs
= tty
->link
->ctrl_status
;
943 tty
->link
->ctrl_status
= 0;
948 /* This statement must be first before checking for input
949 so that any interrupt will set the state back to
951 current
->state
= TASK_INTERRUPTIBLE
;
953 if (((minimum
- (b
- buf
)) < tty
->minimum_to_wake
) &&
954 ((minimum
- (b
- buf
)) >= 1))
955 tty
->minimum_to_wake
= (minimum
- (b
- buf
));
957 if (!input_available_p(tty
, 0)) {
958 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
)) {
962 if (tty_hung_up_p(file
))
966 if (file
->f_flags
& O_NONBLOCK
) {
970 if (signal_pending(current
)) {
971 retval
= -ERESTARTSYS
;
974 clear_bit(TTY_DONT_FLIP
, &tty
->flags
);
975 timeout
= schedule_timeout(timeout
);
976 set_bit(TTY_DONT_FLIP
, &tty
->flags
);
979 current
->state
= TASK_RUNNING
;
981 /* Deal with packet mode. */
982 if (tty
->packet
&& b
== buf
) {
983 put_user(TIOCPKT_DATA
, b
++);
988 /* N.B. avoid overrun if nr == 0 */
989 while (nr
&& tty
->read_cnt
) {
992 eol
= test_and_clear_bit(tty
->read_tail
,
994 c
= tty
->read_buf
[tty
->read_tail
];
995 tty
->read_tail
= ((tty
->read_tail
+1) &
999 if (!eol
|| (c
!= __DISABLED_CHAR
)) {
1004 /* this test should be redundant:
1005 * we shouldn't be reading data if
1008 if (--tty
->canon_data
< 0)
1009 tty
->canon_data
= 0;
1015 uncopied
= copy_from_read_buf(tty
, &b
, &nr
);
1016 uncopied
+= copy_from_read_buf(tty
, &b
, &nr
);
1023 /* If there is enough space in the read buffer now, let the
1024 * low-level driver know. We use n_tty_chars_in_buffer() to
1025 * check the buffer, as it now knows about canonical mode.
1026 * Otherwise, if the driver is throttled and the line is
1027 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1028 * we won't get any more characters.
1030 if (n_tty_chars_in_buffer(tty
) <= TTY_THRESHOLD_UNTHROTTLE
)
1031 check_unthrottle(tty
);
1033 if (b
- buf
>= minimum
)
1038 clear_bit(TTY_DONT_FLIP
, &tty
->flags
);
1039 up(&tty
->atomic_read
);
1040 remove_wait_queue(&tty
->read_wait
, &wait
);
1042 if (!waitqueue_active(&tty
->read_wait
))
1043 tty
->minimum_to_wake
= minimum
;
1045 current
->state
= TASK_RUNNING
;
1050 clear_bit(TTY_PUSH
, &tty
->flags
);
1051 } else if (test_and_clear_bit(TTY_PUSH
, &tty
->flags
))
1057 static ssize_t
write_chan(struct tty_struct
* tty
, struct file
* file
,
1058 const unsigned char * buf
, size_t nr
)
1060 const unsigned char *b
= buf
;
1061 DECLARE_WAITQUEUE(wait
, current
);
1065 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1066 if (L_TOSTOP(tty
) &&
1067 file
->f_dentry
->d_inode
->i_rdev
!= CONSOLE_DEV
&&
1068 file
->f_dentry
->d_inode
->i_rdev
!= SYSCONS_DEV
) {
1069 retval
= tty_check_change(tty
);
1074 add_wait_queue(&tty
->write_wait
, &wait
);
1076 current
->state
= TASK_INTERRUPTIBLE
;
1077 if (signal_pending(current
)) {
1078 retval
= -ERESTARTSYS
;
1081 if (tty_hung_up_p(file
) || (tty
->link
&& !tty
->link
->count
)) {
1085 if (O_OPOST(tty
) && !(test_bit(TTY_HW_COOK_OUT
, &tty
->flags
))) {
1087 ssize_t num
= opost_block(tty
, b
, nr
);
1097 if (opost(c
, tty
) < 0)
1101 if (tty
->driver
.flush_chars
)
1102 tty
->driver
.flush_chars(tty
);
1104 c
= tty
->driver
.write(tty
, 1, b
, nr
);
1114 if (file
->f_flags
& O_NONBLOCK
) {
1121 current
->state
= TASK_RUNNING
;
1122 remove_wait_queue(&tty
->write_wait
, &wait
);
1123 return (b
- buf
) ? b
- buf
: retval
;
1126 static unsigned int normal_poll(struct tty_struct
* tty
, struct file
* file
, poll_table
*wait
)
1128 unsigned int mask
= 0;
1130 poll_wait(file
, &tty
->read_wait
, wait
);
1131 poll_wait(file
, &tty
->write_wait
, wait
);
1132 if (input_available_p(tty
, TIME_CHAR(tty
) ? 0 : MIN_CHAR(tty
)))
1133 mask
|= POLLIN
| POLLRDNORM
;
1134 if (tty
->packet
&& tty
->link
->ctrl_status
)
1135 mask
|= POLLPRI
| POLLIN
| POLLRDNORM
;
1136 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
))
1138 if (tty_hung_up_p(file
))
1140 if (!(mask
& (POLLHUP
| POLLIN
| POLLRDNORM
))) {
1141 if (MIN_CHAR(tty
) && !TIME_CHAR(tty
))
1142 tty
->minimum_to_wake
= MIN_CHAR(tty
);
1144 tty
->minimum_to_wake
= 1;
1146 if (tty
->driver
.chars_in_buffer(tty
) < WAKEUP_CHARS
)
1147 mask
|= POLLOUT
| POLLWRNORM
;
1151 struct tty_ldisc tty_ldisc_N_TTY
= {
1152 TTY_LDISC_MAGIC
, /* magic */
1156 n_tty_open
, /* open */
1157 n_tty_close
, /* close */
1158 n_tty_flush_buffer
, /* flush_buffer */
1159 n_tty_chars_in_buffer
, /* chars_in_buffer */
1160 read_chan
, /* read */
1161 write_chan
, /* write */
1162 n_tty_ioctl
, /* ioctl */
1163 n_tty_set_termios
, /* set_termios */
1164 normal_poll
, /* poll */
1165 n_tty_receive_buf
, /* receive_buf */
1166 n_tty_receive_room
, /* receive_room */
1167 0 /* write_wakeup */