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/ppp-ioctl.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
;
82 struct capi20_appl
*ap
;
91 struct sk_buff_head inqueue
;
93 struct sk_buff_head outqueue
;
95 struct sk_buff
*outskb
;
99 struct list_head ackqueue
;
105 struct list_head list
;
107 struct capidev
*cdev
;
108 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
109 struct capiminor
*minorp
;
110 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
114 struct list_head list
;
115 struct capi20_appl ap
;
119 struct sk_buff_head recvqueue
;
120 wait_queue_head_t recvwait
;
122 struct list_head nccis
;
127 /* -------- global variables ---------------------------------------- */
129 static DEFINE_MUTEX(capidev_list_lock
);
130 static LIST_HEAD(capidev_list
);
132 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
134 static DEFINE_SPINLOCK(capiminors_lock
);
135 static struct capiminor
**capiminors
;
137 static struct tty_driver
*capinc_tty_driver
;
139 /* -------- datahandles --------------------------------------------- */
141 static int capiminor_add_ack(struct capiminor
*mp
, u16 datahandle
)
143 struct ackqueue_entry
*n
;
145 n
= kmalloc(sizeof(*n
), GFP_ATOMIC
);
147 printk(KERN_ERR
"capi: alloc datahandle failed\n");
150 n
->datahandle
= datahandle
;
151 INIT_LIST_HEAD(&n
->list
);
152 spin_lock_bh(&mp
->ackqlock
);
153 list_add_tail(&n
->list
, &mp
->ackqueue
);
155 spin_unlock_bh(&mp
->ackqlock
);
159 static int capiminor_del_ack(struct capiminor
*mp
, u16 datahandle
)
161 struct ackqueue_entry
*p
, *tmp
;
163 spin_lock_bh(&mp
->ackqlock
);
164 list_for_each_entry_safe(p
, tmp
, &mp
->ackqueue
, list
) {
165 if (p
->datahandle
== datahandle
) {
168 spin_unlock_bh(&mp
->ackqlock
);
173 spin_unlock_bh(&mp
->ackqlock
);
177 static void capiminor_del_all_ack(struct capiminor
*mp
)
179 struct ackqueue_entry
*p
, *tmp
;
181 list_for_each_entry_safe(p
, tmp
, &mp
->ackqueue
, list
) {
189 /* -------- struct capiminor ---------------------------------------- */
191 static void capiminor_destroy(struct tty_port
*port
)
193 struct capiminor
*mp
= container_of(port
, struct capiminor
, port
);
195 kfree_skb(mp
->outskb
);
196 skb_queue_purge(&mp
->inqueue
);
197 skb_queue_purge(&mp
->outqueue
);
198 capiminor_del_all_ack(mp
);
202 static const struct tty_port_operations capiminor_port_ops
= {
203 .destruct
= capiminor_destroy
,
206 static struct capiminor
*capiminor_alloc(struct capi20_appl
*ap
, u32 ncci
)
208 struct capiminor
*mp
;
212 mp
= kzalloc(sizeof(*mp
), GFP_KERNEL
);
214 printk(KERN_ERR
"capi: can't alloc capiminor\n");
220 INIT_LIST_HEAD(&mp
->ackqueue
);
221 spin_lock_init(&mp
->ackqlock
);
223 skb_queue_head_init(&mp
->inqueue
);
224 skb_queue_head_init(&mp
->outqueue
);
225 spin_lock_init(&mp
->outlock
);
227 tty_port_init(&mp
->port
);
228 mp
->port
.ops
= &capiminor_port_ops
;
230 /* Allocate the least unused minor number. */
231 spin_lock(&capiminors_lock
);
232 for (minor
= 0; minor
< capi_ttyminors
; minor
++)
233 if (!capiminors
[minor
]) {
234 capiminors
[minor
] = mp
;
237 spin_unlock(&capiminors_lock
);
239 if (minor
== capi_ttyminors
) {
240 printk(KERN_NOTICE
"capi: out of minors\n");
246 dev
= tty_port_register_device(&mp
->port
, capinc_tty_driver
, minor
,
254 spin_lock(&capiminors_lock
);
255 capiminors
[minor
] = NULL
;
256 spin_unlock(&capiminors_lock
);
259 tty_port_put(&mp
->port
);
263 static struct capiminor
*capiminor_get(unsigned int minor
)
265 struct capiminor
*mp
;
267 spin_lock(&capiminors_lock
);
268 mp
= capiminors
[minor
];
270 tty_port_get(&mp
->port
);
271 spin_unlock(&capiminors_lock
);
276 static inline void capiminor_put(struct capiminor
*mp
)
278 tty_port_put(&mp
->port
);
281 static void capiminor_free(struct capiminor
*mp
)
283 tty_unregister_device(capinc_tty_driver
, mp
->minor
);
285 spin_lock(&capiminors_lock
);
286 capiminors
[mp
->minor
] = NULL
;
287 spin_unlock(&capiminors_lock
);
292 /* -------- struct capincci ----------------------------------------- */
294 static void capincci_alloc_minor(struct capidev
*cdev
, struct capincci
*np
)
296 if (cdev
->userflags
& CAPIFLAG_HIGHJACKING
)
297 np
->minorp
= capiminor_alloc(&cdev
->ap
, np
->ncci
);
300 static void capincci_free_minor(struct capincci
*np
)
302 struct capiminor
*mp
= np
->minorp
;
303 struct tty_struct
*tty
;
306 tty
= tty_port_tty_get(&mp
->port
);
316 static inline unsigned int capincci_minor_opencount(struct capincci
*np
)
318 struct capiminor
*mp
= np
->minorp
;
319 unsigned int count
= 0;
320 struct tty_struct
*tty
;
323 tty
= tty_port_tty_get(&mp
->port
);
332 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
335 capincci_alloc_minor(struct capidev
*cdev
, struct capincci
*np
) { }
336 static inline void capincci_free_minor(struct capincci
*np
) { }
338 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
340 static struct capincci
*capincci_alloc(struct capidev
*cdev
, u32 ncci
)
344 np
= kzalloc(sizeof(*np
), GFP_KERNEL
);
350 capincci_alloc_minor(cdev
, np
);
352 list_add_tail(&np
->list
, &cdev
->nccis
);
357 static void capincci_free(struct capidev
*cdev
, u32 ncci
)
359 struct capincci
*np
, *tmp
;
361 list_for_each_entry_safe(np
, tmp
, &cdev
->nccis
, list
)
362 if (ncci
== 0xffffffff || np
->ncci
== ncci
) {
363 capincci_free_minor(np
);
369 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
370 static struct capincci
*capincci_find(struct capidev
*cdev
, u32 ncci
)
374 list_for_each_entry(np
, &cdev
->nccis
, list
)
375 if (np
->ncci
== ncci
)
380 /* -------- handle data queue --------------------------------------- */
382 static struct sk_buff
*
383 gen_data_b3_resp_for(struct capiminor
*mp
, struct sk_buff
*skb
)
385 struct sk_buff
*nskb
;
386 nskb
= alloc_skb(CAPI_DATA_B3_RESP_LEN
, GFP_KERNEL
);
388 u16 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4 + 4 + 2);
389 unsigned char *s
= skb_put(nskb
, CAPI_DATA_B3_RESP_LEN
);
390 capimsg_setu16(s
, 0, CAPI_DATA_B3_RESP_LEN
);
391 capimsg_setu16(s
, 2, mp
->ap
->applid
);
392 capimsg_setu8 (s
, 4, CAPI_DATA_B3
);
393 capimsg_setu8 (s
, 5, CAPI_RESP
);
394 capimsg_setu16(s
, 6, atomic_inc_return(&mp
->msgid
));
395 capimsg_setu32(s
, 8, mp
->ncci
);
396 capimsg_setu16(s
, 12, datahandle
);
401 static int handle_recv_skb(struct capiminor
*mp
, struct sk_buff
*skb
)
403 unsigned int datalen
= skb
->len
- CAPIMSG_LEN(skb
->data
);
404 struct tty_struct
*tty
;
405 struct sk_buff
*nskb
;
406 u16 errcode
, datahandle
;
407 struct tty_ldisc
*ld
;
410 tty
= tty_port_tty_get(&mp
->port
);
412 pr_debug("capi: currently no receiver\n");
416 ld
= tty_ldisc_ref(tty
);
418 /* fatal error, do not requeue */
424 if (ld
->ops
->receive_buf
== NULL
) {
425 pr_debug("capi: ldisc has no receive_buf function\n");
426 /* fatal error, do not requeue */
430 pr_debug("capi: recv tty throttled\n");
434 if (tty
->receive_room
< datalen
) {
435 pr_debug("capi: no room in tty\n");
439 nskb
= gen_data_b3_resp_for(mp
, skb
);
441 printk(KERN_ERR
"capi: gen_data_b3_resp failed\n");
445 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4);
447 errcode
= capi20_put_message(mp
->ap
, nskb
);
449 if (errcode
== CAPI_NOERROR
) {
450 skb_pull(skb
, CAPIMSG_LEN(skb
->data
));
451 pr_debug("capi: DATA_B3_RESP %u len=%d => ldisc\n",
452 datahandle
, skb
->len
);
453 ld
->ops
->receive_buf(tty
, skb
->data
, NULL
, skb
->len
);
455 printk(KERN_ERR
"capi: send DATA_B3_RESP failed=%x\n",
459 if (errcode
== CAPI_SENDQUEUEFULL
)
475 static void handle_minor_recv(struct capiminor
*mp
)
479 while ((skb
= skb_dequeue(&mp
->inqueue
)) != NULL
)
480 if (handle_recv_skb(mp
, skb
) < 0) {
481 skb_queue_head(&mp
->inqueue
, skb
);
486 static void handle_minor_send(struct capiminor
*mp
)
488 struct tty_struct
*tty
;
494 tty
= tty_port_tty_get(&mp
->port
);
498 if (mp
->ttyoutstop
) {
499 pr_debug("capi: send: tty stopped\n");
505 spin_lock_bh(&mp
->outlock
);
506 skb
= __skb_dequeue(&mp
->outqueue
);
508 spin_unlock_bh(&mp
->outlock
);
513 spin_unlock_bh(&mp
->outlock
);
515 datahandle
= atomic_inc_return(&mp
->datahandle
);
516 skb_push(skb
, CAPI_DATA_B3_REQ_LEN
);
517 memset(skb
->data
, 0, CAPI_DATA_B3_REQ_LEN
);
518 capimsg_setu16(skb
->data
, 0, CAPI_DATA_B3_REQ_LEN
);
519 capimsg_setu16(skb
->data
, 2, mp
->ap
->applid
);
520 capimsg_setu8 (skb
->data
, 4, CAPI_DATA_B3
);
521 capimsg_setu8 (skb
->data
, 5, CAPI_REQ
);
522 capimsg_setu16(skb
->data
, 6, atomic_inc_return(&mp
->msgid
));
523 capimsg_setu32(skb
->data
, 8, mp
->ncci
); /* NCCI */
524 capimsg_setu32(skb
->data
, 12, (u32
)(long)skb
->data
);/* Data32 */
525 capimsg_setu16(skb
->data
, 16, len
); /* Data length */
526 capimsg_setu16(skb
->data
, 18, datahandle
);
527 capimsg_setu16(skb
->data
, 20, 0); /* Flags */
529 if (capiminor_add_ack(mp
, datahandle
) < 0) {
530 skb_pull(skb
, CAPI_DATA_B3_REQ_LEN
);
532 spin_lock_bh(&mp
->outlock
);
533 __skb_queue_head(&mp
->outqueue
, skb
);
535 spin_unlock_bh(&mp
->outlock
);
539 errcode
= capi20_put_message(mp
->ap
, skb
);
540 if (errcode
== CAPI_NOERROR
) {
541 pr_debug("capi: DATA_B3_REQ %u len=%u\n",
545 capiminor_del_ack(mp
, datahandle
);
547 if (errcode
== CAPI_SENDQUEUEFULL
) {
548 skb_pull(skb
, CAPI_DATA_B3_REQ_LEN
);
550 spin_lock_bh(&mp
->outlock
);
551 __skb_queue_head(&mp
->outqueue
, skb
);
553 spin_unlock_bh(&mp
->outlock
);
558 /* ups, drop packet */
559 printk(KERN_ERR
"capi: put_message = %x\n", errcode
);
565 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
566 /* -------- function called by lower level -------------------------- */
568 static void capi_recv_message(struct capi20_appl
*ap
, struct sk_buff
*skb
)
570 struct capidev
*cdev
= ap
->private;
571 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
572 struct tty_struct
*tty
;
573 struct capiminor
*mp
;
576 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
578 mutex_lock(&cdev
->lock
);
580 if (CAPIMSG_CMD(skb
->data
) == CAPI_CONNECT_B3_CONF
) {
581 u16 info
= CAPIMSG_U16(skb
->data
, 12); // Info field
582 if ((info
& 0xff00) == 0)
583 capincci_alloc(cdev
, CAPIMSG_NCCI(skb
->data
));
585 if (CAPIMSG_CMD(skb
->data
) == CAPI_CONNECT_B3_IND
)
586 capincci_alloc(cdev
, CAPIMSG_NCCI(skb
->data
));
588 if (CAPIMSG_COMMAND(skb
->data
) != CAPI_DATA_B3
) {
589 skb_queue_tail(&cdev
->recvqueue
, skb
);
590 wake_up_interruptible(&cdev
->recvwait
);
594 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
595 skb_queue_tail(&cdev
->recvqueue
, skb
);
596 wake_up_interruptible(&cdev
->recvwait
);
598 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
600 np
= capincci_find(cdev
, CAPIMSG_CONTROL(skb
->data
));
602 printk(KERN_ERR
"BUG: capi_signal: ncci not found\n");
603 skb_queue_tail(&cdev
->recvqueue
, skb
);
604 wake_up_interruptible(&cdev
->recvwait
);
610 skb_queue_tail(&cdev
->recvqueue
, skb
);
611 wake_up_interruptible(&cdev
->recvwait
);
614 if (CAPIMSG_SUBCOMMAND(skb
->data
) == CAPI_IND
) {
615 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4 + 4 + 2);
616 pr_debug("capi_signal: DATA_B3_IND %u len=%d\n",
617 datahandle
, skb
->len
-CAPIMSG_LEN(skb
->data
));
618 skb_queue_tail(&mp
->inqueue
, skb
);
620 handle_minor_recv(mp
);
622 } else if (CAPIMSG_SUBCOMMAND(skb
->data
) == CAPI_CONF
) {
624 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4);
625 pr_debug("capi_signal: DATA_B3_CONF %u 0x%x\n",
627 CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+ 4 + 2));
629 capiminor_del_ack(mp
, datahandle
);
630 tty
= tty_port_tty_get(&mp
->port
);
635 handle_minor_send(mp
);
638 /* ups, let capi application handle it :-) */
639 skb_queue_tail(&cdev
->recvqueue
, skb
);
640 wake_up_interruptible(&cdev
->recvwait
);
642 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
645 mutex_unlock(&cdev
->lock
);
648 /* -------- file_operations for capidev ----------------------------- */
651 capi_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
653 struct capidev
*cdev
= file
->private_data
;
658 if (!cdev
->ap
.applid
)
661 skb
= skb_dequeue(&cdev
->recvqueue
);
663 if (file
->f_flags
& O_NONBLOCK
)
665 err
= wait_event_interruptible(cdev
->recvwait
,
666 (skb
= skb_dequeue(&cdev
->recvqueue
)));
670 if (skb
->len
> count
) {
671 skb_queue_head(&cdev
->recvqueue
, skb
);
674 if (copy_to_user(buf
, skb
->data
, skb
->len
)) {
675 skb_queue_head(&cdev
->recvqueue
, skb
);
686 capi_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
688 struct capidev
*cdev
= file
->private_data
;
692 if (!cdev
->ap
.applid
)
695 skb
= alloc_skb(count
, GFP_USER
);
699 if (copy_from_user(skb_put(skb
, count
), buf
, count
)) {
703 mlen
= CAPIMSG_LEN(skb
->data
);
704 if (CAPIMSG_CMD(skb
->data
) == CAPI_DATA_B3_REQ
) {
705 if ((size_t)(mlen
+ CAPIMSG_DATALEN(skb
->data
)) != count
) {
715 CAPIMSG_SETAPPID(skb
->data
, cdev
->ap
.applid
);
717 if (CAPIMSG_CMD(skb
->data
) == CAPI_DISCONNECT_B3_RESP
) {
718 mutex_lock(&cdev
->lock
);
719 capincci_free(cdev
, CAPIMSG_NCCI(skb
->data
));
720 mutex_unlock(&cdev
->lock
);
723 cdev
->errcode
= capi20_put_message(&cdev
->ap
, skb
);
733 capi_poll(struct file
*file
, poll_table
*wait
)
735 struct capidev
*cdev
= file
->private_data
;
736 unsigned int mask
= 0;
738 if (!cdev
->ap
.applid
)
741 poll_wait(file
, &(cdev
->recvwait
), wait
);
742 mask
= POLLOUT
| POLLWRNORM
;
743 if (!skb_queue_empty(&cdev
->recvqueue
))
744 mask
|= POLLIN
| POLLRDNORM
;
749 capi_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
751 struct capidev
*cdev
= file
->private_data
;
752 capi_ioctl_struct data
;
753 int retval
= -EINVAL
;
754 void __user
*argp
= (void __user
*)arg
;
758 mutex_lock(&cdev
->lock
);
760 if (cdev
->ap
.applid
) {
764 if (copy_from_user(&cdev
->ap
.rparam
, argp
,
765 sizeof(struct capi_register_params
))) {
769 cdev
->ap
.private = cdev
;
770 cdev
->ap
.recv_message
= capi_recv_message
;
771 cdev
->errcode
= capi20_register(&cdev
->ap
);
772 retval
= (int)cdev
->ap
.applid
;
779 mutex_unlock(&cdev
->lock
);
782 case CAPI_GET_VERSION
:
783 if (copy_from_user(&data
.contr
, argp
,
786 cdev
->errcode
= capi20_get_version(data
.contr
, &data
.version
);
789 if (copy_to_user(argp
, &data
.version
,
790 sizeof(data
.version
)))
794 case CAPI_GET_SERIAL
:
795 if (copy_from_user(&data
.contr
, argp
,
798 cdev
->errcode
= capi20_get_serial(data
.contr
, data
.serial
);
801 if (copy_to_user(argp
, data
.serial
,
802 sizeof(data
.serial
)))
806 case CAPI_GET_PROFILE
:
807 if (copy_from_user(&data
.contr
, argp
,
811 if (data
.contr
== 0) {
812 cdev
->errcode
= capi20_get_profile(data
.contr
, &data
.profile
);
816 retval
= copy_to_user(argp
,
817 &data
.profile
.ncontroller
,
818 sizeof(data
.profile
.ncontroller
));
821 cdev
->errcode
= capi20_get_profile(data
.contr
, &data
.profile
);
825 retval
= copy_to_user(argp
, &data
.profile
,
826 sizeof(data
.profile
));
832 case CAPI_GET_MANUFACTURER
:
833 if (copy_from_user(&data
.contr
, argp
,
836 cdev
->errcode
= capi20_get_manufacturer(data
.contr
, data
.manufacturer
);
840 if (copy_to_user(argp
, data
.manufacturer
,
841 sizeof(data
.manufacturer
)))
846 case CAPI_GET_ERRCODE
:
847 data
.errcode
= cdev
->errcode
;
848 cdev
->errcode
= CAPI_NOERROR
;
850 if (copy_to_user(argp
, &data
.errcode
,
851 sizeof(data
.errcode
)))
857 if (capi20_isinstalled() == CAPI_NOERROR
)
861 case CAPI_MANUFACTURER_CMD
: {
862 struct capi_manufacturer_cmd mcmd
;
863 if (!capable(CAP_SYS_ADMIN
))
865 if (copy_from_user(&mcmd
, argp
, sizeof(mcmd
)))
867 return capi20_manufacturer(mcmd
.cmd
, mcmd
.data
);
870 case CAPI_CLR_FLAGS
: {
873 if (copy_from_user(&userflags
, argp
, sizeof(userflags
)))
876 mutex_lock(&cdev
->lock
);
877 if (cmd
== CAPI_SET_FLAGS
)
878 cdev
->userflags
|= userflags
;
880 cdev
->userflags
&= ~userflags
;
881 mutex_unlock(&cdev
->lock
);
885 if (copy_to_user(argp
, &cdev
->userflags
,
886 sizeof(cdev
->userflags
)))
890 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
891 case CAPI_NCCI_OPENCOUNT
:
894 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
895 case CAPI_NCCI_OPENCOUNT
: {
896 struct capincci
*nccip
;
900 if (copy_from_user(&ncci
, argp
, sizeof(ncci
)))
903 mutex_lock(&cdev
->lock
);
904 nccip
= capincci_find(cdev
, (u32
)ncci
);
906 count
= capincci_minor_opencount(nccip
);
907 mutex_unlock(&cdev
->lock
);
911 case CAPI_NCCI_GETUNIT
: {
912 struct capincci
*nccip
;
913 struct capiminor
*mp
;
917 if (copy_from_user(&ncci
, argp
, sizeof(ncci
)))
920 mutex_lock(&cdev
->lock
);
921 nccip
= capincci_find(cdev
, (u32
)ncci
);
927 mutex_unlock(&cdev
->lock
);
930 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
938 capi_unlocked_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
942 mutex_lock(&capi_mutex
);
943 ret
= capi_ioctl(file
, cmd
, arg
);
944 mutex_unlock(&capi_mutex
);
949 static int capi_open(struct inode
*inode
, struct file
*file
)
951 struct capidev
*cdev
;
953 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
957 mutex_init(&cdev
->lock
);
958 skb_queue_head_init(&cdev
->recvqueue
);
959 init_waitqueue_head(&cdev
->recvwait
);
960 INIT_LIST_HEAD(&cdev
->nccis
);
961 file
->private_data
= cdev
;
963 mutex_lock(&capidev_list_lock
);
964 list_add_tail(&cdev
->list
, &capidev_list
);
965 mutex_unlock(&capidev_list_lock
);
967 return nonseekable_open(inode
, file
);
970 static int capi_release(struct inode
*inode
, struct file
*file
)
972 struct capidev
*cdev
= file
->private_data
;
974 mutex_lock(&capidev_list_lock
);
975 list_del(&cdev
->list
);
976 mutex_unlock(&capidev_list_lock
);
979 capi20_release(&cdev
->ap
);
980 skb_queue_purge(&cdev
->recvqueue
);
981 capincci_free(cdev
, 0xffffffff);
987 static const struct file_operations capi_fops
=
989 .owner
= THIS_MODULE
,
994 .unlocked_ioctl
= capi_unlocked_ioctl
,
996 .release
= capi_release
,
999 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1000 /* -------- tty_operations for capincci ----------------------------- */
1003 capinc_tty_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
1005 struct capiminor
*mp
= capiminor_get(tty
->index
);
1006 int ret
= tty_standard_install(driver
, tty
);
1009 tty
->driver_data
= mp
;
1015 static void capinc_tty_cleanup(struct tty_struct
*tty
)
1017 struct capiminor
*mp
= tty
->driver_data
;
1018 tty
->driver_data
= NULL
;
1022 static int capinc_tty_open(struct tty_struct
*tty
, struct file
*filp
)
1024 struct capiminor
*mp
= tty
->driver_data
;
1027 err
= tty_port_open(&mp
->port
, tty
, filp
);
1031 handle_minor_recv(mp
);
1035 static void capinc_tty_close(struct tty_struct
*tty
, struct file
*filp
)
1037 struct capiminor
*mp
= tty
->driver_data
;
1039 tty_port_close(&mp
->port
, tty
, filp
);
1042 static int capinc_tty_write(struct tty_struct
*tty
,
1043 const unsigned char *buf
, int count
)
1045 struct capiminor
*mp
= tty
->driver_data
;
1046 struct sk_buff
*skb
;
1048 pr_debug("capinc_tty_write(count=%d)\n", count
);
1050 spin_lock_bh(&mp
->outlock
);
1054 __skb_queue_tail(&mp
->outqueue
, skb
);
1055 mp
->outbytes
+= skb
->len
;
1058 skb
= alloc_skb(CAPI_DATA_B3_REQ_LEN
+ count
, GFP_ATOMIC
);
1060 printk(KERN_ERR
"capinc_tty_write: alloc_skb failed\n");
1061 spin_unlock_bh(&mp
->outlock
);
1065 skb_reserve(skb
, CAPI_DATA_B3_REQ_LEN
);
1066 memcpy(skb_put(skb
, count
), buf
, count
);
1068 __skb_queue_tail(&mp
->outqueue
, skb
);
1069 mp
->outbytes
+= skb
->len
;
1070 spin_unlock_bh(&mp
->outlock
);
1072 handle_minor_send(mp
);
1077 static int capinc_tty_put_char(struct tty_struct
*tty
, unsigned char ch
)
1079 struct capiminor
*mp
= tty
->driver_data
;
1080 bool invoke_send
= false;
1081 struct sk_buff
*skb
;
1084 pr_debug("capinc_put_char(%u)\n", ch
);
1086 spin_lock_bh(&mp
->outlock
);
1089 if (skb_tailroom(skb
) > 0) {
1090 *(skb_put(skb
, 1)) = ch
;
1094 __skb_queue_tail(&mp
->outqueue
, skb
);
1095 mp
->outbytes
+= skb
->len
;
1099 skb
= alloc_skb(CAPI_DATA_B3_REQ_LEN
+ CAPI_MAX_BLKSIZE
, GFP_ATOMIC
);
1101 skb_reserve(skb
, CAPI_DATA_B3_REQ_LEN
);
1102 *(skb_put(skb
, 1)) = ch
;
1105 printk(KERN_ERR
"capinc_put_char: char %u lost\n", ch
);
1110 spin_unlock_bh(&mp
->outlock
);
1113 handle_minor_send(mp
);
1118 static void capinc_tty_flush_chars(struct tty_struct
*tty
)
1120 struct capiminor
*mp
= tty
->driver_data
;
1121 struct sk_buff
*skb
;
1123 pr_debug("capinc_tty_flush_chars\n");
1125 spin_lock_bh(&mp
->outlock
);
1129 __skb_queue_tail(&mp
->outqueue
, skb
);
1130 mp
->outbytes
+= skb
->len
;
1131 spin_unlock_bh(&mp
->outlock
);
1133 handle_minor_send(mp
);
1135 spin_unlock_bh(&mp
->outlock
);
1137 handle_minor_recv(mp
);
1140 static int capinc_tty_write_room(struct tty_struct
*tty
)
1142 struct capiminor
*mp
= tty
->driver_data
;
1145 room
= CAPINC_MAX_SENDQUEUE
-skb_queue_len(&mp
->outqueue
);
1146 room
*= CAPI_MAX_BLKSIZE
;
1147 pr_debug("capinc_tty_write_room = %d\n", room
);
1151 static int capinc_tty_chars_in_buffer(struct tty_struct
*tty
)
1153 struct capiminor
*mp
= tty
->driver_data
;
1155 pr_debug("capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1156 mp
->outbytes
, mp
->nack
,
1157 skb_queue_len(&mp
->outqueue
),
1158 skb_queue_len(&mp
->inqueue
));
1159 return mp
->outbytes
;
1162 static int capinc_tty_ioctl(struct tty_struct
*tty
,
1163 unsigned int cmd
, unsigned long arg
)
1165 return -ENOIOCTLCMD
;
1168 static void capinc_tty_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
1170 pr_debug("capinc_tty_set_termios\n");
1173 static void capinc_tty_throttle(struct tty_struct
*tty
)
1175 struct capiminor
*mp
= tty
->driver_data
;
1176 pr_debug("capinc_tty_throttle\n");
1180 static void capinc_tty_unthrottle(struct tty_struct
*tty
)
1182 struct capiminor
*mp
= tty
->driver_data
;
1184 pr_debug("capinc_tty_unthrottle\n");
1186 handle_minor_recv(mp
);
1189 static void capinc_tty_stop(struct tty_struct
*tty
)
1191 struct capiminor
*mp
= tty
->driver_data
;
1193 pr_debug("capinc_tty_stop\n");
1197 static void capinc_tty_start(struct tty_struct
*tty
)
1199 struct capiminor
*mp
= tty
->driver_data
;
1201 pr_debug("capinc_tty_start\n");
1203 handle_minor_send(mp
);
1206 static void capinc_tty_hangup(struct tty_struct
*tty
)
1208 struct capiminor
*mp
= tty
->driver_data
;
1210 pr_debug("capinc_tty_hangup\n");
1211 tty_port_hangup(&mp
->port
);
1214 static int capinc_tty_break_ctl(struct tty_struct
*tty
, int state
)
1216 pr_debug("capinc_tty_break_ctl(%d)\n", state
);
1220 static void capinc_tty_flush_buffer(struct tty_struct
*tty
)
1222 pr_debug("capinc_tty_flush_buffer\n");
1225 static void capinc_tty_set_ldisc(struct tty_struct
*tty
)
1227 pr_debug("capinc_tty_set_ldisc\n");
1230 static void capinc_tty_send_xchar(struct tty_struct
*tty
, char ch
)
1232 pr_debug("capinc_tty_send_xchar(%d)\n", ch
);
1235 static const struct tty_operations capinc_ops
= {
1236 .open
= capinc_tty_open
,
1237 .close
= capinc_tty_close
,
1238 .write
= capinc_tty_write
,
1239 .put_char
= capinc_tty_put_char
,
1240 .flush_chars
= capinc_tty_flush_chars
,
1241 .write_room
= capinc_tty_write_room
,
1242 .chars_in_buffer
= capinc_tty_chars_in_buffer
,
1243 .ioctl
= capinc_tty_ioctl
,
1244 .set_termios
= capinc_tty_set_termios
,
1245 .throttle
= capinc_tty_throttle
,
1246 .unthrottle
= capinc_tty_unthrottle
,
1247 .stop
= capinc_tty_stop
,
1248 .start
= capinc_tty_start
,
1249 .hangup
= capinc_tty_hangup
,
1250 .break_ctl
= capinc_tty_break_ctl
,
1251 .flush_buffer
= capinc_tty_flush_buffer
,
1252 .set_ldisc
= capinc_tty_set_ldisc
,
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
= kzalloc(sizeof(struct capi_minor
*) * capi_ttyminors
,
1273 drv
= alloc_tty_driver(capi_ttyminors
);
1278 drv
->driver_name
= "capi_nc";
1281 drv
->minor_start
= 0;
1282 drv
->type
= TTY_DRIVER_TYPE_SERIAL
;
1283 drv
->subtype
= SERIAL_TYPE_NORMAL
;
1284 drv
->init_termios
= tty_std_termios
;
1285 drv
->init_termios
.c_iflag
= ICRNL
;
1286 drv
->init_termios
.c_oflag
= OPOST
| ONLCR
;
1287 drv
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1288 drv
->init_termios
.c_lflag
= 0;
1290 TTY_DRIVER_REAL_RAW
| TTY_DRIVER_RESET_TERMIOS
|
1291 TTY_DRIVER_DYNAMIC_DEV
;
1292 tty_set_operations(drv
, &capinc_ops
);
1294 err
= tty_register_driver(drv
);
1296 put_tty_driver(drv
);
1298 printk(KERN_ERR
"Couldn't register capi_nc driver\n");
1301 capinc_tty_driver
= drv
;
1305 static void __exit
capinc_tty_exit(void)
1307 tty_unregister_driver(capinc_tty_driver
);
1308 put_tty_driver(capinc_tty_driver
);
1312 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1314 static inline int capinc_tty_init(void)
1319 static inline void capinc_tty_exit(void) { }
1321 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1323 /* -------- /proc functions ----------------------------------------- */
1326 * /proc/capi/capi20:
1327 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1329 static int capi20_proc_show(struct seq_file
*m
, void *v
)
1331 struct capidev
*cdev
;
1332 struct list_head
*l
;
1334 mutex_lock(&capidev_list_lock
);
1335 list_for_each(l
, &capidev_list
) {
1336 cdev
= list_entry(l
, struct capidev
, list
);
1337 seq_printf(m
, "0 %d %lu %lu %lu %lu\n",
1339 cdev
->ap
.nrecvctlpkt
,
1340 cdev
->ap
.nrecvdatapkt
,
1341 cdev
->ap
.nsentctlpkt
,
1342 cdev
->ap
.nsentdatapkt
);
1344 mutex_unlock(&capidev_list_lock
);
1348 static int capi20_proc_open(struct inode
*inode
, struct file
*file
)
1350 return single_open(file
, capi20_proc_show
, NULL
);
1353 static const struct file_operations capi20_proc_fops
= {
1354 .owner
= THIS_MODULE
,
1355 .open
= capi20_proc_open
,
1357 .llseek
= seq_lseek
,
1358 .release
= single_release
,
1362 * /proc/capi/capi20ncci:
1365 static int capi20ncci_proc_show(struct seq_file
*m
, void *v
)
1367 struct capidev
*cdev
;
1368 struct capincci
*np
;
1370 mutex_lock(&capidev_list_lock
);
1371 list_for_each_entry(cdev
, &capidev_list
, list
) {
1372 mutex_lock(&cdev
->lock
);
1373 list_for_each_entry(np
, &cdev
->nccis
, list
)
1374 seq_printf(m
, "%d 0x%x\n", cdev
->ap
.applid
, np
->ncci
);
1375 mutex_unlock(&cdev
->lock
);
1377 mutex_unlock(&capidev_list_lock
);
1381 static int capi20ncci_proc_open(struct inode
*inode
, struct file
*file
)
1383 return single_open(file
, capi20ncci_proc_show
, NULL
);
1386 static const struct file_operations capi20ncci_proc_fops
= {
1387 .owner
= THIS_MODULE
,
1388 .open
= capi20ncci_proc_open
,
1390 .llseek
= seq_lseek
,
1391 .release
= single_release
,
1394 static void __init
proc_init(void)
1396 proc_create("capi/capi20", 0, NULL
, &capi20_proc_fops
);
1397 proc_create("capi/capi20ncci", 0, NULL
, &capi20ncci_proc_fops
);
1400 static void __exit
proc_exit(void)
1402 remove_proc_entry("capi/capi20", NULL
);
1403 remove_proc_entry("capi/capi20ncci", NULL
);
1406 /* -------- init function and module interface ---------------------- */
1409 static int __init
capi_init(void)
1411 const char *compileinfo
;
1414 major_ret
= register_chrdev(capi_major
, "capi20", &capi_fops
);
1415 if (major_ret
< 0) {
1416 printk(KERN_ERR
"capi20: unable to get major %d\n", capi_major
);
1419 capi_class
= class_create(THIS_MODULE
, "capi");
1420 if (IS_ERR(capi_class
)) {
1421 unregister_chrdev(capi_major
, "capi20");
1422 return PTR_ERR(capi_class
);
1425 device_create(capi_class
, NULL
, MKDEV(capi_major
, 0), NULL
, "capi");
1427 if (capinc_tty_init() < 0) {
1428 device_destroy(capi_class
, MKDEV(capi_major
, 0));
1429 class_destroy(capi_class
);
1430 unregister_chrdev(capi_major
, "capi20");
1436 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1437 compileinfo
= " (middleware)";
1439 compileinfo
= " (no middleware)";
1441 printk(KERN_NOTICE
"CAPI 2.0 started up with major %d%s\n",
1442 capi_major
, compileinfo
);
1447 static void __exit
capi_exit(void)
1451 device_destroy(capi_class
, MKDEV(capi_major
, 0));
1452 class_destroy(capi_class
);
1453 unregister_chrdev(capi_major
, "capi20");
1458 module_init(capi_init
);
1459 module_exit(capi_exit
);