layer1.c 9.2 KB

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