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