^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * finite state machine implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Author Karsten Keil <kkeil@novell.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Thanks to Jan den Ouden
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Fritz Elfert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright 2008 by Karsten Keil <kkeil@novell.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "fsm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define FSM_TIMER_DEBUG 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) mISDN_FsmNew(struct Fsm *fsm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct FsmNode *fnlist, int fncount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) fsm->jumpmatrix =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) kzalloc(array3_size(sizeof(FSMFNPTR), fsm->state_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) fsm->event_count),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (fsm->jumpmatrix == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) for (i = 0; i < fncount; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if ((fnlist[i].state >= fsm->state_count) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) (fnlist[i].event >= fsm->event_count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) printk(KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) "mISDN_FsmNew Error: %d st(%ld/%ld) ev(%ld/%ld)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) i, (long)fnlist[i].state, (long)fsm->state_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) (long)fnlist[i].event, (long)fsm->event_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) fsm->jumpmatrix[fsm->state_count * fnlist[i].event +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) fnlist[i].state] = (FSMFNPTR) fnlist[i].routine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) EXPORT_SYMBOL(mISDN_FsmNew);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) mISDN_FsmFree(struct Fsm *fsm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) kfree((void *) fsm->jumpmatrix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) EXPORT_SYMBOL(mISDN_FsmFree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) mISDN_FsmEvent(struct FsmInst *fi, int event, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) FSMFNPTR r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if ((fi->state >= fi->fsm->state_count) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) (event >= fi->fsm->event_count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) printk(KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) "mISDN_FsmEvent Error st(%ld/%ld) ev(%d/%ld)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) (long)fi->state, (long)fi->fsm->state_count, event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) (long)fi->fsm->event_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) r = fi->fsm->jumpmatrix[fi->fsm->state_count * event + fi->state];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (fi->debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) fi->printdebug(fi, "State %s Event %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) fi->fsm->strState[fi->state],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) fi->fsm->strEvent[event]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) r(fi, event, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (fi->debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) fi->printdebug(fi, "State %s Event %s no action",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) fi->fsm->strState[fi->state],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) fi->fsm->strEvent[event]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) EXPORT_SYMBOL(mISDN_FsmEvent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) mISDN_FsmChangeState(struct FsmInst *fi, int newstate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) fi->state = newstate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (fi->debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) fi->printdebug(fi, "ChangeState %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) fi->fsm->strState[newstate]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) EXPORT_SYMBOL(mISDN_FsmChangeState);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) FsmExpireTimer(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct FsmTimer *ft = from_timer(ft, t, tl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #if FSM_TIMER_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (ft->fi->debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) ft->fi->printdebug(ft->fi, "FsmExpireTimer %lx", (long) ft);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) mISDN_FsmEvent(ft->fi, ft->event, ft->arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) mISDN_FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) ft->fi = fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #if FSM_TIMER_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (ft->fi->debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) ft->fi->printdebug(ft->fi, "mISDN_FsmInitTimer %lx", (long) ft);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) timer_setup(&ft->tl, FsmExpireTimer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) EXPORT_SYMBOL(mISDN_FsmInitTimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) mISDN_FsmDelTimer(struct FsmTimer *ft, int where)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #if FSM_TIMER_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (ft->fi->debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) ft->fi->printdebug(ft->fi, "mISDN_FsmDelTimer %lx %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) (long) ft, where);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) del_timer(&ft->tl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) EXPORT_SYMBOL(mISDN_FsmDelTimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) mISDN_FsmAddTimer(struct FsmTimer *ft,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) int millisec, int event, void *arg, int where)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) #if FSM_TIMER_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (ft->fi->debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) ft->fi->printdebug(ft->fi, "mISDN_FsmAddTimer %lx %d %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) (long) ft, millisec, where);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (timer_pending(&ft->tl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (ft->fi->debug) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) printk(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) "mISDN_FsmAddTimer: timer already active!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) ft->fi->printdebug(ft->fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) "mISDN_FsmAddTimer already active!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) ft->event = event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) ft->arg = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) ft->tl.expires = jiffies + (millisec * HZ) / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) add_timer(&ft->tl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) EXPORT_SYMBOL(mISDN_FsmAddTimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) mISDN_FsmRestartTimer(struct FsmTimer *ft,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int millisec, int event, void *arg, int where)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) #if FSM_TIMER_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (ft->fi->debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) ft->fi->printdebug(ft->fi, "mISDN_FsmRestartTimer %lx %d %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) (long) ft, millisec, where);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (timer_pending(&ft->tl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) del_timer(&ft->tl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) ft->event = event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) ft->arg = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) ft->tl.expires = jiffies + (millisec * HZ) / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) add_timer(&ft->tl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) EXPORT_SYMBOL(mISDN_FsmRestartTimer);