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;
89 struct dentry *capifs_dentry;
91 struct capi20_appl *ap;
99 struct sk_buff *ttyskb;
100 atomic_t ttyopencount;
102 struct sk_buff_head inqueue;
104 struct sk_buff_head outqueue;
108 struct list_head ackqueue;
113 /* FIXME: The following lock is a sledgehammer-workaround to a
114 * locking issue with the capiminor (and maybe other) data structure(s).
115 * Access to this data is done in a racy way and crashes the machine with
116 * a FritzCard DSL driver; sooner or later. This is a workaround
117 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
118 * The correct (and scalable) fix for the issue seems to require
119 * an API change to the drivers... . */
120 static DEFINE_SPINLOCK(workaround_lock);
123 struct list_head list;
125 struct capidev *cdev;
126 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
127 struct capiminor *minorp;
128 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
132 struct list_head list;
133 struct capi20_appl ap;
137 struct sk_buff_head recvqueue;
138 wait_queue_head_t recvwait;
140 struct list_head nccis;
145 /* -------- global variables ---------------------------------------- */
147 static DEFINE_MUTEX(capidev_list_lock);
148 static LIST_HEAD(capidev_list);
150 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
152 static DEFINE_SPINLOCK(capiminors_lock);
153 static struct capiminor **capiminors;
155 static struct tty_driver *capinc_tty_driver;
157 /* -------- datahandles --------------------------------------------- */
159 static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
161 struct datahandle_queue *n;
164 n = kmalloc(sizeof(*n), GFP_ATOMIC);
166 printk(KERN_ERR "capi: alloc datahandle failed\n");
169 n->datahandle = datahandle;
170 INIT_LIST_HEAD(&n->list);
171 spin_lock_irqsave(&mp->ackqlock, flags);
172 list_add_tail(&n->list, &mp->ackqueue);
174 spin_unlock_irqrestore(&mp->ackqlock, flags);
178 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
180 struct datahandle_queue *p, *tmp;
183 spin_lock_irqsave(&mp->ackqlock, flags);
184 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
185 if (p->datahandle == datahandle) {
189 spin_unlock_irqrestore(&mp->ackqlock, flags);
193 spin_unlock_irqrestore(&mp->ackqlock, flags);
197 static void capiminor_del_all_ack(struct capiminor *mp)
199 struct datahandle_queue *p, *tmp;
202 spin_lock_irqsave(&mp->ackqlock, flags);
203 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
208 spin_unlock_irqrestore(&mp->ackqlock, flags);
212 /* -------- struct capiminor ---------------------------------------- */
214 static const struct tty_port_operations capiminor_port_ops; /* we have none */
216 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
218 struct capiminor *mp;
222 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
224 printk(KERN_ERR "capi: can't alloc capiminor\n");
228 kref_init(&mp->kref);
233 atomic_set(&mp->ttyopencount,0);
234 INIT_LIST_HEAD(&mp->ackqueue);
235 spin_lock_init(&mp->ackqlock);
237 skb_queue_head_init(&mp->inqueue);
238 skb_queue_head_init(&mp->outqueue);
240 tty_port_init(&mp->port);
241 mp->port.ops = &capiminor_port_ops;
243 /* Allocate the least unused minor number. */
244 spin_lock(&capiminors_lock);
245 for (minor = 0; minor < capi_ttyminors; minor++)
246 if (!capiminors[minor]) {
247 capiminors[minor] = mp;
250 spin_unlock(&capiminors_lock);
252 if (minor == capi_ttyminors) {
253 printk(KERN_NOTICE "capi: out of minors\n");
259 dev = tty_register_device(capinc_tty_driver, minor, NULL);
266 spin_lock(&capiminors_lock);
267 capiminors[minor] = NULL;
268 spin_unlock(&capiminors_lock);
275 static void capiminor_destroy(struct kref *kref)
277 struct capiminor *mp = container_of(kref, struct capiminor, kref);
279 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_get(unsigned int minor)
288 struct capiminor *mp;
290 spin_lock(&capiminors_lock);
291 mp = capiminors[minor];
294 spin_unlock(&capiminors_lock);
299 static inline void capiminor_put(struct capiminor *mp)
301 kref_put(&mp->kref, capiminor_destroy);
304 static void capiminor_free(struct capiminor *mp)
306 tty_unregister_device(capinc_tty_driver, mp->minor);
308 spin_lock(&capiminors_lock);
309 capiminors[mp->minor] = NULL;
310 spin_unlock(&capiminors_lock);
315 /* -------- struct capincci ----------------------------------------- */
317 static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
319 struct capiminor *mp;
322 if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
325 mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
327 device = MKDEV(capinc_tty_driver->major, mp->minor);
328 mp->capifs_dentry = capifs_new_ncci(mp->minor, device);
332 static void capincci_free_minor(struct capincci *np)
334 struct capiminor *mp = np->minorp;
335 struct tty_struct *tty;
338 capifs_free_ncci(mp->capifs_dentry);
340 tty = tty_port_tty_get(&mp->port);
350 static inline unsigned int capincci_minor_opencount(struct capincci *np)
352 struct capiminor *mp = np->minorp;
354 return mp ? atomic_read(&mp->ttyopencount) : 0;
357 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
360 capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
361 static inline void capincci_free_minor(struct capincci *np) { }
363 static inline unsigned int capincci_minor_opencount(struct capincci *np)
368 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
370 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
374 np = kzalloc(sizeof(*np), GFP_KERNEL);
380 capincci_alloc_minor(cdev, np);
382 list_add_tail(&np->list, &cdev->nccis);
387 static void capincci_free(struct capidev *cdev, u32 ncci)
389 struct capincci *np, *tmp;
391 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
392 if (ncci == 0xffffffff || np->ncci == ncci) {
393 capincci_free_minor(np);
399 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
403 list_for_each_entry(np, &cdev->nccis, list)
404 if (np->ncci == ncci)
409 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
410 /* -------- handle data queue --------------------------------------- */
412 static struct sk_buff *
413 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
415 struct sk_buff *nskb;
416 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
418 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
419 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
420 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
421 capimsg_setu16(s, 2, mp->ap->applid);
422 capimsg_setu8 (s, 4, CAPI_DATA_B3);
423 capimsg_setu8 (s, 5, CAPI_RESP);
424 capimsg_setu16(s, 6, mp->msgid++);
425 capimsg_setu32(s, 8, mp->ncci);
426 capimsg_setu16(s, 12, datahandle);
431 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
433 struct tty_struct *tty;
434 struct sk_buff *nskb;
436 u16 errcode, datahandle;
437 struct tty_ldisc *ld;
440 datalen = skb->len - CAPIMSG_LEN(skb->data);
442 tty = tty_port_tty_get(&mp->port);
444 #ifdef _DEBUG_DATAFLOW
445 printk(KERN_DEBUG "capi: currently no receiver\n");
450 ld = tty_ldisc_ref(tty);
454 if (ld->ops->receive_buf == NULL) {
455 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
456 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
461 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
462 printk(KERN_DEBUG "capi: recv tty throttled\n");
466 if (tty->receive_room < datalen) {
467 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
468 printk(KERN_DEBUG "capi: no room in tty\n");
472 if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
473 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
476 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
477 errcode = capi20_put_message(mp->ap, nskb);
478 if (errcode != CAPI_NOERROR) {
479 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
484 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
485 #ifdef _DEBUG_DATAFLOW
486 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
487 datahandle, skb->len);
489 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
499 static void handle_minor_recv(struct capiminor *mp)
502 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
503 unsigned int len = skb->len;
505 if (handle_recv_skb(mp, skb) < 0) {
506 skb_queue_head(&mp->inqueue, skb);
513 static int handle_minor_send(struct capiminor *mp)
515 struct tty_struct *tty;
522 tty = tty_port_tty_get(&mp->port);
526 if (mp->ttyoutstop) {
527 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
528 printk(KERN_DEBUG "capi: send: tty stopped\n");
534 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
535 datahandle = mp->datahandle;
537 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
538 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
539 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
540 capimsg_setu16(skb->data, 2, mp->ap->applid);
541 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
542 capimsg_setu8 (skb->data, 5, CAPI_REQ);
543 capimsg_setu16(skb->data, 6, mp->msgid++);
544 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
545 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
546 capimsg_setu16(skb->data, 16, len); /* Data length */
547 capimsg_setu16(skb->data, 18, datahandle);
548 capimsg_setu16(skb->data, 20, 0); /* Flags */
550 if (capiminor_add_ack(mp, datahandle) < 0) {
551 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
552 skb_queue_head(&mp->outqueue, skb);
556 errcode = capi20_put_message(mp->ap, skb);
557 if (errcode == CAPI_NOERROR) {
561 #ifdef _DEBUG_DATAFLOW
562 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
567 capiminor_del_ack(mp, datahandle);
569 if (errcode == CAPI_SENDQUEUEFULL) {
570 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
571 skb_queue_head(&mp->outqueue, skb);
575 /* ups, drop packet */
576 printk(KERN_ERR "capi: put_message = %x\n", errcode);
584 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
585 /* -------- function called by lower level -------------------------- */
587 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
589 struct capidev *cdev = ap->private;
590 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
591 struct tty_struct *tty;
592 struct capiminor *mp;
594 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
598 mutex_lock(&cdev->lock);
600 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
601 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
602 if ((info & 0xff00) == 0)
603 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
605 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
606 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
608 spin_lock_irqsave(&workaround_lock, flags);
609 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
610 skb_queue_tail(&cdev->recvqueue, skb);
611 wake_up_interruptible(&cdev->recvwait);
615 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
617 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
618 skb_queue_tail(&cdev->recvqueue, skb);
619 wake_up_interruptible(&cdev->recvwait);
623 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
624 skb_queue_tail(&cdev->recvqueue, skb);
625 wake_up_interruptible(&cdev->recvwait);
627 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
631 skb_queue_tail(&cdev->recvqueue, skb);
632 wake_up_interruptible(&cdev->recvwait);
635 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
636 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
637 #ifdef _DEBUG_DATAFLOW
638 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
639 datahandle, skb->len-CAPIMSG_LEN(skb->data));
641 skb_queue_tail(&mp->inqueue, skb);
642 mp->inbytes += skb->len;
643 handle_minor_recv(mp);
645 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
647 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
648 #ifdef _DEBUG_DATAFLOW
649 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
651 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
654 (void)capiminor_del_ack(mp, datahandle);
655 tty = tty_port_tty_get(&mp->port);
660 (void)handle_minor_send(mp);
663 /* ups, let capi application handle it :-) */
664 skb_queue_tail(&cdev->recvqueue, skb);
665 wake_up_interruptible(&cdev->recvwait);
667 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
670 spin_unlock_irqrestore(&workaround_lock, flags);
671 mutex_unlock(&cdev->lock);
674 /* -------- file_operations for capidev ----------------------------- */
677 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
679 struct capidev *cdev = (struct capidev *)file->private_data;
684 if (!cdev->ap.applid)
687 skb = skb_dequeue(&cdev->recvqueue);
689 if (file->f_flags & O_NONBLOCK)
691 err = wait_event_interruptible(cdev->recvwait,
692 (skb = skb_dequeue(&cdev->recvqueue)));
696 if (skb->len > count) {
697 skb_queue_head(&cdev->recvqueue, skb);
700 if (copy_to_user(buf, skb->data, skb->len)) {
701 skb_queue_head(&cdev->recvqueue, skb);
712 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
714 struct capidev *cdev = (struct capidev *)file->private_data;
718 if (!cdev->ap.applid)
721 skb = alloc_skb(count, GFP_USER);
725 if (copy_from_user(skb_put(skb, count), buf, count)) {
729 mlen = CAPIMSG_LEN(skb->data);
730 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
731 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
741 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
743 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
744 mutex_lock(&cdev->lock);
745 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
746 mutex_unlock(&cdev->lock);
749 cdev->errcode = capi20_put_message(&cdev->ap, skb);
759 capi_poll(struct file *file, poll_table * wait)
761 struct capidev *cdev = (struct capidev *)file->private_data;
762 unsigned int mask = 0;
764 if (!cdev->ap.applid)
767 poll_wait(file, &(cdev->recvwait), wait);
768 mask = POLLOUT | POLLWRNORM;
769 if (!skb_queue_empty(&cdev->recvqueue))
770 mask |= POLLIN | POLLRDNORM;
775 capi_ioctl(struct inode *inode, struct file *file,
776 unsigned int cmd, unsigned long arg)
778 struct capidev *cdev = file->private_data;
779 capi_ioctl_struct data;
780 int retval = -EINVAL;
781 void __user *argp = (void __user *)arg;
785 mutex_lock(&cdev->lock);
787 if (cdev->ap.applid) {
791 if (copy_from_user(&cdev->ap.rparam, argp,
792 sizeof(struct capi_register_params))) {
796 cdev->ap.private = cdev;
797 cdev->ap.recv_message = capi_recv_message;
798 cdev->errcode = capi20_register(&cdev->ap);
799 retval = (int)cdev->ap.applid;
806 mutex_unlock(&cdev->lock);
809 case CAPI_GET_VERSION:
811 if (copy_from_user(&data.contr, argp,
814 cdev->errcode = capi20_get_version(data.contr, &data.version);
817 if (copy_to_user(argp, &data.version,
818 sizeof(data.version)))
823 case CAPI_GET_SERIAL:
825 if (copy_from_user(&data.contr, argp,
828 cdev->errcode = capi20_get_serial (data.contr, data.serial);
831 if (copy_to_user(argp, data.serial,
832 sizeof(data.serial)))
836 case CAPI_GET_PROFILE:
838 if (copy_from_user(&data.contr, argp,
842 if (data.contr == 0) {
843 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
847 retval = copy_to_user(argp,
848 &data.profile.ncontroller,
849 sizeof(data.profile.ncontroller));
852 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
856 retval = copy_to_user(argp, &data.profile,
857 sizeof(data.profile));
864 case CAPI_GET_MANUFACTURER:
866 if (copy_from_user(&data.contr, argp,
869 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
873 if (copy_to_user(argp, data.manufacturer,
874 sizeof(data.manufacturer)))
879 case CAPI_GET_ERRCODE:
880 data.errcode = cdev->errcode;
881 cdev->errcode = CAPI_NOERROR;
883 if (copy_to_user(argp, &data.errcode,
884 sizeof(data.errcode)))
890 if (capi20_isinstalled() == CAPI_NOERROR)
894 case CAPI_MANUFACTURER_CMD:
896 struct capi_manufacturer_cmd mcmd;
897 if (!capable(CAP_SYS_ADMIN))
899 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
901 return capi20_manufacturer(mcmd.cmd, mcmd.data);
906 case CAPI_CLR_FLAGS: {
909 if (copy_from_user(&userflags, argp, sizeof(userflags)))
912 mutex_lock(&cdev->lock);
913 if (cmd == CAPI_SET_FLAGS)
914 cdev->userflags |= userflags;
916 cdev->userflags &= ~userflags;
917 mutex_unlock(&cdev->lock);
921 if (copy_to_user(argp, &cdev->userflags,
922 sizeof(cdev->userflags)))
926 case CAPI_NCCI_OPENCOUNT: {
927 struct capincci *nccip;
931 if (copy_from_user(&ncci, argp, sizeof(ncci)))
934 mutex_lock(&cdev->lock);
935 nccip = capincci_find(cdev, (u32)ncci);
937 count = capincci_minor_opencount(nccip);
938 mutex_unlock(&cdev->lock);
942 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
943 case CAPI_NCCI_GETUNIT: {
944 struct capincci *nccip;
945 struct capiminor *mp;
949 if (copy_from_user(&ncci, argp, sizeof(ncci)))
952 mutex_lock(&cdev->lock);
953 nccip = capincci_find(cdev, (u32)ncci);
959 mutex_unlock(&cdev->lock);
962 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
969 static int capi_open(struct inode *inode, struct file *file)
971 struct capidev *cdev;
973 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
977 mutex_init(&cdev->lock);
978 skb_queue_head_init(&cdev->recvqueue);
979 init_waitqueue_head(&cdev->recvwait);
980 INIT_LIST_HEAD(&cdev->nccis);
981 file->private_data = cdev;
983 mutex_lock(&capidev_list_lock);
984 list_add_tail(&cdev->list, &capidev_list);
985 mutex_unlock(&capidev_list_lock);
987 return nonseekable_open(inode, file);
990 static int capi_release(struct inode *inode, struct file *file)
992 struct capidev *cdev = file->private_data;
994 mutex_lock(&capidev_list_lock);
995 list_del(&cdev->list);
996 mutex_unlock(&capidev_list_lock);
999 capi20_release(&cdev->ap);
1000 skb_queue_purge(&cdev->recvqueue);
1001 capincci_free(cdev, 0xffffffff);
1007 static const struct file_operations capi_fops =
1009 .owner = THIS_MODULE,
1010 .llseek = no_llseek,
1012 .write = capi_write,
1014 .ioctl = capi_ioctl,
1016 .release = capi_release,
1019 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1020 /* -------- tty_operations for capincci ----------------------------- */
1023 capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1025 int idx = tty->index;
1026 struct capiminor *mp = capiminor_get(idx);
1027 int ret = tty_init_termios(tty);
1030 tty_driver_kref_get(driver);
1032 tty->driver_data = mp;
1033 driver->ttys[idx] = tty;
1039 static void capinc_tty_cleanup(struct tty_struct *tty)
1041 struct capiminor *mp = tty->driver_data;
1042 tty->driver_data = NULL;
1046 static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
1048 struct capiminor *mp = tty->driver_data;
1049 unsigned long flags;
1052 err = tty_port_open(&mp->port, tty, filp);
1056 spin_lock_irqsave(&workaround_lock, flags);
1057 atomic_inc(&mp->ttyopencount);
1058 #ifdef _DEBUG_REFCOUNT
1059 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1061 handle_minor_recv(mp);
1062 spin_unlock_irqrestore(&workaround_lock, flags);
1066 static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
1068 struct capiminor *mp = tty->driver_data;
1070 if (atomic_dec_and_test(&mp->ttyopencount)) {
1071 #ifdef _DEBUG_REFCOUNT
1072 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1075 #ifdef _DEBUG_REFCOUNT
1076 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1079 #ifdef _DEBUG_REFCOUNT
1080 printk(KERN_DEBUG "capinc_tty_close\n");
1082 tty_port_close(&mp->port, tty, filp);
1085 static int capinc_tty_write(struct tty_struct *tty,
1086 const unsigned char *buf, int count)
1088 struct capiminor *mp = tty->driver_data;
1089 struct sk_buff *skb;
1090 unsigned long flags;
1092 #ifdef _DEBUG_TTYFUNCS
1093 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1096 spin_lock_irqsave(&workaround_lock, flags);
1100 skb_queue_tail(&mp->outqueue, skb);
1101 mp->outbytes += skb->len;
1104 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1106 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1107 spin_unlock_irqrestore(&workaround_lock, flags);
1111 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1112 memcpy(skb_put(skb, count), buf, count);
1114 skb_queue_tail(&mp->outqueue, skb);
1115 mp->outbytes += skb->len;
1116 (void)handle_minor_send(mp);
1117 (void)handle_minor_recv(mp);
1118 spin_unlock_irqrestore(&workaround_lock, flags);
1122 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1124 struct capiminor *mp = tty->driver_data;
1125 struct sk_buff *skb;
1126 unsigned long flags;
1129 #ifdef _DEBUG_TTYFUNCS
1130 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1133 spin_lock_irqsave(&workaround_lock, flags);
1136 if (skb_tailroom(skb) > 0) {
1137 *(skb_put(skb, 1)) = ch;
1138 spin_unlock_irqrestore(&workaround_lock, flags);
1142 skb_queue_tail(&mp->outqueue, skb);
1143 mp->outbytes += skb->len;
1144 (void)handle_minor_send(mp);
1146 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1148 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1149 *(skb_put(skb, 1)) = ch;
1152 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1155 spin_unlock_irqrestore(&workaround_lock, flags);
1159 static void capinc_tty_flush_chars(struct tty_struct *tty)
1161 struct capiminor *mp = tty->driver_data;
1162 struct sk_buff *skb;
1163 unsigned long flags;
1165 #ifdef _DEBUG_TTYFUNCS
1166 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1169 spin_lock_irqsave(&workaround_lock, flags);
1173 skb_queue_tail(&mp->outqueue, skb);
1174 mp->outbytes += skb->len;
1175 (void)handle_minor_send(mp);
1177 (void)handle_minor_recv(mp);
1178 spin_unlock_irqrestore(&workaround_lock, flags);
1181 static int capinc_tty_write_room(struct tty_struct *tty)
1183 struct capiminor *mp = tty->driver_data;
1186 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1187 room *= CAPI_MAX_BLKSIZE;
1188 #ifdef _DEBUG_TTYFUNCS
1189 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1194 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1196 struct capiminor *mp = tty->driver_data;
1198 #ifdef _DEBUG_TTYFUNCS
1199 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1200 mp->outbytes, mp->nack,
1201 skb_queue_len(&mp->outqueue),
1202 skb_queue_len(&mp->inqueue));
1204 return mp->outbytes;
1207 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1208 unsigned int cmd, unsigned long arg)
1213 error = n_tty_ioctl_helper(tty, file, cmd, arg);
1219 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
1221 #ifdef _DEBUG_TTYFUNCS
1222 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1226 static void capinc_tty_throttle(struct tty_struct *tty)
1228 struct capiminor *mp = tty->driver_data;
1229 #ifdef _DEBUG_TTYFUNCS
1230 printk(KERN_DEBUG "capinc_tty_throttle\n");
1235 static void capinc_tty_unthrottle(struct tty_struct *tty)
1237 struct capiminor *mp = tty->driver_data;
1238 unsigned long flags;
1240 #ifdef _DEBUG_TTYFUNCS
1241 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1243 spin_lock_irqsave(&workaround_lock, flags);
1245 handle_minor_recv(mp);
1246 spin_unlock_irqrestore(&workaround_lock, flags);
1249 static void capinc_tty_stop(struct tty_struct *tty)
1251 struct capiminor *mp = tty->driver_data;
1253 #ifdef _DEBUG_TTYFUNCS
1254 printk(KERN_DEBUG "capinc_tty_stop\n");
1259 static void capinc_tty_start(struct tty_struct *tty)
1261 struct capiminor *mp = tty->driver_data;
1262 unsigned long flags;
1264 #ifdef _DEBUG_TTYFUNCS
1265 printk(KERN_DEBUG "capinc_tty_start\n");
1267 spin_lock_irqsave(&workaround_lock, flags);
1269 (void)handle_minor_send(mp);
1270 spin_unlock_irqrestore(&workaround_lock, flags);
1273 static void capinc_tty_hangup(struct tty_struct *tty)
1275 struct capiminor *mp = tty->driver_data;
1277 #ifdef _DEBUG_TTYFUNCS
1278 printk(KERN_DEBUG "capinc_tty_hangup\n");
1280 tty_port_hangup(&mp->port);
1283 static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1285 #ifdef _DEBUG_TTYFUNCS
1286 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1291 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1293 #ifdef _DEBUG_TTYFUNCS
1294 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1298 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1300 #ifdef _DEBUG_TTYFUNCS
1301 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1305 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1307 #ifdef _DEBUG_TTYFUNCS
1308 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1312 static const struct tty_operations capinc_ops = {
1313 .open = capinc_tty_open,
1314 .close = capinc_tty_close,
1315 .write = capinc_tty_write,
1316 .put_char = capinc_tty_put_char,
1317 .flush_chars = capinc_tty_flush_chars,
1318 .write_room = capinc_tty_write_room,
1319 .chars_in_buffer = capinc_tty_chars_in_buffer,
1320 .ioctl = capinc_tty_ioctl,
1321 .set_termios = capinc_tty_set_termios,
1322 .throttle = capinc_tty_throttle,
1323 .unthrottle = capinc_tty_unthrottle,
1324 .stop = capinc_tty_stop,
1325 .start = capinc_tty_start,
1326 .hangup = capinc_tty_hangup,
1327 .break_ctl = capinc_tty_break_ctl,
1328 .flush_buffer = capinc_tty_flush_buffer,
1329 .set_ldisc = capinc_tty_set_ldisc,
1330 .send_xchar = capinc_tty_send_xchar,
1331 .install = capinc_tty_install,
1332 .cleanup = capinc_tty_cleanup,
1335 static int __init capinc_tty_init(void)
1337 struct tty_driver *drv;
1340 if (capi_ttyminors > CAPINC_MAX_PORTS)
1341 capi_ttyminors = CAPINC_MAX_PORTS;
1342 if (capi_ttyminors <= 0)
1343 capi_ttyminors = CAPINC_NR_PORTS;
1345 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1350 drv = alloc_tty_driver(capi_ttyminors);
1355 drv->owner = THIS_MODULE;
1356 drv->driver_name = "capi_nc";
1359 drv->minor_start = 0;
1360 drv->type = TTY_DRIVER_TYPE_SERIAL;
1361 drv->subtype = SERIAL_TYPE_NORMAL;
1362 drv->init_termios = tty_std_termios;
1363 drv->init_termios.c_iflag = ICRNL;
1364 drv->init_termios.c_oflag = OPOST | ONLCR;
1365 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1366 drv->init_termios.c_lflag = 0;
1368 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1369 TTY_DRIVER_DYNAMIC_DEV;
1370 tty_set_operations(drv, &capinc_ops);
1372 err = tty_register_driver(drv);
1374 put_tty_driver(drv);
1376 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1379 capinc_tty_driver = drv;
1383 static void __exit capinc_tty_exit(void)
1385 tty_unregister_driver(capinc_tty_driver);
1386 put_tty_driver(capinc_tty_driver);
1390 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1392 static inline int capinc_tty_init(void)
1397 static inline void capinc_tty_exit(void) { }
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 mutex_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 mutex_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;
1448 mutex_lock(&capidev_list_lock);
1449 list_for_each_entry(cdev, &capidev_list, list) {
1450 mutex_lock(&cdev->lock);
1451 list_for_each_entry(np, &cdev->nccis, list)
1452 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
1453 mutex_unlock(&cdev->lock);
1455 mutex_unlock(&capidev_list_lock);
1459 static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1461 return single_open(file, capi20ncci_proc_show, NULL);
1464 static const struct file_operations capi20ncci_proc_fops = {
1465 .owner = THIS_MODULE,
1466 .open = capi20ncci_proc_open,
1468 .llseek = seq_lseek,
1469 .release = single_release,
1472 static void __init proc_init(void)
1474 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1475 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
1478 static void __exit proc_exit(void)
1480 remove_proc_entry("capi/capi20", NULL);
1481 remove_proc_entry("capi/capi20ncci", NULL);
1484 /* -------- init function and module interface ---------------------- */
1487 static int __init capi_init(void)
1489 const char *compileinfo;
1492 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1493 if (major_ret < 0) {
1494 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1497 capi_class = class_create(THIS_MODULE, "capi");
1498 if (IS_ERR(capi_class)) {
1499 unregister_chrdev(capi_major, "capi20");
1500 return PTR_ERR(capi_class);
1503 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
1505 if (capinc_tty_init() < 0) {
1506 device_destroy(capi_class, MKDEV(capi_major, 0));
1507 class_destroy(capi_class);
1508 unregister_chrdev(capi_major, "capi20");
1514 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1515 compileinfo = " (middleware+capifs)";
1516 #elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
1517 compileinfo = " (no capifs)";
1519 compileinfo = " (no middleware)";
1521 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1522 capi_major, compileinfo);
1527 static void __exit capi_exit(void)
1531 device_destroy(capi_class, MKDEV(capi_major, 0));
1532 class_destroy(capi_class);
1533 unregister_chrdev(capi_major, "capi20");
1538 module_init(capi_init);
1539 module_exit(capi_exit);