blob: 4d540619ac843ded9b54915fe036ad32ab7c63c9 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/io.h>
26#include <asm/uaccess.h>
27#include <asm/system.h>
28
29#undef TTY_DEBUG_WAIT_UNTIL_SENT
30
31#undef DEBUG
32
33/*
34 * Internal flag options for termios setting behavior
35 */
36#define TERMIOS_FLUSH 1
37#define TERMIOS_WAIT 2
38#define TERMIOS_TERMIO 4
39
Alan Coxaf9b8972006-08-27 01:24:01 -070040
41/**
42 * tty_wait_until_sent - wait for I/O to finish
43 * @tty: tty we are waiting for
44 * @timeout: how long we will wait
45 *
46 * Wait for characters pending in a tty driver to hit the wire, or
47 * for a timeout to occur (eg due to flow control)
48 *
49 * Locking: none
50 */
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052void tty_wait_until_sent(struct tty_struct * tty, long timeout)
53{
54 DECLARE_WAITQUEUE(wait, current);
55
56#ifdef TTY_DEBUG_WAIT_UNTIL_SENT
57 char buf[64];
58
59 printk(KERN_DEBUG "%s wait until sent...\n", tty_name(tty, buf));
60#endif
61 if (!tty->driver->chars_in_buffer)
62 return;
63 add_wait_queue(&tty->write_wait, &wait);
64 if (!timeout)
65 timeout = MAX_SCHEDULE_TIMEOUT;
66 do {
67#ifdef TTY_DEBUG_WAIT_UNTIL_SENT
68 printk(KERN_DEBUG "waiting %s...(%d)\n", tty_name(tty, buf),
69 tty->driver->chars_in_buffer(tty));
70#endif
71 set_current_state(TASK_INTERRUPTIBLE);
72 if (signal_pending(current))
73 goto stop_waiting;
74 if (!tty->driver->chars_in_buffer(tty))
75 break;
76 timeout = schedule_timeout(timeout);
77 } while (timeout);
78 if (tty->driver->wait_until_sent)
79 tty->driver->wait_until_sent(tty, timeout);
80stop_waiting:
81 set_current_state(TASK_RUNNING);
82 remove_wait_queue(&tty->write_wait, &wait);
83}
84
85EXPORT_SYMBOL(tty_wait_until_sent);
86
87static void unset_locked_termios(struct termios *termios,
88 struct termios *old,
89 struct termios *locked)
90{
91 int i;
92
93#define NOSET_MASK(x,y,z) (x = ((x) & ~(z)) | ((y) & (z)))
94
95 if (!locked) {
96 printk(KERN_WARNING "Warning?!? termios_locked is NULL.\n");
97 return;
98 }
99
100 NOSET_MASK(termios->c_iflag, old->c_iflag, locked->c_iflag);
101 NOSET_MASK(termios->c_oflag, old->c_oflag, locked->c_oflag);
102 NOSET_MASK(termios->c_cflag, old->c_cflag, locked->c_cflag);
103 NOSET_MASK(termios->c_lflag, old->c_lflag, locked->c_lflag);
104 termios->c_line = locked->c_line ? old->c_line : termios->c_line;
105 for (i=0; i < NCCS; i++)
106 termios->c_cc[i] = locked->c_cc[i] ?
107 old->c_cc[i] : termios->c_cc[i];
108}
109
Alan Coxaf9b8972006-08-27 01:24:01 -0700110/**
111 * change_termios - update termios values
112 * @tty: tty to update
113 * @new_termios: desired new value
114 *
115 * Perform updates to the termios values set on this terminal. There
116 * is a bit of layering violation here with n_tty in terms of the
117 * internal knowledge of this function.
118 *
119 * Locking: termios_sem
120 */
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static void change_termios(struct tty_struct * tty, struct termios * new_termios)
123{
124 int canon_change;
125 struct termios old_termios = *tty->termios;
126 struct tty_ldisc *ld;
127
128 /*
129 * Perform the actual termios internal changes under lock.
130 */
131
132
133 /* FIXME: we need to decide on some locking/ordering semantics
134 for the set_termios notification eventually */
Arjan van de Ven5785c952006-09-29 02:00:43 -0700135 mutex_lock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 *tty->termios = *new_termios;
138 unset_locked_termios(tty->termios, &old_termios, tty->termios_locked);
139 canon_change = (old_termios.c_lflag ^ tty->termios->c_lflag) & ICANON;
140 if (canon_change) {
141 memset(&tty->read_flags, 0, sizeof tty->read_flags);
142 tty->canon_head = tty->read_tail;
143 tty->canon_data = 0;
144 tty->erasing = 0;
145 }
146
147
148 if (canon_change && !L_ICANON(tty) && tty->read_cnt)
149 /* Get characters left over from canonical mode. */
150 wake_up_interruptible(&tty->read_wait);
151
152 /* See if packet mode change of state. */
153
154 if (tty->link && tty->link->packet) {
155 int old_flow = ((old_termios.c_iflag & IXON) &&
156 (old_termios.c_cc[VSTOP] == '\023') &&
157 (old_termios.c_cc[VSTART] == '\021'));
158 int new_flow = (I_IXON(tty) &&
159 STOP_CHAR(tty) == '\023' &&
160 START_CHAR(tty) == '\021');
161 if (old_flow != new_flow) {
162 tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
163 if (new_flow)
164 tty->ctrl_status |= TIOCPKT_DOSTOP;
165 else
166 tty->ctrl_status |= TIOCPKT_NOSTOP;
167 wake_up_interruptible(&tty->link->read_wait);
168 }
169 }
170
171 if (tty->driver->set_termios)
172 (*tty->driver->set_termios)(tty, &old_termios);
173
174 ld = tty_ldisc_ref(tty);
175 if (ld != NULL) {
176 if (ld->set_termios)
177 (ld->set_termios)(tty, &old_termios);
178 tty_ldisc_deref(ld);
179 }
Arjan van de Ven5785c952006-09-29 02:00:43 -0700180 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Alan Coxaf9b8972006-08-27 01:24:01 -0700183/**
184 * set_termios - set termios values for a tty
185 * @tty: terminal device
186 * @arg: user data
187 * @opt: option information
188 *
189 * Helper function to prepare termios data and run neccessary other
190 * functions before using change_termios to do the actual changes.
191 *
192 * Locking:
193 * Called functions take ldisc and termios_sem locks
194 */
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static int set_termios(struct tty_struct * tty, void __user *arg, int opt)
197{
198 struct termios tmp_termios;
199 struct tty_ldisc *ld;
200 int retval = tty_check_change(tty);
201
202 if (retval)
203 return retval;
204
205 if (opt & TERMIOS_TERMIO) {
206 memcpy(&tmp_termios, tty->termios, sizeof(struct termios));
207 if (user_termio_to_kernel_termios(&tmp_termios,
208 (struct termio __user *)arg))
209 return -EFAULT;
210 } else {
211 if (user_termios_to_kernel_termios(&tmp_termios,
212 (struct termios __user *)arg))
213 return -EFAULT;
214 }
215
216 ld = tty_ldisc_ref(tty);
217
218 if (ld != NULL) {
219 if ((opt & TERMIOS_FLUSH) && ld->flush_buffer)
220 ld->flush_buffer(tty);
221 tty_ldisc_deref(ld);
222 }
223
224 if (opt & TERMIOS_WAIT) {
225 tty_wait_until_sent(tty, 0);
226 if (signal_pending(current))
227 return -EINTR;
228 }
229
230 change_termios(tty, &tmp_termios);
231 return 0;
232}
233
234static int get_termio(struct tty_struct * tty, struct termio __user * termio)
235{
236 if (kernel_termios_to_user_termio(termio, tty->termios))
237 return -EFAULT;
238 return 0;
239}
240
241static unsigned long inq_canon(struct tty_struct * tty)
242{
243 int nr, head, tail;
244
245 if (!tty->canon_data || !tty->read_buf)
246 return 0;
247 head = tty->canon_head;
248 tail = tty->read_tail;
249 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
250 /* Skip EOF-chars.. */
251 while (head != tail) {
252 if (test_bit(tail, tty->read_flags) &&
253 tty->read_buf[tail] == __DISABLED_CHAR)
254 nr--;
255 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
256 }
257 return nr;
258}
259
260#ifdef TIOCGETP
261/*
262 * These are deprecated, but there is limited support..
263 *
264 * The "sg_flags" translation is a joke..
265 */
266static int get_sgflags(struct tty_struct * tty)
267{
268 int flags = 0;
269
270 if (!(tty->termios->c_lflag & ICANON)) {
271 if (tty->termios->c_lflag & ISIG)
272 flags |= 0x02; /* cbreak */
273 else
274 flags |= 0x20; /* raw */
275 }
276 if (tty->termios->c_lflag & ECHO)
277 flags |= 0x08; /* echo */
278 if (tty->termios->c_oflag & OPOST)
279 if (tty->termios->c_oflag & ONLCR)
280 flags |= 0x10; /* crmod */
281 return flags;
282}
283
284static int get_sgttyb(struct tty_struct * tty, struct sgttyb __user * sgttyb)
285{
286 struct sgttyb tmp;
287
Arjan van de Ven5785c952006-09-29 02:00:43 -0700288 mutex_lock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 tmp.sg_ispeed = 0;
290 tmp.sg_ospeed = 0;
291 tmp.sg_erase = tty->termios->c_cc[VERASE];
292 tmp.sg_kill = tty->termios->c_cc[VKILL];
293 tmp.sg_flags = get_sgflags(tty);
Arjan van de Ven5785c952006-09-29 02:00:43 -0700294 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
297}
298
299static void set_sgflags(struct termios * termios, int flags)
300{
301 termios->c_iflag = ICRNL | IXON;
302 termios->c_oflag = 0;
303 termios->c_lflag = ISIG | ICANON;
304 if (flags & 0x02) { /* cbreak */
305 termios->c_iflag = 0;
306 termios->c_lflag &= ~ICANON;
307 }
308 if (flags & 0x08) { /* echo */
309 termios->c_lflag |= ECHO | ECHOE | ECHOK |
310 ECHOCTL | ECHOKE | IEXTEN;
311 }
312 if (flags & 0x10) { /* crmod */
313 termios->c_oflag |= OPOST | ONLCR;
314 }
315 if (flags & 0x20) { /* raw */
316 termios->c_iflag = 0;
317 termios->c_lflag &= ~(ISIG | ICANON);
318 }
319 if (!(termios->c_lflag & ICANON)) {
320 termios->c_cc[VMIN] = 1;
321 termios->c_cc[VTIME] = 0;
322 }
323}
324
Alan Coxaf9b8972006-08-27 01:24:01 -0700325/**
326 * set_sgttyb - set legacy terminal values
327 * @tty: tty structure
328 * @sgttyb: pointer to old style terminal structure
329 *
330 * Updates a terminal from the legacy BSD style terminal information
331 * structure.
332 *
333 * Locking: termios_sem
334 */
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336static int set_sgttyb(struct tty_struct * tty, struct sgttyb __user * sgttyb)
337{
338 int retval;
339 struct sgttyb tmp;
340 struct termios termios;
341
342 retval = tty_check_change(tty);
343 if (retval)
344 return retval;
345
346 if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
347 return -EFAULT;
348
Arjan van de Ven5785c952006-09-29 02:00:43 -0700349 mutex_lock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 termios = *tty->termios;
351 termios.c_cc[VERASE] = tmp.sg_erase;
352 termios.c_cc[VKILL] = tmp.sg_kill;
353 set_sgflags(&termios, tmp.sg_flags);
Arjan van de Ven5785c952006-09-29 02:00:43 -0700354 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 change_termios(tty, &termios);
356 return 0;
357}
358#endif
359
360#ifdef TIOCGETC
361static int get_tchars(struct tty_struct * tty, struct tchars __user * tchars)
362{
363 struct tchars tmp;
364
365 tmp.t_intrc = tty->termios->c_cc[VINTR];
366 tmp.t_quitc = tty->termios->c_cc[VQUIT];
367 tmp.t_startc = tty->termios->c_cc[VSTART];
368 tmp.t_stopc = tty->termios->c_cc[VSTOP];
369 tmp.t_eofc = tty->termios->c_cc[VEOF];
370 tmp.t_brkc = tty->termios->c_cc[VEOL2]; /* what is brkc anyway? */
371 return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
372}
373
374static int set_tchars(struct tty_struct * tty, struct tchars __user * tchars)
375{
376 struct tchars tmp;
377
378 if (copy_from_user(&tmp, tchars, sizeof(tmp)))
379 return -EFAULT;
380 tty->termios->c_cc[VINTR] = tmp.t_intrc;
381 tty->termios->c_cc[VQUIT] = tmp.t_quitc;
382 tty->termios->c_cc[VSTART] = tmp.t_startc;
383 tty->termios->c_cc[VSTOP] = tmp.t_stopc;
384 tty->termios->c_cc[VEOF] = tmp.t_eofc;
385 tty->termios->c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
386 return 0;
387}
388#endif
389
390#ifdef TIOCGLTC
391static int get_ltchars(struct tty_struct * tty, struct ltchars __user * ltchars)
392{
393 struct ltchars tmp;
394
395 tmp.t_suspc = tty->termios->c_cc[VSUSP];
396 tmp.t_dsuspc = tty->termios->c_cc[VSUSP]; /* what is dsuspc anyway? */
397 tmp.t_rprntc = tty->termios->c_cc[VREPRINT];
398 tmp.t_flushc = tty->termios->c_cc[VEOL2]; /* what is flushc anyway? */
399 tmp.t_werasc = tty->termios->c_cc[VWERASE];
400 tmp.t_lnextc = tty->termios->c_cc[VLNEXT];
401 return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
402}
403
404static int set_ltchars(struct tty_struct * tty, struct ltchars __user * ltchars)
405{
406 struct ltchars tmp;
407
408 if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
409 return -EFAULT;
410
411 tty->termios->c_cc[VSUSP] = tmp.t_suspc;
412 tty->termios->c_cc[VEOL2] = tmp.t_dsuspc; /* what is dsuspc anyway? */
413 tty->termios->c_cc[VREPRINT] = tmp.t_rprntc;
414 tty->termios->c_cc[VEOL2] = tmp.t_flushc; /* what is flushc anyway? */
415 tty->termios->c_cc[VWERASE] = tmp.t_werasc;
416 tty->termios->c_cc[VLNEXT] = tmp.t_lnextc;
417 return 0;
418}
419#endif
420
Alan Coxaf9b8972006-08-27 01:24:01 -0700421/**
422 * send_prio_char - send priority character
423 *
424 * Send a high priority character to the tty even if stopped
425 *
426 * Locking: none
427 *
428 * FIXME: overlapping calls with start/stop tty lose state of tty
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431static void send_prio_char(struct tty_struct *tty, char ch)
432{
433 int was_stopped = tty->stopped;
434
435 if (tty->driver->send_xchar) {
436 tty->driver->send_xchar(tty, ch);
437 return;
438 }
439 if (was_stopped)
440 start_tty(tty);
441 tty->driver->write(tty, &ch, 1);
442 if (was_stopped)
443 stop_tty(tty);
444}
445
446int n_tty_ioctl(struct tty_struct * tty, struct file * file,
447 unsigned int cmd, unsigned long arg)
448{
449 struct tty_struct * real_tty;
450 void __user *p = (void __user *)arg;
451 int retval;
452 struct tty_ldisc *ld;
453
454 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
455 tty->driver->subtype == PTY_TYPE_MASTER)
456 real_tty = tty->link;
457 else
458 real_tty = tty;
459
460 switch (cmd) {
461#ifdef TIOCGETP
462 case TIOCGETP:
463 return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
464 case TIOCSETP:
465 case TIOCSETN:
466 return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
467#endif
468#ifdef TIOCGETC
469 case TIOCGETC:
470 return get_tchars(real_tty, p);
471 case TIOCSETC:
472 return set_tchars(real_tty, p);
473#endif
474#ifdef TIOCGLTC
475 case TIOCGLTC:
476 return get_ltchars(real_tty, p);
477 case TIOCSLTC:
478 return set_ltchars(real_tty, p);
479#endif
480 case TCGETS:
481 if (kernel_termios_to_user_termios((struct termios __user *)arg, real_tty->termios))
482 return -EFAULT;
483 return 0;
484 case TCSETSF:
485 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
486 case TCSETSW:
487 return set_termios(real_tty, p, TERMIOS_WAIT);
488 case TCSETS:
489 return set_termios(real_tty, p, 0);
490 case TCGETA:
491 return get_termio(real_tty, p);
492 case TCSETAF:
493 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
494 case TCSETAW:
495 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
496 case TCSETA:
497 return set_termios(real_tty, p, TERMIOS_TERMIO);
498 case TCXONC:
499 retval = tty_check_change(tty);
500 if (retval)
501 return retval;
502 switch (arg) {
503 case TCOOFF:
504 if (!tty->flow_stopped) {
505 tty->flow_stopped = 1;
506 stop_tty(tty);
507 }
508 break;
509 case TCOON:
510 if (tty->flow_stopped) {
511 tty->flow_stopped = 0;
512 start_tty(tty);
513 }
514 break;
515 case TCIOFF:
516 if (STOP_CHAR(tty) != __DISABLED_CHAR)
517 send_prio_char(tty, STOP_CHAR(tty));
518 break;
519 case TCION:
520 if (START_CHAR(tty) != __DISABLED_CHAR)
521 send_prio_char(tty, START_CHAR(tty));
522 break;
523 default:
524 return -EINVAL;
525 }
526 return 0;
527 case TCFLSH:
528 retval = tty_check_change(tty);
529 if (retval)
530 return retval;
531
532 ld = tty_ldisc_ref(tty);
533 switch (arg) {
534 case TCIFLUSH:
KAMBAROV, ZAUR69f63c52005-06-28 20:45:12 -0700535 if (ld && ld->flush_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 ld->flush_buffer(tty);
537 break;
538 case TCIOFLUSH:
KAMBAROV, ZAUR69f63c52005-06-28 20:45:12 -0700539 if (ld && ld->flush_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 ld->flush_buffer(tty);
541 /* fall through */
542 case TCOFLUSH:
543 if (tty->driver->flush_buffer)
544 tty->driver->flush_buffer(tty);
545 break;
546 default:
547 tty_ldisc_deref(ld);
548 return -EINVAL;
549 }
550 tty_ldisc_deref(ld);
551 return 0;
552 case TIOCOUTQ:
553 return put_user(tty->driver->chars_in_buffer ?
554 tty->driver->chars_in_buffer(tty) : 0,
555 (int __user *) arg);
556 case TIOCINQ:
557 retval = tty->read_cnt;
558 if (L_ICANON(tty))
559 retval = inq_canon(tty);
560 return put_user(retval, (unsigned int __user *) arg);
561 case TIOCGLCKTRMIOS:
562 if (kernel_termios_to_user_termios((struct termios __user *)arg, real_tty->termios_locked))
563 return -EFAULT;
564 return 0;
565
566 case TIOCSLCKTRMIOS:
567 if (!capable(CAP_SYS_ADMIN))
568 return -EPERM;
569 if (user_termios_to_kernel_termios(real_tty->termios_locked, (struct termios __user *) arg))
570 return -EFAULT;
571 return 0;
572
573 case TIOCPKT:
574 {
575 int pktmode;
576
577 if (tty->driver->type != TTY_DRIVER_TYPE_PTY ||
578 tty->driver->subtype != PTY_TYPE_MASTER)
579 return -ENOTTY;
580 if (get_user(pktmode, (int __user *) arg))
581 return -EFAULT;
582 if (pktmode) {
583 if (!tty->packet) {
584 tty->packet = 1;
585 tty->link->ctrl_status = 0;
586 }
587 } else
588 tty->packet = 0;
589 return 0;
590 }
591 case TIOCGSOFTCAR:
592 return put_user(C_CLOCAL(tty) ? 1 : 0, (int __user *)arg);
593 case TIOCSSOFTCAR:
594 if (get_user(arg, (unsigned int __user *) arg))
595 return -EFAULT;
Arjan van de Ven5785c952006-09-29 02:00:43 -0700596 mutex_lock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 tty->termios->c_cflag =
598 ((tty->termios->c_cflag & ~CLOCAL) |
599 (arg ? CLOCAL : 0));
Arjan van de Ven5785c952006-09-29 02:00:43 -0700600 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 0;
602 default:
603 return -ENOIOCTLCMD;
604 }
605}
606
607EXPORT_SYMBOL(n_tty_ioctl);