Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / isdn / hisax / fsm.c
blob34fade96a581248834c27c6aa9ebf4f0c11ff840
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/init.h>
19 #include "hisax.h"
21 #define FSM_TIMER_DEBUG 0
23 int
24 FsmNew(struct Fsm *fsm, struct FsmNode *fnlist, int fncount)
26 int i;
28 fsm->jumpmatrix = (FSMFNPTR *)
29 kzalloc(sizeof (FSMFNPTR) * fsm->state_count * fsm->event_count, GFP_KERNEL);
30 if (!fsm->jumpmatrix)
31 return -ENOMEM;
33 for (i = 0; i < fncount; i++)
34 if ((fnlist[i].state>=fsm->state_count) || (fnlist[i].event>=fsm->event_count)) {
35 printk(KERN_ERR "FsmNew Error line %d st(%ld/%ld) ev(%ld/%ld)\n",
36 i,(long)fnlist[i].state,(long)fsm->state_count,
37 (long)fnlist[i].event,(long)fsm->event_count);
38 } else
39 fsm->jumpmatrix[fsm->state_count * fnlist[i].event +
40 fnlist[i].state] = (FSMFNPTR) fnlist[i].routine;
41 return 0;
44 void
45 FsmFree(struct Fsm *fsm)
47 kfree((void *) fsm->jumpmatrix);
50 int
51 FsmEvent(struct FsmInst *fi, int event, void *arg)
53 FSMFNPTR r;
55 if ((fi->state>=fi->fsm->state_count) || (event >= fi->fsm->event_count)) {
56 printk(KERN_ERR "FsmEvent Error st(%ld/%ld) ev(%d/%ld)\n",
57 (long)fi->state,(long)fi->fsm->state_count,event,(long)fi->fsm->event_count);
58 return(1);
60 r = fi->fsm->jumpmatrix[fi->fsm->state_count * event + fi->state];
61 if (r) {
62 if (fi->debug)
63 fi->printdebug(fi, "State %s Event %s",
64 fi->fsm->strState[fi->state],
65 fi->fsm->strEvent[event]);
66 r(fi, event, arg);
67 return (0);
68 } else {
69 if (fi->debug)
70 fi->printdebug(fi, "State %s Event %s no routine",
71 fi->fsm->strState[fi->state],
72 fi->fsm->strEvent[event]);
73 return (!0);
77 void
78 FsmChangeState(struct FsmInst *fi, int newstate)
80 fi->state = newstate;
81 if (fi->debug)
82 fi->printdebug(fi, "ChangeState %s",
83 fi->fsm->strState[newstate]);
86 static void
87 FsmExpireTimer(struct FsmTimer *ft)
89 #if FSM_TIMER_DEBUG
90 if (ft->fi->debug)
91 ft->fi->printdebug(ft->fi, "FsmExpireTimer %lx", (long) ft);
92 #endif
93 FsmEvent(ft->fi, ft->event, ft->arg);
96 void
97 FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft)
99 ft->fi = fi;
100 ft->tl.function = (void *) FsmExpireTimer;
101 ft->tl.data = (long) ft;
102 #if FSM_TIMER_DEBUG
103 if (ft->fi->debug)
104 ft->fi->printdebug(ft->fi, "FsmInitTimer %lx", (long) ft);
105 #endif
106 init_timer(&ft->tl);
109 void
110 FsmDelTimer(struct FsmTimer *ft, int where)
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);
120 FsmAddTimer(struct FsmTimer *ft,
121 int millisec, int event, void *arg, int where)
124 #if FSM_TIMER_DEBUG
125 if (ft->fi->debug)
126 ft->fi->printdebug(ft->fi, "FsmAddTimer %lx %d %d",
127 (long) ft, millisec, where);
128 #endif
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;
135 init_timer(&ft->tl);
136 ft->event = event;
137 ft->arg = arg;
138 ft->tl.expires = jiffies + (millisec * HZ) / 1000;
139 add_timer(&ft->tl);
140 return 0;
143 void
144 FsmRestartTimer(struct FsmTimer *ft,
145 int millisec, int event, void *arg, int where)
148 #if FSM_TIMER_DEBUG
149 if (ft->fi->debug)
150 ft->fi->printdebug(ft->fi, "FsmRestartTimer %lx %d %d",
151 (long) ft, millisec, where);
152 #endif
154 if (timer_pending(&ft->tl))
155 del_timer(&ft->tl);
156 init_timer(&ft->tl);
157 ft->event = event;
158 ft->arg = arg;
159 ft->tl.expires = jiffies + (millisec * HZ) / 1000;
160 add_timer(&ft->tl);