Merge tag 'iomap-4.21-merge-3' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[linux/fpc-iii.git] / drivers / isdn / hisax / fsm.c
blob80ba82f77c63d02c0395fdbb17ed0a5b039100bc
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>
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 =
30 kzalloc(array3_size(sizeof(FSMFNPTR), fsm->state_count,
31 fsm->event_count),
32 GFP_KERNEL);
33 if (!fsm->jumpmatrix)
34 return -ENOMEM;
36 for (i = 0; i < fncount; i++)
37 if ((fnlist[i].state >= fsm->state_count) || (fnlist[i].event >= fsm->event_count)) {
38 printk(KERN_ERR "FsmNew Error line %d st(%ld/%ld) ev(%ld/%ld)\n",
39 i, (long)fnlist[i].state, (long)fsm->state_count,
40 (long)fnlist[i].event, (long)fsm->event_count);
41 } else
42 fsm->jumpmatrix[fsm->state_count * fnlist[i].event +
43 fnlist[i].state] = (FSMFNPTR)fnlist[i].routine;
44 return 0;
47 void
48 FsmFree(struct Fsm *fsm)
50 kfree((void *) fsm->jumpmatrix);
53 int
54 FsmEvent(struct FsmInst *fi, int event, void *arg)
56 FSMFNPTR r;
58 if ((fi->state >= fi->fsm->state_count) || (event >= fi->fsm->event_count)) {
59 printk(KERN_ERR "FsmEvent Error st(%ld/%ld) ev(%d/%ld)\n",
60 (long)fi->state, (long)fi->fsm->state_count, event, (long)fi->fsm->event_count);
61 return (1);
63 r = fi->fsm->jumpmatrix[fi->fsm->state_count * event + fi->state];
64 if (r) {
65 if (fi->debug)
66 fi->printdebug(fi, "State %s Event %s",
67 fi->fsm->strState[fi->state],
68 fi->fsm->strEvent[event]);
69 r(fi, event, arg);
70 return (0);
71 } else {
72 if (fi->debug)
73 fi->printdebug(fi, "State %s Event %s no routine",
74 fi->fsm->strState[fi->state],
75 fi->fsm->strEvent[event]);
76 return (!0);
80 void
81 FsmChangeState(struct FsmInst *fi, int newstate)
83 fi->state = newstate;
84 if (fi->debug)
85 fi->printdebug(fi, "ChangeState %s",
86 fi->fsm->strState[newstate]);
89 static void
90 FsmExpireTimer(struct timer_list *t)
92 struct FsmTimer *ft = from_timer(ft, t, tl);
93 #if FSM_TIMER_DEBUG
94 if (ft->fi->debug)
95 ft->fi->printdebug(ft->fi, "FsmExpireTimer %lx", (long) ft);
96 #endif
97 FsmEvent(ft->fi, ft->event, ft->arg);
100 void
101 FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft)
103 ft->fi = fi;
104 #if FSM_TIMER_DEBUG
105 if (ft->fi->debug)
106 ft->fi->printdebug(ft->fi, "FsmInitTimer %lx", (long) ft);
107 #endif
108 timer_setup(&ft->tl, FsmExpireTimer, 0);
111 void
112 FsmDelTimer(struct FsmTimer *ft, int where)
114 #if FSM_TIMER_DEBUG
115 if (ft->fi->debug)
116 ft->fi->printdebug(ft->fi, "FsmDelTimer %lx %d", (long) ft, where);
117 #endif
118 del_timer(&ft->tl);
122 FsmAddTimer(struct FsmTimer *ft,
123 int millisec, int event, void *arg, int where)
126 #if FSM_TIMER_DEBUG
127 if (ft->fi->debug)
128 ft->fi->printdebug(ft->fi, "FsmAddTimer %lx %d %d",
129 (long) ft, millisec, where);
130 #endif
132 if (timer_pending(&ft->tl)) {
133 printk(KERN_WARNING "FsmAddTimer: timer already active!\n");
134 ft->fi->printdebug(ft->fi, "FsmAddTimer already active!");
135 return -1;
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 ft->event = event;
158 ft->arg = arg;
159 ft->tl.expires = jiffies + (millisec * HZ) / 1000;
160 add_timer(&ft->tl);