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_ttymajor = 191;
64 static int capi_ttyminors = CAPINC_NR_PORTS;
66 module_param_named(ttymajor, capi_ttymajor, uint, 0);
67 module_param_named(ttyminors, capi_ttyminors, uint, 0);
68 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
70 /* -------- defines ------------------------------------------------- */
72 #define CAPINC_MAX_RECVQUEUE 10
73 #define CAPINC_MAX_SENDQUEUE 10
74 #define CAPI_MAX_BLKSIZE 2048
76 /* -------- data structures ----------------------------------------- */
82 struct datahandle_queue {
83 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");
231 atomic_set(&mp->ttyopencount,0);
232 INIT_LIST_HEAD(&mp->ackqueue);
233 spin_lock_init(&mp->ackqlock);
235 skb_queue_head_init(&mp->inqueue);
236 skb_queue_head_init(&mp->outqueue);
238 /* Allocate the least unused minor number. */
239 write_lock_irqsave(&capiminors_lock, flags);
240 for (minor = 0; minor < capi_ttyminors; minor++)
241 if (!capiminors[minor]) {
242 capiminors[minor] = mp;
245 write_unlock_irqrestore(&capiminors_lock, flags);
247 if (minor == capi_ttyminors) {
248 printk(KERN_NOTICE "capi: out of minors\n");
254 dev = tty_register_device(capinc_tty_driver, minor, NULL);
261 write_lock_irqsave(&capiminors_lock, flags);
262 capiminors[minor] = NULL;
263 write_unlock_irqrestore(&capiminors_lock, flags);
270 static void capiminor_free(struct capiminor *mp)
274 tty_unregister_device(capinc_tty_driver, mp->minor);
276 write_lock_irqsave(&capiminors_lock, flags);
277 capiminors[mp->minor] = NULL;
278 write_unlock_irqrestore(&capiminors_lock, flags);
280 kfree_skb(mp->ttyskb);
282 skb_queue_purge(&mp->inqueue);
283 skb_queue_purge(&mp->outqueue);
284 capiminor_del_all_ack(mp);
288 static struct capiminor *capiminor_get(unsigned int minor)
290 struct capiminor *mp;
292 read_lock(&capiminors_lock);
293 mp = capiminors[minor];
294 read_unlock(&capiminors_lock);
299 /* -------- struct capincci ----------------------------------------- */
301 static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
303 struct capiminor *mp;
305 if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
308 mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
311 #ifdef _DEBUG_REFCOUNT
312 printk(KERN_DEBUG "set mp->nccip\n");
315 capifs_new_ncci(mp->minor,
316 MKDEV(capi_ttymajor, mp->minor));
320 static void capincci_free_minor(struct capincci *np)
322 struct capiminor *mp = np->minorp;
325 capifs_free_ncci(mp->capifs_dentry);
328 #ifdef _DEBUG_REFCOUNT
329 printk(KERN_DEBUG "reset mp->nccip\n");
338 static inline unsigned int capincci_minor_opencount(struct capincci *np)
340 struct capiminor *mp = np->minorp;
342 return mp ? atomic_read(&mp->ttyopencount) : 0;
345 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
348 capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
349 static inline void capincci_free_minor(struct capincci *np) { }
351 static inline unsigned int capincci_minor_opencount(struct capincci *np)
356 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
358 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
362 np = kzalloc(sizeof(*np), GFP_KERNEL);
368 capincci_alloc_minor(cdev, np);
370 list_add_tail(&np->list, &cdev->nccis);
375 static void capincci_free(struct capidev *cdev, u32 ncci)
377 struct capincci *np, *tmp;
379 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
380 if (ncci == 0xffffffff || np->ncci == ncci) {
381 capincci_free_minor(np);
387 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
391 list_for_each_entry(np, &cdev->nccis, list)
392 if (np->ncci == ncci)
397 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
398 /* -------- handle data queue --------------------------------------- */
400 static struct sk_buff *
401 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
403 struct sk_buff *nskb;
404 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
406 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
407 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
408 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
409 capimsg_setu16(s, 2, mp->ap->applid);
410 capimsg_setu8 (s, 4, CAPI_DATA_B3);
411 capimsg_setu8 (s, 5, CAPI_RESP);
412 capimsg_setu16(s, 6, mp->msgid++);
413 capimsg_setu32(s, 8, mp->ncci);
414 capimsg_setu16(s, 12, datahandle);
419 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
421 struct sk_buff *nskb;
423 u16 errcode, datahandle;
424 struct tty_ldisc *ld;
426 datalen = skb->len - CAPIMSG_LEN(skb->data);
429 #ifdef _DEBUG_DATAFLOW
430 printk(KERN_DEBUG "capi: currently no receiver\n");
435 ld = tty_ldisc_ref(mp->tty);
438 if (ld->ops->receive_buf == NULL) {
439 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
440 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
445 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
446 printk(KERN_DEBUG "capi: recv tty throttled\n");
450 if (mp->tty->receive_room < datalen) {
451 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
452 printk(KERN_DEBUG "capi: no room in tty\n");
456 if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
457 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
460 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
461 errcode = capi20_put_message(mp->ap, nskb);
462 if (errcode != CAPI_NOERROR) {
463 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
468 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
469 #ifdef _DEBUG_DATAFLOW
470 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
471 datahandle, skb->len);
473 ld->ops->receive_buf(mp->tty, skb->data, NULL, skb->len);
482 static void handle_minor_recv(struct capiminor *mp)
485 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
486 unsigned int len = skb->len;
488 if (handle_recv_skb(mp, skb) < 0) {
489 skb_queue_head(&mp->inqueue, skb);
496 static int handle_minor_send(struct capiminor *mp)
504 if (mp->tty && mp->ttyoutstop) {
505 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
506 printk(KERN_DEBUG "capi: send: tty stopped\n");
511 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
512 datahandle = mp->datahandle;
514 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
515 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
516 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
517 capimsg_setu16(skb->data, 2, mp->ap->applid);
518 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
519 capimsg_setu8 (skb->data, 5, CAPI_REQ);
520 capimsg_setu16(skb->data, 6, mp->msgid++);
521 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
522 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
523 capimsg_setu16(skb->data, 16, len); /* Data length */
524 capimsg_setu16(skb->data, 18, datahandle);
525 capimsg_setu16(skb->data, 20, 0); /* Flags */
527 if (capiminor_add_ack(mp, datahandle) < 0) {
528 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
529 skb_queue_head(&mp->outqueue, skb);
532 errcode = capi20_put_message(mp->ap, skb);
533 if (errcode == CAPI_NOERROR) {
537 #ifdef _DEBUG_DATAFLOW
538 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
543 capiminor_del_ack(mp, datahandle);
545 if (errcode == CAPI_SENDQUEUEFULL) {
546 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
547 skb_queue_head(&mp->outqueue, skb);
551 /* ups, drop packet */
552 printk(KERN_ERR "capi: put_message = %x\n", errcode);
559 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
560 /* -------- function called by lower level -------------------------- */
562 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
564 struct capidev *cdev = ap->private;
565 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
566 struct capiminor *mp;
568 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
572 mutex_lock(&cdev->lock);
574 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
575 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
576 if ((info & 0xff00) == 0)
577 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
579 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
580 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
582 spin_lock_irqsave(&workaround_lock, flags);
583 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
584 skb_queue_tail(&cdev->recvqueue, skb);
585 wake_up_interruptible(&cdev->recvwait);
589 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
591 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
592 skb_queue_tail(&cdev->recvqueue, skb);
593 wake_up_interruptible(&cdev->recvwait);
597 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
598 skb_queue_tail(&cdev->recvqueue, skb);
599 wake_up_interruptible(&cdev->recvwait);
601 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
605 skb_queue_tail(&cdev->recvqueue, skb);
606 wake_up_interruptible(&cdev->recvwait);
609 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
610 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
611 #ifdef _DEBUG_DATAFLOW
612 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
613 datahandle, skb->len-CAPIMSG_LEN(skb->data));
615 skb_queue_tail(&mp->inqueue, skb);
616 mp->inbytes += skb->len;
617 handle_minor_recv(mp);
619 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
621 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
622 #ifdef _DEBUG_DATAFLOW
623 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
625 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
628 (void)capiminor_del_ack(mp, datahandle);
631 (void)handle_minor_send(mp);
634 /* ups, let capi application handle it :-) */
635 skb_queue_tail(&cdev->recvqueue, skb);
636 wake_up_interruptible(&cdev->recvwait);
638 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
641 spin_unlock_irqrestore(&workaround_lock, flags);
642 mutex_unlock(&cdev->lock);
645 /* -------- file_operations for capidev ----------------------------- */
648 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
650 struct capidev *cdev = (struct capidev *)file->private_data;
655 if (!cdev->ap.applid)
658 skb = skb_dequeue(&cdev->recvqueue);
660 if (file->f_flags & O_NONBLOCK)
662 err = wait_event_interruptible(cdev->recvwait,
663 (skb = skb_dequeue(&cdev->recvqueue)));
667 if (skb->len > count) {
668 skb_queue_head(&cdev->recvqueue, skb);
671 if (copy_to_user(buf, skb->data, skb->len)) {
672 skb_queue_head(&cdev->recvqueue, skb);
683 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
685 struct capidev *cdev = (struct capidev *)file->private_data;
689 if (!cdev->ap.applid)
692 skb = alloc_skb(count, GFP_USER);
696 if (copy_from_user(skb_put(skb, count), buf, count)) {
700 mlen = CAPIMSG_LEN(skb->data);
701 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
702 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
712 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
714 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
715 mutex_lock(&cdev->lock);
716 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
717 mutex_unlock(&cdev->lock);
720 cdev->errcode = capi20_put_message(&cdev->ap, skb);
730 capi_poll(struct file *file, poll_table * wait)
732 struct capidev *cdev = (struct capidev *)file->private_data;
733 unsigned int mask = 0;
735 if (!cdev->ap.applid)
738 poll_wait(file, &(cdev->recvwait), wait);
739 mask = POLLOUT | POLLWRNORM;
740 if (!skb_queue_empty(&cdev->recvqueue))
741 mask |= POLLIN | POLLRDNORM;
746 capi_ioctl(struct inode *inode, struct file *file,
747 unsigned int cmd, unsigned long arg)
749 struct capidev *cdev = file->private_data;
750 capi_ioctl_struct data;
751 int retval = -EINVAL;
752 void __user *argp = (void __user *)arg;
756 mutex_lock(&cdev->lock);
758 if (cdev->ap.applid) {
762 if (copy_from_user(&cdev->ap.rparam, argp,
763 sizeof(struct capi_register_params))) {
767 cdev->ap.private = cdev;
768 cdev->ap.recv_message = capi_recv_message;
769 cdev->errcode = capi20_register(&cdev->ap);
770 retval = (int)cdev->ap.applid;
777 mutex_unlock(&cdev->lock);
780 case CAPI_GET_VERSION:
782 if (copy_from_user(&data.contr, argp,
785 cdev->errcode = capi20_get_version(data.contr, &data.version);
788 if (copy_to_user(argp, &data.version,
789 sizeof(data.version)))
794 case CAPI_GET_SERIAL:
796 if (copy_from_user(&data.contr, argp,
799 cdev->errcode = capi20_get_serial (data.contr, data.serial);
802 if (copy_to_user(argp, data.serial,
803 sizeof(data.serial)))
807 case CAPI_GET_PROFILE:
809 if (copy_from_user(&data.contr, argp,
813 if (data.contr == 0) {
814 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
818 retval = copy_to_user(argp,
819 &data.profile.ncontroller,
820 sizeof(data.profile.ncontroller));
823 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
827 retval = copy_to_user(argp, &data.profile,
828 sizeof(data.profile));
835 case CAPI_GET_MANUFACTURER:
837 if (copy_from_user(&data.contr, argp,
840 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
844 if (copy_to_user(argp, data.manufacturer,
845 sizeof(data.manufacturer)))
850 case CAPI_GET_ERRCODE:
851 data.errcode = cdev->errcode;
852 cdev->errcode = CAPI_NOERROR;
854 if (copy_to_user(argp, &data.errcode,
855 sizeof(data.errcode)))
861 if (capi20_isinstalled() == CAPI_NOERROR)
865 case CAPI_MANUFACTURER_CMD:
867 struct capi_manufacturer_cmd mcmd;
868 if (!capable(CAP_SYS_ADMIN))
870 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
872 return capi20_manufacturer(mcmd.cmd, mcmd.data);
877 case CAPI_CLR_FLAGS: {
880 if (copy_from_user(&userflags, argp, sizeof(userflags)))
883 mutex_lock(&cdev->lock);
884 if (cmd == CAPI_SET_FLAGS)
885 cdev->userflags |= userflags;
887 cdev->userflags &= ~userflags;
888 mutex_unlock(&cdev->lock);
892 if (copy_to_user(argp, &cdev->userflags,
893 sizeof(cdev->userflags)))
897 case CAPI_NCCI_OPENCOUNT: {
898 struct capincci *nccip;
902 if (copy_from_user(&ncci, argp, sizeof(ncci)))
905 mutex_lock(&cdev->lock);
906 nccip = capincci_find(cdev, (u32)ncci);
908 count = capincci_minor_opencount(nccip);
909 mutex_unlock(&cdev->lock);
913 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
914 case CAPI_NCCI_GETUNIT: {
915 struct capincci *nccip;
916 struct capiminor *mp;
920 if (copy_from_user(&ncci, argp, sizeof(ncci)))
923 mutex_lock(&cdev->lock);
924 nccip = capincci_find(cdev, (u32)ncci);
930 mutex_unlock(&cdev->lock);
933 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
940 static int capi_open(struct inode *inode, struct file *file)
942 struct capidev *cdev;
944 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
948 mutex_init(&cdev->lock);
949 skb_queue_head_init(&cdev->recvqueue);
950 init_waitqueue_head(&cdev->recvwait);
951 INIT_LIST_HEAD(&cdev->nccis);
952 file->private_data = cdev;
954 mutex_lock(&capidev_list_lock);
955 list_add_tail(&cdev->list, &capidev_list);
956 mutex_unlock(&capidev_list_lock);
958 return nonseekable_open(inode, file);
961 static int capi_release(struct inode *inode, struct file *file)
963 struct capidev *cdev = file->private_data;
965 mutex_lock(&capidev_list_lock);
966 list_del(&cdev->list);
967 mutex_unlock(&capidev_list_lock);
970 capi20_release(&cdev->ap);
971 skb_queue_purge(&cdev->recvqueue);
972 capincci_free(cdev, 0xffffffff);
978 static const struct file_operations capi_fops =
980 .owner = THIS_MODULE,
987 .release = capi_release,
990 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
991 /* -------- tty_operations for capincci ----------------------------- */
993 static int capinc_tty_open(struct tty_struct * tty, struct file * file)
995 struct capiminor *mp;
998 mp = capiminor_get(iminor(file->f_path.dentry->d_inode));
999 if (mp->nccip == NULL)
1002 tty->driver_data = (void *)mp;
1004 spin_lock_irqsave(&workaround_lock, flags);
1005 if (atomic_read(&mp->ttyopencount) == 0)
1007 atomic_inc(&mp->ttyopencount);
1008 #ifdef _DEBUG_REFCOUNT
1009 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1011 handle_minor_recv(mp);
1012 spin_unlock_irqrestore(&workaround_lock, flags);
1016 static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1018 struct capiminor *mp;
1020 mp = (struct capiminor *)tty->driver_data;
1022 if (atomic_dec_and_test(&mp->ttyopencount)) {
1023 #ifdef _DEBUG_REFCOUNT
1024 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1026 tty->driver_data = NULL;
1029 #ifdef _DEBUG_REFCOUNT
1030 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1032 if (mp->nccip == NULL)
1036 #ifdef _DEBUG_REFCOUNT
1037 printk(KERN_DEBUG "capinc_tty_close\n");
1041 static int capinc_tty_write(struct tty_struct * tty,
1042 const unsigned char *buf, int count)
1044 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1045 struct sk_buff *skb;
1046 unsigned long flags;
1048 #ifdef _DEBUG_TTYFUNCS
1049 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1052 if (!mp || !mp->nccip) {
1053 #ifdef _DEBUG_TTYFUNCS
1054 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1059 spin_lock_irqsave(&workaround_lock, flags);
1063 skb_queue_tail(&mp->outqueue, skb);
1064 mp->outbytes += skb->len;
1067 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1069 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1070 spin_unlock_irqrestore(&workaround_lock, flags);
1074 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1075 memcpy(skb_put(skb, count), buf, count);
1077 skb_queue_tail(&mp->outqueue, skb);
1078 mp->outbytes += skb->len;
1079 (void)handle_minor_send(mp);
1080 (void)handle_minor_recv(mp);
1081 spin_unlock_irqrestore(&workaround_lock, flags);
1085 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1087 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1088 struct sk_buff *skb;
1089 unsigned long flags;
1092 #ifdef _DEBUG_TTYFUNCS
1093 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1096 if (!mp || !mp->nccip) {
1097 #ifdef _DEBUG_TTYFUNCS
1098 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1103 spin_lock_irqsave(&workaround_lock, flags);
1106 if (skb_tailroom(skb) > 0) {
1107 *(skb_put(skb, 1)) = ch;
1108 spin_unlock_irqrestore(&workaround_lock, flags);
1112 skb_queue_tail(&mp->outqueue, skb);
1113 mp->outbytes += skb->len;
1114 (void)handle_minor_send(mp);
1116 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1118 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1119 *(skb_put(skb, 1)) = ch;
1122 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1125 spin_unlock_irqrestore(&workaround_lock, flags);
1129 static void capinc_tty_flush_chars(struct tty_struct *tty)
1131 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1132 struct sk_buff *skb;
1133 unsigned long flags;
1135 #ifdef _DEBUG_TTYFUNCS
1136 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1139 if (!mp || !mp->nccip) {
1140 #ifdef _DEBUG_TTYFUNCS
1141 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1146 spin_lock_irqsave(&workaround_lock, flags);
1150 skb_queue_tail(&mp->outqueue, skb);
1151 mp->outbytes += skb->len;
1152 (void)handle_minor_send(mp);
1154 (void)handle_minor_recv(mp);
1155 spin_unlock_irqrestore(&workaround_lock, flags);
1158 static int capinc_tty_write_room(struct tty_struct *tty)
1160 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1162 if (!mp || !mp->nccip) {
1163 #ifdef _DEBUG_TTYFUNCS
1164 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1168 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1169 room *= CAPI_MAX_BLKSIZE;
1170 #ifdef _DEBUG_TTYFUNCS
1171 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1176 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1178 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1179 if (!mp || !mp->nccip) {
1180 #ifdef _DEBUG_TTYFUNCS
1181 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1185 #ifdef _DEBUG_TTYFUNCS
1186 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1187 mp->outbytes, mp->nack,
1188 skb_queue_len(&mp->outqueue),
1189 skb_queue_len(&mp->inqueue));
1191 return mp->outbytes;
1194 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1195 unsigned int cmd, unsigned long arg)
1200 error = n_tty_ioctl_helper(tty, file, cmd, arg);
1206 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
1208 #ifdef _DEBUG_TTYFUNCS
1209 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1213 static void capinc_tty_throttle(struct tty_struct * tty)
1215 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1216 #ifdef _DEBUG_TTYFUNCS
1217 printk(KERN_DEBUG "capinc_tty_throttle\n");
1223 static void capinc_tty_unthrottle(struct tty_struct * tty)
1225 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1226 unsigned long flags;
1227 #ifdef _DEBUG_TTYFUNCS
1228 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1231 spin_lock_irqsave(&workaround_lock, flags);
1233 handle_minor_recv(mp);
1234 spin_unlock_irqrestore(&workaround_lock, flags);
1238 static void capinc_tty_stop(struct tty_struct *tty)
1240 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1241 #ifdef _DEBUG_TTYFUNCS
1242 printk(KERN_DEBUG "capinc_tty_stop\n");
1249 static void capinc_tty_start(struct tty_struct *tty)
1251 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1252 unsigned long flags;
1253 #ifdef _DEBUG_TTYFUNCS
1254 printk(KERN_DEBUG "capinc_tty_start\n");
1257 spin_lock_irqsave(&workaround_lock, flags);
1259 (void)handle_minor_send(mp);
1260 spin_unlock_irqrestore(&workaround_lock, flags);
1264 static void capinc_tty_hangup(struct tty_struct *tty)
1266 #ifdef _DEBUG_TTYFUNCS
1267 printk(KERN_DEBUG "capinc_tty_hangup\n");
1271 static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1273 #ifdef _DEBUG_TTYFUNCS
1274 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1279 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1281 #ifdef _DEBUG_TTYFUNCS
1282 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1286 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1288 #ifdef _DEBUG_TTYFUNCS
1289 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1293 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1295 #ifdef _DEBUG_TTYFUNCS
1296 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1300 static const struct tty_operations capinc_ops = {
1301 .open = capinc_tty_open,
1302 .close = capinc_tty_close,
1303 .write = capinc_tty_write,
1304 .put_char = capinc_tty_put_char,
1305 .flush_chars = capinc_tty_flush_chars,
1306 .write_room = capinc_tty_write_room,
1307 .chars_in_buffer = capinc_tty_chars_in_buffer,
1308 .ioctl = capinc_tty_ioctl,
1309 .set_termios = capinc_tty_set_termios,
1310 .throttle = capinc_tty_throttle,
1311 .unthrottle = capinc_tty_unthrottle,
1312 .stop = capinc_tty_stop,
1313 .start = capinc_tty_start,
1314 .hangup = capinc_tty_hangup,
1315 .break_ctl = capinc_tty_break_ctl,
1316 .flush_buffer = capinc_tty_flush_buffer,
1317 .set_ldisc = capinc_tty_set_ldisc,
1318 .send_xchar = capinc_tty_send_xchar,
1321 static int __init capinc_tty_init(void)
1323 struct tty_driver *drv;
1326 if (capi_ttyminors > CAPINC_MAX_PORTS)
1327 capi_ttyminors = CAPINC_MAX_PORTS;
1328 if (capi_ttyminors <= 0)
1329 capi_ttyminors = CAPINC_NR_PORTS;
1331 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1336 drv = alloc_tty_driver(capi_ttyminors);
1341 drv->owner = THIS_MODULE;
1342 drv->driver_name = "capi_nc";
1344 drv->major = capi_ttymajor;
1345 drv->minor_start = 0;
1346 drv->type = TTY_DRIVER_TYPE_SERIAL;
1347 drv->subtype = SERIAL_TYPE_NORMAL;
1348 drv->init_termios = tty_std_termios;
1349 drv->init_termios.c_iflag = ICRNL;
1350 drv->init_termios.c_oflag = OPOST | ONLCR;
1351 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1352 drv->init_termios.c_lflag = 0;
1354 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1355 TTY_DRIVER_DYNAMIC_DEV;
1356 tty_set_operations(drv, &capinc_ops);
1358 err = tty_register_driver(drv);
1360 put_tty_driver(drv);
1362 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1365 capinc_tty_driver = drv;
1369 static void __exit capinc_tty_exit(void)
1371 tty_unregister_driver(capinc_tty_driver);
1372 put_tty_driver(capinc_tty_driver);
1376 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1378 static inline int capinc_tty_init(void)
1383 static inline void capinc_tty_exit(void) { }
1385 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1387 /* -------- /proc functions ----------------------------------------- */
1390 * /proc/capi/capi20:
1391 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1393 static int capi20_proc_show(struct seq_file *m, void *v)
1395 struct capidev *cdev;
1396 struct list_head *l;
1398 mutex_lock(&capidev_list_lock);
1399 list_for_each(l, &capidev_list) {
1400 cdev = list_entry(l, struct capidev, list);
1401 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
1403 cdev->ap.nrecvctlpkt,
1404 cdev->ap.nrecvdatapkt,
1405 cdev->ap.nsentctlpkt,
1406 cdev->ap.nsentdatapkt);
1408 mutex_unlock(&capidev_list_lock);
1412 static int capi20_proc_open(struct inode *inode, struct file *file)
1414 return single_open(file, capi20_proc_show, NULL);
1417 static const struct file_operations capi20_proc_fops = {
1418 .owner = THIS_MODULE,
1419 .open = capi20_proc_open,
1421 .llseek = seq_lseek,
1422 .release = single_release,
1426 * /proc/capi/capi20ncci:
1429 static int capi20ncci_proc_show(struct seq_file *m, void *v)
1431 struct capidev *cdev;
1432 struct capincci *np;
1434 mutex_lock(&capidev_list_lock);
1435 list_for_each_entry(cdev, &capidev_list, list) {
1436 mutex_lock(&cdev->lock);
1437 list_for_each_entry(np, &cdev->nccis, list)
1438 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
1439 mutex_unlock(&cdev->lock);
1441 mutex_unlock(&capidev_list_lock);
1445 static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1447 return single_open(file, capi20ncci_proc_show, NULL);
1450 static const struct file_operations capi20ncci_proc_fops = {
1451 .owner = THIS_MODULE,
1452 .open = capi20ncci_proc_open,
1454 .llseek = seq_lseek,
1455 .release = single_release,
1458 static void __init proc_init(void)
1460 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1461 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
1464 static void __exit proc_exit(void)
1466 remove_proc_entry("capi/capi20", NULL);
1467 remove_proc_entry("capi/capi20ncci", NULL);
1470 /* -------- init function and module interface ---------------------- */
1473 static int __init capi_init(void)
1475 const char *compileinfo;
1478 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1479 if (major_ret < 0) {
1480 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1483 capi_class = class_create(THIS_MODULE, "capi");
1484 if (IS_ERR(capi_class)) {
1485 unregister_chrdev(capi_major, "capi20");
1486 return PTR_ERR(capi_class);
1489 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
1491 if (capinc_tty_init() < 0) {
1492 device_destroy(capi_class, MKDEV(capi_major, 0));
1493 class_destroy(capi_class);
1494 unregister_chrdev(capi_major, "capi20");
1500 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1501 compileinfo = " (middleware+capifs)";
1502 #elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
1503 compileinfo = " (no capifs)";
1505 compileinfo = " (no middleware)";
1507 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1508 capi_major, compileinfo);
1513 static void __exit capi_exit(void)
1517 device_destroy(capi_class, MKDEV(capi_major, 0));
1518 class_destroy(capi_class);
1519 unregister_chrdev(capi_major, "capi20");
1524 module_init(capi_init);
1525 module_exit(capi_exit);