Back to home page

OSCL-LXR

 
 

    


0001 /* $Id: capi.c,v 1.1.2.7 2004/04/28 09:48:59 armin Exp $
0002  *
0003  * CAPI 2.0 Interface for Linux
0004  *
0005  * Copyright 1996 by Carsten Paeth <calle@calle.de>
0006  *
0007  * This software may be used and distributed according to the terms
0008  * of the GNU General Public License, incorporated herein by reference.
0009  *
0010  */
0011 
0012 #include <linux/compiler.h>
0013 #include <linux/module.h>
0014 #include <linux/ethtool.h>
0015 #include <linux/errno.h>
0016 #include <linux/kernel.h>
0017 #include <linux/major.h>
0018 #include <linux/sched.h>
0019 #include <linux/slab.h>
0020 #include <linux/fcntl.h>
0021 #include <linux/fs.h>
0022 #include <linux/signal.h>
0023 #include <linux/mutex.h>
0024 #include <linux/mm.h>
0025 #include <linux/timer.h>
0026 #include <linux/wait.h>
0027 #include <linux/tty.h>
0028 #include <linux/netdevice.h>
0029 #include <linux/ppp_defs.h>
0030 #include <linux/ppp-ioctl.h>
0031 #include <linux/skbuff.h>
0032 #include <linux/proc_fs.h>
0033 #include <linux/seq_file.h>
0034 #include <linux/poll.h>
0035 #include <linux/capi.h>
0036 #include <linux/kernelcapi.h>
0037 #include <linux/init.h>
0038 #include <linux/device.h>
0039 #include <linux/moduleparam.h>
0040 #include <linux/isdn/capiutil.h>
0041 #include <linux/isdn/capicmd.h>
0042 
0043 #include "kcapi.h"
0044 
0045 MODULE_DESCRIPTION("CAPI4Linux: kernel CAPI layer and /dev/capi20 interface");
0046 MODULE_AUTHOR("Carsten Paeth");
0047 MODULE_LICENSE("GPL");
0048 
0049 /* -------- driver information -------------------------------------- */
0050 
0051 static DEFINE_MUTEX(capi_mutex);
0052 static struct class *capi_class;
0053 static int capi_major = 68;     /* allocated */
0054 
0055 module_param_named(major, capi_major, uint, 0);
0056 
0057 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
0058 #define CAPINC_NR_PORTS     32
0059 #define CAPINC_MAX_PORTS    256
0060 
0061 static int capi_ttyminors = CAPINC_NR_PORTS;
0062 
0063 module_param_named(ttyminors, capi_ttyminors, uint, 0);
0064 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
0065 
0066 /* -------- defines ------------------------------------------------- */
0067 
0068 #define CAPINC_MAX_RECVQUEUE    10
0069 #define CAPINC_MAX_SENDQUEUE    10
0070 #define CAPI_MAX_BLKSIZE    2048
0071 
0072 /* -------- data structures ----------------------------------------- */
0073 
0074 struct capidev;
0075 struct capincci;
0076 struct capiminor;
0077 
0078 struct ackqueue_entry {
0079     struct list_head    list;
0080     u16         datahandle;
0081 };
0082 
0083 struct capiminor {
0084     unsigned int      minor;
0085 
0086     struct capi20_appl  *ap;
0087     u32         ncci;
0088     atomic_t        datahandle;
0089     atomic_t        msgid;
0090 
0091     struct tty_port port;
0092     int                ttyinstop;
0093     int                ttyoutstop;
0094 
0095     struct sk_buff_head inqueue;
0096 
0097     struct sk_buff_head outqueue;
0098     int         outbytes;
0099     struct sk_buff      *outskb;
0100     spinlock_t      outlock;
0101 
0102     /* transmit path */
0103     struct list_head ackqueue;
0104     int nack;
0105     spinlock_t ackqlock;
0106 };
0107 
0108 struct capincci {
0109     struct list_head list;
0110     u32      ncci;
0111     struct capidev  *cdev;
0112 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
0113     struct capiminor *minorp;
0114 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
0115 };
0116 
0117 struct capidev {
0118     struct list_head list;
0119     struct capi20_appl ap;
0120     u16     errcode;
0121     unsigned        userflags;
0122 
0123     struct sk_buff_head recvqueue;
0124     wait_queue_head_t recvwait;
0125 
0126     struct list_head nccis;
0127 
0128     struct mutex lock;
0129 };
0130 
0131 /* -------- global variables ---------------------------------------- */
0132 
0133 static DEFINE_MUTEX(capidev_list_lock);
0134 static LIST_HEAD(capidev_list);
0135 
0136 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
0137 
0138 static DEFINE_SPINLOCK(capiminors_lock);
0139 static struct capiminor **capiminors;
0140 
0141 static struct tty_driver *capinc_tty_driver;
0142 
0143 /* -------- datahandles --------------------------------------------- */
0144 
0145 static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
0146 {
0147     struct ackqueue_entry *n;
0148 
0149     n = kmalloc(sizeof(*n), GFP_ATOMIC);
0150     if (unlikely(!n)) {
0151         printk(KERN_ERR "capi: alloc datahandle failed\n");
0152         return -1;
0153     }
0154     n->datahandle = datahandle;
0155     INIT_LIST_HEAD(&n->list);
0156     spin_lock_bh(&mp->ackqlock);
0157     list_add_tail(&n->list, &mp->ackqueue);
0158     mp->nack++;
0159     spin_unlock_bh(&mp->ackqlock);
0160     return 0;
0161 }
0162 
0163 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
0164 {
0165     struct ackqueue_entry *p, *tmp;
0166 
0167     spin_lock_bh(&mp->ackqlock);
0168     list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
0169         if (p->datahandle == datahandle) {
0170             list_del(&p->list);
0171             mp->nack--;
0172             spin_unlock_bh(&mp->ackqlock);
0173             kfree(p);
0174             return 0;
0175         }
0176     }
0177     spin_unlock_bh(&mp->ackqlock);
0178     return -1;
0179 }
0180 
0181 static void capiminor_del_all_ack(struct capiminor *mp)
0182 {
0183     struct ackqueue_entry *p, *tmp;
0184 
0185     list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
0186         list_del(&p->list);
0187         kfree(p);
0188         mp->nack--;
0189     }
0190 }
0191 
0192 
0193 /* -------- struct capiminor ---------------------------------------- */
0194 
0195 static void capiminor_destroy(struct tty_port *port)
0196 {
0197     struct capiminor *mp = container_of(port, struct capiminor, port);
0198 
0199     kfree_skb(mp->outskb);
0200     skb_queue_purge(&mp->inqueue);
0201     skb_queue_purge(&mp->outqueue);
0202     capiminor_del_all_ack(mp);
0203     kfree(mp);
0204 }
0205 
0206 static const struct tty_port_operations capiminor_port_ops = {
0207     .destruct = capiminor_destroy,
0208 };
0209 
0210 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
0211 {
0212     struct capiminor *mp;
0213     struct device *dev;
0214     unsigned int minor;
0215 
0216     mp = kzalloc(sizeof(*mp), GFP_KERNEL);
0217     if (!mp) {
0218         printk(KERN_ERR "capi: can't alloc capiminor\n");
0219         return NULL;
0220     }
0221 
0222     mp->ap = ap;
0223     mp->ncci = ncci;
0224     INIT_LIST_HEAD(&mp->ackqueue);
0225     spin_lock_init(&mp->ackqlock);
0226 
0227     skb_queue_head_init(&mp->inqueue);
0228     skb_queue_head_init(&mp->outqueue);
0229     spin_lock_init(&mp->outlock);
0230 
0231     tty_port_init(&mp->port);
0232     mp->port.ops = &capiminor_port_ops;
0233 
0234     /* Allocate the least unused minor number. */
0235     spin_lock(&capiminors_lock);
0236     for (minor = 0; minor < capi_ttyminors; minor++)
0237         if (!capiminors[minor]) {
0238             capiminors[minor] = mp;
0239             break;
0240         }
0241     spin_unlock(&capiminors_lock);
0242 
0243     if (minor == capi_ttyminors) {
0244         printk(KERN_NOTICE "capi: out of minors\n");
0245         goto err_out1;
0246     }
0247 
0248     mp->minor = minor;
0249 
0250     dev = tty_port_register_device(&mp->port, capinc_tty_driver, minor,
0251             NULL);
0252     if (IS_ERR(dev))
0253         goto err_out2;
0254 
0255     return mp;
0256 
0257 err_out2:
0258     spin_lock(&capiminors_lock);
0259     capiminors[minor] = NULL;
0260     spin_unlock(&capiminors_lock);
0261 
0262 err_out1:
0263     tty_port_put(&mp->port);
0264     return NULL;
0265 }
0266 
0267 static struct capiminor *capiminor_get(unsigned int minor)
0268 {
0269     struct capiminor *mp;
0270 
0271     spin_lock(&capiminors_lock);
0272     mp = capiminors[minor];
0273     if (mp)
0274         tty_port_get(&mp->port);
0275     spin_unlock(&capiminors_lock);
0276 
0277     return mp;
0278 }
0279 
0280 static inline void capiminor_put(struct capiminor *mp)
0281 {
0282     tty_port_put(&mp->port);
0283 }
0284 
0285 static void capiminor_free(struct capiminor *mp)
0286 {
0287     tty_unregister_device(capinc_tty_driver, mp->minor);
0288 
0289     spin_lock(&capiminors_lock);
0290     capiminors[mp->minor] = NULL;
0291     spin_unlock(&capiminors_lock);
0292 
0293     capiminor_put(mp);
0294 }
0295 
0296 /* -------- struct capincci ----------------------------------------- */
0297 
0298 static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
0299 {
0300     if (cdev->userflags & CAPIFLAG_HIGHJACKING)
0301         np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
0302 }
0303 
0304 static void capincci_free_minor(struct capincci *np)
0305 {
0306     struct capiminor *mp = np->minorp;
0307     struct tty_struct *tty;
0308 
0309     if (mp) {
0310         tty = tty_port_tty_get(&mp->port);
0311         if (tty) {
0312             tty_vhangup(tty);
0313             tty_kref_put(tty);
0314         }
0315 
0316         capiminor_free(mp);
0317     }
0318 }
0319 
0320 static inline unsigned int capincci_minor_opencount(struct capincci *np)
0321 {
0322     struct capiminor *mp = np->minorp;
0323     unsigned int count = 0;
0324     struct tty_struct *tty;
0325 
0326     if (mp) {
0327         tty = tty_port_tty_get(&mp->port);
0328         if (tty) {
0329             count = tty->count;
0330             tty_kref_put(tty);
0331         }
0332     }
0333     return count;
0334 }
0335 
0336 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
0337 
0338 static inline void
0339 capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
0340 static inline void capincci_free_minor(struct capincci *np) { }
0341 
0342 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
0343 
0344 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
0345 {
0346     struct capincci *np;
0347 
0348     np = kzalloc(sizeof(*np), GFP_KERNEL);
0349     if (!np)
0350         return NULL;
0351     np->ncci = ncci;
0352     np->cdev = cdev;
0353 
0354     capincci_alloc_minor(cdev, np);
0355 
0356     list_add_tail(&np->list, &cdev->nccis);
0357 
0358     return np;
0359 }
0360 
0361 static void capincci_free(struct capidev *cdev, u32 ncci)
0362 {
0363     struct capincci *np, *tmp;
0364 
0365     list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
0366         if (ncci == 0xffffffff || np->ncci == ncci) {
0367             capincci_free_minor(np);
0368             list_del(&np->list);
0369             kfree(np);
0370         }
0371 }
0372 
0373 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
0374 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
0375 {
0376     struct capincci *np;
0377 
0378     list_for_each_entry(np, &cdev->nccis, list)
0379         if (np->ncci == ncci)
0380             return np;
0381     return NULL;
0382 }
0383 
0384 /* -------- handle data queue --------------------------------------- */
0385 
0386 static struct sk_buff *
0387 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
0388 {
0389     struct sk_buff *nskb;
0390     nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_KERNEL);
0391     if (nskb) {
0392         u16 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
0393         unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
0394         capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
0395         capimsg_setu16(s, 2, mp->ap->applid);
0396         capimsg_setu8 (s, 4, CAPI_DATA_B3);
0397         capimsg_setu8 (s, 5, CAPI_RESP);
0398         capimsg_setu16(s, 6, atomic_inc_return(&mp->msgid));
0399         capimsg_setu32(s, 8, mp->ncci);
0400         capimsg_setu16(s, 12, datahandle);
0401     }
0402     return nskb;
0403 }
0404 
0405 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
0406 {
0407     unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
0408     struct tty_struct *tty;
0409     struct sk_buff *nskb;
0410     u16 errcode, datahandle;
0411     struct tty_ldisc *ld;
0412     int ret = -1;
0413 
0414     tty = tty_port_tty_get(&mp->port);
0415     if (!tty) {
0416         pr_debug("capi: currently no receiver\n");
0417         return -1;
0418     }
0419 
0420     ld = tty_ldisc_ref(tty);
0421     if (!ld) {
0422         /* fatal error, do not requeue */
0423         ret = 0;
0424         kfree_skb(skb);
0425         goto deref_tty;
0426     }
0427 
0428     if (ld->ops->receive_buf == NULL) {
0429         pr_debug("capi: ldisc has no receive_buf function\n");
0430         /* fatal error, do not requeue */
0431         goto free_skb;
0432     }
0433     if (mp->ttyinstop) {
0434         pr_debug("capi: recv tty throttled\n");
0435         goto deref_ldisc;
0436     }
0437 
0438     if (tty->receive_room < datalen) {
0439         pr_debug("capi: no room in tty\n");
0440         goto deref_ldisc;
0441     }
0442 
0443     nskb = gen_data_b3_resp_for(mp, skb);
0444     if (!nskb) {
0445         printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
0446         goto deref_ldisc;
0447     }
0448 
0449     datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
0450 
0451     errcode = capi20_put_message(mp->ap, nskb);
0452 
0453     if (errcode == CAPI_NOERROR) {
0454         skb_pull(skb, CAPIMSG_LEN(skb->data));
0455         pr_debug("capi: DATA_B3_RESP %u len=%d => ldisc\n",
0456              datahandle, skb->len);
0457         ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
0458     } else {
0459         printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
0460                errcode);
0461         kfree_skb(nskb);
0462 
0463         if (errcode == CAPI_SENDQUEUEFULL)
0464             goto deref_ldisc;
0465     }
0466 
0467 free_skb:
0468     ret = 0;
0469     kfree_skb(skb);
0470 
0471 deref_ldisc:
0472     tty_ldisc_deref(ld);
0473 
0474 deref_tty:
0475     tty_kref_put(tty);
0476     return ret;
0477 }
0478 
0479 static void handle_minor_recv(struct capiminor *mp)
0480 {
0481     struct sk_buff *skb;
0482 
0483     while ((skb = skb_dequeue(&mp->inqueue)) != NULL)
0484         if (handle_recv_skb(mp, skb) < 0) {
0485             skb_queue_head(&mp->inqueue, skb);
0486             return;
0487         }
0488 }
0489 
0490 static void handle_minor_send(struct capiminor *mp)
0491 {
0492     struct tty_struct *tty;
0493     struct sk_buff *skb;
0494     u16 len;
0495     u16 errcode;
0496     u16 datahandle;
0497 
0498     tty = tty_port_tty_get(&mp->port);
0499     if (!tty)
0500         return;
0501 
0502     if (mp->ttyoutstop) {
0503         pr_debug("capi: send: tty stopped\n");
0504         tty_kref_put(tty);
0505         return;
0506     }
0507 
0508     while (1) {
0509         spin_lock_bh(&mp->outlock);
0510         skb = __skb_dequeue(&mp->outqueue);
0511         if (!skb) {
0512             spin_unlock_bh(&mp->outlock);
0513             break;
0514         }
0515         len = (u16)skb->len;
0516         mp->outbytes -= len;
0517         spin_unlock_bh(&mp->outlock);
0518 
0519         datahandle = atomic_inc_return(&mp->datahandle);
0520         skb_push(skb, CAPI_DATA_B3_REQ_LEN);
0521         memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
0522         capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
0523         capimsg_setu16(skb->data, 2, mp->ap->applid);
0524         capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
0525         capimsg_setu8 (skb->data, 5, CAPI_REQ);
0526         capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid));
0527         capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
0528         capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
0529         capimsg_setu16(skb->data, 16, len); /* Data length */
0530         capimsg_setu16(skb->data, 18, datahandle);
0531         capimsg_setu16(skb->data, 20, 0);   /* Flags */
0532 
0533         if (capiminor_add_ack(mp, datahandle) < 0) {
0534             skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
0535 
0536             spin_lock_bh(&mp->outlock);
0537             __skb_queue_head(&mp->outqueue, skb);
0538             mp->outbytes += len;
0539             spin_unlock_bh(&mp->outlock);
0540 
0541             break;
0542         }
0543         errcode = capi20_put_message(mp->ap, skb);
0544         if (errcode == CAPI_NOERROR) {
0545             pr_debug("capi: DATA_B3_REQ %u len=%u\n",
0546                  datahandle, len);
0547             continue;
0548         }
0549         capiminor_del_ack(mp, datahandle);
0550 
0551         if (errcode == CAPI_SENDQUEUEFULL) {
0552             skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
0553 
0554             spin_lock_bh(&mp->outlock);
0555             __skb_queue_head(&mp->outqueue, skb);
0556             mp->outbytes += len;
0557             spin_unlock_bh(&mp->outlock);
0558 
0559             break;
0560         }
0561 
0562         /* ups, drop packet */
0563         printk(KERN_ERR "capi: put_message = %x\n", errcode);
0564         kfree_skb(skb);
0565     }
0566     tty_kref_put(tty);
0567 }
0568 
0569 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
0570 /* -------- function called by lower level -------------------------- */
0571 
0572 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
0573 {
0574     struct capidev *cdev = ap->private;
0575 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
0576     struct capiminor *mp;
0577     u16 datahandle;
0578     struct capincci *np;
0579 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
0580 
0581     mutex_lock(&cdev->lock);
0582 
0583     if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
0584         u16 info = CAPIMSG_U16(skb->data, 12); // Info field
0585         if ((info & 0xff00) == 0)
0586             capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
0587     }
0588     if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
0589         capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
0590 
0591     if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
0592         skb_queue_tail(&cdev->recvqueue, skb);
0593         wake_up_interruptible(&cdev->recvwait);
0594         goto unlock_out;
0595     }
0596 
0597 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
0598     skb_queue_tail(&cdev->recvqueue, skb);
0599     wake_up_interruptible(&cdev->recvwait);
0600 
0601 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
0602 
0603     np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
0604     if (!np) {
0605         printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
0606         skb_queue_tail(&cdev->recvqueue, skb);
0607         wake_up_interruptible(&cdev->recvwait);
0608         goto unlock_out;
0609     }
0610 
0611     mp = np->minorp;
0612     if (!mp) {
0613         skb_queue_tail(&cdev->recvqueue, skb);
0614         wake_up_interruptible(&cdev->recvwait);
0615         goto unlock_out;
0616     }
0617     if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
0618         datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
0619         pr_debug("capi_signal: DATA_B3_IND %u len=%d\n",
0620              datahandle, skb->len-CAPIMSG_LEN(skb->data));
0621         skb_queue_tail(&mp->inqueue, skb);
0622 
0623         handle_minor_recv(mp);
0624 
0625     } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
0626 
0627         datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
0628         pr_debug("capi_signal: DATA_B3_CONF %u 0x%x\n",
0629              datahandle,
0630              CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 2));
0631         kfree_skb(skb);
0632         capiminor_del_ack(mp, datahandle);
0633         tty_port_tty_wakeup(&mp->port);
0634         handle_minor_send(mp);
0635 
0636     } else {
0637         /* ups, let capi application handle it :-) */
0638         skb_queue_tail(&cdev->recvqueue, skb);
0639         wake_up_interruptible(&cdev->recvwait);
0640     }
0641 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
0642 
0643 unlock_out:
0644     mutex_unlock(&cdev->lock);
0645 }
0646 
0647 /* -------- file_operations for capidev ----------------------------- */
0648 
0649 static ssize_t
0650 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
0651 {
0652     struct capidev *cdev = file->private_data;
0653     struct sk_buff *skb;
0654     size_t copied;
0655     int err;
0656 
0657     if (!cdev->ap.applid)
0658         return -ENODEV;
0659 
0660     skb = skb_dequeue(&cdev->recvqueue);
0661     if (!skb) {
0662         if (file->f_flags & O_NONBLOCK)
0663             return -EAGAIN;
0664         err = wait_event_interruptible(cdev->recvwait,
0665                            (skb = skb_dequeue(&cdev->recvqueue)));
0666         if (err)
0667             return err;
0668     }
0669     if (skb->len > count) {
0670         skb_queue_head(&cdev->recvqueue, skb);
0671         return -EMSGSIZE;
0672     }
0673     if (copy_to_user(buf, skb->data, skb->len)) {
0674         skb_queue_head(&cdev->recvqueue, skb);
0675         return -EFAULT;
0676     }
0677     copied = skb->len;
0678 
0679     kfree_skb(skb);
0680 
0681     return copied;
0682 }
0683 
0684 static ssize_t
0685 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
0686 {
0687     struct capidev *cdev = file->private_data;
0688     struct sk_buff *skb;
0689     u16 mlen;
0690 
0691     if (!cdev->ap.applid)
0692         return -ENODEV;
0693 
0694     if (count < CAPIMSG_BASELEN)
0695         return -EINVAL;
0696 
0697     skb = alloc_skb(count, GFP_USER);
0698     if (!skb)
0699         return -ENOMEM;
0700 
0701     if (copy_from_user(skb_put(skb, count), buf, count)) {
0702         kfree_skb(skb);
0703         return -EFAULT;
0704     }
0705     mlen = CAPIMSG_LEN(skb->data);
0706     if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
0707         if (count < CAPI_DATA_B3_REQ_LEN ||
0708             (size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
0709             kfree_skb(skb);
0710             return -EINVAL;
0711         }
0712     } else {
0713         if (mlen != count) {
0714             kfree_skb(skb);
0715             return -EINVAL;
0716         }
0717     }
0718     CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
0719 
0720     if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
0721         if (count < CAPI_DISCONNECT_B3_RESP_LEN) {
0722             kfree_skb(skb);
0723             return -EINVAL;
0724         }
0725         mutex_lock(&cdev->lock);
0726         capincci_free(cdev, CAPIMSG_NCCI(skb->data));
0727         mutex_unlock(&cdev->lock);
0728     }
0729 
0730     cdev->errcode = capi20_put_message(&cdev->ap, skb);
0731 
0732     if (cdev->errcode) {
0733         kfree_skb(skb);
0734         return -EIO;
0735     }
0736     return count;
0737 }
0738 
0739 static __poll_t
0740 capi_poll(struct file *file, poll_table *wait)
0741 {
0742     struct capidev *cdev = file->private_data;
0743     __poll_t mask = 0;
0744 
0745     if (!cdev->ap.applid)
0746         return EPOLLERR;
0747 
0748     poll_wait(file, &(cdev->recvwait), wait);
0749     mask = EPOLLOUT | EPOLLWRNORM;
0750     if (!skb_queue_empty_lockless(&cdev->recvqueue))
0751         mask |= EPOLLIN | EPOLLRDNORM;
0752     return mask;
0753 }
0754 
0755 static int
0756 capi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0757 {
0758     struct capidev *cdev = file->private_data;
0759     capi_ioctl_struct data;
0760     int retval = -EINVAL;
0761     void __user *argp = (void __user *)arg;
0762 
0763     switch (cmd) {
0764     case CAPI_REGISTER:
0765         mutex_lock(&cdev->lock);
0766 
0767         if (cdev->ap.applid) {
0768             retval = -EEXIST;
0769             goto register_out;
0770         }
0771         if (copy_from_user(&cdev->ap.rparam, argp,
0772                    sizeof(struct capi_register_params))) {
0773             retval = -EFAULT;
0774             goto register_out;
0775         }
0776         cdev->ap.private = cdev;
0777         cdev->ap.recv_message = capi_recv_message;
0778         cdev->errcode = capi20_register(&cdev->ap);
0779         retval = (int)cdev->ap.applid;
0780         if (cdev->errcode) {
0781             cdev->ap.applid = 0;
0782             retval = -EIO;
0783         }
0784 
0785 register_out:
0786         mutex_unlock(&cdev->lock);
0787         return retval;
0788 
0789     case CAPI_GET_VERSION:
0790         if (copy_from_user(&data.contr, argp,
0791                    sizeof(data.contr)))
0792             return -EFAULT;
0793         cdev->errcode = capi20_get_version(data.contr, &data.version);
0794         if (cdev->errcode)
0795             return -EIO;
0796         if (copy_to_user(argp, &data.version,
0797                  sizeof(data.version)))
0798             return -EFAULT;
0799         return 0;
0800 
0801     case CAPI_GET_SERIAL:
0802         if (copy_from_user(&data.contr, argp,
0803                    sizeof(data.contr)))
0804             return -EFAULT;
0805         cdev->errcode = capi20_get_serial(data.contr, data.serial);
0806         if (cdev->errcode)
0807             return -EIO;
0808         if (copy_to_user(argp, data.serial,
0809                  sizeof(data.serial)))
0810             return -EFAULT;
0811         return 0;
0812 
0813     case CAPI_GET_PROFILE:
0814         if (copy_from_user(&data.contr, argp,
0815                    sizeof(data.contr)))
0816             return -EFAULT;
0817 
0818         if (data.contr == 0) {
0819             cdev->errcode = capi20_get_profile(data.contr, &data.profile);
0820             if (cdev->errcode)
0821                 return -EIO;
0822 
0823             retval = copy_to_user(argp,
0824                           &data.profile.ncontroller,
0825                           sizeof(data.profile.ncontroller));
0826 
0827         } else {
0828             cdev->errcode = capi20_get_profile(data.contr, &data.profile);
0829             if (cdev->errcode)
0830                 return -EIO;
0831 
0832             retval = copy_to_user(argp, &data.profile,
0833                           sizeof(data.profile));
0834         }
0835         if (retval)
0836             return -EFAULT;
0837         return 0;
0838 
0839     case CAPI_GET_MANUFACTURER:
0840         if (copy_from_user(&data.contr, argp,
0841                    sizeof(data.contr)))
0842             return -EFAULT;
0843         cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
0844         if (cdev->errcode)
0845             return -EIO;
0846 
0847         if (copy_to_user(argp, data.manufacturer,
0848                  sizeof(data.manufacturer)))
0849             return -EFAULT;
0850 
0851         return 0;
0852 
0853     case CAPI_GET_ERRCODE:
0854         data.errcode = cdev->errcode;
0855         cdev->errcode = CAPI_NOERROR;
0856         if (arg) {
0857             if (copy_to_user(argp, &data.errcode,
0858                      sizeof(data.errcode)))
0859                 return -EFAULT;
0860         }
0861         return data.errcode;
0862 
0863     case CAPI_INSTALLED:
0864         if (capi20_isinstalled() == CAPI_NOERROR)
0865             return 0;
0866         return -ENXIO;
0867 
0868     case CAPI_MANUFACTURER_CMD: {
0869         struct capi_manufacturer_cmd mcmd;
0870         if (!capable(CAP_SYS_ADMIN))
0871             return -EPERM;
0872         if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
0873             return -EFAULT;
0874         return capi20_manufacturer(mcmd.cmd, mcmd.data);
0875     }
0876     case CAPI_SET_FLAGS:
0877     case CAPI_CLR_FLAGS: {
0878         unsigned userflags;
0879 
0880         if (copy_from_user(&userflags, argp, sizeof(userflags)))
0881             return -EFAULT;
0882 
0883         mutex_lock(&cdev->lock);
0884         if (cmd == CAPI_SET_FLAGS)
0885             cdev->userflags |= userflags;
0886         else
0887             cdev->userflags &= ~userflags;
0888         mutex_unlock(&cdev->lock);
0889         return 0;
0890     }
0891     case CAPI_GET_FLAGS:
0892         if (copy_to_user(argp, &cdev->userflags,
0893                  sizeof(cdev->userflags)))
0894             return -EFAULT;
0895         return 0;
0896 
0897 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
0898     case CAPI_NCCI_OPENCOUNT:
0899         return 0;
0900 
0901 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
0902     case CAPI_NCCI_OPENCOUNT: {
0903         struct capincci *nccip;
0904         unsigned ncci;
0905         int count = 0;
0906 
0907         if (copy_from_user(&ncci, argp, sizeof(ncci)))
0908             return -EFAULT;
0909 
0910         mutex_lock(&cdev->lock);
0911         nccip = capincci_find(cdev, (u32)ncci);
0912         if (nccip)
0913             count = capincci_minor_opencount(nccip);
0914         mutex_unlock(&cdev->lock);
0915         return count;
0916     }
0917 
0918     case CAPI_NCCI_GETUNIT: {
0919         struct capincci *nccip;
0920         struct capiminor *mp;
0921         unsigned ncci;
0922         int unit = -ESRCH;
0923 
0924         if (copy_from_user(&ncci, argp, sizeof(ncci)))
0925             return -EFAULT;
0926 
0927         mutex_lock(&cdev->lock);
0928         nccip = capincci_find(cdev, (u32)ncci);
0929         if (nccip) {
0930             mp = nccip->minorp;
0931             if (mp)
0932                 unit = mp->minor;
0933         }
0934         mutex_unlock(&cdev->lock);
0935         return unit;
0936     }
0937 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
0938 
0939     default:
0940         return -EINVAL;
0941     }
0942 }
0943 
0944 static long
0945 capi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0946 {
0947     int ret;
0948 
0949     mutex_lock(&capi_mutex);
0950     ret = capi_ioctl(file, cmd, arg);
0951     mutex_unlock(&capi_mutex);
0952 
0953     return ret;
0954 }
0955 
0956 #ifdef CONFIG_COMPAT
0957 static long
0958 capi_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0959 {
0960     int ret;
0961 
0962     if (cmd == CAPI_MANUFACTURER_CMD) {
0963         struct {
0964             compat_ulong_t cmd;
0965             compat_uptr_t data;
0966         } mcmd32;
0967 
0968         if (!capable(CAP_SYS_ADMIN))
0969             return -EPERM;
0970         if (copy_from_user(&mcmd32, compat_ptr(arg), sizeof(mcmd32)))
0971             return -EFAULT;
0972 
0973         mutex_lock(&capi_mutex);
0974         ret = capi20_manufacturer(mcmd32.cmd, compat_ptr(mcmd32.data));
0975         mutex_unlock(&capi_mutex);
0976 
0977         return ret;
0978     }
0979 
0980     return capi_unlocked_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
0981 }
0982 #endif
0983 
0984 static int capi_open(struct inode *inode, struct file *file)
0985 {
0986     struct capidev *cdev;
0987 
0988     cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
0989     if (!cdev)
0990         return -ENOMEM;
0991 
0992     mutex_init(&cdev->lock);
0993     skb_queue_head_init(&cdev->recvqueue);
0994     init_waitqueue_head(&cdev->recvwait);
0995     INIT_LIST_HEAD(&cdev->nccis);
0996     file->private_data = cdev;
0997 
0998     mutex_lock(&capidev_list_lock);
0999     list_add_tail(&cdev->list, &capidev_list);
1000     mutex_unlock(&capidev_list_lock);
1001 
1002     return stream_open(inode, file);
1003 }
1004 
1005 static int capi_release(struct inode *inode, struct file *file)
1006 {
1007     struct capidev *cdev = file->private_data;
1008 
1009     mutex_lock(&capidev_list_lock);
1010     list_del(&cdev->list);
1011     mutex_unlock(&capidev_list_lock);
1012 
1013     if (cdev->ap.applid)
1014         capi20_release(&cdev->ap);
1015     skb_queue_purge(&cdev->recvqueue);
1016     capincci_free(cdev, 0xffffffff);
1017 
1018     kfree(cdev);
1019     return 0;
1020 }
1021 
1022 static const struct file_operations capi_fops =
1023 {
1024     .owner      = THIS_MODULE,
1025     .llseek     = no_llseek,
1026     .read       = capi_read,
1027     .write      = capi_write,
1028     .poll       = capi_poll,
1029     .unlocked_ioctl = capi_unlocked_ioctl,
1030 #ifdef CONFIG_COMPAT
1031     .compat_ioctl   = capi_compat_ioctl,
1032 #endif
1033     .open       = capi_open,
1034     .release    = capi_release,
1035 };
1036 
1037 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1038 /* -------- tty_operations for capincci ----------------------------- */
1039 
1040 static int
1041 capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1042 {
1043     struct capiminor *mp = capiminor_get(tty->index);
1044     int ret = tty_standard_install(driver, tty);
1045 
1046     if (ret == 0)
1047         tty->driver_data = mp;
1048     else
1049         capiminor_put(mp);
1050     return ret;
1051 }
1052 
1053 static void capinc_tty_cleanup(struct tty_struct *tty)
1054 {
1055     struct capiminor *mp = tty->driver_data;
1056     tty->driver_data = NULL;
1057     capiminor_put(mp);
1058 }
1059 
1060 static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
1061 {
1062     struct capiminor *mp = tty->driver_data;
1063     int err;
1064 
1065     err = tty_port_open(&mp->port, tty, filp);
1066     if (err)
1067         return err;
1068 
1069     handle_minor_recv(mp);
1070     return 0;
1071 }
1072 
1073 static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
1074 {
1075     struct capiminor *mp = tty->driver_data;
1076 
1077     tty_port_close(&mp->port, tty, filp);
1078 }
1079 
1080 static int capinc_tty_write(struct tty_struct *tty,
1081                 const unsigned char *buf, int count)
1082 {
1083     struct capiminor *mp = tty->driver_data;
1084     struct sk_buff *skb;
1085 
1086     pr_debug("capinc_tty_write(count=%d)\n", count);
1087 
1088     spin_lock_bh(&mp->outlock);
1089     skb = mp->outskb;
1090     if (skb) {
1091         mp->outskb = NULL;
1092         __skb_queue_tail(&mp->outqueue, skb);
1093         mp->outbytes += skb->len;
1094     }
1095 
1096     skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + count, GFP_ATOMIC);
1097     if (!skb) {
1098         printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1099         spin_unlock_bh(&mp->outlock);
1100         return -ENOMEM;
1101     }
1102 
1103     skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1104     skb_put_data(skb, buf, count);
1105 
1106     __skb_queue_tail(&mp->outqueue, skb);
1107     mp->outbytes += skb->len;
1108     spin_unlock_bh(&mp->outlock);
1109 
1110     handle_minor_send(mp);
1111 
1112     return count;
1113 }
1114 
1115 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1116 {
1117     struct capiminor *mp = tty->driver_data;
1118     bool invoke_send = false;
1119     struct sk_buff *skb;
1120     int ret = 1;
1121 
1122     pr_debug("capinc_put_char(%u)\n", ch);
1123 
1124     spin_lock_bh(&mp->outlock);
1125     skb = mp->outskb;
1126     if (skb) {
1127         if (skb_tailroom(skb) > 0) {
1128             skb_put_u8(skb, ch);
1129             goto unlock_out;
1130         }
1131         mp->outskb = NULL;
1132         __skb_queue_tail(&mp->outqueue, skb);
1133         mp->outbytes += skb->len;
1134         invoke_send = true;
1135     }
1136 
1137     skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1138     if (skb) {
1139         skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1140         skb_put_u8(skb, ch);
1141         mp->outskb = skb;
1142     } else {
1143         printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1144         ret = 0;
1145     }
1146 
1147 unlock_out:
1148     spin_unlock_bh(&mp->outlock);
1149 
1150     if (invoke_send)
1151         handle_minor_send(mp);
1152 
1153     return ret;
1154 }
1155 
1156 static void capinc_tty_flush_chars(struct tty_struct *tty)
1157 {
1158     struct capiminor *mp = tty->driver_data;
1159     struct sk_buff *skb;
1160 
1161     spin_lock_bh(&mp->outlock);
1162     skb = mp->outskb;
1163     if (skb) {
1164         mp->outskb = NULL;
1165         __skb_queue_tail(&mp->outqueue, skb);
1166         mp->outbytes += skb->len;
1167         spin_unlock_bh(&mp->outlock);
1168 
1169         handle_minor_send(mp);
1170     } else
1171         spin_unlock_bh(&mp->outlock);
1172 
1173     handle_minor_recv(mp);
1174 }
1175 
1176 static unsigned int capinc_tty_write_room(struct tty_struct *tty)
1177 {
1178     struct capiminor *mp = tty->driver_data;
1179     unsigned int room;
1180 
1181     room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1182     room *= CAPI_MAX_BLKSIZE;
1183     pr_debug("capinc_tty_write_room = %u\n", room);
1184     return room;
1185 }
1186 
1187 static unsigned int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1188 {
1189     struct capiminor *mp = tty->driver_data;
1190 
1191     pr_debug("capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1192          mp->outbytes, mp->nack,
1193          skb_queue_len(&mp->outqueue),
1194          skb_queue_len(&mp->inqueue));
1195     return mp->outbytes;
1196 }
1197 
1198 static void capinc_tty_throttle(struct tty_struct *tty)
1199 {
1200     struct capiminor *mp = tty->driver_data;
1201     mp->ttyinstop = 1;
1202 }
1203 
1204 static void capinc_tty_unthrottle(struct tty_struct *tty)
1205 {
1206     struct capiminor *mp = tty->driver_data;
1207 
1208     mp->ttyinstop = 0;
1209     handle_minor_recv(mp);
1210 }
1211 
1212 static void capinc_tty_stop(struct tty_struct *tty)
1213 {
1214     struct capiminor *mp = tty->driver_data;
1215 
1216     mp->ttyoutstop = 1;
1217 }
1218 
1219 static void capinc_tty_start(struct tty_struct *tty)
1220 {
1221     struct capiminor *mp = tty->driver_data;
1222 
1223     mp->ttyoutstop = 0;
1224     handle_minor_send(mp);
1225 }
1226 
1227 static void capinc_tty_hangup(struct tty_struct *tty)
1228 {
1229     struct capiminor *mp = tty->driver_data;
1230 
1231     tty_port_hangup(&mp->port);
1232 }
1233 
1234 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1235 {
1236     pr_debug("capinc_tty_send_xchar(%d)\n", ch);
1237 }
1238 
1239 static const struct tty_operations capinc_ops = {
1240     .open = capinc_tty_open,
1241     .close = capinc_tty_close,
1242     .write = capinc_tty_write,
1243     .put_char = capinc_tty_put_char,
1244     .flush_chars = capinc_tty_flush_chars,
1245     .write_room = capinc_tty_write_room,
1246     .chars_in_buffer = capinc_tty_chars_in_buffer,
1247     .throttle = capinc_tty_throttle,
1248     .unthrottle = capinc_tty_unthrottle,
1249     .stop = capinc_tty_stop,
1250     .start = capinc_tty_start,
1251     .hangup = capinc_tty_hangup,
1252     .send_xchar = capinc_tty_send_xchar,
1253     .install = capinc_tty_install,
1254     .cleanup = capinc_tty_cleanup,
1255 };
1256 
1257 static int __init capinc_tty_init(void)
1258 {
1259     struct tty_driver *drv;
1260     int err;
1261 
1262     if (capi_ttyminors > CAPINC_MAX_PORTS)
1263         capi_ttyminors = CAPINC_MAX_PORTS;
1264     if (capi_ttyminors <= 0)
1265         capi_ttyminors = CAPINC_NR_PORTS;
1266 
1267     capiminors = kcalloc(capi_ttyminors, sizeof(struct capiminor *),
1268                  GFP_KERNEL);
1269     if (!capiminors)
1270         return -ENOMEM;
1271 
1272     drv = tty_alloc_driver(capi_ttyminors, TTY_DRIVER_REAL_RAW |
1273             TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_DYNAMIC_DEV);
1274     if (IS_ERR(drv)) {
1275         kfree(capiminors);
1276         return PTR_ERR(drv);
1277     }
1278     drv->driver_name = "capi_nc";
1279     drv->name = "capi!";
1280     drv->major = 0;
1281     drv->minor_start = 0;
1282     drv->type = TTY_DRIVER_TYPE_SERIAL;
1283     drv->subtype = SERIAL_TYPE_NORMAL;
1284     drv->init_termios = tty_std_termios;
1285     drv->init_termios.c_iflag = ICRNL;
1286     drv->init_termios.c_oflag = OPOST | ONLCR;
1287     drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1288     drv->init_termios.c_lflag = 0;
1289     tty_set_operations(drv, &capinc_ops);
1290 
1291     err = tty_register_driver(drv);
1292     if (err) {
1293         tty_driver_kref_put(drv);
1294         kfree(capiminors);
1295         printk(KERN_ERR "Couldn't register capi_nc driver\n");
1296         return err;
1297     }
1298     capinc_tty_driver = drv;
1299     return 0;
1300 }
1301 
1302 static void __exit capinc_tty_exit(void)
1303 {
1304     tty_unregister_driver(capinc_tty_driver);
1305     tty_driver_kref_put(capinc_tty_driver);
1306     kfree(capiminors);
1307 }
1308 
1309 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1310 
1311 static inline int capinc_tty_init(void)
1312 {
1313     return 0;
1314 }
1315 
1316 static inline void capinc_tty_exit(void) { }
1317 
1318 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1319 
1320 /* -------- /proc functions ----------------------------------------- */
1321 
1322 /*
1323  * /proc/capi/capi20:
1324  *  minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1325  */
1326 static int __maybe_unused capi20_proc_show(struct seq_file *m, void *v)
1327 {
1328     struct capidev *cdev;
1329     struct list_head *l;
1330 
1331     mutex_lock(&capidev_list_lock);
1332     list_for_each(l, &capidev_list) {
1333         cdev = list_entry(l, struct capidev, list);
1334         seq_printf(m, "0 %d %lu %lu %lu %lu\n",
1335                cdev->ap.applid,
1336                cdev->ap.nrecvctlpkt,
1337                cdev->ap.nrecvdatapkt,
1338                cdev->ap.nsentctlpkt,
1339                cdev->ap.nsentdatapkt);
1340     }
1341     mutex_unlock(&capidev_list_lock);
1342     return 0;
1343 }
1344 
1345 /*
1346  * /proc/capi/capi20ncci:
1347  *  applid ncci
1348  */
1349 static int __maybe_unused capi20ncci_proc_show(struct seq_file *m, void *v)
1350 {
1351     struct capidev *cdev;
1352     struct capincci *np;
1353 
1354     mutex_lock(&capidev_list_lock);
1355     list_for_each_entry(cdev, &capidev_list, list) {
1356         mutex_lock(&cdev->lock);
1357         list_for_each_entry(np, &cdev->nccis, list)
1358             seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
1359         mutex_unlock(&cdev->lock);
1360     }
1361     mutex_unlock(&capidev_list_lock);
1362     return 0;
1363 }
1364 
1365 static void __init proc_init(void)
1366 {
1367     proc_create_single("capi/capi20", 0, NULL, capi20_proc_show);
1368     proc_create_single("capi/capi20ncci", 0, NULL, capi20ncci_proc_show);
1369 }
1370 
1371 static void __exit proc_exit(void)
1372 {
1373     remove_proc_entry("capi/capi20", NULL);
1374     remove_proc_entry("capi/capi20ncci", NULL);
1375 }
1376 
1377 /* -------- init function and module interface ---------------------- */
1378 
1379 
1380 static int __init capi_init(void)
1381 {
1382     const char *compileinfo;
1383     int major_ret;
1384     int ret;
1385 
1386     ret = kcapi_init();
1387     if (ret)
1388         return ret;
1389 
1390     major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1391     if (major_ret < 0) {
1392         printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1393         kcapi_exit();
1394         return major_ret;
1395     }
1396     capi_class = class_create(THIS_MODULE, "capi");
1397     if (IS_ERR(capi_class)) {
1398         unregister_chrdev(capi_major, "capi20");
1399         kcapi_exit();
1400         return PTR_ERR(capi_class);
1401     }
1402 
1403     device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi20");
1404 
1405     if (capinc_tty_init() < 0) {
1406         device_destroy(capi_class, MKDEV(capi_major, 0));
1407         class_destroy(capi_class);
1408         unregister_chrdev(capi_major, "capi20");
1409         kcapi_exit();
1410         return -ENOMEM;
1411     }
1412 
1413     proc_init();
1414 
1415 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1416     compileinfo = " (middleware)";
1417 #else
1418     compileinfo = " (no middleware)";
1419 #endif
1420     printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1421            capi_major, compileinfo);
1422 
1423     return 0;
1424 }
1425 
1426 static void __exit capi_exit(void)
1427 {
1428     proc_exit();
1429 
1430     device_destroy(capi_class, MKDEV(capi_major, 0));
1431     class_destroy(capi_class);
1432     unregister_chrdev(capi_major, "capi20");
1433 
1434     capinc_tty_exit();
1435 
1436     kcapi_exit();
1437 }
1438 
1439 module_init(capi_init);
1440 module_exit(capi_exit);