CAPI: Convert capidev_list_lock into a mutex
[safe/jmp/linux-2.6] / drivers / isdn / capi / capi.c
index d22c022..623412e 100644 (file)
 #include <linux/fcntl.h>
 #include <linux/fs.h>
 #include <linux/signal.h>
+#include <linux/mutex.h>
 #include <linux/mm.h>
 #include <linux/smp_lock.h>
 #include <linux/timer.h>
 #include <linux/wait.h>
-#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
 #include <linux/tty.h>
-#ifdef CONFIG_PPP
 #include <linux/netdevice.h>
 #include <linux/ppp_defs.h>
 #include <linux/if_ppp.h>
-#endif /* CONFIG_PPP */
-#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
 #include <linux/skbuff.h>
 #include <linux/proc_fs.h>
+#include <linux/seq_file.h>
 #include <linux/poll.h>
 #include <linux/capi.h>
 #include <linux/kernelcapi.h>
 #include <linux/moduleparam.h>
 #include <linux/isdn/capiutil.h>
 #include <linux/isdn/capicmd.h>
-#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
-#include "capifs.h"
-#endif
 
-static char *revision = "$Revision: 1.1.2.7 $";
+#include "capifs.h"
 
 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
 MODULE_AUTHOR("Carsten Paeth");
@@ -57,17 +52,17 @@ MODULE_LICENSE("GPL");
 /* -------- driver information -------------------------------------- */
 
 static struct class *capi_class;
-
 static int capi_major = 68;            /* allocated */
+
+module_param_named(major, capi_major, uint, 0);
+
 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
-#define CAPINC_NR_PORTS        32
+#define CAPINC_NR_PORTS                32
 #define CAPINC_MAX_PORTS       256
+
 static int capi_ttymajor = 191;
 static int capi_ttyminors = CAPINC_NR_PORTS;
-#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
 
-module_param_named(major, capi_major, uint, 0);
-#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
 module_param_named(ttymajor, capi_ttymajor, uint, 0);
 module_param_named(ttyminors, capi_ttyminors, uint, 0);
 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
@@ -82,7 +77,6 @@ module_param_named(ttyminors, capi_ttyminors, uint, 0);
 
 struct capidev;
 struct capincci;
-#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
 struct capiminor;
 
 struct datahandle_queue {
@@ -94,6 +88,7 @@ struct capiminor {
        struct list_head list;
        struct capincci  *nccip;
        unsigned int      minor;
+       struct dentry *capifs_dentry;
 
        struct capi20_appl *ap;
        u32              ncci;
@@ -116,7 +111,15 @@ struct capiminor {
        int nack;
        spinlock_t ackqlock;
 };
-#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
+
+/* FIXME: The following lock is a sledgehammer-workaround to a
+ * locking issue with the capiminor (and maybe other) data structure(s).
+ * Access to this data is done in a racy way and crashes the machine with
+ * a FritzCard DSL driver; sooner or later. This is a workaround
+ * which trades scalability vs stability, so it doesn't crash the kernel anymore.
+ * The correct (and scalable) fix for the issue seems to require
+ * an API change to the drivers... . */
+static DEFINE_SPINLOCK(workaround_lock);
 
 struct capincci {
        struct capincci *next;
@@ -138,23 +141,22 @@ struct capidev {
 
        struct capincci *nccis;
 
-       struct semaphore ncci_list_sem;
+       struct mutex ncci_list_mtx;
 };
 
 /* -------- global variables ---------------------------------------- */
 
-static DEFINE_RWLOCK(capidev_list_lock);
+static DEFINE_MUTEX(capidev_list_lock);
 static LIST_HEAD(capidev_list);
 
 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
+
 static DEFINE_RWLOCK(capiminor_list_lock);
 static LIST_HEAD(capiminor_list);
-#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
 
-#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
 /* -------- datahandles --------------------------------------------- */
 
-static int capincci_add_ack(struct capiminor *mp, u16 datahandle)
+static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
 {
        struct datahandle_queue *n;
        unsigned long flags;
@@ -267,7 +269,7 @@ static void capiminor_free(struct capiminor *mp)
        list_del(&mp->list);
        write_unlock_irqrestore(&capiminor_list_lock, flags);
 
-       if (mp->ttyskb) kfree_skb(mp->ttyskb);
+       kfree_skb(mp->ttyskb);
        mp->ttyskb = NULL;
        skb_queue_purge(&mp->inqueue);
        skb_queue_purge(&mp->outqueue);
@@ -292,36 +294,78 @@ static struct capiminor *capiminor_find(unsigned int minor)
 
        return p;
 }
-#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
 
 /* -------- struct capincci ----------------------------------------- */
 
-static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
+static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
 {
-       struct capincci *np, **pp;
-#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
-       struct capiminor *mp = NULL;
-#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
+       struct capiminor *mp;
 
-       np = kzalloc(sizeof(*np), GFP_ATOMIC);
-       if (!np)
-               return NULL;
-       np->ncci = ncci;
-       np->cdev = cdev;
-#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
-       mp = NULL;
-       if (cdev->userflags & CAPIFLAG_HIGHJACKING)
-               mp = np->minorp = capiminor_alloc(&cdev->ap, ncci);
+       if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
+               return;
+
+       mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
        if (mp) {
                mp->nccip = np;
 #ifdef _DEBUG_REFCOUNT
                printk(KERN_DEBUG "set mp->nccip\n");
 #endif
-#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
-               capifs_new_ncci(mp->minor, MKDEV(capi_ttymajor, mp->minor));
+               mp->capifs_dentry =
+                       capifs_new_ncci(mp->minor,
+                                       MKDEV(capi_ttymajor, mp->minor));
+       }
+}
+
+static void capincci_free_minor(struct capincci *np)
+{
+       struct capiminor *mp = np->minorp;
+
+       if (mp) {
+               capifs_free_ncci(mp->capifs_dentry);
+               if (mp->tty) {
+                       mp->nccip = NULL;
+#ifdef _DEBUG_REFCOUNT
+                       printk(KERN_DEBUG "reset mp->nccip\n");
 #endif
+                       tty_hangup(mp->tty);
+               } else {
+                       capiminor_free(mp);
+               }
        }
-#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
+}
+
+static inline unsigned int capincci_minor_opencount(struct capincci *np)
+{
+       struct capiminor *mp = np->minorp;
+
+       return mp ? atomic_read(&mp->ttyopencount) : 0;
+}
+
+#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
+
+static inline void
+capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
+static inline void capincci_free_minor(struct capincci *np) { }
+
+static inline unsigned int capincci_minor_opencount(struct capincci *np)
+{
+       return 0;
+}
+
+#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
+
+static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
+{
+       struct capincci *np, **pp;
+
+       np = kzalloc(sizeof(*np), GFP_ATOMIC);
+       if (!np)
+               return NULL;
+       np->ncci = ncci;
+       np->cdev = cdev;
+
+       capincci_alloc_minor(cdev, np);
+
        for (pp=&cdev->nccis; *pp; pp = &(*pp)->next)
                ;
        *pp = np;
@@ -331,33 +375,15 @@ static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
 static void capincci_free(struct capidev *cdev, u32 ncci)
 {
        struct capincci *np, **pp;
-#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
-       struct capiminor *mp;
-#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
 
        pp=&cdev->nccis;
        while (*pp) {
                np = *pp;
                if (ncci == 0xffffffff || np->ncci == ncci) {
                        *pp = (*pp)->next;
-#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
-                       if ((mp = np->minorp) != 0) {
-#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
-                               capifs_free_ncci(mp->minor);
-#endif
-                               if (mp->tty) {
-                                       mp->nccip = NULL;
-#ifdef _DEBUG_REFCOUNT
-                                       printk(KERN_DEBUG "reset mp->nccip\n");
-#endif
-                                       tty_hangup(mp->tty);
-                               } else {
-                                       capiminor_free(mp);
-                               }
-                       }
-#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
+                       capincci_free_minor(np);
                        kfree(np);
-                       if (*pp == 0) return;
+                       if (*pp == NULL) return;
                } else {
                        pp = &(*pp)->next;
                }
@@ -380,24 +406,27 @@ static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
 static struct capidev *capidev_alloc(void)
 {
        struct capidev *cdev;
-       unsigned long flags;
 
        cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
        if (!cdev)
                return NULL;
 
-       init_MUTEX(&cdev->ncci_list_sem);
+       mutex_init(&cdev->ncci_list_mtx);
        skb_queue_head_init(&cdev->recvqueue);
        init_waitqueue_head(&cdev->recvwait);
-       write_lock_irqsave(&capidev_list_lock, flags);
+
+       mutex_lock(&capidev_list_lock);
        list_add_tail(&cdev->list, &capidev_list);
-       write_unlock_irqrestore(&capidev_list_lock, flags);
+       mutex_unlock(&capidev_list_lock);
+
         return cdev;
 }
 
 static void capidev_free(struct capidev *cdev)
 {
-       unsigned long flags;
+       mutex_lock(&capidev_list_lock);
+       list_del(&cdev->list);
+       mutex_unlock(&capidev_list_lock);
 
        if (cdev->ap.applid) {
                capi20_release(&cdev->ap);
@@ -405,13 +434,10 @@ static void capidev_free(struct capidev *cdev)
        }
        skb_queue_purge(&cdev->recvqueue);
 
-       down(&cdev->ncci_list_sem);
+       mutex_lock(&cdev->ncci_list_mtx);
        capincci_free(cdev, 0xffffffff);
-       up(&cdev->ncci_list_sem);
+       mutex_unlock(&cdev->ncci_list_mtx);
 
-       write_lock_irqsave(&capidev_list_lock, flags);
-       list_del(&cdev->list);
-       write_unlock_irqrestore(&capidev_list_lock, flags);
        kfree(cdev);
 }
 
@@ -456,7 +482,7 @@ static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
        ld = tty_ldisc_ref(mp->tty);
        if (ld == NULL)
                return -1;
-       if (ld->receive_buf == NULL) {
+       if (ld->ops->receive_buf == NULL) {
 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
                printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
 #endif
@@ -474,7 +500,7 @@ static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
 #endif
                goto bad;
        }
-       if ((nskb = gen_data_b3_resp_for(mp, skb)) == 0) {
+       if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
                printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
                goto bad;
        }
@@ -491,7 +517,7 @@ static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
        printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
                                datahandle, skb->len);
 #endif
-       ld->receive_buf(mp->tty, skb->data, NULL, skb->len);
+       ld->ops->receive_buf(mp->tty, skb->data, NULL, skb->len);
        kfree_skb(skb);
        tty_ldisc_deref(ld);
        return 0;
@@ -503,7 +529,7 @@ bad:
 static void handle_minor_recv(struct capiminor *mp)
 {
        struct sk_buff *skb;
-       while ((skb = skb_dequeue(&mp->inqueue)) != 0) {
+       while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
                unsigned int len = skb->len;
                mp->inbytes -= len;
                if (handle_recv_skb(mp, skb) < 0) {
@@ -529,7 +555,7 @@ static int handle_minor_send(struct capiminor *mp)
                return 0;
        }
 
-       while ((skb = skb_dequeue(&mp->outqueue)) != 0) {
+       while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
                datahandle = mp->datahandle;
                len = (u16)skb->len;
                skb_push(skb, CAPI_DATA_B3_REQ_LEN);
@@ -540,12 +566,12 @@ static int handle_minor_send(struct capiminor *mp)
                capimsg_setu8 (skb->data, 5, CAPI_REQ);
                capimsg_setu16(skb->data, 6, mp->msgid++);
                capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
-               capimsg_setu32(skb->data, 12, (u32) skb->data); /* Data32 */
+               capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
                capimsg_setu16(skb->data, 16, len);     /* Data length */
                capimsg_setu16(skb->data, 18, datahandle);
                capimsg_setu16(skb->data, 20, 0);       /* Flags */
 
-               if (capincci_add_ack(mp, datahandle) < 0) {
+               if (capiminor_add_ack(mp, datahandle) < 0) {
                        skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
                        skb_queue_head(&mp->outqueue, skb);
                        return count;
@@ -589,23 +615,26 @@ static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
        struct capincci *np;
        u32 ncci;
+       unsigned long flags;
 
        if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
                u16 info = CAPIMSG_U16(skb->data, 12); // Info field
-               if (info == 0) {
-                       down(&cdev->ncci_list_sem);
+               if ((info & 0xff00) == 0) {
+                       mutex_lock(&cdev->ncci_list_mtx);
                        capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
-                       up(&cdev->ncci_list_sem);
+                       mutex_unlock(&cdev->ncci_list_mtx);
                }
        }
        if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND) {
-               down(&cdev->ncci_list_sem);
+               mutex_lock(&cdev->ncci_list_mtx);
                capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
-               up(&cdev->ncci_list_sem);
+               mutex_unlock(&cdev->ncci_list_mtx);
        }
+       spin_lock_irqsave(&workaround_lock, flags);
        if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
                skb_queue_tail(&cdev->recvqueue, skb);
                wake_up_interruptible(&cdev->recvwait);
+               spin_unlock_irqrestore(&workaround_lock, flags);
                return;
        }
        ncci = CAPIMSG_CONTROL(skb->data);
@@ -615,22 +644,24 @@ static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
                printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
                skb_queue_tail(&cdev->recvqueue, skb);
                wake_up_interruptible(&cdev->recvwait);
+               spin_unlock_irqrestore(&workaround_lock, flags);
                return;
        }
+
 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
        skb_queue_tail(&cdev->recvqueue, skb);
        wake_up_interruptible(&cdev->recvwait);
+
 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
+
        mp = np->minorp;
        if (!mp) {
                skb_queue_tail(&cdev->recvqueue, skb);
                wake_up_interruptible(&cdev->recvwait);
+               spin_unlock_irqrestore(&workaround_lock, flags);
                return;
        }
-
-
        if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
-               
                datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
 #ifdef _DEBUG_DATAFLOW
                printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
@@ -660,6 +691,8 @@ static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
                wake_up_interruptible(&cdev->recvwait);
        }
 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
+
+       spin_unlock_irqrestore(&workaround_lock, flags);
 }
 
 /* -------- file_operations for capidev ----------------------------- */
@@ -674,19 +707,19 @@ capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
        if (!cdev->ap.applid)
                return -ENODEV;
 
-       if ((skb = skb_dequeue(&cdev->recvqueue)) == 0) {
+       if ((skb = skb_dequeue(&cdev->recvqueue)) == NULL) {
 
                if (file->f_flags & O_NONBLOCK)
                        return -EAGAIN;
 
                for (;;) {
                        interruptible_sleep_on(&cdev->recvwait);
-                       if ((skb = skb_dequeue(&cdev->recvqueue)) != 0)
+                       if ((skb = skb_dequeue(&cdev->recvqueue)) != NULL)
                                break;
                        if (signal_pending(current))
                                break;
                }
-               if (skb == 0)
+               if (skb == NULL)
                        return -ERESTARTNOHAND;
        }
        if (skb->len > count) {
@@ -737,9 +770,9 @@ capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos
        CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
 
        if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
-               down(&cdev->ncci_list_sem);
+               mutex_lock(&cdev->ncci_list_mtx);
                capincci_free(cdev, CAPIMSG_NCCI(skb->data));
-               up(&cdev->ncci_list_sem);
+               mutex_unlock(&cdev->ncci_list_mtx);
        }
 
        cdev->errcode = capi20_put_message(&cdev->ap, skb);
@@ -916,25 +949,18 @@ capi_ioctl(struct inode *inode, struct file *file,
        case CAPI_NCCI_OPENCOUNT:
                {
                        struct capincci *nccip;
-#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
-                       struct capiminor *mp;
-#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
                        unsigned ncci;
                        int count = 0;
                        if (copy_from_user(&ncci, argp, sizeof(ncci)))
                                return -EFAULT;
 
-                       down(&cdev->ncci_list_sem);
-                       if ((nccip = capincci_find(cdev, (u32) ncci)) == 0) {
-                               up(&cdev->ncci_list_sem);
+                       mutex_lock(&cdev->ncci_list_mtx);
+                       if ((nccip = capincci_find(cdev, (u32) ncci)) == NULL) {
+                               mutex_unlock(&cdev->ncci_list_mtx);
                                return 0;
                        }
-#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
-                       if ((mp = nccip->minorp) != 0) {
-                               count += atomic_read(&mp->ttyopencount);
-                       }
-#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
-                       up(&cdev->ncci_list_sem);
+                       count += capincci_minor_opencount(nccip);
+                       mutex_unlock(&cdev->ncci_list_mtx);
                        return count;
                }
                return 0;
@@ -949,14 +975,14 @@ capi_ioctl(struct inode *inode, struct file *file,
                        if (copy_from_user(&ncci, argp,
                                           sizeof(ncci)))
                                return -EFAULT;
-                       down(&cdev->ncci_list_sem);
+                       mutex_lock(&cdev->ncci_list_mtx);
                        nccip = capincci_find(cdev, (u32) ncci);
-                       if (!nccip || (mp = nccip->minorp) == 0) {
-                               up(&cdev->ncci_list_sem);
+                       if (!nccip || (mp = nccip->minorp) == NULL) {
+                               mutex_unlock(&cdev->ncci_list_mtx);
                                return -ESRCH;
                        }
                        unit = mp->minor;
-                       up(&cdev->ncci_list_sem);
+                       mutex_unlock(&cdev->ncci_list_mtx);
                        return unit;
                }
                return 0;
@@ -968,13 +994,17 @@ capi_ioctl(struct inode *inode, struct file *file,
 static int
 capi_open(struct inode *inode, struct file *file)
 {
+       int ret;
+       
+       lock_kernel();
        if (file->private_data)
-               return -EEXIST;
-
-       if ((file->private_data = capidev_alloc()) == 0)
-               return -ENOMEM;
-
-       return nonseekable_open(inode, file);
+               ret = -EEXIST;
+       else if ((file->private_data = capidev_alloc()) == NULL)
+               ret = -ENOMEM;
+       else
+               ret = nonseekable_open(inode, file);
+       unlock_kernel();
+       return ret;
 }
 
 static int
@@ -988,7 +1018,7 @@ capi_release(struct inode *inode, struct file *file)
        return 0;
 }
 
-static struct file_operations capi_fops =
+static const struct file_operations capi_fops =
 {
        .owner          = THIS_MODULE,
        .llseek         = no_llseek,
@@ -1006,14 +1036,16 @@ static struct file_operations capi_fops =
 static int capinc_tty_open(struct tty_struct * tty, struct file * file)
 {
        struct capiminor *mp;
+       unsigned long flags;
 
-       if ((mp = capiminor_find(iminor(file->f_path.dentry->d_inode))) == 0)
+       if ((mp = capiminor_find(iminor(file->f_path.dentry->d_inode))) == NULL)
                return -ENXIO;
-       if (mp->nccip == 0)
+       if (mp->nccip == NULL)
                return -ENXIO;
 
        tty->driver_data = (void *)mp;
 
+       spin_lock_irqsave(&workaround_lock, flags);
        if (atomic_read(&mp->ttyopencount) == 0)
                mp->tty = tty;
        atomic_inc(&mp->ttyopencount);
@@ -1021,6 +1053,7 @@ static int capinc_tty_open(struct tty_struct * tty, struct file * file)
        printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
 #endif
        handle_minor_recv(mp);
+       spin_unlock_irqrestore(&workaround_lock, flags);
        return 0;
 }
 
@@ -1040,7 +1073,7 @@ static void capinc_tty_close(struct tty_struct * tty, struct file * file)
 #ifdef _DEBUG_REFCOUNT
                printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
 #endif
-               if (mp->nccip == 0)
+               if (mp->nccip == NULL)
                        capiminor_free(mp);
        }
 
@@ -1054,6 +1087,7 @@ static int capinc_tty_write(struct tty_struct * tty,
 {
        struct capiminor *mp = (struct capiminor *)tty->driver_data;
        struct sk_buff *skb;
+       unsigned long flags;
 
 #ifdef _DEBUG_TTYFUNCS
        printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
@@ -1066,6 +1100,7 @@ static int capinc_tty_write(struct tty_struct * tty,
                return 0;
        }
 
+       spin_lock_irqsave(&workaround_lock, flags);
        skb = mp->ttyskb;
        if (skb) {
                mp->ttyskb = NULL;
@@ -1076,6 +1111,7 @@ static int capinc_tty_write(struct tty_struct * tty,
        skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
        if (!skb) {
                printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
+               spin_unlock_irqrestore(&workaround_lock, flags);
                return -ENOMEM;
        }
 
@@ -1086,13 +1122,16 @@ static int capinc_tty_write(struct tty_struct * tty,
        mp->outbytes += skb->len;
        (void)handle_minor_send(mp);
        (void)handle_minor_recv(mp);
+       spin_unlock_irqrestore(&workaround_lock, flags);
        return count;
 }
 
-static void capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
+static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
 {
        struct capiminor *mp = (struct capiminor *)tty->driver_data;
        struct sk_buff *skb;
+       unsigned long flags;
+       int ret = 1;
 
 #ifdef _DEBUG_TTYFUNCS
        printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
@@ -1102,14 +1141,16 @@ static void capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
 #ifdef _DEBUG_TTYFUNCS
                printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
 #endif
-               return;
+               return 0;
        }
 
+       spin_lock_irqsave(&workaround_lock, flags);
        skb = mp->ttyskb;
        if (skb) {
                if (skb_tailroom(skb) > 0) {
                        *(skb_put(skb, 1)) = ch;
-                       return;
+                       spin_unlock_irqrestore(&workaround_lock, flags);
+                       return 1;
                }
                mp->ttyskb = NULL;
                skb_queue_tail(&mp->outqueue, skb);
@@ -1123,13 +1164,17 @@ static void capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
                mp->ttyskb = skb;
        } else {
                printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
+               ret = 0;
        }
+       spin_unlock_irqrestore(&workaround_lock, flags);
+       return ret;
 }
 
 static void capinc_tty_flush_chars(struct tty_struct *tty)
 {
        struct capiminor *mp = (struct capiminor *)tty->driver_data;
        struct sk_buff *skb;
+       unsigned long flags;
 
 #ifdef _DEBUG_TTYFUNCS
        printk(KERN_DEBUG "capinc_tty_flush_chars\n");
@@ -1142,6 +1187,7 @@ static void capinc_tty_flush_chars(struct tty_struct *tty)
                return;
        }
 
+       spin_lock_irqsave(&workaround_lock, flags);
        skb = mp->ttyskb;
        if (skb) {
                mp->ttyskb = NULL;
@@ -1150,6 +1196,7 @@ static void capinc_tty_flush_chars(struct tty_struct *tty)
                (void)handle_minor_send(mp);
        }
        (void)handle_minor_recv(mp);
+       spin_unlock_irqrestore(&workaround_lock, flags);
 }
 
 static int capinc_tty_write_room(struct tty_struct *tty)
@@ -1194,7 +1241,7 @@ static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
        int error = 0;
        switch (cmd) {
        default:
-               error = n_tty_ioctl (tty, file, cmd, arg);
+               error = n_tty_ioctl_helper(tty, file, cmd, arg);
                break;
        }
        return error;
@@ -1220,12 +1267,15 @@ static void capinc_tty_throttle(struct tty_struct * tty)
 static void capinc_tty_unthrottle(struct tty_struct * tty)
 {
        struct capiminor *mp = (struct capiminor *)tty->driver_data;
+       unsigned long flags;
 #ifdef _DEBUG_TTYFUNCS
        printk(KERN_DEBUG "capinc_tty_unthrottle\n");
 #endif
        if (mp) {
+               spin_lock_irqsave(&workaround_lock, flags);
                mp->ttyinstop = 0;
                handle_minor_recv(mp);
+               spin_unlock_irqrestore(&workaround_lock, flags);
        }
 }
 
@@ -1243,12 +1293,15 @@ static void capinc_tty_stop(struct tty_struct *tty)
 static void capinc_tty_start(struct tty_struct *tty)
 {
        struct capiminor *mp = (struct capiminor *)tty->driver_data;
+       unsigned long flags;
 #ifdef _DEBUG_TTYFUNCS
        printk(KERN_DEBUG "capinc_tty_start\n");
 #endif
        if (mp) {
+               spin_lock_irqsave(&workaround_lock, flags);
                mp->ttyoutstop = 0;
                (void)handle_minor_send(mp);
+               spin_unlock_irqrestore(&workaround_lock, flags);
        }
 }
 
@@ -1259,11 +1312,12 @@ static void capinc_tty_hangup(struct tty_struct *tty)
 #endif
 }
 
-static void capinc_tty_break_ctl(struct tty_struct *tty, int state)
+static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
 {
 #ifdef _DEBUG_TTYFUNCS
        printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
 #endif
+       return 0;
 }
 
 static void capinc_tty_flush_buffer(struct tty_struct *tty)
@@ -1287,12 +1341,6 @@ static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
 #endif
 }
 
-static int capinc_tty_read_proc(char *page, char **start, off_t off,
-                               int count, int *eof, void *data)
-{
-       return 0;
-}
-
 static struct tty_driver *capinc_tty_driver;
 
 static const struct tty_operations capinc_ops = {
@@ -1314,7 +1362,6 @@ static const struct tty_operations capinc_ops = {
        .flush_buffer = capinc_tty_flush_buffer,
        .set_ldisc = capinc_tty_set_ldisc,
        .send_xchar = capinc_tty_send_xchar,
-       .read_proc = capinc_tty_read_proc,
 };
 
 static int capinc_tty_init(void)
@@ -1362,7 +1409,16 @@ static void capinc_tty_exit(void)
        put_tty_driver(drv);
 }
 
-#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
+#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
+
+static inline int capinc_tty_init(void)
+{
+       return 0;
+}
+
+static inline void capinc_tty_exit(void) { }
+
+#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
 
 /* -------- /proc functions ----------------------------------------- */
 
@@ -1370,134 +1426,94 @@ static void capinc_tty_exit(void)
  * /proc/capi/capi20:
  *  minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
  */
-static int proc_capidev_read_proc(char *page, char **start, off_t off,
-                                       int count, int *eof, void *data)
+static int capi20_proc_show(struct seq_file *m, void *v)
 {
         struct capidev *cdev;
        struct list_head *l;
-       int len = 0;
 
-       read_lock(&capidev_list_lock);
+       mutex_lock(&capidev_list_lock);
        list_for_each(l, &capidev_list) {
                cdev = list_entry(l, struct capidev, list);
-               len += sprintf(page+len, "0 %d %lu %lu %lu %lu\n",
+               seq_printf(m, "0 %d %lu %lu %lu %lu\n",
                        cdev->ap.applid,
                        cdev->ap.nrecvctlpkt,
                        cdev->ap.nrecvdatapkt,
                        cdev->ap.nsentctlpkt,
                        cdev->ap.nsentdatapkt);
-               if (len <= off) {
-                       off -= len;
-                       len = 0;
-               } else {
-                       if (len-off > count)
-                               goto endloop;
-               }
        }
+       mutex_unlock(&capidev_list_lock);
+       return 0;
+}
 
-endloop:
-       read_unlock(&capidev_list_lock);
-       if (len < count)
-               *eof = 1;
-       if (len > count) len = count;
-       if (len < 0) len = 0;
-       return len;
+static int capi20_proc_open(struct inode *inode, struct file *file)
+{
+       return single_open(file, capi20_proc_show, NULL);
 }
 
+static const struct file_operations capi20_proc_fops = {
+       .owner          = THIS_MODULE,
+       .open           = capi20_proc_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = single_release,
+};
+
 /*
  * /proc/capi/capi20ncci:
  *  applid ncci
  */
-static int proc_capincci_read_proc(char *page, char **start, off_t off,
-                                       int count, int *eof, void *data)
+static int capi20ncci_proc_show(struct seq_file *m, void *v)
 {
         struct capidev *cdev;
         struct capincci *np;
        struct list_head *l;
-       int len = 0;
 
-       read_lock(&capidev_list_lock);
+       mutex_lock(&capidev_list_lock);
        list_for_each(l, &capidev_list) {
                cdev = list_entry(l, struct capidev, list);
                for (np=cdev->nccis; np; np = np->next) {
-                       len += sprintf(page+len, "%d 0x%x\n",
+                       seq_printf(m, "%d 0x%x\n",
                                       cdev->ap.applid,
                                       np->ncci);
-                       if (len <= off) {
-                               off -= len;
-                               len = 0;
-                       } else {
-                               if (len-off > count)
-                                       goto endloop;
-                       }
                }
        }
-endloop:
-       read_unlock(&capidev_list_lock);
-       *start = page+off;
-       if (len < count)
-               *eof = 1;
-       if (len>count) len = count;
-       if (len<0) len = 0;
-       return len;
+       mutex_unlock(&capidev_list_lock);
+       return 0;
 }
 
-static struct procfsentries {
-  char *name;
-  mode_t mode;
-  int (*read_proc)(char *page, char **start, off_t off,
-                                       int count, int *eof, void *data);
-  struct proc_dir_entry *procent;
-} procfsentries[] = {
-   /* { "capi",                  S_IFDIR, 0 }, */
-   { "capi/capi20",      0      , proc_capidev_read_proc },
-   { "capi/capi20ncci",   0     , proc_capincci_read_proc },
+static int capi20ncci_proc_open(struct inode *inode, struct file *file)
+{
+       return single_open(file, capi20ncci_proc_show, NULL);
+}
+
+static const struct file_operations capi20ncci_proc_fops = {
+       .owner          = THIS_MODULE,
+       .open           = capi20ncci_proc_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = single_release,
 };
 
 static void __init proc_init(void)
 {
-    int nelem = sizeof(procfsentries)/sizeof(procfsentries[0]);
-    int i;
-
-    for (i=0; i < nelem; i++) {
-        struct procfsentries *p = procfsentries + i;
-       p->procent = create_proc_entry(p->name, p->mode, NULL);
-       if (p->procent) p->procent->read_proc = p->read_proc;
-    }
+       proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
+       proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
 }
 
 static void __exit proc_exit(void)
 {
-    int nelem = sizeof(procfsentries)/sizeof(procfsentries[0]);
-    int i;
-
-    for (i=nelem-1; i >= 0; i--) {
-        struct procfsentries *p = procfsentries + i;
-       if (p->procent) {
-          remove_proc_entry(p->name, NULL);
-          p->procent = NULL;
-       }
-    }
+       remove_proc_entry("capi/capi20", NULL);
+       remove_proc_entry("capi/capi20ncci", NULL);
 }
 
 /* -------- init function and module interface ---------------------- */
 
 
-static char rev[32];
-
 static int __init capi_init(void)
 {
-       char *p;
-       char *compileinfo;
+       const char *compileinfo;
        int major_ret;
 
-       if ((p = strchr(revision, ':')) != 0 && p[1]) {
-               strlcpy(rev, p + 2, sizeof(rev));
-               if ((p = strchr(rev, '$')) != 0 && p > rev)
-                  *(p-1) = 0;
-       } else
-               strcpy(rev, "1.0");
-
        major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
        if (major_ret < 0) {
                printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
@@ -1509,30 +1525,26 @@ static int __init capi_init(void)
                return PTR_ERR(capi_class);
        }
 
-       class_device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
+       device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
 
-#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
        if (capinc_tty_init() < 0) {
-               class_device_destroy(capi_class, MKDEV(capi_major, 0));
+               device_destroy(capi_class, MKDEV(capi_major, 0));
                class_destroy(capi_class);
                unregister_chrdev(capi_major, "capi20");
                return -ENOMEM;
        }
-#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
 
        proc_init();
 
-#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
         compileinfo = " (middleware+capifs)";
-#else
+#elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
         compileinfo = " (no capifs)";
-#endif
 #else
         compileinfo = " (no middleware)";
 #endif
-       printk(KERN_NOTICE "capi20: Rev %s: started up with major %d%s\n",
-                               rev, capi_major, compileinfo);
+       printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
+              capi_major, compileinfo);
 
        return 0;
 }
@@ -1541,14 +1553,11 @@ static void __exit capi_exit(void)
 {
        proc_exit();
 
-       class_device_destroy(capi_class, MKDEV(capi_major, 0));
+       device_destroy(capi_class, MKDEV(capi_major, 0));
        class_destroy(capi_class);
        unregister_chrdev(capi_major, "capi20");
 
-#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
        capinc_tty_exit();
-#endif
-       printk(KERN_NOTICE "capi: Rev %s: unloaded\n", rev);
 }
 
 module_init(capi_init);