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/config.h>
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <linux/major.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/fcntl.h>
21 #include <linux/signal.h>
23 #include <linux/smp_lock.h>
24 #include <linux/timer.h>
25 #include <linux/wait.h>
26 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
27 #include <linux/tty.h>
29 #include <linux/netdevice.h>
30 #include <linux/ppp_defs.h>
31 #include <linux/if_ppp.h>
32 #endif /* CONFIG_PPP */
33 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
34 #include <linux/skbuff.h>
35 #include <linux/proc_fs.h>
36 #include <linux/poll.h>
37 #include <linux/capi.h>
38 #include <linux/kernelcapi.h>
39 #include <linux/init.h>
40 #include <linux/device.h>
41 #include <linux/devfs_fs_kernel.h>
42 #include <linux/isdn/capiutil.h>
43 #include <linux/isdn/capicmd.h>
44 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
48 static char *revision = "$Revision: 1.1.2.7 $";
50 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
51 MODULE_AUTHOR("Carsten Paeth");
52 MODULE_LICENSE("GPL");
54 #undef _DEBUG_REFCOUNT /* alloc/free and open/close debug */
55 #undef _DEBUG_TTYFUNCS /* call to tty_driver */
56 #undef _DEBUG_DATAFLOW /* data flow */
58 /* -------- driver information -------------------------------------- */
60 static struct class_simple *capi_class;
62 int capi_major = 68; /* allocated */
63 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
64 #define CAPINC_NR_PORTS 32
65 #define CAPINC_MAX_PORTS 256
66 int capi_ttymajor = 191;
67 int capi_ttyminors = CAPINC_NR_PORTS;
68 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
70 MODULE_PARM(capi_major, "i");
71 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
72 MODULE_PARM(capi_ttymajor, "i");
73 MODULE_PARM(capi_ttyminors, "i");
74 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
76 /* -------- defines ------------------------------------------------- */
78 #define CAPINC_MAX_RECVQUEUE 10
79 #define CAPINC_MAX_SENDQUEUE 10
80 #define CAPI_MAX_BLKSIZE 2048
82 /* -------- data structures ----------------------------------------- */
86 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
90 struct list_head list;
91 struct capincci *nccip;
94 struct capi20_appl *ap;
99 struct tty_struct *tty;
102 struct sk_buff *ttyskb;
103 atomic_t ttyopencount;
105 struct sk_buff_head inqueue;
107 struct sk_buff_head outqueue;
111 struct datahandle_queue {
112 struct datahandle_queue *next;
118 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
121 struct capincci *next;
123 struct capidev *cdev;
124 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
125 struct capiminor *minorp;
126 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
130 struct list_head list;
131 struct capi20_appl ap;
135 struct sk_buff_head recvqueue;
136 wait_queue_head_t recvwait;
138 struct capincci *nccis;
140 struct semaphore ncci_list_sem;
143 /* -------- global variables ---------------------------------------- */
145 static rwlock_t capidev_list_lock = RW_LOCK_UNLOCKED;
146 static LIST_HEAD(capidev_list);
148 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
149 static rwlock_t capiminor_list_lock = RW_LOCK_UNLOCKED;
150 static LIST_HEAD(capiminor_list);
151 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
153 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
154 /* -------- datahandles --------------------------------------------- */
156 static int capincci_add_ack(struct capiminor *mp, u16 datahandle)
158 struct datahandle_queue *n, **pp;
160 n = kmalloc(sizeof(*n), GFP_ATOMIC);
162 printk(KERN_ERR "capi: alloc datahandle failed\n");
166 n->datahandle = datahandle;
167 for (pp = &mp->ackqueue; *pp; pp = &(*pp)->next) ;
173 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
175 struct datahandle_queue **pp, *p;
177 for (pp = &mp->ackqueue; *pp; pp = &(*pp)->next) {
178 if ((*pp)->datahandle == datahandle) {
189 static void capiminor_del_all_ack(struct capiminor *mp)
191 struct datahandle_queue **pp, *p;
203 /* -------- struct capiminor ---------------------------------------- */
205 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
207 struct capiminor *mp, *p;
208 unsigned int minor = 0;
211 mp = kmalloc(sizeof(*mp), GFP_ATOMIC);
213 printk(KERN_ERR "capi: can't alloc capiminor\n");
217 memset(mp, 0, sizeof(struct capiminor));
221 atomic_set(&mp->ttyopencount,0);
223 skb_queue_head_init(&mp->inqueue);
224 skb_queue_head_init(&mp->outqueue);
226 /* Allocate the least unused minor number.
228 write_lock_irqsave(&capiminor_list_lock, flags);
229 if (list_empty(&capiminor_list))
230 list_add(&mp->list, &capiminor_list);
232 list_for_each_entry(p, &capiminor_list, list) {
233 if (p->minor > minor)
238 if (minor < capi_ttyminors) {
240 list_add(&mp->list, p->list.prev);
243 write_unlock_irqrestore(&capiminor_list_lock, flags);
245 if (!(minor < capi_ttyminors)) {
246 printk(KERN_NOTICE "capi: out of minors\n");
254 static void capiminor_free(struct capiminor *mp)
258 write_lock_irqsave(&capiminor_list_lock, flags);
260 write_unlock_irqrestore(&capiminor_list_lock, flags);
262 if (mp->ttyskb) kfree_skb(mp->ttyskb);
264 skb_queue_purge(&mp->inqueue);
265 skb_queue_purge(&mp->outqueue);
266 capiminor_del_all_ack(mp);
270 struct capiminor *capiminor_find(unsigned int minor)
273 struct capiminor *p = NULL;
275 read_lock(&capiminor_list_lock);
276 list_for_each(l, &capiminor_list) {
277 p = list_entry(l, struct capiminor, list);
278 if (p->minor == minor)
281 read_unlock(&capiminor_list_lock);
282 if (l == &capiminor_list)
287 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
289 /* -------- struct capincci ----------------------------------------- */
291 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
293 struct capincci *np, **pp;
294 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
295 struct capiminor *mp = NULL;
296 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
298 np = kmalloc(sizeof(*np), GFP_ATOMIC);
301 memset(np, 0, sizeof(struct capincci));
304 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
306 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
307 mp = np->minorp = capiminor_alloc(&cdev->ap, ncci);
310 #ifdef _DEBUG_REFCOUNT
311 printk(KERN_DEBUG "set mp->nccip\n");
313 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
314 capifs_new_ncci(mp->minor, MKDEV(capi_ttymajor, mp->minor));
317 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
318 for (pp=&cdev->nccis; *pp; pp = &(*pp)->next)
324 static void capincci_free(struct capidev *cdev, u32 ncci)
326 struct capincci *np, **pp;
327 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
328 struct capiminor *mp;
329 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
334 if (ncci == 0xffffffff || np->ncci == ncci) {
336 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
337 if ((mp = np->minorp) != 0) {
338 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
339 capifs_free_ncci(mp->minor);
343 #ifdef _DEBUG_REFCOUNT
344 printk(KERN_DEBUG "reset mp->nccip\n");
351 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
353 if (*pp == 0) return;
360 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
364 for (p=cdev->nccis; p ; p = p->next) {
371 /* -------- struct capidev ------------------------------------------ */
373 static struct capidev *capidev_alloc(void)
375 struct capidev *cdev;
378 cdev = kmalloc(sizeof(*cdev), GFP_KERNEL);
381 memset(cdev, 0, sizeof(struct capidev));
383 init_MUTEX(&cdev->ncci_list_sem);
384 skb_queue_head_init(&cdev->recvqueue);
385 init_waitqueue_head(&cdev->recvwait);
386 write_lock_irqsave(&capidev_list_lock, flags);
387 list_add_tail(&cdev->list, &capidev_list);
388 write_unlock_irqrestore(&capidev_list_lock, flags);
392 static void capidev_free(struct capidev *cdev)
396 if (cdev->ap.applid) {
397 capi20_release(&cdev->ap);
400 skb_queue_purge(&cdev->recvqueue);
402 down(&cdev->ncci_list_sem);
403 capincci_free(cdev, 0xffffffff);
404 up(&cdev->ncci_list_sem);
406 write_lock_irqsave(&capidev_list_lock, flags);
407 list_del(&cdev->list);
408 write_unlock_irqrestore(&capidev_list_lock, flags);
412 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
413 /* -------- handle data queue --------------------------------------- */
415 static struct sk_buff *
416 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
418 struct sk_buff *nskb;
419 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
421 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
422 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
423 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
424 capimsg_setu16(s, 2, mp->ap->applid);
425 capimsg_setu8 (s, 4, CAPI_DATA_B3);
426 capimsg_setu8 (s, 5, CAPI_RESP);
427 capimsg_setu16(s, 6, mp->msgid++);
428 capimsg_setu32(s, 8, mp->ncci);
429 capimsg_setu16(s, 12, datahandle);
434 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
436 struct sk_buff *nskb;
438 u16 errcode, datahandle;
440 datalen = skb->len - CAPIMSG_LEN(skb->data);
442 if (mp->tty->ldisc.receive_buf == 0) {
443 printk(KERN_ERR "capi: ldisc has no receive_buf function\n");
447 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
448 printk(KERN_DEBUG "capi: recv tty throttled\n");
452 if (mp->tty->ldisc.receive_room &&
453 mp->tty->ldisc.receive_room(mp->tty) < datalen) {
454 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
455 printk(KERN_DEBUG "capi: no room in tty\n");
459 if ((nskb = gen_data_b3_resp_for(mp, skb)) == 0) {
460 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
463 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
464 errcode = capi20_put_message(mp->ap, nskb);
465 if (errcode != CAPI_NOERROR) {
466 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
471 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
472 #ifdef _DEBUG_DATAFLOW
473 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
474 datahandle, skb->len);
476 mp->tty->ldisc.receive_buf(mp->tty, skb->data, NULL, skb->len);
481 #ifdef _DEBUG_DATAFLOW
482 printk(KERN_DEBUG "capi: currently no receiver\n");
487 static void handle_minor_recv(struct capiminor *mp)
490 while ((skb = skb_dequeue(&mp->inqueue)) != 0) {
491 unsigned int len = skb->len;
493 if (handle_recv_skb(mp, skb) < 0) {
494 skb_queue_head(&mp->inqueue, skb);
501 static int handle_minor_send(struct capiminor *mp)
509 if (mp->tty && mp->ttyoutstop) {
510 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
511 printk(KERN_DEBUG "capi: send: tty stopped\n");
516 while ((skb = skb_dequeue(&mp->outqueue)) != 0) {
517 datahandle = mp->datahandle;
519 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
520 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
521 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
522 capimsg_setu16(skb->data, 2, mp->ap->applid);
523 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
524 capimsg_setu8 (skb->data, 5, CAPI_REQ);
525 capimsg_setu16(skb->data, 6, mp->msgid++);
526 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
527 capimsg_setu32(skb->data, 12, (u32) skb->data); /* Data32 */
528 capimsg_setu16(skb->data, 16, len); /* Data length */
529 capimsg_setu16(skb->data, 18, datahandle);
530 capimsg_setu16(skb->data, 20, 0); /* Flags */
532 if (capincci_add_ack(mp, datahandle) < 0) {
533 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
534 skb_queue_head(&mp->outqueue, skb);
537 errcode = capi20_put_message(mp->ap, skb);
538 if (errcode == CAPI_NOERROR) {
542 #ifdef _DEBUG_DATAFLOW
543 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
548 capiminor_del_ack(mp, datahandle);
550 if (errcode == CAPI_SENDQUEUEFULL) {
551 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
552 skb_queue_head(&mp->outqueue, skb);
556 /* ups, drop packet */
557 printk(KERN_ERR "capi: put_message = %x\n", errcode);
564 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
565 /* -------- function called by lower level -------------------------- */
567 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
569 struct capidev *cdev = ap->private;
570 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
571 struct capiminor *mp;
573 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
577 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
578 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
580 down(&cdev->ncci_list_sem);
581 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
582 up(&cdev->ncci_list_sem);
585 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND) {
586 down(&cdev->ncci_list_sem);
587 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
588 up(&cdev->ncci_list_sem);
590 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
591 skb_queue_tail(&cdev->recvqueue, skb);
592 wake_up_interruptible(&cdev->recvwait);
595 ncci = CAPIMSG_CONTROL(skb->data);
596 for (np = cdev->nccis; np && np->ncci != ncci; np = np->next)
599 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
600 skb_queue_tail(&cdev->recvqueue, skb);
601 wake_up_interruptible(&cdev->recvwait);
604 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
605 skb_queue_tail(&cdev->recvqueue, skb);
606 wake_up_interruptible(&cdev->recvwait);
607 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
610 skb_queue_tail(&cdev->recvqueue, skb);
611 wake_up_interruptible(&cdev->recvwait);
616 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
617 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
618 #ifdef _DEBUG_DATAFLOW
619 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
620 datahandle, skb->len-CAPIMSG_LEN(skb->data));
622 skb_queue_tail(&mp->inqueue, skb);
623 mp->inbytes += skb->len;
624 handle_minor_recv(mp);
626 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
628 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
629 #ifdef _DEBUG_DATAFLOW
630 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
632 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
635 (void)capiminor_del_ack(mp, datahandle);
637 if (mp->tty->ldisc.write_wakeup)
638 mp->tty->ldisc.write_wakeup(mp->tty);
640 (void)handle_minor_send(mp);
643 /* ups, let capi application handle it :-) */
644 skb_queue_tail(&cdev->recvqueue, skb);
645 wake_up_interruptible(&cdev->recvwait);
647 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
650 /* -------- file_operations for capidev ----------------------------- */
653 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
655 struct capidev *cdev = (struct capidev *)file->private_data;
659 if (!cdev->ap.applid)
662 if ((skb = skb_dequeue(&cdev->recvqueue)) == 0) {
664 if (file->f_flags & O_NONBLOCK)
668 interruptible_sleep_on(&cdev->recvwait);
669 if ((skb = skb_dequeue(&cdev->recvqueue)) != 0)
671 if (signal_pending(current))
675 return -ERESTARTNOHAND;
677 if (skb->len > count) {
678 skb_queue_head(&cdev->recvqueue, skb);
681 if (copy_to_user(buf, skb->data, skb->len)) {
682 skb_queue_head(&cdev->recvqueue, skb);
693 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
695 struct capidev *cdev = (struct capidev *)file->private_data;
699 if (!cdev->ap.applid)
702 skb = alloc_skb(count, GFP_USER);
706 if (copy_from_user(skb_put(skb, count), buf, count)) {
710 mlen = CAPIMSG_LEN(skb->data);
711 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
712 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
722 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
724 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
725 down(&cdev->ncci_list_sem);
726 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
727 up(&cdev->ncci_list_sem);
730 cdev->errcode = capi20_put_message(&cdev->ap, skb);
740 capi_poll(struct file *file, poll_table * wait)
742 struct capidev *cdev = (struct capidev *)file->private_data;
743 unsigned int mask = 0;
745 if (!cdev->ap.applid)
748 poll_wait(file, &(cdev->recvwait), wait);
749 mask = POLLOUT | POLLWRNORM;
750 if (!skb_queue_empty(&cdev->recvqueue))
751 mask |= POLLIN | POLLRDNORM;
756 capi_ioctl(struct inode *inode, struct file *file,
757 unsigned int cmd, unsigned long arg)
759 struct capidev *cdev = file->private_data;
760 struct capi20_appl *ap = &cdev->ap;
761 capi_ioctl_struct data;
762 int retval = -EINVAL;
763 void __user *argp = (void __user *)arg;
771 if (copy_from_user(&cdev->ap.rparam, argp,
772 sizeof(struct capi_register_params)))
775 cdev->ap.private = cdev;
776 cdev->ap.recv_message = capi_recv_message;
777 cdev->errcode = capi20_register(ap);
783 return (int)ap->applid;
785 case CAPI_GET_VERSION:
787 if (copy_from_user(&data.contr, argp,
790 cdev->errcode = capi20_get_version(data.contr, &data.version);
793 if (copy_to_user(argp, &data.version,
794 sizeof(data.version)))
799 case CAPI_GET_SERIAL:
801 if (copy_from_user(&data.contr, argp,
804 cdev->errcode = capi20_get_serial (data.contr, data.serial);
807 if (copy_to_user(argp, data.serial,
808 sizeof(data.serial)))
812 case CAPI_GET_PROFILE:
814 if (copy_from_user(&data.contr, argp,
818 if (data.contr == 0) {
819 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
823 retval = copy_to_user(argp,
824 &data.profile.ncontroller,
825 sizeof(data.profile.ncontroller));
828 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
832 retval = copy_to_user(argp, &data.profile,
833 sizeof(data.profile));
840 case CAPI_GET_MANUFACTURER:
842 if (copy_from_user(&data.contr, argp,
845 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
849 if (copy_to_user(argp, data.manufacturer,
850 sizeof(data.manufacturer)))
855 case CAPI_GET_ERRCODE:
856 data.errcode = cdev->errcode;
857 cdev->errcode = CAPI_NOERROR;
859 if (copy_to_user(argp, &data.errcode,
860 sizeof(data.errcode)))
866 if (capi20_isinstalled() == CAPI_NOERROR)
870 case CAPI_MANUFACTURER_CMD:
872 struct capi_manufacturer_cmd mcmd;
873 if (!capable(CAP_SYS_ADMIN))
875 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
877 return capi20_manufacturer(mcmd.cmd, mcmd.data);
885 if (copy_from_user(&userflags, argp,
888 if (cmd == CAPI_SET_FLAGS)
889 cdev->userflags |= userflags;
891 cdev->userflags &= ~userflags;
896 if (copy_to_user(argp, &cdev->userflags,
897 sizeof(cdev->userflags)))
901 case CAPI_NCCI_OPENCOUNT:
903 struct capincci *nccip;
904 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
905 struct capiminor *mp;
906 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
909 if (copy_from_user(&ncci, argp, sizeof(ncci)))
912 down(&cdev->ncci_list_sem);
913 if ((nccip = capincci_find(cdev, (u32) ncci)) == 0) {
914 up(&cdev->ncci_list_sem);
917 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
918 if ((mp = nccip->minorp) != 0) {
919 count += atomic_read(&mp->ttyopencount);
921 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
922 up(&cdev->ncci_list_sem);
927 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
928 case CAPI_NCCI_GETUNIT:
930 struct capincci *nccip;
931 struct capiminor *mp;
934 if (copy_from_user(&ncci, argp,
937 down(&cdev->ncci_list_sem);
938 nccip = capincci_find(cdev, (u32) ncci);
939 if (!nccip || (mp = nccip->minorp) == 0) {
940 up(&cdev->ncci_list_sem);
944 up(&cdev->ncci_list_sem);
948 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
954 capi_open(struct inode *inode, struct file *file)
956 if (file->private_data)
959 if ((file->private_data = capidev_alloc()) == 0)
962 return nonseekable_open(inode, file);
966 capi_release(struct inode *inode, struct file *file)
968 struct capidev *cdev = (struct capidev *)file->private_data;
971 file->private_data = NULL;
976 static struct file_operations capi_fops =
978 .owner = THIS_MODULE,
985 .release = capi_release,
988 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
989 /* -------- tty_operations for capincci ----------------------------- */
991 static int capinc_tty_open(struct tty_struct * tty, struct file * file)
993 struct capiminor *mp;
995 if ((mp = capiminor_find(iminor(file->f_dentry->d_inode))) == 0)
1000 tty->driver_data = (void *)mp;
1002 if (atomic_read(&mp->ttyopencount) == 0)
1004 atomic_inc(&mp->ttyopencount);
1005 #ifdef _DEBUG_REFCOUNT
1006 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1008 handle_minor_recv(mp);
1012 static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1014 struct capiminor *mp;
1016 mp = (struct capiminor *)tty->driver_data;
1018 if (atomic_dec_and_test(&mp->ttyopencount)) {
1019 #ifdef _DEBUG_REFCOUNT
1020 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1022 tty->driver_data = NULL;
1025 #ifdef _DEBUG_REFCOUNT
1026 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1032 #ifdef _DEBUG_REFCOUNT
1033 printk(KERN_DEBUG "capinc_tty_close\n");
1037 static int capinc_tty_write(struct tty_struct * tty, int from_user,
1038 const unsigned char *buf, int count)
1040 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1041 struct sk_buff *skb;
1044 #ifdef _DEBUG_TTYFUNCS
1045 printk(KERN_DEBUG "capinc_tty_write(from_user=%d,count=%d)\n",
1049 if (!mp || !mp->nccip) {
1050 #ifdef _DEBUG_TTYFUNCS
1051 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1059 skb_queue_tail(&mp->outqueue, skb);
1060 mp->outbytes += skb->len;
1063 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1065 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1069 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1071 retval = copy_from_user(skb_put(skb, count), buf, count);
1074 #ifdef _DEBUG_TTYFUNCS
1075 printk(KERN_DEBUG "capinc_tty_write: copy_from_user=%d\n", retval);
1080 memcpy(skb_put(skb, count), buf, count);
1083 skb_queue_tail(&mp->outqueue, skb);
1084 mp->outbytes += skb->len;
1085 (void)handle_minor_send(mp);
1086 (void)handle_minor_recv(mp);
1090 static void capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1092 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1093 struct sk_buff *skb;
1095 #ifdef _DEBUG_TTYFUNCS
1096 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1099 if (!mp || !mp->nccip) {
1100 #ifdef _DEBUG_TTYFUNCS
1101 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1108 if (skb_tailroom(skb) > 0) {
1109 *(skb_put(skb, 1)) = ch;
1113 skb_queue_tail(&mp->outqueue, skb);
1114 mp->outbytes += skb->len;
1115 (void)handle_minor_send(mp);
1117 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1119 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1120 *(skb_put(skb, 1)) = ch;
1123 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1127 static void capinc_tty_flush_chars(struct tty_struct *tty)
1129 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1130 struct sk_buff *skb;
1132 #ifdef _DEBUG_TTYFUNCS
1133 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1136 if (!mp || !mp->nccip) {
1137 #ifdef _DEBUG_TTYFUNCS
1138 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1146 skb_queue_tail(&mp->outqueue, skb);
1147 mp->outbytes += skb->len;
1148 (void)handle_minor_send(mp);
1150 (void)handle_minor_recv(mp);
1153 static int capinc_tty_write_room(struct tty_struct *tty)
1155 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1157 if (!mp || !mp->nccip) {
1158 #ifdef _DEBUG_TTYFUNCS
1159 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1163 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1164 room *= CAPI_MAX_BLKSIZE;
1165 #ifdef _DEBUG_TTYFUNCS
1166 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1171 int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1173 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1174 if (!mp || !mp->nccip) {
1175 #ifdef _DEBUG_TTYFUNCS
1176 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1180 #ifdef _DEBUG_TTYFUNCS
1181 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1182 mp->outbytes, mp->nack,
1183 skb_queue_len(&mp->outqueue),
1184 skb_queue_len(&mp->inqueue));
1186 return mp->outbytes;
1189 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1190 unsigned int cmd, unsigned long arg)
1195 error = n_tty_ioctl (tty, file, cmd, arg);
1201 static void capinc_tty_set_termios(struct tty_struct *tty, struct termios * old)
1203 #ifdef _DEBUG_TTYFUNCS
1204 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1208 static void capinc_tty_throttle(struct tty_struct * tty)
1210 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1211 #ifdef _DEBUG_TTYFUNCS
1212 printk(KERN_DEBUG "capinc_tty_throttle\n");
1218 static void capinc_tty_unthrottle(struct tty_struct * tty)
1220 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1221 #ifdef _DEBUG_TTYFUNCS
1222 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1226 handle_minor_recv(mp);
1230 static void capinc_tty_stop(struct tty_struct *tty)
1232 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1233 #ifdef _DEBUG_TTYFUNCS
1234 printk(KERN_DEBUG "capinc_tty_stop\n");
1241 static void capinc_tty_start(struct tty_struct *tty)
1243 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1244 #ifdef _DEBUG_TTYFUNCS
1245 printk(KERN_DEBUG "capinc_tty_start\n");
1249 (void)handle_minor_send(mp);
1253 static void capinc_tty_hangup(struct tty_struct *tty)
1255 #ifdef _DEBUG_TTYFUNCS
1256 printk(KERN_DEBUG "capinc_tty_hangup\n");
1260 static void capinc_tty_break_ctl(struct tty_struct *tty, int state)
1262 #ifdef _DEBUG_TTYFUNCS
1263 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1267 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1269 #ifdef _DEBUG_TTYFUNCS
1270 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1274 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1276 #ifdef _DEBUG_TTYFUNCS
1277 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1281 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1283 #ifdef _DEBUG_TTYFUNCS
1284 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1288 static int capinc_tty_read_proc(char *page, char **start, off_t off,
1289 int count, int *eof, void *data)
1294 static struct tty_driver *capinc_tty_driver;
1296 static struct tty_operations capinc_ops = {
1297 .open = capinc_tty_open,
1298 .close = capinc_tty_close,
1299 .write = capinc_tty_write,
1300 .put_char = capinc_tty_put_char,
1301 .flush_chars = capinc_tty_flush_chars,
1302 .write_room = capinc_tty_write_room,
1303 .chars_in_buffer = capinc_tty_chars_in_buffer,
1304 .ioctl = capinc_tty_ioctl,
1305 .set_termios = capinc_tty_set_termios,
1306 .throttle = capinc_tty_throttle,
1307 .unthrottle = capinc_tty_unthrottle,
1308 .stop = capinc_tty_stop,
1309 .start = capinc_tty_start,
1310 .hangup = capinc_tty_hangup,
1311 .break_ctl = capinc_tty_break_ctl,
1312 .flush_buffer = capinc_tty_flush_buffer,
1313 .set_ldisc = capinc_tty_set_ldisc,
1314 .send_xchar = capinc_tty_send_xchar,
1315 .read_proc = capinc_tty_read_proc,
1318 static int capinc_tty_init(void)
1320 struct tty_driver *drv;
1322 if (capi_ttyminors > CAPINC_MAX_PORTS)
1323 capi_ttyminors = CAPINC_MAX_PORTS;
1324 if (capi_ttyminors <= 0)
1325 capi_ttyminors = CAPINC_NR_PORTS;
1327 drv = alloc_tty_driver(capi_ttyminors);
1331 drv->owner = THIS_MODULE;
1332 drv->driver_name = "capi_nc";
1333 drv->devfs_name = "capi/";
1335 drv->major = capi_ttymajor;
1336 drv->minor_start = 0;
1337 drv->type = TTY_DRIVER_TYPE_SERIAL;
1338 drv->subtype = SERIAL_TYPE_NORMAL;
1339 drv->init_termios = tty_std_termios;
1340 drv->init_termios.c_iflag = ICRNL;
1341 drv->init_termios.c_oflag = OPOST | ONLCR;
1342 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1343 drv->init_termios.c_lflag = 0;
1344 drv->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_RESET_TERMIOS;
1345 tty_set_operations(drv, &capinc_ops);
1346 if (tty_register_driver(drv)) {
1347 put_tty_driver(drv);
1348 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1351 capinc_tty_driver = drv;
1355 static void capinc_tty_exit(void)
1357 struct tty_driver *drv = capinc_tty_driver;
1359 if ((retval = tty_unregister_driver(drv)))
1360 printk(KERN_ERR "capi: failed to unregister capi_nc driver (%d)\n", retval);
1361 put_tty_driver(drv);
1364 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1366 /* -------- /proc functions ----------------------------------------- */
1369 * /proc/capi/capi20:
1370 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1372 static int proc_capidev_read_proc(char *page, char **start, off_t off,
1373 int count, int *eof, void *data)
1375 struct capidev *cdev;
1376 struct list_head *l;
1379 read_lock(&capidev_list_lock);
1380 list_for_each(l, &capidev_list) {
1381 cdev = list_entry(l, struct capidev, list);
1382 len += sprintf(page+len, "0 %d %lu %lu %lu %lu\n",
1384 cdev->ap.nrecvctlpkt,
1385 cdev->ap.nrecvdatapkt,
1386 cdev->ap.nsentctlpkt,
1387 cdev->ap.nsentdatapkt);
1392 if (len-off > count)
1398 read_unlock(&capidev_list_lock);
1401 if (len > count) len = count;
1402 if (len < 0) len = 0;
1407 * /proc/capi/capi20ncci:
1410 static int proc_capincci_read_proc(char *page, char **start, off_t off,
1411 int count, int *eof, void *data)
1413 struct capidev *cdev;
1414 struct capincci *np;
1415 struct list_head *l;
1418 read_lock(&capidev_list_lock);
1419 list_for_each(l, &capidev_list) {
1420 cdev = list_entry(l, struct capidev, list);
1421 for (np=cdev->nccis; np; np = np->next) {
1422 len += sprintf(page+len, "%d 0x%x\n",
1429 if (len-off > count)
1435 read_unlock(&capidev_list_lock);
1439 if (len>count) len = count;
1444 static struct procfsentries {
1447 int (*read_proc)(char *page, char **start, off_t off,
1448 int count, int *eof, void *data);
1449 struct proc_dir_entry *procent;
1450 } procfsentries[] = {
1451 /* { "capi", S_IFDIR, 0 }, */
1452 { "capi/capi20", 0 , proc_capidev_read_proc },
1453 { "capi/capi20ncci", 0 , proc_capincci_read_proc },
1456 static void __init proc_init(void)
1458 int nelem = sizeof(procfsentries)/sizeof(procfsentries[0]);
1461 for (i=0; i < nelem; i++) {
1462 struct procfsentries *p = procfsentries + i;
1463 p->procent = create_proc_entry(p->name, p->mode, NULL);
1464 if (p->procent) p->procent->read_proc = p->read_proc;
1468 static void __exit proc_exit(void)
1470 int nelem = sizeof(procfsentries)/sizeof(procfsentries[0]);
1473 for (i=nelem-1; i >= 0; i--) {
1474 struct procfsentries *p = procfsentries + i;
1476 remove_proc_entry(p->name, NULL);
1482 /* -------- init function and module interface ---------------------- */
1485 static char rev[32];
1487 static int __init capi_init(void)
1492 if ((p = strchr(revision, ':')) != 0 && p[1]) {
1493 strlcpy(rev, p + 2, sizeof(rev));
1494 if ((p = strchr(rev, '$')) != 0 && p > rev)
1499 if (register_chrdev(capi_major, "capi20", &capi_fops)) {
1500 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1504 capi_class = class_simple_create(THIS_MODULE, "capi");
1505 if (IS_ERR(capi_class)) {
1506 unregister_chrdev(capi_major, "capi20");
1507 return PTR_ERR(capi_class);
1510 class_simple_device_add(capi_class, MKDEV(capi_major, 0), NULL, "capi");
1511 devfs_mk_cdev(MKDEV(capi_major, 0), S_IFCHR | S_IRUSR | S_IWUSR,
1514 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1515 if (capinc_tty_init() < 0) {
1516 class_simple_device_remove(MKDEV(capi_major, 0));
1517 class_simple_destroy(capi_class);
1518 unregister_chrdev(capi_major, "capi20");
1521 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1525 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1526 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1527 compileinfo = " (middleware+capifs)";
1529 compileinfo = " (no capifs)";
1532 compileinfo = " (no middleware)";
1534 printk(KERN_NOTICE "capi20: Rev %s: started up with major %d%s\n",
1535 rev, capi_major, compileinfo);
1540 static void __exit capi_exit(void)
1544 class_simple_device_remove(MKDEV(capi_major, 0));
1545 class_simple_destroy(capi_class);
1546 unregister_chrdev(capi_major, "capi20");
1547 devfs_remove("isdn/capi20");
1549 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1552 printk(KERN_NOTICE "capi: Rev %s: unloaded\n", rev);
1555 module_init(capi_init);
1556 module_exit(capi_exit);