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 /* 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 unsigned char *alloc_buf(void)
67 int prio
= in_interrupt() ? GFP_ATOMIC
: GFP_KERNEL
;
69 if (PAGE_SIZE
!= N_TTY_BUF_SIZE
) {
70 p
= kmalloc(N_TTY_BUF_SIZE
, prio
);
72 memset(p
, 0, N_TTY_BUF_SIZE
);
74 p
= (unsigned char *)get_zeroed_page(prio
);
79 static inline void free_buf(unsigned char *buf
)
81 if (PAGE_SIZE
!= N_TTY_BUF_SIZE
)
84 free_page((unsigned long) buf
);
87 static inline void put_tty_queue_nolock(unsigned char c
, struct tty_struct
*tty
)
89 if (tty
->read_cnt
< N_TTY_BUF_SIZE
) {
90 tty
->read_buf
[tty
->read_head
] = c
;
91 tty
->read_head
= (tty
->read_head
+ 1) & (N_TTY_BUF_SIZE
-1);
96 static inline void put_tty_queue(unsigned char c
, struct tty_struct
*tty
)
100 * The problem of stomping on the buffers ends here.
101 * Why didn't anyone see this one coming? --AJK
103 spin_lock_irqsave(&tty
->read_lock
, flags
);
104 put_tty_queue_nolock(c
, tty
);
105 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
109 * Check whether to call the driver.unthrottle function.
110 * We test the TTY_THROTTLED bit first so that it always
111 * indicates the current state.
113 static void check_unthrottle(struct tty_struct
* tty
)
116 test_and_clear_bit(TTY_THROTTLED
, &tty
->flags
) &&
117 tty
->driver
->unthrottle
)
118 tty
->driver
->unthrottle(tty
);
122 * Reset the read buffer counters, clear the flags,
123 * and make sure the driver is unthrottled. Called
124 * from n_tty_open() and n_tty_flush_buffer().
126 static void reset_buffer_flags(struct tty_struct
*tty
)
130 spin_lock_irqsave(&tty
->read_lock
, flags
);
131 tty
->read_head
= tty
->read_tail
= tty
->read_cnt
= 0;
132 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
133 tty
->canon_head
= tty
->canon_data
= tty
->erasing
= 0;
134 memset(&tty
->read_flags
, 0, sizeof tty
->read_flags
);
135 check_unthrottle(tty
);
139 * Flush the input buffer
141 void n_tty_flush_buffer(struct tty_struct
* tty
)
143 /* clear everything and unthrottle the driver */
144 reset_buffer_flags(tty
);
149 if (tty
->link
->packet
) {
150 tty
->ctrl_status
|= TIOCPKT_FLUSHREAD
;
151 wake_up_interruptible(&tty
->link
->read_wait
);
156 * Return number of characters buffered to be delivered to user
158 ssize_t
n_tty_chars_in_buffer(struct tty_struct
*tty
)
163 spin_lock_irqsave(&tty
->read_lock
, flags
);
166 } else if (tty
->canon_data
) {
167 n
= (tty
->canon_head
> tty
->read_tail
) ?
168 tty
->canon_head
- tty
->read_tail
:
169 tty
->canon_head
+ (N_TTY_BUF_SIZE
- tty
->read_tail
);
171 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
176 * Perform OPOST processing. Returns -1 when the output device is
177 * full and the character must be retried.
179 static int opost(unsigned char c
, struct tty_struct
*tty
)
183 space
= tty
->driver
->write_room(tty
);
195 tty
->driver
->put_char(tty
, '\r');
198 tty
->canon_column
= tty
->column
;
201 if (O_ONOCR(tty
) && tty
->column
== 0)
206 tty
->canon_column
= tty
->column
= 0;
209 tty
->canon_column
= tty
->column
= 0;
212 spaces
= 8 - (tty
->column
& 7);
213 if (O_TABDLY(tty
) == XTABS
) {
216 tty
->column
+= spaces
;
217 tty
->driver
->write(tty
, 0, " ", spaces
);
220 tty
->column
+= spaces
;
234 tty
->driver
->put_char(tty
, c
);
239 * opost_block --- to speed up block console writes, among other
242 static ssize_t
opost_block(struct tty_struct
* tty
,
243 const unsigned char * inbuf
, unsigned int nr
)
250 space
= tty
->driver
->write_room(tty
);
255 if (nr
> sizeof(buf
))
258 if (copy_from_user(buf
, inbuf
, nr
))
261 for (i
= 0, cp
= buf
; i
< nr
; i
++, cp
++) {
268 tty
->canon_column
= tty
->column
;
271 if (O_ONOCR(tty
) && tty
->column
== 0)
276 tty
->canon_column
= tty
->column
= 0;
279 tty
->canon_column
= tty
->column
= 0;
296 if (tty
->driver
->flush_chars
)
297 tty
->driver
->flush_chars(tty
);
298 i
= tty
->driver
->write(tty
, 0, buf
, i
);
304 static inline void put_char(unsigned char c
, struct tty_struct
*tty
)
306 tty
->driver
->put_char(tty
, c
);
309 /* Must be called only when L_ECHO(tty) is true. */
311 static void echo_char(unsigned char c
, struct tty_struct
*tty
)
313 if (L_ECHOCTL(tty
) && iscntrl(c
) && c
!= '\t') {
315 put_char(c
^ 0100, tty
);
321 static inline void finish_erasing(struct tty_struct
*tty
)
330 static void eraser(unsigned char c
, struct tty_struct
*tty
)
332 enum { ERASE
, WERASE
, KILL
} kill_type
;
333 int head
, seen_alnums
;
336 if (tty
->read_head
== tty
->canon_head
) {
337 /* opost('\a', tty); */ /* what do you think? */
340 if (c
== ERASE_CHAR(tty
))
342 else if (c
== WERASE_CHAR(tty
))
346 spin_lock_irqsave(&tty
->read_lock
, flags
);
347 tty
->read_cnt
-= ((tty
->read_head
- tty
->canon_head
) &
348 (N_TTY_BUF_SIZE
- 1));
349 tty
->read_head
= tty
->canon_head
;
350 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
353 if (!L_ECHOK(tty
) || !L_ECHOKE(tty
) || !L_ECHOE(tty
)) {
354 spin_lock_irqsave(&tty
->read_lock
, flags
);
355 tty
->read_cnt
-= ((tty
->read_head
- tty
->canon_head
) &
356 (N_TTY_BUF_SIZE
- 1));
357 tty
->read_head
= tty
->canon_head
;
358 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
360 echo_char(KILL_CHAR(tty
), tty
);
361 /* Add a newline if ECHOK is on and ECHOKE is off. */
370 while (tty
->read_head
!= tty
->canon_head
) {
371 head
= (tty
->read_head
- 1) & (N_TTY_BUF_SIZE
-1);
372 c
= tty
->read_buf
[head
];
373 if (kill_type
== WERASE
) {
374 /* Equivalent to BSD's ALTWERASE. */
375 if (isalnum(c
) || c
== '_')
377 else if (seen_alnums
)
380 spin_lock_irqsave(&tty
->read_lock
, flags
);
381 tty
->read_head
= head
;
383 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
385 if (L_ECHOPRT(tty
)) {
392 } else if (kill_type
== ERASE
&& !L_ECHOE(tty
)) {
393 echo_char(ERASE_CHAR(tty
), tty
);
394 } else if (c
== '\t') {
395 unsigned int col
= tty
->canon_column
;
396 unsigned long tail
= tty
->canon_head
;
398 /* Find the column of the last char. */
399 while (tail
!= tty
->read_head
) {
400 c
= tty
->read_buf
[tail
];
403 else if (iscntrl(c
)) {
408 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
411 /* should never happen */
412 if (tty
->column
> 0x80000000)
415 /* Now backup to that column. */
416 while (tty
->column
> col
) {
417 /* Can't use opost here. */
423 if (iscntrl(c
) && L_ECHOCTL(tty
)) {
430 if (!iscntrl(c
) || L_ECHOCTL(tty
)) {
439 if (kill_type
== ERASE
)
442 if (tty
->read_head
== tty
->canon_head
)
446 static inline void isig(int sig
, struct tty_struct
*tty
, int flush
)
449 kill_pg(tty
->pgrp
, sig
, 1);
450 if (flush
|| !L_NOFLSH(tty
)) {
451 n_tty_flush_buffer(tty
);
452 if (tty
->driver
->flush_buffer
)
453 tty
->driver
->flush_buffer(tty
);
457 static inline void n_tty_receive_break(struct tty_struct
*tty
)
462 isig(SIGINT
, tty
, 1);
466 put_tty_queue('\377', tty
);
467 put_tty_queue('\0', tty
);
469 put_tty_queue('\0', tty
);
470 wake_up_interruptible(&tty
->read_wait
);
473 static inline void n_tty_receive_overrun(struct tty_struct
*tty
)
478 if (time_before(tty
->overrun_time
, jiffies
- HZ
)) {
479 printk("%s: %d input overrun(s)\n", tty_name(tty
, buf
),
481 tty
->overrun_time
= jiffies
;
482 tty
->num_overrun
= 0;
486 static inline void n_tty_receive_parity_error(struct tty_struct
*tty
,
493 put_tty_queue('\377', tty
);
494 put_tty_queue('\0', tty
);
495 put_tty_queue(c
, tty
);
496 } else if (I_INPCK(tty
))
497 put_tty_queue('\0', tty
);
499 put_tty_queue(c
, tty
);
500 wake_up_interruptible(&tty
->read_wait
);
503 static inline void n_tty_receive_char(struct tty_struct
*tty
, unsigned char c
)
508 put_tty_queue(c
, tty
);
512 if (tty
->stopped
&& !tty
->flow_stopped
&&
513 I_IXON(tty
) && I_IXANY(tty
)) {
520 if (I_IUCLC(tty
) && L_IEXTEN(tty
))
525 if (c
== START_CHAR(tty
))
527 else if (c
== STOP_CHAR(tty
))
534 * If the previous character was LNEXT, or we know that this
535 * character is not one of the characters that we'll have to
536 * handle specially, do shortcut processing to speed things
539 if (!test_bit(c
, tty
->process_char_map
) || tty
->lnext
) {
543 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
544 put_char('\a', tty
); /* beep if no space */
547 /* Record the column of first canon char. */
548 if (tty
->canon_head
== tty
->read_head
)
549 tty
->canon_column
= tty
->column
;
552 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
553 put_tty_queue(c
, tty
);
554 put_tty_queue(c
, tty
);
563 } else if (c
== '\n' && I_INLCR(tty
))
566 if (c
== START_CHAR(tty
)) {
570 if (c
== STOP_CHAR(tty
)) {
578 if (c
== INTR_CHAR(tty
))
581 if (c
== QUIT_CHAR(tty
))
584 if (c
== SUSP_CHAR(tty
)) {
586 isig(signal
, tty
, 0);
591 if (c
== ERASE_CHAR(tty
) || c
== KILL_CHAR(tty
) ||
592 (c
== WERASE_CHAR(tty
) && L_IEXTEN(tty
))) {
596 if (c
== LNEXT_CHAR(tty
) && L_IEXTEN(tty
)) {
600 if (L_ECHOCTL(tty
)) {
607 if (c
== REPRINT_CHAR(tty
) && L_ECHO(tty
) &&
609 unsigned long tail
= tty
->canon_head
;
614 while (tail
!= tty
->read_head
) {
615 echo_char(tty
->read_buf
[tail
], tty
);
616 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
621 if (L_ECHO(tty
) || L_ECHONL(tty
)) {
622 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
630 if (c
== EOF_CHAR(tty
)) {
631 if (tty
->canon_head
!= tty
->read_head
)
632 set_bit(TTY_PUSH
, &tty
->flags
);
636 if ((c
== EOL_CHAR(tty
)) ||
637 (c
== EOL2_CHAR(tty
) && L_IEXTEN(tty
))) {
639 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
642 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
646 /* Record the column of first canon char. */
647 if (tty
->canon_head
== tty
->read_head
)
648 tty
->canon_column
= tty
->column
;
652 * XXX does PARMRK doubling happen for
653 * EOL_CHAR and EOL2_CHAR?
655 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
656 put_tty_queue(c
, tty
);
659 spin_lock_irqsave(&tty
->read_lock
, flags
);
660 set_bit(tty
->read_head
, tty
->read_flags
);
661 put_tty_queue_nolock(c
, tty
);
662 tty
->canon_head
= tty
->read_head
;
664 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
665 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
666 if (waitqueue_active(&tty
->read_wait
))
667 wake_up_interruptible(&tty
->read_wait
);
674 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
675 put_char('\a', tty
); /* beep if no space */
681 /* Record the column of first canon char. */
682 if (tty
->canon_head
== tty
->read_head
)
683 tty
->canon_column
= tty
->column
;
688 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
689 put_tty_queue(c
, tty
);
691 put_tty_queue(c
, tty
);
694 static int n_tty_receive_room(struct tty_struct
*tty
)
696 int left
= N_TTY_BUF_SIZE
- tty
->read_cnt
- 1;
699 * If we are doing input canonicalization, and there are no
700 * pending newlines, let characters through without limit, so
701 * that erase characters will be handled. Other excess
702 * characters will be beeped.
704 if (tty
->icanon
&& !tty
->canon_data
)
705 return N_TTY_BUF_SIZE
;
713 * Required for the ptys, serial driver etc. since processes
714 * that attach themselves to the master and rely on ASYNC
715 * IO must be woken up
718 static void n_tty_write_wakeup(struct tty_struct
*tty
)
722 set_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
723 kill_fasync(&tty
->fasync
, SIGIO
, POLL_OUT
);
728 static void n_tty_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
731 const unsigned char *p
;
732 char *f
, flags
= TTY_NORMAL
;
735 unsigned long cpuflags
;
741 spin_lock_irqsave(&tty
->read_lock
, cpuflags
);
742 i
= min(N_TTY_BUF_SIZE
- tty
->read_cnt
,
743 N_TTY_BUF_SIZE
- tty
->read_head
);
745 memcpy(tty
->read_buf
+ tty
->read_head
, cp
, i
);
746 tty
->read_head
= (tty
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
751 i
= min(N_TTY_BUF_SIZE
- tty
->read_cnt
,
752 N_TTY_BUF_SIZE
- tty
->read_head
);
754 memcpy(tty
->read_buf
+ tty
->read_head
, cp
, i
);
755 tty
->read_head
= (tty
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
757 spin_unlock_irqrestore(&tty
->read_lock
, cpuflags
);
759 for (i
=count
, p
= cp
, f
= fp
; i
; i
--, p
++) {
764 n_tty_receive_char(tty
, *p
);
767 n_tty_receive_break(tty
);
771 n_tty_receive_parity_error(tty
, *p
);
774 n_tty_receive_overrun(tty
);
777 printk("%s: unknown flag %d\n",
778 tty_name(tty
, buf
), flags
);
782 if (tty
->driver
->flush_chars
)
783 tty
->driver
->flush_chars(tty
);
786 if (!tty
->icanon
&& (tty
->read_cnt
>= tty
->minimum_to_wake
)) {
787 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
788 if (waitqueue_active(&tty
->read_wait
))
789 wake_up_interruptible(&tty
->read_wait
);
793 * Check the remaining room for the input canonicalization
794 * mode. We don't want to throttle the driver if we're in
795 * canonical mode and don't have a newline yet!
797 if (n_tty_receive_room(tty
) < TTY_THRESHOLD_THROTTLE
) {
798 /* check TTY_THROTTLED first so it indicates our state */
799 if (!test_and_set_bit(TTY_THROTTLED
, &tty
->flags
) &&
800 tty
->driver
->throttle
)
801 tty
->driver
->throttle(tty
);
805 int is_ignored(int sig
)
807 return (sigismember(¤t
->blocked
, sig
) ||
808 current
->sighand
->action
[sig
-1].sa
.sa_handler
== SIG_IGN
);
811 static void n_tty_set_termios(struct tty_struct
*tty
, struct termios
* old
)
816 tty
->icanon
= (L_ICANON(tty
) != 0);
817 if (test_bit(TTY_HW_COOK_IN
, &tty
->flags
)) {
822 if (I_ISTRIP(tty
) || I_IUCLC(tty
) || I_IGNCR(tty
) ||
823 I_ICRNL(tty
) || I_INLCR(tty
) || L_ICANON(tty
) ||
824 I_IXON(tty
) || L_ISIG(tty
) || L_ECHO(tty
) ||
826 local_irq_disable(); // FIXME: is this safe?
827 memset(tty
->process_char_map
, 0, 256/8);
829 if (I_IGNCR(tty
) || I_ICRNL(tty
))
830 set_bit('\r', tty
->process_char_map
);
832 set_bit('\n', tty
->process_char_map
);
835 set_bit(ERASE_CHAR(tty
), tty
->process_char_map
);
836 set_bit(KILL_CHAR(tty
), tty
->process_char_map
);
837 set_bit(EOF_CHAR(tty
), tty
->process_char_map
);
838 set_bit('\n', tty
->process_char_map
);
839 set_bit(EOL_CHAR(tty
), tty
->process_char_map
);
841 set_bit(WERASE_CHAR(tty
),
842 tty
->process_char_map
);
843 set_bit(LNEXT_CHAR(tty
),
844 tty
->process_char_map
);
845 set_bit(EOL2_CHAR(tty
),
846 tty
->process_char_map
);
848 set_bit(REPRINT_CHAR(tty
),
849 tty
->process_char_map
);
853 set_bit(START_CHAR(tty
), tty
->process_char_map
);
854 set_bit(STOP_CHAR(tty
), tty
->process_char_map
);
857 set_bit(INTR_CHAR(tty
), tty
->process_char_map
);
858 set_bit(QUIT_CHAR(tty
), tty
->process_char_map
);
859 set_bit(SUSP_CHAR(tty
), tty
->process_char_map
);
861 clear_bit(__DISABLED_CHAR
, tty
->process_char_map
);
862 local_irq_enable(); // FIXME: is this safe?
867 if ((I_IGNBRK(tty
) || (!I_BRKINT(tty
) && !I_PARMRK(tty
))) &&
868 (I_IGNPAR(tty
) || !I_INPCK(tty
)) &&
869 (tty
->driver
->flags
& TTY_DRIVER_REAL_RAW
))
876 static void n_tty_close(struct tty_struct
*tty
)
878 n_tty_flush_buffer(tty
);
880 free_buf(tty
->read_buf
);
885 static int n_tty_open(struct tty_struct
*tty
)
890 if (!tty
->read_buf
) {
891 tty
->read_buf
= alloc_buf();
895 memset(tty
->read_buf
, 0, N_TTY_BUF_SIZE
);
896 reset_buffer_flags(tty
);
898 n_tty_set_termios(tty
, 0);
899 tty
->minimum_to_wake
= 1;
904 static inline int input_available_p(struct tty_struct
*tty
, int amt
)
909 } else if (tty
->read_cnt
>= (amt
? amt
: 1))
916 * Helper function to speed up read_chan. It is only called when
917 * ICANON is off; it copies characters straight from the tty queue to
918 * user space directly. It can be profitably called twice; once to
919 * drain the space from the tail pointer to the (physical) end of the
920 * buffer, and once to drain the space from the (physical) beginning of
921 * the buffer to head pointer.
923 static inline int copy_from_read_buf(struct tty_struct
*tty
,
933 spin_lock_irqsave(&tty
->read_lock
, flags
);
934 n
= min(tty
->read_cnt
, N_TTY_BUF_SIZE
- tty
->read_tail
);
935 n
= min((ssize_t
)*nr
, n
);
936 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
939 retval
= copy_to_user(*b
, &tty
->read_buf
[tty
->read_tail
], n
);
941 spin_lock_irqsave(&tty
->read_lock
, flags
);
942 tty
->read_tail
= (tty
->read_tail
+ n
) & (N_TTY_BUF_SIZE
-1);
944 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
951 extern ssize_t
redirected_tty_write(struct file
*,const char *,size_t,loff_t
*);
953 static ssize_t
read_chan(struct tty_struct
*tty
, struct file
*file
,
954 unsigned char *buf
, size_t nr
)
956 unsigned char *b
= buf
;
957 DECLARE_WAITQUEUE(wait
, current
);
967 if (!tty
->read_buf
) {
968 printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
972 /* Job control check -- must be done at start and after
973 every sleep (POSIX.1 7.1.1.4). */
974 /* NOTE: not yet done after every sleep pending a thorough
975 check of the logic of this change. -- jlc */
976 /* don't stop on /dev/console */
977 if (file
->f_op
->write
!= redirected_tty_write
&& current
->tty
== tty
) {
979 printk("read_chan: tty->pgrp <= 0!\n");
980 else if (process_group(current
) != tty
->pgrp
) {
981 if (is_ignored(SIGTTIN
) ||
982 is_orphaned_pgrp(process_group(current
)))
984 kill_pg(process_group(current
), SIGTTIN
, 1);
990 timeout
= MAX_SCHEDULE_TIMEOUT
;
992 time
= (HZ
/ 10) * TIME_CHAR(tty
);
993 minimum
= MIN_CHAR(tty
);
996 tty
->minimum_to_wake
= 1;
997 else if (!waitqueue_active(&tty
->read_wait
) ||
998 (tty
->minimum_to_wake
> minimum
))
999 tty
->minimum_to_wake
= minimum
;
1006 tty
->minimum_to_wake
= minimum
= 1;
1010 if (file
->f_flags
& O_NONBLOCK
) {
1011 if (down_trylock(&tty
->atomic_read
))
1015 if (down_interruptible(&tty
->atomic_read
))
1016 return -ERESTARTSYS
;
1019 add_wait_queue(&tty
->read_wait
, &wait
);
1020 set_bit(TTY_DONT_FLIP
, &tty
->flags
);
1022 /* First test for status change. */
1023 if (tty
->packet
&& tty
->link
->ctrl_status
) {
1027 cs
= tty
->link
->ctrl_status
;
1028 tty
->link
->ctrl_status
= 0;
1029 if (put_user(cs
, b
++)) {
1037 /* This statement must be first before checking for input
1038 so that any interrupt will set the state back to
1040 set_current_state(TASK_INTERRUPTIBLE
);
1042 if (((minimum
- (b
- buf
)) < tty
->minimum_to_wake
) &&
1043 ((minimum
- (b
- buf
)) >= 1))
1044 tty
->minimum_to_wake
= (minimum
- (b
- buf
));
1046 if (!input_available_p(tty
, 0)) {
1047 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
)) {
1051 if (tty_hung_up_p(file
))
1055 if (file
->f_flags
& O_NONBLOCK
) {
1059 if (signal_pending(current
)) {
1060 retval
= -ERESTARTSYS
;
1063 clear_bit(TTY_DONT_FLIP
, &tty
->flags
);
1064 timeout
= schedule_timeout(timeout
);
1065 set_bit(TTY_DONT_FLIP
, &tty
->flags
);
1068 current
->state
= TASK_RUNNING
;
1070 /* Deal with packet mode. */
1071 if (tty
->packet
&& b
== buf
) {
1072 if (put_user(TIOCPKT_DATA
, b
++)) {
1081 /* N.B. avoid overrun if nr == 0 */
1082 while (nr
&& tty
->read_cnt
) {
1085 eol
= test_and_clear_bit(tty
->read_tail
,
1087 c
= tty
->read_buf
[tty
->read_tail
];
1088 spin_lock_irqsave(&tty
->read_lock
, flags
);
1089 tty
->read_tail
= ((tty
->read_tail
+1) &
1090 (N_TTY_BUF_SIZE
-1));
1093 /* this test should be redundant:
1094 * we shouldn't be reading data if
1097 if (--tty
->canon_data
< 0)
1098 tty
->canon_data
= 0;
1100 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
1102 if (!eol
|| (c
!= __DISABLED_CHAR
)) {
1103 if (put_user(c
, b
++)) {
1117 uncopied
= copy_from_read_buf(tty
, &b
, &nr
);
1118 uncopied
+= copy_from_read_buf(tty
, &b
, &nr
);
1125 /* If there is enough space in the read buffer now, let the
1126 * low-level driver know. We use n_tty_chars_in_buffer() to
1127 * check the buffer, as it now knows about canonical mode.
1128 * Otherwise, if the driver is throttled and the line is
1129 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1130 * we won't get any more characters.
1132 if (n_tty_chars_in_buffer(tty
) <= TTY_THRESHOLD_UNTHROTTLE
)
1133 check_unthrottle(tty
);
1135 if (b
- buf
>= minimum
)
1140 clear_bit(TTY_DONT_FLIP
, &tty
->flags
);
1141 up(&tty
->atomic_read
);
1142 remove_wait_queue(&tty
->read_wait
, &wait
);
1144 if (!waitqueue_active(&tty
->read_wait
))
1145 tty
->minimum_to_wake
= minimum
;
1147 current
->state
= TASK_RUNNING
;
1152 clear_bit(TTY_PUSH
, &tty
->flags
);
1153 } else if (test_and_clear_bit(TTY_PUSH
, &tty
->flags
))
1159 static ssize_t
write_chan(struct tty_struct
* tty
, struct file
* file
,
1160 const unsigned char * buf
, size_t nr
)
1162 const unsigned char *b
= buf
;
1163 DECLARE_WAITQUEUE(wait
, current
);
1167 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1168 if (L_TOSTOP(tty
) && file
->f_op
->write
!= redirected_tty_write
) {
1169 retval
= tty_check_change(tty
);
1174 add_wait_queue(&tty
->write_wait
, &wait
);
1176 set_current_state(TASK_INTERRUPTIBLE
);
1177 if (signal_pending(current
)) {
1178 retval
= -ERESTARTSYS
;
1181 if (tty_hung_up_p(file
) || (tty
->link
&& !tty
->link
->count
)) {
1185 if (O_OPOST(tty
) && !(test_bit(TTY_HW_COOK_OUT
, &tty
->flags
))) {
1187 ssize_t num
= opost_block(tty
, b
, nr
);
1199 if (opost(c
, tty
) < 0)
1203 if (tty
->driver
->flush_chars
)
1204 tty
->driver
->flush_chars(tty
);
1206 c
= tty
->driver
->write(tty
, 1, b
, nr
);
1216 if (file
->f_flags
& O_NONBLOCK
) {
1223 current
->state
= TASK_RUNNING
;
1224 remove_wait_queue(&tty
->write_wait
, &wait
);
1225 return (b
- buf
) ? b
- buf
: retval
;
1228 /* Called without the kernel lock held - fine */
1229 static unsigned int normal_poll(struct tty_struct
* tty
, struct file
* file
, poll_table
*wait
)
1231 unsigned int mask
= 0;
1233 poll_wait(file
, &tty
->read_wait
, wait
);
1234 poll_wait(file
, &tty
->write_wait
, wait
);
1235 if (input_available_p(tty
, TIME_CHAR(tty
) ? 0 : MIN_CHAR(tty
)))
1236 mask
|= POLLIN
| POLLRDNORM
;
1237 if (tty
->packet
&& tty
->link
->ctrl_status
)
1238 mask
|= POLLPRI
| POLLIN
| POLLRDNORM
;
1239 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
))
1241 if (tty_hung_up_p(file
))
1243 if (!(mask
& (POLLHUP
| POLLIN
| POLLRDNORM
))) {
1244 if (MIN_CHAR(tty
) && !TIME_CHAR(tty
))
1245 tty
->minimum_to_wake
= MIN_CHAR(tty
);
1247 tty
->minimum_to_wake
= 1;
1249 if (tty
->driver
->chars_in_buffer(tty
) < WAKEUP_CHARS
&&
1250 tty
->driver
->write_room(tty
) > 0)
1251 mask
|= POLLOUT
| POLLWRNORM
;
1255 struct tty_ldisc tty_ldisc_N_TTY
= {
1256 TTY_LDISC_MAGIC
, /* magic */
1260 n_tty_open
, /* open */
1261 n_tty_close
, /* close */
1262 n_tty_flush_buffer
, /* flush_buffer */
1263 n_tty_chars_in_buffer
, /* chars_in_buffer */
1264 read_chan
, /* read */
1265 write_chan
, /* write */
1266 n_tty_ioctl
, /* ioctl */
1267 n_tty_set_termios
, /* set_termios */
1268 normal_poll
, /* poll */
1269 n_tty_receive_buf
, /* receive_buf */
1270 n_tty_receive_room
, /* receive_room */
1271 n_tty_write_wakeup
/* write_wakeup */