blob: 2401dbcbee9c170e2efdfe2bfbf474c61fbe2acc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/char/tty_ioctl.c
3 *
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 *
6 * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
7 * which can be dynamically activated and de-activated by the line
8 * discipline handling modules (like SLIP).
9 */
10
11#include <linux/types.h>
12#include <linux/termios.h>
13#include <linux/errno.h>
14#include <linux/sched.h>
15#include <linux/kernel.h>
16#include <linux/major.h>
17#include <linux/tty.h>
18#include <linux/fcntl.h>
19#include <linux/string.h>
20#include <linux/mm.h>
21#include <linux/module.h>
22#include <linux/bitops.h>
Arjan van de Ven5785c952006-09-29 02:00:43 -070023#include <linux/mutex.h>
Alan Cox0ee9cbb2008-04-30 00:53:32 -070024#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/io.h>
27#include <asm/uaccess.h>
28#include <asm/system.h>
29
30#undef TTY_DEBUG_WAIT_UNTIL_SENT
31
32#undef DEBUG
33
34/*
35 * Internal flag options for termios setting behavior
36 */
37#define TERMIOS_FLUSH 1
38#define TERMIOS_WAIT 2
39#define TERMIOS_TERMIO 4
Alan Coxedc6afc2006-12-08 02:38:44 -080040#define TERMIOS_OLD 8
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Alan Coxaf9b8972006-08-27 01:24:01 -070042
Alan Coxd81ed102008-10-13 10:41:42 +010043/**
44 * tty_chars_in_buffer - characters pending
45 * @tty: terminal
46 *
47 * Return the number of bytes of data in the device private
48 * output queue. If no private method is supplied there is assumed
49 * to be no queue on the device.
50 */
51
Alan Coxf34d7a52008-04-30 00:54:13 -070052int tty_chars_in_buffer(struct tty_struct *tty)
53{
54 if (tty->ops->chars_in_buffer)
55 return tty->ops->chars_in_buffer(tty);
56 else
57 return 0;
58}
Alan Coxf34d7a52008-04-30 00:54:13 -070059EXPORT_SYMBOL(tty_chars_in_buffer);
60
Alan Coxd81ed102008-10-13 10:41:42 +010061/**
62 * tty_write_room - write queue space
63 * @tty: terminal
64 *
65 * Return the number of bytes that can be queued to this device
66 * at the present time. The result should be treated as a guarantee
67 * and the driver cannot offer a value it later shrinks by more than
68 * the number of bytes written. If no method is provided 2K is always
69 * returned and data may be lost as there will be no flow control.
70 */
71
Alan Coxf34d7a52008-04-30 00:54:13 -070072int tty_write_room(struct tty_struct *tty)
73{
74 if (tty->ops->write_room)
75 return tty->ops->write_room(tty);
76 return 2048;
77}
Alan Coxf34d7a52008-04-30 00:54:13 -070078EXPORT_SYMBOL(tty_write_room);
79
Alan Coxd81ed102008-10-13 10:41:42 +010080/**
81 * tty_driver_flush_buffer - discard internal buffer
82 * @tty: terminal
83 *
84 * Discard the internal output buffer for this device. If no method
85 * is provided then either the buffer cannot be hardware flushed or
86 * there is no buffer driver side.
87 */
Alan Coxf34d7a52008-04-30 00:54:13 -070088void tty_driver_flush_buffer(struct tty_struct *tty)
89{
90 if (tty->ops->flush_buffer)
91 tty->ops->flush_buffer(tty);
92}
Alan Coxf34d7a52008-04-30 00:54:13 -070093EXPORT_SYMBOL(tty_driver_flush_buffer);
94
Alan Coxd81ed102008-10-13 10:41:42 +010095/**
96 * tty_throttle - flow control
97 * @tty: terminal
98 *
99 * Indicate that a tty should stop transmitting data down the stack.
Alan Cox38db8972009-06-11 12:44:17 +0100100 * Takes the termios mutex to protect against parallel throttle/unthrottle
101 * and also to ensure the driver can consistently reference its own
102 * termios data at this point when implementing software flow control.
Alan Coxd81ed102008-10-13 10:41:42 +0100103 */
104
Alan Cox39c2e602008-04-30 00:54:18 -0700105void tty_throttle(struct tty_struct *tty)
106{
Alan Cox38db8972009-06-11 12:44:17 +0100107 mutex_lock(&tty->termios_mutex);
Alan Cox39c2e602008-04-30 00:54:18 -0700108 /* check TTY_THROTTLED first so it indicates our state */
109 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
110 tty->ops->throttle)
111 tty->ops->throttle(tty);
Alan Cox38db8972009-06-11 12:44:17 +0100112 mutex_unlock(&tty->termios_mutex);
Alan Cox39c2e602008-04-30 00:54:18 -0700113}
114EXPORT_SYMBOL(tty_throttle);
115
Alan Coxd81ed102008-10-13 10:41:42 +0100116/**
117 * tty_unthrottle - flow control
118 * @tty: terminal
119 *
120 * Indicate that a tty may continue transmitting data down the stack.
Alan Cox38db8972009-06-11 12:44:17 +0100121 * Takes the termios mutex to protect against parallel throttle/unthrottle
122 * and also to ensure the driver can consistently reference its own
123 * termios data at this point when implementing software flow control.
124 *
125 * Drivers should however remember that the stack can issue a throttle,
126 * then change flow control method, then unthrottle.
Alan Coxd81ed102008-10-13 10:41:42 +0100127 */
128
Alan Cox39c2e602008-04-30 00:54:18 -0700129void tty_unthrottle(struct tty_struct *tty)
130{
Alan Cox38db8972009-06-11 12:44:17 +0100131 mutex_lock(&tty->termios_mutex);
Alan Cox39c2e602008-04-30 00:54:18 -0700132 if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
133 tty->ops->unthrottle)
134 tty->ops->unthrottle(tty);
Alan Cox38db8972009-06-11 12:44:17 +0100135 mutex_unlock(&tty->termios_mutex);
Alan Cox39c2e602008-04-30 00:54:18 -0700136}
137EXPORT_SYMBOL(tty_unthrottle);
Alan Coxf34d7a52008-04-30 00:54:13 -0700138
Alan Coxaf9b8972006-08-27 01:24:01 -0700139/**
140 * tty_wait_until_sent - wait for I/O to finish
141 * @tty: tty we are waiting for
142 * @timeout: how long we will wait
143 *
144 * Wait for characters pending in a tty driver to hit the wire, or
145 * for a timeout to occur (eg due to flow control)
146 *
147 * Locking: none
148 */
149
Alan Cox355d95a2008-02-08 04:18:48 -0800150void tty_wait_until_sent(struct tty_struct *tty, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#ifdef TTY_DEBUG_WAIT_UNTIL_SENT
153 char buf[64];
Alan Cox355d95a2008-02-08 04:18:48 -0800154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 printk(KERN_DEBUG "%s wait until sent...\n", tty_name(tty, buf));
156#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (!timeout)
158 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby5a52bd42007-07-15 23:40:18 -0700159 if (wait_event_interruptible_timeout(tty->write_wait,
Alan Coxf34d7a52008-04-30 00:54:13 -0700160 !tty_chars_in_buffer(tty), timeout) >= 0) {
161 if (tty->ops->wait_until_sent)
162 tty->ops->wait_until_sent(tty, timeout);
Alan Cox0ee9cbb2008-04-30 00:53:32 -0700163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165EXPORT_SYMBOL(tty_wait_until_sent);
166
Alan Coxd81ed102008-10-13 10:41:42 +0100167
168/*
169 * Termios Helper Methods
170 */
171
Alan Coxedc6afc2006-12-08 02:38:44 -0800172static void unset_locked_termios(struct ktermios *termios,
173 struct ktermios *old,
174 struct ktermios *locked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 int i;
Alan Cox355d95a2008-02-08 04:18:48 -0800177
178#define NOSET_MASK(x, y, z) (x = ((x) & ~(z)) | ((y) & (z)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 if (!locked) {
181 printk(KERN_WARNING "Warning?!? termios_locked is NULL.\n");
182 return;
183 }
184
185 NOSET_MASK(termios->c_iflag, old->c_iflag, locked->c_iflag);
186 NOSET_MASK(termios->c_oflag, old->c_oflag, locked->c_oflag);
187 NOSET_MASK(termios->c_cflag, old->c_cflag, locked->c_cflag);
188 NOSET_MASK(termios->c_lflag, old->c_lflag, locked->c_lflag);
189 termios->c_line = locked->c_line ? old->c_line : termios->c_line;
Alan Cox355d95a2008-02-08 04:18:48 -0800190 for (i = 0; i < NCCS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 termios->c_cc[i] = locked->c_cc[i] ?
192 old->c_cc[i] : termios->c_cc[i];
Alan Coxedc6afc2006-12-08 02:38:44 -0800193 /* FIXME: What should we do for i/ospeed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Alan Coxedc6afc2006-12-08 02:38:44 -0800196/*
197 * Routine which returns the baud rate of the tty
198 *
199 * Note that the baud_table needs to be kept in sync with the
200 * include/asm/termbits.h file.
201 */
202static const speed_t baud_table[] = {
203 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
204 9600, 19200, 38400, 57600, 115200, 230400, 460800,
205#ifdef __sparc__
206 76800, 153600, 307200, 614400, 921600
207#else
208 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
209 2500000, 3000000, 3500000, 4000000
210#endif
211};
212
213#ifndef __sparc__
214static const tcflag_t baud_bits[] = {
215 B0, B50, B75, B110, B134, B150, B200, B300, B600,
216 B1200, B1800, B2400, B4800, B9600, B19200, B38400,
217 B57600, B115200, B230400, B460800, B500000, B576000,
218 B921600, B1000000, B1152000, B1500000, B2000000, B2500000,
219 B3000000, B3500000, B4000000
220};
221#else
222static const tcflag_t baud_bits[] = {
223 B0, B50, B75, B110, B134, B150, B200, B300, B600,
224 B1200, B1800, B2400, B4800, B9600, B19200, B38400,
225 B57600, B115200, B230400, B460800, B76800, B153600,
226 B307200, B614400, B921600
227};
228#endif
229
230static int n_baud_table = ARRAY_SIZE(baud_table);
231
232/**
233 * tty_termios_baud_rate
234 * @termios: termios structure
235 *
236 * Convert termios baud rate data into a speed. This should be called
237 * with the termios lock held if this termios is a terminal termios
238 * structure. May change the termios data. Device drivers can call this
239 * function but should use ->c_[io]speed directly as they are updated.
240 *
241 * Locking: none
242 */
243
244speed_t tty_termios_baud_rate(struct ktermios *termios)
245{
246 unsigned int cbaud;
247
248 cbaud = termios->c_cflag & CBAUD;
249
250#ifdef BOTHER
251 /* Magic token for arbitary speed via c_ispeed/c_ospeed */
252 if (cbaud == BOTHER)
253 return termios->c_ospeed;
254#endif
255 if (cbaud & CBAUDEX) {
256 cbaud &= ~CBAUDEX;
257
258 if (cbaud < 1 || cbaud + 15 > n_baud_table)
259 termios->c_cflag &= ~CBAUDEX;
260 else
261 cbaud += 15;
262 }
263 return baud_table[cbaud];
264}
Alan Coxedc6afc2006-12-08 02:38:44 -0800265EXPORT_SYMBOL(tty_termios_baud_rate);
266
267/**
268 * tty_termios_input_baud_rate
269 * @termios: termios structure
270 *
271 * Convert termios baud rate data into a speed. This should be called
272 * with the termios lock held if this termios is a terminal termios
273 * structure. May change the termios data. Device drivers can call this
274 * function but should use ->c_[io]speed directly as they are updated.
275 *
276 * Locking: none
277 */
278
279speed_t tty_termios_input_baud_rate(struct ktermios *termios)
280{
281#ifdef IBSHIFT
282 unsigned int cbaud = (termios->c_cflag >> IBSHIFT) & CBAUD;
283
284 if (cbaud == B0)
285 return tty_termios_baud_rate(termios);
286
287 /* Magic token for arbitary speed via c_ispeed*/
288 if (cbaud == BOTHER)
289 return termios->c_ispeed;
290
291 if (cbaud & CBAUDEX) {
292 cbaud &= ~CBAUDEX;
293
294 if (cbaud < 1 || cbaud + 15 > n_baud_table)
295 termios->c_cflag &= ~(CBAUDEX << IBSHIFT);
296 else
297 cbaud += 15;
298 }
299 return baud_table[cbaud];
300#else
301 return tty_termios_baud_rate(termios);
302#endif
303}
Alan Coxedc6afc2006-12-08 02:38:44 -0800304EXPORT_SYMBOL(tty_termios_input_baud_rate);
305
Alan Coxedc6afc2006-12-08 02:38:44 -0800306/**
307 * tty_termios_encode_baud_rate
Alan Cox78137e32007-02-10 01:45:57 -0800308 * @termios: ktermios structure holding user requested state
Alan Coxedc6afc2006-12-08 02:38:44 -0800309 * @ispeed: input speed
310 * @ospeed: output speed
311 *
312 * Encode the speeds set into the passed termios structure. This is
313 * used as a library helper for drivers os that they can report back
314 * the actual speed selected when it differs from the speed requested
315 *
Alan Cox78137e32007-02-10 01:45:57 -0800316 * For maximal back compatibility with legacy SYS5/POSIX *nix behaviour
317 * we need to carefully set the bits when the user does not get the
318 * desired speed. We allow small margins and preserve as much of possible
319 * of the input intent to keep compatiblity.
Alan Coxedc6afc2006-12-08 02:38:44 -0800320 *
321 * Locking: Caller should hold termios lock. This is already held
322 * when calling this function from the driver termios handler.
Alan Cox5f519d72007-10-16 23:30:07 -0700323 *
324 * The ifdefs deal with platforms whose owners have yet to update them
325 * and will all go away once this is done.
Alan Coxedc6afc2006-12-08 02:38:44 -0800326 */
327
Maciej W. Rozycki75e8b712007-10-18 03:04:35 -0700328void tty_termios_encode_baud_rate(struct ktermios *termios,
329 speed_t ibaud, speed_t obaud)
Alan Coxedc6afc2006-12-08 02:38:44 -0800330{
331 int i = 0;
Alan Cox78137e32007-02-10 01:45:57 -0800332 int ifound = -1, ofound = -1;
333 int iclose = ibaud/50, oclose = obaud/50;
334 int ibinput = 0;
Alan Coxedc6afc2006-12-08 02:38:44 -0800335
Alan Cox5f519d72007-10-16 23:30:07 -0700336 if (obaud == 0) /* CD dropped */
337 ibaud = 0; /* Clear ibaud to be sure */
338
Alan Coxedc6afc2006-12-08 02:38:44 -0800339 termios->c_ispeed = ibaud;
340 termios->c_ospeed = obaud;
341
Alan Cox5f519d72007-10-16 23:30:07 -0700342#ifdef BOTHER
Alan Cox78137e32007-02-10 01:45:57 -0800343 /* If the user asked for a precise weird speed give a precise weird
344 answer. If they asked for a Bfoo speed they many have problems
345 digesting non-exact replies so fuzz a bit */
346
347 if ((termios->c_cflag & CBAUD) == BOTHER)
348 oclose = 0;
349 if (((termios->c_cflag >> IBSHIFT) & CBAUD) == BOTHER)
350 iclose = 0;
351 if ((termios->c_cflag >> IBSHIFT) & CBAUD)
352 ibinput = 1; /* An input speed was specified */
Alan Cox5f519d72007-10-16 23:30:07 -0700353#endif
Alan Coxedc6afc2006-12-08 02:38:44 -0800354 termios->c_cflag &= ~CBAUD;
Alan Coxedc6afc2006-12-08 02:38:44 -0800355
Alan Cox5f519d72007-10-16 23:30:07 -0700356 /*
357 * Our goal is to find a close match to the standard baud rate
358 * returned. Walk the baud rate table and if we get a very close
359 * match then report back the speed as a POSIX Bxxxx value by
360 * preference
361 */
362
Alan Coxedc6afc2006-12-08 02:38:44 -0800363 do {
Maciej W. Rozycki75e8b712007-10-18 03:04:35 -0700364 if (obaud - oclose <= baud_table[i] &&
365 obaud + oclose >= baud_table[i]) {
Alan Coxedc6afc2006-12-08 02:38:44 -0800366 termios->c_cflag |= baud_bits[i];
Alan Cox78137e32007-02-10 01:45:57 -0800367 ofound = i;
Alan Coxedc6afc2006-12-08 02:38:44 -0800368 }
Maciej W. Rozycki75e8b712007-10-18 03:04:35 -0700369 if (ibaud - iclose <= baud_table[i] &&
370 ibaud + iclose >= baud_table[i]) {
371 /* For the case input == output don't set IBAUD bits
372 if the user didn't do so */
Alan Cox5f519d72007-10-16 23:30:07 -0700373 if (ofound == i && !ibinput)
374 ifound = i;
375#ifdef IBSHIFT
376 else {
377 ifound = i;
Alan Cox78137e32007-02-10 01:45:57 -0800378 termios->c_cflag |= (baud_bits[i] << IBSHIFT);
Alan Cox5f519d72007-10-16 23:30:07 -0700379 }
380#endif
Alan Coxedc6afc2006-12-08 02:38:44 -0800381 }
Jiri Slaby68043962007-07-15 23:40:18 -0700382 } while (++i < n_baud_table);
Alan Cox5f519d72007-10-16 23:30:07 -0700383
384 /*
385 * If we found no match then use BOTHER if provided or warn
386 * the user their platform maintainer needs to wake up if not.
387 */
388#ifdef BOTHER
Alan Cox78137e32007-02-10 01:45:57 -0800389 if (ofound == -1)
Alan Coxedc6afc2006-12-08 02:38:44 -0800390 termios->c_cflag |= BOTHER;
Alan Cox78137e32007-02-10 01:45:57 -0800391 /* Set exact input bits only if the input and output differ or the
392 user already did */
Jiri Slaby68043962007-07-15 23:40:18 -0700393 if (ifound == -1 && (ibaud != obaud || ibinput))
Alan Coxedc6afc2006-12-08 02:38:44 -0800394 termios->c_cflag |= (BOTHER << IBSHIFT);
Alan Cox5f519d72007-10-16 23:30:07 -0700395#else
396 if (ifound == -1 || ofound == -1) {
397 static int warned;
398 if (!warned++)
399 printk(KERN_WARNING "tty: Unable to return correct "
400 "speed data as your architecture needs updating.\n");
401 }
402#endif
Alan Coxedc6afc2006-12-08 02:38:44 -0800403}
Alan Coxedc6afc2006-12-08 02:38:44 -0800404EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate);
405
Alan Coxd81ed102008-10-13 10:41:42 +0100406/**
407 * tty_encode_baud_rate - set baud rate of the tty
408 * @ibaud: input baud rate
409 * @obad: output baud rate
410 *
411 * Update the current termios data for the tty with the new speed
412 * settings. The caller must hold the termios_mutex for the tty in
413 * question.
414 */
415
Alan Cox5f519d72007-10-16 23:30:07 -0700416void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, speed_t obaud)
417{
418 tty_termios_encode_baud_rate(tty->termios, ibaud, obaud);
419}
420EXPORT_SYMBOL_GPL(tty_encode_baud_rate);
Alan Coxedc6afc2006-12-08 02:38:44 -0800421
422/**
423 * tty_get_baud_rate - get tty bit rates
424 * @tty: tty to query
425 *
426 * Returns the baud rate as an integer for this terminal. The
427 * termios lock must be held by the caller and the terminal bit
428 * flags may be updated.
429 *
430 * Locking: none
431 */
432
433speed_t tty_get_baud_rate(struct tty_struct *tty)
434{
435 speed_t baud = tty_termios_baud_rate(tty->termios);
436
437 if (baud == 38400 && tty->alt_speed) {
438 if (!tty->warned) {
439 printk(KERN_WARNING "Use of setserial/setrocket to "
440 "set SPD_* flags is deprecated\n");
441 tty->warned = 1;
442 }
443 baud = tty->alt_speed;
444 }
445
446 return baud;
447}
Alan Coxedc6afc2006-12-08 02:38:44 -0800448EXPORT_SYMBOL(tty_get_baud_rate);
449
Alan Coxaf9b8972006-08-27 01:24:01 -0700450/**
Alan Cox5f519d72007-10-16 23:30:07 -0700451 * tty_termios_copy_hw - copy hardware settings
452 * @new: New termios
453 * @old: Old termios
454 *
455 * Propogate the hardware specific terminal setting bits from
456 * the old termios structure to the new one. This is used in cases
457 * where the hardware does not support reconfiguration or as a helper
458 * in some cases where only minimal reconfiguration is supported
459 */
460
461void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old)
462{
463 /* The bits a dumb device handles in software. Smart devices need
464 to always provide a set_termios method */
465 new->c_cflag &= HUPCL | CREAD | CLOCAL;
466 new->c_cflag |= old->c_cflag & ~(HUPCL | CREAD | CLOCAL);
467 new->c_ispeed = old->c_ispeed;
468 new->c_ospeed = old->c_ospeed;
469}
Alan Cox5f519d72007-10-16 23:30:07 -0700470EXPORT_SYMBOL(tty_termios_copy_hw);
471
472/**
Alan Coxbf5e5832008-01-08 14:55:51 +0000473 * tty_termios_hw_change - check for setting change
474 * @a: termios
475 * @b: termios to compare
476 *
477 * Check if any of the bits that affect a dumb device have changed
478 * between the two termios structures, or a speed change is needed.
479 */
480
481int tty_termios_hw_change(struct ktermios *a, struct ktermios *b)
482{
483 if (a->c_ispeed != b->c_ispeed || a->c_ospeed != b->c_ospeed)
484 return 1;
485 if ((a->c_cflag ^ b->c_cflag) & ~(HUPCL | CREAD | CLOCAL))
486 return 1;
487 return 0;
488}
489EXPORT_SYMBOL(tty_termios_hw_change);
490
491/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700492 * change_termios - update termios values
493 * @tty: tty to update
494 * @new_termios: desired new value
495 *
496 * Perform updates to the termios values set on this terminal. There
497 * is a bit of layering violation here with n_tty in terms of the
498 * internal knowledge of this function.
499 *
Alan Coxd81ed102008-10-13 10:41:42 +0100500 * Locking: termios_mutex
Alan Coxaf9b8972006-08-27 01:24:01 -0700501 */
502
Alan Cox355d95a2008-02-08 04:18:48 -0800503static void change_termios(struct tty_struct *tty, struct ktermios *new_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Alan Cox978e5952008-04-30 00:53:59 -0700505 struct ktermios old_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 struct tty_ldisc *ld;
Alan Cox04f378b2008-04-30 00:53:29 -0700507 unsigned long flags;
Alan Cox355d95a2008-02-08 04:18:48 -0800508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 /*
510 * Perform the actual termios internal changes under lock.
511 */
Alan Cox355d95a2008-02-08 04:18:48 -0800512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 /* FIXME: we need to decide on some locking/ordering semantics
515 for the set_termios notification eventually */
Arjan van de Ven5785c952006-09-29 02:00:43 -0700516 mutex_lock(&tty->termios_mutex);
Alan Cox978e5952008-04-30 00:53:59 -0700517 old_termios = *tty->termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 *tty->termios = *new_termios;
519 unset_locked_termios(tty->termios, &old_termios, tty->termios_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 /* See if packet mode change of state. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (tty->link && tty->link->packet) {
523 int old_flow = ((old_termios.c_iflag & IXON) &&
524 (old_termios.c_cc[VSTOP] == '\023') &&
525 (old_termios.c_cc[VSTART] == '\021'));
526 int new_flow = (I_IXON(tty) &&
527 STOP_CHAR(tty) == '\023' &&
528 START_CHAR(tty) == '\021');
529 if (old_flow != new_flow) {
Alan Cox04f378b2008-04-30 00:53:29 -0700530 spin_lock_irqsave(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
532 if (new_flow)
533 tty->ctrl_status |= TIOCPKT_DOSTOP;
534 else
535 tty->ctrl_status |= TIOCPKT_NOSTOP;
Alan Cox04f378b2008-04-30 00:53:29 -0700536 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 wake_up_interruptible(&tty->link->read_wait);
538 }
539 }
Alan Cox355d95a2008-02-08 04:18:48 -0800540
Alan Coxf34d7a52008-04-30 00:54:13 -0700541 if (tty->ops->set_termios)
542 (*tty->ops->set_termios)(tty, &old_termios);
Alan Cox5f519d72007-10-16 23:30:07 -0700543 else
544 tty_termios_copy_hw(tty->termios, &old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 ld = tty_ldisc_ref(tty);
547 if (ld != NULL) {
Alan Coxa352def2008-07-16 21:53:12 +0100548 if (ld->ops->set_termios)
549 (ld->ops->set_termios)(tty, &old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 tty_ldisc_deref(ld);
551 }
Arjan van de Ven5785c952006-09-29 02:00:43 -0700552 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Alan Coxaf9b8972006-08-27 01:24:01 -0700555/**
556 * set_termios - set termios values for a tty
557 * @tty: terminal device
558 * @arg: user data
559 * @opt: option information
560 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200561 * Helper function to prepare termios data and run necessary other
Alan Coxaf9b8972006-08-27 01:24:01 -0700562 * functions before using change_termios to do the actual changes.
563 *
564 * Locking:
Alan Coxd81ed102008-10-13 10:41:42 +0100565 * Called functions take ldisc and termios_mutex locks
Alan Coxaf9b8972006-08-27 01:24:01 -0700566 */
567
Alan Cox355d95a2008-02-08 04:18:48 -0800568static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Alan Coxedc6afc2006-12-08 02:38:44 -0800570 struct ktermios tmp_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 struct tty_ldisc *ld;
572 int retval = tty_check_change(tty);
573
574 if (retval)
575 return retval;
576
Alan Cox978e5952008-04-30 00:53:59 -0700577 mutex_lock(&tty->termios_mutex);
Alan Cox64bb6c52006-12-08 02:38:47 -0800578 memcpy(&tmp_termios, tty->termios, sizeof(struct ktermios));
Alan Cox978e5952008-04-30 00:53:59 -0700579 mutex_unlock(&tty->termios_mutex);
Alan Cox64bb6c52006-12-08 02:38:47 -0800580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (opt & TERMIOS_TERMIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (user_termio_to_kernel_termios(&tmp_termios,
583 (struct termio __user *)arg))
584 return -EFAULT;
Alan Coxedc6afc2006-12-08 02:38:44 -0800585#ifdef TCGETS2
586 } else if (opt & TERMIOS_OLD) {
Alan Coxedc6afc2006-12-08 02:38:44 -0800587 if (user_termios_to_kernel_termios_1(&tmp_termios,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 (struct termios __user *)arg))
589 return -EFAULT;
Alan Cox64bb6c52006-12-08 02:38:47 -0800590 } else {
591 if (user_termios_to_kernel_termios(&tmp_termios,
592 (struct termios2 __user *)arg))
593 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Alan Cox64bb6c52006-12-08 02:38:47 -0800595#else
596 } else if (user_termios_to_kernel_termios(&tmp_termios,
597 (struct termios __user *)arg))
598 return -EFAULT;
599#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Alan Cox355d95a2008-02-08 04:18:48 -0800601 /* If old style Bfoo values are used then load c_ispeed/c_ospeed
602 * with the real speed so its unconditionally usable */
Alan Coxedc6afc2006-12-08 02:38:44 -0800603 tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios);
604 tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios);
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 ld = tty_ldisc_ref(tty);
Alan Cox355d95a2008-02-08 04:18:48 -0800607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (ld != NULL) {
Alan Coxa352def2008-07-16 21:53:12 +0100609 if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
610 ld->ops->flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 tty_ldisc_deref(ld);
612 }
Alan Cox355d95a2008-02-08 04:18:48 -0800613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (opt & TERMIOS_WAIT) {
615 tty_wait_until_sent(tty, 0);
616 if (signal_pending(current))
617 return -EINTR;
618 }
619
620 change_termios(tty, &tmp_termios);
Alan Cox5f519d72007-10-16 23:30:07 -0700621
622 /* FIXME: Arguably if tmp_termios == tty->termios AND the
623 actual requested termios was not tmp_termios then we may
624 want to return an error as no user requested change has
625 succeeded */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return 0;
627}
628
Alan Cox355d95a2008-02-08 04:18:48 -0800629static int get_termio(struct tty_struct *tty, struct termio __user *termio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 if (kernel_termios_to_user_termio(termio, tty->termios))
632 return -EFAULT;
633 return 0;
634}
635
Alan Cox1d65b4a2008-10-13 10:38:18 +0100636
637#ifdef TCGETX
638
639/**
640 * set_termiox - set termiox fields if possible
641 * @tty: terminal
642 * @arg: termiox structure from user
643 * @opt: option flags for ioctl type
644 *
645 * Implement the device calling points for the SYS5 termiox ioctl
646 * interface in Linux
647 */
648
649static int set_termiox(struct tty_struct *tty, void __user *arg, int opt)
650{
651 struct termiox tnew;
652 struct tty_ldisc *ld;
653
654 if (tty->termiox == NULL)
655 return -EINVAL;
656 if (copy_from_user(&tnew, arg, sizeof(struct termiox)))
657 return -EFAULT;
658
659 ld = tty_ldisc_ref(tty);
660 if (ld != NULL) {
661 if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
662 ld->ops->flush_buffer(tty);
663 tty_ldisc_deref(ld);
664 }
665 if (opt & TERMIOS_WAIT) {
666 tty_wait_until_sent(tty, 0);
667 if (signal_pending(current))
668 return -EINTR;
669 }
670
671 mutex_lock(&tty->termios_mutex);
672 if (tty->ops->set_termiox)
673 tty->ops->set_termiox(tty, &tnew);
674 mutex_unlock(&tty->termios_mutex);
675 return 0;
676}
677
678#endif
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681#ifdef TIOCGETP
682/*
683 * These are deprecated, but there is limited support..
684 *
685 * The "sg_flags" translation is a joke..
686 */
Alan Cox355d95a2008-02-08 04:18:48 -0800687static int get_sgflags(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 int flags = 0;
690
691 if (!(tty->termios->c_lflag & ICANON)) {
692 if (tty->termios->c_lflag & ISIG)
693 flags |= 0x02; /* cbreak */
694 else
695 flags |= 0x20; /* raw */
696 }
697 if (tty->termios->c_lflag & ECHO)
698 flags |= 0x08; /* echo */
699 if (tty->termios->c_oflag & OPOST)
700 if (tty->termios->c_oflag & ONLCR)
701 flags |= 0x10; /* crmod */
702 return flags;
703}
704
Alan Cox355d95a2008-02-08 04:18:48 -0800705static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 struct sgttyb tmp;
708
Arjan van de Ven5785c952006-09-29 02:00:43 -0700709 mutex_lock(&tty->termios_mutex);
Alan Cox606d0992006-12-08 02:38:45 -0800710 tmp.sg_ispeed = tty->termios->c_ispeed;
711 tmp.sg_ospeed = tty->termios->c_ospeed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 tmp.sg_erase = tty->termios->c_cc[VERASE];
713 tmp.sg_kill = tty->termios->c_cc[VKILL];
714 tmp.sg_flags = get_sgflags(tty);
Arjan van de Ven5785c952006-09-29 02:00:43 -0700715 mutex_unlock(&tty->termios_mutex);
Alan Cox355d95a2008-02-08 04:18:48 -0800716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
718}
719
Alan Cox355d95a2008-02-08 04:18:48 -0800720static void set_sgflags(struct ktermios *termios, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 termios->c_iflag = ICRNL | IXON;
723 termios->c_oflag = 0;
724 termios->c_lflag = ISIG | ICANON;
725 if (flags & 0x02) { /* cbreak */
726 termios->c_iflag = 0;
727 termios->c_lflag &= ~ICANON;
728 }
729 if (flags & 0x08) { /* echo */
730 termios->c_lflag |= ECHO | ECHOE | ECHOK |
731 ECHOCTL | ECHOKE | IEXTEN;
732 }
733 if (flags & 0x10) { /* crmod */
734 termios->c_oflag |= OPOST | ONLCR;
735 }
736 if (flags & 0x20) { /* raw */
737 termios->c_iflag = 0;
738 termios->c_lflag &= ~(ISIG | ICANON);
739 }
740 if (!(termios->c_lflag & ICANON)) {
741 termios->c_cc[VMIN] = 1;
742 termios->c_cc[VTIME] = 0;
743 }
744}
745
Alan Coxaf9b8972006-08-27 01:24:01 -0700746/**
747 * set_sgttyb - set legacy terminal values
748 * @tty: tty structure
749 * @sgttyb: pointer to old style terminal structure
750 *
751 * Updates a terminal from the legacy BSD style terminal information
752 * structure.
753 *
Alan Coxd81ed102008-10-13 10:41:42 +0100754 * Locking: termios_mutex
Alan Coxaf9b8972006-08-27 01:24:01 -0700755 */
756
Alan Cox355d95a2008-02-08 04:18:48 -0800757static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 int retval;
760 struct sgttyb tmp;
Alan Coxedc6afc2006-12-08 02:38:44 -0800761 struct ktermios termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 retval = tty_check_change(tty);
764 if (retval)
765 return retval;
Alan Cox355d95a2008-02-08 04:18:48 -0800766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
768 return -EFAULT;
769
Arjan van de Ven5785c952006-09-29 02:00:43 -0700770 mutex_lock(&tty->termios_mutex);
Jiri Slaby68043962007-07-15 23:40:18 -0700771 termios = *tty->termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 termios.c_cc[VERASE] = tmp.sg_erase;
773 termios.c_cc[VKILL] = tmp.sg_kill;
774 set_sgflags(&termios, tmp.sg_flags);
Alan Coxedc6afc2006-12-08 02:38:44 -0800775 /* Try and encode into Bfoo format */
776#ifdef BOTHER
Alan Cox355d95a2008-02-08 04:18:48 -0800777 tty_termios_encode_baud_rate(&termios, termios.c_ispeed,
778 termios.c_ospeed);
Alan Coxedc6afc2006-12-08 02:38:44 -0800779#endif
Arjan van de Ven5785c952006-09-29 02:00:43 -0700780 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 change_termios(tty, &termios);
782 return 0;
783}
784#endif
785
786#ifdef TIOCGETC
Alan Cox355d95a2008-02-08 04:18:48 -0800787static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 struct tchars tmp;
790
Alan Cox978e5952008-04-30 00:53:59 -0700791 mutex_lock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 tmp.t_intrc = tty->termios->c_cc[VINTR];
793 tmp.t_quitc = tty->termios->c_cc[VQUIT];
794 tmp.t_startc = tty->termios->c_cc[VSTART];
795 tmp.t_stopc = tty->termios->c_cc[VSTOP];
796 tmp.t_eofc = tty->termios->c_cc[VEOF];
797 tmp.t_brkc = tty->termios->c_cc[VEOL2]; /* what is brkc anyway? */
Alan Cox978e5952008-04-30 00:53:59 -0700798 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
800}
801
Alan Cox355d95a2008-02-08 04:18:48 -0800802static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804 struct tchars tmp;
805
806 if (copy_from_user(&tmp, tchars, sizeof(tmp)))
807 return -EFAULT;
Alan Cox978e5952008-04-30 00:53:59 -0700808 mutex_lock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 tty->termios->c_cc[VINTR] = tmp.t_intrc;
810 tty->termios->c_cc[VQUIT] = tmp.t_quitc;
811 tty->termios->c_cc[VSTART] = tmp.t_startc;
812 tty->termios->c_cc[VSTOP] = tmp.t_stopc;
813 tty->termios->c_cc[VEOF] = tmp.t_eofc;
814 tty->termios->c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
Alan Cox978e5952008-04-30 00:53:59 -0700815 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return 0;
817}
818#endif
819
820#ifdef TIOCGLTC
Alan Cox355d95a2008-02-08 04:18:48 -0800821static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 struct ltchars tmp;
824
Alan Cox978e5952008-04-30 00:53:59 -0700825 mutex_lock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 tmp.t_suspc = tty->termios->c_cc[VSUSP];
Alan Cox355d95a2008-02-08 04:18:48 -0800827 /* what is dsuspc anyway? */
828 tmp.t_dsuspc = tty->termios->c_cc[VSUSP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 tmp.t_rprntc = tty->termios->c_cc[VREPRINT];
Alan Cox355d95a2008-02-08 04:18:48 -0800830 /* what is flushc anyway? */
831 tmp.t_flushc = tty->termios->c_cc[VEOL2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 tmp.t_werasc = tty->termios->c_cc[VWERASE];
833 tmp.t_lnextc = tty->termios->c_cc[VLNEXT];
Alan Cox978e5952008-04-30 00:53:59 -0700834 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
836}
837
Alan Cox355d95a2008-02-08 04:18:48 -0800838static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 struct ltchars tmp;
841
842 if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
843 return -EFAULT;
844
Alan Cox978e5952008-04-30 00:53:59 -0700845 mutex_lock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 tty->termios->c_cc[VSUSP] = tmp.t_suspc;
Alan Cox355d95a2008-02-08 04:18:48 -0800847 /* what is dsuspc anyway? */
848 tty->termios->c_cc[VEOL2] = tmp.t_dsuspc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 tty->termios->c_cc[VREPRINT] = tmp.t_rprntc;
Alan Cox355d95a2008-02-08 04:18:48 -0800850 /* what is flushc anyway? */
851 tty->termios->c_cc[VEOL2] = tmp.t_flushc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 tty->termios->c_cc[VWERASE] = tmp.t_werasc;
853 tty->termios->c_cc[VLNEXT] = tmp.t_lnextc;
Alan Cox978e5952008-04-30 00:53:59 -0700854 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return 0;
856}
857#endif
858
Alan Coxaf9b8972006-08-27 01:24:01 -0700859/**
860 * send_prio_char - send priority character
861 *
862 * Send a high priority character to the tty even if stopped
863 *
Alan Cox5f412b22006-09-29 02:01:40 -0700864 * Locking: none for xchar method, write ordering for write method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700866
Alan Cox5f412b22006-09-29 02:01:40 -0700867static int send_prio_char(struct tty_struct *tty, char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
869 int was_stopped = tty->stopped;
870
Alan Coxf34d7a52008-04-30 00:54:13 -0700871 if (tty->ops->send_xchar) {
872 tty->ops->send_xchar(tty, ch);
Alan Cox5f412b22006-09-29 02:01:40 -0700873 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
Alan Cox5f412b22006-09-29 02:01:40 -0700875
Alan Cox9c1729d2007-07-15 23:39:43 -0700876 if (tty_write_lock(tty, 0) < 0)
Alan Cox5f412b22006-09-29 02:01:40 -0700877 return -ERESTARTSYS;
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (was_stopped)
880 start_tty(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -0700881 tty->ops->write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (was_stopped)
883 stop_tty(tty);
Alan Cox9c1729d2007-07-15 23:39:43 -0700884 tty_write_unlock(tty);
Alan Cox5f412b22006-09-29 02:01:40 -0700885 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
Alan Cox0fc00e22007-11-07 01:24:56 -0800888/**
Alan Cox1c2630c2008-04-30 00:53:34 -0700889 * tty_change_softcar - carrier change ioctl helper
890 * @tty: tty to update
891 * @arg: enable/disable CLOCAL
892 *
893 * Perform a change to the CLOCAL state and call into the driver
894 * layer to make it visible. All done with the termios mutex
895 */
896
897static int tty_change_softcar(struct tty_struct *tty, int arg)
898{
899 int ret = 0;
900 int bit = arg ? CLOCAL : 0;
Alan Coxf34d7a52008-04-30 00:54:13 -0700901 struct ktermios old;
Alan Cox1c2630c2008-04-30 00:53:34 -0700902
903 mutex_lock(&tty->termios_mutex);
Alan Coxf34d7a52008-04-30 00:54:13 -0700904 old = *tty->termios;
Alan Cox1c2630c2008-04-30 00:53:34 -0700905 tty->termios->c_cflag &= ~CLOCAL;
906 tty->termios->c_cflag |= bit;
Alan Coxf34d7a52008-04-30 00:54:13 -0700907 if (tty->ops->set_termios)
908 tty->ops->set_termios(tty, &old);
Alan Cox1c2630c2008-04-30 00:53:34 -0700909 if ((tty->termios->c_cflag & CLOCAL) != bit)
910 ret = -EINVAL;
911 mutex_unlock(&tty->termios_mutex);
912 return ret;
913}
914
915/**
Alan Cox0fc00e22007-11-07 01:24:56 -0800916 * tty_mode_ioctl - mode related ioctls
917 * @tty: tty for the ioctl
918 * @file: file pointer for the tty
919 * @cmd: command
920 * @arg: ioctl argument
921 *
922 * Perform non line discipline specific mode control ioctls. This
923 * is designed to be called by line disciplines to ensure they provide
924 * consistent mode setting.
925 */
926
Alan Cox355d95a2008-02-08 04:18:48 -0800927int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
Alan Cox0fc00e22007-11-07 01:24:56 -0800928 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Alan Cox355d95a2008-02-08 04:18:48 -0800930 struct tty_struct *real_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 void __user *p = (void __user *)arg;
Alan Cox8f520022008-10-13 10:38:46 +0100932 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
935 tty->driver->subtype == PTY_TYPE_MASTER)
936 real_tty = tty->link;
937 else
938 real_tty = tty;
939
940 switch (cmd) {
941#ifdef TIOCGETP
Alan Cox355d95a2008-02-08 04:18:48 -0800942 case TIOCGETP:
943 return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
944 case TIOCSETP:
945 case TIOCSETN:
946 return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947#endif
948#ifdef TIOCGETC
Alan Cox355d95a2008-02-08 04:18:48 -0800949 case TIOCGETC:
950 return get_tchars(real_tty, p);
951 case TIOCSETC:
952 return set_tchars(real_tty, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953#endif
954#ifdef TIOCGLTC
Alan Cox355d95a2008-02-08 04:18:48 -0800955 case TIOCGLTC:
956 return get_ltchars(real_tty, p);
957 case TIOCSLTC:
958 return set_ltchars(real_tty, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959#endif
Alan Cox355d95a2008-02-08 04:18:48 -0800960 case TCSETSF:
961 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
962 case TCSETSW:
963 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
964 case TCSETS:
965 return set_termios(real_tty, p, TERMIOS_OLD);
Alan Coxedc6afc2006-12-08 02:38:44 -0800966#ifndef TCGETS2
Alan Cox355d95a2008-02-08 04:18:48 -0800967 case TCGETS:
Alan Cox8f520022008-10-13 10:38:46 +0100968 mutex_lock(&real_tty->termios_mutex);
Alan Cox355d95a2008-02-08 04:18:48 -0800969 if (kernel_termios_to_user_termios((struct termios __user *)arg, real_tty->termios))
Alan Cox8f520022008-10-13 10:38:46 +0100970 ret = -EFAULT;
971 mutex_unlock(&real_tty->termios_mutex);
972 return ret;
Alan Coxedc6afc2006-12-08 02:38:44 -0800973#else
Alan Cox355d95a2008-02-08 04:18:48 -0800974 case TCGETS:
Alan Cox8f520022008-10-13 10:38:46 +0100975 mutex_lock(&real_tty->termios_mutex);
Alan Cox355d95a2008-02-08 04:18:48 -0800976 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, real_tty->termios))
Alan Cox8f520022008-10-13 10:38:46 +0100977 ret = -EFAULT;
978 mutex_unlock(&real_tty->termios_mutex);
979 return ret;
Alan Cox355d95a2008-02-08 04:18:48 -0800980 case TCGETS2:
Alan Cox8f520022008-10-13 10:38:46 +0100981 mutex_lock(&real_tty->termios_mutex);
Alan Cox355d95a2008-02-08 04:18:48 -0800982 if (kernel_termios_to_user_termios((struct termios2 __user *)arg, real_tty->termios))
Alan Cox8f520022008-10-13 10:38:46 +0100983 ret = -EFAULT;
984 mutex_unlock(&real_tty->termios_mutex);
985 return ret;
Alan Cox355d95a2008-02-08 04:18:48 -0800986 case TCSETSF2:
987 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
988 case TCSETSW2:
989 return set_termios(real_tty, p, TERMIOS_WAIT);
990 case TCSETS2:
991 return set_termios(real_tty, p, 0);
Alan Coxedc6afc2006-12-08 02:38:44 -0800992#endif
Alan Cox355d95a2008-02-08 04:18:48 -0800993 case TCGETA:
994 return get_termio(real_tty, p);
995 case TCSETAF:
996 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
997 case TCSETAW:
998 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
999 case TCSETA:
1000 return set_termios(real_tty, p, TERMIOS_TERMIO);
Alan Cox0fc00e22007-11-07 01:24:56 -08001001#ifndef TCGETS2
Alan Cox355d95a2008-02-08 04:18:48 -08001002 case TIOCGLCKTRMIOS:
Alan Cox8f520022008-10-13 10:38:46 +01001003 mutex_lock(&real_tty->termios_mutex);
Alan Cox355d95a2008-02-08 04:18:48 -08001004 if (kernel_termios_to_user_termios((struct termios __user *)arg, real_tty->termios_locked))
Alan Cox8f520022008-10-13 10:38:46 +01001005 ret = -EFAULT;
1006 mutex_unlock(&real_tty->termios_mutex);
1007 return ret;
Alan Cox355d95a2008-02-08 04:18:48 -08001008 case TIOCSLCKTRMIOS:
1009 if (!capable(CAP_SYS_ADMIN))
1010 return -EPERM;
Alan Cox8f520022008-10-13 10:38:46 +01001011 mutex_lock(&real_tty->termios_mutex);
Alan Cox355d95a2008-02-08 04:18:48 -08001012 if (user_termios_to_kernel_termios(real_tty->termios_locked,
1013 (struct termios __user *) arg))
Alan Cox8f520022008-10-13 10:38:46 +01001014 ret = -EFAULT;
1015 mutex_unlock(&real_tty->termios_mutex);
1016 return ret;
Alan Cox0fc00e22007-11-07 01:24:56 -08001017#else
Alan Cox355d95a2008-02-08 04:18:48 -08001018 case TIOCGLCKTRMIOS:
Alan Cox8f520022008-10-13 10:38:46 +01001019 mutex_lock(&real_tty->termios_mutex);
Alan Cox355d95a2008-02-08 04:18:48 -08001020 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, real_tty->termios_locked))
Alan Cox8f520022008-10-13 10:38:46 +01001021 ret = -EFAULT;
1022 mutex_unlock(&real_tty->termios_mutex);
1023 return ret;
Alan Cox355d95a2008-02-08 04:18:48 -08001024 case TIOCSLCKTRMIOS:
1025 if (!capable(CAP_SYS_ADMIN))
Alan Cox8f520022008-10-13 10:38:46 +01001026 ret = -EPERM;
1027 mutex_lock(&real_tty->termios_mutex);
Alan Cox355d95a2008-02-08 04:18:48 -08001028 if (user_termios_to_kernel_termios_1(real_tty->termios_locked,
1029 (struct termios __user *) arg))
Alan Cox8f520022008-10-13 10:38:46 +01001030 ret = -EFAULT;
1031 mutex_unlock(&real_tty->termios_mutex);
1032 return ret;
Alan Cox0fc00e22007-11-07 01:24:56 -08001033#endif
Alan Cox1d65b4a2008-10-13 10:38:18 +01001034#ifdef TCGETX
1035 case TCGETX:
1036 if (real_tty->termiox == NULL)
1037 return -EINVAL;
Alan Cox8f520022008-10-13 10:38:46 +01001038 mutex_lock(&real_tty->termios_mutex);
Alan Cox1d65b4a2008-10-13 10:38:18 +01001039 if (copy_to_user(p, real_tty->termiox, sizeof(struct termiox)))
Alan Cox8f520022008-10-13 10:38:46 +01001040 ret = -EFAULT;
1041 mutex_unlock(&real_tty->termios_mutex);
1042 return ret;
Alan Cox1d65b4a2008-10-13 10:38:18 +01001043 case TCSETX:
1044 return set_termiox(real_tty, p, 0);
1045 case TCSETXW:
1046 return set_termiox(real_tty, p, TERMIOS_WAIT);
1047 case TCSETXF:
1048 return set_termiox(real_tty, p, TERMIOS_FLUSH);
1049#endif
Alan Cox355d95a2008-02-08 04:18:48 -08001050 case TIOCGSOFTCAR:
Alan Cox8f520022008-10-13 10:38:46 +01001051 mutex_lock(&real_tty->termios_mutex);
1052 ret = put_user(C_CLOCAL(real_tty) ? 1 : 0,
Alan Cox355d95a2008-02-08 04:18:48 -08001053 (int __user *)arg);
Alan Cox8f520022008-10-13 10:38:46 +01001054 mutex_unlock(&real_tty->termios_mutex);
1055 return ret;
Alan Cox355d95a2008-02-08 04:18:48 -08001056 case TIOCSSOFTCAR:
1057 if (get_user(arg, (unsigned int __user *) arg))
1058 return -EFAULT;
Alan Coxf753f322008-08-26 19:52:47 +01001059 return tty_change_softcar(real_tty, arg);
Alan Cox355d95a2008-02-08 04:18:48 -08001060 default:
1061 return -ENOIOCTLCMD;
Alan Cox0fc00e22007-11-07 01:24:56 -08001062 }
1063}
Alan Cox0fc00e22007-11-07 01:24:56 -08001064EXPORT_SYMBOL_GPL(tty_mode_ioctl);
1065
1066int tty_perform_flush(struct tty_struct *tty, unsigned long arg)
1067{
1068 struct tty_ldisc *ld;
1069 int retval = tty_check_change(tty);
1070 if (retval)
1071 return retval;
1072
Alan Coxc0253ee2009-01-15 13:30:25 +00001073 ld = tty_ldisc_ref_wait(tty);
Alan Cox0fc00e22007-11-07 01:24:56 -08001074 switch (arg) {
1075 case TCIFLUSH:
Alan Coxa352def2008-07-16 21:53:12 +01001076 if (ld && ld->ops->flush_buffer)
1077 ld->ops->flush_buffer(tty);
Alan Cox0fc00e22007-11-07 01:24:56 -08001078 break;
1079 case TCIOFLUSH:
Alan Coxa352def2008-07-16 21:53:12 +01001080 if (ld && ld->ops->flush_buffer)
1081 ld->ops->flush_buffer(tty);
Alan Cox0fc00e22007-11-07 01:24:56 -08001082 /* fall through */
1083 case TCOFLUSH:
Alan Coxf34d7a52008-04-30 00:54:13 -07001084 tty_driver_flush_buffer(tty);
Alan Cox0fc00e22007-11-07 01:24:56 -08001085 break;
1086 default:
1087 tty_ldisc_deref(ld);
1088 return -EINVAL;
1089 }
1090 tty_ldisc_deref(ld);
1091 return 0;
1092}
Alan Cox0fc00e22007-11-07 01:24:56 -08001093EXPORT_SYMBOL_GPL(tty_perform_flush);
1094
Alan Cox47afa7a2008-10-13 10:44:17 +01001095int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file,
Alan Cox0fc00e22007-11-07 01:24:56 -08001096 unsigned int cmd, unsigned long arg)
1097{
Alan Cox04f378b2008-04-30 00:53:29 -07001098 unsigned long flags;
Alan Cox0fc00e22007-11-07 01:24:56 -08001099 int retval;
1100
Alan Cox0fc00e22007-11-07 01:24:56 -08001101 switch (cmd) {
Alan Cox355d95a2008-02-08 04:18:48 -08001102 case TCXONC:
1103 retval = tty_check_change(tty);
1104 if (retval)
1105 return retval;
1106 switch (arg) {
1107 case TCOOFF:
1108 if (!tty->flow_stopped) {
1109 tty->flow_stopped = 1;
1110 stop_tty(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
Alan Cox355d95a2008-02-08 04:18:48 -08001112 break;
1113 case TCOON:
1114 if (tty->flow_stopped) {
1115 tty->flow_stopped = 0;
1116 start_tty(tty);
1117 }
1118 break;
1119 case TCIOFF:
1120 if (STOP_CHAR(tty) != __DISABLED_CHAR)
1121 return send_prio_char(tty, STOP_CHAR(tty));
1122 break;
1123 case TCION:
1124 if (START_CHAR(tty) != __DISABLED_CHAR)
1125 return send_prio_char(tty, START_CHAR(tty));
1126 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 default:
Alan Cox355d95a2008-02-08 04:18:48 -08001128 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
Alan Cox355d95a2008-02-08 04:18:48 -08001130 return 0;
1131 case TCFLSH:
1132 return tty_perform_flush(tty, arg);
Alan Cox355d95a2008-02-08 04:18:48 -08001133 case TIOCPKT:
1134 {
1135 int pktmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Alan Cox355d95a2008-02-08 04:18:48 -08001137 if (tty->driver->type != TTY_DRIVER_TYPE_PTY ||
1138 tty->driver->subtype != PTY_TYPE_MASTER)
1139 return -ENOTTY;
1140 if (get_user(pktmode, (int __user *) arg))
1141 return -EFAULT;
Alan Cox04f378b2008-04-30 00:53:29 -07001142 spin_lock_irqsave(&tty->ctrl_lock, flags);
Alan Cox355d95a2008-02-08 04:18:48 -08001143 if (pktmode) {
1144 if (!tty->packet) {
1145 tty->packet = 1;
1146 tty->link->ctrl_status = 0;
1147 }
1148 } else
1149 tty->packet = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07001150 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Cox355d95a2008-02-08 04:18:48 -08001151 return 0;
1152 }
1153 default:
1154 /* Try the mode commands */
1155 return tty_mode_ioctl(tty, file, cmd, arg);
1156 }
1157}
Alan Cox47afa7a2008-10-13 10:44:17 +01001158EXPORT_SYMBOL(n_tty_ioctl_helper);