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_REFCOUNT /* alloc/free and open/close debug */
49 #undef _DEBUG_TTYFUNCS /* call to tty_driver */
50 #undef _DEBUG_DATAFLOW /* data flow */
52 /* -------- driver information -------------------------------------- */
54 static struct class *capi_class;
55 static int capi_major = 68; /* allocated */
57 module_param_named(major, capi_major, uint, 0);
59 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
60 #define CAPINC_NR_PORTS 32
61 #define CAPINC_MAX_PORTS 256
63 static int capi_ttyminors = CAPINC_NR_PORTS;
65 module_param_named(ttyminors, capi_ttyminors, uint, 0);
66 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
68 /* -------- defines ------------------------------------------------- */
70 #define CAPINC_MAX_RECVQUEUE 10
71 #define CAPINC_MAX_SENDQUEUE 10
72 #define CAPI_MAX_BLKSIZE 2048
74 /* -------- data structures ----------------------------------------- */
80 struct datahandle_queue {
81 struct list_head list;
88 struct capincci *nccip;
90 struct dentry *capifs_dentry;
92 struct capi20_appl *ap;
97 struct tty_struct *tty;
100 struct sk_buff *ttyskb;
101 atomic_t ttyopencount;
103 struct sk_buff_head inqueue;
105 struct sk_buff_head outqueue;
109 struct list_head ackqueue;
114 /* FIXME: The following lock is a sledgehammer-workaround to a
115 * locking issue with the capiminor (and maybe other) data structure(s).
116 * Access to this data is done in a racy way and crashes the machine with
117 * a FritzCard DSL driver; sooner or later. This is a workaround
118 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
119 * The correct (and scalable) fix for the issue seems to require
120 * an API change to the drivers... . */
121 static DEFINE_SPINLOCK(workaround_lock);
124 struct list_head list;
126 struct capidev *cdev;
127 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
128 struct capiminor *minorp;
129 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
133 struct list_head list;
134 struct capi20_appl ap;
138 struct sk_buff_head recvqueue;
139 wait_queue_head_t recvwait;
141 struct list_head nccis;
146 /* -------- global variables ---------------------------------------- */
148 static DEFINE_MUTEX(capidev_list_lock);
149 static LIST_HEAD(capidev_list);
151 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
153 static DEFINE_RWLOCK(capiminors_lock);
154 static struct capiminor **capiminors;
156 static struct tty_driver *capinc_tty_driver;
158 /* -------- datahandles --------------------------------------------- */
160 static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
162 struct datahandle_queue *n;
165 n = kmalloc(sizeof(*n), GFP_ATOMIC);
167 printk(KERN_ERR "capi: alloc datahandle failed\n");
170 n->datahandle = datahandle;
171 INIT_LIST_HEAD(&n->list);
172 spin_lock_irqsave(&mp->ackqlock, flags);
173 list_add_tail(&n->list, &mp->ackqueue);
175 spin_unlock_irqrestore(&mp->ackqlock, flags);
179 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
181 struct datahandle_queue *p, *tmp;
184 spin_lock_irqsave(&mp->ackqlock, flags);
185 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
186 if (p->datahandle == datahandle) {
190 spin_unlock_irqrestore(&mp->ackqlock, flags);
194 spin_unlock_irqrestore(&mp->ackqlock, flags);
198 static void capiminor_del_all_ack(struct capiminor *mp)
200 struct datahandle_queue *p, *tmp;
203 spin_lock_irqsave(&mp->ackqlock, flags);
204 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
209 spin_unlock_irqrestore(&mp->ackqlock, flags);
213 /* -------- struct capiminor ---------------------------------------- */
215 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
217 struct capiminor *mp;
222 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
224 printk(KERN_ERR "capi: can't alloc capiminor\n");
228 kref_init(&mp->kref);
233 atomic_set(&mp->ttyopencount,0);
234 INIT_LIST_HEAD(&mp->ackqueue);
235 spin_lock_init(&mp->ackqlock);
237 skb_queue_head_init(&mp->inqueue);
238 skb_queue_head_init(&mp->outqueue);
240 /* Allocate the least unused minor number. */
241 write_lock_irqsave(&capiminors_lock, flags);
242 for (minor = 0; minor < capi_ttyminors; minor++)
243 if (!capiminors[minor]) {
244 capiminors[minor] = mp;
247 write_unlock_irqrestore(&capiminors_lock, flags);
249 if (minor == capi_ttyminors) {
250 printk(KERN_NOTICE "capi: out of minors\n");
256 dev = tty_register_device(capinc_tty_driver, minor, NULL);
263 write_lock_irqsave(&capiminors_lock, flags);
264 capiminors[minor] = NULL;
265 write_unlock_irqrestore(&capiminors_lock, flags);
272 static void capiminor_destroy(struct kref *kref)
274 struct capiminor *mp = container_of(kref, struct capiminor, kref);
276 kfree_skb(mp->ttyskb);
277 skb_queue_purge(&mp->inqueue);
278 skb_queue_purge(&mp->outqueue);
279 capiminor_del_all_ack(mp);
283 static struct capiminor *capiminor_get(unsigned int minor)
285 struct capiminor *mp;
287 read_lock(&capiminors_lock);
288 mp = capiminors[minor];
291 read_unlock(&capiminors_lock);
296 static inline void capiminor_put(struct capiminor *mp)
298 kref_put(&mp->kref, capiminor_destroy);
301 static void capiminor_free(struct capiminor *mp)
305 tty_unregister_device(capinc_tty_driver, mp->minor);
307 write_lock_irqsave(&capiminors_lock, flags);
308 capiminors[mp->minor] = NULL;
309 write_unlock_irqrestore(&capiminors_lock, flags);
314 /* -------- struct capincci ----------------------------------------- */
316 static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
318 struct capiminor *mp;
321 if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
324 mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
327 #ifdef _DEBUG_REFCOUNT
328 printk(KERN_DEBUG "set mp->nccip\n");
330 device = MKDEV(capinc_tty_driver->major, mp->minor);
331 mp->capifs_dentry = capifs_new_ncci(mp->minor, device);
335 static void capincci_free_minor(struct capincci *np)
337 struct capiminor *mp = np->minorp;
340 capifs_free_ncci(mp->capifs_dentry);
343 #ifdef _DEBUG_REFCOUNT
344 printk(KERN_DEBUG "reset mp->nccip\n");
353 static inline unsigned int capincci_minor_opencount(struct capincci *np)
355 struct capiminor *mp = np->minorp;
357 return mp ? atomic_read(&mp->ttyopencount) : 0;
360 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
363 capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
364 static inline void capincci_free_minor(struct capincci *np) { }
366 static inline unsigned int capincci_minor_opencount(struct capincci *np)
371 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
373 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
377 np = kzalloc(sizeof(*np), GFP_KERNEL);
383 capincci_alloc_minor(cdev, np);
385 list_add_tail(&np->list, &cdev->nccis);
390 static void capincci_free(struct capidev *cdev, u32 ncci)
392 struct capincci *np, *tmp;
394 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
395 if (ncci == 0xffffffff || np->ncci == ncci) {
396 capincci_free_minor(np);
402 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
406 list_for_each_entry(np, &cdev->nccis, list)
407 if (np->ncci == ncci)
412 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
413 /* -------- handle data queue --------------------------------------- */
415 static struct sk_buff *
416 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
418 struct sk_buff *nskb;
419 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
421 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
422 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
423 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
424 capimsg_setu16(s, 2, mp->ap->applid);
425 capimsg_setu8 (s, 4, CAPI_DATA_B3);
426 capimsg_setu8 (s, 5, CAPI_RESP);
427 capimsg_setu16(s, 6, mp->msgid++);
428 capimsg_setu32(s, 8, mp->ncci);
429 capimsg_setu16(s, 12, datahandle);
434 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
436 struct sk_buff *nskb;
438 u16 errcode, datahandle;
439 struct tty_ldisc *ld;
441 datalen = skb->len - CAPIMSG_LEN(skb->data);
444 #ifdef _DEBUG_DATAFLOW
445 printk(KERN_DEBUG "capi: currently no receiver\n");
450 ld = tty_ldisc_ref(mp->tty);
453 if (ld->ops->receive_buf == NULL) {
454 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
455 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
460 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
461 printk(KERN_DEBUG "capi: recv tty throttled\n");
465 if (mp->tty->receive_room < datalen) {
466 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
467 printk(KERN_DEBUG "capi: no room in tty\n");
471 if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
472 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
475 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
476 errcode = capi20_put_message(mp->ap, nskb);
477 if (errcode != CAPI_NOERROR) {
478 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
483 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
484 #ifdef _DEBUG_DATAFLOW
485 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
486 datahandle, skb->len);
488 ld->ops->receive_buf(mp->tty, skb->data, NULL, skb->len);
497 static void handle_minor_recv(struct capiminor *mp)
500 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
501 unsigned int len = skb->len;
503 if (handle_recv_skb(mp, skb) < 0) {
504 skb_queue_head(&mp->inqueue, skb);
511 static int handle_minor_send(struct capiminor *mp)
519 if (mp->tty && mp->ttyoutstop) {
520 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
521 printk(KERN_DEBUG "capi: send: tty stopped\n");
526 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
527 datahandle = mp->datahandle;
529 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
530 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
531 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
532 capimsg_setu16(skb->data, 2, mp->ap->applid);
533 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
534 capimsg_setu8 (skb->data, 5, CAPI_REQ);
535 capimsg_setu16(skb->data, 6, mp->msgid++);
536 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
537 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
538 capimsg_setu16(skb->data, 16, len); /* Data length */
539 capimsg_setu16(skb->data, 18, datahandle);
540 capimsg_setu16(skb->data, 20, 0); /* Flags */
542 if (capiminor_add_ack(mp, datahandle) < 0) {
543 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
544 skb_queue_head(&mp->outqueue, skb);
547 errcode = capi20_put_message(mp->ap, skb);
548 if (errcode == CAPI_NOERROR) {
552 #ifdef _DEBUG_DATAFLOW
553 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
558 capiminor_del_ack(mp, datahandle);
560 if (errcode == CAPI_SENDQUEUEFULL) {
561 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
562 skb_queue_head(&mp->outqueue, skb);
566 /* ups, drop packet */
567 printk(KERN_ERR "capi: put_message = %x\n", errcode);
574 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
575 /* -------- function called by lower level -------------------------- */
577 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
579 struct capidev *cdev = ap->private;
580 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
581 struct capiminor *mp;
583 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
587 mutex_lock(&cdev->lock);
589 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
590 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
591 if ((info & 0xff00) == 0)
592 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
594 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
595 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
597 spin_lock_irqsave(&workaround_lock, flags);
598 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
599 skb_queue_tail(&cdev->recvqueue, skb);
600 wake_up_interruptible(&cdev->recvwait);
604 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
606 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
607 skb_queue_tail(&cdev->recvqueue, skb);
608 wake_up_interruptible(&cdev->recvwait);
612 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
613 skb_queue_tail(&cdev->recvqueue, skb);
614 wake_up_interruptible(&cdev->recvwait);
616 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
620 skb_queue_tail(&cdev->recvqueue, skb);
621 wake_up_interruptible(&cdev->recvwait);
624 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
625 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
626 #ifdef _DEBUG_DATAFLOW
627 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
628 datahandle, skb->len-CAPIMSG_LEN(skb->data));
630 skb_queue_tail(&mp->inqueue, skb);
631 mp->inbytes += skb->len;
632 handle_minor_recv(mp);
634 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
636 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
637 #ifdef _DEBUG_DATAFLOW
638 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
640 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
643 (void)capiminor_del_ack(mp, datahandle);
646 (void)handle_minor_send(mp);
649 /* ups, let capi application handle it :-) */
650 skb_queue_tail(&cdev->recvqueue, skb);
651 wake_up_interruptible(&cdev->recvwait);
653 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
656 spin_unlock_irqrestore(&workaround_lock, flags);
657 mutex_unlock(&cdev->lock);
660 /* -------- file_operations for capidev ----------------------------- */
663 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
665 struct capidev *cdev = (struct capidev *)file->private_data;
670 if (!cdev->ap.applid)
673 skb = skb_dequeue(&cdev->recvqueue);
675 if (file->f_flags & O_NONBLOCK)
677 err = wait_event_interruptible(cdev->recvwait,
678 (skb = skb_dequeue(&cdev->recvqueue)));
682 if (skb->len > count) {
683 skb_queue_head(&cdev->recvqueue, skb);
686 if (copy_to_user(buf, skb->data, skb->len)) {
687 skb_queue_head(&cdev->recvqueue, skb);
698 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
700 struct capidev *cdev = (struct capidev *)file->private_data;
704 if (!cdev->ap.applid)
707 skb = alloc_skb(count, GFP_USER);
711 if (copy_from_user(skb_put(skb, count), buf, count)) {
715 mlen = CAPIMSG_LEN(skb->data);
716 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
717 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
727 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
729 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
730 mutex_lock(&cdev->lock);
731 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
732 mutex_unlock(&cdev->lock);
735 cdev->errcode = capi20_put_message(&cdev->ap, skb);
745 capi_poll(struct file *file, poll_table * wait)
747 struct capidev *cdev = (struct capidev *)file->private_data;
748 unsigned int mask = 0;
750 if (!cdev->ap.applid)
753 poll_wait(file, &(cdev->recvwait), wait);
754 mask = POLLOUT | POLLWRNORM;
755 if (!skb_queue_empty(&cdev->recvqueue))
756 mask |= POLLIN | POLLRDNORM;
761 capi_ioctl(struct inode *inode, struct file *file,
762 unsigned int cmd, unsigned long arg)
764 struct capidev *cdev = file->private_data;
765 capi_ioctl_struct data;
766 int retval = -EINVAL;
767 void __user *argp = (void __user *)arg;
771 mutex_lock(&cdev->lock);
773 if (cdev->ap.applid) {
777 if (copy_from_user(&cdev->ap.rparam, argp,
778 sizeof(struct capi_register_params))) {
782 cdev->ap.private = cdev;
783 cdev->ap.recv_message = capi_recv_message;
784 cdev->errcode = capi20_register(&cdev->ap);
785 retval = (int)cdev->ap.applid;
792 mutex_unlock(&cdev->lock);
795 case CAPI_GET_VERSION:
797 if (copy_from_user(&data.contr, argp,
800 cdev->errcode = capi20_get_version(data.contr, &data.version);
803 if (copy_to_user(argp, &data.version,
804 sizeof(data.version)))
809 case CAPI_GET_SERIAL:
811 if (copy_from_user(&data.contr, argp,
814 cdev->errcode = capi20_get_serial (data.contr, data.serial);
817 if (copy_to_user(argp, data.serial,
818 sizeof(data.serial)))
822 case CAPI_GET_PROFILE:
824 if (copy_from_user(&data.contr, argp,
828 if (data.contr == 0) {
829 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
833 retval = copy_to_user(argp,
834 &data.profile.ncontroller,
835 sizeof(data.profile.ncontroller));
838 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
842 retval = copy_to_user(argp, &data.profile,
843 sizeof(data.profile));
850 case CAPI_GET_MANUFACTURER:
852 if (copy_from_user(&data.contr, argp,
855 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
859 if (copy_to_user(argp, data.manufacturer,
860 sizeof(data.manufacturer)))
865 case CAPI_GET_ERRCODE:
866 data.errcode = cdev->errcode;
867 cdev->errcode = CAPI_NOERROR;
869 if (copy_to_user(argp, &data.errcode,
870 sizeof(data.errcode)))
876 if (capi20_isinstalled() == CAPI_NOERROR)
880 case CAPI_MANUFACTURER_CMD:
882 struct capi_manufacturer_cmd mcmd;
883 if (!capable(CAP_SYS_ADMIN))
885 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
887 return capi20_manufacturer(mcmd.cmd, mcmd.data);
892 case CAPI_CLR_FLAGS: {
895 if (copy_from_user(&userflags, argp, sizeof(userflags)))
898 mutex_lock(&cdev->lock);
899 if (cmd == CAPI_SET_FLAGS)
900 cdev->userflags |= userflags;
902 cdev->userflags &= ~userflags;
903 mutex_unlock(&cdev->lock);
907 if (copy_to_user(argp, &cdev->userflags,
908 sizeof(cdev->userflags)))
912 case CAPI_NCCI_OPENCOUNT: {
913 struct capincci *nccip;
917 if (copy_from_user(&ncci, argp, sizeof(ncci)))
920 mutex_lock(&cdev->lock);
921 nccip = capincci_find(cdev, (u32)ncci);
923 count = capincci_minor_opencount(nccip);
924 mutex_unlock(&cdev->lock);
928 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
929 case CAPI_NCCI_GETUNIT: {
930 struct capincci *nccip;
931 struct capiminor *mp;
935 if (copy_from_user(&ncci, argp, sizeof(ncci)))
938 mutex_lock(&cdev->lock);
939 nccip = capincci_find(cdev, (u32)ncci);
945 mutex_unlock(&cdev->lock);
948 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
955 static int capi_open(struct inode *inode, struct file *file)
957 struct capidev *cdev;
959 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
963 mutex_init(&cdev->lock);
964 skb_queue_head_init(&cdev->recvqueue);
965 init_waitqueue_head(&cdev->recvwait);
966 INIT_LIST_HEAD(&cdev->nccis);
967 file->private_data = cdev;
969 mutex_lock(&capidev_list_lock);
970 list_add_tail(&cdev->list, &capidev_list);
971 mutex_unlock(&capidev_list_lock);
973 return nonseekable_open(inode, file);
976 static int capi_release(struct inode *inode, struct file *file)
978 struct capidev *cdev = file->private_data;
980 mutex_lock(&capidev_list_lock);
981 list_del(&cdev->list);
982 mutex_unlock(&capidev_list_lock);
985 capi20_release(&cdev->ap);
986 skb_queue_purge(&cdev->recvqueue);
987 capincci_free(cdev, 0xffffffff);
993 static const struct file_operations capi_fops =
995 .owner = THIS_MODULE,
1000 .ioctl = capi_ioctl,
1002 .release = capi_release,
1005 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1006 /* -------- tty_operations for capincci ----------------------------- */
1008 static int capinc_tty_open(struct tty_struct * tty, struct file * file)
1010 struct capiminor *mp;
1011 unsigned long flags;
1013 mp = capiminor_get(iminor(file->f_path.dentry->d_inode));
1014 if (mp->nccip == NULL)
1017 tty->driver_data = (void *)mp;
1019 spin_lock_irqsave(&workaround_lock, flags);
1020 if (atomic_read(&mp->ttyopencount) == 0)
1022 atomic_inc(&mp->ttyopencount);
1023 #ifdef _DEBUG_REFCOUNT
1024 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1026 handle_minor_recv(mp);
1027 spin_unlock_irqrestore(&workaround_lock, flags);
1031 static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1033 struct capiminor *mp;
1035 mp = (struct capiminor *)tty->driver_data;
1037 if (atomic_dec_and_test(&mp->ttyopencount)) {
1038 #ifdef _DEBUG_REFCOUNT
1039 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1041 tty->driver_data = NULL;
1044 #ifdef _DEBUG_REFCOUNT
1045 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1047 if (mp->nccip == NULL)
1053 #ifdef _DEBUG_REFCOUNT
1054 printk(KERN_DEBUG "capinc_tty_close\n");
1058 static int capinc_tty_write(struct tty_struct * tty,
1059 const unsigned char *buf, int count)
1061 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1062 struct sk_buff *skb;
1063 unsigned long flags;
1065 #ifdef _DEBUG_TTYFUNCS
1066 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1069 if (!mp || !mp->nccip) {
1070 #ifdef _DEBUG_TTYFUNCS
1071 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1076 spin_lock_irqsave(&workaround_lock, flags);
1080 skb_queue_tail(&mp->outqueue, skb);
1081 mp->outbytes += skb->len;
1084 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1086 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1087 spin_unlock_irqrestore(&workaround_lock, flags);
1091 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1092 memcpy(skb_put(skb, count), buf, count);
1094 skb_queue_tail(&mp->outqueue, skb);
1095 mp->outbytes += skb->len;
1096 (void)handle_minor_send(mp);
1097 (void)handle_minor_recv(mp);
1098 spin_unlock_irqrestore(&workaround_lock, flags);
1102 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1104 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1105 struct sk_buff *skb;
1106 unsigned long flags;
1109 #ifdef _DEBUG_TTYFUNCS
1110 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1113 if (!mp || !mp->nccip) {
1114 #ifdef _DEBUG_TTYFUNCS
1115 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1120 spin_lock_irqsave(&workaround_lock, flags);
1123 if (skb_tailroom(skb) > 0) {
1124 *(skb_put(skb, 1)) = ch;
1125 spin_unlock_irqrestore(&workaround_lock, flags);
1129 skb_queue_tail(&mp->outqueue, skb);
1130 mp->outbytes += skb->len;
1131 (void)handle_minor_send(mp);
1133 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1135 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1136 *(skb_put(skb, 1)) = ch;
1139 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1142 spin_unlock_irqrestore(&workaround_lock, flags);
1146 static void capinc_tty_flush_chars(struct tty_struct *tty)
1148 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1149 struct sk_buff *skb;
1150 unsigned long flags;
1152 #ifdef _DEBUG_TTYFUNCS
1153 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1156 if (!mp || !mp->nccip) {
1157 #ifdef _DEBUG_TTYFUNCS
1158 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1163 spin_lock_irqsave(&workaround_lock, flags);
1167 skb_queue_tail(&mp->outqueue, skb);
1168 mp->outbytes += skb->len;
1169 (void)handle_minor_send(mp);
1171 (void)handle_minor_recv(mp);
1172 spin_unlock_irqrestore(&workaround_lock, flags);
1175 static int capinc_tty_write_room(struct tty_struct *tty)
1177 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1179 if (!mp || !mp->nccip) {
1180 #ifdef _DEBUG_TTYFUNCS
1181 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
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 = (struct capiminor *)tty->driver_data;
1196 if (!mp || !mp->nccip) {
1197 #ifdef _DEBUG_TTYFUNCS
1198 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1202 #ifdef _DEBUG_TTYFUNCS
1203 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1204 mp->outbytes, mp->nack,
1205 skb_queue_len(&mp->outqueue),
1206 skb_queue_len(&mp->inqueue));
1208 return mp->outbytes;
1211 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1212 unsigned int cmd, unsigned long arg)
1217 error = n_tty_ioctl_helper(tty, file, cmd, arg);
1223 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
1225 #ifdef _DEBUG_TTYFUNCS
1226 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1230 static void capinc_tty_throttle(struct tty_struct * tty)
1232 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1233 #ifdef _DEBUG_TTYFUNCS
1234 printk(KERN_DEBUG "capinc_tty_throttle\n");
1240 static void capinc_tty_unthrottle(struct tty_struct * tty)
1242 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1243 unsigned long flags;
1244 #ifdef _DEBUG_TTYFUNCS
1245 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1248 spin_lock_irqsave(&workaround_lock, flags);
1250 handle_minor_recv(mp);
1251 spin_unlock_irqrestore(&workaround_lock, flags);
1255 static void capinc_tty_stop(struct tty_struct *tty)
1257 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1258 #ifdef _DEBUG_TTYFUNCS
1259 printk(KERN_DEBUG "capinc_tty_stop\n");
1266 static void capinc_tty_start(struct tty_struct *tty)
1268 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1269 unsigned long flags;
1270 #ifdef _DEBUG_TTYFUNCS
1271 printk(KERN_DEBUG "capinc_tty_start\n");
1274 spin_lock_irqsave(&workaround_lock, flags);
1276 (void)handle_minor_send(mp);
1277 spin_unlock_irqrestore(&workaround_lock, flags);
1281 static void capinc_tty_hangup(struct tty_struct *tty)
1283 #ifdef _DEBUG_TTYFUNCS
1284 printk(KERN_DEBUG "capinc_tty_hangup\n");
1288 static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1290 #ifdef _DEBUG_TTYFUNCS
1291 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1296 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1298 #ifdef _DEBUG_TTYFUNCS
1299 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1303 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1305 #ifdef _DEBUG_TTYFUNCS
1306 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1310 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1312 #ifdef _DEBUG_TTYFUNCS
1313 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1317 static const struct tty_operations capinc_ops = {
1318 .open = capinc_tty_open,
1319 .close = capinc_tty_close,
1320 .write = capinc_tty_write,
1321 .put_char = capinc_tty_put_char,
1322 .flush_chars = capinc_tty_flush_chars,
1323 .write_room = capinc_tty_write_room,
1324 .chars_in_buffer = capinc_tty_chars_in_buffer,
1325 .ioctl = capinc_tty_ioctl,
1326 .set_termios = capinc_tty_set_termios,
1327 .throttle = capinc_tty_throttle,
1328 .unthrottle = capinc_tty_unthrottle,
1329 .stop = capinc_tty_stop,
1330 .start = capinc_tty_start,
1331 .hangup = capinc_tty_hangup,
1332 .break_ctl = capinc_tty_break_ctl,
1333 .flush_buffer = capinc_tty_flush_buffer,
1334 .set_ldisc = capinc_tty_set_ldisc,
1335 .send_xchar = capinc_tty_send_xchar,
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);