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