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 int 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
);
81 static inline void put_tty_queue_nolock(unsigned char c
, struct tty_struct
*tty
)
83 if (tty
->read_cnt
< N_TTY_BUF_SIZE
) {
84 tty
->read_buf
[tty
->read_head
] = c
;
85 tty
->read_head
= (tty
->read_head
+ 1) & (N_TTY_BUF_SIZE
-1);
90 static inline void put_tty_queue(unsigned char c
, struct tty_struct
*tty
)
94 * The problem of stomping on the buffers ends here.
95 * Why didn't anyone see this one coming? --AJK
97 spin_lock_irqsave(&tty
->read_lock
, flags
);
98 put_tty_queue_nolock(c
, tty
);
99 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
103 * check_unthrottle - allow new receive data
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. The decision about whether
109 * it is worth allowing more input has been taken by the caller.
110 * Can sleep, may be called under the atomic_read semaphore but
111 * this is not guaranteed.
114 static void check_unthrottle(struct tty_struct
* tty
)
117 test_and_clear_bit(TTY_THROTTLED
, &tty
->flags
) &&
118 tty
->driver
->unthrottle
)
119 tty
->driver
->unthrottle(tty
);
123 * reset_buffer_flags - reset buffer state
124 * @tty: terminal to reset
126 * Reset the read buffer counters, clear the flags,
127 * and make sure the driver is unthrottled. Called
128 * from n_tty_open() and n_tty_flush_buffer().
130 static void reset_buffer_flags(struct tty_struct
*tty
)
134 spin_lock_irqsave(&tty
->read_lock
, flags
);
135 tty
->read_head
= tty
->read_tail
= tty
->read_cnt
= 0;
136 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
137 tty
->canon_head
= tty
->canon_data
= tty
->erasing
= 0;
138 memset(&tty
->read_flags
, 0, sizeof tty
->read_flags
);
139 check_unthrottle(tty
);
143 * n_tty_flush_buffer - clean input queue
144 * @tty: terminal device
146 * Flush the input buffer. Called when the line discipline is
147 * being closed, when the tty layer wants the buffer flushed (eg
148 * at hangup) or when the N_TTY line discipline internally has to
149 * clean the pending queue (for example some signals).
151 * FIXME: tty->ctrl_status is not spinlocked and relies on
152 * lock_kernel() still.
155 static void n_tty_flush_buffer(struct tty_struct
* tty
)
157 /* clear everything and unthrottle the driver */
158 reset_buffer_flags(tty
);
163 if (tty
->link
->packet
) {
164 tty
->ctrl_status
|= TIOCPKT_FLUSHREAD
;
165 wake_up_interruptible(&tty
->link
->read_wait
);
170 * n_tty_chars_in_buffer - report available bytes
173 * Report the number of characters buffered to be delivered to user
174 * at this instant in time.
177 static ssize_t
n_tty_chars_in_buffer(struct tty_struct
*tty
)
182 spin_lock_irqsave(&tty
->read_lock
, flags
);
185 } else if (tty
->canon_data
) {
186 n
= (tty
->canon_head
> tty
->read_tail
) ?
187 tty
->canon_head
- tty
->read_tail
:
188 tty
->canon_head
+ (N_TTY_BUF_SIZE
- tty
->read_tail
);
190 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
195 * is_utf8_continuation - utf8 multibyte check
198 * Returns true if the utf8 character 'c' is a multibyte continuation
199 * character. We use this to correctly compute the on screen size
200 * of the character when printing
203 static inline int is_utf8_continuation(unsigned char c
)
205 return (c
& 0xc0) == 0x80;
209 * is_continuation - multibyte check
212 * Returns true if the utf8 character 'c' is a multibyte continuation
213 * character and the terminal is in unicode mode.
216 static inline int is_continuation(unsigned char c
, struct tty_struct
*tty
)
218 return I_IUTF8(tty
) && is_utf8_continuation(c
);
222 * opost - output post processor
223 * @c: character (or partial unicode symbol)
224 * @tty: terminal device
226 * Perform OPOST processing. Returns -1 when the output device is
227 * full and the character must be retried. Note that Linux currently
228 * ignores TABDLY, CRDLY, VTDLY, FFDLY and NLDLY. They simply aren't
229 * relevant in the world today. If you ever need them, add them here.
231 * Called from both the receive and transmit sides and can be called
232 * re-entrantly. Relies on lock_kernel() still.
235 static int opost(unsigned char c
, struct tty_struct
*tty
)
239 space
= tty
->driver
->write_room(tty
);
251 tty
->driver
->put_char(tty
, '\r');
254 tty
->canon_column
= tty
->column
;
257 if (O_ONOCR(tty
) && tty
->column
== 0)
262 tty
->canon_column
= tty
->column
= 0;
265 tty
->canon_column
= tty
->column
= 0;
268 spaces
= 8 - (tty
->column
& 7);
269 if (O_TABDLY(tty
) == XTABS
) {
272 tty
->column
+= spaces
;
273 tty
->driver
->write(tty
, " ", spaces
);
276 tty
->column
+= spaces
;
285 if (!iscntrl(c
) && !is_continuation(c
, tty
))
290 tty
->driver
->put_char(tty
, c
);
295 * opost_block - block postprocess
296 * @tty: terminal device
297 * @inbuf: user buffer
298 * @nr: number of bytes
300 * This path is used to speed up block console writes, among other
301 * things when processing blocks of output data. It handles only
302 * the simple cases normally found and helps to generate blocks of
303 * symbols for the console driver and thus improve performance.
305 * Called from write_chan under the tty layer write lock.
308 static ssize_t
opost_block(struct tty_struct
* tty
,
309 const unsigned char * buf
, unsigned int nr
)
313 const unsigned char *cp
;
315 space
= tty
->driver
->write_room(tty
);
321 for (i
= 0, cp
= buf
; i
< nr
; i
++, cp
++) {
328 tty
->canon_column
= tty
->column
;
331 if (O_ONOCR(tty
) && tty
->column
== 0)
335 tty
->canon_column
= tty
->column
= 0;
352 if (tty
->driver
->flush_chars
)
353 tty
->driver
->flush_chars(tty
);
354 i
= tty
->driver
->write(tty
, buf
, i
);
360 * put_char - write character to driver
361 * @c: character (or part of unicode symbol)
362 * @tty: terminal device
364 * Queue a byte to the driver layer for output
367 static inline void put_char(unsigned char c
, struct tty_struct
*tty
)
369 tty
->driver
->put_char(tty
, c
);
373 * echo_char - echo characters
374 * @c: unicode byte to echo
375 * @tty: terminal device
377 * Echo user input back onto the screen. This must be called only when
378 * L_ECHO(tty) is true. Called from the driver receive_buf path.
381 static void echo_char(unsigned char c
, struct tty_struct
*tty
)
383 if (L_ECHOCTL(tty
) && iscntrl(c
) && c
!= '\t') {
385 put_char(c
^ 0100, tty
);
391 static inline void finish_erasing(struct tty_struct
*tty
)
401 * eraser - handle erase function
402 * @c: character input
403 * @tty: terminal device
405 * Perform erase and neccessary output when an erase character is
406 * present in the stream from the driver layer. Handles the complexities
407 * of UTF-8 multibyte symbols.
410 static void eraser(unsigned char c
, struct tty_struct
*tty
)
412 enum { ERASE
, WERASE
, KILL
} kill_type
;
413 int head
, seen_alnums
, cnt
;
416 if (tty
->read_head
== tty
->canon_head
) {
417 /* opost('\a', tty); */ /* what do you think? */
420 if (c
== ERASE_CHAR(tty
))
422 else if (c
== WERASE_CHAR(tty
))
426 spin_lock_irqsave(&tty
->read_lock
, flags
);
427 tty
->read_cnt
-= ((tty
->read_head
- tty
->canon_head
) &
428 (N_TTY_BUF_SIZE
- 1));
429 tty
->read_head
= tty
->canon_head
;
430 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
433 if (!L_ECHOK(tty
) || !L_ECHOKE(tty
) || !L_ECHOE(tty
)) {
434 spin_lock_irqsave(&tty
->read_lock
, flags
);
435 tty
->read_cnt
-= ((tty
->read_head
- tty
->canon_head
) &
436 (N_TTY_BUF_SIZE
- 1));
437 tty
->read_head
= tty
->canon_head
;
438 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
440 echo_char(KILL_CHAR(tty
), tty
);
441 /* Add a newline if ECHOK is on and ECHOKE is off. */
450 while (tty
->read_head
!= tty
->canon_head
) {
451 head
= tty
->read_head
;
453 /* erase a single possibly multibyte character */
455 head
= (head
- 1) & (N_TTY_BUF_SIZE
-1);
456 c
= tty
->read_buf
[head
];
457 } while (is_continuation(c
, tty
) && head
!= tty
->canon_head
);
459 /* do not partially erase */
460 if (is_continuation(c
, tty
))
463 if (kill_type
== WERASE
) {
464 /* Equivalent to BSD's ALTWERASE. */
465 if (isalnum(c
) || c
== '_')
467 else if (seen_alnums
)
470 cnt
= (tty
->read_head
- head
) & (N_TTY_BUF_SIZE
-1);
471 spin_lock_irqsave(&tty
->read_lock
, flags
);
472 tty
->read_head
= head
;
473 tty
->read_cnt
-= cnt
;
474 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
476 if (L_ECHOPRT(tty
)) {
482 /* if cnt > 1, output a multi-byte character */
485 head
= (head
+1) & (N_TTY_BUF_SIZE
-1);
486 put_char(tty
->read_buf
[head
], tty
);
488 } else if (kill_type
== ERASE
&& !L_ECHOE(tty
)) {
489 echo_char(ERASE_CHAR(tty
), tty
);
490 } else if (c
== '\t') {
491 unsigned int col
= tty
->canon_column
;
492 unsigned long tail
= tty
->canon_head
;
494 /* Find the column of the last char. */
495 while (tail
!= tty
->read_head
) {
496 c
= tty
->read_buf
[tail
];
499 else if (iscntrl(c
)) {
502 } else if (!is_continuation(c
, tty
))
504 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
507 /* should never happen */
508 if (tty
->column
> 0x80000000)
511 /* Now backup to that column. */
512 while (tty
->column
> col
) {
513 /* Can't use opost here. */
519 if (iscntrl(c
) && L_ECHOCTL(tty
)) {
526 if (!iscntrl(c
) || L_ECHOCTL(tty
)) {
535 if (kill_type
== ERASE
)
538 if (tty
->read_head
== tty
->canon_head
)
543 * isig - handle the ISIG optio
546 * @flush: force flush
548 * Called when a signal is being sent due to terminal input. This
549 * may caus terminal flushing to take place according to the termios
550 * settings and character used. Called from the driver receive_buf
551 * path so serialized.
554 static inline void isig(int sig
, struct tty_struct
*tty
, int flush
)
557 kill_pg(tty
->pgrp
, sig
, 1);
558 if (flush
|| !L_NOFLSH(tty
)) {
559 n_tty_flush_buffer(tty
);
560 if (tty
->driver
->flush_buffer
)
561 tty
->driver
->flush_buffer(tty
);
566 * n_tty_receive_break - handle break
569 * An RS232 break event has been hit in the incoming bitstream. This
570 * can cause a variety of events depending upon the termios settings.
572 * Called from the receive_buf path so single threaded.
575 static inline void n_tty_receive_break(struct tty_struct
*tty
)
580 isig(SIGINT
, tty
, 1);
584 put_tty_queue('\377', tty
);
585 put_tty_queue('\0', tty
);
587 put_tty_queue('\0', tty
);
588 wake_up_interruptible(&tty
->read_wait
);
592 * n_tty_receive_overrun - handle overrun reporting
595 * Data arrived faster than we could process it. While the tty
596 * driver has flagged this the bits that were missed are gone
599 * Called from the receive_buf path so single threaded. Does not
600 * need locking as num_overrun and overrun_time are function
604 static inline void n_tty_receive_overrun(struct tty_struct
*tty
)
609 if (time_before(tty
->overrun_time
, jiffies
- HZ
) ||
610 time_after(tty
->overrun_time
, jiffies
)) {
611 printk(KERN_WARNING
"%s: %d input overrun(s)\n",
614 tty
->overrun_time
= jiffies
;
615 tty
->num_overrun
= 0;
620 * n_tty_receive_parity_error - error notifier
621 * @tty: terminal device
624 * Process a parity error and queue the right data to indicate
625 * the error case if neccessary. Locking as per n_tty_receive_buf.
627 static inline void n_tty_receive_parity_error(struct tty_struct
*tty
,
634 put_tty_queue('\377', tty
);
635 put_tty_queue('\0', tty
);
636 put_tty_queue(c
, tty
);
637 } else if (I_INPCK(tty
))
638 put_tty_queue('\0', tty
);
640 put_tty_queue(c
, tty
);
641 wake_up_interruptible(&tty
->read_wait
);
645 * n_tty_receive_char - perform processing
646 * @tty: terminal device
649 * Process an individual character of input received from the driver.
650 * This is serialized with respect to itself by the rules for the
654 static inline void n_tty_receive_char(struct tty_struct
*tty
, unsigned char c
)
659 put_tty_queue(c
, tty
);
663 if (tty
->stopped
&& !tty
->flow_stopped
&&
664 I_IXON(tty
) && I_IXANY(tty
)) {
671 if (I_IUCLC(tty
) && L_IEXTEN(tty
))
676 if (c
== START_CHAR(tty
))
678 else if (c
== STOP_CHAR(tty
))
685 * If the previous character was LNEXT, or we know that this
686 * character is not one of the characters that we'll have to
687 * handle specially, do shortcut processing to speed things
690 if (!test_bit(c
, tty
->process_char_map
) || tty
->lnext
) {
694 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
695 put_char('\a', tty
); /* beep if no space */
698 /* Record the column of first canon char. */
699 if (tty
->canon_head
== tty
->read_head
)
700 tty
->canon_column
= tty
->column
;
703 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
704 put_tty_queue(c
, tty
);
705 put_tty_queue(c
, tty
);
714 } else if (c
== '\n' && I_INLCR(tty
))
717 if (c
== START_CHAR(tty
)) {
721 if (c
== STOP_CHAR(tty
)) {
729 if (c
== INTR_CHAR(tty
))
732 if (c
== QUIT_CHAR(tty
))
735 if (c
== SUSP_CHAR(tty
)) {
737 isig(signal
, tty
, 0);
742 if (c
== ERASE_CHAR(tty
) || c
== KILL_CHAR(tty
) ||
743 (c
== WERASE_CHAR(tty
) && L_IEXTEN(tty
))) {
747 if (c
== LNEXT_CHAR(tty
) && L_IEXTEN(tty
)) {
751 if (L_ECHOCTL(tty
)) {
758 if (c
== REPRINT_CHAR(tty
) && L_ECHO(tty
) &&
760 unsigned long tail
= tty
->canon_head
;
765 while (tail
!= tty
->read_head
) {
766 echo_char(tty
->read_buf
[tail
], tty
);
767 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
772 if (L_ECHO(tty
) || L_ECHONL(tty
)) {
773 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
781 if (c
== EOF_CHAR(tty
)) {
782 if (tty
->canon_head
!= tty
->read_head
)
783 set_bit(TTY_PUSH
, &tty
->flags
);
787 if ((c
== EOL_CHAR(tty
)) ||
788 (c
== EOL2_CHAR(tty
) && L_IEXTEN(tty
))) {
790 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
793 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
797 /* Record the column of first canon char. */
798 if (tty
->canon_head
== tty
->read_head
)
799 tty
->canon_column
= tty
->column
;
803 * XXX does PARMRK doubling happen for
804 * EOL_CHAR and EOL2_CHAR?
806 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
807 put_tty_queue(c
, tty
);
810 spin_lock_irqsave(&tty
->read_lock
, flags
);
811 set_bit(tty
->read_head
, tty
->read_flags
);
812 put_tty_queue_nolock(c
, tty
);
813 tty
->canon_head
= tty
->read_head
;
815 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
816 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
817 if (waitqueue_active(&tty
->read_wait
))
818 wake_up_interruptible(&tty
->read_wait
);
825 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
826 put_char('\a', tty
); /* beep if no space */
832 /* Record the column of first canon char. */
833 if (tty
->canon_head
== tty
->read_head
)
834 tty
->canon_column
= tty
->column
;
839 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
840 put_tty_queue(c
, tty
);
842 put_tty_queue(c
, tty
);
846 * n_tty_receive_room - receive space
849 * Called by the driver to find out how much data it is
850 * permitted to feed to the line discipline without any being lost
851 * and thus to manage flow control. Not serialized. Answers for the
855 static int n_tty_receive_room(struct tty_struct
*tty
)
857 int left
= N_TTY_BUF_SIZE
- tty
->read_cnt
- 1;
860 * If we are doing input canonicalization, and there are no
861 * pending newlines, let characters through without limit, so
862 * that erase characters will be handled. Other excess
863 * characters will be beeped.
865 if (tty
->icanon
&& !tty
->canon_data
)
866 return N_TTY_BUF_SIZE
;
874 * n_tty_write_wakeup - asynchronous I/O notifier
877 * Required for the ptys, serial driver etc. since processes
878 * that attach themselves to the master and rely on ASYNC
879 * IO must be woken up
882 static void n_tty_write_wakeup(struct tty_struct
*tty
)
886 set_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
887 kill_fasync(&tty
->fasync
, SIGIO
, POLL_OUT
);
893 * n_tty_receive_buf - data receive
894 * @tty: terminal device
899 * Called by the terminal driver when a block of characters has
900 * been received. This function must be called from soft contexts
901 * not from interrupt context. The driver is responsible for making
902 * calls one at a time and in order (or using flush_to_ldisc)
905 static void n_tty_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
908 const unsigned char *p
;
909 char *f
, flags
= TTY_NORMAL
;
912 unsigned long cpuflags
;
918 spin_lock_irqsave(&tty
->read_lock
, cpuflags
);
919 i
= min(N_TTY_BUF_SIZE
- tty
->read_cnt
,
920 N_TTY_BUF_SIZE
- tty
->read_head
);
922 memcpy(tty
->read_buf
+ tty
->read_head
, cp
, i
);
923 tty
->read_head
= (tty
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
928 i
= min(N_TTY_BUF_SIZE
- tty
->read_cnt
,
929 N_TTY_BUF_SIZE
- tty
->read_head
);
931 memcpy(tty
->read_buf
+ tty
->read_head
, cp
, i
);
932 tty
->read_head
= (tty
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
934 spin_unlock_irqrestore(&tty
->read_lock
, cpuflags
);
936 for (i
=count
, p
= cp
, f
= fp
; i
; i
--, p
++) {
941 n_tty_receive_char(tty
, *p
);
944 n_tty_receive_break(tty
);
948 n_tty_receive_parity_error(tty
, *p
);
951 n_tty_receive_overrun(tty
);
954 printk("%s: unknown flag %d\n",
955 tty_name(tty
, buf
), flags
);
959 if (tty
->driver
->flush_chars
)
960 tty
->driver
->flush_chars(tty
);
963 if (!tty
->icanon
&& (tty
->read_cnt
>= tty
->minimum_to_wake
)) {
964 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
965 if (waitqueue_active(&tty
->read_wait
))
966 wake_up_interruptible(&tty
->read_wait
);
970 * Check the remaining room for the input canonicalization
971 * mode. We don't want to throttle the driver if we're in
972 * canonical mode and don't have a newline yet!
974 if (n_tty_receive_room(tty
) < TTY_THRESHOLD_THROTTLE
) {
975 /* check TTY_THROTTLED first so it indicates our state */
976 if (!test_and_set_bit(TTY_THROTTLED
, &tty
->flags
) &&
977 tty
->driver
->throttle
)
978 tty
->driver
->throttle(tty
);
982 int is_ignored(int sig
)
984 return (sigismember(¤t
->blocked
, sig
) ||
985 current
->sighand
->action
[sig
-1].sa
.sa_handler
== SIG_IGN
);
989 * n_tty_set_termios - termios data changed
991 * @old: previous data
993 * Called by the tty layer when the user changes termios flags so
994 * that the line discipline can plan ahead. This function cannot sleep
995 * and is protected from re-entry by the tty layer. The user is
996 * guaranteed that this function will not be re-entered or in progress
997 * when the ldisc is closed.
1000 static void n_tty_set_termios(struct tty_struct
*tty
, struct termios
* old
)
1005 tty
->icanon
= (L_ICANON(tty
) != 0);
1006 if (test_bit(TTY_HW_COOK_IN
, &tty
->flags
)) {
1011 if (I_ISTRIP(tty
) || I_IUCLC(tty
) || I_IGNCR(tty
) ||
1012 I_ICRNL(tty
) || I_INLCR(tty
) || L_ICANON(tty
) ||
1013 I_IXON(tty
) || L_ISIG(tty
) || L_ECHO(tty
) ||
1015 memset(tty
->process_char_map
, 0, 256/8);
1017 if (I_IGNCR(tty
) || I_ICRNL(tty
))
1018 set_bit('\r', tty
->process_char_map
);
1020 set_bit('\n', tty
->process_char_map
);
1022 if (L_ICANON(tty
)) {
1023 set_bit(ERASE_CHAR(tty
), tty
->process_char_map
);
1024 set_bit(KILL_CHAR(tty
), tty
->process_char_map
);
1025 set_bit(EOF_CHAR(tty
), tty
->process_char_map
);
1026 set_bit('\n', tty
->process_char_map
);
1027 set_bit(EOL_CHAR(tty
), tty
->process_char_map
);
1028 if (L_IEXTEN(tty
)) {
1029 set_bit(WERASE_CHAR(tty
),
1030 tty
->process_char_map
);
1031 set_bit(LNEXT_CHAR(tty
),
1032 tty
->process_char_map
);
1033 set_bit(EOL2_CHAR(tty
),
1034 tty
->process_char_map
);
1036 set_bit(REPRINT_CHAR(tty
),
1037 tty
->process_char_map
);
1041 set_bit(START_CHAR(tty
), tty
->process_char_map
);
1042 set_bit(STOP_CHAR(tty
), tty
->process_char_map
);
1045 set_bit(INTR_CHAR(tty
), tty
->process_char_map
);
1046 set_bit(QUIT_CHAR(tty
), tty
->process_char_map
);
1047 set_bit(SUSP_CHAR(tty
), tty
->process_char_map
);
1049 clear_bit(__DISABLED_CHAR
, tty
->process_char_map
);
1054 if ((I_IGNBRK(tty
) || (!I_BRKINT(tty
) && !I_PARMRK(tty
))) &&
1055 (I_IGNPAR(tty
) || !I_INPCK(tty
)) &&
1056 (tty
->driver
->flags
& TTY_DRIVER_REAL_RAW
))
1064 * n_tty_close - close the ldisc for this tty
1067 * Called from the terminal layer when this line discipline is
1068 * being shut down, either because of a close or becsuse of a
1069 * discipline change. The function will not be called while other
1070 * ldisc methods are in progress.
1073 static void n_tty_close(struct tty_struct
*tty
)
1075 n_tty_flush_buffer(tty
);
1076 if (tty
->read_buf
) {
1077 free_buf(tty
->read_buf
);
1078 tty
->read_buf
= NULL
;
1083 * n_tty_open - open an ldisc
1084 * @tty: terminal to open
1086 * Called when this line discipline is being attached to the
1087 * terminal device. Can sleep. Called serialized so that no
1088 * other events will occur in parallel. No further open will occur
1092 static int n_tty_open(struct tty_struct
*tty
)
1097 /* This one is ugly. Currently a malloc failure here can panic */
1098 if (!tty
->read_buf
) {
1099 tty
->read_buf
= alloc_buf();
1103 memset(tty
->read_buf
, 0, N_TTY_BUF_SIZE
);
1104 reset_buffer_flags(tty
);
1106 n_tty_set_termios(tty
, NULL
);
1107 tty
->minimum_to_wake
= 1;
1112 static inline int input_available_p(struct tty_struct
*tty
, int amt
)
1115 if (tty
->canon_data
)
1117 } else if (tty
->read_cnt
>= (amt
? amt
: 1))
1124 * copy_from_read_buf - copy read data directly
1125 * @tty: terminal device
1129 * Helper function to speed up read_chan. It is only called when
1130 * ICANON is off; it copies characters straight from the tty queue to
1131 * user space directly. It can be profitably called twice; once to
1132 * drain the space from the tail pointer to the (physical) end of the
1133 * buffer, and once to drain the space from the (physical) beginning of
1134 * the buffer to head pointer.
1136 * Called under the tty->atomic_read sem and with TTY_DONT_FLIP set
1140 static inline int copy_from_read_buf(struct tty_struct
*tty
,
1141 unsigned char __user
**b
,
1147 unsigned long flags
;
1150 spin_lock_irqsave(&tty
->read_lock
, flags
);
1151 n
= min(tty
->read_cnt
, N_TTY_BUF_SIZE
- tty
->read_tail
);
1153 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
1156 retval
= copy_to_user(*b
, &tty
->read_buf
[tty
->read_tail
], n
);
1158 spin_lock_irqsave(&tty
->read_lock
, flags
);
1159 tty
->read_tail
= (tty
->read_tail
+ n
) & (N_TTY_BUF_SIZE
-1);
1161 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
1168 extern ssize_t
redirected_tty_write(struct file
*,const char *,size_t,loff_t
*);
1171 * job_control - check job control
1173 * @file: file handle
1175 * Perform job control management checks on this file/tty descriptor
1176 * and if appropriate send any needed signals and return a negative
1177 * error code if action should be taken.
1180 static int job_control(struct tty_struct
*tty
, struct file
*file
)
1182 /* Job control check -- must be done at start and after
1183 every sleep (POSIX.1 7.1.1.4). */
1184 /* NOTE: not yet done after every sleep pending a thorough
1185 check of the logic of this change. -- jlc */
1186 /* don't stop on /dev/console */
1187 if (file
->f_op
->write
!= redirected_tty_write
&&
1188 current
->signal
->tty
== tty
) {
1190 printk("read_chan: tty->pgrp <= 0!\n");
1191 else if (process_group(current
) != tty
->pgrp
) {
1192 if (is_ignored(SIGTTIN
) ||
1193 is_orphaned_pgrp(process_group(current
)))
1195 kill_pg(process_group(current
), SIGTTIN
, 1);
1196 return -ERESTARTSYS
;
1204 * read_chan - read function for tty
1206 * @file: file object
1207 * @buf: userspace buffer pointer
1210 * Perform reads for the line discipline. We are guaranteed that the
1211 * line discipline will not be closed under us but we may get multiple
1212 * parallel readers and must handle this ourselves. We may also get
1213 * a hangup. Always called in user context, may sleep.
1215 * This code must be sure never to sleep through a hangup.
1218 static ssize_t
read_chan(struct tty_struct
*tty
, struct file
*file
,
1219 unsigned char __user
*buf
, size_t nr
)
1221 unsigned char __user
*b
= buf
;
1222 DECLARE_WAITQUEUE(wait
, current
);
1228 unsigned long flags
;
1232 if (!tty
->read_buf
) {
1233 printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
1237 c
= job_control(tty
, file
);
1242 timeout
= MAX_SCHEDULE_TIMEOUT
;
1244 time
= (HZ
/ 10) * TIME_CHAR(tty
);
1245 minimum
= MIN_CHAR(tty
);
1248 tty
->minimum_to_wake
= 1;
1249 else if (!waitqueue_active(&tty
->read_wait
) ||
1250 (tty
->minimum_to_wake
> minimum
))
1251 tty
->minimum_to_wake
= minimum
;
1258 tty
->minimum_to_wake
= minimum
= 1;
1263 * Internal serialization of reads.
1265 if (file
->f_flags
& O_NONBLOCK
) {
1266 if (down_trylock(&tty
->atomic_read
))
1270 if (down_interruptible(&tty
->atomic_read
))
1271 return -ERESTARTSYS
;
1274 add_wait_queue(&tty
->read_wait
, &wait
);
1275 set_bit(TTY_DONT_FLIP
, &tty
->flags
);
1277 /* First test for status change. */
1278 if (tty
->packet
&& tty
->link
->ctrl_status
) {
1282 cs
= tty
->link
->ctrl_status
;
1283 tty
->link
->ctrl_status
= 0;
1284 if (put_user(cs
, b
++)) {
1292 /* This statement must be first before checking for input
1293 so that any interrupt will set the state back to
1295 set_current_state(TASK_INTERRUPTIBLE
);
1297 if (((minimum
- (b
- buf
)) < tty
->minimum_to_wake
) &&
1298 ((minimum
- (b
- buf
)) >= 1))
1299 tty
->minimum_to_wake
= (minimum
- (b
- buf
));
1301 if (!input_available_p(tty
, 0)) {
1302 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
)) {
1306 if (tty_hung_up_p(file
))
1310 if (file
->f_flags
& O_NONBLOCK
) {
1314 if (signal_pending(current
)) {
1315 retval
= -ERESTARTSYS
;
1318 clear_bit(TTY_DONT_FLIP
, &tty
->flags
);
1319 timeout
= schedule_timeout(timeout
);
1320 set_bit(TTY_DONT_FLIP
, &tty
->flags
);
1323 __set_current_state(TASK_RUNNING
);
1325 /* Deal with packet mode. */
1326 if (tty
->packet
&& b
== buf
) {
1327 if (put_user(TIOCPKT_DATA
, b
++)) {
1336 /* N.B. avoid overrun if nr == 0 */
1337 while (nr
&& tty
->read_cnt
) {
1340 eol
= test_and_clear_bit(tty
->read_tail
,
1342 c
= tty
->read_buf
[tty
->read_tail
];
1343 spin_lock_irqsave(&tty
->read_lock
, flags
);
1344 tty
->read_tail
= ((tty
->read_tail
+1) &
1345 (N_TTY_BUF_SIZE
-1));
1348 /* this test should be redundant:
1349 * we shouldn't be reading data if
1352 if (--tty
->canon_data
< 0)
1353 tty
->canon_data
= 0;
1355 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
1357 if (!eol
|| (c
!= __DISABLED_CHAR
)) {
1358 if (put_user(c
, b
++)) {
1372 uncopied
= copy_from_read_buf(tty
, &b
, &nr
);
1373 uncopied
+= copy_from_read_buf(tty
, &b
, &nr
);
1380 /* If there is enough space in the read buffer now, let the
1381 * low-level driver know. We use n_tty_chars_in_buffer() to
1382 * check the buffer, as it now knows about canonical mode.
1383 * Otherwise, if the driver is throttled and the line is
1384 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1385 * we won't get any more characters.
1387 if (n_tty_chars_in_buffer(tty
) <= TTY_THRESHOLD_UNTHROTTLE
)
1388 check_unthrottle(tty
);
1390 if (b
- buf
>= minimum
)
1395 clear_bit(TTY_DONT_FLIP
, &tty
->flags
);
1396 up(&tty
->atomic_read
);
1397 remove_wait_queue(&tty
->read_wait
, &wait
);
1399 if (!waitqueue_active(&tty
->read_wait
))
1400 tty
->minimum_to_wake
= minimum
;
1402 __set_current_state(TASK_RUNNING
);
1407 clear_bit(TTY_PUSH
, &tty
->flags
);
1408 } else if (test_and_clear_bit(TTY_PUSH
, &tty
->flags
))
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
);
1476 c
= tty
->driver
->write(tty
, b
, nr
);
1486 if (file
->f_flags
& O_NONBLOCK
) {
1493 __set_current_state(TASK_RUNNING
);
1494 remove_wait_queue(&tty
->write_wait
, &wait
);
1495 return (b
- buf
) ? b
- buf
: retval
;
1499 * normal_poll - poll method for N_TTY
1500 * @tty: terminal device
1501 * @file: file accessing it
1504 * Called when the line discipline is asked to poll() for data or
1505 * for special events. This code is not serialized with respect to
1506 * other events save open/close.
1508 * This code must be sure never to sleep through a hangup.
1509 * Called without the kernel lock held - fine
1511 * FIXME: if someone changes the VMIN or discipline settings for the
1512 * terminal while another process is in poll() the poll does not
1513 * recompute the new limits. Possibly set_termios should issue
1514 * a read wakeup to fix this bug.
1517 static unsigned int normal_poll(struct tty_struct
* tty
, struct file
* file
, poll_table
*wait
)
1519 unsigned int mask
= 0;
1521 poll_wait(file
, &tty
->read_wait
, wait
);
1522 poll_wait(file
, &tty
->write_wait
, wait
);
1523 if (input_available_p(tty
, TIME_CHAR(tty
) ? 0 : MIN_CHAR(tty
)))
1524 mask
|= POLLIN
| POLLRDNORM
;
1525 if (tty
->packet
&& tty
->link
->ctrl_status
)
1526 mask
|= POLLPRI
| POLLIN
| POLLRDNORM
;
1527 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
))
1529 if (tty_hung_up_p(file
))
1531 if (!(mask
& (POLLHUP
| POLLIN
| POLLRDNORM
))) {
1532 if (MIN_CHAR(tty
) && !TIME_CHAR(tty
))
1533 tty
->minimum_to_wake
= MIN_CHAR(tty
);
1535 tty
->minimum_to_wake
= 1;
1537 if (tty
->driver
->chars_in_buffer(tty
) < WAKEUP_CHARS
&&
1538 tty
->driver
->write_room(tty
) > 0)
1539 mask
|= POLLOUT
| POLLWRNORM
;
1543 struct tty_ldisc tty_ldisc_N_TTY
= {
1544 TTY_LDISC_MAGIC
, /* magic */
1548 n_tty_open
, /* open */
1549 n_tty_close
, /* close */
1550 n_tty_flush_buffer
, /* flush_buffer */
1551 n_tty_chars_in_buffer
, /* chars_in_buffer */
1552 read_chan
, /* read */
1553 write_chan
, /* write */
1554 n_tty_ioctl
, /* ioctl */
1555 n_tty_set_termios
, /* set_termios */
1556 normal_poll
, /* poll */
1558 n_tty_receive_buf
, /* receive_buf */
1559 n_tty_receive_room
, /* receive_room */
1560 n_tty_write_wakeup
/* write_wakeup */