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
21 * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
22 * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
23 * who actually finally proved there really was a race.
26 #include <linux/types.h>
27 #include <linux/major.h>
28 #include <linux/errno.h>
29 #include <linux/signal.h>
30 #include <linux/fcntl.h>
31 #include <linux/sched.h>
32 #include <linux/interrupt.h>
33 #include <linux/tty.h>
34 #include <linux/timer.h>
35 #include <linux/ctype.h>
38 #include <linux/string.h>
39 #include <linux/malloc.h>
40 #include <linux/poll.h>
42 #include <asm/uaccess.h>
43 #include <asm/system.h>
44 #include <asm/bitops.h>
46 #define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
47 #define SYSCONS_DEV MKDEV(TTYAUX_MAJOR,1)
50 #define MIN(a,b) ((a) < (b) ? (a) : (b))
53 /* number of characters left in xmit buffer before select has we have room */
54 #define WAKEUP_CHARS 256
57 * This defines the low- and high-watermarks for throttling and
58 * unthrottling the TTY driver. These watermarks are used for
59 * controlling the space in the read buffer.
61 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
62 #define TTY_THRESHOLD_UNTHROTTLE 128
64 static inline void put_tty_queue(unsigned char c
, struct tty_struct
*tty
)
68 * The problem of stomping on the buffers ends here.
69 * Why didn't anyone see this one comming? --AJK
71 spin_lock_irqsave(&tty
->read_lock
, flags
);
72 if (tty
->read_cnt
< N_TTY_BUF_SIZE
) {
73 tty
->read_buf
[tty
->read_head
] = c
;
74 tty
->read_head
= (tty
->read_head
+ 1) & (N_TTY_BUF_SIZE
-1);
77 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
81 * Check whether to call the driver.unthrottle function.
82 * We test the TTY_THROTTLED bit first so that it always
83 * indicates the current state.
85 static void check_unthrottle(struct tty_struct
* tty
)
88 test_and_clear_bit(TTY_THROTTLED
, &tty
->flags
) &&
89 tty
->driver
.unthrottle
)
90 tty
->driver
.unthrottle(tty
);
94 * Reset the read buffer counters, clear the flags,
95 * and make sure the driver is unthrottled. Called
96 * from n_tty_open() and n_tty_flush_buffer().
98 static void reset_buffer_flags(struct tty_struct
*tty
)
102 spin_lock_irqsave(&tty
->read_lock
, flags
);
103 tty
->read_head
= tty
->read_tail
= tty
->read_cnt
= 0;
104 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
105 tty
->canon_head
= tty
->canon_data
= tty
->erasing
= 0;
106 memset(&tty
->read_flags
, 0, sizeof tty
->read_flags
);
107 check_unthrottle(tty
);
111 * Flush the input buffer
113 void n_tty_flush_buffer(struct tty_struct
* tty
)
115 /* clear everything and unthrottle the driver */
116 reset_buffer_flags(tty
);
121 if (tty
->link
->packet
) {
122 tty
->ctrl_status
|= TIOCPKT_FLUSHREAD
;
123 wake_up_interruptible(&tty
->link
->read_wait
);
128 * Return number of characters buffered to be delivered to user
130 ssize_t
n_tty_chars_in_buffer(struct tty_struct
*tty
)
135 spin_lock_irqsave(&tty
->read_lock
, flags
);
138 } else if (tty
->canon_data
) {
139 n
= (tty
->canon_head
> tty
->read_tail
) ?
140 tty
->canon_head
- tty
->read_tail
:
141 tty
->canon_head
+ (N_TTY_BUF_SIZE
- tty
->read_tail
);
143 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
148 * Perform OPOST processing. Returns -1 when the output device is
149 * full and the character must be retried.
151 static int opost(unsigned char c
, struct tty_struct
*tty
)
155 space
= tty
->driver
.write_room(tty
);
167 tty
->driver
.put_char(tty
, '\r');
170 tty
->canon_column
= tty
->column
;
173 if (O_ONOCR(tty
) && tty
->column
== 0)
178 tty
->canon_column
= tty
->column
= 0;
181 tty
->canon_column
= tty
->column
= 0;
184 spaces
= 8 - (tty
->column
& 7);
185 if (O_TABDLY(tty
) == XTABS
) {
188 tty
->column
+= spaces
;
189 tty
->driver
.write(tty
, 0, " ", spaces
);
192 tty
->column
+= spaces
;
206 tty
->driver
.put_char(tty
, c
);
211 * opost_block --- to speed up block console writes, among other
214 static ssize_t
opost_block(struct tty_struct
* tty
,
215 const unsigned char * inbuf
, unsigned int nr
)
222 space
= tty
->driver
.write_room(tty
);
227 if (nr
> sizeof(buf
))
230 if (copy_from_user(buf
, inbuf
, nr
))
233 for (i
= 0, cp
= buf
; i
< nr
; i
++, cp
++) {
240 tty
->canon_column
= tty
->column
;
243 if (O_ONOCR(tty
) && tty
->column
== 0)
248 tty
->canon_column
= tty
->column
= 0;
251 tty
->canon_column
= tty
->column
= 0;
268 if (tty
->driver
.flush_chars
)
269 tty
->driver
.flush_chars(tty
);
270 i
= tty
->driver
.write(tty
, 0, buf
, i
);
276 static inline void put_char(unsigned char c
, struct tty_struct
*tty
)
278 tty
->driver
.put_char(tty
, c
);
281 /* Must be called only when L_ECHO(tty) is true. */
283 static void echo_char(unsigned char c
, struct tty_struct
*tty
)
285 if (L_ECHOCTL(tty
) && iscntrl(c
) && c
!= '\t') {
287 put_char(c
^ 0100, tty
);
293 static inline void finish_erasing(struct tty_struct
*tty
)
302 static void eraser(unsigned char c
, struct tty_struct
*tty
)
304 enum { ERASE
, WERASE
, KILL
} kill_type
;
305 int head
, seen_alnums
;
308 if (tty
->read_head
== tty
->canon_head
) {
309 /* opost('\a', tty); */ /* what do you think? */
312 if (c
== ERASE_CHAR(tty
))
314 else if (c
== WERASE_CHAR(tty
))
318 spin_lock_irqsave(&tty
->read_lock
, flags
);
319 tty
->read_cnt
-= ((tty
->read_head
- tty
->canon_head
) &
320 (N_TTY_BUF_SIZE
- 1));
321 tty
->read_head
= tty
->canon_head
;
322 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
325 if (!L_ECHOK(tty
) || !L_ECHOKE(tty
) || !L_ECHOE(tty
)) {
326 spin_lock_irqsave(&tty
->read_lock
, flags
);
327 tty
->read_cnt
-= ((tty
->read_head
- tty
->canon_head
) &
328 (N_TTY_BUF_SIZE
- 1));
329 tty
->read_head
= tty
->canon_head
;
330 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
332 echo_char(KILL_CHAR(tty
), tty
);
333 /* Add a newline if ECHOK is on and ECHOKE is off. */
342 while (tty
->read_head
!= tty
->canon_head
) {
343 head
= (tty
->read_head
- 1) & (N_TTY_BUF_SIZE
-1);
344 c
= tty
->read_buf
[head
];
345 if (kill_type
== WERASE
) {
346 /* Equivalent to BSD's ALTWERASE. */
347 if (isalnum(c
) || c
== '_')
349 else if (seen_alnums
)
352 spin_lock_irqsave(&tty
->read_lock
, flags
);
353 tty
->read_head
= head
;
355 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
357 if (L_ECHOPRT(tty
)) {
364 } else if (kill_type
== ERASE
&& !L_ECHOE(tty
)) {
365 echo_char(ERASE_CHAR(tty
), tty
);
366 } else if (c
== '\t') {
367 unsigned int col
= tty
->canon_column
;
368 unsigned long tail
= tty
->canon_head
;
370 /* Find the column of the last char. */
371 while (tail
!= tty
->read_head
) {
372 c
= tty
->read_buf
[tail
];
375 else if (iscntrl(c
)) {
380 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
383 /* should never happen */
384 if (tty
->column
> 0x80000000)
387 /* Now backup to that column. */
388 while (tty
->column
> col
) {
389 /* Can't use opost here. */
395 if (iscntrl(c
) && L_ECHOCTL(tty
)) {
402 if (!iscntrl(c
) || L_ECHOCTL(tty
)) {
411 if (kill_type
== ERASE
)
414 if (tty
->read_head
== tty
->canon_head
)
418 static inline void isig(int sig
, struct tty_struct
*tty
, int flush
)
421 kill_pg(tty
->pgrp
, sig
, 1);
422 if (flush
|| !L_NOFLSH(tty
)) {
423 n_tty_flush_buffer(tty
);
424 if (tty
->driver
.flush_buffer
)
425 tty
->driver
.flush_buffer(tty
);
429 static inline void n_tty_receive_break(struct tty_struct
*tty
)
434 isig(SIGINT
, tty
, 1);
438 put_tty_queue('\377', tty
);
439 put_tty_queue('\0', tty
);
441 put_tty_queue('\0', tty
);
442 wake_up_interruptible(&tty
->read_wait
);
445 static inline void n_tty_receive_overrun(struct tty_struct
*tty
)
450 if (time_before(tty
->overrun_time
, jiffies
- HZ
)) {
451 printk("%s: %d input overrun(s)\n", tty_name(tty
, buf
),
453 tty
->overrun_time
= jiffies
;
454 tty
->num_overrun
= 0;
458 static inline void n_tty_receive_parity_error(struct tty_struct
*tty
,
465 put_tty_queue('\377', tty
);
466 put_tty_queue('\0', tty
);
467 put_tty_queue(c
, tty
);
468 } else if (I_INPCK(tty
))
469 put_tty_queue('\0', tty
);
471 put_tty_queue(c
, tty
);
472 wake_up_interruptible(&tty
->read_wait
);
475 static inline void n_tty_receive_char(struct tty_struct
*tty
, unsigned char c
)
478 put_tty_queue(c
, tty
);
482 if (tty
->stopped
&& !tty
->flow_stopped
&&
483 I_IXON(tty
) && I_IXANY(tty
)) {
490 if (I_IUCLC(tty
) && L_IEXTEN(tty
))
495 if (c
== START_CHAR(tty
))
497 else if (c
== STOP_CHAR(tty
))
504 * If the previous character was LNEXT, or we know that this
505 * character is not one of the characters that we'll have to
506 * handle specially, do shortcut processing to speed things
509 if (!test_bit(c
, &tty
->process_char_map
) || tty
->lnext
) {
513 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
514 put_char('\a', tty
); /* beep if no space */
517 /* Record the column of first canon char. */
518 if (tty
->canon_head
== tty
->read_head
)
519 tty
->canon_column
= tty
->column
;
522 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
523 put_tty_queue(c
, tty
);
524 put_tty_queue(c
, tty
);
533 } else if (c
== '\n' && I_INLCR(tty
))
536 if (c
== START_CHAR(tty
)) {
540 if (c
== STOP_CHAR(tty
)) {
548 if (c
== INTR_CHAR(tty
))
551 if (c
== QUIT_CHAR(tty
))
554 if (c
== SUSP_CHAR(tty
)) {
556 isig(signal
, tty
, 0);
561 if (c
== ERASE_CHAR(tty
) || c
== KILL_CHAR(tty
) ||
562 (c
== WERASE_CHAR(tty
) && L_IEXTEN(tty
))) {
566 if (c
== LNEXT_CHAR(tty
) && L_IEXTEN(tty
)) {
570 if (L_ECHOCTL(tty
)) {
577 if (c
== REPRINT_CHAR(tty
) && L_ECHO(tty
) &&
579 unsigned long tail
= tty
->canon_head
;
584 while (tail
!= tty
->read_head
) {
585 echo_char(tty
->read_buf
[tail
], tty
);
586 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
591 if (L_ECHO(tty
) || L_ECHONL(tty
)) {
592 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
600 if (c
== EOF_CHAR(tty
)) {
601 if (tty
->canon_head
!= tty
->read_head
)
602 set_bit(TTY_PUSH
, &tty
->flags
);
606 if ((c
== EOL_CHAR(tty
)) ||
607 (c
== EOL2_CHAR(tty
) && L_IEXTEN(tty
))) {
609 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
612 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
616 /* Record the column of first canon char. */
617 if (tty
->canon_head
== tty
->read_head
)
618 tty
->canon_column
= tty
->column
;
622 * XXX does PARMRK doubling happen for
623 * EOL_CHAR and EOL2_CHAR?
625 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
626 put_tty_queue(c
, tty
);
629 set_bit(tty
->read_head
, &tty
->read_flags
);
630 put_tty_queue(c
, tty
);
631 tty
->canon_head
= tty
->read_head
;
633 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
634 if (waitqueue_active(&tty
->read_wait
))
635 wake_up_interruptible(&tty
->read_wait
);
642 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
643 put_char('\a', tty
); /* beep if no space */
649 /* Record the column of first canon char. */
650 if (tty
->canon_head
== tty
->read_head
)
651 tty
->canon_column
= tty
->column
;
656 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
657 put_tty_queue(c
, tty
);
659 put_tty_queue(c
, tty
);
662 static int n_tty_receive_room(struct tty_struct
*tty
)
664 int left
= N_TTY_BUF_SIZE
- tty
->read_cnt
- 1;
667 * If we are doing input canonicalization, and there are no
668 * pending newlines, let characters through without limit, so
669 * that erase characters will be handled. Other excess
670 * characters will be beeped.
672 if (tty
->icanon
&& !tty
->canon_data
)
673 return N_TTY_BUF_SIZE
;
680 static void n_tty_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
683 const unsigned char *p
;
684 char *f
, flags
= TTY_NORMAL
;
687 unsigned long cpuflags
;
693 spin_lock_irqsave(&tty
->read_lock
, cpuflags
);
694 i
= MIN(count
, MIN(N_TTY_BUF_SIZE
- tty
->read_cnt
,
695 N_TTY_BUF_SIZE
- tty
->read_head
));
696 memcpy(tty
->read_buf
+ tty
->read_head
, cp
, i
);
697 tty
->read_head
= (tty
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
702 i
= MIN(count
, MIN(N_TTY_BUF_SIZE
- tty
->read_cnt
,
703 N_TTY_BUF_SIZE
- tty
->read_head
));
704 memcpy(tty
->read_buf
+ tty
->read_head
, cp
, i
);
705 tty
->read_head
= (tty
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
707 spin_unlock_irqrestore(&tty
->read_lock
, cpuflags
);
709 for (i
=count
, p
= cp
, f
= fp
; i
; i
--, p
++) {
714 n_tty_receive_char(tty
, *p
);
717 n_tty_receive_break(tty
);
721 n_tty_receive_parity_error(tty
, *p
);
724 n_tty_receive_overrun(tty
);
727 printk("%s: unknown flag %d\n",
728 tty_name(tty
, buf
), flags
);
732 if (tty
->driver
.flush_chars
)
733 tty
->driver
.flush_chars(tty
);
736 if (!tty
->icanon
&& (tty
->read_cnt
>= tty
->minimum_to_wake
)) {
737 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
738 if (waitqueue_active(&tty
->read_wait
))
739 wake_up_interruptible(&tty
->read_wait
);
743 * Check the remaining room for the input canonicalization
744 * mode. We don't want to throttle the driver if we're in
745 * canonical mode and don't have a newline yet!
747 if (n_tty_receive_room(tty
) < TTY_THRESHOLD_THROTTLE
) {
748 /* check TTY_THROTTLED first so it indicates our state */
749 if (!test_and_set_bit(TTY_THROTTLED
, &tty
->flags
) &&
750 tty
->driver
.throttle
)
751 tty
->driver
.throttle(tty
);
755 int is_ignored(int sig
)
757 return (sigismember(¤t
->blocked
, sig
) ||
758 current
->sig
->action
[sig
-1].sa
.sa_handler
== SIG_IGN
);
761 static void n_tty_set_termios(struct tty_struct
*tty
, struct termios
* old
)
766 tty
->icanon
= (L_ICANON(tty
) != 0);
767 if (test_bit(TTY_HW_COOK_IN
, &tty
->flags
)) {
772 if (I_ISTRIP(tty
) || I_IUCLC(tty
) || I_IGNCR(tty
) ||
773 I_ICRNL(tty
) || I_INLCR(tty
) || L_ICANON(tty
) ||
774 I_IXON(tty
) || L_ISIG(tty
) || L_ECHO(tty
) ||
777 memset(tty
->process_char_map
, 0, 256/8);
779 if (I_IGNCR(tty
) || I_ICRNL(tty
))
780 set_bit('\r', &tty
->process_char_map
);
782 set_bit('\n', &tty
->process_char_map
);
785 set_bit(ERASE_CHAR(tty
), &tty
->process_char_map
);
786 set_bit(KILL_CHAR(tty
), &tty
->process_char_map
);
787 set_bit(EOF_CHAR(tty
), &tty
->process_char_map
);
788 set_bit('\n', &tty
->process_char_map
);
789 set_bit(EOL_CHAR(tty
), &tty
->process_char_map
);
791 set_bit(WERASE_CHAR(tty
),
792 &tty
->process_char_map
);
793 set_bit(LNEXT_CHAR(tty
),
794 &tty
->process_char_map
);
795 set_bit(EOL2_CHAR(tty
),
796 &tty
->process_char_map
);
798 set_bit(REPRINT_CHAR(tty
),
799 &tty
->process_char_map
);
803 set_bit(START_CHAR(tty
), &tty
->process_char_map
);
804 set_bit(STOP_CHAR(tty
), &tty
->process_char_map
);
807 set_bit(INTR_CHAR(tty
), &tty
->process_char_map
);
808 set_bit(QUIT_CHAR(tty
), &tty
->process_char_map
);
809 set_bit(SUSP_CHAR(tty
), &tty
->process_char_map
);
811 clear_bit(__DISABLED_CHAR
, &tty
->process_char_map
);
817 if ((I_IGNBRK(tty
) || (!I_BRKINT(tty
) && !I_PARMRK(tty
))) &&
818 (I_IGNPAR(tty
) || !I_INPCK(tty
)) &&
819 (tty
->driver
.flags
& TTY_DRIVER_REAL_RAW
))
826 static void n_tty_close(struct tty_struct
*tty
)
828 n_tty_flush_buffer(tty
);
830 free_page((unsigned long) tty
->read_buf
);
835 static int n_tty_open(struct tty_struct
*tty
)
840 if (!tty
->read_buf
) {
841 tty
->read_buf
= (unsigned char *)
842 get_zeroed_page(in_interrupt() ? GFP_ATOMIC
: GFP_KERNEL
);
846 memset(tty
->read_buf
, 0, N_TTY_BUF_SIZE
);
847 reset_buffer_flags(tty
);
849 n_tty_set_termios(tty
, 0);
850 tty
->minimum_to_wake
= 1;
855 static inline int input_available_p(struct tty_struct
*tty
, int amt
)
860 } else if (tty
->read_cnt
>= (amt
? amt
: 1))
867 * Helper function to speed up read_chan. It is only called when
868 * ICANON is off; it copies characters straight from the tty queue to
869 * user space directly. It can be profitably called twice; once to
870 * drain the space from the tail pointer to the (physical) end of the
871 * buffer, and once to drain the space from the (physical) beginning of
872 * the buffer to head pointer.
874 static inline int copy_from_read_buf(struct tty_struct
*tty
,
884 spin_lock_irqsave(&tty
->read_lock
, flags
);
885 n
= MIN(*nr
, MIN(tty
->read_cnt
, N_TTY_BUF_SIZE
- tty
->read_tail
));
886 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
889 retval
= copy_to_user(*b
, &tty
->read_buf
[tty
->read_tail
], n
);
891 spin_lock_irqsave(&tty
->read_lock
, flags
);
892 tty
->read_tail
= (tty
->read_tail
+ n
) & (N_TTY_BUF_SIZE
-1);
894 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
901 static ssize_t
read_chan(struct tty_struct
*tty
, struct file
*file
,
902 unsigned char *buf
, size_t nr
)
904 unsigned char *b
= buf
;
905 DECLARE_WAITQUEUE(wait
, current
);
915 if (!tty
->read_buf
) {
916 printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
920 /* Job control check -- must be done at start and after
921 every sleep (POSIX.1 7.1.1.4). */
922 /* NOTE: not yet done after every sleep pending a thorough
923 check of the logic of this change. -- jlc */
924 /* don't stop on /dev/console */
925 if (file
->f_dentry
->d_inode
->i_rdev
!= CONSOLE_DEV
&&
926 file
->f_dentry
->d_inode
->i_rdev
!= SYSCONS_DEV
&&
927 current
->tty
== tty
) {
929 printk("read_chan: tty->pgrp <= 0!\n");
930 else if (current
->pgrp
!= tty
->pgrp
) {
931 if (is_ignored(SIGTTIN
) ||
932 is_orphaned_pgrp(current
->pgrp
))
934 kill_pg(current
->pgrp
, SIGTTIN
, 1);
940 timeout
= MAX_SCHEDULE_TIMEOUT
;
942 time
= (HZ
/ 10) * TIME_CHAR(tty
);
943 minimum
= MIN_CHAR(tty
);
946 tty
->minimum_to_wake
= 1;
947 else if (!waitqueue_active(&tty
->read_wait
) ||
948 (tty
->minimum_to_wake
> minimum
))
949 tty
->minimum_to_wake
= minimum
;
956 tty
->minimum_to_wake
= minimum
= 1;
960 if (file
->f_flags
& O_NONBLOCK
) {
961 if (down_trylock(&tty
->atomic_read
))
965 if (down_interruptible(&tty
->atomic_read
))
969 add_wait_queue(&tty
->read_wait
, &wait
);
970 set_bit(TTY_DONT_FLIP
, &tty
->flags
);
972 /* First test for status change. */
973 if (tty
->packet
&& tty
->link
->ctrl_status
) {
977 cs
= tty
->link
->ctrl_status
;
978 tty
->link
->ctrl_status
= 0;
983 /* This statement must be first before checking for input
984 so that any interrupt will set the state back to
986 set_current_state(TASK_INTERRUPTIBLE
);
988 if (((minimum
- (b
- buf
)) < tty
->minimum_to_wake
) &&
989 ((minimum
- (b
- buf
)) >= 1))
990 tty
->minimum_to_wake
= (minimum
- (b
- buf
));
992 if (!input_available_p(tty
, 0)) {
993 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
)) {
997 if (tty_hung_up_p(file
))
1001 if (file
->f_flags
& O_NONBLOCK
) {
1005 if (signal_pending(current
)) {
1006 retval
= -ERESTARTSYS
;
1009 clear_bit(TTY_DONT_FLIP
, &tty
->flags
);
1010 timeout
= schedule_timeout(timeout
);
1011 set_bit(TTY_DONT_FLIP
, &tty
->flags
);
1014 current
->state
= TASK_RUNNING
;
1016 /* Deal with packet mode. */
1017 if (tty
->packet
&& b
== buf
) {
1018 put_user(TIOCPKT_DATA
, b
++);
1023 /* N.B. avoid overrun if nr == 0 */
1024 while (nr
&& tty
->read_cnt
) {
1027 eol
= test_and_clear_bit(tty
->read_tail
,
1029 c
= tty
->read_buf
[tty
->read_tail
];
1030 spin_lock_irqsave(&tty
->read_lock
, flags
);
1031 tty
->read_tail
= ((tty
->read_tail
+1) &
1032 (N_TTY_BUF_SIZE
-1));
1034 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
1036 if (!eol
|| (c
!= __DISABLED_CHAR
)) {
1041 /* this test should be redundant:
1042 * we shouldn't be reading data if
1045 if (--tty
->canon_data
< 0)
1046 tty
->canon_data
= 0;
1052 uncopied
= copy_from_read_buf(tty
, &b
, &nr
);
1053 uncopied
+= copy_from_read_buf(tty
, &b
, &nr
);
1060 /* If there is enough space in the read buffer now, let the
1061 * low-level driver know. We use n_tty_chars_in_buffer() to
1062 * check the buffer, as it now knows about canonical mode.
1063 * Otherwise, if the driver is throttled and the line is
1064 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1065 * we won't get any more characters.
1067 if (n_tty_chars_in_buffer(tty
) <= TTY_THRESHOLD_UNTHROTTLE
)
1068 check_unthrottle(tty
);
1070 if (b
- buf
>= minimum
)
1075 clear_bit(TTY_DONT_FLIP
, &tty
->flags
);
1076 up(&tty
->atomic_read
);
1077 remove_wait_queue(&tty
->read_wait
, &wait
);
1079 if (!waitqueue_active(&tty
->read_wait
))
1080 tty
->minimum_to_wake
= minimum
;
1082 current
->state
= TASK_RUNNING
;
1087 clear_bit(TTY_PUSH
, &tty
->flags
);
1088 } else if (test_and_clear_bit(TTY_PUSH
, &tty
->flags
))
1094 static ssize_t
write_chan(struct tty_struct
* tty
, struct file
* file
,
1095 const unsigned char * buf
, size_t nr
)
1097 const unsigned char *b
= buf
;
1098 DECLARE_WAITQUEUE(wait
, current
);
1102 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1103 if (L_TOSTOP(tty
) &&
1104 file
->f_dentry
->d_inode
->i_rdev
!= CONSOLE_DEV
&&
1105 file
->f_dentry
->d_inode
->i_rdev
!= SYSCONS_DEV
) {
1106 retval
= tty_check_change(tty
);
1111 add_wait_queue(&tty
->write_wait
, &wait
);
1113 set_current_state(TASK_INTERRUPTIBLE
);
1114 if (signal_pending(current
)) {
1115 retval
= -ERESTARTSYS
;
1118 if (tty_hung_up_p(file
) || (tty
->link
&& !tty
->link
->count
)) {
1122 if (O_OPOST(tty
) && !(test_bit(TTY_HW_COOK_OUT
, &tty
->flags
))) {
1124 ssize_t num
= opost_block(tty
, b
, nr
);
1133 current
->state
= TASK_RUNNING
;
1135 current
->state
= TASK_INTERRUPTIBLE
;
1136 if (opost(c
, tty
) < 0)
1140 if (tty
->driver
.flush_chars
)
1141 tty
->driver
.flush_chars(tty
);
1143 current
->state
= TASK_RUNNING
;
1144 c
= tty
->driver
.write(tty
, 1, b
, nr
);
1145 current
->state
= TASK_INTERRUPTIBLE
;
1155 if (file
->f_flags
& O_NONBLOCK
) {
1162 current
->state
= TASK_RUNNING
;
1163 remove_wait_queue(&tty
->write_wait
, &wait
);
1164 return (b
- buf
) ? b
- buf
: retval
;
1167 /* Called without the kernel lock held - fine */
1168 static unsigned int normal_poll(struct tty_struct
* tty
, struct file
* file
, poll_table
*wait
)
1170 unsigned int mask
= 0;
1172 poll_wait(file
, &tty
->read_wait
, wait
);
1173 poll_wait(file
, &tty
->write_wait
, wait
);
1174 if (input_available_p(tty
, TIME_CHAR(tty
) ? 0 : MIN_CHAR(tty
)))
1175 mask
|= POLLIN
| POLLRDNORM
;
1176 if (tty
->packet
&& tty
->link
->ctrl_status
)
1177 mask
|= POLLPRI
| POLLIN
| POLLRDNORM
;
1178 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
))
1180 if (tty_hung_up_p(file
))
1182 if (!(mask
& (POLLHUP
| POLLIN
| POLLRDNORM
))) {
1183 if (MIN_CHAR(tty
) && !TIME_CHAR(tty
))
1184 tty
->minimum_to_wake
= MIN_CHAR(tty
);
1186 tty
->minimum_to_wake
= 1;
1188 if (tty
->driver
.chars_in_buffer(tty
) < WAKEUP_CHARS
)
1189 mask
|= POLLOUT
| POLLWRNORM
;
1193 struct tty_ldisc tty_ldisc_N_TTY
= {
1194 TTY_LDISC_MAGIC
, /* magic */
1198 n_tty_open
, /* open */
1199 n_tty_close
, /* close */
1200 n_tty_flush_buffer
, /* flush_buffer */
1201 n_tty_chars_in_buffer
, /* chars_in_buffer */
1202 read_chan
, /* read */
1203 write_chan
, /* write */
1204 n_tty_ioctl
, /* ioctl */
1205 n_tty_set_termios
, /* set_termios */
1206 normal_poll
, /* poll */
1207 n_tty_receive_buf
, /* receive_buf */
1208 n_tty_receive_room
, /* receive_room */
1209 0 /* write_wakeup */