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.
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/mISDNhw.h>
31 struct FsmTimer timer3
;
32 struct FsmTimer timerX
;
36 dchannel_l1callback
*dcb
;
39 #define TIMER3_DEFAULT_VALUE 7000
42 struct Fsm l1fsm_s
= {NULL
, 0, 0, NULL
, NULL
};
54 #define L1S_STATE_COUNT (ST_L1_F8 + 1)
56 static char *strL1SState
[] =
82 #define L1_EVENT_COUNT (EV_TIMER3 + 1)
84 static char *strL1Event
[] =
101 l1m_debug(struct FsmInst
*fi
, char *fmt
, ...)
103 struct layer1
*l1
= fi
->userdata
;
104 struct va_format vaf
;
112 printk(KERN_DEBUG
"%s: %pV\n", dev_name(&l1
->dch
->dev
.dev
), &vaf
);
118 l1_reset(struct FsmInst
*fi
, int event
, void *arg
)
120 mISDN_FsmChangeState(fi
, ST_L1_F3
);
124 l1_deact_cnf(struct FsmInst
*fi
, int event
, void *arg
)
126 struct layer1
*l1
= fi
->userdata
;
128 mISDN_FsmChangeState(fi
, ST_L1_F3
);
129 if (test_bit(FLG_L1_ACTIVATING
, &l1
->Flags
))
130 l1
->dcb(l1
->dch
, HW_POWERUP_REQ
);
134 l1_deact_req_s(struct FsmInst
*fi
, int event
, void *arg
)
136 struct layer1
*l1
= fi
->userdata
;
138 mISDN_FsmChangeState(fi
, ST_L1_F3
);
139 mISDN_FsmRestartTimer(&l1
->timerX
, 550, EV_TIMER_DEACT
, NULL
, 2);
140 test_and_set_bit(FLG_L1_DEACTTIMER
, &l1
->Flags
);
144 l1_power_up_s(struct FsmInst
*fi
, int event
, void *arg
)
146 struct layer1
*l1
= fi
->userdata
;
148 if (test_bit(FLG_L1_ACTIVATING
, &l1
->Flags
)) {
149 mISDN_FsmChangeState(fi
, ST_L1_F4
);
150 l1
->dcb(l1
->dch
, INFO3_P8
);
152 mISDN_FsmChangeState(fi
, ST_L1_F3
);
156 l1_go_F5(struct FsmInst
*fi
, int event
, void *arg
)
158 mISDN_FsmChangeState(fi
, ST_L1_F5
);
162 l1_go_F8(struct FsmInst
*fi
, int event
, void *arg
)
164 mISDN_FsmChangeState(fi
, ST_L1_F8
);
168 l1_info2_ind(struct FsmInst
*fi
, int event
, void *arg
)
170 struct layer1
*l1
= fi
->userdata
;
172 mISDN_FsmChangeState(fi
, ST_L1_F6
);
173 l1
->dcb(l1
->dch
, INFO3_P8
);
177 l1_info4_ind(struct FsmInst
*fi
, int event
, void *arg
)
179 struct layer1
*l1
= fi
->userdata
;
181 mISDN_FsmChangeState(fi
, ST_L1_F7
);
182 l1
->dcb(l1
->dch
, INFO3_P8
);
183 if (test_and_clear_bit(FLG_L1_DEACTTIMER
, &l1
->Flags
))
184 mISDN_FsmDelTimer(&l1
->timerX
, 4);
185 if (!test_bit(FLG_L1_ACTIVATED
, &l1
->Flags
)) {
186 if (test_and_clear_bit(FLG_L1_T3RUN
, &l1
->Flags
))
187 mISDN_FsmDelTimer(&l1
->timer3
, 3);
188 mISDN_FsmRestartTimer(&l1
->timerX
, 110, EV_TIMER_ACT
, NULL
, 2);
189 test_and_set_bit(FLG_L1_ACTTIMER
, &l1
->Flags
);
194 l1_timer3(struct FsmInst
*fi
, int event
, void *arg
)
196 struct layer1
*l1
= fi
->userdata
;
198 test_and_clear_bit(FLG_L1_T3RUN
, &l1
->Flags
);
199 if (test_and_clear_bit(FLG_L1_ACTIVATING
, &l1
->Flags
)) {
200 if (test_and_clear_bit(FLG_L1_DBLOCKED
, &l1
->Flags
))
201 l1
->dcb(l1
->dch
, HW_D_NOBLOCKED
);
202 l1
->dcb(l1
->dch
, PH_DEACTIVATE_IND
);
204 if (l1
->l1m
.state
!= ST_L1_F6
) {
205 mISDN_FsmChangeState(fi
, ST_L1_F3
);
206 /* do not force anything here, we need send INFO 0 */
211 l1_timer_act(struct FsmInst
*fi
, int event
, void *arg
)
213 struct layer1
*l1
= fi
->userdata
;
215 test_and_clear_bit(FLG_L1_ACTTIMER
, &l1
->Flags
);
216 test_and_set_bit(FLG_L1_ACTIVATED
, &l1
->Flags
);
217 l1
->dcb(l1
->dch
, PH_ACTIVATE_IND
);
221 l1_timer_deact(struct FsmInst
*fi
, int event
, void *arg
)
223 struct layer1
*l1
= fi
->userdata
;
225 test_and_clear_bit(FLG_L1_DEACTTIMER
, &l1
->Flags
);
226 test_and_clear_bit(FLG_L1_ACTIVATED
, &l1
->Flags
);
227 if (test_and_clear_bit(FLG_L1_DBLOCKED
, &l1
->Flags
))
228 l1
->dcb(l1
->dch
, HW_D_NOBLOCKED
);
229 l1
->dcb(l1
->dch
, PH_DEACTIVATE_IND
);
230 l1
->dcb(l1
->dch
, HW_DEACT_REQ
);
234 l1_activate_s(struct FsmInst
*fi
, int event
, void *arg
)
236 struct layer1
*l1
= fi
->userdata
;
238 mISDN_FsmRestartTimer(&l1
->timer3
, l1
->t3_value
, EV_TIMER3
, NULL
, 2);
239 test_and_set_bit(FLG_L1_T3RUN
, &l1
->Flags
);
240 /* Tell HW to send INFO 1 */
241 l1
->dcb(l1
->dch
, HW_RESET_REQ
);
245 l1_activate_no(struct FsmInst
*fi
, int event
, void *arg
)
247 struct layer1
*l1
= fi
->userdata
;
249 if ((!test_bit(FLG_L1_DEACTTIMER
, &l1
->Flags
)) &&
250 (!test_bit(FLG_L1_T3RUN
, &l1
->Flags
))) {
251 test_and_clear_bit(FLG_L1_ACTIVATING
, &l1
->Flags
);
252 if (test_and_clear_bit(FLG_L1_DBLOCKED
, &l1
->Flags
))
253 l1
->dcb(l1
->dch
, HW_D_NOBLOCKED
);
254 l1
->dcb(l1
->dch
, PH_DEACTIVATE_IND
);
258 static struct FsmNode L1SFnList
[] =
260 {ST_L1_F3
, EV_PH_ACTIVATE
, l1_activate_s
},
261 {ST_L1_F6
, EV_PH_ACTIVATE
, l1_activate_no
},
262 {ST_L1_F8
, EV_PH_ACTIVATE
, l1_activate_no
},
263 {ST_L1_F3
, EV_RESET_IND
, l1_reset
},
264 {ST_L1_F4
, EV_RESET_IND
, l1_reset
},
265 {ST_L1_F5
, EV_RESET_IND
, l1_reset
},
266 {ST_L1_F6
, EV_RESET_IND
, l1_reset
},
267 {ST_L1_F7
, EV_RESET_IND
, l1_reset
},
268 {ST_L1_F8
, EV_RESET_IND
, l1_reset
},
269 {ST_L1_F3
, EV_DEACT_CNF
, l1_deact_cnf
},
270 {ST_L1_F4
, EV_DEACT_CNF
, l1_deact_cnf
},
271 {ST_L1_F5
, EV_DEACT_CNF
, l1_deact_cnf
},
272 {ST_L1_F6
, EV_DEACT_CNF
, l1_deact_cnf
},
273 {ST_L1_F7
, EV_DEACT_CNF
, l1_deact_cnf
},
274 {ST_L1_F8
, EV_DEACT_CNF
, l1_deact_cnf
},
275 {ST_L1_F6
, EV_DEACT_IND
, l1_deact_req_s
},
276 {ST_L1_F7
, EV_DEACT_IND
, l1_deact_req_s
},
277 {ST_L1_F8
, EV_DEACT_IND
, l1_deact_req_s
},
278 {ST_L1_F3
, EV_POWER_UP
, l1_power_up_s
},
279 {ST_L1_F4
, EV_ANYSIG_IND
, l1_go_F5
},
280 {ST_L1_F6
, EV_ANYSIG_IND
, l1_go_F8
},
281 {ST_L1_F7
, EV_ANYSIG_IND
, l1_go_F8
},
282 {ST_L1_F3
, EV_INFO2_IND
, l1_info2_ind
},
283 {ST_L1_F4
, EV_INFO2_IND
, l1_info2_ind
},
284 {ST_L1_F5
, EV_INFO2_IND
, l1_info2_ind
},
285 {ST_L1_F7
, EV_INFO2_IND
, l1_info2_ind
},
286 {ST_L1_F8
, EV_INFO2_IND
, l1_info2_ind
},
287 {ST_L1_F3
, EV_INFO4_IND
, l1_info4_ind
},
288 {ST_L1_F4
, EV_INFO4_IND
, l1_info4_ind
},
289 {ST_L1_F5
, EV_INFO4_IND
, l1_info4_ind
},
290 {ST_L1_F6
, EV_INFO4_IND
, l1_info4_ind
},
291 {ST_L1_F8
, EV_INFO4_IND
, l1_info4_ind
},
292 {ST_L1_F3
, EV_TIMER3
, l1_timer3
},
293 {ST_L1_F4
, EV_TIMER3
, l1_timer3
},
294 {ST_L1_F5
, EV_TIMER3
, l1_timer3
},
295 {ST_L1_F6
, EV_TIMER3
, l1_timer3
},
296 {ST_L1_F8
, EV_TIMER3
, l1_timer3
},
297 {ST_L1_F7
, EV_TIMER_ACT
, l1_timer_act
},
298 {ST_L1_F3
, EV_TIMER_DEACT
, l1_timer_deact
},
299 {ST_L1_F4
, EV_TIMER_DEACT
, l1_timer_deact
},
300 {ST_L1_F5
, EV_TIMER_DEACT
, l1_timer_deact
},
301 {ST_L1_F6
, EV_TIMER_DEACT
, l1_timer_deact
},
302 {ST_L1_F7
, EV_TIMER_DEACT
, l1_timer_deact
},
303 {ST_L1_F8
, EV_TIMER_DEACT
, l1_timer_deact
},
307 release_l1(struct layer1
*l1
) {
308 mISDN_FsmDelTimer(&l1
->timerX
, 0);
309 mISDN_FsmDelTimer(&l1
->timer3
, 0);
312 module_put(THIS_MODULE
);
317 l1_event(struct layer1
*l1
, u_int event
)
325 mISDN_FsmEvent(&l1
->l1m
, EV_RESET_IND
, NULL
);
328 mISDN_FsmEvent(&l1
->l1m
, EV_DEACT_IND
, NULL
);
331 mISDN_FsmEvent(&l1
->l1m
, EV_POWER_UP
, NULL
);
334 mISDN_FsmEvent(&l1
->l1m
, EV_DEACT_CNF
, NULL
);
337 mISDN_FsmEvent(&l1
->l1m
, EV_ANYSIG_IND
, NULL
);
340 mISDN_FsmEvent(&l1
->l1m
, EV_ANYSIG_IND
, NULL
);
343 mISDN_FsmEvent(&l1
->l1m
, EV_INFO2_IND
, NULL
);
346 mISDN_FsmEvent(&l1
->l1m
, EV_INFO4_IND
, NULL
);
349 mISDN_FsmEvent(&l1
->l1m
, EV_INFO4_IND
, NULL
);
351 case PH_ACTIVATE_REQ
:
352 if (test_bit(FLG_L1_ACTIVATED
, &l1
->Flags
))
353 l1
->dcb(l1
->dch
, PH_ACTIVATE_IND
);
355 test_and_set_bit(FLG_L1_ACTIVATING
, &l1
->Flags
);
356 mISDN_FsmEvent(&l1
->l1m
, EV_PH_ACTIVATE
, NULL
);
363 if ((event
& ~HW_TIMER3_VMASK
) == HW_TIMER3_VALUE
) {
364 int val
= event
& HW_TIMER3_VMASK
;
373 if (*debug
& DEBUG_L1
)
374 printk(KERN_DEBUG
"%s %x unhandled\n",
380 EXPORT_SYMBOL(l1_event
);
383 create_l1(struct dchannel
*dch
, dchannel_l1callback
*dcb
) {
386 nl1
= kzalloc(sizeof(struct layer1
), GFP_ATOMIC
);
388 printk(KERN_ERR
"kmalloc struct layer1 failed\n");
391 nl1
->l1m
.fsm
= &l1fsm_s
;
392 nl1
->l1m
.state
= ST_L1_F3
;
394 nl1
->t3_value
= TIMER3_DEFAULT_VALUE
;
395 nl1
->l1m
.debug
= *debug
& DEBUG_L1_FSM
;
396 nl1
->l1m
.userdata
= nl1
;
397 nl1
->l1m
.userint
= 0;
398 nl1
->l1m
.printdebug
= l1m_debug
;
401 mISDN_FsmInitTimer(&nl1
->l1m
, &nl1
->timer3
);
402 mISDN_FsmInitTimer(&nl1
->l1m
, &nl1
->timerX
);
403 __module_get(THIS_MODULE
);
407 EXPORT_SYMBOL(create_l1
);
413 l1fsm_s
.state_count
= L1S_STATE_COUNT
;
414 l1fsm_s
.event_count
= L1_EVENT_COUNT
;
415 l1fsm_s
.strEvent
= strL1Event
;
416 l1fsm_s
.strState
= strL1SState
;
417 mISDN_FsmNew(&l1fsm_s
, L1SFnList
, ARRAY_SIZE(L1SFnList
));
424 mISDN_FsmFree(&l1fsm_s
);