vserver 1.9.3
[linux-2.6.git] / drivers / isdn / capi / capidrv.c
1 /* $Id: capidrv.c,v 1.1.2.2 2004/01/12 23:17:24 keil Exp $
2  *
3  * ISDN4Linux Driver, using capi20 interface (kernelcapi)
4  *
5  * Copyright 1997 by Carsten Paeth <calle@calle.de>
6  *
7  * This software may be used and distributed according to the terms
8  * of the GNU General Public License, incorporated herein by reference.
9  *
10  */
11
12 #include <linux/module.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/major.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/fcntl.h>
19 #include <linux/fs.h>
20 #include <linux/signal.h>
21 #include <linux/mm.h>
22 #include <linux/timer.h>
23 #include <linux/wait.h>
24 #include <linux/skbuff.h>
25 #include <linux/isdn.h>
26 #include <linux/isdnif.h>
27 #include <linux/proc_fs.h>
28 #include <linux/capi.h>
29 #include <linux/kernelcapi.h>
30 #include <linux/ctype.h>
31 #include <linux/init.h>
32
33 #include <linux/isdn/capiutil.h>
34 #include <linux/isdn/capicmd.h>
35 #include "capidrv.h"
36
37 static char *revision = "$Revision: 1.1.2.2 $";
38 static int debugmode = 0;
39
40 MODULE_DESCRIPTION("CAPI4Linux: Interface to ISDN4Linux");
41 MODULE_AUTHOR("Carsten Paeth");
42 MODULE_LICENSE("GPL");
43 MODULE_PARM(debugmode, "i");
44
45 /* -------- type definitions ----------------------------------------- */
46
47
48 struct capidrv_contr {
49
50         struct capidrv_contr *next;
51         struct module *owner;
52         u32 contrnr;
53         char name[20];
54
55         /*
56          * for isdn4linux
57          */
58         isdn_if interface;
59         int myid;
60
61         /*
62          * LISTEN state
63          */
64         int state;
65         u32 cipmask;
66         u32 cipmask2;
67         struct timer_list listentimer;
68
69         /*
70          * ID of capi message sent
71          */
72         u16 msgid;
73
74         /*
75          * B-Channels
76          */
77         int nbchan;
78         struct capidrv_bchan {
79                 struct capidrv_contr *contr;
80                 u8 msn[ISDN_MSNLEN];
81                 int l2;
82                 int l3;
83                 u8 num[ISDN_MSNLEN];
84                 u8 mynum[ISDN_MSNLEN];
85                 int si1;
86                 int si2;
87                 int incoming;
88                 int disconnecting;
89                 struct capidrv_plci {
90                         struct capidrv_plci *next;
91                         u32 plci;
92                         u32 ncci;       /* ncci for CONNECT_ACTIVE_IND */
93                         u16 msgid;      /* to identfy CONNECT_CONF */
94                         int chan;
95                         int state;
96                         int leasedline;
97                         struct capidrv_ncci {
98                                 struct capidrv_ncci *next;
99                                 struct capidrv_plci *plcip;
100                                 u32 ncci;
101                                 u16 msgid;      /* to identfy CONNECT_B3_CONF */
102                                 int chan;
103                                 int state;
104                                 int oldstate;
105                                 /* */
106                                 u16 datahandle;
107                                 struct ncci_datahandle_queue {
108                                     struct ncci_datahandle_queue *next;
109                                     u16                         datahandle;
110                                     int                           len;
111                                 } *ackqueue;
112                         } *ncci_list;
113                 } *plcip;
114                 struct capidrv_ncci *nccip;
115         } *bchans;
116
117         struct capidrv_plci *plci_list;
118
119         /* for q931 data */
120         u8  q931_buf[4096];
121         u8 *q931_read;
122         u8 *q931_write;
123         u8 *q931_end;
124 };
125
126
127 struct capidrv_data {
128         struct capi20_appl ap;
129         int ncontr;
130         struct capidrv_contr *contr_list;
131 };
132
133 typedef struct capidrv_plci capidrv_plci;
134 typedef struct capidrv_ncci capidrv_ncci;
135 typedef struct capidrv_contr capidrv_contr;
136 typedef struct capidrv_data capidrv_data;
137 typedef struct capidrv_bchan capidrv_bchan;
138
139 /* -------- data definitions ----------------------------------------- */
140
141 static capidrv_data global;
142 static spinlock_t global_lock = SPIN_LOCK_UNLOCKED;
143
144 static void handle_dtrace_data(capidrv_contr *card,
145         int send, int level2, u8 *data, u16 len);
146
147 /* -------- convert functions ---------------------------------------- */
148
149 static inline u32 b1prot(int l2, int l3)
150 {
151         switch (l2) {
152         case ISDN_PROTO_L2_X75I:
153         case ISDN_PROTO_L2_X75UI:
154         case ISDN_PROTO_L2_X75BUI:
155                 return 0;
156         case ISDN_PROTO_L2_HDLC:
157         default:
158                 return 0;
159         case ISDN_PROTO_L2_TRANS:
160                 return 1;
161         case ISDN_PROTO_L2_V11096:
162         case ISDN_PROTO_L2_V11019:
163         case ISDN_PROTO_L2_V11038:
164                 return 2;
165         case ISDN_PROTO_L2_FAX:
166                 return 4;
167         case ISDN_PROTO_L2_MODEM:
168                 return 8;
169         }
170 }
171
172 static inline u32 b2prot(int l2, int l3)
173 {
174         switch (l2) {
175         case ISDN_PROTO_L2_X75I:
176         case ISDN_PROTO_L2_X75UI:
177         case ISDN_PROTO_L2_X75BUI:
178         default:
179                 return 0;
180         case ISDN_PROTO_L2_HDLC:
181         case ISDN_PROTO_L2_TRANS:
182         case ISDN_PROTO_L2_V11096:
183         case ISDN_PROTO_L2_V11019:
184         case ISDN_PROTO_L2_V11038:
185         case ISDN_PROTO_L2_MODEM:
186                 return 1;
187         case ISDN_PROTO_L2_FAX:
188                 return 4;
189         }
190 }
191
192 static inline u32 b3prot(int l2, int l3)
193 {
194         switch (l2) {
195         case ISDN_PROTO_L2_X75I:
196         case ISDN_PROTO_L2_X75UI:
197         case ISDN_PROTO_L2_X75BUI:
198         case ISDN_PROTO_L2_HDLC:
199         case ISDN_PROTO_L2_TRANS:
200         case ISDN_PROTO_L2_V11096:
201         case ISDN_PROTO_L2_V11019:
202         case ISDN_PROTO_L2_V11038:
203         case ISDN_PROTO_L2_MODEM:
204         default:
205                 return 0;
206         case ISDN_PROTO_L2_FAX:
207                 return 4;
208         }
209 }
210
211 static _cstruct b1config_async_v110(u16 rate)
212 {
213         /* CAPI-Spec "B1 Configuration" */
214         static unsigned char buf[9];
215         buf[0] = 8; /* len */
216         /* maximum bitrate */
217         buf[1] = rate & 0xff; buf[2] = (rate >> 8) & 0xff;
218         buf[3] = 8; buf[4] = 0; /* 8 bits per character */
219         buf[5] = 0; buf[6] = 0; /* parity none */
220         buf[7] = 0; buf[8] = 0; /* 1 stop bit */
221         return buf;
222 }
223
224 static _cstruct b1config(int l2, int l3)
225 {
226         switch (l2) {
227         case ISDN_PROTO_L2_X75I:
228         case ISDN_PROTO_L2_X75UI:
229         case ISDN_PROTO_L2_X75BUI:
230         case ISDN_PROTO_L2_HDLC:
231         case ISDN_PROTO_L2_TRANS:
232         default:
233                 return NULL;
234         case ISDN_PROTO_L2_V11096:
235             return b1config_async_v110(9600);
236         case ISDN_PROTO_L2_V11019:
237             return b1config_async_v110(19200);
238         case ISDN_PROTO_L2_V11038:
239             return b1config_async_v110(38400);
240         }
241 }
242
243 static inline u16 si2cip(u8 si1, u8 si2)
244 {
245         static const u8 cip[17][5] =
246         {
247         /*  0  1  2  3  4  */
248                 {0, 0, 0, 0, 0},        /*0 */
249                 {16, 16, 4, 26, 16},    /*1 */
250                 {17, 17, 17, 4, 4},     /*2 */
251                 {2, 2, 2, 2, 2},        /*3 */
252                 {18, 18, 18, 18, 18},   /*4 */
253                 {2, 2, 2, 2, 2},        /*5 */
254                 {0, 0, 0, 0, 0},        /*6 */
255                 {2, 2, 2, 2, 2},        /*7 */
256                 {2, 2, 2, 2, 2},        /*8 */
257                 {21, 21, 21, 21, 21},   /*9 */
258                 {19, 19, 19, 19, 19},   /*10 */
259                 {0, 0, 0, 0, 0},        /*11 */
260                 {0, 0, 0, 0, 0},        /*12 */
261                 {0, 0, 0, 0, 0},        /*13 */
262                 {0, 0, 0, 0, 0},        /*14 */
263                 {22, 22, 22, 22, 22},   /*15 */
264                 {27, 27, 27, 28, 27}    /*16 */
265         };
266         if (si1 > 16)
267                 si1 = 0;
268         if (si2 > 4)
269                 si2 = 0;
270
271         return (u16) cip[si1][si2];
272 }
273
274 static inline u8 cip2si1(u16 cipval)
275 {
276         static const u8 si[32] =
277         {7, 1, 7, 7, 1, 1, 7, 7,        /*0-7 */
278          7, 1, 0, 0, 0, 0, 0, 0,        /*8-15 */
279          1, 2, 4, 10, 9, 9, 15, 7,      /*16-23 */
280          7, 7, 1, 16, 16, 0, 0, 0};     /*24-31 */
281
282         if (cipval > 31)
283                 cipval = 0;     /* .... */
284         return si[cipval];
285 }
286
287 static inline u8 cip2si2(u16 cipval)
288 {
289         static const u8 si[32] =
290         {0, 0, 0, 0, 2, 3, 0, 0,        /*0-7 */
291          0, 3, 0, 0, 0, 0, 0, 0,        /*8-15 */
292          1, 2, 0, 0, 9, 0, 0, 0,        /*16-23 */
293          0, 0, 3, 2, 3, 0, 0, 0};       /*24-31 */
294
295         if (cipval > 31)
296                 cipval = 0;     /* .... */
297         return si[cipval];
298 }
299
300
301 /* -------- controller management ------------------------------------- */
302
303 static inline capidrv_contr *findcontrbydriverid(int driverid)
304 {
305         unsigned long flags;
306         capidrv_contr *p;
307
308         spin_lock_irqsave(&global_lock, flags);
309         for (p = global.contr_list; p; p = p->next)
310                 if (p->myid == driverid)
311                         break;
312         spin_unlock_irqrestore(&global_lock, flags);
313         return p;
314 }
315
316 static capidrv_contr *findcontrbynumber(u32 contr)
317 {
318         unsigned long flags;
319         capidrv_contr *p = global.contr_list;
320
321         spin_lock_irqsave(&global_lock, flags);
322         for (p = global.contr_list; p; p = p->next)
323                 if (p->contrnr == contr)
324                         break;
325         spin_unlock_irqrestore(&global_lock, flags);
326         return p;
327 }
328
329
330 /* -------- plci management ------------------------------------------ */
331
332 static capidrv_plci *new_plci(capidrv_contr * card, int chan)
333 {
334         capidrv_plci *plcip;
335
336         plcip = (capidrv_plci *) kmalloc(sizeof(capidrv_plci), GFP_ATOMIC);
337
338         if (plcip == 0)
339                 return NULL;
340
341         memset(plcip, 0, sizeof(capidrv_plci));
342         plcip->state = ST_PLCI_NONE;
343         plcip->plci = 0;
344         plcip->msgid = 0;
345         plcip->chan = chan;
346         plcip->next = card->plci_list;
347         card->plci_list = plcip;
348         card->bchans[chan].plcip = plcip;
349
350         return plcip;
351 }
352
353 static capidrv_plci *find_plci_by_plci(capidrv_contr * card, u32 plci)
354 {
355         capidrv_plci *p;
356         for (p = card->plci_list; p; p = p->next)
357                 if (p->plci == plci)
358                         return p;
359         return NULL;
360 }
361
362 static capidrv_plci *find_plci_by_msgid(capidrv_contr * card, u16 msgid)
363 {
364         capidrv_plci *p;
365         for (p = card->plci_list; p; p = p->next)
366                 if (p->msgid == msgid)
367                         return p;
368         return NULL;
369 }
370
371 static capidrv_plci *find_plci_by_ncci(capidrv_contr * card, u32 ncci)
372 {
373         capidrv_plci *p;
374         for (p = card->plci_list; p; p = p->next)
375                 if (p->plci == (ncci & 0xffff))
376                         return p;
377         return NULL;
378 }
379
380 static void free_plci(capidrv_contr * card, capidrv_plci * plcip)
381 {
382         capidrv_plci **pp;
383
384         for (pp = &card->plci_list; *pp; pp = &(*pp)->next) {
385                 if (*pp == plcip) {
386                         *pp = (*pp)->next;
387                         card->bchans[plcip->chan].plcip = NULL;
388                         card->bchans[plcip->chan].disconnecting = 0;
389                         card->bchans[plcip->chan].incoming = 0;
390                         kfree(plcip);
391                         return;
392                 }
393         }
394         printk(KERN_ERR "capidrv-%d: free_plci %p (0x%x) not found, Huh?\n",
395                card->contrnr, plcip, plcip->plci);
396 }
397
398 /* -------- ncci management ------------------------------------------ */
399
400 static inline capidrv_ncci *new_ncci(capidrv_contr * card,
401                                      capidrv_plci * plcip,
402                                      u32 ncci)
403 {
404         capidrv_ncci *nccip;
405
406         nccip = (capidrv_ncci *) kmalloc(sizeof(capidrv_ncci), GFP_ATOMIC);
407
408         if (nccip == 0)
409                 return NULL;
410
411         memset(nccip, 0, sizeof(capidrv_ncci));
412         nccip->ncci = ncci;
413         nccip->state = ST_NCCI_NONE;
414         nccip->plcip = plcip;
415         nccip->chan = plcip->chan;
416         nccip->datahandle = 0;
417
418         nccip->next = plcip->ncci_list;
419         plcip->ncci_list = nccip;
420
421         card->bchans[plcip->chan].nccip = nccip;
422
423         return nccip;
424 }
425
426 static inline capidrv_ncci *find_ncci(capidrv_contr * card, u32 ncci)
427 {
428         capidrv_plci *plcip;
429         capidrv_ncci *p;
430
431         if ((plcip = find_plci_by_ncci(card, ncci)) == 0)
432                 return NULL;
433
434         for (p = plcip->ncci_list; p; p = p->next)
435                 if (p->ncci == ncci)
436                         return p;
437         return NULL;
438 }
439
440 static inline capidrv_ncci *find_ncci_by_msgid(capidrv_contr * card,
441                                                u32 ncci, u16 msgid)
442 {
443         capidrv_plci *plcip;
444         capidrv_ncci *p;
445
446         if ((plcip = find_plci_by_ncci(card, ncci)) == 0)
447                 return NULL;
448
449         for (p = plcip->ncci_list; p; p = p->next)
450                 if (p->msgid == msgid)
451                         return p;
452         return NULL;
453 }
454
455 static void free_ncci(capidrv_contr * card, struct capidrv_ncci *nccip)
456 {
457         struct capidrv_ncci **pp;
458
459         for (pp = &(nccip->plcip->ncci_list); *pp; pp = &(*pp)->next) {
460                 if (*pp == nccip) {
461                         *pp = (*pp)->next;
462                         break;
463                 }
464         }
465         card->bchans[nccip->chan].nccip = NULL;
466         kfree(nccip);
467 }
468
469 static int capidrv_add_ack(struct capidrv_ncci *nccip,
470                            u16 datahandle, int len)
471 {
472         struct ncci_datahandle_queue *n, **pp;
473
474         n = (struct ncci_datahandle_queue *)
475                 kmalloc(sizeof(struct ncci_datahandle_queue), GFP_ATOMIC);
476         if (!n) {
477            printk(KERN_ERR "capidrv: kmalloc ncci_datahandle failed\n");
478            return -1;
479         }
480         n->next = NULL;
481         n->datahandle = datahandle;
482         n->len = len;
483         for (pp = &nccip->ackqueue; *pp; pp = &(*pp)->next) ;
484         *pp = n;
485         return 0;
486 }
487
488 static int capidrv_del_ack(struct capidrv_ncci *nccip, u16 datahandle)
489 {
490         struct ncci_datahandle_queue **pp, *p;
491         int len;
492
493         for (pp = &nccip->ackqueue; *pp; pp = &(*pp)->next) {
494                 if ((*pp)->datahandle == datahandle) {
495                         p = *pp;
496                         len = p->len;
497                         *pp = (*pp)->next;
498                         kfree(p);
499                         return len;
500                 }
501         }
502         return -1;
503 }
504
505 /* -------- convert and send capi message ---------------------------- */
506
507 static void send_message(capidrv_contr * card, _cmsg * cmsg)
508 {
509         struct sk_buff *skb;
510         size_t len;
511         capi_cmsg2message(cmsg, cmsg->buf);
512         len = CAPIMSG_LEN(cmsg->buf);
513         skb = alloc_skb(len, GFP_ATOMIC);
514         memcpy(skb_put(skb, len), cmsg->buf, len);
515         if (capi20_put_message(&global.ap, skb) != CAPI_NOERROR)
516                 kfree_skb(skb);
517 }
518
519 /* -------- state machine -------------------------------------------- */
520
521 struct listenstatechange {
522         int actstate;
523         int nextstate;
524         int event;
525 };
526
527 static struct listenstatechange listentable[] =
528 {
529   {ST_LISTEN_NONE, ST_LISTEN_WAIT_CONF, EV_LISTEN_REQ},
530   {ST_LISTEN_ACTIVE, ST_LISTEN_ACTIVE_WAIT_CONF, EV_LISTEN_REQ},
531   {ST_LISTEN_WAIT_CONF, ST_LISTEN_NONE, EV_LISTEN_CONF_ERROR},
532   {ST_LISTEN_ACTIVE_WAIT_CONF, ST_LISTEN_ACTIVE, EV_LISTEN_CONF_ERROR},
533   {ST_LISTEN_WAIT_CONF, ST_LISTEN_NONE, EV_LISTEN_CONF_EMPTY},
534   {ST_LISTEN_ACTIVE_WAIT_CONF, ST_LISTEN_NONE, EV_LISTEN_CONF_EMPTY},
535   {ST_LISTEN_WAIT_CONF, ST_LISTEN_ACTIVE, EV_LISTEN_CONF_OK},
536   {ST_LISTEN_ACTIVE_WAIT_CONF, ST_LISTEN_ACTIVE, EV_LISTEN_CONF_OK},
537   {},
538 };
539
540 static void listen_change_state(capidrv_contr * card, int event)
541 {
542         struct listenstatechange *p = listentable;
543         while (p->event) {
544                 if (card->state == p->actstate && p->event == event) {
545                         if (debugmode)
546                                 printk(KERN_DEBUG "capidrv-%d: listen_change_state %d -> %d\n",
547                                        card->contrnr, card->state, p->nextstate);
548                         card->state = p->nextstate;
549                         return;
550                 }
551                 p++;
552         }
553         printk(KERN_ERR "capidrv-%d: listen_change_state state=%d event=%d ????\n",
554                card->contrnr, card->state, event);
555
556 }
557
558 /* ------------------------------------------------------------------ */
559
560 static void p0(capidrv_contr * card, capidrv_plci * plci)
561 {
562         isdn_ctrl cmd;
563
564         card->bchans[plci->chan].contr = NULL;
565         cmd.command = ISDN_STAT_DHUP;
566         cmd.driver = card->myid;
567         cmd.arg = plci->chan;
568         card->interface.statcallb(&cmd);
569         free_plci(card, plci);
570 }
571
572 /* ------------------------------------------------------------------ */
573
574 struct plcistatechange {
575         int actstate;
576         int nextstate;
577         int event;
578         void (*changefunc) (capidrv_contr * card, capidrv_plci * plci);
579 };
580
581 static struct plcistatechange plcitable[] =
582 {
583   /* P-0 */
584   {ST_PLCI_NONE, ST_PLCI_OUTGOING, EV_PLCI_CONNECT_REQ, NULL},
585   {ST_PLCI_NONE, ST_PLCI_ALLOCATED, EV_PLCI_FACILITY_IND_UP, NULL},
586   {ST_PLCI_NONE, ST_PLCI_INCOMING, EV_PLCI_CONNECT_IND, NULL},
587   {ST_PLCI_NONE, ST_PLCI_RESUMEING, EV_PLCI_RESUME_REQ, NULL},
588   /* P-0.1 */
589   {ST_PLCI_OUTGOING, ST_PLCI_NONE, EV_PLCI_CONNECT_CONF_ERROR, p0},
590   {ST_PLCI_OUTGOING, ST_PLCI_ALLOCATED, EV_PLCI_CONNECT_CONF_OK, NULL},
591   /* P-1 */
592   {ST_PLCI_ALLOCATED, ST_PLCI_ACTIVE, EV_PLCI_CONNECT_ACTIVE_IND, NULL},
593   {ST_PLCI_ALLOCATED, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
594   {ST_PLCI_ALLOCATED, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
595   {ST_PLCI_ALLOCATED, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
596   /* P-ACT */
597   {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
598   {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
599   {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
600   {ST_PLCI_ACTIVE, ST_PLCI_HELD, EV_PLCI_HOLD_IND, NULL},
601   {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTING, EV_PLCI_SUSPEND_IND, NULL},
602   /* P-2 */
603   {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_CONNECT_REJECT, NULL},
604   {ST_PLCI_INCOMING, ST_PLCI_FACILITY_IND, EV_PLCI_FACILITY_IND_UP, NULL},
605   {ST_PLCI_INCOMING, ST_PLCI_ACCEPTING, EV_PLCI_CONNECT_RESP, NULL},
606   {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
607   {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
608   {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
609   {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_CD_IND, NULL},
610   /* P-3 */
611   {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTING, EV_PLCI_CONNECT_REJECT, NULL},
612   {ST_PLCI_FACILITY_IND, ST_PLCI_ACCEPTING, EV_PLCI_CONNECT_ACTIVE_IND, NULL},
613   {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
614   {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
615   {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
616   /* P-4 */
617   {ST_PLCI_ACCEPTING, ST_PLCI_ACTIVE, EV_PLCI_CONNECT_ACTIVE_IND, NULL},
618   {ST_PLCI_ACCEPTING, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
619   {ST_PLCI_ACCEPTING, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
620   {ST_PLCI_ACCEPTING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
621   /* P-5 */
622   {ST_PLCI_DISCONNECTING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
623   /* P-6 */
624   {ST_PLCI_DISCONNECTED, ST_PLCI_NONE, EV_PLCI_DISCONNECT_RESP, p0},
625   /* P-0.Res */
626   {ST_PLCI_RESUMEING, ST_PLCI_NONE, EV_PLCI_RESUME_CONF_ERROR, p0},
627   {ST_PLCI_RESUMEING, ST_PLCI_RESUME, EV_PLCI_RESUME_CONF_OK, NULL},
628   /* P-RES */
629   {ST_PLCI_RESUME, ST_PLCI_ACTIVE, EV_PLCI_RESUME_IND, NULL},
630   /* P-HELD */
631   {ST_PLCI_HELD, ST_PLCI_ACTIVE, EV_PLCI_RETRIEVE_IND, NULL},
632   {},
633 };
634
635 static void plci_change_state(capidrv_contr * card, capidrv_plci * plci, int event)
636 {
637         struct plcistatechange *p = plcitable;
638         while (p->event) {
639                 if (plci->state == p->actstate && p->event == event) {
640                         if (debugmode)
641                                 printk(KERN_DEBUG "capidrv-%d: plci_change_state:0x%x %d -> %d\n",
642                                   card->contrnr, plci->plci, plci->state, p->nextstate);
643                         plci->state = p->nextstate;
644                         if (p->changefunc)
645                                 p->changefunc(card, plci);
646                         return;
647                 }
648                 p++;
649         }
650         printk(KERN_ERR "capidrv-%d: plci_change_state:0x%x state=%d event=%d ????\n",
651                card->contrnr, plci->plci, plci->state, event);
652 }
653
654 /* ------------------------------------------------------------------ */
655
656 static _cmsg cmsg;
657
658 static void n0(capidrv_contr * card, capidrv_ncci * ncci)
659 {
660         isdn_ctrl cmd;
661
662         capi_fill_DISCONNECT_REQ(&cmsg,
663                                  global.ap.applid,
664                                  card->msgid++,
665                                  ncci->plcip->plci,
666                                  NULL,  /* BChannelinformation */
667                                  NULL,  /* Keypadfacility */
668                                  NULL,  /* Useruserdata */   /* $$$$ */
669                                  NULL   /* Facilitydataarray */
670         );
671         send_message(card, &cmsg);
672         plci_change_state(card, ncci->plcip, EV_PLCI_DISCONNECT_REQ);
673
674         cmd.command = ISDN_STAT_BHUP;
675         cmd.driver = card->myid;
676         cmd.arg = ncci->chan;
677         card->interface.statcallb(&cmd);
678         free_ncci(card, ncci);
679 }
680
681 /* ------------------------------------------------------------------ */
682
683 struct nccistatechange {
684         int actstate;
685         int nextstate;
686         int event;
687         void (*changefunc) (capidrv_contr * card, capidrv_ncci * ncci);
688 };
689
690 static struct nccistatechange nccitable[] =
691 {
692   /* N-0 */
693   {ST_NCCI_NONE, ST_NCCI_OUTGOING, EV_NCCI_CONNECT_B3_REQ, NULL},
694   {ST_NCCI_NONE, ST_NCCI_INCOMING, EV_NCCI_CONNECT_B3_IND, NULL},
695   /* N-0.1 */
696   {ST_NCCI_OUTGOING, ST_NCCI_ALLOCATED, EV_NCCI_CONNECT_B3_CONF_OK, NULL},
697   {ST_NCCI_OUTGOING, ST_NCCI_NONE, EV_NCCI_CONNECT_B3_CONF_ERROR, n0},
698   /* N-1 */
699   {ST_NCCI_INCOMING, ST_NCCI_DISCONNECTING, EV_NCCI_CONNECT_B3_REJECT, NULL},
700   {ST_NCCI_INCOMING, ST_NCCI_ALLOCATED, EV_NCCI_CONNECT_B3_RESP, NULL},
701   {ST_NCCI_INCOMING, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
702   {ST_NCCI_INCOMING, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, NULL},
703   /* N-2 */
704   {ST_NCCI_ALLOCATED, ST_NCCI_ACTIVE, EV_NCCI_CONNECT_B3_ACTIVE_IND, NULL},
705   {ST_NCCI_ALLOCATED, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
706   {ST_NCCI_ALLOCATED, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, NULL},
707   /* N-ACT */
708   {ST_NCCI_ACTIVE, ST_NCCI_ACTIVE, EV_NCCI_RESET_B3_IND, NULL},
709   {ST_NCCI_ACTIVE, ST_NCCI_RESETING, EV_NCCI_RESET_B3_REQ, NULL},
710   {ST_NCCI_ACTIVE, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
711   {ST_NCCI_ACTIVE, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, NULL},
712   /* N-3 */
713   {ST_NCCI_RESETING, ST_NCCI_ACTIVE, EV_NCCI_RESET_B3_IND, NULL},
714   {ST_NCCI_RESETING, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
715   {ST_NCCI_RESETING, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, NULL},
716   /* N-4 */
717   {ST_NCCI_DISCONNECTING, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
718   {ST_NCCI_DISCONNECTING, ST_NCCI_PREVIOUS, EV_NCCI_DISCONNECT_B3_CONF_ERROR,NULL},
719   /* N-5 */
720   {ST_NCCI_DISCONNECTED, ST_NCCI_NONE, EV_NCCI_DISCONNECT_B3_RESP, n0},
721   {},
722 };
723
724 static void ncci_change_state(capidrv_contr * card, capidrv_ncci * ncci, int event)
725 {
726         struct nccistatechange *p = nccitable;
727         while (p->event) {
728                 if (ncci->state == p->actstate && p->event == event) {
729                         if (debugmode)
730                                 printk(KERN_DEBUG "capidrv-%d: ncci_change_state:0x%x %d -> %d\n",
731                                   card->contrnr, ncci->ncci, ncci->state, p->nextstate);
732                         if (p->nextstate == ST_NCCI_PREVIOUS) {
733                                 ncci->state = ncci->oldstate;
734                                 ncci->oldstate = p->actstate;
735                         } else {
736                                 ncci->oldstate = p->actstate;
737                                 ncci->state = p->nextstate;
738                         }
739                         if (p->changefunc)
740                                 p->changefunc(card, ncci);
741                         return;
742                 }
743                 p++;
744         }
745         printk(KERN_ERR "capidrv-%d: ncci_change_state:0x%x state=%d event=%d ????\n",
746                card->contrnr, ncci->ncci, ncci->state, event);
747 }
748
749 /* ------------------------------------------------------------------- */
750
751 static inline int new_bchan(capidrv_contr * card)
752 {
753         int i;
754         for (i = 0; i < card->nbchan; i++) {
755                 if (card->bchans[i].plcip == 0) {
756                         card->bchans[i].disconnecting = 0;
757                         return i;
758                 }
759         }
760         return -1;
761 }
762
763 /* ------------------------------------------------------------------- */
764
765 static void handle_controller(_cmsg * cmsg)
766 {
767         capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
768
769         if (!card) {
770                 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
771                        capi_cmd2str(cmsg->Command, cmsg->Subcommand),
772                        cmsg->adr.adrController & 0x7f);
773                 return;
774         }
775         switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
776
777         case CAPI_LISTEN_CONF:  /* Controller */
778                 if (debugmode)
779                         printk(KERN_DEBUG "capidrv-%d: listenconf Info=0x%4x (%s) cipmask=0x%x\n",
780                                card->contrnr, cmsg->Info, capi_info2str(cmsg->Info), card->cipmask);
781                 if (cmsg->Info) {
782                         listen_change_state(card, EV_LISTEN_CONF_ERROR);
783                 } else if (card->cipmask == 0) {
784                         listen_change_state(card, EV_LISTEN_CONF_EMPTY);
785                 } else {
786                         listen_change_state(card, EV_LISTEN_CONF_OK);
787                 }
788                 break;
789
790         case CAPI_MANUFACTURER_IND:     /* Controller */
791                 if (   cmsg->ManuID == 0x214D5641
792                     && cmsg->Class == 0
793                     && cmsg->Function == 1) {
794                    u8  *data = cmsg->ManuData+3;
795                    u16  len = cmsg->ManuData[0];
796                    u16 layer;
797                    int direction;
798                    if (len == 255) {
799                       len = (cmsg->ManuData[1] | (cmsg->ManuData[2] << 8));
800                       data += 2;
801                    }
802                    len -= 2;
803                    layer = ((*(data-1)) << 8) | *(data-2);
804                    if (layer & 0x300)
805                         direction = (layer & 0x200) ? 0 : 1;
806                    else direction = (layer & 0x800) ? 0 : 1;
807                    if (layer & 0x0C00) {
808                         if ((layer & 0xff) == 0x80) {
809                            handle_dtrace_data(card, direction, 1, data, len);
810                            break;
811                         }
812                    } else if ((layer & 0xff) < 0x80) {
813                       handle_dtrace_data(card, direction, 0, data, len);
814                       break;
815                    }
816                    printk(KERN_INFO "capidrv-%d: %s from controller 0x%x layer 0x%x, ignored\n",
817                         card->contrnr, 
818                         capi_cmd2str(cmsg->Command, cmsg->Subcommand),
819                         cmsg->adr.adrController, layer);
820                    break;
821                 }
822                 goto ignored;
823         case CAPI_MANUFACTURER_CONF:    /* Controller */
824                 if (cmsg->ManuID == 0x214D5641) {
825                    char *s = NULL;
826                    switch (cmsg->Class) {
827                       case 0: break;
828                       case 1: s = "unknown class"; break;
829                       case 2: s = "unknown function"; break;
830                       default: s = "unkown error"; break;
831                    }
832                    if (s)
833                    printk(KERN_INFO "capidrv-%d: %s from controller 0x%x function %d: %s\n",
834                         card->contrnr,
835                         capi_cmd2str(cmsg->Command, cmsg->Subcommand),
836                         cmsg->adr.adrController,
837                         cmsg->Function, s);
838                    break;
839                 }
840                 goto ignored;
841         case CAPI_FACILITY_IND: /* Controller/plci/ncci */
842                 goto ignored;
843         case CAPI_FACILITY_CONF:        /* Controller/plci/ncci */
844                 goto ignored;
845         case CAPI_INFO_IND:     /* Controller/plci */
846                 goto ignored;
847         case CAPI_INFO_CONF:    /* Controller/plci */
848                 goto ignored;
849
850         default:
851                 printk(KERN_ERR "capidrv-%d: got %s from controller 0x%x ???",
852                        card->contrnr,
853                        capi_cmd2str(cmsg->Command, cmsg->Subcommand),
854                        cmsg->adr.adrController);
855         }
856         return;
857
858       ignored:
859         printk(KERN_INFO "capidrv-%d: %s from controller 0x%x ignored\n",
860                card->contrnr,
861                capi_cmd2str(cmsg->Command, cmsg->Subcommand),
862                cmsg->adr.adrController);
863 }
864
865 static void handle_incoming_call(capidrv_contr * card, _cmsg * cmsg)
866 {
867         capidrv_plci *plcip;
868         capidrv_bchan *bchan;
869         isdn_ctrl cmd;
870         int chan;
871
872         if ((chan = new_bchan(card)) == -1) {
873                 printk(KERN_ERR "capidrv-%d: incoming call on not existing bchan ?\n", card->contrnr);
874                 return;
875         }
876         bchan = &card->bchans[chan];
877         if ((plcip = new_plci(card, chan)) == 0) {
878                 printk(KERN_ERR "capidrv-%d: incoming call: no memory, sorry.\n", card->contrnr);
879                 return;
880         }
881         bchan->incoming = 1;
882         plcip->plci = cmsg->adr.adrPLCI;
883         plci_change_state(card, plcip, EV_PLCI_CONNECT_IND);
884
885         cmd.command = ISDN_STAT_ICALL;
886         cmd.driver = card->myid;
887         cmd.arg = chan;
888         memset(&cmd.parm.setup, 0, sizeof(cmd.parm.setup));
889         strncpy(cmd.parm.setup.phone,
890                 cmsg->CallingPartyNumber + 3,
891                 cmsg->CallingPartyNumber[0] - 2);
892         strncpy(cmd.parm.setup.eazmsn,
893                 cmsg->CalledPartyNumber + 2,
894                 cmsg->CalledPartyNumber[0] - 1);
895         cmd.parm.setup.si1 = cip2si1(cmsg->CIPValue);
896         cmd.parm.setup.si2 = cip2si2(cmsg->CIPValue);
897         cmd.parm.setup.plan = cmsg->CallingPartyNumber[1];
898         cmd.parm.setup.screen = cmsg->CallingPartyNumber[2];
899
900         printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s\n", 
901                         card->contrnr,
902                         cmd.parm.setup.phone,
903                         cmd.parm.setup.si1,
904                         cmd.parm.setup.si2,
905                         cmd.parm.setup.eazmsn);
906
907         if (cmd.parm.setup.si1 == 1 && cmd.parm.setup.si2 != 0) {
908                 printk(KERN_INFO "capidrv-%d: patching si2=%d to 0 for VBOX\n", 
909                         card->contrnr,
910                         cmd.parm.setup.si2);
911                 cmd.parm.setup.si2 = 0;
912         }
913
914         switch (card->interface.statcallb(&cmd)) {
915         case 0:
916         case 3:
917                 /* No device matching this call.
918                  * and isdn_common.c has send a HANGUP command
919                  * which is ignored in state ST_PLCI_INCOMING,
920                  * so we send RESP to ignore the call
921                  */
922                 capi_cmsg_answer(cmsg);
923                 cmsg->Reject = 1;       /* ignore */
924                 send_message(card, cmsg);
925                 plci_change_state(card, plcip, EV_PLCI_CONNECT_REJECT);
926                 printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s ignored\n",
927                         card->contrnr,
928                         cmd.parm.setup.phone,
929                         cmd.parm.setup.si1,
930                         cmd.parm.setup.si2,
931                         cmd.parm.setup.eazmsn);
932                 break;
933         case 1:
934                 /* At least one device matching this call (RING on ttyI)
935                  * HL-driver may send ALERTING on the D-channel in this
936                  * case.
937                  * really means: RING on ttyI or a net interface
938                  * accepted this call already.
939                  *
940                  * If the call was accepted, state has already changed,
941                  * and CONNECT_RESP already sent.
942                  */
943                 if (plcip->state == ST_PLCI_INCOMING) {
944                         printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s tty alerting\n",
945                                 card->contrnr,
946                                 cmd.parm.setup.phone,
947                                 cmd.parm.setup.si1,
948                                 cmd.parm.setup.si2,
949                                 cmd.parm.setup.eazmsn);
950                         capi_fill_ALERT_REQ(cmsg,
951                                             global.ap.applid,
952                                             card->msgid++,
953                                             plcip->plci,        /* adr */
954                                             NULL,/* BChannelinformation */
955                                             NULL,/* Keypadfacility */
956                                             NULL,/* Useruserdata */
957                                             NULL /* Facilitydataarray */
958                         );
959                         plcip->msgid = cmsg->Messagenumber;
960                         send_message(card, cmsg);
961                 } else {
962                         printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s on netdev\n",
963                                 card->contrnr,
964                                 cmd.parm.setup.phone,
965                                 cmd.parm.setup.si1,
966                                 cmd.parm.setup.si2,
967                                 cmd.parm.setup.eazmsn);
968                 }
969                 break;
970
971         case 2:         /* Call will be rejected. */
972                 capi_cmsg_answer(cmsg);
973                 cmsg->Reject = 2;       /* reject call, normal call clearing */
974                 send_message(card, cmsg);
975                 plci_change_state(card, plcip, EV_PLCI_CONNECT_REJECT);
976                 break;
977
978         default:
979                 /* An error happened. (Invalid parameters for example.) */
980                 capi_cmsg_answer(cmsg);
981                 cmsg->Reject = 8;       /* reject call,
982                                            destination out of order */
983                 send_message(card, cmsg);
984                 plci_change_state(card, plcip, EV_PLCI_CONNECT_REJECT);
985                 break;
986         }
987         return;
988 }
989
990 static void handle_plci(_cmsg * cmsg)
991 {
992         capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
993         capidrv_plci *plcip;
994         isdn_ctrl cmd;
995
996         if (!card) {
997                 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
998                        capi_cmd2str(cmsg->Command, cmsg->Subcommand),
999                        cmsg->adr.adrController & 0x7f);
1000                 return;
1001         }
1002         switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
1003
1004         case CAPI_DISCONNECT_IND:       /* plci */
1005                 if (cmsg->Reason) {
1006                         printk(KERN_INFO "capidrv-%d: %s reason 0x%x (%s) for plci 0x%x\n",
1007                            card->contrnr,
1008                            capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1009                                cmsg->Reason, capi_info2str(cmsg->Reason), cmsg->adr.adrPLCI);
1010                 }
1011                 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI))) {
1012                         capi_cmsg_answer(cmsg);
1013                         send_message(card, cmsg);
1014                         goto notfound;
1015                 }
1016                 card->bchans[plcip->chan].disconnecting = 1;
1017                 plci_change_state(card, plcip, EV_PLCI_DISCONNECT_IND);
1018                 capi_cmsg_answer(cmsg);
1019                 send_message(card, cmsg);
1020                 plci_change_state(card, plcip, EV_PLCI_DISCONNECT_RESP);
1021                 break;
1022
1023         case CAPI_DISCONNECT_CONF:      /* plci */
1024                 if (cmsg->Info) {
1025                         printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
1026                            card->contrnr,
1027                            capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1028                                cmsg->Info, capi_info2str(cmsg->Info), 
1029                                cmsg->adr.adrPLCI);
1030                 }
1031                 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
1032                         goto notfound;
1033
1034                 card->bchans[plcip->chan].disconnecting = 1;
1035                 break;
1036
1037         case CAPI_ALERT_CONF:   /* plci */
1038                 if (cmsg->Info) {
1039                         printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
1040                            card->contrnr,
1041                            capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1042                                cmsg->Info, capi_info2str(cmsg->Info), 
1043                                cmsg->adr.adrPLCI);
1044                 }
1045                 break;
1046
1047         case CAPI_CONNECT_IND:  /* plci */
1048                 handle_incoming_call(card, cmsg);
1049                 break;
1050
1051         case CAPI_CONNECT_CONF: /* plci */
1052                 if (cmsg->Info) {
1053                         printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
1054                            card->contrnr,
1055                            capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1056                                cmsg->Info, capi_info2str(cmsg->Info), 
1057                                cmsg->adr.adrPLCI);
1058                 }
1059                 if (!(plcip = find_plci_by_msgid(card, cmsg->Messagenumber)))
1060                         goto notfound;
1061
1062                 plcip->plci = cmsg->adr.adrPLCI;
1063                 if (cmsg->Info) {
1064                         plci_change_state(card, plcip, EV_PLCI_CONNECT_CONF_ERROR);
1065                 } else {
1066                         plci_change_state(card, plcip, EV_PLCI_CONNECT_CONF_OK);
1067                 }
1068                 break;
1069
1070         case CAPI_CONNECT_ACTIVE_IND:   /* plci */
1071
1072                 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
1073                         goto notfound;
1074
1075                 if (card->bchans[plcip->chan].incoming) {
1076                         capi_cmsg_answer(cmsg);
1077                         send_message(card, cmsg);
1078                         plci_change_state(card, plcip, EV_PLCI_CONNECT_ACTIVE_IND);
1079                 } else {
1080                         capidrv_ncci *nccip;
1081                         capi_cmsg_answer(cmsg);
1082                         send_message(card, cmsg);
1083
1084                         nccip = new_ncci(card, plcip, cmsg->adr.adrPLCI);
1085
1086                         if (!nccip) {
1087                                 printk(KERN_ERR "capidrv-%d: no mem for ncci, sorry\n", card->contrnr);
1088                                 break;  /* $$$$ */
1089                         }
1090                         capi_fill_CONNECT_B3_REQ(cmsg,
1091                                                  global.ap.applid,
1092                                                  card->msgid++,
1093                                                  plcip->plci,   /* adr */
1094                                                  NULL   /* NCPI */
1095                         );
1096                         nccip->msgid = cmsg->Messagenumber;
1097                         send_message(card, cmsg);
1098                         cmd.command = ISDN_STAT_DCONN;
1099                         cmd.driver = card->myid;
1100                         cmd.arg = plcip->chan;
1101                         card->interface.statcallb(&cmd);
1102                         plci_change_state(card, plcip, EV_PLCI_CONNECT_ACTIVE_IND);
1103                         ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_REQ);
1104                 }
1105                 break;
1106
1107         case CAPI_INFO_IND:     /* Controller/plci */
1108
1109                 if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
1110                         goto notfound;
1111
1112                 if (cmsg->InfoNumber == 0x4000) {
1113                         if (cmsg->InfoElement[0] == 4) {
1114                                 cmd.command = ISDN_STAT_CINF;
1115                                 cmd.driver = card->myid;
1116                                 cmd.arg = plcip->chan;
1117                                 sprintf(cmd.parm.num, "%lu",
1118                                         (unsigned long)
1119                                         ((u32) cmsg->InfoElement[1]
1120                                   | ((u32) (cmsg->InfoElement[2]) << 8)
1121                                  | ((u32) (cmsg->InfoElement[3]) << 16)
1122                                          | ((u32) (cmsg->InfoElement[4]) << 24)));
1123                                 card->interface.statcallb(&cmd);
1124                                 break;
1125                         }
1126                 }
1127                 printk(KERN_ERR "capidrv-%d: %s\n",
1128                                 card->contrnr, capi_cmsg2str(cmsg));
1129                 break;
1130
1131         case CAPI_CONNECT_ACTIVE_CONF:          /* plci */
1132                 goto ignored;
1133         case CAPI_SELECT_B_PROTOCOL_CONF:       /* plci */
1134                 goto ignored;
1135         case CAPI_FACILITY_IND: /* Controller/plci/ncci */
1136                 goto ignored;
1137         case CAPI_FACILITY_CONF:        /* Controller/plci/ncci */
1138                 goto ignored;
1139
1140         case CAPI_INFO_CONF:    /* Controller/plci */
1141                 goto ignored;
1142
1143         default:
1144                 printk(KERN_ERR "capidrv-%d: got %s for plci 0x%x ???",
1145                        card->contrnr,
1146                        capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1147                        cmsg->adr.adrPLCI);
1148         }
1149         return;
1150       ignored:
1151         printk(KERN_INFO "capidrv-%d: %s for plci 0x%x ignored\n",
1152                card->contrnr,
1153                capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1154                cmsg->adr.adrPLCI);
1155         return;
1156       notfound:
1157         printk(KERN_ERR "capidrv-%d: %s: plci 0x%x not found\n",
1158                card->contrnr,
1159                capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1160                cmsg->adr.adrPLCI);
1161         return;
1162 }
1163
1164 static void handle_ncci(_cmsg * cmsg)
1165 {
1166         capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
1167         capidrv_plci *plcip;
1168         capidrv_ncci *nccip;
1169         isdn_ctrl cmd;
1170         int len;
1171
1172         if (!card) {
1173                 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
1174                        capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1175                        cmsg->adr.adrController & 0x7f);
1176                 return;
1177         }
1178         switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
1179
1180         case CAPI_CONNECT_B3_ACTIVE_IND:        /* ncci */
1181                 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1182                         goto notfound;
1183
1184                 capi_cmsg_answer(cmsg);
1185                 send_message(card, cmsg);
1186                 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_ACTIVE_IND);
1187
1188                 cmd.command = ISDN_STAT_BCONN;
1189                 cmd.driver = card->myid;
1190                 cmd.arg = nccip->chan;
1191                 card->interface.statcallb(&cmd);
1192
1193                 printk(KERN_INFO "capidrv-%d: chan %d up with ncci 0x%x\n",
1194                        card->contrnr, nccip->chan, nccip->ncci);
1195                 break;
1196
1197         case CAPI_CONNECT_B3_ACTIVE_CONF:       /* ncci */
1198                 goto ignored;
1199
1200         case CAPI_CONNECT_B3_IND:       /* ncci */
1201
1202                 plcip = find_plci_by_ncci(card, cmsg->adr.adrNCCI);
1203                 if (plcip) {
1204                         nccip = new_ncci(card, plcip, cmsg->adr.adrNCCI);
1205                         if (nccip) {
1206                                 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_IND);
1207                                 capi_fill_CONNECT_B3_RESP(cmsg,
1208                                                           global.ap.applid,
1209                                                           card->msgid++,
1210                                                           nccip->ncci,  /* adr */
1211                                                           0,    /* Reject */
1212                                                           NULL  /* NCPI */
1213                                 );
1214                                 send_message(card, cmsg);
1215                                 ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_RESP);
1216                                 break;
1217                         }
1218                         printk(KERN_ERR "capidrv-%d: no mem for ncci, sorry\n",                                                 card->contrnr);
1219                 } else {
1220                         printk(KERN_ERR "capidrv-%d: %s: plci for ncci 0x%x not found\n",
1221                            card->contrnr,
1222                            capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1223                                cmsg->adr.adrNCCI);
1224                 }
1225                 capi_fill_CONNECT_B3_RESP(cmsg,
1226                                           global.ap.applid,
1227                                           card->msgid++,
1228                                           cmsg->adr.adrNCCI,
1229                                           2,    /* Reject */
1230                                           NULL  /* NCPI */
1231                 );
1232                 send_message(card, cmsg);
1233                 break;
1234
1235         case CAPI_CONNECT_B3_CONF:      /* ncci */
1236
1237                 if (!(nccip = find_ncci_by_msgid(card,
1238                                                  cmsg->adr.adrNCCI,
1239                                                  cmsg->Messagenumber)))
1240                         goto notfound;
1241
1242                 nccip->ncci = cmsg->adr.adrNCCI;
1243                 if (cmsg->Info) {
1244                         printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for ncci 0x%x\n",
1245                            card->contrnr,
1246                            capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1247                                cmsg->Info, capi_info2str(cmsg->Info), 
1248                                cmsg->adr.adrNCCI);
1249                 }
1250
1251                 if (cmsg->Info)
1252                         ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_CONF_ERROR);
1253                 else
1254                         ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_CONF_OK);
1255                 break;
1256
1257         case CAPI_CONNECT_B3_T90_ACTIVE_IND:    /* ncci */
1258                 capi_cmsg_answer(cmsg);
1259                 send_message(card, cmsg);
1260                 break;
1261
1262         case CAPI_DATA_B3_IND:  /* ncci */
1263                 /* handled in handle_data() */
1264                 goto ignored;
1265
1266         case CAPI_DATA_B3_CONF: /* ncci */
1267                 if (cmsg->Info) {
1268                         printk(KERN_WARNING "CAPI_DATA_B3_CONF: Info %x - %s\n",
1269                                 cmsg->Info, capi_info2str(cmsg->Info));
1270                 }
1271                 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1272                         goto notfound;
1273
1274                 len = capidrv_del_ack(nccip, cmsg->DataHandle);
1275                 if (len < 0)
1276                         break;
1277                 cmd.command = ISDN_STAT_BSENT;
1278                 cmd.driver = card->myid;
1279                 cmd.arg = nccip->chan;
1280                 cmd.parm.length = len;
1281                 card->interface.statcallb(&cmd);
1282                 break;
1283
1284         case CAPI_DISCONNECT_B3_IND:    /* ncci */
1285                 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1286                         goto notfound;
1287
1288                 card->bchans[nccip->chan].disconnecting = 1;
1289                 ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_IND);
1290                 capi_cmsg_answer(cmsg);
1291                 send_message(card, cmsg);
1292                 ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_RESP);
1293                 break;
1294
1295         case CAPI_DISCONNECT_B3_CONF:   /* ncci */
1296                 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1297                         goto notfound;
1298                 if (cmsg->Info) {
1299                         printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for ncci 0x%x\n",
1300                            card->contrnr,
1301                            capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1302                                cmsg->Info, capi_info2str(cmsg->Info), 
1303                                cmsg->adr.adrNCCI);
1304                         ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_CONF_ERROR);
1305                 }
1306                 break;
1307
1308         case CAPI_RESET_B3_IND: /* ncci */
1309                 if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
1310                         goto notfound;
1311                 ncci_change_state(card, nccip, EV_NCCI_RESET_B3_IND);
1312                 capi_cmsg_answer(cmsg);
1313                 send_message(card, cmsg);
1314                 break;
1315
1316         case CAPI_RESET_B3_CONF:        /* ncci */
1317                 goto ignored;   /* $$$$ */
1318
1319         case CAPI_FACILITY_IND: /* Controller/plci/ncci */
1320                 goto ignored;
1321         case CAPI_FACILITY_CONF:        /* Controller/plci/ncci */
1322                 goto ignored;
1323
1324         default:
1325                 printk(KERN_ERR "capidrv-%d: got %s for ncci 0x%x ???",
1326                        card->contrnr,
1327                        capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1328                        cmsg->adr.adrNCCI);
1329         }
1330         return;
1331       ignored:
1332         printk(KERN_INFO "capidrv-%d: %s for ncci 0x%x ignored\n",
1333                card->contrnr,
1334                capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1335                cmsg->adr.adrNCCI);
1336         return;
1337       notfound:
1338         printk(KERN_ERR "capidrv-%d: %s: ncci 0x%x not found\n",
1339                card->contrnr,
1340                capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1341                cmsg->adr.adrNCCI);
1342 }
1343
1344
1345 static void handle_data(_cmsg * cmsg, struct sk_buff *skb)
1346 {
1347         capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
1348         capidrv_ncci *nccip;
1349
1350         if (!card) {
1351                 printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
1352                        capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1353                        cmsg->adr.adrController & 0x7f);
1354                 kfree_skb(skb);
1355                 return;
1356         }
1357         if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI))) {
1358                 printk(KERN_ERR "capidrv-%d: %s: ncci 0x%x not found\n",
1359                        card->contrnr,
1360                        capi_cmd2str(cmsg->Command, cmsg->Subcommand),
1361                        cmsg->adr.adrNCCI);
1362                 kfree_skb(skb);
1363                 return;
1364         }
1365         (void) skb_pull(skb, CAPIMSG_LEN(skb->data));
1366         card->interface.rcvcallb_skb(card->myid, nccip->chan, skb);
1367         capi_cmsg_answer(cmsg);
1368         send_message(card, cmsg);
1369 }
1370
1371 static _cmsg s_cmsg;
1372
1373 static void capidrv_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
1374 {
1375         capi_message2cmsg(&s_cmsg, skb->data);
1376         if (debugmode > 3)
1377                 printk(KERN_DEBUG "capidrv_signal: applid=%d %s\n",
1378                        ap->applid, capi_cmsg2str(&s_cmsg));
1379         
1380         if (s_cmsg.Command == CAPI_DATA_B3
1381             && s_cmsg.Subcommand == CAPI_IND) {
1382                 handle_data(&s_cmsg, skb);
1383                 return;
1384         }
1385         if ((s_cmsg.adr.adrController & 0xffffff00) == 0)
1386                 handle_controller(&s_cmsg);
1387         else if ((s_cmsg.adr.adrPLCI & 0xffff0000) == 0)
1388                 handle_plci(&s_cmsg);
1389         else
1390                 handle_ncci(&s_cmsg);
1391         /*
1392          * data of skb used in s_cmsg,
1393          * free data when s_cmsg is not used again
1394          * thanks to Lars Heete <hel@admin.de>
1395          */
1396         kfree_skb(skb);
1397 }
1398
1399 /* ------------------------------------------------------------------- */
1400
1401 #define PUTBYTE_TO_STATUS(card, byte) \
1402         do { \
1403                 *(card)->q931_write++ = (byte); \
1404                 if ((card)->q931_write > (card)->q931_end) \
1405                         (card)->q931_write = (card)->q931_buf; \
1406         } while (0)
1407
1408 static void handle_dtrace_data(capidrv_contr *card,
1409                              int send, int level2, u8 *data, u16 len)
1410 {
1411         u8 *p, *end;
1412         isdn_ctrl cmd;
1413
1414         if (!len) {
1415                 printk(KERN_DEBUG "capidrv-%d: avmb1_q931_data: len == %d\n",
1416                                 card->contrnr, len);
1417                 return;
1418         }
1419
1420         if (level2) {
1421                 PUTBYTE_TO_STATUS(card, 'D');
1422                 PUTBYTE_TO_STATUS(card, '2');
1423                 PUTBYTE_TO_STATUS(card, send ? '>' : '<');
1424                 PUTBYTE_TO_STATUS(card, ':');
1425         } else {
1426                 PUTBYTE_TO_STATUS(card, 'D');
1427                 PUTBYTE_TO_STATUS(card, '3');
1428                 PUTBYTE_TO_STATUS(card, send ? '>' : '<');
1429                 PUTBYTE_TO_STATUS(card, ':');
1430         }
1431
1432         for (p = data, end = data+len; p < end; p++) {
1433                 u8 w;
1434                 PUTBYTE_TO_STATUS(card, ' ');
1435                 w = (*p >> 4) & 0xf;
1436                 PUTBYTE_TO_STATUS(card, (w < 10) ? '0'+w : 'A'-10+w);
1437                 w = *p & 0xf;
1438                 PUTBYTE_TO_STATUS(card, (w < 10) ? '0'+w : 'A'-10+w);
1439         }
1440         PUTBYTE_TO_STATUS(card, '\n');
1441
1442         cmd.command = ISDN_STAT_STAVAIL;
1443         cmd.driver = card->myid;
1444         cmd.arg = len*3+5;
1445         card->interface.statcallb(&cmd);
1446 }
1447
1448 /* ------------------------------------------------------------------- */
1449
1450 static _cmsg cmdcmsg;
1451
1452 static int capidrv_ioctl(isdn_ctrl * c, capidrv_contr * card)
1453 {
1454         switch (c->arg) {
1455         case 1:
1456                 debugmode = (int)(*((unsigned int *)c->parm.num));
1457                 printk(KERN_DEBUG "capidrv-%d: debugmode=%d\n",
1458                                 card->contrnr, debugmode);
1459                 return 0;
1460         default:
1461                 printk(KERN_DEBUG "capidrv-%d: capidrv_ioctl(%ld) called ??\n",
1462                                 card->contrnr, c->arg);
1463                 return -EINVAL;
1464         }
1465         return -EINVAL;
1466 }
1467
1468 /*
1469  * Handle leased lines (CAPI-Bundling)
1470  */
1471
1472 struct internal_bchannelinfo {
1473    unsigned short channelalloc;
1474    unsigned short operation;
1475    unsigned char  cmask[31];
1476 };
1477
1478 static int decodeFVteln(char *teln, unsigned long *bmaskp, int *activep)
1479 {
1480         unsigned long bmask = 0;
1481         int active = !0;
1482         char *s;
1483         int i;
1484
1485         if (strncmp(teln, "FV:", 3) != 0)
1486                 return 1;
1487         s = teln + 3;
1488         while (*s && *s == ' ') s++;
1489         if (!*s) return -2;
1490         if (*s == 'p' || *s == 'P') {
1491                 active = 0;
1492                 s++;
1493         }
1494         if (*s == 'a' || *s == 'A') {
1495                 active = !0;
1496                 s++;
1497         }
1498         while (*s) {
1499                 int digit1 = 0;
1500                 int digit2 = 0;
1501                 if (!isdigit(*s)) return -3;
1502                 while (isdigit(*s)) { digit1 = digit1*10 + (*s - '0'); s++; }
1503                 if (digit1 <= 0 && digit1 > 30) return -4;
1504                 if (*s == 0 || *s == ',' || *s == ' ') {
1505                         bmask |= (1 << digit1);
1506                         digit1 = 0;
1507                         if (*s) s++;
1508                         continue;
1509                 }
1510                 if (*s != '-') return -5;
1511                 s++;
1512                 if (!isdigit(*s)) return -3;
1513                 while (isdigit(*s)) { digit2 = digit2*10 + (*s - '0'); s++; }
1514                 if (digit2 <= 0 && digit2 > 30) return -4;
1515                 if (*s == 0 || *s == ',' || *s == ' ') {
1516                         if (digit1 > digit2)
1517                                 for (i = digit2; i <= digit1 ; i++)
1518                                         bmask |= (1 << i);
1519                         else 
1520                                 for (i = digit1; i <= digit2 ; i++)
1521                                         bmask |= (1 << i);
1522                         digit1 = digit2 = 0;
1523                         if (*s) s++;
1524                         continue;
1525                 }
1526                 return -6;
1527         }
1528         if (activep) *activep = active;
1529         if (bmaskp) *bmaskp = bmask;
1530         return 0;
1531 }
1532
1533 static int FVteln2capi20(char *teln, u8 AdditionalInfo[1+2+2+31])
1534 {
1535         unsigned long bmask;
1536         int active;
1537         int rc, i;
1538    
1539         rc = decodeFVteln(teln, &bmask, &active);
1540         if (rc) return rc;
1541         /* Length */
1542         AdditionalInfo[0] = 2+2+31;
1543         /* Channel: 3 => use channel allocation */
1544         AdditionalInfo[1] = 3; AdditionalInfo[2] = 0;
1545         /* Operation: 0 => DTE mode, 1 => DCE mode */
1546         if (active) {
1547                 AdditionalInfo[3] = 0; AdditionalInfo[4] = 0;
1548         } else {
1549                 AdditionalInfo[3] = 1; AdditionalInfo[4] = 0;
1550         }
1551         /* Channel mask array */
1552         AdditionalInfo[5] = 0; /* no D-Channel */
1553         for (i=1; i <= 30; i++)
1554                 AdditionalInfo[5+i] = (bmask & (1 << i)) ? 0xff : 0;
1555         return 0;
1556 }
1557
1558 static int capidrv_command(isdn_ctrl * c, capidrv_contr * card)
1559 {
1560         isdn_ctrl cmd;
1561         struct capidrv_bchan *bchan;
1562         struct capidrv_plci *plcip;
1563         u8 AdditionalInfo[1+2+2+31];
1564         int rc, isleasedline = 0;
1565
1566         if (c->command == ISDN_CMD_IOCTL)
1567                 return capidrv_ioctl(c, card);
1568
1569         switch (c->command) {
1570         case ISDN_CMD_DIAL:{
1571                         u8 calling[ISDN_MSNLEN + 3];
1572                         u8 called[ISDN_MSNLEN + 2];
1573
1574                         if (debugmode)
1575                                 printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_DIAL(ch=%ld,\"%s,%d,%d,%s\")\n",
1576                                         card->contrnr,
1577                                         c->arg,
1578                                         c->parm.setup.phone,
1579                                         c->parm.setup.si1,
1580                                         c->parm.setup.si2,
1581                                         c->parm.setup.eazmsn);
1582
1583                         bchan = &card->bchans[c->arg % card->nbchan];
1584
1585                         if (bchan->plcip) {
1586                                 printk(KERN_ERR "capidrv-%d: dail ch=%ld,\"%s,%d,%d,%s\" in use (plci=0x%x)\n",
1587                                         card->contrnr,
1588                                         c->arg, 
1589                                         c->parm.setup.phone,
1590                                         c->parm.setup.si1,
1591                                         c->parm.setup.si2,
1592                                         c->parm.setup.eazmsn,
1593                                         bchan->plcip->plci);
1594                                 return 0;
1595                         }
1596                         bchan->si1 = c->parm.setup.si1;
1597                         bchan->si2 = c->parm.setup.si2;
1598
1599                         strncpy(bchan->num, c->parm.setup.phone, sizeof(bchan->num));
1600                         strncpy(bchan->mynum, c->parm.setup.eazmsn, sizeof(bchan->mynum));
1601                         rc = FVteln2capi20(bchan->num, AdditionalInfo);
1602                         isleasedline = (rc == 0);
1603                         if (rc < 0)
1604                                 printk(KERN_ERR "capidrv-%d: WARNING: invalid leased linedefinition \"%s\"\n", card->contrnr, bchan->num);
1605
1606                         if (isleasedline) {
1607                                 calling[0] = 0;
1608                                 called[0] = 0;
1609                                 if (debugmode)
1610                                         printk(KERN_DEBUG "capidrv-%d: connecting leased line\n", card->contrnr);
1611                         } else {
1612                                 calling[0] = strlen(bchan->mynum) + 2;
1613                                 calling[1] = 0;
1614                                 calling[2] = 0x80;
1615                                 strncpy(calling + 3, bchan->mynum, ISDN_MSNLEN);
1616                                 called[0] = strlen(bchan->num) + 1;
1617                                 called[1] = 0x80;
1618                                 strncpy(called + 2, bchan->num, ISDN_MSNLEN);
1619                         }
1620
1621                         capi_fill_CONNECT_REQ(&cmdcmsg,
1622                                               global.ap.applid,
1623                                               card->msgid++,
1624                                               card->contrnr,    /* adr */
1625                                           si2cip(bchan->si1, bchan->si2),       /* cipvalue */
1626                                               called,   /* CalledPartyNumber */
1627                                               calling,  /* CallingPartyNumber */
1628                                               NULL,     /* CalledPartySubaddress */
1629                                               NULL,     /* CallingPartySubaddress */
1630                                             b1prot(bchan->l2, bchan->l3),       /* B1protocol */
1631                                             b2prot(bchan->l2, bchan->l3),       /* B2protocol */
1632                                             b3prot(bchan->l2, bchan->l3),       /* B3protocol */
1633                                             b1config(bchan->l2, bchan->l3),     /* B1configuration */
1634                                               NULL,     /* B2configuration */
1635                                               NULL,     /* B3configuration */
1636                                               NULL,     /* BC */
1637                                               NULL,     /* LLC */
1638                                               NULL,     /* HLC */
1639                                               /* BChannelinformation */
1640                                               isleasedline ? AdditionalInfo : NULL,
1641                                               NULL,     /* Keypadfacility */
1642                                               NULL,     /* Useruserdata */
1643                                               NULL      /* Facilitydataarray */
1644                             );
1645                         if ((plcip = new_plci(card, (c->arg % card->nbchan))) == 0) {
1646                                 cmd.command = ISDN_STAT_DHUP;
1647                                 cmd.driver = card->myid;
1648                                 cmd.arg = (c->arg % card->nbchan);
1649                                 card->interface.statcallb(&cmd);
1650                                 return -1;
1651                         }
1652                         plcip->msgid = cmdcmsg.Messagenumber;
1653                         plcip->leasedline = isleasedline;
1654                         plci_change_state(card, plcip, EV_PLCI_CONNECT_REQ);
1655                         send_message(card, &cmdcmsg);
1656                         return 0;
1657                 }
1658
1659         case ISDN_CMD_ACCEPTD:
1660
1661                 bchan = &card->bchans[c->arg % card->nbchan];
1662                 if (debugmode)
1663                         printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_ACCEPTD(ch=%ld) l2=%d l3=%d\n",
1664                                card->contrnr,
1665                                c->arg, bchan->l2, bchan->l3);
1666
1667                 capi_fill_CONNECT_RESP(&cmdcmsg,
1668                                        global.ap.applid,
1669                                        card->msgid++,
1670                                        bchan->plcip->plci,      /* adr */
1671                                        0,       /* Reject */
1672                                        b1prot(bchan->l2, bchan->l3),    /* B1protocol */
1673                                        b2prot(bchan->l2, bchan->l3),    /* B2protocol */
1674                                        b3prot(bchan->l2, bchan->l3),    /* B3protocol */
1675                                        b1config(bchan->l2, bchan->l3),  /* B1configuration */
1676                                        NULL,    /* B2configuration */
1677                                        NULL,    /* B3configuration */
1678                                        NULL,    /* ConnectedNumber */
1679                                        NULL,    /* ConnectedSubaddress */
1680                                        NULL,    /* LLC */
1681                                        NULL,    /* BChannelinformation */
1682                                        NULL,    /* Keypadfacility */
1683                                        NULL,    /* Useruserdata */
1684                                        NULL     /* Facilitydataarray */
1685                 );
1686                 capi_cmsg2message(&cmdcmsg, cmdcmsg.buf);
1687                 plci_change_state(card, bchan->plcip, EV_PLCI_CONNECT_RESP);
1688                 send_message(card, &cmdcmsg);
1689                 return 0;
1690
1691         case ISDN_CMD_ACCEPTB:
1692                 if (debugmode)
1693                         printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_ACCEPTB(ch=%ld)\n",
1694                                card->contrnr,
1695                                c->arg);
1696                 return -ENOSYS;
1697
1698         case ISDN_CMD_HANGUP:
1699                 if (debugmode)
1700                         printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_HANGUP(ch=%ld)\n",
1701                                card->contrnr,
1702                                c->arg);
1703                 bchan = &card->bchans[c->arg % card->nbchan];
1704
1705                 if (bchan->disconnecting) {
1706                         if (debugmode)
1707                                 printk(KERN_DEBUG "capidrv-%d: chan %ld already disconnecting ...\n",
1708                                        card->contrnr,
1709                                        c->arg);
1710                         return 0;
1711                 }
1712                 if (bchan->nccip) {
1713                         bchan->disconnecting = 1;
1714                         capi_fill_DISCONNECT_B3_REQ(&cmdcmsg,
1715                                                     global.ap.applid,
1716                                                     card->msgid++,
1717                                                     bchan->nccip->ncci,
1718                                                     NULL        /* NCPI */
1719                         );
1720                         ncci_change_state(card, bchan->nccip, EV_NCCI_DISCONNECT_B3_REQ);
1721                         send_message(card, &cmdcmsg);
1722                         return 0;
1723                 } else if (bchan->plcip) {
1724                         if (bchan->plcip->state == ST_PLCI_INCOMING) {
1725                                 /*
1726                                  * just ignore, we a called from
1727                                  * isdn_status_callback(),
1728                                  * which will return 0 or 2, this is handled
1729                                  * by the CONNECT_IND handler
1730                                  */
1731                                 bchan->disconnecting = 1;
1732                                 return 0;
1733                         } else if (bchan->plcip->plci) {
1734                                 bchan->disconnecting = 1;
1735                                 capi_fill_DISCONNECT_REQ(&cmdcmsg,
1736                                                          global.ap.applid,
1737                                                          card->msgid++,
1738                                                       bchan->plcip->plci,
1739                                                          NULL,  /* BChannelinformation */
1740                                                          NULL,  /* Keypadfacility */
1741                                                          NULL,  /* Useruserdata */
1742                                                          NULL   /* Facilitydataarray */
1743                                 );
1744                                 plci_change_state(card, bchan->plcip, EV_PLCI_DISCONNECT_REQ);
1745                                 send_message(card, &cmdcmsg);
1746                                 return 0;
1747                         } else {
1748                                 printk(KERN_ERR "capidrv-%d: chan %ld disconnect request while waiting for CONNECT_CONF\n",
1749                                        card->contrnr,
1750                                        c->arg);
1751                                 return -EINVAL;
1752                         }
1753                 }
1754                 printk(KERN_ERR "capidrv-%d: chan %ld disconnect request on free channel\n",
1755                                        card->contrnr,
1756                                        c->arg);
1757                 return -EINVAL;
1758 /* ready */
1759
1760         case ISDN_CMD_SETL2:
1761                 if (debugmode)
1762                         printk(KERN_DEBUG "capidrv-%d: set L2 on chan %ld to %ld\n",
1763                                card->contrnr,
1764                                (c->arg & 0xff), (c->arg >> 8));
1765                 bchan = &card->bchans[(c->arg & 0xff) % card->nbchan];
1766                 bchan->l2 = (c->arg >> 8);
1767                 return 0;
1768
1769         case ISDN_CMD_SETL3:
1770                 if (debugmode)
1771                         printk(KERN_DEBUG "capidrv-%d: set L3 on chan %ld to %ld\n",
1772                                card->contrnr,
1773                                (c->arg & 0xff), (c->arg >> 8));
1774                 bchan = &card->bchans[(c->arg & 0xff) % card->nbchan];
1775                 bchan->l3 = (c->arg >> 8);
1776                 return 0;
1777
1778         case ISDN_CMD_SETEAZ:
1779                 if (debugmode)
1780                         printk(KERN_DEBUG "capidrv-%d: set EAZ \"%s\" on chan %ld\n",
1781                                card->contrnr,
1782                                c->parm.num, c->arg);
1783                 bchan = &card->bchans[c->arg % card->nbchan];
1784                 strncpy(bchan->msn, c->parm.num, ISDN_MSNLEN);
1785                 return 0;
1786
1787         case ISDN_CMD_CLREAZ:
1788                 if (debugmode)
1789                         printk(KERN_DEBUG "capidrv-%d: clearing EAZ on chan %ld\n",
1790                                         card->contrnr, c->arg);
1791                 bchan = &card->bchans[c->arg % card->nbchan];
1792                 bchan->msn[0] = 0;
1793                 return 0;
1794
1795         default:
1796                 printk(KERN_ERR "capidrv-%d: ISDN_CMD_%d, Huh?\n",
1797                                         card->contrnr, c->command);
1798                 return -EINVAL;
1799         }
1800         return 0;
1801 }
1802
1803 static int if_command(isdn_ctrl * c)
1804 {
1805         capidrv_contr *card = findcontrbydriverid(c->driver);
1806
1807         if (card)
1808                 return capidrv_command(c, card);
1809
1810         printk(KERN_ERR
1811              "capidrv: if_command %d called with invalid driverId %d!\n",
1812                                                 c->command, c->driver);
1813         return -ENODEV;
1814 }
1815
1816 static _cmsg sendcmsg;
1817
1818 static int if_sendbuf(int id, int channel, int doack, struct sk_buff *skb)
1819 {
1820         capidrv_contr *card = findcontrbydriverid(id);
1821         capidrv_bchan *bchan;
1822         capidrv_ncci *nccip;
1823         int len = skb->len;
1824         int msglen;
1825         u16 errcode;
1826         u16 datahandle;
1827
1828         if (!card) {
1829                 printk(KERN_ERR "capidrv: if_sendbuf called with invalid driverId %d!\n",
1830                        id);
1831                 return 0;
1832         }
1833         if (debugmode > 4)
1834                 printk(KERN_DEBUG "capidrv-%d: sendbuf len=%d skb=%p doack=%d\n",
1835                                         card->contrnr, len, skb, doack);
1836         bchan = &card->bchans[channel % card->nbchan];
1837         nccip = bchan->nccip;
1838         if (!nccip || nccip->state != ST_NCCI_ACTIVE) {
1839                 printk(KERN_ERR "capidrv-%d: if_sendbuf: %s:%d: chan not up!\n",
1840                        card->contrnr, card->name, channel);
1841                 return 0;
1842         }
1843         datahandle = nccip->datahandle;
1844         capi_fill_DATA_B3_REQ(&sendcmsg, global.ap.applid, card->msgid++,
1845                               nccip->ncci,      /* adr */
1846                               (u32) skb->data,  /* Data */
1847                               skb->len,         /* DataLength */
1848                               datahandle,       /* DataHandle */
1849                               0 /* Flags */
1850             );
1851
1852         if (capidrv_add_ack(nccip, datahandle, doack ? (int)skb->len : -1) < 0)
1853            return 0;
1854
1855         capi_cmsg2message(&sendcmsg, sendcmsg.buf);
1856         msglen = CAPIMSG_LEN(sendcmsg.buf);
1857         if (skb_headroom(skb) < msglen) {
1858                 struct sk_buff *nskb = skb_realloc_headroom(skb, msglen);
1859                 if (!nskb) {
1860                         printk(KERN_ERR "capidrv-%d: if_sendbuf: no memory\n",
1861                                 card->contrnr);
1862                         (void)capidrv_del_ack(nccip, datahandle);
1863                         return 0;
1864                 }
1865                 printk(KERN_DEBUG "capidrv-%d: only %d bytes headroom, need %d\n",
1866                        card->contrnr, skb_headroom(skb), msglen);
1867                 memcpy(skb_push(nskb, msglen), sendcmsg.buf, msglen);
1868                 errcode = capi20_put_message(&global.ap, nskb);
1869                 if (errcode == CAPI_NOERROR) {
1870                         dev_kfree_skb(skb);
1871                         nccip->datahandle++;
1872                         return len;
1873                 }
1874                 if (debugmode > 3)
1875                         printk(KERN_DEBUG "capidrv-%d: sendbuf putmsg ret(%x) - %s\n",
1876                                 card->contrnr, errcode, capi_info2str(errcode));
1877                 (void)capidrv_del_ack(nccip, datahandle);
1878                 dev_kfree_skb(nskb);
1879                 return errcode == CAPI_SENDQUEUEFULL ? 0 : -1;
1880         } else {
1881                 memcpy(skb_push(skb, msglen), sendcmsg.buf, msglen);
1882                 errcode = capi20_put_message(&global.ap, skb);
1883                 if (errcode == CAPI_NOERROR) {
1884                         nccip->datahandle++;
1885                         return len;
1886                 }
1887                 if (debugmode > 3)
1888                         printk(KERN_DEBUG "capidrv-%d: sendbuf putmsg ret(%x) - %s\n",
1889                                 card->contrnr, errcode, capi_info2str(errcode));
1890                 skb_pull(skb, msglen);
1891                 (void)capidrv_del_ack(nccip, datahandle);
1892                 return errcode == CAPI_SENDQUEUEFULL ? 0 : -1;
1893         }
1894 }
1895
1896 static int if_readstat(u8 __user *buf, int len, int id, int channel)
1897 {
1898         capidrv_contr *card = findcontrbydriverid(id);
1899         int count;
1900         u8 __user *p;
1901
1902         if (!card) {
1903                 printk(KERN_ERR "capidrv: if_readstat called with invalid driverId %d!\n",
1904                        id);
1905                 return -ENODEV;
1906         }
1907
1908         for (p=buf, count=0; count < len; p++, count++) {
1909                 put_user(*card->q931_read++, p);
1910                 if (card->q931_read > card->q931_end)
1911                         card->q931_read = card->q931_buf;
1912         }
1913         return count;
1914
1915 }
1916
1917 static void enable_dchannel_trace(capidrv_contr *card)
1918 {
1919         u8 manufacturer[CAPI_MANUFACTURER_LEN];
1920         capi_version version;
1921         u16 contr = card->contrnr;
1922         u16 errcode;
1923         u16 avmversion[3];
1924
1925         errcode = capi20_get_manufacturer(contr, manufacturer);
1926         if (errcode != CAPI_NOERROR) {
1927            printk(KERN_ERR "%s: can't get manufacturer (0x%x)\n",
1928                         card->name, errcode);
1929            return;
1930         }
1931         if (strstr(manufacturer, "AVM") == 0) {
1932            printk(KERN_ERR "%s: not from AVM, no d-channel trace possible (%s)\n",
1933                         card->name, manufacturer);
1934            return;
1935         }
1936         errcode = capi20_get_version(contr, &version);
1937         if (errcode != CAPI_NOERROR) {
1938            printk(KERN_ERR "%s: can't get version (0x%x)\n",
1939                         card->name, errcode);
1940            return;
1941         }
1942         avmversion[0] = (version.majormanuversion >> 4) & 0x0f;
1943         avmversion[1] = (version.majormanuversion << 4) & 0xf0;
1944         avmversion[1] |= (version.minormanuversion >> 4) & 0x0f;
1945         avmversion[2] |= version.minormanuversion & 0x0f;
1946
1947         if (avmversion[0] > 3 || (avmversion[0] == 3 && avmversion[1] > 5)) {
1948                 printk(KERN_INFO "%s: D2 trace enabled\n", card->name);
1949                 capi_fill_MANUFACTURER_REQ(&cmdcmsg, global.ap.applid,
1950                                            card->msgid++,
1951                                            contr,
1952                                            0x214D5641,  /* ManuID */
1953                                            0,           /* Class */
1954                                            1,           /* Function */
1955                                            (_cstruct)"\004\200\014\000\000");
1956         } else {
1957                 printk(KERN_INFO "%s: D3 trace enabled\n", card->name);
1958                 capi_fill_MANUFACTURER_REQ(&cmdcmsg, global.ap.applid,
1959                                            card->msgid++,
1960                                            contr,
1961                                            0x214D5641,  /* ManuID */
1962                                            0,           /* Class */
1963                                            1,           /* Function */
1964                                            (_cstruct)"\004\002\003\000\000");
1965         }
1966         send_message(card, &cmdcmsg);
1967 }
1968
1969
1970 static void send_listen(capidrv_contr *card)
1971 {
1972         capi_fill_LISTEN_REQ(&cmdcmsg, global.ap.applid,
1973                              card->msgid++,
1974                              card->contrnr, /* controller */
1975                              1 << 6,    /* Infomask */
1976                              card->cipmask,
1977                              card->cipmask2,
1978                              NULL, NULL);
1979         send_message(card, &cmdcmsg);
1980         listen_change_state(card, EV_LISTEN_REQ);
1981 }
1982
1983 static void listentimerfunc(unsigned long x)
1984 {
1985         capidrv_contr *card = (capidrv_contr *)x;
1986         if (card->state != ST_LISTEN_NONE && card->state != ST_LISTEN_ACTIVE)
1987                 printk(KERN_ERR "%s: controller dead ??\n", card->name);
1988         send_listen(card);
1989         mod_timer(&card->listentimer, jiffies + 60*HZ);
1990 }
1991
1992
1993 static int capidrv_addcontr(u16 contr, struct capi_profile *profp)
1994 {
1995         capidrv_contr *card;
1996         unsigned long flags;
1997         isdn_ctrl cmd;
1998         char id[20];
1999         int i;
2000
2001         sprintf(id, "capidrv-%d", contr);
2002         if (!try_module_get(THIS_MODULE)) {
2003                 printk(KERN_WARNING "capidrv: (%s) Could not reserve module\n", id);
2004                 return -1;
2005         }
2006         if (!(card = (capidrv_contr *) kmalloc(sizeof(capidrv_contr), GFP_ATOMIC))) {
2007                 printk(KERN_WARNING
2008                  "capidrv: (%s) Could not allocate contr-struct.\n", id);
2009                 return -1;
2010         }
2011         memset(card, 0, sizeof(capidrv_contr));
2012         card->owner = THIS_MODULE;
2013         init_timer(&card->listentimer);
2014         strcpy(card->name, id);
2015         card->contrnr = contr;
2016         card->nbchan = profp->nbchannel;
2017         card->bchans = (capidrv_bchan *) kmalloc(sizeof(capidrv_bchan) * card->nbchan, GFP_ATOMIC);
2018         if (!card->bchans) {
2019                 printk(KERN_WARNING
2020                 "capidrv: (%s) Could not allocate bchan-structs.\n", id);
2021                 module_put(card->owner);
2022                 kfree(card);
2023                 return -1;
2024         }
2025         card->interface.channels = profp->nbchannel;
2026         card->interface.maxbufsize = 2048;
2027         card->interface.command = if_command;
2028         card->interface.writebuf_skb = if_sendbuf;
2029         card->interface.writecmd = NULL;
2030         card->interface.readstat = if_readstat;
2031         card->interface.features = ISDN_FEATURE_L2_HDLC |
2032                                    ISDN_FEATURE_L2_TRANS |
2033                                    ISDN_FEATURE_L3_TRANS |
2034                                    ISDN_FEATURE_P_UNKNOWN |
2035                                    ISDN_FEATURE_L2_X75I |
2036                                    ISDN_FEATURE_L2_X75UI |
2037                                    ISDN_FEATURE_L2_X75BUI;
2038         if (profp->support1 & (1<<2))
2039                 card->interface.features |= ISDN_FEATURE_L2_V11096 |
2040                                             ISDN_FEATURE_L2_V11019 |
2041                                             ISDN_FEATURE_L2_V11038;
2042         if (profp->support1 & (1<<8))
2043                 card->interface.features |= ISDN_FEATURE_L2_MODEM;
2044         card->interface.hl_hdrlen = 22; /* len of DATA_B3_REQ */
2045         strncpy(card->interface.id, id, sizeof(card->interface.id) - 1);
2046
2047
2048         card->q931_read = card->q931_buf;
2049         card->q931_write = card->q931_buf;
2050         card->q931_end = card->q931_buf + sizeof(card->q931_buf) - 1;
2051
2052         if (!register_isdn(&card->interface)) {
2053                 printk(KERN_ERR "capidrv: Unable to register contr %s\n", id);
2054                 kfree(card->bchans);
2055                 module_put(card->owner);
2056                 kfree(card);
2057                 return -1;
2058         }
2059         card->myid = card->interface.channels;
2060
2061         spin_lock_irqsave(&global_lock, flags);
2062         card->next = global.contr_list;
2063         global.contr_list = card;
2064         global.ncontr++;
2065         spin_unlock_irqrestore(&global_lock, flags);
2066
2067         memset(card->bchans, 0, sizeof(capidrv_bchan) * card->nbchan);
2068         for (i = 0; i < card->nbchan; i++) {
2069                 card->bchans[i].contr = card;
2070         }
2071
2072         cmd.command = ISDN_STAT_RUN;
2073         cmd.driver = card->myid;
2074         card->interface.statcallb(&cmd);
2075
2076         card->cipmask = 0x1FFF03FF;     /* any */
2077         card->cipmask2 = 0;
2078
2079         send_listen(card);
2080
2081         card->listentimer.data = (unsigned long)card;
2082         card->listentimer.function = listentimerfunc;
2083         mod_timer(&card->listentimer, jiffies + 60*HZ);
2084
2085         printk(KERN_INFO "%s: now up (%d B channels)\n",
2086                 card->name, card->nbchan);
2087
2088         enable_dchannel_trace(card);
2089
2090         return 0;
2091 }
2092
2093 static int capidrv_delcontr(u16 contr)
2094 {
2095         capidrv_contr **pp, *card;
2096         unsigned long flags;
2097         isdn_ctrl cmd;
2098
2099         spin_lock_irqsave(&global_lock, flags);
2100         for (card = global.contr_list; card; card = card->next) {
2101                 if (card->contrnr == contr)
2102                         break;
2103         }
2104         if (!card) {
2105                 spin_unlock_irqrestore(&global_lock, flags);
2106                 printk(KERN_ERR "capidrv: delcontr: no contr %u\n", contr);
2107                 return -1;
2108         }
2109         #warning FIXME: maybe a race condition the card should be removed here from global list /kkeil
2110         spin_unlock_irqrestore(&global_lock, flags);
2111
2112         del_timer(&card->listentimer);
2113
2114         if (debugmode)
2115                 printk(KERN_DEBUG "capidrv-%d: id=%d unloading\n",
2116                                         card->contrnr, card->myid);
2117
2118         cmd.command = ISDN_STAT_STOP;
2119         cmd.driver = card->myid;
2120         card->interface.statcallb(&cmd);
2121
2122         while (card->nbchan) {
2123
2124                 cmd.command = ISDN_STAT_DISCH;
2125                 cmd.driver = card->myid;
2126                 cmd.arg = card->nbchan-1;
2127                 cmd.parm.num[0] = 0;
2128                 if (debugmode)
2129                         printk(KERN_DEBUG "capidrv-%d: id=%d disable chan=%ld\n",
2130                                         card->contrnr, card->myid, cmd.arg);
2131                 card->interface.statcallb(&cmd);
2132
2133                 if (card->bchans[card->nbchan-1].nccip)
2134                         free_ncci(card, card->bchans[card->nbchan-1].nccip);
2135                 if (card->bchans[card->nbchan-1].plcip)
2136                         free_plci(card, card->bchans[card->nbchan-1].plcip);
2137                 if (card->plci_list)
2138                         printk(KERN_ERR "capidrv: bug in free_plci()\n");
2139                 card->nbchan--;
2140         }
2141         kfree(card->bchans);
2142         card->bchans = NULL;
2143
2144         if (debugmode)
2145                 printk(KERN_DEBUG "capidrv-%d: id=%d isdn unload\n",
2146                                         card->contrnr, card->myid);
2147
2148         cmd.command = ISDN_STAT_UNLOAD;
2149         cmd.driver = card->myid;
2150         card->interface.statcallb(&cmd);
2151
2152         if (debugmode)
2153                 printk(KERN_DEBUG "capidrv-%d: id=%d remove contr from list\n",
2154                                         card->contrnr, card->myid);
2155
2156         spin_lock_irqsave(&global_lock, flags);
2157         for (pp = &global.contr_list; *pp; pp = &(*pp)->next) {
2158                 if (*pp == card) {
2159                         *pp = (*pp)->next;
2160                         card->next = NULL;
2161                         global.ncontr--;
2162                         break;
2163                 }
2164         }
2165         spin_unlock_irqrestore(&global_lock, flags);
2166
2167         module_put(card->owner);
2168         printk(KERN_INFO "%s: now down.\n", card->name);
2169         kfree(card);
2170         return 0;
2171 }
2172
2173
2174 static void lower_callback(unsigned int cmd, u32 contr, void *data)
2175 {
2176
2177         switch (cmd) {
2178         case KCI_CONTRUP:
2179                 printk(KERN_INFO "capidrv: controller %hu up\n", contr);
2180                 (void) capidrv_addcontr(contr, (capi_profile *) data);
2181                 break;
2182         case KCI_CONTRDOWN:
2183                 printk(KERN_INFO "capidrv: controller %hu down\n", contr);
2184                 (void) capidrv_delcontr(contr);
2185                 break;
2186         }
2187 }
2188
2189 /*
2190  * /proc/capi/capidrv:
2191  * nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
2192  */
2193 static int proc_capidrv_read_proc(char *page, char **start, off_t off,
2194                                        int count, int *eof, void *data)
2195 {
2196         int len = 0;
2197
2198         len += sprintf(page+len, "%lu %lu %lu %lu\n",
2199                         global.ap.nrecvctlpkt,
2200                         global.ap.nrecvdatapkt,
2201                         global.ap.nsentctlpkt,
2202                         global.ap.nsentdatapkt);
2203         if (off+count >= len)
2204            *eof = 1;
2205         if (len < off)
2206            return 0;
2207         *start = page + off;
2208         return ((count < len-off) ? count : len-off);
2209 }
2210
2211 static struct procfsentries {
2212   char *name;
2213   mode_t mode;
2214   int (*read_proc)(char *page, char **start, off_t off,
2215                                        int count, int *eof, void *data);
2216   struct proc_dir_entry *procent;
2217 } procfsentries[] = {
2218    /* { "capi",           S_IFDIR, 0 }, */
2219    { "capi/capidrv",      0      , proc_capidrv_read_proc },
2220 };
2221
2222 static void __init proc_init(void)
2223 {
2224     int nelem = sizeof(procfsentries)/sizeof(procfsentries[0]);
2225     int i;
2226
2227     for (i=0; i < nelem; i++) {
2228         struct procfsentries *p = procfsentries + i;
2229         p->procent = create_proc_entry(p->name, p->mode, NULL);
2230         if (p->procent) p->procent->read_proc = p->read_proc;
2231     }
2232 }
2233
2234 static void __exit proc_exit(void)
2235 {
2236     int nelem = sizeof(procfsentries)/sizeof(procfsentries[0]);
2237     int i;
2238
2239     for (i=nelem-1; i >= 0; i--) {
2240         struct procfsentries *p = procfsentries + i;
2241         if (p->procent) {
2242            remove_proc_entry(p->name, NULL);
2243            p->procent = NULL;
2244         }
2245     }
2246 }
2247
2248 static int __init capidrv_init(void)
2249 {
2250         capi_profile profile;
2251         char rev[32];
2252         char *p;
2253         u32 ncontr, contr;
2254         u16 errcode;
2255
2256         if ((p = strchr(revision, ':')) != 0 && p[1]) {
2257                 strncpy(rev, p + 2, sizeof(rev));
2258                 rev[sizeof(rev)-1] = 0;
2259                 if ((p = strchr(rev, '$')) != 0 && p > rev)
2260                    *(p-1) = 0;
2261         } else
2262                 strcpy(rev, "1.0");
2263
2264         global.ap.rparam.level3cnt = -2;  /* number of bchannels twice */
2265         global.ap.rparam.datablkcnt = 16;
2266         global.ap.rparam.datablklen = 2048;
2267
2268         global.ap.recv_message = capidrv_recv_message;
2269         errcode = capi20_register(&global.ap);
2270         if (errcode) {
2271                 return -EIO;
2272         }
2273
2274         capi20_set_callback(&global.ap, lower_callback);
2275
2276         errcode = capi20_get_profile(0, &profile);
2277         if (errcode != CAPI_NOERROR) {
2278                 capi20_release(&global.ap);
2279                 return -EIO;
2280         }
2281
2282         ncontr = profile.ncontroller;
2283         for (contr = 1; contr <= ncontr; contr++) {
2284                 errcode = capi20_get_profile(contr, &profile);
2285                 if (errcode != CAPI_NOERROR)
2286                         continue;
2287                 (void) capidrv_addcontr(contr, &profile);
2288         }
2289         proc_init();
2290
2291         printk(KERN_NOTICE "capidrv: Rev %s: loaded\n", rev);
2292         return 0;
2293 }
2294
2295 static void __exit capidrv_exit(void)
2296 {
2297         char rev[10];
2298         char *p;
2299
2300         if ((p = strchr(revision, ':')) != 0) {
2301                 strcpy(rev, p + 1);
2302                 p = strchr(rev, '$');
2303                 *p = 0;
2304         } else {
2305                 strcpy(rev, " ??? ");
2306         }
2307
2308         capi20_release(&global.ap);
2309
2310         proc_exit();
2311
2312         printk(KERN_NOTICE "capidrv: Rev%s: unloaded\n", rev);
2313 }
2314
2315 module_init(capidrv_init);
2316 module_exit(capidrv_exit);