Merge master.kernel.org:/home/rmk/linux-2.6-mmc
[pandora-kernel.git] / drivers / s390 / net / fsm.c
1 /**
2  * $Id: fsm.c,v 1.6 2003/10/15 11:37:29 mschwide Exp $
3  *
4  * A generic FSM based on fsm used in isdn4linux
5  *
6  */
7
8 #include "fsm.h"
9 #include <linux/config.h>
10 #include <linux/module.h>
11 #include <linux/timer.h>
12
13 MODULE_AUTHOR("(C) 2000 IBM Corp. by Fritz Elfert (felfert@millenux.com)");
14 MODULE_DESCRIPTION("Finite state machine helper functions");
15 MODULE_LICENSE("GPL");
16
17 fsm_instance *
18 init_fsm(char *name, const char **state_names, const char **event_names, int nr_states,
19                 int nr_events, const fsm_node *tmpl, int tmpl_len, gfp_t order)
20 {
21         int i;
22         fsm_instance *this;
23         fsm_function_t *m;
24         fsm *f;
25
26         this = (fsm_instance *)kmalloc(sizeof(fsm_instance), order);
27         if (this == NULL) {
28                 printk(KERN_WARNING
29                         "fsm(%s): init_fsm: Couldn't alloc instance\n", name);
30                 return NULL;
31         }
32         memset(this, 0, sizeof(fsm_instance));
33         strlcpy(this->name, name, sizeof(this->name));
34
35         f = (fsm *)kmalloc(sizeof(fsm), order);
36         if (f == NULL) {
37                 printk(KERN_WARNING
38                         "fsm(%s): init_fsm: Couldn't alloc fsm\n", name);
39                 kfree_fsm(this);
40                 return NULL;
41         }
42         memset(f, 0, sizeof(fsm));
43         f->nr_events = nr_events;
44         f->nr_states = nr_states;
45         f->event_names = event_names;
46         f->state_names = state_names;
47         this->f = f;
48
49         m = (fsm_function_t *)kmalloc(
50                         sizeof(fsm_function_t) * nr_states * nr_events, order);
51         if (m == NULL) {
52                 printk(KERN_WARNING
53                         "fsm(%s): init_fsm: Couldn't alloc jumptable\n", name);
54                 kfree_fsm(this);
55                 return NULL;
56         }
57         memset(m, 0, sizeof(fsm_function_t) * f->nr_states * f->nr_events);
58         f->jumpmatrix = m;
59
60         for (i = 0; i < tmpl_len; i++) {
61                 if ((tmpl[i].cond_state >= nr_states) ||
62                     (tmpl[i].cond_event >= nr_events)   ) {
63                         printk(KERN_ERR
64                                 "fsm(%s): init_fsm: Bad template l=%d st(%ld/%ld) ev(%ld/%ld)\n",
65                                 name, i, (long)tmpl[i].cond_state, (long)f->nr_states,
66                                 (long)tmpl[i].cond_event, (long)f->nr_events);
67                         kfree_fsm(this);
68                         return NULL;
69                 } else
70                         m[nr_states * tmpl[i].cond_event + tmpl[i].cond_state] =
71                                 tmpl[i].function;
72         }
73         return this;
74 }
75
76 void
77 kfree_fsm(fsm_instance *this)
78 {
79         if (this) {
80                 if (this->f) {
81                         kfree(this->f->jumpmatrix);
82                         kfree(this->f);
83                 }
84                 kfree(this);
85         } else
86                 printk(KERN_WARNING
87                         "fsm: kfree_fsm called with NULL argument\n");
88 }
89
90 #if FSM_DEBUG_HISTORY
91 void
92 fsm_print_history(fsm_instance *fi)
93 {
94         int idx = 0;
95         int i;
96
97         if (fi->history_size >= FSM_HISTORY_SIZE)
98                 idx = fi->history_index;
99
100         printk(KERN_DEBUG "fsm(%s): History:\n", fi->name);
101         for (i = 0; i < fi->history_size; i++) {
102                 int e = fi->history[idx].event;
103                 int s = fi->history[idx++].state;
104                 idx %= FSM_HISTORY_SIZE;
105                 if (e == -1)
106                         printk(KERN_DEBUG "  S=%s\n",
107                                fi->f->state_names[s]);
108                 else
109                         printk(KERN_DEBUG "  S=%s E=%s\n",
110                                fi->f->state_names[s],
111                                fi->f->event_names[e]);
112         }
113         fi->history_size = fi->history_index = 0;
114 }
115
116 void
117 fsm_record_history(fsm_instance *fi, int state, int event)
118 {
119         fi->history[fi->history_index].state = state;
120         fi->history[fi->history_index++].event = event;
121         fi->history_index %= FSM_HISTORY_SIZE;
122         if (fi->history_size < FSM_HISTORY_SIZE)
123                 fi->history_size++;
124 }
125 #endif
126
127 const char *
128 fsm_getstate_str(fsm_instance *fi)
129 {
130         int st = atomic_read(&fi->state);
131         if (st >= fi->f->nr_states)
132                 return "Invalid";
133         return fi->f->state_names[st];
134 }
135
136 static void
137 fsm_expire_timer(fsm_timer *this)
138 {
139 #if FSM_TIMER_DEBUG
140         printk(KERN_DEBUG "fsm(%s): Timer %p expired\n",
141                this->fi->name, this);
142 #endif
143         fsm_event(this->fi, this->expire_event, this->event_arg);
144 }
145
146 void
147 fsm_settimer(fsm_instance *fi, fsm_timer *this)
148 {
149         this->fi = fi;
150         this->tl.function = (void *)fsm_expire_timer;
151         this->tl.data = (long)this;
152 #if FSM_TIMER_DEBUG
153         printk(KERN_DEBUG "fsm(%s): Create timer %p\n", fi->name,
154                this);
155 #endif
156         init_timer(&this->tl);
157 }
158
159 void
160 fsm_deltimer(fsm_timer *this)
161 {
162 #if FSM_TIMER_DEBUG
163         printk(KERN_DEBUG "fsm(%s): Delete timer %p\n", this->fi->name,
164                 this);
165 #endif
166         del_timer(&this->tl);
167 }
168
169 int
170 fsm_addtimer(fsm_timer *this, int millisec, int event, void *arg)
171 {
172
173 #if FSM_TIMER_DEBUG
174         printk(KERN_DEBUG "fsm(%s): Add timer %p %dms\n",
175                this->fi->name, this, millisec);
176 #endif
177
178         init_timer(&this->tl);
179         this->tl.function = (void *)fsm_expire_timer;
180         this->tl.data = (long)this;
181         this->expire_event = event;
182         this->event_arg = arg;
183         this->tl.expires = jiffies + (millisec * HZ) / 1000;
184         add_timer(&this->tl);
185         return 0;
186 }
187
188 /* FIXME: this function is never used, why */
189 void
190 fsm_modtimer(fsm_timer *this, int millisec, int event, void *arg)
191 {
192
193 #if FSM_TIMER_DEBUG
194         printk(KERN_DEBUG "fsm(%s): Restart timer %p %dms\n",
195                 this->fi->name, this, millisec);
196 #endif
197
198         del_timer(&this->tl);
199         init_timer(&this->tl);
200         this->tl.function = (void *)fsm_expire_timer;
201         this->tl.data = (long)this;
202         this->expire_event = event;
203         this->event_arg = arg;
204         this->tl.expires = jiffies + (millisec * HZ) / 1000;
205         add_timer(&this->tl);
206 }
207
208 EXPORT_SYMBOL(init_fsm);
209 EXPORT_SYMBOL(kfree_fsm);
210 EXPORT_SYMBOL(fsm_settimer);
211 EXPORT_SYMBOL(fsm_deltimer);
212 EXPORT_SYMBOL(fsm_addtimer);
213 EXPORT_SYMBOL(fsm_modtimer);
214 EXPORT_SYMBOL(fsm_getstate_str);
215
216 #if FSM_DEBUG_HISTORY
217 EXPORT_SYMBOL(fsm_print_history);
218 EXPORT_SYMBOL(fsm_record_history);
219 #endif