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>
44 MODULE_DESCRIPTION("CAPI4Linux: kernel CAPI layer and /dev/capi20 interface");
45 MODULE_AUTHOR("Carsten Paeth");
46 MODULE_LICENSE("GPL");
48 /* -------- driver information -------------------------------------- */
50 static DEFINE_MUTEX(capi_mutex
);
51 static struct class *capi_class
;
52 static int capi_major
= 68; /* allocated */
54 module_param_named(major
, capi_major
, uint
, 0);
56 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
57 #define CAPINC_NR_PORTS 32
58 #define CAPINC_MAX_PORTS 256
60 static int capi_ttyminors
= CAPINC_NR_PORTS
;
62 module_param_named(ttyminors
, capi_ttyminors
, uint
, 0);
63 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
65 /* -------- defines ------------------------------------------------- */
67 #define CAPINC_MAX_RECVQUEUE 10
68 #define CAPINC_MAX_SENDQUEUE 10
69 #define CAPI_MAX_BLKSIZE 2048
71 /* -------- data structures ----------------------------------------- */
77 struct ackqueue_entry
{
78 struct list_head list
;
85 struct capi20_appl
*ap
;
94 struct sk_buff_head inqueue
;
96 struct sk_buff_head outqueue
;
98 struct sk_buff
*outskb
;
102 struct list_head ackqueue
;
108 struct list_head list
;
110 struct capidev
*cdev
;
111 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
112 struct capiminor
*minorp
;
113 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
117 struct list_head list
;
118 struct capi20_appl ap
;
122 struct sk_buff_head recvqueue
;
123 wait_queue_head_t recvwait
;
125 struct list_head nccis
;
130 /* -------- global variables ---------------------------------------- */
132 static DEFINE_MUTEX(capidev_list_lock
);
133 static LIST_HEAD(capidev_list
);
135 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
137 static DEFINE_SPINLOCK(capiminors_lock
);
138 static struct capiminor
**capiminors
;
140 static struct tty_driver
*capinc_tty_driver
;
142 /* -------- datahandles --------------------------------------------- */
144 static int capiminor_add_ack(struct capiminor
*mp
, u16 datahandle
)
146 struct ackqueue_entry
*n
;
148 n
= kmalloc(sizeof(*n
), GFP_ATOMIC
);
150 printk(KERN_ERR
"capi: alloc datahandle failed\n");
153 n
->datahandle
= datahandle
;
154 INIT_LIST_HEAD(&n
->list
);
155 spin_lock_bh(&mp
->ackqlock
);
156 list_add_tail(&n
->list
, &mp
->ackqueue
);
158 spin_unlock_bh(&mp
->ackqlock
);
162 static int capiminor_del_ack(struct capiminor
*mp
, u16 datahandle
)
164 struct ackqueue_entry
*p
, *tmp
;
166 spin_lock_bh(&mp
->ackqlock
);
167 list_for_each_entry_safe(p
, tmp
, &mp
->ackqueue
, list
) {
168 if (p
->datahandle
== datahandle
) {
171 spin_unlock_bh(&mp
->ackqlock
);
176 spin_unlock_bh(&mp
->ackqlock
);
180 static void capiminor_del_all_ack(struct capiminor
*mp
)
182 struct ackqueue_entry
*p
, *tmp
;
184 list_for_each_entry_safe(p
, tmp
, &mp
->ackqueue
, list
) {
192 /* -------- struct capiminor ---------------------------------------- */
194 static void capiminor_destroy(struct tty_port
*port
)
196 struct capiminor
*mp
= container_of(port
, struct capiminor
, port
);
198 kfree_skb(mp
->outskb
);
199 skb_queue_purge(&mp
->inqueue
);
200 skb_queue_purge(&mp
->outqueue
);
201 capiminor_del_all_ack(mp
);
205 static const struct tty_port_operations capiminor_port_ops
= {
206 .destruct
= capiminor_destroy
,
209 static struct capiminor
*capiminor_alloc(struct capi20_appl
*ap
, u32 ncci
)
211 struct capiminor
*mp
;
215 mp
= kzalloc(sizeof(*mp
), GFP_KERNEL
);
217 printk(KERN_ERR
"capi: can't alloc capiminor\n");
223 INIT_LIST_HEAD(&mp
->ackqueue
);
224 spin_lock_init(&mp
->ackqlock
);
226 skb_queue_head_init(&mp
->inqueue
);
227 skb_queue_head_init(&mp
->outqueue
);
228 spin_lock_init(&mp
->outlock
);
230 tty_port_init(&mp
->port
);
231 mp
->port
.ops
= &capiminor_port_ops
;
233 /* Allocate the least unused minor number. */
234 spin_lock(&capiminors_lock
);
235 for (minor
= 0; minor
< capi_ttyminors
; minor
++)
236 if (!capiminors
[minor
]) {
237 capiminors
[minor
] = mp
;
240 spin_unlock(&capiminors_lock
);
242 if (minor
== capi_ttyminors
) {
243 printk(KERN_NOTICE
"capi: out of minors\n");
249 dev
= tty_port_register_device(&mp
->port
, capinc_tty_driver
, minor
,
257 spin_lock(&capiminors_lock
);
258 capiminors
[minor
] = NULL
;
259 spin_unlock(&capiminors_lock
);
262 tty_port_put(&mp
->port
);
266 static struct capiminor
*capiminor_get(unsigned int minor
)
268 struct capiminor
*mp
;
270 spin_lock(&capiminors_lock
);
271 mp
= capiminors
[minor
];
273 tty_port_get(&mp
->port
);
274 spin_unlock(&capiminors_lock
);
279 static inline void capiminor_put(struct capiminor
*mp
)
281 tty_port_put(&mp
->port
);
284 static void capiminor_free(struct capiminor
*mp
)
286 tty_unregister_device(capinc_tty_driver
, mp
->minor
);
288 spin_lock(&capiminors_lock
);
289 capiminors
[mp
->minor
] = NULL
;
290 spin_unlock(&capiminors_lock
);
295 /* -------- struct capincci ----------------------------------------- */
297 static void capincci_alloc_minor(struct capidev
*cdev
, struct capincci
*np
)
299 if (cdev
->userflags
& CAPIFLAG_HIGHJACKING
)
300 np
->minorp
= capiminor_alloc(&cdev
->ap
, np
->ncci
);
303 static void capincci_free_minor(struct capincci
*np
)
305 struct capiminor
*mp
= np
->minorp
;
306 struct tty_struct
*tty
;
309 tty
= tty_port_tty_get(&mp
->port
);
319 static inline unsigned int capincci_minor_opencount(struct capincci
*np
)
321 struct capiminor
*mp
= np
->minorp
;
322 unsigned int count
= 0;
323 struct tty_struct
*tty
;
326 tty
= tty_port_tty_get(&mp
->port
);
335 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
338 capincci_alloc_minor(struct capidev
*cdev
, struct capincci
*np
) { }
339 static inline void capincci_free_minor(struct capincci
*np
) { }
341 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
343 static struct capincci
*capincci_alloc(struct capidev
*cdev
, u32 ncci
)
347 np
= kzalloc(sizeof(*np
), GFP_KERNEL
);
353 capincci_alloc_minor(cdev
, np
);
355 list_add_tail(&np
->list
, &cdev
->nccis
);
360 static void capincci_free(struct capidev
*cdev
, u32 ncci
)
362 struct capincci
*np
, *tmp
;
364 list_for_each_entry_safe(np
, tmp
, &cdev
->nccis
, list
)
365 if (ncci
== 0xffffffff || np
->ncci
== ncci
) {
366 capincci_free_minor(np
);
372 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
373 static struct capincci
*capincci_find(struct capidev
*cdev
, u32 ncci
)
377 list_for_each_entry(np
, &cdev
->nccis
, list
)
378 if (np
->ncci
== ncci
)
383 /* -------- handle data queue --------------------------------------- */
385 static struct sk_buff
*
386 gen_data_b3_resp_for(struct capiminor
*mp
, struct sk_buff
*skb
)
388 struct sk_buff
*nskb
;
389 nskb
= alloc_skb(CAPI_DATA_B3_RESP_LEN
, GFP_KERNEL
);
391 u16 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4 + 4 + 2);
392 unsigned char *s
= skb_put(nskb
, CAPI_DATA_B3_RESP_LEN
);
393 capimsg_setu16(s
, 0, CAPI_DATA_B3_RESP_LEN
);
394 capimsg_setu16(s
, 2, mp
->ap
->applid
);
395 capimsg_setu8 (s
, 4, CAPI_DATA_B3
);
396 capimsg_setu8 (s
, 5, CAPI_RESP
);
397 capimsg_setu16(s
, 6, atomic_inc_return(&mp
->msgid
));
398 capimsg_setu32(s
, 8, mp
->ncci
);
399 capimsg_setu16(s
, 12, datahandle
);
404 static int handle_recv_skb(struct capiminor
*mp
, struct sk_buff
*skb
)
406 unsigned int datalen
= skb
->len
- CAPIMSG_LEN(skb
->data
);
407 struct tty_struct
*tty
;
408 struct sk_buff
*nskb
;
409 u16 errcode
, datahandle
;
410 struct tty_ldisc
*ld
;
413 tty
= tty_port_tty_get(&mp
->port
);
415 pr_debug("capi: currently no receiver\n");
419 ld
= tty_ldisc_ref(tty
);
421 /* fatal error, do not requeue */
427 if (ld
->ops
->receive_buf
== NULL
) {
428 pr_debug("capi: ldisc has no receive_buf function\n");
429 /* fatal error, do not requeue */
433 pr_debug("capi: recv tty throttled\n");
437 if (tty
->receive_room
< datalen
) {
438 pr_debug("capi: no room in tty\n");
442 nskb
= gen_data_b3_resp_for(mp
, skb
);
444 printk(KERN_ERR
"capi: gen_data_b3_resp failed\n");
448 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4);
450 errcode
= capi20_put_message(mp
->ap
, nskb
);
452 if (errcode
== CAPI_NOERROR
) {
453 skb_pull(skb
, CAPIMSG_LEN(skb
->data
));
454 pr_debug("capi: DATA_B3_RESP %u len=%d => ldisc\n",
455 datahandle
, skb
->len
);
456 ld
->ops
->receive_buf(tty
, skb
->data
, NULL
, skb
->len
);
458 printk(KERN_ERR
"capi: send DATA_B3_RESP failed=%x\n",
462 if (errcode
== CAPI_SENDQUEUEFULL
)
478 static void handle_minor_recv(struct capiminor
*mp
)
482 while ((skb
= skb_dequeue(&mp
->inqueue
)) != NULL
)
483 if (handle_recv_skb(mp
, skb
) < 0) {
484 skb_queue_head(&mp
->inqueue
, skb
);
489 static void handle_minor_send(struct capiminor
*mp
)
491 struct tty_struct
*tty
;
497 tty
= tty_port_tty_get(&mp
->port
);
501 if (mp
->ttyoutstop
) {
502 pr_debug("capi: send: tty stopped\n");
508 spin_lock_bh(&mp
->outlock
);
509 skb
= __skb_dequeue(&mp
->outqueue
);
511 spin_unlock_bh(&mp
->outlock
);
516 spin_unlock_bh(&mp
->outlock
);
518 datahandle
= atomic_inc_return(&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, atomic_inc_return(&mp
->msgid
));
526 capimsg_setu32(skb
->data
, 8, mp
->ncci
); /* NCCI */
527 capimsg_setu32(skb
->data
, 12, (u32
)(long)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 (capiminor_add_ack(mp
, datahandle
) < 0) {
533 skb_pull(skb
, CAPI_DATA_B3_REQ_LEN
);
535 spin_lock_bh(&mp
->outlock
);
536 __skb_queue_head(&mp
->outqueue
, skb
);
538 spin_unlock_bh(&mp
->outlock
);
542 errcode
= capi20_put_message(mp
->ap
, skb
);
543 if (errcode
== CAPI_NOERROR
) {
544 pr_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
);
553 spin_lock_bh(&mp
->outlock
);
554 __skb_queue_head(&mp
->outqueue
, skb
);
556 spin_unlock_bh(&mp
->outlock
);
561 /* ups, drop packet */
562 printk(KERN_ERR
"capi: put_message = %x\n", errcode
);
568 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
569 /* -------- function called by lower level -------------------------- */
571 static void capi_recv_message(struct capi20_appl
*ap
, struct sk_buff
*skb
)
573 struct capidev
*cdev
= ap
->private;
574 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
575 struct capiminor
*mp
;
578 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
580 mutex_lock(&cdev
->lock
);
582 if (CAPIMSG_CMD(skb
->data
) == CAPI_CONNECT_B3_CONF
) {
583 u16 info
= CAPIMSG_U16(skb
->data
, 12); // Info field
584 if ((info
& 0xff00) == 0)
585 capincci_alloc(cdev
, CAPIMSG_NCCI(skb
->data
));
587 if (CAPIMSG_CMD(skb
->data
) == CAPI_CONNECT_B3_IND
)
588 capincci_alloc(cdev
, CAPIMSG_NCCI(skb
->data
));
590 if (CAPIMSG_COMMAND(skb
->data
) != CAPI_DATA_B3
) {
591 skb_queue_tail(&cdev
->recvqueue
, skb
);
592 wake_up_interruptible(&cdev
->recvwait
);
596 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
597 skb_queue_tail(&cdev
->recvqueue
, skb
);
598 wake_up_interruptible(&cdev
->recvwait
);
600 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
602 np
= capincci_find(cdev
, CAPIMSG_CONTROL(skb
->data
));
604 printk(KERN_ERR
"BUG: capi_signal: ncci not found\n");
605 skb_queue_tail(&cdev
->recvqueue
, skb
);
606 wake_up_interruptible(&cdev
->recvwait
);
612 skb_queue_tail(&cdev
->recvqueue
, skb
);
613 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 pr_debug("capi_signal: DATA_B3_IND %u len=%d\n",
619 datahandle
, skb
->len
-CAPIMSG_LEN(skb
->data
));
620 skb_queue_tail(&mp
->inqueue
, skb
);
622 handle_minor_recv(mp
);
624 } else if (CAPIMSG_SUBCOMMAND(skb
->data
) == CAPI_CONF
) {
626 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4);
627 pr_debug("capi_signal: DATA_B3_CONF %u 0x%x\n",
629 CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4 + 2));
631 capiminor_del_ack(mp
, datahandle
);
632 tty_port_tty_wakeup(&mp
->port
);
633 handle_minor_send(mp
);
636 /* ups, let capi application handle it :-) */
637 skb_queue_tail(&cdev
->recvqueue
, skb
);
638 wake_up_interruptible(&cdev
->recvwait
);
640 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
643 mutex_unlock(&cdev
->lock
);
646 /* -------- file_operations for capidev ----------------------------- */
649 capi_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
651 struct capidev
*cdev
= file
->private_data
;
656 if (!cdev
->ap
.applid
)
659 skb
= skb_dequeue(&cdev
->recvqueue
);
661 if (file
->f_flags
& O_NONBLOCK
)
663 err
= wait_event_interruptible(cdev
->recvwait
,
664 (skb
= skb_dequeue(&cdev
->recvqueue
)));
668 if (skb
->len
> count
) {
669 skb_queue_head(&cdev
->recvqueue
, skb
);
672 if (copy_to_user(buf
, skb
->data
, skb
->len
)) {
673 skb_queue_head(&cdev
->recvqueue
, skb
);
684 capi_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
686 struct capidev
*cdev
= file
->private_data
;
690 if (!cdev
->ap
.applid
)
693 if (count
< CAPIMSG_BASELEN
)
696 skb
= alloc_skb(count
, GFP_USER
);
700 if (copy_from_user(skb_put(skb
, count
), buf
, count
)) {
704 mlen
= CAPIMSG_LEN(skb
->data
);
705 if (CAPIMSG_CMD(skb
->data
) == CAPI_DATA_B3_REQ
) {
706 if (count
< CAPI_DATA_B3_REQ_LEN
||
707 (size_t)(mlen
+ CAPIMSG_DATALEN(skb
->data
)) != count
) {
717 CAPIMSG_SETAPPID(skb
->data
, cdev
->ap
.applid
);
719 if (CAPIMSG_CMD(skb
->data
) == CAPI_DISCONNECT_B3_RESP
) {
720 if (count
< CAPI_DISCONNECT_B3_RESP_LEN
) {
724 mutex_lock(&cdev
->lock
);
725 capincci_free(cdev
, CAPIMSG_NCCI(skb
->data
));
726 mutex_unlock(&cdev
->lock
);
729 cdev
->errcode
= capi20_put_message(&cdev
->ap
, skb
);
739 capi_poll(struct file
*file
, poll_table
*wait
)
741 struct capidev
*cdev
= file
->private_data
;
744 if (!cdev
->ap
.applid
)
747 poll_wait(file
, &(cdev
->recvwait
), wait
);
748 mask
= EPOLLOUT
| EPOLLWRNORM
;
749 if (!skb_queue_empty_lockless(&cdev
->recvqueue
))
750 mask
|= EPOLLIN
| EPOLLRDNORM
;
755 capi_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
757 struct capidev
*cdev
= file
->private_data
;
758 capi_ioctl_struct data
;
759 int retval
= -EINVAL
;
760 void __user
*argp
= (void __user
*)arg
;
764 mutex_lock(&cdev
->lock
);
766 if (cdev
->ap
.applid
) {
770 if (copy_from_user(&cdev
->ap
.rparam
, argp
,
771 sizeof(struct capi_register_params
))) {
775 cdev
->ap
.private = cdev
;
776 cdev
->ap
.recv_message
= capi_recv_message
;
777 cdev
->errcode
= capi20_register(&cdev
->ap
);
778 retval
= (int)cdev
->ap
.applid
;
785 mutex_unlock(&cdev
->lock
);
788 case CAPI_GET_VERSION
:
789 if (copy_from_user(&data
.contr
, argp
,
792 cdev
->errcode
= capi20_get_version(data
.contr
, &data
.version
);
795 if (copy_to_user(argp
, &data
.version
,
796 sizeof(data
.version
)))
800 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
:
813 if (copy_from_user(&data
.contr
, argp
,
817 if (data
.contr
== 0) {
818 cdev
->errcode
= capi20_get_profile(data
.contr
, &data
.profile
);
822 retval
= copy_to_user(argp
,
823 &data
.profile
.ncontroller
,
824 sizeof(data
.profile
.ncontroller
));
827 cdev
->errcode
= capi20_get_profile(data
.contr
, &data
.profile
);
831 retval
= copy_to_user(argp
, &data
.profile
,
832 sizeof(data
.profile
));
838 case CAPI_GET_MANUFACTURER
:
839 if (copy_from_user(&data
.contr
, argp
,
842 cdev
->errcode
= capi20_get_manufacturer(data
.contr
, data
.manufacturer
);
846 if (copy_to_user(argp
, data
.manufacturer
,
847 sizeof(data
.manufacturer
)))
852 case CAPI_GET_ERRCODE
:
853 data
.errcode
= cdev
->errcode
;
854 cdev
->errcode
= CAPI_NOERROR
;
856 if (copy_to_user(argp
, &data
.errcode
,
857 sizeof(data
.errcode
)))
863 if (capi20_isinstalled() == CAPI_NOERROR
)
867 case CAPI_MANUFACTURER_CMD
: {
868 struct capi_manufacturer_cmd mcmd
;
869 if (!capable(CAP_SYS_ADMIN
))
871 if (copy_from_user(&mcmd
, argp
, sizeof(mcmd
)))
873 return capi20_manufacturer(mcmd
.cmd
, mcmd
.data
);
876 case CAPI_CLR_FLAGS
: {
879 if (copy_from_user(&userflags
, argp
, sizeof(userflags
)))
882 mutex_lock(&cdev
->lock
);
883 if (cmd
== CAPI_SET_FLAGS
)
884 cdev
->userflags
|= userflags
;
886 cdev
->userflags
&= ~userflags
;
887 mutex_unlock(&cdev
->lock
);
891 if (copy_to_user(argp
, &cdev
->userflags
,
892 sizeof(cdev
->userflags
)))
896 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
897 case CAPI_NCCI_OPENCOUNT
:
900 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
901 case CAPI_NCCI_OPENCOUNT
: {
902 struct capincci
*nccip
;
906 if (copy_from_user(&ncci
, argp
, sizeof(ncci
)))
909 mutex_lock(&cdev
->lock
);
910 nccip
= capincci_find(cdev
, (u32
)ncci
);
912 count
= capincci_minor_opencount(nccip
);
913 mutex_unlock(&cdev
->lock
);
917 case CAPI_NCCI_GETUNIT
: {
918 struct capincci
*nccip
;
919 struct capiminor
*mp
;
923 if (copy_from_user(&ncci
, argp
, sizeof(ncci
)))
926 mutex_lock(&cdev
->lock
);
927 nccip
= capincci_find(cdev
, (u32
)ncci
);
933 mutex_unlock(&cdev
->lock
);
936 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
944 capi_unlocked_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
948 mutex_lock(&capi_mutex
);
949 ret
= capi_ioctl(file
, cmd
, arg
);
950 mutex_unlock(&capi_mutex
);
957 capi_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
961 if (cmd
== CAPI_MANUFACTURER_CMD
) {
967 if (!capable(CAP_SYS_ADMIN
))
969 if (copy_from_user(&mcmd32
, compat_ptr(arg
), sizeof(mcmd32
)))
972 mutex_lock(&capi_mutex
);
973 ret
= capi20_manufacturer(mcmd32
.cmd
, compat_ptr(mcmd32
.data
));
974 mutex_unlock(&capi_mutex
);
979 return capi_unlocked_ioctl(file
, cmd
, (unsigned long)compat_ptr(arg
));
983 static int capi_open(struct inode
*inode
, struct file
*file
)
985 struct capidev
*cdev
;
987 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
991 mutex_init(&cdev
->lock
);
992 skb_queue_head_init(&cdev
->recvqueue
);
993 init_waitqueue_head(&cdev
->recvwait
);
994 INIT_LIST_HEAD(&cdev
->nccis
);
995 file
->private_data
= cdev
;
997 mutex_lock(&capidev_list_lock
);
998 list_add_tail(&cdev
->list
, &capidev_list
);
999 mutex_unlock(&capidev_list_lock
);
1001 return stream_open(inode
, file
);
1004 static int capi_release(struct inode
*inode
, struct file
*file
)
1006 struct capidev
*cdev
= file
->private_data
;
1008 mutex_lock(&capidev_list_lock
);
1009 list_del(&cdev
->list
);
1010 mutex_unlock(&capidev_list_lock
);
1012 if (cdev
->ap
.applid
)
1013 capi20_release(&cdev
->ap
);
1014 skb_queue_purge(&cdev
->recvqueue
);
1015 capincci_free(cdev
, 0xffffffff);
1021 static const struct file_operations capi_fops
=
1023 .owner
= THIS_MODULE
,
1024 .llseek
= no_llseek
,
1026 .write
= capi_write
,
1028 .unlocked_ioctl
= capi_unlocked_ioctl
,
1029 #ifdef CONFIG_COMPAT
1030 .compat_ioctl
= capi_compat_ioctl
,
1033 .release
= capi_release
,
1036 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1037 /* -------- tty_operations for capincci ----------------------------- */
1040 capinc_tty_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
1042 struct capiminor
*mp
= capiminor_get(tty
->index
);
1043 int ret
= tty_standard_install(driver
, tty
);
1046 tty
->driver_data
= mp
;
1052 static void capinc_tty_cleanup(struct tty_struct
*tty
)
1054 struct capiminor
*mp
= tty
->driver_data
;
1055 tty
->driver_data
= NULL
;
1059 static int capinc_tty_open(struct tty_struct
*tty
, struct file
*filp
)
1061 struct capiminor
*mp
= tty
->driver_data
;
1064 err
= tty_port_open(&mp
->port
, tty
, filp
);
1068 handle_minor_recv(mp
);
1072 static void capinc_tty_close(struct tty_struct
*tty
, struct file
*filp
)
1074 struct capiminor
*mp
= tty
->driver_data
;
1076 tty_port_close(&mp
->port
, tty
, filp
);
1079 static int capinc_tty_write(struct tty_struct
*tty
,
1080 const unsigned char *buf
, int count
)
1082 struct capiminor
*mp
= tty
->driver_data
;
1083 struct sk_buff
*skb
;
1085 pr_debug("capinc_tty_write(count=%d)\n", count
);
1087 spin_lock_bh(&mp
->outlock
);
1091 __skb_queue_tail(&mp
->outqueue
, skb
);
1092 mp
->outbytes
+= skb
->len
;
1095 skb
= alloc_skb(CAPI_DATA_B3_REQ_LEN
+ count
, GFP_ATOMIC
);
1097 printk(KERN_ERR
"capinc_tty_write: alloc_skb failed\n");
1098 spin_unlock_bh(&mp
->outlock
);
1102 skb_reserve(skb
, CAPI_DATA_B3_REQ_LEN
);
1103 skb_put_data(skb
, buf
, count
);
1105 __skb_queue_tail(&mp
->outqueue
, skb
);
1106 mp
->outbytes
+= skb
->len
;
1107 spin_unlock_bh(&mp
->outlock
);
1109 handle_minor_send(mp
);
1114 static int capinc_tty_put_char(struct tty_struct
*tty
, unsigned char ch
)
1116 struct capiminor
*mp
= tty
->driver_data
;
1117 bool invoke_send
= false;
1118 struct sk_buff
*skb
;
1121 pr_debug("capinc_put_char(%u)\n", ch
);
1123 spin_lock_bh(&mp
->outlock
);
1126 if (skb_tailroom(skb
) > 0) {
1127 skb_put_u8(skb
, ch
);
1131 __skb_queue_tail(&mp
->outqueue
, skb
);
1132 mp
->outbytes
+= skb
->len
;
1136 skb
= alloc_skb(CAPI_DATA_B3_REQ_LEN
+ CAPI_MAX_BLKSIZE
, GFP_ATOMIC
);
1138 skb_reserve(skb
, CAPI_DATA_B3_REQ_LEN
);
1139 skb_put_u8(skb
, ch
);
1142 printk(KERN_ERR
"capinc_put_char: char %u lost\n", ch
);
1147 spin_unlock_bh(&mp
->outlock
);
1150 handle_minor_send(mp
);
1155 static void capinc_tty_flush_chars(struct tty_struct
*tty
)
1157 struct capiminor
*mp
= tty
->driver_data
;
1158 struct sk_buff
*skb
;
1160 pr_debug("capinc_tty_flush_chars\n");
1162 spin_lock_bh(&mp
->outlock
);
1166 __skb_queue_tail(&mp
->outqueue
, skb
);
1167 mp
->outbytes
+= skb
->len
;
1168 spin_unlock_bh(&mp
->outlock
);
1170 handle_minor_send(mp
);
1172 spin_unlock_bh(&mp
->outlock
);
1174 handle_minor_recv(mp
);
1177 static int capinc_tty_write_room(struct tty_struct
*tty
)
1179 struct capiminor
*mp
= tty
->driver_data
;
1182 room
= CAPINC_MAX_SENDQUEUE
-skb_queue_len(&mp
->outqueue
);
1183 room
*= CAPI_MAX_BLKSIZE
;
1184 pr_debug("capinc_tty_write_room = %d\n", room
);
1188 static int capinc_tty_chars_in_buffer(struct tty_struct
*tty
)
1190 struct capiminor
*mp
= tty
->driver_data
;
1192 pr_debug("capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1193 mp
->outbytes
, mp
->nack
,
1194 skb_queue_len(&mp
->outqueue
),
1195 skb_queue_len(&mp
->inqueue
));
1196 return mp
->outbytes
;
1199 static void capinc_tty_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
1201 pr_debug("capinc_tty_set_termios\n");
1204 static void capinc_tty_throttle(struct tty_struct
*tty
)
1206 struct capiminor
*mp
= tty
->driver_data
;
1207 pr_debug("capinc_tty_throttle\n");
1211 static void capinc_tty_unthrottle(struct tty_struct
*tty
)
1213 struct capiminor
*mp
= tty
->driver_data
;
1215 pr_debug("capinc_tty_unthrottle\n");
1217 handle_minor_recv(mp
);
1220 static void capinc_tty_stop(struct tty_struct
*tty
)
1222 struct capiminor
*mp
= tty
->driver_data
;
1224 pr_debug("capinc_tty_stop\n");
1228 static void capinc_tty_start(struct tty_struct
*tty
)
1230 struct capiminor
*mp
= tty
->driver_data
;
1232 pr_debug("capinc_tty_start\n");
1234 handle_minor_send(mp
);
1237 static void capinc_tty_hangup(struct tty_struct
*tty
)
1239 struct capiminor
*mp
= tty
->driver_data
;
1241 pr_debug("capinc_tty_hangup\n");
1242 tty_port_hangup(&mp
->port
);
1245 static int capinc_tty_break_ctl(struct tty_struct
*tty
, int state
)
1247 pr_debug("capinc_tty_break_ctl(%d)\n", state
);
1251 static void capinc_tty_flush_buffer(struct tty_struct
*tty
)
1253 pr_debug("capinc_tty_flush_buffer\n");
1256 static void capinc_tty_set_ldisc(struct tty_struct
*tty
)
1258 pr_debug("capinc_tty_set_ldisc\n");
1261 static void capinc_tty_send_xchar(struct tty_struct
*tty
, char ch
)
1263 pr_debug("capinc_tty_send_xchar(%d)\n", ch
);
1266 static const struct tty_operations capinc_ops
= {
1267 .open
= capinc_tty_open
,
1268 .close
= capinc_tty_close
,
1269 .write
= capinc_tty_write
,
1270 .put_char
= capinc_tty_put_char
,
1271 .flush_chars
= capinc_tty_flush_chars
,
1272 .write_room
= capinc_tty_write_room
,
1273 .chars_in_buffer
= capinc_tty_chars_in_buffer
,
1274 .set_termios
= capinc_tty_set_termios
,
1275 .throttle
= capinc_tty_throttle
,
1276 .unthrottle
= capinc_tty_unthrottle
,
1277 .stop
= capinc_tty_stop
,
1278 .start
= capinc_tty_start
,
1279 .hangup
= capinc_tty_hangup
,
1280 .break_ctl
= capinc_tty_break_ctl
,
1281 .flush_buffer
= capinc_tty_flush_buffer
,
1282 .set_ldisc
= capinc_tty_set_ldisc
,
1283 .send_xchar
= capinc_tty_send_xchar
,
1284 .install
= capinc_tty_install
,
1285 .cleanup
= capinc_tty_cleanup
,
1288 static int __init
capinc_tty_init(void)
1290 struct tty_driver
*drv
;
1293 if (capi_ttyminors
> CAPINC_MAX_PORTS
)
1294 capi_ttyminors
= CAPINC_MAX_PORTS
;
1295 if (capi_ttyminors
<= 0)
1296 capi_ttyminors
= CAPINC_NR_PORTS
;
1298 capiminors
= kcalloc(capi_ttyminors
, sizeof(struct capiminor
*),
1303 drv
= alloc_tty_driver(capi_ttyminors
);
1308 drv
->driver_name
= "capi_nc";
1309 drv
->name
= "capi!";
1311 drv
->minor_start
= 0;
1312 drv
->type
= TTY_DRIVER_TYPE_SERIAL
;
1313 drv
->subtype
= SERIAL_TYPE_NORMAL
;
1314 drv
->init_termios
= tty_std_termios
;
1315 drv
->init_termios
.c_iflag
= ICRNL
;
1316 drv
->init_termios
.c_oflag
= OPOST
| ONLCR
;
1317 drv
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1318 drv
->init_termios
.c_lflag
= 0;
1320 TTY_DRIVER_REAL_RAW
| TTY_DRIVER_RESET_TERMIOS
|
1321 TTY_DRIVER_DYNAMIC_DEV
;
1322 tty_set_operations(drv
, &capinc_ops
);
1324 err
= tty_register_driver(drv
);
1326 put_tty_driver(drv
);
1328 printk(KERN_ERR
"Couldn't register capi_nc driver\n");
1331 capinc_tty_driver
= drv
;
1335 static void __exit
capinc_tty_exit(void)
1337 tty_unregister_driver(capinc_tty_driver
);
1338 put_tty_driver(capinc_tty_driver
);
1342 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1344 static inline int capinc_tty_init(void)
1349 static inline void capinc_tty_exit(void) { }
1351 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1353 /* -------- /proc functions ----------------------------------------- */
1356 * /proc/capi/capi20:
1357 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1359 static int __maybe_unused
capi20_proc_show(struct seq_file
*m
, void *v
)
1361 struct capidev
*cdev
;
1362 struct list_head
*l
;
1364 mutex_lock(&capidev_list_lock
);
1365 list_for_each(l
, &capidev_list
) {
1366 cdev
= list_entry(l
, struct capidev
, list
);
1367 seq_printf(m
, "0 %d %lu %lu %lu %lu\n",
1369 cdev
->ap
.nrecvctlpkt
,
1370 cdev
->ap
.nrecvdatapkt
,
1371 cdev
->ap
.nsentctlpkt
,
1372 cdev
->ap
.nsentdatapkt
);
1374 mutex_unlock(&capidev_list_lock
);
1379 * /proc/capi/capi20ncci:
1382 static int __maybe_unused
capi20ncci_proc_show(struct seq_file
*m
, void *v
)
1384 struct capidev
*cdev
;
1385 struct capincci
*np
;
1387 mutex_lock(&capidev_list_lock
);
1388 list_for_each_entry(cdev
, &capidev_list
, list
) {
1389 mutex_lock(&cdev
->lock
);
1390 list_for_each_entry(np
, &cdev
->nccis
, list
)
1391 seq_printf(m
, "%d 0x%x\n", cdev
->ap
.applid
, np
->ncci
);
1392 mutex_unlock(&cdev
->lock
);
1394 mutex_unlock(&capidev_list_lock
);
1398 static void __init
proc_init(void)
1400 proc_create_single("capi/capi20", 0, NULL
, capi20_proc_show
);
1401 proc_create_single("capi/capi20ncci", 0, NULL
, capi20ncci_proc_show
);
1404 static void __exit
proc_exit(void)
1406 remove_proc_entry("capi/capi20", NULL
);
1407 remove_proc_entry("capi/capi20ncci", NULL
);
1410 /* -------- init function and module interface ---------------------- */
1413 static int __init
capi_init(void)
1415 const char *compileinfo
;
1423 major_ret
= register_chrdev(capi_major
, "capi20", &capi_fops
);
1424 if (major_ret
< 0) {
1425 printk(KERN_ERR
"capi20: unable to get major %d\n", capi_major
);
1429 capi_class
= class_create(THIS_MODULE
, "capi");
1430 if (IS_ERR(capi_class
)) {
1431 unregister_chrdev(capi_major
, "capi20");
1433 return PTR_ERR(capi_class
);
1436 device_create(capi_class
, NULL
, MKDEV(capi_major
, 0), NULL
, "capi20");
1438 if (capinc_tty_init() < 0) {
1439 device_destroy(capi_class
, MKDEV(capi_major
, 0));
1440 class_destroy(capi_class
);
1441 unregister_chrdev(capi_major
, "capi20");
1448 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1449 compileinfo
= " (middleware)";
1451 compileinfo
= " (no middleware)";
1453 printk(KERN_NOTICE
"CAPI 2.0 started up with major %d%s\n",
1454 capi_major
, compileinfo
);
1459 static void __exit
capi_exit(void)
1463 device_destroy(capi_class
, MKDEV(capi_major
, 0));
1464 class_destroy(capi_class
);
1465 unregister_chrdev(capi_major
, "capi20");
1472 module_init(capi_init
);
1473 module_exit(capi_exit
);