2 * n_tty.c --- implements the N_TTY line discipline.
4 * This code used to be in tty_io.c, but things are getting hairy
5 * enough that it made sense to split things off. (The N_TTY
6 * processing has changed so much that it's hardly recognizable,
9 * Note that the open routine for N_TTY is guaranteed never to return
10 * an error. This is because Linux will fall back to setting a line
11 * to N_TTY if it can not switch to any other line discipline.
13 * Written by Theodore Ts'o, Copyright 1994.
15 * This file also contains code originally written by Linus Torvalds,
16 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
18 * This file may be redistributed under the terms of the GNU General Public
21 * Reduced memory usage for older ARM systems - Russell King.
23 * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
24 * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
25 * who actually finally proved there really was a race.
27 * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
28 * waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
29 * Also fixed a bug in BLOCKING mode where n_tty_write returns
33 #include <linux/types.h>
34 #include <linux/major.h>
35 #include <linux/errno.h>
36 #include <linux/signal.h>
37 #include <linux/fcntl.h>
38 #include <linux/sched.h>
39 #include <linux/interrupt.h>
40 #include <linux/tty.h>
41 #include <linux/timer.h>
42 #include <linux/ctype.h>
44 #include <linux/string.h>
45 #include <linux/slab.h>
46 #include <linux/poll.h>
47 #include <linux/bitops.h>
48 #include <linux/audit.h>
49 #include <linux/file.h>
50 #include <linux/uaccess.h>
51 #include <linux/module.h>
52 #include <linux/ratelimit.h>
55 /* number of characters left in xmit buffer before select has we have room */
56 #define WAKEUP_CHARS 256
59 * This defines the low- and high-watermarks for throttling and
60 * unthrottling the TTY driver. These watermarks are used for
61 * controlling the space in the read buffer.
63 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
64 #define TTY_THRESHOLD_UNTHROTTLE 128
67 * Special byte codes used in the echo buffer to represent operations
68 * or special handling of characters. Bytes in the echo buffer that
69 * are not part of such special blocks are treated as normal character
72 #define ECHO_OP_START 0xff
73 #define ECHO_OP_MOVE_BACK_COL 0x80
74 #define ECHO_OP_SET_CANON_COL 0x81
75 #define ECHO_OP_ERASE_TAB 0x82
79 unsigned long overrun_time
;
85 unsigned char lnext
:1, erasing
:1, raw
:1, real_raw
:1, icanon
:1;
86 unsigned char echo_overrun
:1;
88 DECLARE_BITMAP(process_char_map
, 256);
89 DECLARE_BITMAP(read_flags
, N_TTY_BUF_SIZE
);
97 unsigned char *echo_buf
;
98 unsigned int echo_pos
;
99 unsigned int echo_cnt
;
102 unsigned long canon_head
;
103 unsigned int canon_column
;
105 struct mutex atomic_read_lock
;
106 struct mutex output_lock
;
107 struct mutex echo_lock
;
108 raw_spinlock_t read_lock
;
111 static inline int tty_put_user(struct tty_struct
*tty
, unsigned char x
,
112 unsigned char __user
*ptr
)
114 struct n_tty_data
*ldata
= tty
->disc_data
;
116 tty_audit_add_data(tty
, &x
, 1, ldata
->icanon
);
117 return put_user(x
, ptr
);
120 static int receive_room(struct tty_struct
*tty
)
122 struct n_tty_data
*ldata
= tty
->disc_data
;
126 /* Multiply read_cnt by 3, since each byte might take up to
127 * three times as many spaces when PARMRK is set (depending on
128 * its flags, e.g. parity error). */
129 left
= N_TTY_BUF_SIZE
- ldata
->read_cnt
* 3 - 1;
131 left
= N_TTY_BUF_SIZE
- ldata
->read_cnt
- 1;
134 * If we are doing input canonicalization, and there are no
135 * pending newlines, let characters through without limit, so
136 * that erase characters will be handled. Other excess
137 * characters will be beeped.
140 left
= ldata
->icanon
&& !ldata
->canon_data
;
146 * n_tty_set_room - receive space
149 * Re-schedules the flip buffer work if space just became available.
151 * Locks: Concurrent update is protected with read_lock
154 static void n_tty_set_room(struct tty_struct
*tty
)
156 struct n_tty_data
*ldata
= tty
->disc_data
;
158 /* Did this open up the receive buffer? We may need to flip */
159 if (unlikely(ldata
->no_room
) && receive_room(tty
)) {
162 WARN_RATELIMIT(tty
->port
->itty
== NULL
,
163 "scheduling with invalid itty\n");
164 /* see if ldisc has been killed - if so, this means that
165 * even though the ldisc has been halted and ->buf.work
166 * cancelled, ->buf.work is about to be rescheduled
168 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED
, &tty
->flags
),
169 "scheduling buffer work for halted ldisc\n");
170 schedule_work(&tty
->port
->buf
.work
);
174 static void put_tty_queue_nolock(unsigned char c
, struct n_tty_data
*ldata
)
176 if (ldata
->read_cnt
< N_TTY_BUF_SIZE
) {
177 ldata
->read_buf
[ldata
->read_head
] = c
;
178 ldata
->read_head
= (ldata
->read_head
+ 1) & (N_TTY_BUF_SIZE
-1);
184 * put_tty_queue - add character to tty
188 * Add a character to the tty read_buf queue. This is done under the
189 * read_lock to serialize character addition and also to protect us
190 * against parallel reads or flushes
193 static void put_tty_queue(unsigned char c
, struct n_tty_data
*ldata
)
197 * The problem of stomping on the buffers ends here.
198 * Why didn't anyone see this one coming? --AJK
200 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
201 put_tty_queue_nolock(c
, ldata
);
202 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
206 * reset_buffer_flags - reset buffer state
207 * @tty: terminal to reset
209 * Reset the read buffer counters and clear the flags.
210 * Called from n_tty_open() and n_tty_flush_buffer().
212 * Locking: tty_read_lock for read fields.
215 static void reset_buffer_flags(struct n_tty_data
*ldata
)
219 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
220 ldata
->read_head
= ldata
->read_tail
= ldata
->read_cnt
= 0;
221 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
223 mutex_lock(&ldata
->echo_lock
);
224 ldata
->echo_pos
= ldata
->echo_cnt
= ldata
->echo_overrun
= 0;
225 mutex_unlock(&ldata
->echo_lock
);
227 ldata
->canon_head
= ldata
->canon_data
= ldata
->erasing
= 0;
228 bitmap_zero(ldata
->read_flags
, N_TTY_BUF_SIZE
);
231 static void n_tty_packet_mode_flush(struct tty_struct
*tty
)
235 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
236 if (tty
->link
->packet
) {
237 tty
->ctrl_status
|= TIOCPKT_FLUSHREAD
;
238 wake_up_interruptible(&tty
->link
->read_wait
);
240 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
244 * n_tty_flush_buffer - clean input queue
245 * @tty: terminal device
247 * Flush the input buffer. Called when the tty layer wants the
248 * buffer flushed (eg at hangup) or when the N_TTY line discipline
249 * internally has to clean the pending queue (for example some signals).
251 * Locking: ctrl_lock, read_lock.
254 static void n_tty_flush_buffer(struct tty_struct
*tty
)
256 reset_buffer_flags(tty
->disc_data
);
260 n_tty_packet_mode_flush(tty
);
264 * n_tty_chars_in_buffer - report available bytes
267 * Report the number of characters buffered to be delivered to user
268 * at this instant in time.
273 static ssize_t
n_tty_chars_in_buffer(struct tty_struct
*tty
)
275 struct n_tty_data
*ldata
= tty
->disc_data
;
279 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
280 if (!ldata
->icanon
) {
282 } else if (ldata
->canon_data
) {
283 n
= (ldata
->canon_head
> ldata
->read_tail
) ?
284 ldata
->canon_head
- ldata
->read_tail
:
285 ldata
->canon_head
+ (N_TTY_BUF_SIZE
- ldata
->read_tail
);
287 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
292 * is_utf8_continuation - utf8 multibyte check
295 * Returns true if the utf8 character 'c' is a multibyte continuation
296 * character. We use this to correctly compute the on screen size
297 * of the character when printing
300 static inline int is_utf8_continuation(unsigned char c
)
302 return (c
& 0xc0) == 0x80;
306 * is_continuation - multibyte check
309 * Returns true if the utf8 character 'c' is a multibyte continuation
310 * character and the terminal is in unicode mode.
313 static inline int is_continuation(unsigned char c
, struct tty_struct
*tty
)
315 return I_IUTF8(tty
) && is_utf8_continuation(c
);
319 * do_output_char - output one character
320 * @c: character (or partial unicode symbol)
321 * @tty: terminal device
322 * @space: space available in tty driver write buffer
324 * This is a helper function that handles one output character
325 * (including special characters like TAB, CR, LF, etc.),
326 * doing OPOST processing and putting the results in the
327 * tty driver's write buffer.
329 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
330 * and NLDLY. They simply aren't relevant in the world today.
331 * If you ever need them, add them here.
333 * Returns the number of bytes of buffer space used or -1 if
336 * Locking: should be called under the output_lock to protect
337 * the column state and space left in the buffer
340 static int do_output_char(unsigned char c
, struct tty_struct
*tty
, int space
)
342 struct n_tty_data
*ldata
= tty
->disc_data
;
355 ldata
->canon_column
= ldata
->column
= 0;
356 tty
->ops
->write(tty
, "\r\n", 2);
359 ldata
->canon_column
= ldata
->column
;
362 if (O_ONOCR(tty
) && ldata
->column
== 0)
367 ldata
->canon_column
= ldata
->column
= 0;
370 ldata
->canon_column
= ldata
->column
= 0;
373 spaces
= 8 - (ldata
->column
& 7);
374 if (O_TABDLY(tty
) == XTABS
) {
377 ldata
->column
+= spaces
;
378 tty
->ops
->write(tty
, " ", spaces
);
381 ldata
->column
+= spaces
;
384 if (ldata
->column
> 0)
391 if (!is_continuation(c
, tty
))
397 tty_put_char(tty
, c
);
402 * process_output - output post processor
403 * @c: character (or partial unicode symbol)
404 * @tty: terminal device
406 * Output one character with OPOST processing.
407 * Returns -1 when the output device is full and the character
410 * Locking: output_lock to protect column state and space left
411 * (also, this is called from n_tty_write under the
412 * tty layer write lock)
415 static int process_output(unsigned char c
, struct tty_struct
*tty
)
417 struct n_tty_data
*ldata
= tty
->disc_data
;
420 mutex_lock(&ldata
->output_lock
);
422 space
= tty_write_room(tty
);
423 retval
= do_output_char(c
, tty
, space
);
425 mutex_unlock(&ldata
->output_lock
);
433 * process_output_block - block post processor
434 * @tty: terminal device
435 * @buf: character buffer
436 * @nr: number of bytes to output
438 * Output a block of characters with OPOST processing.
439 * Returns the number of characters output.
441 * This path is used to speed up block console writes, among other
442 * things when processing blocks of output data. It handles only
443 * the simple cases normally found and helps to generate blocks of
444 * symbols for the console driver and thus improve performance.
446 * Locking: output_lock to protect column state and space left
447 * (also, this is called from n_tty_write under the
448 * tty layer write lock)
451 static ssize_t
process_output_block(struct tty_struct
*tty
,
452 const unsigned char *buf
, unsigned int nr
)
454 struct n_tty_data
*ldata
= tty
->disc_data
;
457 const unsigned char *cp
;
459 mutex_lock(&ldata
->output_lock
);
461 space
= tty_write_room(tty
);
463 mutex_unlock(&ldata
->output_lock
);
469 for (i
= 0, cp
= buf
; i
< nr
; i
++, cp
++) {
470 unsigned char c
= *cp
;
478 ldata
->canon_column
= ldata
->column
;
481 if (O_ONOCR(tty
) && ldata
->column
== 0)
485 ldata
->canon_column
= ldata
->column
= 0;
490 if (ldata
->column
> 0)
497 if (!is_continuation(c
, tty
))
504 i
= tty
->ops
->write(tty
, buf
, i
);
506 mutex_unlock(&ldata
->output_lock
);
511 * process_echoes - write pending echo characters
512 * @tty: terminal device
514 * Write previously buffered echo (and other ldisc-generated)
515 * characters to the tty.
517 * Characters generated by the ldisc (including echoes) need to
518 * be buffered because the driver's write buffer can fill during
519 * heavy program output. Echoing straight to the driver will
520 * often fail under these conditions, causing lost characters and
521 * resulting mismatches of ldisc state information.
523 * Since the ldisc state must represent the characters actually sent
524 * to the driver at the time of the write, operations like certain
525 * changes in column state are also saved in the buffer and executed
528 * A circular fifo buffer is used so that the most recent characters
529 * are prioritized. Also, when control characters are echoed with a
530 * prefixed "^", the pair is treated atomically and thus not separated.
532 * Locking: output_lock to protect column state and space left,
533 * echo_lock to protect the echo buffer
536 static void process_echoes(struct tty_struct
*tty
)
538 struct n_tty_data
*ldata
= tty
->disc_data
;
541 unsigned char *cp
, *buf_end
;
543 if (!ldata
->echo_cnt
)
546 mutex_lock(&ldata
->output_lock
);
547 mutex_lock(&ldata
->echo_lock
);
549 space
= tty_write_room(tty
);
551 buf_end
= ldata
->echo_buf
+ N_TTY_BUF_SIZE
;
552 cp
= ldata
->echo_buf
+ ldata
->echo_pos
;
553 nr
= ldata
->echo_cnt
;
556 if (c
== ECHO_OP_START
) {
559 int no_space_left
= 0;
562 * If the buffer byte is the start of a multi-byte
563 * operation, get the next byte, which is either the
564 * op code or a control character value.
568 opp
-= N_TTY_BUF_SIZE
;
572 unsigned int num_chars
, num_bs
;
574 case ECHO_OP_ERASE_TAB
:
575 if (++opp
== buf_end
)
576 opp
-= N_TTY_BUF_SIZE
;
580 * Determine how many columns to go back
581 * in order to erase the tab.
582 * This depends on the number of columns
583 * used by other characters within the tab
584 * area. If this (modulo 8) count is from
585 * the start of input rather than from a
586 * previous tab, we offset by canon column.
587 * Otherwise, tab spacing is normal.
589 if (!(num_chars
& 0x80))
590 num_chars
+= ldata
->canon_column
;
591 num_bs
= 8 - (num_chars
& 7);
593 if (num_bs
> space
) {
599 tty_put_char(tty
, '\b');
600 if (ldata
->column
> 0)
607 case ECHO_OP_SET_CANON_COL
:
608 ldata
->canon_column
= ldata
->column
;
613 case ECHO_OP_MOVE_BACK_COL
:
614 if (ldata
->column
> 0)
621 /* This is an escaped echo op start code */
626 tty_put_char(tty
, ECHO_OP_START
);
635 * If the op is not a special byte code,
636 * it is a ctrl char tagged to be echoed
637 * as "^X" (where X is the letter
638 * representing the control char).
639 * Note that we must ensure there is
640 * enough space for the whole ctrl pair.
647 tty_put_char(tty
, '^');
648 tty_put_char(tty
, op
^ 0100);
659 int retval
= do_output_char(c
, tty
, space
);
666 tty_put_char(tty
, c
);
673 /* When end of circular buffer reached, wrap around */
675 cp
-= N_TTY_BUF_SIZE
;
681 ldata
->echo_overrun
= 0;
683 int num_processed
= ldata
->echo_cnt
- nr
;
684 ldata
->echo_pos
+= num_processed
;
685 ldata
->echo_pos
&= N_TTY_BUF_SIZE
- 1;
686 ldata
->echo_cnt
= nr
;
687 if (num_processed
> 0)
688 ldata
->echo_overrun
= 0;
691 mutex_unlock(&ldata
->echo_lock
);
692 mutex_unlock(&ldata
->output_lock
);
694 if (tty
->ops
->flush_chars
)
695 tty
->ops
->flush_chars(tty
);
699 * add_echo_byte - add a byte to the echo buffer
700 * @c: unicode byte to echo
703 * Add a character or operation byte to the echo buffer.
705 * Should be called under the echo lock to protect the echo buffer.
708 static void add_echo_byte(unsigned char c
, struct n_tty_data
*ldata
)
712 if (ldata
->echo_cnt
== N_TTY_BUF_SIZE
) {
713 /* Circular buffer is already at capacity */
714 new_byte_pos
= ldata
->echo_pos
;
717 * Since the buffer start position needs to be advanced,
718 * be sure to step by a whole operation byte group.
720 if (ldata
->echo_buf
[ldata
->echo_pos
] == ECHO_OP_START
) {
721 if (ldata
->echo_buf
[(ldata
->echo_pos
+ 1) &
722 (N_TTY_BUF_SIZE
- 1)] ==
724 ldata
->echo_pos
+= 3;
725 ldata
->echo_cnt
-= 2;
727 ldata
->echo_pos
+= 2;
728 ldata
->echo_cnt
-= 1;
733 ldata
->echo_pos
&= N_TTY_BUF_SIZE
- 1;
735 ldata
->echo_overrun
= 1;
737 new_byte_pos
= ldata
->echo_pos
+ ldata
->echo_cnt
;
738 new_byte_pos
&= N_TTY_BUF_SIZE
- 1;
742 ldata
->echo_buf
[new_byte_pos
] = c
;
746 * echo_move_back_col - add operation to move back a column
749 * Add an operation to the echo buffer to move back one column.
751 * Locking: echo_lock to protect the echo buffer
754 static void echo_move_back_col(struct n_tty_data
*ldata
)
756 mutex_lock(&ldata
->echo_lock
);
757 add_echo_byte(ECHO_OP_START
, ldata
);
758 add_echo_byte(ECHO_OP_MOVE_BACK_COL
, ldata
);
759 mutex_unlock(&ldata
->echo_lock
);
763 * echo_set_canon_col - add operation to set the canon column
766 * Add an operation to the echo buffer to set the canon column
767 * to the current column.
769 * Locking: echo_lock to protect the echo buffer
772 static void echo_set_canon_col(struct n_tty_data
*ldata
)
774 mutex_lock(&ldata
->echo_lock
);
775 add_echo_byte(ECHO_OP_START
, ldata
);
776 add_echo_byte(ECHO_OP_SET_CANON_COL
, ldata
);
777 mutex_unlock(&ldata
->echo_lock
);
781 * echo_erase_tab - add operation to erase a tab
782 * @num_chars: number of character columns already used
783 * @after_tab: true if num_chars starts after a previous tab
786 * Add an operation to the echo buffer to erase a tab.
788 * Called by the eraser function, which knows how many character
789 * columns have been used since either a previous tab or the start
790 * of input. This information will be used later, along with
791 * canon column (if applicable), to go back the correct number
794 * Locking: echo_lock to protect the echo buffer
797 static void echo_erase_tab(unsigned int num_chars
, int after_tab
,
798 struct n_tty_data
*ldata
)
800 mutex_lock(&ldata
->echo_lock
);
802 add_echo_byte(ECHO_OP_START
, ldata
);
803 add_echo_byte(ECHO_OP_ERASE_TAB
, ldata
);
805 /* We only need to know this modulo 8 (tab spacing) */
808 /* Set the high bit as a flag if num_chars is after a previous tab */
812 add_echo_byte(num_chars
, ldata
);
814 mutex_unlock(&ldata
->echo_lock
);
818 * echo_char_raw - echo a character raw
819 * @c: unicode byte to echo
820 * @tty: terminal device
822 * Echo user input back onto the screen. This must be called only when
823 * L_ECHO(tty) is true. Called from the driver receive_buf path.
825 * This variant does not treat control characters specially.
827 * Locking: echo_lock to protect the echo buffer
830 static void echo_char_raw(unsigned char c
, struct n_tty_data
*ldata
)
832 mutex_lock(&ldata
->echo_lock
);
833 if (c
== ECHO_OP_START
) {
834 add_echo_byte(ECHO_OP_START
, ldata
);
835 add_echo_byte(ECHO_OP_START
, ldata
);
837 add_echo_byte(c
, ldata
);
839 mutex_unlock(&ldata
->echo_lock
);
843 * echo_char - echo a character
844 * @c: unicode byte to echo
845 * @tty: terminal device
847 * Echo user input back onto the screen. This must be called only when
848 * L_ECHO(tty) is true. Called from the driver receive_buf path.
850 * This variant tags control characters to be echoed as "^X"
851 * (where X is the letter representing the control char).
853 * Locking: echo_lock to protect the echo buffer
856 static void echo_char(unsigned char c
, struct tty_struct
*tty
)
858 struct n_tty_data
*ldata
= tty
->disc_data
;
860 mutex_lock(&ldata
->echo_lock
);
862 if (c
== ECHO_OP_START
) {
863 add_echo_byte(ECHO_OP_START
, ldata
);
864 add_echo_byte(ECHO_OP_START
, ldata
);
866 if (L_ECHOCTL(tty
) && iscntrl(c
) && c
!= '\t')
867 add_echo_byte(ECHO_OP_START
, ldata
);
868 add_echo_byte(c
, ldata
);
871 mutex_unlock(&ldata
->echo_lock
);
875 * finish_erasing - complete erase
879 static inline void finish_erasing(struct n_tty_data
*ldata
)
881 if (ldata
->erasing
) {
882 echo_char_raw('/', ldata
);
888 * eraser - handle erase function
889 * @c: character input
890 * @tty: terminal device
892 * Perform erase and necessary output when an erase character is
893 * present in the stream from the driver layer. Handles the complexities
894 * of UTF-8 multibyte symbols.
896 * Locking: read_lock for tty buffers
899 static void eraser(unsigned char c
, struct tty_struct
*tty
)
901 struct n_tty_data
*ldata
= tty
->disc_data
;
902 enum { ERASE
, WERASE
, KILL
} kill_type
;
903 int head
, seen_alnums
, cnt
;
906 /* FIXME: locking needed ? */
907 if (ldata
->read_head
== ldata
->canon_head
) {
908 /* process_output('\a', tty); */ /* what do you think? */
911 if (c
== ERASE_CHAR(tty
))
913 else if (c
== WERASE_CHAR(tty
))
917 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
918 ldata
->read_cnt
-= ((ldata
->read_head
- ldata
->canon_head
) &
919 (N_TTY_BUF_SIZE
- 1));
920 ldata
->read_head
= ldata
->canon_head
;
921 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
924 if (!L_ECHOK(tty
) || !L_ECHOKE(tty
) || !L_ECHOE(tty
)) {
925 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
926 ldata
->read_cnt
-= ((ldata
->read_head
- ldata
->canon_head
) &
927 (N_TTY_BUF_SIZE
- 1));
928 ldata
->read_head
= ldata
->canon_head
;
929 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
930 finish_erasing(ldata
);
931 echo_char(KILL_CHAR(tty
), tty
);
932 /* Add a newline if ECHOK is on and ECHOKE is off. */
934 echo_char_raw('\n', ldata
);
941 /* FIXME: Locking ?? */
942 while (ldata
->read_head
!= ldata
->canon_head
) {
943 head
= ldata
->read_head
;
945 /* erase a single possibly multibyte character */
947 head
= (head
- 1) & (N_TTY_BUF_SIZE
-1);
948 c
= ldata
->read_buf
[head
];
949 } while (is_continuation(c
, tty
) && head
!= ldata
->canon_head
);
951 /* do not partially erase */
952 if (is_continuation(c
, tty
))
955 if (kill_type
== WERASE
) {
956 /* Equivalent to BSD's ALTWERASE. */
957 if (isalnum(c
) || c
== '_')
959 else if (seen_alnums
)
962 cnt
= (ldata
->read_head
- head
) & (N_TTY_BUF_SIZE
-1);
963 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
964 ldata
->read_head
= head
;
965 ldata
->read_cnt
-= cnt
;
966 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
968 if (L_ECHOPRT(tty
)) {
969 if (!ldata
->erasing
) {
970 echo_char_raw('\\', ldata
);
973 /* if cnt > 1, output a multi-byte character */
976 head
= (head
+1) & (N_TTY_BUF_SIZE
-1);
977 echo_char_raw(ldata
->read_buf
[head
],
979 echo_move_back_col(ldata
);
981 } else if (kill_type
== ERASE
&& !L_ECHOE(tty
)) {
982 echo_char(ERASE_CHAR(tty
), tty
);
983 } else if (c
== '\t') {
984 unsigned int num_chars
= 0;
986 unsigned long tail
= ldata
->read_head
;
989 * Count the columns used for characters
990 * since the start of input or after a
992 * This info is used to go back the correct
995 while (tail
!= ldata
->canon_head
) {
996 tail
= (tail
-1) & (N_TTY_BUF_SIZE
-1);
997 c
= ldata
->read_buf
[tail
];
1001 } else if (iscntrl(c
)) {
1004 } else if (!is_continuation(c
, tty
)) {
1008 echo_erase_tab(num_chars
, after_tab
, ldata
);
1010 if (iscntrl(c
) && L_ECHOCTL(tty
)) {
1011 echo_char_raw('\b', ldata
);
1012 echo_char_raw(' ', ldata
);
1013 echo_char_raw('\b', ldata
);
1015 if (!iscntrl(c
) || L_ECHOCTL(tty
)) {
1016 echo_char_raw('\b', ldata
);
1017 echo_char_raw(' ', ldata
);
1018 echo_char_raw('\b', ldata
);
1022 if (kill_type
== ERASE
)
1025 if (ldata
->read_head
== ldata
->canon_head
&& L_ECHO(tty
))
1026 finish_erasing(ldata
);
1030 * isig - handle the ISIG optio
1034 * Called when a signal is being sent due to terminal input.
1035 * Called from the driver receive_buf path so serialized.
1037 * Locking: ctrl_lock
1040 static inline void isig(int sig
, struct tty_struct
*tty
)
1042 struct pid
*tty_pgrp
= tty_get_pgrp(tty
);
1044 kill_pgrp(tty_pgrp
, sig
, 1);
1050 * n_tty_receive_break - handle break
1053 * An RS232 break event has been hit in the incoming bitstream. This
1054 * can cause a variety of events depending upon the termios settings.
1056 * Called from the receive_buf path so single threaded.
1059 static inline void n_tty_receive_break(struct tty_struct
*tty
)
1061 struct n_tty_data
*ldata
= tty
->disc_data
;
1065 if (I_BRKINT(tty
)) {
1067 if (!L_NOFLSH(tty
)) {
1068 n_tty_flush_buffer(tty
);
1069 tty_driver_flush_buffer(tty
);
1073 if (I_PARMRK(tty
)) {
1074 put_tty_queue('\377', ldata
);
1075 put_tty_queue('\0', ldata
);
1077 put_tty_queue('\0', ldata
);
1078 wake_up_interruptible(&tty
->read_wait
);
1082 * n_tty_receive_overrun - handle overrun reporting
1085 * Data arrived faster than we could process it. While the tty
1086 * driver has flagged this the bits that were missed are gone
1089 * Called from the receive_buf path so single threaded. Does not
1090 * need locking as num_overrun and overrun_time are function
1094 static inline void n_tty_receive_overrun(struct tty_struct
*tty
)
1096 struct n_tty_data
*ldata
= tty
->disc_data
;
1099 ldata
->num_overrun
++;
1100 if (time_after(jiffies
, ldata
->overrun_time
+ HZ
) ||
1101 time_after(ldata
->overrun_time
, jiffies
)) {
1102 printk(KERN_WARNING
"%s: %d input overrun(s)\n",
1104 ldata
->num_overrun
);
1105 ldata
->overrun_time
= jiffies
;
1106 ldata
->num_overrun
= 0;
1111 * n_tty_receive_parity_error - error notifier
1112 * @tty: terminal device
1115 * Process a parity error and queue the right data to indicate
1116 * the error case if necessary. Locking as per n_tty_receive_buf.
1118 static inline void n_tty_receive_parity_error(struct tty_struct
*tty
,
1121 struct n_tty_data
*ldata
= tty
->disc_data
;
1125 if (I_PARMRK(tty
)) {
1126 put_tty_queue('\377', ldata
);
1127 put_tty_queue('\0', ldata
);
1128 put_tty_queue(c
, ldata
);
1129 } else if (I_INPCK(tty
))
1130 put_tty_queue('\0', ldata
);
1132 put_tty_queue(c
, ldata
);
1133 wake_up_interruptible(&tty
->read_wait
);
1137 * n_tty_receive_char - perform processing
1138 * @tty: terminal device
1141 * Process an individual character of input received from the driver.
1142 * This is serialized with respect to itself by the rules for the
1146 static inline void n_tty_receive_char(struct tty_struct
*tty
, unsigned char c
)
1148 struct n_tty_data
*ldata
= tty
->disc_data
;
1149 unsigned long flags
;
1153 put_tty_queue(c
, ldata
);
1159 if (I_IUCLC(tty
) && L_IEXTEN(tty
))
1162 if (L_EXTPROC(tty
)) {
1163 put_tty_queue(c
, ldata
);
1167 if (tty
->stopped
&& !tty
->flow_stopped
&& I_IXON(tty
) &&
1168 I_IXANY(tty
) && c
!= START_CHAR(tty
) && c
!= STOP_CHAR(tty
) &&
1169 c
!= INTR_CHAR(tty
) && c
!= QUIT_CHAR(tty
) && c
!= SUSP_CHAR(tty
)) {
1171 process_echoes(tty
);
1176 if (c
== START_CHAR(tty
)) {
1178 process_echoes(tty
);
1179 } else if (c
== STOP_CHAR(tty
))
1186 * If the previous character was LNEXT, or we know that this
1187 * character is not one of the characters that we'll have to
1188 * handle specially, do shortcut processing to speed things
1191 if (!test_bit(c
, ldata
->process_char_map
) || ldata
->lnext
) {
1193 parmrk
= (c
== (unsigned char) '\377' && I_PARMRK(tty
)) ? 1 : 0;
1194 if (ldata
->read_cnt
>= (N_TTY_BUF_SIZE
- parmrk
- 1)) {
1195 /* beep if no space */
1197 process_output('\a', tty
);
1201 finish_erasing(ldata
);
1202 /* Record the column of first canon char. */
1203 if (ldata
->canon_head
== ldata
->read_head
)
1204 echo_set_canon_col(ldata
);
1206 process_echoes(tty
);
1209 put_tty_queue(c
, ldata
);
1210 put_tty_queue(c
, ldata
);
1215 if (c
== START_CHAR(tty
)) {
1217 process_echoes(tty
);
1220 if (c
== STOP_CHAR(tty
)) {
1229 if (c
== INTR_CHAR(tty
))
1232 if (c
== QUIT_CHAR(tty
))
1235 if (c
== SUSP_CHAR(tty
)) {
1237 if (!L_NOFLSH(tty
)) {
1238 n_tty_flush_buffer(tty
);
1239 tty_driver_flush_buffer(tty
);
1245 process_echoes(tty
);
1257 } else if (c
== '\n' && I_INLCR(tty
))
1260 if (ldata
->icanon
) {
1261 if (c
== ERASE_CHAR(tty
) || c
== KILL_CHAR(tty
) ||
1262 (c
== WERASE_CHAR(tty
) && L_IEXTEN(tty
))) {
1264 process_echoes(tty
);
1267 if (c
== LNEXT_CHAR(tty
) && L_IEXTEN(tty
)) {
1270 finish_erasing(ldata
);
1271 if (L_ECHOCTL(tty
)) {
1272 echo_char_raw('^', ldata
);
1273 echo_char_raw('\b', ldata
);
1274 process_echoes(tty
);
1279 if (c
== REPRINT_CHAR(tty
) && L_ECHO(tty
) &&
1281 unsigned long tail
= ldata
->canon_head
;
1283 finish_erasing(ldata
);
1285 echo_char_raw('\n', ldata
);
1286 while (tail
!= ldata
->read_head
) {
1287 echo_char(ldata
->read_buf
[tail
], tty
);
1288 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
1290 process_echoes(tty
);
1294 if (ldata
->read_cnt
>= N_TTY_BUF_SIZE
) {
1296 process_output('\a', tty
);
1299 if (L_ECHO(tty
) || L_ECHONL(tty
)) {
1300 echo_char_raw('\n', ldata
);
1301 process_echoes(tty
);
1303 goto handle_newline
;
1305 if (c
== EOF_CHAR(tty
)) {
1306 if (ldata
->read_cnt
>= N_TTY_BUF_SIZE
)
1308 if (ldata
->canon_head
!= ldata
->read_head
)
1309 set_bit(TTY_PUSH
, &tty
->flags
);
1310 c
= __DISABLED_CHAR
;
1311 goto handle_newline
;
1313 if ((c
== EOL_CHAR(tty
)) ||
1314 (c
== EOL2_CHAR(tty
) && L_IEXTEN(tty
))) {
1315 parmrk
= (c
== (unsigned char) '\377' && I_PARMRK(tty
))
1317 if (ldata
->read_cnt
>= (N_TTY_BUF_SIZE
- parmrk
)) {
1319 process_output('\a', tty
);
1323 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1326 /* Record the column of first canon char. */
1327 if (ldata
->canon_head
== ldata
->read_head
)
1328 echo_set_canon_col(ldata
);
1330 process_echoes(tty
);
1333 * XXX does PARMRK doubling happen for
1334 * EOL_CHAR and EOL2_CHAR?
1337 put_tty_queue(c
, ldata
);
1340 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
1341 set_bit(ldata
->read_head
, ldata
->read_flags
);
1342 put_tty_queue_nolock(c
, ldata
);
1343 ldata
->canon_head
= ldata
->read_head
;
1344 ldata
->canon_data
++;
1345 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
1346 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
1347 if (waitqueue_active(&tty
->read_wait
))
1348 wake_up_interruptible(&tty
->read_wait
);
1353 parmrk
= (c
== (unsigned char) '\377' && I_PARMRK(tty
)) ? 1 : 0;
1354 if (ldata
->read_cnt
>= (N_TTY_BUF_SIZE
- parmrk
- 1)) {
1355 /* beep if no space */
1357 process_output('\a', tty
);
1361 finish_erasing(ldata
);
1363 echo_char_raw('\n', ldata
);
1365 /* Record the column of first canon char. */
1366 if (ldata
->canon_head
== ldata
->read_head
)
1367 echo_set_canon_col(ldata
);
1370 process_echoes(tty
);
1374 put_tty_queue(c
, ldata
);
1376 put_tty_queue(c
, ldata
);
1381 * n_tty_write_wakeup - asynchronous I/O notifier
1384 * Required for the ptys, serial driver etc. since processes
1385 * that attach themselves to the master and rely on ASYNC
1386 * IO must be woken up
1389 static void n_tty_write_wakeup(struct tty_struct
*tty
)
1391 if (tty
->fasync
&& test_and_clear_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
))
1392 kill_fasync(&tty
->fasync
, SIGIO
, POLL_OUT
);
1396 * n_tty_receive_buf - data receive
1397 * @tty: terminal device
1400 * @count: characters
1402 * Called by the terminal driver when a block of characters has
1403 * been received. This function must be called from soft contexts
1404 * not from interrupt context. The driver is responsible for making
1405 * calls one at a time and in order (or using flush_to_ldisc)
1408 static void __receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
1409 char *fp
, int count
)
1411 struct n_tty_data
*ldata
= tty
->disc_data
;
1412 const unsigned char *p
;
1413 char *f
, flags
= TTY_NORMAL
;
1416 unsigned long cpuflags
;
1418 if (ldata
->real_raw
) {
1419 raw_spin_lock_irqsave(&ldata
->read_lock
, cpuflags
);
1420 i
= min(N_TTY_BUF_SIZE
- ldata
->read_cnt
,
1421 N_TTY_BUF_SIZE
- ldata
->read_head
);
1423 memcpy(ldata
->read_buf
+ ldata
->read_head
, cp
, i
);
1424 ldata
->read_head
= (ldata
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
1425 ldata
->read_cnt
+= i
;
1429 i
= min(N_TTY_BUF_SIZE
- ldata
->read_cnt
,
1430 N_TTY_BUF_SIZE
- ldata
->read_head
);
1432 memcpy(ldata
->read_buf
+ ldata
->read_head
, cp
, i
);
1433 ldata
->read_head
= (ldata
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
1434 ldata
->read_cnt
+= i
;
1435 raw_spin_unlock_irqrestore(&ldata
->read_lock
, cpuflags
);
1437 for (i
= count
, p
= cp
, f
= fp
; i
; i
--, p
++) {
1442 n_tty_receive_char(tty
, *p
);
1445 n_tty_receive_break(tty
);
1449 n_tty_receive_parity_error(tty
, *p
);
1452 n_tty_receive_overrun(tty
);
1455 printk(KERN_ERR
"%s: unknown flag %d\n",
1456 tty_name(tty
, buf
), flags
);
1460 if (tty
->ops
->flush_chars
)
1461 tty
->ops
->flush_chars(tty
);
1464 if ((!ldata
->icanon
&& (ldata
->read_cnt
>= ldata
->minimum_to_wake
)) ||
1466 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
1467 if (waitqueue_active(&tty
->read_wait
))
1468 wake_up_interruptible(&tty
->read_wait
);
1472 * Check the remaining room for the input canonicalization
1473 * mode. We don't want to throttle the driver if we're in
1474 * canonical mode and don't have a newline yet!
1477 tty_set_flow_change(tty
, TTY_THROTTLE_SAFE
);
1478 if (receive_room(tty
) >= TTY_THRESHOLD_THROTTLE
)
1480 if (!tty_throttle_safe(tty
))
1483 __tty_set_flow_change(tty
, 0);
1486 static void n_tty_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
1487 char *fp
, int count
)
1489 __receive_buf(tty
, cp
, fp
, count
);
1492 static int n_tty_receive_buf2(struct tty_struct
*tty
, const unsigned char *cp
,
1493 char *fp
, int count
)
1495 struct n_tty_data
*ldata
= tty
->disc_data
;
1498 tty
->receive_room
= room
= receive_room(tty
);
1501 count
= min(count
, room
);
1503 __receive_buf(tty
, cp
, fp
, count
);
1508 int is_ignored(int sig
)
1510 return (sigismember(¤t
->blocked
, sig
) ||
1511 current
->sighand
->action
[sig
-1].sa
.sa_handler
== SIG_IGN
);
1515 * n_tty_set_termios - termios data changed
1517 * @old: previous data
1519 * Called by the tty layer when the user changes termios flags so
1520 * that the line discipline can plan ahead. This function cannot sleep
1521 * and is protected from re-entry by the tty layer. The user is
1522 * guaranteed that this function will not be re-entered or in progress
1523 * when the ldisc is closed.
1525 * Locking: Caller holds tty->termios_mutex
1528 static void n_tty_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
1530 struct n_tty_data
*ldata
= tty
->disc_data
;
1531 int canon_change
= 1;
1534 canon_change
= (old
->c_lflag
^ tty
->termios
.c_lflag
) & ICANON
;
1536 bitmap_zero(ldata
->read_flags
, N_TTY_BUF_SIZE
);
1537 ldata
->canon_head
= ldata
->read_tail
;
1538 ldata
->canon_data
= 0;
1542 if (canon_change
&& !L_ICANON(tty
) && ldata
->read_cnt
)
1543 wake_up_interruptible(&tty
->read_wait
);
1545 ldata
->icanon
= (L_ICANON(tty
) != 0);
1547 if (I_ISTRIP(tty
) || I_IUCLC(tty
) || I_IGNCR(tty
) ||
1548 I_ICRNL(tty
) || I_INLCR(tty
) || L_ICANON(tty
) ||
1549 I_IXON(tty
) || L_ISIG(tty
) || L_ECHO(tty
) ||
1551 bitmap_zero(ldata
->process_char_map
, 256);
1553 if (I_IGNCR(tty
) || I_ICRNL(tty
))
1554 set_bit('\r', ldata
->process_char_map
);
1556 set_bit('\n', ldata
->process_char_map
);
1558 if (L_ICANON(tty
)) {
1559 set_bit(ERASE_CHAR(tty
), ldata
->process_char_map
);
1560 set_bit(KILL_CHAR(tty
), ldata
->process_char_map
);
1561 set_bit(EOF_CHAR(tty
), ldata
->process_char_map
);
1562 set_bit('\n', ldata
->process_char_map
);
1563 set_bit(EOL_CHAR(tty
), ldata
->process_char_map
);
1564 if (L_IEXTEN(tty
)) {
1565 set_bit(WERASE_CHAR(tty
),
1566 ldata
->process_char_map
);
1567 set_bit(LNEXT_CHAR(tty
),
1568 ldata
->process_char_map
);
1569 set_bit(EOL2_CHAR(tty
),
1570 ldata
->process_char_map
);
1572 set_bit(REPRINT_CHAR(tty
),
1573 ldata
->process_char_map
);
1577 set_bit(START_CHAR(tty
), ldata
->process_char_map
);
1578 set_bit(STOP_CHAR(tty
), ldata
->process_char_map
);
1581 set_bit(INTR_CHAR(tty
), ldata
->process_char_map
);
1582 set_bit(QUIT_CHAR(tty
), ldata
->process_char_map
);
1583 set_bit(SUSP_CHAR(tty
), ldata
->process_char_map
);
1585 clear_bit(__DISABLED_CHAR
, ldata
->process_char_map
);
1587 ldata
->real_raw
= 0;
1590 if ((I_IGNBRK(tty
) || (!I_BRKINT(tty
) && !I_PARMRK(tty
))) &&
1591 (I_IGNPAR(tty
) || !I_INPCK(tty
)) &&
1592 (tty
->driver
->flags
& TTY_DRIVER_REAL_RAW
))
1593 ldata
->real_raw
= 1;
1595 ldata
->real_raw
= 0;
1597 n_tty_set_room(tty
);
1599 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1600 * been stopped by STOP_CHAR(tty) before it.
1602 if (!I_IXON(tty
) && old
&& (old
->c_iflag
& IXON
) && !tty
->flow_stopped
) {
1606 /* The termios change make the tty ready for I/O */
1607 wake_up_interruptible(&tty
->write_wait
);
1608 wake_up_interruptible(&tty
->read_wait
);
1612 * n_tty_close - close the ldisc for this tty
1615 * Called from the terminal layer when this line discipline is
1616 * being shut down, either because of a close or becsuse of a
1617 * discipline change. The function will not be called while other
1618 * ldisc methods are in progress.
1621 static void n_tty_close(struct tty_struct
*tty
)
1623 struct n_tty_data
*ldata
= tty
->disc_data
;
1626 n_tty_packet_mode_flush(tty
);
1628 kfree(ldata
->read_buf
);
1629 kfree(ldata
->echo_buf
);
1631 tty
->disc_data
= NULL
;
1635 * n_tty_open - open an ldisc
1636 * @tty: terminal to open
1638 * Called when this line discipline is being attached to the
1639 * terminal device. Can sleep. Called serialized so that no
1640 * other events will occur in parallel. No further open will occur
1644 static int n_tty_open(struct tty_struct
*tty
)
1646 struct n_tty_data
*ldata
;
1648 ldata
= kzalloc(sizeof(*ldata
), GFP_KERNEL
);
1652 ldata
->overrun_time
= jiffies
;
1653 mutex_init(&ldata
->atomic_read_lock
);
1654 mutex_init(&ldata
->output_lock
);
1655 mutex_init(&ldata
->echo_lock
);
1656 raw_spin_lock_init(&ldata
->read_lock
);
1658 /* These are ugly. Currently a malloc failure here can panic */
1659 ldata
->read_buf
= kzalloc(N_TTY_BUF_SIZE
, GFP_KERNEL
);
1660 ldata
->echo_buf
= kzalloc(N_TTY_BUF_SIZE
, GFP_KERNEL
);
1661 if (!ldata
->read_buf
|| !ldata
->echo_buf
)
1664 tty
->disc_data
= ldata
;
1665 reset_buffer_flags(tty
->disc_data
);
1667 ldata
->minimum_to_wake
= 1;
1669 /* indicate buffer work may resume */
1670 clear_bit(TTY_LDISC_HALTED
, &tty
->flags
);
1671 n_tty_set_termios(tty
, NULL
);
1672 tty_unthrottle(tty
);
1676 kfree(ldata
->read_buf
);
1677 kfree(ldata
->echo_buf
);
1683 static inline int input_available_p(struct tty_struct
*tty
, int amt
)
1685 struct n_tty_data
*ldata
= tty
->disc_data
;
1687 tty_flush_to_ldisc(tty
);
1688 if (ldata
->icanon
&& !L_EXTPROC(tty
)) {
1689 if (ldata
->canon_data
)
1691 } else if (ldata
->read_cnt
>= (amt
? amt
: 1))
1698 * copy_from_read_buf - copy read data directly
1699 * @tty: terminal device
1703 * Helper function to speed up n_tty_read. It is only called when
1704 * ICANON is off; it copies characters straight from the tty queue to
1705 * user space directly. It can be profitably called twice; once to
1706 * drain the space from the tail pointer to the (physical) end of the
1707 * buffer, and once to drain the space from the (physical) beginning of
1708 * the buffer to head pointer.
1710 * Called under the ldata->atomic_read_lock sem
1714 static int copy_from_read_buf(struct tty_struct
*tty
,
1715 unsigned char __user
**b
,
1719 struct n_tty_data
*ldata
= tty
->disc_data
;
1722 unsigned long flags
;
1726 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
1727 n
= min(ldata
->read_cnt
, N_TTY_BUF_SIZE
- ldata
->read_tail
);
1729 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
1731 retval
= copy_to_user(*b
, &ldata
->read_buf
[ldata
->read_tail
], n
);
1734 ldata
->read_buf
[ldata
->read_tail
] == EOF_CHAR(tty
);
1735 tty_audit_add_data(tty
, &ldata
->read_buf
[ldata
->read_tail
], n
,
1737 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
1738 ldata
->read_tail
= (ldata
->read_tail
+ n
) & (N_TTY_BUF_SIZE
-1);
1739 ldata
->read_cnt
-= n
;
1740 /* Turn single EOF into zero-length read */
1741 if (L_EXTPROC(tty
) && ldata
->icanon
&& is_eof
&& !ldata
->read_cnt
)
1743 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
1751 * canon_copy_to_user - copy read data in canonical mode
1752 * @tty: terminal device
1756 * Helper function for n_tty_read. It is only called when ICANON is on;
1757 * it copies characters one at a time from the read buffer to the user
1760 * Called under the atomic_read_lock mutex
1763 static int canon_copy_to_user(struct tty_struct
*tty
,
1764 unsigned char __user
**b
,
1767 struct n_tty_data
*ldata
= tty
->disc_data
;
1768 unsigned long flags
;
1771 /* N.B. avoid overrun if nr == 0 */
1772 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
1773 while (*nr
&& ldata
->read_cnt
) {
1775 eol
= test_and_clear_bit(ldata
->read_tail
, ldata
->read_flags
);
1776 c
= ldata
->read_buf
[ldata
->read_tail
];
1777 ldata
->read_tail
= (ldata
->read_tail
+1) & (N_TTY_BUF_SIZE
-1);
1780 /* this test should be redundant:
1781 * we shouldn't be reading data if
1784 if (--ldata
->canon_data
< 0)
1785 ldata
->canon_data
= 0;
1787 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
1789 if (!eol
|| (c
!= __DISABLED_CHAR
)) {
1790 if (tty_put_user(tty
, c
, *b
))
1796 tty_audit_push(tty
);
1799 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
1801 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
1806 extern ssize_t
redirected_tty_write(struct file
*, const char __user
*,
1810 * job_control - check job control
1812 * @file: file handle
1814 * Perform job control management checks on this file/tty descriptor
1815 * and if appropriate send any needed signals and return a negative
1816 * error code if action should be taken.
1818 * Locking: redirected write test is safe
1819 * current->signal->tty check is safe
1820 * ctrl_lock to safely reference tty->pgrp
1823 static int job_control(struct tty_struct
*tty
, struct file
*file
)
1825 /* Job control check -- must be done at start and after
1826 every sleep (POSIX.1 7.1.1.4). */
1827 /* NOTE: not yet done after every sleep pending a thorough
1828 check of the logic of this change. -- jlc */
1829 /* don't stop on /dev/console */
1830 if (file
->f_op
->write
== redirected_tty_write
||
1831 current
->signal
->tty
!= tty
)
1834 spin_lock_irq(&tty
->ctrl_lock
);
1836 printk(KERN_ERR
"n_tty_read: no tty->pgrp!\n");
1837 else if (task_pgrp(current
) != tty
->pgrp
) {
1838 spin_unlock_irq(&tty
->ctrl_lock
);
1839 if (is_ignored(SIGTTIN
) || is_current_pgrp_orphaned())
1841 kill_pgrp(task_pgrp(current
), SIGTTIN
, 1);
1842 set_thread_flag(TIF_SIGPENDING
);
1843 return -ERESTARTSYS
;
1845 spin_unlock_irq(&tty
->ctrl_lock
);
1851 * n_tty_read - read function for tty
1853 * @file: file object
1854 * @buf: userspace buffer pointer
1857 * Perform reads for the line discipline. We are guaranteed that the
1858 * line discipline will not be closed under us but we may get multiple
1859 * parallel readers and must handle this ourselves. We may also get
1860 * a hangup. Always called in user context, may sleep.
1862 * This code must be sure never to sleep through a hangup.
1865 static ssize_t
n_tty_read(struct tty_struct
*tty
, struct file
*file
,
1866 unsigned char __user
*buf
, size_t nr
)
1868 struct n_tty_data
*ldata
= tty
->disc_data
;
1869 unsigned char __user
*b
= buf
;
1870 DECLARE_WAITQUEUE(wait
, current
);
1876 unsigned long flags
;
1880 c
= job_control(tty
, file
);
1885 timeout
= MAX_SCHEDULE_TIMEOUT
;
1886 if (!ldata
->icanon
) {
1887 minimum
= MIN_CHAR(tty
);
1889 time
= (HZ
/ 10) * TIME_CHAR(tty
);
1891 ldata
->minimum_to_wake
= 1;
1892 else if (!waitqueue_active(&tty
->read_wait
) ||
1893 (ldata
->minimum_to_wake
> minimum
))
1894 ldata
->minimum_to_wake
= minimum
;
1896 timeout
= (HZ
/ 10) * TIME_CHAR(tty
);
1897 ldata
->minimum_to_wake
= minimum
= 1;
1902 * Internal serialization of reads.
1904 if (file
->f_flags
& O_NONBLOCK
) {
1905 if (!mutex_trylock(&ldata
->atomic_read_lock
))
1908 if (mutex_lock_interruptible(&ldata
->atomic_read_lock
))
1909 return -ERESTARTSYS
;
1911 packet
= tty
->packet
;
1913 add_wait_queue(&tty
->read_wait
, &wait
);
1915 /* First test for status change. */
1916 if (packet
&& tty
->link
->ctrl_status
) {
1920 spin_lock_irqsave(&tty
->link
->ctrl_lock
, flags
);
1921 cs
= tty
->link
->ctrl_status
;
1922 tty
->link
->ctrl_status
= 0;
1923 spin_unlock_irqrestore(&tty
->link
->ctrl_lock
, flags
);
1924 if (tty_put_user(tty
, cs
, b
++)) {
1932 /* This statement must be first before checking for input
1933 so that any interrupt will set the state back to
1935 set_current_state(TASK_INTERRUPTIBLE
);
1937 if (((minimum
- (b
- buf
)) < ldata
->minimum_to_wake
) &&
1938 ((minimum
- (b
- buf
)) >= 1))
1939 ldata
->minimum_to_wake
= (minimum
- (b
- buf
));
1941 if (!input_available_p(tty
, 0)) {
1942 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
)) {
1946 if (tty_hung_up_p(file
))
1950 if (file
->f_flags
& O_NONBLOCK
) {
1954 if (signal_pending(current
)) {
1955 retval
= -ERESTARTSYS
;
1958 n_tty_set_room(tty
);
1959 timeout
= schedule_timeout(timeout
);
1962 __set_current_state(TASK_RUNNING
);
1964 /* Deal with packet mode. */
1965 if (packet
&& b
== buf
) {
1966 if (tty_put_user(tty
, TIOCPKT_DATA
, b
++)) {
1974 if (ldata
->icanon
&& !L_EXTPROC(tty
)) {
1975 retval
= canon_copy_to_user(tty
, &b
, &nr
);
1980 /* The copy function takes the read lock and handles
1981 locking internally for this case */
1982 uncopied
= copy_from_read_buf(tty
, &b
, &nr
);
1983 uncopied
+= copy_from_read_buf(tty
, &b
, &nr
);
1990 /* If there is enough space in the read buffer now, let the
1991 * low-level driver know. We use n_tty_chars_in_buffer() to
1992 * check the buffer, as it now knows about canonical mode.
1993 * Otherwise, if the driver is throttled and the line is
1994 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1995 * we won't get any more characters.
1998 tty_set_flow_change(tty
, TTY_UNTHROTTLE_SAFE
);
1999 if (n_tty_chars_in_buffer(tty
) > TTY_THRESHOLD_UNTHROTTLE
)
2003 n_tty_set_room(tty
);
2004 if (!tty_unthrottle_safe(tty
))
2007 __tty_set_flow_change(tty
, 0);
2009 if (b
- buf
>= minimum
)
2014 mutex_unlock(&ldata
->atomic_read_lock
);
2015 remove_wait_queue(&tty
->read_wait
, &wait
);
2017 if (!waitqueue_active(&tty
->read_wait
))
2018 ldata
->minimum_to_wake
= minimum
;
2020 __set_current_state(TASK_RUNNING
);
2025 clear_bit(TTY_PUSH
, &tty
->flags
);
2026 } else if (test_and_clear_bit(TTY_PUSH
, &tty
->flags
))
2029 n_tty_set_room(tty
);
2034 * n_tty_write - write function for tty
2036 * @file: file object
2037 * @buf: userspace buffer pointer
2040 * Write function of the terminal device. This is serialized with
2041 * respect to other write callers but not to termios changes, reads
2042 * and other such events. Since the receive code will echo characters,
2043 * thus calling driver write methods, the output_lock is used in
2044 * the output processing functions called here as well as in the
2045 * echo processing function to protect the column state and space
2046 * left in the buffer.
2048 * This code must be sure never to sleep through a hangup.
2050 * Locking: output_lock to protect column state and space left
2051 * (note that the process_output*() functions take this
2055 static ssize_t
n_tty_write(struct tty_struct
*tty
, struct file
*file
,
2056 const unsigned char *buf
, size_t nr
)
2058 const unsigned char *b
= buf
;
2059 DECLARE_WAITQUEUE(wait
, current
);
2063 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2064 if (L_TOSTOP(tty
) && file
->f_op
->write
!= redirected_tty_write
) {
2065 retval
= tty_check_change(tty
);
2070 /* Write out any echoed characters that are still pending */
2071 process_echoes(tty
);
2073 add_wait_queue(&tty
->write_wait
, &wait
);
2075 set_current_state(TASK_INTERRUPTIBLE
);
2076 if (signal_pending(current
)) {
2077 retval
= -ERESTARTSYS
;
2080 if (tty_hung_up_p(file
) || (tty
->link
&& !tty
->link
->count
)) {
2086 ssize_t num
= process_output_block(tty
, b
, nr
);
2098 if (process_output(c
, tty
) < 0)
2102 if (tty
->ops
->flush_chars
)
2103 tty
->ops
->flush_chars(tty
);
2106 c
= tty
->ops
->write(tty
, b
, nr
);
2119 if (file
->f_flags
& O_NONBLOCK
) {
2126 __set_current_state(TASK_RUNNING
);
2127 remove_wait_queue(&tty
->write_wait
, &wait
);
2128 if (b
- buf
!= nr
&& tty
->fasync
)
2129 set_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
2130 return (b
- buf
) ? b
- buf
: retval
;
2134 * n_tty_poll - poll method for N_TTY
2135 * @tty: terminal device
2136 * @file: file accessing it
2139 * Called when the line discipline is asked to poll() for data or
2140 * for special events. This code is not serialized with respect to
2141 * other events save open/close.
2143 * This code must be sure never to sleep through a hangup.
2144 * Called without the kernel lock held - fine
2147 static unsigned int n_tty_poll(struct tty_struct
*tty
, struct file
*file
,
2150 struct n_tty_data
*ldata
= tty
->disc_data
;
2151 unsigned int mask
= 0;
2153 poll_wait(file
, &tty
->read_wait
, wait
);
2154 poll_wait(file
, &tty
->write_wait
, wait
);
2155 if (input_available_p(tty
, TIME_CHAR(tty
) ? 0 : MIN_CHAR(tty
)))
2156 mask
|= POLLIN
| POLLRDNORM
;
2157 if (tty
->packet
&& tty
->link
->ctrl_status
)
2158 mask
|= POLLPRI
| POLLIN
| POLLRDNORM
;
2159 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
))
2161 if (tty_hung_up_p(file
))
2163 if (!(mask
& (POLLHUP
| POLLIN
| POLLRDNORM
))) {
2164 if (MIN_CHAR(tty
) && !TIME_CHAR(tty
))
2165 ldata
->minimum_to_wake
= MIN_CHAR(tty
);
2167 ldata
->minimum_to_wake
= 1;
2169 if (tty
->ops
->write
&& !tty_is_writelocked(tty
) &&
2170 tty_chars_in_buffer(tty
) < WAKEUP_CHARS
&&
2171 tty_write_room(tty
) > 0)
2172 mask
|= POLLOUT
| POLLWRNORM
;
2176 static unsigned long inq_canon(struct n_tty_data
*ldata
)
2180 if (!ldata
->canon_data
)
2182 head
= ldata
->canon_head
;
2183 tail
= ldata
->read_tail
;
2184 nr
= (head
- tail
) & (N_TTY_BUF_SIZE
-1);
2185 /* Skip EOF-chars.. */
2186 while (head
!= tail
) {
2187 if (test_bit(tail
, ldata
->read_flags
) &&
2188 ldata
->read_buf
[tail
] == __DISABLED_CHAR
)
2190 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
2195 static int n_tty_ioctl(struct tty_struct
*tty
, struct file
*file
,
2196 unsigned int cmd
, unsigned long arg
)
2198 struct n_tty_data
*ldata
= tty
->disc_data
;
2203 return put_user(tty_chars_in_buffer(tty
), (int __user
*) arg
);
2205 /* FIXME: Locking */
2206 retval
= ldata
->read_cnt
;
2208 retval
= inq_canon(ldata
);
2209 return put_user(retval
, (unsigned int __user
*) arg
);
2211 return n_tty_ioctl_helper(tty
, file
, cmd
, arg
);
2215 static void n_tty_fasync(struct tty_struct
*tty
, int on
)
2217 struct n_tty_data
*ldata
= tty
->disc_data
;
2219 if (!waitqueue_active(&tty
->read_wait
)) {
2221 ldata
->minimum_to_wake
= 1;
2222 else if (!tty
->fasync
)
2223 ldata
->minimum_to_wake
= N_TTY_BUF_SIZE
;
2227 struct tty_ldisc_ops tty_ldisc_N_TTY
= {
2228 .magic
= TTY_LDISC_MAGIC
,
2231 .close
= n_tty_close
,
2232 .flush_buffer
= n_tty_flush_buffer
,
2233 .chars_in_buffer
= n_tty_chars_in_buffer
,
2235 .write
= n_tty_write
,
2236 .ioctl
= n_tty_ioctl
,
2237 .set_termios
= n_tty_set_termios
,
2239 .receive_buf
= n_tty_receive_buf
,
2240 .write_wakeup
= n_tty_write_wakeup
,
2241 .fasync
= n_tty_fasync
,
2242 .receive_buf2
= n_tty_receive_buf2
,
2246 * n_tty_inherit_ops - inherit N_TTY methods
2247 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2249 * Enables a 'subclass' line discipline to 'inherit' N_TTY
2253 void n_tty_inherit_ops(struct tty_ldisc_ops
*ops
)
2255 *ops
= tty_ldisc_N_TTY
;
2257 ops
->refcount
= ops
->flags
= 0;
2259 EXPORT_SYMBOL_GPL(n_tty_inherit_ops
);