[PATCH] aic7xxx_osm build fix
[cris-mirror.git] / drivers / isdn / hisax / isdnl2.c
blobd311b5fbf895014f1b529e0f4cb67fc309918609
1 /* $Id: isdnl2.c,v 2.30.2.4 2004/02/11 13:21:34 keil Exp $
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.
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 "hisax.h"
20 #include "isdnl2.h"
22 const char *l2_revision = "$Revision: 2.30.2.4 $";
24 static void l2m_debug(struct FsmInst *fi, char *fmt, ...);
26 static struct Fsm l2fsm;
28 enum {
29 ST_L2_1,
30 ST_L2_2,
31 ST_L2_3,
32 ST_L2_4,
33 ST_L2_5,
34 ST_L2_6,
35 ST_L2_7,
36 ST_L2_8,
39 #define L2_STATE_COUNT (ST_L2_8+1)
41 static char *strL2State[] =
43 "ST_L2_1",
44 "ST_L2_2",
45 "ST_L2_3",
46 "ST_L2_4",
47 "ST_L2_5",
48 "ST_L2_6",
49 "ST_L2_7",
50 "ST_L2_8",
53 enum {
54 EV_L2_UI,
55 EV_L2_SABME,
56 EV_L2_DISC,
57 EV_L2_DM,
58 EV_L2_UA,
59 EV_L2_FRMR,
60 EV_L2_SUPER,
61 EV_L2_I,
62 EV_L2_DL_DATA,
63 EV_L2_ACK_PULL,
64 EV_L2_DL_UNIT_DATA,
65 EV_L2_DL_ESTABLISH_REQ,
66 EV_L2_DL_RELEASE_REQ,
67 EV_L2_MDL_ASSIGN,
68 EV_L2_MDL_REMOVE,
69 EV_L2_MDL_ERROR,
70 EV_L1_DEACTIVATE,
71 EV_L2_T200,
72 EV_L2_T203,
73 EV_L2_SET_OWN_BUSY,
74 EV_L2_CLEAR_OWN_BUSY,
75 EV_L2_FRAME_ERROR,
78 #define L2_EVENT_COUNT (EV_L2_FRAME_ERROR+1)
80 static char *strL2Event[] =
82 "EV_L2_UI",
83 "EV_L2_SABME",
84 "EV_L2_DISC",
85 "EV_L2_DM",
86 "EV_L2_UA",
87 "EV_L2_FRMR",
88 "EV_L2_SUPER",
89 "EV_L2_I",
90 "EV_L2_DL_DATA",
91 "EV_L2_ACK_PULL",
92 "EV_L2_DL_UNIT_DATA",
93 "EV_L2_DL_ESTABLISH_REQ",
94 "EV_L2_DL_RELEASE_REQ",
95 "EV_L2_MDL_ASSIGN",
96 "EV_L2_MDL_REMOVE",
97 "EV_L2_MDL_ERROR",
98 "EV_L1_DEACTIVATE",
99 "EV_L2_T200",
100 "EV_L2_T203",
101 "EV_L2_SET_OWN_BUSY",
102 "EV_L2_CLEAR_OWN_BUSY",
103 "EV_L2_FRAME_ERROR",
106 static int l2addrsize(struct Layer2 *l2);
108 static void
109 set_peer_busy(struct Layer2 *l2) {
110 test_and_set_bit(FLG_PEER_BUSY, &l2->flag);
111 if (skb_queue_len(&l2->i_queue) || skb_queue_len(&l2->ui_queue))
112 test_and_set_bit(FLG_L2BLOCK, &l2->flag);
115 static void
116 clear_peer_busy(struct Layer2 *l2) {
117 if (test_and_clear_bit(FLG_PEER_BUSY, &l2->flag))
118 test_and_clear_bit(FLG_L2BLOCK, &l2->flag);
121 static void
122 InitWin(struct Layer2 *l2)
124 int i;
126 for (i = 0; i < MAX_WINDOW; i++)
127 l2->windowar[i] = NULL;
130 static int
131 freewin1(struct Layer2 *l2)
133 int i, cnt = 0;
135 for (i = 0; i < MAX_WINDOW; i++) {
136 if (l2->windowar[i]) {
137 cnt++;
138 dev_kfree_skb(l2->windowar[i]);
139 l2->windowar[i] = NULL;
142 return cnt;
145 inline void
146 freewin(struct PStack *st)
148 freewin1(&st->l2);
151 static void
152 ReleaseWin(struct Layer2 *l2)
154 int cnt;
156 if((cnt = freewin1(l2)))
157 printk(KERN_WARNING "isdl2 freed %d skbuffs in release\n", cnt);
160 inline unsigned int
161 cansend(struct PStack *st)
163 unsigned int p1;
165 if(test_bit(FLG_MOD128, &st->l2.flag))
166 p1 = (st->l2.vs - st->l2.va) % 128;
167 else
168 p1 = (st->l2.vs - st->l2.va) % 8;
169 return ((p1 < st->l2.window) && !test_bit(FLG_PEER_BUSY, &st->l2.flag));
172 inline void
173 clear_exception(struct Layer2 *l2)
175 test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
176 test_and_clear_bit(FLG_REJEXC, &l2->flag);
177 test_and_clear_bit(FLG_OWN_BUSY, &l2->flag);
178 clear_peer_busy(l2);
181 inline int
182 l2headersize(struct Layer2 *l2, int ui)
184 return (((test_bit(FLG_MOD128, &l2->flag) && (!ui)) ? 2 : 1) +
185 (test_bit(FLG_LAPD, &l2->flag) ? 2 : 1));
188 inline int
189 l2addrsize(struct Layer2 *l2)
191 return (test_bit(FLG_LAPD, &l2->flag) ? 2 : 1);
194 static int
195 sethdraddr(struct Layer2 *l2, u_char * header, int rsp)
197 u_char *ptr = header;
198 int crbit = rsp;
200 if (test_bit(FLG_LAPD, &l2->flag)) {
201 *ptr++ = (l2->sap << 2) | (rsp ? 2 : 0);
202 *ptr++ = (l2->tei << 1) | 1;
203 return (2);
204 } else {
205 if (test_bit(FLG_ORIG, &l2->flag))
206 crbit = !crbit;
207 if (crbit)
208 *ptr++ = 1;
209 else
210 *ptr++ = 3;
211 return (1);
215 inline static void
216 enqueue_super(struct PStack *st,
217 struct sk_buff *skb)
219 if (test_bit(FLG_LAPB, &st->l2.flag))
220 st->l1.bcs->tx_cnt += skb->len;
221 st->l2.l2l1(st, PH_DATA | REQUEST, skb);
224 #define enqueue_ui(a, b) enqueue_super(a, b)
226 inline int
227 IsUI(u_char * data)
229 return ((data[0] & 0xef) == UI);
232 inline int
233 IsUA(u_char * data)
235 return ((data[0] & 0xef) == UA);
238 inline int
239 IsDM(u_char * data)
241 return ((data[0] & 0xef) == DM);
244 inline int
245 IsDISC(u_char * data)
247 return ((data[0] & 0xef) == DISC);
250 inline int
251 IsRR(u_char * data, struct PStack *st)
253 if (test_bit(FLG_MOD128, &st->l2.flag))
254 return (data[0] == RR);
255 else
256 return ((data[0] & 0xf) == 1);
259 inline int
260 IsSFrame(u_char * data, struct PStack *st)
262 register u_char d = *data;
264 if (!test_bit(FLG_MOD128, &st->l2.flag))
265 d &= 0xf;
266 return(((d & 0xf3) == 1) && ((d & 0x0c) != 0x0c));
269 inline int
270 IsSABME(u_char * data, struct PStack *st)
272 u_char d = data[0] & ~0x10;
274 return (test_bit(FLG_MOD128, &st->l2.flag) ? d == SABME : d == SABM);
277 inline int
278 IsREJ(u_char * data, struct PStack *st)
280 return (test_bit(FLG_MOD128, &st->l2.flag) ? data[0] == REJ : (data[0] & 0xf) == REJ);
283 inline int
284 IsFRMR(u_char * data)
286 return ((data[0] & 0xef) == FRMR);
289 inline int
290 IsRNR(u_char * data, struct PStack *st)
292 return (test_bit(FLG_MOD128, &st->l2.flag) ? data[0] == RNR : (data[0] & 0xf) == RNR);
296 iframe_error(struct PStack *st, struct sk_buff *skb)
298 int i = l2addrsize(&st->l2) + (test_bit(FLG_MOD128, &st->l2.flag) ? 2 : 1);
299 int rsp = *skb->data & 0x2;
301 if (test_bit(FLG_ORIG, &st->l2.flag))
302 rsp = !rsp;
304 if (rsp)
305 return 'L';
308 if (skb->len < i)
309 return 'N';
311 if ((skb->len - i) > st->l2.maxlen)
312 return 'O';
315 return 0;
319 super_error(struct PStack *st, struct sk_buff *skb)
321 if (skb->len != l2addrsize(&st->l2) +
322 (test_bit(FLG_MOD128, &st->l2.flag) ? 2 : 1))
323 return 'N';
325 return 0;
329 unnum_error(struct PStack *st, struct sk_buff *skb, int wantrsp)
331 int rsp = (*skb->data & 0x2) >> 1;
332 if (test_bit(FLG_ORIG, &st->l2.flag))
333 rsp = !rsp;
335 if (rsp != wantrsp)
336 return 'L';
338 if (skb->len != l2addrsize(&st->l2) + 1)
339 return 'N';
341 return 0;
345 UI_error(struct PStack *st, struct sk_buff *skb)
347 int rsp = *skb->data & 0x2;
348 if (test_bit(FLG_ORIG, &st->l2.flag))
349 rsp = !rsp;
351 if (rsp)
352 return 'L';
354 if (skb->len > st->l2.maxlen + l2addrsize(&st->l2) + 1)
355 return 'O';
357 return 0;
361 FRMR_error(struct PStack *st, struct sk_buff *skb)
363 int headers = l2addrsize(&st->l2) + 1;
364 u_char *datap = skb->data + headers;
365 int rsp = *skb->data & 0x2;
367 if (test_bit(FLG_ORIG, &st->l2.flag))
368 rsp = !rsp;
370 if (!rsp)
371 return 'L';
373 if (test_bit(FLG_MOD128, &st->l2.flag)) {
374 if (skb->len < headers + 5)
375 return 'N';
376 else
377 l2m_debug(&st->l2.l2m, "FRMR information %2x %2x %2x %2x %2x",
378 datap[0], datap[1], datap[2],
379 datap[3], datap[4]);
380 } else {
381 if (skb->len < headers + 3)
382 return 'N';
383 else
384 l2m_debug(&st->l2.l2m, "FRMR information %2x %2x %2x",
385 datap[0], datap[1], datap[2]);
388 return 0;
391 static unsigned int
392 legalnr(struct PStack *st, unsigned int nr)
394 struct Layer2 *l2 = &st->l2;
396 if(test_bit(FLG_MOD128, &l2->flag))
397 return ((nr - l2->va) % 128) <= ((l2->vs - l2->va) % 128);
398 else
399 return ((nr - l2->va) % 8) <= ((l2->vs - l2->va) % 8);
402 static void
403 setva(struct PStack *st, unsigned int nr)
405 struct Layer2 *l2 = &st->l2;
406 int len;
407 u_long flags;
409 spin_lock_irqsave(&l2->lock, flags);
410 while (l2->va != nr) {
411 (l2->va)++;
412 if(test_bit(FLG_MOD128, &l2->flag))
413 l2->va %= 128;
414 else
415 l2->va %= 8;
416 len = l2->windowar[l2->sow]->len;
417 if (PACKET_NOACK == l2->windowar[l2->sow]->pkt_type)
418 len = -1;
419 dev_kfree_skb(l2->windowar[l2->sow]);
420 l2->windowar[l2->sow] = NULL;
421 l2->sow = (l2->sow + 1) % l2->window;
422 spin_unlock_irqrestore(&l2->lock, flags);
423 if (test_bit(FLG_LLI_L2WAKEUP, &st->lli.flag) && (len >=0))
424 lli_writewakeup(st, len);
425 spin_lock_irqsave(&l2->lock, flags);
427 spin_unlock_irqrestore(&l2->lock, flags);
430 static void
431 send_uframe(struct PStack *st, u_char cmd, u_char cr)
433 struct sk_buff *skb;
434 u_char tmp[MAX_HEADER_LEN];
435 int i;
437 i = sethdraddr(&st->l2, tmp, cr);
438 tmp[i++] = cmd;
439 if (!(skb = alloc_skb(i, GFP_ATOMIC))) {
440 printk(KERN_WARNING "isdl2 can't alloc sbbuff for send_uframe\n");
441 return;
443 memcpy(skb_put(skb, i), tmp, i);
444 enqueue_super(st, skb);
447 inline u_char
448 get_PollFlag(struct PStack * st, struct sk_buff * skb)
450 return (skb->data[l2addrsize(&(st->l2))] & 0x10);
453 inline void
454 FreeSkb(struct sk_buff *skb)
456 dev_kfree_skb(skb);
460 inline u_char
461 get_PollFlagFree(struct PStack *st, struct sk_buff *skb)
463 u_char PF;
465 PF = get_PollFlag(st, skb);
466 FreeSkb(skb);
467 return (PF);
470 inline void
471 start_t200(struct PStack *st, int i)
473 FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, i);
474 test_and_set_bit(FLG_T200_RUN, &st->l2.flag);
477 inline void
478 restart_t200(struct PStack *st, int i)
480 FsmRestartTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, i);
481 test_and_set_bit(FLG_T200_RUN, &st->l2.flag);
484 inline void
485 stop_t200(struct PStack *st, int i)
487 if(test_and_clear_bit(FLG_T200_RUN, &st->l2.flag))
488 FsmDelTimer(&st->l2.t200, i);
491 inline void
492 st5_dl_release_l2l3(struct PStack *st)
494 int pr;
496 if(test_and_clear_bit(FLG_PEND_REL, &st->l2.flag))
497 pr = DL_RELEASE | CONFIRM;
498 else
499 pr = DL_RELEASE | INDICATION;
501 st->l2.l2l3(st, pr, NULL);
504 inline void
505 lapb_dl_release_l2l3(struct PStack *st, int f)
507 if (test_bit(FLG_LAPB, &st->l2.flag))
508 st->l2.l2l1(st, PH_DEACTIVATE | REQUEST, NULL);
509 st->l2.l2l3(st, DL_RELEASE | f, NULL);
512 static void
513 establishlink(struct FsmInst *fi)
515 struct PStack *st = fi->userdata;
516 u_char cmd;
518 clear_exception(&st->l2);
519 st->l2.rc = 0;
520 cmd = (test_bit(FLG_MOD128, &st->l2.flag) ? SABME : SABM) | 0x10;
521 send_uframe(st, cmd, CMD);
522 FsmDelTimer(&st->l2.t203, 1);
523 restart_t200(st, 1);
524 test_and_clear_bit(FLG_PEND_REL, &st->l2.flag);
525 freewin(st);
526 FsmChangeState(fi, ST_L2_5);
529 static void
530 l2_mdl_error_ua(struct FsmInst *fi, int event, void *arg)
532 struct sk_buff *skb = arg;
533 struct PStack *st = fi->userdata;
535 if (get_PollFlagFree(st, skb))
536 st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'C');
537 else
538 st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'D');
541 static void
542 l2_mdl_error_dm(struct FsmInst *fi, int event, void *arg)
544 struct sk_buff *skb = arg;
545 struct PStack *st = fi->userdata;
547 if (get_PollFlagFree(st, skb))
548 st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'B');
549 else {
550 st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'E');
551 establishlink(fi);
552 test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
556 static void
557 l2_st8_mdl_error_dm(struct FsmInst *fi, int event, void *arg)
559 struct sk_buff *skb = arg;
560 struct PStack *st = fi->userdata;
562 if (get_PollFlagFree(st, skb))
563 st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'B');
564 else {
565 st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'E');
567 establishlink(fi);
568 test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
571 static void
572 l2_go_st3(struct FsmInst *fi, int event, void *arg)
574 FsmChangeState(fi, ST_L2_3);
577 static void
578 l2_mdl_assign(struct FsmInst *fi, int event, void *arg)
580 struct PStack *st = fi->userdata;
582 FsmChangeState(fi, ST_L2_3);
583 st->l2.l2tei(st, MDL_ASSIGN | INDICATION, NULL);
586 static void
587 l2_queue_ui_assign(struct FsmInst *fi, int event, void *arg)
589 struct PStack *st = fi->userdata;
590 struct sk_buff *skb = arg;
592 skb_queue_tail(&st->l2.ui_queue, skb);
593 FsmChangeState(fi, ST_L2_2);
594 st->l2.l2tei(st, MDL_ASSIGN | INDICATION, NULL);
597 static void
598 l2_queue_ui(struct FsmInst *fi, int event, void *arg)
600 struct PStack *st = fi->userdata;
601 struct sk_buff *skb = arg;
603 skb_queue_tail(&st->l2.ui_queue, skb);
606 static void
607 tx_ui(struct PStack *st)
609 struct sk_buff *skb;
610 u_char header[MAX_HEADER_LEN];
611 int i;
613 i = sethdraddr(&(st->l2), header, CMD);
614 header[i++] = UI;
615 while ((skb = skb_dequeue(&st->l2.ui_queue))) {
616 memcpy(skb_push(skb, i), header, i);
617 enqueue_ui(st, skb);
621 static void
622 l2_send_ui(struct FsmInst *fi, int event, void *arg)
624 struct PStack *st = fi->userdata;
625 struct sk_buff *skb = arg;
627 skb_queue_tail(&st->l2.ui_queue, skb);
628 tx_ui(st);
631 static void
632 l2_got_ui(struct FsmInst *fi, int event, void *arg)
634 struct PStack *st = fi->userdata;
635 struct sk_buff *skb = arg;
637 skb_pull(skb, l2headersize(&st->l2, 1));
638 st->l2.l2l3(st, DL_UNIT_DATA | INDICATION, skb);
639 /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
640 * in states 1-3 for broadcast
646 static void
647 l2_establish(struct FsmInst *fi, int event, void *arg)
649 struct PStack *st = fi->userdata;
651 establishlink(fi);
652 test_and_set_bit(FLG_L3_INIT, &st->l2.flag);
655 static void
656 l2_discard_i_setl3(struct FsmInst *fi, int event, void *arg)
658 struct PStack *st = fi->userdata;
660 skb_queue_purge(&st->l2.i_queue);
661 test_and_set_bit(FLG_L3_INIT, &st->l2.flag);
662 test_and_clear_bit(FLG_PEND_REL, &st->l2.flag);
665 static void
666 l2_l3_reestablish(struct FsmInst *fi, int event, void *arg)
668 struct PStack *st = fi->userdata;
670 skb_queue_purge(&st->l2.i_queue);
671 establishlink(fi);
672 test_and_set_bit(FLG_L3_INIT, &st->l2.flag);
675 static void
676 l2_release(struct FsmInst *fi, int event, void *arg)
678 struct PStack *st = fi->userdata;
680 st->l2.l2l3(st, DL_RELEASE | CONFIRM, NULL);
683 static void
684 l2_pend_rel(struct FsmInst *fi, int event, void *arg)
686 struct PStack *st = fi->userdata;
688 test_and_set_bit(FLG_PEND_REL, &st->l2.flag);
691 static void
692 l2_disconnect(struct FsmInst *fi, int event, void *arg)
694 struct PStack *st = fi->userdata;
696 skb_queue_purge(&st->l2.i_queue);
697 freewin(st);
698 FsmChangeState(fi, ST_L2_6);
699 st->l2.rc = 0;
700 send_uframe(st, DISC | 0x10, CMD);
701 FsmDelTimer(&st->l2.t203, 1);
702 restart_t200(st, 2);
705 static void
706 l2_start_multi(struct FsmInst *fi, int event, void *arg)
708 struct PStack *st = fi->userdata;
709 struct sk_buff *skb = arg;
711 send_uframe(st, UA | get_PollFlagFree(st, skb), RSP);
713 clear_exception(&st->l2);
714 st->l2.vs = 0;
715 st->l2.va = 0;
716 st->l2.vr = 0;
717 st->l2.sow = 0;
718 FsmChangeState(fi, ST_L2_7);
719 FsmAddTimer(&st->l2.t203, st->l2.T203, EV_L2_T203, NULL, 3);
721 st->l2.l2l3(st, DL_ESTABLISH | INDICATION, NULL);
724 static void
725 l2_send_UA(struct FsmInst *fi, int event, void *arg)
727 struct PStack *st = fi->userdata;
728 struct sk_buff *skb = arg;
730 send_uframe(st, UA | get_PollFlagFree(st, skb), RSP);
733 static void
734 l2_send_DM(struct FsmInst *fi, int event, void *arg)
736 struct PStack *st = fi->userdata;
737 struct sk_buff *skb = arg;
739 send_uframe(st, DM | get_PollFlagFree(st, skb), RSP);
742 static void
743 l2_restart_multi(struct FsmInst *fi, int event, void *arg)
745 struct PStack *st = fi->userdata;
746 struct sk_buff *skb = arg;
747 int est = 0, state;
749 state = fi->state;
751 send_uframe(st, UA | get_PollFlagFree(st, skb), RSP);
753 st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'F');
755 if (st->l2.vs != st->l2.va) {
756 skb_queue_purge(&st->l2.i_queue);
757 est = 1;
760 clear_exception(&st->l2);
761 st->l2.vs = 0;
762 st->l2.va = 0;
763 st->l2.vr = 0;
764 st->l2.sow = 0;
765 FsmChangeState(fi, ST_L2_7);
766 stop_t200(st, 3);
767 FsmRestartTimer(&st->l2.t203, st->l2.T203, EV_L2_T203, NULL, 3);
769 if (est)
770 st->l2.l2l3(st, DL_ESTABLISH | INDICATION, NULL);
772 if ((ST_L2_7==state) || (ST_L2_8 == state))
773 if (skb_queue_len(&st->l2.i_queue) && cansend(st))
774 st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
777 static void
778 l2_stop_multi(struct FsmInst *fi, int event, void *arg)
780 struct PStack *st = fi->userdata;
781 struct sk_buff *skb = arg;
783 FsmChangeState(fi, ST_L2_4);
784 FsmDelTimer(&st->l2.t203, 3);
785 stop_t200(st, 4);
787 send_uframe(st, UA | get_PollFlagFree(st, skb), RSP);
789 skb_queue_purge(&st->l2.i_queue);
790 freewin(st);
791 lapb_dl_release_l2l3(st, INDICATION);
794 static void
795 l2_connected(struct FsmInst *fi, int event, void *arg)
797 struct PStack *st = fi->userdata;
798 struct sk_buff *skb = arg;
799 int pr=-1;
801 if (!get_PollFlag(st, skb)) {
802 l2_mdl_error_ua(fi, event, arg);
803 return;
805 FreeSkb(skb);
807 if (test_and_clear_bit(FLG_PEND_REL, &st->l2.flag))
808 l2_disconnect(fi, event, arg);
810 if (test_and_clear_bit(FLG_L3_INIT, &st->l2.flag)) {
811 pr = DL_ESTABLISH | CONFIRM;
812 } else if (st->l2.vs != st->l2.va) {
813 skb_queue_purge(&st->l2.i_queue);
814 pr = DL_ESTABLISH | INDICATION;
817 stop_t200(st, 5);
819 st->l2.vr = 0;
820 st->l2.vs = 0;
821 st->l2.va = 0;
822 st->l2.sow = 0;
823 FsmChangeState(fi, ST_L2_7);
824 FsmAddTimer(&st->l2.t203, st->l2.T203, EV_L2_T203, NULL, 4);
826 if (pr != -1)
827 st->l2.l2l3(st, pr, NULL);
829 if (skb_queue_len(&st->l2.i_queue) && cansend(st))
830 st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
833 static void
834 l2_released(struct FsmInst *fi, int event, void *arg)
836 struct PStack *st = fi->userdata;
837 struct sk_buff *skb = arg;
839 if (!get_PollFlag(st, skb)) {
840 l2_mdl_error_ua(fi, event, arg);
841 return;
843 FreeSkb(skb);
845 stop_t200(st, 6);
846 lapb_dl_release_l2l3(st, CONFIRM);
847 FsmChangeState(fi, ST_L2_4);
850 static void
851 l2_reestablish(struct FsmInst *fi, int event, void *arg)
853 struct PStack *st = fi->userdata;
854 struct sk_buff *skb = arg;
856 if (!get_PollFlagFree(st, skb)) {
857 establishlink(fi);
858 test_and_set_bit(FLG_L3_INIT, &st->l2.flag);
862 static void
863 l2_st5_dm_release(struct FsmInst *fi, int event, void *arg)
865 struct PStack *st = fi->userdata;
866 struct sk_buff *skb = arg;
868 if (get_PollFlagFree(st, skb)) {
869 stop_t200(st, 7);
870 if (!test_bit(FLG_L3_INIT, &st->l2.flag))
871 skb_queue_purge(&st->l2.i_queue);
872 if (test_bit(FLG_LAPB, &st->l2.flag))
873 st->l2.l2l1(st, PH_DEACTIVATE | REQUEST, NULL);
874 st5_dl_release_l2l3(st);
875 FsmChangeState(fi, ST_L2_4);
879 static void
880 l2_st6_dm_release(struct FsmInst *fi, int event, void *arg)
882 struct PStack *st = fi->userdata;
883 struct sk_buff *skb = arg;
885 if (get_PollFlagFree(st, skb)) {
886 stop_t200(st, 8);
887 lapb_dl_release_l2l3(st, CONFIRM);
888 FsmChangeState(fi, ST_L2_4);
892 inline void
893 enquiry_cr(struct PStack *st, u_char typ, u_char cr, u_char pf)
895 struct sk_buff *skb;
896 struct Layer2 *l2;
897 u_char tmp[MAX_HEADER_LEN];
898 int i;
900 l2 = &st->l2;
901 i = sethdraddr(l2, tmp, cr);
902 if (test_bit(FLG_MOD128, &l2->flag)) {
903 tmp[i++] = typ;
904 tmp[i++] = (l2->vr << 1) | (pf ? 1 : 0);
905 } else
906 tmp[i++] = (l2->vr << 5) | typ | (pf ? 0x10 : 0);
907 if (!(skb = alloc_skb(i, GFP_ATOMIC))) {
908 printk(KERN_WARNING "isdl2 can't alloc sbbuff for enquiry_cr\n");
909 return;
911 memcpy(skb_put(skb, i), tmp, i);
912 enqueue_super(st, skb);
915 inline void
916 enquiry_response(struct PStack *st)
918 if (test_bit(FLG_OWN_BUSY, &st->l2.flag))
919 enquiry_cr(st, RNR, RSP, 1);
920 else
921 enquiry_cr(st, RR, RSP, 1);
922 test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag);
925 inline void
926 transmit_enquiry(struct PStack *st)
928 if (test_bit(FLG_OWN_BUSY, &st->l2.flag))
929 enquiry_cr(st, RNR, CMD, 1);
930 else
931 enquiry_cr(st, RR, CMD, 1);
932 test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag);
933 start_t200(st, 9);
937 static void
938 nrerrorrecovery(struct FsmInst *fi)
940 struct PStack *st = fi->userdata;
942 st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'J');
943 establishlink(fi);
944 test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
947 static void
948 invoke_retransmission(struct PStack *st, unsigned int nr)
950 struct Layer2 *l2 = &st->l2;
951 u_int p1;
952 u_long flags;
954 spin_lock_irqsave(&l2->lock, flags);
955 if (l2->vs != nr) {
956 while (l2->vs != nr) {
957 (l2->vs)--;
958 if(test_bit(FLG_MOD128, &l2->flag)) {
959 l2->vs %= 128;
960 p1 = (l2->vs - l2->va) % 128;
961 } else {
962 l2->vs %= 8;
963 p1 = (l2->vs - l2->va) % 8;
965 p1 = (p1 + l2->sow) % l2->window;
966 if (test_bit(FLG_LAPB, &l2->flag))
967 st->l1.bcs->tx_cnt += l2->windowar[p1]->len + l2headersize(l2, 0);
968 skb_queue_head(&l2->i_queue, l2->windowar[p1]);
969 l2->windowar[p1] = NULL;
971 spin_unlock_irqrestore(&l2->lock, flags);
972 st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
973 return;
975 spin_unlock_irqrestore(&l2->lock, flags);
978 static void
979 l2_st7_got_super(struct FsmInst *fi, int event, void *arg)
981 struct PStack *st = fi->userdata;
982 struct sk_buff *skb = arg;
983 int PollFlag, rsp, typ = RR;
984 unsigned int nr;
985 struct Layer2 *l2 = &st->l2;
987 rsp = *skb->data & 0x2;
988 if (test_bit(FLG_ORIG, &l2->flag))
989 rsp = !rsp;
991 skb_pull(skb, l2addrsize(l2));
992 if (IsRNR(skb->data, st)) {
993 set_peer_busy(l2);
994 typ = RNR;
995 } else
996 clear_peer_busy(l2);
997 if (IsREJ(skb->data, st))
998 typ = REJ;
1000 if (test_bit(FLG_MOD128, &l2->flag)) {
1001 PollFlag = (skb->data[1] & 0x1) == 0x1;
1002 nr = skb->data[1] >> 1;
1003 } else {
1004 PollFlag = (skb->data[0] & 0x10);
1005 nr = (skb->data[0] >> 5) & 0x7;
1007 FreeSkb(skb);
1009 if (PollFlag) {
1010 if (rsp)
1011 st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'A');
1012 else
1013 enquiry_response(st);
1015 if (legalnr(st, nr)) {
1016 if (typ == REJ) {
1017 setva(st, nr);
1018 invoke_retransmission(st, nr);
1019 stop_t200(st, 10);
1020 if (FsmAddTimer(&st->l2.t203, st->l2.T203,
1021 EV_L2_T203, NULL, 6))
1022 l2m_debug(&st->l2.l2m, "Restart T203 ST7 REJ");
1023 } else if ((nr == l2->vs) && (typ == RR)) {
1024 setva(st, nr);
1025 stop_t200(st, 11);
1026 FsmRestartTimer(&st->l2.t203, st->l2.T203,
1027 EV_L2_T203, NULL, 7);
1028 } else if ((l2->va != nr) || (typ == RNR)) {
1029 setva(st, nr);
1030 if(typ != RR) FsmDelTimer(&st->l2.t203, 9);
1031 restart_t200(st, 12);
1033 if (skb_queue_len(&st->l2.i_queue) && (typ == RR))
1034 st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
1035 } else
1036 nrerrorrecovery(fi);
1039 static void
1040 l2_feed_i_if_reest(struct FsmInst *fi, int event, void *arg)
1042 struct PStack *st = fi->userdata;
1043 struct sk_buff *skb = arg;
1045 if (test_bit(FLG_LAPB, &st->l2.flag))
1046 st->l1.bcs->tx_cnt += skb->len + l2headersize(&st->l2, 0);
1047 if (!test_bit(FLG_L3_INIT, &st->l2.flag))
1048 skb_queue_tail(&st->l2.i_queue, skb);
1049 else
1050 FreeSkb(skb);
1053 static void
1054 l2_feed_i_pull(struct FsmInst *fi, int event, void *arg)
1056 struct PStack *st = fi->userdata;
1057 struct sk_buff *skb = arg;
1059 if (test_bit(FLG_LAPB, &st->l2.flag))
1060 st->l1.bcs->tx_cnt += skb->len + l2headersize(&st->l2, 0);
1061 skb_queue_tail(&st->l2.i_queue, skb);
1062 st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
1065 static void
1066 l2_feed_iqueue(struct FsmInst *fi, int event, void *arg)
1068 struct PStack *st = fi->userdata;
1069 struct sk_buff *skb = arg;
1071 if (test_bit(FLG_LAPB, &st->l2.flag))
1072 st->l1.bcs->tx_cnt += skb->len + l2headersize(&st->l2, 0);
1073 skb_queue_tail(&st->l2.i_queue, skb);
1076 static void
1077 l2_got_iframe(struct FsmInst *fi, int event, void *arg)
1079 struct PStack *st = fi->userdata;
1080 struct sk_buff *skb = arg;
1081 struct Layer2 *l2 = &(st->l2);
1082 int PollFlag, ns, i;
1083 unsigned int nr;
1085 i = l2addrsize(l2);
1086 if (test_bit(FLG_MOD128, &l2->flag)) {
1087 PollFlag = ((skb->data[i + 1] & 0x1) == 0x1);
1088 ns = skb->data[i] >> 1;
1089 nr = (skb->data[i + 1] >> 1) & 0x7f;
1090 } else {
1091 PollFlag = (skb->data[i] & 0x10);
1092 ns = (skb->data[i] >> 1) & 0x7;
1093 nr = (skb->data[i] >> 5) & 0x7;
1095 if (test_bit(FLG_OWN_BUSY, &l2->flag)) {
1096 FreeSkb(skb);
1097 if(PollFlag) enquiry_response(st);
1098 } else if (l2->vr == ns) {
1099 (l2->vr)++;
1100 if(test_bit(FLG_MOD128, &l2->flag))
1101 l2->vr %= 128;
1102 else
1103 l2->vr %= 8;
1104 test_and_clear_bit(FLG_REJEXC, &l2->flag);
1106 if (PollFlag)
1107 enquiry_response(st);
1108 else
1109 test_and_set_bit(FLG_ACK_PEND, &l2->flag);
1110 skb_pull(skb, l2headersize(l2, 0));
1111 st->l2.l2l3(st, DL_DATA | INDICATION, skb);
1112 } else {
1113 /* n(s)!=v(r) */
1114 FreeSkb(skb);
1115 if (test_and_set_bit(FLG_REJEXC, &l2->flag)) {
1116 if (PollFlag)
1117 enquiry_response(st);
1118 } else {
1119 enquiry_cr(st, REJ, RSP, PollFlag);
1120 test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
1124 if (legalnr(st, nr)) {
1125 if (!test_bit(FLG_PEER_BUSY, &st->l2.flag) && (fi->state == ST_L2_7)) {
1126 if (nr == st->l2.vs) {
1127 stop_t200(st, 13);
1128 FsmRestartTimer(&st->l2.t203, st->l2.T203,
1129 EV_L2_T203, NULL, 7);
1130 } else if (nr != st->l2.va)
1131 restart_t200(st, 14);
1133 setva(st, nr);
1134 } else {
1135 nrerrorrecovery(fi);
1136 return;
1139 if (skb_queue_len(&st->l2.i_queue) && (fi->state == ST_L2_7))
1140 st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
1141 if (test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag))
1142 enquiry_cr(st, RR, RSP, 0);
1145 static void
1146 l2_got_tei(struct FsmInst *fi, int event, void *arg)
1148 struct PStack *st = fi->userdata;
1150 st->l2.tei = (long) arg;
1152 if (fi->state == ST_L2_3) {
1153 establishlink(fi);
1154 test_and_set_bit(FLG_L3_INIT, &st->l2.flag);
1155 } else
1156 FsmChangeState(fi, ST_L2_4);
1157 if (skb_queue_len(&st->l2.ui_queue))
1158 tx_ui(st);
1161 static void
1162 l2_st5_tout_200(struct FsmInst *fi, int event, void *arg)
1164 struct PStack *st = fi->userdata;
1166 if (test_bit(FLG_LAPD, &st->l2.flag) &&
1167 test_bit(FLG_DCHAN_BUSY, &st->l2.flag)) {
1168 FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 9);
1169 } else if (st->l2.rc == st->l2.N200) {
1170 FsmChangeState(fi, ST_L2_4);
1171 test_and_clear_bit(FLG_T200_RUN, &st->l2.flag);
1172 skb_queue_purge(&st->l2.i_queue);
1173 st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'G');
1174 if (test_bit(FLG_LAPB, &st->l2.flag))
1175 st->l2.l2l1(st, PH_DEACTIVATE | REQUEST, NULL);
1176 st5_dl_release_l2l3(st);
1177 } else {
1178 st->l2.rc++;
1179 FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 9);
1180 send_uframe(st, (test_bit(FLG_MOD128, &st->l2.flag) ? SABME : SABM)
1181 | 0x10, CMD);
1185 static void
1186 l2_st6_tout_200(struct FsmInst *fi, int event, void *arg)
1188 struct PStack *st = fi->userdata;
1190 if (test_bit(FLG_LAPD, &st->l2.flag) &&
1191 test_bit(FLG_DCHAN_BUSY, &st->l2.flag)) {
1192 FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 9);
1193 } else if (st->l2.rc == st->l2.N200) {
1194 FsmChangeState(fi, ST_L2_4);
1195 test_and_clear_bit(FLG_T200_RUN, &st->l2.flag);
1196 st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'H');
1197 lapb_dl_release_l2l3(st, CONFIRM);
1198 } else {
1199 st->l2.rc++;
1200 FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200,
1201 NULL, 9);
1202 send_uframe(st, DISC | 0x10, CMD);
1206 static void
1207 l2_st7_tout_200(struct FsmInst *fi, int event, void *arg)
1209 struct PStack *st = fi->userdata;
1211 if (test_bit(FLG_LAPD, &st->l2.flag) &&
1212 test_bit(FLG_DCHAN_BUSY, &st->l2.flag)) {
1213 FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 9);
1214 return;
1216 test_and_clear_bit(FLG_T200_RUN, &st->l2.flag);
1217 st->l2.rc = 0;
1218 FsmChangeState(fi, ST_L2_8);
1220 transmit_enquiry(st);
1221 st->l2.rc++;
1224 static void
1225 l2_st8_tout_200(struct FsmInst *fi, int event, void *arg)
1227 struct PStack *st = fi->userdata;
1229 if (test_bit(FLG_LAPD, &st->l2.flag) &&
1230 test_bit(FLG_DCHAN_BUSY, &st->l2.flag)) {
1231 FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 9);
1232 return;
1234 test_and_clear_bit(FLG_T200_RUN, &st->l2.flag);
1235 if (st->l2.rc == st->l2.N200) {
1236 st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'I');
1237 establishlink(fi);
1238 test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
1239 } else {
1240 transmit_enquiry(st);
1241 st->l2.rc++;
1245 static void
1246 l2_st7_tout_203(struct FsmInst *fi, int event, void *arg)
1248 struct PStack *st = fi->userdata;
1250 if (test_bit(FLG_LAPD, &st->l2.flag) &&
1251 test_bit(FLG_DCHAN_BUSY, &st->l2.flag)) {
1252 FsmAddTimer(&st->l2.t203, st->l2.T203, EV_L2_T203, NULL, 9);
1253 return;
1255 FsmChangeState(fi, ST_L2_8);
1256 transmit_enquiry(st);
1257 st->l2.rc = 0;
1260 static void
1261 l2_pull_iqueue(struct FsmInst *fi, int event, void *arg)
1263 struct PStack *st = fi->userdata;
1264 struct sk_buff *skb, *oskb;
1265 struct Layer2 *l2 = &st->l2;
1266 u_char header[MAX_HEADER_LEN];
1267 int i;
1268 int unsigned p1;
1269 u_long flags;
1271 if (!cansend(st))
1272 return;
1274 skb = skb_dequeue(&l2->i_queue);
1275 if (!skb)
1276 return;
1278 spin_lock_irqsave(&l2->lock, flags);
1279 if(test_bit(FLG_MOD128, &l2->flag))
1280 p1 = (l2->vs - l2->va) % 128;
1281 else
1282 p1 = (l2->vs - l2->va) % 8;
1283 p1 = (p1 + l2->sow) % l2->window;
1284 if (l2->windowar[p1]) {
1285 printk(KERN_WARNING "isdnl2 try overwrite ack queue entry %d\n",
1286 p1);
1287 dev_kfree_skb(l2->windowar[p1]);
1289 l2->windowar[p1] = skb_clone(skb, GFP_ATOMIC);
1291 i = sethdraddr(&st->l2, header, CMD);
1293 if (test_bit(FLG_MOD128, &l2->flag)) {
1294 header[i++] = l2->vs << 1;
1295 header[i++] = l2->vr << 1;
1296 l2->vs = (l2->vs + 1) % 128;
1297 } else {
1298 header[i++] = (l2->vr << 5) | (l2->vs << 1);
1299 l2->vs = (l2->vs + 1) % 8;
1301 spin_unlock_irqrestore(&l2->lock, flags);
1302 p1 = skb->data - skb->head;
1303 if (p1 >= i)
1304 memcpy(skb_push(skb, i), header, i);
1305 else {
1306 printk(KERN_WARNING
1307 "isdl2 pull_iqueue skb header(%d/%d) too short\n", i, p1);
1308 oskb = skb;
1309 skb = alloc_skb(oskb->len + i, GFP_ATOMIC);
1310 memcpy(skb_put(skb, i), header, i);
1311 memcpy(skb_put(skb, oskb->len), oskb->data, oskb->len);
1312 FreeSkb(oskb);
1314 st->l2.l2l1(st, PH_PULL | INDICATION, skb);
1315 test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag);
1316 if (!test_and_set_bit(FLG_T200_RUN, &st->l2.flag)) {
1317 FsmDelTimer(&st->l2.t203, 13);
1318 FsmAddTimer(&st->l2.t200, st->l2.T200, EV_L2_T200, NULL, 11);
1320 if (skb_queue_len(&l2->i_queue) && cansend(st))
1321 st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
1324 static void
1325 l2_st8_got_super(struct FsmInst *fi, int event, void *arg)
1327 struct PStack *st = fi->userdata;
1328 struct sk_buff *skb = arg;
1329 int PollFlag, rsp, rnr = 0;
1330 unsigned int nr;
1331 struct Layer2 *l2 = &st->l2;
1333 rsp = *skb->data & 0x2;
1334 if (test_bit(FLG_ORIG, &l2->flag))
1335 rsp = !rsp;
1337 skb_pull(skb, l2addrsize(l2));
1339 if (IsRNR(skb->data, st)) {
1340 set_peer_busy(l2);
1341 rnr = 1;
1342 } else
1343 clear_peer_busy(l2);
1345 if (test_bit(FLG_MOD128, &l2->flag)) {
1346 PollFlag = (skb->data[1] & 0x1) == 0x1;
1347 nr = skb->data[1] >> 1;
1348 } else {
1349 PollFlag = (skb->data[0] & 0x10);
1350 nr = (skb->data[0] >> 5) & 0x7;
1352 FreeSkb(skb);
1354 if (rsp && PollFlag) {
1355 if (legalnr(st, nr)) {
1356 if (rnr) {
1357 restart_t200(st, 15);
1358 } else {
1359 stop_t200(st, 16);
1360 FsmAddTimer(&l2->t203, l2->T203,
1361 EV_L2_T203, NULL, 5);
1362 setva(st, nr);
1364 invoke_retransmission(st, nr);
1365 FsmChangeState(fi, ST_L2_7);
1366 if (skb_queue_len(&l2->i_queue) && cansend(st))
1367 st->l2.l2l1(st, PH_PULL | REQUEST, NULL);
1368 } else
1369 nrerrorrecovery(fi);
1370 } else {
1371 if (!rsp && PollFlag)
1372 enquiry_response(st);
1373 if (legalnr(st, nr)) {
1374 setva(st, nr);
1375 } else
1376 nrerrorrecovery(fi);
1380 static void
1381 l2_got_FRMR(struct FsmInst *fi, int event, void *arg)
1383 struct PStack *st = fi->userdata;
1384 struct sk_buff *skb = arg;
1386 skb_pull(skb, l2addrsize(&st->l2) + 1);
1388 if (!(skb->data[0] & 1) || ((skb->data[0] & 3) == 1) || /* I or S */
1389 (IsUA(skb->data) && (fi->state == ST_L2_7))) {
1390 st->ma.layer(st, MDL_ERROR | INDICATION, (void *) 'K');
1391 establishlink(fi);
1392 test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
1394 FreeSkb(skb);
1397 static void
1398 l2_st24_tei_remove(struct FsmInst *fi, int event, void *arg)
1400 struct PStack *st = fi->userdata;
1402 skb_queue_purge(&st->l2.ui_queue);
1403 st->l2.tei = -1;
1404 FsmChangeState(fi, ST_L2_1);
1407 static void
1408 l2_st3_tei_remove(struct FsmInst *fi, int event, void *arg)
1410 struct PStack *st = fi->userdata;
1412 skb_queue_purge(&st->l2.ui_queue);
1413 st->l2.tei = -1;
1414 st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
1415 FsmChangeState(fi, ST_L2_1);
1418 static void
1419 l2_st5_tei_remove(struct FsmInst *fi, int event, void *arg)
1421 struct PStack *st = fi->userdata;
1423 skb_queue_purge(&st->l2.i_queue);
1424 skb_queue_purge(&st->l2.ui_queue);
1425 freewin(st);
1426 st->l2.tei = -1;
1427 stop_t200(st, 17);
1428 st5_dl_release_l2l3(st);
1429 FsmChangeState(fi, ST_L2_1);
1432 static void
1433 l2_st6_tei_remove(struct FsmInst *fi, int event, void *arg)
1435 struct PStack *st = fi->userdata;
1437 skb_queue_purge(&st->l2.ui_queue);
1438 st->l2.tei = -1;
1439 stop_t200(st, 18);
1440 st->l2.l2l3(st, DL_RELEASE | CONFIRM, NULL);
1441 FsmChangeState(fi, ST_L2_1);
1444 static void
1445 l2_tei_remove(struct FsmInst *fi, int event, void *arg)
1447 struct PStack *st = fi->userdata;
1449 skb_queue_purge(&st->l2.i_queue);
1450 skb_queue_purge(&st->l2.ui_queue);
1451 freewin(st);
1452 st->l2.tei = -1;
1453 stop_t200(st, 17);
1454 FsmDelTimer(&st->l2.t203, 19);
1455 st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
1456 FsmChangeState(fi, ST_L2_1);
1459 static void
1460 l2_st14_persistant_da(struct FsmInst *fi, int event, void *arg)
1462 struct PStack *st = fi->userdata;
1464 skb_queue_purge(&st->l2.i_queue);
1465 skb_queue_purge(&st->l2.ui_queue);
1466 if (test_and_clear_bit(FLG_ESTAB_PEND, &st->l2.flag))
1467 st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
1470 static void
1471 l2_st5_persistant_da(struct FsmInst *fi, int event, void *arg)
1473 struct PStack *st = fi->userdata;
1475 skb_queue_purge(&st->l2.i_queue);
1476 skb_queue_purge(&st->l2.ui_queue);
1477 freewin(st);
1478 stop_t200(st, 19);
1479 st5_dl_release_l2l3(st);
1480 FsmChangeState(fi, ST_L2_4);
1483 static void
1484 l2_st6_persistant_da(struct FsmInst *fi, int event, void *arg)
1486 struct PStack *st = fi->userdata;
1488 skb_queue_purge(&st->l2.ui_queue);
1489 stop_t200(st, 20);
1490 st->l2.l2l3(st, DL_RELEASE | CONFIRM, NULL);
1491 FsmChangeState(fi, ST_L2_4);
1494 static void
1495 l2_persistant_da(struct FsmInst *fi, int event, void *arg)
1497 struct PStack *st = fi->userdata;
1499 skb_queue_purge(&st->l2.i_queue);
1500 skb_queue_purge(&st->l2.ui_queue);
1501 freewin(st);
1502 stop_t200(st, 19);
1503 FsmDelTimer(&st->l2.t203, 19);
1504 st->l2.l2l3(st, DL_RELEASE | INDICATION, NULL);
1505 FsmChangeState(fi, ST_L2_4);
1508 static void
1509 l2_set_own_busy(struct FsmInst *fi, int event, void *arg)
1511 struct PStack *st = fi->userdata;
1513 if(!test_and_set_bit(FLG_OWN_BUSY, &st->l2.flag)) {
1514 enquiry_cr(st, RNR, RSP, 0);
1515 test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag);
1519 static void
1520 l2_clear_own_busy(struct FsmInst *fi, int event, void *arg)
1522 struct PStack *st = fi->userdata;
1524 if(!test_and_clear_bit(FLG_OWN_BUSY, &st->l2.flag)) {
1525 enquiry_cr(st, RR, RSP, 0);
1526 test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag);
1530 static void
1531 l2_frame_error(struct FsmInst *fi, int event, void *arg)
1533 struct PStack *st = fi->userdata;
1535 st->ma.layer(st, MDL_ERROR | INDICATION, arg);
1538 static void
1539 l2_frame_error_reest(struct FsmInst *fi, int event, void *arg)
1541 struct PStack *st = fi->userdata;
1543 st->ma.layer(st, MDL_ERROR | INDICATION, arg);
1544 establishlink(fi);
1545 test_and_clear_bit(FLG_L3_INIT, &st->l2.flag);
1548 static struct FsmNode L2FnList[] __initdata =
1550 {ST_L2_1, EV_L2_DL_ESTABLISH_REQ, l2_mdl_assign},
1551 {ST_L2_2, EV_L2_DL_ESTABLISH_REQ, l2_go_st3},
1552 {ST_L2_4, EV_L2_DL_ESTABLISH_REQ, l2_establish},
1553 {ST_L2_5, EV_L2_DL_ESTABLISH_REQ, l2_discard_i_setl3},
1554 {ST_L2_7, EV_L2_DL_ESTABLISH_REQ, l2_l3_reestablish},
1555 {ST_L2_8, EV_L2_DL_ESTABLISH_REQ, l2_l3_reestablish},
1556 {ST_L2_4, EV_L2_DL_RELEASE_REQ, l2_release},
1557 {ST_L2_5, EV_L2_DL_RELEASE_REQ, l2_pend_rel},
1558 {ST_L2_7, EV_L2_DL_RELEASE_REQ, l2_disconnect},
1559 {ST_L2_8, EV_L2_DL_RELEASE_REQ, l2_disconnect},
1560 {ST_L2_5, EV_L2_DL_DATA, l2_feed_i_if_reest},
1561 {ST_L2_7, EV_L2_DL_DATA, l2_feed_i_pull},
1562 {ST_L2_8, EV_L2_DL_DATA, l2_feed_iqueue},
1563 {ST_L2_1, EV_L2_DL_UNIT_DATA, l2_queue_ui_assign},
1564 {ST_L2_2, EV_L2_DL_UNIT_DATA, l2_queue_ui},
1565 {ST_L2_3, EV_L2_DL_UNIT_DATA, l2_queue_ui},
1566 {ST_L2_4, EV_L2_DL_UNIT_DATA, l2_send_ui},
1567 {ST_L2_5, EV_L2_DL_UNIT_DATA, l2_send_ui},
1568 {ST_L2_6, EV_L2_DL_UNIT_DATA, l2_send_ui},
1569 {ST_L2_7, EV_L2_DL_UNIT_DATA, l2_send_ui},
1570 {ST_L2_8, EV_L2_DL_UNIT_DATA, l2_send_ui},
1571 {ST_L2_1, EV_L2_MDL_ASSIGN, l2_got_tei},
1572 {ST_L2_2, EV_L2_MDL_ASSIGN, l2_got_tei},
1573 {ST_L2_3, EV_L2_MDL_ASSIGN, l2_got_tei},
1574 {ST_L2_2, EV_L2_MDL_ERROR, l2_st24_tei_remove},
1575 {ST_L2_3, EV_L2_MDL_ERROR, l2_st3_tei_remove},
1576 {ST_L2_4, EV_L2_MDL_REMOVE, l2_st24_tei_remove},
1577 {ST_L2_5, EV_L2_MDL_REMOVE, l2_st5_tei_remove},
1578 {ST_L2_6, EV_L2_MDL_REMOVE, l2_st6_tei_remove},
1579 {ST_L2_7, EV_L2_MDL_REMOVE, l2_tei_remove},
1580 {ST_L2_8, EV_L2_MDL_REMOVE, l2_tei_remove},
1581 {ST_L2_4, EV_L2_SABME, l2_start_multi},
1582 {ST_L2_5, EV_L2_SABME, l2_send_UA},
1583 {ST_L2_6, EV_L2_SABME, l2_send_DM},
1584 {ST_L2_7, EV_L2_SABME, l2_restart_multi},
1585 {ST_L2_8, EV_L2_SABME, l2_restart_multi},
1586 {ST_L2_4, EV_L2_DISC, l2_send_DM},
1587 {ST_L2_5, EV_L2_DISC, l2_send_DM},
1588 {ST_L2_6, EV_L2_DISC, l2_send_UA},
1589 {ST_L2_7, EV_L2_DISC, l2_stop_multi},
1590 {ST_L2_8, EV_L2_DISC, l2_stop_multi},
1591 {ST_L2_4, EV_L2_UA, l2_mdl_error_ua},
1592 {ST_L2_5, EV_L2_UA, l2_connected},
1593 {ST_L2_6, EV_L2_UA, l2_released},
1594 {ST_L2_7, EV_L2_UA, l2_mdl_error_ua},
1595 {ST_L2_8, EV_L2_UA, l2_mdl_error_ua},
1596 {ST_L2_4, EV_L2_DM, l2_reestablish},
1597 {ST_L2_5, EV_L2_DM, l2_st5_dm_release},
1598 {ST_L2_6, EV_L2_DM, l2_st6_dm_release},
1599 {ST_L2_7, EV_L2_DM, l2_mdl_error_dm},
1600 {ST_L2_8, EV_L2_DM, l2_st8_mdl_error_dm},
1601 {ST_L2_1, EV_L2_UI, l2_got_ui},
1602 {ST_L2_2, EV_L2_UI, l2_got_ui},
1603 {ST_L2_3, EV_L2_UI, l2_got_ui},
1604 {ST_L2_4, EV_L2_UI, l2_got_ui},
1605 {ST_L2_5, EV_L2_UI, l2_got_ui},
1606 {ST_L2_6, EV_L2_UI, l2_got_ui},
1607 {ST_L2_7, EV_L2_UI, l2_got_ui},
1608 {ST_L2_8, EV_L2_UI, l2_got_ui},
1609 {ST_L2_7, EV_L2_FRMR, l2_got_FRMR},
1610 {ST_L2_8, EV_L2_FRMR, l2_got_FRMR},
1611 {ST_L2_7, EV_L2_SUPER, l2_st7_got_super},
1612 {ST_L2_8, EV_L2_SUPER, l2_st8_got_super},
1613 {ST_L2_7, EV_L2_I, l2_got_iframe},
1614 {ST_L2_8, EV_L2_I, l2_got_iframe},
1615 {ST_L2_5, EV_L2_T200, l2_st5_tout_200},
1616 {ST_L2_6, EV_L2_T200, l2_st6_tout_200},
1617 {ST_L2_7, EV_L2_T200, l2_st7_tout_200},
1618 {ST_L2_8, EV_L2_T200, l2_st8_tout_200},
1619 {ST_L2_7, EV_L2_T203, l2_st7_tout_203},
1620 {ST_L2_7, EV_L2_ACK_PULL, l2_pull_iqueue},
1621 {ST_L2_7, EV_L2_SET_OWN_BUSY, l2_set_own_busy},
1622 {ST_L2_8, EV_L2_SET_OWN_BUSY, l2_set_own_busy},
1623 {ST_L2_7, EV_L2_CLEAR_OWN_BUSY, l2_clear_own_busy},
1624 {ST_L2_8, EV_L2_CLEAR_OWN_BUSY, l2_clear_own_busy},
1625 {ST_L2_4, EV_L2_FRAME_ERROR, l2_frame_error},
1626 {ST_L2_5, EV_L2_FRAME_ERROR, l2_frame_error},
1627 {ST_L2_6, EV_L2_FRAME_ERROR, l2_frame_error},
1628 {ST_L2_7, EV_L2_FRAME_ERROR, l2_frame_error_reest},
1629 {ST_L2_8, EV_L2_FRAME_ERROR, l2_frame_error_reest},
1630 {ST_L2_1, EV_L1_DEACTIVATE, l2_st14_persistant_da},
1631 {ST_L2_2, EV_L1_DEACTIVATE, l2_st24_tei_remove},
1632 {ST_L2_3, EV_L1_DEACTIVATE, l2_st3_tei_remove},
1633 {ST_L2_4, EV_L1_DEACTIVATE, l2_st14_persistant_da},
1634 {ST_L2_5, EV_L1_DEACTIVATE, l2_st5_persistant_da},
1635 {ST_L2_6, EV_L1_DEACTIVATE, l2_st6_persistant_da},
1636 {ST_L2_7, EV_L1_DEACTIVATE, l2_persistant_da},
1637 {ST_L2_8, EV_L1_DEACTIVATE, l2_persistant_da},
1640 #define L2_FN_COUNT (sizeof(L2FnList)/sizeof(struct FsmNode))
1642 static void
1643 isdnl2_l1l2(struct PStack *st, int pr, void *arg)
1645 struct sk_buff *skb = arg;
1646 u_char *datap;
1647 int ret = 1, len;
1648 int c = 0;
1650 switch (pr) {
1651 case (PH_DATA | INDICATION):
1652 datap = skb->data;
1653 len = l2addrsize(&st->l2);
1654 if (skb->len > len)
1655 datap += len;
1656 else {
1657 FsmEvent(&st->l2.l2m, EV_L2_FRAME_ERROR, (void *) 'N');
1658 FreeSkb(skb);
1659 return;
1661 if (!(*datap & 1)) { /* I-Frame */
1662 if(!(c = iframe_error(st, skb)))
1663 ret = FsmEvent(&st->l2.l2m, EV_L2_I, skb);
1664 } else if (IsSFrame(datap, st)) { /* S-Frame */
1665 if(!(c = super_error(st, skb)))
1666 ret = FsmEvent(&st->l2.l2m, EV_L2_SUPER, skb);
1667 } else if (IsUI(datap)) {
1668 if(!(c = UI_error(st, skb)))
1669 ret = FsmEvent(&st->l2.l2m, EV_L2_UI, skb);
1670 } else if (IsSABME(datap, st)) {
1671 if(!(c = unnum_error(st, skb, CMD)))
1672 ret = FsmEvent(&st->l2.l2m, EV_L2_SABME, skb);
1673 } else if (IsUA(datap)) {
1674 if(!(c = unnum_error(st, skb, RSP)))
1675 ret = FsmEvent(&st->l2.l2m, EV_L2_UA, skb);
1676 } else if (IsDISC(datap)) {
1677 if(!(c = unnum_error(st, skb, CMD)))
1678 ret = FsmEvent(&st->l2.l2m, EV_L2_DISC, skb);
1679 } else if (IsDM(datap)) {
1680 if(!(c = unnum_error(st, skb, RSP)))
1681 ret = FsmEvent(&st->l2.l2m, EV_L2_DM, skb);
1682 } else if (IsFRMR(datap)) {
1683 if(!(c = FRMR_error(st,skb)))
1684 ret = FsmEvent(&st->l2.l2m, EV_L2_FRMR, skb);
1685 } else {
1686 FsmEvent(&st->l2.l2m, EV_L2_FRAME_ERROR, (void *) 'L');
1687 FreeSkb(skb);
1688 ret = 0;
1690 if(c) {
1691 FreeSkb(skb);
1692 FsmEvent(&st->l2.l2m, EV_L2_FRAME_ERROR, (void *)(long)c);
1693 ret = 0;
1695 if (ret)
1696 FreeSkb(skb);
1697 break;
1698 case (PH_PULL | CONFIRM):
1699 FsmEvent(&st->l2.l2m, EV_L2_ACK_PULL, arg);
1700 break;
1701 case (PH_PAUSE | INDICATION):
1702 test_and_set_bit(FLG_DCHAN_BUSY, &st->l2.flag);
1703 break;
1704 case (PH_PAUSE | CONFIRM):
1705 test_and_clear_bit(FLG_DCHAN_BUSY, &st->l2.flag);
1706 break;
1707 case (PH_ACTIVATE | CONFIRM):
1708 case (PH_ACTIVATE | INDICATION):
1709 test_and_set_bit(FLG_L1_ACTIV, &st->l2.flag);
1710 if (test_and_clear_bit(FLG_ESTAB_PEND, &st->l2.flag))
1711 FsmEvent(&st->l2.l2m, EV_L2_DL_ESTABLISH_REQ, arg);
1712 break;
1713 case (PH_DEACTIVATE | INDICATION):
1714 case (PH_DEACTIVATE | CONFIRM):
1715 test_and_clear_bit(FLG_L1_ACTIV, &st->l2.flag);
1716 FsmEvent(&st->l2.l2m, EV_L1_DEACTIVATE, arg);
1717 break;
1718 default:
1719 l2m_debug(&st->l2.l2m, "l2 unknown pr %04x", pr);
1720 break;
1724 static void
1725 isdnl2_l3l2(struct PStack *st, int pr, void *arg)
1727 switch (pr) {
1728 case (DL_DATA | REQUEST):
1729 if (FsmEvent(&st->l2.l2m, EV_L2_DL_DATA, arg)) {
1730 dev_kfree_skb((struct sk_buff *) arg);
1732 break;
1733 case (DL_UNIT_DATA | REQUEST):
1734 if (FsmEvent(&st->l2.l2m, EV_L2_DL_UNIT_DATA, arg)) {
1735 dev_kfree_skb((struct sk_buff *) arg);
1737 break;
1738 case (DL_ESTABLISH | REQUEST):
1739 if (test_bit(FLG_L1_ACTIV, &st->l2.flag)) {
1740 if (test_bit(FLG_LAPD, &st->l2.flag) ||
1741 test_bit(FLG_ORIG, &st->l2.flag)) {
1742 FsmEvent(&st->l2.l2m, EV_L2_DL_ESTABLISH_REQ, arg);
1744 } else {
1745 if (test_bit(FLG_LAPD, &st->l2.flag) ||
1746 test_bit(FLG_ORIG, &st->l2.flag)) {
1747 test_and_set_bit(FLG_ESTAB_PEND, &st->l2.flag);
1749 st->l2.l2l1(st, PH_ACTIVATE, NULL);
1751 break;
1752 case (DL_RELEASE | REQUEST):
1753 if (test_bit(FLG_LAPB, &st->l2.flag)) {
1754 st->l2.l2l1(st, PH_DEACTIVATE, NULL);
1756 FsmEvent(&st->l2.l2m, EV_L2_DL_RELEASE_REQ, arg);
1757 break;
1758 case (MDL_ASSIGN | REQUEST):
1759 FsmEvent(&st->l2.l2m, EV_L2_MDL_ASSIGN, arg);
1760 break;
1761 case (MDL_REMOVE | REQUEST):
1762 FsmEvent(&st->l2.l2m, EV_L2_MDL_REMOVE, arg);
1763 break;
1764 case (MDL_ERROR | RESPONSE):
1765 FsmEvent(&st->l2.l2m, EV_L2_MDL_ERROR, arg);
1766 break;
1770 void
1771 releasestack_isdnl2(struct PStack *st)
1773 FsmDelTimer(&st->l2.t200, 21);
1774 FsmDelTimer(&st->l2.t203, 16);
1775 skb_queue_purge(&st->l2.i_queue);
1776 skb_queue_purge(&st->l2.ui_queue);
1777 ReleaseWin(&st->l2);
1780 static void
1781 l2m_debug(struct FsmInst *fi, char *fmt, ...)
1783 va_list args;
1784 struct PStack *st = fi->userdata;
1786 va_start(args, fmt);
1787 VHiSax_putstatus(st->l1.hardware, st->l2.debug_id, fmt, args);
1788 va_end(args);
1791 void
1792 setstack_isdnl2(struct PStack *st, char *debug_id)
1794 spin_lock_init(&st->l2.lock);
1795 st->l1.l1l2 = isdnl2_l1l2;
1796 st->l3.l3l2 = isdnl2_l3l2;
1798 skb_queue_head_init(&st->l2.i_queue);
1799 skb_queue_head_init(&st->l2.ui_queue);
1800 InitWin(&st->l2);
1801 st->l2.debug = 0;
1803 st->l2.l2m.fsm = &l2fsm;
1804 if (test_bit(FLG_LAPB, &st->l2.flag))
1805 st->l2.l2m.state = ST_L2_4;
1806 else
1807 st->l2.l2m.state = ST_L2_1;
1808 st->l2.l2m.debug = 0;
1809 st->l2.l2m.userdata = st;
1810 st->l2.l2m.userint = 0;
1811 st->l2.l2m.printdebug = l2m_debug;
1812 strcpy(st->l2.debug_id, debug_id);
1814 FsmInitTimer(&st->l2.l2m, &st->l2.t200);
1815 FsmInitTimer(&st->l2.l2m, &st->l2.t203);
1818 static void
1819 transl2_l3l2(struct PStack *st, int pr, void *arg)
1821 switch (pr) {
1822 case (DL_DATA | REQUEST):
1823 case (DL_UNIT_DATA | REQUEST):
1824 st->l2.l2l1(st, PH_DATA | REQUEST, arg);
1825 break;
1826 case (DL_ESTABLISH | REQUEST):
1827 st->l2.l2l1(st, PH_ACTIVATE | REQUEST, NULL);
1828 break;
1829 case (DL_RELEASE | REQUEST):
1830 st->l2.l2l1(st, PH_DEACTIVATE | REQUEST, NULL);
1831 break;
1835 void
1836 setstack_transl2(struct PStack *st)
1838 st->l3.l3l2 = transl2_l3l2;
1841 void
1842 releasestack_transl2(struct PStack *st)
1846 int __init
1847 Isdnl2New(void)
1849 l2fsm.state_count = L2_STATE_COUNT;
1850 l2fsm.event_count = L2_EVENT_COUNT;
1851 l2fsm.strEvent = strL2Event;
1852 l2fsm.strState = strL2State;
1853 return FsmNew(&l2fsm, L2FnList, L2_FN_COUNT);
1856 void
1857 Isdnl2Free(void)
1859 FsmFree(&l2fsm);