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