1 /*********************************************************************
3 * Filename: irlap_event.c
5 * Description: IrLAP state machine implementation
6 * Status: Experimental.
7 * Author: Dag Brattli <dag@brattli.net>
8 * Created at: Sat Aug 16 00:59:29 1997
9 * Modified at: Sat Dec 25 21:07:57 1999
10 * Modified by: Dag Brattli <dag@brattli.net>
12 * Copyright (c) 1998-2000 Dag Brattli <dag@brattli.net>,
13 * Copyright (c) 1998 Thomas Davis <ratbert@radiks.net>
14 * All Rights Reserved.
15 * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of
20 * the License, or (at your option) any later version.
22 * Neither Dag Brattli nor University of Tromsø admit liability nor
23 * provide warranty for any of this software. This material is
24 * provided "AS-IS" and at no charge.
26 ********************************************************************/
28 #include <linux/string.h>
29 #include <linux/kernel.h>
30 #include <linux/delay.h>
31 #include <linux/skbuff.h>
32 #include <linux/slab.h>
34 #include <net/irda/irda.h>
35 #include <net/irda/irlap_event.h>
37 #include <net/irda/timer.h>
38 #include <net/irda/irlap.h>
39 #include <net/irda/irlap_frame.h>
40 #include <net/irda/qos.h>
41 #include <net/irda/parameters.h>
42 #include <net/irda/irlmp.h> /* irlmp_flow_indication(), ... */
44 #include <net/irda/irda_device.h>
46 #ifdef CONFIG_IRDA_FAST_RR
47 int sysctl_fast_poll_increase = 50;
50 static int irlap_state_ndm (struct irlap_cb *self, IRLAP_EVENT event,
51 struct sk_buff *skb, struct irlap_info *info);
52 static int irlap_state_query (struct irlap_cb *self, IRLAP_EVENT event,
53 struct sk_buff *skb, struct irlap_info *info);
54 static int irlap_state_reply (struct irlap_cb *self, IRLAP_EVENT event,
55 struct sk_buff *skb, struct irlap_info *info);
56 static int irlap_state_conn (struct irlap_cb *self, IRLAP_EVENT event,
57 struct sk_buff *skb, struct irlap_info *info);
58 static int irlap_state_setup (struct irlap_cb *self, IRLAP_EVENT event,
59 struct sk_buff *skb, struct irlap_info *info);
60 static int irlap_state_offline(struct irlap_cb *self, IRLAP_EVENT event,
61 struct sk_buff *skb, struct irlap_info *info);
62 static int irlap_state_xmit_p (struct irlap_cb *self, IRLAP_EVENT event,
63 struct sk_buff *skb, struct irlap_info *info);
64 static int irlap_state_pclose (struct irlap_cb *self, IRLAP_EVENT event,
65 struct sk_buff *skb, struct irlap_info *info);
66 static int irlap_state_nrm_p (struct irlap_cb *self, IRLAP_EVENT event,
67 struct sk_buff *skb, struct irlap_info *info);
68 static int irlap_state_reset_wait(struct irlap_cb *self, IRLAP_EVENT event,
69 struct sk_buff *skb, struct irlap_info *info);
70 static int irlap_state_reset (struct irlap_cb *self, IRLAP_EVENT event,
71 struct sk_buff *skb, struct irlap_info *info);
72 static int irlap_state_nrm_s (struct irlap_cb *self, IRLAP_EVENT event,
73 struct sk_buff *skb, struct irlap_info *info);
74 static int irlap_state_xmit_s (struct irlap_cb *self, IRLAP_EVENT event,
75 struct sk_buff *skb, struct irlap_info *info);
76 static int irlap_state_sclose (struct irlap_cb *self, IRLAP_EVENT event,
77 struct sk_buff *skb, struct irlap_info *info);
78 static int irlap_state_reset_check(struct irlap_cb *, IRLAP_EVENT event,
79 struct sk_buff *, struct irlap_info *);
81 #ifdef CONFIG_IRDA_DEBUG
82 static const char *const irlap_event[] = {
92 "RECV_DISCOVERY_XID_CMD",
93 "RECV_DISCOVERY_XID_RSP",
113 "SLOT_TIMER_EXPIRED",
114 "QUERY_TIMER_EXPIRED",
115 "FINAL_TIMER_EXPIRED",
116 "POLL_TIMER_EXPIRED",
117 "DISCOVERY_TIMER_EXPIRED",
119 "BACKOFF_TIMER_EXPIRED",
120 "MEDIA_BUSY_TIMER_EXPIRED",
122 #endif /* CONFIG_IRDA_DEBUG */
124 const char *const irlap_state[] = {
142 static int (*state[])(struct irlap_cb *self, IRLAP_EVENT event,
143 struct sk_buff *skb, struct irlap_info *info) =
154 irlap_state_reset_wait,
159 irlap_state_reset_check,
163 * Function irda_poll_timer_expired (data)
165 * Poll timer has expired. Normally we must now send a RR frame to the
168 static void irlap_poll_timer_expired(void *data)
170 struct irlap_cb *self = (struct irlap_cb *) data;
172 IRDA_ASSERT(self != NULL, return;);
173 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
175 irlap_do_event(self, POLL_TIMER_EXPIRED, NULL, NULL);
179 * Calculate and set time before we will have to send back the pf bit
180 * to the peer. Use in primary.
181 * Make sure that state is XMIT_P/XMIT_S when calling this function
182 * (and that nobody messed up with the state). - Jean II
184 static void irlap_start_poll_timer(struct irlap_cb *self, int timeout)
186 IRDA_ASSERT(self != NULL, return;);
187 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
189 #ifdef CONFIG_IRDA_FAST_RR
191 * Send out the RR frames faster if our own transmit queue is empty, or
192 * if the peer is busy. The effect is a much faster conversation
194 if (skb_queue_empty(&self->txq) || self->remote_busy) {
195 if (self->fast_RR == TRUE) {
197 * Assert that the fast poll timer has not reached the
198 * normal poll timer yet
200 if (self->fast_RR_timeout < timeout) {
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", __func__, 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", __func__,
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", __func__,
264 skb_queue_len(&self->txq));
266 if (!skb_queue_empty(&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_state_ndm (event, skb, frame)
323 * NDM (Normal Disconnected Mode) state
326 static int irlap_state_ndm(struct irlap_cb *self, IRLAP_EVENT event,
327 struct sk_buff *skb, struct irlap_info *info)
329 discovery_t *discovery_rsp;
332 IRDA_ASSERT(self != NULL, return -1;);
333 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
336 case CONNECT_REQUEST:
337 IRDA_ASSERT(self->netdev != NULL, return -1;);
339 if (self->media_busy) {
340 /* Note : this will never happen, because we test
341 * media busy in irlap_connect_request() and
342 * postpone the event... - Jean II */
343 IRDA_DEBUG(0, "%s(), CONNECT_REQUEST: media busy!\n",
346 /* Always switch state before calling upper layers */
347 irlap_next_state(self, LAP_NDM);
349 irlap_disconnect_indication(self, LAP_MEDIA_BUSY);
351 irlap_send_snrm_frame(self, &self->qos_rx);
353 /* Start Final-bit timer */
354 irlap_start_final_timer(self, self->final_timeout);
356 self->retry_count = 0;
357 irlap_next_state(self, LAP_SETUP);
361 /* Check if the frame contains and I field */
363 self->daddr = info->daddr;
364 self->caddr = info->caddr;
366 irlap_next_state(self, LAP_CONN);
368 irlap_connect_indication(self, skb);
370 IRDA_DEBUG(0, "%s(), SNRM frame does not "
371 "contain an I field!\n", __func__);
374 case DISCOVERY_REQUEST:
375 IRDA_ASSERT(info != NULL, return -1;);
377 if (self->media_busy) {
378 IRDA_DEBUG(1, "%s(), DISCOVERY_REQUEST: media busy!\n",
380 /* irlap->log.condition = MEDIA_BUSY; */
382 /* This will make IrLMP try again */
383 irlap_discovery_confirm(self, NULL);
384 /* Note : the discovery log is not cleaned up here,
385 * it will be done in irlap_discovery_request()
392 irlap_send_discovery_xid_frame(self, info->S, info->s, TRUE,
394 self->frame_sent = FALSE;
397 irlap_start_slot_timer(self, self->slot_timeout);
398 irlap_next_state(self, LAP_QUERY);
400 case RECV_DISCOVERY_XID_CMD:
401 IRDA_ASSERT(info != NULL, return -1;);
403 /* Assert that this is not the final slot */
404 if (info->s <= info->S) {
405 self->slot = irlap_generate_rand_time_slot(info->S,
407 if (self->slot == info->s) {
408 discovery_rsp = irlmp_get_discovery_response();
409 discovery_rsp->data.daddr = info->daddr;
411 irlap_send_discovery_xid_frame(self, info->S,
415 self->frame_sent = TRUE;
417 self->frame_sent = FALSE;
420 * Go to reply state until end of discovery to
421 * inhibit our own transmissions. Set the timer
422 * to not stay forever there... Jean II
424 irlap_start_query_timer(self, info->S, info->s);
425 irlap_next_state(self, LAP_REPLY);
427 /* This is the final slot. How is it possible ?
428 * This would happen is both discoveries are just slightly
429 * offset (if they are in sync, all packets are lost).
430 * Most often, all the discovery requests will be received
431 * in QUERY state (see my comment there), except for the
432 * last frame that will come here.
433 * The big trouble when it happen is that active discovery
434 * doesn't happen, because nobody answer the discoveries
435 * frame of the other guy, so the log shows up empty.
436 * What should we do ?
437 * Not much. It's too late to answer those discovery frames,
438 * so we just pass the info to IrLMP who will put it in the
439 * log (and post an event).
440 * Another cause would be devices that do discovery much
441 * slower than us, however the latest fixes should minimise
445 IRDA_DEBUG(1, "%s(), Receiving final discovery request, missed the discovery slots :-(\n", __func__);
447 /* Last discovery request -> in the log */
448 irlap_discovery_indication(self, info->discovery);
451 case MEDIA_BUSY_TIMER_EXPIRED:
452 /* A bunch of events may be postponed because the media is
453 * busy (usually immediately after we close a connection),
454 * or while we are doing discovery (state query/reply).
455 * In all those cases, the media busy flag will be cleared
456 * when it's OK for us to process those postponed events.
457 * This event is not mentioned in the state machines in the
458 * IrLAP spec. It's because they didn't consider Ultra and
459 * postponing connection request is optional.
461 #ifdef CONFIG_IRDA_ULTRA
462 /* Send any pending Ultra frames if any */
463 if (!skb_queue_empty(&self->txq_ultra)) {
464 /* We don't send the frame, just post an event.
465 * Also, previously this code was in timer.c...
467 ret = (*state[self->state])(self, SEND_UI_FRAME,
470 #endif /* CONFIG_IRDA_ULTRA */
471 /* Check if we should try to connect.
472 * This code was previously in irlap_do_event() */
473 if (self->connect_pending) {
474 self->connect_pending = FALSE;
476 /* This one *should* not pend in this state, except
477 * if a socket try to connect and immediately
478 * disconnect. - clear - Jean II */
479 if (self->disconnect_pending)
480 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
482 ret = (*state[self->state])(self,
485 self->disconnect_pending = FALSE;
487 /* Note : one way to test if this code works well (including
488 * media busy and small busy) is to create a user space
489 * application generating an Ultra packet every 3.05 sec (or
490 * 2.95 sec) and to see how it interact with discovery.
491 * It's fairly easy to check that no packet is lost, that the
492 * packets are postponed during discovery and that after
493 * discovery indication you have a 100ms "gap".
494 * As connection request and Ultra are now processed the same
495 * way, this avoid the tedious job of trying IrLAP connection
496 * in all those cases...
499 #ifdef CONFIG_IRDA_ULTRA
503 /* Only allowed to repeat an operation twice */
504 for (i=0; ((i<2) && (self->media_busy == FALSE)); i++) {
505 skb = skb_dequeue(&self->txq_ultra);
507 irlap_send_ui_frame(self, skb, CBROADCAST,
511 /* irlap_send_ui_frame() won't increase skb reference
512 * count, so no dev_kfree_skb() - Jean II */
515 /* Force us to listen 500 ms again */
516 irda_device_set_media_busy(self->netdev, TRUE);
521 /* Only accept broadcast frames in NDM mode */
522 if (info->caddr != CBROADCAST) {
523 IRDA_DEBUG(0, "%s(), not a broadcast frame!\n",
526 irlap_unitdata_indication(self, skb);
528 #endif /* CONFIG_IRDA_ULTRA */
530 /* Remove test frame header */
531 skb_pull(skb, sizeof(struct test_frame));
534 * Send response. This skb will not be sent out again, and
535 * will only be used to send out the same info as the cmd
537 irlap_send_test_frame(self, CBROADCAST, info->daddr, skb);
540 IRDA_DEBUG(0, "%s() not implemented!\n", __func__);
543 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __func__,
553 * Function irlap_state_query (event, skb, info)
558 static int irlap_state_query(struct irlap_cb *self, IRLAP_EVENT event,
559 struct sk_buff *skb, struct irlap_info *info)
563 IRDA_ASSERT(self != NULL, return -1;);
564 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
567 case RECV_DISCOVERY_XID_RSP:
568 IRDA_ASSERT(info != NULL, return -1;);
569 IRDA_ASSERT(info->discovery != NULL, return -1;);
571 IRDA_DEBUG(4, "%s(), daddr=%08x\n", __func__,
572 info->discovery->data.daddr);
574 if (!self->discovery_log) {
575 IRDA_WARNING("%s: discovery log is gone! "
576 "maybe the discovery timeout has been set"
577 " too short?\n", __func__);
580 hashbin_insert(self->discovery_log,
581 (irda_queue_t *) info->discovery,
582 info->discovery->data.daddr, NULL);
585 /* irlap_next_state(self, LAP_QUERY); */
588 case RECV_DISCOVERY_XID_CMD:
589 /* Yes, it is possible to receive those frames in this mode.
590 * Note that most often the last discovery request won't
591 * occur here but in NDM state (see my comment there).
592 * What should we do ?
593 * Not much. We are currently performing our own discovery,
594 * therefore we can't answer those frames. We don't want
595 * to change state either. We just pass the info to
596 * IrLMP who will put it in the log (and post an event).
600 IRDA_ASSERT(info != NULL, return -1;);
602 IRDA_DEBUG(1, "%s(), Receiving discovery request (s = %d) while performing discovery :-(\n", __func__, info->s);
604 /* Last discovery request ? */
606 irlap_discovery_indication(self, info->discovery);
608 case SLOT_TIMER_EXPIRED:
610 * Wait a little longer if we detect an incoming frame. This
611 * is not mentioned in the spec, but is a good thing to do,
612 * since we want to work even with devices that violate the
613 * timing requirements.
615 if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
616 IRDA_DEBUG(2, "%s(), device is slow to answer, "
617 "waiting some more!\n", __func__);
618 irlap_start_slot_timer(self, msecs_to_jiffies(10));
619 self->add_wait = TRUE;
622 self->add_wait = FALSE;
624 if (self->s < self->S) {
625 irlap_send_discovery_xid_frame(self, self->S,
627 self->discovery_cmd);
629 irlap_start_slot_timer(self, self->slot_timeout);
632 irlap_next_state(self, LAP_QUERY);
634 /* This is the final slot! */
635 irlap_send_discovery_xid_frame(self, self->S, 0xff,
637 self->discovery_cmd);
639 /* Always switch state before calling upper layers */
640 irlap_next_state(self, LAP_NDM);
643 * We are now finished with the discovery procedure,
644 * so now we must return the results
646 irlap_discovery_confirm(self, self->discovery_log);
648 /* IrLMP should now have taken care of the log */
649 self->discovery_log = NULL;
653 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __func__,
663 * Function irlap_state_reply (self, event, skb, info)
665 * REPLY, we have received a XID discovery frame from a device and we
666 * are waiting for the right time slot to send a response XID frame
669 static int irlap_state_reply(struct irlap_cb *self, IRLAP_EVENT event,
670 struct sk_buff *skb, struct irlap_info *info)
672 discovery_t *discovery_rsp;
675 IRDA_DEBUG(4, "%s()\n", __func__);
677 IRDA_ASSERT(self != NULL, return -1;);
678 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
681 case QUERY_TIMER_EXPIRED:
682 IRDA_DEBUG(0, "%s(), QUERY_TIMER_EXPIRED <%ld>\n",
684 irlap_next_state(self, LAP_NDM);
686 case RECV_DISCOVERY_XID_CMD:
687 IRDA_ASSERT(info != NULL, return -1;);
689 if (info->s == 0xff) {
690 del_timer(&self->query_timer);
692 /* info->log.condition = REMOTE; */
694 /* Always switch state before calling upper layers */
695 irlap_next_state(self, LAP_NDM);
697 irlap_discovery_indication(self, info->discovery);
699 /* If it's our slot, send our reply */
700 if ((info->s >= self->slot) && (!self->frame_sent)) {
701 discovery_rsp = irlmp_get_discovery_response();
702 discovery_rsp->data.daddr = info->daddr;
704 irlap_send_discovery_xid_frame(self, info->S,
709 self->frame_sent = TRUE;
711 /* Readjust our timer to accommodate devices
712 * doing faster or slower discovery than us...
714 irlap_start_query_timer(self, info->S, info->s);
717 //irlap_next_state(self, LAP_REPLY);
721 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __func__,
722 event, irlap_event[event]);
731 * Function irlap_state_conn (event, skb, info)
733 * CONN, we have received a SNRM command and is waiting for the upper
734 * layer to accept or refuse connection
737 static int irlap_state_conn(struct irlap_cb *self, IRLAP_EVENT event,
738 struct sk_buff *skb, struct irlap_info *info)
742 IRDA_DEBUG(4, "%s(), event=%s\n", __func__, irlap_event[ event]);
744 IRDA_ASSERT(self != NULL, return -1;);
745 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
748 case CONNECT_RESPONSE:
749 skb_pull(skb, sizeof(struct snrm_frame));
751 IRDA_ASSERT(self->netdev != NULL, return -1;);
753 irlap_qos_negotiate(self, skb);
755 irlap_initiate_connection_state(self);
758 * Applying the parameters now will make sure we change speed
759 * *after* we have sent the next frame
761 irlap_apply_connection_parameters(self, FALSE);
764 * Sending this frame will force a speed change after it has
765 * been sent (i.e. the frame will be sent at 9600).
767 irlap_send_ua_response_frame(self, &self->qos_rx);
771 * We are allowed to send two frames, but this may increase
772 * the connect latency, so lets not do it for now.
774 /* This is full of good intentions, but doesn't work in
776 * After sending the first UA response, we switch the
777 * dongle to the negotiated speed, which is usually
778 * different than 9600 kb/s.
779 * From there, there is two solutions :
780 * 1) The other end has received the first UA response :
781 * it will set up the connection, move to state LAP_NRM_P,
782 * and will ignore and drop the second UA response.
783 * Actually, it's even worse : the other side will almost
784 * immediately send a RR that will likely collide with the
785 * UA response (depending on negotiated turnaround).
786 * 2) The other end has not received the first UA response,
787 * will stay at 9600 and will never see the second UA response.
789 irlap_send_ua_response_frame(self, &self->qos_rx);
793 * The WD-timer could be set to the duration of the P-timer
794 * for this case, but it is recommended to use twice the
795 * value (note 3 IrLAP p. 60).
797 irlap_start_wd_timer(self, self->wd_timeout);
798 irlap_next_state(self, LAP_NRM_S);
801 case RECV_DISCOVERY_XID_CMD:
802 IRDA_DEBUG(3, "%s(), event RECV_DISCOVER_XID_CMD!\n",
804 irlap_next_state(self, LAP_NDM);
807 case DISCONNECT_REQUEST:
808 IRDA_DEBUG(0, "%s(), Disconnect request!\n", __func__);
809 irlap_send_dm_frame(self);
810 irlap_next_state( self, LAP_NDM);
811 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
814 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __func__,
815 event, irlap_event[event]);
825 * Function irlap_state_setup (event, skb, frame)
827 * SETUP state, The local layer has transmitted a SNRM command frame to
828 * a remote peer layer and is awaiting a reply .
831 static int irlap_state_setup(struct irlap_cb *self, IRLAP_EVENT event,
832 struct sk_buff *skb, struct irlap_info *info)
836 IRDA_DEBUG(4, "%s()\n", __func__);
838 IRDA_ASSERT(self != NULL, return -1;);
839 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
842 case FINAL_TIMER_EXPIRED:
843 if (self->retry_count < self->N3) {
845 * Perform random backoff, Wait a random number of time units, minimum
846 * duration half the time taken to transmitt a SNRM frame, maximum duration
847 * 1.5 times the time taken to transmit a SNRM frame. So this time should
848 * between 15 msecs and 45 msecs.
850 irlap_start_backoff_timer(self, msecs_to_jiffies(20 +
853 /* Always switch state before calling upper layers */
854 irlap_next_state(self, LAP_NDM);
856 irlap_disconnect_indication(self, LAP_FOUND_NONE);
859 case BACKOFF_TIMER_EXPIRED:
860 irlap_send_snrm_frame(self, &self->qos_rx);
861 irlap_start_final_timer(self, self->final_timeout);
865 IRDA_DEBUG(4, "%s(), SNRM battle!\n", __func__);
867 IRDA_ASSERT(skb != NULL, return 0;);
868 IRDA_ASSERT(info != NULL, return 0;);
871 * The device with the largest device address wins the battle
872 * (both have sent a SNRM command!)
874 if (info &&(info->daddr > self->saddr)) {
875 del_timer(&self->final_timer);
876 irlap_initiate_connection_state(self);
878 IRDA_ASSERT(self->netdev != NULL, return -1;);
880 skb_pull(skb, sizeof(struct snrm_frame));
882 irlap_qos_negotiate(self, skb);
884 /* Send UA frame and then change link settings */
885 irlap_apply_connection_parameters(self, FALSE);
886 irlap_send_ua_response_frame(self, &self->qos_rx);
888 irlap_next_state(self, LAP_NRM_S);
889 irlap_connect_confirm(self, skb);
892 * The WD-timer could be set to the duration of the
893 * P-timer for this case, but it is recommended
894 * to use twice the value (note 3 IrLAP p. 60).
896 irlap_start_wd_timer(self, self->wd_timeout);
898 /* We just ignore the other device! */
899 irlap_next_state(self, LAP_SETUP);
904 del_timer(&self->final_timer);
906 /* Initiate connection state */
907 irlap_initiate_connection_state(self);
909 /* Negotiate connection parameters */
910 IRDA_ASSERT(skb->len > 10, return -1;);
912 skb_pull(skb, sizeof(struct ua_frame));
914 IRDA_ASSERT(self->netdev != NULL, return -1;);
916 irlap_qos_negotiate(self, skb);
918 /* Set the new link setting *now* (before the rr frame) */
919 irlap_apply_connection_parameters(self, TRUE);
920 self->retry_count = 0;
922 /* Wait for turnaround time to give a chance to the other
923 * device to be ready to receive us.
924 * Note : the time to switch speed is typically larger
925 * than the turnaround time, but as we don't have the other
926 * side speed switch time, that's our best guess...
928 irlap_wait_min_turn_around(self, &self->qos_tx);
930 /* This frame will actually be sent at the new speed */
931 irlap_send_rr_frame(self, CMD_FRAME);
933 /* The timer is set to half the normal timer to quickly
934 * detect a failure to negotiate the new connection
935 * parameters. IrLAP 6.11.3.2, note 3.
936 * Note that currently we don't process this failure
937 * properly, as we should do a quick disconnect.
939 irlap_start_final_timer(self, self->final_timeout/2);
940 irlap_next_state(self, LAP_NRM_P);
942 irlap_connect_confirm(self, skb);
944 case RECV_DM_RSP: /* FALLTHROUGH */
946 del_timer(&self->final_timer);
947 irlap_next_state(self, LAP_NDM);
949 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
952 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __func__,
953 event, irlap_event[event]);
962 * Function irlap_state_offline (self, event, skb, info)
964 * OFFLINE state, not used for now!
967 static int irlap_state_offline(struct irlap_cb *self, IRLAP_EVENT event,
968 struct sk_buff *skb, struct irlap_info *info)
970 IRDA_DEBUG( 0, "%s(), Unknown event\n", __func__);
976 * Function irlap_state_xmit_p (self, event, skb, info)
978 * XMIT, Only the primary station has right to transmit, and we
979 * therefore do not expect to receive any transmissions from other
983 static int irlap_state_xmit_p(struct irlap_cb *self, IRLAP_EVENT event,
984 struct sk_buff *skb, struct irlap_info *info)
991 * Only send frame if send-window > 0.
993 if ((self->window > 0) && (!self->remote_busy)) {
995 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
996 struct sk_buff *skb_next;
998 /* With DYNAMIC_WINDOW, we keep the window size
999 * maximum, and adapt on the packets we are sending.
1000 * At 115k, we can send only 2 packets of 2048 bytes
1001 * in a 500 ms turnaround. Without this option, we
1002 * would always limit the window to 2. With this
1003 * option, if we send smaller packets, we can send
1004 * up to 7 of them (always depending on QoS).
1007 /* Look at the next skb. This is safe, as we are
1008 * the only consumer of the Tx queue (if we are not,
1009 * we have other problems) - Jean II */
1010 skb_next = skb_peek(&self->txq);
1012 /* Check if a subsequent skb exist and would fit in
1013 * the current window (with respect to turnaround
1015 * This allow us to properly mark the current packet
1016 * with the pf bit, to avoid falling back on the
1017 * second test below, and avoid waiting the
1018 * end of the window and sending a extra RR.
1019 * Note : (skb_next != NULL) <=> (skb_queue_len() > 0)
1021 nextfit = ((skb_next != NULL) &&
1022 ((skb_next->len + skb->len) <=
1026 * The current packet may not fit ! Because of test
1027 * above, this should not happen any more !!!
1028 * Test if we have transmitted more bytes over the
1029 * link than its possible to do with the current
1030 * speed and turn-around-time.
1032 if((!nextfit) && (skb->len > self->bytes_left)) {
1033 IRDA_DEBUG(0, "%s(), Not allowed to transmit"
1034 " more bytes!\n", __func__);
1035 /* Requeue the skb */
1036 skb_queue_head(&self->txq, skb_get(skb));
1038 * We should switch state to LAP_NRM_P, but
1039 * that is not possible since we must be sure
1040 * that we poll the other side. Since we have
1041 * used up our time, the poll timer should
1042 * trigger anyway now, so we just wait for it
1046 * Sorry, but that's not totally true. If
1047 * we send 2000B packets, we may wait another
1048 * 1000B until our turnaround expire. That's
1049 * why we need to be proactive in avoiding
1050 * coming here. - Jean II
1055 /* Subtract space used by this skb */
1056 self->bytes_left -= skb->len;
1057 #else /* CONFIG_IRDA_DYNAMIC_WINDOW */
1058 /* Window has been adjusted for the max packet
1059 * size, so much simpler... - Jean II */
1060 nextfit = !skb_queue_empty(&self->txq);
1061 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1063 * Send data with poll bit cleared only if window > 1
1064 * and there is more frames after this one to be sent
1066 if ((self->window > 1) && (nextfit)) {
1067 /* More packet to send in current window */
1068 irlap_send_data_primary(self, skb);
1069 irlap_next_state(self, LAP_XMIT_P);
1071 /* Final packet of window */
1072 irlap_send_data_primary_poll(self, skb);
1075 * Make sure state machine does not try to send
1080 #ifdef CONFIG_IRDA_FAST_RR
1081 /* Peer may want to reply immediately */
1082 self->fast_RR = FALSE;
1083 #endif /* CONFIG_IRDA_FAST_RR */
1085 IRDA_DEBUG(4, "%s(), Unable to send! remote busy?\n",
1087 skb_queue_head(&self->txq, skb_get(skb));
1090 * The next ret is important, because it tells
1091 * irlap_next_state _not_ to deliver more frames
1096 case POLL_TIMER_EXPIRED:
1097 IRDA_DEBUG(3, "%s(), POLL_TIMER_EXPIRED <%ld>\n",
1099 irlap_send_rr_frame(self, CMD_FRAME);
1100 /* Return to NRM properly - Jean II */
1101 self->window = self->window_size;
1102 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1103 /* Allowed to transmit a maximum number of bytes again. */
1104 self->bytes_left = self->line_capacity;
1105 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1106 irlap_start_final_timer(self, self->final_timeout);
1107 irlap_next_state(self, LAP_NRM_P);
1109 case DISCONNECT_REQUEST:
1110 del_timer(&self->poll_timer);
1111 irlap_wait_min_turn_around(self, &self->qos_tx);
1112 irlap_send_disc_frame(self);
1113 irlap_flush_all_queues(self);
1114 irlap_start_final_timer(self, self->final_timeout);
1115 self->retry_count = 0;
1116 irlap_next_state(self, LAP_PCLOSE);
1119 /* Nothing to do, irlap_do_event() will send the packet
1120 * when we return... - Jean II */
1123 IRDA_DEBUG(0, "%s(), Unknown event %s\n",
1124 __func__, irlap_event[event]);
1133 * Function irlap_state_pclose (event, skb, info)
1137 static int irlap_state_pclose(struct irlap_cb *self, IRLAP_EVENT event,
1138 struct sk_buff *skb, struct irlap_info *info)
1142 IRDA_DEBUG(1, "%s()\n", __func__);
1144 IRDA_ASSERT(self != NULL, return -1;);
1145 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
1148 case RECV_UA_RSP: /* FALLTHROUGH */
1150 del_timer(&self->final_timer);
1152 /* Set new link parameters */
1153 irlap_apply_default_connection_parameters(self);
1155 /* Always switch state before calling upper layers */
1156 irlap_next_state(self, LAP_NDM);
1158 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
1160 case FINAL_TIMER_EXPIRED:
1161 if (self->retry_count < self->N3) {
1162 irlap_wait_min_turn_around(self, &self->qos_tx);
1163 irlap_send_disc_frame(self);
1164 irlap_start_final_timer(self, self->final_timeout);
1165 self->retry_count++;
1168 irlap_apply_default_connection_parameters(self);
1170 /* Always switch state before calling upper layers */
1171 irlap_next_state(self, LAP_NDM);
1173 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1177 IRDA_DEBUG(1, "%s(), Unknown event %d\n", __func__, event);
1186 * Function irlap_state_nrm_p (self, event, skb, info)
1188 * NRM_P (Normal Response Mode as Primary), The primary station has given
1189 * permissions to a secondary station to transmit IrLAP resonse frames
1190 * (by sending a frame with the P bit set). The primary station will not
1191 * transmit any frames and is expecting to receive frames only from the
1192 * secondary to which transmission permissions has been given.
1194 static int irlap_state_nrm_p(struct irlap_cb *self, IRLAP_EVENT event,
1195 struct sk_buff *skb, struct irlap_info *info)
1202 case RECV_I_RSP: /* Optimize for the common case */
1203 if (unlikely(skb->len <= LAP_ADDR_HEADER + LAP_CTRL_HEADER)) {
1205 * Input validation check: a stir4200/mcp2150
1206 * combination sometimes results in an empty i:rsp.
1207 * This makes no sense; we can just ignore the frame
1208 * and send an rr:cmd immediately. This happens before
1209 * changing nr or ns so triggers a retransmit
1211 irlap_wait_min_turn_around(self, &self->qos_tx);
1212 irlap_send_rr_frame(self, CMD_FRAME);
1216 /* FIXME: must check for remote_busy below */
1217 #ifdef CONFIG_IRDA_FAST_RR
1219 * Reset the fast_RR so we can use the fast RR code with
1220 * full speed the next time since peer may have more frames
1223 self->fast_RR = FALSE;
1224 #endif /* CONFIG_IRDA_FAST_RR */
1225 IRDA_ASSERT( info != NULL, return -1;);
1227 ns_status = irlap_validate_ns_received(self, info->ns);
1228 nr_status = irlap_validate_nr_received(self, info->nr);
1231 * Check for expected I(nformation) frame
1233 if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
1235 /* Update Vr (next frame for us to receive) */
1236 self->vr = (self->vr + 1) % 8;
1238 /* Update Nr received, cleanup our retry queue */
1239 irlap_update_nr_received(self, info->nr);
1242 * Got expected NR, so reset the
1243 * retry_count. This is not done by IrLAP spec,
1246 self->retry_count = 0;
1247 self->ack_required = TRUE;
1249 /* poll bit cleared? */
1251 /* Keep state, do not move this line */
1252 irlap_next_state(self, LAP_NRM_P);
1254 irlap_data_indication(self, skb, FALSE);
1256 /* No longer waiting for pf */
1257 del_timer(&self->final_timer);
1259 irlap_wait_min_turn_around(self, &self->qos_tx);
1261 /* Call higher layer *before* changing state
1262 * to give them a chance to send data in the
1265 irlap_data_indication(self, skb, FALSE);
1267 /* XMIT states are the most dangerous state
1268 * to be in, because user requests are
1269 * processed directly and may change state.
1270 * On the other hand, in NDM_P, those
1271 * requests are queued and we will process
1272 * them when we return to irlap_do_event().
1275 irlap_next_state(self, LAP_XMIT_P);
1277 /* This is the last frame.
1278 * Make sure it's always called in XMIT state.
1280 irlap_start_poll_timer(self, self->poll_timeout);
1285 /* Unexpected next to send (Ns) */
1286 if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
1289 irlap_update_nr_received(self, info->nr);
1292 * Wait until the last frame before doing
1297 irlap_next_state(self, LAP_NRM_P);
1300 "%s(), missing or duplicate frame!\n",
1303 /* Update Nr received */
1304 irlap_update_nr_received(self, info->nr);
1306 irlap_wait_min_turn_around(self, &self->qos_tx);
1307 irlap_send_rr_frame(self, CMD_FRAME);
1309 self->ack_required = FALSE;
1311 irlap_start_final_timer(self, self->final_timeout);
1312 irlap_next_state(self, LAP_NRM_P);
1317 * Unexpected next to receive (Nr)
1319 if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
1322 self->vr = (self->vr + 1) % 8;
1324 /* Update Nr received */
1325 irlap_update_nr_received(self, info->nr);
1327 /* Resend rejected frames */
1328 irlap_resend_rejected_frames(self, CMD_FRAME);
1330 self->ack_required = FALSE;
1332 /* Make sure we account for the time
1333 * to transmit our frames. See comemnts
1334 * in irlap_send_data_primary_poll().
1336 irlap_start_final_timer(self, 2 * self->final_timeout);
1338 /* Keep state, do not move this line */
1339 irlap_next_state(self, LAP_NRM_P);
1341 irlap_data_indication(self, skb, FALSE);
1344 * Do not resend frames until the last
1345 * frame has arrived from the other
1346 * device. This is not documented in
1349 self->vr = (self->vr + 1) % 8;
1351 /* Update Nr received */
1352 irlap_update_nr_received(self, info->nr);
1354 self->ack_required = FALSE;
1356 /* Keep state, do not move this line!*/
1357 irlap_next_state(self, LAP_NRM_P);
1359 irlap_data_indication(self, skb, FALSE);
1364 * Unexpected next to send (Ns) and next to receive (Nr)
1365 * Not documented by IrLAP!
1367 if ((ns_status == NS_UNEXPECTED) &&
1368 (nr_status == NR_UNEXPECTED))
1370 IRDA_DEBUG(4, "%s(), unexpected nr and ns!\n",
1373 /* Resend rejected frames */
1374 irlap_resend_rejected_frames(self, CMD_FRAME);
1376 /* Give peer some time to retransmit!
1377 * But account for our own Tx. */
1378 irlap_start_final_timer(self, 2 * self->final_timeout);
1380 /* Keep state, do not move this line */
1381 irlap_next_state(self, LAP_NRM_P);
1383 /* Update Nr received */
1384 /* irlap_update_nr_received( info->nr); */
1386 self->ack_required = FALSE;
1394 if ((nr_status == NR_INVALID) || (ns_status == NS_INVALID)) {
1396 del_timer(&self->final_timer);
1398 irlap_next_state(self, LAP_RESET_WAIT);
1400 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1401 self->xmitflag = TRUE;
1403 del_timer(&self->final_timer);
1405 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1407 self->xmitflag = FALSE;
1411 IRDA_DEBUG(1, "%s(), Not implemented!\n", __func__);
1412 IRDA_DEBUG(1, "%s(), event=%s, ns_status=%d, nr_status=%d\n",
1413 __func__, irlap_event[event], ns_status, nr_status);
1416 /* Poll bit cleared? */
1418 irlap_data_indication(self, skb, TRUE);
1419 irlap_next_state(self, LAP_NRM_P);
1421 del_timer(&self->final_timer);
1422 irlap_data_indication(self, skb, TRUE);
1423 irlap_next_state(self, LAP_XMIT_P);
1424 IRDA_DEBUG(1, "%s: RECV_UI_FRAME: next state %s\n", __func__, irlap_state[self->state]);
1425 irlap_start_poll_timer(self, self->poll_timeout);
1430 * If you get a RR, the remote isn't busy anymore,
1431 * no matter what the NR
1433 self->remote_busy = FALSE;
1435 /* Stop final timer */
1436 del_timer(&self->final_timer);
1441 ret = irlap_validate_nr_received(self, info->nr);
1442 if (ret == NR_EXPECTED) {
1443 /* Update Nr received */
1444 irlap_update_nr_received(self, info->nr);
1447 * Got expected NR, so reset the retry_count. This
1448 * is not done by the IrLAP standard , which is
1451 self->retry_count = 0;
1452 irlap_wait_min_turn_around(self, &self->qos_tx);
1454 irlap_next_state(self, LAP_XMIT_P);
1456 /* Start poll timer */
1457 irlap_start_poll_timer(self, self->poll_timeout);
1458 } else if (ret == NR_UNEXPECTED) {
1459 IRDA_ASSERT(info != NULL, return -1;);
1464 /* Update Nr received */
1465 irlap_update_nr_received(self, info->nr);
1467 IRDA_DEBUG(4, "RECV_RR_FRAME: Retrans:%d, nr=%d, va=%d, "
1469 self->retry_count, info->nr, self->va,
1470 self->vs, self->vr);
1472 /* Resend rejected frames */
1473 irlap_resend_rejected_frames(self, CMD_FRAME);
1474 irlap_start_final_timer(self, self->final_timeout * 2);
1476 irlap_next_state(self, LAP_NRM_P);
1477 } else if (ret == NR_INVALID) {
1478 IRDA_DEBUG(1, "%s(), Received RR with "
1479 "invalid nr !\n", __func__);
1481 irlap_next_state(self, LAP_RESET_WAIT);
1483 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1484 self->xmitflag = TRUE;
1488 IRDA_ASSERT(info != NULL, return -1;);
1490 /* Stop final timer */
1491 del_timer(&self->final_timer);
1492 self->remote_busy = TRUE;
1494 /* Update Nr received */
1495 irlap_update_nr_received(self, info->nr);
1496 irlap_next_state(self, LAP_XMIT_P);
1498 /* Start poll timer */
1499 irlap_start_poll_timer(self, self->poll_timeout);
1502 del_timer(&self->final_timer);
1503 self->xmitflag = TRUE;
1504 irlap_next_state(self, LAP_RESET_WAIT);
1505 irlap_reset_indication(self);
1507 case FINAL_TIMER_EXPIRED:
1509 * We are allowed to wait for additional 300 ms if
1510 * final timer expires when we are in the middle
1511 * of receiving a frame (page 45, IrLAP). Check that
1512 * we only do this once for each frame.
1514 if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
1515 IRDA_DEBUG(1, "FINAL_TIMER_EXPIRED when receiving a "
1516 "frame! Waiting a little bit more!\n");
1517 irlap_start_final_timer(self, msecs_to_jiffies(300));
1520 * Don't allow this to happen one more time in a row,
1521 * or else we can get a pretty tight loop here if
1522 * if we only receive half a frame. DB.
1524 self->add_wait = TRUE;
1527 self->add_wait = FALSE;
1529 /* N2 is the disconnect timer. Until we reach it, we retry */
1530 if (self->retry_count < self->N2) {
1531 if (skb_peek(&self->wx_list) == NULL) {
1532 /* Retry sending the pf bit to the secondary */
1533 IRDA_DEBUG(4, "nrm_p: resending rr");
1534 irlap_wait_min_turn_around(self, &self->qos_tx);
1535 irlap_send_rr_frame(self, CMD_FRAME);
1537 IRDA_DEBUG(4, "nrm_p: resend frames");
1538 irlap_resend_rejected_frames(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", __func__);
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 __func__, 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", __func__, 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", __func__,
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", __func__, 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", __func__);
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 __func__, 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 therefore 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", __func__, 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", __func__);
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 /* Subtract 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", __func__);
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", __func__,
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", __func__, 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", __func__,
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", __func__);
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", __func__,
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", __func__,
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)
2230 IRDA_DEBUG(1, "%s()\n", __func__);
2232 IRDA_ASSERT(self != NULL, return -ENODEV;);
2233 IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
2237 /* Always switch state before calling upper layers */
2238 irlap_next_state(self, LAP_NDM);
2240 /* Send disconnect response */
2241 irlap_wait_min_turn_around(self, &self->qos_tx);
2242 irlap_send_ua_response_frame(self, NULL);
2244 del_timer(&self->wd_timer);
2245 /* Set default link parameters */
2246 irlap_apply_default_connection_parameters(self);
2248 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2251 /* IrLAP-1.1 p.82: in SCLOSE, S and I type RSP frames
2252 * shall take us down into default NDM state, like DM_RSP
2259 /* Always switch state before calling upper layers */
2260 irlap_next_state(self, LAP_NDM);
2262 del_timer(&self->wd_timer);
2263 irlap_apply_default_connection_parameters(self);
2265 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2267 case WD_TIMER_EXPIRED:
2268 /* Always switch state before calling upper layers */
2269 irlap_next_state(self, LAP_NDM);
2271 irlap_apply_default_connection_parameters(self);
2273 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2276 /* IrLAP-1.1 p.82: in SCLOSE, basically any received frame
2277 * with pf=1 shall restart the wd-timer and resend the rd:rsp
2279 if (info != NULL && info->pf) {
2280 del_timer(&self->wd_timer);
2281 irlap_wait_min_turn_around(self, &self->qos_tx);
2282 irlap_send_rd_frame(self);
2283 irlap_start_wd_timer(self, self->wd_timeout);
2284 break; /* stay in SCLOSE */
2287 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __func__,
2288 event, irlap_event[event]);
2296 static int irlap_state_reset_check( struct irlap_cb *self, IRLAP_EVENT event,
2297 struct sk_buff *skb,
2298 struct irlap_info *info)
2302 IRDA_DEBUG(1, "%s(), event=%s\n", __func__, irlap_event[event]);
2304 IRDA_ASSERT(self != NULL, return -ENODEV;);
2305 IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
2308 case RESET_RESPONSE:
2309 irlap_send_ua_response_frame(self, &self->qos_rx);
2310 irlap_initiate_connection_state(self);
2311 irlap_start_wd_timer(self, WD_TIMEOUT);
2312 irlap_flush_all_queues(self);
2314 irlap_next_state(self, LAP_NRM_S);
2316 case DISCONNECT_REQUEST:
2317 irlap_wait_min_turn_around(self, &self->qos_tx);
2318 irlap_send_rd_frame(self);
2319 irlap_start_wd_timer(self, WD_TIMEOUT);
2320 irlap_next_state(self, LAP_SCLOSE);
2323 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __func__,
2324 event, irlap_event[event]);