1/* $Id: fsm.c,v 1.1.1.1 2007/08/03 18:52:35 Exp $
2 *
3 * Finite state machine
4 *
5 * Author       Karsten Keil
6 * Copyright    by Karsten Keil      <keil@isdn4linux.de>
7 *              by Kai Germaschewski <kai.germaschewski@gmx.de>
8 *
9 * This software may be used and distributed according to the terms
10 * of the GNU General Public License, incorporated herein by reference.
11 *
12 * Thanks to    Jan den Ouden
13 *              Fritz Elfert
14 *
15 */
16
17#include <linux/module.h>
18#include <linux/init.h>
19#include "hisax.h"
20
21#define FSM_TIMER_DEBUG 0
22
23int
24FsmNew(struct Fsm *fsm, struct FsmNode *fnlist, int fncount)
25{
26	int i;
27
28	fsm->jumpmatrix = (FSMFNPTR *)
29		kzalloc(sizeof (FSMFNPTR) * fsm->state_count * fsm->event_count, GFP_KERNEL);
30	if (!fsm->jumpmatrix)
31		return -ENOMEM;
32
33	for (i = 0; i < fncount; i++)
34		if ((fnlist[i].state>=fsm->state_count) || (fnlist[i].event>=fsm->event_count)) {
35			printk(KERN_ERR "FsmNew Error line %d st(%ld/%ld) ev(%ld/%ld)\n",
36				i,(long)fnlist[i].state,(long)fsm->state_count,
37				(long)fnlist[i].event,(long)fsm->event_count);
38		} else
39			fsm->jumpmatrix[fsm->state_count * fnlist[i].event +
40				fnlist[i].state] = (FSMFNPTR) fnlist[i].routine;
41	return 0;
42}
43
44void
45FsmFree(struct Fsm *fsm)
46{
47	kfree((void *) fsm->jumpmatrix);
48}
49
50int
51FsmEvent(struct FsmInst *fi, int event, void *arg)
52{
53	FSMFNPTR r;
54
55	if ((fi->state>=fi->fsm->state_count) || (event >= fi->fsm->event_count)) {
56		printk(KERN_ERR "FsmEvent Error st(%ld/%ld) ev(%d/%ld)\n",
57			(long)fi->state,(long)fi->fsm->state_count,event,(long)fi->fsm->event_count);
58		return(1);
59	}
60	r = fi->fsm->jumpmatrix[fi->fsm->state_count * event + fi->state];
61	if (r) {
62		if (fi->debug)
63			fi->printdebug(fi, "State %s Event %s",
64				fi->fsm->strState[fi->state],
65				fi->fsm->strEvent[event]);
66		r(fi, event, arg);
67		return (0);
68	} else {
69		if (fi->debug)
70			fi->printdebug(fi, "State %s Event %s no routine",
71				fi->fsm->strState[fi->state],
72				fi->fsm->strEvent[event]);
73		return (!0);
74	}
75}
76
77void
78FsmChangeState(struct FsmInst *fi, int newstate)
79{
80	fi->state = newstate;
81	if (fi->debug)
82		fi->printdebug(fi, "ChangeState %s",
83			fi->fsm->strState[newstate]);
84}
85
86static void
87FsmExpireTimer(struct FsmTimer *ft)
88{
89#if FSM_TIMER_DEBUG
90	if (ft->fi->debug)
91		ft->fi->printdebug(ft->fi, "FsmExpireTimer %lx", (long) ft);
92#endif
93	FsmEvent(ft->fi, ft->event, ft->arg);
94}
95
96void
97FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft)
98{
99	ft->fi = fi;
100	ft->tl.function = (void *) FsmExpireTimer;
101	ft->tl.data = (long) ft;
102#if FSM_TIMER_DEBUG
103	if (ft->fi->debug)
104		ft->fi->printdebug(ft->fi, "FsmInitTimer %lx", (long) ft);
105#endif
106	init_timer(&ft->tl);
107}
108
109void
110FsmDelTimer(struct FsmTimer *ft, int where)
111{
112#if FSM_TIMER_DEBUG
113	if (ft->fi->debug)
114		ft->fi->printdebug(ft->fi, "FsmDelTimer %lx %d", (long) ft, where);
115#endif
116	del_timer(&ft->tl);
117}
118
119int
120FsmAddTimer(struct FsmTimer *ft,
121	    int millisec, int event, void *arg, int where)
122{
123
124#if FSM_TIMER_DEBUG
125	if (ft->fi->debug)
126		ft->fi->printdebug(ft->fi, "FsmAddTimer %lx %d %d",
127			(long) ft, millisec, where);
128#endif
129
130	if (timer_pending(&ft->tl)) {
131		printk(KERN_WARNING "FsmAddTimer: timer already active!\n");
132		ft->fi->printdebug(ft->fi, "FsmAddTimer already active!");
133		return -1;
134	}
135	init_timer(&ft->tl);
136	ft->event = event;
137	ft->arg = arg;
138	ft->tl.expires = jiffies + (millisec * HZ) / 1000;
139	add_timer(&ft->tl);
140	return 0;
141}
142
143void
144FsmRestartTimer(struct FsmTimer *ft,
145	    int millisec, int event, void *arg, int where)
146{
147
148#if FSM_TIMER_DEBUG
149	if (ft->fi->debug)
150		ft->fi->printdebug(ft->fi, "FsmRestartTimer %lx %d %d",
151			(long) ft, millisec, where);
152#endif
153
154	if (timer_pending(&ft->tl))
155		del_timer(&ft->tl);
156	init_timer(&ft->tl);
157	ft->event = event;
158	ft->arg = arg;
159	ft->tl.expires = jiffies + (millisec * HZ) / 1000;
160	add_timer(&ft->tl);
161}
162