1 // SPDX-License-Identifier: GPL-2.0-only
3 * finite state machine implementation
5 * Author Karsten Keil <kkeil@novell.com>
7 * Thanks to Jan den Ouden
9 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/string.h>
18 #define FSM_TIMER_DEBUG 0
21 mISDN_FsmNew(struct Fsm
*fsm
,
22 struct FsmNode
*fnlist
, int fncount
)
27 kzalloc(array3_size(sizeof(FSMFNPTR
), fsm
->state_count
,
30 if (fsm
->jumpmatrix
== NULL
)
33 for (i
= 0; i
< fncount
; i
++)
34 if ((fnlist
[i
].state
>= fsm
->state_count
) ||
35 (fnlist
[i
].event
>= fsm
->event_count
)) {
37 "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
);
41 fsm
->jumpmatrix
[fsm
->state_count
* fnlist
[i
].event
+
42 fnlist
[i
].state
] = (FSMFNPTR
) fnlist
[i
].routine
;
45 EXPORT_SYMBOL(mISDN_FsmNew
);
48 mISDN_FsmFree(struct Fsm
*fsm
)
50 kfree((void *) fsm
->jumpmatrix
);
52 EXPORT_SYMBOL(mISDN_FsmFree
);
55 mISDN_FsmEvent(struct FsmInst
*fi
, int event
, void *arg
)
59 if ((fi
->state
>= fi
->fsm
->state_count
) ||
60 (event
>= fi
->fsm
->event_count
)) {
62 "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
);
67 r
= fi
->fsm
->jumpmatrix
[fi
->fsm
->state_count
* event
+ fi
->state
];
70 fi
->printdebug(fi
, "State %s Event %s",
71 fi
->fsm
->strState
[fi
->state
],
72 fi
->fsm
->strEvent
[event
]);
77 fi
->printdebug(fi
, "State %s Event %s no action",
78 fi
->fsm
->strState
[fi
->state
],
79 fi
->fsm
->strEvent
[event
]);
83 EXPORT_SYMBOL(mISDN_FsmEvent
);
86 mISDN_FsmChangeState(struct FsmInst
*fi
, int newstate
)
90 fi
->printdebug(fi
, "ChangeState %s",
91 fi
->fsm
->strState
[newstate
]);
93 EXPORT_SYMBOL(mISDN_FsmChangeState
);
96 FsmExpireTimer(struct timer_list
*t
)
98 struct FsmTimer
*ft
= from_timer(ft
, t
, tl
);
101 ft
->fi
->printdebug(ft
->fi
, "FsmExpireTimer %lx", (long) ft
);
103 mISDN_FsmEvent(ft
->fi
, ft
->event
, ft
->arg
);
107 mISDN_FsmInitTimer(struct FsmInst
*fi
, struct FsmTimer
*ft
)
112 ft
->fi
->printdebug(ft
->fi
, "mISDN_FsmInitTimer %lx", (long) ft
);
114 timer_setup(&ft
->tl
, FsmExpireTimer
, 0);
116 EXPORT_SYMBOL(mISDN_FsmInitTimer
);
119 mISDN_FsmDelTimer(struct FsmTimer
*ft
, int where
)
123 ft
->fi
->printdebug(ft
->fi
, "mISDN_FsmDelTimer %lx %d",
128 EXPORT_SYMBOL(mISDN_FsmDelTimer
);
131 mISDN_FsmAddTimer(struct FsmTimer
*ft
,
132 int millisec
, int event
, void *arg
, int where
)
137 ft
->fi
->printdebug(ft
->fi
, "mISDN_FsmAddTimer %lx %d %d",
138 (long) ft
, millisec
, where
);
141 if (timer_pending(&ft
->tl
)) {
144 "mISDN_FsmAddTimer: timer already active!\n");
145 ft
->fi
->printdebug(ft
->fi
,
146 "mISDN_FsmAddTimer already active!");
152 ft
->tl
.expires
= jiffies
+ (millisec
* HZ
) / 1000;
156 EXPORT_SYMBOL(mISDN_FsmAddTimer
);
159 mISDN_FsmRestartTimer(struct FsmTimer
*ft
,
160 int millisec
, int event
, void *arg
, int where
)
165 ft
->fi
->printdebug(ft
->fi
, "mISDN_FsmRestartTimer %lx %d %d",
166 (long) ft
, millisec
, where
);
169 if (timer_pending(&ft
->tl
))
173 ft
->tl
.expires
= jiffies
+ (millisec
* HZ
) / 1000;
176 EXPORT_SYMBOL(mISDN_FsmRestartTimer
);