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