2 * $Id: capi.c,v 1.35 2000/06/19 15:11:24 keil Exp $
4 * CAPI 2.0 Interface for Linux
6 * Copyright 1996 by Carsten Paeth (calle@calle.in-berlin.de)
9 * Revision 1.35 2000/06/19 15:11:24 keil
10 * avoid use of freed structs
11 * changes from 2.4.0-ac21
13 * Revision 1.34 2000/06/18 16:09:54 keil
14 * more changes for 2.4
16 * Revision 1.33 2000/05/18 16:35:43 calle
17 * Uaaahh. Bad memory leak fixed.
19 * Revision 1.32 2000/04/21 12:38:42 calle
20 * Bugfix: error in proc_ functions, begin-off => off-begin
22 * Revision 1.31 2000/04/03 13:29:24 calle
23 * make Tim Waugh happy (module unload races in 2.3.99-pre3).
24 * no real problem there, but now it is much cleaner ...
26 * Revision 1.30 2000/03/19 12:31:36 calle
27 * PPP over CAPI raw driver disabled for now, ppp_generic has been changed.
29 * Revision 1.29 2000/03/13 17:48:13 calle
30 * removed unused variable.
32 * Revision 1.28 2000/03/08 17:06:33 calle
33 * - changes for devfs and 2.3.49
34 * - capifs now configurable (no need with devfs)
35 * - New Middleware ioctl CAPI_NCCI_GETUNIT
36 * - Middleware again tested with 2.2.14 and 2.3.49 (with and without devfs)
38 * Revision 1.27 2000/03/06 18:00:23 calle
39 * - Middleware extention now working with 2.3.49 (capifs).
40 * - Fixed typos in debug section of capi.c
41 * - Bugfix: Makefile corrected for b1pcmcia.c
43 * Revision 1.26 2000/03/03 16:48:38 calle
44 * - Added CAPI2.0 Middleware support (CONFIG_ISDN_CAPI)
45 * It is now possible to create a connection with a CAPI2.0 applikation
46 * and than to handle the data connection from /dev/capi/ (capifs) and also
47 * using async or sync PPP on this connection.
48 * The two major device number 190 and 191 are not confirmed yet,
49 * but I want to save the code in cvs, before I go on.
51 * Revision 1.25 2000/03/03 16:37:11 kai
52 * incorporated some cosmetic changes from the official kernel tree back
55 * Revision 1.24 2000/03/03 15:50:42 calle
57 * - Changed parameter "param" in capi_signal from __u32 to void *.
58 * - rewrote notifier handling in kcapi.c
59 * - new notifier NCCI_UP and NCCI_DOWN
61 * - /dev/capi20 is now a cloning device.
62 * - middleware extentions prepared.
64 * - locking of list operations and module count updates.
66 * Revision 1.23 2000/02/26 01:00:53 keil
69 * Revision 1.22 1999/11/13 21:27:16 keil
70 * remove KERNELVERSION
72 * Revision 1.21 1999/09/10 17:24:18 calle
73 * Changes for proposed standard for CAPI2.0:
74 * - AK148 "Linux Exention"
76 * Revision 1.20 1999/09/07 09:02:53 calle
77 * SETDATA removed. Now inside the kernel the datapart of DATA_B3_REQ and
78 * DATA_B3_IND is always directly after the CAPI message. The "Data" member
79 * ist never used inside the kernel.
81 * Revision 1.19 1999/07/09 15:05:42 keil
82 * compat.h is now isdn_compat.h
84 * Revision 1.18 1999/07/06 07:42:01 calle
85 * - changes in /proc interface
86 * - check and changed calls to [dev_]kfree_skb and [dev_]alloc_skb.
88 * Revision 1.17 1999/07/01 15:26:30 calle
89 * complete new version (I love it):
90 * + new hardware independed "capi_driver" interface that will make it easy to:
91 * - support other controllers with CAPI-2.0 (i.e. USB Controller)
92 * - write a CAPI-2.0 for the passive cards
93 * - support serial link CAPI-2.0 boxes.
94 * + wrote "capi_driver" for all supported cards.
95 * + "capi_driver" (supported cards) now have to be configured with
96 * make menuconfig, in the past all supported cards where included
98 * + new and better informations in /proc/capi/
99 * + new ioctl to switch trace of capi messages per controller
100 * using "avmcapictrl trace [contr] on|off|...."
101 * + complete testcircle with all supported cards and also the
102 * PCMCIA cards (now patch for pcmcia-cs-3.0.13 needed) done.
104 * Revision 1.16 1999/07/01 08:22:57 keil
105 * compatibility macros now in <linux/isdn_compat.h>
107 * Revision 1.15 1999/06/21 15:24:11 calle
108 * extend information in /proc.
110 * Revision 1.14 1999/06/10 16:51:03 calle
111 * Bugfix: open/release of control device was not handled correct.
113 * Revision 1.13 1998/08/28 04:32:25 calle
114 * Added patch send by Michael.Mueller4@post.rwth-aachen.de, to get AVM B1
115 * driver running with 2.1.118.
117 * Revision 1.12 1998/05/26 22:39:34 he
118 * sync'ed with 2.1.102 where appropriate (CAPABILITY changes)
120 * cleared dev.tbusy in isdn_net BCONN status callback
122 * Revision 1.11 1998/03/09 17:46:37 he
123 * merged in 2.1.89 changes
125 * Revision 1.10 1998/02/13 07:09:13 calle
126 * change for 2.1.86 (removing FREE_READ/FREE_WRITE from [dev]_kfree_skb()
128 * Revision 1.9 1998/01/31 11:14:44 calle
129 * merged changes to 2.0 tree, prepare 2.1.82 to work.
131 * Revision 1.8 1997/11/04 06:12:08 calle
132 * capi.c: new read/write in file_ops since 2.1.60
133 * capidrv.c: prepared isdnlog interface for d2-trace in newer firmware.
134 * capiutil.c: needs config.h (CONFIG_ISDN_DRV_AVMB1_VERBOSE_REASON)
135 * compat.h: added #define LinuxVersionCode
137 * Revision 1.7 1997/10/11 10:29:34 calle
138 * llseek() parameters changed in 2.1.56.
140 * Revision 1.6 1997/10/01 09:21:15 fritz
141 * Removed old compatibility stuff for 2.0.X kernels.
142 * From now on, this code is for 2.1.X ONLY!
143 * Old stuff is still in the separate branch.
145 * Revision 1.5 1997/08/21 23:11:55 fritz
146 * Added changes for kernels >= 2.1.45
148 * Revision 1.4 1997/05/27 15:17:50 fritz
149 * Added changes for recent 2.1.x kernels:
150 * changed return type of isdn_close
151 * queue_task_* -> queue_task
152 * clear/set_bit -> test_and_... where apropriate.
153 * changed type of hard_header_cache parameter.
155 * Revision 1.3 1997/05/18 09:24:14 calle
156 * added verbose disconnect reason reporting to avmb1.
157 * some fixes in capi20 interface.
158 * changed info messages for B1-PCI
160 * Revision 1.2 1997/03/05 21:17:59 fritz
161 * Added capi_poll for compiling under 2.1.27
163 * Revision 1.1 1997/03/04 21:50:29 calle
164 * Frirst version in isdn4linux
166 * Revision 2.2 1997/02/12 09:31:39 calle
169 * Revision 1.1 1997/01/31 10:32:20 calle
174 #include <linux/module.h>
175 #include <linux/errno.h>
176 #include <linux/kernel.h>
177 #include <linux/major.h>
178 #include <linux/sched.h>
179 #include <linux/malloc.h>
180 #include <linux/fcntl.h>
181 #include <linux/fs.h>
182 #include <linux/signal.h>
183 #include <linux/mm.h>
184 #include <linux/timer.h>
185 #include <linux/wait.h>
186 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
187 #include <linux/tty.h>
189 #include <linux/netdevice.h>
190 #include <linux/ppp_defs.h>
191 #include <linux/if_ppp.h>
192 #undef CAPI_PPP_ON_RAW_DEVICE
193 #endif /* CONFIG_PPP */
194 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
195 #include <linux/skbuff.h>
196 #include <linux/proc_fs.h>
197 #include <linux/poll.h>
198 #include <linux/capi.h>
199 #include <linux/kernelcapi.h>
200 #include <linux/devfs_fs_kernel.h>
201 #include "capiutil.h"
203 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
205 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
206 #include <linux/slab.h>
208 static char *revision
= "$Revision: 1.35 $";
210 MODULE_AUTHOR("Carsten Paeth (calle@calle.in-berlin.de)");
212 #undef _DEBUG_REFCOUNT /* alloc/free and open/close debug */
213 #undef _DEBUG_TTYFUNCS /* call to tty_driver */
214 #undef _DEBUG_DATAFLOW /* data flow */
216 /* -------- driver information -------------------------------------- */
218 int capi_major
= 68; /* allocated */
219 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
220 int capi_rawmajor
= 190;
221 int capi_ttymajor
= 191;
222 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
224 MODULE_PARM(capi_major
, "i");
225 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
226 MODULE_PARM(capi_rawmajor
, "i");
227 MODULE_PARM(capi_ttymajor
, "i");
228 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
230 /* -------- defines ------------------------------------------------- */
232 #define CAPINC_MAX_RECVQUEUE 10
233 #define CAPINC_MAX_SENDQUEUE 10
234 #define CAPI_MAX_BLKSIZE 2048
236 /* -------- data structures ----------------------------------------- */
240 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
244 struct capiminor
*next
;
245 struct capincci
*nccip
;
254 struct tty_struct
*tty
;
257 struct sk_buff
*ttyskb
;
258 atomic_t ttyopencount
;
260 struct sk_buff_head inqueue
;
262 struct sk_buff_head outqueue
;
266 struct sk_buff_head recvqueue
;
267 wait_queue_head_t recvwait
;
268 wait_queue_head_t sendwait
;
271 struct datahandle_queue
{
272 struct datahandle_queue
*next
;
278 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
281 struct capincci
*next
;
283 struct capidev
*cdev
;
284 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
285 struct capiminor
*minorp
;
286 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
290 struct capidev
*next
;
297 struct sk_buff_head recvqueue
;
298 wait_queue_head_t recvwait
;
301 unsigned long nrecvctlpkt
;
302 unsigned long nrecvdatapkt
;
303 unsigned long nsentctlpkt
;
304 unsigned long nsentdatapkt
;
306 struct capincci
*nccis
;
309 /* -------- global variables ---------------------------------------- */
311 static struct capi_interface
*capifuncs
= 0;
312 static struct capidev
*capidev_openlist
= 0;
313 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
314 static struct capiminor
*minors
= 0;
315 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
317 static kmem_cache_t
*capidev_cachep
= 0;
318 static kmem_cache_t
*capincci_cachep
= 0;
319 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
320 static kmem_cache_t
*capiminor_cachep
= 0;
321 static kmem_cache_t
*capidh_cachep
= 0;
322 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
324 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
325 /* -------- datahandles --------------------------------------------- */
327 int capincci_add_ack(struct capiminor
*mp
, __u16 datahandle
)
329 struct datahandle_queue
*n
, **pp
;
331 n
= (struct datahandle_queue
*)
332 kmem_cache_alloc(capidh_cachep
, GFP_ATOMIC
);
334 printk(KERN_ERR
"capi: alloc datahandle failed\n");
338 n
->datahandle
= datahandle
;
339 for (pp
= &mp
->ackqueue
; *pp
; pp
= &(*pp
)->next
) ;
345 int capiminor_del_ack(struct capiminor
*mp
, __u16 datahandle
)
347 struct datahandle_queue
**pp
, *p
;
349 for (pp
= &mp
->ackqueue
; *pp
; pp
= &(*pp
)->next
) {
350 if ((*pp
)->datahandle
== datahandle
) {
353 kmem_cache_free(capidh_cachep
, p
);
361 void capiminor_del_all_ack(struct capiminor
*mp
)
363 struct datahandle_queue
**pp
, *p
;
365 for (pp
= &mp
->ackqueue
; *pp
; pp
= &(*pp
)->next
) {
368 kmem_cache_free(capidh_cachep
, p
);
374 /* -------- struct capiminor ---------------------------------------- */
376 struct capiminor
*capiminor_alloc(__u16 applid
, __u32 ncci
)
378 struct capiminor
*mp
, **pp
;
379 unsigned int minor
= 0;
382 mp
= (struct capiminor
*)kmem_cache_alloc(capiminor_cachep
, GFP_ATOMIC
);
385 printk(KERN_ERR
"capi: can't alloc capiminor\n");
388 #ifdef _DEBUG_REFCOUNT
389 printk(KERN_DEBUG
"capiminor_alloc %d\n", GET_USE_COUNT(THIS_MODULE
));
391 memset(mp
, 0, sizeof(struct capiminor
));
395 atomic_set(&mp
->ttyopencount
,0);
397 skb_queue_head_init(&mp
->inqueue
);
398 skb_queue_head_init(&mp
->outqueue
);
400 skb_queue_head_init(&mp
->recvqueue
);
401 init_waitqueue_head(&mp
->recvwait
);
402 init_waitqueue_head(&mp
->sendwait
);
404 for (pp
= &minors
; *pp
; pp
= &(*pp
)->next
) {
405 if ((*pp
)->minor
< minor
)
407 if ((*pp
)->minor
> minor
)
417 void capiminor_free(struct capiminor
*mp
)
419 struct capiminor
**pp
;
426 if (mp
->ttyskb
) kfree_skb(mp
->ttyskb
);
428 while ((skb
= skb_dequeue(&mp
->recvqueue
)) != 0)
430 while ((skb
= skb_dequeue(&mp
->inqueue
)) != 0)
432 while ((skb
= skb_dequeue(&mp
->outqueue
)) != 0)
434 capiminor_del_all_ack(mp
);
435 kmem_cache_free(capiminor_cachep
, mp
);
437 #ifdef _DEBUG_REFCOUNT
438 printk(KERN_DEBUG
"capiminor_free %d\n", GET_USE_COUNT(THIS_MODULE
));
447 struct capiminor
*capiminor_find(unsigned int minor
)
450 for (p
= minors
; p
&& p
->minor
!= minor
; p
= p
->next
)
454 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
456 /* -------- struct capincci ----------------------------------------- */
458 static struct capincci
*capincci_alloc(struct capidev
*cdev
, __u32 ncci
)
460 struct capincci
*np
, **pp
;
461 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
462 struct capiminor
*mp
= 0;
464 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
466 np
= (struct capincci
*)kmem_cache_alloc(capincci_cachep
, GFP_ATOMIC
);
469 memset(np
, 0, sizeof(struct capincci
));
472 for (pp
=&cdev
->nccis
; *pp
; pp
= &(*pp
)->next
)
475 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
477 if (cdev
->userflags
& CAPIFLAG_HIGHJACKING
)
478 mp
= np
->minorp
= capiminor_alloc(cdev
->applid
, ncci
);
481 #ifdef _DEBUG_REFCOUNT
482 printk(KERN_DEBUG
"set mp->nccip\n");
484 #ifdef CONFIG_ISDN_CAPIFS
485 kdev
= MKDEV(capi_rawmajor
, mp
->minor
);
486 capifs_new_ncci('r', mp
->minor
, kdev
);
487 kdev
= MKDEV(capi_ttymajor
, mp
->minor
);
488 capifs_new_ncci(0, mp
->minor
, kdev
);
491 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
495 static void capincci_free(struct capidev
*cdev
, __u32 ncci
)
497 struct capincci
*np
, **pp
;
498 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
499 struct capiminor
*mp
;
500 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
505 if (ncci
== 0xffffffff || np
->ncci
== ncci
) {
507 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
508 if ((mp
= np
->minorp
) != 0) {
509 #ifdef CONFIG_ISDN_CAPIFS
510 capifs_free_ncci('r', mp
->minor
);
511 capifs_free_ncci(0, mp
->minor
);
515 #ifdef _DEBUG_REFCOUNT
516 printk(KERN_DEBUG
"reset mp->nccip\n");
519 } else if (mp
->file
) {
521 #ifdef _DEBUG_REFCOUNT
522 printk(KERN_DEBUG
"reset mp->nccip\n");
524 wake_up_interruptible(&mp
->recvwait
);
525 wake_up_interruptible(&mp
->sendwait
);
530 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
531 kmem_cache_free(capincci_cachep
, np
);
532 if (*pp
== 0) return;
539 struct capincci
*capincci_find(struct capidev
*cdev
, __u32 ncci
)
543 for (p
=cdev
->nccis
; p
; p
= p
->next
) {
550 /* -------- struct capidev ------------------------------------------ */
552 static struct capidev
*capidev_alloc(struct file
*file
)
554 struct capidev
*cdev
;
557 cdev
= (struct capidev
*)kmem_cache_alloc(capidev_cachep
, GFP_KERNEL
);
560 memset(cdev
, 0, sizeof(struct capidev
));
562 cdev
->minor
= MINOR(file
->f_dentry
->d_inode
->i_rdev
);
564 skb_queue_head_init(&cdev
->recvqueue
);
565 init_waitqueue_head(&cdev
->recvwait
);
566 pp
=&capidev_openlist
;
567 while (*pp
) pp
= &(*pp
)->next
;
572 static void capidev_free(struct capidev
*cdev
)
578 (*capifuncs
->capi_release
) (cdev
->applid
);
581 while ((skb
= skb_dequeue(&cdev
->recvqueue
)) != 0) {
585 pp
=&capidev_openlist
;
586 while (*pp
&& *pp
!= cdev
) pp
= &(*pp
)->next
;
590 kmem_cache_free(capidev_cachep
, cdev
);
593 static struct capidev
*capidev_find(__u16 applid
)
596 for (p
=capidev_openlist
; p
; p
= p
->next
) {
597 if (p
->applid
== applid
)
603 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
604 /* -------- handle data queue --------------------------------------- */
607 gen_data_b3_resp_for(struct capiminor
*mp
, struct sk_buff
*skb
)
609 struct sk_buff
*nskb
;
610 nskb
= alloc_skb(CAPI_DATA_B3_RESP_LEN
, GFP_ATOMIC
);
612 __u16 datahandle
= CAPIMSG_U16(skb
->data
,CAPIMSG_BASELEN
+4+4+2);
613 unsigned char *s
= skb_put(nskb
, CAPI_DATA_B3_RESP_LEN
);
614 capimsg_setu16(s
, 0, CAPI_DATA_B3_RESP_LEN
);
615 capimsg_setu16(s
, 2, mp
->applid
);
616 capimsg_setu8 (s
, 4, CAPI_DATA_B3
);
617 capimsg_setu8 (s
, 5, CAPI_RESP
);
618 capimsg_setu16(s
, 6, mp
->msgid
++);
619 capimsg_setu32(s
, 8, mp
->ncci
);
620 capimsg_setu16(s
, 12, datahandle
);
625 int handle_recv_skb(struct capiminor
*mp
, struct sk_buff
*skb
)
627 struct sk_buff
*nskb
;
628 unsigned int datalen
;
629 __u16 errcode
, datahandle
;
631 datalen
= skb
->len
- CAPIMSG_LEN(skb
->data
);
633 if (mp
->tty
->ldisc
.receive_buf
== 0) {
634 printk(KERN_ERR
"capi: ldisc has no receive_buf function\n");
638 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
639 printk(KERN_DEBUG
"capi: recv tty throttled\n");
643 if (mp
->tty
->ldisc
.receive_room
&&
644 mp
->tty
->ldisc
.receive_room(mp
->tty
) < datalen
) {
645 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
646 printk(KERN_DEBUG
"capi: no room in tty\n");
650 if ((nskb
= gen_data_b3_resp_for(mp
, skb
)) == 0) {
651 printk(KERN_ERR
"capi: gen_data_b3_resp failed\n");
654 datahandle
= CAPIMSG_U16(skb
->data
,CAPIMSG_BASELEN
+4);
655 errcode
= (*capifuncs
->capi_put_message
)(mp
->applid
, nskb
);
656 if (errcode
!= CAPI_NOERROR
) {
657 printk(KERN_ERR
"capi: send DATA_B3_RESP failed=%x\n",
662 (void)skb_pull(skb
, CAPIMSG_LEN(skb
->data
));
663 #ifdef _DEBUG_DATAFLOW
664 printk(KERN_DEBUG
"capi: DATA_B3_RESP %u len=%d => ldisc\n",
665 datahandle
, skb
->len
);
667 mp
->tty
->ldisc
.receive_buf(mp
->tty
, skb
->data
, 0, skb
->len
);
671 } else if (mp
->file
) {
672 if (skb_queue_len(&mp
->recvqueue
) > CAPINC_MAX_RECVQUEUE
) {
673 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
674 printk(KERN_DEBUG
"capi: no room in raw queue\n");
678 if ((nskb
= gen_data_b3_resp_for(mp
, skb
)) == 0) {
679 printk(KERN_ERR
"capi: gen_data_b3_resp failed\n");
682 datahandle
= CAPIMSG_U16(skb
->data
,CAPIMSG_BASELEN
+4);
683 errcode
= (*capifuncs
->capi_put_message
)(mp
->applid
, nskb
);
684 if (errcode
!= CAPI_NOERROR
) {
685 printk(KERN_ERR
"capi: send DATA_B3_RESP failed=%x\n",
690 (void)skb_pull(skb
, CAPIMSG_LEN(skb
->data
));
691 #ifdef _DEBUG_DATAFLOW
692 printk(KERN_DEBUG
"capi: DATA_B3_RESP %u len=%d => raw\n",
693 datahandle
, skb
->len
);
695 skb_queue_tail(&mp
->recvqueue
, skb
);
696 wake_up_interruptible(&mp
->recvwait
);
699 #ifdef _DEBUG_DATAFLOW
700 printk(KERN_DEBUG
"capi: currently no receiver\n");
705 void handle_minor_recv(struct capiminor
*mp
)
708 while ((skb
= skb_dequeue(&mp
->inqueue
)) != 0) {
709 unsigned int len
= skb
->len
;
711 if (handle_recv_skb(mp
, skb
) < 0) {
712 skb_queue_head(&mp
->inqueue
, skb
);
719 int handle_minor_send(struct capiminor
*mp
)
727 if (mp
->tty
&& mp
->ttyoutstop
) {
728 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
729 printk(KERN_DEBUG
"capi: send: tty stopped\n");
734 while ((skb
= skb_dequeue(&mp
->outqueue
)) != 0) {
735 datahandle
= mp
->datahandle
;
736 len
= (__u16
)skb
->len
;
737 skb_push(skb
, CAPI_DATA_B3_REQ_LEN
);
738 memset(skb
->data
, 0, CAPI_DATA_B3_REQ_LEN
);
739 capimsg_setu16(skb
->data
, 0, CAPI_DATA_B3_REQ_LEN
);
740 capimsg_setu16(skb
->data
, 2, mp
->applid
);
741 capimsg_setu8 (skb
->data
, 4, CAPI_DATA_B3
);
742 capimsg_setu8 (skb
->data
, 5, CAPI_REQ
);
743 capimsg_setu16(skb
->data
, 6, mp
->msgid
++);
744 capimsg_setu32(skb
->data
, 8, mp
->ncci
); /* NCCI */
745 capimsg_setu32(skb
->data
, 12, (__u32
) skb
->data
); /* Data32 */
746 capimsg_setu16(skb
->data
, 16, len
); /* Data length */
747 capimsg_setu16(skb
->data
, 18, datahandle
);
748 capimsg_setu16(skb
->data
, 20, 0); /* Flags */
750 if (capincci_add_ack(mp
, datahandle
) < 0) {
751 skb_pull(skb
, CAPI_DATA_B3_REQ_LEN
);
752 skb_queue_head(&mp
->outqueue
, skb
);
755 errcode
= (*capifuncs
->capi_put_message
) (mp
->applid
, skb
);
756 if (errcode
== CAPI_NOERROR
) {
760 #ifdef _DEBUG_DATAFLOW
761 printk(KERN_DEBUG
"capi: DATA_B3_REQ %u len=%u\n",
766 capiminor_del_ack(mp
, datahandle
);
768 if (errcode
== CAPI_SENDQUEUEFULL
) {
769 skb_pull(skb
, CAPI_DATA_B3_REQ_LEN
);
770 skb_queue_head(&mp
->outqueue
, skb
);
774 /* ups, drop packet */
775 printk(KERN_ERR
"capi: put_message = %x\n", errcode
);
780 wake_up_interruptible(&mp
->sendwait
);
784 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
785 /* -------- function called by lower level -------------------------- */
787 static void capi_signal(__u16 applid
, void *param
)
789 struct capidev
*cdev
= (struct capidev
*)param
;
790 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
791 struct capiminor
*mp
;
793 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
795 struct sk_buff
*skb
= 0;
798 (void) (*capifuncs
->capi_get_message
) (applid
, &skb
);
800 printk(KERN_ERR
"BUG: capi_signal: no skb\n");
804 if (CAPIMSG_COMMAND(skb
->data
) != CAPI_DATA_B3
) {
805 skb_queue_tail(&cdev
->recvqueue
, skb
);
806 wake_up_interruptible(&cdev
->recvwait
);
809 ncci
= CAPIMSG_CONTROL(skb
->data
);
810 for (np
= cdev
->nccis
; np
&& np
->ncci
!= ncci
; np
= np
->next
)
813 printk(KERN_ERR
"BUG: capi_signal: ncci not found\n");
814 skb_queue_tail(&cdev
->recvqueue
, skb
);
815 wake_up_interruptible(&cdev
->recvwait
);
818 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
819 skb_queue_tail(&cdev
->recvqueue
, skb
);
820 wake_up_interruptible(&cdev
->recvwait
);
821 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
824 skb_queue_tail(&cdev
->recvqueue
, skb
);
825 wake_up_interruptible(&cdev
->recvwait
);
830 if (CAPIMSG_SUBCOMMAND(skb
->data
) == CAPI_IND
) {
831 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+4+4+2);
832 #ifdef _DEBUG_DATAFLOW
833 printk(KERN_DEBUG
"capi_signal: DATA_B3_IND %u len=%d\n",
834 datahandle
, skb
->len
-CAPIMSG_LEN(skb
->data
));
836 skb_queue_tail(&mp
->inqueue
, skb
);
837 mp
->inbytes
+= skb
->len
;
838 handle_minor_recv(mp
);
840 } else if (CAPIMSG_SUBCOMMAND(skb
->data
) == CAPI_CONF
) {
842 datahandle
= CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+4);
843 #ifdef _DEBUG_DATAFLOW
844 printk(KERN_DEBUG
"capi_signal: DATA_B3_CONF %u 0x%x\n",
846 CAPIMSG_U16(skb
->data
, CAPIMSG_BASELEN
+4+2));
849 (void)capiminor_del_ack(mp
, datahandle
);
851 if (mp
->tty
->ldisc
.write_wakeup
)
852 mp
->tty
->ldisc
.write_wakeup(mp
->tty
);
854 wake_up_interruptible(&mp
->sendwait
);
856 (void)handle_minor_send(mp
);
859 /* ups, let capi application handle it :-) */
860 skb_queue_tail(&cdev
->recvqueue
, skb
);
861 wake_up_interruptible(&cdev
->recvwait
);
863 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
866 /* -------- file_operations for capidev ----------------------------- */
869 capi_llseek(struct file
*file
, loff_t offset
, int origin
)
875 capi_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
)
877 struct capidev
*cdev
= (struct capidev
*)file
->private_data
;
882 if (ppos
!= &file
->f_pos
)
888 if ((skb
= skb_dequeue(&cdev
->recvqueue
)) == 0) {
890 if (file
->f_flags
& O_NONBLOCK
)
894 interruptible_sleep_on(&cdev
->recvwait
);
895 if ((skb
= skb_dequeue(&cdev
->recvqueue
)) != 0)
897 if (signal_pending(current
))
901 return -ERESTARTNOHAND
;
903 if (skb
->len
> count
) {
904 skb_queue_head(&cdev
->recvqueue
, skb
);
907 retval
= copy_to_user(buf
, skb
->data
, skb
->len
);
909 skb_queue_head(&cdev
->recvqueue
, skb
);
914 if (CAPIMSG_CMD(skb
->data
) == CAPI_DATA_B3_IND
) {
915 cdev
->nrecvdatapkt
++;
926 capi_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
928 struct capidev
*cdev
= (struct capidev
*)file
->private_data
;
933 if (ppos
!= &file
->f_pos
)
939 skb
= alloc_skb(count
, GFP_USER
);
941 if ((retval
= copy_from_user(skb_put(skb
, count
), buf
, count
))) {
945 mlen
= CAPIMSG_LEN(skb
->data
);
946 if (CAPIMSG_CMD(skb
->data
) == CAPI_DATA_B3_REQ
) {
947 if (mlen
+ CAPIMSG_DATALEN(skb
->data
) != count
) {
957 CAPIMSG_SETAPPID(skb
->data
, cdev
->applid
);
959 cdev
->errcode
= (*capifuncs
->capi_put_message
) (cdev
->applid
, skb
);
965 if (CAPIMSG_CMD(skb
->data
) == CAPI_DATA_B3_REQ
) {
966 cdev
->nsentdatapkt
++;
974 capi_poll(struct file
*file
, poll_table
* wait
)
976 struct capidev
*cdev
= (struct capidev
*)file
->private_data
;
977 unsigned int mask
= 0;
982 poll_wait(file
, &(cdev
->recvwait
), wait
);
983 mask
= POLLOUT
| POLLWRNORM
;
984 if (!skb_queue_empty(&cdev
->recvqueue
))
985 mask
|= POLLIN
| POLLRDNORM
;
990 capi_ioctl(struct inode
*inode
, struct file
*file
,
991 unsigned int cmd
, unsigned long arg
)
993 struct capidev
*cdev
= (struct capidev
*)file
->private_data
;
994 capi_ioctl_struct data
;
995 int retval
= -EINVAL
;
1000 retval
= copy_from_user((void *) &data
.rparams
,
1001 (void *) arg
, sizeof(struct capi_register_params
));
1006 cdev
->errcode
= (*capifuncs
->capi_register
) (&data
.rparams
,
1008 if (cdev
->errcode
) {
1012 (void) (*capifuncs
->capi_set_signal
) (cdev
->applid
, capi_signal
, cdev
);
1014 return (int)cdev
->applid
;
1016 case CAPI_GET_VERSION
:
1018 retval
= copy_from_user((void *) &data
.contr
,
1020 sizeof(data
.contr
));
1023 cdev
->errcode
= (*capifuncs
->capi_get_version
) (data
.contr
, &data
.version
);
1026 retval
= copy_to_user((void *) arg
,
1027 (void *) &data
.version
,
1028 sizeof(data
.version
));
1034 case CAPI_GET_SERIAL
:
1036 retval
= copy_from_user((void *) &data
.contr
,
1038 sizeof(data
.contr
));
1041 cdev
->errcode
= (*capifuncs
->capi_get_serial
) (data
.contr
, data
.serial
);
1044 retval
= copy_to_user((void *) arg
,
1045 (void *) data
.serial
,
1046 sizeof(data
.serial
));
1051 case CAPI_GET_PROFILE
:
1053 retval
= copy_from_user((void *) &data
.contr
,
1055 sizeof(data
.contr
));
1059 if (data
.contr
== 0) {
1060 cdev
->errcode
= (*capifuncs
->capi_get_profile
) (data
.contr
, &data
.profile
);
1064 retval
= copy_to_user((void *) arg
,
1065 (void *) &data
.profile
.ncontroller
,
1066 sizeof(data
.profile
.ncontroller
));
1069 cdev
->errcode
= (*capifuncs
->capi_get_profile
) (data
.contr
, &data
.profile
);
1073 retval
= copy_to_user((void *) arg
,
1074 (void *) &data
.profile
,
1075 sizeof(data
.profile
));
1082 case CAPI_GET_MANUFACTURER
:
1084 retval
= copy_from_user((void *) &data
.contr
,
1086 sizeof(data
.contr
));
1089 cdev
->errcode
= (*capifuncs
->capi_get_manufacturer
) (data
.contr
, data
.manufacturer
);
1093 retval
= copy_to_user((void *) arg
, (void *) data
.manufacturer
,
1094 sizeof(data
.manufacturer
));
1100 case CAPI_GET_ERRCODE
:
1101 data
.errcode
= cdev
->errcode
;
1102 cdev
->errcode
= CAPI_NOERROR
;
1104 retval
= copy_to_user((void *) arg
,
1105 (void *) &data
.errcode
,
1106 sizeof(data
.errcode
));
1110 return data
.errcode
;
1112 case CAPI_INSTALLED
:
1113 if ((*capifuncs
->capi_isinstalled
)() == CAPI_NOERROR
)
1117 case CAPI_MANUFACTURER_CMD
:
1119 struct capi_manufacturer_cmd mcmd
;
1120 if (!capable(CAP_SYS_ADMIN
))
1122 retval
= copy_from_user((void *) &mcmd
, (void *) arg
,
1126 return (*capifuncs
->capi_manufacturer
) (mcmd
.cmd
, mcmd
.data
);
1130 case CAPI_SET_FLAGS
:
1131 case CAPI_CLR_FLAGS
:
1134 retval
= copy_from_user((void *) &userflags
,
1139 if (cmd
== CAPI_SET_FLAGS
)
1140 cdev
->userflags
|= userflags
;
1142 cdev
->userflags
&= ~userflags
;
1146 case CAPI_GET_FLAGS
:
1148 retval
= copy_to_user((void *) arg
,
1149 (void *) &cdev
->userflags
,
1150 sizeof(cdev
->userflags
));
1156 case CAPI_NCCI_OPENCOUNT
:
1158 struct capincci
*nccip
;
1159 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1160 struct capiminor
*mp
;
1161 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1164 retval
= copy_from_user((void *) &ncci
,
1169 nccip
= capincci_find(cdev
, (__u32
) ncci
);
1172 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1173 if ((mp
= nccip
->minorp
) != 0) {
1174 count
+= atomic_read(&mp
->ttyopencount
);
1178 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1183 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1184 case CAPI_NCCI_GETUNIT
:
1186 struct capincci
*nccip
;
1187 struct capiminor
*mp
;
1189 retval
= copy_from_user((void *) &ncci
,
1194 nccip
= capincci_find(cdev
, (__u32
) ncci
);
1195 if (!nccip
|| (mp
= nccip
->minorp
) == 0)
1200 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1206 capi_open(struct inode
*inode
, struct file
*file
)
1208 if (file
->private_data
)
1211 if ((file
->private_data
= capidev_alloc(file
)) == 0)
1215 #ifdef _DEBUG_REFCOUNT
1216 printk(KERN_DEBUG
"capi_open %d\n", GET_USE_COUNT(THIS_MODULE
));
1222 capi_release(struct inode
*inode
, struct file
*file
)
1224 struct capidev
*cdev
= (struct capidev
*)file
->private_data
;
1226 capincci_free(cdev
, 0xffffffff);
1228 file
->private_data
= NULL
;
1231 #ifdef _DEBUG_REFCOUNT
1232 printk(KERN_DEBUG
"capi_release %d\n", GET_USE_COUNT(THIS_MODULE
));
1237 static struct file_operations capi_fops
=
1240 llseek
: capi_llseek
,
1246 release
: capi_release
,
1249 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1250 /* -------- file_operations for capincci ---------------------------- */
1253 capinc_raw_open(struct inode
*inode
, struct file
*file
)
1255 struct capiminor
*mp
;
1257 if (file
->private_data
)
1259 if ((mp
= capiminor_find(MINOR(file
->f_dentry
->d_inode
->i_rdev
))) == 0)
1266 #ifdef _DEBUG_REFCOUNT
1267 printk(KERN_DEBUG
"capi_raw_open %d\n", GET_USE_COUNT(THIS_MODULE
));
1272 file
->private_data
= (void *)mp
;
1273 handle_minor_recv(mp
);
1278 capinc_raw_llseek(struct file
*file
, loff_t offset
, int origin
)
1284 capinc_raw_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
)
1286 struct capiminor
*mp
= (struct capiminor
*)file
->private_data
;
1287 struct sk_buff
*skb
;
1291 if (ppos
!= &file
->f_pos
)
1294 if (!mp
|| !mp
->nccip
)
1297 if ((skb
= skb_dequeue(&mp
->recvqueue
)) == 0) {
1299 if (file
->f_flags
& O_NONBLOCK
)
1303 interruptible_sleep_on(&mp
->recvwait
);
1306 if ((skb
= skb_dequeue(&mp
->recvqueue
)) != 0)
1308 if (signal_pending(current
))
1312 return -ERESTARTNOHAND
;
1315 if (count
< skb
->len
) {
1316 retval
= copy_to_user(buf
, skb
->data
, count
);
1318 skb_queue_head(&mp
->recvqueue
, skb
);
1321 skb_pull(skb
, count
);
1322 skb_queue_head(&mp
->recvqueue
, skb
);
1326 retval
= copy_to_user(buf
, skb
->data
, skb
->len
);
1328 skb_queue_head(&mp
->recvqueue
, skb
);
1336 } while ((skb
= skb_dequeue(&mp
->recvqueue
)) != 0);
1342 capinc_raw_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
1344 struct capiminor
*mp
= (struct capiminor
*)file
->private_data
;
1345 struct sk_buff
*skb
;
1348 if (ppos
!= &file
->f_pos
)
1351 if (!mp
|| !mp
->nccip
)
1354 skb
= alloc_skb(CAPI_DATA_B3_REQ_LEN
+count
, GFP_USER
);
1356 skb_reserve(skb
, CAPI_DATA_B3_REQ_LEN
);
1357 if ((retval
= copy_from_user(skb_put(skb
, count
), buf
, count
))) {
1362 while (skb_queue_len(&mp
->outqueue
) > CAPINC_MAX_SENDQUEUE
) {
1363 if (file
->f_flags
& O_NONBLOCK
)
1365 interruptible_sleep_on(&mp
->sendwait
);
1366 if (mp
->nccip
== 0) {
1370 if (signal_pending(current
))
1371 return -ERESTARTNOHAND
;
1373 skb_queue_tail(&mp
->outqueue
, skb
);
1374 mp
->outbytes
+= skb
->len
;
1375 (void)handle_minor_send(mp
);
1380 capinc_raw_poll(struct file
*file
, poll_table
* wait
)
1382 struct capiminor
*mp
= (struct capiminor
*)file
->private_data
;
1383 unsigned int mask
= 0;
1385 if (!mp
|| !mp
->nccip
)
1386 return POLLERR
|POLLHUP
;
1388 poll_wait(file
, &(mp
->recvwait
), wait
);
1389 if (!skb_queue_empty(&mp
->recvqueue
))
1390 mask
|= POLLIN
| POLLRDNORM
;
1391 poll_wait(file
, &(mp
->sendwait
), wait
);
1392 if (skb_queue_len(&mp
->outqueue
) > CAPINC_MAX_SENDQUEUE
)
1393 mask
= POLLOUT
| POLLWRNORM
;
1398 capinc_raw_ioctl(struct inode
*inode
, struct file
*file
,
1399 unsigned int cmd
, unsigned long arg
)
1401 struct capiminor
*mp
= (struct capiminor
*)file
->private_data
;
1402 if (!mp
|| !mp
->nccip
)
1411 capinc_raw_release(struct inode
*inode
, struct file
*file
)
1413 struct capiminor
*mp
= (struct capiminor
*)file
->private_data
;
1417 if (mp
->nccip
== 0) {
1419 file
->private_data
= NULL
;
1423 #ifdef _DEBUG_REFCOUNT
1424 printk(KERN_DEBUG
"capinc_raw_release %d\n", GET_USE_COUNT(THIS_MODULE
));
1429 static struct file_operations capinc_raw_fops
=
1432 llseek
: capinc_raw_llseek
,
1433 read
: capinc_raw_read
,
1434 write
: capinc_raw_write
,
1435 poll
: capinc_raw_poll
,
1436 ioctl
: capinc_raw_ioctl
,
1437 open
: capinc_raw_open
,
1438 release
: capinc_raw_release
,
1441 /* -------- tty_operations for capincci ----------------------------- */
1443 int capinc_tty_open(struct tty_struct
* tty
, struct file
* file
)
1445 struct capiminor
*mp
;
1447 if ((mp
= capiminor_find(MINOR(file
->f_dentry
->d_inode
->i_rdev
))) == 0)
1454 skb_queue_head_init(&mp
->recvqueue
);
1455 init_waitqueue_head(&mp
->recvwait
);
1456 init_waitqueue_head(&mp
->sendwait
);
1457 tty
->driver_data
= (void *)mp
;
1458 #ifdef _DEBUG_REFCOUNT
1459 printk(KERN_DEBUG
"capi_tty_open %d\n", GET_USE_COUNT(THIS_MODULE
));
1461 if (atomic_read(&mp
->ttyopencount
) == 0)
1463 atomic_inc(&mp
->ttyopencount
);
1464 #ifdef _DEBUG_REFCOUNT
1465 printk(KERN_DEBUG
"capinc_tty_open ocount=%d\n", atomic_read(&mp
->ttyopencount
));
1467 handle_minor_recv(mp
);
1471 void capinc_tty_close(struct tty_struct
* tty
, struct file
* file
)
1473 struct capiminor
*mp
;
1475 mp
= (struct capiminor
*)tty
->driver_data
;
1477 if (atomic_dec_and_test(&mp
->ttyopencount
)) {
1478 #ifdef _DEBUG_REFCOUNT
1479 printk(KERN_DEBUG
"capinc_tty_close lastclose\n");
1481 tty
->driver_data
= (void *)0;
1484 #ifdef _DEBUG_REFCOUNT
1485 printk(KERN_DEBUG
"capinc_tty_close ocount=%d\n", atomic_read(&mp
->ttyopencount
));
1491 #ifdef _DEBUG_REFCOUNT
1492 printk(KERN_DEBUG
"capinc_tty_close\n");
1496 int capinc_tty_write(struct tty_struct
* tty
, int from_user
,
1497 const unsigned char *buf
, int count
)
1499 struct capiminor
*mp
= (struct capiminor
*)tty
->driver_data
;
1500 struct sk_buff
*skb
;
1503 #ifdef _DEBUG_TTYFUNCS
1504 printk(KERN_DEBUG
"capinc_tty_write(from_user=%d,count=%d)\n",
1508 if (!mp
|| !mp
->nccip
) {
1509 #ifdef _DEBUG_TTYFUNCS
1510 printk(KERN_DEBUG
"capinc_tty_write: mp or mp->ncci NULL\n");
1518 skb_queue_tail(&mp
->outqueue
, skb
);
1519 mp
->outbytes
+= skb
->len
;
1522 skb
= alloc_skb(CAPI_DATA_B3_REQ_LEN
+count
, GFP_ATOMIC
);
1524 printk(KERN_ERR
"capinc_tty_write: alloc_skb failed\n");
1528 skb_reserve(skb
, CAPI_DATA_B3_REQ_LEN
);
1530 if ((retval
= copy_from_user(skb_put(skb
, count
), buf
, count
))) {
1532 #ifdef _DEBUG_TTYFUNCS
1533 printk(KERN_DEBUG
"capinc_tty_write: copy_from_user=%d\n", retval
);
1538 memcpy(skb_put(skb
, count
), buf
, count
);
1541 skb_queue_tail(&mp
->outqueue
, skb
);
1542 mp
->outbytes
+= skb
->len
;
1543 (void)handle_minor_send(mp
);
1544 (void)handle_minor_recv(mp
);
1548 void capinc_tty_put_char(struct tty_struct
*tty
, unsigned char ch
)
1550 struct capiminor
*mp
= (struct capiminor
*)tty
->driver_data
;
1551 struct sk_buff
*skb
;
1553 #ifdef _DEBUG_TTYFUNCS
1554 printk(KERN_DEBUG
"capinc_put_char(%u)\n", ch
);
1557 if (!mp
|| !mp
->nccip
) {
1558 #ifdef _DEBUG_TTYFUNCS
1559 printk(KERN_DEBUG
"capinc_tty_put_char: mp or mp->ncci NULL\n");
1566 if (skb_tailroom(skb
) > 0) {
1567 *(skb_put(skb
, 1)) = ch
;
1571 skb_queue_tail(&mp
->outqueue
, skb
);
1572 mp
->outbytes
+= skb
->len
;
1573 (void)handle_minor_send(mp
);
1575 skb
= alloc_skb(CAPI_DATA_B3_REQ_LEN
+CAPI_MAX_BLKSIZE
, GFP_ATOMIC
);
1577 skb_reserve(skb
, CAPI_DATA_B3_REQ_LEN
);
1578 *(skb_put(skb
, 1)) = ch
;
1581 printk(KERN_ERR
"capinc_put_char: char %u lost\n", ch
);
1585 void capinc_tty_flush_chars(struct tty_struct
*tty
)
1587 struct capiminor
*mp
= (struct capiminor
*)tty
->driver_data
;
1588 struct sk_buff
*skb
;
1590 #ifdef _DEBUG_TTYFUNCS
1591 printk(KERN_DEBUG
"capinc_tty_flush_chars\n");
1594 if (!mp
|| !mp
->nccip
) {
1595 #ifdef _DEBUG_TTYFUNCS
1596 printk(KERN_DEBUG
"capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1604 skb_queue_tail(&mp
->outqueue
, skb
);
1605 mp
->outbytes
+= skb
->len
;
1606 (void)handle_minor_send(mp
);
1608 (void)handle_minor_recv(mp
);
1611 int capinc_tty_write_room(struct tty_struct
*tty
)
1613 struct capiminor
*mp
= (struct capiminor
*)tty
->driver_data
;
1615 if (!mp
|| !mp
->nccip
) {
1616 #ifdef _DEBUG_TTYFUNCS
1617 printk(KERN_DEBUG
"capinc_tty_write_room: mp or mp->ncci NULL\n");
1621 room
= CAPINC_MAX_SENDQUEUE
-skb_queue_len(&mp
->outqueue
);
1622 room
*= CAPI_MAX_BLKSIZE
;
1623 #ifdef _DEBUG_TTYFUNCS
1624 printk(KERN_DEBUG
"capinc_tty_write_room = %d\n", room
);
1629 int capinc_tty_chars_in_buffer(struct tty_struct
*tty
)
1631 struct capiminor
*mp
= (struct capiminor
*)tty
->driver_data
;
1632 if (!mp
|| !mp
->nccip
) {
1633 #ifdef _DEBUG_TTYFUNCS
1634 printk(KERN_DEBUG
"capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1638 #ifdef _DEBUG_TTYFUNCS
1639 printk(KERN_DEBUG
"capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1640 mp
->outbytes
, mp
->nack
,
1641 skb_queue_len(&mp
->outqueue
),
1642 skb_queue_len(&mp
->inqueue
));
1644 return mp
->outbytes
;
1647 int capinc_tty_ioctl(struct tty_struct
*tty
, struct file
* file
,
1648 unsigned int cmd
, unsigned long arg
)
1650 return -ENOIOCTLCMD
;
1653 void capinc_tty_set_termios(struct tty_struct
*tty
, struct termios
* old
)
1655 #ifdef _DEBUG_TTYFUNCS
1656 printk(KERN_DEBUG
"capinc_tty_set_termios\n");
1660 void capinc_tty_throttle(struct tty_struct
* tty
)
1662 struct capiminor
*mp
= (struct capiminor
*)tty
->driver_data
;
1663 #ifdef _DEBUG_TTYFUNCS
1664 printk(KERN_DEBUG
"capinc_tty_throttle\n");
1670 void capinc_tty_unthrottle(struct tty_struct
* tty
)
1672 struct capiminor
*mp
= (struct capiminor
*)tty
->driver_data
;
1673 #ifdef _DEBUG_TTYFUNCS
1674 printk(KERN_DEBUG
"capinc_tty_unthrottle\n");
1678 handle_minor_recv(mp
);
1682 void capinc_tty_stop(struct tty_struct
*tty
)
1684 struct capiminor
*mp
= (struct capiminor
*)tty
->driver_data
;
1685 #ifdef _DEBUG_TTYFUNCS
1686 printk(KERN_DEBUG
"capinc_tty_stop\n");
1693 void capinc_tty_start(struct tty_struct
*tty
)
1695 struct capiminor
*mp
= (struct capiminor
*)tty
->driver_data
;
1696 #ifdef _DEBUG_TTYFUNCS
1697 printk(KERN_DEBUG
"capinc_tty_start\n");
1701 (void)handle_minor_send(mp
);
1705 void capinc_tty_hangup(struct tty_struct
*tty
)
1707 #ifdef _DEBUG_TTYFUNCS
1708 printk(KERN_DEBUG
"capinc_tty_hangup\n");
1712 void capinc_tty_break_ctl(struct tty_struct
*tty
, int state
)
1714 #ifdef _DEBUG_TTYFUNCS
1715 printk(KERN_DEBUG
"capinc_tty_break_ctl(%d)\n", state
);
1719 void capinc_tty_flush_buffer(struct tty_struct
*tty
)
1721 #ifdef _DEBUG_TTYFUNCS
1722 printk(KERN_DEBUG
"capinc_tty_flush_buffer\n");
1726 void capinc_tty_set_ldisc(struct tty_struct
*tty
)
1728 #ifdef _DEBUG_TTYFUNCS
1729 printk(KERN_DEBUG
"capinc_tty_set_ldisc\n");
1733 void capinc_tty_send_xchar(struct tty_struct
*tty
, char ch
)
1735 #ifdef _DEBUG_TTYFUNCS
1736 printk(KERN_DEBUG
"capinc_tty_send_xchar(%d)\n", ch
);
1740 int capinc_tty_read_proc(char *page
, char **start
, off_t off
,
1741 int count
, int *eof
, void *data
)
1746 int capinc_write_proc(struct file
*file
, const char *buffer
,
1747 unsigned long count
, void *data
)
1752 #define CAPINC_NR_PORTS 256
1753 static struct tty_driver capinc_tty_driver
;
1754 static int capinc_tty_refcount
;
1755 static struct tty_struct
*capinc_tty_table
[CAPINC_NR_PORTS
];
1756 static struct termios
*capinc_tty_termios
[CAPINC_NR_PORTS
];
1757 static struct termios
*capinc_tty_termios_locked
[CAPINC_NR_PORTS
];
1759 int capinc_tty_init(void)
1761 struct tty_driver
*drv
= &capinc_tty_driver
;
1763 /* Initialize the tty_driver structure */
1765 memset(drv
, 0, sizeof(struct tty_driver
));
1766 drv
->magic
= TTY_DRIVER_MAGIC
;
1767 #if (LINUX_VERSION_CODE > 0x20100)
1768 drv
->driver_name
= "capi_nc";
1770 drv
->name
= "capi/%d";
1771 drv
->major
= capi_ttymajor
;
1772 drv
->minor_start
= 0;
1773 drv
->num
= CAPINC_NR_PORTS
;
1774 drv
->type
= TTY_DRIVER_TYPE_SERIAL
;
1775 drv
->subtype
= SERIAL_TYPE_NORMAL
;
1776 drv
->init_termios
= tty_std_termios
;
1777 drv
->init_termios
.c_iflag
= ICRNL
;
1778 drv
->init_termios
.c_oflag
= OPOST
| ONLCR
;
1779 drv
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1780 drv
->init_termios
.c_lflag
= 0;
1781 drv
->flags
= TTY_DRIVER_REAL_RAW
|TTY_DRIVER_RESET_TERMIOS
;
1782 drv
->refcount
= &capinc_tty_refcount
;
1783 drv
->table
= capinc_tty_table
;
1784 drv
->termios
= capinc_tty_termios
;
1785 drv
->termios_locked
= capinc_tty_termios_locked
;
1787 drv
->open
= capinc_tty_open
;
1788 drv
->close
= capinc_tty_close
;
1789 drv
->write
= capinc_tty_write
;
1790 drv
->put_char
= capinc_tty_put_char
;
1791 drv
->flush_chars
= capinc_tty_flush_chars
;
1792 drv
->write_room
= capinc_tty_write_room
;
1793 drv
->chars_in_buffer
= capinc_tty_chars_in_buffer
;
1794 drv
->ioctl
= capinc_tty_ioctl
;
1795 drv
->set_termios
= capinc_tty_set_termios
;
1796 drv
->throttle
= capinc_tty_throttle
;
1797 drv
->unthrottle
= capinc_tty_unthrottle
;
1798 drv
->stop
= capinc_tty_stop
;
1799 drv
->start
= capinc_tty_start
;
1800 drv
->hangup
= capinc_tty_hangup
;
1801 #if (LINUX_VERSION_CODE >= 131394) /* Linux 2.1.66 */
1802 drv
->break_ctl
= capinc_tty_break_ctl
;
1804 drv
->flush_buffer
= capinc_tty_flush_buffer
;
1805 drv
->set_ldisc
= capinc_tty_set_ldisc
;
1806 #if (LINUX_VERSION_CODE >= 131343)
1807 drv
->send_xchar
= capinc_tty_send_xchar
;
1808 drv
->read_proc
= capinc_tty_read_proc
;
1810 if (tty_register_driver(drv
)) {
1811 printk(KERN_ERR
"Couldn't register capi_nc driver\n");
1817 void capinc_tty_exit(void)
1819 struct tty_driver
*drv
= &capinc_tty_driver
;
1821 if ((retval
= tty_unregister_driver(drv
)))
1822 printk(KERN_ERR
"capi: failed to unregister capi_nc driver (%d)\n", retval
);
1825 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1827 /* -------- /proc functions ----------------------------------------- */
1830 * /proc/capi/capi20:
1831 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1833 static int proc_capidev_read_proc(char *page
, char **start
, off_t off
,
1834 int count
, int *eof
, void *data
)
1836 struct capidev
*cdev
;
1840 for (cdev
=capidev_openlist
; cdev
; cdev
= cdev
->next
) {
1841 len
+= sprintf(page
+len
, "%d %d %lu %lu %lu %lu\n",
1847 cdev
->nsentdatapkt
);
1848 if (len
+begin
> off
+count
)
1850 if (len
+begin
< off
) {
1858 if (off
>= len
+begin
)
1860 *start
= page
+ (off
-begin
);
1861 return ((count
< begin
+len
-off
) ? count
: begin
+len
-off
);
1865 * /proc/capi/capi20ncci:
1868 static int proc_capincci_read_proc(char *page
, char **start
, off_t off
,
1869 int count
, int *eof
, void *data
)
1871 struct capidev
*cdev
;
1872 struct capincci
*np
;
1876 for (cdev
=capidev_openlist
; cdev
; cdev
= cdev
->next
) {
1877 for (np
=cdev
->nccis
; np
; np
= np
->next
) {
1878 len
+= sprintf(page
+len
, "%d 0x%x%s\n",
1881 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
1883 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1884 np
->minorp
&& np
->minorp
->file
? " open" : "");
1885 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1886 if (len
+begin
> off
+count
)
1888 if (len
+begin
< off
) {
1897 if (off
>= len
+begin
)
1899 *start
= page
+ (begin
-off
);
1900 return ((count
< begin
+len
-off
) ? count
: begin
+len
-off
);
1903 static struct procfsentries
{
1906 int (*read_proc
)(char *page
, char **start
, off_t off
,
1907 int count
, int *eof
, void *data
);
1908 struct proc_dir_entry
*procent
;
1909 } procfsentries
[] = {
1910 /* { "capi", S_IFDIR, 0 }, */
1911 { "capi/capi20", 0 , proc_capidev_read_proc
},
1912 { "capi/capi20ncci", 0 , proc_capincci_read_proc
},
1915 static void proc_init(void)
1917 int nelem
= sizeof(procfsentries
)/sizeof(procfsentries
[0]);
1920 for (i
=0; i
< nelem
; i
++) {
1921 struct procfsentries
*p
= procfsentries
+ i
;
1922 p
->procent
= create_proc_entry(p
->name
, p
->mode
, 0);
1923 if (p
->procent
) p
->procent
->read_proc
= p
->read_proc
;
1927 static void proc_exit(void)
1929 int nelem
= sizeof(procfsentries
)/sizeof(procfsentries
[0]);
1932 for (i
=nelem
-1; i
>= 0; i
--) {
1933 struct procfsentries
*p
= procfsentries
+ i
;
1935 remove_proc_entry(p
->name
, 0);
1941 /* -------- init function and module interface ---------------------- */
1944 static void alloc_exit(void)
1946 if (capidev_cachep
) {
1947 (void)kmem_cache_destroy(capidev_cachep
);
1950 if (capincci_cachep
) {
1951 (void)kmem_cache_destroy(capincci_cachep
);
1952 capincci_cachep
= 0;
1954 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1955 if (capidh_cachep
) {
1956 (void)kmem_cache_destroy(capidh_cachep
);
1959 if (capiminor_cachep
) {
1960 (void)kmem_cache_destroy(capiminor_cachep
);
1961 capiminor_cachep
= 0;
1963 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1966 static int alloc_init(void)
1968 capidev_cachep
= kmem_cache_create("capi20_dev",
1969 sizeof(struct capidev
),
1973 if (!capidev_cachep
) {
1978 capincci_cachep
= kmem_cache_create("capi20_ncci",
1979 sizeof(struct capincci
),
1983 if (!capincci_cachep
) {
1987 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1988 capidh_cachep
= kmem_cache_create("capi20_dh",
1989 sizeof(struct datahandle_queue
),
1993 if (!capidh_cachep
) {
1997 capiminor_cachep
= kmem_cache_create("capi20_minor",
1998 sizeof(struct capiminor
),
2002 if (!capiminor_cachep
) {
2006 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
2010 static void lower_callback(unsigned int cmd
, __u32 contr
, void *data
)
2012 struct capi_ncciinfo
*np
;
2013 struct capidev
*cdev
;
2017 printk(KERN_INFO
"capi: controller %hu up\n", contr
);
2020 printk(KERN_INFO
"capi: controller %hu down\n", contr
);
2023 np
= (struct capi_ncciinfo
*)data
;
2024 if ((cdev
= capidev_find(np
->applid
)) == 0)
2026 (void)capincci_alloc(cdev
, np
->ncci
);
2029 np
= (struct capi_ncciinfo
*)data
;
2030 if ((cdev
= capidev_find(np
->applid
)) == 0)
2032 (void)capincci_free(cdev
, np
->ncci
);
2038 #define capi_init init_module
2041 static struct capi_interface_user cuser
= {
2046 static char rev
[10];
2054 if ((p
= strchr(revision
, ':'))) {
2056 p
= strchr(rev
, '$');
2061 if (devfs_register_chrdev(capi_major
, "capi20", &capi_fops
)) {
2062 printk(KERN_ERR
"capi20: unable to get major %d\n", capi_major
);
2067 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
2068 if (devfs_register_chrdev(capi_rawmajor
, "capi/r%d", &capinc_raw_fops
)) {
2069 devfs_unregister_chrdev(capi_major
, "capi20");
2070 printk(KERN_ERR
"capi20: unable to get major %d\n", capi_rawmajor
);
2074 devfs_register_series (NULL
, "capi/r%u", CAPINC_NR_PORTS
,
2077 S_IFCHR
| S_IRUSR
| S_IWUSR
,
2078 &capinc_raw_fops
, NULL
);
2079 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
2080 devfs_register (NULL
, "isdn/capi20", DEVFS_FL_DEFAULT
,
2081 capi_major
, 0, S_IFCHR
| S_IRUSR
| S_IWUSR
,
2083 printk(KERN_NOTICE
"capi20: started up with major %d\n", capi_major
);
2085 if ((capifuncs
= attach_capi_interface(&cuser
)) == 0) {
2088 devfs_unregister_chrdev(capi_major
, "capi20");
2089 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
2090 devfs_unregister_chrdev(capi_rawmajor
, "capi/r%d");
2091 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
2092 devfs_unregister(devfs_find_handle(NULL
, "capi20",
2094 DEVFS_SPECIAL_CHR
, 0));
2098 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
2099 if (capinc_tty_init() < 0) {
2100 (void) detach_capi_interface(&cuser
);
2101 devfs_unregister_chrdev(capi_major
, "capi20");
2102 devfs_unregister_chrdev(capi_rawmajor
, "capi/r%d");
2106 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
2108 if (alloc_init() < 0) {
2109 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
2111 devfs_unregister_chrdev(capi_rawmajor
, "capi/r%d");
2112 for (j
= 0; j
< CAPINC_NR_PORTS
; j
++) {
2114 sprintf(devname
, "capi/r%u", j
);
2115 devfs_unregister(devfs_find_handle(NULL
, devname
, capi_rawmajor
, j
, DEVFS_SPECIAL_CHR
, 0));
2118 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
2119 (void) detach_capi_interface(&cuser
);
2120 devfs_unregister_chrdev(capi_major
, "capi20");
2121 devfs_unregister(devfs_find_handle(NULL
, "capi20",
2123 DEVFS_SPECIAL_CHR
, 0));
2130 printk(KERN_NOTICE
"capi20: Rev%s: started up with major %d\n",
2138 void cleanup_module(void)
2140 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
2146 devfs_unregister_chrdev(capi_major
, "capi20");
2147 devfs_unregister(devfs_find_handle(NULL
, "isdn/capi20", capi_major
, 0, DEVFS_SPECIAL_CHR
, 0));
2149 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
2151 devfs_unregister_chrdev(capi_rawmajor
, "capi/r%d");
2152 for (j
= 0; j
< CAPINC_NR_PORTS
; j
++) {
2154 sprintf(devname
, "capi/r%u", j
);
2155 devfs_unregister(devfs_find_handle(NULL
, devname
, capi_rawmajor
, j
, DEVFS_SPECIAL_CHR
, 0));
2158 (void) detach_capi_interface(&cuser
);
2159 printk(KERN_NOTICE
"capi: Rev%s: unloaded\n", rev
);