fsm.c 4.7 KB

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