1 /*********************************************************************
3 * Filename: irlap_event.c
5 * Description: IrLAP state machine implementation
6 * Status: Experimental.
7 * Author: Dag Brattli <dag@brattli.net>
8 * Created at: Sat Aug 16 00:59:29 1997
9 * Modified at: Sat Dec 25 21:07:57 1999
10 * Modified by: Dag Brattli <dag@brattli.net>
12 * Copyright (c) 1998-2000 Dag Brattli <dag@brattli.net>,
13 * Copyright (c) 1998 Thomas Davis <ratbert@radiks.net>
14 * All Rights Reserved.
15 * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of
20 * the License, or (at your option) any later version.
22 * Neither Dag Brattli nor University of Tromsø admit liability nor
23 * provide warranty for any of this software. This material is
24 * provided "AS-IS" and at no charge.
26 ********************************************************************/
28 #include <linux/string.h>
29 #include <linux/kernel.h>
30 #include <linux/delay.h>
31 #include <linux/skbuff.h>
33 #include <net/irda/irda.h>
34 #include <net/irda/irlap_event.h>
36 #include <net/irda/timer.h>
37 #include <net/irda/irlap.h>
38 #include <net/irda/irlap_frame.h>
39 #include <net/irda/qos.h>
40 #include <net/irda/parameters.h>
41 #include <net/irda/irlmp.h> /* irlmp_flow_indication(), ... */
43 #include <net/irda/irda_device.h>
45 #ifdef 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 IRDA_ASSERT(self != NULL, return;);
172 IRDA_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 static void irlap_start_poll_timer(struct irlap_cb *self, int timeout)
185 IRDA_ASSERT(self != NULL, return;);
186 IRDA_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_empty(&self->txq) || 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_empty(&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,
289 /* Drop reference count.
290 * It will be increase as needed in
291 * irlap_send_data_xxx() */
294 /* Poll the higher layers for one more frame */
295 irlmp_flow_indication(self->notify.instance,
299 break; /* Try again later! */
301 /* Finished transmitting */
302 self->local_busy = FALSE;
303 } else if (self->disconnect_pending) {
304 self->disconnect_pending = FALSE;
306 ret = (*state[self->state])(self, DISCONNECT_REQUEST,
312 /* case LAP_RESET_WAIT: */
313 /* case LAP_RESET_CHECK: */
320 * Function irlap_next_state (self, state)
322 * Switches state and provides debug information
325 static inline void irlap_next_state(struct irlap_cb *self, IRLAP_STATE state)
328 if (!self || self->magic != LAP_MAGIC)
331 IRDA_DEBUG(4, "next LAP state = %s\n", irlap_state[state]);
337 * Function irlap_state_ndm (event, skb, frame)
339 * NDM (Normal Disconnected Mode) state
342 static int irlap_state_ndm(struct irlap_cb *self, IRLAP_EVENT event,
343 struct sk_buff *skb, struct irlap_info *info)
345 discovery_t *discovery_rsp;
348 IRDA_ASSERT(self != NULL, return -1;);
349 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
352 case CONNECT_REQUEST:
353 IRDA_ASSERT(self->netdev != NULL, return -1;);
355 if (self->media_busy) {
356 /* Note : this will never happen, because we test
357 * media busy in irlap_connect_request() and
358 * postpone the event... - Jean II */
359 IRDA_DEBUG(0, "%s(), CONNECT_REQUEST: media busy!\n",
362 /* Always switch state before calling upper layers */
363 irlap_next_state(self, LAP_NDM);
365 irlap_disconnect_indication(self, LAP_MEDIA_BUSY);
367 irlap_send_snrm_frame(self, &self->qos_rx);
369 /* Start Final-bit timer */
370 irlap_start_final_timer(self, self->final_timeout);
372 self->retry_count = 0;
373 irlap_next_state(self, LAP_SETUP);
377 /* Check if the frame contains and I field */
379 self->daddr = info->daddr;
380 self->caddr = info->caddr;
382 irlap_next_state(self, LAP_CONN);
384 irlap_connect_indication(self, skb);
386 IRDA_DEBUG(0, "%s(), SNRM frame does not "
387 "contain an I field!\n", __FUNCTION__);
390 case DISCOVERY_REQUEST:
391 IRDA_ASSERT(info != NULL, return -1;);
393 if (self->media_busy) {
394 IRDA_DEBUG(1, "%s(), DISCOVERY_REQUEST: media busy!\n",
396 /* irlap->log.condition = MEDIA_BUSY; */
398 /* This will make IrLMP try again */
399 irlap_discovery_confirm(self, NULL);
400 /* Note : the discovery log is not cleaned up here,
401 * it will be done in irlap_discovery_request()
408 irlap_send_discovery_xid_frame(self, info->S, info->s, TRUE,
410 self->frame_sent = FALSE;
413 irlap_start_slot_timer(self, self->slot_timeout);
414 irlap_next_state(self, LAP_QUERY);
416 case RECV_DISCOVERY_XID_CMD:
417 IRDA_ASSERT(info != NULL, return -1;);
419 /* Assert that this is not the final slot */
420 if (info->s <= info->S) {
421 self->slot = irlap_generate_rand_time_slot(info->S,
423 if (self->slot == info->s) {
424 discovery_rsp = irlmp_get_discovery_response();
425 discovery_rsp->data.daddr = info->daddr;
427 irlap_send_discovery_xid_frame(self, info->S,
431 self->frame_sent = TRUE;
433 self->frame_sent = FALSE;
436 * Go to reply state until end of discovery to
437 * inhibit our own transmissions. Set the timer
438 * to not stay forever there... Jean II
440 irlap_start_query_timer(self, info->S, info->s);
441 irlap_next_state(self, LAP_REPLY);
443 /* This is the final slot. How is it possible ?
444 * This would happen is both discoveries are just slightly
445 * offset (if they are in sync, all packets are lost).
446 * Most often, all the discovery requests will be received
447 * in QUERY state (see my comment there), except for the
448 * last frame that will come here.
449 * The big trouble when it happen is that active discovery
450 * doesn't happen, because nobody answer the discoveries
451 * frame of the other guy, so the log shows up empty.
452 * What should we do ?
453 * Not much. It's too late to answer those discovery frames,
454 * so we just pass the info to IrLMP who will put it in the
455 * log (and post an event).
456 * Another cause would be devices that do discovery much
457 * slower than us, however the latest fixes should minimise
461 IRDA_DEBUG(1, "%s(), Receiving final discovery request, missed the discovery slots :-(\n", __FUNCTION__);
463 /* Last discovery request -> in the log */
464 irlap_discovery_indication(self, info->discovery);
467 case MEDIA_BUSY_TIMER_EXPIRED:
468 /* A bunch of events may be postponed because the media is
469 * busy (usually immediately after we close a connection),
470 * or while we are doing discovery (state query/reply).
471 * In all those cases, the media busy flag will be cleared
472 * when it's OK for us to process those postponed events.
473 * This event is not mentioned in the state machines in the
474 * IrLAP spec. It's because they didn't consider Ultra and
475 * postponing connection request is optional.
477 #ifdef CONFIG_IRDA_ULTRA
478 /* Send any pending Ultra frames if any */
479 if (!skb_queue_empty(&self->txq_ultra)) {
480 /* We don't send the frame, just post an event.
481 * Also, previously this code was in timer.c...
483 ret = (*state[self->state])(self, SEND_UI_FRAME,
486 #endif /* CONFIG_IRDA_ULTRA */
487 /* Check if we should try to connect.
488 * This code was previously in irlap_do_event() */
489 if (self->connect_pending) {
490 self->connect_pending = FALSE;
492 /* This one *should* not pend in this state, except
493 * if a socket try to connect and immediately
494 * disconnect. - clear - Jean II */
495 if (self->disconnect_pending)
496 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
498 ret = (*state[self->state])(self,
501 self->disconnect_pending = FALSE;
503 /* Note : one way to test if this code works well (including
504 * media busy and small busy) is to create a user space
505 * application generating an Ultra packet every 3.05 sec (or
506 * 2.95 sec) and to see how it interact with discovery.
507 * It's fairly easy to check that no packet is lost, that the
508 * packets are postponed during discovery and that after
509 * discovery indication you have a 100ms "gap".
510 * As connection request and Ultra are now processed the same
511 * way, this avoid the tedious job of trying IrLAP connection
512 * in all those cases...
515 #ifdef CONFIG_IRDA_ULTRA
519 /* Only allowed to repeat an operation twice */
520 for (i=0; ((i<2) && (self->media_busy == FALSE)); i++) {
521 skb = skb_dequeue(&self->txq_ultra);
523 irlap_send_ui_frame(self, skb, CBROADCAST,
527 /* irlap_send_ui_frame() won't increase skb reference
528 * count, so no dev_kfree_skb() - Jean II */
531 /* Force us to listen 500 ms again */
532 irda_device_set_media_busy(self->netdev, TRUE);
537 /* Only accept broadcast frames in NDM mode */
538 if (info->caddr != CBROADCAST) {
539 IRDA_DEBUG(0, "%s(), not a broadcast frame!\n",
542 irlap_unitdata_indication(self, skb);
544 #endif /* CONFIG_IRDA_ULTRA */
546 /* Remove test frame header */
547 skb_pull(skb, sizeof(struct test_frame));
550 * Send response. This skb will not be sent out again, and
551 * will only be used to send out the same info as the cmd
553 irlap_send_test_frame(self, CBROADCAST, info->daddr, skb);
556 IRDA_DEBUG(0, "%s() not implemented!\n", __FUNCTION__);
559 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
569 * Function irlap_state_query (event, skb, info)
574 static int irlap_state_query(struct irlap_cb *self, IRLAP_EVENT event,
575 struct sk_buff *skb, struct irlap_info *info)
579 IRDA_ASSERT(self != NULL, return -1;);
580 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
583 case RECV_DISCOVERY_XID_RSP:
584 IRDA_ASSERT(info != NULL, return -1;);
585 IRDA_ASSERT(info->discovery != NULL, return -1;);
587 IRDA_DEBUG(4, "%s(), daddr=%08x\n", __FUNCTION__,
588 info->discovery->data.daddr);
590 if (!self->discovery_log) {
591 IRDA_WARNING("%s: discovery log is gone! "
592 "maybe the discovery timeout has been set"
593 " to short?\n", __FUNCTION__);
596 hashbin_insert(self->discovery_log,
597 (irda_queue_t *) info->discovery,
598 info->discovery->data.daddr, NULL);
601 /* irlap_next_state(self, LAP_QUERY); */
604 case RECV_DISCOVERY_XID_CMD:
605 /* Yes, it is possible to receive those frames in this mode.
606 * Note that most often the last discovery request won't
607 * occur here but in NDM state (see my comment there).
608 * What should we do ?
609 * Not much. We are currently performing our own discovery,
610 * therefore we can't answer those frames. We don't want
611 * to change state either. We just pass the info to
612 * IrLMP who will put it in the log (and post an event).
616 IRDA_ASSERT(info != NULL, return -1;);
618 IRDA_DEBUG(1, "%s(), Receiving discovery request (s = %d) while performing discovery :-(\n", __FUNCTION__, info->s);
620 /* Last discovery request ? */
622 irlap_discovery_indication(self, info->discovery);
624 case SLOT_TIMER_EXPIRED:
626 * Wait a little longer if we detect an incoming frame. This
627 * is not mentioned in the spec, but is a good thing to do,
628 * since we want to work even with devices that violate the
629 * timing requirements.
631 if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
632 IRDA_DEBUG(2, "%s(), device is slow to answer, "
633 "waiting some more!\n", __FUNCTION__);
634 irlap_start_slot_timer(self, msecs_to_jiffies(10));
635 self->add_wait = TRUE;
638 self->add_wait = FALSE;
640 if (self->s < self->S) {
641 irlap_send_discovery_xid_frame(self, self->S,
643 self->discovery_cmd);
645 irlap_start_slot_timer(self, self->slot_timeout);
648 irlap_next_state(self, LAP_QUERY);
650 /* This is the final slot! */
651 irlap_send_discovery_xid_frame(self, self->S, 0xff,
653 self->discovery_cmd);
655 /* Always switch state before calling upper layers */
656 irlap_next_state(self, LAP_NDM);
659 * We are now finished with the discovery procedure,
660 * so now we must return the results
662 irlap_discovery_confirm(self, self->discovery_log);
664 /* IrLMP should now have taken care of the log */
665 self->discovery_log = NULL;
669 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
679 * Function irlap_state_reply (self, event, skb, info)
681 * REPLY, we have received a XID discovery frame from a device and we
682 * are waiting for the right time slot to send a response XID frame
685 static int irlap_state_reply(struct irlap_cb *self, IRLAP_EVENT event,
686 struct sk_buff *skb, struct irlap_info *info)
688 discovery_t *discovery_rsp;
691 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
693 IRDA_ASSERT(self != NULL, return -1;);
694 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
697 case QUERY_TIMER_EXPIRED:
698 IRDA_DEBUG(0, "%s(), QUERY_TIMER_EXPIRED <%ld>\n",
699 __FUNCTION__, jiffies);
700 irlap_next_state(self, LAP_NDM);
702 case RECV_DISCOVERY_XID_CMD:
703 IRDA_ASSERT(info != NULL, return -1;);
705 if (info->s == 0xff) {
706 del_timer(&self->query_timer);
708 /* info->log.condition = REMOTE; */
710 /* Always switch state before calling upper layers */
711 irlap_next_state(self, LAP_NDM);
713 irlap_discovery_indication(self, info->discovery);
715 /* If it's our slot, send our reply */
716 if ((info->s >= self->slot) && (!self->frame_sent)) {
717 discovery_rsp = irlmp_get_discovery_response();
718 discovery_rsp->data.daddr = info->daddr;
720 irlap_send_discovery_xid_frame(self, info->S,
725 self->frame_sent = TRUE;
727 /* Readjust our timer to accomodate devices
728 * doing faster or slower discovery than us...
730 irlap_start_query_timer(self, info->S, info->s);
733 //irlap_next_state(self, LAP_REPLY);
737 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __FUNCTION__,
738 event, irlap_event[event]);
747 * Function irlap_state_conn (event, skb, info)
749 * CONN, we have received a SNRM command and is waiting for the upper
750 * layer to accept or refuse connection
753 static int irlap_state_conn(struct irlap_cb *self, IRLAP_EVENT event,
754 struct sk_buff *skb, struct irlap_info *info)
758 IRDA_DEBUG(4, "%s(), event=%s\n", __FUNCTION__, irlap_event[ event]);
760 IRDA_ASSERT(self != NULL, return -1;);
761 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
764 case CONNECT_RESPONSE:
765 skb_pull(skb, sizeof(struct snrm_frame));
767 IRDA_ASSERT(self->netdev != NULL, return -1;);
769 irlap_qos_negotiate(self, skb);
771 irlap_initiate_connection_state(self);
774 * Applying the parameters now will make sure we change speed
775 * *after* we have sent the next frame
777 irlap_apply_connection_parameters(self, FALSE);
780 * Sending this frame will force a speed change after it has
781 * been sent (i.e. the frame will be sent at 9600).
783 irlap_send_ua_response_frame(self, &self->qos_rx);
787 * We are allowed to send two frames, but this may increase
788 * the connect latency, so lets not do it for now.
790 /* This is full of good intentions, but doesn't work in
792 * After sending the first UA response, we switch the
793 * dongle to the negotiated speed, which is usually
794 * different than 9600 kb/s.
795 * From there, there is two solutions :
796 * 1) The other end has received the first UA response :
797 * it will set up the connection, move to state LAP_NRM_P,
798 * and will ignore and drop the second UA response.
799 * Actually, it's even worse : the other side will almost
800 * immediately send a RR that will likely collide with the
801 * UA response (depending on negotiated turnaround).
802 * 2) The other end has not received the first UA response,
803 * will stay at 9600 and will never see the second UA response.
805 irlap_send_ua_response_frame(self, &self->qos_rx);
809 * The WD-timer could be set to the duration of the P-timer
810 * for this case, but it is recommended to use twice the
811 * value (note 3 IrLAP p. 60).
813 irlap_start_wd_timer(self, self->wd_timeout);
814 irlap_next_state(self, LAP_NRM_S);
817 case RECV_DISCOVERY_XID_CMD:
818 IRDA_DEBUG(3, "%s(), event RECV_DISCOVER_XID_CMD!\n",
820 irlap_next_state(self, LAP_NDM);
823 case DISCONNECT_REQUEST:
824 IRDA_DEBUG(0, "%s(), Disconnect request!\n", __FUNCTION__);
825 irlap_send_dm_frame(self);
826 irlap_next_state( self, LAP_NDM);
827 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
830 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __FUNCTION__,
831 event, irlap_event[event]);
841 * Function irlap_state_setup (event, skb, frame)
843 * SETUP state, The local layer has transmitted a SNRM command frame to
844 * a remote peer layer and is awaiting a reply .
847 static int irlap_state_setup(struct irlap_cb *self, IRLAP_EVENT event,
848 struct sk_buff *skb, struct irlap_info *info)
852 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
854 IRDA_ASSERT(self != NULL, return -1;);
855 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
858 case FINAL_TIMER_EXPIRED:
859 if (self->retry_count < self->N3) {
861 * Perform random backoff, Wait a random number of time units, minimum
862 * duration half the time taken to transmitt a SNRM frame, maximum duration
863 * 1.5 times the time taken to transmit a SNRM frame. So this time should
864 * between 15 msecs and 45 msecs.
866 irlap_start_backoff_timer(self, msecs_to_jiffies(20 +
869 /* Always switch state before calling upper layers */
870 irlap_next_state(self, LAP_NDM);
872 irlap_disconnect_indication(self, LAP_FOUND_NONE);
875 case BACKOFF_TIMER_EXPIRED:
876 irlap_send_snrm_frame(self, &self->qos_rx);
877 irlap_start_final_timer(self, self->final_timeout);
881 IRDA_DEBUG(4, "%s(), SNRM battle!\n", __FUNCTION__);
883 IRDA_ASSERT(skb != NULL, return 0;);
884 IRDA_ASSERT(info != NULL, return 0;);
887 * The device with the largest device address wins the battle
888 * (both have sent a SNRM command!)
890 if (info &&(info->daddr > self->saddr)) {
891 del_timer(&self->final_timer);
892 irlap_initiate_connection_state(self);
894 IRDA_ASSERT(self->netdev != NULL, return -1;);
896 skb_pull(skb, sizeof(struct snrm_frame));
898 irlap_qos_negotiate(self, skb);
900 /* Send UA frame and then change link settings */
901 irlap_apply_connection_parameters(self, FALSE);
902 irlap_send_ua_response_frame(self, &self->qos_rx);
904 irlap_next_state(self, LAP_NRM_S);
905 irlap_connect_confirm(self, skb);
908 * The WD-timer could be set to the duration of the
909 * P-timer for this case, but it is recommended
910 * to use twice the value (note 3 IrLAP p. 60).
912 irlap_start_wd_timer(self, self->wd_timeout);
914 /* We just ignore the other device! */
915 irlap_next_state(self, LAP_SETUP);
920 del_timer(&self->final_timer);
922 /* Initiate connection state */
923 irlap_initiate_connection_state(self);
925 /* Negotiate connection parameters */
926 IRDA_ASSERT(skb->len > 10, return -1;);
928 skb_pull(skb, sizeof(struct ua_frame));
930 IRDA_ASSERT(self->netdev != NULL, return -1;);
932 irlap_qos_negotiate(self, skb);
934 /* Set the new link setting *now* (before the rr frame) */
935 irlap_apply_connection_parameters(self, TRUE);
936 self->retry_count = 0;
938 /* Wait for turnaround time to give a chance to the other
939 * device to be ready to receive us.
940 * Note : the time to switch speed is typically larger
941 * than the turnaround time, but as we don't have the other
942 * side speed switch time, that's our best guess...
944 irlap_wait_min_turn_around(self, &self->qos_tx);
946 /* This frame will actually be sent at the new speed */
947 irlap_send_rr_frame(self, CMD_FRAME);
949 /* The timer is set to half the normal timer to quickly
950 * detect a failure to negociate the new connection
951 * parameters. IrLAP 6.11.3.2, note 3.
952 * Note that currently we don't process this failure
953 * properly, as we should do a quick disconnect.
955 irlap_start_final_timer(self, self->final_timeout/2);
956 irlap_next_state(self, LAP_NRM_P);
958 irlap_connect_confirm(self, skb);
960 case RECV_DM_RSP: /* FALLTHROUGH */
962 del_timer(&self->final_timer);
963 irlap_next_state(self, LAP_NDM);
965 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
968 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __FUNCTION__,
969 event, irlap_event[event]);
978 * Function irlap_state_offline (self, event, skb, info)
980 * OFFLINE state, not used for now!
983 static int irlap_state_offline(struct irlap_cb *self, IRLAP_EVENT event,
984 struct sk_buff *skb, struct irlap_info *info)
986 IRDA_DEBUG( 0, "%s(), Unknown event\n", __FUNCTION__);
992 * Function irlap_state_xmit_p (self, event, skb, info)
994 * XMIT, Only the primary station has right to transmit, and we
995 * therefore do not expect to receive any transmissions from other
999 static int irlap_state_xmit_p(struct irlap_cb *self, IRLAP_EVENT event,
1000 struct sk_buff *skb, struct irlap_info *info)
1007 * Only send frame if send-window > 0.
1009 if ((self->window > 0) && (!self->remote_busy)) {
1011 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1012 struct sk_buff *skb_next;
1014 /* With DYNAMIC_WINDOW, we keep the window size
1015 * maximum, and adapt on the packets we are sending.
1016 * At 115k, we can send only 2 packets of 2048 bytes
1017 * in a 500 ms turnaround. Without this option, we
1018 * would always limit the window to 2. With this
1019 * option, if we send smaller packets, we can send
1020 * up to 7 of them (always depending on QoS).
1023 /* Look at the next skb. This is safe, as we are
1024 * the only consumer of the Tx queue (if we are not,
1025 * we have other problems) - Jean II */
1026 skb_next = skb_peek(&self->txq);
1028 /* Check if a subsequent skb exist and would fit in
1029 * the current window (with respect to turnaround
1031 * This allow us to properly mark the current packet
1032 * with the pf bit, to avoid falling back on the
1033 * second test below, and avoid waiting the
1034 * end of the window and sending a extra RR.
1035 * Note : (skb_next != NULL) <=> (skb_queue_len() > 0)
1037 nextfit = ((skb_next != NULL) &&
1038 ((skb_next->len + skb->len) <=
1042 * The current packet may not fit ! Because of test
1043 * above, this should not happen any more !!!
1044 * Test if we have transmitted more bytes over the
1045 * link than its possible to do with the current
1046 * speed and turn-around-time.
1048 if((!nextfit) && (skb->len > self->bytes_left)) {
1049 IRDA_DEBUG(0, "%s(), Not allowed to transmit"
1050 " more bytes!\n", __FUNCTION__);
1051 /* Requeue the skb */
1052 skb_queue_head(&self->txq, skb_get(skb));
1054 * We should switch state to LAP_NRM_P, but
1055 * that is not possible since we must be sure
1056 * that we poll the other side. Since we have
1057 * used up our time, the poll timer should
1058 * trigger anyway now, so we just wait for it
1062 * Sorry, but that's not totally true. If
1063 * we send 2000B packets, we may wait another
1064 * 1000B until our turnaround expire. That's
1065 * why we need to be proactive in avoiding
1066 * coming here. - Jean II
1071 /* Substract space used by this skb */
1072 self->bytes_left -= skb->len;
1073 #else /* CONFIG_IRDA_DYNAMIC_WINDOW */
1074 /* Window has been adjusted for the max packet
1075 * size, so much simpler... - Jean II */
1076 nextfit = !skb_queue_empty(&self->txq);
1077 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1079 * Send data with poll bit cleared only if window > 1
1080 * and there is more frames after this one to be sent
1082 if ((self->window > 1) && (nextfit)) {
1083 /* More packet to send in current window */
1084 irlap_send_data_primary(self, skb);
1085 irlap_next_state(self, LAP_XMIT_P);
1087 /* Final packet of window */
1088 irlap_send_data_primary_poll(self, skb);
1089 irlap_next_state(self, LAP_NRM_P);
1092 * Make sure state machine does not try to send
1097 #ifdef CONFIG_IRDA_FAST_RR
1098 /* Peer may want to reply immediately */
1099 self->fast_RR = FALSE;
1100 #endif /* CONFIG_IRDA_FAST_RR */
1102 IRDA_DEBUG(4, "%s(), Unable to send! remote busy?\n",
1104 skb_queue_head(&self->txq, skb_get(skb));
1107 * The next ret is important, because it tells
1108 * irlap_next_state _not_ to deliver more frames
1113 case POLL_TIMER_EXPIRED:
1114 IRDA_DEBUG(3, "%s(), POLL_TIMER_EXPIRED <%ld>\n",
1115 __FUNCTION__, jiffies);
1116 irlap_send_rr_frame(self, CMD_FRAME);
1117 /* Return to NRM properly - Jean II */
1118 self->window = self->window_size;
1119 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1120 /* Allowed to transmit a maximum number of bytes again. */
1121 self->bytes_left = self->line_capacity;
1122 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1123 irlap_start_final_timer(self, self->final_timeout);
1124 irlap_next_state(self, LAP_NRM_P);
1126 case DISCONNECT_REQUEST:
1127 del_timer(&self->poll_timer);
1128 irlap_wait_min_turn_around(self, &self->qos_tx);
1129 irlap_send_disc_frame(self);
1130 irlap_flush_all_queues(self);
1131 irlap_start_final_timer(self, self->final_timeout);
1132 self->retry_count = 0;
1133 irlap_next_state(self, LAP_PCLOSE);
1136 /* Nothing to do, irlap_do_event() will send the packet
1137 * when we return... - Jean II */
1140 IRDA_DEBUG(0, "%s(), Unknown event %s\n",
1141 __FUNCTION__, irlap_event[event]);
1150 * Function irlap_state_pclose (event, skb, info)
1154 static int irlap_state_pclose(struct irlap_cb *self, IRLAP_EVENT event,
1155 struct sk_buff *skb, struct irlap_info *info)
1159 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1161 IRDA_ASSERT(self != NULL, return -1;);
1162 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
1165 case RECV_UA_RSP: /* FALLTHROUGH */
1167 del_timer(&self->final_timer);
1169 /* Set new link parameters */
1170 irlap_apply_default_connection_parameters(self);
1172 /* Always switch state before calling upper layers */
1173 irlap_next_state(self, LAP_NDM);
1175 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
1177 case FINAL_TIMER_EXPIRED:
1178 if (self->retry_count < self->N3) {
1179 irlap_wait_min_turn_around(self, &self->qos_tx);
1180 irlap_send_disc_frame(self);
1181 irlap_start_final_timer(self, self->final_timeout);
1182 self->retry_count++;
1185 irlap_apply_default_connection_parameters(self);
1187 /* Always switch state before calling upper layers */
1188 irlap_next_state(self, LAP_NDM);
1190 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1194 IRDA_DEBUG(1, "%s(), Unknown event %d\n", __FUNCTION__, event);
1203 * Function irlap_state_nrm_p (self, event, skb, info)
1205 * NRM_P (Normal Response Mode as Primary), The primary station has given
1206 * permissions to a secondary station to transmit IrLAP resonse frames
1207 * (by sending a frame with the P bit set). The primary station will not
1208 * transmit any frames and is expecting to receive frames only from the
1209 * secondary to which transmission permissions has been given.
1211 static int irlap_state_nrm_p(struct irlap_cb *self, IRLAP_EVENT event,
1212 struct sk_buff *skb, struct irlap_info *info)
1219 case RECV_I_RSP: /* Optimize for the common case */
1220 /* FIXME: must check for remote_busy below */
1221 #ifdef CONFIG_IRDA_FAST_RR
1223 * Reset the fast_RR so we can use the fast RR code with
1224 * full speed the next time since peer may have more frames
1227 self->fast_RR = FALSE;
1228 #endif /* CONFIG_IRDA_FAST_RR */
1229 IRDA_ASSERT( info != NULL, return -1;);
1231 ns_status = irlap_validate_ns_received(self, info->ns);
1232 nr_status = irlap_validate_nr_received(self, info->nr);
1235 * Check for expected I(nformation) frame
1237 if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
1239 /* Update Vr (next frame for us to receive) */
1240 self->vr = (self->vr + 1) % 8;
1242 /* Update Nr received, cleanup our retry queue */
1243 irlap_update_nr_received(self, info->nr);
1246 * Got expected NR, so reset the
1247 * retry_count. This is not done by IrLAP spec,
1250 self->retry_count = 0;
1251 self->ack_required = TRUE;
1253 /* poll bit cleared? */
1255 /* Keep state, do not move this line */
1256 irlap_next_state(self, LAP_NRM_P);
1258 irlap_data_indication(self, skb, FALSE);
1260 /* No longer waiting for pf */
1261 del_timer(&self->final_timer);
1263 irlap_wait_min_turn_around(self, &self->qos_tx);
1265 /* Call higher layer *before* changing state
1266 * to give them a chance to send data in the
1269 irlap_data_indication(self, skb, FALSE);
1271 /* XMIT states are the most dangerous state
1272 * to be in, because user requests are
1273 * processed directly and may change state.
1274 * On the other hand, in NDM_P, those
1275 * requests are queued and we will process
1276 * them when we return to irlap_do_event().
1279 irlap_next_state(self, LAP_XMIT_P);
1281 /* This is the last frame.
1282 * Make sure it's always called in XMIT state.
1284 irlap_start_poll_timer(self, self->poll_timeout);
1289 /* Unexpected next to send (Ns) */
1290 if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
1293 irlap_update_nr_received(self, info->nr);
1296 * Wait until the last frame before doing
1301 irlap_next_state(self, LAP_NRM_P);
1304 "%s(), missing or duplicate frame!\n",
1307 /* Update Nr received */
1308 irlap_update_nr_received(self, info->nr);
1310 irlap_wait_min_turn_around(self, &self->qos_tx);
1311 irlap_send_rr_frame(self, CMD_FRAME);
1313 self->ack_required = FALSE;
1315 irlap_start_final_timer(self, self->final_timeout);
1316 irlap_next_state(self, LAP_NRM_P);
1321 * Unexpected next to receive (Nr)
1323 if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
1326 self->vr = (self->vr + 1) % 8;
1328 /* Update Nr received */
1329 irlap_update_nr_received(self, info->nr);
1331 /* Resend rejected frames */
1332 irlap_resend_rejected_frames(self, CMD_FRAME);
1334 self->ack_required = FALSE;
1336 /* Make sure we account for the time
1337 * to transmit our frames. See comemnts
1338 * in irlap_send_data_primary_poll().
1340 irlap_start_final_timer(self, 2 * self->final_timeout);
1342 /* Keep state, do not move this line */
1343 irlap_next_state(self, LAP_NRM_P);
1345 irlap_data_indication(self, skb, FALSE);
1348 * Do not resend frames until the last
1349 * frame has arrived from the other
1350 * device. This is not documented in
1353 self->vr = (self->vr + 1) % 8;
1355 /* Update Nr received */
1356 irlap_update_nr_received(self, info->nr);
1358 self->ack_required = FALSE;
1360 /* Keep state, do not move this line!*/
1361 irlap_next_state(self, LAP_NRM_P);
1363 irlap_data_indication(self, skb, FALSE);
1368 * Unexpected next to send (Ns) and next to receive (Nr)
1369 * Not documented by IrLAP!
1371 if ((ns_status == NS_UNEXPECTED) &&
1372 (nr_status == NR_UNEXPECTED))
1374 IRDA_DEBUG(4, "%s(), unexpected nr and ns!\n",
1377 /* Resend rejected frames */
1378 irlap_resend_rejected_frames(self, CMD_FRAME);
1380 /* Give peer some time to retransmit!
1381 * But account for our own Tx. */
1382 irlap_start_final_timer(self, 2 * self->final_timeout);
1384 /* Keep state, do not move this line */
1385 irlap_next_state(self, LAP_NRM_P);
1387 /* Update Nr received */
1388 /* irlap_update_nr_received( info->nr); */
1390 self->ack_required = FALSE;
1398 if ((nr_status == NR_INVALID) || (ns_status == NS_INVALID)) {
1400 del_timer(&self->final_timer);
1402 irlap_next_state(self, LAP_RESET_WAIT);
1404 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1405 self->xmitflag = TRUE;
1407 del_timer(&self->final_timer);
1409 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1411 self->xmitflag = FALSE;
1415 IRDA_DEBUG(1, "%s(), Not implemented!\n", __FUNCTION__);
1416 IRDA_DEBUG(1, "%s(), event=%s, ns_status=%d, nr_status=%d\n",
1417 __FUNCTION__, irlap_event[event], ns_status, nr_status);
1420 /* Poll bit cleared? */
1422 irlap_data_indication(self, skb, TRUE);
1423 irlap_next_state(self, LAP_NRM_P);
1425 del_timer(&self->final_timer);
1426 irlap_data_indication(self, skb, TRUE);
1427 irlap_next_state(self, LAP_XMIT_P);
1428 IRDA_DEBUG(1, "%s: RECV_UI_FRAME: next state %s\n", __FUNCTION__, irlap_state[self->state]);
1429 irlap_start_poll_timer(self, self->poll_timeout);
1434 * If you get a RR, the remote isn't busy anymore,
1435 * no matter what the NR
1437 self->remote_busy = FALSE;
1442 ret = irlap_validate_nr_received(self, info->nr);
1443 if (ret == NR_EXPECTED) {
1444 /* Stop final timer */
1445 del_timer(&self->final_timer);
1447 /* Update Nr received */
1448 irlap_update_nr_received(self, info->nr);
1451 * Got expected NR, so reset the retry_count. This
1452 * is not done by the IrLAP standard , which is
1455 self->retry_count = 0;
1456 irlap_wait_min_turn_around(self, &self->qos_tx);
1458 irlap_next_state(self, LAP_XMIT_P);
1460 /* Start poll timer */
1461 irlap_start_poll_timer(self, self->poll_timeout);
1462 } else if (ret == NR_UNEXPECTED) {
1463 IRDA_ASSERT(info != NULL, return -1;);
1468 /* Update Nr received */
1469 irlap_update_nr_received(self, info->nr);
1471 IRDA_DEBUG(4, "RECV_RR_FRAME: Retrans:%d, nr=%d, va=%d, "
1473 self->retry_count, info->nr, self->va,
1474 self->vs, self->vr);
1476 /* Resend rejected frames */
1477 irlap_resend_rejected_frames(self, CMD_FRAME);
1479 /* Final timer ??? Jean II */
1481 irlap_next_state(self, LAP_NRM_P);
1482 } else if (ret == NR_INVALID) {
1483 IRDA_DEBUG(1, "%s(), Received RR with "
1484 "invalid nr !\n", __FUNCTION__);
1485 del_timer(&self->final_timer);
1487 irlap_next_state(self, LAP_RESET_WAIT);
1489 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1490 self->xmitflag = TRUE;
1494 IRDA_ASSERT(info != NULL, return -1;);
1496 /* Stop final timer */
1497 del_timer(&self->final_timer);
1498 self->remote_busy = TRUE;
1500 /* Update Nr received */
1501 irlap_update_nr_received(self, info->nr);
1502 irlap_next_state(self, LAP_XMIT_P);
1504 /* Start poll timer */
1505 irlap_start_poll_timer(self, self->poll_timeout);
1508 del_timer(&self->final_timer);
1509 self->xmitflag = TRUE;
1510 irlap_next_state(self, LAP_RESET_WAIT);
1511 irlap_reset_indication(self);
1513 case FINAL_TIMER_EXPIRED:
1515 * We are allowed to wait for additional 300 ms if
1516 * final timer expires when we are in the middle
1517 * of receiving a frame (page 45, IrLAP). Check that
1518 * we only do this once for each frame.
1520 if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
1521 IRDA_DEBUG(1, "FINAL_TIMER_EXPIRED when receiving a "
1522 "frame! Waiting a little bit more!\n");
1523 irlap_start_final_timer(self, msecs_to_jiffies(300));
1526 * Don't allow this to happen one more time in a row,
1527 * or else we can get a pretty tight loop here if
1528 * if we only receive half a frame. DB.
1530 self->add_wait = TRUE;
1533 self->add_wait = FALSE;
1535 /* N2 is the disconnect timer. Until we reach it, we retry */
1536 if (self->retry_count < self->N2) {
1537 /* Retry sending the pf bit to the secondary */
1538 irlap_wait_min_turn_around(self, &self->qos_tx);
1539 irlap_send_rr_frame(self, CMD_FRAME);
1541 irlap_start_final_timer(self, self->final_timeout);
1542 self->retry_count++;
1543 IRDA_DEBUG(4, "irlap_state_nrm_p: FINAL_TIMER_EXPIRED:"
1544 " retry_count=%d\n", self->retry_count);
1546 /* Early warning event. I'm using a pretty liberal
1547 * interpretation of the spec and generate an event
1548 * every time the timer is multiple of N1 (and not
1549 * only the first time). This allow application
1550 * to know precisely if connectivity restart...
1552 if((self->retry_count % self->N1) == 0)
1553 irlap_status_indication(self,
1554 STATUS_NO_ACTIVITY);
1558 irlap_apply_default_connection_parameters(self);
1560 /* Always switch state before calling upper layers */
1561 irlap_next_state(self, LAP_NDM);
1562 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1566 irlap_update_nr_received(self, info->nr);
1567 if (self->remote_busy) {
1568 irlap_wait_min_turn_around(self, &self->qos_tx);
1569 irlap_send_rr_frame(self, CMD_FRAME);
1571 irlap_resend_rejected_frames(self, CMD_FRAME);
1572 irlap_start_final_timer(self, 2 * self->final_timeout);
1575 irlap_update_nr_received(self, info->nr);
1576 if (self->remote_busy) {
1577 irlap_wait_min_turn_around(self, &self->qos_tx);
1578 irlap_send_rr_frame(self, CMD_FRAME);
1580 irlap_resend_rejected_frame(self, CMD_FRAME);
1581 irlap_start_final_timer(self, 2 * self->final_timeout);
1584 IRDA_DEBUG(1, "%s(), RECV_RD_RSP\n", __FUNCTION__);
1586 irlap_flush_all_queues(self);
1587 irlap_next_state(self, LAP_XMIT_P);
1588 /* Call back the LAP state machine to do a proper disconnect */
1589 irlap_disconnect_request(self);
1592 IRDA_DEBUG(1, "%s(), Unknown event %s\n",
1593 __FUNCTION__, irlap_event[event]);
1602 * Function irlap_state_reset_wait (event, skb, info)
1604 * We have informed the service user of a reset condition, and is
1605 * awaiting reset of disconnect request.
1608 static int irlap_state_reset_wait(struct irlap_cb *self, IRLAP_EVENT event,
1609 struct sk_buff *skb, struct irlap_info *info)
1613 IRDA_DEBUG(3, "%s(), event = %s\n", __FUNCTION__, irlap_event[event]);
1615 IRDA_ASSERT(self != NULL, return -1;);
1616 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
1620 if (self->xmitflag) {
1621 irlap_wait_min_turn_around(self, &self->qos_tx);
1622 irlap_send_snrm_frame(self, NULL);
1623 irlap_start_final_timer(self, self->final_timeout);
1624 irlap_next_state(self, LAP_RESET);
1626 irlap_start_final_timer(self, self->final_timeout);
1627 irlap_next_state(self, LAP_RESET);
1630 case DISCONNECT_REQUEST:
1631 irlap_wait_min_turn_around( self, &self->qos_tx);
1632 irlap_send_disc_frame( self);
1633 irlap_flush_all_queues( self);
1634 irlap_start_final_timer( self, self->final_timeout);
1635 self->retry_count = 0;
1636 irlap_next_state( self, LAP_PCLOSE);
1639 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
1640 irlap_event[event]);
1649 * Function irlap_state_reset (self, event, skb, info)
1651 * We have sent a SNRM reset command to the peer layer, and is awaiting
1655 static int irlap_state_reset(struct irlap_cb *self, IRLAP_EVENT event,
1656 struct sk_buff *skb, struct irlap_info *info)
1660 IRDA_DEBUG(3, "%s(), event = %s\n", __FUNCTION__, irlap_event[event]);
1662 IRDA_ASSERT(self != NULL, return -1;);
1663 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
1667 del_timer(&self->final_timer);
1669 irlap_apply_default_connection_parameters(self);
1671 /* Always switch state before calling upper layers */
1672 irlap_next_state(self, LAP_NDM);
1674 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1678 del_timer(&self->final_timer);
1680 /* Initiate connection state */
1681 irlap_initiate_connection_state(self);
1683 irlap_reset_confirm();
1685 self->remote_busy = FALSE;
1687 irlap_next_state(self, LAP_XMIT_P);
1689 irlap_start_poll_timer(self, self->poll_timeout);
1692 case FINAL_TIMER_EXPIRED:
1693 if (self->retry_count < 3) {
1694 irlap_wait_min_turn_around(self, &self->qos_tx);
1696 IRDA_ASSERT(self->netdev != NULL, return -1;);
1697 irlap_send_snrm_frame(self, self->qos_dev);
1699 self->retry_count++; /* Experimental!! */
1701 irlap_start_final_timer(self, self->final_timeout);
1702 irlap_next_state(self, LAP_RESET);
1703 } else if (self->retry_count >= self->N3) {
1704 irlap_apply_default_connection_parameters(self);
1706 /* Always switch state before calling upper layers */
1707 irlap_next_state(self, LAP_NDM);
1709 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1714 * SNRM frame is not allowed to contain an I-field in this
1718 IRDA_DEBUG(3, "%s(), RECV_SNRM_CMD\n", __FUNCTION__);
1719 irlap_initiate_connection_state(self);
1720 irlap_wait_min_turn_around(self, &self->qos_tx);
1721 irlap_send_ua_response_frame(self, &self->qos_rx);
1722 irlap_reset_confirm();
1723 irlap_start_wd_timer(self, self->wd_timeout);
1724 irlap_next_state(self, LAP_NDM);
1727 "%s(), SNRM frame contained an I field!\n",
1732 IRDA_DEBUG(1, "%s(), Unknown event %s\n",
1733 __FUNCTION__, irlap_event[event]);
1742 * Function irlap_state_xmit_s (event, skb, info)
1744 * XMIT_S, The secondary station has been given the right to transmit,
1745 * and we therefor do not expect to receive any transmissions from other
1748 static int irlap_state_xmit_s(struct irlap_cb *self, IRLAP_EVENT event,
1749 struct sk_buff *skb, struct irlap_info *info)
1753 IRDA_DEBUG(4, "%s(), event=%s\n", __FUNCTION__, irlap_event[event]);
1755 IRDA_ASSERT(self != NULL, return -ENODEV;);
1756 IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
1761 * Send frame only if send window > 0
1763 if ((self->window > 0) && (!self->remote_busy)) {
1765 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1766 struct sk_buff *skb_next;
1769 * Same deal as in irlap_state_xmit_p(), so see
1770 * the comments at that point.
1771 * We are the secondary, so there are only subtle
1772 * differences. - Jean II
1775 /* Check if a subsequent skb exist and would fit in
1776 * the current window (with respect to turnaround
1777 * time). - Jean II */
1778 skb_next = skb_peek(&self->txq);
1779 nextfit = ((skb_next != NULL) &&
1780 ((skb_next->len + skb->len) <=
1784 * Test if we have transmitted more bytes over the
1785 * link than its possible to do with the current
1786 * speed and turn-around-time.
1788 if((!nextfit) && (skb->len > self->bytes_left)) {
1789 IRDA_DEBUG(0, "%s(), Not allowed to transmit"
1790 " more bytes!\n", __FUNCTION__);
1791 /* Requeue the skb */
1792 skb_queue_head(&self->txq, skb_get(skb));
1795 * Switch to NRM_S, this is only possible
1796 * when we are in secondary mode, since we
1797 * must be sure that we don't miss any RR
1800 self->window = self->window_size;
1801 self->bytes_left = self->line_capacity;
1802 irlap_start_wd_timer(self, self->wd_timeout);
1804 irlap_next_state(self, LAP_NRM_S);
1805 /* Slight difference with primary :
1806 * here we would wait for the other side to
1807 * expire the turnaround. - Jean II */
1809 return -EPROTO; /* Try again later */
1811 /* Substract space used by this skb */
1812 self->bytes_left -= skb->len;
1813 #else /* CONFIG_IRDA_DYNAMIC_WINDOW */
1814 /* Window has been adjusted for the max packet
1815 * size, so much simpler... - Jean II */
1816 nextfit = !skb_queue_empty(&self->txq);
1817 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1819 * Send data with final bit cleared only if window > 1
1820 * and there is more frames to be sent
1822 if ((self->window > 1) && (nextfit)) {
1823 irlap_send_data_secondary(self, skb);
1824 irlap_next_state(self, LAP_XMIT_S);
1826 irlap_send_data_secondary_final(self, skb);
1827 irlap_next_state(self, LAP_NRM_S);
1830 * Make sure state machine does not try to send
1836 IRDA_DEBUG(2, "%s(), Unable to send!\n", __FUNCTION__);
1837 skb_queue_head(&self->txq, skb_get(skb));
1841 case DISCONNECT_REQUEST:
1842 irlap_send_rd_frame(self);
1843 irlap_flush_all_queues(self);
1844 irlap_start_wd_timer(self, self->wd_timeout);
1845 irlap_next_state(self, LAP_SCLOSE);
1848 /* Nothing to do, irlap_do_event() will send the packet
1849 * when we return... - Jean II */
1852 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
1853 irlap_event[event]);
1862 * Function irlap_state_nrm_s (event, skb, info)
1864 * NRM_S (Normal Response Mode as Secondary) state, in this state we are
1865 * expecting to receive frames from the primary station
1868 static int irlap_state_nrm_s(struct irlap_cb *self, IRLAP_EVENT event,
1869 struct sk_buff *skb, struct irlap_info *info)
1875 IRDA_DEBUG(4, "%s(), event=%s\n", __FUNCTION__, irlap_event[ event]);
1877 IRDA_ASSERT(self != NULL, return -1;);
1878 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
1881 case RECV_I_CMD: /* Optimize for the common case */
1882 /* FIXME: must check for remote_busy below */
1883 IRDA_DEBUG(4, "%s(), event=%s nr=%d, vs=%d, ns=%d, "
1884 "vr=%d, pf=%d\n", __FUNCTION__,
1885 irlap_event[event], info->nr,
1886 self->vs, info->ns, self->vr, info->pf);
1888 self->retry_count = 0;
1890 ns_status = irlap_validate_ns_received(self, info->ns);
1891 nr_status = irlap_validate_nr_received(self, info->nr);
1893 * Check for expected I(nformation) frame
1895 if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
1897 /* Update Vr (next frame for us to receive) */
1898 self->vr = (self->vr + 1) % 8;
1900 /* Update Nr received */
1901 irlap_update_nr_received(self, info->nr);
1908 self->ack_required = TRUE;
1911 * Starting WD-timer here is optional, but
1912 * not recommended. Note 6 IrLAP p. 83
1915 irda_start_timer(WD_TIMER, self->wd_timeout);
1917 /* Keep state, do not move this line */
1918 irlap_next_state(self, LAP_NRM_S);
1920 irlap_data_indication(self, skb, FALSE);
1924 * We should wait before sending RR, and
1925 * also before changing to XMIT_S
1926 * state. (note 1, IrLAP p. 82)
1928 irlap_wait_min_turn_around(self, &self->qos_tx);
1931 * Give higher layers a chance to
1932 * immediately reply with some data before
1933 * we decide if we should send a RR frame
1936 irlap_data_indication(self, skb, FALSE);
1938 /* Any pending data requests? */
1939 if (!skb_queue_empty(&self->txq) &&
1942 self->ack_required = TRUE;
1944 del_timer(&self->wd_timer);
1946 irlap_next_state(self, LAP_XMIT_S);
1948 irlap_send_rr_frame(self, RSP_FRAME);
1949 irlap_start_wd_timer(self,
1952 /* Keep the state */
1953 irlap_next_state(self, LAP_NRM_S);
1959 * Check for Unexpected next to send (Ns)
1961 if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
1963 /* Unexpected next to send, with final bit cleared */
1965 irlap_update_nr_received(self, info->nr);
1967 irlap_start_wd_timer(self, self->wd_timeout);
1969 /* Update Nr received */
1970 irlap_update_nr_received(self, info->nr);
1972 irlap_wait_min_turn_around(self, &self->qos_tx);
1973 irlap_send_rr_frame(self, RSP_FRAME);
1975 irlap_start_wd_timer(self, self->wd_timeout);
1981 * Unexpected Next to Receive(NR) ?
1983 if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
1986 IRDA_DEBUG(4, "RECV_I_RSP: frame(s) lost\n");
1988 self->vr = (self->vr + 1) % 8;
1990 /* Update Nr received */
1991 irlap_update_nr_received(self, info->nr);
1993 /* Resend rejected frames */
1994 irlap_resend_rejected_frames(self, RSP_FRAME);
1996 /* Keep state, do not move this line */
1997 irlap_next_state(self, LAP_NRM_S);
1999 irlap_data_indication(self, skb, FALSE);
2000 irlap_start_wd_timer(self, self->wd_timeout);
2004 * This is not documented in IrLAP!! Unexpected NR
2005 * with poll bit cleared
2008 self->vr = (self->vr + 1) % 8;
2010 /* Update Nr received */
2011 irlap_update_nr_received(self, info->nr);
2013 /* Keep state, do not move this line */
2014 irlap_next_state(self, LAP_NRM_S);
2016 irlap_data_indication(self, skb, FALSE);
2017 irlap_start_wd_timer(self, self->wd_timeout);
2022 if (ret == NR_INVALID) {
2023 IRDA_DEBUG(0, "NRM_S, NR_INVALID not implemented!\n");
2025 if (ret == NS_INVALID) {
2026 IRDA_DEBUG(0, "NRM_S, NS_INVALID not implemented!\n");
2034 irlap_data_indication(self, skb, TRUE);
2035 irlap_next_state(self, LAP_NRM_S); /* Keep state */
2038 * Any pending data requests?
2040 if (!skb_queue_empty(&self->txq) &&
2041 (self->window > 0) && !self->remote_busy)
2043 irlap_data_indication(self, skb, TRUE);
2045 del_timer(&self->wd_timer);
2047 irlap_next_state(self, LAP_XMIT_S);
2049 irlap_data_indication(self, skb, TRUE);
2051 irlap_wait_min_turn_around(self, &self->qos_tx);
2053 irlap_send_rr_frame(self, RSP_FRAME);
2054 self->ack_required = FALSE;
2056 irlap_start_wd_timer(self, self->wd_timeout);
2058 /* Keep the state */
2059 irlap_next_state(self, LAP_NRM_S);
2064 self->retry_count = 0;
2069 nr_status = irlap_validate_nr_received(self, info->nr);
2070 if (nr_status == NR_EXPECTED) {
2071 if (!skb_queue_empty(&self->txq) &&
2072 (self->window > 0)) {
2073 self->remote_busy = FALSE;
2075 /* Update Nr received */
2076 irlap_update_nr_received(self, info->nr);
2077 del_timer(&self->wd_timer);
2079 irlap_wait_min_turn_around(self, &self->qos_tx);
2080 irlap_next_state(self, LAP_XMIT_S);
2082 self->remote_busy = FALSE;
2083 /* Update Nr received */
2084 irlap_update_nr_received(self, info->nr);
2085 irlap_wait_min_turn_around(self, &self->qos_tx);
2086 irlap_start_wd_timer(self, self->wd_timeout);
2088 /* Note : if the link is idle (this case),
2089 * we never go in XMIT_S, so we never get a
2090 * chance to process any DISCONNECT_REQUEST.
2091 * Do it now ! - Jean II */
2092 if (self->disconnect_pending) {
2094 irlap_send_rd_frame(self);
2095 irlap_flush_all_queues(self);
2097 irlap_next_state(self, LAP_SCLOSE);
2099 /* Just send back pf bit */
2100 irlap_send_rr_frame(self, RSP_FRAME);
2102 irlap_next_state(self, LAP_NRM_S);
2105 } else if (nr_status == NR_UNEXPECTED) {
2106 self->remote_busy = FALSE;
2107 irlap_update_nr_received(self, info->nr);
2108 irlap_resend_rejected_frames(self, RSP_FRAME);
2110 irlap_start_wd_timer(self, self->wd_timeout);
2113 irlap_next_state(self, LAP_NRM_S);
2115 IRDA_DEBUG(1, "%s(), invalid nr not implemented!\n",
2120 /* SNRM frame is not allowed to contain an I-field */
2122 del_timer(&self->wd_timer);
2123 IRDA_DEBUG(1, "%s(), received SNRM cmd\n", __FUNCTION__);
2124 irlap_next_state(self, LAP_RESET_CHECK);
2126 irlap_reset_indication(self);
2129 "%s(), SNRM frame contained an I-field!\n",
2135 irlap_update_nr_received(self, info->nr);
2136 if (self->remote_busy) {
2137 irlap_wait_min_turn_around(self, &self->qos_tx);
2138 irlap_send_rr_frame(self, RSP_FRAME);
2140 irlap_resend_rejected_frames(self, RSP_FRAME);
2141 irlap_start_wd_timer(self, self->wd_timeout);
2144 irlap_update_nr_received(self, info->nr);
2145 if (self->remote_busy) {
2146 irlap_wait_min_turn_around(self, &self->qos_tx);
2147 irlap_send_rr_frame(self, RSP_FRAME);
2149 irlap_resend_rejected_frame(self, RSP_FRAME);
2150 irlap_start_wd_timer(self, self->wd_timeout);
2152 case WD_TIMER_EXPIRED:
2154 * Wait until retry_count * n matches negotiated threshold/
2155 * disconnect time (note 2 in IrLAP p. 82)
2157 * Similar to irlap_state_nrm_p() -> FINAL_TIMER_EXPIRED
2158 * Note : self->wd_timeout = (self->final_timeout * 2),
2159 * which explain why we use (self->N2 / 2) here !!!
2162 IRDA_DEBUG(1, "%s(), retry_count = %d\n", __FUNCTION__,
2165 if (self->retry_count < (self->N2 / 2)) {
2166 /* No retry, just wait for primary */
2167 irlap_start_wd_timer(self, self->wd_timeout);
2168 self->retry_count++;
2170 if((self->retry_count % (self->N1 / 2)) == 0)
2171 irlap_status_indication(self,
2172 STATUS_NO_ACTIVITY);
2174 irlap_apply_default_connection_parameters(self);
2176 /* Always switch state before calling upper layers */
2177 irlap_next_state(self, LAP_NDM);
2178 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
2182 /* Always switch state before calling upper layers */
2183 irlap_next_state(self, LAP_NDM);
2185 /* Send disconnect response */
2186 irlap_wait_min_turn_around(self, &self->qos_tx);
2187 irlap_send_ua_response_frame(self, NULL);
2189 del_timer(&self->wd_timer);
2190 irlap_flush_all_queues(self);
2191 /* Set default link parameters */
2192 irlap_apply_default_connection_parameters(self);
2194 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2196 case RECV_DISCOVERY_XID_CMD:
2197 irlap_wait_min_turn_around(self, &self->qos_tx);
2198 irlap_send_rr_frame(self, RSP_FRAME);
2199 self->ack_required = TRUE;
2200 irlap_start_wd_timer(self, self->wd_timeout);
2201 irlap_next_state(self, LAP_NRM_S);
2205 /* Remove test frame header (only LAP header in NRM) */
2206 skb_pull(skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
2208 irlap_wait_min_turn_around(self, &self->qos_tx);
2209 irlap_start_wd_timer(self, self->wd_timeout);
2211 /* Send response (info will be copied) */
2212 irlap_send_test_frame(self, self->caddr, info->daddr, skb);
2215 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __FUNCTION__,
2216 event, irlap_event[event]);
2225 * Function irlap_state_sclose (self, event, skb, info)
2227 static int irlap_state_sclose(struct irlap_cb *self, IRLAP_EVENT event,
2228 struct sk_buff *skb, struct irlap_info *info)
2232 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
2234 IRDA_ASSERT(self != NULL, return -ENODEV;);
2235 IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
2239 /* Always switch state before calling upper layers */
2240 irlap_next_state(self, LAP_NDM);
2242 /* Send disconnect response */
2243 irlap_wait_min_turn_around(self, &self->qos_tx);
2244 irlap_send_ua_response_frame(self, NULL);
2246 del_timer(&self->wd_timer);
2247 /* Set default link parameters */
2248 irlap_apply_default_connection_parameters(self);
2250 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2253 /* IrLAP-1.1 p.82: in SCLOSE, S and I type RSP frames
2254 * shall take us down into default NDM state, like DM_RSP
2261 /* Always switch state before calling upper layers */
2262 irlap_next_state(self, LAP_NDM);
2264 del_timer(&self->wd_timer);
2265 irlap_apply_default_connection_parameters(self);
2267 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2269 case WD_TIMER_EXPIRED:
2270 /* Always switch state before calling upper layers */
2271 irlap_next_state(self, LAP_NDM);
2273 irlap_apply_default_connection_parameters(self);
2275 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2278 /* IrLAP-1.1 p.82: in SCLOSE, basically any received frame
2279 * with pf=1 shall restart the wd-timer and resend the rd:rsp
2281 if (info != NULL && info->pf) {
2282 del_timer(&self->wd_timer);
2283 irlap_wait_min_turn_around(self, &self->qos_tx);
2284 irlap_send_rd_frame(self);
2285 irlap_start_wd_timer(self, self->wd_timeout);
2286 break; /* stay in SCLOSE */
2289 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __FUNCTION__,
2290 event, irlap_event[event]);
2299 static int irlap_state_reset_check( struct irlap_cb *self, IRLAP_EVENT event,
2300 struct sk_buff *skb,
2301 struct irlap_info *info)
2305 IRDA_DEBUG(1, "%s(), event=%s\n", __FUNCTION__, irlap_event[event]);
2307 IRDA_ASSERT(self != NULL, return -ENODEV;);
2308 IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
2311 case RESET_RESPONSE:
2312 irlap_send_ua_response_frame(self, &self->qos_rx);
2313 irlap_initiate_connection_state(self);
2314 irlap_start_wd_timer(self, WD_TIMEOUT);
2315 irlap_flush_all_queues(self);
2317 irlap_next_state(self, LAP_NRM_S);
2319 case DISCONNECT_REQUEST:
2320 irlap_wait_min_turn_around(self, &self->qos_tx);
2321 irlap_send_rd_frame(self);
2322 irlap_start_wd_timer(self, WD_TIMEOUT);
2323 irlap_next_state(self, LAP_SCLOSE);
2326 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __FUNCTION__,
2327 event, irlap_event[event]);