1 /* $Id: capi.c,v 1.1.2.7 2004/04/28 09:48:59 armin Exp $
3 * CAPI 2.0 Interface for Linux
5 * Copyright 1996 by Carsten Paeth <calle@calle.de>
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
12 #include <linux/module.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/major.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/fcntl.h>
20 #include <linux/signal.h>
21 #include <linux/mutex.h>
23 #include <linux/smp_lock.h>
24 #include <linux/timer.h>
25 #include <linux/wait.h>
26 #include <linux/tty.h>
27 #include <linux/netdevice.h>
28 #include <linux/ppp_defs.h>
29 #include <linux/if_ppp.h>
30 #include <linux/skbuff.h>
31 #include <linux/proc_fs.h>
32 #include <linux/seq_file.h>
33 #include <linux/poll.h>
34 #include <linux/capi.h>
35 #include <linux/kernelcapi.h>
36 #include <linux/init.h>
37 #include <linux/device.h>
38 #include <linux/moduleparam.h>
39 #include <linux/isdn/capiutil.h>
40 #include <linux/isdn/capicmd.h>
44 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
45 MODULE_AUTHOR("Carsten Paeth");
46 MODULE_LICENSE("GPL");
48 #undef _DEBUG_REFCOUNT /* alloc/free and open/close debug */
49 #undef _DEBUG_TTYFUNCS /* call to tty_driver */
50 #undef _DEBUG_DATAFLOW /* data flow */
52 /* -------- driver information -------------------------------------- */
54 static struct class *capi_class;
55 static int capi_major = 68; /* allocated */
57 module_param_named(major, capi_major, uint, 0);
59 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
60 #define CAPINC_NR_PORTS 32
61 #define CAPINC_MAX_PORTS 256
63 static int capi_ttymajor = 191;
64 static int capi_ttyminors = CAPINC_NR_PORTS;
66 module_param_named(ttymajor, capi_ttymajor, uint, 0);
67 module_param_named(ttyminors, capi_ttyminors, uint, 0);
68 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
70 /* -------- defines ------------------------------------------------- */
72 #define CAPINC_MAX_RECVQUEUE 10
73 #define CAPINC_MAX_SENDQUEUE 10
74 #define CAPI_MAX_BLKSIZE 2048
76 /* -------- data structures ----------------------------------------- */
82 struct datahandle_queue {
83 struct list_head list;
88 struct list_head list;
89 struct capincci *nccip;
91 struct dentry *capifs_dentry;
93 struct capi20_appl *ap;
98 struct tty_struct *tty;
101 struct sk_buff *ttyskb;
102 atomic_t ttyopencount;
104 struct sk_buff_head inqueue;
106 struct sk_buff_head outqueue;
110 struct list_head ackqueue;
115 /* FIXME: The following lock is a sledgehammer-workaround to a
116 * locking issue with the capiminor (and maybe other) data structure(s).
117 * Access to this data is done in a racy way and crashes the machine with
118 * a FritzCard DSL driver; sooner or later. This is a workaround
119 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
120 * The correct (and scalable) fix for the issue seems to require
121 * an API change to the drivers... . */
122 static DEFINE_SPINLOCK(workaround_lock);
125 struct capincci *next;
127 struct capidev *cdev;
128 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
129 struct capiminor *minorp;
130 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
134 struct list_head list;
135 struct capi20_appl ap;
139 struct sk_buff_head recvqueue;
140 wait_queue_head_t recvwait;
142 struct capincci *nccis;
144 struct mutex ncci_list_mtx;
147 /* -------- global variables ---------------------------------------- */
149 static DEFINE_MUTEX(capidev_list_lock);
150 static LIST_HEAD(capidev_list);
152 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
154 static DEFINE_RWLOCK(capiminor_list_lock);
155 static LIST_HEAD(capiminor_list);
157 /* -------- datahandles --------------------------------------------- */
159 static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
161 struct datahandle_queue *n;
164 n = kmalloc(sizeof(*n), GFP_ATOMIC);
166 printk(KERN_ERR "capi: alloc datahandle failed\n");
169 n->datahandle = datahandle;
170 INIT_LIST_HEAD(&n->list);
171 spin_lock_irqsave(&mp->ackqlock, flags);
172 list_add_tail(&n->list, &mp->ackqueue);
174 spin_unlock_irqrestore(&mp->ackqlock, flags);
178 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
180 struct datahandle_queue *p, *tmp;
183 spin_lock_irqsave(&mp->ackqlock, flags);
184 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
185 if (p->datahandle == datahandle) {
189 spin_unlock_irqrestore(&mp->ackqlock, flags);
193 spin_unlock_irqrestore(&mp->ackqlock, flags);
197 static void capiminor_del_all_ack(struct capiminor *mp)
199 struct datahandle_queue *p, *tmp;
202 spin_lock_irqsave(&mp->ackqlock, flags);
203 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
208 spin_unlock_irqrestore(&mp->ackqlock, flags);
212 /* -------- struct capiminor ---------------------------------------- */
214 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
216 struct capiminor *mp, *p;
217 unsigned int minor = 0;
220 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
222 printk(KERN_ERR "capi: can't alloc capiminor\n");
229 atomic_set(&mp->ttyopencount,0);
230 INIT_LIST_HEAD(&mp->ackqueue);
231 spin_lock_init(&mp->ackqlock);
233 skb_queue_head_init(&mp->inqueue);
234 skb_queue_head_init(&mp->outqueue);
236 /* Allocate the least unused minor number.
238 write_lock_irqsave(&capiminor_list_lock, flags);
239 if (list_empty(&capiminor_list))
240 list_add(&mp->list, &capiminor_list);
242 list_for_each_entry(p, &capiminor_list, list) {
243 if (p->minor > minor)
248 if (minor < capi_ttyminors) {
250 list_add(&mp->list, p->list.prev);
253 write_unlock_irqrestore(&capiminor_list_lock, flags);
255 if (!(minor < capi_ttyminors)) {
256 printk(KERN_NOTICE "capi: out of minors\n");
264 static void capiminor_free(struct capiminor *mp)
268 write_lock_irqsave(&capiminor_list_lock, flags);
270 write_unlock_irqrestore(&capiminor_list_lock, flags);
272 kfree_skb(mp->ttyskb);
274 skb_queue_purge(&mp->inqueue);
275 skb_queue_purge(&mp->outqueue);
276 capiminor_del_all_ack(mp);
280 static struct capiminor *capiminor_find(unsigned int minor)
283 struct capiminor *p = NULL;
285 read_lock(&capiminor_list_lock);
286 list_for_each(l, &capiminor_list) {
287 p = list_entry(l, struct capiminor, list);
288 if (p->minor == minor)
291 read_unlock(&capiminor_list_lock);
292 if (l == &capiminor_list)
298 /* -------- struct capincci ----------------------------------------- */
300 static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
302 struct capiminor *mp;
304 if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
307 mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
310 #ifdef _DEBUG_REFCOUNT
311 printk(KERN_DEBUG "set mp->nccip\n");
314 capifs_new_ncci(mp->minor,
315 MKDEV(capi_ttymajor, mp->minor));
319 static void capincci_free_minor(struct capincci *np)
321 struct capiminor *mp = np->minorp;
324 capifs_free_ncci(mp->capifs_dentry);
327 #ifdef _DEBUG_REFCOUNT
328 printk(KERN_DEBUG "reset mp->nccip\n");
337 static inline unsigned int capincci_minor_opencount(struct capincci *np)
339 struct capiminor *mp = np->minorp;
341 return mp ? atomic_read(&mp->ttyopencount) : 0;
344 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
347 capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
348 static inline void capincci_free_minor(struct capincci *np) { }
350 static inline unsigned int capincci_minor_opencount(struct capincci *np)
355 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
357 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
359 struct capincci *np, **pp;
361 np = kzalloc(sizeof(*np), GFP_ATOMIC);
367 capincci_alloc_minor(cdev, np);
369 for (pp=&cdev->nccis; *pp; pp = &(*pp)->next)
375 static void capincci_free(struct capidev *cdev, u32 ncci)
377 struct capincci *np, **pp;
382 if (ncci == 0xffffffff || np->ncci == ncci) {
384 capincci_free_minor(np);
386 if (*pp == NULL) return;
393 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
397 for (p=cdev->nccis; p ; p = p->next) {
404 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
405 /* -------- handle data queue --------------------------------------- */
407 static struct sk_buff *
408 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
410 struct sk_buff *nskb;
411 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
413 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
414 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
415 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
416 capimsg_setu16(s, 2, mp->ap->applid);
417 capimsg_setu8 (s, 4, CAPI_DATA_B3);
418 capimsg_setu8 (s, 5, CAPI_RESP);
419 capimsg_setu16(s, 6, mp->msgid++);
420 capimsg_setu32(s, 8, mp->ncci);
421 capimsg_setu16(s, 12, datahandle);
426 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
428 struct sk_buff *nskb;
430 u16 errcode, datahandle;
431 struct tty_ldisc *ld;
433 datalen = skb->len - CAPIMSG_LEN(skb->data);
436 #ifdef _DEBUG_DATAFLOW
437 printk(KERN_DEBUG "capi: currently no receiver\n");
442 ld = tty_ldisc_ref(mp->tty);
445 if (ld->ops->receive_buf == NULL) {
446 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
447 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
452 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
453 printk(KERN_DEBUG "capi: recv tty throttled\n");
457 if (mp->tty->receive_room < datalen) {
458 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
459 printk(KERN_DEBUG "capi: no room in tty\n");
463 if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
464 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
467 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
468 errcode = capi20_put_message(mp->ap, nskb);
469 if (errcode != CAPI_NOERROR) {
470 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
475 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
476 #ifdef _DEBUG_DATAFLOW
477 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
478 datahandle, skb->len);
480 ld->ops->receive_buf(mp->tty, skb->data, NULL, skb->len);
489 static void handle_minor_recv(struct capiminor *mp)
492 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
493 unsigned int len = skb->len;
495 if (handle_recv_skb(mp, skb) < 0) {
496 skb_queue_head(&mp->inqueue, skb);
503 static int handle_minor_send(struct capiminor *mp)
511 if (mp->tty && mp->ttyoutstop) {
512 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
513 printk(KERN_DEBUG "capi: send: tty stopped\n");
518 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
519 datahandle = mp->datahandle;
521 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
522 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
523 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
524 capimsg_setu16(skb->data, 2, mp->ap->applid);
525 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
526 capimsg_setu8 (skb->data, 5, CAPI_REQ);
527 capimsg_setu16(skb->data, 6, mp->msgid++);
528 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
529 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
530 capimsg_setu16(skb->data, 16, len); /* Data length */
531 capimsg_setu16(skb->data, 18, datahandle);
532 capimsg_setu16(skb->data, 20, 0); /* Flags */
534 if (capiminor_add_ack(mp, datahandle) < 0) {
535 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
536 skb_queue_head(&mp->outqueue, skb);
539 errcode = capi20_put_message(mp->ap, skb);
540 if (errcode == CAPI_NOERROR) {
544 #ifdef _DEBUG_DATAFLOW
545 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
550 capiminor_del_ack(mp, datahandle);
552 if (errcode == CAPI_SENDQUEUEFULL) {
553 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
554 skb_queue_head(&mp->outqueue, skb);
558 /* ups, drop packet */
559 printk(KERN_ERR "capi: put_message = %x\n", errcode);
566 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
567 /* -------- function called by lower level -------------------------- */
569 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
571 struct capidev *cdev = ap->private;
572 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
573 struct capiminor *mp;
575 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
580 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
581 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
582 if ((info & 0xff00) == 0) {
583 mutex_lock(&cdev->ncci_list_mtx);
584 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
585 mutex_unlock(&cdev->ncci_list_mtx);
588 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND) {
589 mutex_lock(&cdev->ncci_list_mtx);
590 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
591 mutex_unlock(&cdev->ncci_list_mtx);
593 spin_lock_irqsave(&workaround_lock, flags);
594 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
595 skb_queue_tail(&cdev->recvqueue, skb);
596 wake_up_interruptible(&cdev->recvwait);
597 spin_unlock_irqrestore(&workaround_lock, flags);
600 ncci = CAPIMSG_CONTROL(skb->data);
601 for (np = cdev->nccis; np && np->ncci != ncci; np = np->next)
604 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
605 skb_queue_tail(&cdev->recvqueue, skb);
606 wake_up_interruptible(&cdev->recvwait);
607 spin_unlock_irqrestore(&workaround_lock, flags);
611 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
612 skb_queue_tail(&cdev->recvqueue, skb);
613 wake_up_interruptible(&cdev->recvwait);
615 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
619 skb_queue_tail(&cdev->recvqueue, skb);
620 wake_up_interruptible(&cdev->recvwait);
621 spin_unlock_irqrestore(&workaround_lock, flags);
624 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
625 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
626 #ifdef _DEBUG_DATAFLOW
627 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
628 datahandle, skb->len-CAPIMSG_LEN(skb->data));
630 skb_queue_tail(&mp->inqueue, skb);
631 mp->inbytes += skb->len;
632 handle_minor_recv(mp);
634 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
636 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
637 #ifdef _DEBUG_DATAFLOW
638 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
640 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
643 (void)capiminor_del_ack(mp, datahandle);
646 (void)handle_minor_send(mp);
649 /* ups, let capi application handle it :-) */
650 skb_queue_tail(&cdev->recvqueue, skb);
651 wake_up_interruptible(&cdev->recvwait);
653 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
655 spin_unlock_irqrestore(&workaround_lock, flags);
658 /* -------- file_operations for capidev ----------------------------- */
661 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
663 struct capidev *cdev = (struct capidev *)file->private_data;
667 if (!cdev->ap.applid)
670 if ((skb = skb_dequeue(&cdev->recvqueue)) == NULL) {
672 if (file->f_flags & O_NONBLOCK)
676 interruptible_sleep_on(&cdev->recvwait);
677 if ((skb = skb_dequeue(&cdev->recvqueue)) != NULL)
679 if (signal_pending(current))
683 return -ERESTARTNOHAND;
685 if (skb->len > count) {
686 skb_queue_head(&cdev->recvqueue, skb);
689 if (copy_to_user(buf, skb->data, skb->len)) {
690 skb_queue_head(&cdev->recvqueue, skb);
701 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
703 struct capidev *cdev = (struct capidev *)file->private_data;
707 if (!cdev->ap.applid)
710 skb = alloc_skb(count, GFP_USER);
714 if (copy_from_user(skb_put(skb, count), buf, count)) {
718 mlen = CAPIMSG_LEN(skb->data);
719 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
720 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
730 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
732 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
733 mutex_lock(&cdev->ncci_list_mtx);
734 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
735 mutex_unlock(&cdev->ncci_list_mtx);
738 cdev->errcode = capi20_put_message(&cdev->ap, skb);
748 capi_poll(struct file *file, poll_table * wait)
750 struct capidev *cdev = (struct capidev *)file->private_data;
751 unsigned int mask = 0;
753 if (!cdev->ap.applid)
756 poll_wait(file, &(cdev->recvwait), wait);
757 mask = POLLOUT | POLLWRNORM;
758 if (!skb_queue_empty(&cdev->recvqueue))
759 mask |= POLLIN | POLLRDNORM;
764 capi_ioctl(struct inode *inode, struct file *file,
765 unsigned int cmd, unsigned long arg)
767 struct capidev *cdev = file->private_data;
768 struct capi20_appl *ap = &cdev->ap;
769 capi_ioctl_struct data;
770 int retval = -EINVAL;
771 void __user *argp = (void __user *)arg;
779 if (copy_from_user(&cdev->ap.rparam, argp,
780 sizeof(struct capi_register_params)))
783 cdev->ap.private = cdev;
784 cdev->ap.recv_message = capi_recv_message;
785 cdev->errcode = capi20_register(ap);
791 return (int)ap->applid;
793 case CAPI_GET_VERSION:
795 if (copy_from_user(&data.contr, argp,
798 cdev->errcode = capi20_get_version(data.contr, &data.version);
801 if (copy_to_user(argp, &data.version,
802 sizeof(data.version)))
807 case CAPI_GET_SERIAL:
809 if (copy_from_user(&data.contr, argp,
812 cdev->errcode = capi20_get_serial (data.contr, data.serial);
815 if (copy_to_user(argp, data.serial,
816 sizeof(data.serial)))
820 case CAPI_GET_PROFILE:
822 if (copy_from_user(&data.contr, argp,
826 if (data.contr == 0) {
827 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
831 retval = copy_to_user(argp,
832 &data.profile.ncontroller,
833 sizeof(data.profile.ncontroller));
836 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
840 retval = copy_to_user(argp, &data.profile,
841 sizeof(data.profile));
848 case CAPI_GET_MANUFACTURER:
850 if (copy_from_user(&data.contr, argp,
853 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
857 if (copy_to_user(argp, data.manufacturer,
858 sizeof(data.manufacturer)))
863 case CAPI_GET_ERRCODE:
864 data.errcode = cdev->errcode;
865 cdev->errcode = CAPI_NOERROR;
867 if (copy_to_user(argp, &data.errcode,
868 sizeof(data.errcode)))
874 if (capi20_isinstalled() == CAPI_NOERROR)
878 case CAPI_MANUFACTURER_CMD:
880 struct capi_manufacturer_cmd mcmd;
881 if (!capable(CAP_SYS_ADMIN))
883 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
885 return capi20_manufacturer(mcmd.cmd, mcmd.data);
893 if (copy_from_user(&userflags, argp,
896 if (cmd == CAPI_SET_FLAGS)
897 cdev->userflags |= userflags;
899 cdev->userflags &= ~userflags;
904 if (copy_to_user(argp, &cdev->userflags,
905 sizeof(cdev->userflags)))
909 case CAPI_NCCI_OPENCOUNT:
911 struct capincci *nccip;
914 if (copy_from_user(&ncci, argp, sizeof(ncci)))
917 mutex_lock(&cdev->ncci_list_mtx);
918 if ((nccip = capincci_find(cdev, (u32) ncci)) == NULL) {
919 mutex_unlock(&cdev->ncci_list_mtx);
922 count += capincci_minor_opencount(nccip);
923 mutex_unlock(&cdev->ncci_list_mtx);
928 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
929 case CAPI_NCCI_GETUNIT:
931 struct capincci *nccip;
932 struct capiminor *mp;
935 if (copy_from_user(&ncci, argp,
938 mutex_lock(&cdev->ncci_list_mtx);
939 nccip = capincci_find(cdev, (u32) ncci);
940 if (!nccip || (mp = nccip->minorp) == NULL) {
941 mutex_unlock(&cdev->ncci_list_mtx);
945 mutex_unlock(&cdev->ncci_list_mtx);
949 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
954 static int capi_open(struct inode *inode, struct file *file)
956 struct capidev *cdev;
958 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
962 mutex_init(&cdev->ncci_list_mtx);
963 skb_queue_head_init(&cdev->recvqueue);
964 init_waitqueue_head(&cdev->recvwait);
965 file->private_data = cdev;
967 mutex_lock(&capidev_list_lock);
968 list_add_tail(&cdev->list, &capidev_list);
969 mutex_unlock(&capidev_list_lock);
971 return nonseekable_open(inode, file);
974 static int capi_release(struct inode *inode, struct file *file)
976 struct capidev *cdev = file->private_data;
978 mutex_lock(&capidev_list_lock);
979 list_del(&cdev->list);
980 mutex_unlock(&capidev_list_lock);
982 if (cdev->ap.applid) {
983 capi20_release(&cdev->ap);
986 skb_queue_purge(&cdev->recvqueue);
988 mutex_lock(&cdev->ncci_list_mtx);
989 capincci_free(cdev, 0xffffffff);
990 mutex_unlock(&cdev->ncci_list_mtx);
996 static const struct file_operations capi_fops =
998 .owner = THIS_MODULE,
1001 .write = capi_write,
1003 .ioctl = capi_ioctl,
1005 .release = capi_release,
1008 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1009 /* -------- tty_operations for capincci ----------------------------- */
1011 static int capinc_tty_open(struct tty_struct * tty, struct file * file)
1013 struct capiminor *mp;
1014 unsigned long flags;
1016 if ((mp = capiminor_find(iminor(file->f_path.dentry->d_inode))) == NULL)
1018 if (mp->nccip == NULL)
1021 tty->driver_data = (void *)mp;
1023 spin_lock_irqsave(&workaround_lock, flags);
1024 if (atomic_read(&mp->ttyopencount) == 0)
1026 atomic_inc(&mp->ttyopencount);
1027 #ifdef _DEBUG_REFCOUNT
1028 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1030 handle_minor_recv(mp);
1031 spin_unlock_irqrestore(&workaround_lock, flags);
1035 static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1037 struct capiminor *mp;
1039 mp = (struct capiminor *)tty->driver_data;
1041 if (atomic_dec_and_test(&mp->ttyopencount)) {
1042 #ifdef _DEBUG_REFCOUNT
1043 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1045 tty->driver_data = NULL;
1048 #ifdef _DEBUG_REFCOUNT
1049 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1051 if (mp->nccip == NULL)
1055 #ifdef _DEBUG_REFCOUNT
1056 printk(KERN_DEBUG "capinc_tty_close\n");
1060 static int capinc_tty_write(struct tty_struct * tty,
1061 const unsigned char *buf, int count)
1063 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1064 struct sk_buff *skb;
1065 unsigned long flags;
1067 #ifdef _DEBUG_TTYFUNCS
1068 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1071 if (!mp || !mp->nccip) {
1072 #ifdef _DEBUG_TTYFUNCS
1073 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1078 spin_lock_irqsave(&workaround_lock, flags);
1082 skb_queue_tail(&mp->outqueue, skb);
1083 mp->outbytes += skb->len;
1086 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1088 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1089 spin_unlock_irqrestore(&workaround_lock, flags);
1093 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1094 memcpy(skb_put(skb, count), buf, count);
1096 skb_queue_tail(&mp->outqueue, skb);
1097 mp->outbytes += skb->len;
1098 (void)handle_minor_send(mp);
1099 (void)handle_minor_recv(mp);
1100 spin_unlock_irqrestore(&workaround_lock, flags);
1104 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1106 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1107 struct sk_buff *skb;
1108 unsigned long flags;
1111 #ifdef _DEBUG_TTYFUNCS
1112 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1115 if (!mp || !mp->nccip) {
1116 #ifdef _DEBUG_TTYFUNCS
1117 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1122 spin_lock_irqsave(&workaround_lock, flags);
1125 if (skb_tailroom(skb) > 0) {
1126 *(skb_put(skb, 1)) = ch;
1127 spin_unlock_irqrestore(&workaround_lock, flags);
1131 skb_queue_tail(&mp->outqueue, skb);
1132 mp->outbytes += skb->len;
1133 (void)handle_minor_send(mp);
1135 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1137 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1138 *(skb_put(skb, 1)) = ch;
1141 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1144 spin_unlock_irqrestore(&workaround_lock, flags);
1148 static void capinc_tty_flush_chars(struct tty_struct *tty)
1150 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1151 struct sk_buff *skb;
1152 unsigned long flags;
1154 #ifdef _DEBUG_TTYFUNCS
1155 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1158 if (!mp || !mp->nccip) {
1159 #ifdef _DEBUG_TTYFUNCS
1160 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1165 spin_lock_irqsave(&workaround_lock, flags);
1169 skb_queue_tail(&mp->outqueue, skb);
1170 mp->outbytes += skb->len;
1171 (void)handle_minor_send(mp);
1173 (void)handle_minor_recv(mp);
1174 spin_unlock_irqrestore(&workaround_lock, flags);
1177 static int capinc_tty_write_room(struct tty_struct *tty)
1179 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1181 if (!mp || !mp->nccip) {
1182 #ifdef _DEBUG_TTYFUNCS
1183 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1187 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1188 room *= CAPI_MAX_BLKSIZE;
1189 #ifdef _DEBUG_TTYFUNCS
1190 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1195 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1197 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1198 if (!mp || !mp->nccip) {
1199 #ifdef _DEBUG_TTYFUNCS
1200 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1204 #ifdef _DEBUG_TTYFUNCS
1205 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1206 mp->outbytes, mp->nack,
1207 skb_queue_len(&mp->outqueue),
1208 skb_queue_len(&mp->inqueue));
1210 return mp->outbytes;
1213 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1214 unsigned int cmd, unsigned long arg)
1219 error = n_tty_ioctl_helper(tty, file, cmd, arg);
1225 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
1227 #ifdef _DEBUG_TTYFUNCS
1228 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1232 static void capinc_tty_throttle(struct tty_struct * tty)
1234 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1235 #ifdef _DEBUG_TTYFUNCS
1236 printk(KERN_DEBUG "capinc_tty_throttle\n");
1242 static void capinc_tty_unthrottle(struct tty_struct * tty)
1244 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1245 unsigned long flags;
1246 #ifdef _DEBUG_TTYFUNCS
1247 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1250 spin_lock_irqsave(&workaround_lock, flags);
1252 handle_minor_recv(mp);
1253 spin_unlock_irqrestore(&workaround_lock, flags);
1257 static void capinc_tty_stop(struct tty_struct *tty)
1259 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1260 #ifdef _DEBUG_TTYFUNCS
1261 printk(KERN_DEBUG "capinc_tty_stop\n");
1268 static void capinc_tty_start(struct tty_struct *tty)
1270 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1271 unsigned long flags;
1272 #ifdef _DEBUG_TTYFUNCS
1273 printk(KERN_DEBUG "capinc_tty_start\n");
1276 spin_lock_irqsave(&workaround_lock, flags);
1278 (void)handle_minor_send(mp);
1279 spin_unlock_irqrestore(&workaround_lock, flags);
1283 static void capinc_tty_hangup(struct tty_struct *tty)
1285 #ifdef _DEBUG_TTYFUNCS
1286 printk(KERN_DEBUG "capinc_tty_hangup\n");
1290 static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1292 #ifdef _DEBUG_TTYFUNCS
1293 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1298 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1300 #ifdef _DEBUG_TTYFUNCS
1301 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1305 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1307 #ifdef _DEBUG_TTYFUNCS
1308 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1312 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1314 #ifdef _DEBUG_TTYFUNCS
1315 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1319 static struct tty_driver *capinc_tty_driver;
1321 static const struct tty_operations capinc_ops = {
1322 .open = capinc_tty_open,
1323 .close = capinc_tty_close,
1324 .write = capinc_tty_write,
1325 .put_char = capinc_tty_put_char,
1326 .flush_chars = capinc_tty_flush_chars,
1327 .write_room = capinc_tty_write_room,
1328 .chars_in_buffer = capinc_tty_chars_in_buffer,
1329 .ioctl = capinc_tty_ioctl,
1330 .set_termios = capinc_tty_set_termios,
1331 .throttle = capinc_tty_throttle,
1332 .unthrottle = capinc_tty_unthrottle,
1333 .stop = capinc_tty_stop,
1334 .start = capinc_tty_start,
1335 .hangup = capinc_tty_hangup,
1336 .break_ctl = capinc_tty_break_ctl,
1337 .flush_buffer = capinc_tty_flush_buffer,
1338 .set_ldisc = capinc_tty_set_ldisc,
1339 .send_xchar = capinc_tty_send_xchar,
1342 static int capinc_tty_init(void)
1344 struct tty_driver *drv;
1346 if (capi_ttyminors > CAPINC_MAX_PORTS)
1347 capi_ttyminors = CAPINC_MAX_PORTS;
1348 if (capi_ttyminors <= 0)
1349 capi_ttyminors = CAPINC_NR_PORTS;
1351 drv = alloc_tty_driver(capi_ttyminors);
1355 drv->owner = THIS_MODULE;
1356 drv->driver_name = "capi_nc";
1358 drv->major = capi_ttymajor;
1359 drv->minor_start = 0;
1360 drv->type = TTY_DRIVER_TYPE_SERIAL;
1361 drv->subtype = SERIAL_TYPE_NORMAL;
1362 drv->init_termios = tty_std_termios;
1363 drv->init_termios.c_iflag = ICRNL;
1364 drv->init_termios.c_oflag = OPOST | ONLCR;
1365 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1366 drv->init_termios.c_lflag = 0;
1367 drv->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_RESET_TERMIOS;
1368 tty_set_operations(drv, &capinc_ops);
1369 if (tty_register_driver(drv)) {
1370 put_tty_driver(drv);
1371 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1374 capinc_tty_driver = drv;
1378 static void capinc_tty_exit(void)
1380 struct tty_driver *drv = capinc_tty_driver;
1382 if ((retval = tty_unregister_driver(drv)))
1383 printk(KERN_ERR "capi: failed to unregister capi_nc driver (%d)\n", retval);
1384 put_tty_driver(drv);
1387 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1389 static inline int capinc_tty_init(void)
1394 static inline void capinc_tty_exit(void) { }
1396 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1398 /* -------- /proc functions ----------------------------------------- */
1401 * /proc/capi/capi20:
1402 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1404 static int capi20_proc_show(struct seq_file *m, void *v)
1406 struct capidev *cdev;
1407 struct list_head *l;
1409 mutex_lock(&capidev_list_lock);
1410 list_for_each(l, &capidev_list) {
1411 cdev = list_entry(l, struct capidev, list);
1412 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
1414 cdev->ap.nrecvctlpkt,
1415 cdev->ap.nrecvdatapkt,
1416 cdev->ap.nsentctlpkt,
1417 cdev->ap.nsentdatapkt);
1419 mutex_unlock(&capidev_list_lock);
1423 static int capi20_proc_open(struct inode *inode, struct file *file)
1425 return single_open(file, capi20_proc_show, NULL);
1428 static const struct file_operations capi20_proc_fops = {
1429 .owner = THIS_MODULE,
1430 .open = capi20_proc_open,
1432 .llseek = seq_lseek,
1433 .release = single_release,
1437 * /proc/capi/capi20ncci:
1440 static int capi20ncci_proc_show(struct seq_file *m, void *v)
1442 struct capidev *cdev;
1443 struct capincci *np;
1444 struct list_head *l;
1446 mutex_lock(&capidev_list_lock);
1447 list_for_each(l, &capidev_list) {
1448 cdev = list_entry(l, struct capidev, list);
1449 for (np=cdev->nccis; np; np = np->next) {
1450 seq_printf(m, "%d 0x%x\n",
1455 mutex_unlock(&capidev_list_lock);
1459 static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1461 return single_open(file, capi20ncci_proc_show, NULL);
1464 static const struct file_operations capi20ncci_proc_fops = {
1465 .owner = THIS_MODULE,
1466 .open = capi20ncci_proc_open,
1468 .llseek = seq_lseek,
1469 .release = single_release,
1472 static void __init proc_init(void)
1474 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1475 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
1478 static void __exit proc_exit(void)
1480 remove_proc_entry("capi/capi20", NULL);
1481 remove_proc_entry("capi/capi20ncci", NULL);
1484 /* -------- init function and module interface ---------------------- */
1487 static int __init capi_init(void)
1489 const char *compileinfo;
1492 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1493 if (major_ret < 0) {
1494 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1497 capi_class = class_create(THIS_MODULE, "capi");
1498 if (IS_ERR(capi_class)) {
1499 unregister_chrdev(capi_major, "capi20");
1500 return PTR_ERR(capi_class);
1503 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
1505 if (capinc_tty_init() < 0) {
1506 device_destroy(capi_class, MKDEV(capi_major, 0));
1507 class_destroy(capi_class);
1508 unregister_chrdev(capi_major, "capi20");
1514 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1515 compileinfo = " (middleware+capifs)";
1516 #elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
1517 compileinfo = " (no capifs)";
1519 compileinfo = " (no middleware)";
1521 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1522 capi_major, compileinfo);
1527 static void __exit capi_exit(void)
1531 device_destroy(capi_class, MKDEV(capi_major, 0));
1532 class_destroy(capi_class);
1533 unregister_chrdev(capi_major, "capi20");
1538 module_init(capi_init);
1539 module_exit(capi_exit);