3 * Author Karsten Keil <kkeil@novell.com>
5 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
24 struct Fsm l2fsm = {NULL, 0, 0, NULL, NULL};
26 static char *strL2State[] =
50 EV_L2_DL_ESTABLISH_REQ,
63 #define L2_EVENT_COUNT (EV_L2_FRAME_ERROR+1)
65 static char *strL2Event[] =
78 "EV_L2_DL_ESTABLISH_REQ",
79 "EV_L2_DL_RELEASE_REQ",
87 "EV_L2_CLEAR_OWN_BUSY",
92 l2m_debug(struct FsmInst *fi, char *fmt, ...)
94 struct layer2 *l2 = fi->userdata;
97 if (!(*debug & DEBUG_L2_FSM))
100 printk(KERN_DEBUG "l2 (tei %d): ", l2->tei);
107 l2headersize(struct layer2 *l2, int ui)
109 return ((test_bit(FLG_MOD128, &l2->flag) && (!ui)) ? 2 : 1) +
110 (test_bit(FLG_LAPD, &l2->flag) ? 2 : 1);
114 l2addrsize(struct layer2 *l2)
116 return test_bit(FLG_LAPD, &l2->flag) ? 2 : 1;
120 l2_newid(struct layer2 *l2)
134 l2up(struct layer2 *l2, u_int prim, struct sk_buff *skb)
140 mISDN_HEAD_PRIM(skb) = prim;
141 mISDN_HEAD_ID(skb) = (l2->ch.nr << 16) | l2->ch.addr;
142 err = l2->up->send(l2->up, skb);
144 printk(KERN_WARNING "%s: err=%d\n", __func__, err);
150 l2up_create(struct layer2 *l2, u_int prim, int len, void *arg)
153 struct mISDNhead *hh;
158 skb = mI_alloc_skb(len, GFP_ATOMIC);
161 hh = mISDN_HEAD_P(skb);
163 hh->id = (l2->ch.nr << 16) | l2->ch.addr;
165 memcpy(skb_put(skb, len), arg, len);
166 err = l2->up->send(l2->up, skb);
168 printk(KERN_WARNING "%s: err=%d\n", __func__, err);
174 l2down_skb(struct layer2 *l2, struct sk_buff *skb) {
177 ret = l2->ch.recv(l2->ch.peer, skb);
178 if (ret && (*debug & DEBUG_L2_RECV))
179 printk(KERN_DEBUG "l2down_skb: ret(%d)\n", ret);
184 l2down_raw(struct layer2 *l2, struct sk_buff *skb)
186 struct mISDNhead *hh = mISDN_HEAD_P(skb);
188 if (hh->prim == PH_DATA_REQ) {
189 if (test_and_set_bit(FLG_L1_NOTREADY, &l2->flag)) {
190 skb_queue_tail(&l2->down_queue, skb);
193 l2->down_id = mISDN_HEAD_ID(skb);
195 return l2down_skb(l2, skb);
199 l2down(struct layer2 *l2, u_int prim, u_int id, struct sk_buff *skb)
201 struct mISDNhead *hh = mISDN_HEAD_P(skb);
205 return l2down_raw(l2, skb);
209 l2down_create(struct layer2 *l2, u_int prim, u_int id, int len, void *arg)
213 struct mISDNhead *hh;
215 skb = mI_alloc_skb(len, GFP_ATOMIC);
218 hh = mISDN_HEAD_P(skb);
222 memcpy(skb_put(skb, len), arg, len);
223 err = l2down_raw(l2, skb);
230 ph_data_confirm(struct layer2 *l2, struct mISDNhead *hh, struct sk_buff *skb) {
231 struct sk_buff *nskb = skb;
234 if (test_bit(FLG_L1_NOTREADY, &l2->flag)) {
235 if (hh->id == l2->down_id) {
236 nskb = skb_dequeue(&l2->down_queue);
238 l2->down_id = mISDN_HEAD_ID(nskb);
239 if (l2down_skb(l2, nskb)) {
241 l2->down_id = MISDN_ID_NONE;
244 l2->down_id = MISDN_ID_NONE;
249 if (l2->down_id == MISDN_ID_NONE) {
250 test_and_clear_bit(FLG_L1_NOTREADY, &l2->flag);
251 mISDN_FsmEvent(&l2->l2m, EV_L2_ACK_PULL, NULL);
255 if (!test_and_set_bit(FLG_L1_NOTREADY, &l2->flag)) {
256 nskb = skb_dequeue(&l2->down_queue);
258 l2->down_id = mISDN_HEAD_ID(nskb);
259 if (l2down_skb(l2, nskb)) {
261 l2->down_id = MISDN_ID_NONE;
262 test_and_clear_bit(FLG_L1_NOTREADY, &l2->flag);
265 test_and_clear_bit(FLG_L1_NOTREADY, &l2->flag);
271 l2mgr(struct layer2 *l2, u_int prim, void *arg) {
275 "l2mgr: addr:%x prim %x %c\n", l2->id, prim, (char)c);
276 if (test_bit(FLG_LAPD, &l2->flag) &&
277 !test_bit(FLG_FIXED_TEI, &l2->flag)) {
283 l2_tei(l2, prim, (u_long)arg);
291 set_peer_busy(struct layer2 *l2) {
292 test_and_set_bit(FLG_PEER_BUSY, &l2->flag);
293 if (skb_queue_len(&l2->i_queue) || skb_queue_len(&l2->ui_queue))
294 test_and_set_bit(FLG_L2BLOCK, &l2->flag);
298 clear_peer_busy(struct layer2 *l2) {
299 if (test_and_clear_bit(FLG_PEER_BUSY, &l2->flag))
300 test_and_clear_bit(FLG_L2BLOCK, &l2->flag);
304 InitWin(struct layer2 *l2)
308 for (i = 0; i < MAX_WINDOW; i++)
309 l2->windowar[i] = NULL;
313 freewin(struct layer2 *l2)
317 for (i = 0; i < MAX_WINDOW; i++) {
318 if (l2->windowar[i]) {
320 dev_kfree_skb(l2->windowar[i]);
321 l2->windowar[i] = NULL;
328 ReleaseWin(struct layer2 *l2)
330 int cnt = freewin(l2);
334 "isdnl2 freed %d skbuffs in release\n", cnt);
338 cansend(struct layer2 *l2)
342 if (test_bit(FLG_MOD128, &l2->flag))
343 p1 = (l2->vs - l2->va) % 128;
345 p1 = (l2->vs - l2->va) % 8;
346 return (p1 < l2->window) && !test_bit(FLG_PEER_BUSY, &l2->flag);
350 clear_exception(struct layer2 *l2)
352 test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
353 test_and_clear_bit(FLG_REJEXC, &l2->flag);
354 test_and_clear_bit(FLG_OWN_BUSY, &l2->flag);
359 sethdraddr(struct layer2 *l2, u_char *header, int rsp)
361 u_char *ptr = header;
364 if (test_bit(FLG_LAPD, &l2->flag)) {
365 if (test_bit(FLG_LAPD_NET, &l2->flag))
367 *ptr++ = (l2->sapi << 2) | (crbit ? 2 : 0);
368 *ptr++ = (l2->tei << 1) | 1;
371 if (test_bit(FLG_ORIG, &l2->flag))
382 enqueue_super(struct layer2 *l2, struct sk_buff *skb)
384 if (l2down(l2, PH_DATA_REQ, l2_newid(l2), skb))
389 enqueue_ui(struct layer2 *l2, struct sk_buff *skb)
392 l2_tei(l2, MDL_STATUS_UI_IND, 0);
393 if (l2down(l2, PH_DATA_REQ, l2_newid(l2), skb))
400 return (data[0] & 0xef) == UI;
406 return (data[0] & 0xef) == UA;
412 return (data[0] & 0xef) == DM;
418 return (data[0] & 0xef) == DISC;
422 IsRR(u_char *data, struct layer2 *l2)
424 if (test_bit(FLG_MOD128, &l2->flag))
425 return data[0] == RR;
427 return (data[0] & 0xf) == 1;
431 IsSFrame(u_char *data, struct layer2 *l2)
433 register u_char d = *data;
435 if (!test_bit(FLG_MOD128, &l2->flag))
437 return ((d & 0xf3) == 1) && ((d & 0x0c) != 0x0c);
441 IsSABME(u_char *data, struct layer2 *l2)
443 u_char d = data[0] & ~0x10;
445 return test_bit(FLG_MOD128, &l2->flag) ? d == SABME : d == SABM;
449 IsREJ(u_char *data, struct layer2 *l2)
451 return test_bit(FLG_MOD128, &l2->flag) ?
452 data[0] == REJ : (data[0] & 0xf) == REJ;
458 return (data[0] & 0xef) == FRMR;
462 IsRNR(u_char *data, struct layer2 *l2)
464 return test_bit(FLG_MOD128, &l2->flag) ?
465 data[0] == RNR : (data[0] & 0xf) == RNR;
469 iframe_error(struct layer2 *l2, struct sk_buff *skb)
472 int rsp = *skb->data & 0x2;
474 i = l2addrsize(l2) + (test_bit(FLG_MOD128, &l2->flag) ? 2 : 1);
475 if (test_bit(FLG_ORIG, &l2->flag))
481 if ((skb->len - i) > l2->maxlen)
487 super_error(struct layer2 *l2, struct sk_buff *skb)
489 if (skb->len != l2addrsize(l2) +
490 (test_bit(FLG_MOD128, &l2->flag) ? 2 : 1))
496 unnum_error(struct layer2 *l2, struct sk_buff *skb, int wantrsp)
498 int rsp = (*skb->data & 0x2) >> 1;
499 if (test_bit(FLG_ORIG, &l2->flag))
503 if (skb->len != l2addrsize(l2) + 1)
509 UI_error(struct layer2 *l2, struct sk_buff *skb)
511 int rsp = *skb->data & 0x2;
512 if (test_bit(FLG_ORIG, &l2->flag))
516 if (skb->len > l2->maxlen + l2addrsize(l2) + 1)
522 FRMR_error(struct layer2 *l2, struct sk_buff *skb)
524 u_int headers = l2addrsize(l2) + 1;
525 u_char *datap = skb->data + headers;
526 int rsp = *skb->data & 0x2;
528 if (test_bit(FLG_ORIG, &l2->flag))
532 if (test_bit(FLG_MOD128, &l2->flag)) {
533 if (skb->len < headers + 5)
535 else if (*debug & DEBUG_L2)
537 "FRMR information %2x %2x %2x %2x %2x",
538 datap[0], datap[1], datap[2], datap[3], datap[4]);
540 if (skb->len < headers + 3)
542 else if (*debug & DEBUG_L2)
544 "FRMR information %2x %2x %2x",
545 datap[0], datap[1], datap[2]);
551 legalnr(struct layer2 *l2, unsigned int nr)
553 if (test_bit(FLG_MOD128, &l2->flag))
554 return ((nr - l2->va) % 128) <= ((l2->vs - l2->va) % 128);
556 return ((nr - l2->va) % 8) <= ((l2->vs - l2->va) % 8);
560 setva(struct layer2 *l2, unsigned int nr)
564 while (l2->va != nr) {
566 if (test_bit(FLG_MOD128, &l2->flag))
570 if (l2->windowar[l2->sow]) {
571 skb_trim(l2->windowar[l2->sow], 0);
572 skb_queue_tail(&l2->tmp_queue, l2->windowar[l2->sow]);
573 l2->windowar[l2->sow] = NULL;
575 l2->sow = (l2->sow + 1) % l2->window;
577 skb = skb_dequeue(&l2->tmp_queue);
580 skb = skb_dequeue(&l2->tmp_queue);
585 send_uframe(struct layer2 *l2, struct sk_buff *skb, u_char cmd, u_char cr)
587 u_char tmp[MAX_L2HEADER_LEN];
590 i = sethdraddr(l2, tmp, cr);
595 skb = mI_alloc_skb(i, GFP_ATOMIC);
597 printk(KERN_WARNING "%s: can't alloc skbuff\n",
602 memcpy(skb_put(skb, i), tmp, i);
603 enqueue_super(l2, skb);
608 get_PollFlag(struct layer2 *l2, struct sk_buff *skb)
610 return skb->data[l2addrsize(l2)] & 0x10;
614 get_PollFlagFree(struct layer2 *l2, struct sk_buff *skb)
618 PF = get_PollFlag(l2, skb);
624 start_t200(struct layer2 *l2, int i)
626 mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, i);
627 test_and_set_bit(FLG_T200_RUN, &l2->flag);
631 restart_t200(struct layer2 *l2, int i)
633 mISDN_FsmRestartTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, i);
634 test_and_set_bit(FLG_T200_RUN, &l2->flag);
638 stop_t200(struct layer2 *l2, int i)
640 if (test_and_clear_bit(FLG_T200_RUN, &l2->flag))
641 mISDN_FsmDelTimer(&l2->t200, i);
645 st5_dl_release_l2l3(struct layer2 *l2)
649 if (test_and_clear_bit(FLG_PEND_REL, &l2->flag))
653 l2up_create(l2, pr, 0, NULL);
657 lapb_dl_release_l2l3(struct layer2 *l2, int f)
659 if (test_bit(FLG_LAPB, &l2->flag))
660 l2down_create(l2, PH_DEACTIVATE_REQ, l2_newid(l2), 0, NULL);
661 l2up_create(l2, f, 0, NULL);
665 establishlink(struct FsmInst *fi)
667 struct layer2 *l2 = fi->userdata;
672 cmd = (test_bit(FLG_MOD128, &l2->flag) ? SABME : SABM) | 0x10;
673 send_uframe(l2, NULL, cmd, CMD);
674 mISDN_FsmDelTimer(&l2->t203, 1);
676 test_and_clear_bit(FLG_PEND_REL, &l2->flag);
678 mISDN_FsmChangeState(fi, ST_L2_5);
682 l2_mdl_error_ua(struct FsmInst *fi, int event, void *arg)
684 struct sk_buff *skb = arg;
685 struct layer2 *l2 = fi->userdata;
687 if (get_PollFlagFree(l2, skb))
688 l2mgr(l2, MDL_ERROR_IND, (void *) 'C');
690 l2mgr(l2, MDL_ERROR_IND, (void *) 'D');
695 l2_mdl_error_dm(struct FsmInst *fi, int event, void *arg)
697 struct sk_buff *skb = arg;
698 struct layer2 *l2 = fi->userdata;
700 if (get_PollFlagFree(l2, skb))
701 l2mgr(l2, MDL_ERROR_IND, (void *) 'B');
703 l2mgr(l2, MDL_ERROR_IND, (void *) 'E');
705 test_and_clear_bit(FLG_L3_INIT, &l2->flag);
710 l2_st8_mdl_error_dm(struct FsmInst *fi, int event, void *arg)
712 struct sk_buff *skb = arg;
713 struct layer2 *l2 = fi->userdata;
715 if (get_PollFlagFree(l2, skb))
716 l2mgr(l2, MDL_ERROR_IND, (void *) 'B');
718 l2mgr(l2, MDL_ERROR_IND, (void *) 'E');
720 test_and_clear_bit(FLG_L3_INIT, &l2->flag);
724 l2_go_st3(struct FsmInst *fi, int event, void *arg)
726 dev_kfree_skb((struct sk_buff *)arg);
727 mISDN_FsmChangeState(fi, ST_L2_3);
731 l2_mdl_assign(struct FsmInst *fi, int event, void *arg)
733 struct layer2 *l2 = fi->userdata;
735 mISDN_FsmChangeState(fi, ST_L2_3);
736 dev_kfree_skb((struct sk_buff *)arg);
737 l2_tei(l2, MDL_ASSIGN_IND, 0);
741 l2_queue_ui_assign(struct FsmInst *fi, int event, void *arg)
743 struct layer2 *l2 = fi->userdata;
744 struct sk_buff *skb = arg;
746 skb_queue_tail(&l2->ui_queue, skb);
747 mISDN_FsmChangeState(fi, ST_L2_2);
748 l2_tei(l2, MDL_ASSIGN_IND, 0);
752 l2_queue_ui(struct FsmInst *fi, int event, void *arg)
754 struct layer2 *l2 = fi->userdata;
755 struct sk_buff *skb = arg;
757 skb_queue_tail(&l2->ui_queue, skb);
761 tx_ui(struct layer2 *l2)
764 u_char header[MAX_L2HEADER_LEN];
767 i = sethdraddr(l2, header, CMD);
768 if (test_bit(FLG_LAPD_NET, &l2->flag))
769 header[1] = 0xff; /* tei 127 */
771 while ((skb = skb_dequeue(&l2->ui_queue))) {
772 memcpy(skb_push(skb, i), header, i);
778 l2_send_ui(struct FsmInst *fi, int event, void *arg)
780 struct layer2 *l2 = fi->userdata;
781 struct sk_buff *skb = arg;
783 skb_queue_tail(&l2->ui_queue, skb);
788 l2_got_ui(struct FsmInst *fi, int event, void *arg)
790 struct layer2 *l2 = fi->userdata;
791 struct sk_buff *skb = arg;
793 skb_pull(skb, l2headersize(l2, 1));
795 * in states 1-3 for broadcast
799 l2_tei(l2, MDL_STATUS_UI_IND, 0);
800 l2up(l2, DL_UNITDATA_IND, skb);
804 l2_establish(struct FsmInst *fi, int event, void *arg)
806 struct sk_buff *skb = arg;
807 struct layer2 *l2 = fi->userdata;
810 test_and_set_bit(FLG_L3_INIT, &l2->flag);
815 l2_discard_i_setl3(struct FsmInst *fi, int event, void *arg)
817 struct sk_buff *skb = arg;
818 struct layer2 *l2 = fi->userdata;
820 skb_queue_purge(&l2->i_queue);
821 test_and_set_bit(FLG_L3_INIT, &l2->flag);
822 test_and_clear_bit(FLG_PEND_REL, &l2->flag);
827 l2_l3_reestablish(struct FsmInst *fi, int event, void *arg)
829 struct sk_buff *skb = arg;
830 struct layer2 *l2 = fi->userdata;
832 skb_queue_purge(&l2->i_queue);
834 test_and_set_bit(FLG_L3_INIT, &l2->flag);
839 l2_release(struct FsmInst *fi, int event, void *arg)
841 struct layer2 *l2 = fi->userdata;
842 struct sk_buff *skb = arg;
845 l2up(l2, DL_RELEASE_CNF, skb);
849 l2_pend_rel(struct FsmInst *fi, int event, void *arg)
851 struct sk_buff *skb = arg;
852 struct layer2 *l2 = fi->userdata;
854 test_and_set_bit(FLG_PEND_REL, &l2->flag);
859 l2_disconnect(struct FsmInst *fi, int event, void *arg)
861 struct layer2 *l2 = fi->userdata;
862 struct sk_buff *skb = arg;
864 skb_queue_purge(&l2->i_queue);
866 mISDN_FsmChangeState(fi, ST_L2_6);
868 send_uframe(l2, NULL, DISC | 0x10, CMD);
869 mISDN_FsmDelTimer(&l2->t203, 1);
876 l2_start_multi(struct FsmInst *fi, int event, void *arg)
878 struct layer2 *l2 = fi->userdata;
879 struct sk_buff *skb = arg;
886 send_uframe(l2, NULL, UA | get_PollFlag(l2, skb), RSP);
887 mISDN_FsmChangeState(fi, ST_L2_7);
888 mISDN_FsmAddTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 3);
890 l2up(l2, DL_ESTABLISH_IND, skb);
892 l2_tei(l2, MDL_STATUS_UP_IND, 0);
896 l2_send_UA(struct FsmInst *fi, int event, void *arg)
898 struct layer2 *l2 = fi->userdata;
899 struct sk_buff *skb = arg;
901 send_uframe(l2, skb, UA | get_PollFlag(l2, skb), RSP);
905 l2_send_DM(struct FsmInst *fi, int event, void *arg)
907 struct layer2 *l2 = fi->userdata;
908 struct sk_buff *skb = arg;
910 send_uframe(l2, skb, DM | get_PollFlag(l2, skb), RSP);
914 l2_restart_multi(struct FsmInst *fi, int event, void *arg)
916 struct layer2 *l2 = fi->userdata;
917 struct sk_buff *skb = arg;
920 send_uframe(l2, skb, UA | get_PollFlag(l2, skb), RSP);
922 l2mgr(l2, MDL_ERROR_IND, (void *) 'F');
924 if (l2->vs != l2->va) {
925 skb_queue_purge(&l2->i_queue);
934 mISDN_FsmChangeState(fi, ST_L2_7);
936 mISDN_FsmRestartTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 3);
939 l2up_create(l2, DL_ESTABLISH_IND, 0, NULL);
940 /* mISDN_queue_data(&l2->inst, l2->inst.id | MSG_BROADCAST,
941 * MGR_SHORTSTATUS | INDICATION, SSTATUS_L2_ESTABLISHED,
944 if (skb_queue_len(&l2->i_queue) && cansend(l2))
945 mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
949 l2_stop_multi(struct FsmInst *fi, int event, void *arg)
951 struct layer2 *l2 = fi->userdata;
952 struct sk_buff *skb = arg;
954 mISDN_FsmChangeState(fi, ST_L2_4);
955 mISDN_FsmDelTimer(&l2->t203, 3);
958 send_uframe(l2, skb, UA | get_PollFlag(l2, skb), RSP);
959 skb_queue_purge(&l2->i_queue);
961 lapb_dl_release_l2l3(l2, DL_RELEASE_IND);
963 l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
967 l2_connected(struct FsmInst *fi, int event, void *arg)
969 struct layer2 *l2 = fi->userdata;
970 struct sk_buff *skb = arg;
973 if (!get_PollFlag(l2, skb)) {
974 l2_mdl_error_ua(fi, event, arg);
978 if (test_and_clear_bit(FLG_PEND_REL, &l2->flag))
979 l2_disconnect(fi, event, NULL);
980 if (test_and_clear_bit(FLG_L3_INIT, &l2->flag)) {
981 pr = DL_ESTABLISH_CNF;
982 } else if (l2->vs != l2->va) {
983 skb_queue_purge(&l2->i_queue);
984 pr = DL_ESTABLISH_IND;
991 mISDN_FsmChangeState(fi, ST_L2_7);
992 mISDN_FsmAddTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 4);
994 l2up_create(l2, pr, 0, NULL);
996 if (skb_queue_len(&l2->i_queue) && cansend(l2))
997 mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
1000 l2_tei(l2, MDL_STATUS_UP_IND, 0);
1004 l2_released(struct FsmInst *fi, int event, void *arg)
1006 struct layer2 *l2 = fi->userdata;
1007 struct sk_buff *skb = arg;
1009 if (!get_PollFlag(l2, skb)) {
1010 l2_mdl_error_ua(fi, event, arg);
1015 lapb_dl_release_l2l3(l2, DL_RELEASE_CNF);
1016 mISDN_FsmChangeState(fi, ST_L2_4);
1018 l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1022 l2_reestablish(struct FsmInst *fi, int event, void *arg)
1024 struct layer2 *l2 = fi->userdata;
1025 struct sk_buff *skb = arg;
1027 if (!get_PollFlagFree(l2, skb)) {
1029 test_and_set_bit(FLG_L3_INIT, &l2->flag);
1034 l2_st5_dm_release(struct FsmInst *fi, int event, void *arg)
1036 struct layer2 *l2 = fi->userdata;
1037 struct sk_buff *skb = arg;
1039 if (get_PollFlagFree(l2, skb)) {
1041 if (!test_bit(FLG_L3_INIT, &l2->flag))
1042 skb_queue_purge(&l2->i_queue);
1043 if (test_bit(FLG_LAPB, &l2->flag))
1044 l2down_create(l2, PH_DEACTIVATE_REQ,
1045 l2_newid(l2), 0, NULL);
1046 st5_dl_release_l2l3(l2);
1047 mISDN_FsmChangeState(fi, ST_L2_4);
1049 l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1054 l2_st6_dm_release(struct FsmInst *fi, int event, void *arg)
1056 struct layer2 *l2 = fi->userdata;
1057 struct sk_buff *skb = arg;
1059 if (get_PollFlagFree(l2, skb)) {
1061 lapb_dl_release_l2l3(l2, DL_RELEASE_CNF);
1062 mISDN_FsmChangeState(fi, ST_L2_4);
1064 l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1069 enquiry_cr(struct layer2 *l2, u_char typ, u_char cr, u_char pf)
1071 struct sk_buff *skb;
1072 u_char tmp[MAX_L2HEADER_LEN];
1075 i = sethdraddr(l2, tmp, cr);
1076 if (test_bit(FLG_MOD128, &l2->flag)) {
1078 tmp[i++] = (l2->vr << 1) | (pf ? 1 : 0);
1080 tmp[i++] = (l2->vr << 5) | typ | (pf ? 0x10 : 0);
1081 skb = mI_alloc_skb(i, GFP_ATOMIC);
1084 "isdnl2 can't alloc sbbuff for enquiry_cr\n");
1087 memcpy(skb_put(skb, i), tmp, i);
1088 enqueue_super(l2, skb);
1092 enquiry_response(struct layer2 *l2)
1094 if (test_bit(FLG_OWN_BUSY, &l2->flag))
1095 enquiry_cr(l2, RNR, RSP, 1);
1097 enquiry_cr(l2, RR, RSP, 1);
1098 test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
1102 transmit_enquiry(struct layer2 *l2)
1104 if (test_bit(FLG_OWN_BUSY, &l2->flag))
1105 enquiry_cr(l2, RNR, CMD, 1);
1107 enquiry_cr(l2, RR, CMD, 1);
1108 test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
1114 nrerrorrecovery(struct FsmInst *fi)
1116 struct layer2 *l2 = fi->userdata;
1118 l2mgr(l2, MDL_ERROR_IND, (void *) 'J');
1120 test_and_clear_bit(FLG_L3_INIT, &l2->flag);
1124 invoke_retransmission(struct layer2 *l2, unsigned int nr)
1129 while (l2->vs != nr) {
1131 if (test_bit(FLG_MOD128, &l2->flag)) {
1133 p1 = (l2->vs - l2->va) % 128;
1136 p1 = (l2->vs - l2->va) % 8;
1138 p1 = (p1 + l2->sow) % l2->window;
1139 if (l2->windowar[p1])
1140 skb_queue_head(&l2->i_queue, l2->windowar[p1]);
1143 "%s: windowar[%d] is NULL\n",
1145 l2->windowar[p1] = NULL;
1147 mISDN_FsmEvent(&l2->l2m, EV_L2_ACK_PULL, NULL);
1152 l2_st7_got_super(struct FsmInst *fi, int event, void *arg)
1154 struct layer2 *l2 = fi->userdata;
1155 struct sk_buff *skb = arg;
1156 int PollFlag, rsp, typ = RR;
1159 rsp = *skb->data & 0x2;
1160 if (test_bit(FLG_ORIG, &l2->flag))
1163 skb_pull(skb, l2addrsize(l2));
1164 if (IsRNR(skb->data, l2)) {
1168 clear_peer_busy(l2);
1169 if (IsREJ(skb->data, l2))
1172 if (test_bit(FLG_MOD128, &l2->flag)) {
1173 PollFlag = (skb->data[1] & 0x1) == 0x1;
1174 nr = skb->data[1] >> 1;
1176 PollFlag = (skb->data[0] & 0x10);
1177 nr = (skb->data[0] >> 5) & 0x7;
1183 l2mgr(l2, MDL_ERROR_IND, (void *) 'A');
1185 enquiry_response(l2);
1187 if (legalnr(l2, nr)) {
1190 invoke_retransmission(l2, nr);
1192 if (mISDN_FsmAddTimer(&l2->t203, l2->T203,
1193 EV_L2_T203, NULL, 6))
1194 l2m_debug(&l2->l2m, "Restart T203 ST7 REJ");
1195 } else if ((nr == l2->vs) && (typ == RR)) {
1198 mISDN_FsmRestartTimer(&l2->t203, l2->T203,
1199 EV_L2_T203, NULL, 7);
1200 } else if ((l2->va != nr) || (typ == RNR)) {
1203 mISDN_FsmDelTimer(&l2->t203, 9);
1204 restart_t200(l2, 12);
1206 if (skb_queue_len(&l2->i_queue) && (typ == RR))
1207 mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
1209 nrerrorrecovery(fi);
1213 l2_feed_i_if_reest(struct FsmInst *fi, int event, void *arg)
1215 struct layer2 *l2 = fi->userdata;
1216 struct sk_buff *skb = arg;
1218 if (!test_bit(FLG_L3_INIT, &l2->flag))
1219 skb_queue_tail(&l2->i_queue, skb);
1225 l2_feed_i_pull(struct FsmInst *fi, int event, void *arg)
1227 struct layer2 *l2 = fi->userdata;
1228 struct sk_buff *skb = arg;
1230 skb_queue_tail(&l2->i_queue, skb);
1231 mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
1235 l2_feed_iqueue(struct FsmInst *fi, int event, void *arg)
1237 struct layer2 *l2 = fi->userdata;
1238 struct sk_buff *skb = arg;
1240 skb_queue_tail(&l2->i_queue, skb);
1244 l2_got_iframe(struct FsmInst *fi, int event, void *arg)
1246 struct layer2 *l2 = fi->userdata;
1247 struct sk_buff *skb = arg;
1252 if (test_bit(FLG_MOD128, &l2->flag)) {
1253 PollFlag = ((skb->data[i + 1] & 0x1) == 0x1);
1254 ns = skb->data[i] >> 1;
1255 nr = (skb->data[i + 1] >> 1) & 0x7f;
1257 PollFlag = (skb->data[i] & 0x10);
1258 ns = (skb->data[i] >> 1) & 0x7;
1259 nr = (skb->data[i] >> 5) & 0x7;
1261 if (test_bit(FLG_OWN_BUSY, &l2->flag)) {
1264 enquiry_response(l2);
1268 if (test_bit(FLG_MOD128, &l2->flag))
1272 test_and_clear_bit(FLG_REJEXC, &l2->flag);
1274 enquiry_response(l2);
1276 test_and_set_bit(FLG_ACK_PEND, &l2->flag);
1277 skb_pull(skb, l2headersize(l2, 0));
1278 l2up(l2, DL_DATA_IND, skb);
1282 if (test_and_set_bit(FLG_REJEXC, &l2->flag)) {
1284 enquiry_response(l2);
1286 enquiry_cr(l2, REJ, RSP, PollFlag);
1287 test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
1291 if (legalnr(l2, nr)) {
1292 if (!test_bit(FLG_PEER_BUSY, &l2->flag) &&
1293 (fi->state == ST_L2_7)) {
1296 mISDN_FsmRestartTimer(&l2->t203, l2->T203,
1297 EV_L2_T203, NULL, 7);
1298 } else if (nr != l2->va)
1299 restart_t200(l2, 14);
1303 nrerrorrecovery(fi);
1306 if (skb_queue_len(&l2->i_queue) && (fi->state == ST_L2_7))
1307 mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
1308 if (test_and_clear_bit(FLG_ACK_PEND, &l2->flag))
1309 enquiry_cr(l2, RR, RSP, 0);
1313 l2_got_tei(struct FsmInst *fi, int event, void *arg)
1315 struct layer2 *l2 = fi->userdata;
1318 l2->tei = (signed char)(long)arg;
1319 set_channel_address(&l2->ch, l2->sapi, l2->tei);
1320 info = DL_INFO_L2_CONNECT;
1321 l2up_create(l2, DL_INFORMATION_IND, sizeof(info), &info);
1322 if (fi->state == ST_L2_3) {
1324 test_and_set_bit(FLG_L3_INIT, &l2->flag);
1326 mISDN_FsmChangeState(fi, ST_L2_4);
1327 if (skb_queue_len(&l2->ui_queue))
1332 l2_st5_tout_200(struct FsmInst *fi, int event, void *arg)
1334 struct layer2 *l2 = fi->userdata;
1336 if (test_bit(FLG_LAPD, &l2->flag) &&
1337 test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
1338 mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
1339 } else if (l2->rc == l2->N200) {
1340 mISDN_FsmChangeState(fi, ST_L2_4);
1341 test_and_clear_bit(FLG_T200_RUN, &l2->flag);
1342 skb_queue_purge(&l2->i_queue);
1343 l2mgr(l2, MDL_ERROR_IND, (void *) 'G');
1344 if (test_bit(FLG_LAPB, &l2->flag))
1345 l2down_create(l2, PH_DEACTIVATE_REQ,
1346 l2_newid(l2), 0, NULL);
1347 st5_dl_release_l2l3(l2);
1349 l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1352 mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
1353 send_uframe(l2, NULL, (test_bit(FLG_MOD128, &l2->flag) ?
1354 SABME : SABM) | 0x10, CMD);
1359 l2_st6_tout_200(struct FsmInst *fi, int event, void *arg)
1361 struct layer2 *l2 = fi->userdata;
1363 if (test_bit(FLG_LAPD, &l2->flag) &&
1364 test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
1365 mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
1366 } else if (l2->rc == l2->N200) {
1367 mISDN_FsmChangeState(fi, ST_L2_4);
1368 test_and_clear_bit(FLG_T200_RUN, &l2->flag);
1369 l2mgr(l2, MDL_ERROR_IND, (void *) 'H');
1370 lapb_dl_release_l2l3(l2, DL_RELEASE_CNF);
1372 l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1375 mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200,
1377 send_uframe(l2, NULL, DISC | 0x10, CMD);
1382 l2_st7_tout_200(struct FsmInst *fi, int event, void *arg)
1384 struct layer2 *l2 = fi->userdata;
1386 if (test_bit(FLG_LAPD, &l2->flag) &&
1387 test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
1388 mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
1391 test_and_clear_bit(FLG_T200_RUN, &l2->flag);
1393 mISDN_FsmChangeState(fi, ST_L2_8);
1394 transmit_enquiry(l2);
1399 l2_st8_tout_200(struct FsmInst *fi, int event, void *arg)
1401 struct layer2 *l2 = fi->userdata;
1403 if (test_bit(FLG_LAPD, &l2->flag) &&
1404 test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
1405 mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
1408 test_and_clear_bit(FLG_T200_RUN, &l2->flag);
1409 if (l2->rc == l2->N200) {
1410 l2mgr(l2, MDL_ERROR_IND, (void *) 'I');
1412 test_and_clear_bit(FLG_L3_INIT, &l2->flag);
1414 transmit_enquiry(l2);
1420 l2_st7_tout_203(struct FsmInst *fi, int event, void *arg)
1422 struct layer2 *l2 = fi->userdata;
1424 if (test_bit(FLG_LAPD, &l2->flag) &&
1425 test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
1426 mISDN_FsmAddTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 9);
1429 mISDN_FsmChangeState(fi, ST_L2_8);
1430 transmit_enquiry(l2);
1435 l2_pull_iqueue(struct FsmInst *fi, int event, void *arg)
1437 struct layer2 *l2 = fi->userdata;
1438 struct sk_buff *skb, *nskb, *oskb;
1439 u_char header[MAX_L2HEADER_LEN];
1445 skb = skb_dequeue(&l2->i_queue);
1449 if (test_bit(FLG_MOD128, &l2->flag))
1450 p1 = (l2->vs - l2->va) % 128;
1452 p1 = (l2->vs - l2->va) % 8;
1453 p1 = (p1 + l2->sow) % l2->window;
1454 if (l2->windowar[p1]) {
1455 printk(KERN_WARNING "isdnl2 try overwrite ack queue entry %d\n",
1457 dev_kfree_skb(l2->windowar[p1]);
1459 l2->windowar[p1] = skb;
1460 i = sethdraddr(l2, header, CMD);
1461 if (test_bit(FLG_MOD128, &l2->flag)) {
1462 header[i++] = l2->vs << 1;
1463 header[i++] = l2->vr << 1;
1464 l2->vs = (l2->vs + 1) % 128;
1466 header[i++] = (l2->vr << 5) | (l2->vs << 1);
1467 l2->vs = (l2->vs + 1) % 8;
1470 nskb = skb_clone(skb, GFP_ATOMIC);
1471 p1 = skb_headroom(nskb);
1473 memcpy(skb_push(nskb, i), header, i);
1476 "isdnl2 pull_iqueue skb header(%d/%d) too short\n", i, p1);
1478 nskb = mI_alloc_skb(oskb->len + i, GFP_ATOMIC);
1480 dev_kfree_skb(oskb);
1481 printk(KERN_WARNING "%s: no skb mem\n", __func__);
1484 memcpy(skb_put(nskb, i), header, i);
1485 memcpy(skb_put(nskb, oskb->len), oskb->data, oskb->len);
1486 dev_kfree_skb(oskb);
1488 l2down(l2, PH_DATA_REQ, l2_newid(l2), nskb);
1489 test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
1490 if (!test_and_set_bit(FLG_T200_RUN, &l2->flag)) {
1491 mISDN_FsmDelTimer(&l2->t203, 13);
1492 mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 11);
1497 l2_st8_got_super(struct FsmInst *fi, int event, void *arg)
1499 struct layer2 *l2 = fi->userdata;
1500 struct sk_buff *skb = arg;
1501 int PollFlag, rsp, rnr = 0;
1504 rsp = *skb->data & 0x2;
1505 if (test_bit(FLG_ORIG, &l2->flag))
1508 skb_pull(skb, l2addrsize(l2));
1510 if (IsRNR(skb->data, l2)) {
1514 clear_peer_busy(l2);
1516 if (test_bit(FLG_MOD128, &l2->flag)) {
1517 PollFlag = (skb->data[1] & 0x1) == 0x1;
1518 nr = skb->data[1] >> 1;
1520 PollFlag = (skb->data[0] & 0x10);
1521 nr = (skb->data[0] >> 5) & 0x7;
1524 if (rsp && PollFlag) {
1525 if (legalnr(l2, nr)) {
1527 restart_t200(l2, 15);
1530 mISDN_FsmAddTimer(&l2->t203, l2->T203,
1531 EV_L2_T203, NULL, 5);
1534 invoke_retransmission(l2, nr);
1535 mISDN_FsmChangeState(fi, ST_L2_7);
1536 if (skb_queue_len(&l2->i_queue) && cansend(l2))
1537 mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
1539 nrerrorrecovery(fi);
1541 if (!rsp && PollFlag)
1542 enquiry_response(l2);
1543 if (legalnr(l2, nr))
1546 nrerrorrecovery(fi);
1551 l2_got_FRMR(struct FsmInst *fi, int event, void *arg)
1553 struct layer2 *l2 = fi->userdata;
1554 struct sk_buff *skb = arg;
1556 skb_pull(skb, l2addrsize(l2) + 1);
1558 if (!(skb->data[0] & 1) || ((skb->data[0] & 3) == 1) || /* I or S */
1559 (IsUA(skb->data) && (fi->state == ST_L2_7))) {
1560 l2mgr(l2, MDL_ERROR_IND, (void *) 'K');
1562 test_and_clear_bit(FLG_L3_INIT, &l2->flag);
1568 l2_st24_tei_remove(struct FsmInst *fi, int event, void *arg)
1570 struct layer2 *l2 = fi->userdata;
1572 skb_queue_purge(&l2->ui_queue);
1573 l2->tei = GROUP_TEI;
1574 mISDN_FsmChangeState(fi, ST_L2_1);
1578 l2_st3_tei_remove(struct FsmInst *fi, int event, void *arg)
1580 struct layer2 *l2 = fi->userdata;
1582 skb_queue_purge(&l2->ui_queue);
1583 l2->tei = GROUP_TEI;
1584 l2up_create(l2, DL_RELEASE_IND, 0, NULL);
1585 mISDN_FsmChangeState(fi, ST_L2_1);
1589 l2_st5_tei_remove(struct FsmInst *fi, int event, void *arg)
1591 struct layer2 *l2 = fi->userdata;
1593 skb_queue_purge(&l2->i_queue);
1594 skb_queue_purge(&l2->ui_queue);
1596 l2->tei = GROUP_TEI;
1598 st5_dl_release_l2l3(l2);
1599 mISDN_FsmChangeState(fi, ST_L2_1);
1603 l2_st6_tei_remove(struct FsmInst *fi, int event, void *arg)
1605 struct layer2 *l2 = fi->userdata;
1607 skb_queue_purge(&l2->ui_queue);
1608 l2->tei = GROUP_TEI;
1610 l2up_create(l2, DL_RELEASE_IND, 0, NULL);
1611 mISDN_FsmChangeState(fi, ST_L2_1);
1615 l2_tei_remove(struct FsmInst *fi, int event, void *arg)
1617 struct layer2 *l2 = fi->userdata;
1619 skb_queue_purge(&l2->i_queue);
1620 skb_queue_purge(&l2->ui_queue);
1622 l2->tei = GROUP_TEI;
1624 mISDN_FsmDelTimer(&l2->t203, 19);
1625 l2up_create(l2, DL_RELEASE_IND, 0, NULL);
1626 /* mISDN_queue_data(&l2->inst, l2->inst.id | MSG_BROADCAST,
1627 * MGR_SHORTSTATUS_IND, SSTATUS_L2_RELEASED,
1630 mISDN_FsmChangeState(fi, ST_L2_1);
1634 l2_st14_persistant_da(struct FsmInst *fi, int event, void *arg)
1636 struct layer2 *l2 = fi->userdata;
1637 struct sk_buff *skb = arg;
1639 skb_queue_purge(&l2->i_queue);
1640 skb_queue_purge(&l2->ui_queue);
1641 if (test_and_clear_bit(FLG_ESTAB_PEND, &l2->flag))
1642 l2up(l2, DL_RELEASE_IND, skb);
1648 l2_st5_persistant_da(struct FsmInst *fi, int event, void *arg)
1650 struct layer2 *l2 = fi->userdata;
1651 struct sk_buff *skb = arg;
1653 skb_queue_purge(&l2->i_queue);
1654 skb_queue_purge(&l2->ui_queue);
1657 st5_dl_release_l2l3(l2);
1658 mISDN_FsmChangeState(fi, ST_L2_4);
1660 l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1665 l2_st6_persistant_da(struct FsmInst *fi, int event, void *arg)
1667 struct layer2 *l2 = fi->userdata;
1668 struct sk_buff *skb = arg;
1670 skb_queue_purge(&l2->ui_queue);
1672 l2up(l2, DL_RELEASE_CNF, skb);
1673 mISDN_FsmChangeState(fi, ST_L2_4);
1675 l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1679 l2_persistant_da(struct FsmInst *fi, int event, void *arg)
1681 struct layer2 *l2 = fi->userdata;
1682 struct sk_buff *skb = arg;
1684 skb_queue_purge(&l2->i_queue);
1685 skb_queue_purge(&l2->ui_queue);
1688 mISDN_FsmDelTimer(&l2->t203, 19);
1689 l2up(l2, DL_RELEASE_IND, skb);
1690 mISDN_FsmChangeState(fi, ST_L2_4);
1692 l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
1696 l2_set_own_busy(struct FsmInst *fi, int event, void *arg)
1698 struct layer2 *l2 = fi->userdata;
1699 struct sk_buff *skb = arg;
1701 if (!test_and_set_bit(FLG_OWN_BUSY, &l2->flag)) {
1702 enquiry_cr(l2, RNR, RSP, 0);
1703 test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
1710 l2_clear_own_busy(struct FsmInst *fi, int event, void *arg)
1712 struct layer2 *l2 = fi->userdata;
1713 struct sk_buff *skb = arg;
1715 if (!test_and_clear_bit(FLG_OWN_BUSY, &l2->flag)) {
1716 enquiry_cr(l2, RR, RSP, 0);
1717 test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
1724 l2_frame_error(struct FsmInst *fi, int event, void *arg)
1726 struct layer2 *l2 = fi->userdata;
1728 l2mgr(l2, MDL_ERROR_IND, arg);
1732 l2_frame_error_reest(struct FsmInst *fi, int event, void *arg)
1734 struct layer2 *l2 = fi->userdata;
1736 l2mgr(l2, MDL_ERROR_IND, arg);
1738 test_and_clear_bit(FLG_L3_INIT, &l2->flag);
1741 static struct FsmNode L2FnList[] =
1743 {ST_L2_1, EV_L2_DL_ESTABLISH_REQ, l2_mdl_assign},
1744 {ST_L2_2, EV_L2_DL_ESTABLISH_REQ, l2_go_st3},
1745 {ST_L2_4, EV_L2_DL_ESTABLISH_REQ, l2_establish},
1746 {ST_L2_5, EV_L2_DL_ESTABLISH_REQ, l2_discard_i_setl3},
1747 {ST_L2_7, EV_L2_DL_ESTABLISH_REQ, l2_l3_reestablish},
1748 {ST_L2_8, EV_L2_DL_ESTABLISH_REQ, l2_l3_reestablish},
1749 {ST_L2_4, EV_L2_DL_RELEASE_REQ, l2_release},
1750 {ST_L2_5, EV_L2_DL_RELEASE_REQ, l2_pend_rel},
1751 {ST_L2_7, EV_L2_DL_RELEASE_REQ, l2_disconnect},
1752 {ST_L2_8, EV_L2_DL_RELEASE_REQ, l2_disconnect},
1753 {ST_L2_5, EV_L2_DL_DATA, l2_feed_i_if_reest},
1754 {ST_L2_7, EV_L2_DL_DATA, l2_feed_i_pull},
1755 {ST_L2_8, EV_L2_DL_DATA, l2_feed_iqueue},
1756 {ST_L2_1, EV_L2_DL_UNITDATA, l2_queue_ui_assign},
1757 {ST_L2_2, EV_L2_DL_UNITDATA, l2_queue_ui},
1758 {ST_L2_3, EV_L2_DL_UNITDATA, l2_queue_ui},
1759 {ST_L2_4, EV_L2_DL_UNITDATA, l2_send_ui},
1760 {ST_L2_5, EV_L2_DL_UNITDATA, l2_send_ui},
1761 {ST_L2_6, EV_L2_DL_UNITDATA, l2_send_ui},
1762 {ST_L2_7, EV_L2_DL_UNITDATA, l2_send_ui},
1763 {ST_L2_8, EV_L2_DL_UNITDATA, l2_send_ui},
1764 {ST_L2_1, EV_L2_MDL_ASSIGN, l2_got_tei},
1765 {ST_L2_2, EV_L2_MDL_ASSIGN, l2_got_tei},
1766 {ST_L2_3, EV_L2_MDL_ASSIGN, l2_got_tei},
1767 {ST_L2_2, EV_L2_MDL_ERROR, l2_st24_tei_remove},
1768 {ST_L2_3, EV_L2_MDL_ERROR, l2_st3_tei_remove},
1769 {ST_L2_4, EV_L2_MDL_REMOVE, l2_st24_tei_remove},
1770 {ST_L2_5, EV_L2_MDL_REMOVE, l2_st5_tei_remove},
1771 {ST_L2_6, EV_L2_MDL_REMOVE, l2_st6_tei_remove},
1772 {ST_L2_7, EV_L2_MDL_REMOVE, l2_tei_remove},
1773 {ST_L2_8, EV_L2_MDL_REMOVE, l2_tei_remove},
1774 {ST_L2_4, EV_L2_SABME, l2_start_multi},
1775 {ST_L2_5, EV_L2_SABME, l2_send_UA},
1776 {ST_L2_6, EV_L2_SABME, l2_send_DM},
1777 {ST_L2_7, EV_L2_SABME, l2_restart_multi},
1778 {ST_L2_8, EV_L2_SABME, l2_restart_multi},
1779 {ST_L2_4, EV_L2_DISC, l2_send_DM},
1780 {ST_L2_5, EV_L2_DISC, l2_send_DM},
1781 {ST_L2_6, EV_L2_DISC, l2_send_UA},
1782 {ST_L2_7, EV_L2_DISC, l2_stop_multi},
1783 {ST_L2_8, EV_L2_DISC, l2_stop_multi},
1784 {ST_L2_4, EV_L2_UA, l2_mdl_error_ua},
1785 {ST_L2_5, EV_L2_UA, l2_connected},
1786 {ST_L2_6, EV_L2_UA, l2_released},
1787 {ST_L2_7, EV_L2_UA, l2_mdl_error_ua},
1788 {ST_L2_8, EV_L2_UA, l2_mdl_error_ua},
1789 {ST_L2_4, EV_L2_DM, l2_reestablish},
1790 {ST_L2_5, EV_L2_DM, l2_st5_dm_release},
1791 {ST_L2_6, EV_L2_DM, l2_st6_dm_release},
1792 {ST_L2_7, EV_L2_DM, l2_mdl_error_dm},
1793 {ST_L2_8, EV_L2_DM, l2_st8_mdl_error_dm},
1794 {ST_L2_1, EV_L2_UI, l2_got_ui},
1795 {ST_L2_2, EV_L2_UI, l2_got_ui},
1796 {ST_L2_3, EV_L2_UI, l2_got_ui},
1797 {ST_L2_4, EV_L2_UI, l2_got_ui},
1798 {ST_L2_5, EV_L2_UI, l2_got_ui},
1799 {ST_L2_6, EV_L2_UI, l2_got_ui},
1800 {ST_L2_7, EV_L2_UI, l2_got_ui},
1801 {ST_L2_8, EV_L2_UI, l2_got_ui},
1802 {ST_L2_7, EV_L2_FRMR, l2_got_FRMR},
1803 {ST_L2_8, EV_L2_FRMR, l2_got_FRMR},
1804 {ST_L2_7, EV_L2_SUPER, l2_st7_got_super},
1805 {ST_L2_8, EV_L2_SUPER, l2_st8_got_super},
1806 {ST_L2_7, EV_L2_I, l2_got_iframe},
1807 {ST_L2_8, EV_L2_I, l2_got_iframe},
1808 {ST_L2_5, EV_L2_T200, l2_st5_tout_200},
1809 {ST_L2_6, EV_L2_T200, l2_st6_tout_200},
1810 {ST_L2_7, EV_L2_T200, l2_st7_tout_200},
1811 {ST_L2_8, EV_L2_T200, l2_st8_tout_200},
1812 {ST_L2_7, EV_L2_T203, l2_st7_tout_203},
1813 {ST_L2_7, EV_L2_ACK_PULL, l2_pull_iqueue},
1814 {ST_L2_7, EV_L2_SET_OWN_BUSY, l2_set_own_busy},
1815 {ST_L2_8, EV_L2_SET_OWN_BUSY, l2_set_own_busy},
1816 {ST_L2_7, EV_L2_CLEAR_OWN_BUSY, l2_clear_own_busy},
1817 {ST_L2_8, EV_L2_CLEAR_OWN_BUSY, l2_clear_own_busy},
1818 {ST_L2_4, EV_L2_FRAME_ERROR, l2_frame_error},
1819 {ST_L2_5, EV_L2_FRAME_ERROR, l2_frame_error},
1820 {ST_L2_6, EV_L2_FRAME_ERROR, l2_frame_error},
1821 {ST_L2_7, EV_L2_FRAME_ERROR, l2_frame_error_reest},
1822 {ST_L2_8, EV_L2_FRAME_ERROR, l2_frame_error_reest},
1823 {ST_L2_1, EV_L1_DEACTIVATE, l2_st14_persistant_da},
1824 {ST_L2_2, EV_L1_DEACTIVATE, l2_st24_tei_remove},
1825 {ST_L2_3, EV_L1_DEACTIVATE, l2_st3_tei_remove},
1826 {ST_L2_4, EV_L1_DEACTIVATE, l2_st14_persistant_da},
1827 {ST_L2_5, EV_L1_DEACTIVATE, l2_st5_persistant_da},
1828 {ST_L2_6, EV_L1_DEACTIVATE, l2_st6_persistant_da},
1829 {ST_L2_7, EV_L1_DEACTIVATE, l2_persistant_da},
1830 {ST_L2_8, EV_L1_DEACTIVATE, l2_persistant_da},
1833 #define L2_FN_COUNT (sizeof(L2FnList)/sizeof(struct FsmNode))
1836 ph_data_indication(struct layer2 *l2, struct mISDNhead *hh, struct sk_buff *skb)
1838 u_char *datap = skb->data;
1845 if (skb->len <= l) {
1846 mISDN_FsmEvent(&l2->l2m, EV_L2_FRAME_ERROR, (void *) 'N');
1849 if (test_bit(FLG_LAPD, &l2->flag)) { /* Maybe not needed */
1852 if ((psapi & 1) || !(ptei & 1)) {
1854 "l2 D-channel frame wrong EA0/EA1\n");
1859 if (psapi != l2->sapi) {
1860 /* not our bussiness
1861 * printk(KERN_DEBUG "%s: sapi %d/%d sapi mismatch\n",
1868 if ((ptei != l2->tei) && (ptei != GROUP_TEI)) {
1869 /* not our bussiness
1870 * printk(KERN_DEBUG "%s: tei %d/%d sapi %d mismatch\n",
1872 * ptei, l2->tei, psapi);
1879 if (!(*datap & 1)) { /* I-Frame */
1880 c = iframe_error(l2, skb);
1882 ret = mISDN_FsmEvent(&l2->l2m, EV_L2_I, skb);
1883 } else if (IsSFrame(datap, l2)) { /* S-Frame */
1884 c = super_error(l2, skb);
1886 ret = mISDN_FsmEvent(&l2->l2m, EV_L2_SUPER, skb);
1887 } else if (IsUI(datap)) {
1888 c = UI_error(l2, skb);
1890 ret = mISDN_FsmEvent(&l2->l2m, EV_L2_UI, skb);
1891 } else if (IsSABME(datap, l2)) {
1892 c = unnum_error(l2, skb, CMD);
1894 ret = mISDN_FsmEvent(&l2->l2m, EV_L2_SABME, skb);
1895 } else if (IsUA(datap)) {
1896 c = unnum_error(l2, skb, RSP);
1898 ret = mISDN_FsmEvent(&l2->l2m, EV_L2_UA, skb);
1899 } else if (IsDISC(datap)) {
1900 c = unnum_error(l2, skb, CMD);
1902 ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DISC, skb);
1903 } else if (IsDM(datap)) {
1904 c = unnum_error(l2, skb, RSP);
1906 ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DM, skb);
1907 } else if (IsFRMR(datap)) {
1908 c = FRMR_error(l2, skb);
1910 ret = mISDN_FsmEvent(&l2->l2m, EV_L2_FRMR, skb);
1914 printk(KERN_WARNING "l2 D-channel frame error %c\n", c);
1915 mISDN_FsmEvent(&l2->l2m, EV_L2_FRAME_ERROR, (void *)(long)c);
1921 l2_send(struct mISDNchannel *ch, struct sk_buff *skb)
1923 struct layer2 *l2 = container_of(ch, struct layer2, ch);
1924 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1927 if (*debug & DEBUG_L2_RECV)
1928 printk(KERN_DEBUG "%s: prim(%x) id(%x) tei(%d)\n",
1929 __func__, hh->prim, hh->id, l2->tei);
1932 ret = ph_data_indication(l2, hh, skb);
1935 ret = ph_data_confirm(l2, hh, skb);
1937 case PH_ACTIVATE_IND:
1938 test_and_set_bit(FLG_L1_ACTIV, &l2->flag);
1939 l2up_create(l2, MPH_ACTIVATE_IND, 0, NULL);
1940 if (test_and_clear_bit(FLG_ESTAB_PEND, &l2->flag))
1941 ret = mISDN_FsmEvent(&l2->l2m,
1942 EV_L2_DL_ESTABLISH_REQ, skb);
1944 case PH_DEACTIVATE_IND:
1945 test_and_clear_bit(FLG_L1_ACTIV, &l2->flag);
1946 l2up_create(l2, MPH_DEACTIVATE_IND, 0, NULL);
1947 ret = mISDN_FsmEvent(&l2->l2m, EV_L1_DEACTIVATE, skb);
1949 case MPH_INFORMATION_IND:
1952 ret = l2->up->send(l2->up, skb);
1955 ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DL_DATA, skb);
1957 case DL_UNITDATA_REQ:
1958 ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DL_UNITDATA, skb);
1960 case DL_ESTABLISH_REQ:
1961 if (test_bit(FLG_LAPB, &l2->flag))
1962 test_and_set_bit(FLG_ORIG, &l2->flag);
1963 if (test_bit(FLG_L1_ACTIV, &l2->flag)) {
1964 if (test_bit(FLG_LAPD, &l2->flag) ||
1965 test_bit(FLG_ORIG, &l2->flag))
1966 ret = mISDN_FsmEvent(&l2->l2m,
1967 EV_L2_DL_ESTABLISH_REQ, skb);
1969 if (test_bit(FLG_LAPD, &l2->flag) ||
1970 test_bit(FLG_ORIG, &l2->flag)) {
1971 test_and_set_bit(FLG_ESTAB_PEND,
1974 ret = l2down(l2, PH_ACTIVATE_REQ, l2_newid(l2),
1978 case DL_RELEASE_REQ:
1979 if (test_bit(FLG_LAPB, &l2->flag))
1980 l2down_create(l2, PH_DEACTIVATE_REQ,
1981 l2_newid(l2), 0, NULL);
1982 ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DL_RELEASE_REQ,
1986 if (*debug & DEBUG_L2)
1987 l2m_debug(&l2->l2m, "l2 unknown pr %04x",
1998 tei_l2(struct layer2 *l2, u_int cmd, u_long arg)
2002 if (*debug & DEBUG_L2_TEI)
2003 printk(KERN_DEBUG "%s: cmd(%x)\n", __func__, cmd);
2005 case (MDL_ASSIGN_REQ):
2006 ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_ASSIGN, (void *)arg);
2008 case (MDL_REMOVE_REQ):
2009 ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_REMOVE, NULL);
2011 case (MDL_ERROR_IND):
2012 ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_ERROR, NULL);
2014 case (MDL_ERROR_RSP):
2015 /* ETS 300-125 5.3.2.1 Test: TC13010 */
2016 printk(KERN_NOTICE "MDL_ERROR|REQ (tei_l2)\n");
2017 ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_ERROR, NULL);
2024 release_l2(struct layer2 *l2)
2026 mISDN_FsmDelTimer(&l2->t200, 21);
2027 mISDN_FsmDelTimer(&l2->t203, 16);
2028 skb_queue_purge(&l2->i_queue);
2029 skb_queue_purge(&l2->ui_queue);
2030 skb_queue_purge(&l2->down_queue);
2032 if (test_bit(FLG_LAPD, &l2->flag)) {
2035 l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D,
2036 CLOSE_CHANNEL, NULL);
2042 l2_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
2044 struct layer2 *l2 = container_of(ch, struct layer2, ch);
2047 if (*debug & DEBUG_L2_CTRL)
2048 printk(KERN_DEBUG "%s:(%x)\n", __func__, cmd);
2052 if (test_bit(FLG_LAPD, &l2->flag)) {
2053 set_channel_address(&l2->ch, l2->sapi, l2->tei);
2054 info = DL_INFO_L2_CONNECT;
2055 l2up_create(l2, DL_INFORMATION_IND,
2056 sizeof(info), &info);
2061 l2->ch.peer->ctrl(l2->ch.peer, CLOSE_CHANNEL, NULL);
2069 create_l2(struct mISDNchannel *ch, u_int protocol, u_long options, u_long arg)
2072 struct channel_req rq;
2074 l2 = kzalloc(sizeof(struct layer2), GFP_KERNEL);
2076 printk(KERN_ERR "kzalloc layer2 failed\n");
2080 l2->down_id = MISDN_ID_NONE;
2083 l2->ch.send = l2_send;
2084 l2->ch.ctrl = l2_ctrl;
2086 case ISDN_P_LAPD_NT:
2087 test_and_set_bit(FLG_LAPD, &l2->flag);
2088 test_and_set_bit(FLG_LAPD_NET, &l2->flag);
2089 test_and_set_bit(FLG_MOD128, &l2->flag);
2091 l2->maxlen = MAX_DFRAME_LEN;
2092 if (test_bit(OPTION_L2_PMX, &options))
2096 if (test_bit(OPTION_L2_PTP, &options))
2097 test_and_set_bit(FLG_PTP, &l2->flag);
2098 if (test_bit(OPTION_L2_FIXEDTEI, &options))
2099 test_and_set_bit(FLG_FIXED_TEI, &l2->flag);
2100 l2->tei = (u_int)arg;
2104 if (test_bit(OPTION_L2_PMX, &options))
2105 rq.protocol = ISDN_P_NT_E1;
2107 rq.protocol = ISDN_P_NT_S0;
2109 l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D, OPEN_CHANNEL, &rq);
2111 case ISDN_P_LAPD_TE:
2112 test_and_set_bit(FLG_LAPD, &l2->flag);
2113 test_and_set_bit(FLG_MOD128, &l2->flag);
2114 test_and_set_bit(FLG_ORIG, &l2->flag);
2116 l2->maxlen = MAX_DFRAME_LEN;
2117 if (test_bit(OPTION_L2_PMX, &options))
2121 if (test_bit(OPTION_L2_PTP, &options))
2122 test_and_set_bit(FLG_PTP, &l2->flag);
2123 if (test_bit(OPTION_L2_FIXEDTEI, &options))
2124 test_and_set_bit(FLG_FIXED_TEI, &l2->flag);
2125 l2->tei = (u_int)arg;
2129 if (test_bit(OPTION_L2_PMX, &options))
2130 rq.protocol = ISDN_P_TE_E1;
2132 rq.protocol = ISDN_P_TE_S0;
2134 l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D, OPEN_CHANNEL, &rq);
2136 case ISDN_P_B_X75SLP:
2137 test_and_set_bit(FLG_LAPB, &l2->flag);
2139 l2->maxlen = MAX_DATA_SIZE;
2147 printk(KERN_ERR "layer2 create failed prt %x\n",
2152 skb_queue_head_init(&l2->i_queue);
2153 skb_queue_head_init(&l2->ui_queue);
2154 skb_queue_head_init(&l2->down_queue);
2155 skb_queue_head_init(&l2->tmp_queue);
2157 l2->l2m.fsm = &l2fsm;
2158 if (test_bit(FLG_LAPB, &l2->flag) ||
2159 test_bit(FLG_PTP, &l2->flag) ||
2160 test_bit(FLG_LAPD_NET, &l2->flag))
2161 l2->l2m.state = ST_L2_4;
2163 l2->l2m.state = ST_L2_1;
2164 l2->l2m.debug = *debug;
2165 l2->l2m.userdata = l2;
2166 l2->l2m.userint = 0;
2167 l2->l2m.printdebug = l2m_debug;
2169 mISDN_FsmInitTimer(&l2->l2m, &l2->t200);
2170 mISDN_FsmInitTimer(&l2->l2m, &l2->t203);
2175 x75create(struct channel_req *crq)
2179 if (crq->protocol != ISDN_P_B_X75SLP)
2180 return -EPROTONOSUPPORT;
2181 l2 = create_l2(crq->ch, crq->protocol, 0, 0);
2185 crq->protocol = ISDN_P_B_HDLC;
2189 static struct Bprotocol X75SLP = {
2190 .Bprotocols = (1 << (ISDN_P_B_X75SLP & ISDN_P_B_MASK)),
2196 Isdnl2_Init(u_int *deb)
2199 mISDN_register_Bprotocol(&X75SLP);
2200 l2fsm.state_count = L2_STATE_COUNT;
2201 l2fsm.event_count = L2_EVENT_COUNT;
2202 l2fsm.strEvent = strL2Event;
2203 l2fsm.strState = strL2State;
2204 mISDN_FsmNew(&l2fsm, L2FnList, ARRAY_SIZE(L2FnList));
2210 Isdnl2_cleanup(void)
2212 mISDN_unregister_Bprotocol(&X75SLP);
2214 mISDN_FsmFree(&l2fsm);