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