fsm.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * finite state machine implementation
  4. *
  5. * Author Karsten Keil <[email protected]>
  6. *
  7. * Thanks to Jan den Ouden
  8. * Fritz Elfert
  9. * Copyright 2008 by Karsten Keil <[email protected]>
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/string.h>
  15. #include "fsm.h"
  16. #define FSM_TIMER_DEBUG 0
  17. int
  18. mISDN_FsmNew(struct Fsm *fsm,
  19. struct FsmNode *fnlist, int fncount)
  20. {
  21. int i;
  22. fsm->jumpmatrix =
  23. kzalloc(array3_size(sizeof(FSMFNPTR), fsm->state_count,
  24. fsm->event_count),
  25. GFP_KERNEL);
  26. if (fsm->jumpmatrix == NULL)
  27. return -ENOMEM;
  28. for (i = 0; i < fncount; i++)
  29. if ((fnlist[i].state >= fsm->state_count) ||
  30. (fnlist[i].event >= fsm->event_count)) {
  31. printk(KERN_ERR
  32. "mISDN_FsmNew Error: %d st(%ld/%ld) ev(%ld/%ld)\n",
  33. i, (long)fnlist[i].state, (long)fsm->state_count,
  34. (long)fnlist[i].event, (long)fsm->event_count);
  35. } else
  36. fsm->jumpmatrix[fsm->state_count * fnlist[i].event +
  37. fnlist[i].state] = (FSMFNPTR) fnlist[i].routine;
  38. return 0;
  39. }
  40. EXPORT_SYMBOL(mISDN_FsmNew);
  41. void
  42. mISDN_FsmFree(struct Fsm *fsm)
  43. {
  44. kfree((void *) fsm->jumpmatrix);
  45. }
  46. EXPORT_SYMBOL(mISDN_FsmFree);
  47. int
  48. mISDN_FsmEvent(struct FsmInst *fi, int event, void *arg)
  49. {
  50. FSMFNPTR r;
  51. if ((fi->state >= fi->fsm->state_count) ||
  52. (event >= fi->fsm->event_count)) {
  53. printk(KERN_ERR
  54. "mISDN_FsmEvent Error st(%ld/%ld) ev(%d/%ld)\n",
  55. (long)fi->state, (long)fi->fsm->state_count, event,
  56. (long)fi->fsm->event_count);
  57. return 1;
  58. }
  59. r = fi->fsm->jumpmatrix[fi->fsm->state_count * event + fi->state];
  60. if (r) {
  61. if (fi->debug)
  62. fi->printdebug(fi, "State %s Event %s",
  63. fi->fsm->strState[fi->state],
  64. fi->fsm->strEvent[event]);
  65. r(fi, event, arg);
  66. return 0;
  67. } else {
  68. if (fi->debug)
  69. fi->printdebug(fi, "State %s Event %s no action",
  70. fi->fsm->strState[fi->state],
  71. fi->fsm->strEvent[event]);
  72. return 1;
  73. }
  74. }
  75. EXPORT_SYMBOL(mISDN_FsmEvent);
  76. void
  77. mISDN_FsmChangeState(struct FsmInst *fi, int newstate)
  78. {
  79. fi->state = newstate;
  80. if (fi->debug)
  81. fi->printdebug(fi, "ChangeState %s",
  82. fi->fsm->strState[newstate]);
  83. }
  84. EXPORT_SYMBOL(mISDN_FsmChangeState);
  85. static void
  86. FsmExpireTimer(struct timer_list *t)
  87. {
  88. struct FsmTimer *ft = from_timer(ft, t, tl);
  89. #if FSM_TIMER_DEBUG
  90. if (ft->fi->debug)
  91. ft->fi->printdebug(ft->fi, "FsmExpireTimer %lx", (long) ft);
  92. #endif
  93. mISDN_FsmEvent(ft->fi, ft->event, ft->arg);
  94. }
  95. void
  96. mISDN_FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft)
  97. {
  98. ft->fi = fi;
  99. #if FSM_TIMER_DEBUG
  100. if (ft->fi->debug)
  101. ft->fi->printdebug(ft->fi, "mISDN_FsmInitTimer %lx", (long) ft);
  102. #endif
  103. timer_setup(&ft->tl, FsmExpireTimer, 0);
  104. }
  105. EXPORT_SYMBOL(mISDN_FsmInitTimer);
  106. void
  107. mISDN_FsmDelTimer(struct FsmTimer *ft, int where)
  108. {
  109. #if FSM_TIMER_DEBUG
  110. if (ft->fi->debug)
  111. ft->fi->printdebug(ft->fi, "mISDN_FsmDelTimer %lx %d",
  112. (long) ft, where);
  113. #endif
  114. del_timer(&ft->tl);
  115. }
  116. EXPORT_SYMBOL(mISDN_FsmDelTimer);
  117. int
  118. mISDN_FsmAddTimer(struct FsmTimer *ft,
  119. int millisec, int event, void *arg, int where)
  120. {
  121. #if FSM_TIMER_DEBUG
  122. if (ft->fi->debug)
  123. ft->fi->printdebug(ft->fi, "mISDN_FsmAddTimer %lx %d %d",
  124. (long) ft, millisec, where);
  125. #endif
  126. if (timer_pending(&ft->tl)) {
  127. if (ft->fi->debug) {
  128. printk(KERN_WARNING
  129. "mISDN_FsmAddTimer: timer already active!\n");
  130. ft->fi->printdebug(ft->fi,
  131. "mISDN_FsmAddTimer already active!");
  132. }
  133. return -1;
  134. }
  135. ft->event = event;
  136. ft->arg = arg;
  137. ft->tl.expires = jiffies + (millisec * HZ) / 1000;
  138. add_timer(&ft->tl);
  139. return 0;
  140. }
  141. EXPORT_SYMBOL(mISDN_FsmAddTimer);
  142. void
  143. mISDN_FsmRestartTimer(struct FsmTimer *ft,
  144. int millisec, int event, void *arg, int where)
  145. {
  146. #if FSM_TIMER_DEBUG
  147. if (ft->fi->debug)
  148. ft->fi->printdebug(ft->fi, "mISDN_FsmRestartTimer %lx %d %d",
  149. (long) ft, millisec, where);
  150. #endif
  151. if (timer_pending(&ft->tl))
  152. del_timer(&ft->tl);
  153. ft->event = event;
  154. ft->arg = arg;
  155. ft->tl.expires = jiffies + (millisec * HZ) / 1000;
  156. add_timer(&ft->tl);
  157. }
  158. EXPORT_SYMBOL(mISDN_FsmRestartTimer);