layer1.c 9.3 KB

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