import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / net / irda / irlap_event.c
1 /*********************************************************************
2  *                
3  * Filename:      irlap_event.c
4  * Version:       0.9
5  * Description:   IrLAP state machine implementation
6  * Status:        Experimental.
7  * Author:        Dag Brattli <dag@brattli.net>
8  * Created at:    Sat Aug 16 00:59:29 1997
9  * Modified at:   Sat Dec 25 21:07:57 1999
10  * Modified by:   Dag Brattli <dag@brattli.net>
11  * 
12  *     Copyright (c) 1998-2000 Dag Brattli <dag@brattli.net>,
13  *     Copyright (c) 1998      Thomas Davis <ratbert@radiks.net>
14  *     All Rights Reserved.
15  *     Copyright (c) 2000-2001 Jean Tourrilhes <jt@hpl.hp.com>
16  *     
17  *     This program is free software; you can redistribute it and/or 
18  *     modify it under the terms of the GNU General Public License as 
19  *     published by the Free Software Foundation; either version 2 of 
20  *     the License, or (at your option) any later version.
21  *
22  *     Neither Dag Brattli nor University of Tromsø admit liability nor
23  *     provide warranty for any of this software. This material is 
24  *     provided "AS-IS" and at no charge.
25  *
26  ********************************************************************/
27
28 #include <linux/config.h>
29 #include <linux/string.h>
30 #include <linux/kernel.h>
31 #include <linux/delay.h>
32 #include <linux/skbuff.h>
33
34 #include <net/irda/irda.h>
35 #include <net/irda/irlap_event.h>
36
37 #include <net/irda/timer.h>
38 #include <net/irda/irlap.h>
39 #include <net/irda/irlap_frame.h>
40 #include <net/irda/qos.h>
41 #include <net/irda/parameters.h>
42
43 #include <net/irda/irda_device.h>
44
45 #if CONFIG_IRDA_FAST_RR
46 int sysctl_fast_poll_increase = 50;
47 #endif
48
49 static int irlap_state_ndm    (struct irlap_cb *self, IRLAP_EVENT event, 
50                                struct sk_buff *skb, struct irlap_info *info);
51 static int irlap_state_query  (struct irlap_cb *self, IRLAP_EVENT event, 
52                                struct sk_buff *skb, struct irlap_info *info);
53 static int irlap_state_reply  (struct irlap_cb *self, IRLAP_EVENT event, 
54                                struct sk_buff *skb, struct irlap_info *info);
55 static int irlap_state_conn   (struct irlap_cb *self, IRLAP_EVENT event, 
56                                struct sk_buff *skb, struct irlap_info *info);
57 static int irlap_state_setup  (struct irlap_cb *self, IRLAP_EVENT event, 
58                                struct sk_buff *skb, struct irlap_info *info);
59 static int irlap_state_offline(struct irlap_cb *self, IRLAP_EVENT event, 
60                                struct sk_buff *skb, struct irlap_info *info);
61 static int irlap_state_xmit_p (struct irlap_cb *self, IRLAP_EVENT event, 
62                                struct sk_buff *skb, struct irlap_info *info);
63 static int irlap_state_pclose (struct irlap_cb *self, IRLAP_EVENT event, 
64                                struct sk_buff *skb, struct irlap_info *info);
65 static int irlap_state_nrm_p  (struct irlap_cb *self, IRLAP_EVENT event, 
66                                struct sk_buff *skb, struct irlap_info *info);
67 static int irlap_state_reset_wait(struct irlap_cb *self, IRLAP_EVENT event, 
68                                   struct sk_buff *skb, struct irlap_info *info);
69 static int irlap_state_reset  (struct irlap_cb *self, IRLAP_EVENT event, 
70                                struct sk_buff *skb, struct irlap_info *info);
71 static int irlap_state_nrm_s  (struct irlap_cb *self, IRLAP_EVENT event, 
72                                struct sk_buff *skb, struct irlap_info *info);
73 static int irlap_state_xmit_s (struct irlap_cb *self, IRLAP_EVENT event, 
74                                struct sk_buff *skb, struct irlap_info *info);
75 static int irlap_state_sclose (struct irlap_cb *self, IRLAP_EVENT event, 
76                                struct sk_buff *skb, struct irlap_info *info);
77 static int irlap_state_reset_check(struct irlap_cb *, IRLAP_EVENT event, 
78                                    struct sk_buff *, struct irlap_info *);
79
80 #ifdef CONFIG_IRDA_DEBUG
81 static const char *irlap_event[] = {
82         "DISCOVERY_REQUEST",
83         "CONNECT_REQUEST",
84         "CONNECT_RESPONSE",
85         "DISCONNECT_REQUEST",
86         "DATA_REQUEST",
87         "RESET_REQUEST",
88         "RESET_RESPONSE",
89         "SEND_I_CMD",
90         "SEND_UI_FRAME",
91         "RECV_DISCOVERY_XID_CMD",
92         "RECV_DISCOVERY_XID_RSP",
93         "RECV_SNRM_CMD",
94         "RECV_TEST_CMD",
95         "RECV_TEST_RSP",
96         "RECV_UA_RSP",
97         "RECV_DM_RSP",
98         "RECV_RD_RSP",
99         "RECV_I_CMD",
100         "RECV_I_RSP",
101         "RECV_UI_FRAME",
102         "RECV_FRMR_RSP",
103         "RECV_RR_CMD",
104         "RECV_RR_RSP",
105         "RECV_RNR_CMD",
106         "RECV_RNR_RSP",
107         "RECV_REJ_CMD",
108         "RECV_REJ_RSP",
109         "RECV_SREJ_CMD",
110         "RECV_SREJ_RSP",
111         "RECV_DISC_CMD",
112         "SLOT_TIMER_EXPIRED",
113         "QUERY_TIMER_EXPIRED",
114         "FINAL_TIMER_EXPIRED",
115         "POLL_TIMER_EXPIRED",
116         "DISCOVERY_TIMER_EXPIRED",
117         "WD_TIMER_EXPIRED",
118         "BACKOFF_TIMER_EXPIRED",
119         "MEDIA_BUSY_TIMER_EXPIRED",
120 };
121 #endif  /* CONFIG_IRDA_DEBUG */
122
123 const char *irlap_state[] = {
124         "LAP_NDM",
125         "LAP_QUERY",
126         "LAP_REPLY",
127         "LAP_CONN",
128         "LAP_SETUP",
129         "LAP_OFFLINE",
130         "LAP_XMIT_P",
131         "LAP_PCLOSE",
132         "LAP_NRM_P",
133         "LAP_RESET_WAIT",
134         "LAP_RESET",
135         "LAP_NRM_S",
136         "LAP_XMIT_S",
137         "LAP_SCLOSE",
138         "LAP_RESET_CHECK",
139 };
140
141 static int (*state[])(struct irlap_cb *self, IRLAP_EVENT event, 
142                       struct sk_buff *skb, struct irlap_info *info) = 
143 {
144         irlap_state_ndm,
145         irlap_state_query,
146         irlap_state_reply,
147         irlap_state_conn,
148         irlap_state_setup,
149         irlap_state_offline,
150         irlap_state_xmit_p,
151         irlap_state_pclose,
152         irlap_state_nrm_p,
153         irlap_state_reset_wait,
154         irlap_state_reset,
155         irlap_state_nrm_s,
156         irlap_state_xmit_s,
157         irlap_state_sclose,
158         irlap_state_reset_check,
159 };
160
161 /*
162  * Function irda_poll_timer_expired (data)
163  *
164  *    Poll timer has expired. Normally we must now send a RR frame to the
165  *    remote device
166  */
167 static void irlap_poll_timer_expired(void *data)
168 {
169         struct irlap_cb *self = (struct irlap_cb *) data;
170         
171         ASSERT(self != NULL, return;);
172         ASSERT(self->magic == LAP_MAGIC, return;);
173         
174         irlap_do_event(self, POLL_TIMER_EXPIRED, NULL, NULL);
175 }
176
177 /*
178  * Calculate and set time before we will have to send back the pf bit
179  * to the peer. Use in primary.
180  * Make sure that state is XMIT_P/XMIT_S when calling this function
181  * (and that nobody messed up with the state). - Jean II
182  */
183 void irlap_start_poll_timer(struct irlap_cb *self, int timeout)
184 {
185         ASSERT(self != NULL, return;);
186         ASSERT(self->magic == LAP_MAGIC, return;);
187
188 #ifdef CONFIG_IRDA_FAST_RR
189         /* 
190          * Send out the RR frames faster if our own transmit queue is empty, or
191          * if the peer is busy. The effect is a much faster conversation
192          */
193         if ((skb_queue_len(&self->txq) == 0) || (self->remote_busy)) {
194                 if (self->fast_RR == TRUE) {
195                         /*
196                          *  Assert that the fast poll timer has not reached the
197                          *  normal poll timer yet
198                          */
199                         if (self->fast_RR_timeout < timeout) {
200                                 /*
201                                  *  FIXME: this should be a more configurable
202                                  *         function
203                                  */
204                                 self->fast_RR_timeout += 
205                                         (sysctl_fast_poll_increase * HZ/1000);
206
207                                 /* Use this fast(er) timeout instead */
208                                 timeout = self->fast_RR_timeout;
209                         }
210                 } else {
211                         self->fast_RR = TRUE;
212
213                         /* Start with just 0 ms */
214                         self->fast_RR_timeout = 0;
215                         timeout = 0;
216                 }
217         } else
218                 self->fast_RR = FALSE;
219
220         IRDA_DEBUG(3, "%s(), timeout=%d (%ld)\n", __FUNCTION__, timeout, jiffies);
221 #endif /* CONFIG_IRDA_FAST_RR */
222
223         if (timeout == 0)
224                 irlap_do_event(self, POLL_TIMER_EXPIRED, NULL, NULL);
225         else
226                 irda_start_timer(&self->poll_timer, timeout, self, 
227                                  irlap_poll_timer_expired);
228 }
229
230 /*
231  * Function irlap_do_event (event, skb, info)
232  *
233  *    Rushes through the state machine without any delay. If state == XMIT
234  *    then send queued data frames. 
235  */
236 void irlap_do_event(struct irlap_cb *self, IRLAP_EVENT event, 
237                     struct sk_buff *skb, struct irlap_info *info) 
238 {
239         int ret;
240         
241         if (!self || self->magic != LAP_MAGIC)
242                 return;
243
244         IRDA_DEBUG(3, "%s(), event = %s, state = %s\n", __FUNCTION__,
245                    irlap_event[event], irlap_state[self->state]); 
246         
247         ret = (*state[self->state])(self, event, skb, info);
248
249         /* 
250          *  Check if there are any pending events that needs to be executed
251          */
252         switch (self->state) {
253         case LAP_XMIT_P: /* FALLTHROUGH */
254         case LAP_XMIT_S:
255                 /* 
256                  * We just received the pf bit and are at the beginning
257                  * of a new LAP transmit window.
258                  * Check if there are any queued data frames, and do not
259                  * try to disconnect link if we send any data frames, since
260                  * that will change the state away form XMIT
261                  */
262                 IRDA_DEBUG(2, "%s() : queue len = %d\n", __FUNCTION__,
263                            skb_queue_len(&self->txq));
264
265                 if (skb_queue_len(&self->txq)) {
266                         /* Prevent race conditions with irlap_data_request() */
267                         self->local_busy = TRUE;
268
269                         /* Theory of operation.
270                          * We send frames up to when we fill the window or
271                          * reach line capacity. Those frames will queue up
272                          * in the device queue, and the driver will slowly
273                          * send them.
274                          * After each frame that we send, we poll the higher
275                          * layer for more data. It's the right time to do
276                          * that because the link layer need to perform the mtt
277                          * and then send the first frame, so we can afford
278                          * to send a bit of time in kernel space.
279                          * The explicit flow indication allow to minimise
280                          * buffers (== lower latency), to avoid higher layer
281                          * polling via timers (== less context switches) and
282                          * to implement a crude scheduler - Jean II */
283
284                         /* Try to send away all queued data frames */
285                         while ((skb = skb_dequeue(&self->txq)) != NULL) {
286                                 /* Send one frame */
287                                 ret = (*state[self->state])(self, SEND_I_CMD,
288                                                             skb, NULL);
289                                 kfree_skb(skb);
290
291                                 /* Poll the higher layers for one more frame */
292                                 irlmp_flow_indication(self->notify.instance,
293                                                       FLOW_START);
294
295                                 if (ret == -EPROTO)
296                                         break; /* Try again later! */
297                         }
298                         /* Finished transmitting */
299                         self->local_busy = FALSE;
300                 } else if (self->disconnect_pending) {
301                         self->disconnect_pending = FALSE;
302                         
303                         ret = (*state[self->state])(self, DISCONNECT_REQUEST,
304                                                     NULL, NULL);
305                 }
306                 break;
307 /*      case LAP_NDM: */
308 /*      case LAP_CONN: */
309 /*      case LAP_RESET_WAIT: */
310 /*      case LAP_RESET_CHECK: */
311         default:
312                 break;
313         }
314 }
315
316 /*
317  * Function irlap_next_state (self, state)
318  *
319  *    Switches state and provides debug information
320  *
321  */
322 static inline void irlap_next_state(struct irlap_cb *self, IRLAP_STATE state) 
323 {       
324         /*
325         if (!self || self->magic != LAP_MAGIC)
326                 return;
327         
328         IRDA_DEBUG(4, "next LAP state = %s\n", irlap_state[state]);
329         */
330         self->state = state;
331 }
332
333 /*
334  * Function irlap_state_ndm (event, skb, frame)
335  *
336  *    NDM (Normal Disconnected Mode) state
337  *
338  */
339 static int irlap_state_ndm(struct irlap_cb *self, IRLAP_EVENT event, 
340                            struct sk_buff *skb, struct irlap_info *info) 
341 {
342         discovery_t *discovery_rsp;
343         int ret = 0;
344
345         ASSERT(self != NULL, return -1;);
346         ASSERT(self->magic == LAP_MAGIC, return -1;);
347
348         switch (event) {
349         case CONNECT_REQUEST:
350                 ASSERT(self->netdev != NULL, return -1;);
351
352                 if (self->media_busy) {
353                         /* Note : this will never happen, because we test
354                          * media busy in irlap_connect_request() and
355                          * postpone the event... - Jean II */
356                         IRDA_DEBUG(0, "%s(), CONNECT_REQUEST: media busy!\n", __FUNCTION__);
357                         
358                         /* Always switch state before calling upper layers */
359                         irlap_next_state(self, LAP_NDM);
360                         
361                         irlap_disconnect_indication(self, LAP_MEDIA_BUSY);
362                 } else {
363                         irlap_send_snrm_frame(self, &self->qos_rx);
364                         
365                         /* Start Final-bit timer */
366                         irlap_start_final_timer(self, self->final_timeout);
367
368                         self->retry_count = 0;
369                         irlap_next_state(self, LAP_SETUP);
370                 }
371                 break;
372         case RECV_SNRM_CMD:
373                 /* Check if the frame contains and I field */
374                 if (info) {                    
375                         self->daddr = info->daddr;
376                         self->caddr = info->caddr;
377                         
378                         irlap_next_state(self, LAP_CONN);
379
380                         irlap_connect_indication(self, skb);
381                 } else {
382                         IRDA_DEBUG(0, "%s(), SNRM frame does not "
383                                    "contain an I field!\n", __FUNCTION__);
384                 }
385                 break;
386         case DISCOVERY_REQUEST:         
387                 ASSERT(info != NULL, return -1;);
388
389                 if (self->media_busy) {
390                         IRDA_DEBUG(0, "%s(), media busy!\n", __FUNCTION__); 
391                         /* irlap->log.condition = MEDIA_BUSY; */
392                                                 
393                         /* This will make IrLMP try again */
394                         irlap_discovery_confirm(self, NULL);
395                         /* Note : the discovery log is not cleaned up here,
396                          * it will be done in irlap_discovery_request()
397                          * Jean II */
398                         return 0;
399                 } 
400                 
401                 self->S = info->S;
402                 self->s = info->s;
403                 irlap_send_discovery_xid_frame(self, info->S, info->s, TRUE,
404                                                info->discovery);
405                 self->frame_sent = FALSE;
406                 self->s++;
407
408                 irlap_start_slot_timer(self, self->slot_timeout);
409                 irlap_next_state(self, LAP_QUERY);
410                 break;
411         case RECV_DISCOVERY_XID_CMD:
412                 ASSERT(info != NULL, return -1;);
413
414                 /* Assert that this is not the final slot */
415                 if (info->s <= info->S) {
416                         self->slot = irlap_generate_rand_time_slot(info->S,
417                                                                    info->s);
418                         if (self->slot == info->s) {
419                                 discovery_rsp = irlmp_get_discovery_response();
420                                 discovery_rsp->daddr = info->daddr;
421                                 
422                                 irlap_send_discovery_xid_frame(self, info->S, 
423                                                                self->slot, 
424                                                                FALSE,
425                                                                discovery_rsp);
426                                 self->frame_sent = TRUE;
427                         } else
428                                 self->frame_sent = FALSE;
429                         
430                         /* 
431                          * Remember to multiply the query timeout value with 
432                          * the number of slots used
433                          */
434                         irlap_start_query_timer(self, QUERY_TIMEOUT*info->S);
435                         irlap_next_state(self, LAP_REPLY);
436                 } else {
437                 /* This is the final slot. How is it possible ?
438                  * This would happen is both discoveries are just slightly
439                  * offset (if they are in sync, all packets are lost).
440                  * Most often, all the discovery requests will be received
441                  * in QUERY state (see my comment there), except for the
442                  * last frame that will come here.
443                  * The big trouble when it happen is that active discovery
444                  * doesn't happen, because nobody answer the discoveries
445                  * frame of the other guy, so the log shows up empty.
446                  * What should we do ?
447                  * Not much. It's too late to answer those discovery frames,
448                  * so we just pass the info to IrLMP who will put it in the
449                  * log (and post an event).
450                  * Jean II
451                  */
452                         IRDA_DEBUG(1, "%s(), Receiving final discovery request, missed the discovery slots :-(\n", __FUNCTION__);
453
454                         /* Last discovery request -> in the log */
455                         irlap_discovery_indication(self, info->discovery); 
456                 }
457                 break;
458         case MEDIA_BUSY_TIMER_EXPIRED:
459                 /* A bunch of events may be postponed because the media is
460                  * busy (usually immediately after we close a connection),
461                  * or while we are doing discovery (state query/reply).
462                  * In all those cases, the media busy flag will be cleared
463                  * when it's OK for us to process those postponed events.
464                  * This event is not mentioned in the state machines in the
465                  * IrLAP spec. It's because they didn't consider Ultra and
466                  * postponing connection request is optional.
467                  * Jean II */
468 #ifdef CONFIG_IRDA_ULTRA
469                 /* Send any pending Ultra frames if any */
470                 if (!skb_queue_empty(&self->txq_ultra)) {
471                         /* We don't send the frame, just post an event.
472                          * Also, previously this code was in timer.c...
473                          * Jean II */
474                         ret = (*state[self->state])(self, SEND_UI_FRAME, 
475                                                     NULL, NULL);
476                 }
477 #endif /* CONFIG_IRDA_ULTRA */
478                 /* Check if we should try to connect.
479                  * This code was previously in irlap_do_event() */
480                 if (self->connect_pending) {
481                         self->connect_pending = FALSE;
482
483                         /* This one *should* not pend in this state, except
484                          * if a socket try to connect and immediately
485                          * disconnect. - clear - Jean II */
486                         if (self->disconnect_pending)
487                                 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
488                         else
489                                 ret = (*state[self->state])(self,
490                                                             CONNECT_REQUEST, 
491                                                             NULL, NULL);
492                         self->disconnect_pending = FALSE;
493                 }
494                 /* Note : one way to test if this code works well (including
495                  * media busy and small busy) is to create a user space
496                  * application generating an Ultra packet every 3.05 sec (or
497                  * 2.95 sec) and to see how it interact with discovery.
498                  * It's fairly easy to check that no packet is lost, that the
499                  * packets are postponed during discovery and that after
500                  * discovery indication you have a 100ms "gap".
501                  * As connection request and Ultra are now processed the same
502                  * way, this avoid the tedious job of trying IrLAP connection
503                  * in all those cases...
504                  * Jean II */
505                 break;
506 #ifdef CONFIG_IRDA_ULTRA
507         case SEND_UI_FRAME:
508         {   
509                 int i;
510                 /* Only allowed to repeat an operation twice */
511                 for (i=0; ((i<2) && (self->media_busy == FALSE)); i++) {
512                         skb = skb_dequeue(&self->txq_ultra);
513                         if (skb)
514                                 irlap_send_ui_frame(self, skb, CBROADCAST, 
515                                                     CMD_FRAME);
516                         else
517                                 break;
518                 }
519                 if (i == 2) {
520                         /* Force us to listen 500 ms again */
521                         irda_device_set_media_busy(self->netdev, TRUE);
522                 }
523                 break;
524         }
525         case RECV_UI_FRAME:
526                 /* Only accept broadcast frames in NDM mode */
527                 if (info->caddr != CBROADCAST) {
528                         IRDA_DEBUG(0, "%s(), not a broadcast frame!\n", __FUNCTION__);
529                 } else
530                         irlap_unitdata_indication(self, skb);
531                 break;
532 #endif /* CONFIG_IRDA_ULTRA */
533         case RECV_TEST_CMD:
534                 /* Remove test frame header */
535                 skb_pull(skb, sizeof(struct test_frame));
536
537                 /* 
538                  * Send response. This skb will not be sent out again, and
539                  * will only be used to send out the same info as the cmd
540                  */
541                 irlap_send_test_frame(self, CBROADCAST, info->daddr, skb);
542                 break;
543         case RECV_TEST_RSP:
544                 IRDA_DEBUG(0, "%s() not implemented!\n", __FUNCTION__);
545                 break;
546         default:
547                 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
548                            irlap_event[event]);
549                 
550                 ret = -1;
551                 break;
552         }       
553         return ret;
554 }
555
556 /*
557  * Function irlap_state_query (event, skb, info)
558  *
559  *    QUERY state
560  *
561  */
562 static int irlap_state_query(struct irlap_cb *self, IRLAP_EVENT event, 
563                              struct sk_buff *skb, struct irlap_info *info) 
564 {
565         int ret = 0;
566
567         ASSERT(self != NULL, return -1;);
568         ASSERT(self->magic == LAP_MAGIC, return -1;);
569
570         switch (event) {
571         case RECV_DISCOVERY_XID_RSP:
572                 ASSERT(info != NULL, return -1;);
573                 ASSERT(info->discovery != NULL, return -1;);
574
575                 IRDA_DEBUG(4, "%s(), daddr=%08x\n", __FUNCTION__,
576                            info->discovery->daddr);
577
578                 if (!self->discovery_log) {
579                         WARNING("%s(), discovery log is gone! "
580                                 "maybe the discovery timeout has been set to "
581                                 "short?\n", __FUNCTION__);
582                         break;
583                 }
584                 hashbin_insert(self->discovery_log, 
585                                (irda_queue_t *) info->discovery,
586                                info->discovery->daddr, NULL);
587
588                 /* Keep state */
589                 /* irlap_next_state(self, LAP_QUERY);  */
590
591                 break;
592         case RECV_DISCOVERY_XID_CMD:
593                 /* Yes, it is possible to receive those frames in this mode.
594                  * Note that most often the last discovery request won't
595                  * occur here but in NDM state (see my comment there).
596                  * What should we do ?
597                  * Not much. We are currently performing our own discovery,
598                  * therefore we can't answer those frames. We don't want
599                  * to change state either. We just pass the info to
600                  * IrLMP who will put it in the log (and post an event).
601                  * Jean II
602                  */
603
604                 ASSERT(info != NULL, return -1;);
605
606                 IRDA_DEBUG(1, "%s(), Receiving discovery request (s = %d) while performing discovery :-(\n", __FUNCTION__, info->s);
607
608                 /* Last discovery request ? */
609                 if (info->s == 0xff)
610                         irlap_discovery_indication(self, info->discovery); 
611                 break;
612         case SLOT_TIMER_EXPIRED:
613                 /*
614                  * Wait a little longer if we detect an incoming frame. This
615                  * is not mentioned in the spec, but is a good thing to do, 
616                  * since we want to work even with devices that violate the
617                  * timing requirements.
618                  */
619                 if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
620                         IRDA_DEBUG(2, "%s(), device is slow to answer, "
621                                    "waiting some more!\n", __FUNCTION__);
622                         irlap_start_slot_timer(self, MSECS_TO_JIFFIES(10));
623                         self->add_wait = TRUE;
624                         return ret;
625                 }
626                 self->add_wait = FALSE;
627
628                 if (self->s < self->S) {
629                         irlap_send_discovery_xid_frame(self, self->S, 
630                                                        self->s, TRUE,
631                                                        self->discovery_cmd);
632                         self->s++;
633                         irlap_start_slot_timer(self, self->slot_timeout);
634                         
635                         /* Keep state */
636                         irlap_next_state(self, LAP_QUERY);
637                 } else {
638                         /* This is the final slot! */
639                         irlap_send_discovery_xid_frame(self, self->S, 0xff, 
640                                                        TRUE,
641                                                        self->discovery_cmd);
642
643                         /* Always switch state before calling upper layers */
644                         irlap_next_state(self, LAP_NDM);
645         
646                         /*
647                          *  We are now finished with the discovery procedure, 
648                          *  so now we must return the results
649                          */
650                         irlap_discovery_confirm(self, self->discovery_log);
651
652                         /* IrLMP should now have taken care of the log */
653                         self->discovery_log = NULL;
654                 }
655                 break;
656         default:
657                 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
658                            irlap_event[event]);
659
660                 ret = -1;
661                 break;
662         }
663         return ret;
664 }
665
666 /*
667  * Function irlap_state_reply (self, event, skb, info)
668  *
669  *    REPLY, we have received a XID discovery frame from a device and we
670  *    are waiting for the right time slot to send a response XID frame
671  * 
672  */
673 static int irlap_state_reply(struct irlap_cb *self, IRLAP_EVENT event, 
674                              struct sk_buff *skb, struct irlap_info *info) 
675 {
676         discovery_t *discovery_rsp;
677         int ret=0;
678
679         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
680
681         ASSERT(self != NULL, return -1;);
682         ASSERT(self->magic == LAP_MAGIC, return -1;);
683
684         switch (event) {
685         case QUERY_TIMER_EXPIRED:
686                 IRDA_DEBUG(2, "%s(), QUERY_TIMER_EXPIRED <%ld>\n", __FUNCTION__,
687                       jiffies);
688                 irlap_next_state(self, LAP_NDM);
689                 break;
690         case RECV_DISCOVERY_XID_CMD:
691                 ASSERT(info != NULL, return -1;);
692                 /* Last frame? */
693                 if (info->s == 0xff) {
694                         del_timer(&self->query_timer);
695                         
696                         /* info->log.condition = REMOTE; */
697
698                         /* Always switch state before calling upper layers */
699                         irlap_next_state(self, LAP_NDM);
700
701                         irlap_discovery_indication(self, info->discovery); 
702                 } else if ((info->s >= self->slot) && (!self->frame_sent)) {
703                         discovery_rsp = irlmp_get_discovery_response();
704                         discovery_rsp->daddr = info->daddr;
705
706                         irlap_send_discovery_xid_frame(self, info->S,
707                                                        self->slot, FALSE,
708                                                        discovery_rsp);
709                         
710                         self->frame_sent = TRUE;
711                         irlap_next_state(self, LAP_REPLY);
712                 }
713                 break;
714         default:
715                 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __FUNCTION__, event,
716                            irlap_event[event]);
717
718                 ret = -1;
719                 break;
720         }
721         return ret;
722 }
723
724 /*
725  * Function irlap_state_conn (event, skb, info)
726  *
727  *    CONN, we have received a SNRM command and is waiting for the upper
728  *    layer to accept or refuse connection 
729  *
730  */
731 static int irlap_state_conn(struct irlap_cb *self, IRLAP_EVENT event, 
732                             struct sk_buff *skb, struct irlap_info *info) 
733 {
734         int ret = 0;
735
736         IRDA_DEBUG(4, "%s(), event=%s\n", __FUNCTION__, irlap_event[ event]);
737
738         ASSERT(self != NULL, return -1;);
739         ASSERT(self->magic == LAP_MAGIC, return -1;);
740
741         switch (event) {
742         case CONNECT_RESPONSE:
743                 skb_pull(skb, sizeof(struct snrm_frame));
744
745                 ASSERT(self->netdev != NULL, return -1;);
746
747                 irlap_qos_negotiate(self, skb);
748
749                 irlap_initiate_connection_state(self);
750
751                 /* 
752                  * Applying the parameters now will make sure we change speed
753                  * *after* we have sent the next frame
754                  */
755                 irlap_apply_connection_parameters(self, FALSE);
756
757                 /* 
758                  * Sending this frame will force a speed change after it has
759                  * been sent (i.e. the frame will be sent at 9600).
760                  */
761                 irlap_send_ua_response_frame(self, &self->qos_rx);
762
763 #if 0
764                 /* 
765                  * We are allowed to send two frames, but this may increase
766                  * the connect latency, so lets not do it for now.
767                  */
768                 /* This is full of good intentions, but doesn't work in
769                  * practice.
770                  * After sending the first UA response, we switch the
771                  * dongle to the negociated speed, which is usually
772                  * different than 9600 kb/s.
773                  * From there, there is two solutions :
774                  * 1) The other end has received the first UA response :
775                  * it will set up the connection, move to state LAP_NRM_P,
776                  * and will ignore and drop the second UA response.
777                  * Actually, it's even worse : the other side will almost
778                  * immediately send a RR that will likely collide with the
779                  * UA response (depending on negociated turnaround).
780                  * 2) The other end has not received the first UA response,
781                  * will stay at 9600 and will never see the second UA response.
782                  * Jean II */
783                 irlap_send_ua_response_frame(self, &self->qos_rx);
784 #endif
785
786                 /*
787                  *  The WD-timer could be set to the duration of the P-timer 
788                  *  for this case, but it is recommended to use twice the 
789                  *  value (note 3 IrLAP p. 60). 
790                  */
791                 irlap_start_wd_timer(self, self->wd_timeout);
792                 irlap_next_state(self, LAP_NRM_S);
793
794                 break;
795         case RECV_DISCOVERY_XID_CMD:
796                 IRDA_DEBUG(3, "%s(), event RECV_DISCOVER_XID_CMD!\n", __FUNCTION__);
797                 irlap_next_state(self, LAP_NDM);
798
799                 break;          
800         case DISCONNECT_REQUEST:
801                 IRDA_DEBUG(0, "%s(), Disconnect request!\n", __FUNCTION__);
802                 irlap_send_dm_frame(self);
803                 irlap_next_state( self, LAP_NDM);
804                 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
805                 break;
806         default:
807                 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __FUNCTION__, event,
808                            irlap_event[event]);
809                 
810                 ret = -1;
811                 break;
812         }
813         
814         return ret;
815 }
816
817 /*
818  * Function irlap_state_setup (event, skb, frame)
819  *
820  *    SETUP state, The local layer has transmitted a SNRM command frame to
821  *    a remote peer layer and is awaiting a reply .
822  *
823  */
824 static int irlap_state_setup(struct irlap_cb *self, IRLAP_EVENT event, 
825                              struct sk_buff *skb, struct irlap_info *info) 
826 {
827         int ret = 0;
828
829         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
830         
831         ASSERT(self != NULL, return -1;);
832         ASSERT(self->magic == LAP_MAGIC, return -1;);
833
834         switch (event) {
835         case FINAL_TIMER_EXPIRED:
836                 if (self->retry_count < self->N3) {
837 /* 
838  *  Perform random backoff, Wait a random number of time units, minimum 
839  *  duration half the time taken to transmitt a SNRM frame, maximum duration 
840  *  1.5 times the time taken to transmit a SNRM frame. So this time should 
841  *  between 15 msecs and 45 msecs.
842  */
843                         irlap_start_backoff_timer(self, MSECS_TO_JIFFIES(20 + 
844                                                         (jiffies % 30)));
845                 } else {
846                         /* Always switch state before calling upper layers */
847                         irlap_next_state(self, LAP_NDM);
848
849                         irlap_disconnect_indication(self, LAP_FOUND_NONE);
850                 }
851                 break;
852         case BACKOFF_TIMER_EXPIRED:
853                 irlap_send_snrm_frame(self, &self->qos_rx);
854                 irlap_start_final_timer(self, self->final_timeout);
855                 self->retry_count++;
856                 break;
857         case RECV_SNRM_CMD:
858                 IRDA_DEBUG(4, "%s(), SNRM battle!\n", __FUNCTION__);
859
860                 ASSERT(skb != NULL, return 0;);
861                 ASSERT(info != NULL, return 0;);
862
863                 /*
864                  *  The device with the largest device address wins the battle
865                  *  (both have sent a SNRM command!)
866                  */
867                 if (info &&(info->daddr > self->saddr)) {
868                         del_timer(&self->final_timer);
869                         irlap_initiate_connection_state(self);
870
871                         ASSERT(self->netdev != NULL, return -1;);
872
873                         skb_pull(skb, sizeof(struct snrm_frame));
874
875                         irlap_qos_negotiate(self, skb);
876                         
877                         /* Send UA frame and then change link settings */
878                         irlap_apply_connection_parameters(self, FALSE);
879                         irlap_send_ua_response_frame(self, &self->qos_rx);
880
881                         irlap_next_state(self, LAP_NRM_S);
882                         irlap_connect_confirm(self, skb);
883                         
884                         /* 
885                          *  The WD-timer could be set to the duration of the
886                          *  P-timer for this case, but it is recommended
887                          *  to use twice the value (note 3 IrLAP p. 60).  
888                          */
889                         irlap_start_wd_timer(self, self->wd_timeout);
890                 } else {
891                         /* We just ignore the other device! */
892                         irlap_next_state(self, LAP_SETUP);
893                 }
894                 break;
895         case RECV_UA_RSP:
896                 /* Stop F-timer */
897                 del_timer(&self->final_timer);
898
899                 /* Initiate connection state */
900                 irlap_initiate_connection_state(self);
901
902                 /* Negotiate connection parameters */
903                 ASSERT(skb->len > 10, return -1;);
904
905                 skb_pull(skb, sizeof(struct ua_frame));
906
907                 ASSERT(self->netdev != NULL, return -1;);
908
909                 irlap_qos_negotiate(self, skb);
910
911                 /* Set the new link setting *now* (before the rr frame) */
912                 irlap_apply_connection_parameters(self, TRUE);
913                 self->retry_count = 0;
914                 
915                 /* Wait for turnaround time to give a chance to the other
916                  * device to be ready to receive us.
917                  * Note : the time to switch speed is typically larger
918                  * than the turnaround time, but as we don't have the other
919                  * side speed switch time, that's our best guess...
920                  * Jean II */
921                 irlap_wait_min_turn_around(self, &self->qos_tx);
922
923                 /* This frame will actually be sent at the new speed */
924                 irlap_send_rr_frame(self, CMD_FRAME);
925
926                 irlap_start_final_timer(self, self->final_timeout/2);
927                 irlap_next_state(self, LAP_NRM_P);
928
929                 irlap_connect_confirm(self, skb);
930                 break;
931         case RECV_DM_RSP:     /* FALLTHROUGH */
932         case RECV_DISC_CMD: 
933                 del_timer(&self->final_timer);
934                 irlap_next_state(self, LAP_NDM);
935
936                 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
937                 break;
938         default:
939                 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __FUNCTION__, event,
940                            irlap_event[event]);         
941
942                 ret = -1;
943                 break;
944         }       
945         return ret;
946 }
947
948 /*
949  * Function irlap_state_offline (self, event, skb, info)
950  *
951  *    OFFLINE state, not used for now!
952  *
953  */
954 static int irlap_state_offline(struct irlap_cb *self, IRLAP_EVENT event, 
955                                struct sk_buff *skb, struct irlap_info *info) 
956 {
957         IRDA_DEBUG( 0, "%s(), Unknown event\n", __FUNCTION__);
958
959         return -1;
960 }
961
962 /*
963  * Function irlap_state_xmit_p (self, event, skb, info)
964  * 
965  *    XMIT, Only the primary station has right to transmit, and we
966  *    therefore do not expect to receive any transmissions from other
967  *    stations.
968  * 
969  */
970 static int irlap_state_xmit_p(struct irlap_cb *self, IRLAP_EVENT event, 
971                               struct sk_buff *skb, struct irlap_info *info) 
972 {
973         int ret = 0;
974         
975         switch (event) {
976         case SEND_I_CMD:
977                 /*
978                  *  Only send frame if send-window > 0.
979                  */ 
980                 if ((self->window > 0) && (!self->remote_busy)) {
981 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
982                         /*
983                          *  Test if we have transmitted more bytes over the 
984                          *  link than its possible to do with the current 
985                          *  speed and turn-around-time.
986                          */
987                         if (skb->len > self->bytes_left) {
988                                 IRDA_DEBUG(4, "%s(), Not allowed to transmit more "
989                                            "bytes!\n", __FUNCTION__);
990                                 skb_queue_head(&self->txq, skb_get(skb));
991                                 /*
992                                  *  We should switch state to LAP_NRM_P, but
993                                  *  that is not possible since we must be sure
994                                  *  that we poll the other side. Since we have
995                                  *  used up our time, the poll timer should
996                                  *  trigger anyway now, so we just wait for it
997                                  *  DB
998                                  */
999                                 return -EPROTO;
1000                         }
1001                         self->bytes_left -= skb->len;
1002 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1003                         /*
1004                          *  Send data with poll bit cleared only if window > 1
1005                          *  and there is more frames after this one to be sent
1006                          */
1007                         if ((self->window > 1) && 
1008                             skb_queue_len( &self->txq) > 0) 
1009                         {   
1010                                 irlap_send_data_primary(self, skb);
1011                                 irlap_next_state(self, LAP_XMIT_P);
1012                         } else {
1013                                 irlap_send_data_primary_poll(self, skb);
1014                                 irlap_next_state(self, LAP_NRM_P);
1015                                 
1016                                 /* 
1017                                  * Make sure state machine does not try to send
1018                                  * any more frames 
1019                                  */
1020                                 ret = -EPROTO;
1021                         }
1022 #ifdef CONFIG_IRDA_FAST_RR
1023                         /* Peer may want to reply immediately */
1024                         self->fast_RR = FALSE;
1025 #endif /* CONFIG_IRDA_FAST_RR */
1026                 } else {
1027                         IRDA_DEBUG(4, "%s(), Unable to send! remote busy?\n", __FUNCTION__);
1028                         skb_queue_head(&self->txq, skb_get(skb));
1029
1030                         /*
1031                          *  The next ret is important, because it tells 
1032                          *  irlap_next_state _not_ to deliver more frames
1033                          */
1034                         ret = -EPROTO;
1035                 }
1036                 break;
1037         case POLL_TIMER_EXPIRED:
1038                 IRDA_DEBUG(3, "%s(), POLL_TIMER_EXPIRED (%ld)\n", __FUNCTION__,
1039                            jiffies);
1040                 irlap_send_rr_frame(self, CMD_FRAME);
1041                 /* Return to NRM properly - Jean II  */
1042                 self->window = self->window_size;
1043 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1044                 /* Allowed to transmit a maximum number of bytes again. */
1045                 self->bytes_left = self->line_capacity;
1046 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1047                 irlap_start_final_timer(self, self->final_timeout);
1048                 irlap_next_state(self, LAP_NRM_P);
1049                 break;
1050         case DISCONNECT_REQUEST:
1051                 del_timer(&self->poll_timer);
1052                 irlap_wait_min_turn_around(self, &self->qos_tx);
1053                 irlap_send_disc_frame(self);
1054                 irlap_flush_all_queues(self);
1055                 irlap_start_final_timer(self, self->final_timeout);
1056                 self->retry_count = 0;
1057                 irlap_next_state(self, LAP_PCLOSE);
1058                 break;
1059         case DATA_REQUEST:
1060                 /* Nothing to do, irlap_do_event() will send the packet
1061                  * when we return... - Jean II */
1062                 break;
1063         default:
1064                 IRDA_DEBUG(0, "%s(), Unknown event %s\n", __FUNCTION__,
1065                            irlap_event[event]);
1066
1067                 ret = -EINVAL;
1068                 break;
1069         }
1070         return ret;
1071 }
1072
1073 /*
1074  * Function irlap_state_pclose (event, skb, info)
1075  *
1076  *    PCLOSE state
1077  */
1078 static int irlap_state_pclose(struct irlap_cb *self, IRLAP_EVENT event, 
1079                               struct sk_buff *skb, struct irlap_info *info) 
1080 {
1081         int ret = 0;
1082
1083         IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1084         
1085         ASSERT(self != NULL, return -1;);
1086         ASSERT(self->magic == LAP_MAGIC, return -1;);   
1087
1088         switch (event) {
1089         case RECV_UA_RSP: /* FALLTHROUGH */
1090         case RECV_DM_RSP:
1091                 del_timer(&self->final_timer);
1092
1093                 /* Set new link parameters */
1094                 irlap_apply_default_connection_parameters(self);
1095
1096                 /* Always switch state before calling upper layers */
1097                 irlap_next_state(self, LAP_NDM);
1098                 
1099                 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
1100                 break;
1101         case FINAL_TIMER_EXPIRED:
1102                 if (self->retry_count < self->N3) {
1103                         irlap_wait_min_turn_around(self, &self->qos_tx);
1104                         irlap_send_disc_frame(self);
1105                         irlap_start_final_timer(self, self->final_timeout);
1106                         self->retry_count++;
1107                         /* Keep state */
1108                 } else {
1109                         irlap_apply_default_connection_parameters(self);
1110
1111                         /*  Always switch state before calling upper layers */
1112                         irlap_next_state(self, LAP_NDM);
1113
1114                         irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1115                 }
1116                 break;
1117         default:
1118                 IRDA_DEBUG(1, "%s(), Unknown event %d\n", __FUNCTION__, event);
1119
1120                 ret = -1;
1121                 break;  
1122         }
1123         return ret;
1124 }
1125
1126 /*
1127  * Function irlap_state_nrm_p (self, event, skb, info)
1128  *
1129  *   NRM_P (Normal Response Mode as Primary), The primary station has given
1130  *   permissions to a secondary station to transmit IrLAP resonse frames
1131  *   (by sending a frame with the P bit set). The primary station will not
1132  *   transmit any frames and is expecting to receive frames only from the
1133  *   secondary to which transmission permissions has been given.
1134  */
1135 static int irlap_state_nrm_p(struct irlap_cb *self, IRLAP_EVENT event, 
1136                              struct sk_buff *skb, struct irlap_info *info) 
1137 {
1138         int ret = 0;
1139         int ns_status;
1140         int nr_status;
1141
1142         switch (event) {
1143         case RECV_I_RSP: /* Optimize for the common case */
1144                 /* FIXME: must check for remote_busy below */
1145 #ifdef CONFIG_IRDA_FAST_RR
1146                 /* 
1147                  *  Reset the fast_RR so we can use the fast RR code with
1148                  *  full speed the next time since peer may have more frames
1149                  *  to transmitt
1150                  */
1151                 self->fast_RR = FALSE;
1152 #endif /* CONFIG_IRDA_FAST_RR */
1153                 ASSERT( info != NULL, return -1;);
1154
1155                 ns_status = irlap_validate_ns_received(self, info->ns);
1156                 nr_status = irlap_validate_nr_received(self, info->nr);
1157
1158                 /* 
1159                  *  Check for expected I(nformation) frame
1160                  */
1161                 if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
1162                         /*  poll bit cleared?  */
1163                         if (!info->pf) {
1164                                 self->vr = (self->vr + 1) % 8;
1165                         
1166                                 /* Update Nr received */
1167                                 irlap_update_nr_received( self, info->nr);
1168                                 
1169                                 self->ack_required = TRUE;
1170                                 
1171                                 /* Keep state, do not move this line */
1172                                 irlap_next_state(self, LAP_NRM_P);
1173
1174                                 irlap_data_indication(self, skb, FALSE);
1175                         } else {
1176                                 del_timer(&self->final_timer);
1177
1178                                 self->vr = (self->vr + 1) % 8;
1179                         
1180                                 /* Update Nr received */
1181                                 irlap_update_nr_received(self, info->nr);
1182                 
1183                                 /*  
1184                                  *  Got expected NR, so reset the
1185                                  *  retry_count. This is not done by IrLAP,
1186                                  *  which is strange!  
1187                                  */
1188                                 self->retry_count = 0;
1189                                 self->ack_required = TRUE;
1190                         
1191                                 irlap_wait_min_turn_around(self, &self->qos_tx);
1192
1193                                 /* Call higher layer *before* changing state
1194                                  * to give them a chance to send data in the
1195                                  * next LAP frame.
1196                                  * Jean II */
1197                                 irlap_data_indication(self, skb, FALSE);
1198
1199                                 /* XMIT states are the most dangerous state
1200                                  * to be in, because user requests are
1201                                  * processed directly and may change state.
1202                                  * On the other hand, in NDM_P, those
1203                                  * requests are queued and we will process
1204                                  * them when we return to irlap_do_event().
1205                                  * Jean II
1206                                  */
1207                                 irlap_next_state(self, LAP_XMIT_P);
1208
1209                                 /* This is the last frame.
1210                                  * Make sure it's always called in XMIT state.
1211                                  * - Jean II */
1212                                 irlap_start_poll_timer(self, self->poll_timeout);
1213                         }
1214                         break;
1215                         
1216                 }
1217                 /* Unexpected next to send (Ns) */
1218                 if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
1219                 {
1220                         if (!info->pf) {
1221                                 irlap_update_nr_received(self, info->nr);
1222                                 
1223                                 /*
1224                                  *  Wait until the last frame before doing 
1225                                  *  anything
1226                                  */
1227
1228                                 /* Keep state */
1229                                 irlap_next_state(self, LAP_NRM_P);
1230                         } else {
1231                                 IRDA_DEBUG(4, "%s(), missing or duplicate frame!\n", __FUNCTION__);
1232                                 
1233                                 /* Update Nr received */
1234                                 irlap_update_nr_received(self, info->nr);
1235                                 
1236                                 irlap_wait_min_turn_around(self, &self->qos_tx);
1237                                 irlap_send_rr_frame(self, CMD_FRAME);
1238                                 
1239                                 self->ack_required = FALSE;
1240                         
1241                                 irlap_start_final_timer(self, self->final_timeout);
1242                                 irlap_next_state(self, LAP_NRM_P);
1243                         }
1244                         break;
1245                 }
1246                 /* 
1247                  *  Unexpected next to receive (Nr) 
1248                  */
1249                 if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
1250                 {
1251                         if (info->pf) {
1252                                 self->vr = (self->vr + 1) % 8;
1253                         
1254                                 /* Update Nr received */
1255                                 irlap_update_nr_received(self, info->nr);
1256                         
1257                                 /* Resend rejected frames */
1258                                 irlap_resend_rejected_frames(self, CMD_FRAME);
1259                                 
1260                                 self->ack_required = FALSE;
1261                                 irlap_start_final_timer(self, self->final_timeout);
1262                                 
1263                                 /* Keep state, do not move this line */
1264                                 irlap_next_state(self, LAP_NRM_P);
1265
1266                                 irlap_data_indication(self, skb, FALSE);
1267                         } else {
1268                                 /* 
1269                                  *  Do not resend frames until the last
1270                                  *  frame has arrived from the other
1271                                  *  device. This is not documented in
1272                                  *  IrLAP!!  
1273                                  */
1274                                 self->vr = (self->vr + 1) % 8;
1275
1276                                 /* Update Nr received */
1277                                 irlap_update_nr_received(self, info->nr);
1278                                 
1279                                 self->ack_required = FALSE;
1280
1281                                 /* Keep state, do not move this line!*/
1282                                 irlap_next_state(self, LAP_NRM_P); 
1283
1284                                 irlap_data_indication(self, skb, FALSE);
1285                         }
1286                         break;
1287                 }
1288                 /*
1289                  *  Unexpected next to send (Ns) and next to receive (Nr)
1290                  *  Not documented by IrLAP!
1291                  */
1292                 if ((ns_status == NS_UNEXPECTED) && 
1293                     (nr_status == NR_UNEXPECTED)) 
1294                 {
1295                         IRDA_DEBUG(4, "%s(), unexpected nr and ns!\n", __FUNCTION__);
1296                         if (info->pf) {
1297                                 /* Resend rejected frames */
1298                                 irlap_resend_rejected_frames(self, CMD_FRAME);
1299                                 
1300                                 /* Give peer some time to retransmit! */
1301                                 irlap_start_final_timer(self, self->final_timeout);
1302
1303                                 /* Keep state, do not move this line */
1304                                 irlap_next_state(self, LAP_NRM_P);
1305                         } else {
1306                                 /* Update Nr received */
1307                                 /* irlap_update_nr_received( info->nr); */
1308                                 
1309                                 self->ack_required = FALSE;
1310                         }
1311                         break;
1312                 }
1313
1314                 /*
1315                  *  Invalid NR or NS
1316                  */
1317                 if ((nr_status == NR_INVALID) || (ns_status == NS_INVALID)) {
1318                         if (info->pf) {
1319                                 del_timer(&self->final_timer);
1320                                 
1321                                 irlap_next_state(self, LAP_RESET_WAIT);
1322                                 
1323                                 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1324                                 self->xmitflag = TRUE;
1325                         } else {
1326                                 del_timer(&self->final_timer);
1327                                 
1328                                 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1329                                 
1330                                 self->xmitflag = FALSE;
1331                         }
1332                         break;
1333                 }
1334                 IRDA_DEBUG(1, "%s(), Not implemented!\n", __FUNCTION__);
1335                 IRDA_DEBUG(1, "%s(), event=%s, ns_status=%d, nr_status=%d\n", __FUNCTION__,
1336                       irlap_event[ event], ns_status, nr_status);
1337                 break;
1338         case RECV_UI_FRAME:
1339                 /* Poll bit cleared? */
1340                 if (!info->pf) {
1341                         irlap_data_indication(self, skb, TRUE);
1342                         irlap_next_state(self, LAP_NRM_P);
1343                 } else {
1344                         del_timer(&self->final_timer);
1345                         irlap_data_indication(self, skb, TRUE);
1346                         irlap_next_state(self, LAP_XMIT_P);
1347                         printk("%s(): RECV_UI_FRAME: next state %s\n", __FUNCTION__, irlap_state[self->state]);
1348                         irlap_start_poll_timer(self, self->poll_timeout);
1349                 }
1350                 break;
1351         case RECV_RR_RSP:
1352                 /*  
1353                  *  If you get a RR, the remote isn't busy anymore, 
1354                  *  no matter what the NR 
1355                  */
1356                 self->remote_busy = FALSE;
1357
1358                 /* 
1359                  *  Nr as expected? 
1360                  */
1361                 ret = irlap_validate_nr_received(self, info->nr);
1362                 if (ret == NR_EXPECTED) {       
1363                         /* Stop final timer */
1364                         del_timer(&self->final_timer);
1365                         
1366                         /* Update Nr received */
1367                         irlap_update_nr_received(self, info->nr);
1368                         
1369                         /*
1370                          *  Got expected NR, so reset the retry_count. This 
1371                          *  is not done by the IrLAP standard , which is 
1372                          *  strange! DB.
1373                          */
1374                         self->retry_count = 0;                  
1375                         irlap_wait_min_turn_around(self, &self->qos_tx);
1376
1377                         irlap_next_state(self, LAP_XMIT_P);
1378
1379                         /* Start poll timer */
1380                         irlap_start_poll_timer(self, self->poll_timeout);
1381                 } else if (ret == NR_UNEXPECTED) {
1382                         ASSERT(info != NULL, return -1;);       
1383                         /* 
1384                          *  Unexpected nr! 
1385                          */
1386                         
1387                         /* Update Nr received */
1388                         irlap_update_nr_received(self, info->nr);
1389
1390                         IRDA_DEBUG(4, "RECV_RR_FRAME: Retrans:%d, nr=%d, va=%d, "
1391                               "vs=%d, vr=%d\n",
1392                               self->retry_count, info->nr, self->va, 
1393                               self->vs, self->vr);
1394                         
1395                         /* Resend rejected frames */
1396                         irlap_resend_rejected_frames(self, CMD_FRAME);
1397                         
1398                         irlap_next_state(self, LAP_NRM_P);
1399                 } else if (ret == NR_INVALID) {
1400                         IRDA_DEBUG(1, "%s(), Received RR with "
1401                                    "invalid nr !\n", __FUNCTION__);
1402                         del_timer(&self->final_timer);
1403
1404                         irlap_next_state(self, LAP_RESET_WAIT);
1405
1406                         irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1407                         self->xmitflag = TRUE;
1408                 }
1409                 break;
1410         case RECV_RNR_RSP:
1411                 ASSERT(info != NULL, return -1;);
1412
1413                 /* Stop final timer */
1414                 del_timer(&self->final_timer);
1415                 self->remote_busy = TRUE;
1416
1417                 /* Update Nr received */
1418                 irlap_update_nr_received(self, info->nr);
1419                 irlap_next_state(self, LAP_XMIT_P);
1420                         
1421                 /* Start poll timer */
1422                 irlap_start_poll_timer(self, self->poll_timeout);
1423                 break;
1424         case RECV_FRMR_RSP:
1425                 del_timer(&self->final_timer);
1426                 self->xmitflag = TRUE;
1427                 irlap_next_state(self, LAP_RESET_WAIT);
1428                 irlap_reset_indication(self);
1429                 break;
1430         case FINAL_TIMER_EXPIRED:
1431                 /* 
1432                  *  We are allowed to wait for additional 300 ms if
1433                  *  final timer expires when we are in the middle
1434                  *  of receiving a frame (page 45, IrLAP). Check that
1435                  *  we only do this once for each frame.
1436                  */
1437                 if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
1438                         IRDA_DEBUG(1, "FINAL_TIMER_EXPIRED when receiving a "
1439                               "frame! Waiting a little bit more!\n");
1440                         irlap_start_final_timer(self, MSECS_TO_JIFFIES(300));
1441
1442                         /*
1443                          *  Don't allow this to happen one more time in a row, 
1444                          *  or else we can get a pretty tight loop here if 
1445                          *  if we only receive half a frame. DB.
1446                          */
1447                         self->add_wait = TRUE;
1448                         break;
1449                 }
1450                 self->add_wait = FALSE;
1451
1452                 /* N2 is the disconnect timer. Until we reach it, we retry */
1453                 if (self->retry_count < self->N2) {
1454                         /* Retry sending the pf bit to the secondary */
1455                         irlap_wait_min_turn_around(self, &self->qos_tx);
1456                         irlap_send_rr_frame(self, CMD_FRAME);
1457                         
1458                         irlap_start_final_timer(self, self->final_timeout);
1459                         self->retry_count++;
1460                         IRDA_DEBUG(4, "irlap_state_nrm_p: FINAL_TIMER_EXPIRED:"
1461                                    " retry_count=%d\n", self->retry_count);
1462
1463                         /* Early warning event. I'm using a pretty liberal
1464                          * interpretation of the spec and generate an event
1465                          * every time the timer is multiple of N1 (and not
1466                          * only the first time). This allow application
1467                          * to know precisely if connectivity restart...
1468                          * Jean II */
1469                         if((self->retry_count % self->N1) == 0)
1470                                 irlap_status_indication(self,
1471                                                         STATUS_NO_ACTIVITY);
1472
1473                         /* Keep state */
1474                 } else {
1475                         irlap_apply_default_connection_parameters(self);
1476
1477                         /* Always switch state before calling upper layers */
1478                         irlap_next_state(self, LAP_NDM);
1479                         irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1480                 }
1481                 break;
1482         case RECV_REJ_RSP:
1483                 irlap_update_nr_received(self, info->nr);
1484                 if (self->remote_busy) {
1485                         irlap_wait_min_turn_around(self, &self->qos_tx);
1486                         irlap_send_rr_frame(self, CMD_FRAME);
1487                 } else
1488                         irlap_resend_rejected_frames(self, CMD_FRAME);
1489                 irlap_start_final_timer(self, self->final_timeout);
1490                 break;
1491         case RECV_SREJ_RSP:
1492                 irlap_update_nr_received(self, info->nr);
1493                 if (self->remote_busy) {
1494                         irlap_wait_min_turn_around(self, &self->qos_tx);
1495                         irlap_send_rr_frame(self, CMD_FRAME);
1496                 } else
1497                         irlap_resend_rejected_frame(self, CMD_FRAME);
1498                 irlap_start_final_timer(self, self->final_timeout);
1499                 break;
1500         case RECV_RD_RSP:
1501                 IRDA_DEBUG(1, "%s(), RECV_RD_RSP\n", __FUNCTION__);
1502
1503                 irlap_flush_all_queues(self);
1504                 irlap_next_state(self, LAP_XMIT_P);
1505                 /* Call back the LAP state machine to do a proper disconnect */
1506                 irlap_disconnect_request(self);
1507                 break;
1508         default:
1509                 IRDA_DEBUG(1, "%s(), Unknown event %s\n", __FUNCTION__,
1510                            irlap_event[event]);
1511
1512                 ret = -1;
1513                 break;
1514         }
1515         return ret;
1516 }
1517
1518 /*
1519  * Function irlap_state_reset_wait (event, skb, info)
1520  *
1521  *    We have informed the service user of a reset condition, and is
1522  *    awaiting reset of disconnect request.
1523  *
1524  */
1525 static int irlap_state_reset_wait(struct irlap_cb *self, IRLAP_EVENT event, 
1526                                   struct sk_buff *skb, struct irlap_info *info)
1527 {
1528         int ret = 0;
1529         
1530         IRDA_DEBUG(3, "%s(), event = %s\n", __FUNCTION__, irlap_event[event]);
1531         
1532         ASSERT(self != NULL, return -1;);
1533         ASSERT(self->magic == LAP_MAGIC, return -1;);
1534         
1535         switch (event) {
1536         case RESET_REQUEST:
1537                 if (self->xmitflag) {
1538                         irlap_wait_min_turn_around(self, &self->qos_tx);
1539                         irlap_send_snrm_frame(self, NULL);
1540                         irlap_start_final_timer(self, self->final_timeout);
1541                         irlap_next_state(self, LAP_RESET);
1542                 } else {
1543                         irlap_start_final_timer(self, self->final_timeout);
1544                         irlap_next_state(self, LAP_RESET);
1545                 }
1546                 break;
1547         case DISCONNECT_REQUEST:
1548                 irlap_wait_min_turn_around( self, &self->qos_tx);
1549                 irlap_send_disc_frame( self);
1550                 irlap_flush_all_queues( self);
1551                 irlap_start_final_timer( self, self->final_timeout);
1552                 self->retry_count = 0;
1553                 irlap_next_state( self, LAP_PCLOSE);
1554                 break;
1555         default:
1556                 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
1557                            irlap_event[event]);
1558
1559                 ret = -1;
1560                 break;  
1561         }
1562         return ret;
1563 }
1564
1565 /*
1566  * Function irlap_state_reset (self, event, skb, info)
1567  *
1568  *    We have sent a SNRM reset command to the peer layer, and is awaiting
1569  *    reply.
1570  *
1571  */
1572 static int irlap_state_reset(struct irlap_cb *self, IRLAP_EVENT event, 
1573                              struct sk_buff *skb, struct irlap_info *info)
1574 {
1575         int ret = 0;
1576         
1577         IRDA_DEBUG(3, "%s(), event = %s\n", __FUNCTION__, irlap_event[event]);
1578         
1579         ASSERT(self != NULL, return -1;);
1580         ASSERT(self->magic == LAP_MAGIC, return -1;);
1581         
1582         switch (event) {
1583         case RECV_DISC_CMD:
1584                 del_timer(&self->final_timer);
1585
1586                 irlap_apply_default_connection_parameters(self);
1587
1588                 /* Always switch state before calling upper layers */
1589                 irlap_next_state(self, LAP_NDM);
1590
1591                 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1592
1593                 break;
1594         case RECV_UA_RSP:
1595                 del_timer(&self->final_timer);
1596                 
1597                 /* Initiate connection state */
1598                 irlap_initiate_connection_state(self);
1599                 
1600                 irlap_reset_confirm();
1601                 
1602                 self->remote_busy = FALSE;
1603
1604                 irlap_next_state(self, LAP_XMIT_P);
1605
1606                 irlap_start_poll_timer(self, self->poll_timeout);
1607
1608                 break;
1609         case FINAL_TIMER_EXPIRED:
1610                 if (self->retry_count < 3) {
1611                         irlap_wait_min_turn_around(self, &self->qos_tx);
1612
1613                         ASSERT(self->netdev != NULL, return -1;);
1614                         irlap_send_snrm_frame(self, self->qos_dev);
1615
1616                         self->retry_count++; /* Experimental!! */
1617
1618                         irlap_start_final_timer(self, self->final_timeout);
1619                         irlap_next_state(self, LAP_RESET);
1620                 } else if (self->retry_count >= self->N3) {
1621                         irlap_apply_default_connection_parameters(self);
1622                         
1623                         /* Always switch state before calling upper layers */
1624                         irlap_next_state(self, LAP_NDM);
1625                         
1626                         irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1627                 }
1628                 break;
1629         case RECV_SNRM_CMD:
1630                 /* 
1631                  * SNRM frame is not allowed to contain an I-field in this 
1632                  * state
1633                  */
1634                 if (!info) {
1635                         IRDA_DEBUG(3, "%s(), RECV_SNRM_CMD\n", __FUNCTION__);
1636                         irlap_initiate_connection_state(self);
1637                         irlap_wait_min_turn_around(self, &self->qos_tx);
1638                         irlap_send_ua_response_frame(self, &self->qos_rx);
1639                         irlap_reset_confirm();
1640                         irlap_start_wd_timer(self, self->wd_timeout);
1641                         irlap_next_state(self, LAP_NDM);
1642                 } else {
1643                         IRDA_DEBUG(0, "%s(), SNRM frame contained an I field!\n", __FUNCTION__);
1644                 }
1645                 break;
1646         default:
1647                 IRDA_DEBUG(1, "%s(), Unknown event %s\n", __FUNCTION__,
1648                            irlap_event[event]); 
1649
1650                 ret = -1;
1651                 break;  
1652         }
1653         return ret;
1654 }
1655
1656 /*
1657  * Function irlap_state_xmit_s (event, skb, info)
1658  * 
1659  *   XMIT_S, The secondary station has been given the right to transmit,
1660  *   and we therefor do not expect to receive any transmissions from other
1661  *   stations.  
1662  */
1663 static int irlap_state_xmit_s(struct irlap_cb *self, IRLAP_EVENT event, 
1664                               struct sk_buff *skb, struct irlap_info *info) 
1665 {
1666         int ret = 0;
1667         
1668         IRDA_DEBUG(4, "%s(), event=%s\n", __FUNCTION__, irlap_event[event]); 
1669
1670         ASSERT(self != NULL, return -ENODEV;);
1671         ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
1672         
1673         switch (event) {
1674         case SEND_I_CMD:
1675                 /*
1676                  *  Send frame only if send window > 1
1677                  */ 
1678                 if ((self->window > 0) && (!self->remote_busy)) {
1679 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1680                         /*
1681                          *  Test if we have transmitted more bytes over the 
1682                          *  link than its possible to do with the current 
1683                          *  speed and turn-around-time.
1684                          */
1685                         if (skb->len > self->bytes_left) {
1686                                 skb_queue_head(&self->txq, skb_get(skb));
1687
1688                                 /*
1689                                  *  Switch to NRM_S, this is only possible
1690                                  *  when we are in secondary mode, since we 
1691                                  *  must be sure that we don't miss any RR
1692                                  *  frames
1693                                  */
1694                                 self->window = self->window_size;
1695                                 self->bytes_left = self->line_capacity;
1696                                 irlap_start_wd_timer(self, self->wd_timeout);
1697
1698                                 irlap_next_state(self, LAP_NRM_S);
1699
1700                                 return -EPROTO; /* Try again later */
1701                         }
1702                         self->bytes_left -= skb->len;
1703 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1704                         /*
1705                          *  Send data with final bit cleared only if window > 1
1706                          *  and there is more frames to be sent
1707                          */
1708                         if ((self->window > 1) && 
1709                             skb_queue_len(&self->txq) > 0) 
1710                         {   
1711                                 irlap_send_data_secondary(self, skb);
1712                                 irlap_next_state(self, LAP_XMIT_S);
1713                         } else {
1714                                 irlap_send_data_secondary_final(self, skb);
1715                                 irlap_next_state(self, LAP_NRM_S);
1716
1717                                 /* 
1718                                  * Make sure state machine does not try to send
1719                                  * any more frames 
1720                                  */
1721                                 ret = -EPROTO;
1722                         }
1723                 } else {
1724                         IRDA_DEBUG(2, "%s(), Unable to send!\n", __FUNCTION__);
1725                         skb_queue_head(&self->txq, skb_get(skb));
1726                         ret = -EPROTO;
1727                 }
1728                 break;
1729         case DISCONNECT_REQUEST:
1730                 irlap_send_rd_frame(self);
1731                 irlap_flush_all_queues(self);
1732                 irlap_start_wd_timer(self, self->wd_timeout);
1733                 irlap_next_state(self, LAP_SCLOSE);
1734                 break;
1735         case DATA_REQUEST:
1736                 /* Nothing to do, irlap_do_event() will send the packet
1737                  * when we return... - Jean II */
1738                 break;
1739         default:
1740                 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
1741                            irlap_event[event]);
1742
1743                 ret = -EINVAL;
1744                 break;
1745         }
1746         return ret;
1747 }
1748
1749 /*
1750  * Function irlap_state_nrm_s (event, skb, info)
1751  *
1752  *    NRM_S (Normal Response Mode as Secondary) state, in this state we are 
1753  *    expecting to receive frames from the primary station
1754  *
1755  */
1756 static int irlap_state_nrm_s(struct irlap_cb *self, IRLAP_EVENT event, 
1757                              struct sk_buff *skb, struct irlap_info *info) 
1758 {
1759         int ns_status;
1760         int nr_status;
1761         int ret = 0;
1762
1763         IRDA_DEBUG(4, "%s(), event=%s\n", __FUNCTION__, irlap_event[ event]);
1764
1765         ASSERT(self != NULL, return -1;);
1766         ASSERT(self->magic == LAP_MAGIC, return -1;);
1767
1768         switch (event) {
1769         case RECV_I_CMD: /* Optimize for the common case */
1770                 /* FIXME: must check for remote_busy below */
1771                 IRDA_DEBUG(4, "%s(), event=%s nr=%d, vs=%d, ns=%d, "
1772                            "vr=%d, pf=%d\n", __FUNCTION__, irlap_event[event], info->nr, 
1773                            self->vs, info->ns, self->vr, info->pf);
1774
1775                 self->retry_count = 0;
1776
1777                 ns_status = irlap_validate_ns_received(self, info->ns);
1778                 nr_status = irlap_validate_nr_received(self, info->nr);
1779                 /* 
1780                  *  Check for expected I(nformation) frame
1781                  */
1782                 if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
1783                         /* 
1784                          *  poll bit cleared?
1785                          */
1786                         if (!info->pf) {
1787                                 self->vr = (self->vr + 1) % 8;
1788                                 
1789                                 /* Update Nr received */
1790                                 irlap_update_nr_received(self, info->nr);
1791                                 
1792                                 self->ack_required = TRUE;
1793                                 
1794                                 /*
1795                                  *  Starting WD-timer here is optional, but
1796                                  *  not recommended. Note 6 IrLAP p. 83
1797                                  */
1798 #if 0
1799                                 irda_start_timer(WD_TIMER, self->wd_timeout);
1800 #endif
1801                                 /* Keep state, do not move this line */
1802                                 irlap_next_state(self, LAP_NRM_S);
1803
1804                                 irlap_data_indication(self, skb, FALSE);
1805                                 break;
1806                         } else {
1807                                 self->vr = (self->vr + 1) % 8;
1808                                 
1809                                 /* Update Nr received */
1810                                 irlap_update_nr_received(self, info->nr);
1811                                 
1812                                 /* 
1813                                  *  We should wait before sending RR, and
1814                                  *  also before changing to XMIT_S
1815                                  *  state. (note 1, IrLAP p. 82) 
1816                                  */
1817                                 irlap_wait_min_turn_around(self, &self->qos_tx);
1818
1819                                 /*  
1820                                  * Give higher layers a chance to
1821                                  * immediately reply with some data before
1822                                  * we decide if we should send a RR frame
1823                                  * or not
1824                                  */
1825                                 irlap_data_indication(self, skb, FALSE);
1826
1827                                 /* Any pending data requests?  */
1828                                 if ((skb_queue_len(&self->txq) > 0) && 
1829                                     (self->window > 0)) 
1830                                 {
1831                                         self->ack_required = TRUE;
1832                                         
1833                                         del_timer(&self->wd_timer);
1834                                         
1835                                         irlap_next_state(self, LAP_XMIT_S);
1836                                 } else {
1837                                         irlap_send_rr_frame(self, RSP_FRAME);
1838                                         irlap_start_wd_timer(self, 
1839                                                              self->wd_timeout);
1840
1841                                         /* Keep the state */
1842                                         irlap_next_state(self, LAP_NRM_S);
1843                                 }
1844                                 break;
1845                         }
1846                 }
1847                 /*
1848                  *  Check for Unexpected next to send (Ns)
1849                  */
1850                 if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
1851                 {
1852                         /* Unexpected next to send, with final bit cleared */
1853                         if (!info->pf) {
1854                                 irlap_update_nr_received(self, info->nr);
1855                                 
1856                                 irlap_start_wd_timer(self, self->wd_timeout);
1857                         } else {
1858                                 /* Update Nr received */
1859                                 irlap_update_nr_received(self, info->nr);
1860                         
1861                                 irlap_wait_min_turn_around(self, &self->qos_tx);
1862                                 irlap_send_rr_frame(self, RSP_FRAME);
1863                         
1864                                 irlap_start_wd_timer(self, self->wd_timeout);
1865                         }
1866                         break;
1867                 }
1868
1869                 /* 
1870                  *  Unexpected Next to Receive(NR) ?
1871                  */
1872                 if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
1873                 {
1874                         if (info->pf) {
1875                                 IRDA_DEBUG(4, "RECV_I_RSP: frame(s) lost\n");
1876                                 
1877                                 self->vr = (self->vr + 1) % 8;
1878                                 
1879                                 /* Update Nr received */
1880                                 irlap_update_nr_received(self, info->nr);
1881                                 
1882                                 /* Resend rejected frames */
1883                                 irlap_resend_rejected_frames(self, RSP_FRAME);
1884
1885                                 /* Keep state, do not move this line */
1886                                 irlap_next_state(self, LAP_NRM_S);
1887
1888                                 irlap_data_indication(self, skb, FALSE);
1889                                 irlap_start_wd_timer(self, self->wd_timeout);
1890                                 break;
1891                         }
1892                         /*
1893                          *  This is not documented in IrLAP!! Unexpected NR
1894                          *  with poll bit cleared
1895                          */
1896                         if (!info->pf) {
1897                                 self->vr = (self->vr + 1) % 8;
1898                                 
1899                                 /* Update Nr received */
1900                                 irlap_update_nr_received(self, info->nr);
1901                                 
1902                                 /* Keep state, do not move this line */
1903                                 irlap_next_state(self, LAP_NRM_S);
1904
1905                                 irlap_data_indication(self, skb, FALSE);
1906                                 irlap_start_wd_timer(self, self->wd_timeout);
1907                         }
1908                         break;
1909                 }
1910                 
1911                 if (ret == NR_INVALID) {
1912                         IRDA_DEBUG(0, "NRM_S, NR_INVALID not implemented!\n");
1913                 }
1914                 if (ret == NS_INVALID) {
1915                         IRDA_DEBUG(0, "NRM_S, NS_INVALID not implemented!\n");
1916                 }
1917                 break;
1918         case RECV_UI_FRAME:
1919                 /* 
1920                  *  poll bit cleared?
1921                  */
1922                 if (!info->pf) {
1923                         irlap_data_indication(self, skb, TRUE);
1924                         irlap_next_state(self, LAP_NRM_S); /* Keep state */
1925                 } else {
1926                         /*
1927                          *  Any pending data requests?
1928                          */
1929                         if ((skb_queue_len(&self->txq) > 0) && 
1930                             (self->window > 0) && !self->remote_busy) 
1931                         {
1932                                 irlap_data_indication(self, skb, TRUE);
1933                                 
1934                                 del_timer(&self->wd_timer);
1935
1936                                 irlap_next_state(self, LAP_XMIT_S);
1937                         } else {
1938                                 irlap_data_indication(self, skb, TRUE);
1939
1940                                 irlap_wait_min_turn_around(self, &self->qos_tx);
1941
1942                                 irlap_send_rr_frame(self, RSP_FRAME);
1943                                 self->ack_required = FALSE;
1944                                 
1945                                 irlap_start_wd_timer(self, self->wd_timeout);
1946
1947                                 /* Keep the state */
1948                                 irlap_next_state(self, LAP_NRM_S);
1949                         }
1950                 }
1951                 break;
1952         case RECV_RR_CMD:
1953                 self->retry_count = 0;
1954
1955                 /* 
1956                  *  Nr as expected? 
1957                  */
1958                 nr_status = irlap_validate_nr_received(self, info->nr);
1959                 if (nr_status == NR_EXPECTED) {
1960                         if ((skb_queue_len( &self->txq) > 0) && 
1961                             (self->window > 0)) {
1962                                 self->remote_busy = FALSE;
1963                                 
1964                                 /* Update Nr received */
1965                                 irlap_update_nr_received(self, info->nr);
1966                                 del_timer(&self->wd_timer);
1967                                 
1968                                 irlap_wait_min_turn_around(self, &self->qos_tx);
1969                                 irlap_next_state(self, LAP_XMIT_S);
1970                         } else {                        
1971                                 self->remote_busy = FALSE;
1972                                 /* Update Nr received */
1973                                 irlap_update_nr_received(self, info->nr);
1974                                 irlap_wait_min_turn_around(self, &self->qos_tx);
1975                                 irlap_start_wd_timer(self, self->wd_timeout);
1976                                 
1977                                 /* Note : if the link is idle (this case),
1978                                  * we never go in XMIT_S, so we never get a
1979                                  * chance to process any DISCONNECT_REQUEST.
1980                                  * Do it now ! - Jean II */
1981                                 if (self->disconnect_pending) {
1982                                         /* Disconnect */
1983                                         irlap_send_rd_frame(self);
1984                                         irlap_flush_all_queues(self);
1985
1986                                         irlap_next_state(self, LAP_SCLOSE);
1987                                 } else {
1988                                         /* Just send back pf bit */
1989                                         irlap_send_rr_frame(self, RSP_FRAME);
1990                                 
1991                                         irlap_next_state(self, LAP_NRM_S);
1992                                 }
1993                         }
1994                 } else if (nr_status == NR_UNEXPECTED) {
1995                         self->remote_busy = FALSE;
1996                         irlap_update_nr_received(self, info->nr);
1997                         irlap_resend_rejected_frames(self, RSP_FRAME);
1998
1999                         irlap_start_wd_timer(self, self->wd_timeout);
2000
2001                         /* Keep state */
2002                         irlap_next_state(self, LAP_NRM_S); 
2003                 } else {
2004                         IRDA_DEBUG(1, "%s(), invalid nr not implemented!\n", __FUNCTION__);
2005                 } 
2006                 break;
2007         case RECV_SNRM_CMD:
2008                 /* SNRM frame is not allowed to contain an I-field */
2009                 if (!info) {
2010                         del_timer(&self->wd_timer);
2011                         IRDA_DEBUG(1, "%s(), received SNRM cmd\n", __FUNCTION__);
2012                         irlap_next_state(self, LAP_RESET_CHECK);
2013                         
2014                         irlap_reset_indication(self);
2015                 } else {
2016                         IRDA_DEBUG(0, "%s(), SNRM frame contained an I-field!\n", __FUNCTION__);
2017                         
2018                 }
2019                 break;
2020         case RECV_REJ_CMD:
2021                 irlap_update_nr_received(self, info->nr);
2022                 if (self->remote_busy) {
2023                         irlap_wait_min_turn_around(self, &self->qos_tx);
2024                         irlap_send_rr_frame(self, RSP_FRAME);
2025                 } else
2026                         irlap_resend_rejected_frames(self, RSP_FRAME);
2027                 irlap_start_wd_timer(self, self->wd_timeout);
2028                 break;
2029         case RECV_SREJ_CMD:
2030                 irlap_update_nr_received(self, info->nr);
2031                 if (self->remote_busy) {
2032                         irlap_wait_min_turn_around(self, &self->qos_tx);
2033                         irlap_send_rr_frame(self, RSP_FRAME);
2034                 } else
2035                         irlap_resend_rejected_frame(self, RSP_FRAME);
2036                 irlap_start_wd_timer(self, self->wd_timeout);
2037                 break;
2038         case WD_TIMER_EXPIRED:
2039                 /*
2040                  *  Wait until retry_count * n matches negotiated threshold/
2041                  *  disconnect time (note 2 in IrLAP p. 82)
2042                  *
2043                  * Similar to irlap_state_nrm_p() -> FINAL_TIMER_EXPIRED
2044                  * Note : self->wd_timeout = (self->final_timeout * 2),
2045                  *   which explain why we use (self->N2 / 2) here !!!
2046                  * Jean II
2047                  */
2048                 IRDA_DEBUG(1, "%s(), retry_count = %d\n", __FUNCTION__,
2049                            self->retry_count);
2050
2051                 if (self->retry_count < (self->N2 / 2)) {
2052                         /* No retry, just wait for primary */
2053                         irlap_start_wd_timer(self, self->wd_timeout);
2054                         self->retry_count++;
2055
2056                         if((self->retry_count % (self->N1 / 2)) == 0)
2057                                 irlap_status_indication(self,
2058                                                         STATUS_NO_ACTIVITY);
2059                 } else {
2060                         irlap_apply_default_connection_parameters(self);
2061                         
2062                         /* Always switch state before calling upper layers */
2063                         irlap_next_state(self, LAP_NDM);
2064                         irlap_disconnect_indication(self, LAP_NO_RESPONSE);
2065                 }
2066                 break;
2067         case RECV_DISC_CMD:
2068                 /* Always switch state before calling upper layers */
2069                 irlap_next_state(self, LAP_NDM);
2070
2071                 /* Send disconnect response */
2072                 irlap_wait_min_turn_around(self, &self->qos_tx);
2073                 irlap_send_ua_response_frame(self, NULL);
2074
2075                 del_timer(&self->wd_timer);
2076                 irlap_flush_all_queues(self);
2077                 /* Set default link parameters */
2078                 irlap_apply_default_connection_parameters(self);
2079
2080                 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2081                 break;
2082         case RECV_DISCOVERY_XID_CMD:
2083                 irlap_wait_min_turn_around(self, &self->qos_tx);
2084                 irlap_send_rr_frame(self, RSP_FRAME);
2085                 self->ack_required = TRUE;
2086                 irlap_start_wd_timer(self, self->wd_timeout);
2087                 irlap_next_state(self, LAP_NRM_S);
2088
2089                 break;
2090         case RECV_TEST_CMD:
2091                 /* Remove test frame header (only LAP header in NRM) */
2092                 skb_pull(skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
2093
2094                 irlap_wait_min_turn_around(self, &self->qos_tx);
2095                 irlap_start_wd_timer(self, self->wd_timeout);
2096
2097                 /* Send response (info will be copied) */
2098                 irlap_send_test_frame(self, self->caddr, info->daddr, skb);
2099                 break;
2100         default:
2101                 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __FUNCTION__,
2102                            event, irlap_event[event]);
2103
2104                 ret = -EINVAL;
2105                 break;
2106         }
2107         return ret;
2108 }
2109
2110 /*
2111  * Function irlap_state_sclose (self, event, skb, info)
2112  *
2113  *    
2114  *
2115  */
2116 static int irlap_state_sclose(struct irlap_cb *self, IRLAP_EVENT event, 
2117                               struct sk_buff *skb, struct irlap_info *info) 
2118 {
2119         int ret = 0;
2120
2121         IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
2122
2123         ASSERT(self != NULL, return -ENODEV;);
2124         ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
2125         
2126         switch (event) {
2127         case RECV_DISC_CMD:
2128                 /* Always switch state before calling upper layers */
2129                 irlap_next_state(self, LAP_NDM);
2130
2131                 /* Send disconnect response */
2132                 irlap_wait_min_turn_around(self, &self->qos_tx);
2133                 irlap_send_ua_response_frame(self, NULL);
2134
2135                 del_timer(&self->wd_timer);
2136                 /* Set default link parameters */
2137                 irlap_apply_default_connection_parameters(self);
2138
2139                 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2140                 break;
2141         case RECV_DM_RSP:
2142                 /* Always switch state before calling upper layers */
2143                 irlap_next_state(self, LAP_NDM);
2144
2145                 del_timer(&self->wd_timer);
2146                 irlap_apply_default_connection_parameters(self);
2147                 
2148                 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2149                 break;
2150         case WD_TIMER_EXPIRED:
2151                 /* Always switch state before calling upper layers */
2152                 irlap_next_state(self, LAP_NDM);
2153
2154                 irlap_apply_default_connection_parameters(self);
2155                 
2156                 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2157                 break;
2158         default:
2159                 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __FUNCTION__,
2160                            event, irlap_event[event]);
2161
2162                 ret = -EINVAL;
2163                 break;
2164         }
2165
2166         return -1;
2167 }
2168
2169 static int irlap_state_reset_check( struct irlap_cb *self, IRLAP_EVENT event, 
2170                                    struct sk_buff *skb, 
2171                                    struct irlap_info *info) 
2172 {
2173         int ret = 0;
2174
2175         IRDA_DEBUG(1, "%s(), event=%s\n", __FUNCTION__, irlap_event[event]); 
2176
2177         ASSERT(self != NULL, return -ENODEV;);
2178         ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
2179         
2180         switch (event) {
2181         case RESET_RESPONSE:
2182                 irlap_send_ua_response_frame(self, &self->qos_rx);
2183                 irlap_initiate_connection_state(self);
2184                 irlap_start_wd_timer(self, WD_TIMEOUT);
2185                 irlap_flush_all_queues(self);
2186                 
2187                 irlap_next_state(self, LAP_NRM_S);
2188                 break;
2189         case DISCONNECT_REQUEST:
2190                 irlap_wait_min_turn_around(self, &self->qos_tx);
2191                 irlap_send_rd_frame(self);
2192                 irlap_start_wd_timer(self, WD_TIMEOUT);
2193                 irlap_next_state(self, LAP_SCLOSE);
2194                 break;
2195         default:
2196                 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __FUNCTION__,
2197                            event, irlap_event[event]);
2198
2199                 ret = -EINVAL;
2200                 break;
2201         }
2202         return ret;
2203 }