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>
53 #include <linux/vmalloc.h>
56 /* number of characters left in xmit buffer before select has we have room */
57 #define WAKEUP_CHARS 256
60 * This defines the low- and high-watermarks for throttling and
61 * unthrottling the TTY driver. These watermarks are used for
62 * controlling the space in the read buffer.
64 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
65 #define TTY_THRESHOLD_UNTHROTTLE 128
68 * Special byte codes used in the echo buffer to represent operations
69 * or special handling of characters. Bytes in the echo buffer that
70 * are not part of such special blocks are treated as normal character
73 #define ECHO_OP_START 0xff
74 #define ECHO_OP_MOVE_BACK_COL 0x80
75 #define ECHO_OP_SET_CANON_COL 0x81
76 #define ECHO_OP_ERASE_TAB 0x82
78 #define ECHO_COMMIT_WATERMARK 256
79 #define ECHO_BLOCK 256
80 #define ECHO_DISCARD_WATERMARK N_TTY_BUF_SIZE - (ECHO_BLOCK + 32)
85 # define n_tty_trace(f, args...) trace_printk(f, ##args)
87 # define n_tty_trace(f, args...)
91 /* producer-published */
98 DECLARE_BITMAP(char_map
, 256);
100 /* private to n_tty_receive_overrun (single-threaded) */
101 unsigned long overrun_time
;
107 /* must hold exclusive termios_rwsem to reset these */
108 unsigned char lnext
:1, erasing
:1, raw
:1, real_raw
:1, icanon
:1;
109 unsigned char push
:1;
111 /* shared by producer and consumer */
112 char read_buf
[N_TTY_BUF_SIZE
];
113 DECLARE_BITMAP(read_flags
, N_TTY_BUF_SIZE
);
114 unsigned char echo_buf
[N_TTY_BUF_SIZE
];
118 /* consumer-published */
122 /* protected by output lock */
124 unsigned int canon_column
;
127 struct mutex atomic_read_lock
;
128 struct mutex output_lock
;
131 #define MASK(x) ((x) & (N_TTY_BUF_SIZE - 1))
133 static inline size_t read_cnt(struct n_tty_data
*ldata
)
135 return ldata
->read_head
- ldata
->read_tail
;
138 static inline unsigned char read_buf(struct n_tty_data
*ldata
, size_t i
)
140 return ldata
->read_buf
[i
& (N_TTY_BUF_SIZE
- 1)];
143 static inline unsigned char *read_buf_addr(struct n_tty_data
*ldata
, size_t i
)
145 return &ldata
->read_buf
[i
& (N_TTY_BUF_SIZE
- 1)];
148 static inline unsigned char echo_buf(struct n_tty_data
*ldata
, size_t i
)
150 smp_rmb(); /* Matches smp_wmb() in add_echo_byte(). */
151 return ldata
->echo_buf
[i
& (N_TTY_BUF_SIZE
- 1)];
154 static inline unsigned char *echo_buf_addr(struct n_tty_data
*ldata
, size_t i
)
156 return &ldata
->echo_buf
[i
& (N_TTY_BUF_SIZE
- 1)];
159 static inline int tty_put_user(struct tty_struct
*tty
, unsigned char x
,
160 unsigned char __user
*ptr
)
162 struct n_tty_data
*ldata
= tty
->disc_data
;
164 tty_audit_add_data(tty
, &x
, 1, ldata
->icanon
);
165 return put_user(x
, ptr
);
168 static inline int tty_copy_to_user(struct tty_struct
*tty
,
173 struct n_tty_data
*ldata
= tty
->disc_data
;
175 tty_audit_add_data(tty
, from
, n
, ldata
->icanon
);
176 return copy_to_user(to
, from
, n
);
180 * n_tty_kick_worker - start input worker (if required)
183 * Re-schedules the flip buffer work if it may have stopped
185 * Caller holds exclusive termios_rwsem
187 * n_tty_read()/consumer path:
188 * holds non-exclusive termios_rwsem
191 static void n_tty_kick_worker(struct tty_struct
*tty
)
193 struct n_tty_data
*ldata
= tty
->disc_data
;
195 /* Did the input worker stop? Restart it */
196 if (unlikely(ldata
->no_room
)) {
199 WARN_RATELIMIT(tty
->port
->itty
== NULL
,
200 "scheduling with invalid itty\n");
201 /* see if ldisc has been killed - if so, this means that
202 * even though the ldisc has been halted and ->buf.work
203 * cancelled, ->buf.work is about to be rescheduled
205 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED
, &tty
->flags
),
206 "scheduling buffer work for halted ldisc\n");
207 tty_buffer_restart_work(tty
->port
);
211 static ssize_t
chars_in_buffer(struct tty_struct
*tty
)
213 struct n_tty_data
*ldata
= tty
->disc_data
;
217 n
= ldata
->commit_head
- ldata
->read_tail
;
219 n
= ldata
->canon_head
- ldata
->read_tail
;
224 * n_tty_write_wakeup - asynchronous I/O notifier
227 * Required for the ptys, serial driver etc. since processes
228 * that attach themselves to the master and rely on ASYNC
229 * IO must be woken up
232 static void n_tty_write_wakeup(struct tty_struct
*tty
)
234 if (tty
->fasync
&& test_and_clear_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
))
235 kill_fasync(&tty
->fasync
, SIGIO
, POLL_OUT
);
238 static void n_tty_check_throttle(struct tty_struct
*tty
)
240 struct n_tty_data
*ldata
= tty
->disc_data
;
243 * Check the remaining room for the input canonicalization
244 * mode. We don't want to throttle the driver if we're in
245 * canonical mode and don't have a newline yet!
247 if (ldata
->icanon
&& ldata
->canon_head
== ldata
->read_tail
)
252 tty_set_flow_change(tty
, TTY_THROTTLE_SAFE
);
253 if (N_TTY_BUF_SIZE
- read_cnt(ldata
) >= TTY_THRESHOLD_THROTTLE
)
255 throttled
= tty_throttle_safe(tty
);
259 __tty_set_flow_change(tty
, 0);
262 static void n_tty_check_unthrottle(struct tty_struct
*tty
)
264 if (tty
->driver
->type
== TTY_DRIVER_TYPE_PTY
) {
265 if (chars_in_buffer(tty
) > TTY_THRESHOLD_UNTHROTTLE
)
269 n_tty_kick_worker(tty
);
270 tty_wakeup(tty
->link
);
274 /* If there is enough space in the read buffer now, let the
275 * low-level driver know. We use chars_in_buffer() to
276 * check the buffer, as it now knows about canonical mode.
277 * Otherwise, if the driver is throttled and the line is
278 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
279 * we won't get any more characters.
284 tty_set_flow_change(tty
, TTY_UNTHROTTLE_SAFE
);
285 if (chars_in_buffer(tty
) > TTY_THRESHOLD_UNTHROTTLE
)
289 n_tty_kick_worker(tty
);
290 unthrottled
= tty_unthrottle_safe(tty
);
294 __tty_set_flow_change(tty
, 0);
298 * put_tty_queue - add character to tty
302 * Add a character to the tty read_buf queue.
304 * n_tty_receive_buf()/producer path:
305 * caller holds non-exclusive termios_rwsem
308 static inline void put_tty_queue(unsigned char c
, struct n_tty_data
*ldata
)
310 *read_buf_addr(ldata
, ldata
->read_head
) = c
;
315 * reset_buffer_flags - reset buffer state
316 * @tty: terminal to reset
318 * Reset the read buffer counters and clear the flags.
319 * Called from n_tty_open() and n_tty_flush_buffer().
321 * Locking: caller holds exclusive termios_rwsem
322 * (or locking is not required)
325 static void reset_buffer_flags(struct n_tty_data
*ldata
)
327 ldata
->read_head
= ldata
->canon_head
= ldata
->read_tail
= 0;
328 ldata
->commit_head
= 0;
329 ldata
->line_start
= 0;
332 bitmap_zero(ldata
->read_flags
, N_TTY_BUF_SIZE
);
336 static void n_tty_packet_mode_flush(struct tty_struct
*tty
)
340 if (tty
->link
->packet
) {
341 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
342 tty
->ctrl_status
|= TIOCPKT_FLUSHREAD
;
343 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
344 wake_up_interruptible(&tty
->link
->read_wait
);
349 * n_tty_flush_buffer - clean input queue
350 * @tty: terminal device
352 * Flush the input buffer. Called when the tty layer wants the
353 * buffer flushed (eg at hangup) or when the N_TTY line discipline
354 * internally has to clean the pending queue (for example some signals).
356 * Holds termios_rwsem to exclude producer/consumer while
357 * buffer indices are reset.
359 * Locking: ctrl_lock, exclusive termios_rwsem
362 static void n_tty_flush_buffer(struct tty_struct
*tty
)
364 down_write(&tty
->termios_rwsem
);
365 reset_buffer_flags(tty
->disc_data
);
366 n_tty_kick_worker(tty
);
369 n_tty_packet_mode_flush(tty
);
370 up_write(&tty
->termios_rwsem
);
374 * n_tty_chars_in_buffer - report available bytes
377 * Report the number of characters buffered to be delivered to user
378 * at this instant in time.
380 * Locking: exclusive termios_rwsem
383 static ssize_t
n_tty_chars_in_buffer(struct tty_struct
*tty
)
387 WARN_ONCE(1, "%s is deprecated and scheduled for removal.", __func__
);
389 down_write(&tty
->termios_rwsem
);
390 n
= chars_in_buffer(tty
);
391 up_write(&tty
->termios_rwsem
);
396 * is_utf8_continuation - utf8 multibyte check
399 * Returns true if the utf8 character 'c' is a multibyte continuation
400 * character. We use this to correctly compute the on screen size
401 * of the character when printing
404 static inline int is_utf8_continuation(unsigned char c
)
406 return (c
& 0xc0) == 0x80;
410 * is_continuation - multibyte check
413 * Returns true if the utf8 character 'c' is a multibyte continuation
414 * character and the terminal is in unicode mode.
417 static inline int is_continuation(unsigned char c
, struct tty_struct
*tty
)
419 return I_IUTF8(tty
) && is_utf8_continuation(c
);
423 * do_output_char - output one character
424 * @c: character (or partial unicode symbol)
425 * @tty: terminal device
426 * @space: space available in tty driver write buffer
428 * This is a helper function that handles one output character
429 * (including special characters like TAB, CR, LF, etc.),
430 * doing OPOST processing and putting the results in the
431 * tty driver's write buffer.
433 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
434 * and NLDLY. They simply aren't relevant in the world today.
435 * If you ever need them, add them here.
437 * Returns the number of bytes of buffer space used or -1 if
440 * Locking: should be called under the output_lock to protect
441 * the column state and space left in the buffer
444 static int do_output_char(unsigned char c
, struct tty_struct
*tty
, int space
)
446 struct n_tty_data
*ldata
= tty
->disc_data
;
459 ldata
->canon_column
= ldata
->column
= 0;
460 tty
->ops
->write(tty
, "\r\n", 2);
463 ldata
->canon_column
= ldata
->column
;
466 if (O_ONOCR(tty
) && ldata
->column
== 0)
471 ldata
->canon_column
= ldata
->column
= 0;
474 ldata
->canon_column
= ldata
->column
= 0;
477 spaces
= 8 - (ldata
->column
& 7);
478 if (O_TABDLY(tty
) == XTABS
) {
481 ldata
->column
+= spaces
;
482 tty
->ops
->write(tty
, " ", spaces
);
485 ldata
->column
+= spaces
;
488 if (ldata
->column
> 0)
495 if (!is_continuation(c
, tty
))
501 tty_put_char(tty
, c
);
506 * process_output - output post processor
507 * @c: character (or partial unicode symbol)
508 * @tty: terminal device
510 * Output one character with OPOST processing.
511 * Returns -1 when the output device is full and the character
514 * Locking: output_lock to protect column state and space left
515 * (also, this is called from n_tty_write under the
516 * tty layer write lock)
519 static int process_output(unsigned char c
, struct tty_struct
*tty
)
521 struct n_tty_data
*ldata
= tty
->disc_data
;
524 mutex_lock(&ldata
->output_lock
);
526 space
= tty_write_room(tty
);
527 retval
= do_output_char(c
, tty
, space
);
529 mutex_unlock(&ldata
->output_lock
);
537 * process_output_block - block post processor
538 * @tty: terminal device
539 * @buf: character buffer
540 * @nr: number of bytes to output
542 * Output a block of characters with OPOST processing.
543 * Returns the number of characters output.
545 * This path is used to speed up block console writes, among other
546 * things when processing blocks of output data. It handles only
547 * the simple cases normally found and helps to generate blocks of
548 * symbols for the console driver and thus improve performance.
550 * Locking: output_lock to protect column state and space left
551 * (also, this is called from n_tty_write under the
552 * tty layer write lock)
555 static ssize_t
process_output_block(struct tty_struct
*tty
,
556 const unsigned char *buf
, unsigned int nr
)
558 struct n_tty_data
*ldata
= tty
->disc_data
;
561 const unsigned char *cp
;
563 mutex_lock(&ldata
->output_lock
);
565 space
= tty_write_room(tty
);
567 mutex_unlock(&ldata
->output_lock
);
573 for (i
= 0, cp
= buf
; i
< nr
; i
++, cp
++) {
574 unsigned char c
= *cp
;
582 ldata
->canon_column
= ldata
->column
;
585 if (O_ONOCR(tty
) && ldata
->column
== 0)
589 ldata
->canon_column
= ldata
->column
= 0;
594 if (ldata
->column
> 0)
601 if (!is_continuation(c
, tty
))
608 i
= tty
->ops
->write(tty
, buf
, i
);
610 mutex_unlock(&ldata
->output_lock
);
615 * process_echoes - write pending echo characters
616 * @tty: terminal device
618 * Write previously buffered echo (and other ldisc-generated)
619 * characters to the tty.
621 * Characters generated by the ldisc (including echoes) need to
622 * be buffered because the driver's write buffer can fill during
623 * heavy program output. Echoing straight to the driver will
624 * often fail under these conditions, causing lost characters and
625 * resulting mismatches of ldisc state information.
627 * Since the ldisc state must represent the characters actually sent
628 * to the driver at the time of the write, operations like certain
629 * changes in column state are also saved in the buffer and executed
632 * A circular fifo buffer is used so that the most recent characters
633 * are prioritized. Also, when control characters are echoed with a
634 * prefixed "^", the pair is treated atomically and thus not separated.
636 * Locking: callers must hold output_lock
639 static size_t __process_echoes(struct tty_struct
*tty
)
641 struct n_tty_data
*ldata
= tty
->disc_data
;
642 int space
, old_space
;
646 old_space
= space
= tty_write_room(tty
);
648 tail
= ldata
->echo_tail
;
649 while (MASK(ldata
->echo_commit
) != MASK(tail
)) {
650 c
= echo_buf(ldata
, tail
);
651 if (c
== ECHO_OP_START
) {
653 int no_space_left
= 0;
656 * Since add_echo_byte() is called without holding
657 * output_lock, we might see only portion of multi-byte
660 if (MASK(ldata
->echo_commit
) == MASK(tail
+ 1))
663 * If the buffer byte is the start of a multi-byte
664 * operation, get the next byte, which is either the
665 * op code or a control character value.
667 op
= echo_buf(ldata
, tail
+ 1);
670 unsigned int num_chars
, num_bs
;
672 case ECHO_OP_ERASE_TAB
:
673 if (MASK(ldata
->echo_commit
) == MASK(tail
+ 2))
675 num_chars
= echo_buf(ldata
, tail
+ 2);
678 * Determine how many columns to go back
679 * in order to erase the tab.
680 * This depends on the number of columns
681 * used by other characters within the tab
682 * area. If this (modulo 8) count is from
683 * the start of input rather than from a
684 * previous tab, we offset by canon column.
685 * Otherwise, tab spacing is normal.
687 if (!(num_chars
& 0x80))
688 num_chars
+= ldata
->canon_column
;
689 num_bs
= 8 - (num_chars
& 7);
691 if (num_bs
> space
) {
697 tty_put_char(tty
, '\b');
698 if (ldata
->column
> 0)
704 case ECHO_OP_SET_CANON_COL
:
705 ldata
->canon_column
= ldata
->column
;
709 case ECHO_OP_MOVE_BACK_COL
:
710 if (ldata
->column
> 0)
716 /* This is an escaped echo op start code */
721 tty_put_char(tty
, ECHO_OP_START
);
729 * If the op is not a special byte code,
730 * it is a ctrl char tagged to be echoed
731 * as "^X" (where X is the letter
732 * representing the control char).
733 * Note that we must ensure there is
734 * enough space for the whole ctrl pair.
741 tty_put_char(tty
, '^');
742 tty_put_char(tty
, op
^ 0100);
752 int retval
= do_output_char(c
, tty
, space
);
759 tty_put_char(tty
, c
);
766 /* If the echo buffer is nearly full (so that the possibility exists
767 * of echo overrun before the next commit), then discard enough
768 * data at the tail to prevent a subsequent overrun */
769 while (ldata
->echo_commit
> tail
&&
770 ldata
->echo_commit
- tail
>= ECHO_DISCARD_WATERMARK
) {
771 if (echo_buf(ldata
, tail
) == ECHO_OP_START
) {
772 if (echo_buf(ldata
, tail
+ 1) == ECHO_OP_ERASE_TAB
)
781 ldata
->echo_tail
= tail
;
782 return old_space
- space
;
785 static void commit_echoes(struct tty_struct
*tty
)
787 struct n_tty_data
*ldata
= tty
->disc_data
;
788 size_t nr
, old
, echoed
;
791 mutex_lock(&ldata
->output_lock
);
792 head
= ldata
->echo_head
;
793 ldata
->echo_mark
= head
;
794 old
= ldata
->echo_commit
- ldata
->echo_tail
;
796 /* Process committed echoes if the accumulated # of bytes
797 * is over the threshold (and try again each time another
798 * block is accumulated) */
799 nr
= head
- ldata
->echo_tail
;
800 if (nr
< ECHO_COMMIT_WATERMARK
||
801 (nr
% ECHO_BLOCK
> old
% ECHO_BLOCK
)) {
802 mutex_unlock(&ldata
->output_lock
);
806 ldata
->echo_commit
= head
;
807 echoed
= __process_echoes(tty
);
808 mutex_unlock(&ldata
->output_lock
);
810 if (echoed
&& tty
->ops
->flush_chars
)
811 tty
->ops
->flush_chars(tty
);
814 static void process_echoes(struct tty_struct
*tty
)
816 struct n_tty_data
*ldata
= tty
->disc_data
;
819 if (ldata
->echo_mark
== ldata
->echo_tail
)
822 mutex_lock(&ldata
->output_lock
);
823 ldata
->echo_commit
= ldata
->echo_mark
;
824 echoed
= __process_echoes(tty
);
825 mutex_unlock(&ldata
->output_lock
);
827 if (echoed
&& tty
->ops
->flush_chars
)
828 tty
->ops
->flush_chars(tty
);
831 /* NB: echo_mark and echo_head should be equivalent here */
832 static void flush_echoes(struct tty_struct
*tty
)
834 struct n_tty_data
*ldata
= tty
->disc_data
;
836 if ((!L_ECHO(tty
) && !L_ECHONL(tty
)) ||
837 ldata
->echo_commit
== ldata
->echo_head
)
840 mutex_lock(&ldata
->output_lock
);
841 ldata
->echo_commit
= ldata
->echo_head
;
842 __process_echoes(tty
);
843 mutex_unlock(&ldata
->output_lock
);
847 * add_echo_byte - add a byte to the echo buffer
848 * @c: unicode byte to echo
851 * Add a character or operation byte to the echo buffer.
854 static inline void add_echo_byte(unsigned char c
, struct n_tty_data
*ldata
)
856 *echo_buf_addr(ldata
, ldata
->echo_head
) = c
;
857 smp_wmb(); /* Matches smp_rmb() in echo_buf(). */
862 * echo_move_back_col - add operation to move back a column
865 * Add an operation to the echo buffer to move back one column.
868 static void echo_move_back_col(struct n_tty_data
*ldata
)
870 add_echo_byte(ECHO_OP_START
, ldata
);
871 add_echo_byte(ECHO_OP_MOVE_BACK_COL
, ldata
);
875 * echo_set_canon_col - add operation to set the canon column
878 * Add an operation to the echo buffer to set the canon column
879 * to the current column.
882 static void echo_set_canon_col(struct n_tty_data
*ldata
)
884 add_echo_byte(ECHO_OP_START
, ldata
);
885 add_echo_byte(ECHO_OP_SET_CANON_COL
, ldata
);
889 * echo_erase_tab - add operation to erase a tab
890 * @num_chars: number of character columns already used
891 * @after_tab: true if num_chars starts after a previous tab
894 * Add an operation to the echo buffer to erase a tab.
896 * Called by the eraser function, which knows how many character
897 * columns have been used since either a previous tab or the start
898 * of input. This information will be used later, along with
899 * canon column (if applicable), to go back the correct number
903 static void echo_erase_tab(unsigned int num_chars
, int after_tab
,
904 struct n_tty_data
*ldata
)
906 add_echo_byte(ECHO_OP_START
, ldata
);
907 add_echo_byte(ECHO_OP_ERASE_TAB
, ldata
);
909 /* We only need to know this modulo 8 (tab spacing) */
912 /* Set the high bit as a flag if num_chars is after a previous tab */
916 add_echo_byte(num_chars
, ldata
);
920 * echo_char_raw - echo a character raw
921 * @c: unicode byte to echo
922 * @tty: terminal device
924 * Echo user input back onto the screen. This must be called only when
925 * L_ECHO(tty) is true. Called from the driver receive_buf path.
927 * This variant does not treat control characters specially.
930 static void echo_char_raw(unsigned char c
, struct n_tty_data
*ldata
)
932 if (c
== ECHO_OP_START
) {
933 add_echo_byte(ECHO_OP_START
, ldata
);
934 add_echo_byte(ECHO_OP_START
, ldata
);
936 add_echo_byte(c
, ldata
);
941 * echo_char - echo a character
942 * @c: unicode byte to echo
943 * @tty: terminal device
945 * Echo user input back onto the screen. This must be called only when
946 * L_ECHO(tty) is true. Called from the driver receive_buf path.
948 * This variant tags control characters to be echoed as "^X"
949 * (where X is the letter representing the control char).
952 static void echo_char(unsigned char c
, struct tty_struct
*tty
)
954 struct n_tty_data
*ldata
= tty
->disc_data
;
956 if (c
== ECHO_OP_START
) {
957 add_echo_byte(ECHO_OP_START
, ldata
);
958 add_echo_byte(ECHO_OP_START
, ldata
);
960 if (L_ECHOCTL(tty
) && iscntrl(c
) && c
!= '\t')
961 add_echo_byte(ECHO_OP_START
, ldata
);
962 add_echo_byte(c
, ldata
);
967 * finish_erasing - complete erase
971 static inline void finish_erasing(struct n_tty_data
*ldata
)
973 if (ldata
->erasing
) {
974 echo_char_raw('/', ldata
);
980 * eraser - handle erase function
981 * @c: character input
982 * @tty: terminal device
984 * Perform erase and necessary output when an erase character is
985 * present in the stream from the driver layer. Handles the complexities
986 * of UTF-8 multibyte symbols.
988 * n_tty_receive_buf()/producer path:
989 * caller holds non-exclusive termios_rwsem
992 static void eraser(unsigned char c
, struct tty_struct
*tty
)
994 struct n_tty_data
*ldata
= tty
->disc_data
;
995 enum { ERASE
, WERASE
, KILL
} kill_type
;
1000 if (ldata
->read_head
== ldata
->canon_head
) {
1001 /* process_output('\a', tty); */ /* what do you think? */
1004 if (c
== ERASE_CHAR(tty
))
1006 else if (c
== WERASE_CHAR(tty
))
1010 ldata
->read_head
= ldata
->canon_head
;
1013 if (!L_ECHOK(tty
) || !L_ECHOKE(tty
) || !L_ECHOE(tty
)) {
1014 ldata
->read_head
= ldata
->canon_head
;
1015 finish_erasing(ldata
);
1016 echo_char(KILL_CHAR(tty
), tty
);
1017 /* Add a newline if ECHOK is on and ECHOKE is off. */
1019 echo_char_raw('\n', ldata
);
1026 while (MASK(ldata
->read_head
) != MASK(ldata
->canon_head
)) {
1027 head
= ldata
->read_head
;
1029 /* erase a single possibly multibyte character */
1032 c
= read_buf(ldata
, head
);
1033 } while (is_continuation(c
, tty
) &&
1034 MASK(head
) != MASK(ldata
->canon_head
));
1036 /* do not partially erase */
1037 if (is_continuation(c
, tty
))
1040 if (kill_type
== WERASE
) {
1041 /* Equivalent to BSD's ALTWERASE. */
1042 if (isalnum(c
) || c
== '_')
1044 else if (seen_alnums
)
1047 cnt
= ldata
->read_head
- head
;
1048 ldata
->read_head
= head
;
1050 if (L_ECHOPRT(tty
)) {
1051 if (!ldata
->erasing
) {
1052 echo_char_raw('\\', ldata
);
1055 /* if cnt > 1, output a multi-byte character */
1059 echo_char_raw(read_buf(ldata
, head
), ldata
);
1060 echo_move_back_col(ldata
);
1062 } else if (kill_type
== ERASE
&& !L_ECHOE(tty
)) {
1063 echo_char(ERASE_CHAR(tty
), tty
);
1064 } else if (c
== '\t') {
1065 unsigned int num_chars
= 0;
1067 size_t tail
= ldata
->read_head
;
1070 * Count the columns used for characters
1071 * since the start of input or after a
1073 * This info is used to go back the correct
1074 * number of columns.
1076 while (MASK(tail
) != MASK(ldata
->canon_head
)) {
1078 c
= read_buf(ldata
, tail
);
1082 } else if (iscntrl(c
)) {
1085 } else if (!is_continuation(c
, tty
)) {
1089 echo_erase_tab(num_chars
, after_tab
, ldata
);
1091 if (iscntrl(c
) && L_ECHOCTL(tty
)) {
1092 echo_char_raw('\b', ldata
);
1093 echo_char_raw(' ', ldata
);
1094 echo_char_raw('\b', ldata
);
1096 if (!iscntrl(c
) || L_ECHOCTL(tty
)) {
1097 echo_char_raw('\b', ldata
);
1098 echo_char_raw(' ', ldata
);
1099 echo_char_raw('\b', ldata
);
1103 if (kill_type
== ERASE
)
1106 if (ldata
->read_head
== ldata
->canon_head
&& L_ECHO(tty
))
1107 finish_erasing(ldata
);
1111 * isig - handle the ISIG optio
1115 * Called when a signal is being sent due to terminal input.
1116 * Called from the driver receive_buf path so serialized.
1118 * Performs input and output flush if !NOFLSH. In this context, the echo
1119 * buffer is 'output'. The signal is processed first to alert any current
1120 * readers or writers to discontinue and exit their i/o loops.
1122 * Locking: ctrl_lock
1125 static void __isig(int sig
, struct tty_struct
*tty
)
1127 struct pid
*tty_pgrp
= tty_get_pgrp(tty
);
1129 kill_pgrp(tty_pgrp
, sig
, 1);
1134 static void isig(int sig
, struct tty_struct
*tty
)
1136 struct n_tty_data
*ldata
= tty
->disc_data
;
1138 if (L_NOFLSH(tty
)) {
1142 } else { /* signal and flush */
1143 up_read(&tty
->termios_rwsem
);
1144 down_write(&tty
->termios_rwsem
);
1148 /* clear echo buffer */
1149 mutex_lock(&ldata
->output_lock
);
1150 ldata
->echo_head
= ldata
->echo_tail
= 0;
1151 ldata
->echo_mark
= ldata
->echo_commit
= 0;
1152 mutex_unlock(&ldata
->output_lock
);
1154 /* clear output buffer */
1155 tty_driver_flush_buffer(tty
);
1157 /* clear input buffer */
1158 reset_buffer_flags(tty
->disc_data
);
1160 /* notify pty master of flush */
1162 n_tty_packet_mode_flush(tty
);
1164 up_write(&tty
->termios_rwsem
);
1165 down_read(&tty
->termios_rwsem
);
1170 * n_tty_receive_break - handle break
1173 * An RS232 break event has been hit in the incoming bitstream. This
1174 * can cause a variety of events depending upon the termios settings.
1176 * n_tty_receive_buf()/producer path:
1177 * caller holds non-exclusive termios_rwsem
1179 * Note: may get exclusive termios_rwsem if flushing input buffer
1182 static void n_tty_receive_break(struct tty_struct
*tty
)
1184 struct n_tty_data
*ldata
= tty
->disc_data
;
1188 if (I_BRKINT(tty
)) {
1192 if (I_PARMRK(tty
)) {
1193 put_tty_queue('\377', ldata
);
1194 put_tty_queue('\0', ldata
);
1196 put_tty_queue('\0', ldata
);
1200 * n_tty_receive_overrun - handle overrun reporting
1203 * Data arrived faster than we could process it. While the tty
1204 * driver has flagged this the bits that were missed are gone
1207 * Called from the receive_buf path so single threaded. Does not
1208 * need locking as num_overrun and overrun_time are function
1212 static void n_tty_receive_overrun(struct tty_struct
*tty
)
1214 struct n_tty_data
*ldata
= tty
->disc_data
;
1216 ldata
->num_overrun
++;
1217 if (time_after(jiffies
, ldata
->overrun_time
+ HZ
) ||
1218 time_after(ldata
->overrun_time
, jiffies
)) {
1219 printk(KERN_WARNING
"%s: %d input overrun(s)\n",
1221 ldata
->num_overrun
);
1222 ldata
->overrun_time
= jiffies
;
1223 ldata
->num_overrun
= 0;
1228 * n_tty_receive_parity_error - error notifier
1229 * @tty: terminal device
1232 * Process a parity error and queue the right data to indicate
1233 * the error case if necessary.
1235 * n_tty_receive_buf()/producer path:
1236 * caller holds non-exclusive termios_rwsem
1238 static void n_tty_receive_parity_error(struct tty_struct
*tty
, unsigned char c
)
1240 struct n_tty_data
*ldata
= tty
->disc_data
;
1245 if (I_PARMRK(tty
)) {
1246 put_tty_queue('\377', ldata
);
1247 put_tty_queue('\0', ldata
);
1248 put_tty_queue(c
, ldata
);
1250 put_tty_queue('\0', ldata
);
1252 put_tty_queue(c
, ldata
);
1256 n_tty_receive_signal_char(struct tty_struct
*tty
, int signal
, unsigned char c
)
1265 process_echoes(tty
);
1270 * n_tty_receive_char - perform processing
1271 * @tty: terminal device
1274 * Process an individual character of input received from the driver.
1275 * This is serialized with respect to itself by the rules for the
1278 * n_tty_receive_buf()/producer path:
1279 * caller holds non-exclusive termios_rwsem
1280 * publishes canon_head if canonical mode is active
1282 * Returns 1 if LNEXT was received, else returns 0
1286 n_tty_receive_char_special(struct tty_struct
*tty
, unsigned char c
)
1288 struct n_tty_data
*ldata
= tty
->disc_data
;
1291 if (c
== START_CHAR(tty
)) {
1293 process_echoes(tty
);
1296 if (c
== STOP_CHAR(tty
)) {
1303 if (c
== INTR_CHAR(tty
)) {
1304 n_tty_receive_signal_char(tty
, SIGINT
, c
);
1306 } else if (c
== QUIT_CHAR(tty
)) {
1307 n_tty_receive_signal_char(tty
, SIGQUIT
, c
);
1309 } else if (c
== SUSP_CHAR(tty
)) {
1310 n_tty_receive_signal_char(tty
, SIGTSTP
, c
);
1315 if (tty
->stopped
&& !tty
->flow_stopped
&& I_IXON(tty
) && I_IXANY(tty
)) {
1317 process_echoes(tty
);
1325 } else if (c
== '\n' && I_INLCR(tty
))
1328 if (ldata
->icanon
) {
1329 if (c
== ERASE_CHAR(tty
) || c
== KILL_CHAR(tty
) ||
1330 (c
== WERASE_CHAR(tty
) && L_IEXTEN(tty
))) {
1335 if (c
== LNEXT_CHAR(tty
) && L_IEXTEN(tty
)) {
1338 finish_erasing(ldata
);
1339 if (L_ECHOCTL(tty
)) {
1340 echo_char_raw('^', ldata
);
1341 echo_char_raw('\b', ldata
);
1347 if (c
== REPRINT_CHAR(tty
) && L_ECHO(tty
) && L_IEXTEN(tty
)) {
1348 size_t tail
= ldata
->canon_head
;
1350 finish_erasing(ldata
);
1352 echo_char_raw('\n', ldata
);
1353 while (MASK(tail
) != MASK(ldata
->read_head
)) {
1354 echo_char(read_buf(ldata
, tail
), tty
);
1361 if (L_ECHO(tty
) || L_ECHONL(tty
)) {
1362 echo_char_raw('\n', ldata
);
1365 goto handle_newline
;
1367 if (c
== EOF_CHAR(tty
)) {
1368 c
= __DISABLED_CHAR
;
1369 goto handle_newline
;
1371 if ((c
== EOL_CHAR(tty
)) ||
1372 (c
== EOL2_CHAR(tty
) && L_IEXTEN(tty
))) {
1374 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1377 /* Record the column of first canon char. */
1378 if (ldata
->canon_head
== ldata
->read_head
)
1379 echo_set_canon_col(ldata
);
1384 * XXX does PARMRK doubling happen for
1385 * EOL_CHAR and EOL2_CHAR?
1387 if (c
== (unsigned char) '\377' && I_PARMRK(tty
))
1388 put_tty_queue(c
, ldata
);
1391 set_bit(ldata
->read_head
& (N_TTY_BUF_SIZE
- 1), ldata
->read_flags
);
1392 put_tty_queue(c
, ldata
);
1393 smp_store_release(&ldata
->canon_head
, ldata
->read_head
);
1394 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
1395 wake_up_interruptible_poll(&tty
->read_wait
, POLLIN
);
1401 finish_erasing(ldata
);
1403 echo_char_raw('\n', ldata
);
1405 /* Record the column of first canon char. */
1406 if (ldata
->canon_head
== ldata
->read_head
)
1407 echo_set_canon_col(ldata
);
1413 /* PARMRK doubling check */
1414 if (c
== (unsigned char) '\377' && I_PARMRK(tty
))
1415 put_tty_queue(c
, ldata
);
1417 put_tty_queue(c
, ldata
);
1422 n_tty_receive_char_inline(struct tty_struct
*tty
, unsigned char c
)
1424 struct n_tty_data
*ldata
= tty
->disc_data
;
1426 if (tty
->stopped
&& !tty
->flow_stopped
&& I_IXON(tty
) && I_IXANY(tty
)) {
1428 process_echoes(tty
);
1431 finish_erasing(ldata
);
1432 /* Record the column of first canon char. */
1433 if (ldata
->canon_head
== ldata
->read_head
)
1434 echo_set_canon_col(ldata
);
1438 /* PARMRK doubling check */
1439 if (c
== (unsigned char) '\377' && I_PARMRK(tty
))
1440 put_tty_queue(c
, ldata
);
1441 put_tty_queue(c
, ldata
);
1444 static void n_tty_receive_char(struct tty_struct
*tty
, unsigned char c
)
1446 n_tty_receive_char_inline(tty
, c
);
1450 n_tty_receive_char_fast(struct tty_struct
*tty
, unsigned char c
)
1452 struct n_tty_data
*ldata
= tty
->disc_data
;
1454 if (tty
->stopped
&& !tty
->flow_stopped
&& I_IXON(tty
) && I_IXANY(tty
)) {
1456 process_echoes(tty
);
1459 finish_erasing(ldata
);
1460 /* Record the column of first canon char. */
1461 if (ldata
->canon_head
== ldata
->read_head
)
1462 echo_set_canon_col(ldata
);
1466 put_tty_queue(c
, ldata
);
1469 static void n_tty_receive_char_closing(struct tty_struct
*tty
, unsigned char c
)
1473 if (I_IUCLC(tty
) && L_IEXTEN(tty
))
1477 if (c
== STOP_CHAR(tty
))
1479 else if (c
== START_CHAR(tty
) ||
1480 (tty
->stopped
&& !tty
->flow_stopped
&& I_IXANY(tty
) &&
1481 c
!= INTR_CHAR(tty
) && c
!= QUIT_CHAR(tty
) &&
1482 c
!= SUSP_CHAR(tty
))) {
1484 process_echoes(tty
);
1490 n_tty_receive_char_flagged(struct tty_struct
*tty
, unsigned char c
, char flag
)
1494 n_tty_receive_break(tty
);
1498 n_tty_receive_parity_error(tty
, c
);
1501 n_tty_receive_overrun(tty
);
1504 printk(KERN_ERR
"%s: unknown flag %d\n",
1505 tty_name(tty
), flag
);
1511 n_tty_receive_char_lnext(struct tty_struct
*tty
, unsigned char c
, char flag
)
1513 struct n_tty_data
*ldata
= tty
->disc_data
;
1516 if (likely(flag
== TTY_NORMAL
)) {
1519 if (I_IUCLC(tty
) && L_IEXTEN(tty
))
1521 n_tty_receive_char(tty
, c
);
1523 n_tty_receive_char_flagged(tty
, c
, flag
);
1527 n_tty_receive_buf_real_raw(struct tty_struct
*tty
, const unsigned char *cp
,
1528 char *fp
, int count
)
1530 struct n_tty_data
*ldata
= tty
->disc_data
;
1533 head
= ldata
->read_head
& (N_TTY_BUF_SIZE
- 1);
1534 n
= min_t(size_t, count
, N_TTY_BUF_SIZE
- head
);
1535 memcpy(read_buf_addr(ldata
, head
), cp
, n
);
1536 ldata
->read_head
+= n
;
1540 head
= ldata
->read_head
& (N_TTY_BUF_SIZE
- 1);
1541 n
= min_t(size_t, count
, N_TTY_BUF_SIZE
- head
);
1542 memcpy(read_buf_addr(ldata
, head
), cp
, n
);
1543 ldata
->read_head
+= n
;
1547 n_tty_receive_buf_raw(struct tty_struct
*tty
, const unsigned char *cp
,
1548 char *fp
, int count
)
1550 struct n_tty_data
*ldata
= tty
->disc_data
;
1551 char flag
= TTY_NORMAL
;
1556 if (likely(flag
== TTY_NORMAL
))
1557 put_tty_queue(*cp
++, ldata
);
1559 n_tty_receive_char_flagged(tty
, *cp
++, flag
);
1564 n_tty_receive_buf_closing(struct tty_struct
*tty
, const unsigned char *cp
,
1565 char *fp
, int count
)
1567 char flag
= TTY_NORMAL
;
1572 if (likely(flag
== TTY_NORMAL
))
1573 n_tty_receive_char_closing(tty
, *cp
++);
1575 n_tty_receive_char_flagged(tty
, *cp
++, flag
);
1580 n_tty_receive_buf_standard(struct tty_struct
*tty
, const unsigned char *cp
,
1581 char *fp
, int count
)
1583 struct n_tty_data
*ldata
= tty
->disc_data
;
1584 char flag
= TTY_NORMAL
;
1589 if (likely(flag
== TTY_NORMAL
)) {
1590 unsigned char c
= *cp
++;
1594 if (I_IUCLC(tty
) && L_IEXTEN(tty
))
1596 if (L_EXTPROC(tty
)) {
1597 put_tty_queue(c
, ldata
);
1600 if (!test_bit(c
, ldata
->char_map
))
1601 n_tty_receive_char_inline(tty
, c
);
1602 else if (n_tty_receive_char_special(tty
, c
) && count
) {
1605 n_tty_receive_char_lnext(tty
, *cp
++, flag
);
1609 n_tty_receive_char_flagged(tty
, *cp
++, flag
);
1614 n_tty_receive_buf_fast(struct tty_struct
*tty
, const unsigned char *cp
,
1615 char *fp
, int count
)
1617 struct n_tty_data
*ldata
= tty
->disc_data
;
1618 char flag
= TTY_NORMAL
;
1623 if (likely(flag
== TTY_NORMAL
)) {
1624 unsigned char c
= *cp
++;
1626 if (!test_bit(c
, ldata
->char_map
))
1627 n_tty_receive_char_fast(tty
, c
);
1628 else if (n_tty_receive_char_special(tty
, c
) && count
) {
1631 n_tty_receive_char_lnext(tty
, *cp
++, flag
);
1635 n_tty_receive_char_flagged(tty
, *cp
++, flag
);
1639 static void __receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
1640 char *fp
, int count
)
1642 struct n_tty_data
*ldata
= tty
->disc_data
;
1643 bool preops
= I_ISTRIP(tty
) || (I_IUCLC(tty
) && L_IEXTEN(tty
));
1645 if (ldata
->real_raw
)
1646 n_tty_receive_buf_real_raw(tty
, cp
, fp
, count
);
1647 else if (ldata
->raw
|| (L_EXTPROC(tty
) && !preops
))
1648 n_tty_receive_buf_raw(tty
, cp
, fp
, count
);
1649 else if (tty
->closing
&& !L_EXTPROC(tty
))
1650 n_tty_receive_buf_closing(tty
, cp
, fp
, count
);
1653 char flag
= TTY_NORMAL
;
1657 n_tty_receive_char_lnext(tty
, *cp
++, flag
);
1661 if (!preops
&& !I_PARMRK(tty
))
1662 n_tty_receive_buf_fast(tty
, cp
, fp
, count
);
1664 n_tty_receive_buf_standard(tty
, cp
, fp
, count
);
1667 if (tty
->ops
->flush_chars
)
1668 tty
->ops
->flush_chars(tty
);
1671 if (ldata
->icanon
&& !L_EXTPROC(tty
))
1674 /* publish read_head to consumer */
1675 smp_store_release(&ldata
->commit_head
, ldata
->read_head
);
1677 if ((read_cnt(ldata
) >= ldata
->minimum_to_wake
) || L_EXTPROC(tty
)) {
1678 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
1679 wake_up_interruptible_poll(&tty
->read_wait
, POLLIN
);
1684 * n_tty_receive_buf_common - process input
1685 * @tty: device to receive input
1687 * @fp: flags for each char (if NULL, all chars are TTY_NORMAL)
1688 * @count: number of input chars in @cp
1690 * Called by the terminal driver when a block of characters has
1691 * been received. This function must be called from soft contexts
1692 * not from interrupt context. The driver is responsible for making
1693 * calls one at a time and in order (or using flush_to_ldisc)
1695 * Returns the # of input chars from @cp which were processed.
1697 * In canonical mode, the maximum line length is 4096 chars (including
1698 * the line termination char); lines longer than 4096 chars are
1699 * truncated. After 4095 chars, input data is still processed but
1700 * not stored. Overflow processing ensures the tty can always
1701 * receive more input until at least one line can be read.
1703 * In non-canonical mode, the read buffer will only accept 4095 chars;
1704 * this provides the necessary space for a newline char if the input
1705 * mode is switched to canonical.
1707 * Note it is possible for the read buffer to _contain_ 4096 chars
1708 * in non-canonical mode: the read buffer could already contain the
1709 * maximum canon line of 4096 chars when the mode is switched to
1712 * n_tty_receive_buf()/producer path:
1713 * claims non-exclusive termios_rwsem
1714 * publishes commit_head or canon_head
1717 n_tty_receive_buf_common(struct tty_struct
*tty
, const unsigned char *cp
,
1718 char *fp
, int count
, int flow
)
1720 struct n_tty_data
*ldata
= tty
->disc_data
;
1721 int room
, n
, rcvd
= 0, overflow
;
1723 down_read(&tty
->termios_rwsem
);
1727 * When PARMRK is set, each input char may take up to 3 chars
1728 * in the read buf; reduce the buffer space avail by 3x
1730 * If we are doing input canonicalization, and there are no
1731 * pending newlines, let characters through without limit, so
1732 * that erase characters will be handled. Other excess
1733 * characters will be beeped.
1735 * paired with store in *_copy_from_read_buf() -- guarantees
1736 * the consumer has loaded the data in read_buf up to the new
1737 * read_tail (so this producer will not overwrite unread data)
1739 size_t tail
= smp_load_acquire(&ldata
->read_tail
);
1741 room
= N_TTY_BUF_SIZE
- (ldata
->read_head
- tail
);
1743 room
= (room
+ 2) / 3;
1746 overflow
= ldata
->icanon
&& ldata
->canon_head
== tail
;
1747 if (overflow
&& room
< 0)
1750 ldata
->no_room
= flow
&& !room
;
1754 n
= min(count
, room
);
1758 /* ignore parity errors if handling overflow */
1759 if (!overflow
|| !fp
|| *fp
!= TTY_PARITY
)
1760 __receive_buf(tty
, cp
, fp
, n
);
1769 tty
->receive_room
= room
;
1771 /* Unthrottle if handling overflow on pty */
1772 if (tty
->driver
->type
== TTY_DRIVER_TYPE_PTY
) {
1774 tty_set_flow_change(tty
, TTY_UNTHROTTLE_SAFE
);
1775 tty_unthrottle_safe(tty
);
1776 __tty_set_flow_change(tty
, 0);
1779 n_tty_check_throttle(tty
);
1781 up_read(&tty
->termios_rwsem
);
1786 static void n_tty_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
1787 char *fp
, int count
)
1789 n_tty_receive_buf_common(tty
, cp
, fp
, count
, 0);
1792 static int n_tty_receive_buf2(struct tty_struct
*tty
, const unsigned char *cp
,
1793 char *fp
, int count
)
1795 return n_tty_receive_buf_common(tty
, cp
, fp
, count
, 1);
1798 int is_ignored(int sig
)
1800 return (sigismember(¤t
->blocked
, sig
) ||
1801 current
->sighand
->action
[sig
-1].sa
.sa_handler
== SIG_IGN
);
1805 * n_tty_set_termios - termios data changed
1807 * @old: previous data
1809 * Called by the tty layer when the user changes termios flags so
1810 * that the line discipline can plan ahead. This function cannot sleep
1811 * and is protected from re-entry by the tty layer. The user is
1812 * guaranteed that this function will not be re-entered or in progress
1813 * when the ldisc is closed.
1815 * Locking: Caller holds tty->termios_rwsem
1818 static void n_tty_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
1820 struct n_tty_data
*ldata
= tty
->disc_data
;
1822 if (!old
|| (old
->c_lflag
^ tty
->termios
.c_lflag
) & (ICANON
| EXTPROC
)) {
1823 bitmap_zero(ldata
->read_flags
, N_TTY_BUF_SIZE
);
1824 ldata
->line_start
= ldata
->read_tail
;
1825 if (!L_ICANON(tty
) || !read_cnt(ldata
)) {
1826 ldata
->canon_head
= ldata
->read_tail
;
1829 set_bit((ldata
->read_head
- 1) & (N_TTY_BUF_SIZE
- 1),
1831 ldata
->canon_head
= ldata
->read_head
;
1834 ldata
->commit_head
= ldata
->read_head
;
1839 ldata
->icanon
= (L_ICANON(tty
) != 0);
1841 if (I_ISTRIP(tty
) || I_IUCLC(tty
) || I_IGNCR(tty
) ||
1842 I_ICRNL(tty
) || I_INLCR(tty
) || L_ICANON(tty
) ||
1843 I_IXON(tty
) || L_ISIG(tty
) || L_ECHO(tty
) ||
1845 bitmap_zero(ldata
->char_map
, 256);
1847 if (I_IGNCR(tty
) || I_ICRNL(tty
))
1848 set_bit('\r', ldata
->char_map
);
1850 set_bit('\n', ldata
->char_map
);
1852 if (L_ICANON(tty
)) {
1853 set_bit(ERASE_CHAR(tty
), ldata
->char_map
);
1854 set_bit(KILL_CHAR(tty
), ldata
->char_map
);
1855 set_bit(EOF_CHAR(tty
), ldata
->char_map
);
1856 set_bit('\n', ldata
->char_map
);
1857 set_bit(EOL_CHAR(tty
), ldata
->char_map
);
1858 if (L_IEXTEN(tty
)) {
1859 set_bit(WERASE_CHAR(tty
), ldata
->char_map
);
1860 set_bit(LNEXT_CHAR(tty
), ldata
->char_map
);
1861 set_bit(EOL2_CHAR(tty
), ldata
->char_map
);
1863 set_bit(REPRINT_CHAR(tty
),
1868 set_bit(START_CHAR(tty
), ldata
->char_map
);
1869 set_bit(STOP_CHAR(tty
), ldata
->char_map
);
1872 set_bit(INTR_CHAR(tty
), ldata
->char_map
);
1873 set_bit(QUIT_CHAR(tty
), ldata
->char_map
);
1874 set_bit(SUSP_CHAR(tty
), ldata
->char_map
);
1876 clear_bit(__DISABLED_CHAR
, ldata
->char_map
);
1878 ldata
->real_raw
= 0;
1881 if ((I_IGNBRK(tty
) || (!I_BRKINT(tty
) && !I_PARMRK(tty
))) &&
1882 (I_IGNPAR(tty
) || !I_INPCK(tty
)) &&
1883 (tty
->driver
->flags
& TTY_DRIVER_REAL_RAW
))
1884 ldata
->real_raw
= 1;
1886 ldata
->real_raw
= 0;
1889 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1890 * been stopped by STOP_CHAR(tty) before it.
1892 if (!I_IXON(tty
) && old
&& (old
->c_iflag
& IXON
) && !tty
->flow_stopped
) {
1894 process_echoes(tty
);
1897 /* The termios change make the tty ready for I/O */
1898 wake_up_interruptible(&tty
->write_wait
);
1899 wake_up_interruptible(&tty
->read_wait
);
1903 * n_tty_close - close the ldisc for this tty
1906 * Called from the terminal layer when this line discipline is
1907 * being shut down, either because of a close or becsuse of a
1908 * discipline change. The function will not be called while other
1909 * ldisc methods are in progress.
1912 static void n_tty_close(struct tty_struct
*tty
)
1914 struct n_tty_data
*ldata
= tty
->disc_data
;
1917 n_tty_packet_mode_flush(tty
);
1920 tty
->disc_data
= NULL
;
1924 * n_tty_open - open an ldisc
1925 * @tty: terminal to open
1927 * Called when this line discipline is being attached to the
1928 * terminal device. Can sleep. Called serialized so that no
1929 * other events will occur in parallel. No further open will occur
1933 static int n_tty_open(struct tty_struct
*tty
)
1935 struct n_tty_data
*ldata
;
1937 /* Currently a malloc failure here can panic */
1938 ldata
= vzalloc(sizeof(*ldata
));
1942 ldata
->overrun_time
= jiffies
;
1943 mutex_init(&ldata
->atomic_read_lock
);
1944 mutex_init(&ldata
->output_lock
);
1946 tty
->disc_data
= ldata
;
1947 ldata
->minimum_to_wake
= 1;
1949 /* indicate buffer work may resume */
1950 clear_bit(TTY_LDISC_HALTED
, &tty
->flags
);
1951 n_tty_set_termios(tty
, NULL
);
1952 tty_unthrottle(tty
);
1956 static inline int input_available_p(struct tty_struct
*tty
, int poll
)
1958 struct n_tty_data
*ldata
= tty
->disc_data
;
1959 int amt
= poll
&& !TIME_CHAR(tty
) && MIN_CHAR(tty
) ? MIN_CHAR(tty
) : 1;
1961 if (ldata
->icanon
&& !L_EXTPROC(tty
))
1962 return ldata
->canon_head
!= ldata
->read_tail
;
1964 return ldata
->commit_head
- ldata
->read_tail
>= amt
;
1968 * copy_from_read_buf - copy read data directly
1969 * @tty: terminal device
1973 * Helper function to speed up n_tty_read. It is only called when
1974 * ICANON is off; it copies characters straight from the tty queue to
1975 * user space directly. It can be profitably called twice; once to
1976 * drain the space from the tail pointer to the (physical) end of the
1977 * buffer, and once to drain the space from the (physical) beginning of
1978 * the buffer to head pointer.
1980 * Called under the ldata->atomic_read_lock sem
1982 * n_tty_read()/consumer path:
1983 * caller holds non-exclusive termios_rwsem
1984 * read_tail published
1987 static int copy_from_read_buf(struct tty_struct
*tty
,
1988 unsigned char __user
**b
,
1992 struct n_tty_data
*ldata
= tty
->disc_data
;
1996 size_t head
= smp_load_acquire(&ldata
->commit_head
);
1997 size_t tail
= ldata
->read_tail
& (N_TTY_BUF_SIZE
- 1);
2000 n
= min(head
- ldata
->read_tail
, N_TTY_BUF_SIZE
- tail
);
2003 retval
= copy_to_user(*b
, read_buf_addr(ldata
, tail
), n
);
2005 is_eof
= n
== 1 && read_buf(ldata
, tail
) == EOF_CHAR(tty
);
2006 tty_audit_add_data(tty
, read_buf_addr(ldata
, tail
), n
,
2008 smp_store_release(&ldata
->read_tail
, ldata
->read_tail
+ n
);
2009 /* Turn single EOF into zero-length read */
2010 if (L_EXTPROC(tty
) && ldata
->icanon
&& is_eof
&&
2011 (head
== ldata
->read_tail
))
2020 * canon_copy_from_read_buf - copy read data in canonical mode
2021 * @tty: terminal device
2025 * Helper function for n_tty_read. It is only called when ICANON is on;
2026 * it copies one line of input up to and including the line-delimiting
2027 * character into the user-space buffer.
2029 * NB: When termios is changed from non-canonical to canonical mode and
2030 * the read buffer contains data, n_tty_set_termios() simulates an EOF
2031 * push (as if C-d were input) _without_ the DISABLED_CHAR in the buffer.
2032 * This causes data already processed as input to be immediately available
2033 * as input although a newline has not been received.
2035 * Called under the atomic_read_lock mutex
2037 * n_tty_read()/consumer path:
2038 * caller holds non-exclusive termios_rwsem
2039 * read_tail published
2042 static int canon_copy_from_read_buf(struct tty_struct
*tty
,
2043 unsigned char __user
**b
,
2046 struct n_tty_data
*ldata
= tty
->disc_data
;
2047 size_t n
, size
, more
, c
;
2052 /* N.B. avoid overrun if nr == 0 */
2056 n
= min(*nr
+ 1, smp_load_acquire(&ldata
->canon_head
) - ldata
->read_tail
);
2058 tail
= ldata
->read_tail
& (N_TTY_BUF_SIZE
- 1);
2059 size
= min_t(size_t, tail
+ n
, N_TTY_BUF_SIZE
);
2061 n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
2062 __func__
, *nr
, tail
, n
, size
);
2064 eol
= find_next_bit(ldata
->read_flags
, size
, tail
);
2065 more
= n
- (size
- tail
);
2066 if (eol
== N_TTY_BUF_SIZE
&& more
) {
2067 /* scan wrapped without finding set bit */
2068 eol
= find_next_bit(ldata
->read_flags
, more
, 0);
2071 } else if (eol
!= size
)
2074 size
= N_TTY_BUF_SIZE
- tail
;
2076 if (n
> N_TTY_BUF_SIZE
)
2077 n
+= N_TTY_BUF_SIZE
;
2080 if (!found
|| read_buf(ldata
, eol
) != __DISABLED_CHAR
) {
2085 n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu size:%zu more:%zu\n",
2086 __func__
, eol
, found
, n
, c
, size
, more
);
2089 ret
= tty_copy_to_user(tty
, *b
, read_buf_addr(ldata
, tail
), size
);
2092 ret
= tty_copy_to_user(tty
, *b
+ size
, ldata
->read_buf
, n
- size
);
2094 ret
= tty_copy_to_user(tty
, *b
, read_buf_addr(ldata
, tail
), n
);
2102 clear_bit(eol
, ldata
->read_flags
);
2103 smp_store_release(&ldata
->read_tail
, ldata
->read_tail
+ c
);
2107 ldata
->line_start
= ldata
->read_tail
;
2110 tty_audit_push(tty
);
2115 extern ssize_t
redirected_tty_write(struct file
*, const char __user
*,
2119 * job_control - check job control
2121 * @file: file handle
2123 * Perform job control management checks on this file/tty descriptor
2124 * and if appropriate send any needed signals and return a negative
2125 * error code if action should be taken.
2127 * Locking: redirected write test is safe
2128 * current->signal->tty check is safe
2129 * ctrl_lock to safely reference tty->pgrp
2132 static int job_control(struct tty_struct
*tty
, struct file
*file
)
2134 /* Job control check -- must be done at start and after
2135 every sleep (POSIX.1 7.1.1.4). */
2136 /* NOTE: not yet done after every sleep pending a thorough
2137 check of the logic of this change. -- jlc */
2138 /* don't stop on /dev/console */
2139 if (file
->f_op
->write
== redirected_tty_write
)
2142 return __tty_check_change(tty
, SIGTTIN
);
2147 * n_tty_read - read function for tty
2149 * @file: file object
2150 * @buf: userspace buffer pointer
2153 * Perform reads for the line discipline. We are guaranteed that the
2154 * line discipline will not be closed under us but we may get multiple
2155 * parallel readers and must handle this ourselves. We may also get
2156 * a hangup. Always called in user context, may sleep.
2158 * This code must be sure never to sleep through a hangup.
2160 * n_tty_read()/consumer path:
2161 * claims non-exclusive termios_rwsem
2162 * publishes read_tail
2165 static ssize_t
n_tty_read(struct tty_struct
*tty
, struct file
*file
,
2166 unsigned char __user
*buf
, size_t nr
)
2168 struct n_tty_data
*ldata
= tty
->disc_data
;
2169 unsigned char __user
*b
= buf
;
2170 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
2178 c
= job_control(tty
, file
);
2183 * Internal serialization of reads.
2185 if (file
->f_flags
& O_NONBLOCK
) {
2186 if (!mutex_trylock(&ldata
->atomic_read_lock
))
2189 if (mutex_lock_interruptible(&ldata
->atomic_read_lock
))
2190 return -ERESTARTSYS
;
2193 down_read(&tty
->termios_rwsem
);
2196 timeout
= MAX_SCHEDULE_TIMEOUT
;
2197 if (!ldata
->icanon
) {
2198 minimum
= MIN_CHAR(tty
);
2200 time
= (HZ
/ 10) * TIME_CHAR(tty
);
2202 ldata
->minimum_to_wake
= 1;
2203 else if (!waitqueue_active(&tty
->read_wait
) ||
2204 (ldata
->minimum_to_wake
> minimum
))
2205 ldata
->minimum_to_wake
= minimum
;
2207 timeout
= (HZ
/ 10) * TIME_CHAR(tty
);
2208 ldata
->minimum_to_wake
= minimum
= 1;
2212 packet
= tty
->packet
;
2213 tail
= ldata
->read_tail
;
2215 add_wait_queue(&tty
->read_wait
, &wait
);
2217 /* First test for status change. */
2218 if (packet
&& tty
->link
->ctrl_status
) {
2222 spin_lock_irq(&tty
->link
->ctrl_lock
);
2223 cs
= tty
->link
->ctrl_status
;
2224 tty
->link
->ctrl_status
= 0;
2225 spin_unlock_irq(&tty
->link
->ctrl_lock
);
2226 if (tty_put_user(tty
, cs
, b
++)) {
2235 if (((minimum
- (b
- buf
)) < ldata
->minimum_to_wake
) &&
2236 ((minimum
- (b
- buf
)) >= 1))
2237 ldata
->minimum_to_wake
= (minimum
- (b
- buf
));
2239 if (!input_available_p(tty
, 0)) {
2240 up_read(&tty
->termios_rwsem
);
2241 tty_buffer_flush_work(tty
->port
);
2242 down_read(&tty
->termios_rwsem
);
2243 if (!input_available_p(tty
, 0)) {
2244 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
)) {
2248 if (tty_hung_up_p(file
))
2251 * Abort readers for ttys which never actually
2252 * get hung up. See __tty_hangup().
2254 if (test_bit(TTY_HUPPING
, &tty
->flags
))
2258 if (file
->f_flags
& O_NONBLOCK
) {
2262 if (signal_pending(current
)) {
2263 retval
= -ERESTARTSYS
;
2266 up_read(&tty
->termios_rwsem
);
2268 timeout
= wait_woken(&wait
, TASK_INTERRUPTIBLE
,
2271 down_read(&tty
->termios_rwsem
);
2276 if (ldata
->icanon
&& !L_EXTPROC(tty
)) {
2277 retval
= canon_copy_from_read_buf(tty
, &b
, &nr
);
2283 /* Deal with packet mode. */
2284 if (packet
&& b
== buf
) {
2285 if (tty_put_user(tty
, TIOCPKT_DATA
, b
++)) {
2293 uncopied
= copy_from_read_buf(tty
, &b
, &nr
);
2294 uncopied
+= copy_from_read_buf(tty
, &b
, &nr
);
2301 n_tty_check_unthrottle(tty
);
2303 if (b
- buf
>= minimum
)
2308 if (tail
!= ldata
->read_tail
)
2309 n_tty_kick_worker(tty
);
2310 up_read(&tty
->termios_rwsem
);
2312 remove_wait_queue(&tty
->read_wait
, &wait
);
2313 if (!waitqueue_active(&tty
->read_wait
))
2314 ldata
->minimum_to_wake
= minimum
;
2316 mutex_unlock(&ldata
->atomic_read_lock
);
2325 * n_tty_write - write function for tty
2327 * @file: file object
2328 * @buf: userspace buffer pointer
2331 * Write function of the terminal device. This is serialized with
2332 * respect to other write callers but not to termios changes, reads
2333 * and other such events. Since the receive code will echo characters,
2334 * thus calling driver write methods, the output_lock is used in
2335 * the output processing functions called here as well as in the
2336 * echo processing function to protect the column state and space
2337 * left in the buffer.
2339 * This code must be sure never to sleep through a hangup.
2341 * Locking: output_lock to protect column state and space left
2342 * (note that the process_output*() functions take this
2346 static ssize_t
n_tty_write(struct tty_struct
*tty
, struct file
*file
,
2347 const unsigned char *buf
, size_t nr
)
2349 const unsigned char *b
= buf
;
2350 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
2354 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2355 if (L_TOSTOP(tty
) && file
->f_op
->write
!= redirected_tty_write
) {
2356 retval
= tty_check_change(tty
);
2361 down_read(&tty
->termios_rwsem
);
2363 /* Write out any echoed characters that are still pending */
2364 process_echoes(tty
);
2366 add_wait_queue(&tty
->write_wait
, &wait
);
2368 if (signal_pending(current
)) {
2369 retval
= -ERESTARTSYS
;
2372 if (tty_hung_up_p(file
) || (tty
->link
&& !tty
->link
->count
)) {
2378 ssize_t num
= process_output_block(tty
, b
, nr
);
2390 if (process_output(c
, tty
) < 0)
2394 if (tty
->ops
->flush_chars
)
2395 tty
->ops
->flush_chars(tty
);
2397 struct n_tty_data
*ldata
= tty
->disc_data
;
2400 mutex_lock(&ldata
->output_lock
);
2401 c
= tty
->ops
->write(tty
, b
, nr
);
2402 mutex_unlock(&ldata
->output_lock
);
2415 if (file
->f_flags
& O_NONBLOCK
) {
2419 up_read(&tty
->termios_rwsem
);
2421 wait_woken(&wait
, TASK_INTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
2423 down_read(&tty
->termios_rwsem
);
2426 remove_wait_queue(&tty
->write_wait
, &wait
);
2427 if (b
- buf
!= nr
&& tty
->fasync
)
2428 set_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
2429 up_read(&tty
->termios_rwsem
);
2430 return (b
- buf
) ? b
- buf
: retval
;
2434 * n_tty_poll - poll method for N_TTY
2435 * @tty: terminal device
2436 * @file: file accessing it
2439 * Called when the line discipline is asked to poll() for data or
2440 * for special events. This code is not serialized with respect to
2441 * other events save open/close.
2443 * This code must be sure never to sleep through a hangup.
2444 * Called without the kernel lock held - fine
2447 static unsigned int n_tty_poll(struct tty_struct
*tty
, struct file
*file
,
2450 struct n_tty_data
*ldata
= tty
->disc_data
;
2451 unsigned int mask
= 0;
2453 poll_wait(file
, &tty
->read_wait
, wait
);
2454 poll_wait(file
, &tty
->write_wait
, wait
);
2455 if (input_available_p(tty
, 1))
2456 mask
|= POLLIN
| POLLRDNORM
;
2458 tty_buffer_flush_work(tty
->port
);
2459 if (input_available_p(tty
, 1))
2460 mask
|= POLLIN
| POLLRDNORM
;
2462 if (tty
->packet
&& tty
->link
->ctrl_status
)
2463 mask
|= POLLPRI
| POLLIN
| POLLRDNORM
;
2464 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
))
2466 if (tty_hung_up_p(file
))
2468 if (!(mask
& (POLLHUP
| POLLIN
| POLLRDNORM
))) {
2469 if (MIN_CHAR(tty
) && !TIME_CHAR(tty
))
2470 ldata
->minimum_to_wake
= MIN_CHAR(tty
);
2472 ldata
->minimum_to_wake
= 1;
2474 if (tty
->ops
->write
&& !tty_is_writelocked(tty
) &&
2475 tty_chars_in_buffer(tty
) < WAKEUP_CHARS
&&
2476 tty_write_room(tty
) > 0)
2477 mask
|= POLLOUT
| POLLWRNORM
;
2481 static unsigned long inq_canon(struct n_tty_data
*ldata
)
2483 size_t nr
, head
, tail
;
2485 if (ldata
->canon_head
== ldata
->read_tail
)
2487 head
= ldata
->canon_head
;
2488 tail
= ldata
->read_tail
;
2490 /* Skip EOF-chars.. */
2491 while (MASK(head
) != MASK(tail
)) {
2492 if (test_bit(tail
& (N_TTY_BUF_SIZE
- 1), ldata
->read_flags
) &&
2493 read_buf(ldata
, tail
) == __DISABLED_CHAR
)
2500 static int n_tty_ioctl(struct tty_struct
*tty
, struct file
*file
,
2501 unsigned int cmd
, unsigned long arg
)
2503 struct n_tty_data
*ldata
= tty
->disc_data
;
2508 return put_user(tty_chars_in_buffer(tty
), (int __user
*) arg
);
2510 down_write(&tty
->termios_rwsem
);
2511 if (L_ICANON(tty
) && !L_EXTPROC(tty
))
2512 retval
= inq_canon(ldata
);
2514 retval
= read_cnt(ldata
);
2515 up_write(&tty
->termios_rwsem
);
2516 return put_user(retval
, (unsigned int __user
*) arg
);
2518 return n_tty_ioctl_helper(tty
, file
, cmd
, arg
);
2522 static void n_tty_fasync(struct tty_struct
*tty
, int on
)
2524 struct n_tty_data
*ldata
= tty
->disc_data
;
2526 if (!waitqueue_active(&tty
->read_wait
)) {
2528 ldata
->minimum_to_wake
= 1;
2529 else if (!tty
->fasync
)
2530 ldata
->minimum_to_wake
= N_TTY_BUF_SIZE
;
2534 struct tty_ldisc_ops tty_ldisc_N_TTY
= {
2535 .magic
= TTY_LDISC_MAGIC
,
2538 .close
= n_tty_close
,
2539 .flush_buffer
= n_tty_flush_buffer
,
2540 .chars_in_buffer
= n_tty_chars_in_buffer
,
2542 .write
= n_tty_write
,
2543 .ioctl
= n_tty_ioctl
,
2544 .set_termios
= n_tty_set_termios
,
2546 .receive_buf
= n_tty_receive_buf
,
2547 .write_wakeup
= n_tty_write_wakeup
,
2548 .fasync
= n_tty_fasync
,
2549 .receive_buf2
= n_tty_receive_buf2
,
2553 * n_tty_inherit_ops - inherit N_TTY methods
2554 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2556 * Enables a 'subclass' line discipline to 'inherit' N_TTY
2560 void n_tty_inherit_ops(struct tty_ldisc_ops
*ops
)
2562 *ops
= tty_ldisc_N_TTY
;
2564 ops
->refcount
= ops
->flags
= 0;
2566 EXPORT_SYMBOL_GPL(n_tty_inherit_ops
);