fsm.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* $Id: fsm.c,v 1.14.6.4 2001/09/23 22:24:47 kai Exp $
  2. *
  3. * Finite state machine
  4. *
  5. * Author Karsten Keil
  6. * Copyright by Karsten Keil <keil@isdn4linux.de>
  7. * by Kai Germaschewski <kai.germaschewski@gmx.de>
  8. *
  9. * This software may be used and distributed according to the terms
  10. * of the GNU General Public License, incorporated herein by reference.
  11. *
  12. * Thanks to Jan den Ouden
  13. * Fritz Elfert
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/init.h>
  19. #include "hisax.h"
  20. #define FSM_TIMER_DEBUG 0
  21. int
  22. FsmNew(struct Fsm *fsm, struct FsmNode *fnlist, int fncount)
  23. {
  24. int i;
  25. fsm->jumpmatrix =
  26. kzalloc(sizeof(FSMFNPTR) * fsm->state_count * fsm->event_count, GFP_KERNEL);
  27. if (!fsm->jumpmatrix)
  28. return -ENOMEM;
  29. for (i = 0; i < fncount; i++)
  30. if ((fnlist[i].state >= fsm->state_count) || (fnlist[i].event >= fsm->event_count)) {
  31. printk(KERN_ERR "FsmNew Error line %d st(%ld/%ld) ev(%ld/%ld)\n",
  32. i, (long)fnlist[i].state, (long)fsm->state_count,
  33. (long)fnlist[i].event, (long)fsm->event_count);
  34. } else
  35. fsm->jumpmatrix[fsm->state_count * fnlist[i].event +
  36. fnlist[i].state] = (FSMFNPTR)fnlist[i].routine;
  37. return 0;
  38. }
  39. void
  40. FsmFree(struct Fsm *fsm)
  41. {
  42. kfree((void *) fsm->jumpmatrix);
  43. }
  44. int
  45. FsmEvent(struct FsmInst *fi, int event, void *arg)
  46. {
  47. FSMFNPTR r;
  48. if ((fi->state >= fi->fsm->state_count) || (event >= fi->fsm->event_count)) {
  49. printk(KERN_ERR "FsmEvent Error st(%ld/%ld) ev(%d/%ld)\n",
  50. (long)fi->state, (long)fi->fsm->state_count, event, (long)fi->fsm->event_count);
  51. return (1);
  52. }
  53. r = fi->fsm->jumpmatrix[fi->fsm->state_count * event + fi->state];
  54. if (r) {
  55. if (fi->debug)
  56. fi->printdebug(fi, "State %s Event %s",
  57. fi->fsm->strState[fi->state],
  58. fi->fsm->strEvent[event]);
  59. r(fi, event, arg);
  60. return (0);
  61. } else {
  62. if (fi->debug)
  63. fi->printdebug(fi, "State %s Event %s no routine",
  64. fi->fsm->strState[fi->state],
  65. fi->fsm->strEvent[event]);
  66. return (!0);
  67. }
  68. }
  69. void
  70. FsmChangeState(struct FsmInst *fi, int newstate)
  71. {
  72. fi->state = newstate;
  73. if (fi->debug)
  74. fi->printdebug(fi, "ChangeState %s",
  75. fi->fsm->strState[newstate]);
  76. }
  77. static void
  78. FsmExpireTimer(struct FsmTimer *ft)
  79. {
  80. #if FSM_TIMER_DEBUG
  81. if (ft->fi->debug)
  82. ft->fi->printdebug(ft->fi, "FsmExpireTimer %lx", (long) ft);
  83. #endif
  84. FsmEvent(ft->fi, ft->event, ft->arg);
  85. }
  86. void
  87. FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft)
  88. {
  89. ft->fi = fi;
  90. #if FSM_TIMER_DEBUG
  91. if (ft->fi->debug)
  92. ft->fi->printdebug(ft->fi, "FsmInitTimer %lx", (long) ft);
  93. #endif
  94. setup_timer(&ft->tl, (void *)FsmExpireTimer, (long)ft);
  95. }
  96. void
  97. FsmDelTimer(struct FsmTimer *ft, int where)
  98. {
  99. #if FSM_TIMER_DEBUG
  100. if (ft->fi->debug)
  101. ft->fi->printdebug(ft->fi, "FsmDelTimer %lx %d", (long) ft, where);
  102. #endif
  103. del_timer(&ft->tl);
  104. }
  105. int
  106. FsmAddTimer(struct FsmTimer *ft,
  107. int millisec, int event, void *arg, int where)
  108. {
  109. #if FSM_TIMER_DEBUG
  110. if (ft->fi->debug)
  111. ft->fi->printdebug(ft->fi, "FsmAddTimer %lx %d %d",
  112. (long) ft, millisec, where);
  113. #endif
  114. if (timer_pending(&ft->tl)) {
  115. printk(KERN_WARNING "FsmAddTimer: timer already active!\n");
  116. ft->fi->printdebug(ft->fi, "FsmAddTimer already active!");
  117. return -1;
  118. }
  119. init_timer(&ft->tl);
  120. ft->event = event;
  121. ft->arg = arg;
  122. ft->tl.expires = jiffies + (millisec * HZ) / 1000;
  123. add_timer(&ft->tl);
  124. return 0;
  125. }
  126. void
  127. FsmRestartTimer(struct FsmTimer *ft,
  128. int millisec, int event, void *arg, int where)
  129. {
  130. #if FSM_TIMER_DEBUG
  131. if (ft->fi->debug)
  132. ft->fi->printdebug(ft->fi, "FsmRestartTimer %lx %d %d",
  133. (long) ft, millisec, where);
  134. #endif
  135. if (timer_pending(&ft->tl))
  136. del_timer(&ft->tl);
  137. init_timer(&ft->tl);
  138. ft->event = event;
  139. ft->arg = arg;
  140. ft->tl.expires = jiffies + (millisec * HZ) / 1000;
  141. add_timer(&ft->tl);
  142. }