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 ASSERT(self != NULL, return;);
173 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 void irlap_start_poll_timer(struct irlap_cb *self, int timeout)
186 ASSERT(self != NULL, return;);
187 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 ASSERT(self != NULL, return -1;);
350 ASSERT(self->magic == LAP_MAGIC, return -1;);
353 case CONNECT_REQUEST:
354 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 ASSERT(info != NULL, return -1;);
394 if (self->media_busy) {
395 IRDA_DEBUG(0, "%s(), media busy!\n", __FUNCTION__);
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 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 * Remember to multiply the query timeout value with
437 * the number of slots used
439 irlap_start_query_timer(self, QUERY_TIMEOUT*info->S);
440 irlap_next_state(self, LAP_REPLY);
442 /* This is the final slot. How is it possible ?
443 * This would happen is both discoveries are just slightly
444 * offset (if they are in sync, all packets are lost).
445 * Most often, all the discovery requests will be received
446 * in QUERY state (see my comment there), except for the
447 * last frame that will come here.
448 * The big trouble when it happen is that active discovery
449 * doesn't happen, because nobody answer the discoveries
450 * frame of the other guy, so the log shows up empty.
451 * What should we do ?
452 * Not much. It's too late to answer those discovery frames,
453 * so we just pass the info to IrLMP who will put it in the
454 * log (and post an event).
457 IRDA_DEBUG(1, "%s(), Receiving final discovery request, missed the discovery slots :-(\n", __FUNCTION__);
459 /* Last discovery request -> in the log */
460 irlap_discovery_indication(self, info->discovery);
463 case MEDIA_BUSY_TIMER_EXPIRED:
464 /* A bunch of events may be postponed because the media is
465 * busy (usually immediately after we close a connection),
466 * or while we are doing discovery (state query/reply).
467 * In all those cases, the media busy flag will be cleared
468 * when it's OK for us to process those postponed events.
469 * This event is not mentioned in the state machines in the
470 * IrLAP spec. It's because they didn't consider Ultra and
471 * postponing connection request is optional.
473 #ifdef CONFIG_IRDA_ULTRA
474 /* Send any pending Ultra frames if any */
475 if (!skb_queue_empty(&self->txq_ultra)) {
476 /* We don't send the frame, just post an event.
477 * Also, previously this code was in timer.c...
479 ret = (*state[self->state])(self, SEND_UI_FRAME,
482 #endif /* CONFIG_IRDA_ULTRA */
483 /* Check if we should try to connect.
484 * This code was previously in irlap_do_event() */
485 if (self->connect_pending) {
486 self->connect_pending = FALSE;
488 /* This one *should* not pend in this state, except
489 * if a socket try to connect and immediately
490 * disconnect. - clear - Jean II */
491 if (self->disconnect_pending)
492 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
494 ret = (*state[self->state])(self,
497 self->disconnect_pending = FALSE;
499 /* Note : one way to test if this code works well (including
500 * media busy and small busy) is to create a user space
501 * application generating an Ultra packet every 3.05 sec (or
502 * 2.95 sec) and to see how it interact with discovery.
503 * It's fairly easy to check that no packet is lost, that the
504 * packets are postponed during discovery and that after
505 * discovery indication you have a 100ms "gap".
506 * As connection request and Ultra are now processed the same
507 * way, this avoid the tedious job of trying IrLAP connection
508 * in all those cases...
511 #ifdef CONFIG_IRDA_ULTRA
515 /* Only allowed to repeat an operation twice */
516 for (i=0; ((i<2) && (self->media_busy == FALSE)); i++) {
517 skb = skb_dequeue(&self->txq_ultra);
519 irlap_send_ui_frame(self, skb, CBROADCAST,
523 /* irlap_send_ui_frame() won't increase skb reference
524 * count, so no dev_kfree_skb() - Jean II */
527 /* Force us to listen 500 ms again */
528 irda_device_set_media_busy(self->netdev, TRUE);
533 /* Only accept broadcast frames in NDM mode */
534 if (info->caddr != CBROADCAST) {
535 IRDA_DEBUG(0, "%s(), not a broadcast frame!\n",
538 irlap_unitdata_indication(self, skb);
540 #endif /* CONFIG_IRDA_ULTRA */
542 /* Remove test frame header */
543 skb_pull(skb, sizeof(struct test_frame));
546 * Send response. This skb will not be sent out again, and
547 * will only be used to send out the same info as the cmd
549 irlap_send_test_frame(self, CBROADCAST, info->daddr, skb);
552 IRDA_DEBUG(0, "%s() not implemented!\n", __FUNCTION__);
555 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
565 * Function irlap_state_query (event, skb, info)
570 static int irlap_state_query(struct irlap_cb *self, IRLAP_EVENT event,
571 struct sk_buff *skb, struct irlap_info *info)
575 ASSERT(self != NULL, return -1;);
576 ASSERT(self->magic == LAP_MAGIC, return -1;);
579 case RECV_DISCOVERY_XID_RSP:
580 ASSERT(info != NULL, return -1;);
581 ASSERT(info->discovery != NULL, return -1;);
583 IRDA_DEBUG(4, "%s(), daddr=%08x\n", __FUNCTION__,
584 info->discovery->data.daddr);
586 if (!self->discovery_log) {
587 WARNING("%s: discovery log is gone! "
588 "maybe the discovery timeout has been set to "
589 "short?\n", __FUNCTION__);
592 hashbin_insert(self->discovery_log,
593 (irda_queue_t *) info->discovery,
594 info->discovery->data.daddr, NULL);
597 /* irlap_next_state(self, LAP_QUERY); */
600 case RECV_DISCOVERY_XID_CMD:
601 /* Yes, it is possible to receive those frames in this mode.
602 * Note that most often the last discovery request won't
603 * occur here but in NDM state (see my comment there).
604 * What should we do ?
605 * Not much. We are currently performing our own discovery,
606 * therefore we can't answer those frames. We don't want
607 * to change state either. We just pass the info to
608 * IrLMP who will put it in the log (and post an event).
612 ASSERT(info != NULL, return -1;);
614 IRDA_DEBUG(1, "%s(), Receiving discovery request (s = %d) while performing discovery :-(\n", __FUNCTION__, info->s);
616 /* Last discovery request ? */
618 irlap_discovery_indication(self, info->discovery);
620 case SLOT_TIMER_EXPIRED:
622 * Wait a little longer if we detect an incoming frame. This
623 * is not mentioned in the spec, but is a good thing to do,
624 * since we want to work even with devices that violate the
625 * timing requirements.
627 if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
628 IRDA_DEBUG(2, "%s(), device is slow to answer, "
629 "waiting some more!\n", __FUNCTION__);
630 irlap_start_slot_timer(self, msecs_to_jiffies(10));
631 self->add_wait = TRUE;
634 self->add_wait = FALSE;
636 if (self->s < self->S) {
637 irlap_send_discovery_xid_frame(self, self->S,
639 self->discovery_cmd);
641 irlap_start_slot_timer(self, self->slot_timeout);
644 irlap_next_state(self, LAP_QUERY);
646 /* This is the final slot! */
647 irlap_send_discovery_xid_frame(self, self->S, 0xff,
649 self->discovery_cmd);
651 /* Always switch state before calling upper layers */
652 irlap_next_state(self, LAP_NDM);
655 * We are now finished with the discovery procedure,
656 * so now we must return the results
658 irlap_discovery_confirm(self, self->discovery_log);
660 /* IrLMP should now have taken care of the log */
661 self->discovery_log = NULL;
665 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
675 * Function irlap_state_reply (self, event, skb, info)
677 * REPLY, we have received a XID discovery frame from a device and we
678 * are waiting for the right time slot to send a response XID frame
681 static int irlap_state_reply(struct irlap_cb *self, IRLAP_EVENT event,
682 struct sk_buff *skb, struct irlap_info *info)
684 discovery_t *discovery_rsp;
687 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
689 ASSERT(self != NULL, return -1;);
690 ASSERT(self->magic == LAP_MAGIC, return -1;);
693 case QUERY_TIMER_EXPIRED:
694 IRDA_DEBUG(2, "%s(), QUERY_TIMER_EXPIRED <%ld>\n",
695 __FUNCTION__, jiffies);
696 irlap_next_state(self, LAP_NDM);
698 case RECV_DISCOVERY_XID_CMD:
699 ASSERT(info != NULL, return -1;);
701 if (info->s == 0xff) {
702 del_timer(&self->query_timer);
704 /* info->log.condition = REMOTE; */
706 /* Always switch state before calling upper layers */
707 irlap_next_state(self, LAP_NDM);
709 irlap_discovery_indication(self, info->discovery);
710 } else if ((info->s >= self->slot) && (!self->frame_sent)) {
711 discovery_rsp = irlmp_get_discovery_response();
712 discovery_rsp->data.daddr = info->daddr;
714 irlap_send_discovery_xid_frame(self, info->S,
718 self->frame_sent = TRUE;
719 irlap_next_state(self, LAP_REPLY);
723 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __FUNCTION__,
724 event, irlap_event[event]);
733 * Function irlap_state_conn (event, skb, info)
735 * CONN, we have received a SNRM command and is waiting for the upper
736 * layer to accept or refuse connection
739 static int irlap_state_conn(struct irlap_cb *self, IRLAP_EVENT event,
740 struct sk_buff *skb, struct irlap_info *info)
744 IRDA_DEBUG(4, "%s(), event=%s\n", __FUNCTION__, irlap_event[ event]);
746 ASSERT(self != NULL, return -1;);
747 ASSERT(self->magic == LAP_MAGIC, return -1;);
750 case CONNECT_RESPONSE:
751 skb_pull(skb, sizeof(struct snrm_frame));
753 ASSERT(self->netdev != NULL, return -1;);
755 irlap_qos_negotiate(self, skb);
757 irlap_initiate_connection_state(self);
760 * Applying the parameters now will make sure we change speed
761 * *after* we have sent the next frame
763 irlap_apply_connection_parameters(self, FALSE);
766 * Sending this frame will force a speed change after it has
767 * been sent (i.e. the frame will be sent at 9600).
769 irlap_send_ua_response_frame(self, &self->qos_rx);
773 * We are allowed to send two frames, but this may increase
774 * the connect latency, so lets not do it for now.
776 /* This is full of good intentions, but doesn't work in
778 * After sending the first UA response, we switch the
779 * dongle to the negotiated speed, which is usually
780 * different than 9600 kb/s.
781 * From there, there is two solutions :
782 * 1) The other end has received the first UA response :
783 * it will set up the connection, move to state LAP_NRM_P,
784 * and will ignore and drop the second UA response.
785 * Actually, it's even worse : the other side will almost
786 * immediately send a RR that will likely collide with the
787 * UA response (depending on negotiated turnaround).
788 * 2) The other end has not received the first UA response,
789 * will stay at 9600 and will never see the second UA response.
791 irlap_send_ua_response_frame(self, &self->qos_rx);
795 * The WD-timer could be set to the duration of the P-timer
796 * for this case, but it is recommended to use twice the
797 * value (note 3 IrLAP p. 60).
799 irlap_start_wd_timer(self, self->wd_timeout);
800 irlap_next_state(self, LAP_NRM_S);
803 case RECV_DISCOVERY_XID_CMD:
804 IRDA_DEBUG(3, "%s(), event RECV_DISCOVER_XID_CMD!\n",
806 irlap_next_state(self, LAP_NDM);
809 case DISCONNECT_REQUEST:
810 IRDA_DEBUG(0, "%s(), Disconnect request!\n", __FUNCTION__);
811 irlap_send_dm_frame(self);
812 irlap_next_state( self, LAP_NDM);
813 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
816 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __FUNCTION__,
817 event, irlap_event[event]);
827 * Function irlap_state_setup (event, skb, frame)
829 * SETUP state, The local layer has transmitted a SNRM command frame to
830 * a remote peer layer and is awaiting a reply .
833 static int irlap_state_setup(struct irlap_cb *self, IRLAP_EVENT event,
834 struct sk_buff *skb, struct irlap_info *info)
838 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
840 ASSERT(self != NULL, return -1;);
841 ASSERT(self->magic == LAP_MAGIC, return -1;);
844 case FINAL_TIMER_EXPIRED:
845 if (self->retry_count < self->N3) {
847 * Perform random backoff, Wait a random number of time units, minimum
848 * duration half the time taken to transmitt a SNRM frame, maximum duration
849 * 1.5 times the time taken to transmit a SNRM frame. So this time should
850 * between 15 msecs and 45 msecs.
852 irlap_start_backoff_timer(self, msecs_to_jiffies(20 +
855 /* Always switch state before calling upper layers */
856 irlap_next_state(self, LAP_NDM);
858 irlap_disconnect_indication(self, LAP_FOUND_NONE);
861 case BACKOFF_TIMER_EXPIRED:
862 irlap_send_snrm_frame(self, &self->qos_rx);
863 irlap_start_final_timer(self, self->final_timeout);
867 IRDA_DEBUG(4, "%s(), SNRM battle!\n", __FUNCTION__);
869 ASSERT(skb != NULL, return 0;);
870 ASSERT(info != NULL, return 0;);
873 * The device with the largest device address wins the battle
874 * (both have sent a SNRM command!)
876 if (info &&(info->daddr > self->saddr)) {
877 del_timer(&self->final_timer);
878 irlap_initiate_connection_state(self);
880 ASSERT(self->netdev != NULL, return -1;);
882 skb_pull(skb, sizeof(struct snrm_frame));
884 irlap_qos_negotiate(self, skb);
886 /* Send UA frame and then change link settings */
887 irlap_apply_connection_parameters(self, FALSE);
888 irlap_send_ua_response_frame(self, &self->qos_rx);
890 irlap_next_state(self, LAP_NRM_S);
891 irlap_connect_confirm(self, skb);
894 * The WD-timer could be set to the duration of the
895 * P-timer for this case, but it is recommended
896 * to use twice the value (note 3 IrLAP p. 60).
898 irlap_start_wd_timer(self, self->wd_timeout);
900 /* We just ignore the other device! */
901 irlap_next_state(self, LAP_SETUP);
906 del_timer(&self->final_timer);
908 /* Initiate connection state */
909 irlap_initiate_connection_state(self);
911 /* Negotiate connection parameters */
912 ASSERT(skb->len > 10, return -1;);
914 skb_pull(skb, sizeof(struct ua_frame));
916 ASSERT(self->netdev != NULL, return -1;);
918 irlap_qos_negotiate(self, skb);
920 /* Set the new link setting *now* (before the rr frame) */
921 irlap_apply_connection_parameters(self, TRUE);
922 self->retry_count = 0;
924 /* Wait for turnaround time to give a chance to the other
925 * device to be ready to receive us.
926 * Note : the time to switch speed is typically larger
927 * than the turnaround time, but as we don't have the other
928 * side speed switch time, that's our best guess...
930 irlap_wait_min_turn_around(self, &self->qos_tx);
932 /* This frame will actually be sent at the new speed */
933 irlap_send_rr_frame(self, CMD_FRAME);
935 /* The timer is set to half the normal timer to quickly
936 * detect a failure to negociate the new connection
937 * parameters. IrLAP 6.11.3.2, note 3.
938 * Note that currently we don't process this failure
939 * properly, as we should do a quick disconnect.
941 irlap_start_final_timer(self, self->final_timeout/2);
942 irlap_next_state(self, LAP_NRM_P);
944 irlap_connect_confirm(self, skb);
946 case RECV_DM_RSP: /* FALLTHROUGH */
948 del_timer(&self->final_timer);
949 irlap_next_state(self, LAP_NDM);
951 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
954 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __FUNCTION__,
955 event, irlap_event[event]);
964 * Function irlap_state_offline (self, event, skb, info)
966 * OFFLINE state, not used for now!
969 static int irlap_state_offline(struct irlap_cb *self, IRLAP_EVENT event,
970 struct sk_buff *skb, struct irlap_info *info)
972 IRDA_DEBUG( 0, "%s(), Unknown event\n", __FUNCTION__);
978 * Function irlap_state_xmit_p (self, event, skb, info)
980 * XMIT, Only the primary station has right to transmit, and we
981 * therefore do not expect to receive any transmissions from other
985 static int irlap_state_xmit_p(struct irlap_cb *self, IRLAP_EVENT event,
986 struct sk_buff *skb, struct irlap_info *info)
993 * Only send frame if send-window > 0.
995 if ((self->window > 0) && (!self->remote_busy)) {
997 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
998 struct sk_buff *skb_next;
1000 /* With DYNAMIC_WINDOW, we keep the window size
1001 * maximum, and adapt on the packets we are sending.
1002 * At 115k, we can send only 2 packets of 2048 bytes
1003 * in a 500 ms turnaround. Without this option, we
1004 * would always limit the window to 2. With this
1005 * option, if we send smaller packets, we can send
1006 * up to 7 of them (always depending on QoS).
1009 /* Look at the next skb. This is safe, as we are
1010 * the only consumer of the Tx queue (if we are not,
1011 * we have other problems) - Jean II */
1012 skb_next = skb_peek(&self->txq);
1014 /* Check if a subsequent skb exist and would fit in
1015 * the current window (with respect to turnaround
1017 * This allow us to properly mark the current packet
1018 * with the pf bit, to avoid falling back on the
1019 * second test below, and avoid waiting the
1020 * end of the window and sending a extra RR.
1021 * Note : (skb_next != NULL) <=> (skb_queue_len() > 0)
1023 nextfit = ((skb_next != NULL) &&
1024 ((skb_next->len + skb->len) <=
1028 * The current packet may not fit ! Because of test
1029 * above, this should not happen any more !!!
1030 * Test if we have transmitted more bytes over the
1031 * link than its possible to do with the current
1032 * speed and turn-around-time.
1034 if((!nextfit) && (skb->len > self->bytes_left)) {
1035 IRDA_DEBUG(0, "%s(), Not allowed to transmit"
1036 " more bytes!\n", __FUNCTION__);
1037 /* Requeue the skb */
1038 skb_queue_head(&self->txq, skb_get(skb));
1040 * We should switch state to LAP_NRM_P, but
1041 * that is not possible since we must be sure
1042 * that we poll the other side. Since we have
1043 * used up our time, the poll timer should
1044 * trigger anyway now, so we just wait for it
1048 * Sorry, but that's not totally true. If
1049 * we send 2000B packets, we may wait another
1050 * 1000B until our turnaround expire. That's
1051 * why we need to be proactive in avoiding
1052 * coming here. - Jean II
1057 /* Substract space used by this skb */
1058 self->bytes_left -= skb->len;
1059 #else /* CONFIG_IRDA_DYNAMIC_WINDOW */
1060 /* Window has been adjusted for the max packet
1061 * size, so much simpler... - Jean II */
1062 nextfit = (skb_queue_len(&self->txq) > 0);
1063 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1065 * Send data with poll bit cleared only if window > 1
1066 * and there is more frames after this one to be sent
1068 if ((self->window > 1) && (nextfit)) {
1069 /* More packet to send in current window */
1070 irlap_send_data_primary(self, skb);
1071 irlap_next_state(self, LAP_XMIT_P);
1073 /* Final packet of window */
1074 irlap_send_data_primary_poll(self, skb);
1075 irlap_next_state(self, LAP_NRM_P);
1078 * Make sure state machine does not try to send
1083 #ifdef CONFIG_IRDA_FAST_RR
1084 /* Peer may want to reply immediately */
1085 self->fast_RR = FALSE;
1086 #endif /* CONFIG_IRDA_FAST_RR */
1088 IRDA_DEBUG(4, "%s(), Unable to send! remote busy?\n",
1090 skb_queue_head(&self->txq, skb_get(skb));
1093 * The next ret is important, because it tells
1094 * irlap_next_state _not_ to deliver more frames
1099 case POLL_TIMER_EXPIRED:
1100 IRDA_DEBUG(3, "%s(), POLL_TIMER_EXPIRED <%ld>\n",
1101 __FUNCTION__, jiffies);
1102 irlap_send_rr_frame(self, CMD_FRAME);
1103 /* Return to NRM properly - Jean II */
1104 self->window = self->window_size;
1105 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1106 /* Allowed to transmit a maximum number of bytes again. */
1107 self->bytes_left = self->line_capacity;
1108 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1109 irlap_start_final_timer(self, self->final_timeout);
1110 irlap_next_state(self, LAP_NRM_P);
1112 case DISCONNECT_REQUEST:
1113 del_timer(&self->poll_timer);
1114 irlap_wait_min_turn_around(self, &self->qos_tx);
1115 irlap_send_disc_frame(self);
1116 irlap_flush_all_queues(self);
1117 irlap_start_final_timer(self, self->final_timeout);
1118 self->retry_count = 0;
1119 irlap_next_state(self, LAP_PCLOSE);
1122 /* Nothing to do, irlap_do_event() will send the packet
1123 * when we return... - Jean II */
1126 IRDA_DEBUG(0, "%s(), Unknown event %s\n",
1127 __FUNCTION__, irlap_event[event]);
1136 * Function irlap_state_pclose (event, skb, info)
1140 static int irlap_state_pclose(struct irlap_cb *self, IRLAP_EVENT event,
1141 struct sk_buff *skb, struct irlap_info *info)
1145 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1147 ASSERT(self != NULL, return -1;);
1148 ASSERT(self->magic == LAP_MAGIC, return -1;);
1151 case RECV_UA_RSP: /* FALLTHROUGH */
1153 del_timer(&self->final_timer);
1155 /* Set new link parameters */
1156 irlap_apply_default_connection_parameters(self);
1158 /* Always switch state before calling upper layers */
1159 irlap_next_state(self, LAP_NDM);
1161 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
1163 case FINAL_TIMER_EXPIRED:
1164 if (self->retry_count < self->N3) {
1165 irlap_wait_min_turn_around(self, &self->qos_tx);
1166 irlap_send_disc_frame(self);
1167 irlap_start_final_timer(self, self->final_timeout);
1168 self->retry_count++;
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_NO_RESPONSE);
1180 IRDA_DEBUG(1, "%s(), Unknown event %d\n", __FUNCTION__, event);
1189 * Function irlap_state_nrm_p (self, event, skb, info)
1191 * NRM_P (Normal Response Mode as Primary), The primary station has given
1192 * permissions to a secondary station to transmit IrLAP resonse frames
1193 * (by sending a frame with the P bit set). The primary station will not
1194 * transmit any frames and is expecting to receive frames only from the
1195 * secondary to which transmission permissions has been given.
1197 static int irlap_state_nrm_p(struct irlap_cb *self, IRLAP_EVENT event,
1198 struct sk_buff *skb, struct irlap_info *info)
1205 case RECV_I_RSP: /* Optimize for the common case */
1206 /* FIXME: must check for remote_busy below */
1207 #ifdef CONFIG_IRDA_FAST_RR
1209 * Reset the fast_RR so we can use the fast RR code with
1210 * full speed the next time since peer may have more frames
1213 self->fast_RR = FALSE;
1214 #endif /* CONFIG_IRDA_FAST_RR */
1215 ASSERT( info != NULL, return -1;);
1217 ns_status = irlap_validate_ns_received(self, info->ns);
1218 nr_status = irlap_validate_nr_received(self, info->nr);
1221 * Check for expected I(nformation) frame
1223 if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
1225 /* Update Vr (next frame for us to receive) */
1226 self->vr = (self->vr + 1) % 8;
1228 /* Update Nr received, cleanup our retry queue */
1229 irlap_update_nr_received(self, info->nr);
1232 * Got expected NR, so reset the
1233 * retry_count. This is not done by IrLAP spec,
1236 self->retry_count = 0;
1237 self->ack_required = TRUE;
1239 /* poll bit cleared? */
1241 /* Keep state, do not move this line */
1242 irlap_next_state(self, LAP_NRM_P);
1244 irlap_data_indication(self, skb, FALSE);
1246 /* No longer waiting for pf */
1247 del_timer(&self->final_timer);
1249 irlap_wait_min_turn_around(self, &self->qos_tx);
1251 /* Call higher layer *before* changing state
1252 * to give them a chance to send data in the
1255 irlap_data_indication(self, skb, FALSE);
1257 /* XMIT states are the most dangerous state
1258 * to be in, because user requests are
1259 * processed directly and may change state.
1260 * On the other hand, in NDM_P, those
1261 * requests are queued and we will process
1262 * them when we return to irlap_do_event().
1265 irlap_next_state(self, LAP_XMIT_P);
1267 /* This is the last frame.
1268 * Make sure it's always called in XMIT state.
1270 irlap_start_poll_timer(self, self->poll_timeout);
1275 /* Unexpected next to send (Ns) */
1276 if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
1279 irlap_update_nr_received(self, info->nr);
1282 * Wait until the last frame before doing
1287 irlap_next_state(self, LAP_NRM_P);
1290 "%s(), missing or duplicate frame!\n",
1293 /* Update Nr received */
1294 irlap_update_nr_received(self, info->nr);
1296 irlap_wait_min_turn_around(self, &self->qos_tx);
1297 irlap_send_rr_frame(self, CMD_FRAME);
1299 self->ack_required = FALSE;
1301 irlap_start_final_timer(self, self->final_timeout);
1302 irlap_next_state(self, LAP_NRM_P);
1307 * Unexpected next to receive (Nr)
1309 if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
1312 self->vr = (self->vr + 1) % 8;
1314 /* Update Nr received */
1315 irlap_update_nr_received(self, info->nr);
1317 /* Resend rejected frames */
1318 irlap_resend_rejected_frames(self, CMD_FRAME);
1320 self->ack_required = FALSE;
1322 /* Make sure we account for the time
1323 * to transmit our frames. See comemnts
1324 * in irlap_send_data_primary_poll().
1326 irlap_start_final_timer(self, 2 * self->final_timeout);
1328 /* Keep state, do not move this line */
1329 irlap_next_state(self, LAP_NRM_P);
1331 irlap_data_indication(self, skb, FALSE);
1334 * Do not resend frames until the last
1335 * frame has arrived from the other
1336 * device. This is not documented in
1339 self->vr = (self->vr + 1) % 8;
1341 /* Update Nr received */
1342 irlap_update_nr_received(self, info->nr);
1344 self->ack_required = FALSE;
1346 /* Keep state, do not move this line!*/
1347 irlap_next_state(self, LAP_NRM_P);
1349 irlap_data_indication(self, skb, FALSE);
1354 * Unexpected next to send (Ns) and next to receive (Nr)
1355 * Not documented by IrLAP!
1357 if ((ns_status == NS_UNEXPECTED) &&
1358 (nr_status == NR_UNEXPECTED))
1360 IRDA_DEBUG(4, "%s(), unexpected nr and ns!\n",
1363 /* Resend rejected frames */
1364 irlap_resend_rejected_frames(self, CMD_FRAME);
1366 /* Give peer some time to retransmit!
1367 * But account for our own Tx. */
1368 irlap_start_final_timer(self, 2 * self->final_timeout);
1370 /* Keep state, do not move this line */
1371 irlap_next_state(self, LAP_NRM_P);
1373 /* Update Nr received */
1374 /* irlap_update_nr_received( info->nr); */
1376 self->ack_required = FALSE;
1384 if ((nr_status == NR_INVALID) || (ns_status == NS_INVALID)) {
1386 del_timer(&self->final_timer);
1388 irlap_next_state(self, LAP_RESET_WAIT);
1390 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1391 self->xmitflag = TRUE;
1393 del_timer(&self->final_timer);
1395 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1397 self->xmitflag = FALSE;
1401 IRDA_DEBUG(1, "%s(), Not implemented!\n", __FUNCTION__);
1402 IRDA_DEBUG(1, "%s(), event=%s, ns_status=%d, nr_status=%d\n",
1403 __FUNCTION__, irlap_event[event], ns_status, nr_status);
1406 /* Poll bit cleared? */
1408 irlap_data_indication(self, skb, TRUE);
1409 irlap_next_state(self, LAP_NRM_P);
1411 del_timer(&self->final_timer);
1412 irlap_data_indication(self, skb, TRUE);
1413 irlap_next_state(self, LAP_XMIT_P);
1414 IRDA_DEBUG(1, "%s: RECV_UI_FRAME: next state %s\n", __FUNCTION__, irlap_state[self->state]);
1415 irlap_start_poll_timer(self, self->poll_timeout);
1420 * If you get a RR, the remote isn't busy anymore,
1421 * no matter what the NR
1423 self->remote_busy = FALSE;
1428 ret = irlap_validate_nr_received(self, info->nr);
1429 if (ret == NR_EXPECTED) {
1430 /* Stop final timer */
1431 del_timer(&self->final_timer);
1433 /* Update Nr received */
1434 irlap_update_nr_received(self, info->nr);
1437 * Got expected NR, so reset the retry_count. This
1438 * is not done by the IrLAP standard , which is
1441 self->retry_count = 0;
1442 irlap_wait_min_turn_around(self, &self->qos_tx);
1444 irlap_next_state(self, LAP_XMIT_P);
1446 /* Start poll timer */
1447 irlap_start_poll_timer(self, self->poll_timeout);
1448 } else if (ret == NR_UNEXPECTED) {
1449 ASSERT(info != NULL, return -1;);
1454 /* Update Nr received */
1455 irlap_update_nr_received(self, info->nr);
1457 IRDA_DEBUG(4, "RECV_RR_FRAME: Retrans:%d, nr=%d, va=%d, "
1459 self->retry_count, info->nr, self->va,
1460 self->vs, self->vr);
1462 /* Resend rejected frames */
1463 irlap_resend_rejected_frames(self, CMD_FRAME);
1465 /* Final timer ??? Jean II */
1467 irlap_next_state(self, LAP_NRM_P);
1468 } else if (ret == NR_INVALID) {
1469 IRDA_DEBUG(1, "%s(), Received RR with "
1470 "invalid nr !\n", __FUNCTION__);
1471 del_timer(&self->final_timer);
1473 irlap_next_state(self, LAP_RESET_WAIT);
1475 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1476 self->xmitflag = TRUE;
1480 ASSERT(info != NULL, return -1;);
1482 /* Stop final timer */
1483 del_timer(&self->final_timer);
1484 self->remote_busy = TRUE;
1486 /* Update Nr received */
1487 irlap_update_nr_received(self, info->nr);
1488 irlap_next_state(self, LAP_XMIT_P);
1490 /* Start poll timer */
1491 irlap_start_poll_timer(self, self->poll_timeout);
1494 del_timer(&self->final_timer);
1495 self->xmitflag = TRUE;
1496 irlap_next_state(self, LAP_RESET_WAIT);
1497 irlap_reset_indication(self);
1499 case FINAL_TIMER_EXPIRED:
1501 * We are allowed to wait for additional 300 ms if
1502 * final timer expires when we are in the middle
1503 * of receiving a frame (page 45, IrLAP). Check that
1504 * we only do this once for each frame.
1506 if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
1507 IRDA_DEBUG(1, "FINAL_TIMER_EXPIRED when receiving a "
1508 "frame! Waiting a little bit more!\n");
1509 irlap_start_final_timer(self, msecs_to_jiffies(300));
1512 * Don't allow this to happen one more time in a row,
1513 * or else we can get a pretty tight loop here if
1514 * if we only receive half a frame. DB.
1516 self->add_wait = TRUE;
1519 self->add_wait = FALSE;
1521 /* N2 is the disconnect timer. Until we reach it, we retry */
1522 if (self->retry_count < self->N2) {
1523 /* Retry sending the pf bit to the secondary */
1524 irlap_wait_min_turn_around(self, &self->qos_tx);
1525 irlap_send_rr_frame(self, CMD_FRAME);
1527 irlap_start_final_timer(self, self->final_timeout);
1528 self->retry_count++;
1529 IRDA_DEBUG(4, "irlap_state_nrm_p: FINAL_TIMER_EXPIRED:"
1530 " retry_count=%d\n", self->retry_count);
1532 /* Early warning event. I'm using a pretty liberal
1533 * interpretation of the spec and generate an event
1534 * every time the timer is multiple of N1 (and not
1535 * only the first time). This allow application
1536 * to know precisely if connectivity restart...
1538 if((self->retry_count % self->N1) == 0)
1539 irlap_status_indication(self,
1540 STATUS_NO_ACTIVITY);
1544 irlap_apply_default_connection_parameters(self);
1546 /* Always switch state before calling upper layers */
1547 irlap_next_state(self, LAP_NDM);
1548 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1552 irlap_update_nr_received(self, info->nr);
1553 if (self->remote_busy) {
1554 irlap_wait_min_turn_around(self, &self->qos_tx);
1555 irlap_send_rr_frame(self, CMD_FRAME);
1557 irlap_resend_rejected_frames(self, CMD_FRAME);
1558 irlap_start_final_timer(self, 2 * self->final_timeout);
1561 irlap_update_nr_received(self, info->nr);
1562 if (self->remote_busy) {
1563 irlap_wait_min_turn_around(self, &self->qos_tx);
1564 irlap_send_rr_frame(self, CMD_FRAME);
1566 irlap_resend_rejected_frame(self, CMD_FRAME);
1567 irlap_start_final_timer(self, 2 * self->final_timeout);
1570 IRDA_DEBUG(1, "%s(), RECV_RD_RSP\n", __FUNCTION__);
1572 irlap_flush_all_queues(self);
1573 irlap_next_state(self, LAP_XMIT_P);
1574 /* Call back the LAP state machine to do a proper disconnect */
1575 irlap_disconnect_request(self);
1578 IRDA_DEBUG(1, "%s(), Unknown event %s\n",
1579 __FUNCTION__, irlap_event[event]);
1588 * Function irlap_state_reset_wait (event, skb, info)
1590 * We have informed the service user of a reset condition, and is
1591 * awaiting reset of disconnect request.
1594 static int irlap_state_reset_wait(struct irlap_cb *self, IRLAP_EVENT event,
1595 struct sk_buff *skb, struct irlap_info *info)
1599 IRDA_DEBUG(3, "%s(), event = %s\n", __FUNCTION__, irlap_event[event]);
1601 ASSERT(self != NULL, return -1;);
1602 ASSERT(self->magic == LAP_MAGIC, return -1;);
1606 if (self->xmitflag) {
1607 irlap_wait_min_turn_around(self, &self->qos_tx);
1608 irlap_send_snrm_frame(self, NULL);
1609 irlap_start_final_timer(self, self->final_timeout);
1610 irlap_next_state(self, LAP_RESET);
1612 irlap_start_final_timer(self, self->final_timeout);
1613 irlap_next_state(self, LAP_RESET);
1616 case DISCONNECT_REQUEST:
1617 irlap_wait_min_turn_around( self, &self->qos_tx);
1618 irlap_send_disc_frame( self);
1619 irlap_flush_all_queues( self);
1620 irlap_start_final_timer( self, self->final_timeout);
1621 self->retry_count = 0;
1622 irlap_next_state( self, LAP_PCLOSE);
1625 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
1626 irlap_event[event]);
1635 * Function irlap_state_reset (self, event, skb, info)
1637 * We have sent a SNRM reset command to the peer layer, and is awaiting
1641 static int irlap_state_reset(struct irlap_cb *self, IRLAP_EVENT event,
1642 struct sk_buff *skb, struct irlap_info *info)
1646 IRDA_DEBUG(3, "%s(), event = %s\n", __FUNCTION__, irlap_event[event]);
1648 ASSERT(self != NULL, return -1;);
1649 ASSERT(self->magic == LAP_MAGIC, return -1;);
1653 del_timer(&self->final_timer);
1655 irlap_apply_default_connection_parameters(self);
1657 /* Always switch state before calling upper layers */
1658 irlap_next_state(self, LAP_NDM);
1660 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1664 del_timer(&self->final_timer);
1666 /* Initiate connection state */
1667 irlap_initiate_connection_state(self);
1669 irlap_reset_confirm();
1671 self->remote_busy = FALSE;
1673 irlap_next_state(self, LAP_XMIT_P);
1675 irlap_start_poll_timer(self, self->poll_timeout);
1678 case FINAL_TIMER_EXPIRED:
1679 if (self->retry_count < 3) {
1680 irlap_wait_min_turn_around(self, &self->qos_tx);
1682 ASSERT(self->netdev != NULL, return -1;);
1683 irlap_send_snrm_frame(self, self->qos_dev);
1685 self->retry_count++; /* Experimental!! */
1687 irlap_start_final_timer(self, self->final_timeout);
1688 irlap_next_state(self, LAP_RESET);
1689 } else if (self->retry_count >= self->N3) {
1690 irlap_apply_default_connection_parameters(self);
1692 /* Always switch state before calling upper layers */
1693 irlap_next_state(self, LAP_NDM);
1695 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1700 * SNRM frame is not allowed to contain an I-field in this
1704 IRDA_DEBUG(3, "%s(), RECV_SNRM_CMD\n", __FUNCTION__);
1705 irlap_initiate_connection_state(self);
1706 irlap_wait_min_turn_around(self, &self->qos_tx);
1707 irlap_send_ua_response_frame(self, &self->qos_rx);
1708 irlap_reset_confirm();
1709 irlap_start_wd_timer(self, self->wd_timeout);
1710 irlap_next_state(self, LAP_NDM);
1713 "%s(), SNRM frame contained an I field!\n",
1718 IRDA_DEBUG(1, "%s(), Unknown event %s\n",
1719 __FUNCTION__, irlap_event[event]);
1728 * Function irlap_state_xmit_s (event, skb, info)
1730 * XMIT_S, The secondary station has been given the right to transmit,
1731 * and we therefor do not expect to receive any transmissions from other
1734 static int irlap_state_xmit_s(struct irlap_cb *self, IRLAP_EVENT event,
1735 struct sk_buff *skb, struct irlap_info *info)
1739 IRDA_DEBUG(4, "%s(), event=%s\n", __FUNCTION__, irlap_event[event]);
1741 ASSERT(self != NULL, return -ENODEV;);
1742 ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
1747 * Send frame only if send window > 0
1749 if ((self->window > 0) && (!self->remote_busy)) {
1751 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1752 struct sk_buff *skb_next;
1755 * Same deal as in irlap_state_xmit_p(), so see
1756 * the comments at that point.
1757 * We are the secondary, so there are only subtle
1758 * differences. - Jean II
1761 /* Check if a subsequent skb exist and would fit in
1762 * the current window (with respect to turnaround
1763 * time). - Jean II */
1764 skb_next = skb_peek(&self->txq);
1765 nextfit = ((skb_next != NULL) &&
1766 ((skb_next->len + skb->len) <=
1770 * Test if we have transmitted more bytes over the
1771 * link than its possible to do with the current
1772 * speed and turn-around-time.
1774 if((!nextfit) && (skb->len > self->bytes_left)) {
1775 IRDA_DEBUG(0, "%s(), Not allowed to transmit"
1776 " more bytes!\n", __FUNCTION__);
1777 /* Requeue the skb */
1778 skb_queue_head(&self->txq, skb_get(skb));
1781 * Switch to NRM_S, this is only possible
1782 * when we are in secondary mode, since we
1783 * must be sure that we don't miss any RR
1786 self->window = self->window_size;
1787 self->bytes_left = self->line_capacity;
1788 irlap_start_wd_timer(self, self->wd_timeout);
1790 irlap_next_state(self, LAP_NRM_S);
1791 /* Slight difference with primary :
1792 * here we would wait for the other side to
1793 * expire the turnaround. - Jean II */
1795 return -EPROTO; /* Try again later */
1797 /* Substract space used by this skb */
1798 self->bytes_left -= skb->len;
1799 #else /* CONFIG_IRDA_DYNAMIC_WINDOW */
1800 /* Window has been adjusted for the max packet
1801 * size, so much simpler... - Jean II */
1802 nextfit = (skb_queue_len(&self->txq) > 0);
1803 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1805 * Send data with final bit cleared only if window > 1
1806 * and there is more frames to be sent
1808 if ((self->window > 1) && (nextfit)) {
1809 irlap_send_data_secondary(self, skb);
1810 irlap_next_state(self, LAP_XMIT_S);
1812 irlap_send_data_secondary_final(self, skb);
1813 irlap_next_state(self, LAP_NRM_S);
1816 * Make sure state machine does not try to send
1822 IRDA_DEBUG(2, "%s(), Unable to send!\n", __FUNCTION__);
1823 skb_queue_head(&self->txq, skb_get(skb));
1827 case DISCONNECT_REQUEST:
1828 irlap_send_rd_frame(self);
1829 irlap_flush_all_queues(self);
1830 irlap_start_wd_timer(self, self->wd_timeout);
1831 irlap_next_state(self, LAP_SCLOSE);
1834 /* Nothing to do, irlap_do_event() will send the packet
1835 * when we return... - Jean II */
1838 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
1839 irlap_event[event]);
1848 * Function irlap_state_nrm_s (event, skb, info)
1850 * NRM_S (Normal Response Mode as Secondary) state, in this state we are
1851 * expecting to receive frames from the primary station
1854 static int irlap_state_nrm_s(struct irlap_cb *self, IRLAP_EVENT event,
1855 struct sk_buff *skb, struct irlap_info *info)
1861 IRDA_DEBUG(4, "%s(), event=%s\n", __FUNCTION__, irlap_event[ event]);
1863 ASSERT(self != NULL, return -1;);
1864 ASSERT(self->magic == LAP_MAGIC, return -1;);
1867 case RECV_I_CMD: /* Optimize for the common case */
1868 /* FIXME: must check for remote_busy below */
1869 IRDA_DEBUG(4, "%s(), event=%s nr=%d, vs=%d, ns=%d, "
1870 "vr=%d, pf=%d\n", __FUNCTION__,
1871 irlap_event[event], info->nr,
1872 self->vs, info->ns, self->vr, info->pf);
1874 self->retry_count = 0;
1876 ns_status = irlap_validate_ns_received(self, info->ns);
1877 nr_status = irlap_validate_nr_received(self, info->nr);
1879 * Check for expected I(nformation) frame
1881 if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
1883 /* Update Vr (next frame for us to receive) */
1884 self->vr = (self->vr + 1) % 8;
1886 /* Update Nr received */
1887 irlap_update_nr_received(self, info->nr);
1894 self->ack_required = TRUE;
1897 * Starting WD-timer here is optional, but
1898 * not recommended. Note 6 IrLAP p. 83
1901 irda_start_timer(WD_TIMER, self->wd_timeout);
1903 /* Keep state, do not move this line */
1904 irlap_next_state(self, LAP_NRM_S);
1906 irlap_data_indication(self, skb, FALSE);
1910 * We should wait before sending RR, and
1911 * also before changing to XMIT_S
1912 * state. (note 1, IrLAP p. 82)
1914 irlap_wait_min_turn_around(self, &self->qos_tx);
1917 * Give higher layers a chance to
1918 * immediately reply with some data before
1919 * we decide if we should send a RR frame
1922 irlap_data_indication(self, skb, FALSE);
1924 /* Any pending data requests? */
1925 if ((skb_queue_len(&self->txq) > 0) &&
1928 self->ack_required = TRUE;
1930 del_timer(&self->wd_timer);
1932 irlap_next_state(self, LAP_XMIT_S);
1934 irlap_send_rr_frame(self, RSP_FRAME);
1935 irlap_start_wd_timer(self,
1938 /* Keep the state */
1939 irlap_next_state(self, LAP_NRM_S);
1945 * Check for Unexpected next to send (Ns)
1947 if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
1949 /* Unexpected next to send, with final bit cleared */
1951 irlap_update_nr_received(self, info->nr);
1953 irlap_start_wd_timer(self, self->wd_timeout);
1955 /* Update Nr received */
1956 irlap_update_nr_received(self, info->nr);
1958 irlap_wait_min_turn_around(self, &self->qos_tx);
1959 irlap_send_rr_frame(self, RSP_FRAME);
1961 irlap_start_wd_timer(self, self->wd_timeout);
1967 * Unexpected Next to Receive(NR) ?
1969 if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
1972 IRDA_DEBUG(4, "RECV_I_RSP: frame(s) lost\n");
1974 self->vr = (self->vr + 1) % 8;
1976 /* Update Nr received */
1977 irlap_update_nr_received(self, info->nr);
1979 /* Resend rejected frames */
1980 irlap_resend_rejected_frames(self, RSP_FRAME);
1982 /* Keep state, do not move this line */
1983 irlap_next_state(self, LAP_NRM_S);
1985 irlap_data_indication(self, skb, FALSE);
1986 irlap_start_wd_timer(self, self->wd_timeout);
1990 * This is not documented in IrLAP!! Unexpected NR
1991 * with poll bit cleared
1994 self->vr = (self->vr + 1) % 8;
1996 /* Update Nr received */
1997 irlap_update_nr_received(self, info->nr);
1999 /* Keep state, do not move this line */
2000 irlap_next_state(self, LAP_NRM_S);
2002 irlap_data_indication(self, skb, FALSE);
2003 irlap_start_wd_timer(self, self->wd_timeout);
2008 if (ret == NR_INVALID) {
2009 IRDA_DEBUG(0, "NRM_S, NR_INVALID not implemented!\n");
2011 if (ret == NS_INVALID) {
2012 IRDA_DEBUG(0, "NRM_S, NS_INVALID not implemented!\n");
2020 irlap_data_indication(self, skb, TRUE);
2021 irlap_next_state(self, LAP_NRM_S); /* Keep state */
2024 * Any pending data requests?
2026 if ((skb_queue_len(&self->txq) > 0) &&
2027 (self->window > 0) && !self->remote_busy)
2029 irlap_data_indication(self, skb, TRUE);
2031 del_timer(&self->wd_timer);
2033 irlap_next_state(self, LAP_XMIT_S);
2035 irlap_data_indication(self, skb, TRUE);
2037 irlap_wait_min_turn_around(self, &self->qos_tx);
2039 irlap_send_rr_frame(self, RSP_FRAME);
2040 self->ack_required = FALSE;
2042 irlap_start_wd_timer(self, self->wd_timeout);
2044 /* Keep the state */
2045 irlap_next_state(self, LAP_NRM_S);
2050 self->retry_count = 0;
2055 nr_status = irlap_validate_nr_received(self, info->nr);
2056 if (nr_status == NR_EXPECTED) {
2057 if ((skb_queue_len( &self->txq) > 0) &&
2058 (self->window > 0)) {
2059 self->remote_busy = FALSE;
2061 /* Update Nr received */
2062 irlap_update_nr_received(self, info->nr);
2063 del_timer(&self->wd_timer);
2065 irlap_wait_min_turn_around(self, &self->qos_tx);
2066 irlap_next_state(self, LAP_XMIT_S);
2068 self->remote_busy = FALSE;
2069 /* Update Nr received */
2070 irlap_update_nr_received(self, info->nr);
2071 irlap_wait_min_turn_around(self, &self->qos_tx);
2072 irlap_start_wd_timer(self, self->wd_timeout);
2074 /* Note : if the link is idle (this case),
2075 * we never go in XMIT_S, so we never get a
2076 * chance to process any DISCONNECT_REQUEST.
2077 * Do it now ! - Jean II */
2078 if (self->disconnect_pending) {
2080 irlap_send_rd_frame(self);
2081 irlap_flush_all_queues(self);
2083 irlap_next_state(self, LAP_SCLOSE);
2085 /* Just send back pf bit */
2086 irlap_send_rr_frame(self, RSP_FRAME);
2088 irlap_next_state(self, LAP_NRM_S);
2091 } else if (nr_status == NR_UNEXPECTED) {
2092 self->remote_busy = FALSE;
2093 irlap_update_nr_received(self, info->nr);
2094 irlap_resend_rejected_frames(self, RSP_FRAME);
2096 irlap_start_wd_timer(self, self->wd_timeout);
2099 irlap_next_state(self, LAP_NRM_S);
2101 IRDA_DEBUG(1, "%s(), invalid nr not implemented!\n",
2106 /* SNRM frame is not allowed to contain an I-field */
2108 del_timer(&self->wd_timer);
2109 IRDA_DEBUG(1, "%s(), received SNRM cmd\n", __FUNCTION__);
2110 irlap_next_state(self, LAP_RESET_CHECK);
2112 irlap_reset_indication(self);
2115 "%s(), SNRM frame contained an I-field!\n",
2121 irlap_update_nr_received(self, info->nr);
2122 if (self->remote_busy) {
2123 irlap_wait_min_turn_around(self, &self->qos_tx);
2124 irlap_send_rr_frame(self, RSP_FRAME);
2126 irlap_resend_rejected_frames(self, RSP_FRAME);
2127 irlap_start_wd_timer(self, self->wd_timeout);
2130 irlap_update_nr_received(self, info->nr);
2131 if (self->remote_busy) {
2132 irlap_wait_min_turn_around(self, &self->qos_tx);
2133 irlap_send_rr_frame(self, RSP_FRAME);
2135 irlap_resend_rejected_frame(self, RSP_FRAME);
2136 irlap_start_wd_timer(self, self->wd_timeout);
2138 case WD_TIMER_EXPIRED:
2140 * Wait until retry_count * n matches negotiated threshold/
2141 * disconnect time (note 2 in IrLAP p. 82)
2143 * Similar to irlap_state_nrm_p() -> FINAL_TIMER_EXPIRED
2144 * Note : self->wd_timeout = (self->final_timeout * 2),
2145 * which explain why we use (self->N2 / 2) here !!!
2148 IRDA_DEBUG(1, "%s(), retry_count = %d\n", __FUNCTION__,
2151 if (self->retry_count < (self->N2 / 2)) {
2152 /* No retry, just wait for primary */
2153 irlap_start_wd_timer(self, self->wd_timeout);
2154 self->retry_count++;
2156 if((self->retry_count % (self->N1 / 2)) == 0)
2157 irlap_status_indication(self,
2158 STATUS_NO_ACTIVITY);
2160 irlap_apply_default_connection_parameters(self);
2162 /* Always switch state before calling upper layers */
2163 irlap_next_state(self, LAP_NDM);
2164 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
2168 /* Always switch state before calling upper layers */
2169 irlap_next_state(self, LAP_NDM);
2171 /* Send disconnect response */
2172 irlap_wait_min_turn_around(self, &self->qos_tx);
2173 irlap_send_ua_response_frame(self, NULL);
2175 del_timer(&self->wd_timer);
2176 irlap_flush_all_queues(self);
2177 /* Set default link parameters */
2178 irlap_apply_default_connection_parameters(self);
2180 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2182 case RECV_DISCOVERY_XID_CMD:
2183 irlap_wait_min_turn_around(self, &self->qos_tx);
2184 irlap_send_rr_frame(self, RSP_FRAME);
2185 self->ack_required = TRUE;
2186 irlap_start_wd_timer(self, self->wd_timeout);
2187 irlap_next_state(self, LAP_NRM_S);
2191 /* Remove test frame header (only LAP header in NRM) */
2192 skb_pull(skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
2194 irlap_wait_min_turn_around(self, &self->qos_tx);
2195 irlap_start_wd_timer(self, self->wd_timeout);
2197 /* Send response (info will be copied) */
2198 irlap_send_test_frame(self, self->caddr, info->daddr, skb);
2201 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __FUNCTION__,
2202 event, irlap_event[event]);
2211 * Function irlap_state_sclose (self, event, skb, info)
2213 static int irlap_state_sclose(struct irlap_cb *self, IRLAP_EVENT event,
2214 struct sk_buff *skb, struct irlap_info *info)
2218 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
2220 ASSERT(self != NULL, return -ENODEV;);
2221 ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
2225 /* Always switch state before calling upper layers */
2226 irlap_next_state(self, LAP_NDM);
2228 /* Send disconnect response */
2229 irlap_wait_min_turn_around(self, &self->qos_tx);
2230 irlap_send_ua_response_frame(self, NULL);
2232 del_timer(&self->wd_timer);
2233 /* Set default link parameters */
2234 irlap_apply_default_connection_parameters(self);
2236 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2239 /* IrLAP-1.1 p.82: in SCLOSE, S and I type RSP frames
2240 * shall take us down into default NDM state, like DM_RSP
2247 /* Always switch state before calling upper layers */
2248 irlap_next_state(self, LAP_NDM);
2250 del_timer(&self->wd_timer);
2251 irlap_apply_default_connection_parameters(self);
2253 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2255 case WD_TIMER_EXPIRED:
2256 /* Always switch state before calling upper layers */
2257 irlap_next_state(self, LAP_NDM);
2259 irlap_apply_default_connection_parameters(self);
2261 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2264 /* IrLAP-1.1 p.82: in SCLOSE, basically any received frame
2265 * with pf=1 shall restart the wd-timer and resend the rd:rsp
2267 if (info != NULL && info->pf) {
2268 del_timer(&self->wd_timer);
2269 irlap_wait_min_turn_around(self, &self->qos_tx);
2270 irlap_send_rd_frame(self);
2271 irlap_start_wd_timer(self, self->wd_timeout);
2272 break; /* stay in SCLOSE */
2275 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __FUNCTION__,
2276 event, irlap_event[event]);
2285 static int irlap_state_reset_check( struct irlap_cb *self, IRLAP_EVENT event,
2286 struct sk_buff *skb,
2287 struct irlap_info *info)
2291 IRDA_DEBUG(1, "%s(), event=%s\n", __FUNCTION__, irlap_event[event]);
2293 ASSERT(self != NULL, return -ENODEV;);
2294 ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
2297 case RESET_RESPONSE:
2298 irlap_send_ua_response_frame(self, &self->qos_rx);
2299 irlap_initiate_connection_state(self);
2300 irlap_start_wd_timer(self, WD_TIMEOUT);
2301 irlap_flush_all_queues(self);
2303 irlap_next_state(self, LAP_NRM_S);
2305 case DISCONNECT_REQUEST:
2306 irlap_wait_min_turn_around(self, &self->qos_tx);
2307 irlap_send_rd_frame(self);
2308 irlap_start_wd_timer(self, WD_TIMEOUT);
2309 irlap_next_state(self, LAP_SCLOSE);
2312 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __FUNCTION__,
2313 event, irlap_event[event]);