1 /*
2  * DSS.1 Finite State Machine
3  * base: ITU-T Rec Q.931
4  *
5  * Copyright (C) 1996 Universidade de Lisboa
6  *
7  * Written by Pedro Roque Marques (pedro_m@yahoo.com)
8  *
9  * This software may be used and distributed according to the terms of
10  * the GNU General Public License, incorporated herein by reference.
11  */
12 
13 /*
14  *        TODO: complete the FSM
15  *              move state/event descriptions to a user space logger
16  */
17 
18 #include <linux/sched.h>
19 #include <linux/string.h>
20 #include <linux/kernel.h>
21 
22 #include <linux/types.h>
23 #include <linux/slab.h>
24 #include <linux/mm.h>
25 #include <linux/tqueue.h>
26 #include <linux/skbuff.h>
27 
28 #include <linux/timer.h>
29 #include <asm/io.h>
30 
31 #include <linux/isdnif.h>
32 
33 #include "pcbit.h"
34 #include "edss1.h"
35 #include "layer2.h"
36 #include "callbacks.h"
37 
38 
39 extern void pcbit_state_change(struct pcbit_dev *, struct pcbit_chan *,
40                                unsigned short i, unsigned short ev,
41                                unsigned short f);
42 
43 extern struct pcbit_dev * dev_pcbit[MAX_PCBIT_CARDS];
44 
45 char * isdn_state_table[] = {
46   "Closed",
47   "Call initiated",
48   "Overlap sending",
49   "Outgoing call proceeding",
50   "NOT DEFINED",
51   "Call delivered",
52   "Call present",
53   "Call received",
54   "Connect request",
55   "Incoming call proceeding",
56   "Active",
57   "Disconnect request",
58   "Disconnect indication",
59   "NOT DEFINED",
60   "NOT DEFINED",
61   "Suspend request",
62   "NOT DEFINED",
63   "Resume request",
64   "NOT DEFINED",
65   "Release Request",
66   "NOT DEFINED",
67   "NOT DEFINED",
68   "NOT DEFINED",
69   "NOT DEFINED",
70   "NOT DEFINED",
71   "Overlap receiving",
72   "Select protocol on B-Channel",
73   "Activate B-channel protocol"
74 };
75 
76 #ifdef DEBUG_ERRS
77 static
78 struct CauseValue {
79   byte nr;
80   char *descr;
81 } cvlist[]={
82   {0x01,"Unallocated (unassigned) number"},
83   {0x02,"No route to specified transit network"},
84   {0x03,"No route to destination"},
85   {0x04,"Send special information tone"},
86   {0x05,"Misdialled trunk prefix"},
87   {0x06,"Channel unacceptable"},
88   {0x07,"Channel awarded and being delivered in an established channel"},
89   {0x08,"Preemption"},
90   {0x09,"Preemption - circuit reserved for reuse"},
91   {0x10,"Normal call clearing"},
92   {0x11,"User busy"},
93   {0x12,"No user responding"},
94   {0x13,"No answer from user (user alerted)"},
95   {0x14,"Subscriber absent"},
96   {0x15,"Call rejected"},
97   {0x16,"Number changed"},
98   {0x1a,"non-selected user clearing"},
99   {0x1b,"Destination out of order"},
100   {0x1c,"Invalid number format (address incomplete)"},
101   {0x1d,"Facility rejected"},
102   {0x1e,"Response to Status enquiry"},
103   {0x1f,"Normal, unspecified"},
104   {0x22,"No circuit/channel available"},
105   {0x26,"Network out of order"},
106   {0x27,"Permanent frame mode connection out-of-service"},
107   {0x28,"Permanent frame mode connection operational"},
108   {0x29,"Temporary failure"},
109   {0x2a,"Switching equipment congestion"},
110   {0x2b,"Access information discarded"},
111   {0x2c,"Requested circuit/channel not available"},
112   {0x2e,"Precedence call blocked"},
113   {0x2f,"Resource unavailable, unspecified"},
114   {0x31,"Quality of service unavailable"},
115   {0x32,"Requested facility not subscribed"},
116   {0x35,"Outgoing calls barred within CUG"},
117   {0x37,"Incoming calls barred within CUG"},
118   {0x39,"Bearer capability not authorized"},
119   {0x3a,"Bearer capability not presently available"},
120   {0x3e,"Inconsistency in designated outgoing access information and subscriber class"},
121   {0x3f,"Service or option not available, unspecified"},
122   {0x41,"Bearer capability not implemented"},
123   {0x42,"Channel type not implemented"},
124   {0x43,"Requested facility not implemented"},
125   {0x44,"Only restricted digital information bearer capability is available"},
126   {0x4f,"Service or option not implemented"},
127   {0x51,"Invalid call reference value"},
128   {0x52,"Identified channel does not exist"},
129   {0x53,"A suspended call exists, but this call identity does not"},
130   {0x54,"Call identity in use"},
131   {0x55,"No call suspended"},
132   {0x56,"Call having the requested call identity has been cleared"},
133   {0x57,"User not member of CUG"},
134   {0x58,"Incompatible destination"},
135   {0x5a,"Non-existent CUG"},
136   {0x5b,"Invalid transit network selection"},
137   {0x5f,"Invalid message, unspecified"},
138   {0x60,"Mandatory information element is missing"},
139   {0x61,"Message type non-existent or not implemented"},
140   {0x62,"Message not compatible with call state or message type non-existent or not implemented"},
141   {0x63,"Information element/parameter non-existent or not implemented"},
142   {0x64,"Invalid information element contents"},
143   {0x65,"Message not compatible with call state"},
144   {0x66,"Recovery on timer expiry"},
145   {0x67,"Parameter non-existent or not implemented - passed on"},
146   {0x6e,"Message with unrecognized parameter discarded"},
147   {0x6f,"Protocol error, unspecified"},
148   {0x7f,"Interworking, unspecified"}
149 };
150 
151 #endif
152 
153 static struct isdn_event_desc {
154   unsigned short ev;
155   char * desc;
156 } isdn_event_table [] = {
157   {EV_USR_SETUP_REQ,     "CC->L3: Setup Request"},
158   {EV_USR_SETUP_RESP,    "CC->L3: Setup Response"},
159   {EV_USR_PROCED_REQ,    "CC->L3: Proceeding Request"},
160   {EV_USR_RELEASE_REQ,   "CC->L3: Release Request"},
161 
162   {EV_NET_SETUP,        "NET->TE: setup "},
163   {EV_NET_CALL_PROC,    "NET->TE: call proceeding"},
164   {EV_NET_SETUP_ACK,    "NET->TE: setup acknowledge (more info needed)"},
165   {EV_NET_CONN,         "NET->TE: connect"},
166   {EV_NET_CONN_ACK,     "NET->TE: connect acknowledge"},
167   {EV_NET_DISC,         "NET->TE: disconnect indication"},
168   {EV_NET_RELEASE,      "NET->TE: release"},
169   {EV_NET_RELEASE_COMP, "NET->TE: release complete"},
170   {EV_NET_SELP_RESP,    "Board: Select B-channel protocol ack"},
171   {EV_NET_ACTV_RESP,    "Board: Activate B-channel protocol ack"},
172   {EV_TIMER,            "Timeout"},
173   {0, "NULL"}
174 };
175 
strisdnevent(ushort ev)176 char * strisdnevent(ushort ev)
177 {
178   struct isdn_event_desc * entry;
179 
180   for (entry = isdn_event_table; entry->ev; entry++)
181     if (entry->ev == ev)
182       break;
183 
184   return entry->desc;
185 }
186 
187 /*
188  * Euro ISDN finite state machine
189  */
190 
191 static struct fsm_timer_entry fsm_timers[] = {
192   {ST_CALL_PROC, 10},
193   {ST_DISC_REQ, 2},
194   {ST_ACTIVE_SELP, 5},
195   {ST_ACTIVE_ACTV, 5},
196   {ST_INCM_PROC, 10},
197   {ST_CONN_REQ, 2},
198   {0xff, 0}
199 };
200 
201 static struct fsm_entry fsm_table[] = {
202 /* Connect Phase */
203   /* Outgoing */
204   {ST_NULL, ST_CALL_INIT, EV_USR_SETUP_REQ, cb_out_1},
205 
206   {ST_CALL_INIT, ST_OVER_SEND, EV_NET_SETUP_ACK, cb_notdone},
207   {ST_CALL_INIT, ST_CALL_PROC, EV_NET_CALL_PROC, NULL},
208   {ST_CALL_INIT, ST_NULL, EV_NET_DISC, cb_out_2},
209 
210   {ST_CALL_PROC, ST_ACTIVE_SELP, EV_NET_CONN, cb_out_2},
211   {ST_CALL_PROC, ST_NULL, EV_NET_DISC, cb_disc_1},
212   {ST_CALL_PROC, ST_DISC_REQ, EV_USR_RELEASE_REQ, cb_disc_2},
213 
214   /* Incoming */
215   {ST_NULL, ST_CALL_PRES, EV_NET_SETUP, NULL},
216 
217   {ST_CALL_PRES, ST_INCM_PROC, EV_USR_PROCED_REQ, cb_in_1},
218   {ST_CALL_PRES, ST_DISC_REQ, EV_USR_RELEASE_REQ, cb_disc_2},
219 
220   {ST_INCM_PROC, ST_CONN_REQ, EV_USR_SETUP_RESP, cb_in_2},
221   {ST_INCM_PROC, ST_DISC_REQ, EV_USR_RELEASE_REQ, cb_disc_2},
222 
223   {ST_CONN_REQ, ST_ACTIVE_SELP, EV_NET_CONN_ACK, cb_in_3},
224 
225   /* Active */
226   {ST_ACTIVE, ST_NULL, EV_NET_DISC, cb_disc_1},
227   {ST_ACTIVE, ST_DISC_REQ, EV_USR_RELEASE_REQ, cb_disc_2},
228   {ST_ACTIVE, ST_NULL, EV_NET_RELEASE, cb_disc_3},
229 
230   /* Disconnect */
231 
232   {ST_DISC_REQ, ST_NULL, EV_NET_DISC, cb_disc_1},
233   {ST_DISC_REQ, ST_NULL, EV_NET_RELEASE, cb_disc_3},
234 
235   /* protocol selection */
236   {ST_ACTIVE_SELP, ST_ACTIVE_ACTV, EV_NET_SELP_RESP, cb_selp_1},
237   {ST_ACTIVE_SELP, ST_DISC_REQ, EV_USR_RELEASE_REQ, cb_disc_2},
238 
239   {ST_ACTIVE_ACTV, ST_ACTIVE, EV_NET_ACTV_RESP, cb_open},
240   {ST_ACTIVE_ACTV, ST_DISC_REQ, EV_USR_RELEASE_REQ, cb_disc_2},
241 
242   /* Timers */
243   {ST_CALL_PROC, ST_DISC_REQ, EV_TIMER, cb_disc_2},
244   {ST_DISC_REQ, ST_NULL, EV_TIMER, cb_disc_3},
245   {ST_ACTIVE_SELP, ST_DISC_REQ, EV_TIMER, cb_disc_2},
246   {ST_ACTIVE_ACTV, ST_DISC_REQ, EV_TIMER, cb_disc_2},
247   {ST_INCM_PROC, ST_DISC_REQ, EV_TIMER, cb_disc_2},
248   {ST_CONN_REQ, ST_CONN_REQ, EV_TIMER, cb_in_2},
249 
250   {0xff, 0, 0, NULL}
251 };
252 
253 
pcbit_fsm_timer(unsigned long data)254 static void pcbit_fsm_timer(unsigned long data)
255 {
256         struct pcbit_dev *dev;
257         struct pcbit_chan *chan;
258 
259         chan = (struct pcbit_chan *) data;
260 
261         del_timer(&chan->fsm_timer);
262         chan->fsm_timer.function = NULL;
263 
264         dev = chan2dev(chan);
265 
266         if (dev == NULL) {
267                 printk(KERN_WARNING "pcbit: timer for unknown device\n");
268                 return;
269         }
270 
271         pcbit_fsm_event(dev, chan, EV_TIMER, NULL);
272 }
273 
274 
pcbit_fsm_event(struct pcbit_dev * dev,struct pcbit_chan * chan,unsigned short event,struct callb_data * data)275 void pcbit_fsm_event(struct pcbit_dev *dev, struct pcbit_chan *chan,
276 		   unsigned short event, struct callb_data *data)
277 {
278 	struct fsm_entry * action;
279 	struct fsm_timer_entry *tentry;
280 	unsigned long flags;
281 
282 	save_flags(flags);
283 	cli();
284 
285 
286         for (action = fsm_table; action->init != 0xff; action++)
287                 if (action->init == chan->fsm_state && action->event == event)
288                         break;
289 
290 	if (action->init == 0xff) {
291 
292 		printk(KERN_DEBUG "fsm error: event %x on state %x\n",
293                        event, chan->fsm_state);
294 		restore_flags(flags);
295 		return;
296 	}
297 
298         if (chan->fsm_timer.function) {
299                 del_timer(&chan->fsm_timer);
300                 chan->fsm_timer.function = NULL;
301         }
302 
303 	chan->fsm_state = action->final;
304 
305 	pcbit_state_change(dev, chan, action->init, event, action->final);
306 
307         for (tentry = fsm_timers; tentry->init != 0xff; tentry++)
308                 if (tentry->init == chan->fsm_state)
309                         break;
310 
311         if (tentry->init != 0xff) {
312                 init_timer(&chan->fsm_timer);
313                 chan->fsm_timer.function = &pcbit_fsm_timer;
314                 chan->fsm_timer.data = (ulong) chan;
315                 chan->fsm_timer.expires = jiffies + tentry->timeout * HZ;
316                 add_timer(&chan->fsm_timer);
317         }
318 
319 	restore_flags(flags);
320 
321 	if (action->callb)
322 		action->callb(dev, chan, data);
323 
324 }
325 
326 
327 
328 
329