1 /* $Id: isdnl3.c,v 1.1.4.1 2001/11/20 14:19:36 kai Exp $
4 * based on the teles driver from Jan den Ouden
5 * Copyright by Karsten Keil <keil@isdn4linux.de>
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
10 * For changes and modifications please read
11 * ../../../Documentation/isdn/HiSax.cert
13 * Thanks to Jan den Ouden
18 #define __NO_VERSION__
19 #include <linux/init.h>
22 #include <linux/config.h>
24 const char *l3_revision = "$Revision: 1.1.4.1 $";
26 static struct Fsm l3fsm;
36 #define L3_STATE_COUNT (ST_L3_LC_ESTAB+1)
38 static char *strL3State[] =
41 "ST_L3_LC_ESTAB_WAIT",
57 #define L3_EVENT_COUNT (EV_TIMEOUT+1)
59 static char *strL3Event[] =
71 l3m_debug(struct FsmInst *fi, char *fmt, ...)
74 struct PStack *st = fi->userdata;
77 VHiSax_putstatus(st->l1.hardware, st->l3.debug_id, fmt, args);
82 findie(u_char * p, int size, u_char ie, int wanted_set)
84 int l, codeset, maincodeset;
85 u_char *pend = p + size;
87 /* skip protocol discriminator, callref and message type */
94 /* while there are bytes left... */
96 if ((*p & 0xf0) == 0x90) {
99 maincodeset = codeset;
104 if (codeset == wanted_set) {
106 { /* improved length check (Werner Cornelius) */
109 if (*(p+1) > (pend - (p+2)))
120 codeset = maincodeset;
127 getcallref(u_char * p)
131 p++; /* prot discr */
132 if (*p & 0xfe) /* wrong callref BRI only 1 octet*/
134 l = 0xf & *p++; /* callref length */
135 if (!l) /* dummy CallRef */
141 static int OrigCallRef = 0;
146 if (OrigCallRef == 127)
150 return (OrigCallRef);
154 newl3state(struct l3_process *pc, int state)
156 if (pc->debug & L3_DEB_STATE)
157 l3_debug(pc->st, "newstate cr %d %d --> %d",
164 L3ExpireTimer(struct L3Timer *t)
166 t->pc->st->lli.l4l3(t->pc->st, t->event, t->pc);
170 L3InitTimer(struct l3_process *pc, struct L3Timer *t)
173 t->tl.function = (void *) L3ExpireTimer;
174 t->tl.data = (long) t;
179 L3DelTimer(struct L3Timer *t)
185 L3AddTimer(struct L3Timer *t,
186 int millisec, int event)
188 if (timer_pending(&t->tl)) {
189 printk(KERN_WARNING "L3AddTimer: timer already active!\n");
194 t->tl.expires = jiffies + (millisec * HZ) / 1000;
200 StopAllL3Timer(struct l3_process *pc)
202 L3DelTimer(&pc->timer);
206 l3_alloc_skb(int len)
210 if (!(skb = alloc_skb(len + MAX_HEADER_LEN, GFP_ATOMIC))) {
211 printk(KERN_WARNING "HiSax: No skb for D-channel\n");
214 skb_reserve(skb, MAX_HEADER_LEN);
219 no_l3_proto(struct PStack *st, int pr, void *arg)
221 struct sk_buff *skb = arg;
223 HiSax_putstatus(st->l1.hardware, "L3", "no D protocol");
230 no_l3_proto_spec(struct PStack *st, isdn_ctrl *ic)
232 printk(KERN_WARNING "HiSax: no specific protocol handler for proto %lu\n",ic->arg & 0xFF);
236 #ifdef CONFIG_HISAX_EURO
237 extern void setstack_dss1(struct PStack *st);
240 #ifdef CONFIG_HISAX_NI1
241 extern void setstack_ni1(struct PStack *st);
244 #ifdef CONFIG_HISAX_1TR6
245 extern void setstack_1tr6(struct PStack *st);
249 *getl3proc(struct PStack *st, int cr)
251 struct l3_process *p = st->l3.proc;
254 if (p->callref == cr)
262 *new_l3_process(struct PStack *st, int cr)
264 struct l3_process *p, *np;
266 if (!(p = kmalloc(sizeof(struct l3_process), GFP_ATOMIC))) {
267 printk(KERN_ERR "HiSax can't get memory for cr %d\n", cr);
279 p->debug = st->l3.debug;
284 p->N303 = st->l3.N303;
285 L3InitTimer(p, &p->timer);
290 release_l3_process(struct l3_process *p)
292 struct l3_process *np, *pp = NULL;
302 else if (!(p->st->l3.proc = np->next) &&
303 !test_bit(FLG_PTP, &p->st->l2.flag)) {
305 l3_debug(p->st, "release_l3_process: last process");
306 if (!skb_queue_len(&p->st->l3.squeue)) {
308 l3_debug(p->st, "release_l3_process: release link");
309 if (p->st->protocol != ISDN_PTYPE_NI1)
310 FsmEvent(&p->st->l3.l3m, EV_RELEASE_REQ, NULL);
312 FsmEvent(&p->st->l3.l3m, EV_RELEASE_IND, NULL);
315 l3_debug(p->st, "release_l3_process: not release link");
324 printk(KERN_ERR "HiSax internal L3 error CR(%d) not in list\n", p->callref);
325 l3_debug(p->st, "HiSax internal L3 error CR(%d) not in list", p->callref);
329 l3ml3p(struct PStack *st, int pr)
331 struct l3_process *p = st->l3.proc;
332 struct l3_process *np;
335 /* p might be kfreed under us, so we need to save where we want to go on */
337 st->l3.l3ml3(st, pr, p);
343 setstack_l3dc(struct PStack *st, struct Channel *chanp)
348 st->l3.global = NULL;
349 skb_queue_head_init(&st->l3.squeue);
350 st->l3.l3m.fsm = &l3fsm;
351 st->l3.l3m.state = ST_L3_LC_REL;
352 st->l3.l3m.debug = 1;
353 st->l3.l3m.userdata = st;
354 st->l3.l3m.userint = 0;
355 st->l3.l3m.printdebug = l3m_debug;
356 FsmInitTimer(&st->l3.l3m, &st->l3.l3m_timer);
357 strcpy(st->l3.debug_id, "L3DC ");
358 st->lli.l4l3_proto = no_l3_proto_spec;
360 #ifdef CONFIG_HISAX_EURO
361 if (st->protocol == ISDN_PTYPE_EURO) {
365 #ifdef CONFIG_HISAX_NI1
366 if (st->protocol == ISDN_PTYPE_NI1) {
370 #ifdef CONFIG_HISAX_1TR6
371 if (st->protocol == ISDN_PTYPE_1TR6) {
375 if (st->protocol == ISDN_PTYPE_LEASED) {
376 st->lli.l4l3 = no_l3_proto;
377 st->l2.l2l3 = no_l3_proto;
378 st->l3.l3ml3 = no_l3_proto;
379 printk(KERN_INFO "HiSax: Leased line mode\n");
381 st->lli.l4l3 = no_l3_proto;
382 st->l2.l2l3 = no_l3_proto;
383 st->l3.l3ml3 = no_l3_proto;
384 sprintf(tmp, "protocol %s not supported",
385 (st->protocol == ISDN_PTYPE_1TR6) ? "1tr6" :
386 (st->protocol == ISDN_PTYPE_EURO) ? "euro" :
387 (st->protocol == ISDN_PTYPE_NI1) ? "ni1" :
389 printk(KERN_WARNING "HiSax: %s\n", tmp);
395 isdnl3_trans(struct PStack *st, int pr, void *arg) {
396 st->l3.l3l2(st, pr, arg);
400 releasestack_isdnl3(struct PStack *st)
403 release_l3_process(st->l3.proc);
405 StopAllL3Timer(st->l3.global);
406 kfree(st->l3.global);
407 st->l3.global = NULL;
409 FsmDelTimer(&st->l3.l3m_timer, 54);
410 skb_queue_purge(&st->l3.squeue);
414 setstack_l3bc(struct PStack *st, struct Channel *chanp)
418 st->l3.global = NULL;
419 skb_queue_head_init(&st->l3.squeue);
420 st->l3.l3m.fsm = &l3fsm;
421 st->l3.l3m.state = ST_L3_LC_REL;
422 st->l3.l3m.debug = 1;
423 st->l3.l3m.userdata = st;
424 st->l3.l3m.userint = 0;
425 st->l3.l3m.printdebug = l3m_debug;
426 strcpy(st->l3.debug_id, "L3BC ");
427 st->lli.l4l3 = isdnl3_trans;
430 #define DREL_TIMER_VALUE 40000
433 lc_activate(struct FsmInst *fi, int event, void *arg)
435 struct PStack *st = fi->userdata;
437 FsmChangeState(fi, ST_L3_LC_ESTAB_WAIT);
438 st->l3.l3l2(st, DL_ESTABLISH | REQUEST, NULL);
442 lc_connect(struct FsmInst *fi, int event, void *arg)
444 struct PStack *st = fi->userdata;
445 struct sk_buff *skb = arg;
448 FsmChangeState(fi, ST_L3_LC_ESTAB);
449 while ((skb = skb_dequeue(&st->l3.squeue))) {
450 st->l3.l3l2(st, DL_DATA | REQUEST, skb);
453 if ((!st->l3.proc) && dequeued) {
455 l3_debug(st, "lc_connect: release link");
456 FsmEvent(&st->l3.l3m, EV_RELEASE_REQ, NULL);
458 l3ml3p(st, DL_ESTABLISH | INDICATION);
462 lc_connected(struct FsmInst *fi, int event, void *arg)
464 struct PStack *st = fi->userdata;
465 struct sk_buff *skb = arg;
468 FsmDelTimer(&st->l3.l3m_timer, 51);
469 FsmChangeState(fi, ST_L3_LC_ESTAB);
470 while ((skb = skb_dequeue(&st->l3.squeue))) {
471 st->l3.l3l2(st, DL_DATA | REQUEST, skb);
474 if ((!st->l3.proc) && dequeued) {
476 l3_debug(st, "lc_connected: release link");
477 FsmEvent(&st->l3.l3m, EV_RELEASE_REQ, NULL);
479 l3ml3p(st, DL_ESTABLISH | CONFIRM);
483 lc_start_delay(struct FsmInst *fi, int event, void *arg)
485 struct PStack *st = fi->userdata;
487 FsmChangeState(fi, ST_L3_LC_REL_DELAY);
488 FsmAddTimer(&st->l3.l3m_timer, DREL_TIMER_VALUE, EV_TIMEOUT, NULL, 50);
492 lc_start_delay_check(struct FsmInst *fi, int event, void *arg)
493 /* 20/09/00 - GE timer not user for NI-1 as layer 2 should stay up */
495 struct PStack *st = fi->userdata;
497 FsmChangeState(fi, ST_L3_LC_REL_DELAY);
498 /* 19/09/00 - GE timer not user for NI-1 */
499 if (st->protocol != ISDN_PTYPE_NI1)
500 FsmAddTimer(&st->l3.l3m_timer, DREL_TIMER_VALUE, EV_TIMEOUT, NULL, 50);
504 lc_release_req(struct FsmInst *fi, int event, void *arg)
506 struct PStack *st = fi->userdata;
508 if (test_bit(FLG_L2BLOCK, &st->l2.flag)) {
510 l3_debug(st, "lc_release_req: l2 blocked");
511 /* restart release timer */
512 FsmAddTimer(&st->l3.l3m_timer, DREL_TIMER_VALUE, EV_TIMEOUT, NULL, 51);
514 FsmChangeState(fi, ST_L3_LC_REL_WAIT);
515 st->l3.l3l2(st, DL_RELEASE | REQUEST, NULL);
520 lc_release_ind(struct FsmInst *fi, int event, void *arg)
522 struct PStack *st = fi->userdata;
524 FsmDelTimer(&st->l3.l3m_timer, 52);
525 FsmChangeState(fi, ST_L3_LC_REL);
526 skb_queue_purge(&st->l3.squeue);
527 l3ml3p(st, DL_RELEASE | INDICATION);
531 lc_release_cnf(struct FsmInst *fi, int event, void *arg)
533 struct PStack *st = fi->userdata;
535 FsmChangeState(fi, ST_L3_LC_REL);
536 skb_queue_purge(&st->l3.squeue);
537 l3ml3p(st, DL_RELEASE | CONFIRM);
542 static struct FsmNode L3FnList[] __initdata =
544 {ST_L3_LC_REL, EV_ESTABLISH_REQ, lc_activate},
545 {ST_L3_LC_REL, EV_ESTABLISH_IND, lc_connect},
546 {ST_L3_LC_REL, EV_ESTABLISH_CNF, lc_connect},
547 {ST_L3_LC_ESTAB_WAIT, EV_ESTABLISH_CNF, lc_connected},
548 {ST_L3_LC_ESTAB_WAIT, EV_RELEASE_REQ, lc_start_delay},
549 {ST_L3_LC_ESTAB_WAIT, EV_RELEASE_IND, lc_release_ind},
550 {ST_L3_LC_ESTAB, EV_RELEASE_IND, lc_release_ind},
551 {ST_L3_LC_ESTAB, EV_RELEASE_REQ, lc_start_delay_check},
552 {ST_L3_LC_REL_DELAY, EV_RELEASE_IND, lc_release_ind},
553 {ST_L3_LC_REL_DELAY, EV_ESTABLISH_REQ, lc_connected},
554 {ST_L3_LC_REL_DELAY, EV_TIMEOUT, lc_release_req},
555 {ST_L3_LC_REL_WAIT, EV_RELEASE_CNF, lc_release_cnf},
556 {ST_L3_LC_REL_WAIT, EV_ESTABLISH_REQ, lc_activate},
560 #define L3_FN_COUNT (sizeof(L3FnList)/sizeof(struct FsmNode))
563 l3_msg(struct PStack *st, int pr, void *arg)
566 case (DL_DATA | REQUEST):
567 if (st->l3.l3m.state == ST_L3_LC_ESTAB) {
568 st->l3.l3l2(st, pr, arg);
570 struct sk_buff *skb = arg;
572 skb_queue_tail(&st->l3.squeue, skb);
573 FsmEvent(&st->l3.l3m, EV_ESTABLISH_REQ, NULL);
576 case (DL_ESTABLISH | REQUEST):
577 FsmEvent(&st->l3.l3m, EV_ESTABLISH_REQ, NULL);
579 case (DL_ESTABLISH | CONFIRM):
580 FsmEvent(&st->l3.l3m, EV_ESTABLISH_CNF, NULL);
582 case (DL_ESTABLISH | INDICATION):
583 FsmEvent(&st->l3.l3m, EV_ESTABLISH_IND, NULL);
585 case (DL_RELEASE | INDICATION):
586 FsmEvent(&st->l3.l3m, EV_RELEASE_IND, NULL);
588 case (DL_RELEASE | CONFIRM):
589 FsmEvent(&st->l3.l3m, EV_RELEASE_CNF, NULL);
591 case (DL_RELEASE | REQUEST):
592 FsmEvent(&st->l3.l3m, EV_RELEASE_REQ, NULL);
600 l3fsm.state_count = L3_STATE_COUNT;
601 l3fsm.event_count = L3_EVENT_COUNT;
602 l3fsm.strEvent = strL3Event;
603 l3fsm.strState = strL3State;
604 return FsmNew(&l3fsm, L3FnList, L3_FN_COUNT);