include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[linux-3.10.git] / drivers / isdn / mISDN / layer1.c
1 /*
2  *
3  * Author       Karsten Keil <kkeil@novell.com>
4  *
5  * Copyright 2008  by Karsten Keil <kkeil@novell.com>
6  *
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.
10  *
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.
15  *
16  */
17
18
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"
25
26 static u_int *debug;
27
28 struct layer1 {
29         u_long                  Flags;
30         struct FsmInst          l1m;
31         struct FsmTimer         timer;
32         int                     delay;
33         struct dchannel         *dch;
34         dchannel_l1callback     *dcb;
35 };
36
37 #define TIMER3_VALUE 7000
38
39 static
40 struct Fsm l1fsm_s = {NULL, 0, 0, NULL, NULL};
41
42 enum {
43         ST_L1_F2,
44         ST_L1_F3,
45         ST_L1_F4,
46         ST_L1_F5,
47         ST_L1_F6,
48         ST_L1_F7,
49         ST_L1_F8,
50 };
51
52 #define L1S_STATE_COUNT (ST_L1_F8+1)
53
54 static char *strL1SState[] =
55 {
56         "ST_L1_F2",
57         "ST_L1_F3",
58         "ST_L1_F4",
59         "ST_L1_F5",
60         "ST_L1_F6",
61         "ST_L1_F7",
62         "ST_L1_F8",
63 };
64
65 enum {
66         EV_PH_ACTIVATE,
67         EV_PH_DEACTIVATE,
68         EV_RESET_IND,
69         EV_DEACT_CNF,
70         EV_DEACT_IND,
71         EV_POWER_UP,
72         EV_ANYSIG_IND,
73         EV_INFO2_IND,
74         EV_INFO4_IND,
75         EV_TIMER_DEACT,
76         EV_TIMER_ACT,
77         EV_TIMER3,
78 };
79
80 #define L1_EVENT_COUNT (EV_TIMER3 + 1)
81
82 static char *strL1Event[] =
83 {
84         "EV_PH_ACTIVATE",
85         "EV_PH_DEACTIVATE",
86         "EV_RESET_IND",
87         "EV_DEACT_CNF",
88         "EV_DEACT_IND",
89         "EV_POWER_UP",
90         "EV_ANYSIG_IND",
91         "EV_INFO2_IND",
92         "EV_INFO4_IND",
93         "EV_TIMER_DEACT",
94         "EV_TIMER_ACT",
95         "EV_TIMER3",
96 };
97
98 static void
99 l1m_debug(struct FsmInst *fi, char *fmt, ...)
100 {
101         struct layer1 *l1 = fi->userdata;
102         va_list va;
103
104         va_start(va, fmt);
105         printk(KERN_DEBUG "%s: ", dev_name(&l1->dch->dev.dev));
106         vprintk(fmt, va);
107         printk("\n");
108         va_end(va);
109 }
110
111 static void
112 l1_reset(struct FsmInst *fi, int event, void *arg)
113 {
114         mISDN_FsmChangeState(fi, ST_L1_F3);
115 }
116
117 static void
118 l1_deact_cnf(struct FsmInst *fi, int event, void *arg)
119 {
120         struct layer1 *l1 = fi->userdata;
121
122         mISDN_FsmChangeState(fi, ST_L1_F3);
123         if (test_bit(FLG_L1_ACTIVATING, &l1->Flags))
124                 l1->dcb(l1->dch, HW_POWERUP_REQ);
125 }
126
127 static void
128 l1_deact_req_s(struct FsmInst *fi, int event, void *arg)
129 {
130         struct layer1 *l1 = fi->userdata;
131
132         mISDN_FsmChangeState(fi, ST_L1_F3);
133         mISDN_FsmRestartTimer(&l1->timer, 550, EV_TIMER_DEACT, NULL, 2);
134         test_and_set_bit(FLG_L1_DEACTTIMER, &l1->Flags);
135 }
136
137 static void
138 l1_power_up_s(struct FsmInst *fi, int event, void *arg)
139 {
140         struct layer1 *l1 = fi->userdata;
141
142         if (test_bit(FLG_L1_ACTIVATING, &l1->Flags)) {
143                 mISDN_FsmChangeState(fi, ST_L1_F4);
144                 l1->dcb(l1->dch, INFO3_P8);
145         } else
146                 mISDN_FsmChangeState(fi, ST_L1_F3);
147 }
148
149 static void
150 l1_go_F5(struct FsmInst *fi, int event, void *arg)
151 {
152         mISDN_FsmChangeState(fi, ST_L1_F5);
153 }
154
155 static void
156 l1_go_F8(struct FsmInst *fi, int event, void *arg)
157 {
158         mISDN_FsmChangeState(fi, ST_L1_F8);
159 }
160
161 static void
162 l1_info2_ind(struct FsmInst *fi, int event, void *arg)
163 {
164         struct layer1 *l1 = fi->userdata;
165
166         mISDN_FsmChangeState(fi, ST_L1_F6);
167         l1->dcb(l1->dch, INFO3_P8);
168 }
169
170 static void
171 l1_info4_ind(struct FsmInst *fi, int event, void *arg)
172 {
173         struct layer1 *l1 = fi->userdata;
174
175         mISDN_FsmChangeState(fi, ST_L1_F7);
176         l1->dcb(l1->dch, INFO3_P8);
177         if (test_and_clear_bit(FLG_L1_DEACTTIMER, &l1->Flags))
178                 mISDN_FsmDelTimer(&l1->timer, 4);
179         if (!test_bit(FLG_L1_ACTIVATED, &l1->Flags)) {
180                 if (test_and_clear_bit(FLG_L1_T3RUN, &l1->Flags))
181                         mISDN_FsmDelTimer(&l1->timer, 3);
182                 mISDN_FsmRestartTimer(&l1->timer, 110, EV_TIMER_ACT, NULL, 2);
183                 test_and_set_bit(FLG_L1_ACTTIMER, &l1->Flags);
184         }
185 }
186
187 static void
188 l1_timer3(struct FsmInst *fi, int event, void *arg)
189 {
190         struct layer1 *l1 = fi->userdata;
191
192         test_and_clear_bit(FLG_L1_T3RUN, &l1->Flags);
193         if (test_and_clear_bit(FLG_L1_ACTIVATING, &l1->Flags)) {
194                 if (test_and_clear_bit(FLG_L1_DBLOCKED, &l1->Flags))
195                         l1->dcb(l1->dch, HW_D_NOBLOCKED);
196                 l1->dcb(l1->dch, PH_DEACTIVATE_IND);
197         }
198         if (l1->l1m.state != ST_L1_F6) {
199                 mISDN_FsmChangeState(fi, ST_L1_F3);
200                 l1->dcb(l1->dch, HW_POWERUP_REQ);
201         }
202 }
203
204 static void
205 l1_timer_act(struct FsmInst *fi, int event, void *arg)
206 {
207         struct layer1 *l1 = fi->userdata;
208
209         test_and_clear_bit(FLG_L1_ACTTIMER, &l1->Flags);
210         test_and_set_bit(FLG_L1_ACTIVATED, &l1->Flags);
211         l1->dcb(l1->dch, PH_ACTIVATE_IND);
212 }
213
214 static void
215 l1_timer_deact(struct FsmInst *fi, int event, void *arg)
216 {
217         struct layer1 *l1 = fi->userdata;
218
219         test_and_clear_bit(FLG_L1_DEACTTIMER, &l1->Flags);
220         test_and_clear_bit(FLG_L1_ACTIVATED, &l1->Flags);
221         if (test_and_clear_bit(FLG_L1_DBLOCKED, &l1->Flags))
222                 l1->dcb(l1->dch, HW_D_NOBLOCKED);
223         l1->dcb(l1->dch, PH_DEACTIVATE_IND);
224         l1->dcb(l1->dch, HW_DEACT_REQ);
225 }
226
227 static void
228 l1_activate_s(struct FsmInst *fi, int event, void *arg)
229 {
230         struct layer1 *l1 = fi->userdata;
231
232         mISDN_FsmRestartTimer(&l1->timer, TIMER3_VALUE, EV_TIMER3, NULL, 2);
233         test_and_set_bit(FLG_L1_T3RUN, &l1->Flags);
234         l1->dcb(l1->dch, HW_RESET_REQ);
235 }
236
237 static void
238 l1_activate_no(struct FsmInst *fi, int event, void *arg)
239 {
240         struct layer1 *l1 = fi->userdata;
241
242         if ((!test_bit(FLG_L1_DEACTTIMER, &l1->Flags)) &&
243             (!test_bit(FLG_L1_T3RUN, &l1->Flags))) {
244                 test_and_clear_bit(FLG_L1_ACTIVATING, &l1->Flags);
245                 if (test_and_clear_bit(FLG_L1_DBLOCKED, &l1->Flags))
246                         l1->dcb(l1->dch, HW_D_NOBLOCKED);
247                 l1->dcb(l1->dch, PH_DEACTIVATE_IND);
248         }
249 }
250
251 static struct FsmNode L1SFnList[] =
252 {
253         {ST_L1_F3, EV_PH_ACTIVATE, l1_activate_s},
254         {ST_L1_F6, EV_PH_ACTIVATE, l1_activate_no},
255         {ST_L1_F8, EV_PH_ACTIVATE, l1_activate_no},
256         {ST_L1_F3, EV_RESET_IND, l1_reset},
257         {ST_L1_F4, EV_RESET_IND, l1_reset},
258         {ST_L1_F5, EV_RESET_IND, l1_reset},
259         {ST_L1_F6, EV_RESET_IND, l1_reset},
260         {ST_L1_F7, EV_RESET_IND, l1_reset},
261         {ST_L1_F8, EV_RESET_IND, l1_reset},
262         {ST_L1_F3, EV_DEACT_CNF, l1_deact_cnf},
263         {ST_L1_F4, EV_DEACT_CNF, l1_deact_cnf},
264         {ST_L1_F5, EV_DEACT_CNF, l1_deact_cnf},
265         {ST_L1_F6, EV_DEACT_CNF, l1_deact_cnf},
266         {ST_L1_F7, EV_DEACT_CNF, l1_deact_cnf},
267         {ST_L1_F8, EV_DEACT_CNF, l1_deact_cnf},
268         {ST_L1_F6, EV_DEACT_IND, l1_deact_req_s},
269         {ST_L1_F7, EV_DEACT_IND, l1_deact_req_s},
270         {ST_L1_F8, EV_DEACT_IND, l1_deact_req_s},
271         {ST_L1_F3, EV_POWER_UP,  l1_power_up_s},
272         {ST_L1_F4, EV_ANYSIG_IND, l1_go_F5},
273         {ST_L1_F6, EV_ANYSIG_IND, l1_go_F8},
274         {ST_L1_F7, EV_ANYSIG_IND, l1_go_F8},
275         {ST_L1_F3, EV_INFO2_IND, l1_info2_ind},
276         {ST_L1_F4, EV_INFO2_IND, l1_info2_ind},
277         {ST_L1_F5, EV_INFO2_IND, l1_info2_ind},
278         {ST_L1_F7, EV_INFO2_IND, l1_info2_ind},
279         {ST_L1_F8, EV_INFO2_IND, l1_info2_ind},
280         {ST_L1_F3, EV_INFO4_IND, l1_info4_ind},
281         {ST_L1_F4, EV_INFO4_IND, l1_info4_ind},
282         {ST_L1_F5, EV_INFO4_IND, l1_info4_ind},
283         {ST_L1_F6, EV_INFO4_IND, l1_info4_ind},
284         {ST_L1_F8, EV_INFO4_IND, l1_info4_ind},
285         {ST_L1_F3, EV_TIMER3, l1_timer3},
286         {ST_L1_F4, EV_TIMER3, l1_timer3},
287         {ST_L1_F5, EV_TIMER3, l1_timer3},
288         {ST_L1_F6, EV_TIMER3, l1_timer3},
289         {ST_L1_F8, EV_TIMER3, l1_timer3},
290         {ST_L1_F7, EV_TIMER_ACT, l1_timer_act},
291         {ST_L1_F3, EV_TIMER_DEACT, l1_timer_deact},
292         {ST_L1_F4, EV_TIMER_DEACT, l1_timer_deact},
293         {ST_L1_F5, EV_TIMER_DEACT, l1_timer_deact},
294         {ST_L1_F6, EV_TIMER_DEACT, l1_timer_deact},
295         {ST_L1_F7, EV_TIMER_DEACT, l1_timer_deact},
296         {ST_L1_F8, EV_TIMER_DEACT, l1_timer_deact},
297 };
298
299 static void
300 release_l1(struct layer1 *l1) {
301         mISDN_FsmDelTimer(&l1->timer, 0);
302         if (l1->dch)
303                 l1->dch->l1 = NULL;
304         module_put(THIS_MODULE);
305         kfree(l1);
306 }
307
308 int
309 l1_event(struct layer1 *l1, u_int event)
310 {
311         int             err = 0;
312
313         if (!l1)
314                 return -EINVAL;
315         switch (event) {
316         case HW_RESET_IND:
317                 mISDN_FsmEvent(&l1->l1m, EV_RESET_IND, NULL);
318                 break;
319         case HW_DEACT_IND:
320                 mISDN_FsmEvent(&l1->l1m, EV_DEACT_IND, NULL);
321                 break;
322         case HW_POWERUP_IND:
323                 mISDN_FsmEvent(&l1->l1m, EV_POWER_UP, NULL);
324                 break;
325         case HW_DEACT_CNF:
326                 mISDN_FsmEvent(&l1->l1m, EV_DEACT_CNF, NULL);
327                 break;
328         case ANYSIGNAL:
329                 mISDN_FsmEvent(&l1->l1m, EV_ANYSIG_IND, NULL);
330                 break;
331         case LOSTFRAMING:
332                 mISDN_FsmEvent(&l1->l1m, EV_ANYSIG_IND, NULL);
333                 break;
334         case INFO2:
335                 mISDN_FsmEvent(&l1->l1m, EV_INFO2_IND, NULL);
336                 break;
337         case INFO4_P8:
338                 mISDN_FsmEvent(&l1->l1m, EV_INFO4_IND, NULL);
339                 break;
340         case INFO4_P10:
341                 mISDN_FsmEvent(&l1->l1m, EV_INFO4_IND, NULL);
342                 break;
343         case PH_ACTIVATE_REQ:
344                 if (test_bit(FLG_L1_ACTIVATED, &l1->Flags))
345                         l1->dcb(l1->dch, PH_ACTIVATE_IND);
346                 else {
347                         test_and_set_bit(FLG_L1_ACTIVATING, &l1->Flags);
348                         mISDN_FsmEvent(&l1->l1m, EV_PH_ACTIVATE, NULL);
349                 }
350                 break;
351         case CLOSE_CHANNEL:
352                 release_l1(l1);
353                 break;
354         default:
355                 if (*debug & DEBUG_L1)
356                         printk(KERN_DEBUG "%s %x unhandled\n",
357                             __func__, event);
358                 err = -EINVAL;
359         }
360         return err;
361 }
362 EXPORT_SYMBOL(l1_event);
363
364 int
365 create_l1(struct dchannel *dch, dchannel_l1callback *dcb) {
366         struct layer1   *nl1;
367
368         nl1 = kzalloc(sizeof(struct layer1), GFP_ATOMIC);
369         if (!nl1) {
370                 printk(KERN_ERR "kmalloc struct layer1 failed\n");
371                 return -ENOMEM;
372         }
373         nl1->l1m.fsm = &l1fsm_s;
374         nl1->l1m.state = ST_L1_F3;
375         nl1->Flags = 0;
376         nl1->l1m.debug = *debug & DEBUG_L1_FSM;
377         nl1->l1m.userdata = nl1;
378         nl1->l1m.userint = 0;
379         nl1->l1m.printdebug = l1m_debug;
380         nl1->dch = dch;
381         nl1->dcb = dcb;
382         mISDN_FsmInitTimer(&nl1->l1m, &nl1->timer);
383         __module_get(THIS_MODULE);
384         dch->l1 = nl1;
385         return 0;
386 }
387 EXPORT_SYMBOL(create_l1);
388
389 int
390 l1_init(u_int *deb)
391 {
392         debug = deb;
393         l1fsm_s.state_count = L1S_STATE_COUNT;
394         l1fsm_s.event_count = L1_EVENT_COUNT;
395         l1fsm_s.strEvent = strL1Event;
396         l1fsm_s.strState = strL1SState;
397         mISDN_FsmNew(&l1fsm_s, L1SFnList, ARRAY_SIZE(L1SFnList));
398         return 0;
399 }
400
401 void
402 l1_cleanup(void)
403 {
404         mISDN_FsmFree(&l1fsm_s);
405 }