V4L/DVB (10978): Report tuning algorith correctly
[safe/jmp/linux-2.6] / drivers / char / tty_io.c
index e881e9e..bc84e12 100644 (file)
@@ -1026,19 +1026,19 @@ static inline ssize_t do_tty_write(
 
        /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
        if (tty->write_cnt < chunk) {
-               unsigned char *buf;
+               unsigned char *buf_chunk;
 
                if (chunk < 1024)
                        chunk = 1024;
 
-               buf = kmalloc(chunk, GFP_KERNEL);
-               if (!buf) {
+               buf_chunk = kmalloc(chunk, GFP_KERNEL);
+               if (!buf_chunk) {
                        ret = -ENOMEM;
                        goto out;
                }
                kfree(tty->write_buf);
                tty->write_cnt = chunk;
-               tty->write_buf = buf;
+               tty->write_buf = buf_chunk;
        }
 
        /* Do the write .. */
@@ -1111,9 +1111,7 @@ void tty_write_message(struct tty_struct *tty, char *msg)
  *             Locks the line discipline as required
  *             Writes to the tty driver are serialized by the atomic_write_lock
  *     and are then processed in chunks to the device. The line discipline
- *     write method will not be involked in parallel for each device
- *             The line discipline write method is called under the big
- *     kernel lock for historical reasons. New code should not rely on this.
+ *     write method will not be invoked in parallel for each device.
  */
 
 static ssize_t tty_write(struct file *file, const char __user *buf,
@@ -1213,12 +1211,13 @@ static void tty_line_name(struct tty_driver *driver, int index, char *p)
  *     be held until the 'fast-open' is also done. Will change once we
  *     have refcounting in the driver and per driver locking
  */
-struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver, int idx)
+static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
+               struct inode *inode, int idx)
 {
        struct tty_struct *tty;
 
        if (driver->ops->lookup)
-               return driver->ops->lookup(driver, idx);
+               return driver->ops->lookup(driver, inode, idx);
 
        tty = driver->ttys[idx];
        return tty;
@@ -1234,27 +1233,20 @@ struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver, int idx)
 
 int tty_init_termios(struct tty_struct *tty)
 {
-       struct ktermios *tp, *ltp;
+       struct ktermios *tp;
        int idx = tty->index;
 
        tp = tty->driver->termios[idx];
-       ltp = tty->driver->termios_locked[idx];
        if (tp == NULL) {
-               WARN_ON(ltp != NULL);
-               tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
-               ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
-               if (tp == NULL || ltp == NULL) {
-                       kfree(tp);
-                       kfree(ltp);
+               tp = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
+               if (tp == NULL)
                        return -ENOMEM;
-               }
                memcpy(tp, &tty->driver->init_termios,
                                                sizeof(struct ktermios));
                tty->driver->termios[idx] = tp;
-               tty->driver->termios_locked[idx] = ltp;
        }
        tty->termios = tp;
-       tty->termios_locked = ltp;
+       tty->termios_locked = tp + 1;
 
        /* Compatibility until drivers always set this */
        tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
@@ -1376,19 +1368,6 @@ struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,
        struct tty_struct *tty;
        int retval;
 
-       /* check whether we're reopening an existing tty */
-       tty = tty_driver_lookup_tty(driver, idx);
-
-       if (IS_ERR(tty))
-               return tty;
-
-       if (tty) {
-               retval = tty_reopen(tty);
-               if (retval)
-                       return ERR_PTR(retval);
-               return tty;
-       }
-
        /* Check if pty master is being opened multiple times */
        if (driver->subtype == PTY_TYPE_MASTER &&
                (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok)
@@ -1451,10 +1430,6 @@ void tty_free_termios(struct tty_struct *tty)
                tp = tty->termios;
                tty->driver->termios[idx] = NULL;
                kfree(tp);
-
-               tp = tty->termios_locked;
-               tty->driver->termios_locked[idx] = NULL;
-               kfree(tp);
        }
 }
 EXPORT_SYMBOL(tty_free_termios);
@@ -1552,10 +1527,11 @@ void tty_release_dev(struct file *filp)
        int     devpts;
        int     idx;
        char    buf[64];
+       struct  inode *inode;
 
+       inode = filp->f_path.dentry->d_inode;
        tty = (struct tty_struct *)filp->private_data;
-       if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode,
-                                                       "tty_release_dev"))
+       if (tty_paranoia_check(tty, inode, "tty_release_dev"))
                return;
 
        check_tty_count(tty, "tty_release_dev");
@@ -1586,12 +1562,6 @@ void tty_release_dev(struct file *filp)
                               idx, tty->name);
                        return;
                }
-               if (tty->termios_locked != tty->driver->termios_locked[idx]) {
-                       printk(KERN_DEBUG "tty_release_dev: driver.termios_locked[%d] not "
-                              "termios_locked for (%s)\n",
-                              idx, tty->name);
-                       return;
-               }
        }
 #endif
 
@@ -1615,13 +1585,6 @@ void tty_release_dev(struct file *filp)
                               idx, tty->name);
                        return;
                }
-               if (o_tty->termios_locked !=
-                     tty->driver->other->termios_locked[idx]) {
-                       printk(KERN_DEBUG "tty_release_dev: other->termios_locked["
-                                         "%d] not o_termios_locked for (%s)\n",
-                              idx, tty->name);
-                       return;
-               }
                if (o_tty->link != tty) {
                        printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n");
                        return;
@@ -1764,7 +1727,7 @@ void tty_release_dev(struct file *filp)
 
        /* Make this pty number available for reallocation */
        if (devpts)
-               devpts_kill_index(idx);
+               devpts_kill_index(inode, idx);
 }
 
 /**
@@ -1790,7 +1753,7 @@ void tty_release_dev(struct file *filp)
 
 static int __tty_open(struct inode *inode, struct file *filp)
 {
-       struct tty_struct *tty;
+       struct tty_struct *tty = NULL;
        int noctty, retval;
        struct tty_driver *driver;
        int index;
@@ -1830,12 +1793,15 @@ retry_open:
        }
 #endif
        if (device == MKDEV(TTYAUX_MAJOR, 1)) {
-               driver = tty_driver_kref_get(console_device(&index));
-               if (driver) {
-                       /* Don't let /dev/console block */
-                       filp->f_flags |= O_NONBLOCK;
-                       noctty = 1;
-                       goto got_driver;
+               struct tty_driver *console_driver = console_device(&index);
+               if (console_driver) {
+                       driver = tty_driver_kref_get(console_driver);
+                       if (driver) {
+                               /* Don't let /dev/console block */
+                               filp->f_flags |= O_NONBLOCK;
+                               noctty = 1;
+                               goto got_driver;
+                       }
                }
                mutex_unlock(&tty_mutex);
                return -ENODEV;
@@ -1847,7 +1813,23 @@ retry_open:
                return -ENODEV;
        }
 got_driver:
-       tty = tty_init_dev(driver, index, 0);
+       if (!tty) {
+               /* check whether we're reopening an existing tty */
+               tty = tty_driver_lookup_tty(driver, inode, index);
+
+               if (IS_ERR(tty)) {
+                       mutex_unlock(&tty_mutex);
+                       return PTR_ERR(tty);
+               }
+       }
+
+       if (tty) {
+               retval = tty_reopen(tty);
+               if (retval)
+                       tty = ERR_PTR(retval);
+       } else
+               tty = tty_init_dev(driver, index, 0);
+
        mutex_unlock(&tty_mutex);
        tty_driver_kref_put(driver);
        if (IS_ERR(tty))
@@ -2036,6 +2018,7 @@ static int tiocsti(struct tty_struct *tty, char __user *p)
                return -EPERM;
        if (get_user(ch, p))
                return -EFAULT;
+       tty_audit_tiocsti(tty, ch);
        ld = tty_ldisc_ref_wait(tty);
        ld->ops->receive_buf(tty, &ch, &mbz, 1);
        tty_ldisc_deref(ld);
@@ -2067,7 +2050,6 @@ static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
 /**
  *     tty_do_resize           -       resize event
  *     @tty: tty being resized
- *     @real_tty: real tty (not the same as tty if using a pty/tty pair)
  *     @rows: rows (character)
  *     @cols: cols (character)
  *
@@ -2075,41 +2057,34 @@ static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
  *     peform a terminal resize correctly
  */
 
-int tty_do_resize(struct tty_struct *tty, struct tty_struct *real_tty,
-                                       struct winsize *ws)
+int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
 {
-       struct pid *pgrp, *rpgrp;
+       struct pid *pgrp;
        unsigned long flags;
 
-       /* For a PTY we need to lock the tty side */
-       mutex_lock(&real_tty->termios_mutex);
-       if (!memcmp(ws, &real_tty->winsize, sizeof(*ws)))
+       /* Lock the tty */
+       mutex_lock(&tty->termios_mutex);
+       if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
                goto done;
        /* Get the PID values and reference them so we can
           avoid holding the tty ctrl lock while sending signals */
        spin_lock_irqsave(&tty->ctrl_lock, flags);
        pgrp = get_pid(tty->pgrp);
-       rpgrp = get_pid(real_tty->pgrp);
        spin_unlock_irqrestore(&tty->ctrl_lock, flags);
 
        if (pgrp)
                kill_pgrp(pgrp, SIGWINCH, 1);
-       if (rpgrp != pgrp && rpgrp)
-               kill_pgrp(rpgrp, SIGWINCH, 1);
-
        put_pid(pgrp);
-       put_pid(rpgrp);
 
        tty->winsize = *ws;
-       real_tty->winsize = *ws;
 done:
-       mutex_unlock(&real_tty->termios_mutex);
+       mutex_unlock(&tty->termios_mutex);
        return 0;
 }
 
 /**
  *     tiocswinsz              -       implement window size set ioctl
- *     @tty; tty
+ *     @tty; tty side of tty
  *     @arg: user buffer for result
  *
  *     Copies the user idea of the window size to the kernel. Traditionally
@@ -2122,17 +2097,16 @@ done:
  *     then calls into the default method.
  */
 
-static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
-       struct winsize __user *arg)
+static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
 {
        struct winsize tmp_ws;
        if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
                return -EFAULT;
 
        if (tty->ops->resize)
-               return tty->ops->resize(tty, real_tty, &tmp_ws);
+               return tty->ops->resize(tty, &tmp_ws);
        else
-               return tty_do_resize(tty, real_tty, &tmp_ws);
+               return tty_do_resize(tty, &tmp_ws);
 }
 
 /**
@@ -2557,7 +2531,7 @@ long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
        case TIOCGWINSZ:
                return tiocgwinsz(real_tty, p);
        case TIOCSWINSZ:
-               return tiocswinsz(tty, real_tty, p);
+               return tiocswinsz(real_tty, p);
        case TIOCCONS:
                return real_tty != tty ? -EINVAL : tioccons(file);
        case FIONBIO:
@@ -2802,6 +2776,8 @@ void initialize_tty_struct(struct tty_struct *tty,
        INIT_WORK(&tty->hangup_work, do_tty_hangup);
        mutex_init(&tty->atomic_read_lock);
        mutex_init(&tty->atomic_write_lock);
+       mutex_init(&tty->output_lock);
+       mutex_init(&tty->echo_lock);
        spin_lock_init(&tty->read_lock);
        spin_lock_init(&tty->ctrl_lock);
        INIT_LIST_HEAD(&tty->tty_files);
@@ -2871,7 +2847,7 @@ struct device *tty_register_device(struct tty_driver *driver, unsigned index,
        else
                tty_line_name(driver, index, name);
 
-       return device_create_drvdata(tty_class, device, dev, NULL, name);
+       return device_create(tty_class, device, dev, NULL, name);
 }
 EXPORT_SYMBOL(tty_register_device);
 
@@ -2927,18 +2903,13 @@ static void destruct_tty_driver(struct kref *kref)
                                driver->termios[i] = NULL;
                                kfree(tp);
                        }
-                       tp = driver->termios_locked[i];
-                       if (tp) {
-                               driver->termios_locked[i] = NULL;
-                               kfree(tp);
-                       }
                        if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
                                tty_unregister_device(driver, i);
                }
                p = driver->ttys;
                proc_tty_unregister_driver(driver);
                driver->ttys = NULL;
-               driver->termios = driver->termios_locked = NULL;
+               driver->termios = NULL;
                kfree(p);
                cdev_del(&driver->cdev);
        }
@@ -2975,7 +2946,7 @@ int tty_register_driver(struct tty_driver *driver)
        void **p = NULL;
 
        if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
-               p = kzalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL);
+               p = kzalloc(driver->num * 2 * sizeof(void *), GFP_KERNEL);
                if (!p)
                        return -ENOMEM;
        }
@@ -2999,12 +2970,9 @@ int tty_register_driver(struct tty_driver *driver)
        if (p) {
                driver->ttys = (struct tty_struct **)p;
                driver->termios = (struct ktermios **)(p + driver->num);
-               driver->termios_locked = (struct ktermios **)
-                                                       (p + driver->num * 2);
        } else {
                driver->ttys = NULL;
                driver->termios = NULL;
-               driver->termios_locked = NULL;
        }
 
        cdev_init(&driver->cdev, &tty_fops);
@@ -3013,7 +2981,7 @@ int tty_register_driver(struct tty_driver *driver)
        if (error) {
                unregister_chrdev_region(dev, driver->num);
                driver->ttys = NULL;
-               driver->termios = driver->termios_locked = NULL;
+               driver->termios = NULL;
                kfree(p);
                return error;
        }
@@ -3061,11 +3029,12 @@ EXPORT_SYMBOL(tty_devnum);
 
 void proc_clear_tty(struct task_struct *p)
 {
+       unsigned long flags;
        struct tty_struct *tty;
-       spin_lock_irq(&p->sighand->siglock);
+       spin_lock_irqsave(&p->sighand->siglock, flags);
        tty = p->signal->tty;
        p->signal->tty = NULL;
-       spin_unlock_irq(&p->sighand->siglock);
+       spin_unlock_irqrestore(&p->sighand->siglock, flags);
        tty_kref_put(tty);
 }