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