CAPI: Convert capidev_list_lock into a mutex
[safe/jmp/linux-2.6] / drivers / isdn / capi / capi.c
index 65bf91e..623412e 100644 (file)
 #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");
@@ -58,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 */
@@ -83,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 {
@@ -95,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;
@@ -117,7 +111,6 @@ 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).
@@ -153,18 +146,17 @@ struct capidev {
 
 /* -------- 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;
@@ -302,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;
@@ -341,31 +375,13 @@ 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) != NULL) {
-#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 == NULL) return;
                } else {
@@ -390,7 +406,6 @@ 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)
@@ -399,15 +414,19 @@ static struct capidev *capidev_alloc(void)
        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);
@@ -419,9 +438,6 @@ static void capidev_free(struct capidev *cdev)
        capincci_free(cdev, 0xffffffff);
        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);
 }
 
@@ -555,7 +571,7 @@ static int handle_minor_send(struct capiminor *mp)
                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;
@@ -631,10 +647,13 @@ static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
                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);
@@ -642,10 +661,7 @@ static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
                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",
@@ -675,6 +691,7 @@ 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);
 }
 
@@ -932,9 +949,6 @@ 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)))
@@ -945,11 +959,7 @@ capi_ioctl(struct inode *inode, struct file *file,
                                mutex_unlock(&cdev->ncci_list_mtx);
                                return 0;
                        }
-#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
-                       if ((mp = nccip->minorp) != NULL) {
-                               count += atomic_read(&mp->ttyopencount);
-                       }
-#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
+                       count += capincci_minor_opencount(nccip);
                        mutex_unlock(&cdev->ncci_list_mtx);
                        return count;
                }
@@ -1399,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 ----------------------------------------- */
 
@@ -1407,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 int capi20ncci_proc_open(struct inode *inode, struct file *file)
+{
+       return single_open(file, capi20ncci_proc_show, NULL);
 }
 
-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 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 = ARRAY_SIZE(procfsentries);
-    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 = ARRAY_SIZE(procfsentries);
-    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, ':')) != NULL && p[1]) {
-               strlcpy(rev, p + 2, sizeof(rev));
-               if ((p = strchr(rev, '$')) != NULL && 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);
@@ -1548,28 +1527,24 @@ static int __init capi_init(void)
 
        device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
 
-#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
        if (capinc_tty_init() < 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;
 }
@@ -1582,10 +1557,7 @@ static void __exit capi_exit(void)
        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);