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