1 /*********************************************************************
3 * Filename: irlap_event.c
5 * Description: IrLAP state machine implementation
6 * Status: Experimental.
7 * Author: Dag Brattli <dag@brattli.net>
8 * Created at: Sat Aug 16 00:59:29 1997
9 * Modified at: Sat Dec 25 21:07:57 1999
10 * Modified by: Dag Brattli <dag@brattli.net>
12 * Copyright (c) 1998-2000 Dag Brattli <dag@brattli.net>,
13 * Copyright (c) 1998 Thomas Davis <ratbert@radiks.net>
14 * All Rights Reserved.
15 * Copyright (c) 2000-2001 Jean Tourrilhes <jt@hpl.hp.com>
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of
20 * the License, or (at your option) any later version.
22 * Neither Dag Brattli nor University of Tromsø admit liability nor
23 * provide warranty for any of this software. This material is
24 * provided "AS-IS" and at no charge.
26 ********************************************************************/
28 #include <linux/config.h>
29 #include <linux/string.h>
30 #include <linux/kernel.h>
31 #include <linux/delay.h>
32 #include <linux/skbuff.h>
34 #include <net/irda/irda.h>
35 #include <net/irda/irlap_event.h>
37 #include <net/irda/timer.h>
38 #include <net/irda/irlap.h>
39 #include <net/irda/irlap_frame.h>
40 #include <net/irda/qos.h>
41 #include <net/irda/parameters.h>
43 #include <net/irda/irda_device.h>
45 #if CONFIG_IRDA_FAST_RR
46 int sysctl_fast_poll_increase = 50;
49 static int irlap_state_ndm (struct irlap_cb *self, IRLAP_EVENT event,
50 struct sk_buff *skb, struct irlap_info *info);
51 static int irlap_state_query (struct irlap_cb *self, IRLAP_EVENT event,
52 struct sk_buff *skb, struct irlap_info *info);
53 static int irlap_state_reply (struct irlap_cb *self, IRLAP_EVENT event,
54 struct sk_buff *skb, struct irlap_info *info);
55 static int irlap_state_conn (struct irlap_cb *self, IRLAP_EVENT event,
56 struct sk_buff *skb, struct irlap_info *info);
57 static int irlap_state_setup (struct irlap_cb *self, IRLAP_EVENT event,
58 struct sk_buff *skb, struct irlap_info *info);
59 static int irlap_state_offline(struct irlap_cb *self, IRLAP_EVENT event,
60 struct sk_buff *skb, struct irlap_info *info);
61 static int irlap_state_xmit_p (struct irlap_cb *self, IRLAP_EVENT event,
62 struct sk_buff *skb, struct irlap_info *info);
63 static int irlap_state_pclose (struct irlap_cb *self, IRLAP_EVENT event,
64 struct sk_buff *skb, struct irlap_info *info);
65 static int irlap_state_nrm_p (struct irlap_cb *self, IRLAP_EVENT event,
66 struct sk_buff *skb, struct irlap_info *info);
67 static int irlap_state_reset_wait(struct irlap_cb *self, IRLAP_EVENT event,
68 struct sk_buff *skb, struct irlap_info *info);
69 static int irlap_state_reset (struct irlap_cb *self, IRLAP_EVENT event,
70 struct sk_buff *skb, struct irlap_info *info);
71 static int irlap_state_nrm_s (struct irlap_cb *self, IRLAP_EVENT event,
72 struct sk_buff *skb, struct irlap_info *info);
73 static int irlap_state_xmit_s (struct irlap_cb *self, IRLAP_EVENT event,
74 struct sk_buff *skb, struct irlap_info *info);
75 static int irlap_state_sclose (struct irlap_cb *self, IRLAP_EVENT event,
76 struct sk_buff *skb, struct irlap_info *info);
77 static int irlap_state_reset_check(struct irlap_cb *, IRLAP_EVENT event,
78 struct sk_buff *, struct irlap_info *);
80 #ifdef CONFIG_IRDA_DEBUG
81 static const char *irlap_event[] = {
91 "RECV_DISCOVERY_XID_CMD",
92 "RECV_DISCOVERY_XID_RSP",
112 "SLOT_TIMER_EXPIRED",
113 "QUERY_TIMER_EXPIRED",
114 "FINAL_TIMER_EXPIRED",
115 "POLL_TIMER_EXPIRED",
116 "DISCOVERY_TIMER_EXPIRED",
118 "BACKOFF_TIMER_EXPIRED",
119 "MEDIA_BUSY_TIMER_EXPIRED",
121 #endif /* CONFIG_IRDA_DEBUG */
123 const char *irlap_state[] = {
141 static int (*state[])(struct irlap_cb *self, IRLAP_EVENT event,
142 struct sk_buff *skb, struct irlap_info *info) =
153 irlap_state_reset_wait,
158 irlap_state_reset_check,
162 * Function irda_poll_timer_expired (data)
164 * Poll timer has expired. Normally we must now send a RR frame to the
167 static void irlap_poll_timer_expired(void *data)
169 struct irlap_cb *self = (struct irlap_cb *) data;
171 ASSERT(self != NULL, return;);
172 ASSERT(self->magic == LAP_MAGIC, return;);
174 irlap_do_event(self, POLL_TIMER_EXPIRED, NULL, NULL);
178 * Calculate and set time before we will have to send back the pf bit
179 * to the peer. Use in primary.
180 * Make sure that state is XMIT_P/XMIT_S when calling this function
181 * (and that nobody messed up with the state). - Jean II
183 void irlap_start_poll_timer(struct irlap_cb *self, int timeout)
185 ASSERT(self != NULL, return;);
186 ASSERT(self->magic == LAP_MAGIC, return;);
188 #ifdef CONFIG_IRDA_FAST_RR
190 * Send out the RR frames faster if our own transmit queue is empty, or
191 * if the peer is busy. The effect is a much faster conversation
193 if ((skb_queue_len(&self->txq) == 0) || (self->remote_busy)) {
194 if (self->fast_RR == TRUE) {
196 * Assert that the fast poll timer has not reached the
197 * normal poll timer yet
199 if (self->fast_RR_timeout < timeout) {
201 * FIXME: this should be a more configurable
204 self->fast_RR_timeout +=
205 (sysctl_fast_poll_increase * HZ/1000);
207 /* Use this fast(er) timeout instead */
208 timeout = self->fast_RR_timeout;
211 self->fast_RR = TRUE;
213 /* Start with just 0 ms */
214 self->fast_RR_timeout = 0;
218 self->fast_RR = FALSE;
220 IRDA_DEBUG(3, __FUNCTION__ "(), timeout=%d (%ld)\n", timeout, jiffies);
221 #endif /* CONFIG_IRDA_FAST_RR */
224 irlap_do_event(self, POLL_TIMER_EXPIRED, NULL, NULL);
226 irda_start_timer(&self->poll_timer, timeout, self,
227 irlap_poll_timer_expired);
231 * Function irlap_do_event (event, skb, info)
233 * Rushes through the state machine without any delay. If state == XMIT
234 * then send queued data frames.
236 void irlap_do_event(struct irlap_cb *self, IRLAP_EVENT event,
237 struct sk_buff *skb, struct irlap_info *info)
241 if (!self || self->magic != LAP_MAGIC)
244 IRDA_DEBUG(3, __FUNCTION__ "(), event = %s, state = %s\n",
245 irlap_event[event], irlap_state[self->state]);
247 ret = (*state[self->state])(self, event, skb, info);
250 * Check if there are any pending events that needs to be executed
252 switch (self->state) {
253 case LAP_XMIT_P: /* FALLTHROUGH */
256 * We just received the pf bit and are at the beginning
257 * of a new LAP transmit window.
258 * Check if there are any queued data frames, and do not
259 * try to disconnect link if we send any data frames, since
260 * that will change the state away form XMIT
262 IRDA_DEBUG(2, __FUNCTION__ "() : queue len = %d\n",
263 skb_queue_len(&self->txq));
265 if (skb_queue_len(&self->txq)) {
266 /* Prevent race conditions with irlap_data_request() */
267 self->local_busy = TRUE;
269 /* Theory of operation.
270 * We send frames up to when we fill the window or
271 * reach line capacity. Those frames will queue up
272 * in the device queue, and the driver will slowly
274 * After each frame that we send, we poll the higher
275 * layer for more data. It's the right time to do
276 * that because the link layer need to perform the mtt
277 * and then send the first frame, so we can afford
278 * to send a bit of time in kernel space.
279 * The explicit flow indication allow to minimise
280 * buffers (== lower latency), to avoid higher layer
281 * polling via timers (== less context switches) and
282 * to implement a crude scheduler - Jean II */
284 /* Try to send away all queued data frames */
285 while ((skb = skb_dequeue(&self->txq)) != NULL) {
287 ret = (*state[self->state])(self, SEND_I_CMD,
291 /* Poll the higher layers for one more frame */
292 irlmp_flow_indication(self->notify.instance,
296 break; /* Try again later! */
298 /* Finished transmitting */
299 self->local_busy = FALSE;
300 } else if (self->disconnect_pending) {
301 self->disconnect_pending = FALSE;
303 ret = (*state[self->state])(self, DISCONNECT_REQUEST,
309 /* case LAP_RESET_WAIT: */
310 /* case LAP_RESET_CHECK: */
317 * Function irlap_next_state (self, state)
319 * Switches state and provides debug information
322 static inline void irlap_next_state(struct irlap_cb *self, IRLAP_STATE state)
325 if (!self || self->magic != LAP_MAGIC)
328 IRDA_DEBUG(4, "next LAP state = %s\n", irlap_state[state]);
334 * Function irlap_state_ndm (event, skb, frame)
336 * NDM (Normal Disconnected Mode) state
339 static int irlap_state_ndm(struct irlap_cb *self, IRLAP_EVENT event,
340 struct sk_buff *skb, struct irlap_info *info)
342 discovery_t *discovery_rsp;
345 ASSERT(self != NULL, return -1;);
346 ASSERT(self->magic == LAP_MAGIC, return -1;);
349 case CONNECT_REQUEST:
350 ASSERT(self->netdev != NULL, return -1;);
352 if (self->media_busy) {
353 /* Note : this will never happen, because we test
354 * media busy in irlap_connect_request() and
355 * postpone the event... - Jean II */
356 IRDA_DEBUG(0, __FUNCTION__
357 "(), CONNECT_REQUEST: media busy!\n");
359 /* Always switch state before calling upper layers */
360 irlap_next_state(self, LAP_NDM);
362 irlap_disconnect_indication(self, LAP_MEDIA_BUSY);
364 irlap_send_snrm_frame(self, &self->qos_rx);
366 /* Start Final-bit timer */
367 irlap_start_final_timer(self, self->final_timeout);
369 self->retry_count = 0;
370 irlap_next_state(self, LAP_SETUP);
374 /* Check if the frame contains and I field */
376 self->daddr = info->daddr;
377 self->caddr = info->caddr;
379 irlap_next_state(self, LAP_CONN);
381 irlap_connect_indication(self, skb);
383 IRDA_DEBUG(0, __FUNCTION__ "(), SNRM frame does not "
384 "contain an I field!\n");
387 case DISCOVERY_REQUEST:
388 ASSERT(info != NULL, return -1;);
390 if (self->media_busy) {
391 IRDA_DEBUG(0, __FUNCTION__ "(), media busy!\n");
392 /* irlap->log.condition = MEDIA_BUSY; */
394 /* This will make IrLMP try again */
395 irlap_discovery_confirm(self, NULL);
396 /* Note : the discovery log is not cleaned up here,
397 * it will be done in irlap_discovery_request()
404 irlap_send_discovery_xid_frame(self, info->S, info->s, TRUE,
406 self->frame_sent = FALSE;
409 irlap_start_slot_timer(self, self->slot_timeout);
410 irlap_next_state(self, LAP_QUERY);
412 case RECV_DISCOVERY_XID_CMD:
413 ASSERT(info != NULL, return -1;);
415 /* Assert that this is not the final slot */
416 if (info->s <= info->S) {
417 self->slot = irlap_generate_rand_time_slot(info->S,
419 if (self->slot == info->s) {
420 discovery_rsp = irlmp_get_discovery_response();
421 discovery_rsp->daddr = info->daddr;
423 irlap_send_discovery_xid_frame(self, info->S,
427 self->frame_sent = TRUE;
429 self->frame_sent = FALSE;
432 * Remember to multiply the query timeout value with
433 * the number of slots used
435 irlap_start_query_timer(self, QUERY_TIMEOUT*info->S);
436 irlap_next_state(self, LAP_REPLY);
438 /* This is the final slot. How is it possible ?
439 * This would happen is both discoveries are just slightly
440 * offset (if they are in sync, all packets are lost).
441 * Most often, all the discovery requests will be received
442 * in QUERY state (see my comment there), except for the
443 * last frame that will come here.
444 * The big trouble when it happen is that active discovery
445 * doesn't happen, because nobody answer the discoveries
446 * frame of the other guy, so the log shows up empty.
447 * What should we do ?
448 * Not much. It's too late to answer those discovery frames,
449 * so we just pass the info to IrLMP who will put it in the
450 * log (and post an event).
453 IRDA_DEBUG(1, __FUNCTION__ "(), Receiving final discovery request, missed the discovery slots :-(\n");
455 /* Last discovery request -> in the log */
456 irlap_discovery_indication(self, info->discovery);
459 case MEDIA_BUSY_TIMER_EXPIRED:
460 /* A bunch of events may be postponed because the media is
461 * busy (usually immediately after we close a connection),
462 * or while we are doing discovery (state query/reply).
463 * In all those cases, the media busy flag will be cleared
464 * when it's OK for us to process those postponed events.
465 * This event is not mentioned in the state machines in the
466 * IrLAP spec. It's because they didn't consider Ultra and
467 * postponing connection request is optional.
469 #ifdef CONFIG_IRDA_ULTRA
470 /* Send any pending Ultra frames if any */
471 if (!skb_queue_empty(&self->txq_ultra)) {
472 /* We don't send the frame, just post an event.
473 * Also, previously this code was in timer.c...
475 ret = (*state[self->state])(self, SEND_UI_FRAME,
478 #endif /* CONFIG_IRDA_ULTRA */
479 /* Check if we should try to connect.
480 * This code was previously in irlap_do_event() */
481 if (self->connect_pending) {
482 self->connect_pending = FALSE;
484 /* This one *should* not pend in this state, except
485 * if a socket try to connect and immediately
486 * disconnect. - clear - Jean II */
487 if (self->disconnect_pending)
488 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
490 ret = (*state[self->state])(self,
493 self->disconnect_pending = FALSE;
495 /* Note : one way to test if this code works well (including
496 * media busy and small busy) is to create a user space
497 * application generating an Ultra packet every 3.05 sec (or
498 * 2.95 sec) and to see how it interact with discovery.
499 * It's fairly easy to check that no packet is lost, that the
500 * packets are postponed during discovery and that after
501 * discovery indication you have a 100ms "gap".
502 * As connection request and Ultra are now processed the same
503 * way, this avoid the tedious job of trying IrLAP connection
504 * in all those cases...
507 #ifdef CONFIG_IRDA_ULTRA
511 /* Only allowed to repeat an operation twice */
512 for (i=0; ((i<2) && (self->media_busy == FALSE)); i++) {
513 skb = skb_dequeue(&self->txq_ultra);
515 irlap_send_ui_frame(self, skb, CBROADCAST,
521 /* Force us to listen 500 ms again */
522 irda_device_set_media_busy(self->netdev, TRUE);
527 /* Only accept broadcast frames in NDM mode */
528 if (info->caddr != CBROADCAST) {
529 IRDA_DEBUG(0, __FUNCTION__
530 "(), not a broadcast frame!\n");
532 irlap_unitdata_indication(self, skb);
534 #endif /* CONFIG_IRDA_ULTRA */
536 /* Remove test frame header */
537 skb_pull(skb, sizeof(struct test_frame));
540 * Send response. This skb will not be sent out again, and
541 * will only be used to send out the same info as the cmd
543 irlap_send_test_frame(self, CBROADCAST, info->daddr, skb);
546 IRDA_DEBUG(0, __FUNCTION__ "() not implemented!\n");
549 IRDA_DEBUG(2, __FUNCTION__ "(), Unknown event %s\n",
559 * Function irlap_state_query (event, skb, info)
564 static int irlap_state_query(struct irlap_cb *self, IRLAP_EVENT event,
565 struct sk_buff *skb, struct irlap_info *info)
569 ASSERT(self != NULL, return -1;);
570 ASSERT(self->magic == LAP_MAGIC, return -1;);
573 case RECV_DISCOVERY_XID_RSP:
574 ASSERT(info != NULL, return -1;);
575 ASSERT(info->discovery != NULL, return -1;);
577 IRDA_DEBUG(4, __FUNCTION__ "(), daddr=%08x\n",
578 info->discovery->daddr);
580 if (!self->discovery_log) {
581 WARNING(__FUNCTION__ "(), discovery log is gone! "
582 "maybe the discovery timeout has been set to "
586 hashbin_insert(self->discovery_log,
587 (irda_queue_t *) info->discovery,
588 info->discovery->daddr, NULL);
591 /* irlap_next_state(self, LAP_QUERY); */
594 case RECV_DISCOVERY_XID_CMD:
595 /* Yes, it is possible to receive those frames in this mode.
596 * Note that most often the last discovery request won't
597 * occur here but in NDM state (see my comment there).
598 * What should we do ?
599 * Not much. We are currently performing our own discovery,
600 * therefore we can't answer those frames. We don't want
601 * to change state either. We just pass the info to
602 * IrLMP who will put it in the log (and post an event).
606 ASSERT(info != NULL, return -1;);
608 IRDA_DEBUG(1, __FUNCTION__ "(), Receiving discovery request (s = %d) while performing discovery :-(\n", info->s);
610 /* Last discovery request ? */
612 irlap_discovery_indication(self, info->discovery);
614 case SLOT_TIMER_EXPIRED:
616 * Wait a little longer if we detect an incoming frame. This
617 * is not mentioned in the spec, but is a good thing to do,
618 * since we want to work even with devices that violate the
619 * timing requirements.
621 if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
622 IRDA_DEBUG(2, __FUNCTION__
623 "(), device is slow to answer, "
624 "waiting some more!\n");
625 irlap_start_slot_timer(self, MSECS_TO_JIFFIES(10));
626 self->add_wait = TRUE;
629 self->add_wait = FALSE;
631 if (self->s < self->S) {
632 irlap_send_discovery_xid_frame(self, self->S,
634 self->discovery_cmd);
636 irlap_start_slot_timer(self, self->slot_timeout);
639 irlap_next_state(self, LAP_QUERY);
641 /* This is the final slot! */
642 irlap_send_discovery_xid_frame(self, self->S, 0xff,
644 self->discovery_cmd);
646 /* Always switch state before calling upper layers */
647 irlap_next_state(self, LAP_NDM);
650 * We are now finished with the discovery procedure,
651 * so now we must return the results
653 irlap_discovery_confirm(self, self->discovery_log);
655 /* IrLMP should now have taken care of the log */
656 self->discovery_log = NULL;
660 IRDA_DEBUG(2, __FUNCTION__ "(), Unknown event %s\n",
670 * Function irlap_state_reply (self, event, skb, info)
672 * REPLY, we have received a XID discovery frame from a device and we
673 * are waiting for the right time slot to send a response XID frame
676 static int irlap_state_reply(struct irlap_cb *self, IRLAP_EVENT event,
677 struct sk_buff *skb, struct irlap_info *info)
679 discovery_t *discovery_rsp;
682 IRDA_DEBUG(4, __FUNCTION__ "()\n");
684 ASSERT(self != NULL, return -1;);
685 ASSERT(self->magic == LAP_MAGIC, return -1;);
688 case QUERY_TIMER_EXPIRED:
689 IRDA_DEBUG(2, __FUNCTION__ "(), QUERY_TIMER_EXPIRED <%ld>\n",
691 irlap_next_state(self, LAP_NDM);
693 case RECV_DISCOVERY_XID_CMD:
694 ASSERT(info != NULL, return -1;);
696 if (info->s == 0xff) {
697 del_timer(&self->query_timer);
699 /* info->log.condition = REMOTE; */
701 /* Always switch state before calling upper layers */
702 irlap_next_state(self, LAP_NDM);
704 irlap_discovery_indication(self, info->discovery);
705 } else if ((info->s >= self->slot) && (!self->frame_sent)) {
706 discovery_rsp = irlmp_get_discovery_response();
707 discovery_rsp->daddr = info->daddr;
709 irlap_send_discovery_xid_frame(self, info->S,
713 self->frame_sent = TRUE;
714 irlap_next_state(self, LAP_REPLY);
718 IRDA_DEBUG(1, __FUNCTION__ "(), Unknown event %d, %s\n", event,
728 * Function irlap_state_conn (event, skb, info)
730 * CONN, we have received a SNRM command and is waiting for the upper
731 * layer to accept or refuse connection
734 static int irlap_state_conn(struct irlap_cb *self, IRLAP_EVENT event,
735 struct sk_buff *skb, struct irlap_info *info)
739 IRDA_DEBUG(4, __FUNCTION__ "(), event=%s\n", irlap_event[ event]);
741 ASSERT(self != NULL, return -1;);
742 ASSERT(self->magic == LAP_MAGIC, return -1;);
745 case CONNECT_RESPONSE:
746 skb_pull(skb, sizeof(struct snrm_frame));
748 ASSERT(self->netdev != NULL, return -1;);
750 irlap_qos_negotiate(self, skb);
752 irlap_initiate_connection_state(self);
755 * Applying the parameters now will make sure we change speed
756 * *after* we have sent the next frame
758 irlap_apply_connection_parameters(self, FALSE);
761 * Sending this frame will force a speed change after it has
762 * been sent (i.e. the frame will be sent at 9600).
764 irlap_send_ua_response_frame(self, &self->qos_rx);
768 * We are allowed to send two frames, but this may increase
769 * the connect latency, so lets not do it for now.
771 /* This is full of good intentions, but doesn't work in
773 * After sending the first UA response, we switch the
774 * dongle to the negociated speed, which is usually
775 * different than 9600 kb/s.
776 * From there, there is two solutions :
777 * 1) The other end has received the first UA response :
778 * it will set up the connection, move to state LAP_NRM_P,
779 * and will ignore and drop the second UA response.
780 * Actually, it's even worse : the other side will almost
781 * immediately send a RR that will likely collide with the
782 * UA response (depending on negociated turnaround).
783 * 2) The other end has not received the first UA response,
784 * will stay at 9600 and will never see the second UA response.
786 irlap_send_ua_response_frame(self, &self->qos_rx);
790 * The WD-timer could be set to the duration of the P-timer
791 * for this case, but it is recommended to use twice the
792 * value (note 3 IrLAP p. 60).
794 irlap_start_wd_timer(self, self->wd_timeout);
795 irlap_next_state(self, LAP_NRM_S);
798 case RECV_DISCOVERY_XID_CMD:
799 IRDA_DEBUG(3, __FUNCTION__
800 "(), event RECV_DISCOVER_XID_CMD!\n");
801 irlap_next_state(self, LAP_NDM);
804 case DISCONNECT_REQUEST:
805 IRDA_DEBUG(0, __FUNCTION__ "(), Disconnect request!\n");
806 irlap_send_dm_frame(self);
807 irlap_next_state( self, LAP_NDM);
808 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
811 IRDA_DEBUG(1, __FUNCTION__ "(), Unknown event %d, %s\n", event,
822 * Function irlap_state_setup (event, skb, frame)
824 * SETUP state, The local layer has transmitted a SNRM command frame to
825 * a remote peer layer and is awaiting a reply .
828 static int irlap_state_setup(struct irlap_cb *self, IRLAP_EVENT event,
829 struct sk_buff *skb, struct irlap_info *info)
833 IRDA_DEBUG(4, __FUNCTION__ "()\n");
835 ASSERT(self != NULL, return -1;);
836 ASSERT(self->magic == LAP_MAGIC, return -1;);
839 case FINAL_TIMER_EXPIRED:
840 if (self->retry_count < self->N3) {
842 * Perform random backoff, Wait a random number of time units, minimum
843 * duration half the time taken to transmitt a SNRM frame, maximum duration
844 * 1.5 times the time taken to transmit a SNRM frame. So this time should
845 * between 15 msecs and 45 msecs.
847 irlap_start_backoff_timer(self, MSECS_TO_JIFFIES(20 +
850 /* Always switch state before calling upper layers */
851 irlap_next_state(self, LAP_NDM);
853 irlap_disconnect_indication(self, LAP_FOUND_NONE);
856 case BACKOFF_TIMER_EXPIRED:
857 irlap_send_snrm_frame(self, &self->qos_rx);
858 irlap_start_final_timer(self, self->final_timeout);
862 IRDA_DEBUG(4, __FUNCTION__ "(), SNRM battle!\n");
864 ASSERT(skb != NULL, return 0;);
865 ASSERT(info != NULL, return 0;);
868 * The device with the largest device address wins the battle
869 * (both have sent a SNRM command!)
871 if (info &&(info->daddr > self->saddr)) {
872 del_timer(&self->final_timer);
873 irlap_initiate_connection_state(self);
875 ASSERT(self->netdev != NULL, return -1;);
877 skb_pull(skb, sizeof(struct snrm_frame));
879 irlap_qos_negotiate(self, skb);
881 /* Send UA frame and then change link settings */
882 irlap_apply_connection_parameters(self, FALSE);
883 irlap_send_ua_response_frame(self, &self->qos_rx);
885 irlap_next_state(self, LAP_NRM_S);
886 irlap_connect_confirm(self, skb);
889 * The WD-timer could be set to the duration of the
890 * P-timer for this case, but it is recommended
891 * to use twice the value (note 3 IrLAP p. 60).
893 irlap_start_wd_timer(self, self->wd_timeout);
895 /* We just ignore the other device! */
896 irlap_next_state(self, LAP_SETUP);
901 del_timer(&self->final_timer);
903 /* Initiate connection state */
904 irlap_initiate_connection_state(self);
906 /* Negotiate connection parameters */
907 ASSERT(skb->len > 10, return -1;);
909 skb_pull(skb, sizeof(struct ua_frame));
911 ASSERT(self->netdev != NULL, return -1;);
913 irlap_qos_negotiate(self, skb);
915 /* Set the new link setting *now* (before the rr frame) */
916 irlap_apply_connection_parameters(self, TRUE);
917 self->retry_count = 0;
919 /* Wait for turnaround time to give a chance to the other
920 * device to be ready to receive us.
921 * Note : the time to switch speed is typically larger
922 * than the turnaround time, but as we don't have the other
923 * side speed switch time, that's our best guess...
925 irlap_wait_min_turn_around(self, &self->qos_tx);
927 /* This frame will actually be sent at the new speed */
928 irlap_send_rr_frame(self, CMD_FRAME);
930 irlap_start_final_timer(self, self->final_timeout/2);
931 irlap_next_state(self, LAP_NRM_P);
933 irlap_connect_confirm(self, skb);
935 case RECV_DM_RSP: /* FALLTHROUGH */
937 del_timer(&self->final_timer);
938 irlap_next_state(self, LAP_NDM);
940 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
943 IRDA_DEBUG(1, __FUNCTION__ "(), Unknown event %d, %s\n", event,
953 * Function irlap_state_offline (self, event, skb, info)
955 * OFFLINE state, not used for now!
958 static int irlap_state_offline(struct irlap_cb *self, IRLAP_EVENT event,
959 struct sk_buff *skb, struct irlap_info *info)
961 IRDA_DEBUG( 0, __FUNCTION__ "(), Unknown event\n");
967 * Function irlap_state_xmit_p (self, event, skb, info)
969 * XMIT, Only the primary station has right to transmit, and we
970 * therefore do not expect to receive any transmissions from other
974 static int irlap_state_xmit_p(struct irlap_cb *self, IRLAP_EVENT event,
975 struct sk_buff *skb, struct irlap_info *info)
982 * Only send frame if send-window > 0.
984 if ((self->window > 0) && (!self->remote_busy)) {
985 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
987 * Test if we have transmitted more bytes over the
988 * link than its possible to do with the current
989 * speed and turn-around-time.
991 if (skb->len > self->bytes_left) {
992 IRDA_DEBUG(4, __FUNCTION__
993 "(), Not allowed to transmit more "
995 skb_queue_head(&self->txq, skb_get(skb));
997 * We should switch state to LAP_NRM_P, but
998 * that is not possible since we must be sure
999 * that we poll the other side. Since we have
1000 * used up our time, the poll timer should
1001 * trigger anyway now, so we just wait for it
1006 self->bytes_left -= skb->len;
1007 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1009 * Send data with poll bit cleared only if window > 1
1010 * and there is more frames after this one to be sent
1012 if ((self->window > 1) &&
1013 skb_queue_len( &self->txq) > 0)
1015 irlap_send_data_primary(self, skb);
1016 irlap_next_state(self, LAP_XMIT_P);
1018 irlap_send_data_primary_poll(self, skb);
1019 irlap_next_state(self, LAP_NRM_P);
1022 * Make sure state machine does not try to send
1027 #ifdef CONFIG_IRDA_FAST_RR
1028 /* Peer may want to reply immediately */
1029 self->fast_RR = FALSE;
1030 #endif /* CONFIG_IRDA_FAST_RR */
1032 IRDA_DEBUG(4, __FUNCTION__
1033 "(), Unable to send! remote busy?\n");
1034 skb_queue_head(&self->txq, skb_get(skb));
1037 * The next ret is important, because it tells
1038 * irlap_next_state _not_ to deliver more frames
1043 case POLL_TIMER_EXPIRED:
1044 IRDA_DEBUG(3, __FUNCTION__ "(), POLL_TIMER_EXPIRED (%ld)\n",
1046 irlap_send_rr_frame(self, CMD_FRAME);
1047 /* Return to NRM properly - Jean II */
1048 self->window = self->window_size;
1049 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1050 /* Allowed to transmit a maximum number of bytes again. */
1051 self->bytes_left = self->line_capacity;
1052 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1053 irlap_start_final_timer(self, self->final_timeout);
1054 irlap_next_state(self, LAP_NRM_P);
1056 case DISCONNECT_REQUEST:
1057 del_timer(&self->poll_timer);
1058 irlap_wait_min_turn_around(self, &self->qos_tx);
1059 irlap_send_disc_frame(self);
1060 irlap_flush_all_queues(self);
1061 irlap_start_final_timer(self, self->final_timeout);
1062 self->retry_count = 0;
1063 irlap_next_state(self, LAP_PCLOSE);
1066 /* Nothing to do, irlap_do_event() will send the packet
1067 * when we return... - Jean II */
1070 IRDA_DEBUG(0, __FUNCTION__ "(), Unknown event %s\n",
1071 irlap_event[event]);
1080 * Function irlap_state_pclose (event, skb, info)
1084 static int irlap_state_pclose(struct irlap_cb *self, IRLAP_EVENT event,
1085 struct sk_buff *skb, struct irlap_info *info)
1089 IRDA_DEBUG(1, __FUNCTION__ "()\n");
1091 ASSERT(self != NULL, return -1;);
1092 ASSERT(self->magic == LAP_MAGIC, return -1;);
1095 case RECV_UA_RSP: /* FALLTHROUGH */
1097 del_timer(&self->final_timer);
1099 /* Set new link parameters */
1100 irlap_apply_default_connection_parameters(self);
1102 /* Always switch state before calling upper layers */
1103 irlap_next_state(self, LAP_NDM);
1105 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
1107 case FINAL_TIMER_EXPIRED:
1108 if (self->retry_count < self->N3) {
1109 irlap_wait_min_turn_around(self, &self->qos_tx);
1110 irlap_send_disc_frame(self);
1111 irlap_start_final_timer(self, self->final_timeout);
1112 self->retry_count++;
1115 irlap_apply_default_connection_parameters(self);
1117 /* Always switch state before calling upper layers */
1118 irlap_next_state(self, LAP_NDM);
1120 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1124 IRDA_DEBUG(1, __FUNCTION__ "(), Unknown event %d\n", event);
1133 * Function irlap_state_nrm_p (self, event, skb, info)
1135 * NRM_P (Normal Response Mode as Primary), The primary station has given
1136 * permissions to a secondary station to transmit IrLAP resonse frames
1137 * (by sending a frame with the P bit set). The primary station will not
1138 * transmit any frames and is expecting to receive frames only from the
1139 * secondary to which transmission permissions has been given.
1141 static int irlap_state_nrm_p(struct irlap_cb *self, IRLAP_EVENT event,
1142 struct sk_buff *skb, struct irlap_info *info)
1149 case RECV_I_RSP: /* Optimize for the common case */
1150 /* FIXME: must check for remote_busy below */
1151 #ifdef CONFIG_IRDA_FAST_RR
1153 * Reset the fast_RR so we can use the fast RR code with
1154 * full speed the next time since peer may have more frames
1157 self->fast_RR = FALSE;
1158 #endif /* CONFIG_IRDA_FAST_RR */
1159 ASSERT( info != NULL, return -1;);
1161 ns_status = irlap_validate_ns_received(self, info->ns);
1162 nr_status = irlap_validate_nr_received(self, info->nr);
1165 * Check for expected I(nformation) frame
1167 if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
1168 /* poll bit cleared? */
1170 self->vr = (self->vr + 1) % 8;
1172 /* Update Nr received */
1173 irlap_update_nr_received( self, info->nr);
1175 self->ack_required = TRUE;
1177 /* Keep state, do not move this line */
1178 irlap_next_state(self, LAP_NRM_P);
1180 irlap_data_indication(self, skb, FALSE);
1182 del_timer(&self->final_timer);
1184 self->vr = (self->vr + 1) % 8;
1186 /* Update Nr received */
1187 irlap_update_nr_received(self, info->nr);
1190 * Got expected NR, so reset the
1191 * retry_count. This is not done by IrLAP,
1194 self->retry_count = 0;
1195 self->ack_required = TRUE;
1197 irlap_wait_min_turn_around(self, &self->qos_tx);
1199 /* Call higher layer *before* changing state
1200 * to give them a chance to send data in the
1203 irlap_data_indication(self, skb, FALSE);
1205 /* XMIT states are the most dangerous state
1206 * to be in, because user requests are
1207 * processed directly and may change state.
1208 * On the other hand, in NDM_P, those
1209 * requests are queued and we will process
1210 * them when we return to irlap_do_event().
1213 irlap_next_state(self, LAP_XMIT_P);
1215 /* This is the last frame.
1216 * Make sure it's always called in XMIT state.
1218 irlap_start_poll_timer(self, self->poll_timeout);
1223 /* Unexpected next to send (Ns) */
1224 if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
1227 irlap_update_nr_received(self, info->nr);
1230 * Wait until the last frame before doing
1235 irlap_next_state(self, LAP_NRM_P);
1237 IRDA_DEBUG(4, __FUNCTION__
1238 "(), missing or duplicate frame!\n");
1240 /* Update Nr received */
1241 irlap_update_nr_received(self, info->nr);
1243 irlap_wait_min_turn_around(self, &self->qos_tx);
1244 irlap_send_rr_frame(self, CMD_FRAME);
1246 self->ack_required = FALSE;
1248 irlap_start_final_timer(self, self->final_timeout);
1249 irlap_next_state(self, LAP_NRM_P);
1254 * Unexpected next to receive (Nr)
1256 if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
1259 self->vr = (self->vr + 1) % 8;
1261 /* Update Nr received */
1262 irlap_update_nr_received(self, info->nr);
1264 /* Resend rejected frames */
1265 irlap_resend_rejected_frames(self, CMD_FRAME);
1267 self->ack_required = FALSE;
1268 irlap_start_final_timer(self, self->final_timeout);
1270 /* Keep state, do not move this line */
1271 irlap_next_state(self, LAP_NRM_P);
1273 irlap_data_indication(self, skb, FALSE);
1276 * Do not resend frames until the last
1277 * frame has arrived from the other
1278 * device. This is not documented in
1281 self->vr = (self->vr + 1) % 8;
1283 /* Update Nr received */
1284 irlap_update_nr_received(self, info->nr);
1286 self->ack_required = FALSE;
1288 /* Keep state, do not move this line!*/
1289 irlap_next_state(self, LAP_NRM_P);
1291 irlap_data_indication(self, skb, FALSE);
1296 * Unexpected next to send (Ns) and next to receive (Nr)
1297 * Not documented by IrLAP!
1299 if ((ns_status == NS_UNEXPECTED) &&
1300 (nr_status == NR_UNEXPECTED))
1302 IRDA_DEBUG(4, __FUNCTION__
1303 "(), unexpected nr and ns!\n");
1305 /* Resend rejected frames */
1306 irlap_resend_rejected_frames(self, CMD_FRAME);
1308 /* Give peer some time to retransmit! */
1309 irlap_start_final_timer(self, self->final_timeout);
1311 /* Keep state, do not move this line */
1312 irlap_next_state(self, LAP_NRM_P);
1314 /* Update Nr received */
1315 /* irlap_update_nr_received( info->nr); */
1317 self->ack_required = FALSE;
1325 if ((nr_status == NR_INVALID) || (ns_status == NS_INVALID)) {
1327 del_timer(&self->final_timer);
1329 irlap_next_state(self, LAP_RESET_WAIT);
1331 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1332 self->xmitflag = TRUE;
1334 del_timer(&self->final_timer);
1336 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1338 self->xmitflag = FALSE;
1342 IRDA_DEBUG(1, __FUNCTION__ "(), Not implemented!\n");
1343 IRDA_DEBUG(1, __FUNCTION__
1344 "(), event=%s, ns_status=%d, nr_status=%d\n",
1345 irlap_event[ event], ns_status, nr_status);
1348 /* Poll bit cleared? */
1350 irlap_data_indication(self, skb, TRUE);
1351 irlap_next_state(self, LAP_NRM_P);
1353 del_timer(&self->final_timer);
1354 irlap_data_indication(self, skb, TRUE);
1355 irlap_next_state(self, LAP_XMIT_P);
1356 printk(__FUNCTION__ "(): RECV_UI_FRAME: next state %s\n", irlap_state[self->state]);
1357 irlap_start_poll_timer(self, self->poll_timeout);
1362 * If you get a RR, the remote isn't busy anymore,
1363 * no matter what the NR
1365 self->remote_busy = FALSE;
1370 ret = irlap_validate_nr_received(self, info->nr);
1371 if (ret == NR_EXPECTED) {
1372 /* Stop final timer */
1373 del_timer(&self->final_timer);
1375 /* Update Nr received */
1376 irlap_update_nr_received(self, info->nr);
1379 * Got expected NR, so reset the retry_count. This
1380 * is not done by the IrLAP standard , which is
1383 self->retry_count = 0;
1384 irlap_wait_min_turn_around(self, &self->qos_tx);
1386 irlap_next_state(self, LAP_XMIT_P);
1388 /* Start poll timer */
1389 irlap_start_poll_timer(self, self->poll_timeout);
1390 } else if (ret == NR_UNEXPECTED) {
1391 ASSERT(info != NULL, return -1;);
1396 /* Update Nr received */
1397 irlap_update_nr_received(self, info->nr);
1399 IRDA_DEBUG(4, "RECV_RR_FRAME: Retrans:%d, nr=%d, va=%d, "
1401 self->retry_count, info->nr, self->va,
1402 self->vs, self->vr);
1404 /* Resend rejected frames */
1405 irlap_resend_rejected_frames(self, CMD_FRAME);
1407 irlap_next_state(self, LAP_NRM_P);
1408 } else if (ret == NR_INVALID) {
1409 IRDA_DEBUG(1, __FUNCTION__ "(), Received RR with "
1411 del_timer(&self->final_timer);
1413 irlap_next_state(self, LAP_RESET_WAIT);
1415 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1416 self->xmitflag = TRUE;
1420 ASSERT(info != NULL, return -1;);
1422 /* Stop final timer */
1423 del_timer(&self->final_timer);
1424 self->remote_busy = TRUE;
1426 /* Update Nr received */
1427 irlap_update_nr_received(self, info->nr);
1428 irlap_next_state(self, LAP_XMIT_P);
1430 /* Start poll timer */
1431 irlap_start_poll_timer(self, self->poll_timeout);
1434 del_timer(&self->final_timer);
1435 self->xmitflag = TRUE;
1436 irlap_next_state(self, LAP_RESET_WAIT);
1437 irlap_reset_indication(self);
1439 case FINAL_TIMER_EXPIRED:
1441 * We are allowed to wait for additional 300 ms if
1442 * final timer expires when we are in the middle
1443 * of receiving a frame (page 45, IrLAP). Check that
1444 * we only do this once for each frame.
1446 if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
1447 IRDA_DEBUG(1, "FINAL_TIMER_EXPIRED when receiving a "
1448 "frame! Waiting a little bit more!\n");
1449 irlap_start_final_timer(self, MSECS_TO_JIFFIES(300));
1452 * Don't allow this to happen one more time in a row,
1453 * or else we can get a pretty tight loop here if
1454 * if we only receive half a frame. DB.
1456 self->add_wait = TRUE;
1459 self->add_wait = FALSE;
1461 /* N2 is the disconnect timer. Until we reach it, we retry */
1462 if (self->retry_count < self->N2) {
1463 /* Retry sending the pf bit to the secondary */
1464 irlap_wait_min_turn_around(self, &self->qos_tx);
1465 irlap_send_rr_frame(self, CMD_FRAME);
1467 irlap_start_final_timer(self, self->final_timeout);
1468 self->retry_count++;
1469 IRDA_DEBUG(4, "irlap_state_nrm_p: FINAL_TIMER_EXPIRED:"
1470 " retry_count=%d\n", self->retry_count);
1472 /* Early warning event. I'm using a pretty liberal
1473 * interpretation of the spec and generate an event
1474 * every time the timer is multiple of N1 (and not
1475 * only the first time). This allow application
1476 * to know precisely if connectivity restart...
1478 if((self->retry_count % self->N1) == 0)
1479 irlap_status_indication(self,
1480 STATUS_NO_ACTIVITY);
1484 irlap_apply_default_connection_parameters(self);
1486 /* Always switch state before calling upper layers */
1487 irlap_next_state(self, LAP_NDM);
1488 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1492 irlap_update_nr_received(self, info->nr);
1493 if (self->remote_busy) {
1494 irlap_wait_min_turn_around(self, &self->qos_tx);
1495 irlap_send_rr_frame(self, CMD_FRAME);
1497 irlap_resend_rejected_frames(self, CMD_FRAME);
1498 irlap_start_final_timer(self, self->final_timeout);
1501 irlap_update_nr_received(self, info->nr);
1502 if (self->remote_busy) {
1503 irlap_wait_min_turn_around(self, &self->qos_tx);
1504 irlap_send_rr_frame(self, CMD_FRAME);
1506 irlap_resend_rejected_frame(self, CMD_FRAME);
1507 irlap_start_final_timer(self, self->final_timeout);
1510 IRDA_DEBUG(1, __FUNCTION__ "(), RECV_RD_RSP\n");
1512 irlap_flush_all_queues(self);
1513 irlap_next_state(self, LAP_XMIT_P);
1514 /* Call back the LAP state machine to do a proper disconnect */
1515 irlap_disconnect_request(self);
1518 IRDA_DEBUG(1, __FUNCTION__ "(), Unknown event %s\n",
1519 irlap_event[event]);
1528 * Function irlap_state_reset_wait (event, skb, info)
1530 * We have informed the service user of a reset condition, and is
1531 * awaiting reset of disconnect request.
1534 static int irlap_state_reset_wait(struct irlap_cb *self, IRLAP_EVENT event,
1535 struct sk_buff *skb, struct irlap_info *info)
1539 IRDA_DEBUG(3, __FUNCTION__ "(), event = %s\n", irlap_event[event]);
1541 ASSERT(self != NULL, return -1;);
1542 ASSERT(self->magic == LAP_MAGIC, return -1;);
1546 if (self->xmitflag) {
1547 irlap_wait_min_turn_around(self, &self->qos_tx);
1548 irlap_send_snrm_frame(self, NULL);
1549 irlap_start_final_timer(self, self->final_timeout);
1550 irlap_next_state(self, LAP_RESET);
1552 irlap_start_final_timer(self, self->final_timeout);
1553 irlap_next_state(self, LAP_RESET);
1556 case DISCONNECT_REQUEST:
1557 irlap_wait_min_turn_around( self, &self->qos_tx);
1558 irlap_send_disc_frame( self);
1559 irlap_flush_all_queues( self);
1560 irlap_start_final_timer( self, self->final_timeout);
1561 self->retry_count = 0;
1562 irlap_next_state( self, LAP_PCLOSE);
1565 IRDA_DEBUG(2, __FUNCTION__ "(), Unknown event %s\n",
1566 irlap_event[event]);
1575 * Function irlap_state_reset (self, event, skb, info)
1577 * We have sent a SNRM reset command to the peer layer, and is awaiting
1581 static int irlap_state_reset(struct irlap_cb *self, IRLAP_EVENT event,
1582 struct sk_buff *skb, struct irlap_info *info)
1586 IRDA_DEBUG(3, __FUNCTION__ "(), event = %s\n", irlap_event[event]);
1588 ASSERT(self != NULL, return -1;);
1589 ASSERT(self->magic == LAP_MAGIC, return -1;);
1593 del_timer(&self->final_timer);
1595 irlap_apply_default_connection_parameters(self);
1597 /* Always switch state before calling upper layers */
1598 irlap_next_state(self, LAP_NDM);
1600 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1604 del_timer(&self->final_timer);
1606 /* Initiate connection state */
1607 irlap_initiate_connection_state(self);
1609 irlap_reset_confirm();
1611 self->remote_busy = FALSE;
1613 irlap_next_state(self, LAP_XMIT_P);
1615 irlap_start_poll_timer(self, self->poll_timeout);
1618 case FINAL_TIMER_EXPIRED:
1619 if (self->retry_count < 3) {
1620 irlap_wait_min_turn_around(self, &self->qos_tx);
1622 ASSERT(self->netdev != NULL, return -1;);
1623 irlap_send_snrm_frame(self, self->qos_dev);
1625 self->retry_count++; /* Experimental!! */
1627 irlap_start_final_timer(self, self->final_timeout);
1628 irlap_next_state(self, LAP_RESET);
1629 } else if (self->retry_count >= self->N3) {
1630 irlap_apply_default_connection_parameters(self);
1632 /* Always switch state before calling upper layers */
1633 irlap_next_state(self, LAP_NDM);
1635 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1640 * SNRM frame is not allowed to contain an I-field in this
1644 IRDA_DEBUG(3, __FUNCTION__ "(), RECV_SNRM_CMD\n");
1645 irlap_initiate_connection_state(self);
1646 irlap_wait_min_turn_around(self, &self->qos_tx);
1647 irlap_send_ua_response_frame(self, &self->qos_rx);
1648 irlap_reset_confirm();
1649 irlap_start_wd_timer(self, self->wd_timeout);
1650 irlap_next_state(self, LAP_NDM);
1652 IRDA_DEBUG(0, __FUNCTION__
1653 "(), SNRM frame contained an I field!\n");
1657 IRDA_DEBUG(1, __FUNCTION__ "(), Unknown event %s\n",
1658 irlap_event[event]);
1667 * Function irlap_state_xmit_s (event, skb, info)
1669 * XMIT_S, The secondary station has been given the right to transmit,
1670 * and we therefor do not expect to receive any transmissions from other
1673 static int irlap_state_xmit_s(struct irlap_cb *self, IRLAP_EVENT event,
1674 struct sk_buff *skb, struct irlap_info *info)
1678 IRDA_DEBUG(4, __FUNCTION__ "(), event=%s\n", irlap_event[event]);
1680 ASSERT(self != NULL, return -ENODEV;);
1681 ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
1686 * Send frame only if send window > 1
1688 if ((self->window > 0) && (!self->remote_busy)) {
1689 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1691 * Test if we have transmitted more bytes over the
1692 * link than its possible to do with the current
1693 * speed and turn-around-time.
1695 if (skb->len > self->bytes_left) {
1696 skb_queue_head(&self->txq, skb_get(skb));
1699 * Switch to NRM_S, this is only possible
1700 * when we are in secondary mode, since we
1701 * must be sure that we don't miss any RR
1704 self->window = self->window_size;
1705 self->bytes_left = self->line_capacity;
1706 irlap_start_wd_timer(self, self->wd_timeout);
1708 irlap_next_state(self, LAP_NRM_S);
1710 return -EPROTO; /* Try again later */
1712 self->bytes_left -= skb->len;
1713 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1715 * Send data with final bit cleared only if window > 1
1716 * and there is more frames to be sent
1718 if ((self->window > 1) &&
1719 skb_queue_len(&self->txq) > 0)
1721 irlap_send_data_secondary(self, skb);
1722 irlap_next_state(self, LAP_XMIT_S);
1724 irlap_send_data_secondary_final(self, skb);
1725 irlap_next_state(self, LAP_NRM_S);
1728 * Make sure state machine does not try to send
1734 IRDA_DEBUG(2, __FUNCTION__ "(), Unable to send!\n");
1735 skb_queue_head(&self->txq, skb_get(skb));
1739 case DISCONNECT_REQUEST:
1740 irlap_send_rd_frame(self);
1741 irlap_flush_all_queues(self);
1742 irlap_start_wd_timer(self, self->wd_timeout);
1743 irlap_next_state(self, LAP_SCLOSE);
1746 /* Nothing to do, irlap_do_event() will send the packet
1747 * when we return... - Jean II */
1750 IRDA_DEBUG(2, __FUNCTION__ "(), Unknown event %s\n",
1751 irlap_event[event]);
1760 * Function irlap_state_nrm_s (event, skb, info)
1762 * NRM_S (Normal Response Mode as Secondary) state, in this state we are
1763 * expecting to receive frames from the primary station
1766 static int irlap_state_nrm_s(struct irlap_cb *self, IRLAP_EVENT event,
1767 struct sk_buff *skb, struct irlap_info *info)
1773 IRDA_DEBUG(4, __FUNCTION__ "(), event=%s\n", irlap_event[ event]);
1775 ASSERT(self != NULL, return -1;);
1776 ASSERT(self->magic == LAP_MAGIC, return -1;);
1779 case RECV_I_CMD: /* Optimize for the common case */
1780 /* FIXME: must check for remote_busy below */
1781 IRDA_DEBUG(4, __FUNCTION__ "(), event=%s nr=%d, vs=%d, ns=%d, "
1782 "vr=%d, pf=%d\n", irlap_event[event], info->nr,
1783 self->vs, info->ns, self->vr, info->pf);
1785 self->retry_count = 0;
1787 ns_status = irlap_validate_ns_received(self, info->ns);
1788 nr_status = irlap_validate_nr_received(self, info->nr);
1790 * Check for expected I(nformation) frame
1792 if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
1797 self->vr = (self->vr + 1) % 8;
1799 /* Update Nr received */
1800 irlap_update_nr_received(self, info->nr);
1802 self->ack_required = TRUE;
1805 * Starting WD-timer here is optional, but
1806 * not recommended. Note 6 IrLAP p. 83
1809 irda_start_timer(WD_TIMER, self->wd_timeout);
1811 /* Keep state, do not move this line */
1812 irlap_next_state(self, LAP_NRM_S);
1814 irlap_data_indication(self, skb, FALSE);
1817 self->vr = (self->vr + 1) % 8;
1819 /* Update Nr received */
1820 irlap_update_nr_received(self, info->nr);
1823 * We should wait before sending RR, and
1824 * also before changing to XMIT_S
1825 * state. (note 1, IrLAP p. 82)
1827 irlap_wait_min_turn_around(self, &self->qos_tx);
1830 * Give higher layers a chance to
1831 * immediately reply with some data before
1832 * we decide if we should send a RR frame
1835 irlap_data_indication(self, skb, FALSE);
1837 /* Any pending data requests? */
1838 if ((skb_queue_len(&self->txq) > 0) &&
1841 self->ack_required = TRUE;
1843 del_timer(&self->wd_timer);
1845 irlap_next_state(self, LAP_XMIT_S);
1847 irlap_send_rr_frame(self, RSP_FRAME);
1848 irlap_start_wd_timer(self,
1851 /* Keep the state */
1852 irlap_next_state(self, LAP_NRM_S);
1858 * Check for Unexpected next to send (Ns)
1860 if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
1862 /* Unexpected next to send, with final bit cleared */
1864 irlap_update_nr_received(self, info->nr);
1866 irlap_start_wd_timer(self, self->wd_timeout);
1868 /* Update Nr received */
1869 irlap_update_nr_received(self, info->nr);
1871 irlap_wait_min_turn_around(self, &self->qos_tx);
1872 irlap_send_rr_frame(self, CMD_FRAME);
1874 irlap_start_wd_timer(self, self->wd_timeout);
1880 * Unexpected Next to Receive(NR) ?
1882 if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
1885 IRDA_DEBUG(4, "RECV_I_RSP: frame(s) lost\n");
1887 self->vr = (self->vr + 1) % 8;
1889 /* Update Nr received */
1890 irlap_update_nr_received(self, info->nr);
1892 /* Resend rejected frames */
1893 irlap_resend_rejected_frames(self, RSP_FRAME);
1895 /* Keep state, do not move this line */
1896 irlap_next_state(self, LAP_NRM_S);
1898 irlap_data_indication(self, skb, FALSE);
1899 irlap_start_wd_timer(self, self->wd_timeout);
1903 * This is not documented in IrLAP!! Unexpected NR
1904 * with poll bit cleared
1907 self->vr = (self->vr + 1) % 8;
1909 /* Update Nr received */
1910 irlap_update_nr_received(self, info->nr);
1912 /* Keep state, do not move this line */
1913 irlap_next_state(self, LAP_NRM_S);
1915 irlap_data_indication(self, skb, FALSE);
1916 irlap_start_wd_timer(self, self->wd_timeout);
1921 if (ret == NR_INVALID) {
1922 IRDA_DEBUG(0, "NRM_S, NR_INVALID not implemented!\n");
1924 if (ret == NS_INVALID) {
1925 IRDA_DEBUG(0, "NRM_S, NS_INVALID not implemented!\n");
1933 irlap_data_indication(self, skb, TRUE);
1934 irlap_next_state(self, LAP_NRM_S); /* Keep state */
1937 * Any pending data requests?
1939 if ((skb_queue_len(&self->txq) > 0) &&
1940 (self->window > 0) && !self->remote_busy)
1942 irlap_data_indication(self, skb, TRUE);
1944 del_timer(&self->wd_timer);
1946 irlap_next_state(self, LAP_XMIT_S);
1948 irlap_data_indication(self, skb, TRUE);
1950 irlap_wait_min_turn_around(self, &self->qos_tx);
1952 irlap_send_rr_frame(self, RSP_FRAME);
1953 self->ack_required = FALSE;
1955 irlap_start_wd_timer(self, self->wd_timeout);
1957 /* Keep the state */
1958 irlap_next_state(self, LAP_NRM_S);
1963 self->retry_count = 0;
1968 nr_status = irlap_validate_nr_received(self, info->nr);
1969 if (nr_status == NR_EXPECTED) {
1970 if ((skb_queue_len( &self->txq) > 0) &&
1971 (self->window > 0)) {
1972 self->remote_busy = FALSE;
1974 /* Update Nr received */
1975 irlap_update_nr_received(self, info->nr);
1976 del_timer(&self->wd_timer);
1978 irlap_wait_min_turn_around(self, &self->qos_tx);
1979 irlap_next_state(self, LAP_XMIT_S);
1981 self->remote_busy = FALSE;
1982 /* Update Nr received */
1983 irlap_update_nr_received(self, info->nr);
1984 irlap_wait_min_turn_around(self, &self->qos_tx);
1985 irlap_start_wd_timer(self, self->wd_timeout);
1987 /* Note : if the link is idle (this case),
1988 * we never go in XMIT_S, so we never get a
1989 * chance to process any DISCONNECT_REQUEST.
1990 * Do it now ! - Jean II */
1991 if (self->disconnect_pending) {
1993 irlap_send_rd_frame(self);
1994 irlap_flush_all_queues(self);
1996 irlap_next_state(self, LAP_SCLOSE);
1998 /* Just send back pf bit */
1999 irlap_send_rr_frame(self, RSP_FRAME);
2001 irlap_next_state(self, LAP_NRM_S);
2004 } else if (nr_status == NR_UNEXPECTED) {
2005 self->remote_busy = FALSE;
2006 irlap_update_nr_received(self, info->nr);
2007 irlap_resend_rejected_frames(self, RSP_FRAME);
2009 irlap_start_wd_timer(self, self->wd_timeout);
2012 irlap_next_state(self, LAP_NRM_S);
2014 IRDA_DEBUG(1, __FUNCTION__
2015 "(), invalid nr not implemented!\n");
2019 /* SNRM frame is not allowed to contain an I-field */
2021 del_timer(&self->wd_timer);
2022 IRDA_DEBUG(1, __FUNCTION__ "(), received SNRM cmd\n");
2023 irlap_next_state(self, LAP_RESET_CHECK);
2025 irlap_reset_indication(self);
2027 IRDA_DEBUG(0, __FUNCTION__
2028 "(), SNRM frame contained an I-field!\n");
2033 irlap_update_nr_received(self, info->nr);
2034 if (self->remote_busy) {
2035 irlap_wait_min_turn_around(self, &self->qos_tx);
2036 irlap_send_rr_frame(self, CMD_FRAME);
2038 irlap_resend_rejected_frames(self, CMD_FRAME);
2039 irlap_start_wd_timer(self, self->wd_timeout);
2042 irlap_update_nr_received(self, info->nr);
2043 if (self->remote_busy) {
2044 irlap_wait_min_turn_around(self, &self->qos_tx);
2045 irlap_send_rr_frame(self, CMD_FRAME);
2047 irlap_resend_rejected_frame(self, CMD_FRAME);
2048 irlap_start_wd_timer(self, self->wd_timeout);
2050 case WD_TIMER_EXPIRED:
2052 * Wait until retry_count * n matches negotiated threshold/
2053 * disconnect time (note 2 in IrLAP p. 82)
2055 * Similar to irlap_state_nrm_p() -> FINAL_TIMER_EXPIRED
2056 * Note : self->wd_timeout = (self->final_timeout * 2),
2057 * which explain why we use (self->N2 / 2) here !!!
2060 IRDA_DEBUG(1, __FUNCTION__ "(), retry_count = %d\n",
2063 if (self->retry_count < (self->N2 / 2)) {
2064 /* No retry, just wait for primary */
2065 irlap_start_wd_timer(self, self->wd_timeout);
2066 self->retry_count++;
2068 if((self->retry_count % (self->N1 / 2)) == 0)
2069 irlap_status_indication(self,
2070 STATUS_NO_ACTIVITY);
2072 irlap_apply_default_connection_parameters(self);
2074 /* Always switch state before calling upper layers */
2075 irlap_next_state(self, LAP_NDM);
2076 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
2080 /* Always switch state before calling upper layers */
2081 irlap_next_state(self, LAP_NDM);
2083 /* Send disconnect response */
2084 irlap_wait_min_turn_around(self, &self->qos_tx);
2085 irlap_send_ua_response_frame(self, NULL);
2087 del_timer(&self->wd_timer);
2088 irlap_flush_all_queues(self);
2089 /* Set default link parameters */
2090 irlap_apply_default_connection_parameters(self);
2092 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2094 case RECV_DISCOVERY_XID_CMD:
2095 irlap_wait_min_turn_around(self, &self->qos_tx);
2096 irlap_send_rr_frame(self, RSP_FRAME);
2097 self->ack_required = TRUE;
2098 irlap_start_wd_timer(self, self->wd_timeout);
2099 irlap_next_state(self, LAP_NRM_S);
2103 /* Remove test frame header (only LAP header in NRM) */
2104 skb_pull(skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
2106 irlap_wait_min_turn_around(self, &self->qos_tx);
2107 irlap_start_wd_timer(self, self->wd_timeout);
2109 /* Send response (info will be copied) */
2110 irlap_send_test_frame(self, self->caddr, info->daddr, skb);
2113 IRDA_DEBUG(1, __FUNCTION__ "(), Unknown event %d, (%s)\n",
2114 event, irlap_event[event]);
2123 * Function irlap_state_sclose (self, event, skb, info)
2128 static int irlap_state_sclose(struct irlap_cb *self, IRLAP_EVENT event,
2129 struct sk_buff *skb, struct irlap_info *info)
2133 IRDA_DEBUG(1, __FUNCTION__ "()\n");
2135 ASSERT(self != NULL, return -ENODEV;);
2136 ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
2140 /* Always switch state before calling upper layers */
2141 irlap_next_state(self, LAP_NDM);
2143 /* Send disconnect response */
2144 irlap_wait_min_turn_around(self, &self->qos_tx);
2145 irlap_send_ua_response_frame(self, NULL);
2147 del_timer(&self->wd_timer);
2148 /* Set default link parameters */
2149 irlap_apply_default_connection_parameters(self);
2151 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2154 /* Always switch state before calling upper layers */
2155 irlap_next_state(self, LAP_NDM);
2157 del_timer(&self->wd_timer);
2158 irlap_apply_default_connection_parameters(self);
2160 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2162 case WD_TIMER_EXPIRED:
2163 /* Always switch state before calling upper layers */
2164 irlap_next_state(self, LAP_NDM);
2166 irlap_apply_default_connection_parameters(self);
2168 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2171 IRDA_DEBUG(1, __FUNCTION__ "(), Unknown event %d, (%s)\n",
2172 event, irlap_event[event]);
2181 static int irlap_state_reset_check( struct irlap_cb *self, IRLAP_EVENT event,
2182 struct sk_buff *skb,
2183 struct irlap_info *info)
2187 IRDA_DEBUG(1, __FUNCTION__ "(), event=%s\n", irlap_event[event]);
2189 ASSERT(self != NULL, return -ENODEV;);
2190 ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
2193 case RESET_RESPONSE:
2194 irlap_send_ua_response_frame(self, &self->qos_rx);
2195 irlap_initiate_connection_state(self);
2196 irlap_start_wd_timer(self, WD_TIMEOUT);
2197 irlap_flush_all_queues(self);
2199 irlap_next_state(self, LAP_NRM_S);
2201 case DISCONNECT_REQUEST:
2202 irlap_wait_min_turn_around(self, &self->qos_tx);
2203 irlap_send_rd_frame(self);
2204 irlap_start_wd_timer(self, WD_TIMEOUT);
2205 irlap_next_state(self, LAP_SCLOSE);
2208 IRDA_DEBUG(1, __FUNCTION__ "(), Unknown event %d, (%s)\n",
2209 event, irlap_event[event]);