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>
34 * Internal flag options for termios setting behavior
36 #define TERMIOS_FLUSH BIT(0)
37 #define TERMIOS_WAIT BIT(1)
38 #define TERMIOS_TERMIO BIT(2)
39 #define TERMIOS_OLD BIT(3)
43 * tty_chars_in_buffer - characters pending
46 * Return the number of bytes of data in the device private
47 * output queue. If no private method is supplied there is assumed
48 * to be no queue on the device.
51 unsigned int tty_chars_in_buffer(struct tty_struct *tty)
53 if (tty->ops->chars_in_buffer)
54 return tty->ops->chars_in_buffer(tty);
57 EXPORT_SYMBOL(tty_chars_in_buffer);
60 * tty_write_room - write queue space
63 * Return the number of bytes that can be queued to this device
64 * at the present time. The result should be treated as a guarantee
65 * and the driver cannot offer a value it later shrinks by more than
66 * the number of bytes written. If no method is provided 2K is always
67 * returned and data may be lost as there will be no flow control.
70 unsigned int tty_write_room(struct tty_struct *tty)
72 if (tty->ops->write_room)
73 return tty->ops->write_room(tty);
76 EXPORT_SYMBOL(tty_write_room);
79 * tty_driver_flush_buffer - discard internal buffer
82 * Discard the internal output buffer for this device. If no method
83 * is provided then either the buffer cannot be hardware flushed or
84 * there is no buffer driver side.
86 void tty_driver_flush_buffer(struct tty_struct *tty)
88 if (tty->ops->flush_buffer)
89 tty->ops->flush_buffer(tty);
91 EXPORT_SYMBOL(tty_driver_flush_buffer);
94 * tty_unthrottle - flow control
97 * Indicate that a tty may continue transmitting data down the stack.
98 * Takes the termios rwsem to protect against parallel throttle/unthrottle
99 * and also to ensure the driver can consistently reference its own
100 * termios data at this point when implementing software flow control.
102 * Drivers should however remember that the stack can issue a throttle,
103 * then change flow control method, then unthrottle.
106 void tty_unthrottle(struct tty_struct *tty)
108 down_write(&tty->termios_rwsem);
109 if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
110 tty->ops->unthrottle)
111 tty->ops->unthrottle(tty);
112 tty->flow_change = 0;
113 up_write(&tty->termios_rwsem);
115 EXPORT_SYMBOL(tty_unthrottle);
118 * tty_throttle_safe - flow control
121 * Indicate that a tty should stop transmitting data down the stack.
122 * tty_throttle_safe will only attempt throttle if tty->flow_change is
123 * TTY_THROTTLE_SAFE. Prevents an accidental throttle due to race
124 * conditions when throttling is conditional on factors evaluated prior to
127 * Returns 0 if tty is throttled (or was already throttled)
130 int tty_throttle_safe(struct tty_struct *tty)
134 mutex_lock(&tty->throttle_mutex);
135 if (!tty_throttled(tty)) {
136 if (tty->flow_change != TTY_THROTTLE_SAFE)
139 set_bit(TTY_THROTTLED, &tty->flags);
140 if (tty->ops->throttle)
141 tty->ops->throttle(tty);
144 mutex_unlock(&tty->throttle_mutex);
150 * tty_unthrottle_safe - flow control
153 * Similar to tty_unthrottle() but will only attempt unthrottle
154 * if tty->flow_change is TTY_UNTHROTTLE_SAFE. Prevents an accidental
155 * unthrottle due to race conditions when unthrottling is conditional
156 * on factors evaluated prior to unthrottling.
158 * Returns 0 if tty is unthrottled (or was already unthrottled)
161 int tty_unthrottle_safe(struct tty_struct *tty)
165 mutex_lock(&tty->throttle_mutex);
166 if (tty_throttled(tty)) {
167 if (tty->flow_change != TTY_UNTHROTTLE_SAFE)
170 clear_bit(TTY_THROTTLED, &tty->flags);
171 if (tty->ops->unthrottle)
172 tty->ops->unthrottle(tty);
175 mutex_unlock(&tty->throttle_mutex);
181 * tty_wait_until_sent - wait for I/O to finish
182 * @tty: tty we are waiting for
183 * @timeout: how long we will wait
185 * Wait for characters pending in a tty driver to hit the wire, or
186 * for a timeout to occur (eg due to flow control)
191 void tty_wait_until_sent(struct tty_struct *tty, long timeout)
194 timeout = MAX_SCHEDULE_TIMEOUT;
196 timeout = wait_event_interruptible_timeout(tty->write_wait,
197 !tty_chars_in_buffer(tty), timeout);
201 if (timeout == MAX_SCHEDULE_TIMEOUT)
204 if (tty->ops->wait_until_sent)
205 tty->ops->wait_until_sent(tty, timeout);
207 EXPORT_SYMBOL(tty_wait_until_sent);
211 * Termios Helper Methods
214 static void unset_locked_termios(struct tty_struct *tty, const struct ktermios *old)
216 struct ktermios *termios = &tty->termios;
217 struct ktermios *locked = &tty->termios_locked;
220 #define NOSET_MASK(x, y, z) (x = ((x) & ~(z)) | ((y) & (z)))
222 NOSET_MASK(termios->c_iflag, old->c_iflag, locked->c_iflag);
223 NOSET_MASK(termios->c_oflag, old->c_oflag, locked->c_oflag);
224 NOSET_MASK(termios->c_cflag, old->c_cflag, locked->c_cflag);
225 NOSET_MASK(termios->c_lflag, old->c_lflag, locked->c_lflag);
226 termios->c_line = locked->c_line ? old->c_line : termios->c_line;
227 for (i = 0; i < NCCS; i++)
228 termios->c_cc[i] = locked->c_cc[i] ?
229 old->c_cc[i] : termios->c_cc[i];
230 /* FIXME: What should we do for i/ospeed */
234 * tty_termios_copy_hw - copy hardware settings
238 * Propagate the hardware specific terminal setting bits from
239 * the old termios structure to the new one. This is used in cases
240 * where the hardware does not support reconfiguration or as a helper
241 * in some cases where only minimal reconfiguration is supported
244 void tty_termios_copy_hw(struct ktermios *new, const struct ktermios *old)
246 /* The bits a dumb device handles in software. Smart devices need
247 to always provide a set_termios method */
248 new->c_cflag &= HUPCL | CREAD | CLOCAL;
249 new->c_cflag |= old->c_cflag & ~(HUPCL | CREAD | CLOCAL);
250 new->c_ispeed = old->c_ispeed;
251 new->c_ospeed = old->c_ospeed;
253 EXPORT_SYMBOL(tty_termios_copy_hw);
256 * tty_termios_hw_change - check for setting change
258 * @b: termios to compare
260 * Check if any of the bits that affect a dumb device have changed
261 * between the two termios structures, or a speed change is needed.
264 bool tty_termios_hw_change(const struct ktermios *a, const struct ktermios *b)
266 if (a->c_ispeed != b->c_ispeed || a->c_ospeed != b->c_ospeed)
268 if ((a->c_cflag ^ b->c_cflag) & ~(HUPCL | CREAD | CLOCAL))
272 EXPORT_SYMBOL(tty_termios_hw_change);
275 * tty_get_char_size - get size of a character
276 * @cflag: termios cflag value
278 * Get the size (in bits) of a character depending on @cflag's %CSIZE
281 unsigned char tty_get_char_size(unsigned int cflag)
283 switch (cflag & CSIZE) {
295 EXPORT_SYMBOL_GPL(tty_get_char_size);
298 * tty_get_frame_size - get size of a frame
299 * @cflag: termios cflag value
301 * Get the size (in bits) of a frame depending on @cflag's %CSIZE, %CSTOPB,
302 * and %PARENB setting. The result is a sum of character size, start and
303 * stop bits -- one bit each -- second stop bit (if set), and parity bit
306 unsigned char tty_get_frame_size(unsigned int cflag)
308 unsigned char bits = 2 + tty_get_char_size(cflag);
319 EXPORT_SYMBOL_GPL(tty_get_frame_size);
322 * tty_set_termios - update termios values
323 * @tty: tty to update
324 * @new_termios: desired new value
326 * Perform updates to the termios values set on this terminal.
327 * A master pty's termios should never be set.
329 * Locking: termios_rwsem
332 int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios)
334 struct ktermios old_termios;
335 struct tty_ldisc *ld;
337 WARN_ON(tty->driver->type == TTY_DRIVER_TYPE_PTY &&
338 tty->driver->subtype == PTY_TYPE_MASTER);
340 * Perform the actual termios internal changes under lock.
344 /* FIXME: we need to decide on some locking/ordering semantics
345 for the set_termios notification eventually */
346 down_write(&tty->termios_rwsem);
347 old_termios = tty->termios;
348 tty->termios = *new_termios;
349 unset_locked_termios(tty, &old_termios);
350 /* Reset any ADDRB changes, ADDRB is changed through ->rs485_config() */
351 tty->termios.c_cflag ^= (tty->termios.c_cflag ^ old_termios.c_cflag) & ADDRB;
353 if (tty->ops->set_termios)
354 tty->ops->set_termios(tty, &old_termios);
356 tty_termios_copy_hw(&tty->termios, &old_termios);
358 ld = tty_ldisc_ref(tty);
360 if (ld->ops->set_termios)
361 ld->ops->set_termios(tty, &old_termios);
364 up_write(&tty->termios_rwsem);
367 EXPORT_SYMBOL_GPL(tty_set_termios);
371 * Translate a "termio" structure into a "termios". Ugh.
373 __weak int user_termio_to_kernel_termios(struct ktermios *termios,
374 struct termio __user *termio)
378 if (copy_from_user(&v, termio, sizeof(struct termio)))
381 termios->c_iflag = (0xffff0000 & termios->c_iflag) | v.c_iflag;
382 termios->c_oflag = (0xffff0000 & termios->c_oflag) | v.c_oflag;
383 termios->c_cflag = (0xffff0000 & termios->c_cflag) | v.c_cflag;
384 termios->c_lflag = (0xffff0000 & termios->c_lflag) | v.c_lflag;
385 termios->c_line = (0xffff0000 & termios->c_lflag) | v.c_line;
386 memcpy(termios->c_cc, v.c_cc, NCC);
391 * Translate a "termios" structure into a "termio". Ugh.
393 __weak int kernel_termios_to_user_termio(struct termio __user *termio,
394 struct ktermios *termios)
397 memset(&v, 0, sizeof(struct termio));
398 v.c_iflag = termios->c_iflag;
399 v.c_oflag = termios->c_oflag;
400 v.c_cflag = termios->c_cflag;
401 v.c_lflag = termios->c_lflag;
402 v.c_line = termios->c_line;
403 memcpy(v.c_cc, termios->c_cc, NCC);
404 return copy_to_user(termio, &v, sizeof(struct termio));
408 __weak int user_termios_to_kernel_termios(struct ktermios *k,
409 struct termios2 __user *u)
411 return copy_from_user(k, u, sizeof(struct termios2));
413 __weak int kernel_termios_to_user_termios(struct termios2 __user *u,
416 return copy_to_user(u, k, sizeof(struct termios2));
418 __weak int user_termios_to_kernel_termios_1(struct ktermios *k,
419 struct termios __user *u)
421 return copy_from_user(k, u, sizeof(struct termios));
423 __weak int kernel_termios_to_user_termios_1(struct termios __user *u,
426 return copy_to_user(u, k, sizeof(struct termios));
431 __weak int user_termios_to_kernel_termios(struct ktermios *k,
432 struct termios __user *u)
434 return copy_from_user(k, u, sizeof(struct termios));
436 __weak int kernel_termios_to_user_termios(struct termios __user *u,
439 return copy_to_user(u, k, sizeof(struct termios));
444 * set_termios - set termios values for a tty
445 * @tty: terminal device
447 * @opt: option information
449 * Helper function to prepare termios data and run necessary other
450 * functions before using tty_set_termios to do the actual changes.
453 * Called functions take ldisc and termios_rwsem locks
456 static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
458 struct ktermios tmp_termios;
459 struct tty_ldisc *ld;
460 int retval = tty_check_change(tty);
465 down_read(&tty->termios_rwsem);
466 tmp_termios = tty->termios;
467 up_read(&tty->termios_rwsem);
469 if (opt & TERMIOS_TERMIO) {
470 if (user_termio_to_kernel_termios(&tmp_termios,
471 (struct termio __user *)arg))
474 } else if (opt & TERMIOS_OLD) {
475 if (user_termios_to_kernel_termios_1(&tmp_termios,
476 (struct termios __user *)arg))
479 if (user_termios_to_kernel_termios(&tmp_termios,
480 (struct termios2 __user *)arg))
484 } else if (user_termios_to_kernel_termios(&tmp_termios,
485 (struct termios __user *)arg))
489 /* If old style Bfoo values are used then load c_ispeed/c_ospeed
490 * with the real speed so its unconditionally usable */
491 tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios);
492 tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios);
494 if (opt & (TERMIOS_FLUSH|TERMIOS_WAIT)) {
496 retval = wait_event_interruptible(tty->write_wait, !tty_chars_in_buffer(tty));
500 if (tty_write_lock(tty, false) < 0)
501 goto retry_write_wait;
504 if (tty_chars_in_buffer(tty)) {
505 tty_write_unlock(tty);
506 goto retry_write_wait;
509 ld = tty_ldisc_ref(tty);
511 if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
512 ld->ops->flush_buffer(tty);
516 if ((opt & TERMIOS_WAIT) && tty->ops->wait_until_sent) {
517 tty->ops->wait_until_sent(tty, 0);
518 if (signal_pending(current)) {
519 tty_write_unlock(tty);
524 tty_set_termios(tty, &tmp_termios);
526 tty_write_unlock(tty);
528 tty_set_termios(tty, &tmp_termios);
531 /* FIXME: Arguably if tmp_termios == tty->termios AND the
532 actual requested termios was not tmp_termios then we may
533 want to return an error as no user requested change has
538 static void copy_termios(struct tty_struct *tty, struct ktermios *kterm)
540 down_read(&tty->termios_rwsem);
541 *kterm = tty->termios;
542 up_read(&tty->termios_rwsem);
545 static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm)
547 down_read(&tty->termios_rwsem);
548 *kterm = tty->termios_locked;
549 up_read(&tty->termios_rwsem);
552 static int get_termio(struct tty_struct *tty, struct termio __user *termio)
554 struct ktermios kterm;
555 copy_termios(tty, &kterm);
556 if (kernel_termios_to_user_termio(termio, &kterm))
563 * These are deprecated, but there is limited support..
565 * The "sg_flags" translation is a joke..
567 static int get_sgflags(struct tty_struct *tty)
571 if (!L_ICANON(tty)) {
573 flags |= 0x02; /* cbreak */
575 flags |= 0x20; /* raw */
578 flags |= 0x08; /* echo */
581 flags |= 0x10; /* crmod */
585 static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
589 down_read(&tty->termios_rwsem);
590 tmp.sg_ispeed = tty->termios.c_ispeed;
591 tmp.sg_ospeed = tty->termios.c_ospeed;
592 tmp.sg_erase = tty->termios.c_cc[VERASE];
593 tmp.sg_kill = tty->termios.c_cc[VKILL];
594 tmp.sg_flags = get_sgflags(tty);
595 up_read(&tty->termios_rwsem);
597 return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
600 static void set_sgflags(struct ktermios *termios, int flags)
602 termios->c_iflag = ICRNL | IXON;
603 termios->c_oflag = 0;
604 termios->c_lflag = ISIG | ICANON;
605 if (flags & 0x02) { /* cbreak */
606 termios->c_iflag = 0;
607 termios->c_lflag &= ~ICANON;
609 if (flags & 0x08) { /* echo */
610 termios->c_lflag |= ECHO | ECHOE | ECHOK |
611 ECHOCTL | ECHOKE | IEXTEN;
613 if (flags & 0x10) { /* crmod */
614 termios->c_oflag |= OPOST | ONLCR;
616 if (flags & 0x20) { /* raw */
617 termios->c_iflag = 0;
618 termios->c_lflag &= ~(ISIG | ICANON);
620 if (!(termios->c_lflag & ICANON)) {
621 termios->c_cc[VMIN] = 1;
622 termios->c_cc[VTIME] = 0;
627 * set_sgttyb - set legacy terminal values
628 * @tty: tty structure
629 * @sgttyb: pointer to old style terminal structure
631 * Updates a terminal from the legacy BSD style terminal information
634 * Locking: termios_rwsem
637 static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
641 struct ktermios termios;
643 retval = tty_check_change(tty);
647 if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
650 down_write(&tty->termios_rwsem);
651 termios = tty->termios;
652 termios.c_cc[VERASE] = tmp.sg_erase;
653 termios.c_cc[VKILL] = tmp.sg_kill;
654 set_sgflags(&termios, tmp.sg_flags);
655 /* Try and encode into Bfoo format */
656 tty_termios_encode_baud_rate(&termios, termios.c_ispeed,
658 up_write(&tty->termios_rwsem);
659 tty_set_termios(tty, &termios);
665 static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars)
669 down_read(&tty->termios_rwsem);
670 tmp.t_intrc = tty->termios.c_cc[VINTR];
671 tmp.t_quitc = tty->termios.c_cc[VQUIT];
672 tmp.t_startc = tty->termios.c_cc[VSTART];
673 tmp.t_stopc = tty->termios.c_cc[VSTOP];
674 tmp.t_eofc = tty->termios.c_cc[VEOF];
675 tmp.t_brkc = tty->termios.c_cc[VEOL2]; /* what is brkc anyway? */
676 up_read(&tty->termios_rwsem);
677 return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
680 static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars)
684 if (copy_from_user(&tmp, tchars, sizeof(tmp)))
686 down_write(&tty->termios_rwsem);
687 tty->termios.c_cc[VINTR] = tmp.t_intrc;
688 tty->termios.c_cc[VQUIT] = tmp.t_quitc;
689 tty->termios.c_cc[VSTART] = tmp.t_startc;
690 tty->termios.c_cc[VSTOP] = tmp.t_stopc;
691 tty->termios.c_cc[VEOF] = tmp.t_eofc;
692 tty->termios.c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
693 up_write(&tty->termios_rwsem);
699 static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
703 down_read(&tty->termios_rwsem);
704 tmp.t_suspc = tty->termios.c_cc[VSUSP];
705 /* what is dsuspc anyway? */
706 tmp.t_dsuspc = tty->termios.c_cc[VSUSP];
707 tmp.t_rprntc = tty->termios.c_cc[VREPRINT];
708 /* what is flushc anyway? */
709 tmp.t_flushc = tty->termios.c_cc[VEOL2];
710 tmp.t_werasc = tty->termios.c_cc[VWERASE];
711 tmp.t_lnextc = tty->termios.c_cc[VLNEXT];
712 up_read(&tty->termios_rwsem);
713 return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
716 static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
720 if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
723 down_write(&tty->termios_rwsem);
724 tty->termios.c_cc[VSUSP] = tmp.t_suspc;
725 /* what is dsuspc anyway? */
726 tty->termios.c_cc[VEOL2] = tmp.t_dsuspc;
727 tty->termios.c_cc[VREPRINT] = tmp.t_rprntc;
728 /* what is flushc anyway? */
729 tty->termios.c_cc[VEOL2] = tmp.t_flushc;
730 tty->termios.c_cc[VWERASE] = tmp.t_werasc;
731 tty->termios.c_cc[VLNEXT] = tmp.t_lnextc;
732 up_write(&tty->termios_rwsem);
738 * tty_change_softcar - carrier change ioctl helper
739 * @tty: tty to update
740 * @enable: enable/disable CLOCAL
742 * Perform a change to the CLOCAL state and call into the driver
743 * layer to make it visible. All done with the termios rwsem
746 static int tty_change_softcar(struct tty_struct *tty, bool enable)
750 tcflag_t bit = enable ? CLOCAL : 0;
752 down_write(&tty->termios_rwsem);
754 tty->termios.c_cflag &= ~CLOCAL;
755 tty->termios.c_cflag |= bit;
756 if (tty->ops->set_termios)
757 tty->ops->set_termios(tty, &old);
758 if (C_CLOCAL(tty) != bit)
760 up_write(&tty->termios_rwsem);
765 * tty_mode_ioctl - mode related ioctls
766 * @tty: tty for the ioctl
768 * @arg: ioctl argument
770 * Perform non line discipline specific mode control ioctls. This
771 * is designed to be called by line disciplines to ensure they provide
772 * consistent mode setting.
775 int tty_mode_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
777 struct tty_struct *real_tty;
778 void __user *p = (void __user *)arg;
780 struct ktermios kterm;
782 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
783 tty->driver->subtype == PTY_TYPE_MASTER)
784 real_tty = tty->link;
791 return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
794 return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
798 return get_tchars(real_tty, p);
800 return set_tchars(real_tty, p);
804 return get_ltchars(real_tty, p);
806 return set_ltchars(real_tty, p);
809 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
811 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
813 return set_termios(real_tty, p, TERMIOS_OLD);
816 copy_termios(real_tty, &kterm);
817 if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
822 copy_termios(real_tty, &kterm);
823 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
827 copy_termios(real_tty, &kterm);
828 if (kernel_termios_to_user_termios((struct termios2 __user *)arg, &kterm))
832 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
834 return set_termios(real_tty, p, TERMIOS_WAIT);
836 return set_termios(real_tty, p, 0);
839 return get_termio(real_tty, p);
841 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
843 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
845 return set_termios(real_tty, p, TERMIOS_TERMIO);
848 copy_termios_locked(real_tty, &kterm);
849 if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
853 if (!capable(CAP_SYS_ADMIN))
855 copy_termios_locked(real_tty, &kterm);
856 if (user_termios_to_kernel_termios(&kterm,
857 (struct termios __user *) arg))
859 down_write(&real_tty->termios_rwsem);
860 real_tty->termios_locked = kterm;
861 up_write(&real_tty->termios_rwsem);
865 copy_termios_locked(real_tty, &kterm);
866 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
870 if (!capable(CAP_SYS_ADMIN))
872 copy_termios_locked(real_tty, &kterm);
873 if (user_termios_to_kernel_termios_1(&kterm,
874 (struct termios __user *) arg))
876 down_write(&real_tty->termios_rwsem);
877 real_tty->termios_locked = kterm;
878 up_write(&real_tty->termios_rwsem);
889 copy_termios(real_tty, &kterm);
890 ret = put_user((kterm.c_cflag & CLOCAL) ? 1 : 0,
894 if (get_user(arg, (unsigned int __user *) arg))
896 return tty_change_softcar(real_tty, arg);
901 EXPORT_SYMBOL_GPL(tty_mode_ioctl);
904 /* Caller guarantees ldisc reference is held */
905 static int __tty_perform_flush(struct tty_struct *tty, unsigned long arg)
907 struct tty_ldisc *ld = tty->ldisc;
911 if (ld && ld->ops->flush_buffer) {
912 ld->ops->flush_buffer(tty);
917 if (ld && ld->ops->flush_buffer) {
918 ld->ops->flush_buffer(tty);
923 tty_driver_flush_buffer(tty);
931 int tty_perform_flush(struct tty_struct *tty, unsigned long arg)
933 struct tty_ldisc *ld;
934 int retval = tty_check_change(tty);
938 ld = tty_ldisc_ref_wait(tty);
939 retval = __tty_perform_flush(tty, arg);
944 EXPORT_SYMBOL_GPL(tty_perform_flush);
946 int n_tty_ioctl_helper(struct tty_struct *tty, unsigned int cmd,
953 retval = tty_check_change(tty);
958 spin_lock_irq(&tty->flow.lock);
959 if (!tty->flow.tco_stopped) {
960 tty->flow.tco_stopped = true;
963 spin_unlock_irq(&tty->flow.lock);
966 spin_lock_irq(&tty->flow.lock);
967 if (tty->flow.tco_stopped) {
968 tty->flow.tco_stopped = false;
971 spin_unlock_irq(&tty->flow.lock);
974 if (STOP_CHAR(tty) != __DISABLED_CHAR)
975 retval = tty_send_xchar(tty, STOP_CHAR(tty));
978 if (START_CHAR(tty) != __DISABLED_CHAR)
979 retval = tty_send_xchar(tty, START_CHAR(tty));
986 retval = tty_check_change(tty);
989 return __tty_perform_flush(tty, arg);
991 /* Try the mode commands */
992 return tty_mode_ioctl(tty, cmd, arg);
995 EXPORT_SYMBOL(n_tty_ioctl_helper);