blob: 7c5c2ca6c6d8877547a0064261cbe763ebdab931 [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Karsten Keil1b2b03f2008-07-27 01:54:58 +02002/*
3 * finite state machine implementation
4 *
5 * Author Karsten Keil <kkeil@novell.com>
6 *
7 * Thanks to Jan den Ouden
8 * Fritz Elfert
9 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
Karsten Keil1b2b03f2008-07-27 01:54:58 +020010 */
11
12#include <linux/kernel.h>
13#include <linux/slab.h>
14#include <linux/module.h>
15#include <linux/string.h>
16#include "fsm.h"
17
18#define FSM_TIMER_DEBUG 0
19
Anton Vasilyev54a6a042017-08-11 15:57:22 +030020int
Karsten Keil1b2b03f2008-07-27 01:54:58 +020021mISDN_FsmNew(struct Fsm *fsm,
Joe Perches475be4d2012-02-19 19:52:38 -080022 struct FsmNode *fnlist, int fncount)
Karsten Keil1b2b03f2008-07-27 01:54:58 +020023{
24 int i;
25
Kees Cook6396bb22018-06-12 14:03:40 -070026 fsm->jumpmatrix =
27 kzalloc(array3_size(sizeof(FSMFNPTR), fsm->state_count,
28 fsm->event_count),
29 GFP_KERNEL);
Anton Vasilyev54a6a042017-08-11 15:57:22 +030030 if (fsm->jumpmatrix == NULL)
31 return -ENOMEM;
Karsten Keil1b2b03f2008-07-27 01:54:58 +020032
33 for (i = 0; i < fncount; i++)
34 if ((fnlist[i].state >= fsm->state_count) ||
35 (fnlist[i].event >= fsm->event_count)) {
36 printk(KERN_ERR
Joe Perches475be4d2012-02-19 19:52:38 -080037 "mISDN_FsmNew Error: %d st(%ld/%ld) ev(%ld/%ld)\n",
38 i, (long)fnlist[i].state, (long)fsm->state_count,
39 (long)fnlist[i].event, (long)fsm->event_count);
Karsten Keil1b2b03f2008-07-27 01:54:58 +020040 } else
41 fsm->jumpmatrix[fsm->state_count * fnlist[i].event +
Joe Perches475be4d2012-02-19 19:52:38 -080042 fnlist[i].state] = (FSMFNPTR) fnlist[i].routine;
Anton Vasilyev54a6a042017-08-11 15:57:22 +030043 return 0;
Karsten Keil1b2b03f2008-07-27 01:54:58 +020044}
45EXPORT_SYMBOL(mISDN_FsmNew);
46
47void
48mISDN_FsmFree(struct Fsm *fsm)
49{
50 kfree((void *) fsm->jumpmatrix);
51}
52EXPORT_SYMBOL(mISDN_FsmFree);
53
54int
55mISDN_FsmEvent(struct FsmInst *fi, int event, void *arg)
56{
57 FSMFNPTR r;
58
59 if ((fi->state >= fi->fsm->state_count) ||
60 (event >= fi->fsm->event_count)) {
61 printk(KERN_ERR
Joe Perches475be4d2012-02-19 19:52:38 -080062 "mISDN_FsmEvent Error st(%ld/%ld) ev(%d/%ld)\n",
63 (long)fi->state, (long)fi->fsm->state_count, event,
64 (long)fi->fsm->event_count);
Karsten Keil1b2b03f2008-07-27 01:54:58 +020065 return 1;
66 }
67 r = fi->fsm->jumpmatrix[fi->fsm->state_count * event + fi->state];
68 if (r) {
69 if (fi->debug)
70 fi->printdebug(fi, "State %s Event %s",
Joe Perches475be4d2012-02-19 19:52:38 -080071 fi->fsm->strState[fi->state],
72 fi->fsm->strEvent[event]);
Karsten Keil1b2b03f2008-07-27 01:54:58 +020073 r(fi, event, arg);
74 return 0;
75 } else {
76 if (fi->debug)
77 fi->printdebug(fi, "State %s Event %s no action",
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 return 1;
81 }
82}
83EXPORT_SYMBOL(mISDN_FsmEvent);
84
85void
86mISDN_FsmChangeState(struct FsmInst *fi, int newstate)
87{
88 fi->state = newstate;
89 if (fi->debug)
90 fi->printdebug(fi, "ChangeState %s",
Joe Perches475be4d2012-02-19 19:52:38 -080091 fi->fsm->strState[newstate]);
Karsten Keil1b2b03f2008-07-27 01:54:58 +020092}
93EXPORT_SYMBOL(mISDN_FsmChangeState);
94
95static void
Kees Cooke313ac12017-10-16 17:29:14 -070096FsmExpireTimer(struct timer_list *t)
Karsten Keil1b2b03f2008-07-27 01:54:58 +020097{
Kees Cooke313ac12017-10-16 17:29:14 -070098 struct FsmTimer *ft = from_timer(ft, t, tl);
Karsten Keil1b2b03f2008-07-27 01:54:58 +020099#if FSM_TIMER_DEBUG
100 if (ft->fi->debug)
101 ft->fi->printdebug(ft->fi, "FsmExpireTimer %lx", (long) ft);
102#endif
103 mISDN_FsmEvent(ft->fi, ft->event, ft->arg);
104}
105
106void
107mISDN_FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft)
108{
109 ft->fi = fi;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200110#if FSM_TIMER_DEBUG
111 if (ft->fi->debug)
112 ft->fi->printdebug(ft->fi, "mISDN_FsmInitTimer %lx", (long) ft);
113#endif
Kees Cooke313ac12017-10-16 17:29:14 -0700114 timer_setup(&ft->tl, FsmExpireTimer, 0);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200115}
116EXPORT_SYMBOL(mISDN_FsmInitTimer);
117
118void
119mISDN_FsmDelTimer(struct FsmTimer *ft, int where)
120{
121#if FSM_TIMER_DEBUG
122 if (ft->fi->debug)
123 ft->fi->printdebug(ft->fi, "mISDN_FsmDelTimer %lx %d",
Joe Perches475be4d2012-02-19 19:52:38 -0800124 (long) ft, where);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200125#endif
126 del_timer(&ft->tl);
127}
128EXPORT_SYMBOL(mISDN_FsmDelTimer);
129
130int
131mISDN_FsmAddTimer(struct FsmTimer *ft,
Joe Perches475be4d2012-02-19 19:52:38 -0800132 int millisec, int event, void *arg, int where)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200133{
134
135#if FSM_TIMER_DEBUG
136 if (ft->fi->debug)
137 ft->fi->printdebug(ft->fi, "mISDN_FsmAddTimer %lx %d %d",
Joe Perches475be4d2012-02-19 19:52:38 -0800138 (long) ft, millisec, where);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200139#endif
140
141 if (timer_pending(&ft->tl)) {
142 if (ft->fi->debug) {
143 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -0800144 "mISDN_FsmAddTimer: timer already active!\n");
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200145 ft->fi->printdebug(ft->fi,
Joe Perches475be4d2012-02-19 19:52:38 -0800146 "mISDN_FsmAddTimer already active!");
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200147 }
148 return -1;
149 }
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200150 ft->event = event;
151 ft->arg = arg;
152 ft->tl.expires = jiffies + (millisec * HZ) / 1000;
153 add_timer(&ft->tl);
154 return 0;
155}
156EXPORT_SYMBOL(mISDN_FsmAddTimer);
157
158void
159mISDN_FsmRestartTimer(struct FsmTimer *ft,
Joe Perches475be4d2012-02-19 19:52:38 -0800160 int millisec, int event, void *arg, int where)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200161{
162
163#if FSM_TIMER_DEBUG
164 if (ft->fi->debug)
165 ft->fi->printdebug(ft->fi, "mISDN_FsmRestartTimer %lx %d %d",
Joe Perches475be4d2012-02-19 19:52:38 -0800166 (long) ft, millisec, where);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200167#endif
168
169 if (timer_pending(&ft->tl))
170 del_timer(&ft->tl);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200171 ft->event = event;
172 ft->arg = arg;
173 ft->tl.expires = jiffies + (millisec * HZ) / 1000;
174 add_timer(&ft->tl);
175}
176EXPORT_SYMBOL(mISDN_FsmRestartTimer);