blob: 92e6570b11435e7e4dade7aa433231008c82b294 [file] [log] [blame]
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001/*
2 * finite state machine implementation
3 *
4 * Author Karsten Keil <kkeil@novell.com>
5 *
6 * Thanks to Jan den Ouden
7 * Fritz Elfert
8 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
9 *
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.
13 *
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.
18 *
19 */
20
21#include <linux/kernel.h>
22#include <linux/slab.h>
23#include <linux/module.h>
24#include <linux/string.h>
25#include "fsm.h"
26
27#define FSM_TIMER_DEBUG 0
28
Anton Vasilyev54a6a042017-08-11 15:57:22 +030029int
Karsten Keil1b2b03f2008-07-27 01:54:58 +020030mISDN_FsmNew(struct Fsm *fsm,
Joe Perches475be4d2012-02-19 19:52:38 -080031 struct FsmNode *fnlist, int fncount)
Karsten Keil1b2b03f2008-07-27 01:54:58 +020032{
33 int i;
34
35 fsm->jumpmatrix = kzalloc(sizeof(FSMFNPTR) * fsm->state_count *
Joe Perches475be4d2012-02-19 19:52:38 -080036 fsm->event_count, GFP_KERNEL);
Anton Vasilyev54a6a042017-08-11 15:57:22 +030037 if (fsm->jumpmatrix == NULL)
38 return -ENOMEM;
Karsten Keil1b2b03f2008-07-27 01:54:58 +020039
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
Joe Perches475be4d2012-02-19 19:52:38 -080044 "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);
Karsten Keil1b2b03f2008-07-27 01:54:58 +020047 } else
48 fsm->jumpmatrix[fsm->state_count * fnlist[i].event +
Joe Perches475be4d2012-02-19 19:52:38 -080049 fnlist[i].state] = (FSMFNPTR) fnlist[i].routine;
Anton Vasilyev54a6a042017-08-11 15:57:22 +030050 return 0;
Karsten Keil1b2b03f2008-07-27 01:54:58 +020051}
52EXPORT_SYMBOL(mISDN_FsmNew);
53
54void
55mISDN_FsmFree(struct Fsm *fsm)
56{
57 kfree((void *) fsm->jumpmatrix);
58}
59EXPORT_SYMBOL(mISDN_FsmFree);
60
61int
62mISDN_FsmEvent(struct FsmInst *fi, int event, void *arg)
63{
64 FSMFNPTR r;
65
66 if ((fi->state >= fi->fsm->state_count) ||
67 (event >= fi->fsm->event_count)) {
68 printk(KERN_ERR
Joe Perches475be4d2012-02-19 19:52:38 -080069 "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);
Karsten Keil1b2b03f2008-07-27 01:54:58 +020072 return 1;
73 }
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",
Joe Perches475be4d2012-02-19 19:52:38 -080078 fi->fsm->strState[fi->state],
79 fi->fsm->strEvent[event]);
Karsten Keil1b2b03f2008-07-27 01:54:58 +020080 r(fi, event, arg);
81 return 0;
82 } else {
83 if (fi->debug)
84 fi->printdebug(fi, "State %s Event %s no action",
Joe Perches475be4d2012-02-19 19:52:38 -080085 fi->fsm->strState[fi->state],
86 fi->fsm->strEvent[event]);
Karsten Keil1b2b03f2008-07-27 01:54:58 +020087 return 1;
88 }
89}
90EXPORT_SYMBOL(mISDN_FsmEvent);
91
92void
93mISDN_FsmChangeState(struct FsmInst *fi, int newstate)
94{
95 fi->state = newstate;
96 if (fi->debug)
97 fi->printdebug(fi, "ChangeState %s",
Joe Perches475be4d2012-02-19 19:52:38 -080098 fi->fsm->strState[newstate]);
Karsten Keil1b2b03f2008-07-27 01:54:58 +020099}
100EXPORT_SYMBOL(mISDN_FsmChangeState);
101
102static void
103FsmExpireTimer(struct FsmTimer *ft)
104{
105#if FSM_TIMER_DEBUG
106 if (ft->fi->debug)
107 ft->fi->printdebug(ft->fi, "FsmExpireTimer %lx", (long) ft);
108#endif
109 mISDN_FsmEvent(ft->fi, ft->event, ft->arg);
110}
111
112void
113mISDN_FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft)
114{
115 ft->fi = fi;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200116#if FSM_TIMER_DEBUG
117 if (ft->fi->debug)
118 ft->fi->printdebug(ft->fi, "mISDN_FsmInitTimer %lx", (long) ft);
119#endif
Geliang Tangaff55a32017-03-23 21:15:57 +0800120 setup_timer(&ft->tl, (void *)FsmExpireTimer, (long)ft);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200121}
122EXPORT_SYMBOL(mISDN_FsmInitTimer);
123
124void
125mISDN_FsmDelTimer(struct FsmTimer *ft, int where)
126{
127#if FSM_TIMER_DEBUG
128 if (ft->fi->debug)
129 ft->fi->printdebug(ft->fi, "mISDN_FsmDelTimer %lx %d",
Joe Perches475be4d2012-02-19 19:52:38 -0800130 (long) ft, where);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200131#endif
132 del_timer(&ft->tl);
133}
134EXPORT_SYMBOL(mISDN_FsmDelTimer);
135
136int
137mISDN_FsmAddTimer(struct FsmTimer *ft,
Joe Perches475be4d2012-02-19 19:52:38 -0800138 int millisec, int event, void *arg, int where)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200139{
140
141#if FSM_TIMER_DEBUG
142 if (ft->fi->debug)
143 ft->fi->printdebug(ft->fi, "mISDN_FsmAddTimer %lx %d %d",
Joe Perches475be4d2012-02-19 19:52:38 -0800144 (long) ft, millisec, where);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200145#endif
146
147 if (timer_pending(&ft->tl)) {
148 if (ft->fi->debug) {
149 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -0800150 "mISDN_FsmAddTimer: timer already active!\n");
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200151 ft->fi->printdebug(ft->fi,
Joe Perches475be4d2012-02-19 19:52:38 -0800152 "mISDN_FsmAddTimer already active!");
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200153 }
154 return -1;
155 }
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);
161 return 0;
162}
163EXPORT_SYMBOL(mISDN_FsmAddTimer);
164
165void
166mISDN_FsmRestartTimer(struct FsmTimer *ft,
Joe Perches475be4d2012-02-19 19:52:38 -0800167 int millisec, int event, void *arg, int where)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200168{
169
170#if FSM_TIMER_DEBUG
171 if (ft->fi->debug)
172 ft->fi->printdebug(ft->fi, "mISDN_FsmRestartTimer %lx %d %d",
Joe Perches475be4d2012-02-19 19:52:38 -0800173 (long) ft, millisec, where);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200174#endif
175
176 if (timer_pending(&ft->tl))
177 del_timer(&ft->tl);
178 init_timer(&ft->tl);
179 ft->event = event;
180 ft->arg = arg;
181 ft->tl.expires = jiffies + (millisec * HZ) / 1000;
182 add_timer(&ft->tl);
183}
184EXPORT_SYMBOL(mISDN_FsmRestartTimer);