fsm.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * A generic FSM based on fsm used in isdn4linux
  3. *
  4. */
  5. #include "fsm.h"
  6. #include <linux/module.h>
  7. #include <linux/slab.h>
  8. #include <linux/timer.h>
  9. MODULE_AUTHOR("(C) 2000 IBM Corp. by Fritz Elfert (felfert@millenux.com)");
  10. MODULE_DESCRIPTION("Finite state machine helper functions");
  11. MODULE_LICENSE("GPL");
  12. fsm_instance *
  13. init_fsm(char *name, const char **state_names, const char **event_names, int nr_states,
  14. int nr_events, const fsm_node *tmpl, int tmpl_len, gfp_t order)
  15. {
  16. int i;
  17. fsm_instance *this;
  18. fsm_function_t *m;
  19. fsm *f;
  20. this = kzalloc(sizeof(fsm_instance), order);
  21. if (this == NULL) {
  22. printk(KERN_WARNING
  23. "fsm(%s): init_fsm: Couldn't alloc instance\n", name);
  24. return NULL;
  25. }
  26. strlcpy(this->name, name, sizeof(this->name));
  27. init_waitqueue_head(&this->wait_q);
  28. f = kzalloc(sizeof(fsm), order);
  29. if (f == NULL) {
  30. printk(KERN_WARNING
  31. "fsm(%s): init_fsm: Couldn't alloc fsm\n", name);
  32. kfree_fsm(this);
  33. return NULL;
  34. }
  35. f->nr_events = nr_events;
  36. f->nr_states = nr_states;
  37. f->event_names = event_names;
  38. f->state_names = state_names;
  39. this->f = f;
  40. m = kcalloc(nr_states*nr_events, sizeof(fsm_function_t), order);
  41. if (m == NULL) {
  42. printk(KERN_WARNING
  43. "fsm(%s): init_fsm: Couldn't alloc jumptable\n", name);
  44. kfree_fsm(this);
  45. return NULL;
  46. }
  47. f->jumpmatrix = m;
  48. for (i = 0; i < tmpl_len; i++) {
  49. if ((tmpl[i].cond_state >= nr_states) ||
  50. (tmpl[i].cond_event >= nr_events) ) {
  51. printk(KERN_ERR
  52. "fsm(%s): init_fsm: Bad template l=%d st(%ld/%ld) ev(%ld/%ld)\n",
  53. name, i, (long)tmpl[i].cond_state, (long)f->nr_states,
  54. (long)tmpl[i].cond_event, (long)f->nr_events);
  55. kfree_fsm(this);
  56. return NULL;
  57. } else
  58. m[nr_states * tmpl[i].cond_event + tmpl[i].cond_state] =
  59. tmpl[i].function;
  60. }
  61. return this;
  62. }
  63. void
  64. kfree_fsm(fsm_instance *this)
  65. {
  66. if (this) {
  67. if (this->f) {
  68. kfree(this->f->jumpmatrix);
  69. kfree(this->f);
  70. }
  71. kfree(this);
  72. } else
  73. printk(KERN_WARNING
  74. "fsm: kfree_fsm called with NULL argument\n");
  75. }
  76. #if FSM_DEBUG_HISTORY
  77. void
  78. fsm_print_history(fsm_instance *fi)
  79. {
  80. int idx = 0;
  81. int i;
  82. if (fi->history_size >= FSM_HISTORY_SIZE)
  83. idx = fi->history_index;
  84. printk(KERN_DEBUG "fsm(%s): History:\n", fi->name);
  85. for (i = 0; i < fi->history_size; i++) {
  86. int e = fi->history[idx].event;
  87. int s = fi->history[idx++].state;
  88. idx %= FSM_HISTORY_SIZE;
  89. if (e == -1)
  90. printk(KERN_DEBUG " S=%s\n",
  91. fi->f->state_names[s]);
  92. else
  93. printk(KERN_DEBUG " S=%s E=%s\n",
  94. fi->f->state_names[s],
  95. fi->f->event_names[e]);
  96. }
  97. fi->history_size = fi->history_index = 0;
  98. }
  99. void
  100. fsm_record_history(fsm_instance *fi, int state, int event)
  101. {
  102. fi->history[fi->history_index].state = state;
  103. fi->history[fi->history_index++].event = event;
  104. fi->history_index %= FSM_HISTORY_SIZE;
  105. if (fi->history_size < FSM_HISTORY_SIZE)
  106. fi->history_size++;
  107. }
  108. #endif
  109. const char *
  110. fsm_getstate_str(fsm_instance *fi)
  111. {
  112. int st = atomic_read(&fi->state);
  113. if (st >= fi->f->nr_states)
  114. return "Invalid";
  115. return fi->f->state_names[st];
  116. }
  117. static void
  118. fsm_expire_timer(fsm_timer *this)
  119. {
  120. #if FSM_TIMER_DEBUG
  121. printk(KERN_DEBUG "fsm(%s): Timer %p expired\n",
  122. this->fi->name, this);
  123. #endif
  124. fsm_event(this->fi, this->expire_event, this->event_arg);
  125. }
  126. void
  127. fsm_settimer(fsm_instance *fi, fsm_timer *this)
  128. {
  129. this->fi = fi;
  130. this->tl.function = (void *)fsm_expire_timer;
  131. this->tl.data = (long)this;
  132. #if FSM_TIMER_DEBUG
  133. printk(KERN_DEBUG "fsm(%s): Create timer %p\n", fi->name,
  134. this);
  135. #endif
  136. init_timer(&this->tl);
  137. }
  138. void
  139. fsm_deltimer(fsm_timer *this)
  140. {
  141. #if FSM_TIMER_DEBUG
  142. printk(KERN_DEBUG "fsm(%s): Delete timer %p\n", this->fi->name,
  143. this);
  144. #endif
  145. del_timer(&this->tl);
  146. }
  147. int
  148. fsm_addtimer(fsm_timer *this, int millisec, int event, void *arg)
  149. {
  150. #if FSM_TIMER_DEBUG
  151. printk(KERN_DEBUG "fsm(%s): Add timer %p %dms\n",
  152. this->fi->name, this, millisec);
  153. #endif
  154. init_timer(&this->tl);
  155. this->tl.function = (void *)fsm_expire_timer;
  156. this->tl.data = (long)this;
  157. this->expire_event = event;
  158. this->event_arg = arg;
  159. this->tl.expires = jiffies + (millisec * HZ) / 1000;
  160. add_timer(&this->tl);
  161. return 0;
  162. }
  163. /* FIXME: this function is never used, why */
  164. void
  165. fsm_modtimer(fsm_timer *this, int millisec, int event, void *arg)
  166. {
  167. #if FSM_TIMER_DEBUG
  168. printk(KERN_DEBUG "fsm(%s): Restart timer %p %dms\n",
  169. this->fi->name, this, millisec);
  170. #endif
  171. del_timer(&this->tl);
  172. init_timer(&this->tl);
  173. this->tl.function = (void *)fsm_expire_timer;
  174. this->tl.data = (long)this;
  175. this->expire_event = event;
  176. this->event_arg = arg;
  177. this->tl.expires = jiffies + (millisec * HZ) / 1000;
  178. add_timer(&this->tl);
  179. }
  180. EXPORT_SYMBOL(init_fsm);
  181. EXPORT_SYMBOL(kfree_fsm);
  182. EXPORT_SYMBOL(fsm_settimer);
  183. EXPORT_SYMBOL(fsm_deltimer);
  184. EXPORT_SYMBOL(fsm_addtimer);
  185. EXPORT_SYMBOL(fsm_modtimer);
  186. EXPORT_SYMBOL(fsm_getstate_str);
  187. #if FSM_DEBUG_HISTORY
  188. EXPORT_SYMBOL(fsm_print_history);
  189. EXPORT_SYMBOL(fsm_record_history);
  190. #endif