1 /* $Id: isdnl1.c,v 1.1.4.1 2001/11/20 14:19:36 kai Exp $
2  *
3  * common low level stuff for Siemens Chipsetbased isdn cards
4  *
5  * Author       Karsten Keil
6  *              based on the teles driver from Jan den Ouden
7  * Copyright    by Karsten Keil      <keil@isdn4linux.de>
8  *
9  * This software may be used and distributed according to the terms
10  * of the GNU General Public License, incorporated herein by reference.
11  *
12  * For changes and modifications please read
13  * ../../../Documentation/isdn/HiSax.cert
14  *
15  * Thanks to    Jan den Ouden
16  *              Fritz Elfert
17  *              Beat Doebeli
18  *
19  */
20 
21 const char *l1_revision = "$Revision: 1.1.4.1 $";
22 
23 #define __NO_VERSION__
24 #include <linux/init.h>
25 #include "hisax.h"
26 #include "isdnl1.h"
27 
28 #define TIMER3_VALUE 7000
29 
30 static struct Fsm l1fsm_b;
31 static struct Fsm l1fsm_s;
32 
33 enum {
34 	ST_L1_F2,
35 	ST_L1_F3,
36 	ST_L1_F4,
37 	ST_L1_F5,
38 	ST_L1_F6,
39 	ST_L1_F7,
40 	ST_L1_F8,
41 };
42 
43 #define L1S_STATE_COUNT (ST_L1_F8+1)
44 
45 static char *strL1SState[] =
46 {
47 	"ST_L1_F2",
48 	"ST_L1_F3",
49 	"ST_L1_F4",
50 	"ST_L1_F5",
51 	"ST_L1_F6",
52 	"ST_L1_F7",
53 	"ST_L1_F8",
54 };
55 
56 #ifdef HISAX_UINTERFACE
57 static
58 struct Fsm l1fsm_u =
59 {NULL, 0, 0, NULL, NULL};
60 
61 enum {
62 	ST_L1_RESET,
63 	ST_L1_DEACT,
64 	ST_L1_SYNC2,
65 	ST_L1_TRANS,
66 };
67 
68 #define L1U_STATE_COUNT (ST_L1_TRANS+1)
69 
70 static char *strL1UState[] =
71 {
72 	"ST_L1_RESET",
73 	"ST_L1_DEACT",
74 	"ST_L1_SYNC2",
75 	"ST_L1_TRANS",
76 };
77 #endif
78 
79 enum {
80 	ST_L1_NULL,
81 	ST_L1_WAIT_ACT,
82 	ST_L1_WAIT_DEACT,
83 	ST_L1_ACTIV,
84 };
85 
86 #define L1B_STATE_COUNT (ST_L1_ACTIV+1)
87 
88 static char *strL1BState[] =
89 {
90 	"ST_L1_NULL",
91 	"ST_L1_WAIT_ACT",
92 	"ST_L1_WAIT_DEACT",
93 	"ST_L1_ACTIV",
94 };
95 
96 enum {
97 	EV_PH_ACTIVATE,
98 	EV_PH_DEACTIVATE,
99 	EV_RESET_IND,
100 	EV_DEACT_CNF,
101 	EV_DEACT_IND,
102 	EV_POWER_UP,
103 	EV_RSYNC_IND,
104 	EV_INFO2_IND,
105 	EV_INFO4_IND,
106 	EV_TIMER_DEACT,
107 	EV_TIMER_ACT,
108 	EV_TIMER3,
109 };
110 
111 #define L1_EVENT_COUNT (EV_TIMER3 + 1)
112 
113 static char *strL1Event[] =
114 {
115 	"EV_PH_ACTIVATE",
116 	"EV_PH_DEACTIVATE",
117 	"EV_RESET_IND",
118 	"EV_DEACT_CNF",
119 	"EV_DEACT_IND",
120 	"EV_POWER_UP",
121 	"EV_RSYNC_IND",
122 	"EV_INFO2_IND",
123 	"EV_INFO4_IND",
124 	"EV_TIMER_DEACT",
125 	"EV_TIMER_ACT",
126 	"EV_TIMER3",
127 };
128 
129 void
debugl1(struct IsdnCardState * cs,char * fmt,...)130 debugl1(struct IsdnCardState *cs, char *fmt, ...)
131 {
132 	va_list args;
133 	char tmp[8];
134 
135 	va_start(args, fmt);
136 	sprintf(tmp, "Card%d ", cs->cardnr + 1);
137 	VHiSax_putstatus(cs, tmp, fmt, args);
138 	va_end(args);
139 }
140 
141 static void
l1m_debug(struct FsmInst * fi,char * fmt,...)142 l1m_debug(struct FsmInst *fi, char *fmt, ...)
143 {
144 	va_list args;
145 	struct PStack *st = fi->userdata;
146 	struct IsdnCardState *cs = st->l1.hardware;
147 	char tmp[8];
148 
149 	va_start(args, fmt);
150 	sprintf(tmp, "Card%d ", cs->cardnr + 1);
151 	VHiSax_putstatus(cs, tmp, fmt, args);
152 	va_end(args);
153 }
154 
155 void
L1activated(struct IsdnCardState * cs)156 L1activated(struct IsdnCardState *cs)
157 {
158 	struct PStack *st;
159 
160 	st = cs->stlist;
161 	while (st) {
162 		if (test_and_clear_bit(FLG_L1_ACTIVATING, &st->l1.Flags))
163 			st->l1.l1l2(st, PH_ACTIVATE | CONFIRM, NULL);
164 		else
165 			st->l1.l1l2(st, PH_ACTIVATE | INDICATION, NULL);
166 		st = st->next;
167 	}
168 }
169 
170 void
L1deactivated(struct IsdnCardState * cs)171 L1deactivated(struct IsdnCardState *cs)
172 {
173 	struct PStack *st;
174 
175 	st = cs->stlist;
176 	while (st) {
177 		if (test_bit(FLG_L1_DBUSY, &cs->HW_Flags))
178 			st->l1.l1l2(st, PH_PAUSE | CONFIRM, NULL);
179 		st->l1.l1l2(st, PH_DEACTIVATE | INDICATION, NULL);
180 		st = st->next;
181 	}
182 	test_and_clear_bit(FLG_L1_DBUSY, &cs->HW_Flags);
183 }
184 
185 void
DChannel_proc_xmt(struct IsdnCardState * cs)186 DChannel_proc_xmt(struct IsdnCardState *cs)
187 {
188 	struct PStack *stptr;
189 
190 	if (cs->tx_skb)
191 		return;
192 
193 	stptr = cs->stlist;
194 	while (stptr != NULL)
195 		if (test_and_clear_bit(FLG_L1_PULL_REQ, &stptr->l1.Flags)) {
196 			stptr->l1.l1l2(stptr, PH_PULL | CONFIRM, NULL);
197 			break;
198 		} else
199 			stptr = stptr->next;
200 }
201 
202 void
DChannel_proc_rcv(struct IsdnCardState * cs)203 DChannel_proc_rcv(struct IsdnCardState *cs)
204 {
205 	struct sk_buff *skb, *nskb;
206 	struct PStack *stptr = cs->stlist;
207 	int found, tei, sapi;
208 
209 	if (stptr)
210 		if (test_bit(FLG_L1_ACTTIMER, &stptr->l1.Flags))
211 			FsmEvent(&stptr->l1.l1m, EV_TIMER_ACT, NULL);
212 	while ((skb = skb_dequeue(&cs->rq))) {
213 #ifdef L2FRAME_DEBUG		/* psa */
214 		if (cs->debug & L1_DEB_LAPD)
215 			Logl2Frame(cs, skb, "PH_DATA", 1);
216 #endif
217 		stptr = cs->stlist;
218 		if (skb->len<3) {
219 			debugl1(cs, "D-channel frame too short(%d)",skb->len);
220 			dev_kfree_skb(skb);
221 			return;
222 		}
223 		if ((skb->data[0] & 1) || !(skb->data[1] &1)) {
224 			debugl1(cs, "D-channel frame wrong EA0/EA1");
225 			dev_kfree_skb(skb);
226 			return;
227 		}
228 		sapi = skb->data[0] >> 2;
229 		tei = skb->data[1] >> 1;
230 		if (cs->debug & DEB_DLOG_HEX)
231 			LogFrame(cs, skb->data, skb->len);
232 		if (cs->debug & DEB_DLOG_VERBOSE)
233 			dlogframe(cs, skb, 1);
234 		if (tei == GROUP_TEI) {
235 			if (sapi == CTRL_SAPI) { /* sapi 0 */
236 				while (stptr != NULL) {
237 					if ((nskb = skb_clone(skb, GFP_ATOMIC)))
238 						stptr->l1.l1l2(stptr, PH_DATA | INDICATION, nskb);
239 					else
240 						printk(KERN_WARNING "HiSax: isdn broadcast buffer shortage\n");
241 					stptr = stptr->next;
242 				}
243 			} else if (sapi == TEI_SAPI) {
244 				while (stptr != NULL) {
245 					if ((nskb = skb_clone(skb, GFP_ATOMIC)))
246 						stptr->l1.l1tei(stptr, PH_DATA | INDICATION, nskb);
247 					else
248 						printk(KERN_WARNING "HiSax: tei broadcast buffer shortage\n");
249 					stptr = stptr->next;
250 				}
251 			}
252 			dev_kfree_skb(skb);
253 		} else if (sapi == CTRL_SAPI) { /* sapi 0 */
254 			found = 0;
255 			while (stptr != NULL)
256 				if (tei == stptr->l2.tei) {
257 					stptr->l1.l1l2(stptr, PH_DATA | INDICATION, skb);
258 					found = !0;
259 					break;
260 				} else
261 					stptr = stptr->next;
262 			if (!found)
263 				dev_kfree_skb(skb);
264 		} else
265 			dev_kfree_skb(skb);
266 	}
267 }
268 
269 static void
BChannel_proc_xmt(struct BCState * bcs)270 BChannel_proc_xmt(struct BCState *bcs)
271 {
272 	struct PStack *st = bcs->st;
273 
274 	if (test_bit(BC_FLG_BUSY, &bcs->Flag)) {
275 		debugl1(bcs->cs, "BC_BUSY Error");
276 		return;
277 	}
278 
279 	if (test_and_clear_bit(FLG_L1_PULL_REQ, &st->l1.Flags))
280 		st->l1.l1l2(st, PH_PULL | CONFIRM, NULL);
281 	if (!test_bit(BC_FLG_ACTIV, &bcs->Flag)) {
282 		if (!test_bit(BC_FLG_BUSY, &bcs->Flag) && (!skb_queue_len(&bcs->squeue))) {
283 			st->l2.l2l1(st, PH_DEACTIVATE | CONFIRM, NULL);
284 		}
285 	}
286 }
287 
288 static void
BChannel_proc_rcv(struct BCState * bcs)289 BChannel_proc_rcv(struct BCState *bcs)
290 {
291 	struct sk_buff *skb;
292 
293 	if (bcs->st->l1.l1m.state == ST_L1_WAIT_ACT) {
294 		FsmDelTimer(&bcs->st->l1.timer, 4);
295 		FsmEvent(&bcs->st->l1.l1m, EV_TIMER_ACT, NULL);
296 	}
297 	while ((skb = skb_dequeue(&bcs->rqueue))) {
298 		bcs->st->l1.l1l2(bcs->st, PH_DATA | INDICATION, skb);
299 	}
300 }
301 
302 void
BChannel_bh(struct BCState * bcs)303 BChannel_bh(struct BCState *bcs)
304 {
305 	if (!bcs)
306 		return;
307 	if (test_and_clear_bit(B_RCVBUFREADY, &bcs->event))
308 		BChannel_proc_rcv(bcs);
309 	if (test_and_clear_bit(B_XMTBUFREADY, &bcs->event))
310 		BChannel_proc_xmt(bcs);
311 }
312 
313 void
HiSax_addlist(struct IsdnCardState * cs,struct PStack * st)314 HiSax_addlist(struct IsdnCardState *cs,
315 	      struct PStack *st)
316 {
317 	st->next = cs->stlist;
318 	cs->stlist = st;
319 }
320 
321 void
HiSax_rmlist(struct IsdnCardState * cs,struct PStack * st)322 HiSax_rmlist(struct IsdnCardState *cs,
323 	     struct PStack *st)
324 {
325 	struct PStack *p;
326 
327 	FsmDelTimer(&st->l1.timer, 0);
328 	if (cs->stlist == st)
329 		cs->stlist = st->next;
330 	else {
331 		p = cs->stlist;
332 		while (p)
333 			if (p->next == st) {
334 				p->next = st->next;
335 				return;
336 			} else
337 				p = p->next;
338 	}
339 }
340 
341 void
init_bcstate(struct IsdnCardState * cs,int bc)342 init_bcstate(struct IsdnCardState *cs,
343 	     int bc)
344 {
345 	struct BCState *bcs = cs->bcs + bc;
346 
347 	bcs->cs = cs;
348 	bcs->channel = bc;
349 	bcs->tqueue.sync = 0;
350 	bcs->tqueue.routine = (void *) (void *) BChannel_bh;
351 	bcs->tqueue.data = bcs;
352 	bcs->BC_SetStack = NULL;
353 	bcs->BC_Close = NULL;
354 	bcs->Flag = 0;
355 }
356 
357 #ifdef L2FRAME_DEBUG		/* psa */
358 
359 char *
l2cmd(u_char cmd)360 l2cmd(u_char cmd)
361 {
362 	switch (cmd & ~0x10) {
363 		case 1:
364 			return "RR";
365 		case 5:
366 			return "RNR";
367 		case 9:
368 			return "REJ";
369 		case 0x6f:
370 			return "SABME";
371 		case 0x0f:
372 			return "DM";
373 		case 3:
374 			return "UI";
375 		case 0x43:
376 			return "DISC";
377 		case 0x63:
378 			return "UA";
379 		case 0x87:
380 			return "FRMR";
381 		case 0xaf:
382 			return "XID";
383 		default:
384 			if (!(cmd & 1))
385 				return "I";
386 			else
387 				return "invalid command";
388 	}
389 }
390 
391 static char tmpdeb[32];
392 
393 char *
l2frames(u_char * ptr)394 l2frames(u_char * ptr)
395 {
396 	switch (ptr[2] & ~0x10) {
397 		case 1:
398 		case 5:
399 		case 9:
400 			sprintf(tmpdeb, "%s[%d](nr %d)", l2cmd(ptr[2]), ptr[3] & 1, ptr[3] >> 1);
401 			break;
402 		case 0x6f:
403 		case 0x0f:
404 		case 3:
405 		case 0x43:
406 		case 0x63:
407 		case 0x87:
408 		case 0xaf:
409 			sprintf(tmpdeb, "%s[%d]", l2cmd(ptr[2]), (ptr[2] & 0x10) >> 4);
410 			break;
411 		default:
412 			if (!(ptr[2] & 1)) {
413 				sprintf(tmpdeb, "I[%d](ns %d, nr %d)", ptr[3] & 1, ptr[2] >> 1, ptr[3] >> 1);
414 				break;
415 			} else
416 				return "invalid command";
417 	}
418 
419 
420 	return tmpdeb;
421 }
422 
423 void
Logl2Frame(struct IsdnCardState * cs,struct sk_buff * skb,char * buf,int dir)424 Logl2Frame(struct IsdnCardState *cs, struct sk_buff *skb, char *buf, int dir)
425 {
426 	u_char *ptr;
427 
428 	ptr = skb->data;
429 
430 	if (ptr[0] & 1 || !(ptr[1] & 1))
431 		debugl1(cs, "Address not LAPD");
432 	else
433 		debugl1(cs, "%s %s: %s%c (sapi %d, tei %d)",
434 			(dir ? "<-" : "->"), buf, l2frames(ptr),
435 			((ptr[0] & 2) >> 1) == dir ? 'C' : 'R', ptr[0] >> 2, ptr[1] >> 1);
436 }
437 #endif
438 
439 static void
l1_reset(struct FsmInst * fi,int event,void * arg)440 l1_reset(struct FsmInst *fi, int event, void *arg)
441 {
442 	FsmChangeState(fi, ST_L1_F3);
443 }
444 
445 static void
l1_deact_cnf(struct FsmInst * fi,int event,void * arg)446 l1_deact_cnf(struct FsmInst *fi, int event, void *arg)
447 {
448 	struct PStack *st = fi->userdata;
449 
450 	FsmChangeState(fi, ST_L1_F3);
451 	if (test_bit(FLG_L1_ACTIVATING, &st->l1.Flags))
452 		st->l1.l1hw(st, HW_ENABLE | REQUEST, NULL);
453 }
454 
455 static void
l1_deact_req_s(struct FsmInst * fi,int event,void * arg)456 l1_deact_req_s(struct FsmInst *fi, int event, void *arg)
457 {
458 	struct PStack *st = fi->userdata;
459 
460 	FsmChangeState(fi, ST_L1_F3);
461 	FsmRestartTimer(&st->l1.timer, 550, EV_TIMER_DEACT, NULL, 2);
462 	test_and_set_bit(FLG_L1_DEACTTIMER, &st->l1.Flags);
463 }
464 
465 static void
l1_power_up_s(struct FsmInst * fi,int event,void * arg)466 l1_power_up_s(struct FsmInst *fi, int event, void *arg)
467 {
468 	struct PStack *st = fi->userdata;
469 
470 	if (test_bit(FLG_L1_ACTIVATING, &st->l1.Flags)) {
471 		FsmChangeState(fi, ST_L1_F4);
472 		st->l1.l1hw(st, HW_INFO3 | REQUEST, NULL);
473 		FsmRestartTimer(&st->l1.timer, TIMER3_VALUE, EV_TIMER3, NULL, 2);
474 		test_and_set_bit(FLG_L1_T3RUN, &st->l1.Flags);
475 	} else
476 		FsmChangeState(fi, ST_L1_F3);
477 }
478 
479 static void
l1_go_F5(struct FsmInst * fi,int event,void * arg)480 l1_go_F5(struct FsmInst *fi, int event, void *arg)
481 {
482 	FsmChangeState(fi, ST_L1_F5);
483 }
484 
485 static void
l1_go_F8(struct FsmInst * fi,int event,void * arg)486 l1_go_F8(struct FsmInst *fi, int event, void *arg)
487 {
488 	FsmChangeState(fi, ST_L1_F8);
489 }
490 
491 static void
l1_info2_ind(struct FsmInst * fi,int event,void * arg)492 l1_info2_ind(struct FsmInst *fi, int event, void *arg)
493 {
494 	struct PStack *st = fi->userdata;
495 
496 #ifdef HISAX_UINTERFACE
497 	if (test_bit(FLG_L1_UINT, &st->l1.Flags))
498 		FsmChangeState(fi, ST_L1_SYNC2);
499 	else
500 #endif
501 		FsmChangeState(fi, ST_L1_F6);
502 	st->l1.l1hw(st, HW_INFO3 | REQUEST, NULL);
503 }
504 
505 static void
l1_info4_ind(struct FsmInst * fi,int event,void * arg)506 l1_info4_ind(struct FsmInst *fi, int event, void *arg)
507 {
508 	struct PStack *st = fi->userdata;
509 
510 #ifdef HISAX_UINTERFACE
511 	if (test_bit(FLG_L1_UINT, &st->l1.Flags))
512 		FsmChangeState(fi, ST_L1_TRANS);
513 	else
514 #endif
515 		FsmChangeState(fi, ST_L1_F7);
516 	st->l1.l1hw(st, HW_INFO3 | REQUEST, NULL);
517 	if (test_and_clear_bit(FLG_L1_DEACTTIMER, &st->l1.Flags))
518 		FsmDelTimer(&st->l1.timer, 4);
519 	if (!test_bit(FLG_L1_ACTIVATED, &st->l1.Flags)) {
520 		if (test_and_clear_bit(FLG_L1_T3RUN, &st->l1.Flags))
521 			FsmDelTimer(&st->l1.timer, 3);
522 		FsmRestartTimer(&st->l1.timer, 110, EV_TIMER_ACT, NULL, 2);
523 		test_and_set_bit(FLG_L1_ACTTIMER, &st->l1.Flags);
524 	}
525 }
526 
527 static void
l1_timer3(struct FsmInst * fi,int event,void * arg)528 l1_timer3(struct FsmInst *fi, int event, void *arg)
529 {
530 	struct PStack *st = fi->userdata;
531 
532 	test_and_clear_bit(FLG_L1_T3RUN, &st->l1.Flags);
533 	if (test_and_clear_bit(FLG_L1_ACTIVATING, &st->l1.Flags))
534 		L1deactivated(st->l1.hardware);
535 
536 #ifdef HISAX_UINTERFACE
537 	if (!test_bit(FLG_L1_UINT, &st->l1.Flags))
538 #endif
539 	if (st->l1.l1m.state != ST_L1_F6) {
540 		FsmChangeState(fi, ST_L1_F3);
541 		st->l1.l1hw(st, HW_ENABLE | REQUEST, NULL);
542 	}
543 }
544 
545 static void
l1_timer_act(struct FsmInst * fi,int event,void * arg)546 l1_timer_act(struct FsmInst *fi, int event, void *arg)
547 {
548 	struct PStack *st = fi->userdata;
549 
550 	test_and_clear_bit(FLG_L1_ACTTIMER, &st->l1.Flags);
551 	test_and_set_bit(FLG_L1_ACTIVATED, &st->l1.Flags);
552 	L1activated(st->l1.hardware);
553 }
554 
555 static void
l1_timer_deact(struct FsmInst * fi,int event,void * arg)556 l1_timer_deact(struct FsmInst *fi, int event, void *arg)
557 {
558 	struct PStack *st = fi->userdata;
559 
560 	test_and_clear_bit(FLG_L1_DEACTTIMER, &st->l1.Flags);
561 	test_and_clear_bit(FLG_L1_ACTIVATED, &st->l1.Flags);
562 	L1deactivated(st->l1.hardware);
563 	st->l1.l1hw(st, HW_DEACTIVATE | RESPONSE, NULL);
564 }
565 
566 static void
l1_activate_s(struct FsmInst * fi,int event,void * arg)567 l1_activate_s(struct FsmInst *fi, int event, void *arg)
568 {
569 	struct PStack *st = fi->userdata;
570 
571 	st->l1.l1hw(st, HW_RESET | REQUEST, NULL);
572 }
573 
574 static void
l1_activate_no(struct FsmInst * fi,int event,void * arg)575 l1_activate_no(struct FsmInst *fi, int event, void *arg)
576 {
577 	struct PStack *st = fi->userdata;
578 
579 	if ((!test_bit(FLG_L1_DEACTTIMER, &st->l1.Flags)) && (!test_bit(FLG_L1_T3RUN, &st->l1.Flags))) {
580 		test_and_clear_bit(FLG_L1_ACTIVATING, &st->l1.Flags);
581 		L1deactivated(st->l1.hardware);
582 	}
583 }
584 
585 static struct FsmNode L1SFnList[] __initdata =
586 {
587 	{ST_L1_F3, EV_PH_ACTIVATE, l1_activate_s},
588 	{ST_L1_F6, EV_PH_ACTIVATE, l1_activate_no},
589 	{ST_L1_F8, EV_PH_ACTIVATE, l1_activate_no},
590 	{ST_L1_F3, EV_RESET_IND, l1_reset},
591 	{ST_L1_F4, EV_RESET_IND, l1_reset},
592 	{ST_L1_F5, EV_RESET_IND, l1_reset},
593 	{ST_L1_F6, EV_RESET_IND, l1_reset},
594 	{ST_L1_F7, EV_RESET_IND, l1_reset},
595 	{ST_L1_F8, EV_RESET_IND, l1_reset},
596 	{ST_L1_F3, EV_DEACT_CNF, l1_deact_cnf},
597 	{ST_L1_F4, EV_DEACT_CNF, l1_deact_cnf},
598 	{ST_L1_F5, EV_DEACT_CNF, l1_deact_cnf},
599 	{ST_L1_F6, EV_DEACT_CNF, l1_deact_cnf},
600 	{ST_L1_F7, EV_DEACT_CNF, l1_deact_cnf},
601 	{ST_L1_F8, EV_DEACT_CNF, l1_deact_cnf},
602 	{ST_L1_F6, EV_DEACT_IND, l1_deact_req_s},
603 	{ST_L1_F7, EV_DEACT_IND, l1_deact_req_s},
604 	{ST_L1_F8, EV_DEACT_IND, l1_deact_req_s},
605 	{ST_L1_F3, EV_POWER_UP, l1_power_up_s},
606 	{ST_L1_F4, EV_RSYNC_IND, l1_go_F5},
607 	{ST_L1_F6, EV_RSYNC_IND, l1_go_F8},
608 	{ST_L1_F7, EV_RSYNC_IND, l1_go_F8},
609 	{ST_L1_F3, EV_INFO2_IND, l1_info2_ind},
610 	{ST_L1_F4, EV_INFO2_IND, l1_info2_ind},
611 	{ST_L1_F5, EV_INFO2_IND, l1_info2_ind},
612 	{ST_L1_F7, EV_INFO2_IND, l1_info2_ind},
613 	{ST_L1_F8, EV_INFO2_IND, l1_info2_ind},
614 	{ST_L1_F3, EV_INFO4_IND, l1_info4_ind},
615 	{ST_L1_F4, EV_INFO4_IND, l1_info4_ind},
616 	{ST_L1_F5, EV_INFO4_IND, l1_info4_ind},
617 	{ST_L1_F6, EV_INFO4_IND, l1_info4_ind},
618 	{ST_L1_F8, EV_INFO4_IND, l1_info4_ind},
619 	{ST_L1_F3, EV_TIMER3, l1_timer3},
620 	{ST_L1_F4, EV_TIMER3, l1_timer3},
621 	{ST_L1_F5, EV_TIMER3, l1_timer3},
622 	{ST_L1_F6, EV_TIMER3, l1_timer3},
623 	{ST_L1_F8, EV_TIMER3, l1_timer3},
624 	{ST_L1_F7, EV_TIMER_ACT, l1_timer_act},
625 	{ST_L1_F3, EV_TIMER_DEACT, l1_timer_deact},
626 	{ST_L1_F4, EV_TIMER_DEACT, l1_timer_deact},
627 	{ST_L1_F5, EV_TIMER_DEACT, l1_timer_deact},
628 	{ST_L1_F6, EV_TIMER_DEACT, l1_timer_deact},
629 	{ST_L1_F7, EV_TIMER_DEACT, l1_timer_deact},
630 	{ST_L1_F8, EV_TIMER_DEACT, l1_timer_deact},
631 };
632 
633 #define L1S_FN_COUNT (sizeof(L1SFnList)/sizeof(struct FsmNode))
634 
635 #ifdef HISAX_UINTERFACE
636 static void
l1_deact_req_u(struct FsmInst * fi,int event,void * arg)637 l1_deact_req_u(struct FsmInst *fi, int event, void *arg)
638 {
639 	struct PStack *st = fi->userdata;
640 
641 	FsmChangeState(fi, ST_L1_RESET);
642 	FsmRestartTimer(&st->l1.timer, 550, EV_TIMER_DEACT, NULL, 2);
643 	test_and_set_bit(FLG_L1_DEACTTIMER, &st->l1.Flags);
644 	st->l1.l1hw(st, HW_ENABLE | REQUEST, NULL);
645 }
646 
647 static void
l1_power_up_u(struct FsmInst * fi,int event,void * arg)648 l1_power_up_u(struct FsmInst *fi, int event, void *arg)
649 {
650 	struct PStack *st = fi->userdata;
651 
652 	FsmRestartTimer(&st->l1.timer, TIMER3_VALUE, EV_TIMER3, NULL, 2);
653 	test_and_set_bit(FLG_L1_T3RUN, &st->l1.Flags);
654 }
655 
656 static void
l1_info0_ind(struct FsmInst * fi,int event,void * arg)657 l1_info0_ind(struct FsmInst *fi, int event, void *arg)
658 {
659 	FsmChangeState(fi, ST_L1_DEACT);
660 }
661 
662 static void
l1_activate_u(struct FsmInst * fi,int event,void * arg)663 l1_activate_u(struct FsmInst *fi, int event, void *arg)
664 {
665 	struct PStack *st = fi->userdata;
666 
667 	st->l1.l1hw(st, HW_INFO1 | REQUEST, NULL);
668 }
669 
670 static struct FsmNode L1UFnList[] __initdata =
671 {
672 	{ST_L1_RESET, EV_DEACT_IND, l1_deact_req_u},
673 	{ST_L1_DEACT, EV_DEACT_IND, l1_deact_req_u},
674 	{ST_L1_SYNC2, EV_DEACT_IND, l1_deact_req_u},
675 	{ST_L1_TRANS, EV_DEACT_IND, l1_deact_req_u},
676 	{ST_L1_DEACT, EV_PH_ACTIVATE, l1_activate_u},
677 	{ST_L1_DEACT, EV_POWER_UP, l1_power_up_u},
678 	{ST_L1_DEACT, EV_INFO2_IND, l1_info2_ind},
679 	{ST_L1_TRANS, EV_INFO2_IND, l1_info2_ind},
680 	{ST_L1_RESET, EV_DEACT_CNF, l1_info0_ind},
681 	{ST_L1_DEACT, EV_INFO4_IND, l1_info4_ind},
682 	{ST_L1_SYNC2, EV_INFO4_IND, l1_info4_ind},
683 	{ST_L1_RESET, EV_INFO4_IND, l1_info4_ind},
684 	{ST_L1_DEACT, EV_TIMER3, l1_timer3},
685 	{ST_L1_SYNC2, EV_TIMER3, l1_timer3},
686 	{ST_L1_TRANS, EV_TIMER_ACT, l1_timer_act},
687 	{ST_L1_DEACT, EV_TIMER_DEACT, l1_timer_deact},
688 	{ST_L1_SYNC2, EV_TIMER_DEACT, l1_timer_deact},
689 	{ST_L1_RESET, EV_TIMER_DEACT, l1_timer_deact},
690 };
691 
692 #define L1U_FN_COUNT (sizeof(L1UFnList)/sizeof(struct FsmNode))
693 
694 #endif
695 
696 static void
l1b_activate(struct FsmInst * fi,int event,void * arg)697 l1b_activate(struct FsmInst *fi, int event, void *arg)
698 {
699 	struct PStack *st = fi->userdata;
700 
701 	FsmChangeState(fi, ST_L1_WAIT_ACT);
702 	FsmRestartTimer(&st->l1.timer, st->l1.delay, EV_TIMER_ACT, NULL, 2);
703 }
704 
705 static void
l1b_deactivate(struct FsmInst * fi,int event,void * arg)706 l1b_deactivate(struct FsmInst *fi, int event, void *arg)
707 {
708 	struct PStack *st = fi->userdata;
709 
710 	FsmChangeState(fi, ST_L1_WAIT_DEACT);
711 	FsmRestartTimer(&st->l1.timer, 10, EV_TIMER_DEACT, NULL, 2);
712 }
713 
714 static void
l1b_timer_act(struct FsmInst * fi,int event,void * arg)715 l1b_timer_act(struct FsmInst *fi, int event, void *arg)
716 {
717 	struct PStack *st = fi->userdata;
718 
719 	FsmChangeState(fi, ST_L1_ACTIV);
720 	st->l1.l1l2(st, PH_ACTIVATE | CONFIRM, NULL);
721 }
722 
723 static void
l1b_timer_deact(struct FsmInst * fi,int event,void * arg)724 l1b_timer_deact(struct FsmInst *fi, int event, void *arg)
725 {
726 	struct PStack *st = fi->userdata;
727 
728 	FsmChangeState(fi, ST_L1_NULL);
729 	st->l2.l2l1(st, PH_DEACTIVATE | CONFIRM, NULL);
730 }
731 
732 static struct FsmNode L1BFnList[] __initdata =
733 {
734 	{ST_L1_NULL, EV_PH_ACTIVATE, l1b_activate},
735 	{ST_L1_WAIT_ACT, EV_TIMER_ACT, l1b_timer_act},
736 	{ST_L1_ACTIV, EV_PH_DEACTIVATE, l1b_deactivate},
737 	{ST_L1_WAIT_DEACT, EV_TIMER_DEACT, l1b_timer_deact},
738 };
739 
740 #define L1B_FN_COUNT (sizeof(L1BFnList)/sizeof(struct FsmNode))
741 
742 int __init
Isdnl1New(void)743 Isdnl1New(void)
744 {
745 	int retval;
746 
747 	l1fsm_s.state_count = L1S_STATE_COUNT;
748 	l1fsm_s.event_count = L1_EVENT_COUNT;
749 	l1fsm_s.strEvent = strL1Event;
750 	l1fsm_s.strState = strL1SState;
751 	retval = FsmNew(&l1fsm_s, L1SFnList, L1S_FN_COUNT);
752 	if (retval)
753 		return retval;
754 
755 	l1fsm_b.state_count = L1B_STATE_COUNT;
756 	l1fsm_b.event_count = L1_EVENT_COUNT;
757 	l1fsm_b.strEvent = strL1Event;
758 	l1fsm_b.strState = strL1BState;
759 	retval = FsmNew(&l1fsm_b, L1BFnList, L1B_FN_COUNT);
760 	if (retval) {
761 		FsmFree(&l1fsm_s);
762 		return retval;
763 	}
764 #ifdef HISAX_UINTERFACE
765 	l1fsm_u.state_count = L1U_STATE_COUNT;
766 	l1fsm_u.event_count = L1_EVENT_COUNT;
767 	l1fsm_u.strEvent = strL1Event;
768 	l1fsm_u.strState = strL1UState;
769 	retval = FsmNew(&l1fsm_u, L1UFnList, L1U_FN_COUNT);
770 	if (retval) {
771 		FsmFree(&l1fsm_s);
772 		FsmFree(&l1fsm_b);
773 		return retval;
774 	}
775 #endif
776 	return 0;
777 }
778 
Isdnl1Free(void)779 void Isdnl1Free(void)
780 {
781 #ifdef HISAX_UINTERFACE
782 	FsmFree(&l1fsm_u);
783 #endif
784 	FsmFree(&l1fsm_s);
785 	FsmFree(&l1fsm_b);
786 }
787 
788 static void
dch_l2l1(struct PStack * st,int pr,void * arg)789 dch_l2l1(struct PStack *st, int pr, void *arg)
790 {
791 	struct IsdnCardState *cs = (struct IsdnCardState *) st->l1.hardware;
792 
793 	switch (pr) {
794 		case (PH_DATA | REQUEST):
795 		case (PH_PULL | REQUEST):
796 		case (PH_PULL |INDICATION):
797 			st->l1.l1hw(st, pr, arg);
798 			break;
799 		case (PH_ACTIVATE | REQUEST):
800 			if (cs->debug)
801 				debugl1(cs, "PH_ACTIVATE_REQ %s",
802 					st->l1.l1m.fsm->strState[st->l1.l1m.state]);
803 			if (test_bit(FLG_L1_ACTIVATED, &st->l1.Flags))
804 				st->l1.l1l2(st, PH_ACTIVATE | CONFIRM, NULL);
805 			else {
806 				test_and_set_bit(FLG_L1_ACTIVATING, &st->l1.Flags);
807 				FsmEvent(&st->l1.l1m, EV_PH_ACTIVATE, arg);
808 			}
809 			break;
810 		case (PH_TESTLOOP | REQUEST):
811 			if (1 & (long) arg)
812 				debugl1(cs, "PH_TEST_LOOP B1");
813 			if (2 & (long) arg)
814 				debugl1(cs, "PH_TEST_LOOP B2");
815 			if (!(3 & (long) arg))
816 				debugl1(cs, "PH_TEST_LOOP DISABLED");
817 			st->l1.l1hw(st, HW_TESTLOOP | REQUEST, arg);
818 			break;
819 		default:
820 			if (cs->debug)
821 				debugl1(cs, "dch_l2l1 msg %04X unhandled", pr);
822 			break;
823 	}
824 }
825 
826 void
l1_msg(struct IsdnCardState * cs,int pr,void * arg)827 l1_msg(struct IsdnCardState *cs, int pr, void *arg) {
828 	struct PStack *st;
829 
830 	st = cs->stlist;
831 
832 	while (st) {
833 		switch(pr) {
834 			case (HW_RESET | INDICATION):
835 				FsmEvent(&st->l1.l1m, EV_RESET_IND, arg);
836 				break;
837 			case (HW_DEACTIVATE | CONFIRM):
838 				FsmEvent(&st->l1.l1m, EV_DEACT_CNF, arg);
839 				break;
840 			case (HW_DEACTIVATE | INDICATION):
841 				FsmEvent(&st->l1.l1m, EV_DEACT_IND, arg);
842 				break;
843 			case (HW_POWERUP | CONFIRM):
844 				FsmEvent(&st->l1.l1m, EV_POWER_UP, arg);
845 				break;
846 			case (HW_RSYNC | INDICATION):
847 				FsmEvent(&st->l1.l1m, EV_RSYNC_IND, arg);
848 				break;
849 			case (HW_INFO2 | INDICATION):
850 				FsmEvent(&st->l1.l1m, EV_INFO2_IND, arg);
851 				break;
852 			case (HW_INFO4_P8 | INDICATION):
853 			case (HW_INFO4_P10 | INDICATION):
854 				FsmEvent(&st->l1.l1m, EV_INFO4_IND, arg);
855 				break;
856 			default:
857 				if (cs->debug)
858 					debugl1(cs, "l1msg %04X unhandled", pr);
859 				break;
860 		}
861 		st = st->next;
862 	}
863 }
864 
865 void
l1_msg_b(struct PStack * st,int pr,void * arg)866 l1_msg_b(struct PStack *st, int pr, void *arg) {
867 	switch(pr) {
868 		case (PH_ACTIVATE | REQUEST):
869 			FsmEvent(&st->l1.l1m, EV_PH_ACTIVATE, NULL);
870 			break;
871 		case (PH_DEACTIVATE | REQUEST):
872 			FsmEvent(&st->l1.l1m, EV_PH_DEACTIVATE, NULL);
873 			break;
874 	}
875 }
876 
877 void
setstack_HiSax(struct PStack * st,struct IsdnCardState * cs)878 setstack_HiSax(struct PStack *st, struct IsdnCardState *cs)
879 {
880 	st->l1.hardware = cs;
881 	st->protocol = cs->protocol;
882 	st->l1.l1m.fsm = &l1fsm_s;
883 	st->l1.l1m.state = ST_L1_F3;
884 	st->l1.Flags = 0;
885 #ifdef HISAX_UINTERFACE
886 	if (test_bit(FLG_HW_L1_UINT, &cs->HW_Flags)) {
887 		st->l1.l1m.fsm = &l1fsm_u;
888 		st->l1.l1m.state = ST_L1_RESET;
889 		st->l1.Flags = FLG_L1_UINT;
890 	}
891 #endif
892 	st->l1.l1m.debug = cs->debug;
893 	st->l1.l1m.userdata = st;
894 	st->l1.l1m.userint = 0;
895 	st->l1.l1m.printdebug = l1m_debug;
896 	FsmInitTimer(&st->l1.l1m, &st->l1.timer);
897 	setstack_tei(st);
898 	setstack_manager(st);
899 	st->l1.stlistp = &(cs->stlist);
900 	st->l2.l2l1  = dch_l2l1;
901 	if (cs->setstack_d)
902 		cs->setstack_d(st, cs);
903 }
904 
905 void
setstack_l1_B(struct PStack * st)906 setstack_l1_B(struct PStack *st)
907 {
908 	struct IsdnCardState *cs = st->l1.hardware;
909 
910 	st->l1.l1m.fsm = &l1fsm_b;
911 	st->l1.l1m.state = ST_L1_NULL;
912 	st->l1.l1m.debug = cs->debug;
913 	st->l1.l1m.userdata = st;
914 	st->l1.l1m.userint = 0;
915 	st->l1.l1m.printdebug = l1m_debug;
916 	st->l1.Flags = 0;
917 	FsmInitTimer(&st->l1.l1m, &st->l1.timer);
918 }
919