]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/isdn/mISDN/fsm.c
mtd: nand: atmel: Relax tADL_min constraint
[mirror_ubuntu-artful-kernel.git] / drivers / isdn / mISDN / fsm.c
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
29 void
30 mISDN_FsmNew(struct Fsm *fsm,
31 struct FsmNode *fnlist, int fncount)
32 {
33 int i;
34
35 fsm->jumpmatrix = kzalloc(sizeof(FSMFNPTR) * fsm->state_count *
36 fsm->event_count, GFP_KERNEL);
37
38 for (i = 0; i < fncount; i++)
39 if ((fnlist[i].state >= fsm->state_count) ||
40 (fnlist[i].event >= fsm->event_count)) {
41 printk(KERN_ERR
42 "mISDN_FsmNew Error: %d st(%ld/%ld) ev(%ld/%ld)\n",
43 i, (long)fnlist[i].state, (long)fsm->state_count,
44 (long)fnlist[i].event, (long)fsm->event_count);
45 } else
46 fsm->jumpmatrix[fsm->state_count * fnlist[i].event +
47 fnlist[i].state] = (FSMFNPTR) fnlist[i].routine;
48 }
49 EXPORT_SYMBOL(mISDN_FsmNew);
50
51 void
52 mISDN_FsmFree(struct Fsm *fsm)
53 {
54 kfree((void *) fsm->jumpmatrix);
55 }
56 EXPORT_SYMBOL(mISDN_FsmFree);
57
58 int
59 mISDN_FsmEvent(struct FsmInst *fi, int event, void *arg)
60 {
61 FSMFNPTR r;
62
63 if ((fi->state >= fi->fsm->state_count) ||
64 (event >= fi->fsm->event_count)) {
65 printk(KERN_ERR
66 "mISDN_FsmEvent Error st(%ld/%ld) ev(%d/%ld)\n",
67 (long)fi->state, (long)fi->fsm->state_count, event,
68 (long)fi->fsm->event_count);
69 return 1;
70 }
71 r = fi->fsm->jumpmatrix[fi->fsm->state_count * event + fi->state];
72 if (r) {
73 if (fi->debug)
74 fi->printdebug(fi, "State %s Event %s",
75 fi->fsm->strState[fi->state],
76 fi->fsm->strEvent[event]);
77 r(fi, event, arg);
78 return 0;
79 } else {
80 if (fi->debug)
81 fi->printdebug(fi, "State %s Event %s no action",
82 fi->fsm->strState[fi->state],
83 fi->fsm->strEvent[event]);
84 return 1;
85 }
86 }
87 EXPORT_SYMBOL(mISDN_FsmEvent);
88
89 void
90 mISDN_FsmChangeState(struct FsmInst *fi, int newstate)
91 {
92 fi->state = newstate;
93 if (fi->debug)
94 fi->printdebug(fi, "ChangeState %s",
95 fi->fsm->strState[newstate]);
96 }
97 EXPORT_SYMBOL(mISDN_FsmChangeState);
98
99 static void
100 FsmExpireTimer(struct FsmTimer *ft)
101 {
102 #if FSM_TIMER_DEBUG
103 if (ft->fi->debug)
104 ft->fi->printdebug(ft->fi, "FsmExpireTimer %lx", (long) ft);
105 #endif
106 mISDN_FsmEvent(ft->fi, ft->event, ft->arg);
107 }
108
109 void
110 mISDN_FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft)
111 {
112 ft->fi = fi;
113 #if FSM_TIMER_DEBUG
114 if (ft->fi->debug)
115 ft->fi->printdebug(ft->fi, "mISDN_FsmInitTimer %lx", (long) ft);
116 #endif
117 setup_timer(&ft->tl, (void *)FsmExpireTimer, (long)ft);
118 }
119 EXPORT_SYMBOL(mISDN_FsmInitTimer);
120
121 void
122 mISDN_FsmDelTimer(struct FsmTimer *ft, int where)
123 {
124 #if FSM_TIMER_DEBUG
125 if (ft->fi->debug)
126 ft->fi->printdebug(ft->fi, "mISDN_FsmDelTimer %lx %d",
127 (long) ft, where);
128 #endif
129 del_timer(&ft->tl);
130 }
131 EXPORT_SYMBOL(mISDN_FsmDelTimer);
132
133 int
134 mISDN_FsmAddTimer(struct FsmTimer *ft,
135 int millisec, int event, void *arg, int where)
136 {
137
138 #if FSM_TIMER_DEBUG
139 if (ft->fi->debug)
140 ft->fi->printdebug(ft->fi, "mISDN_FsmAddTimer %lx %d %d",
141 (long) ft, millisec, where);
142 #endif
143
144 if (timer_pending(&ft->tl)) {
145 if (ft->fi->debug) {
146 printk(KERN_WARNING
147 "mISDN_FsmAddTimer: timer already active!\n");
148 ft->fi->printdebug(ft->fi,
149 "mISDN_FsmAddTimer already active!");
150 }
151 return -1;
152 }
153 init_timer(&ft->tl);
154 ft->event = event;
155 ft->arg = arg;
156 ft->tl.expires = jiffies + (millisec * HZ) / 1000;
157 add_timer(&ft->tl);
158 return 0;
159 }
160 EXPORT_SYMBOL(mISDN_FsmAddTimer);
161
162 void
163 mISDN_FsmRestartTimer(struct FsmTimer *ft,
164 int millisec, int event, void *arg, int where)
165 {
166
167 #if FSM_TIMER_DEBUG
168 if (ft->fi->debug)
169 ft->fi->printdebug(ft->fi, "mISDN_FsmRestartTimer %lx %d %d",
170 (long) ft, millisec, where);
171 #endif
172
173 if (timer_pending(&ft->tl))
174 del_timer(&ft->tl);
175 init_timer(&ft->tl);
176 ft->event = event;
177 ft->arg = arg;
178 ft->tl.expires = jiffies + (millisec * HZ) / 1000;
179 add_timer(&ft->tl);
180 }
181 EXPORT_SYMBOL(mISDN_FsmRestartTimer);