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