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, const 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, const 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_get_char_size - get size of a character 284 * @cflag: termios cflag value 285 * 286 * Get the size (in bits) of a character depending on @cflag's %CSIZE 287 * setting. 288 */ 289 unsigned char tty_get_char_size(unsigned int cflag) 290 { 291 switch (cflag & CSIZE) { 292 case CS5: 293 return 5; 294 case CS6: 295 return 6; 296 case CS7: 297 return 7; 298 case CS8: 299 default: 300 return 8; 301 } 302 } 303 EXPORT_SYMBOL_GPL(tty_get_char_size); 304 305 /** 306 * tty_get_frame_size - get size of a frame 307 * @cflag: termios cflag value 308 * 309 * Get the size (in bits) of a frame depending on @cflag's %CSIZE, %CSTOPB, 310 * and %PARENB setting. The result is a sum of character size, start and 311 * stop bits -- one bit each -- second stop bit (if set), and parity bit 312 * (if set). 313 */ 314 unsigned char tty_get_frame_size(unsigned int cflag) 315 { 316 unsigned char bits = 2 + tty_get_char_size(cflag); 317 318 if (cflag & CSTOPB) 319 bits++; 320 if (cflag & PARENB) 321 bits++; 322 if (cflag & ADDRB) 323 bits++; 324 325 return bits; 326 } 327 EXPORT_SYMBOL_GPL(tty_get_frame_size); 328 329 /** 330 * tty_set_termios - update termios values 331 * @tty: tty to update 332 * @new_termios: desired new value 333 * 334 * Perform updates to the termios values set on this terminal. 335 * A master pty's termios should never be set. 336 * 337 * Locking: termios_rwsem 338 */ 339 340 int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios) 341 { 342 struct ktermios old_termios; 343 struct tty_ldisc *ld; 344 345 WARN_ON(tty->driver->type == TTY_DRIVER_TYPE_PTY && 346 tty->driver->subtype == PTY_TYPE_MASTER); 347 /* 348 * Perform the actual termios internal changes under lock. 349 */ 350 351 352 /* FIXME: we need to decide on some locking/ordering semantics 353 for the set_termios notification eventually */ 354 down_write(&tty->termios_rwsem); 355 old_termios = tty->termios; 356 tty->termios = *new_termios; 357 unset_locked_termios(tty, &old_termios); 358 /* Reset any ADDRB changes, ADDRB is changed through ->rs485_config() */ 359 tty->termios.c_cflag ^= (tty->termios.c_cflag ^ old_termios.c_cflag) & ADDRB; 360 361 if (tty->ops->set_termios) 362 tty->ops->set_termios(tty, &old_termios); 363 else 364 tty_termios_copy_hw(&tty->termios, &old_termios); 365 366 ld = tty_ldisc_ref(tty); 367 if (ld != NULL) { 368 if (ld->ops->set_termios) 369 ld->ops->set_termios(tty, &old_termios); 370 tty_ldisc_deref(ld); 371 } 372 up_write(&tty->termios_rwsem); 373 return 0; 374 } 375 EXPORT_SYMBOL_GPL(tty_set_termios); 376 377 378 /* 379 * Translate a "termio" structure into a "termios". Ugh. 380 */ 381 __weak int user_termio_to_kernel_termios(struct ktermios *termios, 382 struct termio __user *termio) 383 { 384 struct termio v; 385 386 if (copy_from_user(&v, termio, sizeof(struct termio))) 387 return -EFAULT; 388 389 termios->c_iflag = (0xffff0000 & termios->c_iflag) | v.c_iflag; 390 termios->c_oflag = (0xffff0000 & termios->c_oflag) | v.c_oflag; 391 termios->c_cflag = (0xffff0000 & termios->c_cflag) | v.c_cflag; 392 termios->c_lflag = (0xffff0000 & termios->c_lflag) | v.c_lflag; 393 termios->c_line = (0xffff0000 & termios->c_lflag) | v.c_line; 394 memcpy(termios->c_cc, v.c_cc, NCC); 395 return 0; 396 } 397 398 /* 399 * Translate a "termios" structure into a "termio". Ugh. 400 */ 401 __weak int kernel_termios_to_user_termio(struct termio __user *termio, 402 struct ktermios *termios) 403 { 404 struct termio v; 405 memset(&v, 0, sizeof(struct termio)); 406 v.c_iflag = termios->c_iflag; 407 v.c_oflag = termios->c_oflag; 408 v.c_cflag = termios->c_cflag; 409 v.c_lflag = termios->c_lflag; 410 v.c_line = termios->c_line; 411 memcpy(v.c_cc, termios->c_cc, NCC); 412 return copy_to_user(termio, &v, sizeof(struct termio)); 413 } 414 415 #ifdef TCGETS2 416 __weak int user_termios_to_kernel_termios(struct ktermios *k, 417 struct termios2 __user *u) 418 { 419 return copy_from_user(k, u, sizeof(struct termios2)); 420 } 421 __weak int kernel_termios_to_user_termios(struct termios2 __user *u, 422 struct ktermios *k) 423 { 424 return copy_to_user(u, k, sizeof(struct termios2)); 425 } 426 __weak int user_termios_to_kernel_termios_1(struct ktermios *k, 427 struct termios __user *u) 428 { 429 return copy_from_user(k, u, sizeof(struct termios)); 430 } 431 __weak int kernel_termios_to_user_termios_1(struct termios __user *u, 432 struct ktermios *k) 433 { 434 return copy_to_user(u, k, sizeof(struct termios)); 435 } 436 437 #else 438 439 __weak int user_termios_to_kernel_termios(struct ktermios *k, 440 struct termios __user *u) 441 { 442 return copy_from_user(k, u, sizeof(struct termios)); 443 } 444 __weak int kernel_termios_to_user_termios(struct termios __user *u, 445 struct ktermios *k) 446 { 447 return copy_to_user(u, k, sizeof(struct termios)); 448 } 449 #endif /* TCGETS2 */ 450 451 /** 452 * set_termios - set termios values for a tty 453 * @tty: terminal device 454 * @arg: user data 455 * @opt: option information 456 * 457 * Helper function to prepare termios data and run necessary other 458 * functions before using tty_set_termios to do the actual changes. 459 * 460 * Locking: 461 * Called functions take ldisc and termios_rwsem locks 462 */ 463 464 static int set_termios(struct tty_struct *tty, void __user *arg, int opt) 465 { 466 struct ktermios tmp_termios; 467 struct tty_ldisc *ld; 468 int retval = tty_check_change(tty); 469 470 if (retval) 471 return retval; 472 473 down_read(&tty->termios_rwsem); 474 tmp_termios = tty->termios; 475 up_read(&tty->termios_rwsem); 476 477 if (opt & TERMIOS_TERMIO) { 478 if (user_termio_to_kernel_termios(&tmp_termios, 479 (struct termio __user *)arg)) 480 return -EFAULT; 481 #ifdef TCGETS2 482 } else if (opt & TERMIOS_OLD) { 483 if (user_termios_to_kernel_termios_1(&tmp_termios, 484 (struct termios __user *)arg)) 485 return -EFAULT; 486 } else { 487 if (user_termios_to_kernel_termios(&tmp_termios, 488 (struct termios2 __user *)arg)) 489 return -EFAULT; 490 } 491 #else 492 } else if (user_termios_to_kernel_termios(&tmp_termios, 493 (struct termios __user *)arg)) 494 return -EFAULT; 495 #endif 496 497 /* If old style Bfoo values are used then load c_ispeed/c_ospeed 498 * with the real speed so its unconditionally usable */ 499 tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios); 500 tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios); 501 502 ld = tty_ldisc_ref(tty); 503 504 if (ld != NULL) { 505 if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer) 506 ld->ops->flush_buffer(tty); 507 tty_ldisc_deref(ld); 508 } 509 510 if (opt & TERMIOS_WAIT) { 511 tty_wait_until_sent(tty, 0); 512 if (signal_pending(current)) 513 return -ERESTARTSYS; 514 } 515 516 tty_set_termios(tty, &tmp_termios); 517 518 /* FIXME: Arguably if tmp_termios == tty->termios AND the 519 actual requested termios was not tmp_termios then we may 520 want to return an error as no user requested change has 521 succeeded */ 522 return 0; 523 } 524 525 static void copy_termios(struct tty_struct *tty, struct ktermios *kterm) 526 { 527 down_read(&tty->termios_rwsem); 528 *kterm = tty->termios; 529 up_read(&tty->termios_rwsem); 530 } 531 532 static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm) 533 { 534 down_read(&tty->termios_rwsem); 535 *kterm = tty->termios_locked; 536 up_read(&tty->termios_rwsem); 537 } 538 539 static int get_termio(struct tty_struct *tty, struct termio __user *termio) 540 { 541 struct ktermios kterm; 542 copy_termios(tty, &kterm); 543 if (kernel_termios_to_user_termio(termio, &kterm)) 544 return -EFAULT; 545 return 0; 546 } 547 548 #ifdef TIOCGETP 549 /* 550 * These are deprecated, but there is limited support.. 551 * 552 * The "sg_flags" translation is a joke.. 553 */ 554 static int get_sgflags(struct tty_struct *tty) 555 { 556 int flags = 0; 557 558 if (!L_ICANON(tty)) { 559 if (L_ISIG(tty)) 560 flags |= 0x02; /* cbreak */ 561 else 562 flags |= 0x20; /* raw */ 563 } 564 if (L_ECHO(tty)) 565 flags |= 0x08; /* echo */ 566 if (O_OPOST(tty)) 567 if (O_ONLCR(tty)) 568 flags |= 0x10; /* crmod */ 569 return flags; 570 } 571 572 static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb) 573 { 574 struct sgttyb tmp; 575 576 down_read(&tty->termios_rwsem); 577 tmp.sg_ispeed = tty->termios.c_ispeed; 578 tmp.sg_ospeed = tty->termios.c_ospeed; 579 tmp.sg_erase = tty->termios.c_cc[VERASE]; 580 tmp.sg_kill = tty->termios.c_cc[VKILL]; 581 tmp.sg_flags = get_sgflags(tty); 582 up_read(&tty->termios_rwsem); 583 584 return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0; 585 } 586 587 static void set_sgflags(struct ktermios *termios, int flags) 588 { 589 termios->c_iflag = ICRNL | IXON; 590 termios->c_oflag = 0; 591 termios->c_lflag = ISIG | ICANON; 592 if (flags & 0x02) { /* cbreak */ 593 termios->c_iflag = 0; 594 termios->c_lflag &= ~ICANON; 595 } 596 if (flags & 0x08) { /* echo */ 597 termios->c_lflag |= ECHO | ECHOE | ECHOK | 598 ECHOCTL | ECHOKE | IEXTEN; 599 } 600 if (flags & 0x10) { /* crmod */ 601 termios->c_oflag |= OPOST | ONLCR; 602 } 603 if (flags & 0x20) { /* raw */ 604 termios->c_iflag = 0; 605 termios->c_lflag &= ~(ISIG | ICANON); 606 } 607 if (!(termios->c_lflag & ICANON)) { 608 termios->c_cc[VMIN] = 1; 609 termios->c_cc[VTIME] = 0; 610 } 611 } 612 613 /** 614 * set_sgttyb - set legacy terminal values 615 * @tty: tty structure 616 * @sgttyb: pointer to old style terminal structure 617 * 618 * Updates a terminal from the legacy BSD style terminal information 619 * structure. 620 * 621 * Locking: termios_rwsem 622 */ 623 624 static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb) 625 { 626 int retval; 627 struct sgttyb tmp; 628 struct ktermios termios; 629 630 retval = tty_check_change(tty); 631 if (retval) 632 return retval; 633 634 if (copy_from_user(&tmp, sgttyb, sizeof(tmp))) 635 return -EFAULT; 636 637 down_write(&tty->termios_rwsem); 638 termios = tty->termios; 639 termios.c_cc[VERASE] = tmp.sg_erase; 640 termios.c_cc[VKILL] = tmp.sg_kill; 641 set_sgflags(&termios, tmp.sg_flags); 642 /* Try and encode into Bfoo format */ 643 tty_termios_encode_baud_rate(&termios, termios.c_ispeed, 644 termios.c_ospeed); 645 up_write(&tty->termios_rwsem); 646 tty_set_termios(tty, &termios); 647 return 0; 648 } 649 #endif 650 651 #ifdef TIOCGETC 652 static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars) 653 { 654 struct tchars tmp; 655 656 down_read(&tty->termios_rwsem); 657 tmp.t_intrc = tty->termios.c_cc[VINTR]; 658 tmp.t_quitc = tty->termios.c_cc[VQUIT]; 659 tmp.t_startc = tty->termios.c_cc[VSTART]; 660 tmp.t_stopc = tty->termios.c_cc[VSTOP]; 661 tmp.t_eofc = tty->termios.c_cc[VEOF]; 662 tmp.t_brkc = tty->termios.c_cc[VEOL2]; /* what is brkc anyway? */ 663 up_read(&tty->termios_rwsem); 664 return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0; 665 } 666 667 static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars) 668 { 669 struct tchars tmp; 670 671 if (copy_from_user(&tmp, tchars, sizeof(tmp))) 672 return -EFAULT; 673 down_write(&tty->termios_rwsem); 674 tty->termios.c_cc[VINTR] = tmp.t_intrc; 675 tty->termios.c_cc[VQUIT] = tmp.t_quitc; 676 tty->termios.c_cc[VSTART] = tmp.t_startc; 677 tty->termios.c_cc[VSTOP] = tmp.t_stopc; 678 tty->termios.c_cc[VEOF] = tmp.t_eofc; 679 tty->termios.c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */ 680 up_write(&tty->termios_rwsem); 681 return 0; 682 } 683 #endif 684 685 #ifdef TIOCGLTC 686 static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars) 687 { 688 struct ltchars tmp; 689 690 down_read(&tty->termios_rwsem); 691 tmp.t_suspc = tty->termios.c_cc[VSUSP]; 692 /* what is dsuspc anyway? */ 693 tmp.t_dsuspc = tty->termios.c_cc[VSUSP]; 694 tmp.t_rprntc = tty->termios.c_cc[VREPRINT]; 695 /* what is flushc anyway? */ 696 tmp.t_flushc = tty->termios.c_cc[VEOL2]; 697 tmp.t_werasc = tty->termios.c_cc[VWERASE]; 698 tmp.t_lnextc = tty->termios.c_cc[VLNEXT]; 699 up_read(&tty->termios_rwsem); 700 return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0; 701 } 702 703 static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars) 704 { 705 struct ltchars tmp; 706 707 if (copy_from_user(&tmp, ltchars, sizeof(tmp))) 708 return -EFAULT; 709 710 down_write(&tty->termios_rwsem); 711 tty->termios.c_cc[VSUSP] = tmp.t_suspc; 712 /* what is dsuspc anyway? */ 713 tty->termios.c_cc[VEOL2] = tmp.t_dsuspc; 714 tty->termios.c_cc[VREPRINT] = tmp.t_rprntc; 715 /* what is flushc anyway? */ 716 tty->termios.c_cc[VEOL2] = tmp.t_flushc; 717 tty->termios.c_cc[VWERASE] = tmp.t_werasc; 718 tty->termios.c_cc[VLNEXT] = tmp.t_lnextc; 719 up_write(&tty->termios_rwsem); 720 return 0; 721 } 722 #endif 723 724 /** 725 * tty_change_softcar - carrier change ioctl helper 726 * @tty: tty to update 727 * @arg: enable/disable CLOCAL 728 * 729 * Perform a change to the CLOCAL state and call into the driver 730 * layer to make it visible. All done with the termios rwsem 731 */ 732 733 static int tty_change_softcar(struct tty_struct *tty, int arg) 734 { 735 int ret = 0; 736 int bit = arg ? CLOCAL : 0; 737 struct ktermios old; 738 739 down_write(&tty->termios_rwsem); 740 old = tty->termios; 741 tty->termios.c_cflag &= ~CLOCAL; 742 tty->termios.c_cflag |= bit; 743 if (tty->ops->set_termios) 744 tty->ops->set_termios(tty, &old); 745 if (C_CLOCAL(tty) != bit) 746 ret = -EINVAL; 747 up_write(&tty->termios_rwsem); 748 return ret; 749 } 750 751 /** 752 * tty_mode_ioctl - mode related ioctls 753 * @tty: tty for the ioctl 754 * @cmd: command 755 * @arg: ioctl argument 756 * 757 * Perform non line discipline specific mode control ioctls. This 758 * is designed to be called by line disciplines to ensure they provide 759 * consistent mode setting. 760 */ 761 762 int tty_mode_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg) 763 { 764 struct tty_struct *real_tty; 765 void __user *p = (void __user *)arg; 766 int ret = 0; 767 struct ktermios kterm; 768 769 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 770 tty->driver->subtype == PTY_TYPE_MASTER) 771 real_tty = tty->link; 772 else 773 real_tty = tty; 774 775 switch (cmd) { 776 #ifdef TIOCGETP 777 case TIOCGETP: 778 return get_sgttyb(real_tty, (struct sgttyb __user *) arg); 779 case TIOCSETP: 780 case TIOCSETN: 781 return set_sgttyb(real_tty, (struct sgttyb __user *) arg); 782 #endif 783 #ifdef TIOCGETC 784 case TIOCGETC: 785 return get_tchars(real_tty, p); 786 case TIOCSETC: 787 return set_tchars(real_tty, p); 788 #endif 789 #ifdef TIOCGLTC 790 case TIOCGLTC: 791 return get_ltchars(real_tty, p); 792 case TIOCSLTC: 793 return set_ltchars(real_tty, p); 794 #endif 795 case TCSETSF: 796 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD); 797 case TCSETSW: 798 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD); 799 case TCSETS: 800 return set_termios(real_tty, p, TERMIOS_OLD); 801 #ifndef TCGETS2 802 case TCGETS: 803 copy_termios(real_tty, &kterm); 804 if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm)) 805 ret = -EFAULT; 806 return ret; 807 #else 808 case TCGETS: 809 copy_termios(real_tty, &kterm); 810 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm)) 811 ret = -EFAULT; 812 return ret; 813 case TCGETS2: 814 copy_termios(real_tty, &kterm); 815 if (kernel_termios_to_user_termios((struct termios2 __user *)arg, &kterm)) 816 ret = -EFAULT; 817 return ret; 818 case TCSETSF2: 819 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT); 820 case TCSETSW2: 821 return set_termios(real_tty, p, TERMIOS_WAIT); 822 case TCSETS2: 823 return set_termios(real_tty, p, 0); 824 #endif 825 case TCGETA: 826 return get_termio(real_tty, p); 827 case TCSETAF: 828 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO); 829 case TCSETAW: 830 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO); 831 case TCSETA: 832 return set_termios(real_tty, p, TERMIOS_TERMIO); 833 #ifndef TCGETS2 834 case TIOCGLCKTRMIOS: 835 copy_termios_locked(real_tty, &kterm); 836 if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm)) 837 ret = -EFAULT; 838 return ret; 839 case TIOCSLCKTRMIOS: 840 if (!capable(CAP_SYS_ADMIN)) 841 return -EPERM; 842 copy_termios_locked(real_tty, &kterm); 843 if (user_termios_to_kernel_termios(&kterm, 844 (struct termios __user *) arg)) 845 return -EFAULT; 846 down_write(&real_tty->termios_rwsem); 847 real_tty->termios_locked = kterm; 848 up_write(&real_tty->termios_rwsem); 849 return 0; 850 #else 851 case TIOCGLCKTRMIOS: 852 copy_termios_locked(real_tty, &kterm); 853 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm)) 854 ret = -EFAULT; 855 return ret; 856 case TIOCSLCKTRMIOS: 857 if (!capable(CAP_SYS_ADMIN)) 858 return -EPERM; 859 copy_termios_locked(real_tty, &kterm); 860 if (user_termios_to_kernel_termios_1(&kterm, 861 (struct termios __user *) arg)) 862 return -EFAULT; 863 down_write(&real_tty->termios_rwsem); 864 real_tty->termios_locked = kterm; 865 up_write(&real_tty->termios_rwsem); 866 return ret; 867 #endif 868 #ifdef TCGETX 869 case TCGETX: 870 case TCSETX: 871 case TCSETXW: 872 case TCSETXF: 873 return -ENOTTY; 874 #endif 875 case TIOCGSOFTCAR: 876 copy_termios(real_tty, &kterm); 877 ret = put_user((kterm.c_cflag & CLOCAL) ? 1 : 0, 878 (int __user *)arg); 879 return ret; 880 case TIOCSSOFTCAR: 881 if (get_user(arg, (unsigned int __user *) arg)) 882 return -EFAULT; 883 return tty_change_softcar(real_tty, arg); 884 default: 885 return -ENOIOCTLCMD; 886 } 887 } 888 EXPORT_SYMBOL_GPL(tty_mode_ioctl); 889 890 891 /* Caller guarantees ldisc reference is held */ 892 static int __tty_perform_flush(struct tty_struct *tty, unsigned long arg) 893 { 894 struct tty_ldisc *ld = tty->ldisc; 895 896 switch (arg) { 897 case TCIFLUSH: 898 if (ld && ld->ops->flush_buffer) { 899 ld->ops->flush_buffer(tty); 900 tty_unthrottle(tty); 901 } 902 break; 903 case TCIOFLUSH: 904 if (ld && ld->ops->flush_buffer) { 905 ld->ops->flush_buffer(tty); 906 tty_unthrottle(tty); 907 } 908 fallthrough; 909 case TCOFLUSH: 910 tty_driver_flush_buffer(tty); 911 break; 912 default: 913 return -EINVAL; 914 } 915 return 0; 916 } 917 918 int tty_perform_flush(struct tty_struct *tty, unsigned long arg) 919 { 920 struct tty_ldisc *ld; 921 int retval = tty_check_change(tty); 922 if (retval) 923 return retval; 924 925 ld = tty_ldisc_ref_wait(tty); 926 retval = __tty_perform_flush(tty, arg); 927 if (ld) 928 tty_ldisc_deref(ld); 929 return retval; 930 } 931 EXPORT_SYMBOL_GPL(tty_perform_flush); 932 933 int n_tty_ioctl_helper(struct tty_struct *tty, unsigned int cmd, 934 unsigned long arg) 935 { 936 int retval; 937 938 switch (cmd) { 939 case TCXONC: 940 retval = tty_check_change(tty); 941 if (retval) 942 return retval; 943 switch (arg) { 944 case TCOOFF: 945 spin_lock_irq(&tty->flow.lock); 946 if (!tty->flow.tco_stopped) { 947 tty->flow.tco_stopped = true; 948 __stop_tty(tty); 949 } 950 spin_unlock_irq(&tty->flow.lock); 951 break; 952 case TCOON: 953 spin_lock_irq(&tty->flow.lock); 954 if (tty->flow.tco_stopped) { 955 tty->flow.tco_stopped = false; 956 __start_tty(tty); 957 } 958 spin_unlock_irq(&tty->flow.lock); 959 break; 960 case TCIOFF: 961 if (STOP_CHAR(tty) != __DISABLED_CHAR) 962 retval = tty_send_xchar(tty, STOP_CHAR(tty)); 963 break; 964 case TCION: 965 if (START_CHAR(tty) != __DISABLED_CHAR) 966 retval = tty_send_xchar(tty, START_CHAR(tty)); 967 break; 968 default: 969 return -EINVAL; 970 } 971 return retval; 972 case TCFLSH: 973 retval = tty_check_change(tty); 974 if (retval) 975 return retval; 976 return __tty_perform_flush(tty, arg); 977 default: 978 /* Try the mode commands */ 979 return tty_mode_ioctl(tty, cmd, arg); 980 } 981 } 982 EXPORT_SYMBOL(n_tty_ioctl_helper); 983