1 // SPDX-License-Identifier: GPL-2.0
3 * A generic FSM based on fsm used in isdn4linux
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/timer.h>
12 MODULE_AUTHOR("(C) 2000 IBM Corp. by Fritz Elfert (felfert@millenux.com)");
13 MODULE_DESCRIPTION("Finite state machine helper functions");
14 MODULE_LICENSE("GPL");
17 init_fsm(char *name
, const char **state_names
, const char **event_names
, int nr_states
,
18 int nr_events
, const fsm_node
*tmpl
, int tmpl_len
, gfp_t order
)
25 this = kzalloc(sizeof(fsm_instance
), order
);
28 "fsm(%s): init_fsm: Couldn't alloc instance\n", name
);
31 strlcpy(this->name
, name
, sizeof(this->name
));
32 init_waitqueue_head(&this->wait_q
);
34 f
= kzalloc(sizeof(fsm
), order
);
37 "fsm(%s): init_fsm: Couldn't alloc fsm\n", name
);
41 f
->nr_events
= nr_events
;
42 f
->nr_states
= nr_states
;
43 f
->event_names
= event_names
;
44 f
->state_names
= state_names
;
47 m
= kcalloc(nr_states
*nr_events
, sizeof(fsm_function_t
), order
);
50 "fsm(%s): init_fsm: Couldn't alloc jumptable\n", name
);
56 for (i
= 0; i
< tmpl_len
; i
++) {
57 if ((tmpl
[i
].cond_state
>= nr_states
) ||
58 (tmpl
[i
].cond_event
>= nr_events
) ) {
60 "fsm(%s): init_fsm: Bad template l=%d st(%ld/%ld) ev(%ld/%ld)\n",
61 name
, i
, (long)tmpl
[i
].cond_state
, (long)f
->nr_states
,
62 (long)tmpl
[i
].cond_event
, (long)f
->nr_events
);
66 m
[nr_states
* tmpl
[i
].cond_event
+ tmpl
[i
].cond_state
] =
73 kfree_fsm(fsm_instance
*this)
77 kfree(this->f
->jumpmatrix
);
83 "fsm: kfree_fsm called with NULL argument\n");
88 fsm_print_history(fsm_instance
*fi
)
93 if (fi
->history_size
>= FSM_HISTORY_SIZE
)
94 idx
= fi
->history_index
;
96 printk(KERN_DEBUG
"fsm(%s): History:\n", fi
->name
);
97 for (i
= 0; i
< fi
->history_size
; i
++) {
98 int e
= fi
->history
[idx
].event
;
99 int s
= fi
->history
[idx
++].state
;
100 idx
%= FSM_HISTORY_SIZE
;
102 printk(KERN_DEBUG
" S=%s\n",
103 fi
->f
->state_names
[s
]);
105 printk(KERN_DEBUG
" S=%s E=%s\n",
106 fi
->f
->state_names
[s
],
107 fi
->f
->event_names
[e
]);
109 fi
->history_size
= fi
->history_index
= 0;
113 fsm_record_history(fsm_instance
*fi
, int state
, int event
)
115 fi
->history
[fi
->history_index
].state
= state
;
116 fi
->history
[fi
->history_index
++].event
= event
;
117 fi
->history_index
%= FSM_HISTORY_SIZE
;
118 if (fi
->history_size
< FSM_HISTORY_SIZE
)
124 fsm_getstate_str(fsm_instance
*fi
)
126 int st
= atomic_read(&fi
->state
);
127 if (st
>= fi
->f
->nr_states
)
129 return fi
->f
->state_names
[st
];
133 fsm_expire_timer(struct timer_list
*t
)
135 fsm_timer
*this = from_timer(this, t
, tl
);
137 printk(KERN_DEBUG
"fsm(%s): Timer %p expired\n",
138 this->fi
->name
, this);
140 fsm_event(this->fi
, this->expire_event
, this->event_arg
);
144 fsm_settimer(fsm_instance
*fi
, fsm_timer
*this)
148 printk(KERN_DEBUG
"fsm(%s): Create timer %p\n", fi
->name
,
151 timer_setup(&this->tl
, fsm_expire_timer
, 0);
155 fsm_deltimer(fsm_timer
*this)
158 printk(KERN_DEBUG
"fsm(%s): Delete timer %p\n", this->fi
->name
,
161 del_timer(&this->tl
);
165 fsm_addtimer(fsm_timer
*this, int millisec
, int event
, void *arg
)
169 printk(KERN_DEBUG
"fsm(%s): Add timer %p %dms\n",
170 this->fi
->name
, this, millisec
);
173 timer_setup(&this->tl
, fsm_expire_timer
, 0);
174 this->expire_event
= event
;
175 this->event_arg
= arg
;
176 this->tl
.expires
= jiffies
+ (millisec
* HZ
) / 1000;
177 add_timer(&this->tl
);
181 /* FIXME: this function is never used, why */
183 fsm_modtimer(fsm_timer
*this, int millisec
, int event
, void *arg
)
187 printk(KERN_DEBUG
"fsm(%s): Restart timer %p %dms\n",
188 this->fi
->name
, this, millisec
);
191 del_timer(&this->tl
);
192 timer_setup(&this->tl
, fsm_expire_timer
, 0);
193 this->expire_event
= event
;
194 this->event_arg
= arg
;
195 this->tl
.expires
= jiffies
+ (millisec
* HZ
) / 1000;
196 add_timer(&this->tl
);
199 EXPORT_SYMBOL(init_fsm
);
200 EXPORT_SYMBOL(kfree_fsm
);
201 EXPORT_SYMBOL(fsm_settimer
);
202 EXPORT_SYMBOL(fsm_deltimer
);
203 EXPORT_SYMBOL(fsm_addtimer
);
204 EXPORT_SYMBOL(fsm_modtimer
);
205 EXPORT_SYMBOL(fsm_getstate_str
);
207 #if FSM_DEBUG_HISTORY
208 EXPORT_SYMBOL(fsm_print_history
);
209 EXPORT_SYMBOL(fsm_record_history
);