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