1 /* $Id: capi.c,v 1.1.2.7 2004/04/28 09:48:59 armin Exp $
3 * CAPI 2.0 Interface for Linux
5 * Copyright 1996 by Carsten Paeth <calle@calle.de>
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
12 #include <linux/module.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/major.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/fcntl.h>
20 #include <linux/signal.h>
21 #include <linux/mutex.h>
23 #include <linux/smp_lock.h>
24 #include <linux/timer.h>
25 #include <linux/wait.h>
26 #include <linux/tty.h>
27 #include <linux/netdevice.h>
28 #include <linux/ppp_defs.h>
29 #include <linux/if_ppp.h>
30 #include <linux/skbuff.h>
31 #include <linux/proc_fs.h>
32 #include <linux/seq_file.h>
33 #include <linux/poll.h>
34 #include <linux/capi.h>
35 #include <linux/kernelcapi.h>
36 #include <linux/init.h>
37 #include <linux/device.h>
38 #include <linux/moduleparam.h>
39 #include <linux/isdn/capiutil.h>
40 #include <linux/isdn/capicmd.h>
44 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
45 MODULE_AUTHOR("Carsten Paeth");
46 MODULE_LICENSE("GPL");
48 #undef _DEBUG_TTYFUNCS /* call to tty_driver */
49 #undef _DEBUG_DATAFLOW /* data flow */
51 /* -------- driver information -------------------------------------- */
53 static struct class *capi_class;
54 static int capi_major = 68; /* allocated */
56 module_param_named(major, capi_major, uint, 0);
58 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
59 #define CAPINC_NR_PORTS 32
60 #define CAPINC_MAX_PORTS 256
62 static int capi_ttyminors = CAPINC_NR_PORTS;
64 module_param_named(ttyminors, capi_ttyminors, uint, 0);
65 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
67 /* -------- defines ------------------------------------------------- */
69 #define CAPINC_MAX_RECVQUEUE 10
70 #define CAPINC_MAX_SENDQUEUE 10
71 #define CAPI_MAX_BLKSIZE 2048
73 /* -------- data structures ----------------------------------------- */
79 struct ackqueue_entry {
80 struct list_head list;
88 struct dentry *capifs_dentry;
90 struct capi20_appl *ap;
98 struct sk_buff *ttyskb;
100 struct sk_buff_head inqueue;
101 struct sk_buff_head outqueue;
105 struct list_head ackqueue;
110 /* FIXME: The following lock is a sledgehammer-workaround to a
111 * locking issue with the capiminor (and maybe other) data structure(s).
112 * Access to this data is done in a racy way and crashes the machine with
113 * a FritzCard DSL driver; sooner or later. This is a workaround
114 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
115 * The correct (and scalable) fix for the issue seems to require
116 * an API change to the drivers... . */
117 static DEFINE_SPINLOCK(workaround_lock);
120 struct list_head list;
122 struct capidev *cdev;
123 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
124 struct capiminor *minorp;
125 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
129 struct list_head list;
130 struct capi20_appl ap;
134 struct sk_buff_head recvqueue;
135 wait_queue_head_t recvwait;
137 struct list_head nccis;
142 /* -------- global variables ---------------------------------------- */
144 static DEFINE_MUTEX(capidev_list_lock);
145 static LIST_HEAD(capidev_list);
147 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
149 static DEFINE_SPINLOCK(capiminors_lock);
150 static struct capiminor **capiminors;
152 static struct tty_driver *capinc_tty_driver;
154 /* -------- datahandles --------------------------------------------- */
156 static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
158 struct ackqueue_entry *n;
161 n = kmalloc(sizeof(*n), GFP_ATOMIC);
163 printk(KERN_ERR "capi: alloc datahandle failed\n");
166 n->datahandle = datahandle;
167 INIT_LIST_HEAD(&n->list);
168 spin_lock_irqsave(&mp->ackqlock, flags);
169 list_add_tail(&n->list, &mp->ackqueue);
171 spin_unlock_irqrestore(&mp->ackqlock, flags);
175 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
177 struct ackqueue_entry *p, *tmp;
180 spin_lock_irqsave(&mp->ackqlock, flags);
181 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
182 if (p->datahandle == datahandle) {
186 spin_unlock_irqrestore(&mp->ackqlock, flags);
190 spin_unlock_irqrestore(&mp->ackqlock, flags);
194 static void capiminor_del_all_ack(struct capiminor *mp)
196 struct ackqueue_entry *p, *tmp;
199 spin_lock_irqsave(&mp->ackqlock, flags);
200 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
205 spin_unlock_irqrestore(&mp->ackqlock, flags);
209 /* -------- struct capiminor ---------------------------------------- */
211 static const struct tty_port_operations capiminor_port_ops; /* we have none */
213 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
215 struct capiminor *mp;
219 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
221 printk(KERN_ERR "capi: can't alloc capiminor\n");
225 kref_init(&mp->kref);
229 INIT_LIST_HEAD(&mp->ackqueue);
230 spin_lock_init(&mp->ackqlock);
232 skb_queue_head_init(&mp->inqueue);
233 skb_queue_head_init(&mp->outqueue);
235 tty_port_init(&mp->port);
236 mp->port.ops = &capiminor_port_ops;
238 /* Allocate the least unused minor number. */
239 spin_lock(&capiminors_lock);
240 for (minor = 0; minor < capi_ttyminors; minor++)
241 if (!capiminors[minor]) {
242 capiminors[minor] = mp;
245 spin_unlock(&capiminors_lock);
247 if (minor == capi_ttyminors) {
248 printk(KERN_NOTICE "capi: out of minors\n");
254 dev = tty_register_device(capinc_tty_driver, minor, NULL);
261 spin_lock(&capiminors_lock);
262 capiminors[minor] = NULL;
263 spin_unlock(&capiminors_lock);
270 static void capiminor_destroy(struct kref *kref)
272 struct capiminor *mp = container_of(kref, struct capiminor, kref);
274 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_get(unsigned int minor)
283 struct capiminor *mp;
285 spin_lock(&capiminors_lock);
286 mp = capiminors[minor];
289 spin_unlock(&capiminors_lock);
294 static inline void capiminor_put(struct capiminor *mp)
296 kref_put(&mp->kref, capiminor_destroy);
299 static void capiminor_free(struct capiminor *mp)
301 tty_unregister_device(capinc_tty_driver, mp->minor);
303 spin_lock(&capiminors_lock);
304 capiminors[mp->minor] = NULL;
305 spin_unlock(&capiminors_lock);
310 /* -------- struct capincci ----------------------------------------- */
312 static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
314 struct capiminor *mp;
317 if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
320 mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
322 device = MKDEV(capinc_tty_driver->major, mp->minor);
323 mp->capifs_dentry = capifs_new_ncci(mp->minor, device);
327 static void capincci_free_minor(struct capincci *np)
329 struct capiminor *mp = np->minorp;
330 struct tty_struct *tty;
333 capifs_free_ncci(mp->capifs_dentry);
335 tty = tty_port_tty_get(&mp->port);
345 static inline unsigned int capincci_minor_opencount(struct capincci *np)
347 struct capiminor *mp = np->minorp;
348 unsigned int count = 0;
349 struct tty_struct *tty;
352 tty = tty_port_tty_get(&mp->port);
361 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
364 capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
365 static inline void capincci_free_minor(struct capincci *np) { }
367 static inline unsigned int capincci_minor_opencount(struct capincci *np)
372 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
374 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
378 np = kzalloc(sizeof(*np), GFP_KERNEL);
384 capincci_alloc_minor(cdev, np);
386 list_add_tail(&np->list, &cdev->nccis);
391 static void capincci_free(struct capidev *cdev, u32 ncci)
393 struct capincci *np, *tmp;
395 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
396 if (ncci == 0xffffffff || np->ncci == ncci) {
397 capincci_free_minor(np);
403 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
407 list_for_each_entry(np, &cdev->nccis, list)
408 if (np->ncci == ncci)
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, atomic_inc_return(&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 unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
438 struct tty_struct *tty;
439 struct sk_buff *nskb;
440 u16 errcode, datahandle;
441 struct tty_ldisc *ld;
444 tty = tty_port_tty_get(&mp->port);
446 #ifdef _DEBUG_DATAFLOW
447 printk(KERN_DEBUG "capi: currently no receiver\n");
452 ld = tty_ldisc_ref(tty);
454 /* fatal error, do not requeue */
460 if (ld->ops->receive_buf == NULL) {
461 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
462 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
464 /* fatal error, do not requeue */
468 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
469 printk(KERN_DEBUG "capi: recv tty throttled\n");
474 if (tty->receive_room < datalen) {
475 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
476 printk(KERN_DEBUG "capi: no room in tty\n");
481 nskb = gen_data_b3_resp_for(mp, skb);
483 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
487 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
489 errcode = capi20_put_message(mp->ap, nskb);
491 if (errcode == CAPI_NOERROR) {
492 skb_pull(skb, CAPIMSG_LEN(skb->data));
493 #ifdef _DEBUG_DATAFLOW
494 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
495 datahandle, skb->len);
497 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
499 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
503 if (errcode == CAPI_SENDQUEUEFULL)
519 static void handle_minor_recv(struct capiminor *mp)
523 while ((skb = skb_dequeue(&mp->inqueue)) != NULL)
524 if (handle_recv_skb(mp, skb) < 0) {
525 skb_queue_head(&mp->inqueue, skb);
530 static int handle_minor_send(struct capiminor *mp)
532 struct tty_struct *tty;
539 tty = tty_port_tty_get(&mp->port);
543 if (mp->ttyoutstop) {
544 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
545 printk(KERN_DEBUG "capi: send: tty stopped\n");
551 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
552 datahandle = atomic_inc_return(&mp->datahandle);
554 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
555 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
556 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
557 capimsg_setu16(skb->data, 2, mp->ap->applid);
558 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
559 capimsg_setu8 (skb->data, 5, CAPI_REQ);
560 capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid));
561 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
562 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
563 capimsg_setu16(skb->data, 16, len); /* Data length */
564 capimsg_setu16(skb->data, 18, datahandle);
565 capimsg_setu16(skb->data, 20, 0); /* Flags */
567 if (capiminor_add_ack(mp, datahandle) < 0) {
568 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
569 skb_queue_head(&mp->outqueue, skb);
573 errcode = capi20_put_message(mp->ap, skb);
574 if (errcode == CAPI_NOERROR) {
577 #ifdef _DEBUG_DATAFLOW
578 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
583 capiminor_del_ack(mp, datahandle);
585 if (errcode == CAPI_SENDQUEUEFULL) {
586 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
587 skb_queue_head(&mp->outqueue, skb);
591 /* ups, drop packet */
592 printk(KERN_ERR "capi: put_message = %x\n", errcode);
600 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
601 /* -------- function called by lower level -------------------------- */
603 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
605 struct capidev *cdev = ap->private;
606 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
607 struct tty_struct *tty;
608 struct capiminor *mp;
610 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
614 mutex_lock(&cdev->lock);
616 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
617 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
618 if ((info & 0xff00) == 0)
619 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
621 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
622 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
624 spin_lock_irqsave(&workaround_lock, flags);
625 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
626 skb_queue_tail(&cdev->recvqueue, skb);
627 wake_up_interruptible(&cdev->recvwait);
631 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
633 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
634 skb_queue_tail(&cdev->recvqueue, skb);
635 wake_up_interruptible(&cdev->recvwait);
639 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
640 skb_queue_tail(&cdev->recvqueue, skb);
641 wake_up_interruptible(&cdev->recvwait);
643 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
647 skb_queue_tail(&cdev->recvqueue, skb);
648 wake_up_interruptible(&cdev->recvwait);
651 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
652 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
653 #ifdef _DEBUG_DATAFLOW
654 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
655 datahandle, skb->len-CAPIMSG_LEN(skb->data));
657 skb_queue_tail(&mp->inqueue, skb);
659 handle_minor_recv(mp);
661 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
663 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
664 #ifdef _DEBUG_DATAFLOW
665 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
667 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
670 (void)capiminor_del_ack(mp, datahandle);
671 tty = tty_port_tty_get(&mp->port);
676 (void)handle_minor_send(mp);
679 /* ups, let capi application handle it :-) */
680 skb_queue_tail(&cdev->recvqueue, skb);
681 wake_up_interruptible(&cdev->recvwait);
683 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
686 spin_unlock_irqrestore(&workaround_lock, flags);
687 mutex_unlock(&cdev->lock);
690 /* -------- file_operations for capidev ----------------------------- */
693 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
695 struct capidev *cdev = (struct capidev *)file->private_data;
700 if (!cdev->ap.applid)
703 skb = skb_dequeue(&cdev->recvqueue);
705 if (file->f_flags & O_NONBLOCK)
707 err = wait_event_interruptible(cdev->recvwait,
708 (skb = skb_dequeue(&cdev->recvqueue)));
712 if (skb->len > count) {
713 skb_queue_head(&cdev->recvqueue, skb);
716 if (copy_to_user(buf, skb->data, skb->len)) {
717 skb_queue_head(&cdev->recvqueue, skb);
728 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
730 struct capidev *cdev = (struct capidev *)file->private_data;
734 if (!cdev->ap.applid)
737 skb = alloc_skb(count, GFP_USER);
741 if (copy_from_user(skb_put(skb, count), buf, count)) {
745 mlen = CAPIMSG_LEN(skb->data);
746 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
747 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
757 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
759 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
760 mutex_lock(&cdev->lock);
761 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
762 mutex_unlock(&cdev->lock);
765 cdev->errcode = capi20_put_message(&cdev->ap, skb);
775 capi_poll(struct file *file, poll_table * wait)
777 struct capidev *cdev = (struct capidev *)file->private_data;
778 unsigned int mask = 0;
780 if (!cdev->ap.applid)
783 poll_wait(file, &(cdev->recvwait), wait);
784 mask = POLLOUT | POLLWRNORM;
785 if (!skb_queue_empty(&cdev->recvqueue))
786 mask |= POLLIN | POLLRDNORM;
791 capi_ioctl(struct inode *inode, struct file *file,
792 unsigned int cmd, unsigned long arg)
794 struct capidev *cdev = file->private_data;
795 capi_ioctl_struct data;
796 int retval = -EINVAL;
797 void __user *argp = (void __user *)arg;
801 mutex_lock(&cdev->lock);
803 if (cdev->ap.applid) {
807 if (copy_from_user(&cdev->ap.rparam, argp,
808 sizeof(struct capi_register_params))) {
812 cdev->ap.private = cdev;
813 cdev->ap.recv_message = capi_recv_message;
814 cdev->errcode = capi20_register(&cdev->ap);
815 retval = (int)cdev->ap.applid;
822 mutex_unlock(&cdev->lock);
825 case CAPI_GET_VERSION:
827 if (copy_from_user(&data.contr, argp,
830 cdev->errcode = capi20_get_version(data.contr, &data.version);
833 if (copy_to_user(argp, &data.version,
834 sizeof(data.version)))
839 case CAPI_GET_SERIAL:
841 if (copy_from_user(&data.contr, argp,
844 cdev->errcode = capi20_get_serial (data.contr, data.serial);
847 if (copy_to_user(argp, data.serial,
848 sizeof(data.serial)))
852 case CAPI_GET_PROFILE:
854 if (copy_from_user(&data.contr, argp,
858 if (data.contr == 0) {
859 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
863 retval = copy_to_user(argp,
864 &data.profile.ncontroller,
865 sizeof(data.profile.ncontroller));
868 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
872 retval = copy_to_user(argp, &data.profile,
873 sizeof(data.profile));
880 case CAPI_GET_MANUFACTURER:
882 if (copy_from_user(&data.contr, argp,
885 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
889 if (copy_to_user(argp, data.manufacturer,
890 sizeof(data.manufacturer)))
895 case CAPI_GET_ERRCODE:
896 data.errcode = cdev->errcode;
897 cdev->errcode = CAPI_NOERROR;
899 if (copy_to_user(argp, &data.errcode,
900 sizeof(data.errcode)))
906 if (capi20_isinstalled() == CAPI_NOERROR)
910 case CAPI_MANUFACTURER_CMD:
912 struct capi_manufacturer_cmd mcmd;
913 if (!capable(CAP_SYS_ADMIN))
915 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
917 return capi20_manufacturer(mcmd.cmd, mcmd.data);
922 case CAPI_CLR_FLAGS: {
925 if (copy_from_user(&userflags, argp, sizeof(userflags)))
928 mutex_lock(&cdev->lock);
929 if (cmd == CAPI_SET_FLAGS)
930 cdev->userflags |= userflags;
932 cdev->userflags &= ~userflags;
933 mutex_unlock(&cdev->lock);
937 if (copy_to_user(argp, &cdev->userflags,
938 sizeof(cdev->userflags)))
942 case CAPI_NCCI_OPENCOUNT: {
943 struct capincci *nccip;
947 if (copy_from_user(&ncci, argp, sizeof(ncci)))
950 mutex_lock(&cdev->lock);
951 nccip = capincci_find(cdev, (u32)ncci);
953 count = capincci_minor_opencount(nccip);
954 mutex_unlock(&cdev->lock);
958 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
959 case CAPI_NCCI_GETUNIT: {
960 struct capincci *nccip;
961 struct capiminor *mp;
965 if (copy_from_user(&ncci, argp, sizeof(ncci)))
968 mutex_lock(&cdev->lock);
969 nccip = capincci_find(cdev, (u32)ncci);
975 mutex_unlock(&cdev->lock);
978 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
985 static int capi_open(struct inode *inode, struct file *file)
987 struct capidev *cdev;
989 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
993 mutex_init(&cdev->lock);
994 skb_queue_head_init(&cdev->recvqueue);
995 init_waitqueue_head(&cdev->recvwait);
996 INIT_LIST_HEAD(&cdev->nccis);
997 file->private_data = cdev;
999 mutex_lock(&capidev_list_lock);
1000 list_add_tail(&cdev->list, &capidev_list);
1001 mutex_unlock(&capidev_list_lock);
1003 return nonseekable_open(inode, file);
1006 static int capi_release(struct inode *inode, struct file *file)
1008 struct capidev *cdev = file->private_data;
1010 mutex_lock(&capidev_list_lock);
1011 list_del(&cdev->list);
1012 mutex_unlock(&capidev_list_lock);
1014 if (cdev->ap.applid)
1015 capi20_release(&cdev->ap);
1016 skb_queue_purge(&cdev->recvqueue);
1017 capincci_free(cdev, 0xffffffff);
1023 static const struct file_operations capi_fops =
1025 .owner = THIS_MODULE,
1026 .llseek = no_llseek,
1028 .write = capi_write,
1030 .ioctl = capi_ioctl,
1032 .release = capi_release,
1035 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1036 /* -------- tty_operations for capincci ----------------------------- */
1039 capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1041 int idx = tty->index;
1042 struct capiminor *mp = capiminor_get(idx);
1043 int ret = tty_init_termios(tty);
1046 tty_driver_kref_get(driver);
1048 tty->driver_data = mp;
1049 driver->ttys[idx] = tty;
1055 static void capinc_tty_cleanup(struct tty_struct *tty)
1057 struct capiminor *mp = tty->driver_data;
1058 tty->driver_data = NULL;
1062 static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
1064 struct capiminor *mp = tty->driver_data;
1065 unsigned long flags;
1068 err = tty_port_open(&mp->port, tty, filp);
1072 spin_lock_irqsave(&workaround_lock, flags);
1073 handle_minor_recv(mp);
1074 spin_unlock_irqrestore(&workaround_lock, flags);
1078 static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
1080 struct capiminor *mp = tty->driver_data;
1082 tty_port_close(&mp->port, tty, filp);
1085 static int capinc_tty_write(struct tty_struct *tty,
1086 const unsigned char *buf, int count)
1088 struct capiminor *mp = tty->driver_data;
1089 struct sk_buff *skb;
1090 unsigned long flags;
1092 #ifdef _DEBUG_TTYFUNCS
1093 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1096 spin_lock_irqsave(&workaround_lock, flags);
1100 skb_queue_tail(&mp->outqueue, skb);
1101 mp->outbytes += skb->len;
1104 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1106 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1107 spin_unlock_irqrestore(&workaround_lock, flags);
1111 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1112 memcpy(skb_put(skb, count), buf, count);
1114 skb_queue_tail(&mp->outqueue, skb);
1115 mp->outbytes += skb->len;
1116 (void)handle_minor_send(mp);
1117 spin_unlock_irqrestore(&workaround_lock, flags);
1121 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1123 struct capiminor *mp = tty->driver_data;
1124 struct sk_buff *skb;
1125 unsigned long flags;
1128 #ifdef _DEBUG_TTYFUNCS
1129 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1132 spin_lock_irqsave(&workaround_lock, flags);
1135 if (skb_tailroom(skb) > 0) {
1136 *(skb_put(skb, 1)) = ch;
1137 spin_unlock_irqrestore(&workaround_lock, flags);
1141 skb_queue_tail(&mp->outqueue, skb);
1142 mp->outbytes += skb->len;
1143 (void)handle_minor_send(mp);
1145 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1147 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1148 *(skb_put(skb, 1)) = ch;
1151 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1154 spin_unlock_irqrestore(&workaround_lock, flags);
1158 static void capinc_tty_flush_chars(struct tty_struct *tty)
1160 struct capiminor *mp = tty->driver_data;
1161 struct sk_buff *skb;
1162 unsigned long flags;
1164 #ifdef _DEBUG_TTYFUNCS
1165 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1168 spin_lock_irqsave(&workaround_lock, flags);
1172 skb_queue_tail(&mp->outqueue, skb);
1173 mp->outbytes += skb->len;
1174 (void)handle_minor_send(mp);
1176 (void)handle_minor_recv(mp);
1177 spin_unlock_irqrestore(&workaround_lock, flags);
1180 static int capinc_tty_write_room(struct tty_struct *tty)
1182 struct capiminor *mp = tty->driver_data;
1185 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1186 room *= CAPI_MAX_BLKSIZE;
1187 #ifdef _DEBUG_TTYFUNCS
1188 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1193 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1195 struct capiminor *mp = tty->driver_data;
1197 #ifdef _DEBUG_TTYFUNCS
1198 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1199 mp->outbytes, mp->nack,
1200 skb_queue_len(&mp->outqueue),
1201 skb_queue_len(&mp->inqueue));
1203 return mp->outbytes;
1206 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1207 unsigned int cmd, unsigned long arg)
1212 error = n_tty_ioctl_helper(tty, file, cmd, arg);
1218 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
1220 #ifdef _DEBUG_TTYFUNCS
1221 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1225 static void capinc_tty_throttle(struct tty_struct *tty)
1227 struct capiminor *mp = tty->driver_data;
1228 #ifdef _DEBUG_TTYFUNCS
1229 printk(KERN_DEBUG "capinc_tty_throttle\n");
1234 static void capinc_tty_unthrottle(struct tty_struct *tty)
1236 struct capiminor *mp = tty->driver_data;
1237 unsigned long flags;
1239 #ifdef _DEBUG_TTYFUNCS
1240 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1242 spin_lock_irqsave(&workaround_lock, flags);
1244 handle_minor_recv(mp);
1245 spin_unlock_irqrestore(&workaround_lock, flags);
1248 static void capinc_tty_stop(struct tty_struct *tty)
1250 struct capiminor *mp = tty->driver_data;
1252 #ifdef _DEBUG_TTYFUNCS
1253 printk(KERN_DEBUG "capinc_tty_stop\n");
1258 static void capinc_tty_start(struct tty_struct *tty)
1260 struct capiminor *mp = tty->driver_data;
1261 unsigned long flags;
1263 #ifdef _DEBUG_TTYFUNCS
1264 printk(KERN_DEBUG "capinc_tty_start\n");
1266 spin_lock_irqsave(&workaround_lock, flags);
1268 (void)handle_minor_send(mp);
1269 spin_unlock_irqrestore(&workaround_lock, flags);
1272 static void capinc_tty_hangup(struct tty_struct *tty)
1274 struct capiminor *mp = tty->driver_data;
1276 #ifdef _DEBUG_TTYFUNCS
1277 printk(KERN_DEBUG "capinc_tty_hangup\n");
1279 tty_port_hangup(&mp->port);
1282 static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1284 #ifdef _DEBUG_TTYFUNCS
1285 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1290 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1292 #ifdef _DEBUG_TTYFUNCS
1293 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1297 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1299 #ifdef _DEBUG_TTYFUNCS
1300 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1304 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1306 #ifdef _DEBUG_TTYFUNCS
1307 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1311 static const struct tty_operations capinc_ops = {
1312 .open = capinc_tty_open,
1313 .close = capinc_tty_close,
1314 .write = capinc_tty_write,
1315 .put_char = capinc_tty_put_char,
1316 .flush_chars = capinc_tty_flush_chars,
1317 .write_room = capinc_tty_write_room,
1318 .chars_in_buffer = capinc_tty_chars_in_buffer,
1319 .ioctl = capinc_tty_ioctl,
1320 .set_termios = capinc_tty_set_termios,
1321 .throttle = capinc_tty_throttle,
1322 .unthrottle = capinc_tty_unthrottle,
1323 .stop = capinc_tty_stop,
1324 .start = capinc_tty_start,
1325 .hangup = capinc_tty_hangup,
1326 .break_ctl = capinc_tty_break_ctl,
1327 .flush_buffer = capinc_tty_flush_buffer,
1328 .set_ldisc = capinc_tty_set_ldisc,
1329 .send_xchar = capinc_tty_send_xchar,
1330 .install = capinc_tty_install,
1331 .cleanup = capinc_tty_cleanup,
1334 static int __init capinc_tty_init(void)
1336 struct tty_driver *drv;
1339 if (capi_ttyminors > CAPINC_MAX_PORTS)
1340 capi_ttyminors = CAPINC_MAX_PORTS;
1341 if (capi_ttyminors <= 0)
1342 capi_ttyminors = CAPINC_NR_PORTS;
1344 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1349 drv = alloc_tty_driver(capi_ttyminors);
1354 drv->owner = THIS_MODULE;
1355 drv->driver_name = "capi_nc";
1358 drv->minor_start = 0;
1359 drv->type = TTY_DRIVER_TYPE_SERIAL;
1360 drv->subtype = SERIAL_TYPE_NORMAL;
1361 drv->init_termios = tty_std_termios;
1362 drv->init_termios.c_iflag = ICRNL;
1363 drv->init_termios.c_oflag = OPOST | ONLCR;
1364 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1365 drv->init_termios.c_lflag = 0;
1367 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1368 TTY_DRIVER_DYNAMIC_DEV;
1369 tty_set_operations(drv, &capinc_ops);
1371 err = tty_register_driver(drv);
1373 put_tty_driver(drv);
1375 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1378 capinc_tty_driver = drv;
1382 static void __exit capinc_tty_exit(void)
1384 tty_unregister_driver(capinc_tty_driver);
1385 put_tty_driver(capinc_tty_driver);
1389 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1391 static inline int capinc_tty_init(void)
1396 static inline void capinc_tty_exit(void) { }
1398 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1400 /* -------- /proc functions ----------------------------------------- */
1403 * /proc/capi/capi20:
1404 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1406 static int capi20_proc_show(struct seq_file *m, void *v)
1408 struct capidev *cdev;
1409 struct list_head *l;
1411 mutex_lock(&capidev_list_lock);
1412 list_for_each(l, &capidev_list) {
1413 cdev = list_entry(l, struct capidev, list);
1414 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
1416 cdev->ap.nrecvctlpkt,
1417 cdev->ap.nrecvdatapkt,
1418 cdev->ap.nsentctlpkt,
1419 cdev->ap.nsentdatapkt);
1421 mutex_unlock(&capidev_list_lock);
1425 static int capi20_proc_open(struct inode *inode, struct file *file)
1427 return single_open(file, capi20_proc_show, NULL);
1430 static const struct file_operations capi20_proc_fops = {
1431 .owner = THIS_MODULE,
1432 .open = capi20_proc_open,
1434 .llseek = seq_lseek,
1435 .release = single_release,
1439 * /proc/capi/capi20ncci:
1442 static int capi20ncci_proc_show(struct seq_file *m, void *v)
1444 struct capidev *cdev;
1445 struct capincci *np;
1447 mutex_lock(&capidev_list_lock);
1448 list_for_each_entry(cdev, &capidev_list, list) {
1449 mutex_lock(&cdev->lock);
1450 list_for_each_entry(np, &cdev->nccis, list)
1451 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
1452 mutex_unlock(&cdev->lock);
1454 mutex_unlock(&capidev_list_lock);
1458 static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1460 return single_open(file, capi20ncci_proc_show, NULL);
1463 static const struct file_operations capi20ncci_proc_fops = {
1464 .owner = THIS_MODULE,
1465 .open = capi20ncci_proc_open,
1467 .llseek = seq_lseek,
1468 .release = single_release,
1471 static void __init proc_init(void)
1473 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1474 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
1477 static void __exit proc_exit(void)
1479 remove_proc_entry("capi/capi20", NULL);
1480 remove_proc_entry("capi/capi20ncci", NULL);
1483 /* -------- init function and module interface ---------------------- */
1486 static int __init capi_init(void)
1488 const char *compileinfo;
1491 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1492 if (major_ret < 0) {
1493 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1496 capi_class = class_create(THIS_MODULE, "capi");
1497 if (IS_ERR(capi_class)) {
1498 unregister_chrdev(capi_major, "capi20");
1499 return PTR_ERR(capi_class);
1502 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
1504 if (capinc_tty_init() < 0) {
1505 device_destroy(capi_class, MKDEV(capi_major, 0));
1506 class_destroy(capi_class);
1507 unregister_chrdev(capi_major, "capi20");
1513 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1514 compileinfo = " (middleware+capifs)";
1515 #elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
1516 compileinfo = " (no capifs)";
1518 compileinfo = " (no middleware)";
1520 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1521 capi_major, compileinfo);
1526 static void __exit capi_exit(void)
1530 device_destroy(capi_class, MKDEV(capi_major, 0));
1531 class_destroy(capi_class);
1532 unregister_chrdev(capi_major, "capi20");
1537 module_init(capi_init);
1538 module_exit(capi_exit);