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 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
27 #include <linux/tty.h>
29 #include <linux/netdevice.h>
30 #include <linux/ppp_defs.h>
31 #include <linux/if_ppp.h>
32 #endif /* CONFIG_PPP */
33 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
34 #include <linux/skbuff.h>
35 #include <linux/proc_fs.h>
36 #include <linux/seq_file.h>
37 #include <linux/poll.h>
38 #include <linux/capi.h>
39 #include <linux/kernelcapi.h>
40 #include <linux/init.h>
41 #include <linux/device.h>
42 #include <linux/moduleparam.h>
43 #include <linux/isdn/capiutil.h>
44 #include <linux/isdn/capicmd.h>
45 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
49 static char *revision = "$Revision: 1.1.2.7 $";
51 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
52 MODULE_AUTHOR("Carsten Paeth");
53 MODULE_LICENSE("GPL");
55 #undef _DEBUG_REFCOUNT /* alloc/free and open/close debug */
56 #undef _DEBUG_TTYFUNCS /* call to tty_driver */
57 #undef _DEBUG_DATAFLOW /* data flow */
59 /* -------- driver information -------------------------------------- */
61 static struct class *capi_class;
63 static int capi_major = 68; /* allocated */
64 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
65 #define CAPINC_NR_PORTS 32
66 #define CAPINC_MAX_PORTS 256
67 static int capi_ttymajor = 191;
68 static int capi_ttyminors = CAPINC_NR_PORTS;
69 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
71 module_param_named(major, capi_major, uint, 0);
72 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
73 module_param_named(ttymajor, capi_ttymajor, uint, 0);
74 module_param_named(ttyminors, capi_ttyminors, uint, 0);
75 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
77 /* -------- defines ------------------------------------------------- */
79 #define CAPINC_MAX_RECVQUEUE 10
80 #define CAPINC_MAX_SENDQUEUE 10
81 #define CAPI_MAX_BLKSIZE 2048
83 /* -------- data structures ----------------------------------------- */
87 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
90 struct datahandle_queue {
91 struct list_head list;
96 struct list_head list;
97 struct capincci *nccip;
100 struct capi20_appl *ap;
105 struct tty_struct *tty;
108 struct sk_buff *ttyskb;
109 atomic_t ttyopencount;
111 struct sk_buff_head inqueue;
113 struct sk_buff_head outqueue;
117 struct list_head ackqueue;
121 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
123 /* FIXME: The following lock is a sledgehammer-workaround to a
124 * locking issue with the capiminor (and maybe other) data structure(s).
125 * Access to this data is done in a racy way and crashes the machine with
126 * a FritzCard DSL driver; sooner or later. This is a workaround
127 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
128 * The correct (and scalable) fix for the issue seems to require
129 * an API change to the drivers... . */
130 static DEFINE_SPINLOCK(workaround_lock);
133 struct capincci *next;
135 struct capidev *cdev;
136 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
137 struct capiminor *minorp;
138 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
142 struct list_head list;
143 struct capi20_appl ap;
147 struct sk_buff_head recvqueue;
148 wait_queue_head_t recvwait;
150 struct capincci *nccis;
152 struct mutex ncci_list_mtx;
155 /* -------- global variables ---------------------------------------- */
157 static DEFINE_RWLOCK(capidev_list_lock);
158 static LIST_HEAD(capidev_list);
160 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
161 static DEFINE_RWLOCK(capiminor_list_lock);
162 static LIST_HEAD(capiminor_list);
163 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
165 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
166 /* -------- datahandles --------------------------------------------- */
168 static int capincci_add_ack(struct capiminor *mp, u16 datahandle)
170 struct datahandle_queue *n;
173 n = kmalloc(sizeof(*n), GFP_ATOMIC);
175 printk(KERN_ERR "capi: alloc datahandle failed\n");
178 n->datahandle = datahandle;
179 INIT_LIST_HEAD(&n->list);
180 spin_lock_irqsave(&mp->ackqlock, flags);
181 list_add_tail(&n->list, &mp->ackqueue);
183 spin_unlock_irqrestore(&mp->ackqlock, flags);
187 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
189 struct datahandle_queue *p, *tmp;
192 spin_lock_irqsave(&mp->ackqlock, flags);
193 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
194 if (p->datahandle == datahandle) {
198 spin_unlock_irqrestore(&mp->ackqlock, flags);
202 spin_unlock_irqrestore(&mp->ackqlock, flags);
206 static void capiminor_del_all_ack(struct capiminor *mp)
208 struct datahandle_queue *p, *tmp;
211 spin_lock_irqsave(&mp->ackqlock, flags);
212 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
217 spin_unlock_irqrestore(&mp->ackqlock, flags);
221 /* -------- struct capiminor ---------------------------------------- */
223 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
225 struct capiminor *mp, *p;
226 unsigned int minor = 0;
229 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
231 printk(KERN_ERR "capi: can't alloc capiminor\n");
238 atomic_set(&mp->ttyopencount,0);
239 INIT_LIST_HEAD(&mp->ackqueue);
240 spin_lock_init(&mp->ackqlock);
242 skb_queue_head_init(&mp->inqueue);
243 skb_queue_head_init(&mp->outqueue);
245 /* Allocate the least unused minor number.
247 write_lock_irqsave(&capiminor_list_lock, flags);
248 if (list_empty(&capiminor_list))
249 list_add(&mp->list, &capiminor_list);
251 list_for_each_entry(p, &capiminor_list, list) {
252 if (p->minor > minor)
257 if (minor < capi_ttyminors) {
259 list_add(&mp->list, p->list.prev);
262 write_unlock_irqrestore(&capiminor_list_lock, flags);
264 if (!(minor < capi_ttyminors)) {
265 printk(KERN_NOTICE "capi: out of minors\n");
273 static void capiminor_free(struct capiminor *mp)
277 write_lock_irqsave(&capiminor_list_lock, flags);
279 write_unlock_irqrestore(&capiminor_list_lock, flags);
281 kfree_skb(mp->ttyskb);
283 skb_queue_purge(&mp->inqueue);
284 skb_queue_purge(&mp->outqueue);
285 capiminor_del_all_ack(mp);
289 static struct capiminor *capiminor_find(unsigned int minor)
292 struct capiminor *p = NULL;
294 read_lock(&capiminor_list_lock);
295 list_for_each(l, &capiminor_list) {
296 p = list_entry(l, struct capiminor, list);
297 if (p->minor == minor)
300 read_unlock(&capiminor_list_lock);
301 if (l == &capiminor_list)
306 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
308 /* -------- struct capincci ----------------------------------------- */
310 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
312 struct capincci *np, **pp;
313 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
314 struct capiminor *mp = NULL;
315 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
317 np = kzalloc(sizeof(*np), GFP_ATOMIC);
322 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
324 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
325 mp = np->minorp = capiminor_alloc(&cdev->ap, ncci);
328 #ifdef _DEBUG_REFCOUNT
329 printk(KERN_DEBUG "set mp->nccip\n");
331 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
332 capifs_new_ncci(mp->minor, MKDEV(capi_ttymajor, mp->minor));
335 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
336 for (pp=&cdev->nccis; *pp; pp = &(*pp)->next)
342 static void capincci_free(struct capidev *cdev, u32 ncci)
344 struct capincci *np, **pp;
345 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
346 struct capiminor *mp;
347 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
352 if (ncci == 0xffffffff || np->ncci == ncci) {
354 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
355 if ((mp = np->minorp) != NULL) {
356 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
357 capifs_free_ncci(mp->minor);
361 #ifdef _DEBUG_REFCOUNT
362 printk(KERN_DEBUG "reset mp->nccip\n");
369 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
371 if (*pp == NULL) return;
378 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
382 for (p=cdev->nccis; p ; p = p->next) {
389 /* -------- struct capidev ------------------------------------------ */
391 static struct capidev *capidev_alloc(void)
393 struct capidev *cdev;
396 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
400 mutex_init(&cdev->ncci_list_mtx);
401 skb_queue_head_init(&cdev->recvqueue);
402 init_waitqueue_head(&cdev->recvwait);
403 write_lock_irqsave(&capidev_list_lock, flags);
404 list_add_tail(&cdev->list, &capidev_list);
405 write_unlock_irqrestore(&capidev_list_lock, flags);
409 static void capidev_free(struct capidev *cdev)
413 if (cdev->ap.applid) {
414 capi20_release(&cdev->ap);
417 skb_queue_purge(&cdev->recvqueue);
419 mutex_lock(&cdev->ncci_list_mtx);
420 capincci_free(cdev, 0xffffffff);
421 mutex_unlock(&cdev->ncci_list_mtx);
423 write_lock_irqsave(&capidev_list_lock, flags);
424 list_del(&cdev->list);
425 write_unlock_irqrestore(&capidev_list_lock, flags);
429 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
430 /* -------- handle data queue --------------------------------------- */
432 static struct sk_buff *
433 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
435 struct sk_buff *nskb;
436 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
438 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
439 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
440 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
441 capimsg_setu16(s, 2, mp->ap->applid);
442 capimsg_setu8 (s, 4, CAPI_DATA_B3);
443 capimsg_setu8 (s, 5, CAPI_RESP);
444 capimsg_setu16(s, 6, mp->msgid++);
445 capimsg_setu32(s, 8, mp->ncci);
446 capimsg_setu16(s, 12, datahandle);
451 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
453 struct sk_buff *nskb;
455 u16 errcode, datahandle;
456 struct tty_ldisc *ld;
458 datalen = skb->len - CAPIMSG_LEN(skb->data);
461 #ifdef _DEBUG_DATAFLOW
462 printk(KERN_DEBUG "capi: currently no receiver\n");
467 ld = tty_ldisc_ref(mp->tty);
470 if (ld->ops->receive_buf == NULL) {
471 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
472 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
477 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
478 printk(KERN_DEBUG "capi: recv tty throttled\n");
482 if (mp->tty->receive_room < datalen) {
483 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
484 printk(KERN_DEBUG "capi: no room in tty\n");
488 if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
489 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
492 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
493 errcode = capi20_put_message(mp->ap, nskb);
494 if (errcode != CAPI_NOERROR) {
495 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
500 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
501 #ifdef _DEBUG_DATAFLOW
502 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
503 datahandle, skb->len);
505 ld->ops->receive_buf(mp->tty, skb->data, NULL, skb->len);
514 static void handle_minor_recv(struct capiminor *mp)
517 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
518 unsigned int len = skb->len;
520 if (handle_recv_skb(mp, skb) < 0) {
521 skb_queue_head(&mp->inqueue, skb);
528 static int handle_minor_send(struct capiminor *mp)
536 if (mp->tty && mp->ttyoutstop) {
537 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
538 printk(KERN_DEBUG "capi: send: tty stopped\n");
543 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
544 datahandle = mp->datahandle;
546 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
547 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
548 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
549 capimsg_setu16(skb->data, 2, mp->ap->applid);
550 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
551 capimsg_setu8 (skb->data, 5, CAPI_REQ);
552 capimsg_setu16(skb->data, 6, mp->msgid++);
553 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
554 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
555 capimsg_setu16(skb->data, 16, len); /* Data length */
556 capimsg_setu16(skb->data, 18, datahandle);
557 capimsg_setu16(skb->data, 20, 0); /* Flags */
559 if (capincci_add_ack(mp, datahandle) < 0) {
560 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
561 skb_queue_head(&mp->outqueue, skb);
564 errcode = capi20_put_message(mp->ap, skb);
565 if (errcode == CAPI_NOERROR) {
569 #ifdef _DEBUG_DATAFLOW
570 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
575 capiminor_del_ack(mp, datahandle);
577 if (errcode == CAPI_SENDQUEUEFULL) {
578 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
579 skb_queue_head(&mp->outqueue, skb);
583 /* ups, drop packet */
584 printk(KERN_ERR "capi: put_message = %x\n", errcode);
591 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
592 /* -------- function called by lower level -------------------------- */
594 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
596 struct capidev *cdev = ap->private;
597 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
598 struct capiminor *mp;
600 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
605 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
606 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
607 if ((info & 0xff00) == 0) {
608 mutex_lock(&cdev->ncci_list_mtx);
609 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
610 mutex_unlock(&cdev->ncci_list_mtx);
613 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND) {
614 mutex_lock(&cdev->ncci_list_mtx);
615 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
616 mutex_unlock(&cdev->ncci_list_mtx);
618 spin_lock_irqsave(&workaround_lock, flags);
619 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
620 skb_queue_tail(&cdev->recvqueue, skb);
621 wake_up_interruptible(&cdev->recvwait);
622 spin_unlock_irqrestore(&workaround_lock, flags);
625 ncci = CAPIMSG_CONTROL(skb->data);
626 for (np = cdev->nccis; np && np->ncci != ncci; np = np->next)
629 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
630 skb_queue_tail(&cdev->recvqueue, skb);
631 wake_up_interruptible(&cdev->recvwait);
632 spin_unlock_irqrestore(&workaround_lock, flags);
635 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
636 skb_queue_tail(&cdev->recvqueue, skb);
637 wake_up_interruptible(&cdev->recvwait);
638 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
641 skb_queue_tail(&cdev->recvqueue, skb);
642 wake_up_interruptible(&cdev->recvwait);
643 spin_unlock_irqrestore(&workaround_lock, flags);
648 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
650 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
651 #ifdef _DEBUG_DATAFLOW
652 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
653 datahandle, skb->len-CAPIMSG_LEN(skb->data));
655 skb_queue_tail(&mp->inqueue, skb);
656 mp->inbytes += skb->len;
657 handle_minor_recv(mp);
659 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
661 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
662 #ifdef _DEBUG_DATAFLOW
663 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
665 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
668 (void)capiminor_del_ack(mp, datahandle);
671 (void)handle_minor_send(mp);
674 /* ups, let capi application handle it :-) */
675 skb_queue_tail(&cdev->recvqueue, skb);
676 wake_up_interruptible(&cdev->recvwait);
678 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
679 spin_unlock_irqrestore(&workaround_lock, flags);
682 /* -------- file_operations for capidev ----------------------------- */
685 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
687 struct capidev *cdev = (struct capidev *)file->private_data;
691 if (!cdev->ap.applid)
694 if ((skb = skb_dequeue(&cdev->recvqueue)) == NULL) {
696 if (file->f_flags & O_NONBLOCK)
700 interruptible_sleep_on(&cdev->recvwait);
701 if ((skb = skb_dequeue(&cdev->recvqueue)) != NULL)
703 if (signal_pending(current))
707 return -ERESTARTNOHAND;
709 if (skb->len > count) {
710 skb_queue_head(&cdev->recvqueue, skb);
713 if (copy_to_user(buf, skb->data, skb->len)) {
714 skb_queue_head(&cdev->recvqueue, skb);
725 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
727 struct capidev *cdev = (struct capidev *)file->private_data;
731 if (!cdev->ap.applid)
734 skb = alloc_skb(count, GFP_USER);
738 if (copy_from_user(skb_put(skb, count), buf, count)) {
742 mlen = CAPIMSG_LEN(skb->data);
743 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
744 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
754 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
756 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
757 mutex_lock(&cdev->ncci_list_mtx);
758 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
759 mutex_unlock(&cdev->ncci_list_mtx);
762 cdev->errcode = capi20_put_message(&cdev->ap, skb);
772 capi_poll(struct file *file, poll_table * wait)
774 struct capidev *cdev = (struct capidev *)file->private_data;
775 unsigned int mask = 0;
777 if (!cdev->ap.applid)
780 poll_wait(file, &(cdev->recvwait), wait);
781 mask = POLLOUT | POLLWRNORM;
782 if (!skb_queue_empty(&cdev->recvqueue))
783 mask |= POLLIN | POLLRDNORM;
788 capi_ioctl(struct inode *inode, struct file *file,
789 unsigned int cmd, unsigned long arg)
791 struct capidev *cdev = file->private_data;
792 struct capi20_appl *ap = &cdev->ap;
793 capi_ioctl_struct data;
794 int retval = -EINVAL;
795 void __user *argp = (void __user *)arg;
803 if (copy_from_user(&cdev->ap.rparam, argp,
804 sizeof(struct capi_register_params)))
807 cdev->ap.private = cdev;
808 cdev->ap.recv_message = capi_recv_message;
809 cdev->errcode = capi20_register(ap);
815 return (int)ap->applid;
817 case CAPI_GET_VERSION:
819 if (copy_from_user(&data.contr, argp,
822 cdev->errcode = capi20_get_version(data.contr, &data.version);
825 if (copy_to_user(argp, &data.version,
826 sizeof(data.version)))
831 case CAPI_GET_SERIAL:
833 if (copy_from_user(&data.contr, argp,
836 cdev->errcode = capi20_get_serial (data.contr, data.serial);
839 if (copy_to_user(argp, data.serial,
840 sizeof(data.serial)))
844 case CAPI_GET_PROFILE:
846 if (copy_from_user(&data.contr, argp,
850 if (data.contr == 0) {
851 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
855 retval = copy_to_user(argp,
856 &data.profile.ncontroller,
857 sizeof(data.profile.ncontroller));
860 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
864 retval = copy_to_user(argp, &data.profile,
865 sizeof(data.profile));
872 case CAPI_GET_MANUFACTURER:
874 if (copy_from_user(&data.contr, argp,
877 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
881 if (copy_to_user(argp, data.manufacturer,
882 sizeof(data.manufacturer)))
887 case CAPI_GET_ERRCODE:
888 data.errcode = cdev->errcode;
889 cdev->errcode = CAPI_NOERROR;
891 if (copy_to_user(argp, &data.errcode,
892 sizeof(data.errcode)))
898 if (capi20_isinstalled() == CAPI_NOERROR)
902 case CAPI_MANUFACTURER_CMD:
904 struct capi_manufacturer_cmd mcmd;
905 if (!capable(CAP_SYS_ADMIN))
907 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
909 return capi20_manufacturer(mcmd.cmd, mcmd.data);
917 if (copy_from_user(&userflags, argp,
920 if (cmd == CAPI_SET_FLAGS)
921 cdev->userflags |= userflags;
923 cdev->userflags &= ~userflags;
928 if (copy_to_user(argp, &cdev->userflags,
929 sizeof(cdev->userflags)))
933 case CAPI_NCCI_OPENCOUNT:
935 struct capincci *nccip;
936 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
937 struct capiminor *mp;
938 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
941 if (copy_from_user(&ncci, argp, sizeof(ncci)))
944 mutex_lock(&cdev->ncci_list_mtx);
945 if ((nccip = capincci_find(cdev, (u32) ncci)) == NULL) {
946 mutex_unlock(&cdev->ncci_list_mtx);
949 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
950 if ((mp = nccip->minorp) != NULL) {
951 count += atomic_read(&mp->ttyopencount);
953 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
954 mutex_unlock(&cdev->ncci_list_mtx);
959 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
960 case CAPI_NCCI_GETUNIT:
962 struct capincci *nccip;
963 struct capiminor *mp;
966 if (copy_from_user(&ncci, argp,
969 mutex_lock(&cdev->ncci_list_mtx);
970 nccip = capincci_find(cdev, (u32) ncci);
971 if (!nccip || (mp = nccip->minorp) == NULL) {
972 mutex_unlock(&cdev->ncci_list_mtx);
976 mutex_unlock(&cdev->ncci_list_mtx);
980 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
986 capi_open(struct inode *inode, struct file *file)
991 if (file->private_data)
993 else if ((file->private_data = capidev_alloc()) == NULL)
996 ret = nonseekable_open(inode, file);
1002 capi_release(struct inode *inode, struct file *file)
1004 struct capidev *cdev = (struct capidev *)file->private_data;
1007 file->private_data = NULL;
1012 static const struct file_operations capi_fops =
1014 .owner = THIS_MODULE,
1015 .llseek = no_llseek,
1017 .write = capi_write,
1019 .ioctl = capi_ioctl,
1021 .release = capi_release,
1024 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1025 /* -------- tty_operations for capincci ----------------------------- */
1027 static int capinc_tty_open(struct tty_struct * tty, struct file * file)
1029 struct capiminor *mp;
1030 unsigned long flags;
1032 if ((mp = capiminor_find(iminor(file->f_path.dentry->d_inode))) == NULL)
1034 if (mp->nccip == NULL)
1037 tty->driver_data = (void *)mp;
1039 spin_lock_irqsave(&workaround_lock, flags);
1040 if (atomic_read(&mp->ttyopencount) == 0)
1042 atomic_inc(&mp->ttyopencount);
1043 #ifdef _DEBUG_REFCOUNT
1044 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1046 handle_minor_recv(mp);
1047 spin_unlock_irqrestore(&workaround_lock, flags);
1051 static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1053 struct capiminor *mp;
1055 mp = (struct capiminor *)tty->driver_data;
1057 if (atomic_dec_and_test(&mp->ttyopencount)) {
1058 #ifdef _DEBUG_REFCOUNT
1059 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1061 tty->driver_data = NULL;
1064 #ifdef _DEBUG_REFCOUNT
1065 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1067 if (mp->nccip == NULL)
1071 #ifdef _DEBUG_REFCOUNT
1072 printk(KERN_DEBUG "capinc_tty_close\n");
1076 static int capinc_tty_write(struct tty_struct * tty,
1077 const unsigned char *buf, int count)
1079 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1080 struct sk_buff *skb;
1081 unsigned long flags;
1083 #ifdef _DEBUG_TTYFUNCS
1084 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1087 if (!mp || !mp->nccip) {
1088 #ifdef _DEBUG_TTYFUNCS
1089 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1094 spin_lock_irqsave(&workaround_lock, flags);
1098 skb_queue_tail(&mp->outqueue, skb);
1099 mp->outbytes += skb->len;
1102 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1104 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1105 spin_unlock_irqrestore(&workaround_lock, flags);
1109 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1110 memcpy(skb_put(skb, count), buf, count);
1112 skb_queue_tail(&mp->outqueue, skb);
1113 mp->outbytes += skb->len;
1114 (void)handle_minor_send(mp);
1115 (void)handle_minor_recv(mp);
1116 spin_unlock_irqrestore(&workaround_lock, flags);
1120 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1122 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1123 struct sk_buff *skb;
1124 unsigned long flags;
1127 #ifdef _DEBUG_TTYFUNCS
1128 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1131 if (!mp || !mp->nccip) {
1132 #ifdef _DEBUG_TTYFUNCS
1133 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1138 spin_lock_irqsave(&workaround_lock, flags);
1141 if (skb_tailroom(skb) > 0) {
1142 *(skb_put(skb, 1)) = ch;
1143 spin_unlock_irqrestore(&workaround_lock, flags);
1147 skb_queue_tail(&mp->outqueue, skb);
1148 mp->outbytes += skb->len;
1149 (void)handle_minor_send(mp);
1151 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1153 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1154 *(skb_put(skb, 1)) = ch;
1157 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1160 spin_unlock_irqrestore(&workaround_lock, flags);
1164 static void capinc_tty_flush_chars(struct tty_struct *tty)
1166 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1167 struct sk_buff *skb;
1168 unsigned long flags;
1170 #ifdef _DEBUG_TTYFUNCS
1171 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1174 if (!mp || !mp->nccip) {
1175 #ifdef _DEBUG_TTYFUNCS
1176 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1181 spin_lock_irqsave(&workaround_lock, flags);
1185 skb_queue_tail(&mp->outqueue, skb);
1186 mp->outbytes += skb->len;
1187 (void)handle_minor_send(mp);
1189 (void)handle_minor_recv(mp);
1190 spin_unlock_irqrestore(&workaround_lock, flags);
1193 static int capinc_tty_write_room(struct tty_struct *tty)
1195 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1197 if (!mp || !mp->nccip) {
1198 #ifdef _DEBUG_TTYFUNCS
1199 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1203 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1204 room *= CAPI_MAX_BLKSIZE;
1205 #ifdef _DEBUG_TTYFUNCS
1206 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1211 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1213 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1214 if (!mp || !mp->nccip) {
1215 #ifdef _DEBUG_TTYFUNCS
1216 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1220 #ifdef _DEBUG_TTYFUNCS
1221 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1222 mp->outbytes, mp->nack,
1223 skb_queue_len(&mp->outqueue),
1224 skb_queue_len(&mp->inqueue));
1226 return mp->outbytes;
1229 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1230 unsigned int cmd, unsigned long arg)
1235 error = n_tty_ioctl_helper(tty, file, cmd, arg);
1241 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
1243 #ifdef _DEBUG_TTYFUNCS
1244 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1248 static void capinc_tty_throttle(struct tty_struct * tty)
1250 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1251 #ifdef _DEBUG_TTYFUNCS
1252 printk(KERN_DEBUG "capinc_tty_throttle\n");
1258 static void capinc_tty_unthrottle(struct tty_struct * tty)
1260 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1261 unsigned long flags;
1262 #ifdef _DEBUG_TTYFUNCS
1263 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1266 spin_lock_irqsave(&workaround_lock, flags);
1268 handle_minor_recv(mp);
1269 spin_unlock_irqrestore(&workaround_lock, flags);
1273 static void capinc_tty_stop(struct tty_struct *tty)
1275 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1276 #ifdef _DEBUG_TTYFUNCS
1277 printk(KERN_DEBUG "capinc_tty_stop\n");
1284 static void capinc_tty_start(struct tty_struct *tty)
1286 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1287 unsigned long flags;
1288 #ifdef _DEBUG_TTYFUNCS
1289 printk(KERN_DEBUG "capinc_tty_start\n");
1292 spin_lock_irqsave(&workaround_lock, flags);
1294 (void)handle_minor_send(mp);
1295 spin_unlock_irqrestore(&workaround_lock, flags);
1299 static void capinc_tty_hangup(struct tty_struct *tty)
1301 #ifdef _DEBUG_TTYFUNCS
1302 printk(KERN_DEBUG "capinc_tty_hangup\n");
1306 static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1308 #ifdef _DEBUG_TTYFUNCS
1309 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1314 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1316 #ifdef _DEBUG_TTYFUNCS
1317 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1321 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1323 #ifdef _DEBUG_TTYFUNCS
1324 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1328 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1330 #ifdef _DEBUG_TTYFUNCS
1331 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1335 static struct tty_driver *capinc_tty_driver;
1337 static const struct tty_operations capinc_ops = {
1338 .open = capinc_tty_open,
1339 .close = capinc_tty_close,
1340 .write = capinc_tty_write,
1341 .put_char = capinc_tty_put_char,
1342 .flush_chars = capinc_tty_flush_chars,
1343 .write_room = capinc_tty_write_room,
1344 .chars_in_buffer = capinc_tty_chars_in_buffer,
1345 .ioctl = capinc_tty_ioctl,
1346 .set_termios = capinc_tty_set_termios,
1347 .throttle = capinc_tty_throttle,
1348 .unthrottle = capinc_tty_unthrottle,
1349 .stop = capinc_tty_stop,
1350 .start = capinc_tty_start,
1351 .hangup = capinc_tty_hangup,
1352 .break_ctl = capinc_tty_break_ctl,
1353 .flush_buffer = capinc_tty_flush_buffer,
1354 .set_ldisc = capinc_tty_set_ldisc,
1355 .send_xchar = capinc_tty_send_xchar,
1358 static int capinc_tty_init(void)
1360 struct tty_driver *drv;
1362 if (capi_ttyminors > CAPINC_MAX_PORTS)
1363 capi_ttyminors = CAPINC_MAX_PORTS;
1364 if (capi_ttyminors <= 0)
1365 capi_ttyminors = CAPINC_NR_PORTS;
1367 drv = alloc_tty_driver(capi_ttyminors);
1371 drv->owner = THIS_MODULE;
1372 drv->driver_name = "capi_nc";
1374 drv->major = capi_ttymajor;
1375 drv->minor_start = 0;
1376 drv->type = TTY_DRIVER_TYPE_SERIAL;
1377 drv->subtype = SERIAL_TYPE_NORMAL;
1378 drv->init_termios = tty_std_termios;
1379 drv->init_termios.c_iflag = ICRNL;
1380 drv->init_termios.c_oflag = OPOST | ONLCR;
1381 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1382 drv->init_termios.c_lflag = 0;
1383 drv->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_RESET_TERMIOS;
1384 tty_set_operations(drv, &capinc_ops);
1385 if (tty_register_driver(drv)) {
1386 put_tty_driver(drv);
1387 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1390 capinc_tty_driver = drv;
1394 static void capinc_tty_exit(void)
1396 struct tty_driver *drv = capinc_tty_driver;
1398 if ((retval = tty_unregister_driver(drv)))
1399 printk(KERN_ERR "capi: failed to unregister capi_nc driver (%d)\n", retval);
1400 put_tty_driver(drv);
1403 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1405 /* -------- /proc functions ----------------------------------------- */
1408 * /proc/capi/capi20:
1409 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1411 static int capi20_proc_show(struct seq_file *m, void *v)
1413 struct capidev *cdev;
1414 struct list_head *l;
1416 read_lock(&capidev_list_lock);
1417 list_for_each(l, &capidev_list) {
1418 cdev = list_entry(l, struct capidev, list);
1419 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
1421 cdev->ap.nrecvctlpkt,
1422 cdev->ap.nrecvdatapkt,
1423 cdev->ap.nsentctlpkt,
1424 cdev->ap.nsentdatapkt);
1426 read_unlock(&capidev_list_lock);
1430 static int capi20_proc_open(struct inode *inode, struct file *file)
1432 return single_open(file, capi20_proc_show, NULL);
1435 static const struct file_operations capi20_proc_fops = {
1436 .owner = THIS_MODULE,
1437 .open = capi20_proc_open,
1439 .llseek = seq_lseek,
1440 .release = single_release,
1444 * /proc/capi/capi20ncci:
1447 static int capi20ncci_proc_show(struct seq_file *m, void *v)
1449 struct capidev *cdev;
1450 struct capincci *np;
1451 struct list_head *l;
1453 read_lock(&capidev_list_lock);
1454 list_for_each(l, &capidev_list) {
1455 cdev = list_entry(l, struct capidev, list);
1456 for (np=cdev->nccis; np; np = np->next) {
1457 seq_printf(m, "%d 0x%x\n",
1462 read_unlock(&capidev_list_lock);
1466 static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1468 return single_open(file, capi20ncci_proc_show, NULL);
1471 static const struct file_operations capi20ncci_proc_fops = {
1472 .owner = THIS_MODULE,
1473 .open = capi20ncci_proc_open,
1475 .llseek = seq_lseek,
1476 .release = single_release,
1479 static void __init proc_init(void)
1481 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1482 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
1485 static void __exit proc_exit(void)
1487 remove_proc_entry("capi/capi20", NULL);
1488 remove_proc_entry("capi/capi20ncci", NULL);
1491 /* -------- init function and module interface ---------------------- */
1494 static char rev[32];
1496 static int __init capi_init(void)
1502 if ((p = strchr(revision, ':')) != NULL && p[1]) {
1503 strlcpy(rev, p + 2, sizeof(rev));
1504 if ((p = strchr(rev, '$')) != NULL && p > rev)
1509 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1510 if (major_ret < 0) {
1511 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1514 capi_class = class_create(THIS_MODULE, "capi");
1515 if (IS_ERR(capi_class)) {
1516 unregister_chrdev(capi_major, "capi20");
1517 return PTR_ERR(capi_class);
1520 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
1522 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1523 if (capinc_tty_init() < 0) {
1524 device_destroy(capi_class, MKDEV(capi_major, 0));
1525 class_destroy(capi_class);
1526 unregister_chrdev(capi_major, "capi20");
1529 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1533 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1534 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1535 compileinfo = " (middleware+capifs)";
1537 compileinfo = " (no capifs)";
1540 compileinfo = " (no middleware)";
1542 printk(KERN_NOTICE "capi20: Rev %s: started up with major %d%s\n",
1543 rev, capi_major, compileinfo);
1548 static void __exit capi_exit(void)
1552 device_destroy(capi_class, MKDEV(capi_major, 0));
1553 class_destroy(capi_class);
1554 unregister_chrdev(capi_major, "capi20");
1556 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1559 printk(KERN_NOTICE "capi: Rev %s: unloaded\n", rev);
1562 module_init(capi_init);
1563 module_exit(capi_exit);