1 /* $Id: isdnl2.c,v 1.1.4.1 2001/11/20 14:19:36 kai Exp $
2  *
3  * Author       Karsten Keil
4  *              based on the teles driver from Jan den Ouden
5  * Copyright    by Karsten Keil      <keil@isdn4linux.de>
6  *
7  * This software may be used and distributed according to the terms
8  * of the GNU General Public License, incorporated herein by reference.
9  *
10  * For changes and modifications please read
11  * ../../../Documentation/isdn/HiSax.cert
12  *
13  * Thanks to    Jan den Ouden
14  *              Fritz Elfert
15  *
16  */
17 
18 #define __NO_VERSION__
19 #include <linux/init.h>
20 #include "hisax.h"
21 #include "isdnl2.h"
22 
23 const char *l2_revision = "$Revision: 1.1.4.1 $";
24 
25 static void l2m_debug(struct FsmInst *fi, char *fmt, ...);
26 
27 static struct Fsm l2fsm;
28 
29 enum {
30 	ST_L2_1,
31 	ST_L2_2,
32 	ST_L2_3,
33 	ST_L2_4,
34 	ST_L2_5,
35 	ST_L2_6,
36 	ST_L2_7,
37 	ST_L2_8,
38 };
39 
40 #define L2_STATE_COUNT (ST_L2_8+1)
41 
42 static char *strL2State[] =
43 {
44 	"ST_L2_1",
45 	"ST_L2_2",
46 	"ST_L2_3",
47 	"ST_L2_4",
48 	"ST_L2_5",
49 	"ST_L2_6",
50 	"ST_L2_7",
51 	"ST_L2_8",
52 };
53 
54 enum {
55 	EV_L2_UI,
56 	EV_L2_SABME,
57 	EV_L2_DISC,
58 	EV_L2_DM,
59 	EV_L2_UA,
60 	EV_L2_FRMR,
61 	EV_L2_SUPER,
62 	EV_L2_I,
63 	EV_L2_DL_DATA,
64 	EV_L2_ACK_PULL,
65 	EV_L2_DL_UNIT_DATA,
66 	EV_L2_DL_ESTABLISH_REQ,
67 	EV_L2_DL_RELEASE_REQ,
68 	EV_L2_MDL_ASSIGN,
69 	EV_L2_MDL_REMOVE,
70 	EV_L2_MDL_ERROR,
71 	EV_L1_DEACTIVATE,
72 	EV_L2_T200,
73 	EV_L2_T203,
74 	EV_L2_SET_OWN_BUSY,
75 	EV_L2_CLEAR_OWN_BUSY,
76 	EV_L2_FRAME_ERROR,
77 };
78 
79 #define L2_EVENT_COUNT (EV_L2_FRAME_ERROR+1)
80 
81 static char *strL2Event[] =
82 {
83 	"EV_L2_UI",
84 	"EV_L2_SABME",
85 	"EV_L2_DISC",
86 	"EV_L2_DM",
87 	"EV_L2_UA",
88 	"EV_L2_FRMR",
89 	"EV_L2_SUPER",
90 	"EV_L2_I",
91 	"EV_L2_DL_DATA",
92 	"EV_L2_ACK_PULL",
93 	"EV_L2_DL_UNIT_DATA",
94 	"EV_L2_DL_ESTABLISH_REQ",
95 	"EV_L2_DL_RELEASE_REQ",
96 	"EV_L2_MDL_ASSIGN",
97 	"EV_L2_MDL_REMOVE",
98 	"EV_L2_MDL_ERROR",
99 	"EV_L1_DEACTIVATE",
100 	"EV_L2_T200",
101 	"EV_L2_T203",
102 	"EV_L2_SET_OWN_BUSY",
103 	"EV_L2_CLEAR_OWN_BUSY",
104 	"EV_L2_FRAME_ERROR",
105 };
106 
107 static int l2addrsize(struct Layer2 *l2);
108 
109 static void
set_peer_busy(struct Layer2 * l2)110 set_peer_busy(struct Layer2 *l2) {
111 	test_and_set_bit(FLG_PEER_BUSY, &l2->flag);
112 	if (skb_queue_len(&l2->i_queue) || skb_queue_len(&l2->ui_queue))
113 		test_and_set_bit(FLG_L2BLOCK, &l2->flag);
114 }
115 
116 static void
clear_peer_busy(struct Layer2 * l2)117 clear_peer_busy(struct Layer2 *l2) {
118 	if (test_and_clear_bit(FLG_PEER_BUSY, &l2->flag))
119 		test_and_clear_bit(FLG_L2BLOCK, &l2->flag);
120 }
121 
122 static void
InitWin(struct Layer2 * l2)123 InitWin(struct Layer2 *l2)
124 {
125 	int i;
126 
127 	for (i = 0; i < MAX_WINDOW; i++)
128 		l2->windowar[i] = NULL;
129 }
130 
131 static int
freewin1(struct Layer2 * l2)132 freewin1(struct Layer2 *l2)
133 {
134 	int i, cnt = 0;
135 
136 	for (i = 0; i < MAX_WINDOW; i++) {
137 		if (l2->windowar[i]) {
138 			cnt++;
139 			dev_kfree_skb(l2->windowar[i]);
140 			l2->windowar[i] = NULL;
141 		}
142 	}
143 	return cnt;
144 }
145 
146 inline void
freewin(struct PStack * st)147 freewin(struct PStack *st)
148 {
149 	freewin1(&st->l2);
150 }
151 
152 static void
ReleaseWin(struct Layer2 * l2)153 ReleaseWin(struct Layer2 *l2)
154 {
155 	int cnt;
156 
157 	if((cnt = freewin1(l2)))
158 		printk(KERN_WARNING "isdl2 freed %d skbuffs in release\n", cnt);
159 }
160 
161 inline unsigned int
cansend(struct PStack * st)162 cansend(struct PStack *st)
163 {
164 	unsigned int p1;
165 
166 	if(test_bit(FLG_MOD128, &st->l2.flag))
167 		p1 = (st->l2.vs - st->l2.va) % 128;
168 	else
169 		p1 = (st->l2.vs - st->l2.va) % 8;
170 	return ((p1 < st->l2.window) && !test_bit(FLG_PEER_BUSY, &st->l2.flag));
171 }
172 
173 inline void
clear_exception(struct Layer2 * l2)174 clear_exception(struct Layer2 *l2)
175 {
176 	test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
177 	test_and_clear_bit(FLG_REJEXC, &l2->flag);
178 	test_and_clear_bit(FLG_OWN_BUSY, &l2->flag);
179 	clear_peer_busy(l2);
180 }
181 
182 inline int
l2headersize(struct Layer2 * l2,int ui)183 l2headersize(struct Layer2 *l2, int ui)
184 {
185 	return (((test_bit(FLG_MOD128, &l2->flag) && (!ui)) ? 2 : 1) +
186 		(test_bit(FLG_LAPD, &l2->flag) ? 2 : 1));
187 }
188 
189 inline int
l2addrsize(struct Layer2 * l2)190 l2addrsize(struct Layer2 *l2)
191 {
192 	return (test_bit(FLG_LAPD, &l2->flag) ? 2 : 1);
193 }
194 
195 static int
sethdraddr(struct Layer2 * l2,u_char * header,int rsp)196 sethdraddr(struct Layer2 *l2, u_char * header, int rsp)
197 {
198 	u_char *ptr = header;
199 	int crbit = rsp;
200 
201 	if (test_bit(FLG_LAPD, &l2->flag)) {
202 		*ptr++ = (l2->sap << 2) | (rsp ? 2 : 0);
203 		*ptr++ = (l2->tei << 1) | 1;
204 		return (2);
205 	} else {
206 		if (test_bit(FLG_ORIG, &l2->flag))
207 			crbit = !crbit;
208 		if (crbit)
209 			*ptr++ = 1;
210 		else
211 			*ptr++ = 3;
212 		return (1);
213 	}
214 }
215 
216 inline static void
enqueue_super(struct PStack * st,struct sk_buff * skb)217 enqueue_super(struct PStack *st,
218 	      struct sk_buff *skb)
219 {
220 	if (test_bit(FLG_LAPB, &st->l2.flag))
221 		st->l1.bcs->tx_cnt += skb->len;
222 	st->l2.l2l1(st, PH_DATA | REQUEST, skb);
223 }
224 
225 #define enqueue_ui(a, b) enqueue_super(a, b)
226 
227 inline int
IsUI(u_char * data)228 IsUI(u_char * data)
229 {
230 	return ((data[0] & 0xef) == UI);
231 }
232 
233 inline int
IsUA(u_char * data)234 IsUA(u_char * data)
235 {
236 	return ((data[0] & 0xef) == UA);
237 }
238 
239 inline int
IsDM(u_char * data)240 IsDM(u_char * data)
241 {
242 	return ((data[0] & 0xef) == DM);
243 }
244 
245 inline int
IsDISC(u_char * data)246 IsDISC(u_char * data)
247 {
248 	return ((data[0] & 0xef) == DISC);
249 }
250 
251 inline int
IsRR(u_char * data,struct PStack * st)252 IsRR(u_char * data, struct PStack *st)
253 {
254 	if (test_bit(FLG_MOD128, &st->l2.flag))
255 		return (data[0] == RR);
256 	else
257 		return ((data[0] & 0xf) == 1);
258 }
259 
260 inline int
IsSFrame(u_char * data,struct PStack * st)261 IsSFrame(u_char * data, struct PStack *st)
262 {
263 	register u_char d = *data;
264 
265 	if (!test_bit(FLG_MOD128, &st->l2.flag))
266 		d &= 0xf;
267 	return(((d & 0xf3) == 1) && ((d & 0x0c) != 0x0c));
268 }
269 
270 inline int
IsSABME(u_char * data,struct PStack * st)271 IsSABME(u_char * data, struct PStack *st)
272 {
273 	u_char d = data[0] & ~0x10;
274 
275 	return (test_bit(FLG_MOD128, &st->l2.flag) ? d == SABME : d == SABM);
276 }
277 
278 inline int
IsREJ(u_char * data,struct PStack * st)279 IsREJ(u_char * data, struct PStack *st)
280 {
281 	return (test_bit(FLG_MOD128, &st->l2.flag) ? data[0] == REJ : (data[0] & 0xf) == REJ);
282 }
283 
284 inline int
IsFRMR(u_char * data)285 IsFRMR(u_char * data)
286 {
287 	return ((data[0] & 0xef) == FRMR);
288 }
289 
290 inline int
IsRNR(u_char * data,struct PStack * st)291 IsRNR(u_char * data, struct PStack *st)
292 {
293 	return (test_bit(FLG_MOD128, &st->l2.flag) ? data[0] == RNR : (data[0] & 0xf) == RNR);
294 }
295 
296 int
iframe_error(struct PStack * st,struct sk_buff * skb)297 iframe_error(struct PStack *st, struct sk_buff *skb)
298 {
299 	int i = l2addrsize(&st->l2) + (test_bit(FLG_MOD128, &st->l2.flag) ? 2 : 1);
300 	int rsp = *skb->data & 0x2;
301 
302 	if (test_bit(FLG_ORIG, &st->l2.flag))
303 		rsp = !rsp;
304 
305 	if (rsp)
306 		return 'L';
307 
308 
309 	if (skb->len < i)
310 		return 'N';
311 
312 	if ((skb->len - i) > st->l2.maxlen)
313 		return 'O';
314 
315 
316 	return 0;
317 }
318 
319 int
super_error(struct PStack * st,struct sk_buff * skb)320 super_error(struct PStack *st, struct sk_buff *skb)
321 {
322 	if (skb->len != l2addrsize(&st->l2) +
323 	    (test_bit(FLG_MOD128, &st->l2.flag) ? 2 : 1))
324 		return 'N';
325 
326 	return 0;
327 }
328 
329 int
unnum_error(struct PStack * st,struct sk_buff * skb,int wantrsp)330 unnum_error(struct PStack *st, struct sk_buff *skb, int wantrsp)
331 {
332 	int rsp = (*skb->data & 0x2) >> 1;
333 	if (test_bit(FLG_ORIG, &st->l2.flag))
334 		rsp = !rsp;
335 
336 	if (rsp != wantrsp)
337 		return 'L';
338 
339 	if (skb->len != l2addrsize(&st->l2) + 1)
340 		return 'N';
341 
342 	return 0;
343 }
344 
345 int
UI_error(struct PStack * st,struct sk_buff * skb)346 UI_error(struct PStack *st, struct sk_buff *skb)
347 {
348 	int rsp = *skb->data & 0x2;
349 	if (test_bit(FLG_ORIG, &st->l2.flag))
350 		rsp = !rsp;
351 
352 	if (rsp)
353 		return 'L';
354 
355 	if (skb->len > st->l2.maxlen + l2addrsize(&st->l2) + 1)
356 		return 'O';
357 
358 	return 0;
359 }
360 
361 int
FRMR_error(struct PStack * st,struct sk_buff * skb)362 FRMR_error(struct PStack *st, struct sk_buff *skb)
363 {
364 	int headers = l2addrsize(&st->l2) + 1;
365 	u_char *datap = skb->data + headers;
366 	int rsp = *skb->data & 0x2;
367 
368 	if (test_bit(FLG_ORIG, &st->l2.flag))
369 		rsp = !rsp;
370 
371 	if (!rsp)
372 		return 'L';
373 
374 	if (test_bit(FLG_MOD128, &st->l2.flag)) {
375 		if (skb->len < headers + 5)
376 			return 'N';
377 		else
378 			l2m_debug(&st->l2.l2m, "FRMR information %2x %2x %2x %2x %2x",
379 				datap[0], datap[1], datap[2],
380 				datap[3], datap[4]);
381 	} else {
382 		if (skb->len < headers + 3)
383 			return 'N';
384 		else
385 			l2m_debug(&st->l2.l2m, "FRMR information %2x %2x %2x",
386 				datap[0], datap[1], datap[2]);
387 	}
388 
389 	return 0;
390 }
391 
392 static unsigned int
legalnr(struct PStack * st,unsigned int nr)393 legalnr(struct PStack *st, unsigned int nr)
394 {
395         struct Layer2 *l2 = &st->l2;
396 
397 	if(test_bit(FLG_MOD128, &l2->flag))
398 		return ((nr - l2->va) % 128) <= ((l2->vs - l2->va) % 128);
399 	else
400 		return ((nr - l2->va) % 8) <= ((l2->vs - l2->va) % 8);
401 }
402 
403 static void
setva(struct PStack * st,unsigned int nr)404 setva(struct PStack *st, unsigned int nr)
405 {
406 	struct Layer2 *l2 = &st->l2;
407 	int len;
408 
409 	while (l2->va != nr) {
410 		(l2->va)++;
411 		if(test_bit(FLG_MOD128, &l2->flag))
412 			l2->va %= 128;
413 		else
414 			l2->va %= 8;
415 		len = l2->windowar[l2->sow]->len;
416 		if (PACKET_NOACK == l2->windowar[l2->sow]->pkt_type)
417 			len = -1;
418 		dev_kfree_skb(l2->windowar[l2->sow]);
419 		l2->windowar[l2->sow] = NULL;
420 		l2->sow = (l2->sow + 1) % l2->window;
421 		if (st->lli.l2writewakeup && (len >=0))
422 			st->lli.l2writewakeup(st, len);
423 	}
424 }
425 
426 static void
send_uframe(struct PStack * st,u_char cmd,u_char cr)427 send_uframe(struct PStack *st, u_char cmd, u_char cr)
428 {
429 	struct sk_buff *skb;
430 	u_char tmp[MAX_HEADER_LEN];
431 	int i;
432 
433 	i = sethdraddr(&st->l2, tmp, cr);
434 	tmp[i++] = cmd;
435 	if (!(skb = alloc_skb(i, GFP_ATOMIC))) {
436 		printk(KERN_WARNING "isdl2 can't alloc sbbuff for send_uframe\n");
437 		return;
438 	}
439 	memcpy(skb_put(skb, i), tmp, i);
440 	enqueue_super(st, skb);
441 }
442 
443 inline u_char
get_PollFlag(struct PStack * st,struct sk_buff * skb)444 get_PollFlag(struct PStack * st, struct sk_buff * skb)
445 {
446 	return (skb->data[l2addrsize(&(st->l2))] & 0x10);
447 }
448 
449 inline void
FreeSkb(struct sk_buff * skb)450 FreeSkb(struct sk_buff *skb)
451 {
452 	dev_kfree_skb(skb);
453 }
454 
455 
456 inline u_char
get_PollFlagFree(struct PStack * st,struct sk_buff * skb)457 get_PollFlagFree(struct PStack *st, struct sk_buff *skb)
458 {
459 	u_char PF;
460 
461 	PF = get_PollFlag(st, skb);
462 	FreeSkb(skb);
463 	return (PF);
464 }
465 
466 inline void
start_t200(struct PStack * st,int i)467 start_t200(struct PStack *st, int i)
468 {
469 	FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, i);
470 	test_and_set_bit(FLG_T200_RUN, &st->l2.flag);
471 }
472 
473 inline void
restart_t200(struct PStack * st,int i)474 restart_t200(struct PStack *st, int i)
475 {
476 	FsmRestartTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, i);
477 	test_and_set_bit(FLG_T200_RUN, &st->l2.flag);
478 }
479 
480 inline void
stop_t200(struct PStack * st,int i)481 stop_t200(struct PStack *st, int i)
482 {
483 	if(test_and_clear_bit(FLG_T200_RUN, &st->l2.flag))
484 		FsmDelTimer(&st->l2.t200, i);
485 }
486 
487 inline void
st5_dl_release_l2l3(struct PStack * st)488 st5_dl_release_l2l3(struct PStack *st)
489 {
490 		int pr;
491 
492 		if(test_and_clear_bit(FLG_PEND_REL, &st->l2.flag))
493 			pr = DL_RELEASE | CONFIRM;
494 		else
495 			pr = DL_RELEASE | INDICATION;
496 
497 		st->l2.l2l3(st, pr, NULL);
498 }
499 
500 inline void
lapb_dl_release_l2l3(struct PStack * st,int f)501 lapb_dl_release_l2l3(struct PStack *st, int f)
502 {
503 		if (test_bit(FLG_LAPB, &st->l2.flag))
504 			st->l2.l2l1(st, PH_DEACTIVATE | REQUEST, NULL);
505 		st->l2.l2l3(st, DL_RELEASE | f, NULL);
506 }
507 
508 static void
establishlink(struct FsmInst * fi)509 establishlink(struct FsmInst *fi)
510 {
511 	struct PStack *st = fi->userdata;
512 	u_char cmd;
513 
514 	clear_exception(&st->l2);
515 	st->l2.rc = 0;
516 	cmd = (test_bit(FLG_MOD128, &st->l2.flag) ? SABME : SABM) | 0x10;
517 	send_uframe(st, cmd, CMD);
518 	FsmDelTimer(&st->l2.t203, 1);
519 	restart_t200(st, 1);
520 	test_and_clear_bit(FLG_PEND_REL, &st->l2.flag);
521 	freewin(st);
522 	FsmChangeState(fi, ST_L2_5);
523 }
524 
525 static void
l2_mdl_error_ua(struct FsmInst * fi,int event,void * arg)526 l2_mdl_error_ua(struct FsmInst *fi, int event, void *arg)
527 {
528 	struct sk_buff *skb = arg;
529 	struct PStack *st = fi->userdata;
530 
531 	if (get_PollFlagFree(st, skb))
532 		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'C');
533 	else
534 		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'D');
535 }
536 
537 static void
l2_mdl_error_dm(struct FsmInst * fi,int event,void * arg)538 l2_mdl_error_dm(struct FsmInst *fi, int event, void *arg)
539 {
540 	struct sk_buff *skb = arg;
541 	struct PStack *st = fi->userdata;
542 
543 	if (get_PollFlagFree(st, skb))
544 		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'B');
545 	else {
546 		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'E');
547 		establishlink(fi);
548 		test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
549 	}
550 }
551 
552 static void
l2_st8_mdl_error_dm(struct FsmInst * fi,int event,void * arg)553 l2_st8_mdl_error_dm(struct FsmInst *fi, int event, void *arg)
554 {
555 	struct sk_buff *skb = arg;
556 	struct PStack *st = fi->userdata;
557 
558 	if (get_PollFlagFree(st, skb))
559 		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'B');
560 	else {
561 		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'E');
562 	}
563 	establishlink(fi);
564 	test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
565 }
566 
567 static void
l2_go_st3(struct FsmInst * fi,int event,void * arg)568 l2_go_st3(struct FsmInst *fi, int event, void *arg)
569 {
570 	FsmChangeState(fi, ST_L2_3);
571 }
572 
573 static void
l2_mdl_assign(struct FsmInst * fi,int event,void * arg)574 l2_mdl_assign(struct FsmInst *fi, int event, void *arg)
575 {
576 	struct PStack *st = fi->userdata;
577 
578 	FsmChangeState(fi, ST_L2_3);
579 	st->l2.l2tei(st, MDL_ASSIGN | INDICATION, NULL);
580 }
581 
582 static void
l2_queue_ui_assign(struct FsmInst * fi,int event,void * arg)583 l2_queue_ui_assign(struct FsmInst *fi, int event, void *arg)
584 {
585 	struct PStack *st = fi->userdata;
586 	struct sk_buff *skb = arg;
587 
588 	skb_queue_tail(&st->l2.ui_queue, skb);
589 	FsmChangeState(fi, ST_L2_2);
590 	st->l2.l2tei(st, MDL_ASSIGN | INDICATION, NULL);
591 }
592 
593 static void
l2_queue_ui(struct FsmInst * fi,int event,void * arg)594 l2_queue_ui(struct FsmInst *fi, int event, void *arg)
595 {
596 	struct PStack *st = fi->userdata;
597 	struct sk_buff *skb = arg;
598 
599 	skb_queue_tail(&st->l2.ui_queue, skb);
600 }
601 
602 static void
tx_ui(struct PStack * st)603 tx_ui(struct PStack *st)
604 {
605 	struct sk_buff *skb;
606 	u_char header[MAX_HEADER_LEN];
607 	int i;
608 
609 	i = sethdraddr(&(st->l2), header, CMD);
610 	header[i++] = UI;
611 	while ((skb = skb_dequeue(&st->l2.ui_queue))) {
612 		memcpy(skb_push(skb, i), header, i);
613 		enqueue_ui(st, skb);
614 	}
615 }
616 
617 static void
l2_send_ui(struct FsmInst * fi,int event,void * arg)618 l2_send_ui(struct FsmInst *fi, int event, void *arg)
619 {
620 	struct PStack *st = fi->userdata;
621 	struct sk_buff *skb = arg;
622 
623 	skb_queue_tail(&st->l2.ui_queue, skb);
624 	tx_ui(st);
625 }
626 
627 static void
l2_got_ui(struct FsmInst * fi,int event,void * arg)628 l2_got_ui(struct FsmInst *fi, int event, void *arg)
629 {
630 	struct PStack *st = fi->userdata;
631 	struct sk_buff *skb = arg;
632 
633 	skb_pull(skb, l2headersize(&st->l2, 1));
634 	st->l2.l2l3(st, DL_UNIT_DATA | INDICATION, skb);
635 /*	^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
636  *		in states 1-3 for broadcast
637  */
638 
639 
640 }
641 
642 static void
l2_establish(struct FsmInst * fi,int event,void * arg)643 l2_establish(struct FsmInst *fi, int event, void *arg)
644 {
645 	struct PStack *st = fi->userdata;
646 
647 	establishlink(fi);
648 	test_and_set_bit(FLG_L3_INIT, &st->l2.flag);
649 }
650 
651 static void
l2_discard_i_setl3(struct FsmInst * fi,int event,void * arg)652 l2_discard_i_setl3(struct FsmInst *fi, int event, void *arg)
653 {
654 	struct PStack *st = fi->userdata;
655 
656 	skb_queue_purge(&st->l2.i_queue);
657 	test_and_set_bit(FLG_L3_INIT, &st->l2.flag);
658 	test_and_clear_bit(FLG_PEND_REL, &st->l2.flag);
659 }
660 
661 static void
l2_l3_reestablish(struct FsmInst * fi,int event,void * arg)662 l2_l3_reestablish(struct FsmInst *fi, int event, void *arg)
663 {
664 	struct PStack *st = fi->userdata;
665 
666 	skb_queue_purge(&st->l2.i_queue);
667 	establishlink(fi);
668 	test_and_set_bit(FLG_L3_INIT, &st->l2.flag);
669 }
670 
671 static void
l2_release(struct FsmInst * fi,int event,void * arg)672 l2_release(struct FsmInst *fi, int event, void *arg)
673 {
674 	struct PStack *st = fi->userdata;
675 
676 	st->l2.l2l3(st, DL_RELEASE | CONFIRM, NULL);
677 }
678 
679 static void
l2_pend_rel(struct FsmInst * fi,int event,void * arg)680 l2_pend_rel(struct FsmInst *fi, int event, void *arg)
681 {
682 	struct PStack *st = fi->userdata;
683 
684 	test_and_set_bit(FLG_PEND_REL, &st->l2.flag);
685 }
686 
687 static void
l2_disconnect(struct FsmInst * fi,int event,void * arg)688 l2_disconnect(struct FsmInst *fi, int event, void *arg)
689 {
690 	struct PStack *st = fi->userdata;
691 
692 	skb_queue_purge(&st->l2.i_queue);
693 	freewin(st);
694 	FsmChangeState(fi, ST_L2_6);
695 	st->l2.rc = 0;
696 	send_uframe(st, DISC | 0x10, CMD);
697 	FsmDelTimer(&st->l2.t203, 1);
698 	restart_t200(st, 2);
699 }
700 
701 static void
l2_start_multi(struct FsmInst * fi,int event,void * arg)702 l2_start_multi(struct FsmInst *fi, int event, void *arg)
703 {
704 	struct PStack *st = fi->userdata;
705 	struct sk_buff *skb = arg;
706 
707 	send_uframe(st, UA | get_PollFlagFree(st, skb), RSP);
708 
709 	clear_exception(&st->l2);
710 	st->l2.vs = 0;
711 	st->l2.va = 0;
712 	st->l2.vr = 0;
713 	st->l2.sow = 0;
714 	FsmChangeState(fi, ST_L2_7);
715 	FsmAddTimer(&st->l2.t203, st->l2.T203, EV_L2_T203, NULL, 3);
716 
717 	st->l2.l2l3(st, DL_ESTABLISH | INDICATION, NULL);
718 }
719 
720 static void
l2_send_UA(struct FsmInst * fi,int event,void * arg)721 l2_send_UA(struct FsmInst *fi, int event, void *arg)
722 {
723 	struct PStack *st = fi->userdata;
724 	struct sk_buff *skb = arg;
725 
726 	send_uframe(st, UA | get_PollFlagFree(st, skb), RSP);
727 }
728 
729 static void
l2_send_DM(struct FsmInst * fi,int event,void * arg)730 l2_send_DM(struct FsmInst *fi, int event, void *arg)
731 {
732 	struct PStack *st = fi->userdata;
733 	struct sk_buff *skb = arg;
734 
735 	send_uframe(st, DM | get_PollFlagFree(st, skb), RSP);
736 }
737 
738 static void
l2_restart_multi(struct FsmInst * fi,int event,void * arg)739 l2_restart_multi(struct FsmInst *fi, int event, void *arg)
740 {
741 	struct PStack *st = fi->userdata;
742 	struct sk_buff *skb = arg;
743 	int est = 0, state;
744 
745 	state = fi->state;
746 
747 	send_uframe(st, UA | get_PollFlagFree(st, skb), RSP);
748 
749 	st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'F');
750 
751 	if (st->l2.vs != st->l2.va) {
752 		skb_queue_purge(&st->l2.i_queue);
753 		est = 1;
754 	}
755 
756 	clear_exception(&st->l2);
757 	st->l2.vs = 0;
758 	st->l2.va = 0;
759 	st->l2.vr = 0;
760 	st->l2.sow = 0;
761 	FsmChangeState(fi, ST_L2_7);
762 	stop_t200(st, 3);
763 	FsmRestartTimer(&st->l2.t203, st->l2.T203, EV_L2_T203, NULL, 3);
764 
765 	if (est)
766 		st->l2.l2l3(st, DL_ESTABLISH | INDICATION, NULL);
767 
768 	if ((ST_L2_7==state) || (ST_L2_8 == state))
769 		if (skb_queue_len(&st->l2.i_queue) && cansend(st))
770 			st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
771 }
772 
773 static void
l2_stop_multi(struct FsmInst * fi,int event,void * arg)774 l2_stop_multi(struct FsmInst *fi, int event, void *arg)
775 {
776 	struct PStack *st = fi->userdata;
777 	struct sk_buff *skb = arg;
778 
779 	FsmChangeState(fi, ST_L2_4);
780 	FsmDelTimer(&st->l2.t203, 3);
781 	stop_t200(st, 4);
782 
783 	send_uframe(st, UA | get_PollFlagFree(st, skb), RSP);
784 
785 	skb_queue_purge(&st->l2.i_queue);
786 	freewin(st);
787 	lapb_dl_release_l2l3(st, INDICATION);
788 }
789 
790 static void
l2_connected(struct FsmInst * fi,int event,void * arg)791 l2_connected(struct FsmInst *fi, int event, void *arg)
792 {
793 	struct PStack *st = fi->userdata;
794 	struct sk_buff *skb = arg;
795 	int pr=-1;
796 
797 	if (!get_PollFlag(st, skb)) {
798 		l2_mdl_error_ua(fi, event, arg);
799 		return;
800 	}
801 	FreeSkb(skb);
802 
803 	if (test_and_clear_bit(FLG_PEND_REL, &st->l2.flag))
804 		l2_disconnect(fi, event, arg);
805 
806 	if (test_and_clear_bit(FLG_L3_INIT, &st->l2.flag)) {
807 		pr = DL_ESTABLISH | CONFIRM;
808 	} else if (st->l2.vs != st->l2.va) {
809 		skb_queue_purge(&st->l2.i_queue);
810 		pr = DL_ESTABLISH | INDICATION;
811 	}
812 
813 	stop_t200(st, 5);
814 
815 	st->l2.vr = 0;
816 	st->l2.vs = 0;
817 	st->l2.va = 0;
818 	st->l2.sow = 0;
819 	FsmChangeState(fi, ST_L2_7);
820 	FsmAddTimer(&st->l2.t203, st->l2.T203, EV_L2_T203, NULL, 4);
821 
822 	if (pr != -1)
823 		st->l2.l2l3(st, pr, NULL);
824 
825 	if (skb_queue_len(&st->l2.i_queue) && cansend(st))
826 		st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
827 }
828 
829 static void
l2_released(struct FsmInst * fi,int event,void * arg)830 l2_released(struct FsmInst *fi, int event, void *arg)
831 {
832 	struct PStack *st = fi->userdata;
833 	struct sk_buff *skb = arg;
834 
835 	if (!get_PollFlag(st, skb)) {
836 		l2_mdl_error_ua(fi, event, arg);
837 		return;
838 	}
839 	FreeSkb(skb);
840 
841 	stop_t200(st, 6);
842 	lapb_dl_release_l2l3(st, CONFIRM);
843 	FsmChangeState(fi, ST_L2_4);
844 }
845 
846 static void
l2_reestablish(struct FsmInst * fi,int event,void * arg)847 l2_reestablish(struct FsmInst *fi, int event, void *arg)
848 {
849 	struct PStack *st = fi->userdata;
850 	struct sk_buff *skb = arg;
851 
852 	if (!get_PollFlagFree(st, skb)) {
853 		establishlink(fi);
854 		test_and_set_bit(FLG_L3_INIT, &st->l2.flag);
855 	}
856 }
857 
858 static void
l2_st5_dm_release(struct FsmInst * fi,int event,void * arg)859 l2_st5_dm_release(struct FsmInst *fi, int event, void *arg)
860 {
861 	struct PStack *st = fi->userdata;
862 	struct sk_buff *skb = arg;
863 
864 	if (get_PollFlagFree(st, skb)) {
865 		stop_t200(st, 7);
866 	 	if (!test_bit(FLG_L3_INIT, &st->l2.flag))
867 			skb_queue_purge(&st->l2.i_queue);
868 		if (test_bit(FLG_LAPB, &st->l2.flag))
869 			st->l2.l2l1(st, PH_DEACTIVATE | REQUEST, NULL);
870 		st5_dl_release_l2l3(st);
871 		FsmChangeState(fi, ST_L2_4);
872 	}
873 }
874 
875 static void
l2_st6_dm_release(struct FsmInst * fi,int event,void * arg)876 l2_st6_dm_release(struct FsmInst *fi, int event, void *arg)
877 {
878 	struct PStack *st = fi->userdata;
879 	struct sk_buff *skb = arg;
880 
881 	if (get_PollFlagFree(st, skb)) {
882 		stop_t200(st, 8);
883 		lapb_dl_release_l2l3(st, CONFIRM);
884 		FsmChangeState(fi, ST_L2_4);
885 	}
886 }
887 
888 inline void
enquiry_cr(struct PStack * st,u_char typ,u_char cr,u_char pf)889 enquiry_cr(struct PStack *st, u_char typ, u_char cr, u_char pf)
890 {
891 	struct sk_buff *skb;
892 	struct Layer2 *l2;
893 	u_char tmp[MAX_HEADER_LEN];
894 	int i;
895 
896 	l2 = &st->l2;
897 	i = sethdraddr(l2, tmp, cr);
898 	if (test_bit(FLG_MOD128, &l2->flag)) {
899 		tmp[i++] = typ;
900 		tmp[i++] = (l2->vr << 1) | (pf ? 1 : 0);
901 	} else
902 		tmp[i++] = (l2->vr << 5) | typ | (pf ? 0x10 : 0);
903 	if (!(skb = alloc_skb(i, GFP_ATOMIC))) {
904 		printk(KERN_WARNING "isdl2 can't alloc sbbuff for enquiry_cr\n");
905 		return;
906 	}
907 	memcpy(skb_put(skb, i), tmp, i);
908 	enqueue_super(st, skb);
909 }
910 
911 inline void
enquiry_response(struct PStack * st)912 enquiry_response(struct PStack *st)
913 {
914 	if (test_bit(FLG_OWN_BUSY, &st->l2.flag))
915 		enquiry_cr(st, RNR, RSP, 1);
916 	else
917 		enquiry_cr(st, RR, RSP, 1);
918 	test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag);
919 }
920 
921 inline void
transmit_enquiry(struct PStack * st)922 transmit_enquiry(struct PStack *st)
923 {
924 	if (test_bit(FLG_OWN_BUSY, &st->l2.flag))
925 		enquiry_cr(st, RNR, CMD, 1);
926 	else
927 		enquiry_cr(st, RR, CMD, 1);
928 	test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag);
929 	start_t200(st, 9);
930 }
931 
932 
933 static void
nrerrorrecovery(struct FsmInst * fi)934 nrerrorrecovery(struct FsmInst *fi)
935 {
936 	struct PStack *st = fi->userdata;
937 
938 	st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'J');
939 	establishlink(fi);
940 	test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
941 }
942 
943 static void
invoke_retransmission(struct PStack * st,unsigned int nr)944 invoke_retransmission(struct PStack *st, unsigned int nr)
945 {
946 	struct Layer2 *l2 = &st->l2;
947 	unsigned int p1;
948 
949 	if (l2->vs != nr) {
950 		while (l2->vs != nr) {
951 			(l2->vs)--;
952 			if(test_bit(FLG_MOD128, &l2->flag)) {
953 				l2->vs %= 128;
954 				p1 = (l2->vs - l2->va) % 128;
955 			} else {
956 				l2->vs %= 8;
957 				p1 = (l2->vs - l2->va) % 8;
958 			}
959 			p1 = (p1 + l2->sow) % l2->window;
960 			if (test_bit(FLG_LAPB, &l2->flag))
961 				st->l1.bcs->tx_cnt += l2->windowar[p1]->len + l2headersize(l2, 0);
962 			skb_queue_head(&l2->i_queue, l2->windowar[p1]);
963 			l2->windowar[p1] = NULL;
964 		}
965 		st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
966 	}
967 }
968 
969 static void
l2_st7_got_super(struct FsmInst * fi,int event,void * arg)970 l2_st7_got_super(struct FsmInst *fi, int event, void *arg)
971 {
972 	struct PStack *st = fi->userdata;
973 	struct sk_buff *skb = arg;
974 	int PollFlag, rsp, typ = RR;
975 	unsigned int nr;
976 	struct Layer2 *l2 = &st->l2;
977 
978 	rsp = *skb->data & 0x2;
979 	if (test_bit(FLG_ORIG, &l2->flag))
980 		rsp = !rsp;
981 
982 	skb_pull(skb, l2addrsize(l2));
983 	if (IsRNR(skb->data, st)) {
984 		set_peer_busy(l2);
985 		typ = RNR;
986 	} else
987 		clear_peer_busy(l2);
988 	if (IsREJ(skb->data, st))
989 		typ = REJ;
990 
991 	if (test_bit(FLG_MOD128, &l2->flag)) {
992 		PollFlag = (skb->data[1] & 0x1) == 0x1;
993 		nr = skb->data[1] >> 1;
994 	} else {
995 		PollFlag = (skb->data[0] & 0x10);
996 		nr = (skb->data[0] >> 5) & 0x7;
997 	}
998 	FreeSkb(skb);
999 
1000 	if (PollFlag) {
1001 		if (rsp)
1002 			st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'A');
1003 		else
1004 			enquiry_response(st);
1005 	}
1006 	if (legalnr(st, nr)) {
1007 		if (typ == REJ) {
1008 			setva(st, nr);
1009 			invoke_retransmission(st, nr);
1010 			stop_t200(st, 10);
1011 			if (FsmAddTimer(&st->l2.t203, st->l2.T203,
1012 					EV_L2_T203, NULL, 6))
1013 				l2m_debug(&st->l2.l2m, "Restart T203 ST7 REJ");
1014 		} else if ((nr == l2->vs) && (typ == RR)) {
1015 			setva(st, nr);
1016 			stop_t200(st, 11);
1017 			FsmRestartTimer(&st->l2.t203, st->l2.T203,
1018 					EV_L2_T203, NULL, 7);
1019 		} else if ((l2->va != nr) || (typ == RNR)) {
1020 			setva(st, nr);
1021 			if(typ != RR) FsmDelTimer(&st->l2.t203, 9);
1022 			restart_t200(st, 12);
1023 		}
1024 		if (skb_queue_len(&st->l2.i_queue) && (typ == RR))
1025 			st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
1026 	} else
1027 		nrerrorrecovery(fi);
1028 }
1029 
1030 static void
l2_feed_i_if_reest(struct FsmInst * fi,int event,void * arg)1031 l2_feed_i_if_reest(struct FsmInst *fi, int event, void *arg)
1032 {
1033 	struct PStack *st = fi->userdata;
1034 	struct sk_buff *skb = arg;
1035 
1036 	if (test_bit(FLG_LAPB, &st->l2.flag))
1037 		st->l1.bcs->tx_cnt += skb->len + l2headersize(&st->l2, 0);
1038 	if (!test_bit(FLG_L3_INIT, &st->l2.flag))
1039 		skb_queue_tail(&st->l2.i_queue, skb);
1040 	else
1041 		FreeSkb(skb);
1042 }
1043 
1044 static void
l2_feed_i_pull(struct FsmInst * fi,int event,void * arg)1045 l2_feed_i_pull(struct FsmInst *fi, int event, void *arg)
1046 {
1047 	struct PStack *st = fi->userdata;
1048 	struct sk_buff *skb = arg;
1049 
1050 	if (test_bit(FLG_LAPB, &st->l2.flag))
1051 		st->l1.bcs->tx_cnt += skb->len + l2headersize(&st->l2, 0);
1052 	skb_queue_tail(&st->l2.i_queue, skb);
1053 	st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
1054 }
1055 
1056 static void
l2_feed_iqueue(struct FsmInst * fi,int event,void * arg)1057 l2_feed_iqueue(struct FsmInst *fi, int event, void *arg)
1058 {
1059 	struct PStack *st = fi->userdata;
1060 	struct sk_buff *skb = arg;
1061 
1062 	if (test_bit(FLG_LAPB, &st->l2.flag))
1063 		st->l1.bcs->tx_cnt += skb->len + l2headersize(&st->l2, 0);
1064 	skb_queue_tail(&st->l2.i_queue, skb);
1065 }
1066 
1067 static void
l2_got_iframe(struct FsmInst * fi,int event,void * arg)1068 l2_got_iframe(struct FsmInst *fi, int event, void *arg)
1069 {
1070 	struct PStack *st = fi->userdata;
1071 	struct sk_buff *skb = arg;
1072 	struct Layer2 *l2 = &(st->l2);
1073 	int PollFlag, ns, i;
1074 	unsigned int nr;
1075 
1076 	i = l2addrsize(l2);
1077 	if (test_bit(FLG_MOD128, &l2->flag)) {
1078 		PollFlag = ((skb->data[i + 1] & 0x1) == 0x1);
1079 		ns = skb->data[i] >> 1;
1080 		nr = (skb->data[i + 1] >> 1) & 0x7f;
1081 	} else {
1082 		PollFlag = (skb->data[i] & 0x10);
1083 		ns = (skb->data[i] >> 1) & 0x7;
1084 		nr = (skb->data[i] >> 5) & 0x7;
1085 	}
1086 	if (test_bit(FLG_OWN_BUSY, &l2->flag)) {
1087 		FreeSkb(skb);
1088 		if(PollFlag) enquiry_response(st);
1089 	} else if (l2->vr == ns) {
1090 		(l2->vr)++;
1091 		if(test_bit(FLG_MOD128, &l2->flag))
1092 			l2->vr %= 128;
1093 		else
1094 			l2->vr %= 8;
1095 		test_and_clear_bit(FLG_REJEXC, &l2->flag);
1096 
1097 		if (PollFlag)
1098 			enquiry_response(st);
1099 		else
1100 			test_and_set_bit(FLG_ACK_PEND, &l2->flag);
1101 		skb_pull(skb, l2headersize(l2, 0));
1102 		st->l2.l2l3(st, DL_DATA | INDICATION, skb);
1103 	} else {
1104 		/* n(s)!=v(r) */
1105 		FreeSkb(skb);
1106 		if (test_and_set_bit(FLG_REJEXC, &l2->flag)) {
1107 			if (PollFlag)
1108 				enquiry_response(st);
1109 		} else {
1110 			enquiry_cr(st, REJ, RSP, PollFlag);
1111 			test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
1112 		}
1113 	}
1114 
1115 	if (legalnr(st, nr)) {
1116 		if (!test_bit(FLG_PEER_BUSY, &st->l2.flag) && (fi->state == ST_L2_7)) {
1117 			if (nr == st->l2.vs) {
1118 				stop_t200(st, 13);
1119 				FsmRestartTimer(&st->l2.t203, st->l2.T203,
1120 						EV_L2_T203, NULL, 7);
1121 			} else if (nr != st->l2.va)
1122 				restart_t200(st, 14);
1123 		}
1124 		setva(st, nr);
1125 	} else {
1126 		nrerrorrecovery(fi);
1127 		return;
1128 	}
1129 
1130 	if (skb_queue_len(&st->l2.i_queue) && (fi->state == ST_L2_7))
1131 		st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
1132 	if (test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag))
1133 		enquiry_cr(st, RR, RSP, 0);
1134 }
1135 
1136 static void
l2_got_tei(struct FsmInst * fi,int event,void * arg)1137 l2_got_tei(struct FsmInst *fi, int event, void *arg)
1138 {
1139 	struct PStack *st = fi->userdata;
1140 
1141 	st->l2.tei = (long) arg;
1142 
1143 	if (fi->state == ST_L2_3) {
1144 		establishlink(fi);
1145 		test_and_set_bit(FLG_L3_INIT, &st->l2.flag);
1146 	} else
1147 		FsmChangeState(fi, ST_L2_4);
1148 	if (skb_queue_len(&st->l2.ui_queue))
1149 		tx_ui(st);
1150 }
1151 
1152 static void
l2_st5_tout_200(struct FsmInst * fi,int event,void * arg)1153 l2_st5_tout_200(struct FsmInst *fi, int event, void *arg)
1154 {
1155 	struct PStack *st = fi->userdata;
1156 
1157 	if (test_bit(FLG_LAPD, &st->l2.flag) &&
1158 		test_bit(FLG_DCHAN_BUSY, &st->l2.flag)) {
1159 		FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 9);
1160 	} else if (st->l2.rc == st->l2.N200) {
1161 		FsmChangeState(fi, ST_L2_4);
1162 		test_and_clear_bit(FLG_T200_RUN, &st->l2.flag);
1163 		skb_queue_purge(&st->l2.i_queue);
1164 		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'G');
1165 		if (test_bit(FLG_LAPB, &st->l2.flag))
1166 			st->l2.l2l1(st, PH_DEACTIVATE | REQUEST, NULL);
1167 		st5_dl_release_l2l3(st);
1168 	} else {
1169 		st->l2.rc++;
1170 		FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 9);
1171 		send_uframe(st, (test_bit(FLG_MOD128, &st->l2.flag) ? SABME : SABM)
1172 			    | 0x10, CMD);
1173 	}
1174 }
1175 
1176 static void
l2_st6_tout_200(struct FsmInst * fi,int event,void * arg)1177 l2_st6_tout_200(struct FsmInst *fi, int event, void *arg)
1178 {
1179 	struct PStack *st = fi->userdata;
1180 
1181 	if (test_bit(FLG_LAPD, &st->l2.flag) &&
1182 		test_bit(FLG_DCHAN_BUSY, &st->l2.flag)) {
1183 		FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 9);
1184 	} else if (st->l2.rc == st->l2.N200) {
1185 		FsmChangeState(fi, ST_L2_4);
1186 		test_and_clear_bit(FLG_T200_RUN, &st->l2.flag);
1187 		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'H');
1188 		lapb_dl_release_l2l3(st, CONFIRM);
1189 	} else {
1190 		st->l2.rc++;
1191 		FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200,
1192 			    NULL, 9);
1193 		send_uframe(st, DISC | 0x10, CMD);
1194 	}
1195 }
1196 
1197 static void
l2_st7_tout_200(struct FsmInst * fi,int event,void * arg)1198 l2_st7_tout_200(struct FsmInst *fi, int event, void *arg)
1199 {
1200 	struct PStack *st = fi->userdata;
1201 
1202 	if (test_bit(FLG_LAPD, &st->l2.flag) &&
1203 		test_bit(FLG_DCHAN_BUSY, &st->l2.flag)) {
1204 		FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 9);
1205 		return;
1206 	}
1207 	test_and_clear_bit(FLG_T200_RUN, &st->l2.flag);
1208 	st->l2.rc = 0;
1209 	FsmChangeState(fi, ST_L2_8);
1210 
1211 	transmit_enquiry(st);
1212 	st->l2.rc++;
1213 }
1214 
1215 static void
l2_st8_tout_200(struct FsmInst * fi,int event,void * arg)1216 l2_st8_tout_200(struct FsmInst *fi, int event, void *arg)
1217 {
1218 	struct PStack *st = fi->userdata;
1219 
1220 	if (test_bit(FLG_LAPD, &st->l2.flag) &&
1221 		test_bit(FLG_DCHAN_BUSY, &st->l2.flag)) {
1222 		FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 9);
1223 		return;
1224 	}
1225 	test_and_clear_bit(FLG_T200_RUN, &st->l2.flag);
1226 	if (st->l2.rc == st->l2.N200) {
1227 		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'I');
1228 		establishlink(fi);
1229 		test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
1230 	} else {
1231 		transmit_enquiry(st);
1232 		st->l2.rc++;
1233 	}
1234 }
1235 
1236 static void
l2_st7_tout_203(struct FsmInst * fi,int event,void * arg)1237 l2_st7_tout_203(struct FsmInst *fi, int event, void *arg)
1238 {
1239 	struct PStack *st = fi->userdata;
1240 
1241 	if (test_bit(FLG_LAPD, &st->l2.flag) &&
1242 		test_bit(FLG_DCHAN_BUSY, &st->l2.flag)) {
1243 		FsmAddTimer(&st->l2.t203, st->l2.T203, EV_L2_T203, NULL, 9);
1244 		return;
1245 	}
1246 	FsmChangeState(fi, ST_L2_8);
1247 	transmit_enquiry(st);
1248 	st->l2.rc = 0;
1249 }
1250 
1251 static void
l2_pull_iqueue(struct FsmInst * fi,int event,void * arg)1252 l2_pull_iqueue(struct FsmInst *fi, int event, void *arg)
1253 {
1254 	struct PStack *st = fi->userdata;
1255 	struct sk_buff *skb, *oskb;
1256 	struct Layer2 *l2 = &st->l2;
1257 	u_char header[MAX_HEADER_LEN];
1258 	int i;
1259 	int unsigned p1;
1260 	long flags;
1261 
1262 	if (!cansend(st))
1263 		return;
1264 
1265 	skb = skb_dequeue(&l2->i_queue);
1266 	if (!skb)
1267 		return;
1268 
1269 	save_flags(flags);
1270 	cli();
1271 	if(test_bit(FLG_MOD128, &l2->flag))
1272 		p1 = (l2->vs - l2->va) % 128;
1273 	else
1274 		p1 = (l2->vs - l2->va) % 8;
1275 	p1 = (p1 + l2->sow) % l2->window;
1276 	if (l2->windowar[p1]) {
1277 		printk(KERN_WARNING "isdnl2 try overwrite ack queue entry %d\n",
1278 		       p1);
1279 		dev_kfree_skb(l2->windowar[p1]);
1280 	}
1281 	l2->windowar[p1] = skb_clone(skb, GFP_ATOMIC);
1282 
1283 	i = sethdraddr(&st->l2, header, CMD);
1284 
1285 	if (test_bit(FLG_MOD128, &l2->flag)) {
1286 		header[i++] = l2->vs << 1;
1287 		header[i++] = l2->vr << 1;
1288 		l2->vs = (l2->vs + 1) % 128;
1289 	} else {
1290 		header[i++] = (l2->vr << 5) | (l2->vs << 1);
1291 		l2->vs = (l2->vs + 1) % 8;
1292 	}
1293 	restore_flags(flags);
1294 
1295 	p1 = skb->data - skb->head;
1296 	if (p1 >= i)
1297 		memcpy(skb_push(skb, i), header, i);
1298 	else {
1299 		printk(KERN_WARNING
1300 		"isdl2 pull_iqueue skb header(%d/%d) too short\n", i, p1);
1301 		oskb = skb;
1302 		skb = alloc_skb(oskb->len + i, GFP_ATOMIC);
1303 		memcpy(skb_put(skb, i), header, i);
1304 		memcpy(skb_put(skb, oskb->len), oskb->data, oskb->len);
1305 		FreeSkb(oskb);
1306 	}
1307 	st->l2.l2l1(st, PH_PULL | INDICATION, skb);
1308 	test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag);
1309 	if (!test_and_set_bit(FLG_T200_RUN, &st->l2.flag)) {
1310 		FsmDelTimer(&st->l2.t203, 13);
1311 		FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 11);
1312 	}
1313 	if (skb_queue_len(&l2->i_queue) && cansend(st))
1314 		st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
1315 }
1316 
1317 static void
l2_st8_got_super(struct FsmInst * fi,int event,void * arg)1318 l2_st8_got_super(struct FsmInst *fi, int event, void *arg)
1319 {
1320 	struct PStack *st = fi->userdata;
1321 	struct sk_buff *skb = arg;
1322 	int PollFlag, rsp, rnr = 0;
1323 	unsigned int nr;
1324 	struct Layer2 *l2 = &st->l2;
1325 
1326 	rsp = *skb->data & 0x2;
1327 	if (test_bit(FLG_ORIG, &l2->flag))
1328 		rsp = !rsp;
1329 
1330 	skb_pull(skb, l2addrsize(l2));
1331 
1332 	if (IsRNR(skb->data, st)) {
1333 		set_peer_busy(l2);
1334 		rnr = 1;
1335 	} else
1336 		clear_peer_busy(l2);
1337 
1338 	if (test_bit(FLG_MOD128, &l2->flag)) {
1339 		PollFlag = (skb->data[1] & 0x1) == 0x1;
1340 		nr = skb->data[1] >> 1;
1341 	} else {
1342 		PollFlag = (skb->data[0] & 0x10);
1343 		nr = (skb->data[0] >> 5) & 0x7;
1344 	}
1345 	FreeSkb(skb);
1346 
1347 	if (rsp && PollFlag) {
1348 		if (legalnr(st, nr)) {
1349 			if (rnr) {
1350 				restart_t200(st, 15);
1351 			} else {
1352 				stop_t200(st, 16);
1353 				FsmAddTimer(&l2->t203, l2->T203,
1354 					    EV_L2_T203, NULL, 5);
1355 				setva(st, nr);
1356 			}
1357 			invoke_retransmission(st, nr);
1358 			FsmChangeState(fi, ST_L2_7);
1359 			if (skb_queue_len(&l2->i_queue) && cansend(st))
1360 				st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
1361 		} else
1362 			nrerrorrecovery(fi);
1363 	} else {
1364 		if (!rsp && PollFlag)
1365 			enquiry_response(st);
1366 		if (legalnr(st, nr)) {
1367 			setva(st, nr);
1368 		} else
1369 			nrerrorrecovery(fi);
1370 	}
1371 }
1372 
1373 static void
l2_got_FRMR(struct FsmInst * fi,int event,void * arg)1374 l2_got_FRMR(struct FsmInst *fi, int event, void *arg)
1375 {
1376 	struct PStack *st = fi->userdata;
1377 	struct sk_buff *skb = arg;
1378 
1379 	skb_pull(skb, l2addrsize(&st->l2) + 1);
1380 
1381 	if (!(skb->data[0] & 1) || ((skb->data[0] & 3) == 1) ||		/* I or S */
1382 	    (IsUA(skb->data) && (fi->state == ST_L2_7))) {
1383 		st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'K');
1384 		establishlink(fi);
1385 		test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
1386 	}
1387 	FreeSkb(skb);
1388 }
1389 
1390 static void
l2_st24_tei_remove(struct FsmInst * fi,int event,void * arg)1391 l2_st24_tei_remove(struct FsmInst *fi, int event, void *arg)
1392 {
1393 	struct PStack *st = fi->userdata;
1394 
1395 	skb_queue_purge(&st->l2.ui_queue);
1396 	st->l2.tei = -1;
1397 	FsmChangeState(fi, ST_L2_1);
1398 }
1399 
1400 static void
l2_st3_tei_remove(struct FsmInst * fi,int event,void * arg)1401 l2_st3_tei_remove(struct FsmInst *fi, int event, void *arg)
1402 {
1403 	struct PStack *st = fi->userdata;
1404 
1405 	skb_queue_purge(&st->l2.ui_queue);
1406 	st->l2.tei = -1;
1407 	st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
1408 	FsmChangeState(fi, ST_L2_1);
1409 }
1410 
1411 static void
l2_st5_tei_remove(struct FsmInst * fi,int event,void * arg)1412 l2_st5_tei_remove(struct FsmInst *fi, int event, void *arg)
1413 {
1414 	struct PStack *st = fi->userdata;
1415 
1416 	skb_queue_purge(&st->l2.i_queue);
1417 	skb_queue_purge(&st->l2.ui_queue);
1418 	freewin(st);
1419 	st->l2.tei = -1;
1420 	stop_t200(st, 17);
1421 	st5_dl_release_l2l3(st);
1422 	FsmChangeState(fi, ST_L2_1);
1423 }
1424 
1425 static void
l2_st6_tei_remove(struct FsmInst * fi,int event,void * arg)1426 l2_st6_tei_remove(struct FsmInst *fi, int event, void *arg)
1427 {
1428 	struct PStack *st = fi->userdata;
1429 
1430 	skb_queue_purge(&st->l2.ui_queue);
1431 	st->l2.tei = -1;
1432 	stop_t200(st, 18);
1433 	st->l2.l2l3(st, DL_RELEASE | CONFIRM, NULL);
1434 	FsmChangeState(fi, ST_L2_1);
1435 }
1436 
1437 static void
l2_tei_remove(struct FsmInst * fi,int event,void * arg)1438 l2_tei_remove(struct FsmInst *fi, int event, void *arg)
1439 {
1440 	struct PStack *st = fi->userdata;
1441 
1442 	skb_queue_purge(&st->l2.i_queue);
1443 	skb_queue_purge(&st->l2.ui_queue);
1444 	freewin(st);
1445 	st->l2.tei = -1;
1446 	stop_t200(st, 17);
1447 	FsmDelTimer(&st->l2.t203, 19);
1448 	st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
1449 	FsmChangeState(fi, ST_L2_1);
1450 }
1451 
1452 static void
l2_st14_persistant_da(struct FsmInst * fi,int event,void * arg)1453 l2_st14_persistant_da(struct FsmInst *fi, int event, void *arg)
1454 {
1455 	struct PStack *st = fi->userdata;
1456 
1457 	skb_queue_purge(&st->l2.i_queue);
1458 	skb_queue_purge(&st->l2.ui_queue);
1459 	if (test_and_clear_bit(FLG_ESTAB_PEND, &st->l2.flag))
1460 		st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
1461 }
1462 
1463 static void
l2_st5_persistant_da(struct FsmInst * fi,int event,void * arg)1464 l2_st5_persistant_da(struct FsmInst *fi, int event, void *arg)
1465 {
1466 	struct PStack *st = fi->userdata;
1467 
1468 	skb_queue_purge(&st->l2.i_queue);
1469 	skb_queue_purge(&st->l2.ui_queue);
1470 	freewin(st);
1471 	stop_t200(st, 19);
1472 	st5_dl_release_l2l3(st);
1473 	FsmChangeState(fi, ST_L2_4);
1474 }
1475 
1476 static void
l2_st6_persistant_da(struct FsmInst * fi,int event,void * arg)1477 l2_st6_persistant_da(struct FsmInst *fi, int event, void *arg)
1478 {
1479 	struct PStack *st = fi->userdata;
1480 
1481 	skb_queue_purge(&st->l2.ui_queue);
1482 	stop_t200(st, 20);
1483 	st->l2.l2l3(st, DL_RELEASE | CONFIRM, NULL);
1484 	FsmChangeState(fi, ST_L2_4);
1485 }
1486 
1487 static void
l2_persistant_da(struct FsmInst * fi,int event,void * arg)1488 l2_persistant_da(struct FsmInst *fi, int event, void *arg)
1489 {
1490 	struct PStack *st = fi->userdata;
1491 
1492 	skb_queue_purge(&st->l2.i_queue);
1493 	skb_queue_purge(&st->l2.ui_queue);
1494 	freewin(st);
1495 	stop_t200(st, 19);
1496 	FsmDelTimer(&st->l2.t203, 19);
1497 	st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
1498 	FsmChangeState(fi, ST_L2_4);
1499 }
1500 
1501 static void
l2_set_own_busy(struct FsmInst * fi,int event,void * arg)1502 l2_set_own_busy(struct FsmInst *fi, int event, void *arg)
1503 {
1504 	struct PStack *st = fi->userdata;
1505 
1506 	if(!test_and_set_bit(FLG_OWN_BUSY, &st->l2.flag)) {
1507 		enquiry_cr(st, RNR, RSP, 0);
1508 		test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag);
1509 	}
1510 }
1511 
1512 static void
l2_clear_own_busy(struct FsmInst * fi,int event,void * arg)1513 l2_clear_own_busy(struct FsmInst *fi, int event, void *arg)
1514 {
1515 	struct PStack *st = fi->userdata;
1516 
1517 	if(!test_and_clear_bit(FLG_OWN_BUSY, &st->l2.flag)) {
1518 		enquiry_cr(st, RR, RSP, 0);
1519 		test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag);
1520 	}
1521 }
1522 
1523 static void
l2_frame_error(struct FsmInst * fi,int event,void * arg)1524 l2_frame_error(struct FsmInst *fi, int event, void *arg)
1525 {
1526 	struct PStack *st = fi->userdata;
1527 
1528 	st->ma.layer(st, MDL_ERROR | INDICATION, arg);
1529 }
1530 
1531 static void
l2_frame_error_reest(struct FsmInst * fi,int event,void * arg)1532 l2_frame_error_reest(struct FsmInst *fi, int event, void *arg)
1533 {
1534 	struct PStack *st = fi->userdata;
1535 
1536 	st->ma.layer(st, MDL_ERROR | INDICATION, arg);
1537 	establishlink(fi);
1538 	test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
1539 }
1540 
1541 static struct FsmNode L2FnList[] __initdata =
1542 {
1543 	{ST_L2_1, EV_L2_DL_ESTABLISH_REQ, l2_mdl_assign},
1544 	{ST_L2_2, EV_L2_DL_ESTABLISH_REQ, l2_go_st3},
1545 	{ST_L2_4, EV_L2_DL_ESTABLISH_REQ, l2_establish},
1546 	{ST_L2_5, EV_L2_DL_ESTABLISH_REQ, l2_discard_i_setl3},
1547 	{ST_L2_7, EV_L2_DL_ESTABLISH_REQ, l2_l3_reestablish},
1548 	{ST_L2_8, EV_L2_DL_ESTABLISH_REQ, l2_l3_reestablish},
1549 	{ST_L2_4, EV_L2_DL_RELEASE_REQ, l2_release},
1550 	{ST_L2_5, EV_L2_DL_RELEASE_REQ, l2_pend_rel},
1551 	{ST_L2_7, EV_L2_DL_RELEASE_REQ, l2_disconnect},
1552 	{ST_L2_8, EV_L2_DL_RELEASE_REQ, l2_disconnect},
1553 	{ST_L2_5, EV_L2_DL_DATA, l2_feed_i_if_reest},
1554 	{ST_L2_7, EV_L2_DL_DATA, l2_feed_i_pull},
1555 	{ST_L2_8, EV_L2_DL_DATA, l2_feed_iqueue},
1556 	{ST_L2_1, EV_L2_DL_UNIT_DATA, l2_queue_ui_assign},
1557 	{ST_L2_2, EV_L2_DL_UNIT_DATA, l2_queue_ui},
1558 	{ST_L2_3, EV_L2_DL_UNIT_DATA, l2_queue_ui},
1559 	{ST_L2_4, EV_L2_DL_UNIT_DATA, l2_send_ui},
1560 	{ST_L2_5, EV_L2_DL_UNIT_DATA, l2_send_ui},
1561 	{ST_L2_6, EV_L2_DL_UNIT_DATA, l2_send_ui},
1562 	{ST_L2_7, EV_L2_DL_UNIT_DATA, l2_send_ui},
1563 	{ST_L2_8, EV_L2_DL_UNIT_DATA, l2_send_ui},
1564 	{ST_L2_1, EV_L2_MDL_ASSIGN, l2_got_tei},
1565 	{ST_L2_2, EV_L2_MDL_ASSIGN, l2_got_tei},
1566 	{ST_L2_3, EV_L2_MDL_ASSIGN, l2_got_tei},
1567 	{ST_L2_2, EV_L2_MDL_ERROR, l2_st24_tei_remove},
1568 	{ST_L2_3, EV_L2_MDL_ERROR, l2_st3_tei_remove},
1569 	{ST_L2_4, EV_L2_MDL_REMOVE, l2_st24_tei_remove},
1570 	{ST_L2_5, EV_L2_MDL_REMOVE, l2_st5_tei_remove},
1571 	{ST_L2_6, EV_L2_MDL_REMOVE, l2_st6_tei_remove},
1572 	{ST_L2_7, EV_L2_MDL_REMOVE, l2_tei_remove},
1573 	{ST_L2_8, EV_L2_MDL_REMOVE, l2_tei_remove},
1574 	{ST_L2_4, EV_L2_SABME, l2_start_multi},
1575 	{ST_L2_5, EV_L2_SABME, l2_send_UA},
1576 	{ST_L2_6, EV_L2_SABME, l2_send_DM},
1577 	{ST_L2_7, EV_L2_SABME, l2_restart_multi},
1578 	{ST_L2_8, EV_L2_SABME, l2_restart_multi},
1579 	{ST_L2_4, EV_L2_DISC, l2_send_DM},
1580 	{ST_L2_5, EV_L2_DISC, l2_send_DM},
1581 	{ST_L2_6, EV_L2_DISC, l2_send_UA},
1582 	{ST_L2_7, EV_L2_DISC, l2_stop_multi},
1583 	{ST_L2_8, EV_L2_DISC, l2_stop_multi},
1584 	{ST_L2_4, EV_L2_UA, l2_mdl_error_ua},
1585 	{ST_L2_5, EV_L2_UA, l2_connected},
1586 	{ST_L2_6, EV_L2_UA, l2_released},
1587 	{ST_L2_7, EV_L2_UA, l2_mdl_error_ua},
1588 	{ST_L2_8, EV_L2_UA, l2_mdl_error_ua},
1589 	{ST_L2_4, EV_L2_DM, l2_reestablish},
1590 	{ST_L2_5, EV_L2_DM, l2_st5_dm_release},
1591 	{ST_L2_6, EV_L2_DM, l2_st6_dm_release},
1592 	{ST_L2_7, EV_L2_DM, l2_mdl_error_dm},
1593 	{ST_L2_8, EV_L2_DM, l2_st8_mdl_error_dm},
1594 	{ST_L2_1, EV_L2_UI, l2_got_ui},
1595 	{ST_L2_2, EV_L2_UI, l2_got_ui},
1596 	{ST_L2_3, EV_L2_UI, l2_got_ui},
1597 	{ST_L2_4, EV_L2_UI, l2_got_ui},
1598 	{ST_L2_5, EV_L2_UI, l2_got_ui},
1599 	{ST_L2_6, EV_L2_UI, l2_got_ui},
1600 	{ST_L2_7, EV_L2_UI, l2_got_ui},
1601 	{ST_L2_8, EV_L2_UI, l2_got_ui},
1602 	{ST_L2_7, EV_L2_FRMR, l2_got_FRMR},
1603 	{ST_L2_8, EV_L2_FRMR, l2_got_FRMR},
1604 	{ST_L2_7, EV_L2_SUPER, l2_st7_got_super},
1605 	{ST_L2_8, EV_L2_SUPER, l2_st8_got_super},
1606 	{ST_L2_7, EV_L2_I, l2_got_iframe},
1607 	{ST_L2_8, EV_L2_I, l2_got_iframe},
1608 	{ST_L2_5, EV_L2_T200, l2_st5_tout_200},
1609 	{ST_L2_6, EV_L2_T200, l2_st6_tout_200},
1610 	{ST_L2_7, EV_L2_T200, l2_st7_tout_200},
1611 	{ST_L2_8, EV_L2_T200, l2_st8_tout_200},
1612 	{ST_L2_7, EV_L2_T203, l2_st7_tout_203},
1613 	{ST_L2_7, EV_L2_ACK_PULL, l2_pull_iqueue},
1614 	{ST_L2_7, EV_L2_SET_OWN_BUSY, l2_set_own_busy},
1615 	{ST_L2_8, EV_L2_SET_OWN_BUSY, l2_set_own_busy},
1616 	{ST_L2_7, EV_L2_CLEAR_OWN_BUSY, l2_clear_own_busy},
1617 	{ST_L2_8, EV_L2_CLEAR_OWN_BUSY, l2_clear_own_busy},
1618 	{ST_L2_4, EV_L2_FRAME_ERROR, l2_frame_error},
1619 	{ST_L2_5, EV_L2_FRAME_ERROR, l2_frame_error},
1620 	{ST_L2_6, EV_L2_FRAME_ERROR, l2_frame_error},
1621 	{ST_L2_7, EV_L2_FRAME_ERROR, l2_frame_error_reest},
1622 	{ST_L2_8, EV_L2_FRAME_ERROR, l2_frame_error_reest},
1623 	{ST_L2_1, EV_L1_DEACTIVATE, l2_st14_persistant_da},
1624 	{ST_L2_2, EV_L1_DEACTIVATE, l2_st24_tei_remove},
1625 	{ST_L2_3, EV_L1_DEACTIVATE, l2_st3_tei_remove},
1626 	{ST_L2_4, EV_L1_DEACTIVATE, l2_st14_persistant_da},
1627 	{ST_L2_5, EV_L1_DEACTIVATE, l2_st5_persistant_da},
1628 	{ST_L2_6, EV_L1_DEACTIVATE, l2_st6_persistant_da},
1629 	{ST_L2_7, EV_L1_DEACTIVATE, l2_persistant_da},
1630 	{ST_L2_8, EV_L1_DEACTIVATE, l2_persistant_da},
1631 };
1632 
1633 #define L2_FN_COUNT (sizeof(L2FnList)/sizeof(struct FsmNode))
1634 
1635 static void
isdnl2_l1l2(struct PStack * st,int pr,void * arg)1636 isdnl2_l1l2(struct PStack *st, int pr, void *arg)
1637 {
1638 	struct sk_buff *skb = arg;
1639 	u_char *datap;
1640 	int ret = 1, len;
1641 	int c = 0;
1642 
1643 	switch (pr) {
1644 		case (PH_DATA | INDICATION):
1645 			datap = skb->data;
1646 			len = l2addrsize(&st->l2);
1647 			if (skb->len > len)
1648 				datap += len;
1649 			else {
1650 				FsmEvent(&st->l2.l2m, EV_L2_FRAME_ERROR, (void *) 'N');
1651 				FreeSkb(skb);
1652 				return;
1653 			}
1654 			if (!(*datap & 1)) {	/* I-Frame */
1655 				if(!(c = iframe_error(st, skb)))
1656 					ret = FsmEvent(&st->l2.l2m, EV_L2_I, skb);
1657 			} else if (IsSFrame(datap, st)) {	/* S-Frame */
1658 				if(!(c = super_error(st, skb)))
1659 					ret = FsmEvent(&st->l2.l2m, EV_L2_SUPER, skb);
1660 			} else if (IsUI(datap)) {
1661 				if(!(c = UI_error(st, skb)))
1662 					ret = FsmEvent(&st->l2.l2m, EV_L2_UI, skb);
1663 			} else if (IsSABME(datap, st)) {
1664 				if(!(c = unnum_error(st, skb, CMD)))
1665 					ret = FsmEvent(&st->l2.l2m, EV_L2_SABME, skb);
1666 			} else if (IsUA(datap)) {
1667 				if(!(c = unnum_error(st, skb, RSP)))
1668 					ret = FsmEvent(&st->l2.l2m, EV_L2_UA, skb);
1669 			} else if (IsDISC(datap)) {
1670 				if(!(c = unnum_error(st, skb, CMD)))
1671 					ret = FsmEvent(&st->l2.l2m, EV_L2_DISC, skb);
1672 			} else if (IsDM(datap)) {
1673 				if(!(c = unnum_error(st, skb, RSP)))
1674 					ret = FsmEvent(&st->l2.l2m, EV_L2_DM, skb);
1675 			} else if (IsFRMR(datap)) {
1676 				if(!(c = FRMR_error(st,skb)))
1677 					ret = FsmEvent(&st->l2.l2m, EV_L2_FRMR, skb);
1678 			} else {
1679 				FsmEvent(&st->l2.l2m, EV_L2_FRAME_ERROR, (void *) 'L');
1680 				FreeSkb(skb);
1681 				ret = 0;
1682 			}
1683 			if(c) {
1684 				FreeSkb(skb);
1685 				FsmEvent(&st->l2.l2m, EV_L2_FRAME_ERROR, (void *)(long)c);
1686 				ret = 0;
1687 			}
1688 			if (ret)
1689 				FreeSkb(skb);
1690 			break;
1691 		case (PH_PULL | CONFIRM):
1692 			FsmEvent(&st->l2.l2m, EV_L2_ACK_PULL, arg);
1693 			break;
1694 		case (PH_PAUSE | INDICATION):
1695 			test_and_set_bit(FLG_DCHAN_BUSY, &st->l2.flag);
1696 			break;
1697 		case (PH_PAUSE | CONFIRM):
1698 			test_and_clear_bit(FLG_DCHAN_BUSY, &st->l2.flag);
1699 			break;
1700 		case (PH_ACTIVATE | CONFIRM):
1701 		case (PH_ACTIVATE | INDICATION):
1702 			test_and_set_bit(FLG_L1_ACTIV, &st->l2.flag);
1703 			if (test_and_clear_bit(FLG_ESTAB_PEND, &st->l2.flag))
1704 				FsmEvent(&st->l2.l2m, EV_L2_DL_ESTABLISH_REQ, arg);
1705 			break;
1706 		case (PH_DEACTIVATE | INDICATION):
1707 		case (PH_DEACTIVATE | CONFIRM):
1708 			test_and_clear_bit(FLG_L1_ACTIV, &st->l2.flag);
1709 			FsmEvent(&st->l2.l2m, EV_L1_DEACTIVATE, arg);
1710 			break;
1711 		default:
1712 			l2m_debug(&st->l2.l2m, "l2 unknown pr %04x", pr);
1713 			break;
1714 	}
1715 }
1716 
1717 static void
isdnl2_l3l2(struct PStack * st,int pr,void * arg)1718 isdnl2_l3l2(struct PStack *st, int pr, void *arg)
1719 {
1720 	switch (pr) {
1721 		case (DL_DATA | REQUEST):
1722 			if (FsmEvent(&st->l2.l2m, EV_L2_DL_DATA, arg)) {
1723 				dev_kfree_skb((struct sk_buff *) arg);
1724 			}
1725 			break;
1726 		case (DL_UNIT_DATA | REQUEST):
1727 			if (FsmEvent(&st->l2.l2m, EV_L2_DL_UNIT_DATA, arg)) {
1728 				dev_kfree_skb((struct sk_buff *) arg);
1729 			}
1730 			break;
1731 		case (DL_ESTABLISH | REQUEST):
1732 			if (test_bit(FLG_L1_ACTIV, &st->l2.flag)) {
1733 				if (test_bit(FLG_LAPD, &st->l2.flag) ||
1734 					test_bit(FLG_ORIG, &st->l2.flag)) {
1735 					FsmEvent(&st->l2.l2m, EV_L2_DL_ESTABLISH_REQ, arg);
1736 				}
1737 			} else {
1738 				if (test_bit(FLG_LAPD, &st->l2.flag) ||
1739 					test_bit(FLG_ORIG, &st->l2.flag)) {
1740 					test_and_set_bit(FLG_ESTAB_PEND, &st->l2.flag);
1741 				}
1742 				st->l2.l2l1(st, PH_ACTIVATE, NULL);
1743 			}
1744 			break;
1745 		case (DL_RELEASE | REQUEST):
1746 			if (test_bit(FLG_LAPB, &st->l2.flag)) {
1747 				st->l2.l2l1(st, PH_DEACTIVATE, NULL);
1748 			}
1749 			FsmEvent(&st->l2.l2m, EV_L2_DL_RELEASE_REQ, arg);
1750 			break;
1751 		case (MDL_ASSIGN | REQUEST):
1752 			FsmEvent(&st->l2.l2m, EV_L2_MDL_ASSIGN, arg);
1753 			break;
1754 		case (MDL_REMOVE | REQUEST):
1755 			FsmEvent(&st->l2.l2m, EV_L2_MDL_REMOVE, arg);
1756 			break;
1757 		case (MDL_ERROR | RESPONSE):
1758 			FsmEvent(&st->l2.l2m, EV_L2_MDL_ERROR, arg);
1759 			break;
1760 	}
1761 }
1762 
1763 void
releasestack_isdnl2(struct PStack * st)1764 releasestack_isdnl2(struct PStack *st)
1765 {
1766 	FsmDelTimer(&st->l2.t200, 21);
1767 	FsmDelTimer(&st->l2.t203, 16);
1768 	skb_queue_purge(&st->l2.i_queue);
1769 	skb_queue_purge(&st->l2.ui_queue);
1770 	ReleaseWin(&st->l2);
1771 }
1772 
1773 static void
l2m_debug(struct FsmInst * fi,char * fmt,...)1774 l2m_debug(struct FsmInst *fi, char *fmt, ...)
1775 {
1776 	va_list args;
1777 	struct PStack *st = fi->userdata;
1778 
1779 	va_start(args, fmt);
1780 	VHiSax_putstatus(st->l1.hardware, st->l2.debug_id, fmt, args);
1781 	va_end(args);
1782 }
1783 
1784 void
setstack_isdnl2(struct PStack * st,char * debug_id)1785 setstack_isdnl2(struct PStack *st, char *debug_id)
1786 {
1787 	st->l1.l1l2 = isdnl2_l1l2;
1788 	st->l3.l3l2 = isdnl2_l3l2;
1789 
1790 	skb_queue_head_init(&st->l2.i_queue);
1791 	skb_queue_head_init(&st->l2.ui_queue);
1792 	InitWin(&st->l2);
1793 	st->l2.debug = 0;
1794 
1795 	st->l2.l2m.fsm = &l2fsm;
1796 	if (test_bit(FLG_LAPB, &st->l2.flag))
1797 		st->l2.l2m.state = ST_L2_4;
1798 	else
1799 	st->l2.l2m.state = ST_L2_1;
1800 	st->l2.l2m.debug = 0;
1801 	st->l2.l2m.userdata = st;
1802 	st->l2.l2m.userint = 0;
1803 	st->l2.l2m.printdebug = l2m_debug;
1804 	strcpy(st->l2.debug_id, debug_id);
1805 
1806 	FsmInitTimer(&st->l2.l2m, &st->l2.t200);
1807 	FsmInitTimer(&st->l2.l2m, &st->l2.t203);
1808 }
1809 
1810 static void
transl2_l3l2(struct PStack * st,int pr,void * arg)1811 transl2_l3l2(struct PStack *st, int pr, void *arg)
1812 {
1813 	switch (pr) {
1814 		case (DL_DATA | REQUEST):
1815 		case (DL_UNIT_DATA | REQUEST):
1816 			st->l2.l2l1(st, PH_DATA | REQUEST, arg);
1817 			break;
1818 		case (DL_ESTABLISH | REQUEST):
1819 			st->l2.l2l1(st, PH_ACTIVATE | REQUEST, NULL);
1820 			break;
1821 		case (DL_RELEASE | REQUEST):
1822 			st->l2.l2l1(st, PH_DEACTIVATE | REQUEST, NULL);
1823 			break;
1824 	}
1825 }
1826 
1827 void
setstack_transl2(struct PStack * st)1828 setstack_transl2(struct PStack *st)
1829 {
1830 	st->l3.l3l2 = transl2_l3l2;
1831 }
1832 
1833 void
releasestack_transl2(struct PStack * st)1834 releasestack_transl2(struct PStack *st)
1835 {
1836 }
1837 
1838 int __init
Isdnl2New(void)1839 Isdnl2New(void)
1840 {
1841 	l2fsm.state_count = L2_STATE_COUNT;
1842 	l2fsm.event_count = L2_EVENT_COUNT;
1843 	l2fsm.strEvent = strL2Event;
1844 	l2fsm.strState = strL2State;
1845 	return FsmNew(&l2fsm, L2FnList, L2_FN_COUNT);
1846 }
1847 
1848 void
Isdnl2Free(void)1849 Isdnl2Free(void)
1850 {
1851 	FsmFree(&l2fsm);
1852 }
1853