]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/char/tty_io.c
tty: Finish fixing up the init_dev interface to use ERR_PTR
[linux-2.6-omap-h63xx.git] / drivers / char / tty_io.c
index 47aa437effe204289cb37388968d71c363506fc5..b0ad4880c3a822cf99a88c8a1a62772dbf05539e 100644 (file)
@@ -1189,7 +1189,7 @@ static void pty_line_name(struct tty_driver *driver, int index, char *p)
 }
 
 /**
- *     pty_line_name   -       generate name for a tty
+ *     tty_line_name   -       generate name for a tty
  *     @driver: the tty driver in use
  *     @index: the minor number
  *     @p: output buffer of at least 7 bytes
@@ -1222,13 +1222,51 @@ struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver, int idx)
        if (driver->ops->lookup)
                return driver->ops->lookup(driver, idx);
 
-               tty = driver->ttys[idx];
+       tty = driver->ttys[idx];
        return tty;
 }
 
 /**
- *     tty_reopen()    - fast re-open of an open tty
- *     @tty    - the tty to open
+ *     tty_driver_install_tty() - install a tty entry in the driver
+ *     @driver: the driver for the tty
+ *     @tty: the tty
+ *
+ *     Install a tty object into the driver tables. The tty->index field
+ *     will be set by the time this is called.
+ *
+ *     Locking: tty_mutex for now
+ */
+static int tty_driver_install_tty(struct tty_driver *driver,
+                                               struct tty_struct *tty)
+{
+       if (driver->ops->install)
+               return driver->ops->install(driver, tty);
+       driver->ttys[tty->index] = tty;
+       return 0;
+}
+
+/**
+ *     tty_driver_remove_tty() - remove a tty from the driver tables
+ *     @driver: the driver for the tty
+ *     @idx:    the minor number
+ *
+ *     Remvoe a tty object from the driver tables. The tty->index field
+ *     will be set by the time this is called.
+ *
+ *     Locking: tty_mutex for now
+ */
+static void tty_driver_remove_tty(struct tty_driver *driver,
+                                               struct tty_struct *tty)
+{
+       if (driver->ops->remove)
+               driver->ops->remove(driver, tty);
+       else
+               driver->ttys[tty->index] = NULL;
+}
+
+/*
+ *     tty_reopen()    - fast re-open of an open tty
+ *     @tty    - the tty to open
  *
  *     Return 0 on success, -errno on error.
  *
@@ -1286,35 +1324,32 @@ static int tty_reopen(struct tty_struct *tty)
  * relaxed for the (most common) case of reopening a tty.
  */
 
-int tty_init_dev(struct tty_driver *driver, int idx,
-       struct tty_struct **ret_tty, int first_ok)
+struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,
+                                                               int first_ok)
 {
        struct tty_struct *tty, *o_tty;
        struct ktermios *tp, **tp_loc, *o_tp, **o_tp_loc;
        struct ktermios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
-       int retval = 0;
+       int retval;
 
        /* check whether we're reopening an existing tty */
        tty = tty_driver_lookup_tty(driver, idx);
-       if (IS_ERR(tty)) {
-               retval = PTR_ERR(tty);
-               goto end_init;
-       }
+
+       if (IS_ERR(tty))
+               return tty;
 
        if (tty) {
                retval = tty_reopen(tty);
                if (retval)
-                       return retval;
-               *ret_tty = tty;
-               return 0;
+                       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) {
-               retval = -EIO;
-               goto end_init;
-       }
+               (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok)
+               return ERR_PTR(-EIO);
+
        /*
         * First time open is complex, especially for PTY devices.
         * This code guarantees that either everything succeeds and the
@@ -1323,10 +1358,8 @@ int tty_init_dev(struct tty_driver *driver, int idx,
         * and locked termios may be retained.)
         */
 
-       if (!try_module_get(driver->owner)) {
-               retval = -ENODEV;
-               goto end_init;
-       }
+       if (!try_module_get(driver->owner))
+               return ERR_PTR(-ENODEV);
 
        o_tty = NULL;
        tp = o_tp = NULL;
@@ -1423,11 +1456,7 @@ int tty_init_dev(struct tty_driver *driver, int idx,
         * All structures have been allocated, so now we install them.
         * Failures after this point use release_tty to clean up, so
         * there's no need to null out the local pointers.
-        *
-        * FIXME: We want a 'driver->install method ?
         */
-       if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM))
-               driver->ttys[idx] = tty;
 
        if (!*tp_loc)
                *tp_loc = tp;
@@ -1441,6 +1470,10 @@ int tty_init_dev(struct tty_driver *driver, int idx,
        tty_driver_kref_get(driver);
        tty->count++;
 
+       retval = tty_driver_install_tty(driver, tty);
+       if (retval < 0)
+               goto release_mem_out;
+
        /*
         * Structures all installed ... call the ldisc open routines.
         * If we fail here just call release_tty to clean up.  No need
@@ -1448,14 +1481,9 @@ int tty_init_dev(struct tty_driver *driver, int idx,
         */
 
        retval = tty_ldisc_setup(tty, o_tty);
-
        if (retval)
                goto release_mem_out;
-
-       *ret_tty = tty;
-       /* All paths come through here to release the mutex */
-end_init:
-       return retval;
+       return tty;
 
        /* Release locally allocated memory ... nothing placed in slots */
 free_mem_out:
@@ -1470,8 +1498,7 @@ free_mem_out:
 
 fail_no_mem:
        module_put(driver->owner);
-       retval = -ENOMEM;
-       goto end_init;
+       return ERR_PTR(-ENOMEM);
 
        /* call the tty release_tty routine to clean out this slot */
 release_mem_out:
@@ -1479,7 +1506,7 @@ release_mem_out:
                printk(KERN_INFO "tty_init_dev: ldisc open failed, "
                                 "clearing slot %d\n", idx);
        release_tty(tty, idx);
-       goto end_init;
+       return ERR_PTR(retval);
 }
 
 void tty_free_termios(struct tty_struct *tty)
@@ -1502,7 +1529,7 @@ EXPORT_SYMBOL(tty_free_termios);
 
 void tty_shutdown(struct tty_struct *tty)
 {
-       tty->driver->ttys[tty->index] = NULL;
+       tty_driver_remove_tty(tty->driver, tty);
        tty_free_termios(tty);
 }
 EXPORT_SYMBOL(tty_shutdown);
@@ -1615,7 +1642,7 @@ void tty_release_dev(struct file *filp)
                                  "free (%s)\n", tty->name);
                return;
        }
-       if (!(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
+       if (!devpts) {
                if (tty != tty->driver->ttys[idx]) {
                        printk(KERN_DEBUG "tty_release_dev: driver.table[%d] not tty "
                               "for (%s)\n", idx, tty->name);
@@ -1888,11 +1915,11 @@ retry_open:
                return -ENODEV;
        }
 got_driver:
-       retval = tty_init_dev(driver, index, &tty, 0);
+       tty = tty_init_dev(driver, index, 0);
        mutex_unlock(&tty_mutex);
        tty_driver_kref_put(driver);
-       if (retval)
-               return retval;
+       if (IS_ERR(tty))
+               return PTR_ERR(tty);
 
        filp->private_data = tty;
        file_move(filp, &tty->tty_files);