layer1.c 9.3 KB

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