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/timer.h>
24 #include <linux/wait.h>
25 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
26 #include <linux/tty.h>
28 #include <linux/netdevice.h>
29 #include <linux/ppp_defs.h>
30 #include <linux/if_ppp.h>
31 #endif /* CONFIG_PPP */
32 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
33 #include <linux/skbuff.h>
34 #include <linux/proc_fs.h>
35 #include <linux/poll.h>
36 #include <linux/capi.h>
37 #include <linux/kernelcapi.h>
38 #include <linux/init.h>
39 #include <linux/device.h>
40 #include <linux/moduleparam.h>
41 #include <linux/isdn/capiutil.h>
42 #include <linux/isdn/capicmd.h>
43 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
47 static char *revision = "$Revision: 1.1.2.7 $";
49 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
50 MODULE_AUTHOR("Carsten Paeth");
51 MODULE_LICENSE("GPL");
53 #undef _DEBUG_REFCOUNT /* alloc/free and open/close debug */
54 #undef _DEBUG_TTYFUNCS /* call to tty_driver */
55 #undef _DEBUG_DATAFLOW /* data flow */
57 /* -------- driver information -------------------------------------- */
59 static struct class *capi_class;
61 static int capi_major = 68; /* allocated */
62 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
63 #define CAPINC_NR_PORTS 32
64 #define CAPINC_MAX_PORTS 256
65 static int capi_ttymajor = 191;
66 static int capi_ttyminors = CAPINC_NR_PORTS;
67 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
69 module_param_named(major, capi_major, uint, 0);
70 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
71 module_param_named(ttymajor, capi_ttymajor, uint, 0);
72 module_param_named(ttyminors, capi_ttyminors, uint, 0);
73 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
75 /* -------- defines ------------------------------------------------- */
77 #define CAPINC_MAX_RECVQUEUE 10
78 #define CAPINC_MAX_SENDQUEUE 10
79 #define CAPI_MAX_BLKSIZE 2048
81 /* -------- data structures ----------------------------------------- */
85 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
88 struct datahandle_queue {
89 struct list_head list;
94 struct list_head list;
95 struct capincci *nccip;
98 struct capi20_appl *ap;
103 struct tty_struct *tty;
106 struct sk_buff *ttyskb;
107 atomic_t ttyopencount;
109 struct sk_buff_head inqueue;
111 struct sk_buff_head outqueue;
115 struct list_head ackqueue;
119 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
121 /* FIXME: The following lock is a sledgehammer-workaround to a
122 * locking issue with the capiminor (and maybe other) data structure(s).
123 * Access to this data is done in a racy way and crashes the machine with
124 * a FritzCard DSL driver; sooner or later. This is a workaround
125 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
126 * The correct (and scalable) fix for the issue seems to require
127 * an API change to the drivers... . */
128 static DEFINE_SPINLOCK(workaround_lock);
131 struct capincci *next;
133 struct capidev *cdev;
134 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
135 struct capiminor *minorp;
136 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
140 struct list_head list;
141 struct capi20_appl ap;
145 struct sk_buff_head recvqueue;
146 wait_queue_head_t recvwait;
148 struct capincci *nccis;
150 struct mutex ncci_list_mtx;
153 /* -------- global variables ---------------------------------------- */
155 static DEFINE_RWLOCK(capidev_list_lock);
156 static LIST_HEAD(capidev_list);
158 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
159 static DEFINE_RWLOCK(capiminor_list_lock);
160 static LIST_HEAD(capiminor_list);
161 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
163 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
164 /* -------- datahandles --------------------------------------------- */
166 static int capincci_add_ack(struct capiminor *mp, u16 datahandle)
168 struct datahandle_queue *n;
171 n = kmalloc(sizeof(*n), GFP_ATOMIC);
173 printk(KERN_ERR "capi: alloc datahandle failed\n");
176 n->datahandle = datahandle;
177 INIT_LIST_HEAD(&n->list);
178 spin_lock_irqsave(&mp->ackqlock, flags);
179 list_add_tail(&n->list, &mp->ackqueue);
181 spin_unlock_irqrestore(&mp->ackqlock, flags);
185 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
187 struct datahandle_queue *p, *tmp;
190 spin_lock_irqsave(&mp->ackqlock, flags);
191 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
192 if (p->datahandle == datahandle) {
196 spin_unlock_irqrestore(&mp->ackqlock, flags);
200 spin_unlock_irqrestore(&mp->ackqlock, flags);
204 static void capiminor_del_all_ack(struct capiminor *mp)
206 struct datahandle_queue *p, *tmp;
209 spin_lock_irqsave(&mp->ackqlock, flags);
210 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
215 spin_unlock_irqrestore(&mp->ackqlock, flags);
219 /* -------- struct capiminor ---------------------------------------- */
221 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
223 struct capiminor *mp, *p;
224 unsigned int minor = 0;
227 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
229 printk(KERN_ERR "capi: can't alloc capiminor\n");
236 atomic_set(&mp->ttyopencount,0);
237 INIT_LIST_HEAD(&mp->ackqueue);
238 spin_lock_init(&mp->ackqlock);
240 skb_queue_head_init(&mp->inqueue);
241 skb_queue_head_init(&mp->outqueue);
243 /* Allocate the least unused minor number.
245 write_lock_irqsave(&capiminor_list_lock, flags);
246 if (list_empty(&capiminor_list))
247 list_add(&mp->list, &capiminor_list);
249 list_for_each_entry(p, &capiminor_list, list) {
250 if (p->minor > minor)
255 if (minor < capi_ttyminors) {
257 list_add(&mp->list, p->list.prev);
260 write_unlock_irqrestore(&capiminor_list_lock, flags);
262 if (!(minor < capi_ttyminors)) {
263 printk(KERN_NOTICE "capi: out of minors\n");
271 static void capiminor_free(struct capiminor *mp)
275 write_lock_irqsave(&capiminor_list_lock, flags);
277 write_unlock_irqrestore(&capiminor_list_lock, flags);
279 if (mp->ttyskb) kfree_skb(mp->ttyskb);
281 skb_queue_purge(&mp->inqueue);
282 skb_queue_purge(&mp->outqueue);
283 capiminor_del_all_ack(mp);
287 static struct capiminor *capiminor_find(unsigned int minor)
290 struct capiminor *p = NULL;
292 read_lock(&capiminor_list_lock);
293 list_for_each(l, &capiminor_list) {
294 p = list_entry(l, struct capiminor, list);
295 if (p->minor == minor)
298 read_unlock(&capiminor_list_lock);
299 if (l == &capiminor_list)
304 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
306 /* -------- struct capincci ----------------------------------------- */
308 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
310 struct capincci *np, **pp;
311 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
312 struct capiminor *mp = NULL;
313 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
315 np = kzalloc(sizeof(*np), GFP_ATOMIC);
320 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
322 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
323 mp = np->minorp = capiminor_alloc(&cdev->ap, ncci);
326 #ifdef _DEBUG_REFCOUNT
327 printk(KERN_DEBUG "set mp->nccip\n");
329 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
330 capifs_new_ncci(mp->minor, MKDEV(capi_ttymajor, mp->minor));
333 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
334 for (pp=&cdev->nccis; *pp; pp = &(*pp)->next)
340 static void capincci_free(struct capidev *cdev, u32 ncci)
342 struct capincci *np, **pp;
343 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
344 struct capiminor *mp;
345 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
350 if (ncci == 0xffffffff || np->ncci == ncci) {
352 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
353 if ((mp = np->minorp) != 0) {
354 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
355 capifs_free_ncci(mp->minor);
359 #ifdef _DEBUG_REFCOUNT
360 printk(KERN_DEBUG "reset mp->nccip\n");
367 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
369 if (*pp == 0) return;
376 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
380 for (p=cdev->nccis; p ; p = p->next) {
387 /* -------- struct capidev ------------------------------------------ */
389 static struct capidev *capidev_alloc(void)
391 struct capidev *cdev;
394 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
398 mutex_init(&cdev->ncci_list_mtx);
399 skb_queue_head_init(&cdev->recvqueue);
400 init_waitqueue_head(&cdev->recvwait);
401 write_lock_irqsave(&capidev_list_lock, flags);
402 list_add_tail(&cdev->list, &capidev_list);
403 write_unlock_irqrestore(&capidev_list_lock, flags);
407 static void capidev_free(struct capidev *cdev)
411 if (cdev->ap.applid) {
412 capi20_release(&cdev->ap);
415 skb_queue_purge(&cdev->recvqueue);
417 mutex_lock(&cdev->ncci_list_mtx);
418 capincci_free(cdev, 0xffffffff);
419 mutex_unlock(&cdev->ncci_list_mtx);
421 write_lock_irqsave(&capidev_list_lock, flags);
422 list_del(&cdev->list);
423 write_unlock_irqrestore(&capidev_list_lock, flags);
427 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
428 /* -------- handle data queue --------------------------------------- */
430 static struct sk_buff *
431 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
433 struct sk_buff *nskb;
434 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
436 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
437 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
438 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
439 capimsg_setu16(s, 2, mp->ap->applid);
440 capimsg_setu8 (s, 4, CAPI_DATA_B3);
441 capimsg_setu8 (s, 5, CAPI_RESP);
442 capimsg_setu16(s, 6, mp->msgid++);
443 capimsg_setu32(s, 8, mp->ncci);
444 capimsg_setu16(s, 12, datahandle);
449 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
451 struct sk_buff *nskb;
453 u16 errcode, datahandle;
454 struct tty_ldisc *ld;
456 datalen = skb->len - CAPIMSG_LEN(skb->data);
459 #ifdef _DEBUG_DATAFLOW
460 printk(KERN_DEBUG "capi: currently no receiver\n");
465 ld = tty_ldisc_ref(mp->tty);
468 if (ld->receive_buf == NULL) {
469 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
470 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
475 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
476 printk(KERN_DEBUG "capi: recv tty throttled\n");
480 if (mp->tty->receive_room < datalen) {
481 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
482 printk(KERN_DEBUG "capi: no room in tty\n");
486 if ((nskb = gen_data_b3_resp_for(mp, skb)) == 0) {
487 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
490 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
491 errcode = capi20_put_message(mp->ap, nskb);
492 if (errcode != CAPI_NOERROR) {
493 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
498 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
499 #ifdef _DEBUG_DATAFLOW
500 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
501 datahandle, skb->len);
503 ld->receive_buf(mp->tty, skb->data, NULL, skb->len);
512 static void handle_minor_recv(struct capiminor *mp)
515 while ((skb = skb_dequeue(&mp->inqueue)) != 0) {
516 unsigned int len = skb->len;
518 if (handle_recv_skb(mp, skb) < 0) {
519 skb_queue_head(&mp->inqueue, skb);
526 static int handle_minor_send(struct capiminor *mp)
534 if (mp->tty && mp->ttyoutstop) {
535 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
536 printk(KERN_DEBUG "capi: send: tty stopped\n");
541 while ((skb = skb_dequeue(&mp->outqueue)) != 0) {
542 datahandle = mp->datahandle;
544 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
545 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
546 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
547 capimsg_setu16(skb->data, 2, mp->ap->applid);
548 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
549 capimsg_setu8 (skb->data, 5, CAPI_REQ);
550 capimsg_setu16(skb->data, 6, mp->msgid++);
551 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
552 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
553 capimsg_setu16(skb->data, 16, len); /* Data length */
554 capimsg_setu16(skb->data, 18, datahandle);
555 capimsg_setu16(skb->data, 20, 0); /* Flags */
557 if (capincci_add_ack(mp, datahandle) < 0) {
558 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
559 skb_queue_head(&mp->outqueue, skb);
562 errcode = capi20_put_message(mp->ap, skb);
563 if (errcode == CAPI_NOERROR) {
567 #ifdef _DEBUG_DATAFLOW
568 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
573 capiminor_del_ack(mp, datahandle);
575 if (errcode == CAPI_SENDQUEUEFULL) {
576 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
577 skb_queue_head(&mp->outqueue, skb);
581 /* ups, drop packet */
582 printk(KERN_ERR "capi: put_message = %x\n", errcode);
589 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
590 /* -------- function called by lower level -------------------------- */
592 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
594 struct capidev *cdev = ap->private;
595 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
596 struct capiminor *mp;
598 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
603 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
604 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
606 mutex_lock(&cdev->ncci_list_mtx);
607 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
608 mutex_unlock(&cdev->ncci_list_mtx);
611 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND) {
612 mutex_lock(&cdev->ncci_list_mtx);
613 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
614 mutex_unlock(&cdev->ncci_list_mtx);
616 spin_lock_irqsave(&workaround_lock, flags);
617 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
618 skb_queue_tail(&cdev->recvqueue, skb);
619 wake_up_interruptible(&cdev->recvwait);
620 spin_unlock_irqrestore(&workaround_lock, flags);
623 ncci = CAPIMSG_CONTROL(skb->data);
624 for (np = cdev->nccis; np && np->ncci != ncci; np = np->next)
627 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
628 skb_queue_tail(&cdev->recvqueue, skb);
629 wake_up_interruptible(&cdev->recvwait);
630 spin_unlock_irqrestore(&workaround_lock, flags);
633 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
634 skb_queue_tail(&cdev->recvqueue, skb);
635 wake_up_interruptible(&cdev->recvwait);
636 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
639 skb_queue_tail(&cdev->recvqueue, skb);
640 wake_up_interruptible(&cdev->recvwait);
641 spin_unlock_irqrestore(&workaround_lock, flags);
646 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
648 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
649 #ifdef _DEBUG_DATAFLOW
650 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
651 datahandle, skb->len-CAPIMSG_LEN(skb->data));
653 skb_queue_tail(&mp->inqueue, skb);
654 mp->inbytes += skb->len;
655 handle_minor_recv(mp);
657 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
659 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
660 #ifdef _DEBUG_DATAFLOW
661 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
663 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
666 (void)capiminor_del_ack(mp, datahandle);
669 (void)handle_minor_send(mp);
672 /* ups, let capi application handle it :-) */
673 skb_queue_tail(&cdev->recvqueue, skb);
674 wake_up_interruptible(&cdev->recvwait);
676 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
677 spin_unlock_irqrestore(&workaround_lock, flags);
680 /* -------- file_operations for capidev ----------------------------- */
683 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
685 struct capidev *cdev = (struct capidev *)file->private_data;
689 if (!cdev->ap.applid)
692 if ((skb = skb_dequeue(&cdev->recvqueue)) == 0) {
694 if (file->f_flags & O_NONBLOCK)
698 interruptible_sleep_on(&cdev->recvwait);
699 if ((skb = skb_dequeue(&cdev->recvqueue)) != 0)
701 if (signal_pending(current))
705 return -ERESTARTNOHAND;
707 if (skb->len > count) {
708 skb_queue_head(&cdev->recvqueue, skb);
711 if (copy_to_user(buf, skb->data, skb->len)) {
712 skb_queue_head(&cdev->recvqueue, skb);
723 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
725 struct capidev *cdev = (struct capidev *)file->private_data;
729 if (!cdev->ap.applid)
732 skb = alloc_skb(count, GFP_USER);
736 if (copy_from_user(skb_put(skb, count), buf, count)) {
740 mlen = CAPIMSG_LEN(skb->data);
741 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
742 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
752 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
754 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
755 mutex_lock(&cdev->ncci_list_mtx);
756 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
757 mutex_unlock(&cdev->ncci_list_mtx);
760 cdev->errcode = capi20_put_message(&cdev->ap, skb);
770 capi_poll(struct file *file, poll_table * wait)
772 struct capidev *cdev = (struct capidev *)file->private_data;
773 unsigned int mask = 0;
775 if (!cdev->ap.applid)
778 poll_wait(file, &(cdev->recvwait), wait);
779 mask = POLLOUT | POLLWRNORM;
780 if (!skb_queue_empty(&cdev->recvqueue))
781 mask |= POLLIN | POLLRDNORM;
786 capi_ioctl(struct inode *inode, struct file *file,
787 unsigned int cmd, unsigned long arg)
789 struct capidev *cdev = file->private_data;
790 struct capi20_appl *ap = &cdev->ap;
791 capi_ioctl_struct data;
792 int retval = -EINVAL;
793 void __user *argp = (void __user *)arg;
801 if (copy_from_user(&cdev->ap.rparam, argp,
802 sizeof(struct capi_register_params)))
805 cdev->ap.private = cdev;
806 cdev->ap.recv_message = capi_recv_message;
807 cdev->errcode = capi20_register(ap);
813 return (int)ap->applid;
815 case CAPI_GET_VERSION:
817 if (copy_from_user(&data.contr, argp,
820 cdev->errcode = capi20_get_version(data.contr, &data.version);
823 if (copy_to_user(argp, &data.version,
824 sizeof(data.version)))
829 case CAPI_GET_SERIAL:
831 if (copy_from_user(&data.contr, argp,
834 cdev->errcode = capi20_get_serial (data.contr, data.serial);
837 if (copy_to_user(argp, data.serial,
838 sizeof(data.serial)))
842 case CAPI_GET_PROFILE:
844 if (copy_from_user(&data.contr, argp,
848 if (data.contr == 0) {
849 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
853 retval = copy_to_user(argp,
854 &data.profile.ncontroller,
855 sizeof(data.profile.ncontroller));
858 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
862 retval = copy_to_user(argp, &data.profile,
863 sizeof(data.profile));
870 case CAPI_GET_MANUFACTURER:
872 if (copy_from_user(&data.contr, argp,
875 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
879 if (copy_to_user(argp, data.manufacturer,
880 sizeof(data.manufacturer)))
885 case CAPI_GET_ERRCODE:
886 data.errcode = cdev->errcode;
887 cdev->errcode = CAPI_NOERROR;
889 if (copy_to_user(argp, &data.errcode,
890 sizeof(data.errcode)))
896 if (capi20_isinstalled() == CAPI_NOERROR)
900 case CAPI_MANUFACTURER_CMD:
902 struct capi_manufacturer_cmd mcmd;
903 if (!capable(CAP_SYS_ADMIN))
905 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
907 return capi20_manufacturer(mcmd.cmd, mcmd.data);
915 if (copy_from_user(&userflags, argp,
918 if (cmd == CAPI_SET_FLAGS)
919 cdev->userflags |= userflags;
921 cdev->userflags &= ~userflags;
926 if (copy_to_user(argp, &cdev->userflags,
927 sizeof(cdev->userflags)))
931 case CAPI_NCCI_OPENCOUNT:
933 struct capincci *nccip;
934 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
935 struct capiminor *mp;
936 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
939 if (copy_from_user(&ncci, argp, sizeof(ncci)))
942 mutex_lock(&cdev->ncci_list_mtx);
943 if ((nccip = capincci_find(cdev, (u32) ncci)) == 0) {
944 mutex_unlock(&cdev->ncci_list_mtx);
947 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
948 if ((mp = nccip->minorp) != 0) {
949 count += atomic_read(&mp->ttyopencount);
951 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
952 mutex_unlock(&cdev->ncci_list_mtx);
957 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
958 case CAPI_NCCI_GETUNIT:
960 struct capincci *nccip;
961 struct capiminor *mp;
964 if (copy_from_user(&ncci, argp,
967 mutex_lock(&cdev->ncci_list_mtx);
968 nccip = capincci_find(cdev, (u32) ncci);
969 if (!nccip || (mp = nccip->minorp) == 0) {
970 mutex_unlock(&cdev->ncci_list_mtx);
974 mutex_unlock(&cdev->ncci_list_mtx);
978 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
984 capi_open(struct inode *inode, struct file *file)
986 if (file->private_data)
989 if ((file->private_data = capidev_alloc()) == 0)
992 return nonseekable_open(inode, file);
996 capi_release(struct inode *inode, struct file *file)
998 struct capidev *cdev = (struct capidev *)file->private_data;
1001 file->private_data = NULL;
1006 static const struct file_operations capi_fops =
1008 .owner = THIS_MODULE,
1009 .llseek = no_llseek,
1011 .write = capi_write,
1013 .ioctl = capi_ioctl,
1015 .release = capi_release,
1018 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1019 /* -------- tty_operations for capincci ----------------------------- */
1021 static int capinc_tty_open(struct tty_struct * tty, struct file * file)
1023 struct capiminor *mp;
1024 unsigned long flags;
1026 if ((mp = capiminor_find(iminor(file->f_path.dentry->d_inode))) == 0)
1031 tty->driver_data = (void *)mp;
1033 spin_lock_irqsave(&workaround_lock, flags);
1034 if (atomic_read(&mp->ttyopencount) == 0)
1036 atomic_inc(&mp->ttyopencount);
1037 #ifdef _DEBUG_REFCOUNT
1038 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1040 handle_minor_recv(mp);
1041 spin_unlock_irqrestore(&workaround_lock, flags);
1045 static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1047 struct capiminor *mp;
1049 mp = (struct capiminor *)tty->driver_data;
1051 if (atomic_dec_and_test(&mp->ttyopencount)) {
1052 #ifdef _DEBUG_REFCOUNT
1053 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1055 tty->driver_data = NULL;
1058 #ifdef _DEBUG_REFCOUNT
1059 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1065 #ifdef _DEBUG_REFCOUNT
1066 printk(KERN_DEBUG "capinc_tty_close\n");
1070 static int capinc_tty_write(struct tty_struct * tty,
1071 const unsigned char *buf, int count)
1073 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1074 struct sk_buff *skb;
1075 unsigned long flags;
1077 #ifdef _DEBUG_TTYFUNCS
1078 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1081 if (!mp || !mp->nccip) {
1082 #ifdef _DEBUG_TTYFUNCS
1083 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1088 spin_lock_irqsave(&workaround_lock, flags);
1092 skb_queue_tail(&mp->outqueue, skb);
1093 mp->outbytes += skb->len;
1096 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1098 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1099 spin_unlock_irqrestore(&workaround_lock, flags);
1103 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1104 memcpy(skb_put(skb, count), buf, count);
1106 skb_queue_tail(&mp->outqueue, skb);
1107 mp->outbytes += skb->len;
1108 (void)handle_minor_send(mp);
1109 (void)handle_minor_recv(mp);
1110 spin_unlock_irqrestore(&workaround_lock, flags);
1114 static void capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1116 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1117 struct sk_buff *skb;
1118 unsigned long flags;
1120 #ifdef _DEBUG_TTYFUNCS
1121 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1124 if (!mp || !mp->nccip) {
1125 #ifdef _DEBUG_TTYFUNCS
1126 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1131 spin_lock_irqsave(&workaround_lock, flags);
1134 if (skb_tailroom(skb) > 0) {
1135 *(skb_put(skb, 1)) = ch;
1136 spin_unlock_irqrestore(&workaround_lock, flags);
1140 skb_queue_tail(&mp->outqueue, skb);
1141 mp->outbytes += skb->len;
1142 (void)handle_minor_send(mp);
1144 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1146 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1147 *(skb_put(skb, 1)) = ch;
1150 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1152 spin_unlock_irqrestore(&workaround_lock, flags);
1155 static void capinc_tty_flush_chars(struct tty_struct *tty)
1157 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1158 struct sk_buff *skb;
1159 unsigned long flags;
1161 #ifdef _DEBUG_TTYFUNCS
1162 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1165 if (!mp || !mp->nccip) {
1166 #ifdef _DEBUG_TTYFUNCS
1167 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1172 spin_lock_irqsave(&workaround_lock, flags);
1176 skb_queue_tail(&mp->outqueue, skb);
1177 mp->outbytes += skb->len;
1178 (void)handle_minor_send(mp);
1180 (void)handle_minor_recv(mp);
1181 spin_unlock_irqrestore(&workaround_lock, flags);
1184 static int capinc_tty_write_room(struct tty_struct *tty)
1186 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1188 if (!mp || !mp->nccip) {
1189 #ifdef _DEBUG_TTYFUNCS
1190 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1194 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1195 room *= CAPI_MAX_BLKSIZE;
1196 #ifdef _DEBUG_TTYFUNCS
1197 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1202 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1204 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1205 if (!mp || !mp->nccip) {
1206 #ifdef _DEBUG_TTYFUNCS
1207 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1211 #ifdef _DEBUG_TTYFUNCS
1212 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1213 mp->outbytes, mp->nack,
1214 skb_queue_len(&mp->outqueue),
1215 skb_queue_len(&mp->inqueue));
1217 return mp->outbytes;
1220 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1221 unsigned int cmd, unsigned long arg)
1226 error = n_tty_ioctl (tty, file, cmd, arg);
1232 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
1234 #ifdef _DEBUG_TTYFUNCS
1235 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1239 static void capinc_tty_throttle(struct tty_struct * tty)
1241 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1242 #ifdef _DEBUG_TTYFUNCS
1243 printk(KERN_DEBUG "capinc_tty_throttle\n");
1249 static void capinc_tty_unthrottle(struct tty_struct * tty)
1251 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1252 unsigned long flags;
1253 #ifdef _DEBUG_TTYFUNCS
1254 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1257 spin_lock_irqsave(&workaround_lock, flags);
1259 handle_minor_recv(mp);
1260 spin_unlock_irqrestore(&workaround_lock, flags);
1264 static void capinc_tty_stop(struct tty_struct *tty)
1266 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1267 #ifdef _DEBUG_TTYFUNCS
1268 printk(KERN_DEBUG "capinc_tty_stop\n");
1275 static void capinc_tty_start(struct tty_struct *tty)
1277 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1278 unsigned long flags;
1279 #ifdef _DEBUG_TTYFUNCS
1280 printk(KERN_DEBUG "capinc_tty_start\n");
1283 spin_lock_irqsave(&workaround_lock, flags);
1285 (void)handle_minor_send(mp);
1286 spin_unlock_irqrestore(&workaround_lock, flags);
1290 static void capinc_tty_hangup(struct tty_struct *tty)
1292 #ifdef _DEBUG_TTYFUNCS
1293 printk(KERN_DEBUG "capinc_tty_hangup\n");
1297 static void capinc_tty_break_ctl(struct tty_struct *tty, int state)
1299 #ifdef _DEBUG_TTYFUNCS
1300 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1304 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1306 #ifdef _DEBUG_TTYFUNCS
1307 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1311 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1313 #ifdef _DEBUG_TTYFUNCS
1314 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1318 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1320 #ifdef _DEBUG_TTYFUNCS
1321 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1325 static int capinc_tty_read_proc(char *page, char **start, off_t off,
1326 int count, int *eof, void *data)
1331 static struct tty_driver *capinc_tty_driver;
1333 static const struct tty_operations capinc_ops = {
1334 .open = capinc_tty_open,
1335 .close = capinc_tty_close,
1336 .write = capinc_tty_write,
1337 .put_char = capinc_tty_put_char,
1338 .flush_chars = capinc_tty_flush_chars,
1339 .write_room = capinc_tty_write_room,
1340 .chars_in_buffer = capinc_tty_chars_in_buffer,
1341 .ioctl = capinc_tty_ioctl,
1342 .set_termios = capinc_tty_set_termios,
1343 .throttle = capinc_tty_throttle,
1344 .unthrottle = capinc_tty_unthrottle,
1345 .stop = capinc_tty_stop,
1346 .start = capinc_tty_start,
1347 .hangup = capinc_tty_hangup,
1348 .break_ctl = capinc_tty_break_ctl,
1349 .flush_buffer = capinc_tty_flush_buffer,
1350 .set_ldisc = capinc_tty_set_ldisc,
1351 .send_xchar = capinc_tty_send_xchar,
1352 .read_proc = capinc_tty_read_proc,
1355 static int capinc_tty_init(void)
1357 struct tty_driver *drv;
1359 if (capi_ttyminors > CAPINC_MAX_PORTS)
1360 capi_ttyminors = CAPINC_MAX_PORTS;
1361 if (capi_ttyminors <= 0)
1362 capi_ttyminors = CAPINC_NR_PORTS;
1364 drv = alloc_tty_driver(capi_ttyminors);
1368 drv->owner = THIS_MODULE;
1369 drv->driver_name = "capi_nc";
1371 drv->major = capi_ttymajor;
1372 drv->minor_start = 0;
1373 drv->type = TTY_DRIVER_TYPE_SERIAL;
1374 drv->subtype = SERIAL_TYPE_NORMAL;
1375 drv->init_termios = tty_std_termios;
1376 drv->init_termios.c_iflag = ICRNL;
1377 drv->init_termios.c_oflag = OPOST | ONLCR;
1378 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1379 drv->init_termios.c_lflag = 0;
1380 drv->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_RESET_TERMIOS;
1381 tty_set_operations(drv, &capinc_ops);
1382 if (tty_register_driver(drv)) {
1383 put_tty_driver(drv);
1384 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1387 capinc_tty_driver = drv;
1391 static void capinc_tty_exit(void)
1393 struct tty_driver *drv = capinc_tty_driver;
1395 if ((retval = tty_unregister_driver(drv)))
1396 printk(KERN_ERR "capi: failed to unregister capi_nc driver (%d)\n", retval);
1397 put_tty_driver(drv);
1400 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1402 /* -------- /proc functions ----------------------------------------- */
1405 * /proc/capi/capi20:
1406 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1408 static int proc_capidev_read_proc(char *page, char **start, off_t off,
1409 int count, int *eof, void *data)
1411 struct capidev *cdev;
1412 struct list_head *l;
1415 read_lock(&capidev_list_lock);
1416 list_for_each(l, &capidev_list) {
1417 cdev = list_entry(l, struct capidev, list);
1418 len += sprintf(page+len, "0 %d %lu %lu %lu %lu\n",
1420 cdev->ap.nrecvctlpkt,
1421 cdev->ap.nrecvdatapkt,
1422 cdev->ap.nsentctlpkt,
1423 cdev->ap.nsentdatapkt);
1428 if (len-off > count)
1434 read_unlock(&capidev_list_lock);
1437 if (len > count) len = count;
1438 if (len < 0) len = 0;
1443 * /proc/capi/capi20ncci:
1446 static int proc_capincci_read_proc(char *page, char **start, off_t off,
1447 int count, int *eof, void *data)
1449 struct capidev *cdev;
1450 struct capincci *np;
1451 struct list_head *l;
1454 read_lock(&capidev_list_lock);
1455 list_for_each(l, &capidev_list) {
1456 cdev = list_entry(l, struct capidev, list);
1457 for (np=cdev->nccis; np; np = np->next) {
1458 len += sprintf(page+len, "%d 0x%x\n",
1465 if (len-off > count)
1471 read_unlock(&capidev_list_lock);
1475 if (len>count) len = count;
1480 static struct procfsentries {
1483 int (*read_proc)(char *page, char **start, off_t off,
1484 int count, int *eof, void *data);
1485 struct proc_dir_entry *procent;
1486 } procfsentries[] = {
1487 /* { "capi", S_IFDIR, 0 }, */
1488 { "capi/capi20", 0 , proc_capidev_read_proc },
1489 { "capi/capi20ncci", 0 , proc_capincci_read_proc },
1492 static void __init proc_init(void)
1494 int nelem = ARRAY_SIZE(procfsentries);
1497 for (i=0; i < nelem; i++) {
1498 struct procfsentries *p = procfsentries + i;
1499 p->procent = create_proc_entry(p->name, p->mode, NULL);
1500 if (p->procent) p->procent->read_proc = p->read_proc;
1504 static void __exit proc_exit(void)
1506 int nelem = ARRAY_SIZE(procfsentries);
1509 for (i=nelem-1; i >= 0; i--) {
1510 struct procfsentries *p = procfsentries + i;
1512 remove_proc_entry(p->name, NULL);
1518 /* -------- init function and module interface ---------------------- */
1521 static char rev[32];
1523 static int __init capi_init(void)
1529 if ((p = strchr(revision, ':')) != 0 && p[1]) {
1530 strlcpy(rev, p + 2, sizeof(rev));
1531 if ((p = strchr(rev, '$')) != 0 && p > rev)
1536 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1537 if (major_ret < 0) {
1538 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1541 capi_class = class_create(THIS_MODULE, "capi");
1542 if (IS_ERR(capi_class)) {
1543 unregister_chrdev(capi_major, "capi20");
1544 return PTR_ERR(capi_class);
1547 device_create(capi_class, NULL, MKDEV(capi_major, 0), "capi");
1549 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1550 if (capinc_tty_init() < 0) {
1551 device_destroy(capi_class, MKDEV(capi_major, 0));
1552 class_destroy(capi_class);
1553 unregister_chrdev(capi_major, "capi20");
1556 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1560 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1561 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1562 compileinfo = " (middleware+capifs)";
1564 compileinfo = " (no capifs)";
1567 compileinfo = " (no middleware)";
1569 printk(KERN_NOTICE "capi20: Rev %s: started up with major %d%s\n",
1570 rev, capi_major, compileinfo);
1575 static void __exit capi_exit(void)
1579 device_destroy(capi_class, MKDEV(capi_major, 0));
1580 class_destroy(capi_class);
1581 unregister_chrdev(capi_major, "capi20");
1583 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1586 printk(KERN_NOTICE "capi: Rev %s: unloaded\n", rev);
1589 module_init(capi_init);
1590 module_exit(capi_exit);