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