fsm.c 4.8 KB

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