layer1.c 9.3 KB

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