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