Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * n_tty.c --- implements the N_TTY line discipline. |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
| 7 | * anyway...) |
| 8 | * |
| 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 |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 11 | * to N_TTY if it can not switch to any other line discipline. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * |
| 13 | * Written by Theodore Ts'o, Copyright 1994. |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 14 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * This file also contains code originally written by Linus Torvalds, |
| 16 | * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994. |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 17 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * This file may be redistributed under the terms of the GNU General Public |
| 19 | * License. |
| 20 | * |
| 21 | * Reduced memory usage for older ARM systems - Russell King. |
| 22 | * |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 23 | * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu> |
| 25 | * who actually finally proved there really was a race. |
| 26 | * |
| 27 | * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to |
| 28 | * waiting writing processes-Sapan Bhatia <sapan@corewars.org>. |
Alan Cox | 11a96d1 | 2008-10-13 10:46:24 +0100 | [diff] [blame] | 29 | * Also fixed a bug in BLOCKING mode where n_tty_write returns |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | * EAGAIN |
| 31 | */ |
| 32 | |
| 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> |
| 43 | #include <linux/mm.h> |
| 44 | #include <linux/string.h> |
| 45 | #include <linux/slab.h> |
| 46 | #include <linux/poll.h> |
| 47 | #include <linux/bitops.h> |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 48 | #include <linux/audit.h> |
| 49 | #include <linux/file.h> |
Alan Cox | 300a620 | 2009-01-02 13:41:04 +0000 | [diff] [blame] | 50 | #include <linux/uaccess.h> |
Rodolfo Giometti | 572b9ad | 2010-03-10 15:23:46 -0800 | [diff] [blame] | 51 | #include <linux/module.h> |
George Spelvin | 593fb1ae4 | 2013-02-12 02:00:43 -0500 | [diff] [blame] | 52 | #include <linux/ratelimit.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | /* number of characters left in xmit buffer before select has we have room */ |
| 56 | #define WAKEUP_CHARS 256 |
| 57 | |
| 58 | /* |
| 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. |
| 62 | */ |
| 63 | #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */ |
Thorsten Wißmann | bbd2075 | 2011-12-08 17:47:33 +0100 | [diff] [blame] | 64 | #define TTY_THRESHOLD_UNTHROTTLE 128 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 66 | /* |
| 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 |
| 70 | * codes. |
| 71 | */ |
| 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 |
| 76 | |
Jiri Slaby | 70ece7a | 2012-10-18 22:26:38 +0200 | [diff] [blame] | 77 | struct n_tty_data { |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 78 | unsigned int column; |
| 79 | unsigned long overrun_time; |
| 80 | int num_overrun; |
| 81 | |
| 82 | unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1; |
| 83 | unsigned char echo_overrun:1; |
Jiri Slaby | 3fe780b | 2012-10-18 22:26:40 +0200 | [diff] [blame] | 84 | |
| 85 | DECLARE_BITMAP(process_char_map, 256); |
| 86 | DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 87 | |
| 88 | char *read_buf; |
| 89 | int read_head; |
| 90 | int read_tail; |
| 91 | int read_cnt; |
Peter Hurley | f6c8dbe | 2013-06-15 07:28:28 -0400 | [diff] [blame] | 92 | int minimum_to_wake; |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 93 | |
| 94 | unsigned char *echo_buf; |
| 95 | unsigned int echo_pos; |
| 96 | unsigned int echo_cnt; |
| 97 | |
| 98 | int canon_data; |
| 99 | unsigned long canon_head; |
| 100 | unsigned int canon_column; |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 101 | |
| 102 | struct mutex atomic_read_lock; |
| 103 | struct mutex output_lock; |
| 104 | struct mutex echo_lock; |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 105 | raw_spinlock_t read_lock; |
Jiri Slaby | 70ece7a | 2012-10-18 22:26:38 +0200 | [diff] [blame] | 106 | }; |
| 107 | |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 108 | static inline int tty_put_user(struct tty_struct *tty, unsigned char x, |
| 109 | unsigned char __user *ptr) |
| 110 | { |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 111 | struct n_tty_data *ldata = tty->disc_data; |
| 112 | |
| 113 | tty_audit_add_data(tty, &x, 1, ldata->icanon); |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 114 | return put_user(x, ptr); |
| 115 | } |
| 116 | |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 117 | /** |
Peter Hurley | b848305 | 2013-06-15 07:28:30 -0400 | [diff] [blame] | 118 | * n_tty_set_room - receive space |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 119 | * @tty: terminal |
| 120 | * |
Peter Hurley | 7879a9f | 2013-06-15 07:28:31 -0400 | [diff] [blame] | 121 | * Updates tty->receive_room to reflect the currently available space |
Peter Hurley | b848305 | 2013-06-15 07:28:30 -0400 | [diff] [blame] | 122 | * in the input buffer, and re-schedules the flip buffer work if space |
| 123 | * just became available. |
| 124 | * |
| 125 | * Locks: Concurrent update is protected with read_lock |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 126 | */ |
| 127 | |
Peter Hurley | 7879a9f | 2013-06-15 07:28:31 -0400 | [diff] [blame] | 128 | static int set_room(struct tty_struct *tty) |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 129 | { |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 130 | struct n_tty_data *ldata = tty->disc_data; |
Jaeden Amero | 090abf7 | 2012-07-27 08:43:11 -0500 | [diff] [blame] | 131 | int left; |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 132 | int old_left; |
Peter Hurley | b848305 | 2013-06-15 07:28:30 -0400 | [diff] [blame] | 133 | unsigned long flags; |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 134 | |
Peter Hurley | b848305 | 2013-06-15 07:28:30 -0400 | [diff] [blame] | 135 | raw_spin_lock_irqsave(&ldata->read_lock, flags); |
| 136 | |
Jaeden Amero | 090abf7 | 2012-07-27 08:43:11 -0500 | [diff] [blame] | 137 | if (I_PARMRK(tty)) { |
| 138 | /* Multiply read_cnt by 3, since each byte might take up to |
| 139 | * three times as many spaces when PARMRK is set (depending on |
| 140 | * its flags, e.g. parity error). */ |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 141 | left = N_TTY_BUF_SIZE - ldata->read_cnt * 3 - 1; |
Jaeden Amero | 090abf7 | 2012-07-27 08:43:11 -0500 | [diff] [blame] | 142 | } else |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 143 | left = N_TTY_BUF_SIZE - ldata->read_cnt - 1; |
Jaeden Amero | 090abf7 | 2012-07-27 08:43:11 -0500 | [diff] [blame] | 144 | |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 145 | /* |
| 146 | * If we are doing input canonicalization, and there are no |
| 147 | * pending newlines, let characters through without limit, so |
| 148 | * that erase characters will be handled. Other excess |
| 149 | * characters will be beeped. |
| 150 | */ |
| 151 | if (left <= 0) |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 152 | left = ldata->icanon && !ldata->canon_data; |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 153 | old_left = tty->receive_room; |
| 154 | tty->receive_room = left; |
| 155 | |
Peter Hurley | b848305 | 2013-06-15 07:28:30 -0400 | [diff] [blame] | 156 | raw_spin_unlock_irqrestore(&ldata->read_lock, flags); |
| 157 | |
Peter Hurley | 7879a9f | 2013-06-15 07:28:31 -0400 | [diff] [blame] | 158 | return left && !old_left; |
| 159 | } |
| 160 | |
| 161 | static void n_tty_set_room(struct tty_struct *tty) |
| 162 | { |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 163 | /* Did this open up the receive buffer? We may need to flip */ |
Peter Hurley | 7879a9f | 2013-06-15 07:28:31 -0400 | [diff] [blame] | 164 | if (set_room(tty)) { |
Jiri Slaby | ecbbfd4 | 2012-10-18 22:26:47 +0200 | [diff] [blame] | 165 | WARN_RATELIMIT(tty->port->itty == NULL, |
Sasha Levin | cadf748 | 2012-10-25 14:26:35 -0400 | [diff] [blame] | 166 | "scheduling with invalid itty\n"); |
Peter Hurley | 2162293 | 2013-03-11 16:44:21 -0400 | [diff] [blame] | 167 | /* see if ldisc has been killed - if so, this means that |
| 168 | * even though the ldisc has been halted and ->buf.work |
| 169 | * cancelled, ->buf.work is about to be rescheduled |
| 170 | */ |
| 171 | WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags), |
| 172 | "scheduling buffer work for halted ldisc\n"); |
Jiri Slaby | ecbbfd4 | 2012-10-18 22:26:47 +0200 | [diff] [blame] | 173 | schedule_work(&tty->port->buf.work); |
| 174 | } |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 175 | } |
| 176 | |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 177 | static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 179 | if (ldata->read_cnt < N_TTY_BUF_SIZE) { |
| 180 | ldata->read_buf[ldata->read_head] = c; |
| 181 | ldata->read_head = (ldata->read_head + 1) & (N_TTY_BUF_SIZE-1); |
| 182 | ldata->read_cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
Alan Cox | 17b8206 | 2008-10-13 10:45:06 +0100 | [diff] [blame] | 186 | /** |
| 187 | * put_tty_queue - add character to tty |
| 188 | * @c: character |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 189 | * @ldata: n_tty data |
Alan Cox | 17b8206 | 2008-10-13 10:45:06 +0100 | [diff] [blame] | 190 | * |
| 191 | * Add a character to the tty read_buf queue. This is done under the |
| 192 | * read_lock to serialize character addition and also to protect us |
| 193 | * against parallel reads or flushes |
| 194 | */ |
| 195 | |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 196 | static void put_tty_queue(unsigned char c, struct n_tty_data *ldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | { |
| 198 | unsigned long flags; |
| 199 | /* |
| 200 | * The problem of stomping on the buffers ends here. |
| 201 | * Why didn't anyone see this one coming? --AJK |
| 202 | */ |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 203 | raw_spin_lock_irqsave(&ldata->read_lock, flags); |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 204 | put_tty_queue_nolock(c, ldata); |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 205 | raw_spin_unlock_irqrestore(&ldata->read_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | * reset_buffer_flags - reset buffer state |
| 210 | * @tty: terminal to reset |
| 211 | * |
Peter Hurley | 25518c6 | 2013-03-11 16:44:31 -0400 | [diff] [blame] | 212 | * Reset the read buffer counters and clear the flags. |
| 213 | * Called from n_tty_open() and n_tty_flush_buffer(). |
Alan Cox | 17b8206 | 2008-10-13 10:45:06 +0100 | [diff] [blame] | 214 | * |
| 215 | * Locking: tty_read_lock for read fields. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | */ |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 217 | |
Peter Hurley | b66f4fa | 2013-03-11 16:44:32 -0400 | [diff] [blame] | 218 | static void reset_buffer_flags(struct n_tty_data *ldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
| 220 | unsigned long flags; |
| 221 | |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 222 | raw_spin_lock_irqsave(&ldata->read_lock, flags); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 223 | ldata->read_head = ldata->read_tail = ldata->read_cnt = 0; |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 224 | raw_spin_unlock_irqrestore(&ldata->read_lock, flags); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 225 | |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 226 | mutex_lock(&ldata->echo_lock); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 227 | ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0; |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 228 | mutex_unlock(&ldata->echo_lock); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 229 | |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 230 | ldata->canon_head = ldata->canon_data = ldata->erasing = 0; |
Jiri Slaby | 3fe780b | 2012-10-18 22:26:40 +0200 | [diff] [blame] | 231 | bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Peter Hurley | a30737a | 2013-03-11 16:44:22 -0400 | [diff] [blame] | 234 | static void n_tty_packet_mode_flush(struct tty_struct *tty) |
| 235 | { |
| 236 | unsigned long flags; |
| 237 | |
| 238 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
| 239 | if (tty->link->packet) { |
| 240 | tty->ctrl_status |= TIOCPKT_FLUSHREAD; |
| 241 | wake_up_interruptible(&tty->link->read_wait); |
| 242 | } |
| 243 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
| 244 | } |
| 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | /** |
| 247 | * n_tty_flush_buffer - clean input queue |
| 248 | * @tty: terminal device |
| 249 | * |
Peter Hurley | 25518c6 | 2013-03-11 16:44:31 -0400 | [diff] [blame] | 250 | * Flush the input buffer. Called when the tty layer wants the |
| 251 | * buffer flushed (eg at hangup) or when the N_TTY line discipline |
| 252 | * internally has to clean the pending queue (for example some signals). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | * |
Alan Cox | 17b8206 | 2008-10-13 10:45:06 +0100 | [diff] [blame] | 254 | * Locking: ctrl_lock, read_lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 256 | |
| 257 | static void n_tty_flush_buffer(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | { |
Peter Hurley | b66f4fa | 2013-03-11 16:44:32 -0400 | [diff] [blame] | 259 | reset_buffer_flags(tty->disc_data); |
| 260 | n_tty_set_room(tty); |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 261 | |
Peter Hurley | a30737a | 2013-03-11 16:44:22 -0400 | [diff] [blame] | 262 | if (tty->link) |
| 263 | n_tty_packet_mode_flush(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /** |
| 267 | * n_tty_chars_in_buffer - report available bytes |
| 268 | * @tty: tty device |
| 269 | * |
| 270 | * Report the number of characters buffered to be delivered to user |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 271 | * at this instant in time. |
Alan Cox | 17b8206 | 2008-10-13 10:45:06 +0100 | [diff] [blame] | 272 | * |
| 273 | * Locking: read_lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty) |
| 277 | { |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 278 | struct n_tty_data *ldata = tty->disc_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | unsigned long flags; |
| 280 | ssize_t n = 0; |
| 281 | |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 282 | raw_spin_lock_irqsave(&ldata->read_lock, flags); |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 283 | if (!ldata->icanon) { |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 284 | n = ldata->read_cnt; |
| 285 | } else if (ldata->canon_data) { |
| 286 | n = (ldata->canon_head > ldata->read_tail) ? |
| 287 | ldata->canon_head - ldata->read_tail : |
| 288 | ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 290 | raw_spin_unlock_irqrestore(&ldata->read_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | return n; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * is_utf8_continuation - utf8 multibyte check |
| 296 | * @c: byte to check |
| 297 | * |
| 298 | * Returns true if the utf8 character 'c' is a multibyte continuation |
| 299 | * character. We use this to correctly compute the on screen size |
| 300 | * of the character when printing |
| 301 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | static inline int is_utf8_continuation(unsigned char c) |
| 304 | { |
| 305 | return (c & 0xc0) == 0x80; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * is_continuation - multibyte check |
| 310 | * @c: byte to check |
| 311 | * |
| 312 | * Returns true if the utf8 character 'c' is a multibyte continuation |
| 313 | * character and the terminal is in unicode mode. |
| 314 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | static inline int is_continuation(unsigned char c, struct tty_struct *tty) |
| 317 | { |
| 318 | return I_IUTF8(tty) && is_utf8_continuation(c); |
| 319 | } |
| 320 | |
| 321 | /** |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 322 | * do_output_char - output one character |
| 323 | * @c: character (or partial unicode symbol) |
| 324 | * @tty: terminal device |
| 325 | * @space: space available in tty driver write buffer |
| 326 | * |
| 327 | * This is a helper function that handles one output character |
| 328 | * (including special characters like TAB, CR, LF, etc.), |
Joe Peterson | ee5aa7b | 2009-09-09 15:03:13 -0600 | [diff] [blame] | 329 | * doing OPOST processing and putting the results in the |
| 330 | * tty driver's write buffer. |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 331 | * |
| 332 | * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY |
| 333 | * and NLDLY. They simply aren't relevant in the world today. |
| 334 | * If you ever need them, add them here. |
| 335 | * |
| 336 | * Returns the number of bytes of buffer space used or -1 if |
| 337 | * no space left. |
| 338 | * |
| 339 | * Locking: should be called under the output_lock to protect |
| 340 | * the column state and space left in the buffer |
| 341 | */ |
| 342 | |
| 343 | static int do_output_char(unsigned char c, struct tty_struct *tty, int space) |
| 344 | { |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 345 | struct n_tty_data *ldata = tty->disc_data; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 346 | int spaces; |
| 347 | |
| 348 | if (!space) |
| 349 | return -1; |
Alan Cox | 300a620 | 2009-01-02 13:41:04 +0000 | [diff] [blame] | 350 | |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 351 | switch (c) { |
| 352 | case '\n': |
| 353 | if (O_ONLRET(tty)) |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 354 | ldata->column = 0; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 355 | if (O_ONLCR(tty)) { |
| 356 | if (space < 2) |
| 357 | return -1; |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 358 | ldata->canon_column = ldata->column = 0; |
Linus Torvalds | 37f81fa | 2009-09-05 12:46:07 -0700 | [diff] [blame] | 359 | tty->ops->write(tty, "\r\n", 2); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 360 | return 2; |
| 361 | } |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 362 | ldata->canon_column = ldata->column; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 363 | break; |
| 364 | case '\r': |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 365 | if (O_ONOCR(tty) && ldata->column == 0) |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 366 | return 0; |
| 367 | if (O_OCRNL(tty)) { |
| 368 | c = '\n'; |
| 369 | if (O_ONLRET(tty)) |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 370 | ldata->canon_column = ldata->column = 0; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 371 | break; |
| 372 | } |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 373 | ldata->canon_column = ldata->column = 0; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 374 | break; |
| 375 | case '\t': |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 376 | spaces = 8 - (ldata->column & 7); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 377 | if (O_TABDLY(tty) == XTABS) { |
| 378 | if (space < spaces) |
| 379 | return -1; |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 380 | ldata->column += spaces; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 381 | tty->ops->write(tty, " ", spaces); |
| 382 | return spaces; |
| 383 | } |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 384 | ldata->column += spaces; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 385 | break; |
| 386 | case '\b': |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 387 | if (ldata->column > 0) |
| 388 | ldata->column--; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 389 | break; |
| 390 | default: |
Joe Peterson | a59c0d6 | 2009-01-02 13:43:25 +0000 | [diff] [blame] | 391 | if (!iscntrl(c)) { |
| 392 | if (O_OLCUC(tty)) |
| 393 | c = toupper(c); |
| 394 | if (!is_continuation(c, tty)) |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 395 | ldata->column++; |
Joe Peterson | a59c0d6 | 2009-01-02 13:43:25 +0000 | [diff] [blame] | 396 | } |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 397 | break; |
| 398 | } |
| 399 | |
| 400 | tty_put_char(tty, c); |
| 401 | return 1; |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * process_output - output post processor |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | * @c: character (or partial unicode symbol) |
| 407 | * @tty: terminal device |
| 408 | * |
Joe Peterson | ee5aa7b | 2009-09-09 15:03:13 -0600 | [diff] [blame] | 409 | * Output one character with OPOST processing. |
| 410 | * Returns -1 when the output device is full and the character |
| 411 | * must be retried. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | * |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 413 | * Locking: output_lock to protect column state and space left |
| 414 | * (also, this is called from n_tty_write under the |
| 415 | * tty layer write lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 417 | |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 418 | static int process_output(unsigned char c, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | { |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 420 | struct n_tty_data *ldata = tty->disc_data; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 421 | int space, retval; |
| 422 | |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 423 | mutex_lock(&ldata->output_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 425 | space = tty_write_room(tty); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 426 | retval = do_output_char(c, tty, space); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 428 | mutex_unlock(&ldata->output_lock); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 429 | if (retval < 0) |
| 430 | return -1; |
| 431 | else |
| 432 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /** |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 436 | * process_output_block - block post processor |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | * @tty: terminal device |
Joe Peterson | ee5aa7b | 2009-09-09 15:03:13 -0600 | [diff] [blame] | 438 | * @buf: character buffer |
| 439 | * @nr: number of bytes to output |
| 440 | * |
| 441 | * Output a block of characters with OPOST processing. |
| 442 | * Returns the number of characters output. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | * |
| 444 | * This path is used to speed up block console writes, among other |
| 445 | * things when processing blocks of output data. It handles only |
| 446 | * the simple cases normally found and helps to generate blocks of |
| 447 | * symbols for the console driver and thus improve performance. |
| 448 | * |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 449 | * Locking: output_lock to protect column state and space left |
| 450 | * (also, this is called from n_tty_write under the |
| 451 | * tty layer write lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 453 | |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 454 | static ssize_t process_output_block(struct tty_struct *tty, |
| 455 | const unsigned char *buf, unsigned int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | { |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 457 | struct n_tty_data *ldata = tty->disc_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | int space; |
Thorsten Wißmann | bbd2075 | 2011-12-08 17:47:33 +0100 | [diff] [blame] | 459 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | const unsigned char *cp; |
| 461 | |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 462 | mutex_lock(&ldata->output_lock); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 463 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 464 | space = tty_write_room(tty); |
Alan Cox | 300a620 | 2009-01-02 13:41:04 +0000 | [diff] [blame] | 465 | if (!space) { |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 466 | mutex_unlock(&ldata->output_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | return 0; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 468 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | if (nr > space) |
| 470 | nr = space; |
| 471 | |
| 472 | for (i = 0, cp = buf; i < nr; i++, cp++) { |
Joe Peterson | a59c0d6 | 2009-01-02 13:43:25 +0000 | [diff] [blame] | 473 | unsigned char c = *cp; |
| 474 | |
| 475 | switch (c) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | case '\n': |
| 477 | if (O_ONLRET(tty)) |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 478 | ldata->column = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | if (O_ONLCR(tty)) |
| 480 | goto break_out; |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 481 | ldata->canon_column = ldata->column; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | break; |
| 483 | case '\r': |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 484 | if (O_ONOCR(tty) && ldata->column == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | goto break_out; |
| 486 | if (O_OCRNL(tty)) |
| 487 | goto break_out; |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 488 | ldata->canon_column = ldata->column = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | break; |
| 490 | case '\t': |
| 491 | goto break_out; |
| 492 | case '\b': |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 493 | if (ldata->column > 0) |
| 494 | ldata->column--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | break; |
| 496 | default: |
Joe Peterson | a59c0d6 | 2009-01-02 13:43:25 +0000 | [diff] [blame] | 497 | if (!iscntrl(c)) { |
| 498 | if (O_OLCUC(tty)) |
| 499 | goto break_out; |
| 500 | if (!is_continuation(c, tty)) |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 501 | ldata->column++; |
Joe Peterson | a59c0d6 | 2009-01-02 13:43:25 +0000 | [diff] [blame] | 502 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | break; |
| 504 | } |
| 505 | } |
| 506 | break_out: |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 507 | i = tty->ops->write(tty, buf, i); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 508 | |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 509 | mutex_unlock(&ldata->output_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | return i; |
| 511 | } |
| 512 | |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 513 | /** |
| 514 | * process_echoes - write pending echo characters |
| 515 | * @tty: terminal device |
| 516 | * |
| 517 | * Write previously buffered echo (and other ldisc-generated) |
| 518 | * characters to the tty. |
| 519 | * |
| 520 | * Characters generated by the ldisc (including echoes) need to |
| 521 | * be buffered because the driver's write buffer can fill during |
| 522 | * heavy program output. Echoing straight to the driver will |
| 523 | * often fail under these conditions, causing lost characters and |
| 524 | * resulting mismatches of ldisc state information. |
| 525 | * |
| 526 | * Since the ldisc state must represent the characters actually sent |
| 527 | * to the driver at the time of the write, operations like certain |
| 528 | * changes in column state are also saved in the buffer and executed |
| 529 | * here. |
| 530 | * |
| 531 | * A circular fifo buffer is used so that the most recent characters |
| 532 | * are prioritized. Also, when control characters are echoed with a |
| 533 | * prefixed "^", the pair is treated atomically and thus not separated. |
| 534 | * |
| 535 | * Locking: output_lock to protect column state and space left, |
| 536 | * echo_lock to protect the echo buffer |
| 537 | */ |
| 538 | |
| 539 | static void process_echoes(struct tty_struct *tty) |
| 540 | { |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 541 | struct n_tty_data *ldata = tty->disc_data; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 542 | int space, nr; |
| 543 | unsigned char c; |
| 544 | unsigned char *cp, *buf_end; |
| 545 | |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 546 | if (!ldata->echo_cnt) |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 547 | return; |
| 548 | |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 549 | mutex_lock(&ldata->output_lock); |
| 550 | mutex_lock(&ldata->echo_lock); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 551 | |
| 552 | space = tty_write_room(tty); |
| 553 | |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 554 | buf_end = ldata->echo_buf + N_TTY_BUF_SIZE; |
| 555 | cp = ldata->echo_buf + ldata->echo_pos; |
| 556 | nr = ldata->echo_cnt; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 557 | while (nr > 0) { |
| 558 | c = *cp; |
| 559 | if (c == ECHO_OP_START) { |
| 560 | unsigned char op; |
| 561 | unsigned char *opp; |
| 562 | int no_space_left = 0; |
| 563 | |
| 564 | /* |
| 565 | * If the buffer byte is the start of a multi-byte |
| 566 | * operation, get the next byte, which is either the |
| 567 | * op code or a control character value. |
| 568 | */ |
| 569 | opp = cp + 1; |
| 570 | if (opp == buf_end) |
| 571 | opp -= N_TTY_BUF_SIZE; |
| 572 | op = *opp; |
Alan Cox | 300a620 | 2009-01-02 13:41:04 +0000 | [diff] [blame] | 573 | |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 574 | switch (op) { |
| 575 | unsigned int num_chars, num_bs; |
| 576 | |
| 577 | case ECHO_OP_ERASE_TAB: |
| 578 | if (++opp == buf_end) |
| 579 | opp -= N_TTY_BUF_SIZE; |
| 580 | num_chars = *opp; |
| 581 | |
| 582 | /* |
| 583 | * Determine how many columns to go back |
| 584 | * in order to erase the tab. |
| 585 | * This depends on the number of columns |
| 586 | * used by other characters within the tab |
| 587 | * area. If this (modulo 8) count is from |
| 588 | * the start of input rather than from a |
| 589 | * previous tab, we offset by canon column. |
| 590 | * Otherwise, tab spacing is normal. |
| 591 | */ |
| 592 | if (!(num_chars & 0x80)) |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 593 | num_chars += ldata->canon_column; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 594 | num_bs = 8 - (num_chars & 7); |
| 595 | |
| 596 | if (num_bs > space) { |
| 597 | no_space_left = 1; |
| 598 | break; |
| 599 | } |
| 600 | space -= num_bs; |
| 601 | while (num_bs--) { |
| 602 | tty_put_char(tty, '\b'); |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 603 | if (ldata->column > 0) |
| 604 | ldata->column--; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 605 | } |
| 606 | cp += 3; |
| 607 | nr -= 3; |
| 608 | break; |
| 609 | |
| 610 | case ECHO_OP_SET_CANON_COL: |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 611 | ldata->canon_column = ldata->column; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 612 | cp += 2; |
| 613 | nr -= 2; |
| 614 | break; |
| 615 | |
| 616 | case ECHO_OP_MOVE_BACK_COL: |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 617 | if (ldata->column > 0) |
| 618 | ldata->column--; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 619 | cp += 2; |
| 620 | nr -= 2; |
| 621 | break; |
| 622 | |
| 623 | case ECHO_OP_START: |
| 624 | /* This is an escaped echo op start code */ |
| 625 | if (!space) { |
| 626 | no_space_left = 1; |
| 627 | break; |
| 628 | } |
| 629 | tty_put_char(tty, ECHO_OP_START); |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 630 | ldata->column++; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 631 | space--; |
| 632 | cp += 2; |
| 633 | nr -= 2; |
| 634 | break; |
| 635 | |
| 636 | default: |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 637 | /* |
Joe Peterson | 62b2635 | 2009-09-09 15:03:47 -0600 | [diff] [blame] | 638 | * If the op is not a special byte code, |
| 639 | * it is a ctrl char tagged to be echoed |
| 640 | * as "^X" (where X is the letter |
| 641 | * representing the control char). |
| 642 | * Note that we must ensure there is |
| 643 | * enough space for the whole ctrl pair. |
| 644 | * |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 645 | */ |
Joe Peterson | 62b2635 | 2009-09-09 15:03:47 -0600 | [diff] [blame] | 646 | if (space < 2) { |
| 647 | no_space_left = 1; |
| 648 | break; |
| 649 | } |
| 650 | tty_put_char(tty, '^'); |
| 651 | tty_put_char(tty, op ^ 0100); |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 652 | ldata->column += 2; |
Joe Peterson | 62b2635 | 2009-09-09 15:03:47 -0600 | [diff] [blame] | 653 | space -= 2; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 654 | cp += 2; |
| 655 | nr -= 2; |
| 656 | } |
| 657 | |
| 658 | if (no_space_left) |
| 659 | break; |
| 660 | } else { |
Peter Hurley | 582f559 | 2013-05-17 12:49:48 -0400 | [diff] [blame] | 661 | if (O_OPOST(tty)) { |
Joe Peterson | ee5aa7b | 2009-09-09 15:03:13 -0600 | [diff] [blame] | 662 | int retval = do_output_char(c, tty, space); |
| 663 | if (retval < 0) |
| 664 | break; |
| 665 | space -= retval; |
| 666 | } else { |
| 667 | if (!space) |
| 668 | break; |
| 669 | tty_put_char(tty, c); |
| 670 | space -= 1; |
| 671 | } |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 672 | cp += 1; |
| 673 | nr -= 1; |
| 674 | } |
| 675 | |
| 676 | /* When end of circular buffer reached, wrap around */ |
| 677 | if (cp >= buf_end) |
| 678 | cp -= N_TTY_BUF_SIZE; |
| 679 | } |
| 680 | |
| 681 | if (nr == 0) { |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 682 | ldata->echo_pos = 0; |
| 683 | ldata->echo_cnt = 0; |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 684 | ldata->echo_overrun = 0; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 685 | } else { |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 686 | int num_processed = ldata->echo_cnt - nr; |
| 687 | ldata->echo_pos += num_processed; |
| 688 | ldata->echo_pos &= N_TTY_BUF_SIZE - 1; |
| 689 | ldata->echo_cnt = nr; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 690 | if (num_processed > 0) |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 691 | ldata->echo_overrun = 0; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 694 | mutex_unlock(&ldata->echo_lock); |
| 695 | mutex_unlock(&ldata->output_lock); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 696 | |
| 697 | if (tty->ops->flush_chars) |
| 698 | tty->ops->flush_chars(tty); |
| 699 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
| 701 | /** |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 702 | * add_echo_byte - add a byte to the echo buffer |
| 703 | * @c: unicode byte to echo |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 704 | * @ldata: n_tty data |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 705 | * |
| 706 | * Add a character or operation byte to the echo buffer. |
| 707 | * |
| 708 | * Should be called under the echo lock to protect the echo buffer. |
| 709 | */ |
| 710 | |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 711 | static void add_echo_byte(unsigned char c, struct n_tty_data *ldata) |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 712 | { |
| 713 | int new_byte_pos; |
| 714 | |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 715 | if (ldata->echo_cnt == N_TTY_BUF_SIZE) { |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 716 | /* Circular buffer is already at capacity */ |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 717 | new_byte_pos = ldata->echo_pos; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 718 | |
| 719 | /* |
| 720 | * Since the buffer start position needs to be advanced, |
| 721 | * be sure to step by a whole operation byte group. |
| 722 | */ |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 723 | if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) { |
| 724 | if (ldata->echo_buf[(ldata->echo_pos + 1) & |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 725 | (N_TTY_BUF_SIZE - 1)] == |
| 726 | ECHO_OP_ERASE_TAB) { |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 727 | ldata->echo_pos += 3; |
| 728 | ldata->echo_cnt -= 2; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 729 | } else { |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 730 | ldata->echo_pos += 2; |
| 731 | ldata->echo_cnt -= 1; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 732 | } |
| 733 | } else { |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 734 | ldata->echo_pos++; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 735 | } |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 736 | ldata->echo_pos &= N_TTY_BUF_SIZE - 1; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 737 | |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 738 | ldata->echo_overrun = 1; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 739 | } else { |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 740 | new_byte_pos = ldata->echo_pos + ldata->echo_cnt; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 741 | new_byte_pos &= N_TTY_BUF_SIZE - 1; |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 742 | ldata->echo_cnt++; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 745 | ldata->echo_buf[new_byte_pos] = c; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | /** |
| 749 | * echo_move_back_col - add operation to move back a column |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 750 | * @ldata: n_tty data |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 751 | * |
| 752 | * Add an operation to the echo buffer to move back one column. |
| 753 | * |
| 754 | * Locking: echo_lock to protect the echo buffer |
| 755 | */ |
| 756 | |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 757 | static void echo_move_back_col(struct n_tty_data *ldata) |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 758 | { |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 759 | mutex_lock(&ldata->echo_lock); |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 760 | add_echo_byte(ECHO_OP_START, ldata); |
| 761 | add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata); |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 762 | mutex_unlock(&ldata->echo_lock); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | /** |
| 766 | * echo_set_canon_col - add operation to set the canon column |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 767 | * @ldata: n_tty data |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 768 | * |
| 769 | * Add an operation to the echo buffer to set the canon column |
| 770 | * to the current column. |
| 771 | * |
| 772 | * Locking: echo_lock to protect the echo buffer |
| 773 | */ |
| 774 | |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 775 | static void echo_set_canon_col(struct n_tty_data *ldata) |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 776 | { |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 777 | mutex_lock(&ldata->echo_lock); |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 778 | add_echo_byte(ECHO_OP_START, ldata); |
| 779 | add_echo_byte(ECHO_OP_SET_CANON_COL, ldata); |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 780 | mutex_unlock(&ldata->echo_lock); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | /** |
| 784 | * echo_erase_tab - add operation to erase a tab |
| 785 | * @num_chars: number of character columns already used |
| 786 | * @after_tab: true if num_chars starts after a previous tab |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 787 | * @ldata: n_tty data |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 788 | * |
| 789 | * Add an operation to the echo buffer to erase a tab. |
| 790 | * |
| 791 | * Called by the eraser function, which knows how many character |
| 792 | * columns have been used since either a previous tab or the start |
| 793 | * of input. This information will be used later, along with |
| 794 | * canon column (if applicable), to go back the correct number |
| 795 | * of columns. |
| 796 | * |
| 797 | * Locking: echo_lock to protect the echo buffer |
| 798 | */ |
| 799 | |
| 800 | static void echo_erase_tab(unsigned int num_chars, int after_tab, |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 801 | struct n_tty_data *ldata) |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 802 | { |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 803 | mutex_lock(&ldata->echo_lock); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 804 | |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 805 | add_echo_byte(ECHO_OP_START, ldata); |
| 806 | add_echo_byte(ECHO_OP_ERASE_TAB, ldata); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 807 | |
| 808 | /* We only need to know this modulo 8 (tab spacing) */ |
| 809 | num_chars &= 7; |
| 810 | |
| 811 | /* Set the high bit as a flag if num_chars is after a previous tab */ |
| 812 | if (after_tab) |
| 813 | num_chars |= 0x80; |
Alan Cox | 300a620 | 2009-01-02 13:41:04 +0000 | [diff] [blame] | 814 | |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 815 | add_echo_byte(num_chars, ldata); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 816 | |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 817 | mutex_unlock(&ldata->echo_lock); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | /** |
| 821 | * echo_char_raw - echo a character raw |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | * @c: unicode byte to echo |
| 823 | * @tty: terminal device |
| 824 | * |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 825 | * Echo user input back onto the screen. This must be called only when |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | * L_ECHO(tty) is true. Called from the driver receive_buf path. |
Alan Cox | 17b8206 | 2008-10-13 10:45:06 +0100 | [diff] [blame] | 827 | * |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 828 | * This variant does not treat control characters specially. |
| 829 | * |
| 830 | * Locking: echo_lock to protect the echo buffer |
| 831 | */ |
| 832 | |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 833 | static void echo_char_raw(unsigned char c, struct n_tty_data *ldata) |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 834 | { |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 835 | mutex_lock(&ldata->echo_lock); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 836 | if (c == ECHO_OP_START) { |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 837 | add_echo_byte(ECHO_OP_START, ldata); |
| 838 | add_echo_byte(ECHO_OP_START, ldata); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 839 | } else { |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 840 | add_echo_byte(c, ldata); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 841 | } |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 842 | mutex_unlock(&ldata->echo_lock); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | /** |
| 846 | * echo_char - echo a character |
| 847 | * @c: unicode byte to echo |
| 848 | * @tty: terminal device |
| 849 | * |
| 850 | * Echo user input back onto the screen. This must be called only when |
| 851 | * L_ECHO(tty) is true. Called from the driver receive_buf path. |
| 852 | * |
Joe Peterson | 62b2635 | 2009-09-09 15:03:47 -0600 | [diff] [blame] | 853 | * This variant tags control characters to be echoed as "^X" |
| 854 | * (where X is the letter representing the control char). |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 855 | * |
| 856 | * Locking: echo_lock to protect the echo buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | */ |
| 858 | |
| 859 | static void echo_char(unsigned char c, struct tty_struct *tty) |
| 860 | { |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 861 | struct n_tty_data *ldata = tty->disc_data; |
| 862 | |
| 863 | mutex_lock(&ldata->echo_lock); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 864 | |
| 865 | if (c == ECHO_OP_START) { |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 866 | add_echo_byte(ECHO_OP_START, ldata); |
| 867 | add_echo_byte(ECHO_OP_START, ldata); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 868 | } else { |
Joe Peterson | 62b2635 | 2009-09-09 15:03:47 -0600 | [diff] [blame] | 869 | if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 870 | add_echo_byte(ECHO_OP_START, ldata); |
| 871 | add_echo_byte(c, ldata); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 874 | mutex_unlock(&ldata->echo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Alan Cox | 17b8206 | 2008-10-13 10:45:06 +0100 | [diff] [blame] | 877 | /** |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 878 | * finish_erasing - complete erase |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 879 | * @ldata: n_tty data |
Alan Cox | 17b8206 | 2008-10-13 10:45:06 +0100 | [diff] [blame] | 880 | */ |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 881 | |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 882 | static inline void finish_erasing(struct n_tty_data *ldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | { |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 884 | if (ldata->erasing) { |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 885 | echo_char_raw('/', ldata); |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 886 | ldata->erasing = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | |
| 890 | /** |
| 891 | * eraser - handle erase function |
| 892 | * @c: character input |
| 893 | * @tty: terminal device |
| 894 | * |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 895 | * Perform erase and necessary output when an erase character is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | * present in the stream from the driver layer. Handles the complexities |
| 897 | * of UTF-8 multibyte symbols. |
Alan Cox | 17b8206 | 2008-10-13 10:45:06 +0100 | [diff] [blame] | 898 | * |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 899 | * Locking: read_lock for tty buffers |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 901 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | static void eraser(unsigned char c, struct tty_struct *tty) |
| 903 | { |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 904 | struct n_tty_data *ldata = tty->disc_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | enum { ERASE, WERASE, KILL } kill_type; |
| 906 | int head, seen_alnums, cnt; |
| 907 | unsigned long flags; |
| 908 | |
Alan Cox | 17b8206 | 2008-10-13 10:45:06 +0100 | [diff] [blame] | 909 | /* FIXME: locking needed ? */ |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 910 | if (ldata->read_head == ldata->canon_head) { |
Joe Peterson | 7e94b1d | 2009-01-02 13:43:40 +0000 | [diff] [blame] | 911 | /* process_output('\a', tty); */ /* what do you think? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | return; |
| 913 | } |
| 914 | if (c == ERASE_CHAR(tty)) |
| 915 | kill_type = ERASE; |
| 916 | else if (c == WERASE_CHAR(tty)) |
| 917 | kill_type = WERASE; |
| 918 | else { |
| 919 | if (!L_ECHO(tty)) { |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 920 | raw_spin_lock_irqsave(&ldata->read_lock, flags); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 921 | ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) & |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | (N_TTY_BUF_SIZE - 1)); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 923 | ldata->read_head = ldata->canon_head; |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 924 | raw_spin_unlock_irqrestore(&ldata->read_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | return; |
| 926 | } |
| 927 | if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) { |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 928 | raw_spin_lock_irqsave(&ldata->read_lock, flags); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 929 | ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) & |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | (N_TTY_BUF_SIZE - 1)); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 931 | ldata->read_head = ldata->canon_head; |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 932 | raw_spin_unlock_irqrestore(&ldata->read_lock, flags); |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 933 | finish_erasing(ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | echo_char(KILL_CHAR(tty), tty); |
| 935 | /* Add a newline if ECHOK is on and ECHOKE is off. */ |
| 936 | if (L_ECHOK(tty)) |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 937 | echo_char_raw('\n', ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | return; |
| 939 | } |
| 940 | kill_type = KILL; |
| 941 | } |
| 942 | |
| 943 | seen_alnums = 0; |
Alan Cox | 17b8206 | 2008-10-13 10:45:06 +0100 | [diff] [blame] | 944 | /* FIXME: Locking ?? */ |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 945 | while (ldata->read_head != ldata->canon_head) { |
| 946 | head = ldata->read_head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | |
| 948 | /* erase a single possibly multibyte character */ |
| 949 | do { |
| 950 | head = (head - 1) & (N_TTY_BUF_SIZE-1); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 951 | c = ldata->read_buf[head]; |
| 952 | } while (is_continuation(c, tty) && head != ldata->canon_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | |
| 954 | /* do not partially erase */ |
| 955 | if (is_continuation(c, tty)) |
| 956 | break; |
| 957 | |
| 958 | if (kill_type == WERASE) { |
| 959 | /* Equivalent to BSD's ALTWERASE. */ |
| 960 | if (isalnum(c) || c == '_') |
| 961 | seen_alnums++; |
| 962 | else if (seen_alnums) |
| 963 | break; |
| 964 | } |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 965 | cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1); |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 966 | raw_spin_lock_irqsave(&ldata->read_lock, flags); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 967 | ldata->read_head = head; |
| 968 | ldata->read_cnt -= cnt; |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 969 | raw_spin_unlock_irqrestore(&ldata->read_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | if (L_ECHO(tty)) { |
| 971 | if (L_ECHOPRT(tty)) { |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 972 | if (!ldata->erasing) { |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 973 | echo_char_raw('\\', ldata); |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 974 | ldata->erasing = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | } |
| 976 | /* if cnt > 1, output a multi-byte character */ |
| 977 | echo_char(c, tty); |
| 978 | while (--cnt > 0) { |
| 979 | head = (head+1) & (N_TTY_BUF_SIZE-1); |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 980 | echo_char_raw(ldata->read_buf[head], |
| 981 | ldata); |
| 982 | echo_move_back_col(ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | } |
| 984 | } else if (kill_type == ERASE && !L_ECHOE(tty)) { |
| 985 | echo_char(ERASE_CHAR(tty), tty); |
| 986 | } else if (c == '\t') { |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 987 | unsigned int num_chars = 0; |
| 988 | int after_tab = 0; |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 989 | unsigned long tail = ldata->read_head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 991 | /* |
| 992 | * Count the columns used for characters |
| 993 | * since the start of input or after a |
| 994 | * previous tab. |
| 995 | * This info is used to go back the correct |
| 996 | * number of columns. |
| 997 | */ |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 998 | while (tail != ldata->canon_head) { |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 999 | tail = (tail-1) & (N_TTY_BUF_SIZE-1); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1000 | c = ldata->read_buf[tail]; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 1001 | if (c == '\t') { |
| 1002 | after_tab = 1; |
| 1003 | break; |
Alan Cox | 300a620 | 2009-01-02 13:41:04 +0000 | [diff] [blame] | 1004 | } else if (iscntrl(c)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | if (L_ECHOCTL(tty)) |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 1006 | num_chars += 2; |
| 1007 | } else if (!is_continuation(c, tty)) { |
| 1008 | num_chars++; |
| 1009 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | } |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1011 | echo_erase_tab(num_chars, after_tab, ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | } else { |
| 1013 | if (iscntrl(c) && L_ECHOCTL(tty)) { |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1014 | echo_char_raw('\b', ldata); |
| 1015 | echo_char_raw(' ', ldata); |
| 1016 | echo_char_raw('\b', ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | } |
| 1018 | if (!iscntrl(c) || L_ECHOCTL(tty)) { |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1019 | echo_char_raw('\b', ldata); |
| 1020 | echo_char_raw(' ', ldata); |
| 1021 | echo_char_raw('\b', ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | if (kill_type == ERASE) |
| 1026 | break; |
| 1027 | } |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1028 | if (ldata->read_head == ldata->canon_head && L_ECHO(tty)) |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1029 | finish_erasing(ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | /** |
| 1033 | * isig - handle the ISIG optio |
| 1034 | * @sig: signal |
| 1035 | * @tty: terminal |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | * |
Peter Hurley | 8c985d1 | 2013-03-06 08:38:19 -0500 | [diff] [blame] | 1037 | * Called when a signal is being sent due to terminal input. |
| 1038 | * Called from the driver receive_buf path so serialized. |
Alan Cox | 17b8206 | 2008-10-13 10:45:06 +0100 | [diff] [blame] | 1039 | * |
Peter Hurley | 8c985d1 | 2013-03-06 08:38:19 -0500 | [diff] [blame] | 1040 | * Locking: ctrl_lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1042 | |
Peter Hurley | 8c985d1 | 2013-03-06 08:38:19 -0500 | [diff] [blame] | 1043 | static inline void isig(int sig, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | { |
Peter Hurley | 8c985d1 | 2013-03-06 08:38:19 -0500 | [diff] [blame] | 1045 | struct pid *tty_pgrp = tty_get_pgrp(tty); |
| 1046 | if (tty_pgrp) { |
| 1047 | kill_pgrp(tty_pgrp, sig, 1); |
| 1048 | put_pid(tty_pgrp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | /** |
| 1053 | * n_tty_receive_break - handle break |
| 1054 | * @tty: terminal |
| 1055 | * |
| 1056 | * An RS232 break event has been hit in the incoming bitstream. This |
| 1057 | * can cause a variety of events depending upon the termios settings. |
| 1058 | * |
| 1059 | * Called from the receive_buf path so single threaded. |
| 1060 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | static inline void n_tty_receive_break(struct tty_struct *tty) |
| 1063 | { |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1064 | struct n_tty_data *ldata = tty->disc_data; |
| 1065 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | if (I_IGNBRK(tty)) |
| 1067 | return; |
| 1068 | if (I_BRKINT(tty)) { |
Peter Hurley | 8c985d1 | 2013-03-06 08:38:19 -0500 | [diff] [blame] | 1069 | isig(SIGINT, tty); |
| 1070 | if (!L_NOFLSH(tty)) { |
| 1071 | n_tty_flush_buffer(tty); |
| 1072 | tty_driver_flush_buffer(tty); |
| 1073 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | return; |
| 1075 | } |
| 1076 | if (I_PARMRK(tty)) { |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1077 | put_tty_queue('\377', ldata); |
| 1078 | put_tty_queue('\0', ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | } |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1080 | put_tty_queue('\0', ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | wake_up_interruptible(&tty->read_wait); |
| 1082 | } |
| 1083 | |
| 1084 | /** |
| 1085 | * n_tty_receive_overrun - handle overrun reporting |
| 1086 | * @tty: terminal |
| 1087 | * |
| 1088 | * Data arrived faster than we could process it. While the tty |
| 1089 | * driver has flagged this the bits that were missed are gone |
| 1090 | * forever. |
| 1091 | * |
| 1092 | * Called from the receive_buf path so single threaded. Does not |
| 1093 | * need locking as num_overrun and overrun_time are function |
| 1094 | * private. |
| 1095 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1096 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | static inline void n_tty_receive_overrun(struct tty_struct *tty) |
| 1098 | { |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1099 | struct n_tty_data *ldata = tty->disc_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | char buf[64]; |
| 1101 | |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1102 | ldata->num_overrun++; |
| 1103 | if (time_after(jiffies, ldata->overrun_time + HZ) || |
| 1104 | time_after(ldata->overrun_time, jiffies)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | printk(KERN_WARNING "%s: %d input overrun(s)\n", |
| 1106 | tty_name(tty, buf), |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1107 | ldata->num_overrun); |
| 1108 | ldata->overrun_time = jiffies; |
| 1109 | ldata->num_overrun = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | /** |
| 1114 | * n_tty_receive_parity_error - error notifier |
| 1115 | * @tty: terminal device |
| 1116 | * @c: character |
| 1117 | * |
| 1118 | * Process a parity error and queue the right data to indicate |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 1119 | * the error case if necessary. Locking as per n_tty_receive_buf. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | */ |
| 1121 | static inline void n_tty_receive_parity_error(struct tty_struct *tty, |
| 1122 | unsigned char c) |
| 1123 | { |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1124 | struct n_tty_data *ldata = tty->disc_data; |
| 1125 | |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1126 | if (I_IGNPAR(tty)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | if (I_PARMRK(tty)) { |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1129 | put_tty_queue('\377', ldata); |
| 1130 | put_tty_queue('\0', ldata); |
| 1131 | put_tty_queue(c, ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | } else if (I_INPCK(tty)) |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1133 | put_tty_queue('\0', ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | else |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1135 | put_tty_queue(c, ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | wake_up_interruptible(&tty->read_wait); |
| 1137 | } |
| 1138 | |
| 1139 | /** |
| 1140 | * n_tty_receive_char - perform processing |
| 1141 | * @tty: terminal device |
| 1142 | * @c: character |
| 1143 | * |
| 1144 | * Process an individual character of input received from the driver. |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1145 | * This is serialized with respect to itself by the rules for the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | * driver above. |
| 1147 | */ |
| 1148 | |
| 1149 | static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c) |
| 1150 | { |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1151 | struct n_tty_data *ldata = tty->disc_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | unsigned long flags; |
Joe Peterson | acc71bb | 2009-01-02 13:43:32 +0000 | [diff] [blame] | 1153 | int parmrk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1155 | if (ldata->raw) { |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1156 | put_tty_queue(c, ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | return; |
| 1158 | } |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | if (I_ISTRIP(tty)) |
| 1161 | c &= 0x7f; |
| 1162 | if (I_IUCLC(tty) && L_IEXTEN(tty)) |
Alan Cox | 300a620 | 2009-01-02 13:41:04 +0000 | [diff] [blame] | 1163 | c = tolower(c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | |
hyc@symas.com | 26df6d1 | 2010-06-22 10:14:49 -0700 | [diff] [blame] | 1165 | if (L_EXTPROC(tty)) { |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1166 | put_tty_queue(c, ldata); |
hyc@symas.com | 26df6d1 | 2010-06-22 10:14:49 -0700 | [diff] [blame] | 1167 | return; |
| 1168 | } |
| 1169 | |
Joe Peterson | 54d2a37 | 2008-02-06 01:37:59 -0800 | [diff] [blame] | 1170 | if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 1171 | I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) && |
| 1172 | c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) { |
Joe Peterson | 54d2a37 | 2008-02-06 01:37:59 -0800 | [diff] [blame] | 1173 | start_tty(tty); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 1174 | process_echoes(tty); |
| 1175 | } |
Joe Peterson | 54d2a37 | 2008-02-06 01:37:59 -0800 | [diff] [blame] | 1176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | if (tty->closing) { |
| 1178 | if (I_IXON(tty)) { |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 1179 | if (c == START_CHAR(tty)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | start_tty(tty); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 1181 | process_echoes(tty); |
Alan Cox | 300a620 | 2009-01-02 13:41:04 +0000 | [diff] [blame] | 1182 | } else if (c == STOP_CHAR(tty)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | stop_tty(tty); |
| 1184 | } |
| 1185 | return; |
| 1186 | } |
| 1187 | |
| 1188 | /* |
| 1189 | * If the previous character was LNEXT, or we know that this |
| 1190 | * character is not one of the characters that we'll have to |
| 1191 | * handle specially, do shortcut processing to speed things |
| 1192 | * up. |
| 1193 | */ |
Jiri Slaby | 3fe780b | 2012-10-18 22:26:40 +0200 | [diff] [blame] | 1194 | if (!test_bit(c, ldata->process_char_map) || ldata->lnext) { |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1195 | ldata->lnext = 0; |
Joe Peterson | acc71bb | 2009-01-02 13:43:32 +0000 | [diff] [blame] | 1196 | parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0; |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1197 | if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) { |
Joe Peterson | acc71bb | 2009-01-02 13:43:32 +0000 | [diff] [blame] | 1198 | /* beep if no space */ |
Joe Peterson | 7e94b1d | 2009-01-02 13:43:40 +0000 | [diff] [blame] | 1199 | if (L_ECHO(tty)) |
| 1200 | process_output('\a', tty); |
Joe Peterson | acc71bb | 2009-01-02 13:43:32 +0000 | [diff] [blame] | 1201 | return; |
| 1202 | } |
| 1203 | if (L_ECHO(tty)) { |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1204 | finish_erasing(ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | /* Record the column of first canon char. */ |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1206 | if (ldata->canon_head == ldata->read_head) |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1207 | echo_set_canon_col(ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | echo_char(c, tty); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 1209 | process_echoes(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | } |
Joe Peterson | acc71bb | 2009-01-02 13:43:32 +0000 | [diff] [blame] | 1211 | if (parmrk) |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1212 | put_tty_queue(c, ldata); |
| 1213 | put_tty_queue(c, ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | return; |
| 1215 | } |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | if (I_IXON(tty)) { |
| 1218 | if (c == START_CHAR(tty)) { |
| 1219 | start_tty(tty); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 1220 | process_echoes(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | return; |
| 1222 | } |
| 1223 | if (c == STOP_CHAR(tty)) { |
| 1224 | stop_tty(tty); |
| 1225 | return; |
| 1226 | } |
| 1227 | } |
Joe Peterson | 575537b3 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 1228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | if (L_ISIG(tty)) { |
| 1230 | int signal; |
| 1231 | signal = SIGINT; |
| 1232 | if (c == INTR_CHAR(tty)) |
| 1233 | goto send_signal; |
| 1234 | signal = SIGQUIT; |
| 1235 | if (c == QUIT_CHAR(tty)) |
| 1236 | goto send_signal; |
| 1237 | signal = SIGTSTP; |
| 1238 | if (c == SUSP_CHAR(tty)) { |
| 1239 | send_signal: |
Joe Peterson | ec5b115 | 2008-02-06 01:37:38 -0800 | [diff] [blame] | 1240 | if (!L_NOFLSH(tty)) { |
| 1241 | n_tty_flush_buffer(tty); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1242 | tty_driver_flush_buffer(tty); |
Joe Peterson | ec5b115 | 2008-02-06 01:37:38 -0800 | [diff] [blame] | 1243 | } |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 1244 | if (I_IXON(tty)) |
| 1245 | start_tty(tty); |
| 1246 | if (L_ECHO(tty)) { |
Joe Peterson | ec5b115 | 2008-02-06 01:37:38 -0800 | [diff] [blame] | 1247 | echo_char(c, tty); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 1248 | process_echoes(tty); |
| 1249 | } |
Peter Hurley | 8c985d1 | 2013-03-06 08:38:19 -0500 | [diff] [blame] | 1250 | isig(signal, tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | return; |
| 1252 | } |
| 1253 | } |
Joe Peterson | 575537b3 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 1254 | |
| 1255 | if (c == '\r') { |
| 1256 | if (I_IGNCR(tty)) |
| 1257 | return; |
| 1258 | if (I_ICRNL(tty)) |
| 1259 | c = '\n'; |
| 1260 | } else if (c == '\n' && I_INLCR(tty)) |
| 1261 | c = '\r'; |
| 1262 | |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1263 | if (ldata->icanon) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) || |
| 1265 | (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) { |
| 1266 | eraser(c, tty); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 1267 | process_echoes(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | return; |
| 1269 | } |
| 1270 | if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) { |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1271 | ldata->lnext = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | if (L_ECHO(tty)) { |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1273 | finish_erasing(ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | if (L_ECHOCTL(tty)) { |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1275 | echo_char_raw('^', ldata); |
| 1276 | echo_char_raw('\b', ldata); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 1277 | process_echoes(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | } |
| 1279 | } |
| 1280 | return; |
| 1281 | } |
| 1282 | if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && |
| 1283 | L_IEXTEN(tty)) { |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1284 | unsigned long tail = ldata->canon_head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1286 | finish_erasing(ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | echo_char(c, tty); |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1288 | echo_char_raw('\n', ldata); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1289 | while (tail != ldata->read_head) { |
| 1290 | echo_char(ldata->read_buf[tail], tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | tail = (tail+1) & (N_TTY_BUF_SIZE-1); |
| 1292 | } |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 1293 | process_echoes(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | return; |
| 1295 | } |
| 1296 | if (c == '\n') { |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1297 | if (ldata->read_cnt >= N_TTY_BUF_SIZE) { |
Joe Peterson | 7e94b1d | 2009-01-02 13:43:40 +0000 | [diff] [blame] | 1298 | if (L_ECHO(tty)) |
| 1299 | process_output('\a', tty); |
Joe Peterson | acc71bb | 2009-01-02 13:43:32 +0000 | [diff] [blame] | 1300 | return; |
| 1301 | } |
| 1302 | if (L_ECHO(tty) || L_ECHONL(tty)) { |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1303 | echo_char_raw('\n', ldata); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 1304 | process_echoes(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | } |
| 1306 | goto handle_newline; |
| 1307 | } |
| 1308 | if (c == EOF_CHAR(tty)) { |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1309 | if (ldata->read_cnt >= N_TTY_BUF_SIZE) |
Joe Peterson | acc71bb | 2009-01-02 13:43:32 +0000 | [diff] [blame] | 1310 | return; |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1311 | if (ldata->canon_head != ldata->read_head) |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1312 | set_bit(TTY_PUSH, &tty->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | c = __DISABLED_CHAR; |
| 1314 | goto handle_newline; |
| 1315 | } |
| 1316 | if ((c == EOL_CHAR(tty)) || |
| 1317 | (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) { |
Joe Peterson | acc71bb | 2009-01-02 13:43:32 +0000 | [diff] [blame] | 1318 | parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) |
| 1319 | ? 1 : 0; |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1320 | if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) { |
Joe Peterson | 7e94b1d | 2009-01-02 13:43:40 +0000 | [diff] [blame] | 1321 | if (L_ECHO(tty)) |
| 1322 | process_output('\a', tty); |
Joe Peterson | acc71bb | 2009-01-02 13:43:32 +0000 | [diff] [blame] | 1323 | return; |
| 1324 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | /* |
| 1326 | * XXX are EOL_CHAR and EOL2_CHAR echoed?!? |
| 1327 | */ |
| 1328 | if (L_ECHO(tty)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | /* Record the column of first canon char. */ |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1330 | if (ldata->canon_head == ldata->read_head) |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1331 | echo_set_canon_col(ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | echo_char(c, tty); |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 1333 | process_echoes(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | } |
| 1335 | /* |
| 1336 | * XXX does PARMRK doubling happen for |
| 1337 | * EOL_CHAR and EOL2_CHAR? |
| 1338 | */ |
Joe Peterson | acc71bb | 2009-01-02 13:43:32 +0000 | [diff] [blame] | 1339 | if (parmrk) |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1340 | put_tty_queue(c, ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1342 | handle_newline: |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 1343 | raw_spin_lock_irqsave(&ldata->read_lock, flags); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1344 | set_bit(ldata->read_head, ldata->read_flags); |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1345 | put_tty_queue_nolock(c, ldata); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1346 | ldata->canon_head = ldata->read_head; |
| 1347 | ldata->canon_data++; |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 1348 | raw_spin_unlock_irqrestore(&ldata->read_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | kill_fasync(&tty->fasync, SIGIO, POLL_IN); |
| 1350 | if (waitqueue_active(&tty->read_wait)) |
| 1351 | wake_up_interruptible(&tty->read_wait); |
| 1352 | return; |
| 1353 | } |
| 1354 | } |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1355 | |
Joe Peterson | acc71bb | 2009-01-02 13:43:32 +0000 | [diff] [blame] | 1356 | parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0; |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1357 | if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) { |
Joe Peterson | acc71bb | 2009-01-02 13:43:32 +0000 | [diff] [blame] | 1358 | /* beep if no space */ |
Joe Peterson | 7e94b1d | 2009-01-02 13:43:40 +0000 | [diff] [blame] | 1359 | if (L_ECHO(tty)) |
| 1360 | process_output('\a', tty); |
Joe Peterson | acc71bb | 2009-01-02 13:43:32 +0000 | [diff] [blame] | 1361 | return; |
| 1362 | } |
| 1363 | if (L_ECHO(tty)) { |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1364 | finish_erasing(ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | if (c == '\n') |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1366 | echo_char_raw('\n', ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | else { |
| 1368 | /* Record the column of first canon char. */ |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1369 | if (ldata->canon_head == ldata->read_head) |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1370 | echo_set_canon_col(ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | echo_char(c, tty); |
| 1372 | } |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 1373 | process_echoes(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | } |
| 1375 | |
Joe Peterson | acc71bb | 2009-01-02 13:43:32 +0000 | [diff] [blame] | 1376 | if (parmrk) |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1377 | put_tty_queue(c, ldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 1379 | put_tty_queue(c, ldata); |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1380 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | |
| 1383 | /** |
| 1384 | * n_tty_write_wakeup - asynchronous I/O notifier |
| 1385 | * @tty: tty device |
| 1386 | * |
| 1387 | * Required for the ptys, serial driver etc. since processes |
| 1388 | * that attach themselves to the master and rely on ASYNC |
| 1389 | * IO must be woken up |
| 1390 | */ |
| 1391 | |
| 1392 | static void n_tty_write_wakeup(struct tty_struct *tty) |
| 1393 | { |
Thomas Pfaff | ff8cb0f | 2009-01-02 13:47:13 +0000 | [diff] [blame] | 1394 | if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | kill_fasync(&tty->fasync, SIGIO, POLL_OUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | /** |
| 1399 | * n_tty_receive_buf - data receive |
| 1400 | * @tty: terminal device |
| 1401 | * @cp: buffer |
| 1402 | * @fp: flag buffer |
| 1403 | * @count: characters |
| 1404 | * |
| 1405 | * Called by the terminal driver when a block of characters has |
| 1406 | * been received. This function must be called from soft contexts |
| 1407 | * not from interrupt context. The driver is responsible for making |
| 1408 | * calls one at a time and in order (or using flush_to_ldisc) |
| 1409 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1410 | |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 1411 | static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp, |
| 1412 | char *fp, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | { |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1414 | struct n_tty_data *ldata = tty->disc_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | const unsigned char *p; |
| 1416 | char *f, flags = TTY_NORMAL; |
| 1417 | int i; |
| 1418 | char buf[64]; |
| 1419 | unsigned long cpuflags; |
| 1420 | |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1421 | if (ldata->real_raw) { |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 1422 | raw_spin_lock_irqsave(&ldata->read_lock, cpuflags); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1423 | i = min(N_TTY_BUF_SIZE - ldata->read_cnt, |
| 1424 | N_TTY_BUF_SIZE - ldata->read_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | i = min(count, i); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1426 | memcpy(ldata->read_buf + ldata->read_head, cp, i); |
| 1427 | ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1); |
| 1428 | ldata->read_cnt += i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | cp += i; |
| 1430 | count -= i; |
| 1431 | |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1432 | i = min(N_TTY_BUF_SIZE - ldata->read_cnt, |
| 1433 | N_TTY_BUF_SIZE - ldata->read_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | i = min(count, i); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1435 | memcpy(ldata->read_buf + ldata->read_head, cp, i); |
| 1436 | ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1); |
| 1437 | ldata->read_cnt += i; |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 1438 | raw_spin_unlock_irqrestore(&ldata->read_lock, cpuflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | } else { |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1440 | for (i = count, p = cp, f = fp; i; i--, p++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | if (f) |
| 1442 | flags = *f++; |
| 1443 | switch (flags) { |
| 1444 | case TTY_NORMAL: |
| 1445 | n_tty_receive_char(tty, *p); |
| 1446 | break; |
| 1447 | case TTY_BREAK: |
| 1448 | n_tty_receive_break(tty); |
| 1449 | break; |
| 1450 | case TTY_PARITY: |
| 1451 | case TTY_FRAME: |
| 1452 | n_tty_receive_parity_error(tty, *p); |
| 1453 | break; |
| 1454 | case TTY_OVERRUN: |
| 1455 | n_tty_receive_overrun(tty); |
| 1456 | break; |
| 1457 | default: |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1458 | printk(KERN_ERR "%s: unknown flag %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | tty_name(tty, buf), flags); |
| 1460 | break; |
| 1461 | } |
| 1462 | } |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1463 | if (tty->ops->flush_chars) |
| 1464 | tty->ops->flush_chars(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
Peter Hurley | 7879a9f | 2013-06-15 07:28:31 -0400 | [diff] [blame] | 1467 | set_room(tty); |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 1468 | |
Peter Hurley | f6c8dbe | 2013-06-15 07:28:28 -0400 | [diff] [blame] | 1469 | if ((!ldata->icanon && (ldata->read_cnt >= ldata->minimum_to_wake)) || |
hyc@symas.com | 26df6d1 | 2010-06-22 10:14:49 -0700 | [diff] [blame] | 1470 | L_EXTPROC(tty)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | kill_fasync(&tty->fasync, SIGIO, POLL_IN); |
| 1472 | if (waitqueue_active(&tty->read_wait)) |
| 1473 | wake_up_interruptible(&tty->read_wait); |
| 1474 | } |
| 1475 | |
| 1476 | /* |
| 1477 | * Check the remaining room for the input canonicalization |
| 1478 | * mode. We don't want to throttle the driver if we're in |
| 1479 | * canonical mode and don't have a newline yet! |
| 1480 | */ |
Peter Hurley | e91e52e | 2013-03-06 08:20:53 -0500 | [diff] [blame] | 1481 | while (1) { |
| 1482 | tty_set_flow_change(tty, TTY_THROTTLE_SAFE); |
| 1483 | if (tty->receive_room >= TTY_THRESHOLD_THROTTLE) |
| 1484 | break; |
| 1485 | if (!tty_throttle_safe(tty)) |
| 1486 | break; |
| 1487 | } |
| 1488 | __tty_set_flow_change(tty, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | int is_ignored(int sig) |
| 1492 | { |
| 1493 | return (sigismember(¤t->blocked, sig) || |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1494 | current->sighand->action[sig-1].sa.sa_handler == SIG_IGN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | /** |
| 1498 | * n_tty_set_termios - termios data changed |
| 1499 | * @tty: terminal |
| 1500 | * @old: previous data |
| 1501 | * |
| 1502 | * Called by the tty layer when the user changes termios flags so |
| 1503 | * that the line discipline can plan ahead. This function cannot sleep |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1504 | * and is protected from re-entry by the tty layer. The user is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | * guaranteed that this function will not be re-entered or in progress |
| 1506 | * when the ldisc is closed. |
Alan Cox | 17b8206 | 2008-10-13 10:45:06 +0100 | [diff] [blame] | 1507 | * |
| 1508 | * Locking: Caller holds tty->termios_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1510 | |
| 1511 | static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | { |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1513 | struct n_tty_data *ldata = tty->disc_data; |
Alan Cox | 47afa7a | 2008-10-13 10:44:17 +0100 | [diff] [blame] | 1514 | int canon_change = 1; |
Alan Cox | 47afa7a | 2008-10-13 10:44:17 +0100 | [diff] [blame] | 1515 | |
| 1516 | if (old) |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 1517 | canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON; |
Alan Cox | 47afa7a | 2008-10-13 10:44:17 +0100 | [diff] [blame] | 1518 | if (canon_change) { |
Jiri Slaby | 3fe780b | 2012-10-18 22:26:40 +0200 | [diff] [blame] | 1519 | bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1520 | ldata->canon_head = ldata->read_tail; |
| 1521 | ldata->canon_data = 0; |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1522 | ldata->erasing = 0; |
Alan Cox | 47afa7a | 2008-10-13 10:44:17 +0100 | [diff] [blame] | 1523 | } |
| 1524 | |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1525 | if (canon_change && !L_ICANON(tty) && ldata->read_cnt) |
Alan Cox | 47afa7a | 2008-10-13 10:44:17 +0100 | [diff] [blame] | 1526 | wake_up_interruptible(&tty->read_wait); |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1527 | |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1528 | ldata->icanon = (L_ICANON(tty) != 0); |
Peter Hurley | 582f559 | 2013-05-17 12:49:48 -0400 | [diff] [blame] | 1529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) || |
| 1531 | I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) || |
| 1532 | I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) || |
| 1533 | I_PARMRK(tty)) { |
Jiri Slaby | 3fe780b | 2012-10-18 22:26:40 +0200 | [diff] [blame] | 1534 | bitmap_zero(ldata->process_char_map, 256); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | |
| 1536 | if (I_IGNCR(tty) || I_ICRNL(tty)) |
Jiri Slaby | 3fe780b | 2012-10-18 22:26:40 +0200 | [diff] [blame] | 1537 | set_bit('\r', ldata->process_char_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | if (I_INLCR(tty)) |
Jiri Slaby | 3fe780b | 2012-10-18 22:26:40 +0200 | [diff] [blame] | 1539 | set_bit('\n', ldata->process_char_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | |
| 1541 | if (L_ICANON(tty)) { |
Jiri Slaby | 3fe780b | 2012-10-18 22:26:40 +0200 | [diff] [blame] | 1542 | set_bit(ERASE_CHAR(tty), ldata->process_char_map); |
| 1543 | set_bit(KILL_CHAR(tty), ldata->process_char_map); |
| 1544 | set_bit(EOF_CHAR(tty), ldata->process_char_map); |
| 1545 | set_bit('\n', ldata->process_char_map); |
| 1546 | set_bit(EOL_CHAR(tty), ldata->process_char_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | if (L_IEXTEN(tty)) { |
| 1548 | set_bit(WERASE_CHAR(tty), |
Jiri Slaby | 3fe780b | 2012-10-18 22:26:40 +0200 | [diff] [blame] | 1549 | ldata->process_char_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | set_bit(LNEXT_CHAR(tty), |
Jiri Slaby | 3fe780b | 2012-10-18 22:26:40 +0200 | [diff] [blame] | 1551 | ldata->process_char_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | set_bit(EOL2_CHAR(tty), |
Jiri Slaby | 3fe780b | 2012-10-18 22:26:40 +0200 | [diff] [blame] | 1553 | ldata->process_char_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | if (L_ECHO(tty)) |
| 1555 | set_bit(REPRINT_CHAR(tty), |
Jiri Slaby | 3fe780b | 2012-10-18 22:26:40 +0200 | [diff] [blame] | 1556 | ldata->process_char_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | } |
| 1558 | } |
| 1559 | if (I_IXON(tty)) { |
Jiri Slaby | 3fe780b | 2012-10-18 22:26:40 +0200 | [diff] [blame] | 1560 | set_bit(START_CHAR(tty), ldata->process_char_map); |
| 1561 | set_bit(STOP_CHAR(tty), ldata->process_char_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | } |
| 1563 | if (L_ISIG(tty)) { |
Jiri Slaby | 3fe780b | 2012-10-18 22:26:40 +0200 | [diff] [blame] | 1564 | set_bit(INTR_CHAR(tty), ldata->process_char_map); |
| 1565 | set_bit(QUIT_CHAR(tty), ldata->process_char_map); |
| 1566 | set_bit(SUSP_CHAR(tty), ldata->process_char_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | } |
Jiri Slaby | 3fe780b | 2012-10-18 22:26:40 +0200 | [diff] [blame] | 1568 | clear_bit(__DISABLED_CHAR, ldata->process_char_map); |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1569 | ldata->raw = 0; |
| 1570 | ldata->real_raw = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | } else { |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1572 | ldata->raw = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) && |
| 1574 | (I_IGNPAR(tty) || !I_INPCK(tty)) && |
| 1575 | (tty->driver->flags & TTY_DRIVER_REAL_RAW)) |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1576 | ldata->real_raw = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | else |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1578 | ldata->real_raw = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | } |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 1580 | n_tty_set_room(tty); |
Wang YanQing | dab73b4 | 2013-05-09 14:16:47 +0800 | [diff] [blame] | 1581 | /* |
| 1582 | * Fix tty hang when I_IXON(tty) is cleared, but the tty |
| 1583 | * been stopped by STOP_CHAR(tty) before it. |
| 1584 | */ |
| 1585 | if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) { |
| 1586 | start_tty(tty); |
| 1587 | } |
| 1588 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1589 | /* The termios change make the tty ready for I/O */ |
| 1590 | wake_up_interruptible(&tty->write_wait); |
| 1591 | wake_up_interruptible(&tty->read_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | /** |
| 1595 | * n_tty_close - close the ldisc for this tty |
| 1596 | * @tty: device |
| 1597 | * |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1598 | * Called from the terminal layer when this line discipline is |
| 1599 | * being shut down, either because of a close or becsuse of a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | * discipline change. The function will not be called while other |
| 1601 | * ldisc methods are in progress. |
| 1602 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | static void n_tty_close(struct tty_struct *tty) |
| 1605 | { |
Jiri Slaby | 70ece7a | 2012-10-18 22:26:38 +0200 | [diff] [blame] | 1606 | struct n_tty_data *ldata = tty->disc_data; |
| 1607 | |
Peter Hurley | 7990131 | 2013-03-11 16:44:23 -0400 | [diff] [blame] | 1608 | if (tty->link) |
| 1609 | n_tty_packet_mode_flush(tty); |
| 1610 | |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1611 | kfree(ldata->read_buf); |
| 1612 | kfree(ldata->echo_buf); |
Jiri Slaby | 70ece7a | 2012-10-18 22:26:38 +0200 | [diff] [blame] | 1613 | kfree(ldata); |
Jiri Slaby | 70ece7a | 2012-10-18 22:26:38 +0200 | [diff] [blame] | 1614 | tty->disc_data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | /** |
| 1618 | * n_tty_open - open an ldisc |
| 1619 | * @tty: terminal to open |
| 1620 | * |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1621 | * Called when this line discipline is being attached to the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | * terminal device. Can sleep. Called serialized so that no |
| 1623 | * other events will occur in parallel. No further open will occur |
| 1624 | * until a close. |
| 1625 | */ |
| 1626 | |
| 1627 | static int n_tty_open(struct tty_struct *tty) |
| 1628 | { |
Jiri Slaby | 70ece7a | 2012-10-18 22:26:38 +0200 | [diff] [blame] | 1629 | struct n_tty_data *ldata; |
| 1630 | |
| 1631 | ldata = kzalloc(sizeof(*ldata), GFP_KERNEL); |
| 1632 | if (!ldata) |
| 1633 | goto err; |
| 1634 | |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1635 | ldata->overrun_time = jiffies; |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 1636 | mutex_init(&ldata->atomic_read_lock); |
| 1637 | mutex_init(&ldata->output_lock); |
| 1638 | mutex_init(&ldata->echo_lock); |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 1639 | raw_spin_lock_init(&ldata->read_lock); |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1640 | |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 1641 | /* These are ugly. Currently a malloc failure here can panic */ |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1642 | ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL); |
| 1643 | ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL); |
| 1644 | if (!ldata->read_buf || !ldata->echo_buf) |
Jiri Slaby | b91939f | 2012-10-18 22:26:35 +0200 | [diff] [blame] | 1645 | goto err_free_bufs; |
Alan Cox | 0b4068a | 2009-06-11 13:05:49 +0100 | [diff] [blame] | 1646 | |
Jiri Slaby | 70ece7a | 2012-10-18 22:26:38 +0200 | [diff] [blame] | 1647 | tty->disc_data = ldata; |
Peter Hurley | b66f4fa | 2013-03-11 16:44:32 -0400 | [diff] [blame] | 1648 | reset_buffer_flags(tty->disc_data); |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1649 | ldata->column = 0; |
Peter Hurley | f6c8dbe | 2013-06-15 07:28:28 -0400 | [diff] [blame] | 1650 | ldata->minimum_to_wake = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | tty->closing = 0; |
Peter Hurley | b66f4fa | 2013-03-11 16:44:32 -0400 | [diff] [blame] | 1652 | /* indicate buffer work may resume */ |
| 1653 | clear_bit(TTY_LDISC_HALTED, &tty->flags); |
| 1654 | n_tty_set_termios(tty, NULL); |
| 1655 | tty_unthrottle(tty); |
Jiri Slaby | 70ece7a | 2012-10-18 22:26:38 +0200 | [diff] [blame] | 1656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | return 0; |
Jiri Slaby | b91939f | 2012-10-18 22:26:35 +0200 | [diff] [blame] | 1658 | err_free_bufs: |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1659 | kfree(ldata->read_buf); |
| 1660 | kfree(ldata->echo_buf); |
Jiri Slaby | 70ece7a | 2012-10-18 22:26:38 +0200 | [diff] [blame] | 1661 | kfree(ldata); |
| 1662 | err: |
Jiri Slaby | b91939f | 2012-10-18 22:26:35 +0200 | [diff] [blame] | 1663 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | static inline int input_available_p(struct tty_struct *tty, int amt) |
| 1667 | { |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1668 | struct n_tty_data *ldata = tty->disc_data; |
| 1669 | |
OGAWA Hirofumi | e043e42 | 2009-07-29 12:15:56 -0700 | [diff] [blame] | 1670 | tty_flush_to_ldisc(tty); |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1671 | if (ldata->icanon && !L_EXTPROC(tty)) { |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1672 | if (ldata->canon_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | return 1; |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1674 | } else if (ldata->read_cnt >= (amt ? amt : 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | return 1; |
| 1676 | |
| 1677 | return 0; |
| 1678 | } |
| 1679 | |
| 1680 | /** |
Thorsten Wißmann | bbd2075 | 2011-12-08 17:47:33 +0100 | [diff] [blame] | 1681 | * copy_from_read_buf - copy read data directly |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | * @tty: terminal device |
| 1683 | * @b: user data |
| 1684 | * @nr: size of data |
| 1685 | * |
Alan Cox | 11a96d1 | 2008-10-13 10:46:24 +0100 | [diff] [blame] | 1686 | * Helper function to speed up n_tty_read. It is only called when |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | * ICANON is off; it copies characters straight from the tty queue to |
| 1688 | * user space directly. It can be profitably called twice; once to |
| 1689 | * drain the space from the tail pointer to the (physical) end of the |
| 1690 | * buffer, and once to drain the space from the (physical) beginning of |
| 1691 | * the buffer to head pointer. |
| 1692 | * |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 1693 | * Called under the ldata->atomic_read_lock sem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | * |
| 1695 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1696 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1697 | static int copy_from_read_buf(struct tty_struct *tty, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | unsigned char __user **b, |
| 1699 | size_t *nr) |
| 1700 | |
| 1701 | { |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1702 | struct n_tty_data *ldata = tty->disc_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | int retval; |
| 1704 | size_t n; |
| 1705 | unsigned long flags; |
Jiri Slaby | 3fa10cc | 2012-04-26 20:13:00 +0200 | [diff] [blame] | 1706 | bool is_eof; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | |
| 1708 | retval = 0; |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 1709 | raw_spin_lock_irqsave(&ldata->read_lock, flags); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1710 | n = min(ldata->read_cnt, N_TTY_BUF_SIZE - ldata->read_tail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | n = min(*nr, n); |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 1712 | raw_spin_unlock_irqrestore(&ldata->read_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | if (n) { |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1714 | retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | n -= retval; |
Jiri Slaby | 3fa10cc | 2012-04-26 20:13:00 +0200 | [diff] [blame] | 1716 | is_eof = n == 1 && |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1717 | ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty); |
| 1718 | tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n, |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1719 | ldata->icanon); |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 1720 | raw_spin_lock_irqsave(&ldata->read_lock, flags); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1721 | ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1); |
| 1722 | ldata->read_cnt -= n; |
hyc@symas.com | 26df6d1 | 2010-06-22 10:14:49 -0700 | [diff] [blame] | 1723 | /* Turn single EOF into zero-length read */ |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1724 | if (L_EXTPROC(tty) && ldata->icanon && is_eof && !ldata->read_cnt) |
Jiri Slaby | 3fa10cc | 2012-04-26 20:13:00 +0200 | [diff] [blame] | 1725 | n = 0; |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 1726 | raw_spin_unlock_irqrestore(&ldata->read_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | *b += n; |
| 1728 | *nr -= n; |
| 1729 | } |
| 1730 | return retval; |
| 1731 | } |
| 1732 | |
Al Viro | cc4191d | 2008-03-29 03:08:48 +0000 | [diff] [blame] | 1733 | extern ssize_t redirected_tty_write(struct file *, const char __user *, |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1734 | size_t, loff_t *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | |
| 1736 | /** |
| 1737 | * job_control - check job control |
| 1738 | * @tty: tty |
| 1739 | * @file: file handle |
| 1740 | * |
| 1741 | * Perform job control management checks on this file/tty descriptor |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1742 | * and if appropriate send any needed signals and return a negative |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | * error code if action should be taken. |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1744 | * |
Peter Hurley | 01a5e44 | 2013-03-06 08:38:20 -0500 | [diff] [blame] | 1745 | * Locking: redirected write test is safe |
| 1746 | * current->signal->tty check is safe |
| 1747 | * ctrl_lock to safely reference tty->pgrp |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | static int job_control(struct tty_struct *tty, struct file *file) |
| 1751 | { |
| 1752 | /* Job control check -- must be done at start and after |
| 1753 | every sleep (POSIX.1 7.1.1.4). */ |
| 1754 | /* NOTE: not yet done after every sleep pending a thorough |
| 1755 | check of the logic of this change. -- jlc */ |
| 1756 | /* don't stop on /dev/console */ |
Peter Hurley | 01a5e44 | 2013-03-06 08:38:20 -0500 | [diff] [blame] | 1757 | if (file->f_op->write == redirected_tty_write || |
| 1758 | current->signal->tty != tty) |
| 1759 | return 0; |
| 1760 | |
| 1761 | spin_lock_irq(&tty->ctrl_lock); |
| 1762 | if (!tty->pgrp) |
| 1763 | printk(KERN_ERR "n_tty_read: no tty->pgrp!\n"); |
| 1764 | else if (task_pgrp(current) != tty->pgrp) { |
| 1765 | spin_unlock_irq(&tty->ctrl_lock); |
| 1766 | if (is_ignored(SIGTTIN) || is_current_pgrp_orphaned()) |
| 1767 | return -EIO; |
| 1768 | kill_pgrp(task_pgrp(current), SIGTTIN, 1); |
| 1769 | set_thread_flag(TIF_SIGPENDING); |
| 1770 | return -ERESTARTSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | } |
Peter Hurley | 01a5e44 | 2013-03-06 08:38:20 -0500 | [diff] [blame] | 1772 | spin_unlock_irq(&tty->ctrl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | return 0; |
| 1774 | } |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | |
| 1777 | /** |
Alan Cox | 11a96d1 | 2008-10-13 10:46:24 +0100 | [diff] [blame] | 1778 | * n_tty_read - read function for tty |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | * @tty: tty device |
| 1780 | * @file: file object |
| 1781 | * @buf: userspace buffer pointer |
| 1782 | * @nr: size of I/O |
| 1783 | * |
| 1784 | * Perform reads for the line discipline. We are guaranteed that the |
| 1785 | * line discipline will not be closed under us but we may get multiple |
| 1786 | * parallel readers and must handle this ourselves. We may also get |
| 1787 | * a hangup. Always called in user context, may sleep. |
| 1788 | * |
| 1789 | * This code must be sure never to sleep through a hangup. |
| 1790 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1791 | |
Alan Cox | 11a96d1 | 2008-10-13 10:46:24 +0100 | [diff] [blame] | 1792 | static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | unsigned char __user *buf, size_t nr) |
| 1794 | { |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1795 | struct n_tty_data *ldata = tty->disc_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | unsigned char __user *b = buf; |
| 1797 | DECLARE_WAITQUEUE(wait, current); |
| 1798 | int c; |
| 1799 | int minimum, time; |
| 1800 | ssize_t retval = 0; |
| 1801 | ssize_t size; |
| 1802 | long timeout; |
| 1803 | unsigned long flags; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1804 | int packet; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | |
| 1806 | do_it_again: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | c = job_control(tty, file); |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1808 | if (c < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | return c; |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | minimum = time = 0; |
| 1812 | timeout = MAX_SCHEDULE_TIMEOUT; |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1813 | if (!ldata->icanon) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | minimum = MIN_CHAR(tty); |
| 1815 | if (minimum) { |
Peter Hurley | a6e5431 | 2013-06-15 07:28:29 -0400 | [diff] [blame] | 1816 | time = (HZ / 10) * TIME_CHAR(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | if (time) |
Peter Hurley | f6c8dbe | 2013-06-15 07:28:28 -0400 | [diff] [blame] | 1818 | ldata->minimum_to_wake = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | else if (!waitqueue_active(&tty->read_wait) || |
Peter Hurley | f6c8dbe | 2013-06-15 07:28:28 -0400 | [diff] [blame] | 1820 | (ldata->minimum_to_wake > minimum)) |
| 1821 | ldata->minimum_to_wake = minimum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | } else { |
Peter Hurley | a6e5431 | 2013-06-15 07:28:29 -0400 | [diff] [blame] | 1823 | timeout = (HZ / 10) * TIME_CHAR(tty); |
Peter Hurley | f6c8dbe | 2013-06-15 07:28:28 -0400 | [diff] [blame] | 1824 | ldata->minimum_to_wake = minimum = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | } |
| 1826 | } |
| 1827 | |
| 1828 | /* |
| 1829 | * Internal serialization of reads. |
| 1830 | */ |
| 1831 | if (file->f_flags & O_NONBLOCK) { |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 1832 | if (!mutex_trylock(&ldata->atomic_read_lock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | return -EAGAIN; |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1834 | } else { |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 1835 | if (mutex_lock_interruptible(&ldata->atomic_read_lock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | return -ERESTARTSYS; |
| 1837 | } |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1838 | packet = tty->packet; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | |
| 1840 | add_wait_queue(&tty->read_wait, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | while (nr) { |
| 1842 | /* First test for status change. */ |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1843 | if (packet && tty->link->ctrl_status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | unsigned char cs; |
| 1845 | if (b != buf) |
| 1846 | break; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1847 | spin_lock_irqsave(&tty->link->ctrl_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | cs = tty->link->ctrl_status; |
| 1849 | tty->link->ctrl_status = 0; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1850 | spin_unlock_irqrestore(&tty->link->ctrl_lock, flags); |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1851 | if (tty_put_user(tty, cs, b++)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | retval = -EFAULT; |
| 1853 | b--; |
| 1854 | break; |
| 1855 | } |
| 1856 | nr--; |
| 1857 | break; |
| 1858 | } |
| 1859 | /* This statement must be first before checking for input |
| 1860 | so that any interrupt will set the state back to |
| 1861 | TASK_RUNNING. */ |
| 1862 | set_current_state(TASK_INTERRUPTIBLE); |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1863 | |
Peter Hurley | f6c8dbe | 2013-06-15 07:28:28 -0400 | [diff] [blame] | 1864 | if (((minimum - (b - buf)) < ldata->minimum_to_wake) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | ((minimum - (b - buf)) >= 1)) |
Peter Hurley | f6c8dbe | 2013-06-15 07:28:28 -0400 | [diff] [blame] | 1866 | ldata->minimum_to_wake = (minimum - (b - buf)); |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | if (!input_available_p(tty, 0)) { |
| 1869 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) { |
| 1870 | retval = -EIO; |
| 1871 | break; |
| 1872 | } |
| 1873 | if (tty_hung_up_p(file)) |
| 1874 | break; |
| 1875 | if (!timeout) |
| 1876 | break; |
| 1877 | if (file->f_flags & O_NONBLOCK) { |
| 1878 | retval = -EAGAIN; |
| 1879 | break; |
| 1880 | } |
| 1881 | if (signal_pending(current)) { |
| 1882 | retval = -ERESTARTSYS; |
| 1883 | break; |
| 1884 | } |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 1885 | n_tty_set_room(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | timeout = schedule_timeout(timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | continue; |
| 1888 | } |
| 1889 | __set_current_state(TASK_RUNNING); |
| 1890 | |
| 1891 | /* Deal with packet mode. */ |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1892 | if (packet && b == buf) { |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1893 | if (tty_put_user(tty, TIOCPKT_DATA, b++)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | retval = -EFAULT; |
| 1895 | b--; |
| 1896 | break; |
| 1897 | } |
| 1898 | nr--; |
| 1899 | } |
| 1900 | |
Jiri Slaby | 53c5ee2 | 2012-10-18 22:26:39 +0200 | [diff] [blame] | 1901 | if (ldata->icanon && !L_EXTPROC(tty)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | /* N.B. avoid overrun if nr == 0 */ |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 1903 | raw_spin_lock_irqsave(&ldata->read_lock, flags); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1904 | while (nr && ldata->read_cnt) { |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1905 | int eol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1907 | eol = test_and_clear_bit(ldata->read_tail, |
Jiri Slaby | 3fe780b | 2012-10-18 22:26:40 +0200 | [diff] [blame] | 1908 | ldata->read_flags); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1909 | c = ldata->read_buf[ldata->read_tail]; |
| 1910 | ldata->read_tail = ((ldata->read_tail+1) & |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | (N_TTY_BUF_SIZE-1)); |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1912 | ldata->read_cnt--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | if (eol) { |
| 1914 | /* this test should be redundant: |
| 1915 | * we shouldn't be reading data if |
| 1916 | * canon_data is 0 |
| 1917 | */ |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 1918 | if (--ldata->canon_data < 0) |
| 1919 | ldata->canon_data = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | } |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 1921 | raw_spin_unlock_irqrestore(&ldata->read_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | |
| 1923 | if (!eol || (c != __DISABLED_CHAR)) { |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1924 | if (tty_put_user(tty, c, b++)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | retval = -EFAULT; |
| 1926 | b--; |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 1927 | raw_spin_lock_irqsave(&ldata->read_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | break; |
| 1929 | } |
| 1930 | nr--; |
| 1931 | } |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1932 | if (eol) { |
| 1933 | tty_audit_push(tty); |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 1934 | raw_spin_lock_irqsave(&ldata->read_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | break; |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1936 | } |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 1937 | raw_spin_lock_irqsave(&ldata->read_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | } |
Ivo Sieben | 9800121 | 2013-01-28 13:32:01 +0100 | [diff] [blame] | 1939 | raw_spin_unlock_irqrestore(&ldata->read_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | if (retval) |
| 1941 | break; |
| 1942 | } else { |
| 1943 | int uncopied; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1944 | /* The copy function takes the read lock and handles |
| 1945 | locking internally for this case */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | uncopied = copy_from_read_buf(tty, &b, &nr); |
| 1947 | uncopied += copy_from_read_buf(tty, &b, &nr); |
| 1948 | if (uncopied) { |
| 1949 | retval = -EFAULT; |
| 1950 | break; |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | /* If there is enough space in the read buffer now, let the |
| 1955 | * low-level driver know. We use n_tty_chars_in_buffer() to |
| 1956 | * check the buffer, as it now knows about canonical mode. |
| 1957 | * Otherwise, if the driver is throttled and the line is |
| 1958 | * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode, |
| 1959 | * we won't get any more characters. |
| 1960 | */ |
Peter Hurley | e91e52e | 2013-03-06 08:20:53 -0500 | [diff] [blame] | 1961 | while (1) { |
| 1962 | tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE); |
| 1963 | if (n_tty_chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE) |
| 1964 | break; |
| 1965 | if (!tty->count) |
| 1966 | break; |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 1967 | n_tty_set_room(tty); |
Peter Hurley | e91e52e | 2013-03-06 08:20:53 -0500 | [diff] [blame] | 1968 | if (!tty_unthrottle_safe(tty)) |
| 1969 | break; |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 1970 | } |
Peter Hurley | e91e52e | 2013-03-06 08:20:53 -0500 | [diff] [blame] | 1971 | __tty_set_flow_change(tty, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | |
| 1973 | if (b - buf >= minimum) |
| 1974 | break; |
| 1975 | if (time) |
| 1976 | timeout = time; |
| 1977 | } |
Jiri Slaby | bddc715 | 2012-10-18 22:26:42 +0200 | [diff] [blame] | 1978 | mutex_unlock(&ldata->atomic_read_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | remove_wait_queue(&tty->read_wait, &wait); |
| 1980 | |
| 1981 | if (!waitqueue_active(&tty->read_wait)) |
Peter Hurley | f6c8dbe | 2013-06-15 07:28:28 -0400 | [diff] [blame] | 1982 | ldata->minimum_to_wake = minimum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | |
| 1984 | __set_current_state(TASK_RUNNING); |
| 1985 | size = b - buf; |
| 1986 | if (size) { |
| 1987 | retval = size; |
| 1988 | if (nr) |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1989 | clear_bit(TTY_PUSH, &tty->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | } else if (test_and_clear_bit(TTY_PUSH, &tty->flags)) |
Thorsten Wißmann | bbd2075 | 2011-12-08 17:47:33 +0100 | [diff] [blame] | 1991 | goto do_it_again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 1993 | n_tty_set_room(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | return retval; |
| 1995 | } |
| 1996 | |
| 1997 | /** |
Alan Cox | 11a96d1 | 2008-10-13 10:46:24 +0100 | [diff] [blame] | 1998 | * n_tty_write - write function for tty |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | * @tty: tty device |
| 2000 | * @file: file object |
| 2001 | * @buf: userspace buffer pointer |
| 2002 | * @nr: size of I/O |
| 2003 | * |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 2004 | * Write function of the terminal device. This is serialized with |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | * respect to other write callers but not to termios changes, reads |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 2006 | * and other such events. Since the receive code will echo characters, |
| 2007 | * thus calling driver write methods, the output_lock is used in |
| 2008 | * the output processing functions called here as well as in the |
| 2009 | * echo processing function to protect the column state and space |
| 2010 | * left in the buffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | * |
| 2012 | * This code must be sure never to sleep through a hangup. |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 2013 | * |
| 2014 | * Locking: output_lock to protect column state and space left |
| 2015 | * (note that the process_output*() functions take this |
| 2016 | * lock themselves) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2017 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 2018 | |
Alan Cox | 11a96d1 | 2008-10-13 10:46:24 +0100 | [diff] [blame] | 2019 | static ssize_t n_tty_write(struct tty_struct *tty, struct file *file, |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 2020 | const unsigned char *buf, size_t nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | { |
| 2022 | const unsigned char *b = buf; |
| 2023 | DECLARE_WAITQUEUE(wait, current); |
| 2024 | int c; |
| 2025 | ssize_t retval = 0; |
| 2026 | |
| 2027 | /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */ |
| 2028 | if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) { |
| 2029 | retval = tty_check_change(tty); |
| 2030 | if (retval) |
| 2031 | return retval; |
| 2032 | } |
| 2033 | |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 2034 | /* Write out any echoed characters that are still pending */ |
| 2035 | process_echoes(tty); |
Alan Cox | 300a620 | 2009-01-02 13:41:04 +0000 | [diff] [blame] | 2036 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | add_wait_queue(&tty->write_wait, &wait); |
| 2038 | while (1) { |
| 2039 | set_current_state(TASK_INTERRUPTIBLE); |
| 2040 | if (signal_pending(current)) { |
| 2041 | retval = -ERESTARTSYS; |
| 2042 | break; |
| 2043 | } |
| 2044 | if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) { |
| 2045 | retval = -EIO; |
| 2046 | break; |
| 2047 | } |
Peter Hurley | 582f559 | 2013-05-17 12:49:48 -0400 | [diff] [blame] | 2048 | if (O_OPOST(tty)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | while (nr > 0) { |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 2050 | ssize_t num = process_output_block(tty, b, nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | if (num < 0) { |
| 2052 | if (num == -EAGAIN) |
| 2053 | break; |
| 2054 | retval = num; |
| 2055 | goto break_out; |
| 2056 | } |
| 2057 | b += num; |
| 2058 | nr -= num; |
| 2059 | if (nr == 0) |
| 2060 | break; |
| 2061 | c = *b; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 2062 | if (process_output(c, tty) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | break; |
| 2064 | b++; nr--; |
| 2065 | } |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2066 | if (tty->ops->flush_chars) |
| 2067 | tty->ops->flush_chars(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | } else { |
Roman Zippel | d6afe27 | 2005-07-07 17:56:55 -0700 | [diff] [blame] | 2069 | while (nr > 0) { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2070 | c = tty->ops->write(tty, b, nr); |
Roman Zippel | d6afe27 | 2005-07-07 17:56:55 -0700 | [diff] [blame] | 2071 | if (c < 0) { |
| 2072 | retval = c; |
| 2073 | goto break_out; |
| 2074 | } |
| 2075 | if (!c) |
| 2076 | break; |
| 2077 | b += c; |
| 2078 | nr -= c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | } |
| 2081 | if (!nr) |
| 2082 | break; |
| 2083 | if (file->f_flags & O_NONBLOCK) { |
| 2084 | retval = -EAGAIN; |
| 2085 | break; |
| 2086 | } |
| 2087 | schedule(); |
| 2088 | } |
| 2089 | break_out: |
| 2090 | __set_current_state(TASK_RUNNING); |
| 2091 | remove_wait_queue(&tty->write_wait, &wait); |
Thomas Pfaff | ff8cb0f | 2009-01-02 13:47:13 +0000 | [diff] [blame] | 2092 | if (b - buf != nr && tty->fasync) |
| 2093 | set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | return (b - buf) ? b - buf : retval; |
| 2095 | } |
| 2096 | |
| 2097 | /** |
Alan Cox | 11a96d1 | 2008-10-13 10:46:24 +0100 | [diff] [blame] | 2098 | * n_tty_poll - poll method for N_TTY |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | * @tty: terminal device |
| 2100 | * @file: file accessing it |
| 2101 | * @wait: poll table |
| 2102 | * |
| 2103 | * Called when the line discipline is asked to poll() for data or |
| 2104 | * for special events. This code is not serialized with respect to |
| 2105 | * other events save open/close. |
| 2106 | * |
| 2107 | * This code must be sure never to sleep through a hangup. |
| 2108 | * Called without the kernel lock held - fine |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 2110 | |
Alan Cox | 11a96d1 | 2008-10-13 10:46:24 +0100 | [diff] [blame] | 2111 | static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file, |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 2112 | poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | { |
Peter Hurley | f6c8dbe | 2013-06-15 07:28:28 -0400 | [diff] [blame] | 2114 | struct n_tty_data *ldata = tty->disc_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | unsigned int mask = 0; |
| 2116 | |
| 2117 | poll_wait(file, &tty->read_wait, wait); |
| 2118 | poll_wait(file, &tty->write_wait, wait); |
| 2119 | if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty))) |
| 2120 | mask |= POLLIN | POLLRDNORM; |
| 2121 | if (tty->packet && tty->link->ctrl_status) |
| 2122 | mask |= POLLPRI | POLLIN | POLLRDNORM; |
| 2123 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) |
| 2124 | mask |= POLLHUP; |
| 2125 | if (tty_hung_up_p(file)) |
| 2126 | mask |= POLLHUP; |
| 2127 | if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) { |
| 2128 | if (MIN_CHAR(tty) && !TIME_CHAR(tty)) |
Peter Hurley | f6c8dbe | 2013-06-15 07:28:28 -0400 | [diff] [blame] | 2129 | ldata->minimum_to_wake = MIN_CHAR(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | else |
Peter Hurley | f6c8dbe | 2013-06-15 07:28:28 -0400 | [diff] [blame] | 2131 | ldata->minimum_to_wake = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | } |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2133 | if (tty->ops->write && !tty_is_writelocked(tty) && |
| 2134 | tty_chars_in_buffer(tty) < WAKEUP_CHARS && |
| 2135 | tty_write_room(tty) > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | mask |= POLLOUT | POLLWRNORM; |
| 2137 | return mask; |
| 2138 | } |
| 2139 | |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 2140 | static unsigned long inq_canon(struct n_tty_data *ldata) |
Alan Cox | 47afa7a | 2008-10-13 10:44:17 +0100 | [diff] [blame] | 2141 | { |
| 2142 | int nr, head, tail; |
| 2143 | |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 2144 | if (!ldata->canon_data) |
Alan Cox | 47afa7a | 2008-10-13 10:44:17 +0100 | [diff] [blame] | 2145 | return 0; |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 2146 | head = ldata->canon_head; |
| 2147 | tail = ldata->read_tail; |
Alan Cox | 47afa7a | 2008-10-13 10:44:17 +0100 | [diff] [blame] | 2148 | nr = (head - tail) & (N_TTY_BUF_SIZE-1); |
| 2149 | /* Skip EOF-chars.. */ |
| 2150 | while (head != tail) { |
Jiri Slaby | 3fe780b | 2012-10-18 22:26:40 +0200 | [diff] [blame] | 2151 | if (test_bit(tail, ldata->read_flags) && |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 2152 | ldata->read_buf[tail] == __DISABLED_CHAR) |
Alan Cox | 47afa7a | 2008-10-13 10:44:17 +0100 | [diff] [blame] | 2153 | nr--; |
| 2154 | tail = (tail+1) & (N_TTY_BUF_SIZE-1); |
| 2155 | } |
| 2156 | return nr; |
| 2157 | } |
| 2158 | |
| 2159 | static int n_tty_ioctl(struct tty_struct *tty, struct file *file, |
| 2160 | unsigned int cmd, unsigned long arg) |
| 2161 | { |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 2162 | struct n_tty_data *ldata = tty->disc_data; |
Alan Cox | 47afa7a | 2008-10-13 10:44:17 +0100 | [diff] [blame] | 2163 | int retval; |
| 2164 | |
| 2165 | switch (cmd) { |
| 2166 | case TIOCOUTQ: |
| 2167 | return put_user(tty_chars_in_buffer(tty), (int __user *) arg); |
| 2168 | case TIOCINQ: |
Alan Cox | 17b8206 | 2008-10-13 10:45:06 +0100 | [diff] [blame] | 2169 | /* FIXME: Locking */ |
Jiri Slaby | ba2e68a | 2012-10-18 22:26:41 +0200 | [diff] [blame] | 2170 | retval = ldata->read_cnt; |
Alan Cox | 47afa7a | 2008-10-13 10:44:17 +0100 | [diff] [blame] | 2171 | if (L_ICANON(tty)) |
Jiri Slaby | 57c9412 | 2012-10-18 22:26:43 +0200 | [diff] [blame] | 2172 | retval = inq_canon(ldata); |
Alan Cox | 47afa7a | 2008-10-13 10:44:17 +0100 | [diff] [blame] | 2173 | return put_user(retval, (unsigned int __user *) arg); |
| 2174 | default: |
| 2175 | return n_tty_ioctl_helper(tty, file, cmd, arg); |
| 2176 | } |
| 2177 | } |
| 2178 | |
Peter Hurley | f6c8dbe | 2013-06-15 07:28:28 -0400 | [diff] [blame] | 2179 | static void n_tty_fasync(struct tty_struct *tty, int on) |
| 2180 | { |
| 2181 | struct n_tty_data *ldata = tty->disc_data; |
| 2182 | |
| 2183 | if (!waitqueue_active(&tty->read_wait)) { |
| 2184 | if (on) |
| 2185 | ldata->minimum_to_wake = 1; |
| 2186 | else if (!tty->fasync) |
| 2187 | ldata->minimum_to_wake = N_TTY_BUF_SIZE; |
| 2188 | } |
| 2189 | } |
| 2190 | |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 2191 | struct tty_ldisc_ops tty_ldisc_N_TTY = { |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 2192 | .magic = TTY_LDISC_MAGIC, |
| 2193 | .name = "n_tty", |
| 2194 | .open = n_tty_open, |
| 2195 | .close = n_tty_close, |
| 2196 | .flush_buffer = n_tty_flush_buffer, |
| 2197 | .chars_in_buffer = n_tty_chars_in_buffer, |
Alan Cox | 11a96d1 | 2008-10-13 10:46:24 +0100 | [diff] [blame] | 2198 | .read = n_tty_read, |
| 2199 | .write = n_tty_write, |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 2200 | .ioctl = n_tty_ioctl, |
| 2201 | .set_termios = n_tty_set_termios, |
Alan Cox | 11a96d1 | 2008-10-13 10:46:24 +0100 | [diff] [blame] | 2202 | .poll = n_tty_poll, |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 2203 | .receive_buf = n_tty_receive_buf, |
Peter Hurley | f6c8dbe | 2013-06-15 07:28:28 -0400 | [diff] [blame] | 2204 | .write_wakeup = n_tty_write_wakeup, |
| 2205 | .fasync = n_tty_fasync, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | }; |
Rodolfo Giometti | 572b9ad | 2010-03-10 15:23:46 -0800 | [diff] [blame] | 2207 | |
| 2208 | /** |
| 2209 | * n_tty_inherit_ops - inherit N_TTY methods |
| 2210 | * @ops: struct tty_ldisc_ops where to save N_TTY methods |
| 2211 | * |
George Spelvin | 593fb1ae4 | 2013-02-12 02:00:43 -0500 | [diff] [blame] | 2212 | * Enables a 'subclass' line discipline to 'inherit' N_TTY |
Rodolfo Giometti | 572b9ad | 2010-03-10 15:23:46 -0800 | [diff] [blame] | 2213 | * methods. |
| 2214 | */ |
| 2215 | |
| 2216 | void n_tty_inherit_ops(struct tty_ldisc_ops *ops) |
| 2217 | { |
| 2218 | *ops = tty_ldisc_N_TTY; |
| 2219 | ops->owner = NULL; |
| 2220 | ops->refcount = ops->flags = 0; |
| 2221 | } |
| 2222 | EXPORT_SYMBOL_GPL(n_tty_inherit_ops); |