import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / drivers / isdn / hisax / callc.c
1 /* $Id: callc.c,v 1.1.1.1 2005/04/11 02:50:23 jack Exp $
2  *
3  * Author       Karsten Keil
4  * Copyright    by Karsten Keil      <keil@isdn4linux.de>
5  * 
6  * This software may be used and distributed according to the terms
7  * of the GNU General Public License, incorporated herein by reference.
8  *
9  * For changes and modifications please read
10  * ../../../Documentation/isdn/HiSax.cert
11  *
12  * based on the teles driver from Jan den Ouden
13  *
14  * Thanks to    Jan den Ouden
15  *              Fritz Elfert
16  *
17  */
18
19 #define __NO_VERSION__
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include "hisax.h"
23 #include "../avmb1/capicmd.h"  /* this should be moved in a common place */
24
25 #ifdef MODULE
26 #define MOD_USE_COUNT ( GET_USE_COUNT (&__this_module))
27 #endif  /* MODULE */
28
29 const char *lli_revision = "$Revision: 1.1.1.1 $";
30
31 extern struct IsdnCard cards[];
32 extern int nrcards;
33 extern void HiSax_mod_dec_use_count(struct IsdnCardState *cs);
34 extern void HiSax_mod_inc_use_count(struct IsdnCardState *cs);
35
36 static int init_b_st(struct Channel *chanp, int incoming);
37 static void release_b_st(struct Channel *chanp);
38
39 static struct Fsm callcfsm;
40 static int chancount;
41
42 /* experimental REJECT after ALERTING for CALLBACK to beat the 4s delay */
43 #define ALERT_REJECT 0
44
45 /* Value to delay the sending of the first B-channel paket after CONNECT
46  * here is no value given by ITU, but experience shows that 300 ms will
47  * work on many networks, if you or your other side is behind local exchanges
48  * a greater value may be recommented. If the delay is to short the first paket
49  * will be lost and autodetect on many comercial routers goes wrong !
50  * You can adjust this value on runtime with
51  * hisaxctrl <id> 2 <value>
52  * value is in milliseconds
53  */
54 #define DEFAULT_B_DELAY 300
55
56 /* Flags for remembering action done in lli */
57
58 #define  FLG_START_B    0
59
60 /*
61  * Find card with given driverId
62  */
63 static inline struct IsdnCardState *
64 hisax_findcard(int driverid)
65 {
66         int i;
67
68         for (i = 0; i < nrcards; i++)
69                 if (cards[i].cs)
70                         if (cards[i].cs->myid == driverid)
71                                 return (cards[i].cs);
72         return (struct IsdnCardState *) 0;
73 }
74
75 static void
76 link_debug(struct Channel *chanp, int direction, char *fmt, ...)
77 {
78         va_list args;
79         char tmp[16];
80
81         va_start(args, fmt);
82         sprintf(tmp, "Ch%d %s ", chanp->chan,
83                 direction ? "LL->HL" : "HL->LL");
84         VHiSax_putstatus(chanp->cs, tmp, fmt, args);
85         va_end(args);
86 }
87
88 enum {
89         ST_NULL,                /*  0 inactive */
90         ST_OUT_DIAL,            /*  1 outgoing, SETUP send; awaiting confirm */
91         ST_IN_WAIT_LL,          /*  2 incoming call received; wait for LL confirm */
92         ST_IN_ALERT_SENT,       /*  3 incoming call received; ALERT send */
93         ST_IN_WAIT_CONN_ACK,    /*  4 incoming CONNECT send; awaiting CONN_ACK */
94         ST_WAIT_BCONN,          /*  5 CONNECT/CONN_ACK received, awaiting b-channel prot. estbl. */
95         ST_ACTIVE,              /*  6 active, b channel prot. established */
96         ST_WAIT_BRELEASE,       /*  7 call clear. (initiator), awaiting b channel prot. rel. */
97         ST_WAIT_BREL_DISC,      /*  8 call clear. (receiver), DISCONNECT req. received */
98         ST_WAIT_DCOMMAND,       /*  9 call clear. (receiver), awaiting DCHANNEL message */
99         ST_WAIT_DRELEASE,       /* 10 DISCONNECT sent, awaiting RELEASE */
100         ST_WAIT_D_REL_CNF,      /* 11 RELEASE sent, awaiting RELEASE confirm */
101         ST_IN_PROCEED_SEND,     /* 12 incoming call, proceeding send */ 
102 };
103   
104
105 #define STATE_COUNT (ST_IN_PROCEED_SEND + 1)
106
107 static char *strState[] =
108 {
109         "ST_NULL",
110         "ST_OUT_DIAL",
111         "ST_IN_WAIT_LL",
112         "ST_IN_ALERT_SENT",
113         "ST_IN_WAIT_CONN_ACK",
114         "ST_WAIT_BCONN",
115         "ST_ACTIVE",
116         "ST_WAIT_BRELEASE",
117         "ST_WAIT_BREL_DISC",
118         "ST_WAIT_DCOMMAND",
119         "ST_WAIT_DRELEASE",
120         "ST_WAIT_D_REL_CNF",
121         "ST_IN_PROCEED_SEND",
122 };
123
124 enum {
125         EV_DIAL,                /*  0 */
126         EV_SETUP_CNF,           /*  1 */
127         EV_ACCEPTB,             /*  2 */
128         EV_DISCONNECT_IND,      /*  3 */
129         EV_RELEASE,             /*  4 */
130         EV_LEASED,              /*  5 */
131         EV_LEASED_REL,          /*  6 */
132         EV_SETUP_IND,           /*  7 */
133         EV_ACCEPTD,             /*  8 */
134         EV_SETUP_CMPL_IND,      /*  9 */
135         EV_BC_EST,              /* 10 */
136         EV_WRITEBUF,            /* 11 */
137         EV_HANGUP,              /* 12 */
138         EV_BC_REL,              /* 13 */
139         EV_CINF,                /* 14 */
140         EV_SUSPEND,             /* 15 */
141         EV_RESUME,              /* 16 */
142         EV_NOSETUP_RSP,         /* 17 */
143         EV_SETUP_ERR,           /* 18 */
144         EV_CONNECT_ERR,         /* 19 */
145         EV_PROCEED,             /* 20 */
146         EV_ALERT,               /* 21 */ 
147         EV_REDIR,               /* 22 */ 
148 };
149
150 #define EVENT_COUNT (EV_REDIR + 1)
151
152 static char *strEvent[] =
153 {
154         "EV_DIAL",
155         "EV_SETUP_CNF",
156         "EV_ACCEPTB",
157         "EV_DISCONNECT_IND",
158         "EV_RELEASE",
159         "EV_LEASED",
160         "EV_LEASED_REL",
161         "EV_SETUP_IND",
162         "EV_ACCEPTD",
163         "EV_SETUP_CMPL_IND",
164         "EV_BC_EST",
165         "EV_WRITEBUF",
166         "EV_HANGUP",
167         "EV_BC_REL",
168         "EV_CINF",
169         "EV_SUSPEND",
170         "EV_RESUME",
171         "EV_NOSETUP_RSP",
172         "EV_SETUP_ERR",
173         "EV_CONNECT_ERR",
174         "EV_PROCEED",
175         "EV_ALERT",
176         "EV_REDIR",
177 };
178
179
180 static inline void
181 HL_LL(struct Channel *chanp, int command)
182 {
183         isdn_ctrl ic;
184
185         ic.driver = chanp->cs->myid;
186         ic.command = command;
187         ic.arg = chanp->chan;
188         chanp->cs->iif.statcallb(&ic);
189 }
190
191 static inline void
192 lli_deliver_cause(struct Channel *chanp)
193 {
194         isdn_ctrl ic;
195
196         if (!chanp->proc)
197                 return;
198         if (chanp->proc->para.cause == NO_CAUSE)
199                 return;
200         ic.driver = chanp->cs->myid;
201         ic.command = ISDN_STAT_CAUSE;
202         ic.arg = chanp->chan;
203         if (chanp->cs->protocol == ISDN_PTYPE_EURO)
204                 sprintf(ic.parm.num, "E%02X%02X", chanp->proc->para.loc & 0x7f,
205                         chanp->proc->para.cause & 0x7f);
206         else
207                 sprintf(ic.parm.num, "%02X%02X", chanp->proc->para.loc & 0x7f,
208                         chanp->proc->para.cause & 0x7f);
209         chanp->cs->iif.statcallb(&ic);
210 }
211
212 static inline void
213 lli_close(struct FsmInst *fi)
214 {
215         struct Channel *chanp = fi->userdata;
216
217         FsmChangeState(fi, ST_NULL);
218         chanp->Flags = 0;
219         chanp->cs->cardmsg(chanp->cs, MDL_INFO_REL, (void *) (long)chanp->chan);
220 }
221
222 static void
223 lli_leased_in(struct FsmInst *fi, int event, void *arg)
224 {
225         struct Channel *chanp = fi->userdata;
226         isdn_ctrl ic;
227         int ret;
228
229         if (!chanp->leased)
230                 return;
231         chanp->cs->cardmsg(chanp->cs, MDL_INFO_SETUP, (void *) (long)chanp->chan);
232         FsmChangeState(fi, ST_IN_WAIT_LL);
233         if (chanp->debug & 1)
234                 link_debug(chanp, 0, "STAT_ICALL_LEASED");
235         ic.driver = chanp->cs->myid;
236         ic.command = ((chanp->chan < 2) ? ISDN_STAT_ICALL : ISDN_STAT_ICALLW);
237         ic.arg = chanp->chan;
238         ic.parm.setup.si1 = 7;
239         ic.parm.setup.si2 = 0;
240         ic.parm.setup.plan = 0;
241         ic.parm.setup.screen = 0;
242         sprintf(ic.parm.setup.eazmsn,"%d", chanp->chan + 1);
243         sprintf(ic.parm.setup.phone,"LEASED%d", chanp->cs->myid);
244         ret = chanp->cs->iif.statcallb(&ic);
245         if (chanp->debug & 1)
246                 link_debug(chanp, 1, "statcallb ret=%d", ret);
247         if (!ret) {
248                 chanp->cs->cardmsg(chanp->cs, MDL_INFO_REL, (void *) (long)chanp->chan);
249                 FsmChangeState(fi, ST_NULL);
250         }
251 }
252
253
254 /*
255  * Dial out
256  */
257 static void
258 lli_init_bchan_out(struct FsmInst *fi, int event, void *arg)
259 {
260         struct Channel *chanp = fi->userdata;
261
262         FsmChangeState(fi, ST_WAIT_BCONN);
263         if (chanp->debug & 1)
264                 link_debug(chanp, 0, "STAT_DCONN");
265         HL_LL(chanp, ISDN_STAT_DCONN);
266         init_b_st(chanp, 0);
267         chanp->b_st->lli.l4l3(chanp->b_st, DL_ESTABLISH | REQUEST, NULL);
268 }
269
270 static void
271 lli_prep_dialout(struct FsmInst *fi, int event, void *arg)
272 {
273         struct Channel *chanp = fi->userdata;
274
275         FsmDelTimer(&chanp->drel_timer, 60);
276         FsmDelTimer(&chanp->dial_timer, 73);
277         chanp->l2_active_protocol = chanp->l2_protocol;
278         chanp->incoming = 0;
279         chanp->cs->cardmsg(chanp->cs, MDL_INFO_SETUP, (void *) (long)chanp->chan);
280         if (chanp->leased) {
281                 lli_init_bchan_out(fi, event, arg);
282         } else {
283                 FsmChangeState(fi, ST_OUT_DIAL);
284                 chanp->d_st->lli.l4l3(chanp->d_st, CC_SETUP | REQUEST, chanp);
285         }
286 }
287
288 static void
289 lli_resume(struct FsmInst *fi, int event, void *arg)
290 {
291         struct Channel *chanp = fi->userdata;
292
293         FsmDelTimer(&chanp->drel_timer, 60);
294         FsmDelTimer(&chanp->dial_timer, 73);
295         chanp->l2_active_protocol = chanp->l2_protocol;
296         chanp->incoming = 0;
297         chanp->cs->cardmsg(chanp->cs, MDL_INFO_SETUP, (void *) (long)chanp->chan);
298         if (chanp->leased) {
299                 lli_init_bchan_out(fi, event, arg);
300         } else {
301                 FsmChangeState(fi, ST_OUT_DIAL);
302                 chanp->d_st->lli.l4l3(chanp->d_st, CC_RESUME | REQUEST, chanp);
303         }
304 }
305
306 static void
307 lli_go_active(struct FsmInst *fi, int event, void *arg)
308 {
309         struct Channel *chanp = fi->userdata;
310         isdn_ctrl ic;
311
312
313         FsmChangeState(fi, ST_ACTIVE);
314         chanp->data_open = !0;
315         if (chanp->bcs->conmsg)
316                 strcpy(ic.parm.num, chanp->bcs->conmsg);
317         else
318                 ic.parm.num[0] = 0;
319         if (chanp->debug & 1)
320                 link_debug(chanp, 0, "STAT_BCONN %s", ic.parm.num);
321         ic.driver = chanp->cs->myid;
322         ic.command = ISDN_STAT_BCONN;
323         ic.arg = chanp->chan;
324         chanp->cs->iif.statcallb(&ic);
325         chanp->cs->cardmsg(chanp->cs, MDL_INFO_CONN, (void *) (long)chanp->chan);
326 }
327
328
329 /*
330  * RESUME
331  */
332
333 /* incoming call */
334
335 static void
336 lli_deliver_call(struct FsmInst *fi, int event, void *arg)
337 {
338         struct Channel *chanp = fi->userdata;
339         isdn_ctrl ic;
340         int ret;
341
342         chanp->cs->cardmsg(chanp->cs, MDL_INFO_SETUP, (void *) (long)chanp->chan);
343         /*
344          * Report incoming calls only once to linklevel, use CallFlags
345          * which is set to 3 with each broadcast message in isdnl1.c
346          * and resetted if a interface  answered the STAT_ICALL.
347          */
348         if (1) { /* for only one TEI */
349                 FsmChangeState(fi, ST_IN_WAIT_LL);
350                 if (chanp->debug & 1)
351                         link_debug(chanp, 0, (chanp->chan < 2) ? "STAT_ICALL" : "STAT_ICALLW");
352                 ic.driver = chanp->cs->myid;
353                 ic.command = ((chanp->chan < 2) ? ISDN_STAT_ICALL : ISDN_STAT_ICALLW);
354
355                 ic.arg = chanp->chan;
356                 /*
357                  * No need to return "unknown" for calls without OAD,
358                  * cause that's handled in linklevel now (replaced by '0')
359                  */
360                 memcpy(&ic.parm.setup, &chanp->proc->para.setup, sizeof(setup_parm));
361                 ret = chanp->cs->iif.statcallb(&ic);
362                 if (chanp->debug & 1)
363                         link_debug(chanp, 1, "statcallb ret=%d", ret);
364
365                 switch (ret) {
366                         case 1: /* OK, someone likes this call */
367                                 FsmDelTimer(&chanp->drel_timer, 61);
368                                 FsmChangeState(fi, ST_IN_ALERT_SENT);
369                                 chanp->d_st->lli.l4l3(chanp->d_st, CC_ALERTING | REQUEST, chanp->proc);
370                                 break;
371                         case 5: /* direct redirect */
372                         case 4: /* Proceeding desired */
373                                 FsmDelTimer(&chanp->drel_timer, 61);
374                                 FsmChangeState(fi, ST_IN_PROCEED_SEND);
375                                 chanp->d_st->lli.l4l3(chanp->d_st, CC_PROCEED_SEND | REQUEST, chanp->proc);
376                                 if (ret == 5) {
377                                         memcpy(&chanp->setup, &ic.parm.setup, sizeof(setup_parm));
378                                         chanp->d_st->lli.l4l3(chanp->d_st, CC_REDIR | REQUEST, chanp->proc);
379                                 }
380                                 break;
381                         case 2: /* Rejecting Call */
382                                 break;
383                         case 3: /* incomplete number */
384                                 FsmDelTimer(&chanp->drel_timer, 61);
385                                 chanp->d_st->lli.l4l3(chanp->d_st, CC_MORE_INFO | REQUEST, chanp->proc);
386                                 break;
387                         case 0: /* OK, nobody likes this call */
388                         default:        /* statcallb problems */
389                                 chanp->d_st->lli.l4l3(chanp->d_st, CC_IGNORE | REQUEST, chanp->proc);
390                                 chanp->cs->cardmsg(chanp->cs, MDL_INFO_REL, (void *) (long)chanp->chan);
391                                 FsmChangeState(fi, ST_NULL);
392                                 break;
393                 }
394         } else {
395                 chanp->d_st->lli.l4l3(chanp->d_st, CC_IGNORE | REQUEST, chanp->proc);
396                 chanp->cs->cardmsg(chanp->cs, MDL_INFO_REL, (void *) (long)chanp->chan);
397         }
398 }
399
400 static void
401 lli_send_dconnect(struct FsmInst *fi, int event, void *arg)
402 {
403         struct Channel *chanp = fi->userdata;
404
405         FsmChangeState(fi, ST_IN_WAIT_CONN_ACK);
406         chanp->d_st->lli.l4l3(chanp->d_st, CC_SETUP | RESPONSE, chanp->proc);
407 }
408
409 static void
410 lli_send_alert(struct FsmInst *fi, int event, void *arg)
411 {
412         struct Channel *chanp = fi->userdata;
413
414         FsmChangeState(fi, ST_IN_ALERT_SENT);
415         chanp->d_st->lli.l4l3(chanp->d_st, CC_ALERTING | REQUEST, chanp->proc);
416 }
417
418 static void
419 lli_send_redir(struct FsmInst *fi, int event, void *arg)
420 {
421         struct Channel *chanp = fi->userdata;
422
423         chanp->d_st->lli.l4l3(chanp->d_st, CC_REDIR | REQUEST, chanp->proc);
424 }
425
426 static void
427 lli_init_bchan_in(struct FsmInst *fi, int event, void *arg)
428 {
429         struct Channel *chanp = fi->userdata;
430
431         FsmChangeState(fi, ST_WAIT_BCONN);
432         if (chanp->debug & 1)
433                 link_debug(chanp, 0, "STAT_DCONN");
434         HL_LL(chanp, ISDN_STAT_DCONN);
435         chanp->l2_active_protocol = chanp->l2_protocol;
436         chanp->incoming = !0;
437         init_b_st(chanp, !0);
438         chanp->b_st->lli.l4l3(chanp->b_st, DL_ESTABLISH | REQUEST, NULL);
439 }
440
441 static void
442 lli_setup_rsp(struct FsmInst *fi, int event, void *arg)
443 {
444         struct Channel *chanp = fi->userdata;
445
446         if (chanp->leased) {
447                 lli_init_bchan_in(fi, event, arg);
448         } else {
449                 FsmChangeState(fi, ST_IN_WAIT_CONN_ACK);
450 #ifdef WANT_ALERT
451                 chanp->d_st->lli.l4l3(chanp->d_st, CC_ALERTING | REQUEST, chanp->proc);
452 #endif
453                 chanp->d_st->lli.l4l3(chanp->d_st, CC_SETUP | RESPONSE, chanp->proc);
454         }
455 }
456
457 /* Call suspend */
458
459 static void
460 lli_suspend(struct FsmInst *fi, int event, void *arg)
461 {
462         struct Channel *chanp = fi->userdata;
463
464         chanp->d_st->lli.l4l3(chanp->d_st, CC_SUSPEND | REQUEST, chanp->proc);
465 }
466
467 /* Call clearing */
468
469 static void
470 lli_leased_hup(struct FsmInst *fi, struct Channel *chanp)
471 {
472         isdn_ctrl ic;
473
474         ic.driver = chanp->cs->myid;
475         ic.command = ISDN_STAT_CAUSE;
476         ic.arg = chanp->chan;
477         sprintf(ic.parm.num, "L0010");
478         chanp->cs->iif.statcallb(&ic);
479         if (chanp->debug & 1)
480                 link_debug(chanp, 0, "STAT_DHUP");
481         HL_LL(chanp, ISDN_STAT_DHUP);
482         lli_close(fi);
483 }
484
485 static void
486 lli_disconnect_req(struct FsmInst *fi, int event, void *arg)
487 {
488         struct Channel *chanp = fi->userdata;
489
490         if (chanp->leased) {
491                 lli_leased_hup(fi, chanp);
492         } else {
493                 FsmChangeState(fi, ST_WAIT_DRELEASE);
494                 if (chanp->proc)
495                         chanp->proc->para.cause = 0x10; /* Normal Call Clearing */
496                 chanp->d_st->lli.l4l3(chanp->d_st, CC_DISCONNECT | REQUEST,
497                         chanp->proc);
498         }
499 }
500
501 static void
502 lli_disconnect_reject(struct FsmInst *fi, int event, void *arg)
503 {
504         struct Channel *chanp = fi->userdata;
505
506         if (chanp->leased) {
507                 lli_leased_hup(fi, chanp);
508         } else {
509                 FsmChangeState(fi, ST_WAIT_DRELEASE);
510                 if (chanp->proc)
511                         chanp->proc->para.cause = 0x15; /* Call Rejected */
512                 chanp->d_st->lli.l4l3(chanp->d_st, CC_DISCONNECT | REQUEST,
513                         chanp->proc);
514         }
515 }
516
517 static void
518 lli_dhup_close(struct FsmInst *fi, int event, void *arg)
519 {
520         struct Channel *chanp = fi->userdata;
521
522         if (chanp->leased) {
523                 lli_leased_hup(fi, chanp);
524         } else {
525                 if (chanp->debug & 1)
526                         link_debug(chanp, 0, "STAT_DHUP");
527                 lli_deliver_cause(chanp);
528                 HL_LL(chanp, ISDN_STAT_DHUP);
529                 lli_close(fi);
530         }
531 }
532
533 static void
534 lli_reject_req(struct FsmInst *fi, int event, void *arg)
535 {
536         struct Channel *chanp = fi->userdata;
537
538         if (chanp->leased) {
539                 lli_leased_hup(fi, chanp);
540                 return;
541         }
542 #ifndef ALERT_REJECT
543         if (chanp->proc)
544                 chanp->proc->para.cause = 0x15; /* Call Rejected */
545         chanp->d_st->lli.l4l3(chanp->d_st, CC_REJECT | REQUEST, chanp->proc);
546         lli_dhup_close(fi, event, arg);
547 #else
548         FsmRestartTimer(&chanp->drel_timer, 40, EV_HANGUP, NULL, 63);
549         FsmChangeState(fi, ST_IN_ALERT_SENT);
550         chanp->d_st->lli.l4l3(chanp->d_st, CC_ALERTING | REQUEST, chanp->proc);
551 #endif
552 }
553
554 static void
555 lli_disconn_bchan(struct FsmInst *fi, int event, void *arg)
556 {
557         struct Channel *chanp = fi->userdata;
558
559         chanp->data_open = 0;
560         FsmChangeState(fi, ST_WAIT_BRELEASE);
561         chanp->b_st->lli.l4l3(chanp->b_st, DL_RELEASE | REQUEST, NULL);
562 }
563
564 static void
565 lli_start_disc(struct FsmInst *fi, int event, void *arg)
566 {
567         struct Channel *chanp = fi->userdata;
568
569         if (chanp->leased) {
570                 lli_leased_hup(fi, chanp);
571         } else {
572                 lli_disconnect_req(fi, event, arg);
573         }
574 }
575
576 static void
577 lli_rel_b_disc(struct FsmInst *fi, int event, void *arg)
578 {
579         struct Channel *chanp = fi->userdata;
580
581         release_b_st(chanp);
582         lli_start_disc(fi, event, arg);
583 }
584
585 static void
586 lli_bhup_disc(struct FsmInst *fi, int event, void *arg)
587 {
588         struct Channel *chanp = fi->userdata;
589  
590         if (chanp->debug & 1)
591                 link_debug(chanp, 0, "STAT_BHUP");
592         HL_LL(chanp, ISDN_STAT_BHUP);
593         lli_rel_b_disc(fi, event, arg);
594 }
595
596 static void
597 lli_bhup_rel_b(struct FsmInst *fi, int event, void *arg)
598 {
599         struct Channel *chanp = fi->userdata;
600
601         FsmChangeState(fi, ST_WAIT_DCOMMAND);
602         chanp->data_open = 0;
603         if (chanp->debug & 1)
604                 link_debug(chanp, 0, "STAT_BHUP");
605         HL_LL(chanp, ISDN_STAT_BHUP);
606         release_b_st(chanp);
607 }
608
609 static void
610 lli_release_bchan(struct FsmInst *fi, int event, void *arg)
611 {
612         struct Channel *chanp = fi->userdata;
613
614         chanp->data_open = 0;
615         FsmChangeState(fi, ST_WAIT_BREL_DISC);
616         chanp->b_st->lli.l4l3(chanp->b_st, DL_RELEASE | REQUEST, NULL);
617 }
618
619
620 static void
621 lli_rel_b_dhup(struct FsmInst *fi, int event, void *arg)
622 {
623         struct Channel *chanp = fi->userdata;
624
625         release_b_st(chanp);
626         lli_dhup_close(fi, event, arg);
627 }
628
629 static void
630 lli_bhup_dhup(struct FsmInst *fi, int event, void *arg)
631 {
632         struct Channel *chanp = fi->userdata;
633
634         if (chanp->debug & 1)
635                 link_debug(chanp, 0, "STAT_BHUP");
636         HL_LL(chanp, ISDN_STAT_BHUP);
637         lli_rel_b_dhup(fi, event, arg);
638 }
639
640 static void
641 lli_abort(struct FsmInst *fi, int event, void *arg)
642 {
643         struct Channel *chanp = fi->userdata;
644
645         chanp->data_open = 0;
646         chanp->b_st->lli.l4l3(chanp->b_st, DL_RELEASE | REQUEST, NULL);
647         lli_bhup_dhup(fi, event, arg);
648 }
649  
650 static void
651 lli_release_req(struct FsmInst *fi, int event, void *arg)
652 {
653         struct Channel *chanp = fi->userdata;
654
655         if (chanp->leased) {
656                 lli_leased_hup(fi, chanp);
657         } else {
658                 FsmChangeState(fi, ST_WAIT_D_REL_CNF);
659                 chanp->d_st->lli.l4l3(chanp->d_st, CC_RELEASE | REQUEST,
660                         chanp->proc);
661         }
662 }
663
664 static void
665 lli_rel_b_release_req(struct FsmInst *fi, int event, void *arg)
666 {
667         struct Channel *chanp = fi->userdata;
668
669         release_b_st(chanp);
670         lli_release_req(fi, event, arg);
671 }
672
673 static void
674 lli_bhup_release_req(struct FsmInst *fi, int event, void *arg)
675 {
676         struct Channel *chanp = fi->userdata;
677  
678         if (chanp->debug & 1)
679                 link_debug(chanp, 0, "STAT_BHUP");
680         HL_LL(chanp, ISDN_STAT_BHUP);
681         lli_rel_b_release_req(fi, event, arg);
682 }
683
684
685 /* processing charge info */
686 static void
687 lli_charge_info(struct FsmInst *fi, int event, void *arg)
688 {
689         struct Channel *chanp = fi->userdata;
690         isdn_ctrl ic;
691
692         ic.driver = chanp->cs->myid;
693         ic.command = ISDN_STAT_CINF;
694         ic.arg = chanp->chan;
695         sprintf(ic.parm.num, "%d", chanp->proc->para.chargeinfo);
696         chanp->cs->iif.statcallb(&ic);
697 }
698
699 /* error procedures */
700
701 static void
702 lli_dchan_not_ready(struct FsmInst *fi, int event, void *arg)
703 {
704         struct Channel *chanp = fi->userdata;
705
706         if (chanp->debug & 1)
707                 link_debug(chanp, 0, "STAT_DHUP");
708         HL_LL(chanp, ISDN_STAT_DHUP); 
709 }
710
711 static void
712 lli_no_setup_rsp(struct FsmInst *fi, int event, void *arg)
713 {
714         struct Channel *chanp = fi->userdata;
715
716         if (chanp->debug & 1)
717                 link_debug(chanp, 0, "STAT_DHUP");
718         HL_LL(chanp, ISDN_STAT_DHUP);
719         lli_close(fi); 
720 }
721
722 static void
723 lli_error(struct FsmInst *fi, int event, void *arg)
724 {
725         FsmChangeState(fi, ST_WAIT_DRELEASE);
726 }
727
728 static void
729 lli_failure_l(struct FsmInst *fi, int event, void *arg)
730 {
731         struct Channel *chanp = fi->userdata;
732         isdn_ctrl ic;
733
734         FsmChangeState(fi, ST_NULL);
735         ic.driver = chanp->cs->myid;
736         ic.command = ISDN_STAT_CAUSE;
737         ic.arg = chanp->chan;
738         sprintf(ic.parm.num, "L%02X%02X", 0, 0x2f);
739         chanp->cs->iif.statcallb(&ic);
740         HL_LL(chanp, ISDN_STAT_DHUP);
741         chanp->Flags = 0;
742         chanp->cs->cardmsg(chanp->cs, MDL_INFO_REL, (void *) (long)chanp->chan);
743 }
744
745 static void
746 lli_rel_b_fail(struct FsmInst *fi, int event, void *arg)
747 {
748         struct Channel *chanp = fi->userdata;
749
750         release_b_st(chanp);
751         lli_failure_l(fi, event, arg);
752 }
753
754 static void
755 lli_bhup_fail(struct FsmInst *fi, int event, void *arg)
756 {
757         struct Channel *chanp = fi->userdata;
758
759         if (chanp->debug & 1)
760                 link_debug(chanp, 0, "STAT_BHUP");
761         HL_LL(chanp, ISDN_STAT_BHUP);
762         lli_rel_b_fail(fi, event, arg);
763 }
764
765 static void
766 lli_failure_a(struct FsmInst *fi, int event, void *arg)
767 {
768         struct Channel *chanp = fi->userdata;
769
770         chanp->data_open = 0;
771         chanp->b_st->lli.l4l3(chanp->b_st, DL_RELEASE | REQUEST, NULL);
772         lli_bhup_fail(fi, event, arg);
773 }
774
775 /* *INDENT-OFF* */
776 static struct FsmNode fnlist[] __initdata =
777 {
778         {ST_NULL,               EV_DIAL,                lli_prep_dialout},
779         {ST_NULL,               EV_RESUME,              lli_resume},
780         {ST_NULL,               EV_SETUP_IND,           lli_deliver_call},
781         {ST_NULL,               EV_LEASED,              lli_leased_in},
782         {ST_OUT_DIAL,           EV_SETUP_CNF,           lli_init_bchan_out},
783         {ST_OUT_DIAL,           EV_HANGUP,              lli_disconnect_req},
784         {ST_OUT_DIAL,           EV_DISCONNECT_IND,      lli_release_req},
785         {ST_OUT_DIAL,           EV_RELEASE,             lli_dhup_close},
786         {ST_OUT_DIAL,           EV_NOSETUP_RSP,         lli_no_setup_rsp},
787         {ST_OUT_DIAL,           EV_SETUP_ERR,           lli_error},
788         {ST_IN_WAIT_LL,         EV_LEASED_REL,          lli_failure_l},
789         {ST_IN_WAIT_LL,         EV_ACCEPTD,             lli_setup_rsp},
790         {ST_IN_WAIT_LL,         EV_HANGUP,              lli_reject_req},
791         {ST_IN_WAIT_LL,         EV_DISCONNECT_IND,      lli_release_req},
792         {ST_IN_WAIT_LL,         EV_RELEASE,             lli_dhup_close},
793         {ST_IN_WAIT_LL,         EV_SETUP_IND,           lli_deliver_call},
794         {ST_IN_WAIT_LL,         EV_SETUP_ERR,           lli_error},
795         {ST_IN_ALERT_SENT,      EV_SETUP_CMPL_IND,      lli_init_bchan_in},
796         {ST_IN_ALERT_SENT,      EV_ACCEPTD,             lli_send_dconnect},
797         {ST_IN_ALERT_SENT,      EV_HANGUP,              lli_disconnect_reject},
798         {ST_IN_ALERT_SENT,      EV_DISCONNECT_IND,      lli_release_req},
799         {ST_IN_ALERT_SENT,      EV_RELEASE,             lli_dhup_close},
800         {ST_IN_ALERT_SENT,      EV_REDIR,               lli_send_redir},
801         {ST_IN_PROCEED_SEND,    EV_REDIR,               lli_send_redir},
802         {ST_IN_PROCEED_SEND,    EV_ALERT,               lli_send_alert},
803         {ST_IN_PROCEED_SEND,    EV_ACCEPTD,             lli_send_dconnect},
804         {ST_IN_PROCEED_SEND,    EV_HANGUP,              lli_disconnect_reject},
805         {ST_IN_PROCEED_SEND,    EV_DISCONNECT_IND,      lli_dhup_close},
806         {ST_IN_ALERT_SENT,      EV_RELEASE,             lli_dhup_close},
807         {ST_IN_WAIT_CONN_ACK,   EV_SETUP_CMPL_IND,      lli_init_bchan_in},
808         {ST_IN_WAIT_CONN_ACK,   EV_HANGUP,              lli_disconnect_req},
809         {ST_IN_WAIT_CONN_ACK,   EV_DISCONNECT_IND,      lli_release_req},
810         {ST_IN_WAIT_CONN_ACK,   EV_RELEASE,             lli_dhup_close},
811         {ST_IN_WAIT_CONN_ACK,   EV_CONNECT_ERR,         lli_error},
812         {ST_WAIT_BCONN,         EV_BC_EST,              lli_go_active},
813         {ST_WAIT_BCONN,         EV_BC_REL,              lli_rel_b_disc},
814         {ST_WAIT_BCONN,         EV_HANGUP,              lli_rel_b_disc},
815         {ST_WAIT_BCONN,         EV_DISCONNECT_IND,      lli_rel_b_release_req},
816         {ST_WAIT_BCONN,         EV_RELEASE,             lli_rel_b_dhup},
817         {ST_WAIT_BCONN,         EV_LEASED_REL,          lli_rel_b_fail},
818         {ST_WAIT_BCONN,         EV_CINF,                lli_charge_info},
819         {ST_ACTIVE,             EV_CINF,                lli_charge_info},
820         {ST_ACTIVE,             EV_BC_REL,              lli_bhup_rel_b},
821         {ST_ACTIVE,             EV_SUSPEND,             lli_suspend},
822         {ST_ACTIVE,             EV_HANGUP,              lli_disconn_bchan},
823         {ST_ACTIVE,             EV_DISCONNECT_IND,      lli_release_bchan},
824         {ST_ACTIVE,             EV_RELEASE,             lli_abort},
825         {ST_ACTIVE,             EV_LEASED_REL,          lli_failure_a},
826         {ST_WAIT_BRELEASE,      EV_BC_REL,              lli_bhup_disc},
827         {ST_WAIT_BRELEASE,      EV_DISCONNECT_IND,      lli_bhup_release_req},
828         {ST_WAIT_BRELEASE,      EV_RELEASE,             lli_bhup_dhup},
829         {ST_WAIT_BRELEASE,      EV_LEASED_REL,          lli_bhup_fail},
830         {ST_WAIT_BREL_DISC,     EV_BC_REL,              lli_bhup_release_req},
831         {ST_WAIT_BREL_DISC,     EV_RELEASE,             lli_bhup_dhup},
832         {ST_WAIT_DCOMMAND,      EV_HANGUP,              lli_start_disc},
833         {ST_WAIT_DCOMMAND,      EV_DISCONNECT_IND,      lli_release_req},
834         {ST_WAIT_DCOMMAND,      EV_RELEASE,             lli_dhup_close},
835         {ST_WAIT_DCOMMAND,      EV_LEASED_REL,          lli_failure_l},
836         {ST_WAIT_DRELEASE,      EV_RELEASE,             lli_dhup_close},
837         {ST_WAIT_DRELEASE,      EV_DIAL,                lli_dchan_not_ready},
838   /* ETS 300-104 16.1 */
839         {ST_WAIT_D_REL_CNF,     EV_RELEASE,             lli_dhup_close},
840         {ST_WAIT_D_REL_CNF,     EV_DIAL,                lli_dchan_not_ready},
841 };
842 /* *INDENT-ON* */
843
844 #define FNCOUNT (sizeof(fnlist)/sizeof(struct FsmNode))
845
846 int __init
847 CallcNew(void)
848 {
849         callcfsm.state_count = STATE_COUNT;
850         callcfsm.event_count = EVENT_COUNT;
851         callcfsm.strEvent = strEvent;
852         callcfsm.strState = strState;
853         return FsmNew(&callcfsm, fnlist, FNCOUNT);
854 }
855
856 void
857 CallcFree(void)
858 {
859         FsmFree(&callcfsm);
860 }
861
862 static void
863 release_b_st(struct Channel *chanp)
864 {
865         struct PStack *st = chanp->b_st;
866
867         if(test_and_clear_bit(FLG_START_B, &chanp->Flags)) {
868                 chanp->bcs->BC_Close(chanp->bcs);
869                 switch (chanp->l2_active_protocol) {
870                         case (ISDN_PROTO_L2_X75I):
871                                 releasestack_isdnl2(st);
872                                 break;
873                         case (ISDN_PROTO_L2_HDLC):
874                         case (ISDN_PROTO_L2_HDLC_56K):
875                         case (ISDN_PROTO_L2_TRANS):
876                         case (ISDN_PROTO_L2_MODEM):
877                         case (ISDN_PROTO_L2_FAX):
878                                 releasestack_transl2(st);
879                                 break;
880                 }
881         } 
882 }
883
884 struct Channel
885 *selectfreechannel(struct PStack *st, int bch)
886 {
887         struct IsdnCardState *cs = st->l1.hardware;
888         struct Channel *chanp = st->lli.userdata;
889         int i;
890
891         if (test_bit(FLG_TWO_DCHAN, &cs->HW_Flags))
892                 i=1;
893         else
894                 i=0;
895
896         if (!bch) {
897                 i = 2; /* virtual channel */
898                 chanp += 2;
899         }
900
901         while (i < ((bch) ? cs->chanlimit : (2 + MAX_WAITING_CALLS))) {
902                 if (chanp->fi.state == ST_NULL)
903                         return (chanp);
904                 chanp++;
905                 i++;
906         }
907
908         if (bch) /* number of channels is limited */ {
909                 i = 2; /* virtual channel */
910                 chanp = st->lli.userdata;
911                 chanp += i;
912                 while (i < (2 + MAX_WAITING_CALLS)) {
913                         if (chanp->fi.state == ST_NULL)
914                                 return (chanp);
915                         chanp++;
916                         i++;
917                 }
918         }
919         return (NULL);
920 }
921
922 static void stat_redir_result(struct IsdnCardState *cs, int chan, ulong result)
923 {       isdn_ctrl ic;
924   
925         ic.driver = cs->myid;
926         ic.command = ISDN_STAT_REDIR;
927         ic.arg = chan; 
928         (ulong)(ic.parm.num[0]) = result;
929         cs->iif.statcallb(&ic);
930 } /* stat_redir_result */
931
932 static void
933 dchan_l3l4(struct PStack *st, int pr, void *arg)
934 {
935         struct l3_process *pc = arg;
936         struct IsdnCardState *cs = st->l1.hardware;
937         struct Channel *chanp;
938
939         if(!pc)
940                 return;
941
942         if (pr == (CC_SETUP | INDICATION)) {
943                 if (!(chanp = selectfreechannel(pc->st, pc->para.bchannel))) {
944                         pc->para.cause = 0x11;  /* User busy */
945                         pc->st->lli.l4l3(pc->st, CC_REJECT | REQUEST, pc);
946                 } else {
947                         chanp->proc = pc;
948                         pc->chan = chanp;
949                         FsmEvent(&chanp->fi, EV_SETUP_IND, NULL);
950                 }
951                 return;
952         }
953         if (!(chanp = pc->chan))
954                 return;
955
956         switch (pr) {
957                 case (CC_MORE_INFO | INDICATION):
958                         FsmEvent(&chanp->fi, EV_SETUP_IND, NULL);
959                         break;
960                 case (CC_DISCONNECT | INDICATION):
961                         FsmEvent(&chanp->fi, EV_DISCONNECT_IND, NULL);
962                         break;
963                 case (CC_RELEASE | CONFIRM):
964                         FsmEvent(&chanp->fi, EV_RELEASE, NULL);
965                         break;
966                 case (CC_SUSPEND | CONFIRM):
967                         FsmEvent(&chanp->fi, EV_RELEASE, NULL);
968                         break;
969                 case (CC_RESUME | CONFIRM):
970                         FsmEvent(&chanp->fi, EV_SETUP_CNF, NULL);
971                         break;
972                 case (CC_RESUME_ERR):
973                         FsmEvent(&chanp->fi, EV_RELEASE, NULL);
974                         break;
975                 case (CC_RELEASE | INDICATION):
976                         FsmEvent(&chanp->fi, EV_RELEASE, NULL);
977                         break;
978                 case (CC_SETUP_COMPL | INDICATION):
979                         FsmEvent(&chanp->fi, EV_SETUP_CMPL_IND, NULL);
980                         break;
981                 case (CC_SETUP | CONFIRM):
982                         FsmEvent(&chanp->fi, EV_SETUP_CNF, NULL);
983                         break;
984                 case (CC_CHARGE | INDICATION):
985                         FsmEvent(&chanp->fi, EV_CINF, NULL);
986                         break;
987                 case (CC_NOSETUP_RSP):
988                         FsmEvent(&chanp->fi, EV_NOSETUP_RSP, NULL);
989                         break;
990                 case (CC_SETUP_ERR):
991                         FsmEvent(&chanp->fi, EV_SETUP_ERR, NULL);
992                         break;
993                 case (CC_CONNECT_ERR):
994                         FsmEvent(&chanp->fi, EV_CONNECT_ERR, NULL);
995                         break;
996                 case (CC_RELEASE_ERR):
997                         FsmEvent(&chanp->fi, EV_RELEASE, NULL);
998                         break;
999                 case (CC_PROCEED_SEND | INDICATION):
1000                 case (CC_PROCEEDING | INDICATION):
1001                 case (CC_ALERTING | INDICATION):
1002                 case (CC_PROGRESS | INDICATION):
1003                 case (CC_NOTIFY | INDICATION):
1004                         break;
1005                 case (CC_REDIR | INDICATION):
1006                         stat_redir_result(cs, chanp->chan, pc->redir_result); 
1007                         break;
1008                         default:
1009                         if (chanp->debug & 0x800) {
1010                                 HiSax_putstatus(chanp->cs, "Ch",
1011                                         "%d L3->L4 unknown primitiv %#x",
1012                                         chanp->chan, pr);
1013                         }
1014         }
1015 }
1016
1017 static void
1018 dummy_pstack(struct PStack *st, int pr, void *arg) {
1019         printk(KERN_WARNING"call to dummy_pstack pr=%04x arg %lx\n", pr, (long)arg);
1020 }
1021
1022 static int
1023 init_PStack(struct PStack **stp) {
1024         *stp = kmalloc(sizeof(struct PStack), GFP_ATOMIC);
1025         if (!*stp)
1026                 return -ENOMEM;
1027         (*stp)->next = NULL;
1028         (*stp)->l1.l1l2 = dummy_pstack;
1029         (*stp)->l1.l1hw = dummy_pstack;
1030         (*stp)->l1.l1tei = dummy_pstack;
1031         (*stp)->l2.l2tei = dummy_pstack;
1032         (*stp)->l2.l2l1 = dummy_pstack;
1033         (*stp)->l2.l2l3 = dummy_pstack;
1034         (*stp)->l3.l3l2 = dummy_pstack;
1035         (*stp)->l3.l3ml3 = dummy_pstack;
1036         (*stp)->l3.l3l4 = dummy_pstack;
1037         (*stp)->lli.l4l3 = dummy_pstack;
1038         (*stp)->ma.layer = dummy_pstack;
1039         return 0;
1040 }
1041
1042 static int
1043 init_d_st(struct Channel *chanp)
1044 {
1045         struct PStack *st;
1046         struct IsdnCardState *cs = chanp->cs;
1047         char tmp[16];
1048         int err;
1049
1050         err = init_PStack(&chanp->d_st);
1051         if (err)
1052                 return err;
1053         st = chanp->d_st;
1054         st->next = NULL;
1055         HiSax_addlist(cs, st);
1056         setstack_HiSax(st, cs);
1057         st->l2.sap = 0;
1058         st->l2.tei = -1;
1059         st->l2.flag = 0;
1060         test_and_set_bit(FLG_MOD128, &st->l2.flag);
1061         test_and_set_bit(FLG_LAPD, &st->l2.flag);
1062         test_and_set_bit(FLG_ORIG, &st->l2.flag);
1063         st->l2.maxlen = MAX_DFRAME_LEN;
1064         st->l2.window = 1;
1065         st->l2.T200 = 1000;     /* 1000 milliseconds  */
1066         st->l2.N200 = 3;        /* try 3 times        */
1067         st->l2.T203 = 10000;    /* 10000 milliseconds */
1068         if (test_bit(FLG_TWO_DCHAN, &cs->HW_Flags))
1069                 sprintf(tmp, "DCh%d Q.921 ", chanp->chan);
1070         else
1071                 sprintf(tmp, "DCh Q.921 ");
1072         setstack_isdnl2(st, tmp);
1073         setstack_l3dc(st, chanp);
1074         st->lli.userdata = chanp;
1075         st->lli.l2writewakeup = NULL;
1076         st->l3.l3l4 = dchan_l3l4;
1077
1078         return 0;
1079 }
1080
1081 static void
1082 callc_debug(struct FsmInst *fi, char *fmt, ...)
1083 {
1084         va_list args;
1085         struct Channel *chanp = fi->userdata;
1086         char tmp[16];
1087
1088         va_start(args, fmt);
1089         sprintf(tmp, "Ch%d callc ", chanp->chan);
1090         VHiSax_putstatus(chanp->cs, tmp, fmt, args);
1091         va_end(args);
1092 }
1093
1094 static int
1095 init_chan(int chan, struct IsdnCardState *csta)
1096 {
1097         struct Channel *chanp = csta->channel + chan;
1098         int err;
1099
1100         chanp->cs = csta;
1101         chanp->bcs = csta->bcs + chan;
1102         chanp->chan = chan;
1103         chanp->incoming = 0;
1104         chanp->debug = 0;
1105         chanp->Flags = 0;
1106         chanp->leased = 0;
1107         err = init_PStack(&chanp->b_st);
1108         if (err)
1109                 return err;
1110         chanp->b_st->l1.delay = DEFAULT_B_DELAY;
1111         chanp->fi.fsm = &callcfsm;
1112         chanp->fi.state = ST_NULL;
1113         chanp->fi.debug = 0;
1114         chanp->fi.userdata = chanp;
1115         chanp->fi.printdebug = callc_debug;
1116         FsmInitTimer(&chanp->fi, &chanp->dial_timer);
1117         FsmInitTimer(&chanp->fi, &chanp->drel_timer);
1118         if (!chan || (test_bit(FLG_TWO_DCHAN, &csta->HW_Flags) && chan < 2)) {
1119                 err = init_d_st(chanp);
1120                 if (err)
1121                         return err;
1122         } else {
1123                 chanp->d_st = csta->channel->d_st;
1124         }
1125         chanp->data_open = 0;
1126         return 0;
1127 }
1128
1129 int
1130 CallcNewChan(struct IsdnCardState *csta) {
1131         int i, err;
1132
1133         chancount += 2;
1134         err = init_chan(0, csta);
1135         if (err)
1136                 return err;
1137         err = init_chan(1, csta);
1138         if (err)
1139                 return err;
1140         printk(KERN_INFO "HiSax: 2 channels added\n");
1141
1142         for (i = 0; i < MAX_WAITING_CALLS; i++) { 
1143                 err = init_chan(i+2,csta);
1144                 if (err)
1145                         return err;
1146         }
1147         printk(KERN_INFO "HiSax: MAX_WAITING_CALLS added\n");
1148         if (test_bit(FLG_PTP, &csta->channel->d_st->l2.flag)) {
1149                 printk(KERN_INFO "LAYER2 WATCHING ESTABLISH\n");
1150                 csta->channel->d_st->lli.l4l3(csta->channel->d_st,
1151                         DL_ESTABLISH | REQUEST, NULL);
1152         }
1153         return (0);
1154 }
1155
1156 static void
1157 release_d_st(struct Channel *chanp)
1158 {
1159         struct PStack *st = chanp->d_st;
1160
1161         if (!st)
1162                 return;
1163         releasestack_isdnl2(st);
1164         releasestack_isdnl3(st);
1165         HiSax_rmlist(st->l1.hardware, st);
1166         kfree(st);
1167         chanp->d_st = NULL;
1168 }
1169
1170 void
1171 CallcFreeChan(struct IsdnCardState *csta)
1172 {
1173         int i;
1174
1175         for (i = 0; i < 2; i++) {
1176                 FsmDelTimer(&csta->channel[i].drel_timer, 74);
1177                 FsmDelTimer(&csta->channel[i].dial_timer, 75);
1178                 if (i || test_bit(FLG_TWO_DCHAN, &csta->HW_Flags))
1179                         release_d_st(csta->channel + i);
1180                 if (csta->channel[i].b_st) {
1181                         release_b_st(csta->channel + i);
1182                         kfree(csta->channel[i].b_st);
1183                         csta->channel[i].b_st = NULL;
1184                 } else
1185                         printk(KERN_WARNING "CallcFreeChan b_st ch%d allready freed\n", i);
1186                 if (i || test_bit(FLG_TWO_DCHAN, &csta->HW_Flags)) {
1187                         release_d_st(csta->channel + i);
1188                 } else
1189                         csta->channel[i].d_st = NULL;
1190         }
1191 }
1192
1193 static void
1194 lldata_handler(struct PStack *st, int pr, void *arg)
1195 {
1196         struct Channel *chanp = (struct Channel *) st->lli.userdata;
1197         struct sk_buff *skb = arg;
1198
1199         switch (pr) {
1200                 case (DL_DATA  | INDICATION):
1201                         if (chanp->data_open) {
1202                                 if (chanp->debug & 0x800)
1203                                         link_debug(chanp, 0, "lldata: %d", skb->len);
1204                                 chanp->cs->iif.rcvcallb_skb(chanp->cs->myid, chanp->chan, skb);
1205                         } else {
1206                                 link_debug(chanp, 0, "lldata: channel not open");
1207                                 dev_kfree_skb(skb);
1208                         }
1209                         break;
1210                 case (DL_ESTABLISH | INDICATION):
1211                 case (DL_ESTABLISH | CONFIRM):
1212                         FsmEvent(&chanp->fi, EV_BC_EST, NULL);
1213                         break;
1214                 case (DL_RELEASE | INDICATION):
1215                 case (DL_RELEASE | CONFIRM):
1216                         FsmEvent(&chanp->fi, EV_BC_REL, NULL);
1217                         break;
1218                 default:
1219                         printk(KERN_WARNING "lldata_handler unknown primitive %#x\n",
1220                                 pr);
1221                         break;
1222         }
1223 }
1224
1225 static void
1226 lltrans_handler(struct PStack *st, int pr, void *arg)
1227 {
1228         struct Channel *chanp = (struct Channel *) st->lli.userdata;
1229         struct sk_buff *skb = arg;
1230
1231         switch (pr) {
1232                 case (PH_DATA | INDICATION):
1233                         if (chanp->data_open) {
1234                                 if (chanp->debug & 0x800)
1235                                         link_debug(chanp, 0, "lltrans: %d", skb->len);
1236                                 chanp->cs->iif.rcvcallb_skb(chanp->cs->myid, chanp->chan, skb);
1237                         } else {
1238                                 link_debug(chanp, 0, "lltrans: channel not open");
1239                                 dev_kfree_skb(skb);
1240                         }
1241                         break;
1242                 case (PH_ACTIVATE | INDICATION):
1243                 case (PH_ACTIVATE | CONFIRM):
1244                         FsmEvent(&chanp->fi, EV_BC_EST, NULL);
1245                         break;
1246                 case (PH_DEACTIVATE | INDICATION):
1247                 case (PH_DEACTIVATE | CONFIRM):
1248                         FsmEvent(&chanp->fi, EV_BC_REL, NULL);
1249                         break;
1250                 default:
1251                         printk(KERN_WARNING "lltrans_handler unknown primitive %#x\n",
1252                                 pr);
1253                         break;
1254         }
1255 }
1256
1257 static void
1258 ll_writewakeup(struct PStack *st, int len)
1259 {
1260         struct Channel *chanp = st->lli.userdata;
1261         isdn_ctrl ic;
1262
1263         if (chanp->debug & 0x800)
1264                 link_debug(chanp, 0, "llwakeup: %d", len);
1265         ic.driver = chanp->cs->myid;
1266         ic.command = ISDN_STAT_BSENT;
1267         ic.arg = chanp->chan;
1268         ic.parm.length = len;
1269         chanp->cs->iif.statcallb(&ic);
1270 }
1271
1272 static int
1273 init_b_st(struct Channel *chanp, int incoming)
1274 {
1275         struct PStack *st = chanp->b_st;
1276         struct IsdnCardState *cs = chanp->cs;
1277         char tmp[16];
1278
1279         st->l1.hardware = cs;
1280         if (chanp->leased)
1281                 st->l1.bc = chanp->chan & 1;
1282         else
1283                 st->l1.bc = chanp->proc->para.bchannel - 1;
1284         switch (chanp->l2_active_protocol) {
1285                 case (ISDN_PROTO_L2_X75I):
1286                 case (ISDN_PROTO_L2_HDLC):
1287                         st->l1.mode = L1_MODE_HDLC;
1288                         break;
1289                 case (ISDN_PROTO_L2_HDLC_56K):
1290                         st->l1.mode = L1_MODE_HDLC_56K;
1291                         break;
1292                 case (ISDN_PROTO_L2_TRANS):
1293                         st->l1.mode = L1_MODE_TRANS;
1294                         break;
1295                 case (ISDN_PROTO_L2_MODEM):
1296                         st->l1.mode = L1_MODE_V32;
1297                         break;
1298                 case (ISDN_PROTO_L2_FAX):
1299                         st->l1.mode = L1_MODE_FAX;
1300                         break;
1301         }
1302         chanp->bcs->conmsg = NULL;
1303         if (chanp->bcs->BC_SetStack(st, chanp->bcs))
1304                 return (-1);
1305         st->l2.flag = 0;
1306         test_and_set_bit(FLG_LAPB, &st->l2.flag);
1307         st->l2.maxlen = MAX_DATA_SIZE;
1308         if (!incoming)
1309                 test_and_set_bit(FLG_ORIG, &st->l2.flag);
1310         st->l2.T200 = 1000;     /* 1000 milliseconds */
1311         st->l2.window = 7;
1312         st->l2.N200 = 4;        /* try 4 times       */
1313         st->l2.T203 = 5000;     /* 5000 milliseconds */
1314         st->l3.debug = 0;
1315         switch (chanp->l2_active_protocol) {
1316                 case (ISDN_PROTO_L2_X75I):
1317                         sprintf(tmp, "Ch%d X.75", chanp->chan);
1318                         setstack_isdnl2(st, tmp);
1319                         setstack_l3bc(st, chanp);
1320                         st->l2.l2l3 = lldata_handler;
1321                         st->lli.userdata = chanp;
1322                         st->lli.l1writewakeup = NULL;
1323                         st->lli.l2writewakeup = ll_writewakeup;
1324                         st->l2.l2m.debug = chanp->debug & 16;
1325                         st->l2.debug = chanp->debug & 64;
1326                         break;
1327                 case (ISDN_PROTO_L2_HDLC):
1328                 case (ISDN_PROTO_L2_HDLC_56K):
1329                 case (ISDN_PROTO_L2_TRANS):
1330                 case (ISDN_PROTO_L2_MODEM):
1331                 case (ISDN_PROTO_L2_FAX):
1332                         st->l1.l1l2 = lltrans_handler;
1333                         st->lli.userdata = chanp;
1334                         st->lli.l1writewakeup = ll_writewakeup;
1335                         setstack_transl2(st);
1336                         setstack_l3bc(st, chanp);
1337                         break;
1338         }
1339         test_and_set_bit(FLG_START_B, &chanp->Flags);
1340         return (0);
1341 }
1342
1343 static void
1344 leased_l4l3(struct PStack *st, int pr, void *arg)
1345 {
1346         struct Channel *chanp = (struct Channel *) st->lli.userdata;
1347         struct sk_buff *skb = arg;
1348
1349         switch (pr) {
1350                 case (DL_DATA | REQUEST):
1351                         link_debug(chanp, 0, "leased line d-channel DATA");
1352                         dev_kfree_skb(skb);
1353                         break;
1354                 case (DL_ESTABLISH | REQUEST):
1355                         st->l2.l2l1(st, PH_ACTIVATE | REQUEST, NULL);
1356                         break;
1357                 case (DL_RELEASE | REQUEST):
1358                         break;
1359                 default:
1360                         printk(KERN_WARNING "transd_l4l3 unknown primitive %#x\n",
1361                                 pr);
1362                         break;
1363         }
1364 }
1365
1366 static void
1367 leased_l1l2(struct PStack *st, int pr, void *arg)
1368 {
1369         struct Channel *chanp = (struct Channel *) st->lli.userdata;
1370         struct sk_buff *skb = arg;
1371         int i,event = EV_LEASED_REL;
1372
1373         switch (pr) {
1374                 case (PH_DATA | INDICATION):
1375                         link_debug(chanp, 0, "leased line d-channel DATA");
1376                         dev_kfree_skb(skb);
1377                         break;
1378                 case (PH_ACTIVATE | INDICATION):
1379                 case (PH_ACTIVATE | CONFIRM):
1380                         event = EV_LEASED;
1381                 case (PH_DEACTIVATE | INDICATION):
1382                 case (PH_DEACTIVATE | CONFIRM):
1383                         if (test_bit(FLG_TWO_DCHAN, &chanp->cs->HW_Flags))
1384                                 i = 1;
1385                         else
1386                                 i = 0;
1387                         while (i < 2) {
1388                                 FsmEvent(&chanp->fi, event, NULL);
1389                                 chanp++;
1390                                 i++;
1391                         }
1392                         break;
1393                 default:
1394                         printk(KERN_WARNING
1395                                 "transd_l1l2 unknown primitive %#x\n", pr);
1396                         break;
1397         }
1398 }
1399
1400 static void
1401 distr_debug(struct IsdnCardState *csta, int debugflags)
1402 {
1403         int i;
1404         struct Channel *chanp = csta->channel;
1405
1406         for (i = 0; i < (2 + MAX_WAITING_CALLS) ; i++) {
1407                 chanp[i].debug = debugflags;
1408                 chanp[i].fi.debug = debugflags & 2;
1409                 chanp[i].d_st->l2.l2m.debug = debugflags & 8;
1410                 chanp[i].b_st->l2.l2m.debug = debugflags & 0x10;
1411                 chanp[i].d_st->l2.debug = debugflags & 0x20;
1412                 chanp[i].b_st->l2.debug = debugflags & 0x40;
1413                 chanp[i].d_st->l3.l3m.debug = debugflags & 0x80;
1414                 chanp[i].b_st->l3.l3m.debug = debugflags & 0x100;
1415                 chanp[i].b_st->ma.tei_m.debug = debugflags & 0x200;
1416                 chanp[i].b_st->ma.debug = debugflags & 0x200;
1417                 chanp[i].d_st->l1.l1m.debug = debugflags & 0x1000;
1418                 chanp[i].b_st->l1.l1m.debug = debugflags & 0x2000;
1419         }
1420         if (debugflags & 4)
1421                 csta->debug |= DEB_DLOG_HEX;
1422         else
1423                 csta->debug &= ~DEB_DLOG_HEX;
1424 }
1425
1426 static char tmpbuf[256];
1427
1428 static void
1429 capi_debug(struct Channel *chanp, capi_msg *cm)
1430 {
1431         char *t = tmpbuf;
1432
1433         t += QuickHex(t, (u_char *)cm, (cm->Length>50)? 50: cm->Length);
1434         t--;
1435         *t= 0;
1436         HiSax_putstatus(chanp->cs, "Ch", "%d CAPIMSG %s", chanp->chan, tmpbuf);
1437 }
1438
1439 void
1440 lli_got_fac_req(struct Channel *chanp, capi_msg *cm) {
1441         if ((cm->para[0] != 3) || (cm->para[1] != 0))
1442                 return;
1443         if (cm->para[2]<3)
1444                 return;
1445         if (cm->para[4] != 0)
1446                 return;
1447         switch(cm->para[3]) {
1448                 case 4: /* Suspend */
1449                         strncpy(chanp->setup.phone, &cm->para[5], cm->para[5] +1);
1450                         FsmEvent(&chanp->fi, EV_SUSPEND, cm);
1451                         break;
1452                 case 5: /* Resume */
1453                         strncpy(chanp->setup.phone, &cm->para[5], cm->para[5] +1);
1454                         if (chanp->fi.state == ST_NULL) {
1455                                 FsmEvent(&chanp->fi, EV_RESUME, cm);
1456                         } else {
1457                                 FsmDelTimer(&chanp->dial_timer, 72);
1458                                 FsmAddTimer(&chanp->dial_timer, 80, EV_RESUME, cm, 73);
1459                         }
1460                         break;
1461         }
1462 }
1463
1464 void
1465 lli_got_manufacturer(struct Channel *chanp, struct IsdnCardState *cs, capi_msg *cm) {
1466         if ((cs->typ == ISDN_CTYPE_ELSA) || (cs->typ == ISDN_CTYPE_ELSA_PNP) ||
1467                 (cs->typ == ISDN_CTYPE_ELSA_PCI)) {
1468                 if (cs->hw.elsa.MFlag) {
1469                         cs->cardmsg(cs, CARD_AUX_IND, cm->para);
1470                 }
1471         }
1472 }
1473
1474
1475 /***************************************************************/
1476 /* Limit the available number of channels for the current card */
1477 /***************************************************************/
1478 static int 
1479 set_channel_limit(struct IsdnCardState *cs, int chanmax)
1480 {
1481         isdn_ctrl ic;
1482         int i, ii;
1483
1484         if ((chanmax < 0) || (chanmax > 2))
1485                 return(-EINVAL);
1486         cs->chanlimit = 0;
1487         for (ii = 0; ii < 2; ii++) {
1488                 ic.driver = cs->myid;
1489                 ic.command = ISDN_STAT_DISCH;
1490                 ic.arg = ii;
1491                 if (ii >= chanmax)
1492                         ic.parm.num[0] = 0; /* disabled */
1493                 else
1494                         ic.parm.num[0] = 1; /* enabled */
1495                 i = cs->iif.statcallb(&ic); 
1496                 if (i) return(-EINVAL);
1497                 if (ii < chanmax) 
1498                         cs->chanlimit++;
1499         }
1500         return(0);
1501 } /* set_channel_limit */
1502
1503 int
1504 HiSax_command(isdn_ctrl * ic)
1505 {
1506         struct IsdnCardState *csta = hisax_findcard(ic->driver);
1507         struct PStack *st;
1508         struct Channel *chanp;
1509         int i;
1510         u_int num;
1511
1512         if (!csta) {
1513                 printk(KERN_ERR
1514                 "HiSax: if_command %d called with invalid driverId %d!\n",
1515                         ic->command, ic->driver);
1516                 return -ENODEV;
1517         }
1518         switch (ic->command) {
1519                 case (ISDN_CMD_SETEAZ):
1520                         chanp = csta->channel + ic->arg;
1521                         break;
1522                 case (ISDN_CMD_SETL2):
1523                         chanp = csta->channel + (ic->arg & 0xff);
1524                         if (chanp->debug & 1)
1525                                 link_debug(chanp, 1, "SETL2 card %d %ld",
1526                                         csta->cardnr + 1, ic->arg >> 8);
1527                         chanp->l2_protocol = ic->arg >> 8;
1528                         break;
1529                 case (ISDN_CMD_SETL3):
1530                         chanp = csta->channel + (ic->arg & 0xff);
1531                         if (chanp->debug & 1)
1532                                 link_debug(chanp, 1, "SETL3 card %d %ld",
1533                                         csta->cardnr + 1, ic->arg >> 8);
1534                         chanp->l3_protocol = ic->arg >> 8;
1535                         break;
1536                 case (ISDN_CMD_DIAL):
1537                         chanp = csta->channel + (ic->arg & 0xff);
1538                         if (chanp->debug & 1)
1539                                 link_debug(chanp, 1, "DIAL %s -> %s (%d,%d)",
1540                                         ic->parm.setup.eazmsn, ic->parm.setup.phone,
1541                                         ic->parm.setup.si1, ic->parm.setup.si2);
1542                         memcpy(&chanp->setup, &ic->parm.setup, sizeof(setup_parm));
1543                         if (!strcmp(chanp->setup.eazmsn, "0"))
1544                                 chanp->setup.eazmsn[0] = '\0';
1545                         /* this solution is dirty and may be change, if
1546                          * we make a callreference based callmanager */
1547                         if (chanp->fi.state == ST_NULL) {
1548                                 FsmEvent(&chanp->fi, EV_DIAL, NULL);
1549                         } else {
1550                                 FsmDelTimer(&chanp->dial_timer, 70);
1551                                 FsmAddTimer(&chanp->dial_timer, 50, EV_DIAL, NULL, 71);
1552                         }
1553                         break;
1554                 case (ISDN_CMD_ACCEPTB):
1555                         chanp = csta->channel + ic->arg;
1556                         if (chanp->debug & 1)
1557                                 link_debug(chanp, 1, "ACCEPTB");
1558                         FsmEvent(&chanp->fi, EV_ACCEPTB, NULL);
1559                         break;
1560                 case (ISDN_CMD_ACCEPTD):
1561                         chanp = csta->channel + ic->arg;
1562                         memcpy(&chanp->setup, &ic->parm.setup, sizeof(setup_parm));
1563                         if (chanp->debug & 1)
1564                                 link_debug(chanp, 1, "ACCEPTD");
1565                         FsmEvent(&chanp->fi, EV_ACCEPTD, NULL);
1566                         break;
1567                 case (ISDN_CMD_HANGUP):
1568                         chanp = csta->channel + ic->arg;
1569                         if (chanp->debug & 1)
1570                                 link_debug(chanp, 1, "HANGUP");
1571                         FsmEvent(&chanp->fi, EV_HANGUP, NULL);
1572                         break;
1573                 case (CAPI_PUT_MESSAGE):
1574                         chanp = csta->channel + ic->arg;
1575                         if (chanp->debug & 1)
1576                                 capi_debug(chanp, &ic->parm.cmsg);
1577                         if (ic->parm.cmsg.Length < 8)
1578                                 break;
1579                         switch(ic->parm.cmsg.Command) {
1580                                 case CAPI_FACILITY:
1581                                         if (ic->parm.cmsg.Subcommand == CAPI_REQ)
1582                                                 lli_got_fac_req(chanp, &ic->parm.cmsg);
1583                                         break;
1584                                 case CAPI_MANUFACTURER:
1585                                         if (ic->parm.cmsg.Subcommand == CAPI_REQ)
1586                                                 lli_got_manufacturer(chanp, csta, &ic->parm.cmsg);
1587                                         break;
1588                                 default:
1589                                         break;
1590                         }
1591                         break;
1592                 case (ISDN_CMD_LOCK):
1593                         HiSax_mod_inc_use_count(csta);
1594 #ifdef MODULE
1595                         if (csta->channel[0].debug & 0x400)
1596                                 HiSax_putstatus(csta, "   LOCK ", "modcnt %lx",
1597                                         MOD_USE_COUNT);
1598 #endif                          /* MODULE */
1599                         break;
1600                 case (ISDN_CMD_UNLOCK):
1601                         HiSax_mod_dec_use_count(csta);
1602 #ifdef MODULE
1603                         if (csta->channel[0].debug & 0x400)
1604                                 HiSax_putstatus(csta, " UNLOCK ", "modcnt %lx",
1605                                         MOD_USE_COUNT);
1606 #endif                          /* MODULE */
1607                         break;
1608                 case (ISDN_CMD_IOCTL):
1609                         switch (ic->arg) {
1610                                 case (0):
1611                                         num = *(unsigned int *) ic->parm.num;
1612                                         HiSax_reportcard(csta->cardnr, num);
1613                                         break;
1614                                 case (1):
1615                                         num = *(unsigned int *) ic->parm.num;
1616                                         distr_debug(csta, num);
1617                                         printk(KERN_DEBUG "HiSax: debugging flags card %d set to %x\n",
1618                                                 csta->cardnr + 1, num);
1619                                         HiSax_putstatus(csta, "debugging flags ",
1620                                                 "card %d set to %x", csta->cardnr + 1, num);
1621                                         break;
1622                                 case (2):
1623                                         num = *(unsigned int *) ic->parm.num;
1624                                         csta->channel[0].b_st->l1.delay = num;
1625                                         csta->channel[1].b_st->l1.delay = num;
1626                                         HiSax_putstatus(csta, "delay ", "card %d set to %d ms",
1627                                                 csta->cardnr + 1, num);
1628                                         printk(KERN_DEBUG "HiSax: delay card %d set to %d ms\n",
1629                                                 csta->cardnr + 1, num);
1630                                         break;
1631                                 case (3):
1632                                         for (i = 0; i < *(unsigned int *) ic->parm.num; i++)
1633                                                 HiSax_mod_dec_use_count(NULL);
1634                                         break;
1635                                 case (4):
1636                                         for (i = 0; i < *(unsigned int *) ic->parm.num; i++)
1637                                                 HiSax_mod_inc_use_count(NULL);
1638                                         break;
1639                                 case (5):       /* set card in leased mode */
1640                                         num = *(unsigned int *) ic->parm.num;
1641                                         if ((num <1) || (num > 2)) {
1642                                                 HiSax_putstatus(csta, "Set LEASED ",
1643                                                         "wrong channel %d", num);
1644                                                 printk(KERN_WARNING "HiSax: Set LEASED wrong channel %d\n",
1645                                                         num);
1646                                         } else {
1647                                                 num--;
1648                                                 chanp = csta->channel +num;
1649                                                 chanp->leased = 1;
1650                                                 HiSax_putstatus(csta, "Card",
1651                                                         "%d channel %d set leased mode\n",
1652                                                         csta->cardnr + 1, num + 1);
1653                                                 chanp->d_st->l1.l1l2 = leased_l1l2;
1654                                                 chanp->d_st->lli.l4l3 = leased_l4l3;
1655                                                 chanp->d_st->lli.l4l3(chanp->d_st,
1656                                                         DL_ESTABLISH | REQUEST, NULL);
1657                                         }
1658                                         break;
1659                                 case (6):       /* set B-channel test loop */
1660                                         num = *(unsigned int *) ic->parm.num;
1661                                         if (csta->stlist)
1662                                                 csta->stlist->l2.l2l1(csta->stlist,
1663                                                         PH_TESTLOOP | REQUEST, (void *) (long)num);
1664                                         break;
1665                                 case (7):       /* set card in PTP mode */
1666                                         num = *(unsigned int *) ic->parm.num;
1667                                         if (test_bit(FLG_TWO_DCHAN, &csta->HW_Flags)) {
1668                                                 printk(KERN_ERR "HiSax PTP mode only with one TEI possible\n");
1669                                         } else if (num) {
1670                                                 test_and_set_bit(FLG_PTP, &csta->channel[0].d_st->l2.flag);
1671                                                 test_and_set_bit(FLG_FIXED_TEI, &csta->channel[0].d_st->l2.flag);
1672                                                 csta->channel[0].d_st->l2.tei = 0;
1673                                                 HiSax_putstatus(csta, "set card ", "in PTP mode");
1674                                                 printk(KERN_DEBUG "HiSax: set card in PTP mode\n");
1675                                                 printk(KERN_INFO "LAYER2 WATCHING ESTABLISH\n");
1676                                                 csta->channel[0].d_st->lli.l4l3(csta->channel[0].d_st,
1677                                                         DL_ESTABLISH | REQUEST, NULL);
1678                                         } else {
1679                                                 test_and_clear_bit(FLG_PTP, &csta->channel[0].d_st->l2.flag);
1680                                                 test_and_clear_bit(FLG_FIXED_TEI, &csta->channel[0].d_st->l2.flag);
1681                                                 HiSax_putstatus(csta, "set card ", "in PTMP mode");
1682                                                 printk(KERN_DEBUG "HiSax: set card in PTMP mode\n");
1683                                         }
1684                                         break;
1685                                 case (8):       /* set card in FIXED TEI mode */
1686                                         num = *(unsigned int *) ic->parm.num;
1687                                         chanp = csta->channel + (num & 1);
1688                                         num = num >>1;
1689                                         if (num == 127) {
1690                                                 test_and_clear_bit(FLG_FIXED_TEI, &chanp->d_st->l2.flag);
1691                                                 chanp->d_st->l2.tei = -1;
1692                                                 HiSax_putstatus(csta, "set card ", "in VAR TEI mode");
1693                                                 printk(KERN_DEBUG "HiSax: set card in VAR TEI mode\n");
1694                                         } else {
1695                                                 test_and_set_bit(FLG_FIXED_TEI, &chanp->d_st->l2.flag);
1696                                                 chanp->d_st->l2.tei = num;
1697                                                 HiSax_putstatus(csta, "set card ", "in FIXED TEI (%d) mode", num);
1698                                                 printk(KERN_DEBUG "HiSax: set card in FIXED TEI (%d) mode\n",
1699                                                         num);
1700                                         }
1701                                         chanp->d_st->lli.l4l3(chanp->d_st,
1702                                                 DL_ESTABLISH | REQUEST, NULL);
1703                                         break;
1704 #ifdef MODULE
1705                                 case (55):
1706                                         MOD_USE_COUNT = 0;
1707                                         HiSax_mod_inc_use_count(NULL);
1708                                         break;
1709 #endif                          /* MODULE */
1710                                 case (11):
1711                                         num = csta->debug & DEB_DLOG_HEX;
1712                                         csta->debug = *(unsigned int *) ic->parm.num;
1713                                         csta->debug |= num;
1714                                         HiSax_putstatus(cards[0].cs, "l1 debugging ",
1715                                                 "flags card %d set to %x",
1716                                                 csta->cardnr + 1, csta->debug);
1717                                         printk(KERN_DEBUG "HiSax: l1 debugging flags card %d set to %x\n",
1718                                                 csta->cardnr + 1, csta->debug);
1719                                         break;
1720                                 case (13):
1721                                         csta->channel[0].d_st->l3.debug = *(unsigned int *) ic->parm.num;
1722                                         csta->channel[1].d_st->l3.debug = *(unsigned int *) ic->parm.num;
1723                                         HiSax_putstatus(cards[0].cs, "l3 debugging ",
1724                                                 "flags card %d set to %x\n", csta->cardnr + 1,
1725                                                 *(unsigned int *) ic->parm.num);
1726                                         printk(KERN_DEBUG "HiSax: l3 debugging flags card %d set to %x\n",
1727                                                 csta->cardnr + 1, *(unsigned int *) ic->parm.num);
1728                                         break;
1729                                 case (10):
1730                                         i = *(unsigned int *) ic->parm.num;
1731                                         return(set_channel_limit(csta, i));
1732                                 default:
1733                                         if (csta->auxcmd)
1734                                                 return(csta->auxcmd(csta, ic));
1735                                         printk(KERN_DEBUG "HiSax: invalid ioclt %d\n",
1736                                                 (int) ic->arg);
1737                                         return (-EINVAL);
1738                         }
1739                         break;
1740                 
1741                 case (ISDN_CMD_PROCEED):
1742                         chanp = csta->channel + ic->arg;
1743                         if (chanp->debug & 1)
1744                                 link_debug(chanp, 1, "PROCEED");
1745                         FsmEvent(&chanp->fi, EV_PROCEED, NULL);
1746                         break;
1747
1748                 case (ISDN_CMD_ALERT):
1749                         chanp = csta->channel + ic->arg;
1750                         if (chanp->debug & 1)
1751                                 link_debug(chanp, 1, "ALERT");
1752                         FsmEvent(&chanp->fi, EV_ALERT, NULL);
1753                         break;
1754
1755                 case (ISDN_CMD_REDIR):
1756                         chanp = csta->channel + ic->arg;
1757                         if (chanp->debug & 1)
1758                                 link_debug(chanp, 1, "REDIR");
1759                         memcpy(&chanp->setup, &ic->parm.setup, sizeof(setup_parm));
1760                         FsmEvent(&chanp->fi, EV_REDIR, NULL);
1761                         break;
1762
1763                 /* protocol specific io commands */
1764                 case (ISDN_CMD_PROT_IO):
1765                         for (st = csta->stlist; st; st = st->next)
1766                                 if (st->protocol == (ic->arg & 0xFF))
1767                                         return(st->lli.l4l3_proto(st, ic));
1768                         return(-EINVAL);
1769                         break;
1770                 default:
1771                         if (csta->auxcmd)
1772                                 return(csta->auxcmd(csta, ic));
1773                         return(-EINVAL);
1774         }
1775         return (0);
1776 }
1777
1778 int
1779 HiSax_writebuf_skb(int id, int chan, int ack, struct sk_buff *skb)
1780 {
1781         struct IsdnCardState *csta = hisax_findcard(id);
1782         struct Channel *chanp;
1783         struct PStack *st;
1784         int len = skb->len;
1785         unsigned long flags;
1786         struct sk_buff *nskb;
1787
1788         if (!csta) {
1789                 printk(KERN_ERR
1790                         "HiSax: if_sendbuf called with invalid driverId!\n");
1791                 return -ENODEV;
1792         }
1793         chanp = csta->channel + chan;
1794         st = chanp->b_st;
1795         if (!chanp->data_open) {
1796                 link_debug(chanp, 1, "writebuf: channel not open");
1797                 return -EIO;
1798         }
1799         if (len > MAX_DATA_SIZE) {
1800                 link_debug(chanp, 1, "writebuf: packet too large (%d bytes)", len);
1801                 printk(KERN_WARNING "HiSax_writebuf: packet too large (%d bytes) !\n",
1802                         len);
1803                 return -EINVAL;
1804         }
1805         if (len) {
1806                 if ((len + chanp->bcs->tx_cnt) > MAX_DATA_MEM) {
1807                         /* Must return 0 here, since this is not an error
1808                          * but a temporary lack of resources.
1809                          */
1810                         if (chanp->debug & 0x800)
1811                                 link_debug(chanp, 1, "writebuf: no buffers for %d bytes", len);
1812                         return 0;
1813                 } else if (chanp->debug & 0x800)
1814                         link_debug(chanp, 1, "writebuf %d/%d/%d", len, chanp->bcs->tx_cnt,MAX_DATA_MEM);
1815                 save_flags(flags);
1816                 cli();
1817                 nskb = skb_clone(skb, GFP_ATOMIC);
1818                 if (nskb) {
1819                         nskb->truesize = nskb->len;
1820                         if (!ack)
1821                                 nskb->pkt_type = PACKET_NOACK;
1822                         if (chanp->l2_active_protocol == ISDN_PROTO_L2_X75I)
1823                                 st->l3.l3l2(st, DL_DATA | REQUEST, nskb);
1824                         else {
1825                                 chanp->bcs->tx_cnt += len;
1826                                 st->l2.l2l1(st, PH_DATA | REQUEST, nskb);
1827                         }
1828                         dev_kfree_skb(skb);
1829                 } else
1830                         len = 0;
1831                 restore_flags(flags);
1832         }
1833         return (len);
1834 }