ipv4: don't remove /proc/net/rt_acct
[safe/jmp/linux-2.6] / drivers / char / tty_port.c
index b580fcf..be492dd 100644 (file)
@@ -23,20 +23,23 @@ void tty_port_init(struct tty_port *port)
        memset(port, 0, sizeof(*port));
        init_waitqueue_head(&port->open_wait);
        init_waitqueue_head(&port->close_wait);
+       init_waitqueue_head(&port->delta_msr_wait);
        mutex_init(&port->mutex);
+       mutex_init(&port->buf_mutex);
        spin_lock_init(&port->lock);
        port->close_delay = (50 * HZ) / 100;
        port->closing_wait = (3000 * HZ) / 100;
+       kref_init(&port->kref);
 }
 EXPORT_SYMBOL(tty_port_init);
 
 int tty_port_alloc_xmit_buf(struct tty_port *port)
 {
        /* We may sleep in get_zeroed_page() */
-       mutex_lock(&port->mutex);
+       mutex_lock(&port->buf_mutex);
        if (port->xmit_buf == NULL)
                port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
-       mutex_unlock(&port->mutex);
+       mutex_unlock(&port->buf_mutex);
        if (port->xmit_buf == NULL)
                return -ENOMEM;
        return 0;
@@ -45,15 +48,32 @@ EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
 
 void tty_port_free_xmit_buf(struct tty_port *port)
 {
-       mutex_lock(&port->mutex);
+       mutex_lock(&port->buf_mutex);
        if (port->xmit_buf != NULL) {
                free_page((unsigned long)port->xmit_buf);
                port->xmit_buf = NULL;
        }
-       mutex_unlock(&port->mutex);
+       mutex_unlock(&port->buf_mutex);
 }
 EXPORT_SYMBOL(tty_port_free_xmit_buf);
 
+static void tty_port_destructor(struct kref *kref)
+{
+       struct tty_port *port = container_of(kref, struct tty_port, kref);
+       if (port->xmit_buf)
+               free_page((unsigned long)port->xmit_buf);
+       if (port->ops->destruct)
+               port->ops->destruct(port);
+       else
+               kfree(port);
+}
+
+void tty_port_put(struct tty_port *port)
+{
+       if (port)
+               kref_put(&port->kref, tty_port_destructor);
+}
+EXPORT_SYMBOL(tty_port_put);
 
 /**
  *     tty_port_tty_get        -       get a tty reference
@@ -96,6 +116,15 @@ void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
 }
 EXPORT_SYMBOL(tty_port_tty_set);
 
+static void tty_port_shutdown(struct tty_port *port)
+{
+       mutex_lock(&port->mutex);
+       if (port->ops->shutdown &&
+               test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags))
+                       port->ops->shutdown(port);
+       mutex_unlock(&port->mutex);
+}
+
 /**
  *     tty_port_hangup         -       hangup helper
  *     @port: tty port
@@ -111,11 +140,15 @@ void tty_port_hangup(struct tty_port *port)
        spin_lock_irqsave(&port->lock, flags);
        port->count = 0;
        port->flags &= ~ASYNC_NORMAL_ACTIVE;
-       if (port->tty)
+       if (port->tty) {
+               set_bit(TTY_IO_ERROR, &port->tty->flags);
                tty_kref_put(port->tty);
+       }
        port->tty = NULL;
        spin_unlock_irqrestore(&port->lock, flags);
        wake_up_interruptible(&port->open_wait);
+       wake_up_interruptible(&port->delta_msr_wait);
+       tty_port_shutdown(port);
 }
 EXPORT_SYMBOL(tty_port_hangup);
 
@@ -137,7 +170,7 @@ int tty_port_carrier_raised(struct tty_port *port)
 EXPORT_SYMBOL(tty_port_carrier_raised);
 
 /**
- *     tty_port_raise_dtr_rts  -       Riase DTR/RTS
+ *     tty_port_raise_dtr_rts  -       Raise DTR/RTS
  *     @port: tty port
  *
  *     Wrapper for the DTR/RTS raise logic. For the moment this is used
@@ -147,12 +180,28 @@ EXPORT_SYMBOL(tty_port_carrier_raised);
 
 void tty_port_raise_dtr_rts(struct tty_port *port)
 {
-       if (port->ops->raise_dtr_rts)
-               port->ops->raise_dtr_rts(port);
+       if (port->ops->dtr_rts)
+               port->ops->dtr_rts(port, 1);
 }
 EXPORT_SYMBOL(tty_port_raise_dtr_rts);
 
 /**
+ *     tty_port_lower_dtr_rts  -       Lower DTR/RTS
+ *     @port: tty port
+ *
+ *     Wrapper for the DTR/RTS raise logic. For the moment this is used
+ *     to hide some internal details. This will eventually become entirely
+ *     internal to the tty port.
+ */
+
+void tty_port_lower_dtr_rts(struct tty_port *port)
+{
+       if (port->ops->dtr_rts)
+               port->ops->dtr_rts(port, 0);
+}
+EXPORT_SYMBOL(tty_port_lower_dtr_rts);
+
+/**
  *     tty_port_block_til_ready        -       Waiting logic for tty open
  *     @port: the tty port being opened
  *     @tty: the tty device being bound
@@ -167,22 +216,23 @@ EXPORT_SYMBOL(tty_port_raise_dtr_rts);
  *             - port flags and counts
  *
  *     The passed tty_port must implement the carrier_raised method if it can
- *     do carrier detect and the raise_dtr_rts method if it supports software
+ *     do carrier detect and the dtr_rts method if it supports software
  *     management of these lines. Note that the dtr/rts raise is done each
  *     iteration as a hangup may have previously dropped them while we wait.
  */
+
 int tty_port_block_til_ready(struct tty_port *port,
                                struct tty_struct *tty, struct file *filp)
 {
        int do_clocal = 0, retval;
        unsigned long flags;
-       DECLARE_WAITQUEUE(wait, current);
+       DEFINE_WAIT(wait);
        int cd;
 
        /* block if port is in the process of being closed */
        if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
-               interruptible_sleep_on(&port->close_wait);
+               wait_event_interruptible(port->close_wait,
+                               !(port->flags & ASYNC_CLOSING));
                if (port->flags & ASYNC_HUP_NOTIFY)
                        return -EAGAIN;
                else
@@ -191,8 +241,14 @@ int tty_port_block_til_ready(struct tty_port *port,
 
        /* if non-blocking mode is set we can pass directly to open unless
           the port has just hung up or is in another error state */
-       if ((filp->f_flags & O_NONBLOCK) ||
-                       (tty->flags & (1 << TTY_IO_ERROR))) {
+       if (tty->flags & (1 << TTY_IO_ERROR)) {
+               port->flags |= ASYNC_NORMAL_ACTIVE;
+               return 0;
+       }
+       if (filp->f_flags & O_NONBLOCK) {
+               /* Indicate we are open */
+               if (tty->termios->c_cflag & CBAUD)
+                       tty_port_raise_dtr_rts(port);
                port->flags |= ASYNC_NORMAL_ACTIVE;
                return 0;
        }
@@ -205,7 +261,6 @@ int tty_port_block_til_ready(struct tty_port *port,
           before the next open may complete */
 
        retval = 0;
-       add_wait_queue(&port->open_wait, &wait);
 
        /* The port lock protects the port counts */
        spin_lock_irqsave(&port->lock, flags);
@@ -219,8 +274,9 @@ int tty_port_block_til_ready(struct tty_port *port,
                if (tty->termios->c_cflag & CBAUD)
                        tty_port_raise_dtr_rts(port);
 
-               set_current_state(TASK_INTERRUPTIBLE);
-               /* Check for a hangup or uninitialised port. Return accordingly */
+               prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
+               /* Check for a hangup or uninitialised port.
+                                                       Return accordingly */
                if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
                        if (port->flags & ASYNC_HUP_NOTIFY)
                                retval = -EAGAIN;
@@ -240,8 +296,7 @@ int tty_port_block_til_ready(struct tty_port *port,
                }
                schedule();
        }
-       set_current_state(TASK_RUNNING);
-       remove_wait_queue(&port->open_wait, &wait);
+       finish_wait(&port->open_wait, &wait);
 
        /* Update counts. A parallel hangup will have set count to zero and
           we must not mess that up further */
@@ -252,12 +307,12 @@ int tty_port_block_til_ready(struct tty_port *port,
        if (retval == 0)
                port->flags |= ASYNC_NORMAL_ACTIVE;
        spin_unlock_irqrestore(&port->lock, flags);
-       return 0;
-       
+       return retval;
 }
 EXPORT_SYMBOL(tty_port_block_til_ready);
 
-int tty_port_close_start(struct tty_port *port, struct tty_struct *tty, struct file *filp)
+int tty_port_close_start(struct tty_port *port,
+                               struct tty_struct *tty, struct file *filp)
 {
        unsigned long flags;
 
@@ -267,7 +322,7 @@ int tty_port_close_start(struct tty_port *port, struct tty_struct *tty, struct f
                return 0;
        }
 
-       iftty->count == 1 && port->count != 1) {
+       if (tty->count == 1 && port->count != 1) {
                printk(KERN_WARNING
                    "tty_port_close_start: tty->count = 1 port count = %d.\n",
                                                                port->count);
@@ -281,14 +336,41 @@ int tty_port_close_start(struct tty_port *port, struct tty_struct *tty, struct f
 
        if (port->count) {
                spin_unlock_irqrestore(&port->lock, flags);
+               if (port->ops->drop)
+                       port->ops->drop(port);
                return 0;
        }
-       port->flags |= ASYNC_CLOSING;
+       set_bit(ASYNCB_CLOSING, &port->flags);
        tty->closing = 1;
        spin_unlock_irqrestore(&port->lock, flags);
-       if (port->flags & ASYNC_INITIALIZED &&
+       /* Don't block on a stalled port, just pull the chain */
+       if (tty->flow_stopped)
+               tty_driver_flush_buffer(tty);
+       if (test_bit(ASYNCB_INITIALIZED, &port->flags) &&
                        port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
                tty_wait_until_sent(tty, port->closing_wait);
+       if (port->drain_delay) {
+               unsigned int bps = tty_get_baud_rate(tty);
+               long timeout;
+
+               if (bps > 1200)
+                       timeout = max_t(long,
+                               (HZ * 10 * port->drain_delay) / bps, HZ / 10);
+               else
+                       timeout = 2 * HZ;
+               schedule_timeout_interruptible(timeout);
+       }
+       /* Flush the ldisc buffering */
+       tty_ldisc_flush(tty);
+
+       /* Drop DTR/RTS if HUPCL is set. This causes any attached modem to
+          hang up the line */
+       if (tty->termios->c_cflag & HUPCL)
+               tty_port_lower_dtr_rts(port);
+
+       /* Don't call port->drop for the last reference. Callers will want
+          to drop the last active reference in ->shutdown() or the tty
+          shutdown path */
        return 1;
 }
 EXPORT_SYMBOL(tty_port_close_start);
@@ -297,8 +379,6 @@ void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
 {
        unsigned long flags;
 
-       tty_ldisc_flush(tty);
-
        spin_lock_irqsave(&port->lock, flags);
        tty->closing = 0;
 
@@ -316,3 +396,49 @@ void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
        spin_unlock_irqrestore(&port->lock, flags);
 }
 EXPORT_SYMBOL(tty_port_close_end);
+
+void tty_port_close(struct tty_port *port, struct tty_struct *tty,
+                                                       struct file *filp)
+{
+       if (tty_port_close_start(port, tty, filp) == 0)
+               return;
+       tty_port_shutdown(port);
+       set_bit(TTY_IO_ERROR, &tty->flags);
+       tty_port_close_end(port, tty);
+       tty_port_tty_set(port, NULL);
+}
+EXPORT_SYMBOL(tty_port_close);
+
+int tty_port_open(struct tty_port *port, struct tty_struct *tty,
+                                                       struct file *filp)
+{
+       spin_lock_irq(&port->lock);
+       if (!tty_hung_up_p(filp))
+               ++port->count;
+       spin_unlock_irq(&port->lock);
+       tty_port_tty_set(port, tty);
+
+       /*
+        * Do the device-specific open only if the hardware isn't
+        * already initialized. Serialize open and shutdown using the
+        * port mutex.
+        */
+
+       mutex_lock(&port->mutex);
+
+       if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
+               clear_bit(TTY_IO_ERROR, &tty->flags);
+               if (port->ops->activate) {
+                       int retval = port->ops->activate(port, tty);
+                       if (retval) {
+                               mutex_unlock(&port->mutex);
+                               return retval;
+                       }
+               }
+               set_bit(ASYNCB_INITIALIZED, &port->flags);
+       }
+       mutex_unlock(&port->mutex);
+       return tty_port_block_til_ready(port, tty, filp);
+}
+
+EXPORT_SYMBOL(tty_port_open);