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-2001 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/config.h>
29 #include <linux/string.h>
30 #include <linux/kernel.h>
31 #include <linux/delay.h>
32 #include <linux/skbuff.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>
43 #include <net/irda/irda_device.h>
45 #if CONFIG_IRDA_FAST_RR
46 int sysctl_fast_poll_increase = 50;
49 static int irlap_state_ndm (struct irlap_cb *self, IRLAP_EVENT event,
50 struct sk_buff *skb, struct irlap_info *info);
51 static int irlap_state_query (struct irlap_cb *self, IRLAP_EVENT event,
52 struct sk_buff *skb, struct irlap_info *info);
53 static int irlap_state_reply (struct irlap_cb *self, IRLAP_EVENT event,
54 struct sk_buff *skb, struct irlap_info *info);
55 static int irlap_state_conn (struct irlap_cb *self, IRLAP_EVENT event,
56 struct sk_buff *skb, struct irlap_info *info);
57 static int irlap_state_setup (struct irlap_cb *self, IRLAP_EVENT event,
58 struct sk_buff *skb, struct irlap_info *info);
59 static int irlap_state_offline(struct irlap_cb *self, IRLAP_EVENT event,
60 struct sk_buff *skb, struct irlap_info *info);
61 static int irlap_state_xmit_p (struct irlap_cb *self, IRLAP_EVENT event,
62 struct sk_buff *skb, struct irlap_info *info);
63 static int irlap_state_pclose (struct irlap_cb *self, IRLAP_EVENT event,
64 struct sk_buff *skb, struct irlap_info *info);
65 static int irlap_state_nrm_p (struct irlap_cb *self, IRLAP_EVENT event,
66 struct sk_buff *skb, struct irlap_info *info);
67 static int irlap_state_reset_wait(struct irlap_cb *self, IRLAP_EVENT event,
68 struct sk_buff *skb, struct irlap_info *info);
69 static int irlap_state_reset (struct irlap_cb *self, IRLAP_EVENT event,
70 struct sk_buff *skb, struct irlap_info *info);
71 static int irlap_state_nrm_s (struct irlap_cb *self, IRLAP_EVENT event,
72 struct sk_buff *skb, struct irlap_info *info);
73 static int irlap_state_xmit_s (struct irlap_cb *self, IRLAP_EVENT event,
74 struct sk_buff *skb, struct irlap_info *info);
75 static int irlap_state_sclose (struct irlap_cb *self, IRLAP_EVENT event,
76 struct sk_buff *skb, struct irlap_info *info);
77 static int irlap_state_reset_check(struct irlap_cb *, IRLAP_EVENT event,
78 struct sk_buff *, struct irlap_info *);
80 #ifdef CONFIG_IRDA_DEBUG
81 static const char *irlap_event[] = {
91 "RECV_DISCOVERY_XID_CMD",
92 "RECV_DISCOVERY_XID_RSP",
112 "SLOT_TIMER_EXPIRED",
113 "QUERY_TIMER_EXPIRED",
114 "FINAL_TIMER_EXPIRED",
115 "POLL_TIMER_EXPIRED",
116 "DISCOVERY_TIMER_EXPIRED",
118 "BACKOFF_TIMER_EXPIRED",
119 "MEDIA_BUSY_TIMER_EXPIRED",
121 #endif /* CONFIG_IRDA_DEBUG */
123 const char *irlap_state[] = {
141 static int (*state[])(struct irlap_cb *self, IRLAP_EVENT event,
142 struct sk_buff *skb, struct irlap_info *info) =
153 irlap_state_reset_wait,
158 irlap_state_reset_check,
162 * Function irda_poll_timer_expired (data)
164 * Poll timer has expired. Normally we must now send a RR frame to the
167 static void irlap_poll_timer_expired(void *data)
169 struct irlap_cb *self = (struct irlap_cb *) data;
171 ASSERT(self != NULL, return;);
172 ASSERT(self->magic == LAP_MAGIC, return;);
174 irlap_do_event(self, POLL_TIMER_EXPIRED, NULL, NULL);
178 * Calculate and set time before we will have to send back the pf bit
179 * to the peer. Use in primary.
180 * Make sure that state is XMIT_P/XMIT_S when calling this function
181 * (and that nobody messed up with the state). - Jean II
183 void irlap_start_poll_timer(struct irlap_cb *self, int timeout)
185 ASSERT(self != NULL, return;);
186 ASSERT(self->magic == LAP_MAGIC, return;);
188 #ifdef CONFIG_IRDA_FAST_RR
190 * Send out the RR frames faster if our own transmit queue is empty, or
191 * if the peer is busy. The effect is a much faster conversation
193 if ((skb_queue_len(&self->txq) == 0) || (self->remote_busy)) {
194 if (self->fast_RR == TRUE) {
196 * Assert that the fast poll timer has not reached the
197 * normal poll timer yet
199 if (self->fast_RR_timeout < timeout) {
201 * FIXME: this should be a more configurable
204 self->fast_RR_timeout +=
205 (sysctl_fast_poll_increase * HZ/1000);
207 /* Use this fast(er) timeout instead */
208 timeout = self->fast_RR_timeout;
211 self->fast_RR = TRUE;
213 /* Start with just 0 ms */
214 self->fast_RR_timeout = 0;
218 self->fast_RR = FALSE;
220 IRDA_DEBUG(3, "%s(), timeout=%d (%ld)\n", __FUNCTION__, timeout, jiffies);
221 #endif /* CONFIG_IRDA_FAST_RR */
224 irlap_do_event(self, POLL_TIMER_EXPIRED, NULL, NULL);
226 irda_start_timer(&self->poll_timer, timeout, self,
227 irlap_poll_timer_expired);
231 * Function irlap_do_event (event, skb, info)
233 * Rushes through the state machine without any delay. If state == XMIT
234 * then send queued data frames.
236 void irlap_do_event(struct irlap_cb *self, IRLAP_EVENT event,
237 struct sk_buff *skb, struct irlap_info *info)
241 if (!self || self->magic != LAP_MAGIC)
244 IRDA_DEBUG(3, "%s(), event = %s, state = %s\n", __FUNCTION__,
245 irlap_event[event], irlap_state[self->state]);
247 ret = (*state[self->state])(self, event, skb, info);
250 * Check if there are any pending events that needs to be executed
252 switch (self->state) {
253 case LAP_XMIT_P: /* FALLTHROUGH */
256 * We just received the pf bit and are at the beginning
257 * of a new LAP transmit window.
258 * Check if there are any queued data frames, and do not
259 * try to disconnect link if we send any data frames, since
260 * that will change the state away form XMIT
262 IRDA_DEBUG(2, "%s() : queue len = %d\n", __FUNCTION__,
263 skb_queue_len(&self->txq));
265 if (skb_queue_len(&self->txq)) {
266 /* Prevent race conditions with irlap_data_request() */
267 self->local_busy = TRUE;
269 /* Theory of operation.
270 * We send frames up to when we fill the window or
271 * reach line capacity. Those frames will queue up
272 * in the device queue, and the driver will slowly
274 * After each frame that we send, we poll the higher
275 * layer for more data. It's the right time to do
276 * that because the link layer need to perform the mtt
277 * and then send the first frame, so we can afford
278 * to send a bit of time in kernel space.
279 * The explicit flow indication allow to minimise
280 * buffers (== lower latency), to avoid higher layer
281 * polling via timers (== less context switches) and
282 * to implement a crude scheduler - Jean II */
284 /* Try to send away all queued data frames */
285 while ((skb = skb_dequeue(&self->txq)) != NULL) {
287 ret = (*state[self->state])(self, SEND_I_CMD,
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_next_state (self, state)
319 * Switches state and provides debug information
322 static inline void irlap_next_state(struct irlap_cb *self, IRLAP_STATE state)
325 if (!self || self->magic != LAP_MAGIC)
328 IRDA_DEBUG(4, "next LAP state = %s\n", irlap_state[state]);
334 * Function irlap_state_ndm (event, skb, frame)
336 * NDM (Normal Disconnected Mode) state
339 static int irlap_state_ndm(struct irlap_cb *self, IRLAP_EVENT event,
340 struct sk_buff *skb, struct irlap_info *info)
342 discovery_t *discovery_rsp;
345 ASSERT(self != NULL, return -1;);
346 ASSERT(self->magic == LAP_MAGIC, return -1;);
349 case CONNECT_REQUEST:
350 ASSERT(self->netdev != NULL, return -1;);
352 if (self->media_busy) {
353 /* Note : this will never happen, because we test
354 * media busy in irlap_connect_request() and
355 * postpone the event... - Jean II */
356 IRDA_DEBUG(0, "%s(), CONNECT_REQUEST: media busy!\n", __FUNCTION__);
358 /* Always switch state before calling upper layers */
359 irlap_next_state(self, LAP_NDM);
361 irlap_disconnect_indication(self, LAP_MEDIA_BUSY);
363 irlap_send_snrm_frame(self, &self->qos_rx);
365 /* Start Final-bit timer */
366 irlap_start_final_timer(self, self->final_timeout);
368 self->retry_count = 0;
369 irlap_next_state(self, LAP_SETUP);
373 /* Check if the frame contains and I field */
375 self->daddr = info->daddr;
376 self->caddr = info->caddr;
378 irlap_next_state(self, LAP_CONN);
380 irlap_connect_indication(self, skb);
382 IRDA_DEBUG(0, "%s(), SNRM frame does not "
383 "contain an I field!\n", __FUNCTION__);
386 case DISCOVERY_REQUEST:
387 ASSERT(info != NULL, return -1;);
389 if (self->media_busy) {
390 IRDA_DEBUG(0, "%s(), media busy!\n", __FUNCTION__);
391 /* irlap->log.condition = MEDIA_BUSY; */
393 /* This will make IrLMP try again */
394 irlap_discovery_confirm(self, NULL);
395 /* Note : the discovery log is not cleaned up here,
396 * it will be done in irlap_discovery_request()
403 irlap_send_discovery_xid_frame(self, info->S, info->s, TRUE,
405 self->frame_sent = FALSE;
408 irlap_start_slot_timer(self, self->slot_timeout);
409 irlap_next_state(self, LAP_QUERY);
411 case RECV_DISCOVERY_XID_CMD:
412 ASSERT(info != NULL, return -1;);
414 /* Assert that this is not the final slot */
415 if (info->s <= info->S) {
416 self->slot = irlap_generate_rand_time_slot(info->S,
418 if (self->slot == info->s) {
419 discovery_rsp = irlmp_get_discovery_response();
420 discovery_rsp->daddr = info->daddr;
422 irlap_send_discovery_xid_frame(self, info->S,
426 self->frame_sent = TRUE;
428 self->frame_sent = FALSE;
431 * Remember to multiply the query timeout value with
432 * the number of slots used
434 irlap_start_query_timer(self, QUERY_TIMEOUT*info->S);
435 irlap_next_state(self, LAP_REPLY);
437 /* This is the final slot. How is it possible ?
438 * This would happen is both discoveries are just slightly
439 * offset (if they are in sync, all packets are lost).
440 * Most often, all the discovery requests will be received
441 * in QUERY state (see my comment there), except for the
442 * last frame that will come here.
443 * The big trouble when it happen is that active discovery
444 * doesn't happen, because nobody answer the discoveries
445 * frame of the other guy, so the log shows up empty.
446 * What should we do ?
447 * Not much. It's too late to answer those discovery frames,
448 * so we just pass the info to IrLMP who will put it in the
449 * log (and post an event).
452 IRDA_DEBUG(1, "%s(), Receiving final discovery request, missed the discovery slots :-(\n", __FUNCTION__);
454 /* Last discovery request -> in the log */
455 irlap_discovery_indication(self, info->discovery);
458 case MEDIA_BUSY_TIMER_EXPIRED:
459 /* A bunch of events may be postponed because the media is
460 * busy (usually immediately after we close a connection),
461 * or while we are doing discovery (state query/reply).
462 * In all those cases, the media busy flag will be cleared
463 * when it's OK for us to process those postponed events.
464 * This event is not mentioned in the state machines in the
465 * IrLAP spec. It's because they didn't consider Ultra and
466 * postponing connection request is optional.
468 #ifdef CONFIG_IRDA_ULTRA
469 /* Send any pending Ultra frames if any */
470 if (!skb_queue_empty(&self->txq_ultra)) {
471 /* We don't send the frame, just post an event.
472 * Also, previously this code was in timer.c...
474 ret = (*state[self->state])(self, SEND_UI_FRAME,
477 #endif /* CONFIG_IRDA_ULTRA */
478 /* Check if we should try to connect.
479 * This code was previously in irlap_do_event() */
480 if (self->connect_pending) {
481 self->connect_pending = FALSE;
483 /* This one *should* not pend in this state, except
484 * if a socket try to connect and immediately
485 * disconnect. - clear - Jean II */
486 if (self->disconnect_pending)
487 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
489 ret = (*state[self->state])(self,
492 self->disconnect_pending = FALSE;
494 /* Note : one way to test if this code works well (including
495 * media busy and small busy) is to create a user space
496 * application generating an Ultra packet every 3.05 sec (or
497 * 2.95 sec) and to see how it interact with discovery.
498 * It's fairly easy to check that no packet is lost, that the
499 * packets are postponed during discovery and that after
500 * discovery indication you have a 100ms "gap".
501 * As connection request and Ultra are now processed the same
502 * way, this avoid the tedious job of trying IrLAP connection
503 * in all those cases...
506 #ifdef CONFIG_IRDA_ULTRA
510 /* Only allowed to repeat an operation twice */
511 for (i=0; ((i<2) && (self->media_busy == FALSE)); i++) {
512 skb = skb_dequeue(&self->txq_ultra);
514 irlap_send_ui_frame(self, skb, CBROADCAST,
520 /* Force us to listen 500 ms again */
521 irda_device_set_media_busy(self->netdev, TRUE);
526 /* Only accept broadcast frames in NDM mode */
527 if (info->caddr != CBROADCAST) {
528 IRDA_DEBUG(0, "%s(), not a broadcast frame!\n", __FUNCTION__);
530 irlap_unitdata_indication(self, skb);
532 #endif /* CONFIG_IRDA_ULTRA */
534 /* Remove test frame header */
535 skb_pull(skb, sizeof(struct test_frame));
538 * Send response. This skb will not be sent out again, and
539 * will only be used to send out the same info as the cmd
541 irlap_send_test_frame(self, CBROADCAST, info->daddr, skb);
544 IRDA_DEBUG(0, "%s() not implemented!\n", __FUNCTION__);
547 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
557 * Function irlap_state_query (event, skb, info)
562 static int irlap_state_query(struct irlap_cb *self, IRLAP_EVENT event,
563 struct sk_buff *skb, struct irlap_info *info)
567 ASSERT(self != NULL, return -1;);
568 ASSERT(self->magic == LAP_MAGIC, return -1;);
571 case RECV_DISCOVERY_XID_RSP:
572 ASSERT(info != NULL, return -1;);
573 ASSERT(info->discovery != NULL, return -1;);
575 IRDA_DEBUG(4, "%s(), daddr=%08x\n", __FUNCTION__,
576 info->discovery->daddr);
578 if (!self->discovery_log) {
579 WARNING("%s(), discovery log is gone! "
580 "maybe the discovery timeout has been set to "
581 "short?\n", __FUNCTION__);
584 hashbin_insert(self->discovery_log,
585 (irda_queue_t *) info->discovery,
586 info->discovery->daddr, NULL);
589 /* irlap_next_state(self, LAP_QUERY); */
592 case RECV_DISCOVERY_XID_CMD:
593 /* Yes, it is possible to receive those frames in this mode.
594 * Note that most often the last discovery request won't
595 * occur here but in NDM state (see my comment there).
596 * What should we do ?
597 * Not much. We are currently performing our own discovery,
598 * therefore we can't answer those frames. We don't want
599 * to change state either. We just pass the info to
600 * IrLMP who will put it in the log (and post an event).
604 ASSERT(info != NULL, return -1;);
606 IRDA_DEBUG(1, "%s(), Receiving discovery request (s = %d) while performing discovery :-(\n", __FUNCTION__, info->s);
608 /* Last discovery request ? */
610 irlap_discovery_indication(self, info->discovery);
612 case SLOT_TIMER_EXPIRED:
614 * Wait a little longer if we detect an incoming frame. This
615 * is not mentioned in the spec, but is a good thing to do,
616 * since we want to work even with devices that violate the
617 * timing requirements.
619 if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
620 IRDA_DEBUG(2, "%s(), device is slow to answer, "
621 "waiting some more!\n", __FUNCTION__);
622 irlap_start_slot_timer(self, MSECS_TO_JIFFIES(10));
623 self->add_wait = TRUE;
626 self->add_wait = FALSE;
628 if (self->s < self->S) {
629 irlap_send_discovery_xid_frame(self, self->S,
631 self->discovery_cmd);
633 irlap_start_slot_timer(self, self->slot_timeout);
636 irlap_next_state(self, LAP_QUERY);
638 /* This is the final slot! */
639 irlap_send_discovery_xid_frame(self, self->S, 0xff,
641 self->discovery_cmd);
643 /* Always switch state before calling upper layers */
644 irlap_next_state(self, LAP_NDM);
647 * We are now finished with the discovery procedure,
648 * so now we must return the results
650 irlap_discovery_confirm(self, self->discovery_log);
652 /* IrLMP should now have taken care of the log */
653 self->discovery_log = NULL;
657 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
667 * Function irlap_state_reply (self, event, skb, info)
669 * REPLY, we have received a XID discovery frame from a device and we
670 * are waiting for the right time slot to send a response XID frame
673 static int irlap_state_reply(struct irlap_cb *self, IRLAP_EVENT event,
674 struct sk_buff *skb, struct irlap_info *info)
676 discovery_t *discovery_rsp;
679 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
681 ASSERT(self != NULL, return -1;);
682 ASSERT(self->magic == LAP_MAGIC, return -1;);
685 case QUERY_TIMER_EXPIRED:
686 IRDA_DEBUG(2, "%s(), QUERY_TIMER_EXPIRED <%ld>\n", __FUNCTION__,
688 irlap_next_state(self, LAP_NDM);
690 case RECV_DISCOVERY_XID_CMD:
691 ASSERT(info != NULL, return -1;);
693 if (info->s == 0xff) {
694 del_timer(&self->query_timer);
696 /* info->log.condition = REMOTE; */
698 /* Always switch state before calling upper layers */
699 irlap_next_state(self, LAP_NDM);
701 irlap_discovery_indication(self, info->discovery);
702 } else if ((info->s >= self->slot) && (!self->frame_sent)) {
703 discovery_rsp = irlmp_get_discovery_response();
704 discovery_rsp->daddr = info->daddr;
706 irlap_send_discovery_xid_frame(self, info->S,
710 self->frame_sent = TRUE;
711 irlap_next_state(self, LAP_REPLY);
715 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __FUNCTION__, event,
725 * Function irlap_state_conn (event, skb, info)
727 * CONN, we have received a SNRM command and is waiting for the upper
728 * layer to accept or refuse connection
731 static int irlap_state_conn(struct irlap_cb *self, IRLAP_EVENT event,
732 struct sk_buff *skb, struct irlap_info *info)
736 IRDA_DEBUG(4, "%s(), event=%s\n", __FUNCTION__, irlap_event[ event]);
738 ASSERT(self != NULL, return -1;);
739 ASSERT(self->magic == LAP_MAGIC, return -1;);
742 case CONNECT_RESPONSE:
743 skb_pull(skb, sizeof(struct snrm_frame));
745 ASSERT(self->netdev != NULL, return -1;);
747 irlap_qos_negotiate(self, skb);
749 irlap_initiate_connection_state(self);
752 * Applying the parameters now will make sure we change speed
753 * *after* we have sent the next frame
755 irlap_apply_connection_parameters(self, FALSE);
758 * Sending this frame will force a speed change after it has
759 * been sent (i.e. the frame will be sent at 9600).
761 irlap_send_ua_response_frame(self, &self->qos_rx);
765 * We are allowed to send two frames, but this may increase
766 * the connect latency, so lets not do it for now.
768 /* This is full of good intentions, but doesn't work in
770 * After sending the first UA response, we switch the
771 * dongle to the negociated speed, which is usually
772 * different than 9600 kb/s.
773 * From there, there is two solutions :
774 * 1) The other end has received the first UA response :
775 * it will set up the connection, move to state LAP_NRM_P,
776 * and will ignore and drop the second UA response.
777 * Actually, it's even worse : the other side will almost
778 * immediately send a RR that will likely collide with the
779 * UA response (depending on negociated turnaround).
780 * 2) The other end has not received the first UA response,
781 * will stay at 9600 and will never see the second UA response.
783 irlap_send_ua_response_frame(self, &self->qos_rx);
787 * The WD-timer could be set to the duration of the P-timer
788 * for this case, but it is recommended to use twice the
789 * value (note 3 IrLAP p. 60).
791 irlap_start_wd_timer(self, self->wd_timeout);
792 irlap_next_state(self, LAP_NRM_S);
795 case RECV_DISCOVERY_XID_CMD:
796 IRDA_DEBUG(3, "%s(), event RECV_DISCOVER_XID_CMD!\n", __FUNCTION__);
797 irlap_next_state(self, LAP_NDM);
800 case DISCONNECT_REQUEST:
801 IRDA_DEBUG(0, "%s(), Disconnect request!\n", __FUNCTION__);
802 irlap_send_dm_frame(self);
803 irlap_next_state( self, LAP_NDM);
804 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
807 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __FUNCTION__, event,
818 * Function irlap_state_setup (event, skb, frame)
820 * SETUP state, The local layer has transmitted a SNRM command frame to
821 * a remote peer layer and is awaiting a reply .
824 static int irlap_state_setup(struct irlap_cb *self, IRLAP_EVENT event,
825 struct sk_buff *skb, struct irlap_info *info)
829 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
831 ASSERT(self != NULL, return -1;);
832 ASSERT(self->magic == LAP_MAGIC, return -1;);
835 case FINAL_TIMER_EXPIRED:
836 if (self->retry_count < self->N3) {
838 * Perform random backoff, Wait a random number of time units, minimum
839 * duration half the time taken to transmitt a SNRM frame, maximum duration
840 * 1.5 times the time taken to transmit a SNRM frame. So this time should
841 * between 15 msecs and 45 msecs.
843 irlap_start_backoff_timer(self, MSECS_TO_JIFFIES(20 +
846 /* Always switch state before calling upper layers */
847 irlap_next_state(self, LAP_NDM);
849 irlap_disconnect_indication(self, LAP_FOUND_NONE);
852 case BACKOFF_TIMER_EXPIRED:
853 irlap_send_snrm_frame(self, &self->qos_rx);
854 irlap_start_final_timer(self, self->final_timeout);
858 IRDA_DEBUG(4, "%s(), SNRM battle!\n", __FUNCTION__);
860 ASSERT(skb != NULL, return 0;);
861 ASSERT(info != NULL, return 0;);
864 * The device with the largest device address wins the battle
865 * (both have sent a SNRM command!)
867 if (info &&(info->daddr > self->saddr)) {
868 del_timer(&self->final_timer);
869 irlap_initiate_connection_state(self);
871 ASSERT(self->netdev != NULL, return -1;);
873 skb_pull(skb, sizeof(struct snrm_frame));
875 irlap_qos_negotiate(self, skb);
877 /* Send UA frame and then change link settings */
878 irlap_apply_connection_parameters(self, FALSE);
879 irlap_send_ua_response_frame(self, &self->qos_rx);
881 irlap_next_state(self, LAP_NRM_S);
882 irlap_connect_confirm(self, skb);
885 * The WD-timer could be set to the duration of the
886 * P-timer for this case, but it is recommended
887 * to use twice the value (note 3 IrLAP p. 60).
889 irlap_start_wd_timer(self, self->wd_timeout);
891 /* We just ignore the other device! */
892 irlap_next_state(self, LAP_SETUP);
897 del_timer(&self->final_timer);
899 /* Initiate connection state */
900 irlap_initiate_connection_state(self);
902 /* Negotiate connection parameters */
903 ASSERT(skb->len > 10, return -1;);
905 skb_pull(skb, sizeof(struct ua_frame));
907 ASSERT(self->netdev != NULL, return -1;);
909 irlap_qos_negotiate(self, skb);
911 /* Set the new link setting *now* (before the rr frame) */
912 irlap_apply_connection_parameters(self, TRUE);
913 self->retry_count = 0;
915 /* Wait for turnaround time to give a chance to the other
916 * device to be ready to receive us.
917 * Note : the time to switch speed is typically larger
918 * than the turnaround time, but as we don't have the other
919 * side speed switch time, that's our best guess...
921 irlap_wait_min_turn_around(self, &self->qos_tx);
923 /* This frame will actually be sent at the new speed */
924 irlap_send_rr_frame(self, CMD_FRAME);
926 irlap_start_final_timer(self, self->final_timeout/2);
927 irlap_next_state(self, LAP_NRM_P);
929 irlap_connect_confirm(self, skb);
931 case RECV_DM_RSP: /* FALLTHROUGH */
933 del_timer(&self->final_timer);
934 irlap_next_state(self, LAP_NDM);
936 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
939 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __FUNCTION__, event,
949 * Function irlap_state_offline (self, event, skb, info)
951 * OFFLINE state, not used for now!
954 static int irlap_state_offline(struct irlap_cb *self, IRLAP_EVENT event,
955 struct sk_buff *skb, struct irlap_info *info)
957 IRDA_DEBUG( 0, "%s(), Unknown event\n", __FUNCTION__);
963 * Function irlap_state_xmit_p (self, event, skb, info)
965 * XMIT, Only the primary station has right to transmit, and we
966 * therefore do not expect to receive any transmissions from other
970 static int irlap_state_xmit_p(struct irlap_cb *self, IRLAP_EVENT event,
971 struct sk_buff *skb, struct irlap_info *info)
978 * Only send frame if send-window > 0.
980 if ((self->window > 0) && (!self->remote_busy)) {
981 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
983 * Test if we have transmitted more bytes over the
984 * link than its possible to do with the current
985 * speed and turn-around-time.
987 if (skb->len > self->bytes_left) {
988 IRDA_DEBUG(4, "%s(), Not allowed to transmit more "
989 "bytes!\n", __FUNCTION__);
990 skb_queue_head(&self->txq, skb_get(skb));
992 * We should switch state to LAP_NRM_P, but
993 * that is not possible since we must be sure
994 * that we poll the other side. Since we have
995 * used up our time, the poll timer should
996 * trigger anyway now, so we just wait for it
1001 self->bytes_left -= skb->len;
1002 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1004 * Send data with poll bit cleared only if window > 1
1005 * and there is more frames after this one to be sent
1007 if ((self->window > 1) &&
1008 skb_queue_len( &self->txq) > 0)
1010 irlap_send_data_primary(self, skb);
1011 irlap_next_state(self, LAP_XMIT_P);
1013 irlap_send_data_primary_poll(self, skb);
1014 irlap_next_state(self, LAP_NRM_P);
1017 * Make sure state machine does not try to send
1022 #ifdef CONFIG_IRDA_FAST_RR
1023 /* Peer may want to reply immediately */
1024 self->fast_RR = FALSE;
1025 #endif /* CONFIG_IRDA_FAST_RR */
1027 IRDA_DEBUG(4, "%s(), Unable to send! remote busy?\n", __FUNCTION__);
1028 skb_queue_head(&self->txq, skb_get(skb));
1031 * The next ret is important, because it tells
1032 * irlap_next_state _not_ to deliver more frames
1037 case POLL_TIMER_EXPIRED:
1038 IRDA_DEBUG(3, "%s(), POLL_TIMER_EXPIRED (%ld)\n", __FUNCTION__,
1040 irlap_send_rr_frame(self, CMD_FRAME);
1041 /* Return to NRM properly - Jean II */
1042 self->window = self->window_size;
1043 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1044 /* Allowed to transmit a maximum number of bytes again. */
1045 self->bytes_left = self->line_capacity;
1046 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1047 irlap_start_final_timer(self, self->final_timeout);
1048 irlap_next_state(self, LAP_NRM_P);
1050 case DISCONNECT_REQUEST:
1051 del_timer(&self->poll_timer);
1052 irlap_wait_min_turn_around(self, &self->qos_tx);
1053 irlap_send_disc_frame(self);
1054 irlap_flush_all_queues(self);
1055 irlap_start_final_timer(self, self->final_timeout);
1056 self->retry_count = 0;
1057 irlap_next_state(self, LAP_PCLOSE);
1060 /* Nothing to do, irlap_do_event() will send the packet
1061 * when we return... - Jean II */
1064 IRDA_DEBUG(0, "%s(), Unknown event %s\n", __FUNCTION__,
1065 irlap_event[event]);
1074 * Function irlap_state_pclose (event, skb, info)
1078 static int irlap_state_pclose(struct irlap_cb *self, IRLAP_EVENT event,
1079 struct sk_buff *skb, struct irlap_info *info)
1083 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1085 ASSERT(self != NULL, return -1;);
1086 ASSERT(self->magic == LAP_MAGIC, return -1;);
1089 case RECV_UA_RSP: /* FALLTHROUGH */
1091 del_timer(&self->final_timer);
1093 /* Set new link parameters */
1094 irlap_apply_default_connection_parameters(self);
1096 /* Always switch state before calling upper layers */
1097 irlap_next_state(self, LAP_NDM);
1099 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
1101 case FINAL_TIMER_EXPIRED:
1102 if (self->retry_count < self->N3) {
1103 irlap_wait_min_turn_around(self, &self->qos_tx);
1104 irlap_send_disc_frame(self);
1105 irlap_start_final_timer(self, self->final_timeout);
1106 self->retry_count++;
1109 irlap_apply_default_connection_parameters(self);
1111 /* Always switch state before calling upper layers */
1112 irlap_next_state(self, LAP_NDM);
1114 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1118 IRDA_DEBUG(1, "%s(), Unknown event %d\n", __FUNCTION__, event);
1127 * Function irlap_state_nrm_p (self, event, skb, info)
1129 * NRM_P (Normal Response Mode as Primary), The primary station has given
1130 * permissions to a secondary station to transmit IrLAP resonse frames
1131 * (by sending a frame with the P bit set). The primary station will not
1132 * transmit any frames and is expecting to receive frames only from the
1133 * secondary to which transmission permissions has been given.
1135 static int irlap_state_nrm_p(struct irlap_cb *self, IRLAP_EVENT event,
1136 struct sk_buff *skb, struct irlap_info *info)
1143 case RECV_I_RSP: /* Optimize for the common case */
1144 /* FIXME: must check for remote_busy below */
1145 #ifdef CONFIG_IRDA_FAST_RR
1147 * Reset the fast_RR so we can use the fast RR code with
1148 * full speed the next time since peer may have more frames
1151 self->fast_RR = FALSE;
1152 #endif /* CONFIG_IRDA_FAST_RR */
1153 ASSERT( info != NULL, return -1;);
1155 ns_status = irlap_validate_ns_received(self, info->ns);
1156 nr_status = irlap_validate_nr_received(self, info->nr);
1159 * Check for expected I(nformation) frame
1161 if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
1162 /* poll bit cleared? */
1164 self->vr = (self->vr + 1) % 8;
1166 /* Update Nr received */
1167 irlap_update_nr_received( self, info->nr);
1169 self->ack_required = TRUE;
1171 /* Keep state, do not move this line */
1172 irlap_next_state(self, LAP_NRM_P);
1174 irlap_data_indication(self, skb, FALSE);
1176 del_timer(&self->final_timer);
1178 self->vr = (self->vr + 1) % 8;
1180 /* Update Nr received */
1181 irlap_update_nr_received(self, info->nr);
1184 * Got expected NR, so reset the
1185 * retry_count. This is not done by IrLAP,
1188 self->retry_count = 0;
1189 self->ack_required = TRUE;
1191 irlap_wait_min_turn_around(self, &self->qos_tx);
1193 /* Call higher layer *before* changing state
1194 * to give them a chance to send data in the
1197 irlap_data_indication(self, skb, FALSE);
1199 /* XMIT states are the most dangerous state
1200 * to be in, because user requests are
1201 * processed directly and may change state.
1202 * On the other hand, in NDM_P, those
1203 * requests are queued and we will process
1204 * them when we return to irlap_do_event().
1207 irlap_next_state(self, LAP_XMIT_P);
1209 /* This is the last frame.
1210 * Make sure it's always called in XMIT state.
1212 irlap_start_poll_timer(self, self->poll_timeout);
1217 /* Unexpected next to send (Ns) */
1218 if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
1221 irlap_update_nr_received(self, info->nr);
1224 * Wait until the last frame before doing
1229 irlap_next_state(self, LAP_NRM_P);
1231 IRDA_DEBUG(4, "%s(), missing or duplicate frame!\n", __FUNCTION__);
1233 /* Update Nr received */
1234 irlap_update_nr_received(self, info->nr);
1236 irlap_wait_min_turn_around(self, &self->qos_tx);
1237 irlap_send_rr_frame(self, CMD_FRAME);
1239 self->ack_required = FALSE;
1241 irlap_start_final_timer(self, self->final_timeout);
1242 irlap_next_state(self, LAP_NRM_P);
1247 * Unexpected next to receive (Nr)
1249 if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
1252 self->vr = (self->vr + 1) % 8;
1254 /* Update Nr received */
1255 irlap_update_nr_received(self, info->nr);
1257 /* Resend rejected frames */
1258 irlap_resend_rejected_frames(self, CMD_FRAME);
1260 self->ack_required = FALSE;
1261 irlap_start_final_timer(self, self->final_timeout);
1263 /* Keep state, do not move this line */
1264 irlap_next_state(self, LAP_NRM_P);
1266 irlap_data_indication(self, skb, FALSE);
1269 * Do not resend frames until the last
1270 * frame has arrived from the other
1271 * device. This is not documented in
1274 self->vr = (self->vr + 1) % 8;
1276 /* Update Nr received */
1277 irlap_update_nr_received(self, info->nr);
1279 self->ack_required = FALSE;
1281 /* Keep state, do not move this line!*/
1282 irlap_next_state(self, LAP_NRM_P);
1284 irlap_data_indication(self, skb, FALSE);
1289 * Unexpected next to send (Ns) and next to receive (Nr)
1290 * Not documented by IrLAP!
1292 if ((ns_status == NS_UNEXPECTED) &&
1293 (nr_status == NR_UNEXPECTED))
1295 IRDA_DEBUG(4, "%s(), unexpected nr and ns!\n", __FUNCTION__);
1297 /* Resend rejected frames */
1298 irlap_resend_rejected_frames(self, CMD_FRAME);
1300 /* Give peer some time to retransmit! */
1301 irlap_start_final_timer(self, self->final_timeout);
1303 /* Keep state, do not move this line */
1304 irlap_next_state(self, LAP_NRM_P);
1306 /* Update Nr received */
1307 /* irlap_update_nr_received( info->nr); */
1309 self->ack_required = FALSE;
1317 if ((nr_status == NR_INVALID) || (ns_status == NS_INVALID)) {
1319 del_timer(&self->final_timer);
1321 irlap_next_state(self, LAP_RESET_WAIT);
1323 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1324 self->xmitflag = TRUE;
1326 del_timer(&self->final_timer);
1328 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1330 self->xmitflag = FALSE;
1334 IRDA_DEBUG(1, "%s(), Not implemented!\n", __FUNCTION__);
1335 IRDA_DEBUG(1, "%s(), event=%s, ns_status=%d, nr_status=%d\n", __FUNCTION__,
1336 irlap_event[ event], ns_status, nr_status);
1339 /* Poll bit cleared? */
1341 irlap_data_indication(self, skb, TRUE);
1342 irlap_next_state(self, LAP_NRM_P);
1344 del_timer(&self->final_timer);
1345 irlap_data_indication(self, skb, TRUE);
1346 irlap_next_state(self, LAP_XMIT_P);
1347 printk("%s(): RECV_UI_FRAME: next state %s\n", __FUNCTION__, irlap_state[self->state]);
1348 irlap_start_poll_timer(self, self->poll_timeout);
1353 * If you get a RR, the remote isn't busy anymore,
1354 * no matter what the NR
1356 self->remote_busy = FALSE;
1361 ret = irlap_validate_nr_received(self, info->nr);
1362 if (ret == NR_EXPECTED) {
1363 /* Stop final timer */
1364 del_timer(&self->final_timer);
1366 /* Update Nr received */
1367 irlap_update_nr_received(self, info->nr);
1370 * Got expected NR, so reset the retry_count. This
1371 * is not done by the IrLAP standard , which is
1374 self->retry_count = 0;
1375 irlap_wait_min_turn_around(self, &self->qos_tx);
1377 irlap_next_state(self, LAP_XMIT_P);
1379 /* Start poll timer */
1380 irlap_start_poll_timer(self, self->poll_timeout);
1381 } else if (ret == NR_UNEXPECTED) {
1382 ASSERT(info != NULL, return -1;);
1387 /* Update Nr received */
1388 irlap_update_nr_received(self, info->nr);
1390 IRDA_DEBUG(4, "RECV_RR_FRAME: Retrans:%d, nr=%d, va=%d, "
1392 self->retry_count, info->nr, self->va,
1393 self->vs, self->vr);
1395 /* Resend rejected frames */
1396 irlap_resend_rejected_frames(self, CMD_FRAME);
1398 irlap_next_state(self, LAP_NRM_P);
1399 } else if (ret == NR_INVALID) {
1400 IRDA_DEBUG(1, "%s(), Received RR with "
1401 "invalid nr !\n", __FUNCTION__);
1402 del_timer(&self->final_timer);
1404 irlap_next_state(self, LAP_RESET_WAIT);
1406 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1407 self->xmitflag = TRUE;
1411 ASSERT(info != NULL, return -1;);
1413 /* Stop final timer */
1414 del_timer(&self->final_timer);
1415 self->remote_busy = TRUE;
1417 /* Update Nr received */
1418 irlap_update_nr_received(self, info->nr);
1419 irlap_next_state(self, LAP_XMIT_P);
1421 /* Start poll timer */
1422 irlap_start_poll_timer(self, self->poll_timeout);
1425 del_timer(&self->final_timer);
1426 self->xmitflag = TRUE;
1427 irlap_next_state(self, LAP_RESET_WAIT);
1428 irlap_reset_indication(self);
1430 case FINAL_TIMER_EXPIRED:
1432 * We are allowed to wait for additional 300 ms if
1433 * final timer expires when we are in the middle
1434 * of receiving a frame (page 45, IrLAP). Check that
1435 * we only do this once for each frame.
1437 if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
1438 IRDA_DEBUG(1, "FINAL_TIMER_EXPIRED when receiving a "
1439 "frame! Waiting a little bit more!\n");
1440 irlap_start_final_timer(self, MSECS_TO_JIFFIES(300));
1443 * Don't allow this to happen one more time in a row,
1444 * or else we can get a pretty tight loop here if
1445 * if we only receive half a frame. DB.
1447 self->add_wait = TRUE;
1450 self->add_wait = FALSE;
1452 /* N2 is the disconnect timer. Until we reach it, we retry */
1453 if (self->retry_count < self->N2) {
1454 /* Retry sending the pf bit to the secondary */
1455 irlap_wait_min_turn_around(self, &self->qos_tx);
1456 irlap_send_rr_frame(self, CMD_FRAME);
1458 irlap_start_final_timer(self, self->final_timeout);
1459 self->retry_count++;
1460 IRDA_DEBUG(4, "irlap_state_nrm_p: FINAL_TIMER_EXPIRED:"
1461 " retry_count=%d\n", self->retry_count);
1463 /* Early warning event. I'm using a pretty liberal
1464 * interpretation of the spec and generate an event
1465 * every time the timer is multiple of N1 (and not
1466 * only the first time). This allow application
1467 * to know precisely if connectivity restart...
1469 if((self->retry_count % self->N1) == 0)
1470 irlap_status_indication(self,
1471 STATUS_NO_ACTIVITY);
1475 irlap_apply_default_connection_parameters(self);
1477 /* Always switch state before calling upper layers */
1478 irlap_next_state(self, LAP_NDM);
1479 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1483 irlap_update_nr_received(self, info->nr);
1484 if (self->remote_busy) {
1485 irlap_wait_min_turn_around(self, &self->qos_tx);
1486 irlap_send_rr_frame(self, CMD_FRAME);
1488 irlap_resend_rejected_frames(self, CMD_FRAME);
1489 irlap_start_final_timer(self, self->final_timeout);
1492 irlap_update_nr_received(self, info->nr);
1493 if (self->remote_busy) {
1494 irlap_wait_min_turn_around(self, &self->qos_tx);
1495 irlap_send_rr_frame(self, CMD_FRAME);
1497 irlap_resend_rejected_frame(self, CMD_FRAME);
1498 irlap_start_final_timer(self, self->final_timeout);
1501 IRDA_DEBUG(1, "%s(), RECV_RD_RSP\n", __FUNCTION__);
1503 irlap_flush_all_queues(self);
1504 irlap_next_state(self, LAP_XMIT_P);
1505 /* Call back the LAP state machine to do a proper disconnect */
1506 irlap_disconnect_request(self);
1509 IRDA_DEBUG(1, "%s(), Unknown event %s\n", __FUNCTION__,
1510 irlap_event[event]);
1519 * Function irlap_state_reset_wait (event, skb, info)
1521 * We have informed the service user of a reset condition, and is
1522 * awaiting reset of disconnect request.
1525 static int irlap_state_reset_wait(struct irlap_cb *self, IRLAP_EVENT event,
1526 struct sk_buff *skb, struct irlap_info *info)
1530 IRDA_DEBUG(3, "%s(), event = %s\n", __FUNCTION__, irlap_event[event]);
1532 ASSERT(self != NULL, return -1;);
1533 ASSERT(self->magic == LAP_MAGIC, return -1;);
1537 if (self->xmitflag) {
1538 irlap_wait_min_turn_around(self, &self->qos_tx);
1539 irlap_send_snrm_frame(self, NULL);
1540 irlap_start_final_timer(self, self->final_timeout);
1541 irlap_next_state(self, LAP_RESET);
1543 irlap_start_final_timer(self, self->final_timeout);
1544 irlap_next_state(self, LAP_RESET);
1547 case DISCONNECT_REQUEST:
1548 irlap_wait_min_turn_around( self, &self->qos_tx);
1549 irlap_send_disc_frame( self);
1550 irlap_flush_all_queues( self);
1551 irlap_start_final_timer( self, self->final_timeout);
1552 self->retry_count = 0;
1553 irlap_next_state( self, LAP_PCLOSE);
1556 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
1557 irlap_event[event]);
1566 * Function irlap_state_reset (self, event, skb, info)
1568 * We have sent a SNRM reset command to the peer layer, and is awaiting
1572 static int irlap_state_reset(struct irlap_cb *self, IRLAP_EVENT event,
1573 struct sk_buff *skb, struct irlap_info *info)
1577 IRDA_DEBUG(3, "%s(), event = %s\n", __FUNCTION__, irlap_event[event]);
1579 ASSERT(self != NULL, return -1;);
1580 ASSERT(self->magic == LAP_MAGIC, return -1;);
1584 del_timer(&self->final_timer);
1586 irlap_apply_default_connection_parameters(self);
1588 /* Always switch state before calling upper layers */
1589 irlap_next_state(self, LAP_NDM);
1591 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1595 del_timer(&self->final_timer);
1597 /* Initiate connection state */
1598 irlap_initiate_connection_state(self);
1600 irlap_reset_confirm();
1602 self->remote_busy = FALSE;
1604 irlap_next_state(self, LAP_XMIT_P);
1606 irlap_start_poll_timer(self, self->poll_timeout);
1609 case FINAL_TIMER_EXPIRED:
1610 if (self->retry_count < 3) {
1611 irlap_wait_min_turn_around(self, &self->qos_tx);
1613 ASSERT(self->netdev != NULL, return -1;);
1614 irlap_send_snrm_frame(self, self->qos_dev);
1616 self->retry_count++; /* Experimental!! */
1618 irlap_start_final_timer(self, self->final_timeout);
1619 irlap_next_state(self, LAP_RESET);
1620 } else if (self->retry_count >= self->N3) {
1621 irlap_apply_default_connection_parameters(self);
1623 /* Always switch state before calling upper layers */
1624 irlap_next_state(self, LAP_NDM);
1626 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1631 * SNRM frame is not allowed to contain an I-field in this
1635 IRDA_DEBUG(3, "%s(), RECV_SNRM_CMD\n", __FUNCTION__);
1636 irlap_initiate_connection_state(self);
1637 irlap_wait_min_turn_around(self, &self->qos_tx);
1638 irlap_send_ua_response_frame(self, &self->qos_rx);
1639 irlap_reset_confirm();
1640 irlap_start_wd_timer(self, self->wd_timeout);
1641 irlap_next_state(self, LAP_NDM);
1643 IRDA_DEBUG(0, "%s(), SNRM frame contained an I field!\n", __FUNCTION__);
1647 IRDA_DEBUG(1, "%s(), Unknown event %s\n", __FUNCTION__,
1648 irlap_event[event]);
1657 * Function irlap_state_xmit_s (event, skb, info)
1659 * XMIT_S, The secondary station has been given the right to transmit,
1660 * and we therefor do not expect to receive any transmissions from other
1663 static int irlap_state_xmit_s(struct irlap_cb *self, IRLAP_EVENT event,
1664 struct sk_buff *skb, struct irlap_info *info)
1668 IRDA_DEBUG(4, "%s(), event=%s\n", __FUNCTION__, irlap_event[event]);
1670 ASSERT(self != NULL, return -ENODEV;);
1671 ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
1676 * Send frame only if send window > 1
1678 if ((self->window > 0) && (!self->remote_busy)) {
1679 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1681 * Test if we have transmitted more bytes over the
1682 * link than its possible to do with the current
1683 * speed and turn-around-time.
1685 if (skb->len > self->bytes_left) {
1686 skb_queue_head(&self->txq, skb_get(skb));
1689 * Switch to NRM_S, this is only possible
1690 * when we are in secondary mode, since we
1691 * must be sure that we don't miss any RR
1694 self->window = self->window_size;
1695 self->bytes_left = self->line_capacity;
1696 irlap_start_wd_timer(self, self->wd_timeout);
1698 irlap_next_state(self, LAP_NRM_S);
1700 return -EPROTO; /* Try again later */
1702 self->bytes_left -= skb->len;
1703 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1705 * Send data with final bit cleared only if window > 1
1706 * and there is more frames to be sent
1708 if ((self->window > 1) &&
1709 skb_queue_len(&self->txq) > 0)
1711 irlap_send_data_secondary(self, skb);
1712 irlap_next_state(self, LAP_XMIT_S);
1714 irlap_send_data_secondary_final(self, skb);
1715 irlap_next_state(self, LAP_NRM_S);
1718 * Make sure state machine does not try to send
1724 IRDA_DEBUG(2, "%s(), Unable to send!\n", __FUNCTION__);
1725 skb_queue_head(&self->txq, skb_get(skb));
1729 case DISCONNECT_REQUEST:
1730 irlap_send_rd_frame(self);
1731 irlap_flush_all_queues(self);
1732 irlap_start_wd_timer(self, self->wd_timeout);
1733 irlap_next_state(self, LAP_SCLOSE);
1736 /* Nothing to do, irlap_do_event() will send the packet
1737 * when we return... - Jean II */
1740 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
1741 irlap_event[event]);
1750 * Function irlap_state_nrm_s (event, skb, info)
1752 * NRM_S (Normal Response Mode as Secondary) state, in this state we are
1753 * expecting to receive frames from the primary station
1756 static int irlap_state_nrm_s(struct irlap_cb *self, IRLAP_EVENT event,
1757 struct sk_buff *skb, struct irlap_info *info)
1763 IRDA_DEBUG(4, "%s(), event=%s\n", __FUNCTION__, irlap_event[ event]);
1765 ASSERT(self != NULL, return -1;);
1766 ASSERT(self->magic == LAP_MAGIC, return -1;);
1769 case RECV_I_CMD: /* Optimize for the common case */
1770 /* FIXME: must check for remote_busy below */
1771 IRDA_DEBUG(4, "%s(), event=%s nr=%d, vs=%d, ns=%d, "
1772 "vr=%d, pf=%d\n", __FUNCTION__, irlap_event[event], info->nr,
1773 self->vs, info->ns, self->vr, info->pf);
1775 self->retry_count = 0;
1777 ns_status = irlap_validate_ns_received(self, info->ns);
1778 nr_status = irlap_validate_nr_received(self, info->nr);
1780 * Check for expected I(nformation) frame
1782 if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
1787 self->vr = (self->vr + 1) % 8;
1789 /* Update Nr received */
1790 irlap_update_nr_received(self, info->nr);
1792 self->ack_required = TRUE;
1795 * Starting WD-timer here is optional, but
1796 * not recommended. Note 6 IrLAP p. 83
1799 irda_start_timer(WD_TIMER, self->wd_timeout);
1801 /* Keep state, do not move this line */
1802 irlap_next_state(self, LAP_NRM_S);
1804 irlap_data_indication(self, skb, FALSE);
1807 self->vr = (self->vr + 1) % 8;
1809 /* Update Nr received */
1810 irlap_update_nr_received(self, info->nr);
1813 * We should wait before sending RR, and
1814 * also before changing to XMIT_S
1815 * state. (note 1, IrLAP p. 82)
1817 irlap_wait_min_turn_around(self, &self->qos_tx);
1820 * Give higher layers a chance to
1821 * immediately reply with some data before
1822 * we decide if we should send a RR frame
1825 irlap_data_indication(self, skb, FALSE);
1827 /* Any pending data requests? */
1828 if ((skb_queue_len(&self->txq) > 0) &&
1831 self->ack_required = TRUE;
1833 del_timer(&self->wd_timer);
1835 irlap_next_state(self, LAP_XMIT_S);
1837 irlap_send_rr_frame(self, RSP_FRAME);
1838 irlap_start_wd_timer(self,
1841 /* Keep the state */
1842 irlap_next_state(self, LAP_NRM_S);
1848 * Check for Unexpected next to send (Ns)
1850 if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
1852 /* Unexpected next to send, with final bit cleared */
1854 irlap_update_nr_received(self, info->nr);
1856 irlap_start_wd_timer(self, self->wd_timeout);
1858 /* Update Nr received */
1859 irlap_update_nr_received(self, info->nr);
1861 irlap_wait_min_turn_around(self, &self->qos_tx);
1862 irlap_send_rr_frame(self, RSP_FRAME);
1864 irlap_start_wd_timer(self, self->wd_timeout);
1870 * Unexpected Next to Receive(NR) ?
1872 if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
1875 IRDA_DEBUG(4, "RECV_I_RSP: frame(s) lost\n");
1877 self->vr = (self->vr + 1) % 8;
1879 /* Update Nr received */
1880 irlap_update_nr_received(self, info->nr);
1882 /* Resend rejected frames */
1883 irlap_resend_rejected_frames(self, RSP_FRAME);
1885 /* Keep state, do not move this line */
1886 irlap_next_state(self, LAP_NRM_S);
1888 irlap_data_indication(self, skb, FALSE);
1889 irlap_start_wd_timer(self, self->wd_timeout);
1893 * This is not documented in IrLAP!! Unexpected NR
1894 * with poll bit cleared
1897 self->vr = (self->vr + 1) % 8;
1899 /* Update Nr received */
1900 irlap_update_nr_received(self, info->nr);
1902 /* Keep state, do not move this line */
1903 irlap_next_state(self, LAP_NRM_S);
1905 irlap_data_indication(self, skb, FALSE);
1906 irlap_start_wd_timer(self, self->wd_timeout);
1911 if (ret == NR_INVALID) {
1912 IRDA_DEBUG(0, "NRM_S, NR_INVALID not implemented!\n");
1914 if (ret == NS_INVALID) {
1915 IRDA_DEBUG(0, "NRM_S, NS_INVALID not implemented!\n");
1923 irlap_data_indication(self, skb, TRUE);
1924 irlap_next_state(self, LAP_NRM_S); /* Keep state */
1927 * Any pending data requests?
1929 if ((skb_queue_len(&self->txq) > 0) &&
1930 (self->window > 0) && !self->remote_busy)
1932 irlap_data_indication(self, skb, TRUE);
1934 del_timer(&self->wd_timer);
1936 irlap_next_state(self, LAP_XMIT_S);
1938 irlap_data_indication(self, skb, TRUE);
1940 irlap_wait_min_turn_around(self, &self->qos_tx);
1942 irlap_send_rr_frame(self, RSP_FRAME);
1943 self->ack_required = FALSE;
1945 irlap_start_wd_timer(self, self->wd_timeout);
1947 /* Keep the state */
1948 irlap_next_state(self, LAP_NRM_S);
1953 self->retry_count = 0;
1958 nr_status = irlap_validate_nr_received(self, info->nr);
1959 if (nr_status == NR_EXPECTED) {
1960 if ((skb_queue_len( &self->txq) > 0) &&
1961 (self->window > 0)) {
1962 self->remote_busy = FALSE;
1964 /* Update Nr received */
1965 irlap_update_nr_received(self, info->nr);
1966 del_timer(&self->wd_timer);
1968 irlap_wait_min_turn_around(self, &self->qos_tx);
1969 irlap_next_state(self, LAP_XMIT_S);
1971 self->remote_busy = FALSE;
1972 /* Update Nr received */
1973 irlap_update_nr_received(self, info->nr);
1974 irlap_wait_min_turn_around(self, &self->qos_tx);
1975 irlap_start_wd_timer(self, self->wd_timeout);
1977 /* Note : if the link is idle (this case),
1978 * we never go in XMIT_S, so we never get a
1979 * chance to process any DISCONNECT_REQUEST.
1980 * Do it now ! - Jean II */
1981 if (self->disconnect_pending) {
1983 irlap_send_rd_frame(self);
1984 irlap_flush_all_queues(self);
1986 irlap_next_state(self, LAP_SCLOSE);
1988 /* Just send back pf bit */
1989 irlap_send_rr_frame(self, RSP_FRAME);
1991 irlap_next_state(self, LAP_NRM_S);
1994 } else if (nr_status == NR_UNEXPECTED) {
1995 self->remote_busy = FALSE;
1996 irlap_update_nr_received(self, info->nr);
1997 irlap_resend_rejected_frames(self, RSP_FRAME);
1999 irlap_start_wd_timer(self, self->wd_timeout);
2002 irlap_next_state(self, LAP_NRM_S);
2004 IRDA_DEBUG(1, "%s(), invalid nr not implemented!\n", __FUNCTION__);
2008 /* SNRM frame is not allowed to contain an I-field */
2010 del_timer(&self->wd_timer);
2011 IRDA_DEBUG(1, "%s(), received SNRM cmd\n", __FUNCTION__);
2012 irlap_next_state(self, LAP_RESET_CHECK);
2014 irlap_reset_indication(self);
2016 IRDA_DEBUG(0, "%s(), SNRM frame contained an I-field!\n", __FUNCTION__);
2021 irlap_update_nr_received(self, info->nr);
2022 if (self->remote_busy) {
2023 irlap_wait_min_turn_around(self, &self->qos_tx);
2024 irlap_send_rr_frame(self, RSP_FRAME);
2026 irlap_resend_rejected_frames(self, RSP_FRAME);
2027 irlap_start_wd_timer(self, self->wd_timeout);
2030 irlap_update_nr_received(self, info->nr);
2031 if (self->remote_busy) {
2032 irlap_wait_min_turn_around(self, &self->qos_tx);
2033 irlap_send_rr_frame(self, RSP_FRAME);
2035 irlap_resend_rejected_frame(self, RSP_FRAME);
2036 irlap_start_wd_timer(self, self->wd_timeout);
2038 case WD_TIMER_EXPIRED:
2040 * Wait until retry_count * n matches negotiated threshold/
2041 * disconnect time (note 2 in IrLAP p. 82)
2043 * Similar to irlap_state_nrm_p() -> FINAL_TIMER_EXPIRED
2044 * Note : self->wd_timeout = (self->final_timeout * 2),
2045 * which explain why we use (self->N2 / 2) here !!!
2048 IRDA_DEBUG(1, "%s(), retry_count = %d\n", __FUNCTION__,
2051 if (self->retry_count < (self->N2 / 2)) {
2052 /* No retry, just wait for primary */
2053 irlap_start_wd_timer(self, self->wd_timeout);
2054 self->retry_count++;
2056 if((self->retry_count % (self->N1 / 2)) == 0)
2057 irlap_status_indication(self,
2058 STATUS_NO_ACTIVITY);
2060 irlap_apply_default_connection_parameters(self);
2062 /* Always switch state before calling upper layers */
2063 irlap_next_state(self, LAP_NDM);
2064 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
2068 /* Always switch state before calling upper layers */
2069 irlap_next_state(self, LAP_NDM);
2071 /* Send disconnect response */
2072 irlap_wait_min_turn_around(self, &self->qos_tx);
2073 irlap_send_ua_response_frame(self, NULL);
2075 del_timer(&self->wd_timer);
2076 irlap_flush_all_queues(self);
2077 /* Set default link parameters */
2078 irlap_apply_default_connection_parameters(self);
2080 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2082 case RECV_DISCOVERY_XID_CMD:
2083 irlap_wait_min_turn_around(self, &self->qos_tx);
2084 irlap_send_rr_frame(self, RSP_FRAME);
2085 self->ack_required = TRUE;
2086 irlap_start_wd_timer(self, self->wd_timeout);
2087 irlap_next_state(self, LAP_NRM_S);
2091 /* Remove test frame header (only LAP header in NRM) */
2092 skb_pull(skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
2094 irlap_wait_min_turn_around(self, &self->qos_tx);
2095 irlap_start_wd_timer(self, self->wd_timeout);
2097 /* Send response (info will be copied) */
2098 irlap_send_test_frame(self, self->caddr, info->daddr, skb);
2101 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __FUNCTION__,
2102 event, irlap_event[event]);
2111 * Function irlap_state_sclose (self, event, skb, info)
2116 static int irlap_state_sclose(struct irlap_cb *self, IRLAP_EVENT event,
2117 struct sk_buff *skb, struct irlap_info *info)
2121 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
2123 ASSERT(self != NULL, return -ENODEV;);
2124 ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
2128 /* Always switch state before calling upper layers */
2129 irlap_next_state(self, LAP_NDM);
2131 /* Send disconnect response */
2132 irlap_wait_min_turn_around(self, &self->qos_tx);
2133 irlap_send_ua_response_frame(self, NULL);
2135 del_timer(&self->wd_timer);
2136 /* Set default link parameters */
2137 irlap_apply_default_connection_parameters(self);
2139 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2142 /* Always switch state before calling upper layers */
2143 irlap_next_state(self, LAP_NDM);
2145 del_timer(&self->wd_timer);
2146 irlap_apply_default_connection_parameters(self);
2148 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2150 case WD_TIMER_EXPIRED:
2151 /* Always switch state before calling upper layers */
2152 irlap_next_state(self, LAP_NDM);
2154 irlap_apply_default_connection_parameters(self);
2156 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2159 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __FUNCTION__,
2160 event, irlap_event[event]);
2169 static int irlap_state_reset_check( struct irlap_cb *self, IRLAP_EVENT event,
2170 struct sk_buff *skb,
2171 struct irlap_info *info)
2175 IRDA_DEBUG(1, "%s(), event=%s\n", __FUNCTION__, irlap_event[event]);
2177 ASSERT(self != NULL, return -ENODEV;);
2178 ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
2181 case RESET_RESPONSE:
2182 irlap_send_ua_response_frame(self, &self->qos_rx);
2183 irlap_initiate_connection_state(self);
2184 irlap_start_wd_timer(self, WD_TIMEOUT);
2185 irlap_flush_all_queues(self);
2187 irlap_next_state(self, LAP_NRM_S);
2189 case DISCONNECT_REQUEST:
2190 irlap_wait_min_turn_around(self, &self->qos_tx);
2191 irlap_send_rd_frame(self);
2192 irlap_start_wd_timer(self, WD_TIMEOUT);
2193 irlap_next_state(self, LAP_SCLOSE);
2196 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __FUNCTION__,
2197 event, irlap_event[event]);