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