0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/slab.h>
0011 #include <linux/module.h>
0012 #include <linux/mISDNhw.h>
0013 #include "core.h"
0014 #include "layer1.h"
0015 #include "fsm.h"
0016
0017 static u_int *debug;
0018
0019 struct layer1 {
0020 u_long Flags;
0021 struct FsmInst l1m;
0022 struct FsmTimer timer3;
0023 struct FsmTimer timerX;
0024 int delay;
0025 int t3_value;
0026 struct dchannel *dch;
0027 dchannel_l1callback *dcb;
0028 };
0029
0030 #define TIMER3_DEFAULT_VALUE 7000
0031
0032 static
0033 struct Fsm l1fsm_s = {NULL, 0, 0, NULL, NULL};
0034
0035 enum {
0036 ST_L1_F2,
0037 ST_L1_F3,
0038 ST_L1_F4,
0039 ST_L1_F5,
0040 ST_L1_F6,
0041 ST_L1_F7,
0042 ST_L1_F8,
0043 };
0044
0045 #define L1S_STATE_COUNT (ST_L1_F8 + 1)
0046
0047 static char *strL1SState[] =
0048 {
0049 "ST_L1_F2",
0050 "ST_L1_F3",
0051 "ST_L1_F4",
0052 "ST_L1_F5",
0053 "ST_L1_F6",
0054 "ST_L1_F7",
0055 "ST_L1_F8",
0056 };
0057
0058 enum {
0059 EV_PH_ACTIVATE,
0060 EV_PH_DEACTIVATE,
0061 EV_RESET_IND,
0062 EV_DEACT_CNF,
0063 EV_DEACT_IND,
0064 EV_POWER_UP,
0065 EV_ANYSIG_IND,
0066 EV_INFO2_IND,
0067 EV_INFO4_IND,
0068 EV_TIMER_DEACT,
0069 EV_TIMER_ACT,
0070 EV_TIMER3,
0071 };
0072
0073 #define L1_EVENT_COUNT (EV_TIMER3 + 1)
0074
0075 static char *strL1Event[] =
0076 {
0077 "EV_PH_ACTIVATE",
0078 "EV_PH_DEACTIVATE",
0079 "EV_RESET_IND",
0080 "EV_DEACT_CNF",
0081 "EV_DEACT_IND",
0082 "EV_POWER_UP",
0083 "EV_ANYSIG_IND",
0084 "EV_INFO2_IND",
0085 "EV_INFO4_IND",
0086 "EV_TIMER_DEACT",
0087 "EV_TIMER_ACT",
0088 "EV_TIMER3",
0089 };
0090
0091 static void
0092 l1m_debug(struct FsmInst *fi, char *fmt, ...)
0093 {
0094 struct layer1 *l1 = fi->userdata;
0095 struct va_format vaf;
0096 va_list va;
0097
0098 va_start(va, fmt);
0099
0100 vaf.fmt = fmt;
0101 vaf.va = &va;
0102
0103 printk(KERN_DEBUG "%s: %pV\n", dev_name(&l1->dch->dev.dev), &vaf);
0104
0105 va_end(va);
0106 }
0107
0108 static void
0109 l1_reset(struct FsmInst *fi, int event, void *arg)
0110 {
0111 mISDN_FsmChangeState(fi, ST_L1_F3);
0112 }
0113
0114 static void
0115 l1_deact_cnf(struct FsmInst *fi, int event, void *arg)
0116 {
0117 struct layer1 *l1 = fi->userdata;
0118
0119 mISDN_FsmChangeState(fi, ST_L1_F3);
0120 if (test_bit(FLG_L1_ACTIVATING, &l1->Flags))
0121 l1->dcb(l1->dch, HW_POWERUP_REQ);
0122 }
0123
0124 static void
0125 l1_deact_req_s(struct FsmInst *fi, int event, void *arg)
0126 {
0127 struct layer1 *l1 = fi->userdata;
0128
0129 mISDN_FsmChangeState(fi, ST_L1_F3);
0130 mISDN_FsmRestartTimer(&l1->timerX, 550, EV_TIMER_DEACT, NULL, 2);
0131 test_and_set_bit(FLG_L1_DEACTTIMER, &l1->Flags);
0132 }
0133
0134 static void
0135 l1_power_up_s(struct FsmInst *fi, int event, void *arg)
0136 {
0137 struct layer1 *l1 = fi->userdata;
0138
0139 if (test_bit(FLG_L1_ACTIVATING, &l1->Flags)) {
0140 mISDN_FsmChangeState(fi, ST_L1_F4);
0141 l1->dcb(l1->dch, INFO3_P8);
0142 } else
0143 mISDN_FsmChangeState(fi, ST_L1_F3);
0144 }
0145
0146 static void
0147 l1_go_F5(struct FsmInst *fi, int event, void *arg)
0148 {
0149 mISDN_FsmChangeState(fi, ST_L1_F5);
0150 }
0151
0152 static void
0153 l1_go_F8(struct FsmInst *fi, int event, void *arg)
0154 {
0155 mISDN_FsmChangeState(fi, ST_L1_F8);
0156 }
0157
0158 static void
0159 l1_info2_ind(struct FsmInst *fi, int event, void *arg)
0160 {
0161 struct layer1 *l1 = fi->userdata;
0162
0163 mISDN_FsmChangeState(fi, ST_L1_F6);
0164 l1->dcb(l1->dch, INFO3_P8);
0165 }
0166
0167 static void
0168 l1_info4_ind(struct FsmInst *fi, int event, void *arg)
0169 {
0170 struct layer1 *l1 = fi->userdata;
0171
0172 mISDN_FsmChangeState(fi, ST_L1_F7);
0173 l1->dcb(l1->dch, INFO3_P8);
0174 if (test_and_clear_bit(FLG_L1_DEACTTIMER, &l1->Flags))
0175 mISDN_FsmDelTimer(&l1->timerX, 4);
0176 if (!test_bit(FLG_L1_ACTIVATED, &l1->Flags)) {
0177 if (test_and_clear_bit(FLG_L1_T3RUN, &l1->Flags))
0178 mISDN_FsmDelTimer(&l1->timer3, 3);
0179 mISDN_FsmRestartTimer(&l1->timerX, 110, EV_TIMER_ACT, NULL, 2);
0180 test_and_set_bit(FLG_L1_ACTTIMER, &l1->Flags);
0181 }
0182 }
0183
0184 static void
0185 l1_timer3(struct FsmInst *fi, int event, void *arg)
0186 {
0187 struct layer1 *l1 = fi->userdata;
0188
0189 test_and_clear_bit(FLG_L1_T3RUN, &l1->Flags);
0190 if (test_and_clear_bit(FLG_L1_ACTIVATING, &l1->Flags)) {
0191 if (test_and_clear_bit(FLG_L1_DBLOCKED, &l1->Flags))
0192 l1->dcb(l1->dch, HW_D_NOBLOCKED);
0193 l1->dcb(l1->dch, PH_DEACTIVATE_IND);
0194 }
0195 if (l1->l1m.state != ST_L1_F6) {
0196 mISDN_FsmChangeState(fi, ST_L1_F3);
0197
0198 }
0199 }
0200
0201 static void
0202 l1_timer_act(struct FsmInst *fi, int event, void *arg)
0203 {
0204 struct layer1 *l1 = fi->userdata;
0205
0206 test_and_clear_bit(FLG_L1_ACTTIMER, &l1->Flags);
0207 test_and_set_bit(FLG_L1_ACTIVATED, &l1->Flags);
0208 l1->dcb(l1->dch, PH_ACTIVATE_IND);
0209 }
0210
0211 static void
0212 l1_timer_deact(struct FsmInst *fi, int event, void *arg)
0213 {
0214 struct layer1 *l1 = fi->userdata;
0215
0216 test_and_clear_bit(FLG_L1_DEACTTIMER, &l1->Flags);
0217 test_and_clear_bit(FLG_L1_ACTIVATED, &l1->Flags);
0218 if (test_and_clear_bit(FLG_L1_DBLOCKED, &l1->Flags))
0219 l1->dcb(l1->dch, HW_D_NOBLOCKED);
0220 l1->dcb(l1->dch, PH_DEACTIVATE_IND);
0221 l1->dcb(l1->dch, HW_DEACT_REQ);
0222 }
0223
0224 static void
0225 l1_activate_s(struct FsmInst *fi, int event, void *arg)
0226 {
0227 struct layer1 *l1 = fi->userdata;
0228
0229 mISDN_FsmRestartTimer(&l1->timer3, l1->t3_value, EV_TIMER3, NULL, 2);
0230 test_and_set_bit(FLG_L1_T3RUN, &l1->Flags);
0231
0232 l1->dcb(l1->dch, HW_RESET_REQ);
0233 }
0234
0235 static void
0236 l1_activate_no(struct FsmInst *fi, int event, void *arg)
0237 {
0238 struct layer1 *l1 = fi->userdata;
0239
0240 if ((!test_bit(FLG_L1_DEACTTIMER, &l1->Flags)) &&
0241 (!test_bit(FLG_L1_T3RUN, &l1->Flags))) {
0242 test_and_clear_bit(FLG_L1_ACTIVATING, &l1->Flags);
0243 if (test_and_clear_bit(FLG_L1_DBLOCKED, &l1->Flags))
0244 l1->dcb(l1->dch, HW_D_NOBLOCKED);
0245 l1->dcb(l1->dch, PH_DEACTIVATE_IND);
0246 }
0247 }
0248
0249 static struct FsmNode L1SFnList[] =
0250 {
0251 {ST_L1_F3, EV_PH_ACTIVATE, l1_activate_s},
0252 {ST_L1_F6, EV_PH_ACTIVATE, l1_activate_no},
0253 {ST_L1_F8, EV_PH_ACTIVATE, l1_activate_no},
0254 {ST_L1_F3, EV_RESET_IND, l1_reset},
0255 {ST_L1_F4, EV_RESET_IND, l1_reset},
0256 {ST_L1_F5, EV_RESET_IND, l1_reset},
0257 {ST_L1_F6, EV_RESET_IND, l1_reset},
0258 {ST_L1_F7, EV_RESET_IND, l1_reset},
0259 {ST_L1_F8, EV_RESET_IND, l1_reset},
0260 {ST_L1_F3, EV_DEACT_CNF, l1_deact_cnf},
0261 {ST_L1_F4, EV_DEACT_CNF, l1_deact_cnf},
0262 {ST_L1_F5, EV_DEACT_CNF, l1_deact_cnf},
0263 {ST_L1_F6, EV_DEACT_CNF, l1_deact_cnf},
0264 {ST_L1_F7, EV_DEACT_CNF, l1_deact_cnf},
0265 {ST_L1_F8, EV_DEACT_CNF, l1_deact_cnf},
0266 {ST_L1_F6, EV_DEACT_IND, l1_deact_req_s},
0267 {ST_L1_F7, EV_DEACT_IND, l1_deact_req_s},
0268 {ST_L1_F8, EV_DEACT_IND, l1_deact_req_s},
0269 {ST_L1_F3, EV_POWER_UP, l1_power_up_s},
0270 {ST_L1_F4, EV_ANYSIG_IND, l1_go_F5},
0271 {ST_L1_F6, EV_ANYSIG_IND, l1_go_F8},
0272 {ST_L1_F7, EV_ANYSIG_IND, l1_go_F8},
0273 {ST_L1_F3, EV_INFO2_IND, l1_info2_ind},
0274 {ST_L1_F4, EV_INFO2_IND, l1_info2_ind},
0275 {ST_L1_F5, EV_INFO2_IND, l1_info2_ind},
0276 {ST_L1_F7, EV_INFO2_IND, l1_info2_ind},
0277 {ST_L1_F8, EV_INFO2_IND, l1_info2_ind},
0278 {ST_L1_F3, EV_INFO4_IND, l1_info4_ind},
0279 {ST_L1_F4, EV_INFO4_IND, l1_info4_ind},
0280 {ST_L1_F5, EV_INFO4_IND, l1_info4_ind},
0281 {ST_L1_F6, EV_INFO4_IND, l1_info4_ind},
0282 {ST_L1_F8, EV_INFO4_IND, l1_info4_ind},
0283 {ST_L1_F3, EV_TIMER3, l1_timer3},
0284 {ST_L1_F4, EV_TIMER3, l1_timer3},
0285 {ST_L1_F5, EV_TIMER3, l1_timer3},
0286 {ST_L1_F6, EV_TIMER3, l1_timer3},
0287 {ST_L1_F8, EV_TIMER3, l1_timer3},
0288 {ST_L1_F7, EV_TIMER_ACT, l1_timer_act},
0289 {ST_L1_F3, EV_TIMER_DEACT, l1_timer_deact},
0290 {ST_L1_F4, EV_TIMER_DEACT, l1_timer_deact},
0291 {ST_L1_F5, EV_TIMER_DEACT, l1_timer_deact},
0292 {ST_L1_F6, EV_TIMER_DEACT, l1_timer_deact},
0293 {ST_L1_F7, EV_TIMER_DEACT, l1_timer_deact},
0294 {ST_L1_F8, EV_TIMER_DEACT, l1_timer_deact},
0295 };
0296
0297 static void
0298 release_l1(struct layer1 *l1) {
0299 mISDN_FsmDelTimer(&l1->timerX, 0);
0300 mISDN_FsmDelTimer(&l1->timer3, 0);
0301 if (l1->dch)
0302 l1->dch->l1 = NULL;
0303 module_put(THIS_MODULE);
0304 kfree(l1);
0305 }
0306
0307 int
0308 l1_event(struct layer1 *l1, u_int event)
0309 {
0310 int err = 0;
0311
0312 if (!l1)
0313 return -EINVAL;
0314 switch (event) {
0315 case HW_RESET_IND:
0316 mISDN_FsmEvent(&l1->l1m, EV_RESET_IND, NULL);
0317 break;
0318 case HW_DEACT_IND:
0319 mISDN_FsmEvent(&l1->l1m, EV_DEACT_IND, NULL);
0320 break;
0321 case HW_POWERUP_IND:
0322 mISDN_FsmEvent(&l1->l1m, EV_POWER_UP, NULL);
0323 break;
0324 case HW_DEACT_CNF:
0325 mISDN_FsmEvent(&l1->l1m, EV_DEACT_CNF, NULL);
0326 break;
0327 case ANYSIGNAL:
0328 mISDN_FsmEvent(&l1->l1m, EV_ANYSIG_IND, NULL);
0329 break;
0330 case LOSTFRAMING:
0331 mISDN_FsmEvent(&l1->l1m, EV_ANYSIG_IND, NULL);
0332 break;
0333 case INFO2:
0334 mISDN_FsmEvent(&l1->l1m, EV_INFO2_IND, NULL);
0335 break;
0336 case INFO4_P8:
0337 mISDN_FsmEvent(&l1->l1m, EV_INFO4_IND, NULL);
0338 break;
0339 case INFO4_P10:
0340 mISDN_FsmEvent(&l1->l1m, EV_INFO4_IND, NULL);
0341 break;
0342 case PH_ACTIVATE_REQ:
0343 if (test_bit(FLG_L1_ACTIVATED, &l1->Flags))
0344 l1->dcb(l1->dch, PH_ACTIVATE_IND);
0345 else {
0346 test_and_set_bit(FLG_L1_ACTIVATING, &l1->Flags);
0347 mISDN_FsmEvent(&l1->l1m, EV_PH_ACTIVATE, NULL);
0348 }
0349 break;
0350 case CLOSE_CHANNEL:
0351 release_l1(l1);
0352 break;
0353 default:
0354 if ((event & ~HW_TIMER3_VMASK) == HW_TIMER3_VALUE) {
0355 int val = event & HW_TIMER3_VMASK;
0356
0357 if (val < 5)
0358 val = 5;
0359 if (val > 30)
0360 val = 30;
0361 l1->t3_value = val;
0362 break;
0363 }
0364 if (*debug & DEBUG_L1)
0365 printk(KERN_DEBUG "%s %x unhandled\n",
0366 __func__, event);
0367 err = -EINVAL;
0368 }
0369 return err;
0370 }
0371 EXPORT_SYMBOL(l1_event);
0372
0373 int
0374 create_l1(struct dchannel *dch, dchannel_l1callback *dcb) {
0375 struct layer1 *nl1;
0376
0377 nl1 = kzalloc(sizeof(struct layer1), GFP_ATOMIC);
0378 if (!nl1) {
0379 printk(KERN_ERR "kmalloc struct layer1 failed\n");
0380 return -ENOMEM;
0381 }
0382 nl1->l1m.fsm = &l1fsm_s;
0383 nl1->l1m.state = ST_L1_F3;
0384 nl1->Flags = 0;
0385 nl1->t3_value = TIMER3_DEFAULT_VALUE;
0386 nl1->l1m.debug = *debug & DEBUG_L1_FSM;
0387 nl1->l1m.userdata = nl1;
0388 nl1->l1m.userint = 0;
0389 nl1->l1m.printdebug = l1m_debug;
0390 nl1->dch = dch;
0391 nl1->dcb = dcb;
0392 mISDN_FsmInitTimer(&nl1->l1m, &nl1->timer3);
0393 mISDN_FsmInitTimer(&nl1->l1m, &nl1->timerX);
0394 __module_get(THIS_MODULE);
0395 dch->l1 = nl1;
0396 return 0;
0397 }
0398 EXPORT_SYMBOL(create_l1);
0399
0400 int
0401 Isdnl1_Init(u_int *deb)
0402 {
0403 debug = deb;
0404 l1fsm_s.state_count = L1S_STATE_COUNT;
0405 l1fsm_s.event_count = L1_EVENT_COUNT;
0406 l1fsm_s.strEvent = strL1Event;
0407 l1fsm_s.strState = strL1SState;
0408 return mISDN_FsmNew(&l1fsm_s, L1SFnList, ARRAY_SIZE(L1SFnList));
0409 }
0410
0411 void
0412 Isdnl1_cleanup(void)
0413 {
0414 mISDN_FsmFree(&l1fsm_s);
0415 }