1 /* $Id: capi.c,v 1.1.2.7 2004/04/28 09:48:59 armin Exp $
3 * CAPI 2.0 Interface for Linux
5 * Copyright 1996 by Carsten Paeth <calle@calle.de>
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
12 #include <linux/module.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/major.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/fcntl.h>
20 #include <linux/signal.h>
21 #include <linux/mutex.h>
23 #include <linux/timer.h>
24 #include <linux/wait.h>
25 #include <linux/tty.h>
26 #include <linux/netdevice.h>
27 #include <linux/ppp_defs.h>
28 #include <linux/if_ppp.h>
29 #include <linux/skbuff.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/poll.h>
33 #include <linux/capi.h>
34 #include <linux/kernelcapi.h>
35 #include <linux/init.h>
36 #include <linux/device.h>
37 #include <linux/moduleparam.h>
38 #include <linux/isdn/capiutil.h>
39 #include <linux/isdn/capicmd.h>
41 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
42 MODULE_AUTHOR("Carsten Paeth");
43 MODULE_LICENSE("GPL");
45 #undef _DEBUG_TTYFUNCS /* call to tty_driver */
46 #undef _DEBUG_DATAFLOW /* data flow */
48 /* -------- driver information -------------------------------------- */
50 static DEFINE_MUTEX(capi_mutex);
51 static struct class *capi_class;
52 static int capi_major = 68; /* allocated */
54 module_param_named(major, capi_major, uint, 0);
56 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
57 #define CAPINC_NR_PORTS 32
58 #define CAPINC_MAX_PORTS 256
60 static int capi_ttyminors = CAPINC_NR_PORTS;
62 module_param_named(ttyminors, capi_ttyminors, uint, 0);
63 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
65 /* -------- defines ------------------------------------------------- */
67 #define CAPINC_MAX_RECVQUEUE 10
68 #define CAPINC_MAX_SENDQUEUE 10
69 #define CAPI_MAX_BLKSIZE 2048
71 /* -------- data structures ----------------------------------------- */
77 struct ackqueue_entry {
78 struct list_head list;
87 struct capi20_appl *ap;
96 struct sk_buff_head inqueue;
98 struct sk_buff_head outqueue;
100 struct sk_buff *outskb;
104 struct list_head ackqueue;
110 struct list_head list;
112 struct capidev *cdev;
113 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
114 struct capiminor *minorp;
115 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
119 struct list_head list;
120 struct capi20_appl ap;
124 struct sk_buff_head recvqueue;
125 wait_queue_head_t recvwait;
127 struct list_head nccis;
132 /* -------- global variables ---------------------------------------- */
134 static DEFINE_MUTEX(capidev_list_lock);
135 static LIST_HEAD(capidev_list);
137 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
139 static DEFINE_SPINLOCK(capiminors_lock);
140 static struct capiminor **capiminors;
142 static struct tty_driver *capinc_tty_driver;
144 /* -------- datahandles --------------------------------------------- */
146 static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
148 struct ackqueue_entry *n;
150 n = kmalloc(sizeof(*n), GFP_ATOMIC);
152 printk(KERN_ERR "capi: alloc datahandle failed\n");
155 n->datahandle = datahandle;
156 INIT_LIST_HEAD(&n->list);
157 spin_lock_bh(&mp->ackqlock);
158 list_add_tail(&n->list, &mp->ackqueue);
160 spin_unlock_bh(&mp->ackqlock);
164 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
166 struct ackqueue_entry *p, *tmp;
168 spin_lock_bh(&mp->ackqlock);
169 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
170 if (p->datahandle == datahandle) {
173 spin_unlock_bh(&mp->ackqlock);
178 spin_unlock_bh(&mp->ackqlock);
182 static void capiminor_del_all_ack(struct capiminor *mp)
184 struct ackqueue_entry *p, *tmp;
186 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
194 /* -------- struct capiminor ---------------------------------------- */
196 static const struct tty_port_operations capiminor_port_ops; /* we have none */
198 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
200 struct capiminor *mp;
204 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
206 printk(KERN_ERR "capi: can't alloc capiminor\n");
210 kref_init(&mp->kref);
214 INIT_LIST_HEAD(&mp->ackqueue);
215 spin_lock_init(&mp->ackqlock);
217 skb_queue_head_init(&mp->inqueue);
218 skb_queue_head_init(&mp->outqueue);
219 spin_lock_init(&mp->outlock);
221 tty_port_init(&mp->port);
222 mp->port.ops = &capiminor_port_ops;
224 /* Allocate the least unused minor number. */
225 spin_lock(&capiminors_lock);
226 for (minor = 0; minor < capi_ttyminors; minor++)
227 if (!capiminors[minor]) {
228 capiminors[minor] = mp;
231 spin_unlock(&capiminors_lock);
233 if (minor == capi_ttyminors) {
234 printk(KERN_NOTICE "capi: out of minors\n");
240 dev = tty_register_device(capinc_tty_driver, minor, NULL);
247 spin_lock(&capiminors_lock);
248 capiminors[minor] = NULL;
249 spin_unlock(&capiminors_lock);
256 static void capiminor_destroy(struct kref *kref)
258 struct capiminor *mp = container_of(kref, struct capiminor, kref);
260 kfree_skb(mp->outskb);
261 skb_queue_purge(&mp->inqueue);
262 skb_queue_purge(&mp->outqueue);
263 capiminor_del_all_ack(mp);
267 static struct capiminor *capiminor_get(unsigned int minor)
269 struct capiminor *mp;
271 spin_lock(&capiminors_lock);
272 mp = capiminors[minor];
275 spin_unlock(&capiminors_lock);
280 static inline void capiminor_put(struct capiminor *mp)
282 kref_put(&mp->kref, capiminor_destroy);
285 static void capiminor_free(struct capiminor *mp)
287 tty_unregister_device(capinc_tty_driver, mp->minor);
289 spin_lock(&capiminors_lock);
290 capiminors[mp->minor] = NULL;
291 spin_unlock(&capiminors_lock);
296 /* -------- struct capincci ----------------------------------------- */
298 static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
300 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
301 np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
304 static void capincci_free_minor(struct capincci *np)
306 struct capiminor *mp = np->minorp;
307 struct tty_struct *tty;
310 tty = tty_port_tty_get(&mp->port);
320 static inline unsigned int capincci_minor_opencount(struct capincci *np)
322 struct capiminor *mp = np->minorp;
323 unsigned int count = 0;
324 struct tty_struct *tty;
327 tty = tty_port_tty_get(&mp->port);
336 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
339 capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
340 static inline void capincci_free_minor(struct capincci *np) { }
342 static inline unsigned int capincci_minor_opencount(struct capincci *np)
347 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
349 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
353 np = kzalloc(sizeof(*np), GFP_KERNEL);
359 capincci_alloc_minor(cdev, np);
361 list_add_tail(&np->list, &cdev->nccis);
366 static void capincci_free(struct capidev *cdev, u32 ncci)
368 struct capincci *np, *tmp;
370 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
371 if (ncci == 0xffffffff || np->ncci == ncci) {
372 capincci_free_minor(np);
378 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
382 list_for_each_entry(np, &cdev->nccis, list)
383 if (np->ncci == ncci)
388 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
389 /* -------- handle data queue --------------------------------------- */
391 static struct sk_buff *
392 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
394 struct sk_buff *nskb;
395 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_KERNEL);
397 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
398 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
399 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
400 capimsg_setu16(s, 2, mp->ap->applid);
401 capimsg_setu8 (s, 4, CAPI_DATA_B3);
402 capimsg_setu8 (s, 5, CAPI_RESP);
403 capimsg_setu16(s, 6, atomic_inc_return(&mp->msgid));
404 capimsg_setu32(s, 8, mp->ncci);
405 capimsg_setu16(s, 12, datahandle);
410 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
412 unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
413 struct tty_struct *tty;
414 struct sk_buff *nskb;
415 u16 errcode, datahandle;
416 struct tty_ldisc *ld;
419 tty = tty_port_tty_get(&mp->port);
421 #ifdef _DEBUG_DATAFLOW
422 printk(KERN_DEBUG "capi: currently no receiver\n");
427 ld = tty_ldisc_ref(tty);
429 /* fatal error, do not requeue */
435 if (ld->ops->receive_buf == NULL) {
436 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
437 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
439 /* fatal error, do not requeue */
443 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
444 printk(KERN_DEBUG "capi: recv tty throttled\n");
449 if (tty->receive_room < datalen) {
450 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
451 printk(KERN_DEBUG "capi: no room in tty\n");
456 nskb = gen_data_b3_resp_for(mp, skb);
458 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
462 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
464 errcode = capi20_put_message(mp->ap, nskb);
466 if (errcode == CAPI_NOERROR) {
467 skb_pull(skb, CAPIMSG_LEN(skb->data));
468 #ifdef _DEBUG_DATAFLOW
469 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
470 datahandle, skb->len);
472 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
474 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
478 if (errcode == CAPI_SENDQUEUEFULL)
494 static void handle_minor_recv(struct capiminor *mp)
498 while ((skb = skb_dequeue(&mp->inqueue)) != NULL)
499 if (handle_recv_skb(mp, skb) < 0) {
500 skb_queue_head(&mp->inqueue, skb);
505 static void handle_minor_send(struct capiminor *mp)
507 struct tty_struct *tty;
513 tty = tty_port_tty_get(&mp->port);
517 if (mp->ttyoutstop) {
518 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
519 printk(KERN_DEBUG "capi: send: tty stopped\n");
526 spin_lock_bh(&mp->outlock);
527 skb = __skb_dequeue(&mp->outqueue);
529 spin_unlock_bh(&mp->outlock);
534 spin_unlock_bh(&mp->outlock);
536 datahandle = atomic_inc_return(&mp->datahandle);
537 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
538 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
539 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
540 capimsg_setu16(skb->data, 2, mp->ap->applid);
541 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
542 capimsg_setu8 (skb->data, 5, CAPI_REQ);
543 capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid));
544 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
545 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
546 capimsg_setu16(skb->data, 16, len); /* Data length */
547 capimsg_setu16(skb->data, 18, datahandle);
548 capimsg_setu16(skb->data, 20, 0); /* Flags */
550 if (capiminor_add_ack(mp, datahandle) < 0) {
551 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
553 spin_lock_bh(&mp->outlock);
554 __skb_queue_head(&mp->outqueue, skb);
556 spin_unlock_bh(&mp->outlock);
560 errcode = capi20_put_message(mp->ap, skb);
561 if (errcode == CAPI_NOERROR) {
562 #ifdef _DEBUG_DATAFLOW
563 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
568 capiminor_del_ack(mp, datahandle);
570 if (errcode == CAPI_SENDQUEUEFULL) {
571 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
573 spin_lock_bh(&mp->outlock);
574 __skb_queue_head(&mp->outqueue, skb);
576 spin_unlock_bh(&mp->outlock);
581 /* ups, drop packet */
582 printk(KERN_ERR "capi: put_message = %x\n", errcode);
588 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
589 /* -------- function called by lower level -------------------------- */
591 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
593 struct capidev *cdev = ap->private;
594 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
595 struct tty_struct *tty;
596 struct capiminor *mp;
598 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
601 mutex_lock(&cdev->lock);
603 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
604 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
605 if ((info & 0xff00) == 0)
606 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
608 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
609 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
611 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
612 skb_queue_tail(&cdev->recvqueue, skb);
613 wake_up_interruptible(&cdev->recvwait);
617 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
619 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
620 skb_queue_tail(&cdev->recvqueue, skb);
621 wake_up_interruptible(&cdev->recvwait);
625 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
626 skb_queue_tail(&cdev->recvqueue, skb);
627 wake_up_interruptible(&cdev->recvwait);
629 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
633 skb_queue_tail(&cdev->recvqueue, skb);
634 wake_up_interruptible(&cdev->recvwait);
637 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
638 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
639 #ifdef _DEBUG_DATAFLOW
640 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
641 datahandle, skb->len-CAPIMSG_LEN(skb->data));
643 skb_queue_tail(&mp->inqueue, skb);
645 handle_minor_recv(mp);
647 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
649 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
650 #ifdef _DEBUG_DATAFLOW
651 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
653 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
656 capiminor_del_ack(mp, datahandle);
657 tty = tty_port_tty_get(&mp->port);
662 handle_minor_send(mp);
665 /* ups, let capi application handle it :-) */
666 skb_queue_tail(&cdev->recvqueue, skb);
667 wake_up_interruptible(&cdev->recvwait);
669 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
672 mutex_unlock(&cdev->lock);
675 /* -------- file_operations for capidev ----------------------------- */
678 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
680 struct capidev *cdev = file->private_data;
685 if (!cdev->ap.applid)
688 skb = skb_dequeue(&cdev->recvqueue);
690 if (file->f_flags & O_NONBLOCK)
692 err = wait_event_interruptible(cdev->recvwait,
693 (skb = skb_dequeue(&cdev->recvqueue)));
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 = 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 mutex_lock(&cdev->lock);
746 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
747 mutex_unlock(&cdev->lock);
750 cdev->errcode = capi20_put_message(&cdev->ap, skb);
760 capi_poll(struct file *file, poll_table * wait)
762 struct capidev *cdev = 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 file *file, unsigned int cmd, unsigned long arg)
778 struct capidev *cdev = file->private_data;
779 capi_ioctl_struct data;
780 int retval = -EINVAL;
781 void __user *argp = (void __user *)arg;
785 mutex_lock(&cdev->lock);
787 if (cdev->ap.applid) {
791 if (copy_from_user(&cdev->ap.rparam, argp,
792 sizeof(struct capi_register_params))) {
796 cdev->ap.private = cdev;
797 cdev->ap.recv_message = capi_recv_message;
798 cdev->errcode = capi20_register(&cdev->ap);
799 retval = (int)cdev->ap.applid;
806 mutex_unlock(&cdev->lock);
809 case CAPI_GET_VERSION:
811 if (copy_from_user(&data.contr, argp,
814 cdev->errcode = capi20_get_version(data.contr, &data.version);
817 if (copy_to_user(argp, &data.version,
818 sizeof(data.version)))
823 case CAPI_GET_SERIAL:
825 if (copy_from_user(&data.contr, argp,
828 cdev->errcode = capi20_get_serial (data.contr, data.serial);
831 if (copy_to_user(argp, data.serial,
832 sizeof(data.serial)))
836 case CAPI_GET_PROFILE:
838 if (copy_from_user(&data.contr, argp,
842 if (data.contr == 0) {
843 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
847 retval = copy_to_user(argp,
848 &data.profile.ncontroller,
849 sizeof(data.profile.ncontroller));
852 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
856 retval = copy_to_user(argp, &data.profile,
857 sizeof(data.profile));
864 case CAPI_GET_MANUFACTURER:
866 if (copy_from_user(&data.contr, argp,
869 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
873 if (copy_to_user(argp, data.manufacturer,
874 sizeof(data.manufacturer)))
879 case CAPI_GET_ERRCODE:
880 data.errcode = cdev->errcode;
881 cdev->errcode = CAPI_NOERROR;
883 if (copy_to_user(argp, &data.errcode,
884 sizeof(data.errcode)))
890 if (capi20_isinstalled() == CAPI_NOERROR)
894 case CAPI_MANUFACTURER_CMD:
896 struct capi_manufacturer_cmd mcmd;
897 if (!capable(CAP_SYS_ADMIN))
899 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
901 return capi20_manufacturer(mcmd.cmd, mcmd.data);
906 case CAPI_CLR_FLAGS: {
909 if (copy_from_user(&userflags, argp, sizeof(userflags)))
912 mutex_lock(&cdev->lock);
913 if (cmd == CAPI_SET_FLAGS)
914 cdev->userflags |= userflags;
916 cdev->userflags &= ~userflags;
917 mutex_unlock(&cdev->lock);
921 if (copy_to_user(argp, &cdev->userflags,
922 sizeof(cdev->userflags)))
926 case CAPI_NCCI_OPENCOUNT: {
927 struct capincci *nccip;
931 if (copy_from_user(&ncci, argp, sizeof(ncci)))
934 mutex_lock(&cdev->lock);
935 nccip = capincci_find(cdev, (u32)ncci);
937 count = capincci_minor_opencount(nccip);
938 mutex_unlock(&cdev->lock);
942 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
943 case CAPI_NCCI_GETUNIT: {
944 struct capincci *nccip;
945 struct capiminor *mp;
949 if (copy_from_user(&ncci, argp, sizeof(ncci)))
952 mutex_lock(&cdev->lock);
953 nccip = capincci_find(cdev, (u32)ncci);
959 mutex_unlock(&cdev->lock);
962 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
970 capi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
974 mutex_lock(&capi_mutex);
975 ret = capi_ioctl(file, cmd, arg);
976 mutex_unlock(&capi_mutex);
981 static int capi_open(struct inode *inode, struct file *file)
983 struct capidev *cdev;
985 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
989 mutex_init(&cdev->lock);
990 skb_queue_head_init(&cdev->recvqueue);
991 init_waitqueue_head(&cdev->recvwait);
992 INIT_LIST_HEAD(&cdev->nccis);
993 file->private_data = cdev;
995 mutex_lock(&capidev_list_lock);
996 list_add_tail(&cdev->list, &capidev_list);
997 mutex_unlock(&capidev_list_lock);
999 return nonseekable_open(inode, file);
1002 static int capi_release(struct inode *inode, struct file *file)
1004 struct capidev *cdev = file->private_data;
1006 mutex_lock(&capidev_list_lock);
1007 list_del(&cdev->list);
1008 mutex_unlock(&capidev_list_lock);
1010 if (cdev->ap.applid)
1011 capi20_release(&cdev->ap);
1012 skb_queue_purge(&cdev->recvqueue);
1013 capincci_free(cdev, 0xffffffff);
1019 static const struct file_operations capi_fops =
1021 .owner = THIS_MODULE,
1022 .llseek = no_llseek,
1024 .write = capi_write,
1026 .unlocked_ioctl = capi_unlocked_ioctl,
1028 .release = capi_release,
1031 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1032 /* -------- tty_operations for capincci ----------------------------- */
1035 capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1037 int idx = tty->index;
1038 struct capiminor *mp = capiminor_get(idx);
1039 int ret = tty_init_termios(tty);
1042 tty_driver_kref_get(driver);
1044 tty->driver_data = mp;
1045 driver->ttys[idx] = tty;
1051 static void capinc_tty_cleanup(struct tty_struct *tty)
1053 struct capiminor *mp = tty->driver_data;
1054 tty->driver_data = NULL;
1058 static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
1060 struct capiminor *mp = tty->driver_data;
1063 err = tty_port_open(&mp->port, tty, filp);
1067 handle_minor_recv(mp);
1071 static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
1073 struct capiminor *mp = tty->driver_data;
1075 tty_port_close(&mp->port, tty, filp);
1078 static int capinc_tty_write(struct tty_struct *tty,
1079 const unsigned char *buf, int count)
1081 struct capiminor *mp = tty->driver_data;
1082 struct sk_buff *skb;
1084 #ifdef _DEBUG_TTYFUNCS
1085 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1088 spin_lock_bh(&mp->outlock);
1092 __skb_queue_tail(&mp->outqueue, skb);
1093 mp->outbytes += skb->len;
1096 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1098 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1099 spin_unlock_bh(&mp->outlock);
1103 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1104 memcpy(skb_put(skb, count), buf, count);
1106 __skb_queue_tail(&mp->outqueue, skb);
1107 mp->outbytes += skb->len;
1108 spin_unlock_bh(&mp->outlock);
1110 handle_minor_send(mp);
1115 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1117 struct capiminor *mp = tty->driver_data;
1118 bool invoke_send = false;
1119 struct sk_buff *skb;
1122 #ifdef _DEBUG_TTYFUNCS
1123 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1126 spin_lock_bh(&mp->outlock);
1129 if (skb_tailroom(skb) > 0) {
1130 *(skb_put(skb, 1)) = ch;
1134 __skb_queue_tail(&mp->outqueue, skb);
1135 mp->outbytes += skb->len;
1139 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1141 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1142 *(skb_put(skb, 1)) = ch;
1145 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1150 spin_unlock_bh(&mp->outlock);
1153 handle_minor_send(mp);
1158 static void capinc_tty_flush_chars(struct tty_struct *tty)
1160 struct capiminor *mp = tty->driver_data;
1161 struct sk_buff *skb;
1163 #ifdef _DEBUG_TTYFUNCS
1164 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1167 spin_lock_bh(&mp->outlock);
1171 __skb_queue_tail(&mp->outqueue, skb);
1172 mp->outbytes += skb->len;
1173 spin_unlock_bh(&mp->outlock);
1175 handle_minor_send(mp);
1177 spin_unlock_bh(&mp->outlock);
1179 handle_minor_recv(mp);
1182 static int capinc_tty_write_room(struct tty_struct *tty)
1184 struct capiminor *mp = tty->driver_data;
1187 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1188 room *= CAPI_MAX_BLKSIZE;
1189 #ifdef _DEBUG_TTYFUNCS
1190 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1195 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1197 struct capiminor *mp = tty->driver_data;
1199 #ifdef _DEBUG_TTYFUNCS
1200 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1201 mp->outbytes, mp->nack,
1202 skb_queue_len(&mp->outqueue),
1203 skb_queue_len(&mp->inqueue));
1205 return mp->outbytes;
1208 static int capinc_tty_ioctl(struct tty_struct *tty,
1209 unsigned int cmd, unsigned long arg)
1211 return -ENOIOCTLCMD;
1214 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
1216 #ifdef _DEBUG_TTYFUNCS
1217 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1221 static void capinc_tty_throttle(struct tty_struct *tty)
1223 struct capiminor *mp = tty->driver_data;
1224 #ifdef _DEBUG_TTYFUNCS
1225 printk(KERN_DEBUG "capinc_tty_throttle\n");
1230 static void capinc_tty_unthrottle(struct tty_struct *tty)
1232 struct capiminor *mp = tty->driver_data;
1234 #ifdef _DEBUG_TTYFUNCS
1235 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1238 handle_minor_recv(mp);
1241 static void capinc_tty_stop(struct tty_struct *tty)
1243 struct capiminor *mp = tty->driver_data;
1245 #ifdef _DEBUG_TTYFUNCS
1246 printk(KERN_DEBUG "capinc_tty_stop\n");
1251 static void capinc_tty_start(struct tty_struct *tty)
1253 struct capiminor *mp = tty->driver_data;
1255 #ifdef _DEBUG_TTYFUNCS
1256 printk(KERN_DEBUG "capinc_tty_start\n");
1259 handle_minor_send(mp);
1262 static void capinc_tty_hangup(struct tty_struct *tty)
1264 struct capiminor *mp = tty->driver_data;
1266 #ifdef _DEBUG_TTYFUNCS
1267 printk(KERN_DEBUG "capinc_tty_hangup\n");
1269 tty_port_hangup(&mp->port);
1272 static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1274 #ifdef _DEBUG_TTYFUNCS
1275 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1280 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1282 #ifdef _DEBUG_TTYFUNCS
1283 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1287 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1289 #ifdef _DEBUG_TTYFUNCS
1290 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1294 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1296 #ifdef _DEBUG_TTYFUNCS
1297 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1301 static const struct tty_operations capinc_ops = {
1302 .open = capinc_tty_open,
1303 .close = capinc_tty_close,
1304 .write = capinc_tty_write,
1305 .put_char = capinc_tty_put_char,
1306 .flush_chars = capinc_tty_flush_chars,
1307 .write_room = capinc_tty_write_room,
1308 .chars_in_buffer = capinc_tty_chars_in_buffer,
1309 .ioctl = capinc_tty_ioctl,
1310 .set_termios = capinc_tty_set_termios,
1311 .throttle = capinc_tty_throttle,
1312 .unthrottle = capinc_tty_unthrottle,
1313 .stop = capinc_tty_stop,
1314 .start = capinc_tty_start,
1315 .hangup = capinc_tty_hangup,
1316 .break_ctl = capinc_tty_break_ctl,
1317 .flush_buffer = capinc_tty_flush_buffer,
1318 .set_ldisc = capinc_tty_set_ldisc,
1319 .send_xchar = capinc_tty_send_xchar,
1320 .install = capinc_tty_install,
1321 .cleanup = capinc_tty_cleanup,
1324 static int __init capinc_tty_init(void)
1326 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 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1339 drv = alloc_tty_driver(capi_ttyminors);
1344 drv->owner = THIS_MODULE;
1345 drv->driver_name = "capi_nc";
1348 drv->minor_start = 0;
1349 drv->type = TTY_DRIVER_TYPE_SERIAL;
1350 drv->subtype = SERIAL_TYPE_NORMAL;
1351 drv->init_termios = tty_std_termios;
1352 drv->init_termios.c_iflag = ICRNL;
1353 drv->init_termios.c_oflag = OPOST | ONLCR;
1354 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1355 drv->init_termios.c_lflag = 0;
1357 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1358 TTY_DRIVER_DYNAMIC_DEV;
1359 tty_set_operations(drv, &capinc_ops);
1361 err = tty_register_driver(drv);
1363 put_tty_driver(drv);
1365 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1368 capinc_tty_driver = drv;
1372 static void __exit capinc_tty_exit(void)
1374 tty_unregister_driver(capinc_tty_driver);
1375 put_tty_driver(capinc_tty_driver);
1379 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1381 static inline int capinc_tty_init(void)
1386 static inline void capinc_tty_exit(void) { }
1388 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1390 /* -------- /proc functions ----------------------------------------- */
1393 * /proc/capi/capi20:
1394 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1396 static int capi20_proc_show(struct seq_file *m, void *v)
1398 struct capidev *cdev;
1399 struct list_head *l;
1401 mutex_lock(&capidev_list_lock);
1402 list_for_each(l, &capidev_list) {
1403 cdev = list_entry(l, struct capidev, list);
1404 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
1406 cdev->ap.nrecvctlpkt,
1407 cdev->ap.nrecvdatapkt,
1408 cdev->ap.nsentctlpkt,
1409 cdev->ap.nsentdatapkt);
1411 mutex_unlock(&capidev_list_lock);
1415 static int capi20_proc_open(struct inode *inode, struct file *file)
1417 return single_open(file, capi20_proc_show, NULL);
1420 static const struct file_operations capi20_proc_fops = {
1421 .owner = THIS_MODULE,
1422 .open = capi20_proc_open,
1424 .llseek = seq_lseek,
1425 .release = single_release,
1429 * /proc/capi/capi20ncci:
1432 static int capi20ncci_proc_show(struct seq_file *m, void *v)
1434 struct capidev *cdev;
1435 struct capincci *np;
1437 mutex_lock(&capidev_list_lock);
1438 list_for_each_entry(cdev, &capidev_list, list) {
1439 mutex_lock(&cdev->lock);
1440 list_for_each_entry(np, &cdev->nccis, list)
1441 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
1442 mutex_unlock(&cdev->lock);
1444 mutex_unlock(&capidev_list_lock);
1448 static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1450 return single_open(file, capi20ncci_proc_show, NULL);
1453 static const struct file_operations capi20ncci_proc_fops = {
1454 .owner = THIS_MODULE,
1455 .open = capi20ncci_proc_open,
1457 .llseek = seq_lseek,
1458 .release = single_release,
1461 static void __init proc_init(void)
1463 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1464 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
1467 static void __exit proc_exit(void)
1469 remove_proc_entry("capi/capi20", NULL);
1470 remove_proc_entry("capi/capi20ncci", NULL);
1473 /* -------- init function and module interface ---------------------- */
1476 static int __init capi_init(void)
1478 const char *compileinfo;
1481 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1482 if (major_ret < 0) {
1483 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1486 capi_class = class_create(THIS_MODULE, "capi");
1487 if (IS_ERR(capi_class)) {
1488 unregister_chrdev(capi_major, "capi20");
1489 return PTR_ERR(capi_class);
1492 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
1494 if (capinc_tty_init() < 0) {
1495 device_destroy(capi_class, MKDEV(capi_major, 0));
1496 class_destroy(capi_class);
1497 unregister_chrdev(capi_major, "capi20");
1503 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1504 compileinfo = " (middleware)";
1506 compileinfo = " (no middleware)";
1508 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1509 capi_major, compileinfo);
1514 static void __exit capi_exit(void)
1518 device_destroy(capi_class, MKDEV(capi_major, 0));
1519 class_destroy(capi_class);
1520 unregister_chrdev(capi_major, "capi20");
1525 module_init(capi_init);
1526 module_exit(capi_exit);