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
;
82 unsigned char lnext
:1, erasing
:1, raw
:1, real_raw
:1, icanon
:1;
83 unsigned char echo_overrun
:1;
85 DECLARE_BITMAP(process_char_map
, 256);
86 DECLARE_BITMAP(read_flags
, N_TTY_BUF_SIZE
);
93 unsigned char *echo_buf
;
94 unsigned int echo_pos
;
95 unsigned int echo_cnt
;
98 unsigned long canon_head
;
99 unsigned int canon_column
;
101 struct mutex atomic_read_lock
;
102 struct mutex output_lock
;
103 struct mutex echo_lock
;
104 raw_spinlock_t read_lock
;
107 static inline int tty_put_user(struct tty_struct
*tty
, unsigned char x
,
108 unsigned char __user
*ptr
)
110 struct n_tty_data
*ldata
= tty
->disc_data
;
112 tty_audit_add_data(tty
, &x
, 1, ldata
->icanon
);
113 return put_user(x
, ptr
);
117 * n_tty_set__room - receive space
120 * Called by the driver to find out how much data it is
121 * permitted to feed to the line discipline without any being lost
122 * and thus to manage flow control. Not serialized. Answers for the
126 static void n_tty_set_room(struct tty_struct
*tty
)
128 struct n_tty_data
*ldata
= tty
->disc_data
;
132 /* ldata->read_cnt is not read locked ? */
134 /* Multiply read_cnt by 3, since each byte might take up to
135 * three times as many spaces when PARMRK is set (depending on
136 * its flags, e.g. parity error). */
137 left
= N_TTY_BUF_SIZE
- ldata
->read_cnt
* 3 - 1;
139 left
= N_TTY_BUF_SIZE
- ldata
->read_cnt
- 1;
142 * If we are doing input canonicalization, and there are no
143 * pending newlines, let characters through without limit, so
144 * that erase characters will be handled. Other excess
145 * characters will be beeped.
148 left
= ldata
->icanon
&& !ldata
->canon_data
;
149 old_left
= tty
->receive_room
;
150 tty
->receive_room
= left
;
152 /* Did this open up the receive buffer? We may need to flip */
153 if (left
&& !old_left
) {
154 WARN_RATELIMIT(tty
->port
->itty
== NULL
,
155 "scheduling with invalid itty\n");
156 schedule_work(&tty
->port
->buf
.work
);
160 static void put_tty_queue_nolock(unsigned char c
, struct n_tty_data
*ldata
)
162 if (ldata
->read_cnt
< N_TTY_BUF_SIZE
) {
163 ldata
->read_buf
[ldata
->read_head
] = c
;
164 ldata
->read_head
= (ldata
->read_head
+ 1) & (N_TTY_BUF_SIZE
-1);
170 * put_tty_queue - add character to tty
174 * Add a character to the tty read_buf queue. This is done under the
175 * read_lock to serialize character addition and also to protect us
176 * against parallel reads or flushes
179 static void put_tty_queue(unsigned char c
, struct n_tty_data
*ldata
)
183 * The problem of stomping on the buffers ends here.
184 * Why didn't anyone see this one coming? --AJK
186 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
187 put_tty_queue_nolock(c
, ldata
);
188 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
192 * reset_buffer_flags - reset buffer state
193 * @tty: terminal to reset
195 * Reset the read buffer counters, clear the flags,
196 * and make sure the driver is unthrottled. Called
197 * from n_tty_open() and n_tty_flush_buffer().
199 * Locking: tty_read_lock for read fields.
202 static void reset_buffer_flags(struct tty_struct
*tty
)
204 struct n_tty_data
*ldata
= tty
->disc_data
;
207 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
208 ldata
->read_head
= ldata
->read_tail
= ldata
->read_cnt
= 0;
209 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
211 mutex_lock(&ldata
->echo_lock
);
212 ldata
->echo_pos
= ldata
->echo_cnt
= ldata
->echo_overrun
= 0;
213 mutex_unlock(&ldata
->echo_lock
);
215 ldata
->canon_head
= ldata
->canon_data
= ldata
->erasing
= 0;
216 bitmap_zero(ldata
->read_flags
, N_TTY_BUF_SIZE
);
221 * n_tty_flush_buffer - clean input queue
222 * @tty: terminal device
224 * Flush the input buffer. Called when the line discipline is
225 * being closed, when the tty layer wants the buffer flushed (eg
226 * at hangup) or when the N_TTY line discipline internally has to
227 * clean the pending queue (for example some signals).
229 * Locking: ctrl_lock, read_lock.
232 static void n_tty_flush_buffer(struct tty_struct
*tty
)
235 /* clear everything and unthrottle the driver */
236 reset_buffer_flags(tty
);
241 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
242 if (tty
->link
->packet
) {
243 tty
->ctrl_status
|= TIOCPKT_FLUSHREAD
;
244 wake_up_interruptible(&tty
->link
->read_wait
);
246 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
250 * n_tty_chars_in_buffer - report available bytes
253 * Report the number of characters buffered to be delivered to user
254 * at this instant in time.
259 static ssize_t
n_tty_chars_in_buffer(struct tty_struct
*tty
)
261 struct n_tty_data
*ldata
= tty
->disc_data
;
265 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
266 if (!ldata
->icanon
) {
268 } else if (ldata
->canon_data
) {
269 n
= (ldata
->canon_head
> ldata
->read_tail
) ?
270 ldata
->canon_head
- ldata
->read_tail
:
271 ldata
->canon_head
+ (N_TTY_BUF_SIZE
- ldata
->read_tail
);
273 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
278 * is_utf8_continuation - utf8 multibyte check
281 * Returns true if the utf8 character 'c' is a multibyte continuation
282 * character. We use this to correctly compute the on screen size
283 * of the character when printing
286 static inline int is_utf8_continuation(unsigned char c
)
288 return (c
& 0xc0) == 0x80;
292 * is_continuation - multibyte check
295 * Returns true if the utf8 character 'c' is a multibyte continuation
296 * character and the terminal is in unicode mode.
299 static inline int is_continuation(unsigned char c
, struct tty_struct
*tty
)
301 return I_IUTF8(tty
) && is_utf8_continuation(c
);
305 * do_output_char - output one character
306 * @c: character (or partial unicode symbol)
307 * @tty: terminal device
308 * @space: space available in tty driver write buffer
310 * This is a helper function that handles one output character
311 * (including special characters like TAB, CR, LF, etc.),
312 * doing OPOST processing and putting the results in the
313 * tty driver's write buffer.
315 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
316 * and NLDLY. They simply aren't relevant in the world today.
317 * If you ever need them, add them here.
319 * Returns the number of bytes of buffer space used or -1 if
322 * Locking: should be called under the output_lock to protect
323 * the column state and space left in the buffer
326 static int do_output_char(unsigned char c
, struct tty_struct
*tty
, int space
)
328 struct n_tty_data
*ldata
= tty
->disc_data
;
341 ldata
->canon_column
= ldata
->column
= 0;
342 tty
->ops
->write(tty
, "\r\n", 2);
345 ldata
->canon_column
= ldata
->column
;
348 if (O_ONOCR(tty
) && ldata
->column
== 0)
353 ldata
->canon_column
= ldata
->column
= 0;
356 ldata
->canon_column
= ldata
->column
= 0;
359 spaces
= 8 - (ldata
->column
& 7);
360 if (O_TABDLY(tty
) == XTABS
) {
363 ldata
->column
+= spaces
;
364 tty
->ops
->write(tty
, " ", spaces
);
367 ldata
->column
+= spaces
;
370 if (ldata
->column
> 0)
377 if (!is_continuation(c
, tty
))
383 tty_put_char(tty
, c
);
388 * process_output - output post processor
389 * @c: character (or partial unicode symbol)
390 * @tty: terminal device
392 * Output one character with OPOST processing.
393 * Returns -1 when the output device is full and the character
396 * Locking: output_lock to protect column state and space left
397 * (also, this is called from n_tty_write under the
398 * tty layer write lock)
401 static int process_output(unsigned char c
, struct tty_struct
*tty
)
403 struct n_tty_data
*ldata
= tty
->disc_data
;
406 mutex_lock(&ldata
->output_lock
);
408 space
= tty_write_room(tty
);
409 retval
= do_output_char(c
, tty
, space
);
411 mutex_unlock(&ldata
->output_lock
);
419 * process_output_block - block post processor
420 * @tty: terminal device
421 * @buf: character buffer
422 * @nr: number of bytes to output
424 * Output a block of characters with OPOST processing.
425 * Returns the number of characters output.
427 * This path is used to speed up block console writes, among other
428 * things when processing blocks of output data. It handles only
429 * the simple cases normally found and helps to generate blocks of
430 * symbols for the console driver and thus improve performance.
432 * Locking: output_lock to protect column state and space left
433 * (also, this is called from n_tty_write under the
434 * tty layer write lock)
437 static ssize_t
process_output_block(struct tty_struct
*tty
,
438 const unsigned char *buf
, unsigned int nr
)
440 struct n_tty_data
*ldata
= tty
->disc_data
;
443 const unsigned char *cp
;
445 mutex_lock(&ldata
->output_lock
);
447 space
= tty_write_room(tty
);
449 mutex_unlock(&ldata
->output_lock
);
455 for (i
= 0, cp
= buf
; i
< nr
; i
++, cp
++) {
456 unsigned char c
= *cp
;
464 ldata
->canon_column
= ldata
->column
;
467 if (O_ONOCR(tty
) && ldata
->column
== 0)
471 ldata
->canon_column
= ldata
->column
= 0;
476 if (ldata
->column
> 0)
483 if (!is_continuation(c
, tty
))
490 i
= tty
->ops
->write(tty
, buf
, i
);
492 mutex_unlock(&ldata
->output_lock
);
497 * process_echoes - write pending echo characters
498 * @tty: terminal device
500 * Write previously buffered echo (and other ldisc-generated)
501 * characters to the tty.
503 * Characters generated by the ldisc (including echoes) need to
504 * be buffered because the driver's write buffer can fill during
505 * heavy program output. Echoing straight to the driver will
506 * often fail under these conditions, causing lost characters and
507 * resulting mismatches of ldisc state information.
509 * Since the ldisc state must represent the characters actually sent
510 * to the driver at the time of the write, operations like certain
511 * changes in column state are also saved in the buffer and executed
514 * A circular fifo buffer is used so that the most recent characters
515 * are prioritized. Also, when control characters are echoed with a
516 * prefixed "^", the pair is treated atomically and thus not separated.
518 * Locking: output_lock to protect column state and space left,
519 * echo_lock to protect the echo buffer
522 static void process_echoes(struct tty_struct
*tty
)
524 struct n_tty_data
*ldata
= tty
->disc_data
;
527 unsigned char *cp
, *buf_end
;
529 if (!ldata
->echo_cnt
)
532 mutex_lock(&ldata
->output_lock
);
533 mutex_lock(&ldata
->echo_lock
);
535 space
= tty_write_room(tty
);
537 buf_end
= ldata
->echo_buf
+ N_TTY_BUF_SIZE
;
538 cp
= ldata
->echo_buf
+ ldata
->echo_pos
;
539 nr
= ldata
->echo_cnt
;
542 if (c
== ECHO_OP_START
) {
545 int no_space_left
= 0;
548 * If the buffer byte is the start of a multi-byte
549 * operation, get the next byte, which is either the
550 * op code or a control character value.
554 opp
-= N_TTY_BUF_SIZE
;
558 unsigned int num_chars
, num_bs
;
560 case ECHO_OP_ERASE_TAB
:
561 if (++opp
== buf_end
)
562 opp
-= N_TTY_BUF_SIZE
;
566 * Determine how many columns to go back
567 * in order to erase the tab.
568 * This depends on the number of columns
569 * used by other characters within the tab
570 * area. If this (modulo 8) count is from
571 * the start of input rather than from a
572 * previous tab, we offset by canon column.
573 * Otherwise, tab spacing is normal.
575 if (!(num_chars
& 0x80))
576 num_chars
+= ldata
->canon_column
;
577 num_bs
= 8 - (num_chars
& 7);
579 if (num_bs
> space
) {
585 tty_put_char(tty
, '\b');
586 if (ldata
->column
> 0)
593 case ECHO_OP_SET_CANON_COL
:
594 ldata
->canon_column
= ldata
->column
;
599 case ECHO_OP_MOVE_BACK_COL
:
600 if (ldata
->column
> 0)
607 /* This is an escaped echo op start code */
612 tty_put_char(tty
, ECHO_OP_START
);
621 * If the op is not a special byte code,
622 * it is a ctrl char tagged to be echoed
623 * as "^X" (where X is the letter
624 * representing the control char).
625 * Note that we must ensure there is
626 * enough space for the whole ctrl pair.
633 tty_put_char(tty
, '^');
634 tty_put_char(tty
, op
^ 0100);
645 !(test_bit(TTY_HW_COOK_OUT
, &tty
->flags
))) {
646 int retval
= do_output_char(c
, tty
, space
);
653 tty_put_char(tty
, c
);
660 /* When end of circular buffer reached, wrap around */
662 cp
-= N_TTY_BUF_SIZE
;
668 ldata
->echo_overrun
= 0;
670 int num_processed
= ldata
->echo_cnt
- nr
;
671 ldata
->echo_pos
+= num_processed
;
672 ldata
->echo_pos
&= N_TTY_BUF_SIZE
- 1;
673 ldata
->echo_cnt
= nr
;
674 if (num_processed
> 0)
675 ldata
->echo_overrun
= 0;
678 mutex_unlock(&ldata
->echo_lock
);
679 mutex_unlock(&ldata
->output_lock
);
681 if (tty
->ops
->flush_chars
)
682 tty
->ops
->flush_chars(tty
);
686 * add_echo_byte - add a byte to the echo buffer
687 * @c: unicode byte to echo
690 * Add a character or operation byte to the echo buffer.
692 * Should be called under the echo lock to protect the echo buffer.
695 static void add_echo_byte(unsigned char c
, struct n_tty_data
*ldata
)
699 if (ldata
->echo_cnt
== N_TTY_BUF_SIZE
) {
700 /* Circular buffer is already at capacity */
701 new_byte_pos
= ldata
->echo_pos
;
704 * Since the buffer start position needs to be advanced,
705 * be sure to step by a whole operation byte group.
707 if (ldata
->echo_buf
[ldata
->echo_pos
] == ECHO_OP_START
) {
708 if (ldata
->echo_buf
[(ldata
->echo_pos
+ 1) &
709 (N_TTY_BUF_SIZE
- 1)] ==
711 ldata
->echo_pos
+= 3;
712 ldata
->echo_cnt
-= 2;
714 ldata
->echo_pos
+= 2;
715 ldata
->echo_cnt
-= 1;
720 ldata
->echo_pos
&= N_TTY_BUF_SIZE
- 1;
722 ldata
->echo_overrun
= 1;
724 new_byte_pos
= ldata
->echo_pos
+ ldata
->echo_cnt
;
725 new_byte_pos
&= N_TTY_BUF_SIZE
- 1;
729 ldata
->echo_buf
[new_byte_pos
] = c
;
733 * echo_move_back_col - add operation to move back a column
736 * Add an operation to the echo buffer to move back one column.
738 * Locking: echo_lock to protect the echo buffer
741 static void echo_move_back_col(struct n_tty_data
*ldata
)
743 mutex_lock(&ldata
->echo_lock
);
744 add_echo_byte(ECHO_OP_START
, ldata
);
745 add_echo_byte(ECHO_OP_MOVE_BACK_COL
, ldata
);
746 mutex_unlock(&ldata
->echo_lock
);
750 * echo_set_canon_col - add operation to set the canon column
753 * Add an operation to the echo buffer to set the canon column
754 * to the current column.
756 * Locking: echo_lock to protect the echo buffer
759 static void echo_set_canon_col(struct n_tty_data
*ldata
)
761 mutex_lock(&ldata
->echo_lock
);
762 add_echo_byte(ECHO_OP_START
, ldata
);
763 add_echo_byte(ECHO_OP_SET_CANON_COL
, ldata
);
764 mutex_unlock(&ldata
->echo_lock
);
768 * echo_erase_tab - add operation to erase a tab
769 * @num_chars: number of character columns already used
770 * @after_tab: true if num_chars starts after a previous tab
773 * Add an operation to the echo buffer to erase a tab.
775 * Called by the eraser function, which knows how many character
776 * columns have been used since either a previous tab or the start
777 * of input. This information will be used later, along with
778 * canon column (if applicable), to go back the correct number
781 * Locking: echo_lock to protect the echo buffer
784 static void echo_erase_tab(unsigned int num_chars
, int after_tab
,
785 struct n_tty_data
*ldata
)
787 mutex_lock(&ldata
->echo_lock
);
789 add_echo_byte(ECHO_OP_START
, ldata
);
790 add_echo_byte(ECHO_OP_ERASE_TAB
, ldata
);
792 /* We only need to know this modulo 8 (tab spacing) */
795 /* Set the high bit as a flag if num_chars is after a previous tab */
799 add_echo_byte(num_chars
, ldata
);
801 mutex_unlock(&ldata
->echo_lock
);
805 * echo_char_raw - echo a character raw
806 * @c: unicode byte to echo
807 * @tty: terminal device
809 * Echo user input back onto the screen. This must be called only when
810 * L_ECHO(tty) is true. Called from the driver receive_buf path.
812 * This variant does not treat control characters specially.
814 * Locking: echo_lock to protect the echo buffer
817 static void echo_char_raw(unsigned char c
, struct n_tty_data
*ldata
)
819 mutex_lock(&ldata
->echo_lock
);
820 if (c
== ECHO_OP_START
) {
821 add_echo_byte(ECHO_OP_START
, ldata
);
822 add_echo_byte(ECHO_OP_START
, ldata
);
824 add_echo_byte(c
, ldata
);
826 mutex_unlock(&ldata
->echo_lock
);
830 * echo_char - echo a character
831 * @c: unicode byte to echo
832 * @tty: terminal device
834 * Echo user input back onto the screen. This must be called only when
835 * L_ECHO(tty) is true. Called from the driver receive_buf path.
837 * This variant tags control characters to be echoed as "^X"
838 * (where X is the letter representing the control char).
840 * Locking: echo_lock to protect the echo buffer
843 static void echo_char(unsigned char c
, struct tty_struct
*tty
)
845 struct n_tty_data
*ldata
= tty
->disc_data
;
847 mutex_lock(&ldata
->echo_lock
);
849 if (c
== ECHO_OP_START
) {
850 add_echo_byte(ECHO_OP_START
, ldata
);
851 add_echo_byte(ECHO_OP_START
, ldata
);
853 if (L_ECHOCTL(tty
) && iscntrl(c
) && c
!= '\t')
854 add_echo_byte(ECHO_OP_START
, ldata
);
855 add_echo_byte(c
, ldata
);
858 mutex_unlock(&ldata
->echo_lock
);
862 * finish_erasing - complete erase
866 static inline void finish_erasing(struct n_tty_data
*ldata
)
868 if (ldata
->erasing
) {
869 echo_char_raw('/', ldata
);
875 * eraser - handle erase function
876 * @c: character input
877 * @tty: terminal device
879 * Perform erase and necessary output when an erase character is
880 * present in the stream from the driver layer. Handles the complexities
881 * of UTF-8 multibyte symbols.
883 * Locking: read_lock for tty buffers
886 static void eraser(unsigned char c
, struct tty_struct
*tty
)
888 struct n_tty_data
*ldata
= tty
->disc_data
;
889 enum { ERASE
, WERASE
, KILL
} kill_type
;
890 int head
, seen_alnums
, cnt
;
893 /* FIXME: locking needed ? */
894 if (ldata
->read_head
== ldata
->canon_head
) {
895 /* process_output('\a', tty); */ /* what do you think? */
898 if (c
== ERASE_CHAR(tty
))
900 else if (c
== WERASE_CHAR(tty
))
904 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
905 ldata
->read_cnt
-= ((ldata
->read_head
- ldata
->canon_head
) &
906 (N_TTY_BUF_SIZE
- 1));
907 ldata
->read_head
= ldata
->canon_head
;
908 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
911 if (!L_ECHOK(tty
) || !L_ECHOKE(tty
) || !L_ECHOE(tty
)) {
912 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
913 ldata
->read_cnt
-= ((ldata
->read_head
- ldata
->canon_head
) &
914 (N_TTY_BUF_SIZE
- 1));
915 ldata
->read_head
= ldata
->canon_head
;
916 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
917 finish_erasing(ldata
);
918 echo_char(KILL_CHAR(tty
), tty
);
919 /* Add a newline if ECHOK is on and ECHOKE is off. */
921 echo_char_raw('\n', ldata
);
928 /* FIXME: Locking ?? */
929 while (ldata
->read_head
!= ldata
->canon_head
) {
930 head
= ldata
->read_head
;
932 /* erase a single possibly multibyte character */
934 head
= (head
- 1) & (N_TTY_BUF_SIZE
-1);
935 c
= ldata
->read_buf
[head
];
936 } while (is_continuation(c
, tty
) && head
!= ldata
->canon_head
);
938 /* do not partially erase */
939 if (is_continuation(c
, tty
))
942 if (kill_type
== WERASE
) {
943 /* Equivalent to BSD's ALTWERASE. */
944 if (isalnum(c
) || c
== '_')
946 else if (seen_alnums
)
949 cnt
= (ldata
->read_head
- head
) & (N_TTY_BUF_SIZE
-1);
950 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
951 ldata
->read_head
= head
;
952 ldata
->read_cnt
-= cnt
;
953 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
955 if (L_ECHOPRT(tty
)) {
956 if (!ldata
->erasing
) {
957 echo_char_raw('\\', ldata
);
960 /* if cnt > 1, output a multi-byte character */
963 head
= (head
+1) & (N_TTY_BUF_SIZE
-1);
964 echo_char_raw(ldata
->read_buf
[head
],
966 echo_move_back_col(ldata
);
968 } else if (kill_type
== ERASE
&& !L_ECHOE(tty
)) {
969 echo_char(ERASE_CHAR(tty
), tty
);
970 } else if (c
== '\t') {
971 unsigned int num_chars
= 0;
973 unsigned long tail
= ldata
->read_head
;
976 * Count the columns used for characters
977 * since the start of input or after a
979 * This info is used to go back the correct
982 while (tail
!= ldata
->canon_head
) {
983 tail
= (tail
-1) & (N_TTY_BUF_SIZE
-1);
984 c
= ldata
->read_buf
[tail
];
988 } else if (iscntrl(c
)) {
991 } else if (!is_continuation(c
, tty
)) {
995 echo_erase_tab(num_chars
, after_tab
, ldata
);
997 if (iscntrl(c
) && L_ECHOCTL(tty
)) {
998 echo_char_raw('\b', ldata
);
999 echo_char_raw(' ', ldata
);
1000 echo_char_raw('\b', ldata
);
1002 if (!iscntrl(c
) || L_ECHOCTL(tty
)) {
1003 echo_char_raw('\b', ldata
);
1004 echo_char_raw(' ', ldata
);
1005 echo_char_raw('\b', ldata
);
1009 if (kill_type
== ERASE
)
1012 if (ldata
->read_head
== ldata
->canon_head
&& L_ECHO(tty
))
1013 finish_erasing(ldata
);
1017 * isig - handle the ISIG optio
1021 * Called when a signal is being sent due to terminal input.
1022 * Called from the driver receive_buf path so serialized.
1024 * Locking: ctrl_lock
1027 static inline void isig(int sig
, struct tty_struct
*tty
)
1029 struct pid
*tty_pgrp
= tty_get_pgrp(tty
);
1031 kill_pgrp(tty_pgrp
, sig
, 1);
1037 * n_tty_receive_break - handle break
1040 * An RS232 break event has been hit in the incoming bitstream. This
1041 * can cause a variety of events depending upon the termios settings.
1043 * Called from the receive_buf path so single threaded.
1046 static inline void n_tty_receive_break(struct tty_struct
*tty
)
1048 struct n_tty_data
*ldata
= tty
->disc_data
;
1052 if (I_BRKINT(tty
)) {
1054 if (!L_NOFLSH(tty
)) {
1055 n_tty_flush_buffer(tty
);
1056 tty_driver_flush_buffer(tty
);
1060 if (I_PARMRK(tty
)) {
1061 put_tty_queue('\377', ldata
);
1062 put_tty_queue('\0', ldata
);
1064 put_tty_queue('\0', ldata
);
1065 wake_up_interruptible(&tty
->read_wait
);
1069 * n_tty_receive_overrun - handle overrun reporting
1072 * Data arrived faster than we could process it. While the tty
1073 * driver has flagged this the bits that were missed are gone
1076 * Called from the receive_buf path so single threaded. Does not
1077 * need locking as num_overrun and overrun_time are function
1081 static inline void n_tty_receive_overrun(struct tty_struct
*tty
)
1083 struct n_tty_data
*ldata
= tty
->disc_data
;
1086 ldata
->num_overrun
++;
1087 if (time_after(jiffies
, ldata
->overrun_time
+ HZ
) ||
1088 time_after(ldata
->overrun_time
, jiffies
)) {
1089 printk(KERN_WARNING
"%s: %d input overrun(s)\n",
1091 ldata
->num_overrun
);
1092 ldata
->overrun_time
= jiffies
;
1093 ldata
->num_overrun
= 0;
1098 * n_tty_receive_parity_error - error notifier
1099 * @tty: terminal device
1102 * Process a parity error and queue the right data to indicate
1103 * the error case if necessary. Locking as per n_tty_receive_buf.
1105 static inline void n_tty_receive_parity_error(struct tty_struct
*tty
,
1108 struct n_tty_data
*ldata
= tty
->disc_data
;
1112 if (I_PARMRK(tty
)) {
1113 put_tty_queue('\377', ldata
);
1114 put_tty_queue('\0', ldata
);
1115 put_tty_queue(c
, ldata
);
1116 } else if (I_INPCK(tty
))
1117 put_tty_queue('\0', ldata
);
1119 put_tty_queue(c
, ldata
);
1120 wake_up_interruptible(&tty
->read_wait
);
1124 * n_tty_receive_char - perform processing
1125 * @tty: terminal device
1128 * Process an individual character of input received from the driver.
1129 * This is serialized with respect to itself by the rules for the
1133 static inline void n_tty_receive_char(struct tty_struct
*tty
, unsigned char c
)
1135 struct n_tty_data
*ldata
= tty
->disc_data
;
1136 unsigned long flags
;
1140 put_tty_queue(c
, ldata
);
1146 if (I_IUCLC(tty
) && L_IEXTEN(tty
))
1149 if (L_EXTPROC(tty
)) {
1150 put_tty_queue(c
, ldata
);
1154 if (tty
->stopped
&& !tty
->flow_stopped
&& I_IXON(tty
) &&
1155 I_IXANY(tty
) && c
!= START_CHAR(tty
) && c
!= STOP_CHAR(tty
) &&
1156 c
!= INTR_CHAR(tty
) && c
!= QUIT_CHAR(tty
) && c
!= SUSP_CHAR(tty
)) {
1158 process_echoes(tty
);
1163 if (c
== START_CHAR(tty
)) {
1165 process_echoes(tty
);
1166 } else if (c
== STOP_CHAR(tty
))
1173 * If the previous character was LNEXT, or we know that this
1174 * character is not one of the characters that we'll have to
1175 * handle specially, do shortcut processing to speed things
1178 if (!test_bit(c
, ldata
->process_char_map
) || ldata
->lnext
) {
1180 parmrk
= (c
== (unsigned char) '\377' && I_PARMRK(tty
)) ? 1 : 0;
1181 if (ldata
->read_cnt
>= (N_TTY_BUF_SIZE
- parmrk
- 1)) {
1182 /* beep if no space */
1184 process_output('\a', tty
);
1188 finish_erasing(ldata
);
1189 /* Record the column of first canon char. */
1190 if (ldata
->canon_head
== ldata
->read_head
)
1191 echo_set_canon_col(ldata
);
1193 process_echoes(tty
);
1196 put_tty_queue(c
, ldata
);
1197 put_tty_queue(c
, ldata
);
1202 if (c
== START_CHAR(tty
)) {
1204 process_echoes(tty
);
1207 if (c
== STOP_CHAR(tty
)) {
1216 if (c
== INTR_CHAR(tty
))
1219 if (c
== QUIT_CHAR(tty
))
1222 if (c
== SUSP_CHAR(tty
)) {
1224 if (!L_NOFLSH(tty
)) {
1225 n_tty_flush_buffer(tty
);
1226 tty_driver_flush_buffer(tty
);
1232 process_echoes(tty
);
1244 } else if (c
== '\n' && I_INLCR(tty
))
1247 if (ldata
->icanon
) {
1248 if (c
== ERASE_CHAR(tty
) || c
== KILL_CHAR(tty
) ||
1249 (c
== WERASE_CHAR(tty
) && L_IEXTEN(tty
))) {
1251 process_echoes(tty
);
1254 if (c
== LNEXT_CHAR(tty
) && L_IEXTEN(tty
)) {
1257 finish_erasing(ldata
);
1258 if (L_ECHOCTL(tty
)) {
1259 echo_char_raw('^', ldata
);
1260 echo_char_raw('\b', ldata
);
1261 process_echoes(tty
);
1266 if (c
== REPRINT_CHAR(tty
) && L_ECHO(tty
) &&
1268 unsigned long tail
= ldata
->canon_head
;
1270 finish_erasing(ldata
);
1272 echo_char_raw('\n', ldata
);
1273 while (tail
!= ldata
->read_head
) {
1274 echo_char(ldata
->read_buf
[tail
], tty
);
1275 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
1277 process_echoes(tty
);
1281 if (ldata
->read_cnt
>= N_TTY_BUF_SIZE
) {
1283 process_output('\a', tty
);
1286 if (L_ECHO(tty
) || L_ECHONL(tty
)) {
1287 echo_char_raw('\n', ldata
);
1288 process_echoes(tty
);
1290 goto handle_newline
;
1292 if (c
== EOF_CHAR(tty
)) {
1293 if (ldata
->read_cnt
>= N_TTY_BUF_SIZE
)
1295 if (ldata
->canon_head
!= ldata
->read_head
)
1296 set_bit(TTY_PUSH
, &tty
->flags
);
1297 c
= __DISABLED_CHAR
;
1298 goto handle_newline
;
1300 if ((c
== EOL_CHAR(tty
)) ||
1301 (c
== EOL2_CHAR(tty
) && L_IEXTEN(tty
))) {
1302 parmrk
= (c
== (unsigned char) '\377' && I_PARMRK(tty
))
1304 if (ldata
->read_cnt
>= (N_TTY_BUF_SIZE
- parmrk
)) {
1306 process_output('\a', tty
);
1310 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1313 /* Record the column of first canon char. */
1314 if (ldata
->canon_head
== ldata
->read_head
)
1315 echo_set_canon_col(ldata
);
1317 process_echoes(tty
);
1320 * XXX does PARMRK doubling happen for
1321 * EOL_CHAR and EOL2_CHAR?
1324 put_tty_queue(c
, ldata
);
1327 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
1328 set_bit(ldata
->read_head
, ldata
->read_flags
);
1329 put_tty_queue_nolock(c
, ldata
);
1330 ldata
->canon_head
= ldata
->read_head
;
1331 ldata
->canon_data
++;
1332 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
1333 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
1334 if (waitqueue_active(&tty
->read_wait
))
1335 wake_up_interruptible(&tty
->read_wait
);
1340 parmrk
= (c
== (unsigned char) '\377' && I_PARMRK(tty
)) ? 1 : 0;
1341 if (ldata
->read_cnt
>= (N_TTY_BUF_SIZE
- parmrk
- 1)) {
1342 /* beep if no space */
1344 process_output('\a', tty
);
1348 finish_erasing(ldata
);
1350 echo_char_raw('\n', ldata
);
1352 /* Record the column of first canon char. */
1353 if (ldata
->canon_head
== ldata
->read_head
)
1354 echo_set_canon_col(ldata
);
1357 process_echoes(tty
);
1361 put_tty_queue(c
, ldata
);
1363 put_tty_queue(c
, ldata
);
1368 * n_tty_write_wakeup - asynchronous I/O notifier
1371 * Required for the ptys, serial driver etc. since processes
1372 * that attach themselves to the master and rely on ASYNC
1373 * IO must be woken up
1376 static void n_tty_write_wakeup(struct tty_struct
*tty
)
1378 if (tty
->fasync
&& test_and_clear_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
))
1379 kill_fasync(&tty
->fasync
, SIGIO
, POLL_OUT
);
1383 * n_tty_receive_buf - data receive
1384 * @tty: terminal device
1387 * @count: characters
1389 * Called by the terminal driver when a block of characters has
1390 * been received. This function must be called from soft contexts
1391 * not from interrupt context. The driver is responsible for making
1392 * calls one at a time and in order (or using flush_to_ldisc)
1395 static void n_tty_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
1396 char *fp
, int count
)
1398 struct n_tty_data
*ldata
= tty
->disc_data
;
1399 const unsigned char *p
;
1400 char *f
, flags
= TTY_NORMAL
;
1403 unsigned long cpuflags
;
1405 if (ldata
->real_raw
) {
1406 raw_spin_lock_irqsave(&ldata
->read_lock
, cpuflags
);
1407 i
= min(N_TTY_BUF_SIZE
- ldata
->read_cnt
,
1408 N_TTY_BUF_SIZE
- ldata
->read_head
);
1410 memcpy(ldata
->read_buf
+ ldata
->read_head
, cp
, i
);
1411 ldata
->read_head
= (ldata
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
1412 ldata
->read_cnt
+= i
;
1416 i
= min(N_TTY_BUF_SIZE
- ldata
->read_cnt
,
1417 N_TTY_BUF_SIZE
- ldata
->read_head
);
1419 memcpy(ldata
->read_buf
+ ldata
->read_head
, cp
, i
);
1420 ldata
->read_head
= (ldata
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
1421 ldata
->read_cnt
+= i
;
1422 raw_spin_unlock_irqrestore(&ldata
->read_lock
, cpuflags
);
1424 for (i
= count
, p
= cp
, f
= fp
; i
; i
--, p
++) {
1429 n_tty_receive_char(tty
, *p
);
1432 n_tty_receive_break(tty
);
1436 n_tty_receive_parity_error(tty
, *p
);
1439 n_tty_receive_overrun(tty
);
1442 printk(KERN_ERR
"%s: unknown flag %d\n",
1443 tty_name(tty
, buf
), flags
);
1447 if (tty
->ops
->flush_chars
)
1448 tty
->ops
->flush_chars(tty
);
1451 n_tty_set_room(tty
);
1453 if ((!ldata
->icanon
&& (ldata
->read_cnt
>= tty
->minimum_to_wake
)) ||
1455 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
1456 if (waitqueue_active(&tty
->read_wait
))
1457 wake_up_interruptible(&tty
->read_wait
);
1461 * Check the remaining room for the input canonicalization
1462 * mode. We don't want to throttle the driver if we're in
1463 * canonical mode and don't have a newline yet!
1466 tty_set_flow_change(tty
, TTY_THROTTLE_SAFE
);
1467 if (tty
->receive_room
>= TTY_THRESHOLD_THROTTLE
)
1469 if (!tty_throttle_safe(tty
))
1472 __tty_set_flow_change(tty
, 0);
1475 int is_ignored(int sig
)
1477 return (sigismember(¤t
->blocked
, sig
) ||
1478 current
->sighand
->action
[sig
-1].sa
.sa_handler
== SIG_IGN
);
1482 * n_tty_set_termios - termios data changed
1484 * @old: previous data
1486 * Called by the tty layer when the user changes termios flags so
1487 * that the line discipline can plan ahead. This function cannot sleep
1488 * and is protected from re-entry by the tty layer. The user is
1489 * guaranteed that this function will not be re-entered or in progress
1490 * when the ldisc is closed.
1492 * Locking: Caller holds tty->termios_mutex
1495 static void n_tty_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
1497 struct n_tty_data
*ldata
= tty
->disc_data
;
1498 int canon_change
= 1;
1501 canon_change
= (old
->c_lflag
^ tty
->termios
.c_lflag
) & ICANON
;
1503 bitmap_zero(ldata
->read_flags
, N_TTY_BUF_SIZE
);
1504 ldata
->canon_head
= ldata
->read_tail
;
1505 ldata
->canon_data
= 0;
1509 if (canon_change
&& !L_ICANON(tty
) && ldata
->read_cnt
)
1510 wake_up_interruptible(&tty
->read_wait
);
1512 ldata
->icanon
= (L_ICANON(tty
) != 0);
1513 if (test_bit(TTY_HW_COOK_IN
, &tty
->flags
)) {
1515 ldata
->real_raw
= 1;
1516 n_tty_set_room(tty
);
1519 if (I_ISTRIP(tty
) || I_IUCLC(tty
) || I_IGNCR(tty
) ||
1520 I_ICRNL(tty
) || I_INLCR(tty
) || L_ICANON(tty
) ||
1521 I_IXON(tty
) || L_ISIG(tty
) || L_ECHO(tty
) ||
1523 bitmap_zero(ldata
->process_char_map
, 256);
1525 if (I_IGNCR(tty
) || I_ICRNL(tty
))
1526 set_bit('\r', ldata
->process_char_map
);
1528 set_bit('\n', ldata
->process_char_map
);
1530 if (L_ICANON(tty
)) {
1531 set_bit(ERASE_CHAR(tty
), ldata
->process_char_map
);
1532 set_bit(KILL_CHAR(tty
), ldata
->process_char_map
);
1533 set_bit(EOF_CHAR(tty
), ldata
->process_char_map
);
1534 set_bit('\n', ldata
->process_char_map
);
1535 set_bit(EOL_CHAR(tty
), ldata
->process_char_map
);
1536 if (L_IEXTEN(tty
)) {
1537 set_bit(WERASE_CHAR(tty
),
1538 ldata
->process_char_map
);
1539 set_bit(LNEXT_CHAR(tty
),
1540 ldata
->process_char_map
);
1541 set_bit(EOL2_CHAR(tty
),
1542 ldata
->process_char_map
);
1544 set_bit(REPRINT_CHAR(tty
),
1545 ldata
->process_char_map
);
1549 set_bit(START_CHAR(tty
), ldata
->process_char_map
);
1550 set_bit(STOP_CHAR(tty
), ldata
->process_char_map
);
1553 set_bit(INTR_CHAR(tty
), ldata
->process_char_map
);
1554 set_bit(QUIT_CHAR(tty
), ldata
->process_char_map
);
1555 set_bit(SUSP_CHAR(tty
), ldata
->process_char_map
);
1557 clear_bit(__DISABLED_CHAR
, ldata
->process_char_map
);
1559 ldata
->real_raw
= 0;
1562 if ((I_IGNBRK(tty
) || (!I_BRKINT(tty
) && !I_PARMRK(tty
))) &&
1563 (I_IGNPAR(tty
) || !I_INPCK(tty
)) &&
1564 (tty
->driver
->flags
& TTY_DRIVER_REAL_RAW
))
1565 ldata
->real_raw
= 1;
1567 ldata
->real_raw
= 0;
1569 n_tty_set_room(tty
);
1570 /* The termios change make the tty ready for I/O */
1571 wake_up_interruptible(&tty
->write_wait
);
1572 wake_up_interruptible(&tty
->read_wait
);
1576 * n_tty_close - close the ldisc for this tty
1579 * Called from the terminal layer when this line discipline is
1580 * being shut down, either because of a close or becsuse of a
1581 * discipline change. The function will not be called while other
1582 * ldisc methods are in progress.
1585 static void n_tty_close(struct tty_struct
*tty
)
1587 struct n_tty_data
*ldata
= tty
->disc_data
;
1589 n_tty_flush_buffer(tty
);
1590 kfree(ldata
->read_buf
);
1591 kfree(ldata
->echo_buf
);
1593 tty
->disc_data
= NULL
;
1597 * n_tty_open - open an ldisc
1598 * @tty: terminal to open
1600 * Called when this line discipline is being attached to the
1601 * terminal device. Can sleep. Called serialized so that no
1602 * other events will occur in parallel. No further open will occur
1606 static int n_tty_open(struct tty_struct
*tty
)
1608 struct n_tty_data
*ldata
;
1610 ldata
= kzalloc(sizeof(*ldata
), GFP_KERNEL
);
1614 ldata
->overrun_time
= jiffies
;
1615 mutex_init(&ldata
->atomic_read_lock
);
1616 mutex_init(&ldata
->output_lock
);
1617 mutex_init(&ldata
->echo_lock
);
1618 raw_spin_lock_init(&ldata
->read_lock
);
1620 /* These are ugly. Currently a malloc failure here can panic */
1621 ldata
->read_buf
= kzalloc(N_TTY_BUF_SIZE
, GFP_KERNEL
);
1622 ldata
->echo_buf
= kzalloc(N_TTY_BUF_SIZE
, GFP_KERNEL
);
1623 if (!ldata
->read_buf
|| !ldata
->echo_buf
)
1626 tty
->disc_data
= ldata
;
1627 reset_buffer_flags(tty
);
1628 tty_unthrottle(tty
);
1630 n_tty_set_termios(tty
, NULL
);
1631 tty
->minimum_to_wake
= 1;
1636 kfree(ldata
->read_buf
);
1637 kfree(ldata
->echo_buf
);
1643 static inline int input_available_p(struct tty_struct
*tty
, int amt
)
1645 struct n_tty_data
*ldata
= tty
->disc_data
;
1647 tty_flush_to_ldisc(tty
);
1648 if (ldata
->icanon
&& !L_EXTPROC(tty
)) {
1649 if (ldata
->canon_data
)
1651 } else if (ldata
->read_cnt
>= (amt
? amt
: 1))
1658 * copy_from_read_buf - copy read data directly
1659 * @tty: terminal device
1663 * Helper function to speed up n_tty_read. It is only called when
1664 * ICANON is off; it copies characters straight from the tty queue to
1665 * user space directly. It can be profitably called twice; once to
1666 * drain the space from the tail pointer to the (physical) end of the
1667 * buffer, and once to drain the space from the (physical) beginning of
1668 * the buffer to head pointer.
1670 * Called under the ldata->atomic_read_lock sem
1674 static int copy_from_read_buf(struct tty_struct
*tty
,
1675 unsigned char __user
**b
,
1679 struct n_tty_data
*ldata
= tty
->disc_data
;
1682 unsigned long flags
;
1686 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
1687 n
= min(ldata
->read_cnt
, N_TTY_BUF_SIZE
- ldata
->read_tail
);
1689 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
1691 retval
= copy_to_user(*b
, &ldata
->read_buf
[ldata
->read_tail
], n
);
1694 ldata
->read_buf
[ldata
->read_tail
] == EOF_CHAR(tty
);
1695 tty_audit_add_data(tty
, &ldata
->read_buf
[ldata
->read_tail
], n
,
1697 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
1698 ldata
->read_tail
= (ldata
->read_tail
+ n
) & (N_TTY_BUF_SIZE
-1);
1699 ldata
->read_cnt
-= n
;
1700 /* Turn single EOF into zero-length read */
1701 if (L_EXTPROC(tty
) && ldata
->icanon
&& is_eof
&& !ldata
->read_cnt
)
1703 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
1710 extern ssize_t
redirected_tty_write(struct file
*, const char __user
*,
1714 * job_control - check job control
1716 * @file: file handle
1718 * Perform job control management checks on this file/tty descriptor
1719 * and if appropriate send any needed signals and return a negative
1720 * error code if action should be taken.
1722 * Locking: redirected write test is safe
1723 * current->signal->tty check is safe
1724 * ctrl_lock to safely reference tty->pgrp
1727 static int job_control(struct tty_struct
*tty
, struct file
*file
)
1729 /* Job control check -- must be done at start and after
1730 every sleep (POSIX.1 7.1.1.4). */
1731 /* NOTE: not yet done after every sleep pending a thorough
1732 check of the logic of this change. -- jlc */
1733 /* don't stop on /dev/console */
1734 if (file
->f_op
->write
== redirected_tty_write
||
1735 current
->signal
->tty
!= tty
)
1738 spin_lock_irq(&tty
->ctrl_lock
);
1740 printk(KERN_ERR
"n_tty_read: no tty->pgrp!\n");
1741 else if (task_pgrp(current
) != tty
->pgrp
) {
1742 spin_unlock_irq(&tty
->ctrl_lock
);
1743 if (is_ignored(SIGTTIN
) || is_current_pgrp_orphaned())
1745 kill_pgrp(task_pgrp(current
), SIGTTIN
, 1);
1746 set_thread_flag(TIF_SIGPENDING
);
1747 return -ERESTARTSYS
;
1749 spin_unlock_irq(&tty
->ctrl_lock
);
1755 * n_tty_read - read function for tty
1757 * @file: file object
1758 * @buf: userspace buffer pointer
1761 * Perform reads for the line discipline. We are guaranteed that the
1762 * line discipline will not be closed under us but we may get multiple
1763 * parallel readers and must handle this ourselves. We may also get
1764 * a hangup. Always called in user context, may sleep.
1766 * This code must be sure never to sleep through a hangup.
1769 static ssize_t
n_tty_read(struct tty_struct
*tty
, struct file
*file
,
1770 unsigned char __user
*buf
, size_t nr
)
1772 struct n_tty_data
*ldata
= tty
->disc_data
;
1773 unsigned char __user
*b
= buf
;
1774 DECLARE_WAITQUEUE(wait
, current
);
1780 unsigned long flags
;
1784 c
= job_control(tty
, file
);
1789 timeout
= MAX_SCHEDULE_TIMEOUT
;
1790 if (!ldata
->icanon
) {
1791 time
= (HZ
/ 10) * TIME_CHAR(tty
);
1792 minimum
= MIN_CHAR(tty
);
1795 tty
->minimum_to_wake
= 1;
1796 else if (!waitqueue_active(&tty
->read_wait
) ||
1797 (tty
->minimum_to_wake
> minimum
))
1798 tty
->minimum_to_wake
= minimum
;
1805 tty
->minimum_to_wake
= minimum
= 1;
1810 * Internal serialization of reads.
1812 if (file
->f_flags
& O_NONBLOCK
) {
1813 if (!mutex_trylock(&ldata
->atomic_read_lock
))
1816 if (mutex_lock_interruptible(&ldata
->atomic_read_lock
))
1817 return -ERESTARTSYS
;
1819 packet
= tty
->packet
;
1821 add_wait_queue(&tty
->read_wait
, &wait
);
1823 /* First test for status change. */
1824 if (packet
&& tty
->link
->ctrl_status
) {
1828 spin_lock_irqsave(&tty
->link
->ctrl_lock
, flags
);
1829 cs
= tty
->link
->ctrl_status
;
1830 tty
->link
->ctrl_status
= 0;
1831 spin_unlock_irqrestore(&tty
->link
->ctrl_lock
, flags
);
1832 if (tty_put_user(tty
, cs
, b
++)) {
1840 /* This statement must be first before checking for input
1841 so that any interrupt will set the state back to
1843 set_current_state(TASK_INTERRUPTIBLE
);
1845 if (((minimum
- (b
- buf
)) < tty
->minimum_to_wake
) &&
1846 ((minimum
- (b
- buf
)) >= 1))
1847 tty
->minimum_to_wake
= (minimum
- (b
- buf
));
1849 if (!input_available_p(tty
, 0)) {
1850 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
)) {
1854 if (tty_hung_up_p(file
))
1858 if (file
->f_flags
& O_NONBLOCK
) {
1862 if (signal_pending(current
)) {
1863 retval
= -ERESTARTSYS
;
1866 /* FIXME: does n_tty_set_room need locking ? */
1867 n_tty_set_room(tty
);
1868 timeout
= schedule_timeout(timeout
);
1871 __set_current_state(TASK_RUNNING
);
1873 /* Deal with packet mode. */
1874 if (packet
&& b
== buf
) {
1875 if (tty_put_user(tty
, TIOCPKT_DATA
, b
++)) {
1883 if (ldata
->icanon
&& !L_EXTPROC(tty
)) {
1884 /* N.B. avoid overrun if nr == 0 */
1885 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
1886 while (nr
&& ldata
->read_cnt
) {
1889 eol
= test_and_clear_bit(ldata
->read_tail
,
1891 c
= ldata
->read_buf
[ldata
->read_tail
];
1892 ldata
->read_tail
= ((ldata
->read_tail
+1) &
1893 (N_TTY_BUF_SIZE
-1));
1896 /* this test should be redundant:
1897 * we shouldn't be reading data if
1900 if (--ldata
->canon_data
< 0)
1901 ldata
->canon_data
= 0;
1903 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
1905 if (!eol
|| (c
!= __DISABLED_CHAR
)) {
1906 if (tty_put_user(tty
, c
, b
++)) {
1909 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
1915 tty_audit_push(tty
);
1916 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
1919 raw_spin_lock_irqsave(&ldata
->read_lock
, flags
);
1921 raw_spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
1926 /* The copy function takes the read lock and handles
1927 locking internally for this case */
1928 uncopied
= copy_from_read_buf(tty
, &b
, &nr
);
1929 uncopied
+= copy_from_read_buf(tty
, &b
, &nr
);
1936 /* If there is enough space in the read buffer now, let the
1937 * low-level driver know. We use n_tty_chars_in_buffer() to
1938 * check the buffer, as it now knows about canonical mode.
1939 * Otherwise, if the driver is throttled and the line is
1940 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1941 * we won't get any more characters.
1944 tty_set_flow_change(tty
, TTY_UNTHROTTLE_SAFE
);
1945 if (n_tty_chars_in_buffer(tty
) > TTY_THRESHOLD_UNTHROTTLE
)
1949 n_tty_set_room(tty
);
1950 if (!tty_unthrottle_safe(tty
))
1953 __tty_set_flow_change(tty
, 0);
1955 if (b
- buf
>= minimum
)
1960 mutex_unlock(&ldata
->atomic_read_lock
);
1961 remove_wait_queue(&tty
->read_wait
, &wait
);
1963 if (!waitqueue_active(&tty
->read_wait
))
1964 tty
->minimum_to_wake
= minimum
;
1966 __set_current_state(TASK_RUNNING
);
1971 clear_bit(TTY_PUSH
, &tty
->flags
);
1972 } else if (test_and_clear_bit(TTY_PUSH
, &tty
->flags
))
1975 n_tty_set_room(tty
);
1980 * n_tty_write - write function for tty
1982 * @file: file object
1983 * @buf: userspace buffer pointer
1986 * Write function of the terminal device. This is serialized with
1987 * respect to other write callers but not to termios changes, reads
1988 * and other such events. Since the receive code will echo characters,
1989 * thus calling driver write methods, the output_lock is used in
1990 * the output processing functions called here as well as in the
1991 * echo processing function to protect the column state and space
1992 * left in the buffer.
1994 * This code must be sure never to sleep through a hangup.
1996 * Locking: output_lock to protect column state and space left
1997 * (note that the process_output*() functions take this
2001 static ssize_t
n_tty_write(struct tty_struct
*tty
, struct file
*file
,
2002 const unsigned char *buf
, size_t nr
)
2004 const unsigned char *b
= buf
;
2005 DECLARE_WAITQUEUE(wait
, current
);
2009 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2010 if (L_TOSTOP(tty
) && file
->f_op
->write
!= redirected_tty_write
) {
2011 retval
= tty_check_change(tty
);
2016 /* Write out any echoed characters that are still pending */
2017 process_echoes(tty
);
2019 add_wait_queue(&tty
->write_wait
, &wait
);
2021 set_current_state(TASK_INTERRUPTIBLE
);
2022 if (signal_pending(current
)) {
2023 retval
= -ERESTARTSYS
;
2026 if (tty_hung_up_p(file
) || (tty
->link
&& !tty
->link
->count
)) {
2030 if (O_OPOST(tty
) && !(test_bit(TTY_HW_COOK_OUT
, &tty
->flags
))) {
2032 ssize_t num
= process_output_block(tty
, b
, nr
);
2044 if (process_output(c
, tty
) < 0)
2048 if (tty
->ops
->flush_chars
)
2049 tty
->ops
->flush_chars(tty
);
2052 c
= tty
->ops
->write(tty
, b
, nr
);
2065 if (file
->f_flags
& O_NONBLOCK
) {
2072 __set_current_state(TASK_RUNNING
);
2073 remove_wait_queue(&tty
->write_wait
, &wait
);
2074 if (b
- buf
!= nr
&& tty
->fasync
)
2075 set_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
2076 return (b
- buf
) ? b
- buf
: retval
;
2080 * n_tty_poll - poll method for N_TTY
2081 * @tty: terminal device
2082 * @file: file accessing it
2085 * Called when the line discipline is asked to poll() for data or
2086 * for special events. This code is not serialized with respect to
2087 * other events save open/close.
2089 * This code must be sure never to sleep through a hangup.
2090 * Called without the kernel lock held - fine
2093 static unsigned int n_tty_poll(struct tty_struct
*tty
, struct file
*file
,
2096 unsigned int mask
= 0;
2098 poll_wait(file
, &tty
->read_wait
, wait
);
2099 poll_wait(file
, &tty
->write_wait
, wait
);
2100 if (input_available_p(tty
, TIME_CHAR(tty
) ? 0 : MIN_CHAR(tty
)))
2101 mask
|= POLLIN
| POLLRDNORM
;
2102 if (tty
->packet
&& tty
->link
->ctrl_status
)
2103 mask
|= POLLPRI
| POLLIN
| POLLRDNORM
;
2104 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
))
2106 if (tty_hung_up_p(file
))
2108 if (!(mask
& (POLLHUP
| POLLIN
| POLLRDNORM
))) {
2109 if (MIN_CHAR(tty
) && !TIME_CHAR(tty
))
2110 tty
->minimum_to_wake
= MIN_CHAR(tty
);
2112 tty
->minimum_to_wake
= 1;
2114 if (tty
->ops
->write
&& !tty_is_writelocked(tty
) &&
2115 tty_chars_in_buffer(tty
) < WAKEUP_CHARS
&&
2116 tty_write_room(tty
) > 0)
2117 mask
|= POLLOUT
| POLLWRNORM
;
2121 static unsigned long inq_canon(struct n_tty_data
*ldata
)
2125 if (!ldata
->canon_data
)
2127 head
= ldata
->canon_head
;
2128 tail
= ldata
->read_tail
;
2129 nr
= (head
- tail
) & (N_TTY_BUF_SIZE
-1);
2130 /* Skip EOF-chars.. */
2131 while (head
!= tail
) {
2132 if (test_bit(tail
, ldata
->read_flags
) &&
2133 ldata
->read_buf
[tail
] == __DISABLED_CHAR
)
2135 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
2140 static int n_tty_ioctl(struct tty_struct
*tty
, struct file
*file
,
2141 unsigned int cmd
, unsigned long arg
)
2143 struct n_tty_data
*ldata
= tty
->disc_data
;
2148 return put_user(tty_chars_in_buffer(tty
), (int __user
*) arg
);
2150 /* FIXME: Locking */
2151 retval
= ldata
->read_cnt
;
2153 retval
= inq_canon(ldata
);
2154 return put_user(retval
, (unsigned int __user
*) arg
);
2156 return n_tty_ioctl_helper(tty
, file
, cmd
, arg
);
2160 struct tty_ldisc_ops tty_ldisc_N_TTY
= {
2161 .magic
= TTY_LDISC_MAGIC
,
2164 .close
= n_tty_close
,
2165 .flush_buffer
= n_tty_flush_buffer
,
2166 .chars_in_buffer
= n_tty_chars_in_buffer
,
2168 .write
= n_tty_write
,
2169 .ioctl
= n_tty_ioctl
,
2170 .set_termios
= n_tty_set_termios
,
2172 .receive_buf
= n_tty_receive_buf
,
2173 .write_wakeup
= n_tty_write_wakeup
2177 * n_tty_inherit_ops - inherit N_TTY methods
2178 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2180 * Enables a 'subclass' line discipline to 'inherit' N_TTY
2184 void n_tty_inherit_ops(struct tty_ldisc_ops
*ops
)
2186 *ops
= tty_ldisc_N_TTY
;
2188 ops
->refcount
= ops
->flags
= 0;
2190 EXPORT_SYMBOL_GPL(n_tty_inherit_ops
);