Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds |
| 3 | * |
| 4 | * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines |
| 5 | * which can be dynamically activated and de-activated by the line |
| 6 | * discipline handling modules (like SLIP). |
| 7 | */ |
| 8 | |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/termios.h> |
| 11 | #include <linux/errno.h> |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/major.h> |
| 15 | #include <linux/tty.h> |
| 16 | #include <linux/fcntl.h> |
| 17 | #include <linux/string.h> |
| 18 | #include <linux/mm.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/bitops.h> |
Arjan van de Ven | 5785c95 | 2006-09-29 02:00:43 -0700 | [diff] [blame] | 21 | #include <linux/mutex.h> |
Thomas Meyer | 8193c42 | 2011-10-05 23:13:13 +0200 | [diff] [blame] | 22 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #include <asm/io.h> |
| 25 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #undef TTY_DEBUG_WAIT_UNTIL_SENT |
| 28 | |
Peter Hurley | ff8339d | 2015-07-12 22:49:11 -0400 | [diff] [blame] | 29 | #ifdef TTY_DEBUG_WAIT_UNTIL_SENT |
| 30 | # define tty_debug_wait_until_sent(tty, f, args...) tty_debug(tty, f, ##args) |
| 31 | #else |
| 32 | # define tty_debug_wait_until_sent(tty, f, args...) do {} while (0) |
| 33 | #endif |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #undef DEBUG |
| 36 | |
| 37 | /* |
| 38 | * Internal flag options for termios setting behavior |
| 39 | */ |
| 40 | #define TERMIOS_FLUSH 1 |
| 41 | #define TERMIOS_WAIT 2 |
| 42 | #define TERMIOS_TERMIO 4 |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 43 | #define TERMIOS_OLD 8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 45 | |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 46 | /** |
| 47 | * tty_chars_in_buffer - characters pending |
| 48 | * @tty: terminal |
| 49 | * |
| 50 | * Return the number of bytes of data in the device private |
| 51 | * output queue. If no private method is supplied there is assumed |
| 52 | * to be no queue on the device. |
| 53 | */ |
| 54 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 55 | int tty_chars_in_buffer(struct tty_struct *tty) |
| 56 | { |
| 57 | if (tty->ops->chars_in_buffer) |
| 58 | return tty->ops->chars_in_buffer(tty); |
| 59 | else |
| 60 | return 0; |
| 61 | } |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 62 | EXPORT_SYMBOL(tty_chars_in_buffer); |
| 63 | |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 64 | /** |
| 65 | * tty_write_room - write queue space |
| 66 | * @tty: terminal |
| 67 | * |
| 68 | * Return the number of bytes that can be queued to this device |
| 69 | * at the present time. The result should be treated as a guarantee |
| 70 | * and the driver cannot offer a value it later shrinks by more than |
| 71 | * the number of bytes written. If no method is provided 2K is always |
| 72 | * returned and data may be lost as there will be no flow control. |
| 73 | */ |
| 74 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 75 | int tty_write_room(struct tty_struct *tty) |
| 76 | { |
| 77 | if (tty->ops->write_room) |
| 78 | return tty->ops->write_room(tty); |
| 79 | return 2048; |
| 80 | } |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 81 | EXPORT_SYMBOL(tty_write_room); |
| 82 | |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 83 | /** |
| 84 | * tty_driver_flush_buffer - discard internal buffer |
| 85 | * @tty: terminal |
| 86 | * |
| 87 | * Discard the internal output buffer for this device. If no method |
| 88 | * is provided then either the buffer cannot be hardware flushed or |
| 89 | * there is no buffer driver side. |
| 90 | */ |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 91 | void tty_driver_flush_buffer(struct tty_struct *tty) |
| 92 | { |
| 93 | if (tty->ops->flush_buffer) |
| 94 | tty->ops->flush_buffer(tty); |
| 95 | } |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 96 | EXPORT_SYMBOL(tty_driver_flush_buffer); |
| 97 | |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 98 | /** |
| 99 | * tty_throttle - flow control |
| 100 | * @tty: terminal |
| 101 | * |
| 102 | * Indicate that a tty should stop transmitting data down the stack. |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 103 | * Takes the termios rwsem to protect against parallel throttle/unthrottle |
Alan Cox | 38db897 | 2009-06-11 12:44:17 +0100 | [diff] [blame] | 104 | * and also to ensure the driver can consistently reference its own |
| 105 | * termios data at this point when implementing software flow control. |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 106 | */ |
| 107 | |
Alan Cox | 39c2e60 | 2008-04-30 00:54:18 -0700 | [diff] [blame] | 108 | void tty_throttle(struct tty_struct *tty) |
| 109 | { |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 110 | down_write(&tty->termios_rwsem); |
Alan Cox | 39c2e60 | 2008-04-30 00:54:18 -0700 | [diff] [blame] | 111 | /* check TTY_THROTTLED first so it indicates our state */ |
| 112 | if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) && |
| 113 | tty->ops->throttle) |
| 114 | tty->ops->throttle(tty); |
Peter Hurley | 70bc126 | 2013-03-06 08:20:52 -0500 | [diff] [blame] | 115 | tty->flow_change = 0; |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 116 | up_write(&tty->termios_rwsem); |
Alan Cox | 39c2e60 | 2008-04-30 00:54:18 -0700 | [diff] [blame] | 117 | } |
| 118 | EXPORT_SYMBOL(tty_throttle); |
| 119 | |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 120 | /** |
| 121 | * tty_unthrottle - flow control |
| 122 | * @tty: terminal |
| 123 | * |
| 124 | * Indicate that a tty may continue transmitting data down the stack. |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 125 | * Takes the termios rwsem to protect against parallel throttle/unthrottle |
Alan Cox | 38db897 | 2009-06-11 12:44:17 +0100 | [diff] [blame] | 126 | * and also to ensure the driver can consistently reference its own |
| 127 | * termios data at this point when implementing software flow control. |
| 128 | * |
| 129 | * Drivers should however remember that the stack can issue a throttle, |
| 130 | * then change flow control method, then unthrottle. |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 131 | */ |
| 132 | |
Alan Cox | 39c2e60 | 2008-04-30 00:54:18 -0700 | [diff] [blame] | 133 | void tty_unthrottle(struct tty_struct *tty) |
| 134 | { |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 135 | down_write(&tty->termios_rwsem); |
Alan Cox | 39c2e60 | 2008-04-30 00:54:18 -0700 | [diff] [blame] | 136 | if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) && |
| 137 | tty->ops->unthrottle) |
| 138 | tty->ops->unthrottle(tty); |
Peter Hurley | 70bc126 | 2013-03-06 08:20:52 -0500 | [diff] [blame] | 139 | tty->flow_change = 0; |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 140 | up_write(&tty->termios_rwsem); |
Alan Cox | 39c2e60 | 2008-04-30 00:54:18 -0700 | [diff] [blame] | 141 | } |
| 142 | EXPORT_SYMBOL(tty_unthrottle); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 143 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 144 | /** |
Peter Hurley | 70bc126 | 2013-03-06 08:20:52 -0500 | [diff] [blame] | 145 | * tty_throttle_safe - flow control |
| 146 | * @tty: terminal |
| 147 | * |
| 148 | * Similar to tty_throttle() but will only attempt throttle |
| 149 | * if tty->flow_change is TTY_THROTTLE_SAFE. Prevents an accidental |
| 150 | * throttle due to race conditions when throttling is conditional |
| 151 | * on factors evaluated prior to throttling. |
| 152 | * |
| 153 | * Returns 0 if tty is throttled (or was already throttled) |
| 154 | */ |
| 155 | |
| 156 | int tty_throttle_safe(struct tty_struct *tty) |
| 157 | { |
| 158 | int ret = 0; |
| 159 | |
Peter Hurley | d8c1f92 | 2013-06-15 09:14:31 -0400 | [diff] [blame] | 160 | mutex_lock(&tty->throttle_mutex); |
Peter Hurley | 97ef38b | 2016-04-09 17:11:36 -0700 | [diff] [blame] | 161 | if (!tty_throttled(tty)) { |
Peter Hurley | 70bc126 | 2013-03-06 08:20:52 -0500 | [diff] [blame] | 162 | if (tty->flow_change != TTY_THROTTLE_SAFE) |
| 163 | ret = 1; |
| 164 | else { |
Peter Hurley | 579a00a | 2013-04-15 11:06:06 -0400 | [diff] [blame] | 165 | set_bit(TTY_THROTTLED, &tty->flags); |
Peter Hurley | 70bc126 | 2013-03-06 08:20:52 -0500 | [diff] [blame] | 166 | if (tty->ops->throttle) |
| 167 | tty->ops->throttle(tty); |
| 168 | } |
| 169 | } |
Peter Hurley | d8c1f92 | 2013-06-15 09:14:31 -0400 | [diff] [blame] | 170 | mutex_unlock(&tty->throttle_mutex); |
Peter Hurley | 70bc126 | 2013-03-06 08:20:52 -0500 | [diff] [blame] | 171 | |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * tty_unthrottle_safe - flow control |
| 177 | * @tty: terminal |
| 178 | * |
| 179 | * Similar to tty_unthrottle() but will only attempt unthrottle |
| 180 | * if tty->flow_change is TTY_UNTHROTTLE_SAFE. Prevents an accidental |
| 181 | * unthrottle due to race conditions when unthrottling is conditional |
| 182 | * on factors evaluated prior to unthrottling. |
| 183 | * |
| 184 | * Returns 0 if tty is unthrottled (or was already unthrottled) |
| 185 | */ |
| 186 | |
| 187 | int tty_unthrottle_safe(struct tty_struct *tty) |
| 188 | { |
| 189 | int ret = 0; |
| 190 | |
Peter Hurley | d8c1f92 | 2013-06-15 09:14:31 -0400 | [diff] [blame] | 191 | mutex_lock(&tty->throttle_mutex); |
Peter Hurley | 97ef38b | 2016-04-09 17:11:36 -0700 | [diff] [blame] | 192 | if (tty_throttled(tty)) { |
Peter Hurley | 70bc126 | 2013-03-06 08:20:52 -0500 | [diff] [blame] | 193 | if (tty->flow_change != TTY_UNTHROTTLE_SAFE) |
| 194 | ret = 1; |
| 195 | else { |
Peter Hurley | 579a00a | 2013-04-15 11:06:06 -0400 | [diff] [blame] | 196 | clear_bit(TTY_THROTTLED, &tty->flags); |
Peter Hurley | 70bc126 | 2013-03-06 08:20:52 -0500 | [diff] [blame] | 197 | if (tty->ops->unthrottle) |
| 198 | tty->ops->unthrottle(tty); |
| 199 | } |
| 200 | } |
Peter Hurley | d8c1f92 | 2013-06-15 09:14:31 -0400 | [diff] [blame] | 201 | mutex_unlock(&tty->throttle_mutex); |
Peter Hurley | 70bc126 | 2013-03-06 08:20:52 -0500 | [diff] [blame] | 202 | |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | /** |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 207 | * tty_wait_until_sent - wait for I/O to finish |
| 208 | * @tty: tty we are waiting for |
| 209 | * @timeout: how long we will wait |
| 210 | * |
| 211 | * Wait for characters pending in a tty driver to hit the wire, or |
| 212 | * for a timeout to occur (eg due to flow control) |
| 213 | * |
| 214 | * Locking: none |
| 215 | */ |
| 216 | |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 217 | void tty_wait_until_sent(struct tty_struct *tty, long timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
Peter Hurley | d435cef | 2015-11-08 13:01:19 -0500 | [diff] [blame] | 219 | tty_debug_wait_until_sent(tty, "wait until sent, timeout=%ld\n", timeout); |
Peter Hurley | ff8339d | 2015-07-12 22:49:11 -0400 | [diff] [blame] | 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | if (!timeout) |
| 222 | timeout = MAX_SCHEDULE_TIMEOUT; |
Johan Hovold | 79fbf4a | 2015-03-04 10:39:06 +0100 | [diff] [blame] | 223 | |
Johan Hovold | c37bc68 | 2015-03-04 10:39:07 +0100 | [diff] [blame] | 224 | timeout = wait_event_interruptible_timeout(tty->write_wait, |
| 225 | !tty_chars_in_buffer(tty), timeout); |
| 226 | if (timeout <= 0) |
Johan Hovold | 79fbf4a | 2015-03-04 10:39:06 +0100 | [diff] [blame] | 227 | return; |
Johan Hovold | 79fbf4a | 2015-03-04 10:39:06 +0100 | [diff] [blame] | 228 | |
| 229 | if (timeout == MAX_SCHEDULE_TIMEOUT) |
| 230 | timeout = 0; |
| 231 | |
| 232 | if (tty->ops->wait_until_sent) |
| 233 | tty->ops->wait_until_sent(tty, timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | EXPORT_SYMBOL(tty_wait_until_sent); |
| 236 | |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 237 | |
| 238 | /* |
| 239 | * Termios Helper Methods |
| 240 | */ |
| 241 | |
Peter Hurley | d97ba9c | 2015-11-08 13:01:16 -0500 | [diff] [blame] | 242 | static void unset_locked_termios(struct tty_struct *tty, struct ktermios *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | { |
Peter Hurley | d97ba9c | 2015-11-08 13:01:16 -0500 | [diff] [blame] | 244 | struct ktermios *termios = &tty->termios; |
| 245 | struct ktermios *locked = &tty->termios_locked; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | int i; |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 247 | |
| 248 | #define NOSET_MASK(x, y, z) (x = ((x) & ~(z)) | ((y) & (z))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | NOSET_MASK(termios->c_iflag, old->c_iflag, locked->c_iflag); |
| 251 | NOSET_MASK(termios->c_oflag, old->c_oflag, locked->c_oflag); |
| 252 | NOSET_MASK(termios->c_cflag, old->c_cflag, locked->c_cflag); |
| 253 | NOSET_MASK(termios->c_lflag, old->c_lflag, locked->c_lflag); |
| 254 | termios->c_line = locked->c_line ? old->c_line : termios->c_line; |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 255 | for (i = 0; i < NCCS; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | termios->c_cc[i] = locked->c_cc[i] ? |
| 257 | old->c_cc[i] : termios->c_cc[i]; |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 258 | /* FIXME: What should we do for i/ospeed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 261 | /* |
| 262 | * Routine which returns the baud rate of the tty |
| 263 | * |
| 264 | * Note that the baud_table needs to be kept in sync with the |
| 265 | * include/asm/termbits.h file. |
| 266 | */ |
| 267 | static const speed_t baud_table[] = { |
| 268 | 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, |
| 269 | 9600, 19200, 38400, 57600, 115200, 230400, 460800, |
| 270 | #ifdef __sparc__ |
| 271 | 76800, 153600, 307200, 614400, 921600 |
| 272 | #else |
| 273 | 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000, |
| 274 | 2500000, 3000000, 3500000, 4000000 |
| 275 | #endif |
| 276 | }; |
| 277 | |
| 278 | #ifndef __sparc__ |
| 279 | static const tcflag_t baud_bits[] = { |
| 280 | B0, B50, B75, B110, B134, B150, B200, B300, B600, |
| 281 | B1200, B1800, B2400, B4800, B9600, B19200, B38400, |
| 282 | B57600, B115200, B230400, B460800, B500000, B576000, |
| 283 | B921600, B1000000, B1152000, B1500000, B2000000, B2500000, |
| 284 | B3000000, B3500000, B4000000 |
| 285 | }; |
| 286 | #else |
| 287 | static const tcflag_t baud_bits[] = { |
| 288 | B0, B50, B75, B110, B134, B150, B200, B300, B600, |
| 289 | B1200, B1800, B2400, B4800, B9600, B19200, B38400, |
| 290 | B57600, B115200, B230400, B460800, B76800, B153600, |
| 291 | B307200, B614400, B921600 |
| 292 | }; |
| 293 | #endif |
| 294 | |
| 295 | static int n_baud_table = ARRAY_SIZE(baud_table); |
| 296 | |
| 297 | /** |
| 298 | * tty_termios_baud_rate |
| 299 | * @termios: termios structure |
| 300 | * |
| 301 | * Convert termios baud rate data into a speed. This should be called |
| 302 | * with the termios lock held if this termios is a terminal termios |
| 303 | * structure. May change the termios data. Device drivers can call this |
| 304 | * function but should use ->c_[io]speed directly as they are updated. |
| 305 | * |
| 306 | * Locking: none |
| 307 | */ |
| 308 | |
| 309 | speed_t tty_termios_baud_rate(struct ktermios *termios) |
| 310 | { |
| 311 | unsigned int cbaud; |
| 312 | |
| 313 | cbaud = termios->c_cflag & CBAUD; |
| 314 | |
| 315 | #ifdef BOTHER |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 316 | /* Magic token for arbitrary speed via c_ispeed/c_ospeed */ |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 317 | if (cbaud == BOTHER) |
| 318 | return termios->c_ospeed; |
| 319 | #endif |
| 320 | if (cbaud & CBAUDEX) { |
| 321 | cbaud &= ~CBAUDEX; |
| 322 | |
| 323 | if (cbaud < 1 || cbaud + 15 > n_baud_table) |
| 324 | termios->c_cflag &= ~CBAUDEX; |
| 325 | else |
| 326 | cbaud += 15; |
| 327 | } |
| 328 | return baud_table[cbaud]; |
| 329 | } |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 330 | EXPORT_SYMBOL(tty_termios_baud_rate); |
| 331 | |
| 332 | /** |
| 333 | * tty_termios_input_baud_rate |
| 334 | * @termios: termios structure |
| 335 | * |
| 336 | * Convert termios baud rate data into a speed. This should be called |
| 337 | * with the termios lock held if this termios is a terminal termios |
| 338 | * structure. May change the termios data. Device drivers can call this |
| 339 | * function but should use ->c_[io]speed directly as they are updated. |
| 340 | * |
| 341 | * Locking: none |
| 342 | */ |
| 343 | |
| 344 | speed_t tty_termios_input_baud_rate(struct ktermios *termios) |
| 345 | { |
| 346 | #ifdef IBSHIFT |
| 347 | unsigned int cbaud = (termios->c_cflag >> IBSHIFT) & CBAUD; |
| 348 | |
| 349 | if (cbaud == B0) |
| 350 | return tty_termios_baud_rate(termios); |
| 351 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 352 | /* Magic token for arbitrary speed via c_ispeed*/ |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 353 | if (cbaud == BOTHER) |
| 354 | return termios->c_ispeed; |
| 355 | |
| 356 | if (cbaud & CBAUDEX) { |
| 357 | cbaud &= ~CBAUDEX; |
| 358 | |
| 359 | if (cbaud < 1 || cbaud + 15 > n_baud_table) |
| 360 | termios->c_cflag &= ~(CBAUDEX << IBSHIFT); |
| 361 | else |
| 362 | cbaud += 15; |
| 363 | } |
| 364 | return baud_table[cbaud]; |
| 365 | #else |
| 366 | return tty_termios_baud_rate(termios); |
| 367 | #endif |
| 368 | } |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 369 | EXPORT_SYMBOL(tty_termios_input_baud_rate); |
| 370 | |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 371 | /** |
| 372 | * tty_termios_encode_baud_rate |
Alan Cox | 78137e3 | 2007-02-10 01:45:57 -0800 | [diff] [blame] | 373 | * @termios: ktermios structure holding user requested state |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 374 | * @ispeed: input speed |
| 375 | * @ospeed: output speed |
| 376 | * |
| 377 | * Encode the speeds set into the passed termios structure. This is |
Uwe Kleine-König | f98e5b8 | 2011-04-12 09:59:29 +0200 | [diff] [blame] | 378 | * used as a library helper for drivers so that they can report back |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 379 | * the actual speed selected when it differs from the speed requested |
| 380 | * |
Alan Cox | 78137e3 | 2007-02-10 01:45:57 -0800 | [diff] [blame] | 381 | * For maximal back compatibility with legacy SYS5/POSIX *nix behaviour |
| 382 | * we need to carefully set the bits when the user does not get the |
| 383 | * desired speed. We allow small margins and preserve as much of possible |
Dirk Hohndel | 06fe9fb | 2009-09-28 21:43:57 -0400 | [diff] [blame] | 384 | * of the input intent to keep compatibility. |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 385 | * |
| 386 | * Locking: Caller should hold termios lock. This is already held |
| 387 | * when calling this function from the driver termios handler. |
Alan Cox | 5f519d7 | 2007-10-16 23:30:07 -0700 | [diff] [blame] | 388 | * |
| 389 | * The ifdefs deal with platforms whose owners have yet to update them |
| 390 | * and will all go away once this is done. |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 391 | */ |
| 392 | |
Maciej W. Rozycki | 75e8b71 | 2007-10-18 03:04:35 -0700 | [diff] [blame] | 393 | void tty_termios_encode_baud_rate(struct ktermios *termios, |
| 394 | speed_t ibaud, speed_t obaud) |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 395 | { |
| 396 | int i = 0; |
Alan Cox | 78137e3 | 2007-02-10 01:45:57 -0800 | [diff] [blame] | 397 | int ifound = -1, ofound = -1; |
| 398 | int iclose = ibaud/50, oclose = obaud/50; |
| 399 | int ibinput = 0; |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 400 | |
Alan Cox | 5f519d7 | 2007-10-16 23:30:07 -0700 | [diff] [blame] | 401 | if (obaud == 0) /* CD dropped */ |
| 402 | ibaud = 0; /* Clear ibaud to be sure */ |
| 403 | |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 404 | termios->c_ispeed = ibaud; |
| 405 | termios->c_ospeed = obaud; |
| 406 | |
Alan Cox | 5f519d7 | 2007-10-16 23:30:07 -0700 | [diff] [blame] | 407 | #ifdef BOTHER |
Alan Cox | 78137e3 | 2007-02-10 01:45:57 -0800 | [diff] [blame] | 408 | /* If the user asked for a precise weird speed give a precise weird |
Matthias Brugger | a1d51aa2 | 2014-08-08 13:01:21 +0200 | [diff] [blame] | 409 | answer. If they asked for a Bfoo speed they may have problems |
Alan Cox | 78137e3 | 2007-02-10 01:45:57 -0800 | [diff] [blame] | 410 | digesting non-exact replies so fuzz a bit */ |
| 411 | |
| 412 | if ((termios->c_cflag & CBAUD) == BOTHER) |
| 413 | oclose = 0; |
| 414 | if (((termios->c_cflag >> IBSHIFT) & CBAUD) == BOTHER) |
| 415 | iclose = 0; |
| 416 | if ((termios->c_cflag >> IBSHIFT) & CBAUD) |
| 417 | ibinput = 1; /* An input speed was specified */ |
Alan Cox | 5f519d7 | 2007-10-16 23:30:07 -0700 | [diff] [blame] | 418 | #endif |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 419 | termios->c_cflag &= ~CBAUD; |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 420 | |
Alan Cox | 5f519d7 | 2007-10-16 23:30:07 -0700 | [diff] [blame] | 421 | /* |
| 422 | * Our goal is to find a close match to the standard baud rate |
| 423 | * returned. Walk the baud rate table and if we get a very close |
| 424 | * match then report back the speed as a POSIX Bxxxx value by |
| 425 | * preference |
| 426 | */ |
| 427 | |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 428 | do { |
Maciej W. Rozycki | 75e8b71 | 2007-10-18 03:04:35 -0700 | [diff] [blame] | 429 | if (obaud - oclose <= baud_table[i] && |
| 430 | obaud + oclose >= baud_table[i]) { |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 431 | termios->c_cflag |= baud_bits[i]; |
Alan Cox | 78137e3 | 2007-02-10 01:45:57 -0800 | [diff] [blame] | 432 | ofound = i; |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 433 | } |
Maciej W. Rozycki | 75e8b71 | 2007-10-18 03:04:35 -0700 | [diff] [blame] | 434 | if (ibaud - iclose <= baud_table[i] && |
| 435 | ibaud + iclose >= baud_table[i]) { |
| 436 | /* For the case input == output don't set IBAUD bits |
| 437 | if the user didn't do so */ |
Alan Cox | 5f519d7 | 2007-10-16 23:30:07 -0700 | [diff] [blame] | 438 | if (ofound == i && !ibinput) |
| 439 | ifound = i; |
| 440 | #ifdef IBSHIFT |
| 441 | else { |
| 442 | ifound = i; |
Alan Cox | 78137e3 | 2007-02-10 01:45:57 -0800 | [diff] [blame] | 443 | termios->c_cflag |= (baud_bits[i] << IBSHIFT); |
Alan Cox | 5f519d7 | 2007-10-16 23:30:07 -0700 | [diff] [blame] | 444 | } |
| 445 | #endif |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 446 | } |
Jiri Slaby | 6804396 | 2007-07-15 23:40:18 -0700 | [diff] [blame] | 447 | } while (++i < n_baud_table); |
Alan Cox | 5f519d7 | 2007-10-16 23:30:07 -0700 | [diff] [blame] | 448 | |
| 449 | /* |
| 450 | * If we found no match then use BOTHER if provided or warn |
| 451 | * the user their platform maintainer needs to wake up if not. |
| 452 | */ |
| 453 | #ifdef BOTHER |
Alan Cox | 78137e3 | 2007-02-10 01:45:57 -0800 | [diff] [blame] | 454 | if (ofound == -1) |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 455 | termios->c_cflag |= BOTHER; |
Alan Cox | 78137e3 | 2007-02-10 01:45:57 -0800 | [diff] [blame] | 456 | /* Set exact input bits only if the input and output differ or the |
| 457 | user already did */ |
Jiri Slaby | 6804396 | 2007-07-15 23:40:18 -0700 | [diff] [blame] | 458 | if (ifound == -1 && (ibaud != obaud || ibinput)) |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 459 | termios->c_cflag |= (BOTHER << IBSHIFT); |
Alan Cox | 5f519d7 | 2007-10-16 23:30:07 -0700 | [diff] [blame] | 460 | #else |
Peter Hurley | 89222e6 | 2015-11-08 13:01:18 -0500 | [diff] [blame] | 461 | if (ifound == -1 || ofound == -1) |
| 462 | pr_warn_once("tty: Unable to return correct speed data as your architecture needs updating.\n"); |
Alan Cox | 5f519d7 | 2007-10-16 23:30:07 -0700 | [diff] [blame] | 463 | #endif |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 464 | } |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 465 | EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate); |
| 466 | |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 467 | /** |
| 468 | * tty_encode_baud_rate - set baud rate of the tty |
| 469 | * @ibaud: input baud rate |
| 470 | * @obad: output baud rate |
| 471 | * |
| 472 | * Update the current termios data for the tty with the new speed |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 473 | * settings. The caller must hold the termios_rwsem for the tty in |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 474 | * question. |
| 475 | */ |
| 476 | |
Alan Cox | 5f519d7 | 2007-10-16 23:30:07 -0700 | [diff] [blame] | 477 | void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, speed_t obaud) |
| 478 | { |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 479 | tty_termios_encode_baud_rate(&tty->termios, ibaud, obaud); |
Alan Cox | 5f519d7 | 2007-10-16 23:30:07 -0700 | [diff] [blame] | 480 | } |
| 481 | EXPORT_SYMBOL_GPL(tty_encode_baud_rate); |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 482 | |
| 483 | /** |
Alan Cox | 5f519d7 | 2007-10-16 23:30:07 -0700 | [diff] [blame] | 484 | * tty_termios_copy_hw - copy hardware settings |
| 485 | * @new: New termios |
| 486 | * @old: Old termios |
| 487 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 488 | * Propagate the hardware specific terminal setting bits from |
Alan Cox | 5f519d7 | 2007-10-16 23:30:07 -0700 | [diff] [blame] | 489 | * the old termios structure to the new one. This is used in cases |
| 490 | * where the hardware does not support reconfiguration or as a helper |
| 491 | * in some cases where only minimal reconfiguration is supported |
| 492 | */ |
| 493 | |
| 494 | void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old) |
| 495 | { |
| 496 | /* The bits a dumb device handles in software. Smart devices need |
| 497 | to always provide a set_termios method */ |
| 498 | new->c_cflag &= HUPCL | CREAD | CLOCAL; |
| 499 | new->c_cflag |= old->c_cflag & ~(HUPCL | CREAD | CLOCAL); |
| 500 | new->c_ispeed = old->c_ispeed; |
| 501 | new->c_ospeed = old->c_ospeed; |
| 502 | } |
Alan Cox | 5f519d7 | 2007-10-16 23:30:07 -0700 | [diff] [blame] | 503 | EXPORT_SYMBOL(tty_termios_copy_hw); |
| 504 | |
| 505 | /** |
Alan Cox | bf5e583 | 2008-01-08 14:55:51 +0000 | [diff] [blame] | 506 | * tty_termios_hw_change - check for setting change |
| 507 | * @a: termios |
| 508 | * @b: termios to compare |
| 509 | * |
| 510 | * Check if any of the bits that affect a dumb device have changed |
| 511 | * between the two termios structures, or a speed change is needed. |
| 512 | */ |
| 513 | |
| 514 | int tty_termios_hw_change(struct ktermios *a, struct ktermios *b) |
| 515 | { |
| 516 | if (a->c_ispeed != b->c_ispeed || a->c_ospeed != b->c_ospeed) |
| 517 | return 1; |
| 518 | if ((a->c_cflag ^ b->c_cflag) & ~(HUPCL | CREAD | CLOCAL)) |
| 519 | return 1; |
| 520 | return 0; |
| 521 | } |
| 522 | EXPORT_SYMBOL(tty_termios_hw_change); |
| 523 | |
| 524 | /** |
Alan Cox | 8d075b1 | 2011-02-14 16:27:53 +0000 | [diff] [blame] | 525 | * tty_set_termios - update termios values |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 526 | * @tty: tty to update |
| 527 | * @new_termios: desired new value |
| 528 | * |
Peter Hurley | dbfcd85 | 2014-10-16 15:33:23 -0400 | [diff] [blame] | 529 | * Perform updates to the termios values set on this terminal. |
Peter Hurley | 6460fbb | 2014-10-16 15:33:22 -0400 | [diff] [blame] | 530 | * A master pty's termios should never be set. |
| 531 | * |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 532 | * Locking: termios_rwsem |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 533 | */ |
| 534 | |
Frederic Danis | b00f5c2 | 2015-04-10 15:13:05 +0200 | [diff] [blame] | 535 | int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | { |
Alan Cox | 978e595 | 2008-04-30 00:53:59 -0700 | [diff] [blame] | 537 | struct ktermios old_termios; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | struct tty_ldisc *ld; |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 539 | |
Peter Hurley | 6460fbb | 2014-10-16 15:33:22 -0400 | [diff] [blame] | 540 | WARN_ON(tty->driver->type == TTY_DRIVER_TYPE_PTY && |
| 541 | tty->driver->subtype == PTY_TYPE_MASTER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | /* |
| 543 | * Perform the actual termios internal changes under lock. |
| 544 | */ |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
| 547 | /* FIXME: we need to decide on some locking/ordering semantics |
| 548 | for the set_termios notification eventually */ |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 549 | down_write(&tty->termios_rwsem); |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 550 | old_termios = tty->termios; |
| 551 | tty->termios = *new_termios; |
Peter Hurley | d97ba9c | 2015-11-08 13:01:16 -0500 | [diff] [blame] | 552 | unset_locked_termios(tty, &old_termios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 554 | if (tty->ops->set_termios) |
Peter Hurley | c961bfb | 2014-11-05 12:26:25 -0500 | [diff] [blame] | 555 | tty->ops->set_termios(tty, &old_termios); |
Alan Cox | 5f519d7 | 2007-10-16 23:30:07 -0700 | [diff] [blame] | 556 | else |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 557 | tty_termios_copy_hw(&tty->termios, &old_termios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
| 559 | ld = tty_ldisc_ref(tty); |
| 560 | if (ld != NULL) { |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 561 | if (ld->ops->set_termios) |
Peter Hurley | c961bfb | 2014-11-05 12:26:25 -0500 | [diff] [blame] | 562 | ld->ops->set_termios(tty, &old_termios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | tty_ldisc_deref(ld); |
| 564 | } |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 565 | up_write(&tty->termios_rwsem); |
Alan Cox | 8d075b1 | 2011-02-14 16:27:53 +0000 | [diff] [blame] | 566 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | } |
Frederic Danis | b00f5c2 | 2015-04-10 15:13:05 +0200 | [diff] [blame] | 568 | EXPORT_SYMBOL_GPL(tty_set_termios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 570 | /** |
| 571 | * set_termios - set termios values for a tty |
| 572 | * @tty: terminal device |
| 573 | * @arg: user data |
| 574 | * @opt: option information |
| 575 | * |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 576 | * Helper function to prepare termios data and run necessary other |
Alan Cox | 8d075b1 | 2011-02-14 16:27:53 +0000 | [diff] [blame] | 577 | * functions before using tty_set_termios to do the actual changes. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 578 | * |
| 579 | * Locking: |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 580 | * Called functions take ldisc and termios_rwsem locks |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 581 | */ |
| 582 | |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 583 | static int set_termios(struct tty_struct *tty, void __user *arg, int opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | { |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 585 | struct ktermios tmp_termios; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | struct tty_ldisc *ld; |
| 587 | int retval = tty_check_change(tty); |
| 588 | |
| 589 | if (retval) |
| 590 | return retval; |
| 591 | |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 592 | down_read(&tty->termios_rwsem); |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 593 | tmp_termios = tty->termios; |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 594 | up_read(&tty->termios_rwsem); |
Alan Cox | 64bb6c5 | 2006-12-08 02:38:47 -0800 | [diff] [blame] | 595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | if (opt & TERMIOS_TERMIO) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | if (user_termio_to_kernel_termios(&tmp_termios, |
| 598 | (struct termio __user *)arg)) |
| 599 | return -EFAULT; |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 600 | #ifdef TCGETS2 |
| 601 | } else if (opt & TERMIOS_OLD) { |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 602 | if (user_termios_to_kernel_termios_1(&tmp_termios, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | (struct termios __user *)arg)) |
| 604 | return -EFAULT; |
Alan Cox | 64bb6c5 | 2006-12-08 02:38:47 -0800 | [diff] [blame] | 605 | } else { |
| 606 | if (user_termios_to_kernel_termios(&tmp_termios, |
| 607 | (struct termios2 __user *)arg)) |
| 608 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | } |
Alan Cox | 64bb6c5 | 2006-12-08 02:38:47 -0800 | [diff] [blame] | 610 | #else |
| 611 | } else if (user_termios_to_kernel_termios(&tmp_termios, |
| 612 | (struct termios __user *)arg)) |
| 613 | return -EFAULT; |
| 614 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 616 | /* If old style Bfoo values are used then load c_ispeed/c_ospeed |
| 617 | * with the real speed so its unconditionally usable */ |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 618 | tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios); |
| 619 | tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios); |
| 620 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | ld = tty_ldisc_ref(tty); |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | if (ld != NULL) { |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 624 | if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer) |
| 625 | ld->ops->flush_buffer(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | tty_ldisc_deref(ld); |
| 627 | } |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | if (opt & TERMIOS_WAIT) { |
| 630 | tty_wait_until_sent(tty, 0); |
| 631 | if (signal_pending(current)) |
Oleg Nesterov | 183d95c | 2013-01-29 20:07:41 +0100 | [diff] [blame] | 632 | return -ERESTARTSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Alan Cox | 8d075b1 | 2011-02-14 16:27:53 +0000 | [diff] [blame] | 635 | tty_set_termios(tty, &tmp_termios); |
Alan Cox | 5f519d7 | 2007-10-16 23:30:07 -0700 | [diff] [blame] | 636 | |
| 637 | /* FIXME: Arguably if tmp_termios == tty->termios AND the |
| 638 | actual requested termios was not tmp_termios then we may |
| 639 | want to return an error as no user requested change has |
| 640 | succeeded */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | return 0; |
| 642 | } |
| 643 | |
Alan Cox | 26a2e20 | 2009-06-11 14:03:13 +0100 | [diff] [blame] | 644 | static void copy_termios(struct tty_struct *tty, struct ktermios *kterm) |
| 645 | { |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 646 | down_read(&tty->termios_rwsem); |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 647 | *kterm = tty->termios; |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 648 | up_read(&tty->termios_rwsem); |
Alan Cox | 26a2e20 | 2009-06-11 14:03:13 +0100 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm) |
| 652 | { |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 653 | down_read(&tty->termios_rwsem); |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 654 | *kterm = tty->termios_locked; |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 655 | up_read(&tty->termios_rwsem); |
Alan Cox | 26a2e20 | 2009-06-11 14:03:13 +0100 | [diff] [blame] | 656 | } |
| 657 | |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 658 | static int get_termio(struct tty_struct *tty, struct termio __user *termio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | { |
Alan Cox | 26a2e20 | 2009-06-11 14:03:13 +0100 | [diff] [blame] | 660 | struct ktermios kterm; |
| 661 | copy_termios(tty, &kterm); |
| 662 | if (kernel_termios_to_user_termio(termio, &kterm)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | return -EFAULT; |
| 664 | return 0; |
| 665 | } |
| 666 | |
Alan Cox | 1d65b4a | 2008-10-13 10:38:18 +0100 | [diff] [blame] | 667 | |
| 668 | #ifdef TCGETX |
| 669 | |
| 670 | /** |
| 671 | * set_termiox - set termiox fields if possible |
| 672 | * @tty: terminal |
| 673 | * @arg: termiox structure from user |
| 674 | * @opt: option flags for ioctl type |
| 675 | * |
| 676 | * Implement the device calling points for the SYS5 termiox ioctl |
| 677 | * interface in Linux |
| 678 | */ |
| 679 | |
| 680 | static int set_termiox(struct tty_struct *tty, void __user *arg, int opt) |
| 681 | { |
| 682 | struct termiox tnew; |
| 683 | struct tty_ldisc *ld; |
| 684 | |
| 685 | if (tty->termiox == NULL) |
| 686 | return -EINVAL; |
| 687 | if (copy_from_user(&tnew, arg, sizeof(struct termiox))) |
| 688 | return -EFAULT; |
| 689 | |
| 690 | ld = tty_ldisc_ref(tty); |
| 691 | if (ld != NULL) { |
| 692 | if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer) |
| 693 | ld->ops->flush_buffer(tty); |
| 694 | tty_ldisc_deref(ld); |
| 695 | } |
| 696 | if (opt & TERMIOS_WAIT) { |
| 697 | tty_wait_until_sent(tty, 0); |
| 698 | if (signal_pending(current)) |
Oleg Nesterov | 183d95c | 2013-01-29 20:07:41 +0100 | [diff] [blame] | 699 | return -ERESTARTSYS; |
Alan Cox | 1d65b4a | 2008-10-13 10:38:18 +0100 | [diff] [blame] | 700 | } |
| 701 | |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 702 | down_write(&tty->termios_rwsem); |
Alan Cox | 1d65b4a | 2008-10-13 10:38:18 +0100 | [diff] [blame] | 703 | if (tty->ops->set_termiox) |
| 704 | tty->ops->set_termiox(tty, &tnew); |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 705 | up_write(&tty->termios_rwsem); |
Alan Cox | 1d65b4a | 2008-10-13 10:38:18 +0100 | [diff] [blame] | 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | #endif |
| 710 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
| 712 | #ifdef TIOCGETP |
| 713 | /* |
| 714 | * These are deprecated, but there is limited support.. |
| 715 | * |
| 716 | * The "sg_flags" translation is a joke.. |
| 717 | */ |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 718 | static int get_sgflags(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | { |
| 720 | int flags = 0; |
| 721 | |
Peter Hurley | 9db276f | 2016-01-10 20:36:15 -0800 | [diff] [blame] | 722 | if (!L_ICANON(tty)) { |
| 723 | if (L_ISIG(tty)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | flags |= 0x02; /* cbreak */ |
| 725 | else |
| 726 | flags |= 0x20; /* raw */ |
| 727 | } |
Peter Hurley | 9db276f | 2016-01-10 20:36:15 -0800 | [diff] [blame] | 728 | if (L_ECHO(tty)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | flags |= 0x08; /* echo */ |
Peter Hurley | 9db276f | 2016-01-10 20:36:15 -0800 | [diff] [blame] | 730 | if (O_OPOST(tty)) |
| 731 | if (O_ONLCR(tty)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | flags |= 0x10; /* crmod */ |
| 733 | return flags; |
| 734 | } |
| 735 | |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 736 | static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | { |
| 738 | struct sgttyb tmp; |
| 739 | |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 740 | down_read(&tty->termios_rwsem); |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 741 | tmp.sg_ispeed = tty->termios.c_ispeed; |
| 742 | tmp.sg_ospeed = tty->termios.c_ospeed; |
| 743 | tmp.sg_erase = tty->termios.c_cc[VERASE]; |
| 744 | tmp.sg_kill = tty->termios.c_cc[VKILL]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | tmp.sg_flags = get_sgflags(tty); |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 746 | up_read(&tty->termios_rwsem); |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0; |
| 749 | } |
| 750 | |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 751 | static void set_sgflags(struct ktermios *termios, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | { |
Alan Cox | 9833fac | 2012-07-17 17:05:40 +0100 | [diff] [blame] | 753 | termios->c_iflag = ICRNL | IXON; |
| 754 | termios->c_oflag = 0; |
| 755 | termios->c_lflag = ISIG | ICANON; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | if (flags & 0x02) { /* cbreak */ |
Alan Cox | 9833fac | 2012-07-17 17:05:40 +0100 | [diff] [blame] | 757 | termios->c_iflag = 0; |
| 758 | termios->c_lflag &= ~ICANON; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | } |
| 760 | if (flags & 0x08) { /* echo */ |
Alan Cox | 9833fac | 2012-07-17 17:05:40 +0100 | [diff] [blame] | 761 | termios->c_lflag |= ECHO | ECHOE | ECHOK | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | ECHOCTL | ECHOKE | IEXTEN; |
| 763 | } |
| 764 | if (flags & 0x10) { /* crmod */ |
Alan Cox | 9833fac | 2012-07-17 17:05:40 +0100 | [diff] [blame] | 765 | termios->c_oflag |= OPOST | ONLCR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | } |
| 767 | if (flags & 0x20) { /* raw */ |
Alan Cox | 9833fac | 2012-07-17 17:05:40 +0100 | [diff] [blame] | 768 | termios->c_iflag = 0; |
| 769 | termios->c_lflag &= ~(ISIG | ICANON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | } |
Alan Cox | 9833fac | 2012-07-17 17:05:40 +0100 | [diff] [blame] | 771 | if (!(termios->c_lflag & ICANON)) { |
| 772 | termios->c_cc[VMIN] = 1; |
| 773 | termios->c_cc[VTIME] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 777 | /** |
| 778 | * set_sgttyb - set legacy terminal values |
| 779 | * @tty: tty structure |
| 780 | * @sgttyb: pointer to old style terminal structure |
| 781 | * |
| 782 | * Updates a terminal from the legacy BSD style terminal information |
| 783 | * structure. |
| 784 | * |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 785 | * Locking: termios_rwsem |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 786 | */ |
| 787 | |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 788 | static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | { |
| 790 | int retval; |
| 791 | struct sgttyb tmp; |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 792 | struct ktermios termios; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | |
| 794 | retval = tty_check_change(tty); |
| 795 | if (retval) |
| 796 | return retval; |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | if (copy_from_user(&tmp, sgttyb, sizeof(tmp))) |
| 799 | return -EFAULT; |
| 800 | |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 801 | down_write(&tty->termios_rwsem); |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 802 | termios = tty->termios; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | termios.c_cc[VERASE] = tmp.sg_erase; |
| 804 | termios.c_cc[VKILL] = tmp.sg_kill; |
| 805 | set_sgflags(&termios, tmp.sg_flags); |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 806 | /* Try and encode into Bfoo format */ |
| 807 | #ifdef BOTHER |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 808 | tty_termios_encode_baud_rate(&termios, termios.c_ispeed, |
| 809 | termios.c_ospeed); |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 810 | #endif |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 811 | up_write(&tty->termios_rwsem); |
Alan Cox | 8d075b1 | 2011-02-14 16:27:53 +0000 | [diff] [blame] | 812 | tty_set_termios(tty, &termios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | return 0; |
| 814 | } |
| 815 | #endif |
| 816 | |
| 817 | #ifdef TIOCGETC |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 818 | static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | { |
| 820 | struct tchars tmp; |
| 821 | |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 822 | down_read(&tty->termios_rwsem); |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 823 | tmp.t_intrc = tty->termios.c_cc[VINTR]; |
| 824 | tmp.t_quitc = tty->termios.c_cc[VQUIT]; |
| 825 | tmp.t_startc = tty->termios.c_cc[VSTART]; |
| 826 | tmp.t_stopc = tty->termios.c_cc[VSTOP]; |
| 827 | tmp.t_eofc = tty->termios.c_cc[VEOF]; |
| 828 | tmp.t_brkc = tty->termios.c_cc[VEOL2]; /* what is brkc anyway? */ |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 829 | up_read(&tty->termios_rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0; |
| 831 | } |
| 832 | |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 833 | static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | { |
| 835 | struct tchars tmp; |
| 836 | |
| 837 | if (copy_from_user(&tmp, tchars, sizeof(tmp))) |
| 838 | return -EFAULT; |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 839 | down_write(&tty->termios_rwsem); |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 840 | tty->termios.c_cc[VINTR] = tmp.t_intrc; |
| 841 | tty->termios.c_cc[VQUIT] = tmp.t_quitc; |
| 842 | tty->termios.c_cc[VSTART] = tmp.t_startc; |
| 843 | tty->termios.c_cc[VSTOP] = tmp.t_stopc; |
| 844 | tty->termios.c_cc[VEOF] = tmp.t_eofc; |
| 845 | tty->termios.c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */ |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 846 | up_write(&tty->termios_rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | return 0; |
| 848 | } |
| 849 | #endif |
| 850 | |
| 851 | #ifdef TIOCGLTC |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 852 | static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | { |
| 854 | struct ltchars tmp; |
| 855 | |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 856 | down_read(&tty->termios_rwsem); |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 857 | tmp.t_suspc = tty->termios.c_cc[VSUSP]; |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 858 | /* what is dsuspc anyway? */ |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 859 | tmp.t_dsuspc = tty->termios.c_cc[VSUSP]; |
| 860 | tmp.t_rprntc = tty->termios.c_cc[VREPRINT]; |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 861 | /* what is flushc anyway? */ |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 862 | tmp.t_flushc = tty->termios.c_cc[VEOL2]; |
| 863 | tmp.t_werasc = tty->termios.c_cc[VWERASE]; |
| 864 | tmp.t_lnextc = tty->termios.c_cc[VLNEXT]; |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 865 | up_read(&tty->termios_rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0; |
| 867 | } |
| 868 | |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 869 | static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | { |
| 871 | struct ltchars tmp; |
| 872 | |
| 873 | if (copy_from_user(&tmp, ltchars, sizeof(tmp))) |
| 874 | return -EFAULT; |
| 875 | |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 876 | down_write(&tty->termios_rwsem); |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 877 | tty->termios.c_cc[VSUSP] = tmp.t_suspc; |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 878 | /* what is dsuspc anyway? */ |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 879 | tty->termios.c_cc[VEOL2] = tmp.t_dsuspc; |
| 880 | tty->termios.c_cc[VREPRINT] = tmp.t_rprntc; |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 881 | /* what is flushc anyway? */ |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 882 | tty->termios.c_cc[VEOL2] = tmp.t_flushc; |
| 883 | tty->termios.c_cc[VWERASE] = tmp.t_werasc; |
| 884 | tty->termios.c_cc[VLNEXT] = tmp.t_lnextc; |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 885 | up_write(&tty->termios_rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | return 0; |
| 887 | } |
| 888 | #endif |
| 889 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 890 | /** |
Alan Cox | 1c2630c | 2008-04-30 00:53:34 -0700 | [diff] [blame] | 891 | * tty_change_softcar - carrier change ioctl helper |
| 892 | * @tty: tty to update |
| 893 | * @arg: enable/disable CLOCAL |
| 894 | * |
| 895 | * Perform a change to the CLOCAL state and call into the driver |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 896 | * layer to make it visible. All done with the termios rwsem |
Alan Cox | 1c2630c | 2008-04-30 00:53:34 -0700 | [diff] [blame] | 897 | */ |
| 898 | |
| 899 | static int tty_change_softcar(struct tty_struct *tty, int arg) |
| 900 | { |
| 901 | int ret = 0; |
| 902 | int bit = arg ? CLOCAL : 0; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 903 | struct ktermios old; |
Alan Cox | 1c2630c | 2008-04-30 00:53:34 -0700 | [diff] [blame] | 904 | |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 905 | down_write(&tty->termios_rwsem); |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 906 | old = tty->termios; |
| 907 | tty->termios.c_cflag &= ~CLOCAL; |
| 908 | tty->termios.c_cflag |= bit; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 909 | if (tty->ops->set_termios) |
| 910 | tty->ops->set_termios(tty, &old); |
Peter Hurley | 9db276f | 2016-01-10 20:36:15 -0800 | [diff] [blame] | 911 | if (C_CLOCAL(tty) != bit) |
Alan Cox | 1c2630c | 2008-04-30 00:53:34 -0700 | [diff] [blame] | 912 | ret = -EINVAL; |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 913 | up_write(&tty->termios_rwsem); |
Alan Cox | 1c2630c | 2008-04-30 00:53:34 -0700 | [diff] [blame] | 914 | return ret; |
| 915 | } |
| 916 | |
| 917 | /** |
Alan Cox | 0fc00e2 | 2007-11-07 01:24:56 -0800 | [diff] [blame] | 918 | * tty_mode_ioctl - mode related ioctls |
| 919 | * @tty: tty for the ioctl |
| 920 | * @file: file pointer for the tty |
| 921 | * @cmd: command |
| 922 | * @arg: ioctl argument |
| 923 | * |
| 924 | * Perform non line discipline specific mode control ioctls. This |
| 925 | * is designed to be called by line disciplines to ensure they provide |
| 926 | * consistent mode setting. |
| 927 | */ |
| 928 | |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 929 | int tty_mode_ioctl(struct tty_struct *tty, struct file *file, |
Alan Cox | 0fc00e2 | 2007-11-07 01:24:56 -0800 | [diff] [blame] | 930 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | { |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 932 | struct tty_struct *real_tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | void __user *p = (void __user *)arg; |
Alan Cox | 8f52002 | 2008-10-13 10:38:46 +0100 | [diff] [blame] | 934 | int ret = 0; |
Alan Cox | 26a2e20 | 2009-06-11 14:03:13 +0100 | [diff] [blame] | 935 | struct ktermios kterm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | |
Alan Cox | 8d075b1 | 2011-02-14 16:27:53 +0000 | [diff] [blame] | 937 | BUG_ON(file == NULL); |
| 938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && |
| 940 | tty->driver->subtype == PTY_TYPE_MASTER) |
| 941 | real_tty = tty->link; |
| 942 | else |
| 943 | real_tty = tty; |
| 944 | |
| 945 | switch (cmd) { |
| 946 | #ifdef TIOCGETP |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 947 | case TIOCGETP: |
| 948 | return get_sgttyb(real_tty, (struct sgttyb __user *) arg); |
| 949 | case TIOCSETP: |
| 950 | case TIOCSETN: |
| 951 | return set_sgttyb(real_tty, (struct sgttyb __user *) arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | #endif |
| 953 | #ifdef TIOCGETC |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 954 | case TIOCGETC: |
| 955 | return get_tchars(real_tty, p); |
| 956 | case TIOCSETC: |
| 957 | return set_tchars(real_tty, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | #endif |
| 959 | #ifdef TIOCGLTC |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 960 | case TIOCGLTC: |
| 961 | return get_ltchars(real_tty, p); |
| 962 | case TIOCSLTC: |
| 963 | return set_ltchars(real_tty, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | #endif |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 965 | case TCSETSF: |
| 966 | return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD); |
| 967 | case TCSETSW: |
| 968 | return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD); |
| 969 | case TCSETS: |
| 970 | return set_termios(real_tty, p, TERMIOS_OLD); |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 971 | #ifndef TCGETS2 |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 972 | case TCGETS: |
Alan Cox | 26a2e20 | 2009-06-11 14:03:13 +0100 | [diff] [blame] | 973 | copy_termios(real_tty, &kterm); |
| 974 | if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm)) |
Alan Cox | 8f52002 | 2008-10-13 10:38:46 +0100 | [diff] [blame] | 975 | ret = -EFAULT; |
Alan Cox | 8f52002 | 2008-10-13 10:38:46 +0100 | [diff] [blame] | 976 | return ret; |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 977 | #else |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 978 | case TCGETS: |
Alan Cox | 26a2e20 | 2009-06-11 14:03:13 +0100 | [diff] [blame] | 979 | copy_termios(real_tty, &kterm); |
| 980 | if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm)) |
Alan Cox | 8f52002 | 2008-10-13 10:38:46 +0100 | [diff] [blame] | 981 | ret = -EFAULT; |
Alan Cox | 8f52002 | 2008-10-13 10:38:46 +0100 | [diff] [blame] | 982 | return ret; |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 983 | case TCGETS2: |
Alan Cox | 26a2e20 | 2009-06-11 14:03:13 +0100 | [diff] [blame] | 984 | copy_termios(real_tty, &kterm); |
| 985 | if (kernel_termios_to_user_termios((struct termios2 __user *)arg, &kterm)) |
Alan Cox | 8f52002 | 2008-10-13 10:38:46 +0100 | [diff] [blame] | 986 | ret = -EFAULT; |
Alan Cox | 8f52002 | 2008-10-13 10:38:46 +0100 | [diff] [blame] | 987 | return ret; |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 988 | case TCSETSF2: |
| 989 | return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT); |
| 990 | case TCSETSW2: |
| 991 | return set_termios(real_tty, p, TERMIOS_WAIT); |
| 992 | case TCSETS2: |
| 993 | return set_termios(real_tty, p, 0); |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 994 | #endif |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 995 | case TCGETA: |
| 996 | return get_termio(real_tty, p); |
| 997 | case TCSETAF: |
| 998 | return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO); |
| 999 | case TCSETAW: |
| 1000 | return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO); |
| 1001 | case TCSETA: |
| 1002 | return set_termios(real_tty, p, TERMIOS_TERMIO); |
Alan Cox | 0fc00e2 | 2007-11-07 01:24:56 -0800 | [diff] [blame] | 1003 | #ifndef TCGETS2 |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 1004 | case TIOCGLCKTRMIOS: |
Alan Cox | 26a2e20 | 2009-06-11 14:03:13 +0100 | [diff] [blame] | 1005 | copy_termios_locked(real_tty, &kterm); |
| 1006 | if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm)) |
Alan Cox | 8f52002 | 2008-10-13 10:38:46 +0100 | [diff] [blame] | 1007 | ret = -EFAULT; |
Alan Cox | 8f52002 | 2008-10-13 10:38:46 +0100 | [diff] [blame] | 1008 | return ret; |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 1009 | case TIOCSLCKTRMIOS: |
| 1010 | if (!capable(CAP_SYS_ADMIN)) |
| 1011 | return -EPERM; |
Alan Cox | 26a2e20 | 2009-06-11 14:03:13 +0100 | [diff] [blame] | 1012 | copy_termios_locked(real_tty, &kterm); |
| 1013 | if (user_termios_to_kernel_termios(&kterm, |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 1014 | (struct termios __user *) arg)) |
Alan Cox | 26a2e20 | 2009-06-11 14:03:13 +0100 | [diff] [blame] | 1015 | return -EFAULT; |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 1016 | down_write(&real_tty->termios_rwsem); |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 1017 | real_tty->termios_locked = kterm; |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 1018 | up_write(&real_tty->termios_rwsem); |
Alan Cox | 26a2e20 | 2009-06-11 14:03:13 +0100 | [diff] [blame] | 1019 | return 0; |
Alan Cox | 0fc00e2 | 2007-11-07 01:24:56 -0800 | [diff] [blame] | 1020 | #else |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 1021 | case TIOCGLCKTRMIOS: |
Alan Cox | 26a2e20 | 2009-06-11 14:03:13 +0100 | [diff] [blame] | 1022 | copy_termios_locked(real_tty, &kterm); |
| 1023 | if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm)) |
Alan Cox | 8f52002 | 2008-10-13 10:38:46 +0100 | [diff] [blame] | 1024 | ret = -EFAULT; |
Alan Cox | 8f52002 | 2008-10-13 10:38:46 +0100 | [diff] [blame] | 1025 | return ret; |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 1026 | case TIOCSLCKTRMIOS: |
| 1027 | if (!capable(CAP_SYS_ADMIN)) |
Alan Cox | 26a2e20 | 2009-06-11 14:03:13 +0100 | [diff] [blame] | 1028 | return -EPERM; |
| 1029 | copy_termios_locked(real_tty, &kterm); |
| 1030 | if (user_termios_to_kernel_termios_1(&kterm, |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 1031 | (struct termios __user *) arg)) |
Alan Cox | 26a2e20 | 2009-06-11 14:03:13 +0100 | [diff] [blame] | 1032 | return -EFAULT; |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 1033 | down_write(&real_tty->termios_rwsem); |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 1034 | real_tty->termios_locked = kterm; |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 1035 | up_write(&real_tty->termios_rwsem); |
Alan Cox | 8f52002 | 2008-10-13 10:38:46 +0100 | [diff] [blame] | 1036 | return ret; |
Alan Cox | 0fc00e2 | 2007-11-07 01:24:56 -0800 | [diff] [blame] | 1037 | #endif |
Alan Cox | 1d65b4a | 2008-10-13 10:38:18 +0100 | [diff] [blame] | 1038 | #ifdef TCGETX |
Mike Frysinger | 5dca607 | 2009-06-16 17:01:02 +0100 | [diff] [blame] | 1039 | case TCGETX: { |
| 1040 | struct termiox ktermx; |
Alan Cox | 1d65b4a | 2008-10-13 10:38:18 +0100 | [diff] [blame] | 1041 | if (real_tty->termiox == NULL) |
| 1042 | return -EINVAL; |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 1043 | down_read(&real_tty->termios_rwsem); |
Alan Cox | 26a2e20 | 2009-06-11 14:03:13 +0100 | [diff] [blame] | 1044 | memcpy(&ktermx, real_tty->termiox, sizeof(struct termiox)); |
Peter Hurley | 6a1c068 | 2013-06-15 09:14:23 -0400 | [diff] [blame] | 1045 | up_read(&real_tty->termios_rwsem); |
Alan Cox | 26a2e20 | 2009-06-11 14:03:13 +0100 | [diff] [blame] | 1046 | if (copy_to_user(p, &ktermx, sizeof(struct termiox))) |
| 1047 | ret = -EFAULT; |
Alan Cox | 8f52002 | 2008-10-13 10:38:46 +0100 | [diff] [blame] | 1048 | return ret; |
Mike Frysinger | 5dca607 | 2009-06-16 17:01:02 +0100 | [diff] [blame] | 1049 | } |
Alan Cox | 1d65b4a | 2008-10-13 10:38:18 +0100 | [diff] [blame] | 1050 | case TCSETX: |
| 1051 | return set_termiox(real_tty, p, 0); |
| 1052 | case TCSETXW: |
| 1053 | return set_termiox(real_tty, p, TERMIOS_WAIT); |
| 1054 | case TCSETXF: |
| 1055 | return set_termiox(real_tty, p, TERMIOS_FLUSH); |
| 1056 | #endif |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 1057 | case TIOCGSOFTCAR: |
Alan Cox | 26a2e20 | 2009-06-11 14:03:13 +0100 | [diff] [blame] | 1058 | copy_termios(real_tty, &kterm); |
| 1059 | ret = put_user((kterm.c_cflag & CLOCAL) ? 1 : 0, |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 1060 | (int __user *)arg); |
Alan Cox | 8f52002 | 2008-10-13 10:38:46 +0100 | [diff] [blame] | 1061 | return ret; |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 1062 | case TIOCSSOFTCAR: |
| 1063 | if (get_user(arg, (unsigned int __user *) arg)) |
| 1064 | return -EFAULT; |
Alan Cox | f753f32 | 2008-08-26 19:52:47 +0100 | [diff] [blame] | 1065 | return tty_change_softcar(real_tty, arg); |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 1066 | default: |
| 1067 | return -ENOIOCTLCMD; |
Alan Cox | 0fc00e2 | 2007-11-07 01:24:56 -0800 | [diff] [blame] | 1068 | } |
| 1069 | } |
Alan Cox | 0fc00e2 | 2007-11-07 01:24:56 -0800 | [diff] [blame] | 1070 | EXPORT_SYMBOL_GPL(tty_mode_ioctl); |
| 1071 | |
Alan Cox | 0fc00e2 | 2007-11-07 01:24:56 -0800 | [diff] [blame] | 1072 | |
Peter Hurley | e7f3880 | 2013-03-11 16:44:45 -0400 | [diff] [blame] | 1073 | /* Caller guarantees ldisc reference is held */ |
| 1074 | static int __tty_perform_flush(struct tty_struct *tty, unsigned long arg) |
| 1075 | { |
| 1076 | struct tty_ldisc *ld = tty->ldisc; |
| 1077 | |
Alan Cox | 0fc00e2 | 2007-11-07 01:24:56 -0800 | [diff] [blame] | 1078 | switch (arg) { |
| 1079 | case TCIFLUSH: |
Ilya Zykov | a1bf958 | 2013-01-16 13:07:50 +0400 | [diff] [blame] | 1080 | if (ld && ld->ops->flush_buffer) { |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1081 | ld->ops->flush_buffer(tty); |
Ilya Zykov | a1bf958 | 2013-01-16 13:07:50 +0400 | [diff] [blame] | 1082 | tty_unthrottle(tty); |
| 1083 | } |
Alan Cox | 0fc00e2 | 2007-11-07 01:24:56 -0800 | [diff] [blame] | 1084 | break; |
| 1085 | case TCIOFLUSH: |
Ilya Zykov | a1bf958 | 2013-01-16 13:07:50 +0400 | [diff] [blame] | 1086 | if (ld && ld->ops->flush_buffer) { |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1087 | ld->ops->flush_buffer(tty); |
Ilya Zykov | a1bf958 | 2013-01-16 13:07:50 +0400 | [diff] [blame] | 1088 | tty_unthrottle(tty); |
| 1089 | } |
Alan Cox | 0fc00e2 | 2007-11-07 01:24:56 -0800 | [diff] [blame] | 1090 | /* fall through */ |
| 1091 | case TCOFLUSH: |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1092 | tty_driver_flush_buffer(tty); |
Alan Cox | 0fc00e2 | 2007-11-07 01:24:56 -0800 | [diff] [blame] | 1093 | break; |
| 1094 | default: |
Alan Cox | 0fc00e2 | 2007-11-07 01:24:56 -0800 | [diff] [blame] | 1095 | return -EINVAL; |
| 1096 | } |
Alan Cox | 0fc00e2 | 2007-11-07 01:24:56 -0800 | [diff] [blame] | 1097 | return 0; |
| 1098 | } |
Peter Hurley | e7f3880 | 2013-03-11 16:44:45 -0400 | [diff] [blame] | 1099 | |
| 1100 | int tty_perform_flush(struct tty_struct *tty, unsigned long arg) |
| 1101 | { |
| 1102 | struct tty_ldisc *ld; |
| 1103 | int retval = tty_check_change(tty); |
| 1104 | if (retval) |
| 1105 | return retval; |
| 1106 | |
| 1107 | ld = tty_ldisc_ref_wait(tty); |
| 1108 | retval = __tty_perform_flush(tty, arg); |
| 1109 | if (ld) |
| 1110 | tty_ldisc_deref(ld); |
| 1111 | return retval; |
| 1112 | } |
Alan Cox | 0fc00e2 | 2007-11-07 01:24:56 -0800 | [diff] [blame] | 1113 | EXPORT_SYMBOL_GPL(tty_perform_flush); |
| 1114 | |
Alan Cox | 47afa7a | 2008-10-13 10:44:17 +0100 | [diff] [blame] | 1115 | int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file, |
Alan Cox | 0fc00e2 | 2007-11-07 01:24:56 -0800 | [diff] [blame] | 1116 | unsigned int cmd, unsigned long arg) |
| 1117 | { |
Alan Cox | 0fc00e2 | 2007-11-07 01:24:56 -0800 | [diff] [blame] | 1118 | int retval; |
| 1119 | |
Alan Cox | 0fc00e2 | 2007-11-07 01:24:56 -0800 | [diff] [blame] | 1120 | switch (cmd) { |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 1121 | case TCXONC: |
| 1122 | retval = tty_check_change(tty); |
| 1123 | if (retval) |
| 1124 | return retval; |
| 1125 | switch (arg) { |
| 1126 | case TCOOFF: |
Peter Hurley | c545b66 | 2014-09-10 15:06:33 -0400 | [diff] [blame] | 1127 | spin_lock_irq(&tty->flow_lock); |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 1128 | if (!tty->flow_stopped) { |
| 1129 | tty->flow_stopped = 1; |
Peter Hurley | c545b66 | 2014-09-10 15:06:33 -0400 | [diff] [blame] | 1130 | __stop_tty(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | } |
Peter Hurley | c545b66 | 2014-09-10 15:06:33 -0400 | [diff] [blame] | 1132 | spin_unlock_irq(&tty->flow_lock); |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 1133 | break; |
| 1134 | case TCOON: |
Peter Hurley | c545b66 | 2014-09-10 15:06:33 -0400 | [diff] [blame] | 1135 | spin_lock_irq(&tty->flow_lock); |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 1136 | if (tty->flow_stopped) { |
| 1137 | tty->flow_stopped = 0; |
Peter Hurley | c545b66 | 2014-09-10 15:06:33 -0400 | [diff] [blame] | 1138 | __start_tty(tty); |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 1139 | } |
Peter Hurley | c545b66 | 2014-09-10 15:06:33 -0400 | [diff] [blame] | 1140 | spin_unlock_irq(&tty->flow_lock); |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 1141 | break; |
| 1142 | case TCIOFF: |
| 1143 | if (STOP_CHAR(tty) != __DISABLED_CHAR) |
Peter Hurley | c274f6e | 2014-09-10 15:06:35 -0400 | [diff] [blame] | 1144 | retval = tty_send_xchar(tty, STOP_CHAR(tty)); |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 1145 | break; |
| 1146 | case TCION: |
| 1147 | if (START_CHAR(tty) != __DISABLED_CHAR) |
Peter Hurley | c274f6e | 2014-09-10 15:06:35 -0400 | [diff] [blame] | 1148 | retval = tty_send_xchar(tty, START_CHAR(tty)); |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 1149 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | default: |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 1151 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | } |
Peter Hurley | c274f6e | 2014-09-10 15:06:35 -0400 | [diff] [blame] | 1153 | return retval; |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 1154 | case TCFLSH: |
Peter Hurley | 5cec7bf | 2013-09-25 20:13:04 -0400 | [diff] [blame] | 1155 | retval = tty_check_change(tty); |
| 1156 | if (retval) |
| 1157 | return retval; |
Peter Hurley | e7f3880 | 2013-03-11 16:44:45 -0400 | [diff] [blame] | 1158 | return __tty_perform_flush(tty, arg); |
Alan Cox | 355d95a | 2008-02-08 04:18:48 -0800 | [diff] [blame] | 1159 | default: |
| 1160 | /* Try the mode commands */ |
| 1161 | return tty_mode_ioctl(tty, file, cmd, arg); |
| 1162 | } |
| 1163 | } |
Alan Cox | 47afa7a | 2008-10-13 10:44:17 +0100 | [diff] [blame] | 1164 | EXPORT_SYMBOL(n_tty_ioctl_helper); |
Thomas Meyer | 8193c42 | 2011-10-05 23:13:13 +0200 | [diff] [blame] | 1165 | |
| 1166 | #ifdef CONFIG_COMPAT |
| 1167 | long n_tty_compat_ioctl_helper(struct tty_struct *tty, struct file *file, |
| 1168 | unsigned int cmd, unsigned long arg) |
| 1169 | { |
| 1170 | switch (cmd) { |
| 1171 | case TIOCGLCKTRMIOS: |
| 1172 | case TIOCSLCKTRMIOS: |
| 1173 | return tty_mode_ioctl(tty, file, cmd, (unsigned long) compat_ptr(arg)); |
| 1174 | default: |
| 1175 | return -ENOIOCTLCMD; |
| 1176 | } |
| 1177 | } |
| 1178 | EXPORT_SYMBOL(n_tty_compat_ioctl_helper); |
| 1179 | #endif |
| 1180 | |