port more changes to make PCI work
[linux-2.4.git] / net / irda / iriap_event.c
1 /*********************************************************************
2  *                
3  * Filename:      iriap_event.c
4  * Version:       0.1
5  * Description:   IAP Finite State Machine
6  * Status:        Experimental.
7  * Author:        Dag Brattli <dagb@cs.uit.no>
8  * Created at:    Thu Aug 21 00:02:07 1997
9  * Modified at:   Wed Mar  1 11:28:34 2000
10  * Modified by:   Dag Brattli <dagb@cs.uit.no>
11  * 
12  *     Copyright (c) 1997, 1999-2000 Dag Brattli <dagb@cs.uit.no>, 
13  *     All Rights Reserved.
14  *     
15  *     This program is free software; you can redistribute it and/or 
16  *     modify it under the terms of the GNU General Public License as 
17  *     published by the Free Software Foundation; either version 2 of 
18  *     the License, or (at your option) any later version.
19  *
20  *     Neither Dag Brattli nor University of Tromsø admit liability nor
21  *     provide warranty for any of this software. This material is 
22  *     provided "AS-IS" and at no charge.
23  *
24  ********************************************************************/
25
26 #include <net/irda/irda.h>
27 #include <net/irda/irlmp.h>
28 #include <net/irda/iriap.h>
29 #include <net/irda/iriap_event.h>
30
31 static void state_s_disconnect   (struct iriap_cb *self, IRIAP_EVENT event, 
32                                   struct sk_buff *skb);
33 static void state_s_connecting   (struct iriap_cb *self, IRIAP_EVENT event, 
34                                   struct sk_buff *skb);
35 static void state_s_call         (struct iriap_cb *self, IRIAP_EVENT event, 
36                                   struct sk_buff *skb);
37
38 static void state_s_make_call    (struct iriap_cb *self, IRIAP_EVENT event, 
39                                   struct sk_buff *skb);
40 static void state_s_calling      (struct iriap_cb *self, IRIAP_EVENT event, 
41                                   struct sk_buff *skb);
42 static void state_s_outstanding  (struct iriap_cb *self, IRIAP_EVENT event, 
43                                   struct sk_buff *skb);
44 static void state_s_replying     (struct iriap_cb *self, IRIAP_EVENT event, 
45                                   struct sk_buff *skb);
46 static void state_s_wait_for_call(struct iriap_cb *self, IRIAP_EVENT event, 
47                                   struct sk_buff *skb);
48 static void state_s_wait_active  (struct iriap_cb *self, IRIAP_EVENT event, 
49                                   struct sk_buff *skb);
50
51 static void state_r_disconnect   (struct iriap_cb *self, IRIAP_EVENT event, 
52                                   struct sk_buff *skb);
53 static void state_r_call         (struct iriap_cb *self, IRIAP_EVENT event, 
54                                   struct sk_buff *skb);
55 static void state_r_waiting      (struct iriap_cb *self, IRIAP_EVENT event, 
56                                   struct sk_buff *skb);
57 static void state_r_wait_active  (struct iriap_cb *self, IRIAP_EVENT event, 
58                                   struct sk_buff *skb);
59 static void state_r_receiving    (struct iriap_cb *self, IRIAP_EVENT event, 
60                                   struct sk_buff *skb);
61 static void state_r_execute      (struct iriap_cb *self, IRIAP_EVENT event, 
62                                   struct sk_buff *skb);
63 static void state_r_returning    (struct iriap_cb *self, IRIAP_EVENT event, 
64                                   struct sk_buff *skb);
65
66 static void (*iriap_state[])(struct iriap_cb *self, IRIAP_EVENT event, 
67                              struct sk_buff *skb) = { 
68         /* Client FSM */
69         state_s_disconnect,
70         state_s_connecting,
71         state_s_call,
72         
73         /* S-Call FSM */
74         state_s_make_call,
75         state_s_calling,
76         state_s_outstanding,
77         state_s_replying,
78         state_s_wait_for_call,
79         state_s_wait_active,
80
81         /* Server FSM */
82         state_r_disconnect,
83         state_r_call,
84
85         /* R-Connect FSM */
86         state_r_waiting,
87         state_r_wait_active,
88         state_r_receiving,
89         state_r_execute,
90         state_r_returning,
91 };
92
93 void iriap_next_client_state(struct iriap_cb *self, IRIAP_STATE state) 
94 {
95         ASSERT(self != NULL, return;);
96         ASSERT(self->magic == IAS_MAGIC, return;);
97
98         self->client_state = state;
99 }
100
101 void iriap_next_call_state(struct iriap_cb *self, IRIAP_STATE state) 
102 {
103         ASSERT(self != NULL, return;);
104         ASSERT(self->magic == IAS_MAGIC, return;);
105
106         self->call_state = state;
107 }
108
109 void iriap_next_server_state(struct iriap_cb *self, IRIAP_STATE state)
110 {
111         ASSERT(self != NULL, return;);
112         ASSERT(self->magic == IAS_MAGIC, return;);
113
114         self->server_state = state;
115 }
116
117 void iriap_next_r_connect_state(struct iriap_cb *self, IRIAP_STATE state)
118 {
119         ASSERT(self != NULL, return;);
120         ASSERT(self->magic == IAS_MAGIC, return;);
121         
122         self->r_connect_state = state;
123 }
124
125 void iriap_do_client_event(struct iriap_cb *self, IRIAP_EVENT event, 
126                            struct sk_buff *skb) 
127 {
128         ASSERT(self != NULL, return;);
129         ASSERT(self->magic == IAS_MAGIC, return;);
130
131         (*iriap_state[ self->client_state]) (self, event, skb);
132 }
133
134 void iriap_do_call_event(struct iriap_cb *self, IRIAP_EVENT event, 
135                          struct sk_buff *skb) 
136 {
137         ASSERT(self != NULL, return;);
138         ASSERT(self->magic == IAS_MAGIC, return;);
139         
140         (*iriap_state[ self->call_state]) (self, event, skb);
141 }
142
143 void iriap_do_server_event(struct iriap_cb *self, IRIAP_EVENT event, 
144                            struct sk_buff *skb) 
145 {
146         ASSERT(self != NULL, return;);
147         ASSERT(self->magic == IAS_MAGIC, return;);
148         
149         (*iriap_state[ self->server_state]) (self, event, skb);
150 }
151
152 void iriap_do_r_connect_event(struct iriap_cb *self, IRIAP_EVENT event, 
153                               struct sk_buff *skb) 
154 {
155         ASSERT(self != NULL, return;);
156         ASSERT(self->magic == IAS_MAGIC, return;);
157         
158         (*iriap_state[ self->r_connect_state]) (self, event, skb);
159 }
160
161
162 /*
163  * Function state_s_disconnect (event, skb)
164  *
165  *    S-Disconnect, The device has no LSAP connection to a particular
166  *    remote device.
167  */
168 static void state_s_disconnect(struct iriap_cb *self, IRIAP_EVENT event, 
169                                struct sk_buff *skb) 
170 {
171         ASSERT(self != NULL, return;);
172         ASSERT(self->magic == IAS_MAGIC, return;);
173
174         switch (event) {
175         case IAP_CALL_REQUEST_GVBC:
176                 iriap_next_client_state(self, S_CONNECTING);
177                 ASSERT(self->skb == NULL, return;);
178                 self->skb = skb;
179                 iriap_connect_request(self);
180                 break;
181         case IAP_LM_DISCONNECT_INDICATION:
182                 break;
183         default:
184                 IRDA_DEBUG(0, "%s(), Unknown event %d\n", __FUNCTION__, event);
185                 break;
186         }
187 }
188
189 /*
190  * Function state_s_connecting (self, event, skb)
191  *
192  *    S-Connecting
193  *
194  */
195 static void state_s_connecting(struct iriap_cb *self, IRIAP_EVENT event, 
196                                struct sk_buff *skb) 
197 {
198         ASSERT(self != NULL, return;);
199         ASSERT(self->magic == IAS_MAGIC, return;);
200
201         switch (event) {
202         case IAP_LM_CONNECT_CONFIRM:
203                 /*
204                  *  Jump to S-Call FSM
205                  */
206                 iriap_do_call_event(self, IAP_CALL_REQUEST, skb);
207                 /* iriap_call_request(self, 0,0,0); */
208                 iriap_next_client_state(self, S_CALL);
209                 break;
210         case IAP_LM_DISCONNECT_INDICATION:
211                 /* Abort calls */
212                 iriap_next_call_state(self, S_MAKE_CALL);
213                 iriap_next_client_state(self, S_DISCONNECT);
214                 break;
215         default:
216                 IRDA_DEBUG(0, "%s(), Unknown event %d\n", __FUNCTION__, event);
217                 break;
218         }
219 }
220
221 /*
222  * Function state_s_call (self, event, skb)
223  *
224  *    S-Call, The device can process calls to a specific remote
225  *    device. Whenever the LSAP connection is disconnected, this state
226  *    catches that event and clears up 
227  */
228 static void state_s_call(struct iriap_cb *self, IRIAP_EVENT event, 
229                          struct sk_buff *skb) 
230 {
231         ASSERT(self != NULL, return;);
232
233         switch (event) {
234         case IAP_LM_DISCONNECT_INDICATION:
235                 /* Abort calls */
236                 iriap_next_call_state(self, S_MAKE_CALL);
237                 iriap_next_client_state(self, S_DISCONNECT);
238                 break;
239         default:
240                 IRDA_DEBUG(0, "state_s_call: Unknown event %d\n", event);
241                 break;
242         }
243 }
244
245 /*
246  * Function state_s_make_call (event, skb)
247  *
248  *    S-Make-Call
249  *
250  */
251 static void state_s_make_call(struct iriap_cb *self, IRIAP_EVENT event, 
252                               struct sk_buff *skb) 
253 {
254         struct sk_buff *tx_skb;
255
256         ASSERT(self != NULL, return;);
257
258         switch (event) {
259         case IAP_CALL_REQUEST:
260                 tx_skb = self->skb;
261                 self->skb = NULL;
262                 
263                 irlmp_data_request(self->lsap, tx_skb);
264                 iriap_next_call_state(self, S_OUTSTANDING);
265                 break;
266         default:
267                 IRDA_DEBUG(0, "%s(), Unknown event %d\n", __FUNCTION__, event);
268                 break;
269         }
270         /* Cleanup time ! */
271         if (skb)
272                 dev_kfree_skb(skb);
273 }
274
275 /*
276  * Function state_s_calling (event, skb)
277  *
278  *    S-Calling
279  *
280  */
281 static void state_s_calling(struct iriap_cb *self, IRIAP_EVENT event, 
282                             struct sk_buff *skb) 
283 {
284         IRDA_DEBUG(0, "%s(), Not implemented\n", __FUNCTION__);
285 }
286
287 /*
288  * Function state_s_outstanding (event, skb)
289  *
290  *    S-Outstanding, The device is waiting for a response to a command
291  *
292  */
293 static void state_s_outstanding(struct iriap_cb *self, IRIAP_EVENT event, 
294                                 struct sk_buff *skb) 
295 {
296         ASSERT(self != NULL, return;);
297
298         switch (event) {
299         case IAP_RECV_F_LST:
300                 /*iriap_send_ack(self);*/
301                 /*LM_Idle_request(idle); */
302
303                 iriap_next_call_state(self, S_WAIT_FOR_CALL);
304                 break;
305         default:
306                 IRDA_DEBUG(0, "%s(), Unknown event %d\n", __FUNCTION__, event);
307                 break;
308         }               
309 }
310
311 /*
312  * Function state_s_replying (event, skb)
313  *
314  *    S-Replying, The device is collecting a multiple part response
315  */
316 static void state_s_replying(struct iriap_cb *self, IRIAP_EVENT event, 
317                              struct sk_buff *skb) 
318 {
319         IRDA_DEBUG(0, "%s(), Not implemented\n", __FUNCTION__);
320 }
321
322 /*
323  * Function state_s_wait_for_call (event, skb)
324  *
325  *    S-Wait-for-Call
326  *
327  */
328 static void state_s_wait_for_call(struct iriap_cb *self, IRIAP_EVENT event, 
329                                   struct sk_buff *skb) 
330 {
331         IRDA_DEBUG(0, "%s(), Not implemented\n", __FUNCTION__);
332 }
333
334
335 /*
336  * Function state_s_wait_active (event, skb)
337  *
338  *    S-Wait-Active
339  *
340  */
341 static void state_s_wait_active(struct iriap_cb *self, IRIAP_EVENT event, 
342                                 struct sk_buff *skb) 
343 {
344         IRDA_DEBUG(0, "%s(), Not implemented\n", __FUNCTION__);
345 }
346
347 /**************************************************************************
348  * 
349  *  Server FSM 
350  *
351  **************************************************************************/
352
353 /*
354  * Function state_r_disconnect (self, event, skb)
355  *
356  *    LM-IAS server is disconnected (not processing any requests!)
357  *
358  */
359 static void state_r_disconnect(struct iriap_cb *self, IRIAP_EVENT event, 
360                                struct sk_buff *skb) 
361 {
362         struct sk_buff *tx_skb;
363         
364         switch (event) {
365         case IAP_LM_CONNECT_INDICATION:
366                 tx_skb = dev_alloc_skb(64);
367                 if (tx_skb == NULL) {
368                         WARNING("%s(), unable to malloc!\n", __FUNCTION__);
369                         return;
370                 }
371
372                 /* Reserve space for MUX_CONTROL and LAP header */
373                 skb_reserve(tx_skb, LMP_MAX_HEADER);
374                 
375                 irlmp_connect_response(self->lsap, tx_skb);
376                 /*LM_Idle_request(idle); */
377                 
378                 iriap_next_server_state(self, R_CALL);
379                 
380                 /*
381                  *  Jump to R-Connect FSM, we skip R-Waiting since we do not
382                  *  care about LM_Idle_request()!
383                  */
384                 iriap_next_r_connect_state(self, R_RECEIVING);
385
386                 if (skb)
387                         dev_kfree_skb(skb);
388                 
389                 break;
390         default:
391                 IRDA_DEBUG(0, "%s(), unknown event %d\n", __FUNCTION__, event);
392                 break;
393         }               
394 }
395
396 /*
397  * Function state_r_call (self, event, skb)
398  *
399  *    
400  *
401  */
402 static void state_r_call(struct iriap_cb *self, IRIAP_EVENT event, 
403                          struct sk_buff *skb) 
404 {
405         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
406
407         switch (event) {
408         case IAP_LM_DISCONNECT_INDICATION:
409                 /* Abort call */
410                 iriap_next_server_state(self, R_DISCONNECT);
411                 iriap_next_r_connect_state(self, R_WAITING);            
412                 break;
413         default:
414                 IRDA_DEBUG(0, "%s(), unknown event!\n", __FUNCTION__);
415                 break;
416         }
417 }
418
419 /* 
420  *  R-Connect FSM 
421 */
422
423 /*
424  * Function state_r_waiting (self, event, skb)
425  *
426  *    
427  *
428  */
429 static void state_r_waiting(struct iriap_cb *self, IRIAP_EVENT event, 
430                             struct sk_buff *skb) 
431 {
432         IRDA_DEBUG(0, "%s(), Not implemented\n", __FUNCTION__);
433 }
434
435 static void state_r_wait_active(struct iriap_cb *self, IRIAP_EVENT event, 
436                                 struct sk_buff *skb) 
437 {
438         IRDA_DEBUG(0, "%s(), Not implemented\n", __FUNCTION__);
439 }
440
441 /*
442  * Function state_r_receiving (self, event, skb)
443  *
444  *    We are receiving a command
445  *
446  */
447 static void state_r_receiving(struct iriap_cb *self, IRIAP_EVENT event, 
448                               struct sk_buff *skb) 
449 {
450         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
451
452         switch (event) {
453         case IAP_RECV_F_LST:
454                 iriap_next_r_connect_state(self, R_EXECUTE);
455                         
456                 iriap_call_indication(self, skb);
457                 break;
458         default:
459                 IRDA_DEBUG(0, "%s(), unknown event!\n", __FUNCTION__);
460                 break;
461         }
462
463 }
464
465 /*
466  * Function state_r_execute (self, event, skb)
467  *
468  *    The server is processing the request
469  *
470  */
471 static void state_r_execute(struct iriap_cb *self, IRIAP_EVENT event, 
472                             struct sk_buff *skb) 
473 {
474         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
475
476         ASSERT(skb != NULL, return;);
477         
478         if (!self || self->magic != IAS_MAGIC) {
479                 IRDA_DEBUG(0, "%s(), bad pointer self\n", __FUNCTION__);
480                 return;
481         }       
482
483         switch (event) {
484         case IAP_CALL_RESPONSE:
485                 /*  
486                  *  Since we don't implement the Waiting state, we return
487                  *  to state Receiving instead, DB.
488                  */
489                 iriap_next_r_connect_state(self, R_RECEIVING);
490                         
491                 irlmp_data_request(self->lsap, skb);
492                 break;
493         default:
494                 IRDA_DEBUG(0, "%s(), unknown event!\n", __FUNCTION__);
495                 break;
496         }
497 }
498
499 static void state_r_returning(struct iriap_cb *self, IRIAP_EVENT event, 
500                               struct sk_buff *skb) 
501 {
502         IRDA_DEBUG(0, "%s(), event=%d\n", __FUNCTION__, event);
503
504         switch (event) {
505         case IAP_RECV_F_LST:
506                 
507                 break;
508         default:
509                 break;
510         }
511 }
512
513
514