2 * finite state machine implementation
4 * Author Karsten Keil <kkeil@novell.com>
6 * Thanks to Jan den Ouden
8 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
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.
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.
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <linux/string.h>
27 #define FSM_TIMER_DEBUG 0
30 mISDN_FsmNew(struct Fsm
*fsm
,
31 struct FsmNode
*fnlist
, int fncount
)
36 kzalloc(array3_size(sizeof(FSMFNPTR
), fsm
->state_count
,
39 if (fsm
->jumpmatrix
== NULL
)
42 for (i
= 0; i
< fncount
; i
++)
43 if ((fnlist
[i
].state
>= fsm
->state_count
) ||
44 (fnlist
[i
].event
>= fsm
->event_count
)) {
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
);
50 fsm
->jumpmatrix
[fsm
->state_count
* fnlist
[i
].event
+
51 fnlist
[i
].state
] = (FSMFNPTR
) fnlist
[i
].routine
;
54 EXPORT_SYMBOL(mISDN_FsmNew
);
57 mISDN_FsmFree(struct Fsm
*fsm
)
59 kfree((void *) fsm
->jumpmatrix
);
61 EXPORT_SYMBOL(mISDN_FsmFree
);
64 mISDN_FsmEvent(struct FsmInst
*fi
, int event
, void *arg
)
68 if ((fi
->state
>= fi
->fsm
->state_count
) ||
69 (event
>= fi
->fsm
->event_count
)) {
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
);
76 r
= fi
->fsm
->jumpmatrix
[fi
->fsm
->state_count
* event
+ fi
->state
];
79 fi
->printdebug(fi
, "State %s Event %s",
80 fi
->fsm
->strState
[fi
->state
],
81 fi
->fsm
->strEvent
[event
]);
86 fi
->printdebug(fi
, "State %s Event %s no action",
87 fi
->fsm
->strState
[fi
->state
],
88 fi
->fsm
->strEvent
[event
]);
92 EXPORT_SYMBOL(mISDN_FsmEvent
);
95 mISDN_FsmChangeState(struct FsmInst
*fi
, int newstate
)
99 fi
->printdebug(fi
, "ChangeState %s",
100 fi
->fsm
->strState
[newstate
]);
102 EXPORT_SYMBOL(mISDN_FsmChangeState
);
105 FsmExpireTimer(struct timer_list
*t
)
107 struct FsmTimer
*ft
= from_timer(ft
, t
, tl
);
110 ft
->fi
->printdebug(ft
->fi
, "FsmExpireTimer %lx", (long) ft
);
112 mISDN_FsmEvent(ft
->fi
, ft
->event
, ft
->arg
);
116 mISDN_FsmInitTimer(struct FsmInst
*fi
, struct FsmTimer
*ft
)
121 ft
->fi
->printdebug(ft
->fi
, "mISDN_FsmInitTimer %lx", (long) ft
);
123 timer_setup(&ft
->tl
, FsmExpireTimer
, 0);
125 EXPORT_SYMBOL(mISDN_FsmInitTimer
);
128 mISDN_FsmDelTimer(struct FsmTimer
*ft
, int where
)
132 ft
->fi
->printdebug(ft
->fi
, "mISDN_FsmDelTimer %lx %d",
137 EXPORT_SYMBOL(mISDN_FsmDelTimer
);
140 mISDN_FsmAddTimer(struct FsmTimer
*ft
,
141 int millisec
, int event
, void *arg
, int where
)
146 ft
->fi
->printdebug(ft
->fi
, "mISDN_FsmAddTimer %lx %d %d",
147 (long) ft
, millisec
, where
);
150 if (timer_pending(&ft
->tl
)) {
153 "mISDN_FsmAddTimer: timer already active!\n");
154 ft
->fi
->printdebug(ft
->fi
,
155 "mISDN_FsmAddTimer already active!");
161 ft
->tl
.expires
= jiffies
+ (millisec
* HZ
) / 1000;
165 EXPORT_SYMBOL(mISDN_FsmAddTimer
);
168 mISDN_FsmRestartTimer(struct FsmTimer
*ft
,
169 int millisec
, int event
, void *arg
, int where
)
174 ft
->fi
->printdebug(ft
->fi
, "mISDN_FsmRestartTimer %lx %d %d",
175 (long) ft
, millisec
, where
);
178 if (timer_pending(&ft
->tl
))
182 ft
->tl
.expires
= jiffies
+ (millisec
* HZ
) / 1000;
185 EXPORT_SYMBOL(mISDN_FsmRestartTimer
);