1 // SPDX-License-Identifier: GPL-2.0-only
4 * Author Karsten Keil <kkeil@novell.com>
6 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/mISDNhw.h>
22 struct FsmTimer timer3;
23 struct FsmTimer timerX;
27 dchannel_l1callback *dcb;
30 #define TIMER3_DEFAULT_VALUE 7000
33 struct Fsm l1fsm_s = {NULL, 0, 0, NULL, NULL};
45 #define L1S_STATE_COUNT (ST_L1_F8 + 1)
47 static char *strL1SState[] =
73 #define L1_EVENT_COUNT (EV_TIMER3 + 1)
75 static char *strL1Event[] =
92 l1m_debug(struct FsmInst *fi, char *fmt, ...)
94 struct layer1 *l1 = fi->userdata;
103 printk(KERN_DEBUG "%s: %pV\n", dev_name(&l1->dch->dev.dev), &vaf);
109 l1_reset(struct FsmInst *fi, int event, void *arg)
111 mISDN_FsmChangeState(fi, ST_L1_F3);
115 l1_deact_cnf(struct FsmInst *fi, int event, void *arg)
117 struct layer1 *l1 = fi->userdata;
119 mISDN_FsmChangeState(fi, ST_L1_F3);
120 if (test_bit(FLG_L1_ACTIVATING, &l1->Flags))
121 l1->dcb(l1->dch, HW_POWERUP_REQ);
125 l1_deact_req_s(struct FsmInst *fi, int event, void *arg)
127 struct layer1 *l1 = fi->userdata;
129 mISDN_FsmChangeState(fi, ST_L1_F3);
130 mISDN_FsmRestartTimer(&l1->timerX, 550, EV_TIMER_DEACT, NULL, 2);
131 test_and_set_bit(FLG_L1_DEACTTIMER, &l1->Flags);
135 l1_power_up_s(struct FsmInst *fi, int event, void *arg)
137 struct layer1 *l1 = fi->userdata;
139 if (test_bit(FLG_L1_ACTIVATING, &l1->Flags)) {
140 mISDN_FsmChangeState(fi, ST_L1_F4);
141 l1->dcb(l1->dch, INFO3_P8);
143 mISDN_FsmChangeState(fi, ST_L1_F3);
147 l1_go_F5(struct FsmInst *fi, int event, void *arg)
149 mISDN_FsmChangeState(fi, ST_L1_F5);
153 l1_go_F8(struct FsmInst *fi, int event, void *arg)
155 mISDN_FsmChangeState(fi, ST_L1_F8);
159 l1_info2_ind(struct FsmInst *fi, int event, void *arg)
161 struct layer1 *l1 = fi->userdata;
163 mISDN_FsmChangeState(fi, ST_L1_F6);
164 l1->dcb(l1->dch, INFO3_P8);
168 l1_info4_ind(struct FsmInst *fi, int event, void *arg)
170 struct layer1 *l1 = fi->userdata;
172 mISDN_FsmChangeState(fi, ST_L1_F7);
173 l1->dcb(l1->dch, INFO3_P8);
174 if (test_and_clear_bit(FLG_L1_DEACTTIMER, &l1->Flags))
175 mISDN_FsmDelTimer(&l1->timerX, 4);
176 if (!test_bit(FLG_L1_ACTIVATED, &l1->Flags)) {
177 if (test_and_clear_bit(FLG_L1_T3RUN, &l1->Flags))
178 mISDN_FsmDelTimer(&l1->timer3, 3);
179 mISDN_FsmRestartTimer(&l1->timerX, 110, EV_TIMER_ACT, NULL, 2);
180 test_and_set_bit(FLG_L1_ACTTIMER, &l1->Flags);
185 l1_timer3(struct FsmInst *fi, int event, void *arg)
187 struct layer1 *l1 = fi->userdata;
189 test_and_clear_bit(FLG_L1_T3RUN, &l1->Flags);
190 if (test_and_clear_bit(FLG_L1_ACTIVATING, &l1->Flags)) {
191 if (test_and_clear_bit(FLG_L1_DBLOCKED, &l1->Flags))
192 l1->dcb(l1->dch, HW_D_NOBLOCKED);
193 l1->dcb(l1->dch, PH_DEACTIVATE_IND);
195 if (l1->l1m.state != ST_L1_F6) {
196 mISDN_FsmChangeState(fi, ST_L1_F3);
197 /* do not force anything here, we need send INFO 0 */
202 l1_timer_act(struct FsmInst *fi, int event, void *arg)
204 struct layer1 *l1 = fi->userdata;
206 test_and_clear_bit(FLG_L1_ACTTIMER, &l1->Flags);
207 test_and_set_bit(FLG_L1_ACTIVATED, &l1->Flags);
208 l1->dcb(l1->dch, PH_ACTIVATE_IND);
212 l1_timer_deact(struct FsmInst *fi, int event, void *arg)
214 struct layer1 *l1 = fi->userdata;
216 test_and_clear_bit(FLG_L1_DEACTTIMER, &l1->Flags);
217 test_and_clear_bit(FLG_L1_ACTIVATED, &l1->Flags);
218 if (test_and_clear_bit(FLG_L1_DBLOCKED, &l1->Flags))
219 l1->dcb(l1->dch, HW_D_NOBLOCKED);
220 l1->dcb(l1->dch, PH_DEACTIVATE_IND);
221 l1->dcb(l1->dch, HW_DEACT_REQ);
225 l1_activate_s(struct FsmInst *fi, int event, void *arg)
227 struct layer1 *l1 = fi->userdata;
229 mISDN_FsmRestartTimer(&l1->timer3, l1->t3_value, EV_TIMER3, NULL, 2);
230 test_and_set_bit(FLG_L1_T3RUN, &l1->Flags);
231 /* Tell HW to send INFO 1 */
232 l1->dcb(l1->dch, HW_RESET_REQ);
236 l1_activate_no(struct FsmInst *fi, int event, void *arg)
238 struct layer1 *l1 = fi->userdata;
240 if ((!test_bit(FLG_L1_DEACTTIMER, &l1->Flags)) &&
241 (!test_bit(FLG_L1_T3RUN, &l1->Flags))) {
242 test_and_clear_bit(FLG_L1_ACTIVATING, &l1->Flags);
243 if (test_and_clear_bit(FLG_L1_DBLOCKED, &l1->Flags))
244 l1->dcb(l1->dch, HW_D_NOBLOCKED);
245 l1->dcb(l1->dch, PH_DEACTIVATE_IND);
249 static struct FsmNode L1SFnList[] =
251 {ST_L1_F3, EV_PH_ACTIVATE, l1_activate_s},
252 {ST_L1_F6, EV_PH_ACTIVATE, l1_activate_no},
253 {ST_L1_F8, EV_PH_ACTIVATE, l1_activate_no},
254 {ST_L1_F3, EV_RESET_IND, l1_reset},
255 {ST_L1_F4, EV_RESET_IND, l1_reset},
256 {ST_L1_F5, EV_RESET_IND, l1_reset},
257 {ST_L1_F6, EV_RESET_IND, l1_reset},
258 {ST_L1_F7, EV_RESET_IND, l1_reset},
259 {ST_L1_F8, EV_RESET_IND, l1_reset},
260 {ST_L1_F3, EV_DEACT_CNF, l1_deact_cnf},
261 {ST_L1_F4, EV_DEACT_CNF, l1_deact_cnf},
262 {ST_L1_F5, EV_DEACT_CNF, l1_deact_cnf},
263 {ST_L1_F6, EV_DEACT_CNF, l1_deact_cnf},
264 {ST_L1_F7, EV_DEACT_CNF, l1_deact_cnf},
265 {ST_L1_F8, EV_DEACT_CNF, l1_deact_cnf},
266 {ST_L1_F6, EV_DEACT_IND, l1_deact_req_s},
267 {ST_L1_F7, EV_DEACT_IND, l1_deact_req_s},
268 {ST_L1_F8, EV_DEACT_IND, l1_deact_req_s},
269 {ST_L1_F3, EV_POWER_UP, l1_power_up_s},
270 {ST_L1_F4, EV_ANYSIG_IND, l1_go_F5},
271 {ST_L1_F6, EV_ANYSIG_IND, l1_go_F8},
272 {ST_L1_F7, EV_ANYSIG_IND, l1_go_F8},
273 {ST_L1_F3, EV_INFO2_IND, l1_info2_ind},
274 {ST_L1_F4, EV_INFO2_IND, l1_info2_ind},
275 {ST_L1_F5, EV_INFO2_IND, l1_info2_ind},
276 {ST_L1_F7, EV_INFO2_IND, l1_info2_ind},
277 {ST_L1_F8, EV_INFO2_IND, l1_info2_ind},
278 {ST_L1_F3, EV_INFO4_IND, l1_info4_ind},
279 {ST_L1_F4, EV_INFO4_IND, l1_info4_ind},
280 {ST_L1_F5, EV_INFO4_IND, l1_info4_ind},
281 {ST_L1_F6, EV_INFO4_IND, l1_info4_ind},
282 {ST_L1_F8, EV_INFO4_IND, l1_info4_ind},
283 {ST_L1_F3, EV_TIMER3, l1_timer3},
284 {ST_L1_F4, EV_TIMER3, l1_timer3},
285 {ST_L1_F5, EV_TIMER3, l1_timer3},
286 {ST_L1_F6, EV_TIMER3, l1_timer3},
287 {ST_L1_F8, EV_TIMER3, l1_timer3},
288 {ST_L1_F7, EV_TIMER_ACT, l1_timer_act},
289 {ST_L1_F3, EV_TIMER_DEACT, l1_timer_deact},
290 {ST_L1_F4, EV_TIMER_DEACT, l1_timer_deact},
291 {ST_L1_F5, EV_TIMER_DEACT, l1_timer_deact},
292 {ST_L1_F6, EV_TIMER_DEACT, l1_timer_deact},
293 {ST_L1_F7, EV_TIMER_DEACT, l1_timer_deact},
294 {ST_L1_F8, EV_TIMER_DEACT, l1_timer_deact},
298 release_l1(struct layer1 *l1) {
299 mISDN_FsmDelTimer(&l1->timerX, 0);
300 mISDN_FsmDelTimer(&l1->timer3, 0);
303 module_put(THIS_MODULE);
308 l1_event(struct layer1 *l1, u_int event)
316 mISDN_FsmEvent(&l1->l1m, EV_RESET_IND, NULL);
319 mISDN_FsmEvent(&l1->l1m, EV_DEACT_IND, NULL);
322 mISDN_FsmEvent(&l1->l1m, EV_POWER_UP, NULL);
325 mISDN_FsmEvent(&l1->l1m, EV_DEACT_CNF, NULL);
328 mISDN_FsmEvent(&l1->l1m, EV_ANYSIG_IND, NULL);
331 mISDN_FsmEvent(&l1->l1m, EV_ANYSIG_IND, NULL);
334 mISDN_FsmEvent(&l1->l1m, EV_INFO2_IND, NULL);
337 mISDN_FsmEvent(&l1->l1m, EV_INFO4_IND, NULL);
340 mISDN_FsmEvent(&l1->l1m, EV_INFO4_IND, NULL);
342 case PH_ACTIVATE_REQ:
343 if (test_bit(FLG_L1_ACTIVATED, &l1->Flags))
344 l1->dcb(l1->dch, PH_ACTIVATE_IND);
346 test_and_set_bit(FLG_L1_ACTIVATING, &l1->Flags);
347 mISDN_FsmEvent(&l1->l1m, EV_PH_ACTIVATE, NULL);
354 if ((event & ~HW_TIMER3_VMASK) == HW_TIMER3_VALUE) {
355 int val = event & HW_TIMER3_VMASK;
364 if (*debug & DEBUG_L1)
365 printk(KERN_DEBUG "%s %x unhandled\n",
371 EXPORT_SYMBOL(l1_event);
374 create_l1(struct dchannel *dch, dchannel_l1callback *dcb) {
377 nl1 = kzalloc(sizeof(struct layer1), GFP_ATOMIC);
379 printk(KERN_ERR "kmalloc struct layer1 failed\n");
382 nl1->l1m.fsm = &l1fsm_s;
383 nl1->l1m.state = ST_L1_F3;
385 nl1->t3_value = TIMER3_DEFAULT_VALUE;
386 nl1->l1m.debug = *debug & DEBUG_L1_FSM;
387 nl1->l1m.userdata = nl1;
388 nl1->l1m.userint = 0;
389 nl1->l1m.printdebug = l1m_debug;
392 mISDN_FsmInitTimer(&nl1->l1m, &nl1->timer3);
393 mISDN_FsmInitTimer(&nl1->l1m, &nl1->timerX);
394 __module_get(THIS_MODULE);
398 EXPORT_SYMBOL(create_l1);
401 Isdnl1_Init(u_int *deb)
404 l1fsm_s.state_count = L1S_STATE_COUNT;
405 l1fsm_s.event_count = L1_EVENT_COUNT;
406 l1fsm_s.strEvent = strL1Event;
407 l1fsm_s.strState = strL1SState;
408 return mISDN_FsmNew(&l1fsm_s, L1SFnList, ARRAY_SIZE(L1SFnList));
414 mISDN_FsmFree(&l1fsm_s);