Thomas Gleixner | 1802d0b | 2019-05-27 08:55:21 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 2 | /* |
| 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 Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 10 | */ |
| 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 Vasilyev | 54a6a04 | 2017-08-11 15:57:22 +0300 | [diff] [blame] | 20 | int |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 21 | mISDN_FsmNew(struct Fsm *fsm, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 22 | struct FsmNode *fnlist, int fncount) |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 23 | { |
| 24 | int i; |
| 25 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 26 | fsm->jumpmatrix = |
| 27 | kzalloc(array3_size(sizeof(FSMFNPTR), fsm->state_count, |
| 28 | fsm->event_count), |
| 29 | GFP_KERNEL); |
Anton Vasilyev | 54a6a04 | 2017-08-11 15:57:22 +0300 | [diff] [blame] | 30 | if (fsm->jumpmatrix == NULL) |
| 31 | return -ENOMEM; |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 32 | |
| 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 Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 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); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 40 | } else |
| 41 | fsm->jumpmatrix[fsm->state_count * fnlist[i].event + |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 42 | fnlist[i].state] = (FSMFNPTR) fnlist[i].routine; |
Anton Vasilyev | 54a6a04 | 2017-08-11 15:57:22 +0300 | [diff] [blame] | 43 | return 0; |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 44 | } |
| 45 | EXPORT_SYMBOL(mISDN_FsmNew); |
| 46 | |
| 47 | void |
| 48 | mISDN_FsmFree(struct Fsm *fsm) |
| 49 | { |
| 50 | kfree((void *) fsm->jumpmatrix); |
| 51 | } |
| 52 | EXPORT_SYMBOL(mISDN_FsmFree); |
| 53 | |
| 54 | int |
| 55 | mISDN_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 Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 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); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 65 | 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 Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 71 | fi->fsm->strState[fi->state], |
| 72 | fi->fsm->strEvent[event]); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 73 | r(fi, event, arg); |
| 74 | return 0; |
| 75 | } else { |
| 76 | if (fi->debug) |
| 77 | fi->printdebug(fi, "State %s Event %s no action", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 78 | fi->fsm->strState[fi->state], |
| 79 | fi->fsm->strEvent[event]); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 80 | return 1; |
| 81 | } |
| 82 | } |
| 83 | EXPORT_SYMBOL(mISDN_FsmEvent); |
| 84 | |
| 85 | void |
| 86 | mISDN_FsmChangeState(struct FsmInst *fi, int newstate) |
| 87 | { |
| 88 | fi->state = newstate; |
| 89 | if (fi->debug) |
| 90 | fi->printdebug(fi, "ChangeState %s", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 91 | fi->fsm->strState[newstate]); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 92 | } |
| 93 | EXPORT_SYMBOL(mISDN_FsmChangeState); |
| 94 | |
| 95 | static void |
Kees Cook | e313ac1 | 2017-10-16 17:29:14 -0700 | [diff] [blame] | 96 | FsmExpireTimer(struct timer_list *t) |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 97 | { |
Kees Cook | e313ac1 | 2017-10-16 17:29:14 -0700 | [diff] [blame] | 98 | struct FsmTimer *ft = from_timer(ft, t, tl); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 99 | #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 | |
| 106 | void |
| 107 | mISDN_FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft) |
| 108 | { |
| 109 | ft->fi = fi; |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 110 | #if FSM_TIMER_DEBUG |
| 111 | if (ft->fi->debug) |
| 112 | ft->fi->printdebug(ft->fi, "mISDN_FsmInitTimer %lx", (long) ft); |
| 113 | #endif |
Kees Cook | e313ac1 | 2017-10-16 17:29:14 -0700 | [diff] [blame] | 114 | timer_setup(&ft->tl, FsmExpireTimer, 0); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 115 | } |
| 116 | EXPORT_SYMBOL(mISDN_FsmInitTimer); |
| 117 | |
| 118 | void |
| 119 | mISDN_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 Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 124 | (long) ft, where); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 125 | #endif |
| 126 | del_timer(&ft->tl); |
| 127 | } |
| 128 | EXPORT_SYMBOL(mISDN_FsmDelTimer); |
| 129 | |
| 130 | int |
| 131 | mISDN_FsmAddTimer(struct FsmTimer *ft, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 132 | int millisec, int event, void *arg, int where) |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 133 | { |
| 134 | |
| 135 | #if FSM_TIMER_DEBUG |
| 136 | if (ft->fi->debug) |
| 137 | ft->fi->printdebug(ft->fi, "mISDN_FsmAddTimer %lx %d %d", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 138 | (long) ft, millisec, where); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 139 | #endif |
| 140 | |
| 141 | if (timer_pending(&ft->tl)) { |
| 142 | if (ft->fi->debug) { |
| 143 | printk(KERN_WARNING |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 144 | "mISDN_FsmAddTimer: timer already active!\n"); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 145 | ft->fi->printdebug(ft->fi, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 146 | "mISDN_FsmAddTimer already active!"); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 147 | } |
| 148 | return -1; |
| 149 | } |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 150 | ft->event = event; |
| 151 | ft->arg = arg; |
| 152 | ft->tl.expires = jiffies + (millisec * HZ) / 1000; |
| 153 | add_timer(&ft->tl); |
| 154 | return 0; |
| 155 | } |
| 156 | EXPORT_SYMBOL(mISDN_FsmAddTimer); |
| 157 | |
| 158 | void |
| 159 | mISDN_FsmRestartTimer(struct FsmTimer *ft, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 160 | int millisec, int event, void *arg, int where) |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 161 | { |
| 162 | |
| 163 | #if FSM_TIMER_DEBUG |
| 164 | if (ft->fi->debug) |
| 165 | ft->fi->printdebug(ft->fi, "mISDN_FsmRestartTimer %lx %d %d", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 166 | (long) ft, millisec, where); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 167 | #endif |
| 168 | |
| 169 | if (timer_pending(&ft->tl)) |
| 170 | del_timer(&ft->tl); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 171 | ft->event = event; |
| 172 | ft->arg = arg; |
| 173 | ft->tl.expires = jiffies + (millisec * HZ) / 1000; |
| 174 | add_timer(&ft->tl); |
| 175 | } |
| 176 | EXPORT_SYMBOL(mISDN_FsmRestartTimer); |