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 mISDN_FsmAddTimer(&mgr->datimer, DATIMER_VAL, EV_DATIMER, NULL, 1);
126 mISDN_FsmChangeState(fi, ST_L1_DEACT_PENDING);
130 da_ui(struct FsmInst *fi, int event, void *arg)
132 struct manager *mgr = fi->userdata;
134 /* restart da timer */
135 mISDN_FsmDelTimer(&mgr->datimer, 2);
136 mISDN_FsmAddTimer(&mgr->datimer, DATIMER_VAL, EV_DATIMER, NULL, 2);
141 da_timer(struct FsmInst *fi, int event, void *arg)
143 struct manager *mgr = fi->userdata;
148 read_lock_irqsave(&mgr->lock, flags);
149 list_for_each_entry(l2, &mgr->layer2, list) {
150 if (l2->l2m.state > ST_L2_4) {
151 /* have still activ TEI */
152 read_unlock_irqrestore(&mgr->lock, flags);
153 mISDN_FsmChangeState(fi, ST_L1_ACTIV);
157 read_unlock_irqrestore(&mgr->lock, flags);
158 /* All TEI are inactiv */
159 mISDN_FsmChangeState(fi, ST_L1_DEACT);
160 _queue_data(&mgr->ch, PH_DEACTIVATE_REQ, MISDN_ID_ANY, 0, NULL,
164 static struct FsmNode DeactFnList[] =
166 {ST_L1_DEACT, EV_ACTIVATE_IND, da_activate},
167 {ST_L1_ACTIV, EV_DEACTIVATE_IND, da_deactivate_ind},
168 {ST_L1_ACTIV, EV_DEACTIVATE, da_deactivate},
169 {ST_L1_DEACT_PENDING, EV_ACTIVATE, da_activate},
170 {ST_L1_DEACT_PENDING, EV_UI, da_ui},
171 {ST_L1_DEACT_PENDING, EV_DATIMER, da_timer},
180 #define TEI_STATE_COUNT (ST_TEI_IDVERIFY+1)
182 static char *strTeiState[] =
201 #define TEI_EVENT_COUNT (EV_TIMER+1)
203 static char *strTeiEvent[] =
217 tei_debug(struct FsmInst *fi, char *fmt, ...)
219 struct teimgr *tm = fi->userdata;
222 if (!(*debug & DEBUG_L2_TEIFSM))
225 printk(KERN_DEBUG "tei(%d): ", tm->l2->tei);
234 get_free_id(struct manager *mgr)
240 list_for_each_entry(l2, &mgr->layer2, list) {
241 if (l2->ch.nr > 63) {
243 "%s: more as 63 layer2 for one device\n",
247 test_and_set_bit(l2->ch.nr, (u_long *)&ids);
249 for (i = 1; i < 64; i++)
250 if (!test_bit(i, (u_long *)&ids))
252 printk(KERN_WARNING "%s: more as 63 layer2 for one device\n",
258 get_free_tei(struct manager *mgr)
264 list_for_each_entry(l2, &mgr->layer2, list) {
267 if ((l2->ch.addr & 0xff) != 0)
269 i = l2->ch.addr >> 8;
274 test_and_set_bit(i, (u_long *)&ids);
276 for (i = 0; i < 64; i++)
277 if (!test_bit(i, (u_long *)&ids))
279 printk(KERN_WARNING "%s: more as 63 dynamic tei for one device\n",
285 teiup_create(struct manager *mgr, u_int prim, int len, void *arg)
288 struct mISDNhead *hh;
291 skb = mI_alloc_skb(len, GFP_ATOMIC);
294 hh = mISDN_HEAD_P(skb);
296 hh->id = (mgr->ch.nr << 16) | mgr->ch.addr;
298 memcpy(skb_put(skb, len), arg, len);
299 err = mgr->up->send(mgr->up, skb);
301 printk(KERN_WARNING "%s: err=%d\n", __func__, err);
307 new_id(struct manager *mgr)
315 id |= GROUP_TEI << 8;
321 do_send(struct manager *mgr)
323 if (!test_bit(MGR_PH_ACTIVE, &mgr->options))
326 if (!test_and_set_bit(MGR_PH_NOTREADY, &mgr->options)) {
327 struct sk_buff *skb = skb_dequeue(&mgr->sendq);
330 test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
333 mgr->lastid = mISDN_HEAD_ID(skb);
334 mISDN_FsmEvent(&mgr->deact, EV_UI, NULL);
335 if (mgr->ch.recv(mgr->ch.peer, skb)) {
337 test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
338 mgr->lastid = MISDN_ID_NONE;
344 do_ack(struct manager *mgr, u_int id)
346 if (test_bit(MGR_PH_NOTREADY, &mgr->options)) {
347 if (id == mgr->lastid) {
348 if (test_bit(MGR_PH_ACTIVE, &mgr->options)) {
351 skb = skb_dequeue(&mgr->sendq);
353 mgr->lastid = mISDN_HEAD_ID(skb);
354 if (!mgr->ch.recv(mgr->ch.peer, skb))
359 mgr->lastid = MISDN_ID_NONE;
360 test_and_clear_bit(MGR_PH_NOTREADY, &mgr->options);
366 mgr_send_down(struct manager *mgr, struct sk_buff *skb)
368 skb_queue_tail(&mgr->sendq, skb);
369 if (!test_bit(MGR_PH_ACTIVE, &mgr->options)) {
370 _queue_data(&mgr->ch, PH_ACTIVATE_REQ, MISDN_ID_ANY, 0,
378 dl_unit_data(struct manager *mgr, struct sk_buff *skb)
380 if (!test_bit(MGR_OPT_NETWORK, &mgr->options)) /* only net send UI */
382 if (!test_bit(MGR_PH_ACTIVE, &mgr->options))
383 _queue_data(&mgr->ch, PH_ACTIVATE_REQ, MISDN_ID_ANY, 0,
386 skb->data[0] = 0x02; /* SAPI 0 C/R = 1 */
387 skb->data[1] = 0xff; /* TEI 127 */
388 skb->data[2] = UI; /* UI frame */
389 mISDN_HEAD_PRIM(skb) = PH_DATA_REQ;
390 mISDN_HEAD_ID(skb) = new_id(mgr);
391 skb_queue_tail(&mgr->sendq, skb);
401 get_random_bytes(&x, sizeof(x));
405 static struct layer2 *
406 findtei(struct manager *mgr, int tei)
411 read_lock_irqsave(&mgr->lock, flags);
412 list_for_each_entry(l2, &mgr->layer2, list) {
413 if ((l2->sapi == 0) && (l2->tei > 0) &&
414 (l2->tei != GROUP_TEI) && (l2->tei == tei))
419 read_unlock_irqrestore(&mgr->lock, flags);
424 put_tei_msg(struct manager *mgr, u_char m_id, unsigned int ri, u_char tei)
429 bp[0] = (TEI_SAPI << 2);
430 if (test_bit(MGR_OPT_NETWORK, &mgr->options))
431 bp[0] |= 2; /* CR:=1 for net command */
432 bp[1] = (GROUP_TEI << 1) | 0x1;
434 bp[3] = TEI_ENTITY_ID;
438 bp[7] = (tei << 1) | 1;
439 skb = _alloc_mISDN_skb(PH_DATA_REQ, new_id(mgr),
442 printk(KERN_WARNING "%s: no skb for tei msg\n", __func__);
445 mgr_send_down(mgr, skb);
449 tei_id_request(struct FsmInst *fi, int event, void *arg)
451 struct teimgr *tm = fi->userdata;
453 if (tm->l2->tei != GROUP_TEI) {
454 tm->tei_m.printdebug(&tm->tei_m,
455 "assign request for allready assigned tei %d",
459 tm->ri = random_ri();
460 if (*debug & DEBUG_L2_TEI)
461 tm->tei_m.printdebug(&tm->tei_m,
462 "assign request ri %d", tm->ri);
463 put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI);
464 mISDN_FsmChangeState(fi, ST_TEI_IDREQ);
465 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 1);
470 tei_id_assign(struct FsmInst *fi, int event, void *arg)
472 struct teimgr *tm = fi->userdata;
477 ri = ((unsigned int) *dp++ << 8);
481 if (*debug & DEBUG_L2_TEI)
482 tm->tei_m.printdebug(fi, "identity assign ri %d tei %d",
484 l2 = findtei(tm->mgr, tei);
485 if (l2) { /* same tei is in use */
486 if (ri != l2->tm->ri) {
487 tm->tei_m.printdebug(fi,
488 "possible duplicate assignment tei %d", tei);
489 tei_l2(l2, MDL_ERROR_RSP, 0);
491 } else if (ri == tm->ri) {
492 mISDN_FsmDelTimer(&tm->timer, 1);
493 mISDN_FsmChangeState(fi, ST_TEI_NOP);
494 tei_l2(tm->l2, MDL_ASSIGN_REQ, tei);
499 tei_id_test_dup(struct FsmInst *fi, int event, void *arg)
501 struct teimgr *tm = fi->userdata;
506 ri = ((unsigned int) *dp++ << 8);
510 if (*debug & DEBUG_L2_TEI)
511 tm->tei_m.printdebug(fi, "foreign identity assign ri %d tei %d",
513 l2 = findtei(tm->mgr, tei);
514 if (l2) { /* same tei is in use */
515 if (ri != l2->tm->ri) { /* and it wasn't our request */
516 tm->tei_m.printdebug(fi,
517 "possible duplicate assignment tei %d", tei);
518 mISDN_FsmEvent(&l2->tm->tei_m, EV_VERIFY, NULL);
524 tei_id_denied(struct FsmInst *fi, int event, void *arg)
526 struct teimgr *tm = fi->userdata;
530 ri = ((unsigned int) *dp++ << 8);
534 if (*debug & DEBUG_L2_TEI)
535 tm->tei_m.printdebug(fi, "identity denied ri %d tei %d",
540 tei_id_chk_req(struct FsmInst *fi, int event, void *arg)
542 struct teimgr *tm = fi->userdata;
547 if (*debug & DEBUG_L2_TEI)
548 tm->tei_m.printdebug(fi, "identity check req tei %d", tei);
549 if ((tm->l2->tei != GROUP_TEI) && ((tei == GROUP_TEI) ||
550 (tei == tm->l2->tei))) {
551 mISDN_FsmDelTimer(&tm->timer, 4);
552 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP);
553 put_tei_msg(tm->mgr, ID_CHK_RES, random_ri(), tm->l2->tei);
558 tei_id_remove(struct FsmInst *fi, int event, void *arg)
560 struct teimgr *tm = fi->userdata;
565 if (*debug & DEBUG_L2_TEI)
566 tm->tei_m.printdebug(fi, "identity remove tei %d", tei);
567 if ((tm->l2->tei != GROUP_TEI) &&
568 ((tei == GROUP_TEI) || (tei == tm->l2->tei))) {
569 mISDN_FsmDelTimer(&tm->timer, 5);
570 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_NOP);
571 tei_l2(tm->l2, MDL_REMOVE_REQ, 0);
576 tei_id_verify(struct FsmInst *fi, int event, void *arg)
578 struct teimgr *tm = fi->userdata;
580 if (*debug & DEBUG_L2_TEI)
581 tm->tei_m.printdebug(fi, "id verify request for tei %d",
583 put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei);
584 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY);
585 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2);
590 tei_id_req_tout(struct FsmInst *fi, int event, void *arg)
592 struct teimgr *tm = fi->userdata;
595 tm->ri = random_ri();
596 if (*debug & DEBUG_L2_TEI)
597 tm->tei_m.printdebug(fi, "assign req(%d) ri %d",
598 4 - tm->nval, tm->ri);
599 put_tei_msg(tm->mgr, ID_REQUEST, tm->ri, GROUP_TEI);
600 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 3);
602 tm->tei_m.printdebug(fi, "assign req failed");
603 tei_l2(tm->l2, MDL_ERROR_RSP, 0);
604 mISDN_FsmChangeState(fi, ST_TEI_NOP);
609 tei_id_ver_tout(struct FsmInst *fi, int event, void *arg)
611 struct teimgr *tm = fi->userdata;
614 if (*debug & DEBUG_L2_TEI)
615 tm->tei_m.printdebug(fi,
616 "id verify req(%d) for tei %d",
617 3 - tm->nval, tm->l2->tei);
618 put_tei_msg(tm->mgr, ID_VERIFY, 0, tm->l2->tei);
619 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4);
621 tm->tei_m.printdebug(fi, "verify req for tei %d failed",
623 tei_l2(tm->l2, MDL_REMOVE_REQ, 0);
624 mISDN_FsmChangeState(fi, ST_TEI_NOP);
628 static struct FsmNode TeiFnListUser[] =
630 {ST_TEI_NOP, EV_IDREQ, tei_id_request},
631 {ST_TEI_NOP, EV_ASSIGN, tei_id_test_dup},
632 {ST_TEI_NOP, EV_VERIFY, tei_id_verify},
633 {ST_TEI_NOP, EV_REMOVE, tei_id_remove},
634 {ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req},
635 {ST_TEI_IDREQ, EV_TIMER, tei_id_req_tout},
636 {ST_TEI_IDREQ, EV_ASSIGN, tei_id_assign},
637 {ST_TEI_IDREQ, EV_DENIED, tei_id_denied},
638 {ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout},
639 {ST_TEI_IDVERIFY, EV_REMOVE, tei_id_remove},
640 {ST_TEI_IDVERIFY, EV_CHKREQ, tei_id_chk_req},
644 tei_l2remove(struct layer2 *l2)
646 put_tei_msg(l2->tm->mgr, ID_REMOVE, 0, l2->tei);
647 tei_l2(l2, MDL_REMOVE_REQ, 0);
648 list_del(&l2->ch.list);
649 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
653 tei_assign_req(struct FsmInst *fi, int event, void *arg)
655 struct teimgr *tm = fi->userdata;
658 if (tm->l2->tei == GROUP_TEI) {
659 tm->tei_m.printdebug(&tm->tei_m,
660 "net tei assign request without tei");
663 tm->ri = ((unsigned int) *dp++ << 8);
665 if (*debug & DEBUG_L2_TEI)
666 tm->tei_m.printdebug(&tm->tei_m,
667 "net assign request ri %d teim %d", tm->ri, *dp);
668 put_tei_msg(tm->mgr, ID_ASSIGNED, tm->ri, tm->l2->tei);
669 mISDN_FsmChangeState(fi, ST_TEI_NOP);
673 tei_id_chk_req_net(struct FsmInst *fi, int event, void *arg)
675 struct teimgr *tm = fi->userdata;
677 if (*debug & DEBUG_L2_TEI)
678 tm->tei_m.printdebug(fi, "id check request for tei %d",
681 put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei);
682 mISDN_FsmChangeState(&tm->tei_m, ST_TEI_IDVERIFY);
683 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 2);
688 tei_id_chk_resp(struct FsmInst *fi, int event, void *arg)
690 struct teimgr *tm = fi->userdata;
695 if (*debug & DEBUG_L2_TEI)
696 tm->tei_m.printdebug(fi, "identity check resp tei %d", tei);
697 if (tei == tm->l2->tei)
702 tei_id_verify_net(struct FsmInst *fi, int event, void *arg)
704 struct teimgr *tm = fi->userdata;
709 if (*debug & DEBUG_L2_TEI)
710 tm->tei_m.printdebug(fi, "identity verify req tei %d/%d",
712 if (tei == tm->l2->tei)
713 tei_id_chk_req_net(fi, event, arg);
717 tei_id_ver_tout_net(struct FsmInst *fi, int event, void *arg)
719 struct teimgr *tm = fi->userdata;
722 if (*debug & DEBUG_L2_TEI)
723 tm->tei_m.printdebug(fi,
724 "check req for tei %d sucessful\n", tm->l2->tei);
725 mISDN_FsmChangeState(fi, ST_TEI_NOP);
726 } else if (tm->rcnt > 1) {
727 /* duplicate assignment; remove */
728 tei_l2remove(tm->l2);
729 } else if (--tm->nval) {
730 if (*debug & DEBUG_L2_TEI)
731 tm->tei_m.printdebug(fi,
732 "id check req(%d) for tei %d",
733 3 - tm->nval, tm->l2->tei);
734 put_tei_msg(tm->mgr, ID_CHK_REQ, 0, tm->l2->tei);
735 mISDN_FsmAddTimer(&tm->timer, tm->tval, EV_TIMER, NULL, 4);
737 tm->tei_m.printdebug(fi, "check req for tei %d failed",
739 mISDN_FsmChangeState(fi, ST_TEI_NOP);
740 tei_l2remove(tm->l2);
744 static struct FsmNode TeiFnListNet[] =
746 {ST_TEI_NOP, EV_ASSIGN_REQ, tei_assign_req},
747 {ST_TEI_NOP, EV_VERIFY, tei_id_verify_net},
748 {ST_TEI_NOP, EV_CHKREQ, tei_id_chk_req_net},
749 {ST_TEI_IDVERIFY, EV_TIMER, tei_id_ver_tout_net},
750 {ST_TEI_IDVERIFY, EV_CHKRESP, tei_id_chk_resp},
754 tei_ph_data_ind(struct teimgr *tm, u_int mt, u_char *dp, int len)
756 if (test_bit(FLG_FIXED_TEI, &tm->l2->flag))
758 if (*debug & DEBUG_L2_TEI)
759 tm->tei_m.printdebug(&tm->tei_m, "tei handler mt %x", mt);
760 if (mt == ID_ASSIGNED)
761 mISDN_FsmEvent(&tm->tei_m, EV_ASSIGN, dp);
762 else if (mt == ID_DENIED)
763 mISDN_FsmEvent(&tm->tei_m, EV_DENIED, dp);
764 else if (mt == ID_CHK_REQ)
765 mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, dp);
766 else if (mt == ID_REMOVE)
767 mISDN_FsmEvent(&tm->tei_m, EV_REMOVE, dp);
768 else if (mt == ID_VERIFY)
769 mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, dp);
770 else if (mt == ID_CHK_RES)
771 mISDN_FsmEvent(&tm->tei_m, EV_CHKRESP, dp);
774 static struct layer2 *
775 create_new_tei(struct manager *mgr, int tei)
785 test_and_set_bit(OPTION_L2_FIXEDTEI, &opt);
786 if (mgr->ch.st->dev->Dprotocols
787 & ((1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1)))
788 test_and_set_bit(OPTION_L2_PMX, &opt);
789 l2 = create_l2(mgr->up, ISDN_P_LAPD_NT, (u_int)opt, (u_long)tei);
791 printk(KERN_WARNING "%s:no memory for layer2\n", __func__);
794 l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL);
797 printk(KERN_WARNING "%s:no memory for teimgr\n", __func__);
802 l2->tm->tei_m.debug = *debug & DEBUG_L2_TEIFSM;
803 l2->tm->tei_m.userdata = l2->tm;
804 l2->tm->tei_m.printdebug = tei_debug;
805 l2->tm->tei_m.fsm = &teifsmn;
806 l2->tm->tei_m.state = ST_TEI_NOP;
807 l2->tm->tval = 2000; /* T202 2 sec */
808 mISDN_FsmInitTimer(&l2->tm->tei_m, &l2->tm->timer);
809 write_lock_irqsave(&mgr->lock, flags);
810 id = get_free_id(mgr);
811 list_add_tail(&l2->list, &mgr->layer2);
812 write_unlock_irqrestore(&mgr->lock, flags);
814 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
815 printk(KERN_WARNING "%s:no free id\n", __func__);
819 __add_layer2(&l2->ch, mgr->ch.st);
820 l2->ch.recv = mgr->ch.recv;
821 l2->ch.peer = mgr->ch.peer;
822 l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL);
828 new_tei_req(struct manager *mgr, u_char *dp)
837 tei = get_free_tei(mgr);
839 printk(KERN_WARNING "%s:No free tei\n", __func__);
842 l2 = create_new_tei(mgr, tei);
846 mISDN_FsmEvent(&l2->tm->tei_m, EV_ASSIGN_REQ, dp);
849 put_tei_msg(mgr, ID_DENIED, ri, GROUP_TEI);
853 ph_data_ind(struct manager *mgr, struct sk_buff *skb)
861 if (*debug & DEBUG_L2_TEI)
862 printk(KERN_DEBUG "%s: short mgr frame %d/8\n",
866 if (*debug & DEBUG_L2_TEI)
868 if ((skb->data[0] >> 2) != TEI_SAPI) /* not for us */
870 if (skb->data[0] & 1) /* EA0 formal error */
872 if (!(skb->data[1] & 1)) /* EA1 formal error */
874 if ((skb->data[1] >> 1) != GROUP_TEI) /* not for us */
876 if ((skb->data[2] & 0xef) != UI) /* not UI */
878 if (skb->data[3] != TEI_ENTITY_ID) /* not tei entity */
885 if (!test_bit(MGR_OPT_NETWORK, &mgr->options))
892 if (test_bit(MGR_OPT_NETWORK, &mgr->options))
899 if (mt == ID_REQUEST) {
900 new_tei_req(mgr, &skb->data[4]);
903 read_lock_irqsave(&mgr->lock, flags);
904 list_for_each_entry(l2, &mgr->layer2, list) {
905 tei_ph_data_ind(l2->tm, mt, &skb->data[4], skb->len - 4);
907 read_unlock_irqrestore(&mgr->lock, flags);
913 l2_tei(struct layer2 *l2, u_int cmd, u_long arg)
915 struct teimgr *tm = l2->tm;
917 if (test_bit(FLG_FIXED_TEI, &l2->flag))
919 if (*debug & DEBUG_L2_TEI)
920 printk(KERN_DEBUG "%s: cmd(%x)\n", __func__, cmd);
923 mISDN_FsmEvent(&tm->tei_m, EV_IDREQ, NULL);
926 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
927 mISDN_FsmEvent(&tm->tei_m, EV_CHKREQ, &l2->tei);
928 if (test_bit(MGR_OPT_USER, &tm->mgr->options))
929 mISDN_FsmEvent(&tm->tei_m, EV_VERIFY, NULL);
931 case MDL_STATUS_UP_IND:
932 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
933 mISDN_FsmEvent(&tm->mgr->deact, EV_ACTIVATE, NULL);
935 case MDL_STATUS_DOWN_IND:
936 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
937 mISDN_FsmEvent(&tm->mgr->deact, EV_DEACTIVATE, NULL);
939 case MDL_STATUS_UI_IND:
940 if (test_bit(MGR_OPT_NETWORK, &tm->mgr->options))
941 mISDN_FsmEvent(&tm->mgr->deact, EV_UI, NULL);
948 TEIrelease(struct layer2 *l2)
950 struct teimgr *tm = l2->tm;
953 mISDN_FsmDelTimer(&tm->timer, 1);
954 write_lock_irqsave(&tm->mgr->lock, flags);
956 write_unlock_irqrestore(&tm->mgr->lock, flags);
962 create_teimgr(struct manager *mgr, struct channel_req *crq)
969 if (*debug & DEBUG_L2_TEI)
970 printk(KERN_DEBUG "%s: %s proto(%x) adr(%d %d %d %d)\n",
971 __func__, dev_name(&mgr->ch.st->dev->dev),
972 crq->protocol, crq->adr.dev, crq->adr.channel,
973 crq->adr.sapi, crq->adr.tei);
974 if (crq->adr.sapi != 0) /* not supported yet */
976 if (crq->adr.tei > GROUP_TEI)
978 if (crq->adr.tei < 64)
979 test_and_set_bit(OPTION_L2_FIXEDTEI, &opt);
980 if (crq->adr.tei == 0)
981 test_and_set_bit(OPTION_L2_PTP, &opt);
982 if (test_bit(MGR_OPT_NETWORK, &mgr->options)) {
983 if (crq->protocol == ISDN_P_LAPD_TE)
984 return -EPROTONOSUPPORT;
985 if ((crq->adr.tei != 0) && (crq->adr.tei != 127))
989 "%s: only one network manager is allowed\n",
993 } else if (test_bit(MGR_OPT_USER, &mgr->options)) {
994 if (crq->protocol == ISDN_P_LAPD_NT)
995 return -EPROTONOSUPPORT;
996 if ((crq->adr.tei >= 64) && (crq->adr.tei < GROUP_TEI))
997 return -EINVAL; /* dyn tei */
999 if (crq->protocol == ISDN_P_LAPD_NT)
1000 test_and_set_bit(MGR_OPT_NETWORK, &mgr->options);
1001 if (crq->protocol == ISDN_P_LAPD_TE)
1002 test_and_set_bit(MGR_OPT_USER, &mgr->options);
1004 if (mgr->ch.st->dev->Dprotocols
1005 & ((1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1)))
1006 test_and_set_bit(OPTION_L2_PMX, &opt);
1007 if ((crq->protocol == ISDN_P_LAPD_NT) && (crq->adr.tei == 127)) {
1009 id = DL_INFO_L2_CONNECT;
1010 teiup_create(mgr, DL_INFORMATION_IND, sizeof(id), &id);
1012 if (!list_empty(&mgr->layer2)) {
1013 read_lock_irqsave(&mgr->lock, flags);
1014 list_for_each_entry(l2, &mgr->layer2, list) {
1016 l2->ch.ctrl(&l2->ch, OPEN_CHANNEL, NULL);
1018 read_unlock_irqrestore(&mgr->lock, flags);
1022 l2 = create_l2(crq->ch, crq->protocol, (u_int)opt,
1023 (u_long)crq->adr.tei);
1026 l2->tm = kzalloc(sizeof(struct teimgr), GFP_KERNEL);
1029 printk(KERN_ERR "kmalloc teimgr failed\n");
1034 l2->tm->tei_m.debug = *debug & DEBUG_L2_TEIFSM;
1035 l2->tm->tei_m.userdata = l2->tm;
1036 l2->tm->tei_m.printdebug = tei_debug;
1037 if (crq->protocol == ISDN_P_LAPD_TE) {
1038 l2->tm->tei_m.fsm = &teifsmu;
1039 l2->tm->tei_m.state = ST_TEI_NOP;
1040 l2->tm->tval = 1000; /* T201 1 sec */
1042 l2->tm->tei_m.fsm = &teifsmn;
1043 l2->tm->tei_m.state = ST_TEI_NOP;
1044 l2->tm->tval = 2000; /* T202 2 sec */
1046 mISDN_FsmInitTimer(&l2->tm->tei_m, &l2->tm->timer);
1047 write_lock_irqsave(&mgr->lock, flags);
1048 id = get_free_id(mgr);
1049 list_add_tail(&l2->list, &mgr->layer2);
1050 write_unlock_irqrestore(&mgr->lock, flags);
1052 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1063 mgr_send(struct mISDNchannel *ch, struct sk_buff *skb)
1065 struct manager *mgr;
1066 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1069 mgr = container_of(ch, struct manager, ch);
1070 if (*debug & DEBUG_L2_RECV)
1071 printk(KERN_DEBUG "%s: prim(%x) id(%x)\n",
1072 __func__, hh->prim, hh->id);
1075 mISDN_FsmEvent(&mgr->deact, EV_UI, NULL);
1076 ret = ph_data_ind(mgr, skb);
1079 do_ack(mgr, hh->id);
1082 case PH_ACTIVATE_IND:
1083 test_and_set_bit(MGR_PH_ACTIVE, &mgr->options);
1084 mISDN_FsmEvent(&mgr->deact, EV_ACTIVATE_IND, NULL);
1088 case PH_DEACTIVATE_IND:
1089 test_and_clear_bit(MGR_PH_ACTIVE, &mgr->options);
1090 mISDN_FsmEvent(&mgr->deact, EV_DEACTIVATE_IND, NULL);
1093 case DL_UNITDATA_REQ:
1094 return dl_unit_data(mgr, skb);
1102 free_teimanager(struct manager *mgr)
1104 struct layer2 *l2, *nl2;
1106 if (test_bit(MGR_OPT_NETWORK, &mgr->options)) {
1107 /* not locked lock is taken in release tei */
1109 if (test_bit(OPTION_L2_CLEANUP, &mgr->options)) {
1110 list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1111 put_tei_msg(mgr, ID_REMOVE, 0, l2->tei);
1112 mutex_lock(&mgr->ch.st->lmutex);
1113 list_del(&l2->ch.list);
1114 mutex_unlock(&mgr->ch.st->lmutex);
1115 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1117 test_and_clear_bit(MGR_OPT_NETWORK, &mgr->options);
1119 list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1124 if (test_bit(MGR_OPT_USER, &mgr->options)) {
1125 if (list_empty(&mgr->layer2))
1126 test_and_clear_bit(MGR_OPT_USER, &mgr->options);
1128 mgr->ch.st->dev->D.ctrl(&mgr->ch.st->dev->D, CLOSE_CHANNEL, NULL);
1133 ctrl_teimanager(struct manager *mgr, void *arg)
1135 /* currently we only have one option */
1136 int clean = *((int *)arg);
1139 test_and_set_bit(OPTION_L2_CLEANUP, &mgr->options);
1141 test_and_clear_bit(OPTION_L2_CLEANUP, &mgr->options);
1145 /* This function does create a L2 for fixed TEI in NT Mode */
1147 check_data(struct manager *mgr, struct sk_buff *skb)
1149 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1153 if (*debug & DEBUG_L2_CTRL)
1154 printk(KERN_DEBUG "%s: prim(%x) id(%x)\n",
1155 __func__, hh->prim, hh->id);
1156 if (test_bit(MGR_OPT_USER, &mgr->options))
1158 if (hh->prim != PH_DATA_IND)
1162 if (skb->data[0] != 0)
1163 /* only SAPI 0 command */
1165 if (!(skb->data[1] & 1)) /* invalid EA1 */
1167 tei = skb->data[1] >> 0;
1168 if (tei > 63) /* not a fixed tei */
1170 if ((skb->data[2] & ~0x10) != SABME)
1172 /* We got a SABME for a fixed TEI */
1173 l2 = create_new_tei(mgr, tei);
1176 ret = l2->ch.send(&l2->ch, skb);
1181 delete_teimanager(struct mISDNchannel *ch)
1183 struct manager *mgr;
1184 struct layer2 *l2, *nl2;
1186 mgr = container_of(ch, struct manager, ch);
1187 /* not locked lock is taken in release tei */
1188 list_for_each_entry_safe(l2, nl2, &mgr->layer2, list) {
1189 mutex_lock(&mgr->ch.st->lmutex);
1190 list_del(&l2->ch.list);
1191 mutex_unlock(&mgr->ch.st->lmutex);
1192 l2->ch.ctrl(&l2->ch, CLOSE_CHANNEL, NULL);
1194 list_del(&mgr->ch.list);
1195 list_del(&mgr->bcast.list);
1196 skb_queue_purge(&mgr->sendq);
1201 mgr_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1203 struct manager *mgr;
1206 mgr = container_of(ch, struct manager, ch);
1207 if (*debug & DEBUG_L2_CTRL)
1208 printk(KERN_DEBUG "%s(%x, %p)\n", __func__, cmd, arg);
1211 ret = create_teimgr(mgr, arg);
1214 ret = free_teimanager(mgr);
1216 case CONTROL_CHANNEL:
1217 ret = ctrl_teimanager(mgr, arg);
1220 ret = check_data(mgr, arg);
1227 mgr_bcast(struct mISDNchannel *ch, struct sk_buff *skb)
1229 struct manager *mgr = container_of(ch, struct manager, bcast);
1230 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1231 struct sk_buff *cskb = NULL;
1236 read_lock_irqsave(&mgr->lock, flags);
1237 list_for_each_entry(l2, &mgr->layer2, list) {
1238 if ((hh->id & MISDN_ID_SAPI_MASK) ==
1239 (l2->ch.addr & MISDN_ID_SAPI_MASK)) {
1240 if (list_is_last(&l2->list, &mgr->layer2)) {
1245 cskb = skb_copy(skb, GFP_KERNEL);
1248 ret = l2->ch.send(&l2->ch, cskb);
1250 if (*debug & DEBUG_SEND_ERR)
1252 "%s ch%d prim(%x) addr(%x)"
1254 __func__, l2->ch.nr,
1255 hh->prim, l2->ch.addr, ret);
1259 printk(KERN_WARNING "%s ch%d addr %x no mem\n",
1260 __func__, ch->nr, ch->addr);
1266 read_unlock_irqrestore(&mgr->lock, flags);
1268 dev_kfree_skb(cskb);
1275 mgr_bcast_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1282 create_teimanager(struct mISDNdevice *dev)
1284 struct manager *mgr;
1286 mgr = kzalloc(sizeof(struct manager), GFP_KERNEL);
1289 INIT_LIST_HEAD(&mgr->layer2);
1290 rwlock_init(&mgr->lock);
1291 skb_queue_head_init(&mgr->sendq);
1293 mgr->lastid = MISDN_ID_NONE;
1294 mgr->ch.send = mgr_send;
1295 mgr->ch.ctrl = mgr_ctrl;
1296 mgr->ch.st = dev->D.st;
1297 set_channel_address(&mgr->ch, TEI_SAPI, GROUP_TEI);
1298 add_layer2(&mgr->ch, dev->D.st);
1299 mgr->bcast.send = mgr_bcast;
1300 mgr->bcast.ctrl = mgr_bcast_ctrl;
1301 mgr->bcast.st = dev->D.st;
1302 set_channel_address(&mgr->bcast, 0, GROUP_TEI);
1303 add_layer2(&mgr->bcast, dev->D.st);
1304 mgr->deact.debug = *debug & DEBUG_MANAGER;
1305 mgr->deact.userdata = mgr;
1306 mgr->deact.printdebug = da_debug;
1307 mgr->deact.fsm = &deactfsm;
1308 mgr->deact.state = ST_L1_DEACT;
1309 mISDN_FsmInitTimer(&mgr->deact, &mgr->datimer);
1310 dev->teimgr = &mgr->ch;
1314 int TEIInit(u_int *deb)
1317 teifsmu.state_count = TEI_STATE_COUNT;
1318 teifsmu.event_count = TEI_EVENT_COUNT;
1319 teifsmu.strEvent = strTeiEvent;
1320 teifsmu.strState = strTeiState;
1321 mISDN_FsmNew(&teifsmu, TeiFnListUser, ARRAY_SIZE(TeiFnListUser));
1322 teifsmn.state_count = TEI_STATE_COUNT;
1323 teifsmn.event_count = TEI_EVENT_COUNT;
1324 teifsmn.strEvent = strTeiEvent;
1325 teifsmn.strState = strTeiState;
1326 mISDN_FsmNew(&teifsmn, TeiFnListNet, ARRAY_SIZE(TeiFnListNet));
1327 deactfsm.state_count = DEACT_STATE_COUNT;
1328 deactfsm.event_count = DEACT_EVENT_COUNT;
1329 deactfsm.strEvent = strDeactEvent;
1330 deactfsm.strState = strDeactState;
1331 mISDN_FsmNew(&deactfsm, DeactFnList, ARRAY_SIZE(DeactFnList));
1337 mISDN_FsmFree(&teifsmu);
1338 mISDN_FsmFree(&teifsmn);
1339 mISDN_FsmFree(&deactfsm);