1#ifndef _FSM_H_
2#define _FSM_H_
3
4#include <linux/kernel.h>
5#include <linux/types.h>
6#include <linux/timer.h>
7#include <linux/time.h>
8#include <linux/slab.h>
9#include <linux/sched.h>
10#include <linux/string.h>
11#include <asm/atomic.h>
12
13
14
15
16#define FSM_DEBUG 0
17
18
19
20
21
22#define FSM_TIMER_DEBUG 0
23
24
25
26
27
28
29#define FSM_DEBUG_HISTORY 0
30#define FSM_HISTORY_SIZE 40
31
32struct fsm_instance_t;
33
34
35
36
37typedef void (*fsm_function_t)(struct fsm_instance_t *, int, void *);
38
39
40
41
42typedef struct {
43 fsm_function_t *jumpmatrix;
44 int nr_events;
45 int nr_states;
46 const char **event_names;
47 const char **state_names;
48} fsm;
49
50#if FSM_DEBUG_HISTORY
51
52
53
54typedef struct {
55 int state;
56 int event;
57} fsm_history;
58#endif
59
60
61
62
63typedef struct fsm_instance_t {
64 fsm *f;
65 atomic_t state;
66 char name[16];
67 void *userdata;
68 int userint;
69#if FSM_DEBUG_HISTORY
70 int history_index;
71 int history_size;
72 fsm_history history[FSM_HISTORY_SIZE];
73#endif
74} fsm_instance;
75
76
77
78
79typedef struct {
80 int cond_state;
81 int cond_event;
82 fsm_function_t function;
83} fsm_node;
84
85
86
87
88typedef struct {
89 fsm_instance *fi;
90 struct timer_list tl;
91 int expire_event;
92 void *event_arg;
93} fsm_timer;
94
95
96
97
98
99
100
101
102
103
104
105
106
107extern fsm_instance *
108init_fsm(char *name, const char **state_names,
109 const char **event_names,
110 int nr_states, int nr_events, const fsm_node *tmpl,
111 int tmpl_len, gfp_t order);
112
113
114
115
116
117
118extern void kfree_fsm(fsm_instance *fi);
119
120#if FSM_DEBUG_HISTORY
121extern void
122fsm_print_history(fsm_instance *fi);
123
124extern void
125fsm_record_history(fsm_instance *fi, int state, int event);
126#endif
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141static inline int
142fsm_event(fsm_instance *fi, int event, void *arg)
143{
144 fsm_function_t r;
145 int state = atomic_read(&fi->state);
146
147 if ((state >= fi->f->nr_states) ||
148 (event >= fi->f->nr_events) ) {
149 printk(KERN_ERR "fsm(%s): Invalid state st(%ld/%ld) ev(%d/%ld)\n",
150 fi->name, (long)state,(long)fi->f->nr_states, event,
151 (long)fi->f->nr_events);
152#if FSM_DEBUG_HISTORY
153 fsm_print_history(fi);
154#endif
155 return 1;
156 }
157 r = fi->f->jumpmatrix[fi->f->nr_states * event + state];
158 if (r) {
159#if FSM_DEBUG
160 printk(KERN_DEBUG "fsm(%s): state %s event %s\n",
161 fi->name, fi->f->state_names[state],
162 fi->f->event_names[event]);
163#endif
164#if FSM_DEBUG_HISTORY
165 fsm_record_history(fi, state, event);
166#endif
167 r(fi, event, arg);
168 return 0;
169 } else {
170#if FSM_DEBUG || FSM_DEBUG_HISTORY
171 printk(KERN_DEBUG "fsm(%s): no function for event %s in state %s\n",
172 fi->name, fi->f->event_names[event],
173 fi->f->state_names[state]);
174#endif
175#if FSM_DEBUG_HISTORY
176 fsm_print_history(fi);
177#endif
178 return !0;
179 }
180}
181
182
183
184
185
186
187
188
189static inline void
190fsm_newstate(fsm_instance *fi, int newstate)
191{
192 atomic_set(&fi->state,newstate);
193#if FSM_DEBUG_HISTORY
194 fsm_record_history(fi, newstate, -1);
195#endif
196#if FSM_DEBUG
197 printk(KERN_DEBUG "fsm(%s): New state %s\n", fi->name,
198 fi->f->state_names[newstate]);
199#endif
200}
201
202
203
204
205
206
207
208
209static inline int
210fsm_getstate(fsm_instance *fi)
211{
212 return atomic_read(&fi->state);
213}
214
215
216
217
218
219
220
221
222extern const char *fsm_getstate_str(fsm_instance *fi);
223
224
225
226
227
228
229
230
231extern void fsm_settimer(fsm_instance *fi, fsm_timer *);
232
233
234
235
236
237
238extern void fsm_deltimer(fsm_timer *timer);
239
240
241
242
243
244
245
246
247
248
249
250
251extern int fsm_addtimer(fsm_timer *timer, int millisec, int event, void *arg);
252
253
254
255
256
257
258
259
260
261extern void fsm_modtimer(fsm_timer *timer, int millisec, int event, void *arg);
262
263#endif
264