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/smp_lock.h>
23 #include <linux/timer.h>
24 #include <linux/wait.h>
25 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
26 #include <linux/tty.h>
28 #include <linux/netdevice.h>
29 #include <linux/ppp_defs.h>
30 #include <linux/if_ppp.h>
31 #endif /* CONFIG_PPP */
32 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
33 #include <linux/skbuff.h>
34 #include <linux/proc_fs.h>
35 #include <linux/poll.h>
36 #include <linux/capi.h>
37 #include <linux/kernelcapi.h>
38 #include <linux/init.h>
39 #include <linux/device.h>
40 #include <linux/moduleparam.h>
41 #include <linux/isdn/capiutil.h>
42 #include <linux/isdn/capicmd.h>
43 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
47 static char *revision = "$Revision: 1.1.2.7 $";
49 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
50 MODULE_AUTHOR("Carsten Paeth");
51 MODULE_LICENSE("GPL");
53 #undef _DEBUG_REFCOUNT /* alloc/free and open/close debug */
54 #undef _DEBUG_TTYFUNCS /* call to tty_driver */
55 #undef _DEBUG_DATAFLOW /* data flow */
57 /* -------- driver information -------------------------------------- */
59 static struct class *capi_class;
61 static int capi_major = 68; /* allocated */
62 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
63 #define CAPINC_NR_PORTS 32
64 #define CAPINC_MAX_PORTS 256
65 static int capi_ttymajor = 191;
66 static int capi_ttyminors = CAPINC_NR_PORTS;
67 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
69 module_param_named(major, capi_major, uint, 0);
70 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
71 module_param_named(ttymajor, capi_ttymajor, uint, 0);
72 module_param_named(ttyminors, capi_ttyminors, uint, 0);
73 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
75 /* -------- defines ------------------------------------------------- */
77 #define CAPINC_MAX_RECVQUEUE 10
78 #define CAPINC_MAX_SENDQUEUE 10
79 #define CAPI_MAX_BLKSIZE 2048
81 /* -------- data structures ----------------------------------------- */
85 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
88 struct datahandle_queue {
89 struct list_head list;
94 struct list_head list;
95 struct capincci *nccip;
98 struct capi20_appl *ap;
103 struct tty_struct *tty;
106 struct sk_buff *ttyskb;
107 atomic_t ttyopencount;
109 struct sk_buff_head inqueue;
111 struct sk_buff_head outqueue;
115 struct list_head ackqueue;
119 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
122 struct capincci *next;
124 struct capidev *cdev;
125 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
126 struct capiminor *minorp;
127 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
131 struct list_head list;
132 struct capi20_appl ap;
136 struct sk_buff_head recvqueue;
137 wait_queue_head_t recvwait;
139 struct capincci *nccis;
141 struct semaphore ncci_list_sem;
144 /* -------- global variables ---------------------------------------- */
146 static DEFINE_RWLOCK(capidev_list_lock);
147 static LIST_HEAD(capidev_list);
149 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
150 static DEFINE_RWLOCK(capiminor_list_lock);
151 static LIST_HEAD(capiminor_list);
152 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
154 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
155 /* -------- datahandles --------------------------------------------- */
157 static int capincci_add_ack(struct capiminor *mp, u16 datahandle)
159 struct datahandle_queue *n;
162 n = kmalloc(sizeof(*n), GFP_ATOMIC);
164 printk(KERN_ERR "capi: alloc datahandle failed\n");
167 n->datahandle = datahandle;
168 INIT_LIST_HEAD(&n->list);
169 spin_lock_irqsave(&mp->ackqlock, flags);
170 list_add_tail(&n->list, &mp->ackqueue);
172 spin_unlock_irqrestore(&mp->ackqlock, flags);
176 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
178 struct datahandle_queue *p, *tmp;
181 spin_lock_irqsave(&mp->ackqlock, flags);
182 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
183 if (p->datahandle == datahandle) {
187 spin_unlock_irqrestore(&mp->ackqlock, flags);
191 spin_unlock_irqrestore(&mp->ackqlock, flags);
195 static void capiminor_del_all_ack(struct capiminor *mp)
197 struct datahandle_queue *p, *tmp;
200 spin_lock_irqsave(&mp->ackqlock, flags);
201 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
206 spin_unlock_irqrestore(&mp->ackqlock, flags);
210 /* -------- struct capiminor ---------------------------------------- */
212 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
214 struct capiminor *mp, *p;
215 unsigned int minor = 0;
218 mp = kmalloc(sizeof(*mp), GFP_ATOMIC);
220 printk(KERN_ERR "capi: can't alloc capiminor\n");
224 memset(mp, 0, sizeof(struct capiminor));
228 atomic_set(&mp->ttyopencount,0);
229 INIT_LIST_HEAD(&mp->ackqueue);
230 spin_lock_init(&mp->ackqlock);
232 skb_queue_head_init(&mp->inqueue);
233 skb_queue_head_init(&mp->outqueue);
235 /* Allocate the least unused minor number.
237 write_lock_irqsave(&capiminor_list_lock, flags);
238 if (list_empty(&capiminor_list))
239 list_add(&mp->list, &capiminor_list);
241 list_for_each_entry(p, &capiminor_list, list) {
242 if (p->minor > minor)
247 if (minor < capi_ttyminors) {
249 list_add(&mp->list, p->list.prev);
252 write_unlock_irqrestore(&capiminor_list_lock, flags);
254 if (!(minor < capi_ttyminors)) {
255 printk(KERN_NOTICE "capi: out of minors\n");
263 static void capiminor_free(struct capiminor *mp)
267 write_lock_irqsave(&capiminor_list_lock, flags);
269 write_unlock_irqrestore(&capiminor_list_lock, flags);
271 if (mp->ttyskb) kfree_skb(mp->ttyskb);
273 skb_queue_purge(&mp->inqueue);
274 skb_queue_purge(&mp->outqueue);
275 capiminor_del_all_ack(mp);
279 static struct capiminor *capiminor_find(unsigned int minor)
282 struct capiminor *p = NULL;
284 read_lock(&capiminor_list_lock);
285 list_for_each(l, &capiminor_list) {
286 p = list_entry(l, struct capiminor, list);
287 if (p->minor == minor)
290 read_unlock(&capiminor_list_lock);
291 if (l == &capiminor_list)
296 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
298 /* -------- struct capincci ----------------------------------------- */
300 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
302 struct capincci *np, **pp;
303 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
304 struct capiminor *mp = NULL;
305 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
307 np = kmalloc(sizeof(*np), GFP_ATOMIC);
310 memset(np, 0, sizeof(struct capincci));
313 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
315 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
316 mp = np->minorp = capiminor_alloc(&cdev->ap, ncci);
319 #ifdef _DEBUG_REFCOUNT
320 printk(KERN_DEBUG "set mp->nccip\n");
322 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
323 capifs_new_ncci(mp->minor, MKDEV(capi_ttymajor, mp->minor));
326 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
327 for (pp=&cdev->nccis; *pp; pp = &(*pp)->next)
333 static void capincci_free(struct capidev *cdev, u32 ncci)
335 struct capincci *np, **pp;
336 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
337 struct capiminor *mp;
338 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
343 if (ncci == 0xffffffff || np->ncci == ncci) {
345 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
346 if ((mp = np->minorp) != 0) {
347 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
348 capifs_free_ncci(mp->minor);
352 #ifdef _DEBUG_REFCOUNT
353 printk(KERN_DEBUG "reset mp->nccip\n");
360 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
362 if (*pp == 0) return;
369 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
373 for (p=cdev->nccis; p ; p = p->next) {
380 /* -------- struct capidev ------------------------------------------ */
382 static struct capidev *capidev_alloc(void)
384 struct capidev *cdev;
387 cdev = kmalloc(sizeof(*cdev), GFP_KERNEL);
390 memset(cdev, 0, sizeof(struct capidev));
392 init_MUTEX(&cdev->ncci_list_sem);
393 skb_queue_head_init(&cdev->recvqueue);
394 init_waitqueue_head(&cdev->recvwait);
395 write_lock_irqsave(&capidev_list_lock, flags);
396 list_add_tail(&cdev->list, &capidev_list);
397 write_unlock_irqrestore(&capidev_list_lock, flags);
401 static void capidev_free(struct capidev *cdev)
405 if (cdev->ap.applid) {
406 capi20_release(&cdev->ap);
409 skb_queue_purge(&cdev->recvqueue);
411 down(&cdev->ncci_list_sem);
412 capincci_free(cdev, 0xffffffff);
413 up(&cdev->ncci_list_sem);
415 write_lock_irqsave(&capidev_list_lock, flags);
416 list_del(&cdev->list);
417 write_unlock_irqrestore(&capidev_list_lock, flags);
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 sk_buff *nskb;
447 u16 errcode, datahandle;
448 struct tty_ldisc *ld;
450 datalen = skb->len - CAPIMSG_LEN(skb->data);
453 #ifdef _DEBUG_DATAFLOW
454 printk(KERN_DEBUG "capi: currently no receiver\n");
459 ld = tty_ldisc_ref(mp->tty);
462 if (ld->receive_buf == NULL) {
463 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
464 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
469 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
470 printk(KERN_DEBUG "capi: recv tty throttled\n");
474 if (mp->tty->receive_room < datalen) {
475 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
476 printk(KERN_DEBUG "capi: no room in tty\n");
480 if ((nskb = gen_data_b3_resp_for(mp, skb)) == 0) {
481 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
484 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
485 errcode = capi20_put_message(mp->ap, nskb);
486 if (errcode != CAPI_NOERROR) {
487 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
492 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
493 #ifdef _DEBUG_DATAFLOW
494 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
495 datahandle, skb->len);
497 ld->receive_buf(mp->tty, skb->data, NULL, skb->len);
506 static void handle_minor_recv(struct capiminor *mp)
509 while ((skb = skb_dequeue(&mp->inqueue)) != 0) {
510 unsigned int len = skb->len;
512 if (handle_recv_skb(mp, skb) < 0) {
513 skb_queue_head(&mp->inqueue, skb);
520 static int handle_minor_send(struct capiminor *mp)
528 if (mp->tty && mp->ttyoutstop) {
529 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
530 printk(KERN_DEBUG "capi: send: tty stopped\n");
535 while ((skb = skb_dequeue(&mp->outqueue)) != 0) {
536 datahandle = mp->datahandle;
538 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
539 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
540 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
541 capimsg_setu16(skb->data, 2, mp->ap->applid);
542 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
543 capimsg_setu8 (skb->data, 5, CAPI_REQ);
544 capimsg_setu16(skb->data, 6, mp->msgid++);
545 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
546 capimsg_setu32(skb->data, 12, (u32) skb->data); /* Data32 */
547 capimsg_setu16(skb->data, 16, len); /* Data length */
548 capimsg_setu16(skb->data, 18, datahandle);
549 capimsg_setu16(skb->data, 20, 0); /* Flags */
551 if (capincci_add_ack(mp, datahandle) < 0) {
552 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
553 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);
583 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
584 /* -------- function called by lower level -------------------------- */
586 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
588 struct capidev *cdev = ap->private;
589 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
590 struct capiminor *mp;
592 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
596 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
597 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
599 down(&cdev->ncci_list_sem);
600 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
601 up(&cdev->ncci_list_sem);
604 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND) {
605 down(&cdev->ncci_list_sem);
606 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
607 up(&cdev->ncci_list_sem);
609 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
610 skb_queue_tail(&cdev->recvqueue, skb);
611 wake_up_interruptible(&cdev->recvwait);
614 ncci = CAPIMSG_CONTROL(skb->data);
615 for (np = cdev->nccis; np && np->ncci != ncci; np = np->next)
618 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
619 skb_queue_tail(&cdev->recvqueue, skb);
620 wake_up_interruptible(&cdev->recvwait);
623 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
624 skb_queue_tail(&cdev->recvqueue, skb);
625 wake_up_interruptible(&cdev->recvwait);
626 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
629 skb_queue_tail(&cdev->recvqueue, skb);
630 wake_up_interruptible(&cdev->recvwait);
635 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
637 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
638 #ifdef _DEBUG_DATAFLOW
639 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
640 datahandle, skb->len-CAPIMSG_LEN(skb->data));
642 skb_queue_tail(&mp->inqueue, skb);
643 mp->inbytes += skb->len;
644 handle_minor_recv(mp);
646 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
648 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
649 #ifdef _DEBUG_DATAFLOW
650 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
652 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
655 (void)capiminor_del_ack(mp, datahandle);
658 (void)handle_minor_send(mp);
661 /* ups, let capi application handle it :-) */
662 skb_queue_tail(&cdev->recvqueue, skb);
663 wake_up_interruptible(&cdev->recvwait);
665 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
668 /* -------- file_operations for capidev ----------------------------- */
671 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
673 struct capidev *cdev = (struct capidev *)file->private_data;
677 if (!cdev->ap.applid)
680 if ((skb = skb_dequeue(&cdev->recvqueue)) == 0) {
682 if (file->f_flags & O_NONBLOCK)
686 interruptible_sleep_on(&cdev->recvwait);
687 if ((skb = skb_dequeue(&cdev->recvqueue)) != 0)
689 if (signal_pending(current))
693 return -ERESTARTNOHAND;
695 if (skb->len > count) {
696 skb_queue_head(&cdev->recvqueue, skb);
699 if (copy_to_user(buf, skb->data, skb->len)) {
700 skb_queue_head(&cdev->recvqueue, skb);
711 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
713 struct capidev *cdev = (struct capidev *)file->private_data;
717 if (!cdev->ap.applid)
720 skb = alloc_skb(count, GFP_USER);
724 if (copy_from_user(skb_put(skb, count), buf, count)) {
728 mlen = CAPIMSG_LEN(skb->data);
729 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
730 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
740 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
742 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
743 down(&cdev->ncci_list_sem);
744 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
745 up(&cdev->ncci_list_sem);
748 cdev->errcode = capi20_put_message(&cdev->ap, skb);
758 capi_poll(struct file *file, poll_table * wait)
760 struct capidev *cdev = (struct capidev *)file->private_data;
761 unsigned int mask = 0;
763 if (!cdev->ap.applid)
766 poll_wait(file, &(cdev->recvwait), wait);
767 mask = POLLOUT | POLLWRNORM;
768 if (!skb_queue_empty(&cdev->recvqueue))
769 mask |= POLLIN | POLLRDNORM;
774 capi_ioctl(struct inode *inode, struct file *file,
775 unsigned int cmd, unsigned long arg)
777 struct capidev *cdev = file->private_data;
778 struct capi20_appl *ap = &cdev->ap;
779 capi_ioctl_struct data;
780 int retval = -EINVAL;
781 void __user *argp = (void __user *)arg;
789 if (copy_from_user(&cdev->ap.rparam, argp,
790 sizeof(struct capi_register_params)))
793 cdev->ap.private = cdev;
794 cdev->ap.recv_message = capi_recv_message;
795 cdev->errcode = capi20_register(ap);
801 return (int)ap->applid;
803 case CAPI_GET_VERSION:
805 if (copy_from_user(&data.contr, argp,
808 cdev->errcode = capi20_get_version(data.contr, &data.version);
811 if (copy_to_user(argp, &data.version,
812 sizeof(data.version)))
817 case CAPI_GET_SERIAL:
819 if (copy_from_user(&data.contr, argp,
822 cdev->errcode = capi20_get_serial (data.contr, data.serial);
825 if (copy_to_user(argp, data.serial,
826 sizeof(data.serial)))
830 case CAPI_GET_PROFILE:
832 if (copy_from_user(&data.contr, argp,
836 if (data.contr == 0) {
837 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
841 retval = copy_to_user(argp,
842 &data.profile.ncontroller,
843 sizeof(data.profile.ncontroller));
846 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
850 retval = copy_to_user(argp, &data.profile,
851 sizeof(data.profile));
858 case CAPI_GET_MANUFACTURER:
860 if (copy_from_user(&data.contr, argp,
863 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
867 if (copy_to_user(argp, data.manufacturer,
868 sizeof(data.manufacturer)))
873 case CAPI_GET_ERRCODE:
874 data.errcode = cdev->errcode;
875 cdev->errcode = CAPI_NOERROR;
877 if (copy_to_user(argp, &data.errcode,
878 sizeof(data.errcode)))
884 if (capi20_isinstalled() == CAPI_NOERROR)
888 case CAPI_MANUFACTURER_CMD:
890 struct capi_manufacturer_cmd mcmd;
891 if (!capable(CAP_SYS_ADMIN))
893 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
895 return capi20_manufacturer(mcmd.cmd, mcmd.data);
903 if (copy_from_user(&userflags, argp,
906 if (cmd == CAPI_SET_FLAGS)
907 cdev->userflags |= userflags;
909 cdev->userflags &= ~userflags;
914 if (copy_to_user(argp, &cdev->userflags,
915 sizeof(cdev->userflags)))
919 case CAPI_NCCI_OPENCOUNT:
921 struct capincci *nccip;
922 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
923 struct capiminor *mp;
924 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
927 if (copy_from_user(&ncci, argp, sizeof(ncci)))
930 down(&cdev->ncci_list_sem);
931 if ((nccip = capincci_find(cdev, (u32) ncci)) == 0) {
932 up(&cdev->ncci_list_sem);
935 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
936 if ((mp = nccip->minorp) != 0) {
937 count += atomic_read(&mp->ttyopencount);
939 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
940 up(&cdev->ncci_list_sem);
945 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
946 case CAPI_NCCI_GETUNIT:
948 struct capincci *nccip;
949 struct capiminor *mp;
952 if (copy_from_user(&ncci, argp,
955 down(&cdev->ncci_list_sem);
956 nccip = capincci_find(cdev, (u32) ncci);
957 if (!nccip || (mp = nccip->minorp) == 0) {
958 up(&cdev->ncci_list_sem);
962 up(&cdev->ncci_list_sem);
966 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
972 capi_open(struct inode *inode, struct file *file)
974 if (file->private_data)
977 if ((file->private_data = capidev_alloc()) == 0)
980 return nonseekable_open(inode, file);
984 capi_release(struct inode *inode, struct file *file)
986 struct capidev *cdev = (struct capidev *)file->private_data;
989 file->private_data = NULL;
994 static struct file_operations capi_fops =
996 .owner = THIS_MODULE,
1001 .ioctl = capi_ioctl,
1003 .release = capi_release,
1006 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1007 /* -------- tty_operations for capincci ----------------------------- */
1009 static int capinc_tty_open(struct tty_struct * tty, struct file * file)
1011 struct capiminor *mp;
1013 if ((mp = capiminor_find(iminor(file->f_dentry->d_inode))) == 0)
1018 tty->driver_data = (void *)mp;
1020 if (atomic_read(&mp->ttyopencount) == 0)
1022 atomic_inc(&mp->ttyopencount);
1023 #ifdef _DEBUG_REFCOUNT
1024 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1026 handle_minor_recv(mp);
1030 static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1032 struct capiminor *mp;
1034 mp = (struct capiminor *)tty->driver_data;
1036 if (atomic_dec_and_test(&mp->ttyopencount)) {
1037 #ifdef _DEBUG_REFCOUNT
1038 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1040 tty->driver_data = NULL;
1043 #ifdef _DEBUG_REFCOUNT
1044 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1050 #ifdef _DEBUG_REFCOUNT
1051 printk(KERN_DEBUG "capinc_tty_close\n");
1055 static int capinc_tty_write(struct tty_struct * tty,
1056 const unsigned char *buf, int count)
1058 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1059 struct sk_buff *skb;
1061 #ifdef _DEBUG_TTYFUNCS
1062 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1065 if (!mp || !mp->nccip) {
1066 #ifdef _DEBUG_TTYFUNCS
1067 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1075 skb_queue_tail(&mp->outqueue, skb);
1076 mp->outbytes += skb->len;
1079 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1081 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1085 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1086 memcpy(skb_put(skb, count), buf, count);
1088 skb_queue_tail(&mp->outqueue, skb);
1089 mp->outbytes += skb->len;
1090 (void)handle_minor_send(mp);
1091 (void)handle_minor_recv(mp);
1095 static void capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1097 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1098 struct sk_buff *skb;
1100 #ifdef _DEBUG_TTYFUNCS
1101 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1104 if (!mp || !mp->nccip) {
1105 #ifdef _DEBUG_TTYFUNCS
1106 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1113 if (skb_tailroom(skb) > 0) {
1114 *(skb_put(skb, 1)) = ch;
1118 skb_queue_tail(&mp->outqueue, skb);
1119 mp->outbytes += skb->len;
1120 (void)handle_minor_send(mp);
1122 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1124 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1125 *(skb_put(skb, 1)) = ch;
1128 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1132 static void capinc_tty_flush_chars(struct tty_struct *tty)
1134 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1135 struct sk_buff *skb;
1137 #ifdef _DEBUG_TTYFUNCS
1138 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1141 if (!mp || !mp->nccip) {
1142 #ifdef _DEBUG_TTYFUNCS
1143 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1151 skb_queue_tail(&mp->outqueue, skb);
1152 mp->outbytes += skb->len;
1153 (void)handle_minor_send(mp);
1155 (void)handle_minor_recv(mp);
1158 static int capinc_tty_write_room(struct tty_struct *tty)
1160 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1162 if (!mp || !mp->nccip) {
1163 #ifdef _DEBUG_TTYFUNCS
1164 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1168 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1169 room *= CAPI_MAX_BLKSIZE;
1170 #ifdef _DEBUG_TTYFUNCS
1171 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1176 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1178 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1179 if (!mp || !mp->nccip) {
1180 #ifdef _DEBUG_TTYFUNCS
1181 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1185 #ifdef _DEBUG_TTYFUNCS
1186 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1187 mp->outbytes, mp->nack,
1188 skb_queue_len(&mp->outqueue),
1189 skb_queue_len(&mp->inqueue));
1191 return mp->outbytes;
1194 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1195 unsigned int cmd, unsigned long arg)
1200 error = n_tty_ioctl (tty, file, cmd, arg);
1206 static void capinc_tty_set_termios(struct tty_struct *tty, struct termios * old)
1208 #ifdef _DEBUG_TTYFUNCS
1209 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1213 static void capinc_tty_throttle(struct tty_struct * tty)
1215 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1216 #ifdef _DEBUG_TTYFUNCS
1217 printk(KERN_DEBUG "capinc_tty_throttle\n");
1223 static void capinc_tty_unthrottle(struct tty_struct * tty)
1225 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1226 #ifdef _DEBUG_TTYFUNCS
1227 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1231 handle_minor_recv(mp);
1235 static void capinc_tty_stop(struct tty_struct *tty)
1237 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1238 #ifdef _DEBUG_TTYFUNCS
1239 printk(KERN_DEBUG "capinc_tty_stop\n");
1246 static void capinc_tty_start(struct tty_struct *tty)
1248 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1249 #ifdef _DEBUG_TTYFUNCS
1250 printk(KERN_DEBUG "capinc_tty_start\n");
1254 (void)handle_minor_send(mp);
1258 static void capinc_tty_hangup(struct tty_struct *tty)
1260 #ifdef _DEBUG_TTYFUNCS
1261 printk(KERN_DEBUG "capinc_tty_hangup\n");
1265 static void capinc_tty_break_ctl(struct tty_struct *tty, int state)
1267 #ifdef _DEBUG_TTYFUNCS
1268 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1272 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1274 #ifdef _DEBUG_TTYFUNCS
1275 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1279 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1281 #ifdef _DEBUG_TTYFUNCS
1282 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1286 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1288 #ifdef _DEBUG_TTYFUNCS
1289 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1293 static int capinc_tty_read_proc(char *page, char **start, off_t off,
1294 int count, int *eof, void *data)
1299 static struct tty_driver *capinc_tty_driver;
1301 static struct tty_operations capinc_ops = {
1302 .open = capinc_tty_open,
1303 .close = capinc_tty_close,
1304 .write = capinc_tty_write,
1305 .put_char = capinc_tty_put_char,
1306 .flush_chars = capinc_tty_flush_chars,
1307 .write_room = capinc_tty_write_room,
1308 .chars_in_buffer = capinc_tty_chars_in_buffer,
1309 .ioctl = capinc_tty_ioctl,
1310 .set_termios = capinc_tty_set_termios,
1311 .throttle = capinc_tty_throttle,
1312 .unthrottle = capinc_tty_unthrottle,
1313 .stop = capinc_tty_stop,
1314 .start = capinc_tty_start,
1315 .hangup = capinc_tty_hangup,
1316 .break_ctl = capinc_tty_break_ctl,
1317 .flush_buffer = capinc_tty_flush_buffer,
1318 .set_ldisc = capinc_tty_set_ldisc,
1319 .send_xchar = capinc_tty_send_xchar,
1320 .read_proc = capinc_tty_read_proc,
1323 static int capinc_tty_init(void)
1325 struct tty_driver *drv;
1327 if (capi_ttyminors > CAPINC_MAX_PORTS)
1328 capi_ttyminors = CAPINC_MAX_PORTS;
1329 if (capi_ttyminors <= 0)
1330 capi_ttyminors = CAPINC_NR_PORTS;
1332 drv = alloc_tty_driver(capi_ttyminors);
1336 drv->owner = THIS_MODULE;
1337 drv->driver_name = "capi_nc";
1339 drv->major = capi_ttymajor;
1340 drv->minor_start = 0;
1341 drv->type = TTY_DRIVER_TYPE_SERIAL;
1342 drv->subtype = SERIAL_TYPE_NORMAL;
1343 drv->init_termios = tty_std_termios;
1344 drv->init_termios.c_iflag = ICRNL;
1345 drv->init_termios.c_oflag = OPOST | ONLCR;
1346 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1347 drv->init_termios.c_lflag = 0;
1348 drv->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_RESET_TERMIOS;
1349 tty_set_operations(drv, &capinc_ops);
1350 if (tty_register_driver(drv)) {
1351 put_tty_driver(drv);
1352 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1355 capinc_tty_driver = drv;
1359 static void capinc_tty_exit(void)
1361 struct tty_driver *drv = capinc_tty_driver;
1363 if ((retval = tty_unregister_driver(drv)))
1364 printk(KERN_ERR "capi: failed to unregister capi_nc driver (%d)\n", retval);
1365 put_tty_driver(drv);
1368 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1370 /* -------- /proc functions ----------------------------------------- */
1373 * /proc/capi/capi20:
1374 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1376 static int proc_capidev_read_proc(char *page, char **start, off_t off,
1377 int count, int *eof, void *data)
1379 struct capidev *cdev;
1380 struct list_head *l;
1383 read_lock(&capidev_list_lock);
1384 list_for_each(l, &capidev_list) {
1385 cdev = list_entry(l, struct capidev, list);
1386 len += sprintf(page+len, "0 %d %lu %lu %lu %lu\n",
1388 cdev->ap.nrecvctlpkt,
1389 cdev->ap.nrecvdatapkt,
1390 cdev->ap.nsentctlpkt,
1391 cdev->ap.nsentdatapkt);
1396 if (len-off > count)
1402 read_unlock(&capidev_list_lock);
1405 if (len > count) len = count;
1406 if (len < 0) len = 0;
1411 * /proc/capi/capi20ncci:
1414 static int proc_capincci_read_proc(char *page, char **start, off_t off,
1415 int count, int *eof, void *data)
1417 struct capidev *cdev;
1418 struct capincci *np;
1419 struct list_head *l;
1422 read_lock(&capidev_list_lock);
1423 list_for_each(l, &capidev_list) {
1424 cdev = list_entry(l, struct capidev, list);
1425 for (np=cdev->nccis; np; np = np->next) {
1426 len += sprintf(page+len, "%d 0x%x\n",
1433 if (len-off > count)
1439 read_unlock(&capidev_list_lock);
1443 if (len>count) len = count;
1448 static struct procfsentries {
1451 int (*read_proc)(char *page, char **start, off_t off,
1452 int count, int *eof, void *data);
1453 struct proc_dir_entry *procent;
1454 } procfsentries[] = {
1455 /* { "capi", S_IFDIR, 0 }, */
1456 { "capi/capi20", 0 , proc_capidev_read_proc },
1457 { "capi/capi20ncci", 0 , proc_capincci_read_proc },
1460 static void __init proc_init(void)
1462 int nelem = sizeof(procfsentries)/sizeof(procfsentries[0]);
1465 for (i=0; i < nelem; i++) {
1466 struct procfsentries *p = procfsentries + i;
1467 p->procent = create_proc_entry(p->name, p->mode, NULL);
1468 if (p->procent) p->procent->read_proc = p->read_proc;
1472 static void __exit proc_exit(void)
1474 int nelem = sizeof(procfsentries)/sizeof(procfsentries[0]);
1477 for (i=nelem-1; i >= 0; i--) {
1478 struct procfsentries *p = procfsentries + i;
1480 remove_proc_entry(p->name, NULL);
1486 /* -------- init function and module interface ---------------------- */
1489 static char rev[32];
1491 static int __init capi_init(void)
1497 if ((p = strchr(revision, ':')) != 0 && p[1]) {
1498 strlcpy(rev, p + 2, sizeof(rev));
1499 if ((p = strchr(rev, '$')) != 0 && p > rev)
1504 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1505 if (major_ret < 0) {
1506 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1509 capi_class = class_create(THIS_MODULE, "capi");
1510 if (IS_ERR(capi_class)) {
1511 unregister_chrdev(capi_major, "capi20");
1512 return PTR_ERR(capi_class);
1515 class_device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
1517 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1518 if (capinc_tty_init() < 0) {
1519 class_device_destroy(capi_class, MKDEV(capi_major, 0));
1520 class_destroy(capi_class);
1521 unregister_chrdev(capi_major, "capi20");
1524 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1528 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1529 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1530 compileinfo = " (middleware+capifs)";
1532 compileinfo = " (no capifs)";
1535 compileinfo = " (no middleware)";
1537 printk(KERN_NOTICE "capi20: Rev %s: started up with major %d%s\n",
1538 rev, capi_major, compileinfo);
1543 static void __exit capi_exit(void)
1547 class_device_destroy(capi_class, MKDEV(capi_major, 0));
1548 class_destroy(capi_class);
1549 unregister_chrdev(capi_major, "capi20");
1551 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1554 printk(KERN_NOTICE "capi: Rev %s: unloaded\n", rev);
1557 module_init(capi_init);
1558 module_exit(capi_exit);