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>
48 #include <asm/uaccess.h>
49 #include <asm/system.h>
50 #include <asm/bitops.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 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 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, 0, " ", 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 __user * inbuf, unsigned int nr)
316 space = tty->driver->write_room(tty);
321 if (nr > sizeof(buf))
324 if (copy_from_user(buf, inbuf, nr))
327 for (i = 0, cp = buf; i < nr; i++, cp++) {
334 tty->canon_column = tty->column;
337 if (O_ONOCR(tty) && tty->column == 0)
342 tty->canon_column = tty->column = 0;
345 tty->canon_column = tty->column = 0;
362 if (tty->driver->flush_chars)
363 tty->driver->flush_chars(tty);
364 i = tty->driver->write(tty, 0, buf, i);
370 * put_char - write character to driver
371 * @c: character (or part of unicode symbol)
372 * @tty: terminal device
374 * Queue a byte to the driver layer for output
377 static inline void put_char(unsigned char c, struct tty_struct *tty)
379 tty->driver->put_char(tty, c);
383 * echo_char - echo characters
384 * @c: unicode byte to echo
385 * @tty: terminal device
387 * Echo user input back onto the screen. This must be called only when
388 * L_ECHO(tty) is true. Called from the driver receive_buf path.
391 static void echo_char(unsigned char c, struct tty_struct *tty)
393 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
395 put_char(c ^ 0100, tty);
401 static inline void finish_erasing(struct tty_struct *tty)
411 * eraser - handle erase function
412 * @c: character input
413 * @tty: terminal device
415 * Perform erase and neccessary output when an erase character is
416 * present in the stream from the driver layer. Handles the complexities
417 * of UTF-8 multibyte symbols.
420 static void eraser(unsigned char c, struct tty_struct *tty)
422 enum { ERASE, WERASE, KILL } kill_type;
423 int head, seen_alnums, cnt;
426 if (tty->read_head == tty->canon_head) {
427 /* opost('\a', tty); */ /* what do you think? */
430 if (c == ERASE_CHAR(tty))
432 else if (c == WERASE_CHAR(tty))
436 spin_lock_irqsave(&tty->read_lock, flags);
437 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
438 (N_TTY_BUF_SIZE - 1));
439 tty->read_head = tty->canon_head;
440 spin_unlock_irqrestore(&tty->read_lock, flags);
443 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
444 spin_lock_irqsave(&tty->read_lock, flags);
445 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
446 (N_TTY_BUF_SIZE - 1));
447 tty->read_head = tty->canon_head;
448 spin_unlock_irqrestore(&tty->read_lock, flags);
450 echo_char(KILL_CHAR(tty), tty);
451 /* Add a newline if ECHOK is on and ECHOKE is off. */
460 while (tty->read_head != tty->canon_head) {
461 head = tty->read_head;
463 /* erase a single possibly multibyte character */
465 head = (head - 1) & (N_TTY_BUF_SIZE-1);
466 c = tty->read_buf[head];
467 } while (is_continuation(c, tty) && head != tty->canon_head);
469 /* do not partially erase */
470 if (is_continuation(c, tty))
473 if (kill_type == WERASE) {
474 /* Equivalent to BSD's ALTWERASE. */
475 if (isalnum(c) || c == '_')
477 else if (seen_alnums)
480 cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
481 spin_lock_irqsave(&tty->read_lock, flags);
482 tty->read_head = head;
483 tty->read_cnt -= cnt;
484 spin_unlock_irqrestore(&tty->read_lock, flags);
486 if (L_ECHOPRT(tty)) {
492 /* if cnt > 1, output a multi-byte character */
495 head = (head+1) & (N_TTY_BUF_SIZE-1);
496 put_char(tty->read_buf[head], tty);
498 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
499 echo_char(ERASE_CHAR(tty), tty);
500 } else if (c == '\t') {
501 unsigned int col = tty->canon_column;
502 unsigned long tail = tty->canon_head;
504 /* Find the column of the last char. */
505 while (tail != tty->read_head) {
506 c = tty->read_buf[tail];
509 else if (iscntrl(c)) {
512 } else if (!is_continuation(c, tty))
514 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
517 /* should never happen */
518 if (tty->column > 0x80000000)
521 /* Now backup to that column. */
522 while (tty->column > col) {
523 /* Can't use opost here. */
529 if (iscntrl(c) && L_ECHOCTL(tty)) {
536 if (!iscntrl(c) || L_ECHOCTL(tty)) {
545 if (kill_type == ERASE)
548 if (tty->read_head == tty->canon_head)
553 * isig - handle the ISIG optio
556 * @flush: force flush
558 * Called when a signal is being sent due to terminal input. This
559 * may caus terminal flushing to take place according to the termios
560 * settings and character used. Called from the driver receive_buf
561 * path so serialized.
564 static inline void isig(int sig, struct tty_struct *tty, int flush)
567 kill_pg(tty->pgrp, sig, 1);
568 if (flush || !L_NOFLSH(tty)) {
569 n_tty_flush_buffer(tty);
570 if (tty->driver->flush_buffer)
571 tty->driver->flush_buffer(tty);
576 * n_tty_receive_break - handle break
579 * An RS232 break event has been hit in the incoming bitstream. This
580 * can cause a variety of events depending upon the termios settings.
582 * Called from the receive_buf path so single threaded.
585 static inline void n_tty_receive_break(struct tty_struct *tty)
590 isig(SIGINT, tty, 1);
594 put_tty_queue('\377', tty);
595 put_tty_queue('\0', tty);
597 put_tty_queue('\0', tty);
598 wake_up_interruptible(&tty->read_wait);
602 * n_tty_receive_overrun - handle overrun reporting
605 * Data arrived faster than we could process it. While the tty
606 * driver has flagged this the bits that were missed are gone
609 * Called from the receive_buf path so single threaded. Does not
610 * need locking as num_overrun and overrun_time are function
614 static inline void n_tty_receive_overrun(struct tty_struct *tty)
619 if (time_before(tty->overrun_time, jiffies - HZ)) {
620 printk(KERN_WARNING "%s: %d input overrun(s)\n", tty_name(tty, buf),
622 tty->overrun_time = jiffies;
623 tty->num_overrun = 0;
628 * n_tty_receive_parity_error - error notifier
629 * @tty: terminal device
632 * Process a parity error and queue the right data to indicate
633 * the error case if neccessary. Locking as per n_tty_receive_buf.
635 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
642 put_tty_queue('\377', tty);
643 put_tty_queue('\0', tty);
644 put_tty_queue(c, tty);
645 } else if (I_INPCK(tty))
646 put_tty_queue('\0', tty);
648 put_tty_queue(c, tty);
649 wake_up_interruptible(&tty->read_wait);
653 * n_tty_receive_char - perform processing
654 * @tty: terminal device
657 * Process an individual character of input received from the driver.
658 * This is serialized with respect to itself by the rules for the
662 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
667 put_tty_queue(c, tty);
671 if (tty->stopped && !tty->flow_stopped &&
672 I_IXON(tty) && I_IXANY(tty)) {
679 if (I_IUCLC(tty) && L_IEXTEN(tty))
684 if (c == START_CHAR(tty))
686 else if (c == STOP_CHAR(tty))
693 * If the previous character was LNEXT, or we know that this
694 * character is not one of the characters that we'll have to
695 * handle specially, do shortcut processing to speed things
698 if (!test_bit(c, tty->process_char_map) || tty->lnext) {
702 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
703 put_char('\a', tty); /* beep if no space */
706 /* Record the column of first canon char. */
707 if (tty->canon_head == tty->read_head)
708 tty->canon_column = tty->column;
711 if (I_PARMRK(tty) && c == (unsigned char) '\377')
712 put_tty_queue(c, tty);
713 put_tty_queue(c, tty);
722 } else if (c == '\n' && I_INLCR(tty))
725 if (c == START_CHAR(tty)) {
729 if (c == STOP_CHAR(tty)) {
737 if (c == INTR_CHAR(tty))
740 if (c == QUIT_CHAR(tty))
743 if (c == SUSP_CHAR(tty)) {
745 isig(signal, tty, 0);
750 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
751 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
755 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
759 if (L_ECHOCTL(tty)) {
766 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
768 unsigned long tail = tty->canon_head;
773 while (tail != tty->read_head) {
774 echo_char(tty->read_buf[tail], tty);
775 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
780 if (L_ECHO(tty) || L_ECHONL(tty)) {
781 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
789 if (c == EOF_CHAR(tty)) {
790 if (tty->canon_head != tty->read_head)
791 set_bit(TTY_PUSH, &tty->flags);
795 if ((c == EOL_CHAR(tty)) ||
796 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
798 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
801 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
805 /* Record the column of first canon char. */
806 if (tty->canon_head == tty->read_head)
807 tty->canon_column = tty->column;
811 * XXX does PARMRK doubling happen for
812 * EOL_CHAR and EOL2_CHAR?
814 if (I_PARMRK(tty) && c == (unsigned char) '\377')
815 put_tty_queue(c, tty);
818 spin_lock_irqsave(&tty->read_lock, flags);
819 set_bit(tty->read_head, tty->read_flags);
820 put_tty_queue_nolock(c, tty);
821 tty->canon_head = tty->read_head;
823 spin_unlock_irqrestore(&tty->read_lock, flags);
824 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
825 if (waitqueue_active(&tty->read_wait))
826 wake_up_interruptible(&tty->read_wait);
833 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
834 put_char('\a', tty); /* beep if no space */
840 /* Record the column of first canon char. */
841 if (tty->canon_head == tty->read_head)
842 tty->canon_column = tty->column;
847 if (I_PARMRK(tty) && c == (unsigned char) '\377')
848 put_tty_queue(c, tty);
850 put_tty_queue(c, tty);
854 * n_tty_receive_room - receive space
857 * Called by the driver to find out how much data it is
858 * permitted to feed to the line discipline without any being lost
859 * and thus to manage flow control. Not serialized. Answers for the
863 static int n_tty_receive_room(struct tty_struct *tty)
865 int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
868 * If we are doing input canonicalization, and there are no
869 * pending newlines, let characters through without limit, so
870 * that erase characters will be handled. Other excess
871 * characters will be beeped.
873 if (tty->icanon && !tty->canon_data)
874 return N_TTY_BUF_SIZE;
882 * n_tty_write_wakeup - asynchronous I/O notifier
885 * Required for the ptys, serial driver etc. since processes
886 * that attach themselves to the master and rely on ASYNC
887 * IO must be woken up
890 static void n_tty_write_wakeup(struct tty_struct *tty)
894 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
895 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
901 * n_tty_receive_buf - data receive
902 * @tty: terminal device
907 * Called by the terminal driver when a block of characters has
908 * been received. This function must be called from soft contexts
909 * not from interrupt context. The driver is responsible for making
910 * calls one at a time and in order (or using flush_to_ldisc)
913 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
916 const unsigned char *p;
917 char *f, flags = TTY_NORMAL;
920 unsigned long cpuflags;
926 spin_lock_irqsave(&tty->read_lock, cpuflags);
927 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
928 N_TTY_BUF_SIZE - tty->read_head);
930 memcpy(tty->read_buf + tty->read_head, cp, i);
931 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
936 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
937 N_TTY_BUF_SIZE - tty->read_head);
939 memcpy(tty->read_buf + tty->read_head, cp, i);
940 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
942 spin_unlock_irqrestore(&tty->read_lock, cpuflags);
944 for (i=count, p = cp, f = fp; i; i--, p++) {
949 n_tty_receive_char(tty, *p);
952 n_tty_receive_break(tty);
956 n_tty_receive_parity_error(tty, *p);
959 n_tty_receive_overrun(tty);
962 printk("%s: unknown flag %d\n",
963 tty_name(tty, buf), flags);
967 if (tty->driver->flush_chars)
968 tty->driver->flush_chars(tty);
971 if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
972 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
973 if (waitqueue_active(&tty->read_wait))
974 wake_up_interruptible(&tty->read_wait);
978 * Check the remaining room for the input canonicalization
979 * mode. We don't want to throttle the driver if we're in
980 * canonical mode and don't have a newline yet!
982 if (n_tty_receive_room(tty) < TTY_THRESHOLD_THROTTLE) {
983 /* check TTY_THROTTLED first so it indicates our state */
984 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
985 tty->driver->throttle)
986 tty->driver->throttle(tty);
990 int is_ignored(int sig)
992 return (sigismember(¤t->blocked, sig) ||
993 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
997 * n_tty_set_termios - termios data changed
999 * @old: previous data
1001 * Called by the tty layer when the user changes termios flags so
1002 * that the line discipline can plan ahead. This function cannot sleep
1003 * and is protected from re-entry by the tty layer. The user is
1004 * guaranteed that this function will not be re-entered or in progress
1005 * when the ldisc is closed.
1008 static void n_tty_set_termios(struct tty_struct *tty, struct termios * old)
1013 tty->icanon = (L_ICANON(tty) != 0);
1014 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
1019 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1020 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1021 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1023 memset(tty->process_char_map, 0, 256/8);
1025 if (I_IGNCR(tty) || I_ICRNL(tty))
1026 set_bit('\r', tty->process_char_map);
1028 set_bit('\n', tty->process_char_map);
1030 if (L_ICANON(tty)) {
1031 set_bit(ERASE_CHAR(tty), tty->process_char_map);
1032 set_bit(KILL_CHAR(tty), tty->process_char_map);
1033 set_bit(EOF_CHAR(tty), tty->process_char_map);
1034 set_bit('\n', tty->process_char_map);
1035 set_bit(EOL_CHAR(tty), tty->process_char_map);
1036 if (L_IEXTEN(tty)) {
1037 set_bit(WERASE_CHAR(tty),
1038 tty->process_char_map);
1039 set_bit(LNEXT_CHAR(tty),
1040 tty->process_char_map);
1041 set_bit(EOL2_CHAR(tty),
1042 tty->process_char_map);
1044 set_bit(REPRINT_CHAR(tty),
1045 tty->process_char_map);
1049 set_bit(START_CHAR(tty), tty->process_char_map);
1050 set_bit(STOP_CHAR(tty), tty->process_char_map);
1053 set_bit(INTR_CHAR(tty), tty->process_char_map);
1054 set_bit(QUIT_CHAR(tty), tty->process_char_map);
1055 set_bit(SUSP_CHAR(tty), tty->process_char_map);
1057 clear_bit(__DISABLED_CHAR, tty->process_char_map);
1062 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1063 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1064 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
1072 * n_tty_close - close the ldisc for this tty
1075 * Called from the terminal layer when this line discipline is
1076 * being shut down, either because of a close or becsuse of a
1077 * discipline change. The function will not be called while other
1078 * ldisc methods are in progress.
1081 static void n_tty_close(struct tty_struct *tty)
1083 n_tty_flush_buffer(tty);
1084 if (tty->read_buf) {
1085 free_buf(tty->read_buf);
1086 tty->read_buf = NULL;
1091 * n_tty_open - open an ldisc
1092 * @tty: terminal to open
1094 * Called when this line discipline is being attached to the
1095 * terminal device. Can sleep. Called serialized so that no
1096 * other events will occur in parallel. No further open will occur
1100 static int n_tty_open(struct tty_struct *tty)
1105 /* This one is ugly. Currently a malloc failure here can panic */
1106 if (!tty->read_buf) {
1107 tty->read_buf = alloc_buf();
1111 memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
1112 reset_buffer_flags(tty);
1114 n_tty_set_termios(tty, NULL);
1115 tty->minimum_to_wake = 1;
1120 static inline int input_available_p(struct tty_struct *tty, int amt)
1123 if (tty->canon_data)
1125 } else if (tty->read_cnt >= (amt ? amt : 1))
1132 * copy_from_read_buf - copy read data directly
1133 * @tty: terminal device
1137 * Helper function to speed up read_chan. It is only called when
1138 * ICANON is off; it copies characters straight from the tty queue to
1139 * user space directly. It can be profitably called twice; once to
1140 * drain the space from the tail pointer to the (physical) end of the
1141 * buffer, and once to drain the space from the (physical) beginning of
1142 * the buffer to head pointer.
1144 * Called under the tty->atomic_read sem and with TTY_DONT_FLIP set
1148 static inline int copy_from_read_buf(struct tty_struct *tty,
1149 unsigned char __user **b,
1155 unsigned long flags;
1158 spin_lock_irqsave(&tty->read_lock, flags);
1159 n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
1160 n = min((ssize_t)*nr, n);
1161 spin_unlock_irqrestore(&tty->read_lock, flags);
1164 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
1166 spin_lock_irqsave(&tty->read_lock, flags);
1167 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
1169 spin_unlock_irqrestore(&tty->read_lock, flags);
1176 extern ssize_t redirected_tty_write(struct file *,const char *,size_t,loff_t *);
1179 * job_control - check job control
1181 * @file: file handle
1183 * Perform job control management checks on this file/tty descriptor
1184 * and if appropriate send any needed signals and return a negative
1185 * error code if action should be taken.
1188 static int job_control(struct tty_struct *tty, struct file *file)
1190 /* Job control check -- must be done at start and after
1191 every sleep (POSIX.1 7.1.1.4). */
1192 /* NOTE: not yet done after every sleep pending a thorough
1193 check of the logic of this change. -- jlc */
1194 /* don't stop on /dev/console */
1195 if (file->f_op->write != redirected_tty_write &&
1196 current->signal->tty == tty) {
1198 printk("read_chan: tty->pgrp <= 0!\n");
1199 else if (process_group(current) != tty->pgrp) {
1200 if (is_ignored(SIGTTIN) ||
1201 is_orphaned_pgrp(process_group(current)))
1203 kill_pg(process_group(current), SIGTTIN, 1);
1204 return -ERESTARTSYS;
1212 * read_chan - read function for tty
1214 * @file: file object
1215 * @buf: userspace buffer pointer
1218 * Perform reads for the line discipline. We are guaranteed that the
1219 * line discipline will not be closed under us but we may get multiple
1220 * parallel readers and must handle this ourselves. We may also get
1221 * a hangup. Always called in user context, may sleep.
1223 * This code must be sure never to sleep through a hangup.
1226 static ssize_t read_chan(struct tty_struct *tty, struct file *file,
1227 unsigned char __user *buf, size_t nr)
1229 unsigned char __user *b = buf;
1230 DECLARE_WAITQUEUE(wait, current);
1236 unsigned long flags;
1240 if (!tty->read_buf) {
1241 printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
1245 c = job_control(tty, file);
1250 timeout = MAX_SCHEDULE_TIMEOUT;
1252 time = (HZ / 10) * TIME_CHAR(tty);
1253 minimum = MIN_CHAR(tty);
1256 tty->minimum_to_wake = 1;
1257 else if (!waitqueue_active(&tty->read_wait) ||
1258 (tty->minimum_to_wake > minimum))
1259 tty->minimum_to_wake = minimum;
1266 tty->minimum_to_wake = minimum = 1;
1271 * Internal serialization of reads.
1273 if (file->f_flags & O_NONBLOCK) {
1274 if (down_trylock(&tty->atomic_read))
1278 if (down_interruptible(&tty->atomic_read))
1279 return -ERESTARTSYS;
1282 add_wait_queue(&tty->read_wait, &wait);
1283 set_bit(TTY_DONT_FLIP, &tty->flags);
1285 /* First test for status change. */
1286 if (tty->packet && tty->link->ctrl_status) {
1290 cs = tty->link->ctrl_status;
1291 tty->link->ctrl_status = 0;
1292 if (put_user(cs, b++)) {
1300 /* This statement must be first before checking for input
1301 so that any interrupt will set the state back to
1303 set_current_state(TASK_INTERRUPTIBLE);
1305 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1306 ((minimum - (b - buf)) >= 1))
1307 tty->minimum_to_wake = (minimum - (b - buf));
1309 if (!input_available_p(tty, 0)) {
1310 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1314 if (tty_hung_up_p(file))
1318 if (file->f_flags & O_NONBLOCK) {
1322 if (signal_pending(current)) {
1323 retval = -ERESTARTSYS;
1326 clear_bit(TTY_DONT_FLIP, &tty->flags);
1327 timeout = schedule_timeout(timeout);
1328 set_bit(TTY_DONT_FLIP, &tty->flags);
1331 __set_current_state(TASK_RUNNING);
1333 /* Deal with packet mode. */
1334 if (tty->packet && b == buf) {
1335 if (put_user(TIOCPKT_DATA, b++)) {
1344 /* N.B. avoid overrun if nr == 0 */
1345 while (nr && tty->read_cnt) {
1348 eol = test_and_clear_bit(tty->read_tail,
1350 c = tty->read_buf[tty->read_tail];
1351 spin_lock_irqsave(&tty->read_lock, flags);
1352 tty->read_tail = ((tty->read_tail+1) &
1353 (N_TTY_BUF_SIZE-1));
1356 /* this test should be redundant:
1357 * we shouldn't be reading data if
1360 if (--tty->canon_data < 0)
1361 tty->canon_data = 0;
1363 spin_unlock_irqrestore(&tty->read_lock, flags);
1365 if (!eol || (c != __DISABLED_CHAR)) {
1366 if (put_user(c, b++)) {
1380 uncopied = copy_from_read_buf(tty, &b, &nr);
1381 uncopied += copy_from_read_buf(tty, &b, &nr);
1388 /* If there is enough space in the read buffer now, let the
1389 * low-level driver know. We use n_tty_chars_in_buffer() to
1390 * check the buffer, as it now knows about canonical mode.
1391 * Otherwise, if the driver is throttled and the line is
1392 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1393 * we won't get any more characters.
1395 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE)
1396 check_unthrottle(tty);
1398 if (b - buf >= minimum)
1403 clear_bit(TTY_DONT_FLIP, &tty->flags);
1404 up(&tty->atomic_read);
1405 remove_wait_queue(&tty->read_wait, &wait);
1407 if (!waitqueue_active(&tty->read_wait))
1408 tty->minimum_to_wake = minimum;
1410 __set_current_state(TASK_RUNNING);
1415 clear_bit(TTY_PUSH, &tty->flags);
1416 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1423 * write_chan - write function for tty
1425 * @file: file object
1426 * @buf: userspace buffer pointer
1429 * Write function of the terminal device. This is serialized with
1430 * respect to other write callers but not to termios changes, reads
1431 * and other such events. We must be careful with N_TTY as the receive
1432 * code will echo characters, thus calling driver write methods.
1434 * This code must be sure never to sleep through a hangup.
1437 static ssize_t write_chan(struct tty_struct * tty, struct file * file,
1438 const unsigned char __user * buf, size_t nr)
1440 const unsigned char __user *b = buf;
1441 DECLARE_WAITQUEUE(wait, current);
1445 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1446 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
1447 retval = tty_check_change(tty);
1452 add_wait_queue(&tty->write_wait, &wait);
1454 set_current_state(TASK_INTERRUPTIBLE);
1455 if (signal_pending(current)) {
1456 retval = -ERESTARTSYS;
1459 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1463 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1465 ssize_t num = opost_block(tty, b, nr);
1477 if (opost(c, tty) < 0)
1481 if (tty->driver->flush_chars)
1482 tty->driver->flush_chars(tty);
1484 c = tty->driver->write(tty, 1, b, nr);
1494 if (file->f_flags & O_NONBLOCK) {
1501 __set_current_state(TASK_RUNNING);
1502 remove_wait_queue(&tty->write_wait, &wait);
1503 return (b - buf) ? b - buf : retval;
1507 * normal_poll - poll method for N_TTY
1508 * @tty: terminal device
1509 * @file: file accessing it
1512 * Called when the line discipline is asked to poll() for data or
1513 * for special events. This code is not serialized with respect to
1514 * other events save open/close.
1516 * This code must be sure never to sleep through a hangup.
1517 * Called without the kernel lock held - fine
1519 * FIXME: if someone changes the VMIN or discipline settings for the
1520 * terminal while another process is in poll() the poll does not
1521 * recompute the new limits. Possibly set_termios should issue
1522 * a read wakeup to fix this bug.
1525 static unsigned int normal_poll(struct tty_struct * tty, struct file * file, poll_table *wait)
1527 unsigned int mask = 0;
1529 poll_wait(file, &tty->read_wait, wait);
1530 poll_wait(file, &tty->write_wait, wait);
1531 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
1532 mask |= POLLIN | POLLRDNORM;
1533 if (tty->packet && tty->link->ctrl_status)
1534 mask |= POLLPRI | POLLIN | POLLRDNORM;
1535 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
1537 if (tty_hung_up_p(file))
1539 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
1540 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
1541 tty->minimum_to_wake = MIN_CHAR(tty);
1543 tty->minimum_to_wake = 1;
1545 if (tty->driver->chars_in_buffer(tty) < WAKEUP_CHARS &&
1546 tty->driver->write_room(tty) > 0)
1547 mask |= POLLOUT | POLLWRNORM;
1551 struct tty_ldisc tty_ldisc_N_TTY = {
1552 TTY_LDISC_MAGIC, /* magic */
1556 n_tty_open, /* open */
1557 n_tty_close, /* close */
1558 n_tty_flush_buffer, /* flush_buffer */
1559 n_tty_chars_in_buffer, /* chars_in_buffer */
1560 read_chan, /* read */
1561 write_chan, /* write */
1562 n_tty_ioctl, /* ioctl */
1563 n_tty_set_termios, /* set_termios */
1564 normal_poll, /* poll */
1566 n_tty_receive_buf, /* receive_buf */
1567 n_tty_receive_room, /* receive_room */
1568 n_tty_write_wakeup /* write_wakeup */