layer1.c 9.6 KB

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