Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / isdn / hisax / fsm.c
blob732ea633758ceac7b2cae2e3bf00d276da0050f1
1 /* $Id: fsm.c,v 1.14.6.4 2001/09/23 22:24:47 kai Exp $
3 * Finite state machine
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.
12 * Thanks to Jan den Ouden
13 * Fritz Elfert
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include "hisax.h"
22 #define FSM_TIMER_DEBUG 0
24 int
25 FsmNew(struct Fsm *fsm, struct FsmNode *fnlist, int fncount)
27 int i;
29 fsm->jumpmatrix = (FSMFNPTR *)
30 kzalloc(sizeof (FSMFNPTR) * fsm->state_count * fsm->event_count, GFP_KERNEL);
31 if (!fsm->jumpmatrix)
32 return -ENOMEM;
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;
45 void
46 FsmFree(struct Fsm *fsm)
48 kfree((void *) fsm->jumpmatrix);
51 int
52 FsmEvent(struct FsmInst *fi, int event, void *arg)
54 FSMFNPTR r;
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);
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);
78 void
79 FsmChangeState(struct FsmInst *fi, int newstate)
81 fi->state = newstate;
82 if (fi->debug)
83 fi->printdebug(fi, "ChangeState %s",
84 fi->fsm->strState[newstate]);
87 static void
88 FsmExpireTimer(struct FsmTimer *ft)
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);
97 void
98 FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft)
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);
110 void
111 FsmDelTimer(struct FsmTimer *ft, int where)
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);
121 FsmAddTimer(struct FsmTimer *ft,
122 int millisec, int event, void *arg, int where)
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
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;
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;
144 void
145 FsmRestartTimer(struct FsmTimer *ft,
146 int millisec, int event, void *arg, int where)
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
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);