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