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>
54 /* number of characters left in xmit buffer before select has we have room */
55 #define WAKEUP_CHARS 256
58 * This defines the low- and high-watermarks for throttling and
59 * unthrottling the TTY driver. These watermarks are used for
60 * controlling the space in the read buffer.
62 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
63 #define TTY_THRESHOLD_UNTHROTTLE 128
66 * Special byte codes used in the echo buffer to represent operations
67 * or special handling of characters. Bytes in the echo buffer that
68 * are not part of such special blocks are treated as normal character
71 #define ECHO_OP_START 0xff
72 #define ECHO_OP_MOVE_BACK_COL 0x80
73 #define ECHO_OP_SET_CANON_COL 0x81
74 #define ECHO_OP_ERASE_TAB 0x82
78 unsigned long overrun_time
;
81 unsigned char lnext
:1, erasing
:1, raw
:1, real_raw
:1, icanon
:1;
82 unsigned char echo_overrun
:1;
84 DECLARE_BITMAP(process_char_map
, 256);
85 DECLARE_BITMAP(read_flags
, N_TTY_BUF_SIZE
);
92 unsigned char *echo_buf
;
93 unsigned int echo_pos
;
94 unsigned int echo_cnt
;
97 unsigned long canon_head
;
98 unsigned int canon_column
;
100 struct mutex atomic_read_lock
;
101 struct mutex output_lock
;
102 struct mutex echo_lock
;
103 spinlock_t read_lock
;
106 static inline int tty_put_user(struct tty_struct
*tty
, unsigned char x
,
107 unsigned char __user
*ptr
)
109 struct n_tty_data
*ldata
= tty
->disc_data
;
111 tty_audit_add_data(tty
, &x
, 1, ldata
->icanon
);
112 return put_user(x
, ptr
);
116 * n_tty_set__room - receive space
119 * Called by the driver to find out how much data it is
120 * permitted to feed to the line discipline without any being lost
121 * and thus to manage flow control. Not serialized. Answers for the
125 static void n_tty_set_room(struct tty_struct
*tty
)
127 struct n_tty_data
*ldata
= tty
->disc_data
;
131 /* ldata->read_cnt is not read locked ? */
133 /* Multiply read_cnt by 3, since each byte might take up to
134 * three times as many spaces when PARMRK is set (depending on
135 * its flags, e.g. parity error). */
136 left
= N_TTY_BUF_SIZE
- ldata
->read_cnt
* 3 - 1;
138 left
= N_TTY_BUF_SIZE
- ldata
->read_cnt
- 1;
141 * If we are doing input canonicalization, and there are no
142 * pending newlines, let characters through without limit, so
143 * that erase characters will be handled. Other excess
144 * characters will be beeped.
147 left
= ldata
->icanon
&& !ldata
->canon_data
;
148 old_left
= tty
->receive_room
;
149 tty
->receive_room
= left
;
151 /* Did this open up the receive buffer? We may need to flip */
152 if (left
&& !old_left
)
153 schedule_work(&tty
->buf
.work
);
156 static void put_tty_queue_nolock(unsigned char c
, struct n_tty_data
*ldata
)
158 if (ldata
->read_cnt
< N_TTY_BUF_SIZE
) {
159 ldata
->read_buf
[ldata
->read_head
] = c
;
160 ldata
->read_head
= (ldata
->read_head
+ 1) & (N_TTY_BUF_SIZE
-1);
166 * put_tty_queue - add character to tty
170 * Add a character to the tty read_buf queue. This is done under the
171 * read_lock to serialize character addition and also to protect us
172 * against parallel reads or flushes
175 static void put_tty_queue(unsigned char c
, struct n_tty_data
*ldata
)
179 * The problem of stomping on the buffers ends here.
180 * Why didn't anyone see this one coming? --AJK
182 spin_lock_irqsave(&ldata
->read_lock
, flags
);
183 put_tty_queue_nolock(c
, ldata
);
184 spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
188 * check_unthrottle - allow new receive data
191 * Check whether to call the driver unthrottle functions
193 * Can sleep, may be called under the atomic_read_lock mutex but
194 * this is not guaranteed.
196 static void check_unthrottle(struct tty_struct
*tty
)
203 * reset_buffer_flags - reset buffer state
204 * @tty: terminal to reset
206 * Reset the read buffer counters, clear the flags,
207 * and make sure the driver is unthrottled. Called
208 * from n_tty_open() and n_tty_flush_buffer().
210 * Locking: tty_read_lock for read fields.
213 static void reset_buffer_flags(struct tty_struct
*tty
)
215 struct n_tty_data
*ldata
= tty
->disc_data
;
218 spin_lock_irqsave(&ldata
->read_lock
, flags
);
219 ldata
->read_head
= ldata
->read_tail
= ldata
->read_cnt
= 0;
220 spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
222 mutex_lock(&ldata
->echo_lock
);
223 ldata
->echo_pos
= ldata
->echo_cnt
= ldata
->echo_overrun
= 0;
224 mutex_unlock(&ldata
->echo_lock
);
226 ldata
->canon_head
= ldata
->canon_data
= ldata
->erasing
= 0;
227 bitmap_zero(ldata
->read_flags
, N_TTY_BUF_SIZE
);
232 * n_tty_flush_buffer - clean input queue
233 * @tty: terminal device
235 * Flush the input buffer. Called when the line discipline is
236 * being closed, when the tty layer wants the buffer flushed (eg
237 * at hangup) or when the N_TTY line discipline internally has to
238 * clean the pending queue (for example some signals).
240 * Locking: ctrl_lock, read_lock.
243 static void n_tty_flush_buffer(struct tty_struct
*tty
)
246 /* clear everything and unthrottle the driver */
247 reset_buffer_flags(tty
);
252 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
253 if (tty
->link
->packet
) {
254 tty
->ctrl_status
|= TIOCPKT_FLUSHREAD
;
255 wake_up_interruptible(&tty
->link
->read_wait
);
257 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
261 * n_tty_chars_in_buffer - report available bytes
264 * Report the number of characters buffered to be delivered to user
265 * at this instant in time.
270 static ssize_t
n_tty_chars_in_buffer(struct tty_struct
*tty
)
272 struct n_tty_data
*ldata
= tty
->disc_data
;
276 spin_lock_irqsave(&ldata
->read_lock
, flags
);
277 if (!ldata
->icanon
) {
279 } else if (ldata
->canon_data
) {
280 n
= (ldata
->canon_head
> ldata
->read_tail
) ?
281 ldata
->canon_head
- ldata
->read_tail
:
282 ldata
->canon_head
+ (N_TTY_BUF_SIZE
- ldata
->read_tail
);
284 spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
289 * is_utf8_continuation - utf8 multibyte check
292 * Returns true if the utf8 character 'c' is a multibyte continuation
293 * character. We use this to correctly compute the on screen size
294 * of the character when printing
297 static inline int is_utf8_continuation(unsigned char c
)
299 return (c
& 0xc0) == 0x80;
303 * is_continuation - multibyte check
306 * Returns true if the utf8 character 'c' is a multibyte continuation
307 * character and the terminal is in unicode mode.
310 static inline int is_continuation(unsigned char c
, struct tty_struct
*tty
)
312 return I_IUTF8(tty
) && is_utf8_continuation(c
);
316 * do_output_char - output one character
317 * @c: character (or partial unicode symbol)
318 * @tty: terminal device
319 * @space: space available in tty driver write buffer
321 * This is a helper function that handles one output character
322 * (including special characters like TAB, CR, LF, etc.),
323 * doing OPOST processing and putting the results in the
324 * tty driver's write buffer.
326 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
327 * and NLDLY. They simply aren't relevant in the world today.
328 * If you ever need them, add them here.
330 * Returns the number of bytes of buffer space used or -1 if
333 * Locking: should be called under the output_lock to protect
334 * the column state and space left in the buffer
337 static int do_output_char(unsigned char c
, struct tty_struct
*tty
, int space
)
339 struct n_tty_data
*ldata
= tty
->disc_data
;
352 ldata
->canon_column
= ldata
->column
= 0;
353 tty
->ops
->write(tty
, "\r\n", 2);
356 ldata
->canon_column
= ldata
->column
;
359 if (O_ONOCR(tty
) && ldata
->column
== 0)
364 ldata
->canon_column
= ldata
->column
= 0;
367 ldata
->canon_column
= ldata
->column
= 0;
370 spaces
= 8 - (ldata
->column
& 7);
371 if (O_TABDLY(tty
) == XTABS
) {
374 ldata
->column
+= spaces
;
375 tty
->ops
->write(tty
, " ", spaces
);
378 ldata
->column
+= spaces
;
381 if (ldata
->column
> 0)
388 if (!is_continuation(c
, tty
))
394 tty_put_char(tty
, c
);
399 * process_output - output post processor
400 * @c: character (or partial unicode symbol)
401 * @tty: terminal device
403 * Output one character with OPOST processing.
404 * Returns -1 when the output device is full and the character
407 * Locking: output_lock to protect column state and space left
408 * (also, this is called from n_tty_write under the
409 * tty layer write lock)
412 static int process_output(unsigned char c
, struct tty_struct
*tty
)
414 struct n_tty_data
*ldata
= tty
->disc_data
;
417 mutex_lock(&ldata
->output_lock
);
419 space
= tty_write_room(tty
);
420 retval
= do_output_char(c
, tty
, space
);
422 mutex_unlock(&ldata
->output_lock
);
430 * process_output_block - block post processor
431 * @tty: terminal device
432 * @buf: character buffer
433 * @nr: number of bytes to output
435 * Output a block of characters with OPOST processing.
436 * Returns the number of characters output.
438 * This path is used to speed up block console writes, among other
439 * things when processing blocks of output data. It handles only
440 * the simple cases normally found and helps to generate blocks of
441 * symbols for the console driver and thus improve performance.
443 * Locking: output_lock to protect column state and space left
444 * (also, this is called from n_tty_write under the
445 * tty layer write lock)
448 static ssize_t
process_output_block(struct tty_struct
*tty
,
449 const unsigned char *buf
, unsigned int nr
)
451 struct n_tty_data
*ldata
= tty
->disc_data
;
454 const unsigned char *cp
;
456 mutex_lock(&ldata
->output_lock
);
458 space
= tty_write_room(tty
);
460 mutex_unlock(&ldata
->output_lock
);
466 for (i
= 0, cp
= buf
; i
< nr
; i
++, cp
++) {
467 unsigned char c
= *cp
;
475 ldata
->canon_column
= ldata
->column
;
478 if (O_ONOCR(tty
) && ldata
->column
== 0)
482 ldata
->canon_column
= ldata
->column
= 0;
487 if (ldata
->column
> 0)
494 if (!is_continuation(c
, tty
))
501 i
= tty
->ops
->write(tty
, buf
, i
);
503 mutex_unlock(&ldata
->output_lock
);
508 * process_echoes - write pending echo characters
509 * @tty: terminal device
511 * Write previously buffered echo (and other ldisc-generated)
512 * characters to the tty.
514 * Characters generated by the ldisc (including echoes) need to
515 * be buffered because the driver's write buffer can fill during
516 * heavy program output. Echoing straight to the driver will
517 * often fail under these conditions, causing lost characters and
518 * resulting mismatches of ldisc state information.
520 * Since the ldisc state must represent the characters actually sent
521 * to the driver at the time of the write, operations like certain
522 * changes in column state are also saved in the buffer and executed
525 * A circular fifo buffer is used so that the most recent characters
526 * are prioritized. Also, when control characters are echoed with a
527 * prefixed "^", the pair is treated atomically and thus not separated.
529 * Locking: output_lock to protect column state and space left,
530 * echo_lock to protect the echo buffer
533 static void process_echoes(struct tty_struct
*tty
)
535 struct n_tty_data
*ldata
= tty
->disc_data
;
538 unsigned char *cp
, *buf_end
;
540 if (!ldata
->echo_cnt
)
543 mutex_lock(&ldata
->output_lock
);
544 mutex_lock(&ldata
->echo_lock
);
546 space
= tty_write_room(tty
);
548 buf_end
= ldata
->echo_buf
+ N_TTY_BUF_SIZE
;
549 cp
= ldata
->echo_buf
+ ldata
->echo_pos
;
550 nr
= ldata
->echo_cnt
;
553 if (c
== ECHO_OP_START
) {
556 int no_space_left
= 0;
559 * If the buffer byte is the start of a multi-byte
560 * operation, get the next byte, which is either the
561 * op code or a control character value.
565 opp
-= N_TTY_BUF_SIZE
;
569 unsigned int num_chars
, num_bs
;
571 case ECHO_OP_ERASE_TAB
:
572 if (++opp
== buf_end
)
573 opp
-= N_TTY_BUF_SIZE
;
577 * Determine how many columns to go back
578 * in order to erase the tab.
579 * This depends on the number of columns
580 * used by other characters within the tab
581 * area. If this (modulo 8) count is from
582 * the start of input rather than from a
583 * previous tab, we offset by canon column.
584 * Otherwise, tab spacing is normal.
586 if (!(num_chars
& 0x80))
587 num_chars
+= ldata
->canon_column
;
588 num_bs
= 8 - (num_chars
& 7);
590 if (num_bs
> space
) {
596 tty_put_char(tty
, '\b');
597 if (ldata
->column
> 0)
604 case ECHO_OP_SET_CANON_COL
:
605 ldata
->canon_column
= ldata
->column
;
610 case ECHO_OP_MOVE_BACK_COL
:
611 if (ldata
->column
> 0)
618 /* This is an escaped echo op start code */
623 tty_put_char(tty
, ECHO_OP_START
);
632 * If the op is not a special byte code,
633 * it is a ctrl char tagged to be echoed
634 * as "^X" (where X is the letter
635 * representing the control char).
636 * Note that we must ensure there is
637 * enough space for the whole ctrl pair.
644 tty_put_char(tty
, '^');
645 tty_put_char(tty
, op
^ 0100);
656 !(test_bit(TTY_HW_COOK_OUT
, &tty
->flags
))) {
657 int retval
= do_output_char(c
, tty
, space
);
664 tty_put_char(tty
, c
);
671 /* When end of circular buffer reached, wrap around */
673 cp
-= N_TTY_BUF_SIZE
;
679 ldata
->echo_overrun
= 0;
681 int num_processed
= ldata
->echo_cnt
- nr
;
682 ldata
->echo_pos
+= num_processed
;
683 ldata
->echo_pos
&= N_TTY_BUF_SIZE
- 1;
684 ldata
->echo_cnt
= nr
;
685 if (num_processed
> 0)
686 ldata
->echo_overrun
= 0;
689 mutex_unlock(&ldata
->echo_lock
);
690 mutex_unlock(&ldata
->output_lock
);
692 if (tty
->ops
->flush_chars
)
693 tty
->ops
->flush_chars(tty
);
697 * add_echo_byte - add a byte to the echo buffer
698 * @c: unicode byte to echo
701 * Add a character or operation byte to the echo buffer.
703 * Should be called under the echo lock to protect the echo buffer.
706 static void add_echo_byte(unsigned char c
, struct n_tty_data
*ldata
)
710 if (ldata
->echo_cnt
== N_TTY_BUF_SIZE
) {
711 /* Circular buffer is already at capacity */
712 new_byte_pos
= ldata
->echo_pos
;
715 * Since the buffer start position needs to be advanced,
716 * be sure to step by a whole operation byte group.
718 if (ldata
->echo_buf
[ldata
->echo_pos
] == ECHO_OP_START
) {
719 if (ldata
->echo_buf
[(ldata
->echo_pos
+ 1) &
720 (N_TTY_BUF_SIZE
- 1)] ==
722 ldata
->echo_pos
+= 3;
723 ldata
->echo_cnt
-= 2;
725 ldata
->echo_pos
+= 2;
726 ldata
->echo_cnt
-= 1;
731 ldata
->echo_pos
&= N_TTY_BUF_SIZE
- 1;
733 ldata
->echo_overrun
= 1;
735 new_byte_pos
= ldata
->echo_pos
+ ldata
->echo_cnt
;
736 new_byte_pos
&= N_TTY_BUF_SIZE
- 1;
740 ldata
->echo_buf
[new_byte_pos
] = c
;
744 * echo_move_back_col - add operation to move back a column
747 * Add an operation to the echo buffer to move back one column.
749 * Locking: echo_lock to protect the echo buffer
752 static void echo_move_back_col(struct n_tty_data
*ldata
)
754 mutex_lock(&ldata
->echo_lock
);
755 add_echo_byte(ECHO_OP_START
, ldata
);
756 add_echo_byte(ECHO_OP_MOVE_BACK_COL
, ldata
);
757 mutex_unlock(&ldata
->echo_lock
);
761 * echo_set_canon_col - add operation to set the canon column
764 * Add an operation to the echo buffer to set the canon column
765 * to the current column.
767 * Locking: echo_lock to protect the echo buffer
770 static void echo_set_canon_col(struct n_tty_data
*ldata
)
772 mutex_lock(&ldata
->echo_lock
);
773 add_echo_byte(ECHO_OP_START
, ldata
);
774 add_echo_byte(ECHO_OP_SET_CANON_COL
, ldata
);
775 mutex_unlock(&ldata
->echo_lock
);
779 * echo_erase_tab - add operation to erase a tab
780 * @num_chars: number of character columns already used
781 * @after_tab: true if num_chars starts after a previous tab
784 * Add an operation to the echo buffer to erase a tab.
786 * Called by the eraser function, which knows how many character
787 * columns have been used since either a previous tab or the start
788 * of input. This information will be used later, along with
789 * canon column (if applicable), to go back the correct number
792 * Locking: echo_lock to protect the echo buffer
795 static void echo_erase_tab(unsigned int num_chars
, int after_tab
,
796 struct n_tty_data
*ldata
)
798 mutex_lock(&ldata
->echo_lock
);
800 add_echo_byte(ECHO_OP_START
, ldata
);
801 add_echo_byte(ECHO_OP_ERASE_TAB
, ldata
);
803 /* We only need to know this modulo 8 (tab spacing) */
806 /* Set the high bit as a flag if num_chars is after a previous tab */
810 add_echo_byte(num_chars
, ldata
);
812 mutex_unlock(&ldata
->echo_lock
);
816 * echo_char_raw - echo a character raw
817 * @c: unicode byte to echo
818 * @tty: terminal device
820 * Echo user input back onto the screen. This must be called only when
821 * L_ECHO(tty) is true. Called from the driver receive_buf path.
823 * This variant does not treat control characters specially.
825 * Locking: echo_lock to protect the echo buffer
828 static void echo_char_raw(unsigned char c
, struct n_tty_data
*ldata
)
830 mutex_lock(&ldata
->echo_lock
);
831 if (c
== ECHO_OP_START
) {
832 add_echo_byte(ECHO_OP_START
, ldata
);
833 add_echo_byte(ECHO_OP_START
, ldata
);
835 add_echo_byte(c
, ldata
);
837 mutex_unlock(&ldata
->echo_lock
);
841 * echo_char - echo a character
842 * @c: unicode byte to echo
843 * @tty: terminal device
845 * Echo user input back onto the screen. This must be called only when
846 * L_ECHO(tty) is true. Called from the driver receive_buf path.
848 * This variant tags control characters to be echoed as "^X"
849 * (where X is the letter representing the control char).
851 * Locking: echo_lock to protect the echo buffer
854 static void echo_char(unsigned char c
, struct tty_struct
*tty
)
856 struct n_tty_data
*ldata
= tty
->disc_data
;
858 mutex_lock(&ldata
->echo_lock
);
860 if (c
== ECHO_OP_START
) {
861 add_echo_byte(ECHO_OP_START
, ldata
);
862 add_echo_byte(ECHO_OP_START
, ldata
);
864 if (L_ECHOCTL(tty
) && iscntrl(c
) && c
!= '\t')
865 add_echo_byte(ECHO_OP_START
, ldata
);
866 add_echo_byte(c
, ldata
);
869 mutex_unlock(&ldata
->echo_lock
);
873 * finish_erasing - complete erase
877 static inline void finish_erasing(struct n_tty_data
*ldata
)
879 if (ldata
->erasing
) {
880 echo_char_raw('/', ldata
);
886 * eraser - handle erase function
887 * @c: character input
888 * @tty: terminal device
890 * Perform erase and necessary output when an erase character is
891 * present in the stream from the driver layer. Handles the complexities
892 * of UTF-8 multibyte symbols.
894 * Locking: read_lock for tty buffers
897 static void eraser(unsigned char c
, struct tty_struct
*tty
)
899 struct n_tty_data
*ldata
= tty
->disc_data
;
900 enum { ERASE
, WERASE
, KILL
} kill_type
;
901 int head
, seen_alnums
, cnt
;
904 /* FIXME: locking needed ? */
905 if (ldata
->read_head
== ldata
->canon_head
) {
906 /* process_output('\a', tty); */ /* what do you think? */
909 if (c
== ERASE_CHAR(tty
))
911 else if (c
== WERASE_CHAR(tty
))
915 spin_lock_irqsave(&ldata
->read_lock
, flags
);
916 ldata
->read_cnt
-= ((ldata
->read_head
- ldata
->canon_head
) &
917 (N_TTY_BUF_SIZE
- 1));
918 ldata
->read_head
= ldata
->canon_head
;
919 spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
922 if (!L_ECHOK(tty
) || !L_ECHOKE(tty
) || !L_ECHOE(tty
)) {
923 spin_lock_irqsave(&ldata
->read_lock
, flags
);
924 ldata
->read_cnt
-= ((ldata
->read_head
- ldata
->canon_head
) &
925 (N_TTY_BUF_SIZE
- 1));
926 ldata
->read_head
= ldata
->canon_head
;
927 spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
928 finish_erasing(ldata
);
929 echo_char(KILL_CHAR(tty
), tty
);
930 /* Add a newline if ECHOK is on and ECHOKE is off. */
932 echo_char_raw('\n', ldata
);
939 /* FIXME: Locking ?? */
940 while (ldata
->read_head
!= ldata
->canon_head
) {
941 head
= ldata
->read_head
;
943 /* erase a single possibly multibyte character */
945 head
= (head
- 1) & (N_TTY_BUF_SIZE
-1);
946 c
= ldata
->read_buf
[head
];
947 } while (is_continuation(c
, tty
) && head
!= ldata
->canon_head
);
949 /* do not partially erase */
950 if (is_continuation(c
, tty
))
953 if (kill_type
== WERASE
) {
954 /* Equivalent to BSD's ALTWERASE. */
955 if (isalnum(c
) || c
== '_')
957 else if (seen_alnums
)
960 cnt
= (ldata
->read_head
- head
) & (N_TTY_BUF_SIZE
-1);
961 spin_lock_irqsave(&ldata
->read_lock
, flags
);
962 ldata
->read_head
= head
;
963 ldata
->read_cnt
-= cnt
;
964 spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
966 if (L_ECHOPRT(tty
)) {
967 if (!ldata
->erasing
) {
968 echo_char_raw('\\', ldata
);
971 /* if cnt > 1, output a multi-byte character */
974 head
= (head
+1) & (N_TTY_BUF_SIZE
-1);
975 echo_char_raw(ldata
->read_buf
[head
],
977 echo_move_back_col(ldata
);
979 } else if (kill_type
== ERASE
&& !L_ECHOE(tty
)) {
980 echo_char(ERASE_CHAR(tty
), tty
);
981 } else if (c
== '\t') {
982 unsigned int num_chars
= 0;
984 unsigned long tail
= ldata
->read_head
;
987 * Count the columns used for characters
988 * since the start of input or after a
990 * This info is used to go back the correct
993 while (tail
!= ldata
->canon_head
) {
994 tail
= (tail
-1) & (N_TTY_BUF_SIZE
-1);
995 c
= ldata
->read_buf
[tail
];
999 } else if (iscntrl(c
)) {
1002 } else if (!is_continuation(c
, tty
)) {
1006 echo_erase_tab(num_chars
, after_tab
, ldata
);
1008 if (iscntrl(c
) && L_ECHOCTL(tty
)) {
1009 echo_char_raw('\b', ldata
);
1010 echo_char_raw(' ', ldata
);
1011 echo_char_raw('\b', ldata
);
1013 if (!iscntrl(c
) || L_ECHOCTL(tty
)) {
1014 echo_char_raw('\b', ldata
);
1015 echo_char_raw(' ', ldata
);
1016 echo_char_raw('\b', ldata
);
1020 if (kill_type
== ERASE
)
1023 if (ldata
->read_head
== ldata
->canon_head
&& L_ECHO(tty
))
1024 finish_erasing(ldata
);
1028 * isig - handle the ISIG optio
1031 * @flush: force flush
1033 * Called when a signal is being sent due to terminal input. This
1034 * may caus terminal flushing to take place according to the termios
1035 * settings and character used. Called from the driver receive_buf
1036 * path so serialized.
1038 * Locking: ctrl_lock, read_lock (both via flush buffer)
1041 static inline void isig(int sig
, struct tty_struct
*tty
, int flush
)
1044 kill_pgrp(tty
->pgrp
, sig
, 1);
1045 if (flush
|| !L_NOFLSH(tty
)) {
1046 n_tty_flush_buffer(tty
);
1047 tty_driver_flush_buffer(tty
);
1052 * n_tty_receive_break - handle break
1055 * An RS232 break event has been hit in the incoming bitstream. This
1056 * can cause a variety of events depending upon the termios settings.
1058 * Called from the receive_buf path so single threaded.
1061 static inline void n_tty_receive_break(struct tty_struct
*tty
)
1063 struct n_tty_data
*ldata
= tty
->disc_data
;
1067 if (I_BRKINT(tty
)) {
1068 isig(SIGINT
, tty
, 1);
1071 if (I_PARMRK(tty
)) {
1072 put_tty_queue('\377', ldata
);
1073 put_tty_queue('\0', ldata
);
1075 put_tty_queue('\0', ldata
);
1076 wake_up_interruptible(&tty
->read_wait
);
1080 * n_tty_receive_overrun - handle overrun reporting
1083 * Data arrived faster than we could process it. While the tty
1084 * driver has flagged this the bits that were missed are gone
1087 * Called from the receive_buf path so single threaded. Does not
1088 * need locking as num_overrun and overrun_time are function
1092 static inline void n_tty_receive_overrun(struct tty_struct
*tty
)
1094 struct n_tty_data
*ldata
= tty
->disc_data
;
1097 ldata
->num_overrun
++;
1098 if (time_after(jiffies
, ldata
->overrun_time
+ HZ
) ||
1099 time_after(ldata
->overrun_time
, jiffies
)) {
1100 printk(KERN_WARNING
"%s: %d input overrun(s)\n",
1102 ldata
->num_overrun
);
1103 ldata
->overrun_time
= jiffies
;
1104 ldata
->num_overrun
= 0;
1109 * n_tty_receive_parity_error - error notifier
1110 * @tty: terminal device
1113 * Process a parity error and queue the right data to indicate
1114 * the error case if necessary. Locking as per n_tty_receive_buf.
1116 static inline void n_tty_receive_parity_error(struct tty_struct
*tty
,
1119 struct n_tty_data
*ldata
= tty
->disc_data
;
1123 if (I_PARMRK(tty
)) {
1124 put_tty_queue('\377', ldata
);
1125 put_tty_queue('\0', ldata
);
1126 put_tty_queue(c
, ldata
);
1127 } else if (I_INPCK(tty
))
1128 put_tty_queue('\0', ldata
);
1130 put_tty_queue(c
, ldata
);
1131 wake_up_interruptible(&tty
->read_wait
);
1135 * n_tty_receive_char - perform processing
1136 * @tty: terminal device
1139 * Process an individual character of input received from the driver.
1140 * This is serialized with respect to itself by the rules for the
1144 static inline void n_tty_receive_char(struct tty_struct
*tty
, unsigned char c
)
1146 struct n_tty_data
*ldata
= tty
->disc_data
;
1147 unsigned long flags
;
1151 put_tty_queue(c
, ldata
);
1157 if (I_IUCLC(tty
) && L_IEXTEN(tty
))
1160 if (L_EXTPROC(tty
)) {
1161 put_tty_queue(c
, ldata
);
1165 if (tty
->stopped
&& !tty
->flow_stopped
&& I_IXON(tty
) &&
1166 I_IXANY(tty
) && c
!= START_CHAR(tty
) && c
!= STOP_CHAR(tty
) &&
1167 c
!= INTR_CHAR(tty
) && c
!= QUIT_CHAR(tty
) && c
!= SUSP_CHAR(tty
)) {
1169 process_echoes(tty
);
1174 if (c
== START_CHAR(tty
)) {
1176 process_echoes(tty
);
1177 } else if (c
== STOP_CHAR(tty
))
1184 * If the previous character was LNEXT, or we know that this
1185 * character is not one of the characters that we'll have to
1186 * handle specially, do shortcut processing to speed things
1189 if (!test_bit(c
, ldata
->process_char_map
) || ldata
->lnext
) {
1191 parmrk
= (c
== (unsigned char) '\377' && I_PARMRK(tty
)) ? 1 : 0;
1192 if (ldata
->read_cnt
>= (N_TTY_BUF_SIZE
- parmrk
- 1)) {
1193 /* beep if no space */
1195 process_output('\a', tty
);
1199 finish_erasing(ldata
);
1200 /* Record the column of first canon char. */
1201 if (ldata
->canon_head
== ldata
->read_head
)
1202 echo_set_canon_col(ldata
);
1204 process_echoes(tty
);
1207 put_tty_queue(c
, ldata
);
1208 put_tty_queue(c
, ldata
);
1213 if (c
== START_CHAR(tty
)) {
1215 process_echoes(tty
);
1218 if (c
== STOP_CHAR(tty
)) {
1227 if (c
== INTR_CHAR(tty
))
1230 if (c
== QUIT_CHAR(tty
))
1233 if (c
== SUSP_CHAR(tty
)) {
1236 * Note that we do not use isig() here because we want
1238 * 1) flush, 2) echo, 3) signal
1240 if (!L_NOFLSH(tty
)) {
1241 n_tty_flush_buffer(tty
);
1242 tty_driver_flush_buffer(tty
);
1248 process_echoes(tty
);
1251 kill_pgrp(tty
->pgrp
, signal
, 1);
1261 } else if (c
== '\n' && I_INLCR(tty
))
1264 if (ldata
->icanon
) {
1265 if (c
== ERASE_CHAR(tty
) || c
== KILL_CHAR(tty
) ||
1266 (c
== WERASE_CHAR(tty
) && L_IEXTEN(tty
))) {
1268 process_echoes(tty
);
1271 if (c
== LNEXT_CHAR(tty
) && L_IEXTEN(tty
)) {
1274 finish_erasing(ldata
);
1275 if (L_ECHOCTL(tty
)) {
1276 echo_char_raw('^', ldata
);
1277 echo_char_raw('\b', ldata
);
1278 process_echoes(tty
);
1283 if (c
== REPRINT_CHAR(tty
) && L_ECHO(tty
) &&
1285 unsigned long tail
= ldata
->canon_head
;
1287 finish_erasing(ldata
);
1289 echo_char_raw('\n', ldata
);
1290 while (tail
!= ldata
->read_head
) {
1291 echo_char(ldata
->read_buf
[tail
], tty
);
1292 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
1294 process_echoes(tty
);
1298 if (ldata
->read_cnt
>= N_TTY_BUF_SIZE
) {
1300 process_output('\a', tty
);
1303 if (L_ECHO(tty
) || L_ECHONL(tty
)) {
1304 echo_char_raw('\n', ldata
);
1305 process_echoes(tty
);
1307 goto handle_newline
;
1309 if (c
== EOF_CHAR(tty
)) {
1310 if (ldata
->read_cnt
>= N_TTY_BUF_SIZE
)
1312 if (ldata
->canon_head
!= ldata
->read_head
)
1313 set_bit(TTY_PUSH
, &tty
->flags
);
1314 c
= __DISABLED_CHAR
;
1315 goto handle_newline
;
1317 if ((c
== EOL_CHAR(tty
)) ||
1318 (c
== EOL2_CHAR(tty
) && L_IEXTEN(tty
))) {
1319 parmrk
= (c
== (unsigned char) '\377' && I_PARMRK(tty
))
1321 if (ldata
->read_cnt
>= (N_TTY_BUF_SIZE
- parmrk
)) {
1323 process_output('\a', tty
);
1327 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1330 /* Record the column of first canon char. */
1331 if (ldata
->canon_head
== ldata
->read_head
)
1332 echo_set_canon_col(ldata
);
1334 process_echoes(tty
);
1337 * XXX does PARMRK doubling happen for
1338 * EOL_CHAR and EOL2_CHAR?
1341 put_tty_queue(c
, ldata
);
1344 spin_lock_irqsave(&ldata
->read_lock
, flags
);
1345 set_bit(ldata
->read_head
, ldata
->read_flags
);
1346 put_tty_queue_nolock(c
, ldata
);
1347 ldata
->canon_head
= ldata
->read_head
;
1348 ldata
->canon_data
++;
1349 spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
1350 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
1351 if (waitqueue_active(&tty
->read_wait
))
1352 wake_up_interruptible(&tty
->read_wait
);
1357 parmrk
= (c
== (unsigned char) '\377' && I_PARMRK(tty
)) ? 1 : 0;
1358 if (ldata
->read_cnt
>= (N_TTY_BUF_SIZE
- parmrk
- 1)) {
1359 /* beep if no space */
1361 process_output('\a', tty
);
1365 finish_erasing(ldata
);
1367 echo_char_raw('\n', ldata
);
1369 /* Record the column of first canon char. */
1370 if (ldata
->canon_head
== ldata
->read_head
)
1371 echo_set_canon_col(ldata
);
1374 process_echoes(tty
);
1378 put_tty_queue(c
, ldata
);
1380 put_tty_queue(c
, ldata
);
1385 * n_tty_write_wakeup - asynchronous I/O notifier
1388 * Required for the ptys, serial driver etc. since processes
1389 * that attach themselves to the master and rely on ASYNC
1390 * IO must be woken up
1393 static void n_tty_write_wakeup(struct tty_struct
*tty
)
1395 if (tty
->fasync
&& test_and_clear_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
))
1396 kill_fasync(&tty
->fasync
, SIGIO
, POLL_OUT
);
1400 * n_tty_receive_buf - data receive
1401 * @tty: terminal device
1404 * @count: characters
1406 * Called by the terminal driver when a block of characters has
1407 * been received. This function must be called from soft contexts
1408 * not from interrupt context. The driver is responsible for making
1409 * calls one at a time and in order (or using flush_to_ldisc)
1412 static void n_tty_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
1413 char *fp
, int count
)
1415 struct n_tty_data
*ldata
= tty
->disc_data
;
1416 const unsigned char *p
;
1417 char *f
, flags
= TTY_NORMAL
;
1420 unsigned long cpuflags
;
1422 if (ldata
->real_raw
) {
1423 spin_lock_irqsave(&ldata
->read_lock
, cpuflags
);
1424 i
= min(N_TTY_BUF_SIZE
- ldata
->read_cnt
,
1425 N_TTY_BUF_SIZE
- ldata
->read_head
);
1427 memcpy(ldata
->read_buf
+ ldata
->read_head
, cp
, i
);
1428 ldata
->read_head
= (ldata
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
1429 ldata
->read_cnt
+= i
;
1433 i
= min(N_TTY_BUF_SIZE
- ldata
->read_cnt
,
1434 N_TTY_BUF_SIZE
- ldata
->read_head
);
1436 memcpy(ldata
->read_buf
+ ldata
->read_head
, cp
, i
);
1437 ldata
->read_head
= (ldata
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
1438 ldata
->read_cnt
+= i
;
1439 spin_unlock_irqrestore(&ldata
->read_lock
, cpuflags
);
1441 for (i
= count
, p
= cp
, f
= fp
; i
; i
--, p
++) {
1446 n_tty_receive_char(tty
, *p
);
1449 n_tty_receive_break(tty
);
1453 n_tty_receive_parity_error(tty
, *p
);
1456 n_tty_receive_overrun(tty
);
1459 printk(KERN_ERR
"%s: unknown flag %d\n",
1460 tty_name(tty
, buf
), flags
);
1464 if (tty
->ops
->flush_chars
)
1465 tty
->ops
->flush_chars(tty
);
1468 n_tty_set_room(tty
);
1470 if ((!ldata
->icanon
&& (ldata
->read_cnt
>= tty
->minimum_to_wake
)) ||
1472 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
1473 if (waitqueue_active(&tty
->read_wait
))
1474 wake_up_interruptible(&tty
->read_wait
);
1478 * Check the remaining room for the input canonicalization
1479 * mode. We don't want to throttle the driver if we're in
1480 * canonical mode and don't have a newline yet!
1482 if (tty
->receive_room
< TTY_THRESHOLD_THROTTLE
)
1485 /* FIXME: there is a tiny race here if the receive room check runs
1486 before the other work executes and empties the buffer (upping
1487 the receiving room and unthrottling. We then throttle and get
1488 stuck. This has been observed and traced down by Vincent Pillet/
1489 We need to address this when we sort out out the rx path locking */
1492 int is_ignored(int sig
)
1494 return (sigismember(¤t
->blocked
, sig
) ||
1495 current
->sighand
->action
[sig
-1].sa
.sa_handler
== SIG_IGN
);
1499 * n_tty_set_termios - termios data changed
1501 * @old: previous data
1503 * Called by the tty layer when the user changes termios flags so
1504 * that the line discipline can plan ahead. This function cannot sleep
1505 * and is protected from re-entry by the tty layer. The user is
1506 * guaranteed that this function will not be re-entered or in progress
1507 * when the ldisc is closed.
1509 * Locking: Caller holds tty->termios_mutex
1512 static void n_tty_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
1514 struct n_tty_data
*ldata
= tty
->disc_data
;
1515 int canon_change
= 1;
1518 canon_change
= (old
->c_lflag
^ tty
->termios
.c_lflag
) & ICANON
;
1520 bitmap_zero(ldata
->read_flags
, N_TTY_BUF_SIZE
);
1521 ldata
->canon_head
= ldata
->read_tail
;
1522 ldata
->canon_data
= 0;
1526 if (canon_change
&& !L_ICANON(tty
) && ldata
->read_cnt
)
1527 wake_up_interruptible(&tty
->read_wait
);
1529 ldata
->icanon
= (L_ICANON(tty
) != 0);
1530 if (test_bit(TTY_HW_COOK_IN
, &tty
->flags
)) {
1532 ldata
->real_raw
= 1;
1533 n_tty_set_room(tty
);
1536 if (I_ISTRIP(tty
) || I_IUCLC(tty
) || I_IGNCR(tty
) ||
1537 I_ICRNL(tty
) || I_INLCR(tty
) || L_ICANON(tty
) ||
1538 I_IXON(tty
) || L_ISIG(tty
) || L_ECHO(tty
) ||
1540 bitmap_zero(ldata
->process_char_map
, 256);
1542 if (I_IGNCR(tty
) || I_ICRNL(tty
))
1543 set_bit('\r', ldata
->process_char_map
);
1545 set_bit('\n', ldata
->process_char_map
);
1547 if (L_ICANON(tty
)) {
1548 set_bit(ERASE_CHAR(tty
), ldata
->process_char_map
);
1549 set_bit(KILL_CHAR(tty
), ldata
->process_char_map
);
1550 set_bit(EOF_CHAR(tty
), ldata
->process_char_map
);
1551 set_bit('\n', ldata
->process_char_map
);
1552 set_bit(EOL_CHAR(tty
), ldata
->process_char_map
);
1553 if (L_IEXTEN(tty
)) {
1554 set_bit(WERASE_CHAR(tty
),
1555 ldata
->process_char_map
);
1556 set_bit(LNEXT_CHAR(tty
),
1557 ldata
->process_char_map
);
1558 set_bit(EOL2_CHAR(tty
),
1559 ldata
->process_char_map
);
1561 set_bit(REPRINT_CHAR(tty
),
1562 ldata
->process_char_map
);
1566 set_bit(START_CHAR(tty
), ldata
->process_char_map
);
1567 set_bit(STOP_CHAR(tty
), ldata
->process_char_map
);
1570 set_bit(INTR_CHAR(tty
), ldata
->process_char_map
);
1571 set_bit(QUIT_CHAR(tty
), ldata
->process_char_map
);
1572 set_bit(SUSP_CHAR(tty
), ldata
->process_char_map
);
1574 clear_bit(__DISABLED_CHAR
, ldata
->process_char_map
);
1576 ldata
->real_raw
= 0;
1579 if ((I_IGNBRK(tty
) || (!I_BRKINT(tty
) && !I_PARMRK(tty
))) &&
1580 (I_IGNPAR(tty
) || !I_INPCK(tty
)) &&
1581 (tty
->driver
->flags
& TTY_DRIVER_REAL_RAW
))
1582 ldata
->real_raw
= 1;
1584 ldata
->real_raw
= 0;
1586 n_tty_set_room(tty
);
1587 /* The termios change make the tty ready for I/O */
1588 wake_up_interruptible(&tty
->write_wait
);
1589 wake_up_interruptible(&tty
->read_wait
);
1593 * n_tty_close - close the ldisc for this tty
1596 * Called from the terminal layer when this line discipline is
1597 * being shut down, either because of a close or becsuse of a
1598 * discipline change. The function will not be called while other
1599 * ldisc methods are in progress.
1602 static void n_tty_close(struct tty_struct
*tty
)
1604 struct n_tty_data
*ldata
= tty
->disc_data
;
1606 n_tty_flush_buffer(tty
);
1607 kfree(ldata
->read_buf
);
1608 kfree(ldata
->echo_buf
);
1610 tty
->disc_data
= NULL
;
1614 * n_tty_open - open an ldisc
1615 * @tty: terminal to open
1617 * Called when this line discipline is being attached to the
1618 * terminal device. Can sleep. Called serialized so that no
1619 * other events will occur in parallel. No further open will occur
1623 static int n_tty_open(struct tty_struct
*tty
)
1625 struct n_tty_data
*ldata
;
1627 ldata
= kzalloc(sizeof(*ldata
), GFP_KERNEL
);
1631 ldata
->overrun_time
= jiffies
;
1632 mutex_init(&ldata
->atomic_read_lock
);
1633 mutex_init(&ldata
->output_lock
);
1634 mutex_init(&ldata
->echo_lock
);
1635 spin_lock_init(&ldata
->read_lock
);
1637 /* These are ugly. Currently a malloc failure here can panic */
1638 ldata
->read_buf
= kzalloc(N_TTY_BUF_SIZE
, GFP_KERNEL
);
1639 ldata
->echo_buf
= kzalloc(N_TTY_BUF_SIZE
, GFP_KERNEL
);
1640 if (!ldata
->read_buf
|| !ldata
->echo_buf
)
1643 tty
->disc_data
= ldata
;
1644 reset_buffer_flags(tty
);
1645 tty_unthrottle(tty
);
1647 n_tty_set_termios(tty
, NULL
);
1648 tty
->minimum_to_wake
= 1;
1653 kfree(ldata
->read_buf
);
1654 kfree(ldata
->echo_buf
);
1660 static inline int input_available_p(struct tty_struct
*tty
, int amt
)
1662 struct n_tty_data
*ldata
= tty
->disc_data
;
1664 tty_flush_to_ldisc(tty
);
1665 if (ldata
->icanon
&& !L_EXTPROC(tty
)) {
1666 if (ldata
->canon_data
)
1668 } else if (ldata
->read_cnt
>= (amt
? amt
: 1))
1675 * copy_from_read_buf - copy read data directly
1676 * @tty: terminal device
1680 * Helper function to speed up n_tty_read. It is only called when
1681 * ICANON is off; it copies characters straight from the tty queue to
1682 * user space directly. It can be profitably called twice; once to
1683 * drain the space from the tail pointer to the (physical) end of the
1684 * buffer, and once to drain the space from the (physical) beginning of
1685 * the buffer to head pointer.
1687 * Called under the ldata->atomic_read_lock sem
1691 static int copy_from_read_buf(struct tty_struct
*tty
,
1692 unsigned char __user
**b
,
1696 struct n_tty_data
*ldata
= tty
->disc_data
;
1699 unsigned long flags
;
1703 spin_lock_irqsave(&ldata
->read_lock
, flags
);
1704 n
= min(ldata
->read_cnt
, N_TTY_BUF_SIZE
- ldata
->read_tail
);
1706 spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
1708 retval
= copy_to_user(*b
, &ldata
->read_buf
[ldata
->read_tail
], n
);
1711 ldata
->read_buf
[ldata
->read_tail
] == EOF_CHAR(tty
);
1712 tty_audit_add_data(tty
, &ldata
->read_buf
[ldata
->read_tail
], n
,
1714 spin_lock_irqsave(&ldata
->read_lock
, flags
);
1715 ldata
->read_tail
= (ldata
->read_tail
+ n
) & (N_TTY_BUF_SIZE
-1);
1716 ldata
->read_cnt
-= n
;
1717 /* Turn single EOF into zero-length read */
1718 if (L_EXTPROC(tty
) && ldata
->icanon
&& is_eof
&& !ldata
->read_cnt
)
1720 spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
1727 extern ssize_t
redirected_tty_write(struct file
*, const char __user
*,
1731 * job_control - check job control
1733 * @file: file handle
1735 * Perform job control management checks on this file/tty descriptor
1736 * and if appropriate send any needed signals and return a negative
1737 * error code if action should be taken.
1740 * Locking: None - redirected write test is safe, testing
1741 * current->signal should possibly lock current->sighand
1745 static int job_control(struct tty_struct
*tty
, struct file
*file
)
1747 /* Job control check -- must be done at start and after
1748 every sleep (POSIX.1 7.1.1.4). */
1749 /* NOTE: not yet done after every sleep pending a thorough
1750 check of the logic of this change. -- jlc */
1751 /* don't stop on /dev/console */
1752 if (file
->f_op
->write
!= redirected_tty_write
&&
1753 current
->signal
->tty
== tty
) {
1755 printk(KERN_ERR
"n_tty_read: no tty->pgrp!\n");
1756 else if (task_pgrp(current
) != tty
->pgrp
) {
1757 if (is_ignored(SIGTTIN
) ||
1758 is_current_pgrp_orphaned())
1760 kill_pgrp(task_pgrp(current
), SIGTTIN
, 1);
1761 set_thread_flag(TIF_SIGPENDING
);
1762 return -ERESTARTSYS
;
1770 * n_tty_read - read function for tty
1772 * @file: file object
1773 * @buf: userspace buffer pointer
1776 * Perform reads for the line discipline. We are guaranteed that the
1777 * line discipline will not be closed under us but we may get multiple
1778 * parallel readers and must handle this ourselves. We may also get
1779 * a hangup. Always called in user context, may sleep.
1781 * This code must be sure never to sleep through a hangup.
1784 static ssize_t
n_tty_read(struct tty_struct
*tty
, struct file
*file
,
1785 unsigned char __user
*buf
, size_t nr
)
1787 struct n_tty_data
*ldata
= tty
->disc_data
;
1788 unsigned char __user
*b
= buf
;
1789 DECLARE_WAITQUEUE(wait
, current
);
1795 unsigned long flags
;
1799 c
= job_control(tty
, file
);
1804 timeout
= MAX_SCHEDULE_TIMEOUT
;
1805 if (!ldata
->icanon
) {
1806 time
= (HZ
/ 10) * TIME_CHAR(tty
);
1807 minimum
= MIN_CHAR(tty
);
1810 tty
->minimum_to_wake
= 1;
1811 else if (!waitqueue_active(&tty
->read_wait
) ||
1812 (tty
->minimum_to_wake
> minimum
))
1813 tty
->minimum_to_wake
= minimum
;
1820 tty
->minimum_to_wake
= minimum
= 1;
1825 * Internal serialization of reads.
1827 if (file
->f_flags
& O_NONBLOCK
) {
1828 if (!mutex_trylock(&ldata
->atomic_read_lock
))
1831 if (mutex_lock_interruptible(&ldata
->atomic_read_lock
))
1832 return -ERESTARTSYS
;
1834 packet
= tty
->packet
;
1836 add_wait_queue(&tty
->read_wait
, &wait
);
1838 /* First test for status change. */
1839 if (packet
&& tty
->link
->ctrl_status
) {
1843 spin_lock_irqsave(&tty
->link
->ctrl_lock
, flags
);
1844 cs
= tty
->link
->ctrl_status
;
1845 tty
->link
->ctrl_status
= 0;
1846 spin_unlock_irqrestore(&tty
->link
->ctrl_lock
, flags
);
1847 if (tty_put_user(tty
, cs
, b
++)) {
1855 /* This statement must be first before checking for input
1856 so that any interrupt will set the state back to
1858 set_current_state(TASK_INTERRUPTIBLE
);
1860 if (((minimum
- (b
- buf
)) < tty
->minimum_to_wake
) &&
1861 ((minimum
- (b
- buf
)) >= 1))
1862 tty
->minimum_to_wake
= (minimum
- (b
- buf
));
1864 if (!input_available_p(tty
, 0)) {
1865 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
)) {
1869 if (tty_hung_up_p(file
))
1873 if (file
->f_flags
& O_NONBLOCK
) {
1877 if (signal_pending(current
)) {
1878 retval
= -ERESTARTSYS
;
1881 /* FIXME: does n_tty_set_room need locking ? */
1882 n_tty_set_room(tty
);
1883 timeout
= schedule_timeout(timeout
);
1886 __set_current_state(TASK_RUNNING
);
1888 /* Deal with packet mode. */
1889 if (packet
&& b
== buf
) {
1890 if (tty_put_user(tty
, TIOCPKT_DATA
, b
++)) {
1898 if (ldata
->icanon
&& !L_EXTPROC(tty
)) {
1899 /* N.B. avoid overrun if nr == 0 */
1900 spin_lock_irqsave(&ldata
->read_lock
, flags
);
1901 while (nr
&& ldata
->read_cnt
) {
1904 eol
= test_and_clear_bit(ldata
->read_tail
,
1906 c
= ldata
->read_buf
[ldata
->read_tail
];
1907 ldata
->read_tail
= ((ldata
->read_tail
+1) &
1908 (N_TTY_BUF_SIZE
-1));
1911 /* this test should be redundant:
1912 * we shouldn't be reading data if
1915 if (--ldata
->canon_data
< 0)
1916 ldata
->canon_data
= 0;
1918 spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
1920 if (!eol
|| (c
!= __DISABLED_CHAR
)) {
1921 if (tty_put_user(tty
, c
, b
++)) {
1924 spin_lock_irqsave(&ldata
->read_lock
, flags
);
1930 tty_audit_push(tty
);
1931 spin_lock_irqsave(&ldata
->read_lock
, flags
);
1934 spin_lock_irqsave(&ldata
->read_lock
, flags
);
1936 spin_unlock_irqrestore(&ldata
->read_lock
, flags
);
1941 /* The copy function takes the read lock and handles
1942 locking internally for this case */
1943 uncopied
= copy_from_read_buf(tty
, &b
, &nr
);
1944 uncopied
+= copy_from_read_buf(tty
, &b
, &nr
);
1951 /* If there is enough space in the read buffer now, let the
1952 * low-level driver know. We use n_tty_chars_in_buffer() to
1953 * check the buffer, as it now knows about canonical mode.
1954 * Otherwise, if the driver is throttled and the line is
1955 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1956 * we won't get any more characters.
1958 if (n_tty_chars_in_buffer(tty
) <= TTY_THRESHOLD_UNTHROTTLE
) {
1959 n_tty_set_room(tty
);
1960 check_unthrottle(tty
);
1963 if (b
- buf
>= minimum
)
1968 mutex_unlock(&ldata
->atomic_read_lock
);
1969 remove_wait_queue(&tty
->read_wait
, &wait
);
1971 if (!waitqueue_active(&tty
->read_wait
))
1972 tty
->minimum_to_wake
= minimum
;
1974 __set_current_state(TASK_RUNNING
);
1979 clear_bit(TTY_PUSH
, &tty
->flags
);
1980 } else if (test_and_clear_bit(TTY_PUSH
, &tty
->flags
))
1983 n_tty_set_room(tty
);
1988 * n_tty_write - write function for tty
1990 * @file: file object
1991 * @buf: userspace buffer pointer
1994 * Write function of the terminal device. This is serialized with
1995 * respect to other write callers but not to termios changes, reads
1996 * and other such events. Since the receive code will echo characters,
1997 * thus calling driver write methods, the output_lock is used in
1998 * the output processing functions called here as well as in the
1999 * echo processing function to protect the column state and space
2000 * left in the buffer.
2002 * This code must be sure never to sleep through a hangup.
2004 * Locking: output_lock to protect column state and space left
2005 * (note that the process_output*() functions take this
2009 static ssize_t
n_tty_write(struct tty_struct
*tty
, struct file
*file
,
2010 const unsigned char *buf
, size_t nr
)
2012 const unsigned char *b
= buf
;
2013 DECLARE_WAITQUEUE(wait
, current
);
2017 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2018 if (L_TOSTOP(tty
) && file
->f_op
->write
!= redirected_tty_write
) {
2019 retval
= tty_check_change(tty
);
2024 /* Write out any echoed characters that are still pending */
2025 process_echoes(tty
);
2027 add_wait_queue(&tty
->write_wait
, &wait
);
2029 set_current_state(TASK_INTERRUPTIBLE
);
2030 if (signal_pending(current
)) {
2031 retval
= -ERESTARTSYS
;
2034 if (tty_hung_up_p(file
) || (tty
->link
&& !tty
->link
->count
)) {
2038 if (O_OPOST(tty
) && !(test_bit(TTY_HW_COOK_OUT
, &tty
->flags
))) {
2040 ssize_t num
= process_output_block(tty
, b
, nr
);
2052 if (process_output(c
, tty
) < 0)
2056 if (tty
->ops
->flush_chars
)
2057 tty
->ops
->flush_chars(tty
);
2060 c
= tty
->ops
->write(tty
, b
, nr
);
2073 if (file
->f_flags
& O_NONBLOCK
) {
2080 __set_current_state(TASK_RUNNING
);
2081 remove_wait_queue(&tty
->write_wait
, &wait
);
2082 if (b
- buf
!= nr
&& tty
->fasync
)
2083 set_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
2084 return (b
- buf
) ? b
- buf
: retval
;
2088 * n_tty_poll - poll method for N_TTY
2089 * @tty: terminal device
2090 * @file: file accessing it
2093 * Called when the line discipline is asked to poll() for data or
2094 * for special events. This code is not serialized with respect to
2095 * other events save open/close.
2097 * This code must be sure never to sleep through a hangup.
2098 * Called without the kernel lock held - fine
2101 static unsigned int n_tty_poll(struct tty_struct
*tty
, struct file
*file
,
2104 unsigned int mask
= 0;
2106 poll_wait(file
, &tty
->read_wait
, wait
);
2107 poll_wait(file
, &tty
->write_wait
, wait
);
2108 if (input_available_p(tty
, TIME_CHAR(tty
) ? 0 : MIN_CHAR(tty
)))
2109 mask
|= POLLIN
| POLLRDNORM
;
2110 if (tty
->packet
&& tty
->link
->ctrl_status
)
2111 mask
|= POLLPRI
| POLLIN
| POLLRDNORM
;
2112 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
))
2114 if (tty_hung_up_p(file
))
2116 if (!(mask
& (POLLHUP
| POLLIN
| POLLRDNORM
))) {
2117 if (MIN_CHAR(tty
) && !TIME_CHAR(tty
))
2118 tty
->minimum_to_wake
= MIN_CHAR(tty
);
2120 tty
->minimum_to_wake
= 1;
2122 if (tty
->ops
->write
&& !tty_is_writelocked(tty
) &&
2123 tty_chars_in_buffer(tty
) < WAKEUP_CHARS
&&
2124 tty_write_room(tty
) > 0)
2125 mask
|= POLLOUT
| POLLWRNORM
;
2129 static unsigned long inq_canon(struct n_tty_data
*ldata
)
2133 if (!ldata
->canon_data
)
2135 head
= ldata
->canon_head
;
2136 tail
= ldata
->read_tail
;
2137 nr
= (head
- tail
) & (N_TTY_BUF_SIZE
-1);
2138 /* Skip EOF-chars.. */
2139 while (head
!= tail
) {
2140 if (test_bit(tail
, ldata
->read_flags
) &&
2141 ldata
->read_buf
[tail
] == __DISABLED_CHAR
)
2143 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
2148 static int n_tty_ioctl(struct tty_struct
*tty
, struct file
*file
,
2149 unsigned int cmd
, unsigned long arg
)
2151 struct n_tty_data
*ldata
= tty
->disc_data
;
2156 return put_user(tty_chars_in_buffer(tty
), (int __user
*) arg
);
2158 /* FIXME: Locking */
2159 retval
= ldata
->read_cnt
;
2161 retval
= inq_canon(ldata
);
2162 return put_user(retval
, (unsigned int __user
*) arg
);
2164 return n_tty_ioctl_helper(tty
, file
, cmd
, arg
);
2168 struct tty_ldisc_ops tty_ldisc_N_TTY
= {
2169 .magic
= TTY_LDISC_MAGIC
,
2172 .close
= n_tty_close
,
2173 .flush_buffer
= n_tty_flush_buffer
,
2174 .chars_in_buffer
= n_tty_chars_in_buffer
,
2176 .write
= n_tty_write
,
2177 .ioctl
= n_tty_ioctl
,
2178 .set_termios
= n_tty_set_termios
,
2180 .receive_buf
= n_tty_receive_buf
,
2181 .write_wakeup
= n_tty_write_wakeup
2185 * n_tty_inherit_ops - inherit N_TTY methods
2186 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2188 * Used by a generic struct tty_ldisc_ops to easily inherit N_TTY
2192 void n_tty_inherit_ops(struct tty_ldisc_ops
*ops
)
2194 *ops
= tty_ldisc_N_TTY
;
2196 ops
->refcount
= ops
->flags
= 0;
2198 EXPORT_SYMBOL_GPL(n_tty_inherit_ops
);