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_TTYFUNCS /* call to tty_driver */
49 #undef _DEBUG_DATAFLOW /* data flow */
51 /* -------- driver information -------------------------------------- */
53 static struct class *capi_class
;
54 static int capi_major
= 68; /* allocated */
56 module_param_named(major
, capi_major
, uint
, 0);
58 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
59 #define CAPINC_NR_PORTS 32
60 #define CAPINC_MAX_PORTS 256
62 static int capi_ttyminors
= CAPINC_NR_PORTS
;
64 module_param_named(ttyminors
, capi_ttyminors
, uint
, 0);
65 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
67 /* -------- defines ------------------------------------------------- */
69 #define CAPINC_MAX_RECVQUEUE 10
70 #define CAPINC_MAX_SENDQUEUE 10
71 #define CAPI_MAX_BLKSIZE 2048
73 /* -------- data structures ----------------------------------------- */
79 struct ackqueue_entry
{
80 struct list_head list
;
88 struct dentry
*capifs_dentry
;
90 struct capi20_appl
*ap
;
99 struct sk_buff_head inqueue
;
101 struct sk_buff_head outqueue
;
103 struct sk_buff
*outskb
;
107 struct list_head ackqueue
;
113 struct list_head list
;
115 struct capidev
*cdev
;
116 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
117 struct capiminor
*minorp
;
118 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
122 struct list_head list
;
123 struct capi20_appl ap
;
127 struct sk_buff_head recvqueue
;
128 wait_queue_head_t recvwait
;
130 struct list_head nccis
;
135 /* -------- global variables ---------------------------------------- */
137 static DEFINE_MUTEX(capidev_list_lock
);
138 static LIST_HEAD(capidev_list
);
140 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
142 static DEFINE_SPINLOCK(capiminors_lock
);
143 static struct capiminor
**capiminors
;
145 static struct tty_driver
*capinc_tty_driver
;
147 /* -------- datahandles --------------------------------------------- */
149 static int capiminor_add_ack(struct capiminor
*mp
, u16 datahandle
)
151 struct ackqueue_entry
*n
;
153 n
= kmalloc(sizeof(*n
), GFP_ATOMIC
);
155 printk(KERN_ERR
"capi: alloc datahandle failed\n");
158 n
->datahandle
= datahandle
;
159 INIT_LIST_HEAD(&n
->list
);
160 spin_lock_bh(&mp
->ackqlock
);
161 list_add_tail(&n
->list
, &mp
->ackqueue
);
163 spin_unlock_bh(&mp
->ackqlock
);
167 static int capiminor_del_ack(struct capiminor
*mp
, u16 datahandle
)
169 struct ackqueue_entry
*p
, *tmp
;
171 spin_lock_bh(&mp
->ackqlock
);
172 list_for_each_entry_safe(p
, tmp
, &mp
->ackqueue
, list
) {
173 if (p
->datahandle
== datahandle
) {
176 spin_unlock_bh(&mp
->ackqlock
);
181 spin_unlock_bh(&mp
->ackqlock
);
185 static void capiminor_del_all_ack(struct capiminor
*mp
)
187 struct ackqueue_entry
*p
, *tmp
;
189 list_for_each_entry_safe(p
, tmp
, &mp
->ackqueue
, list
) {
197 /* -------- struct capiminor ---------------------------------------- */
199 static const struct tty_port_operations capiminor_port_ops
; /* we have none */
201 static struct capiminor
*capiminor_alloc(struct capi20_appl
*ap
, u32 ncci
)
203 struct capiminor
*mp
;
207 mp
= kzalloc(sizeof(*mp
), GFP_KERNEL
);
209 printk(KERN_ERR
"capi: can't alloc capiminor\n");
213 kref_init(&mp
->kref
);
217 INIT_LIST_HEAD(&mp
->ackqueue
);
218 spin_lock_init(&mp
->ackqlock
);
220 skb_queue_head_init(&mp
->inqueue
);
221 skb_queue_head_init(&mp
->outqueue
);
222 spin_lock_init(&mp
->outlock
);
224 tty_port_init(&mp
->port
);
225 mp
->port
.ops
= &capiminor_port_ops
;
227 /* Allocate the least unused minor number. */
228 spin_lock(&capiminors_lock
);
229 for (minor
= 0; minor
< capi_ttyminors
; minor
++)
230 if (!capiminors
[minor
]) {
231 capiminors
[minor
] = mp
;
234 spin_unlock(&capiminors_lock
);
236 if (minor
== capi_ttyminors
) {
237 printk(KERN_NOTICE
"capi: out of minors\n");
243 dev
= tty_register_device(capinc_tty_driver
, minor
, NULL
);
250 spin_lock(&capiminors_lock
);
251 capiminors
[minor
] = NULL
;
252 spin_unlock(&capiminors_lock
);
259 static void capiminor_destroy(struct kref
*kref
)
261 struct capiminor
*mp
= container_of(kref
, struct capiminor
, kref
);
263 kfree_skb(mp
->outskb
);
264 skb_queue_purge(&mp
->inqueue
);
265 skb_queue_purge(&mp
->outqueue
);
266 capiminor_del_all_ack(mp
);
270 static struct capiminor
*capiminor_get(unsigned int minor
)
272 struct capiminor
*mp
;
274 spin_lock(&capiminors_lock
);
275 mp
= capiminors
[minor
];
278 spin_unlock(&capiminors_lock
);
283 static inline void capiminor_put(struct capiminor
*mp
)
285 kref_put(&mp
->kref
, capiminor_destroy
);
288 static void capiminor_free(struct capiminor
*mp
)
290 tty_unregister_device(capinc_tty_driver
, mp
->minor
);
292 spin_lock(&capiminors_lock
);
293 capiminors
[mp
->minor
] = NULL
;
294 spin_unlock(&capiminors_lock
);
299 /* -------- struct capincci ----------------------------------------- */
301 static void capincci_alloc_minor(struct capidev
*cdev
, struct capincci
*np
)
303 struct capiminor
*mp
;
306 if (!(cdev
->userflags
& CAPIFLAG_HIGHJACKING
))
309 mp
= np
->minorp
= capiminor_alloc(&cdev
->ap
, np
->ncci
);
311 device
= MKDEV(capinc_tty_driver
->major
, mp
->minor
);
312 mp
->capifs_dentry
= capifs_new_ncci(mp
->minor
, device
);
316 static void capincci_free_minor(struct capincci
*np
)
318 struct capiminor
*mp
= np
->minorp
;
319 struct tty_struct
*tty
;
322 capifs_free_ncci(mp
->capifs_dentry
);
324 tty
= tty_port_tty_get(&mp
->port
);
334 static inline unsigned int capincci_minor_opencount(struct capincci
*np
)
336 struct capiminor
*mp
= np
->minorp
;
337 unsigned int count
= 0;
338 struct tty_struct
*tty
;
341 tty
= tty_port_tty_get(&mp
->port
);
350 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
353 capincci_alloc_minor(struct capidev
*cdev
, struct capincci
*np
) { }
354 static inline void capincci_free_minor(struct capincci
*np
) { }
356 static inline unsigned int capincci_minor_opencount(struct capincci
*np
)
361 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
363 static struct capincci
*capincci_alloc(struct capidev
*cdev
, u32 ncci
)
367 np
= kzalloc(sizeof(*np
), GFP_KERNEL
);
373 capincci_alloc_minor(cdev
, np
);
375 list_add_tail(&np
->list
, &cdev
->nccis
);
380 static void capincci_free(struct capidev
*cdev
, u32 ncci
)
382 struct capincci
*np
, *tmp
;
384 list_for_each_entry_safe(np
, tmp
, &cdev
->nccis
, list
)
385 if (ncci
== 0xffffffff || np
->ncci
== ncci
) {
386 capincci_free_minor(np
);
392 static struct capincci
*capincci_find(struct capidev
*cdev
, u32 ncci
)
396 list_for_each_entry(np
, &cdev
->nccis
, list
)
397 if (np
->ncci
== ncci
)
402 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
403 /* -------- handle data queue --------------------------------------- */
405 static struct sk_buff
*
406 gen_data_b3_resp_for(struct capiminor
*mp
, struct sk_buff
*skb
)
408 struct sk_buff
*nskb
;
409 nskb
= alloc_skb(CAPI_DATA_B3_RESP_LEN
, GFP_KERNEL
);
411 u16 datahandle
= CAPIMSG_U16(skb
->data
,CAPIMSG_BASELEN
+4+4+2);
412 unsigned char *s
= skb_put(nskb
, CAPI_DATA_B3_RESP_LEN
);
413 capimsg_setu16(s
, 0, CAPI_DATA_B3_RESP_LEN
);
414 capimsg_setu16(s
, 2, mp
->ap
->applid
);
415 capimsg_setu8 (s
, 4, CAPI_DATA_B3
);
416 capimsg_setu8 (s
, 5, CAPI_RESP
);
417 capimsg_setu16(s
, 6, atomic_inc_return(&mp
->msgid
));
418 capimsg_setu32(s
, 8, mp
->ncci
);
419 capimsg_setu16(s
, 12, datahandle
);
424 static int handle_recv_skb(struct capiminor
*mp
, struct sk_buff
*skb
)
426 unsigned int datalen
= skb
->len
- CAPIMSG_LEN(skb
->data
);
427 struct tty_struct
*tty
;
428 struct sk_buff
*nskb
;
429 u16 errcode
, datahandle
;
430 struct tty_ldisc
*ld
;
433 tty
= tty_port_tty_get(&mp
->port
);
435 #ifdef _DEBUG_DATAFLOW
436 printk(KERN_DEBUG
"capi: currently no receiver\n");
441 ld
= tty_ldisc_ref(tty
);
443 /* fatal error, do not requeue */
449 if (ld
->ops
->receive_buf
== NULL
) {
450 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
451 printk(KERN_DEBUG
"capi: ldisc has no receive_buf function\n");
453 /* fatal error, do not requeue */
457 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
458 printk(KERN_DEBUG
"capi: recv tty throttled\n");
463 if (tty
->receive_room
< datalen
) {
464 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
465 printk(KERN_DEBUG
"capi: no room in tty\n");
470 nskb
= gen_data_b3_resp_for(mp
, skb
);
472 printk(KERN_ERR
"capi: gen_data_b3_resp failed\n");
476 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4);
478 errcode
= capi20_put_message(mp
->ap
, nskb
);
480 if (errcode
== CAPI_NOERROR
) {
481 skb_pull(skb
, CAPIMSG_LEN(skb
->data
));
482 #ifdef _DEBUG_DATAFLOW
483 printk(KERN_DEBUG
"capi: DATA_B3_RESP %u len=%d => ldisc\n",
484 datahandle
, skb
->len
);
486 ld
->ops
->receive_buf(tty
, skb
->data
, NULL
, skb
->len
);
488 printk(KERN_ERR
"capi: send DATA_B3_RESP failed=%x\n",
492 if (errcode
== CAPI_SENDQUEUEFULL
)
508 static void handle_minor_recv(struct capiminor
*mp
)
512 while ((skb
= skb_dequeue(&mp
->inqueue
)) != NULL
)
513 if (handle_recv_skb(mp
, skb
) < 0) {
514 skb_queue_head(&mp
->inqueue
, skb
);
519 static void handle_minor_send(struct capiminor
*mp
)
521 struct tty_struct
*tty
;
527 tty
= tty_port_tty_get(&mp
->port
);
531 if (mp
->ttyoutstop
) {
532 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
533 printk(KERN_DEBUG
"capi: send: tty stopped\n");
540 spin_lock_bh(&mp
->outlock
);
541 skb
= __skb_dequeue(&mp
->outqueue
);
543 spin_unlock_bh(&mp
->outlock
);
548 spin_unlock_bh(&mp
->outlock
);
550 datahandle
= atomic_inc_return(&mp
->datahandle
);
551 skb_push(skb
, CAPI_DATA_B3_REQ_LEN
);
552 memset(skb
->data
, 0, CAPI_DATA_B3_REQ_LEN
);
553 capimsg_setu16(skb
->data
, 0, CAPI_DATA_B3_REQ_LEN
);
554 capimsg_setu16(skb
->data
, 2, mp
->ap
->applid
);
555 capimsg_setu8 (skb
->data
, 4, CAPI_DATA_B3
);
556 capimsg_setu8 (skb
->data
, 5, CAPI_REQ
);
557 capimsg_setu16(skb
->data
, 6, atomic_inc_return(&mp
->msgid
));
558 capimsg_setu32(skb
->data
, 8, mp
->ncci
); /* NCCI */
559 capimsg_setu32(skb
->data
, 12, (u32
)(long)skb
->data
);/* Data32 */
560 capimsg_setu16(skb
->data
, 16, len
); /* Data length */
561 capimsg_setu16(skb
->data
, 18, datahandle
);
562 capimsg_setu16(skb
->data
, 20, 0); /* Flags */
564 if (capiminor_add_ack(mp
, datahandle
) < 0) {
565 skb_pull(skb
, CAPI_DATA_B3_REQ_LEN
);
567 spin_lock_bh(&mp
->outlock
);
568 __skb_queue_head(&mp
->outqueue
, skb
);
570 spin_unlock_bh(&mp
->outlock
);
574 errcode
= capi20_put_message(mp
->ap
, skb
);
575 if (errcode
== CAPI_NOERROR
) {
576 #ifdef _DEBUG_DATAFLOW
577 printk(KERN_DEBUG
"capi: DATA_B3_REQ %u len=%u\n",
582 capiminor_del_ack(mp
, datahandle
);
584 if (errcode
== CAPI_SENDQUEUEFULL
) {
585 skb_pull(skb
, CAPI_DATA_B3_REQ_LEN
);
587 spin_lock_bh(&mp
->outlock
);
588 __skb_queue_head(&mp
->outqueue
, skb
);
590 spin_unlock_bh(&mp
->outlock
);
595 /* ups, drop packet */
596 printk(KERN_ERR
"capi: put_message = %x\n", errcode
);
602 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
603 /* -------- function called by lower level -------------------------- */
605 static void capi_recv_message(struct capi20_appl
*ap
, struct sk_buff
*skb
)
607 struct capidev
*cdev
= ap
->private;
608 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
609 struct tty_struct
*tty
;
610 struct capiminor
*mp
;
612 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
615 mutex_lock(&cdev
->lock
);
617 if (CAPIMSG_CMD(skb
->data
) == CAPI_CONNECT_B3_CONF
) {
618 u16 info
= CAPIMSG_U16(skb
->data
, 12); // Info field
619 if ((info
& 0xff00) == 0)
620 capincci_alloc(cdev
, CAPIMSG_NCCI(skb
->data
));
622 if (CAPIMSG_CMD(skb
->data
) == CAPI_CONNECT_B3_IND
)
623 capincci_alloc(cdev
, CAPIMSG_NCCI(skb
->data
));
625 if (CAPIMSG_COMMAND(skb
->data
) != CAPI_DATA_B3
) {
626 skb_queue_tail(&cdev
->recvqueue
, skb
);
627 wake_up_interruptible(&cdev
->recvwait
);
631 np
= capincci_find(cdev
, CAPIMSG_CONTROL(skb
->data
));
633 printk(KERN_ERR
"BUG: capi_signal: ncci not found\n");
634 skb_queue_tail(&cdev
->recvqueue
, skb
);
635 wake_up_interruptible(&cdev
->recvwait
);
639 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
640 skb_queue_tail(&cdev
->recvqueue
, skb
);
641 wake_up_interruptible(&cdev
->recvwait
);
643 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
647 skb_queue_tail(&cdev
->recvqueue
, skb
);
648 wake_up_interruptible(&cdev
->recvwait
);
651 if (CAPIMSG_SUBCOMMAND(skb
->data
) == CAPI_IND
) {
652 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+4+4+2);
653 #ifdef _DEBUG_DATAFLOW
654 printk(KERN_DEBUG
"capi_signal: DATA_B3_IND %u len=%d\n",
655 datahandle
, skb
->len
-CAPIMSG_LEN(skb
->data
));
657 skb_queue_tail(&mp
->inqueue
, skb
);
659 handle_minor_recv(mp
);
661 } else if (CAPIMSG_SUBCOMMAND(skb
->data
) == CAPI_CONF
) {
663 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+4);
664 #ifdef _DEBUG_DATAFLOW
665 printk(KERN_DEBUG
"capi_signal: DATA_B3_CONF %u 0x%x\n",
667 CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+4+2));
670 capiminor_del_ack(mp
, datahandle
);
671 tty
= tty_port_tty_get(&mp
->port
);
676 handle_minor_send(mp
);
679 /* ups, let capi application handle it :-) */
680 skb_queue_tail(&cdev
->recvqueue
, skb
);
681 wake_up_interruptible(&cdev
->recvwait
);
683 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
686 mutex_unlock(&cdev
->lock
);
689 /* -------- file_operations for capidev ----------------------------- */
692 capi_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
694 struct capidev
*cdev
= (struct capidev
*)file
->private_data
;
699 if (!cdev
->ap
.applid
)
702 skb
= skb_dequeue(&cdev
->recvqueue
);
704 if (file
->f_flags
& O_NONBLOCK
)
706 err
= wait_event_interruptible(cdev
->recvwait
,
707 (skb
= skb_dequeue(&cdev
->recvqueue
)));
711 if (skb
->len
> count
) {
712 skb_queue_head(&cdev
->recvqueue
, skb
);
715 if (copy_to_user(buf
, skb
->data
, skb
->len
)) {
716 skb_queue_head(&cdev
->recvqueue
, skb
);
727 capi_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
729 struct capidev
*cdev
= (struct capidev
*)file
->private_data
;
733 if (!cdev
->ap
.applid
)
736 skb
= alloc_skb(count
, GFP_USER
);
740 if (copy_from_user(skb_put(skb
, count
), buf
, count
)) {
744 mlen
= CAPIMSG_LEN(skb
->data
);
745 if (CAPIMSG_CMD(skb
->data
) == CAPI_DATA_B3_REQ
) {
746 if ((size_t)(mlen
+ CAPIMSG_DATALEN(skb
->data
)) != count
) {
756 CAPIMSG_SETAPPID(skb
->data
, cdev
->ap
.applid
);
758 if (CAPIMSG_CMD(skb
->data
) == CAPI_DISCONNECT_B3_RESP
) {
759 mutex_lock(&cdev
->lock
);
760 capincci_free(cdev
, CAPIMSG_NCCI(skb
->data
));
761 mutex_unlock(&cdev
->lock
);
764 cdev
->errcode
= capi20_put_message(&cdev
->ap
, skb
);
774 capi_poll(struct file
*file
, poll_table
* wait
)
776 struct capidev
*cdev
= (struct capidev
*)file
->private_data
;
777 unsigned int mask
= 0;
779 if (!cdev
->ap
.applid
)
782 poll_wait(file
, &(cdev
->recvwait
), wait
);
783 mask
= POLLOUT
| POLLWRNORM
;
784 if (!skb_queue_empty(&cdev
->recvqueue
))
785 mask
|= POLLIN
| POLLRDNORM
;
790 capi_ioctl(struct inode
*inode
, struct file
*file
,
791 unsigned int cmd
, unsigned long arg
)
793 struct capidev
*cdev
= file
->private_data
;
794 capi_ioctl_struct data
;
795 int retval
= -EINVAL
;
796 void __user
*argp
= (void __user
*)arg
;
800 mutex_lock(&cdev
->lock
);
802 if (cdev
->ap
.applid
) {
806 if (copy_from_user(&cdev
->ap
.rparam
, argp
,
807 sizeof(struct capi_register_params
))) {
811 cdev
->ap
.private = cdev
;
812 cdev
->ap
.recv_message
= capi_recv_message
;
813 cdev
->errcode
= capi20_register(&cdev
->ap
);
814 retval
= (int)cdev
->ap
.applid
;
821 mutex_unlock(&cdev
->lock
);
824 case CAPI_GET_VERSION
:
826 if (copy_from_user(&data
.contr
, argp
,
829 cdev
->errcode
= capi20_get_version(data
.contr
, &data
.version
);
832 if (copy_to_user(argp
, &data
.version
,
833 sizeof(data
.version
)))
838 case CAPI_GET_SERIAL
:
840 if (copy_from_user(&data
.contr
, argp
,
843 cdev
->errcode
= capi20_get_serial (data
.contr
, data
.serial
);
846 if (copy_to_user(argp
, data
.serial
,
847 sizeof(data
.serial
)))
851 case CAPI_GET_PROFILE
:
853 if (copy_from_user(&data
.contr
, argp
,
857 if (data
.contr
== 0) {
858 cdev
->errcode
= capi20_get_profile(data
.contr
, &data
.profile
);
862 retval
= copy_to_user(argp
,
863 &data
.profile
.ncontroller
,
864 sizeof(data
.profile
.ncontroller
));
867 cdev
->errcode
= capi20_get_profile(data
.contr
, &data
.profile
);
871 retval
= copy_to_user(argp
, &data
.profile
,
872 sizeof(data
.profile
));
879 case CAPI_GET_MANUFACTURER
:
881 if (copy_from_user(&data
.contr
, argp
,
884 cdev
->errcode
= capi20_get_manufacturer(data
.contr
, data
.manufacturer
);
888 if (copy_to_user(argp
, data
.manufacturer
,
889 sizeof(data
.manufacturer
)))
894 case CAPI_GET_ERRCODE
:
895 data
.errcode
= cdev
->errcode
;
896 cdev
->errcode
= CAPI_NOERROR
;
898 if (copy_to_user(argp
, &data
.errcode
,
899 sizeof(data
.errcode
)))
905 if (capi20_isinstalled() == CAPI_NOERROR
)
909 case CAPI_MANUFACTURER_CMD
:
911 struct capi_manufacturer_cmd mcmd
;
912 if (!capable(CAP_SYS_ADMIN
))
914 if (copy_from_user(&mcmd
, argp
, sizeof(mcmd
)))
916 return capi20_manufacturer(mcmd
.cmd
, mcmd
.data
);
921 case CAPI_CLR_FLAGS
: {
924 if (copy_from_user(&userflags
, argp
, sizeof(userflags
)))
927 mutex_lock(&cdev
->lock
);
928 if (cmd
== CAPI_SET_FLAGS
)
929 cdev
->userflags
|= userflags
;
931 cdev
->userflags
&= ~userflags
;
932 mutex_unlock(&cdev
->lock
);
936 if (copy_to_user(argp
, &cdev
->userflags
,
937 sizeof(cdev
->userflags
)))
941 case CAPI_NCCI_OPENCOUNT
: {
942 struct capincci
*nccip
;
946 if (copy_from_user(&ncci
, argp
, sizeof(ncci
)))
949 mutex_lock(&cdev
->lock
);
950 nccip
= capincci_find(cdev
, (u32
)ncci
);
952 count
= capincci_minor_opencount(nccip
);
953 mutex_unlock(&cdev
->lock
);
957 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
958 case CAPI_NCCI_GETUNIT
: {
959 struct capincci
*nccip
;
960 struct capiminor
*mp
;
964 if (copy_from_user(&ncci
, argp
, sizeof(ncci
)))
967 mutex_lock(&cdev
->lock
);
968 nccip
= capincci_find(cdev
, (u32
)ncci
);
974 mutex_unlock(&cdev
->lock
);
977 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
984 static int capi_open(struct inode
*inode
, struct file
*file
)
986 struct capidev
*cdev
;
988 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
992 mutex_init(&cdev
->lock
);
993 skb_queue_head_init(&cdev
->recvqueue
);
994 init_waitqueue_head(&cdev
->recvwait
);
995 INIT_LIST_HEAD(&cdev
->nccis
);
996 file
->private_data
= cdev
;
998 mutex_lock(&capidev_list_lock
);
999 list_add_tail(&cdev
->list
, &capidev_list
);
1000 mutex_unlock(&capidev_list_lock
);
1002 return nonseekable_open(inode
, file
);
1005 static int capi_release(struct inode
*inode
, struct file
*file
)
1007 struct capidev
*cdev
= file
->private_data
;
1009 mutex_lock(&capidev_list_lock
);
1010 list_del(&cdev
->list
);
1011 mutex_unlock(&capidev_list_lock
);
1013 if (cdev
->ap
.applid
)
1014 capi20_release(&cdev
->ap
);
1015 skb_queue_purge(&cdev
->recvqueue
);
1016 capincci_free(cdev
, 0xffffffff);
1022 static const struct file_operations capi_fops
=
1024 .owner
= THIS_MODULE
,
1025 .llseek
= no_llseek
,
1027 .write
= capi_write
,
1029 .ioctl
= capi_ioctl
,
1031 .release
= capi_release
,
1034 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1035 /* -------- tty_operations for capincci ----------------------------- */
1038 capinc_tty_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
1040 int idx
= tty
->index
;
1041 struct capiminor
*mp
= capiminor_get(idx
);
1042 int ret
= tty_init_termios(tty
);
1045 tty_driver_kref_get(driver
);
1047 tty
->driver_data
= mp
;
1048 driver
->ttys
[idx
] = tty
;
1054 static void capinc_tty_cleanup(struct tty_struct
*tty
)
1056 struct capiminor
*mp
= tty
->driver_data
;
1057 tty
->driver_data
= NULL
;
1061 static int capinc_tty_open(struct tty_struct
*tty
, struct file
*filp
)
1063 struct capiminor
*mp
= tty
->driver_data
;
1066 err
= tty_port_open(&mp
->port
, tty
, filp
);
1070 handle_minor_recv(mp
);
1074 static void capinc_tty_close(struct tty_struct
*tty
, struct file
*filp
)
1076 struct capiminor
*mp
= tty
->driver_data
;
1078 tty_port_close(&mp
->port
, tty
, filp
);
1081 static int capinc_tty_write(struct tty_struct
*tty
,
1082 const unsigned char *buf
, int count
)
1084 struct capiminor
*mp
= tty
->driver_data
;
1085 struct sk_buff
*skb
;
1087 #ifdef _DEBUG_TTYFUNCS
1088 printk(KERN_DEBUG
"capinc_tty_write(count=%d)\n", count
);
1091 spin_lock_bh(&mp
->outlock
);
1095 __skb_queue_tail(&mp
->outqueue
, skb
);
1096 mp
->outbytes
+= skb
->len
;
1099 skb
= alloc_skb(CAPI_DATA_B3_REQ_LEN
+count
, GFP_ATOMIC
);
1101 printk(KERN_ERR
"capinc_tty_write: alloc_skb failed\n");
1102 spin_unlock_bh(&mp
->outlock
);
1106 skb_reserve(skb
, CAPI_DATA_B3_REQ_LEN
);
1107 memcpy(skb_put(skb
, count
), buf
, count
);
1109 __skb_queue_tail(&mp
->outqueue
, skb
);
1110 mp
->outbytes
+= skb
->len
;
1111 spin_unlock_bh(&mp
->outlock
);
1113 handle_minor_send(mp
);
1118 static int capinc_tty_put_char(struct tty_struct
*tty
, unsigned char ch
)
1120 struct capiminor
*mp
= tty
->driver_data
;
1121 bool invoke_send
= false;
1122 struct sk_buff
*skb
;
1125 #ifdef _DEBUG_TTYFUNCS
1126 printk(KERN_DEBUG
"capinc_put_char(%u)\n", ch
);
1129 spin_lock_bh(&mp
->outlock
);
1132 if (skb_tailroom(skb
) > 0) {
1133 *(skb_put(skb
, 1)) = ch
;
1137 __skb_queue_tail(&mp
->outqueue
, skb
);
1138 mp
->outbytes
+= skb
->len
;
1142 skb
= alloc_skb(CAPI_DATA_B3_REQ_LEN
+CAPI_MAX_BLKSIZE
, GFP_ATOMIC
);
1144 skb_reserve(skb
, CAPI_DATA_B3_REQ_LEN
);
1145 *(skb_put(skb
, 1)) = ch
;
1148 printk(KERN_ERR
"capinc_put_char: char %u lost\n", ch
);
1153 spin_unlock_bh(&mp
->outlock
);
1156 handle_minor_send(mp
);
1161 static void capinc_tty_flush_chars(struct tty_struct
*tty
)
1163 struct capiminor
*mp
= tty
->driver_data
;
1164 struct sk_buff
*skb
;
1166 #ifdef _DEBUG_TTYFUNCS
1167 printk(KERN_DEBUG
"capinc_tty_flush_chars\n");
1170 spin_lock_bh(&mp
->outlock
);
1174 __skb_queue_tail(&mp
->outqueue
, skb
);
1175 mp
->outbytes
+= skb
->len
;
1176 spin_unlock_bh(&mp
->outlock
);
1178 handle_minor_send(mp
);
1180 spin_unlock_bh(&mp
->outlock
);
1182 handle_minor_recv(mp
);
1185 static int capinc_tty_write_room(struct tty_struct
*tty
)
1187 struct capiminor
*mp
= tty
->driver_data
;
1190 room
= CAPINC_MAX_SENDQUEUE
-skb_queue_len(&mp
->outqueue
);
1191 room
*= CAPI_MAX_BLKSIZE
;
1192 #ifdef _DEBUG_TTYFUNCS
1193 printk(KERN_DEBUG
"capinc_tty_write_room = %d\n", room
);
1198 static int capinc_tty_chars_in_buffer(struct tty_struct
*tty
)
1200 struct capiminor
*mp
= tty
->driver_data
;
1202 #ifdef _DEBUG_TTYFUNCS
1203 printk(KERN_DEBUG
"capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1204 mp
->outbytes
, mp
->nack
,
1205 skb_queue_len(&mp
->outqueue
),
1206 skb_queue_len(&mp
->inqueue
));
1208 return mp
->outbytes
;
1211 static int capinc_tty_ioctl(struct tty_struct
*tty
, struct file
* file
,
1212 unsigned int cmd
, unsigned long arg
)
1217 error
= n_tty_ioctl_helper(tty
, file
, cmd
, arg
);
1223 static void capinc_tty_set_termios(struct tty_struct
*tty
, struct ktermios
* old
)
1225 #ifdef _DEBUG_TTYFUNCS
1226 printk(KERN_DEBUG
"capinc_tty_set_termios\n");
1230 static void capinc_tty_throttle(struct tty_struct
*tty
)
1232 struct capiminor
*mp
= tty
->driver_data
;
1233 #ifdef _DEBUG_TTYFUNCS
1234 printk(KERN_DEBUG
"capinc_tty_throttle\n");
1239 static void capinc_tty_unthrottle(struct tty_struct
*tty
)
1241 struct capiminor
*mp
= tty
->driver_data
;
1243 #ifdef _DEBUG_TTYFUNCS
1244 printk(KERN_DEBUG
"capinc_tty_unthrottle\n");
1247 handle_minor_recv(mp
);
1250 static void capinc_tty_stop(struct tty_struct
*tty
)
1252 struct capiminor
*mp
= tty
->driver_data
;
1254 #ifdef _DEBUG_TTYFUNCS
1255 printk(KERN_DEBUG
"capinc_tty_stop\n");
1260 static void capinc_tty_start(struct tty_struct
*tty
)
1262 struct capiminor
*mp
= tty
->driver_data
;
1264 #ifdef _DEBUG_TTYFUNCS
1265 printk(KERN_DEBUG
"capinc_tty_start\n");
1268 handle_minor_send(mp
);
1271 static void capinc_tty_hangup(struct tty_struct
*tty
)
1273 struct capiminor
*mp
= tty
->driver_data
;
1275 #ifdef _DEBUG_TTYFUNCS
1276 printk(KERN_DEBUG
"capinc_tty_hangup\n");
1278 tty_port_hangup(&mp
->port
);
1281 static int capinc_tty_break_ctl(struct tty_struct
*tty
, int state
)
1283 #ifdef _DEBUG_TTYFUNCS
1284 printk(KERN_DEBUG
"capinc_tty_break_ctl(%d)\n", state
);
1289 static void capinc_tty_flush_buffer(struct tty_struct
*tty
)
1291 #ifdef _DEBUG_TTYFUNCS
1292 printk(KERN_DEBUG
"capinc_tty_flush_buffer\n");
1296 static void capinc_tty_set_ldisc(struct tty_struct
*tty
)
1298 #ifdef _DEBUG_TTYFUNCS
1299 printk(KERN_DEBUG
"capinc_tty_set_ldisc\n");
1303 static void capinc_tty_send_xchar(struct tty_struct
*tty
, char ch
)
1305 #ifdef _DEBUG_TTYFUNCS
1306 printk(KERN_DEBUG
"capinc_tty_send_xchar(%d)\n", ch
);
1310 static const struct tty_operations capinc_ops
= {
1311 .open
= capinc_tty_open
,
1312 .close
= capinc_tty_close
,
1313 .write
= capinc_tty_write
,
1314 .put_char
= capinc_tty_put_char
,
1315 .flush_chars
= capinc_tty_flush_chars
,
1316 .write_room
= capinc_tty_write_room
,
1317 .chars_in_buffer
= capinc_tty_chars_in_buffer
,
1318 .ioctl
= capinc_tty_ioctl
,
1319 .set_termios
= capinc_tty_set_termios
,
1320 .throttle
= capinc_tty_throttle
,
1321 .unthrottle
= capinc_tty_unthrottle
,
1322 .stop
= capinc_tty_stop
,
1323 .start
= capinc_tty_start
,
1324 .hangup
= capinc_tty_hangup
,
1325 .break_ctl
= capinc_tty_break_ctl
,
1326 .flush_buffer
= capinc_tty_flush_buffer
,
1327 .set_ldisc
= capinc_tty_set_ldisc
,
1328 .send_xchar
= capinc_tty_send_xchar
,
1329 .install
= capinc_tty_install
,
1330 .cleanup
= capinc_tty_cleanup
,
1333 static int __init
capinc_tty_init(void)
1335 struct tty_driver
*drv
;
1338 if (capi_ttyminors
> CAPINC_MAX_PORTS
)
1339 capi_ttyminors
= CAPINC_MAX_PORTS
;
1340 if (capi_ttyminors
<= 0)
1341 capi_ttyminors
= CAPINC_NR_PORTS
;
1343 capiminors
= kzalloc(sizeof(struct capi_minor
*) * capi_ttyminors
,
1348 drv
= alloc_tty_driver(capi_ttyminors
);
1353 drv
->owner
= THIS_MODULE
;
1354 drv
->driver_name
= "capi_nc";
1357 drv
->minor_start
= 0;
1358 drv
->type
= TTY_DRIVER_TYPE_SERIAL
;
1359 drv
->subtype
= SERIAL_TYPE_NORMAL
;
1360 drv
->init_termios
= tty_std_termios
;
1361 drv
->init_termios
.c_iflag
= ICRNL
;
1362 drv
->init_termios
.c_oflag
= OPOST
| ONLCR
;
1363 drv
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1364 drv
->init_termios
.c_lflag
= 0;
1366 TTY_DRIVER_REAL_RAW
| TTY_DRIVER_RESET_TERMIOS
|
1367 TTY_DRIVER_DYNAMIC_DEV
;
1368 tty_set_operations(drv
, &capinc_ops
);
1370 err
= tty_register_driver(drv
);
1372 put_tty_driver(drv
);
1374 printk(KERN_ERR
"Couldn't register capi_nc driver\n");
1377 capinc_tty_driver
= drv
;
1381 static void __exit
capinc_tty_exit(void)
1383 tty_unregister_driver(capinc_tty_driver
);
1384 put_tty_driver(capinc_tty_driver
);
1388 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1390 static inline int capinc_tty_init(void)
1395 static inline void capinc_tty_exit(void) { }
1397 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1399 /* -------- /proc functions ----------------------------------------- */
1402 * /proc/capi/capi20:
1403 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1405 static int capi20_proc_show(struct seq_file
*m
, void *v
)
1407 struct capidev
*cdev
;
1408 struct list_head
*l
;
1410 mutex_lock(&capidev_list_lock
);
1411 list_for_each(l
, &capidev_list
) {
1412 cdev
= list_entry(l
, struct capidev
, list
);
1413 seq_printf(m
, "0 %d %lu %lu %lu %lu\n",
1415 cdev
->ap
.nrecvctlpkt
,
1416 cdev
->ap
.nrecvdatapkt
,
1417 cdev
->ap
.nsentctlpkt
,
1418 cdev
->ap
.nsentdatapkt
);
1420 mutex_unlock(&capidev_list_lock
);
1424 static int capi20_proc_open(struct inode
*inode
, struct file
*file
)
1426 return single_open(file
, capi20_proc_show
, NULL
);
1429 static const struct file_operations capi20_proc_fops
= {
1430 .owner
= THIS_MODULE
,
1431 .open
= capi20_proc_open
,
1433 .llseek
= seq_lseek
,
1434 .release
= single_release
,
1438 * /proc/capi/capi20ncci:
1441 static int capi20ncci_proc_show(struct seq_file
*m
, void *v
)
1443 struct capidev
*cdev
;
1444 struct capincci
*np
;
1446 mutex_lock(&capidev_list_lock
);
1447 list_for_each_entry(cdev
, &capidev_list
, list
) {
1448 mutex_lock(&cdev
->lock
);
1449 list_for_each_entry(np
, &cdev
->nccis
, list
)
1450 seq_printf(m
, "%d 0x%x\n", cdev
->ap
.applid
, np
->ncci
);
1451 mutex_unlock(&cdev
->lock
);
1453 mutex_unlock(&capidev_list_lock
);
1457 static int capi20ncci_proc_open(struct inode
*inode
, struct file
*file
)
1459 return single_open(file
, capi20ncci_proc_show
, NULL
);
1462 static const struct file_operations capi20ncci_proc_fops
= {
1463 .owner
= THIS_MODULE
,
1464 .open
= capi20ncci_proc_open
,
1466 .llseek
= seq_lseek
,
1467 .release
= single_release
,
1470 static void __init
proc_init(void)
1472 proc_create("capi/capi20", 0, NULL
, &capi20_proc_fops
);
1473 proc_create("capi/capi20ncci", 0, NULL
, &capi20ncci_proc_fops
);
1476 static void __exit
proc_exit(void)
1478 remove_proc_entry("capi/capi20", NULL
);
1479 remove_proc_entry("capi/capi20ncci", NULL
);
1482 /* -------- init function and module interface ---------------------- */
1485 static int __init
capi_init(void)
1487 const char *compileinfo
;
1490 major_ret
= register_chrdev(capi_major
, "capi20", &capi_fops
);
1491 if (major_ret
< 0) {
1492 printk(KERN_ERR
"capi20: unable to get major %d\n", capi_major
);
1495 capi_class
= class_create(THIS_MODULE
, "capi");
1496 if (IS_ERR(capi_class
)) {
1497 unregister_chrdev(capi_major
, "capi20");
1498 return PTR_ERR(capi_class
);
1501 device_create(capi_class
, NULL
, MKDEV(capi_major
, 0), NULL
, "capi");
1503 if (capinc_tty_init() < 0) {
1504 device_destroy(capi_class
, MKDEV(capi_major
, 0));
1505 class_destroy(capi_class
);
1506 unregister_chrdev(capi_major
, "capi20");
1512 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1513 compileinfo
= " (middleware+capifs)";
1514 #elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
1515 compileinfo
= " (no capifs)";
1517 compileinfo
= " (no middleware)";
1519 printk(KERN_NOTICE
"CAPI 2.0 started up with major %d%s\n",
1520 capi_major
, compileinfo
);
1525 static void __exit
capi_exit(void)
1529 device_destroy(capi_class
, MKDEV(capi_major
, 0));
1530 class_destroy(capi_class
);
1531 unregister_chrdev(capi_major
, "capi20");
1536 module_init(capi_init
);
1537 module_exit(capi_exit
);