fsm.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. ft->tl.function = (void *) FsmExpireTimer;
  91. ft->tl.data = (long) ft;
  92. #if FSM_TIMER_DEBUG
  93. if (ft->fi->debug)
  94. ft->fi->printdebug(ft->fi, "FsmInitTimer %lx", (long) ft);
  95. #endif
  96. init_timer(&ft->tl);
  97. }
  98. void
  99. FsmDelTimer(struct FsmTimer *ft, int where)
  100. {
  101. #if FSM_TIMER_DEBUG
  102. if (ft->fi->debug)
  103. ft->fi->printdebug(ft->fi, "FsmDelTimer %lx %d", (long) ft, where);
  104. #endif
  105. del_timer(&ft->tl);
  106. }
  107. int
  108. FsmAddTimer(struct FsmTimer *ft,
  109. int millisec, int event, void *arg, int where)
  110. {
  111. #if FSM_TIMER_DEBUG
  112. if (ft->fi->debug)
  113. ft->fi->printdebug(ft->fi, "FsmAddTimer %lx %d %d",
  114. (long) ft, millisec, where);
  115. #endif
  116. if (timer_pending(&ft->tl)) {
  117. printk(KERN_WARNING "FsmAddTimer: timer already active!\n");
  118. ft->fi->printdebug(ft->fi, "FsmAddTimer already active!");
  119. return -1;
  120. }
  121. init_timer(&ft->tl);
  122. ft->event = event;
  123. ft->arg = arg;
  124. ft->tl.expires = jiffies + (millisec * HZ) / 1000;
  125. add_timer(&ft->tl);
  126. return 0;
  127. }
  128. void
  129. FsmRestartTimer(struct FsmTimer *ft,
  130. int millisec, int event, void *arg, int where)
  131. {
  132. #if FSM_TIMER_DEBUG
  133. if (ft->fi->debug)
  134. ft->fi->printdebug(ft->fi, "FsmRestartTimer %lx %d %d",
  135. (long) ft, millisec, where);
  136. #endif
  137. if (timer_pending(&ft->tl))
  138. del_timer(&ft->tl);
  139. init_timer(&ft->tl);
  140. ft->event = event;
  141. ft->arg = arg;
  142. ft->tl.expires = jiffies + (millisec * HZ) / 1000;
  143. add_timer(&ft->tl);
  144. }