fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / net / ppp_synctty.c
index 0e1e057..b6f0e9a 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Complete PPP frames without encoding/decoding are exchanged between
  * the channel driver and the device driver.
- * 
+ *
  * The async map IOCTL codes are implemented to keep the user mode
  * applications happy if they call them. Synchronous PPP does not use
  * the async maps.
@@ -29,7 +29,7 @@
  * PPP driver, written by Michael Callahan and Al Longyear, and
  * subsequently hacked by Paul Mackerras.
  *
- * ==FILEVERSION 20020125==
+ * ==FILEVERSION 20040616==
  */
 
 #include <linux/module.h>
@@ -65,7 +65,9 @@ struct syncppp {
        struct sk_buff  *tpkt;
        unsigned long   last_xmit;
 
-       struct sk_buff  *rpkt;
+       struct sk_buff_head rqueue;
+
+       struct tasklet_struct tsk;
 
        atomic_t        refcnt;
        struct semaphore dead_sem;
@@ -88,6 +90,7 @@ static struct sk_buff* ppp_sync_txmunge(struct syncppp *ap, struct sk_buff *);
 static int ppp_sync_send(struct ppp_channel *chan, struct sk_buff *skb);
 static int ppp_sync_ioctl(struct ppp_channel *chan, unsigned int cmd,
                          unsigned long arg);
+static void ppp_sync_process(unsigned long arg);
 static int ppp_sync_push(struct syncppp *ap);
 static void ppp_sync_flush_output(struct syncppp *ap);
 static void ppp_sync_input(struct syncppp *ap, const unsigned char *buf,
@@ -105,7 +108,7 @@ static void
 ppp_print_hex (register __u8 * out, const __u8 * in, int count)
 {
        register __u8 next_ch;
-       static char hex[] = "0123456789ABCDEF";
+       static const char hex[] = "0123456789ABCDEF";
 
        while (count-- > 0) {
                next_ch = *in++;
@@ -172,8 +175,10 @@ ppp_print_buffer (const char *name, const __u8 *buf, int count)
  * frees the memory that ppp_synctty_receive is using.  The best
  * way to fix this is to use a rwlock in the tty struct, but for now
  * we use a single global rwlock for all ttys in ppp line discipline.
+ *
+ * FIXME: Fixed in tty_io nowdays.
  */
-static rwlock_t disc_data_lock = RW_LOCK_UNLOCKED;
+static DEFINE_RWLOCK(disc_data_lock);
 
 static struct syncppp *sp_get(struct tty_struct *tty)
 {
@@ -217,6 +222,9 @@ ppp_sync_open(struct tty_struct *tty)
        ap->xaccm[3] = 0x60000000U;
        ap->raccm = ~0U;
 
+       skb_queue_head_init(&ap->rqueue);
+       tasklet_init(&ap->tsk, ppp_sync_process, (unsigned long) ap);
+
        atomic_set(&ap->refcnt, 1);
        init_MUTEX_LOCKED(&ap->dead_sem);
 
@@ -229,7 +237,7 @@ ppp_sync_open(struct tty_struct *tty)
                goto out_free;
 
        tty->disc_data = ap;
-
+       tty->receive_room = 65536;
        return 0;
 
  out_free:
@@ -253,7 +261,7 @@ ppp_sync_close(struct tty_struct *tty)
 
        write_lock_irq(&disc_data_lock);
        ap = tty->disc_data;
-       tty->disc_data = 0;
+       tty->disc_data = NULL;
        write_unlock_irq(&disc_data_lock);
        if (ap == 0)
                return;
@@ -267,22 +275,34 @@ ppp_sync_close(struct tty_struct *tty)
         */
        if (!atomic_dec_and_test(&ap->refcnt))
                down(&ap->dead_sem);
+       tasklet_kill(&ap->tsk);
 
        ppp_unregister_channel(&ap->chan);
-       if (ap->rpkt != 0)
-               kfree_skb(ap->rpkt);
+       skb_queue_purge(&ap->rqueue);
        if (ap->tpkt != 0)
                kfree_skb(ap->tpkt);
        kfree(ap);
 }
 
+/*
+ * Called on tty hangup in process context.
+ *
+ * Wait for I/O to driver to complete and unregister PPP channel.
+ * This is already done by the close routine, so just call that.
+ */
+static int ppp_sync_hangup(struct tty_struct *tty)
+{
+       ppp_sync_close(tty);
+       return 0;
+}
+
 /*
  * Read does nothing - no data is ever available this way.
  * Pppd reads and writes packets via /dev/ppp instead.
  */
 static ssize_t
 ppp_sync_read(struct tty_struct *tty, struct file *file,
-              unsigned char *buf, size_t count)
+              unsigned char __user *buf, size_t count)
 {
        return -EAGAIN;
 }
@@ -303,6 +323,7 @@ ppp_synctty_ioctl(struct tty_struct *tty, struct file *file,
                  unsigned int cmd, unsigned long arg)
 {
        struct syncppp *ap = sp_get(tty);
+       int __user *p = (int __user *)arg;
        int err, val;
 
        if (ap == 0)
@@ -314,7 +335,7 @@ ppp_synctty_ioctl(struct tty_struct *tty, struct file *file,
                if (ap == 0)
                        break;
                err = -EFAULT;
-               if (put_user(ppp_channel_index(&ap->chan), (int *) arg))
+               if (put_user(ppp_channel_index(&ap->chan), p))
                        break;
                err = 0;
                break;
@@ -324,7 +345,7 @@ ppp_synctty_ioctl(struct tty_struct *tty, struct file *file,
                if (ap == 0)
                        break;
                err = -EFAULT;
-               if (put_user(ppp_unit_number(&ap->chan), (int *) arg))
+               if (put_user(ppp_unit_number(&ap->chan), p))
                        break;
                err = 0;
                break;
@@ -343,7 +364,7 @@ ppp_synctty_ioctl(struct tty_struct *tty, struct file *file,
 
        case FIONREAD:
                val = 0;
-               if (put_user(val, (int *) arg))
+               if (put_user(val, p))
                        break;
                err = 0;
                break;
@@ -363,23 +384,24 @@ ppp_sync_poll(struct tty_struct *tty, struct file *file, poll_table *wait)
        return 0;
 }
 
-static int
-ppp_sync_room(struct tty_struct *tty)
-{
-       return 65535;
-}
-
+/*
+ * This can now be called from hard interrupt level as well
+ * as soft interrupt level or mainline.
+ */
 static void
 ppp_sync_receive(struct tty_struct *tty, const unsigned char *buf,
-                 char *flags, int count)
+                 char *cflags, int count)
 {
        struct syncppp *ap = sp_get(tty);
+       unsigned long flags;
 
        if (ap == 0)
                return;
-       spin_lock_bh(&ap->recv_lock);
-       ppp_sync_input(ap, buf, flags, count);
-       spin_unlock_bh(&ap->recv_lock);
+       spin_lock_irqsave(&ap->recv_lock, flags);
+       ppp_sync_input(ap, buf, cflags, count);
+       spin_unlock_irqrestore(&ap->recv_lock, flags);
+       if (!skb_queue_empty(&ap->rqueue))
+               tasklet_schedule(&ap->tsk);
        sp_put(ap);
        if (test_and_clear_bit(TTY_THROTTLED, &tty->flags)
            && tty->driver->unthrottle)
@@ -394,8 +416,8 @@ ppp_sync_wakeup(struct tty_struct *tty)
        clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
        if (ap == 0)
                return;
-       if (ppp_sync_push(ap))
-               ppp_output_wakeup(&ap->chan);
+       set_bit(XMIT_WAKEUP, &ap->xmit_flags);
+       tasklet_schedule(&ap->tsk);
        sp_put(ap);
 }
 
@@ -406,11 +428,11 @@ static struct tty_ldisc ppp_sync_ldisc = {
        .name   = "pppsync",
        .open   = ppp_sync_open,
        .close  = ppp_sync_close,
+       .hangup = ppp_sync_hangup,
        .read   = ppp_sync_read,
        .write  = ppp_sync_write,
        .ioctl  = ppp_synctty_ioctl,
        .poll   = ppp_sync_poll,
-       .receive_room = ppp_sync_room,
        .receive_buf = ppp_sync_receive,
        .write_wakeup = ppp_sync_wakeup,
 };
@@ -436,54 +458,56 @@ ppp_sync_ioctl(struct ppp_channel *chan, unsigned int cmd, unsigned long arg)
        struct syncppp *ap = chan->private;
        int err, val;
        u32 accm[8];
+       void __user *argp = (void __user *)arg;
+       u32 __user *p = argp;
 
        err = -EFAULT;
        switch (cmd) {
        case PPPIOCGFLAGS:
                val = ap->flags | ap->rbits;
-               if (put_user(val, (int *) arg))
+               if (put_user(val, (int __user *) argp))
                        break;
                err = 0;
                break;
        case PPPIOCSFLAGS:
-               if (get_user(val, (int *) arg))
+               if (get_user(val, (int __user *) argp))
                        break;
                ap->flags = val & ~SC_RCV_BITS;
-               spin_lock_bh(&ap->recv_lock);
+               spin_lock_irq(&ap->recv_lock);
                ap->rbits = val & SC_RCV_BITS;
-               spin_unlock_bh(&ap->recv_lock);
+               spin_unlock_irq(&ap->recv_lock);
                err = 0;
                break;
 
        case PPPIOCGASYNCMAP:
-               if (put_user(ap->xaccm[0], (u32 *) arg))
+               if (put_user(ap->xaccm[0], p))
                        break;
                err = 0;
                break;
        case PPPIOCSASYNCMAP:
-               if (get_user(ap->xaccm[0], (u32 *) arg))
+               if (get_user(ap->xaccm[0], p))
                        break;
                err = 0;
                break;
 
        case PPPIOCGRASYNCMAP:
-               if (put_user(ap->raccm, (u32 *) arg))
+               if (put_user(ap->raccm, p))
                        break;
                err = 0;
                break;
        case PPPIOCSRASYNCMAP:
-               if (get_user(ap->raccm, (u32 *) arg))
+               if (get_user(ap->raccm, p))
                        break;
                err = 0;
                break;
 
        case PPPIOCGXASYNCMAP:
-               if (copy_to_user((void *) arg, ap->xaccm, sizeof(ap->xaccm)))
+               if (copy_to_user(argp, ap->xaccm, sizeof(ap->xaccm)))
                        break;
                err = 0;
                break;
        case PPPIOCSXASYNCMAP:
-               if (copy_from_user(accm, (void *) arg, sizeof(accm)))
+               if (copy_from_user(accm, argp, sizeof(accm)))
                        break;
                accm[2] &= ~0x40000000U;        /* can't escape 0x5e */
                accm[3] |= 0x60000000U;         /* must escape 0x7d, 0x7e */
@@ -492,12 +516,12 @@ ppp_sync_ioctl(struct ppp_channel *chan, unsigned int cmd, unsigned long arg)
                break;
 
        case PPPIOCGMRU:
-               if (put_user(ap->mru, (int *) arg))
+               if (put_user(ap->mru, (int __user *) argp))
                        break;
                err = 0;
                break;
        case PPPIOCSMRU:
-               if (get_user(val, (int *) arg))
+               if (get_user(val, (int __user *) argp))
                        break;
                if (val < PPP_MRU)
                        val = PPP_MRU;
@@ -511,6 +535,32 @@ ppp_sync_ioctl(struct ppp_channel *chan, unsigned int cmd, unsigned long arg)
        return err;
 }
 
+/*
+ * This is called at softirq level to deliver received packets
+ * to the ppp_generic code, and to tell the ppp_generic code
+ * if we can accept more output now.
+ */
+static void ppp_sync_process(unsigned long arg)
+{
+       struct syncppp *ap = (struct syncppp *) arg;
+       struct sk_buff *skb;
+
+       /* process received packets */
+       while ((skb = skb_dequeue(&ap->rqueue)) != NULL) {
+               if (skb->len == 0) {
+                       /* zero length buffers indicate error */
+                       ppp_input_error(&ap->chan, 0);
+                       kfree_skb(skb);
+               }
+               else
+                       ppp_input(&ap->chan, skb);
+       }
+
+       /* try to push more stuff out */
+       if (test_bit(XMIT_WAKEUP, &ap->xmit_flags) && ppp_sync_push(ap))
+               ppp_output_wakeup(&ap->chan);
+}
+
 /*
  * Procedures for encapsulation and framing.
  */
@@ -600,7 +650,6 @@ ppp_sync_push(struct syncppp *ap)
        struct tty_struct *tty = ap->tty;
        int tty_stuffed = 0;
 
-       set_bit(XMIT_WAKEUP, &ap->xmit_flags);
        if (!spin_trylock_bh(&ap->xmit_lock))
                return 0;
        for (;;) {
@@ -608,14 +657,14 @@ ppp_sync_push(struct syncppp *ap)
                        tty_stuffed = 0;
                if (!tty_stuffed && ap->tpkt != 0) {
                        set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
-                       sent = tty->driver->write(tty, 0, ap->tpkt->data, ap->tpkt->len);
+                       sent = tty->driver->write(tty, ap->tpkt->data, ap->tpkt->len);
                        if (sent < 0)
                                goto flush;     /* error, e.g. loss of CD */
                        if (sent < ap->tpkt->len) {
                                tty_stuffed = 1;
                        } else {
                                kfree_skb(ap->tpkt);
-                               ap->tpkt = 0;
+                               ap->tpkt = NULL;
                                clear_bit(XMIT_FULL, &ap->xmit_flags);
                                done = 1;
                        }
@@ -634,7 +683,7 @@ ppp_sync_push(struct syncppp *ap)
 flush:
        if (ap->tpkt != 0) {
                kfree_skb(ap->tpkt);
-               ap->tpkt = 0;
+               ap->tpkt = NULL;
                clear_bit(XMIT_FULL, &ap->xmit_flags);
                done = 1;
        }
@@ -654,7 +703,7 @@ ppp_sync_flush_output(struct syncppp *ap)
        spin_lock_bh(&ap->xmit_lock);
        if (ap->tpkt != NULL) {
                kfree_skb(ap->tpkt);
-               ap->tpkt = 0;
+               ap->tpkt = NULL;
                clear_bit(XMIT_FULL, &ap->xmit_flags);
                done = 1;
        }
@@ -667,15 +716,44 @@ ppp_sync_flush_output(struct syncppp *ap)
  * Receive-side routines.
  */
 
-static inline void
-process_input_packet(struct syncppp *ap)
+/* called when the tty driver has data for us.
+ *
+ * Data is frame oriented: each call to ppp_sync_input is considered
+ * a whole frame. If the 1st flag byte is non-zero then the whole
+ * frame is considered to be in error and is tossed.
+ */
+static void
+ppp_sync_input(struct syncppp *ap, const unsigned char *buf,
+               char *flags, int count)
 {
        struct sk_buff *skb;
        unsigned char *p;
-       int code = 0;
 
-       skb = ap->rpkt;
-       ap->rpkt = 0;
+       if (count == 0)
+               return;
+
+       if (ap->flags & SC_LOG_INPKT)
+               ppp_print_buffer ("receive buffer", buf, count);
+
+       /* stuff the chars in the skb */
+       if ((skb = dev_alloc_skb(ap->mru + PPP_HDRLEN + 2)) == 0) {
+               printk(KERN_ERR "PPPsync: no memory (input pkt)\n");
+               goto err;
+       }
+       /* Try to get the payload 4-byte aligned */
+       if (buf[0] != PPP_ALLSTATIONS)
+               skb_reserve(skb, 2 + (buf[0] & 1));
+
+       if (flags != 0 && *flags) {
+               /* error flag set, ignore frame */
+               goto err;
+       } else if (count > skb_tailroom(skb)) {
+               /* packet overflowed MRU */
+               goto err;
+       }
+
+       p = skb_put(skb, count);
+       memcpy(p, buf, count);
 
        /* strip address/control field if present */
        p = skb->data;
@@ -693,66 +771,22 @@ process_input_packet(struct syncppp *ap)
        } else if (skb->len < 2)
                goto err;
 
-       /* pass to generic layer */
-       ppp_input(&ap->chan, skb);
+       /* queue the frame to be processed */
+       skb_queue_tail(&ap->rqueue, skb);
        return;
 
- err:
-       kfree_skb(skb);
-       ppp_input_error(&ap->chan, code);
-}
-
-/* called when the tty driver has data for us. 
- *
- * Data is frame oriented: each call to ppp_sync_input is considered
- * a whole frame. If the 1st flag byte is non-zero then the whole
- * frame is considered to be in error and is tossed.
- */
-static void
-ppp_sync_input(struct syncppp *ap, const unsigned char *buf,
-               char *flags, int count)
-{
-       struct sk_buff *skb;
-       unsigned char *sp;
-
-       if (count == 0)
-               return;
-
-       /* if flag set, then error, ignore frame */
-       if (flags != 0 && *flags) {
-               ppp_input_error(&ap->chan, *flags);
-               return;
-       }
-
-       if (ap->flags & SC_LOG_INPKT)
-               ppp_print_buffer ("receive buffer", buf, count);
-
-       /* stuff the chars in the skb */
-       if ((skb = ap->rpkt) == 0) {
-               if ((skb = dev_alloc_skb(ap->mru + PPP_HDRLEN + 2)) == 0) {
-                       printk(KERN_ERR "PPPsync: no memory (input pkt)\n");
-                       ppp_input_error(&ap->chan, 0);
-                       return;
-               }
-               /* Try to get the payload 4-byte aligned */
-               if (buf[0] != PPP_ALLSTATIONS)
-                       skb_reserve(skb, 2 + (buf[0] & 1));
-               ap->rpkt = skb;
-       }
-       if (count > skb_tailroom(skb)) {
-               /* packet overflowed MRU */
-               ppp_input_error(&ap->chan, 1);
-       } else {
-               sp = skb_put(skb, count);
-               memcpy(sp, buf, count);
-               process_input_packet(ap);
+err:
+       /* queue zero length packet as error indication */
+       if (skb || (skb = dev_alloc_skb(0))) {
+               skb_trim(skb, 0);
+               skb_queue_tail(&ap->rqueue, skb);
        }
 }
 
 static void __exit
 ppp_sync_cleanup(void)
 {
-       if (tty_register_ldisc(N_SYNC_PPP, NULL) != 0)
+       if (tty_unregister_ldisc(N_SYNC_PPP) != 0)
                printk(KERN_ERR "failed to unregister Sync PPP line discipline\n");
 }