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 timer_list
*timer
)
165 struct L3Timer
*t
= from_timer(t
, timer
, tl
);
166 t
->pc
->st
->lli
.l4l3(t
->pc
->st
, t
->event
, t
->pc
);
170 L3InitTimer(struct l3_process
*pc
, struct L3Timer
*t
)
173 timer_setup(&t
->tl
, L3ExpireTimer
, 0);
177 L3DelTimer(struct L3Timer
*t
)
183 L3AddTimer(struct L3Timer
*t
,
184 int millisec
, int event
)
186 if (timer_pending(&t
->tl
)) {
187 printk(KERN_WARNING
"L3AddTimer: timer already active!\n");
191 t
->tl
.expires
= jiffies
+ (millisec
* HZ
) / 1000;
197 StopAllL3Timer(struct l3_process
*pc
)
199 L3DelTimer(&pc
->timer
);
203 l3_alloc_skb(int len
)
207 if (!(skb
= alloc_skb(len
+ MAX_HEADER_LEN
, GFP_ATOMIC
))) {
208 printk(KERN_WARNING
"HiSax: No skb for D-channel\n");
211 skb_reserve(skb
, MAX_HEADER_LEN
);
216 no_l3_proto(struct PStack
*st
, int pr
, void *arg
)
218 struct sk_buff
*skb
= arg
;
220 HiSax_putstatus(st
->l1
.hardware
, "L3", "no D protocol");
227 no_l3_proto_spec(struct PStack
*st
, isdn_ctrl
*ic
)
229 printk(KERN_WARNING
"HiSax: no specific protocol handler for proto %lu\n", ic
->arg
& 0xFF);
234 *getl3proc(struct PStack
*st
, int cr
)
236 struct l3_process
*p
= st
->l3
.proc
;
239 if (p
->callref
== cr
)
247 *new_l3_process(struct PStack
*st
, int cr
)
249 struct l3_process
*p
, *np
;
251 if (!(p
= kmalloc(sizeof(struct l3_process
), GFP_ATOMIC
))) {
252 printk(KERN_ERR
"HiSax can't get memory for cr %d\n", cr
);
264 p
->debug
= st
->l3
.debug
;
269 p
->N303
= st
->l3
.N303
;
270 L3InitTimer(p
, &p
->timer
);
275 release_l3_process(struct l3_process
*p
)
277 struct l3_process
*np
, *pp
= NULL
;
287 else if (!(p
->st
->l3
.proc
= np
->next
) &&
288 !test_bit(FLG_PTP
, &p
->st
->l2
.flag
)) {
290 l3_debug(p
->st
, "release_l3_process: last process");
291 if (skb_queue_empty(&p
->st
->l3
.squeue
)) {
293 l3_debug(p
->st
, "release_l3_process: release link");
294 if (p
->st
->protocol
!= ISDN_PTYPE_NI1
)
295 FsmEvent(&p
->st
->l3
.l3m
, EV_RELEASE_REQ
, NULL
);
297 FsmEvent(&p
->st
->l3
.l3m
, EV_RELEASE_IND
, NULL
);
300 l3_debug(p
->st
, "release_l3_process: not release link");
309 printk(KERN_ERR
"HiSax internal L3 error CR(%d) not in list\n", p
->callref
);
310 l3_debug(p
->st
, "HiSax internal L3 error CR(%d) not in list", p
->callref
);
314 l3ml3p(struct PStack
*st
, int pr
)
316 struct l3_process
*p
= st
->l3
.proc
;
317 struct l3_process
*np
;
320 /* p might be kfreed under us, so we need to save where we want to go on */
322 st
->l3
.l3ml3(st
, pr
, p
);
328 setstack_l3dc(struct PStack
*st
, struct Channel
*chanp
)
333 st
->l3
.global
= NULL
;
334 skb_queue_head_init(&st
->l3
.squeue
);
335 st
->l3
.l3m
.fsm
= &l3fsm
;
336 st
->l3
.l3m
.state
= ST_L3_LC_REL
;
337 st
->l3
.l3m
.debug
= 1;
338 st
->l3
.l3m
.userdata
= st
;
339 st
->l3
.l3m
.userint
= 0;
340 st
->l3
.l3m
.printdebug
= l3m_debug
;
341 FsmInitTimer(&st
->l3
.l3m
, &st
->l3
.l3m_timer
);
342 strcpy(st
->l3
.debug_id
, "L3DC ");
343 st
->lli
.l4l3_proto
= no_l3_proto_spec
;
345 #ifdef CONFIG_HISAX_EURO
346 if (st
->protocol
== ISDN_PTYPE_EURO
) {
350 #ifdef CONFIG_HISAX_NI1
351 if (st
->protocol
== ISDN_PTYPE_NI1
) {
355 #ifdef CONFIG_HISAX_1TR6
356 if (st
->protocol
== ISDN_PTYPE_1TR6
) {
360 if (st
->protocol
== ISDN_PTYPE_LEASED
) {
361 st
->lli
.l4l3
= no_l3_proto
;
362 st
->l2
.l2l3
= no_l3_proto
;
363 st
->l3
.l3ml3
= no_l3_proto
;
364 printk(KERN_INFO
"HiSax: Leased line mode\n");
366 st
->lli
.l4l3
= no_l3_proto
;
367 st
->l2
.l2l3
= no_l3_proto
;
368 st
->l3
.l3ml3
= no_l3_proto
;
369 sprintf(tmp
, "protocol %s not supported",
370 (st
->protocol
== ISDN_PTYPE_1TR6
) ? "1tr6" :
371 (st
->protocol
== ISDN_PTYPE_EURO
) ? "euro" :
372 (st
->protocol
== ISDN_PTYPE_NI1
) ? "ni1" :
374 printk(KERN_WARNING
"HiSax: %s\n", tmp
);
380 isdnl3_trans(struct PStack
*st
, int pr
, void *arg
) {
381 st
->l3
.l3l2(st
, pr
, arg
);
385 releasestack_isdnl3(struct PStack
*st
)
388 release_l3_process(st
->l3
.proc
);
390 StopAllL3Timer(st
->l3
.global
);
391 kfree(st
->l3
.global
);
392 st
->l3
.global
= NULL
;
394 FsmDelTimer(&st
->l3
.l3m_timer
, 54);
395 skb_queue_purge(&st
->l3
.squeue
);
399 setstack_l3bc(struct PStack
*st
, struct Channel
*chanp
)
403 st
->l3
.global
= NULL
;
404 skb_queue_head_init(&st
->l3
.squeue
);
405 st
->l3
.l3m
.fsm
= &l3fsm
;
406 st
->l3
.l3m
.state
= ST_L3_LC_REL
;
407 st
->l3
.l3m
.debug
= 1;
408 st
->l3
.l3m
.userdata
= st
;
409 st
->l3
.l3m
.userint
= 0;
410 st
->l3
.l3m
.printdebug
= l3m_debug
;
411 strcpy(st
->l3
.debug_id
, "L3BC ");
412 st
->lli
.l4l3
= isdnl3_trans
;
415 #define DREL_TIMER_VALUE 40000
418 lc_activate(struct FsmInst
*fi
, int event
, void *arg
)
420 struct PStack
*st
= fi
->userdata
;
422 FsmChangeState(fi
, ST_L3_LC_ESTAB_WAIT
);
423 st
->l3
.l3l2(st
, DL_ESTABLISH
| REQUEST
, NULL
);
427 lc_connect(struct FsmInst
*fi
, int event
, void *arg
)
429 struct PStack
*st
= fi
->userdata
;
430 struct sk_buff
*skb
= arg
;
433 FsmChangeState(fi
, ST_L3_LC_ESTAB
);
434 while ((skb
= skb_dequeue(&st
->l3
.squeue
))) {
435 st
->l3
.l3l2(st
, DL_DATA
| REQUEST
, skb
);
438 if ((!st
->l3
.proc
) && dequeued
) {
440 l3_debug(st
, "lc_connect: release link");
441 FsmEvent(&st
->l3
.l3m
, EV_RELEASE_REQ
, NULL
);
443 l3ml3p(st
, DL_ESTABLISH
| INDICATION
);
447 lc_connected(struct FsmInst
*fi
, int event
, void *arg
)
449 struct PStack
*st
= fi
->userdata
;
450 struct sk_buff
*skb
= arg
;
453 FsmDelTimer(&st
->l3
.l3m_timer
, 51);
454 FsmChangeState(fi
, ST_L3_LC_ESTAB
);
455 while ((skb
= skb_dequeue(&st
->l3
.squeue
))) {
456 st
->l3
.l3l2(st
, DL_DATA
| REQUEST
, skb
);
459 if ((!st
->l3
.proc
) && dequeued
) {
461 l3_debug(st
, "lc_connected: release link");
462 FsmEvent(&st
->l3
.l3m
, EV_RELEASE_REQ
, NULL
);
464 l3ml3p(st
, DL_ESTABLISH
| CONFIRM
);
468 lc_start_delay(struct FsmInst
*fi
, int event
, void *arg
)
470 struct PStack
*st
= fi
->userdata
;
472 FsmChangeState(fi
, ST_L3_LC_REL_DELAY
);
473 FsmAddTimer(&st
->l3
.l3m_timer
, DREL_TIMER_VALUE
, EV_TIMEOUT
, NULL
, 50);
477 lc_start_delay_check(struct FsmInst
*fi
, int event
, void *arg
)
478 /* 20/09/00 - GE timer not user for NI-1 as layer 2 should stay up */
480 struct PStack
*st
= fi
->userdata
;
482 FsmChangeState(fi
, ST_L3_LC_REL_DELAY
);
483 /* 19/09/00 - GE timer not user for NI-1 */
484 if (st
->protocol
!= ISDN_PTYPE_NI1
)
485 FsmAddTimer(&st
->l3
.l3m_timer
, DREL_TIMER_VALUE
, EV_TIMEOUT
, NULL
, 50);
489 lc_release_req(struct FsmInst
*fi
, int event
, void *arg
)
491 struct PStack
*st
= fi
->userdata
;
493 if (test_bit(FLG_L2BLOCK
, &st
->l2
.flag
)) {
495 l3_debug(st
, "lc_release_req: l2 blocked");
496 /* restart release timer */
497 FsmAddTimer(&st
->l3
.l3m_timer
, DREL_TIMER_VALUE
, EV_TIMEOUT
, NULL
, 51);
499 FsmChangeState(fi
, ST_L3_LC_REL_WAIT
);
500 st
->l3
.l3l2(st
, DL_RELEASE
| REQUEST
, NULL
);
505 lc_release_ind(struct FsmInst
*fi
, int event
, void *arg
)
507 struct PStack
*st
= fi
->userdata
;
509 FsmDelTimer(&st
->l3
.l3m_timer
, 52);
510 FsmChangeState(fi
, ST_L3_LC_REL
);
511 skb_queue_purge(&st
->l3
.squeue
);
512 l3ml3p(st
, DL_RELEASE
| INDICATION
);
516 lc_release_cnf(struct FsmInst
*fi
, int event
, void *arg
)
518 struct PStack
*st
= fi
->userdata
;
520 FsmChangeState(fi
, ST_L3_LC_REL
);
521 skb_queue_purge(&st
->l3
.squeue
);
522 l3ml3p(st
, DL_RELEASE
| CONFIRM
);
527 static struct FsmNode L3FnList
[] __initdata
=
529 {ST_L3_LC_REL
, EV_ESTABLISH_REQ
, lc_activate
},
530 {ST_L3_LC_REL
, EV_ESTABLISH_IND
, lc_connect
},
531 {ST_L3_LC_REL
, EV_ESTABLISH_CNF
, lc_connect
},
532 {ST_L3_LC_ESTAB_WAIT
, EV_ESTABLISH_CNF
, lc_connected
},
533 {ST_L3_LC_ESTAB_WAIT
, EV_RELEASE_REQ
, lc_start_delay
},
534 {ST_L3_LC_ESTAB_WAIT
, EV_RELEASE_IND
, lc_release_ind
},
535 {ST_L3_LC_ESTAB
, EV_RELEASE_IND
, lc_release_ind
},
536 {ST_L3_LC_ESTAB
, EV_RELEASE_REQ
, lc_start_delay_check
},
537 {ST_L3_LC_REL_DELAY
, EV_RELEASE_IND
, lc_release_ind
},
538 {ST_L3_LC_REL_DELAY
, EV_ESTABLISH_REQ
, lc_connected
},
539 {ST_L3_LC_REL_DELAY
, EV_TIMEOUT
, lc_release_req
},
540 {ST_L3_LC_REL_WAIT
, EV_RELEASE_CNF
, lc_release_cnf
},
541 {ST_L3_LC_REL_WAIT
, EV_ESTABLISH_REQ
, lc_activate
},
546 l3_msg(struct PStack
*st
, int pr
, void *arg
)
549 case (DL_DATA
| REQUEST
):
550 if (st
->l3
.l3m
.state
== ST_L3_LC_ESTAB
) {
551 st
->l3
.l3l2(st
, pr
, arg
);
553 struct sk_buff
*skb
= arg
;
555 skb_queue_tail(&st
->l3
.squeue
, skb
);
556 FsmEvent(&st
->l3
.l3m
, EV_ESTABLISH_REQ
, NULL
);
559 case (DL_ESTABLISH
| REQUEST
):
560 FsmEvent(&st
->l3
.l3m
, EV_ESTABLISH_REQ
, NULL
);
562 case (DL_ESTABLISH
| CONFIRM
):
563 FsmEvent(&st
->l3
.l3m
, EV_ESTABLISH_CNF
, NULL
);
565 case (DL_ESTABLISH
| INDICATION
):
566 FsmEvent(&st
->l3
.l3m
, EV_ESTABLISH_IND
, NULL
);
568 case (DL_RELEASE
| INDICATION
):
569 FsmEvent(&st
->l3
.l3m
, EV_RELEASE_IND
, NULL
);
571 case (DL_RELEASE
| CONFIRM
):
572 FsmEvent(&st
->l3
.l3m
, EV_RELEASE_CNF
, NULL
);
574 case (DL_RELEASE
| REQUEST
):
575 FsmEvent(&st
->l3
.l3m
, EV_RELEASE_REQ
, NULL
);
583 l3fsm
.state_count
= L3_STATE_COUNT
;
584 l3fsm
.event_count
= L3_EVENT_COUNT
;
585 l3fsm
.strEvent
= strL3Event
;
586 l3fsm
.strState
= strL3State
;
587 return FsmNew(&l3fsm
, L3FnList
, ARRAY_SIZE(L3FnList
));