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;
102 struct sk_buff_head outqueue;
106 struct list_head ackqueue;
111 /* FIXME: The following lock is a sledgehammer-workaround to a
112 * locking issue with the capiminor (and maybe other) data structure(s).
113 * Access to this data is done in a racy way and crashes the machine with
114 * a FritzCard DSL driver; sooner or later. This is a workaround
115 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
116 * The correct (and scalable) fix for the issue seems to require
117 * an API change to the drivers... . */
118 static DEFINE_SPINLOCK(workaround_lock);
121 struct list_head list;
123 struct capidev *cdev;
124 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
125 struct capiminor *minorp;
126 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
130 struct list_head list;
131 struct capi20_appl ap;
135 struct sk_buff_head recvqueue;
136 wait_queue_head_t recvwait;
138 struct list_head nccis;
143 /* -------- global variables ---------------------------------------- */
145 static DEFINE_MUTEX(capidev_list_lock);
146 static LIST_HEAD(capidev_list);
148 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
150 static DEFINE_SPINLOCK(capiminors_lock);
151 static struct capiminor **capiminors;
153 static struct tty_driver *capinc_tty_driver;
155 /* -------- datahandles --------------------------------------------- */
157 static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
159 struct ackqueue_entry *n;
162 n = kmalloc(sizeof(*n), GFP_ATOMIC);
164 printk(KERN_ERR "capi: alloc datahandle failed\n");
167 n->datahandle = datahandle;
168 INIT_LIST_HEAD(&n->list);
169 spin_lock_irqsave(&mp->ackqlock, flags);
170 list_add_tail(&n->list, &mp->ackqueue);
172 spin_unlock_irqrestore(&mp->ackqlock, flags);
176 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
178 struct ackqueue_entry *p, *tmp;
181 spin_lock_irqsave(&mp->ackqlock, flags);
182 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
183 if (p->datahandle == datahandle) {
187 spin_unlock_irqrestore(&mp->ackqlock, flags);
191 spin_unlock_irqrestore(&mp->ackqlock, flags);
195 static void capiminor_del_all_ack(struct capiminor *mp)
197 struct ackqueue_entry *p, *tmp;
200 spin_lock_irqsave(&mp->ackqlock, flags);
201 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
206 spin_unlock_irqrestore(&mp->ackqlock, flags);
210 /* -------- struct capiminor ---------------------------------------- */
212 static const struct tty_port_operations capiminor_port_ops; /* we have none */
214 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
216 struct capiminor *mp;
220 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
222 printk(KERN_ERR "capi: can't alloc capiminor\n");
226 kref_init(&mp->kref);
230 INIT_LIST_HEAD(&mp->ackqueue);
231 spin_lock_init(&mp->ackqlock);
233 skb_queue_head_init(&mp->inqueue);
234 skb_queue_head_init(&mp->outqueue);
236 tty_port_init(&mp->port);
237 mp->port.ops = &capiminor_port_ops;
239 /* Allocate the least unused minor number. */
240 spin_lock(&capiminors_lock);
241 for (minor = 0; minor < capi_ttyminors; minor++)
242 if (!capiminors[minor]) {
243 capiminors[minor] = mp;
246 spin_unlock(&capiminors_lock);
248 if (minor == capi_ttyminors) {
249 printk(KERN_NOTICE "capi: out of minors\n");
255 dev = tty_register_device(capinc_tty_driver, minor, NULL);
262 spin_lock(&capiminors_lock);
263 capiminors[minor] = NULL;
264 spin_unlock(&capiminors_lock);
271 static void capiminor_destroy(struct kref *kref)
273 struct capiminor *mp = container_of(kref, struct capiminor, kref);
275 kfree_skb(mp->ttyskb);
276 skb_queue_purge(&mp->inqueue);
277 skb_queue_purge(&mp->outqueue);
278 capiminor_del_all_ack(mp);
282 static struct capiminor *capiminor_get(unsigned int minor)
284 struct capiminor *mp;
286 spin_lock(&capiminors_lock);
287 mp = capiminors[minor];
290 spin_unlock(&capiminors_lock);
295 static inline void capiminor_put(struct capiminor *mp)
297 kref_put(&mp->kref, capiminor_destroy);
300 static void capiminor_free(struct capiminor *mp)
302 tty_unregister_device(capinc_tty_driver, mp->minor);
304 spin_lock(&capiminors_lock);
305 capiminors[mp->minor] = NULL;
306 spin_unlock(&capiminors_lock);
311 /* -------- struct capincci ----------------------------------------- */
313 static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
315 struct capiminor *mp;
318 if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
321 mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
323 device = MKDEV(capinc_tty_driver->major, mp->minor);
324 mp->capifs_dentry = capifs_new_ncci(mp->minor, device);
328 static void capincci_free_minor(struct capincci *np)
330 struct capiminor *mp = np->minorp;
331 struct tty_struct *tty;
334 capifs_free_ncci(mp->capifs_dentry);
336 tty = tty_port_tty_get(&mp->port);
346 static inline unsigned int capincci_minor_opencount(struct capincci *np)
348 struct capiminor *mp = np->minorp;
349 unsigned int count = 0;
350 struct tty_struct *tty;
353 tty = tty_port_tty_get(&mp->port);
362 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
365 capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
366 static inline void capincci_free_minor(struct capincci *np) { }
368 static inline unsigned int capincci_minor_opencount(struct capincci *np)
373 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
375 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
379 np = kzalloc(sizeof(*np), GFP_KERNEL);
385 capincci_alloc_minor(cdev, np);
387 list_add_tail(&np->list, &cdev->nccis);
392 static void capincci_free(struct capidev *cdev, u32 ncci)
394 struct capincci *np, *tmp;
396 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
397 if (ncci == 0xffffffff || np->ncci == ncci) {
398 capincci_free_minor(np);
404 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
408 list_for_each_entry(np, &cdev->nccis, list)
409 if (np->ncci == ncci)
414 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
415 /* -------- handle data queue --------------------------------------- */
417 static struct sk_buff *
418 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
420 struct sk_buff *nskb;
421 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
423 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
424 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
425 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
426 capimsg_setu16(s, 2, mp->ap->applid);
427 capimsg_setu8 (s, 4, CAPI_DATA_B3);
428 capimsg_setu8 (s, 5, CAPI_RESP);
429 capimsg_setu16(s, 6, atomic_inc_return(&mp->msgid));
430 capimsg_setu32(s, 8, mp->ncci);
431 capimsg_setu16(s, 12, datahandle);
436 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
438 unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
439 struct tty_struct *tty;
440 struct sk_buff *nskb;
441 u16 errcode, datahandle;
442 struct tty_ldisc *ld;
445 tty = tty_port_tty_get(&mp->port);
447 #ifdef _DEBUG_DATAFLOW
448 printk(KERN_DEBUG "capi: currently no receiver\n");
453 ld = tty_ldisc_ref(tty);
455 /* fatal error, do not requeue */
461 if (ld->ops->receive_buf == NULL) {
462 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
463 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
465 /* fatal error, do not requeue */
469 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
470 printk(KERN_DEBUG "capi: recv tty throttled\n");
475 if (tty->receive_room < datalen) {
476 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
477 printk(KERN_DEBUG "capi: no room in tty\n");
482 nskb = gen_data_b3_resp_for(mp, skb);
484 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
488 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
490 errcode = capi20_put_message(mp->ap, nskb);
492 if (errcode == CAPI_NOERROR) {
493 skb_pull(skb, CAPIMSG_LEN(skb->data));
494 #ifdef _DEBUG_DATAFLOW
495 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
496 datahandle, skb->len);
498 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
500 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
504 if (errcode == CAPI_SENDQUEUEFULL)
520 static void handle_minor_recv(struct capiminor *mp)
523 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
524 unsigned int len = skb->len;
526 if (handle_recv_skb(mp, skb) < 0) {
527 skb_queue_head(&mp->inqueue, skb);
534 static int handle_minor_send(struct capiminor *mp)
536 struct tty_struct *tty;
543 tty = tty_port_tty_get(&mp->port);
547 if (mp->ttyoutstop) {
548 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
549 printk(KERN_DEBUG "capi: send: tty stopped\n");
555 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
556 datahandle = atomic_inc_return(&mp->datahandle);
558 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
559 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
560 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
561 capimsg_setu16(skb->data, 2, mp->ap->applid);
562 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
563 capimsg_setu8 (skb->data, 5, CAPI_REQ);
564 capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid));
565 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
566 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
567 capimsg_setu16(skb->data, 16, len); /* Data length */
568 capimsg_setu16(skb->data, 18, datahandle);
569 capimsg_setu16(skb->data, 20, 0); /* Flags */
571 if (capiminor_add_ack(mp, datahandle) < 0) {
572 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
573 skb_queue_head(&mp->outqueue, skb);
577 errcode = capi20_put_message(mp->ap, skb);
578 if (errcode == CAPI_NOERROR) {
581 #ifdef _DEBUG_DATAFLOW
582 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
587 capiminor_del_ack(mp, datahandle);
589 if (errcode == CAPI_SENDQUEUEFULL) {
590 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
591 skb_queue_head(&mp->outqueue, skb);
595 /* ups, drop packet */
596 printk(KERN_ERR "capi: put_message = %x\n", errcode);
604 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
605 /* -------- function called by lower level -------------------------- */
607 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
609 struct capidev *cdev = ap->private;
610 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
611 struct tty_struct *tty;
612 struct capiminor *mp;
614 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
618 mutex_lock(&cdev->lock);
620 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
621 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
622 if ((info & 0xff00) == 0)
623 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
625 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
626 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
628 spin_lock_irqsave(&workaround_lock, flags);
629 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
630 skb_queue_tail(&cdev->recvqueue, skb);
631 wake_up_interruptible(&cdev->recvwait);
635 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
637 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
638 skb_queue_tail(&cdev->recvqueue, skb);
639 wake_up_interruptible(&cdev->recvwait);
643 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
644 skb_queue_tail(&cdev->recvqueue, skb);
645 wake_up_interruptible(&cdev->recvwait);
647 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
651 skb_queue_tail(&cdev->recvqueue, skb);
652 wake_up_interruptible(&cdev->recvwait);
655 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
656 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
657 #ifdef _DEBUG_DATAFLOW
658 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
659 datahandle, skb->len-CAPIMSG_LEN(skb->data));
661 skb_queue_tail(&mp->inqueue, skb);
662 mp->inbytes += skb->len;
663 handle_minor_recv(mp);
665 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
667 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
668 #ifdef _DEBUG_DATAFLOW
669 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
671 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
674 (void)capiminor_del_ack(mp, datahandle);
675 tty = tty_port_tty_get(&mp->port);
680 (void)handle_minor_send(mp);
683 /* ups, let capi application handle it :-) */
684 skb_queue_tail(&cdev->recvqueue, skb);
685 wake_up_interruptible(&cdev->recvwait);
687 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
690 spin_unlock_irqrestore(&workaround_lock, flags);
691 mutex_unlock(&cdev->lock);
694 /* -------- file_operations for capidev ----------------------------- */
697 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
699 struct capidev *cdev = (struct capidev *)file->private_data;
704 if (!cdev->ap.applid)
707 skb = skb_dequeue(&cdev->recvqueue);
709 if (file->f_flags & O_NONBLOCK)
711 err = wait_event_interruptible(cdev->recvwait,
712 (skb = skb_dequeue(&cdev->recvqueue)));
716 if (skb->len > count) {
717 skb_queue_head(&cdev->recvqueue, skb);
720 if (copy_to_user(buf, skb->data, skb->len)) {
721 skb_queue_head(&cdev->recvqueue, skb);
732 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
734 struct capidev *cdev = (struct capidev *)file->private_data;
738 if (!cdev->ap.applid)
741 skb = alloc_skb(count, GFP_USER);
745 if (copy_from_user(skb_put(skb, count), buf, count)) {
749 mlen = CAPIMSG_LEN(skb->data);
750 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
751 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
761 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
763 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
764 mutex_lock(&cdev->lock);
765 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
766 mutex_unlock(&cdev->lock);
769 cdev->errcode = capi20_put_message(&cdev->ap, skb);
779 capi_poll(struct file *file, poll_table * wait)
781 struct capidev *cdev = (struct capidev *)file->private_data;
782 unsigned int mask = 0;
784 if (!cdev->ap.applid)
787 poll_wait(file, &(cdev->recvwait), wait);
788 mask = POLLOUT | POLLWRNORM;
789 if (!skb_queue_empty(&cdev->recvqueue))
790 mask |= POLLIN | POLLRDNORM;
795 capi_ioctl(struct inode *inode, struct file *file,
796 unsigned int cmd, unsigned long arg)
798 struct capidev *cdev = file->private_data;
799 capi_ioctl_struct data;
800 int retval = -EINVAL;
801 void __user *argp = (void __user *)arg;
805 mutex_lock(&cdev->lock);
807 if (cdev->ap.applid) {
811 if (copy_from_user(&cdev->ap.rparam, argp,
812 sizeof(struct capi_register_params))) {
816 cdev->ap.private = cdev;
817 cdev->ap.recv_message = capi_recv_message;
818 cdev->errcode = capi20_register(&cdev->ap);
819 retval = (int)cdev->ap.applid;
826 mutex_unlock(&cdev->lock);
829 case CAPI_GET_VERSION:
831 if (copy_from_user(&data.contr, argp,
834 cdev->errcode = capi20_get_version(data.contr, &data.version);
837 if (copy_to_user(argp, &data.version,
838 sizeof(data.version)))
843 case CAPI_GET_SERIAL:
845 if (copy_from_user(&data.contr, argp,
848 cdev->errcode = capi20_get_serial (data.contr, data.serial);
851 if (copy_to_user(argp, data.serial,
852 sizeof(data.serial)))
856 case CAPI_GET_PROFILE:
858 if (copy_from_user(&data.contr, argp,
862 if (data.contr == 0) {
863 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
867 retval = copy_to_user(argp,
868 &data.profile.ncontroller,
869 sizeof(data.profile.ncontroller));
872 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
876 retval = copy_to_user(argp, &data.profile,
877 sizeof(data.profile));
884 case CAPI_GET_MANUFACTURER:
886 if (copy_from_user(&data.contr, argp,
889 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
893 if (copy_to_user(argp, data.manufacturer,
894 sizeof(data.manufacturer)))
899 case CAPI_GET_ERRCODE:
900 data.errcode = cdev->errcode;
901 cdev->errcode = CAPI_NOERROR;
903 if (copy_to_user(argp, &data.errcode,
904 sizeof(data.errcode)))
910 if (capi20_isinstalled() == CAPI_NOERROR)
914 case CAPI_MANUFACTURER_CMD:
916 struct capi_manufacturer_cmd mcmd;
917 if (!capable(CAP_SYS_ADMIN))
919 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
921 return capi20_manufacturer(mcmd.cmd, mcmd.data);
926 case CAPI_CLR_FLAGS: {
929 if (copy_from_user(&userflags, argp, sizeof(userflags)))
932 mutex_lock(&cdev->lock);
933 if (cmd == CAPI_SET_FLAGS)
934 cdev->userflags |= userflags;
936 cdev->userflags &= ~userflags;
937 mutex_unlock(&cdev->lock);
941 if (copy_to_user(argp, &cdev->userflags,
942 sizeof(cdev->userflags)))
946 case CAPI_NCCI_OPENCOUNT: {
947 struct capincci *nccip;
951 if (copy_from_user(&ncci, argp, sizeof(ncci)))
954 mutex_lock(&cdev->lock);
955 nccip = capincci_find(cdev, (u32)ncci);
957 count = capincci_minor_opencount(nccip);
958 mutex_unlock(&cdev->lock);
962 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
963 case CAPI_NCCI_GETUNIT: {
964 struct capincci *nccip;
965 struct capiminor *mp;
969 if (copy_from_user(&ncci, argp, sizeof(ncci)))
972 mutex_lock(&cdev->lock);
973 nccip = capincci_find(cdev, (u32)ncci);
979 mutex_unlock(&cdev->lock);
982 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
989 static int capi_open(struct inode *inode, struct file *file)
991 struct capidev *cdev;
993 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
997 mutex_init(&cdev->lock);
998 skb_queue_head_init(&cdev->recvqueue);
999 init_waitqueue_head(&cdev->recvwait);
1000 INIT_LIST_HEAD(&cdev->nccis);
1001 file->private_data = cdev;
1003 mutex_lock(&capidev_list_lock);
1004 list_add_tail(&cdev->list, &capidev_list);
1005 mutex_unlock(&capidev_list_lock);
1007 return nonseekable_open(inode, file);
1010 static int capi_release(struct inode *inode, struct file *file)
1012 struct capidev *cdev = file->private_data;
1014 mutex_lock(&capidev_list_lock);
1015 list_del(&cdev->list);
1016 mutex_unlock(&capidev_list_lock);
1018 if (cdev->ap.applid)
1019 capi20_release(&cdev->ap);
1020 skb_queue_purge(&cdev->recvqueue);
1021 capincci_free(cdev, 0xffffffff);
1027 static const struct file_operations capi_fops =
1029 .owner = THIS_MODULE,
1030 .llseek = no_llseek,
1032 .write = capi_write,
1034 .ioctl = capi_ioctl,
1036 .release = capi_release,
1039 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1040 /* -------- tty_operations for capincci ----------------------------- */
1043 capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1045 int idx = tty->index;
1046 struct capiminor *mp = capiminor_get(idx);
1047 int ret = tty_init_termios(tty);
1050 tty_driver_kref_get(driver);
1052 tty->driver_data = mp;
1053 driver->ttys[idx] = tty;
1059 static void capinc_tty_cleanup(struct tty_struct *tty)
1061 struct capiminor *mp = tty->driver_data;
1062 tty->driver_data = NULL;
1066 static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
1068 struct capiminor *mp = tty->driver_data;
1069 unsigned long flags;
1072 err = tty_port_open(&mp->port, tty, filp);
1076 spin_lock_irqsave(&workaround_lock, flags);
1077 handle_minor_recv(mp);
1078 spin_unlock_irqrestore(&workaround_lock, flags);
1082 static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
1084 struct capiminor *mp = tty->driver_data;
1086 tty_port_close(&mp->port, tty, filp);
1089 static int capinc_tty_write(struct tty_struct *tty,
1090 const unsigned char *buf, int count)
1092 struct capiminor *mp = tty->driver_data;
1093 struct sk_buff *skb;
1094 unsigned long flags;
1096 #ifdef _DEBUG_TTYFUNCS
1097 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1100 spin_lock_irqsave(&workaround_lock, flags);
1104 skb_queue_tail(&mp->outqueue, skb);
1105 mp->outbytes += skb->len;
1108 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1110 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1111 spin_unlock_irqrestore(&workaround_lock, flags);
1115 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1116 memcpy(skb_put(skb, count), buf, count);
1118 skb_queue_tail(&mp->outqueue, skb);
1119 mp->outbytes += skb->len;
1120 (void)handle_minor_send(mp);
1121 spin_unlock_irqrestore(&workaround_lock, flags);
1125 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1127 struct capiminor *mp = tty->driver_data;
1128 struct sk_buff *skb;
1129 unsigned long flags;
1132 #ifdef _DEBUG_TTYFUNCS
1133 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1136 spin_lock_irqsave(&workaround_lock, flags);
1139 if (skb_tailroom(skb) > 0) {
1140 *(skb_put(skb, 1)) = ch;
1141 spin_unlock_irqrestore(&workaround_lock, flags);
1145 skb_queue_tail(&mp->outqueue, skb);
1146 mp->outbytes += skb->len;
1147 (void)handle_minor_send(mp);
1149 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1151 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1152 *(skb_put(skb, 1)) = ch;
1155 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1158 spin_unlock_irqrestore(&workaround_lock, flags);
1162 static void capinc_tty_flush_chars(struct tty_struct *tty)
1164 struct capiminor *mp = tty->driver_data;
1165 struct sk_buff *skb;
1166 unsigned long flags;
1168 #ifdef _DEBUG_TTYFUNCS
1169 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1172 spin_lock_irqsave(&workaround_lock, flags);
1176 skb_queue_tail(&mp->outqueue, skb);
1177 mp->outbytes += skb->len;
1178 (void)handle_minor_send(mp);
1180 (void)handle_minor_recv(mp);
1181 spin_unlock_irqrestore(&workaround_lock, flags);
1184 static int capinc_tty_write_room(struct tty_struct *tty)
1186 struct capiminor *mp = tty->driver_data;
1189 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1190 room *= CAPI_MAX_BLKSIZE;
1191 #ifdef _DEBUG_TTYFUNCS
1192 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1197 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1199 struct capiminor *mp = tty->driver_data;
1201 #ifdef _DEBUG_TTYFUNCS
1202 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1203 mp->outbytes, mp->nack,
1204 skb_queue_len(&mp->outqueue),
1205 skb_queue_len(&mp->inqueue));
1207 return mp->outbytes;
1210 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1211 unsigned int cmd, unsigned long arg)
1216 error = n_tty_ioctl_helper(tty, file, cmd, arg);
1222 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
1224 #ifdef _DEBUG_TTYFUNCS
1225 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1229 static void capinc_tty_throttle(struct tty_struct *tty)
1231 struct capiminor *mp = tty->driver_data;
1232 #ifdef _DEBUG_TTYFUNCS
1233 printk(KERN_DEBUG "capinc_tty_throttle\n");
1238 static void capinc_tty_unthrottle(struct tty_struct *tty)
1240 struct capiminor *mp = tty->driver_data;
1241 unsigned long flags;
1243 #ifdef _DEBUG_TTYFUNCS
1244 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1246 spin_lock_irqsave(&workaround_lock, flags);
1248 handle_minor_recv(mp);
1249 spin_unlock_irqrestore(&workaround_lock, flags);
1252 static void capinc_tty_stop(struct tty_struct *tty)
1254 struct capiminor *mp = tty->driver_data;
1256 #ifdef _DEBUG_TTYFUNCS
1257 printk(KERN_DEBUG "capinc_tty_stop\n");
1262 static void capinc_tty_start(struct tty_struct *tty)
1264 struct capiminor *mp = tty->driver_data;
1265 unsigned long flags;
1267 #ifdef _DEBUG_TTYFUNCS
1268 printk(KERN_DEBUG "capinc_tty_start\n");
1270 spin_lock_irqsave(&workaround_lock, flags);
1272 (void)handle_minor_send(mp);
1273 spin_unlock_irqrestore(&workaround_lock, flags);
1276 static void capinc_tty_hangup(struct tty_struct *tty)
1278 struct capiminor *mp = tty->driver_data;
1280 #ifdef _DEBUG_TTYFUNCS
1281 printk(KERN_DEBUG "capinc_tty_hangup\n");
1283 tty_port_hangup(&mp->port);
1286 static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1288 #ifdef _DEBUG_TTYFUNCS
1289 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1294 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1296 #ifdef _DEBUG_TTYFUNCS
1297 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1301 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1303 #ifdef _DEBUG_TTYFUNCS
1304 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1308 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1310 #ifdef _DEBUG_TTYFUNCS
1311 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1315 static const struct tty_operations capinc_ops = {
1316 .open = capinc_tty_open,
1317 .close = capinc_tty_close,
1318 .write = capinc_tty_write,
1319 .put_char = capinc_tty_put_char,
1320 .flush_chars = capinc_tty_flush_chars,
1321 .write_room = capinc_tty_write_room,
1322 .chars_in_buffer = capinc_tty_chars_in_buffer,
1323 .ioctl = capinc_tty_ioctl,
1324 .set_termios = capinc_tty_set_termios,
1325 .throttle = capinc_tty_throttle,
1326 .unthrottle = capinc_tty_unthrottle,
1327 .stop = capinc_tty_stop,
1328 .start = capinc_tty_start,
1329 .hangup = capinc_tty_hangup,
1330 .break_ctl = capinc_tty_break_ctl,
1331 .flush_buffer = capinc_tty_flush_buffer,
1332 .set_ldisc = capinc_tty_set_ldisc,
1333 .send_xchar = capinc_tty_send_xchar,
1334 .install = capinc_tty_install,
1335 .cleanup = capinc_tty_cleanup,
1338 static int __init capinc_tty_init(void)
1340 struct tty_driver *drv;
1343 if (capi_ttyminors > CAPINC_MAX_PORTS)
1344 capi_ttyminors = CAPINC_MAX_PORTS;
1345 if (capi_ttyminors <= 0)
1346 capi_ttyminors = CAPINC_NR_PORTS;
1348 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1353 drv = alloc_tty_driver(capi_ttyminors);
1358 drv->owner = THIS_MODULE;
1359 drv->driver_name = "capi_nc";
1362 drv->minor_start = 0;
1363 drv->type = TTY_DRIVER_TYPE_SERIAL;
1364 drv->subtype = SERIAL_TYPE_NORMAL;
1365 drv->init_termios = tty_std_termios;
1366 drv->init_termios.c_iflag = ICRNL;
1367 drv->init_termios.c_oflag = OPOST | ONLCR;
1368 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1369 drv->init_termios.c_lflag = 0;
1371 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1372 TTY_DRIVER_DYNAMIC_DEV;
1373 tty_set_operations(drv, &capinc_ops);
1375 err = tty_register_driver(drv);
1377 put_tty_driver(drv);
1379 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1382 capinc_tty_driver = drv;
1386 static void __exit capinc_tty_exit(void)
1388 tty_unregister_driver(capinc_tty_driver);
1389 put_tty_driver(capinc_tty_driver);
1393 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1395 static inline int capinc_tty_init(void)
1400 static inline void capinc_tty_exit(void) { }
1402 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1404 /* -------- /proc functions ----------------------------------------- */
1407 * /proc/capi/capi20:
1408 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1410 static int capi20_proc_show(struct seq_file *m, void *v)
1412 struct capidev *cdev;
1413 struct list_head *l;
1415 mutex_lock(&capidev_list_lock);
1416 list_for_each(l, &capidev_list) {
1417 cdev = list_entry(l, struct capidev, list);
1418 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
1420 cdev->ap.nrecvctlpkt,
1421 cdev->ap.nrecvdatapkt,
1422 cdev->ap.nsentctlpkt,
1423 cdev->ap.nsentdatapkt);
1425 mutex_unlock(&capidev_list_lock);
1429 static int capi20_proc_open(struct inode *inode, struct file *file)
1431 return single_open(file, capi20_proc_show, NULL);
1434 static const struct file_operations capi20_proc_fops = {
1435 .owner = THIS_MODULE,
1436 .open = capi20_proc_open,
1438 .llseek = seq_lseek,
1439 .release = single_release,
1443 * /proc/capi/capi20ncci:
1446 static int capi20ncci_proc_show(struct seq_file *m, void *v)
1448 struct capidev *cdev;
1449 struct capincci *np;
1451 mutex_lock(&capidev_list_lock);
1452 list_for_each_entry(cdev, &capidev_list, list) {
1453 mutex_lock(&cdev->lock);
1454 list_for_each_entry(np, &cdev->nccis, list)
1455 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
1456 mutex_unlock(&cdev->lock);
1458 mutex_unlock(&capidev_list_lock);
1462 static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1464 return single_open(file, capi20ncci_proc_show, NULL);
1467 static const struct file_operations capi20ncci_proc_fops = {
1468 .owner = THIS_MODULE,
1469 .open = capi20ncci_proc_open,
1471 .llseek = seq_lseek,
1472 .release = single_release,
1475 static void __init proc_init(void)
1477 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1478 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
1481 static void __exit proc_exit(void)
1483 remove_proc_entry("capi/capi20", NULL);
1484 remove_proc_entry("capi/capi20ncci", NULL);
1487 /* -------- init function and module interface ---------------------- */
1490 static int __init capi_init(void)
1492 const char *compileinfo;
1495 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1496 if (major_ret < 0) {
1497 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1500 capi_class = class_create(THIS_MODULE, "capi");
1501 if (IS_ERR(capi_class)) {
1502 unregister_chrdev(capi_major, "capi20");
1503 return PTR_ERR(capi_class);
1506 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
1508 if (capinc_tty_init() < 0) {
1509 device_destroy(capi_class, MKDEV(capi_major, 0));
1510 class_destroy(capi_class);
1511 unregister_chrdev(capi_major, "capi20");
1517 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1518 compileinfo = " (middleware+capifs)";
1519 #elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
1520 compileinfo = " (no capifs)";
1522 compileinfo = " (no middleware)";
1524 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1525 capi_major, compileinfo);
1530 static void __exit capi_exit(void)
1534 device_destroy(capi_class, MKDEV(capi_major, 0));
1535 class_destroy(capi_class);
1536 unregister_chrdev(capi_major, "capi20");
1541 module_init(capi_init);
1542 module_exit(capi_exit);