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>
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 /* see if ldisc has been killed - if so, this means that
157 * even though the ldisc has been halted and ->buf.work
158 * cancelled, ->buf.work is about to be rescheduled
160 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
161 "scheduling buffer work for halted ldisc\n");
162 schedule_work(&tty->port->buf.work);
166 static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata)
168 if (ldata->read_cnt < N_TTY_BUF_SIZE) {
169 ldata->read_buf[ldata->read_head] = c;
170 ldata->read_head = (ldata->read_head + 1) & (N_TTY_BUF_SIZE-1);
176 * put_tty_queue - add character to tty
180 * Add a character to the tty read_buf queue. This is done under the
181 * read_lock to serialize character addition and also to protect us
182 * against parallel reads or flushes
185 static void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
189 * The problem of stomping on the buffers ends here.
190 * Why didn't anyone see this one coming? --AJK
192 raw_spin_lock_irqsave(&ldata->read_lock, flags);
193 put_tty_queue_nolock(c, ldata);
194 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
198 * reset_buffer_flags - reset buffer state
199 * @tty: terminal to reset
201 * Reset the read buffer counters and clear the flags.
202 * Called from n_tty_open() and n_tty_flush_buffer().
204 * Locking: tty_read_lock for read fields.
207 static void reset_buffer_flags(struct tty_struct *tty)
209 struct n_tty_data *ldata = tty->disc_data;
212 raw_spin_lock_irqsave(&ldata->read_lock, flags);
213 ldata->read_head = ldata->read_tail = ldata->read_cnt = 0;
214 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
216 mutex_lock(&ldata->echo_lock);
217 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
218 mutex_unlock(&ldata->echo_lock);
220 ldata->canon_head = ldata->canon_data = ldata->erasing = 0;
221 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
225 static void n_tty_packet_mode_flush(struct tty_struct *tty)
229 spin_lock_irqsave(&tty->ctrl_lock, flags);
230 if (tty->link->packet) {
231 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
232 wake_up_interruptible(&tty->link->read_wait);
234 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
238 * n_tty_flush_buffer - clean input queue
239 * @tty: terminal device
241 * Flush the input buffer. Called when the tty layer wants the
242 * buffer flushed (eg at hangup) or when the N_TTY line discipline
243 * internally has to clean the pending queue (for example some signals).
245 * Locking: ctrl_lock, read_lock.
248 static void n_tty_flush_buffer(struct tty_struct *tty)
250 reset_buffer_flags(tty);
253 n_tty_packet_mode_flush(tty);
257 * n_tty_chars_in_buffer - report available bytes
260 * Report the number of characters buffered to be delivered to user
261 * at this instant in time.
266 static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
268 struct n_tty_data *ldata = tty->disc_data;
272 raw_spin_lock_irqsave(&ldata->read_lock, flags);
273 if (!ldata->icanon) {
275 } else if (ldata->canon_data) {
276 n = (ldata->canon_head > ldata->read_tail) ?
277 ldata->canon_head - ldata->read_tail :
278 ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail);
280 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
285 * is_utf8_continuation - utf8 multibyte check
288 * Returns true if the utf8 character 'c' is a multibyte continuation
289 * character. We use this to correctly compute the on screen size
290 * of the character when printing
293 static inline int is_utf8_continuation(unsigned char c)
295 return (c & 0xc0) == 0x80;
299 * is_continuation - multibyte check
302 * Returns true if the utf8 character 'c' is a multibyte continuation
303 * character and the terminal is in unicode mode.
306 static inline int is_continuation(unsigned char c, struct tty_struct *tty)
308 return I_IUTF8(tty) && is_utf8_continuation(c);
312 * do_output_char - output one character
313 * @c: character (or partial unicode symbol)
314 * @tty: terminal device
315 * @space: space available in tty driver write buffer
317 * This is a helper function that handles one output character
318 * (including special characters like TAB, CR, LF, etc.),
319 * doing OPOST processing and putting the results in the
320 * tty driver's write buffer.
322 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
323 * and NLDLY. They simply aren't relevant in the world today.
324 * If you ever need them, add them here.
326 * Returns the number of bytes of buffer space used or -1 if
329 * Locking: should be called under the output_lock to protect
330 * the column state and space left in the buffer
333 static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
335 struct n_tty_data *ldata = tty->disc_data;
348 ldata->canon_column = ldata->column = 0;
349 tty->ops->write(tty, "\r\n", 2);
352 ldata->canon_column = ldata->column;
355 if (O_ONOCR(tty) && ldata->column == 0)
360 ldata->canon_column = ldata->column = 0;
363 ldata->canon_column = ldata->column = 0;
366 spaces = 8 - (ldata->column & 7);
367 if (O_TABDLY(tty) == XTABS) {
370 ldata->column += spaces;
371 tty->ops->write(tty, " ", spaces);
374 ldata->column += spaces;
377 if (ldata->column > 0)
384 if (!is_continuation(c, tty))
390 tty_put_char(tty, c);
395 * process_output - output post processor
396 * @c: character (or partial unicode symbol)
397 * @tty: terminal device
399 * Output one character with OPOST processing.
400 * Returns -1 when the output device is full and the character
403 * Locking: output_lock to protect column state and space left
404 * (also, this is called from n_tty_write under the
405 * tty layer write lock)
408 static int process_output(unsigned char c, struct tty_struct *tty)
410 struct n_tty_data *ldata = tty->disc_data;
413 mutex_lock(&ldata->output_lock);
415 space = tty_write_room(tty);
416 retval = do_output_char(c, tty, space);
418 mutex_unlock(&ldata->output_lock);
426 * process_output_block - block post processor
427 * @tty: terminal device
428 * @buf: character buffer
429 * @nr: number of bytes to output
431 * Output a block of characters with OPOST processing.
432 * Returns the number of characters output.
434 * This path is used to speed up block console writes, among other
435 * things when processing blocks of output data. It handles only
436 * the simple cases normally found and helps to generate blocks of
437 * symbols for the console driver and thus improve performance.
439 * Locking: output_lock to protect column state and space left
440 * (also, this is called from n_tty_write under the
441 * tty layer write lock)
444 static ssize_t process_output_block(struct tty_struct *tty,
445 const unsigned char *buf, unsigned int nr)
447 struct n_tty_data *ldata = tty->disc_data;
450 const unsigned char *cp;
452 mutex_lock(&ldata->output_lock);
454 space = tty_write_room(tty);
456 mutex_unlock(&ldata->output_lock);
462 for (i = 0, cp = buf; i < nr; i++, cp++) {
463 unsigned char c = *cp;
471 ldata->canon_column = ldata->column;
474 if (O_ONOCR(tty) && ldata->column == 0)
478 ldata->canon_column = ldata->column = 0;
483 if (ldata->column > 0)
490 if (!is_continuation(c, tty))
497 i = tty->ops->write(tty, buf, i);
499 mutex_unlock(&ldata->output_lock);
504 * process_echoes - write pending echo characters
505 * @tty: terminal device
507 * Write previously buffered echo (and other ldisc-generated)
508 * characters to the tty.
510 * Characters generated by the ldisc (including echoes) need to
511 * be buffered because the driver's write buffer can fill during
512 * heavy program output. Echoing straight to the driver will
513 * often fail under these conditions, causing lost characters and
514 * resulting mismatches of ldisc state information.
516 * Since the ldisc state must represent the characters actually sent
517 * to the driver at the time of the write, operations like certain
518 * changes in column state are also saved in the buffer and executed
521 * A circular fifo buffer is used so that the most recent characters
522 * are prioritized. Also, when control characters are echoed with a
523 * prefixed "^", the pair is treated atomically and thus not separated.
525 * Locking: output_lock to protect column state and space left,
526 * echo_lock to protect the echo buffer
529 static void process_echoes(struct tty_struct *tty)
531 struct n_tty_data *ldata = tty->disc_data;
534 unsigned char *cp, *buf_end;
536 if (!ldata->echo_cnt)
539 mutex_lock(&ldata->output_lock);
540 mutex_lock(&ldata->echo_lock);
542 space = tty_write_room(tty);
544 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE;
545 cp = ldata->echo_buf + ldata->echo_pos;
546 nr = ldata->echo_cnt;
549 if (c == ECHO_OP_START) {
552 int no_space_left = 0;
555 * If the buffer byte is the start of a multi-byte
556 * operation, get the next byte, which is either the
557 * op code or a control character value.
561 opp -= N_TTY_BUF_SIZE;
565 unsigned int num_chars, num_bs;
567 case ECHO_OP_ERASE_TAB:
568 if (++opp == buf_end)
569 opp -= N_TTY_BUF_SIZE;
573 * Determine how many columns to go back
574 * in order to erase the tab.
575 * This depends on the number of columns
576 * used by other characters within the tab
577 * area. If this (modulo 8) count is from
578 * the start of input rather than from a
579 * previous tab, we offset by canon column.
580 * Otherwise, tab spacing is normal.
582 if (!(num_chars & 0x80))
583 num_chars += ldata->canon_column;
584 num_bs = 8 - (num_chars & 7);
586 if (num_bs > space) {
592 tty_put_char(tty, '\b');
593 if (ldata->column > 0)
600 case ECHO_OP_SET_CANON_COL:
601 ldata->canon_column = ldata->column;
606 case ECHO_OP_MOVE_BACK_COL:
607 if (ldata->column > 0)
614 /* This is an escaped echo op start code */
619 tty_put_char(tty, ECHO_OP_START);
628 * If the op is not a special byte code,
629 * it is a ctrl char tagged to be echoed
630 * as "^X" (where X is the letter
631 * representing the control char).
632 * Note that we must ensure there is
633 * enough space for the whole ctrl pair.
640 tty_put_char(tty, '^');
641 tty_put_char(tty, op ^ 0100);
652 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
653 int retval = do_output_char(c, tty, space);
660 tty_put_char(tty, c);
667 /* When end of circular buffer reached, wrap around */
669 cp -= N_TTY_BUF_SIZE;
675 ldata->echo_overrun = 0;
677 int num_processed = ldata->echo_cnt - nr;
678 ldata->echo_pos += num_processed;
679 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
680 ldata->echo_cnt = nr;
681 if (num_processed > 0)
682 ldata->echo_overrun = 0;
685 mutex_unlock(&ldata->echo_lock);
686 mutex_unlock(&ldata->output_lock);
688 if (tty->ops->flush_chars)
689 tty->ops->flush_chars(tty);
693 * add_echo_byte - add a byte to the echo buffer
694 * @c: unicode byte to echo
697 * Add a character or operation byte to the echo buffer.
699 * Should be called under the echo lock to protect the echo buffer.
702 static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
706 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
707 /* Circular buffer is already at capacity */
708 new_byte_pos = ldata->echo_pos;
711 * Since the buffer start position needs to be advanced,
712 * be sure to step by a whole operation byte group.
714 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
715 if (ldata->echo_buf[(ldata->echo_pos + 1) &
716 (N_TTY_BUF_SIZE - 1)] ==
718 ldata->echo_pos += 3;
719 ldata->echo_cnt -= 2;
721 ldata->echo_pos += 2;
722 ldata->echo_cnt -= 1;
727 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
729 ldata->echo_overrun = 1;
731 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
732 new_byte_pos &= N_TTY_BUF_SIZE - 1;
736 ldata->echo_buf[new_byte_pos] = c;
740 * echo_move_back_col - add operation to move back a column
743 * Add an operation to the echo buffer to move back one column.
745 * Locking: echo_lock to protect the echo buffer
748 static void echo_move_back_col(struct n_tty_data *ldata)
750 mutex_lock(&ldata->echo_lock);
751 add_echo_byte(ECHO_OP_START, ldata);
752 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
753 mutex_unlock(&ldata->echo_lock);
757 * echo_set_canon_col - add operation to set the canon column
760 * Add an operation to the echo buffer to set the canon column
761 * to the current column.
763 * Locking: echo_lock to protect the echo buffer
766 static void echo_set_canon_col(struct n_tty_data *ldata)
768 mutex_lock(&ldata->echo_lock);
769 add_echo_byte(ECHO_OP_START, ldata);
770 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
771 mutex_unlock(&ldata->echo_lock);
775 * echo_erase_tab - add operation to erase a tab
776 * @num_chars: number of character columns already used
777 * @after_tab: true if num_chars starts after a previous tab
780 * Add an operation to the echo buffer to erase a tab.
782 * Called by the eraser function, which knows how many character
783 * columns have been used since either a previous tab or the start
784 * of input. This information will be used later, along with
785 * canon column (if applicable), to go back the correct number
788 * Locking: echo_lock to protect the echo buffer
791 static void echo_erase_tab(unsigned int num_chars, int after_tab,
792 struct n_tty_data *ldata)
794 mutex_lock(&ldata->echo_lock);
796 add_echo_byte(ECHO_OP_START, ldata);
797 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
799 /* We only need to know this modulo 8 (tab spacing) */
802 /* Set the high bit as a flag if num_chars is after a previous tab */
806 add_echo_byte(num_chars, ldata);
808 mutex_unlock(&ldata->echo_lock);
812 * echo_char_raw - echo a character raw
813 * @c: unicode byte to echo
814 * @tty: terminal device
816 * Echo user input back onto the screen. This must be called only when
817 * L_ECHO(tty) is true. Called from the driver receive_buf path.
819 * This variant does not treat control characters specially.
821 * Locking: echo_lock to protect the echo buffer
824 static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
826 mutex_lock(&ldata->echo_lock);
827 if (c == ECHO_OP_START) {
828 add_echo_byte(ECHO_OP_START, ldata);
829 add_echo_byte(ECHO_OP_START, ldata);
831 add_echo_byte(c, ldata);
833 mutex_unlock(&ldata->echo_lock);
837 * echo_char - echo a character
838 * @c: unicode byte to echo
839 * @tty: terminal device
841 * Echo user input back onto the screen. This must be called only when
842 * L_ECHO(tty) is true. Called from the driver receive_buf path.
844 * This variant tags control characters to be echoed as "^X"
845 * (where X is the letter representing the control char).
847 * Locking: echo_lock to protect the echo buffer
850 static void echo_char(unsigned char c, struct tty_struct *tty)
852 struct n_tty_data *ldata = tty->disc_data;
854 mutex_lock(&ldata->echo_lock);
856 if (c == ECHO_OP_START) {
857 add_echo_byte(ECHO_OP_START, ldata);
858 add_echo_byte(ECHO_OP_START, ldata);
860 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
861 add_echo_byte(ECHO_OP_START, ldata);
862 add_echo_byte(c, ldata);
865 mutex_unlock(&ldata->echo_lock);
869 * finish_erasing - complete erase
873 static inline void finish_erasing(struct n_tty_data *ldata)
875 if (ldata->erasing) {
876 echo_char_raw('/', ldata);
882 * eraser - handle erase function
883 * @c: character input
884 * @tty: terminal device
886 * Perform erase and necessary output when an erase character is
887 * present in the stream from the driver layer. Handles the complexities
888 * of UTF-8 multibyte symbols.
890 * Locking: read_lock for tty buffers
893 static void eraser(unsigned char c, struct tty_struct *tty)
895 struct n_tty_data *ldata = tty->disc_data;
896 enum { ERASE, WERASE, KILL } kill_type;
897 int head, seen_alnums, cnt;
900 /* FIXME: locking needed ? */
901 if (ldata->read_head == ldata->canon_head) {
902 /* process_output('\a', tty); */ /* what do you think? */
905 if (c == ERASE_CHAR(tty))
907 else if (c == WERASE_CHAR(tty))
911 raw_spin_lock_irqsave(&ldata->read_lock, flags);
912 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
913 (N_TTY_BUF_SIZE - 1));
914 ldata->read_head = ldata->canon_head;
915 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
918 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(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);
924 finish_erasing(ldata);
925 echo_char(KILL_CHAR(tty), tty);
926 /* Add a newline if ECHOK is on and ECHOKE is off. */
928 echo_char_raw('\n', ldata);
935 /* FIXME: Locking ?? */
936 while (ldata->read_head != ldata->canon_head) {
937 head = ldata->read_head;
939 /* erase a single possibly multibyte character */
941 head = (head - 1) & (N_TTY_BUF_SIZE-1);
942 c = ldata->read_buf[head];
943 } while (is_continuation(c, tty) && head != ldata->canon_head);
945 /* do not partially erase */
946 if (is_continuation(c, tty))
949 if (kill_type == WERASE) {
950 /* Equivalent to BSD's ALTWERASE. */
951 if (isalnum(c) || c == '_')
953 else if (seen_alnums)
956 cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1);
957 raw_spin_lock_irqsave(&ldata->read_lock, flags);
958 ldata->read_head = head;
959 ldata->read_cnt -= cnt;
960 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
962 if (L_ECHOPRT(tty)) {
963 if (!ldata->erasing) {
964 echo_char_raw('\\', ldata);
967 /* if cnt > 1, output a multi-byte character */
970 head = (head+1) & (N_TTY_BUF_SIZE-1);
971 echo_char_raw(ldata->read_buf[head],
973 echo_move_back_col(ldata);
975 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
976 echo_char(ERASE_CHAR(tty), tty);
977 } else if (c == '\t') {
978 unsigned int num_chars = 0;
980 unsigned long tail = ldata->read_head;
983 * Count the columns used for characters
984 * since the start of input or after a
986 * This info is used to go back the correct
989 while (tail != ldata->canon_head) {
990 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
991 c = ldata->read_buf[tail];
995 } else if (iscntrl(c)) {
998 } else if (!is_continuation(c, tty)) {
1002 echo_erase_tab(num_chars, after_tab, ldata);
1004 if (iscntrl(c) && L_ECHOCTL(tty)) {
1005 echo_char_raw('\b', ldata);
1006 echo_char_raw(' ', ldata);
1007 echo_char_raw('\b', ldata);
1009 if (!iscntrl(c) || L_ECHOCTL(tty)) {
1010 echo_char_raw('\b', ldata);
1011 echo_char_raw(' ', ldata);
1012 echo_char_raw('\b', ldata);
1016 if (kill_type == ERASE)
1019 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
1020 finish_erasing(ldata);
1024 * isig - handle the ISIG optio
1028 * Called when a signal is being sent due to terminal input.
1029 * Called from the driver receive_buf path so serialized.
1031 * Locking: ctrl_lock
1034 static inline void isig(int sig, struct tty_struct *tty)
1036 struct pid *tty_pgrp = tty_get_pgrp(tty);
1038 kill_pgrp(tty_pgrp, sig, 1);
1044 * n_tty_receive_break - handle break
1047 * An RS232 break event has been hit in the incoming bitstream. This
1048 * can cause a variety of events depending upon the termios settings.
1050 * Called from the receive_buf path so single threaded.
1053 static inline void n_tty_receive_break(struct tty_struct *tty)
1055 struct n_tty_data *ldata = tty->disc_data;
1059 if (I_BRKINT(tty)) {
1061 if (!L_NOFLSH(tty)) {
1062 n_tty_flush_buffer(tty);
1063 tty_driver_flush_buffer(tty);
1067 if (I_PARMRK(tty)) {
1068 put_tty_queue('\377', ldata);
1069 put_tty_queue('\0', ldata);
1071 put_tty_queue('\0', ldata);
1072 wake_up_interruptible(&tty->read_wait);
1076 * n_tty_receive_overrun - handle overrun reporting
1079 * Data arrived faster than we could process it. While the tty
1080 * driver has flagged this the bits that were missed are gone
1083 * Called from the receive_buf path so single threaded. Does not
1084 * need locking as num_overrun and overrun_time are function
1088 static inline void n_tty_receive_overrun(struct tty_struct *tty)
1090 struct n_tty_data *ldata = tty->disc_data;
1093 ldata->num_overrun++;
1094 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1095 time_after(ldata->overrun_time, jiffies)) {
1096 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1098 ldata->num_overrun);
1099 ldata->overrun_time = jiffies;
1100 ldata->num_overrun = 0;
1105 * n_tty_receive_parity_error - error notifier
1106 * @tty: terminal device
1109 * Process a parity error and queue the right data to indicate
1110 * the error case if necessary. Locking as per n_tty_receive_buf.
1112 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1115 struct n_tty_data *ldata = tty->disc_data;
1119 if (I_PARMRK(tty)) {
1120 put_tty_queue('\377', ldata);
1121 put_tty_queue('\0', ldata);
1122 put_tty_queue(c, ldata);
1123 } else if (I_INPCK(tty))
1124 put_tty_queue('\0', ldata);
1126 put_tty_queue(c, ldata);
1127 wake_up_interruptible(&tty->read_wait);
1131 * n_tty_receive_char - perform processing
1132 * @tty: terminal device
1135 * Process an individual character of input received from the driver.
1136 * This is serialized with respect to itself by the rules for the
1140 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1142 struct n_tty_data *ldata = tty->disc_data;
1143 unsigned long flags;
1147 put_tty_queue(c, ldata);
1153 if (I_IUCLC(tty) && L_IEXTEN(tty))
1156 if (L_EXTPROC(tty)) {
1157 put_tty_queue(c, ldata);
1161 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
1162 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1163 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
1165 process_echoes(tty);
1170 if (c == START_CHAR(tty)) {
1172 process_echoes(tty);
1173 } else if (c == STOP_CHAR(tty))
1180 * If the previous character was LNEXT, or we know that this
1181 * character is not one of the characters that we'll have to
1182 * handle specially, do shortcut processing to speed things
1185 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
1187 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1188 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1189 /* beep if no space */
1191 process_output('\a', tty);
1195 finish_erasing(ldata);
1196 /* Record the column of first canon char. */
1197 if (ldata->canon_head == ldata->read_head)
1198 echo_set_canon_col(ldata);
1200 process_echoes(tty);
1203 put_tty_queue(c, ldata);
1204 put_tty_queue(c, ldata);
1209 if (c == START_CHAR(tty)) {
1211 process_echoes(tty);
1214 if (c == STOP_CHAR(tty)) {
1223 if (c == INTR_CHAR(tty))
1226 if (c == QUIT_CHAR(tty))
1229 if (c == SUSP_CHAR(tty)) {
1231 if (!L_NOFLSH(tty)) {
1232 n_tty_flush_buffer(tty);
1233 tty_driver_flush_buffer(tty);
1239 process_echoes(tty);
1251 } else if (c == '\n' && I_INLCR(tty))
1254 if (ldata->icanon) {
1255 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1256 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1258 process_echoes(tty);
1261 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
1264 finish_erasing(ldata);
1265 if (L_ECHOCTL(tty)) {
1266 echo_char_raw('^', ldata);
1267 echo_char_raw('\b', ldata);
1268 process_echoes(tty);
1273 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1275 unsigned long tail = ldata->canon_head;
1277 finish_erasing(ldata);
1279 echo_char_raw('\n', ldata);
1280 while (tail != ldata->read_head) {
1281 echo_char(ldata->read_buf[tail], tty);
1282 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1284 process_echoes(tty);
1288 if (ldata->read_cnt >= N_TTY_BUF_SIZE) {
1290 process_output('\a', tty);
1293 if (L_ECHO(tty) || L_ECHONL(tty)) {
1294 echo_char_raw('\n', ldata);
1295 process_echoes(tty);
1297 goto handle_newline;
1299 if (c == EOF_CHAR(tty)) {
1300 if (ldata->read_cnt >= N_TTY_BUF_SIZE)
1302 if (ldata->canon_head != ldata->read_head)
1303 set_bit(TTY_PUSH, &tty->flags);
1304 c = __DISABLED_CHAR;
1305 goto handle_newline;
1307 if ((c == EOL_CHAR(tty)) ||
1308 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1309 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1311 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
1313 process_output('\a', tty);
1317 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1320 /* Record the column of first canon char. */
1321 if (ldata->canon_head == ldata->read_head)
1322 echo_set_canon_col(ldata);
1324 process_echoes(tty);
1327 * XXX does PARMRK doubling happen for
1328 * EOL_CHAR and EOL2_CHAR?
1331 put_tty_queue(c, ldata);
1334 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1335 set_bit(ldata->read_head, ldata->read_flags);
1336 put_tty_queue_nolock(c, ldata);
1337 ldata->canon_head = ldata->read_head;
1338 ldata->canon_data++;
1339 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1340 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1341 if (waitqueue_active(&tty->read_wait))
1342 wake_up_interruptible(&tty->read_wait);
1347 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1348 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1349 /* beep if no space */
1351 process_output('\a', tty);
1355 finish_erasing(ldata);
1357 echo_char_raw('\n', ldata);
1359 /* Record the column of first canon char. */
1360 if (ldata->canon_head == ldata->read_head)
1361 echo_set_canon_col(ldata);
1364 process_echoes(tty);
1368 put_tty_queue(c, ldata);
1370 put_tty_queue(c, ldata);
1375 * n_tty_write_wakeup - asynchronous I/O notifier
1378 * Required for the ptys, serial driver etc. since processes
1379 * that attach themselves to the master and rely on ASYNC
1380 * IO must be woken up
1383 static void n_tty_write_wakeup(struct tty_struct *tty)
1385 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
1386 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
1390 * n_tty_receive_buf - data receive
1391 * @tty: terminal device
1394 * @count: characters
1396 * Called by the terminal driver when a block of characters has
1397 * been received. This function must be called from soft contexts
1398 * not from interrupt context. The driver is responsible for making
1399 * calls one at a time and in order (or using flush_to_ldisc)
1402 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1403 char *fp, int count)
1405 struct n_tty_data *ldata = tty->disc_data;
1406 const unsigned char *p;
1407 char *f, flags = TTY_NORMAL;
1410 unsigned long cpuflags;
1412 if (ldata->real_raw) {
1413 raw_spin_lock_irqsave(&ldata->read_lock, cpuflags);
1414 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1415 N_TTY_BUF_SIZE - ldata->read_head);
1417 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1418 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1419 ldata->read_cnt += i;
1423 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1424 N_TTY_BUF_SIZE - ldata->read_head);
1426 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1427 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1428 ldata->read_cnt += i;
1429 raw_spin_unlock_irqrestore(&ldata->read_lock, cpuflags);
1431 for (i = count, p = cp, f = fp; i; i--, p++) {
1436 n_tty_receive_char(tty, *p);
1439 n_tty_receive_break(tty);
1443 n_tty_receive_parity_error(tty, *p);
1446 n_tty_receive_overrun(tty);
1449 printk(KERN_ERR "%s: unknown flag %d\n",
1450 tty_name(tty, buf), flags);
1454 if (tty->ops->flush_chars)
1455 tty->ops->flush_chars(tty);
1458 n_tty_set_room(tty);
1460 if ((!ldata->icanon && (ldata->read_cnt >= tty->minimum_to_wake)) ||
1462 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1463 if (waitqueue_active(&tty->read_wait))
1464 wake_up_interruptible(&tty->read_wait);
1468 * Check the remaining room for the input canonicalization
1469 * mode. We don't want to throttle the driver if we're in
1470 * canonical mode and don't have a newline yet!
1473 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
1474 if (tty->receive_room >= TTY_THRESHOLD_THROTTLE)
1476 if (!tty_throttle_safe(tty))
1479 __tty_set_flow_change(tty, 0);
1482 int is_ignored(int sig)
1484 return (sigismember(¤t->blocked, sig) ||
1485 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
1489 * n_tty_set_termios - termios data changed
1491 * @old: previous data
1493 * Called by the tty layer when the user changes termios flags so
1494 * that the line discipline can plan ahead. This function cannot sleep
1495 * and is protected from re-entry by the tty layer. The user is
1496 * guaranteed that this function will not be re-entered or in progress
1497 * when the ldisc is closed.
1499 * Locking: Caller holds tty->termios_mutex
1502 static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1504 struct n_tty_data *ldata = tty->disc_data;
1505 int canon_change = 1;
1508 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
1510 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
1511 ldata->canon_head = ldata->read_tail;
1512 ldata->canon_data = 0;
1516 if (canon_change && !L_ICANON(tty) && ldata->read_cnt)
1517 wake_up_interruptible(&tty->read_wait);
1519 ldata->icanon = (L_ICANON(tty) != 0);
1520 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
1522 ldata->real_raw = 1;
1523 n_tty_set_room(tty);
1526 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1527 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1528 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1530 bitmap_zero(ldata->process_char_map, 256);
1532 if (I_IGNCR(tty) || I_ICRNL(tty))
1533 set_bit('\r', ldata->process_char_map);
1535 set_bit('\n', ldata->process_char_map);
1537 if (L_ICANON(tty)) {
1538 set_bit(ERASE_CHAR(tty), ldata->process_char_map);
1539 set_bit(KILL_CHAR(tty), ldata->process_char_map);
1540 set_bit(EOF_CHAR(tty), ldata->process_char_map);
1541 set_bit('\n', ldata->process_char_map);
1542 set_bit(EOL_CHAR(tty), ldata->process_char_map);
1543 if (L_IEXTEN(tty)) {
1544 set_bit(WERASE_CHAR(tty),
1545 ldata->process_char_map);
1546 set_bit(LNEXT_CHAR(tty),
1547 ldata->process_char_map);
1548 set_bit(EOL2_CHAR(tty),
1549 ldata->process_char_map);
1551 set_bit(REPRINT_CHAR(tty),
1552 ldata->process_char_map);
1556 set_bit(START_CHAR(tty), ldata->process_char_map);
1557 set_bit(STOP_CHAR(tty), ldata->process_char_map);
1560 set_bit(INTR_CHAR(tty), ldata->process_char_map);
1561 set_bit(QUIT_CHAR(tty), ldata->process_char_map);
1562 set_bit(SUSP_CHAR(tty), ldata->process_char_map);
1564 clear_bit(__DISABLED_CHAR, ldata->process_char_map);
1566 ldata->real_raw = 0;
1569 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1570 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1571 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
1572 ldata->real_raw = 1;
1574 ldata->real_raw = 0;
1576 n_tty_set_room(tty);
1577 /* The termios change make the tty ready for I/O */
1578 wake_up_interruptible(&tty->write_wait);
1579 wake_up_interruptible(&tty->read_wait);
1583 * n_tty_close - close the ldisc for this tty
1586 * Called from the terminal layer when this line discipline is
1587 * being shut down, either because of a close or becsuse of a
1588 * discipline change. The function will not be called while other
1589 * ldisc methods are in progress.
1592 static void n_tty_close(struct tty_struct *tty)
1594 struct n_tty_data *ldata = tty->disc_data;
1597 n_tty_packet_mode_flush(tty);
1599 kfree(ldata->read_buf);
1600 kfree(ldata->echo_buf);
1602 tty->disc_data = NULL;
1606 * n_tty_open - open an ldisc
1607 * @tty: terminal to open
1609 * Called when this line discipline is being attached to the
1610 * terminal device. Can sleep. Called serialized so that no
1611 * other events will occur in parallel. No further open will occur
1615 static int n_tty_open(struct tty_struct *tty)
1617 struct n_tty_data *ldata;
1619 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1623 ldata->overrun_time = jiffies;
1624 mutex_init(&ldata->atomic_read_lock);
1625 mutex_init(&ldata->output_lock);
1626 mutex_init(&ldata->echo_lock);
1627 raw_spin_lock_init(&ldata->read_lock);
1629 /* These are ugly. Currently a malloc failure here can panic */
1630 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1631 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1632 if (!ldata->read_buf || !ldata->echo_buf)
1635 tty->disc_data = ldata;
1636 /* indicate buffer work may resume */
1637 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1638 reset_buffer_flags(tty);
1639 tty_unthrottle(tty);
1641 n_tty_set_termios(tty, NULL);
1642 tty->minimum_to_wake = 1;
1647 kfree(ldata->read_buf);
1648 kfree(ldata->echo_buf);
1654 static inline int input_available_p(struct tty_struct *tty, int amt)
1656 struct n_tty_data *ldata = tty->disc_data;
1658 tty_flush_to_ldisc(tty);
1659 if (ldata->icanon && !L_EXTPROC(tty)) {
1660 if (ldata->canon_data)
1662 } else if (ldata->read_cnt >= (amt ? amt : 1))
1669 * copy_from_read_buf - copy read data directly
1670 * @tty: terminal device
1674 * Helper function to speed up n_tty_read. It is only called when
1675 * ICANON is off; it copies characters straight from the tty queue to
1676 * user space directly. It can be profitably called twice; once to
1677 * drain the space from the tail pointer to the (physical) end of the
1678 * buffer, and once to drain the space from the (physical) beginning of
1679 * the buffer to head pointer.
1681 * Called under the ldata->atomic_read_lock sem
1685 static int copy_from_read_buf(struct tty_struct *tty,
1686 unsigned char __user **b,
1690 struct n_tty_data *ldata = tty->disc_data;
1693 unsigned long flags;
1697 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1698 n = min(ldata->read_cnt, N_TTY_BUF_SIZE - ldata->read_tail);
1700 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1702 retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n);
1705 ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty);
1706 tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n,
1708 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1709 ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1);
1710 ldata->read_cnt -= n;
1711 /* Turn single EOF into zero-length read */
1712 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !ldata->read_cnt)
1714 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1721 extern ssize_t redirected_tty_write(struct file *, const char __user *,
1725 * job_control - check job control
1727 * @file: file handle
1729 * Perform job control management checks on this file/tty descriptor
1730 * and if appropriate send any needed signals and return a negative
1731 * error code if action should be taken.
1733 * Locking: redirected write test is safe
1734 * current->signal->tty check is safe
1735 * ctrl_lock to safely reference tty->pgrp
1738 static int job_control(struct tty_struct *tty, struct file *file)
1740 /* Job control check -- must be done at start and after
1741 every sleep (POSIX.1 7.1.1.4). */
1742 /* NOTE: not yet done after every sleep pending a thorough
1743 check of the logic of this change. -- jlc */
1744 /* don't stop on /dev/console */
1745 if (file->f_op->write == redirected_tty_write ||
1746 current->signal->tty != tty)
1749 spin_lock_irq(&tty->ctrl_lock);
1751 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
1752 else if (task_pgrp(current) != tty->pgrp) {
1753 spin_unlock_irq(&tty->ctrl_lock);
1754 if (is_ignored(SIGTTIN) || is_current_pgrp_orphaned())
1756 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1757 set_thread_flag(TIF_SIGPENDING);
1758 return -ERESTARTSYS;
1760 spin_unlock_irq(&tty->ctrl_lock);
1766 * n_tty_read - read function for tty
1768 * @file: file object
1769 * @buf: userspace buffer pointer
1772 * Perform reads for the line discipline. We are guaranteed that the
1773 * line discipline will not be closed under us but we may get multiple
1774 * parallel readers and must handle this ourselves. We may also get
1775 * a hangup. Always called in user context, may sleep.
1777 * This code must be sure never to sleep through a hangup.
1780 static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
1781 unsigned char __user *buf, size_t nr)
1783 struct n_tty_data *ldata = tty->disc_data;
1784 unsigned char __user *b = buf;
1785 DECLARE_WAITQUEUE(wait, current);
1791 unsigned long flags;
1795 c = job_control(tty, file);
1800 timeout = MAX_SCHEDULE_TIMEOUT;
1801 if (!ldata->icanon) {
1802 time = (HZ / 10) * TIME_CHAR(tty);
1803 minimum = MIN_CHAR(tty);
1806 tty->minimum_to_wake = 1;
1807 else if (!waitqueue_active(&tty->read_wait) ||
1808 (tty->minimum_to_wake > minimum))
1809 tty->minimum_to_wake = minimum;
1816 tty->minimum_to_wake = minimum = 1;
1821 * Internal serialization of reads.
1823 if (file->f_flags & O_NONBLOCK) {
1824 if (!mutex_trylock(&ldata->atomic_read_lock))
1827 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
1828 return -ERESTARTSYS;
1830 packet = tty->packet;
1832 add_wait_queue(&tty->read_wait, &wait);
1834 /* First test for status change. */
1835 if (packet && tty->link->ctrl_status) {
1839 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
1840 cs = tty->link->ctrl_status;
1841 tty->link->ctrl_status = 0;
1842 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
1843 if (tty_put_user(tty, cs, b++)) {
1851 /* This statement must be first before checking for input
1852 so that any interrupt will set the state back to
1854 set_current_state(TASK_INTERRUPTIBLE);
1856 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1857 ((minimum - (b - buf)) >= 1))
1858 tty->minimum_to_wake = (minimum - (b - buf));
1860 if (!input_available_p(tty, 0)) {
1861 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1865 if (tty_hung_up_p(file))
1869 if (file->f_flags & O_NONBLOCK) {
1873 if (signal_pending(current)) {
1874 retval = -ERESTARTSYS;
1877 /* FIXME: does n_tty_set_room need locking ? */
1878 n_tty_set_room(tty);
1879 timeout = schedule_timeout(timeout);
1882 __set_current_state(TASK_RUNNING);
1884 /* Deal with packet mode. */
1885 if (packet && b == buf) {
1886 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
1894 if (ldata->icanon && !L_EXTPROC(tty)) {
1895 /* N.B. avoid overrun if nr == 0 */
1896 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1897 while (nr && ldata->read_cnt) {
1900 eol = test_and_clear_bit(ldata->read_tail,
1902 c = ldata->read_buf[ldata->read_tail];
1903 ldata->read_tail = ((ldata->read_tail+1) &
1904 (N_TTY_BUF_SIZE-1));
1907 /* this test should be redundant:
1908 * we shouldn't be reading data if
1911 if (--ldata->canon_data < 0)
1912 ldata->canon_data = 0;
1914 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1916 if (!eol || (c != __DISABLED_CHAR)) {
1917 if (tty_put_user(tty, c, b++)) {
1920 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1926 tty_audit_push(tty);
1927 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1930 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1932 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1937 /* The copy function takes the read lock and handles
1938 locking internally for this case */
1939 uncopied = copy_from_read_buf(tty, &b, &nr);
1940 uncopied += copy_from_read_buf(tty, &b, &nr);
1947 /* If there is enough space in the read buffer now, let the
1948 * low-level driver know. We use n_tty_chars_in_buffer() to
1949 * check the buffer, as it now knows about canonical mode.
1950 * Otherwise, if the driver is throttled and the line is
1951 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1952 * we won't get any more characters.
1955 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1956 if (n_tty_chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
1960 n_tty_set_room(tty);
1961 if (!tty_unthrottle_safe(tty))
1964 __tty_set_flow_change(tty, 0);
1966 if (b - buf >= minimum)
1971 mutex_unlock(&ldata->atomic_read_lock);
1972 remove_wait_queue(&tty->read_wait, &wait);
1974 if (!waitqueue_active(&tty->read_wait))
1975 tty->minimum_to_wake = minimum;
1977 __set_current_state(TASK_RUNNING);
1982 clear_bit(TTY_PUSH, &tty->flags);
1983 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1986 n_tty_set_room(tty);
1991 * n_tty_write - write function for tty
1993 * @file: file object
1994 * @buf: userspace buffer pointer
1997 * Write function of the terminal device. This is serialized with
1998 * respect to other write callers but not to termios changes, reads
1999 * and other such events. Since the receive code will echo characters,
2000 * thus calling driver write methods, the output_lock is used in
2001 * the output processing functions called here as well as in the
2002 * echo processing function to protect the column state and space
2003 * left in the buffer.
2005 * This code must be sure never to sleep through a hangup.
2007 * Locking: output_lock to protect column state and space left
2008 * (note that the process_output*() functions take this
2012 static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
2013 const unsigned char *buf, size_t nr)
2015 const unsigned char *b = buf;
2016 DECLARE_WAITQUEUE(wait, current);
2020 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2021 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2022 retval = tty_check_change(tty);
2027 /* Write out any echoed characters that are still pending */
2028 process_echoes(tty);
2030 add_wait_queue(&tty->write_wait, &wait);
2032 set_current_state(TASK_INTERRUPTIBLE);
2033 if (signal_pending(current)) {
2034 retval = -ERESTARTSYS;
2037 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2041 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
2043 ssize_t num = process_output_block(tty, b, nr);
2055 if (process_output(c, tty) < 0)
2059 if (tty->ops->flush_chars)
2060 tty->ops->flush_chars(tty);
2063 c = tty->ops->write(tty, b, nr);
2076 if (file->f_flags & O_NONBLOCK) {
2083 __set_current_state(TASK_RUNNING);
2084 remove_wait_queue(&tty->write_wait, &wait);
2085 if (b - buf != nr && tty->fasync)
2086 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2087 return (b - buf) ? b - buf : retval;
2091 * n_tty_poll - poll method for N_TTY
2092 * @tty: terminal device
2093 * @file: file accessing it
2096 * Called when the line discipline is asked to poll() for data or
2097 * for special events. This code is not serialized with respect to
2098 * other events save open/close.
2100 * This code must be sure never to sleep through a hangup.
2101 * Called without the kernel lock held - fine
2104 static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
2107 unsigned int mask = 0;
2109 poll_wait(file, &tty->read_wait, wait);
2110 poll_wait(file, &tty->write_wait, wait);
2111 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2112 mask |= POLLIN | POLLRDNORM;
2113 if (tty->packet && tty->link->ctrl_status)
2114 mask |= POLLPRI | POLLIN | POLLRDNORM;
2115 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2117 if (tty_hung_up_p(file))
2119 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2120 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2121 tty->minimum_to_wake = MIN_CHAR(tty);
2123 tty->minimum_to_wake = 1;
2125 if (tty->ops->write && !tty_is_writelocked(tty) &&
2126 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2127 tty_write_room(tty) > 0)
2128 mask |= POLLOUT | POLLWRNORM;
2132 static unsigned long inq_canon(struct n_tty_data *ldata)
2136 if (!ldata->canon_data)
2138 head = ldata->canon_head;
2139 tail = ldata->read_tail;
2140 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2141 /* Skip EOF-chars.. */
2142 while (head != tail) {
2143 if (test_bit(tail, ldata->read_flags) &&
2144 ldata->read_buf[tail] == __DISABLED_CHAR)
2146 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2151 static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2152 unsigned int cmd, unsigned long arg)
2154 struct n_tty_data *ldata = tty->disc_data;
2159 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2161 /* FIXME: Locking */
2162 retval = ldata->read_cnt;
2164 retval = inq_canon(ldata);
2165 return put_user(retval, (unsigned int __user *) arg);
2167 return n_tty_ioctl_helper(tty, file, cmd, arg);
2171 struct tty_ldisc_ops tty_ldisc_N_TTY = {
2172 .magic = TTY_LDISC_MAGIC,
2175 .close = n_tty_close,
2176 .flush_buffer = n_tty_flush_buffer,
2177 .chars_in_buffer = n_tty_chars_in_buffer,
2179 .write = n_tty_write,
2180 .ioctl = n_tty_ioctl,
2181 .set_termios = n_tty_set_termios,
2183 .receive_buf = n_tty_receive_buf,
2184 .write_wakeup = n_tty_write_wakeup
2188 * n_tty_inherit_ops - inherit N_TTY methods
2189 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2191 * Enables a 'subclass' line discipline to 'inherit' N_TTY
2195 void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2197 *ops = tty_ldisc_N_TTY;
2199 ops->refcount = ops->flags = 0;
2201 EXPORT_SYMBOL_GPL(n_tty_inherit_ops);