1 /*********************************************************************
3 * Filename: irlap_event.c
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>
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>
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.
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.
26 ********************************************************************/
28 #include <linux/string.h>
29 #include <linux/kernel.h>
30 #include <linux/delay.h>
31 #include <linux/skbuff.h>
32 #include <linux/slab.h>
34 #include <net/irda/irda.h>
35 #include <net/irda/irlap_event.h>
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(), ... */
44 #include <net/irda/irda_device.h>
46 #ifdef CONFIG_IRDA_FAST_RR
47 int sysctl_fast_poll_increase
= 50;
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
*);
81 #ifdef CONFIG_IRDA_DEBUG
82 static const char *const irlap_event
[] = {
92 "RECV_DISCOVERY_XID_CMD",
93 "RECV_DISCOVERY_XID_RSP",
113 "SLOT_TIMER_EXPIRED",
114 "QUERY_TIMER_EXPIRED",
115 "FINAL_TIMER_EXPIRED",
116 "POLL_TIMER_EXPIRED",
117 "DISCOVERY_TIMER_EXPIRED",
119 "BACKOFF_TIMER_EXPIRED",
120 "MEDIA_BUSY_TIMER_EXPIRED",
122 #endif /* CONFIG_IRDA_DEBUG */
124 const char *const irlap_state
[] = {
142 static int (*state
[])(struct irlap_cb
*self
, IRLAP_EVENT event
,
143 struct sk_buff
*skb
, struct irlap_info
*info
) =
154 irlap_state_reset_wait
,
159 irlap_state_reset_check
,
163 * Function irda_poll_timer_expired (data)
165 * Poll timer has expired. Normally we must now send a RR frame to the
168 static void irlap_poll_timer_expired(void *data
)
170 struct irlap_cb
*self
= (struct irlap_cb
*) data
;
172 IRDA_ASSERT(self
!= NULL
, return;);
173 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return;);
175 irlap_do_event(self
, POLL_TIMER_EXPIRED
, NULL
, NULL
);
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
184 static void irlap_start_poll_timer(struct irlap_cb
*self
, int timeout
)
186 IRDA_ASSERT(self
!= NULL
, return;);
187 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return;);
189 #ifdef CONFIG_IRDA_FAST_RR
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
194 if (skb_queue_empty(&self
->txq
) || self
->remote_busy
) {
195 if (self
->fast_RR
== TRUE
) {
197 * Assert that the fast poll timer has not reached the
198 * normal poll timer yet
200 if (self
->fast_RR_timeout
< timeout
) {
201 self
->fast_RR_timeout
+=
202 (sysctl_fast_poll_increase
* HZ
/1000);
204 /* Use this fast(er) timeout instead */
205 timeout
= self
->fast_RR_timeout
;
208 self
->fast_RR
= TRUE
;
210 /* Start with just 0 ms */
211 self
->fast_RR_timeout
= 0;
215 self
->fast_RR
= FALSE
;
217 IRDA_DEBUG(3, "%s(), timeout=%d (%ld)\n", __func__
, timeout
, jiffies
);
218 #endif /* CONFIG_IRDA_FAST_RR */
221 irlap_do_event(self
, POLL_TIMER_EXPIRED
, NULL
, NULL
);
223 irda_start_timer(&self
->poll_timer
, timeout
, self
,
224 irlap_poll_timer_expired
);
228 * Function irlap_do_event (event, skb, info)
230 * Rushes through the state machine without any delay. If state == XMIT
231 * then send queued data frames.
233 void irlap_do_event(struct irlap_cb
*self
, IRLAP_EVENT event
,
234 struct sk_buff
*skb
, struct irlap_info
*info
)
238 if (!self
|| self
->magic
!= LAP_MAGIC
)
241 IRDA_DEBUG(3, "%s(), event = %s, state = %s\n", __func__
,
242 irlap_event
[event
], irlap_state
[self
->state
]);
244 ret
= (*state
[self
->state
])(self
, event
, skb
, info
);
247 * Check if there are any pending events that needs to be executed
249 switch (self
->state
) {
250 case LAP_XMIT_P
: /* FALLTHROUGH */
253 * We just received the pf bit and are at the beginning
254 * of a new LAP transmit window.
255 * Check if there are any queued data frames, and do not
256 * try to disconnect link if we send any data frames, since
257 * that will change the state away form XMIT
259 IRDA_DEBUG(2, "%s() : queue len = %d\n", __func__
,
260 skb_queue_len(&self
->txq
));
262 if (!skb_queue_empty(&self
->txq
)) {
263 /* Prevent race conditions with irlap_data_request() */
264 self
->local_busy
= TRUE
;
266 /* Theory of operation.
267 * We send frames up to when we fill the window or
268 * reach line capacity. Those frames will queue up
269 * in the device queue, and the driver will slowly
271 * After each frame that we send, we poll the higher
272 * layer for more data. It's the right time to do
273 * that because the link layer need to perform the mtt
274 * and then send the first frame, so we can afford
275 * to send a bit of time in kernel space.
276 * The explicit flow indication allow to minimise
277 * buffers (== lower latency), to avoid higher layer
278 * polling via timers (== less context switches) and
279 * to implement a crude scheduler - Jean II */
281 /* Try to send away all queued data frames */
282 while ((skb
= skb_dequeue(&self
->txq
)) != NULL
) {
284 ret
= (*state
[self
->state
])(self
, SEND_I_CMD
,
286 /* Drop reference count.
287 * It will be increase as needed in
288 * irlap_send_data_xxx() */
291 /* Poll the higher layers for one more frame */
292 irlmp_flow_indication(self
->notify
.instance
,
296 break; /* Try again later! */
298 /* Finished transmitting */
299 self
->local_busy
= FALSE
;
300 } else if (self
->disconnect_pending
) {
301 self
->disconnect_pending
= FALSE
;
303 ret
= (*state
[self
->state
])(self
, DISCONNECT_REQUEST
,
309 /* case LAP_RESET_WAIT: */
310 /* case LAP_RESET_CHECK: */
317 * Function irlap_state_ndm (event, skb, frame)
319 * NDM (Normal Disconnected Mode) state
322 static int irlap_state_ndm(struct irlap_cb
*self
, IRLAP_EVENT event
,
323 struct sk_buff
*skb
, struct irlap_info
*info
)
325 discovery_t
*discovery_rsp
;
328 IRDA_ASSERT(self
!= NULL
, return -1;);
329 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
332 case CONNECT_REQUEST
:
333 IRDA_ASSERT(self
->netdev
!= NULL
, return -1;);
335 if (self
->media_busy
) {
336 /* Note : this will never happen, because we test
337 * media busy in irlap_connect_request() and
338 * postpone the event... - Jean II */
339 IRDA_DEBUG(0, "%s(), CONNECT_REQUEST: media busy!\n",
342 /* Always switch state before calling upper layers */
343 irlap_next_state(self
, LAP_NDM
);
345 irlap_disconnect_indication(self
, LAP_MEDIA_BUSY
);
347 irlap_send_snrm_frame(self
, &self
->qos_rx
);
349 /* Start Final-bit timer */
350 irlap_start_final_timer(self
, self
->final_timeout
);
352 self
->retry_count
= 0;
353 irlap_next_state(self
, LAP_SETUP
);
357 /* Check if the frame contains and I field */
359 self
->daddr
= info
->daddr
;
360 self
->caddr
= info
->caddr
;
362 irlap_next_state(self
, LAP_CONN
);
364 irlap_connect_indication(self
, skb
);
366 IRDA_DEBUG(0, "%s(), SNRM frame does not "
367 "contain an I field!\n", __func__
);
370 case DISCOVERY_REQUEST
:
371 IRDA_ASSERT(info
!= NULL
, return -1;);
373 if (self
->media_busy
) {
374 IRDA_DEBUG(1, "%s(), DISCOVERY_REQUEST: media busy!\n",
376 /* irlap->log.condition = MEDIA_BUSY; */
378 /* This will make IrLMP try again */
379 irlap_discovery_confirm(self
, NULL
);
380 /* Note : the discovery log is not cleaned up here,
381 * it will be done in irlap_discovery_request()
388 irlap_send_discovery_xid_frame(self
, info
->S
, info
->s
, TRUE
,
390 self
->frame_sent
= FALSE
;
393 irlap_start_slot_timer(self
, self
->slot_timeout
);
394 irlap_next_state(self
, LAP_QUERY
);
396 case RECV_DISCOVERY_XID_CMD
:
397 IRDA_ASSERT(info
!= NULL
, return -1;);
399 /* Assert that this is not the final slot */
400 if (info
->s
<= info
->S
) {
401 self
->slot
= irlap_generate_rand_time_slot(info
->S
,
403 if (self
->slot
== info
->s
) {
404 discovery_rsp
= irlmp_get_discovery_response();
405 discovery_rsp
->data
.daddr
= info
->daddr
;
407 irlap_send_discovery_xid_frame(self
, info
->S
,
411 self
->frame_sent
= TRUE
;
413 self
->frame_sent
= FALSE
;
416 * Go to reply state until end of discovery to
417 * inhibit our own transmissions. Set the timer
418 * to not stay forever there... Jean II
420 irlap_start_query_timer(self
, info
->S
, info
->s
);
421 irlap_next_state(self
, LAP_REPLY
);
423 /* This is the final slot. How is it possible ?
424 * This would happen is both discoveries are just slightly
425 * offset (if they are in sync, all packets are lost).
426 * Most often, all the discovery requests will be received
427 * in QUERY state (see my comment there), except for the
428 * last frame that will come here.
429 * The big trouble when it happen is that active discovery
430 * doesn't happen, because nobody answer the discoveries
431 * frame of the other guy, so the log shows up empty.
432 * What should we do ?
433 * Not much. It's too late to answer those discovery frames,
434 * so we just pass the info to IrLMP who will put it in the
435 * log (and post an event).
436 * Another cause would be devices that do discovery much
437 * slower than us, however the latest fixes should minimise
441 IRDA_DEBUG(1, "%s(), Receiving final discovery request, missed the discovery slots :-(\n", __func__
);
443 /* Last discovery request -> in the log */
444 irlap_discovery_indication(self
, info
->discovery
);
447 case MEDIA_BUSY_TIMER_EXPIRED
:
448 /* A bunch of events may be postponed because the media is
449 * busy (usually immediately after we close a connection),
450 * or while we are doing discovery (state query/reply).
451 * In all those cases, the media busy flag will be cleared
452 * when it's OK for us to process those postponed events.
453 * This event is not mentioned in the state machines in the
454 * IrLAP spec. It's because they didn't consider Ultra and
455 * postponing connection request is optional.
457 #ifdef CONFIG_IRDA_ULTRA
458 /* Send any pending Ultra frames if any */
459 if (!skb_queue_empty(&self
->txq_ultra
)) {
460 /* We don't send the frame, just post an event.
461 * Also, previously this code was in timer.c...
463 ret
= (*state
[self
->state
])(self
, SEND_UI_FRAME
,
466 #endif /* CONFIG_IRDA_ULTRA */
467 /* Check if we should try to connect.
468 * This code was previously in irlap_do_event() */
469 if (self
->connect_pending
) {
470 self
->connect_pending
= FALSE
;
472 /* This one *should* not pend in this state, except
473 * if a socket try to connect and immediately
474 * disconnect. - clear - Jean II */
475 if (self
->disconnect_pending
)
476 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
478 ret
= (*state
[self
->state
])(self
,
481 self
->disconnect_pending
= FALSE
;
483 /* Note : one way to test if this code works well (including
484 * media busy and small busy) is to create a user space
485 * application generating an Ultra packet every 3.05 sec (or
486 * 2.95 sec) and to see how it interact with discovery.
487 * It's fairly easy to check that no packet is lost, that the
488 * packets are postponed during discovery and that after
489 * discovery indication you have a 100ms "gap".
490 * As connection request and Ultra are now processed the same
491 * way, this avoid the tedious job of trying IrLAP connection
492 * in all those cases...
495 #ifdef CONFIG_IRDA_ULTRA
499 /* Only allowed to repeat an operation twice */
500 for (i
=0; ((i
<2) && (self
->media_busy
== FALSE
)); i
++) {
501 skb
= skb_dequeue(&self
->txq_ultra
);
503 irlap_send_ui_frame(self
, skb
, CBROADCAST
,
507 /* irlap_send_ui_frame() won't increase skb reference
508 * count, so no dev_kfree_skb() - Jean II */
511 /* Force us to listen 500 ms again */
512 irda_device_set_media_busy(self
->netdev
, TRUE
);
517 /* Only accept broadcast frames in NDM mode */
518 if (info
->caddr
!= CBROADCAST
) {
519 IRDA_DEBUG(0, "%s(), not a broadcast frame!\n",
522 irlap_unitdata_indication(self
, skb
);
524 #endif /* CONFIG_IRDA_ULTRA */
526 /* Remove test frame header */
527 skb_pull(skb
, sizeof(struct test_frame
));
530 * Send response. This skb will not be sent out again, and
531 * will only be used to send out the same info as the cmd
533 irlap_send_test_frame(self
, CBROADCAST
, info
->daddr
, skb
);
536 IRDA_DEBUG(0, "%s() not implemented!\n", __func__
);
539 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __func__
,
549 * Function irlap_state_query (event, skb, info)
554 static int irlap_state_query(struct irlap_cb
*self
, IRLAP_EVENT event
,
555 struct sk_buff
*skb
, struct irlap_info
*info
)
559 IRDA_ASSERT(self
!= NULL
, return -1;);
560 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
563 case RECV_DISCOVERY_XID_RSP
:
564 IRDA_ASSERT(info
!= NULL
, return -1;);
565 IRDA_ASSERT(info
->discovery
!= NULL
, return -1;);
567 IRDA_DEBUG(4, "%s(), daddr=%08x\n", __func__
,
568 info
->discovery
->data
.daddr
);
570 if (!self
->discovery_log
) {
571 IRDA_WARNING("%s: discovery log is gone! "
572 "maybe the discovery timeout has been set"
573 " too short?\n", __func__
);
576 hashbin_insert(self
->discovery_log
,
577 (irda_queue_t
*) info
->discovery
,
578 info
->discovery
->data
.daddr
, NULL
);
581 /* irlap_next_state(self, LAP_QUERY); */
584 case RECV_DISCOVERY_XID_CMD
:
585 /* Yes, it is possible to receive those frames in this mode.
586 * Note that most often the last discovery request won't
587 * occur here but in NDM state (see my comment there).
588 * What should we do ?
589 * Not much. We are currently performing our own discovery,
590 * therefore we can't answer those frames. We don't want
591 * to change state either. We just pass the info to
592 * IrLMP who will put it in the log (and post an event).
596 IRDA_ASSERT(info
!= NULL
, return -1;);
598 IRDA_DEBUG(1, "%s(), Receiving discovery request (s = %d) while performing discovery :-(\n", __func__
, info
->s
);
600 /* Last discovery request ? */
602 irlap_discovery_indication(self
, info
->discovery
);
604 case SLOT_TIMER_EXPIRED
:
606 * Wait a little longer if we detect an incoming frame. This
607 * is not mentioned in the spec, but is a good thing to do,
608 * since we want to work even with devices that violate the
609 * timing requirements.
611 if (irda_device_is_receiving(self
->netdev
) && !self
->add_wait
) {
612 IRDA_DEBUG(2, "%s(), device is slow to answer, "
613 "waiting some more!\n", __func__
);
614 irlap_start_slot_timer(self
, msecs_to_jiffies(10));
615 self
->add_wait
= TRUE
;
618 self
->add_wait
= FALSE
;
620 if (self
->s
< self
->S
) {
621 irlap_send_discovery_xid_frame(self
, self
->S
,
623 self
->discovery_cmd
);
625 irlap_start_slot_timer(self
, self
->slot_timeout
);
628 irlap_next_state(self
, LAP_QUERY
);
630 /* This is the final slot! */
631 irlap_send_discovery_xid_frame(self
, self
->S
, 0xff,
633 self
->discovery_cmd
);
635 /* Always switch state before calling upper layers */
636 irlap_next_state(self
, LAP_NDM
);
639 * We are now finished with the discovery procedure,
640 * so now we must return the results
642 irlap_discovery_confirm(self
, self
->discovery_log
);
644 /* IrLMP should now have taken care of the log */
645 self
->discovery_log
= NULL
;
649 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __func__
,
659 * Function irlap_state_reply (self, event, skb, info)
661 * REPLY, we have received a XID discovery frame from a device and we
662 * are waiting for the right time slot to send a response XID frame
665 static int irlap_state_reply(struct irlap_cb
*self
, IRLAP_EVENT event
,
666 struct sk_buff
*skb
, struct irlap_info
*info
)
668 discovery_t
*discovery_rsp
;
671 IRDA_DEBUG(4, "%s()\n", __func__
);
673 IRDA_ASSERT(self
!= NULL
, return -1;);
674 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
677 case QUERY_TIMER_EXPIRED
:
678 IRDA_DEBUG(0, "%s(), QUERY_TIMER_EXPIRED <%ld>\n",
680 irlap_next_state(self
, LAP_NDM
);
682 case RECV_DISCOVERY_XID_CMD
:
683 IRDA_ASSERT(info
!= NULL
, return -1;);
685 if (info
->s
== 0xff) {
686 del_timer(&self
->query_timer
);
688 /* info->log.condition = REMOTE; */
690 /* Always switch state before calling upper layers */
691 irlap_next_state(self
, LAP_NDM
);
693 irlap_discovery_indication(self
, info
->discovery
);
695 /* If it's our slot, send our reply */
696 if ((info
->s
>= self
->slot
) && (!self
->frame_sent
)) {
697 discovery_rsp
= irlmp_get_discovery_response();
698 discovery_rsp
->data
.daddr
= info
->daddr
;
700 irlap_send_discovery_xid_frame(self
, info
->S
,
705 self
->frame_sent
= TRUE
;
707 /* Readjust our timer to accomodate devices
708 * doing faster or slower discovery than us...
710 irlap_start_query_timer(self
, info
->S
, info
->s
);
713 //irlap_next_state(self, LAP_REPLY);
717 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __func__
,
718 event
, irlap_event
[event
]);
727 * Function irlap_state_conn (event, skb, info)
729 * CONN, we have received a SNRM command and is waiting for the upper
730 * layer to accept or refuse connection
733 static int irlap_state_conn(struct irlap_cb
*self
, IRLAP_EVENT event
,
734 struct sk_buff
*skb
, struct irlap_info
*info
)
738 IRDA_DEBUG(4, "%s(), event=%s\n", __func__
, irlap_event
[ event
]);
740 IRDA_ASSERT(self
!= NULL
, return -1;);
741 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
744 case CONNECT_RESPONSE
:
745 skb_pull(skb
, sizeof(struct snrm_frame
));
747 IRDA_ASSERT(self
->netdev
!= NULL
, return -1;);
749 irlap_qos_negotiate(self
, skb
);
751 irlap_initiate_connection_state(self
);
754 * Applying the parameters now will make sure we change speed
755 * *after* we have sent the next frame
757 irlap_apply_connection_parameters(self
, FALSE
);
760 * Sending this frame will force a speed change after it has
761 * been sent (i.e. the frame will be sent at 9600).
763 irlap_send_ua_response_frame(self
, &self
->qos_rx
);
767 * The WD-timer could be set to the duration of the P-timer
768 * for this case, but it is recommended to use twice the
769 * value (note 3 IrLAP p. 60).
771 irlap_start_wd_timer(self
, self
->wd_timeout
);
772 irlap_next_state(self
, LAP_NRM_S
);
775 case RECV_DISCOVERY_XID_CMD
:
776 IRDA_DEBUG(3, "%s(), event RECV_DISCOVER_XID_CMD!\n",
778 irlap_next_state(self
, LAP_NDM
);
781 case DISCONNECT_REQUEST
:
782 IRDA_DEBUG(0, "%s(), Disconnect request!\n", __func__
);
783 irlap_send_dm_frame(self
);
784 irlap_next_state( self
, LAP_NDM
);
785 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
788 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __func__
,
789 event
, irlap_event
[event
]);
799 * Function irlap_state_setup (event, skb, frame)
801 * SETUP state, The local layer has transmitted a SNRM command frame to
802 * a remote peer layer and is awaiting a reply .
805 static int irlap_state_setup(struct irlap_cb
*self
, IRLAP_EVENT event
,
806 struct sk_buff
*skb
, struct irlap_info
*info
)
810 IRDA_DEBUG(4, "%s()\n", __func__
);
812 IRDA_ASSERT(self
!= NULL
, return -1;);
813 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
816 case FINAL_TIMER_EXPIRED
:
817 if (self
->retry_count
< self
->N3
) {
819 * Perform random backoff, Wait a random number of time units, minimum
820 * duration half the time taken to transmitt a SNRM frame, maximum duration
821 * 1.5 times the time taken to transmit a SNRM frame. So this time should
822 * between 15 msecs and 45 msecs.
824 irlap_start_backoff_timer(self
, msecs_to_jiffies(20 +
827 /* Always switch state before calling upper layers */
828 irlap_next_state(self
, LAP_NDM
);
830 irlap_disconnect_indication(self
, LAP_FOUND_NONE
);
833 case BACKOFF_TIMER_EXPIRED
:
834 irlap_send_snrm_frame(self
, &self
->qos_rx
);
835 irlap_start_final_timer(self
, self
->final_timeout
);
839 IRDA_DEBUG(4, "%s(), SNRM battle!\n", __func__
);
841 IRDA_ASSERT(skb
!= NULL
, return 0;);
842 IRDA_ASSERT(info
!= NULL
, return 0;);
845 * The device with the largest device address wins the battle
846 * (both have sent a SNRM command!)
848 if (info
&&(info
->daddr
> self
->saddr
)) {
849 del_timer(&self
->final_timer
);
850 irlap_initiate_connection_state(self
);
852 IRDA_ASSERT(self
->netdev
!= NULL
, return -1;);
854 skb_pull(skb
, sizeof(struct snrm_frame
));
856 irlap_qos_negotiate(self
, skb
);
858 /* Send UA frame and then change link settings */
859 irlap_apply_connection_parameters(self
, FALSE
);
860 irlap_send_ua_response_frame(self
, &self
->qos_rx
);
862 irlap_next_state(self
, LAP_NRM_S
);
863 irlap_connect_confirm(self
, skb
);
866 * The WD-timer could be set to the duration of the
867 * P-timer for this case, but it is recommended
868 * to use twice the value (note 3 IrLAP p. 60).
870 irlap_start_wd_timer(self
, self
->wd_timeout
);
872 /* We just ignore the other device! */
873 irlap_next_state(self
, LAP_SETUP
);
878 del_timer(&self
->final_timer
);
880 /* Initiate connection state */
881 irlap_initiate_connection_state(self
);
883 /* Negotiate connection parameters */
884 IRDA_ASSERT(skb
->len
> 10, return -1;);
886 skb_pull(skb
, sizeof(struct ua_frame
));
888 IRDA_ASSERT(self
->netdev
!= NULL
, return -1;);
890 irlap_qos_negotiate(self
, skb
);
892 /* Set the new link setting *now* (before the rr frame) */
893 irlap_apply_connection_parameters(self
, TRUE
);
894 self
->retry_count
= 0;
896 /* Wait for turnaround time to give a chance to the other
897 * device to be ready to receive us.
898 * Note : the time to switch speed is typically larger
899 * than the turnaround time, but as we don't have the other
900 * side speed switch time, that's our best guess...
902 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
904 /* This frame will actually be sent at the new speed */
905 irlap_send_rr_frame(self
, CMD_FRAME
);
907 /* The timer is set to half the normal timer to quickly
908 * detect a failure to negociate the new connection
909 * parameters. IrLAP 6.11.3.2, note 3.
910 * Note that currently we don't process this failure
911 * properly, as we should do a quick disconnect.
913 irlap_start_final_timer(self
, self
->final_timeout
/2);
914 irlap_next_state(self
, LAP_NRM_P
);
916 irlap_connect_confirm(self
, skb
);
918 case RECV_DM_RSP
: /* FALLTHROUGH */
920 del_timer(&self
->final_timer
);
921 irlap_next_state(self
, LAP_NDM
);
923 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
926 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __func__
,
927 event
, irlap_event
[event
]);
936 * Function irlap_state_offline (self, event, skb, info)
938 * OFFLINE state, not used for now!
941 static int irlap_state_offline(struct irlap_cb
*self
, IRLAP_EVENT event
,
942 struct sk_buff
*skb
, struct irlap_info
*info
)
944 IRDA_DEBUG( 0, "%s(), Unknown event\n", __func__
);
950 * Function irlap_state_xmit_p (self, event, skb, info)
952 * XMIT, Only the primary station has right to transmit, and we
953 * therefore do not expect to receive any transmissions from other
957 static int irlap_state_xmit_p(struct irlap_cb
*self
, IRLAP_EVENT event
,
958 struct sk_buff
*skb
, struct irlap_info
*info
)
965 * Only send frame if send-window > 0.
967 if ((self
->window
> 0) && (!self
->remote_busy
)) {
969 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
970 struct sk_buff
*skb_next
;
972 /* With DYNAMIC_WINDOW, we keep the window size
973 * maximum, and adapt on the packets we are sending.
974 * At 115k, we can send only 2 packets of 2048 bytes
975 * in a 500 ms turnaround. Without this option, we
976 * would always limit the window to 2. With this
977 * option, if we send smaller packets, we can send
978 * up to 7 of them (always depending on QoS).
981 /* Look at the next skb. This is safe, as we are
982 * the only consumer of the Tx queue (if we are not,
983 * we have other problems) - Jean II */
984 skb_next
= skb_peek(&self
->txq
);
986 /* Check if a subsequent skb exist and would fit in
987 * the current window (with respect to turnaround
989 * This allow us to properly mark the current packet
990 * with the pf bit, to avoid falling back on the
991 * second test below, and avoid waiting the
992 * end of the window and sending a extra RR.
993 * Note : (skb_next != NULL) <=> (skb_queue_len() > 0)
995 nextfit
= ((skb_next
!= NULL
) &&
996 ((skb_next
->len
+ skb
->len
) <=
1000 * The current packet may not fit ! Because of test
1001 * above, this should not happen any more !!!
1002 * Test if we have transmitted more bytes over the
1003 * link than its possible to do with the current
1004 * speed and turn-around-time.
1006 if((!nextfit
) && (skb
->len
> self
->bytes_left
)) {
1007 IRDA_DEBUG(0, "%s(), Not allowed to transmit"
1008 " more bytes!\n", __func__
);
1009 /* Requeue the skb */
1010 skb_queue_head(&self
->txq
, skb_get(skb
));
1012 * We should switch state to LAP_NRM_P, but
1013 * that is not possible since we must be sure
1014 * that we poll the other side. Since we have
1015 * used up our time, the poll timer should
1016 * trigger anyway now, so we just wait for it
1020 * Sorry, but that's not totally true. If
1021 * we send 2000B packets, we may wait another
1022 * 1000B until our turnaround expire. That's
1023 * why we need to be proactive in avoiding
1024 * coming here. - Jean II
1029 /* Substract space used by this skb */
1030 self
->bytes_left
-= skb
->len
;
1031 #else /* CONFIG_IRDA_DYNAMIC_WINDOW */
1032 /* Window has been adjusted for the max packet
1033 * size, so much simpler... - Jean II */
1034 nextfit
= !skb_queue_empty(&self
->txq
);
1035 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1037 * Send data with poll bit cleared only if window > 1
1038 * and there is more frames after this one to be sent
1040 if ((self
->window
> 1) && (nextfit
)) {
1041 /* More packet to send in current window */
1042 irlap_send_data_primary(self
, skb
);
1043 irlap_next_state(self
, LAP_XMIT_P
);
1045 /* Final packet of window */
1046 irlap_send_data_primary_poll(self
, skb
);
1049 * Make sure state machine does not try to send
1054 #ifdef CONFIG_IRDA_FAST_RR
1055 /* Peer may want to reply immediately */
1056 self
->fast_RR
= FALSE
;
1057 #endif /* CONFIG_IRDA_FAST_RR */
1059 IRDA_DEBUG(4, "%s(), Unable to send! remote busy?\n",
1061 skb_queue_head(&self
->txq
, skb_get(skb
));
1064 * The next ret is important, because it tells
1065 * irlap_next_state _not_ to deliver more frames
1070 case POLL_TIMER_EXPIRED
:
1071 IRDA_DEBUG(3, "%s(), POLL_TIMER_EXPIRED <%ld>\n",
1073 irlap_send_rr_frame(self
, CMD_FRAME
);
1074 /* Return to NRM properly - Jean II */
1075 self
->window
= self
->window_size
;
1076 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1077 /* Allowed to transmit a maximum number of bytes again. */
1078 self
->bytes_left
= self
->line_capacity
;
1079 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1080 irlap_start_final_timer(self
, self
->final_timeout
);
1081 irlap_next_state(self
, LAP_NRM_P
);
1083 case DISCONNECT_REQUEST
:
1084 del_timer(&self
->poll_timer
);
1085 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1086 irlap_send_disc_frame(self
);
1087 irlap_flush_all_queues(self
);
1088 irlap_start_final_timer(self
, self
->final_timeout
);
1089 self
->retry_count
= 0;
1090 irlap_next_state(self
, LAP_PCLOSE
);
1093 /* Nothing to do, irlap_do_event() will send the packet
1094 * when we return... - Jean II */
1097 IRDA_DEBUG(0, "%s(), Unknown event %s\n",
1098 __func__
, irlap_event
[event
]);
1107 * Function irlap_state_pclose (event, skb, info)
1111 static int irlap_state_pclose(struct irlap_cb
*self
, IRLAP_EVENT event
,
1112 struct sk_buff
*skb
, struct irlap_info
*info
)
1116 IRDA_DEBUG(1, "%s()\n", __func__
);
1118 IRDA_ASSERT(self
!= NULL
, return -1;);
1119 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
1122 case RECV_UA_RSP
: /* FALLTHROUGH */
1124 del_timer(&self
->final_timer
);
1126 /* Set new link parameters */
1127 irlap_apply_default_connection_parameters(self
);
1129 /* Always switch state before calling upper layers */
1130 irlap_next_state(self
, LAP_NDM
);
1132 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
1134 case FINAL_TIMER_EXPIRED
:
1135 if (self
->retry_count
< self
->N3
) {
1136 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1137 irlap_send_disc_frame(self
);
1138 irlap_start_final_timer(self
, self
->final_timeout
);
1139 self
->retry_count
++;
1142 irlap_apply_default_connection_parameters(self
);
1144 /* Always switch state before calling upper layers */
1145 irlap_next_state(self
, LAP_NDM
);
1147 irlap_disconnect_indication(self
, LAP_NO_RESPONSE
);
1151 IRDA_DEBUG(1, "%s(), Unknown event %d\n", __func__
, event
);
1160 * Function irlap_state_nrm_p (self, event, skb, info)
1162 * NRM_P (Normal Response Mode as Primary), The primary station has given
1163 * permissions to a secondary station to transmit IrLAP resonse frames
1164 * (by sending a frame with the P bit set). The primary station will not
1165 * transmit any frames and is expecting to receive frames only from the
1166 * secondary to which transmission permissions has been given.
1168 static int irlap_state_nrm_p(struct irlap_cb
*self
, IRLAP_EVENT event
,
1169 struct sk_buff
*skb
, struct irlap_info
*info
)
1176 case RECV_I_RSP
: /* Optimize for the common case */
1177 if (unlikely(skb
->len
<= LAP_ADDR_HEADER
+ LAP_CTRL_HEADER
)) {
1179 * Input validation check: a stir4200/mcp2150
1180 * combination sometimes results in an empty i:rsp.
1181 * This makes no sense; we can just ignore the frame
1182 * and send an rr:cmd immediately. This happens before
1183 * changing nr or ns so triggers a retransmit
1185 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1186 irlap_send_rr_frame(self
, CMD_FRAME
);
1190 #ifdef CONFIG_IRDA_FAST_RR
1192 * Reset the fast_RR so we can use the fast RR code with
1193 * full speed the next time since peer may have more frames
1196 self
->fast_RR
= FALSE
;
1197 #endif /* CONFIG_IRDA_FAST_RR */
1198 IRDA_ASSERT( info
!= NULL
, return -1;);
1200 ns_status
= irlap_validate_ns_received(self
, info
->ns
);
1201 nr_status
= irlap_validate_nr_received(self
, info
->nr
);
1204 * Check for expected I(nformation) frame
1206 if ((ns_status
== NS_EXPECTED
) && (nr_status
== NR_EXPECTED
)) {
1208 /* Update Vr (next frame for us to receive) */
1209 self
->vr
= (self
->vr
+ 1) % 8;
1211 /* Update Nr received, cleanup our retry queue */
1212 irlap_update_nr_received(self
, info
->nr
);
1215 * Got expected NR, so reset the
1216 * retry_count. This is not done by IrLAP spec,
1219 self
->retry_count
= 0;
1220 self
->ack_required
= TRUE
;
1222 /* poll bit cleared? */
1224 /* Keep state, do not move this line */
1225 irlap_next_state(self
, LAP_NRM_P
);
1227 irlap_data_indication(self
, skb
, FALSE
);
1229 /* No longer waiting for pf */
1230 del_timer(&self
->final_timer
);
1232 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1234 /* Call higher layer *before* changing state
1235 * to give them a chance to send data in the
1238 irlap_data_indication(self
, skb
, FALSE
);
1240 /* XMIT states are the most dangerous state
1241 * to be in, because user requests are
1242 * processed directly and may change state.
1243 * On the other hand, in NDM_P, those
1244 * requests are queued and we will process
1245 * them when we return to irlap_do_event().
1248 irlap_next_state(self
, LAP_XMIT_P
);
1250 /* This is the last frame.
1251 * Make sure it's always called in XMIT state.
1253 irlap_start_poll_timer(self
, self
->poll_timeout
);
1258 /* Unexpected next to send (Ns) */
1259 if ((ns_status
== NS_UNEXPECTED
) && (nr_status
== NR_EXPECTED
))
1262 irlap_update_nr_received(self
, info
->nr
);
1265 * Wait until the last frame before doing
1270 irlap_next_state(self
, LAP_NRM_P
);
1273 "%s(), missing or duplicate frame!\n",
1276 /* Update Nr received */
1277 irlap_update_nr_received(self
, info
->nr
);
1279 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1280 irlap_send_rr_frame(self
, CMD_FRAME
);
1282 self
->ack_required
= FALSE
;
1284 irlap_start_final_timer(self
, self
->final_timeout
);
1285 irlap_next_state(self
, LAP_NRM_P
);
1290 * Unexpected next to receive (Nr)
1292 if ((ns_status
== NS_EXPECTED
) && (nr_status
== NR_UNEXPECTED
))
1295 self
->vr
= (self
->vr
+ 1) % 8;
1297 /* Update Nr received */
1298 irlap_update_nr_received(self
, info
->nr
);
1300 /* Resend rejected frames */
1301 irlap_resend_rejected_frames(self
, CMD_FRAME
);
1303 self
->ack_required
= FALSE
;
1305 /* Make sure we account for the time
1306 * to transmit our frames. See comemnts
1307 * in irlap_send_data_primary_poll().
1309 irlap_start_final_timer(self
, 2 * self
->final_timeout
);
1311 /* Keep state, do not move this line */
1312 irlap_next_state(self
, LAP_NRM_P
);
1314 irlap_data_indication(self
, skb
, FALSE
);
1317 * Do not resend frames until the last
1318 * frame has arrived from the other
1319 * device. This is not documented in
1322 self
->vr
= (self
->vr
+ 1) % 8;
1324 /* Update Nr received */
1325 irlap_update_nr_received(self
, info
->nr
);
1327 self
->ack_required
= FALSE
;
1329 /* Keep state, do not move this line!*/
1330 irlap_next_state(self
, LAP_NRM_P
);
1332 irlap_data_indication(self
, skb
, FALSE
);
1337 * Unexpected next to send (Ns) and next to receive (Nr)
1338 * Not documented by IrLAP!
1340 if ((ns_status
== NS_UNEXPECTED
) &&
1341 (nr_status
== NR_UNEXPECTED
))
1343 IRDA_DEBUG(4, "%s(), unexpected nr and ns!\n",
1346 /* Resend rejected frames */
1347 irlap_resend_rejected_frames(self
, CMD_FRAME
);
1349 /* Give peer some time to retransmit!
1350 * But account for our own Tx. */
1351 irlap_start_final_timer(self
, 2 * self
->final_timeout
);
1353 /* Keep state, do not move this line */
1354 irlap_next_state(self
, LAP_NRM_P
);
1356 /* Update Nr received */
1357 /* irlap_update_nr_received( info->nr); */
1359 self
->ack_required
= FALSE
;
1367 if ((nr_status
== NR_INVALID
) || (ns_status
== NS_INVALID
)) {
1369 del_timer(&self
->final_timer
);
1371 irlap_next_state(self
, LAP_RESET_WAIT
);
1373 irlap_disconnect_indication(self
, LAP_RESET_INDICATION
);
1374 self
->xmitflag
= TRUE
;
1376 del_timer(&self
->final_timer
);
1378 irlap_disconnect_indication(self
, LAP_RESET_INDICATION
);
1380 self
->xmitflag
= FALSE
;
1384 IRDA_DEBUG(1, "%s(), Not implemented!\n", __func__
);
1385 IRDA_DEBUG(1, "%s(), event=%s, ns_status=%d, nr_status=%d\n",
1386 __func__
, irlap_event
[event
], ns_status
, nr_status
);
1389 /* Poll bit cleared? */
1391 irlap_data_indication(self
, skb
, TRUE
);
1392 irlap_next_state(self
, LAP_NRM_P
);
1394 del_timer(&self
->final_timer
);
1395 irlap_data_indication(self
, skb
, TRUE
);
1396 irlap_next_state(self
, LAP_XMIT_P
);
1397 IRDA_DEBUG(1, "%s: RECV_UI_FRAME: next state %s\n", __func__
, irlap_state
[self
->state
]);
1398 irlap_start_poll_timer(self
, self
->poll_timeout
);
1403 * If you get a RR, the remote isn't busy anymore,
1404 * no matter what the NR
1406 self
->remote_busy
= FALSE
;
1408 /* Stop final timer */
1409 del_timer(&self
->final_timer
);
1414 ret
= irlap_validate_nr_received(self
, info
->nr
);
1415 if (ret
== NR_EXPECTED
) {
1416 /* Update Nr received */
1417 irlap_update_nr_received(self
, info
->nr
);
1420 * Got expected NR, so reset the retry_count. This
1421 * is not done by the IrLAP standard , which is
1424 self
->retry_count
= 0;
1425 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1427 irlap_next_state(self
, LAP_XMIT_P
);
1429 /* Start poll timer */
1430 irlap_start_poll_timer(self
, self
->poll_timeout
);
1431 } else if (ret
== NR_UNEXPECTED
) {
1432 IRDA_ASSERT(info
!= NULL
, return -1;);
1437 /* Update Nr received */
1438 irlap_update_nr_received(self
, info
->nr
);
1440 IRDA_DEBUG(4, "RECV_RR_FRAME: Retrans:%d, nr=%d, va=%d, "
1442 self
->retry_count
, info
->nr
, self
->va
,
1443 self
->vs
, self
->vr
);
1445 /* Resend rejected frames */
1446 irlap_resend_rejected_frames(self
, CMD_FRAME
);
1447 irlap_start_final_timer(self
, self
->final_timeout
* 2);
1449 irlap_next_state(self
, LAP_NRM_P
);
1450 } else if (ret
== NR_INVALID
) {
1451 IRDA_DEBUG(1, "%s(), Received RR with "
1452 "invalid nr !\n", __func__
);
1454 irlap_next_state(self
, LAP_RESET_WAIT
);
1456 irlap_disconnect_indication(self
, LAP_RESET_INDICATION
);
1457 self
->xmitflag
= TRUE
;
1461 IRDA_ASSERT(info
!= NULL
, return -1;);
1463 /* Stop final timer */
1464 del_timer(&self
->final_timer
);
1465 self
->remote_busy
= TRUE
;
1467 /* Update Nr received */
1468 irlap_update_nr_received(self
, info
->nr
);
1469 irlap_next_state(self
, LAP_XMIT_P
);
1471 /* Start poll timer */
1472 irlap_start_poll_timer(self
, self
->poll_timeout
);
1475 del_timer(&self
->final_timer
);
1476 self
->xmitflag
= TRUE
;
1477 irlap_next_state(self
, LAP_RESET_WAIT
);
1478 irlap_reset_indication(self
);
1480 case FINAL_TIMER_EXPIRED
:
1482 * We are allowed to wait for additional 300 ms if
1483 * final timer expires when we are in the middle
1484 * of receiving a frame (page 45, IrLAP). Check that
1485 * we only do this once for each frame.
1487 if (irda_device_is_receiving(self
->netdev
) && !self
->add_wait
) {
1488 IRDA_DEBUG(1, "FINAL_TIMER_EXPIRED when receiving a "
1489 "frame! Waiting a little bit more!\n");
1490 irlap_start_final_timer(self
, msecs_to_jiffies(300));
1493 * Don't allow this to happen one more time in a row,
1494 * or else we can get a pretty tight loop here if
1495 * if we only receive half a frame. DB.
1497 self
->add_wait
= TRUE
;
1500 self
->add_wait
= FALSE
;
1502 /* N2 is the disconnect timer. Until we reach it, we retry */
1503 if (self
->retry_count
< self
->N2
) {
1504 if (skb_peek(&self
->wx_list
) == NULL
) {
1505 /* Retry sending the pf bit to the secondary */
1506 IRDA_DEBUG(4, "nrm_p: resending rr");
1507 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1508 irlap_send_rr_frame(self
, CMD_FRAME
);
1510 IRDA_DEBUG(4, "nrm_p: resend frames");
1511 irlap_resend_rejected_frames(self
, CMD_FRAME
);
1514 irlap_start_final_timer(self
, self
->final_timeout
);
1515 self
->retry_count
++;
1516 IRDA_DEBUG(4, "irlap_state_nrm_p: FINAL_TIMER_EXPIRED:"
1517 " retry_count=%d\n", self
->retry_count
);
1519 /* Early warning event. I'm using a pretty liberal
1520 * interpretation of the spec and generate an event
1521 * every time the timer is multiple of N1 (and not
1522 * only the first time). This allow application
1523 * to know precisely if connectivity restart...
1525 if((self
->retry_count
% self
->N1
) == 0)
1526 irlap_status_indication(self
,
1527 STATUS_NO_ACTIVITY
);
1531 irlap_apply_default_connection_parameters(self
);
1533 /* Always switch state before calling upper layers */
1534 irlap_next_state(self
, LAP_NDM
);
1535 irlap_disconnect_indication(self
, LAP_NO_RESPONSE
);
1539 irlap_update_nr_received(self
, info
->nr
);
1540 if (self
->remote_busy
) {
1541 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1542 irlap_send_rr_frame(self
, CMD_FRAME
);
1544 irlap_resend_rejected_frames(self
, CMD_FRAME
);
1545 irlap_start_final_timer(self
, 2 * self
->final_timeout
);
1548 irlap_update_nr_received(self
, info
->nr
);
1549 if (self
->remote_busy
) {
1550 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1551 irlap_send_rr_frame(self
, CMD_FRAME
);
1553 irlap_resend_rejected_frame(self
, CMD_FRAME
);
1554 irlap_start_final_timer(self
, 2 * self
->final_timeout
);
1557 IRDA_DEBUG(1, "%s(), RECV_RD_RSP\n", __func__
);
1559 irlap_flush_all_queues(self
);
1560 irlap_next_state(self
, LAP_XMIT_P
);
1561 /* Call back the LAP state machine to do a proper disconnect */
1562 irlap_disconnect_request(self
);
1565 IRDA_DEBUG(1, "%s(), Unknown event %s\n",
1566 __func__
, irlap_event
[event
]);
1575 * Function irlap_state_reset_wait (event, skb, info)
1577 * We have informed the service user of a reset condition, and is
1578 * awaiting reset of disconnect request.
1581 static int irlap_state_reset_wait(struct irlap_cb
*self
, IRLAP_EVENT event
,
1582 struct sk_buff
*skb
, struct irlap_info
*info
)
1586 IRDA_DEBUG(3, "%s(), event = %s\n", __func__
, irlap_event
[event
]);
1588 IRDA_ASSERT(self
!= NULL
, return -1;);
1589 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
1593 if (self
->xmitflag
) {
1594 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1595 irlap_send_snrm_frame(self
, NULL
);
1596 irlap_start_final_timer(self
, self
->final_timeout
);
1597 irlap_next_state(self
, LAP_RESET
);
1599 irlap_start_final_timer(self
, self
->final_timeout
);
1600 irlap_next_state(self
, LAP_RESET
);
1603 case DISCONNECT_REQUEST
:
1604 irlap_wait_min_turn_around( self
, &self
->qos_tx
);
1605 irlap_send_disc_frame( self
);
1606 irlap_flush_all_queues( self
);
1607 irlap_start_final_timer( self
, self
->final_timeout
);
1608 self
->retry_count
= 0;
1609 irlap_next_state( self
, LAP_PCLOSE
);
1612 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __func__
,
1613 irlap_event
[event
]);
1622 * Function irlap_state_reset (self, event, skb, info)
1624 * We have sent a SNRM reset command to the peer layer, and is awaiting
1628 static int irlap_state_reset(struct irlap_cb
*self
, IRLAP_EVENT event
,
1629 struct sk_buff
*skb
, struct irlap_info
*info
)
1633 IRDA_DEBUG(3, "%s(), event = %s\n", __func__
, irlap_event
[event
]);
1635 IRDA_ASSERT(self
!= NULL
, return -1;);
1636 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
1640 del_timer(&self
->final_timer
);
1642 irlap_apply_default_connection_parameters(self
);
1644 /* Always switch state before calling upper layers */
1645 irlap_next_state(self
, LAP_NDM
);
1647 irlap_disconnect_indication(self
, LAP_NO_RESPONSE
);
1651 del_timer(&self
->final_timer
);
1653 /* Initiate connection state */
1654 irlap_initiate_connection_state(self
);
1656 irlap_reset_confirm();
1658 self
->remote_busy
= FALSE
;
1660 irlap_next_state(self
, LAP_XMIT_P
);
1662 irlap_start_poll_timer(self
, self
->poll_timeout
);
1665 case FINAL_TIMER_EXPIRED
:
1666 if (self
->retry_count
< 3) {
1667 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1669 IRDA_ASSERT(self
->netdev
!= NULL
, return -1;);
1670 irlap_send_snrm_frame(self
, self
->qos_dev
);
1672 self
->retry_count
++; /* Experimental!! */
1674 irlap_start_final_timer(self
, self
->final_timeout
);
1675 irlap_next_state(self
, LAP_RESET
);
1676 } else if (self
->retry_count
>= self
->N3
) {
1677 irlap_apply_default_connection_parameters(self
);
1679 /* Always switch state before calling upper layers */
1680 irlap_next_state(self
, LAP_NDM
);
1682 irlap_disconnect_indication(self
, LAP_NO_RESPONSE
);
1687 * SNRM frame is not allowed to contain an I-field in this
1691 IRDA_DEBUG(3, "%s(), RECV_SNRM_CMD\n", __func__
);
1692 irlap_initiate_connection_state(self
);
1693 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1694 irlap_send_ua_response_frame(self
, &self
->qos_rx
);
1695 irlap_reset_confirm();
1696 irlap_start_wd_timer(self
, self
->wd_timeout
);
1697 irlap_next_state(self
, LAP_NDM
);
1700 "%s(), SNRM frame contained an I field!\n",
1705 IRDA_DEBUG(1, "%s(), Unknown event %s\n",
1706 __func__
, irlap_event
[event
]);
1715 * Function irlap_state_xmit_s (event, skb, info)
1717 * XMIT_S, The secondary station has been given the right to transmit,
1718 * and we therefore do not expect to receive any transmissions from other
1721 static int irlap_state_xmit_s(struct irlap_cb
*self
, IRLAP_EVENT event
,
1722 struct sk_buff
*skb
, struct irlap_info
*info
)
1726 IRDA_DEBUG(4, "%s(), event=%s\n", __func__
, irlap_event
[event
]);
1728 IRDA_ASSERT(self
!= NULL
, return -ENODEV
;);
1729 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -EBADR
;);
1734 * Send frame only if send window > 0
1736 if ((self
->window
> 0) && (!self
->remote_busy
)) {
1738 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1739 struct sk_buff
*skb_next
;
1742 * Same deal as in irlap_state_xmit_p(), so see
1743 * the comments at that point.
1744 * We are the secondary, so there are only subtle
1745 * differences. - Jean II
1748 /* Check if a subsequent skb exist and would fit in
1749 * the current window (with respect to turnaround
1750 * time). - Jean II */
1751 skb_next
= skb_peek(&self
->txq
);
1752 nextfit
= ((skb_next
!= NULL
) &&
1753 ((skb_next
->len
+ skb
->len
) <=
1757 * Test if we have transmitted more bytes over the
1758 * link than its possible to do with the current
1759 * speed and turn-around-time.
1761 if((!nextfit
) && (skb
->len
> self
->bytes_left
)) {
1762 IRDA_DEBUG(0, "%s(), Not allowed to transmit"
1763 " more bytes!\n", __func__
);
1764 /* Requeue the skb */
1765 skb_queue_head(&self
->txq
, skb_get(skb
));
1768 * Switch to NRM_S, this is only possible
1769 * when we are in secondary mode, since we
1770 * must be sure that we don't miss any RR
1773 self
->window
= self
->window_size
;
1774 self
->bytes_left
= self
->line_capacity
;
1775 irlap_start_wd_timer(self
, self
->wd_timeout
);
1777 irlap_next_state(self
, LAP_NRM_S
);
1778 /* Slight difference with primary :
1779 * here we would wait for the other side to
1780 * expire the turnaround. - Jean II */
1782 return -EPROTO
; /* Try again later */
1784 /* Substract space used by this skb */
1785 self
->bytes_left
-= skb
->len
;
1786 #else /* CONFIG_IRDA_DYNAMIC_WINDOW */
1787 /* Window has been adjusted for the max packet
1788 * size, so much simpler... - Jean II */
1789 nextfit
= !skb_queue_empty(&self
->txq
);
1790 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1792 * Send data with final bit cleared only if window > 1
1793 * and there is more frames to be sent
1795 if ((self
->window
> 1) && (nextfit
)) {
1796 irlap_send_data_secondary(self
, skb
);
1797 irlap_next_state(self
, LAP_XMIT_S
);
1799 irlap_send_data_secondary_final(self
, skb
);
1800 irlap_next_state(self
, LAP_NRM_S
);
1803 * Make sure state machine does not try to send
1809 IRDA_DEBUG(2, "%s(), Unable to send!\n", __func__
);
1810 skb_queue_head(&self
->txq
, skb_get(skb
));
1814 case DISCONNECT_REQUEST
:
1815 irlap_send_rd_frame(self
);
1816 irlap_flush_all_queues(self
);
1817 irlap_start_wd_timer(self
, self
->wd_timeout
);
1818 irlap_next_state(self
, LAP_SCLOSE
);
1821 /* Nothing to do, irlap_do_event() will send the packet
1822 * when we return... - Jean II */
1825 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __func__
,
1826 irlap_event
[event
]);
1835 * Function irlap_state_nrm_s (event, skb, info)
1837 * NRM_S (Normal Response Mode as Secondary) state, in this state we are
1838 * expecting to receive frames from the primary station
1841 static int irlap_state_nrm_s(struct irlap_cb
*self
, IRLAP_EVENT event
,
1842 struct sk_buff
*skb
, struct irlap_info
*info
)
1848 IRDA_DEBUG(4, "%s(), event=%s\n", __func__
, irlap_event
[ event
]);
1850 IRDA_ASSERT(self
!= NULL
, return -1;);
1851 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
1854 case RECV_I_CMD
: /* Optimize for the common case */
1855 IRDA_DEBUG(4, "%s(), event=%s nr=%d, vs=%d, ns=%d, "
1856 "vr=%d, pf=%d\n", __func__
,
1857 irlap_event
[event
], info
->nr
,
1858 self
->vs
, info
->ns
, self
->vr
, info
->pf
);
1860 self
->retry_count
= 0;
1862 ns_status
= irlap_validate_ns_received(self
, info
->ns
);
1863 nr_status
= irlap_validate_nr_received(self
, info
->nr
);
1865 * Check for expected I(nformation) frame
1867 if ((ns_status
== NS_EXPECTED
) && (nr_status
== NR_EXPECTED
)) {
1869 /* Update Vr (next frame for us to receive) */
1870 self
->vr
= (self
->vr
+ 1) % 8;
1872 /* Update Nr received */
1873 irlap_update_nr_received(self
, info
->nr
);
1880 self
->ack_required
= TRUE
;
1883 * Starting WD-timer here is optional, but
1884 * not recommended. Note 6 IrLAP p. 83
1886 /* Keep state, do not move this line */
1887 irlap_next_state(self
, LAP_NRM_S
);
1889 irlap_data_indication(self
, skb
, FALSE
);
1893 * We should wait before sending RR, and
1894 * also before changing to XMIT_S
1895 * state. (note 1, IrLAP p. 82)
1897 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1900 * Give higher layers a chance to
1901 * immediately reply with some data before
1902 * we decide if we should send a RR frame
1905 irlap_data_indication(self
, skb
, FALSE
);
1907 /* Any pending data requests? */
1908 if (!skb_queue_empty(&self
->txq
) &&
1911 self
->ack_required
= TRUE
;
1913 del_timer(&self
->wd_timer
);
1915 irlap_next_state(self
, LAP_XMIT_S
);
1917 irlap_send_rr_frame(self
, RSP_FRAME
);
1918 irlap_start_wd_timer(self
,
1921 /* Keep the state */
1922 irlap_next_state(self
, LAP_NRM_S
);
1928 * Check for Unexpected next to send (Ns)
1930 if ((ns_status
== NS_UNEXPECTED
) && (nr_status
== NR_EXPECTED
))
1932 /* Unexpected next to send, with final bit cleared */
1934 irlap_update_nr_received(self
, info
->nr
);
1936 irlap_start_wd_timer(self
, self
->wd_timeout
);
1938 /* Update Nr received */
1939 irlap_update_nr_received(self
, info
->nr
);
1941 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1942 irlap_send_rr_frame(self
, RSP_FRAME
);
1944 irlap_start_wd_timer(self
, self
->wd_timeout
);
1950 * Unexpected Next to Receive(NR) ?
1952 if ((ns_status
== NS_EXPECTED
) && (nr_status
== NR_UNEXPECTED
))
1955 IRDA_DEBUG(4, "RECV_I_RSP: frame(s) lost\n");
1957 self
->vr
= (self
->vr
+ 1) % 8;
1959 /* Update Nr received */
1960 irlap_update_nr_received(self
, info
->nr
);
1962 /* Resend rejected frames */
1963 irlap_resend_rejected_frames(self
, RSP_FRAME
);
1965 /* Keep state, do not move this line */
1966 irlap_next_state(self
, LAP_NRM_S
);
1968 irlap_data_indication(self
, skb
, FALSE
);
1969 irlap_start_wd_timer(self
, self
->wd_timeout
);
1973 * This is not documented in IrLAP!! Unexpected NR
1974 * with poll bit cleared
1977 self
->vr
= (self
->vr
+ 1) % 8;
1979 /* Update Nr received */
1980 irlap_update_nr_received(self
, info
->nr
);
1982 /* Keep state, do not move this line */
1983 irlap_next_state(self
, LAP_NRM_S
);
1985 irlap_data_indication(self
, skb
, FALSE
);
1986 irlap_start_wd_timer(self
, self
->wd_timeout
);
1991 if (ret
== NR_INVALID
) {
1992 IRDA_DEBUG(0, "NRM_S, NR_INVALID not implemented!\n");
1994 if (ret
== NS_INVALID
) {
1995 IRDA_DEBUG(0, "NRM_S, NS_INVALID not implemented!\n");
2003 irlap_data_indication(self
, skb
, TRUE
);
2004 irlap_next_state(self
, LAP_NRM_S
); /* Keep state */
2007 * Any pending data requests?
2009 if (!skb_queue_empty(&self
->txq
) &&
2010 (self
->window
> 0) && !self
->remote_busy
)
2012 irlap_data_indication(self
, skb
, TRUE
);
2014 del_timer(&self
->wd_timer
);
2016 irlap_next_state(self
, LAP_XMIT_S
);
2018 irlap_data_indication(self
, skb
, TRUE
);
2020 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2022 irlap_send_rr_frame(self
, RSP_FRAME
);
2023 self
->ack_required
= FALSE
;
2025 irlap_start_wd_timer(self
, self
->wd_timeout
);
2027 /* Keep the state */
2028 irlap_next_state(self
, LAP_NRM_S
);
2033 self
->retry_count
= 0;
2038 nr_status
= irlap_validate_nr_received(self
, info
->nr
);
2039 if (nr_status
== NR_EXPECTED
) {
2040 if (!skb_queue_empty(&self
->txq
) &&
2041 (self
->window
> 0)) {
2042 self
->remote_busy
= FALSE
;
2044 /* Update Nr received */
2045 irlap_update_nr_received(self
, info
->nr
);
2046 del_timer(&self
->wd_timer
);
2048 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2049 irlap_next_state(self
, LAP_XMIT_S
);
2051 self
->remote_busy
= FALSE
;
2052 /* Update Nr received */
2053 irlap_update_nr_received(self
, info
->nr
);
2054 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2055 irlap_start_wd_timer(self
, self
->wd_timeout
);
2057 /* Note : if the link is idle (this case),
2058 * we never go in XMIT_S, so we never get a
2059 * chance to process any DISCONNECT_REQUEST.
2060 * Do it now ! - Jean II */
2061 if (self
->disconnect_pending
) {
2063 irlap_send_rd_frame(self
);
2064 irlap_flush_all_queues(self
);
2066 irlap_next_state(self
, LAP_SCLOSE
);
2068 /* Just send back pf bit */
2069 irlap_send_rr_frame(self
, RSP_FRAME
);
2071 irlap_next_state(self
, LAP_NRM_S
);
2074 } else if (nr_status
== NR_UNEXPECTED
) {
2075 self
->remote_busy
= FALSE
;
2076 irlap_update_nr_received(self
, info
->nr
);
2077 irlap_resend_rejected_frames(self
, RSP_FRAME
);
2079 irlap_start_wd_timer(self
, self
->wd_timeout
);
2082 irlap_next_state(self
, LAP_NRM_S
);
2084 IRDA_DEBUG(1, "%s(), invalid nr not implemented!\n",
2089 /* SNRM frame is not allowed to contain an I-field */
2091 del_timer(&self
->wd_timer
);
2092 IRDA_DEBUG(1, "%s(), received SNRM cmd\n", __func__
);
2093 irlap_next_state(self
, LAP_RESET_CHECK
);
2095 irlap_reset_indication(self
);
2098 "%s(), SNRM frame contained an I-field!\n",
2104 irlap_update_nr_received(self
, info
->nr
);
2105 if (self
->remote_busy
) {
2106 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2107 irlap_send_rr_frame(self
, RSP_FRAME
);
2109 irlap_resend_rejected_frames(self
, RSP_FRAME
);
2110 irlap_start_wd_timer(self
, self
->wd_timeout
);
2113 irlap_update_nr_received(self
, info
->nr
);
2114 if (self
->remote_busy
) {
2115 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2116 irlap_send_rr_frame(self
, RSP_FRAME
);
2118 irlap_resend_rejected_frame(self
, RSP_FRAME
);
2119 irlap_start_wd_timer(self
, self
->wd_timeout
);
2121 case WD_TIMER_EXPIRED
:
2123 * Wait until retry_count * n matches negotiated threshold/
2124 * disconnect time (note 2 in IrLAP p. 82)
2126 * Similar to irlap_state_nrm_p() -> FINAL_TIMER_EXPIRED
2127 * Note : self->wd_timeout = (self->final_timeout * 2),
2128 * which explain why we use (self->N2 / 2) here !!!
2131 IRDA_DEBUG(1, "%s(), retry_count = %d\n", __func__
,
2134 if (self
->retry_count
< (self
->N2
/ 2)) {
2135 /* No retry, just wait for primary */
2136 irlap_start_wd_timer(self
, self
->wd_timeout
);
2137 self
->retry_count
++;
2139 if((self
->retry_count
% (self
->N1
/ 2)) == 0)
2140 irlap_status_indication(self
,
2141 STATUS_NO_ACTIVITY
);
2143 irlap_apply_default_connection_parameters(self
);
2145 /* Always switch state before calling upper layers */
2146 irlap_next_state(self
, LAP_NDM
);
2147 irlap_disconnect_indication(self
, LAP_NO_RESPONSE
);
2151 /* Always switch state before calling upper layers */
2152 irlap_next_state(self
, LAP_NDM
);
2154 /* Send disconnect response */
2155 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2156 irlap_send_ua_response_frame(self
, NULL
);
2158 del_timer(&self
->wd_timer
);
2159 irlap_flush_all_queues(self
);
2160 /* Set default link parameters */
2161 irlap_apply_default_connection_parameters(self
);
2163 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
2165 case RECV_DISCOVERY_XID_CMD
:
2166 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2167 irlap_send_rr_frame(self
, RSP_FRAME
);
2168 self
->ack_required
= TRUE
;
2169 irlap_start_wd_timer(self
, self
->wd_timeout
);
2170 irlap_next_state(self
, LAP_NRM_S
);
2174 /* Remove test frame header (only LAP header in NRM) */
2175 skb_pull(skb
, LAP_ADDR_HEADER
+ LAP_CTRL_HEADER
);
2177 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2178 irlap_start_wd_timer(self
, self
->wd_timeout
);
2180 /* Send response (info will be copied) */
2181 irlap_send_test_frame(self
, self
->caddr
, info
->daddr
, skb
);
2184 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __func__
,
2185 event
, irlap_event
[event
]);
2194 * Function irlap_state_sclose (self, event, skb, info)
2196 static int irlap_state_sclose(struct irlap_cb
*self
, IRLAP_EVENT event
,
2197 struct sk_buff
*skb
, struct irlap_info
*info
)
2201 IRDA_DEBUG(1, "%s()\n", __func__
);
2203 IRDA_ASSERT(self
!= NULL
, return -ENODEV
;);
2204 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -EBADR
;);
2208 /* Always switch state before calling upper layers */
2209 irlap_next_state(self
, LAP_NDM
);
2211 /* Send disconnect response */
2212 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2213 irlap_send_ua_response_frame(self
, NULL
);
2215 del_timer(&self
->wd_timer
);
2216 /* Set default link parameters */
2217 irlap_apply_default_connection_parameters(self
);
2219 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
2222 /* IrLAP-1.1 p.82: in SCLOSE, S and I type RSP frames
2223 * shall take us down into default NDM state, like DM_RSP
2230 /* Always switch state before calling upper layers */
2231 irlap_next_state(self
, LAP_NDM
);
2233 del_timer(&self
->wd_timer
);
2234 irlap_apply_default_connection_parameters(self
);
2236 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
2238 case WD_TIMER_EXPIRED
:
2239 /* Always switch state before calling upper layers */
2240 irlap_next_state(self
, LAP_NDM
);
2242 irlap_apply_default_connection_parameters(self
);
2244 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
2247 /* IrLAP-1.1 p.82: in SCLOSE, basically any received frame
2248 * with pf=1 shall restart the wd-timer and resend the rd:rsp
2250 if (info
!= NULL
&& info
->pf
) {
2251 del_timer(&self
->wd_timer
);
2252 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2253 irlap_send_rd_frame(self
);
2254 irlap_start_wd_timer(self
, self
->wd_timeout
);
2255 break; /* stay in SCLOSE */
2258 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __func__
,
2259 event
, irlap_event
[event
]);
2268 static int irlap_state_reset_check( struct irlap_cb
*self
, IRLAP_EVENT event
,
2269 struct sk_buff
*skb
,
2270 struct irlap_info
*info
)
2274 IRDA_DEBUG(1, "%s(), event=%s\n", __func__
, irlap_event
[event
]);
2276 IRDA_ASSERT(self
!= NULL
, return -ENODEV
;);
2277 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -EBADR
;);
2280 case RESET_RESPONSE
:
2281 irlap_send_ua_response_frame(self
, &self
->qos_rx
);
2282 irlap_initiate_connection_state(self
);
2283 irlap_start_wd_timer(self
, WD_TIMEOUT
);
2284 irlap_flush_all_queues(self
);
2286 irlap_next_state(self
, LAP_NRM_S
);
2288 case DISCONNECT_REQUEST
:
2289 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2290 irlap_send_rd_frame(self
);
2291 irlap_start_wd_timer(self
, WD_TIMEOUT
);
2292 irlap_next_state(self
, LAP_SCLOSE
);
2295 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __func__
,
2296 event
, irlap_event
[event
]);