1 /* $Id: isdnl3.c,v 2.22.2.3 2004/01/13 14:31:25 keil 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 #include <linux/init.h>
19 #include <linux/slab.h>
23 const char *l3_revision = "$Revision: 2.22.2.3 $";
25 static struct Fsm l3fsm;
35 #define L3_STATE_COUNT (ST_L3_LC_ESTAB + 1)
37 static char *strL3State[] =
40 "ST_L3_LC_ESTAB_WAIT",
56 #define L3_EVENT_COUNT (EV_TIMEOUT + 1)
58 static char *strL3Event[] =
69 static __printf(2, 3) void
70 l3m_debug(struct FsmInst *fi, char *fmt, ...)
73 struct PStack *st = fi->userdata;
76 VHiSax_putstatus(st->l1.hardware, st->l3.debug_id, fmt, args);
81 findie(u_char *p, int size, u_char ie, int wanted_set)
83 int l, codeset, maincodeset;
84 u_char *pend = p + size;
86 /* skip protocol discriminator, callref and message type */
93 /* while there are bytes left... */
95 if ((*p & 0xf0) == 0x90) {
98 maincodeset = codeset;
103 if (codeset == wanted_set) {
105 { /* improved length check (Werner Cornelius) */
108 if (*(p + 1) > (pend - (p + 2)))
119 codeset = maincodeset;
126 getcallref(u_char *p)
130 p++; /* prot discr */
131 if (*p & 0xfe) /* wrong callref BRI only 1 octet*/
133 l = 0xf & *p++; /* callref length */
134 if (!l) /* dummy CallRef */
140 static int OrigCallRef = 0;
145 if (OrigCallRef == 127)
149 return (OrigCallRef);
153 newl3state(struct l3_process *pc, int state)
155 if (pc->debug & L3_DEB_STATE)
156 l3_debug(pc->st, "%s cr %d %d --> %d", __func__,
163 L3ExpireTimer(struct L3Timer *t)
165 t->pc->st->lli.l4l3(t->pc->st, t->event, t->pc);
169 L3InitTimer(struct l3_process *pc, struct L3Timer *t)
172 t->tl.function = (void *) L3ExpireTimer;
173 t->tl.data = (long) t;
178 L3DelTimer(struct L3Timer *t)
184 L3AddTimer(struct L3Timer *t,
185 int millisec, int event)
187 if (timer_pending(&t->tl)) {
188 printk(KERN_WARNING "L3AddTimer: timer already active!\n");
193 t->tl.expires = jiffies + (millisec * HZ) / 1000;
199 StopAllL3Timer(struct l3_process *pc)
201 L3DelTimer(&pc->timer);
205 l3_alloc_skb(int len)
209 if (!(skb = alloc_skb(len + MAX_HEADER_LEN, GFP_ATOMIC))) {
210 printk(KERN_WARNING "HiSax: No skb for D-channel\n");
213 skb_reserve(skb, MAX_HEADER_LEN);
218 no_l3_proto(struct PStack *st, int pr, void *arg)
220 struct sk_buff *skb = arg;
222 HiSax_putstatus(st->l1.hardware, "L3", "no D protocol");
229 no_l3_proto_spec(struct PStack *st, isdn_ctrl *ic)
231 printk(KERN_WARNING "HiSax: no specific protocol handler for proto %lu\n", ic->arg & 0xFF);
236 *getl3proc(struct PStack *st, int cr)
238 struct l3_process *p = st->l3.proc;
241 if (p->callref == cr)
249 *new_l3_process(struct PStack *st, int cr)
251 struct l3_process *p, *np;
253 if (!(p = kmalloc(sizeof(struct l3_process), GFP_ATOMIC))) {
254 printk(KERN_ERR "HiSax can't get memory for cr %d\n", cr);
266 p->debug = st->l3.debug;
271 p->N303 = st->l3.N303;
272 L3InitTimer(p, &p->timer);
277 release_l3_process(struct l3_process *p)
279 struct l3_process *np, *pp = NULL;
289 else if (!(p->st->l3.proc = np->next) &&
290 !test_bit(FLG_PTP, &p->st->l2.flag)) {
292 l3_debug(p->st, "release_l3_process: last process");
293 if (skb_queue_empty(&p->st->l3.squeue)) {
295 l3_debug(p->st, "release_l3_process: release link");
296 if (p->st->protocol != ISDN_PTYPE_NI1)
297 FsmEvent(&p->st->l3.l3m, EV_RELEASE_REQ, NULL);
299 FsmEvent(&p->st->l3.l3m, EV_RELEASE_IND, NULL);
302 l3_debug(p->st, "release_l3_process: not release link");
311 printk(KERN_ERR "HiSax internal L3 error CR(%d) not in list\n", p->callref);
312 l3_debug(p->st, "HiSax internal L3 error CR(%d) not in list", p->callref);
316 l3ml3p(struct PStack *st, int pr)
318 struct l3_process *p = st->l3.proc;
319 struct l3_process *np;
322 /* p might be kfreed under us, so we need to save where we want to go on */
324 st->l3.l3ml3(st, pr, p);
330 setstack_l3dc(struct PStack *st, struct Channel *chanp)
335 st->l3.global = NULL;
336 skb_queue_head_init(&st->l3.squeue);
337 st->l3.l3m.fsm = &l3fsm;
338 st->l3.l3m.state = ST_L3_LC_REL;
339 st->l3.l3m.debug = 1;
340 st->l3.l3m.userdata = st;
341 st->l3.l3m.userint = 0;
342 st->l3.l3m.printdebug = l3m_debug;
343 FsmInitTimer(&st->l3.l3m, &st->l3.l3m_timer);
344 strcpy(st->l3.debug_id, "L3DC ");
345 st->lli.l4l3_proto = no_l3_proto_spec;
347 #ifdef CONFIG_HISAX_EURO
348 if (st->protocol == ISDN_PTYPE_EURO) {
352 #ifdef CONFIG_HISAX_NI1
353 if (st->protocol == ISDN_PTYPE_NI1) {
357 #ifdef CONFIG_HISAX_1TR6
358 if (st->protocol == ISDN_PTYPE_1TR6) {
362 if (st->protocol == ISDN_PTYPE_LEASED) {
363 st->lli.l4l3 = no_l3_proto;
364 st->l2.l2l3 = no_l3_proto;
365 st->l3.l3ml3 = no_l3_proto;
366 printk(KERN_INFO "HiSax: Leased line mode\n");
368 st->lli.l4l3 = no_l3_proto;
369 st->l2.l2l3 = no_l3_proto;
370 st->l3.l3ml3 = no_l3_proto;
371 sprintf(tmp, "protocol %s not supported",
372 (st->protocol == ISDN_PTYPE_1TR6) ? "1tr6" :
373 (st->protocol == ISDN_PTYPE_EURO) ? "euro" :
374 (st->protocol == ISDN_PTYPE_NI1) ? "ni1" :
376 printk(KERN_WARNING "HiSax: %s\n", tmp);
382 isdnl3_trans(struct PStack *st, int pr, void *arg) {
383 st->l3.l3l2(st, pr, arg);
387 releasestack_isdnl3(struct PStack *st)
390 release_l3_process(st->l3.proc);
392 StopAllL3Timer(st->l3.global);
393 kfree(st->l3.global);
394 st->l3.global = NULL;
396 FsmDelTimer(&st->l3.l3m_timer, 54);
397 skb_queue_purge(&st->l3.squeue);
401 setstack_l3bc(struct PStack *st, struct Channel *chanp)
405 st->l3.global = NULL;
406 skb_queue_head_init(&st->l3.squeue);
407 st->l3.l3m.fsm = &l3fsm;
408 st->l3.l3m.state = ST_L3_LC_REL;
409 st->l3.l3m.debug = 1;
410 st->l3.l3m.userdata = st;
411 st->l3.l3m.userint = 0;
412 st->l3.l3m.printdebug = l3m_debug;
413 strcpy(st->l3.debug_id, "L3BC ");
414 st->lli.l4l3 = isdnl3_trans;
417 #define DREL_TIMER_VALUE 40000
420 lc_activate(struct FsmInst *fi, int event, void *arg)
422 struct PStack *st = fi->userdata;
424 FsmChangeState(fi, ST_L3_LC_ESTAB_WAIT);
425 st->l3.l3l2(st, DL_ESTABLISH | REQUEST, NULL);
429 lc_connect(struct FsmInst *fi, int event, void *arg)
431 struct PStack *st = fi->userdata;
432 struct sk_buff *skb = arg;
435 FsmChangeState(fi, ST_L3_LC_ESTAB);
436 while ((skb = skb_dequeue(&st->l3.squeue))) {
437 st->l3.l3l2(st, DL_DATA | REQUEST, skb);
440 if ((!st->l3.proc) && dequeued) {
442 l3_debug(st, "lc_connect: release link");
443 FsmEvent(&st->l3.l3m, EV_RELEASE_REQ, NULL);
445 l3ml3p(st, DL_ESTABLISH | INDICATION);
449 lc_connected(struct FsmInst *fi, int event, void *arg)
451 struct PStack *st = fi->userdata;
452 struct sk_buff *skb = arg;
455 FsmDelTimer(&st->l3.l3m_timer, 51);
456 FsmChangeState(fi, ST_L3_LC_ESTAB);
457 while ((skb = skb_dequeue(&st->l3.squeue))) {
458 st->l3.l3l2(st, DL_DATA | REQUEST, skb);
461 if ((!st->l3.proc) && dequeued) {
463 l3_debug(st, "lc_connected: release link");
464 FsmEvent(&st->l3.l3m, EV_RELEASE_REQ, NULL);
466 l3ml3p(st, DL_ESTABLISH | CONFIRM);
470 lc_start_delay(struct FsmInst *fi, int event, void *arg)
472 struct PStack *st = fi->userdata;
474 FsmChangeState(fi, ST_L3_LC_REL_DELAY);
475 FsmAddTimer(&st->l3.l3m_timer, DREL_TIMER_VALUE, EV_TIMEOUT, NULL, 50);
479 lc_start_delay_check(struct FsmInst *fi, int event, void *arg)
480 /* 20/09/00 - GE timer not user for NI-1 as layer 2 should stay up */
482 struct PStack *st = fi->userdata;
484 FsmChangeState(fi, ST_L3_LC_REL_DELAY);
485 /* 19/09/00 - GE timer not user for NI-1 */
486 if (st->protocol != ISDN_PTYPE_NI1)
487 FsmAddTimer(&st->l3.l3m_timer, DREL_TIMER_VALUE, EV_TIMEOUT, NULL, 50);
491 lc_release_req(struct FsmInst *fi, int event, void *arg)
493 struct PStack *st = fi->userdata;
495 if (test_bit(FLG_L2BLOCK, &st->l2.flag)) {
497 l3_debug(st, "lc_release_req: l2 blocked");
498 /* restart release timer */
499 FsmAddTimer(&st->l3.l3m_timer, DREL_TIMER_VALUE, EV_TIMEOUT, NULL, 51);
501 FsmChangeState(fi, ST_L3_LC_REL_WAIT);
502 st->l3.l3l2(st, DL_RELEASE | REQUEST, NULL);
507 lc_release_ind(struct FsmInst *fi, int event, void *arg)
509 struct PStack *st = fi->userdata;
511 FsmDelTimer(&st->l3.l3m_timer, 52);
512 FsmChangeState(fi, ST_L3_LC_REL);
513 skb_queue_purge(&st->l3.squeue);
514 l3ml3p(st, DL_RELEASE | INDICATION);
518 lc_release_cnf(struct FsmInst *fi, int event, void *arg)
520 struct PStack *st = fi->userdata;
522 FsmChangeState(fi, ST_L3_LC_REL);
523 skb_queue_purge(&st->l3.squeue);
524 l3ml3p(st, DL_RELEASE | CONFIRM);
529 static struct FsmNode L3FnList[] __initdata =
531 {ST_L3_LC_REL, EV_ESTABLISH_REQ, lc_activate},
532 {ST_L3_LC_REL, EV_ESTABLISH_IND, lc_connect},
533 {ST_L3_LC_REL, EV_ESTABLISH_CNF, lc_connect},
534 {ST_L3_LC_ESTAB_WAIT, EV_ESTABLISH_CNF, lc_connected},
535 {ST_L3_LC_ESTAB_WAIT, EV_RELEASE_REQ, lc_start_delay},
536 {ST_L3_LC_ESTAB_WAIT, EV_RELEASE_IND, lc_release_ind},
537 {ST_L3_LC_ESTAB, EV_RELEASE_IND, lc_release_ind},
538 {ST_L3_LC_ESTAB, EV_RELEASE_REQ, lc_start_delay_check},
539 {ST_L3_LC_REL_DELAY, EV_RELEASE_IND, lc_release_ind},
540 {ST_L3_LC_REL_DELAY, EV_ESTABLISH_REQ, lc_connected},
541 {ST_L3_LC_REL_DELAY, EV_TIMEOUT, lc_release_req},
542 {ST_L3_LC_REL_WAIT, EV_RELEASE_CNF, lc_release_cnf},
543 {ST_L3_LC_REL_WAIT, EV_ESTABLISH_REQ, lc_activate},
548 l3_msg(struct PStack *st, int pr, void *arg)
551 case (DL_DATA | REQUEST):
552 if (st->l3.l3m.state == ST_L3_LC_ESTAB) {
553 st->l3.l3l2(st, pr, arg);
555 struct sk_buff *skb = arg;
557 skb_queue_tail(&st->l3.squeue, skb);
558 FsmEvent(&st->l3.l3m, EV_ESTABLISH_REQ, NULL);
561 case (DL_ESTABLISH | REQUEST):
562 FsmEvent(&st->l3.l3m, EV_ESTABLISH_REQ, NULL);
564 case (DL_ESTABLISH | CONFIRM):
565 FsmEvent(&st->l3.l3m, EV_ESTABLISH_CNF, NULL);
567 case (DL_ESTABLISH | INDICATION):
568 FsmEvent(&st->l3.l3m, EV_ESTABLISH_IND, NULL);
570 case (DL_RELEASE | INDICATION):
571 FsmEvent(&st->l3.l3m, EV_RELEASE_IND, NULL);
573 case (DL_RELEASE | CONFIRM):
574 FsmEvent(&st->l3.l3m, EV_RELEASE_CNF, NULL);
576 case (DL_RELEASE | REQUEST):
577 FsmEvent(&st->l3.l3m, EV_RELEASE_REQ, NULL);
585 l3fsm.state_count = L3_STATE_COUNT;
586 l3fsm.event_count = L3_EVENT_COUNT;
587 l3fsm.strEvent = strL3Event;
588 l3fsm.strState = strL3State;
589 return FsmNew(&l3fsm, L3FnList, ARRAY_SIZE(L3FnList));