1 /* $Id: capi.c,v 1.1.2.7 2004/04/28 09:48:59 armin Exp $
3 * CAPI 2.0 Interface for Linux
5 * Copyright 1996 by Carsten Paeth <calle@calle.de>
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
12 #include <linux/module.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/major.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/fcntl.h>
20 #include <linux/signal.h>
21 #include <linux/mutex.h>
23 #include <linux/smp_lock.h>
24 #include <linux/timer.h>
25 #include <linux/wait.h>
26 #include <linux/tty.h>
27 #include <linux/netdevice.h>
28 #include <linux/ppp_defs.h>
29 #include <linux/if_ppp.h>
30 #include <linux/skbuff.h>
31 #include <linux/proc_fs.h>
32 #include <linux/seq_file.h>
33 #include <linux/poll.h>
34 #include <linux/capi.h>
35 #include <linux/kernelcapi.h>
36 #include <linux/init.h>
37 #include <linux/device.h>
38 #include <linux/moduleparam.h>
39 #include <linux/isdn/capiutil.h>
40 #include <linux/isdn/capicmd.h>
44 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
45 MODULE_AUTHOR("Carsten Paeth");
46 MODULE_LICENSE("GPL");
48 #undef _DEBUG_REFCOUNT /* alloc/free and open/close debug */
49 #undef _DEBUG_TTYFUNCS /* call to tty_driver */
50 #undef _DEBUG_DATAFLOW /* data flow */
52 /* -------- driver information -------------------------------------- */
54 static struct class *capi_class;
55 static int capi_major = 68; /* allocated */
57 module_param_named(major, capi_major, uint, 0);
59 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
60 #define CAPINC_NR_PORTS 32
61 #define CAPINC_MAX_PORTS 256
63 static int capi_ttyminors = CAPINC_NR_PORTS;
65 module_param_named(ttyminors, capi_ttyminors, uint, 0);
66 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
68 /* -------- defines ------------------------------------------------- */
70 #define CAPINC_MAX_RECVQUEUE 10
71 #define CAPINC_MAX_SENDQUEUE 10
72 #define CAPI_MAX_BLKSIZE 2048
74 /* -------- data structures ----------------------------------------- */
80 struct datahandle_queue {
81 struct list_head list;
88 struct capincci *nccip;
90 struct dentry *capifs_dentry;
92 struct capi20_appl *ap;
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 const struct tty_port_operations capiminor_port_ops; /* we have none */
217 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
219 struct capiminor *mp;
224 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
226 printk(KERN_ERR "capi: can't alloc capiminor\n");
230 kref_init(&mp->kref);
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 tty_port_init(&mp->port);
243 mp->port.ops = &capiminor_port_ops;
245 /* Allocate the least unused minor number. */
246 write_lock_irqsave(&capiminors_lock, flags);
247 for (minor = 0; minor < capi_ttyminors; minor++)
248 if (!capiminors[minor]) {
249 capiminors[minor] = mp;
252 write_unlock_irqrestore(&capiminors_lock, flags);
254 if (minor == capi_ttyminors) {
255 printk(KERN_NOTICE "capi: out of minors\n");
261 dev = tty_register_device(capinc_tty_driver, minor, NULL);
268 write_lock_irqsave(&capiminors_lock, flags);
269 capiminors[minor] = NULL;
270 write_unlock_irqrestore(&capiminors_lock, flags);
277 static void capiminor_destroy(struct kref *kref)
279 struct capiminor *mp = container_of(kref, struct capiminor, kref);
281 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];
296 read_unlock(&capiminors_lock);
301 static inline void capiminor_put(struct capiminor *mp)
303 kref_put(&mp->kref, capiminor_destroy);
306 static void capiminor_free(struct capiminor *mp)
310 tty_unregister_device(capinc_tty_driver, mp->minor);
312 write_lock_irqsave(&capiminors_lock, flags);
313 capiminors[mp->minor] = NULL;
314 write_unlock_irqrestore(&capiminors_lock, flags);
319 /* -------- struct capincci ----------------------------------------- */
321 static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
323 struct capiminor *mp;
326 if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
329 mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
332 #ifdef _DEBUG_REFCOUNT
333 printk(KERN_DEBUG "set mp->nccip\n");
335 device = MKDEV(capinc_tty_driver->major, mp->minor);
336 mp->capifs_dentry = capifs_new_ncci(mp->minor, device);
340 static void capincci_free_minor(struct capincci *np)
342 struct capiminor *mp = np->minorp;
343 struct tty_struct *tty;
346 capifs_free_ncci(mp->capifs_dentry);
348 tty = tty_port_tty_get(&mp->port);
351 #ifdef _DEBUG_REFCOUNT
352 printk(KERN_DEBUG "reset mp->nccip\n");
362 static inline unsigned int capincci_minor_opencount(struct capincci *np)
364 struct capiminor *mp = np->minorp;
366 return mp ? atomic_read(&mp->ttyopencount) : 0;
369 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
372 capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
373 static inline void capincci_free_minor(struct capincci *np) { }
375 static inline unsigned int capincci_minor_opencount(struct capincci *np)
380 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
382 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
386 np = kzalloc(sizeof(*np), GFP_KERNEL);
392 capincci_alloc_minor(cdev, np);
394 list_add_tail(&np->list, &cdev->nccis);
399 static void capincci_free(struct capidev *cdev, u32 ncci)
401 struct capincci *np, *tmp;
403 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
404 if (ncci == 0xffffffff || np->ncci == ncci) {
405 capincci_free_minor(np);
411 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
415 list_for_each_entry(np, &cdev->nccis, list)
416 if (np->ncci == ncci)
421 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
422 /* -------- handle data queue --------------------------------------- */
424 static struct sk_buff *
425 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
427 struct sk_buff *nskb;
428 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
430 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
431 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
432 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
433 capimsg_setu16(s, 2, mp->ap->applid);
434 capimsg_setu8 (s, 4, CAPI_DATA_B3);
435 capimsg_setu8 (s, 5, CAPI_RESP);
436 capimsg_setu16(s, 6, mp->msgid++);
437 capimsg_setu32(s, 8, mp->ncci);
438 capimsg_setu16(s, 12, datahandle);
443 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
445 struct tty_struct *tty;
446 struct sk_buff *nskb;
448 u16 errcode, datahandle;
449 struct tty_ldisc *ld;
452 datalen = skb->len - CAPIMSG_LEN(skb->data);
454 tty = tty_port_tty_get(&mp->port);
456 #ifdef _DEBUG_DATAFLOW
457 printk(KERN_DEBUG "capi: currently no receiver\n");
462 ld = tty_ldisc_ref(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 (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(tty, skb->data, NULL, skb->len);
511 static void handle_minor_recv(struct capiminor *mp)
514 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
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)
527 struct tty_struct *tty;
534 tty = tty_port_tty_get(&mp->port);
538 if (mp->ttyoutstop) {
539 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
540 printk(KERN_DEBUG "capi: send: tty stopped\n");
546 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
547 datahandle = mp->datahandle;
549 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
550 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
551 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
552 capimsg_setu16(skb->data, 2, mp->ap->applid);
553 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
554 capimsg_setu8 (skb->data, 5, CAPI_REQ);
555 capimsg_setu16(skb->data, 6, mp->msgid++);
556 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
557 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
558 capimsg_setu16(skb->data, 16, len); /* Data length */
559 capimsg_setu16(skb->data, 18, datahandle);
560 capimsg_setu16(skb->data, 20, 0); /* Flags */
562 if (capiminor_add_ack(mp, datahandle) < 0) {
563 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
564 skb_queue_head(&mp->outqueue, skb);
568 errcode = capi20_put_message(mp->ap, skb);
569 if (errcode == CAPI_NOERROR) {
573 #ifdef _DEBUG_DATAFLOW
574 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
579 capiminor_del_ack(mp, datahandle);
581 if (errcode == CAPI_SENDQUEUEFULL) {
582 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
583 skb_queue_head(&mp->outqueue, skb);
587 /* ups, drop packet */
588 printk(KERN_ERR "capi: put_message = %x\n", errcode);
596 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
597 /* -------- function called by lower level -------------------------- */
599 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
601 struct capidev *cdev = ap->private;
602 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
603 struct tty_struct *tty;
604 struct capiminor *mp;
606 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
610 mutex_lock(&cdev->lock);
612 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
613 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
614 if ((info & 0xff00) == 0)
615 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
617 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
618 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
620 spin_lock_irqsave(&workaround_lock, flags);
621 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
622 skb_queue_tail(&cdev->recvqueue, skb);
623 wake_up_interruptible(&cdev->recvwait);
627 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
629 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
630 skb_queue_tail(&cdev->recvqueue, skb);
631 wake_up_interruptible(&cdev->recvwait);
635 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
636 skb_queue_tail(&cdev->recvqueue, skb);
637 wake_up_interruptible(&cdev->recvwait);
639 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
643 skb_queue_tail(&cdev->recvqueue, skb);
644 wake_up_interruptible(&cdev->recvwait);
647 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
648 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
649 #ifdef _DEBUG_DATAFLOW
650 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
651 datahandle, skb->len-CAPIMSG_LEN(skb->data));
653 skb_queue_tail(&mp->inqueue, skb);
654 mp->inbytes += skb->len;
655 handle_minor_recv(mp);
657 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
659 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
660 #ifdef _DEBUG_DATAFLOW
661 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
663 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
666 (void)capiminor_del_ack(mp, datahandle);
667 tty = tty_port_tty_get(&mp->port);
672 (void)handle_minor_send(mp);
675 /* ups, let capi application handle it :-) */
676 skb_queue_tail(&cdev->recvqueue, skb);
677 wake_up_interruptible(&cdev->recvwait);
679 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
682 spin_unlock_irqrestore(&workaround_lock, flags);
683 mutex_unlock(&cdev->lock);
686 /* -------- file_operations for capidev ----------------------------- */
689 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
691 struct capidev *cdev = (struct capidev *)file->private_data;
696 if (!cdev->ap.applid)
699 skb = skb_dequeue(&cdev->recvqueue);
701 if (file->f_flags & O_NONBLOCK)
703 err = wait_event_interruptible(cdev->recvwait,
704 (skb = skb_dequeue(&cdev->recvqueue)));
708 if (skb->len > count) {
709 skb_queue_head(&cdev->recvqueue, skb);
712 if (copy_to_user(buf, skb->data, skb->len)) {
713 skb_queue_head(&cdev->recvqueue, skb);
724 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
726 struct capidev *cdev = (struct capidev *)file->private_data;
730 if (!cdev->ap.applid)
733 skb = alloc_skb(count, GFP_USER);
737 if (copy_from_user(skb_put(skb, count), buf, count)) {
741 mlen = CAPIMSG_LEN(skb->data);
742 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
743 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
753 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
755 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
756 mutex_lock(&cdev->lock);
757 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
758 mutex_unlock(&cdev->lock);
761 cdev->errcode = capi20_put_message(&cdev->ap, skb);
771 capi_poll(struct file *file, poll_table * wait)
773 struct capidev *cdev = (struct capidev *)file->private_data;
774 unsigned int mask = 0;
776 if (!cdev->ap.applid)
779 poll_wait(file, &(cdev->recvwait), wait);
780 mask = POLLOUT | POLLWRNORM;
781 if (!skb_queue_empty(&cdev->recvqueue))
782 mask |= POLLIN | POLLRDNORM;
787 capi_ioctl(struct inode *inode, struct file *file,
788 unsigned int cmd, unsigned long arg)
790 struct capidev *cdev = file->private_data;
791 capi_ioctl_struct data;
792 int retval = -EINVAL;
793 void __user *argp = (void __user *)arg;
797 mutex_lock(&cdev->lock);
799 if (cdev->ap.applid) {
803 if (copy_from_user(&cdev->ap.rparam, argp,
804 sizeof(struct capi_register_params))) {
808 cdev->ap.private = cdev;
809 cdev->ap.recv_message = capi_recv_message;
810 cdev->errcode = capi20_register(&cdev->ap);
811 retval = (int)cdev->ap.applid;
818 mutex_unlock(&cdev->lock);
821 case CAPI_GET_VERSION:
823 if (copy_from_user(&data.contr, argp,
826 cdev->errcode = capi20_get_version(data.contr, &data.version);
829 if (copy_to_user(argp, &data.version,
830 sizeof(data.version)))
835 case CAPI_GET_SERIAL:
837 if (copy_from_user(&data.contr, argp,
840 cdev->errcode = capi20_get_serial (data.contr, data.serial);
843 if (copy_to_user(argp, data.serial,
844 sizeof(data.serial)))
848 case CAPI_GET_PROFILE:
850 if (copy_from_user(&data.contr, argp,
854 if (data.contr == 0) {
855 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
859 retval = copy_to_user(argp,
860 &data.profile.ncontroller,
861 sizeof(data.profile.ncontroller));
864 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
868 retval = copy_to_user(argp, &data.profile,
869 sizeof(data.profile));
876 case CAPI_GET_MANUFACTURER:
878 if (copy_from_user(&data.contr, argp,
881 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
885 if (copy_to_user(argp, data.manufacturer,
886 sizeof(data.manufacturer)))
891 case CAPI_GET_ERRCODE:
892 data.errcode = cdev->errcode;
893 cdev->errcode = CAPI_NOERROR;
895 if (copy_to_user(argp, &data.errcode,
896 sizeof(data.errcode)))
902 if (capi20_isinstalled() == CAPI_NOERROR)
906 case CAPI_MANUFACTURER_CMD:
908 struct capi_manufacturer_cmd mcmd;
909 if (!capable(CAP_SYS_ADMIN))
911 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
913 return capi20_manufacturer(mcmd.cmd, mcmd.data);
918 case CAPI_CLR_FLAGS: {
921 if (copy_from_user(&userflags, argp, sizeof(userflags)))
924 mutex_lock(&cdev->lock);
925 if (cmd == CAPI_SET_FLAGS)
926 cdev->userflags |= userflags;
928 cdev->userflags &= ~userflags;
929 mutex_unlock(&cdev->lock);
933 if (copy_to_user(argp, &cdev->userflags,
934 sizeof(cdev->userflags)))
938 case CAPI_NCCI_OPENCOUNT: {
939 struct capincci *nccip;
943 if (copy_from_user(&ncci, argp, sizeof(ncci)))
946 mutex_lock(&cdev->lock);
947 nccip = capincci_find(cdev, (u32)ncci);
949 count = capincci_minor_opencount(nccip);
950 mutex_unlock(&cdev->lock);
954 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
955 case CAPI_NCCI_GETUNIT: {
956 struct capincci *nccip;
957 struct capiminor *mp;
961 if (copy_from_user(&ncci, argp, sizeof(ncci)))
964 mutex_lock(&cdev->lock);
965 nccip = capincci_find(cdev, (u32)ncci);
971 mutex_unlock(&cdev->lock);
974 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
981 static int capi_open(struct inode *inode, struct file *file)
983 struct capidev *cdev;
985 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
989 mutex_init(&cdev->lock);
990 skb_queue_head_init(&cdev->recvqueue);
991 init_waitqueue_head(&cdev->recvwait);
992 INIT_LIST_HEAD(&cdev->nccis);
993 file->private_data = cdev;
995 mutex_lock(&capidev_list_lock);
996 list_add_tail(&cdev->list, &capidev_list);
997 mutex_unlock(&capidev_list_lock);
999 return nonseekable_open(inode, file);
1002 static int capi_release(struct inode *inode, struct file *file)
1004 struct capidev *cdev = file->private_data;
1006 mutex_lock(&capidev_list_lock);
1007 list_del(&cdev->list);
1008 mutex_unlock(&capidev_list_lock);
1010 if (cdev->ap.applid)
1011 capi20_release(&cdev->ap);
1012 skb_queue_purge(&cdev->recvqueue);
1013 capincci_free(cdev, 0xffffffff);
1019 static const struct file_operations capi_fops =
1021 .owner = THIS_MODULE,
1022 .llseek = no_llseek,
1024 .write = capi_write,
1026 .ioctl = capi_ioctl,
1028 .release = capi_release,
1031 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1032 /* -------- tty_operations for capincci ----------------------------- */
1035 capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1037 int idx = tty->index;
1038 struct capiminor *mp = capiminor_get(idx);
1039 int ret = tty_init_termios(tty);
1042 tty_driver_kref_get(driver);
1044 tty->driver_data = mp;
1045 driver->ttys[idx] = tty;
1051 static void capinc_tty_cleanup(struct tty_struct *tty)
1053 struct capiminor *mp = tty->driver_data;
1054 tty->driver_data = NULL;
1058 static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
1060 struct capiminor *mp = tty->driver_data;
1061 unsigned long flags;
1064 err = tty_port_open(&mp->port, tty, filp);
1068 spin_lock_irqsave(&workaround_lock, flags);
1069 atomic_inc(&mp->ttyopencount);
1070 #ifdef _DEBUG_REFCOUNT
1071 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1073 handle_minor_recv(mp);
1074 spin_unlock_irqrestore(&workaround_lock, flags);
1078 static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
1080 struct capiminor *mp = tty->driver_data;
1082 if (atomic_dec_and_test(&mp->ttyopencount)) {
1083 #ifdef _DEBUG_REFCOUNT
1084 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1087 #ifdef _DEBUG_REFCOUNT
1088 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1091 #ifdef _DEBUG_REFCOUNT
1092 printk(KERN_DEBUG "capinc_tty_close\n");
1094 tty_port_close(&mp->port, tty, filp);
1097 static int capinc_tty_write(struct tty_struct * tty,
1098 const unsigned char *buf, int count)
1100 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1101 struct sk_buff *skb;
1102 unsigned long flags;
1104 #ifdef _DEBUG_TTYFUNCS
1105 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1108 if (!mp || !mp->nccip) {
1109 #ifdef _DEBUG_TTYFUNCS
1110 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1115 spin_lock_irqsave(&workaround_lock, flags);
1119 skb_queue_tail(&mp->outqueue, skb);
1120 mp->outbytes += skb->len;
1123 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1125 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1126 spin_unlock_irqrestore(&workaround_lock, flags);
1130 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1131 memcpy(skb_put(skb, count), buf, count);
1133 skb_queue_tail(&mp->outqueue, skb);
1134 mp->outbytes += skb->len;
1135 (void)handle_minor_send(mp);
1136 (void)handle_minor_recv(mp);
1137 spin_unlock_irqrestore(&workaround_lock, flags);
1141 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1143 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1144 struct sk_buff *skb;
1145 unsigned long flags;
1148 #ifdef _DEBUG_TTYFUNCS
1149 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1152 if (!mp || !mp->nccip) {
1153 #ifdef _DEBUG_TTYFUNCS
1154 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1159 spin_lock_irqsave(&workaround_lock, flags);
1162 if (skb_tailroom(skb) > 0) {
1163 *(skb_put(skb, 1)) = ch;
1164 spin_unlock_irqrestore(&workaround_lock, flags);
1168 skb_queue_tail(&mp->outqueue, skb);
1169 mp->outbytes += skb->len;
1170 (void)handle_minor_send(mp);
1172 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1174 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1175 *(skb_put(skb, 1)) = ch;
1178 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1181 spin_unlock_irqrestore(&workaround_lock, flags);
1185 static void capinc_tty_flush_chars(struct tty_struct *tty)
1187 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1188 struct sk_buff *skb;
1189 unsigned long flags;
1191 #ifdef _DEBUG_TTYFUNCS
1192 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1195 if (!mp || !mp->nccip) {
1196 #ifdef _DEBUG_TTYFUNCS
1197 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1202 spin_lock_irqsave(&workaround_lock, flags);
1206 skb_queue_tail(&mp->outqueue, skb);
1207 mp->outbytes += skb->len;
1208 (void)handle_minor_send(mp);
1210 (void)handle_minor_recv(mp);
1211 spin_unlock_irqrestore(&workaround_lock, flags);
1214 static int capinc_tty_write_room(struct tty_struct *tty)
1216 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1218 if (!mp || !mp->nccip) {
1219 #ifdef _DEBUG_TTYFUNCS
1220 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1224 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1225 room *= CAPI_MAX_BLKSIZE;
1226 #ifdef _DEBUG_TTYFUNCS
1227 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1232 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1234 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1235 if (!mp || !mp->nccip) {
1236 #ifdef _DEBUG_TTYFUNCS
1237 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1241 #ifdef _DEBUG_TTYFUNCS
1242 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1243 mp->outbytes, mp->nack,
1244 skb_queue_len(&mp->outqueue),
1245 skb_queue_len(&mp->inqueue));
1247 return mp->outbytes;
1250 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1251 unsigned int cmd, unsigned long arg)
1256 error = n_tty_ioctl_helper(tty, file, cmd, arg);
1262 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
1264 #ifdef _DEBUG_TTYFUNCS
1265 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1269 static void capinc_tty_throttle(struct tty_struct * tty)
1271 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1272 #ifdef _DEBUG_TTYFUNCS
1273 printk(KERN_DEBUG "capinc_tty_throttle\n");
1279 static void capinc_tty_unthrottle(struct tty_struct * tty)
1281 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1282 unsigned long flags;
1283 #ifdef _DEBUG_TTYFUNCS
1284 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1287 spin_lock_irqsave(&workaround_lock, flags);
1289 handle_minor_recv(mp);
1290 spin_unlock_irqrestore(&workaround_lock, flags);
1294 static void capinc_tty_stop(struct tty_struct *tty)
1296 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1297 #ifdef _DEBUG_TTYFUNCS
1298 printk(KERN_DEBUG "capinc_tty_stop\n");
1305 static void capinc_tty_start(struct tty_struct *tty)
1307 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1308 unsigned long flags;
1309 #ifdef _DEBUG_TTYFUNCS
1310 printk(KERN_DEBUG "capinc_tty_start\n");
1313 spin_lock_irqsave(&workaround_lock, flags);
1315 (void)handle_minor_send(mp);
1316 spin_unlock_irqrestore(&workaround_lock, flags);
1320 static void capinc_tty_hangup(struct tty_struct *tty)
1322 struct capiminor *mp = tty->driver_data;
1324 #ifdef _DEBUG_TTYFUNCS
1325 printk(KERN_DEBUG "capinc_tty_hangup\n");
1327 tty_port_hangup(&mp->port);
1330 static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1332 #ifdef _DEBUG_TTYFUNCS
1333 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1338 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1340 #ifdef _DEBUG_TTYFUNCS
1341 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1345 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1347 #ifdef _DEBUG_TTYFUNCS
1348 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1352 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1354 #ifdef _DEBUG_TTYFUNCS
1355 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1359 static const struct tty_operations capinc_ops = {
1360 .open = capinc_tty_open,
1361 .close = capinc_tty_close,
1362 .write = capinc_tty_write,
1363 .put_char = capinc_tty_put_char,
1364 .flush_chars = capinc_tty_flush_chars,
1365 .write_room = capinc_tty_write_room,
1366 .chars_in_buffer = capinc_tty_chars_in_buffer,
1367 .ioctl = capinc_tty_ioctl,
1368 .set_termios = capinc_tty_set_termios,
1369 .throttle = capinc_tty_throttle,
1370 .unthrottle = capinc_tty_unthrottle,
1371 .stop = capinc_tty_stop,
1372 .start = capinc_tty_start,
1373 .hangup = capinc_tty_hangup,
1374 .break_ctl = capinc_tty_break_ctl,
1375 .flush_buffer = capinc_tty_flush_buffer,
1376 .set_ldisc = capinc_tty_set_ldisc,
1377 .send_xchar = capinc_tty_send_xchar,
1378 .install = capinc_tty_install,
1379 .cleanup = capinc_tty_cleanup,
1382 static int __init capinc_tty_init(void)
1384 struct tty_driver *drv;
1387 if (capi_ttyminors > CAPINC_MAX_PORTS)
1388 capi_ttyminors = CAPINC_MAX_PORTS;
1389 if (capi_ttyminors <= 0)
1390 capi_ttyminors = CAPINC_NR_PORTS;
1392 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1397 drv = alloc_tty_driver(capi_ttyminors);
1402 drv->owner = THIS_MODULE;
1403 drv->driver_name = "capi_nc";
1406 drv->minor_start = 0;
1407 drv->type = TTY_DRIVER_TYPE_SERIAL;
1408 drv->subtype = SERIAL_TYPE_NORMAL;
1409 drv->init_termios = tty_std_termios;
1410 drv->init_termios.c_iflag = ICRNL;
1411 drv->init_termios.c_oflag = OPOST | ONLCR;
1412 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1413 drv->init_termios.c_lflag = 0;
1415 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1416 TTY_DRIVER_DYNAMIC_DEV;
1417 tty_set_operations(drv, &capinc_ops);
1419 err = tty_register_driver(drv);
1421 put_tty_driver(drv);
1423 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1426 capinc_tty_driver = drv;
1430 static void __exit capinc_tty_exit(void)
1432 tty_unregister_driver(capinc_tty_driver);
1433 put_tty_driver(capinc_tty_driver);
1437 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1439 static inline int capinc_tty_init(void)
1444 static inline void capinc_tty_exit(void) { }
1446 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1448 /* -------- /proc functions ----------------------------------------- */
1451 * /proc/capi/capi20:
1452 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1454 static int capi20_proc_show(struct seq_file *m, void *v)
1456 struct capidev *cdev;
1457 struct list_head *l;
1459 mutex_lock(&capidev_list_lock);
1460 list_for_each(l, &capidev_list) {
1461 cdev = list_entry(l, struct capidev, list);
1462 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
1464 cdev->ap.nrecvctlpkt,
1465 cdev->ap.nrecvdatapkt,
1466 cdev->ap.nsentctlpkt,
1467 cdev->ap.nsentdatapkt);
1469 mutex_unlock(&capidev_list_lock);
1473 static int capi20_proc_open(struct inode *inode, struct file *file)
1475 return single_open(file, capi20_proc_show, NULL);
1478 static const struct file_operations capi20_proc_fops = {
1479 .owner = THIS_MODULE,
1480 .open = capi20_proc_open,
1482 .llseek = seq_lseek,
1483 .release = single_release,
1487 * /proc/capi/capi20ncci:
1490 static int capi20ncci_proc_show(struct seq_file *m, void *v)
1492 struct capidev *cdev;
1493 struct capincci *np;
1495 mutex_lock(&capidev_list_lock);
1496 list_for_each_entry(cdev, &capidev_list, list) {
1497 mutex_lock(&cdev->lock);
1498 list_for_each_entry(np, &cdev->nccis, list)
1499 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
1500 mutex_unlock(&cdev->lock);
1502 mutex_unlock(&capidev_list_lock);
1506 static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1508 return single_open(file, capi20ncci_proc_show, NULL);
1511 static const struct file_operations capi20ncci_proc_fops = {
1512 .owner = THIS_MODULE,
1513 .open = capi20ncci_proc_open,
1515 .llseek = seq_lseek,
1516 .release = single_release,
1519 static void __init proc_init(void)
1521 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1522 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
1525 static void __exit proc_exit(void)
1527 remove_proc_entry("capi/capi20", NULL);
1528 remove_proc_entry("capi/capi20ncci", NULL);
1531 /* -------- init function and module interface ---------------------- */
1534 static int __init capi_init(void)
1536 const char *compileinfo;
1539 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1540 if (major_ret < 0) {
1541 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1544 capi_class = class_create(THIS_MODULE, "capi");
1545 if (IS_ERR(capi_class)) {
1546 unregister_chrdev(capi_major, "capi20");
1547 return PTR_ERR(capi_class);
1550 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
1552 if (capinc_tty_init() < 0) {
1553 device_destroy(capi_class, MKDEV(capi_major, 0));
1554 class_destroy(capi_class);
1555 unregister_chrdev(capi_major, "capi20");
1561 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1562 compileinfo = " (middleware+capifs)";
1563 #elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
1564 compileinfo = " (no capifs)";
1566 compileinfo = " (no middleware)";
1568 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1569 capi_major, compileinfo);
1574 static void __exit capi_exit(void)
1578 device_destroy(capi_class, MKDEV(capi_major, 0));
1579 class_destroy(capi_class);
1580 unregister_chrdev(capi_major, "capi20");
1585 module_init(capi_init);
1586 module_exit(capi_exit);