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