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>
54 /* number of characters left in xmit buffer before select has we have room */
55 #define WAKEUP_CHARS 256
58 * This defines the low- and high-watermarks for throttling and
59 * unthrottling the TTY driver. These watermarks are used for
60 * controlling the space in the read buffer.
62 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
63 #define TTY_THRESHOLD_UNTHROTTLE 128
66 * Special byte codes used in the echo buffer to represent operations
67 * or special handling of characters. Bytes in the echo buffer that
68 * are not part of such special blocks are treated as normal character
71 #define ECHO_OP_START 0xff
72 #define ECHO_OP_MOVE_BACK_COL 0x80
73 #define ECHO_OP_SET_CANON_COL 0x81
74 #define ECHO_OP_ERASE_TAB 0x82
76 static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
77 unsigned char __user *ptr)
79 tty_audit_add_data(tty, &x, 1);
80 return put_user(x, ptr);
84 * n_tty_set__room - receive space
87 * Called by the driver to find out how much data it is
88 * permitted to feed to the line discipline without any being lost
89 * and thus to manage flow control. Not serialized. Answers for the
93 static void n_tty_set_room(struct tty_struct *tty)
98 /* tty->read_cnt is not read locked ? */
100 /* Multiply read_cnt by 3, since each byte might take up to
101 * three times as many spaces when PARMRK is set (depending on
102 * its flags, e.g. parity error). */
103 left = N_TTY_BUF_SIZE - tty->read_cnt * 3 - 1;
105 left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
108 * If we are doing input canonicalization, and there are no
109 * pending newlines, let characters through without limit, so
110 * that erase characters will be handled. Other excess
111 * characters will be beeped.
114 left = tty->icanon && !tty->canon_data;
115 old_left = tty->receive_room;
116 tty->receive_room = left;
118 /* Did this open up the receive buffer? We may need to flip */
119 if (left && !old_left)
120 schedule_work(&tty->buf.work);
123 static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
125 if (tty->read_cnt < N_TTY_BUF_SIZE) {
126 tty->read_buf[tty->read_head] = c;
127 tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
133 * put_tty_queue - add character to tty
137 * Add a character to the tty read_buf queue. This is done under the
138 * read_lock to serialize character addition and also to protect us
139 * against parallel reads or flushes
142 static void put_tty_queue(unsigned char c, struct tty_struct *tty)
146 * The problem of stomping on the buffers ends here.
147 * Why didn't anyone see this one coming? --AJK
149 spin_lock_irqsave(&tty->read_lock, flags);
150 put_tty_queue_nolock(c, tty);
151 spin_unlock_irqrestore(&tty->read_lock, flags);
155 * check_unthrottle - allow new receive data
158 * Check whether to call the driver unthrottle functions
160 * Can sleep, may be called under the atomic_read_lock mutex but
161 * this is not guaranteed.
163 static void check_unthrottle(struct tty_struct *tty)
170 * reset_buffer_flags - reset buffer state
171 * @tty: terminal to reset
173 * Reset the read buffer counters, clear the flags,
174 * and make sure the driver is unthrottled. Called
175 * from n_tty_open() and n_tty_flush_buffer().
177 * Locking: tty_read_lock for read fields.
180 static void reset_buffer_flags(struct tty_struct *tty)
184 spin_lock_irqsave(&tty->read_lock, flags);
185 tty->read_head = tty->read_tail = tty->read_cnt = 0;
186 spin_unlock_irqrestore(&tty->read_lock, flags);
188 mutex_lock(&tty->echo_lock);
189 tty->echo_pos = tty->echo_cnt = tty->echo_overrun = 0;
190 mutex_unlock(&tty->echo_lock);
192 tty->canon_head = tty->canon_data = tty->erasing = 0;
193 memset(&tty->read_flags, 0, sizeof tty->read_flags);
198 * n_tty_flush_buffer - clean input queue
199 * @tty: terminal device
201 * Flush the input buffer. Called when the line discipline is
202 * being closed, when the tty layer wants the buffer flushed (eg
203 * at hangup) or when the N_TTY line discipline internally has to
204 * clean the pending queue (for example some signals).
206 * Locking: ctrl_lock, read_lock.
209 static void n_tty_flush_buffer(struct tty_struct *tty)
212 /* clear everything and unthrottle the driver */
213 reset_buffer_flags(tty);
218 spin_lock_irqsave(&tty->ctrl_lock, flags);
219 if (tty->link->packet) {
220 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
221 wake_up_interruptible(&tty->link->read_wait);
223 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
227 * n_tty_chars_in_buffer - report available bytes
230 * Report the number of characters buffered to be delivered to user
231 * at this instant in time.
236 static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
241 spin_lock_irqsave(&tty->read_lock, flags);
244 } else if (tty->canon_data) {
245 n = (tty->canon_head > tty->read_tail) ?
246 tty->canon_head - tty->read_tail :
247 tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
249 spin_unlock_irqrestore(&tty->read_lock, flags);
254 * is_utf8_continuation - utf8 multibyte check
257 * Returns true if the utf8 character 'c' is a multibyte continuation
258 * character. We use this to correctly compute the on screen size
259 * of the character when printing
262 static inline int is_utf8_continuation(unsigned char c)
264 return (c & 0xc0) == 0x80;
268 * is_continuation - multibyte check
271 * Returns true if the utf8 character 'c' is a multibyte continuation
272 * character and the terminal is in unicode mode.
275 static inline int is_continuation(unsigned char c, struct tty_struct *tty)
277 return I_IUTF8(tty) && is_utf8_continuation(c);
281 * do_output_char - output one character
282 * @c: character (or partial unicode symbol)
283 * @tty: terminal device
284 * @space: space available in tty driver write buffer
286 * This is a helper function that handles one output character
287 * (including special characters like TAB, CR, LF, etc.),
288 * doing OPOST processing and putting the results in the
289 * tty driver's write buffer.
291 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
292 * and NLDLY. They simply aren't relevant in the world today.
293 * If you ever need them, add them here.
295 * Returns the number of bytes of buffer space used or -1 if
298 * Locking: should be called under the output_lock to protect
299 * the column state and space left in the buffer
302 static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
316 tty->canon_column = tty->column = 0;
317 tty->ops->write(tty, "\r\n", 2);
320 tty->canon_column = tty->column;
323 if (O_ONOCR(tty) && tty->column == 0)
328 tty->canon_column = tty->column = 0;
331 tty->canon_column = tty->column = 0;
334 spaces = 8 - (tty->column & 7);
335 if (O_TABDLY(tty) == XTABS) {
338 tty->column += spaces;
339 tty->ops->write(tty, " ", spaces);
342 tty->column += spaces;
352 if (!is_continuation(c, tty))
358 tty_put_char(tty, c);
363 * process_output - output post processor
364 * @c: character (or partial unicode symbol)
365 * @tty: terminal device
367 * Output one character with OPOST processing.
368 * Returns -1 when the output device is full and the character
371 * Locking: output_lock to protect column state and space left
372 * (also, this is called from n_tty_write under the
373 * tty layer write lock)
376 static int process_output(unsigned char c, struct tty_struct *tty)
380 mutex_lock(&tty->output_lock);
382 space = tty_write_room(tty);
383 retval = do_output_char(c, tty, space);
385 mutex_unlock(&tty->output_lock);
393 * process_output_block - block post processor
394 * @tty: terminal device
395 * @buf: character buffer
396 * @nr: number of bytes to output
398 * Output a block of characters with OPOST processing.
399 * Returns the number of characters output.
401 * This path is used to speed up block console writes, among other
402 * things when processing blocks of output data. It handles only
403 * the simple cases normally found and helps to generate blocks of
404 * symbols for the console driver and thus improve performance.
406 * Locking: output_lock to protect column state and space left
407 * (also, this is called from n_tty_write under the
408 * tty layer write lock)
411 static ssize_t process_output_block(struct tty_struct *tty,
412 const unsigned char *buf, unsigned int nr)
416 const unsigned char *cp;
418 mutex_lock(&tty->output_lock);
420 space = tty_write_room(tty);
422 mutex_unlock(&tty->output_lock);
428 for (i = 0, cp = buf; i < nr; i++, cp++) {
429 unsigned char c = *cp;
437 tty->canon_column = tty->column;
440 if (O_ONOCR(tty) && tty->column == 0)
444 tty->canon_column = tty->column = 0;
456 if (!is_continuation(c, tty))
463 i = tty->ops->write(tty, buf, i);
465 mutex_unlock(&tty->output_lock);
470 * process_echoes - write pending echo characters
471 * @tty: terminal device
473 * Write previously buffered echo (and other ldisc-generated)
474 * characters to the tty.
476 * Characters generated by the ldisc (including echoes) need to
477 * be buffered because the driver's write buffer can fill during
478 * heavy program output. Echoing straight to the driver will
479 * often fail under these conditions, causing lost characters and
480 * resulting mismatches of ldisc state information.
482 * Since the ldisc state must represent the characters actually sent
483 * to the driver at the time of the write, operations like certain
484 * changes in column state are also saved in the buffer and executed
487 * A circular fifo buffer is used so that the most recent characters
488 * are prioritized. Also, when control characters are echoed with a
489 * prefixed "^", the pair is treated atomically and thus not separated.
491 * Locking: output_lock to protect column state and space left,
492 * echo_lock to protect the echo buffer
495 static void process_echoes(struct tty_struct *tty)
499 unsigned char *cp, *buf_end;
504 mutex_lock(&tty->output_lock);
505 mutex_lock(&tty->echo_lock);
507 space = tty_write_room(tty);
509 buf_end = tty->echo_buf + N_TTY_BUF_SIZE;
510 cp = tty->echo_buf + tty->echo_pos;
514 if (c == ECHO_OP_START) {
517 int no_space_left = 0;
520 * If the buffer byte is the start of a multi-byte
521 * operation, get the next byte, which is either the
522 * op code or a control character value.
526 opp -= N_TTY_BUF_SIZE;
530 unsigned int num_chars, num_bs;
532 case ECHO_OP_ERASE_TAB:
533 if (++opp == buf_end)
534 opp -= N_TTY_BUF_SIZE;
538 * Determine how many columns to go back
539 * in order to erase the tab.
540 * This depends on the number of columns
541 * used by other characters within the tab
542 * area. If this (modulo 8) count is from
543 * the start of input rather than from a
544 * previous tab, we offset by canon column.
545 * Otherwise, tab spacing is normal.
547 if (!(num_chars & 0x80))
548 num_chars += tty->canon_column;
549 num_bs = 8 - (num_chars & 7);
551 if (num_bs > space) {
557 tty_put_char(tty, '\b');
565 case ECHO_OP_SET_CANON_COL:
566 tty->canon_column = tty->column;
571 case ECHO_OP_MOVE_BACK_COL:
579 /* This is an escaped echo op start code */
584 tty_put_char(tty, ECHO_OP_START);
593 * If the op is not a special byte code,
594 * it is a ctrl char tagged to be echoed
595 * as "^X" (where X is the letter
596 * representing the control char).
597 * Note that we must ensure there is
598 * enough space for the whole ctrl pair.
605 tty_put_char(tty, '^');
606 tty_put_char(tty, op ^ 0100);
617 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
618 int retval = do_output_char(c, tty, space);
625 tty_put_char(tty, c);
632 /* When end of circular buffer reached, wrap around */
634 cp -= N_TTY_BUF_SIZE;
640 tty->echo_overrun = 0;
642 int num_processed = tty->echo_cnt - nr;
643 tty->echo_pos += num_processed;
644 tty->echo_pos &= N_TTY_BUF_SIZE - 1;
646 if (num_processed > 0)
647 tty->echo_overrun = 0;
650 mutex_unlock(&tty->echo_lock);
651 mutex_unlock(&tty->output_lock);
653 if (tty->ops->flush_chars)
654 tty->ops->flush_chars(tty);
658 * add_echo_byte - add a byte to the echo buffer
659 * @c: unicode byte to echo
660 * @tty: terminal device
662 * Add a character or operation byte to the echo buffer.
664 * Should be called under the echo lock to protect the echo buffer.
667 static void add_echo_byte(unsigned char c, struct tty_struct *tty)
671 if (tty->echo_cnt == N_TTY_BUF_SIZE) {
672 /* Circular buffer is already at capacity */
673 new_byte_pos = tty->echo_pos;
676 * Since the buffer start position needs to be advanced,
677 * be sure to step by a whole operation byte group.
679 if (tty->echo_buf[tty->echo_pos] == ECHO_OP_START) {
680 if (tty->echo_buf[(tty->echo_pos + 1) &
681 (N_TTY_BUF_SIZE - 1)] ==
692 tty->echo_pos &= N_TTY_BUF_SIZE - 1;
694 tty->echo_overrun = 1;
696 new_byte_pos = tty->echo_pos + tty->echo_cnt;
697 new_byte_pos &= N_TTY_BUF_SIZE - 1;
701 tty->echo_buf[new_byte_pos] = c;
705 * echo_move_back_col - add operation to move back a column
706 * @tty: terminal device
708 * Add an operation to the echo buffer to move back one column.
710 * Locking: echo_lock to protect the echo buffer
713 static void echo_move_back_col(struct tty_struct *tty)
715 mutex_lock(&tty->echo_lock);
717 add_echo_byte(ECHO_OP_START, tty);
718 add_echo_byte(ECHO_OP_MOVE_BACK_COL, tty);
720 mutex_unlock(&tty->echo_lock);
724 * echo_set_canon_col - add operation to set the canon column
725 * @tty: terminal device
727 * Add an operation to the echo buffer to set the canon column
728 * to the current column.
730 * Locking: echo_lock to protect the echo buffer
733 static void echo_set_canon_col(struct tty_struct *tty)
735 mutex_lock(&tty->echo_lock);
737 add_echo_byte(ECHO_OP_START, tty);
738 add_echo_byte(ECHO_OP_SET_CANON_COL, tty);
740 mutex_unlock(&tty->echo_lock);
744 * echo_erase_tab - add operation to erase a tab
745 * @num_chars: number of character columns already used
746 * @after_tab: true if num_chars starts after a previous tab
747 * @tty: terminal device
749 * Add an operation to the echo buffer to erase a tab.
751 * Called by the eraser function, which knows how many character
752 * columns have been used since either a previous tab or the start
753 * of input. This information will be used later, along with
754 * canon column (if applicable), to go back the correct number
757 * Locking: echo_lock to protect the echo buffer
760 static void echo_erase_tab(unsigned int num_chars, int after_tab,
761 struct tty_struct *tty)
763 mutex_lock(&tty->echo_lock);
765 add_echo_byte(ECHO_OP_START, tty);
766 add_echo_byte(ECHO_OP_ERASE_TAB, tty);
768 /* We only need to know this modulo 8 (tab spacing) */
771 /* Set the high bit as a flag if num_chars is after a previous tab */
775 add_echo_byte(num_chars, tty);
777 mutex_unlock(&tty->echo_lock);
781 * echo_char_raw - echo a character raw
782 * @c: unicode byte to echo
783 * @tty: terminal device
785 * Echo user input back onto the screen. This must be called only when
786 * L_ECHO(tty) is true. Called from the driver receive_buf path.
788 * This variant does not treat control characters specially.
790 * Locking: echo_lock to protect the echo buffer
793 static void echo_char_raw(unsigned char c, struct tty_struct *tty)
795 mutex_lock(&tty->echo_lock);
797 if (c == ECHO_OP_START) {
798 add_echo_byte(ECHO_OP_START, tty);
799 add_echo_byte(ECHO_OP_START, tty);
801 add_echo_byte(c, tty);
804 mutex_unlock(&tty->echo_lock);
808 * echo_char - echo a character
809 * @c: unicode byte to echo
810 * @tty: terminal device
812 * Echo user input back onto the screen. This must be called only when
813 * L_ECHO(tty) is true. Called from the driver receive_buf path.
815 * This variant tags control characters to be echoed as "^X"
816 * (where X is the letter representing the control char).
818 * Locking: echo_lock to protect the echo buffer
821 static void echo_char(unsigned char c, struct tty_struct *tty)
823 mutex_lock(&tty->echo_lock);
825 if (c == ECHO_OP_START) {
826 add_echo_byte(ECHO_OP_START, tty);
827 add_echo_byte(ECHO_OP_START, tty);
829 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
830 add_echo_byte(ECHO_OP_START, tty);
831 add_echo_byte(c, tty);
834 mutex_unlock(&tty->echo_lock);
838 * finish_erasing - complete erase
839 * @tty: tty doing the erase
842 static inline void finish_erasing(struct tty_struct *tty)
845 echo_char_raw('/', tty);
851 * eraser - handle erase function
852 * @c: character input
853 * @tty: terminal device
855 * Perform erase and necessary output when an erase character is
856 * present in the stream from the driver layer. Handles the complexities
857 * of UTF-8 multibyte symbols.
859 * Locking: read_lock for tty buffers
862 static void eraser(unsigned char c, struct tty_struct *tty)
864 enum { ERASE, WERASE, KILL } kill_type;
865 int head, seen_alnums, cnt;
868 /* FIXME: locking needed ? */
869 if (tty->read_head == tty->canon_head) {
870 /* process_output('\a', tty); */ /* what do you think? */
873 if (c == ERASE_CHAR(tty))
875 else if (c == WERASE_CHAR(tty))
879 spin_lock_irqsave(&tty->read_lock, flags);
880 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
881 (N_TTY_BUF_SIZE - 1));
882 tty->read_head = tty->canon_head;
883 spin_unlock_irqrestore(&tty->read_lock, flags);
886 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
887 spin_lock_irqsave(&tty->read_lock, flags);
888 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
889 (N_TTY_BUF_SIZE - 1));
890 tty->read_head = tty->canon_head;
891 spin_unlock_irqrestore(&tty->read_lock, flags);
893 echo_char(KILL_CHAR(tty), tty);
894 /* Add a newline if ECHOK is on and ECHOKE is off. */
896 echo_char_raw('\n', tty);
903 /* FIXME: Locking ?? */
904 while (tty->read_head != tty->canon_head) {
905 head = tty->read_head;
907 /* erase a single possibly multibyte character */
909 head = (head - 1) & (N_TTY_BUF_SIZE-1);
910 c = tty->read_buf[head];
911 } while (is_continuation(c, tty) && head != tty->canon_head);
913 /* do not partially erase */
914 if (is_continuation(c, tty))
917 if (kill_type == WERASE) {
918 /* Equivalent to BSD's ALTWERASE. */
919 if (isalnum(c) || c == '_')
921 else if (seen_alnums)
924 cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
925 spin_lock_irqsave(&tty->read_lock, flags);
926 tty->read_head = head;
927 tty->read_cnt -= cnt;
928 spin_unlock_irqrestore(&tty->read_lock, flags);
930 if (L_ECHOPRT(tty)) {
932 echo_char_raw('\\', tty);
935 /* if cnt > 1, output a multi-byte character */
938 head = (head+1) & (N_TTY_BUF_SIZE-1);
939 echo_char_raw(tty->read_buf[head], tty);
940 echo_move_back_col(tty);
942 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
943 echo_char(ERASE_CHAR(tty), tty);
944 } else if (c == '\t') {
945 unsigned int num_chars = 0;
947 unsigned long tail = tty->read_head;
950 * Count the columns used for characters
951 * since the start of input or after a
953 * This info is used to go back the correct
956 while (tail != tty->canon_head) {
957 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
958 c = tty->read_buf[tail];
962 } else if (iscntrl(c)) {
965 } else if (!is_continuation(c, tty)) {
969 echo_erase_tab(num_chars, after_tab, tty);
971 if (iscntrl(c) && L_ECHOCTL(tty)) {
972 echo_char_raw('\b', tty);
973 echo_char_raw(' ', tty);
974 echo_char_raw('\b', tty);
976 if (!iscntrl(c) || L_ECHOCTL(tty)) {
977 echo_char_raw('\b', tty);
978 echo_char_raw(' ', tty);
979 echo_char_raw('\b', tty);
983 if (kill_type == ERASE)
986 if (tty->read_head == tty->canon_head && L_ECHO(tty))
991 * isig - handle the ISIG optio
994 * @flush: force flush
996 * Called when a signal is being sent due to terminal input. This
997 * may caus terminal flushing to take place according to the termios
998 * settings and character used. Called from the driver receive_buf
999 * path so serialized.
1001 * Locking: ctrl_lock, read_lock (both via flush buffer)
1004 static inline void isig(int sig, struct tty_struct *tty, int flush)
1007 kill_pgrp(tty->pgrp, sig, 1);
1008 if (flush || !L_NOFLSH(tty)) {
1009 n_tty_flush_buffer(tty);
1010 tty_driver_flush_buffer(tty);
1015 * n_tty_receive_break - handle break
1018 * An RS232 break event has been hit in the incoming bitstream. This
1019 * can cause a variety of events depending upon the termios settings.
1021 * Called from the receive_buf path so single threaded.
1024 static inline void n_tty_receive_break(struct tty_struct *tty)
1028 if (I_BRKINT(tty)) {
1029 isig(SIGINT, tty, 1);
1032 if (I_PARMRK(tty)) {
1033 put_tty_queue('\377', tty);
1034 put_tty_queue('\0', tty);
1036 put_tty_queue('\0', tty);
1037 wake_up_interruptible(&tty->read_wait);
1041 * n_tty_receive_overrun - handle overrun reporting
1044 * Data arrived faster than we could process it. While the tty
1045 * driver has flagged this the bits that were missed are gone
1048 * Called from the receive_buf path so single threaded. Does not
1049 * need locking as num_overrun and overrun_time are function
1053 static inline void n_tty_receive_overrun(struct tty_struct *tty)
1058 if (time_before(tty->overrun_time, jiffies - HZ) ||
1059 time_after(tty->overrun_time, jiffies)) {
1060 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1063 tty->overrun_time = jiffies;
1064 tty->num_overrun = 0;
1069 * n_tty_receive_parity_error - error notifier
1070 * @tty: terminal device
1073 * Process a parity error and queue the right data to indicate
1074 * the error case if necessary. Locking as per n_tty_receive_buf.
1076 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1081 if (I_PARMRK(tty)) {
1082 put_tty_queue('\377', tty);
1083 put_tty_queue('\0', tty);
1084 put_tty_queue(c, tty);
1085 } else if (I_INPCK(tty))
1086 put_tty_queue('\0', tty);
1088 put_tty_queue(c, tty);
1089 wake_up_interruptible(&tty->read_wait);
1093 * n_tty_receive_char - perform processing
1094 * @tty: terminal device
1097 * Process an individual character of input received from the driver.
1098 * This is serialized with respect to itself by the rules for the
1102 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1104 unsigned long flags;
1108 put_tty_queue(c, tty);
1114 if (I_IUCLC(tty) && L_IEXTEN(tty))
1117 if (L_EXTPROC(tty)) {
1118 put_tty_queue(c, tty);
1122 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
1123 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1124 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
1126 process_echoes(tty);
1131 if (c == START_CHAR(tty)) {
1133 process_echoes(tty);
1134 } else if (c == STOP_CHAR(tty))
1141 * If the previous character was LNEXT, or we know that this
1142 * character is not one of the characters that we'll have to
1143 * handle specially, do shortcut processing to speed things
1146 if (!test_bit(c, tty->process_char_map) || tty->lnext) {
1148 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1149 if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1150 /* beep if no space */
1152 process_output('\a', tty);
1156 finish_erasing(tty);
1157 /* Record the column of first canon char. */
1158 if (tty->canon_head == tty->read_head)
1159 echo_set_canon_col(tty);
1161 process_echoes(tty);
1164 put_tty_queue(c, tty);
1165 put_tty_queue(c, tty);
1170 if (c == START_CHAR(tty)) {
1172 process_echoes(tty);
1175 if (c == STOP_CHAR(tty)) {
1184 if (c == INTR_CHAR(tty))
1187 if (c == QUIT_CHAR(tty))
1190 if (c == SUSP_CHAR(tty)) {
1193 * Note that we do not use isig() here because we want
1195 * 1) flush, 2) echo, 3) signal
1197 if (!L_NOFLSH(tty)) {
1198 n_tty_flush_buffer(tty);
1199 tty_driver_flush_buffer(tty);
1205 process_echoes(tty);
1208 kill_pgrp(tty->pgrp, signal, 1);
1218 } else if (c == '\n' && I_INLCR(tty))
1222 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1223 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1225 process_echoes(tty);
1228 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
1231 finish_erasing(tty);
1232 if (L_ECHOCTL(tty)) {
1233 echo_char_raw('^', tty);
1234 echo_char_raw('\b', tty);
1235 process_echoes(tty);
1240 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1242 unsigned long tail = tty->canon_head;
1244 finish_erasing(tty);
1246 echo_char_raw('\n', tty);
1247 while (tail != tty->read_head) {
1248 echo_char(tty->read_buf[tail], tty);
1249 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1251 process_echoes(tty);
1255 if (tty->read_cnt >= N_TTY_BUF_SIZE) {
1257 process_output('\a', tty);
1260 if (L_ECHO(tty) || L_ECHONL(tty)) {
1261 echo_char_raw('\n', tty);
1262 process_echoes(tty);
1264 goto handle_newline;
1266 if (c == EOF_CHAR(tty)) {
1267 if (tty->read_cnt >= N_TTY_BUF_SIZE)
1269 if (tty->canon_head != tty->read_head)
1270 set_bit(TTY_PUSH, &tty->flags);
1271 c = __DISABLED_CHAR;
1272 goto handle_newline;
1274 if ((c == EOL_CHAR(tty)) ||
1275 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1276 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1278 if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
1280 process_output('\a', tty);
1284 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1287 /* Record the column of first canon char. */
1288 if (tty->canon_head == tty->read_head)
1289 echo_set_canon_col(tty);
1291 process_echoes(tty);
1294 * XXX does PARMRK doubling happen for
1295 * EOL_CHAR and EOL2_CHAR?
1298 put_tty_queue(c, tty);
1301 spin_lock_irqsave(&tty->read_lock, flags);
1302 set_bit(tty->read_head, tty->read_flags);
1303 put_tty_queue_nolock(c, tty);
1304 tty->canon_head = tty->read_head;
1306 spin_unlock_irqrestore(&tty->read_lock, flags);
1307 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1308 if (waitqueue_active(&tty->read_wait))
1309 wake_up_interruptible(&tty->read_wait);
1314 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1315 if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1316 /* beep if no space */
1318 process_output('\a', tty);
1322 finish_erasing(tty);
1324 echo_char_raw('\n', tty);
1326 /* Record the column of first canon char. */
1327 if (tty->canon_head == tty->read_head)
1328 echo_set_canon_col(tty);
1331 process_echoes(tty);
1335 put_tty_queue(c, tty);
1337 put_tty_queue(c, tty);
1342 * n_tty_write_wakeup - asynchronous I/O notifier
1345 * Required for the ptys, serial driver etc. since processes
1346 * that attach themselves to the master and rely on ASYNC
1347 * IO must be woken up
1350 static void n_tty_write_wakeup(struct tty_struct *tty)
1352 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
1353 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
1357 * n_tty_receive_buf - data receive
1358 * @tty: terminal device
1361 * @count: characters
1363 * Called by the terminal driver when a block of characters has
1364 * been received. This function must be called from soft contexts
1365 * not from interrupt context. The driver is responsible for making
1366 * calls one at a time and in order (or using flush_to_ldisc)
1369 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1370 char *fp, int count)
1372 const unsigned char *p;
1373 char *f, flags = TTY_NORMAL;
1376 unsigned long cpuflags;
1378 if (tty->real_raw) {
1379 spin_lock_irqsave(&tty->read_lock, cpuflags);
1380 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
1381 N_TTY_BUF_SIZE - tty->read_head);
1383 memcpy(tty->read_buf + tty->read_head, cp, i);
1384 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
1389 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
1390 N_TTY_BUF_SIZE - tty->read_head);
1392 memcpy(tty->read_buf + tty->read_head, cp, i);
1393 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
1395 spin_unlock_irqrestore(&tty->read_lock, cpuflags);
1397 for (i = count, p = cp, f = fp; i; i--, p++) {
1402 n_tty_receive_char(tty, *p);
1405 n_tty_receive_break(tty);
1409 n_tty_receive_parity_error(tty, *p);
1412 n_tty_receive_overrun(tty);
1415 printk(KERN_ERR "%s: unknown flag %d\n",
1416 tty_name(tty, buf), flags);
1420 if (tty->ops->flush_chars)
1421 tty->ops->flush_chars(tty);
1424 n_tty_set_room(tty);
1426 if ((!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) ||
1428 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1429 if (waitqueue_active(&tty->read_wait))
1430 wake_up_interruptible(&tty->read_wait);
1434 * Check the remaining room for the input canonicalization
1435 * mode. We don't want to throttle the driver if we're in
1436 * canonical mode and don't have a newline yet!
1438 if (tty->receive_room < TTY_THRESHOLD_THROTTLE)
1441 /* FIXME: there is a tiny race here if the receive room check runs
1442 before the other work executes and empties the buffer (upping
1443 the receiving room and unthrottling. We then throttle and get
1444 stuck. This has been observed and traced down by Vincent Pillet/
1445 We need to address this when we sort out out the rx path locking */
1448 int is_ignored(int sig)
1450 return (sigismember(¤t->blocked, sig) ||
1451 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
1455 * n_tty_set_termios - termios data changed
1457 * @old: previous data
1459 * Called by the tty layer when the user changes termios flags so
1460 * that the line discipline can plan ahead. This function cannot sleep
1461 * and is protected from re-entry by the tty layer. The user is
1462 * guaranteed that this function will not be re-entered or in progress
1463 * when the ldisc is closed.
1465 * Locking: Caller holds tty->termios_mutex
1468 static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1470 int canon_change = 1;
1473 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
1475 memset(&tty->read_flags, 0, sizeof tty->read_flags);
1476 tty->canon_head = tty->read_tail;
1477 tty->canon_data = 0;
1481 if (canon_change && !L_ICANON(tty) && tty->read_cnt)
1482 wake_up_interruptible(&tty->read_wait);
1484 tty->icanon = (L_ICANON(tty) != 0);
1485 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
1488 n_tty_set_room(tty);
1491 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1492 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1493 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1495 memset(tty->process_char_map, 0, 256/8);
1497 if (I_IGNCR(tty) || I_ICRNL(tty))
1498 set_bit('\r', tty->process_char_map);
1500 set_bit('\n', tty->process_char_map);
1502 if (L_ICANON(tty)) {
1503 set_bit(ERASE_CHAR(tty), tty->process_char_map);
1504 set_bit(KILL_CHAR(tty), tty->process_char_map);
1505 set_bit(EOF_CHAR(tty), tty->process_char_map);
1506 set_bit('\n', tty->process_char_map);
1507 set_bit(EOL_CHAR(tty), tty->process_char_map);
1508 if (L_IEXTEN(tty)) {
1509 set_bit(WERASE_CHAR(tty),
1510 tty->process_char_map);
1511 set_bit(LNEXT_CHAR(tty),
1512 tty->process_char_map);
1513 set_bit(EOL2_CHAR(tty),
1514 tty->process_char_map);
1516 set_bit(REPRINT_CHAR(tty),
1517 tty->process_char_map);
1521 set_bit(START_CHAR(tty), tty->process_char_map);
1522 set_bit(STOP_CHAR(tty), tty->process_char_map);
1525 set_bit(INTR_CHAR(tty), tty->process_char_map);
1526 set_bit(QUIT_CHAR(tty), tty->process_char_map);
1527 set_bit(SUSP_CHAR(tty), tty->process_char_map);
1529 clear_bit(__DISABLED_CHAR, tty->process_char_map);
1534 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1535 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1536 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
1541 n_tty_set_room(tty);
1542 /* The termios change make the tty ready for I/O */
1543 wake_up_interruptible(&tty->write_wait);
1544 wake_up_interruptible(&tty->read_wait);
1548 * n_tty_close - close the ldisc for this tty
1551 * Called from the terminal layer when this line discipline is
1552 * being shut down, either because of a close or becsuse of a
1553 * discipline change. The function will not be called while other
1554 * ldisc methods are in progress.
1557 static void n_tty_close(struct tty_struct *tty)
1559 n_tty_flush_buffer(tty);
1560 kfree(tty->read_buf);
1561 kfree(tty->echo_buf);
1562 tty->read_buf = NULL;
1563 tty->echo_buf = NULL;
1567 * n_tty_open - open an ldisc
1568 * @tty: terminal to open
1570 * Called when this line discipline is being attached to the
1571 * terminal device. Can sleep. Called serialized so that no
1572 * other events will occur in parallel. No further open will occur
1576 static int n_tty_open(struct tty_struct *tty)
1578 /* These are ugly. Currently a malloc failure here can panic */
1579 tty->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1580 tty->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1581 if (!tty->read_buf || !tty->echo_buf)
1584 reset_buffer_flags(tty);
1585 tty_unthrottle(tty);
1587 n_tty_set_termios(tty, NULL);
1588 tty->minimum_to_wake = 1;
1592 kfree(tty->read_buf);
1593 kfree(tty->echo_buf);
1598 static inline int input_available_p(struct tty_struct *tty, int amt)
1600 tty_flush_to_ldisc(tty);
1601 if (tty->icanon && !L_EXTPROC(tty)) {
1602 if (tty->canon_data)
1604 } else if (tty->read_cnt >= (amt ? amt : 1))
1611 * copy_from_read_buf - copy read data directly
1612 * @tty: terminal device
1616 * Helper function to speed up n_tty_read. It is only called when
1617 * ICANON is off; it copies characters straight from the tty queue to
1618 * user space directly. It can be profitably called twice; once to
1619 * drain the space from the tail pointer to the (physical) end of the
1620 * buffer, and once to drain the space from the (physical) beginning of
1621 * the buffer to head pointer.
1623 * Called under the tty->atomic_read_lock sem
1627 static int copy_from_read_buf(struct tty_struct *tty,
1628 unsigned char __user **b,
1634 unsigned long flags;
1638 spin_lock_irqsave(&tty->read_lock, flags);
1639 n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
1641 spin_unlock_irqrestore(&tty->read_lock, flags);
1643 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
1646 tty->read_buf[tty->read_tail] == EOF_CHAR(tty);
1647 tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n);
1648 spin_lock_irqsave(&tty->read_lock, flags);
1649 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
1651 /* Turn single EOF into zero-length read */
1652 if (L_EXTPROC(tty) && tty->icanon && is_eof && !tty->read_cnt)
1654 spin_unlock_irqrestore(&tty->read_lock, flags);
1661 extern ssize_t redirected_tty_write(struct file *, const char __user *,
1665 * job_control - check job control
1667 * @file: file handle
1669 * Perform job control management checks on this file/tty descriptor
1670 * and if appropriate send any needed signals and return a negative
1671 * error code if action should be taken.
1674 * Locking: None - redirected write test is safe, testing
1675 * current->signal should possibly lock current->sighand
1679 static int job_control(struct tty_struct *tty, struct file *file)
1681 /* Job control check -- must be done at start and after
1682 every sleep (POSIX.1 7.1.1.4). */
1683 /* NOTE: not yet done after every sleep pending a thorough
1684 check of the logic of this change. -- jlc */
1685 /* don't stop on /dev/console */
1686 if (file->f_op->write != redirected_tty_write &&
1687 current->signal->tty == tty) {
1689 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
1690 else if (task_pgrp(current) != tty->pgrp) {
1691 if (is_ignored(SIGTTIN) ||
1692 is_current_pgrp_orphaned())
1694 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1695 set_thread_flag(TIF_SIGPENDING);
1696 return -ERESTARTSYS;
1704 * n_tty_read - read function for tty
1706 * @file: file object
1707 * @buf: userspace buffer pointer
1710 * Perform reads for the line discipline. We are guaranteed that the
1711 * line discipline will not be closed under us but we may get multiple
1712 * parallel readers and must handle this ourselves. We may also get
1713 * a hangup. Always called in user context, may sleep.
1715 * This code must be sure never to sleep through a hangup.
1718 static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
1719 unsigned char __user *buf, size_t nr)
1721 unsigned char __user *b = buf;
1722 DECLARE_WAITQUEUE(wait, current);
1728 unsigned long flags;
1732 c = job_control(tty, file);
1737 timeout = MAX_SCHEDULE_TIMEOUT;
1739 time = (HZ / 10) * TIME_CHAR(tty);
1740 minimum = MIN_CHAR(tty);
1743 tty->minimum_to_wake = 1;
1744 else if (!waitqueue_active(&tty->read_wait) ||
1745 (tty->minimum_to_wake > minimum))
1746 tty->minimum_to_wake = minimum;
1753 tty->minimum_to_wake = minimum = 1;
1758 * Internal serialization of reads.
1760 if (file->f_flags & O_NONBLOCK) {
1761 if (!mutex_trylock(&tty->atomic_read_lock))
1764 if (mutex_lock_interruptible(&tty->atomic_read_lock))
1765 return -ERESTARTSYS;
1767 packet = tty->packet;
1769 add_wait_queue(&tty->read_wait, &wait);
1771 /* First test for status change. */
1772 if (packet && tty->link->ctrl_status) {
1776 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
1777 cs = tty->link->ctrl_status;
1778 tty->link->ctrl_status = 0;
1779 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
1780 if (tty_put_user(tty, cs, b++)) {
1788 /* This statement must be first before checking for input
1789 so that any interrupt will set the state back to
1791 set_current_state(TASK_INTERRUPTIBLE);
1793 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1794 ((minimum - (b - buf)) >= 1))
1795 tty->minimum_to_wake = (minimum - (b - buf));
1797 if (!input_available_p(tty, 0)) {
1798 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1802 if (tty_hung_up_p(file))
1806 if (file->f_flags & O_NONBLOCK) {
1810 if (signal_pending(current)) {
1811 retval = -ERESTARTSYS;
1814 /* FIXME: does n_tty_set_room need locking ? */
1815 n_tty_set_room(tty);
1816 timeout = schedule_timeout(timeout);
1819 __set_current_state(TASK_RUNNING);
1821 /* Deal with packet mode. */
1822 if (packet && b == buf) {
1823 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
1831 if (tty->icanon && !L_EXTPROC(tty)) {
1832 /* N.B. avoid overrun if nr == 0 */
1833 spin_lock_irqsave(&tty->read_lock, flags);
1834 while (nr && tty->read_cnt) {
1837 eol = test_and_clear_bit(tty->read_tail,
1839 c = tty->read_buf[tty->read_tail];
1840 tty->read_tail = ((tty->read_tail+1) &
1841 (N_TTY_BUF_SIZE-1));
1844 /* this test should be redundant:
1845 * we shouldn't be reading data if
1848 if (--tty->canon_data < 0)
1849 tty->canon_data = 0;
1851 spin_unlock_irqrestore(&tty->read_lock, flags);
1853 if (!eol || (c != __DISABLED_CHAR)) {
1854 if (tty_put_user(tty, c, b++)) {
1857 spin_lock_irqsave(&tty->read_lock, flags);
1863 tty_audit_push(tty);
1864 spin_lock_irqsave(&tty->read_lock, flags);
1867 spin_lock_irqsave(&tty->read_lock, flags);
1869 spin_unlock_irqrestore(&tty->read_lock, flags);
1874 /* The copy function takes the read lock and handles
1875 locking internally for this case */
1876 uncopied = copy_from_read_buf(tty, &b, &nr);
1877 uncopied += copy_from_read_buf(tty, &b, &nr);
1884 /* If there is enough space in the read buffer now, let the
1885 * low-level driver know. We use n_tty_chars_in_buffer() to
1886 * check the buffer, as it now knows about canonical mode.
1887 * Otherwise, if the driver is throttled and the line is
1888 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1889 * we won't get any more characters.
1891 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) {
1892 n_tty_set_room(tty);
1893 check_unthrottle(tty);
1896 if (b - buf >= minimum)
1901 mutex_unlock(&tty->atomic_read_lock);
1902 remove_wait_queue(&tty->read_wait, &wait);
1904 if (!waitqueue_active(&tty->read_wait))
1905 tty->minimum_to_wake = minimum;
1907 __set_current_state(TASK_RUNNING);
1912 clear_bit(TTY_PUSH, &tty->flags);
1913 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1916 n_tty_set_room(tty);
1921 * n_tty_write - write function for tty
1923 * @file: file object
1924 * @buf: userspace buffer pointer
1927 * Write function of the terminal device. This is serialized with
1928 * respect to other write callers but not to termios changes, reads
1929 * and other such events. Since the receive code will echo characters,
1930 * thus calling driver write methods, the output_lock is used in
1931 * the output processing functions called here as well as in the
1932 * echo processing function to protect the column state and space
1933 * left in the buffer.
1935 * This code must be sure never to sleep through a hangup.
1937 * Locking: output_lock to protect column state and space left
1938 * (note that the process_output*() functions take this
1942 static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
1943 const unsigned char *buf, size_t nr)
1945 const unsigned char *b = buf;
1946 DECLARE_WAITQUEUE(wait, current);
1950 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1951 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
1952 retval = tty_check_change(tty);
1957 /* Write out any echoed characters that are still pending */
1958 process_echoes(tty);
1960 add_wait_queue(&tty->write_wait, &wait);
1962 set_current_state(TASK_INTERRUPTIBLE);
1963 if (signal_pending(current)) {
1964 retval = -ERESTARTSYS;
1967 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1971 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1973 ssize_t num = process_output_block(tty, b, nr);
1985 if (process_output(c, tty) < 0)
1989 if (tty->ops->flush_chars)
1990 tty->ops->flush_chars(tty);
1993 c = tty->ops->write(tty, b, nr);
2006 if (file->f_flags & O_NONBLOCK) {
2013 __set_current_state(TASK_RUNNING);
2014 remove_wait_queue(&tty->write_wait, &wait);
2015 if (b - buf != nr && tty->fasync)
2016 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2017 return (b - buf) ? b - buf : retval;
2021 * n_tty_poll - poll method for N_TTY
2022 * @tty: terminal device
2023 * @file: file accessing it
2026 * Called when the line discipline is asked to poll() for data or
2027 * for special events. This code is not serialized with respect to
2028 * other events save open/close.
2030 * This code must be sure never to sleep through a hangup.
2031 * Called without the kernel lock held - fine
2034 static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
2037 unsigned int mask = 0;
2039 poll_wait(file, &tty->read_wait, wait);
2040 poll_wait(file, &tty->write_wait, wait);
2041 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2042 mask |= POLLIN | POLLRDNORM;
2043 if (tty->packet && tty->link->ctrl_status)
2044 mask |= POLLPRI | POLLIN | POLLRDNORM;
2045 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2047 if (tty_hung_up_p(file))
2049 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2050 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2051 tty->minimum_to_wake = MIN_CHAR(tty);
2053 tty->minimum_to_wake = 1;
2055 if (tty->ops->write && !tty_is_writelocked(tty) &&
2056 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2057 tty_write_room(tty) > 0)
2058 mask |= POLLOUT | POLLWRNORM;
2062 static unsigned long inq_canon(struct tty_struct *tty)
2066 if (!tty->canon_data)
2068 head = tty->canon_head;
2069 tail = tty->read_tail;
2070 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2071 /* Skip EOF-chars.. */
2072 while (head != tail) {
2073 if (test_bit(tail, tty->read_flags) &&
2074 tty->read_buf[tail] == __DISABLED_CHAR)
2076 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2081 static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2082 unsigned int cmd, unsigned long arg)
2088 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2090 /* FIXME: Locking */
2091 retval = tty->read_cnt;
2093 retval = inq_canon(tty);
2094 return put_user(retval, (unsigned int __user *) arg);
2096 return n_tty_ioctl_helper(tty, file, cmd, arg);
2100 struct tty_ldisc_ops tty_ldisc_N_TTY = {
2101 .magic = TTY_LDISC_MAGIC,
2104 .close = n_tty_close,
2105 .flush_buffer = n_tty_flush_buffer,
2106 .chars_in_buffer = n_tty_chars_in_buffer,
2108 .write = n_tty_write,
2109 .ioctl = n_tty_ioctl,
2110 .set_termios = n_tty_set_termios,
2112 .receive_buf = n_tty_receive_buf,
2113 .write_wakeup = n_tty_write_wakeup
2117 * n_tty_inherit_ops - inherit N_TTY methods
2118 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2120 * Used by a generic struct tty_ldisc_ops to easily inherit N_TTY
2124 void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2126 *ops = tty_ldisc_N_TTY;
2128 ops->refcount = ops->flags = 0;
2130 EXPORT_SYMBOL_GPL(n_tty_inherit_ops);