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