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>
29 #define TEI_ENTITY_ID 0xf
31 #define MGR_PH_ACTIVE 16
32 #define MGR_PH_NOTREADY 17
34 #define DATIMER_VAL 10000
38 static struct Fsm deactfsm = {NULL, 0, 0, NULL, NULL};
39 static struct Fsm teifsmu = {NULL, 0, 0, NULL, NULL};
40 static struct Fsm teifsmn = {NULL, 0, 0, NULL, NULL};
47 #define DEACT_STATE_COUNT (ST_L1_ACTIV+1)
49 static char *strDeactState[] =
52 "ST_L1_DEACT_PENDING",
65 #define DEACT_EVENT_COUNT (EV_DATIMER+1)
67 static char *strDeactEvent[] =
78 da_debug(struct FsmInst *fi, char *fmt, ...)
80 struct manager *mgr = fi->userdata;
83 if (!(*debug & DEBUG_L2_TEIFSM))
86 printk(KERN_DEBUG "mgr(%d): ", mgr->ch.st->dev->id);
93 da_activate(struct FsmInst *fi, int event, void *arg)
95 struct manager *mgr = fi->userdata;
97 if (fi->state == ST_L1_DEACT_PENDING)
98 mISDN_FsmDelTimer(&mgr->datimer, 1);
99 mISDN_FsmChangeState(fi, ST_L1_ACTIV);
103 da_deactivate_ind(struct FsmInst *fi, int event, void *arg)
105 mISDN_FsmChangeState(fi, ST_L1_DEACT);
109 da_deactivate(struct FsmInst *fi, int event, void *arg)
111 struct manager *mgr = fi->userdata;
115 read_lock_irqsave(&mgr->lock, flags);
116 list_for_each_entry(l2, &mgr->layer2, list) {
117 if (l2->l2m.state > ST_L2_4) {
118 /* have still activ TEI */
119 read_unlock_irqrestore(&mgr->lock, flags);
123 read_unlock_irqrestore(&mgr->lock, flags);
124 /* All TEI are inactiv */
125 if (!test_bit(OPTION_L1_HOLD, &mgr->options)) {
126 mISDN_FsmAddTimer(&mgr->datimer, DATIMER_VAL, EV_DATIMER,
128 mISDN_FsmChangeState(fi, ST_L1_DEACT_PENDING);
133 da_ui(struct FsmInst *fi, int event, void *arg)
135 struct manager *mgr = fi->userdata;
137 /* restart da timer */
138 if (!test_bit(OPTION_L1_HOLD, &mgr->options)) {
139 mISDN_FsmDelTimer(&mgr->datimer, 2);
140 mISDN_FsmAddTimer(&mgr->datimer, DATIMER_VAL, EV_DATIMER,
146 da_timer(struct FsmInst *fi, int event, void *arg)
148 struct manager *mgr = fi->userdata;
153 read_lock_irqsave(&mgr->lock, flags);
154 list_for_each_entry(l2, &mgr->layer2, list) {
155 if (l2->l2m.state > ST_L2_4) {
156 /* have still activ TEI */
157 read_unlock_irqrestore(&mgr->lock, flags);
158 mISDN_FsmChangeState(fi, ST_L1_ACTIV);
162 read_unlock_irqrestore(&mgr->lock, flags);
163 /* All TEI are inactiv */
164 mISDN_FsmChangeState(fi, ST_L1_DEACT);
165 _queue_data(&mgr->ch, PH_DEACTIVATE_REQ, MISDN_ID_ANY, 0, NULL,
169 static struct FsmNode DeactFnList[] =
171 {ST_L1_DEACT, EV_ACTIVATE_IND, da_activate},
172 {ST_L1_ACTIV, EV_DEACTIVATE_IND, da_deactivate_ind},
173 {ST_L1_ACTIV, EV_DEACTIVATE, da_deactivate},
174 {ST_L1_DEACT_PENDING, EV_ACTIVATE, da_activate},
175 {ST_L1_DEACT_PENDING, EV_UI, da_ui},
176 {ST_L1_DEACT_PENDING, EV_DATIMER, da_timer},
185 #define TEI_STATE_COUNT (ST_TEI_IDVERIFY+1)
187 static char *strTeiState[] =
206 #define TEI_EVENT_COUNT (EV_TIMER+1)
208 static char *strTeiEvent[] =
222 tei_debug(struct FsmInst *fi, char *fmt, ...)
224 struct teimgr *tm = fi->userdata;
227 if (!(*debug & DEBUG_L2_TEIFSM))
230 printk(KERN_DEBUG "sapi(%d) tei(%d): ", tm->l2->sapi, tm->l2->tei);
239 get_free_id(struct manager *mgr)
245 list_for_each_entry(l2, &mgr->layer2, list) {
246 if (l2->ch.nr > 63) {
248 "%s: more as 63 layer2 for one device\n",
252 test_and_set_bit(l2->ch.nr, (u_long *)&ids);
254 for (i = 1; i < 64; i++)
255 if (!test_bit(i, (u_long *)&ids))
257 printk(KERN_WARNING "%s: more as 63 layer2 for one device\n",
263 get_free_tei(struct manager *mgr)
269 list_for_each_entry(l2, &mgr->layer2, list) {
272 if ((l2->ch.addr & 0xff) != 0)
274 i = l2->ch.addr >> 8;
279 test_and_set_bit(i, (u_long *)&ids);
281 for (i = 0; i < 64; i++)
282 if (!test_bit(i, (u_long *)&ids))
284 printk(KERN_WARNING "%s: more as 63 dynamic tei for one device\n",
290 teiup_create(struct manager *mgr, u_int prim, int len, void *arg)
293 struct mISDNhead *hh;
296 skb = mI_alloc_skb(len, GFP_ATOMIC);
299 hh = mISDN_HEAD_P(skb);
301 hh->id = (mgr->ch.nr << 16) | mgr->ch.addr;
303 memcpy(skb_put(skb, len), arg, len);
304 err = mgr->up->send(mgr->up, skb);
306 printk(KERN_WARNING "%s: err=%d\n", __func__, err);
312 new_id(struct manager *mgr)
320 id |= GROUP_TEI << 8;
326 do_send(struct manager *mgr)
328 if (!test_bit(MGR_PH_ACTIVE, &mgr->options))
331 if (!test_and_set_bit(MGR_PH_NOTREADY, &mgr->options)) {
332 struct sk_buff *skb = skb_dequeue(&mgr->sendq);
335 test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
338 mgr->lastid = mISDN_HEAD_ID(skb);
339 mISDN_FsmEvent(&mgr->deact, EV_UI, NULL);
340 if (mgr->ch.recv(mgr->ch.peer, skb)) {
342 test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
343 mgr->lastid = MISDN_ID_NONE;
349 do_ack(struct manager *mgr, u_int id)
351 if (test_bit(MGR_PH_NOTREADY, &mgr->options)) {
352 if (id == mgr->lastid) {
353 if (test_bit(MGR_PH_ACTIVE, &mgr->options)) {
356 skb = skb_dequeue(&mgr->sendq);
358 mgr->lastid = mISDN_HEAD_ID(skb);
359 if (!mgr->ch.recv(mgr->ch.peer, skb))
364 mgr->lastid = MISDN_ID_NONE;
365 test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
371 mgr_send_down(struct manager *mgr, struct sk_buff *skb)
373 skb_queue_tail(&mgr->sendq, skb);
374 if (!test_bit(MGR_PH_ACTIVE, &mgr->options)) {
375 _queue_data(&mgr->ch, PH_ACTIVATE_REQ, MISDN_ID_ANY, 0,
383 dl_unit_data(struct manager *mgr, struct sk_buff *skb)
385 if (!test_bit(MGR_OPT_NETWORK, &mgr->options)) /* only net send UI */
387 if (!test_bit(MGR_PH_ACTIVE, &mgr->options))
388 _queue_data(&mgr->ch, PH_ACTIVATE_REQ, MISDN_ID_ANY, 0,
391 skb->data[0] = 0x02; /* SAPI 0 C/R = 1 */
392 skb->data[1] = 0xff; /* TEI 127 */
393 skb->data[2] = UI; /* UI frame */
394 mISDN_HEAD_PRIM(skb) = PH_DATA_REQ;
395 mISDN_HEAD_ID(skb) = new_id(mgr);
396 skb_queue_tail(&mgr->sendq, skb);
406 get_random_bytes(&x, sizeof(x));
410 static struct layer2 *
411 findtei(struct manager *mgr, int tei)
416 read_lock_irqsave(&mgr->lock, flags);
417 list_for_each_entry(l2, &mgr->layer2, list) {
418 if ((l2->sapi == 0) && (l2->tei > 0) &&
419 (l2->tei != GROUP_TEI) && (l2->tei == tei))
424 read_unlock_irqrestore(&mgr->lock, flags);
429 put_tei_msg(struct manager *mgr, u_char m_id, unsigned int ri, int tei)
434 bp[0] = (TEI_SAPI << 2);
435 if (test_bit(MGR_OPT_NETWORK, &mgr->options))
436 bp[0] |= 2; /* CR:=1 for net command */
437 bp[1] = (GROUP_TEI << 1) | 0x1;
439 bp[3] = TEI_ENTITY_ID;
443 bp[7] = ((tei << 1) & 0xff) | 1;
444 skb = _alloc_mISDN_skb(PH_DATA_REQ, new_id(mgr), 8, bp, GFP_ATOMIC);
446 printk(KERN_WARNING "%s: no skb for tei msg\n", __func__);
449 mgr_send_down(mgr, skb);
453 tei_id_request(struct FsmInst *fi, int event, void *arg)
455 struct teimgr *tm = fi->userdata;
457 if (tm->l2->tei != GROUP_TEI) {
458 tm->tei_m.printdebug(&tm->tei_m,
459 "assign request for allready assigned tei %d",
463 tm->ri = random_ri();
464 if (*debug & DEBUG_L2_TEI)
465 tm->tei_m.printdebug(&tm->tei_m,
466 "assign request ri %d", tm->ri);
467 put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI);
468 mISDN_FsmChangeState(fi, ST_TEI_IDREQ);
469 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 1);
474 tei_id_assign(struct FsmInst *fi, int event, void *arg)
476 struct teimgr *tm = fi->userdata;
481 ri = ((unsigned int) *dp++ << 8);
485 if (*debug & DEBUG_L2_TEI)
486 tm->tei_m.printdebug(fi, "identity assign ri %d tei %d",
488 l2 = findtei(tm->mgr, tei);
489 if (l2) { /* same tei is in use */
490 if (ri != l2->tm->ri) {
491 tm->tei_m.printdebug(fi,
492 "possible duplicate assignment tei %d", tei);
493 tei_l2(l2, MDL_ERROR_RSP, 0);
495 } else if (ri == tm->ri) {
496 mISDN_FsmDelTimer(&tm->timer, 1);
497 mISDN_FsmChangeState(fi, ST_TEI_NOP);
498 tei_l2(tm->l2, MDL_ASSIGN_REQ, tei);
503 tei_id_test_dup(struct FsmInst *fi, int event, void *arg)
505 struct teimgr *tm = fi->userdata;
510 ri = ((unsigned int) *dp++ << 8);
514 if (*debug & DEBUG_L2_TEI)
515 tm->tei_m.printdebug(fi, "foreign identity assign ri %d tei %d",
517 l2 = findtei(tm->mgr, tei);
518 if (l2) { /* same tei is in use */
519 if (ri != l2->tm->ri) { /* and it wasn't our request */
520 tm->tei_m.printdebug(fi,
521 "possible duplicate assignment tei %d", tei);
522 mISDN_FsmEvent(&l2->tm->tei_m, EV_VERIFY, NULL);
528 tei_id_denied(struct FsmInst *fi, int event, void *arg)
530 struct teimgr *tm = fi->userdata;
534 ri = ((unsigned int) *dp++ << 8);
538 if (*debug & DEBUG_L2_TEI)
539 tm->tei_m.printdebug(fi, "identity denied ri %d tei %d",
544 tei_id_chk_req(struct FsmInst *fi, int event, void *arg)
546 struct teimgr *tm = fi->userdata;
551 if (*debug & DEBUG_L2_TEI)
552 tm->tei_m.printdebug(fi, "identity check req tei %d", tei);
553 if ((tm->l2->tei != GROUP_TEI) && ((tei == GROUP_TEI) ||
554 (tei == tm->l2->tei))) {
555 mISDN_FsmDelTimer(&tm->timer, 4);
556 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP);
557 put_tei_msg(tm->mgr, ID_CHK_RES, random_ri(), tm->l2->tei);
562 tei_id_remove(struct FsmInst *fi, int event, void *arg)
564 struct teimgr *tm = fi->userdata;
569 if (*debug & DEBUG_L2_TEI)
570 tm->tei_m.printdebug(fi, "identity remove tei %d", tei);
571 if ((tm->l2->tei != GROUP_TEI) &&
572 ((tei == GROUP_TEI) || (tei == tm->l2->tei))) {
573 mISDN_FsmDelTimer(&tm->timer, 5);
574 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP);
575 tei_l2(tm->l2, MDL_REMOVE_REQ, 0);
580 tei_id_verify(struct FsmInst *fi, int event, void *arg)
582 struct teimgr *tm = fi->userdata;
584 if (*debug & DEBUG_L2_TEI)
585 tm->tei_m.printdebug(fi, "id verify request for tei %d",
587 put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei);
588 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY);
589 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2);
594 tei_id_req_tout(struct FsmInst *fi, int event, void *arg)
596 struct teimgr *tm = fi->userdata;
599 tm->ri = random_ri();
600 if (*debug & DEBUG_L2_TEI)
601 tm->tei_m.printdebug(fi, "assign req(%d) ri %d",
602 4 - tm->nval, tm->ri);
603 put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI);
604 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 3);
606 tm->tei_m.printdebug(fi, "assign req failed");
607 tei_l2(tm->l2, MDL_ERROR_RSP, 0);
608 mISDN_FsmChangeState(fi, ST_TEI_NOP);
613 tei_id_ver_tout(struct FsmInst *fi, int event, void *arg)
615 struct teimgr *tm = fi->userdata;
618 if (*debug & DEBUG_L2_TEI)
619 tm->tei_m.printdebug(fi,
620 "id verify req(%d) for tei %d",
621 3 - tm->nval, tm->l2->tei);
622 put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei);
623 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4);
625 tm->tei_m.printdebug(fi, "verify req for tei %d failed",
627 tei_l2(tm->l2, MDL_REMOVE_REQ, 0);
628 mISDN_FsmChangeState(fi, ST_TEI_NOP);
632 static struct FsmNode TeiFnListUser[] =
634 {ST_TEI_NOP, EV_IDREQ, tei_id_request},
635 {ST_TEI_NOP, EV_ASSIGN, tei_id_test_dup},
636 {ST_TEI_NOP, EV_VERIFY, tei_id_verify},
637 {ST_TEI_NOP, EV_REMOVE, tei_id_remove},
638 {ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req},
639 {ST_TEI_IDREQ, EV_TIMER, tei_id_req_tout},
640 {ST_TEI_IDREQ, EV_ASSIGN, tei_id_assign},
641 {ST_TEI_IDREQ, EV_DENIED, tei_id_denied},
642 {ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout},
643 {ST_TEI_IDVERIFY, EV_REMOVE, tei_id_remove},
644 {ST_TEI_IDVERIFY, EV_CHKREQ, tei_id_chk_req},
648 tei_l2remove(struct layer2 *l2)
650 put_tei_msg(l2->tm->mgr, ID_REMOVE, 0, l2->tei);
651 tei_l2(l2, MDL_REMOVE_REQ, 0);
652 list_del(&l2->ch.list);
653 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
657 tei_assign_req(struct FsmInst *fi, int event, void *arg)
659 struct teimgr *tm = fi->userdata;
662 if (tm->l2->tei == GROUP_TEI) {
663 tm->tei_m.printdebug(&tm->tei_m,
664 "net tei assign request without tei");
667 tm->ri = ((unsigned int) *dp++ << 8);
669 if (*debug & DEBUG_L2_TEI)
670 tm->tei_m.printdebug(&tm->tei_m,
671 "net assign request ri %d teim %d", tm->ri, *dp);
672 put_tei_msg(tm->mgr, ID_ASSIGNED, tm->ri, tm->l2->tei);
673 mISDN_FsmChangeState(fi, ST_TEI_NOP);
677 tei_id_chk_req_net(struct FsmInst *fi, int event, void *arg)
679 struct teimgr *tm = fi->userdata;
681 if (*debug & DEBUG_L2_TEI)
682 tm->tei_m.printdebug(fi, "id check request for tei %d",
685 put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei);
686 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY);
687 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2);
692 tei_id_chk_resp(struct FsmInst *fi, int event, void *arg)
694 struct teimgr *tm = fi->userdata;
699 if (*debug & DEBUG_L2_TEI)
700 tm->tei_m.printdebug(fi, "identity check resp tei %d", tei);
701 if (tei == tm->l2->tei)
706 tei_id_verify_net(struct FsmInst *fi, int event, void *arg)
708 struct teimgr *tm = fi->userdata;
713 if (*debug & DEBUG_L2_TEI)
714 tm->tei_m.printdebug(fi, "identity verify req tei %d/%d",
716 if (tei == tm->l2->tei)
717 tei_id_chk_req_net(fi, event, arg);
721 tei_id_ver_tout_net(struct FsmInst *fi, int event, void *arg)
723 struct teimgr *tm = fi->userdata;
726 if (*debug & DEBUG_L2_TEI)
727 tm->tei_m.printdebug(fi,
728 "check req for tei %d sucessful\n", tm->l2->tei);
729 mISDN_FsmChangeState(fi, ST_TEI_NOP);
730 } else if (tm->rcnt > 1) {
731 /* duplicate assignment; remove */
732 tei_l2remove(tm->l2);
733 } else if (--tm->nval) {
734 if (*debug & DEBUG_L2_TEI)
735 tm->tei_m.printdebug(fi,
736 "id check req(%d) for tei %d",
737 3 - tm->nval, tm->l2->tei);
738 put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei);
739 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4);
741 tm->tei_m.printdebug(fi, "check req for tei %d failed",
743 mISDN_FsmChangeState(fi, ST_TEI_NOP);
744 tei_l2remove(tm->l2);
748 static struct FsmNode TeiFnListNet[] =
750 {ST_TEI_NOP, EV_ASSIGN_REQ, tei_assign_req},
751 {ST_TEI_NOP, EV_VERIFY, tei_id_verify_net},
752 {ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req_net},
753 {ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout_net},
754 {ST_TEI_IDVERIFY, EV_CHKRESP, tei_id_chk_resp},
758 tei_ph_data_ind(struct teimgr *tm, u_int mt, u_char *dp, int len)
760 if (test_bit(FLG_FIXED_TEI, &tm->l2->flag))
762 if (*debug & DEBUG_L2_TEI)
763 tm->tei_m.printdebug(&tm->tei_m, "tei handler mt %x", mt);
764 if (mt == ID_ASSIGNED)
765 mISDN_FsmEvent(&tm->tei_m, EV_ASSIGN, dp);
766 else if (mt == ID_DENIED)
767 mISDN_FsmEvent(&tm->tei_m, EV_DENIED, dp);
768 else if (mt == ID_CHK_REQ)
769 mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, dp);
770 else if (mt == ID_REMOVE)
771 mISDN_FsmEvent(&tm->tei_m, EV_REMOVE, dp);
772 else if (mt == ID_VERIFY)
773 mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, dp);
774 else if (mt == ID_CHK_RES)
775 mISDN_FsmEvent(&tm->tei_m, EV_CHKRESP, dp);
778 static struct layer2 *
779 create_new_tei(struct manager *mgr, int tei, int sapi)
788 if ((tei >= 0) && (tei < 64))
789 test_and_set_bit(OPTION_L2_FIXEDTEI, &opt);
790 if (mgr->ch.st->dev->Dprotocols
791 & ((1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1)))
792 test_and_set_bit(OPTION_L2_PMX, &opt);
793 l2 = create_l2(mgr->up, ISDN_P_LAPD_NT, opt, tei, sapi);
795 printk(KERN_WARNING "%s:no memory for layer2\n", __func__);
798 l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL);
801 printk(KERN_WARNING "%s:no memory for teimgr\n", __func__);
806 l2->tm->tei_m.debug = *debug & DEBUG_L2_TEIFSM;
807 l2->tm->tei_m.userdata = l2->tm;
808 l2->tm->tei_m.printdebug = tei_debug;
809 l2->tm->tei_m.fsm = &teifsmn;
810 l2->tm->tei_m.state = ST_TEI_NOP;
811 l2->tm->tval = 2000; /* T202 2 sec */
812 mISDN_FsmInitTimer(&l2->tm->tei_m, &l2->tm->timer);
813 write_lock_irqsave(&mgr->lock, flags);
814 id = get_free_id(mgr);
815 list_add_tail(&l2->list, &mgr->layer2);
816 write_unlock_irqrestore(&mgr->lock, flags);
818 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
819 printk(KERN_WARNING "%s:no free id\n", __func__);
823 __add_layer2(&l2->ch, mgr->ch.st);
824 l2->ch.recv = mgr->ch.recv;
825 l2->ch.peer = mgr->ch.peer;
826 l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL);
832 new_tei_req(struct manager *mgr, u_char *dp)
841 if (!(dp[3] & 1)) /* Extension bit != 1 */
844 tei = dp[3] >> 1; /* 3GPP TS 08.56 6.1.11.2 */
846 tei = get_free_tei(mgr);
848 printk(KERN_WARNING "%s:No free tei\n", __func__);
851 l2 = create_new_tei(mgr, tei, CTRL_SAPI);
855 mISDN_FsmEvent(&l2->tm->tei_m, EV_ASSIGN_REQ, dp);
858 put_tei_msg(mgr, ID_DENIED, ri, GROUP_TEI);
862 ph_data_ind(struct manager *mgr, struct sk_buff *skb)
870 if (*debug & DEBUG_L2_TEI)
871 printk(KERN_DEBUG "%s: short mgr frame %d/8\n",
876 if ((skb->data[0] >> 2) != TEI_SAPI) /* not for us */
878 if (skb->data[0] & 1) /* EA0 formal error */
880 if (!(skb->data[1] & 1)) /* EA1 formal error */
882 if ((skb->data[1] >> 1) != GROUP_TEI) /* not for us */
884 if ((skb->data[2] & 0xef) != UI) /* not UI */
886 if (skb->data[3] != TEI_ENTITY_ID) /* not tei entity */
893 if (!test_bit(MGR_OPT_NETWORK, &mgr->options))
900 if (test_bit(MGR_OPT_NETWORK, &mgr->options))
907 if (mt == ID_REQUEST) {
908 new_tei_req(mgr, &skb->data[4]);
911 read_lock_irqsave(&mgr->lock, flags);
912 list_for_each_entry(l2, &mgr->layer2, list) {
913 tei_ph_data_ind(l2->tm, mt, &skb->data[4], skb->len - 4);
915 read_unlock_irqrestore(&mgr->lock, flags);
921 l2_tei(struct layer2 *l2, u_int cmd, u_long arg)
923 struct teimgr *tm = l2->tm;
925 if (test_bit(FLG_FIXED_TEI, &l2->flag))
927 if (*debug & DEBUG_L2_TEI)
928 printk(KERN_DEBUG "%s: cmd(%x)\n", __func__, cmd);
931 mISDN_FsmEvent(&tm->tei_m, EV_IDREQ, NULL);
934 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
935 mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, &l2->tei);
936 if (test_bit(MGR_OPT_USER, &tm->mgr->options))
937 mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, NULL);
939 case MDL_STATUS_UP_IND:
940 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
941 mISDN_FsmEvent(&tm->mgr->deact, EV_ACTIVATE, NULL);
943 case MDL_STATUS_DOWN_IND:
944 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
945 mISDN_FsmEvent(&tm->mgr->deact, EV_DEACTIVATE, NULL);
947 case MDL_STATUS_UI_IND:
948 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
949 mISDN_FsmEvent(&tm->mgr->deact, EV_UI, NULL);
956 TEIrelease(struct layer2 *l2)
958 struct teimgr *tm = l2->tm;
961 mISDN_FsmDelTimer(&tm->timer, 1);
962 write_lock_irqsave(&tm->mgr->lock, flags);
964 write_unlock_irqrestore(&tm->mgr->lock, flags);
970 create_teimgr(struct manager *mgr, struct channel_req *crq)
977 if (*debug & DEBUG_L2_TEI)
978 printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n",
979 __func__, dev_name(&mgr->ch.st->dev->dev),
980 crq->protocol, crq->adr.dev, crq->adr.channel,
981 crq->adr.sapi, crq->adr.tei);
982 if (crq->adr.tei > GROUP_TEI)
984 if (crq->adr.tei < 64)
985 test_and_set_bit(OPTION_L2_FIXEDTEI, &opt);
986 if (crq->adr.tei == 0)
987 test_and_set_bit(OPTION_L2_PTP, &opt);
988 if (test_bit(MGR_OPT_NETWORK, &mgr->options)) {
989 if (crq->protocol == ISDN_P_LAPD_TE)
990 return -EPROTONOSUPPORT;
991 if ((crq->adr.tei != 0) && (crq->adr.tei != 127))
995 "%s: only one network manager is allowed\n",
999 } else if (test_bit(MGR_OPT_USER, &mgr->options)) {
1000 if (crq->protocol == ISDN_P_LAPD_NT)
1001 return -EPROTONOSUPPORT;
1002 if ((crq->adr.tei >= 64) && (crq->adr.tei < GROUP_TEI))
1003 return -EINVAL; /* dyn tei */
1005 if (crq->protocol == ISDN_P_LAPD_NT)
1006 test_and_set_bit(MGR_OPT_NETWORK, &mgr->options);
1007 if (crq->protocol == ISDN_P_LAPD_TE)
1008 test_and_set_bit(MGR_OPT_USER, &mgr->options);
1010 if (mgr->ch.st->dev->Dprotocols
1011 & ((1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1)))
1012 test_and_set_bit(OPTION_L2_PMX, &opt);
1013 if ((crq->protocol == ISDN_P_LAPD_NT) && (crq->adr.tei == 127)) {
1015 id = DL_INFO_L2_CONNECT;
1016 teiup_create(mgr, DL_INFORMATION_IND, sizeof(id), &id);
1018 if (!list_empty(&mgr->layer2)) {
1019 read_lock_irqsave(&mgr->lock, flags);
1020 list_for_each_entry(l2, &mgr->layer2, list) {
1022 l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL);
1024 read_unlock_irqrestore(&mgr->lock, flags);
1028 l2 = create_l2(crq->ch, crq->protocol, opt,
1029 crq->adr.tei, crq->adr.sapi);
1032 l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL);
1035 printk(KERN_ERR "kmalloc teimgr failed\n");
1040 l2->tm->tei_m.debug = *debug & DEBUG_L2_TEIFSM;
1041 l2->tm->tei_m.userdata = l2->tm;
1042 l2->tm->tei_m.printdebug = tei_debug;
1043 if (crq->protocol == ISDN_P_LAPD_TE) {
1044 l2->tm->tei_m.fsm = &teifsmu;
1045 l2->tm->tei_m.state = ST_TEI_NOP;
1046 l2->tm->tval = 1000; /* T201 1 sec */
1048 l2->tm->tei_m.fsm = &teifsmn;
1049 l2->tm->tei_m.state = ST_TEI_NOP;
1050 l2->tm->tval = 2000; /* T202 2 sec */
1052 mISDN_FsmInitTimer(&l2->tm->tei_m, &l2->tm->timer);
1053 write_lock_irqsave(&mgr->lock, flags);
1054 id = get_free_id(mgr);
1055 list_add_tail(&l2->list, &mgr->layer2);
1056 write_unlock_irqrestore(&mgr->lock, flags);
1058 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1069 mgr_send(struct mISDNchannel *ch, struct sk_buff *skb)
1071 struct manager *mgr;
1072 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1075 mgr = container_of(ch, struct manager, ch);
1076 if (*debug & DEBUG_L2_RECV)
1077 printk(KERN_DEBUG "%s: prim(%x) id(%x)\n",
1078 __func__, hh->prim, hh->id);
1081 mISDN_FsmEvent(&mgr->deact, EV_UI, NULL);
1082 ret = ph_data_ind(mgr, skb);
1085 do_ack(mgr, hh->id);
1088 case PH_ACTIVATE_IND:
1089 test_and_set_bit(MGR_PH_ACTIVE, &mgr->options);
1090 mISDN_FsmEvent(&mgr->deact, EV_ACTIVATE_IND, NULL);
1094 case PH_DEACTIVATE_IND:
1095 test_and_clear_bit(MGR_PH_ACTIVE, &mgr->options);
1096 mISDN_FsmEvent(&mgr->deact, EV_DEACTIVATE_IND, NULL);
1099 case DL_UNITDATA_REQ:
1100 return dl_unit_data(mgr, skb);
1108 free_teimanager(struct manager *mgr)
1110 struct layer2 *l2, *nl2;
1112 test_and_clear_bit(OPTION_L1_HOLD, &mgr->options);
1113 if (test_bit(MGR_OPT_NETWORK, &mgr->options)) {
1114 /* not locked lock is taken in release tei */
1116 if (test_bit(OPTION_L2_CLEANUP, &mgr->options)) {
1117 list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1118 put_tei_msg(mgr, ID_REMOVE, 0, l2->tei);
1119 mutex_lock(&mgr->ch.st->lmutex);
1120 list_del(&l2->ch.list);
1121 mutex_unlock(&mgr->ch.st->lmutex);
1122 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1124 test_and_clear_bit(MGR_OPT_NETWORK, &mgr->options);
1126 list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1131 if (test_bit(MGR_OPT_USER, &mgr->options)) {
1132 if (list_empty(&mgr->layer2))
1133 test_and_clear_bit(MGR_OPT_USER, &mgr->options);
1135 mgr->ch.st->dev->D.ctrl(&mgr->ch.st->dev->D, CLOSE_CHANNEL, NULL);
1140 ctrl_teimanager(struct manager *mgr, void *arg)
1142 /* currently we only have one option */
1143 int *val = (int *)arg;
1149 test_and_set_bit(OPTION_L2_CLEANUP, &mgr->options);
1151 test_and_clear_bit(OPTION_L2_CLEANUP, &mgr->options);
1155 test_and_set_bit(OPTION_L1_HOLD, &mgr->options);
1157 test_and_clear_bit(OPTION_L1_HOLD, &mgr->options);
1165 /* This function does create a L2 for fixed TEI in NT Mode */
1167 check_data(struct manager *mgr, struct sk_buff *skb)
1169 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1173 if (*debug & DEBUG_L2_CTRL)
1174 printk(KERN_DEBUG "%s: prim(%x) id(%x)\n",
1175 __func__, hh->prim, hh->id);
1176 if (test_bit(MGR_OPT_USER, &mgr->options))
1178 if (hh->prim != PH_DATA_IND)
1182 if (skb->data[0] & 3) /* EA0 and CR must be 0 */
1184 sapi = skb->data[0] >> 2;
1185 if (!(skb->data[1] & 1)) /* invalid EA1 */
1187 tei = skb->data[1] >> 1;
1188 if (tei > 63) /* not a fixed tei */
1190 if ((skb->data[2] & ~0x10) != SABME)
1192 /* We got a SABME for a fixed TEI */
1193 if (*debug & DEBUG_L2_CTRL)
1194 printk(KERN_DEBUG "%s: SABME sapi(%d) tei(%d)\n",
1195 __func__, sapi, tei);
1196 l2 = create_new_tei(mgr, tei, sapi);
1198 if (*debug & DEBUG_L2_CTRL)
1199 printk(KERN_DEBUG "%s: failed to create new tei\n",
1203 ret = l2->ch.send(&l2->ch, skb);
1208 delete_teimanager(struct mISDNchannel *ch)
1210 struct manager *mgr;
1211 struct layer2 *l2, *nl2;
1213 mgr = container_of(ch, struct manager, ch);
1214 /* not locked lock is taken in release tei */
1215 list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1216 mutex_lock(&mgr->ch.st->lmutex);
1217 list_del(&l2->ch.list);
1218 mutex_unlock(&mgr->ch.st->lmutex);
1219 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1221 list_del(&mgr->ch.list);
1222 list_del(&mgr->bcast.list);
1223 skb_queue_purge(&mgr->sendq);
1228 mgr_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1230 struct manager *mgr;
1233 mgr = container_of(ch, struct manager, ch);
1234 if (*debug & DEBUG_L2_CTRL)
1235 printk(KERN_DEBUG "%s(%x, %p)\n", __func__, cmd, arg);
1238 ret = create_teimgr(mgr, arg);
1241 ret = free_teimanager(mgr);
1243 case CONTROL_CHANNEL:
1244 ret = ctrl_teimanager(mgr, arg);
1247 ret = check_data(mgr, arg);
1254 mgr_bcast(struct mISDNchannel *ch, struct sk_buff *skb)
1256 struct manager *mgr = container_of(ch, struct manager, bcast);
1257 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1258 struct sk_buff *cskb = NULL;
1263 read_lock_irqsave(&mgr->lock, flags);
1264 list_for_each_entry(l2, &mgr->layer2, list) {
1265 if ((hh->id & MISDN_ID_SAPI_MASK) ==
1266 (l2->ch.addr & MISDN_ID_SAPI_MASK)) {
1267 if (list_is_last(&l2->list, &mgr->layer2)) {
1272 cskb = skb_copy(skb, GFP_KERNEL);
1275 ret = l2->ch.send(&l2->ch, cskb);
1277 if (*debug & DEBUG_SEND_ERR)
1279 "%s ch%d prim(%x) addr(%x)"
1281 __func__, l2->ch.nr,
1282 hh->prim, l2->ch.addr, ret);
1286 printk(KERN_WARNING "%s ch%d addr %x no mem\n",
1287 __func__, ch->nr, ch->addr);
1293 read_unlock_irqrestore(&mgr->lock, flags);
1295 dev_kfree_skb(cskb);
1302 mgr_bcast_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1309 create_teimanager(struct mISDNdevice *dev)
1311 struct manager *mgr;
1313 mgr = kzalloc(sizeof(struct manager), GFP_KERNEL);
1316 INIT_LIST_HEAD(&mgr->layer2);
1317 rwlock_init(&mgr->lock);
1318 skb_queue_head_init(&mgr->sendq);
1320 mgr->lastid = MISDN_ID_NONE;
1321 mgr->ch.send = mgr_send;
1322 mgr->ch.ctrl = mgr_ctrl;
1323 mgr->ch.st = dev->D.st;
1324 set_channel_address(&mgr->ch, TEI_SAPI, GROUP_TEI);
1325 add_layer2(&mgr->ch, dev->D.st);
1326 mgr->bcast.send = mgr_bcast;
1327 mgr->bcast.ctrl = mgr_bcast_ctrl;
1328 mgr->bcast.st = dev->D.st;
1329 set_channel_address(&mgr->bcast, 0, GROUP_TEI);
1330 add_layer2(&mgr->bcast, dev->D.st);
1331 mgr->deact.debug = *debug & DEBUG_MANAGER;
1332 mgr->deact.userdata = mgr;
1333 mgr->deact.printdebug = da_debug;
1334 mgr->deact.fsm = &deactfsm;
1335 mgr->deact.state = ST_L1_DEACT;
1336 mISDN_FsmInitTimer(&mgr->deact, &mgr->datimer);
1337 dev->teimgr = &mgr->ch;
1341 int TEIInit(u_int *deb)
1344 teifsmu.state_count = TEI_STATE_COUNT;
1345 teifsmu.event_count = TEI_EVENT_COUNT;
1346 teifsmu.strEvent = strTeiEvent;
1347 teifsmu.strState = strTeiState;
1348 mISDN_FsmNew(&teifsmu, TeiFnListUser, ARRAY_SIZE(TeiFnListUser));
1349 teifsmn.state_count = TEI_STATE_COUNT;
1350 teifsmn.event_count = TEI_EVENT_COUNT;
1351 teifsmn.strEvent = strTeiEvent;
1352 teifsmn.strState = strTeiState;
1353 mISDN_FsmNew(&teifsmn, TeiFnListNet, ARRAY_SIZE(TeiFnListNet));
1354 deactfsm.state_count = DEACT_STATE_COUNT;
1355 deactfsm.event_count = DEACT_EVENT_COUNT;
1356 deactfsm.strEvent = strDeactEvent;
1357 deactfsm.strState = strDeactState;
1358 mISDN_FsmNew(&deactfsm, DeactFnList, ARRAY_SIZE(DeactFnList));
1364 mISDN_FsmFree(&teifsmu);
1365 mISDN_FsmFree(&teifsmn);
1366 mISDN_FsmFree(&deactfsm);