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