blob: 9ab33c729f02a5dc695e79e457729b9c89be601e [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);
Alan Cox38db8972009-06-11 12:44:17 +0100109 mutex_unlock(&tty->termios_mutex);
Alan Cox39c2e602008-04-30 00:54:18 -0700110}
111EXPORT_SYMBOL(tty_throttle);
112
Alan Coxd81ed102008-10-13 10:41:42 +0100113/**
114 * tty_unthrottle - flow control
115 * @tty: terminal
116 *
117 * Indicate that a tty may continue transmitting data down the stack.
Alan Cox38db8972009-06-11 12:44:17 +0100118 * Takes the termios mutex to protect against parallel throttle/unthrottle
119 * and also to ensure the driver can consistently reference its own
120 * termios data at this point when implementing software flow control.
121 *
122 * Drivers should however remember that the stack can issue a throttle,
123 * then change flow control method, then unthrottle.
Alan Coxd81ed102008-10-13 10:41:42 +0100124 */
125
Alan Cox39c2e602008-04-30 00:54:18 -0700126void tty_unthrottle(struct tty_struct *tty)
127{
Alan Cox38db8972009-06-11 12:44:17 +0100128 mutex_lock(&tty->termios_mutex);
Alan Cox39c2e602008-04-30 00:54:18 -0700129 if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
130 tty->ops->unthrottle)
131 tty->ops->unthrottle(tty);
Alan Cox38db8972009-06-11 12:44:17 +0100132 mutex_unlock(&tty->termios_mutex);
Alan Cox39c2e602008-04-30 00:54:18 -0700133}
134EXPORT_SYMBOL(tty_unthrottle);
Alan Coxf34d7a52008-04-30 00:54:13 -0700135
Alan Coxaf9b8972006-08-27 01:24:01 -0700136/**
137 * tty_wait_until_sent - wait for I/O to finish
138 * @tty: tty we are waiting for
139 * @timeout: how long we will wait
140 *
141 * Wait for characters pending in a tty driver to hit the wire, or
142 * for a timeout to occur (eg due to flow control)
143 *
144 * Locking: none
145 */
146
Alan Cox355d95a2008-02-08 04:18:48 -0800147void tty_wait_until_sent(struct tty_struct *tty, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#ifdef TTY_DEBUG_WAIT_UNTIL_SENT
150 char buf[64];
Alan Cox355d95a2008-02-08 04:18:48 -0800151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 printk(KERN_DEBUG "%s wait until sent...\n", tty_name(tty, buf));
153#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (!timeout)
155 timeout = MAX_SCHEDULE_TIMEOUT;
José Adolfo Galdámez900469d2015-06-20 23:45:36 -0600156
Jiri Slaby5a52bd42007-07-15 23:40:18 -0700157 if (wait_event_interruptible_timeout(tty->write_wait,
José Adolfo Galdámez900469d2015-06-20 23:45:36 -0600158 !tty_chars_in_buffer(tty), timeout) < 0) {
159 return;
Alan Cox0ee9cbb2008-04-30 00:53:32 -0700160 }
José Adolfo Galdámez900469d2015-06-20 23:45:36 -0600161
162 if (timeout == MAX_SCHEDULE_TIMEOUT)
163 timeout = 0;
164
165 if (tty->ops->wait_until_sent)
166 tty->ops->wait_until_sent(tty, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168EXPORT_SYMBOL(tty_wait_until_sent);
169
Alan Coxd81ed102008-10-13 10:41:42 +0100170
171/*
172 * Termios Helper Methods
173 */
174
Alan Coxedc6afc2006-12-08 02:38:44 -0800175static void unset_locked_termios(struct ktermios *termios,
176 struct ktermios *old,
177 struct ktermios *locked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 int i;
Alan Cox355d95a2008-02-08 04:18:48 -0800180
181#define NOSET_MASK(x, y, z) (x = ((x) & ~(z)) | ((y) & (z)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 if (!locked) {
184 printk(KERN_WARNING "Warning?!? termios_locked is NULL.\n");
185 return;
186 }
187
188 NOSET_MASK(termios->c_iflag, old->c_iflag, locked->c_iflag);
189 NOSET_MASK(termios->c_oflag, old->c_oflag, locked->c_oflag);
190 NOSET_MASK(termios->c_cflag, old->c_cflag, locked->c_cflag);
191 NOSET_MASK(termios->c_lflag, old->c_lflag, locked->c_lflag);
192 termios->c_line = locked->c_line ? old->c_line : termios->c_line;
Alan Cox355d95a2008-02-08 04:18:48 -0800193 for (i = 0; i < NCCS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 termios->c_cc[i] = locked->c_cc[i] ?
195 old->c_cc[i] : termios->c_cc[i];
Alan Coxedc6afc2006-12-08 02:38:44 -0800196 /* FIXME: What should we do for i/ospeed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Alan Coxedc6afc2006-12-08 02:38:44 -0800199/*
200 * Routine which returns the baud rate of the tty
201 *
202 * Note that the baud_table needs to be kept in sync with the
203 * include/asm/termbits.h file.
204 */
205static const speed_t baud_table[] = {
206 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
207 9600, 19200, 38400, 57600, 115200, 230400, 460800,
208#ifdef __sparc__
209 76800, 153600, 307200, 614400, 921600
210#else
211 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
212 2500000, 3000000, 3500000, 4000000
213#endif
214};
215
216#ifndef __sparc__
217static const tcflag_t baud_bits[] = {
218 B0, B50, B75, B110, B134, B150, B200, B300, B600,
219 B1200, B1800, B2400, B4800, B9600, B19200, B38400,
220 B57600, B115200, B230400, B460800, B500000, B576000,
221 B921600, B1000000, B1152000, B1500000, B2000000, B2500000,
222 B3000000, B3500000, B4000000
223};
224#else
225static const tcflag_t baud_bits[] = {
226 B0, B50, B75, B110, B134, B150, B200, B300, B600,
227 B1200, B1800, B2400, B4800, B9600, B19200, B38400,
228 B57600, B115200, B230400, B460800, B76800, B153600,
229 B307200, B614400, B921600
230};
231#endif
232
233static int n_baud_table = ARRAY_SIZE(baud_table);
234
235/**
236 * tty_termios_baud_rate
237 * @termios: termios structure
238 *
239 * Convert termios baud rate data into a speed. This should be called
240 * with the termios lock held if this termios is a terminal termios
241 * structure. May change the termios data. Device drivers can call this
242 * function but should use ->c_[io]speed directly as they are updated.
243 *
244 * Locking: none
245 */
246
247speed_t tty_termios_baud_rate(struct ktermios *termios)
248{
249 unsigned int cbaud;
250
251 cbaud = termios->c_cflag & CBAUD;
252
253#ifdef BOTHER
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300254 /* Magic token for arbitrary speed via c_ispeed/c_ospeed */
Alan Coxedc6afc2006-12-08 02:38:44 -0800255 if (cbaud == BOTHER)
256 return termios->c_ospeed;
257#endif
258 if (cbaud & CBAUDEX) {
259 cbaud &= ~CBAUDEX;
260
261 if (cbaud < 1 || cbaud + 15 > n_baud_table)
262 termios->c_cflag &= ~CBAUDEX;
263 else
264 cbaud += 15;
265 }
266 return baud_table[cbaud];
267}
Alan Coxedc6afc2006-12-08 02:38:44 -0800268EXPORT_SYMBOL(tty_termios_baud_rate);
269
270/**
271 * tty_termios_input_baud_rate
272 * @termios: termios structure
273 *
274 * Convert termios baud rate data into a speed. This should be called
275 * with the termios lock held if this termios is a terminal termios
276 * structure. May change the termios data. Device drivers can call this
277 * function but should use ->c_[io]speed directly as they are updated.
278 *
279 * Locking: none
280 */
281
282speed_t tty_termios_input_baud_rate(struct ktermios *termios)
283{
284#ifdef IBSHIFT
285 unsigned int cbaud = (termios->c_cflag >> IBSHIFT) & CBAUD;
286
287 if (cbaud == B0)
288 return tty_termios_baud_rate(termios);
289
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300290 /* Magic token for arbitrary speed via c_ispeed*/
Alan Coxedc6afc2006-12-08 02:38:44 -0800291 if (cbaud == BOTHER)
292 return termios->c_ispeed;
293
294 if (cbaud & CBAUDEX) {
295 cbaud &= ~CBAUDEX;
296
297 if (cbaud < 1 || cbaud + 15 > n_baud_table)
298 termios->c_cflag &= ~(CBAUDEX << IBSHIFT);
299 else
300 cbaud += 15;
301 }
302 return baud_table[cbaud];
303#else
304 return tty_termios_baud_rate(termios);
305#endif
306}
Alan Coxedc6afc2006-12-08 02:38:44 -0800307EXPORT_SYMBOL(tty_termios_input_baud_rate);
308
Alan Coxedc6afc2006-12-08 02:38:44 -0800309/**
310 * tty_termios_encode_baud_rate
Alan Cox78137e32007-02-10 01:45:57 -0800311 * @termios: ktermios structure holding user requested state
Alan Coxedc6afc2006-12-08 02:38:44 -0800312 * @ispeed: input speed
313 * @ospeed: output speed
314 *
315 * Encode the speeds set into the passed termios structure. This is
Uwe Kleine-Königf98e5b82011-04-12 09:59:29 +0200316 * used as a library helper for drivers so that they can report back
Alan Coxedc6afc2006-12-08 02:38:44 -0800317 * the actual speed selected when it differs from the speed requested
318 *
Alan Cox78137e32007-02-10 01:45:57 -0800319 * For maximal back compatibility with legacy SYS5/POSIX *nix behaviour
320 * we need to carefully set the bits when the user does not get the
321 * desired speed. We allow small margins and preserve as much of possible
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400322 * of the input intent to keep compatibility.
Alan Coxedc6afc2006-12-08 02:38:44 -0800323 *
324 * Locking: Caller should hold termios lock. This is already held
325 * when calling this function from the driver termios handler.
Alan Cox5f519d72007-10-16 23:30:07 -0700326 *
327 * The ifdefs deal with platforms whose owners have yet to update them
328 * and will all go away once this is done.
Alan Coxedc6afc2006-12-08 02:38:44 -0800329 */
330
Maciej W. Rozycki75e8b712007-10-18 03:04:35 -0700331void tty_termios_encode_baud_rate(struct ktermios *termios,
332 speed_t ibaud, speed_t obaud)
Alan Coxedc6afc2006-12-08 02:38:44 -0800333{
334 int i = 0;
Alan Cox78137e32007-02-10 01:45:57 -0800335 int ifound = -1, ofound = -1;
336 int iclose = ibaud/50, oclose = obaud/50;
337 int ibinput = 0;
Alan Coxedc6afc2006-12-08 02:38:44 -0800338
Alan Cox5f519d72007-10-16 23:30:07 -0700339 if (obaud == 0) /* CD dropped */
340 ibaud = 0; /* Clear ibaud to be sure */
341
Alan Coxedc6afc2006-12-08 02:38:44 -0800342 termios->c_ispeed = ibaud;
343 termios->c_ospeed = obaud;
344
Alan Cox5f519d72007-10-16 23:30:07 -0700345#ifdef BOTHER
Alan Cox78137e32007-02-10 01:45:57 -0800346 /* If the user asked for a precise weird speed give a precise weird
347 answer. If they asked for a Bfoo speed they many have problems
348 digesting non-exact replies so fuzz a bit */
349
350 if ((termios->c_cflag & CBAUD) == BOTHER)
351 oclose = 0;
352 if (((termios->c_cflag >> IBSHIFT) & CBAUD) == BOTHER)
353 iclose = 0;
354 if ((termios->c_cflag >> IBSHIFT) & CBAUD)
355 ibinput = 1; /* An input speed was specified */
Alan Cox5f519d72007-10-16 23:30:07 -0700356#endif
Alan Coxedc6afc2006-12-08 02:38:44 -0800357 termios->c_cflag &= ~CBAUD;
Alan Coxedc6afc2006-12-08 02:38:44 -0800358
Alan Cox5f519d72007-10-16 23:30:07 -0700359 /*
360 * Our goal is to find a close match to the standard baud rate
361 * returned. Walk the baud rate table and if we get a very close
362 * match then report back the speed as a POSIX Bxxxx value by
363 * preference
364 */
365
Alan Coxedc6afc2006-12-08 02:38:44 -0800366 do {
Maciej W. Rozycki75e8b712007-10-18 03:04:35 -0700367 if (obaud - oclose <= baud_table[i] &&
368 obaud + oclose >= baud_table[i]) {
Alan Coxedc6afc2006-12-08 02:38:44 -0800369 termios->c_cflag |= baud_bits[i];
Alan Cox78137e32007-02-10 01:45:57 -0800370 ofound = i;
Alan Coxedc6afc2006-12-08 02:38:44 -0800371 }
Maciej W. Rozycki75e8b712007-10-18 03:04:35 -0700372 if (ibaud - iclose <= baud_table[i] &&
373 ibaud + iclose >= baud_table[i]) {
374 /* For the case input == output don't set IBAUD bits
375 if the user didn't do so */
Alan Cox5f519d72007-10-16 23:30:07 -0700376 if (ofound == i && !ibinput)
377 ifound = i;
378#ifdef IBSHIFT
379 else {
380 ifound = i;
Alan Cox78137e32007-02-10 01:45:57 -0800381 termios->c_cflag |= (baud_bits[i] << IBSHIFT);
Alan Cox5f519d72007-10-16 23:30:07 -0700382 }
383#endif
Alan Coxedc6afc2006-12-08 02:38:44 -0800384 }
Jiri Slaby68043962007-07-15 23:40:18 -0700385 } while (++i < n_baud_table);
Alan Cox5f519d72007-10-16 23:30:07 -0700386
387 /*
388 * If we found no match then use BOTHER if provided or warn
389 * the user their platform maintainer needs to wake up if not.
390 */
391#ifdef BOTHER
Alan Cox78137e32007-02-10 01:45:57 -0800392 if (ofound == -1)
Alan Coxedc6afc2006-12-08 02:38:44 -0800393 termios->c_cflag |= BOTHER;
Alan Cox78137e32007-02-10 01:45:57 -0800394 /* Set exact input bits only if the input and output differ or the
395 user already did */
Jiri Slaby68043962007-07-15 23:40:18 -0700396 if (ifound == -1 && (ibaud != obaud || ibinput))
Alan Coxedc6afc2006-12-08 02:38:44 -0800397 termios->c_cflag |= (BOTHER << IBSHIFT);
Alan Cox5f519d72007-10-16 23:30:07 -0700398#else
399 if (ifound == -1 || ofound == -1) {
Marcin Slusarz9074d962009-08-09 21:54:03 +0200400 printk_once(KERN_WARNING "tty: Unable to return correct "
Alan Cox5f519d72007-10-16 23:30:07 -0700401 "speed data as your architecture needs updating.\n");
402 }
403#endif
Alan Coxedc6afc2006-12-08 02:38:44 -0800404}
Alan Coxedc6afc2006-12-08 02:38:44 -0800405EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate);
406
Alan Coxd81ed102008-10-13 10:41:42 +0100407/**
408 * tty_encode_baud_rate - set baud rate of the tty
409 * @ibaud: input baud rate
410 * @obad: output baud rate
411 *
412 * Update the current termios data for the tty with the new speed
413 * settings. The caller must hold the termios_mutex for the tty in
414 * question.
415 */
416
Alan Cox5f519d72007-10-16 23:30:07 -0700417void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, speed_t obaud)
418{
419 tty_termios_encode_baud_rate(tty->termios, ibaud, obaud);
420}
421EXPORT_SYMBOL_GPL(tty_encode_baud_rate);
Alan Coxedc6afc2006-12-08 02:38:44 -0800422
423/**
424 * tty_get_baud_rate - get tty bit rates
425 * @tty: tty to query
426 *
427 * Returns the baud rate as an integer for this terminal. The
428 * termios lock must be held by the caller and the terminal bit
429 * flags may be updated.
430 *
431 * Locking: none
432 */
433
434speed_t tty_get_baud_rate(struct tty_struct *tty)
435{
436 speed_t baud = tty_termios_baud_rate(tty->termios);
437
438 if (baud == 38400 && tty->alt_speed) {
439 if (!tty->warned) {
440 printk(KERN_WARNING "Use of setserial/setrocket to "
441 "set SPD_* flags is deprecated\n");
442 tty->warned = 1;
443 }
444 baud = tty->alt_speed;
445 }
446
447 return baud;
448}
Alan Coxedc6afc2006-12-08 02:38:44 -0800449EXPORT_SYMBOL(tty_get_baud_rate);
450
Alan Coxaf9b8972006-08-27 01:24:01 -0700451/**
Alan Cox5f519d72007-10-16 23:30:07 -0700452 * tty_termios_copy_hw - copy hardware settings
453 * @new: New termios
454 * @old: Old termios
455 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300456 * Propagate the hardware specific terminal setting bits from
Alan Cox5f519d72007-10-16 23:30:07 -0700457 * the old termios structure to the new one. This is used in cases
458 * where the hardware does not support reconfiguration or as a helper
459 * in some cases where only minimal reconfiguration is supported
460 */
461
462void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old)
463{
464 /* The bits a dumb device handles in software. Smart devices need
465 to always provide a set_termios method */
466 new->c_cflag &= HUPCL | CREAD | CLOCAL;
467 new->c_cflag |= old->c_cflag & ~(HUPCL | CREAD | CLOCAL);
468 new->c_ispeed = old->c_ispeed;
469 new->c_ospeed = old->c_ospeed;
470}
Alan Cox5f519d72007-10-16 23:30:07 -0700471EXPORT_SYMBOL(tty_termios_copy_hw);
472
473/**
Alan Coxbf5e5832008-01-08 14:55:51 +0000474 * tty_termios_hw_change - check for setting change
475 * @a: termios
476 * @b: termios to compare
477 *
478 * Check if any of the bits that affect a dumb device have changed
479 * between the two termios structures, or a speed change is needed.
480 */
481
482int tty_termios_hw_change(struct ktermios *a, struct ktermios *b)
483{
484 if (a->c_ispeed != b->c_ispeed || a->c_ospeed != b->c_ospeed)
485 return 1;
486 if ((a->c_cflag ^ b->c_cflag) & ~(HUPCL | CREAD | CLOCAL))
487 return 1;
488 return 0;
489}
490EXPORT_SYMBOL(tty_termios_hw_change);
491
492/**
Alan Cox8d075b12011-02-14 16:27:53 +0000493 * tty_set_termios - update termios values
Alan Coxaf9b8972006-08-27 01:24:01 -0700494 * @tty: tty to update
495 * @new_termios: desired new value
496 *
497 * Perform updates to the termios values set on this terminal. There
498 * is a bit of layering violation here with n_tty in terms of the
499 * internal knowledge of this function.
500 *
Alan Coxd81ed102008-10-13 10:41:42 +0100501 * Locking: termios_mutex
Alan Coxaf9b8972006-08-27 01:24:01 -0700502 */
503
Alan Cox8d075b12011-02-14 16:27:53 +0000504int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Alan Cox978e5952008-04-30 00:53:59 -0700506 struct ktermios old_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct tty_ldisc *ld;
Alan Cox04f378b2008-04-30 00:53:29 -0700508 unsigned long flags;
Alan Cox355d95a2008-02-08 04:18:48 -0800509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /*
511 * Perform the actual termios internal changes under lock.
512 */
Alan Cox355d95a2008-02-08 04:18:48 -0800513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 /* FIXME: we need to decide on some locking/ordering semantics
516 for the set_termios notification eventually */
Arjan van de Ven5785c952006-09-29 02:00:43 -0700517 mutex_lock(&tty->termios_mutex);
Alan Cox978e5952008-04-30 00:53:59 -0700518 old_termios = *tty->termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 *tty->termios = *new_termios;
520 unset_locked_termios(tty->termios, &old_termios, tty->termios_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 /* See if packet mode change of state. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (tty->link && tty->link->packet) {
hyc@symas.com26df6d12010-06-22 10:14:49 -0700524 int extproc = (old_termios.c_lflag & EXTPROC) |
525 (tty->termios->c_lflag & EXTPROC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 int old_flow = ((old_termios.c_iflag & IXON) &&
527 (old_termios.c_cc[VSTOP] == '\023') &&
528 (old_termios.c_cc[VSTART] == '\021'));
529 int new_flow = (I_IXON(tty) &&
530 STOP_CHAR(tty) == '\023' &&
531 START_CHAR(tty) == '\021');
hyc@symas.com26df6d12010-06-22 10:14:49 -0700532 if ((old_flow != new_flow) || extproc) {
Alan Cox04f378b2008-04-30 00:53:29 -0700533 spin_lock_irqsave(&tty->ctrl_lock, flags);
hyc@symas.com26df6d12010-06-22 10:14:49 -0700534 if (old_flow != new_flow) {
535 tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
536 if (new_flow)
537 tty->ctrl_status |= TIOCPKT_DOSTOP;
538 else
539 tty->ctrl_status |= TIOCPKT_NOSTOP;
540 }
541 if (extproc)
542 tty->ctrl_status |= TIOCPKT_IOCTL;
Alan Cox04f378b2008-04-30 00:53:29 -0700543 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 wake_up_interruptible(&tty->link->read_wait);
545 }
546 }
Alan Cox355d95a2008-02-08 04:18:48 -0800547
Alan Coxf34d7a52008-04-30 00:54:13 -0700548 if (tty->ops->set_termios)
549 (*tty->ops->set_termios)(tty, &old_termios);
Alan Cox5f519d72007-10-16 23:30:07 -0700550 else
551 tty_termios_copy_hw(tty->termios, &old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 ld = tty_ldisc_ref(tty);
554 if (ld != NULL) {
Alan Coxa352def2008-07-16 21:53:12 +0100555 if (ld->ops->set_termios)
556 (ld->ops->set_termios)(tty, &old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 tty_ldisc_deref(ld);
558 }
Arjan van de Ven5785c952006-09-29 02:00:43 -0700559 mutex_unlock(&tty->termios_mutex);
Alan Cox8d075b12011-02-14 16:27:53 +0000560 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
Alan Cox8d075b12011-02-14 16:27:53 +0000562EXPORT_SYMBOL_GPL(tty_set_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Alan Coxaf9b8972006-08-27 01:24:01 -0700564/**
565 * set_termios - set termios values for a tty
566 * @tty: terminal device
567 * @arg: user data
568 * @opt: option information
569 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200570 * Helper function to prepare termios data and run necessary other
Alan Cox8d075b12011-02-14 16:27:53 +0000571 * functions before using tty_set_termios to do the actual changes.
Alan Coxaf9b8972006-08-27 01:24:01 -0700572 *
573 * Locking:
Alan Coxd81ed102008-10-13 10:41:42 +0100574 * Called functions take ldisc and termios_mutex locks
Alan Coxaf9b8972006-08-27 01:24:01 -0700575 */
576
Alan Cox355d95a2008-02-08 04:18:48 -0800577static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Alan Coxedc6afc2006-12-08 02:38:44 -0800579 struct ktermios tmp_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 struct tty_ldisc *ld;
581 int retval = tty_check_change(tty);
582
583 if (retval)
584 return retval;
585
Alan Cox978e5952008-04-30 00:53:59 -0700586 mutex_lock(&tty->termios_mutex);
Alan Cox64bb6c52006-12-08 02:38:47 -0800587 memcpy(&tmp_termios, tty->termios, sizeof(struct ktermios));
Alan Cox978e5952008-04-30 00:53:59 -0700588 mutex_unlock(&tty->termios_mutex);
Alan Cox64bb6c52006-12-08 02:38:47 -0800589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (opt & TERMIOS_TERMIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (user_termio_to_kernel_termios(&tmp_termios,
592 (struct termio __user *)arg))
593 return -EFAULT;
Alan Coxedc6afc2006-12-08 02:38:44 -0800594#ifdef TCGETS2
595 } else if (opt & TERMIOS_OLD) {
Alan Coxedc6afc2006-12-08 02:38:44 -0800596 if (user_termios_to_kernel_termios_1(&tmp_termios,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 (struct termios __user *)arg))
598 return -EFAULT;
Alan Cox64bb6c52006-12-08 02:38:47 -0800599 } else {
600 if (user_termios_to_kernel_termios(&tmp_termios,
601 (struct termios2 __user *)arg))
602 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
Alan Cox64bb6c52006-12-08 02:38:47 -0800604#else
605 } else if (user_termios_to_kernel_termios(&tmp_termios,
606 (struct termios __user *)arg))
607 return -EFAULT;
608#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Alan Cox355d95a2008-02-08 04:18:48 -0800610 /* If old style Bfoo values are used then load c_ispeed/c_ospeed
611 * with the real speed so its unconditionally usable */
Alan Coxedc6afc2006-12-08 02:38:44 -0800612 tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios);
613 tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios);
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 ld = tty_ldisc_ref(tty);
Alan Cox355d95a2008-02-08 04:18:48 -0800616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (ld != NULL) {
Alan Coxa352def2008-07-16 21:53:12 +0100618 if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
619 ld->ops->flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 tty_ldisc_deref(ld);
621 }
Alan Cox355d95a2008-02-08 04:18:48 -0800622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (opt & TERMIOS_WAIT) {
624 tty_wait_until_sent(tty, 0);
625 if (signal_pending(current))
Oleg Nesterov46605da2013-01-29 20:07:41 +0100626 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628
Alan Cox8d075b12011-02-14 16:27:53 +0000629 tty_set_termios(tty, &tmp_termios);
Alan Cox5f519d72007-10-16 23:30:07 -0700630
631 /* FIXME: Arguably if tmp_termios == tty->termios AND the
632 actual requested termios was not tmp_termios then we may
633 want to return an error as no user requested change has
634 succeeded */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return 0;
636}
637
Alan Cox26a2e202009-06-11 14:03:13 +0100638static void copy_termios(struct tty_struct *tty, struct ktermios *kterm)
639{
640 mutex_lock(&tty->termios_mutex);
641 memcpy(kterm, tty->termios, sizeof(struct ktermios));
642 mutex_unlock(&tty->termios_mutex);
643}
644
645static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm)
646{
647 mutex_lock(&tty->termios_mutex);
648 memcpy(kterm, tty->termios_locked, sizeof(struct ktermios));
649 mutex_unlock(&tty->termios_mutex);
650}
651
Alan Cox355d95a2008-02-08 04:18:48 -0800652static int get_termio(struct tty_struct *tty, struct termio __user *termio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Alan Cox26a2e202009-06-11 14:03:13 +0100654 struct ktermios kterm;
655 copy_termios(tty, &kterm);
656 if (kernel_termios_to_user_termio(termio, &kterm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return -EFAULT;
658 return 0;
659}
660
Alan Cox1d65b4a2008-10-13 10:38:18 +0100661
662#ifdef TCGETX
663
664/**
665 * set_termiox - set termiox fields if possible
666 * @tty: terminal
667 * @arg: termiox structure from user
668 * @opt: option flags for ioctl type
669 *
670 * Implement the device calling points for the SYS5 termiox ioctl
671 * interface in Linux
672 */
673
674static int set_termiox(struct tty_struct *tty, void __user *arg, int opt)
675{
676 struct termiox tnew;
677 struct tty_ldisc *ld;
678
679 if (tty->termiox == NULL)
680 return -EINVAL;
681 if (copy_from_user(&tnew, arg, sizeof(struct termiox)))
682 return -EFAULT;
683
684 ld = tty_ldisc_ref(tty);
685 if (ld != NULL) {
686 if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
687 ld->ops->flush_buffer(tty);
688 tty_ldisc_deref(ld);
689 }
690 if (opt & TERMIOS_WAIT) {
691 tty_wait_until_sent(tty, 0);
692 if (signal_pending(current))
Oleg Nesterov46605da2013-01-29 20:07:41 +0100693 return -ERESTARTSYS;
Alan Cox1d65b4a2008-10-13 10:38:18 +0100694 }
695
696 mutex_lock(&tty->termios_mutex);
697 if (tty->ops->set_termiox)
698 tty->ops->set_termiox(tty, &tnew);
699 mutex_unlock(&tty->termios_mutex);
700 return 0;
701}
702
703#endif
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706#ifdef TIOCGETP
707/*
708 * These are deprecated, but there is limited support..
709 *
710 * The "sg_flags" translation is a joke..
711 */
Alan Cox355d95a2008-02-08 04:18:48 -0800712static int get_sgflags(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 int flags = 0;
715
716 if (!(tty->termios->c_lflag & ICANON)) {
717 if (tty->termios->c_lflag & ISIG)
718 flags |= 0x02; /* cbreak */
719 else
720 flags |= 0x20; /* raw */
721 }
722 if (tty->termios->c_lflag & ECHO)
723 flags |= 0x08; /* echo */
724 if (tty->termios->c_oflag & OPOST)
725 if (tty->termios->c_oflag & ONLCR)
726 flags |= 0x10; /* crmod */
727 return flags;
728}
729
Alan Cox355d95a2008-02-08 04:18:48 -0800730static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 struct sgttyb tmp;
733
Arjan van de Ven5785c952006-09-29 02:00:43 -0700734 mutex_lock(&tty->termios_mutex);
Alan Cox606d0992006-12-08 02:38:45 -0800735 tmp.sg_ispeed = tty->termios->c_ispeed;
736 tmp.sg_ospeed = tty->termios->c_ospeed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 tmp.sg_erase = tty->termios->c_cc[VERASE];
738 tmp.sg_kill = tty->termios->c_cc[VKILL];
739 tmp.sg_flags = get_sgflags(tty);
Arjan van de Ven5785c952006-09-29 02:00:43 -0700740 mutex_unlock(&tty->termios_mutex);
Alan Cox355d95a2008-02-08 04:18:48 -0800741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
743}
744
Alan Cox355d95a2008-02-08 04:18:48 -0800745static void set_sgflags(struct ktermios *termios, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 termios->c_iflag = ICRNL | IXON;
748 termios->c_oflag = 0;
749 termios->c_lflag = ISIG | ICANON;
750 if (flags & 0x02) { /* cbreak */
751 termios->c_iflag = 0;
752 termios->c_lflag &= ~ICANON;
753 }
754 if (flags & 0x08) { /* echo */
755 termios->c_lflag |= ECHO | ECHOE | ECHOK |
756 ECHOCTL | ECHOKE | IEXTEN;
757 }
758 if (flags & 0x10) { /* crmod */
759 termios->c_oflag |= OPOST | ONLCR;
760 }
761 if (flags & 0x20) { /* raw */
762 termios->c_iflag = 0;
763 termios->c_lflag &= ~(ISIG | ICANON);
764 }
765 if (!(termios->c_lflag & ICANON)) {
766 termios->c_cc[VMIN] = 1;
767 termios->c_cc[VTIME] = 0;
768 }
769}
770
Alan Coxaf9b8972006-08-27 01:24:01 -0700771/**
772 * set_sgttyb - set legacy terminal values
773 * @tty: tty structure
774 * @sgttyb: pointer to old style terminal structure
775 *
776 * Updates a terminal from the legacy BSD style terminal information
777 * structure.
778 *
Alan Coxd81ed102008-10-13 10:41:42 +0100779 * Locking: termios_mutex
Alan Coxaf9b8972006-08-27 01:24:01 -0700780 */
781
Alan Cox355d95a2008-02-08 04:18:48 -0800782static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 int retval;
785 struct sgttyb tmp;
Alan Coxedc6afc2006-12-08 02:38:44 -0800786 struct ktermios termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 retval = tty_check_change(tty);
789 if (retval)
790 return retval;
Alan Cox355d95a2008-02-08 04:18:48 -0800791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
793 return -EFAULT;
794
Arjan van de Ven5785c952006-09-29 02:00:43 -0700795 mutex_lock(&tty->termios_mutex);
Jiri Slaby68043962007-07-15 23:40:18 -0700796 termios = *tty->termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 termios.c_cc[VERASE] = tmp.sg_erase;
798 termios.c_cc[VKILL] = tmp.sg_kill;
799 set_sgflags(&termios, tmp.sg_flags);
Alan Coxedc6afc2006-12-08 02:38:44 -0800800 /* Try and encode into Bfoo format */
801#ifdef BOTHER
Alan Cox355d95a2008-02-08 04:18:48 -0800802 tty_termios_encode_baud_rate(&termios, termios.c_ispeed,
803 termios.c_ospeed);
Alan Coxedc6afc2006-12-08 02:38:44 -0800804#endif
Arjan van de Ven5785c952006-09-29 02:00:43 -0700805 mutex_unlock(&tty->termios_mutex);
Alan Cox8d075b12011-02-14 16:27:53 +0000806 tty_set_termios(tty, &termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return 0;
808}
809#endif
810
811#ifdef TIOCGETC
Alan Cox355d95a2008-02-08 04:18:48 -0800812static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 struct tchars tmp;
815
Alan Cox978e5952008-04-30 00:53:59 -0700816 mutex_lock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 tmp.t_intrc = tty->termios->c_cc[VINTR];
818 tmp.t_quitc = tty->termios->c_cc[VQUIT];
819 tmp.t_startc = tty->termios->c_cc[VSTART];
820 tmp.t_stopc = tty->termios->c_cc[VSTOP];
821 tmp.t_eofc = tty->termios->c_cc[VEOF];
822 tmp.t_brkc = tty->termios->c_cc[VEOL2]; /* what is brkc anyway? */
Alan Cox978e5952008-04-30 00:53:59 -0700823 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
825}
826
Alan Cox355d95a2008-02-08 04:18:48 -0800827static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 struct tchars tmp;
830
831 if (copy_from_user(&tmp, tchars, sizeof(tmp)))
832 return -EFAULT;
Alan Cox978e5952008-04-30 00:53:59 -0700833 mutex_lock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 tty->termios->c_cc[VINTR] = tmp.t_intrc;
835 tty->termios->c_cc[VQUIT] = tmp.t_quitc;
836 tty->termios->c_cc[VSTART] = tmp.t_startc;
837 tty->termios->c_cc[VSTOP] = tmp.t_stopc;
838 tty->termios->c_cc[VEOF] = tmp.t_eofc;
839 tty->termios->c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
Alan Cox978e5952008-04-30 00:53:59 -0700840 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return 0;
842}
843#endif
844
845#ifdef TIOCGLTC
Alan Cox355d95a2008-02-08 04:18:48 -0800846static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 struct ltchars tmp;
849
Alan Cox978e5952008-04-30 00:53:59 -0700850 mutex_lock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 tmp.t_suspc = tty->termios->c_cc[VSUSP];
Alan Cox355d95a2008-02-08 04:18:48 -0800852 /* what is dsuspc anyway? */
853 tmp.t_dsuspc = tty->termios->c_cc[VSUSP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 tmp.t_rprntc = tty->termios->c_cc[VREPRINT];
Alan Cox355d95a2008-02-08 04:18:48 -0800855 /* what is flushc anyway? */
856 tmp.t_flushc = tty->termios->c_cc[VEOL2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 tmp.t_werasc = tty->termios->c_cc[VWERASE];
858 tmp.t_lnextc = tty->termios->c_cc[VLNEXT];
Alan Cox978e5952008-04-30 00:53:59 -0700859 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
861}
862
Alan Cox355d95a2008-02-08 04:18:48 -0800863static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 struct ltchars tmp;
866
867 if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
868 return -EFAULT;
869
Alan Cox978e5952008-04-30 00:53:59 -0700870 mutex_lock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 tty->termios->c_cc[VSUSP] = tmp.t_suspc;
Alan Cox355d95a2008-02-08 04:18:48 -0800872 /* what is dsuspc anyway? */
873 tty->termios->c_cc[VEOL2] = tmp.t_dsuspc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 tty->termios->c_cc[VREPRINT] = tmp.t_rprntc;
Alan Cox355d95a2008-02-08 04:18:48 -0800875 /* what is flushc anyway? */
876 tty->termios->c_cc[VEOL2] = tmp.t_flushc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 tty->termios->c_cc[VWERASE] = tmp.t_werasc;
878 tty->termios->c_cc[VLNEXT] = tmp.t_lnextc;
Alan Cox978e5952008-04-30 00:53:59 -0700879 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return 0;
881}
882#endif
883
Alan Coxaf9b8972006-08-27 01:24:01 -0700884/**
885 * send_prio_char - send priority character
886 *
887 * Send a high priority character to the tty even if stopped
888 *
Alan Cox5f412b22006-09-29 02:01:40 -0700889 * Locking: none for xchar method, write ordering for write method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700891
Alan Cox5f412b22006-09-29 02:01:40 -0700892static int send_prio_char(struct tty_struct *tty, char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 int was_stopped = tty->stopped;
895
Alan Coxf34d7a52008-04-30 00:54:13 -0700896 if (tty->ops->send_xchar) {
897 tty->ops->send_xchar(tty, ch);
Alan Cox5f412b22006-09-29 02:01:40 -0700898 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
Alan Cox5f412b22006-09-29 02:01:40 -0700900
Alan Cox9c1729d2007-07-15 23:39:43 -0700901 if (tty_write_lock(tty, 0) < 0)
Alan Cox5f412b22006-09-29 02:01:40 -0700902 return -ERESTARTSYS;
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (was_stopped)
905 start_tty(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -0700906 tty->ops->write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (was_stopped)
908 stop_tty(tty);
Alan Cox9c1729d2007-07-15 23:39:43 -0700909 tty_write_unlock(tty);
Alan Cox5f412b22006-09-29 02:01:40 -0700910 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
Alan Cox0fc00e22007-11-07 01:24:56 -0800913/**
Alan Cox1c2630c2008-04-30 00:53:34 -0700914 * tty_change_softcar - carrier change ioctl helper
915 * @tty: tty to update
916 * @arg: enable/disable CLOCAL
917 *
918 * Perform a change to the CLOCAL state and call into the driver
919 * layer to make it visible. All done with the termios mutex
920 */
921
922static int tty_change_softcar(struct tty_struct *tty, int arg)
923{
924 int ret = 0;
925 int bit = arg ? CLOCAL : 0;
Alan Coxf34d7a52008-04-30 00:54:13 -0700926 struct ktermios old;
Alan Cox1c2630c2008-04-30 00:53:34 -0700927
928 mutex_lock(&tty->termios_mutex);
Alan Coxf34d7a52008-04-30 00:54:13 -0700929 old = *tty->termios;
Alan Cox1c2630c2008-04-30 00:53:34 -0700930 tty->termios->c_cflag &= ~CLOCAL;
931 tty->termios->c_cflag |= bit;
Alan Coxf34d7a52008-04-30 00:54:13 -0700932 if (tty->ops->set_termios)
933 tty->ops->set_termios(tty, &old);
Alan Cox1c2630c2008-04-30 00:53:34 -0700934 if ((tty->termios->c_cflag & CLOCAL) != bit)
935 ret = -EINVAL;
936 mutex_unlock(&tty->termios_mutex);
937 return ret;
938}
939
940/**
Alan Cox0fc00e22007-11-07 01:24:56 -0800941 * tty_mode_ioctl - mode related ioctls
942 * @tty: tty for the ioctl
943 * @file: file pointer for the tty
944 * @cmd: command
945 * @arg: ioctl argument
946 *
947 * Perform non line discipline specific mode control ioctls. This
948 * is designed to be called by line disciplines to ensure they provide
949 * consistent mode setting.
950 */
951
Alan Cox355d95a2008-02-08 04:18:48 -0800952int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
Alan Cox0fc00e22007-11-07 01:24:56 -0800953 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Alan Cox355d95a2008-02-08 04:18:48 -0800955 struct tty_struct *real_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 void __user *p = (void __user *)arg;
Alan Cox8f520022008-10-13 10:38:46 +0100957 int ret = 0;
Alan Cox26a2e202009-06-11 14:03:13 +0100958 struct ktermios kterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Alan Cox8d075b12011-02-14 16:27:53 +0000960 BUG_ON(file == NULL);
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
963 tty->driver->subtype == PTY_TYPE_MASTER)
964 real_tty = tty->link;
965 else
966 real_tty = tty;
967
968 switch (cmd) {
969#ifdef TIOCGETP
Alan Cox355d95a2008-02-08 04:18:48 -0800970 case TIOCGETP:
971 return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
972 case TIOCSETP:
973 case TIOCSETN:
974 return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975#endif
976#ifdef TIOCGETC
Alan Cox355d95a2008-02-08 04:18:48 -0800977 case TIOCGETC:
978 return get_tchars(real_tty, p);
979 case TIOCSETC:
980 return set_tchars(real_tty, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981#endif
982#ifdef TIOCGLTC
Alan Cox355d95a2008-02-08 04:18:48 -0800983 case TIOCGLTC:
984 return get_ltchars(real_tty, p);
985 case TIOCSLTC:
986 return set_ltchars(real_tty, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987#endif
Alan Cox355d95a2008-02-08 04:18:48 -0800988 case TCSETSF:
989 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
990 case TCSETSW:
991 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
992 case TCSETS:
993 return set_termios(real_tty, p, TERMIOS_OLD);
Alan Coxedc6afc2006-12-08 02:38:44 -0800994#ifndef TCGETS2
Alan Cox355d95a2008-02-08 04:18:48 -0800995 case TCGETS:
Alan Cox26a2e202009-06-11 14:03:13 +0100996 copy_termios(real_tty, &kterm);
997 if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
Alan Cox8f520022008-10-13 10:38:46 +0100998 ret = -EFAULT;
Alan Cox8f520022008-10-13 10:38:46 +0100999 return ret;
Alan Coxedc6afc2006-12-08 02:38:44 -08001000#else
Alan Cox355d95a2008-02-08 04:18:48 -08001001 case TCGETS:
Alan Cox26a2e202009-06-11 14:03:13 +01001002 copy_termios(real_tty, &kterm);
1003 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
Alan Cox8f520022008-10-13 10:38:46 +01001004 ret = -EFAULT;
Alan Cox8f520022008-10-13 10:38:46 +01001005 return ret;
Alan Cox355d95a2008-02-08 04:18:48 -08001006 case TCGETS2:
Alan Cox26a2e202009-06-11 14:03:13 +01001007 copy_termios(real_tty, &kterm);
1008 if (kernel_termios_to_user_termios((struct termios2 __user *)arg, &kterm))
Alan Cox8f520022008-10-13 10:38:46 +01001009 ret = -EFAULT;
Alan Cox8f520022008-10-13 10:38:46 +01001010 return ret;
Alan Cox355d95a2008-02-08 04:18:48 -08001011 case TCSETSF2:
1012 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
1013 case TCSETSW2:
1014 return set_termios(real_tty, p, TERMIOS_WAIT);
1015 case TCSETS2:
1016 return set_termios(real_tty, p, 0);
Alan Coxedc6afc2006-12-08 02:38:44 -08001017#endif
Alan Cox355d95a2008-02-08 04:18:48 -08001018 case TCGETA:
1019 return get_termio(real_tty, p);
1020 case TCSETAF:
1021 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
1022 case TCSETAW:
1023 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
1024 case TCSETA:
1025 return set_termios(real_tty, p, TERMIOS_TERMIO);
Alan Cox0fc00e22007-11-07 01:24:56 -08001026#ifndef TCGETS2
Alan Cox355d95a2008-02-08 04:18:48 -08001027 case TIOCGLCKTRMIOS:
Alan Cox26a2e202009-06-11 14:03:13 +01001028 copy_termios_locked(real_tty, &kterm);
1029 if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
Alan Cox8f520022008-10-13 10:38:46 +01001030 ret = -EFAULT;
Alan Cox8f520022008-10-13 10:38:46 +01001031 return ret;
Alan Cox355d95a2008-02-08 04:18:48 -08001032 case TIOCSLCKTRMIOS:
1033 if (!capable(CAP_SYS_ADMIN))
1034 return -EPERM;
Alan Cox26a2e202009-06-11 14:03:13 +01001035 copy_termios_locked(real_tty, &kterm);
1036 if (user_termios_to_kernel_termios(&kterm,
Alan Cox355d95a2008-02-08 04:18:48 -08001037 (struct termios __user *) arg))
Alan Cox26a2e202009-06-11 14:03:13 +01001038 return -EFAULT;
1039 mutex_lock(&real_tty->termios_mutex);
1040 memcpy(real_tty->termios_locked, &kterm, sizeof(struct ktermios));
Alan Cox8f520022008-10-13 10:38:46 +01001041 mutex_unlock(&real_tty->termios_mutex);
Alan Cox26a2e202009-06-11 14:03:13 +01001042 return 0;
Alan Cox0fc00e22007-11-07 01:24:56 -08001043#else
Alan Cox355d95a2008-02-08 04:18:48 -08001044 case TIOCGLCKTRMIOS:
Alan Cox26a2e202009-06-11 14:03:13 +01001045 copy_termios_locked(real_tty, &kterm);
1046 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
Alan Cox8f520022008-10-13 10:38:46 +01001047 ret = -EFAULT;
Alan Cox8f520022008-10-13 10:38:46 +01001048 return ret;
Alan Cox355d95a2008-02-08 04:18:48 -08001049 case TIOCSLCKTRMIOS:
1050 if (!capable(CAP_SYS_ADMIN))
Alan Cox26a2e202009-06-11 14:03:13 +01001051 return -EPERM;
1052 copy_termios_locked(real_tty, &kterm);
1053 if (user_termios_to_kernel_termios_1(&kterm,
Alan Cox355d95a2008-02-08 04:18:48 -08001054 (struct termios __user *) arg))
Alan Cox26a2e202009-06-11 14:03:13 +01001055 return -EFAULT;
1056 mutex_lock(&real_tty->termios_mutex);
1057 memcpy(real_tty->termios_locked, &kterm, sizeof(struct ktermios));
Alan Cox8f520022008-10-13 10:38:46 +01001058 mutex_unlock(&real_tty->termios_mutex);
1059 return ret;
Alan Cox0fc00e22007-11-07 01:24:56 -08001060#endif
Alan Cox1d65b4a2008-10-13 10:38:18 +01001061#ifdef TCGETX
Mike Frysinger5dca6072009-06-16 17:01:02 +01001062 case TCGETX: {
1063 struct termiox ktermx;
Alan Cox1d65b4a2008-10-13 10:38:18 +01001064 if (real_tty->termiox == NULL)
1065 return -EINVAL;
Alan Cox8f520022008-10-13 10:38:46 +01001066 mutex_lock(&real_tty->termios_mutex);
Alan Cox26a2e202009-06-11 14:03:13 +01001067 memcpy(&ktermx, real_tty->termiox, sizeof(struct termiox));
Alan Cox8f520022008-10-13 10:38:46 +01001068 mutex_unlock(&real_tty->termios_mutex);
Alan Cox26a2e202009-06-11 14:03:13 +01001069 if (copy_to_user(p, &ktermx, sizeof(struct termiox)))
1070 ret = -EFAULT;
Alan Cox8f520022008-10-13 10:38:46 +01001071 return ret;
Mike Frysinger5dca6072009-06-16 17:01:02 +01001072 }
Alan Cox1d65b4a2008-10-13 10:38:18 +01001073 case TCSETX:
1074 return set_termiox(real_tty, p, 0);
1075 case TCSETXW:
1076 return set_termiox(real_tty, p, TERMIOS_WAIT);
1077 case TCSETXF:
1078 return set_termiox(real_tty, p, TERMIOS_FLUSH);
1079#endif
Alan Cox355d95a2008-02-08 04:18:48 -08001080 case TIOCGSOFTCAR:
Alan Cox26a2e202009-06-11 14:03:13 +01001081 copy_termios(real_tty, &kterm);
1082 ret = put_user((kterm.c_cflag & CLOCAL) ? 1 : 0,
Alan Cox355d95a2008-02-08 04:18:48 -08001083 (int __user *)arg);
Alan Cox8f520022008-10-13 10:38:46 +01001084 return ret;
Alan Cox355d95a2008-02-08 04:18:48 -08001085 case TIOCSSOFTCAR:
1086 if (get_user(arg, (unsigned int __user *) arg))
1087 return -EFAULT;
Alan Coxf753f322008-08-26 19:52:47 +01001088 return tty_change_softcar(real_tty, arg);
Alan Cox355d95a2008-02-08 04:18:48 -08001089 default:
1090 return -ENOIOCTLCMD;
Alan Cox0fc00e22007-11-07 01:24:56 -08001091 }
1092}
Alan Cox0fc00e22007-11-07 01:24:56 -08001093EXPORT_SYMBOL_GPL(tty_mode_ioctl);
1094
1095int tty_perform_flush(struct tty_struct *tty, unsigned long arg)
1096{
1097 struct tty_ldisc *ld;
1098 int retval = tty_check_change(tty);
1099 if (retval)
1100 return retval;
1101
Alan Coxc0253ee2009-01-15 13:30:25 +00001102 ld = tty_ldisc_ref_wait(tty);
Alan Cox0fc00e22007-11-07 01:24:56 -08001103 switch (arg) {
1104 case TCIFLUSH:
Alan Coxa352def2008-07-16 21:53:12 +01001105 if (ld && ld->ops->flush_buffer)
1106 ld->ops->flush_buffer(tty);
Alan Cox0fc00e22007-11-07 01:24:56 -08001107 break;
1108 case TCIOFLUSH:
Alan Coxa352def2008-07-16 21:53:12 +01001109 if (ld && ld->ops->flush_buffer)
1110 ld->ops->flush_buffer(tty);
Alan Cox0fc00e22007-11-07 01:24:56 -08001111 /* fall through */
1112 case TCOFLUSH:
Alan Coxf34d7a52008-04-30 00:54:13 -07001113 tty_driver_flush_buffer(tty);
Alan Cox0fc00e22007-11-07 01:24:56 -08001114 break;
1115 default:
1116 tty_ldisc_deref(ld);
1117 return -EINVAL;
1118 }
1119 tty_ldisc_deref(ld);
1120 return 0;
1121}
Alan Cox0fc00e22007-11-07 01:24:56 -08001122EXPORT_SYMBOL_GPL(tty_perform_flush);
1123
Alan Cox47afa7a2008-10-13 10:44:17 +01001124int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file,
Alan Cox0fc00e22007-11-07 01:24:56 -08001125 unsigned int cmd, unsigned long arg)
1126{
Alan Cox04f378b2008-04-30 00:53:29 -07001127 unsigned long flags;
Alan Cox0fc00e22007-11-07 01:24:56 -08001128 int retval;
1129
Alan Cox0fc00e22007-11-07 01:24:56 -08001130 switch (cmd) {
Alan Cox355d95a2008-02-08 04:18:48 -08001131 case TCXONC:
1132 retval = tty_check_change(tty);
1133 if (retval)
1134 return retval;
1135 switch (arg) {
1136 case TCOOFF:
1137 if (!tty->flow_stopped) {
1138 tty->flow_stopped = 1;
1139 stop_tty(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
Alan Cox355d95a2008-02-08 04:18:48 -08001141 break;
1142 case TCOON:
1143 if (tty->flow_stopped) {
1144 tty->flow_stopped = 0;
1145 start_tty(tty);
1146 }
1147 break;
1148 case TCIOFF:
1149 if (STOP_CHAR(tty) != __DISABLED_CHAR)
1150 return send_prio_char(tty, STOP_CHAR(tty));
1151 break;
1152 case TCION:
1153 if (START_CHAR(tty) != __DISABLED_CHAR)
1154 return send_prio_char(tty, START_CHAR(tty));
1155 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 default:
Alan Cox355d95a2008-02-08 04:18:48 -08001157 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
Alan Cox355d95a2008-02-08 04:18:48 -08001159 return 0;
1160 case TCFLSH:
1161 return tty_perform_flush(tty, arg);
Alan Cox355d95a2008-02-08 04:18:48 -08001162 case TIOCPKT:
1163 {
1164 int pktmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Alan Cox355d95a2008-02-08 04:18:48 -08001166 if (tty->driver->type != TTY_DRIVER_TYPE_PTY ||
1167 tty->driver->subtype != PTY_TYPE_MASTER)
1168 return -ENOTTY;
1169 if (get_user(pktmode, (int __user *) arg))
1170 return -EFAULT;
Alan Cox04f378b2008-04-30 00:53:29 -07001171 spin_lock_irqsave(&tty->ctrl_lock, flags);
Alan Cox355d95a2008-02-08 04:18:48 -08001172 if (pktmode) {
1173 if (!tty->packet) {
1174 tty->packet = 1;
1175 tty->link->ctrl_status = 0;
1176 }
1177 } else
1178 tty->packet = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07001179 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Cox355d95a2008-02-08 04:18:48 -08001180 return 0;
1181 }
1182 default:
1183 /* Try the mode commands */
1184 return tty_mode_ioctl(tty, file, cmd, arg);
1185 }
1186}
Alan Cox47afa7a2008-10-13 10:44:17 +01001187EXPORT_SYMBOL(n_tty_ioctl_helper);
Thomas Meyer8193c422011-10-05 23:13:13 +02001188
1189#ifdef CONFIG_COMPAT
1190long n_tty_compat_ioctl_helper(struct tty_struct *tty, struct file *file,
1191 unsigned int cmd, unsigned long arg)
1192{
1193 switch (cmd) {
1194 case TIOCGLCKTRMIOS:
1195 case TIOCSLCKTRMIOS:
1196 return tty_mode_ioctl(tty, file, cmd, (unsigned long) compat_ptr(arg));
1197 default:
1198 return -ENOIOCTLCMD;
1199 }
1200}
1201EXPORT_SYMBOL(n_tty_compat_ioctl_helper);
1202#endif
1203