1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
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).
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>
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>
29 #include <linux/uaccess.h>
31 #undef TTY_DEBUG_WAIT_UNTIL_SENT
33 #ifdef TTY_DEBUG_WAIT_UNTIL_SENT
34 # define tty_debug_wait_until_sent(tty, f, args...) tty_debug(tty, f, ##args)
36 # define tty_debug_wait_until_sent(tty, f, args...) do {} while (0)
42 * Internal flag options for termios setting behavior
44 #define TERMIOS_FLUSH BIT(0)
45 #define TERMIOS_WAIT BIT(1)
46 #define TERMIOS_TERMIO BIT(2)
47 #define TERMIOS_OLD BIT(3)
51 * tty_chars_in_buffer - characters pending
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.
59 unsigned int tty_chars_in_buffer(struct tty_struct *tty)
61 if (tty->ops->chars_in_buffer)
62 return tty->ops->chars_in_buffer(tty);
65 EXPORT_SYMBOL(tty_chars_in_buffer);
68 * tty_write_room - write queue space
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.
78 unsigned int tty_write_room(struct tty_struct *tty)
80 if (tty->ops->write_room)
81 return tty->ops->write_room(tty);
84 EXPORT_SYMBOL(tty_write_room);
87 * tty_driver_flush_buffer - discard internal buffer
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.
94 void tty_driver_flush_buffer(struct tty_struct *tty)
96 if (tty->ops->flush_buffer)
97 tty->ops->flush_buffer(tty);
99 EXPORT_SYMBOL(tty_driver_flush_buffer);
102 * tty_unthrottle - flow control
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.
110 * Drivers should however remember that the stack can issue a throttle,
111 * then change flow control method, then unthrottle.
114 void tty_unthrottle(struct tty_struct *tty)
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);
123 EXPORT_SYMBOL(tty_unthrottle);
126 * tty_throttle_safe - flow control
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
135 * Returns 0 if tty is throttled (or was already throttled)
138 int tty_throttle_safe(struct tty_struct *tty)
142 mutex_lock(&tty->throttle_mutex);
143 if (!tty_throttled(tty)) {
144 if (tty->flow_change != TTY_THROTTLE_SAFE)
147 set_bit(TTY_THROTTLED, &tty->flags);
148 if (tty->ops->throttle)
149 tty->ops->throttle(tty);
152 mutex_unlock(&tty->throttle_mutex);
158 * tty_unthrottle_safe - flow control
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.
166 * Returns 0 if tty is unthrottled (or was already unthrottled)
169 int tty_unthrottle_safe(struct tty_struct *tty)
173 mutex_lock(&tty->throttle_mutex);
174 if (tty_throttled(tty)) {
175 if (tty->flow_change != TTY_UNTHROTTLE_SAFE)
178 clear_bit(TTY_THROTTLED, &tty->flags);
179 if (tty->ops->unthrottle)
180 tty->ops->unthrottle(tty);
183 mutex_unlock(&tty->throttle_mutex);
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
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)
199 void tty_wait_until_sent(struct tty_struct *tty, long timeout)
201 tty_debug_wait_until_sent(tty, "wait until sent, timeout=%ld\n", timeout);
204 timeout = MAX_SCHEDULE_TIMEOUT;
206 timeout = wait_event_interruptible_timeout(tty->write_wait,
207 !tty_chars_in_buffer(tty), timeout);
211 if (timeout == MAX_SCHEDULE_TIMEOUT)
214 if (tty->ops->wait_until_sent)
215 tty->ops->wait_until_sent(tty, timeout);
217 EXPORT_SYMBOL(tty_wait_until_sent);
221 * Termios Helper Methods
224 static void unset_locked_termios(struct tty_struct *tty, const struct ktermios *old)
226 struct ktermios *termios = &tty->termios;
227 struct ktermios *locked = &tty->termios_locked;
230 #define NOSET_MASK(x, y, z) (x = ((x) & ~(z)) | ((y) & (z)))
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 */
244 * tty_termios_copy_hw - copy hardware settings
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
254 void tty_termios_copy_hw(struct ktermios *new, const struct ktermios *old)
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;
263 EXPORT_SYMBOL(tty_termios_copy_hw);
266 * tty_termios_hw_change - check for setting change
268 * @b: termios to compare
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.
274 bool tty_termios_hw_change(const struct ktermios *a, const struct ktermios *b)
276 if (a->c_ispeed != b->c_ispeed || a->c_ospeed != b->c_ospeed)
278 if ((a->c_cflag ^ b->c_cflag) & ~(HUPCL | CREAD | CLOCAL))
282 EXPORT_SYMBOL(tty_termios_hw_change);
285 * tty_get_char_size - get size of a character
286 * @cflag: termios cflag value
288 * Get the size (in bits) of a character depending on @cflag's %CSIZE
291 unsigned char tty_get_char_size(unsigned int cflag)
293 switch (cflag & CSIZE) {
305 EXPORT_SYMBOL_GPL(tty_get_char_size);
308 * tty_get_frame_size - get size of a frame
309 * @cflag: termios cflag value
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
316 unsigned char tty_get_frame_size(unsigned int cflag)
318 unsigned char bits = 2 + tty_get_char_size(cflag);
329 EXPORT_SYMBOL_GPL(tty_get_frame_size);
332 * tty_set_termios - update termios values
333 * @tty: tty to update
334 * @new_termios: desired new value
336 * Perform updates to the termios values set on this terminal.
337 * A master pty's termios should never be set.
339 * Locking: termios_rwsem
342 int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios)
344 struct ktermios old_termios;
345 struct tty_ldisc *ld;
347 WARN_ON(tty->driver->type == TTY_DRIVER_TYPE_PTY &&
348 tty->driver->subtype == PTY_TYPE_MASTER);
350 * Perform the actual termios internal changes under lock.
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;
363 if (tty->ops->set_termios)
364 tty->ops->set_termios(tty, &old_termios);
366 tty_termios_copy_hw(&tty->termios, &old_termios);
368 ld = tty_ldisc_ref(tty);
370 if (ld->ops->set_termios)
371 ld->ops->set_termios(tty, &old_termios);
374 up_write(&tty->termios_rwsem);
377 EXPORT_SYMBOL_GPL(tty_set_termios);
381 * Translate a "termio" structure into a "termios". Ugh.
383 __weak int user_termio_to_kernel_termios(struct ktermios *termios,
384 struct termio __user *termio)
388 if (copy_from_user(&v, termio, sizeof(struct termio)))
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);
401 * Translate a "termios" structure into a "termio". Ugh.
403 __weak int kernel_termios_to_user_termio(struct termio __user *termio,
404 struct ktermios *termios)
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));
418 __weak int user_termios_to_kernel_termios(struct ktermios *k,
419 struct termios2 __user *u)
421 return copy_from_user(k, u, sizeof(struct termios2));
423 __weak int kernel_termios_to_user_termios(struct termios2 __user *u,
426 return copy_to_user(u, k, sizeof(struct termios2));
428 __weak int user_termios_to_kernel_termios_1(struct ktermios *k,
429 struct termios __user *u)
431 return copy_from_user(k, u, sizeof(struct termios));
433 __weak int kernel_termios_to_user_termios_1(struct termios __user *u,
436 return copy_to_user(u, k, sizeof(struct termios));
441 __weak int user_termios_to_kernel_termios(struct ktermios *k,
442 struct termios __user *u)
444 return copy_from_user(k, u, sizeof(struct termios));
446 __weak int kernel_termios_to_user_termios(struct termios __user *u,
449 return copy_to_user(u, k, sizeof(struct termios));
454 * set_termios - set termios values for a tty
455 * @tty: terminal device
457 * @opt: option information
459 * Helper function to prepare termios data and run necessary other
460 * functions before using tty_set_termios to do the actual changes.
463 * Called functions take ldisc and termios_rwsem locks
466 static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
468 struct ktermios tmp_termios;
469 struct tty_ldisc *ld;
470 int retval = tty_check_change(tty);
475 down_read(&tty->termios_rwsem);
476 tmp_termios = tty->termios;
477 up_read(&tty->termios_rwsem);
479 if (opt & TERMIOS_TERMIO) {
480 if (user_termio_to_kernel_termios(&tmp_termios,
481 (struct termio __user *)arg))
484 } else if (opt & TERMIOS_OLD) {
485 if (user_termios_to_kernel_termios_1(&tmp_termios,
486 (struct termios __user *)arg))
489 if (user_termios_to_kernel_termios(&tmp_termios,
490 (struct termios2 __user *)arg))
494 } else if (user_termios_to_kernel_termios(&tmp_termios,
495 (struct termios __user *)arg))
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);
504 if (opt & (TERMIOS_FLUSH|TERMIOS_WAIT)) {
506 retval = wait_event_interruptible(tty->write_wait, !tty_chars_in_buffer(tty));
510 if (tty_write_lock(tty, 0) < 0)
511 goto retry_write_wait;
514 if (tty_chars_in_buffer(tty)) {
515 tty_write_unlock(tty);
516 goto retry_write_wait;
519 ld = tty_ldisc_ref(tty);
521 if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
522 ld->ops->flush_buffer(tty);
526 if ((opt & TERMIOS_WAIT) && tty->ops->wait_until_sent) {
527 tty->ops->wait_until_sent(tty, 0);
528 if (signal_pending(current)) {
529 tty_write_unlock(tty);
534 tty_set_termios(tty, &tmp_termios);
536 tty_write_unlock(tty);
538 tty_set_termios(tty, &tmp_termios);
541 /* FIXME: Arguably if tmp_termios == tty->termios AND the
542 actual requested termios was not tmp_termios then we may
543 want to return an error as no user requested change has
548 static void copy_termios(struct tty_struct *tty, struct ktermios *kterm)
550 down_read(&tty->termios_rwsem);
551 *kterm = tty->termios;
552 up_read(&tty->termios_rwsem);
555 static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm)
557 down_read(&tty->termios_rwsem);
558 *kterm = tty->termios_locked;
559 up_read(&tty->termios_rwsem);
562 static int get_termio(struct tty_struct *tty, struct termio __user *termio)
564 struct ktermios kterm;
565 copy_termios(tty, &kterm);
566 if (kernel_termios_to_user_termio(termio, &kterm))
573 * These are deprecated, but there is limited support..
575 * The "sg_flags" translation is a joke..
577 static int get_sgflags(struct tty_struct *tty)
581 if (!L_ICANON(tty)) {
583 flags |= 0x02; /* cbreak */
585 flags |= 0x20; /* raw */
588 flags |= 0x08; /* echo */
591 flags |= 0x10; /* crmod */
595 static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
599 down_read(&tty->termios_rwsem);
600 tmp.sg_ispeed = tty->termios.c_ispeed;
601 tmp.sg_ospeed = tty->termios.c_ospeed;
602 tmp.sg_erase = tty->termios.c_cc[VERASE];
603 tmp.sg_kill = tty->termios.c_cc[VKILL];
604 tmp.sg_flags = get_sgflags(tty);
605 up_read(&tty->termios_rwsem);
607 return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
610 static void set_sgflags(struct ktermios *termios, int flags)
612 termios->c_iflag = ICRNL | IXON;
613 termios->c_oflag = 0;
614 termios->c_lflag = ISIG | ICANON;
615 if (flags & 0x02) { /* cbreak */
616 termios->c_iflag = 0;
617 termios->c_lflag &= ~ICANON;
619 if (flags & 0x08) { /* echo */
620 termios->c_lflag |= ECHO | ECHOE | ECHOK |
621 ECHOCTL | ECHOKE | IEXTEN;
623 if (flags & 0x10) { /* crmod */
624 termios->c_oflag |= OPOST | ONLCR;
626 if (flags & 0x20) { /* raw */
627 termios->c_iflag = 0;
628 termios->c_lflag &= ~(ISIG | ICANON);
630 if (!(termios->c_lflag & ICANON)) {
631 termios->c_cc[VMIN] = 1;
632 termios->c_cc[VTIME] = 0;
637 * set_sgttyb - set legacy terminal values
638 * @tty: tty structure
639 * @sgttyb: pointer to old style terminal structure
641 * Updates a terminal from the legacy BSD style terminal information
644 * Locking: termios_rwsem
647 static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
651 struct ktermios termios;
653 retval = tty_check_change(tty);
657 if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
660 down_write(&tty->termios_rwsem);
661 termios = tty->termios;
662 termios.c_cc[VERASE] = tmp.sg_erase;
663 termios.c_cc[VKILL] = tmp.sg_kill;
664 set_sgflags(&termios, tmp.sg_flags);
665 /* Try and encode into Bfoo format */
666 tty_termios_encode_baud_rate(&termios, termios.c_ispeed,
668 up_write(&tty->termios_rwsem);
669 tty_set_termios(tty, &termios);
675 static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars)
679 down_read(&tty->termios_rwsem);
680 tmp.t_intrc = tty->termios.c_cc[VINTR];
681 tmp.t_quitc = tty->termios.c_cc[VQUIT];
682 tmp.t_startc = tty->termios.c_cc[VSTART];
683 tmp.t_stopc = tty->termios.c_cc[VSTOP];
684 tmp.t_eofc = tty->termios.c_cc[VEOF];
685 tmp.t_brkc = tty->termios.c_cc[VEOL2]; /* what is brkc anyway? */
686 up_read(&tty->termios_rwsem);
687 return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
690 static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars)
694 if (copy_from_user(&tmp, tchars, sizeof(tmp)))
696 down_write(&tty->termios_rwsem);
697 tty->termios.c_cc[VINTR] = tmp.t_intrc;
698 tty->termios.c_cc[VQUIT] = tmp.t_quitc;
699 tty->termios.c_cc[VSTART] = tmp.t_startc;
700 tty->termios.c_cc[VSTOP] = tmp.t_stopc;
701 tty->termios.c_cc[VEOF] = tmp.t_eofc;
702 tty->termios.c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
703 up_write(&tty->termios_rwsem);
709 static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
713 down_read(&tty->termios_rwsem);
714 tmp.t_suspc = tty->termios.c_cc[VSUSP];
715 /* what is dsuspc anyway? */
716 tmp.t_dsuspc = tty->termios.c_cc[VSUSP];
717 tmp.t_rprntc = tty->termios.c_cc[VREPRINT];
718 /* what is flushc anyway? */
719 tmp.t_flushc = tty->termios.c_cc[VEOL2];
720 tmp.t_werasc = tty->termios.c_cc[VWERASE];
721 tmp.t_lnextc = tty->termios.c_cc[VLNEXT];
722 up_read(&tty->termios_rwsem);
723 return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
726 static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
730 if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
733 down_write(&tty->termios_rwsem);
734 tty->termios.c_cc[VSUSP] = tmp.t_suspc;
735 /* what is dsuspc anyway? */
736 tty->termios.c_cc[VEOL2] = tmp.t_dsuspc;
737 tty->termios.c_cc[VREPRINT] = tmp.t_rprntc;
738 /* what is flushc anyway? */
739 tty->termios.c_cc[VEOL2] = tmp.t_flushc;
740 tty->termios.c_cc[VWERASE] = tmp.t_werasc;
741 tty->termios.c_cc[VLNEXT] = tmp.t_lnextc;
742 up_write(&tty->termios_rwsem);
748 * tty_change_softcar - carrier change ioctl helper
749 * @tty: tty to update
750 * @arg: enable/disable CLOCAL
752 * Perform a change to the CLOCAL state and call into the driver
753 * layer to make it visible. All done with the termios rwsem
756 static int tty_change_softcar(struct tty_struct *tty, int arg)
759 int bit = arg ? CLOCAL : 0;
762 down_write(&tty->termios_rwsem);
764 tty->termios.c_cflag &= ~CLOCAL;
765 tty->termios.c_cflag |= bit;
766 if (tty->ops->set_termios)
767 tty->ops->set_termios(tty, &old);
768 if (C_CLOCAL(tty) != bit)
770 up_write(&tty->termios_rwsem);
775 * tty_mode_ioctl - mode related ioctls
776 * @tty: tty for the ioctl
778 * @arg: ioctl argument
780 * Perform non line discipline specific mode control ioctls. This
781 * is designed to be called by line disciplines to ensure they provide
782 * consistent mode setting.
785 int tty_mode_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
787 struct tty_struct *real_tty;
788 void __user *p = (void __user *)arg;
790 struct ktermios kterm;
792 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
793 tty->driver->subtype == PTY_TYPE_MASTER)
794 real_tty = tty->link;
801 return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
804 return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
808 return get_tchars(real_tty, p);
810 return set_tchars(real_tty, p);
814 return get_ltchars(real_tty, p);
816 return set_ltchars(real_tty, p);
819 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
821 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
823 return set_termios(real_tty, p, TERMIOS_OLD);
826 copy_termios(real_tty, &kterm);
827 if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
832 copy_termios(real_tty, &kterm);
833 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
837 copy_termios(real_tty, &kterm);
838 if (kernel_termios_to_user_termios((struct termios2 __user *)arg, &kterm))
842 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
844 return set_termios(real_tty, p, TERMIOS_WAIT);
846 return set_termios(real_tty, p, 0);
849 return get_termio(real_tty, p);
851 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
853 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
855 return set_termios(real_tty, p, TERMIOS_TERMIO);
858 copy_termios_locked(real_tty, &kterm);
859 if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
863 if (!capable(CAP_SYS_ADMIN))
865 copy_termios_locked(real_tty, &kterm);
866 if (user_termios_to_kernel_termios(&kterm,
867 (struct termios __user *) arg))
869 down_write(&real_tty->termios_rwsem);
870 real_tty->termios_locked = kterm;
871 up_write(&real_tty->termios_rwsem);
875 copy_termios_locked(real_tty, &kterm);
876 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
880 if (!capable(CAP_SYS_ADMIN))
882 copy_termios_locked(real_tty, &kterm);
883 if (user_termios_to_kernel_termios_1(&kterm,
884 (struct termios __user *) arg))
886 down_write(&real_tty->termios_rwsem);
887 real_tty->termios_locked = kterm;
888 up_write(&real_tty->termios_rwsem);
899 copy_termios(real_tty, &kterm);
900 ret = put_user((kterm.c_cflag & CLOCAL) ? 1 : 0,
904 if (get_user(arg, (unsigned int __user *) arg))
906 return tty_change_softcar(real_tty, arg);
911 EXPORT_SYMBOL_GPL(tty_mode_ioctl);
914 /* Caller guarantees ldisc reference is held */
915 static int __tty_perform_flush(struct tty_struct *tty, unsigned long arg)
917 struct tty_ldisc *ld = tty->ldisc;
921 if (ld && ld->ops->flush_buffer) {
922 ld->ops->flush_buffer(tty);
927 if (ld && ld->ops->flush_buffer) {
928 ld->ops->flush_buffer(tty);
933 tty_driver_flush_buffer(tty);
941 int tty_perform_flush(struct tty_struct *tty, unsigned long arg)
943 struct tty_ldisc *ld;
944 int retval = tty_check_change(tty);
948 ld = tty_ldisc_ref_wait(tty);
949 retval = __tty_perform_flush(tty, arg);
954 EXPORT_SYMBOL_GPL(tty_perform_flush);
956 int n_tty_ioctl_helper(struct tty_struct *tty, unsigned int cmd,
963 retval = tty_check_change(tty);
968 spin_lock_irq(&tty->flow.lock);
969 if (!tty->flow.tco_stopped) {
970 tty->flow.tco_stopped = true;
973 spin_unlock_irq(&tty->flow.lock);
976 spin_lock_irq(&tty->flow.lock);
977 if (tty->flow.tco_stopped) {
978 tty->flow.tco_stopped = false;
981 spin_unlock_irq(&tty->flow.lock);
984 if (STOP_CHAR(tty) != __DISABLED_CHAR)
985 retval = tty_send_xchar(tty, STOP_CHAR(tty));
988 if (START_CHAR(tty) != __DISABLED_CHAR)
989 retval = tty_send_xchar(tty, START_CHAR(tty));
996 retval = tty_check_change(tty);
999 return __tty_perform_flush(tty, arg);
1001 /* Try the mode commands */
1002 return tty_mode_ioctl(tty, cmd, arg);
1005 EXPORT_SYMBOL(n_tty_ioctl_helper);