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