Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
fsm.c
Go to the documentation of this file.
1 /* $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 <[email protected]>
7  * by Kai Germaschewski <[email protected]>
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/slab.h>
19 #include <linux/init.h>
20 #include "hisax.h"
21 
22 #define FSM_TIMER_DEBUG 0
23 
24 int
25 FsmNew(struct Fsm *fsm, struct FsmNode *fnlist, int fncount)
26 {
27  int i;
28 
29  fsm->jumpmatrix = (FSMFNPTR *)
30  kzalloc(sizeof(FSMFNPTR) * fsm->state_count * fsm->event_count, GFP_KERNEL);
31  if (!fsm->jumpmatrix)
32  return -ENOMEM;
33 
34  for (i = 0; i < fncount; i++)
35  if ((fnlist[i].state >= fsm->state_count) || (fnlist[i].event >= fsm->event_count)) {
36  printk(KERN_ERR "FsmNew Error line %d st(%ld/%ld) ev(%ld/%ld)\n",
37  i, (long)fnlist[i].state, (long)fsm->state_count,
38  (long)fnlist[i].event, (long)fsm->event_count);
39  } else
40  fsm->jumpmatrix[fsm->state_count * fnlist[i].event +
41  fnlist[i].state] = (FSMFNPTR)fnlist[i].routine;
42  return 0;
43 }
44 
45 void
46 FsmFree(struct Fsm *fsm)
47 {
48  kfree((void *) fsm->jumpmatrix);
49 }
50 
51 int
52 FsmEvent(struct FsmInst *fi, int event, void *arg)
53 {
54  FSMFNPTR r;
55 
56  if ((fi->state >= fi->fsm->state_count) || (event >= fi->fsm->event_count)) {
57  printk(KERN_ERR "FsmEvent Error st(%ld/%ld) ev(%d/%ld)\n",
58  (long)fi->state, (long)fi->fsm->state_count, event, (long)fi->fsm->event_count);
59  return (1);
60  }
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",
65  fi->fsm->strState[fi->state],
66  fi->fsm->strEvent[event]);
67  r(fi, event, arg);
68  return (0);
69  } else {
70  if (fi->debug)
71  fi->printdebug(fi, "State %s Event %s no routine",
72  fi->fsm->strState[fi->state],
73  fi->fsm->strEvent[event]);
74  return (!0);
75  }
76 }
77 
78 void
79 FsmChangeState(struct FsmInst *fi, int newstate)
80 {
81  fi->state = newstate;
82  if (fi->debug)
83  fi->printdebug(fi, "ChangeState %s",
84  fi->fsm->strState[newstate]);
85 }
86 
87 static void
88 FsmExpireTimer(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 
97 void
98 FsmInitTimer(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 
110 void
111 FsmDelTimer(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 
120 int
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",
128  (long) ft, millisec, where);
129 #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 
144 void
146  int millisec, int event, void *arg, int where)
147 {
148 
149 #if FSM_TIMER_DEBUG
150  if (ft->fi->debug)
151  ft->fi->printdebug(ft->fi, "FsmRestartTimer %lx %d %d",
152  (long) ft, millisec, where);
153 #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 }