layer1.c 9.7 KB

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