| 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>. | 
|  | 29 | *		Also fixed a bug in BLOCKING mode where write_chan returns | 
|  | 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> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 |  | 
|  | 51 | #include <asm/uaccess.h> | 
|  | 52 | #include <asm/system.h> | 
|  | 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 */ | 
|  | 63 | #define TTY_THRESHOLD_UNTHROTTLE 	128 | 
|  | 64 |  | 
|  | 65 | static inline unsigned char *alloc_buf(void) | 
|  | 66 | { | 
| Al Viro | b4e3ca1 | 2005-10-21 03:22:34 -0400 | [diff] [blame] | 67 | gfp_t prio = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 |  | 
|  | 69 | if (PAGE_SIZE != N_TTY_BUF_SIZE) | 
|  | 70 | return kmalloc(N_TTY_BUF_SIZE, prio); | 
|  | 71 | else | 
|  | 72 | return (unsigned char *)__get_free_page(prio); | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | static inline void free_buf(unsigned char *buf) | 
|  | 76 | { | 
|  | 77 | if (PAGE_SIZE != N_TTY_BUF_SIZE) | 
|  | 78 | kfree(buf); | 
|  | 79 | else | 
|  | 80 | free_page((unsigned long) buf); | 
|  | 81 | } | 
|  | 82 |  | 
| Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 83 | static inline int tty_put_user(struct tty_struct *tty, unsigned char x, | 
|  | 84 | unsigned char __user *ptr) | 
|  | 85 | { | 
|  | 86 | tty_audit_add_data(tty, &x, 1); | 
|  | 87 | return put_user(x, ptr); | 
|  | 88 | } | 
|  | 89 |  | 
| Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 90 | /** | 
|  | 91 | *	n_tty_set__room	-	receive space | 
|  | 92 | *	@tty: terminal | 
|  | 93 | * | 
|  | 94 | *	Called by the driver to find out how much data it is | 
|  | 95 | *	permitted to feed to the line discipline without any being lost | 
|  | 96 | *	and thus to manage flow control. Not serialized. Answers for the | 
|  | 97 | *	"instant". | 
|  | 98 | */ | 
|  | 99 |  | 
|  | 100 | static void n_tty_set_room(struct tty_struct *tty) | 
|  | 101 | { | 
|  | 102 | int	left = N_TTY_BUF_SIZE - tty->read_cnt - 1; | 
|  | 103 |  | 
|  | 104 | /* | 
|  | 105 | * If we are doing input canonicalization, and there are no | 
|  | 106 | * pending newlines, let characters through without limit, so | 
|  | 107 | * that erase characters will be handled.  Other excess | 
|  | 108 | * characters will be beeped. | 
|  | 109 | */ | 
|  | 110 | if (left <= 0) | 
|  | 111 | left = tty->icanon && !tty->canon_data; | 
|  | 112 | tty->receive_room = left; | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { | 
|  | 117 | if (tty->read_cnt < N_TTY_BUF_SIZE) { | 
|  | 118 | tty->read_buf[tty->read_head] = c; | 
|  | 119 | tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1); | 
|  | 120 | tty->read_cnt++; | 
|  | 121 | } | 
|  | 122 | } | 
|  | 123 |  | 
| Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 124 | static void put_tty_queue(unsigned char c, struct tty_struct *tty) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { | 
|  | 126 | unsigned long flags; | 
|  | 127 | /* | 
|  | 128 | *	The problem of stomping on the buffers ends here. | 
|  | 129 | *	Why didn't anyone see this one coming? --AJK | 
|  | 130 | */ | 
|  | 131 | spin_lock_irqsave(&tty->read_lock, flags); | 
|  | 132 | put_tty_queue_nolock(c, tty); | 
|  | 133 | spin_unlock_irqrestore(&tty->read_lock, flags); | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | /** | 
|  | 137 | *	check_unthrottle	-	allow new receive data | 
|  | 138 | *	@tty; tty device | 
|  | 139 | * | 
|  | 140 | *	Check whether to call the driver.unthrottle function. | 
|  | 141 | *	We test the TTY_THROTTLED bit first so that it always | 
|  | 142 | *	indicates the current state. The decision about whether | 
|  | 143 | *	it is worth allowing more input has been taken by the caller. | 
| Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 144 | *	Can sleep, may be called under the atomic_read_lock mutex but | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | *	this is not guaranteed. | 
|  | 146 | */ | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 147 |  | 
|  | 148 | static void check_unthrottle(struct tty_struct *tty) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { | 
| Alan Cox | 39c2e60 | 2008-04-30 00:54:18 -0700 | [diff] [blame] | 150 | if (tty->count) | 
|  | 151 | tty_unthrottle(tty); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } | 
|  | 153 |  | 
|  | 154 | /** | 
|  | 155 | *	reset_buffer_flags	-	reset buffer state | 
|  | 156 | *	@tty: terminal to reset | 
|  | 157 | * | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 158 | *	Reset the read buffer counters, clear the flags, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | *	and make sure the driver is unthrottled. Called | 
|  | 160 | *	from n_tty_open() and n_tty_flush_buffer(). | 
|  | 161 | */ | 
|  | 162 | static void reset_buffer_flags(struct tty_struct *tty) | 
|  | 163 | { | 
|  | 164 | unsigned long flags; | 
|  | 165 |  | 
|  | 166 | spin_lock_irqsave(&tty->read_lock, flags); | 
|  | 167 | tty->read_head = tty->read_tail = tty->read_cnt = 0; | 
|  | 168 | spin_unlock_irqrestore(&tty->read_lock, flags); | 
|  | 169 | tty->canon_head = tty->canon_data = tty->erasing = 0; | 
|  | 170 | memset(&tty->read_flags, 0, sizeof tty->read_flags); | 
| Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 171 | n_tty_set_room(tty); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | check_unthrottle(tty); | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | /** | 
|  | 176 | *	n_tty_flush_buffer	-	clean input queue | 
|  | 177 | *	@tty:	terminal device | 
|  | 178 | * | 
|  | 179 | *	Flush the input buffer. Called when the line discipline is | 
|  | 180 | *	being closed, when the tty layer wants the buffer flushed (eg | 
|  | 181 | *	at hangup) or when the N_TTY line discipline internally has to | 
|  | 182 | *	clean the pending queue (for example some signals). | 
|  | 183 | * | 
| Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 184 | *	Locking: ctrl_lock | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | */ | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 186 |  | 
|  | 187 | static void n_tty_flush_buffer(struct tty_struct *tty) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { | 
| Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 189 | unsigned long flags; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | /* clear everything and unthrottle the driver */ | 
|  | 191 | reset_buffer_flags(tty); | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 192 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | if (!tty->link) | 
|  | 194 | return; | 
|  | 195 |  | 
| Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 196 | spin_lock_irqsave(&tty->ctrl_lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | if (tty->link->packet) { | 
|  | 198 | tty->ctrl_status |= TIOCPKT_FLUSHREAD; | 
|  | 199 | wake_up_interruptible(&tty->link->read_wait); | 
|  | 200 | } | 
| Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 201 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } | 
|  | 203 |  | 
|  | 204 | /** | 
|  | 205 | *	n_tty_chars_in_buffer	-	report available bytes | 
|  | 206 | *	@tty: tty device | 
|  | 207 | * | 
|  | 208 | *	Report the number of characters buffered to be delivered to user | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 209 | *	at this instant in time. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | */ | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 211 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty) | 
|  | 213 | { | 
|  | 214 | unsigned long flags; | 
|  | 215 | ssize_t n = 0; | 
|  | 216 |  | 
|  | 217 | spin_lock_irqsave(&tty->read_lock, flags); | 
|  | 218 | if (!tty->icanon) { | 
|  | 219 | n = tty->read_cnt; | 
|  | 220 | } else if (tty->canon_data) { | 
|  | 221 | n = (tty->canon_head > tty->read_tail) ? | 
|  | 222 | tty->canon_head - tty->read_tail : | 
|  | 223 | tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail); | 
|  | 224 | } | 
|  | 225 | spin_unlock_irqrestore(&tty->read_lock, flags); | 
|  | 226 | return n; | 
|  | 227 | } | 
|  | 228 |  | 
|  | 229 | /** | 
|  | 230 | *	is_utf8_continuation	-	utf8 multibyte check | 
|  | 231 | *	@c: byte to check | 
|  | 232 | * | 
|  | 233 | *	Returns true if the utf8 character 'c' is a multibyte continuation | 
|  | 234 | *	character. We use this to correctly compute the on screen size | 
|  | 235 | *	of the character when printing | 
|  | 236 | */ | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 237 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | static inline int is_utf8_continuation(unsigned char c) | 
|  | 239 | { | 
|  | 240 | return (c & 0xc0) == 0x80; | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | /** | 
|  | 244 | *	is_continuation		-	multibyte check | 
|  | 245 | *	@c: byte to check | 
|  | 246 | * | 
|  | 247 | *	Returns true if the utf8 character 'c' is a multibyte continuation | 
|  | 248 | *	character and the terminal is in unicode mode. | 
|  | 249 | */ | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 250 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | static inline int is_continuation(unsigned char c, struct tty_struct *tty) | 
|  | 252 | { | 
|  | 253 | return I_IUTF8(tty) && is_utf8_continuation(c); | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | /** | 
|  | 257 | *	opost			-	output post processor | 
|  | 258 | *	@c: character (or partial unicode symbol) | 
|  | 259 | *	@tty: terminal device | 
|  | 260 | * | 
|  | 261 | *	Perform OPOST processing.  Returns -1 when the output device is | 
|  | 262 | *	full and the character must be retried. Note that Linux currently | 
|  | 263 | *	ignores TABDLY, CRDLY, VTDLY, FFDLY and NLDLY. They simply aren't | 
|  | 264 | *	relevant in the world today. If you ever need them, add them here. | 
|  | 265 | * | 
|  | 266 | *	Called from both the receive and transmit sides and can be called | 
| Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 267 | *	re-entrantly. Relies on lock_kernel() for tty->column state. | 
| 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 int opost(unsigned char c, struct tty_struct *tty) | 
|  | 271 | { | 
|  | 272 | int	space, spaces; | 
|  | 273 |  | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 274 | space = tty_write_room(tty); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | if (!space) | 
|  | 276 | return -1; | 
|  | 277 |  | 
| Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 278 | lock_kernel(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | if (O_OPOST(tty)) { | 
|  | 280 | switch (c) { | 
|  | 281 | case '\n': | 
|  | 282 | if (O_ONLRET(tty)) | 
|  | 283 | tty->column = 0; | 
|  | 284 | if (O_ONLCR(tty)) { | 
| Ingo Molnar | 487ad7e | 2008-05-14 17:11:46 +0200 | [diff] [blame] | 285 | if (space < 2) { | 
|  | 286 | unlock_kernel(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | return -1; | 
| Ingo Molnar | 487ad7e | 2008-05-14 17:11:46 +0200 | [diff] [blame] | 288 | } | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 289 | tty_put_char(tty, '\r'); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | tty->column = 0; | 
|  | 291 | } | 
|  | 292 | tty->canon_column = tty->column; | 
|  | 293 | break; | 
|  | 294 | case '\r': | 
| Ingo Molnar | 487ad7e | 2008-05-14 17:11:46 +0200 | [diff] [blame] | 295 | if (O_ONOCR(tty) && tty->column == 0) { | 
|  | 296 | unlock_kernel(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | return 0; | 
| Ingo Molnar | 487ad7e | 2008-05-14 17:11:46 +0200 | [diff] [blame] | 298 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | if (O_OCRNL(tty)) { | 
|  | 300 | c = '\n'; | 
|  | 301 | if (O_ONLRET(tty)) | 
|  | 302 | tty->canon_column = tty->column = 0; | 
|  | 303 | break; | 
|  | 304 | } | 
|  | 305 | tty->canon_column = tty->column = 0; | 
|  | 306 | break; | 
|  | 307 | case '\t': | 
|  | 308 | spaces = 8 - (tty->column & 7); | 
|  | 309 | if (O_TABDLY(tty) == XTABS) { | 
| Ingo Molnar | 487ad7e | 2008-05-14 17:11:46 +0200 | [diff] [blame] | 310 | if (space < spaces) { | 
|  | 311 | unlock_kernel(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | return -1; | 
| Ingo Molnar | 487ad7e | 2008-05-14 17:11:46 +0200 | [diff] [blame] | 313 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | tty->column += spaces; | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 315 | tty->ops->write(tty, "        ", spaces); | 
| Ingo Molnar | 487ad7e | 2008-05-14 17:11:46 +0200 | [diff] [blame] | 316 | unlock_kernel(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | return 0; | 
|  | 318 | } | 
|  | 319 | tty->column += spaces; | 
|  | 320 | break; | 
|  | 321 | case '\b': | 
|  | 322 | if (tty->column > 0) | 
|  | 323 | tty->column--; | 
|  | 324 | break; | 
|  | 325 | default: | 
|  | 326 | if (O_OLCUC(tty)) | 
|  | 327 | c = toupper(c); | 
|  | 328 | if (!iscntrl(c) && !is_continuation(c, tty)) | 
|  | 329 | tty->column++; | 
|  | 330 | break; | 
|  | 331 | } | 
|  | 332 | } | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 333 | tty_put_char(tty, c); | 
| Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 334 | unlock_kernel(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | return 0; | 
|  | 336 | } | 
|  | 337 |  | 
|  | 338 | /** | 
|  | 339 | *	opost_block		-	block postprocess | 
|  | 340 | *	@tty: terminal device | 
|  | 341 | *	@inbuf: user buffer | 
|  | 342 | *	@nr: number of bytes | 
|  | 343 | * | 
|  | 344 | *	This path is used to speed up block console writes, among other | 
|  | 345 | *	things when processing blocks of output data. It handles only | 
|  | 346 | *	the simple cases normally found and helps to generate blocks of | 
|  | 347 | *	symbols for the console driver and thus improve performance. | 
|  | 348 | * | 
| Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 349 | *	Called from write_chan under the tty layer write lock. Relies | 
|  | 350 | *	on lock_kernel for the tty->column state. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | */ | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 352 |  | 
|  | 353 | static ssize_t opost_block(struct tty_struct *tty, | 
|  | 354 | const unsigned char *buf, unsigned int nr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | { | 
|  | 356 | int	space; | 
|  | 357 | int 	i; | 
|  | 358 | const unsigned char *cp; | 
|  | 359 |  | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 360 | space = tty_write_room(tty); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | if (!space) | 
|  | 362 | return 0; | 
|  | 363 | if (nr > space) | 
|  | 364 | nr = space; | 
|  | 365 |  | 
| Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 366 | lock_kernel(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | for (i = 0, cp = buf; i < nr; i++, cp++) { | 
|  | 368 | switch (*cp) { | 
|  | 369 | case '\n': | 
|  | 370 | if (O_ONLRET(tty)) | 
|  | 371 | tty->column = 0; | 
|  | 372 | if (O_ONLCR(tty)) | 
|  | 373 | goto break_out; | 
|  | 374 | tty->canon_column = tty->column; | 
|  | 375 | break; | 
|  | 376 | case '\r': | 
|  | 377 | if (O_ONOCR(tty) && tty->column == 0) | 
|  | 378 | goto break_out; | 
|  | 379 | if (O_OCRNL(tty)) | 
|  | 380 | goto break_out; | 
|  | 381 | tty->canon_column = tty->column = 0; | 
|  | 382 | break; | 
|  | 383 | case '\t': | 
|  | 384 | goto break_out; | 
|  | 385 | case '\b': | 
|  | 386 | if (tty->column > 0) | 
|  | 387 | tty->column--; | 
|  | 388 | break; | 
|  | 389 | default: | 
|  | 390 | if (O_OLCUC(tty)) | 
|  | 391 | goto break_out; | 
|  | 392 | if (!iscntrl(*cp)) | 
|  | 393 | tty->column++; | 
|  | 394 | break; | 
|  | 395 | } | 
|  | 396 | } | 
|  | 397 | break_out: | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 398 | if (tty->ops->flush_chars) | 
|  | 399 | tty->ops->flush_chars(tty); | 
|  | 400 | i = tty->ops->write(tty, buf, i); | 
| Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 401 | unlock_kernel(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | return i; | 
|  | 403 | } | 
|  | 404 |  | 
|  | 405 |  | 
|  | 406 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | *	echo_char	-	echo characters | 
|  | 408 | *	@c: unicode byte to echo | 
|  | 409 | *	@tty: terminal device | 
|  | 410 | * | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 411 | *	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] | 412 | *	L_ECHO(tty) is true. Called from the driver receive_buf path. | 
|  | 413 | */ | 
|  | 414 |  | 
|  | 415 | static void echo_char(unsigned char c, struct tty_struct *tty) | 
|  | 416 | { | 
|  | 417 | if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') { | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 418 | tty_put_char(tty, '^'); | 
|  | 419 | tty_put_char(tty, c ^ 0100); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | tty->column += 2; | 
|  | 421 | } else | 
|  | 422 | opost(c, tty); | 
|  | 423 | } | 
|  | 424 |  | 
|  | 425 | static inline void finish_erasing(struct tty_struct *tty) | 
|  | 426 | { | 
|  | 427 | if (tty->erasing) { | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 428 | tty_put_char(tty, '/'); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | tty->column++; | 
|  | 430 | tty->erasing = 0; | 
|  | 431 | } | 
|  | 432 | } | 
|  | 433 |  | 
|  | 434 | /** | 
|  | 435 | *	eraser		-	handle erase function | 
|  | 436 | *	@c: character input | 
|  | 437 | *	@tty: terminal device | 
|  | 438 | * | 
| Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 439 | *	Perform erase and necessary output when an erase character is | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | *	present in the stream from the driver layer. Handles the complexities | 
|  | 441 | *	of UTF-8 multibyte symbols. | 
|  | 442 | */ | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 443 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | static void eraser(unsigned char c, struct tty_struct *tty) | 
|  | 445 | { | 
|  | 446 | enum { ERASE, WERASE, KILL } kill_type; | 
|  | 447 | int head, seen_alnums, cnt; | 
|  | 448 | unsigned long flags; | 
|  | 449 |  | 
|  | 450 | if (tty->read_head == tty->canon_head) { | 
|  | 451 | /* opost('\a', tty); */		/* what do you think? */ | 
|  | 452 | return; | 
|  | 453 | } | 
|  | 454 | if (c == ERASE_CHAR(tty)) | 
|  | 455 | kill_type = ERASE; | 
|  | 456 | else if (c == WERASE_CHAR(tty)) | 
|  | 457 | kill_type = WERASE; | 
|  | 458 | else { | 
|  | 459 | if (!L_ECHO(tty)) { | 
|  | 460 | spin_lock_irqsave(&tty->read_lock, flags); | 
|  | 461 | tty->read_cnt -= ((tty->read_head - tty->canon_head) & | 
|  | 462 | (N_TTY_BUF_SIZE - 1)); | 
|  | 463 | tty->read_head = tty->canon_head; | 
|  | 464 | spin_unlock_irqrestore(&tty->read_lock, flags); | 
|  | 465 | return; | 
|  | 466 | } | 
|  | 467 | if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) { | 
|  | 468 | spin_lock_irqsave(&tty->read_lock, flags); | 
|  | 469 | tty->read_cnt -= ((tty->read_head - tty->canon_head) & | 
|  | 470 | (N_TTY_BUF_SIZE - 1)); | 
|  | 471 | tty->read_head = tty->canon_head; | 
|  | 472 | spin_unlock_irqrestore(&tty->read_lock, flags); | 
|  | 473 | finish_erasing(tty); | 
|  | 474 | echo_char(KILL_CHAR(tty), tty); | 
|  | 475 | /* Add a newline if ECHOK is on and ECHOKE is off. */ | 
|  | 476 | if (L_ECHOK(tty)) | 
|  | 477 | opost('\n', tty); | 
|  | 478 | return; | 
|  | 479 | } | 
|  | 480 | kill_type = KILL; | 
|  | 481 | } | 
|  | 482 |  | 
|  | 483 | seen_alnums = 0; | 
|  | 484 | while (tty->read_head != tty->canon_head) { | 
|  | 485 | head = tty->read_head; | 
|  | 486 |  | 
|  | 487 | /* erase a single possibly multibyte character */ | 
|  | 488 | do { | 
|  | 489 | head = (head - 1) & (N_TTY_BUF_SIZE-1); | 
|  | 490 | c = tty->read_buf[head]; | 
|  | 491 | } while (is_continuation(c, tty) && head != tty->canon_head); | 
|  | 492 |  | 
|  | 493 | /* do not partially erase */ | 
|  | 494 | if (is_continuation(c, tty)) | 
|  | 495 | break; | 
|  | 496 |  | 
|  | 497 | if (kill_type == WERASE) { | 
|  | 498 | /* Equivalent to BSD's ALTWERASE. */ | 
|  | 499 | if (isalnum(c) || c == '_') | 
|  | 500 | seen_alnums++; | 
|  | 501 | else if (seen_alnums) | 
|  | 502 | break; | 
|  | 503 | } | 
|  | 504 | cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1); | 
|  | 505 | spin_lock_irqsave(&tty->read_lock, flags); | 
|  | 506 | tty->read_head = head; | 
|  | 507 | tty->read_cnt -= cnt; | 
|  | 508 | spin_unlock_irqrestore(&tty->read_lock, flags); | 
|  | 509 | if (L_ECHO(tty)) { | 
|  | 510 | if (L_ECHOPRT(tty)) { | 
|  | 511 | if (!tty->erasing) { | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 512 | tty_put_char(tty, '\\'); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | tty->column++; | 
|  | 514 | tty->erasing = 1; | 
|  | 515 | } | 
|  | 516 | /* if cnt > 1, output a multi-byte character */ | 
|  | 517 | echo_char(c, tty); | 
|  | 518 | while (--cnt > 0) { | 
|  | 519 | head = (head+1) & (N_TTY_BUF_SIZE-1); | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 520 | tty_put_char(tty, tty->read_buf[head]); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } | 
|  | 522 | } else if (kill_type == ERASE && !L_ECHOE(tty)) { | 
|  | 523 | echo_char(ERASE_CHAR(tty), tty); | 
|  | 524 | } else if (c == '\t') { | 
|  | 525 | unsigned int col = tty->canon_column; | 
|  | 526 | unsigned long tail = tty->canon_head; | 
|  | 527 |  | 
|  | 528 | /* Find the column of the last char. */ | 
|  | 529 | while (tail != tty->read_head) { | 
|  | 530 | c = tty->read_buf[tail]; | 
|  | 531 | if (c == '\t') | 
|  | 532 | col = (col | 7) + 1; | 
|  | 533 | else if (iscntrl(c)) { | 
|  | 534 | if (L_ECHOCTL(tty)) | 
|  | 535 | col += 2; | 
|  | 536 | } else if (!is_continuation(c, tty)) | 
|  | 537 | col++; | 
|  | 538 | tail = (tail+1) & (N_TTY_BUF_SIZE-1); | 
|  | 539 | } | 
|  | 540 |  | 
|  | 541 | /* should never happen */ | 
|  | 542 | if (tty->column > 0x80000000) | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 543 | tty->column = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 |  | 
|  | 545 | /* Now backup to that column. */ | 
|  | 546 | while (tty->column > col) { | 
|  | 547 | /* Can't use opost here. */ | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 548 | tty_put_char(tty, '\b'); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | if (tty->column > 0) | 
|  | 550 | tty->column--; | 
|  | 551 | } | 
|  | 552 | } else { | 
|  | 553 | if (iscntrl(c) && L_ECHOCTL(tty)) { | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 554 | tty_put_char(tty, '\b'); | 
|  | 555 | tty_put_char(tty, ' '); | 
|  | 556 | tty_put_char(tty, '\b'); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | if (tty->column > 0) | 
|  | 558 | tty->column--; | 
|  | 559 | } | 
|  | 560 | if (!iscntrl(c) || L_ECHOCTL(tty)) { | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 561 | tty_put_char(tty, '\b'); | 
|  | 562 | tty_put_char(tty, ' '); | 
|  | 563 | tty_put_char(tty, '\b'); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | if (tty->column > 0) | 
|  | 565 | tty->column--; | 
|  | 566 | } | 
|  | 567 | } | 
|  | 568 | } | 
|  | 569 | if (kill_type == ERASE) | 
|  | 570 | break; | 
|  | 571 | } | 
|  | 572 | if (tty->read_head == tty->canon_head) | 
|  | 573 | finish_erasing(tty); | 
|  | 574 | } | 
|  | 575 |  | 
|  | 576 | /** | 
|  | 577 | *	isig		-	handle the ISIG optio | 
|  | 578 | *	@sig: signal | 
|  | 579 | *	@tty: terminal | 
|  | 580 | *	@flush: force flush | 
|  | 581 | * | 
|  | 582 | *	Called when a signal is being sent due to terminal input. This | 
|  | 583 | *	may caus terminal flushing to take place according to the termios | 
|  | 584 | *	settings and character used. Called from the driver receive_buf | 
|  | 585 | *	path so serialized. | 
|  | 586 | */ | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 587 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | static inline void isig(int sig, struct tty_struct *tty, int flush) | 
|  | 589 | { | 
| Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 590 | if (tty->pgrp) | 
|  | 591 | kill_pgrp(tty->pgrp, sig, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | if (flush || !L_NOFLSH(tty)) { | 
|  | 593 | n_tty_flush_buffer(tty); | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 594 | tty_driver_flush_buffer(tty); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } | 
|  | 596 | } | 
|  | 597 |  | 
|  | 598 | /** | 
|  | 599 | *	n_tty_receive_break	-	handle break | 
|  | 600 | *	@tty: terminal | 
|  | 601 | * | 
|  | 602 | *	An RS232 break event has been hit in the incoming bitstream. This | 
|  | 603 | *	can cause a variety of events depending upon the termios settings. | 
|  | 604 | * | 
|  | 605 | *	Called from the receive_buf path so single threaded. | 
|  | 606 | */ | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 607 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | static inline void n_tty_receive_break(struct tty_struct *tty) | 
|  | 609 | { | 
|  | 610 | if (I_IGNBRK(tty)) | 
|  | 611 | return; | 
|  | 612 | if (I_BRKINT(tty)) { | 
|  | 613 | isig(SIGINT, tty, 1); | 
|  | 614 | return; | 
|  | 615 | } | 
|  | 616 | if (I_PARMRK(tty)) { | 
|  | 617 | put_tty_queue('\377', tty); | 
|  | 618 | put_tty_queue('\0', tty); | 
|  | 619 | } | 
|  | 620 | put_tty_queue('\0', tty); | 
|  | 621 | wake_up_interruptible(&tty->read_wait); | 
|  | 622 | } | 
|  | 623 |  | 
|  | 624 | /** | 
|  | 625 | *	n_tty_receive_overrun	-	handle overrun reporting | 
|  | 626 | *	@tty: terminal | 
|  | 627 | * | 
|  | 628 | *	Data arrived faster than we could process it. While the tty | 
|  | 629 | *	driver has flagged this the bits that were missed are gone | 
|  | 630 | *	forever. | 
|  | 631 | * | 
|  | 632 | *	Called from the receive_buf path so single threaded. Does not | 
|  | 633 | *	need locking as num_overrun and overrun_time are function | 
|  | 634 | *	private. | 
|  | 635 | */ | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 636 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | static inline void n_tty_receive_overrun(struct tty_struct *tty) | 
|  | 638 | { | 
|  | 639 | char buf[64]; | 
|  | 640 |  | 
|  | 641 | tty->num_overrun++; | 
|  | 642 | if (time_before(tty->overrun_time, jiffies - HZ) || | 
|  | 643 | time_after(tty->overrun_time, jiffies)) { | 
|  | 644 | printk(KERN_WARNING "%s: %d input overrun(s)\n", | 
|  | 645 | tty_name(tty, buf), | 
|  | 646 | tty->num_overrun); | 
|  | 647 | tty->overrun_time = jiffies; | 
|  | 648 | tty->num_overrun = 0; | 
|  | 649 | } | 
|  | 650 | } | 
|  | 651 |  | 
|  | 652 | /** | 
|  | 653 | *	n_tty_receive_parity_error	-	error notifier | 
|  | 654 | *	@tty: terminal device | 
|  | 655 | *	@c: character | 
|  | 656 | * | 
|  | 657 | *	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] | 658 | *	the error case if necessary. Locking as per n_tty_receive_buf. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | */ | 
|  | 660 | static inline void n_tty_receive_parity_error(struct tty_struct *tty, | 
|  | 661 | unsigned char c) | 
|  | 662 | { | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 663 | if (I_IGNPAR(tty)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | if (I_PARMRK(tty)) { | 
|  | 666 | put_tty_queue('\377', tty); | 
|  | 667 | put_tty_queue('\0', tty); | 
|  | 668 | put_tty_queue(c, tty); | 
|  | 669 | } else	if (I_INPCK(tty)) | 
|  | 670 | put_tty_queue('\0', tty); | 
|  | 671 | else | 
|  | 672 | put_tty_queue(c, tty); | 
|  | 673 | wake_up_interruptible(&tty->read_wait); | 
|  | 674 | } | 
|  | 675 |  | 
|  | 676 | /** | 
|  | 677 | *	n_tty_receive_char	-	perform processing | 
|  | 678 | *	@tty: terminal device | 
|  | 679 | *	@c: character | 
|  | 680 | * | 
|  | 681 | *	Process an individual character of input received from the driver. | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 682 | *	This is serialized with respect to itself by the rules for the | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | *	driver above. | 
|  | 684 | */ | 
|  | 685 |  | 
|  | 686 | static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c) | 
|  | 687 | { | 
|  | 688 | unsigned long flags; | 
|  | 689 |  | 
|  | 690 | if (tty->raw) { | 
|  | 691 | put_tty_queue(c, tty); | 
|  | 692 | return; | 
|  | 693 | } | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 694 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | if (I_ISTRIP(tty)) | 
|  | 696 | c &= 0x7f; | 
|  | 697 | if (I_IUCLC(tty) && L_IEXTEN(tty)) | 
|  | 698 | c=tolower(c); | 
|  | 699 |  | 
| Joe Peterson | 54d2a37 | 2008-02-06 01:37:59 -0800 | [diff] [blame] | 700 | if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && | 
|  | 701 | ((I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty)) || | 
| Joe Peterson | 575537b3 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 702 | c == INTR_CHAR(tty) || c == QUIT_CHAR(tty) || c == SUSP_CHAR(tty))) | 
| Joe Peterson | 54d2a37 | 2008-02-06 01:37:59 -0800 | [diff] [blame] | 703 | start_tty(tty); | 
|  | 704 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | if (tty->closing) { | 
|  | 706 | if (I_IXON(tty)) { | 
|  | 707 | if (c == START_CHAR(tty)) | 
|  | 708 | start_tty(tty); | 
|  | 709 | else if (c == STOP_CHAR(tty)) | 
|  | 710 | stop_tty(tty); | 
|  | 711 | } | 
|  | 712 | return; | 
|  | 713 | } | 
|  | 714 |  | 
|  | 715 | /* | 
|  | 716 | * If the previous character was LNEXT, or we know that this | 
|  | 717 | * character is not one of the characters that we'll have to | 
|  | 718 | * handle specially, do shortcut processing to speed things | 
|  | 719 | * up. | 
|  | 720 | */ | 
|  | 721 | if (!test_bit(c, tty->process_char_map) || tty->lnext) { | 
|  | 722 | finish_erasing(tty); | 
|  | 723 | tty->lnext = 0; | 
|  | 724 | if (L_ECHO(tty)) { | 
|  | 725 | if (tty->read_cnt >= N_TTY_BUF_SIZE-1) { | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 726 | tty_put_char(tty, '\a'); /* beep if no space */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | return; | 
|  | 728 | } | 
|  | 729 | /* Record the column of first canon char. */ | 
|  | 730 | if (tty->canon_head == tty->read_head) | 
|  | 731 | tty->canon_column = tty->column; | 
|  | 732 | echo_char(c, tty); | 
|  | 733 | } | 
|  | 734 | if (I_PARMRK(tty) && c == (unsigned char) '\377') | 
|  | 735 | put_tty_queue(c, tty); | 
|  | 736 | put_tty_queue(c, tty); | 
|  | 737 | return; | 
|  | 738 | } | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 739 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | if (I_IXON(tty)) { | 
|  | 741 | if (c == START_CHAR(tty)) { | 
|  | 742 | start_tty(tty); | 
|  | 743 | return; | 
|  | 744 | } | 
|  | 745 | if (c == STOP_CHAR(tty)) { | 
|  | 746 | stop_tty(tty); | 
|  | 747 | return; | 
|  | 748 | } | 
|  | 749 | } | 
| Joe Peterson | 575537b3 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 750 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | if (L_ISIG(tty)) { | 
|  | 752 | int signal; | 
|  | 753 | signal = SIGINT; | 
|  | 754 | if (c == INTR_CHAR(tty)) | 
|  | 755 | goto send_signal; | 
|  | 756 | signal = SIGQUIT; | 
|  | 757 | if (c == QUIT_CHAR(tty)) | 
|  | 758 | goto send_signal; | 
|  | 759 | signal = SIGTSTP; | 
|  | 760 | if (c == SUSP_CHAR(tty)) { | 
|  | 761 | send_signal: | 
| Joe Peterson | ec5b115 | 2008-02-06 01:37:38 -0800 | [diff] [blame] | 762 | /* | 
|  | 763 | * Echo character, and then send the signal. | 
|  | 764 | * Note that we do not use isig() here because we want | 
|  | 765 | * the order to be: | 
|  | 766 | * 1) flush, 2) echo, 3) signal | 
|  | 767 | */ | 
|  | 768 | if (!L_NOFLSH(tty)) { | 
|  | 769 | n_tty_flush_buffer(tty); | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 770 | tty_driver_flush_buffer(tty); | 
| Joe Peterson | ec5b115 | 2008-02-06 01:37:38 -0800 | [diff] [blame] | 771 | } | 
|  | 772 | if (L_ECHO(tty)) | 
|  | 773 | echo_char(c, tty); | 
|  | 774 | if (tty->pgrp) | 
|  | 775 | kill_pgrp(tty->pgrp, signal, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | return; | 
|  | 777 | } | 
|  | 778 | } | 
| Joe Peterson | 575537b3 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 779 |  | 
|  | 780 | if (c == '\r') { | 
|  | 781 | if (I_IGNCR(tty)) | 
|  | 782 | return; | 
|  | 783 | if (I_ICRNL(tty)) | 
|  | 784 | c = '\n'; | 
|  | 785 | } else if (c == '\n' && I_INLCR(tty)) | 
|  | 786 | c = '\r'; | 
|  | 787 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | if (tty->icanon) { | 
|  | 789 | if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) || | 
|  | 790 | (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) { | 
|  | 791 | eraser(c, tty); | 
|  | 792 | return; | 
|  | 793 | } | 
|  | 794 | if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) { | 
|  | 795 | tty->lnext = 1; | 
|  | 796 | if (L_ECHO(tty)) { | 
|  | 797 | finish_erasing(tty); | 
|  | 798 | if (L_ECHOCTL(tty)) { | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 799 | tty_put_char(tty, '^'); | 
|  | 800 | tty_put_char(tty, '\b'); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | } | 
|  | 802 | } | 
|  | 803 | return; | 
|  | 804 | } | 
|  | 805 | if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && | 
|  | 806 | L_IEXTEN(tty)) { | 
|  | 807 | unsigned long tail = tty->canon_head; | 
|  | 808 |  | 
|  | 809 | finish_erasing(tty); | 
|  | 810 | echo_char(c, tty); | 
|  | 811 | opost('\n', tty); | 
|  | 812 | while (tail != tty->read_head) { | 
|  | 813 | echo_char(tty->read_buf[tail], tty); | 
|  | 814 | tail = (tail+1) & (N_TTY_BUF_SIZE-1); | 
|  | 815 | } | 
|  | 816 | return; | 
|  | 817 | } | 
|  | 818 | if (c == '\n') { | 
|  | 819 | if (L_ECHO(tty) || L_ECHONL(tty)) { | 
| Roman Zippel | d6afe27 | 2005-07-07 17:56:55 -0700 | [diff] [blame] | 820 | if (tty->read_cnt >= N_TTY_BUF_SIZE-1) | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 821 | tty_put_char(tty, '\a'); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | opost('\n', tty); | 
|  | 823 | } | 
|  | 824 | goto handle_newline; | 
|  | 825 | } | 
|  | 826 | if (c == EOF_CHAR(tty)) { | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 827 | if (tty->canon_head != tty->read_head) | 
|  | 828 | set_bit(TTY_PUSH, &tty->flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | c = __DISABLED_CHAR; | 
|  | 830 | goto handle_newline; | 
|  | 831 | } | 
|  | 832 | if ((c == EOL_CHAR(tty)) || | 
|  | 833 | (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) { | 
|  | 834 | /* | 
|  | 835 | * XXX are EOL_CHAR and EOL2_CHAR echoed?!? | 
|  | 836 | */ | 
|  | 837 | if (L_ECHO(tty)) { | 
| Roman Zippel | d6afe27 | 2005-07-07 17:56:55 -0700 | [diff] [blame] | 838 | if (tty->read_cnt >= N_TTY_BUF_SIZE-1) | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 839 | tty_put_char(tty, '\a'); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | /* Record the column of first canon char. */ | 
|  | 841 | if (tty->canon_head == tty->read_head) | 
|  | 842 | tty->canon_column = tty->column; | 
|  | 843 | echo_char(c, tty); | 
|  | 844 | } | 
|  | 845 | /* | 
|  | 846 | * XXX does PARMRK doubling happen for | 
|  | 847 | * EOL_CHAR and EOL2_CHAR? | 
|  | 848 | */ | 
|  | 849 | if (I_PARMRK(tty) && c == (unsigned char) '\377') | 
|  | 850 | put_tty_queue(c, tty); | 
|  | 851 |  | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 852 | handle_newline: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | spin_lock_irqsave(&tty->read_lock, flags); | 
|  | 854 | set_bit(tty->read_head, tty->read_flags); | 
|  | 855 | put_tty_queue_nolock(c, tty); | 
|  | 856 | tty->canon_head = tty->read_head; | 
|  | 857 | tty->canon_data++; | 
|  | 858 | spin_unlock_irqrestore(&tty->read_lock, flags); | 
|  | 859 | kill_fasync(&tty->fasync, SIGIO, POLL_IN); | 
|  | 860 | if (waitqueue_active(&tty->read_wait)) | 
|  | 861 | wake_up_interruptible(&tty->read_wait); | 
|  | 862 | return; | 
|  | 863 | } | 
|  | 864 | } | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 865 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | finish_erasing(tty); | 
|  | 867 | if (L_ECHO(tty)) { | 
|  | 868 | if (tty->read_cnt >= N_TTY_BUF_SIZE-1) { | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 869 | tty_put_char(tty, '\a'); /* beep if no space */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | return; | 
|  | 871 | } | 
|  | 872 | if (c == '\n') | 
|  | 873 | opost('\n', tty); | 
|  | 874 | else { | 
|  | 875 | /* Record the column of first canon char. */ | 
|  | 876 | if (tty->canon_head == tty->read_head) | 
|  | 877 | tty->canon_column = tty->column; | 
|  | 878 | echo_char(c, tty); | 
|  | 879 | } | 
|  | 880 | } | 
|  | 881 |  | 
|  | 882 | if (I_PARMRK(tty) && c == (unsigned char) '\377') | 
|  | 883 | put_tty_queue(c, tty); | 
|  | 884 |  | 
|  | 885 | put_tty_queue(c, tty); | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 886 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 |  | 
|  | 889 | /** | 
|  | 890 | *	n_tty_write_wakeup	-	asynchronous I/O notifier | 
|  | 891 | *	@tty: tty device | 
|  | 892 | * | 
|  | 893 | *	Required for the ptys, serial driver etc. since processes | 
|  | 894 | *	that attach themselves to the master and rely on ASYNC | 
|  | 895 | *	IO must be woken up | 
|  | 896 | */ | 
|  | 897 |  | 
|  | 898 | static void n_tty_write_wakeup(struct tty_struct *tty) | 
|  | 899 | { | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 900 | if (tty->fasync) { | 
|  | 901 | set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | kill_fasync(&tty->fasync, SIGIO, POLL_OUT); | 
|  | 903 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | } | 
|  | 905 |  | 
|  | 906 | /** | 
|  | 907 | *	n_tty_receive_buf	-	data receive | 
|  | 908 | *	@tty: terminal device | 
|  | 909 | *	@cp: buffer | 
|  | 910 | *	@fp: flag buffer | 
|  | 911 | *	@count: characters | 
|  | 912 | * | 
|  | 913 | *	Called by the terminal driver when a block of characters has | 
|  | 914 | *	been received. This function must be called from soft contexts | 
|  | 915 | *	not from interrupt context. The driver is responsible for making | 
|  | 916 | *	calls one at a time and in order (or using flush_to_ldisc) | 
|  | 917 | */ | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 918 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp, | 
|  | 920 | char *fp, int count) | 
|  | 921 | { | 
|  | 922 | const unsigned char *p; | 
|  | 923 | char *f, flags = TTY_NORMAL; | 
|  | 924 | int	i; | 
|  | 925 | char	buf[64]; | 
|  | 926 | unsigned long cpuflags; | 
|  | 927 |  | 
|  | 928 | if (!tty->read_buf) | 
|  | 929 | return; | 
|  | 930 |  | 
|  | 931 | if (tty->real_raw) { | 
|  | 932 | spin_lock_irqsave(&tty->read_lock, cpuflags); | 
|  | 933 | i = min(N_TTY_BUF_SIZE - tty->read_cnt, | 
|  | 934 | N_TTY_BUF_SIZE - tty->read_head); | 
|  | 935 | i = min(count, i); | 
|  | 936 | memcpy(tty->read_buf + tty->read_head, cp, i); | 
|  | 937 | tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1); | 
|  | 938 | tty->read_cnt += i; | 
|  | 939 | cp += i; | 
|  | 940 | count -= i; | 
|  | 941 |  | 
|  | 942 | i = min(N_TTY_BUF_SIZE - tty->read_cnt, | 
|  | 943 | N_TTY_BUF_SIZE - tty->read_head); | 
|  | 944 | i = min(count, i); | 
|  | 945 | memcpy(tty->read_buf + tty->read_head, cp, i); | 
|  | 946 | tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1); | 
|  | 947 | tty->read_cnt += i; | 
|  | 948 | spin_unlock_irqrestore(&tty->read_lock, cpuflags); | 
|  | 949 | } else { | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 950 | for (i = count, p = cp, f = fp; i; i--, p++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | if (f) | 
|  | 952 | flags = *f++; | 
|  | 953 | switch (flags) { | 
|  | 954 | case TTY_NORMAL: | 
|  | 955 | n_tty_receive_char(tty, *p); | 
|  | 956 | break; | 
|  | 957 | case TTY_BREAK: | 
|  | 958 | n_tty_receive_break(tty); | 
|  | 959 | break; | 
|  | 960 | case TTY_PARITY: | 
|  | 961 | case TTY_FRAME: | 
|  | 962 | n_tty_receive_parity_error(tty, *p); | 
|  | 963 | break; | 
|  | 964 | case TTY_OVERRUN: | 
|  | 965 | n_tty_receive_overrun(tty); | 
|  | 966 | break; | 
|  | 967 | default: | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 968 | printk(KERN_ERR "%s: unknown flag %d\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | tty_name(tty, buf), flags); | 
|  | 970 | break; | 
|  | 971 | } | 
|  | 972 | } | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 973 | if (tty->ops->flush_chars) | 
|  | 974 | tty->ops->flush_chars(tty); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | } | 
|  | 976 |  | 
| Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 977 | n_tty_set_room(tty); | 
|  | 978 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) { | 
|  | 980 | kill_fasync(&tty->fasync, SIGIO, POLL_IN); | 
|  | 981 | if (waitqueue_active(&tty->read_wait)) | 
|  | 982 | wake_up_interruptible(&tty->read_wait); | 
|  | 983 | } | 
|  | 984 |  | 
|  | 985 | /* | 
|  | 986 | * Check the remaining room for the input canonicalization | 
|  | 987 | * mode.  We don't want to throttle the driver if we're in | 
|  | 988 | * canonical mode and don't have a newline yet! | 
|  | 989 | */ | 
| Alan Cox | 39c2e60 | 2008-04-30 00:54:18 -0700 | [diff] [blame] | 990 | if (tty->receive_room < TTY_THRESHOLD_THROTTLE) | 
|  | 991 | tty_throttle(tty); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | } | 
|  | 993 |  | 
|  | 994 | int is_ignored(int sig) | 
|  | 995 | { | 
|  | 996 | return (sigismember(¤t->blocked, sig) || | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 997 | current->sighand->action[sig-1].sa.sa_handler == SIG_IGN); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | } | 
|  | 999 |  | 
|  | 1000 | /** | 
|  | 1001 | *	n_tty_set_termios	-	termios data changed | 
|  | 1002 | *	@tty: terminal | 
|  | 1003 | *	@old: previous data | 
|  | 1004 | * | 
|  | 1005 | *	Called by the tty layer when the user changes termios flags so | 
|  | 1006 | *	that the line discipline can plan ahead. This function cannot sleep | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1007 | *	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] | 1008 | *	guaranteed that this function will not be re-entered or in progress | 
|  | 1009 | *	when the ldisc is closed. | 
|  | 1010 | */ | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1011 |  | 
|  | 1012 | 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] | 1013 | { | 
|  | 1014 | if (!tty) | 
|  | 1015 | return; | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1016 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | tty->icanon = (L_ICANON(tty) != 0); | 
|  | 1018 | if (test_bit(TTY_HW_COOK_IN, &tty->flags)) { | 
|  | 1019 | tty->raw = 1; | 
|  | 1020 | tty->real_raw = 1; | 
| Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1021 | n_tty_set_room(tty); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | return; | 
|  | 1023 | } | 
|  | 1024 | if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) || | 
|  | 1025 | I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) || | 
|  | 1026 | I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) || | 
|  | 1027 | I_PARMRK(tty)) { | 
|  | 1028 | memset(tty->process_char_map, 0, 256/8); | 
|  | 1029 |  | 
|  | 1030 | if (I_IGNCR(tty) || I_ICRNL(tty)) | 
|  | 1031 | set_bit('\r', tty->process_char_map); | 
|  | 1032 | if (I_INLCR(tty)) | 
|  | 1033 | set_bit('\n', tty->process_char_map); | 
|  | 1034 |  | 
|  | 1035 | if (L_ICANON(tty)) { | 
|  | 1036 | set_bit(ERASE_CHAR(tty), tty->process_char_map); | 
|  | 1037 | set_bit(KILL_CHAR(tty), tty->process_char_map); | 
|  | 1038 | set_bit(EOF_CHAR(tty), tty->process_char_map); | 
|  | 1039 | set_bit('\n', tty->process_char_map); | 
|  | 1040 | set_bit(EOL_CHAR(tty), tty->process_char_map); | 
|  | 1041 | if (L_IEXTEN(tty)) { | 
|  | 1042 | set_bit(WERASE_CHAR(tty), | 
|  | 1043 | tty->process_char_map); | 
|  | 1044 | set_bit(LNEXT_CHAR(tty), | 
|  | 1045 | tty->process_char_map); | 
|  | 1046 | set_bit(EOL2_CHAR(tty), | 
|  | 1047 | tty->process_char_map); | 
|  | 1048 | if (L_ECHO(tty)) | 
|  | 1049 | set_bit(REPRINT_CHAR(tty), | 
|  | 1050 | tty->process_char_map); | 
|  | 1051 | } | 
|  | 1052 | } | 
|  | 1053 | if (I_IXON(tty)) { | 
|  | 1054 | set_bit(START_CHAR(tty), tty->process_char_map); | 
|  | 1055 | set_bit(STOP_CHAR(tty), tty->process_char_map); | 
|  | 1056 | } | 
|  | 1057 | if (L_ISIG(tty)) { | 
|  | 1058 | set_bit(INTR_CHAR(tty), tty->process_char_map); | 
|  | 1059 | set_bit(QUIT_CHAR(tty), tty->process_char_map); | 
|  | 1060 | set_bit(SUSP_CHAR(tty), tty->process_char_map); | 
|  | 1061 | } | 
|  | 1062 | clear_bit(__DISABLED_CHAR, tty->process_char_map); | 
|  | 1063 | tty->raw = 0; | 
|  | 1064 | tty->real_raw = 0; | 
|  | 1065 | } else { | 
|  | 1066 | tty->raw = 1; | 
|  | 1067 | if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) && | 
|  | 1068 | (I_IGNPAR(tty) || !I_INPCK(tty)) && | 
|  | 1069 | (tty->driver->flags & TTY_DRIVER_REAL_RAW)) | 
|  | 1070 | tty->real_raw = 1; | 
|  | 1071 | else | 
|  | 1072 | tty->real_raw = 0; | 
|  | 1073 | } | 
| Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1074 | n_tty_set_room(tty); | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1075 | /* The termios change make the tty ready for I/O */ | 
|  | 1076 | wake_up_interruptible(&tty->write_wait); | 
|  | 1077 | wake_up_interruptible(&tty->read_wait); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | } | 
|  | 1079 |  | 
|  | 1080 | /** | 
|  | 1081 | *	n_tty_close		-	close the ldisc for this tty | 
|  | 1082 | *	@tty: device | 
|  | 1083 | * | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1084 | *	Called from the terminal layer when this line discipline is | 
|  | 1085 | *	being shut down, either because of a close or becsuse of a | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | *	discipline change. The function will not be called while other | 
|  | 1087 | *	ldisc methods are in progress. | 
|  | 1088 | */ | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1089 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | static void n_tty_close(struct tty_struct *tty) | 
|  | 1091 | { | 
|  | 1092 | n_tty_flush_buffer(tty); | 
|  | 1093 | if (tty->read_buf) { | 
|  | 1094 | free_buf(tty->read_buf); | 
|  | 1095 | tty->read_buf = NULL; | 
|  | 1096 | } | 
|  | 1097 | } | 
|  | 1098 |  | 
|  | 1099 | /** | 
|  | 1100 | *	n_tty_open		-	open an ldisc | 
|  | 1101 | *	@tty: terminal to open | 
|  | 1102 | * | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1103 | *	Called when this line discipline is being attached to the | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | *	terminal device. Can sleep. Called serialized so that no | 
|  | 1105 | *	other events will occur in parallel. No further open will occur | 
|  | 1106 | *	until a close. | 
|  | 1107 | */ | 
|  | 1108 |  | 
|  | 1109 | static int n_tty_open(struct tty_struct *tty) | 
|  | 1110 | { | 
|  | 1111 | if (!tty) | 
|  | 1112 | return -EINVAL; | 
|  | 1113 |  | 
|  | 1114 | /* This one is ugly. Currently a malloc failure here can panic */ | 
|  | 1115 | if (!tty->read_buf) { | 
|  | 1116 | tty->read_buf = alloc_buf(); | 
|  | 1117 | if (!tty->read_buf) | 
|  | 1118 | return -ENOMEM; | 
|  | 1119 | } | 
|  | 1120 | memset(tty->read_buf, 0, N_TTY_BUF_SIZE); | 
|  | 1121 | reset_buffer_flags(tty); | 
|  | 1122 | tty->column = 0; | 
|  | 1123 | n_tty_set_termios(tty, NULL); | 
|  | 1124 | tty->minimum_to_wake = 1; | 
|  | 1125 | tty->closing = 0; | 
|  | 1126 | return 0; | 
|  | 1127 | } | 
|  | 1128 |  | 
|  | 1129 | static inline int input_available_p(struct tty_struct *tty, int amt) | 
|  | 1130 | { | 
|  | 1131 | if (tty->icanon) { | 
|  | 1132 | if (tty->canon_data) | 
|  | 1133 | return 1; | 
|  | 1134 | } else if (tty->read_cnt >= (amt ? amt : 1)) | 
|  | 1135 | return 1; | 
|  | 1136 |  | 
|  | 1137 | return 0; | 
|  | 1138 | } | 
|  | 1139 |  | 
|  | 1140 | /** | 
|  | 1141 | * 	copy_from_read_buf	-	copy read data directly | 
|  | 1142 | *	@tty: terminal device | 
|  | 1143 | *	@b: user data | 
|  | 1144 | *	@nr: size of data | 
|  | 1145 | * | 
|  | 1146 | *	Helper function to speed up read_chan.  It is only called when | 
|  | 1147 | *	ICANON is off; it copies characters straight from the tty queue to | 
|  | 1148 | *	user space directly.  It can be profitably called twice; once to | 
|  | 1149 | *	drain the space from the tail pointer to the (physical) end of the | 
|  | 1150 | *	buffer, and once to drain the space from the (physical) beginning of | 
|  | 1151 | *	the buffer to head pointer. | 
|  | 1152 | * | 
| Paul Fulghum | 817d6d3 | 2006-06-28 04:26:47 -0700 | [diff] [blame] | 1153 | *	Called under the tty->atomic_read_lock sem | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | * | 
|  | 1155 | */ | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1156 |  | 
| Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1157 | static int copy_from_read_buf(struct tty_struct *tty, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | unsigned char __user **b, | 
|  | 1159 | size_t *nr) | 
|  | 1160 |  | 
|  | 1161 | { | 
|  | 1162 | int retval; | 
|  | 1163 | size_t n; | 
|  | 1164 | unsigned long flags; | 
|  | 1165 |  | 
|  | 1166 | retval = 0; | 
|  | 1167 | spin_lock_irqsave(&tty->read_lock, flags); | 
|  | 1168 | n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail); | 
|  | 1169 | n = min(*nr, n); | 
|  | 1170 | spin_unlock_irqrestore(&tty->read_lock, flags); | 
|  | 1171 | if (n) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n); | 
|  | 1173 | n -= retval; | 
| Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1174 | tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | spin_lock_irqsave(&tty->read_lock, flags); | 
|  | 1176 | tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1); | 
|  | 1177 | tty->read_cnt -= n; | 
|  | 1178 | spin_unlock_irqrestore(&tty->read_lock, flags); | 
|  | 1179 | *b += n; | 
|  | 1180 | *nr -= n; | 
|  | 1181 | } | 
|  | 1182 | return retval; | 
|  | 1183 | } | 
|  | 1184 |  | 
| Al Viro | cc4191d | 2008-03-29 03:08:48 +0000 | [diff] [blame] | 1185 | extern ssize_t redirected_tty_write(struct file *, const char __user *, | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1186 | size_t, loff_t *); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 |  | 
|  | 1188 | /** | 
|  | 1189 | *	job_control		-	check job control | 
|  | 1190 | *	@tty: tty | 
|  | 1191 | *	@file: file handle | 
|  | 1192 | * | 
|  | 1193 | *	Perform job control management checks on this file/tty descriptor | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1194 | *	and if appropriate send any needed signals and return a negative | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | *	error code if action should be taken. | 
| Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1196 | * | 
|  | 1197 | *	FIXME: | 
|  | 1198 | *	Locking: None - redirected write test is safe, testing | 
|  | 1199 | *	current->signal should possibly lock current->sighand | 
|  | 1200 | *	pgrp locking ? | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | */ | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1202 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | static int job_control(struct tty_struct *tty, struct file *file) | 
|  | 1204 | { | 
|  | 1205 | /* Job control check -- must be done at start and after | 
|  | 1206 | every sleep (POSIX.1 7.1.1.4). */ | 
|  | 1207 | /* NOTE: not yet done after every sleep pending a thorough | 
|  | 1208 | check of the logic of this change. -- jlc */ | 
|  | 1209 | /* don't stop on /dev/console */ | 
|  | 1210 | if (file->f_op->write != redirected_tty_write && | 
|  | 1211 | current->signal->tty == tty) { | 
| Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1212 | if (!tty->pgrp) | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1213 | printk(KERN_ERR "read_chan: no tty->pgrp!\n"); | 
| Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1214 | else if (task_pgrp(current) != tty->pgrp) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | if (is_ignored(SIGTTIN) || | 
| Eric W. Biederman | 3e7cd6c | 2007-02-12 00:52:58 -0800 | [diff] [blame] | 1216 | is_current_pgrp_orphaned()) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | return -EIO; | 
| Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1218 | kill_pgrp(task_pgrp(current), SIGTTIN, 1); | 
| Oleg Nesterov | 040b636 | 2007-06-01 00:46:53 -0700 | [diff] [blame] | 1219 | set_thread_flag(TIF_SIGPENDING); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | return -ERESTARTSYS; | 
|  | 1221 | } | 
|  | 1222 | } | 
|  | 1223 | return 0; | 
|  | 1224 | } | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1225 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 |  | 
|  | 1227 | /** | 
|  | 1228 | *	read_chan		-	read function for tty | 
|  | 1229 | *	@tty: tty device | 
|  | 1230 | *	@file: file object | 
|  | 1231 | *	@buf: userspace buffer pointer | 
|  | 1232 | *	@nr: size of I/O | 
|  | 1233 | * | 
|  | 1234 | *	Perform reads for the line discipline. We are guaranteed that the | 
|  | 1235 | *	line discipline will not be closed under us but we may get multiple | 
|  | 1236 | *	parallel readers and must handle this ourselves. We may also get | 
|  | 1237 | *	a hangup. Always called in user context, may sleep. | 
|  | 1238 | * | 
|  | 1239 | *	This code must be sure never to sleep through a hangup. | 
|  | 1240 | */ | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1241 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | static ssize_t read_chan(struct tty_struct *tty, struct file *file, | 
|  | 1243 | unsigned char __user *buf, size_t nr) | 
|  | 1244 | { | 
|  | 1245 | unsigned char __user *b = buf; | 
|  | 1246 | DECLARE_WAITQUEUE(wait, current); | 
|  | 1247 | int c; | 
|  | 1248 | int minimum, time; | 
|  | 1249 | ssize_t retval = 0; | 
|  | 1250 | ssize_t size; | 
|  | 1251 | long timeout; | 
|  | 1252 | unsigned long flags; | 
| Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1253 | int packet; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 |  | 
|  | 1255 | do_it_again: | 
|  | 1256 |  | 
|  | 1257 | if (!tty->read_buf) { | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1258 | printk(KERN_ERR "n_tty_read_chan: read_buf == NULL?!?\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | return -EIO; | 
|  | 1260 | } | 
|  | 1261 |  | 
|  | 1262 | c = job_control(tty, file); | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1263 | if (c < 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | return c; | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1265 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | minimum = time = 0; | 
|  | 1267 | timeout = MAX_SCHEDULE_TIMEOUT; | 
|  | 1268 | if (!tty->icanon) { | 
|  | 1269 | time = (HZ / 10) * TIME_CHAR(tty); | 
|  | 1270 | minimum = MIN_CHAR(tty); | 
|  | 1271 | if (minimum) { | 
|  | 1272 | if (time) | 
|  | 1273 | tty->minimum_to_wake = 1; | 
|  | 1274 | else if (!waitqueue_active(&tty->read_wait) || | 
|  | 1275 | (tty->minimum_to_wake > minimum)) | 
|  | 1276 | tty->minimum_to_wake = minimum; | 
|  | 1277 | } else { | 
|  | 1278 | timeout = 0; | 
|  | 1279 | if (time) { | 
|  | 1280 | timeout = time; | 
|  | 1281 | time = 0; | 
|  | 1282 | } | 
|  | 1283 | tty->minimum_to_wake = minimum = 1; | 
|  | 1284 | } | 
|  | 1285 | } | 
|  | 1286 |  | 
|  | 1287 | /* | 
|  | 1288 | *	Internal serialization of reads. | 
|  | 1289 | */ | 
|  | 1290 | if (file->f_flags & O_NONBLOCK) { | 
| Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1291 | if (!mutex_trylock(&tty->atomic_read_lock)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | return -EAGAIN; | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1293 | } else { | 
| Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1294 | if (mutex_lock_interruptible(&tty->atomic_read_lock)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | return -ERESTARTSYS; | 
|  | 1296 | } | 
| Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1297 | packet = tty->packet; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 |  | 
|  | 1299 | add_wait_queue(&tty->read_wait, &wait); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | while (nr) { | 
|  | 1301 | /* First test for status change. */ | 
| Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1302 | if (packet && tty->link->ctrl_status) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | unsigned char cs; | 
|  | 1304 | if (b != buf) | 
|  | 1305 | break; | 
| Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1306 | spin_lock_irqsave(&tty->link->ctrl_lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | cs = tty->link->ctrl_status; | 
|  | 1308 | tty->link->ctrl_status = 0; | 
| Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1309 | spin_unlock_irqrestore(&tty->link->ctrl_lock, flags); | 
| Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1310 | if (tty_put_user(tty, cs, b++)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | retval = -EFAULT; | 
|  | 1312 | b--; | 
|  | 1313 | break; | 
|  | 1314 | } | 
|  | 1315 | nr--; | 
|  | 1316 | break; | 
|  | 1317 | } | 
|  | 1318 | /* This statement must be first before checking for input | 
|  | 1319 | so that any interrupt will set the state back to | 
|  | 1320 | TASK_RUNNING. */ | 
|  | 1321 | set_current_state(TASK_INTERRUPTIBLE); | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1322 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | if (((minimum - (b - buf)) < tty->minimum_to_wake) && | 
|  | 1324 | ((minimum - (b - buf)) >= 1)) | 
|  | 1325 | tty->minimum_to_wake = (minimum - (b - buf)); | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1326 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | if (!input_available_p(tty, 0)) { | 
|  | 1328 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) { | 
|  | 1329 | retval = -EIO; | 
|  | 1330 | break; | 
|  | 1331 | } | 
|  | 1332 | if (tty_hung_up_p(file)) | 
|  | 1333 | break; | 
|  | 1334 | if (!timeout) | 
|  | 1335 | break; | 
|  | 1336 | if (file->f_flags & O_NONBLOCK) { | 
|  | 1337 | retval = -EAGAIN; | 
|  | 1338 | break; | 
|  | 1339 | } | 
|  | 1340 | if (signal_pending(current)) { | 
|  | 1341 | retval = -ERESTARTSYS; | 
|  | 1342 | break; | 
|  | 1343 | } | 
| Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1344 | /* FIXME: does n_tty_set_room need locking ? */ | 
| Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1345 | n_tty_set_room(tty); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | timeout = schedule_timeout(timeout); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | continue; | 
|  | 1348 | } | 
|  | 1349 | __set_current_state(TASK_RUNNING); | 
|  | 1350 |  | 
|  | 1351 | /* Deal with packet mode. */ | 
| Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1352 | if (packet && b == buf) { | 
| Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1353 | if (tty_put_user(tty, TIOCPKT_DATA, b++)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | retval = -EFAULT; | 
|  | 1355 | b--; | 
|  | 1356 | break; | 
|  | 1357 | } | 
|  | 1358 | nr--; | 
|  | 1359 | } | 
|  | 1360 |  | 
|  | 1361 | if (tty->icanon) { | 
|  | 1362 | /* N.B. avoid overrun if nr == 0 */ | 
|  | 1363 | while (nr && tty->read_cnt) { | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1364 | int eol; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 |  | 
|  | 1366 | eol = test_and_clear_bit(tty->read_tail, | 
|  | 1367 | tty->read_flags); | 
|  | 1368 | c = tty->read_buf[tty->read_tail]; | 
|  | 1369 | spin_lock_irqsave(&tty->read_lock, flags); | 
|  | 1370 | tty->read_tail = ((tty->read_tail+1) & | 
|  | 1371 | (N_TTY_BUF_SIZE-1)); | 
|  | 1372 | tty->read_cnt--; | 
|  | 1373 | if (eol) { | 
|  | 1374 | /* this test should be redundant: | 
|  | 1375 | * we shouldn't be reading data if | 
|  | 1376 | * canon_data is 0 | 
|  | 1377 | */ | 
|  | 1378 | if (--tty->canon_data < 0) | 
|  | 1379 | tty->canon_data = 0; | 
|  | 1380 | } | 
|  | 1381 | spin_unlock_irqrestore(&tty->read_lock, flags); | 
|  | 1382 |  | 
|  | 1383 | if (!eol || (c != __DISABLED_CHAR)) { | 
| Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1384 | if (tty_put_user(tty, c, b++)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | retval = -EFAULT; | 
|  | 1386 | b--; | 
|  | 1387 | break; | 
|  | 1388 | } | 
|  | 1389 | nr--; | 
|  | 1390 | } | 
| Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1391 | if (eol) { | 
|  | 1392 | tty_audit_push(tty); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | break; | 
| Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1394 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | } | 
|  | 1396 | if (retval) | 
|  | 1397 | break; | 
|  | 1398 | } else { | 
|  | 1399 | int uncopied; | 
| Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1400 | /* The copy function takes the read lock and handles | 
|  | 1401 | locking internally for this case */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | uncopied = copy_from_read_buf(tty, &b, &nr); | 
|  | 1403 | uncopied += copy_from_read_buf(tty, &b, &nr); | 
|  | 1404 | if (uncopied) { | 
|  | 1405 | retval = -EFAULT; | 
|  | 1406 | break; | 
|  | 1407 | } | 
|  | 1408 | } | 
|  | 1409 |  | 
|  | 1410 | /* If there is enough space in the read buffer now, let the | 
|  | 1411 | * low-level driver know. We use n_tty_chars_in_buffer() to | 
|  | 1412 | * check the buffer, as it now knows about canonical mode. | 
|  | 1413 | * Otherwise, if the driver is throttled and the line is | 
|  | 1414 | * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode, | 
|  | 1415 | * we won't get any more characters. | 
|  | 1416 | */ | 
| Paul Mackerras | 289a1e9 | 2006-06-12 12:16:26 +1000 | [diff] [blame] | 1417 | if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) { | 
|  | 1418 | n_tty_set_room(tty); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | check_unthrottle(tty); | 
| Paul Mackerras | 289a1e9 | 2006-06-12 12:16:26 +1000 | [diff] [blame] | 1420 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 |  | 
|  | 1422 | if (b - buf >= minimum) | 
|  | 1423 | break; | 
|  | 1424 | if (time) | 
|  | 1425 | timeout = time; | 
|  | 1426 | } | 
| Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1427 | mutex_unlock(&tty->atomic_read_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | remove_wait_queue(&tty->read_wait, &wait); | 
|  | 1429 |  | 
|  | 1430 | if (!waitqueue_active(&tty->read_wait)) | 
|  | 1431 | tty->minimum_to_wake = minimum; | 
|  | 1432 |  | 
|  | 1433 | __set_current_state(TASK_RUNNING); | 
|  | 1434 | size = b - buf; | 
|  | 1435 | if (size) { | 
|  | 1436 | retval = size; | 
|  | 1437 | if (nr) | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1438 | clear_bit(TTY_PUSH, &tty->flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | } else if (test_and_clear_bit(TTY_PUSH, &tty->flags)) | 
|  | 1440 | goto do_it_again; | 
|  | 1441 |  | 
| Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1442 | n_tty_set_room(tty); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | return retval; | 
|  | 1444 | } | 
|  | 1445 |  | 
|  | 1446 | /** | 
|  | 1447 | *	write_chan		-	write function for tty | 
|  | 1448 | *	@tty: tty device | 
|  | 1449 | *	@file: file object | 
|  | 1450 | *	@buf: userspace buffer pointer | 
|  | 1451 | *	@nr: size of I/O | 
|  | 1452 | * | 
|  | 1453 | *	Write function of the terminal device. This is serialized with | 
|  | 1454 | *	respect to other write callers but not to termios changes, reads | 
|  | 1455 | *	and other such events. We must be careful with N_TTY as the receive | 
|  | 1456 | *	code will echo characters, thus calling driver write methods. | 
|  | 1457 | * | 
|  | 1458 | *	This code must be sure never to sleep through a hangup. | 
|  | 1459 | */ | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1460 |  | 
|  | 1461 | static ssize_t write_chan(struct tty_struct *tty, struct file *file, | 
|  | 1462 | const unsigned char *buf, size_t nr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | { | 
|  | 1464 | const unsigned char *b = buf; | 
|  | 1465 | DECLARE_WAITQUEUE(wait, current); | 
|  | 1466 | int c; | 
|  | 1467 | ssize_t retval = 0; | 
|  | 1468 |  | 
|  | 1469 | /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */ | 
|  | 1470 | if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) { | 
|  | 1471 | retval = tty_check_change(tty); | 
|  | 1472 | if (retval) | 
|  | 1473 | return retval; | 
|  | 1474 | } | 
|  | 1475 |  | 
|  | 1476 | add_wait_queue(&tty->write_wait, &wait); | 
|  | 1477 | while (1) { | 
|  | 1478 | set_current_state(TASK_INTERRUPTIBLE); | 
|  | 1479 | if (signal_pending(current)) { | 
|  | 1480 | retval = -ERESTARTSYS; | 
|  | 1481 | break; | 
|  | 1482 | } | 
|  | 1483 | if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) { | 
|  | 1484 | retval = -EIO; | 
|  | 1485 | break; | 
|  | 1486 | } | 
|  | 1487 | if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) { | 
|  | 1488 | while (nr > 0) { | 
|  | 1489 | ssize_t num = opost_block(tty, b, nr); | 
|  | 1490 | if (num < 0) { | 
|  | 1491 | if (num == -EAGAIN) | 
|  | 1492 | break; | 
|  | 1493 | retval = num; | 
|  | 1494 | goto break_out; | 
|  | 1495 | } | 
|  | 1496 | b += num; | 
|  | 1497 | nr -= num; | 
|  | 1498 | if (nr == 0) | 
|  | 1499 | break; | 
|  | 1500 | c = *b; | 
|  | 1501 | if (opost(c, tty) < 0) | 
|  | 1502 | break; | 
|  | 1503 | b++; nr--; | 
|  | 1504 | } | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1505 | if (tty->ops->flush_chars) | 
|  | 1506 | tty->ops->flush_chars(tty); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | } else { | 
| Roman Zippel | d6afe27 | 2005-07-07 17:56:55 -0700 | [diff] [blame] | 1508 | while (nr > 0) { | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1509 | c = tty->ops->write(tty, b, nr); | 
| Roman Zippel | d6afe27 | 2005-07-07 17:56:55 -0700 | [diff] [blame] | 1510 | if (c < 0) { | 
|  | 1511 | retval = c; | 
|  | 1512 | goto break_out; | 
|  | 1513 | } | 
|  | 1514 | if (!c) | 
|  | 1515 | break; | 
|  | 1516 | b += c; | 
|  | 1517 | nr -= c; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | } | 
|  | 1520 | if (!nr) | 
|  | 1521 | break; | 
|  | 1522 | if (file->f_flags & O_NONBLOCK) { | 
|  | 1523 | retval = -EAGAIN; | 
|  | 1524 | break; | 
|  | 1525 | } | 
|  | 1526 | schedule(); | 
|  | 1527 | } | 
|  | 1528 | break_out: | 
|  | 1529 | __set_current_state(TASK_RUNNING); | 
|  | 1530 | remove_wait_queue(&tty->write_wait, &wait); | 
|  | 1531 | return (b - buf) ? b - buf : retval; | 
|  | 1532 | } | 
|  | 1533 |  | 
|  | 1534 | /** | 
|  | 1535 | *	normal_poll		-	poll method for N_TTY | 
|  | 1536 | *	@tty: terminal device | 
|  | 1537 | *	@file: file accessing it | 
|  | 1538 | *	@wait: poll table | 
|  | 1539 | * | 
|  | 1540 | *	Called when the line discipline is asked to poll() for data or | 
|  | 1541 | *	for special events. This code is not serialized with respect to | 
|  | 1542 | *	other events save open/close. | 
|  | 1543 | * | 
|  | 1544 | *	This code must be sure never to sleep through a hangup. | 
|  | 1545 | *	Called without the kernel lock held - fine | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | */ | 
| Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1547 |  | 
|  | 1548 | static unsigned int normal_poll(struct tty_struct *tty, struct file *file, | 
|  | 1549 | poll_table *wait) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | { | 
|  | 1551 | unsigned int mask = 0; | 
|  | 1552 |  | 
|  | 1553 | poll_wait(file, &tty->read_wait, wait); | 
|  | 1554 | poll_wait(file, &tty->write_wait, wait); | 
|  | 1555 | if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty))) | 
|  | 1556 | mask |= POLLIN | POLLRDNORM; | 
|  | 1557 | if (tty->packet && tty->link->ctrl_status) | 
|  | 1558 | mask |= POLLPRI | POLLIN | POLLRDNORM; | 
|  | 1559 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) | 
|  | 1560 | mask |= POLLHUP; | 
|  | 1561 | if (tty_hung_up_p(file)) | 
|  | 1562 | mask |= POLLHUP; | 
|  | 1563 | if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) { | 
|  | 1564 | if (MIN_CHAR(tty) && !TIME_CHAR(tty)) | 
|  | 1565 | tty->minimum_to_wake = MIN_CHAR(tty); | 
|  | 1566 | else | 
|  | 1567 | tty->minimum_to_wake = 1; | 
|  | 1568 | } | 
| Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1569 | if (tty->ops->write && !tty_is_writelocked(tty) && | 
|  | 1570 | tty_chars_in_buffer(tty) < WAKEUP_CHARS && | 
|  | 1571 | tty_write_room(tty) > 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | mask |= POLLOUT | POLLWRNORM; | 
|  | 1573 | return mask; | 
|  | 1574 | } | 
|  | 1575 |  | 
| Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1576 | struct tty_ldisc_ops tty_ldisc_N_TTY = { | 
| Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 1577 | .magic           = TTY_LDISC_MAGIC, | 
|  | 1578 | .name            = "n_tty", | 
|  | 1579 | .open            = n_tty_open, | 
|  | 1580 | .close           = n_tty_close, | 
|  | 1581 | .flush_buffer    = n_tty_flush_buffer, | 
|  | 1582 | .chars_in_buffer = n_tty_chars_in_buffer, | 
|  | 1583 | .read            = read_chan, | 
|  | 1584 | .write           = write_chan, | 
|  | 1585 | .ioctl           = n_tty_ioctl, | 
|  | 1586 | .set_termios     = n_tty_set_termios, | 
|  | 1587 | .poll            = normal_poll, | 
|  | 1588 | .receive_buf     = n_tty_receive_buf, | 
|  | 1589 | .write_wakeup    = n_tty_write_wakeup | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | }; | 
|  | 1591 |  |