33 #include <linux/types.h>
35 #include <linux/errno.h>
36 #include <linux/signal.h>
37 #include <linux/fcntl.h>
38 #include <linux/sched.h>
40 #include <linux/tty.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>
51 #include <linux/module.h>
55 #define WAKEUP_CHARS 256
62 #define TTY_THRESHOLD_THROTTLE 128
63 #define TTY_THRESHOLD_UNTHROTTLE 128
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
76 static inline int tty_put_user(
struct tty_struct *tty,
unsigned char x,
77 unsigned char __user *
ptr)
93 static void n_tty_set_room(
struct tty_struct *tty)
119 if (left && !old_left)
123 static void put_tty_queue_nolock(
unsigned char c,
struct tty_struct *tty)
142 static void put_tty_queue(
unsigned char c,
struct tty_struct *tty)
150 put_tty_queue_nolock(c, tty);
151 spin_unlock_irqrestore(&tty->
read_lock, flags);
163 static void check_unthrottle(
struct tty_struct *tty)
180 static void reset_buffer_flags(
struct tty_struct *tty)
186 spin_unlock_irqrestore(&tty->
read_lock, flags);
209 static void n_tty_flush_buffer(
struct tty_struct *tty)
213 reset_buffer_flags(tty);
219 if (tty->
link->packet) {
223 spin_unlock_irqrestore(&tty->
ctrl_lock, flags);
249 spin_unlock_irqrestore(&tty->
read_lock, flags);
262 static inline int is_utf8_continuation(
unsigned char c)
264 return (c & 0xc0) == 0x80;
275 static inline int is_continuation(
unsigned char c,
struct tty_struct *tty)
277 return I_IUTF8(tty) && is_utf8_continuation(c);
302 static int do_output_char(
unsigned char c,
struct tty_struct *tty,
int space)
317 tty->
ops->write(tty,
"\r\n", 2);
334 spaces = 8 - (tty->
column & 7);
339 tty->
ops->write(tty,
" ", spaces);
352 if (!is_continuation(c, tty))
376 static int process_output(
unsigned char c,
struct tty_struct *tty)
383 retval = do_output_char(c, tty, space);
412 const unsigned char *
buf,
unsigned int nr)
416 const unsigned char *
cp;
428 for (i = 0, cp = buf; i <
nr; i++, cp++) {
429 unsigned char c = *
cp;
456 if (!is_continuation(c, tty))
463 i = tty->
ops->write(tty, buf, i);
495 static void process_echoes(
struct tty_struct *tty)
499 unsigned char *
cp, *buf_end;
517 int no_space_left = 0;
530 unsigned int num_chars, num_bs;
533 if (++opp == buf_end)
547 if (!(num_chars & 0x80))
549 num_bs = 8 - (num_chars & 7);
551 if (num_bs > space) {
618 int retval = do_output_char(c, tty, space);
646 if (num_processed > 0)
653 if (tty->
ops->flush_chars)
654 tty->
ops->flush_chars(tty);
667 static void add_echo_byte(
unsigned char c,
struct tty_struct *tty)
713 static void echo_move_back_col(
struct tty_struct *tty)
733 static void echo_set_canon_col(
struct tty_struct *tty)
760 static void echo_erase_tab(
unsigned int num_chars,
int after_tab,
775 add_echo_byte(num_chars, tty);
793 static void echo_char_raw(
unsigned char c,
struct tty_struct *tty)
801 add_echo_byte(c, tty);
821 static void echo_char(
unsigned char c,
struct tty_struct *tty)
831 add_echo_byte(c, tty);
842 static inline void finish_erasing(
struct tty_struct *tty)
845 echo_char_raw(
'/', tty);
862 static void eraser(
unsigned char c,
struct tty_struct *tty)
864 enum {
ERASE, WERASE, KILL } kill_type;
883 spin_unlock_irqrestore(&tty->
read_lock, flags);
891 spin_unlock_irqrestore(&tty->
read_lock, flags);
896 echo_char_raw(
'\n', tty);
911 }
while (is_continuation(c, tty) && head != tty->
canon_head);
914 if (is_continuation(c, tty))
917 if (kill_type == WERASE) {
921 else if (seen_alnums)
928 spin_unlock_irqrestore(&tty->
read_lock, flags);
932 echo_char_raw(
'\\', tty);
939 echo_char_raw(tty->
read_buf[head], tty);
940 echo_move_back_col(tty);
944 }
else if (c ==
'\t') {
945 unsigned int num_chars = 0;
965 }
else if (!is_continuation(c, tty)) {
969 echo_erase_tab(num_chars, after_tab, tty);
972 echo_char_raw(
'\b', tty);
973 echo_char_raw(
' ', tty);
974 echo_char_raw(
'\b', tty);
977 echo_char_raw(
'\b', tty);
978 echo_char_raw(
' ', tty);
979 echo_char_raw(
'\b', tty);
983 if (kill_type ==
ERASE)
1004 static inline void isig(
int sig,
struct tty_struct *tty,
int flush)
1009 n_tty_flush_buffer(tty);
1024 static inline void n_tty_receive_break(
struct tty_struct *tty)
1033 put_tty_queue(
'\377', tty);
1034 put_tty_queue(
'\0', tty);
1036 put_tty_queue(
'\0', tty);
1053 static inline void n_tty_receive_overrun(
struct tty_struct *tty)
1076 static inline void n_tty_receive_parity_error(
struct tty_struct *tty,
1082 put_tty_queue(
'\377', tty);
1083 put_tty_queue(
'\0', tty);
1084 put_tty_queue(c, tty);
1086 put_tty_queue(
'\0', tty);
1088 put_tty_queue(c, tty);
1102 static inline void n_tty_receive_char(
struct tty_struct *tty,
unsigned char c)
1104 unsigned long flags;
1108 put_tty_queue(c, tty);
1118 put_tty_queue(c, tty);
1126 process_echoes(tty);
1133 process_echoes(tty);
1148 parmrk = (c == (
unsigned char)
'\377' &&
I_PARMRK(tty)) ? 1 : 0;
1152 process_output(
'\a', tty);
1156 finish_erasing(tty);
1159 echo_set_canon_col(tty);
1161 process_echoes(tty);
1164 put_tty_queue(c, tty);
1165 put_tty_queue(c, tty);
1172 process_echoes(tty);
1198 n_tty_flush_buffer(tty);
1205 process_echoes(tty);
1218 }
else if (c ==
'\n' &&
I_INLCR(tty))
1225 process_echoes(tty);
1231 finish_erasing(tty);
1233 echo_char_raw(
'^', tty);
1234 echo_char_raw(
'\b', tty);
1235 process_echoes(tty);
1244 finish_erasing(tty);
1246 echo_char_raw(
'\n', tty);
1248 echo_char(tty->
read_buf[tail], tty);
1251 process_echoes(tty);
1257 process_output(
'\a', tty);
1261 echo_char_raw(
'\n', tty);
1262 process_echoes(tty);
1264 goto handle_newline;
1272 goto handle_newline;
1276 parmrk = (c == (
unsigned char)
'\377' &&
I_PARMRK(tty))
1280 process_output(
'\a', tty);
1289 echo_set_canon_col(tty);
1291 process_echoes(tty);
1298 put_tty_queue(c, tty);
1303 put_tty_queue_nolock(c, tty);
1306 spin_unlock_irqrestore(&tty->
read_lock, flags);
1314 parmrk = (c == (
unsigned char)
'\377' &&
I_PARMRK(tty)) ? 1 : 0;
1318 process_output(
'\a', tty);
1322 finish_erasing(tty);
1324 echo_char_raw(
'\n', tty);
1328 echo_set_canon_col(tty);
1331 process_echoes(tty);
1335 put_tty_queue(c, tty);
1337 put_tty_queue(c, tty);
1350 static void n_tty_write_wakeup(
struct tty_struct *tty)
1369 static void n_tty_receive_buf(
struct tty_struct *tty,
const unsigned char *cp,
1372 const unsigned char *
p;
1398 spin_unlock_irqrestore(&tty->
read_lock, cpuflags);
1400 for (i = count, p = cp, f = fp;
i; i--, p++) {
1405 n_tty_receive_char(tty, *p);
1408 n_tty_receive_break(tty);
1412 n_tty_receive_parity_error(tty, *p);
1415 n_tty_receive_overrun(tty);
1423 if (tty->
ops->flush_chars)
1424 tty->
ops->flush_chars(tty);
1427 n_tty_set_room(tty);
1473 int canon_change = 1;
1477 canon_change = (old->
c_lflag ^ tty->termios.c_lflag) &
ICANON;
1492 n_tty_set_room(tty);
1545 n_tty_set_room(tty);
1561 static void n_tty_close(
struct tty_struct *tty)
1563 n_tty_flush_buffer(tty);
1584 static int n_tty_open(
struct tty_struct *tty)
1601 reset_buffer_flags(tty);
1604 n_tty_set_termios(tty,
NULL);
1610 static inline int input_available_p(
struct tty_struct *tty,
int amt)
1616 }
else if (tty->
read_cnt >= (amt ? amt : 1))
1639 static int copy_from_read_buf(
struct tty_struct *tty,
1640 unsigned char __user **
b,
1646 unsigned long flags;
1653 spin_unlock_irqrestore(&tty->
read_lock, flags);
1666 spin_unlock_irqrestore(&tty->
read_lock, flags);
1699 current->signal->tty == tty) {
1731 unsigned char __user *buf,
size_t nr)
1740 unsigned long flags;
1748 c = job_control(tty, file);
1760 else if (!waitqueue_active(&tty->
read_wait) ||
1788 if (packet && tty->
link->ctrl_status) {
1793 cs = tty->
link->ctrl_status;
1794 tty->
link->ctrl_status = 0;
1795 spin_unlock_irqrestore(&tty->
link->ctrl_lock, flags);
1796 if (tty_put_user(tty, cs, b++)) {
1810 ((minimum - (b - buf)) >= 1))
1813 if (!input_available_p(tty, 0)) {
1826 if (signal_pending(
current)) {
1831 n_tty_set_room(tty);
1839 if (packet && b == buf) {
1868 spin_unlock_irqrestore(&tty->
read_lock, flags);
1871 if (tty_put_user(tty, c, b++)) {
1886 spin_unlock_irqrestore(&tty->
read_lock, flags);
1893 uncopied = copy_from_read_buf(tty, &b, &nr);
1894 uncopied += copy_from_read_buf(tty, &b, &nr);
1909 n_tty_set_room(tty);
1910 check_unthrottle(tty);
1913 if (b - buf >= minimum)
1933 n_tty_set_room(tty);
1960 const unsigned char *buf,
size_t nr)
1962 const unsigned char *b =
buf;
1975 process_echoes(tty);
1980 if (signal_pending(
current)) {
1990 ssize_t num = process_output_block(tty, b, nr);
2002 if (process_output(c, tty) < 0)
2006 if (tty->
ops->flush_chars)
2007 tty->
ops->flush_chars(tty);
2010 c = tty->
ops->write(tty, b, nr);
2032 if (b - buf != nr && tty->
fasync)
2034 return (b - buf) ? b - buf :
retval;
2051 static unsigned int n_tty_poll(
struct tty_struct *tty,
struct file *file,
2054 unsigned int mask = 0;
2079 static unsigned long inq_canon(
struct tty_struct *tty)
2089 while (head != tail) {
2098 static int n_tty_ioctl(
struct tty_struct *tty,
struct file *file,
2099 unsigned int cmd,
unsigned long arg)
2110 retval = inq_canon(tty);
2111 return put_user(retval, (
unsigned int __user *) arg);
2121 .close = n_tty_close,
2122 .flush_buffer = n_tty_flush_buffer,
2123 .chars_in_buffer = n_tty_chars_in_buffer,
2125 .write = n_tty_write,
2126 .ioctl = n_tty_ioctl,
2127 .set_termios = n_tty_set_termios,
2129 .receive_buf = n_tty_receive_buf,
2130 .write_wakeup = n_tty_write_wakeup