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(array3_size(sizeof(FSMFNPTR), fsm->state_count,
  27. fsm->event_count),
  28. GFP_KERNEL);
  29. if (!fsm->jumpmatrix)
  30. return -ENOMEM;
  31. for (i = 0; i < fncount; i++)
  32. if ((fnlist[i].state >= fsm->state_count) || (fnlist[i].event >= fsm->event_count)) {
  33. printk(KERN_ERR "FsmNew Error line %d st(%ld/%ld) ev(%ld/%ld)\n",
  34. i, (long)fnlist[i].state, (long)fsm->state_count,
  35. (long)fnlist[i].event, (long)fsm->event_count);
  36. } else
  37. fsm->jumpmatrix[fsm->state_count * fnlist[i].event +
  38. fnlist[i].state] = (FSMFNPTR)fnlist[i].routine;
  39. return 0;
  40. }
  41. void
  42. FsmFree(struct Fsm *fsm)
  43. {
  44. kfree((void *) fsm->jumpmatrix);
  45. }
  46. int
  47. FsmEvent(struct FsmInst *fi, int event, void *arg)
  48. {
  49. FSMFNPTR r;
  50. if ((fi->state >= fi->fsm->state_count) || (event >= fi->fsm->event_count)) {
  51. printk(KERN_ERR "FsmEvent Error st(%ld/%ld) ev(%d/%ld)\n",
  52. (long)fi->state, (long)fi->fsm->state_count, event, (long)fi->fsm->event_count);
  53. return (1);
  54. }
  55. r = fi->fsm->jumpmatrix[fi->fsm->state_count * event + fi->state];
  56. if (r) {
  57. if (fi->debug)
  58. fi->printdebug(fi, "State %s Event %s",
  59. fi->fsm->strState[fi->state],
  60. fi->fsm->strEvent[event]);
  61. r(fi, event, arg);
  62. return (0);
  63. } else {
  64. if (fi->debug)
  65. fi->printdebug(fi, "State %s Event %s no routine",
  66. fi->fsm->strState[fi->state],
  67. fi->fsm->strEvent[event]);
  68. return (!0);
  69. }
  70. }
  71. void
  72. FsmChangeState(struct FsmInst *fi, int newstate)
  73. {
  74. fi->state = newstate;
  75. if (fi->debug)
  76. fi->printdebug(fi, "ChangeState %s",
  77. fi->fsm->strState[newstate]);
  78. }
  79. static void
  80. FsmExpireTimer(struct timer_list *t)
  81. {
  82. struct FsmTimer *ft = from_timer(ft, t, tl);
  83. #if FSM_TIMER_DEBUG
  84. if (ft->fi->debug)
  85. ft->fi->printdebug(ft->fi, "FsmExpireTimer %lx", (long) ft);
  86. #endif
  87. FsmEvent(ft->fi, ft->event, ft->arg);
  88. }
  89. void
  90. FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft)
  91. {
  92. ft->fi = fi;
  93. #if FSM_TIMER_DEBUG
  94. if (ft->fi->debug)
  95. ft->fi->printdebug(ft->fi, "FsmInitTimer %lx", (long) ft);
  96. #endif
  97. timer_setup(&ft->tl, FsmExpireTimer, 0);
  98. }
  99. void
  100. FsmDelTimer(struct FsmTimer *ft, int where)
  101. {
  102. #if FSM_TIMER_DEBUG
  103. if (ft->fi->debug)
  104. ft->fi->printdebug(ft->fi, "FsmDelTimer %lx %d", (long) ft, where);
  105. #endif
  106. del_timer(&ft->tl);
  107. }
  108. int
  109. FsmAddTimer(struct FsmTimer *ft,
  110. int millisec, int event, void *arg, int where)
  111. {
  112. #if FSM_TIMER_DEBUG
  113. if (ft->fi->debug)
  114. ft->fi->printdebug(ft->fi, "FsmAddTimer %lx %d %d",
  115. (long) ft, millisec, where);
  116. #endif
  117. if (timer_pending(&ft->tl)) {
  118. printk(KERN_WARNING "FsmAddTimer: timer already active!\n");
  119. ft->fi->printdebug(ft->fi, "FsmAddTimer already active!");
  120. return -1;
  121. }
  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. ft->event = event;
  140. ft->arg = arg;
  141. ft->tl.expires = jiffies + (millisec * HZ) / 1000;
  142. add_timer(&ft->tl);
  143. }