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