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/timer.h>
24 #include <linux/wait.h>
25 #include <linux/tty.h>
26 #include <linux/netdevice.h>
27 #include <linux/ppp_defs.h>
28 #include <linux/if_ppp.h>
29 #include <linux/skbuff.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/poll.h>
33 #include <linux/capi.h>
34 #include <linux/kernelcapi.h>
35 #include <linux/init.h>
36 #include <linux/device.h>
37 #include <linux/moduleparam.h>
38 #include <linux/isdn/capiutil.h>
39 #include <linux/isdn/capicmd.h>
41 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
42 MODULE_AUTHOR("Carsten Paeth");
43 MODULE_LICENSE("GPL");
45 /* -------- driver information -------------------------------------- */
47 static DEFINE_MUTEX(capi_mutex
);
48 static struct class *capi_class
;
49 static int capi_major
= 68; /* allocated */
51 module_param_named(major
, capi_major
, uint
, 0);
53 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
54 #define CAPINC_NR_PORTS 32
55 #define CAPINC_MAX_PORTS 256
57 static int capi_ttyminors
= CAPINC_NR_PORTS
;
59 module_param_named(ttyminors
, capi_ttyminors
, uint
, 0);
60 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
62 /* -------- defines ------------------------------------------------- */
64 #define CAPINC_MAX_RECVQUEUE 10
65 #define CAPINC_MAX_SENDQUEUE 10
66 #define CAPI_MAX_BLKSIZE 2048
68 /* -------- data structures ----------------------------------------- */
74 struct ackqueue_entry
{
75 struct list_head list
;
84 struct capi20_appl
*ap
;
93 struct sk_buff_head inqueue
;
95 struct sk_buff_head outqueue
;
97 struct sk_buff
*outskb
;
101 struct list_head ackqueue
;
107 struct list_head list
;
109 struct capidev
*cdev
;
110 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
111 struct capiminor
*minorp
;
112 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
116 struct list_head list
;
117 struct capi20_appl ap
;
121 struct sk_buff_head recvqueue
;
122 wait_queue_head_t recvwait
;
124 struct list_head nccis
;
129 /* -------- global variables ---------------------------------------- */
131 static DEFINE_MUTEX(capidev_list_lock
);
132 static LIST_HEAD(capidev_list
);
134 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
136 static DEFINE_SPINLOCK(capiminors_lock
);
137 static struct capiminor
**capiminors
;
139 static struct tty_driver
*capinc_tty_driver
;
141 /* -------- datahandles --------------------------------------------- */
143 static int capiminor_add_ack(struct capiminor
*mp
, u16 datahandle
)
145 struct ackqueue_entry
*n
;
147 n
= kmalloc(sizeof(*n
), GFP_ATOMIC
);
149 printk(KERN_ERR
"capi: alloc datahandle failed\n");
152 n
->datahandle
= datahandle
;
153 INIT_LIST_HEAD(&n
->list
);
154 spin_lock_bh(&mp
->ackqlock
);
155 list_add_tail(&n
->list
, &mp
->ackqueue
);
157 spin_unlock_bh(&mp
->ackqlock
);
161 static int capiminor_del_ack(struct capiminor
*mp
, u16 datahandle
)
163 struct ackqueue_entry
*p
, *tmp
;
165 spin_lock_bh(&mp
->ackqlock
);
166 list_for_each_entry_safe(p
, tmp
, &mp
->ackqueue
, list
) {
167 if (p
->datahandle
== datahandle
) {
170 spin_unlock_bh(&mp
->ackqlock
);
175 spin_unlock_bh(&mp
->ackqlock
);
179 static void capiminor_del_all_ack(struct capiminor
*mp
)
181 struct ackqueue_entry
*p
, *tmp
;
183 list_for_each_entry_safe(p
, tmp
, &mp
->ackqueue
, list
) {
191 /* -------- struct capiminor ---------------------------------------- */
193 static const struct tty_port_operations capiminor_port_ops
; /* we have none */
195 static struct capiminor
*capiminor_alloc(struct capi20_appl
*ap
, u32 ncci
)
197 struct capiminor
*mp
;
201 mp
= kzalloc(sizeof(*mp
), GFP_KERNEL
);
203 printk(KERN_ERR
"capi: can't alloc capiminor\n");
207 kref_init(&mp
->kref
);
211 INIT_LIST_HEAD(&mp
->ackqueue
);
212 spin_lock_init(&mp
->ackqlock
);
214 skb_queue_head_init(&mp
->inqueue
);
215 skb_queue_head_init(&mp
->outqueue
);
216 spin_lock_init(&mp
->outlock
);
218 tty_port_init(&mp
->port
);
219 mp
->port
.ops
= &capiminor_port_ops
;
221 /* Allocate the least unused minor number. */
222 spin_lock(&capiminors_lock
);
223 for (minor
= 0; minor
< capi_ttyminors
; minor
++)
224 if (!capiminors
[minor
]) {
225 capiminors
[minor
] = mp
;
228 spin_unlock(&capiminors_lock
);
230 if (minor
== capi_ttyminors
) {
231 printk(KERN_NOTICE
"capi: out of minors\n");
237 dev
= tty_register_device(capinc_tty_driver
, minor
, NULL
);
244 spin_lock(&capiminors_lock
);
245 capiminors
[minor
] = NULL
;
246 spin_unlock(&capiminors_lock
);
253 static void capiminor_destroy(struct kref
*kref
)
255 struct capiminor
*mp
= container_of(kref
, struct capiminor
, kref
);
257 kfree_skb(mp
->outskb
);
258 skb_queue_purge(&mp
->inqueue
);
259 skb_queue_purge(&mp
->outqueue
);
260 capiminor_del_all_ack(mp
);
264 static struct capiminor
*capiminor_get(unsigned int minor
)
266 struct capiminor
*mp
;
268 spin_lock(&capiminors_lock
);
269 mp
= capiminors
[minor
];
272 spin_unlock(&capiminors_lock
);
277 static inline void capiminor_put(struct capiminor
*mp
)
279 kref_put(&mp
->kref
, capiminor_destroy
);
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 static inline unsigned int capincci_minor_opencount(struct capincci
*np
)
344 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
346 static struct capincci
*capincci_alloc(struct capidev
*cdev
, u32 ncci
)
350 np
= kzalloc(sizeof(*np
), GFP_KERNEL
);
356 capincci_alloc_minor(cdev
, np
);
358 list_add_tail(&np
->list
, &cdev
->nccis
);
363 static void capincci_free(struct capidev
*cdev
, u32 ncci
)
365 struct capincci
*np
, *tmp
;
367 list_for_each_entry_safe(np
, tmp
, &cdev
->nccis
, list
)
368 if (ncci
== 0xffffffff || np
->ncci
== ncci
) {
369 capincci_free_minor(np
);
375 static struct capincci
*capincci_find(struct capidev
*cdev
, u32 ncci
)
379 list_for_each_entry(np
, &cdev
->nccis
, list
)
380 if (np
->ncci
== ncci
)
385 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
386 /* -------- handle data queue --------------------------------------- */
388 static struct sk_buff
*
389 gen_data_b3_resp_for(struct capiminor
*mp
, struct sk_buff
*skb
)
391 struct sk_buff
*nskb
;
392 nskb
= alloc_skb(CAPI_DATA_B3_RESP_LEN
, GFP_KERNEL
);
394 u16 datahandle
= CAPIMSG_U16(skb
->data
,CAPIMSG_BASELEN
+4+4+2);
395 unsigned char *s
= skb_put(nskb
, CAPI_DATA_B3_RESP_LEN
);
396 capimsg_setu16(s
, 0, CAPI_DATA_B3_RESP_LEN
);
397 capimsg_setu16(s
, 2, mp
->ap
->applid
);
398 capimsg_setu8 (s
, 4, CAPI_DATA_B3
);
399 capimsg_setu8 (s
, 5, CAPI_RESP
);
400 capimsg_setu16(s
, 6, atomic_inc_return(&mp
->msgid
));
401 capimsg_setu32(s
, 8, mp
->ncci
);
402 capimsg_setu16(s
, 12, datahandle
);
407 static int handle_recv_skb(struct capiminor
*mp
, struct sk_buff
*skb
)
409 unsigned int datalen
= skb
->len
- CAPIMSG_LEN(skb
->data
);
410 struct tty_struct
*tty
;
411 struct sk_buff
*nskb
;
412 u16 errcode
, datahandle
;
413 struct tty_ldisc
*ld
;
416 tty
= tty_port_tty_get(&mp
->port
);
418 pr_debug("capi: currently no receiver\n");
422 ld
= tty_ldisc_ref(tty
);
424 /* fatal error, do not requeue */
430 if (ld
->ops
->receive_buf
== NULL
) {
431 pr_debug("capi: ldisc has no receive_buf function\n");
432 /* fatal error, do not requeue */
436 pr_debug("capi: recv tty throttled\n");
440 if (tty
->receive_room
< datalen
) {
441 pr_debug("capi: no room in tty\n");
445 nskb
= gen_data_b3_resp_for(mp
, skb
);
447 printk(KERN_ERR
"capi: gen_data_b3_resp failed\n");
451 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4);
453 errcode
= capi20_put_message(mp
->ap
, nskb
);
455 if (errcode
== CAPI_NOERROR
) {
456 skb_pull(skb
, CAPIMSG_LEN(skb
->data
));
457 pr_debug("capi: DATA_B3_RESP %u len=%d => ldisc\n",
458 datahandle
, skb
->len
);
459 ld
->ops
->receive_buf(tty
, skb
->data
, NULL
, skb
->len
);
461 printk(KERN_ERR
"capi: send DATA_B3_RESP failed=%x\n",
465 if (errcode
== CAPI_SENDQUEUEFULL
)
481 static void handle_minor_recv(struct capiminor
*mp
)
485 while ((skb
= skb_dequeue(&mp
->inqueue
)) != NULL
)
486 if (handle_recv_skb(mp
, skb
) < 0) {
487 skb_queue_head(&mp
->inqueue
, skb
);
492 static void handle_minor_send(struct capiminor
*mp
)
494 struct tty_struct
*tty
;
500 tty
= tty_port_tty_get(&mp
->port
);
504 if (mp
->ttyoutstop
) {
505 pr_debug("capi: send: tty stopped\n");
511 spin_lock_bh(&mp
->outlock
);
512 skb
= __skb_dequeue(&mp
->outqueue
);
514 spin_unlock_bh(&mp
->outlock
);
519 spin_unlock_bh(&mp
->outlock
);
521 datahandle
= atomic_inc_return(&mp
->datahandle
);
522 skb_push(skb
, CAPI_DATA_B3_REQ_LEN
);
523 memset(skb
->data
, 0, CAPI_DATA_B3_REQ_LEN
);
524 capimsg_setu16(skb
->data
, 0, CAPI_DATA_B3_REQ_LEN
);
525 capimsg_setu16(skb
->data
, 2, mp
->ap
->applid
);
526 capimsg_setu8 (skb
->data
, 4, CAPI_DATA_B3
);
527 capimsg_setu8 (skb
->data
, 5, CAPI_REQ
);
528 capimsg_setu16(skb
->data
, 6, atomic_inc_return(&mp
->msgid
));
529 capimsg_setu32(skb
->data
, 8, mp
->ncci
); /* NCCI */
530 capimsg_setu32(skb
->data
, 12, (u32
)(long)skb
->data
);/* Data32 */
531 capimsg_setu16(skb
->data
, 16, len
); /* Data length */
532 capimsg_setu16(skb
->data
, 18, datahandle
);
533 capimsg_setu16(skb
->data
, 20, 0); /* Flags */
535 if (capiminor_add_ack(mp
, datahandle
) < 0) {
536 skb_pull(skb
, CAPI_DATA_B3_REQ_LEN
);
538 spin_lock_bh(&mp
->outlock
);
539 __skb_queue_head(&mp
->outqueue
, skb
);
541 spin_unlock_bh(&mp
->outlock
);
545 errcode
= capi20_put_message(mp
->ap
, skb
);
546 if (errcode
== CAPI_NOERROR
) {
547 pr_debug("capi: DATA_B3_REQ %u len=%u\n",
551 capiminor_del_ack(mp
, datahandle
);
553 if (errcode
== CAPI_SENDQUEUEFULL
) {
554 skb_pull(skb
, CAPI_DATA_B3_REQ_LEN
);
556 spin_lock_bh(&mp
->outlock
);
557 __skb_queue_head(&mp
->outqueue
, skb
);
559 spin_unlock_bh(&mp
->outlock
);
564 /* ups, drop packet */
565 printk(KERN_ERR
"capi: put_message = %x\n", errcode
);
571 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
572 /* -------- function called by lower level -------------------------- */
574 static void capi_recv_message(struct capi20_appl
*ap
, struct sk_buff
*skb
)
576 struct capidev
*cdev
= ap
->private;
577 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
578 struct tty_struct
*tty
;
579 struct capiminor
*mp
;
581 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
584 mutex_lock(&cdev
->lock
);
586 if (CAPIMSG_CMD(skb
->data
) == CAPI_CONNECT_B3_CONF
) {
587 u16 info
= CAPIMSG_U16(skb
->data
, 12); // Info field
588 if ((info
& 0xff00) == 0)
589 capincci_alloc(cdev
, CAPIMSG_NCCI(skb
->data
));
591 if (CAPIMSG_CMD(skb
->data
) == CAPI_CONNECT_B3_IND
)
592 capincci_alloc(cdev
, CAPIMSG_NCCI(skb
->data
));
594 if (CAPIMSG_COMMAND(skb
->data
) != CAPI_DATA_B3
) {
595 skb_queue_tail(&cdev
->recvqueue
, skb
);
596 wake_up_interruptible(&cdev
->recvwait
);
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
);
608 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
609 skb_queue_tail(&cdev
->recvqueue
, skb
);
610 wake_up_interruptible(&cdev
->recvwait
);
612 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
616 skb_queue_tail(&cdev
->recvqueue
, skb
);
617 wake_up_interruptible(&cdev
->recvwait
);
620 if (CAPIMSG_SUBCOMMAND(skb
->data
) == CAPI_IND
) {
621 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+4+4+2);
622 pr_debug("capi_signal: DATA_B3_IND %u len=%d\n",
623 datahandle
, skb
->len
-CAPIMSG_LEN(skb
->data
));
624 skb_queue_tail(&mp
->inqueue
, skb
);
626 handle_minor_recv(mp
);
628 } else if (CAPIMSG_SUBCOMMAND(skb
->data
) == CAPI_CONF
) {
630 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+4);
631 pr_debug("capi_signal: DATA_B3_CONF %u 0x%x\n",
633 CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+4+2));
635 capiminor_del_ack(mp
, datahandle
);
636 tty
= tty_port_tty_get(&mp
->port
);
641 handle_minor_send(mp
);
644 /* ups, let capi application handle it :-) */
645 skb_queue_tail(&cdev
->recvqueue
, skb
);
646 wake_up_interruptible(&cdev
->recvwait
);
648 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
651 mutex_unlock(&cdev
->lock
);
654 /* -------- file_operations for capidev ----------------------------- */
657 capi_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
659 struct capidev
*cdev
= file
->private_data
;
664 if (!cdev
->ap
.applid
)
667 skb
= skb_dequeue(&cdev
->recvqueue
);
669 if (file
->f_flags
& O_NONBLOCK
)
671 err
= wait_event_interruptible(cdev
->recvwait
,
672 (skb
= skb_dequeue(&cdev
->recvqueue
)));
676 if (skb
->len
> count
) {
677 skb_queue_head(&cdev
->recvqueue
, skb
);
680 if (copy_to_user(buf
, skb
->data
, skb
->len
)) {
681 skb_queue_head(&cdev
->recvqueue
, skb
);
692 capi_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
694 struct capidev
*cdev
= file
->private_data
;
698 if (!cdev
->ap
.applid
)
701 skb
= alloc_skb(count
, GFP_USER
);
705 if (copy_from_user(skb_put(skb
, count
), buf
, count
)) {
709 mlen
= CAPIMSG_LEN(skb
->data
);
710 if (CAPIMSG_CMD(skb
->data
) == CAPI_DATA_B3_REQ
) {
711 if ((size_t)(mlen
+ CAPIMSG_DATALEN(skb
->data
)) != count
) {
721 CAPIMSG_SETAPPID(skb
->data
, cdev
->ap
.applid
);
723 if (CAPIMSG_CMD(skb
->data
) == CAPI_DISCONNECT_B3_RESP
) {
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
;
742 unsigned int mask
= 0;
744 if (!cdev
->ap
.applid
)
747 poll_wait(file
, &(cdev
->recvwait
), wait
);
748 mask
= POLLOUT
| POLLWRNORM
;
749 if (!skb_queue_empty(&cdev
->recvqueue
))
750 mask
|= POLLIN
| POLLRDNORM
;
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
:
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
)))
802 case CAPI_GET_SERIAL
:
804 if (copy_from_user(&data
.contr
, argp
,
807 cdev
->errcode
= capi20_get_serial (data
.contr
, data
.serial
);
810 if (copy_to_user(argp
, data
.serial
,
811 sizeof(data
.serial
)))
815 case CAPI_GET_PROFILE
:
817 if (copy_from_user(&data
.contr
, argp
,
821 if (data
.contr
== 0) {
822 cdev
->errcode
= capi20_get_profile(data
.contr
, &data
.profile
);
826 retval
= copy_to_user(argp
,
827 &data
.profile
.ncontroller
,
828 sizeof(data
.profile
.ncontroller
));
831 cdev
->errcode
= capi20_get_profile(data
.contr
, &data
.profile
);
835 retval
= copy_to_user(argp
, &data
.profile
,
836 sizeof(data
.profile
));
843 case CAPI_GET_MANUFACTURER
:
845 if (copy_from_user(&data
.contr
, argp
,
848 cdev
->errcode
= capi20_get_manufacturer(data
.contr
, data
.manufacturer
);
852 if (copy_to_user(argp
, data
.manufacturer
,
853 sizeof(data
.manufacturer
)))
858 case CAPI_GET_ERRCODE
:
859 data
.errcode
= cdev
->errcode
;
860 cdev
->errcode
= CAPI_NOERROR
;
862 if (copy_to_user(argp
, &data
.errcode
,
863 sizeof(data
.errcode
)))
869 if (capi20_isinstalled() == CAPI_NOERROR
)
873 case CAPI_MANUFACTURER_CMD
:
875 struct capi_manufacturer_cmd mcmd
;
876 if (!capable(CAP_SYS_ADMIN
))
878 if (copy_from_user(&mcmd
, argp
, sizeof(mcmd
)))
880 return capi20_manufacturer(mcmd
.cmd
, mcmd
.data
);
885 case CAPI_CLR_FLAGS
: {
888 if (copy_from_user(&userflags
, argp
, sizeof(userflags
)))
891 mutex_lock(&cdev
->lock
);
892 if (cmd
== CAPI_SET_FLAGS
)
893 cdev
->userflags
|= userflags
;
895 cdev
->userflags
&= ~userflags
;
896 mutex_unlock(&cdev
->lock
);
900 if (copy_to_user(argp
, &cdev
->userflags
,
901 sizeof(cdev
->userflags
)))
905 case CAPI_NCCI_OPENCOUNT
: {
906 struct capincci
*nccip
;
910 if (copy_from_user(&ncci
, argp
, sizeof(ncci
)))
913 mutex_lock(&cdev
->lock
);
914 nccip
= capincci_find(cdev
, (u32
)ncci
);
916 count
= capincci_minor_opencount(nccip
);
917 mutex_unlock(&cdev
->lock
);
921 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
922 case CAPI_NCCI_GETUNIT
: {
923 struct capincci
*nccip
;
924 struct capiminor
*mp
;
928 if (copy_from_user(&ncci
, argp
, sizeof(ncci
)))
931 mutex_lock(&cdev
->lock
);
932 nccip
= capincci_find(cdev
, (u32
)ncci
);
938 mutex_unlock(&cdev
->lock
);
941 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
949 capi_unlocked_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
953 mutex_lock(&capi_mutex
);
954 ret
= capi_ioctl(file
, cmd
, arg
);
955 mutex_unlock(&capi_mutex
);
960 static int capi_open(struct inode
*inode
, struct file
*file
)
962 struct capidev
*cdev
;
964 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
968 mutex_init(&cdev
->lock
);
969 skb_queue_head_init(&cdev
->recvqueue
);
970 init_waitqueue_head(&cdev
->recvwait
);
971 INIT_LIST_HEAD(&cdev
->nccis
);
972 file
->private_data
= cdev
;
974 mutex_lock(&capidev_list_lock
);
975 list_add_tail(&cdev
->list
, &capidev_list
);
976 mutex_unlock(&capidev_list_lock
);
978 return nonseekable_open(inode
, file
);
981 static int capi_release(struct inode
*inode
, struct file
*file
)
983 struct capidev
*cdev
= file
->private_data
;
985 mutex_lock(&capidev_list_lock
);
986 list_del(&cdev
->list
);
987 mutex_unlock(&capidev_list_lock
);
990 capi20_release(&cdev
->ap
);
991 skb_queue_purge(&cdev
->recvqueue
);
992 capincci_free(cdev
, 0xffffffff);
998 static const struct file_operations capi_fops
=
1000 .owner
= THIS_MODULE
,
1001 .llseek
= no_llseek
,
1003 .write
= capi_write
,
1005 .unlocked_ioctl
= capi_unlocked_ioctl
,
1007 .release
= capi_release
,
1010 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1011 /* -------- tty_operations for capincci ----------------------------- */
1014 capinc_tty_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
1016 int idx
= tty
->index
;
1017 struct capiminor
*mp
= capiminor_get(idx
);
1018 int ret
= tty_init_termios(tty
);
1021 tty_driver_kref_get(driver
);
1023 tty
->driver_data
= mp
;
1024 driver
->ttys
[idx
] = tty
;
1030 static void capinc_tty_cleanup(struct tty_struct
*tty
)
1032 struct capiminor
*mp
= tty
->driver_data
;
1033 tty
->driver_data
= NULL
;
1037 static int capinc_tty_open(struct tty_struct
*tty
, struct file
*filp
)
1039 struct capiminor
*mp
= tty
->driver_data
;
1042 err
= tty_port_open(&mp
->port
, tty
, filp
);
1046 handle_minor_recv(mp
);
1050 static void capinc_tty_close(struct tty_struct
*tty
, struct file
*filp
)
1052 struct capiminor
*mp
= tty
->driver_data
;
1054 tty_port_close(&mp
->port
, tty
, filp
);
1057 static int capinc_tty_write(struct tty_struct
*tty
,
1058 const unsigned char *buf
, int count
)
1060 struct capiminor
*mp
= tty
->driver_data
;
1061 struct sk_buff
*skb
;
1063 pr_debug("capinc_tty_write(count=%d)\n", count
);
1065 spin_lock_bh(&mp
->outlock
);
1069 __skb_queue_tail(&mp
->outqueue
, skb
);
1070 mp
->outbytes
+= skb
->len
;
1073 skb
= alloc_skb(CAPI_DATA_B3_REQ_LEN
+count
, GFP_ATOMIC
);
1075 printk(KERN_ERR
"capinc_tty_write: alloc_skb failed\n");
1076 spin_unlock_bh(&mp
->outlock
);
1080 skb_reserve(skb
, CAPI_DATA_B3_REQ_LEN
);
1081 memcpy(skb_put(skb
, count
), buf
, count
);
1083 __skb_queue_tail(&mp
->outqueue
, skb
);
1084 mp
->outbytes
+= skb
->len
;
1085 spin_unlock_bh(&mp
->outlock
);
1087 handle_minor_send(mp
);
1092 static int capinc_tty_put_char(struct tty_struct
*tty
, unsigned char ch
)
1094 struct capiminor
*mp
= tty
->driver_data
;
1095 bool invoke_send
= false;
1096 struct sk_buff
*skb
;
1099 pr_debug("capinc_put_char(%u)\n", ch
);
1101 spin_lock_bh(&mp
->outlock
);
1104 if (skb_tailroom(skb
) > 0) {
1105 *(skb_put(skb
, 1)) = ch
;
1109 __skb_queue_tail(&mp
->outqueue
, skb
);
1110 mp
->outbytes
+= skb
->len
;
1114 skb
= alloc_skb(CAPI_DATA_B3_REQ_LEN
+CAPI_MAX_BLKSIZE
, GFP_ATOMIC
);
1116 skb_reserve(skb
, CAPI_DATA_B3_REQ_LEN
);
1117 *(skb_put(skb
, 1)) = ch
;
1120 printk(KERN_ERR
"capinc_put_char: char %u lost\n", ch
);
1125 spin_unlock_bh(&mp
->outlock
);
1128 handle_minor_send(mp
);
1133 static void capinc_tty_flush_chars(struct tty_struct
*tty
)
1135 struct capiminor
*mp
= tty
->driver_data
;
1136 struct sk_buff
*skb
;
1138 pr_debug("capinc_tty_flush_chars\n");
1140 spin_lock_bh(&mp
->outlock
);
1144 __skb_queue_tail(&mp
->outqueue
, skb
);
1145 mp
->outbytes
+= skb
->len
;
1146 spin_unlock_bh(&mp
->outlock
);
1148 handle_minor_send(mp
);
1150 spin_unlock_bh(&mp
->outlock
);
1152 handle_minor_recv(mp
);
1155 static int capinc_tty_write_room(struct tty_struct
*tty
)
1157 struct capiminor
*mp
= tty
->driver_data
;
1160 room
= CAPINC_MAX_SENDQUEUE
-skb_queue_len(&mp
->outqueue
);
1161 room
*= CAPI_MAX_BLKSIZE
;
1162 pr_debug("capinc_tty_write_room = %d\n", room
);
1166 static int capinc_tty_chars_in_buffer(struct tty_struct
*tty
)
1168 struct capiminor
*mp
= tty
->driver_data
;
1170 pr_debug("capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1171 mp
->outbytes
, mp
->nack
,
1172 skb_queue_len(&mp
->outqueue
),
1173 skb_queue_len(&mp
->inqueue
));
1174 return mp
->outbytes
;
1177 static int capinc_tty_ioctl(struct tty_struct
*tty
,
1178 unsigned int cmd
, unsigned long arg
)
1180 return -ENOIOCTLCMD
;
1183 static void capinc_tty_set_termios(struct tty_struct
*tty
, struct ktermios
* old
)
1185 pr_debug("capinc_tty_set_termios\n");
1188 static void capinc_tty_throttle(struct tty_struct
*tty
)
1190 struct capiminor
*mp
= tty
->driver_data
;
1191 pr_debug("capinc_tty_throttle\n");
1195 static void capinc_tty_unthrottle(struct tty_struct
*tty
)
1197 struct capiminor
*mp
= tty
->driver_data
;
1199 pr_debug("capinc_tty_unthrottle\n");
1201 handle_minor_recv(mp
);
1204 static void capinc_tty_stop(struct tty_struct
*tty
)
1206 struct capiminor
*mp
= tty
->driver_data
;
1208 pr_debug("capinc_tty_stop\n");
1212 static void capinc_tty_start(struct tty_struct
*tty
)
1214 struct capiminor
*mp
= tty
->driver_data
;
1216 pr_debug("capinc_tty_start\n");
1218 handle_minor_send(mp
);
1221 static void capinc_tty_hangup(struct tty_struct
*tty
)
1223 struct capiminor
*mp
= tty
->driver_data
;
1225 pr_debug("capinc_tty_hangup\n");
1226 tty_port_hangup(&mp
->port
);
1229 static int capinc_tty_break_ctl(struct tty_struct
*tty
, int state
)
1231 pr_debug("capinc_tty_break_ctl(%d)\n", state
);
1235 static void capinc_tty_flush_buffer(struct tty_struct
*tty
)
1237 pr_debug("capinc_tty_flush_buffer\n");
1240 static void capinc_tty_set_ldisc(struct tty_struct
*tty
)
1242 pr_debug("capinc_tty_set_ldisc\n");
1245 static void capinc_tty_send_xchar(struct tty_struct
*tty
, char ch
)
1247 pr_debug("capinc_tty_send_xchar(%d)\n", ch
);
1250 static const struct tty_operations capinc_ops
= {
1251 .open
= capinc_tty_open
,
1252 .close
= capinc_tty_close
,
1253 .write
= capinc_tty_write
,
1254 .put_char
= capinc_tty_put_char
,
1255 .flush_chars
= capinc_tty_flush_chars
,
1256 .write_room
= capinc_tty_write_room
,
1257 .chars_in_buffer
= capinc_tty_chars_in_buffer
,
1258 .ioctl
= capinc_tty_ioctl
,
1259 .set_termios
= capinc_tty_set_termios
,
1260 .throttle
= capinc_tty_throttle
,
1261 .unthrottle
= capinc_tty_unthrottle
,
1262 .stop
= capinc_tty_stop
,
1263 .start
= capinc_tty_start
,
1264 .hangup
= capinc_tty_hangup
,
1265 .break_ctl
= capinc_tty_break_ctl
,
1266 .flush_buffer
= capinc_tty_flush_buffer
,
1267 .set_ldisc
= capinc_tty_set_ldisc
,
1268 .send_xchar
= capinc_tty_send_xchar
,
1269 .install
= capinc_tty_install
,
1270 .cleanup
= capinc_tty_cleanup
,
1273 static int __init
capinc_tty_init(void)
1275 struct tty_driver
*drv
;
1278 if (capi_ttyminors
> CAPINC_MAX_PORTS
)
1279 capi_ttyminors
= CAPINC_MAX_PORTS
;
1280 if (capi_ttyminors
<= 0)
1281 capi_ttyminors
= CAPINC_NR_PORTS
;
1283 capiminors
= kzalloc(sizeof(struct capi_minor
*) * capi_ttyminors
,
1288 drv
= alloc_tty_driver(capi_ttyminors
);
1293 drv
->owner
= THIS_MODULE
;
1294 drv
->driver_name
= "capi_nc";
1297 drv
->minor_start
= 0;
1298 drv
->type
= TTY_DRIVER_TYPE_SERIAL
;
1299 drv
->subtype
= SERIAL_TYPE_NORMAL
;
1300 drv
->init_termios
= tty_std_termios
;
1301 drv
->init_termios
.c_iflag
= ICRNL
;
1302 drv
->init_termios
.c_oflag
= OPOST
| ONLCR
;
1303 drv
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1304 drv
->init_termios
.c_lflag
= 0;
1306 TTY_DRIVER_REAL_RAW
| TTY_DRIVER_RESET_TERMIOS
|
1307 TTY_DRIVER_DYNAMIC_DEV
;
1308 tty_set_operations(drv
, &capinc_ops
);
1310 err
= tty_register_driver(drv
);
1312 put_tty_driver(drv
);
1314 printk(KERN_ERR
"Couldn't register capi_nc driver\n");
1317 capinc_tty_driver
= drv
;
1321 static void __exit
capinc_tty_exit(void)
1323 tty_unregister_driver(capinc_tty_driver
);
1324 put_tty_driver(capinc_tty_driver
);
1328 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1330 static inline int capinc_tty_init(void)
1335 static inline void capinc_tty_exit(void) { }
1337 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1339 /* -------- /proc functions ----------------------------------------- */
1342 * /proc/capi/capi20:
1343 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1345 static int capi20_proc_show(struct seq_file
*m
, void *v
)
1347 struct capidev
*cdev
;
1348 struct list_head
*l
;
1350 mutex_lock(&capidev_list_lock
);
1351 list_for_each(l
, &capidev_list
) {
1352 cdev
= list_entry(l
, struct capidev
, list
);
1353 seq_printf(m
, "0 %d %lu %lu %lu %lu\n",
1355 cdev
->ap
.nrecvctlpkt
,
1356 cdev
->ap
.nrecvdatapkt
,
1357 cdev
->ap
.nsentctlpkt
,
1358 cdev
->ap
.nsentdatapkt
);
1360 mutex_unlock(&capidev_list_lock
);
1364 static int capi20_proc_open(struct inode
*inode
, struct file
*file
)
1366 return single_open(file
, capi20_proc_show
, NULL
);
1369 static const struct file_operations capi20_proc_fops
= {
1370 .owner
= THIS_MODULE
,
1371 .open
= capi20_proc_open
,
1373 .llseek
= seq_lseek
,
1374 .release
= single_release
,
1378 * /proc/capi/capi20ncci:
1381 static int capi20ncci_proc_show(struct seq_file
*m
, void *v
)
1383 struct capidev
*cdev
;
1384 struct capincci
*np
;
1386 mutex_lock(&capidev_list_lock
);
1387 list_for_each_entry(cdev
, &capidev_list
, list
) {
1388 mutex_lock(&cdev
->lock
);
1389 list_for_each_entry(np
, &cdev
->nccis
, list
)
1390 seq_printf(m
, "%d 0x%x\n", cdev
->ap
.applid
, np
->ncci
);
1391 mutex_unlock(&cdev
->lock
);
1393 mutex_unlock(&capidev_list_lock
);
1397 static int capi20ncci_proc_open(struct inode
*inode
, struct file
*file
)
1399 return single_open(file
, capi20ncci_proc_show
, NULL
);
1402 static const struct file_operations capi20ncci_proc_fops
= {
1403 .owner
= THIS_MODULE
,
1404 .open
= capi20ncci_proc_open
,
1406 .llseek
= seq_lseek
,
1407 .release
= single_release
,
1410 static void __init
proc_init(void)
1412 proc_create("capi/capi20", 0, NULL
, &capi20_proc_fops
);
1413 proc_create("capi/capi20ncci", 0, NULL
, &capi20ncci_proc_fops
);
1416 static void __exit
proc_exit(void)
1418 remove_proc_entry("capi/capi20", NULL
);
1419 remove_proc_entry("capi/capi20ncci", NULL
);
1422 /* -------- init function and module interface ---------------------- */
1425 static int __init
capi_init(void)
1427 const char *compileinfo
;
1430 major_ret
= register_chrdev(capi_major
, "capi20", &capi_fops
);
1431 if (major_ret
< 0) {
1432 printk(KERN_ERR
"capi20: unable to get major %d\n", capi_major
);
1435 capi_class
= class_create(THIS_MODULE
, "capi");
1436 if (IS_ERR(capi_class
)) {
1437 unregister_chrdev(capi_major
, "capi20");
1438 return PTR_ERR(capi_class
);
1441 device_create(capi_class
, NULL
, MKDEV(capi_major
, 0), NULL
, "capi");
1443 if (capinc_tty_init() < 0) {
1444 device_destroy(capi_class
, MKDEV(capi_major
, 0));
1445 class_destroy(capi_class
);
1446 unregister_chrdev(capi_major
, "capi20");
1452 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1453 compileinfo
= " (middleware)";
1455 compileinfo
= " (no middleware)";
1457 printk(KERN_NOTICE
"CAPI 2.0 started up with major %d%s\n",
1458 capi_major
, compileinfo
);
1463 static void __exit
capi_exit(void)
1467 device_destroy(capi_class
, MKDEV(capi_major
, 0));
1468 class_destroy(capi_class
);
1469 unregister_chrdev(capi_major
, "capi20");
1474 module_init(capi_init
);
1475 module_exit(capi_exit
);