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