Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* $Id: fsm.c,v 1.14.6.4 2001/09/23 22:24:47 kai Exp $ |
| 2 | * |
| 3 | * Finite state machine |
| 4 | * |
| 5 | * Author Karsten Keil |
| 6 | * Copyright by Karsten Keil <keil@isdn4linux.de> |
| 7 | * by Kai Germaschewski <kai.germaschewski@gmx.de> |
| 8 | * |
| 9 | * This software may be used and distributed according to the terms |
| 10 | * of the GNU General Public License, incorporated herein by reference. |
| 11 | * |
| 12 | * Thanks to Jan den Ouden |
| 13 | * Fritz Elfert |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/init.h> |
| 19 | #include "hisax.h" |
| 20 | |
| 21 | #define FSM_TIMER_DEBUG 0 |
| 22 | |
| 23 | int |
| 24 | FsmNew(struct Fsm *fsm, struct FsmNode *fnlist, int fncount) |
| 25 | { |
| 26 | int i; |
| 27 | |
| 28 | fsm->jumpmatrix = (FSMFNPTR *) |
| 29 | kmalloc(sizeof (FSMFNPTR) * fsm->state_count * fsm->event_count, GFP_KERNEL); |
| 30 | if (!fsm->jumpmatrix) |
| 31 | return -ENOMEM; |
| 32 | |
| 33 | memset(fsm->jumpmatrix, 0, sizeof (FSMFNPTR) * fsm->state_count * fsm->event_count); |
| 34 | |
| 35 | for (i = 0; i < fncount; i++) |
| 36 | if ((fnlist[i].state>=fsm->state_count) || (fnlist[i].event>=fsm->event_count)) { |
| 37 | printk(KERN_ERR "FsmNew Error line %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); |
| 40 | } else |
| 41 | fsm->jumpmatrix[fsm->state_count * fnlist[i].event + |
| 42 | fnlist[i].state] = (FSMFNPTR) fnlist[i].routine; |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | FsmFree(struct Fsm *fsm) |
| 48 | { |
| 49 | kfree((void *) fsm->jumpmatrix); |
| 50 | } |
| 51 | |
| 52 | int |
| 53 | FsmEvent(struct FsmInst *fi, int event, void *arg) |
| 54 | { |
| 55 | FSMFNPTR r; |
| 56 | |
| 57 | if ((fi->state>=fi->fsm->state_count) || (event >= fi->fsm->event_count)) { |
| 58 | printk(KERN_ERR "FsmEvent Error st(%ld/%ld) ev(%d/%ld)\n", |
| 59 | (long)fi->state,(long)fi->fsm->state_count,event,(long)fi->fsm->event_count); |
| 60 | return(1); |
| 61 | } |
| 62 | r = fi->fsm->jumpmatrix[fi->fsm->state_count * event + fi->state]; |
| 63 | if (r) { |
| 64 | if (fi->debug) |
| 65 | fi->printdebug(fi, "State %s Event %s", |
| 66 | fi->fsm->strState[fi->state], |
| 67 | fi->fsm->strEvent[event]); |
| 68 | r(fi, event, arg); |
| 69 | return (0); |
| 70 | } else { |
| 71 | if (fi->debug) |
| 72 | fi->printdebug(fi, "State %s Event %s no routine", |
| 73 | fi->fsm->strState[fi->state], |
| 74 | fi->fsm->strEvent[event]); |
| 75 | return (!0); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | FsmChangeState(struct FsmInst *fi, int newstate) |
| 81 | { |
| 82 | fi->state = newstate; |
| 83 | if (fi->debug) |
| 84 | fi->printdebug(fi, "ChangeState %s", |
| 85 | fi->fsm->strState[newstate]); |
| 86 | } |
| 87 | |
| 88 | static void |
| 89 | FsmExpireTimer(struct FsmTimer *ft) |
| 90 | { |
| 91 | #if FSM_TIMER_DEBUG |
| 92 | if (ft->fi->debug) |
| 93 | ft->fi->printdebug(ft->fi, "FsmExpireTimer %lx", (long) ft); |
| 94 | #endif |
| 95 | FsmEvent(ft->fi, ft->event, ft->arg); |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft) |
| 100 | { |
| 101 | ft->fi = fi; |
| 102 | ft->tl.function = (void *) FsmExpireTimer; |
| 103 | ft->tl.data = (long) ft; |
| 104 | #if FSM_TIMER_DEBUG |
| 105 | if (ft->fi->debug) |
| 106 | ft->fi->printdebug(ft->fi, "FsmInitTimer %lx", (long) ft); |
| 107 | #endif |
| 108 | init_timer(&ft->tl); |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | FsmDelTimer(struct FsmTimer *ft, int where) |
| 113 | { |
| 114 | #if FSM_TIMER_DEBUG |
| 115 | if (ft->fi->debug) |
| 116 | ft->fi->printdebug(ft->fi, "FsmDelTimer %lx %d", (long) ft, where); |
| 117 | #endif |
| 118 | del_timer(&ft->tl); |
| 119 | } |
| 120 | |
| 121 | int |
| 122 | FsmAddTimer(struct FsmTimer *ft, |
| 123 | int millisec, int event, void *arg, int where) |
| 124 | { |
| 125 | |
| 126 | #if FSM_TIMER_DEBUG |
| 127 | if (ft->fi->debug) |
| 128 | ft->fi->printdebug(ft->fi, "FsmAddTimer %lx %d %d", |
| 129 | (long) ft, millisec, where); |
| 130 | #endif |
| 131 | |
| 132 | if (timer_pending(&ft->tl)) { |
| 133 | printk(KERN_WARNING "FsmAddTimer: timer already active!\n"); |
| 134 | ft->fi->printdebug(ft->fi, "FsmAddTimer already active!"); |
| 135 | return -1; |
| 136 | } |
| 137 | init_timer(&ft->tl); |
| 138 | ft->event = event; |
| 139 | ft->arg = arg; |
| 140 | ft->tl.expires = jiffies + (millisec * HZ) / 1000; |
| 141 | add_timer(&ft->tl); |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | void |
| 146 | FsmRestartTimer(struct FsmTimer *ft, |
| 147 | int millisec, int event, void *arg, int where) |
| 148 | { |
| 149 | |
| 150 | #if FSM_TIMER_DEBUG |
| 151 | if (ft->fi->debug) |
| 152 | ft->fi->printdebug(ft->fi, "FsmRestartTimer %lx %d %d", |
| 153 | (long) ft, millisec, where); |
| 154 | #endif |
| 155 | |
| 156 | if (timer_pending(&ft->tl)) |
| 157 | del_timer(&ft->tl); |
| 158 | init_timer(&ft->tl); |
| 159 | ft->event = event; |
| 160 | ft->arg = arg; |
| 161 | ft->tl.expires = jiffies + (millisec * HZ) / 1000; |
| 162 | add_timer(&ft->tl); |
| 163 | } |