blob: 1bb291021fdbad40ff6c094e4aa23f0cee584112 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: fsm.c,v 1.14.6.4 2001/09/23 22:24:47 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>
Joe Perches475be4d2012-02-19 19:52:38 -08008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/init.h>
20#include "hisax.h"
21
22#define FSM_TIMER_DEBUG 0
23
24int
25FsmNew(struct Fsm *fsm, struct FsmNode *fnlist, int fncount)
26{
27 int i;
28
29 fsm->jumpmatrix = (FSMFNPTR *)
Joe Perches475be4d2012-02-19 19:52:38 -080030 kzalloc(sizeof(FSMFNPTR) * fsm->state_count * fsm->event_count, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 if (!fsm->jumpmatrix)
32 return -ENOMEM;
33
Joe Perches475be4d2012-02-19 19:52:38 -080034 for (i = 0; i < fncount; i++)
35 if ((fnlist[i].state >= fsm->state_count) || (fnlist[i].event >= fsm->event_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 printk(KERN_ERR "FsmNew Error line %d st(%ld/%ld) ev(%ld/%ld)\n",
Joe Perches475be4d2012-02-19 19:52:38 -080037 i, (long)fnlist[i].state, (long)fsm->state_count,
38 (long)fnlist[i].event, (long)fsm->event_count);
39 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 fsm->jumpmatrix[fsm->state_count * fnlist[i].event +
Joe Perches475be4d2012-02-19 19:52:38 -080041 fnlist[i].state] = (FSMFNPTR)fnlist[i].routine;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 return 0;
43}
44
45void
46FsmFree(struct Fsm *fsm)
47{
48 kfree((void *) fsm->jumpmatrix);
49}
50
51int
52FsmEvent(struct FsmInst *fi, int event, void *arg)
53{
54 FSMFNPTR r;
55
Joe Perches475be4d2012-02-19 19:52:38 -080056 if ((fi->state >= fi->fsm->state_count) || (event >= fi->fsm->event_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 printk(KERN_ERR "FsmEvent Error st(%ld/%ld) ev(%d/%ld)\n",
Joe Perches475be4d2012-02-19 19:52:38 -080058 (long)fi->state, (long)fi->fsm->state_count, event, (long)fi->fsm->event_count);
59 return (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 }
61 r = fi->fsm->jumpmatrix[fi->fsm->state_count * event + fi->state];
62 if (r) {
63 if (fi->debug)
64 fi->printdebug(fi, "State %s Event %s",
Joe Perches475be4d2012-02-19 19:52:38 -080065 fi->fsm->strState[fi->state],
66 fi->fsm->strEvent[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 r(fi, event, arg);
68 return (0);
69 } else {
70 if (fi->debug)
71 fi->printdebug(fi, "State %s Event %s no routine",
Joe Perches475be4d2012-02-19 19:52:38 -080072 fi->fsm->strState[fi->state],
73 fi->fsm->strEvent[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return (!0);
75 }
76}
77
78void
79FsmChangeState(struct FsmInst *fi, int newstate)
80{
81 fi->state = newstate;
82 if (fi->debug)
83 fi->printdebug(fi, "ChangeState %s",
Joe Perches475be4d2012-02-19 19:52:38 -080084 fi->fsm->strState[newstate]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87static void
88FsmExpireTimer(struct FsmTimer *ft)
89{
90#if FSM_TIMER_DEBUG
91 if (ft->fi->debug)
92 ft->fi->printdebug(ft->fi, "FsmExpireTimer %lx", (long) ft);
93#endif
94 FsmEvent(ft->fi, ft->event, ft->arg);
95}
96
97void
98FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft)
99{
100 ft->fi = fi;
101 ft->tl.function = (void *) FsmExpireTimer;
102 ft->tl.data = (long) ft;
103#if FSM_TIMER_DEBUG
104 if (ft->fi->debug)
105 ft->fi->printdebug(ft->fi, "FsmInitTimer %lx", (long) ft);
106#endif
107 init_timer(&ft->tl);
108}
109
110void
111FsmDelTimer(struct FsmTimer *ft, int where)
112{
113#if FSM_TIMER_DEBUG
114 if (ft->fi->debug)
115 ft->fi->printdebug(ft->fi, "FsmDelTimer %lx %d", (long) ft, where);
116#endif
117 del_timer(&ft->tl);
118}
119
120int
121FsmAddTimer(struct FsmTimer *ft,
122 int millisec, int event, void *arg, int where)
123{
124
125#if FSM_TIMER_DEBUG
126 if (ft->fi->debug)
127 ft->fi->printdebug(ft->fi, "FsmAddTimer %lx %d %d",
Joe Perches475be4d2012-02-19 19:52:38 -0800128 (long) ft, millisec, where);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#endif
130
131 if (timer_pending(&ft->tl)) {
132 printk(KERN_WARNING "FsmAddTimer: timer already active!\n");
133 ft->fi->printdebug(ft->fi, "FsmAddTimer already active!");
134 return -1;
135 }
136 init_timer(&ft->tl);
137 ft->event = event;
138 ft->arg = arg;
139 ft->tl.expires = jiffies + (millisec * HZ) / 1000;
140 add_timer(&ft->tl);
141 return 0;
142}
143
144void
145FsmRestartTimer(struct FsmTimer *ft,
Joe Perches475be4d2012-02-19 19:52:38 -0800146 int millisec, int event, void *arg, int where)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148
149#if FSM_TIMER_DEBUG
150 if (ft->fi->debug)
151 ft->fi->printdebug(ft->fi, "FsmRestartTimer %lx %d %d",
Joe Perches475be4d2012-02-19 19:52:38 -0800152 (long) ft, millisec, where);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#endif
154
155 if (timer_pending(&ft->tl))
156 del_timer(&ft->tl);
157 init_timer(&ft->tl);
158 ft->event = event;
159 ft->arg = arg;
160 ft->tl.expires = jiffies + (millisec * HZ) / 1000;
161 add_timer(&ft->tl);
162}