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 ----------------------------- */
1009 capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1011 int idx = tty->index;
1012 struct capiminor *mp = capiminor_get(idx);
1013 int ret = tty_init_termios(tty);
1016 tty_driver_kref_get(driver);
1018 tty->driver_data = mp;
1019 driver->ttys[idx] = tty;
1025 static void capinc_tty_cleanup(struct tty_struct *tty)
1027 struct capiminor *mp = tty->driver_data;
1028 tty->driver_data = NULL;
1032 static int capinc_tty_open(struct tty_struct * tty, struct file * file)
1034 struct capiminor *mp = tty->driver_data;
1035 unsigned long flags;
1037 spin_lock_irqsave(&workaround_lock, flags);
1038 if (atomic_read(&mp->ttyopencount) == 0)
1040 atomic_inc(&mp->ttyopencount);
1041 #ifdef _DEBUG_REFCOUNT
1042 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1044 handle_minor_recv(mp);
1045 spin_unlock_irqrestore(&workaround_lock, flags);
1049 static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1051 struct capiminor *mp = tty->driver_data;
1053 if (atomic_dec_and_test(&mp->ttyopencount)) {
1054 #ifdef _DEBUG_REFCOUNT
1055 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1059 #ifdef _DEBUG_REFCOUNT
1060 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1062 if (mp->nccip == NULL)
1065 #ifdef _DEBUG_REFCOUNT
1066 printk(KERN_DEBUG "capinc_tty_close\n");
1070 static int capinc_tty_write(struct tty_struct * tty,
1071 const unsigned char *buf, int count)
1073 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1074 struct sk_buff *skb;
1075 unsigned long flags;
1077 #ifdef _DEBUG_TTYFUNCS
1078 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1081 if (!mp || !mp->nccip) {
1082 #ifdef _DEBUG_TTYFUNCS
1083 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1088 spin_lock_irqsave(&workaround_lock, flags);
1092 skb_queue_tail(&mp->outqueue, skb);
1093 mp->outbytes += skb->len;
1096 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1098 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1099 spin_unlock_irqrestore(&workaround_lock, flags);
1103 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1104 memcpy(skb_put(skb, count), buf, count);
1106 skb_queue_tail(&mp->outqueue, skb);
1107 mp->outbytes += skb->len;
1108 (void)handle_minor_send(mp);
1109 (void)handle_minor_recv(mp);
1110 spin_unlock_irqrestore(&workaround_lock, flags);
1114 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1116 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1117 struct sk_buff *skb;
1118 unsigned long flags;
1121 #ifdef _DEBUG_TTYFUNCS
1122 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1125 if (!mp || !mp->nccip) {
1126 #ifdef _DEBUG_TTYFUNCS
1127 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
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 = (struct capiminor *)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 if (!mp || !mp->nccip) {
1169 #ifdef _DEBUG_TTYFUNCS
1170 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1175 spin_lock_irqsave(&workaround_lock, flags);
1179 skb_queue_tail(&mp->outqueue, skb);
1180 mp->outbytes += skb->len;
1181 (void)handle_minor_send(mp);
1183 (void)handle_minor_recv(mp);
1184 spin_unlock_irqrestore(&workaround_lock, flags);
1187 static int capinc_tty_write_room(struct tty_struct *tty)
1189 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1191 if (!mp || !mp->nccip) {
1192 #ifdef _DEBUG_TTYFUNCS
1193 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1197 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1198 room *= CAPI_MAX_BLKSIZE;
1199 #ifdef _DEBUG_TTYFUNCS
1200 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1205 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1207 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1208 if (!mp || !mp->nccip) {
1209 #ifdef _DEBUG_TTYFUNCS
1210 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1214 #ifdef _DEBUG_TTYFUNCS
1215 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1216 mp->outbytes, mp->nack,
1217 skb_queue_len(&mp->outqueue),
1218 skb_queue_len(&mp->inqueue));
1220 return mp->outbytes;
1223 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1224 unsigned int cmd, unsigned long arg)
1229 error = n_tty_ioctl_helper(tty, file, cmd, arg);
1235 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
1237 #ifdef _DEBUG_TTYFUNCS
1238 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1242 static void capinc_tty_throttle(struct tty_struct * tty)
1244 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1245 #ifdef _DEBUG_TTYFUNCS
1246 printk(KERN_DEBUG "capinc_tty_throttle\n");
1252 static void capinc_tty_unthrottle(struct tty_struct * tty)
1254 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1255 unsigned long flags;
1256 #ifdef _DEBUG_TTYFUNCS
1257 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1260 spin_lock_irqsave(&workaround_lock, flags);
1262 handle_minor_recv(mp);
1263 spin_unlock_irqrestore(&workaround_lock, flags);
1267 static void capinc_tty_stop(struct tty_struct *tty)
1269 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1270 #ifdef _DEBUG_TTYFUNCS
1271 printk(KERN_DEBUG "capinc_tty_stop\n");
1278 static void capinc_tty_start(struct tty_struct *tty)
1280 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1281 unsigned long flags;
1282 #ifdef _DEBUG_TTYFUNCS
1283 printk(KERN_DEBUG "capinc_tty_start\n");
1286 spin_lock_irqsave(&workaround_lock, flags);
1288 (void)handle_minor_send(mp);
1289 spin_unlock_irqrestore(&workaround_lock, flags);
1293 static void capinc_tty_hangup(struct tty_struct *tty)
1295 #ifdef _DEBUG_TTYFUNCS
1296 printk(KERN_DEBUG "capinc_tty_hangup\n");
1300 static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1302 #ifdef _DEBUG_TTYFUNCS
1303 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1308 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1310 #ifdef _DEBUG_TTYFUNCS
1311 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1315 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1317 #ifdef _DEBUG_TTYFUNCS
1318 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1322 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1324 #ifdef _DEBUG_TTYFUNCS
1325 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1329 static const struct tty_operations capinc_ops = {
1330 .open = capinc_tty_open,
1331 .close = capinc_tty_close,
1332 .write = capinc_tty_write,
1333 .put_char = capinc_tty_put_char,
1334 .flush_chars = capinc_tty_flush_chars,
1335 .write_room = capinc_tty_write_room,
1336 .chars_in_buffer = capinc_tty_chars_in_buffer,
1337 .ioctl = capinc_tty_ioctl,
1338 .set_termios = capinc_tty_set_termios,
1339 .throttle = capinc_tty_throttle,
1340 .unthrottle = capinc_tty_unthrottle,
1341 .stop = capinc_tty_stop,
1342 .start = capinc_tty_start,
1343 .hangup = capinc_tty_hangup,
1344 .break_ctl = capinc_tty_break_ctl,
1345 .flush_buffer = capinc_tty_flush_buffer,
1346 .set_ldisc = capinc_tty_set_ldisc,
1347 .send_xchar = capinc_tty_send_xchar,
1348 .install = capinc_tty_install,
1349 .cleanup = capinc_tty_cleanup,
1352 static int __init capinc_tty_init(void)
1354 struct tty_driver *drv;
1357 if (capi_ttyminors > CAPINC_MAX_PORTS)
1358 capi_ttyminors = CAPINC_MAX_PORTS;
1359 if (capi_ttyminors <= 0)
1360 capi_ttyminors = CAPINC_NR_PORTS;
1362 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1367 drv = alloc_tty_driver(capi_ttyminors);
1372 drv->owner = THIS_MODULE;
1373 drv->driver_name = "capi_nc";
1376 drv->minor_start = 0;
1377 drv->type = TTY_DRIVER_TYPE_SERIAL;
1378 drv->subtype = SERIAL_TYPE_NORMAL;
1379 drv->init_termios = tty_std_termios;
1380 drv->init_termios.c_iflag = ICRNL;
1381 drv->init_termios.c_oflag = OPOST | ONLCR;
1382 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1383 drv->init_termios.c_lflag = 0;
1385 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1386 TTY_DRIVER_DYNAMIC_DEV;
1387 tty_set_operations(drv, &capinc_ops);
1389 err = tty_register_driver(drv);
1391 put_tty_driver(drv);
1393 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1396 capinc_tty_driver = drv;
1400 static void __exit capinc_tty_exit(void)
1402 tty_unregister_driver(capinc_tty_driver);
1403 put_tty_driver(capinc_tty_driver);
1407 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1409 static inline int capinc_tty_init(void)
1414 static inline void capinc_tty_exit(void) { }
1416 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1418 /* -------- /proc functions ----------------------------------------- */
1421 * /proc/capi/capi20:
1422 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1424 static int capi20_proc_show(struct seq_file *m, void *v)
1426 struct capidev *cdev;
1427 struct list_head *l;
1429 mutex_lock(&capidev_list_lock);
1430 list_for_each(l, &capidev_list) {
1431 cdev = list_entry(l, struct capidev, list);
1432 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
1434 cdev->ap.nrecvctlpkt,
1435 cdev->ap.nrecvdatapkt,
1436 cdev->ap.nsentctlpkt,
1437 cdev->ap.nsentdatapkt);
1439 mutex_unlock(&capidev_list_lock);
1443 static int capi20_proc_open(struct inode *inode, struct file *file)
1445 return single_open(file, capi20_proc_show, NULL);
1448 static const struct file_operations capi20_proc_fops = {
1449 .owner = THIS_MODULE,
1450 .open = capi20_proc_open,
1452 .llseek = seq_lseek,
1453 .release = single_release,
1457 * /proc/capi/capi20ncci:
1460 static int capi20ncci_proc_show(struct seq_file *m, void *v)
1462 struct capidev *cdev;
1463 struct capincci *np;
1465 mutex_lock(&capidev_list_lock);
1466 list_for_each_entry(cdev, &capidev_list, list) {
1467 mutex_lock(&cdev->lock);
1468 list_for_each_entry(np, &cdev->nccis, list)
1469 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
1470 mutex_unlock(&cdev->lock);
1472 mutex_unlock(&capidev_list_lock);
1476 static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1478 return single_open(file, capi20ncci_proc_show, NULL);
1481 static const struct file_operations capi20ncci_proc_fops = {
1482 .owner = THIS_MODULE,
1483 .open = capi20ncci_proc_open,
1485 .llseek = seq_lseek,
1486 .release = single_release,
1489 static void __init proc_init(void)
1491 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1492 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
1495 static void __exit proc_exit(void)
1497 remove_proc_entry("capi/capi20", NULL);
1498 remove_proc_entry("capi/capi20ncci", NULL);
1501 /* -------- init function and module interface ---------------------- */
1504 static int __init capi_init(void)
1506 const char *compileinfo;
1509 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1510 if (major_ret < 0) {
1511 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1514 capi_class = class_create(THIS_MODULE, "capi");
1515 if (IS_ERR(capi_class)) {
1516 unregister_chrdev(capi_major, "capi20");
1517 return PTR_ERR(capi_class);
1520 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
1522 if (capinc_tty_init() < 0) {
1523 device_destroy(capi_class, MKDEV(capi_major, 0));
1524 class_destroy(capi_class);
1525 unregister_chrdev(capi_major, "capi20");
1531 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1532 compileinfo = " (middleware+capifs)";
1533 #elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
1534 compileinfo = " (no capifs)";
1536 compileinfo = " (no middleware)";
1538 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1539 capi_major, compileinfo);
1544 static void __exit capi_exit(void)
1548 device_destroy(capi_class, MKDEV(capi_major, 0));
1549 class_destroy(capi_class);
1550 unregister_chrdev(capi_major, "capi20");
1555 module_init(capi_init);
1556 module_exit(capi_exit);