1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _FSM_H_
3*4882a593Smuzhiyun #define _FSM_H_
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun #include <linux/kernel.h>
6*4882a593Smuzhiyun #include <linux/types.h>
7*4882a593Smuzhiyun #include <linux/timer.h>
8*4882a593Smuzhiyun #include <linux/time.h>
9*4882a593Smuzhiyun #include <linux/slab.h>
10*4882a593Smuzhiyun #include <linux/sched.h>
11*4882a593Smuzhiyun #include <linux/string.h>
12*4882a593Smuzhiyun #include <linux/atomic.h>
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun /**
15*4882a593Smuzhiyun * Define this to get debugging messages.
16*4882a593Smuzhiyun */
17*4882a593Smuzhiyun #define FSM_DEBUG 0
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun /**
20*4882a593Smuzhiyun * Define this to get debugging massages for
21*4882a593Smuzhiyun * timer handling.
22*4882a593Smuzhiyun */
23*4882a593Smuzhiyun #define FSM_TIMER_DEBUG 0
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun /**
26*4882a593Smuzhiyun * Define these to record a history of
27*4882a593Smuzhiyun * Events/Statechanges and print it if a
28*4882a593Smuzhiyun * action_function is not found.
29*4882a593Smuzhiyun */
30*4882a593Smuzhiyun #define FSM_DEBUG_HISTORY 0
31*4882a593Smuzhiyun #define FSM_HISTORY_SIZE 40
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun struct fsm_instance_t;
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun /**
36*4882a593Smuzhiyun * Definition of an action function, called by a FSM
37*4882a593Smuzhiyun */
38*4882a593Smuzhiyun typedef void (*fsm_function_t)(struct fsm_instance_t *, int, void *);
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /**
41*4882a593Smuzhiyun * Internal jump table for a FSM
42*4882a593Smuzhiyun */
43*4882a593Smuzhiyun typedef struct {
44*4882a593Smuzhiyun fsm_function_t *jumpmatrix;
45*4882a593Smuzhiyun int nr_events;
46*4882a593Smuzhiyun int nr_states;
47*4882a593Smuzhiyun const char **event_names;
48*4882a593Smuzhiyun const char **state_names;
49*4882a593Smuzhiyun } fsm;
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun #if FSM_DEBUG_HISTORY
52*4882a593Smuzhiyun /**
53*4882a593Smuzhiyun * Element of State/Event history used for debugging.
54*4882a593Smuzhiyun */
55*4882a593Smuzhiyun typedef struct {
56*4882a593Smuzhiyun int state;
57*4882a593Smuzhiyun int event;
58*4882a593Smuzhiyun } fsm_history;
59*4882a593Smuzhiyun #endif
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun /**
62*4882a593Smuzhiyun * Representation of a FSM
63*4882a593Smuzhiyun */
64*4882a593Smuzhiyun typedef struct fsm_instance_t {
65*4882a593Smuzhiyun fsm *f;
66*4882a593Smuzhiyun atomic_t state;
67*4882a593Smuzhiyun char name[16];
68*4882a593Smuzhiyun void *userdata;
69*4882a593Smuzhiyun int userint;
70*4882a593Smuzhiyun wait_queue_head_t wait_q;
71*4882a593Smuzhiyun #if FSM_DEBUG_HISTORY
72*4882a593Smuzhiyun int history_index;
73*4882a593Smuzhiyun int history_size;
74*4882a593Smuzhiyun fsm_history history[FSM_HISTORY_SIZE];
75*4882a593Smuzhiyun #endif
76*4882a593Smuzhiyun } fsm_instance;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun /**
79*4882a593Smuzhiyun * Description of a state-event combination
80*4882a593Smuzhiyun */
81*4882a593Smuzhiyun typedef struct {
82*4882a593Smuzhiyun int cond_state;
83*4882a593Smuzhiyun int cond_event;
84*4882a593Smuzhiyun fsm_function_t function;
85*4882a593Smuzhiyun } fsm_node;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /**
88*4882a593Smuzhiyun * Description of a FSM Timer.
89*4882a593Smuzhiyun */
90*4882a593Smuzhiyun typedef struct {
91*4882a593Smuzhiyun fsm_instance *fi;
92*4882a593Smuzhiyun struct timer_list tl;
93*4882a593Smuzhiyun int expire_event;
94*4882a593Smuzhiyun void *event_arg;
95*4882a593Smuzhiyun } fsm_timer;
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun /**
98*4882a593Smuzhiyun * Creates an FSM
99*4882a593Smuzhiyun *
100*4882a593Smuzhiyun * @param name Name of this instance for logging purposes.
101*4882a593Smuzhiyun * @param state_names An array of names for all states for logging purposes.
102*4882a593Smuzhiyun * @param event_names An array of names for all events for logging purposes.
103*4882a593Smuzhiyun * @param nr_states Number of states for this instance.
104*4882a593Smuzhiyun * @param nr_events Number of events for this instance.
105*4882a593Smuzhiyun * @param tmpl An array of fsm_nodes, describing this FSM.
106*4882a593Smuzhiyun * @param tmpl_len Length of the describing array.
107*4882a593Smuzhiyun * @param order Parameter for allocation of the FSM data structs.
108*4882a593Smuzhiyun */
109*4882a593Smuzhiyun extern fsm_instance *
110*4882a593Smuzhiyun init_fsm(char *name, const char **state_names,
111*4882a593Smuzhiyun const char **event_names,
112*4882a593Smuzhiyun int nr_states, int nr_events, const fsm_node *tmpl,
113*4882a593Smuzhiyun int tmpl_len, gfp_t order);
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun /**
116*4882a593Smuzhiyun * Releases an FSM
117*4882a593Smuzhiyun *
118*4882a593Smuzhiyun * @param fi Pointer to an FSM, previously created with init_fsm.
119*4882a593Smuzhiyun */
120*4882a593Smuzhiyun extern void kfree_fsm(fsm_instance *fi);
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun #if FSM_DEBUG_HISTORY
123*4882a593Smuzhiyun extern void
124*4882a593Smuzhiyun fsm_print_history(fsm_instance *fi);
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun extern void
127*4882a593Smuzhiyun fsm_record_history(fsm_instance *fi, int state, int event);
128*4882a593Smuzhiyun #endif
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun /**
131*4882a593Smuzhiyun * Emits an event to a FSM.
132*4882a593Smuzhiyun * If an action function is defined for the current state/event combination,
133*4882a593Smuzhiyun * this function is called.
134*4882a593Smuzhiyun *
135*4882a593Smuzhiyun * @param fi Pointer to FSM which should receive the event.
136*4882a593Smuzhiyun * @param event The event do be delivered.
137*4882a593Smuzhiyun * @param arg A generic argument, handed to the action function.
138*4882a593Smuzhiyun *
139*4882a593Smuzhiyun * @return 0 on success,
140*4882a593Smuzhiyun * 1 if current state or event is out of range
141*4882a593Smuzhiyun * !0 if state and event in range, but no action defined.
142*4882a593Smuzhiyun */
143*4882a593Smuzhiyun static inline int
fsm_event(fsm_instance * fi,int event,void * arg)144*4882a593Smuzhiyun fsm_event(fsm_instance *fi, int event, void *arg)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun fsm_function_t r;
147*4882a593Smuzhiyun int state = atomic_read(&fi->state);
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun if ((state >= fi->f->nr_states) ||
150*4882a593Smuzhiyun (event >= fi->f->nr_events) ) {
151*4882a593Smuzhiyun printk(KERN_ERR "fsm(%s): Invalid state st(%ld/%ld) ev(%d/%ld)\n",
152*4882a593Smuzhiyun fi->name, (long)state,(long)fi->f->nr_states, event,
153*4882a593Smuzhiyun (long)fi->f->nr_events);
154*4882a593Smuzhiyun #if FSM_DEBUG_HISTORY
155*4882a593Smuzhiyun fsm_print_history(fi);
156*4882a593Smuzhiyun #endif
157*4882a593Smuzhiyun return 1;
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun r = fi->f->jumpmatrix[fi->f->nr_states * event + state];
160*4882a593Smuzhiyun if (r) {
161*4882a593Smuzhiyun #if FSM_DEBUG
162*4882a593Smuzhiyun printk(KERN_DEBUG "fsm(%s): state %s event %s\n",
163*4882a593Smuzhiyun fi->name, fi->f->state_names[state],
164*4882a593Smuzhiyun fi->f->event_names[event]);
165*4882a593Smuzhiyun #endif
166*4882a593Smuzhiyun #if FSM_DEBUG_HISTORY
167*4882a593Smuzhiyun fsm_record_history(fi, state, event);
168*4882a593Smuzhiyun #endif
169*4882a593Smuzhiyun r(fi, event, arg);
170*4882a593Smuzhiyun return 0;
171*4882a593Smuzhiyun } else {
172*4882a593Smuzhiyun #if FSM_DEBUG || FSM_DEBUG_HISTORY
173*4882a593Smuzhiyun printk(KERN_DEBUG "fsm(%s): no function for event %s in state %s\n",
174*4882a593Smuzhiyun fi->name, fi->f->event_names[event],
175*4882a593Smuzhiyun fi->f->state_names[state]);
176*4882a593Smuzhiyun #endif
177*4882a593Smuzhiyun #if FSM_DEBUG_HISTORY
178*4882a593Smuzhiyun fsm_print_history(fi);
179*4882a593Smuzhiyun #endif
180*4882a593Smuzhiyun return !0;
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun /**
185*4882a593Smuzhiyun * Modifies the state of an FSM.
186*4882a593Smuzhiyun * This does <em>not</em> trigger an event or calls an action function.
187*4882a593Smuzhiyun *
188*4882a593Smuzhiyun * @param fi Pointer to FSM
189*4882a593Smuzhiyun * @param state The new state for this FSM.
190*4882a593Smuzhiyun */
191*4882a593Smuzhiyun static inline void
fsm_newstate(fsm_instance * fi,int newstate)192*4882a593Smuzhiyun fsm_newstate(fsm_instance *fi, int newstate)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun atomic_set(&fi->state,newstate);
195*4882a593Smuzhiyun #if FSM_DEBUG_HISTORY
196*4882a593Smuzhiyun fsm_record_history(fi, newstate, -1);
197*4882a593Smuzhiyun #endif
198*4882a593Smuzhiyun #if FSM_DEBUG
199*4882a593Smuzhiyun printk(KERN_DEBUG "fsm(%s): New state %s\n", fi->name,
200*4882a593Smuzhiyun fi->f->state_names[newstate]);
201*4882a593Smuzhiyun #endif
202*4882a593Smuzhiyun wake_up(&fi->wait_q);
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun /**
206*4882a593Smuzhiyun * Retrieves the state of an FSM
207*4882a593Smuzhiyun *
208*4882a593Smuzhiyun * @param fi Pointer to FSM
209*4882a593Smuzhiyun *
210*4882a593Smuzhiyun * @return The current state of the FSM.
211*4882a593Smuzhiyun */
212*4882a593Smuzhiyun static inline int
fsm_getstate(fsm_instance * fi)213*4882a593Smuzhiyun fsm_getstate(fsm_instance *fi)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun return atomic_read(&fi->state);
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun /**
219*4882a593Smuzhiyun * Retrieves the name of the state of an FSM
220*4882a593Smuzhiyun *
221*4882a593Smuzhiyun * @param fi Pointer to FSM
222*4882a593Smuzhiyun *
223*4882a593Smuzhiyun * @return The current state of the FSM in a human readable form.
224*4882a593Smuzhiyun */
225*4882a593Smuzhiyun extern const char *fsm_getstate_str(fsm_instance *fi);
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun /**
228*4882a593Smuzhiyun * Initializes a timer for an FSM.
229*4882a593Smuzhiyun * This prepares an fsm_timer for usage with fsm_addtimer.
230*4882a593Smuzhiyun *
231*4882a593Smuzhiyun * @param fi Pointer to FSM
232*4882a593Smuzhiyun * @param timer The timer to be initialized.
233*4882a593Smuzhiyun */
234*4882a593Smuzhiyun extern void fsm_settimer(fsm_instance *fi, fsm_timer *);
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun /**
237*4882a593Smuzhiyun * Clears a pending timer of an FSM instance.
238*4882a593Smuzhiyun *
239*4882a593Smuzhiyun * @param timer The timer to clear.
240*4882a593Smuzhiyun */
241*4882a593Smuzhiyun extern void fsm_deltimer(fsm_timer *timer);
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun /**
244*4882a593Smuzhiyun * Adds and starts a timer to an FSM instance.
245*4882a593Smuzhiyun *
246*4882a593Smuzhiyun * @param timer The timer to be added. The field fi of that timer
247*4882a593Smuzhiyun * must have been set to point to the instance.
248*4882a593Smuzhiyun * @param millisec Duration, after which the timer should expire.
249*4882a593Smuzhiyun * @param event Event, to trigger if timer expires.
250*4882a593Smuzhiyun * @param arg Generic argument, provided to expiry function.
251*4882a593Smuzhiyun *
252*4882a593Smuzhiyun * @return 0 on success, -1 if timer is already active.
253*4882a593Smuzhiyun */
254*4882a593Smuzhiyun extern int fsm_addtimer(fsm_timer *timer, int millisec, int event, void *arg);
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun /**
257*4882a593Smuzhiyun * Modifies a timer of an FSM.
258*4882a593Smuzhiyun *
259*4882a593Smuzhiyun * @param timer The timer to modify.
260*4882a593Smuzhiyun * @param millisec Duration, after which the timer should expire.
261*4882a593Smuzhiyun * @param event Event, to trigger if timer expires.
262*4882a593Smuzhiyun * @param arg Generic argument, provided to expiry function.
263*4882a593Smuzhiyun */
264*4882a593Smuzhiyun extern void fsm_modtimer(fsm_timer *timer, int millisec, int event, void *arg);
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun #endif /* _FSM_H_ */
267