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.
102 void tty_throttle(struct tty_struct *tty)
104 /* check TTY_THROTTLED first so it indicates our state */
105 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
107 tty->ops->throttle(tty);
109 EXPORT_SYMBOL(tty_throttle);
112 * tty_unthrottle - flow control
115 * Indicate that a tty may continue transmitting data down the stack.
118 void tty_unthrottle(struct tty_struct *tty)
120 if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
121 tty->ops->unthrottle)
122 tty->ops->unthrottle(tty);
124 EXPORT_SYMBOL(tty_unthrottle);
127 * tty_wait_until_sent - wait for I/O to finish
128 * @tty: tty we are waiting for
129 * @timeout: how long we will wait
131 * Wait for characters pending in a tty driver to hit the wire, or
132 * for a timeout to occur (eg due to flow control)
137 void tty_wait_until_sent(struct tty_struct *tty, long timeout)
139 #ifdef TTY_DEBUG_WAIT_UNTIL_SENT
142 printk(KERN_DEBUG "%s wait until sent...\n", tty_name(tty, buf));
145 timeout = MAX_SCHEDULE_TIMEOUT;
146 if (wait_event_interruptible_timeout(tty->write_wait,
147 !tty_chars_in_buffer(tty), timeout) >= 0) {
148 if (tty->ops->wait_until_sent)
149 tty->ops->wait_until_sent(tty, timeout);
152 EXPORT_SYMBOL(tty_wait_until_sent);
156 * Termios Helper Methods
159 static void unset_locked_termios(struct ktermios *termios,
160 struct ktermios *old,
161 struct ktermios *locked)
165 #define NOSET_MASK(x, y, z) (x = ((x) & ~(z)) | ((y) & (z)))
168 printk(KERN_WARNING "Warning?!? termios_locked is NULL.\n");
172 NOSET_MASK(termios->c_iflag, old->c_iflag, locked->c_iflag);
173 NOSET_MASK(termios->c_oflag, old->c_oflag, locked->c_oflag);
174 NOSET_MASK(termios->c_cflag, old->c_cflag, locked->c_cflag);
175 NOSET_MASK(termios->c_lflag, old->c_lflag, locked->c_lflag);
176 termios->c_line = locked->c_line ? old->c_line : termios->c_line;
177 for (i = 0; i < NCCS; i++)
178 termios->c_cc[i] = locked->c_cc[i] ?
179 old->c_cc[i] : termios->c_cc[i];
180 /* FIXME: What should we do for i/ospeed */
184 * Routine which returns the baud rate of the tty
186 * Note that the baud_table needs to be kept in sync with the
187 * include/asm/termbits.h file.
189 static const speed_t baud_table[] = {
190 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
191 9600, 19200, 38400, 57600, 115200, 230400, 460800,
193 76800, 153600, 307200, 614400, 921600
195 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
196 2500000, 3000000, 3500000, 4000000
201 static const tcflag_t baud_bits[] = {
202 B0, B50, B75, B110, B134, B150, B200, B300, B600,
203 B1200, B1800, B2400, B4800, B9600, B19200, B38400,
204 B57600, B115200, B230400, B460800, B500000, B576000,
205 B921600, B1000000, B1152000, B1500000, B2000000, B2500000,
206 B3000000, B3500000, B4000000
209 static const tcflag_t baud_bits[] = {
210 B0, B50, B75, B110, B134, B150, B200, B300, B600,
211 B1200, B1800, B2400, B4800, B9600, B19200, B38400,
212 B57600, B115200, B230400, B460800, B76800, B153600,
213 B307200, B614400, B921600
217 static int n_baud_table = ARRAY_SIZE(baud_table);
220 * tty_termios_baud_rate
221 * @termios: termios structure
223 * Convert termios baud rate data into a speed. This should be called
224 * with the termios lock held if this termios is a terminal termios
225 * structure. May change the termios data. Device drivers can call this
226 * function but should use ->c_[io]speed directly as they are updated.
231 speed_t tty_termios_baud_rate(struct ktermios *termios)
235 cbaud = termios->c_cflag & CBAUD;
238 /* Magic token for arbitary speed via c_ispeed/c_ospeed */
240 return termios->c_ospeed;
242 if (cbaud & CBAUDEX) {
245 if (cbaud < 1 || cbaud + 15 > n_baud_table)
246 termios->c_cflag &= ~CBAUDEX;
250 return baud_table[cbaud];
252 EXPORT_SYMBOL(tty_termios_baud_rate);
255 * tty_termios_input_baud_rate
256 * @termios: termios structure
258 * Convert termios baud rate data into a speed. This should be called
259 * with the termios lock held if this termios is a terminal termios
260 * structure. May change the termios data. Device drivers can call this
261 * function but should use ->c_[io]speed directly as they are updated.
266 speed_t tty_termios_input_baud_rate(struct ktermios *termios)
269 unsigned int cbaud = (termios->c_cflag >> IBSHIFT) & CBAUD;
272 return tty_termios_baud_rate(termios);
274 /* Magic token for arbitary speed via c_ispeed*/
276 return termios->c_ispeed;
278 if (cbaud & CBAUDEX) {
281 if (cbaud < 1 || cbaud + 15 > n_baud_table)
282 termios->c_cflag &= ~(CBAUDEX << IBSHIFT);
286 return baud_table[cbaud];
288 return tty_termios_baud_rate(termios);
291 EXPORT_SYMBOL(tty_termios_input_baud_rate);
294 * tty_termios_encode_baud_rate
295 * @termios: ktermios structure holding user requested state
296 * @ispeed: input speed
297 * @ospeed: output speed
299 * Encode the speeds set into the passed termios structure. This is
300 * used as a library helper for drivers os that they can report back
301 * the actual speed selected when it differs from the speed requested
303 * For maximal back compatibility with legacy SYS5/POSIX *nix behaviour
304 * we need to carefully set the bits when the user does not get the
305 * desired speed. We allow small margins and preserve as much of possible
306 * of the input intent to keep compatiblity.
308 * Locking: Caller should hold termios lock. This is already held
309 * when calling this function from the driver termios handler.
311 * The ifdefs deal with platforms whose owners have yet to update them
312 * and will all go away once this is done.
315 void tty_termios_encode_baud_rate(struct ktermios *termios,
316 speed_t ibaud, speed_t obaud)
319 int ifound = -1, ofound = -1;
320 int iclose = ibaud/50, oclose = obaud/50;
323 if (obaud == 0) /* CD dropped */
324 ibaud = 0; /* Clear ibaud to be sure */
326 termios->c_ispeed = ibaud;
327 termios->c_ospeed = obaud;
330 /* If the user asked for a precise weird speed give a precise weird
331 answer. If they asked for a Bfoo speed they many have problems
332 digesting non-exact replies so fuzz a bit */
334 if ((termios->c_cflag & CBAUD) == BOTHER)
336 if (((termios->c_cflag >> IBSHIFT) & CBAUD) == BOTHER)
338 if ((termios->c_cflag >> IBSHIFT) & CBAUD)
339 ibinput = 1; /* An input speed was specified */
341 termios->c_cflag &= ~CBAUD;
344 * Our goal is to find a close match to the standard baud rate
345 * returned. Walk the baud rate table and if we get a very close
346 * match then report back the speed as a POSIX Bxxxx value by
351 if (obaud - oclose <= baud_table[i] &&
352 obaud + oclose >= baud_table[i]) {
353 termios->c_cflag |= baud_bits[i];
356 if (ibaud - iclose <= baud_table[i] &&
357 ibaud + iclose >= baud_table[i]) {
358 /* For the case input == output don't set IBAUD bits
359 if the user didn't do so */
360 if (ofound == i && !ibinput)
365 termios->c_cflag |= (baud_bits[i] << IBSHIFT);
369 } while (++i < n_baud_table);
372 * If we found no match then use BOTHER if provided or warn
373 * the user their platform maintainer needs to wake up if not.
377 termios->c_cflag |= BOTHER;
378 /* Set exact input bits only if the input and output differ or the
380 if (ifound == -1 && (ibaud != obaud || ibinput))
381 termios->c_cflag |= (BOTHER << IBSHIFT);
383 if (ifound == -1 || ofound == -1) {
386 printk(KERN_WARNING "tty: Unable to return correct "
387 "speed data as your architecture needs updating.\n");
391 EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate);
394 * tty_encode_baud_rate - set baud rate of the tty
395 * @ibaud: input baud rate
396 * @obad: output baud rate
398 * Update the current termios data for the tty with the new speed
399 * settings. The caller must hold the termios_mutex for the tty in
403 void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, speed_t obaud)
405 tty_termios_encode_baud_rate(tty->termios, ibaud, obaud);
407 EXPORT_SYMBOL_GPL(tty_encode_baud_rate);
410 * tty_get_baud_rate - get tty bit rates
413 * Returns the baud rate as an integer for this terminal. The
414 * termios lock must be held by the caller and the terminal bit
415 * flags may be updated.
420 speed_t tty_get_baud_rate(struct tty_struct *tty)
422 speed_t baud = tty_termios_baud_rate(tty->termios);
424 if (baud == 38400 && tty->alt_speed) {
426 printk(KERN_WARNING "Use of setserial/setrocket to "
427 "set SPD_* flags is deprecated\n");
430 baud = tty->alt_speed;
435 EXPORT_SYMBOL(tty_get_baud_rate);
438 * tty_termios_copy_hw - copy hardware settings
442 * Propogate the hardware specific terminal setting bits from
443 * the old termios structure to the new one. This is used in cases
444 * where the hardware does not support reconfiguration or as a helper
445 * in some cases where only minimal reconfiguration is supported
448 void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old)
450 /* The bits a dumb device handles in software. Smart devices need
451 to always provide a set_termios method */
452 new->c_cflag &= HUPCL | CREAD | CLOCAL;
453 new->c_cflag |= old->c_cflag & ~(HUPCL | CREAD | CLOCAL);
454 new->c_ispeed = old->c_ispeed;
455 new->c_ospeed = old->c_ospeed;
457 EXPORT_SYMBOL(tty_termios_copy_hw);
460 * tty_termios_hw_change - check for setting change
462 * @b: termios to compare
464 * Check if any of the bits that affect a dumb device have changed
465 * between the two termios structures, or a speed change is needed.
468 int tty_termios_hw_change(struct ktermios *a, struct ktermios *b)
470 if (a->c_ispeed != b->c_ispeed || a->c_ospeed != b->c_ospeed)
472 if ((a->c_cflag ^ b->c_cflag) & ~(HUPCL | CREAD | CLOCAL))
476 EXPORT_SYMBOL(tty_termios_hw_change);
479 * change_termios - update termios values
480 * @tty: tty to update
481 * @new_termios: desired new value
483 * Perform updates to the termios values set on this terminal. There
484 * is a bit of layering violation here with n_tty in terms of the
485 * internal knowledge of this function.
487 * Locking: termios_mutex
490 static void change_termios(struct tty_struct *tty, struct ktermios *new_termios)
493 struct ktermios old_termios;
494 struct tty_ldisc *ld;
498 * Perform the actual termios internal changes under lock.
502 /* FIXME: we need to decide on some locking/ordering semantics
503 for the set_termios notification eventually */
504 mutex_lock(&tty->termios_mutex);
505 old_termios = *tty->termios;
506 *tty->termios = *new_termios;
507 unset_locked_termios(tty->termios, &old_termios, tty->termios_locked);
508 canon_change = (old_termios.c_lflag ^ tty->termios->c_lflag) & ICANON;
510 memset(&tty->read_flags, 0, sizeof tty->read_flags);
511 tty->canon_head = tty->read_tail;
516 /* This bit should be in the ldisc code */
517 if (canon_change && !L_ICANON(tty) && tty->read_cnt)
518 /* Get characters left over from canonical mode. */
519 wake_up_interruptible(&tty->read_wait);
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);
680 static unsigned long inq_canon(struct tty_struct *tty)
684 if (!tty->canon_data || !tty->read_buf)
686 head = tty->canon_head;
687 tail = tty->read_tail;
688 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
689 /* Skip EOF-chars.. */
690 while (head != tail) {
691 if (test_bit(tail, tty->read_flags) &&
692 tty->read_buf[tail] == __DISABLED_CHAR)
694 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
701 * These are deprecated, but there is limited support..
703 * The "sg_flags" translation is a joke..
705 static int get_sgflags(struct tty_struct *tty)
709 if (!(tty->termios->c_lflag & ICANON)) {
710 if (tty->termios->c_lflag & ISIG)
711 flags |= 0x02; /* cbreak */
713 flags |= 0x20; /* raw */
715 if (tty->termios->c_lflag & ECHO)
716 flags |= 0x08; /* echo */
717 if (tty->termios->c_oflag & OPOST)
718 if (tty->termios->c_oflag & ONLCR)
719 flags |= 0x10; /* crmod */
723 static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
727 mutex_lock(&tty->termios_mutex);
728 tmp.sg_ispeed = tty->termios->c_ispeed;
729 tmp.sg_ospeed = tty->termios->c_ospeed;
730 tmp.sg_erase = tty->termios->c_cc[VERASE];
731 tmp.sg_kill = tty->termios->c_cc[VKILL];
732 tmp.sg_flags = get_sgflags(tty);
733 mutex_unlock(&tty->termios_mutex);
735 return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
738 static void set_sgflags(struct ktermios *termios, int flags)
740 termios->c_iflag = ICRNL | IXON;
741 termios->c_oflag = 0;
742 termios->c_lflag = ISIG | ICANON;
743 if (flags & 0x02) { /* cbreak */
744 termios->c_iflag = 0;
745 termios->c_lflag &= ~ICANON;
747 if (flags & 0x08) { /* echo */
748 termios->c_lflag |= ECHO | ECHOE | ECHOK |
749 ECHOCTL | ECHOKE | IEXTEN;
751 if (flags & 0x10) { /* crmod */
752 termios->c_oflag |= OPOST | ONLCR;
754 if (flags & 0x20) { /* raw */
755 termios->c_iflag = 0;
756 termios->c_lflag &= ~(ISIG | ICANON);
758 if (!(termios->c_lflag & ICANON)) {
759 termios->c_cc[VMIN] = 1;
760 termios->c_cc[VTIME] = 0;
765 * set_sgttyb - set legacy terminal values
766 * @tty: tty structure
767 * @sgttyb: pointer to old style terminal structure
769 * Updates a terminal from the legacy BSD style terminal information
772 * Locking: termios_mutex
775 static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
779 struct ktermios termios;
781 retval = tty_check_change(tty);
785 if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
788 mutex_lock(&tty->termios_mutex);
789 termios = *tty->termios;
790 termios.c_cc[VERASE] = tmp.sg_erase;
791 termios.c_cc[VKILL] = tmp.sg_kill;
792 set_sgflags(&termios, tmp.sg_flags);
793 /* Try and encode into Bfoo format */
795 tty_termios_encode_baud_rate(&termios, termios.c_ispeed,
798 mutex_unlock(&tty->termios_mutex);
799 change_termios(tty, &termios);
805 static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars)
809 mutex_lock(&tty->termios_mutex);
810 tmp.t_intrc = tty->termios->c_cc[VINTR];
811 tmp.t_quitc = tty->termios->c_cc[VQUIT];
812 tmp.t_startc = tty->termios->c_cc[VSTART];
813 tmp.t_stopc = tty->termios->c_cc[VSTOP];
814 tmp.t_eofc = tty->termios->c_cc[VEOF];
815 tmp.t_brkc = tty->termios->c_cc[VEOL2]; /* what is brkc anyway? */
816 mutex_unlock(&tty->termios_mutex);
817 return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
820 static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars)
824 if (copy_from_user(&tmp, tchars, sizeof(tmp)))
826 mutex_lock(&tty->termios_mutex);
827 tty->termios->c_cc[VINTR] = tmp.t_intrc;
828 tty->termios->c_cc[VQUIT] = tmp.t_quitc;
829 tty->termios->c_cc[VSTART] = tmp.t_startc;
830 tty->termios->c_cc[VSTOP] = tmp.t_stopc;
831 tty->termios->c_cc[VEOF] = tmp.t_eofc;
832 tty->termios->c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
833 mutex_unlock(&tty->termios_mutex);
839 static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
843 mutex_lock(&tty->termios_mutex);
844 tmp.t_suspc = tty->termios->c_cc[VSUSP];
845 /* what is dsuspc anyway? */
846 tmp.t_dsuspc = tty->termios->c_cc[VSUSP];
847 tmp.t_rprntc = tty->termios->c_cc[VREPRINT];
848 /* what is flushc anyway? */
849 tmp.t_flushc = tty->termios->c_cc[VEOL2];
850 tmp.t_werasc = tty->termios->c_cc[VWERASE];
851 tmp.t_lnextc = tty->termios->c_cc[VLNEXT];
852 mutex_unlock(&tty->termios_mutex);
853 return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
856 static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
860 if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
863 mutex_lock(&tty->termios_mutex);
864 tty->termios->c_cc[VSUSP] = tmp.t_suspc;
865 /* what is dsuspc anyway? */
866 tty->termios->c_cc[VEOL2] = tmp.t_dsuspc;
867 tty->termios->c_cc[VREPRINT] = tmp.t_rprntc;
868 /* what is flushc anyway? */
869 tty->termios->c_cc[VEOL2] = tmp.t_flushc;
870 tty->termios->c_cc[VWERASE] = tmp.t_werasc;
871 tty->termios->c_cc[VLNEXT] = tmp.t_lnextc;
872 mutex_unlock(&tty->termios_mutex);
878 * send_prio_char - send priority character
880 * Send a high priority character to the tty even if stopped
882 * Locking: none for xchar method, write ordering for write method.
885 static int send_prio_char(struct tty_struct *tty, char ch)
887 int was_stopped = tty->stopped;
889 if (tty->ops->send_xchar) {
890 tty->ops->send_xchar(tty, ch);
894 if (tty_write_lock(tty, 0) < 0)
899 tty->ops->write(tty, &ch, 1);
902 tty_write_unlock(tty);
907 * tty_change_softcar - carrier change ioctl helper
908 * @tty: tty to update
909 * @arg: enable/disable CLOCAL
911 * Perform a change to the CLOCAL state and call into the driver
912 * layer to make it visible. All done with the termios mutex
915 static int tty_change_softcar(struct tty_struct *tty, int arg)
918 int bit = arg ? CLOCAL : 0;
921 mutex_lock(&tty->termios_mutex);
923 tty->termios->c_cflag &= ~CLOCAL;
924 tty->termios->c_cflag |= bit;
925 if (tty->ops->set_termios)
926 tty->ops->set_termios(tty, &old);
927 if ((tty->termios->c_cflag & CLOCAL) != bit)
929 mutex_unlock(&tty->termios_mutex);
934 * tty_mode_ioctl - mode related ioctls
935 * @tty: tty for the ioctl
936 * @file: file pointer for the tty
938 * @arg: ioctl argument
940 * Perform non line discipline specific mode control ioctls. This
941 * is designed to be called by line disciplines to ensure they provide
942 * consistent mode setting.
945 int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
946 unsigned int cmd, unsigned long arg)
948 struct tty_struct *real_tty;
949 void __user *p = (void __user *)arg;
952 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
953 tty->driver->subtype == PTY_TYPE_MASTER)
954 real_tty = tty->link;
961 return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
964 return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
968 return get_tchars(real_tty, p);
970 return set_tchars(real_tty, p);
974 return get_ltchars(real_tty, p);
976 return set_ltchars(real_tty, p);
979 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
981 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
983 return set_termios(real_tty, p, TERMIOS_OLD);
986 mutex_lock(&real_tty->termios_mutex);
987 if (kernel_termios_to_user_termios((struct termios __user *)arg, real_tty->termios))
989 mutex_unlock(&real_tty->termios_mutex);
993 mutex_lock(&real_tty->termios_mutex);
994 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, real_tty->termios))
996 mutex_unlock(&real_tty->termios_mutex);
999 mutex_lock(&real_tty->termios_mutex);
1000 if (kernel_termios_to_user_termios((struct termios2 __user *)arg, real_tty->termios))
1002 mutex_unlock(&real_tty->termios_mutex);
1005 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
1007 return set_termios(real_tty, p, TERMIOS_WAIT);
1009 return set_termios(real_tty, p, 0);
1012 return get_termio(real_tty, p);
1014 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
1016 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
1018 return set_termios(real_tty, p, TERMIOS_TERMIO);
1020 case TIOCGLCKTRMIOS:
1021 mutex_lock(&real_tty->termios_mutex);
1022 if (kernel_termios_to_user_termios((struct termios __user *)arg, real_tty->termios_locked))
1024 mutex_unlock(&real_tty->termios_mutex);
1026 case TIOCSLCKTRMIOS:
1027 if (!capable(CAP_SYS_ADMIN))
1029 mutex_lock(&real_tty->termios_mutex);
1030 if (user_termios_to_kernel_termios(real_tty->termios_locked,
1031 (struct termios __user *) arg))
1033 mutex_unlock(&real_tty->termios_mutex);
1036 case TIOCGLCKTRMIOS:
1037 mutex_lock(&real_tty->termios_mutex);
1038 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, real_tty->termios_locked))
1040 mutex_unlock(&real_tty->termios_mutex);
1042 case TIOCSLCKTRMIOS:
1043 if (!capable(CAP_SYS_ADMIN))
1045 mutex_lock(&real_tty->termios_mutex);
1046 if (user_termios_to_kernel_termios_1(real_tty->termios_locked,
1047 (struct termios __user *) arg))
1049 mutex_unlock(&real_tty->termios_mutex);
1054 if (real_tty->termiox == NULL)
1056 mutex_lock(&real_tty->termios_mutex);
1057 if (copy_to_user(p, real_tty->termiox, sizeof(struct termiox)))
1059 mutex_unlock(&real_tty->termios_mutex);
1062 return set_termiox(real_tty, p, 0);
1064 return set_termiox(real_tty, p, TERMIOS_WAIT);
1066 return set_termiox(real_tty, p, TERMIOS_FLUSH);
1069 mutex_lock(&real_tty->termios_mutex);
1070 ret = put_user(C_CLOCAL(real_tty) ? 1 : 0,
1072 mutex_unlock(&real_tty->termios_mutex);
1075 if (get_user(arg, (unsigned int __user *) arg))
1077 return tty_change_softcar(real_tty, arg);
1079 return -ENOIOCTLCMD;
1082 EXPORT_SYMBOL_GPL(tty_mode_ioctl);
1084 int tty_perform_flush(struct tty_struct *tty, unsigned long arg)
1086 struct tty_ldisc *ld;
1087 int retval = tty_check_change(tty);
1091 ld = tty_ldisc_ref(tty);
1094 if (ld && ld->ops->flush_buffer)
1095 ld->ops->flush_buffer(tty);
1098 if (ld && ld->ops->flush_buffer)
1099 ld->ops->flush_buffer(tty);
1102 tty_driver_flush_buffer(tty);
1105 tty_ldisc_deref(ld);
1108 tty_ldisc_deref(ld);
1111 EXPORT_SYMBOL_GPL(tty_perform_flush);
1113 int n_tty_ioctl(struct tty_struct *tty, struct file *file,
1114 unsigned int cmd, unsigned long arg)
1116 unsigned long flags;
1121 retval = tty_check_change(tty);
1126 if (!tty->flow_stopped) {
1127 tty->flow_stopped = 1;
1132 if (tty->flow_stopped) {
1133 tty->flow_stopped = 0;
1138 if (STOP_CHAR(tty) != __DISABLED_CHAR)
1139 return send_prio_char(tty, STOP_CHAR(tty));
1142 if (START_CHAR(tty) != __DISABLED_CHAR)
1143 return send_prio_char(tty, START_CHAR(tty));
1150 return tty_perform_flush(tty, arg);
1152 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
1154 retval = tty->read_cnt;
1156 retval = inq_canon(tty);
1157 return put_user(retval, (unsigned int __user *) arg);
1162 if (tty->driver->type != TTY_DRIVER_TYPE_PTY ||
1163 tty->driver->subtype != PTY_TYPE_MASTER)
1165 if (get_user(pktmode, (int __user *) arg))
1167 spin_lock_irqsave(&tty->ctrl_lock, flags);
1171 tty->link->ctrl_status = 0;
1175 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1179 /* Try the mode commands */
1180 return tty_mode_ioctl(tty, file, cmd, arg);
1183 EXPORT_SYMBOL(n_tty_ioctl);