~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/irda/irlap_event.c

Version: ~ [ linux-6.4-rc3 ] ~ [ linux-6.3.4 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.30 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.113 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.180 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.243 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.283 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.315 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp