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/config.h>
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <linux/major.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/fcntl.h>
21 #include <linux/signal.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/devfs_fs_kernel.h>
43 #include <linux/isdn/capiutil.h>
44 #include <linux/isdn/capicmd.h>
45 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
49 static char *revision = "$Revision: 1.1.2.7 $";
51 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
52 MODULE_AUTHOR("Carsten Paeth");
53 MODULE_LICENSE("GPL");
55 #undef _DEBUG_REFCOUNT /* alloc/free and open/close debug */
56 #undef _DEBUG_TTYFUNCS /* call to tty_driver */
57 #undef _DEBUG_DATAFLOW /* data flow */
59 /* -------- driver information -------------------------------------- */
61 static struct class *capi_class;
63 static int capi_major = 68; /* allocated */
64 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
65 #define CAPINC_NR_PORTS 32
66 #define CAPINC_MAX_PORTS 256
67 static int capi_ttymajor = 191;
68 static int capi_ttyminors = CAPINC_NR_PORTS;
69 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
71 module_param_named(major, capi_major, uint, 0);
72 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
73 module_param_named(ttymajor, capi_ttymajor, uint, 0);
74 module_param_named(ttyminors, capi_ttyminors, uint, 0);
75 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
77 /* -------- defines ------------------------------------------------- */
79 #define CAPINC_MAX_RECVQUEUE 10
80 #define CAPINC_MAX_SENDQUEUE 10
81 #define CAPI_MAX_BLKSIZE 2048
83 /* -------- data structures ----------------------------------------- */
87 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
91 struct list_head list;
92 struct capincci *nccip;
95 struct capi20_appl *ap;
100 struct tty_struct *tty;
103 struct sk_buff *ttyskb;
104 atomic_t ttyopencount;
106 struct sk_buff_head inqueue;
108 struct sk_buff_head outqueue;
112 struct datahandle_queue {
113 struct datahandle_queue *next;
119 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
122 struct capincci *next;
124 struct capidev *cdev;
125 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
126 struct capiminor *minorp;
127 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
131 struct list_head list;
132 struct capi20_appl ap;
136 struct sk_buff_head recvqueue;
137 wait_queue_head_t recvwait;
139 struct capincci *nccis;
141 struct semaphore ncci_list_sem;
144 /* -------- global variables ---------------------------------------- */
146 static DEFINE_RWLOCK(capidev_list_lock);
147 static LIST_HEAD(capidev_list);
149 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
150 static DEFINE_RWLOCK(capiminor_list_lock);
151 static LIST_HEAD(capiminor_list);
152 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
154 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
155 /* -------- datahandles --------------------------------------------- */
157 static int capincci_add_ack(struct capiminor *mp, u16 datahandle)
159 struct datahandle_queue *n, **pp;
161 n = kmalloc(sizeof(*n), GFP_ATOMIC);
163 printk(KERN_ERR "capi: alloc datahandle failed\n");
167 n->datahandle = datahandle;
168 for (pp = &mp->ackqueue; *pp; pp = &(*pp)->next) ;
174 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
176 struct datahandle_queue **pp, *p;
178 for (pp = &mp->ackqueue; *pp; pp = &(*pp)->next) {
179 if ((*pp)->datahandle == datahandle) {
190 static void capiminor_del_all_ack(struct capiminor *mp)
192 struct datahandle_queue **pp, *p;
204 /* -------- struct capiminor ---------------------------------------- */
206 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
208 struct capiminor *mp, *p;
209 unsigned int minor = 0;
212 mp = kmalloc(sizeof(*mp), GFP_ATOMIC);
214 printk(KERN_ERR "capi: can't alloc capiminor\n");
218 memset(mp, 0, sizeof(struct capiminor));
222 atomic_set(&mp->ttyopencount,0);
224 skb_queue_head_init(&mp->inqueue);
225 skb_queue_head_init(&mp->outqueue);
227 /* Allocate the least unused minor number.
229 write_lock_irqsave(&capiminor_list_lock, flags);
230 if (list_empty(&capiminor_list))
231 list_add(&mp->list, &capiminor_list);
233 list_for_each_entry(p, &capiminor_list, list) {
234 if (p->minor > minor)
239 if (minor < capi_ttyminors) {
241 list_add(&mp->list, p->list.prev);
244 write_unlock_irqrestore(&capiminor_list_lock, flags);
246 if (!(minor < capi_ttyminors)) {
247 printk(KERN_NOTICE "capi: out of minors\n");
255 static void capiminor_free(struct capiminor *mp)
259 write_lock_irqsave(&capiminor_list_lock, flags);
261 write_unlock_irqrestore(&capiminor_list_lock, flags);
263 if (mp->ttyskb) kfree_skb(mp->ttyskb);
265 skb_queue_purge(&mp->inqueue);
266 skb_queue_purge(&mp->outqueue);
267 capiminor_del_all_ack(mp);
271 static struct capiminor *capiminor_find(unsigned int minor)
274 struct capiminor *p = NULL;
276 read_lock(&capiminor_list_lock);
277 list_for_each(l, &capiminor_list) {
278 p = list_entry(l, struct capiminor, list);
279 if (p->minor == minor)
282 read_unlock(&capiminor_list_lock);
283 if (l == &capiminor_list)
288 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
290 /* -------- struct capincci ----------------------------------------- */
292 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
294 struct capincci *np, **pp;
295 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
296 struct capiminor *mp = NULL;
297 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
299 np = kmalloc(sizeof(*np), GFP_ATOMIC);
302 memset(np, 0, sizeof(struct capincci));
305 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
307 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
308 mp = np->minorp = capiminor_alloc(&cdev->ap, ncci);
311 #ifdef _DEBUG_REFCOUNT
312 printk(KERN_DEBUG "set mp->nccip\n");
314 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
315 capifs_new_ncci(mp->minor, MKDEV(capi_ttymajor, mp->minor));
318 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
319 for (pp=&cdev->nccis; *pp; pp = &(*pp)->next)
325 static void capincci_free(struct capidev *cdev, u32 ncci)
327 struct capincci *np, **pp;
328 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
329 struct capiminor *mp;
330 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
335 if (ncci == 0xffffffff || np->ncci == ncci) {
337 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
338 if ((mp = np->minorp) != 0) {
339 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
340 capifs_free_ncci(mp->minor);
344 #ifdef _DEBUG_REFCOUNT
345 printk(KERN_DEBUG "reset mp->nccip\n");
352 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
354 if (*pp == 0) return;
361 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
365 for (p=cdev->nccis; p ; p = p->next) {
372 /* -------- struct capidev ------------------------------------------ */
374 static struct capidev *capidev_alloc(void)
376 struct capidev *cdev;
379 cdev = kmalloc(sizeof(*cdev), GFP_KERNEL);
382 memset(cdev, 0, sizeof(struct capidev));
384 init_MUTEX(&cdev->ncci_list_sem);
385 skb_queue_head_init(&cdev->recvqueue);
386 init_waitqueue_head(&cdev->recvwait);
387 write_lock_irqsave(&capidev_list_lock, flags);
388 list_add_tail(&cdev->list, &capidev_list);
389 write_unlock_irqrestore(&capidev_list_lock, flags);
393 static void capidev_free(struct capidev *cdev)
397 if (cdev->ap.applid) {
398 capi20_release(&cdev->ap);
401 skb_queue_purge(&cdev->recvqueue);
403 down(&cdev->ncci_list_sem);
404 capincci_free(cdev, 0xffffffff);
405 up(&cdev->ncci_list_sem);
407 write_lock_irqsave(&capidev_list_lock, flags);
408 list_del(&cdev->list);
409 write_unlock_irqrestore(&capidev_list_lock, flags);
413 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
414 /* -------- handle data queue --------------------------------------- */
416 static struct sk_buff *
417 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
419 struct sk_buff *nskb;
420 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
422 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
423 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
424 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
425 capimsg_setu16(s, 2, mp->ap->applid);
426 capimsg_setu8 (s, 4, CAPI_DATA_B3);
427 capimsg_setu8 (s, 5, CAPI_RESP);
428 capimsg_setu16(s, 6, mp->msgid++);
429 capimsg_setu32(s, 8, mp->ncci);
430 capimsg_setu16(s, 12, datahandle);
435 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
437 struct sk_buff *nskb;
439 u16 errcode, datahandle;
440 struct tty_ldisc *ld;
442 datalen = skb->len - CAPIMSG_LEN(skb->data);
445 #ifdef _DEBUG_DATAFLOW
446 printk(KERN_DEBUG "capi: currently no receiver\n");
451 ld = tty_ldisc_ref(mp->tty);
454 if (ld->receive_buf == NULL) {
455 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
456 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
461 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
462 printk(KERN_DEBUG "capi: recv tty throttled\n");
466 if (mp->tty->receive_room < datalen) {
467 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
468 printk(KERN_DEBUG "capi: no room in tty\n");
472 if ((nskb = gen_data_b3_resp_for(mp, skb)) == 0) {
473 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
476 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
477 errcode = capi20_put_message(mp->ap, nskb);
478 if (errcode != CAPI_NOERROR) {
479 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
484 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
485 #ifdef _DEBUG_DATAFLOW
486 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
487 datahandle, skb->len);
489 ld->receive_buf(mp->tty, skb->data, NULL, skb->len);
498 static void handle_minor_recv(struct capiminor *mp)
501 while ((skb = skb_dequeue(&mp->inqueue)) != 0) {
502 unsigned int len = skb->len;
504 if (handle_recv_skb(mp, skb) < 0) {
505 skb_queue_head(&mp->inqueue, skb);
512 static int handle_minor_send(struct capiminor *mp)
520 if (mp->tty && mp->ttyoutstop) {
521 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
522 printk(KERN_DEBUG "capi: send: tty stopped\n");
527 while ((skb = skb_dequeue(&mp->outqueue)) != 0) {
528 datahandle = mp->datahandle;
530 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
531 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
532 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
533 capimsg_setu16(skb->data, 2, mp->ap->applid);
534 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
535 capimsg_setu8 (skb->data, 5, CAPI_REQ);
536 capimsg_setu16(skb->data, 6, mp->msgid++);
537 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
538 capimsg_setu32(skb->data, 12, (u32) skb->data); /* Data32 */
539 capimsg_setu16(skb->data, 16, len); /* Data length */
540 capimsg_setu16(skb->data, 18, datahandle);
541 capimsg_setu16(skb->data, 20, 0); /* Flags */
543 if (capincci_add_ack(mp, datahandle) < 0) {
544 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
545 skb_queue_head(&mp->outqueue, skb);
548 errcode = capi20_put_message(mp->ap, skb);
549 if (errcode == CAPI_NOERROR) {
553 #ifdef _DEBUG_DATAFLOW
554 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
559 capiminor_del_ack(mp, datahandle);
561 if (errcode == CAPI_SENDQUEUEFULL) {
562 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
563 skb_queue_head(&mp->outqueue, skb);
567 /* ups, drop packet */
568 printk(KERN_ERR "capi: put_message = %x\n", errcode);
575 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
576 /* -------- function called by lower level -------------------------- */
578 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
580 struct capidev *cdev = ap->private;
581 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
582 struct capiminor *mp;
584 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
588 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
589 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
591 down(&cdev->ncci_list_sem);
592 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
593 up(&cdev->ncci_list_sem);
596 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND) {
597 down(&cdev->ncci_list_sem);
598 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
599 up(&cdev->ncci_list_sem);
601 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
602 skb_queue_tail(&cdev->recvqueue, skb);
603 wake_up_interruptible(&cdev->recvwait);
606 ncci = CAPIMSG_CONTROL(skb->data);
607 for (np = cdev->nccis; np && np->ncci != ncci; np = np->next)
610 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
611 skb_queue_tail(&cdev->recvqueue, skb);
612 wake_up_interruptible(&cdev->recvwait);
615 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
616 skb_queue_tail(&cdev->recvqueue, skb);
617 wake_up_interruptible(&cdev->recvwait);
618 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
621 skb_queue_tail(&cdev->recvqueue, skb);
622 wake_up_interruptible(&cdev->recvwait);
627 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
629 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
630 #ifdef _DEBUG_DATAFLOW
631 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
632 datahandle, skb->len-CAPIMSG_LEN(skb->data));
634 skb_queue_tail(&mp->inqueue, skb);
635 mp->inbytes += skb->len;
636 handle_minor_recv(mp);
638 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
640 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
641 #ifdef _DEBUG_DATAFLOW
642 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
644 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
647 (void)capiminor_del_ack(mp, datahandle);
650 (void)handle_minor_send(mp);
653 /* ups, let capi application handle it :-) */
654 skb_queue_tail(&cdev->recvqueue, skb);
655 wake_up_interruptible(&cdev->recvwait);
657 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
660 /* -------- file_operations for capidev ----------------------------- */
663 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
665 struct capidev *cdev = (struct capidev *)file->private_data;
669 if (!cdev->ap.applid)
672 if ((skb = skb_dequeue(&cdev->recvqueue)) == 0) {
674 if (file->f_flags & O_NONBLOCK)
678 interruptible_sleep_on(&cdev->recvwait);
679 if ((skb = skb_dequeue(&cdev->recvqueue)) != 0)
681 if (signal_pending(current))
685 return -ERESTARTNOHAND;
687 if (skb->len > count) {
688 skb_queue_head(&cdev->recvqueue, skb);
691 if (copy_to_user(buf, skb->data, skb->len)) {
692 skb_queue_head(&cdev->recvqueue, skb);
703 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
705 struct capidev *cdev = (struct capidev *)file->private_data;
709 if (!cdev->ap.applid)
712 skb = alloc_skb(count, GFP_USER);
716 if (copy_from_user(skb_put(skb, count), buf, count)) {
720 mlen = CAPIMSG_LEN(skb->data);
721 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
722 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
732 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
734 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
735 down(&cdev->ncci_list_sem);
736 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
737 up(&cdev->ncci_list_sem);
740 cdev->errcode = capi20_put_message(&cdev->ap, skb);
750 capi_poll(struct file *file, poll_table * wait)
752 struct capidev *cdev = (struct capidev *)file->private_data;
753 unsigned int mask = 0;
755 if (!cdev->ap.applid)
758 poll_wait(file, &(cdev->recvwait), wait);
759 mask = POLLOUT | POLLWRNORM;
760 if (!skb_queue_empty(&cdev->recvqueue))
761 mask |= POLLIN | POLLRDNORM;
766 capi_ioctl(struct inode *inode, struct file *file,
767 unsigned int cmd, unsigned long arg)
769 struct capidev *cdev = file->private_data;
770 struct capi20_appl *ap = &cdev->ap;
771 capi_ioctl_struct data;
772 int retval = -EINVAL;
773 void __user *argp = (void __user *)arg;
781 if (copy_from_user(&cdev->ap.rparam, argp,
782 sizeof(struct capi_register_params)))
785 cdev->ap.private = cdev;
786 cdev->ap.recv_message = capi_recv_message;
787 cdev->errcode = capi20_register(ap);
793 return (int)ap->applid;
795 case CAPI_GET_VERSION:
797 if (copy_from_user(&data.contr, argp,
800 cdev->errcode = capi20_get_version(data.contr, &data.version);
803 if (copy_to_user(argp, &data.version,
804 sizeof(data.version)))
809 case CAPI_GET_SERIAL:
811 if (copy_from_user(&data.contr, argp,
814 cdev->errcode = capi20_get_serial (data.contr, data.serial);
817 if (copy_to_user(argp, data.serial,
818 sizeof(data.serial)))
822 case CAPI_GET_PROFILE:
824 if (copy_from_user(&data.contr, argp,
828 if (data.contr == 0) {
829 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
833 retval = copy_to_user(argp,
834 &data.profile.ncontroller,
835 sizeof(data.profile.ncontroller));
838 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
842 retval = copy_to_user(argp, &data.profile,
843 sizeof(data.profile));
850 case CAPI_GET_MANUFACTURER:
852 if (copy_from_user(&data.contr, argp,
855 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
859 if (copy_to_user(argp, data.manufacturer,
860 sizeof(data.manufacturer)))
865 case CAPI_GET_ERRCODE:
866 data.errcode = cdev->errcode;
867 cdev->errcode = CAPI_NOERROR;
869 if (copy_to_user(argp, &data.errcode,
870 sizeof(data.errcode)))
876 if (capi20_isinstalled() == CAPI_NOERROR)
880 case CAPI_MANUFACTURER_CMD:
882 struct capi_manufacturer_cmd mcmd;
883 if (!capable(CAP_SYS_ADMIN))
885 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
887 return capi20_manufacturer(mcmd.cmd, mcmd.data);
895 if (copy_from_user(&userflags, argp,
898 if (cmd == CAPI_SET_FLAGS)
899 cdev->userflags |= userflags;
901 cdev->userflags &= ~userflags;
906 if (copy_to_user(argp, &cdev->userflags,
907 sizeof(cdev->userflags)))
911 case CAPI_NCCI_OPENCOUNT:
913 struct capincci *nccip;
914 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
915 struct capiminor *mp;
916 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
919 if (copy_from_user(&ncci, argp, sizeof(ncci)))
922 down(&cdev->ncci_list_sem);
923 if ((nccip = capincci_find(cdev, (u32) ncci)) == 0) {
924 up(&cdev->ncci_list_sem);
927 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
928 if ((mp = nccip->minorp) != 0) {
929 count += atomic_read(&mp->ttyopencount);
931 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
932 up(&cdev->ncci_list_sem);
937 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
938 case CAPI_NCCI_GETUNIT:
940 struct capincci *nccip;
941 struct capiminor *mp;
944 if (copy_from_user(&ncci, argp,
947 down(&cdev->ncci_list_sem);
948 nccip = capincci_find(cdev, (u32) ncci);
949 if (!nccip || (mp = nccip->minorp) == 0) {
950 up(&cdev->ncci_list_sem);
954 up(&cdev->ncci_list_sem);
958 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
964 capi_open(struct inode *inode, struct file *file)
966 if (file->private_data)
969 if ((file->private_data = capidev_alloc()) == 0)
972 return nonseekable_open(inode, file);
976 capi_release(struct inode *inode, struct file *file)
978 struct capidev *cdev = (struct capidev *)file->private_data;
981 file->private_data = NULL;
986 static struct file_operations capi_fops =
988 .owner = THIS_MODULE,
995 .release = capi_release,
998 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
999 /* -------- tty_operations for capincci ----------------------------- */
1001 static int capinc_tty_open(struct tty_struct * tty, struct file * file)
1003 struct capiminor *mp;
1005 if ((mp = capiminor_find(iminor(file->f_dentry->d_inode))) == 0)
1010 tty->driver_data = (void *)mp;
1012 if (atomic_read(&mp->ttyopencount) == 0)
1014 atomic_inc(&mp->ttyopencount);
1015 #ifdef _DEBUG_REFCOUNT
1016 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1018 handle_minor_recv(mp);
1022 static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1024 struct capiminor *mp;
1026 mp = (struct capiminor *)tty->driver_data;
1028 if (atomic_dec_and_test(&mp->ttyopencount)) {
1029 #ifdef _DEBUG_REFCOUNT
1030 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1032 tty->driver_data = NULL;
1035 #ifdef _DEBUG_REFCOUNT
1036 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1042 #ifdef _DEBUG_REFCOUNT
1043 printk(KERN_DEBUG "capinc_tty_close\n");
1047 static int capinc_tty_write(struct tty_struct * tty,
1048 const unsigned char *buf, int count)
1050 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1051 struct sk_buff *skb;
1053 #ifdef _DEBUG_TTYFUNCS
1054 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1057 if (!mp || !mp->nccip) {
1058 #ifdef _DEBUG_TTYFUNCS
1059 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1067 skb_queue_tail(&mp->outqueue, skb);
1068 mp->outbytes += skb->len;
1071 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1073 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1077 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1078 memcpy(skb_put(skb, count), buf, count);
1080 skb_queue_tail(&mp->outqueue, skb);
1081 mp->outbytes += skb->len;
1082 (void)handle_minor_send(mp);
1083 (void)handle_minor_recv(mp);
1087 static void capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1089 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1090 struct sk_buff *skb;
1092 #ifdef _DEBUG_TTYFUNCS
1093 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1096 if (!mp || !mp->nccip) {
1097 #ifdef _DEBUG_TTYFUNCS
1098 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1105 if (skb_tailroom(skb) > 0) {
1106 *(skb_put(skb, 1)) = ch;
1110 skb_queue_tail(&mp->outqueue, skb);
1111 mp->outbytes += skb->len;
1112 (void)handle_minor_send(mp);
1114 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1116 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1117 *(skb_put(skb, 1)) = ch;
1120 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1124 static void capinc_tty_flush_chars(struct tty_struct *tty)
1126 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1127 struct sk_buff *skb;
1129 #ifdef _DEBUG_TTYFUNCS
1130 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1133 if (!mp || !mp->nccip) {
1134 #ifdef _DEBUG_TTYFUNCS
1135 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1143 skb_queue_tail(&mp->outqueue, skb);
1144 mp->outbytes += skb->len;
1145 (void)handle_minor_send(mp);
1147 (void)handle_minor_recv(mp);
1150 static int capinc_tty_write_room(struct tty_struct *tty)
1152 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1154 if (!mp || !mp->nccip) {
1155 #ifdef _DEBUG_TTYFUNCS
1156 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1160 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1161 room *= CAPI_MAX_BLKSIZE;
1162 #ifdef _DEBUG_TTYFUNCS
1163 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1168 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1170 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1171 if (!mp || !mp->nccip) {
1172 #ifdef _DEBUG_TTYFUNCS
1173 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1177 #ifdef _DEBUG_TTYFUNCS
1178 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1179 mp->outbytes, mp->nack,
1180 skb_queue_len(&mp->outqueue),
1181 skb_queue_len(&mp->inqueue));
1183 return mp->outbytes;
1186 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1187 unsigned int cmd, unsigned long arg)
1192 error = n_tty_ioctl (tty, file, cmd, arg);
1198 static void capinc_tty_set_termios(struct tty_struct *tty, struct termios * old)
1200 #ifdef _DEBUG_TTYFUNCS
1201 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1205 static void capinc_tty_throttle(struct tty_struct * tty)
1207 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1208 #ifdef _DEBUG_TTYFUNCS
1209 printk(KERN_DEBUG "capinc_tty_throttle\n");
1215 static void capinc_tty_unthrottle(struct tty_struct * tty)
1217 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1218 #ifdef _DEBUG_TTYFUNCS
1219 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1223 handle_minor_recv(mp);
1227 static void capinc_tty_stop(struct tty_struct *tty)
1229 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1230 #ifdef _DEBUG_TTYFUNCS
1231 printk(KERN_DEBUG "capinc_tty_stop\n");
1238 static void capinc_tty_start(struct tty_struct *tty)
1240 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1241 #ifdef _DEBUG_TTYFUNCS
1242 printk(KERN_DEBUG "capinc_tty_start\n");
1246 (void)handle_minor_send(mp);
1250 static void capinc_tty_hangup(struct tty_struct *tty)
1252 #ifdef _DEBUG_TTYFUNCS
1253 printk(KERN_DEBUG "capinc_tty_hangup\n");
1257 static void capinc_tty_break_ctl(struct tty_struct *tty, int state)
1259 #ifdef _DEBUG_TTYFUNCS
1260 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1264 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1266 #ifdef _DEBUG_TTYFUNCS
1267 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1271 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1273 #ifdef _DEBUG_TTYFUNCS
1274 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1278 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1280 #ifdef _DEBUG_TTYFUNCS
1281 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1285 static int capinc_tty_read_proc(char *page, char **start, off_t off,
1286 int count, int *eof, void *data)
1291 static struct tty_driver *capinc_tty_driver;
1293 static struct tty_operations capinc_ops = {
1294 .open = capinc_tty_open,
1295 .close = capinc_tty_close,
1296 .write = capinc_tty_write,
1297 .put_char = capinc_tty_put_char,
1298 .flush_chars = capinc_tty_flush_chars,
1299 .write_room = capinc_tty_write_room,
1300 .chars_in_buffer = capinc_tty_chars_in_buffer,
1301 .ioctl = capinc_tty_ioctl,
1302 .set_termios = capinc_tty_set_termios,
1303 .throttle = capinc_tty_throttle,
1304 .unthrottle = capinc_tty_unthrottle,
1305 .stop = capinc_tty_stop,
1306 .start = capinc_tty_start,
1307 .hangup = capinc_tty_hangup,
1308 .break_ctl = capinc_tty_break_ctl,
1309 .flush_buffer = capinc_tty_flush_buffer,
1310 .set_ldisc = capinc_tty_set_ldisc,
1311 .send_xchar = capinc_tty_send_xchar,
1312 .read_proc = capinc_tty_read_proc,
1315 static int capinc_tty_init(void)
1317 struct tty_driver *drv;
1319 if (capi_ttyminors > CAPINC_MAX_PORTS)
1320 capi_ttyminors = CAPINC_MAX_PORTS;
1321 if (capi_ttyminors <= 0)
1322 capi_ttyminors = CAPINC_NR_PORTS;
1324 drv = alloc_tty_driver(capi_ttyminors);
1328 drv->owner = THIS_MODULE;
1329 drv->driver_name = "capi_nc";
1330 drv->devfs_name = "capi/";
1332 drv->major = capi_ttymajor;
1333 drv->minor_start = 0;
1334 drv->type = TTY_DRIVER_TYPE_SERIAL;
1335 drv->subtype = SERIAL_TYPE_NORMAL;
1336 drv->init_termios = tty_std_termios;
1337 drv->init_termios.c_iflag = ICRNL;
1338 drv->init_termios.c_oflag = OPOST | ONLCR;
1339 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1340 drv->init_termios.c_lflag = 0;
1341 drv->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_RESET_TERMIOS;
1342 tty_set_operations(drv, &capinc_ops);
1343 if (tty_register_driver(drv)) {
1344 put_tty_driver(drv);
1345 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1348 capinc_tty_driver = drv;
1352 static void capinc_tty_exit(void)
1354 struct tty_driver *drv = capinc_tty_driver;
1356 if ((retval = tty_unregister_driver(drv)))
1357 printk(KERN_ERR "capi: failed to unregister capi_nc driver (%d)\n", retval);
1358 put_tty_driver(drv);
1361 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1363 /* -------- /proc functions ----------------------------------------- */
1366 * /proc/capi/capi20:
1367 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1369 static int proc_capidev_read_proc(char *page, char **start, off_t off,
1370 int count, int *eof, void *data)
1372 struct capidev *cdev;
1373 struct list_head *l;
1376 read_lock(&capidev_list_lock);
1377 list_for_each(l, &capidev_list) {
1378 cdev = list_entry(l, struct capidev, list);
1379 len += sprintf(page+len, "0 %d %lu %lu %lu %lu\n",
1381 cdev->ap.nrecvctlpkt,
1382 cdev->ap.nrecvdatapkt,
1383 cdev->ap.nsentctlpkt,
1384 cdev->ap.nsentdatapkt);
1389 if (len-off > count)
1395 read_unlock(&capidev_list_lock);
1398 if (len > count) len = count;
1399 if (len < 0) len = 0;
1404 * /proc/capi/capi20ncci:
1407 static int proc_capincci_read_proc(char *page, char **start, off_t off,
1408 int count, int *eof, void *data)
1410 struct capidev *cdev;
1411 struct capincci *np;
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 for (np=cdev->nccis; np; np = np->next) {
1419 len += sprintf(page+len, "%d 0x%x\n",
1426 if (len-off > count)
1432 read_unlock(&capidev_list_lock);
1436 if (len>count) len = count;
1441 static struct procfsentries {
1444 int (*read_proc)(char *page, char **start, off_t off,
1445 int count, int *eof, void *data);
1446 struct proc_dir_entry *procent;
1447 } procfsentries[] = {
1448 /* { "capi", S_IFDIR, 0 }, */
1449 { "capi/capi20", 0 , proc_capidev_read_proc },
1450 { "capi/capi20ncci", 0 , proc_capincci_read_proc },
1453 static void __init proc_init(void)
1455 int nelem = sizeof(procfsentries)/sizeof(procfsentries[0]);
1458 for (i=0; i < nelem; i++) {
1459 struct procfsentries *p = procfsentries + i;
1460 p->procent = create_proc_entry(p->name, p->mode, NULL);
1461 if (p->procent) p->procent->read_proc = p->read_proc;
1465 static void __exit proc_exit(void)
1467 int nelem = sizeof(procfsentries)/sizeof(procfsentries[0]);
1470 for (i=nelem-1; i >= 0; i--) {
1471 struct procfsentries *p = procfsentries + i;
1473 remove_proc_entry(p->name, NULL);
1479 /* -------- init function and module interface ---------------------- */
1482 static char rev[32];
1484 static int __init capi_init(void)
1490 if ((p = strchr(revision, ':')) != 0 && p[1]) {
1491 strlcpy(rev, p + 2, sizeof(rev));
1492 if ((p = strchr(rev, '$')) != 0 && p > rev)
1497 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1498 if (major_ret < 0) {
1499 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1502 capi_major = major_ret;
1503 capi_class = class_create(THIS_MODULE, "capi");
1504 if (IS_ERR(capi_class)) {
1505 unregister_chrdev(capi_major, "capi20");
1506 return PTR_ERR(capi_class);
1509 class_device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
1510 devfs_mk_cdev(MKDEV(capi_major, 0), S_IFCHR | S_IRUSR | S_IWUSR,
1513 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1514 if (capinc_tty_init() < 0) {
1515 class_device_destroy(capi_class, MKDEV(capi_major, 0));
1516 class_destroy(capi_class);
1517 unregister_chrdev(capi_major, "capi20");
1520 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1524 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1525 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1526 compileinfo = " (middleware+capifs)";
1528 compileinfo = " (no capifs)";
1531 compileinfo = " (no middleware)";
1533 printk(KERN_NOTICE "capi20: Rev %s: started up with major %d%s\n",
1534 rev, capi_major, compileinfo);
1539 static void __exit capi_exit(void)
1543 class_device_destroy(capi_class, MKDEV(capi_major, 0));
1544 class_destroy(capi_class);
1545 unregister_chrdev(capi_major, "capi20");
1546 devfs_remove("isdn/capi20");
1548 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1551 printk(KERN_NOTICE "capi: Rev %s: unloaded\n", rev);
1554 module_init(capi_init);
1555 module_exit(capi_exit);