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
25 * who actually finally proved there really was a race.
27 * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
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>
50 #include <linux/uaccess.h>
51 #include <linux/module.h>
52 #include <linux/ratelimit.h>
55 /* number of characters left in xmit buffer before select has we have room */
56 #define WAKEUP_CHARS 256
59 * This defines the low- and high-watermarks for throttling and
60 * unthrottling the TTY driver. These watermarks are used for
61 * controlling the space in the read buffer.
63 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
64 #define TTY_THRESHOLD_UNTHROTTLE 128
67 * Special byte codes used in the echo buffer to represent operations
68 * or special handling of characters. Bytes in the echo buffer that
69 * are not part of such special blocks are treated as normal character
72 #define ECHO_OP_START 0xff
73 #define ECHO_OP_MOVE_BACK_COL 0x80
74 #define ECHO_OP_SET_CANON_COL 0x81
75 #define ECHO_OP_ERASE_TAB 0x82
79 unsigned long overrun_time;
82 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
83 unsigned char echo_overrun:1;
85 DECLARE_BITMAP(process_char_map, 256);
86 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
93 unsigned char *echo_buf;
94 unsigned int echo_pos;
95 unsigned int echo_cnt;
98 unsigned long canon_head;
99 unsigned int canon_column;
101 struct mutex atomic_read_lock;
102 struct mutex output_lock;
103 struct mutex echo_lock;
104 raw_spinlock_t read_lock;
107 static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
108 unsigned char __user *ptr)
110 struct n_tty_data *ldata = tty->disc_data;
112 tty_audit_add_data(tty, &x, 1, ldata->icanon);
113 return put_user(x, ptr);
117 * n_tty_set__room - receive space
120 * Called by the driver to find out how much data it is
121 * permitted to feed to the line discipline without any being lost
122 * and thus to manage flow control. Not serialized. Answers for the
126 static void n_tty_set_room(struct tty_struct *tty)
128 struct n_tty_data *ldata = tty->disc_data;
132 /* ldata->read_cnt is not read locked ? */
134 /* Multiply read_cnt by 3, since each byte might take up to
135 * three times as many spaces when PARMRK is set (depending on
136 * its flags, e.g. parity error). */
137 left = N_TTY_BUF_SIZE - ldata->read_cnt * 3 - 1;
139 left = N_TTY_BUF_SIZE - ldata->read_cnt - 1;
142 * If we are doing input canonicalization, and there are no
143 * pending newlines, let characters through without limit, so
144 * that erase characters will be handled. Other excess
145 * characters will be beeped.
148 left = ldata->icanon && !ldata->canon_data;
149 old_left = tty->receive_room;
150 tty->receive_room = left;
152 /* Did this open up the receive buffer? We may need to flip */
153 if (left && !old_left) {
154 WARN_RATELIMIT(tty->port->itty == NULL,
155 "scheduling with invalid itty\n");
156 schedule_work(&tty->port->buf.work);
160 static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata)
162 if (ldata->read_cnt < N_TTY_BUF_SIZE) {
163 ldata->read_buf[ldata->read_head] = c;
164 ldata->read_head = (ldata->read_head + 1) & (N_TTY_BUF_SIZE-1);
170 * put_tty_queue - add character to tty
174 * Add a character to the tty read_buf queue. This is done under the
175 * read_lock to serialize character addition and also to protect us
176 * against parallel reads or flushes
179 static void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
183 * The problem of stomping on the buffers ends here.
184 * Why didn't anyone see this one coming? --AJK
186 raw_spin_lock_irqsave(&ldata->read_lock, flags);
187 put_tty_queue_nolock(c, ldata);
188 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
192 * check_unthrottle - allow new receive data
195 * Check whether to call the driver unthrottle functions
197 * Can sleep, may be called under the atomic_read_lock mutex but
198 * this is not guaranteed.
200 static void check_unthrottle(struct tty_struct *tty)
207 * reset_buffer_flags - reset buffer state
208 * @tty: terminal to reset
210 * Reset the read buffer counters, clear the flags,
211 * and make sure the driver is unthrottled. Called
212 * from n_tty_open() and n_tty_flush_buffer().
214 * Locking: tty_read_lock for read fields.
217 static void reset_buffer_flags(struct tty_struct *tty)
219 struct n_tty_data *ldata = tty->disc_data;
222 raw_spin_lock_irqsave(&ldata->read_lock, flags);
223 ldata->read_head = ldata->read_tail = ldata->read_cnt = 0;
224 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
226 mutex_lock(&ldata->echo_lock);
227 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
228 mutex_unlock(&ldata->echo_lock);
230 ldata->canon_head = ldata->canon_data = ldata->erasing = 0;
231 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
236 * n_tty_flush_buffer - clean input queue
237 * @tty: terminal device
239 * Flush the input buffer. Called when the line discipline is
240 * being closed, when the tty layer wants the buffer flushed (eg
241 * at hangup) or when the N_TTY line discipline internally has to
242 * clean the pending queue (for example some signals).
244 * Locking: ctrl_lock, read_lock.
247 static void n_tty_flush_buffer(struct tty_struct *tty)
250 /* clear everything and unthrottle the driver */
251 reset_buffer_flags(tty);
256 spin_lock_irqsave(&tty->ctrl_lock, flags);
257 if (tty->link->packet) {
258 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
259 wake_up_interruptible(&tty->link->read_wait);
261 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
265 * n_tty_chars_in_buffer - report available bytes
268 * Report the number of characters buffered to be delivered to user
269 * at this instant in time.
274 static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
276 struct n_tty_data *ldata = tty->disc_data;
280 raw_spin_lock_irqsave(&ldata->read_lock, flags);
281 if (!ldata->icanon) {
283 } else if (ldata->canon_data) {
284 n = (ldata->canon_head > ldata->read_tail) ?
285 ldata->canon_head - ldata->read_tail :
286 ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail);
288 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
293 * is_utf8_continuation - utf8 multibyte check
296 * Returns true if the utf8 character 'c' is a multibyte continuation
297 * character. We use this to correctly compute the on screen size
298 * of the character when printing
301 static inline int is_utf8_continuation(unsigned char c)
303 return (c & 0xc0) == 0x80;
307 * is_continuation - multibyte check
310 * Returns true if the utf8 character 'c' is a multibyte continuation
311 * character and the terminal is in unicode mode.
314 static inline int is_continuation(unsigned char c, struct tty_struct *tty)
316 return I_IUTF8(tty) && is_utf8_continuation(c);
320 * do_output_char - output one character
321 * @c: character (or partial unicode symbol)
322 * @tty: terminal device
323 * @space: space available in tty driver write buffer
325 * This is a helper function that handles one output character
326 * (including special characters like TAB, CR, LF, etc.),
327 * doing OPOST processing and putting the results in the
328 * tty driver's write buffer.
330 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
331 * and NLDLY. They simply aren't relevant in the world today.
332 * If you ever need them, add them here.
334 * Returns the number of bytes of buffer space used or -1 if
337 * Locking: should be called under the output_lock to protect
338 * the column state and space left in the buffer
341 static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
343 struct n_tty_data *ldata = tty->disc_data;
356 ldata->canon_column = ldata->column = 0;
357 tty->ops->write(tty, "\r\n", 2);
360 ldata->canon_column = ldata->column;
363 if (O_ONOCR(tty) && ldata->column == 0)
368 ldata->canon_column = ldata->column = 0;
371 ldata->canon_column = ldata->column = 0;
374 spaces = 8 - (ldata->column & 7);
375 if (O_TABDLY(tty) == XTABS) {
378 ldata->column += spaces;
379 tty->ops->write(tty, " ", spaces);
382 ldata->column += spaces;
385 if (ldata->column > 0)
392 if (!is_continuation(c, tty))
398 tty_put_char(tty, c);
403 * process_output - output post processor
404 * @c: character (or partial unicode symbol)
405 * @tty: terminal device
407 * Output one character with OPOST processing.
408 * Returns -1 when the output device is full and the character
411 * Locking: output_lock to protect column state and space left
412 * (also, this is called from n_tty_write under the
413 * tty layer write lock)
416 static int process_output(unsigned char c, struct tty_struct *tty)
418 struct n_tty_data *ldata = tty->disc_data;
421 mutex_lock(&ldata->output_lock);
423 space = tty_write_room(tty);
424 retval = do_output_char(c, tty, space);
426 mutex_unlock(&ldata->output_lock);
434 * process_output_block - block post processor
435 * @tty: terminal device
436 * @buf: character buffer
437 * @nr: number of bytes to output
439 * Output a block of characters with OPOST processing.
440 * Returns the number of characters output.
442 * This path is used to speed up block console writes, among other
443 * things when processing blocks of output data. It handles only
444 * the simple cases normally found and helps to generate blocks of
445 * symbols for the console driver and thus improve performance.
447 * Locking: output_lock to protect column state and space left
448 * (also, this is called from n_tty_write under the
449 * tty layer write lock)
452 static ssize_t process_output_block(struct tty_struct *tty,
453 const unsigned char *buf, unsigned int nr)
455 struct n_tty_data *ldata = tty->disc_data;
458 const unsigned char *cp;
460 mutex_lock(&ldata->output_lock);
462 space = tty_write_room(tty);
464 mutex_unlock(&ldata->output_lock);
470 for (i = 0, cp = buf; i < nr; i++, cp++) {
471 unsigned char c = *cp;
479 ldata->canon_column = ldata->column;
482 if (O_ONOCR(tty) && ldata->column == 0)
486 ldata->canon_column = ldata->column = 0;
491 if (ldata->column > 0)
498 if (!is_continuation(c, tty))
505 i = tty->ops->write(tty, buf, i);
507 mutex_unlock(&ldata->output_lock);
512 * process_echoes - write pending echo characters
513 * @tty: terminal device
515 * Write previously buffered echo (and other ldisc-generated)
516 * characters to the tty.
518 * Characters generated by the ldisc (including echoes) need to
519 * be buffered because the driver's write buffer can fill during
520 * heavy program output. Echoing straight to the driver will
521 * often fail under these conditions, causing lost characters and
522 * resulting mismatches of ldisc state information.
524 * Since the ldisc state must represent the characters actually sent
525 * to the driver at the time of the write, operations like certain
526 * changes in column state are also saved in the buffer and executed
529 * A circular fifo buffer is used so that the most recent characters
530 * are prioritized. Also, when control characters are echoed with a
531 * prefixed "^", the pair is treated atomically and thus not separated.
533 * Locking: output_lock to protect column state and space left,
534 * echo_lock to protect the echo buffer
537 static void process_echoes(struct tty_struct *tty)
539 struct n_tty_data *ldata = tty->disc_data;
542 unsigned char *cp, *buf_end;
544 if (!ldata->echo_cnt)
547 mutex_lock(&ldata->output_lock);
548 mutex_lock(&ldata->echo_lock);
550 space = tty_write_room(tty);
552 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE;
553 cp = ldata->echo_buf + ldata->echo_pos;
554 nr = ldata->echo_cnt;
557 if (c == ECHO_OP_START) {
560 int no_space_left = 0;
563 * If the buffer byte is the start of a multi-byte
564 * operation, get the next byte, which is either the
565 * op code or a control character value.
569 opp -= N_TTY_BUF_SIZE;
573 unsigned int num_chars, num_bs;
575 case ECHO_OP_ERASE_TAB:
576 if (++opp == buf_end)
577 opp -= N_TTY_BUF_SIZE;
581 * Determine how many columns to go back
582 * in order to erase the tab.
583 * This depends on the number of columns
584 * used by other characters within the tab
585 * area. If this (modulo 8) count is from
586 * the start of input rather than from a
587 * previous tab, we offset by canon column.
588 * Otherwise, tab spacing is normal.
590 if (!(num_chars & 0x80))
591 num_chars += ldata->canon_column;
592 num_bs = 8 - (num_chars & 7);
594 if (num_bs > space) {
600 tty_put_char(tty, '\b');
601 if (ldata->column > 0)
608 case ECHO_OP_SET_CANON_COL:
609 ldata->canon_column = ldata->column;
614 case ECHO_OP_MOVE_BACK_COL:
615 if (ldata->column > 0)
622 /* This is an escaped echo op start code */
627 tty_put_char(tty, ECHO_OP_START);
636 * If the op is not a special byte code,
637 * it is a ctrl char tagged to be echoed
638 * as "^X" (where X is the letter
639 * representing the control char).
640 * Note that we must ensure there is
641 * enough space for the whole ctrl pair.
648 tty_put_char(tty, '^');
649 tty_put_char(tty, op ^ 0100);
660 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
661 int retval = do_output_char(c, tty, space);
668 tty_put_char(tty, c);
675 /* When end of circular buffer reached, wrap around */
677 cp -= N_TTY_BUF_SIZE;
683 ldata->echo_overrun = 0;
685 int num_processed = ldata->echo_cnt - nr;
686 ldata->echo_pos += num_processed;
687 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
688 ldata->echo_cnt = nr;
689 if (num_processed > 0)
690 ldata->echo_overrun = 0;
693 mutex_unlock(&ldata->echo_lock);
694 mutex_unlock(&ldata->output_lock);
696 if (tty->ops->flush_chars)
697 tty->ops->flush_chars(tty);
701 * add_echo_byte - add a byte to the echo buffer
702 * @c: unicode byte to echo
705 * Add a character or operation byte to the echo buffer.
707 * Should be called under the echo lock to protect the echo buffer.
710 static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
714 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
715 /* Circular buffer is already at capacity */
716 new_byte_pos = ldata->echo_pos;
719 * Since the buffer start position needs to be advanced,
720 * be sure to step by a whole operation byte group.
722 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
723 if (ldata->echo_buf[(ldata->echo_pos + 1) &
724 (N_TTY_BUF_SIZE - 1)] ==
726 ldata->echo_pos += 3;
727 ldata->echo_cnt -= 2;
729 ldata->echo_pos += 2;
730 ldata->echo_cnt -= 1;
735 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
737 ldata->echo_overrun = 1;
739 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
740 new_byte_pos &= N_TTY_BUF_SIZE - 1;
744 ldata->echo_buf[new_byte_pos] = c;
748 * echo_move_back_col - add operation to move back a column
751 * Add an operation to the echo buffer to move back one column.
753 * Locking: echo_lock to protect the echo buffer
756 static void echo_move_back_col(struct n_tty_data *ldata)
758 mutex_lock(&ldata->echo_lock);
759 add_echo_byte(ECHO_OP_START, ldata);
760 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
761 mutex_unlock(&ldata->echo_lock);
765 * echo_set_canon_col - add operation to set the canon column
768 * Add an operation to the echo buffer to set the canon column
769 * to the current column.
771 * Locking: echo_lock to protect the echo buffer
774 static void echo_set_canon_col(struct n_tty_data *ldata)
776 mutex_lock(&ldata->echo_lock);
777 add_echo_byte(ECHO_OP_START, ldata);
778 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
779 mutex_unlock(&ldata->echo_lock);
783 * echo_erase_tab - add operation to erase a tab
784 * @num_chars: number of character columns already used
785 * @after_tab: true if num_chars starts after a previous tab
788 * Add an operation to the echo buffer to erase a tab.
790 * Called by the eraser function, which knows how many character
791 * columns have been used since either a previous tab or the start
792 * of input. This information will be used later, along with
793 * canon column (if applicable), to go back the correct number
796 * Locking: echo_lock to protect the echo buffer
799 static void echo_erase_tab(unsigned int num_chars, int after_tab,
800 struct n_tty_data *ldata)
802 mutex_lock(&ldata->echo_lock);
804 add_echo_byte(ECHO_OP_START, ldata);
805 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
807 /* We only need to know this modulo 8 (tab spacing) */
810 /* Set the high bit as a flag if num_chars is after a previous tab */
814 add_echo_byte(num_chars, ldata);
816 mutex_unlock(&ldata->echo_lock);
820 * echo_char_raw - echo a character raw
821 * @c: unicode byte to echo
822 * @tty: terminal device
824 * Echo user input back onto the screen. This must be called only when
825 * L_ECHO(tty) is true. Called from the driver receive_buf path.
827 * This variant does not treat control characters specially.
829 * Locking: echo_lock to protect the echo buffer
832 static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
834 mutex_lock(&ldata->echo_lock);
835 if (c == ECHO_OP_START) {
836 add_echo_byte(ECHO_OP_START, ldata);
837 add_echo_byte(ECHO_OP_START, ldata);
839 add_echo_byte(c, ldata);
841 mutex_unlock(&ldata->echo_lock);
845 * echo_char - echo a character
846 * @c: unicode byte to echo
847 * @tty: terminal device
849 * Echo user input back onto the screen. This must be called only when
850 * L_ECHO(tty) is true. Called from the driver receive_buf path.
852 * This variant tags control characters to be echoed as "^X"
853 * (where X is the letter representing the control char).
855 * Locking: echo_lock to protect the echo buffer
858 static void echo_char(unsigned char c, struct tty_struct *tty)
860 struct n_tty_data *ldata = tty->disc_data;
862 mutex_lock(&ldata->echo_lock);
864 if (c == ECHO_OP_START) {
865 add_echo_byte(ECHO_OP_START, ldata);
866 add_echo_byte(ECHO_OP_START, ldata);
868 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
869 add_echo_byte(ECHO_OP_START, ldata);
870 add_echo_byte(c, ldata);
873 mutex_unlock(&ldata->echo_lock);
877 * finish_erasing - complete erase
881 static inline void finish_erasing(struct n_tty_data *ldata)
883 if (ldata->erasing) {
884 echo_char_raw('/', ldata);
890 * eraser - handle erase function
891 * @c: character input
892 * @tty: terminal device
894 * Perform erase and necessary output when an erase character is
895 * present in the stream from the driver layer. Handles the complexities
896 * of UTF-8 multibyte symbols.
898 * Locking: read_lock for tty buffers
901 static void eraser(unsigned char c, struct tty_struct *tty)
903 struct n_tty_data *ldata = tty->disc_data;
904 enum { ERASE, WERASE, KILL } kill_type;
905 int head, seen_alnums, cnt;
908 /* FIXME: locking needed ? */
909 if (ldata->read_head == ldata->canon_head) {
910 /* process_output('\a', tty); */ /* what do you think? */
913 if (c == ERASE_CHAR(tty))
915 else if (c == WERASE_CHAR(tty))
919 raw_spin_lock_irqsave(&ldata->read_lock, flags);
920 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
921 (N_TTY_BUF_SIZE - 1));
922 ldata->read_head = ldata->canon_head;
923 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
926 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
927 raw_spin_lock_irqsave(&ldata->read_lock, flags);
928 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
929 (N_TTY_BUF_SIZE - 1));
930 ldata->read_head = ldata->canon_head;
931 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
932 finish_erasing(ldata);
933 echo_char(KILL_CHAR(tty), tty);
934 /* Add a newline if ECHOK is on and ECHOKE is off. */
936 echo_char_raw('\n', ldata);
943 /* FIXME: Locking ?? */
944 while (ldata->read_head != ldata->canon_head) {
945 head = ldata->read_head;
947 /* erase a single possibly multibyte character */
949 head = (head - 1) & (N_TTY_BUF_SIZE-1);
950 c = ldata->read_buf[head];
951 } while (is_continuation(c, tty) && head != ldata->canon_head);
953 /* do not partially erase */
954 if (is_continuation(c, tty))
957 if (kill_type == WERASE) {
958 /* Equivalent to BSD's ALTWERASE. */
959 if (isalnum(c) || c == '_')
961 else if (seen_alnums)
964 cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1);
965 raw_spin_lock_irqsave(&ldata->read_lock, flags);
966 ldata->read_head = head;
967 ldata->read_cnt -= cnt;
968 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
970 if (L_ECHOPRT(tty)) {
971 if (!ldata->erasing) {
972 echo_char_raw('\\', ldata);
975 /* if cnt > 1, output a multi-byte character */
978 head = (head+1) & (N_TTY_BUF_SIZE-1);
979 echo_char_raw(ldata->read_buf[head],
981 echo_move_back_col(ldata);
983 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
984 echo_char(ERASE_CHAR(tty), tty);
985 } else if (c == '\t') {
986 unsigned int num_chars = 0;
988 unsigned long tail = ldata->read_head;
991 * Count the columns used for characters
992 * since the start of input or after a
994 * This info is used to go back the correct
997 while (tail != ldata->canon_head) {
998 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
999 c = ldata->read_buf[tail];
1003 } else if (iscntrl(c)) {
1006 } else if (!is_continuation(c, tty)) {
1010 echo_erase_tab(num_chars, after_tab, ldata);
1012 if (iscntrl(c) && L_ECHOCTL(tty)) {
1013 echo_char_raw('\b', ldata);
1014 echo_char_raw(' ', ldata);
1015 echo_char_raw('\b', ldata);
1017 if (!iscntrl(c) || L_ECHOCTL(tty)) {
1018 echo_char_raw('\b', ldata);
1019 echo_char_raw(' ', ldata);
1020 echo_char_raw('\b', ldata);
1024 if (kill_type == ERASE)
1027 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
1028 finish_erasing(ldata);
1032 * isig - handle the ISIG optio
1035 * @flush: force flush
1037 * Called when a signal is being sent due to terminal input. This
1038 * may caus terminal flushing to take place according to the termios
1039 * settings and character used. Called from the driver receive_buf
1040 * path so serialized.
1042 * Locking: ctrl_lock, read_lock (both via flush buffer)
1045 static inline void isig(int sig, struct tty_struct *tty, int flush)
1048 kill_pgrp(tty->pgrp, sig, 1);
1049 if (flush || !L_NOFLSH(tty)) {
1050 n_tty_flush_buffer(tty);
1051 tty_driver_flush_buffer(tty);
1056 * n_tty_receive_break - handle break
1059 * An RS232 break event has been hit in the incoming bitstream. This
1060 * can cause a variety of events depending upon the termios settings.
1062 * Called from the receive_buf path so single threaded.
1065 static inline void n_tty_receive_break(struct tty_struct *tty)
1067 struct n_tty_data *ldata = tty->disc_data;
1071 if (I_BRKINT(tty)) {
1072 isig(SIGINT, tty, 1);
1075 if (I_PARMRK(tty)) {
1076 put_tty_queue('\377', ldata);
1077 put_tty_queue('\0', ldata);
1079 put_tty_queue('\0', ldata);
1080 wake_up_interruptible(&tty->read_wait);
1084 * n_tty_receive_overrun - handle overrun reporting
1087 * Data arrived faster than we could process it. While the tty
1088 * driver has flagged this the bits that were missed are gone
1091 * Called from the receive_buf path so single threaded. Does not
1092 * need locking as num_overrun and overrun_time are function
1096 static inline void n_tty_receive_overrun(struct tty_struct *tty)
1098 struct n_tty_data *ldata = tty->disc_data;
1101 ldata->num_overrun++;
1102 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1103 time_after(ldata->overrun_time, jiffies)) {
1104 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1106 ldata->num_overrun);
1107 ldata->overrun_time = jiffies;
1108 ldata->num_overrun = 0;
1113 * n_tty_receive_parity_error - error notifier
1114 * @tty: terminal device
1117 * Process a parity error and queue the right data to indicate
1118 * the error case if necessary. Locking as per n_tty_receive_buf.
1120 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1123 struct n_tty_data *ldata = tty->disc_data;
1127 if (I_PARMRK(tty)) {
1128 put_tty_queue('\377', ldata);
1129 put_tty_queue('\0', ldata);
1130 put_tty_queue(c, ldata);
1131 } else if (I_INPCK(tty))
1132 put_tty_queue('\0', ldata);
1134 put_tty_queue(c, ldata);
1135 wake_up_interruptible(&tty->read_wait);
1139 * n_tty_receive_char - perform processing
1140 * @tty: terminal device
1143 * Process an individual character of input received from the driver.
1144 * This is serialized with respect to itself by the rules for the
1148 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1150 struct n_tty_data *ldata = tty->disc_data;
1151 unsigned long flags;
1155 put_tty_queue(c, ldata);
1161 if (I_IUCLC(tty) && L_IEXTEN(tty))
1164 if (L_EXTPROC(tty)) {
1165 put_tty_queue(c, ldata);
1169 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
1170 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1171 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
1173 process_echoes(tty);
1178 if (c == START_CHAR(tty)) {
1180 process_echoes(tty);
1181 } else if (c == STOP_CHAR(tty))
1188 * If the previous character was LNEXT, or we know that this
1189 * character is not one of the characters that we'll have to
1190 * handle specially, do shortcut processing to speed things
1193 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
1195 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1196 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1197 /* beep if no space */
1199 process_output('\a', tty);
1203 finish_erasing(ldata);
1204 /* Record the column of first canon char. */
1205 if (ldata->canon_head == ldata->read_head)
1206 echo_set_canon_col(ldata);
1208 process_echoes(tty);
1211 put_tty_queue(c, ldata);
1212 put_tty_queue(c, ldata);
1217 if (c == START_CHAR(tty)) {
1219 process_echoes(tty);
1222 if (c == STOP_CHAR(tty)) {
1231 if (c == INTR_CHAR(tty))
1234 if (c == QUIT_CHAR(tty))
1237 if (c == SUSP_CHAR(tty)) {
1240 * Note that we do not use isig() here because we want
1242 * 1) flush, 2) echo, 3) signal
1244 if (!L_NOFLSH(tty)) {
1245 n_tty_flush_buffer(tty);
1246 tty_driver_flush_buffer(tty);
1252 process_echoes(tty);
1255 kill_pgrp(tty->pgrp, signal, 1);
1265 } else if (c == '\n' && I_INLCR(tty))
1268 if (ldata->icanon) {
1269 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1270 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1272 process_echoes(tty);
1275 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
1278 finish_erasing(ldata);
1279 if (L_ECHOCTL(tty)) {
1280 echo_char_raw('^', ldata);
1281 echo_char_raw('\b', ldata);
1282 process_echoes(tty);
1287 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1289 unsigned long tail = ldata->canon_head;
1291 finish_erasing(ldata);
1293 echo_char_raw('\n', ldata);
1294 while (tail != ldata->read_head) {
1295 echo_char(ldata->read_buf[tail], tty);
1296 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1298 process_echoes(tty);
1302 if (ldata->read_cnt >= N_TTY_BUF_SIZE) {
1304 process_output('\a', tty);
1307 if (L_ECHO(tty) || L_ECHONL(tty)) {
1308 echo_char_raw('\n', ldata);
1309 process_echoes(tty);
1311 goto handle_newline;
1313 if (c == EOF_CHAR(tty)) {
1314 if (ldata->read_cnt >= N_TTY_BUF_SIZE)
1316 if (ldata->canon_head != ldata->read_head)
1317 set_bit(TTY_PUSH, &tty->flags);
1318 c = __DISABLED_CHAR;
1319 goto handle_newline;
1321 if ((c == EOL_CHAR(tty)) ||
1322 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1323 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1325 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
1327 process_output('\a', tty);
1331 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1334 /* Record the column of first canon char. */
1335 if (ldata->canon_head == ldata->read_head)
1336 echo_set_canon_col(ldata);
1338 process_echoes(tty);
1341 * XXX does PARMRK doubling happen for
1342 * EOL_CHAR and EOL2_CHAR?
1345 put_tty_queue(c, ldata);
1348 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1349 set_bit(ldata->read_head, ldata->read_flags);
1350 put_tty_queue_nolock(c, ldata);
1351 ldata->canon_head = ldata->read_head;
1352 ldata->canon_data++;
1353 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1354 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1355 if (waitqueue_active(&tty->read_wait))
1356 wake_up_interruptible(&tty->read_wait);
1361 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1362 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1363 /* beep if no space */
1365 process_output('\a', tty);
1369 finish_erasing(ldata);
1371 echo_char_raw('\n', ldata);
1373 /* Record the column of first canon char. */
1374 if (ldata->canon_head == ldata->read_head)
1375 echo_set_canon_col(ldata);
1378 process_echoes(tty);
1382 put_tty_queue(c, ldata);
1384 put_tty_queue(c, ldata);
1389 * n_tty_write_wakeup - asynchronous I/O notifier
1392 * Required for the ptys, serial driver etc. since processes
1393 * that attach themselves to the master and rely on ASYNC
1394 * IO must be woken up
1397 static void n_tty_write_wakeup(struct tty_struct *tty)
1399 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
1400 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
1404 * n_tty_receive_buf - data receive
1405 * @tty: terminal device
1408 * @count: characters
1410 * Called by the terminal driver when a block of characters has
1411 * been received. This function must be called from soft contexts
1412 * not from interrupt context. The driver is responsible for making
1413 * calls one at a time and in order (or using flush_to_ldisc)
1416 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1417 char *fp, int count)
1419 struct n_tty_data *ldata = tty->disc_data;
1420 const unsigned char *p;
1421 char *f, flags = TTY_NORMAL;
1424 unsigned long cpuflags;
1426 if (ldata->real_raw) {
1427 raw_spin_lock_irqsave(&ldata->read_lock, cpuflags);
1428 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1429 N_TTY_BUF_SIZE - ldata->read_head);
1431 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1432 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1433 ldata->read_cnt += i;
1437 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1438 N_TTY_BUF_SIZE - ldata->read_head);
1440 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1441 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1442 ldata->read_cnt += i;
1443 raw_spin_unlock_irqrestore(&ldata->read_lock, cpuflags);
1445 for (i = count, p = cp, f = fp; i; i--, p++) {
1450 n_tty_receive_char(tty, *p);
1453 n_tty_receive_break(tty);
1457 n_tty_receive_parity_error(tty, *p);
1460 n_tty_receive_overrun(tty);
1463 printk(KERN_ERR "%s: unknown flag %d\n",
1464 tty_name(tty, buf), flags);
1468 if (tty->ops->flush_chars)
1469 tty->ops->flush_chars(tty);
1472 n_tty_set_room(tty);
1474 if ((!ldata->icanon && (ldata->read_cnt >= tty->minimum_to_wake)) ||
1476 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1477 if (waitqueue_active(&tty->read_wait))
1478 wake_up_interruptible(&tty->read_wait);
1482 * Check the remaining room for the input canonicalization
1483 * mode. We don't want to throttle the driver if we're in
1484 * canonical mode and don't have a newline yet!
1486 if (tty->receive_room < TTY_THRESHOLD_THROTTLE)
1489 /* FIXME: there is a tiny race here if the receive room check runs
1490 before the other work executes and empties the buffer (upping
1491 the receiving room and unthrottling. We then throttle and get
1492 stuck. This has been observed and traced down by Vincent Pillet/
1493 We need to address this when we sort out out the rx path locking */
1496 int is_ignored(int sig)
1498 return (sigismember(¤t->blocked, sig) ||
1499 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
1503 * n_tty_set_termios - termios data changed
1505 * @old: previous data
1507 * Called by the tty layer when the user changes termios flags so
1508 * that the line discipline can plan ahead. This function cannot sleep
1509 * and is protected from re-entry by the tty layer. The user is
1510 * guaranteed that this function will not be re-entered or in progress
1511 * when the ldisc is closed.
1513 * Locking: Caller holds tty->termios_mutex
1516 static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1518 struct n_tty_data *ldata = tty->disc_data;
1519 int canon_change = 1;
1522 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
1524 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
1525 ldata->canon_head = ldata->read_tail;
1526 ldata->canon_data = 0;
1530 if (canon_change && !L_ICANON(tty) && ldata->read_cnt)
1531 wake_up_interruptible(&tty->read_wait);
1533 ldata->icanon = (L_ICANON(tty) != 0);
1534 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
1536 ldata->real_raw = 1;
1537 n_tty_set_room(tty);
1540 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1541 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1542 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1544 bitmap_zero(ldata->process_char_map, 256);
1546 if (I_IGNCR(tty) || I_ICRNL(tty))
1547 set_bit('\r', ldata->process_char_map);
1549 set_bit('\n', ldata->process_char_map);
1551 if (L_ICANON(tty)) {
1552 set_bit(ERASE_CHAR(tty), ldata->process_char_map);
1553 set_bit(KILL_CHAR(tty), ldata->process_char_map);
1554 set_bit(EOF_CHAR(tty), ldata->process_char_map);
1555 set_bit('\n', ldata->process_char_map);
1556 set_bit(EOL_CHAR(tty), ldata->process_char_map);
1557 if (L_IEXTEN(tty)) {
1558 set_bit(WERASE_CHAR(tty),
1559 ldata->process_char_map);
1560 set_bit(LNEXT_CHAR(tty),
1561 ldata->process_char_map);
1562 set_bit(EOL2_CHAR(tty),
1563 ldata->process_char_map);
1565 set_bit(REPRINT_CHAR(tty),
1566 ldata->process_char_map);
1570 set_bit(START_CHAR(tty), ldata->process_char_map);
1571 set_bit(STOP_CHAR(tty), ldata->process_char_map);
1574 set_bit(INTR_CHAR(tty), ldata->process_char_map);
1575 set_bit(QUIT_CHAR(tty), ldata->process_char_map);
1576 set_bit(SUSP_CHAR(tty), ldata->process_char_map);
1578 clear_bit(__DISABLED_CHAR, ldata->process_char_map);
1580 ldata->real_raw = 0;
1583 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1584 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1585 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
1586 ldata->real_raw = 1;
1588 ldata->real_raw = 0;
1590 n_tty_set_room(tty);
1591 /* The termios change make the tty ready for I/O */
1592 wake_up_interruptible(&tty->write_wait);
1593 wake_up_interruptible(&tty->read_wait);
1597 * n_tty_close - close the ldisc for this tty
1600 * Called from the terminal layer when this line discipline is
1601 * being shut down, either because of a close or becsuse of a
1602 * discipline change. The function will not be called while other
1603 * ldisc methods are in progress.
1606 static void n_tty_close(struct tty_struct *tty)
1608 struct n_tty_data *ldata = tty->disc_data;
1610 n_tty_flush_buffer(tty);
1611 kfree(ldata->read_buf);
1612 kfree(ldata->echo_buf);
1614 tty->disc_data = NULL;
1618 * n_tty_open - open an ldisc
1619 * @tty: terminal to open
1621 * Called when this line discipline is being attached to the
1622 * terminal device. Can sleep. Called serialized so that no
1623 * other events will occur in parallel. No further open will occur
1627 static int n_tty_open(struct tty_struct *tty)
1629 struct n_tty_data *ldata;
1631 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1635 ldata->overrun_time = jiffies;
1636 mutex_init(&ldata->atomic_read_lock);
1637 mutex_init(&ldata->output_lock);
1638 mutex_init(&ldata->echo_lock);
1639 raw_spin_lock_init(&ldata->read_lock);
1641 /* These are ugly. Currently a malloc failure here can panic */
1642 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1643 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1644 if (!ldata->read_buf || !ldata->echo_buf)
1647 tty->disc_data = ldata;
1648 reset_buffer_flags(tty);
1649 tty_unthrottle(tty);
1651 n_tty_set_termios(tty, NULL);
1652 tty->minimum_to_wake = 1;
1657 kfree(ldata->read_buf);
1658 kfree(ldata->echo_buf);
1664 static inline int input_available_p(struct tty_struct *tty, int amt)
1666 struct n_tty_data *ldata = tty->disc_data;
1668 tty_flush_to_ldisc(tty);
1669 if (ldata->icanon && !L_EXTPROC(tty)) {
1670 if (ldata->canon_data)
1672 } else if (ldata->read_cnt >= (amt ? amt : 1))
1679 * copy_from_read_buf - copy read data directly
1680 * @tty: terminal device
1684 * Helper function to speed up n_tty_read. It is only called when
1685 * ICANON is off; it copies characters straight from the tty queue to
1686 * user space directly. It can be profitably called twice; once to
1687 * drain the space from the tail pointer to the (physical) end of the
1688 * buffer, and once to drain the space from the (physical) beginning of
1689 * the buffer to head pointer.
1691 * Called under the ldata->atomic_read_lock sem
1695 static int copy_from_read_buf(struct tty_struct *tty,
1696 unsigned char __user **b,
1700 struct n_tty_data *ldata = tty->disc_data;
1703 unsigned long flags;
1707 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1708 n = min(ldata->read_cnt, N_TTY_BUF_SIZE - ldata->read_tail);
1710 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1712 retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n);
1715 ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty);
1716 tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n,
1718 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1719 ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1);
1720 ldata->read_cnt -= n;
1721 /* Turn single EOF into zero-length read */
1722 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !ldata->read_cnt)
1724 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1731 extern ssize_t redirected_tty_write(struct file *, const char __user *,
1735 * job_control - check job control
1737 * @file: file handle
1739 * Perform job control management checks on this file/tty descriptor
1740 * and if appropriate send any needed signals and return a negative
1741 * error code if action should be taken.
1744 * Locking: None - redirected write test is safe, testing
1745 * current->signal should possibly lock current->sighand
1749 static int job_control(struct tty_struct *tty, struct file *file)
1751 /* Job control check -- must be done at start and after
1752 every sleep (POSIX.1 7.1.1.4). */
1753 /* NOTE: not yet done after every sleep pending a thorough
1754 check of the logic of this change. -- jlc */
1755 /* don't stop on /dev/console */
1756 if (file->f_op->write != redirected_tty_write &&
1757 current->signal->tty == tty) {
1759 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
1760 else if (task_pgrp(current) != tty->pgrp) {
1761 if (is_ignored(SIGTTIN) ||
1762 is_current_pgrp_orphaned())
1764 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1765 set_thread_flag(TIF_SIGPENDING);
1766 return -ERESTARTSYS;
1774 * n_tty_read - read function for tty
1776 * @file: file object
1777 * @buf: userspace buffer pointer
1780 * Perform reads for the line discipline. We are guaranteed that the
1781 * line discipline will not be closed under us but we may get multiple
1782 * parallel readers and must handle this ourselves. We may also get
1783 * a hangup. Always called in user context, may sleep.
1785 * This code must be sure never to sleep through a hangup.
1788 static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
1789 unsigned char __user *buf, size_t nr)
1791 struct n_tty_data *ldata = tty->disc_data;
1792 unsigned char __user *b = buf;
1793 DECLARE_WAITQUEUE(wait, current);
1799 unsigned long flags;
1803 c = job_control(tty, file);
1808 timeout = MAX_SCHEDULE_TIMEOUT;
1809 if (!ldata->icanon) {
1810 time = (HZ / 10) * TIME_CHAR(tty);
1811 minimum = MIN_CHAR(tty);
1814 tty->minimum_to_wake = 1;
1815 else if (!waitqueue_active(&tty->read_wait) ||
1816 (tty->minimum_to_wake > minimum))
1817 tty->minimum_to_wake = minimum;
1824 tty->minimum_to_wake = minimum = 1;
1829 * Internal serialization of reads.
1831 if (file->f_flags & O_NONBLOCK) {
1832 if (!mutex_trylock(&ldata->atomic_read_lock))
1835 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
1836 return -ERESTARTSYS;
1838 packet = tty->packet;
1840 add_wait_queue(&tty->read_wait, &wait);
1842 /* First test for status change. */
1843 if (packet && tty->link->ctrl_status) {
1847 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
1848 cs = tty->link->ctrl_status;
1849 tty->link->ctrl_status = 0;
1850 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
1851 if (tty_put_user(tty, cs, b++)) {
1859 /* This statement must be first before checking for input
1860 so that any interrupt will set the state back to
1862 set_current_state(TASK_INTERRUPTIBLE);
1864 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1865 ((minimum - (b - buf)) >= 1))
1866 tty->minimum_to_wake = (minimum - (b - buf));
1868 if (!input_available_p(tty, 0)) {
1869 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1873 if (tty_hung_up_p(file))
1877 if (file->f_flags & O_NONBLOCK) {
1881 if (signal_pending(current)) {
1882 retval = -ERESTARTSYS;
1885 /* FIXME: does n_tty_set_room need locking ? */
1886 n_tty_set_room(tty);
1887 timeout = schedule_timeout(timeout);
1890 __set_current_state(TASK_RUNNING);
1892 /* Deal with packet mode. */
1893 if (packet && b == buf) {
1894 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
1902 if (ldata->icanon && !L_EXTPROC(tty)) {
1903 /* N.B. avoid overrun if nr == 0 */
1904 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1905 while (nr && ldata->read_cnt) {
1908 eol = test_and_clear_bit(ldata->read_tail,
1910 c = ldata->read_buf[ldata->read_tail];
1911 ldata->read_tail = ((ldata->read_tail+1) &
1912 (N_TTY_BUF_SIZE-1));
1915 /* this test should be redundant:
1916 * we shouldn't be reading data if
1919 if (--ldata->canon_data < 0)
1920 ldata->canon_data = 0;
1922 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1924 if (!eol || (c != __DISABLED_CHAR)) {
1925 if (tty_put_user(tty, c, b++)) {
1928 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1934 tty_audit_push(tty);
1935 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1938 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1940 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1945 /* The copy function takes the read lock and handles
1946 locking internally for this case */
1947 uncopied = copy_from_read_buf(tty, &b, &nr);
1948 uncopied += copy_from_read_buf(tty, &b, &nr);
1955 /* If there is enough space in the read buffer now, let the
1956 * low-level driver know. We use n_tty_chars_in_buffer() to
1957 * check the buffer, as it now knows about canonical mode.
1958 * Otherwise, if the driver is throttled and the line is
1959 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1960 * we won't get any more characters.
1962 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) {
1963 n_tty_set_room(tty);
1964 check_unthrottle(tty);
1967 if (b - buf >= minimum)
1972 mutex_unlock(&ldata->atomic_read_lock);
1973 remove_wait_queue(&tty->read_wait, &wait);
1975 if (!waitqueue_active(&tty->read_wait))
1976 tty->minimum_to_wake = minimum;
1978 __set_current_state(TASK_RUNNING);
1983 clear_bit(TTY_PUSH, &tty->flags);
1984 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1987 n_tty_set_room(tty);
1992 * n_tty_write - write function for tty
1994 * @file: file object
1995 * @buf: userspace buffer pointer
1998 * Write function of the terminal device. This is serialized with
1999 * respect to other write callers but not to termios changes, reads
2000 * and other such events. Since the receive code will echo characters,
2001 * thus calling driver write methods, the output_lock is used in
2002 * the output processing functions called here as well as in the
2003 * echo processing function to protect the column state and space
2004 * left in the buffer.
2006 * This code must be sure never to sleep through a hangup.
2008 * Locking: output_lock to protect column state and space left
2009 * (note that the process_output*() functions take this
2013 static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
2014 const unsigned char *buf, size_t nr)
2016 const unsigned char *b = buf;
2017 DECLARE_WAITQUEUE(wait, current);
2021 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2022 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2023 retval = tty_check_change(tty);
2028 /* Write out any echoed characters that are still pending */
2029 process_echoes(tty);
2031 add_wait_queue(&tty->write_wait, &wait);
2033 set_current_state(TASK_INTERRUPTIBLE);
2034 if (signal_pending(current)) {
2035 retval = -ERESTARTSYS;
2038 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2042 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
2044 ssize_t num = process_output_block(tty, b, nr);
2056 if (process_output(c, tty) < 0)
2060 if (tty->ops->flush_chars)
2061 tty->ops->flush_chars(tty);
2064 c = tty->ops->write(tty, b, nr);
2077 if (file->f_flags & O_NONBLOCK) {
2084 __set_current_state(TASK_RUNNING);
2085 remove_wait_queue(&tty->write_wait, &wait);
2086 if (b - buf != nr && tty->fasync)
2087 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2088 return (b - buf) ? b - buf : retval;
2092 * n_tty_poll - poll method for N_TTY
2093 * @tty: terminal device
2094 * @file: file accessing it
2097 * Called when the line discipline is asked to poll() for data or
2098 * for special events. This code is not serialized with respect to
2099 * other events save open/close.
2101 * This code must be sure never to sleep through a hangup.
2102 * Called without the kernel lock held - fine
2105 static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
2108 unsigned int mask = 0;
2110 poll_wait(file, &tty->read_wait, wait);
2111 poll_wait(file, &tty->write_wait, wait);
2112 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2113 mask |= POLLIN | POLLRDNORM;
2114 if (tty->packet && tty->link->ctrl_status)
2115 mask |= POLLPRI | POLLIN | POLLRDNORM;
2116 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2118 if (tty_hung_up_p(file))
2120 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2121 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2122 tty->minimum_to_wake = MIN_CHAR(tty);
2124 tty->minimum_to_wake = 1;
2126 if (tty->ops->write && !tty_is_writelocked(tty) &&
2127 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2128 tty_write_room(tty) > 0)
2129 mask |= POLLOUT | POLLWRNORM;
2133 static unsigned long inq_canon(struct n_tty_data *ldata)
2137 if (!ldata->canon_data)
2139 head = ldata->canon_head;
2140 tail = ldata->read_tail;
2141 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2142 /* Skip EOF-chars.. */
2143 while (head != tail) {
2144 if (test_bit(tail, ldata->read_flags) &&
2145 ldata->read_buf[tail] == __DISABLED_CHAR)
2147 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2152 static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2153 unsigned int cmd, unsigned long arg)
2155 struct n_tty_data *ldata = tty->disc_data;
2160 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2162 /* FIXME: Locking */
2163 retval = ldata->read_cnt;
2165 retval = inq_canon(ldata);
2166 return put_user(retval, (unsigned int __user *) arg);
2168 return n_tty_ioctl_helper(tty, file, cmd, arg);
2172 struct tty_ldisc_ops tty_ldisc_N_TTY = {
2173 .magic = TTY_LDISC_MAGIC,
2176 .close = n_tty_close,
2177 .flush_buffer = n_tty_flush_buffer,
2178 .chars_in_buffer = n_tty_chars_in_buffer,
2180 .write = n_tty_write,
2181 .ioctl = n_tty_ioctl,
2182 .set_termios = n_tty_set_termios,
2184 .receive_buf = n_tty_receive_buf,
2185 .write_wakeup = n_tty_write_wakeup
2189 * n_tty_inherit_ops - inherit N_TTY methods
2190 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2192 * Enables a 'subclass' line discipline to 'inherit' N_TTY
2196 void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2198 *ops = tty_ldisc_N_TTY;
2200 ops->refcount = ops->flags = 0;
2202 EXPORT_SYMBOL_GPL(n_tty_inherit_ops);