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