locks: allow lockd to process blocked locks during grace period
[safe/jmp/linux-2.6] / drivers / char / generic_serial.c
index 8facf3e..19d3afb 100644 (file)
@@ -28,7 +28,6 @@
 #include <linux/interrupt.h>
 #include <linux/tty_flip.h>
 #include <linux/delay.h>
-#include <asm/semaphore.h>
 #include <asm/uaccess.h>
 
 #define DEBUG 
@@ -41,27 +40,27 @@ static int gs_debug;
 #define gs_dprintk(f, str...) /* nothing */
 #endif
 
-#define func_enter() gs_dprintk (GS_DEBUG_FLOW, "gs: enter %s\n", __FUNCTION__)
-#define func_exit()  gs_dprintk (GS_DEBUG_FLOW, "gs: exit  %s\n", __FUNCTION__)
+#define func_enter() gs_dprintk (GS_DEBUG_FLOW, "gs: enter %s\n", __func__)
+#define func_exit()  gs_dprintk (GS_DEBUG_FLOW, "gs: exit  %s\n", __func__)
 
 #define RS_EVENT_WRITE_WAKEUP  1
 
 module_param(gs_debug, int, 0644);
 
 
-void gs_put_char(struct tty_struct * tty, unsigned char ch)
+int gs_put_char(struct tty_struct * tty, unsigned char ch)
 {
        struct gs_port *port;
 
        func_enter (); 
 
-       if (!tty) return;
+       if (!tty) return 0;
 
        port = tty->driver_data;
 
-       if (!port) return;
+       if (!port) return 0;
 
-       if (! (port->flags & ASYNC_INITIALIZED)) return;
+       if (! (port->port.flags & ASYNC_INITIALIZED)) return 0;
 
        /* Take a lock on the serial tranmit buffer! */
        mutex_lock(& port->port_write_mutex);
@@ -69,7 +68,7 @@ void gs_put_char(struct tty_struct * tty, unsigned char ch)
        if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
                /* Sorry, buffer is full, drop character. Update statistics???? -- REW */
                mutex_unlock(&port->port_write_mutex);
-               return;
+               return 0;
        }
 
        port->xmit_buf[port->xmit_head++] = ch;
@@ -78,6 +77,7 @@ void gs_put_char(struct tty_struct * tty, unsigned char ch)
 
        mutex_unlock(&port->port_write_mutex);
        func_exit ();
+       return 1;
 }
 
 
@@ -103,7 +103,7 @@ int gs_write(struct tty_struct * tty,
 
        if (!port) return 0;
 
-       if (! (port->flags & ASYNC_INITIALIZED))
+       if (! (port->port.flags & ASYNC_INITIALIZED))
                return 0;
 
        /* get exclusive "write" access to this port (problem 3) */
@@ -141,13 +141,13 @@ int gs_write(struct tty_struct * tty,
        mutex_unlock(& port->port_write_mutex);
 
        gs_dprintk (GS_DEBUG_WRITE, "write: interrupts are %s\n", 
-                   (port->flags & GS_TX_INTEN)?"enabled": "disabled"); 
+                   (port->port.flags & GS_TX_INTEN)?"enabled": "disabled");
 
        if (port->xmit_cnt && 
            !tty->stopped && 
            !tty->hw_stopped &&
-           !(port->flags & GS_TX_INTEN)) {
-               port->flags |= GS_TX_INTEN;
+           !(port->port.flags & GS_TX_INTEN)) {
+               port->port.flags |= GS_TX_INTEN;
                port->rd->enable_tx_interrupts (port);
        }
        func_exit ();
@@ -208,7 +208,7 @@ static int gs_wait_tx_flushed (void * ptr, unsigned long timeout)
        gs_dprintk (GS_DEBUG_FLUSH, "port=%p.\n", port);
        if (port) {
                gs_dprintk (GS_DEBUG_FLUSH, "xmit_cnt=%x, xmit_buf=%p, tty=%p.\n", 
-               port->xmit_cnt, port->xmit_buf, port->tty);
+               port->xmit_cnt, port->xmit_buf, port->port.tty);
        }
 
        if (!port || port->xmit_cnt < 0 || !port->xmit_buf) {
@@ -217,7 +217,7 @@ static int gs_wait_tx_flushed (void * ptr, unsigned long timeout)
                return -EINVAL;  /* This is an error which we don't know how to handle. */
        }
 
-       rcib = gs_real_chars_in_buffer(port->tty);
+       rcib = gs_real_chars_in_buffer(port->port.tty);
 
        if(rcib <= 0) {
                gs_dprintk (GS_DEBUG_FLUSH, "nothing to wait for.\n");
@@ -236,7 +236,7 @@ static int gs_wait_tx_flushed (void * ptr, unsigned long timeout)
 
        /* the expression is actually jiffies < end_jiffies, but that won't
           work around the wraparound. Tricky eh? */
-       while ((charsleft = gs_real_chars_in_buffer (port->tty)) &&
+       while ((charsleft = gs_real_chars_in_buffer (port->port.tty)) &&
                time_after (end_jiffies, jiffies)) {
                /* Units check: 
                   chars * (bits/char) * (jiffies /sec) / (bits/sec) = jiffies!
@@ -309,7 +309,7 @@ void gs_flush_chars(struct tty_struct * tty)
        }
 
        /* Beats me -- REW */
-       port->flags |= GS_TX_INTEN;
+       port->port.flags |= GS_TX_INTEN;
        port->rd->enable_tx_interrupts (port);
        func_exit ();
 }
@@ -329,8 +329,8 @@ void gs_stop(struct tty_struct * tty)
 
        if (port->xmit_cnt && 
            port->xmit_buf && 
-           (port->flags & GS_TX_INTEN) ) {
-               port->flags &= ~GS_TX_INTEN;
+           (port->port.flags & GS_TX_INTEN) ) {
+               port->port.flags &= ~GS_TX_INTEN;
                port->rd->disable_tx_interrupts (port);
        }
        func_exit ();
@@ -349,8 +349,8 @@ void gs_start(struct tty_struct * tty)
 
        if (port->xmit_cnt && 
            port->xmit_buf && 
-           !(port->flags & GS_TX_INTEN) ) {
-               port->flags |= GS_TX_INTEN;
+           !(port->port.flags & GS_TX_INTEN) ) {
+               port->port.flags |= GS_TX_INTEN;
                port->rd->enable_tx_interrupts (port);
        }
        func_exit ();
@@ -365,7 +365,7 @@ static void gs_shutdown_port (struct gs_port *port)
        
        if (!port) return;
        
-       if (!(port->flags & ASYNC_INITIALIZED))
+       if (!(port->port.flags & ASYNC_INITIALIZED))
                return;
 
        spin_lock_irqsave(&port->driver_lock, flags);
@@ -375,12 +375,12 @@ static void gs_shutdown_port (struct gs_port *port)
                port->xmit_buf = NULL;
        }
 
-       if (port->tty)
-               set_bit(TTY_IO_ERROR, &port->tty->flags);
+       if (port->port.tty)
+               set_bit(TTY_IO_ERROR, &port->port.tty->flags);
 
        port->rd->shutdown_port (port);
 
-       port->flags &= ~ASYNC_INITIALIZED;
+       port->port.flags &= ~ASYNC_INITIALIZED;
        spin_unlock_irqrestore(&port->driver_lock, flags);
 
        func_exit();
@@ -396,16 +396,16 @@ void gs_hangup(struct tty_struct *tty)
        if (!tty) return;
 
        port = tty->driver_data;
-       tty = port->tty;
+       tty = port->port.tty;
        if (!tty) 
                return;
 
        gs_shutdown_port (port);
-       port->flags &= ~(ASYNC_NORMAL_ACTIVE|GS_ACTIVE);
-       port->tty = NULL;
-       port->count = 0;
+       port->port.flags &= ~(ASYNC_NORMAL_ACTIVE|GS_ACTIVE);
+       port->port.tty = NULL;
+       port->port.count = 0;
 
-       wake_up_interruptible(&port->open_wait);
+       wake_up_interruptible(&port->port.open_wait);
        func_exit ();
 }
 
@@ -424,7 +424,7 @@ int gs_block_til_ready(void *port_, struct file * filp)
 
        if (!port) return 0;
 
-       tty = port->tty;
+       tty = port->port.tty;
 
        if (!tty) return 0;
 
@@ -433,9 +433,9 @@ int gs_block_til_ready(void *port_, struct file * filp)
         * If the device is in the middle of being closed, then block
         * until it's done, and then try again.
         */
-       if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
-               interruptible_sleep_on(&port->close_wait);
-               if (port->flags & ASYNC_HUP_NOTIFY)
+       if (tty_hung_up_p(filp) || port->port.flags & ASYNC_CLOSING) {
+               interruptible_sleep_on(&port->port.close_wait);
+               if (port->port.flags & ASYNC_HUP_NOTIFY)
                        return -EAGAIN;
                else
                        return -ERESTARTSYS;
@@ -449,7 +449,7 @@ int gs_block_til_ready(void *port_, struct file * filp)
         */
        if ((filp->f_flags & O_NONBLOCK) ||
            (tty->flags & (1 << TTY_IO_ERROR))) {
-               port->flags |= ASYNC_NORMAL_ACTIVE;
+               port->port.flags |= ASYNC_NORMAL_ACTIVE;
                return 0;
        }
 
@@ -461,34 +461,34 @@ int gs_block_til_ready(void *port_, struct file * filp)
        /*
         * Block waiting for the carrier detect and the line to become
         * free (i.e., not in use by the callout).  While we are in
-        * this loop, port->count is dropped by one, so that
+        * this loop, port->port.count is dropped by one, so that
         * rs_close() knows when to free things.  We restore it upon
         * exit, either normal or abnormal.
         */
        retval = 0;
 
-       add_wait_queue(&port->open_wait, &wait);
+       add_wait_queue(&port->port.open_wait, &wait);
 
        gs_dprintk (GS_DEBUG_BTR, "after add waitq.\n"); 
        spin_lock_irqsave(&port->driver_lock, flags);
        if (!tty_hung_up_p(filp)) {
-               port->count--;
+               port->port.count--;
        }
        spin_unlock_irqrestore(&port->driver_lock, flags);
-       port->blocked_open++;
+       port->port.blocked_open++;
        while (1) {
                CD = port->rd->get_CD (port);
                gs_dprintk (GS_DEBUG_BTR, "CD is now %d.\n", CD);
                set_current_state (TASK_INTERRUPTIBLE);
                if (tty_hung_up_p(filp) ||
-                   !(port->flags & ASYNC_INITIALIZED)) {
-                       if (port->flags & ASYNC_HUP_NOTIFY)
+                   !(port->port.flags & ASYNC_INITIALIZED)) {
+                       if (port->port.flags & ASYNC_HUP_NOTIFY)
                                retval = -EAGAIN;
                        else
                                retval = -ERESTARTSYS;
                        break;
                }
-               if (!(port->flags & ASYNC_CLOSING) &&
+               if (!(port->port.flags & ASYNC_CLOSING) &&
                    (do_clocal || CD))
                        break;
                gs_dprintk (GS_DEBUG_BTR, "signal_pending is now: %d (%lx)\n", 
@@ -500,17 +500,17 @@ int gs_block_til_ready(void *port_, struct file * filp)
                schedule();
        }
        gs_dprintk (GS_DEBUG_BTR, "Got out of the loop. (%d)\n",
-                   port->blocked_open);
+                   port->port.blocked_open);
        set_current_state (TASK_RUNNING);
-       remove_wait_queue(&port->open_wait, &wait);
+       remove_wait_queue(&port->port.open_wait, &wait);
        if (!tty_hung_up_p(filp)) {
-               port->count++;
+               port->port.count++;
        }
-       port->blocked_open--;
+       port->port.blocked_open--;
        if (retval)
                return retval;
 
-       port->flags |= ASYNC_NORMAL_ACTIVE;
+       port->port.flags |= ASYNC_NORMAL_ACTIVE;
        func_exit ();
        return 0;
 }                       
@@ -529,10 +529,10 @@ void gs_close(struct tty_struct * tty, struct file * filp)
 
        if (!port) return;
 
-       if (!port->tty) {
+       if (!port->port.tty) {
                /* This seems to happen when this is called from vhangup. */
-               gs_dprintk (GS_DEBUG_CLOSE, "gs: Odd: port->tty is NULL\n");
-               port->tty = tty;
+               gs_dprintk (GS_DEBUG_CLOSE, "gs: Odd: port->port.tty is NULL\n");
+               port->port.tty = tty;
        }
 
        spin_lock_irqsave(&port->driver_lock, flags);
@@ -545,23 +545,23 @@ void gs_close(struct tty_struct * tty, struct file * filp)
                return;
        }
 
-       if ((tty->count == 1) && (port->count != 1)) {
+       if ((tty->count == 1) && (port->port.count != 1)) {
                printk(KERN_ERR "gs: gs_close port %p: bad port count;"
-                      " tty->count is 1, port count is %d\n", port, port->count);
-               port->count = 1;
+                      " tty->count is 1, port count is %d\n", port, port->port.count);
+               port->port.count = 1;
        }
-       if (--port->count < 0) {
-               printk(KERN_ERR "gs: gs_close port %p: bad port count: %d\n", port, port->count);
-               port->count = 0;
+       if (--port->port.count < 0) {
+               printk(KERN_ERR "gs: gs_close port %p: bad port count: %d\n", port, port->port.count);
+               port->port.count = 0;
        }
 
-       if (port->count) {
-               gs_dprintk(GS_DEBUG_CLOSE, "gs_close port %p: count: %d\n", port, port->count);
+       if (port->port.count) {
+               gs_dprintk(GS_DEBUG_CLOSE, "gs_close port %p: count: %d\n", port, port->port.count);
                spin_unlock_irqrestore(&port->driver_lock, flags);
                func_exit ();
                return;
        }
-       port->flags |= ASYNC_CLOSING;
+       port->port.flags |= ASYNC_CLOSING;
 
        /*
         * Now we wait for the transmit buffer to clear; and we notify 
@@ -585,10 +585,9 @@ void gs_close(struct tty_struct * tty, struct file * filp)
        if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
                gs_wait_tx_flushed (port, port->closing_wait);
 
-       port->flags &= ~GS_ACTIVE;
+       port->port.flags &= ~GS_ACTIVE;
 
-       if (tty->driver->flush_buffer)
-               tty->driver->flush_buffer(tty);
+       gs_flush_buffer(tty);
 
        tty_ldisc_flush(tty);
        tty->closing = 0;
@@ -596,18 +595,18 @@ void gs_close(struct tty_struct * tty, struct file * filp)
        port->event = 0;
        port->rd->close (port);
        port->rd->shutdown_port (port);
-       port->tty = NULL;
+       port->port.tty = NULL;
 
-       if (port->blocked_open) {
+       if (port->port.blocked_open) {
                if (port->close_delay) {
                        spin_unlock_irqrestore(&port->driver_lock, flags);
                        msleep_interruptible(jiffies_to_msecs(port->close_delay));
                        spin_lock_irqsave(&port->driver_lock, flags);
                }
-               wake_up_interruptible(&port->open_wait);
+               wake_up_interruptible(&port->port.open_wait);
        }
-       port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING | ASYNC_INITIALIZED);
-       wake_up_interruptible(&port->close_wait);
+       port->port.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING | ASYNC_INITIALIZED);
+       wake_up_interruptible(&port->port.close_wait);
 
        func_exit ();
 }
@@ -627,10 +626,10 @@ void gs_set_termios (struct tty_struct * tty,
        port = tty->driver_data;
 
        if (!port) return;
-       if (!port->tty) {
+       if (!port->port.tty) {
                /* This seems to happen when this is called after gs_close. */
-               gs_dprintk (GS_DEBUG_TERMIOS, "gs: Odd: port->tty is NULL\n");
-               port->tty = tty;
+               gs_dprintk (GS_DEBUG_TERMIOS, "gs: Odd: port->port.tty is NULL\n");
+               port->port.tty = tty;
        }
 
 
@@ -652,15 +651,15 @@ void gs_set_termios (struct tty_struct * tty,
        baudrate = tty_get_baud_rate(tty);
 
        if ((tiosp->c_cflag & CBAUD) == B38400) {
-               if (     (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
+               if (     (port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
                        baudrate = 57600;
-               else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
+               else if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
                        baudrate = 115200;
-               else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
+               else if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
                        baudrate = 230400;
-               else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
+               else if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
                        baudrate = 460800;
-               else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
+               else if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
                        baudrate = (port->baud_base / port->custom_divisor);
        }
 
@@ -716,7 +715,7 @@ int gs_init_port(struct gs_port *port)
 
        func_enter ();
 
-       if (port->flags & ASYNC_INITIALIZED) {
+       if (port->port.flags & ASYNC_INITIALIZED) {
                func_exit ();
                return 0;
        }
@@ -738,15 +737,15 @@ int gs_init_port(struct gs_port *port)
        }
 
        spin_lock_irqsave (&port->driver_lock, flags);
-       if (port->tty) 
-               clear_bit(TTY_IO_ERROR, &port->tty->flags);
+       if (port->port.tty)
+               clear_bit(TTY_IO_ERROR, &port->port.tty->flags);
        mutex_init(&port->port_write_mutex);
        port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
        spin_unlock_irqrestore(&port->driver_lock, flags);
-       gs_set_termios(port->tty, NULL);
+       gs_set_termios(port->port.tty, NULL);
        spin_lock_irqsave (&port->driver_lock, flags);
-       port->flags |= ASYNC_INITIALIZED;
-       port->flags &= ~GS_TX_INTEN;
+       port->port.flags |= ASYNC_INITIALIZED;
+       port->port.flags &= ~GS_TX_INTEN;
 
        spin_unlock_irqrestore(&port->driver_lock, flags);
        func_exit ();
@@ -765,11 +764,11 @@ int gs_setserial(struct gs_port *port, struct serial_struct __user *sp)
                if ((sio.baud_base != port->baud_base) ||
                    (sio.close_delay != port->close_delay) ||
                    ((sio.flags & ~ASYNC_USR_MASK) !=
-                    (port->flags & ~ASYNC_USR_MASK)))
+                    (port->port.flags & ~ASYNC_USR_MASK)))
                        return(-EPERM);
        } 
 
-       port->flags = (port->flags & ~ASYNC_USR_MASK) |
+       port->port.flags = (port->port.flags & ~ASYNC_USR_MASK) |
                (sio.flags & ASYNC_USR_MASK);
   
        port->baud_base = sio.baud_base;
@@ -777,7 +776,7 @@ int gs_setserial(struct gs_port *port, struct serial_struct __user *sp)
        port->closing_wait = sio.closing_wait;
        port->custom_divisor = sio.custom_divisor;
 
-       gs_set_termios (port->tty, NULL);
+       gs_set_termios (port->port.tty, NULL);
 
        return 0;
 }
@@ -794,7 +793,7 @@ int gs_getserial(struct gs_port *port, struct serial_struct __user *sp)
        struct serial_struct    sio;
 
        memset(&sio, 0, sizeof(struct serial_struct));
-       sio.flags = port->flags;
+       sio.flags = port->port.flags;
        sio.baud_base = port->baud_base;
        sio.close_delay = port->close_delay;
        sio.closing_wait = port->closing_wait;
@@ -822,10 +821,10 @@ void gs_got_break(struct gs_port *port)
 {
        func_enter ();
 
-       tty_insert_flip_char(port->tty, 0, TTY_BREAK);
-       tty_schedule_flip(port->tty);
-       if (port->flags & ASYNC_SAK) {
-               do_SAK (port->tty);
+       tty_insert_flip_char(port->port.tty, 0, TTY_BREAK);
+       tty_schedule_flip(port->port.tty);
+       if (port->port.flags & ASYNC_SAK) {
+               do_SAK (port->port.tty);
        }
 
        func_exit ();