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>
22 #include <linux/timer.h>
23 #include <linux/wait.h>
24 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
25 #include <linux/tty.h>
27 #include <linux/netdevice.h>
28 #include <linux/ppp_defs.h>
29 #include <linux/if_ppp.h>
30 #endif /* CONFIG_PPP */
31 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
32 #include <linux/skbuff.h>
33 #include <linux/proc_fs.h>
34 #include <linux/poll.h>
35 #include <linux/capi.h>
36 #include <linux/kernelcapi.h>
37 #include <linux/init.h>
38 #include <linux/device.h>
39 #include <linux/moduleparam.h>
40 #include <linux/isdn/capiutil.h>
41 #include <linux/isdn/capicmd.h>
42 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
46 static char *revision = "$Revision: 1.1.2.7 $";
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;
97 struct capi20_appl *ap;
102 struct tty_struct *tty;
105 struct sk_buff *ttyskb;
106 atomic_t ttyopencount;
108 struct sk_buff_head inqueue;
110 struct sk_buff_head outqueue;
114 struct list_head ackqueue;
118 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
120 /* FIXME: The following lock is a sledgehammer-workaround to a
121 * locking issue with the capiminor (and maybe other) data structure(s).
122 * Access to this data is done in a racy way and crashes the machine with
123 * a FritzCard DSL driver; sooner or later. This is a workaround
124 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
125 * The correct (and scalable) fix for the issue seems to require
126 * an API change to the drivers... . */
127 static DEFINE_SPINLOCK(workaround_lock);
130 struct capincci *next;
132 struct capidev *cdev;
133 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
134 struct capiminor *minorp;
135 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
139 struct list_head list;
140 struct capi20_appl ap;
144 struct sk_buff_head recvqueue;
145 wait_queue_head_t recvwait;
147 struct capincci *nccis;
149 struct semaphore ncci_list_sem;
152 /* -------- global variables ---------------------------------------- */
154 static DEFINE_RWLOCK(capidev_list_lock);
155 static LIST_HEAD(capidev_list);
157 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
158 static DEFINE_RWLOCK(capiminor_list_lock);
159 static LIST_HEAD(capiminor_list);
160 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
162 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
163 /* -------- datahandles --------------------------------------------- */
165 static int capincci_add_ack(struct capiminor *mp, u16 datahandle)
167 struct datahandle_queue *n;
170 n = kmalloc(sizeof(*n), GFP_ATOMIC);
172 printk(KERN_ERR "capi: alloc datahandle failed\n");
175 n->datahandle = datahandle;
176 INIT_LIST_HEAD(&n->list);
177 spin_lock_irqsave(&mp->ackqlock, flags);
178 list_add_tail(&n->list, &mp->ackqueue);
180 spin_unlock_irqrestore(&mp->ackqlock, flags);
184 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
186 struct datahandle_queue *p, *tmp;
189 spin_lock_irqsave(&mp->ackqlock, flags);
190 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
191 if (p->datahandle == datahandle) {
195 spin_unlock_irqrestore(&mp->ackqlock, flags);
199 spin_unlock_irqrestore(&mp->ackqlock, flags);
203 static void capiminor_del_all_ack(struct capiminor *mp)
205 struct datahandle_queue *p, *tmp;
208 spin_lock_irqsave(&mp->ackqlock, flags);
209 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
214 spin_unlock_irqrestore(&mp->ackqlock, flags);
218 /* -------- struct capiminor ---------------------------------------- */
220 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
222 struct capiminor *mp, *p;
223 unsigned int minor = 0;
226 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
228 printk(KERN_ERR "capi: can't alloc capiminor\n");
235 atomic_set(&mp->ttyopencount,0);
236 INIT_LIST_HEAD(&mp->ackqueue);
237 spin_lock_init(&mp->ackqlock);
239 skb_queue_head_init(&mp->inqueue);
240 skb_queue_head_init(&mp->outqueue);
242 /* Allocate the least unused minor number.
244 write_lock_irqsave(&capiminor_list_lock, flags);
245 if (list_empty(&capiminor_list))
246 list_add(&mp->list, &capiminor_list);
248 list_for_each_entry(p, &capiminor_list, list) {
249 if (p->minor > minor)
254 if (minor < capi_ttyminors) {
256 list_add(&mp->list, p->list.prev);
259 write_unlock_irqrestore(&capiminor_list_lock, flags);
261 if (!(minor < capi_ttyminors)) {
262 printk(KERN_NOTICE "capi: out of minors\n");
270 static void capiminor_free(struct capiminor *mp)
274 write_lock_irqsave(&capiminor_list_lock, flags);
276 write_unlock_irqrestore(&capiminor_list_lock, flags);
278 if (mp->ttyskb) kfree_skb(mp->ttyskb);
280 skb_queue_purge(&mp->inqueue);
281 skb_queue_purge(&mp->outqueue);
282 capiminor_del_all_ack(mp);
286 static struct capiminor *capiminor_find(unsigned int minor)
289 struct capiminor *p = NULL;
291 read_lock(&capiminor_list_lock);
292 list_for_each(l, &capiminor_list) {
293 p = list_entry(l, struct capiminor, list);
294 if (p->minor == minor)
297 read_unlock(&capiminor_list_lock);
298 if (l == &capiminor_list)
303 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
305 /* -------- struct capincci ----------------------------------------- */
307 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
309 struct capincci *np, **pp;
310 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
311 struct capiminor *mp = NULL;
312 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
314 np = kzalloc(sizeof(*np), GFP_ATOMIC);
319 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
321 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
322 mp = np->minorp = capiminor_alloc(&cdev->ap, ncci);
325 #ifdef _DEBUG_REFCOUNT
326 printk(KERN_DEBUG "set mp->nccip\n");
328 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
329 capifs_new_ncci(mp->minor, MKDEV(capi_ttymajor, mp->minor));
332 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
333 for (pp=&cdev->nccis; *pp; pp = &(*pp)->next)
339 static void capincci_free(struct capidev *cdev, u32 ncci)
341 struct capincci *np, **pp;
342 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
343 struct capiminor *mp;
344 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
349 if (ncci == 0xffffffff || np->ncci == ncci) {
351 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
352 if ((mp = np->minorp) != 0) {
353 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
354 capifs_free_ncci(mp->minor);
358 #ifdef _DEBUG_REFCOUNT
359 printk(KERN_DEBUG "reset mp->nccip\n");
366 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
368 if (*pp == 0) return;
375 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
379 for (p=cdev->nccis; p ; p = p->next) {
386 /* -------- struct capidev ------------------------------------------ */
388 static struct capidev *capidev_alloc(void)
390 struct capidev *cdev;
393 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
397 init_MUTEX(&cdev->ncci_list_sem);
398 skb_queue_head_init(&cdev->recvqueue);
399 init_waitqueue_head(&cdev->recvwait);
400 write_lock_irqsave(&capidev_list_lock, flags);
401 list_add_tail(&cdev->list, &capidev_list);
402 write_unlock_irqrestore(&capidev_list_lock, flags);
406 static void capidev_free(struct capidev *cdev)
410 if (cdev->ap.applid) {
411 capi20_release(&cdev->ap);
414 skb_queue_purge(&cdev->recvqueue);
416 down(&cdev->ncci_list_sem);
417 capincci_free(cdev, 0xffffffff);
418 up(&cdev->ncci_list_sem);
420 write_lock_irqsave(&capidev_list_lock, flags);
421 list_del(&cdev->list);
422 write_unlock_irqrestore(&capidev_list_lock, flags);
426 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
427 /* -------- handle data queue --------------------------------------- */
429 static struct sk_buff *
430 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
432 struct sk_buff *nskb;
433 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
435 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
436 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
437 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
438 capimsg_setu16(s, 2, mp->ap->applid);
439 capimsg_setu8 (s, 4, CAPI_DATA_B3);
440 capimsg_setu8 (s, 5, CAPI_RESP);
441 capimsg_setu16(s, 6, mp->msgid++);
442 capimsg_setu32(s, 8, mp->ncci);
443 capimsg_setu16(s, 12, datahandle);
448 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
450 struct sk_buff *nskb;
452 u16 errcode, datahandle;
453 struct tty_ldisc *ld;
455 datalen = skb->len - CAPIMSG_LEN(skb->data);
458 #ifdef _DEBUG_DATAFLOW
459 printk(KERN_DEBUG "capi: currently no receiver\n");
464 ld = tty_ldisc_ref(mp->tty);
467 if (ld->receive_buf == NULL) {
468 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
469 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
474 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
475 printk(KERN_DEBUG "capi: recv tty throttled\n");
479 if (mp->tty->receive_room < datalen) {
480 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
481 printk(KERN_DEBUG "capi: no room in tty\n");
485 if ((nskb = gen_data_b3_resp_for(mp, skb)) == 0) {
486 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
489 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
490 errcode = capi20_put_message(mp->ap, nskb);
491 if (errcode != CAPI_NOERROR) {
492 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
497 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
498 #ifdef _DEBUG_DATAFLOW
499 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
500 datahandle, skb->len);
502 ld->receive_buf(mp->tty, skb->data, NULL, skb->len);
511 static void handle_minor_recv(struct capiminor *mp)
514 while ((skb = skb_dequeue(&mp->inqueue)) != 0) {
515 unsigned int len = skb->len;
517 if (handle_recv_skb(mp, skb) < 0) {
518 skb_queue_head(&mp->inqueue, skb);
525 static int handle_minor_send(struct capiminor *mp)
533 if (mp->tty && mp->ttyoutstop) {
534 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
535 printk(KERN_DEBUG "capi: send: tty stopped\n");
540 while ((skb = skb_dequeue(&mp->outqueue)) != 0) {
541 datahandle = mp->datahandle;
543 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
544 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
545 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
546 capimsg_setu16(skb->data, 2, mp->ap->applid);
547 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
548 capimsg_setu8 (skb->data, 5, CAPI_REQ);
549 capimsg_setu16(skb->data, 6, mp->msgid++);
550 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
551 capimsg_setu32(skb->data, 12, (u32) skb->data); /* Data32 */
552 capimsg_setu16(skb->data, 16, len); /* Data length */
553 capimsg_setu16(skb->data, 18, datahandle);
554 capimsg_setu16(skb->data, 20, 0); /* Flags */
556 if (capincci_add_ack(mp, datahandle) < 0) {
557 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
558 skb_queue_head(&mp->outqueue, skb);
561 errcode = capi20_put_message(mp->ap, skb);
562 if (errcode == CAPI_NOERROR) {
566 #ifdef _DEBUG_DATAFLOW
567 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
572 capiminor_del_ack(mp, datahandle);
574 if (errcode == CAPI_SENDQUEUEFULL) {
575 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
576 skb_queue_head(&mp->outqueue, skb);
580 /* ups, drop packet */
581 printk(KERN_ERR "capi: put_message = %x\n", errcode);
588 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
589 /* -------- function called by lower level -------------------------- */
591 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
593 struct capidev *cdev = ap->private;
594 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
595 struct capiminor *mp;
597 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
602 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
603 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
605 down(&cdev->ncci_list_sem);
606 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
607 up(&cdev->ncci_list_sem);
610 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND) {
611 down(&cdev->ncci_list_sem);
612 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
613 up(&cdev->ncci_list_sem);
615 spin_lock_irqsave(&workaround_lock, flags);
616 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
617 skb_queue_tail(&cdev->recvqueue, skb);
618 wake_up_interruptible(&cdev->recvwait);
619 spin_unlock_irqrestore(&workaround_lock, flags);
622 ncci = CAPIMSG_CONTROL(skb->data);
623 for (np = cdev->nccis; np && np->ncci != ncci; np = np->next)
626 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
627 skb_queue_tail(&cdev->recvqueue, skb);
628 wake_up_interruptible(&cdev->recvwait);
629 spin_unlock_irqrestore(&workaround_lock, flags);
632 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
633 skb_queue_tail(&cdev->recvqueue, skb);
634 wake_up_interruptible(&cdev->recvwait);
635 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
638 skb_queue_tail(&cdev->recvqueue, skb);
639 wake_up_interruptible(&cdev->recvwait);
640 spin_unlock_irqrestore(&workaround_lock, flags);
645 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
647 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
648 #ifdef _DEBUG_DATAFLOW
649 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
650 datahandle, skb->len-CAPIMSG_LEN(skb->data));
652 skb_queue_tail(&mp->inqueue, skb);
653 mp->inbytes += skb->len;
654 handle_minor_recv(mp);
656 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
658 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
659 #ifdef _DEBUG_DATAFLOW
660 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
662 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
665 (void)capiminor_del_ack(mp, datahandle);
668 (void)handle_minor_send(mp);
671 /* ups, let capi application handle it :-) */
672 skb_queue_tail(&cdev->recvqueue, skb);
673 wake_up_interruptible(&cdev->recvwait);
675 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
676 spin_unlock_irqrestore(&workaround_lock, flags);
679 /* -------- file_operations for capidev ----------------------------- */
682 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
684 struct capidev *cdev = (struct capidev *)file->private_data;
688 if (!cdev->ap.applid)
691 if ((skb = skb_dequeue(&cdev->recvqueue)) == 0) {
693 if (file->f_flags & O_NONBLOCK)
697 interruptible_sleep_on(&cdev->recvwait);
698 if ((skb = skb_dequeue(&cdev->recvqueue)) != 0)
700 if (signal_pending(current))
704 return -ERESTARTNOHAND;
706 if (skb->len > count) {
707 skb_queue_head(&cdev->recvqueue, skb);
710 if (copy_to_user(buf, skb->data, skb->len)) {
711 skb_queue_head(&cdev->recvqueue, skb);
722 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
724 struct capidev *cdev = (struct capidev *)file->private_data;
728 if (!cdev->ap.applid)
731 skb = alloc_skb(count, GFP_USER);
735 if (copy_from_user(skb_put(skb, count), buf, count)) {
739 mlen = CAPIMSG_LEN(skb->data);
740 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
741 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
751 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
753 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
754 down(&cdev->ncci_list_sem);
755 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
756 up(&cdev->ncci_list_sem);
759 cdev->errcode = capi20_put_message(&cdev->ap, skb);
769 capi_poll(struct file *file, poll_table * wait)
771 struct capidev *cdev = (struct capidev *)file->private_data;
772 unsigned int mask = 0;
774 if (!cdev->ap.applid)
777 poll_wait(file, &(cdev->recvwait), wait);
778 mask = POLLOUT | POLLWRNORM;
779 if (!skb_queue_empty(&cdev->recvqueue))
780 mask |= POLLIN | POLLRDNORM;
785 capi_ioctl(struct inode *inode, struct file *file,
786 unsigned int cmd, unsigned long arg)
788 struct capidev *cdev = file->private_data;
789 struct capi20_appl *ap = &cdev->ap;
790 capi_ioctl_struct data;
791 int retval = -EINVAL;
792 void __user *argp = (void __user *)arg;
800 if (copy_from_user(&cdev->ap.rparam, argp,
801 sizeof(struct capi_register_params)))
804 cdev->ap.private = cdev;
805 cdev->ap.recv_message = capi_recv_message;
806 cdev->errcode = capi20_register(ap);
812 return (int)ap->applid;
814 case CAPI_GET_VERSION:
816 if (copy_from_user(&data.contr, argp,
819 cdev->errcode = capi20_get_version(data.contr, &data.version);
822 if (copy_to_user(argp, &data.version,
823 sizeof(data.version)))
828 case CAPI_GET_SERIAL:
830 if (copy_from_user(&data.contr, argp,
833 cdev->errcode = capi20_get_serial (data.contr, data.serial);
836 if (copy_to_user(argp, data.serial,
837 sizeof(data.serial)))
841 case CAPI_GET_PROFILE:
843 if (copy_from_user(&data.contr, argp,
847 if (data.contr == 0) {
848 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
852 retval = copy_to_user(argp,
853 &data.profile.ncontroller,
854 sizeof(data.profile.ncontroller));
857 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
861 retval = copy_to_user(argp, &data.profile,
862 sizeof(data.profile));
869 case CAPI_GET_MANUFACTURER:
871 if (copy_from_user(&data.contr, argp,
874 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
878 if (copy_to_user(argp, data.manufacturer,
879 sizeof(data.manufacturer)))
884 case CAPI_GET_ERRCODE:
885 data.errcode = cdev->errcode;
886 cdev->errcode = CAPI_NOERROR;
888 if (copy_to_user(argp, &data.errcode,
889 sizeof(data.errcode)))
895 if (capi20_isinstalled() == CAPI_NOERROR)
899 case CAPI_MANUFACTURER_CMD:
901 struct capi_manufacturer_cmd mcmd;
902 if (!capable(CAP_SYS_ADMIN))
904 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
906 return capi20_manufacturer(mcmd.cmd, mcmd.data);
914 if (copy_from_user(&userflags, argp,
917 if (cmd == CAPI_SET_FLAGS)
918 cdev->userflags |= userflags;
920 cdev->userflags &= ~userflags;
925 if (copy_to_user(argp, &cdev->userflags,
926 sizeof(cdev->userflags)))
930 case CAPI_NCCI_OPENCOUNT:
932 struct capincci *nccip;
933 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
934 struct capiminor *mp;
935 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
938 if (copy_from_user(&ncci, argp, sizeof(ncci)))
941 down(&cdev->ncci_list_sem);
942 if ((nccip = capincci_find(cdev, (u32) ncci)) == 0) {
943 up(&cdev->ncci_list_sem);
946 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
947 if ((mp = nccip->minorp) != 0) {
948 count += atomic_read(&mp->ttyopencount);
950 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
951 up(&cdev->ncci_list_sem);
956 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
957 case CAPI_NCCI_GETUNIT:
959 struct capincci *nccip;
960 struct capiminor *mp;
963 if (copy_from_user(&ncci, argp,
966 down(&cdev->ncci_list_sem);
967 nccip = capincci_find(cdev, (u32) ncci);
968 if (!nccip || (mp = nccip->minorp) == 0) {
969 up(&cdev->ncci_list_sem);
973 up(&cdev->ncci_list_sem);
977 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
983 capi_open(struct inode *inode, struct file *file)
985 if (file->private_data)
988 if ((file->private_data = capidev_alloc()) == 0)
991 return nonseekable_open(inode, file);
995 capi_release(struct inode *inode, struct file *file)
997 struct capidev *cdev = (struct capidev *)file->private_data;
1000 file->private_data = NULL;
1005 static const struct file_operations capi_fops =
1007 .owner = THIS_MODULE,
1008 .llseek = no_llseek,
1010 .write = capi_write,
1012 .ioctl = capi_ioctl,
1014 .release = capi_release,
1017 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1018 /* -------- tty_operations for capincci ----------------------------- */
1020 static int capinc_tty_open(struct tty_struct * tty, struct file * file)
1022 struct capiminor *mp;
1023 unsigned long flags;
1025 if ((mp = capiminor_find(iminor(file->f_path.dentry->d_inode))) == 0)
1030 tty->driver_data = (void *)mp;
1032 spin_lock_irqsave(&workaround_lock, flags);
1033 if (atomic_read(&mp->ttyopencount) == 0)
1035 atomic_inc(&mp->ttyopencount);
1036 #ifdef _DEBUG_REFCOUNT
1037 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1039 handle_minor_recv(mp);
1040 spin_unlock_irqrestore(&workaround_lock, flags);
1044 static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1046 struct capiminor *mp;
1048 mp = (struct capiminor *)tty->driver_data;
1050 if (atomic_dec_and_test(&mp->ttyopencount)) {
1051 #ifdef _DEBUG_REFCOUNT
1052 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1054 tty->driver_data = NULL;
1057 #ifdef _DEBUG_REFCOUNT
1058 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1064 #ifdef _DEBUG_REFCOUNT
1065 printk(KERN_DEBUG "capinc_tty_close\n");
1069 static int capinc_tty_write(struct tty_struct * tty,
1070 const unsigned char *buf, int count)
1072 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1073 struct sk_buff *skb;
1074 unsigned long flags;
1076 #ifdef _DEBUG_TTYFUNCS
1077 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1080 if (!mp || !mp->nccip) {
1081 #ifdef _DEBUG_TTYFUNCS
1082 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1087 spin_lock_irqsave(&workaround_lock, flags);
1091 skb_queue_tail(&mp->outqueue, skb);
1092 mp->outbytes += skb->len;
1095 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1097 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1098 spin_unlock_irqrestore(&workaround_lock, flags);
1102 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1103 memcpy(skb_put(skb, count), buf, count);
1105 skb_queue_tail(&mp->outqueue, skb);
1106 mp->outbytes += skb->len;
1107 (void)handle_minor_send(mp);
1108 (void)handle_minor_recv(mp);
1109 spin_unlock_irqrestore(&workaround_lock, flags);
1113 static void capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1115 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1116 struct sk_buff *skb;
1117 unsigned long flags;
1119 #ifdef _DEBUG_TTYFUNCS
1120 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1123 if (!mp || !mp->nccip) {
1124 #ifdef _DEBUG_TTYFUNCS
1125 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1130 spin_lock_irqsave(&workaround_lock, flags);
1133 if (skb_tailroom(skb) > 0) {
1134 *(skb_put(skb, 1)) = ch;
1135 spin_unlock_irqrestore(&workaround_lock, flags);
1139 skb_queue_tail(&mp->outqueue, skb);
1140 mp->outbytes += skb->len;
1141 (void)handle_minor_send(mp);
1143 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1145 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1146 *(skb_put(skb, 1)) = ch;
1149 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1151 spin_unlock_irqrestore(&workaround_lock, flags);
1154 static void capinc_tty_flush_chars(struct tty_struct *tty)
1156 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1157 struct sk_buff *skb;
1158 unsigned long flags;
1160 #ifdef _DEBUG_TTYFUNCS
1161 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1164 if (!mp || !mp->nccip) {
1165 #ifdef _DEBUG_TTYFUNCS
1166 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1171 spin_lock_irqsave(&workaround_lock, flags);
1175 skb_queue_tail(&mp->outqueue, skb);
1176 mp->outbytes += skb->len;
1177 (void)handle_minor_send(mp);
1179 (void)handle_minor_recv(mp);
1180 spin_unlock_irqrestore(&workaround_lock, flags);
1183 static int capinc_tty_write_room(struct tty_struct *tty)
1185 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1187 if (!mp || !mp->nccip) {
1188 #ifdef _DEBUG_TTYFUNCS
1189 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1193 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1194 room *= CAPI_MAX_BLKSIZE;
1195 #ifdef _DEBUG_TTYFUNCS
1196 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1201 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1203 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1204 if (!mp || !mp->nccip) {
1205 #ifdef _DEBUG_TTYFUNCS
1206 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1210 #ifdef _DEBUG_TTYFUNCS
1211 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1212 mp->outbytes, mp->nack,
1213 skb_queue_len(&mp->outqueue),
1214 skb_queue_len(&mp->inqueue));
1216 return mp->outbytes;
1219 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1220 unsigned int cmd, unsigned long arg)
1225 error = n_tty_ioctl (tty, file, cmd, arg);
1231 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
1233 #ifdef _DEBUG_TTYFUNCS
1234 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1238 static void capinc_tty_throttle(struct tty_struct * tty)
1240 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1241 #ifdef _DEBUG_TTYFUNCS
1242 printk(KERN_DEBUG "capinc_tty_throttle\n");
1248 static void capinc_tty_unthrottle(struct tty_struct * tty)
1250 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1251 unsigned long flags;
1252 #ifdef _DEBUG_TTYFUNCS
1253 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1256 spin_lock_irqsave(&workaround_lock, flags);
1258 handle_minor_recv(mp);
1259 spin_unlock_irqrestore(&workaround_lock, flags);
1263 static void capinc_tty_stop(struct tty_struct *tty)
1265 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1266 #ifdef _DEBUG_TTYFUNCS
1267 printk(KERN_DEBUG "capinc_tty_stop\n");
1274 static void capinc_tty_start(struct tty_struct *tty)
1276 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1277 unsigned long flags;
1278 #ifdef _DEBUG_TTYFUNCS
1279 printk(KERN_DEBUG "capinc_tty_start\n");
1282 spin_lock_irqsave(&workaround_lock, flags);
1284 (void)handle_minor_send(mp);
1285 spin_unlock_irqrestore(&workaround_lock, flags);
1289 static void capinc_tty_hangup(struct tty_struct *tty)
1291 #ifdef _DEBUG_TTYFUNCS
1292 printk(KERN_DEBUG "capinc_tty_hangup\n");
1296 static void capinc_tty_break_ctl(struct tty_struct *tty, int state)
1298 #ifdef _DEBUG_TTYFUNCS
1299 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1303 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1305 #ifdef _DEBUG_TTYFUNCS
1306 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1310 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1312 #ifdef _DEBUG_TTYFUNCS
1313 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1317 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1319 #ifdef _DEBUG_TTYFUNCS
1320 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1324 static int capinc_tty_read_proc(char *page, char **start, off_t off,
1325 int count, int *eof, void *data)
1330 static struct tty_driver *capinc_tty_driver;
1332 static const struct tty_operations capinc_ops = {
1333 .open = capinc_tty_open,
1334 .close = capinc_tty_close,
1335 .write = capinc_tty_write,
1336 .put_char = capinc_tty_put_char,
1337 .flush_chars = capinc_tty_flush_chars,
1338 .write_room = capinc_tty_write_room,
1339 .chars_in_buffer = capinc_tty_chars_in_buffer,
1340 .ioctl = capinc_tty_ioctl,
1341 .set_termios = capinc_tty_set_termios,
1342 .throttle = capinc_tty_throttle,
1343 .unthrottle = capinc_tty_unthrottle,
1344 .stop = capinc_tty_stop,
1345 .start = capinc_tty_start,
1346 .hangup = capinc_tty_hangup,
1347 .break_ctl = capinc_tty_break_ctl,
1348 .flush_buffer = capinc_tty_flush_buffer,
1349 .set_ldisc = capinc_tty_set_ldisc,
1350 .send_xchar = capinc_tty_send_xchar,
1351 .read_proc = capinc_tty_read_proc,
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 proc_capidev_read_proc(char *page, char **start, off_t off,
1408 int count, int *eof, void *data)
1410 struct capidev *cdev;
1411 struct list_head *l;
1414 read_lock(&capidev_list_lock);
1415 list_for_each(l, &capidev_list) {
1416 cdev = list_entry(l, struct capidev, list);
1417 len += sprintf(page+len, "0 %d %lu %lu %lu %lu\n",
1419 cdev->ap.nrecvctlpkt,
1420 cdev->ap.nrecvdatapkt,
1421 cdev->ap.nsentctlpkt,
1422 cdev->ap.nsentdatapkt);
1427 if (len-off > count)
1433 read_unlock(&capidev_list_lock);
1436 if (len > count) len = count;
1437 if (len < 0) len = 0;
1442 * /proc/capi/capi20ncci:
1445 static int proc_capincci_read_proc(char *page, char **start, off_t off,
1446 int count, int *eof, void *data)
1448 struct capidev *cdev;
1449 struct capincci *np;
1450 struct list_head *l;
1453 read_lock(&capidev_list_lock);
1454 list_for_each(l, &capidev_list) {
1455 cdev = list_entry(l, struct capidev, list);
1456 for (np=cdev->nccis; np; np = np->next) {
1457 len += sprintf(page+len, "%d 0x%x\n",
1464 if (len-off > count)
1470 read_unlock(&capidev_list_lock);
1474 if (len>count) len = count;
1479 static struct procfsentries {
1482 int (*read_proc)(char *page, char **start, off_t off,
1483 int count, int *eof, void *data);
1484 struct proc_dir_entry *procent;
1485 } procfsentries[] = {
1486 /* { "capi", S_IFDIR, 0 }, */
1487 { "capi/capi20", 0 , proc_capidev_read_proc },
1488 { "capi/capi20ncci", 0 , proc_capincci_read_proc },
1491 static void __init proc_init(void)
1493 int nelem = ARRAY_SIZE(procfsentries);
1496 for (i=0; i < nelem; i++) {
1497 struct procfsentries *p = procfsentries + i;
1498 p->procent = create_proc_entry(p->name, p->mode, NULL);
1499 if (p->procent) p->procent->read_proc = p->read_proc;
1503 static void __exit proc_exit(void)
1505 int nelem = ARRAY_SIZE(procfsentries);
1508 for (i=nelem-1; i >= 0; i--) {
1509 struct procfsentries *p = procfsentries + i;
1511 remove_proc_entry(p->name, NULL);
1517 /* -------- init function and module interface ---------------------- */
1520 static char rev[32];
1522 static int __init capi_init(void)
1528 if ((p = strchr(revision, ':')) != 0 && p[1]) {
1529 strlcpy(rev, p + 2, sizeof(rev));
1530 if ((p = strchr(rev, '$')) != 0 && p > rev)
1535 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1536 if (major_ret < 0) {
1537 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1540 capi_class = class_create(THIS_MODULE, "capi");
1541 if (IS_ERR(capi_class)) {
1542 unregister_chrdev(capi_major, "capi20");
1543 return PTR_ERR(capi_class);
1546 class_device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
1548 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1549 if (capinc_tty_init() < 0) {
1550 class_device_destroy(capi_class, MKDEV(capi_major, 0));
1551 class_destroy(capi_class);
1552 unregister_chrdev(capi_major, "capi20");
1555 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1559 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1560 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1561 compileinfo = " (middleware+capifs)";
1563 compileinfo = " (no capifs)";
1566 compileinfo = " (no middleware)";
1568 printk(KERN_NOTICE "capi20: Rev %s: started up with major %d%s\n",
1569 rev, capi_major, compileinfo);
1574 static void __exit capi_exit(void)
1578 class_device_destroy(capi_class, MKDEV(capi_major, 0));
1579 class_destroy(capi_class);
1580 unregister_chrdev(capi_major, "capi20");
1582 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1585 printk(KERN_NOTICE "capi: Rev %s: unloaded\n", rev);
1588 module_init(capi_init);
1589 module_exit(capi_exit);