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 n_tty_write 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>
48 #include <linux/audit.h>
49 #include <linux/file.h>
51 #include <asm/uaccess.h>
52 #include <asm/system.h>
54 /* number of characters left in xmit buffer before select has we have room */
55 #define WAKEUP_CHARS 256
58 * This defines the low- and high-watermarks for throttling and
59 * unthrottling the TTY driver. These watermarks are used for
60 * controlling the space in the read buffer.
62 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
63 #define TTY_THRESHOLD_UNTHROTTLE 128
65 static inline unsigned char *alloc_buf(void)
67 gfp_t prio = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
69 if (PAGE_SIZE != N_TTY_BUF_SIZE)
70 return kmalloc(N_TTY_BUF_SIZE, prio);
72 return (unsigned char *)__get_free_page(prio);
75 static inline void free_buf(unsigned char *buf)
77 if (PAGE_SIZE != N_TTY_BUF_SIZE)
80 free_page((unsigned long) buf);
83 static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
84 unsigned char __user *ptr)
86 tty_audit_add_data(tty, &x, 1);
87 return put_user(x, ptr);
91 * n_tty_set__room - receive space
94 * Called by the driver to find out how much data it is
95 * permitted to feed to the line discipline without any being lost
96 * and thus to manage flow control. Not serialized. Answers for the
100 static void n_tty_set_room(struct tty_struct *tty)
102 /* tty->read_cnt is not read locked ? */
103 int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
106 * If we are doing input canonicalization, and there are no
107 * pending newlines, let characters through without limit, so
108 * that erase characters will be handled. Other excess
109 * characters will be beeped.
112 left = tty->icanon && !tty->canon_data;
113 tty->receive_room = left;
116 static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
118 if (tty->read_cnt < N_TTY_BUF_SIZE) {
119 tty->read_buf[tty->read_head] = c;
120 tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
126 * put_tty_queue - add character to tty
130 * Add a character to the tty read_buf queue. This is done under the
131 * read_lock to serialize character addition and also to protect us
132 * against parallel reads or flushes
135 static void put_tty_queue(unsigned char c, struct tty_struct *tty)
139 * The problem of stomping on the buffers ends here.
140 * Why didn't anyone see this one coming? --AJK
142 spin_lock_irqsave(&tty->read_lock, flags);
143 put_tty_queue_nolock(c, tty);
144 spin_unlock_irqrestore(&tty->read_lock, flags);
148 * check_unthrottle - allow new receive data
151 * Check whether to call the driver unthrottle functions
153 * Can sleep, may be called under the atomic_read_lock mutex but
154 * this is not guaranteed.
156 static void check_unthrottle(struct tty_struct *tty)
163 * reset_buffer_flags - reset buffer state
164 * @tty: terminal to reset
166 * Reset the read buffer counters, clear the flags,
167 * and make sure the driver is unthrottled. Called
168 * from n_tty_open() and n_tty_flush_buffer().
170 * Locking: tty_read_lock for read fields.
172 static void reset_buffer_flags(struct tty_struct *tty)
176 spin_lock_irqsave(&tty->read_lock, flags);
177 tty->read_head = tty->read_tail = tty->read_cnt = 0;
178 spin_unlock_irqrestore(&tty->read_lock, flags);
179 tty->canon_head = tty->canon_data = tty->erasing = 0;
180 memset(&tty->read_flags, 0, sizeof tty->read_flags);
182 check_unthrottle(tty);
186 * n_tty_flush_buffer - clean input queue
187 * @tty: terminal device
189 * Flush the input buffer. Called when the line discipline is
190 * being closed, when the tty layer wants the buffer flushed (eg
191 * at hangup) or when the N_TTY line discipline internally has to
192 * clean the pending queue (for example some signals).
194 * Locking: ctrl_lock, read_lock.
197 static void n_tty_flush_buffer(struct tty_struct *tty)
200 /* clear everything and unthrottle the driver */
201 reset_buffer_flags(tty);
206 spin_lock_irqsave(&tty->ctrl_lock, flags);
207 if (tty->link->packet) {
208 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
209 wake_up_interruptible(&tty->link->read_wait);
211 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
215 * n_tty_chars_in_buffer - report available bytes
218 * Report the number of characters buffered to be delivered to user
219 * at this instant in time.
224 static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
229 spin_lock_irqsave(&tty->read_lock, flags);
232 } else if (tty->canon_data) {
233 n = (tty->canon_head > tty->read_tail) ?
234 tty->canon_head - tty->read_tail :
235 tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
237 spin_unlock_irqrestore(&tty->read_lock, flags);
242 * is_utf8_continuation - utf8 multibyte check
245 * Returns true if the utf8 character 'c' is a multibyte continuation
246 * character. We use this to correctly compute the on screen size
247 * of the character when printing
250 static inline int is_utf8_continuation(unsigned char c)
252 return (c & 0xc0) == 0x80;
256 * is_continuation - multibyte check
259 * Returns true if the utf8 character 'c' is a multibyte continuation
260 * character and the terminal is in unicode mode.
263 static inline int is_continuation(unsigned char c, struct tty_struct *tty)
265 return I_IUTF8(tty) && is_utf8_continuation(c);
269 * opost - output post processor
270 * @c: character (or partial unicode symbol)
271 * @tty: terminal device
273 * Perform OPOST processing. Returns -1 when the output device is
274 * full and the character must be retried. Note that Linux currently
275 * ignores TABDLY, CRDLY, VTDLY, FFDLY and NLDLY. They simply aren't
276 * relevant in the world today. If you ever need them, add them here.
278 * Called from both the receive and transmit sides and can be called
279 * re-entrantly. Relies on lock_kernel() for tty->column state.
282 static int opost(unsigned char c, struct tty_struct *tty)
286 space = tty_write_room(tty);
301 tty_put_char(tty, '\r');
304 tty->canon_column = tty->column;
307 if (O_ONOCR(tty) && tty->column == 0) {
314 tty->canon_column = tty->column = 0;
317 tty->canon_column = tty->column = 0;
320 spaces = 8 - (tty->column & 7);
321 if (O_TABDLY(tty) == XTABS) {
322 if (space < spaces) {
326 tty->column += spaces;
327 tty->ops->write(tty, " ", spaces);
331 tty->column += spaces;
340 if (!iscntrl(c) && !is_continuation(c, tty))
345 tty_put_char(tty, c);
351 * opost_block - block postprocess
352 * @tty: terminal device
353 * @inbuf: user buffer
354 * @nr: number of bytes
356 * This path is used to speed up block console writes, among other
357 * things when processing blocks of output data. It handles only
358 * the simple cases normally found and helps to generate blocks of
359 * symbols for the console driver and thus improve performance.
361 * Called from n_tty_write under the tty layer write lock. Relies
362 * on lock_kernel for the tty->column state.
365 static ssize_t opost_block(struct tty_struct *tty,
366 const unsigned char *buf, unsigned int nr)
370 const unsigned char *cp;
372 space = tty_write_room(tty);
379 for (i = 0, cp = buf; i < nr; i++, cp++) {
386 tty->canon_column = tty->column;
389 if (O_ONOCR(tty) && tty->column == 0)
393 tty->canon_column = tty->column = 0;
410 if (tty->ops->flush_chars)
411 tty->ops->flush_chars(tty);
412 i = tty->ops->write(tty, buf, i);
419 * echo_char - echo characters
420 * @c: unicode byte to echo
421 * @tty: terminal device
423 * Echo user input back onto the screen. This must be called only when
424 * L_ECHO(tty) is true. Called from the driver receive_buf path.
426 * Relies on BKL for tty column locking
429 static void echo_char(unsigned char c, struct tty_struct *tty)
431 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
432 tty_put_char(tty, '^');
433 tty_put_char(tty, c ^ 0100);
440 * finsh_erasing - complete erase
441 * @tty: tty doing the erase
443 * Relies on BKL for tty column locking
445 static inline void finish_erasing(struct tty_struct *tty)
448 tty_put_char(tty, '/');
455 * eraser - handle erase function
456 * @c: character input
457 * @tty: terminal device
459 * Perform erase and necessary output when an erase character is
460 * present in the stream from the driver layer. Handles the complexities
461 * of UTF-8 multibyte symbols.
463 * Locking: read_lock for tty buffers, BKL for column/erasing state
466 static void eraser(unsigned char c, struct tty_struct *tty)
468 enum { ERASE, WERASE, KILL } kill_type;
469 int head, seen_alnums, cnt;
472 /* FIXME: locking needed ? */
473 if (tty->read_head == tty->canon_head) {
474 /* opost('\a', tty); */ /* what do you think? */
477 if (c == ERASE_CHAR(tty))
479 else if (c == WERASE_CHAR(tty))
483 spin_lock_irqsave(&tty->read_lock, flags);
484 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
485 (N_TTY_BUF_SIZE - 1));
486 tty->read_head = tty->canon_head;
487 spin_unlock_irqrestore(&tty->read_lock, flags);
490 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
491 spin_lock_irqsave(&tty->read_lock, flags);
492 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
493 (N_TTY_BUF_SIZE - 1));
494 tty->read_head = tty->canon_head;
495 spin_unlock_irqrestore(&tty->read_lock, flags);
497 echo_char(KILL_CHAR(tty), tty);
498 /* Add a newline if ECHOK is on and ECHOKE is off. */
507 /* FIXME: Locking ?? */
508 while (tty->read_head != tty->canon_head) {
509 head = tty->read_head;
511 /* erase a single possibly multibyte character */
513 head = (head - 1) & (N_TTY_BUF_SIZE-1);
514 c = tty->read_buf[head];
515 } while (is_continuation(c, tty) && head != tty->canon_head);
517 /* do not partially erase */
518 if (is_continuation(c, tty))
521 if (kill_type == WERASE) {
522 /* Equivalent to BSD's ALTWERASE. */
523 if (isalnum(c) || c == '_')
525 else if (seen_alnums)
528 cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
529 spin_lock_irqsave(&tty->read_lock, flags);
530 tty->read_head = head;
531 tty->read_cnt -= cnt;
532 spin_unlock_irqrestore(&tty->read_lock, flags);
534 if (L_ECHOPRT(tty)) {
536 tty_put_char(tty, '\\');
540 /* if cnt > 1, output a multi-byte character */
543 head = (head+1) & (N_TTY_BUF_SIZE-1);
544 tty_put_char(tty, tty->read_buf[head]);
546 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
547 echo_char(ERASE_CHAR(tty), tty);
548 } else if (c == '\t') {
549 unsigned int col = tty->canon_column;
550 unsigned long tail = tty->canon_head;
552 /* Find the column of the last char. */
553 while (tail != tty->read_head) {
554 c = tty->read_buf[tail];
557 else if (iscntrl(c)) {
560 } else if (!is_continuation(c, tty))
562 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
565 /* should never happen */
566 if (tty->column > 0x80000000)
569 /* Now backup to that column. */
570 while (tty->column > col) {
571 /* Can't use opost here. */
572 tty_put_char(tty, '\b');
577 if (iscntrl(c) && L_ECHOCTL(tty)) {
578 tty_put_char(tty, '\b');
579 tty_put_char(tty, ' ');
580 tty_put_char(tty, '\b');
584 if (!iscntrl(c) || L_ECHOCTL(tty)) {
585 tty_put_char(tty, '\b');
586 tty_put_char(tty, ' ');
587 tty_put_char(tty, '\b');
593 if (kill_type == ERASE)
596 if (tty->read_head == tty->canon_head)
601 * isig - handle the ISIG optio
604 * @flush: force flush
606 * Called when a signal is being sent due to terminal input. This
607 * may caus terminal flushing to take place according to the termios
608 * settings and character used. Called from the driver receive_buf
609 * path so serialized.
611 * Locking: ctrl_lock, read_lock (both via flush buffer)
614 static inline void isig(int sig, struct tty_struct *tty, int flush)
617 kill_pgrp(tty->pgrp, sig, 1);
618 if (flush || !L_NOFLSH(tty)) {
619 n_tty_flush_buffer(tty);
620 tty_driver_flush_buffer(tty);
625 * n_tty_receive_break - handle break
628 * An RS232 break event has been hit in the incoming bitstream. This
629 * can cause a variety of events depending upon the termios settings.
631 * Called from the receive_buf path so single threaded.
634 static inline void n_tty_receive_break(struct tty_struct *tty)
639 isig(SIGINT, tty, 1);
643 put_tty_queue('\377', tty);
644 put_tty_queue('\0', tty);
646 put_tty_queue('\0', tty);
647 wake_up_interruptible(&tty->read_wait);
651 * n_tty_receive_overrun - handle overrun reporting
654 * Data arrived faster than we could process it. While the tty
655 * driver has flagged this the bits that were missed are gone
658 * Called from the receive_buf path so single threaded. Does not
659 * need locking as num_overrun and overrun_time are function
663 static inline void n_tty_receive_overrun(struct tty_struct *tty)
668 if (time_before(tty->overrun_time, jiffies - HZ) ||
669 time_after(tty->overrun_time, jiffies)) {
670 printk(KERN_WARNING "%s: %d input overrun(s)\n",
673 tty->overrun_time = jiffies;
674 tty->num_overrun = 0;
679 * n_tty_receive_parity_error - error notifier
680 * @tty: terminal device
683 * Process a parity error and queue the right data to indicate
684 * the error case if necessary. Locking as per n_tty_receive_buf.
686 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
692 put_tty_queue('\377', tty);
693 put_tty_queue('\0', tty);
694 put_tty_queue(c, tty);
695 } else if (I_INPCK(tty))
696 put_tty_queue('\0', tty);
698 put_tty_queue(c, tty);
699 wake_up_interruptible(&tty->read_wait);
703 * n_tty_receive_char - perform processing
704 * @tty: terminal device
707 * Process an individual character of input received from the driver.
708 * This is serialized with respect to itself by the rules for the
712 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
717 put_tty_queue(c, tty);
723 if (I_IUCLC(tty) && L_IEXTEN(tty))
726 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
727 ((I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty)) ||
728 c == INTR_CHAR(tty) || c == QUIT_CHAR(tty) || c == SUSP_CHAR(tty)))
733 if (c == START_CHAR(tty))
735 else if (c == STOP_CHAR(tty))
742 * If the previous character was LNEXT, or we know that this
743 * character is not one of the characters that we'll have to
744 * handle specially, do shortcut processing to speed things
747 if (!test_bit(c, tty->process_char_map) || tty->lnext) {
751 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
752 tty_put_char(tty, '\a'); /* beep if no space */
755 /* Record the column of first canon char. */
756 if (tty->canon_head == tty->read_head)
757 tty->canon_column = tty->column;
760 if (I_PARMRK(tty) && c == (unsigned char) '\377')
761 put_tty_queue(c, tty);
762 put_tty_queue(c, tty);
767 if (c == START_CHAR(tty)) {
771 if (c == STOP_CHAR(tty)) {
780 if (c == INTR_CHAR(tty))
783 if (c == QUIT_CHAR(tty))
786 if (c == SUSP_CHAR(tty)) {
789 * Echo character, and then send the signal.
790 * Note that we do not use isig() here because we want
792 * 1) flush, 2) echo, 3) signal
794 if (!L_NOFLSH(tty)) {
795 n_tty_flush_buffer(tty);
796 tty_driver_flush_buffer(tty);
801 kill_pgrp(tty->pgrp, signal, 1);
811 } else if (c == '\n' && I_INLCR(tty))
815 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
816 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
820 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
824 if (L_ECHOCTL(tty)) {
825 tty_put_char(tty, '^');
826 tty_put_char(tty, '\b');
831 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
833 unsigned long tail = tty->canon_head;
838 while (tail != tty->read_head) {
839 echo_char(tty->read_buf[tail], tty);
840 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
845 if (L_ECHO(tty) || L_ECHONL(tty)) {
846 if (tty->read_cnt >= N_TTY_BUF_SIZE-1)
847 tty_put_char(tty, '\a');
852 if (c == EOF_CHAR(tty)) {
853 if (tty->canon_head != tty->read_head)
854 set_bit(TTY_PUSH, &tty->flags);
858 if ((c == EOL_CHAR(tty)) ||
859 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
861 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
864 if (tty->read_cnt >= N_TTY_BUF_SIZE-1)
865 tty_put_char(tty, '\a');
866 /* Record the column of first canon char. */
867 if (tty->canon_head == tty->read_head)
868 tty->canon_column = tty->column;
872 * XXX does PARMRK doubling happen for
873 * EOL_CHAR and EOL2_CHAR?
875 if (I_PARMRK(tty) && c == (unsigned char) '\377')
876 put_tty_queue(c, tty);
879 spin_lock_irqsave(&tty->read_lock, flags);
880 set_bit(tty->read_head, tty->read_flags);
881 put_tty_queue_nolock(c, tty);
882 tty->canon_head = tty->read_head;
884 spin_unlock_irqrestore(&tty->read_lock, flags);
885 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
886 if (waitqueue_active(&tty->read_wait))
887 wake_up_interruptible(&tty->read_wait);
894 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
895 tty_put_char(tty, '\a'); /* beep if no space */
901 /* Record the column of first canon char. */
902 if (tty->canon_head == tty->read_head)
903 tty->canon_column = tty->column;
908 if (I_PARMRK(tty) && c == (unsigned char) '\377')
909 put_tty_queue(c, tty);
911 put_tty_queue(c, tty);
916 * n_tty_write_wakeup - asynchronous I/O notifier
919 * Required for the ptys, serial driver etc. since processes
920 * that attach themselves to the master and rely on ASYNC
921 * IO must be woken up
924 static void n_tty_write_wakeup(struct tty_struct *tty)
927 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
928 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
933 * n_tty_receive_buf - data receive
934 * @tty: terminal device
939 * Called by the terminal driver when a block of characters has
940 * been received. This function must be called from soft contexts
941 * not from interrupt context. The driver is responsible for making
942 * calls one at a time and in order (or using flush_to_ldisc)
945 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
948 const unsigned char *p;
949 char *f, flags = TTY_NORMAL;
952 unsigned long cpuflags;
958 spin_lock_irqsave(&tty->read_lock, cpuflags);
959 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
960 N_TTY_BUF_SIZE - tty->read_head);
962 memcpy(tty->read_buf + tty->read_head, cp, i);
963 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
968 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
969 N_TTY_BUF_SIZE - tty->read_head);
971 memcpy(tty->read_buf + tty->read_head, cp, i);
972 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
974 spin_unlock_irqrestore(&tty->read_lock, cpuflags);
976 for (i = count, p = cp, f = fp; i; i--, p++) {
981 n_tty_receive_char(tty, *p);
984 n_tty_receive_break(tty);
988 n_tty_receive_parity_error(tty, *p);
991 n_tty_receive_overrun(tty);
994 printk(KERN_ERR "%s: unknown flag %d\n",
995 tty_name(tty, buf), flags);
999 if (tty->ops->flush_chars)
1000 tty->ops->flush_chars(tty);
1003 n_tty_set_room(tty);
1005 if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
1006 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1007 if (waitqueue_active(&tty->read_wait))
1008 wake_up_interruptible(&tty->read_wait);
1012 * Check the remaining room for the input canonicalization
1013 * mode. We don't want to throttle the driver if we're in
1014 * canonical mode and don't have a newline yet!
1016 if (tty->receive_room < TTY_THRESHOLD_THROTTLE)
1020 int is_ignored(int sig)
1022 return (sigismember(¤t->blocked, sig) ||
1023 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
1027 * n_tty_set_termios - termios data changed
1029 * @old: previous data
1031 * Called by the tty layer when the user changes termios flags so
1032 * that the line discipline can plan ahead. This function cannot sleep
1033 * and is protected from re-entry by the tty layer. The user is
1034 * guaranteed that this function will not be re-entered or in progress
1035 * when the ldisc is closed.
1037 * Locking: Caller holds tty->termios_mutex
1040 static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1042 int canon_change = 1;
1046 canon_change = (old->c_lflag ^ tty->termios->c_lflag) & ICANON;
1048 memset(&tty->read_flags, 0, sizeof tty->read_flags);
1049 tty->canon_head = tty->read_tail;
1050 tty->canon_data = 0;
1054 if (canon_change && !L_ICANON(tty) && tty->read_cnt)
1055 wake_up_interruptible(&tty->read_wait);
1057 tty->icanon = (L_ICANON(tty) != 0);
1058 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
1061 n_tty_set_room(tty);
1064 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1065 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1066 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1068 memset(tty->process_char_map, 0, 256/8);
1070 if (I_IGNCR(tty) || I_ICRNL(tty))
1071 set_bit('\r', tty->process_char_map);
1073 set_bit('\n', tty->process_char_map);
1075 if (L_ICANON(tty)) {
1076 set_bit(ERASE_CHAR(tty), tty->process_char_map);
1077 set_bit(KILL_CHAR(tty), tty->process_char_map);
1078 set_bit(EOF_CHAR(tty), tty->process_char_map);
1079 set_bit('\n', tty->process_char_map);
1080 set_bit(EOL_CHAR(tty), tty->process_char_map);
1081 if (L_IEXTEN(tty)) {
1082 set_bit(WERASE_CHAR(tty),
1083 tty->process_char_map);
1084 set_bit(LNEXT_CHAR(tty),
1085 tty->process_char_map);
1086 set_bit(EOL2_CHAR(tty),
1087 tty->process_char_map);
1089 set_bit(REPRINT_CHAR(tty),
1090 tty->process_char_map);
1094 set_bit(START_CHAR(tty), tty->process_char_map);
1095 set_bit(STOP_CHAR(tty), tty->process_char_map);
1098 set_bit(INTR_CHAR(tty), tty->process_char_map);
1099 set_bit(QUIT_CHAR(tty), tty->process_char_map);
1100 set_bit(SUSP_CHAR(tty), tty->process_char_map);
1102 clear_bit(__DISABLED_CHAR, tty->process_char_map);
1107 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1108 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1109 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
1114 n_tty_set_room(tty);
1115 /* The termios change make the tty ready for I/O */
1116 wake_up_interruptible(&tty->write_wait);
1117 wake_up_interruptible(&tty->read_wait);
1121 * n_tty_close - close the ldisc for this tty
1124 * Called from the terminal layer when this line discipline is
1125 * being shut down, either because of a close or becsuse of a
1126 * discipline change. The function will not be called while other
1127 * ldisc methods are in progress.
1130 static void n_tty_close(struct tty_struct *tty)
1132 n_tty_flush_buffer(tty);
1133 if (tty->read_buf) {
1134 free_buf(tty->read_buf);
1135 tty->read_buf = NULL;
1140 * n_tty_open - open an ldisc
1141 * @tty: terminal to open
1143 * Called when this line discipline is being attached to the
1144 * terminal device. Can sleep. Called serialized so that no
1145 * other events will occur in parallel. No further open will occur
1149 static int n_tty_open(struct tty_struct *tty)
1154 /* This one is ugly. Currently a malloc failure here can panic */
1155 if (!tty->read_buf) {
1156 tty->read_buf = alloc_buf();
1160 memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
1161 reset_buffer_flags(tty);
1163 n_tty_set_termios(tty, NULL);
1164 tty->minimum_to_wake = 1;
1169 static inline int input_available_p(struct tty_struct *tty, int amt)
1172 if (tty->canon_data)
1174 } else if (tty->read_cnt >= (amt ? amt : 1))
1181 * copy_from_read_buf - copy read data directly
1182 * @tty: terminal device
1186 * Helper function to speed up n_tty_read. It is only called when
1187 * ICANON is off; it copies characters straight from the tty queue to
1188 * user space directly. It can be profitably called twice; once to
1189 * drain the space from the tail pointer to the (physical) end of the
1190 * buffer, and once to drain the space from the (physical) beginning of
1191 * the buffer to head pointer.
1193 * Called under the tty->atomic_read_lock sem
1197 static int copy_from_read_buf(struct tty_struct *tty,
1198 unsigned char __user **b,
1204 unsigned long flags;
1207 spin_lock_irqsave(&tty->read_lock, flags);
1208 n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
1210 spin_unlock_irqrestore(&tty->read_lock, flags);
1212 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
1214 tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n);
1215 spin_lock_irqsave(&tty->read_lock, flags);
1216 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
1218 spin_unlock_irqrestore(&tty->read_lock, flags);
1225 extern ssize_t redirected_tty_write(struct file *, const char __user *,
1229 * job_control - check job control
1231 * @file: file handle
1233 * Perform job control management checks on this file/tty descriptor
1234 * and if appropriate send any needed signals and return a negative
1235 * error code if action should be taken.
1238 * Locking: None - redirected write test is safe, testing
1239 * current->signal should possibly lock current->sighand
1243 static int job_control(struct tty_struct *tty, struct file *file)
1245 /* Job control check -- must be done at start and after
1246 every sleep (POSIX.1 7.1.1.4). */
1247 /* NOTE: not yet done after every sleep pending a thorough
1248 check of the logic of this change. -- jlc */
1249 /* don't stop on /dev/console */
1250 if (file->f_op->write != redirected_tty_write &&
1251 current->signal->tty == tty) {
1253 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
1254 else if (task_pgrp(current) != tty->pgrp) {
1255 if (is_ignored(SIGTTIN) ||
1256 is_current_pgrp_orphaned())
1258 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1259 set_thread_flag(TIF_SIGPENDING);
1260 return -ERESTARTSYS;
1268 * n_tty_read - read function for tty
1270 * @file: file object
1271 * @buf: userspace buffer pointer
1274 * Perform reads for the line discipline. We are guaranteed that the
1275 * line discipline will not be closed under us but we may get multiple
1276 * parallel readers and must handle this ourselves. We may also get
1277 * a hangup. Always called in user context, may sleep.
1279 * This code must be sure never to sleep through a hangup.
1282 static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
1283 unsigned char __user *buf, size_t nr)
1285 unsigned char __user *b = buf;
1286 DECLARE_WAITQUEUE(wait, current);
1292 unsigned long flags;
1297 BUG_ON(!tty->read_buf);
1299 c = job_control(tty, file);
1304 timeout = MAX_SCHEDULE_TIMEOUT;
1306 time = (HZ / 10) * TIME_CHAR(tty);
1307 minimum = MIN_CHAR(tty);
1310 tty->minimum_to_wake = 1;
1311 else if (!waitqueue_active(&tty->read_wait) ||
1312 (tty->minimum_to_wake > minimum))
1313 tty->minimum_to_wake = minimum;
1320 tty->minimum_to_wake = minimum = 1;
1325 * Internal serialization of reads.
1327 if (file->f_flags & O_NONBLOCK) {
1328 if (!mutex_trylock(&tty->atomic_read_lock))
1331 if (mutex_lock_interruptible(&tty->atomic_read_lock))
1332 return -ERESTARTSYS;
1334 packet = tty->packet;
1336 add_wait_queue(&tty->read_wait, &wait);
1338 /* First test for status change. */
1339 if (packet && tty->link->ctrl_status) {
1343 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
1344 cs = tty->link->ctrl_status;
1345 tty->link->ctrl_status = 0;
1346 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
1347 if (tty_put_user(tty, cs, b++)) {
1355 /* This statement must be first before checking for input
1356 so that any interrupt will set the state back to
1358 set_current_state(TASK_INTERRUPTIBLE);
1360 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1361 ((minimum - (b - buf)) >= 1))
1362 tty->minimum_to_wake = (minimum - (b - buf));
1364 if (!input_available_p(tty, 0)) {
1365 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1369 if (tty_hung_up_p(file))
1373 if (file->f_flags & O_NONBLOCK) {
1377 if (signal_pending(current)) {
1378 retval = -ERESTARTSYS;
1381 /* FIXME: does n_tty_set_room need locking ? */
1382 n_tty_set_room(tty);
1383 timeout = schedule_timeout(timeout);
1386 __set_current_state(TASK_RUNNING);
1388 /* Deal with packet mode. */
1389 if (packet && b == buf) {
1390 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
1399 /* N.B. avoid overrun if nr == 0 */
1400 while (nr && tty->read_cnt) {
1403 eol = test_and_clear_bit(tty->read_tail,
1405 c = tty->read_buf[tty->read_tail];
1406 spin_lock_irqsave(&tty->read_lock, flags);
1407 tty->read_tail = ((tty->read_tail+1) &
1408 (N_TTY_BUF_SIZE-1));
1411 /* this test should be redundant:
1412 * we shouldn't be reading data if
1415 if (--tty->canon_data < 0)
1416 tty->canon_data = 0;
1418 spin_unlock_irqrestore(&tty->read_lock, flags);
1420 if (!eol || (c != __DISABLED_CHAR)) {
1421 if (tty_put_user(tty, c, b++)) {
1429 tty_audit_push(tty);
1437 /* The copy function takes the read lock and handles
1438 locking internally for this case */
1439 uncopied = copy_from_read_buf(tty, &b, &nr);
1440 uncopied += copy_from_read_buf(tty, &b, &nr);
1447 /* If there is enough space in the read buffer now, let the
1448 * low-level driver know. We use n_tty_chars_in_buffer() to
1449 * check the buffer, as it now knows about canonical mode.
1450 * Otherwise, if the driver is throttled and the line is
1451 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1452 * we won't get any more characters.
1454 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) {
1455 n_tty_set_room(tty);
1456 check_unthrottle(tty);
1459 if (b - buf >= minimum)
1464 mutex_unlock(&tty->atomic_read_lock);
1465 remove_wait_queue(&tty->read_wait, &wait);
1467 if (!waitqueue_active(&tty->read_wait))
1468 tty->minimum_to_wake = minimum;
1470 __set_current_state(TASK_RUNNING);
1475 clear_bit(TTY_PUSH, &tty->flags);
1476 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1479 n_tty_set_room(tty);
1484 * n_tty_write - write function for tty
1486 * @file: file object
1487 * @buf: userspace buffer pointer
1490 * Write function of the terminal device. This is serialized with
1491 * respect to other write callers but not to termios changes, reads
1492 * and other such events. We must be careful with N_TTY as the receive
1493 * code will echo characters, thus calling driver write methods.
1495 * This code must be sure never to sleep through a hangup.
1498 static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
1499 const unsigned char *buf, size_t nr)
1501 const unsigned char *b = buf;
1502 DECLARE_WAITQUEUE(wait, current);
1506 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1507 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
1508 retval = tty_check_change(tty);
1513 add_wait_queue(&tty->write_wait, &wait);
1515 set_current_state(TASK_INTERRUPTIBLE);
1516 if (signal_pending(current)) {
1517 retval = -ERESTARTSYS;
1520 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1524 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1526 ssize_t num = opost_block(tty, b, nr);
1538 if (opost(c, tty) < 0)
1542 if (tty->ops->flush_chars)
1543 tty->ops->flush_chars(tty);
1546 c = tty->ops->write(tty, b, nr);
1559 if (file->f_flags & O_NONBLOCK) {
1566 __set_current_state(TASK_RUNNING);
1567 remove_wait_queue(&tty->write_wait, &wait);
1568 return (b - buf) ? b - buf : retval;
1572 * n_tty_poll - poll method for N_TTY
1573 * @tty: terminal device
1574 * @file: file accessing it
1577 * Called when the line discipline is asked to poll() for data or
1578 * for special events. This code is not serialized with respect to
1579 * other events save open/close.
1581 * This code must be sure never to sleep through a hangup.
1582 * Called without the kernel lock held - fine
1585 static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
1588 unsigned int mask = 0;
1590 poll_wait(file, &tty->read_wait, wait);
1591 poll_wait(file, &tty->write_wait, wait);
1592 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
1593 mask |= POLLIN | POLLRDNORM;
1594 if (tty->packet && tty->link->ctrl_status)
1595 mask |= POLLPRI | POLLIN | POLLRDNORM;
1596 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
1598 if (tty_hung_up_p(file))
1600 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
1601 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
1602 tty->minimum_to_wake = MIN_CHAR(tty);
1604 tty->minimum_to_wake = 1;
1606 if (tty->ops->write && !tty_is_writelocked(tty) &&
1607 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
1608 tty_write_room(tty) > 0)
1609 mask |= POLLOUT | POLLWRNORM;
1613 static unsigned long inq_canon(struct tty_struct *tty)
1617 if (!tty->canon_data)
1619 head = tty->canon_head;
1620 tail = tty->read_tail;
1621 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
1622 /* Skip EOF-chars.. */
1623 while (head != tail) {
1624 if (test_bit(tail, tty->read_flags) &&
1625 tty->read_buf[tail] == __DISABLED_CHAR)
1627 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1632 static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
1633 unsigned int cmd, unsigned long arg)
1639 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
1641 /* FIXME: Locking */
1642 retval = tty->read_cnt;
1644 retval = inq_canon(tty);
1645 return put_user(retval, (unsigned int __user *) arg);
1647 return n_tty_ioctl_helper(tty, file, cmd, arg);
1651 struct tty_ldisc_ops tty_ldisc_N_TTY = {
1652 .magic = TTY_LDISC_MAGIC,
1655 .close = n_tty_close,
1656 .flush_buffer = n_tty_flush_buffer,
1657 .chars_in_buffer = n_tty_chars_in_buffer,
1659 .write = n_tty_write,
1660 .ioctl = n_tty_ioctl,
1661 .set_termios = n_tty_set_termios,
1663 .receive_buf = n_tty_receive_buf,
1664 .write_wakeup = n_tty_write_wakeup