]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/char/tty_ioctl.c
Merge master.kernel.org:/pub/scm/linux/kernel/git/mchehab/v4l-dvb
[linux-2.6-omap-h63xx.git] / drivers / char / tty_ioctl.c
index 0ffba4e911ca0279aaeab2d91c78ad20c0f57c5c..fd471cb3338fbd1d008c936f5d44cfe5691a952b 100644 (file)
@@ -225,7 +225,7 @@ EXPORT_SYMBOL(tty_termios_input_baud_rate);
 
 /**
  *     tty_termios_encode_baud_rate
- *     @termios: termios structure
+ *     @termios: ktermios structure holding user requested state
  *     @ispeed: input speed
  *     @ospeed: output speed
  *
@@ -233,7 +233,10 @@ EXPORT_SYMBOL(tty_termios_input_baud_rate);
  *     used as a library helper for drivers os that they can report back
  *     the actual speed selected when it differs from the speed requested
  *
- *     For now input and output speed must agree.
+ *     For maximal back compatibility with legacy SYS5/POSIX *nix behaviour
+ *     we need to carefully set the bits when the user does not get the
+ *     desired speed. We allow small margins and preserve as much of possible
+ *     of the input intent to keep compatiblity.
  *
  *     Locking: Caller should hold termios lock. This is already held
  *     when calling this function from the driver termios handler.
@@ -242,32 +245,44 @@ EXPORT_SYMBOL(tty_termios_input_baud_rate);
 void tty_termios_encode_baud_rate(struct ktermios *termios, speed_t ibaud, speed_t obaud)
 {
        int i = 0;
-       int ifound = 0, ofound = 0;
+       int ifound = -1, ofound = -1;
+       int iclose = ibaud/50, oclose = obaud/50;
+       int ibinput = 0;
 
        termios->c_ispeed = ibaud;
        termios->c_ospeed = obaud;
 
+       /* If the user asked for a precise weird speed give a precise weird
+          answer. If they asked for a Bfoo speed they many have problems
+          digesting non-exact replies so fuzz a bit */
+
+       if ((termios->c_cflag & CBAUD) == BOTHER)
+               oclose = 0;
+       if (((termios->c_cflag >> IBSHIFT) & CBAUD) == BOTHER)
+               iclose = 0;
+       if ((termios->c_cflag >> IBSHIFT) & CBAUD)
+               ibinput = 1;    /* An input speed was specified */
+
        termios->c_cflag &= ~CBAUD;
-       /* Identical speed means no input encoding (ie B0 << IBSHIFT)*/
-       if (termios->c_ispeed == termios->c_ospeed)
-               ifound = 1;
 
        do {
-               if (obaud == baud_table[i]) {
+               if (obaud - oclose >= baud_table[i] && obaud + oclose <= baud_table[i]) {
                        termios->c_cflag |= baud_bits[i];
-                       ofound = 1;
-                       /* So that if ibaud == obaud we don't set it */
-                       continue;
+                       ofound = i;
                }
-               if (ibaud == baud_table[i]) {
-                       termios->c_cflag |= (baud_bits[i] << IBSHIFT);
-                       ifound = 1;
+               if (ibaud - iclose >= baud_table[i] && ibaud + iclose <= baud_table[i]) {
+                       /* For the case input == output don't set IBAUD bits if the user didn't do so */
+                       if (ofound != i || ibinput)
+                               termios->c_cflag |= (baud_bits[i] << IBSHIFT);
+                       ifound = i;
                }
        }
        while(++i < n_baud_table);
-       if (!ofound)
+       if (ofound == -1)
                termios->c_cflag |= BOTHER;
-       if (!ifound)
+       /* Set exact input bits only if the input and output differ or the
+          user already did */
+       if (ifound == -1 && (ibaud != obaud  || ibinput))
                termios->c_cflag |= (BOTHER << IBSHIFT);
 }
 
@@ -399,23 +414,27 @@ static int set_termios(struct tty_struct * tty, void __user *arg, int opt)
        if (retval)
                return retval;
 
+       memcpy(&tmp_termios, tty->termios, sizeof(struct ktermios));
+
        if (opt & TERMIOS_TERMIO) {
-               memcpy(&tmp_termios, tty->termios, sizeof(struct ktermios));
                if (user_termio_to_kernel_termios(&tmp_termios,
                                                (struct termio __user *)arg))
                        return -EFAULT;
 #ifdef TCGETS2
        } else if (opt & TERMIOS_OLD) {
-               memcpy(&tmp_termios, tty->termios, sizeof(struct termios));
                if (user_termios_to_kernel_termios_1(&tmp_termios,
-                                               (struct termios_v1 __user *)arg))
+                                               (struct termios __user *)arg))
                        return -EFAULT;
-#endif
        } else {
                if (user_termios_to_kernel_termios(&tmp_termios,
-                                               (struct termios __user *)arg))
+                                               (struct termios2 __user *)arg))
                        return -EFAULT;
        }
+#else
+       } else if (user_termios_to_kernel_termios(&tmp_termios,
+                                       (struct termios __user *)arg))
+               return -EFAULT;
+#endif
 
        /* If old style Bfoo values are used then load c_ispeed/c_ospeed with the real speed
           so its unconditionally usable */
@@ -495,8 +514,8 @@ static int get_sgttyb(struct tty_struct * tty, struct sgttyb __user * sgttyb)
        struct sgttyb tmp;
 
        mutex_lock(&tty->termios_mutex);
-       tmp.sg_ispeed = tty->c_ispeed;
-       tmp.sg_ospeed = tty->c_ospeed;
+       tmp.sg_ispeed = tty->termios->c_ispeed;
+       tmp.sg_ospeed = tty->termios->c_ospeed;
        tmp.sg_erase = tty->termios->c_cc[VERASE];
        tmp.sg_kill = tty->termios->c_cc[VKILL];
        tmp.sg_flags = get_sgflags(tty);
@@ -707,11 +726,11 @@ int n_tty_ioctl(struct tty_struct * tty, struct file * file,
                        return 0;
 #else
                case TCGETS:
-                       if (kernel_termios_to_user_termios_1((struct termios_v1 __user *)arg, real_tty->termios))
+                       if (kernel_termios_to_user_termios_1((struct termios __user *)arg, real_tty->termios))
                                return -EFAULT;
                        return 0;
                case TCGETS2:
-                       if (kernel_termios_to_user_termios((struct termios __user *)arg, real_tty->termios))
+                       if (kernel_termios_to_user_termios((struct termios2 __user *)arg, real_tty->termios))
                                return -EFAULT;
                        return 0;
                case TCSETSF2: