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