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/compiler.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>
22 #include <linux/mutex.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/ppp-ioctl.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>
42 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
43 MODULE_AUTHOR("Carsten Paeth");
44 MODULE_LICENSE("GPL");
46 /* -------- driver information -------------------------------------- */
48 static DEFINE_MUTEX(capi_mutex
);
49 static struct class *capi_class
;
50 static int capi_major
= 68; /* allocated */
52 module_param_named(major
, capi_major
, uint
, 0);
54 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
55 #define CAPINC_NR_PORTS 32
56 #define CAPINC_MAX_PORTS 256
58 static int capi_ttyminors
= CAPINC_NR_PORTS
;
60 module_param_named(ttyminors
, capi_ttyminors
, uint
, 0);
61 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
63 /* -------- defines ------------------------------------------------- */
65 #define CAPINC_MAX_RECVQUEUE 10
66 #define CAPINC_MAX_SENDQUEUE 10
67 #define CAPI_MAX_BLKSIZE 2048
69 /* -------- data structures ----------------------------------------- */
75 struct ackqueue_entry
{
76 struct list_head list
;
83 struct capi20_appl
*ap
;
92 struct sk_buff_head inqueue
;
94 struct sk_buff_head outqueue
;
96 struct sk_buff
*outskb
;
100 struct list_head ackqueue
;
106 struct list_head list
;
108 struct capidev
*cdev
;
109 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
110 struct capiminor
*minorp
;
111 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
115 struct list_head list
;
116 struct capi20_appl ap
;
120 struct sk_buff_head recvqueue
;
121 wait_queue_head_t recvwait
;
123 struct list_head nccis
;
128 /* -------- global variables ---------------------------------------- */
130 static DEFINE_MUTEX(capidev_list_lock
);
131 static LIST_HEAD(capidev_list
);
133 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
135 static DEFINE_SPINLOCK(capiminors_lock
);
136 static struct capiminor
**capiminors
;
138 static struct tty_driver
*capinc_tty_driver
;
140 /* -------- datahandles --------------------------------------------- */
142 static int capiminor_add_ack(struct capiminor
*mp
, u16 datahandle
)
144 struct ackqueue_entry
*n
;
146 n
= kmalloc(sizeof(*n
), GFP_ATOMIC
);
148 printk(KERN_ERR
"capi: alloc datahandle failed\n");
151 n
->datahandle
= datahandle
;
152 INIT_LIST_HEAD(&n
->list
);
153 spin_lock_bh(&mp
->ackqlock
);
154 list_add_tail(&n
->list
, &mp
->ackqueue
);
156 spin_unlock_bh(&mp
->ackqlock
);
160 static int capiminor_del_ack(struct capiminor
*mp
, u16 datahandle
)
162 struct ackqueue_entry
*p
, *tmp
;
164 spin_lock_bh(&mp
->ackqlock
);
165 list_for_each_entry_safe(p
, tmp
, &mp
->ackqueue
, list
) {
166 if (p
->datahandle
== datahandle
) {
169 spin_unlock_bh(&mp
->ackqlock
);
174 spin_unlock_bh(&mp
->ackqlock
);
178 static void capiminor_del_all_ack(struct capiminor
*mp
)
180 struct ackqueue_entry
*p
, *tmp
;
182 list_for_each_entry_safe(p
, tmp
, &mp
->ackqueue
, list
) {
190 /* -------- struct capiminor ---------------------------------------- */
192 static void capiminor_destroy(struct tty_port
*port
)
194 struct capiminor
*mp
= container_of(port
, struct capiminor
, port
);
196 kfree_skb(mp
->outskb
);
197 skb_queue_purge(&mp
->inqueue
);
198 skb_queue_purge(&mp
->outqueue
);
199 capiminor_del_all_ack(mp
);
203 static const struct tty_port_operations capiminor_port_ops
= {
204 .destruct
= capiminor_destroy
,
207 static struct capiminor
*capiminor_alloc(struct capi20_appl
*ap
, u32 ncci
)
209 struct capiminor
*mp
;
213 mp
= kzalloc(sizeof(*mp
), GFP_KERNEL
);
215 printk(KERN_ERR
"capi: can't alloc capiminor\n");
221 INIT_LIST_HEAD(&mp
->ackqueue
);
222 spin_lock_init(&mp
->ackqlock
);
224 skb_queue_head_init(&mp
->inqueue
);
225 skb_queue_head_init(&mp
->outqueue
);
226 spin_lock_init(&mp
->outlock
);
228 tty_port_init(&mp
->port
);
229 mp
->port
.ops
= &capiminor_port_ops
;
231 /* Allocate the least unused minor number. */
232 spin_lock(&capiminors_lock
);
233 for (minor
= 0; minor
< capi_ttyminors
; minor
++)
234 if (!capiminors
[minor
]) {
235 capiminors
[minor
] = mp
;
238 spin_unlock(&capiminors_lock
);
240 if (minor
== capi_ttyminors
) {
241 printk(KERN_NOTICE
"capi: out of minors\n");
247 dev
= tty_port_register_device(&mp
->port
, capinc_tty_driver
, minor
,
255 spin_lock(&capiminors_lock
);
256 capiminors
[minor
] = NULL
;
257 spin_unlock(&capiminors_lock
);
260 tty_port_put(&mp
->port
);
264 static struct capiminor
*capiminor_get(unsigned int minor
)
266 struct capiminor
*mp
;
268 spin_lock(&capiminors_lock
);
269 mp
= capiminors
[minor
];
271 tty_port_get(&mp
->port
);
272 spin_unlock(&capiminors_lock
);
277 static inline void capiminor_put(struct capiminor
*mp
)
279 tty_port_put(&mp
->port
);
282 static void capiminor_free(struct capiminor
*mp
)
284 tty_unregister_device(capinc_tty_driver
, mp
->minor
);
286 spin_lock(&capiminors_lock
);
287 capiminors
[mp
->minor
] = NULL
;
288 spin_unlock(&capiminors_lock
);
293 /* -------- struct capincci ----------------------------------------- */
295 static void capincci_alloc_minor(struct capidev
*cdev
, struct capincci
*np
)
297 if (cdev
->userflags
& CAPIFLAG_HIGHJACKING
)
298 np
->minorp
= capiminor_alloc(&cdev
->ap
, np
->ncci
);
301 static void capincci_free_minor(struct capincci
*np
)
303 struct capiminor
*mp
= np
->minorp
;
304 struct tty_struct
*tty
;
307 tty
= tty_port_tty_get(&mp
->port
);
317 static inline unsigned int capincci_minor_opencount(struct capincci
*np
)
319 struct capiminor
*mp
= np
->minorp
;
320 unsigned int count
= 0;
321 struct tty_struct
*tty
;
324 tty
= tty_port_tty_get(&mp
->port
);
333 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
336 capincci_alloc_minor(struct capidev
*cdev
, struct capincci
*np
) { }
337 static inline void capincci_free_minor(struct capincci
*np
) { }
339 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
341 static struct capincci
*capincci_alloc(struct capidev
*cdev
, u32 ncci
)
345 np
= kzalloc(sizeof(*np
), GFP_KERNEL
);
351 capincci_alloc_minor(cdev
, np
);
353 list_add_tail(&np
->list
, &cdev
->nccis
);
358 static void capincci_free(struct capidev
*cdev
, u32 ncci
)
360 struct capincci
*np
, *tmp
;
362 list_for_each_entry_safe(np
, tmp
, &cdev
->nccis
, list
)
363 if (ncci
== 0xffffffff || np
->ncci
== ncci
) {
364 capincci_free_minor(np
);
370 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
371 static struct capincci
*capincci_find(struct capidev
*cdev
, u32 ncci
)
375 list_for_each_entry(np
, &cdev
->nccis
, list
)
376 if (np
->ncci
== ncci
)
381 /* -------- handle data queue --------------------------------------- */
383 static struct sk_buff
*
384 gen_data_b3_resp_for(struct capiminor
*mp
, struct sk_buff
*skb
)
386 struct sk_buff
*nskb
;
387 nskb
= alloc_skb(CAPI_DATA_B3_RESP_LEN
, GFP_KERNEL
);
389 u16 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4 + 4 + 2);
390 unsigned char *s
= skb_put(nskb
, CAPI_DATA_B3_RESP_LEN
);
391 capimsg_setu16(s
, 0, CAPI_DATA_B3_RESP_LEN
);
392 capimsg_setu16(s
, 2, mp
->ap
->applid
);
393 capimsg_setu8 (s
, 4, CAPI_DATA_B3
);
394 capimsg_setu8 (s
, 5, CAPI_RESP
);
395 capimsg_setu16(s
, 6, atomic_inc_return(&mp
->msgid
));
396 capimsg_setu32(s
, 8, mp
->ncci
);
397 capimsg_setu16(s
, 12, datahandle
);
402 static int handle_recv_skb(struct capiminor
*mp
, struct sk_buff
*skb
)
404 unsigned int datalen
= skb
->len
- CAPIMSG_LEN(skb
->data
);
405 struct tty_struct
*tty
;
406 struct sk_buff
*nskb
;
407 u16 errcode
, datahandle
;
408 struct tty_ldisc
*ld
;
411 tty
= tty_port_tty_get(&mp
->port
);
413 pr_debug("capi: currently no receiver\n");
417 ld
= tty_ldisc_ref(tty
);
419 /* fatal error, do not requeue */
425 if (ld
->ops
->receive_buf
== NULL
) {
426 pr_debug("capi: ldisc has no receive_buf function\n");
427 /* fatal error, do not requeue */
431 pr_debug("capi: recv tty throttled\n");
435 if (tty
->receive_room
< datalen
) {
436 pr_debug("capi: no room in tty\n");
440 nskb
= gen_data_b3_resp_for(mp
, skb
);
442 printk(KERN_ERR
"capi: gen_data_b3_resp failed\n");
446 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4);
448 errcode
= capi20_put_message(mp
->ap
, nskb
);
450 if (errcode
== CAPI_NOERROR
) {
451 skb_pull(skb
, CAPIMSG_LEN(skb
->data
));
452 pr_debug("capi: DATA_B3_RESP %u len=%d => ldisc\n",
453 datahandle
, skb
->len
);
454 ld
->ops
->receive_buf(tty
, skb
->data
, NULL
, skb
->len
);
456 printk(KERN_ERR
"capi: send DATA_B3_RESP failed=%x\n",
460 if (errcode
== CAPI_SENDQUEUEFULL
)
476 static void handle_minor_recv(struct capiminor
*mp
)
480 while ((skb
= skb_dequeue(&mp
->inqueue
)) != NULL
)
481 if (handle_recv_skb(mp
, skb
) < 0) {
482 skb_queue_head(&mp
->inqueue
, skb
);
487 static void handle_minor_send(struct capiminor
*mp
)
489 struct tty_struct
*tty
;
495 tty
= tty_port_tty_get(&mp
->port
);
499 if (mp
->ttyoutstop
) {
500 pr_debug("capi: send: tty stopped\n");
506 spin_lock_bh(&mp
->outlock
);
507 skb
= __skb_dequeue(&mp
->outqueue
);
509 spin_unlock_bh(&mp
->outlock
);
514 spin_unlock_bh(&mp
->outlock
);
516 datahandle
= atomic_inc_return(&mp
->datahandle
);
517 skb_push(skb
, CAPI_DATA_B3_REQ_LEN
);
518 memset(skb
->data
, 0, CAPI_DATA_B3_REQ_LEN
);
519 capimsg_setu16(skb
->data
, 0, CAPI_DATA_B3_REQ_LEN
);
520 capimsg_setu16(skb
->data
, 2, mp
->ap
->applid
);
521 capimsg_setu8 (skb
->data
, 4, CAPI_DATA_B3
);
522 capimsg_setu8 (skb
->data
, 5, CAPI_REQ
);
523 capimsg_setu16(skb
->data
, 6, atomic_inc_return(&mp
->msgid
));
524 capimsg_setu32(skb
->data
, 8, mp
->ncci
); /* NCCI */
525 capimsg_setu32(skb
->data
, 12, (u32
)(long)skb
->data
);/* Data32 */
526 capimsg_setu16(skb
->data
, 16, len
); /* Data length */
527 capimsg_setu16(skb
->data
, 18, datahandle
);
528 capimsg_setu16(skb
->data
, 20, 0); /* Flags */
530 if (capiminor_add_ack(mp
, datahandle
) < 0) {
531 skb_pull(skb
, CAPI_DATA_B3_REQ_LEN
);
533 spin_lock_bh(&mp
->outlock
);
534 __skb_queue_head(&mp
->outqueue
, skb
);
536 spin_unlock_bh(&mp
->outlock
);
540 errcode
= capi20_put_message(mp
->ap
, skb
);
541 if (errcode
== CAPI_NOERROR
) {
542 pr_debug("capi: DATA_B3_REQ %u len=%u\n",
546 capiminor_del_ack(mp
, datahandle
);
548 if (errcode
== CAPI_SENDQUEUEFULL
) {
549 skb_pull(skb
, CAPI_DATA_B3_REQ_LEN
);
551 spin_lock_bh(&mp
->outlock
);
552 __skb_queue_head(&mp
->outqueue
, skb
);
554 spin_unlock_bh(&mp
->outlock
);
559 /* ups, drop packet */
560 printk(KERN_ERR
"capi: put_message = %x\n", errcode
);
566 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
567 /* -------- function called by lower level -------------------------- */
569 static void capi_recv_message(struct capi20_appl
*ap
, struct sk_buff
*skb
)
571 struct capidev
*cdev
= ap
->private;
572 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
573 struct capiminor
*mp
;
576 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
578 mutex_lock(&cdev
->lock
);
580 if (CAPIMSG_CMD(skb
->data
) == CAPI_CONNECT_B3_CONF
) {
581 u16 info
= CAPIMSG_U16(skb
->data
, 12); // Info field
582 if ((info
& 0xff00) == 0)
583 capincci_alloc(cdev
, CAPIMSG_NCCI(skb
->data
));
585 if (CAPIMSG_CMD(skb
->data
) == CAPI_CONNECT_B3_IND
)
586 capincci_alloc(cdev
, CAPIMSG_NCCI(skb
->data
));
588 if (CAPIMSG_COMMAND(skb
->data
) != CAPI_DATA_B3
) {
589 skb_queue_tail(&cdev
->recvqueue
, skb
);
590 wake_up_interruptible(&cdev
->recvwait
);
594 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
595 skb_queue_tail(&cdev
->recvqueue
, skb
);
596 wake_up_interruptible(&cdev
->recvwait
);
598 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
600 np
= capincci_find(cdev
, CAPIMSG_CONTROL(skb
->data
));
602 printk(KERN_ERR
"BUG: capi_signal: ncci not found\n");
603 skb_queue_tail(&cdev
->recvqueue
, skb
);
604 wake_up_interruptible(&cdev
->recvwait
);
610 skb_queue_tail(&cdev
->recvqueue
, skb
);
611 wake_up_interruptible(&cdev
->recvwait
);
614 if (CAPIMSG_SUBCOMMAND(skb
->data
) == CAPI_IND
) {
615 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4 + 4 + 2);
616 pr_debug("capi_signal: DATA_B3_IND %u len=%d\n",
617 datahandle
, skb
->len
-CAPIMSG_LEN(skb
->data
));
618 skb_queue_tail(&mp
->inqueue
, skb
);
620 handle_minor_recv(mp
);
622 } else if (CAPIMSG_SUBCOMMAND(skb
->data
) == CAPI_CONF
) {
624 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4);
625 pr_debug("capi_signal: DATA_B3_CONF %u 0x%x\n",
627 CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4 + 2));
629 capiminor_del_ack(mp
, datahandle
);
630 tty_port_tty_wakeup(&mp
->port
);
631 handle_minor_send(mp
);
634 /* ups, let capi application handle it :-) */
635 skb_queue_tail(&cdev
->recvqueue
, skb
);
636 wake_up_interruptible(&cdev
->recvwait
);
638 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
641 mutex_unlock(&cdev
->lock
);
644 /* -------- file_operations for capidev ----------------------------- */
647 capi_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
649 struct capidev
*cdev
= file
->private_data
;
654 if (!cdev
->ap
.applid
)
657 skb
= skb_dequeue(&cdev
->recvqueue
);
659 if (file
->f_flags
& O_NONBLOCK
)
661 err
= wait_event_interruptible(cdev
->recvwait
,
662 (skb
= skb_dequeue(&cdev
->recvqueue
)));
666 if (skb
->len
> count
) {
667 skb_queue_head(&cdev
->recvqueue
, skb
);
670 if (copy_to_user(buf
, skb
->data
, skb
->len
)) {
671 skb_queue_head(&cdev
->recvqueue
, skb
);
682 capi_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
684 struct capidev
*cdev
= file
->private_data
;
688 if (!cdev
->ap
.applid
)
691 if (count
< CAPIMSG_BASELEN
)
694 skb
= alloc_skb(count
, GFP_USER
);
698 if (copy_from_user(skb_put(skb
, count
), buf
, count
)) {
702 mlen
= CAPIMSG_LEN(skb
->data
);
703 if (CAPIMSG_CMD(skb
->data
) == CAPI_DATA_B3_REQ
) {
704 if (count
< CAPI_DATA_B3_REQ_LEN
||
705 (size_t)(mlen
+ CAPIMSG_DATALEN(skb
->data
)) != count
) {
715 CAPIMSG_SETAPPID(skb
->data
, cdev
->ap
.applid
);
717 if (CAPIMSG_CMD(skb
->data
) == CAPI_DISCONNECT_B3_RESP
) {
718 if (count
< CAPI_DISCONNECT_B3_RESP_LEN
) {
722 mutex_lock(&cdev
->lock
);
723 capincci_free(cdev
, CAPIMSG_NCCI(skb
->data
));
724 mutex_unlock(&cdev
->lock
);
727 cdev
->errcode
= capi20_put_message(&cdev
->ap
, skb
);
737 capi_poll(struct file
*file
, poll_table
*wait
)
739 struct capidev
*cdev
= file
->private_data
;
742 if (!cdev
->ap
.applid
)
745 poll_wait(file
, &(cdev
->recvwait
), wait
);
746 mask
= EPOLLOUT
| EPOLLWRNORM
;
747 if (!skb_queue_empty_lockless(&cdev
->recvqueue
))
748 mask
|= EPOLLIN
| EPOLLRDNORM
;
753 capi_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
755 struct capidev
*cdev
= file
->private_data
;
756 capi_ioctl_struct data
;
757 int retval
= -EINVAL
;
758 void __user
*argp
= (void __user
*)arg
;
762 mutex_lock(&cdev
->lock
);
764 if (cdev
->ap
.applid
) {
768 if (copy_from_user(&cdev
->ap
.rparam
, argp
,
769 sizeof(struct capi_register_params
))) {
773 cdev
->ap
.private = cdev
;
774 cdev
->ap
.recv_message
= capi_recv_message
;
775 cdev
->errcode
= capi20_register(&cdev
->ap
);
776 retval
= (int)cdev
->ap
.applid
;
783 mutex_unlock(&cdev
->lock
);
786 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
)))
798 case CAPI_GET_SERIAL
:
799 if (copy_from_user(&data
.contr
, argp
,
802 cdev
->errcode
= capi20_get_serial(data
.contr
, data
.serial
);
805 if (copy_to_user(argp
, data
.serial
,
806 sizeof(data
.serial
)))
810 case CAPI_GET_PROFILE
:
811 if (copy_from_user(&data
.contr
, argp
,
815 if (data
.contr
== 0) {
816 cdev
->errcode
= capi20_get_profile(data
.contr
, &data
.profile
);
820 retval
= copy_to_user(argp
,
821 &data
.profile
.ncontroller
,
822 sizeof(data
.profile
.ncontroller
));
825 cdev
->errcode
= capi20_get_profile(data
.contr
, &data
.profile
);
829 retval
= copy_to_user(argp
, &data
.profile
,
830 sizeof(data
.profile
));
836 case CAPI_GET_MANUFACTURER
:
837 if (copy_from_user(&data
.contr
, argp
,
840 cdev
->errcode
= capi20_get_manufacturer(data
.contr
, data
.manufacturer
);
844 if (copy_to_user(argp
, data
.manufacturer
,
845 sizeof(data
.manufacturer
)))
850 case CAPI_GET_ERRCODE
:
851 data
.errcode
= cdev
->errcode
;
852 cdev
->errcode
= CAPI_NOERROR
;
854 if (copy_to_user(argp
, &data
.errcode
,
855 sizeof(data
.errcode
)))
861 if (capi20_isinstalled() == CAPI_NOERROR
)
865 case CAPI_MANUFACTURER_CMD
: {
866 struct capi_manufacturer_cmd mcmd
;
867 if (!capable(CAP_SYS_ADMIN
))
869 if (copy_from_user(&mcmd
, argp
, sizeof(mcmd
)))
871 return capi20_manufacturer(mcmd
.cmd
, mcmd
.data
);
874 case CAPI_CLR_FLAGS
: {
877 if (copy_from_user(&userflags
, argp
, sizeof(userflags
)))
880 mutex_lock(&cdev
->lock
);
881 if (cmd
== CAPI_SET_FLAGS
)
882 cdev
->userflags
|= userflags
;
884 cdev
->userflags
&= ~userflags
;
885 mutex_unlock(&cdev
->lock
);
889 if (copy_to_user(argp
, &cdev
->userflags
,
890 sizeof(cdev
->userflags
)))
894 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
895 case CAPI_NCCI_OPENCOUNT
:
898 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
899 case CAPI_NCCI_OPENCOUNT
: {
900 struct capincci
*nccip
;
904 if (copy_from_user(&ncci
, argp
, sizeof(ncci
)))
907 mutex_lock(&cdev
->lock
);
908 nccip
= capincci_find(cdev
, (u32
)ncci
);
910 count
= capincci_minor_opencount(nccip
);
911 mutex_unlock(&cdev
->lock
);
915 case CAPI_NCCI_GETUNIT
: {
916 struct capincci
*nccip
;
917 struct capiminor
*mp
;
921 if (copy_from_user(&ncci
, argp
, sizeof(ncci
)))
924 mutex_lock(&cdev
->lock
);
925 nccip
= capincci_find(cdev
, (u32
)ncci
);
931 mutex_unlock(&cdev
->lock
);
934 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
942 capi_unlocked_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
946 mutex_lock(&capi_mutex
);
947 ret
= capi_ioctl(file
, cmd
, arg
);
948 mutex_unlock(&capi_mutex
);
953 static int capi_open(struct inode
*inode
, struct file
*file
)
955 struct capidev
*cdev
;
957 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
961 mutex_init(&cdev
->lock
);
962 skb_queue_head_init(&cdev
->recvqueue
);
963 init_waitqueue_head(&cdev
->recvwait
);
964 INIT_LIST_HEAD(&cdev
->nccis
);
965 file
->private_data
= cdev
;
967 mutex_lock(&capidev_list_lock
);
968 list_add_tail(&cdev
->list
, &capidev_list
);
969 mutex_unlock(&capidev_list_lock
);
971 return nonseekable_open(inode
, file
);
974 static int capi_release(struct inode
*inode
, struct file
*file
)
976 struct capidev
*cdev
= file
->private_data
;
978 mutex_lock(&capidev_list_lock
);
979 list_del(&cdev
->list
);
980 mutex_unlock(&capidev_list_lock
);
983 capi20_release(&cdev
->ap
);
984 skb_queue_purge(&cdev
->recvqueue
);
985 capincci_free(cdev
, 0xffffffff);
991 static const struct file_operations capi_fops
=
993 .owner
= THIS_MODULE
,
998 .unlocked_ioctl
= capi_unlocked_ioctl
,
1000 .release
= capi_release
,
1003 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1004 /* -------- tty_operations for capincci ----------------------------- */
1007 capinc_tty_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
1009 struct capiminor
*mp
= capiminor_get(tty
->index
);
1010 int ret
= tty_standard_install(driver
, tty
);
1013 tty
->driver_data
= mp
;
1019 static void capinc_tty_cleanup(struct tty_struct
*tty
)
1021 struct capiminor
*mp
= tty
->driver_data
;
1022 tty
->driver_data
= NULL
;
1026 static int capinc_tty_open(struct tty_struct
*tty
, struct file
*filp
)
1028 struct capiminor
*mp
= tty
->driver_data
;
1031 err
= tty_port_open(&mp
->port
, tty
, filp
);
1035 handle_minor_recv(mp
);
1039 static void capinc_tty_close(struct tty_struct
*tty
, struct file
*filp
)
1041 struct capiminor
*mp
= tty
->driver_data
;
1043 tty_port_close(&mp
->port
, tty
, filp
);
1046 static int capinc_tty_write(struct tty_struct
*tty
,
1047 const unsigned char *buf
, int count
)
1049 struct capiminor
*mp
= tty
->driver_data
;
1050 struct sk_buff
*skb
;
1052 pr_debug("capinc_tty_write(count=%d)\n", count
);
1054 spin_lock_bh(&mp
->outlock
);
1058 __skb_queue_tail(&mp
->outqueue
, skb
);
1059 mp
->outbytes
+= skb
->len
;
1062 skb
= alloc_skb(CAPI_DATA_B3_REQ_LEN
+ count
, GFP_ATOMIC
);
1064 printk(KERN_ERR
"capinc_tty_write: alloc_skb failed\n");
1065 spin_unlock_bh(&mp
->outlock
);
1069 skb_reserve(skb
, CAPI_DATA_B3_REQ_LEN
);
1070 skb_put_data(skb
, buf
, count
);
1072 __skb_queue_tail(&mp
->outqueue
, skb
);
1073 mp
->outbytes
+= skb
->len
;
1074 spin_unlock_bh(&mp
->outlock
);
1076 handle_minor_send(mp
);
1081 static int capinc_tty_put_char(struct tty_struct
*tty
, unsigned char ch
)
1083 struct capiminor
*mp
= tty
->driver_data
;
1084 bool invoke_send
= false;
1085 struct sk_buff
*skb
;
1088 pr_debug("capinc_put_char(%u)\n", ch
);
1090 spin_lock_bh(&mp
->outlock
);
1093 if (skb_tailroom(skb
) > 0) {
1094 skb_put_u8(skb
, ch
);
1098 __skb_queue_tail(&mp
->outqueue
, skb
);
1099 mp
->outbytes
+= skb
->len
;
1103 skb
= alloc_skb(CAPI_DATA_B3_REQ_LEN
+ CAPI_MAX_BLKSIZE
, GFP_ATOMIC
);
1105 skb_reserve(skb
, CAPI_DATA_B3_REQ_LEN
);
1106 skb_put_u8(skb
, ch
);
1109 printk(KERN_ERR
"capinc_put_char: char %u lost\n", ch
);
1114 spin_unlock_bh(&mp
->outlock
);
1117 handle_minor_send(mp
);
1122 static void capinc_tty_flush_chars(struct tty_struct
*tty
)
1124 struct capiminor
*mp
= tty
->driver_data
;
1125 struct sk_buff
*skb
;
1127 pr_debug("capinc_tty_flush_chars\n");
1129 spin_lock_bh(&mp
->outlock
);
1133 __skb_queue_tail(&mp
->outqueue
, skb
);
1134 mp
->outbytes
+= skb
->len
;
1135 spin_unlock_bh(&mp
->outlock
);
1137 handle_minor_send(mp
);
1139 spin_unlock_bh(&mp
->outlock
);
1141 handle_minor_recv(mp
);
1144 static int capinc_tty_write_room(struct tty_struct
*tty
)
1146 struct capiminor
*mp
= tty
->driver_data
;
1149 room
= CAPINC_MAX_SENDQUEUE
-skb_queue_len(&mp
->outqueue
);
1150 room
*= CAPI_MAX_BLKSIZE
;
1151 pr_debug("capinc_tty_write_room = %d\n", room
);
1155 static int capinc_tty_chars_in_buffer(struct tty_struct
*tty
)
1157 struct capiminor
*mp
= tty
->driver_data
;
1159 pr_debug("capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1160 mp
->outbytes
, mp
->nack
,
1161 skb_queue_len(&mp
->outqueue
),
1162 skb_queue_len(&mp
->inqueue
));
1163 return mp
->outbytes
;
1166 static int capinc_tty_ioctl(struct tty_struct
*tty
,
1167 unsigned int cmd
, unsigned long arg
)
1169 return -ENOIOCTLCMD
;
1172 static void capinc_tty_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
1174 pr_debug("capinc_tty_set_termios\n");
1177 static void capinc_tty_throttle(struct tty_struct
*tty
)
1179 struct capiminor
*mp
= tty
->driver_data
;
1180 pr_debug("capinc_tty_throttle\n");
1184 static void capinc_tty_unthrottle(struct tty_struct
*tty
)
1186 struct capiminor
*mp
= tty
->driver_data
;
1188 pr_debug("capinc_tty_unthrottle\n");
1190 handle_minor_recv(mp
);
1193 static void capinc_tty_stop(struct tty_struct
*tty
)
1195 struct capiminor
*mp
= tty
->driver_data
;
1197 pr_debug("capinc_tty_stop\n");
1201 static void capinc_tty_start(struct tty_struct
*tty
)
1203 struct capiminor
*mp
= tty
->driver_data
;
1205 pr_debug("capinc_tty_start\n");
1207 handle_minor_send(mp
);
1210 static void capinc_tty_hangup(struct tty_struct
*tty
)
1212 struct capiminor
*mp
= tty
->driver_data
;
1214 pr_debug("capinc_tty_hangup\n");
1215 tty_port_hangup(&mp
->port
);
1218 static int capinc_tty_break_ctl(struct tty_struct
*tty
, int state
)
1220 pr_debug("capinc_tty_break_ctl(%d)\n", state
);
1224 static void capinc_tty_flush_buffer(struct tty_struct
*tty
)
1226 pr_debug("capinc_tty_flush_buffer\n");
1229 static void capinc_tty_set_ldisc(struct tty_struct
*tty
)
1231 pr_debug("capinc_tty_set_ldisc\n");
1234 static void capinc_tty_send_xchar(struct tty_struct
*tty
, char ch
)
1236 pr_debug("capinc_tty_send_xchar(%d)\n", ch
);
1239 static const struct tty_operations capinc_ops
= {
1240 .open
= capinc_tty_open
,
1241 .close
= capinc_tty_close
,
1242 .write
= capinc_tty_write
,
1243 .put_char
= capinc_tty_put_char
,
1244 .flush_chars
= capinc_tty_flush_chars
,
1245 .write_room
= capinc_tty_write_room
,
1246 .chars_in_buffer
= capinc_tty_chars_in_buffer
,
1247 .ioctl
= capinc_tty_ioctl
,
1248 .set_termios
= capinc_tty_set_termios
,
1249 .throttle
= capinc_tty_throttle
,
1250 .unthrottle
= capinc_tty_unthrottle
,
1251 .stop
= capinc_tty_stop
,
1252 .start
= capinc_tty_start
,
1253 .hangup
= capinc_tty_hangup
,
1254 .break_ctl
= capinc_tty_break_ctl
,
1255 .flush_buffer
= capinc_tty_flush_buffer
,
1256 .set_ldisc
= capinc_tty_set_ldisc
,
1257 .send_xchar
= capinc_tty_send_xchar
,
1258 .install
= capinc_tty_install
,
1259 .cleanup
= capinc_tty_cleanup
,
1262 static int __init
capinc_tty_init(void)
1264 struct tty_driver
*drv
;
1267 if (capi_ttyminors
> CAPINC_MAX_PORTS
)
1268 capi_ttyminors
= CAPINC_MAX_PORTS
;
1269 if (capi_ttyminors
<= 0)
1270 capi_ttyminors
= CAPINC_NR_PORTS
;
1272 capiminors
= kcalloc(capi_ttyminors
, sizeof(struct capiminor
*),
1277 drv
= alloc_tty_driver(capi_ttyminors
);
1282 drv
->driver_name
= "capi_nc";
1283 drv
->name
= "capi!";
1285 drv
->minor_start
= 0;
1286 drv
->type
= TTY_DRIVER_TYPE_SERIAL
;
1287 drv
->subtype
= SERIAL_TYPE_NORMAL
;
1288 drv
->init_termios
= tty_std_termios
;
1289 drv
->init_termios
.c_iflag
= ICRNL
;
1290 drv
->init_termios
.c_oflag
= OPOST
| ONLCR
;
1291 drv
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1292 drv
->init_termios
.c_lflag
= 0;
1294 TTY_DRIVER_REAL_RAW
| TTY_DRIVER_RESET_TERMIOS
|
1295 TTY_DRIVER_DYNAMIC_DEV
;
1296 tty_set_operations(drv
, &capinc_ops
);
1298 err
= tty_register_driver(drv
);
1300 put_tty_driver(drv
);
1302 printk(KERN_ERR
"Couldn't register capi_nc driver\n");
1305 capinc_tty_driver
= drv
;
1309 static void __exit
capinc_tty_exit(void)
1311 tty_unregister_driver(capinc_tty_driver
);
1312 put_tty_driver(capinc_tty_driver
);
1316 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1318 static inline int capinc_tty_init(void)
1323 static inline void capinc_tty_exit(void) { }
1325 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1327 /* -------- /proc functions ----------------------------------------- */
1330 * /proc/capi/capi20:
1331 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1333 static int __maybe_unused
capi20_proc_show(struct seq_file
*m
, void *v
)
1335 struct capidev
*cdev
;
1336 struct list_head
*l
;
1338 mutex_lock(&capidev_list_lock
);
1339 list_for_each(l
, &capidev_list
) {
1340 cdev
= list_entry(l
, struct capidev
, list
);
1341 seq_printf(m
, "0 %d %lu %lu %lu %lu\n",
1343 cdev
->ap
.nrecvctlpkt
,
1344 cdev
->ap
.nrecvdatapkt
,
1345 cdev
->ap
.nsentctlpkt
,
1346 cdev
->ap
.nsentdatapkt
);
1348 mutex_unlock(&capidev_list_lock
);
1353 * /proc/capi/capi20ncci:
1356 static int __maybe_unused
capi20ncci_proc_show(struct seq_file
*m
, void *v
)
1358 struct capidev
*cdev
;
1359 struct capincci
*np
;
1361 mutex_lock(&capidev_list_lock
);
1362 list_for_each_entry(cdev
, &capidev_list
, list
) {
1363 mutex_lock(&cdev
->lock
);
1364 list_for_each_entry(np
, &cdev
->nccis
, list
)
1365 seq_printf(m
, "%d 0x%x\n", cdev
->ap
.applid
, np
->ncci
);
1366 mutex_unlock(&cdev
->lock
);
1368 mutex_unlock(&capidev_list_lock
);
1372 static void __init
proc_init(void)
1374 proc_create_single("capi/capi20", 0, NULL
, capi20_proc_show
);
1375 proc_create_single("capi/capi20ncci", 0, NULL
, capi20ncci_proc_show
);
1378 static void __exit
proc_exit(void)
1380 remove_proc_entry("capi/capi20", NULL
);
1381 remove_proc_entry("capi/capi20ncci", NULL
);
1384 /* -------- init function and module interface ---------------------- */
1387 static int __init
capi_init(void)
1389 const char *compileinfo
;
1392 major_ret
= register_chrdev(capi_major
, "capi20", &capi_fops
);
1393 if (major_ret
< 0) {
1394 printk(KERN_ERR
"capi20: unable to get major %d\n", capi_major
);
1397 capi_class
= class_create(THIS_MODULE
, "capi");
1398 if (IS_ERR(capi_class
)) {
1399 unregister_chrdev(capi_major
, "capi20");
1400 return PTR_ERR(capi_class
);
1403 device_create(capi_class
, NULL
, MKDEV(capi_major
, 0), NULL
, "capi20");
1405 if (capinc_tty_init() < 0) {
1406 device_destroy(capi_class
, MKDEV(capi_major
, 0));
1407 class_destroy(capi_class
);
1408 unregister_chrdev(capi_major
, "capi20");
1414 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1415 compileinfo
= " (middleware)";
1417 compileinfo
= " (no middleware)";
1419 printk(KERN_NOTICE
"CAPI 2.0 started up with major %d%s\n",
1420 capi_major
, compileinfo
);
1425 static void __exit
capi_exit(void)
1429 device_destroy(capi_class
, MKDEV(capi_major
, 0));
1430 class_destroy(capi_class
);
1431 unregister_chrdev(capi_major
, "capi20");
1436 module_init(capi_init
);
1437 module_exit(capi_exit
);