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