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