2 * n_tty.c --- implements the N_TTY line discipline.
4 * This code used to be in tty_io.c, but things are getting hairy
5 * enough that it made sense to split things off. (The N_TTY
6 * processing has changed so much that it's hardly recognizable,
9 * Note that the open routine for N_TTY is guaranteed never to return
10 * an error. This is because Linux will fall back to setting a line
11 * to N_TTY if it can not switch to any other line discipline.
13 * Written by Theodore Ts'o, Copyright 1994.
15 * This file also contains code originally written by Linus Torvalds,
16 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
18 * This file may be redistributed under the terms of the GNU General Public
21 * Reduced memory usage for older ARM systems - Russell King.
23 * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
24 * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
25 * who actually finally proved there really was a race.
27 * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
28 * waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
29 * Also fixed a bug in BLOCKING mode where n_tty_write returns
33 #include <linux/types.h>
34 #include <linux/major.h>
35 #include <linux/errno.h>
36 #include <linux/signal.h>
37 #include <linux/fcntl.h>
38 #include <linux/sched.h>
39 #include <linux/interrupt.h>
40 #include <linux/tty.h>
41 #include <linux/timer.h>
42 #include <linux/ctype.h>
44 #include <linux/string.h>
45 #include <linux/slab.h>
46 #include <linux/poll.h>
47 #include <linux/bitops.h>
48 #include <linux/audit.h>
49 #include <linux/file.h>
50 #include <linux/uaccess.h>
51 #include <linux/module.h>
52 #include <linux/ratelimit.h>
55 /* number of characters left in xmit buffer before select has we have room */
56 #define WAKEUP_CHARS 256
59 * This defines the low- and high-watermarks for throttling and
60 * unthrottling the TTY driver. These watermarks are used for
61 * controlling the space in the read buffer.
63 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
64 #define TTY_THRESHOLD_UNTHROTTLE 128
67 * Special byte codes used in the echo buffer to represent operations
68 * or special handling of characters. Bytes in the echo buffer that
69 * are not part of such special blocks are treated as normal character
72 #define ECHO_OP_START 0xff
73 #define ECHO_OP_MOVE_BACK_COL 0x80
74 #define ECHO_OP_SET_CANON_COL 0x81
75 #define ECHO_OP_ERASE_TAB 0x82
79 # define n_tty_trace(f, args...) trace_printk(f, ##args)
81 # define n_tty_trace(f, args...)
86 unsigned long overrun_time
;
92 unsigned char lnext
:1, erasing
:1, raw
:1, real_raw
:1, icanon
:1;
93 unsigned char echo_overrun
:1;
95 DECLARE_BITMAP(process_char_map
, 256);
96 DECLARE_BITMAP(read_flags
, N_TTY_BUF_SIZE
);
103 unsigned char *echo_buf
;
104 unsigned int echo_pos
;
105 unsigned int echo_cnt
;
109 unsigned int canon_column
;
111 struct mutex atomic_read_lock
;
112 struct mutex output_lock
;
113 struct mutex echo_lock
;
114 raw_spinlock_t read_lock
;
117 static inline size_t read_cnt(struct n_tty_data
*ldata
)
119 return ldata
->read_head
- ldata
->read_tail
;
122 static inline unsigned char read_buf(struct n_tty_data
*ldata
, size_t i
)
124 return ldata
->read_buf
[i
& (N_TTY_BUF_SIZE
- 1)];
127 static inline unsigned char *read_buf_addr(struct n_tty_data
*ldata
, size_t i
)
129 return &ldata
->read_buf
[i
& (N_TTY_BUF_SIZE
- 1)];
132 static inline int tty_put_user(struct tty_struct
*tty
, unsigned char x
,
133 unsigned char __user
*ptr
)
135 struct n_tty_data
*ldata
= tty
->disc_data
;
137 tty_audit_add_data(tty
, &x
, 1, ldata
->icanon
);
138 return put_user(x
, ptr
);
141 static int receive_room(struct tty_struct
*tty
)
143 struct n_tty_data
*ldata
= tty
->disc_data
;
147 /* Multiply read_cnt by 3, since each byte might take up to
148 * three times as many spaces when PARMRK is set (depending on
149 * its flags, e.g. parity error). */
150 left
= N_TTY_BUF_SIZE
- read_cnt(ldata
) * 3 - 1;
152 left
= N_TTY_BUF_SIZE
- read_cnt(ldata
) - 1;
155 * If we are doing input canonicalization, and there are no
156 * pending newlines, let characters through without limit, so
157 * that erase characters will be handled. Other excess
158 * characters will be beeped.
161 left
= ldata
->icanon
&& !ldata
->canon_data
;
167 * n_tty_set_room - receive space
170 * Re-schedules the flip buffer work if space just became available.
172 * Locks: Concurrent update is protected with read_lock
175 static void n_tty_set_room(struct tty_struct
*tty
)
177 struct n_tty_data
*ldata
= tty
->disc_data
;
179 /* Did this open up the receive buffer? We may need to flip */
180 if (unlikely(ldata
->no_room
) && receive_room(tty
)) {
183 WARN_RATELIMIT(tty
->port
->itty
== NULL
,
184 "scheduling with invalid itty\n");
185 /* see if ldisc has been killed - if so, this means that
186 * even though the ldisc has been halted and ->buf.work
187 * cancelled, ->buf.work is about to be rescheduled
189 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED
, &tty
->flags
),
190 "scheduling buffer work for halted ldisc\n");
191 schedule_work(&tty
->port
->buf
.work
);
195 static void put_tty_queue_nolock(unsigned char c
, struct n_tty_data
*ldata
)
197 if (read_cnt(ldata
) < N_TTY_BUF_SIZE
) {
198 *read_buf_addr(ldata
, ldata
->read_head
) = c
;
204 * put_tty_queue - add character to tty
208 * Add a character to the tty read_buf queue. This is done under the
209 * read_lock to serialize character addition and also to protect us
210 * against parallel reads or flushes
213 static void put_tty_queue(unsigned char c
, struct n_tty_data
*ldata
)
217 * The problem of stomping on the buffers ends here.
218 * Why didn't anyone see this one coming? --AJK
220 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
221 put_tty_queue_nolock(c
, ldata
);
222 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
226 * reset_buffer_flags - reset buffer state
227 * @tty: terminal to reset
229 * Reset the read buffer counters and clear the flags.
230 * Called from n_tty_open() and n_tty_flush_buffer().
232 * Locking: tty_read_lock for read fields.
235 static void reset_buffer_flags(struct n_tty_data
*ldata
)
239 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
240 ldata
->read_head
= ldata
->read_tail
= 0;
241 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
243 mutex_lock(&ldata
->echo_lock
);
244 ldata
->echo_pos
= ldata
->echo_cnt
= ldata
->echo_overrun
= 0;
245 mutex_unlock(&ldata
->echo_lock
);
247 ldata
->canon_head
= ldata
->canon_data
= ldata
->erasing
= 0;
248 bitmap_zero(ldata
->read_flags
, N_TTY_BUF_SIZE
);
251 static void n_tty_packet_mode_flush(struct tty_struct
*tty
)
255 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
256 if (tty
->link
->packet
) {
257 tty
->ctrl_status
|= TIOCPKT_FLUSHREAD
;
258 wake_up_interruptible(&tty
->link
->read_wait
);
260 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
264 * n_tty_flush_buffer - clean input queue
265 * @tty: terminal device
267 * Flush the input buffer. Called when the tty layer wants the
268 * buffer flushed (eg at hangup) or when the N_TTY line discipline
269 * internally has to clean the pending queue (for example some signals).
271 * Locking: ctrl_lock, read_lock.
274 static void n_tty_flush_buffer(struct tty_struct
*tty
)
276 reset_buffer_flags(tty
->disc_data
);
280 n_tty_packet_mode_flush(tty
);
284 * n_tty_chars_in_buffer - report available bytes
287 * Report the number of characters buffered to be delivered to user
288 * at this instant in time.
293 static ssize_t
chars_in_buffer(struct tty_struct
*tty
)
295 struct n_tty_data
*ldata
= tty
->disc_data
;
299 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
303 n
= ldata
->canon_head
- ldata
->read_tail
;
304 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
308 static ssize_t
n_tty_chars_in_buffer(struct tty_struct
*tty
)
310 WARN_ONCE(1, "%s is deprecated and scheduled for removal.", __func__
);
311 return chars_in_buffer(tty
);
315 * is_utf8_continuation - utf8 multibyte check
318 * Returns true if the utf8 character 'c' is a multibyte continuation
319 * character. We use this to correctly compute the on screen size
320 * of the character when printing
323 static inline int is_utf8_continuation(unsigned char c
)
325 return (c
& 0xc0) == 0x80;
329 * is_continuation - multibyte check
332 * Returns true if the utf8 character 'c' is a multibyte continuation
333 * character and the terminal is in unicode mode.
336 static inline int is_continuation(unsigned char c
, struct tty_struct
*tty
)
338 return I_IUTF8(tty
) && is_utf8_continuation(c
);
342 * do_output_char - output one character
343 * @c: character (or partial unicode symbol)
344 * @tty: terminal device
345 * @space: space available in tty driver write buffer
347 * This is a helper function that handles one output character
348 * (including special characters like TAB, CR, LF, etc.),
349 * doing OPOST processing and putting the results in the
350 * tty driver's write buffer.
352 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
353 * and NLDLY. They simply aren't relevant in the world today.
354 * If you ever need them, add them here.
356 * Returns the number of bytes of buffer space used or -1 if
359 * Locking: should be called under the output_lock to protect
360 * the column state and space left in the buffer
363 static int do_output_char(unsigned char c
, struct tty_struct
*tty
, int space
)
365 struct n_tty_data
*ldata
= tty
->disc_data
;
378 ldata
->canon_column
= ldata
->column
= 0;
379 tty
->ops
->write(tty
, "\r\n", 2);
382 ldata
->canon_column
= ldata
->column
;
385 if (O_ONOCR(tty
) && ldata
->column
== 0)
390 ldata
->canon_column
= ldata
->column
= 0;
393 ldata
->canon_column
= ldata
->column
= 0;
396 spaces
= 8 - (ldata
->column
& 7);
397 if (O_TABDLY(tty
) == XTABS
) {
400 ldata
->column
+= spaces
;
401 tty
->ops
->write(tty
, " ", spaces
);
404 ldata
->column
+= spaces
;
407 if (ldata
->column
> 0)
414 if (!is_continuation(c
, tty
))
420 tty_put_char(tty
, c
);
425 * process_output - output post processor
426 * @c: character (or partial unicode symbol)
427 * @tty: terminal device
429 * Output one character with OPOST processing.
430 * Returns -1 when the output device is full and the character
433 * Locking: output_lock to protect column state and space left
434 * (also, this is called from n_tty_write under the
435 * tty layer write lock)
438 static int process_output(unsigned char c
, struct tty_struct
*tty
)
440 struct n_tty_data
*ldata
= tty
->disc_data
;
443 mutex_lock(&ldata
->output_lock
);
445 space
= tty_write_room(tty
);
446 retval
= do_output_char(c
, tty
, space
);
448 mutex_unlock(&ldata
->output_lock
);
456 * process_output_block - block post processor
457 * @tty: terminal device
458 * @buf: character buffer
459 * @nr: number of bytes to output
461 * Output a block of characters with OPOST processing.
462 * Returns the number of characters output.
464 * This path is used to speed up block console writes, among other
465 * things when processing blocks of output data. It handles only
466 * the simple cases normally found and helps to generate blocks of
467 * symbols for the console driver and thus improve performance.
469 * Locking: output_lock to protect column state and space left
470 * (also, this is called from n_tty_write under the
471 * tty layer write lock)
474 static ssize_t
process_output_block(struct tty_struct
*tty
,
475 const unsigned char *buf
, unsigned int nr
)
477 struct n_tty_data
*ldata
= tty
->disc_data
;
480 const unsigned char *cp
;
482 mutex_lock(&ldata
->output_lock
);
484 space
= tty_write_room(tty
);
486 mutex_unlock(&ldata
->output_lock
);
492 for (i
= 0, cp
= buf
; i
< nr
; i
++, cp
++) {
493 unsigned char c
= *cp
;
501 ldata
->canon_column
= ldata
->column
;
504 if (O_ONOCR(tty
) && ldata
->column
== 0)
508 ldata
->canon_column
= ldata
->column
= 0;
513 if (ldata
->column
> 0)
520 if (!is_continuation(c
, tty
))
527 i
= tty
->ops
->write(tty
, buf
, i
);
529 mutex_unlock(&ldata
->output_lock
);
534 * process_echoes - write pending echo characters
535 * @tty: terminal device
537 * Write previously buffered echo (and other ldisc-generated)
538 * characters to the tty.
540 * Characters generated by the ldisc (including echoes) need to
541 * be buffered because the driver's write buffer can fill during
542 * heavy program output. Echoing straight to the driver will
543 * often fail under these conditions, causing lost characters and
544 * resulting mismatches of ldisc state information.
546 * Since the ldisc state must represent the characters actually sent
547 * to the driver at the time of the write, operations like certain
548 * changes in column state are also saved in the buffer and executed
551 * A circular fifo buffer is used so that the most recent characters
552 * are prioritized. Also, when control characters are echoed with a
553 * prefixed "^", the pair is treated atomically and thus not separated.
555 * Locking: output_lock to protect column state and space left,
556 * echo_lock to protect the echo buffer
559 static void process_echoes(struct tty_struct
*tty
)
561 struct n_tty_data
*ldata
= tty
->disc_data
;
564 unsigned char *cp
, *buf_end
;
566 if (!ldata
->echo_cnt
)
569 mutex_lock(&ldata
->output_lock
);
570 mutex_lock(&ldata
->echo_lock
);
572 space
= tty_write_room(tty
);
574 buf_end
= ldata
->echo_buf
+ N_TTY_BUF_SIZE
;
575 cp
= ldata
->echo_buf
+ ldata
->echo_pos
;
576 nr
= ldata
->echo_cnt
;
579 if (c
== ECHO_OP_START
) {
582 int no_space_left
= 0;
585 * If the buffer byte is the start of a multi-byte
586 * operation, get the next byte, which is either the
587 * op code or a control character value.
591 opp
-= N_TTY_BUF_SIZE
;
595 unsigned int num_chars
, num_bs
;
597 case ECHO_OP_ERASE_TAB
:
598 if (++opp
== buf_end
)
599 opp
-= N_TTY_BUF_SIZE
;
603 * Determine how many columns to go back
604 * in order to erase the tab.
605 * This depends on the number of columns
606 * used by other characters within the tab
607 * area. If this (modulo 8) count is from
608 * the start of input rather than from a
609 * previous tab, we offset by canon column.
610 * Otherwise, tab spacing is normal.
612 if (!(num_chars
& 0x80))
613 num_chars
+= ldata
->canon_column
;
614 num_bs
= 8 - (num_chars
& 7);
616 if (num_bs
> space
) {
622 tty_put_char(tty
, '\b');
623 if (ldata
->column
> 0)
630 case ECHO_OP_SET_CANON_COL
:
631 ldata
->canon_column
= ldata
->column
;
636 case ECHO_OP_MOVE_BACK_COL
:
637 if (ldata
->column
> 0)
644 /* This is an escaped echo op start code */
649 tty_put_char(tty
, ECHO_OP_START
);
658 * If the op is not a special byte code,
659 * it is a ctrl char tagged to be echoed
660 * as "^X" (where X is the letter
661 * representing the control char).
662 * Note that we must ensure there is
663 * enough space for the whole ctrl pair.
670 tty_put_char(tty
, '^');
671 tty_put_char(tty
, op
^ 0100);
682 int retval
= do_output_char(c
, tty
, space
);
689 tty_put_char(tty
, c
);
696 /* When end of circular buffer reached, wrap around */
698 cp
-= N_TTY_BUF_SIZE
;
704 ldata
->echo_overrun
= 0;
706 int num_processed
= ldata
->echo_cnt
- nr
;
707 ldata
->echo_pos
+= num_processed
;
708 ldata
->echo_pos
&= N_TTY_BUF_SIZE
- 1;
709 ldata
->echo_cnt
= nr
;
710 if (num_processed
> 0)
711 ldata
->echo_overrun
= 0;
714 mutex_unlock(&ldata
->echo_lock
);
715 mutex_unlock(&ldata
->output_lock
);
717 if (tty
->ops
->flush_chars
)
718 tty
->ops
->flush_chars(tty
);
722 * add_echo_byte - add a byte to the echo buffer
723 * @c: unicode byte to echo
726 * Add a character or operation byte to the echo buffer.
728 * Should be called under the echo lock to protect the echo buffer.
731 static void add_echo_byte(unsigned char c
, struct n_tty_data
*ldata
)
735 if (ldata
->echo_cnt
== N_TTY_BUF_SIZE
) {
736 /* Circular buffer is already at capacity */
737 new_byte_pos
= ldata
->echo_pos
;
740 * Since the buffer start position needs to be advanced,
741 * be sure to step by a whole operation byte group.
743 if (ldata
->echo_buf
[ldata
->echo_pos
] == ECHO_OP_START
) {
744 if (ldata
->echo_buf
[(ldata
->echo_pos
+ 1) &
745 (N_TTY_BUF_SIZE
- 1)] ==
747 ldata
->echo_pos
+= 3;
748 ldata
->echo_cnt
-= 2;
750 ldata
->echo_pos
+= 2;
751 ldata
->echo_cnt
-= 1;
756 ldata
->echo_pos
&= N_TTY_BUF_SIZE
- 1;
758 ldata
->echo_overrun
= 1;
760 new_byte_pos
= ldata
->echo_pos
+ ldata
->echo_cnt
;
761 new_byte_pos
&= N_TTY_BUF_SIZE
- 1;
765 ldata
->echo_buf
[new_byte_pos
] = c
;
769 * echo_move_back_col - add operation to move back a column
772 * Add an operation to the echo buffer to move back one column.
774 * Locking: echo_lock to protect the echo buffer
777 static void echo_move_back_col(struct n_tty_data
*ldata
)
779 mutex_lock(&ldata
->echo_lock
);
780 add_echo_byte(ECHO_OP_START
, ldata
);
781 add_echo_byte(ECHO_OP_MOVE_BACK_COL
, ldata
);
782 mutex_unlock(&ldata
->echo_lock
);
786 * echo_set_canon_col - add operation to set the canon column
789 * Add an operation to the echo buffer to set the canon column
790 * to the current column.
792 * Locking: echo_lock to protect the echo buffer
795 static void echo_set_canon_col(struct n_tty_data
*ldata
)
797 mutex_lock(&ldata
->echo_lock
);
798 add_echo_byte(ECHO_OP_START
, ldata
);
799 add_echo_byte(ECHO_OP_SET_CANON_COL
, ldata
);
800 mutex_unlock(&ldata
->echo_lock
);
804 * echo_erase_tab - add operation to erase a tab
805 * @num_chars: number of character columns already used
806 * @after_tab: true if num_chars starts after a previous tab
809 * Add an operation to the echo buffer to erase a tab.
811 * Called by the eraser function, which knows how many character
812 * columns have been used since either a previous tab or the start
813 * of input. This information will be used later, along with
814 * canon column (if applicable), to go back the correct number
817 * Locking: echo_lock to protect the echo buffer
820 static void echo_erase_tab(unsigned int num_chars
, int after_tab
,
821 struct n_tty_data
*ldata
)
823 mutex_lock(&ldata
->echo_lock
);
825 add_echo_byte(ECHO_OP_START
, ldata
);
826 add_echo_byte(ECHO_OP_ERASE_TAB
, ldata
);
828 /* We only need to know this modulo 8 (tab spacing) */
831 /* Set the high bit as a flag if num_chars is after a previous tab */
835 add_echo_byte(num_chars
, ldata
);
837 mutex_unlock(&ldata
->echo_lock
);
841 * echo_char_raw - echo a character raw
842 * @c: unicode byte to echo
843 * @tty: terminal device
845 * Echo user input back onto the screen. This must be called only when
846 * L_ECHO(tty) is true. Called from the driver receive_buf path.
848 * This variant does not treat control characters specially.
850 * Locking: echo_lock to protect the echo buffer
853 static void echo_char_raw(unsigned char c
, struct n_tty_data
*ldata
)
855 mutex_lock(&ldata
->echo_lock
);
856 if (c
== ECHO_OP_START
) {
857 add_echo_byte(ECHO_OP_START
, ldata
);
858 add_echo_byte(ECHO_OP_START
, ldata
);
860 add_echo_byte(c
, ldata
);
862 mutex_unlock(&ldata
->echo_lock
);
866 * echo_char - echo a character
867 * @c: unicode byte to echo
868 * @tty: terminal device
870 * Echo user input back onto the screen. This must be called only when
871 * L_ECHO(tty) is true. Called from the driver receive_buf path.
873 * This variant tags control characters to be echoed as "^X"
874 * (where X is the letter representing the control char).
876 * Locking: echo_lock to protect the echo buffer
879 static void echo_char(unsigned char c
, struct tty_struct
*tty
)
881 struct n_tty_data
*ldata
= tty
->disc_data
;
883 mutex_lock(&ldata
->echo_lock
);
885 if (c
== ECHO_OP_START
) {
886 add_echo_byte(ECHO_OP_START
, ldata
);
887 add_echo_byte(ECHO_OP_START
, ldata
);
889 if (L_ECHOCTL(tty
) && iscntrl(c
) && c
!= '\t')
890 add_echo_byte(ECHO_OP_START
, ldata
);
891 add_echo_byte(c
, ldata
);
894 mutex_unlock(&ldata
->echo_lock
);
898 * finish_erasing - complete erase
902 static inline void finish_erasing(struct n_tty_data
*ldata
)
904 if (ldata
->erasing
) {
905 echo_char_raw('/', ldata
);
911 * eraser - handle erase function
912 * @c: character input
913 * @tty: terminal device
915 * Perform erase and necessary output when an erase character is
916 * present in the stream from the driver layer. Handles the complexities
917 * of UTF-8 multibyte symbols.
919 * Locking: read_lock for tty buffers
922 static void eraser(unsigned char c
, struct tty_struct
*tty
)
924 struct n_tty_data
*ldata
= tty
->disc_data
;
925 enum { ERASE
, WERASE
, KILL
} kill_type
;
931 /* FIXME: locking needed ? */
932 if (ldata
->read_head
== ldata
->canon_head
) {
933 /* process_output('\a', tty); */ /* what do you think? */
936 if (c
== ERASE_CHAR(tty
))
938 else if (c
== WERASE_CHAR(tty
))
942 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
943 ldata
->read_head
= ldata
->canon_head
;
944 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
947 if (!L_ECHOK(tty
) || !L_ECHOKE(tty
) || !L_ECHOE(tty
)) {
948 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
949 ldata
->read_head
= ldata
->canon_head
;
950 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
951 finish_erasing(ldata
);
952 echo_char(KILL_CHAR(tty
), tty
);
953 /* Add a newline if ECHOK is on and ECHOKE is off. */
955 echo_char_raw('\n', ldata
);
962 /* FIXME: Locking ?? */
963 while (ldata
->read_head
!= ldata
->canon_head
) {
964 head
= ldata
->read_head
;
966 /* erase a single possibly multibyte character */
969 c
= read_buf(ldata
, head
);
970 } while (is_continuation(c
, tty
) && head
!= ldata
->canon_head
);
972 /* do not partially erase */
973 if (is_continuation(c
, tty
))
976 if (kill_type
== WERASE
) {
977 /* Equivalent to BSD's ALTWERASE. */
978 if (isalnum(c
) || c
== '_')
980 else if (seen_alnums
)
983 cnt
= ldata
->read_head
- head
;
984 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
985 ldata
->read_head
= head
;
986 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
988 if (L_ECHOPRT(tty
)) {
989 if (!ldata
->erasing
) {
990 echo_char_raw('\\', ldata
);
993 /* if cnt > 1, output a multi-byte character */
997 echo_char_raw(read_buf(ldata
, head
), ldata
);
998 echo_move_back_col(ldata
);
1000 } else if (kill_type
== ERASE
&& !L_ECHOE(tty
)) {
1001 echo_char(ERASE_CHAR(tty
), tty
);
1002 } else if (c
== '\t') {
1003 unsigned int num_chars
= 0;
1005 size_t tail
= ldata
->read_head
;
1008 * Count the columns used for characters
1009 * since the start of input or after a
1011 * This info is used to go back the correct
1012 * number of columns.
1014 while (tail
!= ldata
->canon_head
) {
1016 c
= read_buf(ldata
, tail
);
1020 } else if (iscntrl(c
)) {
1023 } else if (!is_continuation(c
, tty
)) {
1027 echo_erase_tab(num_chars
, after_tab
, ldata
);
1029 if (iscntrl(c
) && L_ECHOCTL(tty
)) {
1030 echo_char_raw('\b', ldata
);
1031 echo_char_raw(' ', ldata
);
1032 echo_char_raw('\b', ldata
);
1034 if (!iscntrl(c
) || L_ECHOCTL(tty
)) {
1035 echo_char_raw('\b', ldata
);
1036 echo_char_raw(' ', ldata
);
1037 echo_char_raw('\b', ldata
);
1041 if (kill_type
== ERASE
)
1044 if (ldata
->read_head
== ldata
->canon_head
&& L_ECHO(tty
))
1045 finish_erasing(ldata
);
1049 * isig - handle the ISIG optio
1053 * Called when a signal is being sent due to terminal input.
1054 * Called from the driver receive_buf path so serialized.
1056 * Locking: ctrl_lock
1059 static inline void isig(int sig
, struct tty_struct
*tty
)
1061 struct pid
*tty_pgrp
= tty_get_pgrp(tty
);
1063 kill_pgrp(tty_pgrp
, sig
, 1);
1069 * n_tty_receive_break - handle break
1072 * An RS232 break event has been hit in the incoming bitstream. This
1073 * can cause a variety of events depending upon the termios settings.
1075 * Called from the receive_buf path so single threaded.
1078 static inline void n_tty_receive_break(struct tty_struct
*tty
)
1080 struct n_tty_data
*ldata
= tty
->disc_data
;
1084 if (I_BRKINT(tty
)) {
1086 if (!L_NOFLSH(tty
)) {
1087 n_tty_flush_buffer(tty
);
1088 tty_driver_flush_buffer(tty
);
1092 if (I_PARMRK(tty
)) {
1093 put_tty_queue('\377', ldata
);
1094 put_tty_queue('\0', ldata
);
1096 put_tty_queue('\0', ldata
);
1097 wake_up_interruptible(&tty
->read_wait
);
1101 * n_tty_receive_overrun - handle overrun reporting
1104 * Data arrived faster than we could process it. While the tty
1105 * driver has flagged this the bits that were missed are gone
1108 * Called from the receive_buf path so single threaded. Does not
1109 * need locking as num_overrun and overrun_time are function
1113 static inline void n_tty_receive_overrun(struct tty_struct
*tty
)
1115 struct n_tty_data
*ldata
= tty
->disc_data
;
1118 ldata
->num_overrun
++;
1119 if (time_after(jiffies
, ldata
->overrun_time
+ HZ
) ||
1120 time_after(ldata
->overrun_time
, jiffies
)) {
1121 printk(KERN_WARNING
"%s: %d input overrun(s)\n",
1123 ldata
->num_overrun
);
1124 ldata
->overrun_time
= jiffies
;
1125 ldata
->num_overrun
= 0;
1130 * n_tty_receive_parity_error - error notifier
1131 * @tty: terminal device
1134 * Process a parity error and queue the right data to indicate
1135 * the error case if necessary. Locking as per n_tty_receive_buf.
1137 static inline void n_tty_receive_parity_error(struct tty_struct
*tty
,
1140 struct n_tty_data
*ldata
= tty
->disc_data
;
1144 if (I_PARMRK(tty
)) {
1145 put_tty_queue('\377', ldata
);
1146 put_tty_queue('\0', ldata
);
1147 put_tty_queue(c
, ldata
);
1148 } else if (I_INPCK(tty
))
1149 put_tty_queue('\0', ldata
);
1151 put_tty_queue(c
, ldata
);
1152 wake_up_interruptible(&tty
->read_wait
);
1156 * n_tty_receive_char - perform processing
1157 * @tty: terminal device
1160 * Process an individual character of input received from the driver.
1161 * This is serialized with respect to itself by the rules for the
1165 static inline void n_tty_receive_char(struct tty_struct
*tty
, unsigned char c
)
1167 struct n_tty_data
*ldata
= tty
->disc_data
;
1168 unsigned long flags
;
1172 put_tty_queue(c
, ldata
);
1178 if (I_IUCLC(tty
) && L_IEXTEN(tty
))
1181 if (L_EXTPROC(tty
)) {
1182 put_tty_queue(c
, ldata
);
1186 if (tty
->stopped
&& !tty
->flow_stopped
&& I_IXON(tty
) &&
1187 I_IXANY(tty
) && c
!= START_CHAR(tty
) && c
!= STOP_CHAR(tty
) &&
1188 c
!= INTR_CHAR(tty
) && c
!= QUIT_CHAR(tty
) && c
!= SUSP_CHAR(tty
)) {
1190 process_echoes(tty
);
1195 if (c
== START_CHAR(tty
)) {
1197 process_echoes(tty
);
1198 } else if (c
== STOP_CHAR(tty
))
1205 * If the previous character was LNEXT, or we know that this
1206 * character is not one of the characters that we'll have to
1207 * handle specially, do shortcut processing to speed things
1210 if (!test_bit(c
, ldata
->process_char_map
) || ldata
->lnext
) {
1212 parmrk
= (c
== (unsigned char) '\377' && I_PARMRK(tty
)) ? 1 : 0;
1213 if (read_cnt(ldata
) >= (N_TTY_BUF_SIZE
- parmrk
- 1)) {
1214 /* beep if no space */
1216 process_output('\a', tty
);
1220 finish_erasing(ldata
);
1221 /* Record the column of first canon char. */
1222 if (ldata
->canon_head
== ldata
->read_head
)
1223 echo_set_canon_col(ldata
);
1225 process_echoes(tty
);
1228 put_tty_queue(c
, ldata
);
1229 put_tty_queue(c
, ldata
);
1234 if (c
== START_CHAR(tty
)) {
1236 process_echoes(tty
);
1239 if (c
== STOP_CHAR(tty
)) {
1248 if (c
== INTR_CHAR(tty
))
1251 if (c
== QUIT_CHAR(tty
))
1254 if (c
== SUSP_CHAR(tty
)) {
1256 if (!L_NOFLSH(tty
)) {
1257 n_tty_flush_buffer(tty
);
1258 tty_driver_flush_buffer(tty
);
1264 process_echoes(tty
);
1276 } else if (c
== '\n' && I_INLCR(tty
))
1279 if (ldata
->icanon
) {
1280 if (c
== ERASE_CHAR(tty
) || c
== KILL_CHAR(tty
) ||
1281 (c
== WERASE_CHAR(tty
) && L_IEXTEN(tty
))) {
1283 process_echoes(tty
);
1286 if (c
== LNEXT_CHAR(tty
) && L_IEXTEN(tty
)) {
1289 finish_erasing(ldata
);
1290 if (L_ECHOCTL(tty
)) {
1291 echo_char_raw('^', ldata
);
1292 echo_char_raw('\b', ldata
);
1293 process_echoes(tty
);
1298 if (c
== REPRINT_CHAR(tty
) && L_ECHO(tty
) &&
1300 size_t tail
= ldata
->canon_head
;
1302 finish_erasing(ldata
);
1304 echo_char_raw('\n', ldata
);
1305 while (tail
!= ldata
->read_head
) {
1306 echo_char(read_buf(ldata
, tail
), tty
);
1309 process_echoes(tty
);
1313 if (read_cnt(ldata
) >= N_TTY_BUF_SIZE
) {
1315 process_output('\a', tty
);
1318 if (L_ECHO(tty
) || L_ECHONL(tty
)) {
1319 echo_char_raw('\n', ldata
);
1320 process_echoes(tty
);
1322 goto handle_newline
;
1324 if (c
== EOF_CHAR(tty
)) {
1325 if (read_cnt(ldata
) >= N_TTY_BUF_SIZE
)
1327 if (ldata
->canon_head
!= ldata
->read_head
)
1328 set_bit(TTY_PUSH
, &tty
->flags
);
1329 c
= __DISABLED_CHAR
;
1330 goto handle_newline
;
1332 if ((c
== EOL_CHAR(tty
)) ||
1333 (c
== EOL2_CHAR(tty
) && L_IEXTEN(tty
))) {
1334 parmrk
= (c
== (unsigned char) '\377' && I_PARMRK(tty
))
1336 if (read_cnt(ldata
) >= (N_TTY_BUF_SIZE
- parmrk
)) {
1338 process_output('\a', tty
);
1342 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1345 /* Record the column of first canon char. */
1346 if (ldata
->canon_head
== ldata
->read_head
)
1347 echo_set_canon_col(ldata
);
1349 process_echoes(tty
);
1352 * XXX does PARMRK doubling happen for
1353 * EOL_CHAR and EOL2_CHAR?
1356 put_tty_queue(c
, ldata
);
1359 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
1360 set_bit(ldata
->read_head
& (N_TTY_BUF_SIZE
- 1), ldata
->read_flags
);
1361 put_tty_queue_nolock(c
, ldata
);
1362 ldata
->canon_head
= ldata
->read_head
;
1363 ldata
->canon_data
++;
1364 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
1365 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
1366 if (waitqueue_active(&tty
->read_wait
))
1367 wake_up_interruptible(&tty
->read_wait
);
1372 parmrk
= (c
== (unsigned char) '\377' && I_PARMRK(tty
)) ? 1 : 0;
1373 if (read_cnt(ldata
) >= (N_TTY_BUF_SIZE
- parmrk
- 1)) {
1374 /* beep if no space */
1376 process_output('\a', tty
);
1380 finish_erasing(ldata
);
1382 echo_char_raw('\n', ldata
);
1384 /* Record the column of first canon char. */
1385 if (ldata
->canon_head
== ldata
->read_head
)
1386 echo_set_canon_col(ldata
);
1389 process_echoes(tty
);
1393 put_tty_queue(c
, ldata
);
1395 put_tty_queue(c
, ldata
);
1400 * n_tty_write_wakeup - asynchronous I/O notifier
1403 * Required for the ptys, serial driver etc. since processes
1404 * that attach themselves to the master and rely on ASYNC
1405 * IO must be woken up
1408 static void n_tty_write_wakeup(struct tty_struct
*tty
)
1410 if (tty
->fasync
&& test_and_clear_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
))
1411 kill_fasync(&tty
->fasync
, SIGIO
, POLL_OUT
);
1415 * n_tty_receive_buf - data receive
1416 * @tty: terminal device
1419 * @count: characters
1421 * Called by the terminal driver when a block of characters has
1422 * been received. This function must be called from soft contexts
1423 * not from interrupt context. The driver is responsible for making
1424 * calls one at a time and in order (or using flush_to_ldisc)
1427 static void __receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
1428 char *fp
, int count
)
1430 struct n_tty_data
*ldata
= tty
->disc_data
;
1431 const unsigned char *p
;
1432 char *f
, flags
= TTY_NORMAL
;
1435 unsigned long cpuflags
;
1437 if (ldata
->real_raw
) {
1438 raw_spin_lock_irqsave(&ldata
->read_lock
, cpuflags
);
1439 i
= min(N_TTY_BUF_SIZE
- read_cnt(ldata
),
1440 N_TTY_BUF_SIZE
- (ldata
->read_head
& (N_TTY_BUF_SIZE
- 1)));
1442 memcpy(read_buf_addr(ldata
, ldata
->read_head
), cp
, i
);
1443 ldata
->read_head
+= i
;
1447 i
= min(N_TTY_BUF_SIZE
- read_cnt(ldata
),
1448 N_TTY_BUF_SIZE
- (ldata
->read_head
& (N_TTY_BUF_SIZE
- 1)));
1450 memcpy(read_buf_addr(ldata
, ldata
->read_head
), cp
, i
);
1451 ldata
->read_head
+= i
;
1452 raw_spin_unlock_irqrestore(&ldata
->read_lock
, cpuflags
);
1454 for (i
= count
, p
= cp
, f
= fp
; i
; i
--, p
++) {
1459 n_tty_receive_char(tty
, *p
);
1462 n_tty_receive_break(tty
);
1466 n_tty_receive_parity_error(tty
, *p
);
1469 n_tty_receive_overrun(tty
);
1472 printk(KERN_ERR
"%s: unknown flag %d\n",
1473 tty_name(tty
, buf
), flags
);
1477 if (tty
->ops
->flush_chars
)
1478 tty
->ops
->flush_chars(tty
);
1481 if ((!ldata
->icanon
&& (read_cnt(ldata
) >= ldata
->minimum_to_wake
)) ||
1483 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
1484 if (waitqueue_active(&tty
->read_wait
))
1485 wake_up_interruptible(&tty
->read_wait
);
1489 * Check the remaining room for the input canonicalization
1490 * mode. We don't want to throttle the driver if we're in
1491 * canonical mode and don't have a newline yet!
1494 tty_set_flow_change(tty
, TTY_THROTTLE_SAFE
);
1495 if (receive_room(tty
) >= TTY_THRESHOLD_THROTTLE
)
1497 if (!tty_throttle_safe(tty
))
1500 __tty_set_flow_change(tty
, 0);
1503 static void n_tty_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
1504 char *fp
, int count
)
1506 __receive_buf(tty
, cp
, fp
, count
);
1509 static int n_tty_receive_buf2(struct tty_struct
*tty
, const unsigned char *cp
,
1510 char *fp
, int count
)
1512 struct n_tty_data
*ldata
= tty
->disc_data
;
1515 tty
->receive_room
= room
= receive_room(tty
);
1518 count
= min(count
, room
);
1520 __receive_buf(tty
, cp
, fp
, count
);
1525 int is_ignored(int sig
)
1527 return (sigismember(¤t
->blocked
, sig
) ||
1528 current
->sighand
->action
[sig
-1].sa
.sa_handler
== SIG_IGN
);
1532 * n_tty_set_termios - termios data changed
1534 * @old: previous data
1536 * Called by the tty layer when the user changes termios flags so
1537 * that the line discipline can plan ahead. This function cannot sleep
1538 * and is protected from re-entry by the tty layer. The user is
1539 * guaranteed that this function will not be re-entered or in progress
1540 * when the ldisc is closed.
1542 * Locking: Caller holds tty->termios_mutex
1545 static void n_tty_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
1547 struct n_tty_data
*ldata
= tty
->disc_data
;
1548 int canon_change
= 1;
1551 canon_change
= (old
->c_lflag
^ tty
->termios
.c_lflag
) & ICANON
;
1553 bitmap_zero(ldata
->read_flags
, N_TTY_BUF_SIZE
);
1554 ldata
->canon_head
= ldata
->read_tail
;
1555 ldata
->canon_data
= 0;
1559 if (canon_change
&& !L_ICANON(tty
) && read_cnt(ldata
))
1560 wake_up_interruptible(&tty
->read_wait
);
1562 ldata
->icanon
= (L_ICANON(tty
) != 0);
1564 if (I_ISTRIP(tty
) || I_IUCLC(tty
) || I_IGNCR(tty
) ||
1565 I_ICRNL(tty
) || I_INLCR(tty
) || L_ICANON(tty
) ||
1566 I_IXON(tty
) || L_ISIG(tty
) || L_ECHO(tty
) ||
1568 bitmap_zero(ldata
->process_char_map
, 256);
1570 if (I_IGNCR(tty
) || I_ICRNL(tty
))
1571 set_bit('\r', ldata
->process_char_map
);
1573 set_bit('\n', ldata
->process_char_map
);
1575 if (L_ICANON(tty
)) {
1576 set_bit(ERASE_CHAR(tty
), ldata
->process_char_map
);
1577 set_bit(KILL_CHAR(tty
), ldata
->process_char_map
);
1578 set_bit(EOF_CHAR(tty
), ldata
->process_char_map
);
1579 set_bit('\n', ldata
->process_char_map
);
1580 set_bit(EOL_CHAR(tty
), ldata
->process_char_map
);
1581 if (L_IEXTEN(tty
)) {
1582 set_bit(WERASE_CHAR(tty
),
1583 ldata
->process_char_map
);
1584 set_bit(LNEXT_CHAR(tty
),
1585 ldata
->process_char_map
);
1586 set_bit(EOL2_CHAR(tty
),
1587 ldata
->process_char_map
);
1589 set_bit(REPRINT_CHAR(tty
),
1590 ldata
->process_char_map
);
1594 set_bit(START_CHAR(tty
), ldata
->process_char_map
);
1595 set_bit(STOP_CHAR(tty
), ldata
->process_char_map
);
1598 set_bit(INTR_CHAR(tty
), ldata
->process_char_map
);
1599 set_bit(QUIT_CHAR(tty
), ldata
->process_char_map
);
1600 set_bit(SUSP_CHAR(tty
), ldata
->process_char_map
);
1602 clear_bit(__DISABLED_CHAR
, ldata
->process_char_map
);
1604 ldata
->real_raw
= 0;
1607 if ((I_IGNBRK(tty
) || (!I_BRKINT(tty
) && !I_PARMRK(tty
))) &&
1608 (I_IGNPAR(tty
) || !I_INPCK(tty
)) &&
1609 (tty
->driver
->flags
& TTY_DRIVER_REAL_RAW
))
1610 ldata
->real_raw
= 1;
1612 ldata
->real_raw
= 0;
1614 n_tty_set_room(tty
);
1616 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1617 * been stopped by STOP_CHAR(tty) before it.
1619 if (!I_IXON(tty
) && old
&& (old
->c_iflag
& IXON
) && !tty
->flow_stopped
) {
1623 /* The termios change make the tty ready for I/O */
1624 wake_up_interruptible(&tty
->write_wait
);
1625 wake_up_interruptible(&tty
->read_wait
);
1629 * n_tty_close - close the ldisc for this tty
1632 * Called from the terminal layer when this line discipline is
1633 * being shut down, either because of a close or becsuse of a
1634 * discipline change. The function will not be called while other
1635 * ldisc methods are in progress.
1638 static void n_tty_close(struct tty_struct
*tty
)
1640 struct n_tty_data
*ldata
= tty
->disc_data
;
1643 n_tty_packet_mode_flush(tty
);
1645 kfree(ldata
->read_buf
);
1646 kfree(ldata
->echo_buf
);
1648 tty
->disc_data
= NULL
;
1652 * n_tty_open - open an ldisc
1653 * @tty: terminal to open
1655 * Called when this line discipline is being attached to the
1656 * terminal device. Can sleep. Called serialized so that no
1657 * other events will occur in parallel. No further open will occur
1661 static int n_tty_open(struct tty_struct
*tty
)
1663 struct n_tty_data
*ldata
;
1665 ldata
= kzalloc(sizeof(*ldata
), GFP_KERNEL
);
1669 ldata
->overrun_time
= jiffies
;
1670 mutex_init(&ldata
->atomic_read_lock
);
1671 mutex_init(&ldata
->output_lock
);
1672 mutex_init(&ldata
->echo_lock
);
1673 raw_spin_lock_init(&ldata
->read_lock
);
1675 /* These are ugly. Currently a malloc failure here can panic */
1676 ldata
->read_buf
= kzalloc(N_TTY_BUF_SIZE
, GFP_KERNEL
);
1677 ldata
->echo_buf
= kzalloc(N_TTY_BUF_SIZE
, GFP_KERNEL
);
1678 if (!ldata
->read_buf
|| !ldata
->echo_buf
)
1681 tty
->disc_data
= ldata
;
1682 reset_buffer_flags(tty
->disc_data
);
1684 ldata
->minimum_to_wake
= 1;
1686 /* indicate buffer work may resume */
1687 clear_bit(TTY_LDISC_HALTED
, &tty
->flags
);
1688 n_tty_set_termios(tty
, NULL
);
1689 tty_unthrottle(tty
);
1693 kfree(ldata
->read_buf
);
1694 kfree(ldata
->echo_buf
);
1700 static inline int input_available_p(struct tty_struct
*tty
, int amt
)
1702 struct n_tty_data
*ldata
= tty
->disc_data
;
1704 tty_flush_to_ldisc(tty
);
1705 if (ldata
->icanon
&& !L_EXTPROC(tty
)) {
1706 if (ldata
->canon_data
)
1708 } else if (read_cnt(ldata
) >= (amt
? amt
: 1))
1715 * copy_from_read_buf - copy read data directly
1716 * @tty: terminal device
1720 * Helper function to speed up n_tty_read. It is only called when
1721 * ICANON is off; it copies characters straight from the tty queue to
1722 * user space directly. It can be profitably called twice; once to
1723 * drain the space from the tail pointer to the (physical) end of the
1724 * buffer, and once to drain the space from the (physical) beginning of
1725 * the buffer to head pointer.
1727 * Called under the ldata->atomic_read_lock sem
1731 static int copy_from_read_buf(struct tty_struct
*tty
,
1732 unsigned char __user
**b
,
1736 struct n_tty_data
*ldata
= tty
->disc_data
;
1739 unsigned long flags
;
1741 size_t tail
= ldata
->read_tail
& (N_TTY_BUF_SIZE
- 1);
1744 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
1745 n
= min(read_cnt(ldata
), N_TTY_BUF_SIZE
- tail
);
1747 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
1749 retval
= copy_to_user(*b
, read_buf_addr(ldata
, tail
), n
);
1751 is_eof
= n
== 1 && read_buf(ldata
, tail
) == EOF_CHAR(tty
);
1752 tty_audit_add_data(tty
, read_buf_addr(ldata
, tail
), n
,
1754 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
1755 ldata
->read_tail
+= n
;
1756 /* Turn single EOF into zero-length read */
1757 if (L_EXTPROC(tty
) && ldata
->icanon
&& is_eof
&& !read_cnt(ldata
))
1759 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
1767 * canon_copy_from_read_buf - copy read data in canonical mode
1768 * @tty: terminal device
1772 * Helper function for n_tty_read. It is only called when ICANON is on;
1773 * it copies one line of input up to and including the line-delimiting
1774 * character into the user-space buffer.
1776 * Called under the atomic_read_lock mutex
1779 static int canon_copy_from_read_buf(struct tty_struct
*tty
,
1780 unsigned char __user
**b
,
1783 struct n_tty_data
*ldata
= tty
->disc_data
;
1784 unsigned long flags
;
1785 size_t n
, size
, more
, c
;
1790 /* N.B. avoid overrun if nr == 0 */
1792 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
1794 n
= min(*nr
, read_cnt(ldata
));
1796 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
1800 tail
= ldata
->read_tail
& (N_TTY_BUF_SIZE
- 1);
1801 size
= min_t(size_t, tail
+ n
, N_TTY_BUF_SIZE
);
1803 n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
1804 __func__
, *nr
, tail
, n
, size
);
1806 eol
= find_next_bit(ldata
->read_flags
, size
, tail
);
1807 more
= n
- (size
- tail
);
1808 if (eol
== N_TTY_BUF_SIZE
&& more
) {
1809 /* scan wrapped without finding set bit */
1810 eol
= find_next_bit(ldata
->read_flags
, more
, 0);
1813 } else if (eol
!= size
)
1816 size
= N_TTY_BUF_SIZE
- tail
;
1817 n
= (found
+ eol
+ size
) & (N_TTY_BUF_SIZE
- 1);
1820 if (found
&& read_buf(ldata
, eol
) == __DISABLED_CHAR
)
1823 n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu size:%zu more:%zu\n",
1824 __func__
, eol
, found
, n
, c
, size
, more
);
1826 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
1829 ret
= copy_to_user(*b
, read_buf_addr(ldata
, tail
), size
);
1832 ret
= copy_to_user(*b
+ size
, ldata
->read_buf
, n
- size
);
1834 ret
= copy_to_user(*b
, read_buf_addr(ldata
, tail
), n
);
1841 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
1842 ldata
->read_tail
+= c
;
1844 __clear_bit(eol
, ldata
->read_flags
);
1845 /* this test should be redundant:
1846 * we shouldn't be reading data if
1849 if (--ldata
->canon_data
< 0)
1850 ldata
->canon_data
= 0;
1852 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
1855 tty_audit_push(tty
);
1859 extern ssize_t
redirected_tty_write(struct file
*, const char __user
*,
1863 * job_control - check job control
1865 * @file: file handle
1867 * Perform job control management checks on this file/tty descriptor
1868 * and if appropriate send any needed signals and return a negative
1869 * error code if action should be taken.
1871 * Locking: redirected write test is safe
1872 * current->signal->tty check is safe
1873 * ctrl_lock to safely reference tty->pgrp
1876 static int job_control(struct tty_struct
*tty
, struct file
*file
)
1878 /* Job control check -- must be done at start and after
1879 every sleep (POSIX.1 7.1.1.4). */
1880 /* NOTE: not yet done after every sleep pending a thorough
1881 check of the logic of this change. -- jlc */
1882 /* don't stop on /dev/console */
1883 if (file
->f_op
->write
== redirected_tty_write
||
1884 current
->signal
->tty
!= tty
)
1887 spin_lock_irq(&tty
->ctrl_lock
);
1889 printk(KERN_ERR
"n_tty_read: no tty->pgrp!\n");
1890 else if (task_pgrp(current
) != tty
->pgrp
) {
1891 spin_unlock_irq(&tty
->ctrl_lock
);
1892 if (is_ignored(SIGTTIN
) || is_current_pgrp_orphaned())
1894 kill_pgrp(task_pgrp(current
), SIGTTIN
, 1);
1895 set_thread_flag(TIF_SIGPENDING
);
1896 return -ERESTARTSYS
;
1898 spin_unlock_irq(&tty
->ctrl_lock
);
1904 * n_tty_read - read function for tty
1906 * @file: file object
1907 * @buf: userspace buffer pointer
1910 * Perform reads for the line discipline. We are guaranteed that the
1911 * line discipline will not be closed under us but we may get multiple
1912 * parallel readers and must handle this ourselves. We may also get
1913 * a hangup. Always called in user context, may sleep.
1915 * This code must be sure never to sleep through a hangup.
1918 static ssize_t
n_tty_read(struct tty_struct
*tty
, struct file
*file
,
1919 unsigned char __user
*buf
, size_t nr
)
1921 struct n_tty_data
*ldata
= tty
->disc_data
;
1922 unsigned char __user
*b
= buf
;
1923 DECLARE_WAITQUEUE(wait
, current
);
1929 unsigned long flags
;
1933 c
= job_control(tty
, file
);
1938 timeout
= MAX_SCHEDULE_TIMEOUT
;
1939 if (!ldata
->icanon
) {
1940 minimum
= MIN_CHAR(tty
);
1942 time
= (HZ
/ 10) * TIME_CHAR(tty
);
1944 ldata
->minimum_to_wake
= 1;
1945 else if (!waitqueue_active(&tty
->read_wait
) ||
1946 (ldata
->minimum_to_wake
> minimum
))
1947 ldata
->minimum_to_wake
= minimum
;
1949 timeout
= (HZ
/ 10) * TIME_CHAR(tty
);
1950 ldata
->minimum_to_wake
= minimum
= 1;
1955 * Internal serialization of reads.
1957 if (file
->f_flags
& O_NONBLOCK
) {
1958 if (!mutex_trylock(&ldata
->atomic_read_lock
))
1961 if (mutex_lock_interruptible(&ldata
->atomic_read_lock
))
1962 return -ERESTARTSYS
;
1964 packet
= tty
->packet
;
1966 add_wait_queue(&tty
->read_wait
, &wait
);
1968 /* First test for status change. */
1969 if (packet
&& tty
->link
->ctrl_status
) {
1973 spin_lock_irqsave(&tty
->link
->ctrl_lock
, flags
);
1974 cs
= tty
->link
->ctrl_status
;
1975 tty
->link
->ctrl_status
= 0;
1976 spin_unlock_irqrestore(&tty
->link
->ctrl_lock
, flags
);
1977 if (tty_put_user(tty
, cs
, b
++)) {
1985 /* This statement must be first before checking for input
1986 so that any interrupt will set the state back to
1988 set_current_state(TASK_INTERRUPTIBLE
);
1990 if (((minimum
- (b
- buf
)) < ldata
->minimum_to_wake
) &&
1991 ((minimum
- (b
- buf
)) >= 1))
1992 ldata
->minimum_to_wake
= (minimum
- (b
- buf
));
1994 if (!input_available_p(tty
, 0)) {
1995 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
)) {
1999 if (tty_hung_up_p(file
))
2003 if (file
->f_flags
& O_NONBLOCK
) {
2007 if (signal_pending(current
)) {
2008 retval
= -ERESTARTSYS
;
2011 n_tty_set_room(tty
);
2012 timeout
= schedule_timeout(timeout
);
2015 __set_current_state(TASK_RUNNING
);
2017 /* Deal with packet mode. */
2018 if (packet
&& b
== buf
) {
2019 if (tty_put_user(tty
, TIOCPKT_DATA
, b
++)) {
2027 if (ldata
->icanon
&& !L_EXTPROC(tty
)) {
2028 retval
= canon_copy_from_read_buf(tty
, &b
, &nr
);
2033 /* The copy function takes the read lock and handles
2034 locking internally for this case */
2035 uncopied
= copy_from_read_buf(tty
, &b
, &nr
);
2036 uncopied
+= copy_from_read_buf(tty
, &b
, &nr
);
2043 /* If there is enough space in the read buffer now, let the
2044 * low-level driver know. We use chars_in_buffer() to
2045 * check the buffer, as it now knows about canonical mode.
2046 * Otherwise, if the driver is throttled and the line is
2047 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
2048 * we won't get any more characters.
2051 tty_set_flow_change(tty
, TTY_UNTHROTTLE_SAFE
);
2052 if (chars_in_buffer(tty
) > TTY_THRESHOLD_UNTHROTTLE
)
2056 n_tty_set_room(tty
);
2057 if (!tty_unthrottle_safe(tty
))
2060 __tty_set_flow_change(tty
, 0);
2062 if (b
- buf
>= minimum
)
2067 mutex_unlock(&ldata
->atomic_read_lock
);
2068 remove_wait_queue(&tty
->read_wait
, &wait
);
2070 if (!waitqueue_active(&tty
->read_wait
))
2071 ldata
->minimum_to_wake
= minimum
;
2073 __set_current_state(TASK_RUNNING
);
2078 clear_bit(TTY_PUSH
, &tty
->flags
);
2079 } else if (test_and_clear_bit(TTY_PUSH
, &tty
->flags
))
2082 n_tty_set_room(tty
);
2087 * n_tty_write - write function for tty
2089 * @file: file object
2090 * @buf: userspace buffer pointer
2093 * Write function of the terminal device. This is serialized with
2094 * respect to other write callers but not to termios changes, reads
2095 * and other such events. Since the receive code will echo characters,
2096 * thus calling driver write methods, the output_lock is used in
2097 * the output processing functions called here as well as in the
2098 * echo processing function to protect the column state and space
2099 * left in the buffer.
2101 * This code must be sure never to sleep through a hangup.
2103 * Locking: output_lock to protect column state and space left
2104 * (note that the process_output*() functions take this
2108 static ssize_t
n_tty_write(struct tty_struct
*tty
, struct file
*file
,
2109 const unsigned char *buf
, size_t nr
)
2111 const unsigned char *b
= buf
;
2112 DECLARE_WAITQUEUE(wait
, current
);
2116 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2117 if (L_TOSTOP(tty
) && file
->f_op
->write
!= redirected_tty_write
) {
2118 retval
= tty_check_change(tty
);
2123 /* Write out any echoed characters that are still pending */
2124 process_echoes(tty
);
2126 add_wait_queue(&tty
->write_wait
, &wait
);
2128 set_current_state(TASK_INTERRUPTIBLE
);
2129 if (signal_pending(current
)) {
2130 retval
= -ERESTARTSYS
;
2133 if (tty_hung_up_p(file
) || (tty
->link
&& !tty
->link
->count
)) {
2139 ssize_t num
= process_output_block(tty
, b
, nr
);
2151 if (process_output(c
, tty
) < 0)
2155 if (tty
->ops
->flush_chars
)
2156 tty
->ops
->flush_chars(tty
);
2159 c
= tty
->ops
->write(tty
, b
, nr
);
2172 if (file
->f_flags
& O_NONBLOCK
) {
2179 __set_current_state(TASK_RUNNING
);
2180 remove_wait_queue(&tty
->write_wait
, &wait
);
2181 if (b
- buf
!= nr
&& tty
->fasync
)
2182 set_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
2183 return (b
- buf
) ? b
- buf
: retval
;
2187 * n_tty_poll - poll method for N_TTY
2188 * @tty: terminal device
2189 * @file: file accessing it
2192 * Called when the line discipline is asked to poll() for data or
2193 * for special events. This code is not serialized with respect to
2194 * other events save open/close.
2196 * This code must be sure never to sleep through a hangup.
2197 * Called without the kernel lock held - fine
2200 static unsigned int n_tty_poll(struct tty_struct
*tty
, struct file
*file
,
2203 struct n_tty_data
*ldata
= tty
->disc_data
;
2204 unsigned int mask
= 0;
2206 poll_wait(file
, &tty
->read_wait
, wait
);
2207 poll_wait(file
, &tty
->write_wait
, wait
);
2208 if (input_available_p(tty
, TIME_CHAR(tty
) ? 0 : MIN_CHAR(tty
)))
2209 mask
|= POLLIN
| POLLRDNORM
;
2210 if (tty
->packet
&& tty
->link
->ctrl_status
)
2211 mask
|= POLLPRI
| POLLIN
| POLLRDNORM
;
2212 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
))
2214 if (tty_hung_up_p(file
))
2216 if (!(mask
& (POLLHUP
| POLLIN
| POLLRDNORM
))) {
2217 if (MIN_CHAR(tty
) && !TIME_CHAR(tty
))
2218 ldata
->minimum_to_wake
= MIN_CHAR(tty
);
2220 ldata
->minimum_to_wake
= 1;
2222 if (tty
->ops
->write
&& !tty_is_writelocked(tty
) &&
2223 tty_chars_in_buffer(tty
) < WAKEUP_CHARS
&&
2224 tty_write_room(tty
) > 0)
2225 mask
|= POLLOUT
| POLLWRNORM
;
2229 static unsigned long inq_canon(struct n_tty_data
*ldata
)
2231 size_t nr
, head
, tail
;
2233 if (!ldata
->canon_data
)
2235 head
= ldata
->canon_head
;
2236 tail
= ldata
->read_tail
;
2238 /* Skip EOF-chars.. */
2239 while (head
!= tail
) {
2240 if (test_bit(tail
& (N_TTY_BUF_SIZE
- 1), ldata
->read_flags
) &&
2241 read_buf(ldata
, tail
) == __DISABLED_CHAR
)
2248 static int n_tty_ioctl(struct tty_struct
*tty
, struct file
*file
,
2249 unsigned int cmd
, unsigned long arg
)
2251 struct n_tty_data
*ldata
= tty
->disc_data
;
2256 return put_user(tty_chars_in_buffer(tty
), (int __user
*) arg
);
2258 /* FIXME: Locking */
2259 retval
= read_cnt(ldata
);
2261 retval
= inq_canon(ldata
);
2262 return put_user(retval
, (unsigned int __user
*) arg
);
2264 return n_tty_ioctl_helper(tty
, file
, cmd
, arg
);
2268 static void n_tty_fasync(struct tty_struct
*tty
, int on
)
2270 struct n_tty_data
*ldata
= tty
->disc_data
;
2272 if (!waitqueue_active(&tty
->read_wait
)) {
2274 ldata
->minimum_to_wake
= 1;
2275 else if (!tty
->fasync
)
2276 ldata
->minimum_to_wake
= N_TTY_BUF_SIZE
;
2280 struct tty_ldisc_ops tty_ldisc_N_TTY
= {
2281 .magic
= TTY_LDISC_MAGIC
,
2284 .close
= n_tty_close
,
2285 .flush_buffer
= n_tty_flush_buffer
,
2286 .chars_in_buffer
= n_tty_chars_in_buffer
,
2288 .write
= n_tty_write
,
2289 .ioctl
= n_tty_ioctl
,
2290 .set_termios
= n_tty_set_termios
,
2292 .receive_buf
= n_tty_receive_buf
,
2293 .write_wakeup
= n_tty_write_wakeup
,
2294 .fasync
= n_tty_fasync
,
2295 .receive_buf2
= n_tty_receive_buf2
,
2299 * n_tty_inherit_ops - inherit N_TTY methods
2300 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2302 * Enables a 'subclass' line discipline to 'inherit' N_TTY
2306 void n_tty_inherit_ops(struct tty_ldisc_ops
*ops
)
2308 *ops
= tty_ldisc_N_TTY
;
2310 ops
->refcount
= ops
->flags
= 0;
2312 EXPORT_SYMBOL_GPL(n_tty_inherit_ops
);