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/smp_lock.h>
24 #include <linux/timer.h>
25 #include <linux/wait.h>
26 #include <linux/tty.h>
27 #include <linux/netdevice.h>
28 #include <linux/ppp_defs.h>
29 #include <linux/if_ppp.h>
30 #include <linux/skbuff.h>
31 #include <linux/proc_fs.h>
32 #include <linux/seq_file.h>
33 #include <linux/poll.h>
34 #include <linux/capi.h>
35 #include <linux/kernelcapi.h>
36 #include <linux/init.h>
37 #include <linux/device.h>
38 #include <linux/moduleparam.h>
39 #include <linux/isdn/capiutil.h>
40 #include <linux/isdn/capicmd.h>
44 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
45 MODULE_AUTHOR("Carsten Paeth");
46 MODULE_LICENSE("GPL");
48 #undef _DEBUG_REFCOUNT /* alloc/free and open/close debug */
49 #undef _DEBUG_TTYFUNCS /* call to tty_driver */
50 #undef _DEBUG_DATAFLOW /* data flow */
52 /* -------- driver information -------------------------------------- */
54 static 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 datahandle_queue
{
81 struct list_head list
;
86 struct capincci
*nccip
;
88 struct dentry
*capifs_dentry
;
90 struct capi20_appl
*ap
;
95 struct tty_struct
*tty
;
98 struct sk_buff
*ttyskb
;
99 atomic_t ttyopencount
;
101 struct sk_buff_head inqueue
;
103 struct sk_buff_head outqueue
;
107 struct list_head ackqueue
;
112 /* FIXME: The following lock is a sledgehammer-workaround to a
113 * locking issue with the capiminor (and maybe other) data structure(s).
114 * Access to this data is done in a racy way and crashes the machine with
115 * a FritzCard DSL driver; sooner or later. This is a workaround
116 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
117 * The correct (and scalable) fix for the issue seems to require
118 * an API change to the drivers... . */
119 static DEFINE_SPINLOCK(workaround_lock
);
122 struct list_head list
;
124 struct capidev
*cdev
;
125 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
126 struct capiminor
*minorp
;
127 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
131 struct list_head list
;
132 struct capi20_appl ap
;
136 struct sk_buff_head recvqueue
;
137 wait_queue_head_t recvwait
;
139 struct list_head nccis
;
144 /* -------- global variables ---------------------------------------- */
146 static DEFINE_MUTEX(capidev_list_lock
);
147 static LIST_HEAD(capidev_list
);
149 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
151 static DEFINE_RWLOCK(capiminors_lock
);
152 static struct capiminor
**capiminors
;
154 static struct tty_driver
*capinc_tty_driver
;
156 /* -------- datahandles --------------------------------------------- */
158 static int capiminor_add_ack(struct capiminor
*mp
, u16 datahandle
)
160 struct datahandle_queue
*n
;
163 n
= kmalloc(sizeof(*n
), GFP_ATOMIC
);
165 printk(KERN_ERR
"capi: alloc datahandle failed\n");
168 n
->datahandle
= datahandle
;
169 INIT_LIST_HEAD(&n
->list
);
170 spin_lock_irqsave(&mp
->ackqlock
, flags
);
171 list_add_tail(&n
->list
, &mp
->ackqueue
);
173 spin_unlock_irqrestore(&mp
->ackqlock
, flags
);
177 static int capiminor_del_ack(struct capiminor
*mp
, u16 datahandle
)
179 struct datahandle_queue
*p
, *tmp
;
182 spin_lock_irqsave(&mp
->ackqlock
, flags
);
183 list_for_each_entry_safe(p
, tmp
, &mp
->ackqueue
, list
) {
184 if (p
->datahandle
== datahandle
) {
188 spin_unlock_irqrestore(&mp
->ackqlock
, flags
);
192 spin_unlock_irqrestore(&mp
->ackqlock
, flags
);
196 static void capiminor_del_all_ack(struct capiminor
*mp
)
198 struct datahandle_queue
*p
, *tmp
;
201 spin_lock_irqsave(&mp
->ackqlock
, flags
);
202 list_for_each_entry_safe(p
, tmp
, &mp
->ackqueue
, list
) {
207 spin_unlock_irqrestore(&mp
->ackqlock
, flags
);
211 /* -------- struct capiminor ---------------------------------------- */
213 static struct capiminor
*capiminor_alloc(struct capi20_appl
*ap
, u32 ncci
)
215 struct capiminor
*mp
;
220 mp
= kzalloc(sizeof(*mp
), GFP_KERNEL
);
222 printk(KERN_ERR
"capi: can't alloc capiminor\n");
229 atomic_set(&mp
->ttyopencount
,0);
230 INIT_LIST_HEAD(&mp
->ackqueue
);
231 spin_lock_init(&mp
->ackqlock
);
233 skb_queue_head_init(&mp
->inqueue
);
234 skb_queue_head_init(&mp
->outqueue
);
236 /* Allocate the least unused minor number. */
237 write_lock_irqsave(&capiminors_lock
, flags
);
238 for (minor
= 0; minor
< capi_ttyminors
; minor
++)
239 if (!capiminors
[minor
]) {
240 capiminors
[minor
] = mp
;
243 write_unlock_irqrestore(&capiminors_lock
, flags
);
245 if (minor
== capi_ttyminors
) {
246 printk(KERN_NOTICE
"capi: out of minors\n");
252 dev
= tty_register_device(capinc_tty_driver
, minor
, NULL
);
259 write_lock_irqsave(&capiminors_lock
, flags
);
260 capiminors
[minor
] = NULL
;
261 write_unlock_irqrestore(&capiminors_lock
, flags
);
268 static void capiminor_free(struct capiminor
*mp
)
272 tty_unregister_device(capinc_tty_driver
, mp
->minor
);
274 write_lock_irqsave(&capiminors_lock
, flags
);
275 capiminors
[mp
->minor
] = NULL
;
276 write_unlock_irqrestore(&capiminors_lock
, flags
);
278 kfree_skb(mp
->ttyskb
);
280 skb_queue_purge(&mp
->inqueue
);
281 skb_queue_purge(&mp
->outqueue
);
282 capiminor_del_all_ack(mp
);
286 static struct capiminor
*capiminor_get(unsigned int minor
)
288 struct capiminor
*mp
;
290 read_lock(&capiminors_lock
);
291 mp
= capiminors
[minor
];
292 read_unlock(&capiminors_lock
);
297 /* -------- struct capincci ----------------------------------------- */
299 static void capincci_alloc_minor(struct capidev
*cdev
, struct capincci
*np
)
301 struct capiminor
*mp
;
304 if (!(cdev
->userflags
& CAPIFLAG_HIGHJACKING
))
307 mp
= np
->minorp
= capiminor_alloc(&cdev
->ap
, np
->ncci
);
310 #ifdef _DEBUG_REFCOUNT
311 printk(KERN_DEBUG
"set mp->nccip\n");
313 device
= MKDEV(capinc_tty_driver
->major
, mp
->minor
);
314 mp
->capifs_dentry
= capifs_new_ncci(mp
->minor
, device
);
318 static void capincci_free_minor(struct capincci
*np
)
320 struct capiminor
*mp
= np
->minorp
;
323 capifs_free_ncci(mp
->capifs_dentry
);
326 #ifdef _DEBUG_REFCOUNT
327 printk(KERN_DEBUG
"reset mp->nccip\n");
336 static inline unsigned int capincci_minor_opencount(struct capincci
*np
)
338 struct capiminor
*mp
= np
->minorp
;
340 return mp
? atomic_read(&mp
->ttyopencount
) : 0;
343 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
346 capincci_alloc_minor(struct capidev
*cdev
, struct capincci
*np
) { }
347 static inline void capincci_free_minor(struct capincci
*np
) { }
349 static inline unsigned int capincci_minor_opencount(struct capincci
*np
)
354 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
356 static struct capincci
*capincci_alloc(struct capidev
*cdev
, u32 ncci
)
360 np
= kzalloc(sizeof(*np
), GFP_KERNEL
);
366 capincci_alloc_minor(cdev
, np
);
368 list_add_tail(&np
->list
, &cdev
->nccis
);
373 static void capincci_free(struct capidev
*cdev
, u32 ncci
)
375 struct capincci
*np
, *tmp
;
377 list_for_each_entry_safe(np
, tmp
, &cdev
->nccis
, list
)
378 if (ncci
== 0xffffffff || np
->ncci
== ncci
) {
379 capincci_free_minor(np
);
385 static struct capincci
*capincci_find(struct capidev
*cdev
, u32 ncci
)
389 list_for_each_entry(np
, &cdev
->nccis
, list
)
390 if (np
->ncci
== ncci
)
395 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
396 /* -------- handle data queue --------------------------------------- */
398 static struct sk_buff
*
399 gen_data_b3_resp_for(struct capiminor
*mp
, struct sk_buff
*skb
)
401 struct sk_buff
*nskb
;
402 nskb
= alloc_skb(CAPI_DATA_B3_RESP_LEN
, GFP_ATOMIC
);
404 u16 datahandle
= CAPIMSG_U16(skb
->data
,CAPIMSG_BASELEN
+4+4+2);
405 unsigned char *s
= skb_put(nskb
, CAPI_DATA_B3_RESP_LEN
);
406 capimsg_setu16(s
, 0, CAPI_DATA_B3_RESP_LEN
);
407 capimsg_setu16(s
, 2, mp
->ap
->applid
);
408 capimsg_setu8 (s
, 4, CAPI_DATA_B3
);
409 capimsg_setu8 (s
, 5, CAPI_RESP
);
410 capimsg_setu16(s
, 6, mp
->msgid
++);
411 capimsg_setu32(s
, 8, mp
->ncci
);
412 capimsg_setu16(s
, 12, datahandle
);
417 static int handle_recv_skb(struct capiminor
*mp
, struct sk_buff
*skb
)
419 struct sk_buff
*nskb
;
421 u16 errcode
, datahandle
;
422 struct tty_ldisc
*ld
;
424 datalen
= skb
->len
- CAPIMSG_LEN(skb
->data
);
427 #ifdef _DEBUG_DATAFLOW
428 printk(KERN_DEBUG
"capi: currently no receiver\n");
433 ld
= tty_ldisc_ref(mp
->tty
);
436 if (ld
->ops
->receive_buf
== NULL
) {
437 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
438 printk(KERN_DEBUG
"capi: ldisc has no receive_buf function\n");
443 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
444 printk(KERN_DEBUG
"capi: recv tty throttled\n");
448 if (mp
->tty
->receive_room
< datalen
) {
449 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
450 printk(KERN_DEBUG
"capi: no room in tty\n");
454 if ((nskb
= gen_data_b3_resp_for(mp
, skb
)) == NULL
) {
455 printk(KERN_ERR
"capi: gen_data_b3_resp failed\n");
458 datahandle
= CAPIMSG_U16(skb
->data
,CAPIMSG_BASELEN
+4);
459 errcode
= capi20_put_message(mp
->ap
, nskb
);
460 if (errcode
!= CAPI_NOERROR
) {
461 printk(KERN_ERR
"capi: send DATA_B3_RESP failed=%x\n",
466 (void)skb_pull(skb
, CAPIMSG_LEN(skb
->data
));
467 #ifdef _DEBUG_DATAFLOW
468 printk(KERN_DEBUG
"capi: DATA_B3_RESP %u len=%d => ldisc\n",
469 datahandle
, skb
->len
);
471 ld
->ops
->receive_buf(mp
->tty
, skb
->data
, NULL
, skb
->len
);
480 static void handle_minor_recv(struct capiminor
*mp
)
483 while ((skb
= skb_dequeue(&mp
->inqueue
)) != NULL
) {
484 unsigned int len
= skb
->len
;
486 if (handle_recv_skb(mp
, skb
) < 0) {
487 skb_queue_head(&mp
->inqueue
, skb
);
494 static int handle_minor_send(struct capiminor
*mp
)
502 if (mp
->tty
&& mp
->ttyoutstop
) {
503 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
504 printk(KERN_DEBUG
"capi: send: tty stopped\n");
509 while ((skb
= skb_dequeue(&mp
->outqueue
)) != NULL
) {
510 datahandle
= mp
->datahandle
;
512 skb_push(skb
, CAPI_DATA_B3_REQ_LEN
);
513 memset(skb
->data
, 0, CAPI_DATA_B3_REQ_LEN
);
514 capimsg_setu16(skb
->data
, 0, CAPI_DATA_B3_REQ_LEN
);
515 capimsg_setu16(skb
->data
, 2, mp
->ap
->applid
);
516 capimsg_setu8 (skb
->data
, 4, CAPI_DATA_B3
);
517 capimsg_setu8 (skb
->data
, 5, CAPI_REQ
);
518 capimsg_setu16(skb
->data
, 6, mp
->msgid
++);
519 capimsg_setu32(skb
->data
, 8, mp
->ncci
); /* NCCI */
520 capimsg_setu32(skb
->data
, 12, (u32
)(long)skb
->data
);/* Data32 */
521 capimsg_setu16(skb
->data
, 16, len
); /* Data length */
522 capimsg_setu16(skb
->data
, 18, datahandle
);
523 capimsg_setu16(skb
->data
, 20, 0); /* Flags */
525 if (capiminor_add_ack(mp
, datahandle
) < 0) {
526 skb_pull(skb
, CAPI_DATA_B3_REQ_LEN
);
527 skb_queue_head(&mp
->outqueue
, skb
);
530 errcode
= capi20_put_message(mp
->ap
, skb
);
531 if (errcode
== CAPI_NOERROR
) {
535 #ifdef _DEBUG_DATAFLOW
536 printk(KERN_DEBUG
"capi: DATA_B3_REQ %u len=%u\n",
541 capiminor_del_ack(mp
, datahandle
);
543 if (errcode
== CAPI_SENDQUEUEFULL
) {
544 skb_pull(skb
, CAPI_DATA_B3_REQ_LEN
);
545 skb_queue_head(&mp
->outqueue
, skb
);
549 /* ups, drop packet */
550 printk(KERN_ERR
"capi: put_message = %x\n", errcode
);
557 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
558 /* -------- function called by lower level -------------------------- */
560 static void capi_recv_message(struct capi20_appl
*ap
, struct sk_buff
*skb
)
562 struct capidev
*cdev
= ap
->private;
563 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
564 struct capiminor
*mp
;
566 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
570 mutex_lock(&cdev
->lock
);
572 if (CAPIMSG_CMD(skb
->data
) == CAPI_CONNECT_B3_CONF
) {
573 u16 info
= CAPIMSG_U16(skb
->data
, 12); // Info field
574 if ((info
& 0xff00) == 0)
575 capincci_alloc(cdev
, CAPIMSG_NCCI(skb
->data
));
577 if (CAPIMSG_CMD(skb
->data
) == CAPI_CONNECT_B3_IND
)
578 capincci_alloc(cdev
, CAPIMSG_NCCI(skb
->data
));
580 spin_lock_irqsave(&workaround_lock
, flags
);
581 if (CAPIMSG_COMMAND(skb
->data
) != CAPI_DATA_B3
) {
582 skb_queue_tail(&cdev
->recvqueue
, skb
);
583 wake_up_interruptible(&cdev
->recvwait
);
587 np
= capincci_find(cdev
, CAPIMSG_CONTROL(skb
->data
));
589 printk(KERN_ERR
"BUG: capi_signal: ncci not found\n");
590 skb_queue_tail(&cdev
->recvqueue
, skb
);
591 wake_up_interruptible(&cdev
->recvwait
);
595 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
596 skb_queue_tail(&cdev
->recvqueue
, skb
);
597 wake_up_interruptible(&cdev
->recvwait
);
599 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
603 skb_queue_tail(&cdev
->recvqueue
, skb
);
604 wake_up_interruptible(&cdev
->recvwait
);
607 if (CAPIMSG_SUBCOMMAND(skb
->data
) == CAPI_IND
) {
608 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+4+4+2);
609 #ifdef _DEBUG_DATAFLOW
610 printk(KERN_DEBUG
"capi_signal: DATA_B3_IND %u len=%d\n",
611 datahandle
, skb
->len
-CAPIMSG_LEN(skb
->data
));
613 skb_queue_tail(&mp
->inqueue
, skb
);
614 mp
->inbytes
+= skb
->len
;
615 handle_minor_recv(mp
);
617 } else if (CAPIMSG_SUBCOMMAND(skb
->data
) == CAPI_CONF
) {
619 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+4);
620 #ifdef _DEBUG_DATAFLOW
621 printk(KERN_DEBUG
"capi_signal: DATA_B3_CONF %u 0x%x\n",
623 CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+4+2));
626 (void)capiminor_del_ack(mp
, datahandle
);
629 (void)handle_minor_send(mp
);
632 /* ups, let capi application handle it :-) */
633 skb_queue_tail(&cdev
->recvqueue
, skb
);
634 wake_up_interruptible(&cdev
->recvwait
);
636 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
639 spin_unlock_irqrestore(&workaround_lock
, flags
);
640 mutex_unlock(&cdev
->lock
);
643 /* -------- file_operations for capidev ----------------------------- */
646 capi_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
648 struct capidev
*cdev
= (struct capidev
*)file
->private_data
;
653 if (!cdev
->ap
.applid
)
656 skb
= skb_dequeue(&cdev
->recvqueue
);
658 if (file
->f_flags
& O_NONBLOCK
)
660 err
= wait_event_interruptible(cdev
->recvwait
,
661 (skb
= skb_dequeue(&cdev
->recvqueue
)));
665 if (skb
->len
> count
) {
666 skb_queue_head(&cdev
->recvqueue
, skb
);
669 if (copy_to_user(buf
, skb
->data
, skb
->len
)) {
670 skb_queue_head(&cdev
->recvqueue
, skb
);
681 capi_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
683 struct capidev
*cdev
= (struct capidev
*)file
->private_data
;
687 if (!cdev
->ap
.applid
)
690 skb
= alloc_skb(count
, GFP_USER
);
694 if (copy_from_user(skb_put(skb
, count
), buf
, count
)) {
698 mlen
= CAPIMSG_LEN(skb
->data
);
699 if (CAPIMSG_CMD(skb
->data
) == CAPI_DATA_B3_REQ
) {
700 if ((size_t)(mlen
+ CAPIMSG_DATALEN(skb
->data
)) != count
) {
710 CAPIMSG_SETAPPID(skb
->data
, cdev
->ap
.applid
);
712 if (CAPIMSG_CMD(skb
->data
) == CAPI_DISCONNECT_B3_RESP
) {
713 mutex_lock(&cdev
->lock
);
714 capincci_free(cdev
, CAPIMSG_NCCI(skb
->data
));
715 mutex_unlock(&cdev
->lock
);
718 cdev
->errcode
= capi20_put_message(&cdev
->ap
, skb
);
728 capi_poll(struct file
*file
, poll_table
* wait
)
730 struct capidev
*cdev
= (struct capidev
*)file
->private_data
;
731 unsigned int mask
= 0;
733 if (!cdev
->ap
.applid
)
736 poll_wait(file
, &(cdev
->recvwait
), wait
);
737 mask
= POLLOUT
| POLLWRNORM
;
738 if (!skb_queue_empty(&cdev
->recvqueue
))
739 mask
|= POLLIN
| POLLRDNORM
;
744 capi_ioctl(struct inode
*inode
, struct file
*file
,
745 unsigned int cmd
, unsigned long arg
)
747 struct capidev
*cdev
= file
->private_data
;
748 capi_ioctl_struct data
;
749 int retval
= -EINVAL
;
750 void __user
*argp
= (void __user
*)arg
;
754 mutex_lock(&cdev
->lock
);
756 if (cdev
->ap
.applid
) {
760 if (copy_from_user(&cdev
->ap
.rparam
, argp
,
761 sizeof(struct capi_register_params
))) {
765 cdev
->ap
.private = cdev
;
766 cdev
->ap
.recv_message
= capi_recv_message
;
767 cdev
->errcode
= capi20_register(&cdev
->ap
);
768 retval
= (int)cdev
->ap
.applid
;
775 mutex_unlock(&cdev
->lock
);
778 case CAPI_GET_VERSION
:
780 if (copy_from_user(&data
.contr
, argp
,
783 cdev
->errcode
= capi20_get_version(data
.contr
, &data
.version
);
786 if (copy_to_user(argp
, &data
.version
,
787 sizeof(data
.version
)))
792 case CAPI_GET_SERIAL
:
794 if (copy_from_user(&data
.contr
, argp
,
797 cdev
->errcode
= capi20_get_serial (data
.contr
, data
.serial
);
800 if (copy_to_user(argp
, data
.serial
,
801 sizeof(data
.serial
)))
805 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
));
833 case CAPI_GET_MANUFACTURER
:
835 if (copy_from_user(&data
.contr
, argp
,
838 cdev
->errcode
= capi20_get_manufacturer(data
.contr
, data
.manufacturer
);
842 if (copy_to_user(argp
, data
.manufacturer
,
843 sizeof(data
.manufacturer
)))
848 case CAPI_GET_ERRCODE
:
849 data
.errcode
= cdev
->errcode
;
850 cdev
->errcode
= CAPI_NOERROR
;
852 if (copy_to_user(argp
, &data
.errcode
,
853 sizeof(data
.errcode
)))
859 if (capi20_isinstalled() == CAPI_NOERROR
)
863 case CAPI_MANUFACTURER_CMD
:
865 struct capi_manufacturer_cmd mcmd
;
866 if (!capable(CAP_SYS_ADMIN
))
868 if (copy_from_user(&mcmd
, argp
, sizeof(mcmd
)))
870 return capi20_manufacturer(mcmd
.cmd
, mcmd
.data
);
875 case CAPI_CLR_FLAGS
: {
878 if (copy_from_user(&userflags
, argp
, sizeof(userflags
)))
881 mutex_lock(&cdev
->lock
);
882 if (cmd
== CAPI_SET_FLAGS
)
883 cdev
->userflags
|= userflags
;
885 cdev
->userflags
&= ~userflags
;
886 mutex_unlock(&cdev
->lock
);
890 if (copy_to_user(argp
, &cdev
->userflags
,
891 sizeof(cdev
->userflags
)))
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 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
912 case CAPI_NCCI_GETUNIT
: {
913 struct capincci
*nccip
;
914 struct capiminor
*mp
;
918 if (copy_from_user(&ncci
, argp
, sizeof(ncci
)))
921 mutex_lock(&cdev
->lock
);
922 nccip
= capincci_find(cdev
, (u32
)ncci
);
928 mutex_unlock(&cdev
->lock
);
931 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
938 static int capi_open(struct inode
*inode
, struct file
*file
)
940 struct capidev
*cdev
;
942 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
946 mutex_init(&cdev
->lock
);
947 skb_queue_head_init(&cdev
->recvqueue
);
948 init_waitqueue_head(&cdev
->recvwait
);
949 INIT_LIST_HEAD(&cdev
->nccis
);
950 file
->private_data
= cdev
;
952 mutex_lock(&capidev_list_lock
);
953 list_add_tail(&cdev
->list
, &capidev_list
);
954 mutex_unlock(&capidev_list_lock
);
956 return nonseekable_open(inode
, file
);
959 static int capi_release(struct inode
*inode
, struct file
*file
)
961 struct capidev
*cdev
= file
->private_data
;
963 mutex_lock(&capidev_list_lock
);
964 list_del(&cdev
->list
);
965 mutex_unlock(&capidev_list_lock
);
968 capi20_release(&cdev
->ap
);
969 skb_queue_purge(&cdev
->recvqueue
);
970 capincci_free(cdev
, 0xffffffff);
976 static const struct file_operations capi_fops
=
978 .owner
= THIS_MODULE
,
985 .release
= capi_release
,
988 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
989 /* -------- tty_operations for capincci ----------------------------- */
991 static int capinc_tty_open(struct tty_struct
* tty
, struct file
* file
)
993 struct capiminor
*mp
;
996 mp
= capiminor_get(iminor(file
->f_path
.dentry
->d_inode
));
997 if (mp
->nccip
== NULL
)
1000 tty
->driver_data
= (void *)mp
;
1002 spin_lock_irqsave(&workaround_lock
, flags
);
1003 if (atomic_read(&mp
->ttyopencount
) == 0)
1005 atomic_inc(&mp
->ttyopencount
);
1006 #ifdef _DEBUG_REFCOUNT
1007 printk(KERN_DEBUG
"capinc_tty_open ocount=%d\n", atomic_read(&mp
->ttyopencount
));
1009 handle_minor_recv(mp
);
1010 spin_unlock_irqrestore(&workaround_lock
, flags
);
1014 static void capinc_tty_close(struct tty_struct
* tty
, struct file
* file
)
1016 struct capiminor
*mp
;
1018 mp
= (struct capiminor
*)tty
->driver_data
;
1020 if (atomic_dec_and_test(&mp
->ttyopencount
)) {
1021 #ifdef _DEBUG_REFCOUNT
1022 printk(KERN_DEBUG
"capinc_tty_close lastclose\n");
1024 tty
->driver_data
= NULL
;
1027 #ifdef _DEBUG_REFCOUNT
1028 printk(KERN_DEBUG
"capinc_tty_close ocount=%d\n", atomic_read(&mp
->ttyopencount
));
1030 if (mp
->nccip
== NULL
)
1034 #ifdef _DEBUG_REFCOUNT
1035 printk(KERN_DEBUG
"capinc_tty_close\n");
1039 static int capinc_tty_write(struct tty_struct
* tty
,
1040 const unsigned char *buf
, int count
)
1042 struct capiminor
*mp
= (struct capiminor
*)tty
->driver_data
;
1043 struct sk_buff
*skb
;
1044 unsigned long flags
;
1046 #ifdef _DEBUG_TTYFUNCS
1047 printk(KERN_DEBUG
"capinc_tty_write(count=%d)\n", count
);
1050 if (!mp
|| !mp
->nccip
) {
1051 #ifdef _DEBUG_TTYFUNCS
1052 printk(KERN_DEBUG
"capinc_tty_write: mp or mp->ncci NULL\n");
1057 spin_lock_irqsave(&workaround_lock
, flags
);
1061 skb_queue_tail(&mp
->outqueue
, skb
);
1062 mp
->outbytes
+= skb
->len
;
1065 skb
= alloc_skb(CAPI_DATA_B3_REQ_LEN
+count
, GFP_ATOMIC
);
1067 printk(KERN_ERR
"capinc_tty_write: alloc_skb failed\n");
1068 spin_unlock_irqrestore(&workaround_lock
, flags
);
1072 skb_reserve(skb
, CAPI_DATA_B3_REQ_LEN
);
1073 memcpy(skb_put(skb
, count
), buf
, count
);
1075 skb_queue_tail(&mp
->outqueue
, skb
);
1076 mp
->outbytes
+= skb
->len
;
1077 (void)handle_minor_send(mp
);
1078 (void)handle_minor_recv(mp
);
1079 spin_unlock_irqrestore(&workaround_lock
, flags
);
1083 static int capinc_tty_put_char(struct tty_struct
*tty
, unsigned char ch
)
1085 struct capiminor
*mp
= (struct capiminor
*)tty
->driver_data
;
1086 struct sk_buff
*skb
;
1087 unsigned long flags
;
1090 #ifdef _DEBUG_TTYFUNCS
1091 printk(KERN_DEBUG
"capinc_put_char(%u)\n", ch
);
1094 if (!mp
|| !mp
->nccip
) {
1095 #ifdef _DEBUG_TTYFUNCS
1096 printk(KERN_DEBUG
"capinc_tty_put_char: mp or mp->ncci NULL\n");
1101 spin_lock_irqsave(&workaround_lock
, flags
);
1104 if (skb_tailroom(skb
) > 0) {
1105 *(skb_put(skb
, 1)) = ch
;
1106 spin_unlock_irqrestore(&workaround_lock
, flags
);
1110 skb_queue_tail(&mp
->outqueue
, skb
);
1111 mp
->outbytes
+= skb
->len
;
1112 (void)handle_minor_send(mp
);
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
);
1123 spin_unlock_irqrestore(&workaround_lock
, flags
);
1127 static void capinc_tty_flush_chars(struct tty_struct
*tty
)
1129 struct capiminor
*mp
= (struct capiminor
*)tty
->driver_data
;
1130 struct sk_buff
*skb
;
1131 unsigned long flags
;
1133 #ifdef _DEBUG_TTYFUNCS
1134 printk(KERN_DEBUG
"capinc_tty_flush_chars\n");
1137 if (!mp
|| !mp
->nccip
) {
1138 #ifdef _DEBUG_TTYFUNCS
1139 printk(KERN_DEBUG
"capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1144 spin_lock_irqsave(&workaround_lock
, flags
);
1148 skb_queue_tail(&mp
->outqueue
, skb
);
1149 mp
->outbytes
+= skb
->len
;
1150 (void)handle_minor_send(mp
);
1152 (void)handle_minor_recv(mp
);
1153 spin_unlock_irqrestore(&workaround_lock
, flags
);
1156 static int capinc_tty_write_room(struct tty_struct
*tty
)
1158 struct capiminor
*mp
= (struct capiminor
*)tty
->driver_data
;
1160 if (!mp
|| !mp
->nccip
) {
1161 #ifdef _DEBUG_TTYFUNCS
1162 printk(KERN_DEBUG
"capinc_tty_write_room: mp or mp->ncci NULL\n");
1166 room
= CAPINC_MAX_SENDQUEUE
-skb_queue_len(&mp
->outqueue
);
1167 room
*= CAPI_MAX_BLKSIZE
;
1168 #ifdef _DEBUG_TTYFUNCS
1169 printk(KERN_DEBUG
"capinc_tty_write_room = %d\n", room
);
1174 static int capinc_tty_chars_in_buffer(struct tty_struct
*tty
)
1176 struct capiminor
*mp
= (struct capiminor
*)tty
->driver_data
;
1177 if (!mp
|| !mp
->nccip
) {
1178 #ifdef _DEBUG_TTYFUNCS
1179 printk(KERN_DEBUG
"capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1183 #ifdef _DEBUG_TTYFUNCS
1184 printk(KERN_DEBUG
"capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1185 mp
->outbytes
, mp
->nack
,
1186 skb_queue_len(&mp
->outqueue
),
1187 skb_queue_len(&mp
->inqueue
));
1189 return mp
->outbytes
;
1192 static int capinc_tty_ioctl(struct tty_struct
*tty
, struct file
* file
,
1193 unsigned int cmd
, unsigned long arg
)
1198 error
= n_tty_ioctl_helper(tty
, file
, cmd
, arg
);
1204 static void capinc_tty_set_termios(struct tty_struct
*tty
, struct ktermios
* old
)
1206 #ifdef _DEBUG_TTYFUNCS
1207 printk(KERN_DEBUG
"capinc_tty_set_termios\n");
1211 static void capinc_tty_throttle(struct tty_struct
* tty
)
1213 struct capiminor
*mp
= (struct capiminor
*)tty
->driver_data
;
1214 #ifdef _DEBUG_TTYFUNCS
1215 printk(KERN_DEBUG
"capinc_tty_throttle\n");
1221 static void capinc_tty_unthrottle(struct tty_struct
* tty
)
1223 struct capiminor
*mp
= (struct capiminor
*)tty
->driver_data
;
1224 unsigned long flags
;
1225 #ifdef _DEBUG_TTYFUNCS
1226 printk(KERN_DEBUG
"capinc_tty_unthrottle\n");
1229 spin_lock_irqsave(&workaround_lock
, flags
);
1231 handle_minor_recv(mp
);
1232 spin_unlock_irqrestore(&workaround_lock
, flags
);
1236 static void capinc_tty_stop(struct tty_struct
*tty
)
1238 struct capiminor
*mp
= (struct capiminor
*)tty
->driver_data
;
1239 #ifdef _DEBUG_TTYFUNCS
1240 printk(KERN_DEBUG
"capinc_tty_stop\n");
1247 static void capinc_tty_start(struct tty_struct
*tty
)
1249 struct capiminor
*mp
= (struct capiminor
*)tty
->driver_data
;
1250 unsigned long flags
;
1251 #ifdef _DEBUG_TTYFUNCS
1252 printk(KERN_DEBUG
"capinc_tty_start\n");
1255 spin_lock_irqsave(&workaround_lock
, flags
);
1257 (void)handle_minor_send(mp
);
1258 spin_unlock_irqrestore(&workaround_lock
, flags
);
1262 static void capinc_tty_hangup(struct tty_struct
*tty
)
1264 #ifdef _DEBUG_TTYFUNCS
1265 printk(KERN_DEBUG
"capinc_tty_hangup\n");
1269 static int capinc_tty_break_ctl(struct tty_struct
*tty
, int state
)
1271 #ifdef _DEBUG_TTYFUNCS
1272 printk(KERN_DEBUG
"capinc_tty_break_ctl(%d)\n", state
);
1277 static void capinc_tty_flush_buffer(struct tty_struct
*tty
)
1279 #ifdef _DEBUG_TTYFUNCS
1280 printk(KERN_DEBUG
"capinc_tty_flush_buffer\n");
1284 static void capinc_tty_set_ldisc(struct tty_struct
*tty
)
1286 #ifdef _DEBUG_TTYFUNCS
1287 printk(KERN_DEBUG
"capinc_tty_set_ldisc\n");
1291 static void capinc_tty_send_xchar(struct tty_struct
*tty
, char ch
)
1293 #ifdef _DEBUG_TTYFUNCS
1294 printk(KERN_DEBUG
"capinc_tty_send_xchar(%d)\n", ch
);
1298 static const struct tty_operations capinc_ops
= {
1299 .open
= capinc_tty_open
,
1300 .close
= capinc_tty_close
,
1301 .write
= capinc_tty_write
,
1302 .put_char
= capinc_tty_put_char
,
1303 .flush_chars
= capinc_tty_flush_chars
,
1304 .write_room
= capinc_tty_write_room
,
1305 .chars_in_buffer
= capinc_tty_chars_in_buffer
,
1306 .ioctl
= capinc_tty_ioctl
,
1307 .set_termios
= capinc_tty_set_termios
,
1308 .throttle
= capinc_tty_throttle
,
1309 .unthrottle
= capinc_tty_unthrottle
,
1310 .stop
= capinc_tty_stop
,
1311 .start
= capinc_tty_start
,
1312 .hangup
= capinc_tty_hangup
,
1313 .break_ctl
= capinc_tty_break_ctl
,
1314 .flush_buffer
= capinc_tty_flush_buffer
,
1315 .set_ldisc
= capinc_tty_set_ldisc
,
1316 .send_xchar
= capinc_tty_send_xchar
,
1319 static int __init
capinc_tty_init(void)
1321 struct tty_driver
*drv
;
1324 if (capi_ttyminors
> CAPINC_MAX_PORTS
)
1325 capi_ttyminors
= CAPINC_MAX_PORTS
;
1326 if (capi_ttyminors
<= 0)
1327 capi_ttyminors
= CAPINC_NR_PORTS
;
1329 capiminors
= kzalloc(sizeof(struct capi_minor
*) * capi_ttyminors
,
1334 drv
= alloc_tty_driver(capi_ttyminors
);
1339 drv
->owner
= THIS_MODULE
;
1340 drv
->driver_name
= "capi_nc";
1343 drv
->minor_start
= 0;
1344 drv
->type
= TTY_DRIVER_TYPE_SERIAL
;
1345 drv
->subtype
= SERIAL_TYPE_NORMAL
;
1346 drv
->init_termios
= tty_std_termios
;
1347 drv
->init_termios
.c_iflag
= ICRNL
;
1348 drv
->init_termios
.c_oflag
= OPOST
| ONLCR
;
1349 drv
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1350 drv
->init_termios
.c_lflag
= 0;
1352 TTY_DRIVER_REAL_RAW
| TTY_DRIVER_RESET_TERMIOS
|
1353 TTY_DRIVER_DYNAMIC_DEV
;
1354 tty_set_operations(drv
, &capinc_ops
);
1356 err
= tty_register_driver(drv
);
1358 put_tty_driver(drv
);
1360 printk(KERN_ERR
"Couldn't register capi_nc driver\n");
1363 capinc_tty_driver
= drv
;
1367 static void __exit
capinc_tty_exit(void)
1369 tty_unregister_driver(capinc_tty_driver
);
1370 put_tty_driver(capinc_tty_driver
);
1374 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1376 static inline int capinc_tty_init(void)
1381 static inline void capinc_tty_exit(void) { }
1383 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1385 /* -------- /proc functions ----------------------------------------- */
1388 * /proc/capi/capi20:
1389 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1391 static int capi20_proc_show(struct seq_file
*m
, void *v
)
1393 struct capidev
*cdev
;
1394 struct list_head
*l
;
1396 mutex_lock(&capidev_list_lock
);
1397 list_for_each(l
, &capidev_list
) {
1398 cdev
= list_entry(l
, struct capidev
, list
);
1399 seq_printf(m
, "0 %d %lu %lu %lu %lu\n",
1401 cdev
->ap
.nrecvctlpkt
,
1402 cdev
->ap
.nrecvdatapkt
,
1403 cdev
->ap
.nsentctlpkt
,
1404 cdev
->ap
.nsentdatapkt
);
1406 mutex_unlock(&capidev_list_lock
);
1410 static int capi20_proc_open(struct inode
*inode
, struct file
*file
)
1412 return single_open(file
, capi20_proc_show
, NULL
);
1415 static const struct file_operations capi20_proc_fops
= {
1416 .owner
= THIS_MODULE
,
1417 .open
= capi20_proc_open
,
1419 .llseek
= seq_lseek
,
1420 .release
= single_release
,
1424 * /proc/capi/capi20ncci:
1427 static int capi20ncci_proc_show(struct seq_file
*m
, void *v
)
1429 struct capidev
*cdev
;
1430 struct capincci
*np
;
1432 mutex_lock(&capidev_list_lock
);
1433 list_for_each_entry(cdev
, &capidev_list
, list
) {
1434 mutex_lock(&cdev
->lock
);
1435 list_for_each_entry(np
, &cdev
->nccis
, list
)
1436 seq_printf(m
, "%d 0x%x\n", cdev
->ap
.applid
, np
->ncci
);
1437 mutex_unlock(&cdev
->lock
);
1439 mutex_unlock(&capidev_list_lock
);
1443 static int capi20ncci_proc_open(struct inode
*inode
, struct file
*file
)
1445 return single_open(file
, capi20ncci_proc_show
, NULL
);
1448 static const struct file_operations capi20ncci_proc_fops
= {
1449 .owner
= THIS_MODULE
,
1450 .open
= capi20ncci_proc_open
,
1452 .llseek
= seq_lseek
,
1453 .release
= single_release
,
1456 static void __init
proc_init(void)
1458 proc_create("capi/capi20", 0, NULL
, &capi20_proc_fops
);
1459 proc_create("capi/capi20ncci", 0, NULL
, &capi20ncci_proc_fops
);
1462 static void __exit
proc_exit(void)
1464 remove_proc_entry("capi/capi20", NULL
);
1465 remove_proc_entry("capi/capi20ncci", NULL
);
1468 /* -------- init function and module interface ---------------------- */
1471 static int __init
capi_init(void)
1473 const char *compileinfo
;
1476 major_ret
= register_chrdev(capi_major
, "capi20", &capi_fops
);
1477 if (major_ret
< 0) {
1478 printk(KERN_ERR
"capi20: unable to get major %d\n", capi_major
);
1481 capi_class
= class_create(THIS_MODULE
, "capi");
1482 if (IS_ERR(capi_class
)) {
1483 unregister_chrdev(capi_major
, "capi20");
1484 return PTR_ERR(capi_class
);
1487 device_create(capi_class
, NULL
, MKDEV(capi_major
, 0), NULL
, "capi");
1489 if (capinc_tty_init() < 0) {
1490 device_destroy(capi_class
, MKDEV(capi_major
, 0));
1491 class_destroy(capi_class
);
1492 unregister_chrdev(capi_major
, "capi20");
1498 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1499 compileinfo
= " (middleware+capifs)";
1500 #elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
1501 compileinfo
= " (no capifs)";
1503 compileinfo
= " (no middleware)";
1505 printk(KERN_NOTICE
"CAPI 2.0 started up with major %d%s\n",
1506 capi_major
, compileinfo
);
1511 static void __exit
capi_exit(void)
1515 device_destroy(capi_class
, MKDEV(capi_major
, 0));
1516 class_destroy(capi_class
);
1517 unregister_chrdev(capi_major
, "capi20");
1522 module_init(capi_init
);
1523 module_exit(capi_exit
);