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