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 const struct class capi_class
= {
55 static int capi_major
= 68; /* allocated */
57 module_param_named(major
, capi_major
, uint
, 0);
59 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
60 #define CAPINC_NR_PORTS 32
61 #define CAPINC_MAX_PORTS 256
63 static int capi_ttyminors
= CAPINC_NR_PORTS
;
65 module_param_named(ttyminors
, capi_ttyminors
, uint
, 0);
66 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
68 /* -------- defines ------------------------------------------------- */
70 #define CAPINC_MAX_RECVQUEUE 10
71 #define CAPINC_MAX_SENDQUEUE 10
72 #define CAPI_MAX_BLKSIZE 2048
74 /* -------- data structures ----------------------------------------- */
80 struct ackqueue_entry
{
81 struct list_head list
;
88 struct capi20_appl
*ap
;
97 struct sk_buff_head inqueue
;
99 struct sk_buff_head outqueue
;
101 struct sk_buff
*outskb
;
105 struct list_head ackqueue
;
111 struct list_head list
;
113 struct capidev
*cdev
;
114 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
115 struct capiminor
*minorp
;
116 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
120 struct list_head list
;
121 struct capi20_appl ap
;
125 struct sk_buff_head recvqueue
;
126 wait_queue_head_t recvwait
;
128 struct list_head nccis
;
133 /* -------- global variables ---------------------------------------- */
135 static DEFINE_MUTEX(capidev_list_lock
);
136 static LIST_HEAD(capidev_list
);
138 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
140 static DEFINE_SPINLOCK(capiminors_lock
);
141 static struct capiminor
**capiminors
;
143 static struct tty_driver
*capinc_tty_driver
;
145 /* -------- datahandles --------------------------------------------- */
147 static int capiminor_add_ack(struct capiminor
*mp
, u16 datahandle
)
149 struct ackqueue_entry
*n
;
151 n
= kmalloc(sizeof(*n
), GFP_ATOMIC
);
153 printk(KERN_ERR
"capi: alloc datahandle failed\n");
156 n
->datahandle
= datahandle
;
157 INIT_LIST_HEAD(&n
->list
);
158 spin_lock_bh(&mp
->ackqlock
);
159 list_add_tail(&n
->list
, &mp
->ackqueue
);
161 spin_unlock_bh(&mp
->ackqlock
);
165 static int capiminor_del_ack(struct capiminor
*mp
, u16 datahandle
)
167 struct ackqueue_entry
*p
, *tmp
;
169 spin_lock_bh(&mp
->ackqlock
);
170 list_for_each_entry_safe(p
, tmp
, &mp
->ackqueue
, list
) {
171 if (p
->datahandle
== datahandle
) {
174 spin_unlock_bh(&mp
->ackqlock
);
179 spin_unlock_bh(&mp
->ackqlock
);
183 static void capiminor_del_all_ack(struct capiminor
*mp
)
185 struct ackqueue_entry
*p
, *tmp
;
187 list_for_each_entry_safe(p
, tmp
, &mp
->ackqueue
, list
) {
195 /* -------- struct capiminor ---------------------------------------- */
197 static void capiminor_destroy(struct tty_port
*port
)
199 struct capiminor
*mp
= container_of(port
, struct capiminor
, port
);
201 kfree_skb(mp
->outskb
);
202 skb_queue_purge(&mp
->inqueue
);
203 skb_queue_purge(&mp
->outqueue
);
204 capiminor_del_all_ack(mp
);
208 static const struct tty_port_operations capiminor_port_ops
= {
209 .destruct
= capiminor_destroy
,
212 static struct capiminor
*capiminor_alloc(struct capi20_appl
*ap
, u32 ncci
)
214 struct capiminor
*mp
;
218 mp
= kzalloc(sizeof(*mp
), GFP_KERNEL
);
220 printk(KERN_ERR
"capi: can't alloc capiminor\n");
226 INIT_LIST_HEAD(&mp
->ackqueue
);
227 spin_lock_init(&mp
->ackqlock
);
229 skb_queue_head_init(&mp
->inqueue
);
230 skb_queue_head_init(&mp
->outqueue
);
231 spin_lock_init(&mp
->outlock
);
233 tty_port_init(&mp
->port
);
234 mp
->port
.ops
= &capiminor_port_ops
;
236 /* Allocate the least unused minor number. */
237 spin_lock(&capiminors_lock
);
238 for (minor
= 0; minor
< capi_ttyminors
; minor
++)
239 if (!capiminors
[minor
]) {
240 capiminors
[minor
] = mp
;
243 spin_unlock(&capiminors_lock
);
245 if (minor
== capi_ttyminors
) {
246 printk(KERN_NOTICE
"capi: out of minors\n");
252 dev
= tty_port_register_device(&mp
->port
, capinc_tty_driver
, minor
,
260 spin_lock(&capiminors_lock
);
261 capiminors
[minor
] = NULL
;
262 spin_unlock(&capiminors_lock
);
265 tty_port_put(&mp
->port
);
269 static struct capiminor
*capiminor_get(unsigned int minor
)
271 struct capiminor
*mp
;
273 spin_lock(&capiminors_lock
);
274 mp
= capiminors
[minor
];
276 tty_port_get(&mp
->port
);
277 spin_unlock(&capiminors_lock
);
282 static inline void capiminor_put(struct capiminor
*mp
)
284 tty_port_put(&mp
->port
);
287 static void capiminor_free(struct capiminor
*mp
)
289 tty_unregister_device(capinc_tty_driver
, mp
->minor
);
291 spin_lock(&capiminors_lock
);
292 capiminors
[mp
->minor
] = NULL
;
293 spin_unlock(&capiminors_lock
);
298 /* -------- struct capincci ----------------------------------------- */
300 static void capincci_alloc_minor(struct capidev
*cdev
, struct capincci
*np
)
302 if (cdev
->userflags
& CAPIFLAG_HIGHJACKING
)
303 np
->minorp
= capiminor_alloc(&cdev
->ap
, np
->ncci
);
306 static void capincci_free_minor(struct capincci
*np
)
308 struct capiminor
*mp
= np
->minorp
;
309 struct tty_struct
*tty
;
312 tty
= tty_port_tty_get(&mp
->port
);
322 static inline unsigned int capincci_minor_opencount(struct capincci
*np
)
324 struct capiminor
*mp
= np
->minorp
;
325 unsigned int count
= 0;
326 struct tty_struct
*tty
;
329 tty
= tty_port_tty_get(&mp
->port
);
338 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
341 capincci_alloc_minor(struct capidev
*cdev
, struct capincci
*np
) { }
342 static inline void capincci_free_minor(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 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
376 static struct capincci
*capincci_find(struct capidev
*cdev
, u32 ncci
)
380 list_for_each_entry(np
, &cdev
->nccis
, list
)
381 if (np
->ncci
== ncci
)
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 capiminor
*mp
;
581 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
583 mutex_lock(&cdev
->lock
);
585 if (CAPIMSG_CMD(skb
->data
) == CAPI_CONNECT_B3_CONF
) {
586 u16 info
= CAPIMSG_U16(skb
->data
, 12); // Info field
587 if ((info
& 0xff00) == 0)
588 capincci_alloc(cdev
, CAPIMSG_NCCI(skb
->data
));
590 if (CAPIMSG_CMD(skb
->data
) == CAPI_CONNECT_B3_IND
)
591 capincci_alloc(cdev
, CAPIMSG_NCCI(skb
->data
));
593 if (CAPIMSG_COMMAND(skb
->data
) != CAPI_DATA_B3
) {
594 skb_queue_tail(&cdev
->recvqueue
, skb
);
595 wake_up_interruptible(&cdev
->recvwait
);
599 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
600 skb_queue_tail(&cdev
->recvqueue
, skb
);
601 wake_up_interruptible(&cdev
->recvwait
);
603 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
605 np
= capincci_find(cdev
, CAPIMSG_CONTROL(skb
->data
));
607 printk(KERN_ERR
"BUG: capi_signal: ncci not found\n");
608 skb_queue_tail(&cdev
->recvqueue
, skb
);
609 wake_up_interruptible(&cdev
->recvwait
);
615 skb_queue_tail(&cdev
->recvqueue
, skb
);
616 wake_up_interruptible(&cdev
->recvwait
);
619 if (CAPIMSG_SUBCOMMAND(skb
->data
) == CAPI_IND
) {
620 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4 + 4 + 2);
621 pr_debug("capi_signal: DATA_B3_IND %u len=%d\n",
622 datahandle
, skb
->len
-CAPIMSG_LEN(skb
->data
));
623 skb_queue_tail(&mp
->inqueue
, skb
);
625 handle_minor_recv(mp
);
627 } else if (CAPIMSG_SUBCOMMAND(skb
->data
) == CAPI_CONF
) {
629 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4);
630 pr_debug("capi_signal: DATA_B3_CONF %u 0x%x\n",
632 CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4 + 2));
634 capiminor_del_ack(mp
, datahandle
);
635 tty_port_tty_wakeup(&mp
->port
);
636 handle_minor_send(mp
);
639 /* ups, let capi application handle it :-) */
640 skb_queue_tail(&cdev
->recvqueue
, skb
);
641 wake_up_interruptible(&cdev
->recvwait
);
643 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
646 mutex_unlock(&cdev
->lock
);
649 /* -------- file_operations for capidev ----------------------------- */
652 capi_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
654 struct capidev
*cdev
= file
->private_data
;
659 if (!cdev
->ap
.applid
)
662 skb
= skb_dequeue(&cdev
->recvqueue
);
664 if (file
->f_flags
& O_NONBLOCK
)
666 err
= wait_event_interruptible(cdev
->recvwait
,
667 (skb
= skb_dequeue(&cdev
->recvqueue
)));
671 if (skb
->len
> count
) {
672 skb_queue_head(&cdev
->recvqueue
, skb
);
675 if (copy_to_user(buf
, skb
->data
, skb
->len
)) {
676 skb_queue_head(&cdev
->recvqueue
, skb
);
687 capi_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
689 struct capidev
*cdev
= file
->private_data
;
693 if (!cdev
->ap
.applid
)
696 if (count
< CAPIMSG_BASELEN
)
699 skb
= alloc_skb(count
, GFP_USER
);
703 if (copy_from_user(skb_put(skb
, count
), buf
, count
)) {
707 mlen
= CAPIMSG_LEN(skb
->data
);
708 if (CAPIMSG_CMD(skb
->data
) == CAPI_DATA_B3_REQ
) {
709 if (count
< CAPI_DATA_B3_REQ_LEN
||
710 (size_t)(mlen
+ CAPIMSG_DATALEN(skb
->data
)) != count
) {
720 CAPIMSG_SETAPPID(skb
->data
, cdev
->ap
.applid
);
722 if (CAPIMSG_CMD(skb
->data
) == CAPI_DISCONNECT_B3_RESP
) {
723 if (count
< CAPI_DISCONNECT_B3_RESP_LEN
) {
727 mutex_lock(&cdev
->lock
);
728 capincci_free(cdev
, CAPIMSG_NCCI(skb
->data
));
729 mutex_unlock(&cdev
->lock
);
732 cdev
->errcode
= capi20_put_message(&cdev
->ap
, skb
);
742 capi_poll(struct file
*file
, poll_table
*wait
)
744 struct capidev
*cdev
= file
->private_data
;
747 if (!cdev
->ap
.applid
)
750 poll_wait(file
, &(cdev
->recvwait
), wait
);
751 mask
= EPOLLOUT
| EPOLLWRNORM
;
752 if (!skb_queue_empty_lockless(&cdev
->recvqueue
))
753 mask
|= EPOLLIN
| EPOLLRDNORM
;
758 capi_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
760 struct capidev
*cdev
= file
->private_data
;
761 capi_ioctl_struct data
;
762 int retval
= -EINVAL
;
763 void __user
*argp
= (void __user
*)arg
;
767 mutex_lock(&cdev
->lock
);
769 if (cdev
->ap
.applid
) {
773 if (copy_from_user(&cdev
->ap
.rparam
, argp
,
774 sizeof(struct capi_register_params
))) {
778 cdev
->ap
.private = cdev
;
779 cdev
->ap
.recv_message
= capi_recv_message
;
780 cdev
->errcode
= capi20_register(&cdev
->ap
);
781 retval
= (int)cdev
->ap
.applid
;
788 mutex_unlock(&cdev
->lock
);
791 case CAPI_GET_VERSION
:
792 if (copy_from_user(&data
.contr
, argp
,
795 cdev
->errcode
= capi20_get_version(data
.contr
, &data
.version
);
798 if (copy_to_user(argp
, &data
.version
,
799 sizeof(data
.version
)))
803 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
:
816 if (copy_from_user(&data
.contr
, argp
,
820 if (data
.contr
== 0) {
821 cdev
->errcode
= capi20_get_profile(data
.contr
, &data
.profile
);
825 retval
= copy_to_user(argp
,
826 &data
.profile
.ncontroller
,
827 sizeof(data
.profile
.ncontroller
));
830 cdev
->errcode
= capi20_get_profile(data
.contr
, &data
.profile
);
834 retval
= copy_to_user(argp
, &data
.profile
,
835 sizeof(data
.profile
));
841 case CAPI_GET_MANUFACTURER
:
842 if (copy_from_user(&data
.contr
, argp
,
845 cdev
->errcode
= capi20_get_manufacturer(data
.contr
, data
.manufacturer
);
849 if (copy_to_user(argp
, data
.manufacturer
,
850 sizeof(data
.manufacturer
)))
855 case CAPI_GET_ERRCODE
:
856 data
.errcode
= cdev
->errcode
;
857 cdev
->errcode
= CAPI_NOERROR
;
859 if (copy_to_user(argp
, &data
.errcode
,
860 sizeof(data
.errcode
)))
866 if (capi20_isinstalled() == CAPI_NOERROR
)
870 case CAPI_MANUFACTURER_CMD
: {
871 struct capi_manufacturer_cmd mcmd
;
872 if (!capable(CAP_SYS_ADMIN
))
874 if (copy_from_user(&mcmd
, argp
, sizeof(mcmd
)))
876 return capi20_manufacturer(mcmd
.cmd
, mcmd
.data
);
879 case CAPI_CLR_FLAGS
: {
882 if (copy_from_user(&userflags
, argp
, sizeof(userflags
)))
885 mutex_lock(&cdev
->lock
);
886 if (cmd
== CAPI_SET_FLAGS
)
887 cdev
->userflags
|= userflags
;
889 cdev
->userflags
&= ~userflags
;
890 mutex_unlock(&cdev
->lock
);
894 if (copy_to_user(argp
, &cdev
->userflags
,
895 sizeof(cdev
->userflags
)))
899 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
900 case CAPI_NCCI_OPENCOUNT
:
903 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
904 case CAPI_NCCI_OPENCOUNT
: {
905 struct capincci
*nccip
;
909 if (copy_from_user(&ncci
, argp
, sizeof(ncci
)))
912 mutex_lock(&cdev
->lock
);
913 nccip
= capincci_find(cdev
, (u32
)ncci
);
915 count
= capincci_minor_opencount(nccip
);
916 mutex_unlock(&cdev
->lock
);
920 case CAPI_NCCI_GETUNIT
: {
921 struct capincci
*nccip
;
922 struct capiminor
*mp
;
926 if (copy_from_user(&ncci
, argp
, sizeof(ncci
)))
929 mutex_lock(&cdev
->lock
);
930 nccip
= capincci_find(cdev
, (u32
)ncci
);
936 mutex_unlock(&cdev
->lock
);
939 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
947 capi_unlocked_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
951 mutex_lock(&capi_mutex
);
952 ret
= capi_ioctl(file
, cmd
, arg
);
953 mutex_unlock(&capi_mutex
);
960 capi_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
964 if (cmd
== CAPI_MANUFACTURER_CMD
) {
970 if (!capable(CAP_SYS_ADMIN
))
972 if (copy_from_user(&mcmd32
, compat_ptr(arg
), sizeof(mcmd32
)))
975 mutex_lock(&capi_mutex
);
976 ret
= capi20_manufacturer(mcmd32
.cmd
, compat_ptr(mcmd32
.data
));
977 mutex_unlock(&capi_mutex
);
982 return capi_unlocked_ioctl(file
, cmd
, (unsigned long)compat_ptr(arg
));
986 static int capi_open(struct inode
*inode
, struct file
*file
)
988 struct capidev
*cdev
;
990 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
994 mutex_init(&cdev
->lock
);
995 skb_queue_head_init(&cdev
->recvqueue
);
996 init_waitqueue_head(&cdev
->recvwait
);
997 INIT_LIST_HEAD(&cdev
->nccis
);
998 file
->private_data
= cdev
;
1000 mutex_lock(&capidev_list_lock
);
1001 list_add_tail(&cdev
->list
, &capidev_list
);
1002 mutex_unlock(&capidev_list_lock
);
1004 return stream_open(inode
, file
);
1007 static int capi_release(struct inode
*inode
, struct file
*file
)
1009 struct capidev
*cdev
= file
->private_data
;
1011 mutex_lock(&capidev_list_lock
);
1012 list_del(&cdev
->list
);
1013 mutex_unlock(&capidev_list_lock
);
1015 if (cdev
->ap
.applid
)
1016 capi20_release(&cdev
->ap
);
1017 skb_queue_purge(&cdev
->recvqueue
);
1018 capincci_free(cdev
, 0xffffffff);
1024 static const struct file_operations capi_fops
=
1026 .owner
= THIS_MODULE
,
1028 .write
= capi_write
,
1030 .unlocked_ioctl
= capi_unlocked_ioctl
,
1031 #ifdef CONFIG_COMPAT
1032 .compat_ioctl
= capi_compat_ioctl
,
1035 .release
= capi_release
,
1038 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1039 /* -------- tty_operations for capincci ----------------------------- */
1042 capinc_tty_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
1044 struct capiminor
*mp
= capiminor_get(tty
->index
);
1045 int ret
= tty_standard_install(driver
, tty
);
1048 tty
->driver_data
= mp
;
1054 static void capinc_tty_cleanup(struct tty_struct
*tty
)
1056 struct capiminor
*mp
= tty
->driver_data
;
1057 tty
->driver_data
= NULL
;
1061 static int capinc_tty_open(struct tty_struct
*tty
, struct file
*filp
)
1063 struct capiminor
*mp
= tty
->driver_data
;
1066 err
= tty_port_open(&mp
->port
, tty
, filp
);
1070 handle_minor_recv(mp
);
1074 static void capinc_tty_close(struct tty_struct
*tty
, struct file
*filp
)
1076 struct capiminor
*mp
= tty
->driver_data
;
1078 tty_port_close(&mp
->port
, tty
, filp
);
1081 static ssize_t
capinc_tty_write(struct tty_struct
*tty
, const u8
*buf
,
1084 struct capiminor
*mp
= tty
->driver_data
;
1085 struct sk_buff
*skb
;
1087 pr_debug("capinc_tty_write(count=%zu)\n", count
);
1089 spin_lock_bh(&mp
->outlock
);
1093 __skb_queue_tail(&mp
->outqueue
, skb
);
1094 mp
->outbytes
+= skb
->len
;
1097 skb
= alloc_skb(CAPI_DATA_B3_REQ_LEN
+ count
, GFP_ATOMIC
);
1099 printk(KERN_ERR
"capinc_tty_write: alloc_skb failed\n");
1100 spin_unlock_bh(&mp
->outlock
);
1104 skb_reserve(skb
, CAPI_DATA_B3_REQ_LEN
);
1105 skb_put_data(skb
, buf
, count
);
1107 __skb_queue_tail(&mp
->outqueue
, skb
);
1108 mp
->outbytes
+= skb
->len
;
1109 spin_unlock_bh(&mp
->outlock
);
1111 handle_minor_send(mp
);
1116 static int capinc_tty_put_char(struct tty_struct
*tty
, u8 ch
)
1118 struct capiminor
*mp
= tty
->driver_data
;
1119 bool invoke_send
= false;
1120 struct sk_buff
*skb
;
1123 pr_debug("capinc_put_char(%u)\n", ch
);
1125 spin_lock_bh(&mp
->outlock
);
1128 if (skb_tailroom(skb
) > 0) {
1129 skb_put_u8(skb
, ch
);
1133 __skb_queue_tail(&mp
->outqueue
, skb
);
1134 mp
->outbytes
+= skb
->len
;
1138 skb
= alloc_skb(CAPI_DATA_B3_REQ_LEN
+ CAPI_MAX_BLKSIZE
, GFP_ATOMIC
);
1140 skb_reserve(skb
, CAPI_DATA_B3_REQ_LEN
);
1141 skb_put_u8(skb
, ch
);
1144 printk(KERN_ERR
"capinc_put_char: char %u lost\n", ch
);
1149 spin_unlock_bh(&mp
->outlock
);
1152 handle_minor_send(mp
);
1157 static void capinc_tty_flush_chars(struct tty_struct
*tty
)
1159 struct capiminor
*mp
= tty
->driver_data
;
1160 struct sk_buff
*skb
;
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 unsigned 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 = %u\n", room
);
1188 static unsigned 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_throttle(struct tty_struct
*tty
)
1201 struct capiminor
*mp
= tty
->driver_data
;
1205 static void capinc_tty_unthrottle(struct tty_struct
*tty
)
1207 struct capiminor
*mp
= tty
->driver_data
;
1210 handle_minor_recv(mp
);
1213 static void capinc_tty_stop(struct tty_struct
*tty
)
1215 struct capiminor
*mp
= tty
->driver_data
;
1220 static void capinc_tty_start(struct tty_struct
*tty
)
1222 struct capiminor
*mp
= tty
->driver_data
;
1225 handle_minor_send(mp
);
1228 static void capinc_tty_hangup(struct tty_struct
*tty
)
1230 struct capiminor
*mp
= tty
->driver_data
;
1232 tty_port_hangup(&mp
->port
);
1235 static void capinc_tty_send_xchar(struct tty_struct
*tty
, u8 ch
)
1237 pr_debug("capinc_tty_send_xchar(%u)\n", ch
);
1240 static const struct tty_operations capinc_ops
= {
1241 .open
= capinc_tty_open
,
1242 .close
= capinc_tty_close
,
1243 .write
= capinc_tty_write
,
1244 .put_char
= capinc_tty_put_char
,
1245 .flush_chars
= capinc_tty_flush_chars
,
1246 .write_room
= capinc_tty_write_room
,
1247 .chars_in_buffer
= capinc_tty_chars_in_buffer
,
1248 .throttle
= capinc_tty_throttle
,
1249 .unthrottle
= capinc_tty_unthrottle
,
1250 .stop
= capinc_tty_stop
,
1251 .start
= capinc_tty_start
,
1252 .hangup
= capinc_tty_hangup
,
1253 .send_xchar
= capinc_tty_send_xchar
,
1254 .install
= capinc_tty_install
,
1255 .cleanup
= capinc_tty_cleanup
,
1258 static int __init
capinc_tty_init(void)
1260 struct tty_driver
*drv
;
1263 if (capi_ttyminors
> CAPINC_MAX_PORTS
)
1264 capi_ttyminors
= CAPINC_MAX_PORTS
;
1265 if (capi_ttyminors
<= 0)
1266 capi_ttyminors
= CAPINC_NR_PORTS
;
1268 capiminors
= kcalloc(capi_ttyminors
, sizeof(struct capiminor
*),
1273 drv
= tty_alloc_driver(capi_ttyminors
, TTY_DRIVER_REAL_RAW
|
1274 TTY_DRIVER_RESET_TERMIOS
| TTY_DRIVER_DYNAMIC_DEV
);
1277 return PTR_ERR(drv
);
1279 drv
->driver_name
= "capi_nc";
1280 drv
->name
= "capi!";
1282 drv
->minor_start
= 0;
1283 drv
->type
= TTY_DRIVER_TYPE_SERIAL
;
1284 drv
->subtype
= SERIAL_TYPE_NORMAL
;
1285 drv
->init_termios
= tty_std_termios
;
1286 drv
->init_termios
.c_iflag
= ICRNL
;
1287 drv
->init_termios
.c_oflag
= OPOST
| ONLCR
;
1288 drv
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1289 drv
->init_termios
.c_lflag
= 0;
1290 tty_set_operations(drv
, &capinc_ops
);
1292 err
= tty_register_driver(drv
);
1294 tty_driver_kref_put(drv
);
1296 printk(KERN_ERR
"Couldn't register capi_nc driver\n");
1299 capinc_tty_driver
= drv
;
1303 static void __exit
capinc_tty_exit(void)
1305 tty_unregister_driver(capinc_tty_driver
);
1306 tty_driver_kref_put(capinc_tty_driver
);
1310 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1312 static inline int capinc_tty_init(void)
1317 static inline void capinc_tty_exit(void) { }
1319 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1321 /* -------- /proc functions ----------------------------------------- */
1324 * /proc/capi/capi20:
1325 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1327 static int __maybe_unused
capi20_proc_show(struct seq_file
*m
, void *v
)
1329 struct capidev
*cdev
;
1330 struct list_head
*l
;
1332 mutex_lock(&capidev_list_lock
);
1333 list_for_each(l
, &capidev_list
) {
1334 cdev
= list_entry(l
, struct capidev
, list
);
1335 seq_printf(m
, "0 %d %lu %lu %lu %lu\n",
1337 cdev
->ap
.nrecvctlpkt
,
1338 cdev
->ap
.nrecvdatapkt
,
1339 cdev
->ap
.nsentctlpkt
,
1340 cdev
->ap
.nsentdatapkt
);
1342 mutex_unlock(&capidev_list_lock
);
1347 * /proc/capi/capi20ncci:
1350 static int __maybe_unused
capi20ncci_proc_show(struct seq_file
*m
, void *v
)
1352 struct capidev
*cdev
;
1353 struct capincci
*np
;
1355 mutex_lock(&capidev_list_lock
);
1356 list_for_each_entry(cdev
, &capidev_list
, list
) {
1357 mutex_lock(&cdev
->lock
);
1358 list_for_each_entry(np
, &cdev
->nccis
, list
)
1359 seq_printf(m
, "%d 0x%x\n", cdev
->ap
.applid
, np
->ncci
);
1360 mutex_unlock(&cdev
->lock
);
1362 mutex_unlock(&capidev_list_lock
);
1366 static void __init
proc_init(void)
1368 proc_create_single("capi/capi20", 0, NULL
, capi20_proc_show
);
1369 proc_create_single("capi/capi20ncci", 0, NULL
, capi20ncci_proc_show
);
1372 static void __exit
proc_exit(void)
1374 remove_proc_entry("capi/capi20", NULL
);
1375 remove_proc_entry("capi/capi20ncci", NULL
);
1378 /* -------- init function and module interface ---------------------- */
1381 static int __init
capi_init(void)
1383 const char *compileinfo
;
1391 major_ret
= register_chrdev(capi_major
, "capi20", &capi_fops
);
1392 if (major_ret
< 0) {
1393 printk(KERN_ERR
"capi20: unable to get major %d\n", capi_major
);
1398 ret
= class_register(&capi_class
);
1400 unregister_chrdev(capi_major
, "capi20");
1405 device_create(&capi_class
, NULL
, MKDEV(capi_major
, 0), NULL
, "capi20");
1407 if (capinc_tty_init() < 0) {
1408 device_destroy(&capi_class
, MKDEV(capi_major
, 0));
1409 class_unregister(&capi_class
);
1410 unregister_chrdev(capi_major
, "capi20");
1417 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1418 compileinfo
= " (middleware)";
1420 compileinfo
= " (no middleware)";
1422 printk(KERN_NOTICE
"CAPI 2.0 started up with major %d%s\n",
1423 capi_major
, compileinfo
);
1428 static void __exit
capi_exit(void)
1432 device_destroy(&capi_class
, MKDEV(capi_major
, 0));
1433 class_unregister(&capi_class
);
1434 unregister_chrdev(capi_major
, "capi20");
1441 module_init(capi_init
);
1442 module_exit(capi_exit
);