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.
18 #include <linux/random.h>
19 #include <linux/slab.h>
30 #define TEI_ENTITY_ID 0xf
32 #define MGR_PH_ACTIVE 16
33 #define MGR_PH_NOTREADY 17
35 #define DATIMER_VAL 10000
39 static struct Fsm deactfsm = {NULL, 0, 0, NULL, NULL};
40 static struct Fsm teifsmu = {NULL, 0, 0, NULL, NULL};
41 static struct Fsm teifsmn = {NULL, 0, 0, NULL, NULL};
48 #define DEACT_STATE_COUNT (ST_L1_ACTIV + 1)
50 static char *strDeactState[] =
53 "ST_L1_DEACT_PENDING",
66 #define DEACT_EVENT_COUNT (EV_DATIMER + 1)
68 static char *strDeactEvent[] =
79 da_debug(struct FsmInst *fi, char *fmt, ...)
81 struct manager *mgr = fi->userdata;
85 if (!(*debug & DEBUG_L2_TEIFSM))
93 printk(KERN_DEBUG "mgr(%d): %pV\n", mgr->ch.st->dev->id, &vaf);
99 da_activate(struct FsmInst *fi, int event, void *arg)
101 struct manager *mgr = fi->userdata;
103 if (fi->state == ST_L1_DEACT_PENDING)
104 mISDN_FsmDelTimer(&mgr->datimer, 1);
105 mISDN_FsmChangeState(fi, ST_L1_ACTIV);
109 da_deactivate_ind(struct FsmInst *fi, int event, void *arg)
111 mISDN_FsmChangeState(fi, ST_L1_DEACT);
115 da_deactivate(struct FsmInst *fi, int event, void *arg)
117 struct manager *mgr = fi->userdata;
121 read_lock_irqsave(&mgr->lock, flags);
122 list_for_each_entry(l2, &mgr->layer2, list) {
123 if (l2->l2m.state > ST_L2_4) {
124 /* have still activ TEI */
125 read_unlock_irqrestore(&mgr->lock, flags);
129 read_unlock_irqrestore(&mgr->lock, flags);
130 /* All TEI are inactiv */
131 if (!test_bit(OPTION_L1_HOLD, &mgr->options)) {
132 mISDN_FsmAddTimer(&mgr->datimer, DATIMER_VAL, EV_DATIMER,
134 mISDN_FsmChangeState(fi, ST_L1_DEACT_PENDING);
139 da_ui(struct FsmInst *fi, int event, void *arg)
141 struct manager *mgr = fi->userdata;
143 /* restart da timer */
144 if (!test_bit(OPTION_L1_HOLD, &mgr->options)) {
145 mISDN_FsmDelTimer(&mgr->datimer, 2);
146 mISDN_FsmAddTimer(&mgr->datimer, DATIMER_VAL, EV_DATIMER,
152 da_timer(struct FsmInst *fi, int event, void *arg)
154 struct manager *mgr = fi->userdata;
159 read_lock_irqsave(&mgr->lock, flags);
160 list_for_each_entry(l2, &mgr->layer2, list) {
161 if (l2->l2m.state > ST_L2_4) {
162 /* have still activ TEI */
163 read_unlock_irqrestore(&mgr->lock, flags);
164 mISDN_FsmChangeState(fi, ST_L1_ACTIV);
168 read_unlock_irqrestore(&mgr->lock, flags);
169 /* All TEI are inactiv */
170 mISDN_FsmChangeState(fi, ST_L1_DEACT);
171 _queue_data(&mgr->ch, PH_DEACTIVATE_REQ, MISDN_ID_ANY, 0, NULL,
175 static struct FsmNode DeactFnList[] =
177 {ST_L1_DEACT, EV_ACTIVATE_IND, da_activate},
178 {ST_L1_ACTIV, EV_DEACTIVATE_IND, da_deactivate_ind},
179 {ST_L1_ACTIV, EV_DEACTIVATE, da_deactivate},
180 {ST_L1_DEACT_PENDING, EV_ACTIVATE, da_activate},
181 {ST_L1_DEACT_PENDING, EV_UI, da_ui},
182 {ST_L1_DEACT_PENDING, EV_DATIMER, da_timer},
191 #define TEI_STATE_COUNT (ST_TEI_IDVERIFY + 1)
193 static char *strTeiState[] =
212 #define TEI_EVENT_COUNT (EV_TIMER + 1)
214 static char *strTeiEvent[] =
228 tei_debug(struct FsmInst *fi, char *fmt, ...)
230 struct teimgr *tm = fi->userdata;
231 struct va_format vaf;
234 if (!(*debug & DEBUG_L2_TEIFSM))
242 printk(KERN_DEBUG "sapi(%d) tei(%d): %pV\n",
243 tm->l2->sapi, tm->l2->tei, &vaf);
251 get_free_id(struct manager *mgr)
257 list_for_each_entry(l2, &mgr->layer2, list) {
258 if (l2->ch.nr > 63) {
260 "%s: more as 63 layer2 for one device\n",
264 test_and_set_bit(l2->ch.nr, (u_long *)&ids);
266 for (i = 1; i < 64; i++)
267 if (!test_bit(i, (u_long *)&ids))
269 printk(KERN_WARNING "%s: more as 63 layer2 for one device\n",
275 get_free_tei(struct manager *mgr)
281 list_for_each_entry(l2, &mgr->layer2, list) {
284 if ((l2->ch.addr & 0xff) != 0)
286 i = l2->ch.addr >> 8;
291 test_and_set_bit(i, (u_long *)&ids);
293 for (i = 0; i < 64; i++)
294 if (!test_bit(i, (u_long *)&ids))
296 printk(KERN_WARNING "%s: more as 63 dynamic tei for one device\n",
302 teiup_create(struct manager *mgr, u_int prim, int len, void *arg)
305 struct mISDNhead *hh;
308 skb = mI_alloc_skb(len, GFP_ATOMIC);
311 hh = mISDN_HEAD_P(skb);
313 hh->id = (mgr->ch.nr << 16) | mgr->ch.addr;
315 memcpy(skb_put(skb, len), arg, len);
316 err = mgr->up->send(mgr->up, skb);
318 printk(KERN_WARNING "%s: err=%d\n", __func__, err);
324 new_id(struct manager *mgr)
332 id |= GROUP_TEI << 8;
338 do_send(struct manager *mgr)
340 if (!test_bit(MGR_PH_ACTIVE, &mgr->options))
343 if (!test_and_set_bit(MGR_PH_NOTREADY, &mgr->options)) {
344 struct sk_buff *skb = skb_dequeue(&mgr->sendq);
347 test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
350 mgr->lastid = mISDN_HEAD_ID(skb);
351 mISDN_FsmEvent(&mgr->deact, EV_UI, NULL);
352 if (mgr->ch.recv(mgr->ch.peer, skb)) {
354 test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
355 mgr->lastid = MISDN_ID_NONE;
361 do_ack(struct manager *mgr, u_int id)
363 if (test_bit(MGR_PH_NOTREADY, &mgr->options)) {
364 if (id == mgr->lastid) {
365 if (test_bit(MGR_PH_ACTIVE, &mgr->options)) {
368 skb = skb_dequeue(&mgr->sendq);
370 mgr->lastid = mISDN_HEAD_ID(skb);
371 if (!mgr->ch.recv(mgr->ch.peer, skb))
376 mgr->lastid = MISDN_ID_NONE;
377 test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
383 mgr_send_down(struct manager *mgr, struct sk_buff *skb)
385 skb_queue_tail(&mgr->sendq, skb);
386 if (!test_bit(MGR_PH_ACTIVE, &mgr->options)) {
387 _queue_data(&mgr->ch, PH_ACTIVATE_REQ, MISDN_ID_ANY, 0,
395 dl_unit_data(struct manager *mgr, struct sk_buff *skb)
397 if (!test_bit(MGR_OPT_NETWORK, &mgr->options)) /* only net send UI */
399 if (!test_bit(MGR_PH_ACTIVE, &mgr->options))
400 _queue_data(&mgr->ch, PH_ACTIVATE_REQ, MISDN_ID_ANY, 0,
403 skb->data[0] = 0x02; /* SAPI 0 C/R = 1 */
404 skb->data[1] = 0xff; /* TEI 127 */
405 skb->data[2] = UI; /* UI frame */
406 mISDN_HEAD_PRIM(skb) = PH_DATA_REQ;
407 mISDN_HEAD_ID(skb) = new_id(mgr);
408 skb_queue_tail(&mgr->sendq, skb);
418 get_random_bytes(&x, sizeof(x));
422 static struct layer2 *
423 findtei(struct manager *mgr, int tei)
428 read_lock_irqsave(&mgr->lock, flags);
429 list_for_each_entry(l2, &mgr->layer2, list) {
430 if ((l2->sapi == 0) && (l2->tei > 0) &&
431 (l2->tei != GROUP_TEI) && (l2->tei == tei))
436 read_unlock_irqrestore(&mgr->lock, flags);
441 put_tei_msg(struct manager *mgr, u_char m_id, unsigned int ri, int tei)
446 bp[0] = (TEI_SAPI << 2);
447 if (test_bit(MGR_OPT_NETWORK, &mgr->options))
448 bp[0] |= 2; /* CR:=1 for net command */
449 bp[1] = (GROUP_TEI << 1) | 0x1;
451 bp[3] = TEI_ENTITY_ID;
455 bp[7] = ((tei << 1) & 0xff) | 1;
456 skb = _alloc_mISDN_skb(PH_DATA_REQ, new_id(mgr), 8, bp, GFP_ATOMIC);
458 printk(KERN_WARNING "%s: no skb for tei msg\n", __func__);
461 mgr_send_down(mgr, skb);
465 tei_id_request(struct FsmInst *fi, int event, void *arg)
467 struct teimgr *tm = fi->userdata;
469 if (tm->l2->tei != GROUP_TEI) {
470 tm->tei_m.printdebug(&tm->tei_m,
471 "assign request for already assigned tei %d",
475 tm->ri = random_ri();
476 if (*debug & DEBUG_L2_TEI)
477 tm->tei_m.printdebug(&tm->tei_m,
478 "assign request ri %d", tm->ri);
479 put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI);
480 mISDN_FsmChangeState(fi, ST_TEI_IDREQ);
481 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 1);
486 tei_id_assign(struct FsmInst *fi, int event, void *arg)
488 struct teimgr *tm = fi->userdata;
493 ri = ((unsigned int) *dp++ << 8);
497 if (*debug & DEBUG_L2_TEI)
498 tm->tei_m.printdebug(fi, "identity assign ri %d tei %d",
500 l2 = findtei(tm->mgr, tei);
501 if (l2) { /* same tei is in use */
502 if (ri != l2->tm->ri) {
503 tm->tei_m.printdebug(fi,
504 "possible duplicate assignment tei %d", tei);
505 tei_l2(l2, MDL_ERROR_RSP, 0);
507 } else if (ri == tm->ri) {
508 mISDN_FsmDelTimer(&tm->timer, 1);
509 mISDN_FsmChangeState(fi, ST_TEI_NOP);
510 tei_l2(tm->l2, MDL_ASSIGN_REQ, tei);
515 tei_id_test_dup(struct FsmInst *fi, int event, void *arg)
517 struct teimgr *tm = fi->userdata;
522 ri = ((unsigned int) *dp++ << 8);
526 if (*debug & DEBUG_L2_TEI)
527 tm->tei_m.printdebug(fi, "foreign identity assign ri %d tei %d",
529 l2 = findtei(tm->mgr, tei);
530 if (l2) { /* same tei is in use */
531 if (ri != l2->tm->ri) { /* and it wasn't our request */
532 tm->tei_m.printdebug(fi,
533 "possible duplicate assignment tei %d", tei);
534 mISDN_FsmEvent(&l2->tm->tei_m, EV_VERIFY, NULL);
540 tei_id_denied(struct FsmInst *fi, int event, void *arg)
542 struct teimgr *tm = fi->userdata;
546 ri = ((unsigned int) *dp++ << 8);
550 if (*debug & DEBUG_L2_TEI)
551 tm->tei_m.printdebug(fi, "identity denied ri %d tei %d",
556 tei_id_chk_req(struct FsmInst *fi, int event, void *arg)
558 struct teimgr *tm = fi->userdata;
562 tei = *(dp + 3) >> 1;
563 if (*debug & DEBUG_L2_TEI)
564 tm->tei_m.printdebug(fi, "identity check req tei %d", tei);
565 if ((tm->l2->tei != GROUP_TEI) && ((tei == GROUP_TEI) ||
566 (tei == tm->l2->tei))) {
567 mISDN_FsmDelTimer(&tm->timer, 4);
568 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP);
569 put_tei_msg(tm->mgr, ID_CHK_RES, random_ri(), tm->l2->tei);
574 tei_id_remove(struct FsmInst *fi, int event, void *arg)
576 struct teimgr *tm = fi->userdata;
580 tei = *(dp + 3) >> 1;
581 if (*debug & DEBUG_L2_TEI)
582 tm->tei_m.printdebug(fi, "identity remove tei %d", tei);
583 if ((tm->l2->tei != GROUP_TEI) &&
584 ((tei == GROUP_TEI) || (tei == tm->l2->tei))) {
585 mISDN_FsmDelTimer(&tm->timer, 5);
586 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP);
587 tei_l2(tm->l2, MDL_REMOVE_REQ, 0);
592 tei_id_verify(struct FsmInst *fi, int event, void *arg)
594 struct teimgr *tm = fi->userdata;
596 if (*debug & DEBUG_L2_TEI)
597 tm->tei_m.printdebug(fi, "id verify request for tei %d",
599 put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei);
600 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY);
601 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2);
606 tei_id_req_tout(struct FsmInst *fi, int event, void *arg)
608 struct teimgr *tm = fi->userdata;
611 tm->ri = random_ri();
612 if (*debug & DEBUG_L2_TEI)
613 tm->tei_m.printdebug(fi, "assign req(%d) ri %d",
614 4 - tm->nval, tm->ri);
615 put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI);
616 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 3);
618 tm->tei_m.printdebug(fi, "assign req failed");
619 tei_l2(tm->l2, MDL_ERROR_RSP, 0);
620 mISDN_FsmChangeState(fi, ST_TEI_NOP);
625 tei_id_ver_tout(struct FsmInst *fi, int event, void *arg)
627 struct teimgr *tm = fi->userdata;
630 if (*debug & DEBUG_L2_TEI)
631 tm->tei_m.printdebug(fi,
632 "id verify req(%d) for tei %d",
633 3 - tm->nval, tm->l2->tei);
634 put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei);
635 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4);
637 tm->tei_m.printdebug(fi, "verify req for tei %d failed",
639 tei_l2(tm->l2, MDL_REMOVE_REQ, 0);
640 mISDN_FsmChangeState(fi, ST_TEI_NOP);
644 static struct FsmNode TeiFnListUser[] =
646 {ST_TEI_NOP, EV_IDREQ, tei_id_request},
647 {ST_TEI_NOP, EV_ASSIGN, tei_id_test_dup},
648 {ST_TEI_NOP, EV_VERIFY, tei_id_verify},
649 {ST_TEI_NOP, EV_REMOVE, tei_id_remove},
650 {ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req},
651 {ST_TEI_IDREQ, EV_TIMER, tei_id_req_tout},
652 {ST_TEI_IDREQ, EV_ASSIGN, tei_id_assign},
653 {ST_TEI_IDREQ, EV_DENIED, tei_id_denied},
654 {ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout},
655 {ST_TEI_IDVERIFY, EV_REMOVE, tei_id_remove},
656 {ST_TEI_IDVERIFY, EV_CHKREQ, tei_id_chk_req},
660 tei_l2remove(struct layer2 *l2)
662 put_tei_msg(l2->tm->mgr, ID_REMOVE, 0, l2->tei);
663 tei_l2(l2, MDL_REMOVE_REQ, 0);
664 list_del(&l2->ch.list);
665 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
669 tei_assign_req(struct FsmInst *fi, int event, void *arg)
671 struct teimgr *tm = fi->userdata;
674 if (tm->l2->tei == GROUP_TEI) {
675 tm->tei_m.printdebug(&tm->tei_m,
676 "net tei assign request without tei");
679 tm->ri = ((unsigned int) *dp++ << 8);
681 if (*debug & DEBUG_L2_TEI)
682 tm->tei_m.printdebug(&tm->tei_m,
683 "net assign request ri %d teim %d", tm->ri, *dp);
684 put_tei_msg(tm->mgr, ID_ASSIGNED, tm->ri, tm->l2->tei);
685 mISDN_FsmChangeState(fi, ST_TEI_NOP);
689 tei_id_chk_req_net(struct FsmInst *fi, int event, void *arg)
691 struct teimgr *tm = fi->userdata;
693 if (*debug & DEBUG_L2_TEI)
694 tm->tei_m.printdebug(fi, "id check request for tei %d",
697 put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei);
698 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY);
699 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2);
704 tei_id_chk_resp(struct FsmInst *fi, int event, void *arg)
706 struct teimgr *tm = fi->userdata;
711 if (*debug & DEBUG_L2_TEI)
712 tm->tei_m.printdebug(fi, "identity check resp tei %d", tei);
713 if (tei == tm->l2->tei)
718 tei_id_verify_net(struct FsmInst *fi, int event, void *arg)
720 struct teimgr *tm = fi->userdata;
725 if (*debug & DEBUG_L2_TEI)
726 tm->tei_m.printdebug(fi, "identity verify req tei %d/%d",
728 if (tei == tm->l2->tei)
729 tei_id_chk_req_net(fi, event, arg);
733 tei_id_ver_tout_net(struct FsmInst *fi, int event, void *arg)
735 struct teimgr *tm = fi->userdata;
738 if (*debug & DEBUG_L2_TEI)
739 tm->tei_m.printdebug(fi,
740 "check req for tei %d successful\n", tm->l2->tei);
741 mISDN_FsmChangeState(fi, ST_TEI_NOP);
742 } else if (tm->rcnt > 1) {
743 /* duplicate assignment; remove */
744 tei_l2remove(tm->l2);
745 } else if (--tm->nval) {
746 if (*debug & DEBUG_L2_TEI)
747 tm->tei_m.printdebug(fi,
748 "id check req(%d) for tei %d",
749 3 - tm->nval, tm->l2->tei);
750 put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei);
751 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4);
753 tm->tei_m.printdebug(fi, "check req for tei %d failed",
755 mISDN_FsmChangeState(fi, ST_TEI_NOP);
756 tei_l2remove(tm->l2);
760 static struct FsmNode TeiFnListNet[] =
762 {ST_TEI_NOP, EV_ASSIGN_REQ, tei_assign_req},
763 {ST_TEI_NOP, EV_VERIFY, tei_id_verify_net},
764 {ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req_net},
765 {ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout_net},
766 {ST_TEI_IDVERIFY, EV_CHKRESP, tei_id_chk_resp},
770 tei_ph_data_ind(struct teimgr *tm, u_int mt, u_char *dp, int len)
772 if (test_bit(FLG_FIXED_TEI, &tm->l2->flag))
774 if (*debug & DEBUG_L2_TEI)
775 tm->tei_m.printdebug(&tm->tei_m, "tei handler mt %x", mt);
776 if (mt == ID_ASSIGNED)
777 mISDN_FsmEvent(&tm->tei_m, EV_ASSIGN, dp);
778 else if (mt == ID_DENIED)
779 mISDN_FsmEvent(&tm->tei_m, EV_DENIED, dp);
780 else if (mt == ID_CHK_REQ)
781 mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, dp);
782 else if (mt == ID_REMOVE)
783 mISDN_FsmEvent(&tm->tei_m, EV_REMOVE, dp);
784 else if (mt == ID_VERIFY)
785 mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, dp);
786 else if (mt == ID_CHK_RES)
787 mISDN_FsmEvent(&tm->tei_m, EV_CHKRESP, dp);
790 static struct layer2 *
791 create_new_tei(struct manager *mgr, int tei, int sapi)
800 if ((tei >= 0) && (tei < 64))
801 test_and_set_bit(OPTION_L2_FIXEDTEI, &opt);
802 if (mgr->ch.st->dev->Dprotocols
803 & ((1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1)))
804 test_and_set_bit(OPTION_L2_PMX, &opt);
805 l2 = create_l2(mgr->up, ISDN_P_LAPD_NT, opt, tei, sapi);
807 printk(KERN_WARNING "%s:no memory for layer2\n", __func__);
810 l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL);
813 printk(KERN_WARNING "%s:no memory for teimgr\n", __func__);
818 l2->tm->tei_m.debug = *debug & DEBUG_L2_TEIFSM;
819 l2->tm->tei_m.userdata = l2->tm;
820 l2->tm->tei_m.printdebug = tei_debug;
821 l2->tm->tei_m.fsm = &teifsmn;
822 l2->tm->tei_m.state = ST_TEI_NOP;
823 l2->tm->tval = 2000; /* T202 2 sec */
824 mISDN_FsmInitTimer(&l2->tm->tei_m, &l2->tm->timer);
825 write_lock_irqsave(&mgr->lock, flags);
826 id = get_free_id(mgr);
827 list_add_tail(&l2->list, &mgr->layer2);
828 write_unlock_irqrestore(&mgr->lock, flags);
830 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
831 printk(KERN_WARNING "%s:no free id\n", __func__);
835 __add_layer2(&l2->ch, mgr->ch.st);
836 l2->ch.recv = mgr->ch.recv;
837 l2->ch.peer = mgr->ch.peer;
838 l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL);
844 new_tei_req(struct manager *mgr, u_char *dp)
853 if (!(dp[3] & 1)) /* Extension bit != 1 */
856 tei = dp[3] >> 1; /* 3GPP TS 08.56 6.1.11.2 */
858 tei = get_free_tei(mgr);
860 printk(KERN_WARNING "%s:No free tei\n", __func__);
863 l2 = create_new_tei(mgr, tei, CTRL_SAPI);
867 mISDN_FsmEvent(&l2->tm->tei_m, EV_ASSIGN_REQ, dp);
870 put_tei_msg(mgr, ID_DENIED, ri, GROUP_TEI);
874 ph_data_ind(struct manager *mgr, struct sk_buff *skb)
877 struct layer2 *l2, *nl2;
881 if (*debug & DEBUG_L2_TEI)
882 printk(KERN_DEBUG "%s: short mgr frame %d/8\n",
887 if ((skb->data[0] >> 2) != TEI_SAPI) /* not for us */
889 if (skb->data[0] & 1) /* EA0 formal error */
891 if (!(skb->data[1] & 1)) /* EA1 formal error */
893 if ((skb->data[1] >> 1) != GROUP_TEI) /* not for us */
895 if ((skb->data[2] & 0xef) != UI) /* not UI */
897 if (skb->data[3] != TEI_ENTITY_ID) /* not tei entity */
904 if (!test_bit(MGR_OPT_NETWORK, &mgr->options))
911 if (test_bit(MGR_OPT_NETWORK, &mgr->options))
918 if (mt == ID_REQUEST) {
919 new_tei_req(mgr, &skb->data[4]);
922 list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
923 tei_ph_data_ind(l2->tm, mt, &skb->data[4], skb->len - 4);
930 l2_tei(struct layer2 *l2, u_int cmd, u_long arg)
932 struct teimgr *tm = l2->tm;
934 if (test_bit(FLG_FIXED_TEI, &l2->flag))
936 if (*debug & DEBUG_L2_TEI)
937 printk(KERN_DEBUG "%s: cmd(%x)\n", __func__, cmd);
940 mISDN_FsmEvent(&tm->tei_m, EV_IDREQ, NULL);
943 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
944 mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, &l2->tei);
945 if (test_bit(MGR_OPT_USER, &tm->mgr->options))
946 mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, NULL);
948 case MDL_STATUS_UP_IND:
949 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
950 mISDN_FsmEvent(&tm->mgr->deact, EV_ACTIVATE, NULL);
952 case MDL_STATUS_DOWN_IND:
953 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
954 mISDN_FsmEvent(&tm->mgr->deact, EV_DEACTIVATE, NULL);
956 case MDL_STATUS_UI_IND:
957 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
958 mISDN_FsmEvent(&tm->mgr->deact, EV_UI, NULL);
965 TEIrelease(struct layer2 *l2)
967 struct teimgr *tm = l2->tm;
970 mISDN_FsmDelTimer(&tm->timer, 1);
971 write_lock_irqsave(&tm->mgr->lock, flags);
973 write_unlock_irqrestore(&tm->mgr->lock, flags);
979 create_teimgr(struct manager *mgr, struct channel_req *crq)
986 if (*debug & DEBUG_L2_TEI)
987 printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n",
988 __func__, dev_name(&mgr->ch.st->dev->dev),
989 crq->protocol, crq->adr.dev, crq->adr.channel,
990 crq->adr.sapi, crq->adr.tei);
991 if (crq->adr.tei > GROUP_TEI)
993 if (crq->adr.tei < 64)
994 test_and_set_bit(OPTION_L2_FIXEDTEI, &opt);
995 if (crq->adr.tei == 0)
996 test_and_set_bit(OPTION_L2_PTP, &opt);
997 if (test_bit(MGR_OPT_NETWORK, &mgr->options)) {
998 if (crq->protocol == ISDN_P_LAPD_TE)
999 return -EPROTONOSUPPORT;
1000 if ((crq->adr.tei != 0) && (crq->adr.tei != 127))
1004 "%s: only one network manager is allowed\n",
1008 } else if (test_bit(MGR_OPT_USER, &mgr->options)) {
1009 if (crq->protocol == ISDN_P_LAPD_NT)
1010 return -EPROTONOSUPPORT;
1011 if ((crq->adr.tei >= 64) && (crq->adr.tei < GROUP_TEI))
1012 return -EINVAL; /* dyn tei */
1014 if (crq->protocol == ISDN_P_LAPD_NT)
1015 test_and_set_bit(MGR_OPT_NETWORK, &mgr->options);
1016 if (crq->protocol == ISDN_P_LAPD_TE)
1017 test_and_set_bit(MGR_OPT_USER, &mgr->options);
1019 if (mgr->ch.st->dev->Dprotocols
1020 & ((1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1)))
1021 test_and_set_bit(OPTION_L2_PMX, &opt);
1022 if ((crq->protocol == ISDN_P_LAPD_NT) && (crq->adr.tei == 127)) {
1024 id = DL_INFO_L2_CONNECT;
1025 teiup_create(mgr, DL_INFORMATION_IND, sizeof(id), &id);
1027 if (!list_empty(&mgr->layer2)) {
1028 read_lock_irqsave(&mgr->lock, flags);
1029 list_for_each_entry(l2, &mgr->layer2, list) {
1031 l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL);
1033 read_unlock_irqrestore(&mgr->lock, flags);
1037 l2 = create_l2(crq->ch, crq->protocol, opt,
1038 crq->adr.tei, crq->adr.sapi);
1041 l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL);
1044 printk(KERN_ERR "kmalloc teimgr failed\n");
1049 l2->tm->tei_m.debug = *debug & DEBUG_L2_TEIFSM;
1050 l2->tm->tei_m.userdata = l2->tm;
1051 l2->tm->tei_m.printdebug = tei_debug;
1052 if (crq->protocol == ISDN_P_LAPD_TE) {
1053 l2->tm->tei_m.fsm = &teifsmu;
1054 l2->tm->tei_m.state = ST_TEI_NOP;
1055 l2->tm->tval = 1000; /* T201 1 sec */
1057 l2->tm->tei_m.fsm = &teifsmn;
1058 l2->tm->tei_m.state = ST_TEI_NOP;
1059 l2->tm->tval = 2000; /* T202 2 sec */
1061 mISDN_FsmInitTimer(&l2->tm->tei_m, &l2->tm->timer);
1062 write_lock_irqsave(&mgr->lock, flags);
1063 id = get_free_id(mgr);
1064 list_add_tail(&l2->list, &mgr->layer2);
1065 write_unlock_irqrestore(&mgr->lock, flags);
1067 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1078 mgr_send(struct mISDNchannel *ch, struct sk_buff *skb)
1080 struct manager *mgr;
1081 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1084 mgr = container_of(ch, struct manager, ch);
1085 if (*debug & DEBUG_L2_RECV)
1086 printk(KERN_DEBUG "%s: prim(%x) id(%x)\n",
1087 __func__, hh->prim, hh->id);
1090 mISDN_FsmEvent(&mgr->deact, EV_UI, NULL);
1091 ret = ph_data_ind(mgr, skb);
1094 do_ack(mgr, hh->id);
1097 case PH_ACTIVATE_IND:
1098 test_and_set_bit(MGR_PH_ACTIVE, &mgr->options);
1099 mISDN_FsmEvent(&mgr->deact, EV_ACTIVATE_IND, NULL);
1103 case PH_DEACTIVATE_IND:
1104 test_and_clear_bit(MGR_PH_ACTIVE, &mgr->options);
1105 mISDN_FsmEvent(&mgr->deact, EV_DEACTIVATE_IND, NULL);
1108 case DL_UNITDATA_REQ:
1109 return dl_unit_data(mgr, skb);
1117 free_teimanager(struct manager *mgr)
1119 struct layer2 *l2, *nl2;
1121 test_and_clear_bit(OPTION_L1_HOLD, &mgr->options);
1122 if (test_bit(MGR_OPT_NETWORK, &mgr->options)) {
1123 /* not locked lock is taken in release tei */
1125 if (test_bit(OPTION_L2_CLEANUP, &mgr->options)) {
1126 list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1127 put_tei_msg(mgr, ID_REMOVE, 0, l2->tei);
1128 mutex_lock(&mgr->ch.st->lmutex);
1129 list_del(&l2->ch.list);
1130 mutex_unlock(&mgr->ch.st->lmutex);
1131 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1133 test_and_clear_bit(MGR_OPT_NETWORK, &mgr->options);
1135 list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1140 if (test_bit(MGR_OPT_USER, &mgr->options)) {
1141 if (list_empty(&mgr->layer2))
1142 test_and_clear_bit(MGR_OPT_USER, &mgr->options);
1144 mgr->ch.st->dev->D.ctrl(&mgr->ch.st->dev->D, CLOSE_CHANNEL, NULL);
1149 ctrl_teimanager(struct manager *mgr, void *arg)
1151 /* currently we only have one option */
1152 int *val = (int *)arg;
1158 test_and_set_bit(OPTION_L2_CLEANUP, &mgr->options);
1160 test_and_clear_bit(OPTION_L2_CLEANUP, &mgr->options);
1164 test_and_set_bit(OPTION_L1_HOLD, &mgr->options);
1166 test_and_clear_bit(OPTION_L1_HOLD, &mgr->options);
1174 /* This function does create a L2 for fixed TEI in NT Mode */
1176 check_data(struct manager *mgr, struct sk_buff *skb)
1178 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1182 if (*debug & DEBUG_L2_CTRL)
1183 printk(KERN_DEBUG "%s: prim(%x) id(%x)\n",
1184 __func__, hh->prim, hh->id);
1185 if (test_bit(MGR_OPT_USER, &mgr->options))
1187 if (hh->prim != PH_DATA_IND)
1191 if (skb->data[0] & 3) /* EA0 and CR must be 0 */
1193 sapi = skb->data[0] >> 2;
1194 if (!(skb->data[1] & 1)) /* invalid EA1 */
1196 tei = skb->data[1] >> 1;
1197 if (tei > 63) /* not a fixed tei */
1199 if ((skb->data[2] & ~0x10) != SABME)
1201 /* We got a SABME for a fixed TEI */
1202 if (*debug & DEBUG_L2_CTRL)
1203 printk(KERN_DEBUG "%s: SABME sapi(%d) tei(%d)\n",
1204 __func__, sapi, tei);
1205 l2 = create_new_tei(mgr, tei, sapi);
1207 if (*debug & DEBUG_L2_CTRL)
1208 printk(KERN_DEBUG "%s: failed to create new tei\n",
1212 ret = l2->ch.send(&l2->ch, skb);
1217 delete_teimanager(struct mISDNchannel *ch)
1219 struct manager *mgr;
1220 struct layer2 *l2, *nl2;
1222 mgr = container_of(ch, struct manager, ch);
1223 /* not locked lock is taken in release tei */
1224 list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1225 mutex_lock(&mgr->ch.st->lmutex);
1226 list_del(&l2->ch.list);
1227 mutex_unlock(&mgr->ch.st->lmutex);
1228 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1230 list_del(&mgr->ch.list);
1231 list_del(&mgr->bcast.list);
1232 skb_queue_purge(&mgr->sendq);
1237 mgr_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1239 struct manager *mgr;
1242 mgr = container_of(ch, struct manager, ch);
1243 if (*debug & DEBUG_L2_CTRL)
1244 printk(KERN_DEBUG "%s(%x, %p)\n", __func__, cmd, arg);
1247 ret = create_teimgr(mgr, arg);
1250 ret = free_teimanager(mgr);
1252 case CONTROL_CHANNEL:
1253 ret = ctrl_teimanager(mgr, arg);
1256 ret = check_data(mgr, arg);
1263 mgr_bcast(struct mISDNchannel *ch, struct sk_buff *skb)
1265 struct manager *mgr = container_of(ch, struct manager, bcast);
1266 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1267 struct sk_buff *cskb = NULL;
1272 read_lock_irqsave(&mgr->lock, flags);
1273 list_for_each_entry(l2, &mgr->layer2, list) {
1274 if ((hh->id & MISDN_ID_SAPI_MASK) ==
1275 (l2->ch.addr & MISDN_ID_SAPI_MASK)) {
1276 if (list_is_last(&l2->list, &mgr->layer2)) {
1281 cskb = skb_copy(skb, GFP_KERNEL);
1284 ret = l2->ch.send(&l2->ch, cskb);
1286 if (*debug & DEBUG_SEND_ERR)
1288 "%s ch%d prim(%x) addr(%x)"
1290 __func__, l2->ch.nr,
1291 hh->prim, l2->ch.addr, ret);
1295 printk(KERN_WARNING "%s ch%d addr %x no mem\n",
1296 __func__, ch->nr, ch->addr);
1302 read_unlock_irqrestore(&mgr->lock, flags);
1304 dev_kfree_skb(cskb);
1311 mgr_bcast_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1318 create_teimanager(struct mISDNdevice *dev)
1320 struct manager *mgr;
1322 mgr = kzalloc(sizeof(struct manager), GFP_KERNEL);
1325 INIT_LIST_HEAD(&mgr->layer2);
1326 rwlock_init(&mgr->lock);
1327 skb_queue_head_init(&mgr->sendq);
1329 mgr->lastid = MISDN_ID_NONE;
1330 mgr->ch.send = mgr_send;
1331 mgr->ch.ctrl = mgr_ctrl;
1332 mgr->ch.st = dev->D.st;
1333 set_channel_address(&mgr->ch, TEI_SAPI, GROUP_TEI);
1334 add_layer2(&mgr->ch, dev->D.st);
1335 mgr->bcast.send = mgr_bcast;
1336 mgr->bcast.ctrl = mgr_bcast_ctrl;
1337 mgr->bcast.st = dev->D.st;
1338 set_channel_address(&mgr->bcast, 0, GROUP_TEI);
1339 add_layer2(&mgr->bcast, dev->D.st);
1340 mgr->deact.debug = *debug & DEBUG_MANAGER;
1341 mgr->deact.userdata = mgr;
1342 mgr->deact.printdebug = da_debug;
1343 mgr->deact.fsm = &deactfsm;
1344 mgr->deact.state = ST_L1_DEACT;
1345 mISDN_FsmInitTimer(&mgr->deact, &mgr->datimer);
1346 dev->teimgr = &mgr->ch;
1350 int TEIInit(u_int *deb)
1353 teifsmu.state_count = TEI_STATE_COUNT;
1354 teifsmu.event_count = TEI_EVENT_COUNT;
1355 teifsmu.strEvent = strTeiEvent;
1356 teifsmu.strState = strTeiState;
1357 mISDN_FsmNew(&teifsmu, TeiFnListUser, ARRAY_SIZE(TeiFnListUser));
1358 teifsmn.state_count = TEI_STATE_COUNT;
1359 teifsmn.event_count = TEI_EVENT_COUNT;
1360 teifsmn.strEvent = strTeiEvent;
1361 teifsmn.strState = strTeiState;
1362 mISDN_FsmNew(&teifsmn, TeiFnListNet, ARRAY_SIZE(TeiFnListNet));
1363 deactfsm.state_count = DEACT_STATE_COUNT;
1364 deactfsm.event_count = DEACT_EVENT_COUNT;
1365 deactfsm.strEvent = strDeactEvent;
1366 deactfsm.strState = strDeactState;
1367 mISDN_FsmNew(&deactfsm, DeactFnList, ARRAY_SIZE(DeactFnList));
1373 mISDN_FsmFree(&teifsmu);
1374 mISDN_FsmFree(&teifsmn);
1375 mISDN_FsmFree(&deactfsm);