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
90 struct datahandle_queue {
91 struct list_head list;
96 struct list_head list;
97 struct capincci *nccip;
100 struct capi20_appl *ap;
105 struct tty_struct *tty;
108 struct sk_buff *ttyskb;
109 atomic_t ttyopencount;
111 struct sk_buff_head inqueue;
113 struct sk_buff_head outqueue;
117 struct list_head ackqueue;
121 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
124 struct capincci *next;
126 struct capidev *cdev;
127 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
128 struct capiminor *minorp;
129 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
133 struct list_head list;
134 struct capi20_appl ap;
138 struct sk_buff_head recvqueue;
139 wait_queue_head_t recvwait;
141 struct capincci *nccis;
143 struct semaphore ncci_list_sem;
146 /* -------- global variables ---------------------------------------- */
148 static DEFINE_RWLOCK(capidev_list_lock);
149 static LIST_HEAD(capidev_list);
151 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
152 static DEFINE_RWLOCK(capiminor_list_lock);
153 static LIST_HEAD(capiminor_list);
154 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
156 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
157 /* -------- datahandles --------------------------------------------- */
159 static int capincci_add_ack(struct capiminor *mp, u16 datahandle)
161 struct datahandle_queue *n;
164 n = kmalloc(sizeof(*n), GFP_ATOMIC);
166 printk(KERN_ERR "capi: alloc datahandle failed\n");
169 n->datahandle = datahandle;
170 INIT_LIST_HEAD(&n->list);
171 spin_lock_irqsave(&mp->ackqlock, flags);
172 list_add_tail(&n->list, &mp->ackqueue);
174 spin_unlock_irqrestore(&mp->ackqlock, flags);
178 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
180 struct datahandle_queue *p, *tmp;
183 spin_lock_irqsave(&mp->ackqlock, flags);
184 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
185 if (p->datahandle == datahandle) {
189 spin_unlock_irqrestore(&mp->ackqlock, flags);
193 spin_unlock_irqrestore(&mp->ackqlock, flags);
197 static void capiminor_del_all_ack(struct capiminor *mp)
199 struct datahandle_queue *p, *tmp;
202 spin_lock_irqsave(&mp->ackqlock, flags);
203 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
208 spin_unlock_irqrestore(&mp->ackqlock, flags);
212 /* -------- struct capiminor ---------------------------------------- */
214 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
216 struct capiminor *mp, *p;
217 unsigned int minor = 0;
220 mp = kmalloc(sizeof(*mp), GFP_ATOMIC);
222 printk(KERN_ERR "capi: can't alloc capiminor\n");
226 memset(mp, 0, sizeof(struct capiminor));
230 atomic_set(&mp->ttyopencount,0);
231 INIT_LIST_HEAD(&mp->ackqueue);
232 spin_lock_init(&mp->ackqlock);
234 skb_queue_head_init(&mp->inqueue);
235 skb_queue_head_init(&mp->outqueue);
237 /* Allocate the least unused minor number.
239 write_lock_irqsave(&capiminor_list_lock, flags);
240 if (list_empty(&capiminor_list))
241 list_add(&mp->list, &capiminor_list);
243 list_for_each_entry(p, &capiminor_list, list) {
244 if (p->minor > minor)
249 if (minor < capi_ttyminors) {
251 list_add(&mp->list, p->list.prev);
254 write_unlock_irqrestore(&capiminor_list_lock, flags);
256 if (!(minor < capi_ttyminors)) {
257 printk(KERN_NOTICE "capi: out of minors\n");
265 static void capiminor_free(struct capiminor *mp)
269 write_lock_irqsave(&capiminor_list_lock, flags);
271 write_unlock_irqrestore(&capiminor_list_lock, flags);
273 if (mp->ttyskb) kfree_skb(mp->ttyskb);
275 skb_queue_purge(&mp->inqueue);
276 skb_queue_purge(&mp->outqueue);
277 capiminor_del_all_ack(mp);
281 static struct capiminor *capiminor_find(unsigned int minor)
284 struct capiminor *p = NULL;
286 read_lock(&capiminor_list_lock);
287 list_for_each(l, &capiminor_list) {
288 p = list_entry(l, struct capiminor, list);
289 if (p->minor == minor)
292 read_unlock(&capiminor_list_lock);
293 if (l == &capiminor_list)
298 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
300 /* -------- struct capincci ----------------------------------------- */
302 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
304 struct capincci *np, **pp;
305 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
306 struct capiminor *mp = NULL;
307 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
309 np = kmalloc(sizeof(*np), GFP_ATOMIC);
312 memset(np, 0, sizeof(struct capincci));
315 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
317 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
318 mp = np->minorp = capiminor_alloc(&cdev->ap, ncci);
321 #ifdef _DEBUG_REFCOUNT
322 printk(KERN_DEBUG "set mp->nccip\n");
324 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
325 capifs_new_ncci(mp->minor, MKDEV(capi_ttymajor, mp->minor));
328 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
329 for (pp=&cdev->nccis; *pp; pp = &(*pp)->next)
335 static void capincci_free(struct capidev *cdev, u32 ncci)
337 struct capincci *np, **pp;
338 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
339 struct capiminor *mp;
340 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
345 if (ncci == 0xffffffff || np->ncci == ncci) {
347 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
348 if ((mp = np->minorp) != 0) {
349 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
350 capifs_free_ncci(mp->minor);
354 #ifdef _DEBUG_REFCOUNT
355 printk(KERN_DEBUG "reset mp->nccip\n");
362 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
364 if (*pp == 0) return;
371 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
375 for (p=cdev->nccis; p ; p = p->next) {
382 /* -------- struct capidev ------------------------------------------ */
384 static struct capidev *capidev_alloc(void)
386 struct capidev *cdev;
389 cdev = kmalloc(sizeof(*cdev), GFP_KERNEL);
392 memset(cdev, 0, sizeof(struct capidev));
394 init_MUTEX(&cdev->ncci_list_sem);
395 skb_queue_head_init(&cdev->recvqueue);
396 init_waitqueue_head(&cdev->recvwait);
397 write_lock_irqsave(&capidev_list_lock, flags);
398 list_add_tail(&cdev->list, &capidev_list);
399 write_unlock_irqrestore(&capidev_list_lock, flags);
403 static void capidev_free(struct capidev *cdev)
407 if (cdev->ap.applid) {
408 capi20_release(&cdev->ap);
411 skb_queue_purge(&cdev->recvqueue);
413 down(&cdev->ncci_list_sem);
414 capincci_free(cdev, 0xffffffff);
415 up(&cdev->ncci_list_sem);
417 write_lock_irqsave(&capidev_list_lock, flags);
418 list_del(&cdev->list);
419 write_unlock_irqrestore(&capidev_list_lock, flags);
423 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
424 /* -------- handle data queue --------------------------------------- */
426 static struct sk_buff *
427 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
429 struct sk_buff *nskb;
430 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
432 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
433 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
434 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
435 capimsg_setu16(s, 2, mp->ap->applid);
436 capimsg_setu8 (s, 4, CAPI_DATA_B3);
437 capimsg_setu8 (s, 5, CAPI_RESP);
438 capimsg_setu16(s, 6, mp->msgid++);
439 capimsg_setu32(s, 8, mp->ncci);
440 capimsg_setu16(s, 12, datahandle);
445 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
447 struct sk_buff *nskb;
449 u16 errcode, datahandle;
450 struct tty_ldisc *ld;
452 datalen = skb->len - CAPIMSG_LEN(skb->data);
455 #ifdef _DEBUG_DATAFLOW
456 printk(KERN_DEBUG "capi: currently no receiver\n");
461 ld = tty_ldisc_ref(mp->tty);
464 if (ld->receive_buf == NULL) {
465 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
466 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
471 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
472 printk(KERN_DEBUG "capi: recv tty throttled\n");
476 if (mp->tty->receive_room < datalen) {
477 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
478 printk(KERN_DEBUG "capi: no room in tty\n");
482 if ((nskb = gen_data_b3_resp_for(mp, skb)) == 0) {
483 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
486 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
487 errcode = capi20_put_message(mp->ap, nskb);
488 if (errcode != CAPI_NOERROR) {
489 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
494 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
495 #ifdef _DEBUG_DATAFLOW
496 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
497 datahandle, skb->len);
499 ld->receive_buf(mp->tty, skb->data, NULL, skb->len);
508 static void handle_minor_recv(struct capiminor *mp)
511 while ((skb = skb_dequeue(&mp->inqueue)) != 0) {
512 unsigned int len = skb->len;
514 if (handle_recv_skb(mp, skb) < 0) {
515 skb_queue_head(&mp->inqueue, skb);
522 static int handle_minor_send(struct capiminor *mp)
530 if (mp->tty && mp->ttyoutstop) {
531 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
532 printk(KERN_DEBUG "capi: send: tty stopped\n");
537 while ((skb = skb_dequeue(&mp->outqueue)) != 0) {
538 datahandle = mp->datahandle;
540 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
541 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
542 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
543 capimsg_setu16(skb->data, 2, mp->ap->applid);
544 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
545 capimsg_setu8 (skb->data, 5, CAPI_REQ);
546 capimsg_setu16(skb->data, 6, mp->msgid++);
547 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
548 capimsg_setu32(skb->data, 12, (u32) skb->data); /* Data32 */
549 capimsg_setu16(skb->data, 16, len); /* Data length */
550 capimsg_setu16(skb->data, 18, datahandle);
551 capimsg_setu16(skb->data, 20, 0); /* Flags */
553 if (capincci_add_ack(mp, datahandle) < 0) {
554 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
555 skb_queue_head(&mp->outqueue, skb);
558 errcode = capi20_put_message(mp->ap, skb);
559 if (errcode == CAPI_NOERROR) {
563 #ifdef _DEBUG_DATAFLOW
564 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
569 capiminor_del_ack(mp, datahandle);
571 if (errcode == CAPI_SENDQUEUEFULL) {
572 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
573 skb_queue_head(&mp->outqueue, skb);
577 /* ups, drop packet */
578 printk(KERN_ERR "capi: put_message = %x\n", errcode);
585 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
586 /* -------- function called by lower level -------------------------- */
588 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
590 struct capidev *cdev = ap->private;
591 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
592 struct capiminor *mp;
594 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
598 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
599 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
601 down(&cdev->ncci_list_sem);
602 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
603 up(&cdev->ncci_list_sem);
606 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND) {
607 down(&cdev->ncci_list_sem);
608 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
609 up(&cdev->ncci_list_sem);
611 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
612 skb_queue_tail(&cdev->recvqueue, skb);
613 wake_up_interruptible(&cdev->recvwait);
616 ncci = CAPIMSG_CONTROL(skb->data);
617 for (np = cdev->nccis; np && np->ncci != ncci; np = np->next)
620 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
621 skb_queue_tail(&cdev->recvqueue, skb);
622 wake_up_interruptible(&cdev->recvwait);
625 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
626 skb_queue_tail(&cdev->recvqueue, skb);
627 wake_up_interruptible(&cdev->recvwait);
628 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
631 skb_queue_tail(&cdev->recvqueue, skb);
632 wake_up_interruptible(&cdev->recvwait);
637 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
639 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
640 #ifdef _DEBUG_DATAFLOW
641 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
642 datahandle, skb->len-CAPIMSG_LEN(skb->data));
644 skb_queue_tail(&mp->inqueue, skb);
645 mp->inbytes += skb->len;
646 handle_minor_recv(mp);
648 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
650 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
651 #ifdef _DEBUG_DATAFLOW
652 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
654 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
657 (void)capiminor_del_ack(mp, datahandle);
660 (void)handle_minor_send(mp);
663 /* ups, let capi application handle it :-) */
664 skb_queue_tail(&cdev->recvqueue, skb);
665 wake_up_interruptible(&cdev->recvwait);
667 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
670 /* -------- file_operations for capidev ----------------------------- */
673 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
675 struct capidev *cdev = (struct capidev *)file->private_data;
679 if (!cdev->ap.applid)
682 if ((skb = skb_dequeue(&cdev->recvqueue)) == 0) {
684 if (file->f_flags & O_NONBLOCK)
688 interruptible_sleep_on(&cdev->recvwait);
689 if ((skb = skb_dequeue(&cdev->recvqueue)) != 0)
691 if (signal_pending(current))
695 return -ERESTARTNOHAND;
697 if (skb->len > count) {
698 skb_queue_head(&cdev->recvqueue, skb);
701 if (copy_to_user(buf, skb->data, skb->len)) {
702 skb_queue_head(&cdev->recvqueue, skb);
713 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
715 struct capidev *cdev = (struct capidev *)file->private_data;
719 if (!cdev->ap.applid)
722 skb = alloc_skb(count, GFP_USER);
726 if (copy_from_user(skb_put(skb, count), buf, count)) {
730 mlen = CAPIMSG_LEN(skb->data);
731 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
732 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
742 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
744 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
745 down(&cdev->ncci_list_sem);
746 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
747 up(&cdev->ncci_list_sem);
750 cdev->errcode = capi20_put_message(&cdev->ap, skb);
760 capi_poll(struct file *file, poll_table * wait)
762 struct capidev *cdev = (struct capidev *)file->private_data;
763 unsigned int mask = 0;
765 if (!cdev->ap.applid)
768 poll_wait(file, &(cdev->recvwait), wait);
769 mask = POLLOUT | POLLWRNORM;
770 if (!skb_queue_empty(&cdev->recvqueue))
771 mask |= POLLIN | POLLRDNORM;
776 capi_ioctl(struct inode *inode, struct file *file,
777 unsigned int cmd, unsigned long arg)
779 struct capidev *cdev = file->private_data;
780 struct capi20_appl *ap = &cdev->ap;
781 capi_ioctl_struct data;
782 int retval = -EINVAL;
783 void __user *argp = (void __user *)arg;
791 if (copy_from_user(&cdev->ap.rparam, argp,
792 sizeof(struct capi_register_params)))
795 cdev->ap.private = cdev;
796 cdev->ap.recv_message = capi_recv_message;
797 cdev->errcode = capi20_register(ap);
803 return (int)ap->applid;
805 case CAPI_GET_VERSION:
807 if (copy_from_user(&data.contr, argp,
810 cdev->errcode = capi20_get_version(data.contr, &data.version);
813 if (copy_to_user(argp, &data.version,
814 sizeof(data.version)))
819 case CAPI_GET_SERIAL:
821 if (copy_from_user(&data.contr, argp,
824 cdev->errcode = capi20_get_serial (data.contr, data.serial);
827 if (copy_to_user(argp, data.serial,
828 sizeof(data.serial)))
832 case CAPI_GET_PROFILE:
834 if (copy_from_user(&data.contr, argp,
838 if (data.contr == 0) {
839 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
843 retval = copy_to_user(argp,
844 &data.profile.ncontroller,
845 sizeof(data.profile.ncontroller));
848 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
852 retval = copy_to_user(argp, &data.profile,
853 sizeof(data.profile));
860 case CAPI_GET_MANUFACTURER:
862 if (copy_from_user(&data.contr, argp,
865 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
869 if (copy_to_user(argp, data.manufacturer,
870 sizeof(data.manufacturer)))
875 case CAPI_GET_ERRCODE:
876 data.errcode = cdev->errcode;
877 cdev->errcode = CAPI_NOERROR;
879 if (copy_to_user(argp, &data.errcode,
880 sizeof(data.errcode)))
886 if (capi20_isinstalled() == CAPI_NOERROR)
890 case CAPI_MANUFACTURER_CMD:
892 struct capi_manufacturer_cmd mcmd;
893 if (!capable(CAP_SYS_ADMIN))
895 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
897 return capi20_manufacturer(mcmd.cmd, mcmd.data);
905 if (copy_from_user(&userflags, argp,
908 if (cmd == CAPI_SET_FLAGS)
909 cdev->userflags |= userflags;
911 cdev->userflags &= ~userflags;
916 if (copy_to_user(argp, &cdev->userflags,
917 sizeof(cdev->userflags)))
921 case CAPI_NCCI_OPENCOUNT:
923 struct capincci *nccip;
924 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
925 struct capiminor *mp;
926 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
929 if (copy_from_user(&ncci, argp, sizeof(ncci)))
932 down(&cdev->ncci_list_sem);
933 if ((nccip = capincci_find(cdev, (u32) ncci)) == 0) {
934 up(&cdev->ncci_list_sem);
937 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
938 if ((mp = nccip->minorp) != 0) {
939 count += atomic_read(&mp->ttyopencount);
941 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
942 up(&cdev->ncci_list_sem);
947 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
948 case CAPI_NCCI_GETUNIT:
950 struct capincci *nccip;
951 struct capiminor *mp;
954 if (copy_from_user(&ncci, argp,
957 down(&cdev->ncci_list_sem);
958 nccip = capincci_find(cdev, (u32) ncci);
959 if (!nccip || (mp = nccip->minorp) == 0) {
960 up(&cdev->ncci_list_sem);
964 up(&cdev->ncci_list_sem);
968 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
974 capi_open(struct inode *inode, struct file *file)
976 if (file->private_data)
979 if ((file->private_data = capidev_alloc()) == 0)
982 return nonseekable_open(inode, file);
986 capi_release(struct inode *inode, struct file *file)
988 struct capidev *cdev = (struct capidev *)file->private_data;
991 file->private_data = NULL;
996 static struct file_operations capi_fops =
998 .owner = THIS_MODULE,
1001 .write = capi_write,
1003 .ioctl = capi_ioctl,
1005 .release = capi_release,
1008 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1009 /* -------- tty_operations for capincci ----------------------------- */
1011 static int capinc_tty_open(struct tty_struct * tty, struct file * file)
1013 struct capiminor *mp;
1015 if ((mp = capiminor_find(iminor(file->f_dentry->d_inode))) == 0)
1020 tty->driver_data = (void *)mp;
1022 if (atomic_read(&mp->ttyopencount) == 0)
1024 atomic_inc(&mp->ttyopencount);
1025 #ifdef _DEBUG_REFCOUNT
1026 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1028 handle_minor_recv(mp);
1032 static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1034 struct capiminor *mp;
1036 mp = (struct capiminor *)tty->driver_data;
1038 if (atomic_dec_and_test(&mp->ttyopencount)) {
1039 #ifdef _DEBUG_REFCOUNT
1040 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1042 tty->driver_data = NULL;
1045 #ifdef _DEBUG_REFCOUNT
1046 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1052 #ifdef _DEBUG_REFCOUNT
1053 printk(KERN_DEBUG "capinc_tty_close\n");
1057 static int capinc_tty_write(struct tty_struct * tty,
1058 const unsigned char *buf, int count)
1060 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1061 struct sk_buff *skb;
1063 #ifdef _DEBUG_TTYFUNCS
1064 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1067 if (!mp || !mp->nccip) {
1068 #ifdef _DEBUG_TTYFUNCS
1069 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1077 skb_queue_tail(&mp->outqueue, skb);
1078 mp->outbytes += skb->len;
1081 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1083 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1087 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1088 memcpy(skb_put(skb, count), buf, count);
1090 skb_queue_tail(&mp->outqueue, skb);
1091 mp->outbytes += skb->len;
1092 (void)handle_minor_send(mp);
1093 (void)handle_minor_recv(mp);
1097 static void capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1099 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1100 struct sk_buff *skb;
1102 #ifdef _DEBUG_TTYFUNCS
1103 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1106 if (!mp || !mp->nccip) {
1107 #ifdef _DEBUG_TTYFUNCS
1108 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1115 if (skb_tailroom(skb) > 0) {
1116 *(skb_put(skb, 1)) = ch;
1120 skb_queue_tail(&mp->outqueue, skb);
1121 mp->outbytes += skb->len;
1122 (void)handle_minor_send(mp);
1124 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1126 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1127 *(skb_put(skb, 1)) = ch;
1130 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1134 static void capinc_tty_flush_chars(struct tty_struct *tty)
1136 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1137 struct sk_buff *skb;
1139 #ifdef _DEBUG_TTYFUNCS
1140 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1143 if (!mp || !mp->nccip) {
1144 #ifdef _DEBUG_TTYFUNCS
1145 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1153 skb_queue_tail(&mp->outqueue, skb);
1154 mp->outbytes += skb->len;
1155 (void)handle_minor_send(mp);
1157 (void)handle_minor_recv(mp);
1160 static int capinc_tty_write_room(struct tty_struct *tty)
1162 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1164 if (!mp || !mp->nccip) {
1165 #ifdef _DEBUG_TTYFUNCS
1166 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1170 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1171 room *= CAPI_MAX_BLKSIZE;
1172 #ifdef _DEBUG_TTYFUNCS
1173 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1178 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1180 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1181 if (!mp || !mp->nccip) {
1182 #ifdef _DEBUG_TTYFUNCS
1183 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1187 #ifdef _DEBUG_TTYFUNCS
1188 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1189 mp->outbytes, mp->nack,
1190 skb_queue_len(&mp->outqueue),
1191 skb_queue_len(&mp->inqueue));
1193 return mp->outbytes;
1196 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1197 unsigned int cmd, unsigned long arg)
1202 error = n_tty_ioctl (tty, file, cmd, arg);
1208 static void capinc_tty_set_termios(struct tty_struct *tty, struct termios * old)
1210 #ifdef _DEBUG_TTYFUNCS
1211 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1215 static void capinc_tty_throttle(struct tty_struct * tty)
1217 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1218 #ifdef _DEBUG_TTYFUNCS
1219 printk(KERN_DEBUG "capinc_tty_throttle\n");
1225 static void capinc_tty_unthrottle(struct tty_struct * tty)
1227 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1228 #ifdef _DEBUG_TTYFUNCS
1229 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1233 handle_minor_recv(mp);
1237 static void capinc_tty_stop(struct tty_struct *tty)
1239 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1240 #ifdef _DEBUG_TTYFUNCS
1241 printk(KERN_DEBUG "capinc_tty_stop\n");
1248 static void capinc_tty_start(struct tty_struct *tty)
1250 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1251 #ifdef _DEBUG_TTYFUNCS
1252 printk(KERN_DEBUG "capinc_tty_start\n");
1256 (void)handle_minor_send(mp);
1260 static void capinc_tty_hangup(struct tty_struct *tty)
1262 #ifdef _DEBUG_TTYFUNCS
1263 printk(KERN_DEBUG "capinc_tty_hangup\n");
1267 static void capinc_tty_break_ctl(struct tty_struct *tty, int state)
1269 #ifdef _DEBUG_TTYFUNCS
1270 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1274 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1276 #ifdef _DEBUG_TTYFUNCS
1277 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1281 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1283 #ifdef _DEBUG_TTYFUNCS
1284 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1288 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1290 #ifdef _DEBUG_TTYFUNCS
1291 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1295 static int capinc_tty_read_proc(char *page, char **start, off_t off,
1296 int count, int *eof, void *data)
1301 static struct tty_driver *capinc_tty_driver;
1303 static struct tty_operations capinc_ops = {
1304 .open = capinc_tty_open,
1305 .close = capinc_tty_close,
1306 .write = capinc_tty_write,
1307 .put_char = capinc_tty_put_char,
1308 .flush_chars = capinc_tty_flush_chars,
1309 .write_room = capinc_tty_write_room,
1310 .chars_in_buffer = capinc_tty_chars_in_buffer,
1311 .ioctl = capinc_tty_ioctl,
1312 .set_termios = capinc_tty_set_termios,
1313 .throttle = capinc_tty_throttle,
1314 .unthrottle = capinc_tty_unthrottle,
1315 .stop = capinc_tty_stop,
1316 .start = capinc_tty_start,
1317 .hangup = capinc_tty_hangup,
1318 .break_ctl = capinc_tty_break_ctl,
1319 .flush_buffer = capinc_tty_flush_buffer,
1320 .set_ldisc = capinc_tty_set_ldisc,
1321 .send_xchar = capinc_tty_send_xchar,
1322 .read_proc = capinc_tty_read_proc,
1325 static int capinc_tty_init(void)
1327 struct tty_driver *drv;
1329 if (capi_ttyminors > CAPINC_MAX_PORTS)
1330 capi_ttyminors = CAPINC_MAX_PORTS;
1331 if (capi_ttyminors <= 0)
1332 capi_ttyminors = CAPINC_NR_PORTS;
1334 drv = alloc_tty_driver(capi_ttyminors);
1338 drv->owner = THIS_MODULE;
1339 drv->driver_name = "capi_nc";
1340 drv->devfs_name = "capi/";
1342 drv->major = capi_ttymajor;
1343 drv->minor_start = 0;
1344 drv->type = TTY_DRIVER_TYPE_SERIAL;
1345 drv->subtype = SERIAL_TYPE_NORMAL;
1346 drv->init_termios = tty_std_termios;
1347 drv->init_termios.c_iflag = ICRNL;
1348 drv->init_termios.c_oflag = OPOST | ONLCR;
1349 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1350 drv->init_termios.c_lflag = 0;
1351 drv->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_RESET_TERMIOS;
1352 tty_set_operations(drv, &capinc_ops);
1353 if (tty_register_driver(drv)) {
1354 put_tty_driver(drv);
1355 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1358 capinc_tty_driver = drv;
1362 static void capinc_tty_exit(void)
1364 struct tty_driver *drv = capinc_tty_driver;
1366 if ((retval = tty_unregister_driver(drv)))
1367 printk(KERN_ERR "capi: failed to unregister capi_nc driver (%d)\n", retval);
1368 put_tty_driver(drv);
1371 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1373 /* -------- /proc functions ----------------------------------------- */
1376 * /proc/capi/capi20:
1377 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1379 static int proc_capidev_read_proc(char *page, char **start, off_t off,
1380 int count, int *eof, void *data)
1382 struct capidev *cdev;
1383 struct list_head *l;
1386 read_lock(&capidev_list_lock);
1387 list_for_each(l, &capidev_list) {
1388 cdev = list_entry(l, struct capidev, list);
1389 len += sprintf(page+len, "0 %d %lu %lu %lu %lu\n",
1391 cdev->ap.nrecvctlpkt,
1392 cdev->ap.nrecvdatapkt,
1393 cdev->ap.nsentctlpkt,
1394 cdev->ap.nsentdatapkt);
1399 if (len-off > count)
1405 read_unlock(&capidev_list_lock);
1408 if (len > count) len = count;
1409 if (len < 0) len = 0;
1414 * /proc/capi/capi20ncci:
1417 static int proc_capincci_read_proc(char *page, char **start, off_t off,
1418 int count, int *eof, void *data)
1420 struct capidev *cdev;
1421 struct capincci *np;
1422 struct list_head *l;
1425 read_lock(&capidev_list_lock);
1426 list_for_each(l, &capidev_list) {
1427 cdev = list_entry(l, struct capidev, list);
1428 for (np=cdev->nccis; np; np = np->next) {
1429 len += sprintf(page+len, "%d 0x%x\n",
1436 if (len-off > count)
1442 read_unlock(&capidev_list_lock);
1446 if (len>count) len = count;
1451 static struct procfsentries {
1454 int (*read_proc)(char *page, char **start, off_t off,
1455 int count, int *eof, void *data);
1456 struct proc_dir_entry *procent;
1457 } procfsentries[] = {
1458 /* { "capi", S_IFDIR, 0 }, */
1459 { "capi/capi20", 0 , proc_capidev_read_proc },
1460 { "capi/capi20ncci", 0 , proc_capincci_read_proc },
1463 static void __init proc_init(void)
1465 int nelem = sizeof(procfsentries)/sizeof(procfsentries[0]);
1468 for (i=0; i < nelem; i++) {
1469 struct procfsentries *p = procfsentries + i;
1470 p->procent = create_proc_entry(p->name, p->mode, NULL);
1471 if (p->procent) p->procent->read_proc = p->read_proc;
1475 static void __exit proc_exit(void)
1477 int nelem = sizeof(procfsentries)/sizeof(procfsentries[0]);
1480 for (i=nelem-1; i >= 0; i--) {
1481 struct procfsentries *p = procfsentries + i;
1483 remove_proc_entry(p->name, NULL);
1489 /* -------- init function and module interface ---------------------- */
1492 static char rev[32];
1494 static int __init capi_init(void)
1500 if ((p = strchr(revision, ':')) != 0 && p[1]) {
1501 strlcpy(rev, p + 2, sizeof(rev));
1502 if ((p = strchr(rev, '$')) != 0 && p > rev)
1507 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1508 if (major_ret < 0) {
1509 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1512 capi_class = class_create(THIS_MODULE, "capi");
1513 if (IS_ERR(capi_class)) {
1514 unregister_chrdev(capi_major, "capi20");
1515 return PTR_ERR(capi_class);
1518 class_device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
1520 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1521 if (capinc_tty_init() < 0) {
1522 class_device_destroy(capi_class, MKDEV(capi_major, 0));
1523 class_destroy(capi_class);
1524 unregister_chrdev(capi_major, "capi20");
1527 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1531 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1532 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1533 compileinfo = " (middleware+capifs)";
1535 compileinfo = " (no capifs)";
1538 compileinfo = " (no middleware)";
1540 printk(KERN_NOTICE "capi20: Rev %s: started up with major %d%s\n",
1541 rev, capi_major, compileinfo);
1546 static void __exit capi_exit(void)
1550 class_device_destroy(capi_class, MKDEV(capi_major, 0));
1551 class_destroy(capi_class);
1552 unregister_chrdev(capi_major, "capi20");
1553 devfs_remove("isdn/capi20");
1555 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1558 printk(KERN_NOTICE "capi: Rev %s: unloaded\n", rev);
1561 module_init(capi_init);
1562 module_exit(capi_exit);