2 * linux/drivers/char/tty_ioctl.c
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
6 * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
7 * which can be dynamically activated and de-activated by the line
8 * discipline handling modules (like SLIP).
11 #include <linux/types.h>
12 #include <linux/termios.h>
13 #include <linux/errno.h>
14 #include <linux/sched.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/smp_lock.h>
27 #include <asm/uaccess.h>
28 #include <asm/system.h>
30 #undef TTY_DEBUG_WAIT_UNTIL_SENT
35 * Internal flag options for termios setting behavior
37 #define TERMIOS_FLUSH 1
38 #define TERMIOS_WAIT 2
39 #define TERMIOS_TERMIO 4
44 * tty_chars_in_buffer - characters pending
47 * Return the number of bytes of data in the device private
48 * output queue. If no private method is supplied there is assumed
49 * to be no queue on the device.
52 int tty_chars_in_buffer(struct tty_struct *tty)
54 if (tty->ops->chars_in_buffer)
55 return tty->ops->chars_in_buffer(tty);
59 EXPORT_SYMBOL(tty_chars_in_buffer);
62 * tty_write_room - write queue space
65 * Return the number of bytes that can be queued to this device
66 * at the present time. The result should be treated as a guarantee
67 * and the driver cannot offer a value it later shrinks by more than
68 * the number of bytes written. If no method is provided 2K is always
69 * returned and data may be lost as there will be no flow control.
72 int tty_write_room(struct tty_struct *tty)
74 if (tty->ops->write_room)
75 return tty->ops->write_room(tty);
78 EXPORT_SYMBOL(tty_write_room);
81 * tty_driver_flush_buffer - discard internal buffer
84 * Discard the internal output buffer for this device. If no method
85 * is provided then either the buffer cannot be hardware flushed or
86 * there is no buffer driver side.
88 void tty_driver_flush_buffer(struct tty_struct *tty)
90 if (tty->ops->flush_buffer)
91 tty->ops->flush_buffer(tty);
93 EXPORT_SYMBOL(tty_driver_flush_buffer);
96 * tty_throttle - flow control
99 * Indicate that a tty should stop transmitting data down the stack.
100 * Takes the termios mutex to protect against parallel throttle/unthrottle
101 * and also to ensure the driver can consistently reference its own
102 * termios data at this point when implementing software flow control.
105 void tty_throttle(struct tty_struct *tty)
107 mutex_lock(&tty->termios_mutex);
108 /* check TTY_THROTTLED first so it indicates our state */
109 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
111 tty->ops->throttle(tty);
112 mutex_unlock(&tty->termios_mutex);
114 EXPORT_SYMBOL(tty_throttle);
117 * tty_unthrottle - flow control
120 * Indicate that a tty may continue transmitting data down the stack.
121 * Takes the termios mutex to protect against parallel throttle/unthrottle
122 * and also to ensure the driver can consistently reference its own
123 * termios data at this point when implementing software flow control.
125 * Drivers should however remember that the stack can issue a throttle,
126 * then change flow control method, then unthrottle.
129 void tty_unthrottle(struct tty_struct *tty)
131 mutex_lock(&tty->termios_mutex);
132 if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
133 tty->ops->unthrottle)
134 tty->ops->unthrottle(tty);
135 mutex_unlock(&tty->termios_mutex);
137 EXPORT_SYMBOL(tty_unthrottle);
140 * tty_wait_until_sent - wait for I/O to finish
141 * @tty: tty we are waiting for
142 * @timeout: how long we will wait
144 * Wait for characters pending in a tty driver to hit the wire, or
145 * for a timeout to occur (eg due to flow control)
150 void tty_wait_until_sent(struct tty_struct *tty, long timeout)
152 #ifdef TTY_DEBUG_WAIT_UNTIL_SENT
155 printk(KERN_DEBUG "%s wait until sent...\n", tty_name(tty, buf));
158 timeout = MAX_SCHEDULE_TIMEOUT;
159 if (wait_event_interruptible_timeout(tty->write_wait,
160 !tty_chars_in_buffer(tty), timeout) >= 0) {
161 if (tty->ops->wait_until_sent)
162 tty->ops->wait_until_sent(tty, timeout);
165 EXPORT_SYMBOL(tty_wait_until_sent);
169 * Termios Helper Methods
172 static void unset_locked_termios(struct ktermios *termios,
173 struct ktermios *old,
174 struct ktermios *locked)
178 #define NOSET_MASK(x, y, z) (x = ((x) & ~(z)) | ((y) & (z)))
181 printk(KERN_WARNING "Warning?!? termios_locked is NULL.\n");
185 NOSET_MASK(termios->c_iflag, old->c_iflag, locked->c_iflag);
186 NOSET_MASK(termios->c_oflag, old->c_oflag, locked->c_oflag);
187 NOSET_MASK(termios->c_cflag, old->c_cflag, locked->c_cflag);
188 NOSET_MASK(termios->c_lflag, old->c_lflag, locked->c_lflag);
189 termios->c_line = locked->c_line ? old->c_line : termios->c_line;
190 for (i = 0; i < NCCS; i++)
191 termios->c_cc[i] = locked->c_cc[i] ?
192 old->c_cc[i] : termios->c_cc[i];
193 /* FIXME: What should we do for i/ospeed */
197 * Routine which returns the baud rate of the tty
199 * Note that the baud_table needs to be kept in sync with the
200 * include/asm/termbits.h file.
202 static const speed_t baud_table[] = {
203 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
204 9600, 19200, 38400, 57600, 115200, 230400, 460800,
206 76800, 153600, 307200, 614400, 921600
208 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
209 2500000, 3000000, 3500000, 4000000
214 static const tcflag_t baud_bits[] = {
215 B0, B50, B75, B110, B134, B150, B200, B300, B600,
216 B1200, B1800, B2400, B4800, B9600, B19200, B38400,
217 B57600, B115200, B230400, B460800, B500000, B576000,
218 B921600, B1000000, B1152000, B1500000, B2000000, B2500000,
219 B3000000, B3500000, B4000000
222 static const tcflag_t baud_bits[] = {
223 B0, B50, B75, B110, B134, B150, B200, B300, B600,
224 B1200, B1800, B2400, B4800, B9600, B19200, B38400,
225 B57600, B115200, B230400, B460800, B76800, B153600,
226 B307200, B614400, B921600
230 static int n_baud_table = ARRAY_SIZE(baud_table);
233 * tty_termios_baud_rate
234 * @termios: termios structure
236 * Convert termios baud rate data into a speed. This should be called
237 * with the termios lock held if this termios is a terminal termios
238 * structure. May change the termios data. Device drivers can call this
239 * function but should use ->c_[io]speed directly as they are updated.
244 speed_t tty_termios_baud_rate(struct ktermios *termios)
248 cbaud = termios->c_cflag & CBAUD;
251 /* Magic token for arbitary speed via c_ispeed/c_ospeed */
253 return termios->c_ospeed;
255 if (cbaud & CBAUDEX) {
258 if (cbaud < 1 || cbaud + 15 > n_baud_table)
259 termios->c_cflag &= ~CBAUDEX;
263 return baud_table[cbaud];
265 EXPORT_SYMBOL(tty_termios_baud_rate);
268 * tty_termios_input_baud_rate
269 * @termios: termios structure
271 * Convert termios baud rate data into a speed. This should be called
272 * with the termios lock held if this termios is a terminal termios
273 * structure. May change the termios data. Device drivers can call this
274 * function but should use ->c_[io]speed directly as they are updated.
279 speed_t tty_termios_input_baud_rate(struct ktermios *termios)
282 unsigned int cbaud = (termios->c_cflag >> IBSHIFT) & CBAUD;
285 return tty_termios_baud_rate(termios);
287 /* Magic token for arbitary speed via c_ispeed*/
289 return termios->c_ispeed;
291 if (cbaud & CBAUDEX) {
294 if (cbaud < 1 || cbaud + 15 > n_baud_table)
295 termios->c_cflag &= ~(CBAUDEX << IBSHIFT);
299 return baud_table[cbaud];
301 return tty_termios_baud_rate(termios);
304 EXPORT_SYMBOL(tty_termios_input_baud_rate);
307 * tty_termios_encode_baud_rate
308 * @termios: ktermios structure holding user requested state
309 * @ispeed: input speed
310 * @ospeed: output speed
312 * Encode the speeds set into the passed termios structure. This is
313 * used as a library helper for drivers os that they can report back
314 * the actual speed selected when it differs from the speed requested
316 * For maximal back compatibility with legacy SYS5/POSIX *nix behaviour
317 * we need to carefully set the bits when the user does not get the
318 * desired speed. We allow small margins and preserve as much of possible
319 * of the input intent to keep compatiblity.
321 * Locking: Caller should hold termios lock. This is already held
322 * when calling this function from the driver termios handler.
324 * The ifdefs deal with platforms whose owners have yet to update them
325 * and will all go away once this is done.
328 void tty_termios_encode_baud_rate(struct ktermios *termios,
329 speed_t ibaud, speed_t obaud)
332 int ifound = -1, ofound = -1;
333 int iclose = ibaud/50, oclose = obaud/50;
336 if (obaud == 0) /* CD dropped */
337 ibaud = 0; /* Clear ibaud to be sure */
339 termios->c_ispeed = ibaud;
340 termios->c_ospeed = obaud;
343 /* If the user asked for a precise weird speed give a precise weird
344 answer. If they asked for a Bfoo speed they many have problems
345 digesting non-exact replies so fuzz a bit */
347 if ((termios->c_cflag & CBAUD) == BOTHER)
349 if (((termios->c_cflag >> IBSHIFT) & CBAUD) == BOTHER)
351 if ((termios->c_cflag >> IBSHIFT) & CBAUD)
352 ibinput = 1; /* An input speed was specified */
354 termios->c_cflag &= ~CBAUD;
357 * Our goal is to find a close match to the standard baud rate
358 * returned. Walk the baud rate table and if we get a very close
359 * match then report back the speed as a POSIX Bxxxx value by
364 if (obaud - oclose <= baud_table[i] &&
365 obaud + oclose >= baud_table[i]) {
366 termios->c_cflag |= baud_bits[i];
369 if (ibaud - iclose <= baud_table[i] &&
370 ibaud + iclose >= baud_table[i]) {
371 /* For the case input == output don't set IBAUD bits
372 if the user didn't do so */
373 if (ofound == i && !ibinput)
378 termios->c_cflag |= (baud_bits[i] << IBSHIFT);
382 } while (++i < n_baud_table);
385 * If we found no match then use BOTHER if provided or warn
386 * the user their platform maintainer needs to wake up if not.
390 termios->c_cflag |= BOTHER;
391 /* Set exact input bits only if the input and output differ or the
393 if (ifound == -1 && (ibaud != obaud || ibinput))
394 termios->c_cflag |= (BOTHER << IBSHIFT);
396 if (ifound == -1 || ofound == -1) {
399 printk(KERN_WARNING "tty: Unable to return correct "
400 "speed data as your architecture needs updating.\n");
404 EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate);
407 * tty_encode_baud_rate - set baud rate of the tty
408 * @ibaud: input baud rate
409 * @obad: output baud rate
411 * Update the current termios data for the tty with the new speed
412 * settings. The caller must hold the termios_mutex for the tty in
416 void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, speed_t obaud)
418 tty_termios_encode_baud_rate(tty->termios, ibaud, obaud);
420 EXPORT_SYMBOL_GPL(tty_encode_baud_rate);
423 * tty_get_baud_rate - get tty bit rates
426 * Returns the baud rate as an integer for this terminal. The
427 * termios lock must be held by the caller and the terminal bit
428 * flags may be updated.
433 speed_t tty_get_baud_rate(struct tty_struct *tty)
435 speed_t baud = tty_termios_baud_rate(tty->termios);
437 if (baud == 38400 && tty->alt_speed) {
439 printk(KERN_WARNING "Use of setserial/setrocket to "
440 "set SPD_* flags is deprecated\n");
443 baud = tty->alt_speed;
448 EXPORT_SYMBOL(tty_get_baud_rate);
451 * tty_termios_copy_hw - copy hardware settings
455 * Propogate the hardware specific terminal setting bits from
456 * the old termios structure to the new one. This is used in cases
457 * where the hardware does not support reconfiguration or as a helper
458 * in some cases where only minimal reconfiguration is supported
461 void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old)
463 /* The bits a dumb device handles in software. Smart devices need
464 to always provide a set_termios method */
465 new->c_cflag &= HUPCL | CREAD | CLOCAL;
466 new->c_cflag |= old->c_cflag & ~(HUPCL | CREAD | CLOCAL);
467 new->c_ispeed = old->c_ispeed;
468 new->c_ospeed = old->c_ospeed;
470 EXPORT_SYMBOL(tty_termios_copy_hw);
473 * tty_termios_hw_change - check for setting change
475 * @b: termios to compare
477 * Check if any of the bits that affect a dumb device have changed
478 * between the two termios structures, or a speed change is needed.
481 int tty_termios_hw_change(struct ktermios *a, struct ktermios *b)
483 if (a->c_ispeed != b->c_ispeed || a->c_ospeed != b->c_ospeed)
485 if ((a->c_cflag ^ b->c_cflag) & ~(HUPCL | CREAD | CLOCAL))
489 EXPORT_SYMBOL(tty_termios_hw_change);
492 * change_termios - update termios values
493 * @tty: tty to update
494 * @new_termios: desired new value
496 * Perform updates to the termios values set on this terminal. There
497 * is a bit of layering violation here with n_tty in terms of the
498 * internal knowledge of this function.
500 * Locking: termios_mutex
503 static void change_termios(struct tty_struct *tty, struct ktermios *new_termios)
505 struct ktermios old_termios;
506 struct tty_ldisc *ld;
510 * Perform the actual termios internal changes under lock.
514 /* FIXME: we need to decide on some locking/ordering semantics
515 for the set_termios notification eventually */
516 mutex_lock(&tty->termios_mutex);
517 old_termios = *tty->termios;
518 *tty->termios = *new_termios;
519 unset_locked_termios(tty->termios, &old_termios, tty->termios_locked);
521 /* See if packet mode change of state. */
522 if (tty->link && tty->link->packet) {
523 int old_flow = ((old_termios.c_iflag & IXON) &&
524 (old_termios.c_cc[VSTOP] == '\023') &&
525 (old_termios.c_cc[VSTART] == '\021'));
526 int new_flow = (I_IXON(tty) &&
527 STOP_CHAR(tty) == '\023' &&
528 START_CHAR(tty) == '\021');
529 if (old_flow != new_flow) {
530 spin_lock_irqsave(&tty->ctrl_lock, flags);
531 tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
533 tty->ctrl_status |= TIOCPKT_DOSTOP;
535 tty->ctrl_status |= TIOCPKT_NOSTOP;
536 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
537 wake_up_interruptible(&tty->link->read_wait);
541 if (tty->ops->set_termios)
542 (*tty->ops->set_termios)(tty, &old_termios);
544 tty_termios_copy_hw(tty->termios, &old_termios);
546 ld = tty_ldisc_ref(tty);
548 if (ld->ops->set_termios)
549 (ld->ops->set_termios)(tty, &old_termios);
552 mutex_unlock(&tty->termios_mutex);
556 * set_termios - set termios values for a tty
557 * @tty: terminal device
559 * @opt: option information
561 * Helper function to prepare termios data and run necessary other
562 * functions before using change_termios to do the actual changes.
565 * Called functions take ldisc and termios_mutex locks
568 static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
570 struct ktermios tmp_termios;
571 struct tty_ldisc *ld;
572 int retval = tty_check_change(tty);
577 mutex_lock(&tty->termios_mutex);
578 memcpy(&tmp_termios, tty->termios, sizeof(struct ktermios));
579 mutex_unlock(&tty->termios_mutex);
581 if (opt & TERMIOS_TERMIO) {
582 if (user_termio_to_kernel_termios(&tmp_termios,
583 (struct termio __user *)arg))
586 } else if (opt & TERMIOS_OLD) {
587 if (user_termios_to_kernel_termios_1(&tmp_termios,
588 (struct termios __user *)arg))
591 if (user_termios_to_kernel_termios(&tmp_termios,
592 (struct termios2 __user *)arg))
596 } else if (user_termios_to_kernel_termios(&tmp_termios,
597 (struct termios __user *)arg))
601 /* If old style Bfoo values are used then load c_ispeed/c_ospeed
602 * with the real speed so its unconditionally usable */
603 tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios);
604 tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios);
606 ld = tty_ldisc_ref(tty);
609 if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
610 ld->ops->flush_buffer(tty);
614 if (opt & TERMIOS_WAIT) {
615 tty_wait_until_sent(tty, 0);
616 if (signal_pending(current))
620 change_termios(tty, &tmp_termios);
622 /* FIXME: Arguably if tmp_termios == tty->termios AND the
623 actual requested termios was not tmp_termios then we may
624 want to return an error as no user requested change has
629 static int get_termio(struct tty_struct *tty, struct termio __user *termio)
631 if (kernel_termios_to_user_termio(termio, tty->termios))
640 * set_termiox - set termiox fields if possible
642 * @arg: termiox structure from user
643 * @opt: option flags for ioctl type
645 * Implement the device calling points for the SYS5 termiox ioctl
649 static int set_termiox(struct tty_struct *tty, void __user *arg, int opt)
652 struct tty_ldisc *ld;
654 if (tty->termiox == NULL)
656 if (copy_from_user(&tnew, arg, sizeof(struct termiox)))
659 ld = tty_ldisc_ref(tty);
661 if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
662 ld->ops->flush_buffer(tty);
665 if (opt & TERMIOS_WAIT) {
666 tty_wait_until_sent(tty, 0);
667 if (signal_pending(current))
671 mutex_lock(&tty->termios_mutex);
672 if (tty->ops->set_termiox)
673 tty->ops->set_termiox(tty, &tnew);
674 mutex_unlock(&tty->termios_mutex);
683 * These are deprecated, but there is limited support..
685 * The "sg_flags" translation is a joke..
687 static int get_sgflags(struct tty_struct *tty)
691 if (!(tty->termios->c_lflag & ICANON)) {
692 if (tty->termios->c_lflag & ISIG)
693 flags |= 0x02; /* cbreak */
695 flags |= 0x20; /* raw */
697 if (tty->termios->c_lflag & ECHO)
698 flags |= 0x08; /* echo */
699 if (tty->termios->c_oflag & OPOST)
700 if (tty->termios->c_oflag & ONLCR)
701 flags |= 0x10; /* crmod */
705 static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
709 mutex_lock(&tty->termios_mutex);
710 tmp.sg_ispeed = tty->termios->c_ispeed;
711 tmp.sg_ospeed = tty->termios->c_ospeed;
712 tmp.sg_erase = tty->termios->c_cc[VERASE];
713 tmp.sg_kill = tty->termios->c_cc[VKILL];
714 tmp.sg_flags = get_sgflags(tty);
715 mutex_unlock(&tty->termios_mutex);
717 return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
720 static void set_sgflags(struct ktermios *termios, int flags)
722 termios->c_iflag = ICRNL | IXON;
723 termios->c_oflag = 0;
724 termios->c_lflag = ISIG | ICANON;
725 if (flags & 0x02) { /* cbreak */
726 termios->c_iflag = 0;
727 termios->c_lflag &= ~ICANON;
729 if (flags & 0x08) { /* echo */
730 termios->c_lflag |= ECHO | ECHOE | ECHOK |
731 ECHOCTL | ECHOKE | IEXTEN;
733 if (flags & 0x10) { /* crmod */
734 termios->c_oflag |= OPOST | ONLCR;
736 if (flags & 0x20) { /* raw */
737 termios->c_iflag = 0;
738 termios->c_lflag &= ~(ISIG | ICANON);
740 if (!(termios->c_lflag & ICANON)) {
741 termios->c_cc[VMIN] = 1;
742 termios->c_cc[VTIME] = 0;
747 * set_sgttyb - set legacy terminal values
748 * @tty: tty structure
749 * @sgttyb: pointer to old style terminal structure
751 * Updates a terminal from the legacy BSD style terminal information
754 * Locking: termios_mutex
757 static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
761 struct ktermios termios;
763 retval = tty_check_change(tty);
767 if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
770 mutex_lock(&tty->termios_mutex);
771 termios = *tty->termios;
772 termios.c_cc[VERASE] = tmp.sg_erase;
773 termios.c_cc[VKILL] = tmp.sg_kill;
774 set_sgflags(&termios, tmp.sg_flags);
775 /* Try and encode into Bfoo format */
777 tty_termios_encode_baud_rate(&termios, termios.c_ispeed,
780 mutex_unlock(&tty->termios_mutex);
781 change_termios(tty, &termios);
787 static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars)
791 mutex_lock(&tty->termios_mutex);
792 tmp.t_intrc = tty->termios->c_cc[VINTR];
793 tmp.t_quitc = tty->termios->c_cc[VQUIT];
794 tmp.t_startc = tty->termios->c_cc[VSTART];
795 tmp.t_stopc = tty->termios->c_cc[VSTOP];
796 tmp.t_eofc = tty->termios->c_cc[VEOF];
797 tmp.t_brkc = tty->termios->c_cc[VEOL2]; /* what is brkc anyway? */
798 mutex_unlock(&tty->termios_mutex);
799 return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
802 static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars)
806 if (copy_from_user(&tmp, tchars, sizeof(tmp)))
808 mutex_lock(&tty->termios_mutex);
809 tty->termios->c_cc[VINTR] = tmp.t_intrc;
810 tty->termios->c_cc[VQUIT] = tmp.t_quitc;
811 tty->termios->c_cc[VSTART] = tmp.t_startc;
812 tty->termios->c_cc[VSTOP] = tmp.t_stopc;
813 tty->termios->c_cc[VEOF] = tmp.t_eofc;
814 tty->termios->c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
815 mutex_unlock(&tty->termios_mutex);
821 static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
825 mutex_lock(&tty->termios_mutex);
826 tmp.t_suspc = tty->termios->c_cc[VSUSP];
827 /* what is dsuspc anyway? */
828 tmp.t_dsuspc = tty->termios->c_cc[VSUSP];
829 tmp.t_rprntc = tty->termios->c_cc[VREPRINT];
830 /* what is flushc anyway? */
831 tmp.t_flushc = tty->termios->c_cc[VEOL2];
832 tmp.t_werasc = tty->termios->c_cc[VWERASE];
833 tmp.t_lnextc = tty->termios->c_cc[VLNEXT];
834 mutex_unlock(&tty->termios_mutex);
835 return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
838 static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
842 if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
845 mutex_lock(&tty->termios_mutex);
846 tty->termios->c_cc[VSUSP] = tmp.t_suspc;
847 /* what is dsuspc anyway? */
848 tty->termios->c_cc[VEOL2] = tmp.t_dsuspc;
849 tty->termios->c_cc[VREPRINT] = tmp.t_rprntc;
850 /* what is flushc anyway? */
851 tty->termios->c_cc[VEOL2] = tmp.t_flushc;
852 tty->termios->c_cc[VWERASE] = tmp.t_werasc;
853 tty->termios->c_cc[VLNEXT] = tmp.t_lnextc;
854 mutex_unlock(&tty->termios_mutex);
860 * send_prio_char - send priority character
862 * Send a high priority character to the tty even if stopped
864 * Locking: none for xchar method, write ordering for write method.
867 static int send_prio_char(struct tty_struct *tty, char ch)
869 int was_stopped = tty->stopped;
871 if (tty->ops->send_xchar) {
872 tty->ops->send_xchar(tty, ch);
876 if (tty_write_lock(tty, 0) < 0)
881 tty->ops->write(tty, &ch, 1);
884 tty_write_unlock(tty);
889 * tty_change_softcar - carrier change ioctl helper
890 * @tty: tty to update
891 * @arg: enable/disable CLOCAL
893 * Perform a change to the CLOCAL state and call into the driver
894 * layer to make it visible. All done with the termios mutex
897 static int tty_change_softcar(struct tty_struct *tty, int arg)
900 int bit = arg ? CLOCAL : 0;
903 mutex_lock(&tty->termios_mutex);
905 tty->termios->c_cflag &= ~CLOCAL;
906 tty->termios->c_cflag |= bit;
907 if (tty->ops->set_termios)
908 tty->ops->set_termios(tty, &old);
909 if ((tty->termios->c_cflag & CLOCAL) != bit)
911 mutex_unlock(&tty->termios_mutex);
916 * tty_mode_ioctl - mode related ioctls
917 * @tty: tty for the ioctl
918 * @file: file pointer for the tty
920 * @arg: ioctl argument
922 * Perform non line discipline specific mode control ioctls. This
923 * is designed to be called by line disciplines to ensure they provide
924 * consistent mode setting.
927 int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
928 unsigned int cmd, unsigned long arg)
930 struct tty_struct *real_tty;
931 void __user *p = (void __user *)arg;
934 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
935 tty->driver->subtype == PTY_TYPE_MASTER)
936 real_tty = tty->link;
943 return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
946 return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
950 return get_tchars(real_tty, p);
952 return set_tchars(real_tty, p);
956 return get_ltchars(real_tty, p);
958 return set_ltchars(real_tty, p);
961 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
963 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
965 return set_termios(real_tty, p, TERMIOS_OLD);
968 mutex_lock(&real_tty->termios_mutex);
969 if (kernel_termios_to_user_termios((struct termios __user *)arg, real_tty->termios))
971 mutex_unlock(&real_tty->termios_mutex);
975 mutex_lock(&real_tty->termios_mutex);
976 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, real_tty->termios))
978 mutex_unlock(&real_tty->termios_mutex);
981 mutex_lock(&real_tty->termios_mutex);
982 if (kernel_termios_to_user_termios((struct termios2 __user *)arg, real_tty->termios))
984 mutex_unlock(&real_tty->termios_mutex);
987 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
989 return set_termios(real_tty, p, TERMIOS_WAIT);
991 return set_termios(real_tty, p, 0);
994 return get_termio(real_tty, p);
996 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
998 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
1000 return set_termios(real_tty, p, TERMIOS_TERMIO);
1002 case TIOCGLCKTRMIOS:
1003 mutex_lock(&real_tty->termios_mutex);
1004 if (kernel_termios_to_user_termios((struct termios __user *)arg, real_tty->termios_locked))
1006 mutex_unlock(&real_tty->termios_mutex);
1008 case TIOCSLCKTRMIOS:
1009 if (!capable(CAP_SYS_ADMIN))
1011 mutex_lock(&real_tty->termios_mutex);
1012 if (user_termios_to_kernel_termios(real_tty->termios_locked,
1013 (struct termios __user *) arg))
1015 mutex_unlock(&real_tty->termios_mutex);
1018 case TIOCGLCKTRMIOS:
1019 mutex_lock(&real_tty->termios_mutex);
1020 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, real_tty->termios_locked))
1022 mutex_unlock(&real_tty->termios_mutex);
1024 case TIOCSLCKTRMIOS:
1025 if (!capable(CAP_SYS_ADMIN))
1027 mutex_lock(&real_tty->termios_mutex);
1028 if (user_termios_to_kernel_termios_1(real_tty->termios_locked,
1029 (struct termios __user *) arg))
1031 mutex_unlock(&real_tty->termios_mutex);
1036 if (real_tty->termiox == NULL)
1038 mutex_lock(&real_tty->termios_mutex);
1039 if (copy_to_user(p, real_tty->termiox, sizeof(struct termiox)))
1041 mutex_unlock(&real_tty->termios_mutex);
1044 return set_termiox(real_tty, p, 0);
1046 return set_termiox(real_tty, p, TERMIOS_WAIT);
1048 return set_termiox(real_tty, p, TERMIOS_FLUSH);
1051 mutex_lock(&real_tty->termios_mutex);
1052 ret = put_user(C_CLOCAL(real_tty) ? 1 : 0,
1054 mutex_unlock(&real_tty->termios_mutex);
1057 if (get_user(arg, (unsigned int __user *) arg))
1059 return tty_change_softcar(real_tty, arg);
1061 return -ENOIOCTLCMD;
1064 EXPORT_SYMBOL_GPL(tty_mode_ioctl);
1066 int tty_perform_flush(struct tty_struct *tty, unsigned long arg)
1068 struct tty_ldisc *ld;
1069 int retval = tty_check_change(tty);
1073 ld = tty_ldisc_ref_wait(tty);
1076 if (ld && ld->ops->flush_buffer)
1077 ld->ops->flush_buffer(tty);
1080 if (ld && ld->ops->flush_buffer)
1081 ld->ops->flush_buffer(tty);
1084 tty_driver_flush_buffer(tty);
1087 tty_ldisc_deref(ld);
1090 tty_ldisc_deref(ld);
1093 EXPORT_SYMBOL_GPL(tty_perform_flush);
1095 int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file,
1096 unsigned int cmd, unsigned long arg)
1098 unsigned long flags;
1103 retval = tty_check_change(tty);
1108 if (!tty->flow_stopped) {
1109 tty->flow_stopped = 1;
1114 if (tty->flow_stopped) {
1115 tty->flow_stopped = 0;
1120 if (STOP_CHAR(tty) != __DISABLED_CHAR)
1121 return send_prio_char(tty, STOP_CHAR(tty));
1124 if (START_CHAR(tty) != __DISABLED_CHAR)
1125 return send_prio_char(tty, START_CHAR(tty));
1132 return tty_perform_flush(tty, arg);
1137 if (tty->driver->type != TTY_DRIVER_TYPE_PTY ||
1138 tty->driver->subtype != PTY_TYPE_MASTER)
1140 if (get_user(pktmode, (int __user *) arg))
1142 spin_lock_irqsave(&tty->ctrl_lock, flags);
1146 tty->link->ctrl_status = 0;
1150 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1154 /* Try the mode commands */
1155 return tty_mode_ioctl(tty, file, cmd, arg);
1158 EXPORT_SYMBOL(n_tty_ioctl_helper);