1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 4 * 5 * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines 6 * which can be dynamically activated and de-activated by the line 7 * discipline handling modules (like SLIP). 8 */ 9 10 #include <linux/bits.h> 11 #include <linux/types.h> 12 #include <linux/termios.h> 13 #include <linux/errno.h> 14 #include <linux/sched/signal.h> 15 #include <linux/kernel.h> 16 #include <linux/major.h> 17 #include <linux/tty.h> 18 #include <linux/fcntl.h> 19 #include <linux/string.h> 20 #include <linux/mm.h> 21 #include <linux/module.h> 22 #include <linux/bitops.h> 23 #include <linux/mutex.h> 24 #include <linux/compat.h> 25 #include <linux/termios_internal.h> 26 #include "tty.h" 27 28 #include <asm/io.h> 29 #include <linux/uaccess.h> 30 31 #undef TTY_DEBUG_WAIT_UNTIL_SENT 32 33 #ifdef TTY_DEBUG_WAIT_UNTIL_SENT 34 # define tty_debug_wait_until_sent(tty, f, args...) tty_debug(tty, f, ##args) 35 #else 36 # define tty_debug_wait_until_sent(tty, f, args...) do {} while (0) 37 #endif 38 39 #undef DEBUG 40 41 /* 42 * Internal flag options for termios setting behavior 43 */ 44 #define TERMIOS_FLUSH BIT(0) 45 #define TERMIOS_WAIT BIT(1) 46 #define TERMIOS_TERMIO BIT(2) 47 #define TERMIOS_OLD BIT(3) 48 49 50 /** 51 * tty_chars_in_buffer - characters pending 52 * @tty: terminal 53 * 54 * Return the number of bytes of data in the device private 55 * output queue. If no private method is supplied there is assumed 56 * to be no queue on the device. 57 */ 58 59 unsigned int tty_chars_in_buffer(struct tty_struct *tty) 60 { 61 if (tty->ops->chars_in_buffer) 62 return tty->ops->chars_in_buffer(tty); 63 return 0; 64 } 65 EXPORT_SYMBOL(tty_chars_in_buffer); 66 67 /** 68 * tty_write_room - write queue space 69 * @tty: terminal 70 * 71 * Return the number of bytes that can be queued to this device 72 * at the present time. The result should be treated as a guarantee 73 * and the driver cannot offer a value it later shrinks by more than 74 * the number of bytes written. If no method is provided 2K is always 75 * returned and data may be lost as there will be no flow control. 76 */ 77 78 unsigned int tty_write_room(struct tty_struct *tty) 79 { 80 if (tty->ops->write_room) 81 return tty->ops->write_room(tty); 82 return 2048; 83 } 84 EXPORT_SYMBOL(tty_write_room); 85 86 /** 87 * tty_driver_flush_buffer - discard internal buffer 88 * @tty: terminal 89 * 90 * Discard the internal output buffer for this device. If no method 91 * is provided then either the buffer cannot be hardware flushed or 92 * there is no buffer driver side. 93 */ 94 void tty_driver_flush_buffer(struct tty_struct *tty) 95 { 96 if (tty->ops->flush_buffer) 97 tty->ops->flush_buffer(tty); 98 } 99 EXPORT_SYMBOL(tty_driver_flush_buffer); 100 101 /** 102 * tty_unthrottle - flow control 103 * @tty: terminal 104 * 105 * Indicate that a tty may continue transmitting data down the stack. 106 * Takes the termios rwsem to protect against parallel throttle/unthrottle 107 * and also to ensure the driver can consistently reference its own 108 * termios data at this point when implementing software flow control. 109 * 110 * Drivers should however remember that the stack can issue a throttle, 111 * then change flow control method, then unthrottle. 112 */ 113 114 void tty_unthrottle(struct tty_struct *tty) 115 { 116 down_write(&tty->termios_rwsem); 117 if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) && 118 tty->ops->unthrottle) 119 tty->ops->unthrottle(tty); 120 tty->flow_change = 0; 121 up_write(&tty->termios_rwsem); 122 } 123 EXPORT_SYMBOL(tty_unthrottle); 124 125 /** 126 * tty_throttle_safe - flow control 127 * @tty: terminal 128 * 129 * Indicate that a tty should stop transmitting data down the stack. 130 * tty_throttle_safe will only attempt throttle if tty->flow_change is 131 * TTY_THROTTLE_SAFE. Prevents an accidental throttle due to race 132 * conditions when throttling is conditional on factors evaluated prior to 133 * throttling. 134 * 135 * Returns 0 if tty is throttled (or was already throttled) 136 */ 137 138 int tty_throttle_safe(struct tty_struct *tty) 139 { 140 int ret = 0; 141 142 mutex_lock(&tty->throttle_mutex); 143 if (!tty_throttled(tty)) { 144 if (tty->flow_change != TTY_THROTTLE_SAFE) 145 ret = 1; 146 else { 147 set_bit(TTY_THROTTLED, &tty->flags); 148 if (tty->ops->throttle) 149 tty->ops->throttle(tty); 150 } 151 } 152 mutex_unlock(&tty->throttle_mutex); 153 154 return ret; 155 } 156 157 /** 158 * tty_unthrottle_safe - flow control 159 * @tty: terminal 160 * 161 * Similar to tty_unthrottle() but will only attempt unthrottle 162 * if tty->flow_change is TTY_UNTHROTTLE_SAFE. Prevents an accidental 163 * unthrottle due to race conditions when unthrottling is conditional 164 * on factors evaluated prior to unthrottling. 165 * 166 * Returns 0 if tty is unthrottled (or was already unthrottled) 167 */ 168 169 int tty_unthrottle_safe(struct tty_struct *tty) 170 { 171 int ret = 0; 172 173 mutex_lock(&tty->throttle_mutex); 174 if (tty_throttled(tty)) { 175 if (tty->flow_change != TTY_UNTHROTTLE_SAFE) 176 ret = 1; 177 else { 178 clear_bit(TTY_THROTTLED, &tty->flags); 179 if (tty->ops->unthrottle) 180 tty->ops->unthrottle(tty); 181 } 182 } 183 mutex_unlock(&tty->throttle_mutex); 184 185 return ret; 186 } 187 188 /** 189 * tty_wait_until_sent - wait for I/O to finish 190 * @tty: tty we are waiting for 191 * @timeout: how long we will wait 192 * 193 * Wait for characters pending in a tty driver to hit the wire, or 194 * for a timeout to occur (eg due to flow control) 195 * 196 * Locking: none 197 */ 198 199 void tty_wait_until_sent(struct tty_struct *tty, long timeout) 200 { 201 tty_debug_wait_until_sent(tty, "wait until sent, timeout=%ld\n", timeout); 202 203 if (!timeout) 204 timeout = MAX_SCHEDULE_TIMEOUT; 205 206 timeout = wait_event_interruptible_timeout(tty->write_wait, 207 !tty_chars_in_buffer(tty), timeout); 208 if (timeout <= 0) 209 return; 210 211 if (timeout == MAX_SCHEDULE_TIMEOUT) 212 timeout = 0; 213 214 if (tty->ops->wait_until_sent) 215 tty->ops->wait_until_sent(tty, timeout); 216 } 217 EXPORT_SYMBOL(tty_wait_until_sent); 218 219 220 /* 221 * Termios Helper Methods 222 */ 223 224 static void unset_locked_termios(struct tty_struct *tty, const struct ktermios *old) 225 { 226 struct ktermios *termios = &tty->termios; 227 struct ktermios *locked = &tty->termios_locked; 228 int i; 229 230 #define NOSET_MASK(x, y, z) (x = ((x) & ~(z)) | ((y) & (z))) 231 232 NOSET_MASK(termios->c_iflag, old->c_iflag, locked->c_iflag); 233 NOSET_MASK(termios->c_oflag, old->c_oflag, locked->c_oflag); 234 NOSET_MASK(termios->c_cflag, old->c_cflag, locked->c_cflag); 235 NOSET_MASK(termios->c_lflag, old->c_lflag, locked->c_lflag); 236 termios->c_line = locked->c_line ? old->c_line : termios->c_line; 237 for (i = 0; i < NCCS; i++) 238 termios->c_cc[i] = locked->c_cc[i] ? 239 old->c_cc[i] : termios->c_cc[i]; 240 /* FIXME: What should we do for i/ospeed */ 241 } 242 243 /** 244 * tty_termios_copy_hw - copy hardware settings 245 * @new: New termios 246 * @old: Old termios 247 * 248 * Propagate the hardware specific terminal setting bits from 249 * the old termios structure to the new one. This is used in cases 250 * where the hardware does not support reconfiguration or as a helper 251 * in some cases where only minimal reconfiguration is supported 252 */ 253 254 void tty_termios_copy_hw(struct ktermios *new, const struct ktermios *old) 255 { 256 /* The bits a dumb device handles in software. Smart devices need 257 to always provide a set_termios method */ 258 new->c_cflag &= HUPCL | CREAD | CLOCAL; 259 new->c_cflag |= old->c_cflag & ~(HUPCL | CREAD | CLOCAL); 260 new->c_ispeed = old->c_ispeed; 261 new->c_ospeed = old->c_ospeed; 262 } 263 EXPORT_SYMBOL(tty_termios_copy_hw); 264 265 /** 266 * tty_termios_hw_change - check for setting change 267 * @a: termios 268 * @b: termios to compare 269 * 270 * Check if any of the bits that affect a dumb device have changed 271 * between the two termios structures, or a speed change is needed. 272 */ 273 274 bool tty_termios_hw_change(const struct ktermios *a, const struct ktermios *b) 275 { 276 if (a->c_ispeed != b->c_ispeed || a->c_ospeed != b->c_ospeed) 277 return true; 278 if ((a->c_cflag ^ b->c_cflag) & ~(HUPCL | CREAD | CLOCAL)) 279 return true; 280 return false; 281 } 282 EXPORT_SYMBOL(tty_termios_hw_change); 283 284 /** 285 * tty_get_char_size - get size of a character 286 * @cflag: termios cflag value 287 * 288 * Get the size (in bits) of a character depending on @cflag's %CSIZE 289 * setting. 290 */ 291 unsigned char tty_get_char_size(unsigned int cflag) 292 { 293 switch (cflag & CSIZE) { 294 case CS5: 295 return 5; 296 case CS6: 297 return 6; 298 case CS7: 299 return 7; 300 case CS8: 301 default: 302 return 8; 303 } 304 } 305 EXPORT_SYMBOL_GPL(tty_get_char_size); 306 307 /** 308 * tty_get_frame_size - get size of a frame 309 * @cflag: termios cflag value 310 * 311 * Get the size (in bits) of a frame depending on @cflag's %CSIZE, %CSTOPB, 312 * and %PARENB setting. The result is a sum of character size, start and 313 * stop bits -- one bit each -- second stop bit (if set), and parity bit 314 * (if set). 315 */ 316 unsigned char tty_get_frame_size(unsigned int cflag) 317 { 318 unsigned char bits = 2 + tty_get_char_size(cflag); 319 320 if (cflag & CSTOPB) 321 bits++; 322 if (cflag & PARENB) 323 bits++; 324 if (cflag & ADDRB) 325 bits++; 326 327 return bits; 328 } 329 EXPORT_SYMBOL_GPL(tty_get_frame_size); 330 331 /** 332 * tty_set_termios - update termios values 333 * @tty: tty to update 334 * @new_termios: desired new value 335 * 336 * Perform updates to the termios values set on this terminal. 337 * A master pty's termios should never be set. 338 * 339 * Locking: termios_rwsem 340 */ 341 342 int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios) 343 { 344 struct ktermios old_termios; 345 struct tty_ldisc *ld; 346 347 WARN_ON(tty->driver->type == TTY_DRIVER_TYPE_PTY && 348 tty->driver->subtype == PTY_TYPE_MASTER); 349 /* 350 * Perform the actual termios internal changes under lock. 351 */ 352 353 354 /* FIXME: we need to decide on some locking/ordering semantics 355 for the set_termios notification eventually */ 356 down_write(&tty->termios_rwsem); 357 old_termios = tty->termios; 358 tty->termios = *new_termios; 359 unset_locked_termios(tty, &old_termios); 360 /* Reset any ADDRB changes, ADDRB is changed through ->rs485_config() */ 361 tty->termios.c_cflag ^= (tty->termios.c_cflag ^ old_termios.c_cflag) & ADDRB; 362 363 if (tty->ops->set_termios) 364 tty->ops->set_termios(tty, &old_termios); 365 else 366 tty_termios_copy_hw(&tty->termios, &old_termios); 367 368 ld = tty_ldisc_ref(tty); 369 if (ld != NULL) { 370 if (ld->ops->set_termios) 371 ld->ops->set_termios(tty, &old_termios); 372 tty_ldisc_deref(ld); 373 } 374 up_write(&tty->termios_rwsem); 375 return 0; 376 } 377 EXPORT_SYMBOL_GPL(tty_set_termios); 378 379 380 /* 381 * Translate a "termio" structure into a "termios". Ugh. 382 */ 383 __weak int user_termio_to_kernel_termios(struct ktermios *termios, 384 struct termio __user *termio) 385 { 386 struct termio v; 387 388 if (copy_from_user(&v, termio, sizeof(struct termio))) 389 return -EFAULT; 390 391 termios->c_iflag = (0xffff0000 & termios->c_iflag) | v.c_iflag; 392 termios->c_oflag = (0xffff0000 & termios->c_oflag) | v.c_oflag; 393 termios->c_cflag = (0xffff0000 & termios->c_cflag) | v.c_cflag; 394 termios->c_lflag = (0xffff0000 & termios->c_lflag) | v.c_lflag; 395 termios->c_line = (0xffff0000 & termios->c_lflag) | v.c_line; 396 memcpy(termios->c_cc, v.c_cc, NCC); 397 return 0; 398 } 399 400 /* 401 * Translate a "termios" structure into a "termio". Ugh. 402 */ 403 __weak int kernel_termios_to_user_termio(struct termio __user *termio, 404 struct ktermios *termios) 405 { 406 struct termio v; 407 memset(&v, 0, sizeof(struct termio)); 408 v.c_iflag = termios->c_iflag; 409 v.c_oflag = termios->c_oflag; 410 v.c_cflag = termios->c_cflag; 411 v.c_lflag = termios->c_lflag; 412 v.c_line = termios->c_line; 413 memcpy(v.c_cc, termios->c_cc, NCC); 414 return copy_to_user(termio, &v, sizeof(struct termio)); 415 } 416 417 #ifdef TCGETS2 418 __weak int user_termios_to_kernel_termios(struct ktermios *k, 419 struct termios2 __user *u) 420 { 421 return copy_from_user(k, u, sizeof(struct termios2)); 422 } 423 __weak int kernel_termios_to_user_termios(struct termios2 __user *u, 424 struct ktermios *k) 425 { 426 return copy_to_user(u, k, sizeof(struct termios2)); 427 } 428 __weak int user_termios_to_kernel_termios_1(struct ktermios *k, 429 struct termios __user *u) 430 { 431 return copy_from_user(k, u, sizeof(struct termios)); 432 } 433 __weak int kernel_termios_to_user_termios_1(struct termios __user *u, 434 struct ktermios *k) 435 { 436 return copy_to_user(u, k, sizeof(struct termios)); 437 } 438 439 #else 440 441 __weak int user_termios_to_kernel_termios(struct ktermios *k, 442 struct termios __user *u) 443 { 444 return copy_from_user(k, u, sizeof(struct termios)); 445 } 446 __weak int kernel_termios_to_user_termios(struct termios __user *u, 447 struct ktermios *k) 448 { 449 return copy_to_user(u, k, sizeof(struct termios)); 450 } 451 #endif /* TCGETS2 */ 452 453 /** 454 * set_termios - set termios values for a tty 455 * @tty: terminal device 456 * @arg: user data 457 * @opt: option information 458 * 459 * Helper function to prepare termios data and run necessary other 460 * functions before using tty_set_termios to do the actual changes. 461 * 462 * Locking: 463 * Called functions take ldisc and termios_rwsem locks 464 */ 465 466 static int set_termios(struct tty_struct *tty, void __user *arg, int opt) 467 { 468 struct ktermios tmp_termios; 469 struct tty_ldisc *ld; 470 int retval = tty_check_change(tty); 471 472 if (retval) 473 return retval; 474 475 down_read(&tty->termios_rwsem); 476 tmp_termios = tty->termios; 477 up_read(&tty->termios_rwsem); 478 479 if (opt & TERMIOS_TERMIO) { 480 if (user_termio_to_kernel_termios(&tmp_termios, 481 (struct termio __user *)arg)) 482 return -EFAULT; 483 #ifdef TCGETS2 484 } else if (opt & TERMIOS_OLD) { 485 if (user_termios_to_kernel_termios_1(&tmp_termios, 486 (struct termios __user *)arg)) 487 return -EFAULT; 488 } else { 489 if (user_termios_to_kernel_termios(&tmp_termios, 490 (struct termios2 __user *)arg)) 491 return -EFAULT; 492 } 493 #else 494 } else if (user_termios_to_kernel_termios(&tmp_termios, 495 (struct termios __user *)arg)) 496 return -EFAULT; 497 #endif 498 499 /* If old style Bfoo values are used then load c_ispeed/c_ospeed 500 * with the real speed so its unconditionally usable */ 501 tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios); 502 tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios); 503 504 ld = tty_ldisc_ref(tty); 505 506 if (ld != NULL) { 507 if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer) 508 ld->ops->flush_buffer(tty); 509 tty_ldisc_deref(ld); 510 } 511 512 if (opt & TERMIOS_WAIT) { 513 tty_wait_until_sent(tty, 0); 514 if (signal_pending(current)) 515 return -ERESTARTSYS; 516 } 517 518 tty_set_termios(tty, &tmp_termios); 519 520 /* FIXME: Arguably if tmp_termios == tty->termios AND the 521 actual requested termios was not tmp_termios then we may 522 want to return an error as no user requested change has 523 succeeded */ 524 return 0; 525 } 526 527 static void copy_termios(struct tty_struct *tty, struct ktermios *kterm) 528 { 529 down_read(&tty->termios_rwsem); 530 *kterm = tty->termios; 531 up_read(&tty->termios_rwsem); 532 } 533 534 static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm) 535 { 536 down_read(&tty->termios_rwsem); 537 *kterm = tty->termios_locked; 538 up_read(&tty->termios_rwsem); 539 } 540 541 static int get_termio(struct tty_struct *tty, struct termio __user *termio) 542 { 543 struct ktermios kterm; 544 copy_termios(tty, &kterm); 545 if (kernel_termios_to_user_termio(termio, &kterm)) 546 return -EFAULT; 547 return 0; 548 } 549 550 #ifdef TIOCGETP 551 /* 552 * These are deprecated, but there is limited support.. 553 * 554 * The "sg_flags" translation is a joke.. 555 */ 556 static int get_sgflags(struct tty_struct *tty) 557 { 558 int flags = 0; 559 560 if (!L_ICANON(tty)) { 561 if (L_ISIG(tty)) 562 flags |= 0x02; /* cbreak */ 563 else 564 flags |= 0x20; /* raw */ 565 } 566 if (L_ECHO(tty)) 567 flags |= 0x08; /* echo */ 568 if (O_OPOST(tty)) 569 if (O_ONLCR(tty)) 570 flags |= 0x10; /* crmod */ 571 return flags; 572 } 573 574 static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb) 575 { 576 struct sgttyb tmp; 577 578 down_read(&tty->termios_rwsem); 579 tmp.sg_ispeed = tty->termios.c_ispeed; 580 tmp.sg_ospeed = tty->termios.c_ospeed; 581 tmp.sg_erase = tty->termios.c_cc[VERASE]; 582 tmp.sg_kill = tty->termios.c_cc[VKILL]; 583 tmp.sg_flags = get_sgflags(tty); 584 up_read(&tty->termios_rwsem); 585 586 return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0; 587 } 588 589 static void set_sgflags(struct ktermios *termios, int flags) 590 { 591 termios->c_iflag = ICRNL | IXON; 592 termios->c_oflag = 0; 593 termios->c_lflag = ISIG | ICANON; 594 if (flags & 0x02) { /* cbreak */ 595 termios->c_iflag = 0; 596 termios->c_lflag &= ~ICANON; 597 } 598 if (flags & 0x08) { /* echo */ 599 termios->c_lflag |= ECHO | ECHOE | ECHOK | 600 ECHOCTL | ECHOKE | IEXTEN; 601 } 602 if (flags & 0x10) { /* crmod */ 603 termios->c_oflag |= OPOST | ONLCR; 604 } 605 if (flags & 0x20) { /* raw */ 606 termios->c_iflag = 0; 607 termios->c_lflag &= ~(ISIG | ICANON); 608 } 609 if (!(termios->c_lflag & ICANON)) { 610 termios->c_cc[VMIN] = 1; 611 termios->c_cc[VTIME] = 0; 612 } 613 } 614 615 /** 616 * set_sgttyb - set legacy terminal values 617 * @tty: tty structure 618 * @sgttyb: pointer to old style terminal structure 619 * 620 * Updates a terminal from the legacy BSD style terminal information 621 * structure. 622 * 623 * Locking: termios_rwsem 624 */ 625 626 static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb) 627 { 628 int retval; 629 struct sgttyb tmp; 630 struct ktermios termios; 631 632 retval = tty_check_change(tty); 633 if (retval) 634 return retval; 635 636 if (copy_from_user(&tmp, sgttyb, sizeof(tmp))) 637 return -EFAULT; 638 639 down_write(&tty->termios_rwsem); 640 termios = tty->termios; 641 termios.c_cc[VERASE] = tmp.sg_erase; 642 termios.c_cc[VKILL] = tmp.sg_kill; 643 set_sgflags(&termios, tmp.sg_flags); 644 /* Try and encode into Bfoo format */ 645 tty_termios_encode_baud_rate(&termios, termios.c_ispeed, 646 termios.c_ospeed); 647 up_write(&tty->termios_rwsem); 648 tty_set_termios(tty, &termios); 649 return 0; 650 } 651 #endif 652 653 #ifdef TIOCGETC 654 static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars) 655 { 656 struct tchars tmp; 657 658 down_read(&tty->termios_rwsem); 659 tmp.t_intrc = tty->termios.c_cc[VINTR]; 660 tmp.t_quitc = tty->termios.c_cc[VQUIT]; 661 tmp.t_startc = tty->termios.c_cc[VSTART]; 662 tmp.t_stopc = tty->termios.c_cc[VSTOP]; 663 tmp.t_eofc = tty->termios.c_cc[VEOF]; 664 tmp.t_brkc = tty->termios.c_cc[VEOL2]; /* what is brkc anyway? */ 665 up_read(&tty->termios_rwsem); 666 return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0; 667 } 668 669 static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars) 670 { 671 struct tchars tmp; 672 673 if (copy_from_user(&tmp, tchars, sizeof(tmp))) 674 return -EFAULT; 675 down_write(&tty->termios_rwsem); 676 tty->termios.c_cc[VINTR] = tmp.t_intrc; 677 tty->termios.c_cc[VQUIT] = tmp.t_quitc; 678 tty->termios.c_cc[VSTART] = tmp.t_startc; 679 tty->termios.c_cc[VSTOP] = tmp.t_stopc; 680 tty->termios.c_cc[VEOF] = tmp.t_eofc; 681 tty->termios.c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */ 682 up_write(&tty->termios_rwsem); 683 return 0; 684 } 685 #endif 686 687 #ifdef TIOCGLTC 688 static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars) 689 { 690 struct ltchars tmp; 691 692 down_read(&tty->termios_rwsem); 693 tmp.t_suspc = tty->termios.c_cc[VSUSP]; 694 /* what is dsuspc anyway? */ 695 tmp.t_dsuspc = tty->termios.c_cc[VSUSP]; 696 tmp.t_rprntc = tty->termios.c_cc[VREPRINT]; 697 /* what is flushc anyway? */ 698 tmp.t_flushc = tty->termios.c_cc[VEOL2]; 699 tmp.t_werasc = tty->termios.c_cc[VWERASE]; 700 tmp.t_lnextc = tty->termios.c_cc[VLNEXT]; 701 up_read(&tty->termios_rwsem); 702 return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0; 703 } 704 705 static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars) 706 { 707 struct ltchars tmp; 708 709 if (copy_from_user(&tmp, ltchars, sizeof(tmp))) 710 return -EFAULT; 711 712 down_write(&tty->termios_rwsem); 713 tty->termios.c_cc[VSUSP] = tmp.t_suspc; 714 /* what is dsuspc anyway? */ 715 tty->termios.c_cc[VEOL2] = tmp.t_dsuspc; 716 tty->termios.c_cc[VREPRINT] = tmp.t_rprntc; 717 /* what is flushc anyway? */ 718 tty->termios.c_cc[VEOL2] = tmp.t_flushc; 719 tty->termios.c_cc[VWERASE] = tmp.t_werasc; 720 tty->termios.c_cc[VLNEXT] = tmp.t_lnextc; 721 up_write(&tty->termios_rwsem); 722 return 0; 723 } 724 #endif 725 726 /** 727 * tty_change_softcar - carrier change ioctl helper 728 * @tty: tty to update 729 * @arg: enable/disable CLOCAL 730 * 731 * Perform a change to the CLOCAL state and call into the driver 732 * layer to make it visible. All done with the termios rwsem 733 */ 734 735 static int tty_change_softcar(struct tty_struct *tty, int arg) 736 { 737 int ret = 0; 738 int bit = arg ? CLOCAL : 0; 739 struct ktermios old; 740 741 down_write(&tty->termios_rwsem); 742 old = tty->termios; 743 tty->termios.c_cflag &= ~CLOCAL; 744 tty->termios.c_cflag |= bit; 745 if (tty->ops->set_termios) 746 tty->ops->set_termios(tty, &old); 747 if (C_CLOCAL(tty) != bit) 748 ret = -EINVAL; 749 up_write(&tty->termios_rwsem); 750 return ret; 751 } 752 753 /** 754 * tty_mode_ioctl - mode related ioctls 755 * @tty: tty for the ioctl 756 * @cmd: command 757 * @arg: ioctl argument 758 * 759 * Perform non line discipline specific mode control ioctls. This 760 * is designed to be called by line disciplines to ensure they provide 761 * consistent mode setting. 762 */ 763 764 int tty_mode_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg) 765 { 766 struct tty_struct *real_tty; 767 void __user *p = (void __user *)arg; 768 int ret = 0; 769 struct ktermios kterm; 770 771 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 772 tty->driver->subtype == PTY_TYPE_MASTER) 773 real_tty = tty->link; 774 else 775 real_tty = tty; 776 777 switch (cmd) { 778 #ifdef TIOCGETP 779 case TIOCGETP: 780 return get_sgttyb(real_tty, (struct sgttyb __user *) arg); 781 case TIOCSETP: 782 case TIOCSETN: 783 return set_sgttyb(real_tty, (struct sgttyb __user *) arg); 784 #endif 785 #ifdef TIOCGETC 786 case TIOCGETC: 787 return get_tchars(real_tty, p); 788 case TIOCSETC: 789 return set_tchars(real_tty, p); 790 #endif 791 #ifdef TIOCGLTC 792 case TIOCGLTC: 793 return get_ltchars(real_tty, p); 794 case TIOCSLTC: 795 return set_ltchars(real_tty, p); 796 #endif 797 case TCSETSF: 798 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD); 799 case TCSETSW: 800 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD); 801 case TCSETS: 802 return set_termios(real_tty, p, TERMIOS_OLD); 803 #ifndef TCGETS2 804 case TCGETS: 805 copy_termios(real_tty, &kterm); 806 if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm)) 807 ret = -EFAULT; 808 return ret; 809 #else 810 case TCGETS: 811 copy_termios(real_tty, &kterm); 812 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm)) 813 ret = -EFAULT; 814 return ret; 815 case TCGETS2: 816 copy_termios(real_tty, &kterm); 817 if (kernel_termios_to_user_termios((struct termios2 __user *)arg, &kterm)) 818 ret = -EFAULT; 819 return ret; 820 case TCSETSF2: 821 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT); 822 case TCSETSW2: 823 return set_termios(real_tty, p, TERMIOS_WAIT); 824 case TCSETS2: 825 return set_termios(real_tty, p, 0); 826 #endif 827 case TCGETA: 828 return get_termio(real_tty, p); 829 case TCSETAF: 830 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO); 831 case TCSETAW: 832 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO); 833 case TCSETA: 834 return set_termios(real_tty, p, TERMIOS_TERMIO); 835 #ifndef TCGETS2 836 case TIOCGLCKTRMIOS: 837 copy_termios_locked(real_tty, &kterm); 838 if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm)) 839 ret = -EFAULT; 840 return ret; 841 case TIOCSLCKTRMIOS: 842 if (!capable(CAP_SYS_ADMIN)) 843 return -EPERM; 844 copy_termios_locked(real_tty, &kterm); 845 if (user_termios_to_kernel_termios(&kterm, 846 (struct termios __user *) arg)) 847 return -EFAULT; 848 down_write(&real_tty->termios_rwsem); 849 real_tty->termios_locked = kterm; 850 up_write(&real_tty->termios_rwsem); 851 return 0; 852 #else 853 case TIOCGLCKTRMIOS: 854 copy_termios_locked(real_tty, &kterm); 855 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm)) 856 ret = -EFAULT; 857 return ret; 858 case TIOCSLCKTRMIOS: 859 if (!capable(CAP_SYS_ADMIN)) 860 return -EPERM; 861 copy_termios_locked(real_tty, &kterm); 862 if (user_termios_to_kernel_termios_1(&kterm, 863 (struct termios __user *) arg)) 864 return -EFAULT; 865 down_write(&real_tty->termios_rwsem); 866 real_tty->termios_locked = kterm; 867 up_write(&real_tty->termios_rwsem); 868 return ret; 869 #endif 870 #ifdef TCGETX 871 case TCGETX: 872 case TCSETX: 873 case TCSETXW: 874 case TCSETXF: 875 return -ENOTTY; 876 #endif 877 case TIOCGSOFTCAR: 878 copy_termios(real_tty, &kterm); 879 ret = put_user((kterm.c_cflag & CLOCAL) ? 1 : 0, 880 (int __user *)arg); 881 return ret; 882 case TIOCSSOFTCAR: 883 if (get_user(arg, (unsigned int __user *) arg)) 884 return -EFAULT; 885 return tty_change_softcar(real_tty, arg); 886 default: 887 return -ENOIOCTLCMD; 888 } 889 } 890 EXPORT_SYMBOL_GPL(tty_mode_ioctl); 891 892 893 /* Caller guarantees ldisc reference is held */ 894 static int __tty_perform_flush(struct tty_struct *tty, unsigned long arg) 895 { 896 struct tty_ldisc *ld = tty->ldisc; 897 898 switch (arg) { 899 case TCIFLUSH: 900 if (ld && ld->ops->flush_buffer) { 901 ld->ops->flush_buffer(tty); 902 tty_unthrottle(tty); 903 } 904 break; 905 case TCIOFLUSH: 906 if (ld && ld->ops->flush_buffer) { 907 ld->ops->flush_buffer(tty); 908 tty_unthrottle(tty); 909 } 910 fallthrough; 911 case TCOFLUSH: 912 tty_driver_flush_buffer(tty); 913 break; 914 default: 915 return -EINVAL; 916 } 917 return 0; 918 } 919 920 int tty_perform_flush(struct tty_struct *tty, unsigned long arg) 921 { 922 struct tty_ldisc *ld; 923 int retval = tty_check_change(tty); 924 if (retval) 925 return retval; 926 927 ld = tty_ldisc_ref_wait(tty); 928 retval = __tty_perform_flush(tty, arg); 929 if (ld) 930 tty_ldisc_deref(ld); 931 return retval; 932 } 933 EXPORT_SYMBOL_GPL(tty_perform_flush); 934 935 int n_tty_ioctl_helper(struct tty_struct *tty, unsigned int cmd, 936 unsigned long arg) 937 { 938 int retval; 939 940 switch (cmd) { 941 case TCXONC: 942 retval = tty_check_change(tty); 943 if (retval) 944 return retval; 945 switch (arg) { 946 case TCOOFF: 947 spin_lock_irq(&tty->flow.lock); 948 if (!tty->flow.tco_stopped) { 949 tty->flow.tco_stopped = true; 950 __stop_tty(tty); 951 } 952 spin_unlock_irq(&tty->flow.lock); 953 break; 954 case TCOON: 955 spin_lock_irq(&tty->flow.lock); 956 if (tty->flow.tco_stopped) { 957 tty->flow.tco_stopped = false; 958 __start_tty(tty); 959 } 960 spin_unlock_irq(&tty->flow.lock); 961 break; 962 case TCIOFF: 963 if (STOP_CHAR(tty) != __DISABLED_CHAR) 964 retval = tty_send_xchar(tty, STOP_CHAR(tty)); 965 break; 966 case TCION: 967 if (START_CHAR(tty) != __DISABLED_CHAR) 968 retval = tty_send_xchar(tty, START_CHAR(tty)); 969 break; 970 default: 971 return -EINVAL; 972 } 973 return retval; 974 case TCFLSH: 975 retval = tty_check_change(tty); 976 if (retval) 977 return retval; 978 return __tty_perform_flush(tty, arg); 979 default: 980 /* Try the mode commands */ 981 return tty_mode_ioctl(tty, cmd, arg); 982 } 983 } 984 EXPORT_SYMBOL(n_tty_ioctl_helper); 985