GNU Linux-libre 4.14.303-gnu1
[releases.git] / drivers / isdn / hisax / fsm.c
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/slab.h>
19 #include <linux/init.h>
20 #include "hisax.h"
21
22 #define FSM_TIMER_DEBUG 0
23
24 int
25 FsmNew(struct Fsm *fsm, struct FsmNode *fnlist, int fncount)
26 {
27         int i;
28
29         fsm->jumpmatrix =
30                 kzalloc(sizeof(FSMFNPTR) * fsm->state_count * fsm->event_count, GFP_KERNEL);
31         if (!fsm->jumpmatrix)
32                 return -ENOMEM;
33
34         for (i = 0; i < fncount; i++)
35                 if ((fnlist[i].state >= fsm->state_count) || (fnlist[i].event >= fsm->event_count)) {
36                         printk(KERN_ERR "FsmNew Error line %d st(%ld/%ld) ev(%ld/%ld)\n",
37                                i, (long)fnlist[i].state, (long)fsm->state_count,
38                                (long)fnlist[i].event, (long)fsm->event_count);
39                 } else
40                         fsm->jumpmatrix[fsm->state_count * fnlist[i].event +
41                                         fnlist[i].state] = (FSMFNPTR)fnlist[i].routine;
42         return 0;
43 }
44
45 void
46 FsmFree(struct Fsm *fsm)
47 {
48         kfree((void *) fsm->jumpmatrix);
49 }
50
51 int
52 FsmEvent(struct FsmInst *fi, int event, void *arg)
53 {
54         FSMFNPTR r;
55
56         if ((fi->state >= fi->fsm->state_count) || (event >= fi->fsm->event_count)) {
57                 printk(KERN_ERR "FsmEvent Error st(%ld/%ld) ev(%d/%ld)\n",
58                        (long)fi->state, (long)fi->fsm->state_count, event, (long)fi->fsm->event_count);
59                 return (1);
60         }
61         r = fi->fsm->jumpmatrix[fi->fsm->state_count * event + fi->state];
62         if (r) {
63                 if (fi->debug)
64                         fi->printdebug(fi, "State %s Event %s",
65                                        fi->fsm->strState[fi->state],
66                                        fi->fsm->strEvent[event]);
67                 r(fi, event, arg);
68                 return (0);
69         } else {
70                 if (fi->debug)
71                         fi->printdebug(fi, "State %s Event %s no routine",
72                                        fi->fsm->strState[fi->state],
73                                        fi->fsm->strEvent[event]);
74                 return (!0);
75         }
76 }
77
78 void
79 FsmChangeState(struct FsmInst *fi, int newstate)
80 {
81         fi->state = newstate;
82         if (fi->debug)
83                 fi->printdebug(fi, "ChangeState %s",
84                                fi->fsm->strState[newstate]);
85 }
86
87 static void
88 FsmExpireTimer(struct FsmTimer *ft)
89 {
90 #if FSM_TIMER_DEBUG
91         if (ft->fi->debug)
92                 ft->fi->printdebug(ft->fi, "FsmExpireTimer %lx", (long) ft);
93 #endif
94         FsmEvent(ft->fi, ft->event, ft->arg);
95 }
96
97 void
98 FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft)
99 {
100         ft->fi = fi;
101 #if FSM_TIMER_DEBUG
102         if (ft->fi->debug)
103                 ft->fi->printdebug(ft->fi, "FsmInitTimer %lx", (long) ft);
104 #endif
105         setup_timer(&ft->tl, (void *)FsmExpireTimer, (long)ft);
106 }
107
108 void
109 FsmDelTimer(struct FsmTimer *ft, int where)
110 {
111 #if FSM_TIMER_DEBUG
112         if (ft->fi->debug)
113                 ft->fi->printdebug(ft->fi, "FsmDelTimer %lx %d", (long) ft, where);
114 #endif
115         del_timer(&ft->tl);
116 }
117
118 int
119 FsmAddTimer(struct FsmTimer *ft,
120             int millisec, int event, void *arg, int where)
121 {
122
123 #if FSM_TIMER_DEBUG
124         if (ft->fi->debug)
125                 ft->fi->printdebug(ft->fi, "FsmAddTimer %lx %d %d",
126                                    (long) ft, millisec, where);
127 #endif
128
129         if (timer_pending(&ft->tl)) {
130                 printk(KERN_WARNING "FsmAddTimer: timer already active!\n");
131                 ft->fi->printdebug(ft->fi, "FsmAddTimer already active!");
132                 return -1;
133         }
134         init_timer(&ft->tl);
135         ft->event = event;
136         ft->arg = arg;
137         ft->tl.expires = jiffies + (millisec * HZ) / 1000;
138         add_timer(&ft->tl);
139         return 0;
140 }
141
142 void
143 FsmRestartTimer(struct FsmTimer *ft,
144                 int millisec, int event, void *arg, int where)
145 {
146
147 #if FSM_TIMER_DEBUG
148         if (ft->fi->debug)
149                 ft->fi->printdebug(ft->fi, "FsmRestartTimer %lx %d %d",
150                                    (long) ft, millisec, where);
151 #endif
152
153         if (timer_pending(&ft->tl))
154                 del_timer(&ft->tl);
155         init_timer(&ft->tl);
156         ft->event = event;
157         ft->arg = arg;
158         ft->tl.expires = jiffies + (millisec * HZ) / 1000;
159         add_timer(&ft->tl);
160 }