Linux 4.16.11
[linux/fpc-iii.git] / drivers / isdn / mISDN / layer1.c
blob3192b0eb39445435d7b38c0f914bded7c93b2fac
1 /*
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>
22 #include "core.h"
23 #include "layer1.h"
24 #include "fsm.h"
26 static u_int *debug;
28 struct layer1 {
29 u_long Flags;
30 struct FsmInst l1m;
31 struct FsmTimer timer3;
32 struct FsmTimer timerX;
33 int delay;
34 int t3_value;
35 struct dchannel *dch;
36 dchannel_l1callback *dcb;
39 #define TIMER3_DEFAULT_VALUE 7000
41 static
42 struct Fsm l1fsm_s = {NULL, 0, 0, NULL, NULL};
44 enum {
45 ST_L1_F2,
46 ST_L1_F3,
47 ST_L1_F4,
48 ST_L1_F5,
49 ST_L1_F6,
50 ST_L1_F7,
51 ST_L1_F8,
54 #define L1S_STATE_COUNT (ST_L1_F8 + 1)
56 static char *strL1SState[] =
58 "ST_L1_F2",
59 "ST_L1_F3",
60 "ST_L1_F4",
61 "ST_L1_F5",
62 "ST_L1_F6",
63 "ST_L1_F7",
64 "ST_L1_F8",
67 enum {
68 EV_PH_ACTIVATE,
69 EV_PH_DEACTIVATE,
70 EV_RESET_IND,
71 EV_DEACT_CNF,
72 EV_DEACT_IND,
73 EV_POWER_UP,
74 EV_ANYSIG_IND,
75 EV_INFO2_IND,
76 EV_INFO4_IND,
77 EV_TIMER_DEACT,
78 EV_TIMER_ACT,
79 EV_TIMER3,
82 #define L1_EVENT_COUNT (EV_TIMER3 + 1)
84 static char *strL1Event[] =
86 "EV_PH_ACTIVATE",
87 "EV_PH_DEACTIVATE",
88 "EV_RESET_IND",
89 "EV_DEACT_CNF",
90 "EV_DEACT_IND",
91 "EV_POWER_UP",
92 "EV_ANYSIG_IND",
93 "EV_INFO2_IND",
94 "EV_INFO4_IND",
95 "EV_TIMER_DEACT",
96 "EV_TIMER_ACT",
97 "EV_TIMER3",
100 static void
101 l1m_debug(struct FsmInst *fi, char *fmt, ...)
103 struct layer1 *l1 = fi->userdata;
104 struct va_format vaf;
105 va_list va;
107 va_start(va, fmt);
109 vaf.fmt = fmt;
110 vaf.va = &va;
112 printk(KERN_DEBUG "%s: %pV\n", dev_name(&l1->dch->dev.dev), &vaf);
114 va_end(va);
117 static void
118 l1_reset(struct FsmInst *fi, int event, void *arg)
120 mISDN_FsmChangeState(fi, ST_L1_F3);
123 static void
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);
133 static void
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);
143 static void
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);
151 } else
152 mISDN_FsmChangeState(fi, ST_L1_F3);
155 static void
156 l1_go_F5(struct FsmInst *fi, int event, void *arg)
158 mISDN_FsmChangeState(fi, ST_L1_F5);
161 static void
162 l1_go_F8(struct FsmInst *fi, int event, void *arg)
164 mISDN_FsmChangeState(fi, ST_L1_F8);
167 static void
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);
176 static void
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);
193 static void
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 */
210 static void
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);
220 static void
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);
233 static void
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);
244 static void
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},
306 static void
307 release_l1(struct layer1 *l1) {
308 mISDN_FsmDelTimer(&l1->timerX, 0);
309 mISDN_FsmDelTimer(&l1->timer3, 0);
310 if (l1->dch)
311 l1->dch->l1 = NULL;
312 module_put(THIS_MODULE);
313 kfree(l1);
317 l1_event(struct layer1 *l1, u_int event)
319 int err = 0;
321 if (!l1)
322 return -EINVAL;
323 switch (event) {
324 case HW_RESET_IND:
325 mISDN_FsmEvent(&l1->l1m, EV_RESET_IND, NULL);
326 break;
327 case HW_DEACT_IND:
328 mISDN_FsmEvent(&l1->l1m, EV_DEACT_IND, NULL);
329 break;
330 case HW_POWERUP_IND:
331 mISDN_FsmEvent(&l1->l1m, EV_POWER_UP, NULL);
332 break;
333 case HW_DEACT_CNF:
334 mISDN_FsmEvent(&l1->l1m, EV_DEACT_CNF, NULL);
335 break;
336 case ANYSIGNAL:
337 mISDN_FsmEvent(&l1->l1m, EV_ANYSIG_IND, NULL);
338 break;
339 case LOSTFRAMING:
340 mISDN_FsmEvent(&l1->l1m, EV_ANYSIG_IND, NULL);
341 break;
342 case INFO2:
343 mISDN_FsmEvent(&l1->l1m, EV_INFO2_IND, NULL);
344 break;
345 case INFO4_P8:
346 mISDN_FsmEvent(&l1->l1m, EV_INFO4_IND, NULL);
347 break;
348 case INFO4_P10:
349 mISDN_FsmEvent(&l1->l1m, EV_INFO4_IND, NULL);
350 break;
351 case PH_ACTIVATE_REQ:
352 if (test_bit(FLG_L1_ACTIVATED, &l1->Flags))
353 l1->dcb(l1->dch, PH_ACTIVATE_IND);
354 else {
355 test_and_set_bit(FLG_L1_ACTIVATING, &l1->Flags);
356 mISDN_FsmEvent(&l1->l1m, EV_PH_ACTIVATE, NULL);
358 break;
359 case CLOSE_CHANNEL:
360 release_l1(l1);
361 break;
362 default:
363 if ((event & ~HW_TIMER3_VMASK) == HW_TIMER3_VALUE) {
364 int val = event & HW_TIMER3_VMASK;
366 if (val < 5)
367 val = 5;
368 if (val > 30)
369 val = 30;
370 l1->t3_value = val;
371 break;
373 if (*debug & DEBUG_L1)
374 printk(KERN_DEBUG "%s %x unhandled\n",
375 __func__, event);
376 err = -EINVAL;
378 return err;
380 EXPORT_SYMBOL(l1_event);
383 create_l1(struct dchannel *dch, dchannel_l1callback *dcb) {
384 struct layer1 *nl1;
386 nl1 = kzalloc(sizeof(struct layer1), GFP_ATOMIC);
387 if (!nl1) {
388 printk(KERN_ERR "kmalloc struct layer1 failed\n");
389 return -ENOMEM;
391 nl1->l1m.fsm = &l1fsm_s;
392 nl1->l1m.state = ST_L1_F3;
393 nl1->Flags = 0;
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;
399 nl1->dch = dch;
400 nl1->dcb = dcb;
401 mISDN_FsmInitTimer(&nl1->l1m, &nl1->timer3);
402 mISDN_FsmInitTimer(&nl1->l1m, &nl1->timerX);
403 __module_get(THIS_MODULE);
404 dch->l1 = nl1;
405 return 0;
407 EXPORT_SYMBOL(create_l1);
410 l1_init(u_int *deb)
412 debug = deb;
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 return mISDN_FsmNew(&l1fsm_s, L1SFnList, ARRAY_SIZE(L1SFnList));
420 void
421 l1_cleanup(void)
423 mISDN_FsmFree(&l1fsm_s);