Linux 5.1.15
[linux/fpc-iii.git] / drivers / isdn / hisax / isdnl3.c
blobbb3f9ec62749ebc57a4a79a0f960b96326c6e213
1 /* $Id: isdnl3.c,v 2.22.2.3 2004/01/13 14:31:25 keil Exp $
3 * Author Karsten Keil
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
14 * Fritz Elfert
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include "hisax.h"
21 #include "isdnl3.h"
23 const char *l3_revision = "$Revision: 2.22.2.3 $";
25 static struct Fsm l3fsm;
27 enum {
28 ST_L3_LC_REL,
29 ST_L3_LC_ESTAB_WAIT,
30 ST_L3_LC_REL_DELAY,
31 ST_L3_LC_REL_WAIT,
32 ST_L3_LC_ESTAB,
35 #define L3_STATE_COUNT (ST_L3_LC_ESTAB + 1)
37 static char *strL3State[] =
39 "ST_L3_LC_REL",
40 "ST_L3_LC_ESTAB_WAIT",
41 "ST_L3_LC_REL_DELAY",
42 "ST_L3_LC_REL_WAIT",
43 "ST_L3_LC_ESTAB",
46 enum {
47 EV_ESTABLISH_REQ,
48 EV_ESTABLISH_IND,
49 EV_ESTABLISH_CNF,
50 EV_RELEASE_REQ,
51 EV_RELEASE_CNF,
52 EV_RELEASE_IND,
53 EV_TIMEOUT,
56 #define L3_EVENT_COUNT (EV_TIMEOUT + 1)
58 static char *strL3Event[] =
60 "EV_ESTABLISH_REQ",
61 "EV_ESTABLISH_IND",
62 "EV_ESTABLISH_CNF",
63 "EV_RELEASE_REQ",
64 "EV_RELEASE_CNF",
65 "EV_RELEASE_IND",
66 "EV_TIMEOUT",
69 static __printf(2, 3) void
70 l3m_debug(struct FsmInst *fi, char *fmt, ...)
72 va_list args;
73 struct PStack *st = fi->userdata;
75 va_start(args, fmt);
76 VHiSax_putstatus(st->l1.hardware, st->l3.debug_id, fmt, args);
77 va_end(args);
80 u_char *
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 */
87 p++;
88 l = (*p++) & 0xf;
89 p += l;
90 p++;
91 codeset = 0;
92 maincodeset = 0;
93 /* while there are bytes left... */
94 while (p < pend) {
95 if ((*p & 0xf0) == 0x90) {
96 codeset = *p & 0x07;
97 if (!(*p & 0x08))
98 maincodeset = codeset;
100 if (*p & 0x80)
101 p++;
102 else {
103 if (codeset == wanted_set) {
104 if (*p == ie)
105 { /* improved length check (Werner Cornelius) */
106 if ((pend - p) < 2)
107 return (NULL);
108 if (*(p + 1) > (pend - (p + 2)))
109 return (NULL);
110 return (p);
113 if (*p > ie)
114 return (NULL);
116 p++;
117 l = *p++;
118 p += l;
119 codeset = maincodeset;
122 return (NULL);
126 getcallref(u_char *p)
128 int l, cr = 0;
130 p++; /* prot discr */
131 if (*p & 0xfe) /* wrong callref BRI only 1 octet*/
132 return (-2);
133 l = 0xf & *p++; /* callref length */
134 if (!l) /* dummy CallRef */
135 return (-1);
136 cr = *p++;
137 return (cr);
140 static int OrigCallRef = 0;
143 newcallref(void)
145 if (OrigCallRef == 127)
146 OrigCallRef = 1;
147 else
148 OrigCallRef++;
149 return (OrigCallRef);
152 void
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__,
157 pc->callref & 0x7F,
158 pc->state, state);
159 pc->state = state;
162 static void
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);
169 void
170 L3InitTimer(struct l3_process *pc, struct L3Timer *t)
172 t->pc = pc;
173 timer_setup(&t->tl, L3ExpireTimer, 0);
176 void
177 L3DelTimer(struct L3Timer *t)
179 del_timer(&t->tl);
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");
188 return -1;
190 t->event = event;
191 t->tl.expires = jiffies + (millisec * HZ) / 1000;
192 add_timer(&t->tl);
193 return 0;
196 void
197 StopAllL3Timer(struct l3_process *pc)
199 L3DelTimer(&pc->timer);
202 struct sk_buff *
203 l3_alloc_skb(int len)
205 struct sk_buff *skb;
207 if (!(skb = alloc_skb(len + MAX_HEADER_LEN, GFP_ATOMIC))) {
208 printk(KERN_WARNING "HiSax: No skb for D-channel\n");
209 return (NULL);
211 skb_reserve(skb, MAX_HEADER_LEN);
212 return (skb);
215 static void
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");
221 if (skb) {
222 dev_kfree_skb(skb);
226 static int
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);
230 return (-1);
233 struct l3_process
234 *getl3proc(struct PStack *st, int cr)
236 struct l3_process *p = st->l3.proc;
238 while (p)
239 if (p->callref == cr)
240 return (p);
241 else
242 p = p->next;
243 return (NULL);
246 struct l3_process
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);
253 return (NULL);
255 if (!st->l3.proc)
256 st->l3.proc = p;
257 else {
258 np = st->l3.proc;
259 while (np->next)
260 np = np->next;
261 np->next = p;
263 p->next = NULL;
264 p->debug = st->l3.debug;
265 p->callref = cr;
266 p->state = 0;
267 p->chan = NULL;
268 p->st = st;
269 p->N303 = st->l3.N303;
270 L3InitTimer(p, &p->timer);
271 return (p);
274 void
275 release_l3_process(struct l3_process *p)
277 struct l3_process *np, *pp = NULL;
279 if (!p)
280 return;
281 np = p->st->l3.proc;
282 while (np) {
283 if (np == p) {
284 StopAllL3Timer(p);
285 if (pp)
286 pp->next = np->next;
287 else if (!(p->st->l3.proc = np->next) &&
288 !test_bit(FLG_PTP, &p->st->l2.flag)) {
289 if (p->debug)
290 l3_debug(p->st, "release_l3_process: last process");
291 if (skb_queue_empty(&p->st->l3.squeue)) {
292 if (p->debug)
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);
296 else
297 FsmEvent(&p->st->l3.l3m, EV_RELEASE_IND, NULL);
298 } else {
299 if (p->debug)
300 l3_debug(p->st, "release_l3_process: not release link");
303 kfree(p);
304 return;
306 pp = np;
307 np = np->next;
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);
313 static void
314 l3ml3p(struct PStack *st, int pr)
316 struct l3_process *p = st->l3.proc;
317 struct l3_process *np;
319 while (p) {
320 /* p might be kfreed under us, so we need to save where we want to go on */
321 np = p->next;
322 st->l3.l3ml3(st, pr, p);
323 p = np;
327 void
328 setstack_l3dc(struct PStack *st, struct Channel *chanp)
330 char tmp[64];
332 st->l3.proc = NULL;
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) {
347 setstack_dss1(st);
348 } else
349 #endif
350 #ifdef CONFIG_HISAX_NI1
351 if (st->protocol == ISDN_PTYPE_NI1) {
352 setstack_ni1(st);
353 } else
354 #endif
355 #ifdef CONFIG_HISAX_1TR6
356 if (st->protocol == ISDN_PTYPE_1TR6) {
357 setstack_1tr6(st);
358 } else
359 #endif
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");
365 } else {
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" :
373 "unknown");
374 printk(KERN_WARNING "HiSax: %s\n", tmp);
375 st->protocol = -1;
379 static void
380 isdnl3_trans(struct PStack *st, int pr, void *arg) {
381 st->l3.l3l2(st, pr, arg);
384 void
385 releasestack_isdnl3(struct PStack *st)
387 while (st->l3.proc)
388 release_l3_process(st->l3.proc);
389 if (st->l3.global) {
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);
398 void
399 setstack_l3bc(struct PStack *st, struct Channel *chanp)
402 st->l3.proc = NULL;
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
417 static void
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);
426 static void
427 lc_connect(struct FsmInst *fi, int event, void *arg)
429 struct PStack *st = fi->userdata;
430 struct sk_buff *skb = arg;
431 int dequeued = 0;
433 FsmChangeState(fi, ST_L3_LC_ESTAB);
434 while ((skb = skb_dequeue(&st->l3.squeue))) {
435 st->l3.l3l2(st, DL_DATA | REQUEST, skb);
436 dequeued++;
438 if ((!st->l3.proc) && dequeued) {
439 if (st->l3.debug)
440 l3_debug(st, "lc_connect: release link");
441 FsmEvent(&st->l3.l3m, EV_RELEASE_REQ, NULL);
442 } else
443 l3ml3p(st, DL_ESTABLISH | INDICATION);
446 static void
447 lc_connected(struct FsmInst *fi, int event, void *arg)
449 struct PStack *st = fi->userdata;
450 struct sk_buff *skb = arg;
451 int dequeued = 0;
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);
457 dequeued++;
459 if ((!st->l3.proc) && dequeued) {
460 if (st->l3.debug)
461 l3_debug(st, "lc_connected: release link");
462 FsmEvent(&st->l3.l3m, EV_RELEASE_REQ, NULL);
463 } else
464 l3ml3p(st, DL_ESTABLISH | CONFIRM);
467 static void
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);
476 static void
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);
488 static void
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)) {
494 if (st->l3.debug)
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);
498 } else {
499 FsmChangeState(fi, ST_L3_LC_REL_WAIT);
500 st->l3.l3l2(st, DL_RELEASE | REQUEST, NULL);
504 static void
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);
515 static void
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);
526 /* *INDENT-OFF* */
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},
543 /* *INDENT-ON* */
545 void
546 l3_msg(struct PStack *st, int pr, void *arg)
548 switch (pr) {
549 case (DL_DATA | REQUEST):
550 if (st->l3.l3m.state == ST_L3_LC_ESTAB) {
551 st->l3.l3l2(st, pr, arg);
552 } else {
553 struct sk_buff *skb = arg;
555 skb_queue_tail(&st->l3.squeue, skb);
556 FsmEvent(&st->l3.l3m, EV_ESTABLISH_REQ, NULL);
558 break;
559 case (DL_ESTABLISH | REQUEST):
560 FsmEvent(&st->l3.l3m, EV_ESTABLISH_REQ, NULL);
561 break;
562 case (DL_ESTABLISH | CONFIRM):
563 FsmEvent(&st->l3.l3m, EV_ESTABLISH_CNF, NULL);
564 break;
565 case (DL_ESTABLISH | INDICATION):
566 FsmEvent(&st->l3.l3m, EV_ESTABLISH_IND, NULL);
567 break;
568 case (DL_RELEASE | INDICATION):
569 FsmEvent(&st->l3.l3m, EV_RELEASE_IND, NULL);
570 break;
571 case (DL_RELEASE | CONFIRM):
572 FsmEvent(&st->l3.l3m, EV_RELEASE_CNF, NULL);
573 break;
574 case (DL_RELEASE | REQUEST):
575 FsmEvent(&st->l3.l3m, EV_RELEASE_REQ, NULL);
576 break;
580 int __init
581 Isdnl3New(void)
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));
590 void
591 Isdnl3Free(void)
593 FsmFree(&l3fsm);