blob: 3e020ec0f65e075809848790ec535cc615a86065 [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
Zhang Yanfeif754e912013-03-11 19:15:49 +000029 fsm->jumpmatrix =
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
Kees Cook5e8b8242017-10-16 17:28:54 -070088FsmExpireTimer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Kees Cook5e8b8242017-10-16 17:28:54 -070090 struct FsmTimer *ft = from_timer(ft, t, tl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#if FSM_TIMER_DEBUG
92 if (ft->fi->debug)
93 ft->fi->printdebug(ft->fi, "FsmExpireTimer %lx", (long) ft);
94#endif
95 FsmEvent(ft->fi, ft->event, ft->arg);
96}
97
98void
99FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft)
100{
101 ft->fi = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#if FSM_TIMER_DEBUG
103 if (ft->fi->debug)
104 ft->fi->printdebug(ft->fi, "FsmInitTimer %lx", (long) ft);
105#endif
Kees Cook5e8b8242017-10-16 17:28:54 -0700106 timer_setup(&ft->tl, FsmExpireTimer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
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",
Joe Perches475be4d2012-02-19 19:52:38 -0800127 (long) ft, millisec, where);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 ft->event = event;
136 ft->arg = arg;
137 ft->tl.expires = jiffies + (millisec * HZ) / 1000;
138 add_timer(&ft->tl);
139 return 0;
140}
141
142void
143FsmRestartTimer(struct FsmTimer *ft,
Joe Perches475be4d2012-02-19 19:52:38 -0800144 int millisec, int event, void *arg, int where)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146
147#if FSM_TIMER_DEBUG
148 if (ft->fi->debug)
149 ft->fi->printdebug(ft->fi, "FsmRestartTimer %lx %d %d",
Joe Perches475be4d2012-02-19 19:52:38 -0800150 (long) ft, millisec, where);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151#endif
152
153 if (timer_pending(&ft->tl))
154 del_timer(&ft->tl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 ft->event = event;
156 ft->arg = arg;
157 ft->tl.expires = jiffies + (millisec * HZ) / 1000;
158 add_timer(&ft->tl);
159}