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>
14 * Define this to get debugging messages.
19 * Define this to get debugging massages for
22 #define FSM_TIMER_DEBUG 0
25 * Define these to record a history of
26 * Events/Statechanges and print it if a
27 * action_function is not found.
29 #define FSM_DEBUG_HISTORY 0
30 #define FSM_HISTORY_SIZE 40
32 struct fsm_instance_t
;
35 * Definition of an action function, called by a FSM
37 typedef void (*fsm_function_t
)(struct fsm_instance_t
*, int, void *);
40 * Internal jump table for a FSM
43 fsm_function_t
*jumpmatrix
;
46 const char **event_names
;
47 const char **state_names
;
52 * Element of State/Event history used for debugging.
61 * Representation of a FSM
63 typedef struct fsm_instance_t
{
69 wait_queue_head_t wait_q
;
73 fsm_history history
[FSM_HISTORY_SIZE
];
78 * Description of a state-event combination
83 fsm_function_t function
;
87 * Description of a FSM Timer.
99 * @param name Name of this instance for logging purposes.
100 * @param state_names An array of names for all states for logging purposes.
101 * @param event_names An array of names for all events for logging purposes.
102 * @param nr_states Number of states for this instance.
103 * @param nr_events Number of events for this instance.
104 * @param tmpl An array of fsm_nodes, describing this FSM.
105 * @param tmpl_len Length of the describing array.
106 * @param order Parameter for allocation of the FSM data structs.
108 extern fsm_instance
*
109 init_fsm(char *name
, const char **state_names
,
110 const char **event_names
,
111 int nr_states
, int nr_events
, const fsm_node
*tmpl
,
112 int tmpl_len
, gfp_t order
);
117 * @param fi Pointer to an FSM, previously created with init_fsm.
119 extern void kfree_fsm(fsm_instance
*fi
);
121 #if FSM_DEBUG_HISTORY
123 fsm_print_history(fsm_instance
*fi
);
126 fsm_record_history(fsm_instance
*fi
, int state
, int event
);
130 * Emits an event to a FSM.
131 * If an action function is defined for the current state/event combination,
132 * this function is called.
134 * @param fi Pointer to FSM which should receive the event.
135 * @param event The event do be delivered.
136 * @param arg A generic argument, handed to the action function.
138 * @return 0 on success,
139 * 1 if current state or event is out of range
140 * !0 if state and event in range, but no action defined.
143 fsm_event(fsm_instance
*fi
, int event
, void *arg
)
146 int state
= atomic_read(&fi
->state
);
148 if ((state
>= fi
->f
->nr_states
) ||
149 (event
>= fi
->f
->nr_events
) ) {
150 printk(KERN_ERR
"fsm(%s): Invalid state st(%ld/%ld) ev(%d/%ld)\n",
151 fi
->name
, (long)state
,(long)fi
->f
->nr_states
, event
,
152 (long)fi
->f
->nr_events
);
153 #if FSM_DEBUG_HISTORY
154 fsm_print_history(fi
);
158 r
= fi
->f
->jumpmatrix
[fi
->f
->nr_states
* event
+ state
];
161 printk(KERN_DEBUG
"fsm(%s): state %s event %s\n",
162 fi
->name
, fi
->f
->state_names
[state
],
163 fi
->f
->event_names
[event
]);
165 #if FSM_DEBUG_HISTORY
166 fsm_record_history(fi
, state
, event
);
171 #if FSM_DEBUG || FSM_DEBUG_HISTORY
172 printk(KERN_DEBUG
"fsm(%s): no function for event %s in state %s\n",
173 fi
->name
, fi
->f
->event_names
[event
],
174 fi
->f
->state_names
[state
]);
176 #if FSM_DEBUG_HISTORY
177 fsm_print_history(fi
);
184 * Modifies the state of an FSM.
185 * This does <em>not</em> trigger an event or calls an action function.
187 * @param fi Pointer to FSM
188 * @param state The new state for this FSM.
191 fsm_newstate(fsm_instance
*fi
, int newstate
)
193 atomic_set(&fi
->state
,newstate
);
194 #if FSM_DEBUG_HISTORY
195 fsm_record_history(fi
, newstate
, -1);
198 printk(KERN_DEBUG
"fsm(%s): New state %s\n", fi
->name
,
199 fi
->f
->state_names
[newstate
]);
201 wake_up(&fi
->wait_q
);
205 * Retrieves the state of an FSM
207 * @param fi Pointer to FSM
209 * @return The current state of the FSM.
212 fsm_getstate(fsm_instance
*fi
)
214 return atomic_read(&fi
->state
);
218 * Retrieves the name of the state of an FSM
220 * @param fi Pointer to FSM
222 * @return The current state of the FSM in a human readable form.
224 extern const char *fsm_getstate_str(fsm_instance
*fi
);
227 * Initializes a timer for an FSM.
228 * This prepares an fsm_timer for usage with fsm_addtimer.
230 * @param fi Pointer to FSM
231 * @param timer The timer to be initialized.
233 extern void fsm_settimer(fsm_instance
*fi
, fsm_timer
*);
236 * Clears a pending timer of an FSM instance.
238 * @param timer The timer to clear.
240 extern void fsm_deltimer(fsm_timer
*timer
);
243 * Adds and starts a timer to an FSM instance.
245 * @param timer The timer to be added. The field fi of that timer
246 * must have been set to point to the instance.
247 * @param millisec Duration, after which the timer should expire.
248 * @param event Event, to trigger if timer expires.
249 * @param arg Generic argument, provided to expiry function.
251 * @return 0 on success, -1 if timer is already active.
253 extern int fsm_addtimer(fsm_timer
*timer
, int millisec
, int event
, void *arg
);
256 * Modifies a timer of an FSM.
258 * @param timer The timer to modify.
259 * @param millisec Duration, after which the timer should expire.
260 * @param event Event, to trigger if timer expires.
261 * @param arg Generic argument, provided to expiry function.
263 extern void fsm_modtimer(fsm_timer
*timer
, int millisec
, int event
, void *arg
);