Import 2.4.0-test3pre3
[davej-history.git] / drivers / isdn / avmb1 / capi.c
blob009010896ec460fce880a55599be5fe6adfd2f32
1 /*
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)
8 * $Log: capi.c,v $
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
53 * into CVS
55 * Revision 1.24 2000/03/03 15:50:42 calle
56 * - kernel CAPI:
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
60 * - User CAPI:
61 * - /dev/capi20 is now a cloning device.
62 * - middleware extentions prepared.
63 * - capidrv.c
64 * - locking of list operations and module count updates.
66 * Revision 1.23 2000/02/26 01:00:53 keil
67 * changes from 2.3.47
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
97 * at once.
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)
119 * concap typo
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
167 * new version
169 * Revision 1.1 1997/01/31 10:32:20 calle
170 * Initial revision
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>
188 #ifdef CONFIG_PPP
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"
202 #include "capicmd.h"
203 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
204 #include "capifs.h"
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 ----------------------------------------- */
238 struct capidev;
239 struct capincci;
240 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
241 struct capiminor;
243 struct capiminor {
244 struct capiminor *next;
245 struct capincci *nccip;
246 unsigned int minor;
248 __u16 applid;
249 __u32 ncci;
250 __u16 datahandle;
251 __u16 msgid;
253 struct file *file;
254 struct tty_struct *tty;
255 int ttyinstop;
256 int ttyoutstop;
257 struct sk_buff *ttyskb;
258 atomic_t ttyopencount;
260 struct sk_buff_head inqueue;
261 int inbytes;
262 struct sk_buff_head outqueue;
263 int outbytes;
265 /* for raw device */
266 struct sk_buff_head recvqueue;
267 wait_queue_head_t recvwait;
268 wait_queue_head_t sendwait;
270 /* transmit path */
271 struct datahandle_queue {
272 struct datahandle_queue *next;
273 __u16 datahandle;
274 } *ackqueue;
275 int nack;
278 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
280 struct capincci {
281 struct capincci *next;
282 __u32 ncci;
283 struct capidev *cdev;
284 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
285 struct capiminor *minorp;
286 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
289 struct capidev {
290 struct capidev *next;
291 struct file *file;
292 __u16 applid;
293 __u16 errcode;
294 unsigned int minor;
295 unsigned userflags;
297 struct sk_buff_head recvqueue;
298 wait_queue_head_t recvwait;
300 /* Statistic */
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);
333 if (!n) {
334 printk(KERN_ERR "capi: alloc datahandle failed\n");
335 return -1;
337 n->next = 0;
338 n->datahandle = datahandle;
339 for (pp = &mp->ackqueue; *pp; pp = &(*pp)->next) ;
340 *pp = n;
341 mp->nack++;
342 return 0;
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) {
351 p = *pp;
352 *pp = (*pp)->next;
353 kmem_cache_free(capidh_cachep, p);
354 mp->nack--;
355 return 0;
358 return -1;
361 void capiminor_del_all_ack(struct capiminor *mp)
363 struct datahandle_queue **pp, *p;
365 for (pp = &mp->ackqueue; *pp; pp = &(*pp)->next) {
366 p = *pp;
367 *pp = (*pp)->next;
368 kmem_cache_free(capidh_cachep, p);
369 mp->nack--;
374 /* -------- struct capiminor ---------------------------------------- */
376 struct capiminor *capiminor_alloc(__u16 applid, __u32 ncci)
378 struct capiminor *mp, **pp;
379 unsigned int minor = 0;
381 MOD_INC_USE_COUNT;
382 mp = (struct capiminor *)kmem_cache_alloc(capiminor_cachep, GFP_ATOMIC);
383 if (!mp) {
384 MOD_DEC_USE_COUNT;
385 printk(KERN_ERR "capi: can't alloc capiminor\n");
386 return 0;
388 #ifdef _DEBUG_REFCOUNT
389 printk(KERN_DEBUG "capiminor_alloc %d\n", GET_USE_COUNT(THIS_MODULE));
390 #endif
391 memset(mp, 0, sizeof(struct capiminor));
392 mp->applid = applid;
393 mp->ncci = ncci;
394 mp->msgid = 0;
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)
406 continue;
407 if ((*pp)->minor > minor)
408 break;
409 minor++;
411 mp->minor = minor;
412 mp->next = *pp;
413 *pp = mp;
414 return mp;
417 void capiminor_free(struct capiminor *mp)
419 struct capiminor **pp;
420 struct sk_buff *skb;
422 pp = &minors;
423 while (*pp) {
424 if (*pp == mp) {
425 *pp = (*pp)->next;
426 if (mp->ttyskb) kfree_skb(mp->ttyskb);
427 mp->ttyskb = 0;
428 while ((skb = skb_dequeue(&mp->recvqueue)) != 0)
429 kfree_skb(skb);
430 while ((skb = skb_dequeue(&mp->inqueue)) != 0)
431 kfree_skb(skb);
432 while ((skb = skb_dequeue(&mp->outqueue)) != 0)
433 kfree_skb(skb);
434 capiminor_del_all_ack(mp);
435 kmem_cache_free(capiminor_cachep, mp);
436 MOD_DEC_USE_COUNT;
437 #ifdef _DEBUG_REFCOUNT
438 printk(KERN_DEBUG "capiminor_free %d\n", GET_USE_COUNT(THIS_MODULE));
439 #endif
440 return;
441 } else {
442 pp = &(*pp)->next;
447 struct capiminor *capiminor_find(unsigned int minor)
449 struct capiminor *p;
450 for (p = minors; p && p->minor != minor; p = p->next)
452 return p;
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;
463 kdev_t kdev;
464 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
466 np = (struct capincci *)kmem_cache_alloc(capincci_cachep, GFP_ATOMIC);
467 if (!np)
468 return 0;
469 memset(np, 0, sizeof(struct capincci));
470 np->ncci = ncci;
471 np->cdev = cdev;
472 for (pp=&cdev->nccis; *pp; pp = &(*pp)->next)
474 *pp = np;
475 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
476 mp = 0;
477 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
478 mp = np->minorp = capiminor_alloc(cdev->applid, ncci);
479 if (mp) {
480 mp->nccip = np;
481 #ifdef _DEBUG_REFCOUNT
482 printk(KERN_DEBUG "set mp->nccip\n");
483 #endif
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);
489 #endif
491 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
492 return np;
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 */
502 pp=&cdev->nccis;
503 while (*pp) {
504 np = *pp;
505 if (ncci == 0xffffffff || np->ncci == ncci) {
506 *pp = (*pp)->next;
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);
512 #endif
513 if (mp->tty) {
514 mp->nccip = 0;
515 #ifdef _DEBUG_REFCOUNT
516 printk(KERN_DEBUG "reset mp->nccip\n");
517 #endif
518 tty_hangup(mp->tty);
519 } else if (mp->file) {
520 mp->nccip = 0;
521 #ifdef _DEBUG_REFCOUNT
522 printk(KERN_DEBUG "reset mp->nccip\n");
523 #endif
524 wake_up_interruptible(&mp->recvwait);
525 wake_up_interruptible(&mp->sendwait);
526 } else {
527 capiminor_free(mp);
530 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
531 kmem_cache_free(capincci_cachep, np);
532 if (*pp == 0) return;
533 } else {
534 pp = &(*pp)->next;
539 struct capincci *capincci_find(struct capidev *cdev, __u32 ncci)
541 struct capincci *p;
543 for (p=cdev->nccis; p ; p = p->next) {
544 if (p->ncci == ncci)
545 break;
547 return p;
550 /* -------- struct capidev ------------------------------------------ */
552 static struct capidev *capidev_alloc(struct file *file)
554 struct capidev *cdev;
555 struct capidev **pp;
557 cdev = (struct capidev *)kmem_cache_alloc(capidev_cachep, GFP_KERNEL);
558 if (!cdev)
559 return 0;
560 memset(cdev, 0, sizeof(struct capidev));
561 cdev->file = file;
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;
568 *pp = cdev;
569 return cdev;
572 static void capidev_free(struct capidev *cdev)
574 struct capidev **pp;
575 struct sk_buff *skb;
577 if (cdev->applid)
578 (*capifuncs->capi_release) (cdev->applid);
579 cdev->applid = 0;
581 while ((skb = skb_dequeue(&cdev->recvqueue)) != 0) {
582 kfree_skb(skb);
585 pp=&capidev_openlist;
586 while (*pp && *pp != cdev) pp = &(*pp)->next;
587 if (*pp)
588 *pp = cdev->next;
590 kmem_cache_free(capidev_cachep, cdev);
593 static struct capidev *capidev_find(__u16 applid)
595 struct capidev *p;
596 for (p=capidev_openlist; p; p = p->next) {
597 if (p->applid == applid)
598 break;
600 return p;
603 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
604 /* -------- handle data queue --------------------------------------- */
606 struct sk_buff *
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);
611 if (nskb) {
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);
622 return nskb;
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);
632 if (mp->tty) {
633 if (mp->tty->ldisc.receive_buf == 0) {
634 printk(KERN_ERR "capi: ldisc has no receive_buf function\n");
635 return -1;
637 if (mp->ttyinstop) {
638 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
639 printk(KERN_DEBUG "capi: recv tty throttled\n");
640 #endif
641 return -1;
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");
647 #endif
648 return -1;
650 if ((nskb = gen_data_b3_resp_for(mp, skb)) == 0) {
651 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
652 return -1;
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",
658 errcode);
659 kfree_skb(nskb);
660 return -1;
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);
666 #endif
667 mp->tty->ldisc.receive_buf(mp->tty, skb->data, 0, skb->len);
668 kfree_skb(skb);
669 return 0;
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");
675 #endif
676 return -1;
678 if ((nskb = gen_data_b3_resp_for(mp, skb)) == 0) {
679 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
680 return -1;
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",
686 errcode);
687 kfree_skb(nskb);
688 return -1;
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);
694 #endif
695 skb_queue_tail(&mp->recvqueue, skb);
696 wake_up_interruptible(&mp->recvwait);
697 return 0;
699 #ifdef _DEBUG_DATAFLOW
700 printk(KERN_DEBUG "capi: currently no receiver\n");
701 #endif
702 return -1;
705 void handle_minor_recv(struct capiminor *mp)
707 struct sk_buff *skb;
708 while ((skb = skb_dequeue(&mp->inqueue)) != 0) {
709 unsigned int len = skb->len;
710 mp->inbytes -= len;
711 if (handle_recv_skb(mp, skb) < 0) {
712 skb_queue_head(&mp->inqueue, skb);
713 mp->inbytes += len;
714 return;
719 int handle_minor_send(struct capiminor *mp)
721 struct sk_buff *skb;
722 __u16 len;
723 int count = 0;
724 __u16 errcode;
725 __u16 datahandle;
727 if (mp->tty && mp->ttyoutstop) {
728 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
729 printk(KERN_DEBUG "capi: send: tty stopped\n");
730 #endif
731 return 0;
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);
753 return count;
755 errcode = (*capifuncs->capi_put_message) (mp->applid, skb);
756 if (errcode == CAPI_NOERROR) {
757 mp->datahandle++;
758 count++;
759 mp->outbytes -= len;
760 #ifdef _DEBUG_DATAFLOW
761 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
762 datahandle, len);
763 #endif
764 continue;
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);
771 break;
774 /* ups, drop packet */
775 printk(KERN_ERR "capi: put_message = %x\n", errcode);
776 mp->outbytes -= len;
777 kfree_skb(skb);
779 if (count)
780 wake_up_interruptible(&mp->sendwait);
781 return count;
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;
792 __u16 datahandle;
793 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
794 struct capincci *np;
795 struct sk_buff *skb = 0;
796 __u32 ncci;
798 (void) (*capifuncs->capi_get_message) (applid, &skb);
799 if (!skb) {
800 printk(KERN_ERR "BUG: capi_signal: no skb\n");
801 return;
804 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
805 skb_queue_tail(&cdev->recvqueue, skb);
806 wake_up_interruptible(&cdev->recvwait);
807 return;
809 ncci = CAPIMSG_CONTROL(skb->data);
810 for (np = cdev->nccis; np && np->ncci != ncci; np = np->next)
812 if (!np) {
813 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
814 skb_queue_tail(&cdev->recvqueue, skb);
815 wake_up_interruptible(&cdev->recvwait);
816 return;
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 */
822 mp = np->minorp;
823 if (!mp) {
824 skb_queue_tail(&cdev->recvqueue, skb);
825 wake_up_interruptible(&cdev->recvwait);
826 return;
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));
835 #endif
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",
845 datahandle,
846 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
847 #endif
848 kfree_skb(skb);
849 (void)capiminor_del_ack(mp, datahandle);
850 if (mp->tty) {
851 if (mp->tty->ldisc.write_wakeup)
852 mp->tty->ldisc.write_wakeup(mp->tty);
853 } else {
854 wake_up_interruptible(&mp->sendwait);
856 (void)handle_minor_send(mp);
858 } else {
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 ----------------------------- */
868 static loff_t
869 capi_llseek(struct file *file, loff_t offset, int origin)
871 return -ESPIPE;
874 static ssize_t
875 capi_read(struct file *file, char *buf, size_t count, loff_t *ppos)
877 struct capidev *cdev = (struct capidev *)file->private_data;
878 struct sk_buff *skb;
879 int retval;
880 size_t copied;
882 if (ppos != &file->f_pos)
883 return -ESPIPE;
885 if (!cdev->applid)
886 return -ENODEV;
888 if ((skb = skb_dequeue(&cdev->recvqueue)) == 0) {
890 if (file->f_flags & O_NONBLOCK)
891 return -EAGAIN;
893 for (;;) {
894 interruptible_sleep_on(&cdev->recvwait);
895 if ((skb = skb_dequeue(&cdev->recvqueue)) != 0)
896 break;
897 if (signal_pending(current))
898 break;
900 if (skb == 0)
901 return -ERESTARTNOHAND;
903 if (skb->len > count) {
904 skb_queue_head(&cdev->recvqueue, skb);
905 return -EMSGSIZE;
907 retval = copy_to_user(buf, skb->data, skb->len);
908 if (retval) {
909 skb_queue_head(&cdev->recvqueue, skb);
910 return retval;
912 copied = skb->len;
914 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_IND) {
915 cdev->nrecvdatapkt++;
916 } else {
917 cdev->nrecvctlpkt++;
920 kfree_skb(skb);
922 return copied;
925 static ssize_t
926 capi_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
928 struct capidev *cdev = (struct capidev *)file->private_data;
929 struct sk_buff *skb;
930 int retval;
931 __u16 mlen;
933 if (ppos != &file->f_pos)
934 return -ESPIPE;
936 if (!cdev->applid)
937 return -ENODEV;
939 skb = alloc_skb(count, GFP_USER);
941 if ((retval = copy_from_user(skb_put(skb, count), buf, count))) {
942 kfree_skb(skb);
943 return retval;
945 mlen = CAPIMSG_LEN(skb->data);
946 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
947 if (mlen + CAPIMSG_DATALEN(skb->data) != count) {
948 kfree_skb(skb);
949 return -EINVAL;
951 } else {
952 if (mlen != count) {
953 kfree_skb(skb);
954 return -EINVAL;
957 CAPIMSG_SETAPPID(skb->data, cdev->applid);
959 cdev->errcode = (*capifuncs->capi_put_message) (cdev->applid, skb);
961 if (cdev->errcode) {
962 kfree_skb(skb);
963 return -EIO;
965 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
966 cdev->nsentdatapkt++;
967 } else {
968 cdev->nsentctlpkt++;
970 return count;
973 static unsigned int
974 capi_poll(struct file *file, poll_table * wait)
976 struct capidev *cdev = (struct capidev *)file->private_data;
977 unsigned int mask = 0;
979 if (!cdev->applid)
980 return POLLERR;
982 poll_wait(file, &(cdev->recvwait), wait);
983 mask = POLLOUT | POLLWRNORM;
984 if (!skb_queue_empty(&cdev->recvqueue))
985 mask |= POLLIN | POLLRDNORM;
986 return mask;
989 static int
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;
997 switch (cmd) {
998 case CAPI_REGISTER:
1000 retval = copy_from_user((void *) &data.rparams,
1001 (void *) arg, sizeof(struct capi_register_params));
1002 if (retval)
1003 return -EFAULT;
1004 if (cdev->applid)
1005 return -EEXIST;
1006 cdev->errcode = (*capifuncs->capi_register) (&data.rparams,
1007 &cdev->applid);
1008 if (cdev->errcode) {
1009 cdev->applid = 0;
1010 return -EIO;
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,
1019 (void *) arg,
1020 sizeof(data.contr));
1021 if (retval)
1022 return -EFAULT;
1023 cdev->errcode = (*capifuncs->capi_get_version) (data.contr, &data.version);
1024 if (cdev->errcode)
1025 return -EIO;
1026 retval = copy_to_user((void *) arg,
1027 (void *) &data.version,
1028 sizeof(data.version));
1029 if (retval)
1030 return -EFAULT;
1032 return 0;
1034 case CAPI_GET_SERIAL:
1036 retval = copy_from_user((void *) &data.contr,
1037 (void *) arg,
1038 sizeof(data.contr));
1039 if (retval)
1040 return -EFAULT;
1041 cdev->errcode = (*capifuncs->capi_get_serial) (data.contr, data.serial);
1042 if (cdev->errcode)
1043 return -EIO;
1044 retval = copy_to_user((void *) arg,
1045 (void *) data.serial,
1046 sizeof(data.serial));
1047 if (retval)
1048 return -EFAULT;
1050 return 0;
1051 case CAPI_GET_PROFILE:
1053 retval = copy_from_user((void *) &data.contr,
1054 (void *) arg,
1055 sizeof(data.contr));
1056 if (retval)
1057 return -EFAULT;
1059 if (data.contr == 0) {
1060 cdev->errcode = (*capifuncs->capi_get_profile) (data.contr, &data.profile);
1061 if (cdev->errcode)
1062 return -EIO;
1064 retval = copy_to_user((void *) arg,
1065 (void *) &data.profile.ncontroller,
1066 sizeof(data.profile.ncontroller));
1068 } else {
1069 cdev->errcode = (*capifuncs->capi_get_profile) (data.contr, &data.profile);
1070 if (cdev->errcode)
1071 return -EIO;
1073 retval = copy_to_user((void *) arg,
1074 (void *) &data.profile,
1075 sizeof(data.profile));
1077 if (retval)
1078 return -EFAULT;
1080 return 0;
1082 case CAPI_GET_MANUFACTURER:
1084 retval = copy_from_user((void *) &data.contr,
1085 (void *) arg,
1086 sizeof(data.contr));
1087 if (retval)
1088 return -EFAULT;
1089 cdev->errcode = (*capifuncs->capi_get_manufacturer) (data.contr, data.manufacturer);
1090 if (cdev->errcode)
1091 return -EIO;
1093 retval = copy_to_user((void *) arg, (void *) data.manufacturer,
1094 sizeof(data.manufacturer));
1095 if (retval)
1096 return -EFAULT;
1099 return 0;
1100 case CAPI_GET_ERRCODE:
1101 data.errcode = cdev->errcode;
1102 cdev->errcode = CAPI_NOERROR;
1103 if (arg) {
1104 retval = copy_to_user((void *) arg,
1105 (void *) &data.errcode,
1106 sizeof(data.errcode));
1107 if (retval)
1108 return -EFAULT;
1110 return data.errcode;
1112 case CAPI_INSTALLED:
1113 if ((*capifuncs->capi_isinstalled)() == CAPI_NOERROR)
1114 return 0;
1115 return -ENXIO;
1117 case CAPI_MANUFACTURER_CMD:
1119 struct capi_manufacturer_cmd mcmd;
1120 if (!capable(CAP_SYS_ADMIN))
1121 return -EPERM;
1122 retval = copy_from_user((void *) &mcmd, (void *) arg,
1123 sizeof(mcmd));
1124 if (retval)
1125 return -EFAULT;
1126 return (*capifuncs->capi_manufacturer) (mcmd.cmd, mcmd.data);
1128 return 0;
1130 case CAPI_SET_FLAGS:
1131 case CAPI_CLR_FLAGS:
1133 unsigned userflags;
1134 retval = copy_from_user((void *) &userflags,
1135 (void *) arg,
1136 sizeof(userflags));
1137 if (retval)
1138 return -EFAULT;
1139 if (cmd == CAPI_SET_FLAGS)
1140 cdev->userflags |= userflags;
1141 else
1142 cdev->userflags &= ~userflags;
1144 return 0;
1146 case CAPI_GET_FLAGS:
1148 retval = copy_to_user((void *) arg,
1149 (void *) &cdev->userflags,
1150 sizeof(cdev->userflags));
1151 if (retval)
1152 return -EFAULT;
1154 return 0;
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 */
1162 unsigned ncci;
1163 int count = 0;
1164 retval = copy_from_user((void *) &ncci,
1165 (void *) arg,
1166 sizeof(ncci));
1167 if (retval)
1168 return -EFAULT;
1169 nccip = capincci_find(cdev, (__u32) ncci);
1170 if (!nccip)
1171 return 0;
1172 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1173 if ((mp = nccip->minorp) != 0) {
1174 count += atomic_read(&mp->ttyopencount);
1175 if (mp->file)
1176 count++;
1178 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1179 return count;
1181 return 0;
1183 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1184 case CAPI_NCCI_GETUNIT:
1186 struct capincci *nccip;
1187 struct capiminor *mp;
1188 unsigned ncci;
1189 retval = copy_from_user((void *) &ncci,
1190 (void *) arg,
1191 sizeof(ncci));
1192 if (retval)
1193 return -EFAULT;
1194 nccip = capincci_find(cdev, (__u32) ncci);
1195 if (!nccip || (mp = nccip->minorp) == 0)
1196 return -ESRCH;
1197 return mp->minor;
1199 return 0;
1200 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1202 return -EINVAL;
1205 static int
1206 capi_open(struct inode *inode, struct file *file)
1208 if (file->private_data)
1209 return -EEXIST;
1211 if ((file->private_data = capidev_alloc(file)) == 0)
1212 return -ENOMEM;
1214 MOD_INC_USE_COUNT;
1215 #ifdef _DEBUG_REFCOUNT
1216 printk(KERN_DEBUG "capi_open %d\n", GET_USE_COUNT(THIS_MODULE));
1217 #endif
1218 return 0;
1221 static int
1222 capi_release(struct inode *inode, struct file *file)
1224 struct capidev *cdev = (struct capidev *)file->private_data;
1226 capincci_free(cdev, 0xffffffff);
1227 capidev_free(cdev);
1228 file->private_data = NULL;
1230 MOD_DEC_USE_COUNT;
1231 #ifdef _DEBUG_REFCOUNT
1232 printk(KERN_DEBUG "capi_release %d\n", GET_USE_COUNT(THIS_MODULE));
1233 #endif
1234 return 0;
1237 static struct file_operations capi_fops =
1239 owner: THIS_MODULE,
1240 llseek: capi_llseek,
1241 read: capi_read,
1242 write: capi_write,
1243 poll: capi_poll,
1244 ioctl: capi_ioctl,
1245 open: capi_open,
1246 release: capi_release,
1249 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1250 /* -------- file_operations for capincci ---------------------------- */
1252 static int
1253 capinc_raw_open(struct inode *inode, struct file *file)
1255 struct capiminor *mp;
1257 if (file->private_data)
1258 return -EEXIST;
1259 if ((mp = capiminor_find(MINOR(file->f_dentry->d_inode->i_rdev))) == 0)
1260 return -ENXIO;
1261 if (mp->nccip == 0)
1262 return -ENXIO;
1263 if (mp->file)
1264 return -EBUSY;
1266 #ifdef _DEBUG_REFCOUNT
1267 printk(KERN_DEBUG "capi_raw_open %d\n", GET_USE_COUNT(THIS_MODULE));
1268 #endif
1270 mp->datahandle = 0;
1271 mp->file = file;
1272 file->private_data = (void *)mp;
1273 handle_minor_recv(mp);
1274 return 0;
1277 static loff_t
1278 capinc_raw_llseek(struct file *file, loff_t offset, int origin)
1280 return -ESPIPE;
1283 static ssize_t
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;
1288 int retval;
1289 size_t copied = 0;
1291 if (ppos != &file->f_pos)
1292 return -ESPIPE;
1294 if (!mp || !mp->nccip)
1295 return -EINVAL;
1297 if ((skb = skb_dequeue(&mp->recvqueue)) == 0) {
1299 if (file->f_flags & O_NONBLOCK)
1300 return -EAGAIN;
1302 for (;;) {
1303 interruptible_sleep_on(&mp->recvwait);
1304 if (mp->nccip == 0)
1305 return 0;
1306 if ((skb = skb_dequeue(&mp->recvqueue)) != 0)
1307 break;
1308 if (signal_pending(current))
1309 break;
1311 if (skb == 0)
1312 return -ERESTARTNOHAND;
1314 do {
1315 if (count < skb->len) {
1316 retval = copy_to_user(buf, skb->data, count);
1317 if (retval) {
1318 skb_queue_head(&mp->recvqueue, skb);
1319 return retval;
1321 skb_pull(skb, count);
1322 skb_queue_head(&mp->recvqueue, skb);
1323 copied += count;
1324 return copied;
1325 } else {
1326 retval = copy_to_user(buf, skb->data, skb->len);
1327 if (retval) {
1328 skb_queue_head(&mp->recvqueue, skb);
1329 return copied;
1331 copied += skb->len;
1332 count -= skb->len;
1333 buf += skb->len;
1334 kfree_skb(skb);
1336 } while ((skb = skb_dequeue(&mp->recvqueue)) != 0);
1338 return copied;
1341 static ssize_t
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;
1346 int retval;
1348 if (ppos != &file->f_pos)
1349 return -ESPIPE;
1351 if (!mp || !mp->nccip)
1352 return -EINVAL;
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))) {
1358 kfree_skb(skb);
1359 return retval;
1362 while (skb_queue_len(&mp->outqueue) > CAPINC_MAX_SENDQUEUE) {
1363 if (file->f_flags & O_NONBLOCK)
1364 return -EAGAIN;
1365 interruptible_sleep_on(&mp->sendwait);
1366 if (mp->nccip == 0) {
1367 kfree_skb(skb);
1368 return -EIO;
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);
1376 return count;
1379 static unsigned int
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;
1394 return mask;
1397 static int
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)
1403 return -EINVAL;
1405 switch (cmd) {
1407 return -EINVAL;
1410 static int
1411 capinc_raw_release(struct inode *inode, struct file *file)
1413 struct capiminor *mp = (struct capiminor *)file->private_data;
1415 if (mp) {
1416 mp->file = 0;
1417 if (mp->nccip == 0) {
1418 capiminor_free(mp);
1419 file->private_data = NULL;
1423 #ifdef _DEBUG_REFCOUNT
1424 printk(KERN_DEBUG "capinc_raw_release %d\n", GET_USE_COUNT(THIS_MODULE));
1425 #endif
1426 return 0;
1429 static struct file_operations capinc_raw_fops =
1431 owner: THIS_MODULE,
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)
1448 return -ENXIO;
1449 if (mp->nccip == 0)
1450 return -ENXIO;
1451 if (mp->file)
1452 return -EBUSY;
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));
1460 #endif
1461 if (atomic_read(&mp->ttyopencount) == 0)
1462 mp->tty = tty;
1463 atomic_inc(&mp->ttyopencount);
1464 #ifdef _DEBUG_REFCOUNT
1465 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1466 #endif
1467 handle_minor_recv(mp);
1468 return 0;
1471 void capinc_tty_close(struct tty_struct * tty, struct file * file)
1473 struct capiminor *mp;
1475 mp = (struct capiminor *)tty->driver_data;
1476 if (mp) {
1477 if (atomic_dec_and_test(&mp->ttyopencount)) {
1478 #ifdef _DEBUG_REFCOUNT
1479 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1480 #endif
1481 tty->driver_data = (void *)0;
1482 mp->tty = 0;
1484 #ifdef _DEBUG_REFCOUNT
1485 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1486 #endif
1487 if (mp->nccip == 0)
1488 capiminor_free(mp);
1491 #ifdef _DEBUG_REFCOUNT
1492 printk(KERN_DEBUG "capinc_tty_close\n");
1493 #endif
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;
1501 int retval;
1503 #ifdef _DEBUG_TTYFUNCS
1504 printk(KERN_DEBUG "capinc_tty_write(from_user=%d,count=%d)\n",
1505 from_user, count);
1506 #endif
1508 if (!mp || !mp->nccip) {
1509 #ifdef _DEBUG_TTYFUNCS
1510 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1511 #endif
1512 return 0;
1515 skb = mp->ttyskb;
1516 if (skb) {
1517 mp->ttyskb = 0;
1518 skb_queue_tail(&mp->outqueue, skb);
1519 mp->outbytes += skb->len;
1522 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1523 if (!skb) {
1524 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1525 return -ENOMEM;
1528 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1529 if (from_user) {
1530 if ((retval = copy_from_user(skb_put(skb, count), buf, count))) {
1531 kfree_skb(skb);
1532 #ifdef _DEBUG_TTYFUNCS
1533 printk(KERN_DEBUG "capinc_tty_write: copy_from_user=%d\n", retval);
1534 #endif
1535 return retval;
1537 } else {
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);
1545 return count;
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);
1555 #endif
1557 if (!mp || !mp->nccip) {
1558 #ifdef _DEBUG_TTYFUNCS
1559 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1560 #endif
1561 return;
1564 skb = mp->ttyskb;
1565 if (skb) {
1566 if (skb_tailroom(skb) > 0) {
1567 *(skb_put(skb, 1)) = ch;
1568 return;
1570 mp->ttyskb = 0;
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);
1576 if (skb) {
1577 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1578 *(skb_put(skb, 1)) = ch;
1579 mp->ttyskb = skb;
1580 } else {
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");
1592 #endif
1594 if (!mp || !mp->nccip) {
1595 #ifdef _DEBUG_TTYFUNCS
1596 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1597 #endif
1598 return;
1601 skb = mp->ttyskb;
1602 if (skb) {
1603 mp->ttyskb = 0;
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;
1614 int room;
1615 if (!mp || !mp->nccip) {
1616 #ifdef _DEBUG_TTYFUNCS
1617 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1618 #endif
1619 return 0;
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);
1625 #endif
1626 return 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");
1635 #endif
1636 return 0;
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));
1643 #endif
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");
1657 #endif
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");
1665 #endif
1666 if (mp)
1667 mp->ttyinstop = 1;
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");
1675 #endif
1676 if (mp) {
1677 mp->ttyinstop = 0;
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");
1687 #endif
1688 if (mp) {
1689 mp->ttyoutstop = 1;
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");
1698 #endif
1699 if (mp) {
1700 mp->ttyoutstop = 0;
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");
1709 #endif
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);
1716 #endif
1719 void capinc_tty_flush_buffer(struct tty_struct *tty)
1721 #ifdef _DEBUG_TTYFUNCS
1722 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1723 #endif
1726 void capinc_tty_set_ldisc(struct tty_struct *tty)
1728 #ifdef _DEBUG_TTYFUNCS
1729 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1730 #endif
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);
1737 #endif
1740 int capinc_tty_read_proc(char *page, char **start, off_t off,
1741 int count, int *eof, void *data)
1743 return 0;
1746 int capinc_write_proc(struct file *file, const char *buffer,
1747 unsigned long count, void *data)
1749 return 0;
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";
1769 #endif
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;
1803 #endif
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;
1809 #endif
1810 if (tty_register_driver(drv)) {
1811 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1812 return -1;
1814 return 0;
1817 void capinc_tty_exit(void)
1819 struct tty_driver *drv = &capinc_tty_driver;
1820 int retval;
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;
1837 int len = 0;
1838 off_t begin = 0;
1840 for (cdev=capidev_openlist; cdev; cdev = cdev->next) {
1841 len += sprintf(page+len, "%d %d %lu %lu %lu %lu\n",
1842 cdev->minor,
1843 cdev->applid,
1844 cdev->nrecvctlpkt,
1845 cdev->nrecvdatapkt,
1846 cdev->nsentctlpkt,
1847 cdev->nsentdatapkt);
1848 if (len+begin > off+count)
1849 goto endloop;
1850 if (len+begin < off) {
1851 begin += len;
1852 len = 0;
1855 endloop:
1856 if (cdev == 0)
1857 *eof = 1;
1858 if (off >= len+begin)
1859 return 0;
1860 *start = page + (off-begin);
1861 return ((count < begin+len-off) ? count : begin+len-off);
1865 * /proc/capi/capi20ncci:
1866 * applid ncci
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;
1873 int len = 0;
1874 off_t begin = 0;
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",
1879 cdev->applid,
1880 np->ncci,
1881 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
1882 "");
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)
1887 goto endloop;
1888 if (len+begin < off) {
1889 begin += len;
1890 len = 0;
1894 endloop:
1895 if (cdev == 0)
1896 *eof = 1;
1897 if (off >= len+begin)
1898 return 0;
1899 *start = page + (begin-off);
1900 return ((count < begin+len-off) ? count : begin+len-off);
1903 static struct procfsentries {
1904 char *name;
1905 mode_t mode;
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]);
1918 int i;
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]);
1930 int i;
1932 for (i=nelem-1; i >= 0; i--) {
1933 struct procfsentries *p = procfsentries + i;
1934 if (p->procent) {
1935 remove_proc_entry(p->name, 0);
1936 p->procent = 0;
1941 /* -------- init function and module interface ---------------------- */
1944 static void alloc_exit(void)
1946 if (capidev_cachep) {
1947 (void)kmem_cache_destroy(capidev_cachep);
1948 capidev_cachep = 0;
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);
1957 capidh_cachep = 0;
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),
1971 SLAB_HWCACHE_ALIGN,
1972 NULL, NULL);
1973 if (!capidev_cachep) {
1974 alloc_exit();
1975 return -ENOMEM;
1978 capincci_cachep = kmem_cache_create("capi20_ncci",
1979 sizeof(struct capincci),
1981 SLAB_HWCACHE_ALIGN,
1982 NULL, NULL);
1983 if (!capincci_cachep) {
1984 alloc_exit();
1985 return -ENOMEM;
1987 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1988 capidh_cachep = kmem_cache_create("capi20_dh",
1989 sizeof(struct datahandle_queue),
1991 SLAB_HWCACHE_ALIGN,
1992 NULL, NULL);
1993 if (!capidh_cachep) {
1994 alloc_exit();
1995 return -ENOMEM;
1997 capiminor_cachep = kmem_cache_create("capi20_minor",
1998 sizeof(struct capiminor),
2000 SLAB_HWCACHE_ALIGN,
2001 NULL, NULL);
2002 if (!capiminor_cachep) {
2003 alloc_exit();
2004 return -ENOMEM;
2006 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
2007 return 0;
2010 static void lower_callback(unsigned int cmd, __u32 contr, void *data)
2012 struct capi_ncciinfo *np;
2013 struct capidev *cdev;
2015 switch (cmd) {
2016 case KCI_CONTRUP:
2017 printk(KERN_INFO "capi: controller %hu up\n", contr);
2018 break;
2019 case KCI_CONTRDOWN:
2020 printk(KERN_INFO "capi: controller %hu down\n", contr);
2021 break;
2022 case KCI_NCCIUP:
2023 np = (struct capi_ncciinfo *)data;
2024 if ((cdev = capidev_find(np->applid)) == 0)
2025 return;
2026 (void)capincci_alloc(cdev, np->ncci);
2027 break;
2028 case KCI_NCCIDOWN:
2029 np = (struct capi_ncciinfo *)data;
2030 if ((cdev = capidev_find(np->applid)) == 0)
2031 return;
2032 (void)capincci_free(cdev, np->ncci);
2033 break;
2037 #ifdef MODULE
2038 #define capi_init init_module
2039 #endif
2041 static struct capi_interface_user cuser = {
2042 "capi20",
2043 lower_callback,
2046 static char rev[10];
2048 int capi_init(void)
2050 char *p;
2052 MOD_INC_USE_COUNT;
2054 if ((p = strchr(revision, ':'))) {
2055 strcpy(rev, p + 2);
2056 p = strchr(rev, '$');
2057 *(p-1) = 0;
2058 } else
2059 strcpy(rev, "???");
2061 if (devfs_register_chrdev(capi_major, "capi20", &capi_fops)) {
2062 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
2063 MOD_DEC_USE_COUNT;
2064 return -EIO;
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);
2071 MOD_DEC_USE_COUNT;
2072 return -EIO;
2074 devfs_register_series (NULL, "capi/r%u", CAPINC_NR_PORTS,
2075 DEVFS_FL_DEFAULT,
2076 capi_rawmajor, 0,
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,
2082 &capi_fops, NULL);
2083 printk(KERN_NOTICE "capi20: started up with major %d\n", capi_major);
2085 if ((capifuncs = attach_capi_interface(&cuser)) == 0) {
2087 MOD_DEC_USE_COUNT;
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",
2093 capi_major, 0,
2094 DEVFS_SPECIAL_CHR, 0));
2095 return -EIO;
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");
2103 MOD_DEC_USE_COUNT;
2104 return -ENOMEM;
2106 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
2108 if (alloc_init() < 0) {
2109 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
2110 unsigned int j;
2111 devfs_unregister_chrdev(capi_rawmajor, "capi/r%d");
2112 for (j = 0; j < CAPINC_NR_PORTS; j++) {
2113 char devname[32];
2114 sprintf(devname, "capi/r%u", j);
2115 devfs_unregister(devfs_find_handle(NULL, devname, capi_rawmajor, j, DEVFS_SPECIAL_CHR, 0));
2117 capinc_tty_exit();
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",
2122 capi_major, 0,
2123 DEVFS_SPECIAL_CHR, 0));
2124 MOD_DEC_USE_COUNT;
2125 return -ENOMEM;
2128 (void)proc_init();
2130 printk(KERN_NOTICE "capi20: Rev%s: started up with major %d\n",
2131 rev, capi_major);
2133 MOD_DEC_USE_COUNT;
2134 return 0;
2137 #ifdef MODULE
2138 void cleanup_module(void)
2140 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
2141 unsigned int j;
2142 #endif
2143 alloc_exit();
2144 (void)proc_exit();
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
2150 capinc_tty_exit();
2151 devfs_unregister_chrdev(capi_rawmajor, "capi/r%d");
2152 for (j = 0; j < CAPINC_NR_PORTS; j++) {
2153 char devname[32];
2154 sprintf(devname, "capi/r%u", j);
2155 devfs_unregister(devfs_find_handle(NULL, devname, capi_rawmajor, j, DEVFS_SPECIAL_CHR, 0));
2157 #endif
2158 (void) detach_capi_interface(&cuser);
2159 printk(KERN_NOTICE "capi: Rev%s: unloaded\n", rev);
2162 #endif