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>
44 #include <linux/string.h>
45 #include <linux/slab.h>
46 #include <linux/poll.h>
47 #include <linux/bitops.h>
49 #include <asm/uaccess.h>
50 #include <asm/system.h>
52 /* number of characters left in xmit buffer before select has we have room */
53 #define WAKEUP_CHARS 256
56 * This defines the low- and high-watermarks for throttling and
57 * unthrottling the TTY driver. These watermarks are used for
58 * controlling the space in the read buffer.
60 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
61 #define TTY_THRESHOLD_UNTHROTTLE 128
63 static inline unsigned char *alloc_buf(void)
65 gfp_t prio
= in_interrupt() ? GFP_ATOMIC
: GFP_KERNEL
;
67 if (PAGE_SIZE
!= N_TTY_BUF_SIZE
)
68 return kmalloc(N_TTY_BUF_SIZE
, prio
);
70 return (unsigned char *)__get_free_page(prio
);
73 static inline void free_buf(unsigned char *buf
)
75 if (PAGE_SIZE
!= N_TTY_BUF_SIZE
)
78 free_page((unsigned long) buf
);
82 * n_tty_set__room - receive space
85 * Called by the driver to find out how much data it is
86 * permitted to feed to the line discipline without any being lost
87 * and thus to manage flow control. Not serialized. Answers for the
91 static void n_tty_set_room(struct tty_struct
*tty
)
93 int left
= N_TTY_BUF_SIZE
- tty
->read_cnt
- 1;
96 * If we are doing input canonicalization, and there are no
97 * pending newlines, let characters through without limit, so
98 * that erase characters will be handled. Other excess
99 * characters will be beeped.
102 left
= tty
->icanon
&& !tty
->canon_data
;
103 tty
->receive_room
= left
;
106 static void put_tty_queue_nolock(unsigned char c
, struct tty_struct
*tty
)
108 if (tty
->read_cnt
< N_TTY_BUF_SIZE
) {
109 tty
->read_buf
[tty
->read_head
] = c
;
110 tty
->read_head
= (tty
->read_head
+ 1) & (N_TTY_BUF_SIZE
-1);
115 static void put_tty_queue(unsigned char c
, struct tty_struct
*tty
)
119 * The problem of stomping on the buffers ends here.
120 * Why didn't anyone see this one coming? --AJK
122 spin_lock_irqsave(&tty
->read_lock
, flags
);
123 put_tty_queue_nolock(c
, tty
);
124 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
128 * check_unthrottle - allow new receive data
131 * Check whether to call the driver.unthrottle function.
132 * We test the TTY_THROTTLED bit first so that it always
133 * indicates the current state. The decision about whether
134 * it is worth allowing more input has been taken by the caller.
135 * Can sleep, may be called under the atomic_read_lock mutex but
136 * this is not guaranteed.
139 static void check_unthrottle(struct tty_struct
* tty
)
142 test_and_clear_bit(TTY_THROTTLED
, &tty
->flags
) &&
143 tty
->driver
->unthrottle
)
144 tty
->driver
->unthrottle(tty
);
148 * reset_buffer_flags - reset buffer state
149 * @tty: terminal to reset
151 * Reset the read buffer counters, clear the flags,
152 * and make sure the driver is unthrottled. Called
153 * from n_tty_open() and n_tty_flush_buffer().
155 static void reset_buffer_flags(struct tty_struct
*tty
)
159 spin_lock_irqsave(&tty
->read_lock
, flags
);
160 tty
->read_head
= tty
->read_tail
= tty
->read_cnt
= 0;
161 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
162 tty
->canon_head
= tty
->canon_data
= tty
->erasing
= 0;
163 memset(&tty
->read_flags
, 0, sizeof tty
->read_flags
);
165 check_unthrottle(tty
);
169 * n_tty_flush_buffer - clean input queue
170 * @tty: terminal device
172 * Flush the input buffer. Called when the line discipline is
173 * being closed, when the tty layer wants the buffer flushed (eg
174 * at hangup) or when the N_TTY line discipline internally has to
175 * clean the pending queue (for example some signals).
177 * FIXME: tty->ctrl_status is not spinlocked and relies on
178 * lock_kernel() still.
181 static void n_tty_flush_buffer(struct tty_struct
* tty
)
183 /* clear everything and unthrottle the driver */
184 reset_buffer_flags(tty
);
189 if (tty
->link
->packet
) {
190 tty
->ctrl_status
|= TIOCPKT_FLUSHREAD
;
191 wake_up_interruptible(&tty
->link
->read_wait
);
196 * n_tty_chars_in_buffer - report available bytes
199 * Report the number of characters buffered to be delivered to user
200 * at this instant in time.
203 static ssize_t
n_tty_chars_in_buffer(struct tty_struct
*tty
)
208 spin_lock_irqsave(&tty
->read_lock
, flags
);
211 } else if (tty
->canon_data
) {
212 n
= (tty
->canon_head
> tty
->read_tail
) ?
213 tty
->canon_head
- tty
->read_tail
:
214 tty
->canon_head
+ (N_TTY_BUF_SIZE
- tty
->read_tail
);
216 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
221 * is_utf8_continuation - utf8 multibyte check
224 * Returns true if the utf8 character 'c' is a multibyte continuation
225 * character. We use this to correctly compute the on screen size
226 * of the character when printing
229 static inline int is_utf8_continuation(unsigned char c
)
231 return (c
& 0xc0) == 0x80;
235 * is_continuation - multibyte check
238 * Returns true if the utf8 character 'c' is a multibyte continuation
239 * character and the terminal is in unicode mode.
242 static inline int is_continuation(unsigned char c
, struct tty_struct
*tty
)
244 return I_IUTF8(tty
) && is_utf8_continuation(c
);
248 * opost - output post processor
249 * @c: character (or partial unicode symbol)
250 * @tty: terminal device
252 * Perform OPOST processing. Returns -1 when the output device is
253 * full and the character must be retried. Note that Linux currently
254 * ignores TABDLY, CRDLY, VTDLY, FFDLY and NLDLY. They simply aren't
255 * relevant in the world today. If you ever need them, add them here.
257 * Called from both the receive and transmit sides and can be called
258 * re-entrantly. Relies on lock_kernel() still.
261 static int opost(unsigned char c
, struct tty_struct
*tty
)
265 space
= tty
->driver
->write_room(tty
);
277 tty
->driver
->put_char(tty
, '\r');
280 tty
->canon_column
= tty
->column
;
283 if (O_ONOCR(tty
) && tty
->column
== 0)
288 tty
->canon_column
= tty
->column
= 0;
291 tty
->canon_column
= tty
->column
= 0;
294 spaces
= 8 - (tty
->column
& 7);
295 if (O_TABDLY(tty
) == XTABS
) {
298 tty
->column
+= spaces
;
299 tty
->driver
->write(tty
, " ", spaces
);
302 tty
->column
+= spaces
;
311 if (!iscntrl(c
) && !is_continuation(c
, tty
))
316 tty
->driver
->put_char(tty
, c
);
321 * opost_block - block postprocess
322 * @tty: terminal device
323 * @inbuf: user buffer
324 * @nr: number of bytes
326 * This path is used to speed up block console writes, among other
327 * things when processing blocks of output data. It handles only
328 * the simple cases normally found and helps to generate blocks of
329 * symbols for the console driver and thus improve performance.
331 * Called from write_chan under the tty layer write lock.
334 static ssize_t
opost_block(struct tty_struct
* tty
,
335 const unsigned char * buf
, unsigned int nr
)
339 const unsigned char *cp
;
341 space
= tty
->driver
->write_room(tty
);
347 for (i
= 0, cp
= buf
; i
< nr
; i
++, cp
++) {
354 tty
->canon_column
= tty
->column
;
357 if (O_ONOCR(tty
) && tty
->column
== 0)
361 tty
->canon_column
= tty
->column
= 0;
378 if (tty
->driver
->flush_chars
)
379 tty
->driver
->flush_chars(tty
);
380 i
= tty
->driver
->write(tty
, buf
, i
);
386 * put_char - write character to driver
387 * @c: character (or part of unicode symbol)
388 * @tty: terminal device
390 * Queue a byte to the driver layer for output
393 static inline void put_char(unsigned char c
, struct tty_struct
*tty
)
395 tty
->driver
->put_char(tty
, c
);
399 * echo_char - echo characters
400 * @c: unicode byte to echo
401 * @tty: terminal device
403 * Echo user input back onto the screen. This must be called only when
404 * L_ECHO(tty) is true. Called from the driver receive_buf path.
407 static void echo_char(unsigned char c
, struct tty_struct
*tty
)
409 if (L_ECHOCTL(tty
) && iscntrl(c
) && c
!= '\t') {
411 put_char(c
^ 0100, tty
);
417 static inline void finish_erasing(struct tty_struct
*tty
)
427 * eraser - handle erase function
428 * @c: character input
429 * @tty: terminal device
431 * Perform erase and neccessary output when an erase character is
432 * present in the stream from the driver layer. Handles the complexities
433 * of UTF-8 multibyte symbols.
436 static void eraser(unsigned char c
, struct tty_struct
*tty
)
438 enum { ERASE
, WERASE
, KILL
} kill_type
;
439 int head
, seen_alnums
, cnt
;
442 if (tty
->read_head
== tty
->canon_head
) {
443 /* opost('\a', tty); */ /* what do you think? */
446 if (c
== ERASE_CHAR(tty
))
448 else if (c
== WERASE_CHAR(tty
))
452 spin_lock_irqsave(&tty
->read_lock
, flags
);
453 tty
->read_cnt
-= ((tty
->read_head
- tty
->canon_head
) &
454 (N_TTY_BUF_SIZE
- 1));
455 tty
->read_head
= tty
->canon_head
;
456 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
459 if (!L_ECHOK(tty
) || !L_ECHOKE(tty
) || !L_ECHOE(tty
)) {
460 spin_lock_irqsave(&tty
->read_lock
, flags
);
461 tty
->read_cnt
-= ((tty
->read_head
- tty
->canon_head
) &
462 (N_TTY_BUF_SIZE
- 1));
463 tty
->read_head
= tty
->canon_head
;
464 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
466 echo_char(KILL_CHAR(tty
), tty
);
467 /* Add a newline if ECHOK is on and ECHOKE is off. */
476 while (tty
->read_head
!= tty
->canon_head
) {
477 head
= tty
->read_head
;
479 /* erase a single possibly multibyte character */
481 head
= (head
- 1) & (N_TTY_BUF_SIZE
-1);
482 c
= tty
->read_buf
[head
];
483 } while (is_continuation(c
, tty
) && head
!= tty
->canon_head
);
485 /* do not partially erase */
486 if (is_continuation(c
, tty
))
489 if (kill_type
== WERASE
) {
490 /* Equivalent to BSD's ALTWERASE. */
491 if (isalnum(c
) || c
== '_')
493 else if (seen_alnums
)
496 cnt
= (tty
->read_head
- head
) & (N_TTY_BUF_SIZE
-1);
497 spin_lock_irqsave(&tty
->read_lock
, flags
);
498 tty
->read_head
= head
;
499 tty
->read_cnt
-= cnt
;
500 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
502 if (L_ECHOPRT(tty
)) {
508 /* if cnt > 1, output a multi-byte character */
511 head
= (head
+1) & (N_TTY_BUF_SIZE
-1);
512 put_char(tty
->read_buf
[head
], tty
);
514 } else if (kill_type
== ERASE
&& !L_ECHOE(tty
)) {
515 echo_char(ERASE_CHAR(tty
), tty
);
516 } else if (c
== '\t') {
517 unsigned int col
= tty
->canon_column
;
518 unsigned long tail
= tty
->canon_head
;
520 /* Find the column of the last char. */
521 while (tail
!= tty
->read_head
) {
522 c
= tty
->read_buf
[tail
];
525 else if (iscntrl(c
)) {
528 } else if (!is_continuation(c
, tty
))
530 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
533 /* should never happen */
534 if (tty
->column
> 0x80000000)
537 /* Now backup to that column. */
538 while (tty
->column
> col
) {
539 /* Can't use opost here. */
545 if (iscntrl(c
) && L_ECHOCTL(tty
)) {
552 if (!iscntrl(c
) || L_ECHOCTL(tty
)) {
561 if (kill_type
== ERASE
)
564 if (tty
->read_head
== tty
->canon_head
)
569 * isig - handle the ISIG optio
572 * @flush: force flush
574 * Called when a signal is being sent due to terminal input. This
575 * may caus terminal flushing to take place according to the termios
576 * settings and character used. Called from the driver receive_buf
577 * path so serialized.
580 static inline void isig(int sig
, struct tty_struct
*tty
, int flush
)
583 kill_pg(tty
->pgrp
, sig
, 1);
584 if (flush
|| !L_NOFLSH(tty
)) {
585 n_tty_flush_buffer(tty
);
586 if (tty
->driver
->flush_buffer
)
587 tty
->driver
->flush_buffer(tty
);
592 * n_tty_receive_break - handle break
595 * An RS232 break event has been hit in the incoming bitstream. This
596 * can cause a variety of events depending upon the termios settings.
598 * Called from the receive_buf path so single threaded.
601 static inline void n_tty_receive_break(struct tty_struct
*tty
)
606 isig(SIGINT
, tty
, 1);
610 put_tty_queue('\377', tty
);
611 put_tty_queue('\0', tty
);
613 put_tty_queue('\0', tty
);
614 wake_up_interruptible(&tty
->read_wait
);
618 * n_tty_receive_overrun - handle overrun reporting
621 * Data arrived faster than we could process it. While the tty
622 * driver has flagged this the bits that were missed are gone
625 * Called from the receive_buf path so single threaded. Does not
626 * need locking as num_overrun and overrun_time are function
630 static inline void n_tty_receive_overrun(struct tty_struct
*tty
)
635 if (time_before(tty
->overrun_time
, jiffies
- HZ
) ||
636 time_after(tty
->overrun_time
, jiffies
)) {
637 printk(KERN_WARNING
"%s: %d input overrun(s)\n",
640 tty
->overrun_time
= jiffies
;
641 tty
->num_overrun
= 0;
646 * n_tty_receive_parity_error - error notifier
647 * @tty: terminal device
650 * Process a parity error and queue the right data to indicate
651 * the error case if neccessary. Locking as per n_tty_receive_buf.
653 static inline void n_tty_receive_parity_error(struct tty_struct
*tty
,
660 put_tty_queue('\377', tty
);
661 put_tty_queue('\0', tty
);
662 put_tty_queue(c
, tty
);
663 } else if (I_INPCK(tty
))
664 put_tty_queue('\0', tty
);
666 put_tty_queue(c
, tty
);
667 wake_up_interruptible(&tty
->read_wait
);
671 * n_tty_receive_char - perform processing
672 * @tty: terminal device
675 * Process an individual character of input received from the driver.
676 * This is serialized with respect to itself by the rules for the
680 static inline void n_tty_receive_char(struct tty_struct
*tty
, unsigned char c
)
685 put_tty_queue(c
, tty
);
689 if (tty
->stopped
&& !tty
->flow_stopped
&&
690 I_IXON(tty
) && I_IXANY(tty
)) {
697 if (I_IUCLC(tty
) && L_IEXTEN(tty
))
702 if (c
== START_CHAR(tty
))
704 else if (c
== STOP_CHAR(tty
))
711 * If the previous character was LNEXT, or we know that this
712 * character is not one of the characters that we'll have to
713 * handle specially, do shortcut processing to speed things
716 if (!test_bit(c
, tty
->process_char_map
) || tty
->lnext
) {
720 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
721 put_char('\a', tty
); /* beep if no space */
724 /* Record the column of first canon char. */
725 if (tty
->canon_head
== tty
->read_head
)
726 tty
->canon_column
= tty
->column
;
729 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
730 put_tty_queue(c
, tty
);
731 put_tty_queue(c
, tty
);
740 } else if (c
== '\n' && I_INLCR(tty
))
743 if (c
== START_CHAR(tty
)) {
747 if (c
== STOP_CHAR(tty
)) {
755 if (c
== INTR_CHAR(tty
))
758 if (c
== QUIT_CHAR(tty
))
761 if (c
== SUSP_CHAR(tty
)) {
763 isig(signal
, tty
, 0);
768 if (c
== ERASE_CHAR(tty
) || c
== KILL_CHAR(tty
) ||
769 (c
== WERASE_CHAR(tty
) && L_IEXTEN(tty
))) {
773 if (c
== LNEXT_CHAR(tty
) && L_IEXTEN(tty
)) {
777 if (L_ECHOCTL(tty
)) {
784 if (c
== REPRINT_CHAR(tty
) && L_ECHO(tty
) &&
786 unsigned long tail
= tty
->canon_head
;
791 while (tail
!= tty
->read_head
) {
792 echo_char(tty
->read_buf
[tail
], tty
);
793 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
798 if (L_ECHO(tty
) || L_ECHONL(tty
)) {
799 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1)
805 if (c
== EOF_CHAR(tty
)) {
806 if (tty
->canon_head
!= tty
->read_head
)
807 set_bit(TTY_PUSH
, &tty
->flags
);
811 if ((c
== EOL_CHAR(tty
)) ||
812 (c
== EOL2_CHAR(tty
) && L_IEXTEN(tty
))) {
814 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
817 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1)
819 /* Record the column of first canon char. */
820 if (tty
->canon_head
== tty
->read_head
)
821 tty
->canon_column
= tty
->column
;
825 * XXX does PARMRK doubling happen for
826 * EOL_CHAR and EOL2_CHAR?
828 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
829 put_tty_queue(c
, tty
);
832 spin_lock_irqsave(&tty
->read_lock
, flags
);
833 set_bit(tty
->read_head
, tty
->read_flags
);
834 put_tty_queue_nolock(c
, tty
);
835 tty
->canon_head
= tty
->read_head
;
837 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
838 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
839 if (waitqueue_active(&tty
->read_wait
))
840 wake_up_interruptible(&tty
->read_wait
);
847 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
848 put_char('\a', tty
); /* beep if no space */
854 /* Record the column of first canon char. */
855 if (tty
->canon_head
== tty
->read_head
)
856 tty
->canon_column
= tty
->column
;
861 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
862 put_tty_queue(c
, tty
);
864 put_tty_queue(c
, tty
);
869 * n_tty_write_wakeup - asynchronous I/O notifier
872 * Required for the ptys, serial driver etc. since processes
873 * that attach themselves to the master and rely on ASYNC
874 * IO must be woken up
877 static void n_tty_write_wakeup(struct tty_struct
*tty
)
881 set_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
882 kill_fasync(&tty
->fasync
, SIGIO
, POLL_OUT
);
888 * n_tty_receive_buf - data receive
889 * @tty: terminal device
894 * Called by the terminal driver when a block of characters has
895 * been received. This function must be called from soft contexts
896 * not from interrupt context. The driver is responsible for making
897 * calls one at a time and in order (or using flush_to_ldisc)
900 static void n_tty_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
903 const unsigned char *p
;
904 char *f
, flags
= TTY_NORMAL
;
907 unsigned long cpuflags
;
913 spin_lock_irqsave(&tty
->read_lock
, cpuflags
);
914 i
= min(N_TTY_BUF_SIZE
- tty
->read_cnt
,
915 N_TTY_BUF_SIZE
- tty
->read_head
);
917 memcpy(tty
->read_buf
+ tty
->read_head
, cp
, i
);
918 tty
->read_head
= (tty
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
923 i
= min(N_TTY_BUF_SIZE
- tty
->read_cnt
,
924 N_TTY_BUF_SIZE
- tty
->read_head
);
926 memcpy(tty
->read_buf
+ tty
->read_head
, cp
, i
);
927 tty
->read_head
= (tty
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
929 spin_unlock_irqrestore(&tty
->read_lock
, cpuflags
);
931 for (i
=count
, p
= cp
, f
= fp
; i
; i
--, p
++) {
936 n_tty_receive_char(tty
, *p
);
939 n_tty_receive_break(tty
);
943 n_tty_receive_parity_error(tty
, *p
);
946 n_tty_receive_overrun(tty
);
949 printk("%s: unknown flag %d\n",
950 tty_name(tty
, buf
), flags
);
954 if (tty
->driver
->flush_chars
)
955 tty
->driver
->flush_chars(tty
);
960 if (!tty
->icanon
&& (tty
->read_cnt
>= tty
->minimum_to_wake
)) {
961 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
962 if (waitqueue_active(&tty
->read_wait
))
963 wake_up_interruptible(&tty
->read_wait
);
967 * Check the remaining room for the input canonicalization
968 * mode. We don't want to throttle the driver if we're in
969 * canonical mode and don't have a newline yet!
971 if (tty
->receive_room
< TTY_THRESHOLD_THROTTLE
) {
972 /* check TTY_THROTTLED first so it indicates our state */
973 if (!test_and_set_bit(TTY_THROTTLED
, &tty
->flags
) &&
974 tty
->driver
->throttle
)
975 tty
->driver
->throttle(tty
);
979 int is_ignored(int sig
)
981 return (sigismember(¤t
->blocked
, sig
) ||
982 current
->sighand
->action
[sig
-1].sa
.sa_handler
== SIG_IGN
);
986 * n_tty_set_termios - termios data changed
988 * @old: previous data
990 * Called by the tty layer when the user changes termios flags so
991 * that the line discipline can plan ahead. This function cannot sleep
992 * and is protected from re-entry by the tty layer. The user is
993 * guaranteed that this function will not be re-entered or in progress
994 * when the ldisc is closed.
997 static void n_tty_set_termios(struct tty_struct
*tty
, struct termios
* old
)
1002 tty
->icanon
= (L_ICANON(tty
) != 0);
1003 if (test_bit(TTY_HW_COOK_IN
, &tty
->flags
)) {
1006 n_tty_set_room(tty
);
1009 if (I_ISTRIP(tty
) || I_IUCLC(tty
) || I_IGNCR(tty
) ||
1010 I_ICRNL(tty
) || I_INLCR(tty
) || L_ICANON(tty
) ||
1011 I_IXON(tty
) || L_ISIG(tty
) || L_ECHO(tty
) ||
1013 memset(tty
->process_char_map
, 0, 256/8);
1015 if (I_IGNCR(tty
) || I_ICRNL(tty
))
1016 set_bit('\r', tty
->process_char_map
);
1018 set_bit('\n', tty
->process_char_map
);
1020 if (L_ICANON(tty
)) {
1021 set_bit(ERASE_CHAR(tty
), tty
->process_char_map
);
1022 set_bit(KILL_CHAR(tty
), tty
->process_char_map
);
1023 set_bit(EOF_CHAR(tty
), tty
->process_char_map
);
1024 set_bit('\n', tty
->process_char_map
);
1025 set_bit(EOL_CHAR(tty
), tty
->process_char_map
);
1026 if (L_IEXTEN(tty
)) {
1027 set_bit(WERASE_CHAR(tty
),
1028 tty
->process_char_map
);
1029 set_bit(LNEXT_CHAR(tty
),
1030 tty
->process_char_map
);
1031 set_bit(EOL2_CHAR(tty
),
1032 tty
->process_char_map
);
1034 set_bit(REPRINT_CHAR(tty
),
1035 tty
->process_char_map
);
1039 set_bit(START_CHAR(tty
), tty
->process_char_map
);
1040 set_bit(STOP_CHAR(tty
), tty
->process_char_map
);
1043 set_bit(INTR_CHAR(tty
), tty
->process_char_map
);
1044 set_bit(QUIT_CHAR(tty
), tty
->process_char_map
);
1045 set_bit(SUSP_CHAR(tty
), tty
->process_char_map
);
1047 clear_bit(__DISABLED_CHAR
, tty
->process_char_map
);
1052 if ((I_IGNBRK(tty
) || (!I_BRKINT(tty
) && !I_PARMRK(tty
))) &&
1053 (I_IGNPAR(tty
) || !I_INPCK(tty
)) &&
1054 (tty
->driver
->flags
& TTY_DRIVER_REAL_RAW
))
1059 n_tty_set_room(tty
);
1063 * n_tty_close - close the ldisc for this tty
1066 * Called from the terminal layer when this line discipline is
1067 * being shut down, either because of a close or becsuse of a
1068 * discipline change. The function will not be called while other
1069 * ldisc methods are in progress.
1072 static void n_tty_close(struct tty_struct
*tty
)
1074 n_tty_flush_buffer(tty
);
1075 if (tty
->read_buf
) {
1076 free_buf(tty
->read_buf
);
1077 tty
->read_buf
= NULL
;
1082 * n_tty_open - open an ldisc
1083 * @tty: terminal to open
1085 * Called when this line discipline is being attached to the
1086 * terminal device. Can sleep. Called serialized so that no
1087 * other events will occur in parallel. No further open will occur
1091 static int n_tty_open(struct tty_struct
*tty
)
1096 /* This one is ugly. Currently a malloc failure here can panic */
1097 if (!tty
->read_buf
) {
1098 tty
->read_buf
= alloc_buf();
1102 memset(tty
->read_buf
, 0, N_TTY_BUF_SIZE
);
1103 reset_buffer_flags(tty
);
1105 n_tty_set_termios(tty
, NULL
);
1106 tty
->minimum_to_wake
= 1;
1111 static inline int input_available_p(struct tty_struct
*tty
, int amt
)
1114 if (tty
->canon_data
)
1116 } else if (tty
->read_cnt
>= (amt
? amt
: 1))
1123 * copy_from_read_buf - copy read data directly
1124 * @tty: terminal device
1128 * Helper function to speed up read_chan. It is only called when
1129 * ICANON is off; it copies characters straight from the tty queue to
1130 * user space directly. It can be profitably called twice; once to
1131 * drain the space from the tail pointer to the (physical) end of the
1132 * buffer, and once to drain the space from the (physical) beginning of
1133 * the buffer to head pointer.
1135 * Called under the tty->atomic_read_lock sem
1139 static int copy_from_read_buf(struct tty_struct
*tty
,
1140 unsigned char __user
**b
,
1146 unsigned long flags
;
1149 spin_lock_irqsave(&tty
->read_lock
, flags
);
1150 n
= min(tty
->read_cnt
, N_TTY_BUF_SIZE
- tty
->read_tail
);
1152 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
1155 retval
= copy_to_user(*b
, &tty
->read_buf
[tty
->read_tail
], n
);
1157 spin_lock_irqsave(&tty
->read_lock
, flags
);
1158 tty
->read_tail
= (tty
->read_tail
+ n
) & (N_TTY_BUF_SIZE
-1);
1160 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
1167 extern ssize_t
redirected_tty_write(struct file
*,const char *,size_t,loff_t
*);
1170 * job_control - check job control
1172 * @file: file handle
1174 * Perform job control management checks on this file/tty descriptor
1175 * and if appropriate send any needed signals and return a negative
1176 * error code if action should be taken.
1179 static int job_control(struct tty_struct
*tty
, struct file
*file
)
1181 /* Job control check -- must be done at start and after
1182 every sleep (POSIX.1 7.1.1.4). */
1183 /* NOTE: not yet done after every sleep pending a thorough
1184 check of the logic of this change. -- jlc */
1185 /* don't stop on /dev/console */
1186 if (file
->f_op
->write
!= redirected_tty_write
&&
1187 current
->signal
->tty
== tty
) {
1189 printk("read_chan: tty->pgrp <= 0!\n");
1190 else if (process_group(current
) != tty
->pgrp
) {
1191 if (is_ignored(SIGTTIN
) ||
1192 is_orphaned_pgrp(process_group(current
)))
1194 kill_pg(process_group(current
), SIGTTIN
, 1);
1195 return -ERESTARTSYS
;
1203 * read_chan - read function for tty
1205 * @file: file object
1206 * @buf: userspace buffer pointer
1209 * Perform reads for the line discipline. We are guaranteed that the
1210 * line discipline will not be closed under us but we may get multiple
1211 * parallel readers and must handle this ourselves. We may also get
1212 * a hangup. Always called in user context, may sleep.
1214 * This code must be sure never to sleep through a hangup.
1217 static ssize_t
read_chan(struct tty_struct
*tty
, struct file
*file
,
1218 unsigned char __user
*buf
, size_t nr
)
1220 unsigned char __user
*b
= buf
;
1221 DECLARE_WAITQUEUE(wait
, current
);
1227 unsigned long flags
;
1231 if (!tty
->read_buf
) {
1232 printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
1236 c
= job_control(tty
, file
);
1241 timeout
= MAX_SCHEDULE_TIMEOUT
;
1243 time
= (HZ
/ 10) * TIME_CHAR(tty
);
1244 minimum
= MIN_CHAR(tty
);
1247 tty
->minimum_to_wake
= 1;
1248 else if (!waitqueue_active(&tty
->read_wait
) ||
1249 (tty
->minimum_to_wake
> minimum
))
1250 tty
->minimum_to_wake
= minimum
;
1257 tty
->minimum_to_wake
= minimum
= 1;
1262 * Internal serialization of reads.
1264 if (file
->f_flags
& O_NONBLOCK
) {
1265 if (!mutex_trylock(&tty
->atomic_read_lock
))
1269 if (mutex_lock_interruptible(&tty
->atomic_read_lock
))
1270 return -ERESTARTSYS
;
1273 add_wait_queue(&tty
->read_wait
, &wait
);
1275 /* First test for status change. */
1276 if (tty
->packet
&& tty
->link
->ctrl_status
) {
1280 cs
= tty
->link
->ctrl_status
;
1281 tty
->link
->ctrl_status
= 0;
1282 if (put_user(cs
, b
++)) {
1290 /* This statement must be first before checking for input
1291 so that any interrupt will set the state back to
1293 set_current_state(TASK_INTERRUPTIBLE
);
1295 if (((minimum
- (b
- buf
)) < tty
->minimum_to_wake
) &&
1296 ((minimum
- (b
- buf
)) >= 1))
1297 tty
->minimum_to_wake
= (minimum
- (b
- buf
));
1299 if (!input_available_p(tty
, 0)) {
1300 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
)) {
1304 if (tty_hung_up_p(file
))
1308 if (file
->f_flags
& O_NONBLOCK
) {
1312 if (signal_pending(current
)) {
1313 retval
= -ERESTARTSYS
;
1316 n_tty_set_room(tty
);
1317 timeout
= schedule_timeout(timeout
);
1320 __set_current_state(TASK_RUNNING
);
1322 /* Deal with packet mode. */
1323 if (tty
->packet
&& b
== buf
) {
1324 if (put_user(TIOCPKT_DATA
, b
++)) {
1333 /* N.B. avoid overrun if nr == 0 */
1334 while (nr
&& tty
->read_cnt
) {
1337 eol
= test_and_clear_bit(tty
->read_tail
,
1339 c
= tty
->read_buf
[tty
->read_tail
];
1340 spin_lock_irqsave(&tty
->read_lock
, flags
);
1341 tty
->read_tail
= ((tty
->read_tail
+1) &
1342 (N_TTY_BUF_SIZE
-1));
1345 /* this test should be redundant:
1346 * we shouldn't be reading data if
1349 if (--tty
->canon_data
< 0)
1350 tty
->canon_data
= 0;
1352 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
1354 if (!eol
|| (c
!= __DISABLED_CHAR
)) {
1355 if (put_user(c
, b
++)) {
1369 uncopied
= copy_from_read_buf(tty
, &b
, &nr
);
1370 uncopied
+= copy_from_read_buf(tty
, &b
, &nr
);
1377 /* If there is enough space in the read buffer now, let the
1378 * low-level driver know. We use n_tty_chars_in_buffer() to
1379 * check the buffer, as it now knows about canonical mode.
1380 * Otherwise, if the driver is throttled and the line is
1381 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1382 * we won't get any more characters.
1384 if (n_tty_chars_in_buffer(tty
) <= TTY_THRESHOLD_UNTHROTTLE
) {
1385 n_tty_set_room(tty
);
1386 check_unthrottle(tty
);
1389 if (b
- buf
>= minimum
)
1394 mutex_unlock(&tty
->atomic_read_lock
);
1395 remove_wait_queue(&tty
->read_wait
, &wait
);
1397 if (!waitqueue_active(&tty
->read_wait
))
1398 tty
->minimum_to_wake
= minimum
;
1400 __set_current_state(TASK_RUNNING
);
1405 clear_bit(TTY_PUSH
, &tty
->flags
);
1406 } else if (test_and_clear_bit(TTY_PUSH
, &tty
->flags
))
1409 n_tty_set_room(tty
);
1415 * write_chan - write function for tty
1417 * @file: file object
1418 * @buf: userspace buffer pointer
1421 * Write function of the terminal device. This is serialized with
1422 * respect to other write callers but not to termios changes, reads
1423 * and other such events. We must be careful with N_TTY as the receive
1424 * code will echo characters, thus calling driver write methods.
1426 * This code must be sure never to sleep through a hangup.
1429 static ssize_t
write_chan(struct tty_struct
* tty
, struct file
* file
,
1430 const unsigned char * buf
, size_t nr
)
1432 const unsigned char *b
= buf
;
1433 DECLARE_WAITQUEUE(wait
, current
);
1437 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1438 if (L_TOSTOP(tty
) && file
->f_op
->write
!= redirected_tty_write
) {
1439 retval
= tty_check_change(tty
);
1444 add_wait_queue(&tty
->write_wait
, &wait
);
1446 set_current_state(TASK_INTERRUPTIBLE
);
1447 if (signal_pending(current
)) {
1448 retval
= -ERESTARTSYS
;
1451 if (tty_hung_up_p(file
) || (tty
->link
&& !tty
->link
->count
)) {
1455 if (O_OPOST(tty
) && !(test_bit(TTY_HW_COOK_OUT
, &tty
->flags
))) {
1457 ssize_t num
= opost_block(tty
, b
, nr
);
1469 if (opost(c
, tty
) < 0)
1473 if (tty
->driver
->flush_chars
)
1474 tty
->driver
->flush_chars(tty
);
1477 c
= tty
->driver
->write(tty
, b
, nr
);
1490 if (file
->f_flags
& O_NONBLOCK
) {
1497 __set_current_state(TASK_RUNNING
);
1498 remove_wait_queue(&tty
->write_wait
, &wait
);
1499 return (b
- buf
) ? b
- buf
: retval
;
1503 * normal_poll - poll method for N_TTY
1504 * @tty: terminal device
1505 * @file: file accessing it
1508 * Called when the line discipline is asked to poll() for data or
1509 * for special events. This code is not serialized with respect to
1510 * other events save open/close.
1512 * This code must be sure never to sleep through a hangup.
1513 * Called without the kernel lock held - fine
1515 * FIXME: if someone changes the VMIN or discipline settings for the
1516 * terminal while another process is in poll() the poll does not
1517 * recompute the new limits. Possibly set_termios should issue
1518 * a read wakeup to fix this bug.
1521 static unsigned int normal_poll(struct tty_struct
* tty
, struct file
* file
, poll_table
*wait
)
1523 unsigned int mask
= 0;
1525 poll_wait(file
, &tty
->read_wait
, wait
);
1526 poll_wait(file
, &tty
->write_wait
, wait
);
1527 if (input_available_p(tty
, TIME_CHAR(tty
) ? 0 : MIN_CHAR(tty
)))
1528 mask
|= POLLIN
| POLLRDNORM
;
1529 if (tty
->packet
&& tty
->link
->ctrl_status
)
1530 mask
|= POLLPRI
| POLLIN
| POLLRDNORM
;
1531 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
))
1533 if (tty_hung_up_p(file
))
1535 if (!(mask
& (POLLHUP
| POLLIN
| POLLRDNORM
))) {
1536 if (MIN_CHAR(tty
) && !TIME_CHAR(tty
))
1537 tty
->minimum_to_wake
= MIN_CHAR(tty
);
1539 tty
->minimum_to_wake
= 1;
1541 if (tty
->driver
->chars_in_buffer(tty
) < WAKEUP_CHARS
&&
1542 tty
->driver
->write_room(tty
) > 0)
1543 mask
|= POLLOUT
| POLLWRNORM
;
1547 struct tty_ldisc tty_ldisc_N_TTY
= {
1548 TTY_LDISC_MAGIC
, /* magic */
1552 n_tty_open
, /* open */
1553 n_tty_close
, /* close */
1554 n_tty_flush_buffer
, /* flush_buffer */
1555 n_tty_chars_in_buffer
, /* chars_in_buffer */
1556 read_chan
, /* read */
1557 write_chan
, /* write */
1558 n_tty_ioctl
, /* ioctl */
1559 n_tty_set_termios
, /* set_termios */
1560 normal_poll
, /* poll */
1562 n_tty_receive_buf
, /* receive_buf */
1563 n_tty_write_wakeup
/* write_wakeup */