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