Linux Audio

Check our new training course

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