blob: 7fad532f765054f324d01d7d188582ee9aa6866a [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 * 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>
21#include <linux/mutex.h>
22#include <linux/compat.h>
23
24#include <asm/io.h>
25#include <asm/uaccess.h>
26
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
37#define TERMIOS_OLD 8
38
39
40/**
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
49int 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}
56EXPORT_SYMBOL(tty_chars_in_buffer);
57
58/**
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
69int 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}
75EXPORT_SYMBOL(tty_write_room);
76
77/**
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 */
85void tty_driver_flush_buffer(struct tty_struct *tty)
86{
87 if (tty->ops->flush_buffer)
88 tty->ops->flush_buffer(tty);
89}
90EXPORT_SYMBOL(tty_driver_flush_buffer);
91
92/**
93 * tty_throttle - flow control
94 * @tty: terminal
95 *
96 * Indicate that a tty should stop transmitting data down the stack.
97 * 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.
100 */
101
102void tty_throttle(struct tty_struct *tty)
103{
104 mutex_lock(&tty->termios_mutex);
105 /* 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);
109 mutex_unlock(&tty->termios_mutex);
110}
111EXPORT_SYMBOL(tty_throttle);
112
113/**
114 * tty_unthrottle - flow control
115 * @tty: terminal
116 *
117 * Indicate that a tty may continue transmitting data down the stack.
118 * 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.
124 */
125
126void tty_unthrottle(struct tty_struct *tty)
127{
128 mutex_lock(&tty->termios_mutex);
129 if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
130 tty->ops->unthrottle)
131 tty->ops->unthrottle(tty);
132 mutex_unlock(&tty->termios_mutex);
133}
134EXPORT_SYMBOL(tty_unthrottle);
135
136/**
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
147void tty_wait_until_sent(struct tty_struct *tty, long timeout)
148{
149#ifdef TTY_DEBUG_WAIT_UNTIL_SENT
150 char buf[64];
151
152 printk(KERN_DEBUG "%s wait until sent...\n", tty_name(tty, buf));
153#endif
154 if (!timeout)
155 timeout = MAX_SCHEDULE_TIMEOUT;
156
157 if (wait_event_interruptible_timeout(tty->write_wait,
158 !tty_chars_in_buffer(tty), timeout) < 0) {
159 return;
160 }
161
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);
167}
168EXPORT_SYMBOL(tty_wait_until_sent);
169
170
171/*
172 * Termios Helper Methods
173 */
174
175static void unset_locked_termios(struct ktermios *termios,
176 struct ktermios *old,
177 struct ktermios *locked)
178{
179 int i;
180
181#define NOSET_MASK(x, y, z) (x = ((x) & ~(z)) | ((y) & (z)))
182
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;
193 for (i = 0; i < NCCS; i++)
194 termios->c_cc[i] = locked->c_cc[i] ?
195 old->c_cc[i] : termios->c_cc[i];
196 /* FIXME: What should we do for i/ospeed */
197}
198
199/*
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
254 /* Magic token for arbitrary speed via c_ispeed/c_ospeed */
255 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#ifdef CONFIG_KLOCWORK
267 if(cbaud >= n_baud_table)
268 BUG_ON(1);
269#endif
270 return baud_table[cbaud];
271}
272EXPORT_SYMBOL(tty_termios_baud_rate);
273
274/**
275 * tty_termios_input_baud_rate
276 * @termios: termios structure
277 *
278 * Convert termios baud rate data into a speed. This should be called
279 * with the termios lock held if this termios is a terminal termios
280 * structure. May change the termios data. Device drivers can call this
281 * function but should use ->c_[io]speed directly as they are updated.
282 *
283 * Locking: none
284 */
285
286speed_t tty_termios_input_baud_rate(struct ktermios *termios)
287{
288#ifdef IBSHIFT
289 unsigned int cbaud = (termios->c_cflag >> IBSHIFT) & CBAUD;
290
291 if (cbaud == B0)
292 return tty_termios_baud_rate(termios);
293
294 /* Magic token for arbitrary speed via c_ispeed*/
295 if (cbaud == BOTHER)
296 return termios->c_ispeed;
297
298 if (cbaud & CBAUDEX) {
299 cbaud &= ~CBAUDEX;
300
301 if (cbaud < 1 || cbaud + 15 > n_baud_table)
302 termios->c_cflag &= ~(CBAUDEX << IBSHIFT);
303 else
304 cbaud += 15;
305 }
306#ifdef CONFIG_KLOCWORK
307 if(cbaud >= n_baud_table)
308 BUG_ON(1);
309#endif
310 return baud_table[cbaud];
311#else
312 return tty_termios_baud_rate(termios);
313#endif
314}
315EXPORT_SYMBOL(tty_termios_input_baud_rate);
316
317/**
318 * tty_termios_encode_baud_rate
319 * @termios: ktermios structure holding user requested state
320 * @ispeed: input speed
321 * @ospeed: output speed
322 *
323 * Encode the speeds set into the passed termios structure. This is
324 * used as a library helper for drivers so that they can report back
325 * the actual speed selected when it differs from the speed requested
326 *
327 * For maximal back compatibility with legacy SYS5/POSIX *nix behaviour
328 * we need to carefully set the bits when the user does not get the
329 * desired speed. We allow small margins and preserve as much of possible
330 * of the input intent to keep compatibility.
331 *
332 * Locking: Caller should hold termios lock. This is already held
333 * when calling this function from the driver termios handler.
334 *
335 * The ifdefs deal with platforms whose owners have yet to update them
336 * and will all go away once this is done.
337 */
338
339void tty_termios_encode_baud_rate(struct ktermios *termios,
340 speed_t ibaud, speed_t obaud)
341{
342 int i = 0;
343 int ifound = -1, ofound = -1;
344 int iclose = ibaud/50, oclose = obaud/50;
345 int ibinput = 0;
346
347 if (obaud == 0) /* CD dropped */
348 ibaud = 0; /* Clear ibaud to be sure */
349
350 termios->c_ispeed = ibaud;
351 termios->c_ospeed = obaud;
352
353#ifdef BOTHER
354 /* If the user asked for a precise weird speed give a precise weird
355 answer. If they asked for a Bfoo speed they many have problems
356 digesting non-exact replies so fuzz a bit */
357
358 if ((termios->c_cflag & CBAUD) == BOTHER)
359 oclose = 0;
360 if (((termios->c_cflag >> IBSHIFT) & CBAUD) == BOTHER)
361 iclose = 0;
362 if ((termios->c_cflag >> IBSHIFT) & CBAUD)
363 ibinput = 1; /* An input speed was specified */
364#endif
365 termios->c_cflag &= ~CBAUD;
366
367 /*
368 * Our goal is to find a close match to the standard baud rate
369 * returned. Walk the baud rate table and if we get a very close
370 * match then report back the speed as a POSIX Bxxxx value by
371 * preference
372 */
373
374 do {
375 if (obaud - oclose <= baud_table[i] &&
376 obaud + oclose >= baud_table[i]) {
377 termios->c_cflag |= baud_bits[i];
378 ofound = i;
379 }
380 if (ibaud - iclose <= baud_table[i] &&
381 ibaud + iclose >= baud_table[i]) {
382 /* For the case input == output don't set IBAUD bits
383 if the user didn't do so */
384 if (ofound == i && !ibinput)
385 ifound = i;
386#ifdef IBSHIFT
387 else {
388 ifound = i;
389 termios->c_cflag |= (baud_bits[i] << IBSHIFT);
390 }
391#endif
392 }
393 } while (++i < n_baud_table);
394
395 /*
396 * If we found no match then use BOTHER if provided or warn
397 * the user their platform maintainer needs to wake up if not.
398 */
399#ifdef BOTHER
400 if (ofound == -1)
401 termios->c_cflag |= BOTHER;
402 /* Set exact input bits only if the input and output differ or the
403 user already did */
404 if (ifound == -1 && (ibaud != obaud || ibinput))
405 termios->c_cflag |= (BOTHER << IBSHIFT);
406#else
407 if (ifound == -1 || ofound == -1) {
408 printk_once(KERN_WARNING "tty: Unable to return correct "
409 "speed data as your architecture needs updating.\n");
410 }
411#endif
412}
413EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate);
414
415/**
416 * tty_encode_baud_rate - set baud rate of the tty
417 * @ibaud: input baud rate
418 * @obad: output baud rate
419 *
420 * Update the current termios data for the tty with the new speed
421 * settings. The caller must hold the termios_mutex for the tty in
422 * question.
423 */
424
425void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, speed_t obaud)
426{
427 tty_termios_encode_baud_rate(tty->termios, ibaud, obaud);
428}
429EXPORT_SYMBOL_GPL(tty_encode_baud_rate);
430
431/**
432 * tty_get_baud_rate - get tty bit rates
433 * @tty: tty to query
434 *
435 * Returns the baud rate as an integer for this terminal. The
436 * termios lock must be held by the caller and the terminal bit
437 * flags may be updated.
438 *
439 * Locking: none
440 */
441
442speed_t tty_get_baud_rate(struct tty_struct *tty)
443{
444 speed_t baud = tty_termios_baud_rate(tty->termios);
445
446 if (baud == 38400 && tty->alt_speed) {
447 if (!tty->warned) {
448 printk(KERN_WARNING "Use of setserial/setrocket to "
449 "set SPD_* flags is deprecated\n");
450 tty->warned = 1;
451 }
452 baud = tty->alt_speed;
453 }
454
455 return baud;
456}
457EXPORT_SYMBOL(tty_get_baud_rate);
458
459/**
460 * tty_termios_copy_hw - copy hardware settings
461 * @new: New termios
462 * @old: Old termios
463 *
464 * Propagate the hardware specific terminal setting bits from
465 * the old termios structure to the new one. This is used in cases
466 * where the hardware does not support reconfiguration or as a helper
467 * in some cases where only minimal reconfiguration is supported
468 */
469
470void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old)
471{
472 /* The bits a dumb device handles in software. Smart devices need
473 to always provide a set_termios method */
474 new->c_cflag &= HUPCL | CREAD | CLOCAL;
475 new->c_cflag |= old->c_cflag & ~(HUPCL | CREAD | CLOCAL);
476 new->c_ispeed = old->c_ispeed;
477 new->c_ospeed = old->c_ospeed;
478}
479EXPORT_SYMBOL(tty_termios_copy_hw);
480
481/**
482 * tty_termios_hw_change - check for setting change
483 * @a: termios
484 * @b: termios to compare
485 *
486 * Check if any of the bits that affect a dumb device have changed
487 * between the two termios structures, or a speed change is needed.
488 */
489
490int tty_termios_hw_change(struct ktermios *a, struct ktermios *b)
491{
492 if (a->c_ispeed != b->c_ispeed || a->c_ospeed != b->c_ospeed)
493 return 1;
494 if ((a->c_cflag ^ b->c_cflag) & ~(HUPCL | CREAD | CLOCAL))
495 return 1;
496 return 0;
497}
498EXPORT_SYMBOL(tty_termios_hw_change);
499
500/**
501 * tty_set_termios - update termios values
502 * @tty: tty to update
503 * @new_termios: desired new value
504 *
505 * Perform updates to the termios values set on this terminal. There
506 * is a bit of layering violation here with n_tty in terms of the
507 * internal knowledge of this function.
508 *
509 * Locking: termios_mutex
510 */
511
512int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios)
513{
514 struct ktermios old_termios;
515 struct tty_ldisc *ld;
516 unsigned long flags;
517
518 /*
519 * Perform the actual termios internal changes under lock.
520 */
521
522
523 /* FIXME: we need to decide on some locking/ordering semantics
524 for the set_termios notification eventually */
525 mutex_lock(&tty->termios_mutex);
526 old_termios = *tty->termios;
527 *tty->termios = *new_termios;
528 unset_locked_termios(tty->termios, &old_termios, tty->termios_locked);
529
530 /* See if packet mode change of state. */
531 if (tty->link && tty->link->packet) {
532 int extproc = (old_termios.c_lflag & EXTPROC) |
533 (tty->termios->c_lflag & EXTPROC);
534 int old_flow = ((old_termios.c_iflag & IXON) &&
535 (old_termios.c_cc[VSTOP] == '\023') &&
536 (old_termios.c_cc[VSTART] == '\021'));
537 int new_flow = (I_IXON(tty) &&
538 STOP_CHAR(tty) == '\023' &&
539 START_CHAR(tty) == '\021');
540 if ((old_flow != new_flow) || extproc) {
541 spin_lock_irqsave(&tty->ctrl_lock, flags);
542 if (old_flow != new_flow) {
543 tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
544 if (new_flow)
545 tty->ctrl_status |= TIOCPKT_DOSTOP;
546 else
547 tty->ctrl_status |= TIOCPKT_NOSTOP;
548 }
549 if (extproc)
550 tty->ctrl_status |= TIOCPKT_IOCTL;
551 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
552 wake_up_interruptible(&tty->link->read_wait);
553 }
554 }
555
556 if (tty->ops->set_termios)
557 (*tty->ops->set_termios)(tty, &old_termios);
558 else
559 tty_termios_copy_hw(tty->termios, &old_termios);
560
561 ld = tty_ldisc_ref(tty);
562 if (ld != NULL) {
563 if (ld->ops->set_termios)
564 (ld->ops->set_termios)(tty, &old_termios);
565 tty_ldisc_deref(ld);
566 }
567 mutex_unlock(&tty->termios_mutex);
568 return 0;
569}
570EXPORT_SYMBOL_GPL(tty_set_termios);
571
572/**
573 * set_termios - set termios values for a tty
574 * @tty: terminal device
575 * @arg: user data
576 * @opt: option information
577 *
578 * Helper function to prepare termios data and run necessary other
579 * functions before using tty_set_termios to do the actual changes.
580 *
581 * Locking:
582 * Called functions take ldisc and termios_mutex locks
583 */
584
585static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
586{
587 struct ktermios tmp_termios;
588 struct tty_ldisc *ld;
589 int retval = tty_check_change(tty);
590
591 if (retval)
592 return retval;
593
594 mutex_lock(&tty->termios_mutex);
595 memcpy(&tmp_termios, tty->termios, sizeof(struct ktermios));
596 mutex_unlock(&tty->termios_mutex);
597
598 if (opt & TERMIOS_TERMIO) {
599 if (user_termio_to_kernel_termios(&tmp_termios,
600 (struct termio __user *)arg))
601 return -EFAULT;
602#ifdef TCGETS2
603 } else if (opt & TERMIOS_OLD) {
604 if (user_termios_to_kernel_termios_1(&tmp_termios,
605 (struct termios __user *)arg))
606 return -EFAULT;
607 } else {
608 if (user_termios_to_kernel_termios(&tmp_termios,
609 (struct termios2 __user *)arg))
610 return -EFAULT;
611 }
612#else
613 } else if (user_termios_to_kernel_termios(&tmp_termios,
614 (struct termios __user *)arg))
615 return -EFAULT;
616#endif
617
618 /* If old style Bfoo values are used then load c_ispeed/c_ospeed
619 * with the real speed so its unconditionally usable */
620 tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios);
621 tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios);
622
623 ld = tty_ldisc_ref(tty);
624
625 if (ld != NULL) {
626 if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
627 ld->ops->flush_buffer(tty);
628 tty_ldisc_deref(ld);
629 }
630
631 if (opt & TERMIOS_WAIT) {
632 tty_wait_until_sent(tty, 0);
633 if (signal_pending(current))
634 return -ERESTARTSYS;
635 }
636
637 tty_set_termios(tty, &tmp_termios);
638
639 /* FIXME: Arguably if tmp_termios == tty->termios AND the
640 actual requested termios was not tmp_termios then we may
641 want to return an error as no user requested change has
642 succeeded */
643 return 0;
644}
645
646static void copy_termios(struct tty_struct *tty, struct ktermios *kterm)
647{
648 mutex_lock(&tty->termios_mutex);
649 memcpy(kterm, tty->termios, sizeof(struct ktermios));
650 mutex_unlock(&tty->termios_mutex);
651}
652
653static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm)
654{
655 mutex_lock(&tty->termios_mutex);
656 memcpy(kterm, tty->termios_locked, sizeof(struct ktermios));
657 mutex_unlock(&tty->termios_mutex);
658}
659
660static int get_termio(struct tty_struct *tty, struct termio __user *termio)
661{
662 struct ktermios kterm;
663 copy_termios(tty, &kterm);
664 if (kernel_termios_to_user_termio(termio, &kterm))
665 return -EFAULT;
666 return 0;
667}
668
669
670#ifdef TCGETX
671
672/**
673 * set_termiox - set termiox fields if possible
674 * @tty: terminal
675 * @arg: termiox structure from user
676 * @opt: option flags for ioctl type
677 *
678 * Implement the device calling points for the SYS5 termiox ioctl
679 * interface in Linux
680 */
681
682static int set_termiox(struct tty_struct *tty, void __user *arg, int opt)
683{
684 struct termiox tnew;
685 struct tty_ldisc *ld;
686
687 if (tty->termiox == NULL)
688 return -EINVAL;
689 if (copy_from_user(&tnew, arg, sizeof(struct termiox)))
690 return -EFAULT;
691
692 ld = tty_ldisc_ref(tty);
693 if (ld != NULL) {
694 if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
695 ld->ops->flush_buffer(tty);
696 tty_ldisc_deref(ld);
697 }
698 if (opt & TERMIOS_WAIT) {
699 tty_wait_until_sent(tty, 0);
700 if (signal_pending(current))
701 return -ERESTARTSYS;
702 }
703
704 mutex_lock(&tty->termios_mutex);
705 if (tty->ops->set_termiox)
706 tty->ops->set_termiox(tty, &tnew);
707 mutex_unlock(&tty->termios_mutex);
708 return 0;
709}
710
711#endif
712
713
714#ifdef TIOCGETP
715/*
716 * These are deprecated, but there is limited support..
717 *
718 * The "sg_flags" translation is a joke..
719 */
720static int get_sgflags(struct tty_struct *tty)
721{
722 int flags = 0;
723
724 if (!(tty->termios->c_lflag & ICANON)) {
725 if (tty->termios->c_lflag & ISIG)
726 flags |= 0x02; /* cbreak */
727 else
728 flags |= 0x20; /* raw */
729 }
730 if (tty->termios->c_lflag & ECHO)
731 flags |= 0x08; /* echo */
732 if (tty->termios->c_oflag & OPOST)
733 if (tty->termios->c_oflag & ONLCR)
734 flags |= 0x10; /* crmod */
735 return flags;
736}
737
738static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
739{
740 struct sgttyb tmp;
741
742 mutex_lock(&tty->termios_mutex);
743 tmp.sg_ispeed = tty->termios->c_ispeed;
744 tmp.sg_ospeed = tty->termios->c_ospeed;
745 tmp.sg_erase = tty->termios->c_cc[VERASE];
746 tmp.sg_kill = tty->termios->c_cc[VKILL];
747 tmp.sg_flags = get_sgflags(tty);
748 mutex_unlock(&tty->termios_mutex);
749
750 return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
751}
752
753static void set_sgflags(struct ktermios *termios, int flags)
754{
755 termios->c_iflag = ICRNL | IXON;
756 termios->c_oflag = 0;
757 termios->c_lflag = ISIG | ICANON;
758 if (flags & 0x02) { /* cbreak */
759 termios->c_iflag = 0;
760 termios->c_lflag &= ~ICANON;
761 }
762 if (flags & 0x08) { /* echo */
763 termios->c_lflag |= ECHO | ECHOE | ECHOK |
764 ECHOCTL | ECHOKE | IEXTEN;
765 }
766 if (flags & 0x10) { /* crmod */
767 termios->c_oflag |= OPOST | ONLCR;
768 }
769 if (flags & 0x20) { /* raw */
770 termios->c_iflag = 0;
771 termios->c_lflag &= ~(ISIG | ICANON);
772 }
773 if (!(termios->c_lflag & ICANON)) {
774 termios->c_cc[VMIN] = 1;
775 termios->c_cc[VTIME] = 0;
776 }
777}
778
779/**
780 * set_sgttyb - set legacy terminal values
781 * @tty: tty structure
782 * @sgttyb: pointer to old style terminal structure
783 *
784 * Updates a terminal from the legacy BSD style terminal information
785 * structure.
786 *
787 * Locking: termios_mutex
788 */
789
790static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
791{
792 int retval;
793 struct sgttyb tmp;
794 struct ktermios termios;
795
796 retval = tty_check_change(tty);
797 if (retval)
798 return retval;
799
800 if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
801 return -EFAULT;
802
803 mutex_lock(&tty->termios_mutex);
804 termios = *tty->termios;
805 termios.c_cc[VERASE] = tmp.sg_erase;
806 termios.c_cc[VKILL] = tmp.sg_kill;
807 set_sgflags(&termios, tmp.sg_flags);
808 /* Try and encode into Bfoo format */
809#ifdef BOTHER
810 tty_termios_encode_baud_rate(&termios, termios.c_ispeed,
811 termios.c_ospeed);
812#endif
813 mutex_unlock(&tty->termios_mutex);
814 tty_set_termios(tty, &termios);
815 return 0;
816}
817#endif
818
819#ifdef TIOCGETC
820static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars)
821{
822 struct tchars tmp;
823
824 mutex_lock(&tty->termios_mutex);
825 tmp.t_intrc = tty->termios->c_cc[VINTR];
826 tmp.t_quitc = tty->termios->c_cc[VQUIT];
827 tmp.t_startc = tty->termios->c_cc[VSTART];
828 tmp.t_stopc = tty->termios->c_cc[VSTOP];
829 tmp.t_eofc = tty->termios->c_cc[VEOF];
830 tmp.t_brkc = tty->termios->c_cc[VEOL2]; /* what is brkc anyway? */
831 mutex_unlock(&tty->termios_mutex);
832 return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
833}
834
835static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars)
836{
837 struct tchars tmp;
838
839 if (copy_from_user(&tmp, tchars, sizeof(tmp)))
840 return -EFAULT;
841 mutex_lock(&tty->termios_mutex);
842 tty->termios->c_cc[VINTR] = tmp.t_intrc;
843 tty->termios->c_cc[VQUIT] = tmp.t_quitc;
844 tty->termios->c_cc[VSTART] = tmp.t_startc;
845 tty->termios->c_cc[VSTOP] = tmp.t_stopc;
846 tty->termios->c_cc[VEOF] = tmp.t_eofc;
847 tty->termios->c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
848 mutex_unlock(&tty->termios_mutex);
849 return 0;
850}
851#endif
852
853#ifdef TIOCGLTC
854static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
855{
856 struct ltchars tmp;
857
858 mutex_lock(&tty->termios_mutex);
859 tmp.t_suspc = tty->termios->c_cc[VSUSP];
860 /* what is dsuspc anyway? */
861 tmp.t_dsuspc = tty->termios->c_cc[VSUSP];
862 tmp.t_rprntc = tty->termios->c_cc[VREPRINT];
863 /* what is flushc anyway? */
864 tmp.t_flushc = tty->termios->c_cc[VEOL2];
865 tmp.t_werasc = tty->termios->c_cc[VWERASE];
866 tmp.t_lnextc = tty->termios->c_cc[VLNEXT];
867 mutex_unlock(&tty->termios_mutex);
868 return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
869}
870
871static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
872{
873 struct ltchars tmp;
874
875 if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
876 return -EFAULT;
877
878 mutex_lock(&tty->termios_mutex);
879 tty->termios->c_cc[VSUSP] = tmp.t_suspc;
880 /* what is dsuspc anyway? */
881 tty->termios->c_cc[VEOL2] = tmp.t_dsuspc;
882 tty->termios->c_cc[VREPRINT] = tmp.t_rprntc;
883 /* what is flushc anyway? */
884 tty->termios->c_cc[VEOL2] = tmp.t_flushc;
885 tty->termios->c_cc[VWERASE] = tmp.t_werasc;
886 tty->termios->c_cc[VLNEXT] = tmp.t_lnextc;
887 mutex_unlock(&tty->termios_mutex);
888 return 0;
889}
890#endif
891
892/**
893 * send_prio_char - send priority character
894 *
895 * Send a high priority character to the tty even if stopped
896 *
897 * Locking: none for xchar method, write ordering for write method.
898 */
899
900static int send_prio_char(struct tty_struct *tty, char ch)
901{
902 int was_stopped = tty->stopped;
903
904 if (tty->ops->send_xchar) {
905 tty->ops->send_xchar(tty, ch);
906 return 0;
907 }
908
909 if (tty_write_lock(tty, 0) < 0)
910 return -ERESTARTSYS;
911
912 if (was_stopped)
913 start_tty(tty);
914 tty->ops->write(tty, &ch, 1);
915 if (was_stopped)
916 stop_tty(tty);
917 tty_write_unlock(tty);
918 return 0;
919}
920
921/**
922 * tty_change_softcar - carrier change ioctl helper
923 * @tty: tty to update
924 * @arg: enable/disable CLOCAL
925 *
926 * Perform a change to the CLOCAL state and call into the driver
927 * layer to make it visible. All done with the termios mutex
928 */
929
930static int tty_change_softcar(struct tty_struct *tty, int arg)
931{
932 int ret = 0;
933 int bit = arg ? CLOCAL : 0;
934 struct ktermios old;
935
936 mutex_lock(&tty->termios_mutex);
937 old = *tty->termios;
938 tty->termios->c_cflag &= ~CLOCAL;
939 tty->termios->c_cflag |= bit;
940 if (tty->ops->set_termios)
941 tty->ops->set_termios(tty, &old);
942 if ((tty->termios->c_cflag & CLOCAL) != bit)
943 ret = -EINVAL;
944 mutex_unlock(&tty->termios_mutex);
945 return ret;
946}
947
948/**
949 * tty_mode_ioctl - mode related ioctls
950 * @tty: tty for the ioctl
951 * @file: file pointer for the tty
952 * @cmd: command
953 * @arg: ioctl argument
954 *
955 * Perform non line discipline specific mode control ioctls. This
956 * is designed to be called by line disciplines to ensure they provide
957 * consistent mode setting.
958 */
959
960int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
961 unsigned int cmd, unsigned long arg)
962{
963 struct tty_struct *real_tty;
964 void __user *p = (void __user *)arg;
965 int ret = 0;
966 struct ktermios kterm;
967
968 BUG_ON(file == NULL);
969
970 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
971 tty->driver->subtype == PTY_TYPE_MASTER)
972 real_tty = tty->link;
973 else
974 real_tty = tty;
975
976 switch (cmd) {
977#ifdef TIOCGETP
978 case TIOCGETP:
979 return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
980 case TIOCSETP:
981 case TIOCSETN:
982 return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
983#endif
984#ifdef TIOCGETC
985 case TIOCGETC:
986 return get_tchars(real_tty, p);
987 case TIOCSETC:
988 return set_tchars(real_tty, p);
989#endif
990#ifdef TIOCGLTC
991 case TIOCGLTC:
992 return get_ltchars(real_tty, p);
993 case TIOCSLTC:
994 return set_ltchars(real_tty, p);
995#endif
996 case TCSETSF:
997 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
998 case TCSETSW:
999 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
1000 case TCSETS:
1001 return set_termios(real_tty, p, TERMIOS_OLD);
1002#ifndef TCGETS2
1003 case TCGETS:
1004 copy_termios(real_tty, &kterm);
1005 if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
1006 ret = -EFAULT;
1007 return ret;
1008#else
1009 case TCGETS:
1010 copy_termios(real_tty, &kterm);
1011 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
1012 ret = -EFAULT;
1013 return ret;
1014 case TCGETS2:
1015 copy_termios(real_tty, &kterm);
1016 if (kernel_termios_to_user_termios((struct termios2 __user *)arg, &kterm))
1017 ret = -EFAULT;
1018 return ret;
1019 case TCSETSF2:
1020 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
1021 case TCSETSW2:
1022 return set_termios(real_tty, p, TERMIOS_WAIT);
1023 case TCSETS2:
1024 return set_termios(real_tty, p, 0);
1025#endif
1026 case TCGETA:
1027 return get_termio(real_tty, p);
1028 case TCSETAF:
1029 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
1030 case TCSETAW:
1031 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
1032 case TCSETA:
1033 return set_termios(real_tty, p, TERMIOS_TERMIO);
1034#ifndef TCGETS2
1035 case TIOCGLCKTRMIOS:
1036 copy_termios_locked(real_tty, &kterm);
1037 if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
1038 ret = -EFAULT;
1039 return ret;
1040 case TIOCSLCKTRMIOS:
1041 if (!capable(CAP_SYS_ADMIN))
1042 return -EPERM;
1043 copy_termios_locked(real_tty, &kterm);
1044 if (user_termios_to_kernel_termios(&kterm,
1045 (struct termios __user *) arg))
1046 return -EFAULT;
1047 mutex_lock(&real_tty->termios_mutex);
1048 memcpy(real_tty->termios_locked, &kterm, sizeof(struct ktermios));
1049 mutex_unlock(&real_tty->termios_mutex);
1050 return 0;
1051#else
1052 case TIOCGLCKTRMIOS:
1053 copy_termios_locked(real_tty, &kterm);
1054 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
1055 ret = -EFAULT;
1056 return ret;
1057 case TIOCSLCKTRMIOS:
1058 if (!capable(CAP_SYS_ADMIN))
1059 return -EPERM;
1060 copy_termios_locked(real_tty, &kterm);
1061 if (user_termios_to_kernel_termios_1(&kterm,
1062 (struct termios __user *) arg))
1063 return -EFAULT;
1064 mutex_lock(&real_tty->termios_mutex);
1065 memcpy(real_tty->termios_locked, &kterm, sizeof(struct ktermios));
1066 mutex_unlock(&real_tty->termios_mutex);
1067 return ret;
1068#endif
1069#ifdef TCGETX
1070 case TCGETX: {
1071 struct termiox ktermx;
1072 if (real_tty->termiox == NULL)
1073 return -EINVAL;
1074 mutex_lock(&real_tty->termios_mutex);
1075 memcpy(&ktermx, real_tty->termiox, sizeof(struct termiox));
1076 mutex_unlock(&real_tty->termios_mutex);
1077 if (copy_to_user(p, &ktermx, sizeof(struct termiox)))
1078 ret = -EFAULT;
1079 return ret;
1080 }
1081 case TCSETX:
1082 return set_termiox(real_tty, p, 0);
1083 case TCSETXW:
1084 return set_termiox(real_tty, p, TERMIOS_WAIT);
1085 case TCSETXF:
1086 return set_termiox(real_tty, p, TERMIOS_FLUSH);
1087#endif
1088 case TIOCGSOFTCAR:
1089 copy_termios(real_tty, &kterm);
1090 ret = put_user((kterm.c_cflag & CLOCAL) ? 1 : 0,
1091 (int __user *)arg);
1092 return ret;
1093 case TIOCSSOFTCAR:
1094 if (get_user(arg, (unsigned int __user *) arg))
1095 return -EFAULT;
1096 return tty_change_softcar(real_tty, arg);
1097 default:
1098 return -ENOIOCTLCMD;
1099 }
1100}
1101EXPORT_SYMBOL_GPL(tty_mode_ioctl);
1102
1103int tty_perform_flush(struct tty_struct *tty, unsigned long arg)
1104{
1105 struct tty_ldisc *ld;
1106 int retval = tty_check_change(tty);
1107 if (retval)
1108 return retval;
1109
1110 ld = tty_ldisc_ref_wait(tty);
1111 switch (arg) {
1112 case TCIFLUSH:
1113 if (ld && ld->ops->flush_buffer)
1114 ld->ops->flush_buffer(tty);
1115 break;
1116 case TCIOFLUSH:
1117 if (ld && ld->ops->flush_buffer)
1118 ld->ops->flush_buffer(tty);
1119 /* fall through */
1120 case TCOFLUSH:
1121 tty_driver_flush_buffer(tty);
1122 break;
1123 default:
1124 tty_ldisc_deref(ld);
1125 return -EINVAL;
1126 }
1127 tty_ldisc_deref(ld);
1128 return 0;
1129}
1130EXPORT_SYMBOL_GPL(tty_perform_flush);
1131
1132int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file,
1133 unsigned int cmd, unsigned long arg)
1134{
1135 unsigned long flags;
1136 int retval;
1137
1138 switch (cmd) {
1139 case TCXONC:
1140 retval = tty_check_change(tty);
1141 if (retval)
1142 return retval;
1143 switch (arg) {
1144 case TCOOFF:
1145 if (!tty->flow_stopped) {
1146 tty->flow_stopped = 1;
1147 stop_tty(tty);
1148 }
1149 break;
1150 case TCOON:
1151 if (tty->flow_stopped) {
1152 tty->flow_stopped = 0;
1153 start_tty(tty);
1154 }
1155 break;
1156 case TCIOFF:
1157 if (STOP_CHAR(tty) != __DISABLED_CHAR)
1158 return send_prio_char(tty, STOP_CHAR(tty));
1159 break;
1160 case TCION:
1161 if (START_CHAR(tty) != __DISABLED_CHAR)
1162 return send_prio_char(tty, START_CHAR(tty));
1163 break;
1164 default:
1165 return -EINVAL;
1166 }
1167 return 0;
1168 case TCFLSH:
1169 return tty_perform_flush(tty, arg);
1170 case TIOCPKT:
1171 {
1172 int pktmode;
1173
1174 if (tty->driver->type != TTY_DRIVER_TYPE_PTY ||
1175 tty->driver->subtype != PTY_TYPE_MASTER)
1176 return -ENOTTY;
1177 if (get_user(pktmode, (int __user *) arg))
1178 return -EFAULT;
1179 spin_lock_irqsave(&tty->ctrl_lock, flags);
1180 if (pktmode) {
1181 if (!tty->packet) {
1182 tty->packet = 1;
1183 tty->link->ctrl_status = 0;
1184 }
1185 } else
1186 tty->packet = 0;
1187 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1188 return 0;
1189 }
1190 default:
1191 /* Try the mode commands */
1192 return tty_mode_ioctl(tty, file, cmd, arg);
1193 }
1194}
1195EXPORT_SYMBOL(n_tty_ioctl_helper);
1196
1197#ifdef CONFIG_COMPAT
1198long n_tty_compat_ioctl_helper(struct tty_struct *tty, struct file *file,
1199 unsigned int cmd, unsigned long arg)
1200{
1201 switch (cmd) {
1202 case TIOCGLCKTRMIOS:
1203 case TIOCSLCKTRMIOS:
1204 return tty_mode_ioctl(tty, file, cmd, (unsigned long) compat_ptr(arg));
1205 default:
1206 return -ENOIOCTLCMD;
1207 }
1208}
1209EXPORT_SYMBOL(n_tty_compat_ioctl_helper);
1210#endif
1211