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 * 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.
28 #include <linux/types.h>
29 #include <linux/major.h>
30 #include <linux/errno.h>
31 #include <linux/signal.h>
32 #include <linux/fcntl.h>
33 #include <linux/sched.h>
34 #include <linux/interrupt.h>
35 #include <linux/tty.h>
36 #include <linux/timer.h>
37 #include <linux/ctype.h>
40 #include <linux/string.h>
41 #include <linux/malloc.h>
42 #include <linux/poll.h>
44 #include <asm/uaccess.h>
45 #include <asm/system.h>
46 #include <asm/bitops.h>
48 #define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
49 #define SYSCONS_DEV MKDEV(TTYAUX_MAJOR,1)
52 #define MIN(a,b) ((a) < (b) ? (a) : (b))
55 /* number of characters left in xmit buffer before select has we have room */
56 #define WAKEUP_CHARS 256
59 * This defines the low- and high-watermarks for throttling and
60 * unthrottling the TTY driver. These watermarks are used for
61 * controlling the space in the read buffer.
63 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
64 #define TTY_THRESHOLD_UNTHROTTLE 128
66 static inline unsigned char *alloc_buf(void)
69 int prio
= in_interrupt() ? GFP_ATOMIC
: GFP_KERNEL
;
71 if (PAGE_SIZE
!= N_TTY_BUF_SIZE
) {
72 p
= kmalloc(N_TTY_BUF_SIZE
, prio
);
74 memset(p
, 0, N_TTY_BUF_SIZE
);
76 p
= (unsigned char *)get_zeroed_page(prio
);
81 static inline void free_buf(unsigned char *buf
)
83 if (PAGE_SIZE
!= N_TTY_BUF_SIZE
)
86 free_page((unsigned long) buf
);
89 static inline void put_tty_queue(unsigned char c
, struct tty_struct
*tty
)
93 * The problem of stomping on the buffers ends here.
94 * Why didn't anyone see this one comming? --AJK
96 spin_lock_irqsave(&tty
->read_lock
, flags
);
97 if (tty
->read_cnt
< N_TTY_BUF_SIZE
) {
98 tty
->read_buf
[tty
->read_head
] = c
;
99 tty
->read_head
= (tty
->read_head
+ 1) & (N_TTY_BUF_SIZE
-1);
102 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
106 * Check whether to call the driver.unthrottle function.
107 * We test the TTY_THROTTLED bit first so that it always
108 * indicates the current state.
110 static void check_unthrottle(struct tty_struct
* tty
)
113 test_and_clear_bit(TTY_THROTTLED
, &tty
->flags
) &&
114 tty
->driver
.unthrottle
)
115 tty
->driver
.unthrottle(tty
);
119 * Reset the read buffer counters, clear the flags,
120 * and make sure the driver is unthrottled. Called
121 * from n_tty_open() and n_tty_flush_buffer().
123 static void reset_buffer_flags(struct tty_struct
*tty
)
127 spin_lock_irqsave(&tty
->read_lock
, flags
);
128 tty
->read_head
= tty
->read_tail
= tty
->read_cnt
= 0;
129 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
130 tty
->canon_head
= tty
->canon_data
= tty
->erasing
= 0;
131 memset(&tty
->read_flags
, 0, sizeof tty
->read_flags
);
132 check_unthrottle(tty
);
136 * Flush the input buffer
138 void n_tty_flush_buffer(struct tty_struct
* tty
)
140 /* clear everything and unthrottle the driver */
141 reset_buffer_flags(tty
);
146 if (tty
->link
->packet
) {
147 tty
->ctrl_status
|= TIOCPKT_FLUSHREAD
;
148 wake_up_interruptible(&tty
->link
->read_wait
);
153 * Return number of characters buffered to be delivered to user
155 ssize_t
n_tty_chars_in_buffer(struct tty_struct
*tty
)
160 spin_lock_irqsave(&tty
->read_lock
, flags
);
163 } else if (tty
->canon_data
) {
164 n
= (tty
->canon_head
> tty
->read_tail
) ?
165 tty
->canon_head
- tty
->read_tail
:
166 tty
->canon_head
+ (N_TTY_BUF_SIZE
- tty
->read_tail
);
168 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
173 * Perform OPOST processing. Returns -1 when the output device is
174 * full and the character must be retried.
176 static int opost(unsigned char c
, struct tty_struct
*tty
)
180 space
= tty
->driver
.write_room(tty
);
192 tty
->driver
.put_char(tty
, '\r');
195 tty
->canon_column
= tty
->column
;
198 if (O_ONOCR(tty
) && tty
->column
== 0)
203 tty
->canon_column
= tty
->column
= 0;
206 tty
->canon_column
= tty
->column
= 0;
209 spaces
= 8 - (tty
->column
& 7);
210 if (O_TABDLY(tty
) == XTABS
) {
213 tty
->column
+= spaces
;
214 tty
->driver
.write(tty
, 0, " ", spaces
);
217 tty
->column
+= spaces
;
231 tty
->driver
.put_char(tty
, c
);
236 * opost_block --- to speed up block console writes, among other
239 static ssize_t
opost_block(struct tty_struct
* tty
,
240 const unsigned char * inbuf
, unsigned int nr
)
247 space
= tty
->driver
.write_room(tty
);
252 if (nr
> sizeof(buf
))
255 if (copy_from_user(buf
, inbuf
, nr
))
258 for (i
= 0, cp
= buf
; i
< nr
; i
++, cp
++) {
265 tty
->canon_column
= tty
->column
;
268 if (O_ONOCR(tty
) && tty
->column
== 0)
273 tty
->canon_column
= tty
->column
= 0;
276 tty
->canon_column
= tty
->column
= 0;
293 if (tty
->driver
.flush_chars
)
294 tty
->driver
.flush_chars(tty
);
295 i
= tty
->driver
.write(tty
, 0, buf
, i
);
301 static inline void put_char(unsigned char c
, struct tty_struct
*tty
)
303 tty
->driver
.put_char(tty
, c
);
306 /* Must be called only when L_ECHO(tty) is true. */
308 static void echo_char(unsigned char c
, struct tty_struct
*tty
)
310 if (L_ECHOCTL(tty
) && iscntrl(c
) && c
!= '\t') {
312 put_char(c
^ 0100, tty
);
318 static inline void finish_erasing(struct tty_struct
*tty
)
327 static void eraser(unsigned char c
, struct tty_struct
*tty
)
329 enum { ERASE
, WERASE
, KILL
} kill_type
;
330 int head
, seen_alnums
;
333 if (tty
->read_head
== tty
->canon_head
) {
334 /* opost('\a', tty); */ /* what do you think? */
337 if (c
== ERASE_CHAR(tty
))
339 else if (c
== WERASE_CHAR(tty
))
343 spin_lock_irqsave(&tty
->read_lock
, flags
);
344 tty
->read_cnt
-= ((tty
->read_head
- tty
->canon_head
) &
345 (N_TTY_BUF_SIZE
- 1));
346 tty
->read_head
= tty
->canon_head
;
347 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
350 if (!L_ECHOK(tty
) || !L_ECHOKE(tty
) || !L_ECHOE(tty
)) {
351 spin_lock_irqsave(&tty
->read_lock
, flags
);
352 tty
->read_cnt
-= ((tty
->read_head
- tty
->canon_head
) &
353 (N_TTY_BUF_SIZE
- 1));
354 tty
->read_head
= tty
->canon_head
;
355 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
357 echo_char(KILL_CHAR(tty
), tty
);
358 /* Add a newline if ECHOK is on and ECHOKE is off. */
367 while (tty
->read_head
!= tty
->canon_head
) {
368 head
= (tty
->read_head
- 1) & (N_TTY_BUF_SIZE
-1);
369 c
= tty
->read_buf
[head
];
370 if (kill_type
== WERASE
) {
371 /* Equivalent to BSD's ALTWERASE. */
372 if (isalnum(c
) || c
== '_')
374 else if (seen_alnums
)
377 spin_lock_irqsave(&tty
->read_lock
, flags
);
378 tty
->read_head
= head
;
380 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
382 if (L_ECHOPRT(tty
)) {
389 } else if (kill_type
== ERASE
&& !L_ECHOE(tty
)) {
390 echo_char(ERASE_CHAR(tty
), tty
);
391 } else if (c
== '\t') {
392 unsigned int col
= tty
->canon_column
;
393 unsigned long tail
= tty
->canon_head
;
395 /* Find the column of the last char. */
396 while (tail
!= tty
->read_head
) {
397 c
= tty
->read_buf
[tail
];
400 else if (iscntrl(c
)) {
405 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
408 /* should never happen */
409 if (tty
->column
> 0x80000000)
412 /* Now backup to that column. */
413 while (tty
->column
> col
) {
414 /* Can't use opost here. */
420 if (iscntrl(c
) && L_ECHOCTL(tty
)) {
427 if (!iscntrl(c
) || L_ECHOCTL(tty
)) {
436 if (kill_type
== ERASE
)
439 if (tty
->read_head
== tty
->canon_head
)
443 static inline void isig(int sig
, struct tty_struct
*tty
, int flush
)
446 kill_pg(tty
->pgrp
, sig
, 1);
447 if (flush
|| !L_NOFLSH(tty
)) {
448 n_tty_flush_buffer(tty
);
449 if (tty
->driver
.flush_buffer
)
450 tty
->driver
.flush_buffer(tty
);
454 static inline void n_tty_receive_break(struct tty_struct
*tty
)
459 isig(SIGINT
, tty
, 1);
463 put_tty_queue('\377', tty
);
464 put_tty_queue('\0', tty
);
466 put_tty_queue('\0', tty
);
467 wake_up_interruptible(&tty
->read_wait
);
470 static inline void n_tty_receive_overrun(struct tty_struct
*tty
)
475 if (time_before(tty
->overrun_time
, jiffies
- HZ
)) {
476 printk("%s: %d input overrun(s)\n", tty_name(tty
, buf
),
478 tty
->overrun_time
= jiffies
;
479 tty
->num_overrun
= 0;
483 static inline void n_tty_receive_parity_error(struct tty_struct
*tty
,
490 put_tty_queue('\377', tty
);
491 put_tty_queue('\0', tty
);
492 put_tty_queue(c
, tty
);
493 } else if (I_INPCK(tty
))
494 put_tty_queue('\0', tty
);
496 put_tty_queue(c
, tty
);
497 wake_up_interruptible(&tty
->read_wait
);
500 static inline void n_tty_receive_char(struct tty_struct
*tty
, unsigned char c
)
503 put_tty_queue(c
, tty
);
507 if (tty
->stopped
&& !tty
->flow_stopped
&&
508 I_IXON(tty
) && I_IXANY(tty
)) {
515 if (I_IUCLC(tty
) && L_IEXTEN(tty
))
520 if (c
== START_CHAR(tty
))
522 else if (c
== STOP_CHAR(tty
))
529 * If the previous character was LNEXT, or we know that this
530 * character is not one of the characters that we'll have to
531 * handle specially, do shortcut processing to speed things
534 if (!test_bit(c
, &tty
->process_char_map
) || tty
->lnext
) {
538 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
539 put_char('\a', tty
); /* beep if no space */
542 /* Record the column of first canon char. */
543 if (tty
->canon_head
== tty
->read_head
)
544 tty
->canon_column
= tty
->column
;
547 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
548 put_tty_queue(c
, tty
);
549 put_tty_queue(c
, tty
);
558 } else if (c
== '\n' && I_INLCR(tty
))
561 if (c
== START_CHAR(tty
)) {
565 if (c
== STOP_CHAR(tty
)) {
573 if (c
== INTR_CHAR(tty
))
576 if (c
== QUIT_CHAR(tty
))
579 if (c
== SUSP_CHAR(tty
)) {
581 isig(signal
, tty
, 0);
586 if (c
== ERASE_CHAR(tty
) || c
== KILL_CHAR(tty
) ||
587 (c
== WERASE_CHAR(tty
) && L_IEXTEN(tty
))) {
591 if (c
== LNEXT_CHAR(tty
) && L_IEXTEN(tty
)) {
595 if (L_ECHOCTL(tty
)) {
602 if (c
== REPRINT_CHAR(tty
) && L_ECHO(tty
) &&
604 unsigned long tail
= tty
->canon_head
;
609 while (tail
!= tty
->read_head
) {
610 echo_char(tty
->read_buf
[tail
], tty
);
611 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
616 if (L_ECHO(tty
) || L_ECHONL(tty
)) {
617 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
625 if (c
== EOF_CHAR(tty
)) {
626 if (tty
->canon_head
!= tty
->read_head
)
627 set_bit(TTY_PUSH
, &tty
->flags
);
631 if ((c
== EOL_CHAR(tty
)) ||
632 (c
== EOL2_CHAR(tty
) && L_IEXTEN(tty
))) {
634 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
637 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
641 /* Record the column of first canon char. */
642 if (tty
->canon_head
== tty
->read_head
)
643 tty
->canon_column
= tty
->column
;
647 * XXX does PARMRK doubling happen for
648 * EOL_CHAR and EOL2_CHAR?
650 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
651 put_tty_queue(c
, tty
);
654 set_bit(tty
->read_head
, &tty
->read_flags
);
655 put_tty_queue(c
, tty
);
656 tty
->canon_head
= tty
->read_head
;
658 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
659 if (waitqueue_active(&tty
->read_wait
))
660 wake_up_interruptible(&tty
->read_wait
);
667 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
668 put_char('\a', tty
); /* beep if no space */
674 /* Record the column of first canon char. */
675 if (tty
->canon_head
== tty
->read_head
)
676 tty
->canon_column
= tty
->column
;
681 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
682 put_tty_queue(c
, tty
);
684 put_tty_queue(c
, tty
);
687 static int n_tty_receive_room(struct tty_struct
*tty
)
689 int left
= N_TTY_BUF_SIZE
- tty
->read_cnt
- 1;
692 * If we are doing input canonicalization, and there are no
693 * pending newlines, let characters through without limit, so
694 * that erase characters will be handled. Other excess
695 * characters will be beeped.
697 if (tty
->icanon
&& !tty
->canon_data
)
698 return N_TTY_BUF_SIZE
;
705 static void n_tty_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
708 const unsigned char *p
;
709 char *f
, flags
= TTY_NORMAL
;
712 unsigned long cpuflags
;
718 spin_lock_irqsave(&tty
->read_lock
, cpuflags
);
719 i
= MIN(count
, MIN(N_TTY_BUF_SIZE
- tty
->read_cnt
,
720 N_TTY_BUF_SIZE
- tty
->read_head
));
721 memcpy(tty
->read_buf
+ tty
->read_head
, cp
, i
);
722 tty
->read_head
= (tty
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
727 i
= MIN(count
, MIN(N_TTY_BUF_SIZE
- tty
->read_cnt
,
728 N_TTY_BUF_SIZE
- tty
->read_head
));
729 memcpy(tty
->read_buf
+ tty
->read_head
, cp
, i
);
730 tty
->read_head
= (tty
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
732 spin_unlock_irqrestore(&tty
->read_lock
, cpuflags
);
734 for (i
=count
, p
= cp
, f
= fp
; i
; i
--, p
++) {
739 n_tty_receive_char(tty
, *p
);
742 n_tty_receive_break(tty
);
746 n_tty_receive_parity_error(tty
, *p
);
749 n_tty_receive_overrun(tty
);
752 printk("%s: unknown flag %d\n",
753 tty_name(tty
, buf
), flags
);
757 if (tty
->driver
.flush_chars
)
758 tty
->driver
.flush_chars(tty
);
761 if (!tty
->icanon
&& (tty
->read_cnt
>= tty
->minimum_to_wake
)) {
762 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
763 if (waitqueue_active(&tty
->read_wait
))
764 wake_up_interruptible(&tty
->read_wait
);
768 * Check the remaining room for the input canonicalization
769 * mode. We don't want to throttle the driver if we're in
770 * canonical mode and don't have a newline yet!
772 if (n_tty_receive_room(tty
) < TTY_THRESHOLD_THROTTLE
) {
773 /* check TTY_THROTTLED first so it indicates our state */
774 if (!test_and_set_bit(TTY_THROTTLED
, &tty
->flags
) &&
775 tty
->driver
.throttle
)
776 tty
->driver
.throttle(tty
);
780 int is_ignored(int sig
)
782 return (sigismember(¤t
->blocked
, sig
) ||
783 current
->sig
->action
[sig
-1].sa
.sa_handler
== SIG_IGN
);
786 static void n_tty_set_termios(struct tty_struct
*tty
, struct termios
* old
)
791 tty
->icanon
= (L_ICANON(tty
) != 0);
792 if (test_bit(TTY_HW_COOK_IN
, &tty
->flags
)) {
797 if (I_ISTRIP(tty
) || I_IUCLC(tty
) || I_IGNCR(tty
) ||
798 I_ICRNL(tty
) || I_INLCR(tty
) || L_ICANON(tty
) ||
799 I_IXON(tty
) || L_ISIG(tty
) || L_ECHO(tty
) ||
802 memset(tty
->process_char_map
, 0, 256/8);
804 if (I_IGNCR(tty
) || I_ICRNL(tty
))
805 set_bit('\r', &tty
->process_char_map
);
807 set_bit('\n', &tty
->process_char_map
);
810 set_bit(ERASE_CHAR(tty
), &tty
->process_char_map
);
811 set_bit(KILL_CHAR(tty
), &tty
->process_char_map
);
812 set_bit(EOF_CHAR(tty
), &tty
->process_char_map
);
813 set_bit('\n', &tty
->process_char_map
);
814 set_bit(EOL_CHAR(tty
), &tty
->process_char_map
);
816 set_bit(WERASE_CHAR(tty
),
817 &tty
->process_char_map
);
818 set_bit(LNEXT_CHAR(tty
),
819 &tty
->process_char_map
);
820 set_bit(EOL2_CHAR(tty
),
821 &tty
->process_char_map
);
823 set_bit(REPRINT_CHAR(tty
),
824 &tty
->process_char_map
);
828 set_bit(START_CHAR(tty
), &tty
->process_char_map
);
829 set_bit(STOP_CHAR(tty
), &tty
->process_char_map
);
832 set_bit(INTR_CHAR(tty
), &tty
->process_char_map
);
833 set_bit(QUIT_CHAR(tty
), &tty
->process_char_map
);
834 set_bit(SUSP_CHAR(tty
), &tty
->process_char_map
);
836 clear_bit(__DISABLED_CHAR
, &tty
->process_char_map
);
842 if ((I_IGNBRK(tty
) || (!I_BRKINT(tty
) && !I_PARMRK(tty
))) &&
843 (I_IGNPAR(tty
) || !I_INPCK(tty
)) &&
844 (tty
->driver
.flags
& TTY_DRIVER_REAL_RAW
))
851 static void n_tty_close(struct tty_struct
*tty
)
853 n_tty_flush_buffer(tty
);
855 free_buf(tty
->read_buf
);
860 static int n_tty_open(struct tty_struct
*tty
)
865 if (!tty
->read_buf
) {
866 tty
->read_buf
= alloc_buf();
870 memset(tty
->read_buf
, 0, N_TTY_BUF_SIZE
);
871 reset_buffer_flags(tty
);
873 n_tty_set_termios(tty
, 0);
874 tty
->minimum_to_wake
= 1;
879 static inline int input_available_p(struct tty_struct
*tty
, int amt
)
884 } else if (tty
->read_cnt
>= (amt
? amt
: 1))
891 * Helper function to speed up read_chan. It is only called when
892 * ICANON is off; it copies characters straight from the tty queue to
893 * user space directly. It can be profitably called twice; once to
894 * drain the space from the tail pointer to the (physical) end of the
895 * buffer, and once to drain the space from the (physical) beginning of
896 * the buffer to head pointer.
898 static inline int copy_from_read_buf(struct tty_struct
*tty
,
908 spin_lock_irqsave(&tty
->read_lock
, flags
);
909 n
= MIN(*nr
, MIN(tty
->read_cnt
, N_TTY_BUF_SIZE
- tty
->read_tail
));
910 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
913 retval
= copy_to_user(*b
, &tty
->read_buf
[tty
->read_tail
], n
);
915 spin_lock_irqsave(&tty
->read_lock
, flags
);
916 tty
->read_tail
= (tty
->read_tail
+ n
) & (N_TTY_BUF_SIZE
-1);
918 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
925 static ssize_t
read_chan(struct tty_struct
*tty
, struct file
*file
,
926 unsigned char *buf
, size_t nr
)
928 unsigned char *b
= buf
;
929 DECLARE_WAITQUEUE(wait
, current
);
939 if (!tty
->read_buf
) {
940 printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
944 /* Job control check -- must be done at start and after
945 every sleep (POSIX.1 7.1.1.4). */
946 /* NOTE: not yet done after every sleep pending a thorough
947 check of the logic of this change. -- jlc */
948 /* don't stop on /dev/console */
949 if (file
->f_dentry
->d_inode
->i_rdev
!= CONSOLE_DEV
&&
950 file
->f_dentry
->d_inode
->i_rdev
!= SYSCONS_DEV
&&
951 current
->tty
== tty
) {
953 printk("read_chan: tty->pgrp <= 0!\n");
954 else if (current
->pgrp
!= tty
->pgrp
) {
955 if (is_ignored(SIGTTIN
) ||
956 is_orphaned_pgrp(current
->pgrp
))
958 kill_pg(current
->pgrp
, SIGTTIN
, 1);
964 timeout
= MAX_SCHEDULE_TIMEOUT
;
966 time
= (HZ
/ 10) * TIME_CHAR(tty
);
967 minimum
= MIN_CHAR(tty
);
970 tty
->minimum_to_wake
= 1;
971 else if (!waitqueue_active(&tty
->read_wait
) ||
972 (tty
->minimum_to_wake
> minimum
))
973 tty
->minimum_to_wake
= minimum
;
980 tty
->minimum_to_wake
= minimum
= 1;
984 if (file
->f_flags
& O_NONBLOCK
) {
985 if (down_trylock(&tty
->atomic_read
))
989 if (down_interruptible(&tty
->atomic_read
))
993 add_wait_queue(&tty
->read_wait
, &wait
);
994 set_bit(TTY_DONT_FLIP
, &tty
->flags
);
996 /* First test for status change. */
997 if (tty
->packet
&& tty
->link
->ctrl_status
) {
1001 cs
= tty
->link
->ctrl_status
;
1002 tty
->link
->ctrl_status
= 0;
1007 /* This statement must be first before checking for input
1008 so that any interrupt will set the state back to
1010 set_current_state(TASK_INTERRUPTIBLE
);
1012 if (((minimum
- (b
- buf
)) < tty
->minimum_to_wake
) &&
1013 ((minimum
- (b
- buf
)) >= 1))
1014 tty
->minimum_to_wake
= (minimum
- (b
- buf
));
1016 if (!input_available_p(tty
, 0)) {
1017 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
)) {
1021 if (tty_hung_up_p(file
))
1025 if (file
->f_flags
& O_NONBLOCK
) {
1029 if (signal_pending(current
)) {
1030 retval
= -ERESTARTSYS
;
1033 clear_bit(TTY_DONT_FLIP
, &tty
->flags
);
1034 timeout
= schedule_timeout(timeout
);
1035 set_bit(TTY_DONT_FLIP
, &tty
->flags
);
1038 current
->state
= TASK_RUNNING
;
1040 /* Deal with packet mode. */
1041 if (tty
->packet
&& b
== buf
) {
1042 put_user(TIOCPKT_DATA
, b
++);
1047 /* N.B. avoid overrun if nr == 0 */
1048 while (nr
&& tty
->read_cnt
) {
1051 eol
= test_and_clear_bit(tty
->read_tail
,
1053 c
= tty
->read_buf
[tty
->read_tail
];
1054 spin_lock_irqsave(&tty
->read_lock
, flags
);
1055 tty
->read_tail
= ((tty
->read_tail
+1) &
1056 (N_TTY_BUF_SIZE
-1));
1058 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
1060 if (!eol
|| (c
!= __DISABLED_CHAR
)) {
1065 /* this test should be redundant:
1066 * we shouldn't be reading data if
1069 if (--tty
->canon_data
< 0)
1070 tty
->canon_data
= 0;
1076 uncopied
= copy_from_read_buf(tty
, &b
, &nr
);
1077 uncopied
+= copy_from_read_buf(tty
, &b
, &nr
);
1084 /* If there is enough space in the read buffer now, let the
1085 * low-level driver know. We use n_tty_chars_in_buffer() to
1086 * check the buffer, as it now knows about canonical mode.
1087 * Otherwise, if the driver is throttled and the line is
1088 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1089 * we won't get any more characters.
1091 if (n_tty_chars_in_buffer(tty
) <= TTY_THRESHOLD_UNTHROTTLE
)
1092 check_unthrottle(tty
);
1094 if (b
- buf
>= minimum
)
1099 clear_bit(TTY_DONT_FLIP
, &tty
->flags
);
1100 up(&tty
->atomic_read
);
1101 remove_wait_queue(&tty
->read_wait
, &wait
);
1103 if (!waitqueue_active(&tty
->read_wait
))
1104 tty
->minimum_to_wake
= minimum
;
1106 current
->state
= TASK_RUNNING
;
1111 clear_bit(TTY_PUSH
, &tty
->flags
);
1112 } else if (test_and_clear_bit(TTY_PUSH
, &tty
->flags
))
1118 static ssize_t
write_chan(struct tty_struct
* tty
, struct file
* file
,
1119 const unsigned char * buf
, size_t nr
)
1121 const unsigned char *b
= buf
;
1122 DECLARE_WAITQUEUE(wait
, current
);
1126 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1127 if (L_TOSTOP(tty
) &&
1128 file
->f_dentry
->d_inode
->i_rdev
!= CONSOLE_DEV
&&
1129 file
->f_dentry
->d_inode
->i_rdev
!= SYSCONS_DEV
) {
1130 retval
= tty_check_change(tty
);
1135 add_wait_queue(&tty
->write_wait
, &wait
);
1137 set_current_state(TASK_INTERRUPTIBLE
);
1138 if (signal_pending(current
)) {
1139 retval
= -ERESTARTSYS
;
1142 if (tty_hung_up_p(file
) || (tty
->link
&& !tty
->link
->count
)) {
1146 if (O_OPOST(tty
) && !(test_bit(TTY_HW_COOK_OUT
, &tty
->flags
))) {
1148 ssize_t num
= opost_block(tty
, b
, nr
);
1158 if (opost(c
, tty
) < 0)
1162 if (tty
->driver
.flush_chars
)
1163 tty
->driver
.flush_chars(tty
);
1165 c
= tty
->driver
.write(tty
, 1, b
, nr
);
1175 if (file
->f_flags
& O_NONBLOCK
) {
1182 current
->state
= TASK_RUNNING
;
1183 remove_wait_queue(&tty
->write_wait
, &wait
);
1184 return (b
- buf
) ? b
- buf
: retval
;
1187 /* Called without the kernel lock held - fine */
1188 static unsigned int normal_poll(struct tty_struct
* tty
, struct file
* file
, poll_table
*wait
)
1190 unsigned int mask
= 0;
1192 poll_wait(file
, &tty
->read_wait
, wait
);
1193 poll_wait(file
, &tty
->write_wait
, wait
);
1194 if (input_available_p(tty
, TIME_CHAR(tty
) ? 0 : MIN_CHAR(tty
)))
1195 mask
|= POLLIN
| POLLRDNORM
;
1196 if (tty
->packet
&& tty
->link
->ctrl_status
)
1197 mask
|= POLLPRI
| POLLIN
| POLLRDNORM
;
1198 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
))
1200 if (tty_hung_up_p(file
))
1202 if (!(mask
& (POLLHUP
| POLLIN
| POLLRDNORM
))) {
1203 if (MIN_CHAR(tty
) && !TIME_CHAR(tty
))
1204 tty
->minimum_to_wake
= MIN_CHAR(tty
);
1206 tty
->minimum_to_wake
= 1;
1208 if (tty
->driver
.chars_in_buffer(tty
) < WAKEUP_CHARS
)
1209 mask
|= POLLOUT
| POLLWRNORM
;
1213 struct tty_ldisc tty_ldisc_N_TTY
= {
1214 TTY_LDISC_MAGIC
, /* magic */
1218 n_tty_open
, /* open */
1219 n_tty_close
, /* close */
1220 n_tty_flush_buffer
, /* flush_buffer */
1221 n_tty_chars_in_buffer
, /* chars_in_buffer */
1222 read_chan
, /* read */
1223 write_chan
, /* write */
1224 n_tty_ioctl
, /* ioctl */
1225 n_tty_set_termios
, /* set_termios */
1226 normal_poll
, /* poll */
1227 n_tty_receive_buf
, /* receive_buf */
1228 n_tty_receive_room
, /* receive_room */
1229 0 /* write_wakeup */