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>
45 #include <linux/string.h>
46 #include <linux/slab.h>
47 #include <linux/poll.h>
49 #include <asm/uaccess.h>
50 #include <asm/system.h>
51 #include <asm/bitops.h>
53 #define IS_CONSOLE_DEV(dev) (kdev_val(dev) == __mkdev(TTY_MAJOR,0))
54 #define IS_SYSCONS_DEV(dev) (kdev_val(dev) == __mkdev(TTYAUX_MAJOR,1))
56 /* number of characters left in xmit buffer before select has we have room */
57 #define WAKEUP_CHARS 256
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.
64 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
65 #define TTY_THRESHOLD_UNTHROTTLE 128
67 static inline unsigned char *alloc_buf(void)
70 int prio
= in_interrupt() ? GFP_ATOMIC
: GFP_KERNEL
;
72 if (PAGE_SIZE
!= N_TTY_BUF_SIZE
) {
73 p
= kmalloc(N_TTY_BUF_SIZE
, prio
);
75 memset(p
, 0, N_TTY_BUF_SIZE
);
77 p
= (unsigned char *)get_zeroed_page(prio
);
82 static inline void free_buf(unsigned char *buf
)
84 if (PAGE_SIZE
!= N_TTY_BUF_SIZE
)
87 free_page((unsigned long) buf
);
90 static inline void put_tty_queue_nolock(unsigned char c
, struct tty_struct
*tty
)
92 if (tty
->read_cnt
< N_TTY_BUF_SIZE
) {
93 tty
->read_buf
[tty
->read_head
] = c
;
94 tty
->read_head
= (tty
->read_head
+ 1) & (N_TTY_BUF_SIZE
-1);
99 static inline void put_tty_queue(unsigned char c
, struct tty_struct
*tty
)
103 * The problem of stomping on the buffers ends here.
104 * Why didn't anyone see this one coming? --AJK
106 spin_lock_irqsave(&tty
->read_lock
, flags
);
107 put_tty_queue_nolock(c
, tty
);
108 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
112 * Check whether to call the driver.unthrottle function.
113 * We test the TTY_THROTTLED bit first so that it always
114 * indicates the current state.
116 static void check_unthrottle(struct tty_struct
* tty
)
119 test_and_clear_bit(TTY_THROTTLED
, &tty
->flags
) &&
120 tty
->driver
->unthrottle
)
121 tty
->driver
->unthrottle(tty
);
125 * Reset the read buffer counters, clear the flags,
126 * and make sure the driver is unthrottled. Called
127 * from n_tty_open() and n_tty_flush_buffer().
129 static void reset_buffer_flags(struct tty_struct
*tty
)
133 spin_lock_irqsave(&tty
->read_lock
, flags
);
134 tty
->read_head
= tty
->read_tail
= tty
->read_cnt
= 0;
135 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
136 tty
->canon_head
= tty
->canon_data
= tty
->erasing
= 0;
137 memset(&tty
->read_flags
, 0, sizeof tty
->read_flags
);
138 check_unthrottle(tty
);
142 * Flush the input buffer
144 void n_tty_flush_buffer(struct tty_struct
* tty
)
146 /* clear everything and unthrottle the driver */
147 reset_buffer_flags(tty
);
152 if (tty
->link
->packet
) {
153 tty
->ctrl_status
|= TIOCPKT_FLUSHREAD
;
154 wake_up_interruptible(&tty
->link
->read_wait
);
159 * Return number of characters buffered to be delivered to user
161 ssize_t
n_tty_chars_in_buffer(struct tty_struct
*tty
)
166 spin_lock_irqsave(&tty
->read_lock
, flags
);
169 } else if (tty
->canon_data
) {
170 n
= (tty
->canon_head
> tty
->read_tail
) ?
171 tty
->canon_head
- tty
->read_tail
:
172 tty
->canon_head
+ (N_TTY_BUF_SIZE
- tty
->read_tail
);
174 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
179 * Perform OPOST processing. Returns -1 when the output device is
180 * full and the character must be retried.
182 static int opost(unsigned char c
, struct tty_struct
*tty
)
186 space
= tty
->driver
->write_room(tty
);
198 tty
->driver
->put_char(tty
, '\r');
201 tty
->canon_column
= tty
->column
;
204 if (O_ONOCR(tty
) && tty
->column
== 0)
209 tty
->canon_column
= tty
->column
= 0;
212 tty
->canon_column
= tty
->column
= 0;
215 spaces
= 8 - (tty
->column
& 7);
216 if (O_TABDLY(tty
) == XTABS
) {
219 tty
->column
+= spaces
;
220 tty
->driver
->write(tty
, 0, " ", spaces
);
223 tty
->column
+= spaces
;
237 tty
->driver
->put_char(tty
, c
);
242 * opost_block --- to speed up block console writes, among other
245 static ssize_t
opost_block(struct tty_struct
* tty
,
246 const unsigned char * inbuf
, unsigned int nr
)
253 space
= tty
->driver
->write_room(tty
);
258 if (nr
> sizeof(buf
))
261 if (copy_from_user(buf
, inbuf
, nr
))
264 for (i
= 0, cp
= buf
; i
< nr
; i
++, cp
++) {
271 tty
->canon_column
= tty
->column
;
274 if (O_ONOCR(tty
) && tty
->column
== 0)
279 tty
->canon_column
= tty
->column
= 0;
282 tty
->canon_column
= tty
->column
= 0;
299 if (tty
->driver
->flush_chars
)
300 tty
->driver
->flush_chars(tty
);
301 i
= tty
->driver
->write(tty
, 0, buf
, i
);
307 static inline void put_char(unsigned char c
, struct tty_struct
*tty
)
309 tty
->driver
->put_char(tty
, c
);
312 /* Must be called only when L_ECHO(tty) is true. */
314 static void echo_char(unsigned char c
, struct tty_struct
*tty
)
316 if (L_ECHOCTL(tty
) && iscntrl(c
) && c
!= '\t') {
318 put_char(c
^ 0100, tty
);
324 static inline void finish_erasing(struct tty_struct
*tty
)
333 static void eraser(unsigned char c
, struct tty_struct
*tty
)
335 enum { ERASE
, WERASE
, KILL
} kill_type
;
336 int head
, seen_alnums
;
339 if (tty
->read_head
== tty
->canon_head
) {
340 /* opost('\a', tty); */ /* what do you think? */
343 if (c
== ERASE_CHAR(tty
))
345 else if (c
== WERASE_CHAR(tty
))
349 spin_lock_irqsave(&tty
->read_lock
, flags
);
350 tty
->read_cnt
-= ((tty
->read_head
- tty
->canon_head
) &
351 (N_TTY_BUF_SIZE
- 1));
352 tty
->read_head
= tty
->canon_head
;
353 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
356 if (!L_ECHOK(tty
) || !L_ECHOKE(tty
) || !L_ECHOE(tty
)) {
357 spin_lock_irqsave(&tty
->read_lock
, flags
);
358 tty
->read_cnt
-= ((tty
->read_head
- tty
->canon_head
) &
359 (N_TTY_BUF_SIZE
- 1));
360 tty
->read_head
= tty
->canon_head
;
361 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
363 echo_char(KILL_CHAR(tty
), tty
);
364 /* Add a newline if ECHOK is on and ECHOKE is off. */
373 while (tty
->read_head
!= tty
->canon_head
) {
374 head
= (tty
->read_head
- 1) & (N_TTY_BUF_SIZE
-1);
375 c
= tty
->read_buf
[head
];
376 if (kill_type
== WERASE
) {
377 /* Equivalent to BSD's ALTWERASE. */
378 if (isalnum(c
) || c
== '_')
380 else if (seen_alnums
)
383 spin_lock_irqsave(&tty
->read_lock
, flags
);
384 tty
->read_head
= head
;
386 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
388 if (L_ECHOPRT(tty
)) {
395 } else if (kill_type
== ERASE
&& !L_ECHOE(tty
)) {
396 echo_char(ERASE_CHAR(tty
), tty
);
397 } else if (c
== '\t') {
398 unsigned int col
= tty
->canon_column
;
399 unsigned long tail
= tty
->canon_head
;
401 /* Find the column of the last char. */
402 while (tail
!= tty
->read_head
) {
403 c
= tty
->read_buf
[tail
];
406 else if (iscntrl(c
)) {
411 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
414 /* should never happen */
415 if (tty
->column
> 0x80000000)
418 /* Now backup to that column. */
419 while (tty
->column
> col
) {
420 /* Can't use opost here. */
426 if (iscntrl(c
) && L_ECHOCTL(tty
)) {
433 if (!iscntrl(c
) || L_ECHOCTL(tty
)) {
442 if (kill_type
== ERASE
)
445 if (tty
->read_head
== tty
->canon_head
)
449 static inline void isig(int sig
, struct tty_struct
*tty
, int flush
)
452 kill_pg(tty
->pgrp
, sig
, 1);
453 if (flush
|| !L_NOFLSH(tty
)) {
454 n_tty_flush_buffer(tty
);
455 if (tty
->driver
->flush_buffer
)
456 tty
->driver
->flush_buffer(tty
);
460 static inline void n_tty_receive_break(struct tty_struct
*tty
)
465 isig(SIGINT
, tty
, 1);
469 put_tty_queue('\377', tty
);
470 put_tty_queue('\0', tty
);
472 put_tty_queue('\0', tty
);
473 wake_up_interruptible(&tty
->read_wait
);
476 static inline void n_tty_receive_overrun(struct tty_struct
*tty
)
481 if (time_before(tty
->overrun_time
, jiffies
- HZ
)) {
482 printk("%s: %d input overrun(s)\n", tty_name(tty
, buf
),
484 tty
->overrun_time
= jiffies
;
485 tty
->num_overrun
= 0;
489 static inline void n_tty_receive_parity_error(struct tty_struct
*tty
,
496 put_tty_queue('\377', tty
);
497 put_tty_queue('\0', tty
);
498 put_tty_queue(c
, tty
);
499 } else if (I_INPCK(tty
))
500 put_tty_queue('\0', tty
);
502 put_tty_queue(c
, tty
);
503 wake_up_interruptible(&tty
->read_wait
);
506 static inline void n_tty_receive_char(struct tty_struct
*tty
, unsigned char c
)
511 put_tty_queue(c
, tty
);
515 if (tty
->stopped
&& !tty
->flow_stopped
&&
516 I_IXON(tty
) && I_IXANY(tty
)) {
523 if (I_IUCLC(tty
) && L_IEXTEN(tty
))
528 if (c
== START_CHAR(tty
))
530 else if (c
== STOP_CHAR(tty
))
537 * If the previous character was LNEXT, or we know that this
538 * character is not one of the characters that we'll have to
539 * handle specially, do shortcut processing to speed things
542 if (!test_bit(c
, tty
->process_char_map
) || tty
->lnext
) {
546 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
547 put_char('\a', tty
); /* beep if no space */
550 /* Record the column of first canon char. */
551 if (tty
->canon_head
== tty
->read_head
)
552 tty
->canon_column
= tty
->column
;
555 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
556 put_tty_queue(c
, tty
);
557 put_tty_queue(c
, tty
);
566 } else if (c
== '\n' && I_INLCR(tty
))
569 if (c
== START_CHAR(tty
)) {
573 if (c
== STOP_CHAR(tty
)) {
581 if (c
== INTR_CHAR(tty
))
584 if (c
== QUIT_CHAR(tty
))
587 if (c
== SUSP_CHAR(tty
)) {
589 isig(signal
, tty
, 0);
594 if (c
== ERASE_CHAR(tty
) || c
== KILL_CHAR(tty
) ||
595 (c
== WERASE_CHAR(tty
) && L_IEXTEN(tty
))) {
599 if (c
== LNEXT_CHAR(tty
) && L_IEXTEN(tty
)) {
603 if (L_ECHOCTL(tty
)) {
610 if (c
== REPRINT_CHAR(tty
) && L_ECHO(tty
) &&
612 unsigned long tail
= tty
->canon_head
;
617 while (tail
!= tty
->read_head
) {
618 echo_char(tty
->read_buf
[tail
], tty
);
619 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
624 if (L_ECHO(tty
) || L_ECHONL(tty
)) {
625 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
633 if (c
== EOF_CHAR(tty
)) {
634 if (tty
->canon_head
!= tty
->read_head
)
635 set_bit(TTY_PUSH
, &tty
->flags
);
639 if ((c
== EOL_CHAR(tty
)) ||
640 (c
== EOL2_CHAR(tty
) && L_IEXTEN(tty
))) {
642 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
645 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
649 /* Record the column of first canon char. */
650 if (tty
->canon_head
== tty
->read_head
)
651 tty
->canon_column
= tty
->column
;
655 * XXX does PARMRK doubling happen for
656 * EOL_CHAR and EOL2_CHAR?
658 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
659 put_tty_queue(c
, tty
);
662 spin_lock_irqsave(&tty
->read_lock
, flags
);
663 set_bit(tty
->read_head
, tty
->read_flags
);
664 put_tty_queue_nolock(c
, tty
);
665 tty
->canon_head
= tty
->read_head
;
667 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
668 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
669 if (waitqueue_active(&tty
->read_wait
))
670 wake_up_interruptible(&tty
->read_wait
);
677 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
678 put_char('\a', tty
); /* beep if no space */
684 /* Record the column of first canon char. */
685 if (tty
->canon_head
== tty
->read_head
)
686 tty
->canon_column
= tty
->column
;
691 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
692 put_tty_queue(c
, tty
);
694 put_tty_queue(c
, tty
);
697 static int n_tty_receive_room(struct tty_struct
*tty
)
699 int left
= N_TTY_BUF_SIZE
- tty
->read_cnt
- 1;
702 * If we are doing input canonicalization, and there are no
703 * pending newlines, let characters through without limit, so
704 * that erase characters will be handled. Other excess
705 * characters will be beeped.
707 if (tty
->icanon
&& !tty
->canon_data
)
708 return N_TTY_BUF_SIZE
;
716 * Required for the ptys, serial driver etc. since processes
717 * that attach themselves to the master and rely on ASYNC
718 * IO must be woken up
721 static void n_tty_write_wakeup(struct tty_struct
*tty
)
725 set_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
726 kill_fasync(&tty
->fasync
, SIGIO
, POLL_OUT
);
731 static void n_tty_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
734 const unsigned char *p
;
735 char *f
, flags
= TTY_NORMAL
;
738 unsigned long cpuflags
;
744 spin_lock_irqsave(&tty
->read_lock
, cpuflags
);
745 i
= min(N_TTY_BUF_SIZE
- tty
->read_cnt
,
746 N_TTY_BUF_SIZE
- tty
->read_head
);
748 memcpy(tty
->read_buf
+ tty
->read_head
, cp
, i
);
749 tty
->read_head
= (tty
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
754 i
= min(N_TTY_BUF_SIZE
- tty
->read_cnt
,
755 N_TTY_BUF_SIZE
- tty
->read_head
);
757 memcpy(tty
->read_buf
+ tty
->read_head
, cp
, i
);
758 tty
->read_head
= (tty
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
760 spin_unlock_irqrestore(&tty
->read_lock
, cpuflags
);
762 for (i
=count
, p
= cp
, f
= fp
; i
; i
--, p
++) {
767 n_tty_receive_char(tty
, *p
);
770 n_tty_receive_break(tty
);
774 n_tty_receive_parity_error(tty
, *p
);
777 n_tty_receive_overrun(tty
);
780 printk("%s: unknown flag %d\n",
781 tty_name(tty
, buf
), flags
);
785 if (tty
->driver
->flush_chars
)
786 tty
->driver
->flush_chars(tty
);
789 if (!tty
->icanon
&& (tty
->read_cnt
>= tty
->minimum_to_wake
)) {
790 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
791 if (waitqueue_active(&tty
->read_wait
))
792 wake_up_interruptible(&tty
->read_wait
);
796 * Check the remaining room for the input canonicalization
797 * mode. We don't want to throttle the driver if we're in
798 * canonical mode and don't have a newline yet!
800 if (n_tty_receive_room(tty
) < TTY_THRESHOLD_THROTTLE
) {
801 /* check TTY_THROTTLED first so it indicates our state */
802 if (!test_and_set_bit(TTY_THROTTLED
, &tty
->flags
) &&
803 tty
->driver
->throttle
)
804 tty
->driver
->throttle(tty
);
808 int is_ignored(int sig
)
810 return (sigismember(¤t
->blocked
, sig
) ||
811 current
->sighand
->action
[sig
-1].sa
.sa_handler
== SIG_IGN
);
814 static void n_tty_set_termios(struct tty_struct
*tty
, struct termios
* old
)
819 tty
->icanon
= (L_ICANON(tty
) != 0);
820 if (test_bit(TTY_HW_COOK_IN
, &tty
->flags
)) {
825 if (I_ISTRIP(tty
) || I_IUCLC(tty
) || I_IGNCR(tty
) ||
826 I_ICRNL(tty
) || I_INLCR(tty
) || L_ICANON(tty
) ||
827 I_IXON(tty
) || L_ISIG(tty
) || L_ECHO(tty
) ||
829 local_irq_disable(); // FIXME: is this safe?
830 memset(tty
->process_char_map
, 0, 256/8);
832 if (I_IGNCR(tty
) || I_ICRNL(tty
))
833 set_bit('\r', tty
->process_char_map
);
835 set_bit('\n', tty
->process_char_map
);
838 set_bit(ERASE_CHAR(tty
), tty
->process_char_map
);
839 set_bit(KILL_CHAR(tty
), tty
->process_char_map
);
840 set_bit(EOF_CHAR(tty
), tty
->process_char_map
);
841 set_bit('\n', tty
->process_char_map
);
842 set_bit(EOL_CHAR(tty
), tty
->process_char_map
);
844 set_bit(WERASE_CHAR(tty
),
845 tty
->process_char_map
);
846 set_bit(LNEXT_CHAR(tty
),
847 tty
->process_char_map
);
848 set_bit(EOL2_CHAR(tty
),
849 tty
->process_char_map
);
851 set_bit(REPRINT_CHAR(tty
),
852 tty
->process_char_map
);
856 set_bit(START_CHAR(tty
), tty
->process_char_map
);
857 set_bit(STOP_CHAR(tty
), tty
->process_char_map
);
860 set_bit(INTR_CHAR(tty
), tty
->process_char_map
);
861 set_bit(QUIT_CHAR(tty
), tty
->process_char_map
);
862 set_bit(SUSP_CHAR(tty
), tty
->process_char_map
);
864 clear_bit(__DISABLED_CHAR
, tty
->process_char_map
);
865 local_irq_enable(); // FIXME: is this safe?
870 if ((I_IGNBRK(tty
) || (!I_BRKINT(tty
) && !I_PARMRK(tty
))) &&
871 (I_IGNPAR(tty
) || !I_INPCK(tty
)) &&
872 (tty
->driver
->flags
& TTY_DRIVER_REAL_RAW
))
879 static void n_tty_close(struct tty_struct
*tty
)
881 n_tty_flush_buffer(tty
);
883 free_buf(tty
->read_buf
);
888 static int n_tty_open(struct tty_struct
*tty
)
893 if (!tty
->read_buf
) {
894 tty
->read_buf
= alloc_buf();
898 memset(tty
->read_buf
, 0, N_TTY_BUF_SIZE
);
899 reset_buffer_flags(tty
);
901 n_tty_set_termios(tty
, 0);
902 tty
->minimum_to_wake
= 1;
907 static inline int input_available_p(struct tty_struct
*tty
, int amt
)
912 } else if (tty
->read_cnt
>= (amt
? amt
: 1))
919 * Helper function to speed up read_chan. It is only called when
920 * ICANON is off; it copies characters straight from the tty queue to
921 * user space directly. It can be profitably called twice; once to
922 * drain the space from the tail pointer to the (physical) end of the
923 * buffer, and once to drain the space from the (physical) beginning of
924 * the buffer to head pointer.
926 static inline int copy_from_read_buf(struct tty_struct
*tty
,
936 spin_lock_irqsave(&tty
->read_lock
, flags
);
937 n
= min(tty
->read_cnt
, N_TTY_BUF_SIZE
- tty
->read_tail
);
938 n
= min((ssize_t
)*nr
, n
);
939 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
942 retval
= copy_to_user(*b
, &tty
->read_buf
[tty
->read_tail
], n
);
944 spin_lock_irqsave(&tty
->read_lock
, flags
);
945 tty
->read_tail
= (tty
->read_tail
+ n
) & (N_TTY_BUF_SIZE
-1);
947 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
954 static ssize_t
read_chan(struct tty_struct
*tty
, struct file
*file
,
955 unsigned char *buf
, size_t nr
)
957 unsigned char *b
= buf
;
958 DECLARE_WAITQUEUE(wait
, current
);
968 if (!tty
->read_buf
) {
969 printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
973 /* Job control check -- must be done at start and after
974 every sleep (POSIX.1 7.1.1.4). */
975 /* NOTE: not yet done after every sleep pending a thorough
976 check of the logic of this change. -- jlc */
977 /* don't stop on /dev/console */
978 if (!IS_CONSOLE_DEV(file
->f_dentry
->d_inode
->i_rdev
) &&
979 !IS_SYSCONS_DEV(file
->f_dentry
->d_inode
->i_rdev
) &&
980 current
->tty
== tty
) {
982 printk("read_chan: tty->pgrp <= 0!\n");
983 else if (current
->pgrp
!= tty
->pgrp
) {
984 if (is_ignored(SIGTTIN
) ||
985 is_orphaned_pgrp(current
->pgrp
))
987 kill_pg(current
->pgrp
, SIGTTIN
, 1);
993 timeout
= MAX_SCHEDULE_TIMEOUT
;
995 time
= (HZ
/ 10) * TIME_CHAR(tty
);
996 minimum
= MIN_CHAR(tty
);
999 tty
->minimum_to_wake
= 1;
1000 else if (!waitqueue_active(&tty
->read_wait
) ||
1001 (tty
->minimum_to_wake
> minimum
))
1002 tty
->minimum_to_wake
= minimum
;
1009 tty
->minimum_to_wake
= minimum
= 1;
1013 if (file
->f_flags
& O_NONBLOCK
) {
1014 if (down_trylock(&tty
->atomic_read
))
1018 if (down_interruptible(&tty
->atomic_read
))
1019 return -ERESTARTSYS
;
1022 add_wait_queue(&tty
->read_wait
, &wait
);
1023 set_bit(TTY_DONT_FLIP
, &tty
->flags
);
1025 /* First test for status change. */
1026 if (tty
->packet
&& tty
->link
->ctrl_status
) {
1030 cs
= tty
->link
->ctrl_status
;
1031 tty
->link
->ctrl_status
= 0;
1032 if (put_user(cs
, b
++)) {
1040 /* This statement must be first before checking for input
1041 so that any interrupt will set the state back to
1043 set_current_state(TASK_INTERRUPTIBLE
);
1045 if (((minimum
- (b
- buf
)) < tty
->minimum_to_wake
) &&
1046 ((minimum
- (b
- buf
)) >= 1))
1047 tty
->minimum_to_wake
= (minimum
- (b
- buf
));
1049 if (!input_available_p(tty
, 0)) {
1050 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
)) {
1054 if (tty_hung_up_p(file
))
1058 if (file
->f_flags
& O_NONBLOCK
) {
1062 if (signal_pending(current
)) {
1063 retval
= -ERESTARTSYS
;
1066 clear_bit(TTY_DONT_FLIP
, &tty
->flags
);
1067 timeout
= schedule_timeout(timeout
);
1068 set_bit(TTY_DONT_FLIP
, &tty
->flags
);
1071 current
->state
= TASK_RUNNING
;
1073 /* Deal with packet mode. */
1074 if (tty
->packet
&& b
== buf
) {
1075 if (put_user(TIOCPKT_DATA
, b
++)) {
1084 /* N.B. avoid overrun if nr == 0 */
1085 while (nr
&& tty
->read_cnt
) {
1088 eol
= test_and_clear_bit(tty
->read_tail
,
1090 c
= tty
->read_buf
[tty
->read_tail
];
1091 spin_lock_irqsave(&tty
->read_lock
, flags
);
1092 tty
->read_tail
= ((tty
->read_tail
+1) &
1093 (N_TTY_BUF_SIZE
-1));
1096 /* this test should be redundant:
1097 * we shouldn't be reading data if
1100 if (--tty
->canon_data
< 0)
1101 tty
->canon_data
= 0;
1103 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
1105 if (!eol
|| (c
!= __DISABLED_CHAR
)) {
1106 if (put_user(c
, b
++)) {
1120 uncopied
= copy_from_read_buf(tty
, &b
, &nr
);
1121 uncopied
+= copy_from_read_buf(tty
, &b
, &nr
);
1128 /* If there is enough space in the read buffer now, let the
1129 * low-level driver know. We use n_tty_chars_in_buffer() to
1130 * check the buffer, as it now knows about canonical mode.
1131 * Otherwise, if the driver is throttled and the line is
1132 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1133 * we won't get any more characters.
1135 if (n_tty_chars_in_buffer(tty
) <= TTY_THRESHOLD_UNTHROTTLE
)
1136 check_unthrottle(tty
);
1138 if (b
- buf
>= minimum
)
1143 clear_bit(TTY_DONT_FLIP
, &tty
->flags
);
1144 up(&tty
->atomic_read
);
1145 remove_wait_queue(&tty
->read_wait
, &wait
);
1147 if (!waitqueue_active(&tty
->read_wait
))
1148 tty
->minimum_to_wake
= minimum
;
1150 current
->state
= TASK_RUNNING
;
1155 clear_bit(TTY_PUSH
, &tty
->flags
);
1156 } else if (test_and_clear_bit(TTY_PUSH
, &tty
->flags
))
1162 static ssize_t
write_chan(struct tty_struct
* tty
, struct file
* file
,
1163 const unsigned char * buf
, size_t nr
)
1165 const unsigned char *b
= buf
;
1166 DECLARE_WAITQUEUE(wait
, current
);
1170 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1171 if (L_TOSTOP(tty
) &&
1172 !IS_CONSOLE_DEV(file
->f_dentry
->d_inode
->i_rdev
) &&
1173 !IS_SYSCONS_DEV(file
->f_dentry
->d_inode
->i_rdev
)) {
1174 retval
= tty_check_change(tty
);
1179 add_wait_queue(&tty
->write_wait
, &wait
);
1181 set_current_state(TASK_INTERRUPTIBLE
);
1182 if (signal_pending(current
)) {
1183 retval
= -ERESTARTSYS
;
1186 if (tty_hung_up_p(file
) || (tty
->link
&& !tty
->link
->count
)) {
1190 if (O_OPOST(tty
) && !(test_bit(TTY_HW_COOK_OUT
, &tty
->flags
))) {
1192 ssize_t num
= opost_block(tty
, b
, nr
);
1204 if (opost(c
, tty
) < 0)
1208 if (tty
->driver
->flush_chars
)
1209 tty
->driver
->flush_chars(tty
);
1211 c
= tty
->driver
->write(tty
, 1, b
, nr
);
1221 if (file
->f_flags
& O_NONBLOCK
) {
1228 current
->state
= TASK_RUNNING
;
1229 remove_wait_queue(&tty
->write_wait
, &wait
);
1230 return (b
- buf
) ? b
- buf
: retval
;
1233 /* Called without the kernel lock held - fine */
1234 static unsigned int normal_poll(struct tty_struct
* tty
, struct file
* file
, poll_table
*wait
)
1236 unsigned int mask
= 0;
1238 poll_wait(file
, &tty
->read_wait
, wait
);
1239 poll_wait(file
, &tty
->write_wait
, wait
);
1240 if (input_available_p(tty
, TIME_CHAR(tty
) ? 0 : MIN_CHAR(tty
)))
1241 mask
|= POLLIN
| POLLRDNORM
;
1242 if (tty
->packet
&& tty
->link
->ctrl_status
)
1243 mask
|= POLLPRI
| POLLIN
| POLLRDNORM
;
1244 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
))
1246 if (tty_hung_up_p(file
))
1248 if (!(mask
& (POLLHUP
| POLLIN
| POLLRDNORM
))) {
1249 if (MIN_CHAR(tty
) && !TIME_CHAR(tty
))
1250 tty
->minimum_to_wake
= MIN_CHAR(tty
);
1252 tty
->minimum_to_wake
= 1;
1254 if (tty
->driver
->chars_in_buffer(tty
) < WAKEUP_CHARS
)
1255 mask
|= POLLOUT
| POLLWRNORM
;
1259 struct tty_ldisc tty_ldisc_N_TTY
= {
1260 TTY_LDISC_MAGIC
, /* magic */
1264 n_tty_open
, /* open */
1265 n_tty_close
, /* close */
1266 n_tty_flush_buffer
, /* flush_buffer */
1267 n_tty_chars_in_buffer
, /* chars_in_buffer */
1268 read_chan
, /* read */
1269 write_chan
, /* write */
1270 n_tty_ioctl
, /* ioctl */
1271 n_tty_set_termios
, /* set_termios */
1272 normal_poll
, /* poll */
1273 n_tty_receive_buf
, /* receive_buf */
1274 n_tty_receive_room
, /* receive_room */
1275 n_tty_write_wakeup
/* write_wakeup */