1 /* $Id: fsm.c,v 1.1.4.1 2001/11/20 14:19:35 kai 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 #define __NO_VERSION__
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include "hisax.h"
21
22 #define FSM_TIMER_DEBUG 0
23
24 int __init
FsmNew(struct Fsm * fsm,struct FsmNode * fnlist,int fncount)25 FsmNew(struct Fsm *fsm, struct FsmNode *fnlist, int fncount)
26 {
27 int i;
28
29 fsm->jumpmatrix = (FSMFNPTR *)
30 kmalloc(sizeof (FSMFNPTR) * fsm->state_count * fsm->event_count, GFP_KERNEL);
31 if (!fsm->jumpmatrix)
32 return -ENOMEM;
33
34 memset(fsm->jumpmatrix, 0, sizeof (FSMFNPTR) * fsm->state_count * fsm->event_count);
35
36 for (i = 0; i < fncount; i++)
37 if ((fnlist[i].state>=fsm->state_count) || (fnlist[i].event>=fsm->event_count)) {
38 printk(KERN_ERR "FsmNew Error line %d st(%ld/%ld) ev(%ld/%ld)\n",
39 i,(long)fnlist[i].state,(long)fsm->state_count,
40 (long)fnlist[i].event,(long)fsm->event_count);
41 } else
42 fsm->jumpmatrix[fsm->state_count * fnlist[i].event +
43 fnlist[i].state] = (FSMFNPTR) fnlist[i].routine;
44 return 0;
45 }
46
47 void
FsmFree(struct Fsm * fsm)48 FsmFree(struct Fsm *fsm)
49 {
50 kfree((void *) fsm->jumpmatrix);
51 }
52
53 int
FsmEvent(struct FsmInst * fi,int event,void * arg)54 FsmEvent(struct FsmInst *fi, int event, void *arg)
55 {
56 FSMFNPTR r;
57
58 if ((fi->state>=fi->fsm->state_count) || (event >= fi->fsm->event_count)) {
59 printk(KERN_ERR "FsmEvent Error st(%ld/%ld) ev(%d/%ld)\n",
60 (long)fi->state,(long)fi->fsm->state_count,event,(long)fi->fsm->event_count);
61 return(1);
62 }
63 r = fi->fsm->jumpmatrix[fi->fsm->state_count * event + fi->state];
64 if (r) {
65 if (fi->debug)
66 fi->printdebug(fi, "State %s Event %s",
67 fi->fsm->strState[fi->state],
68 fi->fsm->strEvent[event]);
69 r(fi, event, arg);
70 return (0);
71 } else {
72 if (fi->debug)
73 fi->printdebug(fi, "State %s Event %s no routine",
74 fi->fsm->strState[fi->state],
75 fi->fsm->strEvent[event]);
76 return (!0);
77 }
78 }
79
80 void
FsmChangeState(struct FsmInst * fi,int newstate)81 FsmChangeState(struct FsmInst *fi, int newstate)
82 {
83 fi->state = newstate;
84 if (fi->debug)
85 fi->printdebug(fi, "ChangeState %s",
86 fi->fsm->strState[newstate]);
87 }
88
89 static void
FsmExpireTimer(struct FsmTimer * ft)90 FsmExpireTimer(struct FsmTimer *ft)
91 {
92 #if FSM_TIMER_DEBUG
93 if (ft->fi->debug)
94 ft->fi->printdebug(ft->fi, "FsmExpireTimer %lx", (long) ft);
95 #endif
96 FsmEvent(ft->fi, ft->event, ft->arg);
97 }
98
99 void
FsmInitTimer(struct FsmInst * fi,struct FsmTimer * ft)100 FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft)
101 {
102 ft->fi = fi;
103 ft->tl.function = (void *) FsmExpireTimer;
104 ft->tl.data = (long) ft;
105 #if FSM_TIMER_DEBUG
106 if (ft->fi->debug)
107 ft->fi->printdebug(ft->fi, "FsmInitTimer %lx", (long) ft);
108 #endif
109 init_timer(&ft->tl);
110 }
111
112 void
FsmDelTimer(struct FsmTimer * ft,int where)113 FsmDelTimer(struct FsmTimer *ft, int where)
114 {
115 #if FSM_TIMER_DEBUG
116 if (ft->fi->debug)
117 ft->fi->printdebug(ft->fi, "FsmDelTimer %lx %d", (long) ft, where);
118 #endif
119 del_timer(&ft->tl);
120 }
121
122 int
FsmAddTimer(struct FsmTimer * ft,int millisec,int event,void * arg,int where)123 FsmAddTimer(struct FsmTimer *ft,
124 int millisec, int event, void *arg, int where)
125 {
126
127 #if FSM_TIMER_DEBUG
128 if (ft->fi->debug)
129 ft->fi->printdebug(ft->fi, "FsmAddTimer %lx %d %d",
130 (long) ft, millisec, where);
131 #endif
132
133 if (timer_pending(&ft->tl)) {
134 printk(KERN_WARNING "FsmAddTimer: timer already active!\n");
135 ft->fi->printdebug(ft->fi, "FsmAddTimer already active!");
136 return -1;
137 }
138 init_timer(&ft->tl);
139 ft->event = event;
140 ft->arg = arg;
141 ft->tl.expires = jiffies + (millisec * HZ) / 1000;
142 add_timer(&ft->tl);
143 return 0;
144 }
145
146 void
FsmRestartTimer(struct FsmTimer * ft,int millisec,int event,void * arg,int where)147 FsmRestartTimer(struct FsmTimer *ft,
148 int millisec, int event, void *arg, int where)
149 {
150
151 #if FSM_TIMER_DEBUG
152 if (ft->fi->debug)
153 ft->fi->printdebug(ft->fi, "FsmRestartTimer %lx %d %d",
154 (long) ft, millisec, where);
155 #endif
156
157 if (timer_pending(&ft->tl))
158 del_timer(&ft->tl);
159 init_timer(&ft->tl);
160 ft->event = event;
161 ft->arg = arg;
162 ft->tl.expires = jiffies + (millisec * HZ) / 1000;
163 add_timer(&ft->tl);
164 }
165