xtensa: fix high memory/reserved memory collision
[cris-mirror.git] / drivers / isdn / mISDN / fsm.c
blobcabcb906e0b564a014d4be7941f40acc7792e131
1 /*
2 * finite state machine implementation
4 * Author Karsten Keil <kkeil@novell.com>
6 * Thanks to Jan den Ouden
7 * Fritz Elfert
8 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <linux/string.h>
25 #include "fsm.h"
27 #define FSM_TIMER_DEBUG 0
29 int
30 mISDN_FsmNew(struct Fsm *fsm,
31 struct FsmNode *fnlist, int fncount)
33 int i;
35 fsm->jumpmatrix = kzalloc(sizeof(FSMFNPTR) * fsm->state_count *
36 fsm->event_count, GFP_KERNEL);
37 if (fsm->jumpmatrix == NULL)
38 return -ENOMEM;
40 for (i = 0; i < fncount; i++)
41 if ((fnlist[i].state >= fsm->state_count) ||
42 (fnlist[i].event >= fsm->event_count)) {
43 printk(KERN_ERR
44 "mISDN_FsmNew Error: %d st(%ld/%ld) ev(%ld/%ld)\n",
45 i, (long)fnlist[i].state, (long)fsm->state_count,
46 (long)fnlist[i].event, (long)fsm->event_count);
47 } else
48 fsm->jumpmatrix[fsm->state_count * fnlist[i].event +
49 fnlist[i].state] = (FSMFNPTR) fnlist[i].routine;
50 return 0;
52 EXPORT_SYMBOL(mISDN_FsmNew);
54 void
55 mISDN_FsmFree(struct Fsm *fsm)
57 kfree((void *) fsm->jumpmatrix);
59 EXPORT_SYMBOL(mISDN_FsmFree);
61 int
62 mISDN_FsmEvent(struct FsmInst *fi, int event, void *arg)
64 FSMFNPTR r;
66 if ((fi->state >= fi->fsm->state_count) ||
67 (event >= fi->fsm->event_count)) {
68 printk(KERN_ERR
69 "mISDN_FsmEvent Error st(%ld/%ld) ev(%d/%ld)\n",
70 (long)fi->state, (long)fi->fsm->state_count, event,
71 (long)fi->fsm->event_count);
72 return 1;
74 r = fi->fsm->jumpmatrix[fi->fsm->state_count * event + fi->state];
75 if (r) {
76 if (fi->debug)
77 fi->printdebug(fi, "State %s Event %s",
78 fi->fsm->strState[fi->state],
79 fi->fsm->strEvent[event]);
80 r(fi, event, arg);
81 return 0;
82 } else {
83 if (fi->debug)
84 fi->printdebug(fi, "State %s Event %s no action",
85 fi->fsm->strState[fi->state],
86 fi->fsm->strEvent[event]);
87 return 1;
90 EXPORT_SYMBOL(mISDN_FsmEvent);
92 void
93 mISDN_FsmChangeState(struct FsmInst *fi, int newstate)
95 fi->state = newstate;
96 if (fi->debug)
97 fi->printdebug(fi, "ChangeState %s",
98 fi->fsm->strState[newstate]);
100 EXPORT_SYMBOL(mISDN_FsmChangeState);
102 static void
103 FsmExpireTimer(struct timer_list *t)
105 struct FsmTimer *ft = from_timer(ft, t, tl);
106 #if FSM_TIMER_DEBUG
107 if (ft->fi->debug)
108 ft->fi->printdebug(ft->fi, "FsmExpireTimer %lx", (long) ft);
109 #endif
110 mISDN_FsmEvent(ft->fi, ft->event, ft->arg);
113 void
114 mISDN_FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft)
116 ft->fi = fi;
117 #if FSM_TIMER_DEBUG
118 if (ft->fi->debug)
119 ft->fi->printdebug(ft->fi, "mISDN_FsmInitTimer %lx", (long) ft);
120 #endif
121 timer_setup(&ft->tl, FsmExpireTimer, 0);
123 EXPORT_SYMBOL(mISDN_FsmInitTimer);
125 void
126 mISDN_FsmDelTimer(struct FsmTimer *ft, int where)
128 #if FSM_TIMER_DEBUG
129 if (ft->fi->debug)
130 ft->fi->printdebug(ft->fi, "mISDN_FsmDelTimer %lx %d",
131 (long) ft, where);
132 #endif
133 del_timer(&ft->tl);
135 EXPORT_SYMBOL(mISDN_FsmDelTimer);
138 mISDN_FsmAddTimer(struct FsmTimer *ft,
139 int millisec, int event, void *arg, int where)
142 #if FSM_TIMER_DEBUG
143 if (ft->fi->debug)
144 ft->fi->printdebug(ft->fi, "mISDN_FsmAddTimer %lx %d %d",
145 (long) ft, millisec, where);
146 #endif
148 if (timer_pending(&ft->tl)) {
149 if (ft->fi->debug) {
150 printk(KERN_WARNING
151 "mISDN_FsmAddTimer: timer already active!\n");
152 ft->fi->printdebug(ft->fi,
153 "mISDN_FsmAddTimer already active!");
155 return -1;
157 ft->event = event;
158 ft->arg = arg;
159 ft->tl.expires = jiffies + (millisec * HZ) / 1000;
160 add_timer(&ft->tl);
161 return 0;
163 EXPORT_SYMBOL(mISDN_FsmAddTimer);
165 void
166 mISDN_FsmRestartTimer(struct FsmTimer *ft,
167 int millisec, int event, void *arg, int where)
170 #if FSM_TIMER_DEBUG
171 if (ft->fi->debug)
172 ft->fi->printdebug(ft->fi, "mISDN_FsmRestartTimer %lx %d %d",
173 (long) ft, millisec, where);
174 #endif
176 if (timer_pending(&ft->tl))
177 del_timer(&ft->tl);
178 ft->event = event;
179 ft->arg = arg;
180 ft->tl.expires = jiffies + (millisec * HZ) / 1000;
181 add_timer(&ft->tl);
183 EXPORT_SYMBOL(mISDN_FsmRestartTimer);