layer1.c 9.2 KB

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