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) != NULL) {
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 == NULL) 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)) == NULL) {
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)) != NULL) {
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)) != NULL) {
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)) == NULL) {
694 if (file->f_flags & O_NONBLOCK)
698 interruptible_sleep_on(&cdev->recvwait);
699 if ((skb = skb_dequeue(&cdev->recvqueue)) != NULL)
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)) == NULL) {
944 mutex_unlock(&cdev->ncci_list_mtx);
947 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
948 if ((mp = nccip->minorp) != NULL) {
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) == NULL) {
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()) == NULL)
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))) == NULL)
1028 if (mp->nccip == NULL)
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));
1061 if (mp->nccip == NULL)
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 int 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;
1121 #ifdef _DEBUG_TTYFUNCS
1122 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1125 if (!mp || !mp->nccip) {
1126 #ifdef _DEBUG_TTYFUNCS
1127 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1132 spin_lock_irqsave(&workaround_lock, flags);
1135 if (skb_tailroom(skb) > 0) {
1136 *(skb_put(skb, 1)) = ch;
1137 spin_unlock_irqrestore(&workaround_lock, flags);
1141 skb_queue_tail(&mp->outqueue, skb);
1142 mp->outbytes += skb->len;
1143 (void)handle_minor_send(mp);
1145 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1147 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1148 *(skb_put(skb, 1)) = ch;
1151 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1154 spin_unlock_irqrestore(&workaround_lock, flags);
1158 static void capinc_tty_flush_chars(struct tty_struct *tty)
1160 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1161 struct sk_buff *skb;
1162 unsigned long flags;
1164 #ifdef _DEBUG_TTYFUNCS
1165 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1168 if (!mp || !mp->nccip) {
1169 #ifdef _DEBUG_TTYFUNCS
1170 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1175 spin_lock_irqsave(&workaround_lock, flags);
1179 skb_queue_tail(&mp->outqueue, skb);
1180 mp->outbytes += skb->len;
1181 (void)handle_minor_send(mp);
1183 (void)handle_minor_recv(mp);
1184 spin_unlock_irqrestore(&workaround_lock, flags);
1187 static int capinc_tty_write_room(struct tty_struct *tty)
1189 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1191 if (!mp || !mp->nccip) {
1192 #ifdef _DEBUG_TTYFUNCS
1193 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1197 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1198 room *= CAPI_MAX_BLKSIZE;
1199 #ifdef _DEBUG_TTYFUNCS
1200 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1205 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1207 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1208 if (!mp || !mp->nccip) {
1209 #ifdef _DEBUG_TTYFUNCS
1210 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1214 #ifdef _DEBUG_TTYFUNCS
1215 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1216 mp->outbytes, mp->nack,
1217 skb_queue_len(&mp->outqueue),
1218 skb_queue_len(&mp->inqueue));
1220 return mp->outbytes;
1223 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1224 unsigned int cmd, unsigned long arg)
1229 error = n_tty_ioctl (tty, file, cmd, arg);
1235 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
1237 #ifdef _DEBUG_TTYFUNCS
1238 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1242 static void capinc_tty_throttle(struct tty_struct * tty)
1244 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1245 #ifdef _DEBUG_TTYFUNCS
1246 printk(KERN_DEBUG "capinc_tty_throttle\n");
1252 static void capinc_tty_unthrottle(struct tty_struct * tty)
1254 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1255 unsigned long flags;
1256 #ifdef _DEBUG_TTYFUNCS
1257 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1260 spin_lock_irqsave(&workaround_lock, flags);
1262 handle_minor_recv(mp);
1263 spin_unlock_irqrestore(&workaround_lock, flags);
1267 static void capinc_tty_stop(struct tty_struct *tty)
1269 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1270 #ifdef _DEBUG_TTYFUNCS
1271 printk(KERN_DEBUG "capinc_tty_stop\n");
1278 static void capinc_tty_start(struct tty_struct *tty)
1280 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1281 unsigned long flags;
1282 #ifdef _DEBUG_TTYFUNCS
1283 printk(KERN_DEBUG "capinc_tty_start\n");
1286 spin_lock_irqsave(&workaround_lock, flags);
1288 (void)handle_minor_send(mp);
1289 spin_unlock_irqrestore(&workaround_lock, flags);
1293 static void capinc_tty_hangup(struct tty_struct *tty)
1295 #ifdef _DEBUG_TTYFUNCS
1296 printk(KERN_DEBUG "capinc_tty_hangup\n");
1300 static void capinc_tty_break_ctl(struct tty_struct *tty, int state)
1302 #ifdef _DEBUG_TTYFUNCS
1303 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1307 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1309 #ifdef _DEBUG_TTYFUNCS
1310 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1314 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1316 #ifdef _DEBUG_TTYFUNCS
1317 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1321 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1323 #ifdef _DEBUG_TTYFUNCS
1324 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1328 static int capinc_tty_read_proc(char *page, char **start, off_t off,
1329 int count, int *eof, void *data)
1334 static struct tty_driver *capinc_tty_driver;
1336 static const struct tty_operations capinc_ops = {
1337 .open = capinc_tty_open,
1338 .close = capinc_tty_close,
1339 .write = capinc_tty_write,
1340 .put_char = capinc_tty_put_char,
1341 .flush_chars = capinc_tty_flush_chars,
1342 .write_room = capinc_tty_write_room,
1343 .chars_in_buffer = capinc_tty_chars_in_buffer,
1344 .ioctl = capinc_tty_ioctl,
1345 .set_termios = capinc_tty_set_termios,
1346 .throttle = capinc_tty_throttle,
1347 .unthrottle = capinc_tty_unthrottle,
1348 .stop = capinc_tty_stop,
1349 .start = capinc_tty_start,
1350 .hangup = capinc_tty_hangup,
1351 .break_ctl = capinc_tty_break_ctl,
1352 .flush_buffer = capinc_tty_flush_buffer,
1353 .set_ldisc = capinc_tty_set_ldisc,
1354 .send_xchar = capinc_tty_send_xchar,
1355 .read_proc = capinc_tty_read_proc,
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 proc_capidev_read_proc(char *page, char **start, off_t off,
1412 int count, int *eof, void *data)
1414 struct capidev *cdev;
1415 struct list_head *l;
1418 read_lock(&capidev_list_lock);
1419 list_for_each(l, &capidev_list) {
1420 cdev = list_entry(l, struct capidev, list);
1421 len += sprintf(page+len, "0 %d %lu %lu %lu %lu\n",
1423 cdev->ap.nrecvctlpkt,
1424 cdev->ap.nrecvdatapkt,
1425 cdev->ap.nsentctlpkt,
1426 cdev->ap.nsentdatapkt);
1431 if (len-off > count)
1437 read_unlock(&capidev_list_lock);
1440 if (len > count) len = count;
1441 if (len < 0) len = 0;
1446 * /proc/capi/capi20ncci:
1449 static int proc_capincci_read_proc(char *page, char **start, off_t off,
1450 int count, int *eof, void *data)
1452 struct capidev *cdev;
1453 struct capincci *np;
1454 struct list_head *l;
1457 read_lock(&capidev_list_lock);
1458 list_for_each(l, &capidev_list) {
1459 cdev = list_entry(l, struct capidev, list);
1460 for (np=cdev->nccis; np; np = np->next) {
1461 len += sprintf(page+len, "%d 0x%x\n",
1468 if (len-off > count)
1474 read_unlock(&capidev_list_lock);
1478 if (len>count) len = count;
1483 static struct procfsentries {
1486 int (*read_proc)(char *page, char **start, off_t off,
1487 int count, int *eof, void *data);
1488 struct proc_dir_entry *procent;
1489 } procfsentries[] = {
1490 /* { "capi", S_IFDIR, 0 }, */
1491 { "capi/capi20", 0 , proc_capidev_read_proc },
1492 { "capi/capi20ncci", 0 , proc_capincci_read_proc },
1495 static void __init proc_init(void)
1497 int nelem = ARRAY_SIZE(procfsentries);
1500 for (i=0; i < nelem; i++) {
1501 struct procfsentries *p = procfsentries + i;
1502 p->procent = create_proc_entry(p->name, p->mode, NULL);
1503 if (p->procent) p->procent->read_proc = p->read_proc;
1507 static void __exit proc_exit(void)
1509 int nelem = ARRAY_SIZE(procfsentries);
1512 for (i=nelem-1; i >= 0; i--) {
1513 struct procfsentries *p = procfsentries + i;
1515 remove_proc_entry(p->name, NULL);
1521 /* -------- init function and module interface ---------------------- */
1524 static char rev[32];
1526 static int __init capi_init(void)
1532 if ((p = strchr(revision, ':')) != NULL && p[1]) {
1533 strlcpy(rev, p + 2, sizeof(rev));
1534 if ((p = strchr(rev, '$')) != NULL && p > rev)
1539 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1540 if (major_ret < 0) {
1541 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1544 capi_class = class_create(THIS_MODULE, "capi");
1545 if (IS_ERR(capi_class)) {
1546 unregister_chrdev(capi_major, "capi20");
1547 return PTR_ERR(capi_class);
1550 device_create(capi_class, NULL, MKDEV(capi_major, 0), "capi");
1552 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1553 if (capinc_tty_init() < 0) {
1554 device_destroy(capi_class, MKDEV(capi_major, 0));
1555 class_destroy(capi_class);
1556 unregister_chrdev(capi_major, "capi20");
1559 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1563 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1564 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1565 compileinfo = " (middleware+capifs)";
1567 compileinfo = " (no capifs)";
1570 compileinfo = " (no middleware)";
1572 printk(KERN_NOTICE "capi20: Rev %s: started up with major %d%s\n",
1573 rev, capi_major, compileinfo);
1578 static void __exit capi_exit(void)
1582 device_destroy(capi_class, MKDEV(capi_major, 0));
1583 class_destroy(capi_class);
1584 unregister_chrdev(capi_major, "capi20");
1586 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1589 printk(KERN_NOTICE "capi: Rev %s: unloaded\n", rev);
1592 module_init(capi_init);
1593 module_exit(capi_exit);