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 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
27 #include <linux/tty.h>
29 #include <linux/netdevice.h>
30 #include <linux/ppp_defs.h>
31 #include <linux/if_ppp.h>
32 #endif /* CONFIG_PPP */
33 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
34 #include <linux/skbuff.h>
35 #include <linux/proc_fs.h>
36 #include <linux/seq_file.h>
37 #include <linux/poll.h>
38 #include <linux/capi.h>
39 #include <linux/kernelcapi.h>
40 #include <linux/init.h>
41 #include <linux/device.h>
42 #include <linux/moduleparam.h>
43 #include <linux/isdn/capiutil.h>
44 #include <linux/isdn/capicmd.h>
48 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
49 MODULE_AUTHOR("Carsten Paeth");
50 MODULE_LICENSE("GPL");
52 #undef _DEBUG_REFCOUNT /* alloc/free and open/close debug */
53 #undef _DEBUG_TTYFUNCS /* call to tty_driver */
54 #undef _DEBUG_DATAFLOW /* data flow */
56 /* -------- driver information -------------------------------------- */
58 static struct class *capi_class;
60 static int capi_major = 68; /* allocated */
61 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
62 #define CAPINC_NR_PORTS 32
63 #define CAPINC_MAX_PORTS 256
64 static int capi_ttymajor = 191;
65 static int capi_ttyminors = CAPINC_NR_PORTS;
66 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
68 module_param_named(major, capi_major, uint, 0);
69 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
70 module_param_named(ttymajor, capi_ttymajor, uint, 0);
71 module_param_named(ttyminors, capi_ttyminors, uint, 0);
72 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
74 /* -------- defines ------------------------------------------------- */
76 #define CAPINC_MAX_RECVQUEUE 10
77 #define CAPINC_MAX_SENDQUEUE 10
78 #define CAPI_MAX_BLKSIZE 2048
80 /* -------- data structures ----------------------------------------- */
84 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
87 struct datahandle_queue {
88 struct list_head list;
93 struct list_head list;
94 struct capincci *nccip;
96 struct dentry *capifs_dentry;
98 struct capi20_appl *ap;
103 struct tty_struct *tty;
106 struct sk_buff *ttyskb;
107 atomic_t ttyopencount;
109 struct sk_buff_head inqueue;
111 struct sk_buff_head outqueue;
115 struct list_head ackqueue;
119 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
121 /* FIXME: The following lock is a sledgehammer-workaround to a
122 * locking issue with the capiminor (and maybe other) data structure(s).
123 * Access to this data is done in a racy way and crashes the machine with
124 * a FritzCard DSL driver; sooner or later. This is a workaround
125 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
126 * The correct (and scalable) fix for the issue seems to require
127 * an API change to the drivers... . */
128 static DEFINE_SPINLOCK(workaround_lock);
131 struct capincci *next;
133 struct capidev *cdev;
134 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
135 struct capiminor *minorp;
136 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
140 struct list_head list;
141 struct capi20_appl ap;
145 struct sk_buff_head recvqueue;
146 wait_queue_head_t recvwait;
148 struct capincci *nccis;
150 struct mutex ncci_list_mtx;
153 /* -------- global variables ---------------------------------------- */
155 static DEFINE_RWLOCK(capidev_list_lock);
156 static LIST_HEAD(capidev_list);
158 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
159 static DEFINE_RWLOCK(capiminor_list_lock);
160 static LIST_HEAD(capiminor_list);
161 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
163 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
164 /* -------- datahandles --------------------------------------------- */
166 static int capincci_add_ack(struct capiminor *mp, u16 datahandle)
168 struct datahandle_queue *n;
171 n = kmalloc(sizeof(*n), GFP_ATOMIC);
173 printk(KERN_ERR "capi: alloc datahandle failed\n");
176 n->datahandle = datahandle;
177 INIT_LIST_HEAD(&n->list);
178 spin_lock_irqsave(&mp->ackqlock, flags);
179 list_add_tail(&n->list, &mp->ackqueue);
181 spin_unlock_irqrestore(&mp->ackqlock, flags);
185 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
187 struct datahandle_queue *p, *tmp;
190 spin_lock_irqsave(&mp->ackqlock, flags);
191 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
192 if (p->datahandle == datahandle) {
196 spin_unlock_irqrestore(&mp->ackqlock, flags);
200 spin_unlock_irqrestore(&mp->ackqlock, flags);
204 static void capiminor_del_all_ack(struct capiminor *mp)
206 struct datahandle_queue *p, *tmp;
209 spin_lock_irqsave(&mp->ackqlock, flags);
210 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
215 spin_unlock_irqrestore(&mp->ackqlock, flags);
219 /* -------- struct capiminor ---------------------------------------- */
221 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
223 struct capiminor *mp, *p;
224 unsigned int minor = 0;
227 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
229 printk(KERN_ERR "capi: can't alloc capiminor\n");
236 atomic_set(&mp->ttyopencount,0);
237 INIT_LIST_HEAD(&mp->ackqueue);
238 spin_lock_init(&mp->ackqlock);
240 skb_queue_head_init(&mp->inqueue);
241 skb_queue_head_init(&mp->outqueue);
243 /* Allocate the least unused minor number.
245 write_lock_irqsave(&capiminor_list_lock, flags);
246 if (list_empty(&capiminor_list))
247 list_add(&mp->list, &capiminor_list);
249 list_for_each_entry(p, &capiminor_list, list) {
250 if (p->minor > minor)
255 if (minor < capi_ttyminors) {
257 list_add(&mp->list, p->list.prev);
260 write_unlock_irqrestore(&capiminor_list_lock, flags);
262 if (!(minor < capi_ttyminors)) {
263 printk(KERN_NOTICE "capi: out of minors\n");
271 static void capiminor_free(struct capiminor *mp)
275 write_lock_irqsave(&capiminor_list_lock, flags);
277 write_unlock_irqrestore(&capiminor_list_lock, flags);
279 kfree_skb(mp->ttyskb);
281 skb_queue_purge(&mp->inqueue);
282 skb_queue_purge(&mp->outqueue);
283 capiminor_del_all_ack(mp);
287 static struct capiminor *capiminor_find(unsigned int minor)
290 struct capiminor *p = NULL;
292 read_lock(&capiminor_list_lock);
293 list_for_each(l, &capiminor_list) {
294 p = list_entry(l, struct capiminor, list);
295 if (p->minor == minor)
298 read_unlock(&capiminor_list_lock);
299 if (l == &capiminor_list)
304 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
306 /* -------- struct capincci ----------------------------------------- */
308 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
310 struct capincci *np, **pp;
311 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
312 struct capiminor *mp = NULL;
313 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
315 np = kzalloc(sizeof(*np), GFP_ATOMIC);
320 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
322 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
323 mp = np->minorp = capiminor_alloc(&cdev->ap, ncci);
326 #ifdef _DEBUG_REFCOUNT
327 printk(KERN_DEBUG "set mp->nccip\n");
330 capifs_new_ncci(mp->minor,
331 MKDEV(capi_ttymajor, mp->minor));
333 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
334 for (pp=&cdev->nccis; *pp; pp = &(*pp)->next)
340 static void capincci_free(struct capidev *cdev, u32 ncci)
342 struct capincci *np, **pp;
343 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
344 struct capiminor *mp;
345 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
350 if (ncci == 0xffffffff || np->ncci == ncci) {
352 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
353 if ((mp = np->minorp) != NULL) {
354 capifs_free_ncci(mp->capifs_dentry);
357 #ifdef _DEBUG_REFCOUNT
358 printk(KERN_DEBUG "reset mp->nccip\n");
365 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
367 if (*pp == NULL) return;
374 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
378 for (p=cdev->nccis; p ; p = p->next) {
385 /* -------- struct capidev ------------------------------------------ */
387 static struct capidev *capidev_alloc(void)
389 struct capidev *cdev;
392 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
396 mutex_init(&cdev->ncci_list_mtx);
397 skb_queue_head_init(&cdev->recvqueue);
398 init_waitqueue_head(&cdev->recvwait);
399 write_lock_irqsave(&capidev_list_lock, flags);
400 list_add_tail(&cdev->list, &capidev_list);
401 write_unlock_irqrestore(&capidev_list_lock, flags);
405 static void capidev_free(struct capidev *cdev)
409 if (cdev->ap.applid) {
410 capi20_release(&cdev->ap);
413 skb_queue_purge(&cdev->recvqueue);
415 mutex_lock(&cdev->ncci_list_mtx);
416 capincci_free(cdev, 0xffffffff);
417 mutex_unlock(&cdev->ncci_list_mtx);
419 write_lock_irqsave(&capidev_list_lock, flags);
420 list_del(&cdev->list);
421 write_unlock_irqrestore(&capidev_list_lock, flags);
425 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
426 /* -------- handle data queue --------------------------------------- */
428 static struct sk_buff *
429 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
431 struct sk_buff *nskb;
432 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
434 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
435 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
436 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
437 capimsg_setu16(s, 2, mp->ap->applid);
438 capimsg_setu8 (s, 4, CAPI_DATA_B3);
439 capimsg_setu8 (s, 5, CAPI_RESP);
440 capimsg_setu16(s, 6, mp->msgid++);
441 capimsg_setu32(s, 8, mp->ncci);
442 capimsg_setu16(s, 12, datahandle);
447 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
449 struct sk_buff *nskb;
451 u16 errcode, datahandle;
452 struct tty_ldisc *ld;
454 datalen = skb->len - CAPIMSG_LEN(skb->data);
457 #ifdef _DEBUG_DATAFLOW
458 printk(KERN_DEBUG "capi: currently no receiver\n");
463 ld = tty_ldisc_ref(mp->tty);
466 if (ld->ops->receive_buf == NULL) {
467 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
468 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
473 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
474 printk(KERN_DEBUG "capi: recv tty throttled\n");
478 if (mp->tty->receive_room < datalen) {
479 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
480 printk(KERN_DEBUG "capi: no room in tty\n");
484 if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
485 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
488 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
489 errcode = capi20_put_message(mp->ap, nskb);
490 if (errcode != CAPI_NOERROR) {
491 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
496 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
497 #ifdef _DEBUG_DATAFLOW
498 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
499 datahandle, skb->len);
501 ld->ops->receive_buf(mp->tty, skb->data, NULL, skb->len);
510 static void handle_minor_recv(struct capiminor *mp)
513 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
514 unsigned int len = skb->len;
516 if (handle_recv_skb(mp, skb) < 0) {
517 skb_queue_head(&mp->inqueue, skb);
524 static int handle_minor_send(struct capiminor *mp)
532 if (mp->tty && mp->ttyoutstop) {
533 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
534 printk(KERN_DEBUG "capi: send: tty stopped\n");
539 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
540 datahandle = mp->datahandle;
542 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
543 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
544 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
545 capimsg_setu16(skb->data, 2, mp->ap->applid);
546 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
547 capimsg_setu8 (skb->data, 5, CAPI_REQ);
548 capimsg_setu16(skb->data, 6, mp->msgid++);
549 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
550 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
551 capimsg_setu16(skb->data, 16, len); /* Data length */
552 capimsg_setu16(skb->data, 18, datahandle);
553 capimsg_setu16(skb->data, 20, 0); /* Flags */
555 if (capincci_add_ack(mp, datahandle) < 0) {
556 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
557 skb_queue_head(&mp->outqueue, skb);
560 errcode = capi20_put_message(mp->ap, skb);
561 if (errcode == CAPI_NOERROR) {
565 #ifdef _DEBUG_DATAFLOW
566 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
571 capiminor_del_ack(mp, datahandle);
573 if (errcode == CAPI_SENDQUEUEFULL) {
574 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
575 skb_queue_head(&mp->outqueue, skb);
579 /* ups, drop packet */
580 printk(KERN_ERR "capi: put_message = %x\n", errcode);
587 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
588 /* -------- function called by lower level -------------------------- */
590 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
592 struct capidev *cdev = ap->private;
593 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
594 struct capiminor *mp;
596 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
601 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
602 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
603 if ((info & 0xff00) == 0) {
604 mutex_lock(&cdev->ncci_list_mtx);
605 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
606 mutex_unlock(&cdev->ncci_list_mtx);
609 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND) {
610 mutex_lock(&cdev->ncci_list_mtx);
611 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
612 mutex_unlock(&cdev->ncci_list_mtx);
614 spin_lock_irqsave(&workaround_lock, flags);
615 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
616 skb_queue_tail(&cdev->recvqueue, skb);
617 wake_up_interruptible(&cdev->recvwait);
618 spin_unlock_irqrestore(&workaround_lock, flags);
621 ncci = CAPIMSG_CONTROL(skb->data);
622 for (np = cdev->nccis; np && np->ncci != ncci; np = np->next)
625 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
626 skb_queue_tail(&cdev->recvqueue, skb);
627 wake_up_interruptible(&cdev->recvwait);
628 spin_unlock_irqrestore(&workaround_lock, flags);
631 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
632 skb_queue_tail(&cdev->recvqueue, skb);
633 wake_up_interruptible(&cdev->recvwait);
634 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
637 skb_queue_tail(&cdev->recvqueue, skb);
638 wake_up_interruptible(&cdev->recvwait);
639 spin_unlock_irqrestore(&workaround_lock, flags);
644 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
646 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
647 #ifdef _DEBUG_DATAFLOW
648 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
649 datahandle, skb->len-CAPIMSG_LEN(skb->data));
651 skb_queue_tail(&mp->inqueue, skb);
652 mp->inbytes += skb->len;
653 handle_minor_recv(mp);
655 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
657 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
658 #ifdef _DEBUG_DATAFLOW
659 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
661 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
664 (void)capiminor_del_ack(mp, datahandle);
667 (void)handle_minor_send(mp);
670 /* ups, let capi application handle it :-) */
671 skb_queue_tail(&cdev->recvqueue, skb);
672 wake_up_interruptible(&cdev->recvwait);
674 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
675 spin_unlock_irqrestore(&workaround_lock, flags);
678 /* -------- file_operations for capidev ----------------------------- */
681 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
683 struct capidev *cdev = (struct capidev *)file->private_data;
687 if (!cdev->ap.applid)
690 if ((skb = skb_dequeue(&cdev->recvqueue)) == NULL) {
692 if (file->f_flags & O_NONBLOCK)
696 interruptible_sleep_on(&cdev->recvwait);
697 if ((skb = skb_dequeue(&cdev->recvqueue)) != NULL)
699 if (signal_pending(current))
703 return -ERESTARTNOHAND;
705 if (skb->len > count) {
706 skb_queue_head(&cdev->recvqueue, skb);
709 if (copy_to_user(buf, skb->data, skb->len)) {
710 skb_queue_head(&cdev->recvqueue, skb);
721 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
723 struct capidev *cdev = (struct capidev *)file->private_data;
727 if (!cdev->ap.applid)
730 skb = alloc_skb(count, GFP_USER);
734 if (copy_from_user(skb_put(skb, count), buf, count)) {
738 mlen = CAPIMSG_LEN(skb->data);
739 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
740 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
750 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
752 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
753 mutex_lock(&cdev->ncci_list_mtx);
754 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
755 mutex_unlock(&cdev->ncci_list_mtx);
758 cdev->errcode = capi20_put_message(&cdev->ap, skb);
768 capi_poll(struct file *file, poll_table * wait)
770 struct capidev *cdev = (struct capidev *)file->private_data;
771 unsigned int mask = 0;
773 if (!cdev->ap.applid)
776 poll_wait(file, &(cdev->recvwait), wait);
777 mask = POLLOUT | POLLWRNORM;
778 if (!skb_queue_empty(&cdev->recvqueue))
779 mask |= POLLIN | POLLRDNORM;
784 capi_ioctl(struct inode *inode, struct file *file,
785 unsigned int cmd, unsigned long arg)
787 struct capidev *cdev = file->private_data;
788 struct capi20_appl *ap = &cdev->ap;
789 capi_ioctl_struct data;
790 int retval = -EINVAL;
791 void __user *argp = (void __user *)arg;
799 if (copy_from_user(&cdev->ap.rparam, argp,
800 sizeof(struct capi_register_params)))
803 cdev->ap.private = cdev;
804 cdev->ap.recv_message = capi_recv_message;
805 cdev->errcode = capi20_register(ap);
811 return (int)ap->applid;
813 case CAPI_GET_VERSION:
815 if (copy_from_user(&data.contr, argp,
818 cdev->errcode = capi20_get_version(data.contr, &data.version);
821 if (copy_to_user(argp, &data.version,
822 sizeof(data.version)))
827 case CAPI_GET_SERIAL:
829 if (copy_from_user(&data.contr, argp,
832 cdev->errcode = capi20_get_serial (data.contr, data.serial);
835 if (copy_to_user(argp, data.serial,
836 sizeof(data.serial)))
840 case CAPI_GET_PROFILE:
842 if (copy_from_user(&data.contr, argp,
846 if (data.contr == 0) {
847 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
851 retval = copy_to_user(argp,
852 &data.profile.ncontroller,
853 sizeof(data.profile.ncontroller));
856 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
860 retval = copy_to_user(argp, &data.profile,
861 sizeof(data.profile));
868 case CAPI_GET_MANUFACTURER:
870 if (copy_from_user(&data.contr, argp,
873 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
877 if (copy_to_user(argp, data.manufacturer,
878 sizeof(data.manufacturer)))
883 case CAPI_GET_ERRCODE:
884 data.errcode = cdev->errcode;
885 cdev->errcode = CAPI_NOERROR;
887 if (copy_to_user(argp, &data.errcode,
888 sizeof(data.errcode)))
894 if (capi20_isinstalled() == CAPI_NOERROR)
898 case CAPI_MANUFACTURER_CMD:
900 struct capi_manufacturer_cmd mcmd;
901 if (!capable(CAP_SYS_ADMIN))
903 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
905 return capi20_manufacturer(mcmd.cmd, mcmd.data);
913 if (copy_from_user(&userflags, argp,
916 if (cmd == CAPI_SET_FLAGS)
917 cdev->userflags |= userflags;
919 cdev->userflags &= ~userflags;
924 if (copy_to_user(argp, &cdev->userflags,
925 sizeof(cdev->userflags)))
929 case CAPI_NCCI_OPENCOUNT:
931 struct capincci *nccip;
932 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
933 struct capiminor *mp;
934 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
937 if (copy_from_user(&ncci, argp, sizeof(ncci)))
940 mutex_lock(&cdev->ncci_list_mtx);
941 if ((nccip = capincci_find(cdev, (u32) ncci)) == NULL) {
942 mutex_unlock(&cdev->ncci_list_mtx);
945 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
946 if ((mp = nccip->minorp) != NULL) {
947 count += atomic_read(&mp->ttyopencount);
949 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
950 mutex_unlock(&cdev->ncci_list_mtx);
955 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
956 case CAPI_NCCI_GETUNIT:
958 struct capincci *nccip;
959 struct capiminor *mp;
962 if (copy_from_user(&ncci, argp,
965 mutex_lock(&cdev->ncci_list_mtx);
966 nccip = capincci_find(cdev, (u32) ncci);
967 if (!nccip || (mp = nccip->minorp) == NULL) {
968 mutex_unlock(&cdev->ncci_list_mtx);
972 mutex_unlock(&cdev->ncci_list_mtx);
976 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
982 capi_open(struct inode *inode, struct file *file)
987 if (file->private_data)
989 else if ((file->private_data = capidev_alloc()) == NULL)
992 ret = nonseekable_open(inode, file);
998 capi_release(struct inode *inode, struct file *file)
1000 struct capidev *cdev = (struct capidev *)file->private_data;
1003 file->private_data = NULL;
1008 static const struct file_operations capi_fops =
1010 .owner = THIS_MODULE,
1011 .llseek = no_llseek,
1013 .write = capi_write,
1015 .ioctl = capi_ioctl,
1017 .release = capi_release,
1020 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1021 /* -------- tty_operations for capincci ----------------------------- */
1023 static int capinc_tty_open(struct tty_struct * tty, struct file * file)
1025 struct capiminor *mp;
1026 unsigned long flags;
1028 if ((mp = capiminor_find(iminor(file->f_path.dentry->d_inode))) == NULL)
1030 if (mp->nccip == NULL)
1033 tty->driver_data = (void *)mp;
1035 spin_lock_irqsave(&workaround_lock, flags);
1036 if (atomic_read(&mp->ttyopencount) == 0)
1038 atomic_inc(&mp->ttyopencount);
1039 #ifdef _DEBUG_REFCOUNT
1040 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1042 handle_minor_recv(mp);
1043 spin_unlock_irqrestore(&workaround_lock, flags);
1047 static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1049 struct capiminor *mp;
1051 mp = (struct capiminor *)tty->driver_data;
1053 if (atomic_dec_and_test(&mp->ttyopencount)) {
1054 #ifdef _DEBUG_REFCOUNT
1055 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1057 tty->driver_data = NULL;
1060 #ifdef _DEBUG_REFCOUNT
1061 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1063 if (mp->nccip == NULL)
1067 #ifdef _DEBUG_REFCOUNT
1068 printk(KERN_DEBUG "capinc_tty_close\n");
1072 static int capinc_tty_write(struct tty_struct * tty,
1073 const unsigned char *buf, int count)
1075 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1076 struct sk_buff *skb;
1077 unsigned long flags;
1079 #ifdef _DEBUG_TTYFUNCS
1080 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1083 if (!mp || !mp->nccip) {
1084 #ifdef _DEBUG_TTYFUNCS
1085 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1090 spin_lock_irqsave(&workaround_lock, flags);
1094 skb_queue_tail(&mp->outqueue, skb);
1095 mp->outbytes += skb->len;
1098 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1100 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1101 spin_unlock_irqrestore(&workaround_lock, flags);
1105 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1106 memcpy(skb_put(skb, count), buf, count);
1108 skb_queue_tail(&mp->outqueue, skb);
1109 mp->outbytes += skb->len;
1110 (void)handle_minor_send(mp);
1111 (void)handle_minor_recv(mp);
1112 spin_unlock_irqrestore(&workaround_lock, flags);
1116 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1118 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1119 struct sk_buff *skb;
1120 unsigned long flags;
1123 #ifdef _DEBUG_TTYFUNCS
1124 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1127 if (!mp || !mp->nccip) {
1128 #ifdef _DEBUG_TTYFUNCS
1129 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1134 spin_lock_irqsave(&workaround_lock, flags);
1137 if (skb_tailroom(skb) > 0) {
1138 *(skb_put(skb, 1)) = ch;
1139 spin_unlock_irqrestore(&workaround_lock, flags);
1143 skb_queue_tail(&mp->outqueue, skb);
1144 mp->outbytes += skb->len;
1145 (void)handle_minor_send(mp);
1147 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1149 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1150 *(skb_put(skb, 1)) = ch;
1153 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1156 spin_unlock_irqrestore(&workaround_lock, flags);
1160 static void capinc_tty_flush_chars(struct tty_struct *tty)
1162 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1163 struct sk_buff *skb;
1164 unsigned long flags;
1166 #ifdef _DEBUG_TTYFUNCS
1167 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1170 if (!mp || !mp->nccip) {
1171 #ifdef _DEBUG_TTYFUNCS
1172 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1177 spin_lock_irqsave(&workaround_lock, flags);
1181 skb_queue_tail(&mp->outqueue, skb);
1182 mp->outbytes += skb->len;
1183 (void)handle_minor_send(mp);
1185 (void)handle_minor_recv(mp);
1186 spin_unlock_irqrestore(&workaround_lock, flags);
1189 static int capinc_tty_write_room(struct tty_struct *tty)
1191 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1193 if (!mp || !mp->nccip) {
1194 #ifdef _DEBUG_TTYFUNCS
1195 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1199 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1200 room *= CAPI_MAX_BLKSIZE;
1201 #ifdef _DEBUG_TTYFUNCS
1202 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1207 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1209 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1210 if (!mp || !mp->nccip) {
1211 #ifdef _DEBUG_TTYFUNCS
1212 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1216 #ifdef _DEBUG_TTYFUNCS
1217 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1218 mp->outbytes, mp->nack,
1219 skb_queue_len(&mp->outqueue),
1220 skb_queue_len(&mp->inqueue));
1222 return mp->outbytes;
1225 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1226 unsigned int cmd, unsigned long arg)
1231 error = n_tty_ioctl_helper(tty, file, cmd, arg);
1237 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
1239 #ifdef _DEBUG_TTYFUNCS
1240 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1244 static void capinc_tty_throttle(struct tty_struct * tty)
1246 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1247 #ifdef _DEBUG_TTYFUNCS
1248 printk(KERN_DEBUG "capinc_tty_throttle\n");
1254 static void capinc_tty_unthrottle(struct tty_struct * tty)
1256 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1257 unsigned long flags;
1258 #ifdef _DEBUG_TTYFUNCS
1259 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1262 spin_lock_irqsave(&workaround_lock, flags);
1264 handle_minor_recv(mp);
1265 spin_unlock_irqrestore(&workaround_lock, flags);
1269 static void capinc_tty_stop(struct tty_struct *tty)
1271 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1272 #ifdef _DEBUG_TTYFUNCS
1273 printk(KERN_DEBUG "capinc_tty_stop\n");
1280 static void capinc_tty_start(struct tty_struct *tty)
1282 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1283 unsigned long flags;
1284 #ifdef _DEBUG_TTYFUNCS
1285 printk(KERN_DEBUG "capinc_tty_start\n");
1288 spin_lock_irqsave(&workaround_lock, flags);
1290 (void)handle_minor_send(mp);
1291 spin_unlock_irqrestore(&workaround_lock, flags);
1295 static void capinc_tty_hangup(struct tty_struct *tty)
1297 #ifdef _DEBUG_TTYFUNCS
1298 printk(KERN_DEBUG "capinc_tty_hangup\n");
1302 static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1304 #ifdef _DEBUG_TTYFUNCS
1305 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1310 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1312 #ifdef _DEBUG_TTYFUNCS
1313 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1317 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1319 #ifdef _DEBUG_TTYFUNCS
1320 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1324 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1326 #ifdef _DEBUG_TTYFUNCS
1327 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1331 static struct tty_driver *capinc_tty_driver;
1333 static const struct tty_operations capinc_ops = {
1334 .open = capinc_tty_open,
1335 .close = capinc_tty_close,
1336 .write = capinc_tty_write,
1337 .put_char = capinc_tty_put_char,
1338 .flush_chars = capinc_tty_flush_chars,
1339 .write_room = capinc_tty_write_room,
1340 .chars_in_buffer = capinc_tty_chars_in_buffer,
1341 .ioctl = capinc_tty_ioctl,
1342 .set_termios = capinc_tty_set_termios,
1343 .throttle = capinc_tty_throttle,
1344 .unthrottle = capinc_tty_unthrottle,
1345 .stop = capinc_tty_stop,
1346 .start = capinc_tty_start,
1347 .hangup = capinc_tty_hangup,
1348 .break_ctl = capinc_tty_break_ctl,
1349 .flush_buffer = capinc_tty_flush_buffer,
1350 .set_ldisc = capinc_tty_set_ldisc,
1351 .send_xchar = capinc_tty_send_xchar,
1354 static int capinc_tty_init(void)
1356 struct tty_driver *drv;
1358 if (capi_ttyminors > CAPINC_MAX_PORTS)
1359 capi_ttyminors = CAPINC_MAX_PORTS;
1360 if (capi_ttyminors <= 0)
1361 capi_ttyminors = CAPINC_NR_PORTS;
1363 drv = alloc_tty_driver(capi_ttyminors);
1367 drv->owner = THIS_MODULE;
1368 drv->driver_name = "capi_nc";
1370 drv->major = capi_ttymajor;
1371 drv->minor_start = 0;
1372 drv->type = TTY_DRIVER_TYPE_SERIAL;
1373 drv->subtype = SERIAL_TYPE_NORMAL;
1374 drv->init_termios = tty_std_termios;
1375 drv->init_termios.c_iflag = ICRNL;
1376 drv->init_termios.c_oflag = OPOST | ONLCR;
1377 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1378 drv->init_termios.c_lflag = 0;
1379 drv->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_RESET_TERMIOS;
1380 tty_set_operations(drv, &capinc_ops);
1381 if (tty_register_driver(drv)) {
1382 put_tty_driver(drv);
1383 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1386 capinc_tty_driver = drv;
1390 static void capinc_tty_exit(void)
1392 struct tty_driver *drv = capinc_tty_driver;
1394 if ((retval = tty_unregister_driver(drv)))
1395 printk(KERN_ERR "capi: failed to unregister capi_nc driver (%d)\n", retval);
1396 put_tty_driver(drv);
1399 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1401 /* -------- /proc functions ----------------------------------------- */
1404 * /proc/capi/capi20:
1405 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1407 static int capi20_proc_show(struct seq_file *m, void *v)
1409 struct capidev *cdev;
1410 struct list_head *l;
1412 read_lock(&capidev_list_lock);
1413 list_for_each(l, &capidev_list) {
1414 cdev = list_entry(l, struct capidev, list);
1415 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
1417 cdev->ap.nrecvctlpkt,
1418 cdev->ap.nrecvdatapkt,
1419 cdev->ap.nsentctlpkt,
1420 cdev->ap.nsentdatapkt);
1422 read_unlock(&capidev_list_lock);
1426 static int capi20_proc_open(struct inode *inode, struct file *file)
1428 return single_open(file, capi20_proc_show, NULL);
1431 static const struct file_operations capi20_proc_fops = {
1432 .owner = THIS_MODULE,
1433 .open = capi20_proc_open,
1435 .llseek = seq_lseek,
1436 .release = single_release,
1440 * /proc/capi/capi20ncci:
1443 static int capi20ncci_proc_show(struct seq_file *m, void *v)
1445 struct capidev *cdev;
1446 struct capincci *np;
1447 struct list_head *l;
1449 read_lock(&capidev_list_lock);
1450 list_for_each(l, &capidev_list) {
1451 cdev = list_entry(l, struct capidev, list);
1452 for (np=cdev->nccis; np; np = np->next) {
1453 seq_printf(m, "%d 0x%x\n",
1458 read_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 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1509 if (capinc_tty_init() < 0) {
1510 device_destroy(capi_class, MKDEV(capi_major, 0));
1511 class_destroy(capi_class);
1512 unregister_chrdev(capi_major, "capi20");
1515 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1519 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1520 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1521 compileinfo = " (middleware+capifs)";
1523 compileinfo = " (no capifs)";
1526 compileinfo = " (no middleware)";
1528 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1529 capi_major, compileinfo);
1534 static void __exit capi_exit(void)
1538 device_destroy(capi_class, MKDEV(capi_major, 0));
1539 class_destroy(capi_class);
1540 unregister_chrdev(capi_major, "capi20");
1542 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1547 module_init(capi_init);
1548 module_exit(capi_exit);