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 /* If we are not echoing the data, perhaps this is a secret so erase it */
169 static inline void zero_buffer(struct tty_struct *tty, u8 *buffer, int size)
171 bool icanon = !!L_ICANON(tty);
172 bool no_echo = !L_ECHO(tty);
174 if (icanon && no_echo)
175 memset(buffer, 0x00, size);
178 static inline int tty_copy_to_user(struct tty_struct *tty,
183 struct n_tty_data *ldata = tty->disc_data;
186 tty_audit_add_data(tty, from, n, ldata->icanon);
187 retval = copy_to_user(to, from, n);
189 zero_buffer(tty, from, n);
194 * n_tty_kick_worker - start input worker (if required)
197 * Re-schedules the flip buffer work if it may have stopped
199 * Caller holds exclusive termios_rwsem
201 * n_tty_read()/consumer path:
202 * holds non-exclusive termios_rwsem
205 static void n_tty_kick_worker(struct tty_struct *tty)
207 struct n_tty_data *ldata = tty->disc_data;
209 /* Did the input worker stop? Restart it */
210 if (unlikely(ldata->no_room)) {
213 WARN_RATELIMIT(tty->port->itty == NULL,
214 "scheduling with invalid itty\n");
215 /* see if ldisc has been killed - if so, this means that
216 * even though the ldisc has been halted and ->buf.work
217 * cancelled, ->buf.work is about to be rescheduled
219 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
220 "scheduling buffer work for halted ldisc\n");
221 tty_buffer_restart_work(tty->port);
225 static ssize_t chars_in_buffer(struct tty_struct *tty)
227 struct n_tty_data *ldata = tty->disc_data;
231 n = ldata->commit_head - ldata->read_tail;
233 n = ldata->canon_head - ldata->read_tail;
238 * n_tty_write_wakeup - asynchronous I/O notifier
241 * Required for the ptys, serial driver etc. since processes
242 * that attach themselves to the master and rely on ASYNC
243 * IO must be woken up
246 static void n_tty_write_wakeup(struct tty_struct *tty)
248 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
249 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
252 static void n_tty_check_throttle(struct tty_struct *tty)
254 struct n_tty_data *ldata = tty->disc_data;
257 * Check the remaining room for the input canonicalization
258 * mode. We don't want to throttle the driver if we're in
259 * canonical mode and don't have a newline yet!
261 if (ldata->icanon && ldata->canon_head == ldata->read_tail)
266 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
267 if (N_TTY_BUF_SIZE - read_cnt(ldata) >= TTY_THRESHOLD_THROTTLE)
269 throttled = tty_throttle_safe(tty);
273 __tty_set_flow_change(tty, 0);
276 static void n_tty_check_unthrottle(struct tty_struct *tty)
278 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
279 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
283 n_tty_kick_worker(tty);
284 tty_wakeup(tty->link);
288 /* If there is enough space in the read buffer now, let the
289 * low-level driver know. We use chars_in_buffer() to
290 * check the buffer, as it now knows about canonical mode.
291 * Otherwise, if the driver is throttled and the line is
292 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
293 * we won't get any more characters.
298 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
299 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
303 n_tty_kick_worker(tty);
304 unthrottled = tty_unthrottle_safe(tty);
308 __tty_set_flow_change(tty, 0);
312 * put_tty_queue - add character to tty
316 * Add a character to the tty read_buf queue.
318 * n_tty_receive_buf()/producer path:
319 * caller holds non-exclusive termios_rwsem
322 static inline void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
324 *read_buf_addr(ldata, ldata->read_head) = c;
329 * reset_buffer_flags - reset buffer state
330 * @tty: terminal to reset
332 * Reset the read buffer counters and clear the flags.
333 * Called from n_tty_open() and n_tty_flush_buffer().
335 * Locking: caller holds exclusive termios_rwsem
336 * (or locking is not required)
339 static void reset_buffer_flags(struct n_tty_data *ldata)
341 ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
342 ldata->commit_head = 0;
343 ldata->line_start = 0;
346 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
350 static void n_tty_packet_mode_flush(struct tty_struct *tty)
354 if (tty->link->packet) {
355 spin_lock_irqsave(&tty->ctrl_lock, flags);
356 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
357 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
358 wake_up_interruptible(&tty->link->read_wait);
363 * n_tty_flush_buffer - clean input queue
364 * @tty: terminal device
366 * Flush the input buffer. Called when the tty layer wants the
367 * buffer flushed (eg at hangup) or when the N_TTY line discipline
368 * internally has to clean the pending queue (for example some signals).
370 * Holds termios_rwsem to exclude producer/consumer while
371 * buffer indices are reset.
373 * Locking: ctrl_lock, exclusive termios_rwsem
376 static void n_tty_flush_buffer(struct tty_struct *tty)
378 down_write(&tty->termios_rwsem);
379 reset_buffer_flags(tty->disc_data);
380 n_tty_kick_worker(tty);
383 n_tty_packet_mode_flush(tty);
384 up_write(&tty->termios_rwsem);
388 * n_tty_chars_in_buffer - report available bytes
391 * Report the number of characters buffered to be delivered to user
392 * at this instant in time.
394 * Locking: exclusive termios_rwsem
397 static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
401 WARN_ONCE(1, "%s is deprecated and scheduled for removal.", __func__);
403 down_write(&tty->termios_rwsem);
404 n = chars_in_buffer(tty);
405 up_write(&tty->termios_rwsem);
410 * is_utf8_continuation - utf8 multibyte check
413 * Returns true if the utf8 character 'c' is a multibyte continuation
414 * character. We use this to correctly compute the on screen size
415 * of the character when printing
418 static inline int is_utf8_continuation(unsigned char c)
420 return (c & 0xc0) == 0x80;
424 * is_continuation - multibyte check
427 * Returns true if the utf8 character 'c' is a multibyte continuation
428 * character and the terminal is in unicode mode.
431 static inline int is_continuation(unsigned char c, struct tty_struct *tty)
433 return I_IUTF8(tty) && is_utf8_continuation(c);
437 * do_output_char - output one character
438 * @c: character (or partial unicode symbol)
439 * @tty: terminal device
440 * @space: space available in tty driver write buffer
442 * This is a helper function that handles one output character
443 * (including special characters like TAB, CR, LF, etc.),
444 * doing OPOST processing and putting the results in the
445 * tty driver's write buffer.
447 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
448 * and NLDLY. They simply aren't relevant in the world today.
449 * If you ever need them, add them here.
451 * Returns the number of bytes of buffer space used or -1 if
454 * Locking: should be called under the output_lock to protect
455 * the column state and space left in the buffer
458 static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
460 struct n_tty_data *ldata = tty->disc_data;
473 ldata->canon_column = ldata->column = 0;
474 tty->ops->write(tty, "\r\n", 2);
477 ldata->canon_column = ldata->column;
480 if (O_ONOCR(tty) && ldata->column == 0)
485 ldata->canon_column = ldata->column = 0;
488 ldata->canon_column = ldata->column = 0;
491 spaces = 8 - (ldata->column & 7);
492 if (O_TABDLY(tty) == XTABS) {
495 ldata->column += spaces;
496 tty->ops->write(tty, " ", spaces);
499 ldata->column += spaces;
502 if (ldata->column > 0)
509 if (!is_continuation(c, tty))
515 tty_put_char(tty, c);
520 * process_output - output post processor
521 * @c: character (or partial unicode symbol)
522 * @tty: terminal device
524 * Output one character with OPOST processing.
525 * Returns -1 when the output device is full and the character
528 * Locking: output_lock to protect column state and space left
529 * (also, this is called from n_tty_write under the
530 * tty layer write lock)
533 static int process_output(unsigned char c, struct tty_struct *tty)
535 struct n_tty_data *ldata = tty->disc_data;
538 mutex_lock(&ldata->output_lock);
540 space = tty_write_room(tty);
541 retval = do_output_char(c, tty, space);
543 mutex_unlock(&ldata->output_lock);
551 * process_output_block - block post processor
552 * @tty: terminal device
553 * @buf: character buffer
554 * @nr: number of bytes to output
556 * Output a block of characters with OPOST processing.
557 * Returns the number of characters output.
559 * This path is used to speed up block console writes, among other
560 * things when processing blocks of output data. It handles only
561 * the simple cases normally found and helps to generate blocks of
562 * symbols for the console driver and thus improve performance.
564 * Locking: output_lock to protect column state and space left
565 * (also, this is called from n_tty_write under the
566 * tty layer write lock)
569 static ssize_t process_output_block(struct tty_struct *tty,
570 const unsigned char *buf, unsigned int nr)
572 struct n_tty_data *ldata = tty->disc_data;
575 const unsigned char *cp;
577 mutex_lock(&ldata->output_lock);
579 space = tty_write_room(tty);
581 mutex_unlock(&ldata->output_lock);
587 for (i = 0, cp = buf; i < nr; i++, cp++) {
588 unsigned char c = *cp;
596 ldata->canon_column = ldata->column;
599 if (O_ONOCR(tty) && ldata->column == 0)
603 ldata->canon_column = ldata->column = 0;
608 if (ldata->column > 0)
615 if (!is_continuation(c, tty))
622 i = tty->ops->write(tty, buf, i);
624 mutex_unlock(&ldata->output_lock);
629 * process_echoes - write pending echo characters
630 * @tty: terminal device
632 * Write previously buffered echo (and other ldisc-generated)
633 * characters to the tty.
635 * Characters generated by the ldisc (including echoes) need to
636 * be buffered because the driver's write buffer can fill during
637 * heavy program output. Echoing straight to the driver will
638 * often fail under these conditions, causing lost characters and
639 * resulting mismatches of ldisc state information.
641 * Since the ldisc state must represent the characters actually sent
642 * to the driver at the time of the write, operations like certain
643 * changes in column state are also saved in the buffer and executed
646 * A circular fifo buffer is used so that the most recent characters
647 * are prioritized. Also, when control characters are echoed with a
648 * prefixed "^", the pair is treated atomically and thus not separated.
650 * Locking: callers must hold output_lock
653 static size_t __process_echoes(struct tty_struct *tty)
655 struct n_tty_data *ldata = tty->disc_data;
656 int space, old_space;
660 old_space = space = tty_write_room(tty);
662 tail = ldata->echo_tail;
663 while (MASK(ldata->echo_commit) != MASK(tail)) {
664 c = echo_buf(ldata, tail);
665 if (c == ECHO_OP_START) {
667 int no_space_left = 0;
670 * Since add_echo_byte() is called without holding
671 * output_lock, we might see only portion of multi-byte
674 if (MASK(ldata->echo_commit) == MASK(tail + 1))
677 * If the buffer byte is the start of a multi-byte
678 * operation, get the next byte, which is either the
679 * op code or a control character value.
681 op = echo_buf(ldata, tail + 1);
684 unsigned int num_chars, num_bs;
686 case ECHO_OP_ERASE_TAB:
687 if (MASK(ldata->echo_commit) == MASK(tail + 2))
689 num_chars = echo_buf(ldata, tail + 2);
692 * Determine how many columns to go back
693 * in order to erase the tab.
694 * This depends on the number of columns
695 * used by other characters within the tab
696 * area. If this (modulo 8) count is from
697 * the start of input rather than from a
698 * previous tab, we offset by canon column.
699 * Otherwise, tab spacing is normal.
701 if (!(num_chars & 0x80))
702 num_chars += ldata->canon_column;
703 num_bs = 8 - (num_chars & 7);
705 if (num_bs > space) {
711 tty_put_char(tty, '\b');
712 if (ldata->column > 0)
718 case ECHO_OP_SET_CANON_COL:
719 ldata->canon_column = ldata->column;
723 case ECHO_OP_MOVE_BACK_COL:
724 if (ldata->column > 0)
730 /* This is an escaped echo op start code */
735 tty_put_char(tty, ECHO_OP_START);
743 * If the op is not a special byte code,
744 * it is a ctrl char tagged to be echoed
745 * as "^X" (where X is the letter
746 * representing the control char).
747 * Note that we must ensure there is
748 * enough space for the whole ctrl pair.
755 tty_put_char(tty, '^');
756 tty_put_char(tty, op ^ 0100);
766 int retval = do_output_char(c, tty, space);
773 tty_put_char(tty, c);
780 /* If the echo buffer is nearly full (so that the possibility exists
781 * of echo overrun before the next commit), then discard enough
782 * data at the tail to prevent a subsequent overrun */
783 while (ldata->echo_commit > tail &&
784 ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) {
785 if (echo_buf(ldata, tail) == ECHO_OP_START) {
786 if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB)
795 ldata->echo_tail = tail;
796 return old_space - space;
799 static void commit_echoes(struct tty_struct *tty)
801 struct n_tty_data *ldata = tty->disc_data;
802 size_t nr, old, echoed;
805 mutex_lock(&ldata->output_lock);
806 head = ldata->echo_head;
807 ldata->echo_mark = head;
808 old = ldata->echo_commit - ldata->echo_tail;
810 /* Process committed echoes if the accumulated # of bytes
811 * is over the threshold (and try again each time another
812 * block is accumulated) */
813 nr = head - ldata->echo_tail;
814 if (nr < ECHO_COMMIT_WATERMARK ||
815 (nr % ECHO_BLOCK > old % ECHO_BLOCK)) {
816 mutex_unlock(&ldata->output_lock);
820 ldata->echo_commit = head;
821 echoed = __process_echoes(tty);
822 mutex_unlock(&ldata->output_lock);
824 if (echoed && tty->ops->flush_chars)
825 tty->ops->flush_chars(tty);
828 static void process_echoes(struct tty_struct *tty)
830 struct n_tty_data *ldata = tty->disc_data;
833 if (ldata->echo_mark == ldata->echo_tail)
836 mutex_lock(&ldata->output_lock);
837 ldata->echo_commit = ldata->echo_mark;
838 echoed = __process_echoes(tty);
839 mutex_unlock(&ldata->output_lock);
841 if (echoed && tty->ops->flush_chars)
842 tty->ops->flush_chars(tty);
845 /* NB: echo_mark and echo_head should be equivalent here */
846 static void flush_echoes(struct tty_struct *tty)
848 struct n_tty_data *ldata = tty->disc_data;
850 if ((!L_ECHO(tty) && !L_ECHONL(tty)) ||
851 ldata->echo_commit == ldata->echo_head)
854 mutex_lock(&ldata->output_lock);
855 ldata->echo_commit = ldata->echo_head;
856 __process_echoes(tty);
857 mutex_unlock(&ldata->output_lock);
861 * add_echo_byte - add a byte to the echo buffer
862 * @c: unicode byte to echo
865 * Add a character or operation byte to the echo buffer.
868 static inline void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
870 *echo_buf_addr(ldata, ldata->echo_head) = c;
871 smp_wmb(); /* Matches smp_rmb() in echo_buf(). */
876 * echo_move_back_col - add operation to move back a column
879 * Add an operation to the echo buffer to move back one column.
882 static void echo_move_back_col(struct n_tty_data *ldata)
884 add_echo_byte(ECHO_OP_START, ldata);
885 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
889 * echo_set_canon_col - add operation to set the canon column
892 * Add an operation to the echo buffer to set the canon column
893 * to the current column.
896 static void echo_set_canon_col(struct n_tty_data *ldata)
898 add_echo_byte(ECHO_OP_START, ldata);
899 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
903 * echo_erase_tab - add operation to erase a tab
904 * @num_chars: number of character columns already used
905 * @after_tab: true if num_chars starts after a previous tab
908 * Add an operation to the echo buffer to erase a tab.
910 * Called by the eraser function, which knows how many character
911 * columns have been used since either a previous tab or the start
912 * of input. This information will be used later, along with
913 * canon column (if applicable), to go back the correct number
917 static void echo_erase_tab(unsigned int num_chars, int after_tab,
918 struct n_tty_data *ldata)
920 add_echo_byte(ECHO_OP_START, ldata);
921 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
923 /* We only need to know this modulo 8 (tab spacing) */
926 /* Set the high bit as a flag if num_chars is after a previous tab */
930 add_echo_byte(num_chars, ldata);
934 * echo_char_raw - echo a character raw
935 * @c: unicode byte to echo
936 * @tty: terminal device
938 * Echo user input back onto the screen. This must be called only when
939 * L_ECHO(tty) is true. Called from the driver receive_buf path.
941 * This variant does not treat control characters specially.
944 static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
946 if (c == ECHO_OP_START) {
947 add_echo_byte(ECHO_OP_START, ldata);
948 add_echo_byte(ECHO_OP_START, ldata);
950 add_echo_byte(c, ldata);
955 * echo_char - echo a character
956 * @c: unicode byte to echo
957 * @tty: terminal device
959 * Echo user input back onto the screen. This must be called only when
960 * L_ECHO(tty) is true. Called from the driver receive_buf path.
962 * This variant tags control characters to be echoed as "^X"
963 * (where X is the letter representing the control char).
966 static void echo_char(unsigned char c, struct tty_struct *tty)
968 struct n_tty_data *ldata = tty->disc_data;
970 if (c == ECHO_OP_START) {
971 add_echo_byte(ECHO_OP_START, ldata);
972 add_echo_byte(ECHO_OP_START, ldata);
974 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
975 add_echo_byte(ECHO_OP_START, ldata);
976 add_echo_byte(c, ldata);
981 * finish_erasing - complete erase
985 static inline void finish_erasing(struct n_tty_data *ldata)
987 if (ldata->erasing) {
988 echo_char_raw('/', ldata);
994 * eraser - handle erase function
995 * @c: character input
996 * @tty: terminal device
998 * Perform erase and necessary output when an erase character is
999 * present in the stream from the driver layer. Handles the complexities
1000 * of UTF-8 multibyte symbols.
1002 * n_tty_receive_buf()/producer path:
1003 * caller holds non-exclusive termios_rwsem
1006 static void eraser(unsigned char c, struct tty_struct *tty)
1008 struct n_tty_data *ldata = tty->disc_data;
1009 enum { ERASE, WERASE, KILL } kill_type;
1014 if (ldata->read_head == ldata->canon_head) {
1015 /* process_output('\a', tty); */ /* what do you think? */
1018 if (c == ERASE_CHAR(tty))
1020 else if (c == WERASE_CHAR(tty))
1024 ldata->read_head = ldata->canon_head;
1027 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
1028 ldata->read_head = ldata->canon_head;
1029 finish_erasing(ldata);
1030 echo_char(KILL_CHAR(tty), tty);
1031 /* Add a newline if ECHOK is on and ECHOKE is off. */
1033 echo_char_raw('\n', ldata);
1040 while (MASK(ldata->read_head) != MASK(ldata->canon_head)) {
1041 head = ldata->read_head;
1043 /* erase a single possibly multibyte character */
1046 c = read_buf(ldata, head);
1047 } while (is_continuation(c, tty) &&
1048 MASK(head) != MASK(ldata->canon_head));
1050 /* do not partially erase */
1051 if (is_continuation(c, tty))
1054 if (kill_type == WERASE) {
1055 /* Equivalent to BSD's ALTWERASE. */
1056 if (isalnum(c) || c == '_')
1058 else if (seen_alnums)
1061 cnt = ldata->read_head - head;
1062 ldata->read_head = head;
1064 if (L_ECHOPRT(tty)) {
1065 if (!ldata->erasing) {
1066 echo_char_raw('\\', ldata);
1069 /* if cnt > 1, output a multi-byte character */
1073 echo_char_raw(read_buf(ldata, head), ldata);
1074 echo_move_back_col(ldata);
1076 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
1077 echo_char(ERASE_CHAR(tty), tty);
1078 } else if (c == '\t') {
1079 unsigned int num_chars = 0;
1081 size_t tail = ldata->read_head;
1084 * Count the columns used for characters
1085 * since the start of input or after a
1087 * This info is used to go back the correct
1088 * number of columns.
1090 while (MASK(tail) != MASK(ldata->canon_head)) {
1092 c = read_buf(ldata, tail);
1096 } else if (iscntrl(c)) {
1099 } else if (!is_continuation(c, tty)) {
1103 echo_erase_tab(num_chars, after_tab, ldata);
1105 if (iscntrl(c) && L_ECHOCTL(tty)) {
1106 echo_char_raw('\b', ldata);
1107 echo_char_raw(' ', ldata);
1108 echo_char_raw('\b', ldata);
1110 if (!iscntrl(c) || L_ECHOCTL(tty)) {
1111 echo_char_raw('\b', ldata);
1112 echo_char_raw(' ', ldata);
1113 echo_char_raw('\b', ldata);
1117 if (kill_type == ERASE)
1120 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
1121 finish_erasing(ldata);
1125 * isig - handle the ISIG optio
1129 * Called when a signal is being sent due to terminal input.
1130 * Called from the driver receive_buf path so serialized.
1132 * Performs input and output flush if !NOFLSH. In this context, the echo
1133 * buffer is 'output'. The signal is processed first to alert any current
1134 * readers or writers to discontinue and exit their i/o loops.
1136 * Locking: ctrl_lock
1139 static void __isig(int sig, struct tty_struct *tty)
1141 struct pid *tty_pgrp = tty_get_pgrp(tty);
1143 kill_pgrp(tty_pgrp, sig, 1);
1148 static void isig(int sig, struct tty_struct *tty)
1150 struct n_tty_data *ldata = tty->disc_data;
1152 if (L_NOFLSH(tty)) {
1156 } else { /* signal and flush */
1157 up_read(&tty->termios_rwsem);
1158 down_write(&tty->termios_rwsem);
1162 /* clear echo buffer */
1163 mutex_lock(&ldata->output_lock);
1164 ldata->echo_head = ldata->echo_tail = 0;
1165 ldata->echo_mark = ldata->echo_commit = 0;
1166 mutex_unlock(&ldata->output_lock);
1168 /* clear output buffer */
1169 tty_driver_flush_buffer(tty);
1171 /* clear input buffer */
1172 reset_buffer_flags(tty->disc_data);
1174 /* notify pty master of flush */
1176 n_tty_packet_mode_flush(tty);
1178 up_write(&tty->termios_rwsem);
1179 down_read(&tty->termios_rwsem);
1184 * n_tty_receive_break - handle break
1187 * An RS232 break event has been hit in the incoming bitstream. This
1188 * can cause a variety of events depending upon the termios settings.
1190 * n_tty_receive_buf()/producer path:
1191 * caller holds non-exclusive termios_rwsem
1193 * Note: may get exclusive termios_rwsem if flushing input buffer
1196 static void n_tty_receive_break(struct tty_struct *tty)
1198 struct n_tty_data *ldata = tty->disc_data;
1202 if (I_BRKINT(tty)) {
1206 if (I_PARMRK(tty)) {
1207 put_tty_queue('\377', ldata);
1208 put_tty_queue('\0', ldata);
1210 put_tty_queue('\0', ldata);
1214 * n_tty_receive_overrun - handle overrun reporting
1217 * Data arrived faster than we could process it. While the tty
1218 * driver has flagged this the bits that were missed are gone
1221 * Called from the receive_buf path so single threaded. Does not
1222 * need locking as num_overrun and overrun_time are function
1226 static void n_tty_receive_overrun(struct tty_struct *tty)
1228 struct n_tty_data *ldata = tty->disc_data;
1230 ldata->num_overrun++;
1231 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1232 time_after(ldata->overrun_time, jiffies)) {
1233 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1235 ldata->num_overrun);
1236 ldata->overrun_time = jiffies;
1237 ldata->num_overrun = 0;
1242 * n_tty_receive_parity_error - error notifier
1243 * @tty: terminal device
1246 * Process a parity error and queue the right data to indicate
1247 * the error case if necessary.
1249 * n_tty_receive_buf()/producer path:
1250 * caller holds non-exclusive termios_rwsem
1252 static void n_tty_receive_parity_error(struct tty_struct *tty, unsigned char c)
1254 struct n_tty_data *ldata = tty->disc_data;
1259 if (I_PARMRK(tty)) {
1260 put_tty_queue('\377', ldata);
1261 put_tty_queue('\0', ldata);
1262 put_tty_queue(c, ldata);
1264 put_tty_queue('\0', ldata);
1266 put_tty_queue(c, ldata);
1270 n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
1279 process_echoes(tty);
1284 * n_tty_receive_char - perform processing
1285 * @tty: terminal device
1288 * Process an individual character of input received from the driver.
1289 * This is serialized with respect to itself by the rules for the
1292 * n_tty_receive_buf()/producer path:
1293 * caller holds non-exclusive termios_rwsem
1294 * publishes canon_head if canonical mode is active
1296 * Returns 1 if LNEXT was received, else returns 0
1300 n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
1302 struct n_tty_data *ldata = tty->disc_data;
1305 if (c == START_CHAR(tty)) {
1307 process_echoes(tty);
1310 if (c == STOP_CHAR(tty)) {
1317 if (c == INTR_CHAR(tty)) {
1318 n_tty_receive_signal_char(tty, SIGINT, c);
1320 } else if (c == QUIT_CHAR(tty)) {
1321 n_tty_receive_signal_char(tty, SIGQUIT, c);
1323 } else if (c == SUSP_CHAR(tty)) {
1324 n_tty_receive_signal_char(tty, SIGTSTP, c);
1329 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1331 process_echoes(tty);
1339 } else if (c == '\n' && I_INLCR(tty))
1342 if (ldata->icanon) {
1343 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1344 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1349 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
1352 finish_erasing(ldata);
1353 if (L_ECHOCTL(tty)) {
1354 echo_char_raw('^', ldata);
1355 echo_char_raw('\b', ldata);
1361 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) {
1362 size_t tail = ldata->canon_head;
1364 finish_erasing(ldata);
1366 echo_char_raw('\n', ldata);
1367 while (MASK(tail) != MASK(ldata->read_head)) {
1368 echo_char(read_buf(ldata, tail), tty);
1375 if (L_ECHO(tty) || L_ECHONL(tty)) {
1376 echo_char_raw('\n', ldata);
1379 goto handle_newline;
1381 if (c == EOF_CHAR(tty)) {
1382 c = __DISABLED_CHAR;
1383 goto handle_newline;
1385 if ((c == EOL_CHAR(tty)) ||
1386 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1388 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1391 /* Record the column of first canon char. */
1392 if (ldata->canon_head == ldata->read_head)
1393 echo_set_canon_col(ldata);
1398 * XXX does PARMRK doubling happen for
1399 * EOL_CHAR and EOL2_CHAR?
1401 if (c == (unsigned char) '\377' && I_PARMRK(tty))
1402 put_tty_queue(c, ldata);
1405 set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags);
1406 put_tty_queue(c, ldata);
1407 smp_store_release(&ldata->canon_head, ldata->read_head);
1408 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1409 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
1415 finish_erasing(ldata);
1417 echo_char_raw('\n', ldata);
1419 /* Record the column of first canon char. */
1420 if (ldata->canon_head == ldata->read_head)
1421 echo_set_canon_col(ldata);
1427 /* PARMRK doubling check */
1428 if (c == (unsigned char) '\377' && I_PARMRK(tty))
1429 put_tty_queue(c, ldata);
1431 put_tty_queue(c, ldata);
1436 n_tty_receive_char_inline(struct tty_struct *tty, unsigned char c)
1438 struct n_tty_data *ldata = tty->disc_data;
1440 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1442 process_echoes(tty);
1445 finish_erasing(ldata);
1446 /* Record the column of first canon char. */
1447 if (ldata->canon_head == ldata->read_head)
1448 echo_set_canon_col(ldata);
1452 /* PARMRK doubling check */
1453 if (c == (unsigned char) '\377' && I_PARMRK(tty))
1454 put_tty_queue(c, ldata);
1455 put_tty_queue(c, ldata);
1458 static void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1460 n_tty_receive_char_inline(tty, c);
1464 n_tty_receive_char_fast(struct tty_struct *tty, unsigned char c)
1466 struct n_tty_data *ldata = tty->disc_data;
1468 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1470 process_echoes(tty);
1473 finish_erasing(ldata);
1474 /* Record the column of first canon char. */
1475 if (ldata->canon_head == ldata->read_head)
1476 echo_set_canon_col(ldata);
1480 put_tty_queue(c, ldata);
1483 static void n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c)
1487 if (I_IUCLC(tty) && L_IEXTEN(tty))
1491 if (c == STOP_CHAR(tty))
1493 else if (c == START_CHAR(tty) ||
1494 (tty->stopped && !tty->flow_stopped && I_IXANY(tty) &&
1495 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
1496 c != SUSP_CHAR(tty))) {
1498 process_echoes(tty);
1504 n_tty_receive_char_flagged(struct tty_struct *tty, unsigned char c, char flag)
1508 n_tty_receive_break(tty);
1512 n_tty_receive_parity_error(tty, c);
1515 n_tty_receive_overrun(tty);
1518 printk(KERN_ERR "%s: unknown flag %d\n",
1519 tty_name(tty), flag);
1525 n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
1527 struct n_tty_data *ldata = tty->disc_data;
1530 if (likely(flag == TTY_NORMAL)) {
1533 if (I_IUCLC(tty) && L_IEXTEN(tty))
1535 n_tty_receive_char(tty, c);
1537 n_tty_receive_char_flagged(tty, c, flag);
1541 n_tty_receive_buf_real_raw(struct tty_struct *tty, const unsigned char *cp,
1542 char *fp, int count)
1544 struct n_tty_data *ldata = tty->disc_data;
1547 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
1548 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
1549 memcpy(read_buf_addr(ldata, head), cp, n);
1550 ldata->read_head += n;
1554 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
1555 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
1556 memcpy(read_buf_addr(ldata, head), cp, n);
1557 ldata->read_head += n;
1561 n_tty_receive_buf_raw(struct tty_struct *tty, const unsigned char *cp,
1562 char *fp, int count)
1564 struct n_tty_data *ldata = tty->disc_data;
1565 char flag = TTY_NORMAL;
1570 if (likely(flag == TTY_NORMAL))
1571 put_tty_queue(*cp++, ldata);
1573 n_tty_receive_char_flagged(tty, *cp++, flag);
1578 n_tty_receive_buf_closing(struct tty_struct *tty, const unsigned char *cp,
1579 char *fp, int count)
1581 char flag = TTY_NORMAL;
1586 if (likely(flag == TTY_NORMAL))
1587 n_tty_receive_char_closing(tty, *cp++);
1589 n_tty_receive_char_flagged(tty, *cp++, flag);
1594 n_tty_receive_buf_standard(struct tty_struct *tty, const unsigned char *cp,
1595 char *fp, int count)
1597 struct n_tty_data *ldata = tty->disc_data;
1598 char flag = TTY_NORMAL;
1603 if (likely(flag == TTY_NORMAL)) {
1604 unsigned char c = *cp++;
1608 if (I_IUCLC(tty) && L_IEXTEN(tty))
1610 if (L_EXTPROC(tty)) {
1611 put_tty_queue(c, ldata);
1614 if (!test_bit(c, ldata->char_map))
1615 n_tty_receive_char_inline(tty, c);
1616 else if (n_tty_receive_char_special(tty, c) && count) {
1619 n_tty_receive_char_lnext(tty, *cp++, flag);
1623 n_tty_receive_char_flagged(tty, *cp++, flag);
1628 n_tty_receive_buf_fast(struct tty_struct *tty, const unsigned char *cp,
1629 char *fp, int count)
1631 struct n_tty_data *ldata = tty->disc_data;
1632 char flag = TTY_NORMAL;
1637 if (likely(flag == TTY_NORMAL)) {
1638 unsigned char c = *cp++;
1640 if (!test_bit(c, ldata->char_map))
1641 n_tty_receive_char_fast(tty, c);
1642 else if (n_tty_receive_char_special(tty, c) && count) {
1645 n_tty_receive_char_lnext(tty, *cp++, flag);
1649 n_tty_receive_char_flagged(tty, *cp++, flag);
1653 static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1654 char *fp, int count)
1656 struct n_tty_data *ldata = tty->disc_data;
1657 bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
1659 if (ldata->real_raw)
1660 n_tty_receive_buf_real_raw(tty, cp, fp, count);
1661 else if (ldata->raw || (L_EXTPROC(tty) && !preops))
1662 n_tty_receive_buf_raw(tty, cp, fp, count);
1663 else if (tty->closing && !L_EXTPROC(tty))
1664 n_tty_receive_buf_closing(tty, cp, fp, count);
1667 char flag = TTY_NORMAL;
1671 n_tty_receive_char_lnext(tty, *cp++, flag);
1675 if (!preops && !I_PARMRK(tty))
1676 n_tty_receive_buf_fast(tty, cp, fp, count);
1678 n_tty_receive_buf_standard(tty, cp, fp, count);
1681 if (tty->ops->flush_chars)
1682 tty->ops->flush_chars(tty);
1685 if (ldata->icanon && !L_EXTPROC(tty))
1688 /* publish read_head to consumer */
1689 smp_store_release(&ldata->commit_head, ldata->read_head);
1691 if ((read_cnt(ldata) >= ldata->minimum_to_wake) || L_EXTPROC(tty)) {
1692 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1693 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
1698 * n_tty_receive_buf_common - process input
1699 * @tty: device to receive input
1701 * @fp: flags for each char (if NULL, all chars are TTY_NORMAL)
1702 * @count: number of input chars in @cp
1704 * Called by the terminal driver when a block of characters has
1705 * been received. This function must be called from soft contexts
1706 * not from interrupt context. The driver is responsible for making
1707 * calls one at a time and in order (or using flush_to_ldisc)
1709 * Returns the # of input chars from @cp which were processed.
1711 * In canonical mode, the maximum line length is 4096 chars (including
1712 * the line termination char); lines longer than 4096 chars are
1713 * truncated. After 4095 chars, input data is still processed but
1714 * not stored. Overflow processing ensures the tty can always
1715 * receive more input until at least one line can be read.
1717 * In non-canonical mode, the read buffer will only accept 4095 chars;
1718 * this provides the necessary space for a newline char if the input
1719 * mode is switched to canonical.
1721 * Note it is possible for the read buffer to _contain_ 4096 chars
1722 * in non-canonical mode: the read buffer could already contain the
1723 * maximum canon line of 4096 chars when the mode is switched to
1726 * n_tty_receive_buf()/producer path:
1727 * claims non-exclusive termios_rwsem
1728 * publishes commit_head or canon_head
1731 n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp,
1732 char *fp, int count, int flow)
1734 struct n_tty_data *ldata = tty->disc_data;
1735 int room, n, rcvd = 0, overflow;
1737 down_read(&tty->termios_rwsem);
1741 * When PARMRK is set, each input char may take up to 3 chars
1742 * in the read buf; reduce the buffer space avail by 3x
1744 * If we are doing input canonicalization, and there are no
1745 * pending newlines, let characters through without limit, so
1746 * that erase characters will be handled. Other excess
1747 * characters will be beeped.
1749 * paired with store in *_copy_from_read_buf() -- guarantees
1750 * the consumer has loaded the data in read_buf up to the new
1751 * read_tail (so this producer will not overwrite unread data)
1753 size_t tail = smp_load_acquire(&ldata->read_tail);
1755 room = N_TTY_BUF_SIZE - (ldata->read_head - tail);
1757 room = (room + 2) / 3;
1760 overflow = ldata->icanon && ldata->canon_head == tail;
1761 if (overflow && room < 0)
1764 ldata->no_room = flow && !room;
1768 n = min(count, room);
1772 /* ignore parity errors if handling overflow */
1773 if (!overflow || !fp || *fp != TTY_PARITY)
1774 __receive_buf(tty, cp, fp, n);
1783 tty->receive_room = room;
1785 /* Unthrottle if handling overflow on pty */
1786 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
1788 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1789 tty_unthrottle_safe(tty);
1790 __tty_set_flow_change(tty, 0);
1793 n_tty_check_throttle(tty);
1795 up_read(&tty->termios_rwsem);
1800 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1801 char *fp, int count)
1803 n_tty_receive_buf_common(tty, cp, fp, count, 0);
1806 static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
1807 char *fp, int count)
1809 return n_tty_receive_buf_common(tty, cp, fp, count, 1);
1812 int is_ignored(int sig)
1814 return (sigismember(¤t->blocked, sig) ||
1815 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
1819 * n_tty_set_termios - termios data changed
1821 * @old: previous data
1823 * Called by the tty layer when the user changes termios flags so
1824 * that the line discipline can plan ahead. This function cannot sleep
1825 * and is protected from re-entry by the tty layer. The user is
1826 * guaranteed that this function will not be re-entered or in progress
1827 * when the ldisc is closed.
1829 * Locking: Caller holds tty->termios_rwsem
1832 static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1834 struct n_tty_data *ldata = tty->disc_data;
1836 if (!old || (old->c_lflag ^ tty->termios.c_lflag) & (ICANON | EXTPROC)) {
1837 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
1838 ldata->line_start = ldata->read_tail;
1839 if (!L_ICANON(tty) || !read_cnt(ldata)) {
1840 ldata->canon_head = ldata->read_tail;
1843 set_bit((ldata->read_head - 1) & (N_TTY_BUF_SIZE - 1),
1845 ldata->canon_head = ldata->read_head;
1848 ldata->commit_head = ldata->read_head;
1853 ldata->icanon = (L_ICANON(tty) != 0);
1855 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1856 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1857 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1859 bitmap_zero(ldata->char_map, 256);
1861 if (I_IGNCR(tty) || I_ICRNL(tty))
1862 set_bit('\r', ldata->char_map);
1864 set_bit('\n', ldata->char_map);
1866 if (L_ICANON(tty)) {
1867 set_bit(ERASE_CHAR(tty), ldata->char_map);
1868 set_bit(KILL_CHAR(tty), ldata->char_map);
1869 set_bit(EOF_CHAR(tty), ldata->char_map);
1870 set_bit('\n', ldata->char_map);
1871 set_bit(EOL_CHAR(tty), ldata->char_map);
1872 if (L_IEXTEN(tty)) {
1873 set_bit(WERASE_CHAR(tty), ldata->char_map);
1874 set_bit(LNEXT_CHAR(tty), ldata->char_map);
1875 set_bit(EOL2_CHAR(tty), ldata->char_map);
1877 set_bit(REPRINT_CHAR(tty),
1882 set_bit(START_CHAR(tty), ldata->char_map);
1883 set_bit(STOP_CHAR(tty), ldata->char_map);
1886 set_bit(INTR_CHAR(tty), ldata->char_map);
1887 set_bit(QUIT_CHAR(tty), ldata->char_map);
1888 set_bit(SUSP_CHAR(tty), ldata->char_map);
1890 clear_bit(__DISABLED_CHAR, ldata->char_map);
1892 ldata->real_raw = 0;
1895 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1896 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1897 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
1898 ldata->real_raw = 1;
1900 ldata->real_raw = 0;
1903 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1904 * been stopped by STOP_CHAR(tty) before it.
1906 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
1908 process_echoes(tty);
1911 /* The termios change make the tty ready for I/O */
1912 wake_up_interruptible(&tty->write_wait);
1913 wake_up_interruptible(&tty->read_wait);
1917 * n_tty_close - close the ldisc for this tty
1920 * Called from the terminal layer when this line discipline is
1921 * being shut down, either because of a close or becsuse of a
1922 * discipline change. The function will not be called while other
1923 * ldisc methods are in progress.
1926 static void n_tty_close(struct tty_struct *tty)
1928 struct n_tty_data *ldata = tty->disc_data;
1931 n_tty_packet_mode_flush(tty);
1934 tty->disc_data = NULL;
1938 * n_tty_open - open an ldisc
1939 * @tty: terminal to open
1941 * Called when this line discipline is being attached to the
1942 * terminal device. Can sleep. Called serialized so that no
1943 * other events will occur in parallel. No further open will occur
1947 static int n_tty_open(struct tty_struct *tty)
1949 struct n_tty_data *ldata;
1951 /* Currently a malloc failure here can panic */
1952 ldata = vzalloc(sizeof(*ldata));
1956 ldata->overrun_time = jiffies;
1957 mutex_init(&ldata->atomic_read_lock);
1958 mutex_init(&ldata->output_lock);
1960 tty->disc_data = ldata;
1961 ldata->minimum_to_wake = 1;
1963 /* indicate buffer work may resume */
1964 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1965 n_tty_set_termios(tty, NULL);
1966 tty_unthrottle(tty);
1970 static inline int input_available_p(struct tty_struct *tty, int poll)
1972 struct n_tty_data *ldata = tty->disc_data;
1973 int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1;
1975 if (ldata->icanon && !L_EXTPROC(tty))
1976 return ldata->canon_head != ldata->read_tail;
1978 return ldata->commit_head - ldata->read_tail >= amt;
1982 * copy_from_read_buf - copy read data directly
1983 * @tty: terminal device
1987 * Helper function to speed up n_tty_read. It is only called when
1988 * ICANON is off; it copies characters straight from the tty queue to
1989 * user space directly. It can be profitably called twice; once to
1990 * drain the space from the tail pointer to the (physical) end of the
1991 * buffer, and once to drain the space from the (physical) beginning of
1992 * the buffer to head pointer.
1994 * Called under the ldata->atomic_read_lock sem
1996 * n_tty_read()/consumer path:
1997 * caller holds non-exclusive termios_rwsem
1998 * read_tail published
2001 static int copy_from_read_buf(struct tty_struct *tty,
2002 unsigned char __user **b,
2006 struct n_tty_data *ldata = tty->disc_data;
2010 size_t head = smp_load_acquire(&ldata->commit_head);
2011 size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
2014 n = min(head - ldata->read_tail, N_TTY_BUF_SIZE - tail);
2017 retval = copy_to_user(*b, read_buf_addr(ldata, tail), n);
2019 is_eof = n == 1 && read_buf(ldata, tail) == EOF_CHAR(tty);
2020 tty_audit_add_data(tty, read_buf_addr(ldata, tail), n,
2022 zero_buffer(tty, read_buf_addr(ldata, tail), n);
2023 smp_store_release(&ldata->read_tail, ldata->read_tail + n);
2024 /* Turn single EOF into zero-length read */
2025 if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
2026 (head == ldata->read_tail))
2035 * canon_copy_from_read_buf - copy read data in canonical mode
2036 * @tty: terminal device
2040 * Helper function for n_tty_read. It is only called when ICANON is on;
2041 * it copies one line of input up to and including the line-delimiting
2042 * character into the user-space buffer.
2044 * NB: When termios is changed from non-canonical to canonical mode and
2045 * the read buffer contains data, n_tty_set_termios() simulates an EOF
2046 * push (as if C-d were input) _without_ the DISABLED_CHAR in the buffer.
2047 * This causes data already processed as input to be immediately available
2048 * as input although a newline has not been received.
2050 * Called under the atomic_read_lock mutex
2052 * n_tty_read()/consumer path:
2053 * caller holds non-exclusive termios_rwsem
2054 * read_tail published
2057 static int canon_copy_from_read_buf(struct tty_struct *tty,
2058 unsigned char __user **b,
2061 struct n_tty_data *ldata = tty->disc_data;
2062 size_t n, size, more, c;
2067 /* N.B. avoid overrun if nr == 0 */
2071 n = min(*nr + 1, smp_load_acquire(&ldata->canon_head) - ldata->read_tail);
2073 tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
2074 size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
2076 n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
2077 __func__, *nr, tail, n, size);
2079 eol = find_next_bit(ldata->read_flags, size, tail);
2080 more = n - (size - tail);
2081 if (eol == N_TTY_BUF_SIZE && more) {
2082 /* scan wrapped without finding set bit */
2083 eol = find_next_bit(ldata->read_flags, more, 0);
2086 } else if (eol != size)
2089 size = N_TTY_BUF_SIZE - tail;
2091 if (n > N_TTY_BUF_SIZE)
2092 n += N_TTY_BUF_SIZE;
2095 if (!found || read_buf(ldata, eol) != __DISABLED_CHAR) {
2100 n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu size:%zu more:%zu\n",
2101 __func__, eol, found, n, c, size, more);
2104 ret = tty_copy_to_user(tty, *b, read_buf_addr(ldata, tail), size);
2107 ret = tty_copy_to_user(tty, *b + size, ldata->read_buf, n - size);
2109 ret = tty_copy_to_user(tty, *b, read_buf_addr(ldata, tail), n);
2117 clear_bit(eol, ldata->read_flags);
2118 smp_store_release(&ldata->read_tail, ldata->read_tail + c);
2122 ldata->line_start = ldata->read_tail;
2125 tty_audit_push(tty);
2130 extern ssize_t redirected_tty_write(struct file *, const char __user *,
2134 * job_control - check job control
2136 * @file: file handle
2138 * Perform job control management checks on this file/tty descriptor
2139 * and if appropriate send any needed signals and return a negative
2140 * error code if action should be taken.
2142 * Locking: redirected write test is safe
2143 * current->signal->tty check is safe
2144 * ctrl_lock to safely reference tty->pgrp
2147 static int job_control(struct tty_struct *tty, struct file *file)
2149 /* Job control check -- must be done at start and after
2150 every sleep (POSIX.1 7.1.1.4). */
2151 /* NOTE: not yet done after every sleep pending a thorough
2152 check of the logic of this change. -- jlc */
2153 /* don't stop on /dev/console */
2154 if (file->f_op->write == redirected_tty_write)
2157 return __tty_check_change(tty, SIGTTIN);
2162 * n_tty_read - read function for tty
2164 * @file: file object
2165 * @buf: userspace buffer pointer
2168 * Perform reads for the line discipline. We are guaranteed that the
2169 * line discipline will not be closed under us but we may get multiple
2170 * parallel readers and must handle this ourselves. We may also get
2171 * a hangup. Always called in user context, may sleep.
2173 * This code must be sure never to sleep through a hangup.
2175 * n_tty_read()/consumer path:
2176 * claims non-exclusive termios_rwsem
2177 * publishes read_tail
2180 static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
2181 unsigned char __user *buf, size_t nr)
2183 struct n_tty_data *ldata = tty->disc_data;
2184 unsigned char __user *b = buf;
2185 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2193 c = job_control(tty, file);
2198 * Internal serialization of reads.
2200 if (file->f_flags & O_NONBLOCK) {
2201 if (!mutex_trylock(&ldata->atomic_read_lock))
2204 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
2205 return -ERESTARTSYS;
2208 down_read(&tty->termios_rwsem);
2211 timeout = MAX_SCHEDULE_TIMEOUT;
2212 if (!ldata->icanon) {
2213 minimum = MIN_CHAR(tty);
2215 time = (HZ / 10) * TIME_CHAR(tty);
2217 ldata->minimum_to_wake = 1;
2218 else if (!waitqueue_active(&tty->read_wait) ||
2219 (ldata->minimum_to_wake > minimum))
2220 ldata->minimum_to_wake = minimum;
2222 timeout = (HZ / 10) * TIME_CHAR(tty);
2223 ldata->minimum_to_wake = minimum = 1;
2227 packet = tty->packet;
2228 tail = ldata->read_tail;
2230 add_wait_queue(&tty->read_wait, &wait);
2232 /* First test for status change. */
2233 if (packet && tty->link->ctrl_status) {
2237 spin_lock_irq(&tty->link->ctrl_lock);
2238 cs = tty->link->ctrl_status;
2239 tty->link->ctrl_status = 0;
2240 spin_unlock_irq(&tty->link->ctrl_lock);
2241 if (tty_put_user(tty, cs, b++)) {
2250 if (((minimum - (b - buf)) < ldata->minimum_to_wake) &&
2251 ((minimum - (b - buf)) >= 1))
2252 ldata->minimum_to_wake = (minimum - (b - buf));
2254 if (!input_available_p(tty, 0)) {
2255 up_read(&tty->termios_rwsem);
2256 tty_buffer_flush_work(tty->port);
2257 down_read(&tty->termios_rwsem);
2258 if (!input_available_p(tty, 0)) {
2259 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
2263 if (tty_hung_up_p(file))
2266 * Abort readers for ttys which never actually
2267 * get hung up. See __tty_hangup().
2269 if (test_bit(TTY_HUPPING, &tty->flags))
2273 if (file->f_flags & O_NONBLOCK) {
2277 if (signal_pending(current)) {
2278 retval = -ERESTARTSYS;
2281 up_read(&tty->termios_rwsem);
2283 timeout = wait_woken(&wait, TASK_INTERRUPTIBLE,
2286 down_read(&tty->termios_rwsem);
2291 if (ldata->icanon && !L_EXTPROC(tty)) {
2292 retval = canon_copy_from_read_buf(tty, &b, &nr);
2298 /* Deal with packet mode. */
2299 if (packet && b == buf) {
2300 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
2308 uncopied = copy_from_read_buf(tty, &b, &nr);
2309 uncopied += copy_from_read_buf(tty, &b, &nr);
2316 n_tty_check_unthrottle(tty);
2318 if (b - buf >= minimum)
2323 if (tail != ldata->read_tail)
2324 n_tty_kick_worker(tty);
2325 up_read(&tty->termios_rwsem);
2327 remove_wait_queue(&tty->read_wait, &wait);
2328 if (!waitqueue_active(&tty->read_wait))
2329 ldata->minimum_to_wake = minimum;
2331 mutex_unlock(&ldata->atomic_read_lock);
2340 * n_tty_write - write function for tty
2342 * @file: file object
2343 * @buf: userspace buffer pointer
2346 * Write function of the terminal device. This is serialized with
2347 * respect to other write callers but not to termios changes, reads
2348 * and other such events. Since the receive code will echo characters,
2349 * thus calling driver write methods, the output_lock is used in
2350 * the output processing functions called here as well as in the
2351 * echo processing function to protect the column state and space
2352 * left in the buffer.
2354 * This code must be sure never to sleep through a hangup.
2356 * Locking: output_lock to protect column state and space left
2357 * (note that the process_output*() functions take this
2361 static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
2362 const unsigned char *buf, size_t nr)
2364 const unsigned char *b = buf;
2365 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2369 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2370 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2371 retval = tty_check_change(tty);
2376 down_read(&tty->termios_rwsem);
2378 /* Write out any echoed characters that are still pending */
2379 process_echoes(tty);
2381 add_wait_queue(&tty->write_wait, &wait);
2383 if (signal_pending(current)) {
2384 retval = -ERESTARTSYS;
2387 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2393 ssize_t num = process_output_block(tty, b, nr);
2405 if (process_output(c, tty) < 0)
2409 if (tty->ops->flush_chars)
2410 tty->ops->flush_chars(tty);
2412 struct n_tty_data *ldata = tty->disc_data;
2415 mutex_lock(&ldata->output_lock);
2416 c = tty->ops->write(tty, b, nr);
2417 mutex_unlock(&ldata->output_lock);
2430 if (file->f_flags & O_NONBLOCK) {
2434 up_read(&tty->termios_rwsem);
2436 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
2438 down_read(&tty->termios_rwsem);
2441 remove_wait_queue(&tty->write_wait, &wait);
2442 if (b - buf != nr && tty->fasync)
2443 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2444 up_read(&tty->termios_rwsem);
2445 return (b - buf) ? b - buf : retval;
2449 * n_tty_poll - poll method for N_TTY
2450 * @tty: terminal device
2451 * @file: file accessing it
2454 * Called when the line discipline is asked to poll() for data or
2455 * for special events. This code is not serialized with respect to
2456 * other events save open/close.
2458 * This code must be sure never to sleep through a hangup.
2459 * Called without the kernel lock held - fine
2462 static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
2465 struct n_tty_data *ldata = tty->disc_data;
2466 unsigned int mask = 0;
2468 poll_wait(file, &tty->read_wait, wait);
2469 poll_wait(file, &tty->write_wait, wait);
2470 if (input_available_p(tty, 1))
2471 mask |= POLLIN | POLLRDNORM;
2473 tty_buffer_flush_work(tty->port);
2474 if (input_available_p(tty, 1))
2475 mask |= POLLIN | POLLRDNORM;
2477 if (tty->packet && tty->link->ctrl_status)
2478 mask |= POLLPRI | POLLIN | POLLRDNORM;
2479 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2481 if (tty_hung_up_p(file))
2483 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2484 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2485 ldata->minimum_to_wake = MIN_CHAR(tty);
2487 ldata->minimum_to_wake = 1;
2489 if (tty->ops->write && !tty_is_writelocked(tty) &&
2490 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2491 tty_write_room(tty) > 0)
2492 mask |= POLLOUT | POLLWRNORM;
2496 static unsigned long inq_canon(struct n_tty_data *ldata)
2498 size_t nr, head, tail;
2500 if (ldata->canon_head == ldata->read_tail)
2502 head = ldata->canon_head;
2503 tail = ldata->read_tail;
2505 /* Skip EOF-chars.. */
2506 while (MASK(head) != MASK(tail)) {
2507 if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) &&
2508 read_buf(ldata, tail) == __DISABLED_CHAR)
2515 static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2516 unsigned int cmd, unsigned long arg)
2518 struct n_tty_data *ldata = tty->disc_data;
2523 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2525 down_write(&tty->termios_rwsem);
2526 if (L_ICANON(tty) && !L_EXTPROC(tty))
2527 retval = inq_canon(ldata);
2529 retval = read_cnt(ldata);
2530 up_write(&tty->termios_rwsem);
2531 return put_user(retval, (unsigned int __user *) arg);
2533 return n_tty_ioctl_helper(tty, file, cmd, arg);
2537 static void n_tty_fasync(struct tty_struct *tty, int on)
2539 struct n_tty_data *ldata = tty->disc_data;
2541 if (!waitqueue_active(&tty->read_wait)) {
2543 ldata->minimum_to_wake = 1;
2544 else if (!tty->fasync)
2545 ldata->minimum_to_wake = N_TTY_BUF_SIZE;
2549 struct tty_ldisc_ops tty_ldisc_N_TTY = {
2550 .magic = TTY_LDISC_MAGIC,
2553 .close = n_tty_close,
2554 .flush_buffer = n_tty_flush_buffer,
2555 .chars_in_buffer = n_tty_chars_in_buffer,
2557 .write = n_tty_write,
2558 .ioctl = n_tty_ioctl,
2559 .set_termios = n_tty_set_termios,
2561 .receive_buf = n_tty_receive_buf,
2562 .write_wakeup = n_tty_write_wakeup,
2563 .fasync = n_tty_fasync,
2564 .receive_buf2 = n_tty_receive_buf2,
2568 * n_tty_inherit_ops - inherit N_TTY methods
2569 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2571 * Enables a 'subclass' line discipline to 'inherit' N_TTY
2575 void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2577 *ops = tty_ldisc_N_TTY;
2579 ops->refcount = ops->flags = 0;
2581 EXPORT_SYMBOL_GPL(n_tty_inherit_ops);