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
)
76 if (test_and_clear_bit(TTY_THROTTLED
, &tty
->flags
) &&
77 tty
->driver
.unthrottle
)
78 tty
->driver
.unthrottle(tty
);
82 * Reset the read buffer counters, clear the flags,
83 * and make sure the driver is unthrottled. Called
84 * from n_tty_open() and n_tty_flush_buffer().
86 static void reset_buffer_flags(struct tty_struct
*tty
)
88 tty
->read_head
= tty
->read_tail
= tty
->read_cnt
= 0;
89 tty
->canon_head
= tty
->canon_data
= tty
->erasing
= 0;
90 memset(&tty
->read_flags
, 0, sizeof tty
->read_flags
);
91 check_unthrottle(tty
);
95 * Flush the input buffer
97 void n_tty_flush_buffer(struct tty_struct
* tty
)
99 /* clear everything and unthrottle the driver */
100 reset_buffer_flags(tty
);
105 if (tty
->link
->packet
) {
106 tty
->ctrl_status
|= TIOCPKT_FLUSHREAD
;
107 wake_up_interruptible(&tty
->link
->read_wait
);
112 * Return number of characters buffered to be delivered to user
114 ssize_t
n_tty_chars_in_buffer(struct tty_struct
*tty
)
117 if (!tty
->canon_data
) return 0;
119 return (tty
->canon_head
> tty
->read_tail
) ?
120 tty
->canon_head
- tty
->read_tail
:
121 tty
->canon_head
+ (N_TTY_BUF_SIZE
- tty
->read_tail
);
123 return tty
->read_cnt
;
127 * Perform OPOST processing. Returns -1 when the output device is
128 * full and the character must be retried.
130 static int opost(unsigned char c
, struct tty_struct
*tty
)
134 space
= tty
->driver
.write_room(tty
);
146 tty
->driver
.put_char(tty
, '\r');
149 tty
->canon_column
= tty
->column
;
152 if (O_ONOCR(tty
) && tty
->column
== 0)
157 tty
->canon_column
= tty
->column
= 0;
160 tty
->canon_column
= tty
->column
= 0;
163 spaces
= 8 - (tty
->column
& 7);
164 if (O_TABDLY(tty
) == XTABS
) {
167 tty
->column
+= spaces
;
168 tty
->driver
.write(tty
, 0, " ", spaces
);
171 tty
->column
+= spaces
;
185 tty
->driver
.put_char(tty
, c
);
190 * opost_block --- to speed up block console writes, among other
193 static ssize_t
opost_block(struct tty_struct
* tty
,
194 const unsigned char * inbuf
, unsigned int nr
)
201 space
= tty
->driver
.write_room(tty
);
206 if (nr
> sizeof(buf
))
208 nr
-= copy_from_user(buf
, inbuf
, nr
);
212 for (i
= 0, cp
= buf
; i
< nr
; i
++, cp
++) {
219 tty
->canon_column
= tty
->column
;
222 if (O_ONOCR(tty
) && tty
->column
== 0)
227 tty
->canon_column
= tty
->column
= 0;
230 tty
->canon_column
= tty
->column
= 0;
247 if (tty
->driver
.flush_chars
)
248 tty
->driver
.flush_chars(tty
);
249 i
= tty
->driver
.write(tty
, 0, buf
, i
);
255 static inline void put_char(unsigned char c
, struct tty_struct
*tty
)
257 tty
->driver
.put_char(tty
, c
);
260 /* Must be called only when L_ECHO(tty) is true. */
262 static void echo_char(unsigned char c
, struct tty_struct
*tty
)
264 if (L_ECHOCTL(tty
) && iscntrl(c
) && c
!= '\t') {
266 put_char(c
^ 0100, tty
);
272 static inline void finish_erasing(struct tty_struct
*tty
)
281 static void eraser(unsigned char c
, struct tty_struct
*tty
)
283 enum { ERASE
, WERASE
, KILL
} kill_type
;
284 int head
, seen_alnums
;
286 if (tty
->read_head
== tty
->canon_head
) {
287 /* opost('\a', tty); */ /* what do you think? */
290 if (c
== ERASE_CHAR(tty
))
292 else if (c
== WERASE_CHAR(tty
))
296 tty
->read_cnt
-= ((tty
->read_head
- tty
->canon_head
) &
297 (N_TTY_BUF_SIZE
- 1));
298 tty
->read_head
= tty
->canon_head
;
301 if (!L_ECHOK(tty
) || !L_ECHOKE(tty
) || !L_ECHOE(tty
)) {
302 tty
->read_cnt
-= ((tty
->read_head
- tty
->canon_head
) &
303 (N_TTY_BUF_SIZE
- 1));
304 tty
->read_head
= tty
->canon_head
;
306 echo_char(KILL_CHAR(tty
), tty
);
307 /* Add a newline if ECHOK is on and ECHOKE is off. */
316 while (tty
->read_head
!= tty
->canon_head
) {
317 head
= (tty
->read_head
- 1) & (N_TTY_BUF_SIZE
-1);
318 c
= tty
->read_buf
[head
];
319 if (kill_type
== WERASE
) {
320 /* Equivalent to BSD's ALTWERASE. */
321 if (isalnum(c
) || c
== '_')
323 else if (seen_alnums
)
326 tty
->read_head
= head
;
329 if (L_ECHOPRT(tty
)) {
336 } else if (kill_type
== ERASE
&& !L_ECHOE(tty
)) {
337 echo_char(ERASE_CHAR(tty
), tty
);
338 } else if (c
== '\t') {
339 unsigned int col
= tty
->canon_column
;
340 unsigned long tail
= tty
->canon_head
;
342 /* Find the column of the last char. */
343 while (tail
!= tty
->read_head
) {
344 c
= tty
->read_buf
[tail
];
347 else if (iscntrl(c
)) {
352 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
355 /* should never happen */
356 if (tty
->column
> 0x80000000)
359 /* Now backup to that column. */
360 while (tty
->column
> col
) {
361 /* Can't use opost here. */
367 if (iscntrl(c
) && L_ECHOCTL(tty
)) {
374 if (!iscntrl(c
) || L_ECHOCTL(tty
)) {
383 if (kill_type
== ERASE
)
386 if (tty
->read_head
== tty
->canon_head
)
390 static inline void isig(int sig
, struct tty_struct
*tty
, int flush
)
393 kill_pg(tty
->pgrp
, sig
, 1);
394 if (flush
|| !L_NOFLSH(tty
)) {
395 n_tty_flush_buffer(tty
);
396 if (tty
->driver
.flush_buffer
)
397 tty
->driver
.flush_buffer(tty
);
401 static inline void n_tty_receive_break(struct tty_struct
*tty
)
406 isig(SIGINT
, tty
, 1);
410 put_tty_queue('\377', tty
);
411 put_tty_queue('\0', tty
);
413 put_tty_queue('\0', tty
);
414 wake_up_interruptible(&tty
->read_wait
);
417 static inline void n_tty_receive_overrun(struct tty_struct
*tty
)
422 if (tty
->overrun_time
< (jiffies
- HZ
)) {
423 printk("%s: %d input overrun(s)\n", tty_name(tty
, buf
),
425 tty
->overrun_time
= jiffies
;
426 tty
->num_overrun
= 0;
430 static inline void n_tty_receive_parity_error(struct tty_struct
*tty
,
437 put_tty_queue('\377', tty
);
438 put_tty_queue('\0', tty
);
439 put_tty_queue(c
, tty
);
440 } else if (I_INPCK(tty
))
441 put_tty_queue('\0', tty
);
443 put_tty_queue(c
, tty
);
444 wake_up_interruptible(&tty
->read_wait
);
447 static inline void n_tty_receive_char(struct tty_struct
*tty
, unsigned char c
)
450 put_tty_queue(c
, tty
);
454 if (tty
->stopped
&& !tty
->flow_stopped
&&
455 I_IXON(tty
) && I_IXANY(tty
)) {
462 if (I_IUCLC(tty
) && L_IEXTEN(tty
))
467 if (c
== START_CHAR(tty
))
469 else if (c
== STOP_CHAR(tty
))
476 * If the previous character was LNEXT, or we know that this
477 * character is not one of the characters that we'll have to
478 * handle specially, do shortcut processing to speed things
481 if (!test_bit(c
, &tty
->process_char_map
) || tty
->lnext
) {
485 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
486 put_char('\a', tty
); /* beep if no space */
489 /* Record the column of first canon char. */
490 if (tty
->canon_head
== tty
->read_head
)
491 tty
->canon_column
= tty
->column
;
494 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
495 put_tty_queue(c
, tty
);
496 put_tty_queue(c
, tty
);
505 } else if (c
== '\n' && I_INLCR(tty
))
508 if (c
== START_CHAR(tty
)) {
512 if (c
== STOP_CHAR(tty
)) {
520 if (c
== INTR_CHAR(tty
))
523 if (c
== QUIT_CHAR(tty
))
526 if (c
== SUSP_CHAR(tty
)) {
528 isig(signal
, tty
, 0);
533 if (c
== ERASE_CHAR(tty
) || c
== KILL_CHAR(tty
) ||
534 (c
== WERASE_CHAR(tty
) && L_IEXTEN(tty
))) {
538 if (c
== LNEXT_CHAR(tty
) && L_IEXTEN(tty
)) {
542 if (L_ECHOCTL(tty
)) {
549 if (c
== REPRINT_CHAR(tty
) && L_ECHO(tty
) &&
551 unsigned long tail
= tty
->canon_head
;
556 while (tail
!= tty
->read_head
) {
557 echo_char(tty
->read_buf
[tail
], tty
);
558 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
563 if (L_ECHO(tty
) || L_ECHONL(tty
)) {
564 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
572 if (c
== EOF_CHAR(tty
)) {
573 if (tty
->canon_head
!= tty
->read_head
)
574 set_bit(TTY_PUSH
, &tty
->flags
);
578 if ((c
== EOL_CHAR(tty
)) ||
579 (c
== EOL2_CHAR(tty
) && L_IEXTEN(tty
))) {
581 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
584 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
588 /* Record the column of first canon char. */
589 if (tty
->canon_head
== tty
->read_head
)
590 tty
->canon_column
= tty
->column
;
594 * XXX does PARMRK doubling happen for
595 * EOL_CHAR and EOL2_CHAR?
597 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
598 put_tty_queue(c
, tty
);
601 set_bit(tty
->read_head
, &tty
->read_flags
);
602 put_tty_queue(c
, tty
);
603 tty
->canon_head
= tty
->read_head
;
606 kill_fasync(tty
->fasync
, SIGIO
);
608 wake_up_interruptible(&tty
->read_wait
);
615 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
616 put_char('\a', tty
); /* beep if no space */
622 /* Record the column of first canon char. */
623 if (tty
->canon_head
== tty
->read_head
)
624 tty
->canon_column
= tty
->column
;
629 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
630 put_tty_queue(c
, tty
);
632 put_tty_queue(c
, tty
);
635 static int n_tty_receive_room(struct tty_struct
*tty
)
637 int left
= N_TTY_BUF_SIZE
- tty
->read_cnt
- 1;
640 * If we are doing input canonicalization, and there are no
641 * pending newlines, let characters through without limit, so
642 * that erase characters will be handled. Other excess
643 * characters will be beeped.
645 if (tty
->icanon
&& !tty
->canon_data
)
646 return N_TTY_BUF_SIZE
;
653 static void n_tty_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
656 const unsigned char *p
;
657 char *f
, flags
= TTY_NORMAL
;
665 i
= MIN(count
, MIN(N_TTY_BUF_SIZE
- tty
->read_cnt
,
666 N_TTY_BUF_SIZE
- tty
->read_head
));
667 memcpy(tty
->read_buf
+ tty
->read_head
, cp
, i
);
668 tty
->read_head
= (tty
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
673 i
= MIN(count
, MIN(N_TTY_BUF_SIZE
- tty
->read_cnt
,
674 N_TTY_BUF_SIZE
- tty
->read_head
));
675 memcpy(tty
->read_buf
+ tty
->read_head
, cp
, i
);
676 tty
->read_head
= (tty
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
679 for (i
=count
, p
= cp
, f
= fp
; i
; i
--, p
++) {
684 n_tty_receive_char(tty
, *p
);
687 n_tty_receive_break(tty
);
691 n_tty_receive_parity_error(tty
, *p
);
694 n_tty_receive_overrun(tty
);
697 printk("%s: unknown flag %d\n",
698 tty_name(tty
, buf
), flags
);
702 if (tty
->driver
.flush_chars
)
703 tty
->driver
.flush_chars(tty
);
706 if (!tty
->icanon
&& (tty
->read_cnt
>= tty
->minimum_to_wake
)) {
708 kill_fasync(tty
->fasync
, SIGIO
);
710 wake_up_interruptible(&tty
->read_wait
);
714 * Check the remaining room for the input canonicalization
715 * mode. We don't want to throttle the driver if we're in
716 * canonical mode and don't have a newline yet!
718 if (n_tty_receive_room(tty
) < TTY_THRESHOLD_THROTTLE
) {
719 /* check TTY_THROTTLED first so it indicates our state */
720 if (!test_and_set_bit(TTY_THROTTLED
, &tty
->flags
) &&
721 tty
->driver
.throttle
)
722 tty
->driver
.throttle(tty
);
726 int is_ignored(int sig
)
728 return (sigismember(¤t
->blocked
, sig
) ||
729 current
->sig
->action
[sig
-1].sa
.sa_handler
== SIG_IGN
);
732 static void n_tty_set_termios(struct tty_struct
*tty
, struct termios
* old
)
737 tty
->icanon
= (L_ICANON(tty
) != 0);
738 if (test_bit(TTY_HW_COOK_IN
, &tty
->flags
)) {
743 if (I_ISTRIP(tty
) || I_IUCLC(tty
) || I_IGNCR(tty
) ||
744 I_ICRNL(tty
) || I_INLCR(tty
) || L_ICANON(tty
) ||
745 I_IXON(tty
) || L_ISIG(tty
) || L_ECHO(tty
) ||
748 memset(tty
->process_char_map
, 0, 256/8);
750 if (I_IGNCR(tty
) || I_ICRNL(tty
))
751 set_bit('\r', &tty
->process_char_map
);
753 set_bit('\n', &tty
->process_char_map
);
756 set_bit(ERASE_CHAR(tty
), &tty
->process_char_map
);
757 set_bit(KILL_CHAR(tty
), &tty
->process_char_map
);
758 set_bit(EOF_CHAR(tty
), &tty
->process_char_map
);
759 set_bit('\n', &tty
->process_char_map
);
760 set_bit(EOL_CHAR(tty
), &tty
->process_char_map
);
762 set_bit(WERASE_CHAR(tty
),
763 &tty
->process_char_map
);
764 set_bit(LNEXT_CHAR(tty
),
765 &tty
->process_char_map
);
766 set_bit(EOL2_CHAR(tty
),
767 &tty
->process_char_map
);
769 set_bit(REPRINT_CHAR(tty
),
770 &tty
->process_char_map
);
774 set_bit(START_CHAR(tty
), &tty
->process_char_map
);
775 set_bit(STOP_CHAR(tty
), &tty
->process_char_map
);
778 set_bit(INTR_CHAR(tty
), &tty
->process_char_map
);
779 set_bit(QUIT_CHAR(tty
), &tty
->process_char_map
);
780 set_bit(SUSP_CHAR(tty
), &tty
->process_char_map
);
782 clear_bit(__DISABLED_CHAR
, &tty
->process_char_map
);
788 if ((I_IGNBRK(tty
) || (!I_BRKINT(tty
) && !I_PARMRK(tty
))) &&
789 (I_IGNPAR(tty
) || !I_INPCK(tty
)) &&
790 (tty
->driver
.flags
& TTY_DRIVER_REAL_RAW
))
797 static void n_tty_close(struct tty_struct
*tty
)
799 n_tty_flush_buffer(tty
);
801 free_page((unsigned long) tty
->read_buf
);
806 static int n_tty_open(struct tty_struct
*tty
)
811 if (!tty
->read_buf
) {
812 tty
->read_buf
= (unsigned char *)
813 get_free_page(in_interrupt() ? GFP_ATOMIC
: GFP_KERNEL
);
817 memset(tty
->read_buf
, 0, N_TTY_BUF_SIZE
);
818 reset_buffer_flags(tty
);
820 n_tty_set_termios(tty
, 0);
821 tty
->minimum_to_wake
= 1;
826 static inline int input_available_p(struct tty_struct
*tty
, int amt
)
831 } else if (tty
->read_cnt
>= (amt
? amt
: 1))
838 * Helper function to speed up read_chan. It is only called when
839 * ICANON is off; it copies characters straight from the tty queue to
840 * user space directly. It can be profitably called twice; once to
841 * drain the space from the tail pointer to the (physical) end of the
842 * buffer, and once to drain the space from the (physical) beginning of
843 * the buffer to head pointer.
845 static inline int copy_from_read_buf(struct tty_struct
*tty
,
854 n
= MIN(*nr
, MIN(tty
->read_cnt
, N_TTY_BUF_SIZE
- tty
->read_tail
));
856 retval
= copy_to_user(*b
, &tty
->read_buf
[tty
->read_tail
], n
);
858 tty
->read_tail
= (tty
->read_tail
+ n
) & (N_TTY_BUF_SIZE
-1);
866 static ssize_t
read_chan(struct tty_struct
*tty
, struct file
*file
,
867 unsigned char *buf
, size_t nr
)
869 unsigned char *b
= buf
;
870 struct wait_queue wait
= { current
, NULL
};
878 if (!tty
->read_buf
) {
879 printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
883 /* Job control check -- must be done at start and after
884 every sleep (POSIX.1 7.1.1.4). */
885 /* NOTE: not yet done after every sleep pending a thorough
886 check of the logic of this change. -- jlc */
887 /* don't stop on /dev/console */
888 if (file
->f_dentry
->d_inode
->i_rdev
!= CONSOLE_DEV
&&
889 file
->f_dentry
->d_inode
->i_rdev
!= SYSCONS_DEV
&&
890 current
->tty
== tty
) {
892 printk("read_chan: tty->pgrp <= 0!\n");
893 else if (current
->pgrp
!= tty
->pgrp
) {
894 if (is_ignored(SIGTTIN
) ||
895 is_orphaned_pgrp(current
->pgrp
))
897 kill_pg(current
->pgrp
, SIGTTIN
, 1);
903 current
->timeout
= (unsigned long) -1;
905 time
= (HZ
/ 10) * TIME_CHAR(tty
);
906 minimum
= MIN_CHAR(tty
);
909 tty
->minimum_to_wake
= 1;
910 else if (!waitqueue_active(&tty
->read_wait
) ||
911 (tty
->minimum_to_wake
> minimum
))
912 tty
->minimum_to_wake
= minimum
;
914 current
->timeout
= 0;
916 current
->timeout
= time
+ jiffies
;
919 tty
->minimum_to_wake
= minimum
= 1;
923 add_wait_queue(&tty
->read_wait
, &wait
);
925 disable_bh(TQUEUE_BH
);
927 /* First test for status change. */
928 if (tty
->packet
&& tty
->link
->ctrl_status
) {
931 put_user(tty
->link
->ctrl_status
, b
++);
933 tty
->link
->ctrl_status
= 0;
936 /* This statement must be first before checking for input
937 so that any interrupt will set the state back to
939 current
->state
= TASK_INTERRUPTIBLE
;
941 if (((minimum
- (b
- buf
)) < tty
->minimum_to_wake
) &&
942 ((minimum
- (b
- buf
)) >= 1))
943 tty
->minimum_to_wake
= (minimum
- (b
- buf
));
945 if (!input_available_p(tty
, 0)) {
946 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
)) {
950 if (tty_hung_up_p(file
))
952 if (!current
->timeout
)
954 if (file
->f_flags
& O_NONBLOCK
) {
958 if (signal_pending(current
)) {
959 retval
= -ERESTARTSYS
;
962 enable_bh(TQUEUE_BH
);
964 disable_bh(TQUEUE_BH
);
967 current
->state
= TASK_RUNNING
;
969 /* Deal with packet mode. */
970 if (tty
->packet
&& b
== buf
) {
971 put_user(TIOCPKT_DATA
, b
++);
976 /* N.B. avoid overrun if nr == 0 */
977 while (nr
&& tty
->read_cnt
) {
980 eol
= test_and_clear_bit(tty
->read_tail
,
982 c
= tty
->read_buf
[tty
->read_tail
];
983 tty
->read_tail
= ((tty
->read_tail
+1) &
987 if (!eol
|| (c
!= __DISABLED_CHAR
)) {
992 /* this test should be redundant:
993 * we shouldn't be reading data if
996 if (--tty
->canon_data
< 0)
1003 uncopied
= copy_from_read_buf(tty
, &b
, &nr
);
1004 uncopied
+= copy_from_read_buf(tty
, &b
, &nr
);
1011 /* If there is enough space in the read buffer now, let the
1012 * low-level driver know. We use n_tty_chars_in_buffer() to
1013 * check the buffer, as it now knows about canonical mode.
1014 * Otherwise, if the driver is throttled and the line is
1015 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1016 * we won't get any more characters.
1018 if (n_tty_chars_in_buffer(tty
) <= TTY_THRESHOLD_UNTHROTTLE
)
1019 check_unthrottle(tty
);
1021 if (b
- buf
>= minimum
)
1024 current
->timeout
= time
+ jiffies
;
1026 enable_bh(TQUEUE_BH
);
1027 remove_wait_queue(&tty
->read_wait
, &wait
);
1029 if (!waitqueue_active(&tty
->read_wait
))
1030 tty
->minimum_to_wake
= minimum
;
1032 current
->state
= TASK_RUNNING
;
1033 current
->timeout
= 0;
1038 clear_bit(TTY_PUSH
, &tty
->flags
);
1039 } else if (test_and_clear_bit(TTY_PUSH
, &tty
->flags
))
1045 static ssize_t
write_chan(struct tty_struct
* tty
, struct file
* file
,
1046 const unsigned char * buf
, size_t nr
)
1048 const unsigned char *b
= buf
;
1049 struct wait_queue wait
= { current
, NULL
};
1053 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1054 if (L_TOSTOP(tty
) &&
1055 file
->f_dentry
->d_inode
->i_rdev
!= CONSOLE_DEV
&&
1056 file
->f_dentry
->d_inode
->i_rdev
!= SYSCONS_DEV
) {
1057 retval
= tty_check_change(tty
);
1062 add_wait_queue(&tty
->write_wait
, &wait
);
1064 current
->state
= TASK_INTERRUPTIBLE
;
1065 if (signal_pending(current
)) {
1066 retval
= -ERESTARTSYS
;
1069 if (tty_hung_up_p(file
) || (tty
->link
&& !tty
->link
->count
)) {
1073 if (O_OPOST(tty
) && !(test_bit(TTY_HW_COOK_OUT
, &tty
->flags
))) {
1075 ssize_t num
= opost_block(tty
, b
, nr
);
1085 if (opost(c
, tty
) < 0)
1089 if (tty
->driver
.flush_chars
)
1090 tty
->driver
.flush_chars(tty
);
1092 c
= tty
->driver
.write(tty
, 1, b
, nr
);
1102 if (file
->f_flags
& O_NONBLOCK
) {
1109 current
->state
= TASK_RUNNING
;
1110 remove_wait_queue(&tty
->write_wait
, &wait
);
1111 return (b
- buf
) ? b
- buf
: retval
;
1114 static unsigned int normal_poll(struct tty_struct
* tty
, struct file
* file
, poll_table
*wait
)
1116 unsigned int mask
= 0;
1118 poll_wait(file
, &tty
->read_wait
, wait
);
1119 poll_wait(file
, &tty
->write_wait
, wait
);
1120 if (input_available_p(tty
, TIME_CHAR(tty
) ? 0 : MIN_CHAR(tty
)))
1121 mask
|= POLLIN
| POLLRDNORM
;
1122 if (tty
->packet
&& tty
->link
->ctrl_status
)
1123 mask
|= POLLPRI
| POLLIN
| POLLRDNORM
;
1124 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
))
1126 if (tty_hung_up_p(file
))
1128 if (!(mask
& (POLLHUP
| POLLIN
| POLLRDNORM
))) {
1129 if (MIN_CHAR(tty
) && !TIME_CHAR(tty
))
1130 tty
->minimum_to_wake
= MIN_CHAR(tty
);
1132 tty
->minimum_to_wake
= 1;
1134 if (tty
->driver
.chars_in_buffer(tty
) < WAKEUP_CHARS
)
1135 mask
|= POLLOUT
| POLLWRNORM
;
1139 struct tty_ldisc tty_ldisc_N_TTY
= {
1140 TTY_LDISC_MAGIC
, /* magic */
1144 n_tty_open
, /* open */
1145 n_tty_close
, /* close */
1146 n_tty_flush_buffer
, /* flush_buffer */
1147 n_tty_chars_in_buffer
, /* chars_in_buffer */
1148 read_chan
, /* read */
1149 write_chan
, /* write */
1150 n_tty_ioctl
, /* ioctl */
1151 n_tty_set_termios
, /* set_termios */
1152 normal_poll
, /* poll */
1153 n_tty_receive_buf
, /* receive_buf */
1154 n_tty_receive_room
, /* receive_room */
1155 0 /* write_wakeup */