TTY: add support for unnumbered device nodes
[linux-2.6-block.git] / drivers / tty / tty_io.c
index ca7c25d9f6d5347322cf80f7ac54d5c47df9c7ab..4d9898c2b641d8981c505d150f19db89b5313de9 100644 (file)
@@ -181,6 +181,8 @@ struct tty_struct *alloc_tty_struct(void)
 
 void free_tty_struct(struct tty_struct *tty)
 {
+       if (!tty)
+               return;
        if (tty->dev)
                put_device(tty->dev);
        kfree(tty->write_buf);
@@ -1214,7 +1216,10 @@ static void pty_line_name(struct tty_driver *driver, int index, char *p)
  */
 static void tty_line_name(struct tty_driver *driver, int index, char *p)
 {
-       sprintf(p, "%s%d", driver->name, index + driver->name_base);
+       if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE)
+               strcpy(p, driver->name);
+       else
+               sprintf(p, "%s%d", driver->name, index + driver->name_base);
 }
 
 /**
@@ -1250,21 +1255,19 @@ int tty_init_termios(struct tty_struct *tty)
        struct ktermios *tp;
        int idx = tty->index;
 
-       tp = tty->driver->termios[idx];
-       if (tp == NULL) {
-               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;
+       if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
+               tty->termios = tty->driver->init_termios;
+       else {
+               /* Check for lazy saved data */
+               tp = tty->driver->termios[idx];
+               if (tp != NULL)
+                       tty->termios = *tp;
+               else
+                       tty->termios = tty->driver->init_termios;
        }
-       tty->termios = tp;
-       tty->termios_locked = tp + 1;
-
        /* Compatibility until drivers always set this */
-       tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
-       tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
+       tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
+       tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
        return 0;
 }
 EXPORT_SYMBOL_GPL(tty_init_termios);
@@ -1444,22 +1447,25 @@ void tty_free_termios(struct tty_struct *tty)
 {
        struct ktermios *tp;
        int idx = tty->index;
-       /* Kill this flag and push into drivers for locking etc */
-       if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
-               /* FIXME: Locking on ->termios array */
-               tp = tty->termios;
-               tty->driver->termios[idx] = NULL;
-               kfree(tp);
+
+       /* If the port is going to reset then it has no termios to save */
+       if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
+               return;
+
+       /* Stash the termios data */
+       tp = tty->driver->termios[idx];
+       if (tp == NULL) {
+               tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
+               if (tp == NULL) {
+                       pr_warn("tty: no memory to save termios state.\n");
+                       return;
+               }
+               tty->driver->termios[idx] = tp;
        }
+       *tp = tty->termios;
 }
 EXPORT_SYMBOL(tty_free_termios);
 
-void tty_shutdown(struct tty_struct *tty)
-{
-       tty_driver_remove_tty(tty->driver, tty);
-       tty_free_termios(tty);
-}
-EXPORT_SYMBOL(tty_shutdown);
 
 /**
  *     release_one_tty         -       release tty structure memory
@@ -1470,7 +1476,6 @@ EXPORT_SYMBOL(tty_shutdown);
  *     in use. It also gets called when setup of a device fails.
  *
  *     Locking:
- *             tty_mutex - sometimes only
  *             takes the file list lock internally when working on the list
  *     of ttys that the driver keeps.
  *
@@ -1503,11 +1508,6 @@ static void queue_release_one_tty(struct kref *kref)
 {
        struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
 
-       if (tty->ops->shutdown)
-               tty->ops->shutdown(tty);
-       else
-               tty_shutdown(tty);
-
        /* The hangup queue is now free so we can reuse it rather than
           waste a chunk of memory for each port */
        INIT_WORK(&tty->hangup_work, release_one_tty);
@@ -1536,16 +1536,20 @@ EXPORT_SYMBOL(tty_kref_put);
  *     and decrement the refcount of the backing module.
  *
  *     Locking:
- *             tty_mutex - sometimes only
+ *             tty_mutex
  *             takes the file list lock internally when working on the list
  *     of ttys that the driver keeps.
- *             FIXME: should we require tty_mutex is held here ??
  *
  */
 static void release_tty(struct tty_struct *tty, int idx)
 {
        /* This should always be true but check for the moment */
        WARN_ON(tty->index != idx);
+       WARN_ON(!mutex_is_locked(&tty_mutex));
+       if (tty->ops->shutdown)
+               tty->ops->shutdown(tty);
+       tty_free_termios(tty);
+       tty_driver_remove_tty(tty->driver, tty);
 
        if (tty->link)
                tty_kref_put(tty->link);
@@ -1580,22 +1584,12 @@ static int tty_release_checks(struct tty_struct *tty, struct tty_struct *o_tty,
                                __func__, idx, tty->name);
                return -1;
        }
-       if (tty->termios != tty->driver->termios[idx]) {
-               printk(KERN_DEBUG "%s: driver.termios[%d] not termios for (%s)\n",
-                               __func__, idx, tty->name);
-               return -1;
-       }
        if (tty->driver->other) {
                if (o_tty != tty->driver->other->ttys[idx]) {
                        printk(KERN_DEBUG "%s: other->table[%d] not o_tty for (%s)\n",
                                        __func__, idx, tty->name);
                        return -1;
                }
-               if (o_tty->termios != tty->driver->other->termios[idx]) {
-                       printk(KERN_DEBUG "%s: other->termios[%d] not o_termios for (%s)\n",
-                                       __func__, idx, tty->name);
-                       return -1;
-               }
                if (o_tty->link != tty) {
                        printk(KERN_DEBUG "%s: bad pty pointers\n", __func__);
                        return -1;
@@ -1724,6 +1718,9 @@ int tty_release(struct inode *inode, struct file *filp)
         * The closing flags are now consistent with the open counts on
         * both sides, and we've completed the last operation that could
         * block, so it's safe to proceed with closing.
+        *
+        * We must *not* drop the tty_mutex until we ensure that a further
+        * entry into tty_open can not pick up this tty.
         */
        if (pty_master) {
                if (--o_tty->count < 0) {
@@ -1775,12 +1772,13 @@ int tty_release(struct inode *inode, struct file *filp)
        }
 
        mutex_unlock(&tty_mutex);
+       tty_unlock_pair(tty, o_tty);
+       /* At this point the TTY_CLOSING flag should ensure a dead tty
+          cannot be re-opened by a racing opener */
 
        /* check whether both sides are closing ... */
-       if (!tty_closing || (o_tty && !o_tty_closing)) {
-               tty_unlock_pair(tty, o_tty);
+       if (!tty_closing || (o_tty && !o_tty_closing))
                return 0;
-       }
 
 #ifdef TTY_DEBUG_HANGUP
        printk(KERN_DEBUG "%s: freeing tty structure...\n", __func__);
@@ -1795,8 +1793,9 @@ int tty_release(struct inode *inode, struct file *filp)
         * should be safe as we keep a kref while the tty is locked (so the
         * unlock never unlocks a freed tty).
         */
+       mutex_lock(&tty_mutex);
        release_tty(tty, idx);
-       tty_unlock_pair(tty, o_tty);
+       mutex_unlock(&tty_mutex);
 
        /* Make this pty number available for reallocation */
        if (devpts)
@@ -3065,28 +3064,69 @@ void tty_unregister_device(struct tty_driver *driver, unsigned index)
 }
 EXPORT_SYMBOL(tty_unregister_device);
 
-struct tty_driver *__alloc_tty_driver(int lines, struct module *owner)
+/**
+ * __tty_alloc_driver -- allocate tty driver
+ * @lines: count of lines this driver can handle at most
+ * @owner: module which is repsonsible for this driver
+ * @flags: some of TTY_DRIVER_* flags, will be set in driver->flags
+ *
+ * This should not be called directly, some of the provided macros should be
+ * used instead. Use IS_ERR and friends on @retval.
+ */
+struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner,
+               unsigned long flags)
 {
        struct tty_driver *driver;
+       int err;
+
+       if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1))
+               return ERR_PTR(-EINVAL);
 
        driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
-       if (driver) {
-               kref_init(&driver->kref);
-               driver->magic = TTY_DRIVER_MAGIC;
-               driver->num = lines;
-               driver->owner = owner;
-               /* later we'll move allocation of tables here */
+       if (!driver)
+               return ERR_PTR(-ENOMEM);
+
+       kref_init(&driver->kref);
+       driver->magic = TTY_DRIVER_MAGIC;
+       driver->num = lines;
+       driver->owner = owner;
+       driver->flags = flags;
+
+       if (!(flags & TTY_DRIVER_DEVPTS_MEM)) {
+               driver->ttys = kcalloc(lines, sizeof(*driver->ttys),
+                               GFP_KERNEL);
+               driver->termios = kcalloc(lines, sizeof(*driver->termios),
+                               GFP_KERNEL);
+               if (!driver->ttys || !driver->termios) {
+                       err = -ENOMEM;
+                       goto err_free_all;
+               }
+       }
+
+       if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
+               driver->ports = kcalloc(lines, sizeof(*driver->ports),
+                               GFP_KERNEL);
+               if (!driver->ports) {
+                       err = -ENOMEM;
+                       goto err_free_all;
+               }
        }
+
        return driver;
+err_free_all:
+       kfree(driver->ports);
+       kfree(driver->ttys);
+       kfree(driver->termios);
+       kfree(driver);
+       return ERR_PTR(err);
 }
-EXPORT_SYMBOL(__alloc_tty_driver);
+EXPORT_SYMBOL(__tty_alloc_driver);
 
 static void destruct_tty_driver(struct kref *kref)
 {
        struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
        int i;
        struct ktermios *tp;
-       void *p;
 
        if (driver->flags & TTY_DRIVER_INSTALLED) {
                /*
@@ -3103,14 +3143,12 @@ static void destruct_tty_driver(struct kref *kref)
                        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 = NULL;
-               kfree(p);
                cdev_del(&driver->cdev);
        }
        kfree(driver->ports);
+       kfree(driver->termios);
+       kfree(driver->ttys);
        kfree(driver);
 }
 
@@ -3141,27 +3179,8 @@ int tty_register_driver(struct tty_driver *driver)
        int error;
        int i;
        dev_t dev;
-       void **p = NULL;
        struct device *d;
 
-       if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
-               p = kzalloc(driver->num * 2 * sizeof(void *), GFP_KERNEL);
-               if (!p)
-                       return -ENOMEM;
-       }
-       /*
-        * There is too many lines in PTY and we won't need the array there
-        * since it has an ->install hook where it assigns ports properly.
-        */
-       if (driver->type != TTY_DRIVER_TYPE_PTY) {
-               driver->ports = kcalloc(driver->num, sizeof(struct tty_port *),
-                               GFP_KERNEL);
-               if (!driver->ports) {
-                       error = -ENOMEM;
-                       goto err_free_p;
-               }
-       }
-
        if (!driver->major) {
                error = alloc_chrdev_region(&dev, driver->minor_start,
                                                driver->num, driver->name);
@@ -3174,15 +3193,7 @@ int tty_register_driver(struct tty_driver *driver)
                error = register_chrdev_region(dev, driver->num, driver->name);
        }
        if (error < 0)
-               goto err_free_p;
-
-       if (p) {
-               driver->ttys = (struct tty_struct **)p;
-               driver->termios = (struct ktermios **)(p + driver->num);
-       } else {
-               driver->ttys = NULL;
-               driver->termios = NULL;
-       }
+               goto err;
 
        cdev_init(&driver->cdev, &tty_fops);
        driver->cdev.owner = driver->owner;
@@ -3199,7 +3210,7 @@ int tty_register_driver(struct tty_driver *driver)
                        d = tty_register_device(driver, i, NULL);
                        if (IS_ERR(d)) {
                                error = PTR_ERR(d);
-                               goto err;
+                               goto err_unreg_devs;
                        }
                }
        }
@@ -3207,7 +3218,7 @@ int tty_register_driver(struct tty_driver *driver)
        driver->flags |= TTY_DRIVER_INSTALLED;
        return 0;
 
-err:
+err_unreg_devs:
        for (i--; i >= 0; i--)
                tty_unregister_device(driver, i);
 
@@ -3217,10 +3228,7 @@ err:
 
 err_unreg_char:
        unregister_chrdev_region(dev, driver->num);
-       driver->ttys = NULL;
-       driver->termios = NULL;
-err_free_p: /* destruct_tty_driver will free driver->ports */
-       kfree(p);
+err:
        return error;
 }
 EXPORT_SYMBOL(tty_register_driver);