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/ethtool.h>
15 #include <linux/errno.h>
16 #include <linux/kernel.h>
17 #include <linux/major.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/fcntl.h>
22 #include <linux/signal.h>
23 #include <linux/mutex.h>
25 #include <linux/timer.h>
26 #include <linux/wait.h>
27 #include <linux/tty.h>
28 #include <linux/netdevice.h>
29 #include <linux/ppp_defs.h>
30 #include <linux/ppp-ioctl.h>
31 #include <linux/skbuff.h>
32 #include <linux/proc_fs.h>
33 #include <linux/seq_file.h>
34 #include <linux/poll.h>
35 #include <linux/capi.h>
36 #include <linux/kernelcapi.h>
37 #include <linux/init.h>
38 #include <linux/device.h>
39 #include <linux/moduleparam.h>
40 #include <linux/isdn/capiutil.h>
41 #include <linux/isdn/capicmd.h>
45 MODULE_DESCRIPTION("CAPI4Linux: kernel CAPI layer and /dev/capi20 interface");
46 MODULE_AUTHOR("Carsten Paeth");
47 MODULE_LICENSE("GPL");
49 /* -------- driver information -------------------------------------- */
51 static DEFINE_MUTEX(capi_mutex
);
52 static struct class *capi_class
;
53 static int capi_major
= 68; /* allocated */
55 module_param_named(major
, capi_major
, uint
, 0);
57 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
58 #define CAPINC_NR_PORTS 32
59 #define CAPINC_MAX_PORTS 256
61 static int capi_ttyminors
= CAPINC_NR_PORTS
;
63 module_param_named(ttyminors
, capi_ttyminors
, uint
, 0);
64 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
66 /* -------- defines ------------------------------------------------- */
68 #define CAPINC_MAX_RECVQUEUE 10
69 #define CAPINC_MAX_SENDQUEUE 10
70 #define CAPI_MAX_BLKSIZE 2048
72 /* -------- data structures ----------------------------------------- */
78 struct ackqueue_entry
{
79 struct list_head list
;
86 struct capi20_appl
*ap
;
95 struct sk_buff_head inqueue
;
97 struct sk_buff_head outqueue
;
99 struct sk_buff
*outskb
;
103 struct list_head ackqueue
;
109 struct list_head list
;
111 struct capidev
*cdev
;
112 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
113 struct capiminor
*minorp
;
114 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
118 struct list_head list
;
119 struct capi20_appl ap
;
123 struct sk_buff_head recvqueue
;
124 wait_queue_head_t recvwait
;
126 struct list_head nccis
;
131 /* -------- global variables ---------------------------------------- */
133 static DEFINE_MUTEX(capidev_list_lock
);
134 static LIST_HEAD(capidev_list
);
136 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
138 static DEFINE_SPINLOCK(capiminors_lock
);
139 static struct capiminor
**capiminors
;
141 static struct tty_driver
*capinc_tty_driver
;
143 /* -------- datahandles --------------------------------------------- */
145 static int capiminor_add_ack(struct capiminor
*mp
, u16 datahandle
)
147 struct ackqueue_entry
*n
;
149 n
= kmalloc(sizeof(*n
), GFP_ATOMIC
);
151 printk(KERN_ERR
"capi: alloc datahandle failed\n");
154 n
->datahandle
= datahandle
;
155 INIT_LIST_HEAD(&n
->list
);
156 spin_lock_bh(&mp
->ackqlock
);
157 list_add_tail(&n
->list
, &mp
->ackqueue
);
159 spin_unlock_bh(&mp
->ackqlock
);
163 static int capiminor_del_ack(struct capiminor
*mp
, u16 datahandle
)
165 struct ackqueue_entry
*p
, *tmp
;
167 spin_lock_bh(&mp
->ackqlock
);
168 list_for_each_entry_safe(p
, tmp
, &mp
->ackqueue
, list
) {
169 if (p
->datahandle
== datahandle
) {
172 spin_unlock_bh(&mp
->ackqlock
);
177 spin_unlock_bh(&mp
->ackqlock
);
181 static void capiminor_del_all_ack(struct capiminor
*mp
)
183 struct ackqueue_entry
*p
, *tmp
;
185 list_for_each_entry_safe(p
, tmp
, &mp
->ackqueue
, list
) {
193 /* -------- struct capiminor ---------------------------------------- */
195 static void capiminor_destroy(struct tty_port
*port
)
197 struct capiminor
*mp
= container_of(port
, struct capiminor
, port
);
199 kfree_skb(mp
->outskb
);
200 skb_queue_purge(&mp
->inqueue
);
201 skb_queue_purge(&mp
->outqueue
);
202 capiminor_del_all_ack(mp
);
206 static const struct tty_port_operations capiminor_port_ops
= {
207 .destruct
= capiminor_destroy
,
210 static struct capiminor
*capiminor_alloc(struct capi20_appl
*ap
, u32 ncci
)
212 struct capiminor
*mp
;
216 mp
= kzalloc(sizeof(*mp
), GFP_KERNEL
);
218 printk(KERN_ERR
"capi: can't alloc capiminor\n");
224 INIT_LIST_HEAD(&mp
->ackqueue
);
225 spin_lock_init(&mp
->ackqlock
);
227 skb_queue_head_init(&mp
->inqueue
);
228 skb_queue_head_init(&mp
->outqueue
);
229 spin_lock_init(&mp
->outlock
);
231 tty_port_init(&mp
->port
);
232 mp
->port
.ops
= &capiminor_port_ops
;
234 /* Allocate the least unused minor number. */
235 spin_lock(&capiminors_lock
);
236 for (minor
= 0; minor
< capi_ttyminors
; minor
++)
237 if (!capiminors
[minor
]) {
238 capiminors
[minor
] = mp
;
241 spin_unlock(&capiminors_lock
);
243 if (minor
== capi_ttyminors
) {
244 printk(KERN_NOTICE
"capi: out of minors\n");
250 dev
= tty_port_register_device(&mp
->port
, capinc_tty_driver
, minor
,
258 spin_lock(&capiminors_lock
);
259 capiminors
[minor
] = NULL
;
260 spin_unlock(&capiminors_lock
);
263 tty_port_put(&mp
->port
);
267 static struct capiminor
*capiminor_get(unsigned int minor
)
269 struct capiminor
*mp
;
271 spin_lock(&capiminors_lock
);
272 mp
= capiminors
[minor
];
274 tty_port_get(&mp
->port
);
275 spin_unlock(&capiminors_lock
);
280 static inline void capiminor_put(struct capiminor
*mp
)
282 tty_port_put(&mp
->port
);
285 static void capiminor_free(struct capiminor
*mp
)
287 tty_unregister_device(capinc_tty_driver
, mp
->minor
);
289 spin_lock(&capiminors_lock
);
290 capiminors
[mp
->minor
] = NULL
;
291 spin_unlock(&capiminors_lock
);
296 /* -------- struct capincci ----------------------------------------- */
298 static void capincci_alloc_minor(struct capidev
*cdev
, struct capincci
*np
)
300 if (cdev
->userflags
& CAPIFLAG_HIGHJACKING
)
301 np
->minorp
= capiminor_alloc(&cdev
->ap
, np
->ncci
);
304 static void capincci_free_minor(struct capincci
*np
)
306 struct capiminor
*mp
= np
->minorp
;
307 struct tty_struct
*tty
;
310 tty
= tty_port_tty_get(&mp
->port
);
320 static inline unsigned int capincci_minor_opencount(struct capincci
*np
)
322 struct capiminor
*mp
= np
->minorp
;
323 unsigned int count
= 0;
324 struct tty_struct
*tty
;
327 tty
= tty_port_tty_get(&mp
->port
);
336 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
339 capincci_alloc_minor(struct capidev
*cdev
, struct capincci
*np
) { }
340 static inline void capincci_free_minor(struct capincci
*np
) { }
342 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
344 static struct capincci
*capincci_alloc(struct capidev
*cdev
, u32 ncci
)
348 np
= kzalloc(sizeof(*np
), GFP_KERNEL
);
354 capincci_alloc_minor(cdev
, np
);
356 list_add_tail(&np
->list
, &cdev
->nccis
);
361 static void capincci_free(struct capidev
*cdev
, u32 ncci
)
363 struct capincci
*np
, *tmp
;
365 list_for_each_entry_safe(np
, tmp
, &cdev
->nccis
, list
)
366 if (ncci
== 0xffffffff || np
->ncci
== ncci
) {
367 capincci_free_minor(np
);
373 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
374 static struct capincci
*capincci_find(struct capidev
*cdev
, u32 ncci
)
378 list_for_each_entry(np
, &cdev
->nccis
, list
)
379 if (np
->ncci
== ncci
)
384 /* -------- handle data queue --------------------------------------- */
386 static struct sk_buff
*
387 gen_data_b3_resp_for(struct capiminor
*mp
, struct sk_buff
*skb
)
389 struct sk_buff
*nskb
;
390 nskb
= alloc_skb(CAPI_DATA_B3_RESP_LEN
, GFP_KERNEL
);
392 u16 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4 + 4 + 2);
393 unsigned char *s
= skb_put(nskb
, CAPI_DATA_B3_RESP_LEN
);
394 capimsg_setu16(s
, 0, CAPI_DATA_B3_RESP_LEN
);
395 capimsg_setu16(s
, 2, mp
->ap
->applid
);
396 capimsg_setu8 (s
, 4, CAPI_DATA_B3
);
397 capimsg_setu8 (s
, 5, CAPI_RESP
);
398 capimsg_setu16(s
, 6, atomic_inc_return(&mp
->msgid
));
399 capimsg_setu32(s
, 8, mp
->ncci
);
400 capimsg_setu16(s
, 12, datahandle
);
405 static int handle_recv_skb(struct capiminor
*mp
, struct sk_buff
*skb
)
407 unsigned int datalen
= skb
->len
- CAPIMSG_LEN(skb
->data
);
408 struct tty_struct
*tty
;
409 struct sk_buff
*nskb
;
410 u16 errcode
, datahandle
;
411 struct tty_ldisc
*ld
;
414 tty
= tty_port_tty_get(&mp
->port
);
416 pr_debug("capi: currently no receiver\n");
420 ld
= tty_ldisc_ref(tty
);
422 /* fatal error, do not requeue */
428 if (ld
->ops
->receive_buf
== NULL
) {
429 pr_debug("capi: ldisc has no receive_buf function\n");
430 /* fatal error, do not requeue */
434 pr_debug("capi: recv tty throttled\n");
438 if (tty
->receive_room
< datalen
) {
439 pr_debug("capi: no room in tty\n");
443 nskb
= gen_data_b3_resp_for(mp
, skb
);
445 printk(KERN_ERR
"capi: gen_data_b3_resp failed\n");
449 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4);
451 errcode
= capi20_put_message(mp
->ap
, nskb
);
453 if (errcode
== CAPI_NOERROR
) {
454 skb_pull(skb
, CAPIMSG_LEN(skb
->data
));
455 pr_debug("capi: DATA_B3_RESP %u len=%d => ldisc\n",
456 datahandle
, skb
->len
);
457 ld
->ops
->receive_buf(tty
, skb
->data
, NULL
, skb
->len
);
459 printk(KERN_ERR
"capi: send DATA_B3_RESP failed=%x\n",
463 if (errcode
== CAPI_SENDQUEUEFULL
)
479 static void handle_minor_recv(struct capiminor
*mp
)
483 while ((skb
= skb_dequeue(&mp
->inqueue
)) != NULL
)
484 if (handle_recv_skb(mp
, skb
) < 0) {
485 skb_queue_head(&mp
->inqueue
, skb
);
490 static void handle_minor_send(struct capiminor
*mp
)
492 struct tty_struct
*tty
;
498 tty
= tty_port_tty_get(&mp
->port
);
502 if (mp
->ttyoutstop
) {
503 pr_debug("capi: send: tty stopped\n");
509 spin_lock_bh(&mp
->outlock
);
510 skb
= __skb_dequeue(&mp
->outqueue
);
512 spin_unlock_bh(&mp
->outlock
);
517 spin_unlock_bh(&mp
->outlock
);
519 datahandle
= atomic_inc_return(&mp
->datahandle
);
520 skb_push(skb
, CAPI_DATA_B3_REQ_LEN
);
521 memset(skb
->data
, 0, CAPI_DATA_B3_REQ_LEN
);
522 capimsg_setu16(skb
->data
, 0, CAPI_DATA_B3_REQ_LEN
);
523 capimsg_setu16(skb
->data
, 2, mp
->ap
->applid
);
524 capimsg_setu8 (skb
->data
, 4, CAPI_DATA_B3
);
525 capimsg_setu8 (skb
->data
, 5, CAPI_REQ
);
526 capimsg_setu16(skb
->data
, 6, atomic_inc_return(&mp
->msgid
));
527 capimsg_setu32(skb
->data
, 8, mp
->ncci
); /* NCCI */
528 capimsg_setu32(skb
->data
, 12, (u32
)(long)skb
->data
);/* Data32 */
529 capimsg_setu16(skb
->data
, 16, len
); /* Data length */
530 capimsg_setu16(skb
->data
, 18, datahandle
);
531 capimsg_setu16(skb
->data
, 20, 0); /* Flags */
533 if (capiminor_add_ack(mp
, datahandle
) < 0) {
534 skb_pull(skb
, CAPI_DATA_B3_REQ_LEN
);
536 spin_lock_bh(&mp
->outlock
);
537 __skb_queue_head(&mp
->outqueue
, skb
);
539 spin_unlock_bh(&mp
->outlock
);
543 errcode
= capi20_put_message(mp
->ap
, skb
);
544 if (errcode
== CAPI_NOERROR
) {
545 pr_debug("capi: DATA_B3_REQ %u len=%u\n",
549 capiminor_del_ack(mp
, datahandle
);
551 if (errcode
== CAPI_SENDQUEUEFULL
) {
552 skb_pull(skb
, CAPI_DATA_B3_REQ_LEN
);
554 spin_lock_bh(&mp
->outlock
);
555 __skb_queue_head(&mp
->outqueue
, skb
);
557 spin_unlock_bh(&mp
->outlock
);
562 /* ups, drop packet */
563 printk(KERN_ERR
"capi: put_message = %x\n", errcode
);
569 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
570 /* -------- function called by lower level -------------------------- */
572 static void capi_recv_message(struct capi20_appl
*ap
, struct sk_buff
*skb
)
574 struct capidev
*cdev
= ap
->private;
575 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
576 struct capiminor
*mp
;
579 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
581 mutex_lock(&cdev
->lock
);
583 if (CAPIMSG_CMD(skb
->data
) == CAPI_CONNECT_B3_CONF
) {
584 u16 info
= CAPIMSG_U16(skb
->data
, 12); // Info field
585 if ((info
& 0xff00) == 0)
586 capincci_alloc(cdev
, CAPIMSG_NCCI(skb
->data
));
588 if (CAPIMSG_CMD(skb
->data
) == CAPI_CONNECT_B3_IND
)
589 capincci_alloc(cdev
, CAPIMSG_NCCI(skb
->data
));
591 if (CAPIMSG_COMMAND(skb
->data
) != CAPI_DATA_B3
) {
592 skb_queue_tail(&cdev
->recvqueue
, skb
);
593 wake_up_interruptible(&cdev
->recvwait
);
597 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
598 skb_queue_tail(&cdev
->recvqueue
, skb
);
599 wake_up_interruptible(&cdev
->recvwait
);
601 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
603 np
= capincci_find(cdev
, CAPIMSG_CONTROL(skb
->data
));
605 printk(KERN_ERR
"BUG: capi_signal: ncci not found\n");
606 skb_queue_tail(&cdev
->recvqueue
, skb
);
607 wake_up_interruptible(&cdev
->recvwait
);
613 skb_queue_tail(&cdev
->recvqueue
, skb
);
614 wake_up_interruptible(&cdev
->recvwait
);
617 if (CAPIMSG_SUBCOMMAND(skb
->data
) == CAPI_IND
) {
618 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4 + 4 + 2);
619 pr_debug("capi_signal: DATA_B3_IND %u len=%d\n",
620 datahandle
, skb
->len
-CAPIMSG_LEN(skb
->data
));
621 skb_queue_tail(&mp
->inqueue
, skb
);
623 handle_minor_recv(mp
);
625 } else if (CAPIMSG_SUBCOMMAND(skb
->data
) == CAPI_CONF
) {
627 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4);
628 pr_debug("capi_signal: DATA_B3_CONF %u 0x%x\n",
630 CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4 + 2));
632 capiminor_del_ack(mp
, datahandle
);
633 tty_port_tty_wakeup(&mp
->port
);
634 handle_minor_send(mp
);
637 /* ups, let capi application handle it :-) */
638 skb_queue_tail(&cdev
->recvqueue
, skb
);
639 wake_up_interruptible(&cdev
->recvwait
);
641 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
644 mutex_unlock(&cdev
->lock
);
647 /* -------- file_operations for capidev ----------------------------- */
650 capi_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
652 struct capidev
*cdev
= file
->private_data
;
657 if (!cdev
->ap
.applid
)
660 skb
= skb_dequeue(&cdev
->recvqueue
);
662 if (file
->f_flags
& O_NONBLOCK
)
664 err
= wait_event_interruptible(cdev
->recvwait
,
665 (skb
= skb_dequeue(&cdev
->recvqueue
)));
669 if (skb
->len
> count
) {
670 skb_queue_head(&cdev
->recvqueue
, skb
);
673 if (copy_to_user(buf
, skb
->data
, skb
->len
)) {
674 skb_queue_head(&cdev
->recvqueue
, skb
);
685 capi_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
687 struct capidev
*cdev
= file
->private_data
;
691 if (!cdev
->ap
.applid
)
694 if (count
< CAPIMSG_BASELEN
)
697 skb
= alloc_skb(count
, GFP_USER
);
701 if (copy_from_user(skb_put(skb
, count
), buf
, count
)) {
705 mlen
= CAPIMSG_LEN(skb
->data
);
706 if (CAPIMSG_CMD(skb
->data
) == CAPI_DATA_B3_REQ
) {
707 if (count
< CAPI_DATA_B3_REQ_LEN
||
708 (size_t)(mlen
+ CAPIMSG_DATALEN(skb
->data
)) != count
) {
718 CAPIMSG_SETAPPID(skb
->data
, cdev
->ap
.applid
);
720 if (CAPIMSG_CMD(skb
->data
) == CAPI_DISCONNECT_B3_RESP
) {
721 if (count
< CAPI_DISCONNECT_B3_RESP_LEN
) {
725 mutex_lock(&cdev
->lock
);
726 capincci_free(cdev
, CAPIMSG_NCCI(skb
->data
));
727 mutex_unlock(&cdev
->lock
);
730 cdev
->errcode
= capi20_put_message(&cdev
->ap
, skb
);
740 capi_poll(struct file
*file
, poll_table
*wait
)
742 struct capidev
*cdev
= file
->private_data
;
745 if (!cdev
->ap
.applid
)
748 poll_wait(file
, &(cdev
->recvwait
), wait
);
749 mask
= EPOLLOUT
| EPOLLWRNORM
;
750 if (!skb_queue_empty_lockless(&cdev
->recvqueue
))
751 mask
|= EPOLLIN
| EPOLLRDNORM
;
756 capi_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
758 struct capidev
*cdev
= file
->private_data
;
759 capi_ioctl_struct data
;
760 int retval
= -EINVAL
;
761 void __user
*argp
= (void __user
*)arg
;
765 mutex_lock(&cdev
->lock
);
767 if (cdev
->ap
.applid
) {
771 if (copy_from_user(&cdev
->ap
.rparam
, argp
,
772 sizeof(struct capi_register_params
))) {
776 cdev
->ap
.private = cdev
;
777 cdev
->ap
.recv_message
= capi_recv_message
;
778 cdev
->errcode
= capi20_register(&cdev
->ap
);
779 retval
= (int)cdev
->ap
.applid
;
786 mutex_unlock(&cdev
->lock
);
789 case CAPI_GET_VERSION
:
790 if (copy_from_user(&data
.contr
, argp
,
793 cdev
->errcode
= capi20_get_version(data
.contr
, &data
.version
);
796 if (copy_to_user(argp
, &data
.version
,
797 sizeof(data
.version
)))
801 case CAPI_GET_SERIAL
:
802 if (copy_from_user(&data
.contr
, argp
,
805 cdev
->errcode
= capi20_get_serial(data
.contr
, data
.serial
);
808 if (copy_to_user(argp
, data
.serial
,
809 sizeof(data
.serial
)))
813 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
));
839 case CAPI_GET_MANUFACTURER
:
840 if (copy_from_user(&data
.contr
, argp
,
843 cdev
->errcode
= capi20_get_manufacturer(data
.contr
, data
.manufacturer
);
847 if (copy_to_user(argp
, data
.manufacturer
,
848 sizeof(data
.manufacturer
)))
853 case CAPI_GET_ERRCODE
:
854 data
.errcode
= cdev
->errcode
;
855 cdev
->errcode
= CAPI_NOERROR
;
857 if (copy_to_user(argp
, &data
.errcode
,
858 sizeof(data
.errcode
)))
864 if (capi20_isinstalled() == CAPI_NOERROR
)
868 case CAPI_MANUFACTURER_CMD
: {
869 struct capi_manufacturer_cmd mcmd
;
870 if (!capable(CAP_SYS_ADMIN
))
872 if (copy_from_user(&mcmd
, argp
, sizeof(mcmd
)))
874 return capi20_manufacturer(mcmd
.cmd
, mcmd
.data
);
877 case CAPI_CLR_FLAGS
: {
880 if (copy_from_user(&userflags
, argp
, sizeof(userflags
)))
883 mutex_lock(&cdev
->lock
);
884 if (cmd
== CAPI_SET_FLAGS
)
885 cdev
->userflags
|= userflags
;
887 cdev
->userflags
&= ~userflags
;
888 mutex_unlock(&cdev
->lock
);
892 if (copy_to_user(argp
, &cdev
->userflags
,
893 sizeof(cdev
->userflags
)))
897 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
898 case CAPI_NCCI_OPENCOUNT
:
901 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
902 case CAPI_NCCI_OPENCOUNT
: {
903 struct capincci
*nccip
;
907 if (copy_from_user(&ncci
, argp
, sizeof(ncci
)))
910 mutex_lock(&cdev
->lock
);
911 nccip
= capincci_find(cdev
, (u32
)ncci
);
913 count
= capincci_minor_opencount(nccip
);
914 mutex_unlock(&cdev
->lock
);
918 case CAPI_NCCI_GETUNIT
: {
919 struct capincci
*nccip
;
920 struct capiminor
*mp
;
924 if (copy_from_user(&ncci
, argp
, sizeof(ncci
)))
927 mutex_lock(&cdev
->lock
);
928 nccip
= capincci_find(cdev
, (u32
)ncci
);
934 mutex_unlock(&cdev
->lock
);
937 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
945 capi_unlocked_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
949 mutex_lock(&capi_mutex
);
950 ret
= capi_ioctl(file
, cmd
, arg
);
951 mutex_unlock(&capi_mutex
);
958 capi_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
962 if (cmd
== CAPI_MANUFACTURER_CMD
) {
968 if (!capable(CAP_SYS_ADMIN
))
970 if (copy_from_user(&mcmd32
, compat_ptr(arg
), sizeof(mcmd32
)))
973 mutex_lock(&capi_mutex
);
974 ret
= capi20_manufacturer(mcmd32
.cmd
, compat_ptr(mcmd32
.data
));
975 mutex_unlock(&capi_mutex
);
980 return capi_unlocked_ioctl(file
, cmd
, (unsigned long)compat_ptr(arg
));
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 stream_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 .unlocked_ioctl
= capi_unlocked_ioctl
,
1030 #ifdef CONFIG_COMPAT
1031 .compat_ioctl
= capi_compat_ioctl
,
1034 .release
= capi_release
,
1037 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1038 /* -------- tty_operations for capincci ----------------------------- */
1041 capinc_tty_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
1043 struct capiminor
*mp
= capiminor_get(tty
->index
);
1044 int ret
= tty_standard_install(driver
, tty
);
1047 tty
->driver_data
= mp
;
1053 static void capinc_tty_cleanup(struct tty_struct
*tty
)
1055 struct capiminor
*mp
= tty
->driver_data
;
1056 tty
->driver_data
= NULL
;
1060 static int capinc_tty_open(struct tty_struct
*tty
, struct file
*filp
)
1062 struct capiminor
*mp
= tty
->driver_data
;
1065 err
= tty_port_open(&mp
->port
, tty
, filp
);
1069 handle_minor_recv(mp
);
1073 static void capinc_tty_close(struct tty_struct
*tty
, struct file
*filp
)
1075 struct capiminor
*mp
= tty
->driver_data
;
1077 tty_port_close(&mp
->port
, tty
, filp
);
1080 static int capinc_tty_write(struct tty_struct
*tty
,
1081 const unsigned char *buf
, int count
)
1083 struct capiminor
*mp
= tty
->driver_data
;
1084 struct sk_buff
*skb
;
1086 pr_debug("capinc_tty_write(count=%d)\n", count
);
1088 spin_lock_bh(&mp
->outlock
);
1092 __skb_queue_tail(&mp
->outqueue
, skb
);
1093 mp
->outbytes
+= skb
->len
;
1096 skb
= alloc_skb(CAPI_DATA_B3_REQ_LEN
+ count
, GFP_ATOMIC
);
1098 printk(KERN_ERR
"capinc_tty_write: alloc_skb failed\n");
1099 spin_unlock_bh(&mp
->outlock
);
1103 skb_reserve(skb
, CAPI_DATA_B3_REQ_LEN
);
1104 skb_put_data(skb
, buf
, count
);
1106 __skb_queue_tail(&mp
->outqueue
, skb
);
1107 mp
->outbytes
+= skb
->len
;
1108 spin_unlock_bh(&mp
->outlock
);
1110 handle_minor_send(mp
);
1115 static int capinc_tty_put_char(struct tty_struct
*tty
, unsigned char ch
)
1117 struct capiminor
*mp
= tty
->driver_data
;
1118 bool invoke_send
= false;
1119 struct sk_buff
*skb
;
1122 pr_debug("capinc_put_char(%u)\n", ch
);
1124 spin_lock_bh(&mp
->outlock
);
1127 if (skb_tailroom(skb
) > 0) {
1128 skb_put_u8(skb
, ch
);
1132 __skb_queue_tail(&mp
->outqueue
, skb
);
1133 mp
->outbytes
+= skb
->len
;
1137 skb
= alloc_skb(CAPI_DATA_B3_REQ_LEN
+ CAPI_MAX_BLKSIZE
, GFP_ATOMIC
);
1139 skb_reserve(skb
, CAPI_DATA_B3_REQ_LEN
);
1140 skb_put_u8(skb
, ch
);
1143 printk(KERN_ERR
"capinc_put_char: char %u lost\n", ch
);
1148 spin_unlock_bh(&mp
->outlock
);
1151 handle_minor_send(mp
);
1156 static void capinc_tty_flush_chars(struct tty_struct
*tty
)
1158 struct capiminor
*mp
= tty
->driver_data
;
1159 struct sk_buff
*skb
;
1161 pr_debug("capinc_tty_flush_chars\n");
1163 spin_lock_bh(&mp
->outlock
);
1167 __skb_queue_tail(&mp
->outqueue
, skb
);
1168 mp
->outbytes
+= skb
->len
;
1169 spin_unlock_bh(&mp
->outlock
);
1171 handle_minor_send(mp
);
1173 spin_unlock_bh(&mp
->outlock
);
1175 handle_minor_recv(mp
);
1178 static int capinc_tty_write_room(struct tty_struct
*tty
)
1180 struct capiminor
*mp
= tty
->driver_data
;
1183 room
= CAPINC_MAX_SENDQUEUE
-skb_queue_len(&mp
->outqueue
);
1184 room
*= CAPI_MAX_BLKSIZE
;
1185 pr_debug("capinc_tty_write_room = %d\n", room
);
1189 static int capinc_tty_chars_in_buffer(struct tty_struct
*tty
)
1191 struct capiminor
*mp
= tty
->driver_data
;
1193 pr_debug("capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1194 mp
->outbytes
, mp
->nack
,
1195 skb_queue_len(&mp
->outqueue
),
1196 skb_queue_len(&mp
->inqueue
));
1197 return mp
->outbytes
;
1200 static void capinc_tty_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
1202 pr_debug("capinc_tty_set_termios\n");
1205 static void capinc_tty_throttle(struct tty_struct
*tty
)
1207 struct capiminor
*mp
= tty
->driver_data
;
1208 pr_debug("capinc_tty_throttle\n");
1212 static void capinc_tty_unthrottle(struct tty_struct
*tty
)
1214 struct capiminor
*mp
= tty
->driver_data
;
1216 pr_debug("capinc_tty_unthrottle\n");
1218 handle_minor_recv(mp
);
1221 static void capinc_tty_stop(struct tty_struct
*tty
)
1223 struct capiminor
*mp
= tty
->driver_data
;
1225 pr_debug("capinc_tty_stop\n");
1229 static void capinc_tty_start(struct tty_struct
*tty
)
1231 struct capiminor
*mp
= tty
->driver_data
;
1233 pr_debug("capinc_tty_start\n");
1235 handle_minor_send(mp
);
1238 static void capinc_tty_hangup(struct tty_struct
*tty
)
1240 struct capiminor
*mp
= tty
->driver_data
;
1242 pr_debug("capinc_tty_hangup\n");
1243 tty_port_hangup(&mp
->port
);
1246 static int capinc_tty_break_ctl(struct tty_struct
*tty
, int state
)
1248 pr_debug("capinc_tty_break_ctl(%d)\n", state
);
1252 static void capinc_tty_flush_buffer(struct tty_struct
*tty
)
1254 pr_debug("capinc_tty_flush_buffer\n");
1257 static void capinc_tty_set_ldisc(struct tty_struct
*tty
)
1259 pr_debug("capinc_tty_set_ldisc\n");
1262 static void capinc_tty_send_xchar(struct tty_struct
*tty
, char ch
)
1264 pr_debug("capinc_tty_send_xchar(%d)\n", ch
);
1267 static const struct tty_operations capinc_ops
= {
1268 .open
= capinc_tty_open
,
1269 .close
= capinc_tty_close
,
1270 .write
= capinc_tty_write
,
1271 .put_char
= capinc_tty_put_char
,
1272 .flush_chars
= capinc_tty_flush_chars
,
1273 .write_room
= capinc_tty_write_room
,
1274 .chars_in_buffer
= capinc_tty_chars_in_buffer
,
1275 .set_termios
= capinc_tty_set_termios
,
1276 .throttle
= capinc_tty_throttle
,
1277 .unthrottle
= capinc_tty_unthrottle
,
1278 .stop
= capinc_tty_stop
,
1279 .start
= capinc_tty_start
,
1280 .hangup
= capinc_tty_hangup
,
1281 .break_ctl
= capinc_tty_break_ctl
,
1282 .flush_buffer
= capinc_tty_flush_buffer
,
1283 .set_ldisc
= capinc_tty_set_ldisc
,
1284 .send_xchar
= capinc_tty_send_xchar
,
1285 .install
= capinc_tty_install
,
1286 .cleanup
= capinc_tty_cleanup
,
1289 static int __init
capinc_tty_init(void)
1291 struct tty_driver
*drv
;
1294 if (capi_ttyminors
> CAPINC_MAX_PORTS
)
1295 capi_ttyminors
= CAPINC_MAX_PORTS
;
1296 if (capi_ttyminors
<= 0)
1297 capi_ttyminors
= CAPINC_NR_PORTS
;
1299 capiminors
= kcalloc(capi_ttyminors
, sizeof(struct capiminor
*),
1304 drv
= alloc_tty_driver(capi_ttyminors
);
1309 drv
->driver_name
= "capi_nc";
1310 drv
->name
= "capi!";
1312 drv
->minor_start
= 0;
1313 drv
->type
= TTY_DRIVER_TYPE_SERIAL
;
1314 drv
->subtype
= SERIAL_TYPE_NORMAL
;
1315 drv
->init_termios
= tty_std_termios
;
1316 drv
->init_termios
.c_iflag
= ICRNL
;
1317 drv
->init_termios
.c_oflag
= OPOST
| ONLCR
;
1318 drv
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1319 drv
->init_termios
.c_lflag
= 0;
1321 TTY_DRIVER_REAL_RAW
| TTY_DRIVER_RESET_TERMIOS
|
1322 TTY_DRIVER_DYNAMIC_DEV
;
1323 tty_set_operations(drv
, &capinc_ops
);
1325 err
= tty_register_driver(drv
);
1327 put_tty_driver(drv
);
1329 printk(KERN_ERR
"Couldn't register capi_nc driver\n");
1332 capinc_tty_driver
= drv
;
1336 static void __exit
capinc_tty_exit(void)
1338 tty_unregister_driver(capinc_tty_driver
);
1339 put_tty_driver(capinc_tty_driver
);
1343 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1345 static inline int capinc_tty_init(void)
1350 static inline void capinc_tty_exit(void) { }
1352 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1354 /* -------- /proc functions ----------------------------------------- */
1357 * /proc/capi/capi20:
1358 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1360 static int __maybe_unused
capi20_proc_show(struct seq_file
*m
, void *v
)
1362 struct capidev
*cdev
;
1363 struct list_head
*l
;
1365 mutex_lock(&capidev_list_lock
);
1366 list_for_each(l
, &capidev_list
) {
1367 cdev
= list_entry(l
, struct capidev
, list
);
1368 seq_printf(m
, "0 %d %lu %lu %lu %lu\n",
1370 cdev
->ap
.nrecvctlpkt
,
1371 cdev
->ap
.nrecvdatapkt
,
1372 cdev
->ap
.nsentctlpkt
,
1373 cdev
->ap
.nsentdatapkt
);
1375 mutex_unlock(&capidev_list_lock
);
1380 * /proc/capi/capi20ncci:
1383 static int __maybe_unused
capi20ncci_proc_show(struct seq_file
*m
, void *v
)
1385 struct capidev
*cdev
;
1386 struct capincci
*np
;
1388 mutex_lock(&capidev_list_lock
);
1389 list_for_each_entry(cdev
, &capidev_list
, list
) {
1390 mutex_lock(&cdev
->lock
);
1391 list_for_each_entry(np
, &cdev
->nccis
, list
)
1392 seq_printf(m
, "%d 0x%x\n", cdev
->ap
.applid
, np
->ncci
);
1393 mutex_unlock(&cdev
->lock
);
1395 mutex_unlock(&capidev_list_lock
);
1399 static void __init
proc_init(void)
1401 proc_create_single("capi/capi20", 0, NULL
, capi20_proc_show
);
1402 proc_create_single("capi/capi20ncci", 0, NULL
, capi20ncci_proc_show
);
1405 static void __exit
proc_exit(void)
1407 remove_proc_entry("capi/capi20", NULL
);
1408 remove_proc_entry("capi/capi20ncci", NULL
);
1411 /* -------- init function and module interface ---------------------- */
1414 static int __init
capi_init(void)
1416 const char *compileinfo
;
1424 major_ret
= register_chrdev(capi_major
, "capi20", &capi_fops
);
1425 if (major_ret
< 0) {
1426 printk(KERN_ERR
"capi20: unable to get major %d\n", capi_major
);
1430 capi_class
= class_create(THIS_MODULE
, "capi");
1431 if (IS_ERR(capi_class
)) {
1432 unregister_chrdev(capi_major
, "capi20");
1434 return PTR_ERR(capi_class
);
1437 device_create(capi_class
, NULL
, MKDEV(capi_major
, 0), NULL
, "capi20");
1439 if (capinc_tty_init() < 0) {
1440 device_destroy(capi_class
, MKDEV(capi_major
, 0));
1441 class_destroy(capi_class
);
1442 unregister_chrdev(capi_major
, "capi20");
1449 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1450 compileinfo
= " (middleware)";
1452 compileinfo
= " (no middleware)";
1454 printk(KERN_NOTICE
"CAPI 2.0 started up with major %d%s\n",
1455 capi_major
, compileinfo
);
1460 static void __exit
capi_exit(void)
1464 device_destroy(capi_class
, MKDEV(capi_major
, 0));
1465 class_destroy(capi_class
);
1466 unregister_chrdev(capi_major
, "capi20");
1473 module_init(capi_init
);
1474 module_exit(capi_exit
);