Merge tag 'iomap-4.21-merge-3' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[linux/fpc-iii.git] / drivers / isdn / hisax / callc.c
blob9ee06328784c59f8e6eec67b64873b22f9b46fbb
1 /* $Id: callc.c,v 2.59.2.4 2004/02/11 13:21:32 keil Exp $
3 * Author Karsten Keil
4 * Copyright by Karsten Keil <keil@isdn4linux.de>
6 * This software may be used and distributed according to the terms
7 * of the GNU General Public License, incorporated herein by reference.
9 * For changes and modifications please read
10 * Documentation/isdn/HiSax.cert
12 * based on the teles driver from Jan den Ouden
14 * Thanks to Jan den Ouden
15 * Fritz Elfert
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/init.h>
22 #include "hisax.h"
23 #include <linux/isdn/capicmd.h>
25 const char *lli_revision = "$Revision: 2.59.2.4 $";
27 extern struct IsdnCard cards[];
29 static int init_b_st(struct Channel *chanp, int incoming);
30 static void release_b_st(struct Channel *chanp);
32 static struct Fsm callcfsm;
33 static int chancount;
35 /* experimental REJECT after ALERTING for CALLBACK to beat the 4s delay */
36 #define ALERT_REJECT 0
38 /* Value to delay the sending of the first B-channel packet 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
47 #define DEFAULT_B_DELAY 300
49 /* Flags for remembering action done in lli */
51 #define FLG_START_B 0
54 * Find card with given driverId
56 static inline struct IsdnCardState *
57 hisax_findcard(int driverid)
59 int i;
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;
68 static __printf(3, 4) void
69 link_debug(struct Channel *chanp, int direction, char *fmt, ...)
71 va_list args;
72 char tmp[16];
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);
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 */
98 #define STATE_COUNT (ST_IN_PROCEED_SEND + 1)
100 static char *strState[] =
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",
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 */
143 #define EVENT_COUNT (EV_REDIR + 1)
145 static char *strEvent[] =
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",
173 static inline void
174 HL_LL(struct Channel *chanp, int command)
176 isdn_ctrl ic;
178 ic.driver = chanp->cs->myid;
179 ic.command = command;
180 ic.arg = chanp->chan;
181 chanp->cs->iif.statcallb(&ic);
184 static inline void
185 lli_deliver_cause(struct Channel *chanp)
187 isdn_ctrl ic;
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);
205 static inline void
206 lli_close(struct FsmInst *fi)
208 struct Channel *chanp = fi->userdata;
210 FsmChangeState(fi, ST_NULL);
211 chanp->Flags = 0;
212 chanp->cs->cardmsg(chanp->cs, MDL_INFO_REL, (void *) (long)chanp->chan);
215 static void
216 lli_leased_in(struct FsmInst *fi, int event, void *arg)
218 struct Channel *chanp = fi->userdata;
219 isdn_ctrl ic;
220 int ret;
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);
248 * Dial out
250 static void
251 lli_init_bchan_out(struct FsmInst *fi, int event, void *arg)
253 struct Channel *chanp = fi->userdata;
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);
263 static void
264 lli_prep_dialout(struct FsmInst *fi, int event, void *arg)
266 struct Channel *chanp = fi->userdata;
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);
281 static void
282 lli_resume(struct FsmInst *fi, int event, void *arg)
284 struct Channel *chanp = fi->userdata;
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);
299 static void
300 lli_go_active(struct FsmInst *fi, int event, void *arg)
302 struct Channel *chanp = fi->userdata;
303 isdn_ctrl ic;
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);
323 * RESUME
326 /* incoming call */
328 static void
329 lli_deliver_call(struct FsmInst *fi, int event, void *arg)
331 struct Channel *chanp = fi->userdata;
332 isdn_ctrl ic;
333 int ret;
335 chanp->cs->cardmsg(chanp->cs, MDL_INFO_SETUP, (void *) (long)chanp->chan);
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.
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);
348 ic.arg = chanp->chan;
350 * No need to return "unknown" for calls without OAD,
351 * cause that's handled in linklevel now (replaced by '0')
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);
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);
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;
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);
393 static void
394 lli_send_dconnect(struct FsmInst *fi, int event, void *arg)
396 struct Channel *chanp = fi->userdata;
398 FsmChangeState(fi, ST_IN_WAIT_CONN_ACK);
399 chanp->d_st->lli.l4l3(chanp->d_st, CC_SETUP | RESPONSE, chanp->proc);
402 static void
403 lli_send_alert(struct FsmInst *fi, int event, void *arg)
405 struct Channel *chanp = fi->userdata;
407 FsmChangeState(fi, ST_IN_ALERT_SENT);
408 chanp->d_st->lli.l4l3(chanp->d_st, CC_ALERTING | REQUEST, chanp->proc);
411 static void
412 lli_send_redir(struct FsmInst *fi, int event, void *arg)
414 struct Channel *chanp = fi->userdata;
416 chanp->d_st->lli.l4l3(chanp->d_st, CC_REDIR | REQUEST, chanp->proc);
419 static void
420 lli_init_bchan_in(struct FsmInst *fi, int event, void *arg)
422 struct Channel *chanp = fi->userdata;
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);
434 static void
435 lli_setup_rsp(struct FsmInst *fi, int event, void *arg)
437 struct Channel *chanp = fi->userdata;
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);
450 /* Call suspend */
452 static void
453 lli_suspend(struct FsmInst *fi, int event, void *arg)
455 struct Channel *chanp = fi->userdata;
457 chanp->d_st->lli.l4l3(chanp->d_st, CC_SUSPEND | REQUEST, chanp->proc);
460 /* Call clearing */
462 static void
463 lli_leased_hup(struct FsmInst *fi, struct Channel *chanp)
465 isdn_ctrl ic;
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);
478 static void
479 lli_disconnect_req(struct FsmInst *fi, int event, void *arg)
481 struct Channel *chanp = fi->userdata;
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);
494 static void
495 lli_disconnect_reject(struct FsmInst *fi, int event, void *arg)
497 struct Channel *chanp = fi->userdata;
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);
510 static void
511 lli_dhup_close(struct FsmInst *fi, int event, void *arg)
513 struct Channel *chanp = fi->userdata;
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);
526 static void
527 lli_reject_req(struct FsmInst *fi, int event, void *arg)
529 struct Channel *chanp = fi->userdata;
531 if (chanp->leased) {
532 lli_leased_hup(fi, chanp);
533 return;
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
547 static void
548 lli_disconn_bchan(struct FsmInst *fi, int event, void *arg)
550 struct Channel *chanp = fi->userdata;
552 chanp->data_open = 0;
553 FsmChangeState(fi, ST_WAIT_BRELEASE);
554 chanp->b_st->lli.l4l3(chanp->b_st, DL_RELEASE | REQUEST, NULL);
557 static void
558 lli_start_disc(struct FsmInst *fi, int event, void *arg)
560 struct Channel *chanp = fi->userdata;
562 if (chanp->leased) {
563 lli_leased_hup(fi, chanp);
564 } else {
565 lli_disconnect_req(fi, event, arg);
569 static void
570 lli_rel_b_disc(struct FsmInst *fi, int event, void *arg)
572 struct Channel *chanp = fi->userdata;
574 release_b_st(chanp);
575 lli_start_disc(fi, event, arg);
578 static void
579 lli_bhup_disc(struct FsmInst *fi, int event, void *arg)
581 struct Channel *chanp = fi->userdata;
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);
589 static void
590 lli_bhup_rel_b(struct FsmInst *fi, int event, void *arg)
592 struct Channel *chanp = fi->userdata;
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);
602 static void
603 lli_release_bchan(struct FsmInst *fi, int event, void *arg)
605 struct Channel *chanp = fi->userdata;
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);
613 static void
614 lli_rel_b_dhup(struct FsmInst *fi, int event, void *arg)
616 struct Channel *chanp = fi->userdata;
618 release_b_st(chanp);
619 lli_dhup_close(fi, event, arg);
622 static void
623 lli_bhup_dhup(struct FsmInst *fi, int event, void *arg)
625 struct Channel *chanp = fi->userdata;
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);
633 static void
634 lli_abort(struct FsmInst *fi, int event, void *arg)
636 struct Channel *chanp = fi->userdata;
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);
643 static void
644 lli_release_req(struct FsmInst *fi, int event, void *arg)
646 struct Channel *chanp = fi->userdata;
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);
657 static void
658 lli_rel_b_release_req(struct FsmInst *fi, int event, void *arg)
660 struct Channel *chanp = fi->userdata;
662 release_b_st(chanp);
663 lli_release_req(fi, event, arg);
666 static void
667 lli_bhup_release_req(struct FsmInst *fi, int event, void *arg)
669 struct Channel *chanp = fi->userdata;
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);
678 /* processing charge info */
679 static void
680 lli_charge_info(struct FsmInst *fi, int event, void *arg)
682 struct Channel *chanp = fi->userdata;
683 isdn_ctrl ic;
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);
692 /* error procedures */
694 static void
695 lli_dchan_not_ready(struct FsmInst *fi, int event, void *arg)
697 struct Channel *chanp = fi->userdata;
699 if (chanp->debug & 1)
700 link_debug(chanp, 0, "STAT_DHUP");
701 HL_LL(chanp, ISDN_STAT_DHUP);
704 static void
705 lli_no_setup_rsp(struct FsmInst *fi, int event, void *arg)
707 struct Channel *chanp = fi->userdata;
709 if (chanp->debug & 1)
710 link_debug(chanp, 0, "STAT_DHUP");
711 HL_LL(chanp, ISDN_STAT_DHUP);
712 lli_close(fi);
715 static void
716 lli_error(struct FsmInst *fi, int event, void *arg)
718 FsmChangeState(fi, ST_WAIT_DRELEASE);
721 static void
722 lli_failure_l(struct FsmInst *fi, int event, void *arg)
724 struct Channel *chanp = fi->userdata;
725 isdn_ctrl ic;
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);
738 static void
739 lli_rel_b_fail(struct FsmInst *fi, int event, void *arg)
741 struct Channel *chanp = fi->userdata;
743 release_b_st(chanp);
744 lli_failure_l(fi, event, arg);
747 static void
748 lli_bhup_fail(struct FsmInst *fi, int event, void *arg)
750 struct Channel *chanp = fi->userdata;
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);
758 static void
759 lli_failure_a(struct FsmInst *fi, int event, void *arg)
761 struct Channel *chanp = fi->userdata;
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);
768 /* *INDENT-OFF* */
769 static struct FsmNode fnlist[] __initdata =
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},
835 /* *INDENT-ON* */
837 int __init
838 CallcNew(void)
840 callcfsm.state_count = STATE_COUNT;
841 callcfsm.event_count = EVENT_COUNT;
842 callcfsm.strEvent = strEvent;
843 callcfsm.strState = strState;
844 return FsmNew(&callcfsm, fnlist, ARRAY_SIZE(fnlist));
847 void
848 CallcFree(void)
850 FsmFree(&callcfsm);
853 static void
854 release_b_st(struct Channel *chanp)
856 struct PStack *st = chanp->b_st;
858 if (test_and_clear_bit(FLG_START_B, &chanp->Flags)) {
859 chanp->bcs->BC_Close(chanp->bcs);
860 switch (chanp->l2_active_protocol) {
861 case (ISDN_PROTO_L2_X75I):
862 releasestack_isdnl2(st);
863 break;
864 case (ISDN_PROTO_L2_HDLC):
865 case (ISDN_PROTO_L2_HDLC_56K):
866 case (ISDN_PROTO_L2_TRANS):
867 case (ISDN_PROTO_L2_MODEM):
868 case (ISDN_PROTO_L2_FAX):
869 releasestack_transl2(st);
870 break;
875 static struct Channel
876 *selectfreechannel(struct PStack *st, int bch)
878 struct IsdnCardState *cs = st->l1.hardware;
879 struct Channel *chanp = st->lli.userdata;
880 int i;
882 if (test_bit(FLG_TWO_DCHAN, &cs->HW_Flags))
883 i = 1;
884 else
885 i = 0;
887 if (!bch) {
888 i = 2; /* virtual channel */
889 chanp += 2;
892 while (i < ((bch) ? cs->chanlimit : (2 + MAX_WAITING_CALLS))) {
893 if (chanp->fi.state == ST_NULL)
894 return (chanp);
895 chanp++;
896 i++;
899 if (bch) /* number of channels is limited */ {
900 i = 2; /* virtual channel */
901 chanp = st->lli.userdata;
902 chanp += i;
903 while (i < (2 + MAX_WAITING_CALLS)) {
904 if (chanp->fi.state == ST_NULL)
905 return (chanp);
906 chanp++;
907 i++;
910 return (NULL);
913 static void stat_redir_result(struct IsdnCardState *cs, int chan, ulong result)
914 { isdn_ctrl ic;
916 ic.driver = cs->myid;
917 ic.command = ISDN_STAT_REDIR;
918 ic.arg = chan;
919 ic.parm.num[0] = result;
920 cs->iif.statcallb(&ic);
921 } /* stat_redir_result */
923 static void
924 dchan_l3l4(struct PStack *st, int pr, void *arg)
926 struct l3_process *pc = arg;
927 struct IsdnCardState *cs = st->l1.hardware;
928 struct Channel *chanp;
930 if (!pc)
931 return;
933 if (pr == (CC_SETUP | INDICATION)) {
934 if (!(chanp = selectfreechannel(pc->st, pc->para.bchannel))) {
935 pc->para.cause = 0x11; /* User busy */
936 pc->st->lli.l4l3(pc->st, CC_REJECT | REQUEST, pc);
937 } else {
938 chanp->proc = pc;
939 pc->chan = chanp;
940 FsmEvent(&chanp->fi, EV_SETUP_IND, NULL);
942 return;
944 if (!(chanp = pc->chan))
945 return;
947 switch (pr) {
948 case (CC_MORE_INFO | INDICATION):
949 FsmEvent(&chanp->fi, EV_SETUP_IND, NULL);
950 break;
951 case (CC_DISCONNECT | INDICATION):
952 FsmEvent(&chanp->fi, EV_DISCONNECT_IND, NULL);
953 break;
954 case (CC_RELEASE | CONFIRM):
955 FsmEvent(&chanp->fi, EV_RELEASE, NULL);
956 break;
957 case (CC_SUSPEND | CONFIRM):
958 FsmEvent(&chanp->fi, EV_RELEASE, NULL);
959 break;
960 case (CC_RESUME | CONFIRM):
961 FsmEvent(&chanp->fi, EV_SETUP_CNF, NULL);
962 break;
963 case (CC_RESUME_ERR):
964 FsmEvent(&chanp->fi, EV_RELEASE, NULL);
965 break;
966 case (CC_RELEASE | INDICATION):
967 FsmEvent(&chanp->fi, EV_RELEASE, NULL);
968 break;
969 case (CC_SETUP_COMPL | INDICATION):
970 FsmEvent(&chanp->fi, EV_SETUP_CMPL_IND, NULL);
971 break;
972 case (CC_SETUP | CONFIRM):
973 FsmEvent(&chanp->fi, EV_SETUP_CNF, NULL);
974 break;
975 case (CC_CHARGE | INDICATION):
976 FsmEvent(&chanp->fi, EV_CINF, NULL);
977 break;
978 case (CC_NOSETUP_RSP):
979 FsmEvent(&chanp->fi, EV_NOSETUP_RSP, NULL);
980 break;
981 case (CC_SETUP_ERR):
982 FsmEvent(&chanp->fi, EV_SETUP_ERR, NULL);
983 break;
984 case (CC_CONNECT_ERR):
985 FsmEvent(&chanp->fi, EV_CONNECT_ERR, NULL);
986 break;
987 case (CC_RELEASE_ERR):
988 FsmEvent(&chanp->fi, EV_RELEASE, NULL);
989 break;
990 case (CC_PROCEED_SEND | INDICATION):
991 case (CC_PROCEEDING | INDICATION):
992 case (CC_ALERTING | INDICATION):
993 case (CC_PROGRESS | INDICATION):
994 case (CC_NOTIFY | INDICATION):
995 break;
996 case (CC_REDIR | INDICATION):
997 stat_redir_result(cs, chanp->chan, pc->redir_result);
998 break;
999 default:
1000 if (chanp->debug & 0x800) {
1001 HiSax_putstatus(chanp->cs, "Ch",
1002 "%d L3->L4 unknown primitiv %#x",
1003 chanp->chan, pr);
1008 static void
1009 dummy_pstack(struct PStack *st, int pr, void *arg) {
1010 printk(KERN_WARNING"call to dummy_pstack pr=%04x arg %lx\n", pr, (long)arg);
1013 static int
1014 init_PStack(struct PStack **stp) {
1015 *stp = kmalloc(sizeof(struct PStack), GFP_KERNEL);
1016 if (!*stp)
1017 return -ENOMEM;
1018 (*stp)->next = NULL;
1019 (*stp)->l1.l1l2 = dummy_pstack;
1020 (*stp)->l1.l1hw = dummy_pstack;
1021 (*stp)->l1.l1tei = dummy_pstack;
1022 (*stp)->l2.l2tei = dummy_pstack;
1023 (*stp)->l2.l2l1 = dummy_pstack;
1024 (*stp)->l2.l2l3 = dummy_pstack;
1025 (*stp)->l3.l3l2 = dummy_pstack;
1026 (*stp)->l3.l3ml3 = dummy_pstack;
1027 (*stp)->l3.l3l4 = dummy_pstack;
1028 (*stp)->lli.l4l3 = dummy_pstack;
1029 (*stp)->ma.layer = dummy_pstack;
1030 return 0;
1033 static int
1034 init_d_st(struct Channel *chanp)
1036 struct PStack *st;
1037 struct IsdnCardState *cs = chanp->cs;
1038 char tmp[16];
1039 int err;
1041 err = init_PStack(&chanp->d_st);
1042 if (err)
1043 return err;
1044 st = chanp->d_st;
1045 st->next = NULL;
1046 HiSax_addlist(cs, st);
1047 setstack_HiSax(st, cs);
1048 st->l2.sap = 0;
1049 st->l2.tei = -1;
1050 st->l2.flag = 0;
1051 test_and_set_bit(FLG_MOD128, &st->l2.flag);
1052 test_and_set_bit(FLG_LAPD, &st->l2.flag);
1053 test_and_set_bit(FLG_ORIG, &st->l2.flag);
1054 st->l2.maxlen = MAX_DFRAME_LEN;
1055 st->l2.window = 1;
1056 st->l2.T200 = 1000; /* 1000 milliseconds */
1057 st->l2.N200 = 3; /* try 3 times */
1058 st->l2.T203 = 10000; /* 10000 milliseconds */
1059 if (test_bit(FLG_TWO_DCHAN, &cs->HW_Flags))
1060 sprintf(tmp, "DCh%d Q.921 ", chanp->chan);
1061 else
1062 sprintf(tmp, "DCh Q.921 ");
1063 setstack_isdnl2(st, tmp);
1064 setstack_l3dc(st, chanp);
1065 st->lli.userdata = chanp;
1066 st->l3.l3l4 = dchan_l3l4;
1068 return 0;
1071 static __printf(2, 3) void
1072 callc_debug(struct FsmInst *fi, char *fmt, ...)
1074 va_list args;
1075 struct Channel *chanp = fi->userdata;
1076 char tmp[16];
1078 va_start(args, fmt);
1079 sprintf(tmp, "Ch%d callc ", chanp->chan);
1080 VHiSax_putstatus(chanp->cs, tmp, fmt, args);
1081 va_end(args);
1084 static int
1085 init_chan(int chan, struct IsdnCardState *csta)
1087 struct Channel *chanp = csta->channel + chan;
1088 int err;
1090 chanp->cs = csta;
1091 chanp->bcs = csta->bcs + chan;
1092 chanp->chan = chan;
1093 chanp->incoming = 0;
1094 chanp->debug = 0;
1095 chanp->Flags = 0;
1096 chanp->leased = 0;
1097 err = init_PStack(&chanp->b_st);
1098 if (err)
1099 return err;
1100 chanp->b_st->l1.delay = DEFAULT_B_DELAY;
1101 chanp->fi.fsm = &callcfsm;
1102 chanp->fi.state = ST_NULL;
1103 chanp->fi.debug = 0;
1104 chanp->fi.userdata = chanp;
1105 chanp->fi.printdebug = callc_debug;
1106 FsmInitTimer(&chanp->fi, &chanp->dial_timer);
1107 FsmInitTimer(&chanp->fi, &chanp->drel_timer);
1108 if (!chan || (test_bit(FLG_TWO_DCHAN, &csta->HW_Flags) && chan < 2)) {
1109 err = init_d_st(chanp);
1110 if (err)
1111 return err;
1112 } else {
1113 chanp->d_st = csta->channel->d_st;
1115 chanp->data_open = 0;
1116 return 0;
1120 CallcNewChan(struct IsdnCardState *csta) {
1121 int i, err;
1123 chancount += 2;
1124 err = init_chan(0, csta);
1125 if (err)
1126 return err;
1127 err = init_chan(1, csta);
1128 if (err)
1129 return err;
1130 printk(KERN_INFO "HiSax: 2 channels added\n");
1132 for (i = 0; i < MAX_WAITING_CALLS; i++) {
1133 err = init_chan(i + 2, csta);
1134 if (err)
1135 return err;
1137 printk(KERN_INFO "HiSax: MAX_WAITING_CALLS added\n");
1138 if (test_bit(FLG_PTP, &csta->channel->d_st->l2.flag)) {
1139 printk(KERN_INFO "LAYER2 WATCHING ESTABLISH\n");
1140 csta->channel->d_st->lli.l4l3(csta->channel->d_st,
1141 DL_ESTABLISH | REQUEST, NULL);
1143 return (0);
1146 static void
1147 release_d_st(struct Channel *chanp)
1149 struct PStack *st = chanp->d_st;
1151 if (!st)
1152 return;
1153 releasestack_isdnl2(st);
1154 releasestack_isdnl3(st);
1155 HiSax_rmlist(st->l1.hardware, st);
1156 kfree(st);
1157 chanp->d_st = NULL;
1160 void
1161 CallcFreeChan(struct IsdnCardState *csta)
1163 int i;
1165 for (i = 0; i < 2; i++) {
1166 FsmDelTimer(&csta->channel[i].drel_timer, 74);
1167 FsmDelTimer(&csta->channel[i].dial_timer, 75);
1168 if (i || test_bit(FLG_TWO_DCHAN, &csta->HW_Flags))
1169 release_d_st(csta->channel + i);
1170 if (csta->channel[i].b_st) {
1171 release_b_st(csta->channel + i);
1172 kfree(csta->channel[i].b_st);
1173 csta->channel[i].b_st = NULL;
1174 } else
1175 printk(KERN_WARNING "CallcFreeChan b_st ch%d already freed\n", i);
1176 if (i || test_bit(FLG_TWO_DCHAN, &csta->HW_Flags)) {
1177 release_d_st(csta->channel + i);
1178 } else
1179 csta->channel[i].d_st = NULL;
1183 static void
1184 lldata_handler(struct PStack *st, int pr, void *arg)
1186 struct Channel *chanp = (struct Channel *) st->lli.userdata;
1187 struct sk_buff *skb = arg;
1189 switch (pr) {
1190 case (DL_DATA | INDICATION):
1191 if (chanp->data_open) {
1192 if (chanp->debug & 0x800)
1193 link_debug(chanp, 0, "lldata: %d", skb->len);
1194 chanp->cs->iif.rcvcallb_skb(chanp->cs->myid, chanp->chan, skb);
1195 } else {
1196 link_debug(chanp, 0, "lldata: channel not open");
1197 dev_kfree_skb(skb);
1199 break;
1200 case (DL_ESTABLISH | INDICATION):
1201 case (DL_ESTABLISH | CONFIRM):
1202 FsmEvent(&chanp->fi, EV_BC_EST, NULL);
1203 break;
1204 case (DL_RELEASE | INDICATION):
1205 case (DL_RELEASE | CONFIRM):
1206 FsmEvent(&chanp->fi, EV_BC_REL, NULL);
1207 break;
1208 default:
1209 printk(KERN_WARNING "lldata_handler unknown primitive %#x\n",
1210 pr);
1211 break;
1215 static void
1216 lltrans_handler(struct PStack *st, int pr, void *arg)
1218 struct Channel *chanp = (struct Channel *) st->lli.userdata;
1219 struct sk_buff *skb = arg;
1221 switch (pr) {
1222 case (PH_DATA | INDICATION):
1223 if (chanp->data_open) {
1224 if (chanp->debug & 0x800)
1225 link_debug(chanp, 0, "lltrans: %d", skb->len);
1226 chanp->cs->iif.rcvcallb_skb(chanp->cs->myid, chanp->chan, skb);
1227 } else {
1228 link_debug(chanp, 0, "lltrans: channel not open");
1229 dev_kfree_skb(skb);
1231 break;
1232 case (PH_ACTIVATE | INDICATION):
1233 case (PH_ACTIVATE | CONFIRM):
1234 FsmEvent(&chanp->fi, EV_BC_EST, NULL);
1235 break;
1236 case (PH_DEACTIVATE | INDICATION):
1237 case (PH_DEACTIVATE | CONFIRM):
1238 FsmEvent(&chanp->fi, EV_BC_REL, NULL);
1239 break;
1240 default:
1241 printk(KERN_WARNING "lltrans_handler unknown primitive %#x\n",
1242 pr);
1243 break;
1247 void
1248 lli_writewakeup(struct PStack *st, int len)
1250 struct Channel *chanp = st->lli.userdata;
1251 isdn_ctrl ic;
1253 if (chanp->debug & 0x800)
1254 link_debug(chanp, 0, "llwakeup: %d", len);
1255 ic.driver = chanp->cs->myid;
1256 ic.command = ISDN_STAT_BSENT;
1257 ic.arg = chanp->chan;
1258 ic.parm.length = len;
1259 chanp->cs->iif.statcallb(&ic);
1262 static int
1263 init_b_st(struct Channel *chanp, int incoming)
1265 struct PStack *st = chanp->b_st;
1266 struct IsdnCardState *cs = chanp->cs;
1267 char tmp[16];
1269 st->l1.hardware = cs;
1270 if (chanp->leased)
1271 st->l1.bc = chanp->chan & 1;
1272 else
1273 st->l1.bc = chanp->proc->para.bchannel - 1;
1274 switch (chanp->l2_active_protocol) {
1275 case (ISDN_PROTO_L2_X75I):
1276 case (ISDN_PROTO_L2_HDLC):
1277 st->l1.mode = L1_MODE_HDLC;
1278 break;
1279 case (ISDN_PROTO_L2_HDLC_56K):
1280 st->l1.mode = L1_MODE_HDLC_56K;
1281 break;
1282 case (ISDN_PROTO_L2_TRANS):
1283 st->l1.mode = L1_MODE_TRANS;
1284 break;
1285 case (ISDN_PROTO_L2_MODEM):
1286 st->l1.mode = L1_MODE_V32;
1287 break;
1288 case (ISDN_PROTO_L2_FAX):
1289 st->l1.mode = L1_MODE_FAX;
1290 break;
1292 chanp->bcs->conmsg = NULL;
1293 if (chanp->bcs->BC_SetStack(st, chanp->bcs))
1294 return (-1);
1295 st->l2.flag = 0;
1296 test_and_set_bit(FLG_LAPB, &st->l2.flag);
1297 st->l2.maxlen = MAX_DATA_SIZE;
1298 if (!incoming)
1299 test_and_set_bit(FLG_ORIG, &st->l2.flag);
1300 st->l2.T200 = 1000; /* 1000 milliseconds */
1301 st->l2.window = 7;
1302 st->l2.N200 = 4; /* try 4 times */
1303 st->l2.T203 = 5000; /* 5000 milliseconds */
1304 st->l3.debug = 0;
1305 switch (chanp->l2_active_protocol) {
1306 case (ISDN_PROTO_L2_X75I):
1307 sprintf(tmp, "Ch%d X.75", chanp->chan);
1308 setstack_isdnl2(st, tmp);
1309 setstack_l3bc(st, chanp);
1310 st->l2.l2l3 = lldata_handler;
1311 st->lli.userdata = chanp;
1312 test_and_clear_bit(FLG_LLI_L1WAKEUP, &st->lli.flag);
1313 test_and_set_bit(FLG_LLI_L2WAKEUP, &st->lli.flag);
1314 st->l2.l2m.debug = chanp->debug & 16;
1315 st->l2.debug = chanp->debug & 64;
1316 break;
1317 case (ISDN_PROTO_L2_HDLC):
1318 case (ISDN_PROTO_L2_HDLC_56K):
1319 case (ISDN_PROTO_L2_TRANS):
1320 case (ISDN_PROTO_L2_MODEM):
1321 case (ISDN_PROTO_L2_FAX):
1322 st->l1.l1l2 = lltrans_handler;
1323 st->lli.userdata = chanp;
1324 test_and_set_bit(FLG_LLI_L1WAKEUP, &st->lli.flag);
1325 test_and_clear_bit(FLG_LLI_L2WAKEUP, &st->lli.flag);
1326 setstack_transl2(st);
1327 setstack_l3bc(st, chanp);
1328 break;
1330 test_and_set_bit(FLG_START_B, &chanp->Flags);
1331 return (0);
1334 static void
1335 leased_l4l3(struct PStack *st, int pr, void *arg)
1337 struct Channel *chanp = (struct Channel *) st->lli.userdata;
1338 struct sk_buff *skb = arg;
1340 switch (pr) {
1341 case (DL_DATA | REQUEST):
1342 link_debug(chanp, 0, "leased line d-channel DATA");
1343 dev_kfree_skb(skb);
1344 break;
1345 case (DL_ESTABLISH | REQUEST):
1346 st->l2.l2l1(st, PH_ACTIVATE | REQUEST, NULL);
1347 break;
1348 case (DL_RELEASE | REQUEST):
1349 break;
1350 default:
1351 printk(KERN_WARNING "transd_l4l3 unknown primitive %#x\n",
1352 pr);
1353 break;
1357 static void
1358 leased_l1l2(struct PStack *st, int pr, void *arg)
1360 struct Channel *chanp = (struct Channel *) st->lli.userdata;
1361 struct sk_buff *skb = arg;
1362 int i, event = EV_LEASED_REL;
1364 switch (pr) {
1365 case (PH_DATA | INDICATION):
1366 link_debug(chanp, 0, "leased line d-channel DATA");
1367 dev_kfree_skb(skb);
1368 break;
1369 case (PH_ACTIVATE | INDICATION):
1370 case (PH_ACTIVATE | CONFIRM):
1371 event = EV_LEASED;
1372 /* fall through */
1373 case (PH_DEACTIVATE | INDICATION):
1374 case (PH_DEACTIVATE | CONFIRM):
1375 if (test_bit(FLG_TWO_DCHAN, &chanp->cs->HW_Flags))
1376 i = 1;
1377 else
1378 i = 0;
1379 while (i < 2) {
1380 FsmEvent(&chanp->fi, event, NULL);
1381 chanp++;
1382 i++;
1384 break;
1385 default:
1386 printk(KERN_WARNING
1387 "transd_l1l2 unknown primitive %#x\n", pr);
1388 break;
1392 static void
1393 distr_debug(struct IsdnCardState *csta, int debugflags)
1395 int i;
1396 struct Channel *chanp = csta->channel;
1398 for (i = 0; i < (2 + MAX_WAITING_CALLS); i++) {
1399 chanp[i].debug = debugflags;
1400 chanp[i].fi.debug = debugflags & 2;
1401 chanp[i].d_st->l2.l2m.debug = debugflags & 8;
1402 chanp[i].b_st->l2.l2m.debug = debugflags & 0x10;
1403 chanp[i].d_st->l2.debug = debugflags & 0x20;
1404 chanp[i].b_st->l2.debug = debugflags & 0x40;
1405 chanp[i].d_st->l3.l3m.debug = debugflags & 0x80;
1406 chanp[i].b_st->l3.l3m.debug = debugflags & 0x100;
1407 chanp[i].b_st->ma.tei_m.debug = debugflags & 0x200;
1408 chanp[i].b_st->ma.debug = debugflags & 0x200;
1409 chanp[i].d_st->l1.l1m.debug = debugflags & 0x1000;
1410 chanp[i].b_st->l1.l1m.debug = debugflags & 0x2000;
1412 if (debugflags & 4)
1413 csta->debug |= DEB_DLOG_HEX;
1414 else
1415 csta->debug &= ~DEB_DLOG_HEX;
1418 static char tmpbuf[256];
1420 static void
1421 capi_debug(struct Channel *chanp, capi_msg *cm)
1423 char *t = tmpbuf;
1425 t += QuickHex(t, (u_char *)cm, (cm->Length > 50) ? 50 : cm->Length);
1426 t--;
1427 *t = 0;
1428 HiSax_putstatus(chanp->cs, "Ch", "%d CAPIMSG %s", chanp->chan, tmpbuf);
1431 static void
1432 lli_got_fac_req(struct Channel *chanp, capi_msg *cm) {
1433 if ((cm->para[0] != 3) || (cm->para[1] != 0))
1434 return;
1435 if (cm->para[2] < 3)
1436 return;
1437 if (cm->para[4] != 0)
1438 return;
1439 switch (cm->para[3]) {
1440 case 4: /* Suspend */
1441 strncpy(chanp->setup.phone, &cm->para[5], cm->para[5] + 1);
1442 FsmEvent(&chanp->fi, EV_SUSPEND, cm);
1443 break;
1444 case 5: /* Resume */
1445 strncpy(chanp->setup.phone, &cm->para[5], cm->para[5] + 1);
1446 if (chanp->fi.state == ST_NULL) {
1447 FsmEvent(&chanp->fi, EV_RESUME, cm);
1448 } else {
1449 FsmDelTimer(&chanp->dial_timer, 72);
1450 FsmAddTimer(&chanp->dial_timer, 80, EV_RESUME, cm, 73);
1452 break;
1456 static void
1457 lli_got_manufacturer(struct Channel *chanp, struct IsdnCardState *cs, capi_msg *cm) {
1458 if ((cs->typ == ISDN_CTYPE_ELSA) || (cs->typ == ISDN_CTYPE_ELSA_PNP) ||
1459 (cs->typ == ISDN_CTYPE_ELSA_PCI)) {
1460 if (cs->hw.elsa.MFlag) {
1461 cs->cardmsg(cs, CARD_AUX_IND, cm->para);
1467 /***************************************************************/
1468 /* Limit the available number of channels for the current card */
1469 /***************************************************************/
1470 static int
1471 set_channel_limit(struct IsdnCardState *cs, int chanmax)
1473 isdn_ctrl ic;
1474 int i, ii;
1476 if ((chanmax < 0) || (chanmax > 2))
1477 return (-EINVAL);
1478 cs->chanlimit = 0;
1479 for (ii = 0; ii < 2; ii++) {
1480 ic.driver = cs->myid;
1481 ic.command = ISDN_STAT_DISCH;
1482 ic.arg = ii;
1483 if (ii >= chanmax)
1484 ic.parm.num[0] = 0; /* disabled */
1485 else
1486 ic.parm.num[0] = 1; /* enabled */
1487 i = cs->iif.statcallb(&ic);
1488 if (i) return (-EINVAL);
1489 if (ii < chanmax)
1490 cs->chanlimit++;
1492 return (0);
1493 } /* set_channel_limit */
1496 HiSax_command(isdn_ctrl *ic)
1498 struct IsdnCardState *csta = hisax_findcard(ic->driver);
1499 struct PStack *st;
1500 struct Channel *chanp;
1501 int i;
1502 u_int num;
1504 if (!csta) {
1505 printk(KERN_ERR
1506 "HiSax: if_command %d called with invalid driverId %d!\n",
1507 ic->command, ic->driver);
1508 return -ENODEV;
1510 switch (ic->command) {
1511 case (ISDN_CMD_SETEAZ):
1512 chanp = csta->channel + ic->arg;
1513 break;
1514 case (ISDN_CMD_SETL2):
1515 chanp = csta->channel + (ic->arg & 0xff);
1516 if (chanp->debug & 1)
1517 link_debug(chanp, 1, "SETL2 card %d %ld",
1518 csta->cardnr + 1, ic->arg >> 8);
1519 chanp->l2_protocol = ic->arg >> 8;
1520 break;
1521 case (ISDN_CMD_SETL3):
1522 chanp = csta->channel + (ic->arg & 0xff);
1523 if (chanp->debug & 1)
1524 link_debug(chanp, 1, "SETL3 card %d %ld",
1525 csta->cardnr + 1, ic->arg >> 8);
1526 chanp->l3_protocol = ic->arg >> 8;
1527 break;
1528 case (ISDN_CMD_DIAL):
1529 chanp = csta->channel + (ic->arg & 0xff);
1530 if (chanp->debug & 1)
1531 link_debug(chanp, 1, "DIAL %s -> %s (%d,%d)",
1532 ic->parm.setup.eazmsn, ic->parm.setup.phone,
1533 ic->parm.setup.si1, ic->parm.setup.si2);
1534 memcpy(&chanp->setup, &ic->parm.setup, sizeof(setup_parm));
1535 if (!strcmp(chanp->setup.eazmsn, "0"))
1536 chanp->setup.eazmsn[0] = '\0';
1537 /* this solution is dirty and may be change, if
1538 * we make a callreference based callmanager */
1539 if (chanp->fi.state == ST_NULL) {
1540 FsmEvent(&chanp->fi, EV_DIAL, NULL);
1541 } else {
1542 FsmDelTimer(&chanp->dial_timer, 70);
1543 FsmAddTimer(&chanp->dial_timer, 50, EV_DIAL, NULL, 71);
1545 break;
1546 case (ISDN_CMD_ACCEPTB):
1547 chanp = csta->channel + ic->arg;
1548 if (chanp->debug & 1)
1549 link_debug(chanp, 1, "ACCEPTB");
1550 FsmEvent(&chanp->fi, EV_ACCEPTB, NULL);
1551 break;
1552 case (ISDN_CMD_ACCEPTD):
1553 chanp = csta->channel + ic->arg;
1554 memcpy(&chanp->setup, &ic->parm.setup, sizeof(setup_parm));
1555 if (chanp->debug & 1)
1556 link_debug(chanp, 1, "ACCEPTD");
1557 FsmEvent(&chanp->fi, EV_ACCEPTD, NULL);
1558 break;
1559 case (ISDN_CMD_HANGUP):
1560 chanp = csta->channel + ic->arg;
1561 if (chanp->debug & 1)
1562 link_debug(chanp, 1, "HANGUP");
1563 FsmEvent(&chanp->fi, EV_HANGUP, NULL);
1564 break;
1565 case (CAPI_PUT_MESSAGE):
1566 chanp = csta->channel + ic->arg;
1567 if (chanp->debug & 1)
1568 capi_debug(chanp, &ic->parm.cmsg);
1569 if (ic->parm.cmsg.Length < 8)
1570 break;
1571 switch (ic->parm.cmsg.Command) {
1572 case CAPI_FACILITY:
1573 if (ic->parm.cmsg.Subcommand == CAPI_REQ)
1574 lli_got_fac_req(chanp, &ic->parm.cmsg);
1575 break;
1576 case CAPI_MANUFACTURER:
1577 if (ic->parm.cmsg.Subcommand == CAPI_REQ)
1578 lli_got_manufacturer(chanp, csta, &ic->parm.cmsg);
1579 break;
1580 default:
1581 break;
1583 break;
1584 case (ISDN_CMD_IOCTL):
1585 switch (ic->arg) {
1586 case (0):
1587 num = *(unsigned int *) ic->parm.num;
1588 HiSax_reportcard(csta->cardnr, num);
1589 break;
1590 case (1):
1591 num = *(unsigned int *) ic->parm.num;
1592 distr_debug(csta, num);
1593 printk(KERN_DEBUG "HiSax: debugging flags card %d set to %x\n",
1594 csta->cardnr + 1, num);
1595 HiSax_putstatus(csta, "debugging flags ",
1596 "card %d set to %x", csta->cardnr + 1, num);
1597 break;
1598 case (2):
1599 num = *(unsigned int *) ic->parm.num;
1600 csta->channel[0].b_st->l1.delay = num;
1601 csta->channel[1].b_st->l1.delay = num;
1602 HiSax_putstatus(csta, "delay ", "card %d set to %d ms",
1603 csta->cardnr + 1, num);
1604 printk(KERN_DEBUG "HiSax: delay card %d set to %d ms\n",
1605 csta->cardnr + 1, num);
1606 break;
1607 case (5): /* set card in leased mode */
1608 num = *(unsigned int *) ic->parm.num;
1609 if ((num < 1) || (num > 2)) {
1610 HiSax_putstatus(csta, "Set LEASED ",
1611 "wrong channel %d", num);
1612 printk(KERN_WARNING "HiSax: Set LEASED wrong channel %d\n",
1613 num);
1614 } else {
1615 num--;
1616 chanp = csta->channel + num;
1617 chanp->leased = 1;
1618 HiSax_putstatus(csta, "Card",
1619 "%d channel %d set leased mode\n",
1620 csta->cardnr + 1, num + 1);
1621 chanp->d_st->l1.l1l2 = leased_l1l2;
1622 chanp->d_st->lli.l4l3 = leased_l4l3;
1623 chanp->d_st->lli.l4l3(chanp->d_st,
1624 DL_ESTABLISH | REQUEST, NULL);
1626 break;
1627 case (6): /* set B-channel test loop */
1628 num = *(unsigned int *) ic->parm.num;
1629 if (csta->stlist)
1630 csta->stlist->l2.l2l1(csta->stlist,
1631 PH_TESTLOOP | REQUEST, (void *) (long)num);
1632 break;
1633 case (7): /* set card in PTP mode */
1634 num = *(unsigned int *) ic->parm.num;
1635 if (test_bit(FLG_TWO_DCHAN, &csta->HW_Flags)) {
1636 printk(KERN_ERR "HiSax PTP mode only with one TEI possible\n");
1637 } else if (num) {
1638 test_and_set_bit(FLG_PTP, &csta->channel[0].d_st->l2.flag);
1639 test_and_set_bit(FLG_FIXED_TEI, &csta->channel[0].d_st->l2.flag);
1640 csta->channel[0].d_st->l2.tei = 0;
1641 HiSax_putstatus(csta, "set card ", "in PTP mode");
1642 printk(KERN_DEBUG "HiSax: set card in PTP mode\n");
1643 printk(KERN_INFO "LAYER2 WATCHING ESTABLISH\n");
1644 csta->channel[0].d_st->lli.l4l3(csta->channel[0].d_st,
1645 DL_ESTABLISH | REQUEST, NULL);
1646 } else {
1647 test_and_clear_bit(FLG_PTP, &csta->channel[0].d_st->l2.flag);
1648 test_and_clear_bit(FLG_FIXED_TEI, &csta->channel[0].d_st->l2.flag);
1649 HiSax_putstatus(csta, "set card ", "in PTMP mode");
1650 printk(KERN_DEBUG "HiSax: set card in PTMP mode\n");
1652 break;
1653 case (8): /* set card in FIXED TEI mode */
1654 num = *(unsigned int *)ic->parm.num;
1655 chanp = csta->channel + (num & 1);
1656 num = num >> 1;
1657 if (num == 127) {
1658 test_and_clear_bit(FLG_FIXED_TEI, &chanp->d_st->l2.flag);
1659 chanp->d_st->l2.tei = -1;
1660 HiSax_putstatus(csta, "set card ", "in VAR TEI mode");
1661 printk(KERN_DEBUG "HiSax: set card in VAR TEI mode\n");
1662 } else {
1663 test_and_set_bit(FLG_FIXED_TEI, &chanp->d_st->l2.flag);
1664 chanp->d_st->l2.tei = num;
1665 HiSax_putstatus(csta, "set card ", "in FIXED TEI (%d) mode", num);
1666 printk(KERN_DEBUG "HiSax: set card in FIXED TEI (%d) mode\n",
1667 num);
1669 chanp->d_st->lli.l4l3(chanp->d_st,
1670 DL_ESTABLISH | REQUEST, NULL);
1671 break;
1672 case (11):
1673 num = csta->debug & DEB_DLOG_HEX;
1674 csta->debug = *(unsigned int *) ic->parm.num;
1675 csta->debug |= num;
1676 HiSax_putstatus(cards[0].cs, "l1 debugging ",
1677 "flags card %d set to %x",
1678 csta->cardnr + 1, csta->debug);
1679 printk(KERN_DEBUG "HiSax: l1 debugging flags card %d set to %x\n",
1680 csta->cardnr + 1, csta->debug);
1681 break;
1682 case (13):
1683 csta->channel[0].d_st->l3.debug = *(unsigned int *) ic->parm.num;
1684 csta->channel[1].d_st->l3.debug = *(unsigned int *) ic->parm.num;
1685 HiSax_putstatus(cards[0].cs, "l3 debugging ",
1686 "flags card %d set to %x\n", csta->cardnr + 1,
1687 *(unsigned int *) ic->parm.num);
1688 printk(KERN_DEBUG "HiSax: l3 debugging flags card %d set to %x\n",
1689 csta->cardnr + 1, *(unsigned int *) ic->parm.num);
1690 break;
1691 case (10):
1692 i = *(unsigned int *) ic->parm.num;
1693 return (set_channel_limit(csta, i));
1694 default:
1695 if (csta->auxcmd)
1696 return (csta->auxcmd(csta, ic));
1697 printk(KERN_DEBUG "HiSax: invalid ioctl %d\n",
1698 (int) ic->arg);
1699 return (-EINVAL);
1701 break;
1703 case (ISDN_CMD_PROCEED):
1704 chanp = csta->channel + ic->arg;
1705 if (chanp->debug & 1)
1706 link_debug(chanp, 1, "PROCEED");
1707 FsmEvent(&chanp->fi, EV_PROCEED, NULL);
1708 break;
1710 case (ISDN_CMD_ALERT):
1711 chanp = csta->channel + ic->arg;
1712 if (chanp->debug & 1)
1713 link_debug(chanp, 1, "ALERT");
1714 FsmEvent(&chanp->fi, EV_ALERT, NULL);
1715 break;
1717 case (ISDN_CMD_REDIR):
1718 chanp = csta->channel + ic->arg;
1719 if (chanp->debug & 1)
1720 link_debug(chanp, 1, "REDIR");
1721 memcpy(&chanp->setup, &ic->parm.setup, sizeof(setup_parm));
1722 FsmEvent(&chanp->fi, EV_REDIR, NULL);
1723 break;
1725 /* protocol specific io commands */
1726 case (ISDN_CMD_PROT_IO):
1727 for (st = csta->stlist; st; st = st->next)
1728 if (st->protocol == (ic->arg & 0xFF))
1729 return (st->lli.l4l3_proto(st, ic));
1730 return (-EINVAL);
1731 break;
1732 default:
1733 if (csta->auxcmd)
1734 return (csta->auxcmd(csta, ic));
1735 return (-EINVAL);
1737 return (0);
1741 HiSax_writebuf_skb(int id, int chan, int ack, struct sk_buff *skb)
1743 struct IsdnCardState *csta = hisax_findcard(id);
1744 struct Channel *chanp;
1745 struct PStack *st;
1746 int len = skb->len;
1747 struct sk_buff *nskb;
1749 if (!csta) {
1750 printk(KERN_ERR
1751 "HiSax: if_sendbuf called with invalid driverId!\n");
1752 return -ENODEV;
1754 chanp = csta->channel + chan;
1755 st = chanp->b_st;
1756 if (!chanp->data_open) {
1757 link_debug(chanp, 1, "writebuf: channel not open");
1758 return -EIO;
1760 if (len > MAX_DATA_SIZE) {
1761 link_debug(chanp, 1, "writebuf: packet too large (%d bytes)", len);
1762 printk(KERN_WARNING "HiSax_writebuf: packet too large (%d bytes) !\n",
1763 len);
1764 return -EINVAL;
1766 if (len) {
1767 if ((len + chanp->bcs->tx_cnt) > MAX_DATA_MEM) {
1768 /* Must return 0 here, since this is not an error
1769 * but a temporary lack of resources.
1771 if (chanp->debug & 0x800)
1772 link_debug(chanp, 1, "writebuf: no buffers for %d bytes", len);
1773 return 0;
1774 } else if (chanp->debug & 0x800)
1775 link_debug(chanp, 1, "writebuf %d/%d/%d", len, chanp->bcs->tx_cnt, MAX_DATA_MEM);
1776 nskb = skb_clone(skb, GFP_ATOMIC);
1777 if (nskb) {
1778 nskb->truesize = nskb->len;
1779 if (!ack)
1780 nskb->pkt_type = PACKET_NOACK;
1781 if (chanp->l2_active_protocol == ISDN_PROTO_L2_X75I)
1782 st->l3.l3l2(st, DL_DATA | REQUEST, nskb);
1783 else {
1784 chanp->bcs->tx_cnt += len;
1785 st->l2.l2l1(st, PH_DATA | REQUEST, nskb);
1787 dev_kfree_skb(skb);
1788 } else
1789 len = 0;
1791 return (len);