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