blob: 132d452578bbd948639c6247735fb58965fa32c9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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 Ven5785c952006-09-29 02:00:43 -070021#include <linux/mutex.h>
Thomas Meyer8193c422011-10-05 23:13:13 +020022#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/io.h>
25#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#undef TTY_DEBUG_WAIT_UNTIL_SENT
28
29#undef DEBUG
30
31/*
32 * Internal flag options for termios setting behavior
33 */
34#define TERMIOS_FLUSH 1
35#define TERMIOS_WAIT 2
36#define TERMIOS_TERMIO 4
Alan Coxedc6afc2006-12-08 02:38:44 -080037#define TERMIOS_OLD 8
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Alan Coxaf9b8972006-08-27 01:24:01 -070039
Alan Coxd81ed102008-10-13 10:41:42 +010040/**
41 * tty_chars_in_buffer - characters pending
42 * @tty: terminal
43 *
44 * Return the number of bytes of data in the device private
45 * output queue. If no private method is supplied there is assumed
46 * to be no queue on the device.
47 */
48
Alan Coxf34d7a52008-04-30 00:54:13 -070049int tty_chars_in_buffer(struct tty_struct *tty)
50{
51 if (tty->ops->chars_in_buffer)
52 return tty->ops->chars_in_buffer(tty);
53 else
54 return 0;
55}
Alan Coxf34d7a52008-04-30 00:54:13 -070056EXPORT_SYMBOL(tty_chars_in_buffer);
57
Alan Coxd81ed102008-10-13 10:41:42 +010058/**
59 * tty_write_room - write queue space
60 * @tty: terminal
61 *
62 * Return the number of bytes that can be queued to this device
63 * at the present time. The result should be treated as a guarantee
64 * and the driver cannot offer a value it later shrinks by more than
65 * the number of bytes written. If no method is provided 2K is always
66 * returned and data may be lost as there will be no flow control.
67 */
68
Alan Coxf34d7a52008-04-30 00:54:13 -070069int tty_write_room(struct tty_struct *tty)
70{
71 if (tty->ops->write_room)
72 return tty->ops->write_room(tty);
73 return 2048;
74}
Alan Coxf34d7a52008-04-30 00:54:13 -070075EXPORT_SYMBOL(tty_write_room);
76
Alan Coxd81ed102008-10-13 10:41:42 +010077/**
78 * tty_driver_flush_buffer - discard internal buffer
79 * @tty: terminal
80 *
81 * Discard the internal output buffer for this device. If no method
82 * is provided then either the buffer cannot be hardware flushed or
83 * there is no buffer driver side.
84 */
Alan Coxf34d7a52008-04-30 00:54:13 -070085void tty_driver_flush_buffer(struct tty_struct *tty)
86{
87 if (tty->ops->flush_buffer)
88 tty->ops->flush_buffer(tty);
89}
Alan Coxf34d7a52008-04-30 00:54:13 -070090EXPORT_SYMBOL(tty_driver_flush_buffer);
91
Alan Coxd81ed102008-10-13 10:41:42 +010092/**
93 * tty_throttle - flow control
94 * @tty: terminal
95 *
96 * Indicate that a tty should stop transmitting data down the stack.
Alan Cox38db8972009-06-11 12:44:17 +010097 * Takes the termios mutex to protect against parallel throttle/unthrottle
98 * and also to ensure the driver can consistently reference its own
99 * termios data at this point when implementing software flow control.
Alan Coxd81ed102008-10-13 10:41:42 +0100100 */
101
Alan Cox39c2e602008-04-30 00:54:18 -0700102void tty_throttle(struct tty_struct *tty)
103{
Alan Cox38db8972009-06-11 12:44:17 +0100104 mutex_lock(&tty->termios_mutex);
Alan Cox39c2e602008-04-30 00:54:18 -0700105 /* check TTY_THROTTLED first so it indicates our state */
106 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
107 tty->ops->throttle)
108 tty->ops->throttle(tty);
Peter Hurley70bc1262013-03-06 08:20:52 -0500109 tty->flow_change = 0;
Alan Cox38db8972009-06-11 12:44:17 +0100110 mutex_unlock(&tty->termios_mutex);
Alan Cox39c2e602008-04-30 00:54:18 -0700111}
112EXPORT_SYMBOL(tty_throttle);
113
Alan Coxd81ed102008-10-13 10:41:42 +0100114/**
115 * tty_unthrottle - flow control
116 * @tty: terminal
117 *
118 * Indicate that a tty may continue transmitting data down the stack.
Alan Cox38db8972009-06-11 12:44:17 +0100119 * Takes the termios mutex to protect against parallel throttle/unthrottle
120 * and also to ensure the driver can consistently reference its own
121 * termios data at this point when implementing software flow control.
122 *
123 * Drivers should however remember that the stack can issue a throttle,
124 * then change flow control method, then unthrottle.
Alan Coxd81ed102008-10-13 10:41:42 +0100125 */
126
Alan Cox39c2e602008-04-30 00:54:18 -0700127void tty_unthrottle(struct tty_struct *tty)
128{
Alan Cox38db8972009-06-11 12:44:17 +0100129 mutex_lock(&tty->termios_mutex);
Alan Cox39c2e602008-04-30 00:54:18 -0700130 if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
131 tty->ops->unthrottle)
132 tty->ops->unthrottle(tty);
Peter Hurley70bc1262013-03-06 08:20:52 -0500133 tty->flow_change = 0;
Alan Cox38db8972009-06-11 12:44:17 +0100134 mutex_unlock(&tty->termios_mutex);
Alan Cox39c2e602008-04-30 00:54:18 -0700135}
136EXPORT_SYMBOL(tty_unthrottle);
Alan Coxf34d7a52008-04-30 00:54:13 -0700137
Alan Coxaf9b8972006-08-27 01:24:01 -0700138/**
Peter Hurley70bc1262013-03-06 08:20:52 -0500139 * tty_throttle_safe - flow control
140 * @tty: terminal
141 *
142 * Similar to tty_throttle() but will only attempt throttle
143 * if tty->flow_change is TTY_THROTTLE_SAFE. Prevents an accidental
144 * throttle due to race conditions when throttling is conditional
145 * on factors evaluated prior to throttling.
146 *
147 * Returns 0 if tty is throttled (or was already throttled)
148 */
149
150int tty_throttle_safe(struct tty_struct *tty)
151{
152 int ret = 0;
153
154 mutex_lock(&tty->termios_mutex);
155 if (!test_bit(TTY_THROTTLED, &tty->flags)) {
156 if (tty->flow_change != TTY_THROTTLE_SAFE)
157 ret = 1;
158 else {
159 __set_bit(TTY_THROTTLED, &tty->flags);
160 if (tty->ops->throttle)
161 tty->ops->throttle(tty);
162 }
163 }
164 mutex_unlock(&tty->termios_mutex);
165
166 return ret;
167}
168
169/**
170 * tty_unthrottle_safe - flow control
171 * @tty: terminal
172 *
173 * Similar to tty_unthrottle() but will only attempt unthrottle
174 * if tty->flow_change is TTY_UNTHROTTLE_SAFE. Prevents an accidental
175 * unthrottle due to race conditions when unthrottling is conditional
176 * on factors evaluated prior to unthrottling.
177 *
178 * Returns 0 if tty is unthrottled (or was already unthrottled)
179 */
180
181int tty_unthrottle_safe(struct tty_struct *tty)
182{
183 int ret = 0;
184
185 mutex_lock(&tty->termios_mutex);
186 if (test_bit(TTY_THROTTLED, &tty->flags)) {
187 if (tty->flow_change != TTY_UNTHROTTLE_SAFE)
188 ret = 1;
189 else {
190 __clear_bit(TTY_THROTTLED, &tty->flags);
191 if (tty->ops->unthrottle)
192 tty->ops->unthrottle(tty);
193 }
194 }
195 mutex_unlock(&tty->termios_mutex);
196
197 return ret;
198}
199
200/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700201 * tty_wait_until_sent - wait for I/O to finish
202 * @tty: tty we are waiting for
203 * @timeout: how long we will wait
204 *
205 * Wait for characters pending in a tty driver to hit the wire, or
206 * for a timeout to occur (eg due to flow control)
207 *
208 * Locking: none
209 */
210
Alan Cox355d95a12008-02-08 04:18:48 -0800211void tty_wait_until_sent(struct tty_struct *tty, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213#ifdef TTY_DEBUG_WAIT_UNTIL_SENT
214 char buf[64];
Alan Cox355d95a12008-02-08 04:18:48 -0800215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 printk(KERN_DEBUG "%s wait until sent...\n", tty_name(tty, buf));
217#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (!timeout)
219 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby5a52bd42007-07-15 23:40:18 -0700220 if (wait_event_interruptible_timeout(tty->write_wait,
Alan Coxf34d7a52008-04-30 00:54:13 -0700221 !tty_chars_in_buffer(tty), timeout) >= 0) {
222 if (tty->ops->wait_until_sent)
223 tty->ops->wait_until_sent(tty, timeout);
Alan Cox0ee9cbb2008-04-30 00:53:32 -0700224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226EXPORT_SYMBOL(tty_wait_until_sent);
227
Alan Coxd81ed102008-10-13 10:41:42 +0100228
229/*
230 * Termios Helper Methods
231 */
232
Alan Coxedc6afc2006-12-08 02:38:44 -0800233static void unset_locked_termios(struct ktermios *termios,
234 struct ktermios *old,
235 struct ktermios *locked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 int i;
Alan Cox355d95a12008-02-08 04:18:48 -0800238
239#define NOSET_MASK(x, y, z) (x = ((x) & ~(z)) | ((y) & (z)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 if (!locked) {
242 printk(KERN_WARNING "Warning?!? termios_locked is NULL.\n");
243 return;
244 }
245
246 NOSET_MASK(termios->c_iflag, old->c_iflag, locked->c_iflag);
247 NOSET_MASK(termios->c_oflag, old->c_oflag, locked->c_oflag);
248 NOSET_MASK(termios->c_cflag, old->c_cflag, locked->c_cflag);
249 NOSET_MASK(termios->c_lflag, old->c_lflag, locked->c_lflag);
250 termios->c_line = locked->c_line ? old->c_line : termios->c_line;
Alan Cox355d95a12008-02-08 04:18:48 -0800251 for (i = 0; i < NCCS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 termios->c_cc[i] = locked->c_cc[i] ?
253 old->c_cc[i] : termios->c_cc[i];
Alan Coxedc6afc2006-12-08 02:38:44 -0800254 /* FIXME: What should we do for i/ospeed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Alan Coxedc6afc2006-12-08 02:38:44 -0800257/*
258 * Routine which returns the baud rate of the tty
259 *
260 * Note that the baud_table needs to be kept in sync with the
261 * include/asm/termbits.h file.
262 */
263static const speed_t baud_table[] = {
264 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
265 9600, 19200, 38400, 57600, 115200, 230400, 460800,
266#ifdef __sparc__
267 76800, 153600, 307200, 614400, 921600
268#else
269 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
270 2500000, 3000000, 3500000, 4000000
271#endif
272};
273
274#ifndef __sparc__
275static const tcflag_t baud_bits[] = {
276 B0, B50, B75, B110, B134, B150, B200, B300, B600,
277 B1200, B1800, B2400, B4800, B9600, B19200, B38400,
278 B57600, B115200, B230400, B460800, B500000, B576000,
279 B921600, B1000000, B1152000, B1500000, B2000000, B2500000,
280 B3000000, B3500000, B4000000
281};
282#else
283static const tcflag_t baud_bits[] = {
284 B0, B50, B75, B110, B134, B150, B200, B300, B600,
285 B1200, B1800, B2400, B4800, B9600, B19200, B38400,
286 B57600, B115200, B230400, B460800, B76800, B153600,
287 B307200, B614400, B921600
288};
289#endif
290
291static int n_baud_table = ARRAY_SIZE(baud_table);
292
293/**
294 * tty_termios_baud_rate
295 * @termios: termios structure
296 *
297 * Convert termios baud rate data into a speed. This should be called
298 * with the termios lock held if this termios is a terminal termios
299 * structure. May change the termios data. Device drivers can call this
300 * function but should use ->c_[io]speed directly as they are updated.
301 *
302 * Locking: none
303 */
304
305speed_t tty_termios_baud_rate(struct ktermios *termios)
306{
307 unsigned int cbaud;
308
309 cbaud = termios->c_cflag & CBAUD;
310
311#ifdef BOTHER
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300312 /* Magic token for arbitrary speed via c_ispeed/c_ospeed */
Alan Coxedc6afc2006-12-08 02:38:44 -0800313 if (cbaud == BOTHER)
314 return termios->c_ospeed;
315#endif
316 if (cbaud & CBAUDEX) {
317 cbaud &= ~CBAUDEX;
318
319 if (cbaud < 1 || cbaud + 15 > n_baud_table)
320 termios->c_cflag &= ~CBAUDEX;
321 else
322 cbaud += 15;
323 }
324 return baud_table[cbaud];
325}
Alan Coxedc6afc2006-12-08 02:38:44 -0800326EXPORT_SYMBOL(tty_termios_baud_rate);
327
328/**
329 * tty_termios_input_baud_rate
330 * @termios: termios structure
331 *
332 * Convert termios baud rate data into a speed. This should be called
333 * with the termios lock held if this termios is a terminal termios
334 * structure. May change the termios data. Device drivers can call this
335 * function but should use ->c_[io]speed directly as they are updated.
336 *
337 * Locking: none
338 */
339
340speed_t tty_termios_input_baud_rate(struct ktermios *termios)
341{
342#ifdef IBSHIFT
343 unsigned int cbaud = (termios->c_cflag >> IBSHIFT) & CBAUD;
344
345 if (cbaud == B0)
346 return tty_termios_baud_rate(termios);
347
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300348 /* Magic token for arbitrary speed via c_ispeed*/
Alan Coxedc6afc2006-12-08 02:38:44 -0800349 if (cbaud == BOTHER)
350 return termios->c_ispeed;
351
352 if (cbaud & CBAUDEX) {
353 cbaud &= ~CBAUDEX;
354
355 if (cbaud < 1 || cbaud + 15 > n_baud_table)
356 termios->c_cflag &= ~(CBAUDEX << IBSHIFT);
357 else
358 cbaud += 15;
359 }
360 return baud_table[cbaud];
361#else
362 return tty_termios_baud_rate(termios);
363#endif
364}
Alan Coxedc6afc2006-12-08 02:38:44 -0800365EXPORT_SYMBOL(tty_termios_input_baud_rate);
366
Alan Coxedc6afc2006-12-08 02:38:44 -0800367/**
368 * tty_termios_encode_baud_rate
Alan Cox78137e32007-02-10 01:45:57 -0800369 * @termios: ktermios structure holding user requested state
Alan Coxedc6afc2006-12-08 02:38:44 -0800370 * @ispeed: input speed
371 * @ospeed: output speed
372 *
373 * Encode the speeds set into the passed termios structure. This is
Uwe Kleine-Königf98e5b82011-04-12 09:59:29 +0200374 * used as a library helper for drivers so that they can report back
Alan Coxedc6afc2006-12-08 02:38:44 -0800375 * the actual speed selected when it differs from the speed requested
376 *
Alan Cox78137e32007-02-10 01:45:57 -0800377 * For maximal back compatibility with legacy SYS5/POSIX *nix behaviour
378 * we need to carefully set the bits when the user does not get the
379 * desired speed. We allow small margins and preserve as much of possible
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400380 * of the input intent to keep compatibility.
Alan Coxedc6afc2006-12-08 02:38:44 -0800381 *
382 * Locking: Caller should hold termios lock. This is already held
383 * when calling this function from the driver termios handler.
Alan Cox5f519d72007-10-16 23:30:07 -0700384 *
385 * The ifdefs deal with platforms whose owners have yet to update them
386 * and will all go away once this is done.
Alan Coxedc6afc2006-12-08 02:38:44 -0800387 */
388
Maciej W. Rozycki75e8b712007-10-18 03:04:35 -0700389void tty_termios_encode_baud_rate(struct ktermios *termios,
390 speed_t ibaud, speed_t obaud)
Alan Coxedc6afc2006-12-08 02:38:44 -0800391{
392 int i = 0;
Alan Cox78137e32007-02-10 01:45:57 -0800393 int ifound = -1, ofound = -1;
394 int iclose = ibaud/50, oclose = obaud/50;
395 int ibinput = 0;
Alan Coxedc6afc2006-12-08 02:38:44 -0800396
Alan Cox5f519d72007-10-16 23:30:07 -0700397 if (obaud == 0) /* CD dropped */
398 ibaud = 0; /* Clear ibaud to be sure */
399
Alan Coxedc6afc2006-12-08 02:38:44 -0800400 termios->c_ispeed = ibaud;
401 termios->c_ospeed = obaud;
402
Alan Cox5f519d72007-10-16 23:30:07 -0700403#ifdef BOTHER
Alan Cox78137e32007-02-10 01:45:57 -0800404 /* If the user asked for a precise weird speed give a precise weird
405 answer. If they asked for a Bfoo speed they many have problems
406 digesting non-exact replies so fuzz a bit */
407
408 if ((termios->c_cflag & CBAUD) == BOTHER)
409 oclose = 0;
410 if (((termios->c_cflag >> IBSHIFT) & CBAUD) == BOTHER)
411 iclose = 0;
412 if ((termios->c_cflag >> IBSHIFT) & CBAUD)
413 ibinput = 1; /* An input speed was specified */
Alan Cox5f519d72007-10-16 23:30:07 -0700414#endif
Alan Coxedc6afc2006-12-08 02:38:44 -0800415 termios->c_cflag &= ~CBAUD;
Alan Coxedc6afc2006-12-08 02:38:44 -0800416
Alan Cox5f519d72007-10-16 23:30:07 -0700417 /*
418 * Our goal is to find a close match to the standard baud rate
419 * returned. Walk the baud rate table and if we get a very close
420 * match then report back the speed as a POSIX Bxxxx value by
421 * preference
422 */
423
Alan Coxedc6afc2006-12-08 02:38:44 -0800424 do {
Maciej W. Rozycki75e8b712007-10-18 03:04:35 -0700425 if (obaud - oclose <= baud_table[i] &&
426 obaud + oclose >= baud_table[i]) {
Alan Coxedc6afc2006-12-08 02:38:44 -0800427 termios->c_cflag |= baud_bits[i];
Alan Cox78137e32007-02-10 01:45:57 -0800428 ofound = i;
Alan Coxedc6afc2006-12-08 02:38:44 -0800429 }
Maciej W. Rozycki75e8b712007-10-18 03:04:35 -0700430 if (ibaud - iclose <= baud_table[i] &&
431 ibaud + iclose >= baud_table[i]) {
432 /* For the case input == output don't set IBAUD bits
433 if the user didn't do so */
Alan Cox5f519d72007-10-16 23:30:07 -0700434 if (ofound == i && !ibinput)
435 ifound = i;
436#ifdef IBSHIFT
437 else {
438 ifound = i;
Alan Cox78137e32007-02-10 01:45:57 -0800439 termios->c_cflag |= (baud_bits[i] << IBSHIFT);
Alan Cox5f519d72007-10-16 23:30:07 -0700440 }
441#endif
Alan Coxedc6afc2006-12-08 02:38:44 -0800442 }
Jiri Slaby68043962007-07-15 23:40:18 -0700443 } while (++i < n_baud_table);
Alan Cox5f519d72007-10-16 23:30:07 -0700444
445 /*
446 * If we found no match then use BOTHER if provided or warn
447 * the user their platform maintainer needs to wake up if not.
448 */
449#ifdef BOTHER
Alan Cox78137e32007-02-10 01:45:57 -0800450 if (ofound == -1)
Alan Coxedc6afc2006-12-08 02:38:44 -0800451 termios->c_cflag |= BOTHER;
Alan Cox78137e32007-02-10 01:45:57 -0800452 /* Set exact input bits only if the input and output differ or the
453 user already did */
Jiri Slaby68043962007-07-15 23:40:18 -0700454 if (ifound == -1 && (ibaud != obaud || ibinput))
Alan Coxedc6afc2006-12-08 02:38:44 -0800455 termios->c_cflag |= (BOTHER << IBSHIFT);
Alan Cox5f519d72007-10-16 23:30:07 -0700456#else
457 if (ifound == -1 || ofound == -1) {
Marcin Slusarz9074d962009-08-09 21:54:03 +0200458 printk_once(KERN_WARNING "tty: Unable to return correct "
Alan Cox5f519d72007-10-16 23:30:07 -0700459 "speed data as your architecture needs updating.\n");
460 }
461#endif
Alan Coxedc6afc2006-12-08 02:38:44 -0800462}
Alan Coxedc6afc2006-12-08 02:38:44 -0800463EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate);
464
Alan Coxd81ed102008-10-13 10:41:42 +0100465/**
466 * tty_encode_baud_rate - set baud rate of the tty
467 * @ibaud: input baud rate
468 * @obad: output baud rate
469 *
470 * Update the current termios data for the tty with the new speed
471 * settings. The caller must hold the termios_mutex for the tty in
472 * question.
473 */
474
Alan Cox5f519d72007-10-16 23:30:07 -0700475void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, speed_t obaud)
476{
Alan Coxadc8d742012-07-14 15:31:47 +0100477 tty_termios_encode_baud_rate(&tty->termios, ibaud, obaud);
Alan Cox5f519d72007-10-16 23:30:07 -0700478}
479EXPORT_SYMBOL_GPL(tty_encode_baud_rate);
Alan Coxedc6afc2006-12-08 02:38:44 -0800480
481/**
482 * tty_get_baud_rate - get tty bit rates
483 * @tty: tty to query
484 *
485 * Returns the baud rate as an integer for this terminal. The
486 * termios lock must be held by the caller and the terminal bit
487 * flags may be updated.
488 *
489 * Locking: none
490 */
491
492speed_t tty_get_baud_rate(struct tty_struct *tty)
493{
Alan Coxadc8d742012-07-14 15:31:47 +0100494 speed_t baud = tty_termios_baud_rate(&tty->termios);
Alan Coxedc6afc2006-12-08 02:38:44 -0800495
496 if (baud == 38400 && tty->alt_speed) {
497 if (!tty->warned) {
498 printk(KERN_WARNING "Use of setserial/setrocket to "
499 "set SPD_* flags is deprecated\n");
500 tty->warned = 1;
501 }
502 baud = tty->alt_speed;
503 }
504
505 return baud;
506}
Alan Coxedc6afc2006-12-08 02:38:44 -0800507EXPORT_SYMBOL(tty_get_baud_rate);
508
Alan Coxaf9b8972006-08-27 01:24:01 -0700509/**
Alan Cox5f519d72007-10-16 23:30:07 -0700510 * tty_termios_copy_hw - copy hardware settings
511 * @new: New termios
512 * @old: Old termios
513 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300514 * Propagate the hardware specific terminal setting bits from
Alan Cox5f519d72007-10-16 23:30:07 -0700515 * the old termios structure to the new one. This is used in cases
516 * where the hardware does not support reconfiguration or as a helper
517 * in some cases where only minimal reconfiguration is supported
518 */
519
520void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old)
521{
522 /* The bits a dumb device handles in software. Smart devices need
523 to always provide a set_termios method */
524 new->c_cflag &= HUPCL | CREAD | CLOCAL;
525 new->c_cflag |= old->c_cflag & ~(HUPCL | CREAD | CLOCAL);
526 new->c_ispeed = old->c_ispeed;
527 new->c_ospeed = old->c_ospeed;
528}
Alan Cox5f519d72007-10-16 23:30:07 -0700529EXPORT_SYMBOL(tty_termios_copy_hw);
530
531/**
Alan Coxbf5e5832008-01-08 14:55:51 +0000532 * tty_termios_hw_change - check for setting change
533 * @a: termios
534 * @b: termios to compare
535 *
536 * Check if any of the bits that affect a dumb device have changed
537 * between the two termios structures, or a speed change is needed.
538 */
539
540int tty_termios_hw_change(struct ktermios *a, struct ktermios *b)
541{
542 if (a->c_ispeed != b->c_ispeed || a->c_ospeed != b->c_ospeed)
543 return 1;
544 if ((a->c_cflag ^ b->c_cflag) & ~(HUPCL | CREAD | CLOCAL))
545 return 1;
546 return 0;
547}
548EXPORT_SYMBOL(tty_termios_hw_change);
549
550/**
Alan Cox8d075b12011-02-14 16:27:53 +0000551 * tty_set_termios - update termios values
Alan Coxaf9b8972006-08-27 01:24:01 -0700552 * @tty: tty to update
553 * @new_termios: desired new value
554 *
555 * Perform updates to the termios values set on this terminal. There
556 * is a bit of layering violation here with n_tty in terms of the
557 * internal knowledge of this function.
558 *
Alan Coxd81ed102008-10-13 10:41:42 +0100559 * Locking: termios_mutex
Alan Coxaf9b8972006-08-27 01:24:01 -0700560 */
561
Alan Cox8d075b12011-02-14 16:27:53 +0000562int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Alan Cox978e5952008-04-30 00:53:59 -0700564 struct ktermios old_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct tty_ldisc *ld;
Alan Cox04f378b2008-04-30 00:53:29 -0700566 unsigned long flags;
Alan Cox355d95a12008-02-08 04:18:48 -0800567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /*
569 * Perform the actual termios internal changes under lock.
570 */
Alan Cox355d95a12008-02-08 04:18:48 -0800571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 /* FIXME: we need to decide on some locking/ordering semantics
574 for the set_termios notification eventually */
Arjan van de Ven5785c952006-09-29 02:00:43 -0700575 mutex_lock(&tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +0100576 old_termios = tty->termios;
577 tty->termios = *new_termios;
578 unset_locked_termios(&tty->termios, &old_termios, &tty->termios_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 /* See if packet mode change of state. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (tty->link && tty->link->packet) {
hyc@symas.com26df6d12010-06-22 10:14:49 -0700582 int extproc = (old_termios.c_lflag & EXTPROC) |
Alan Coxadc8d742012-07-14 15:31:47 +0100583 (tty->termios.c_lflag & EXTPROC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 int old_flow = ((old_termios.c_iflag & IXON) &&
585 (old_termios.c_cc[VSTOP] == '\023') &&
586 (old_termios.c_cc[VSTART] == '\021'));
587 int new_flow = (I_IXON(tty) &&
588 STOP_CHAR(tty) == '\023' &&
589 START_CHAR(tty) == '\021');
hyc@symas.com26df6d12010-06-22 10:14:49 -0700590 if ((old_flow != new_flow) || extproc) {
Alan Cox04f378b2008-04-30 00:53:29 -0700591 spin_lock_irqsave(&tty->ctrl_lock, flags);
hyc@symas.com26df6d12010-06-22 10:14:49 -0700592 if (old_flow != new_flow) {
593 tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
594 if (new_flow)
595 tty->ctrl_status |= TIOCPKT_DOSTOP;
596 else
597 tty->ctrl_status |= TIOCPKT_NOSTOP;
598 }
599 if (extproc)
600 tty->ctrl_status |= TIOCPKT_IOCTL;
Alan Cox04f378b2008-04-30 00:53:29 -0700601 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 wake_up_interruptible(&tty->link->read_wait);
603 }
604 }
Alan Cox355d95a12008-02-08 04:18:48 -0800605
Alan Coxf34d7a52008-04-30 00:54:13 -0700606 if (tty->ops->set_termios)
607 (*tty->ops->set_termios)(tty, &old_termios);
Alan Cox5f519d72007-10-16 23:30:07 -0700608 else
Alan Coxadc8d742012-07-14 15:31:47 +0100609 tty_termios_copy_hw(&tty->termios, &old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 ld = tty_ldisc_ref(tty);
612 if (ld != NULL) {
Alan Coxa352def2008-07-16 21:53:12 +0100613 if (ld->ops->set_termios)
614 (ld->ops->set_termios)(tty, &old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 tty_ldisc_deref(ld);
616 }
Arjan van de Ven5785c952006-09-29 02:00:43 -0700617 mutex_unlock(&tty->termios_mutex);
Alan Cox8d075b12011-02-14 16:27:53 +0000618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
Alan Cox8d075b12011-02-14 16:27:53 +0000620EXPORT_SYMBOL_GPL(tty_set_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Alan Coxaf9b8972006-08-27 01:24:01 -0700622/**
623 * set_termios - set termios values for a tty
624 * @tty: terminal device
625 * @arg: user data
626 * @opt: option information
627 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200628 * Helper function to prepare termios data and run necessary other
Alan Cox8d075b12011-02-14 16:27:53 +0000629 * functions before using tty_set_termios to do the actual changes.
Alan Coxaf9b8972006-08-27 01:24:01 -0700630 *
631 * Locking:
Alan Coxd81ed102008-10-13 10:41:42 +0100632 * Called functions take ldisc and termios_mutex locks
Alan Coxaf9b8972006-08-27 01:24:01 -0700633 */
634
Alan Cox355d95a12008-02-08 04:18:48 -0800635static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Alan Coxedc6afc2006-12-08 02:38:44 -0800637 struct ktermios tmp_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 struct tty_ldisc *ld;
639 int retval = tty_check_change(tty);
640
641 if (retval)
642 return retval;
643
Alan Cox978e5952008-04-30 00:53:59 -0700644 mutex_lock(&tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +0100645 tmp_termios = tty->termios;
Alan Cox978e5952008-04-30 00:53:59 -0700646 mutex_unlock(&tty->termios_mutex);
Alan Cox64bb6c52006-12-08 02:38:47 -0800647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (opt & TERMIOS_TERMIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (user_termio_to_kernel_termios(&tmp_termios,
650 (struct termio __user *)arg))
651 return -EFAULT;
Alan Coxedc6afc2006-12-08 02:38:44 -0800652#ifdef TCGETS2
653 } else if (opt & TERMIOS_OLD) {
Alan Coxedc6afc2006-12-08 02:38:44 -0800654 if (user_termios_to_kernel_termios_1(&tmp_termios,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 (struct termios __user *)arg))
656 return -EFAULT;
Alan Cox64bb6c52006-12-08 02:38:47 -0800657 } else {
658 if (user_termios_to_kernel_termios(&tmp_termios,
659 (struct termios2 __user *)arg))
660 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
Alan Cox64bb6c52006-12-08 02:38:47 -0800662#else
663 } else if (user_termios_to_kernel_termios(&tmp_termios,
664 (struct termios __user *)arg))
665 return -EFAULT;
666#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Alan Cox355d95a12008-02-08 04:18:48 -0800668 /* If old style Bfoo values are used then load c_ispeed/c_ospeed
669 * with the real speed so its unconditionally usable */
Alan Coxedc6afc2006-12-08 02:38:44 -0800670 tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios);
671 tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios);
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 ld = tty_ldisc_ref(tty);
Alan Cox355d95a12008-02-08 04:18:48 -0800674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (ld != NULL) {
Alan Coxa352def2008-07-16 21:53:12 +0100676 if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
677 ld->ops->flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 tty_ldisc_deref(ld);
679 }
Alan Cox355d95a12008-02-08 04:18:48 -0800680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (opt & TERMIOS_WAIT) {
682 tty_wait_until_sent(tty, 0);
683 if (signal_pending(current))
Oleg Nesterov183d95c2013-01-29 20:07:41 +0100684 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686
Alan Cox8d075b12011-02-14 16:27:53 +0000687 tty_set_termios(tty, &tmp_termios);
Alan Cox5f519d72007-10-16 23:30:07 -0700688
689 /* FIXME: Arguably if tmp_termios == tty->termios AND the
690 actual requested termios was not tmp_termios then we may
691 want to return an error as no user requested change has
692 succeeded */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return 0;
694}
695
Alan Cox26a2e202009-06-11 14:03:13 +0100696static void copy_termios(struct tty_struct *tty, struct ktermios *kterm)
697{
698 mutex_lock(&tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +0100699 *kterm = tty->termios;
Alan Cox26a2e202009-06-11 14:03:13 +0100700 mutex_unlock(&tty->termios_mutex);
701}
702
703static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm)
704{
705 mutex_lock(&tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +0100706 *kterm = tty->termios_locked;
Alan Cox26a2e202009-06-11 14:03:13 +0100707 mutex_unlock(&tty->termios_mutex);
708}
709
Alan Cox355d95a12008-02-08 04:18:48 -0800710static int get_termio(struct tty_struct *tty, struct termio __user *termio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Alan Cox26a2e202009-06-11 14:03:13 +0100712 struct ktermios kterm;
713 copy_termios(tty, &kterm);
714 if (kernel_termios_to_user_termio(termio, &kterm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return -EFAULT;
716 return 0;
717}
718
Alan Cox1d65b4a2008-10-13 10:38:18 +0100719
720#ifdef TCGETX
721
722/**
723 * set_termiox - set termiox fields if possible
724 * @tty: terminal
725 * @arg: termiox structure from user
726 * @opt: option flags for ioctl type
727 *
728 * Implement the device calling points for the SYS5 termiox ioctl
729 * interface in Linux
730 */
731
732static int set_termiox(struct tty_struct *tty, void __user *arg, int opt)
733{
734 struct termiox tnew;
735 struct tty_ldisc *ld;
736
737 if (tty->termiox == NULL)
738 return -EINVAL;
739 if (copy_from_user(&tnew, arg, sizeof(struct termiox)))
740 return -EFAULT;
741
742 ld = tty_ldisc_ref(tty);
743 if (ld != NULL) {
744 if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
745 ld->ops->flush_buffer(tty);
746 tty_ldisc_deref(ld);
747 }
748 if (opt & TERMIOS_WAIT) {
749 tty_wait_until_sent(tty, 0);
750 if (signal_pending(current))
Oleg Nesterov183d95c2013-01-29 20:07:41 +0100751 return -ERESTARTSYS;
Alan Cox1d65b4a2008-10-13 10:38:18 +0100752 }
753
754 mutex_lock(&tty->termios_mutex);
755 if (tty->ops->set_termiox)
756 tty->ops->set_termiox(tty, &tnew);
757 mutex_unlock(&tty->termios_mutex);
758 return 0;
759}
760
761#endif
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764#ifdef TIOCGETP
765/*
766 * These are deprecated, but there is limited support..
767 *
768 * The "sg_flags" translation is a joke..
769 */
Alan Cox355d95a12008-02-08 04:18:48 -0800770static int get_sgflags(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 int flags = 0;
773
Alan Coxadc8d742012-07-14 15:31:47 +0100774 if (!(tty->termios.c_lflag & ICANON)) {
775 if (tty->termios.c_lflag & ISIG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 flags |= 0x02; /* cbreak */
777 else
778 flags |= 0x20; /* raw */
779 }
Alan Coxadc8d742012-07-14 15:31:47 +0100780 if (tty->termios.c_lflag & ECHO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 flags |= 0x08; /* echo */
Alan Coxadc8d742012-07-14 15:31:47 +0100782 if (tty->termios.c_oflag & OPOST)
783 if (tty->termios.c_oflag & ONLCR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 flags |= 0x10; /* crmod */
785 return flags;
786}
787
Alan Cox355d95a12008-02-08 04:18:48 -0800788static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 struct sgttyb tmp;
791
Arjan van de Ven5785c952006-09-29 02:00:43 -0700792 mutex_lock(&tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +0100793 tmp.sg_ispeed = tty->termios.c_ispeed;
794 tmp.sg_ospeed = tty->termios.c_ospeed;
795 tmp.sg_erase = tty->termios.c_cc[VERASE];
796 tmp.sg_kill = tty->termios.c_cc[VKILL];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 tmp.sg_flags = get_sgflags(tty);
Arjan van de Ven5785c952006-09-29 02:00:43 -0700798 mutex_unlock(&tty->termios_mutex);
Alan Cox355d95a12008-02-08 04:18:48 -0800799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
801}
802
Alan Cox355d95a12008-02-08 04:18:48 -0800803static void set_sgflags(struct ktermios *termios, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Alan Cox9833fac2012-07-17 17:05:40 +0100805 termios->c_iflag = ICRNL | IXON;
806 termios->c_oflag = 0;
807 termios->c_lflag = ISIG | ICANON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (flags & 0x02) { /* cbreak */
Alan Cox9833fac2012-07-17 17:05:40 +0100809 termios->c_iflag = 0;
810 termios->c_lflag &= ~ICANON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812 if (flags & 0x08) { /* echo */
Alan Cox9833fac2012-07-17 17:05:40 +0100813 termios->c_lflag |= ECHO | ECHOE | ECHOK |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 ECHOCTL | ECHOKE | IEXTEN;
815 }
816 if (flags & 0x10) { /* crmod */
Alan Cox9833fac2012-07-17 17:05:40 +0100817 termios->c_oflag |= OPOST | ONLCR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819 if (flags & 0x20) { /* raw */
Alan Cox9833fac2012-07-17 17:05:40 +0100820 termios->c_iflag = 0;
821 termios->c_lflag &= ~(ISIG | ICANON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
Alan Cox9833fac2012-07-17 17:05:40 +0100823 if (!(termios->c_lflag & ICANON)) {
824 termios->c_cc[VMIN] = 1;
825 termios->c_cc[VTIME] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827}
828
Alan Coxaf9b8972006-08-27 01:24:01 -0700829/**
830 * set_sgttyb - set legacy terminal values
831 * @tty: tty structure
832 * @sgttyb: pointer to old style terminal structure
833 *
834 * Updates a terminal from the legacy BSD style terminal information
835 * structure.
836 *
Alan Coxd81ed102008-10-13 10:41:42 +0100837 * Locking: termios_mutex
Alan Coxaf9b8972006-08-27 01:24:01 -0700838 */
839
Alan Cox355d95a12008-02-08 04:18:48 -0800840static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 int retval;
843 struct sgttyb tmp;
Alan Coxedc6afc2006-12-08 02:38:44 -0800844 struct ktermios termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 retval = tty_check_change(tty);
847 if (retval)
848 return retval;
Alan Cox355d95a12008-02-08 04:18:48 -0800849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
851 return -EFAULT;
852
Arjan van de Ven5785c952006-09-29 02:00:43 -0700853 mutex_lock(&tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +0100854 termios = tty->termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 termios.c_cc[VERASE] = tmp.sg_erase;
856 termios.c_cc[VKILL] = tmp.sg_kill;
857 set_sgflags(&termios, tmp.sg_flags);
Alan Coxedc6afc2006-12-08 02:38:44 -0800858 /* Try and encode into Bfoo format */
859#ifdef BOTHER
Alan Cox355d95a12008-02-08 04:18:48 -0800860 tty_termios_encode_baud_rate(&termios, termios.c_ispeed,
861 termios.c_ospeed);
Alan Coxedc6afc2006-12-08 02:38:44 -0800862#endif
Arjan van de Ven5785c952006-09-29 02:00:43 -0700863 mutex_unlock(&tty->termios_mutex);
Alan Cox8d075b12011-02-14 16:27:53 +0000864 tty_set_termios(tty, &termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return 0;
866}
867#endif
868
869#ifdef TIOCGETC
Alan Cox355d95a12008-02-08 04:18:48 -0800870static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 struct tchars tmp;
873
Alan Cox978e5952008-04-30 00:53:59 -0700874 mutex_lock(&tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +0100875 tmp.t_intrc = tty->termios.c_cc[VINTR];
876 tmp.t_quitc = tty->termios.c_cc[VQUIT];
877 tmp.t_startc = tty->termios.c_cc[VSTART];
878 tmp.t_stopc = tty->termios.c_cc[VSTOP];
879 tmp.t_eofc = tty->termios.c_cc[VEOF];
880 tmp.t_brkc = tty->termios.c_cc[VEOL2]; /* what is brkc anyway? */
Alan Cox978e5952008-04-30 00:53:59 -0700881 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
883}
884
Alan Cox355d95a12008-02-08 04:18:48 -0800885static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 struct tchars tmp;
888
889 if (copy_from_user(&tmp, tchars, sizeof(tmp)))
890 return -EFAULT;
Alan Cox978e5952008-04-30 00:53:59 -0700891 mutex_lock(&tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +0100892 tty->termios.c_cc[VINTR] = tmp.t_intrc;
893 tty->termios.c_cc[VQUIT] = tmp.t_quitc;
894 tty->termios.c_cc[VSTART] = tmp.t_startc;
895 tty->termios.c_cc[VSTOP] = tmp.t_stopc;
896 tty->termios.c_cc[VEOF] = tmp.t_eofc;
897 tty->termios.c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
Alan Cox978e5952008-04-30 00:53:59 -0700898 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return 0;
900}
901#endif
902
903#ifdef TIOCGLTC
Alan Cox355d95a12008-02-08 04:18:48 -0800904static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 struct ltchars tmp;
907
Alan Cox978e5952008-04-30 00:53:59 -0700908 mutex_lock(&tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +0100909 tmp.t_suspc = tty->termios.c_cc[VSUSP];
Alan Cox355d95a12008-02-08 04:18:48 -0800910 /* what is dsuspc anyway? */
Alan Coxadc8d742012-07-14 15:31:47 +0100911 tmp.t_dsuspc = tty->termios.c_cc[VSUSP];
912 tmp.t_rprntc = tty->termios.c_cc[VREPRINT];
Alan Cox355d95a12008-02-08 04:18:48 -0800913 /* what is flushc anyway? */
Alan Coxadc8d742012-07-14 15:31:47 +0100914 tmp.t_flushc = tty->termios.c_cc[VEOL2];
915 tmp.t_werasc = tty->termios.c_cc[VWERASE];
916 tmp.t_lnextc = tty->termios.c_cc[VLNEXT];
Alan Cox978e5952008-04-30 00:53:59 -0700917 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
919}
920
Alan Cox355d95a12008-02-08 04:18:48 -0800921static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 struct ltchars tmp;
924
925 if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
926 return -EFAULT;
927
Alan Cox978e5952008-04-30 00:53:59 -0700928 mutex_lock(&tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +0100929 tty->termios.c_cc[VSUSP] = tmp.t_suspc;
Alan Cox355d95a12008-02-08 04:18:48 -0800930 /* what is dsuspc anyway? */
Alan Coxadc8d742012-07-14 15:31:47 +0100931 tty->termios.c_cc[VEOL2] = tmp.t_dsuspc;
932 tty->termios.c_cc[VREPRINT] = tmp.t_rprntc;
Alan Cox355d95a12008-02-08 04:18:48 -0800933 /* what is flushc anyway? */
Alan Coxadc8d742012-07-14 15:31:47 +0100934 tty->termios.c_cc[VEOL2] = tmp.t_flushc;
935 tty->termios.c_cc[VWERASE] = tmp.t_werasc;
936 tty->termios.c_cc[VLNEXT] = tmp.t_lnextc;
Alan Cox978e5952008-04-30 00:53:59 -0700937 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return 0;
939}
940#endif
941
Alan Coxaf9b8972006-08-27 01:24:01 -0700942/**
943 * send_prio_char - send priority character
944 *
945 * Send a high priority character to the tty even if stopped
946 *
Alan Cox5f412b22006-09-29 02:01:40 -0700947 * Locking: none for xchar method, write ordering for write method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700949
Alan Cox5f412b22006-09-29 02:01:40 -0700950static int send_prio_char(struct tty_struct *tty, char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
952 int was_stopped = tty->stopped;
953
Alan Coxf34d7a52008-04-30 00:54:13 -0700954 if (tty->ops->send_xchar) {
955 tty->ops->send_xchar(tty, ch);
Alan Cox5f412b22006-09-29 02:01:40 -0700956 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
Alan Cox5f412b22006-09-29 02:01:40 -0700958
Alan Cox9c1729d2007-07-15 23:39:43 -0700959 if (tty_write_lock(tty, 0) < 0)
Alan Cox5f412b22006-09-29 02:01:40 -0700960 return -ERESTARTSYS;
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (was_stopped)
963 start_tty(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -0700964 tty->ops->write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (was_stopped)
966 stop_tty(tty);
Alan Cox9c1729d2007-07-15 23:39:43 -0700967 tty_write_unlock(tty);
Alan Cox5f412b22006-09-29 02:01:40 -0700968 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
Alan Cox0fc00e22007-11-07 01:24:56 -0800971/**
Alan Cox1c2630c2008-04-30 00:53:34 -0700972 * tty_change_softcar - carrier change ioctl helper
973 * @tty: tty to update
974 * @arg: enable/disable CLOCAL
975 *
976 * Perform a change to the CLOCAL state and call into the driver
977 * layer to make it visible. All done with the termios mutex
978 */
979
980static int tty_change_softcar(struct tty_struct *tty, int arg)
981{
982 int ret = 0;
983 int bit = arg ? CLOCAL : 0;
Alan Coxf34d7a52008-04-30 00:54:13 -0700984 struct ktermios old;
Alan Cox1c2630c2008-04-30 00:53:34 -0700985
986 mutex_lock(&tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +0100987 old = tty->termios;
988 tty->termios.c_cflag &= ~CLOCAL;
989 tty->termios.c_cflag |= bit;
Alan Coxf34d7a52008-04-30 00:54:13 -0700990 if (tty->ops->set_termios)
991 tty->ops->set_termios(tty, &old);
Alan Coxadc8d742012-07-14 15:31:47 +0100992 if ((tty->termios.c_cflag & CLOCAL) != bit)
Alan Cox1c2630c2008-04-30 00:53:34 -0700993 ret = -EINVAL;
994 mutex_unlock(&tty->termios_mutex);
995 return ret;
996}
997
998/**
Alan Cox0fc00e22007-11-07 01:24:56 -0800999 * tty_mode_ioctl - mode related ioctls
1000 * @tty: tty for the ioctl
1001 * @file: file pointer for the tty
1002 * @cmd: command
1003 * @arg: ioctl argument
1004 *
1005 * Perform non line discipline specific mode control ioctls. This
1006 * is designed to be called by line disciplines to ensure they provide
1007 * consistent mode setting.
1008 */
1009
Alan Cox355d95a12008-02-08 04:18:48 -08001010int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
Alan Cox0fc00e22007-11-07 01:24:56 -08001011 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Alan Cox355d95a12008-02-08 04:18:48 -08001013 struct tty_struct *real_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 void __user *p = (void __user *)arg;
Alan Cox8f520022008-10-13 10:38:46 +01001015 int ret = 0;
Alan Cox26a2e202009-06-11 14:03:13 +01001016 struct ktermios kterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Alan Cox8d075b12011-02-14 16:27:53 +00001018 BUG_ON(file == NULL);
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1021 tty->driver->subtype == PTY_TYPE_MASTER)
1022 real_tty = tty->link;
1023 else
1024 real_tty = tty;
1025
1026 switch (cmd) {
1027#ifdef TIOCGETP
Alan Cox355d95a12008-02-08 04:18:48 -08001028 case TIOCGETP:
1029 return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
1030 case TIOCSETP:
1031 case TIOCSETN:
1032 return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033#endif
1034#ifdef TIOCGETC
Alan Cox355d95a12008-02-08 04:18:48 -08001035 case TIOCGETC:
1036 return get_tchars(real_tty, p);
1037 case TIOCSETC:
1038 return set_tchars(real_tty, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039#endif
1040#ifdef TIOCGLTC
Alan Cox355d95a12008-02-08 04:18:48 -08001041 case TIOCGLTC:
1042 return get_ltchars(real_tty, p);
1043 case TIOCSLTC:
1044 return set_ltchars(real_tty, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045#endif
Alan Cox355d95a12008-02-08 04:18:48 -08001046 case TCSETSF:
1047 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
1048 case TCSETSW:
1049 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
1050 case TCSETS:
1051 return set_termios(real_tty, p, TERMIOS_OLD);
Alan Coxedc6afc2006-12-08 02:38:44 -08001052#ifndef TCGETS2
Alan Cox355d95a12008-02-08 04:18:48 -08001053 case TCGETS:
Alan Cox26a2e202009-06-11 14:03:13 +01001054 copy_termios(real_tty, &kterm);
1055 if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
Alan Cox8f520022008-10-13 10:38:46 +01001056 ret = -EFAULT;
Alan Cox8f520022008-10-13 10:38:46 +01001057 return ret;
Alan Coxedc6afc2006-12-08 02:38:44 -08001058#else
Alan Cox355d95a12008-02-08 04:18:48 -08001059 case TCGETS:
Alan Cox26a2e202009-06-11 14:03:13 +01001060 copy_termios(real_tty, &kterm);
1061 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
Alan Cox8f520022008-10-13 10:38:46 +01001062 ret = -EFAULT;
Alan Cox8f520022008-10-13 10:38:46 +01001063 return ret;
Alan Cox355d95a12008-02-08 04:18:48 -08001064 case TCGETS2:
Alan Cox26a2e202009-06-11 14:03:13 +01001065 copy_termios(real_tty, &kterm);
1066 if (kernel_termios_to_user_termios((struct termios2 __user *)arg, &kterm))
Alan Cox8f520022008-10-13 10:38:46 +01001067 ret = -EFAULT;
Alan Cox8f520022008-10-13 10:38:46 +01001068 return ret;
Alan Cox355d95a12008-02-08 04:18:48 -08001069 case TCSETSF2:
1070 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
1071 case TCSETSW2:
1072 return set_termios(real_tty, p, TERMIOS_WAIT);
1073 case TCSETS2:
1074 return set_termios(real_tty, p, 0);
Alan Coxedc6afc2006-12-08 02:38:44 -08001075#endif
Alan Cox355d95a12008-02-08 04:18:48 -08001076 case TCGETA:
1077 return get_termio(real_tty, p);
1078 case TCSETAF:
1079 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
1080 case TCSETAW:
1081 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
1082 case TCSETA:
1083 return set_termios(real_tty, p, TERMIOS_TERMIO);
Alan Cox0fc00e22007-11-07 01:24:56 -08001084#ifndef TCGETS2
Alan Cox355d95a12008-02-08 04:18:48 -08001085 case TIOCGLCKTRMIOS:
Alan Cox26a2e202009-06-11 14:03:13 +01001086 copy_termios_locked(real_tty, &kterm);
1087 if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
Alan Cox8f520022008-10-13 10:38:46 +01001088 ret = -EFAULT;
Alan Cox8f520022008-10-13 10:38:46 +01001089 return ret;
Alan Cox355d95a12008-02-08 04:18:48 -08001090 case TIOCSLCKTRMIOS:
1091 if (!capable(CAP_SYS_ADMIN))
1092 return -EPERM;
Alan Cox26a2e202009-06-11 14:03:13 +01001093 copy_termios_locked(real_tty, &kterm);
1094 if (user_termios_to_kernel_termios(&kterm,
Alan Cox355d95a12008-02-08 04:18:48 -08001095 (struct termios __user *) arg))
Alan Cox26a2e202009-06-11 14:03:13 +01001096 return -EFAULT;
1097 mutex_lock(&real_tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +01001098 real_tty->termios_locked = kterm;
Alan Cox8f520022008-10-13 10:38:46 +01001099 mutex_unlock(&real_tty->termios_mutex);
Alan Cox26a2e202009-06-11 14:03:13 +01001100 return 0;
Alan Cox0fc00e22007-11-07 01:24:56 -08001101#else
Alan Cox355d95a12008-02-08 04:18:48 -08001102 case TIOCGLCKTRMIOS:
Alan Cox26a2e202009-06-11 14:03:13 +01001103 copy_termios_locked(real_tty, &kterm);
1104 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
Alan Cox8f520022008-10-13 10:38:46 +01001105 ret = -EFAULT;
Alan Cox8f520022008-10-13 10:38:46 +01001106 return ret;
Alan Cox355d95a12008-02-08 04:18:48 -08001107 case TIOCSLCKTRMIOS:
1108 if (!capable(CAP_SYS_ADMIN))
Alan Cox26a2e202009-06-11 14:03:13 +01001109 return -EPERM;
1110 copy_termios_locked(real_tty, &kterm);
1111 if (user_termios_to_kernel_termios_1(&kterm,
Alan Cox355d95a12008-02-08 04:18:48 -08001112 (struct termios __user *) arg))
Alan Cox26a2e202009-06-11 14:03:13 +01001113 return -EFAULT;
1114 mutex_lock(&real_tty->termios_mutex);
Alan Coxadc8d742012-07-14 15:31:47 +01001115 real_tty->termios_locked = kterm;
Alan Cox8f520022008-10-13 10:38:46 +01001116 mutex_unlock(&real_tty->termios_mutex);
1117 return ret;
Alan Cox0fc00e22007-11-07 01:24:56 -08001118#endif
Alan Cox1d65b4a2008-10-13 10:38:18 +01001119#ifdef TCGETX
Mike Frysinger5dca6072009-06-16 17:01:02 +01001120 case TCGETX: {
1121 struct termiox ktermx;
Alan Cox1d65b4a2008-10-13 10:38:18 +01001122 if (real_tty->termiox == NULL)
1123 return -EINVAL;
Alan Cox8f520022008-10-13 10:38:46 +01001124 mutex_lock(&real_tty->termios_mutex);
Alan Cox26a2e202009-06-11 14:03:13 +01001125 memcpy(&ktermx, real_tty->termiox, sizeof(struct termiox));
Alan Cox8f520022008-10-13 10:38:46 +01001126 mutex_unlock(&real_tty->termios_mutex);
Alan Cox26a2e202009-06-11 14:03:13 +01001127 if (copy_to_user(p, &ktermx, sizeof(struct termiox)))
1128 ret = -EFAULT;
Alan Cox8f520022008-10-13 10:38:46 +01001129 return ret;
Mike Frysinger5dca6072009-06-16 17:01:02 +01001130 }
Alan Cox1d65b4a2008-10-13 10:38:18 +01001131 case TCSETX:
1132 return set_termiox(real_tty, p, 0);
1133 case TCSETXW:
1134 return set_termiox(real_tty, p, TERMIOS_WAIT);
1135 case TCSETXF:
1136 return set_termiox(real_tty, p, TERMIOS_FLUSH);
1137#endif
Alan Cox355d95a12008-02-08 04:18:48 -08001138 case TIOCGSOFTCAR:
Alan Cox26a2e202009-06-11 14:03:13 +01001139 copy_termios(real_tty, &kterm);
1140 ret = put_user((kterm.c_cflag & CLOCAL) ? 1 : 0,
Alan Cox355d95a12008-02-08 04:18:48 -08001141 (int __user *)arg);
Alan Cox8f520022008-10-13 10:38:46 +01001142 return ret;
Alan Cox355d95a12008-02-08 04:18:48 -08001143 case TIOCSSOFTCAR:
1144 if (get_user(arg, (unsigned int __user *) arg))
1145 return -EFAULT;
Alan Coxf753f322008-08-26 19:52:47 +01001146 return tty_change_softcar(real_tty, arg);
Alan Cox355d95a12008-02-08 04:18:48 -08001147 default:
1148 return -ENOIOCTLCMD;
Alan Cox0fc00e22007-11-07 01:24:56 -08001149 }
1150}
Alan Cox0fc00e22007-11-07 01:24:56 -08001151EXPORT_SYMBOL_GPL(tty_mode_ioctl);
1152
1153int tty_perform_flush(struct tty_struct *tty, unsigned long arg)
1154{
1155 struct tty_ldisc *ld;
1156 int retval = tty_check_change(tty);
1157 if (retval)
1158 return retval;
1159
Alan Coxc0253ee2009-01-15 13:30:25 +00001160 ld = tty_ldisc_ref_wait(tty);
Alan Cox0fc00e22007-11-07 01:24:56 -08001161 switch (arg) {
1162 case TCIFLUSH:
Ilya Zykova1bf9582013-01-16 13:07:50 +04001163 if (ld && ld->ops->flush_buffer) {
Alan Coxa352def2008-07-16 21:53:12 +01001164 ld->ops->flush_buffer(tty);
Ilya Zykova1bf9582013-01-16 13:07:50 +04001165 tty_unthrottle(tty);
1166 }
Alan Cox0fc00e22007-11-07 01:24:56 -08001167 break;
1168 case TCIOFLUSH:
Ilya Zykova1bf9582013-01-16 13:07:50 +04001169 if (ld && ld->ops->flush_buffer) {
Alan Coxa352def2008-07-16 21:53:12 +01001170 ld->ops->flush_buffer(tty);
Ilya Zykova1bf9582013-01-16 13:07:50 +04001171 tty_unthrottle(tty);
1172 }
Alan Cox0fc00e22007-11-07 01:24:56 -08001173 /* fall through */
1174 case TCOFLUSH:
Alan Coxf34d7a52008-04-30 00:54:13 -07001175 tty_driver_flush_buffer(tty);
Alan Cox0fc00e22007-11-07 01:24:56 -08001176 break;
1177 default:
1178 tty_ldisc_deref(ld);
1179 return -EINVAL;
1180 }
1181 tty_ldisc_deref(ld);
1182 return 0;
1183}
Alan Cox0fc00e22007-11-07 01:24:56 -08001184EXPORT_SYMBOL_GPL(tty_perform_flush);
1185
Alan Cox47afa7a2008-10-13 10:44:17 +01001186int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file,
Alan Cox0fc00e22007-11-07 01:24:56 -08001187 unsigned int cmd, unsigned long arg)
1188{
Alan Cox0fc00e22007-11-07 01:24:56 -08001189 int retval;
1190
Alan Cox0fc00e22007-11-07 01:24:56 -08001191 switch (cmd) {
Alan Cox355d95a12008-02-08 04:18:48 -08001192 case TCXONC:
1193 retval = tty_check_change(tty);
1194 if (retval)
1195 return retval;
1196 switch (arg) {
1197 case TCOOFF:
1198 if (!tty->flow_stopped) {
1199 tty->flow_stopped = 1;
1200 stop_tty(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
Alan Cox355d95a12008-02-08 04:18:48 -08001202 break;
1203 case TCOON:
1204 if (tty->flow_stopped) {
1205 tty->flow_stopped = 0;
1206 start_tty(tty);
1207 }
1208 break;
1209 case TCIOFF:
1210 if (STOP_CHAR(tty) != __DISABLED_CHAR)
1211 return send_prio_char(tty, STOP_CHAR(tty));
1212 break;
1213 case TCION:
1214 if (START_CHAR(tty) != __DISABLED_CHAR)
1215 return send_prio_char(tty, START_CHAR(tty));
1216 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 default:
Alan Cox355d95a12008-02-08 04:18:48 -08001218 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
Alan Cox355d95a12008-02-08 04:18:48 -08001220 return 0;
1221 case TCFLSH:
1222 return tty_perform_flush(tty, arg);
Alan Cox355d95a12008-02-08 04:18:48 -08001223 default:
1224 /* Try the mode commands */
1225 return tty_mode_ioctl(tty, file, cmd, arg);
1226 }
1227}
Alan Cox47afa7a2008-10-13 10:44:17 +01001228EXPORT_SYMBOL(n_tty_ioctl_helper);
Thomas Meyer8193c422011-10-05 23:13:13 +02001229
1230#ifdef CONFIG_COMPAT
1231long n_tty_compat_ioctl_helper(struct tty_struct *tty, struct file *file,
1232 unsigned int cmd, unsigned long arg)
1233{
1234 switch (cmd) {
1235 case TIOCGLCKTRMIOS:
1236 case TIOCSLCKTRMIOS:
1237 return tty_mode_ioctl(tty, file, cmd, (unsigned long) compat_ptr(arg));
1238 default:
1239 return -ENOIOCTLCMD;
1240 }
1241}
1242EXPORT_SYMBOL(n_tty_compat_ioctl_helper);
1243#endif
1244