rtc: don't use __exit_p to wrap ds1302_rtc_remove
[safe/jmp/linux-2.6] / drivers / char / n_hdlc.c
index c3660d8..c68118e 100644 (file)
@@ -10,7 +10,6 @@
  *     Paul Mackerras <Paul.Mackerras@cs.anu.edu.au>
  *
  * Original release 01/11/99
- * $Id: n_hdlc.c,v 4.8 2003/05/06 21:18:51 paulkf Exp $
  *
  * This code is released under the GNU General Public License (GPL)
  *
@@ -79,9 +78,7 @@
  */
 
 #define HDLC_MAGIC 0x239e
-#define HDLC_VERSION "$Revision: 4.8 $"
 
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/tty.h>
 #include <linux/errno.h>
+#include <linux/smp_lock.h>
 #include <linux/string.h>      /* used in new tty drivers */
 #include <linux/signal.h>      /* used in new tty drivers */
 #include <linux/if.h>
 #define MAX_HDLC_FRAME_SIZE 65535 
 #define DEFAULT_RX_BUF_COUNT 10
 #define MAX_RX_BUF_COUNT 60
-#define DEFAULT_TX_BUF_COUNT 1
+#define DEFAULT_TX_BUF_COUNT 3
 
 struct n_hdlc_buf {
        struct n_hdlc_buf *link;
@@ -191,7 +189,6 @@ static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
                                    poll_table *wait);
 static int n_hdlc_tty_open(struct tty_struct *tty);
 static void n_hdlc_tty_close(struct tty_struct *tty);
-static int n_hdlc_tty_room(struct tty_struct *tty);
 static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *cp,
                               char *fp, int count);
 static void n_hdlc_tty_wakeup(struct tty_struct *tty);
@@ -201,7 +198,32 @@ static void n_hdlc_tty_wakeup(struct tty_struct *tty);
 #define tty2n_hdlc(tty)        ((struct n_hdlc *) ((tty)->disc_data))
 #define n_hdlc2tty(n_hdlc)     ((n_hdlc)->tty)
 
-static struct tty_ldisc n_hdlc_ldisc = {
+static void flush_rx_queue(struct tty_struct *tty)
+{
+       struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
+       struct n_hdlc_buf *buf;
+
+       while ((buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list)))
+               n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, buf);
+}
+
+static void flush_tx_queue(struct tty_struct *tty)
+{
+       struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
+       struct n_hdlc_buf *buf;
+       unsigned long flags;
+
+       while ((buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list)))
+               n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, buf);
+       spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
+       if (n_hdlc->tbuf) {
+               n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, n_hdlc->tbuf);
+               n_hdlc->tbuf = NULL;
+       }
+       spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
+}
+
+static struct tty_ldisc_ops n_hdlc_ldisc = {
        .owner          = THIS_MODULE,
        .magic          = TTY_LDISC_MAGIC,
        .name           = "hdlc",
@@ -212,8 +234,8 @@ static struct tty_ldisc n_hdlc_ldisc = {
        .ioctl          = n_hdlc_tty_ioctl,
        .poll           = n_hdlc_tty_poll,
        .receive_buf    = n_hdlc_tty_receive,
-       .receive_room   = n_hdlc_tty_room,
        .write_wakeup   = n_hdlc_tty_wakeup,
+       .flush_buffer   = flush_rx_queue,
 };
 
 /**
@@ -232,7 +254,7 @@ static void n_hdlc_release(struct n_hdlc *n_hdlc)
        wake_up_interruptible (&tty->read_wait);
        wake_up_interruptible (&tty->write_wait);
 
-       if (tty != NULL && tty->disc_data == n_hdlc)
+       if (tty->disc_data == n_hdlc)
                tty->disc_data = NULL;  /* Break the tty->n_hdlc link */
 
        /* Release transmit and receive buffers */
@@ -337,19 +359,15 @@ static int n_hdlc_tty_open (struct tty_struct *tty)
                
        tty->disc_data = n_hdlc;
        n_hdlc->tty    = tty;
+       tty->receive_room = 65536;
        
 #if defined(TTY_NO_WRITE_SPLIT)
        /* change tty_io write() to not split large writes into 8K chunks */
        set_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
 #endif
        
-       /* Flush any pending characters in the driver and discipline. */
-       
-       if (tty->ldisc.flush_buffer)
-               tty->ldisc.flush_buffer (tty);
-
-       if (tty->driver->flush_buffer)
-               tty->driver->flush_buffer (tty);
+       /* flush receive data from driver */
+       tty_driver_flush_buffer(tty);
                
        if (debuglevel >= DEBUG_LEVEL_INFO)     
                printk("%s(%d)n_hdlc_tty_open() success\n",__FILE__,__LINE__);
@@ -401,8 +419,13 @@ static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty)
                        
                /* Send the next block of data to device */
                tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
-               actual = tty->driver->write(tty, tbuf->buf, tbuf->count);
-                   
+               actual = tty->ops->write(tty, tbuf->buf, tbuf->count);
+
+               /* rollback was possible and has been done */
+               if (actual == -ERESTARTSYS) {
+                       n_hdlc->tbuf = tbuf;
+                       break;
+               }
                /* if transmit error, throw frame away by */
                /* pretending it was accepted by driver */
                if (actual < 0)
@@ -478,22 +501,6 @@ static void n_hdlc_tty_wakeup(struct tty_struct *tty)
 }      /* end of n_hdlc_tty_wakeup() */
 
 /**
- * n_hdlc_tty_room - Return the amount of space left in the receiver's buffer
- * @tty        - pointer to associated tty instance data
- *
- * Callback function from tty driver. Return the amount of space left in the
- * receiver's buffer to decide if remote transmitter is to be throttled.
- */
-static int n_hdlc_tty_room(struct tty_struct *tty)
-{
-       if (debuglevel >= DEBUG_LEVEL_INFO)     
-               printk("%s(%d)n_hdlc_tty_room() called\n",__FILE__,__LINE__);
-       /* always return a larger number to prevent */
-       /* throttling of remote transmitter. */
-       return 65536;
-}      /* end of n_hdlc_tty_root() */
-
-/**
  * n_hdlc_tty_receive - Called by tty driver when receive data is available
  * @tty        - pointer to tty instance data
  * @data - pointer to received data
@@ -514,7 +521,7 @@ static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data,
                        __FILE__,__LINE__, count);
                
        /* This can happen if stuff comes in on the backup tty */
-       if (n_hdlc == 0 || tty != n_hdlc->tty)
+       if (!n_hdlc || tty != n_hdlc->tty)
                return;
                
        /* verify line is using HDLC discipline */
@@ -562,7 +569,7 @@ static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data,
 }      /* end of n_hdlc_tty_receive() */
 
 /**
- * n_hdlc_tty_read - Called to retreive one frame of data (if available)
+ * n_hdlc_tty_read - Called to retrieve one frame of data (if available)
  * @tty - pointer to tty instance data
  * @file - pointer to open file object
  * @buf - pointer to returned data buffer
@@ -591,26 +598,36 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
                return -EFAULT;
        }
 
+       lock_kernel();
+
        for (;;) {
-               if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
+               if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
+                       unlock_kernel();
                        return -EIO;
+               }
 
                n_hdlc = tty2n_hdlc (tty);
                if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
-                        tty != n_hdlc->tty)
+                        tty != n_hdlc->tty) {
+                       unlock_kernel();
                        return 0;
+               }
 
                rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
                if (rbuf)
                        break;
                        
                /* no data */
-               if (file->f_flags & O_NONBLOCK)
+               if (file->f_flags & O_NONBLOCK) {
+                       unlock_kernel();
                        return -EAGAIN;
+               }
                        
                interruptible_sleep_on (&tty->read_wait);
-               if (signal_pending(current))
+               if (signal_pending(current)) {
+                       unlock_kernel();
                        return -EINTR;
+               }
        }
                
        if (rbuf->count > nr)
@@ -631,7 +648,7 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
                kfree(rbuf);
        else    
                n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,rbuf);
-       
+       unlock_kernel();
        return ret;
        
 }      /* end of n_hdlc_tty_read() */
@@ -674,12 +691,18 @@ static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
                count = maxframe;
        }
        
+       lock_kernel();
+
        add_wait_queue(&tty->write_wait, &wait);
        set_current_state(TASK_INTERRUPTIBLE);
        
        /* Allocate transmit buffer */
        /* sleep until transmit buffer available */             
        while (!(tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list))) {
+               if (file->f_flags & O_NONBLOCK) {
+                       error = -EAGAIN;
+                       break;
+               }
                schedule();
                        
                n_hdlc = tty2n_hdlc (tty);
@@ -708,7 +731,7 @@ static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
                n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf);
                n_hdlc_send_frames(n_hdlc,tty);
        }
-
+       unlock_kernel();
        return error;
        
 }      /* end of n_hdlc_tty_write() */
@@ -753,8 +776,7 @@ static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
 
        case TIOCOUTQ:
                /* get the pending tx byte count in the driver */
-               count = tty->driver->chars_in_buffer ?
-                               tty->driver->chars_in_buffer(tty) : 0;
+               count = tty_chars_in_buffer(tty);
                /* add size of next output frame in queue */
                spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock,flags);
                if (n_hdlc->tx_buf_list.head)
@@ -763,8 +785,16 @@ static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
                error = put_user(count, (int __user *)arg);
                break;
 
+       case TCFLSH:
+               switch (arg) {
+               case TCIOFLUSH:
+               case TCOFLUSH:
+                       flush_tx_queue(tty);
+               }
+               /* fall through to default */
+
        default:
-               error = n_tty_ioctl (tty, file, cmd, arg);
+               error = n_tty_ioctl_helper(tty, file, cmd, arg);
                break;
        }
        return error;
@@ -798,13 +828,14 @@ static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
                poll_wait(filp, &tty->write_wait, wait);
 
                /* set bits for operations that won't block */
-               if(n_hdlc->rx_buf_list.head)
+               if (n_hdlc->rx_buf_list.head)
                        mask |= POLLIN | POLLRDNORM;    /* readable */
                if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
                        mask |= POLLHUP;
-               if(tty_hung_up_p(filp))
+               if (tty_hung_up_p(filp))
                        mask |= POLLHUP;
-               if(n_hdlc->tx_free_buf_list.head)
+               if (!tty_is_writelocked(tty) &&
+                               n_hdlc->tx_free_buf_list.head)
                        mask |= POLLOUT | POLLWRNORM;   /* writable */
        }
        return mask;
@@ -879,7 +910,7 @@ static void n_hdlc_buf_put(struct n_hdlc_buf_list *list,
        spin_lock_irqsave(&list->spinlock,flags);
        
        buf->link=NULL;
-       if(list->tail)
+       if (list->tail)
                list->tail->link = buf;
        else
                list->head = buf;
@@ -918,8 +949,7 @@ static struct n_hdlc_buf* n_hdlc_buf_get(struct n_hdlc_buf_list *list)
 }      /* end of n_hdlc_buf_get() */
 
 static char hdlc_banner[] __initdata =
-       KERN_INFO "HDLC line discipline: version " HDLC_VERSION
-       ", maxframe=%u\n";
+       KERN_INFO "HDLC line discipline maxframe=%u\n";
 static char hdlc_register_ok[] __initdata =
        KERN_INFO "N_HDLC line discipline registered.\n";
 static char hdlc_register_fail[] __initdata =