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/poll.h>
37 #include <linux/capi.h>
38 #include <linux/kernelcapi.h>
39 #include <linux/init.h>
40 #include <linux/device.h>
41 #include <linux/moduleparam.h>
42 #include <linux/isdn/capiutil.h>
43 #include <linux/isdn/capicmd.h>
44 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
48 static char *revision = "$Revision: 1.1.2.7 $";
50 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
51 MODULE_AUTHOR("Carsten Paeth");
52 MODULE_LICENSE("GPL");
54 #undef _DEBUG_REFCOUNT /* alloc/free and open/close debug */
55 #undef _DEBUG_TTYFUNCS /* call to tty_driver */
56 #undef _DEBUG_DATAFLOW /* data flow */
58 /* -------- driver information -------------------------------------- */
60 static struct class *capi_class;
62 static int capi_major = 68; /* allocated */
63 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
64 #define CAPINC_NR_PORTS 32
65 #define CAPINC_MAX_PORTS 256
66 static int capi_ttymajor = 191;
67 static int capi_ttyminors = CAPINC_NR_PORTS;
68 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
70 module_param_named(major, capi_major, uint, 0);
71 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
72 module_param_named(ttymajor, capi_ttymajor, uint, 0);
73 module_param_named(ttyminors, capi_ttyminors, uint, 0);
74 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
76 /* -------- defines ------------------------------------------------- */
78 #define CAPINC_MAX_RECVQUEUE 10
79 #define CAPINC_MAX_SENDQUEUE 10
80 #define CAPI_MAX_BLKSIZE 2048
82 /* -------- data structures ----------------------------------------- */
86 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
89 struct datahandle_queue {
90 struct list_head list;
95 struct list_head list;
96 struct capincci *nccip;
99 struct capi20_appl *ap;
104 struct tty_struct *tty;
107 struct sk_buff *ttyskb;
108 atomic_t ttyopencount;
110 struct sk_buff_head inqueue;
112 struct sk_buff_head outqueue;
116 struct list_head ackqueue;
120 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
122 /* FIXME: The following lock is a sledgehammer-workaround to a
123 * locking issue with the capiminor (and maybe other) data structure(s).
124 * Access to this data is done in a racy way and crashes the machine with
125 * a FritzCard DSL driver; sooner or later. This is a workaround
126 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
127 * The correct (and scalable) fix for the issue seems to require
128 * an API change to the drivers... . */
129 static DEFINE_SPINLOCK(workaround_lock);
132 struct capincci *next;
134 struct capidev *cdev;
135 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
136 struct capiminor *minorp;
137 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
141 struct list_head list;
142 struct capi20_appl ap;
146 struct sk_buff_head recvqueue;
147 wait_queue_head_t recvwait;
149 struct capincci *nccis;
151 struct mutex ncci_list_mtx;
154 /* -------- global variables ---------------------------------------- */
156 static DEFINE_RWLOCK(capidev_list_lock);
157 static LIST_HEAD(capidev_list);
159 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
160 static DEFINE_RWLOCK(capiminor_list_lock);
161 static LIST_HEAD(capiminor_list);
162 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
164 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
165 /* -------- datahandles --------------------------------------------- */
167 static int capincci_add_ack(struct capiminor *mp, u16 datahandle)
169 struct datahandle_queue *n;
172 n = kmalloc(sizeof(*n), GFP_ATOMIC);
174 printk(KERN_ERR "capi: alloc datahandle failed\n");
177 n->datahandle = datahandle;
178 INIT_LIST_HEAD(&n->list);
179 spin_lock_irqsave(&mp->ackqlock, flags);
180 list_add_tail(&n->list, &mp->ackqueue);
182 spin_unlock_irqrestore(&mp->ackqlock, flags);
186 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
188 struct datahandle_queue *p, *tmp;
191 spin_lock_irqsave(&mp->ackqlock, flags);
192 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
193 if (p->datahandle == datahandle) {
197 spin_unlock_irqrestore(&mp->ackqlock, flags);
201 spin_unlock_irqrestore(&mp->ackqlock, flags);
205 static void capiminor_del_all_ack(struct capiminor *mp)
207 struct datahandle_queue *p, *tmp;
210 spin_lock_irqsave(&mp->ackqlock, flags);
211 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
216 spin_unlock_irqrestore(&mp->ackqlock, flags);
220 /* -------- struct capiminor ---------------------------------------- */
222 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
224 struct capiminor *mp, *p;
225 unsigned int minor = 0;
228 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
230 printk(KERN_ERR "capi: can't alloc capiminor\n");
237 atomic_set(&mp->ttyopencount,0);
238 INIT_LIST_HEAD(&mp->ackqueue);
239 spin_lock_init(&mp->ackqlock);
241 skb_queue_head_init(&mp->inqueue);
242 skb_queue_head_init(&mp->outqueue);
244 /* Allocate the least unused minor number.
246 write_lock_irqsave(&capiminor_list_lock, flags);
247 if (list_empty(&capiminor_list))
248 list_add(&mp->list, &capiminor_list);
250 list_for_each_entry(p, &capiminor_list, list) {
251 if (p->minor > minor)
256 if (minor < capi_ttyminors) {
258 list_add(&mp->list, p->list.prev);
261 write_unlock_irqrestore(&capiminor_list_lock, flags);
263 if (!(minor < capi_ttyminors)) {
264 printk(KERN_NOTICE "capi: out of minors\n");
272 static void capiminor_free(struct capiminor *mp)
276 write_lock_irqsave(&capiminor_list_lock, flags);
278 write_unlock_irqrestore(&capiminor_list_lock, flags);
280 if (mp->ttyskb) kfree_skb(mp->ttyskb);
282 skb_queue_purge(&mp->inqueue);
283 skb_queue_purge(&mp->outqueue);
284 capiminor_del_all_ack(mp);
288 static struct capiminor *capiminor_find(unsigned int minor)
291 struct capiminor *p = NULL;
293 read_lock(&capiminor_list_lock);
294 list_for_each(l, &capiminor_list) {
295 p = list_entry(l, struct capiminor, list);
296 if (p->minor == minor)
299 read_unlock(&capiminor_list_lock);
300 if (l == &capiminor_list)
305 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
307 /* -------- struct capincci ----------------------------------------- */
309 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
311 struct capincci *np, **pp;
312 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
313 struct capiminor *mp = NULL;
314 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
316 np = kzalloc(sizeof(*np), GFP_ATOMIC);
321 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
323 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
324 mp = np->minorp = capiminor_alloc(&cdev->ap, ncci);
327 #ifdef _DEBUG_REFCOUNT
328 printk(KERN_DEBUG "set mp->nccip\n");
330 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
331 capifs_new_ncci(mp->minor, MKDEV(capi_ttymajor, mp->minor));
334 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
335 for (pp=&cdev->nccis; *pp; pp = &(*pp)->next)
341 static void capincci_free(struct capidev *cdev, u32 ncci)
343 struct capincci *np, **pp;
344 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
345 struct capiminor *mp;
346 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
351 if (ncci == 0xffffffff || np->ncci == ncci) {
353 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
354 if ((mp = np->minorp) != NULL) {
355 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
356 capifs_free_ncci(mp->minor);
360 #ifdef _DEBUG_REFCOUNT
361 printk(KERN_DEBUG "reset mp->nccip\n");
368 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
370 if (*pp == NULL) return;
377 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
381 for (p=cdev->nccis; p ; p = p->next) {
388 /* -------- struct capidev ------------------------------------------ */
390 static struct capidev *capidev_alloc(void)
392 struct capidev *cdev;
395 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
399 mutex_init(&cdev->ncci_list_mtx);
400 skb_queue_head_init(&cdev->recvqueue);
401 init_waitqueue_head(&cdev->recvwait);
402 write_lock_irqsave(&capidev_list_lock, flags);
403 list_add_tail(&cdev->list, &capidev_list);
404 write_unlock_irqrestore(&capidev_list_lock, flags);
408 static void capidev_free(struct capidev *cdev)
412 if (cdev->ap.applid) {
413 capi20_release(&cdev->ap);
416 skb_queue_purge(&cdev->recvqueue);
418 mutex_lock(&cdev->ncci_list_mtx);
419 capincci_free(cdev, 0xffffffff);
420 mutex_unlock(&cdev->ncci_list_mtx);
422 write_lock_irqsave(&capidev_list_lock, flags);
423 list_del(&cdev->list);
424 write_unlock_irqrestore(&capidev_list_lock, flags);
428 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
429 /* -------- handle data queue --------------------------------------- */
431 static struct sk_buff *
432 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
434 struct sk_buff *nskb;
435 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
437 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
438 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
439 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
440 capimsg_setu16(s, 2, mp->ap->applid);
441 capimsg_setu8 (s, 4, CAPI_DATA_B3);
442 capimsg_setu8 (s, 5, CAPI_RESP);
443 capimsg_setu16(s, 6, mp->msgid++);
444 capimsg_setu32(s, 8, mp->ncci);
445 capimsg_setu16(s, 12, datahandle);
450 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
452 struct sk_buff *nskb;
454 u16 errcode, datahandle;
455 struct tty_ldisc *ld;
457 datalen = skb->len - CAPIMSG_LEN(skb->data);
460 #ifdef _DEBUG_DATAFLOW
461 printk(KERN_DEBUG "capi: currently no receiver\n");
466 ld = tty_ldisc_ref(mp->tty);
469 if (ld->receive_buf == NULL) {
470 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
471 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
476 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
477 printk(KERN_DEBUG "capi: recv tty throttled\n");
481 if (mp->tty->receive_room < datalen) {
482 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
483 printk(KERN_DEBUG "capi: no room in tty\n");
487 if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
488 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
491 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
492 errcode = capi20_put_message(mp->ap, nskb);
493 if (errcode != CAPI_NOERROR) {
494 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
499 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
500 #ifdef _DEBUG_DATAFLOW
501 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
502 datahandle, skb->len);
504 ld->receive_buf(mp->tty, skb->data, NULL, skb->len);
513 static void handle_minor_recv(struct capiminor *mp)
516 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
517 unsigned int len = skb->len;
519 if (handle_recv_skb(mp, skb) < 0) {
520 skb_queue_head(&mp->inqueue, skb);
527 static int handle_minor_send(struct capiminor *mp)
535 if (mp->tty && mp->ttyoutstop) {
536 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
537 printk(KERN_DEBUG "capi: send: tty stopped\n");
542 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
543 datahandle = mp->datahandle;
545 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
546 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
547 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
548 capimsg_setu16(skb->data, 2, mp->ap->applid);
549 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
550 capimsg_setu8 (skb->data, 5, CAPI_REQ);
551 capimsg_setu16(skb->data, 6, mp->msgid++);
552 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
553 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
554 capimsg_setu16(skb->data, 16, len); /* Data length */
555 capimsg_setu16(skb->data, 18, datahandle);
556 capimsg_setu16(skb->data, 20, 0); /* Flags */
558 if (capincci_add_ack(mp, datahandle) < 0) {
559 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
560 skb_queue_head(&mp->outqueue, skb);
563 errcode = capi20_put_message(mp->ap, skb);
564 if (errcode == CAPI_NOERROR) {
568 #ifdef _DEBUG_DATAFLOW
569 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
574 capiminor_del_ack(mp, datahandle);
576 if (errcode == CAPI_SENDQUEUEFULL) {
577 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
578 skb_queue_head(&mp->outqueue, skb);
582 /* ups, drop packet */
583 printk(KERN_ERR "capi: put_message = %x\n", errcode);
590 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
591 /* -------- function called by lower level -------------------------- */
593 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
595 struct capidev *cdev = ap->private;
596 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
597 struct capiminor *mp;
599 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
604 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
605 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
607 mutex_lock(&cdev->ncci_list_mtx);
608 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
609 mutex_unlock(&cdev->ncci_list_mtx);
612 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND) {
613 mutex_lock(&cdev->ncci_list_mtx);
614 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
615 mutex_unlock(&cdev->ncci_list_mtx);
617 spin_lock_irqsave(&workaround_lock, flags);
618 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
619 skb_queue_tail(&cdev->recvqueue, skb);
620 wake_up_interruptible(&cdev->recvwait);
621 spin_unlock_irqrestore(&workaround_lock, flags);
624 ncci = CAPIMSG_CONTROL(skb->data);
625 for (np = cdev->nccis; np && np->ncci != ncci; np = np->next)
628 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
629 skb_queue_tail(&cdev->recvqueue, skb);
630 wake_up_interruptible(&cdev->recvwait);
631 spin_unlock_irqrestore(&workaround_lock, flags);
634 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
635 skb_queue_tail(&cdev->recvqueue, skb);
636 wake_up_interruptible(&cdev->recvwait);
637 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
640 skb_queue_tail(&cdev->recvqueue, skb);
641 wake_up_interruptible(&cdev->recvwait);
642 spin_unlock_irqrestore(&workaround_lock, flags);
647 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
649 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
650 #ifdef _DEBUG_DATAFLOW
651 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
652 datahandle, skb->len-CAPIMSG_LEN(skb->data));
654 skb_queue_tail(&mp->inqueue, skb);
655 mp->inbytes += skb->len;
656 handle_minor_recv(mp);
658 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
660 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
661 #ifdef _DEBUG_DATAFLOW
662 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
664 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
667 (void)capiminor_del_ack(mp, datahandle);
670 (void)handle_minor_send(mp);
673 /* ups, let capi application handle it :-) */
674 skb_queue_tail(&cdev->recvqueue, skb);
675 wake_up_interruptible(&cdev->recvwait);
677 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
678 spin_unlock_irqrestore(&workaround_lock, flags);
681 /* -------- file_operations for capidev ----------------------------- */
684 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
686 struct capidev *cdev = (struct capidev *)file->private_data;
690 if (!cdev->ap.applid)
693 if ((skb = skb_dequeue(&cdev->recvqueue)) == NULL) {
695 if (file->f_flags & O_NONBLOCK)
699 interruptible_sleep_on(&cdev->recvwait);
700 if ((skb = skb_dequeue(&cdev->recvqueue)) != NULL)
702 if (signal_pending(current))
706 return -ERESTARTNOHAND;
708 if (skb->len > count) {
709 skb_queue_head(&cdev->recvqueue, skb);
712 if (copy_to_user(buf, skb->data, skb->len)) {
713 skb_queue_head(&cdev->recvqueue, skb);
724 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
726 struct capidev *cdev = (struct capidev *)file->private_data;
730 if (!cdev->ap.applid)
733 skb = alloc_skb(count, GFP_USER);
737 if (copy_from_user(skb_put(skb, count), buf, count)) {
741 mlen = CAPIMSG_LEN(skb->data);
742 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
743 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
753 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
755 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
756 mutex_lock(&cdev->ncci_list_mtx);
757 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
758 mutex_unlock(&cdev->ncci_list_mtx);
761 cdev->errcode = capi20_put_message(&cdev->ap, skb);
771 capi_poll(struct file *file, poll_table * wait)
773 struct capidev *cdev = (struct capidev *)file->private_data;
774 unsigned int mask = 0;
776 if (!cdev->ap.applid)
779 poll_wait(file, &(cdev->recvwait), wait);
780 mask = POLLOUT | POLLWRNORM;
781 if (!skb_queue_empty(&cdev->recvqueue))
782 mask |= POLLIN | POLLRDNORM;
787 capi_ioctl(struct inode *inode, struct file *file,
788 unsigned int cmd, unsigned long arg)
790 struct capidev *cdev = file->private_data;
791 struct capi20_appl *ap = &cdev->ap;
792 capi_ioctl_struct data;
793 int retval = -EINVAL;
794 void __user *argp = (void __user *)arg;
802 if (copy_from_user(&cdev->ap.rparam, argp,
803 sizeof(struct capi_register_params)))
806 cdev->ap.private = cdev;
807 cdev->ap.recv_message = capi_recv_message;
808 cdev->errcode = capi20_register(ap);
814 return (int)ap->applid;
816 case CAPI_GET_VERSION:
818 if (copy_from_user(&data.contr, argp,
821 cdev->errcode = capi20_get_version(data.contr, &data.version);
824 if (copy_to_user(argp, &data.version,
825 sizeof(data.version)))
830 case CAPI_GET_SERIAL:
832 if (copy_from_user(&data.contr, argp,
835 cdev->errcode = capi20_get_serial (data.contr, data.serial);
838 if (copy_to_user(argp, data.serial,
839 sizeof(data.serial)))
843 case CAPI_GET_PROFILE:
845 if (copy_from_user(&data.contr, argp,
849 if (data.contr == 0) {
850 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
854 retval = copy_to_user(argp,
855 &data.profile.ncontroller,
856 sizeof(data.profile.ncontroller));
859 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
863 retval = copy_to_user(argp, &data.profile,
864 sizeof(data.profile));
871 case CAPI_GET_MANUFACTURER:
873 if (copy_from_user(&data.contr, argp,
876 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
880 if (copy_to_user(argp, data.manufacturer,
881 sizeof(data.manufacturer)))
886 case CAPI_GET_ERRCODE:
887 data.errcode = cdev->errcode;
888 cdev->errcode = CAPI_NOERROR;
890 if (copy_to_user(argp, &data.errcode,
891 sizeof(data.errcode)))
897 if (capi20_isinstalled() == CAPI_NOERROR)
901 case CAPI_MANUFACTURER_CMD:
903 struct capi_manufacturer_cmd mcmd;
904 if (!capable(CAP_SYS_ADMIN))
906 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
908 return capi20_manufacturer(mcmd.cmd, mcmd.data);
916 if (copy_from_user(&userflags, argp,
919 if (cmd == CAPI_SET_FLAGS)
920 cdev->userflags |= userflags;
922 cdev->userflags &= ~userflags;
927 if (copy_to_user(argp, &cdev->userflags,
928 sizeof(cdev->userflags)))
932 case CAPI_NCCI_OPENCOUNT:
934 struct capincci *nccip;
935 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
936 struct capiminor *mp;
937 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
940 if (copy_from_user(&ncci, argp, sizeof(ncci)))
943 mutex_lock(&cdev->ncci_list_mtx);
944 if ((nccip = capincci_find(cdev, (u32) ncci)) == NULL) {
945 mutex_unlock(&cdev->ncci_list_mtx);
948 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
949 if ((mp = nccip->minorp) != NULL) {
950 count += atomic_read(&mp->ttyopencount);
952 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
953 mutex_unlock(&cdev->ncci_list_mtx);
958 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
959 case CAPI_NCCI_GETUNIT:
961 struct capincci *nccip;
962 struct capiminor *mp;
965 if (copy_from_user(&ncci, argp,
968 mutex_lock(&cdev->ncci_list_mtx);
969 nccip = capincci_find(cdev, (u32) ncci);
970 if (!nccip || (mp = nccip->minorp) == NULL) {
971 mutex_unlock(&cdev->ncci_list_mtx);
975 mutex_unlock(&cdev->ncci_list_mtx);
979 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
985 capi_open(struct inode *inode, struct file *file)
990 if (file->private_data)
992 else if ((file->private_data = capidev_alloc()) == NULL)
995 ret = nonseekable_open(inode, file);
1001 capi_release(struct inode *inode, struct file *file)
1003 struct capidev *cdev = (struct capidev *)file->private_data;
1006 file->private_data = NULL;
1011 static const struct file_operations capi_fops =
1013 .owner = THIS_MODULE,
1014 .llseek = no_llseek,
1016 .write = capi_write,
1018 .ioctl = capi_ioctl,
1020 .release = capi_release,
1023 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1024 /* -------- tty_operations for capincci ----------------------------- */
1026 static int capinc_tty_open(struct tty_struct * tty, struct file * file)
1028 struct capiminor *mp;
1029 unsigned long flags;
1031 if ((mp = capiminor_find(iminor(file->f_path.dentry->d_inode))) == NULL)
1033 if (mp->nccip == NULL)
1036 tty->driver_data = (void *)mp;
1038 spin_lock_irqsave(&workaround_lock, flags);
1039 if (atomic_read(&mp->ttyopencount) == 0)
1041 atomic_inc(&mp->ttyopencount);
1042 #ifdef _DEBUG_REFCOUNT
1043 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1045 handle_minor_recv(mp);
1046 spin_unlock_irqrestore(&workaround_lock, flags);
1050 static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1052 struct capiminor *mp;
1054 mp = (struct capiminor *)tty->driver_data;
1056 if (atomic_dec_and_test(&mp->ttyopencount)) {
1057 #ifdef _DEBUG_REFCOUNT
1058 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1060 tty->driver_data = NULL;
1063 #ifdef _DEBUG_REFCOUNT
1064 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1066 if (mp->nccip == NULL)
1070 #ifdef _DEBUG_REFCOUNT
1071 printk(KERN_DEBUG "capinc_tty_close\n");
1075 static int capinc_tty_write(struct tty_struct * tty,
1076 const unsigned char *buf, int count)
1078 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1079 struct sk_buff *skb;
1080 unsigned long flags;
1082 #ifdef _DEBUG_TTYFUNCS
1083 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1086 if (!mp || !mp->nccip) {
1087 #ifdef _DEBUG_TTYFUNCS
1088 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1093 spin_lock_irqsave(&workaround_lock, flags);
1097 skb_queue_tail(&mp->outqueue, skb);
1098 mp->outbytes += skb->len;
1101 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1103 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1104 spin_unlock_irqrestore(&workaround_lock, flags);
1108 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1109 memcpy(skb_put(skb, count), buf, count);
1111 skb_queue_tail(&mp->outqueue, skb);
1112 mp->outbytes += skb->len;
1113 (void)handle_minor_send(mp);
1114 (void)handle_minor_recv(mp);
1115 spin_unlock_irqrestore(&workaround_lock, flags);
1119 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1121 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1122 struct sk_buff *skb;
1123 unsigned long flags;
1126 #ifdef _DEBUG_TTYFUNCS
1127 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1130 if (!mp || !mp->nccip) {
1131 #ifdef _DEBUG_TTYFUNCS
1132 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1137 spin_lock_irqsave(&workaround_lock, flags);
1140 if (skb_tailroom(skb) > 0) {
1141 *(skb_put(skb, 1)) = ch;
1142 spin_unlock_irqrestore(&workaround_lock, flags);
1146 skb_queue_tail(&mp->outqueue, skb);
1147 mp->outbytes += skb->len;
1148 (void)handle_minor_send(mp);
1150 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1152 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1153 *(skb_put(skb, 1)) = ch;
1156 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1159 spin_unlock_irqrestore(&workaround_lock, flags);
1163 static void capinc_tty_flush_chars(struct tty_struct *tty)
1165 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1166 struct sk_buff *skb;
1167 unsigned long flags;
1169 #ifdef _DEBUG_TTYFUNCS
1170 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1173 if (!mp || !mp->nccip) {
1174 #ifdef _DEBUG_TTYFUNCS
1175 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1180 spin_lock_irqsave(&workaround_lock, flags);
1184 skb_queue_tail(&mp->outqueue, skb);
1185 mp->outbytes += skb->len;
1186 (void)handle_minor_send(mp);
1188 (void)handle_minor_recv(mp);
1189 spin_unlock_irqrestore(&workaround_lock, flags);
1192 static int capinc_tty_write_room(struct tty_struct *tty)
1194 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1196 if (!mp || !mp->nccip) {
1197 #ifdef _DEBUG_TTYFUNCS
1198 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1202 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1203 room *= CAPI_MAX_BLKSIZE;
1204 #ifdef _DEBUG_TTYFUNCS
1205 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1210 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1212 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1213 if (!mp || !mp->nccip) {
1214 #ifdef _DEBUG_TTYFUNCS
1215 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1219 #ifdef _DEBUG_TTYFUNCS
1220 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1221 mp->outbytes, mp->nack,
1222 skb_queue_len(&mp->outqueue),
1223 skb_queue_len(&mp->inqueue));
1225 return mp->outbytes;
1228 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1229 unsigned int cmd, unsigned long arg)
1234 error = n_tty_ioctl (tty, file, cmd, arg);
1240 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
1242 #ifdef _DEBUG_TTYFUNCS
1243 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1247 static void capinc_tty_throttle(struct tty_struct * tty)
1249 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1250 #ifdef _DEBUG_TTYFUNCS
1251 printk(KERN_DEBUG "capinc_tty_throttle\n");
1257 static void capinc_tty_unthrottle(struct tty_struct * tty)
1259 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1260 unsigned long flags;
1261 #ifdef _DEBUG_TTYFUNCS
1262 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1265 spin_lock_irqsave(&workaround_lock, flags);
1267 handle_minor_recv(mp);
1268 spin_unlock_irqrestore(&workaround_lock, flags);
1272 static void capinc_tty_stop(struct tty_struct *tty)
1274 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1275 #ifdef _DEBUG_TTYFUNCS
1276 printk(KERN_DEBUG "capinc_tty_stop\n");
1283 static void capinc_tty_start(struct tty_struct *tty)
1285 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1286 unsigned long flags;
1287 #ifdef _DEBUG_TTYFUNCS
1288 printk(KERN_DEBUG "capinc_tty_start\n");
1291 spin_lock_irqsave(&workaround_lock, flags);
1293 (void)handle_minor_send(mp);
1294 spin_unlock_irqrestore(&workaround_lock, flags);
1298 static void capinc_tty_hangup(struct tty_struct *tty)
1300 #ifdef _DEBUG_TTYFUNCS
1301 printk(KERN_DEBUG "capinc_tty_hangup\n");
1305 static void capinc_tty_break_ctl(struct tty_struct *tty, int state)
1307 #ifdef _DEBUG_TTYFUNCS
1308 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1312 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1314 #ifdef _DEBUG_TTYFUNCS
1315 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1319 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1321 #ifdef _DEBUG_TTYFUNCS
1322 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1326 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1328 #ifdef _DEBUG_TTYFUNCS
1329 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1333 static int capinc_tty_read_proc(char *page, char **start, off_t off,
1334 int count, int *eof, void *data)
1339 static struct tty_driver *capinc_tty_driver;
1341 static const struct tty_operations capinc_ops = {
1342 .open = capinc_tty_open,
1343 .close = capinc_tty_close,
1344 .write = capinc_tty_write,
1345 .put_char = capinc_tty_put_char,
1346 .flush_chars = capinc_tty_flush_chars,
1347 .write_room = capinc_tty_write_room,
1348 .chars_in_buffer = capinc_tty_chars_in_buffer,
1349 .ioctl = capinc_tty_ioctl,
1350 .set_termios = capinc_tty_set_termios,
1351 .throttle = capinc_tty_throttle,
1352 .unthrottle = capinc_tty_unthrottle,
1353 .stop = capinc_tty_stop,
1354 .start = capinc_tty_start,
1355 .hangup = capinc_tty_hangup,
1356 .break_ctl = capinc_tty_break_ctl,
1357 .flush_buffer = capinc_tty_flush_buffer,
1358 .set_ldisc = capinc_tty_set_ldisc,
1359 .send_xchar = capinc_tty_send_xchar,
1360 .read_proc = capinc_tty_read_proc,
1363 static int capinc_tty_init(void)
1365 struct tty_driver *drv;
1367 if (capi_ttyminors > CAPINC_MAX_PORTS)
1368 capi_ttyminors = CAPINC_MAX_PORTS;
1369 if (capi_ttyminors <= 0)
1370 capi_ttyminors = CAPINC_NR_PORTS;
1372 drv = alloc_tty_driver(capi_ttyminors);
1376 drv->owner = THIS_MODULE;
1377 drv->driver_name = "capi_nc";
1379 drv->major = capi_ttymajor;
1380 drv->minor_start = 0;
1381 drv->type = TTY_DRIVER_TYPE_SERIAL;
1382 drv->subtype = SERIAL_TYPE_NORMAL;
1383 drv->init_termios = tty_std_termios;
1384 drv->init_termios.c_iflag = ICRNL;
1385 drv->init_termios.c_oflag = OPOST | ONLCR;
1386 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1387 drv->init_termios.c_lflag = 0;
1388 drv->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_RESET_TERMIOS;
1389 tty_set_operations(drv, &capinc_ops);
1390 if (tty_register_driver(drv)) {
1391 put_tty_driver(drv);
1392 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1395 capinc_tty_driver = drv;
1399 static void capinc_tty_exit(void)
1401 struct tty_driver *drv = capinc_tty_driver;
1403 if ((retval = tty_unregister_driver(drv)))
1404 printk(KERN_ERR "capi: failed to unregister capi_nc driver (%d)\n", retval);
1405 put_tty_driver(drv);
1408 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1410 /* -------- /proc functions ----------------------------------------- */
1413 * /proc/capi/capi20:
1414 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1416 static int proc_capidev_read_proc(char *page, char **start, off_t off,
1417 int count, int *eof, void *data)
1419 struct capidev *cdev;
1420 struct list_head *l;
1423 read_lock(&capidev_list_lock);
1424 list_for_each(l, &capidev_list) {
1425 cdev = list_entry(l, struct capidev, list);
1426 len += sprintf(page+len, "0 %d %lu %lu %lu %lu\n",
1428 cdev->ap.nrecvctlpkt,
1429 cdev->ap.nrecvdatapkt,
1430 cdev->ap.nsentctlpkt,
1431 cdev->ap.nsentdatapkt);
1436 if (len-off > count)
1442 read_unlock(&capidev_list_lock);
1445 if (len > count) len = count;
1446 if (len < 0) len = 0;
1451 * /proc/capi/capi20ncci:
1454 static int proc_capincci_read_proc(char *page, char **start, off_t off,
1455 int count, int *eof, void *data)
1457 struct capidev *cdev;
1458 struct capincci *np;
1459 struct list_head *l;
1462 read_lock(&capidev_list_lock);
1463 list_for_each(l, &capidev_list) {
1464 cdev = list_entry(l, struct capidev, list);
1465 for (np=cdev->nccis; np; np = np->next) {
1466 len += sprintf(page+len, "%d 0x%x\n",
1473 if (len-off > count)
1479 read_unlock(&capidev_list_lock);
1483 if (len>count) len = count;
1488 static struct procfsentries {
1491 int (*read_proc)(char *page, char **start, off_t off,
1492 int count, int *eof, void *data);
1493 struct proc_dir_entry *procent;
1494 } procfsentries[] = {
1495 /* { "capi", S_IFDIR, 0 }, */
1496 { "capi/capi20", 0 , proc_capidev_read_proc },
1497 { "capi/capi20ncci", 0 , proc_capincci_read_proc },
1500 static void __init proc_init(void)
1502 int nelem = ARRAY_SIZE(procfsentries);
1505 for (i=0; i < nelem; i++) {
1506 struct procfsentries *p = procfsentries + i;
1507 p->procent = create_proc_entry(p->name, p->mode, NULL);
1508 if (p->procent) p->procent->read_proc = p->read_proc;
1512 static void __exit proc_exit(void)
1514 int nelem = ARRAY_SIZE(procfsentries);
1517 for (i=nelem-1; i >= 0; i--) {
1518 struct procfsentries *p = procfsentries + i;
1520 remove_proc_entry(p->name, NULL);
1526 /* -------- init function and module interface ---------------------- */
1529 static char rev[32];
1531 static int __init capi_init(void)
1537 if ((p = strchr(revision, ':')) != NULL && p[1]) {
1538 strlcpy(rev, p + 2, sizeof(rev));
1539 if ((p = strchr(rev, '$')) != NULL && p > rev)
1544 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1545 if (major_ret < 0) {
1546 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1549 capi_class = class_create(THIS_MODULE, "capi");
1550 if (IS_ERR(capi_class)) {
1551 unregister_chrdev(capi_major, "capi20");
1552 return PTR_ERR(capi_class);
1555 device_create(capi_class, NULL, MKDEV(capi_major, 0), "capi");
1557 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1558 if (capinc_tty_init() < 0) {
1559 device_destroy(capi_class, MKDEV(capi_major, 0));
1560 class_destroy(capi_class);
1561 unregister_chrdev(capi_major, "capi20");
1564 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1568 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1569 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1570 compileinfo = " (middleware+capifs)";
1572 compileinfo = " (no capifs)";
1575 compileinfo = " (no middleware)";
1577 printk(KERN_NOTICE "capi20: Rev %s: started up with major %d%s\n",
1578 rev, capi_major, compileinfo);
1583 static void __exit capi_exit(void)
1587 device_destroy(capi_class, MKDEV(capi_major, 0));
1588 class_destroy(capi_class);
1589 unregister_chrdev(capi_major, "capi20");
1591 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1594 printk(KERN_NOTICE "capi: Rev %s: unloaded\n", rev);
1597 module_init(capi_init);
1598 module_exit(capi_exit);