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/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>
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 *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 *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_len(&self->txq) == 0) || (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) {
202 * FIXME: this should be a more configurable
205 self->fast_RR_timeout +=
206 (sysctl_fast_poll_increase * HZ/1000);
208 /* Use this fast(er) timeout instead */
209 timeout = self->fast_RR_timeout;
212 self->fast_RR = TRUE;
214 /* Start with just 0 ms */
215 self->fast_RR_timeout = 0;
219 self->fast_RR = FALSE;
221 IRDA_DEBUG(3, "%s(), timeout=%d (%ld)\n", __FUNCTION__, timeout, jiffies);
222 #endif /* CONFIG_IRDA_FAST_RR */
225 irlap_do_event(self, POLL_TIMER_EXPIRED, NULL, NULL);
227 irda_start_timer(&self->poll_timer, timeout, self,
228 irlap_poll_timer_expired);
232 * Function irlap_do_event (event, skb, info)
234 * Rushes through the state machine without any delay. If state == XMIT
235 * then send queued data frames.
237 void irlap_do_event(struct irlap_cb *self, IRLAP_EVENT event,
238 struct sk_buff *skb, struct irlap_info *info)
242 if (!self || self->magic != LAP_MAGIC)
245 IRDA_DEBUG(3, "%s(), event = %s, state = %s\n", __FUNCTION__,
246 irlap_event[event], irlap_state[self->state]);
248 ret = (*state[self->state])(self, event, skb, info);
251 * Check if there are any pending events that needs to be executed
253 switch (self->state) {
254 case LAP_XMIT_P: /* FALLTHROUGH */
257 * We just received the pf bit and are at the beginning
258 * of a new LAP transmit window.
259 * Check if there are any queued data frames, and do not
260 * try to disconnect link if we send any data frames, since
261 * that will change the state away form XMIT
263 IRDA_DEBUG(2, "%s() : queue len = %d\n", __FUNCTION__,
264 skb_queue_len(&self->txq));
266 if (skb_queue_len(&self->txq)) {
267 /* Prevent race conditions with irlap_data_request() */
268 self->local_busy = TRUE;
270 /* Theory of operation.
271 * We send frames up to when we fill the window or
272 * reach line capacity. Those frames will queue up
273 * in the device queue, and the driver will slowly
275 * After each frame that we send, we poll the higher
276 * layer for more data. It's the right time to do
277 * that because the link layer need to perform the mtt
278 * and then send the first frame, so we can afford
279 * to send a bit of time in kernel space.
280 * The explicit flow indication allow to minimise
281 * buffers (== lower latency), to avoid higher layer
282 * polling via timers (== less context switches) and
283 * to implement a crude scheduler - Jean II */
285 /* Try to send away all queued data frames */
286 while ((skb = skb_dequeue(&self->txq)) != NULL) {
288 ret = (*state[self->state])(self, SEND_I_CMD,
290 /* Drop reference count.
291 * It will be increase as needed in
292 * irlap_send_data_xxx() */
295 /* Poll the higher layers for one more frame */
296 irlmp_flow_indication(self->notify.instance,
300 break; /* Try again later! */
302 /* Finished transmitting */
303 self->local_busy = FALSE;
304 } else if (self->disconnect_pending) {
305 self->disconnect_pending = FALSE;
307 ret = (*state[self->state])(self, DISCONNECT_REQUEST,
313 /* case LAP_RESET_WAIT: */
314 /* case LAP_RESET_CHECK: */
321 * Function irlap_next_state (self, state)
323 * Switches state and provides debug information
326 static inline void irlap_next_state(struct irlap_cb *self, IRLAP_STATE state)
329 if (!self || self->magic != LAP_MAGIC)
332 IRDA_DEBUG(4, "next LAP state = %s\n", irlap_state[state]);
338 * Function irlap_state_ndm (event, skb, frame)
340 * NDM (Normal Disconnected Mode) state
343 static int irlap_state_ndm(struct irlap_cb *self, IRLAP_EVENT event,
344 struct sk_buff *skb, struct irlap_info *info)
346 discovery_t *discovery_rsp;
349 IRDA_ASSERT(self != NULL, return -1;);
350 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
353 case CONNECT_REQUEST:
354 IRDA_ASSERT(self->netdev != NULL, return -1;);
356 if (self->media_busy) {
357 /* Note : this will never happen, because we test
358 * media busy in irlap_connect_request() and
359 * postpone the event... - Jean II */
360 IRDA_DEBUG(0, "%s(), CONNECT_REQUEST: media busy!\n",
363 /* Always switch state before calling upper layers */
364 irlap_next_state(self, LAP_NDM);
366 irlap_disconnect_indication(self, LAP_MEDIA_BUSY);
368 irlap_send_snrm_frame(self, &self->qos_rx);
370 /* Start Final-bit timer */
371 irlap_start_final_timer(self, self->final_timeout);
373 self->retry_count = 0;
374 irlap_next_state(self, LAP_SETUP);
378 /* Check if the frame contains and I field */
380 self->daddr = info->daddr;
381 self->caddr = info->caddr;
383 irlap_next_state(self, LAP_CONN);
385 irlap_connect_indication(self, skb);
387 IRDA_DEBUG(0, "%s(), SNRM frame does not "
388 "contain an I field!\n", __FUNCTION__);
391 case DISCOVERY_REQUEST:
392 IRDA_ASSERT(info != NULL, return -1;);
394 if (self->media_busy) {
395 IRDA_DEBUG(1, "%s(), DISCOVERY_REQUEST: media busy!\n",
397 /* irlap->log.condition = MEDIA_BUSY; */
399 /* This will make IrLMP try again */
400 irlap_discovery_confirm(self, NULL);
401 /* Note : the discovery log is not cleaned up here,
402 * it will be done in irlap_discovery_request()
409 irlap_send_discovery_xid_frame(self, info->S, info->s, TRUE,
411 self->frame_sent = FALSE;
414 irlap_start_slot_timer(self, self->slot_timeout);
415 irlap_next_state(self, LAP_QUERY);
417 case RECV_DISCOVERY_XID_CMD:
418 IRDA_ASSERT(info != NULL, return -1;);
420 /* Assert that this is not the final slot */
421 if (info->s <= info->S) {
422 self->slot = irlap_generate_rand_time_slot(info->S,
424 if (self->slot == info->s) {
425 discovery_rsp = irlmp_get_discovery_response();
426 discovery_rsp->data.daddr = info->daddr;
428 irlap_send_discovery_xid_frame(self, info->S,
432 self->frame_sent = TRUE;
434 self->frame_sent = FALSE;
437 * Go to reply state until end of discovery to
438 * inhibit our own transmissions. Set the timer
439 * to not stay forever there... Jean II
441 irlap_start_query_timer(self, info->S, info->s);
442 irlap_next_state(self, LAP_REPLY);
444 /* This is the final slot. How is it possible ?
445 * This would happen is both discoveries are just slightly
446 * offset (if they are in sync, all packets are lost).
447 * Most often, all the discovery requests will be received
448 * in QUERY state (see my comment there), except for the
449 * last frame that will come here.
450 * The big trouble when it happen is that active discovery
451 * doesn't happen, because nobody answer the discoveries
452 * frame of the other guy, so the log shows up empty.
453 * What should we do ?
454 * Not much. It's too late to answer those discovery frames,
455 * so we just pass the info to IrLMP who will put it in the
456 * log (and post an event).
457 * Another cause would be devices that do discovery much
458 * slower than us, however the latest fixes should minimise
462 IRDA_DEBUG(1, "%s(), Receiving final discovery request, missed the discovery slots :-(\n", __FUNCTION__);
464 /* Last discovery request -> in the log */
465 irlap_discovery_indication(self, info->discovery);
468 case MEDIA_BUSY_TIMER_EXPIRED:
469 /* A bunch of events may be postponed because the media is
470 * busy (usually immediately after we close a connection),
471 * or while we are doing discovery (state query/reply).
472 * In all those cases, the media busy flag will be cleared
473 * when it's OK for us to process those postponed events.
474 * This event is not mentioned in the state machines in the
475 * IrLAP spec. It's because they didn't consider Ultra and
476 * postponing connection request is optional.
478 #ifdef CONFIG_IRDA_ULTRA
479 /* Send any pending Ultra frames if any */
480 if (!skb_queue_empty(&self->txq_ultra)) {
481 /* We don't send the frame, just post an event.
482 * Also, previously this code was in timer.c...
484 ret = (*state[self->state])(self, SEND_UI_FRAME,
487 #endif /* CONFIG_IRDA_ULTRA */
488 /* Check if we should try to connect.
489 * This code was previously in irlap_do_event() */
490 if (self->connect_pending) {
491 self->connect_pending = FALSE;
493 /* This one *should* not pend in this state, except
494 * if a socket try to connect and immediately
495 * disconnect. - clear - Jean II */
496 if (self->disconnect_pending)
497 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
499 ret = (*state[self->state])(self,
502 self->disconnect_pending = FALSE;
504 /* Note : one way to test if this code works well (including
505 * media busy and small busy) is to create a user space
506 * application generating an Ultra packet every 3.05 sec (or
507 * 2.95 sec) and to see how it interact with discovery.
508 * It's fairly easy to check that no packet is lost, that the
509 * packets are postponed during discovery and that after
510 * discovery indication you have a 100ms "gap".
511 * As connection request and Ultra are now processed the same
512 * way, this avoid the tedious job of trying IrLAP connection
513 * in all those cases...
516 #ifdef CONFIG_IRDA_ULTRA
520 /* Only allowed to repeat an operation twice */
521 for (i=0; ((i<2) && (self->media_busy == FALSE)); i++) {
522 skb = skb_dequeue(&self->txq_ultra);
524 irlap_send_ui_frame(self, skb, CBROADCAST,
528 /* irlap_send_ui_frame() won't increase skb reference
529 * count, so no dev_kfree_skb() - Jean II */
532 /* Force us to listen 500 ms again */
533 irda_device_set_media_busy(self->netdev, TRUE);
538 /* Only accept broadcast frames in NDM mode */
539 if (info->caddr != CBROADCAST) {
540 IRDA_DEBUG(0, "%s(), not a broadcast frame!\n",
543 irlap_unitdata_indication(self, skb);
545 #endif /* CONFIG_IRDA_ULTRA */
547 /* Remove test frame header */
548 skb_pull(skb, sizeof(struct test_frame));
551 * Send response. This skb will not be sent out again, and
552 * will only be used to send out the same info as the cmd
554 irlap_send_test_frame(self, CBROADCAST, info->daddr, skb);
557 IRDA_DEBUG(0, "%s() not implemented!\n", __FUNCTION__);
560 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
570 * Function irlap_state_query (event, skb, info)
575 static int irlap_state_query(struct irlap_cb *self, IRLAP_EVENT event,
576 struct sk_buff *skb, struct irlap_info *info)
580 IRDA_ASSERT(self != NULL, return -1;);
581 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
584 case RECV_DISCOVERY_XID_RSP:
585 IRDA_ASSERT(info != NULL, return -1;);
586 IRDA_ASSERT(info->discovery != NULL, return -1;);
588 IRDA_DEBUG(4, "%s(), daddr=%08x\n", __FUNCTION__,
589 info->discovery->data.daddr);
591 if (!self->discovery_log) {
592 IRDA_WARNING("%s: discovery log is gone! "
593 "maybe the discovery timeout has been set"
594 " to short?\n", __FUNCTION__);
597 hashbin_insert(self->discovery_log,
598 (irda_queue_t *) info->discovery,
599 info->discovery->data.daddr, NULL);
602 /* irlap_next_state(self, LAP_QUERY); */
605 case RECV_DISCOVERY_XID_CMD:
606 /* Yes, it is possible to receive those frames in this mode.
607 * Note that most often the last discovery request won't
608 * occur here but in NDM state (see my comment there).
609 * What should we do ?
610 * Not much. We are currently performing our own discovery,
611 * therefore we can't answer those frames. We don't want
612 * to change state either. We just pass the info to
613 * IrLMP who will put it in the log (and post an event).
617 IRDA_ASSERT(info != NULL, return -1;);
619 IRDA_DEBUG(1, "%s(), Receiving discovery request (s = %d) while performing discovery :-(\n", __FUNCTION__, info->s);
621 /* Last discovery request ? */
623 irlap_discovery_indication(self, info->discovery);
625 case SLOT_TIMER_EXPIRED:
627 * Wait a little longer if we detect an incoming frame. This
628 * is not mentioned in the spec, but is a good thing to do,
629 * since we want to work even with devices that violate the
630 * timing requirements.
632 if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
633 IRDA_DEBUG(2, "%s(), device is slow to answer, "
634 "waiting some more!\n", __FUNCTION__);
635 irlap_start_slot_timer(self, msecs_to_jiffies(10));
636 self->add_wait = TRUE;
639 self->add_wait = FALSE;
641 if (self->s < self->S) {
642 irlap_send_discovery_xid_frame(self, self->S,
644 self->discovery_cmd);
646 irlap_start_slot_timer(self, self->slot_timeout);
649 irlap_next_state(self, LAP_QUERY);
651 /* This is the final slot! */
652 irlap_send_discovery_xid_frame(self, self->S, 0xff,
654 self->discovery_cmd);
656 /* Always switch state before calling upper layers */
657 irlap_next_state(self, LAP_NDM);
660 * We are now finished with the discovery procedure,
661 * so now we must return the results
663 irlap_discovery_confirm(self, self->discovery_log);
665 /* IrLMP should now have taken care of the log */
666 self->discovery_log = NULL;
670 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
680 * Function irlap_state_reply (self, event, skb, info)
682 * REPLY, we have received a XID discovery frame from a device and we
683 * are waiting for the right time slot to send a response XID frame
686 static int irlap_state_reply(struct irlap_cb *self, IRLAP_EVENT event,
687 struct sk_buff *skb, struct irlap_info *info)
689 discovery_t *discovery_rsp;
692 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
694 IRDA_ASSERT(self != NULL, return -1;);
695 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
698 case QUERY_TIMER_EXPIRED:
699 IRDA_DEBUG(0, "%s(), QUERY_TIMER_EXPIRED <%ld>\n",
700 __FUNCTION__, jiffies);
701 irlap_next_state(self, LAP_NDM);
703 case RECV_DISCOVERY_XID_CMD:
704 IRDA_ASSERT(info != NULL, return -1;);
706 if (info->s == 0xff) {
707 del_timer(&self->query_timer);
709 /* info->log.condition = REMOTE; */
711 /* Always switch state before calling upper layers */
712 irlap_next_state(self, LAP_NDM);
714 irlap_discovery_indication(self, info->discovery);
716 /* If it's our slot, send our reply */
717 if ((info->s >= self->slot) && (!self->frame_sent)) {
718 discovery_rsp = irlmp_get_discovery_response();
719 discovery_rsp->data.daddr = info->daddr;
721 irlap_send_discovery_xid_frame(self, info->S,
726 self->frame_sent = TRUE;
728 /* Readjust our timer to accomodate devices
729 * doing faster or slower discovery than us...
731 irlap_start_query_timer(self, info->S, info->s);
734 //irlap_next_state(self, LAP_REPLY);
738 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __FUNCTION__,
739 event, irlap_event[event]);
748 * Function irlap_state_conn (event, skb, info)
750 * CONN, we have received a SNRM command and is waiting for the upper
751 * layer to accept or refuse connection
754 static int irlap_state_conn(struct irlap_cb *self, IRLAP_EVENT event,
755 struct sk_buff *skb, struct irlap_info *info)
759 IRDA_DEBUG(4, "%s(), event=%s\n", __FUNCTION__, irlap_event[ event]);
761 IRDA_ASSERT(self != NULL, return -1;);
762 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
765 case CONNECT_RESPONSE:
766 skb_pull(skb, sizeof(struct snrm_frame));
768 IRDA_ASSERT(self->netdev != NULL, return -1;);
770 irlap_qos_negotiate(self, skb);
772 irlap_initiate_connection_state(self);
775 * Applying the parameters now will make sure we change speed
776 * *after* we have sent the next frame
778 irlap_apply_connection_parameters(self, FALSE);
781 * Sending this frame will force a speed change after it has
782 * been sent (i.e. the frame will be sent at 9600).
784 irlap_send_ua_response_frame(self, &self->qos_rx);
788 * We are allowed to send two frames, but this may increase
789 * the connect latency, so lets not do it for now.
791 /* This is full of good intentions, but doesn't work in
793 * After sending the first UA response, we switch the
794 * dongle to the negotiated speed, which is usually
795 * different than 9600 kb/s.
796 * From there, there is two solutions :
797 * 1) The other end has received the first UA response :
798 * it will set up the connection, move to state LAP_NRM_P,
799 * and will ignore and drop the second UA response.
800 * Actually, it's even worse : the other side will almost
801 * immediately send a RR that will likely collide with the
802 * UA response (depending on negotiated turnaround).
803 * 2) The other end has not received the first UA response,
804 * will stay at 9600 and will never see the second UA response.
806 irlap_send_ua_response_frame(self, &self->qos_rx);
810 * The WD-timer could be set to the duration of the P-timer
811 * for this case, but it is recommended to use twice the
812 * value (note 3 IrLAP p. 60).
814 irlap_start_wd_timer(self, self->wd_timeout);
815 irlap_next_state(self, LAP_NRM_S);
818 case RECV_DISCOVERY_XID_CMD:
819 IRDA_DEBUG(3, "%s(), event RECV_DISCOVER_XID_CMD!\n",
821 irlap_next_state(self, LAP_NDM);
824 case DISCONNECT_REQUEST:
825 IRDA_DEBUG(0, "%s(), Disconnect request!\n", __FUNCTION__);
826 irlap_send_dm_frame(self);
827 irlap_next_state( self, LAP_NDM);
828 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
831 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __FUNCTION__,
832 event, irlap_event[event]);
842 * Function irlap_state_setup (event, skb, frame)
844 * SETUP state, The local layer has transmitted a SNRM command frame to
845 * a remote peer layer and is awaiting a reply .
848 static int irlap_state_setup(struct irlap_cb *self, IRLAP_EVENT event,
849 struct sk_buff *skb, struct irlap_info *info)
853 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
855 IRDA_ASSERT(self != NULL, return -1;);
856 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
859 case FINAL_TIMER_EXPIRED:
860 if (self->retry_count < self->N3) {
862 * Perform random backoff, Wait a random number of time units, minimum
863 * duration half the time taken to transmitt a SNRM frame, maximum duration
864 * 1.5 times the time taken to transmit a SNRM frame. So this time should
865 * between 15 msecs and 45 msecs.
867 irlap_start_backoff_timer(self, msecs_to_jiffies(20 +
870 /* Always switch state before calling upper layers */
871 irlap_next_state(self, LAP_NDM);
873 irlap_disconnect_indication(self, LAP_FOUND_NONE);
876 case BACKOFF_TIMER_EXPIRED:
877 irlap_send_snrm_frame(self, &self->qos_rx);
878 irlap_start_final_timer(self, self->final_timeout);
882 IRDA_DEBUG(4, "%s(), SNRM battle!\n", __FUNCTION__);
884 IRDA_ASSERT(skb != NULL, return 0;);
885 IRDA_ASSERT(info != NULL, return 0;);
888 * The device with the largest device address wins the battle
889 * (both have sent a SNRM command!)
891 if (info &&(info->daddr > self->saddr)) {
892 del_timer(&self->final_timer);
893 irlap_initiate_connection_state(self);
895 IRDA_ASSERT(self->netdev != NULL, return -1;);
897 skb_pull(skb, sizeof(struct snrm_frame));
899 irlap_qos_negotiate(self, skb);
901 /* Send UA frame and then change link settings */
902 irlap_apply_connection_parameters(self, FALSE);
903 irlap_send_ua_response_frame(self, &self->qos_rx);
905 irlap_next_state(self, LAP_NRM_S);
906 irlap_connect_confirm(self, skb);
909 * The WD-timer could be set to the duration of the
910 * P-timer for this case, but it is recommended
911 * to use twice the value (note 3 IrLAP p. 60).
913 irlap_start_wd_timer(self, self->wd_timeout);
915 /* We just ignore the other device! */
916 irlap_next_state(self, LAP_SETUP);
921 del_timer(&self->final_timer);
923 /* Initiate connection state */
924 irlap_initiate_connection_state(self);
926 /* Negotiate connection parameters */
927 IRDA_ASSERT(skb->len > 10, return -1;);
929 skb_pull(skb, sizeof(struct ua_frame));
931 IRDA_ASSERT(self->netdev != NULL, return -1;);
933 irlap_qos_negotiate(self, skb);
935 /* Set the new link setting *now* (before the rr frame) */
936 irlap_apply_connection_parameters(self, TRUE);
937 self->retry_count = 0;
939 /* Wait for turnaround time to give a chance to the other
940 * device to be ready to receive us.
941 * Note : the time to switch speed is typically larger
942 * than the turnaround time, but as we don't have the other
943 * side speed switch time, that's our best guess...
945 irlap_wait_min_turn_around(self, &self->qos_tx);
947 /* This frame will actually be sent at the new speed */
948 irlap_send_rr_frame(self, CMD_FRAME);
950 /* The timer is set to half the normal timer to quickly
951 * detect a failure to negociate the new connection
952 * parameters. IrLAP 6.11.3.2, note 3.
953 * Note that currently we don't process this failure
954 * properly, as we should do a quick disconnect.
956 irlap_start_final_timer(self, self->final_timeout/2);
957 irlap_next_state(self, LAP_NRM_P);
959 irlap_connect_confirm(self, skb);
961 case RECV_DM_RSP: /* FALLTHROUGH */
963 del_timer(&self->final_timer);
964 irlap_next_state(self, LAP_NDM);
966 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
969 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __FUNCTION__,
970 event, irlap_event[event]);
979 * Function irlap_state_offline (self, event, skb, info)
981 * OFFLINE state, not used for now!
984 static int irlap_state_offline(struct irlap_cb *self, IRLAP_EVENT event,
985 struct sk_buff *skb, struct irlap_info *info)
987 IRDA_DEBUG( 0, "%s(), Unknown event\n", __FUNCTION__);
993 * Function irlap_state_xmit_p (self, event, skb, info)
995 * XMIT, Only the primary station has right to transmit, and we
996 * therefore do not expect to receive any transmissions from other
1000 static int irlap_state_xmit_p(struct irlap_cb *self, IRLAP_EVENT event,
1001 struct sk_buff *skb, struct irlap_info *info)
1008 * Only send frame if send-window > 0.
1010 if ((self->window > 0) && (!self->remote_busy)) {
1012 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1013 struct sk_buff *skb_next;
1015 /* With DYNAMIC_WINDOW, we keep the window size
1016 * maximum, and adapt on the packets we are sending.
1017 * At 115k, we can send only 2 packets of 2048 bytes
1018 * in a 500 ms turnaround. Without this option, we
1019 * would always limit the window to 2. With this
1020 * option, if we send smaller packets, we can send
1021 * up to 7 of them (always depending on QoS).
1024 /* Look at the next skb. This is safe, as we are
1025 * the only consumer of the Tx queue (if we are not,
1026 * we have other problems) - Jean II */
1027 skb_next = skb_peek(&self->txq);
1029 /* Check if a subsequent skb exist and would fit in
1030 * the current window (with respect to turnaround
1032 * This allow us to properly mark the current packet
1033 * with the pf bit, to avoid falling back on the
1034 * second test below, and avoid waiting the
1035 * end of the window and sending a extra RR.
1036 * Note : (skb_next != NULL) <=> (skb_queue_len() > 0)
1038 nextfit = ((skb_next != NULL) &&
1039 ((skb_next->len + skb->len) <=
1043 * The current packet may not fit ! Because of test
1044 * above, this should not happen any more !!!
1045 * Test if we have transmitted more bytes over the
1046 * link than its possible to do with the current
1047 * speed and turn-around-time.
1049 if((!nextfit) && (skb->len > self->bytes_left)) {
1050 IRDA_DEBUG(0, "%s(), Not allowed to transmit"
1051 " more bytes!\n", __FUNCTION__);
1052 /* Requeue the skb */
1053 skb_queue_head(&self->txq, skb_get(skb));
1055 * We should switch state to LAP_NRM_P, but
1056 * that is not possible since we must be sure
1057 * that we poll the other side. Since we have
1058 * used up our time, the poll timer should
1059 * trigger anyway now, so we just wait for it
1063 * Sorry, but that's not totally true. If
1064 * we send 2000B packets, we may wait another
1065 * 1000B until our turnaround expire. That's
1066 * why we need to be proactive in avoiding
1067 * coming here. - Jean II
1072 /* Substract space used by this skb */
1073 self->bytes_left -= skb->len;
1074 #else /* CONFIG_IRDA_DYNAMIC_WINDOW */
1075 /* Window has been adjusted for the max packet
1076 * size, so much simpler... - Jean II */
1077 nextfit = (skb_queue_len(&self->txq) > 0);
1078 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1080 * Send data with poll bit cleared only if window > 1
1081 * and there is more frames after this one to be sent
1083 if ((self->window > 1) && (nextfit)) {
1084 /* More packet to send in current window */
1085 irlap_send_data_primary(self, skb);
1086 irlap_next_state(self, LAP_XMIT_P);
1088 /* Final packet of window */
1089 irlap_send_data_primary_poll(self, skb);
1090 irlap_next_state(self, LAP_NRM_P);
1093 * Make sure state machine does not try to send
1098 #ifdef CONFIG_IRDA_FAST_RR
1099 /* Peer may want to reply immediately */
1100 self->fast_RR = FALSE;
1101 #endif /* CONFIG_IRDA_FAST_RR */
1103 IRDA_DEBUG(4, "%s(), Unable to send! remote busy?\n",
1105 skb_queue_head(&self->txq, skb_get(skb));
1108 * The next ret is important, because it tells
1109 * irlap_next_state _not_ to deliver more frames
1114 case POLL_TIMER_EXPIRED:
1115 IRDA_DEBUG(3, "%s(), POLL_TIMER_EXPIRED <%ld>\n",
1116 __FUNCTION__, jiffies);
1117 irlap_send_rr_frame(self, CMD_FRAME);
1118 /* Return to NRM properly - Jean II */
1119 self->window = self->window_size;
1120 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1121 /* Allowed to transmit a maximum number of bytes again. */
1122 self->bytes_left = self->line_capacity;
1123 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1124 irlap_start_final_timer(self, self->final_timeout);
1125 irlap_next_state(self, LAP_NRM_P);
1127 case DISCONNECT_REQUEST:
1128 del_timer(&self->poll_timer);
1129 irlap_wait_min_turn_around(self, &self->qos_tx);
1130 irlap_send_disc_frame(self);
1131 irlap_flush_all_queues(self);
1132 irlap_start_final_timer(self, self->final_timeout);
1133 self->retry_count = 0;
1134 irlap_next_state(self, LAP_PCLOSE);
1137 /* Nothing to do, irlap_do_event() will send the packet
1138 * when we return... - Jean II */
1141 IRDA_DEBUG(0, "%s(), Unknown event %s\n",
1142 __FUNCTION__, irlap_event[event]);
1151 * Function irlap_state_pclose (event, skb, info)
1155 static int irlap_state_pclose(struct irlap_cb *self, IRLAP_EVENT event,
1156 struct sk_buff *skb, struct irlap_info *info)
1160 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1162 IRDA_ASSERT(self != NULL, return -1;);
1163 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
1166 case RECV_UA_RSP: /* FALLTHROUGH */
1168 del_timer(&self->final_timer);
1170 /* Set new link parameters */
1171 irlap_apply_default_connection_parameters(self);
1173 /* Always switch state before calling upper layers */
1174 irlap_next_state(self, LAP_NDM);
1176 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
1178 case FINAL_TIMER_EXPIRED:
1179 if (self->retry_count < self->N3) {
1180 irlap_wait_min_turn_around(self, &self->qos_tx);
1181 irlap_send_disc_frame(self);
1182 irlap_start_final_timer(self, self->final_timeout);
1183 self->retry_count++;
1186 irlap_apply_default_connection_parameters(self);
1188 /* Always switch state before calling upper layers */
1189 irlap_next_state(self, LAP_NDM);
1191 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1195 IRDA_DEBUG(1, "%s(), Unknown event %d\n", __FUNCTION__, event);
1204 * Function irlap_state_nrm_p (self, event, skb, info)
1206 * NRM_P (Normal Response Mode as Primary), The primary station has given
1207 * permissions to a secondary station to transmit IrLAP resonse frames
1208 * (by sending a frame with the P bit set). The primary station will not
1209 * transmit any frames and is expecting to receive frames only from the
1210 * secondary to which transmission permissions has been given.
1212 static int irlap_state_nrm_p(struct irlap_cb *self, IRLAP_EVENT event,
1213 struct sk_buff *skb, struct irlap_info *info)
1220 case RECV_I_RSP: /* Optimize for the common case */
1221 /* FIXME: must check for remote_busy below */
1222 #ifdef CONFIG_IRDA_FAST_RR
1224 * Reset the fast_RR so we can use the fast RR code with
1225 * full speed the next time since peer may have more frames
1228 self->fast_RR = FALSE;
1229 #endif /* CONFIG_IRDA_FAST_RR */
1230 IRDA_ASSERT( info != NULL, return -1;);
1232 ns_status = irlap_validate_ns_received(self, info->ns);
1233 nr_status = irlap_validate_nr_received(self, info->nr);
1236 * Check for expected I(nformation) frame
1238 if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
1240 /* Update Vr (next frame for us to receive) */
1241 self->vr = (self->vr + 1) % 8;
1243 /* Update Nr received, cleanup our retry queue */
1244 irlap_update_nr_received(self, info->nr);
1247 * Got expected NR, so reset the
1248 * retry_count. This is not done by IrLAP spec,
1251 self->retry_count = 0;
1252 self->ack_required = TRUE;
1254 /* poll bit cleared? */
1256 /* Keep state, do not move this line */
1257 irlap_next_state(self, LAP_NRM_P);
1259 irlap_data_indication(self, skb, FALSE);
1261 /* No longer waiting for pf */
1262 del_timer(&self->final_timer);
1264 irlap_wait_min_turn_around(self, &self->qos_tx);
1266 /* Call higher layer *before* changing state
1267 * to give them a chance to send data in the
1270 irlap_data_indication(self, skb, FALSE);
1272 /* XMIT states are the most dangerous state
1273 * to be in, because user requests are
1274 * processed directly and may change state.
1275 * On the other hand, in NDM_P, those
1276 * requests are queued and we will process
1277 * them when we return to irlap_do_event().
1280 irlap_next_state(self, LAP_XMIT_P);
1282 /* This is the last frame.
1283 * Make sure it's always called in XMIT state.
1285 irlap_start_poll_timer(self, self->poll_timeout);
1290 /* Unexpected next to send (Ns) */
1291 if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
1294 irlap_update_nr_received(self, info->nr);
1297 * Wait until the last frame before doing
1302 irlap_next_state(self, LAP_NRM_P);
1305 "%s(), missing or duplicate frame!\n",
1308 /* Update Nr received */
1309 irlap_update_nr_received(self, info->nr);
1311 irlap_wait_min_turn_around(self, &self->qos_tx);
1312 irlap_send_rr_frame(self, CMD_FRAME);
1314 self->ack_required = FALSE;
1316 irlap_start_final_timer(self, self->final_timeout);
1317 irlap_next_state(self, LAP_NRM_P);
1322 * Unexpected next to receive (Nr)
1324 if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
1327 self->vr = (self->vr + 1) % 8;
1329 /* Update Nr received */
1330 irlap_update_nr_received(self, info->nr);
1332 /* Resend rejected frames */
1333 irlap_resend_rejected_frames(self, CMD_FRAME);
1335 self->ack_required = FALSE;
1337 /* Make sure we account for the time
1338 * to transmit our frames. See comemnts
1339 * in irlap_send_data_primary_poll().
1341 irlap_start_final_timer(self, 2 * self->final_timeout);
1343 /* Keep state, do not move this line */
1344 irlap_next_state(self, LAP_NRM_P);
1346 irlap_data_indication(self, skb, FALSE);
1349 * Do not resend frames until the last
1350 * frame has arrived from the other
1351 * device. This is not documented in
1354 self->vr = (self->vr + 1) % 8;
1356 /* Update Nr received */
1357 irlap_update_nr_received(self, info->nr);
1359 self->ack_required = FALSE;
1361 /* Keep state, do not move this line!*/
1362 irlap_next_state(self, LAP_NRM_P);
1364 irlap_data_indication(self, skb, FALSE);
1369 * Unexpected next to send (Ns) and next to receive (Nr)
1370 * Not documented by IrLAP!
1372 if ((ns_status == NS_UNEXPECTED) &&
1373 (nr_status == NR_UNEXPECTED))
1375 IRDA_DEBUG(4, "%s(), unexpected nr and ns!\n",
1378 /* Resend rejected frames */
1379 irlap_resend_rejected_frames(self, CMD_FRAME);
1381 /* Give peer some time to retransmit!
1382 * But account for our own Tx. */
1383 irlap_start_final_timer(self, 2 * self->final_timeout);
1385 /* Keep state, do not move this line */
1386 irlap_next_state(self, LAP_NRM_P);
1388 /* Update Nr received */
1389 /* irlap_update_nr_received( info->nr); */
1391 self->ack_required = FALSE;
1399 if ((nr_status == NR_INVALID) || (ns_status == NS_INVALID)) {
1401 del_timer(&self->final_timer);
1403 irlap_next_state(self, LAP_RESET_WAIT);
1405 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1406 self->xmitflag = TRUE;
1408 del_timer(&self->final_timer);
1410 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1412 self->xmitflag = FALSE;
1416 IRDA_DEBUG(1, "%s(), Not implemented!\n", __FUNCTION__);
1417 IRDA_DEBUG(1, "%s(), event=%s, ns_status=%d, nr_status=%d\n",
1418 __FUNCTION__, irlap_event[event], ns_status, nr_status);
1421 /* Poll bit cleared? */
1423 irlap_data_indication(self, skb, TRUE);
1424 irlap_next_state(self, LAP_NRM_P);
1426 del_timer(&self->final_timer);
1427 irlap_data_indication(self, skb, TRUE);
1428 irlap_next_state(self, LAP_XMIT_P);
1429 IRDA_DEBUG(1, "%s: RECV_UI_FRAME: next state %s\n", __FUNCTION__, irlap_state[self->state]);
1430 irlap_start_poll_timer(self, self->poll_timeout);
1435 * If you get a RR, the remote isn't busy anymore,
1436 * no matter what the NR
1438 self->remote_busy = FALSE;
1443 ret = irlap_validate_nr_received(self, info->nr);
1444 if (ret == NR_EXPECTED) {
1445 /* Stop final timer */
1446 del_timer(&self->final_timer);
1448 /* Update Nr received */
1449 irlap_update_nr_received(self, info->nr);
1452 * Got expected NR, so reset the retry_count. This
1453 * is not done by the IrLAP standard , which is
1456 self->retry_count = 0;
1457 irlap_wait_min_turn_around(self, &self->qos_tx);
1459 irlap_next_state(self, LAP_XMIT_P);
1461 /* Start poll timer */
1462 irlap_start_poll_timer(self, self->poll_timeout);
1463 } else if (ret == NR_UNEXPECTED) {
1464 IRDA_ASSERT(info != NULL, return -1;);
1469 /* Update Nr received */
1470 irlap_update_nr_received(self, info->nr);
1472 IRDA_DEBUG(4, "RECV_RR_FRAME: Retrans:%d, nr=%d, va=%d, "
1474 self->retry_count, info->nr, self->va,
1475 self->vs, self->vr);
1477 /* Resend rejected frames */
1478 irlap_resend_rejected_frames(self, CMD_FRAME);
1480 /* Final timer ??? Jean II */
1482 irlap_next_state(self, LAP_NRM_P);
1483 } else if (ret == NR_INVALID) {
1484 IRDA_DEBUG(1, "%s(), Received RR with "
1485 "invalid nr !\n", __FUNCTION__);
1486 del_timer(&self->final_timer);
1488 irlap_next_state(self, LAP_RESET_WAIT);
1490 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1491 self->xmitflag = TRUE;
1495 IRDA_ASSERT(info != NULL, return -1;);
1497 /* Stop final timer */
1498 del_timer(&self->final_timer);
1499 self->remote_busy = TRUE;
1501 /* Update Nr received */
1502 irlap_update_nr_received(self, info->nr);
1503 irlap_next_state(self, LAP_XMIT_P);
1505 /* Start poll timer */
1506 irlap_start_poll_timer(self, self->poll_timeout);
1509 del_timer(&self->final_timer);
1510 self->xmitflag = TRUE;
1511 irlap_next_state(self, LAP_RESET_WAIT);
1512 irlap_reset_indication(self);
1514 case FINAL_TIMER_EXPIRED:
1516 * We are allowed to wait for additional 300 ms if
1517 * final timer expires when we are in the middle
1518 * of receiving a frame (page 45, IrLAP). Check that
1519 * we only do this once for each frame.
1521 if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
1522 IRDA_DEBUG(1, "FINAL_TIMER_EXPIRED when receiving a "
1523 "frame! Waiting a little bit more!\n");
1524 irlap_start_final_timer(self, msecs_to_jiffies(300));
1527 * Don't allow this to happen one more time in a row,
1528 * or else we can get a pretty tight loop here if
1529 * if we only receive half a frame. DB.
1531 self->add_wait = TRUE;
1534 self->add_wait = FALSE;
1536 /* N2 is the disconnect timer. Until we reach it, we retry */
1537 if (self->retry_count < self->N2) {
1538 /* Retry sending the pf bit to the secondary */
1539 irlap_wait_min_turn_around(self, &self->qos_tx);
1540 irlap_send_rr_frame(self, CMD_FRAME);
1542 irlap_start_final_timer(self, self->final_timeout);
1543 self->retry_count++;
1544 IRDA_DEBUG(4, "irlap_state_nrm_p: FINAL_TIMER_EXPIRED:"
1545 " retry_count=%d\n", self->retry_count);
1547 /* Early warning event. I'm using a pretty liberal
1548 * interpretation of the spec and generate an event
1549 * every time the timer is multiple of N1 (and not
1550 * only the first time). This allow application
1551 * to know precisely if connectivity restart...
1553 if((self->retry_count % self->N1) == 0)
1554 irlap_status_indication(self,
1555 STATUS_NO_ACTIVITY);
1559 irlap_apply_default_connection_parameters(self);
1561 /* Always switch state before calling upper layers */
1562 irlap_next_state(self, LAP_NDM);
1563 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1567 irlap_update_nr_received(self, info->nr);
1568 if (self->remote_busy) {
1569 irlap_wait_min_turn_around(self, &self->qos_tx);
1570 irlap_send_rr_frame(self, CMD_FRAME);
1572 irlap_resend_rejected_frames(self, CMD_FRAME);
1573 irlap_start_final_timer(self, 2 * self->final_timeout);
1576 irlap_update_nr_received(self, info->nr);
1577 if (self->remote_busy) {
1578 irlap_wait_min_turn_around(self, &self->qos_tx);
1579 irlap_send_rr_frame(self, CMD_FRAME);
1581 irlap_resend_rejected_frame(self, CMD_FRAME);
1582 irlap_start_final_timer(self, 2 * self->final_timeout);
1585 IRDA_DEBUG(1, "%s(), RECV_RD_RSP\n", __FUNCTION__);
1587 irlap_flush_all_queues(self);
1588 irlap_next_state(self, LAP_XMIT_P);
1589 /* Call back the LAP state machine to do a proper disconnect */
1590 irlap_disconnect_request(self);
1593 IRDA_DEBUG(1, "%s(), Unknown event %s\n",
1594 __FUNCTION__, irlap_event[event]);
1603 * Function irlap_state_reset_wait (event, skb, info)
1605 * We have informed the service user of a reset condition, and is
1606 * awaiting reset of disconnect request.
1609 static int irlap_state_reset_wait(struct irlap_cb *self, IRLAP_EVENT event,
1610 struct sk_buff *skb, struct irlap_info *info)
1614 IRDA_DEBUG(3, "%s(), event = %s\n", __FUNCTION__, irlap_event[event]);
1616 IRDA_ASSERT(self != NULL, return -1;);
1617 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
1621 if (self->xmitflag) {
1622 irlap_wait_min_turn_around(self, &self->qos_tx);
1623 irlap_send_snrm_frame(self, NULL);
1624 irlap_start_final_timer(self, self->final_timeout);
1625 irlap_next_state(self, LAP_RESET);
1627 irlap_start_final_timer(self, self->final_timeout);
1628 irlap_next_state(self, LAP_RESET);
1631 case DISCONNECT_REQUEST:
1632 irlap_wait_min_turn_around( self, &self->qos_tx);
1633 irlap_send_disc_frame( self);
1634 irlap_flush_all_queues( self);
1635 irlap_start_final_timer( self, self->final_timeout);
1636 self->retry_count = 0;
1637 irlap_next_state( self, LAP_PCLOSE);
1640 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
1641 irlap_event[event]);
1650 * Function irlap_state_reset (self, event, skb, info)
1652 * We have sent a SNRM reset command to the peer layer, and is awaiting
1656 static int irlap_state_reset(struct irlap_cb *self, IRLAP_EVENT event,
1657 struct sk_buff *skb, struct irlap_info *info)
1661 IRDA_DEBUG(3, "%s(), event = %s\n", __FUNCTION__, irlap_event[event]);
1663 IRDA_ASSERT(self != NULL, return -1;);
1664 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
1668 del_timer(&self->final_timer);
1670 irlap_apply_default_connection_parameters(self);
1672 /* Always switch state before calling upper layers */
1673 irlap_next_state(self, LAP_NDM);
1675 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1679 del_timer(&self->final_timer);
1681 /* Initiate connection state */
1682 irlap_initiate_connection_state(self);
1684 irlap_reset_confirm();
1686 self->remote_busy = FALSE;
1688 irlap_next_state(self, LAP_XMIT_P);
1690 irlap_start_poll_timer(self, self->poll_timeout);
1693 case FINAL_TIMER_EXPIRED:
1694 if (self->retry_count < 3) {
1695 irlap_wait_min_turn_around(self, &self->qos_tx);
1697 IRDA_ASSERT(self->netdev != NULL, return -1;);
1698 irlap_send_snrm_frame(self, self->qos_dev);
1700 self->retry_count++; /* Experimental!! */
1702 irlap_start_final_timer(self, self->final_timeout);
1703 irlap_next_state(self, LAP_RESET);
1704 } else if (self->retry_count >= self->N3) {
1705 irlap_apply_default_connection_parameters(self);
1707 /* Always switch state before calling upper layers */
1708 irlap_next_state(self, LAP_NDM);
1710 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1715 * SNRM frame is not allowed to contain an I-field in this
1719 IRDA_DEBUG(3, "%s(), RECV_SNRM_CMD\n", __FUNCTION__);
1720 irlap_initiate_connection_state(self);
1721 irlap_wait_min_turn_around(self, &self->qos_tx);
1722 irlap_send_ua_response_frame(self, &self->qos_rx);
1723 irlap_reset_confirm();
1724 irlap_start_wd_timer(self, self->wd_timeout);
1725 irlap_next_state(self, LAP_NDM);
1728 "%s(), SNRM frame contained an I field!\n",
1733 IRDA_DEBUG(1, "%s(), Unknown event %s\n",
1734 __FUNCTION__, irlap_event[event]);
1743 * Function irlap_state_xmit_s (event, skb, info)
1745 * XMIT_S, The secondary station has been given the right to transmit,
1746 * and we therefor do not expect to receive any transmissions from other
1749 static int irlap_state_xmit_s(struct irlap_cb *self, IRLAP_EVENT event,
1750 struct sk_buff *skb, struct irlap_info *info)
1754 IRDA_DEBUG(4, "%s(), event=%s\n", __FUNCTION__, irlap_event[event]);
1756 IRDA_ASSERT(self != NULL, return -ENODEV;);
1757 IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
1762 * Send frame only if send window > 0
1764 if ((self->window > 0) && (!self->remote_busy)) {
1766 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1767 struct sk_buff *skb_next;
1770 * Same deal as in irlap_state_xmit_p(), so see
1771 * the comments at that point.
1772 * We are the secondary, so there are only subtle
1773 * differences. - Jean II
1776 /* Check if a subsequent skb exist and would fit in
1777 * the current window (with respect to turnaround
1778 * time). - Jean II */
1779 skb_next = skb_peek(&self->txq);
1780 nextfit = ((skb_next != NULL) &&
1781 ((skb_next->len + skb->len) <=
1785 * Test if we have transmitted more bytes over the
1786 * link than its possible to do with the current
1787 * speed and turn-around-time.
1789 if((!nextfit) && (skb->len > self->bytes_left)) {
1790 IRDA_DEBUG(0, "%s(), Not allowed to transmit"
1791 " more bytes!\n", __FUNCTION__);
1792 /* Requeue the skb */
1793 skb_queue_head(&self->txq, skb_get(skb));
1796 * Switch to NRM_S, this is only possible
1797 * when we are in secondary mode, since we
1798 * must be sure that we don't miss any RR
1801 self->window = self->window_size;
1802 self->bytes_left = self->line_capacity;
1803 irlap_start_wd_timer(self, self->wd_timeout);
1805 irlap_next_state(self, LAP_NRM_S);
1806 /* Slight difference with primary :
1807 * here we would wait for the other side to
1808 * expire the turnaround. - Jean II */
1810 return -EPROTO; /* Try again later */
1812 /* Substract space used by this skb */
1813 self->bytes_left -= skb->len;
1814 #else /* CONFIG_IRDA_DYNAMIC_WINDOW */
1815 /* Window has been adjusted for the max packet
1816 * size, so much simpler... - Jean II */
1817 nextfit = (skb_queue_len(&self->txq) > 0);
1818 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1820 * Send data with final bit cleared only if window > 1
1821 * and there is more frames to be sent
1823 if ((self->window > 1) && (nextfit)) {
1824 irlap_send_data_secondary(self, skb);
1825 irlap_next_state(self, LAP_XMIT_S);
1827 irlap_send_data_secondary_final(self, skb);
1828 irlap_next_state(self, LAP_NRM_S);
1831 * Make sure state machine does not try to send
1837 IRDA_DEBUG(2, "%s(), Unable to send!\n", __FUNCTION__);
1838 skb_queue_head(&self->txq, skb_get(skb));
1842 case DISCONNECT_REQUEST:
1843 irlap_send_rd_frame(self);
1844 irlap_flush_all_queues(self);
1845 irlap_start_wd_timer(self, self->wd_timeout);
1846 irlap_next_state(self, LAP_SCLOSE);
1849 /* Nothing to do, irlap_do_event() will send the packet
1850 * when we return... - Jean II */
1853 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
1854 irlap_event[event]);
1863 * Function irlap_state_nrm_s (event, skb, info)
1865 * NRM_S (Normal Response Mode as Secondary) state, in this state we are
1866 * expecting to receive frames from the primary station
1869 static int irlap_state_nrm_s(struct irlap_cb *self, IRLAP_EVENT event,
1870 struct sk_buff *skb, struct irlap_info *info)
1876 IRDA_DEBUG(4, "%s(), event=%s\n", __FUNCTION__, irlap_event[ event]);
1878 IRDA_ASSERT(self != NULL, return -1;);
1879 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
1882 case RECV_I_CMD: /* Optimize for the common case */
1883 /* FIXME: must check for remote_busy below */
1884 IRDA_DEBUG(4, "%s(), event=%s nr=%d, vs=%d, ns=%d, "
1885 "vr=%d, pf=%d\n", __FUNCTION__,
1886 irlap_event[event], info->nr,
1887 self->vs, info->ns, self->vr, info->pf);
1889 self->retry_count = 0;
1891 ns_status = irlap_validate_ns_received(self, info->ns);
1892 nr_status = irlap_validate_nr_received(self, info->nr);
1894 * Check for expected I(nformation) frame
1896 if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
1898 /* Update Vr (next frame for us to receive) */
1899 self->vr = (self->vr + 1) % 8;
1901 /* Update Nr received */
1902 irlap_update_nr_received(self, info->nr);
1909 self->ack_required = TRUE;
1912 * Starting WD-timer here is optional, but
1913 * not recommended. Note 6 IrLAP p. 83
1916 irda_start_timer(WD_TIMER, self->wd_timeout);
1918 /* Keep state, do not move this line */
1919 irlap_next_state(self, LAP_NRM_S);
1921 irlap_data_indication(self, skb, FALSE);
1925 * We should wait before sending RR, and
1926 * also before changing to XMIT_S
1927 * state. (note 1, IrLAP p. 82)
1929 irlap_wait_min_turn_around(self, &self->qos_tx);
1932 * Give higher layers a chance to
1933 * immediately reply with some data before
1934 * we decide if we should send a RR frame
1937 irlap_data_indication(self, skb, FALSE);
1939 /* Any pending data requests? */
1940 if ((skb_queue_len(&self->txq) > 0) &&
1943 self->ack_required = TRUE;
1945 del_timer(&self->wd_timer);
1947 irlap_next_state(self, LAP_XMIT_S);
1949 irlap_send_rr_frame(self, RSP_FRAME);
1950 irlap_start_wd_timer(self,
1953 /* Keep the state */
1954 irlap_next_state(self, LAP_NRM_S);
1960 * Check for Unexpected next to send (Ns)
1962 if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
1964 /* Unexpected next to send, with final bit cleared */
1966 irlap_update_nr_received(self, info->nr);
1968 irlap_start_wd_timer(self, self->wd_timeout);
1970 /* Update Nr received */
1971 irlap_update_nr_received(self, info->nr);
1973 irlap_wait_min_turn_around(self, &self->qos_tx);
1974 irlap_send_rr_frame(self, RSP_FRAME);
1976 irlap_start_wd_timer(self, self->wd_timeout);
1982 * Unexpected Next to Receive(NR) ?
1984 if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
1987 IRDA_DEBUG(4, "RECV_I_RSP: frame(s) lost\n");
1989 self->vr = (self->vr + 1) % 8;
1991 /* Update Nr received */
1992 irlap_update_nr_received(self, info->nr);
1994 /* Resend rejected frames */
1995 irlap_resend_rejected_frames(self, RSP_FRAME);
1997 /* Keep state, do not move this line */
1998 irlap_next_state(self, LAP_NRM_S);
2000 irlap_data_indication(self, skb, FALSE);
2001 irlap_start_wd_timer(self, self->wd_timeout);
2005 * This is not documented in IrLAP!! Unexpected NR
2006 * with poll bit cleared
2009 self->vr = (self->vr + 1) % 8;
2011 /* Update Nr received */
2012 irlap_update_nr_received(self, info->nr);
2014 /* Keep state, do not move this line */
2015 irlap_next_state(self, LAP_NRM_S);
2017 irlap_data_indication(self, skb, FALSE);
2018 irlap_start_wd_timer(self, self->wd_timeout);
2023 if (ret == NR_INVALID) {
2024 IRDA_DEBUG(0, "NRM_S, NR_INVALID not implemented!\n");
2026 if (ret == NS_INVALID) {
2027 IRDA_DEBUG(0, "NRM_S, NS_INVALID not implemented!\n");
2035 irlap_data_indication(self, skb, TRUE);
2036 irlap_next_state(self, LAP_NRM_S); /* Keep state */
2039 * Any pending data requests?
2041 if ((skb_queue_len(&self->txq) > 0) &&
2042 (self->window > 0) && !self->remote_busy)
2044 irlap_data_indication(self, skb, TRUE);
2046 del_timer(&self->wd_timer);
2048 irlap_next_state(self, LAP_XMIT_S);
2050 irlap_data_indication(self, skb, TRUE);
2052 irlap_wait_min_turn_around(self, &self->qos_tx);
2054 irlap_send_rr_frame(self, RSP_FRAME);
2055 self->ack_required = FALSE;
2057 irlap_start_wd_timer(self, self->wd_timeout);
2059 /* Keep the state */
2060 irlap_next_state(self, LAP_NRM_S);
2065 self->retry_count = 0;
2070 nr_status = irlap_validate_nr_received(self, info->nr);
2071 if (nr_status == NR_EXPECTED) {
2072 if ((skb_queue_len( &self->txq) > 0) &&
2073 (self->window > 0)) {
2074 self->remote_busy = FALSE;
2076 /* Update Nr received */
2077 irlap_update_nr_received(self, info->nr);
2078 del_timer(&self->wd_timer);
2080 irlap_wait_min_turn_around(self, &self->qos_tx);
2081 irlap_next_state(self, LAP_XMIT_S);
2083 self->remote_busy = FALSE;
2084 /* Update Nr received */
2085 irlap_update_nr_received(self, info->nr);
2086 irlap_wait_min_turn_around(self, &self->qos_tx);
2087 irlap_start_wd_timer(self, self->wd_timeout);
2089 /* Note : if the link is idle (this case),
2090 * we never go in XMIT_S, so we never get a
2091 * chance to process any DISCONNECT_REQUEST.
2092 * Do it now ! - Jean II */
2093 if (self->disconnect_pending) {
2095 irlap_send_rd_frame(self);
2096 irlap_flush_all_queues(self);
2098 irlap_next_state(self, LAP_SCLOSE);
2100 /* Just send back pf bit */
2101 irlap_send_rr_frame(self, RSP_FRAME);
2103 irlap_next_state(self, LAP_NRM_S);
2106 } else if (nr_status == NR_UNEXPECTED) {
2107 self->remote_busy = FALSE;
2108 irlap_update_nr_received(self, info->nr);
2109 irlap_resend_rejected_frames(self, RSP_FRAME);
2111 irlap_start_wd_timer(self, self->wd_timeout);
2114 irlap_next_state(self, LAP_NRM_S);
2116 IRDA_DEBUG(1, "%s(), invalid nr not implemented!\n",
2121 /* SNRM frame is not allowed to contain an I-field */
2123 del_timer(&self->wd_timer);
2124 IRDA_DEBUG(1, "%s(), received SNRM cmd\n", __FUNCTION__);
2125 irlap_next_state(self, LAP_RESET_CHECK);
2127 irlap_reset_indication(self);
2130 "%s(), SNRM frame contained an I-field!\n",
2136 irlap_update_nr_received(self, info->nr);
2137 if (self->remote_busy) {
2138 irlap_wait_min_turn_around(self, &self->qos_tx);
2139 irlap_send_rr_frame(self, RSP_FRAME);
2141 irlap_resend_rejected_frames(self, RSP_FRAME);
2142 irlap_start_wd_timer(self, self->wd_timeout);
2145 irlap_update_nr_received(self, info->nr);
2146 if (self->remote_busy) {
2147 irlap_wait_min_turn_around(self, &self->qos_tx);
2148 irlap_send_rr_frame(self, RSP_FRAME);
2150 irlap_resend_rejected_frame(self, RSP_FRAME);
2151 irlap_start_wd_timer(self, self->wd_timeout);
2153 case WD_TIMER_EXPIRED:
2155 * Wait until retry_count * n matches negotiated threshold/
2156 * disconnect time (note 2 in IrLAP p. 82)
2158 * Similar to irlap_state_nrm_p() -> FINAL_TIMER_EXPIRED
2159 * Note : self->wd_timeout = (self->final_timeout * 2),
2160 * which explain why we use (self->N2 / 2) here !!!
2163 IRDA_DEBUG(1, "%s(), retry_count = %d\n", __FUNCTION__,
2166 if (self->retry_count < (self->N2 / 2)) {
2167 /* No retry, just wait for primary */
2168 irlap_start_wd_timer(self, self->wd_timeout);
2169 self->retry_count++;
2171 if((self->retry_count % (self->N1 / 2)) == 0)
2172 irlap_status_indication(self,
2173 STATUS_NO_ACTIVITY);
2175 irlap_apply_default_connection_parameters(self);
2177 /* Always switch state before calling upper layers */
2178 irlap_next_state(self, LAP_NDM);
2179 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
2183 /* Always switch state before calling upper layers */
2184 irlap_next_state(self, LAP_NDM);
2186 /* Send disconnect response */
2187 irlap_wait_min_turn_around(self, &self->qos_tx);
2188 irlap_send_ua_response_frame(self, NULL);
2190 del_timer(&self->wd_timer);
2191 irlap_flush_all_queues(self);
2192 /* Set default link parameters */
2193 irlap_apply_default_connection_parameters(self);
2195 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2197 case RECV_DISCOVERY_XID_CMD:
2198 irlap_wait_min_turn_around(self, &self->qos_tx);
2199 irlap_send_rr_frame(self, RSP_FRAME);
2200 self->ack_required = TRUE;
2201 irlap_start_wd_timer(self, self->wd_timeout);
2202 irlap_next_state(self, LAP_NRM_S);
2206 /* Remove test frame header (only LAP header in NRM) */
2207 skb_pull(skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
2209 irlap_wait_min_turn_around(self, &self->qos_tx);
2210 irlap_start_wd_timer(self, self->wd_timeout);
2212 /* Send response (info will be copied) */
2213 irlap_send_test_frame(self, self->caddr, info->daddr, skb);
2216 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __FUNCTION__,
2217 event, irlap_event[event]);
2226 * Function irlap_state_sclose (self, event, skb, info)
2228 static int irlap_state_sclose(struct irlap_cb *self, IRLAP_EVENT event,
2229 struct sk_buff *skb, struct irlap_info *info)
2233 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
2235 IRDA_ASSERT(self != NULL, return -ENODEV;);
2236 IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
2240 /* Always switch state before calling upper layers */
2241 irlap_next_state(self, LAP_NDM);
2243 /* Send disconnect response */
2244 irlap_wait_min_turn_around(self, &self->qos_tx);
2245 irlap_send_ua_response_frame(self, NULL);
2247 del_timer(&self->wd_timer);
2248 /* Set default link parameters */
2249 irlap_apply_default_connection_parameters(self);
2251 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2254 /* IrLAP-1.1 p.82: in SCLOSE, S and I type RSP frames
2255 * shall take us down into default NDM state, like DM_RSP
2262 /* Always switch state before calling upper layers */
2263 irlap_next_state(self, LAP_NDM);
2265 del_timer(&self->wd_timer);
2266 irlap_apply_default_connection_parameters(self);
2268 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2270 case WD_TIMER_EXPIRED:
2271 /* Always switch state before calling upper layers */
2272 irlap_next_state(self, LAP_NDM);
2274 irlap_apply_default_connection_parameters(self);
2276 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2279 /* IrLAP-1.1 p.82: in SCLOSE, basically any received frame
2280 * with pf=1 shall restart the wd-timer and resend the rd:rsp
2282 if (info != NULL && info->pf) {
2283 del_timer(&self->wd_timer);
2284 irlap_wait_min_turn_around(self, &self->qos_tx);
2285 irlap_send_rd_frame(self);
2286 irlap_start_wd_timer(self, self->wd_timeout);
2287 break; /* stay in SCLOSE */
2290 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __FUNCTION__,
2291 event, irlap_event[event]);
2300 static int irlap_state_reset_check( struct irlap_cb *self, IRLAP_EVENT event,
2301 struct sk_buff *skb,
2302 struct irlap_info *info)
2306 IRDA_DEBUG(1, "%s(), event=%s\n", __FUNCTION__, irlap_event[event]);
2308 IRDA_ASSERT(self != NULL, return -ENODEV;);
2309 IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
2312 case RESET_RESPONSE:
2313 irlap_send_ua_response_frame(self, &self->qos_rx);
2314 irlap_initiate_connection_state(self);
2315 irlap_start_wd_timer(self, WD_TIMEOUT);
2316 irlap_flush_all_queues(self);
2318 irlap_next_state(self, LAP_NRM_S);
2320 case DISCONNECT_REQUEST:
2321 irlap_wait_min_turn_around(self, &self->qos_tx);
2322 irlap_send_rd_frame(self);
2323 irlap_start_wd_timer(self, WD_TIMEOUT);
2324 irlap_next_state(self, LAP_SCLOSE);
2327 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __FUNCTION__,
2328 event, irlap_event[event]);