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 static const char *const irlap_event
[] __maybe_unused
= {
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",
122 const char *const irlap_state
[] = {
140 static int (*state
[])(struct irlap_cb
*self
, IRLAP_EVENT event
,
141 struct sk_buff
*skb
, struct irlap_info
*info
) =
152 irlap_state_reset_wait
,
157 irlap_state_reset_check
,
161 * Function irda_poll_timer_expired (data)
163 * Poll timer has expired. Normally we must now send a RR frame to the
166 static void irlap_poll_timer_expired(void *data
)
168 struct irlap_cb
*self
= (struct irlap_cb
*) data
;
170 IRDA_ASSERT(self
!= NULL
, return;);
171 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return;);
173 irlap_do_event(self
, POLL_TIMER_EXPIRED
, NULL
, NULL
);
177 * Calculate and set time before we will have to send back the pf bit
178 * to the peer. Use in primary.
179 * Make sure that state is XMIT_P/XMIT_S when calling this function
180 * (and that nobody messed up with the state). - Jean II
182 static void irlap_start_poll_timer(struct irlap_cb
*self
, int timeout
)
184 IRDA_ASSERT(self
!= NULL
, return;);
185 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return;);
187 #ifdef CONFIG_IRDA_FAST_RR
189 * Send out the RR frames faster if our own transmit queue is empty, or
190 * if the peer is busy. The effect is a much faster conversation
192 if (skb_queue_empty(&self
->txq
) || self
->remote_busy
) {
193 if (self
->fast_RR
== TRUE
) {
195 * Assert that the fast poll timer has not reached the
196 * normal poll timer yet
198 if (self
->fast_RR_timeout
< timeout
) {
200 * FIXME: this should be a more configurable
203 self
->fast_RR_timeout
+=
204 (sysctl_fast_poll_increase
* HZ
/1000);
206 /* Use this fast(er) timeout instead */
207 timeout
= self
->fast_RR_timeout
;
210 self
->fast_RR
= TRUE
;
212 /* Start with just 0 ms */
213 self
->fast_RR_timeout
= 0;
217 self
->fast_RR
= FALSE
;
219 pr_debug("%s(), timeout=%d (%ld)\n", __func__
, timeout
, jiffies
);
220 #endif /* CONFIG_IRDA_FAST_RR */
223 irlap_do_event(self
, POLL_TIMER_EXPIRED
, NULL
, NULL
);
225 irda_start_timer(&self
->poll_timer
, timeout
, self
,
226 irlap_poll_timer_expired
);
230 * Function irlap_do_event (event, skb, info)
232 * Rushes through the state machine without any delay. If state == XMIT
233 * then send queued data frames.
235 void irlap_do_event(struct irlap_cb
*self
, IRLAP_EVENT event
,
236 struct sk_buff
*skb
, struct irlap_info
*info
)
240 if (!self
|| self
->magic
!= LAP_MAGIC
)
243 pr_debug("%s(), event = %s, state = %s\n", __func__
,
244 irlap_event
[event
], irlap_state
[self
->state
]);
246 ret
= (*state
[self
->state
])(self
, event
, skb
, info
);
249 * Check if there are any pending events that needs to be executed
251 switch (self
->state
) {
252 case LAP_XMIT_P
: /* FALLTHROUGH */
255 * We just received the pf bit and are at the beginning
256 * of a new LAP transmit window.
257 * Check if there are any queued data frames, and do not
258 * try to disconnect link if we send any data frames, since
259 * that will change the state away form XMIT
261 pr_debug("%s() : queue len = %d\n", __func__
,
262 skb_queue_len(&self
->txq
));
264 if (!skb_queue_empty(&self
->txq
)) {
265 /* Prevent race conditions with irlap_data_request() */
266 self
->local_busy
= TRUE
;
268 /* Theory of operation.
269 * We send frames up to when we fill the window or
270 * reach line capacity. Those frames will queue up
271 * in the device queue, and the driver will slowly
273 * After each frame that we send, we poll the higher
274 * layer for more data. It's the right time to do
275 * that because the link layer need to perform the mtt
276 * and then send the first frame, so we can afford
277 * to send a bit of time in kernel space.
278 * The explicit flow indication allow to minimise
279 * buffers (== lower latency), to avoid higher layer
280 * polling via timers (== less context switches) and
281 * to implement a crude scheduler - Jean II */
283 /* Try to send away all queued data frames */
284 while ((skb
= skb_dequeue(&self
->txq
)) != NULL
) {
286 ret
= (*state
[self
->state
])(self
, SEND_I_CMD
,
288 /* Drop reference count.
289 * It will be increase as needed in
290 * irlap_send_data_xxx() */
293 /* Poll the higher layers for one more frame */
294 irlmp_flow_indication(self
->notify
.instance
,
298 break; /* Try again later! */
300 /* Finished transmitting */
301 self
->local_busy
= FALSE
;
302 } else if (self
->disconnect_pending
) {
303 self
->disconnect_pending
= FALSE
;
305 ret
= (*state
[self
->state
])(self
, DISCONNECT_REQUEST
,
311 /* case LAP_RESET_WAIT: */
312 /* case LAP_RESET_CHECK: */
319 * Function irlap_state_ndm (event, skb, frame)
321 * NDM (Normal Disconnected Mode) state
324 static int irlap_state_ndm(struct irlap_cb
*self
, IRLAP_EVENT event
,
325 struct sk_buff
*skb
, struct irlap_info
*info
)
327 discovery_t
*discovery_rsp
;
330 IRDA_ASSERT(self
!= NULL
, return -1;);
331 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
334 case CONNECT_REQUEST
:
335 IRDA_ASSERT(self
->netdev
!= NULL
, return -1;);
337 if (self
->media_busy
) {
338 /* Note : this will never happen, because we test
339 * media busy in irlap_connect_request() and
340 * postpone the event... - Jean II */
341 pr_debug("%s(), CONNECT_REQUEST: media busy!\n",
344 /* Always switch state before calling upper layers */
345 irlap_next_state(self
, LAP_NDM
);
347 irlap_disconnect_indication(self
, LAP_MEDIA_BUSY
);
349 irlap_send_snrm_frame(self
, &self
->qos_rx
);
351 /* Start Final-bit timer */
352 irlap_start_final_timer(self
, self
->final_timeout
);
354 self
->retry_count
= 0;
355 irlap_next_state(self
, LAP_SETUP
);
359 /* Check if the frame contains and I field */
361 self
->daddr
= info
->daddr
;
362 self
->caddr
= info
->caddr
;
364 irlap_next_state(self
, LAP_CONN
);
366 irlap_connect_indication(self
, skb
);
368 pr_debug("%s(), SNRM frame does not contain an I field!\n",
372 case DISCOVERY_REQUEST
:
373 IRDA_ASSERT(info
!= NULL
, return -1;);
375 if (self
->media_busy
) {
376 pr_debug("%s(), DISCOVERY_REQUEST: media busy!\n",
378 /* irlap->log.condition = MEDIA_BUSY; */
380 /* This will make IrLMP try again */
381 irlap_discovery_confirm(self
, NULL
);
382 /* Note : the discovery log is not cleaned up here,
383 * it will be done in irlap_discovery_request()
390 irlap_send_discovery_xid_frame(self
, info
->S
, info
->s
, TRUE
,
392 self
->frame_sent
= FALSE
;
395 irlap_start_slot_timer(self
, self
->slot_timeout
);
396 irlap_next_state(self
, LAP_QUERY
);
398 case RECV_DISCOVERY_XID_CMD
:
399 IRDA_ASSERT(info
!= NULL
, return -1;);
401 /* Assert that this is not the final slot */
402 if (info
->s
<= info
->S
) {
403 self
->slot
= irlap_generate_rand_time_slot(info
->S
,
405 if (self
->slot
== info
->s
) {
406 discovery_rsp
= irlmp_get_discovery_response();
407 discovery_rsp
->data
.daddr
= info
->daddr
;
409 irlap_send_discovery_xid_frame(self
, info
->S
,
413 self
->frame_sent
= TRUE
;
415 self
->frame_sent
= FALSE
;
418 * Go to reply state until end of discovery to
419 * inhibit our own transmissions. Set the timer
420 * to not stay forever there... Jean II
422 irlap_start_query_timer(self
, info
->S
, info
->s
);
423 irlap_next_state(self
, LAP_REPLY
);
425 /* This is the final slot. How is it possible ?
426 * This would happen is both discoveries are just slightly
427 * offset (if they are in sync, all packets are lost).
428 * Most often, all the discovery requests will be received
429 * in QUERY state (see my comment there), except for the
430 * last frame that will come here.
431 * The big trouble when it happen is that active discovery
432 * doesn't happen, because nobody answer the discoveries
433 * frame of the other guy, so the log shows up empty.
434 * What should we do ?
435 * Not much. It's too late to answer those discovery frames,
436 * so we just pass the info to IrLMP who will put it in the
437 * log (and post an event).
438 * Another cause would be devices that do discovery much
439 * slower than us, however the latest fixes should minimise
443 pr_debug("%s(), Receiving final discovery request, missed the discovery slots :-(\n",
446 /* Last discovery request -> in the log */
447 irlap_discovery_indication(self
, info
->discovery
);
450 case MEDIA_BUSY_TIMER_EXPIRED
:
451 /* A bunch of events may be postponed because the media is
452 * busy (usually immediately after we close a connection),
453 * or while we are doing discovery (state query/reply).
454 * In all those cases, the media busy flag will be cleared
455 * when it's OK for us to process those postponed events.
456 * This event is not mentioned in the state machines in the
457 * IrLAP spec. It's because they didn't consider Ultra and
458 * postponing connection request is optional.
460 #ifdef CONFIG_IRDA_ULTRA
461 /* Send any pending Ultra frames if any */
462 if (!skb_queue_empty(&self
->txq_ultra
)) {
463 /* We don't send the frame, just post an event.
464 * Also, previously this code was in timer.c...
466 ret
= (*state
[self
->state
])(self
, SEND_UI_FRAME
,
469 #endif /* CONFIG_IRDA_ULTRA */
470 /* Check if we should try to connect.
471 * This code was previously in irlap_do_event() */
472 if (self
->connect_pending
) {
473 self
->connect_pending
= FALSE
;
475 /* This one *should* not pend in this state, except
476 * if a socket try to connect and immediately
477 * disconnect. - clear - Jean II */
478 if (self
->disconnect_pending
)
479 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
481 ret
= (*state
[self
->state
])(self
,
484 self
->disconnect_pending
= FALSE
;
486 /* Note : one way to test if this code works well (including
487 * media busy and small busy) is to create a user space
488 * application generating an Ultra packet every 3.05 sec (or
489 * 2.95 sec) and to see how it interact with discovery.
490 * It's fairly easy to check that no packet is lost, that the
491 * packets are postponed during discovery and that after
492 * discovery indication you have a 100ms "gap".
493 * As connection request and Ultra are now processed the same
494 * way, this avoid the tedious job of trying IrLAP connection
495 * in all those cases...
498 #ifdef CONFIG_IRDA_ULTRA
502 /* Only allowed to repeat an operation twice */
503 for (i
=0; ((i
<2) && (self
->media_busy
== FALSE
)); i
++) {
504 skb
= skb_dequeue(&self
->txq_ultra
);
506 irlap_send_ui_frame(self
, skb
, CBROADCAST
,
510 /* irlap_send_ui_frame() won't increase skb reference
511 * count, so no dev_kfree_skb() - Jean II */
514 /* Force us to listen 500 ms again */
515 irda_device_set_media_busy(self
->netdev
, TRUE
);
520 /* Only accept broadcast frames in NDM mode */
521 if (info
->caddr
!= CBROADCAST
) {
522 pr_debug("%s(), not a broadcast frame!\n",
525 irlap_unitdata_indication(self
, skb
);
527 #endif /* CONFIG_IRDA_ULTRA */
529 /* Remove test frame header */
530 skb_pull(skb
, sizeof(struct test_frame
));
533 * Send response. This skb will not be sent out again, and
534 * will only be used to send out the same info as the cmd
536 irlap_send_test_frame(self
, CBROADCAST
, info
->daddr
, skb
);
539 pr_debug("%s() not implemented!\n", __func__
);
542 pr_debug("%s(), Unknown event %s\n", __func__
,
552 * Function irlap_state_query (event, skb, info)
557 static int irlap_state_query(struct irlap_cb
*self
, IRLAP_EVENT event
,
558 struct sk_buff
*skb
, struct irlap_info
*info
)
562 IRDA_ASSERT(self
!= NULL
, return -1;);
563 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
566 case RECV_DISCOVERY_XID_RSP
:
567 IRDA_ASSERT(info
!= NULL
, return -1;);
568 IRDA_ASSERT(info
->discovery
!= NULL
, return -1;);
570 pr_debug("%s(), daddr=%08x\n", __func__
,
571 info
->discovery
->data
.daddr
);
573 if (!self
->discovery_log
) {
574 net_warn_ratelimited("%s: discovery log is gone! maybe the discovery timeout has been set too short?\n",
578 hashbin_insert(self
->discovery_log
,
579 (irda_queue_t
*) info
->discovery
,
580 info
->discovery
->data
.daddr
, NULL
);
583 /* irlap_next_state(self, LAP_QUERY); */
586 case RECV_DISCOVERY_XID_CMD
:
587 /* Yes, it is possible to receive those frames in this mode.
588 * Note that most often the last discovery request won't
589 * occur here but in NDM state (see my comment there).
590 * What should we do ?
591 * Not much. We are currently performing our own discovery,
592 * therefore we can't answer those frames. We don't want
593 * to change state either. We just pass the info to
594 * IrLMP who will put it in the log (and post an event).
598 IRDA_ASSERT(info
!= NULL
, return -1;);
600 pr_debug("%s(), Receiving discovery request (s = %d) while performing discovery :-(\n",
603 /* Last discovery request ? */
605 irlap_discovery_indication(self
, info
->discovery
);
607 case SLOT_TIMER_EXPIRED
:
609 * Wait a little longer if we detect an incoming frame. This
610 * is not mentioned in the spec, but is a good thing to do,
611 * since we want to work even with devices that violate the
612 * timing requirements.
614 if (irda_device_is_receiving(self
->netdev
) && !self
->add_wait
) {
615 pr_debug("%s(), device is slow to answer, waiting some more!\n",
617 irlap_start_slot_timer(self
, msecs_to_jiffies(10));
618 self
->add_wait
= TRUE
;
621 self
->add_wait
= FALSE
;
623 if (self
->s
< self
->S
) {
624 irlap_send_discovery_xid_frame(self
, self
->S
,
626 self
->discovery_cmd
);
628 irlap_start_slot_timer(self
, self
->slot_timeout
);
631 irlap_next_state(self
, LAP_QUERY
);
633 /* This is the final slot! */
634 irlap_send_discovery_xid_frame(self
, self
->S
, 0xff,
636 self
->discovery_cmd
);
638 /* Always switch state before calling upper layers */
639 irlap_next_state(self
, LAP_NDM
);
642 * We are now finished with the discovery procedure,
643 * so now we must return the results
645 irlap_discovery_confirm(self
, self
->discovery_log
);
647 /* IrLMP should now have taken care of the log */
648 self
->discovery_log
= NULL
;
652 pr_debug("%s(), Unknown event %s\n", __func__
,
662 * Function irlap_state_reply (self, event, skb, info)
664 * REPLY, we have received a XID discovery frame from a device and we
665 * are waiting for the right time slot to send a response XID frame
668 static int irlap_state_reply(struct irlap_cb
*self
, IRLAP_EVENT event
,
669 struct sk_buff
*skb
, struct irlap_info
*info
)
671 discovery_t
*discovery_rsp
;
674 IRDA_ASSERT(self
!= NULL
, return -1;);
675 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
678 case QUERY_TIMER_EXPIRED
:
679 pr_debug("%s(), QUERY_TIMER_EXPIRED <%ld>\n",
681 irlap_next_state(self
, LAP_NDM
);
683 case RECV_DISCOVERY_XID_CMD
:
684 IRDA_ASSERT(info
!= NULL
, return -1;);
686 if (info
->s
== 0xff) {
687 del_timer(&self
->query_timer
);
689 /* info->log.condition = REMOTE; */
691 /* Always switch state before calling upper layers */
692 irlap_next_state(self
, LAP_NDM
);
694 irlap_discovery_indication(self
, info
->discovery
);
696 /* If it's our slot, send our reply */
697 if ((info
->s
>= self
->slot
) && (!self
->frame_sent
)) {
698 discovery_rsp
= irlmp_get_discovery_response();
699 discovery_rsp
->data
.daddr
= info
->daddr
;
701 irlap_send_discovery_xid_frame(self
, info
->S
,
706 self
->frame_sent
= TRUE
;
708 /* Readjust our timer to accommodate devices
709 * doing faster or slower discovery than us...
711 irlap_start_query_timer(self
, info
->S
, info
->s
);
714 //irlap_next_state(self, LAP_REPLY);
718 pr_debug("%s(), Unknown event %d, %s\n", __func__
,
719 event
, irlap_event
[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 pr_debug("%s(), event=%s\n", __func__
, irlap_event
[event
]);
741 IRDA_ASSERT(self
!= NULL
, return -1;);
742 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
745 case CONNECT_RESPONSE
:
746 skb_pull(skb
, sizeof(struct snrm_frame
));
748 IRDA_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 negotiated 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 negotiated 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 pr_debug("%s(), event RECV_DISCOVER_XID_CMD!\n",
801 irlap_next_state(self
, LAP_NDM
);
804 case DISCONNECT_REQUEST
:
805 pr_debug("%s(), Disconnect request!\n", __func__
);
806 irlap_send_dm_frame(self
);
807 irlap_next_state( self
, LAP_NDM
);
808 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
811 pr_debug("%s(), Unknown event %d, %s\n", __func__
,
812 event
, irlap_event
[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_ASSERT(self
!= NULL
, return -1;);
834 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
837 case FINAL_TIMER_EXPIRED
:
838 if (self
->retry_count
< self
->N3
) {
840 * Perform random backoff, Wait a random number of time units, minimum
841 * duration half the time taken to transmitt a SNRM frame, maximum duration
842 * 1.5 times the time taken to transmit a SNRM frame. So this time should
843 * between 15 msecs and 45 msecs.
845 irlap_start_backoff_timer(self
, msecs_to_jiffies(20 +
848 /* Always switch state before calling upper layers */
849 irlap_next_state(self
, LAP_NDM
);
851 irlap_disconnect_indication(self
, LAP_FOUND_NONE
);
854 case BACKOFF_TIMER_EXPIRED
:
855 irlap_send_snrm_frame(self
, &self
->qos_rx
);
856 irlap_start_final_timer(self
, self
->final_timeout
);
860 pr_debug("%s(), SNRM battle!\n", __func__
);
862 IRDA_ASSERT(skb
!= NULL
, return 0;);
863 IRDA_ASSERT(info
!= NULL
, return 0;);
866 * The device with the largest device address wins the battle
867 * (both have sent a SNRM command!)
869 if (info
&&(info
->daddr
> self
->saddr
)) {
870 del_timer(&self
->final_timer
);
871 irlap_initiate_connection_state(self
);
873 IRDA_ASSERT(self
->netdev
!= NULL
, return -1;);
875 skb_pull(skb
, sizeof(struct snrm_frame
));
877 irlap_qos_negotiate(self
, skb
);
879 /* Send UA frame and then change link settings */
880 irlap_apply_connection_parameters(self
, FALSE
);
881 irlap_send_ua_response_frame(self
, &self
->qos_rx
);
883 irlap_next_state(self
, LAP_NRM_S
);
884 irlap_connect_confirm(self
, skb
);
887 * The WD-timer could be set to the duration of the
888 * P-timer for this case, but it is recommended
889 * to use twice the value (note 3 IrLAP p. 60).
891 irlap_start_wd_timer(self
, self
->wd_timeout
);
893 /* We just ignore the other device! */
894 irlap_next_state(self
, LAP_SETUP
);
899 del_timer(&self
->final_timer
);
901 /* Initiate connection state */
902 irlap_initiate_connection_state(self
);
904 /* Negotiate connection parameters */
905 IRDA_ASSERT(skb
->len
> 10, return -1;);
907 skb_pull(skb
, sizeof(struct ua_frame
));
909 IRDA_ASSERT(self
->netdev
!= NULL
, return -1;);
911 irlap_qos_negotiate(self
, skb
);
913 /* Set the new link setting *now* (before the rr frame) */
914 irlap_apply_connection_parameters(self
, TRUE
);
915 self
->retry_count
= 0;
917 /* Wait for turnaround time to give a chance to the other
918 * device to be ready to receive us.
919 * Note : the time to switch speed is typically larger
920 * than the turnaround time, but as we don't have the other
921 * side speed switch time, that's our best guess...
923 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
925 /* This frame will actually be sent at the new speed */
926 irlap_send_rr_frame(self
, CMD_FRAME
);
928 /* The timer is set to half the normal timer to quickly
929 * detect a failure to negotiate the new connection
930 * parameters. IrLAP 6.11.3.2, note 3.
931 * Note that currently we don't process this failure
932 * properly, as we should do a quick disconnect.
934 irlap_start_final_timer(self
, self
->final_timeout
/2);
935 irlap_next_state(self
, LAP_NRM_P
);
937 irlap_connect_confirm(self
, skb
);
939 case RECV_DM_RSP
: /* FALLTHROUGH */
941 del_timer(&self
->final_timer
);
942 irlap_next_state(self
, LAP_NDM
);
944 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
947 pr_debug("%s(), Unknown event %d, %s\n", __func__
,
948 event
, irlap_event
[event
]);
957 * Function irlap_state_offline (self, event, skb, info)
959 * OFFLINE state, not used for now!
962 static int irlap_state_offline(struct irlap_cb
*self
, IRLAP_EVENT event
,
963 struct sk_buff
*skb
, struct irlap_info
*info
)
965 pr_debug("%s(), Unknown event\n", __func__
);
971 * Function irlap_state_xmit_p (self, event, skb, info)
973 * XMIT, Only the primary station has right to transmit, and we
974 * therefore do not expect to receive any transmissions from other
978 static int irlap_state_xmit_p(struct irlap_cb
*self
, IRLAP_EVENT event
,
979 struct sk_buff
*skb
, struct irlap_info
*info
)
986 * Only send frame if send-window > 0.
988 if ((self
->window
> 0) && (!self
->remote_busy
)) {
990 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
991 struct sk_buff
*skb_next
;
993 /* With DYNAMIC_WINDOW, we keep the window size
994 * maximum, and adapt on the packets we are sending.
995 * At 115k, we can send only 2 packets of 2048 bytes
996 * in a 500 ms turnaround. Without this option, we
997 * would always limit the window to 2. With this
998 * option, if we send smaller packets, we can send
999 * up to 7 of them (always depending on QoS).
1002 /* Look at the next skb. This is safe, as we are
1003 * the only consumer of the Tx queue (if we are not,
1004 * we have other problems) - Jean II */
1005 skb_next
= skb_peek(&self
->txq
);
1007 /* Check if a subsequent skb exist and would fit in
1008 * the current window (with respect to turnaround
1010 * This allow us to properly mark the current packet
1011 * with the pf bit, to avoid falling back on the
1012 * second test below, and avoid waiting the
1013 * end of the window and sending a extra RR.
1014 * Note : (skb_next != NULL) <=> (skb_queue_len() > 0)
1016 nextfit
= ((skb_next
!= NULL
) &&
1017 ((skb_next
->len
+ skb
->len
) <=
1021 * The current packet may not fit ! Because of test
1022 * above, this should not happen any more !!!
1023 * Test if we have transmitted more bytes over the
1024 * link than its possible to do with the current
1025 * speed and turn-around-time.
1027 if((!nextfit
) && (skb
->len
> self
->bytes_left
)) {
1028 pr_debug("%s(), Not allowed to transmit more bytes!\n",
1030 /* Requeue the skb */
1031 skb_queue_head(&self
->txq
, skb_get(skb
));
1033 * We should switch state to LAP_NRM_P, but
1034 * that is not possible since we must be sure
1035 * that we poll the other side. Since we have
1036 * used up our time, the poll timer should
1037 * trigger anyway now, so we just wait for it
1041 * Sorry, but that's not totally true. If
1042 * we send 2000B packets, we may wait another
1043 * 1000B until our turnaround expire. That's
1044 * why we need to be proactive in avoiding
1045 * coming here. - Jean II
1050 /* Subtract space used by this skb */
1051 self
->bytes_left
-= skb
->len
;
1052 #else /* CONFIG_IRDA_DYNAMIC_WINDOW */
1053 /* Window has been adjusted for the max packet
1054 * size, so much simpler... - Jean II */
1055 nextfit
= !skb_queue_empty(&self
->txq
);
1056 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1058 * Send data with poll bit cleared only if window > 1
1059 * and there is more frames after this one to be sent
1061 if ((self
->window
> 1) && (nextfit
)) {
1062 /* More packet to send in current window */
1063 irlap_send_data_primary(self
, skb
);
1064 irlap_next_state(self
, LAP_XMIT_P
);
1066 /* Final packet of window */
1067 irlap_send_data_primary_poll(self
, skb
);
1070 * Make sure state machine does not try to send
1075 #ifdef CONFIG_IRDA_FAST_RR
1076 /* Peer may want to reply immediately */
1077 self
->fast_RR
= FALSE
;
1078 #endif /* CONFIG_IRDA_FAST_RR */
1080 pr_debug("%s(), Unable to send! remote busy?\n",
1082 skb_queue_head(&self
->txq
, skb_get(skb
));
1085 * The next ret is important, because it tells
1086 * irlap_next_state _not_ to deliver more frames
1091 case POLL_TIMER_EXPIRED
:
1092 pr_debug("%s(), POLL_TIMER_EXPIRED <%ld>\n",
1094 irlap_send_rr_frame(self
, CMD_FRAME
);
1095 /* Return to NRM properly - Jean II */
1096 self
->window
= self
->window_size
;
1097 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1098 /* Allowed to transmit a maximum number of bytes again. */
1099 self
->bytes_left
= self
->line_capacity
;
1100 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1101 irlap_start_final_timer(self
, self
->final_timeout
);
1102 irlap_next_state(self
, LAP_NRM_P
);
1104 case DISCONNECT_REQUEST
:
1105 del_timer(&self
->poll_timer
);
1106 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1107 irlap_send_disc_frame(self
);
1108 irlap_flush_all_queues(self
);
1109 irlap_start_final_timer(self
, self
->final_timeout
);
1110 self
->retry_count
= 0;
1111 irlap_next_state(self
, LAP_PCLOSE
);
1114 /* Nothing to do, irlap_do_event() will send the packet
1115 * when we return... - Jean II */
1118 pr_debug("%s(), Unknown event %s\n",
1119 __func__
, irlap_event
[event
]);
1128 * Function irlap_state_pclose (event, skb, info)
1132 static int irlap_state_pclose(struct irlap_cb
*self
, IRLAP_EVENT event
,
1133 struct sk_buff
*skb
, struct irlap_info
*info
)
1137 IRDA_ASSERT(self
!= NULL
, return -1;);
1138 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
1141 case RECV_UA_RSP
: /* FALLTHROUGH */
1143 del_timer(&self
->final_timer
);
1145 /* Set new link parameters */
1146 irlap_apply_default_connection_parameters(self
);
1148 /* Always switch state before calling upper layers */
1149 irlap_next_state(self
, LAP_NDM
);
1151 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
1153 case FINAL_TIMER_EXPIRED
:
1154 if (self
->retry_count
< self
->N3
) {
1155 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1156 irlap_send_disc_frame(self
);
1157 irlap_start_final_timer(self
, self
->final_timeout
);
1158 self
->retry_count
++;
1161 irlap_apply_default_connection_parameters(self
);
1163 /* Always switch state before calling upper layers */
1164 irlap_next_state(self
, LAP_NDM
);
1166 irlap_disconnect_indication(self
, LAP_NO_RESPONSE
);
1170 pr_debug("%s(), Unknown event %d\n", __func__
, event
);
1179 * Function irlap_state_nrm_p (self, event, skb, info)
1181 * NRM_P (Normal Response Mode as Primary), The primary station has given
1182 * permissions to a secondary station to transmit IrLAP resonse frames
1183 * (by sending a frame with the P bit set). The primary station will not
1184 * transmit any frames and is expecting to receive frames only from the
1185 * secondary to which transmission permissions has been given.
1187 static int irlap_state_nrm_p(struct irlap_cb
*self
, IRLAP_EVENT event
,
1188 struct sk_buff
*skb
, struct irlap_info
*info
)
1195 case RECV_I_RSP
: /* Optimize for the common case */
1196 if (unlikely(skb
->len
<= LAP_ADDR_HEADER
+ LAP_CTRL_HEADER
)) {
1198 * Input validation check: a stir4200/mcp2150
1199 * combination sometimes results in an empty i:rsp.
1200 * This makes no sense; we can just ignore the frame
1201 * and send an rr:cmd immediately. This happens before
1202 * changing nr or ns so triggers a retransmit
1204 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1205 irlap_send_rr_frame(self
, CMD_FRAME
);
1209 /* FIXME: must check for remote_busy below */
1210 #ifdef CONFIG_IRDA_FAST_RR
1212 * Reset the fast_RR so we can use the fast RR code with
1213 * full speed the next time since peer may have more frames
1216 self
->fast_RR
= FALSE
;
1217 #endif /* CONFIG_IRDA_FAST_RR */
1218 IRDA_ASSERT( info
!= NULL
, return -1;);
1220 ns_status
= irlap_validate_ns_received(self
, info
->ns
);
1221 nr_status
= irlap_validate_nr_received(self
, info
->nr
);
1224 * Check for expected I(nformation) frame
1226 if ((ns_status
== NS_EXPECTED
) && (nr_status
== NR_EXPECTED
)) {
1228 /* Update Vr (next frame for us to receive) */
1229 self
->vr
= (self
->vr
+ 1) % 8;
1231 /* Update Nr received, cleanup our retry queue */
1232 irlap_update_nr_received(self
, info
->nr
);
1235 * Got expected NR, so reset the
1236 * retry_count. This is not done by IrLAP spec,
1239 self
->retry_count
= 0;
1240 self
->ack_required
= TRUE
;
1242 /* poll bit cleared? */
1244 /* Keep state, do not move this line */
1245 irlap_next_state(self
, LAP_NRM_P
);
1247 irlap_data_indication(self
, skb
, FALSE
);
1249 /* No longer waiting for pf */
1250 del_timer(&self
->final_timer
);
1252 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1254 /* Call higher layer *before* changing state
1255 * to give them a chance to send data in the
1258 irlap_data_indication(self
, skb
, FALSE
);
1260 /* XMIT states are the most dangerous state
1261 * to be in, because user requests are
1262 * processed directly and may change state.
1263 * On the other hand, in NDM_P, those
1264 * requests are queued and we will process
1265 * them when we return to irlap_do_event().
1268 irlap_next_state(self
, LAP_XMIT_P
);
1270 /* This is the last frame.
1271 * Make sure it's always called in XMIT state.
1273 irlap_start_poll_timer(self
, self
->poll_timeout
);
1278 /* Unexpected next to send (Ns) */
1279 if ((ns_status
== NS_UNEXPECTED
) && (nr_status
== NR_EXPECTED
))
1282 irlap_update_nr_received(self
, info
->nr
);
1285 * Wait until the last frame before doing
1290 irlap_next_state(self
, LAP_NRM_P
);
1292 pr_debug("%s(), missing or duplicate frame!\n",
1295 /* Update Nr received */
1296 irlap_update_nr_received(self
, info
->nr
);
1298 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1299 irlap_send_rr_frame(self
, CMD_FRAME
);
1301 self
->ack_required
= FALSE
;
1303 irlap_start_final_timer(self
, self
->final_timeout
);
1304 irlap_next_state(self
, LAP_NRM_P
);
1309 * Unexpected next to receive (Nr)
1311 if ((ns_status
== NS_EXPECTED
) && (nr_status
== NR_UNEXPECTED
))
1314 self
->vr
= (self
->vr
+ 1) % 8;
1316 /* Update Nr received */
1317 irlap_update_nr_received(self
, info
->nr
);
1319 /* Resend rejected frames */
1320 irlap_resend_rejected_frames(self
, CMD_FRAME
);
1322 self
->ack_required
= FALSE
;
1324 /* Make sure we account for the time
1325 * to transmit our frames. See comemnts
1326 * in irlap_send_data_primary_poll().
1328 irlap_start_final_timer(self
, 2 * self
->final_timeout
);
1330 /* Keep state, do not move this line */
1331 irlap_next_state(self
, LAP_NRM_P
);
1333 irlap_data_indication(self
, skb
, FALSE
);
1336 * Do not resend frames until the last
1337 * frame has arrived from the other
1338 * device. This is not documented in
1341 self
->vr
= (self
->vr
+ 1) % 8;
1343 /* Update Nr received */
1344 irlap_update_nr_received(self
, info
->nr
);
1346 self
->ack_required
= FALSE
;
1348 /* Keep state, do not move this line!*/
1349 irlap_next_state(self
, LAP_NRM_P
);
1351 irlap_data_indication(self
, skb
, FALSE
);
1356 * Unexpected next to send (Ns) and next to receive (Nr)
1357 * Not documented by IrLAP!
1359 if ((ns_status
== NS_UNEXPECTED
) &&
1360 (nr_status
== NR_UNEXPECTED
))
1362 pr_debug("%s(), unexpected nr and ns!\n",
1365 /* Resend rejected frames */
1366 irlap_resend_rejected_frames(self
, CMD_FRAME
);
1368 /* Give peer some time to retransmit!
1369 * But account for our own Tx. */
1370 irlap_start_final_timer(self
, 2 * self
->final_timeout
);
1372 /* Keep state, do not move this line */
1373 irlap_next_state(self
, LAP_NRM_P
);
1375 /* Update Nr received */
1376 /* irlap_update_nr_received( info->nr); */
1378 self
->ack_required
= FALSE
;
1386 if ((nr_status
== NR_INVALID
) || (ns_status
== NS_INVALID
)) {
1388 del_timer(&self
->final_timer
);
1390 irlap_next_state(self
, LAP_RESET_WAIT
);
1392 irlap_disconnect_indication(self
, LAP_RESET_INDICATION
);
1393 self
->xmitflag
= TRUE
;
1395 del_timer(&self
->final_timer
);
1397 irlap_disconnect_indication(self
, LAP_RESET_INDICATION
);
1399 self
->xmitflag
= FALSE
;
1403 pr_debug("%s(), Not implemented!\n", __func__
);
1404 pr_debug("%s(), event=%s, ns_status=%d, nr_status=%d\n",
1405 __func__
, irlap_event
[event
], ns_status
, nr_status
);
1408 /* Poll bit cleared? */
1410 irlap_data_indication(self
, skb
, TRUE
);
1411 irlap_next_state(self
, LAP_NRM_P
);
1413 del_timer(&self
->final_timer
);
1414 irlap_data_indication(self
, skb
, TRUE
);
1415 irlap_next_state(self
, LAP_XMIT_P
);
1416 pr_debug("%s: RECV_UI_FRAME: next state %s\n",
1417 __func__
, irlap_state
[self
->state
]);
1418 irlap_start_poll_timer(self
, self
->poll_timeout
);
1423 * If you get a RR, the remote isn't busy anymore,
1424 * no matter what the NR
1426 self
->remote_busy
= FALSE
;
1428 /* Stop final timer */
1429 del_timer(&self
->final_timer
);
1434 ret
= irlap_validate_nr_received(self
, info
->nr
);
1435 if (ret
== NR_EXPECTED
) {
1436 /* Update Nr received */
1437 irlap_update_nr_received(self
, info
->nr
);
1440 * Got expected NR, so reset the retry_count. This
1441 * is not done by the IrLAP standard , which is
1444 self
->retry_count
= 0;
1445 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1447 irlap_next_state(self
, LAP_XMIT_P
);
1449 /* Start poll timer */
1450 irlap_start_poll_timer(self
, self
->poll_timeout
);
1451 } else if (ret
== NR_UNEXPECTED
) {
1452 IRDA_ASSERT(info
!= NULL
, return -1;);
1457 /* Update Nr received */
1458 irlap_update_nr_received(self
, info
->nr
);
1460 pr_debug("RECV_RR_FRAME: Retrans:%d, nr=%d, va=%d, vs=%d, vr=%d\n",
1461 self
->retry_count
, info
->nr
, self
->va
,
1462 self
->vs
, self
->vr
);
1464 /* Resend rejected frames */
1465 irlap_resend_rejected_frames(self
, CMD_FRAME
);
1466 irlap_start_final_timer(self
, self
->final_timeout
* 2);
1468 irlap_next_state(self
, LAP_NRM_P
);
1469 } else if (ret
== NR_INVALID
) {
1470 pr_debug("%s(), Received RR with invalid nr !\n",
1473 irlap_next_state(self
, LAP_RESET_WAIT
);
1475 irlap_disconnect_indication(self
, LAP_RESET_INDICATION
);
1476 self
->xmitflag
= TRUE
;
1480 IRDA_ASSERT(info
!= NULL
, return -1;);
1482 /* Stop final timer */
1483 del_timer(&self
->final_timer
);
1484 self
->remote_busy
= TRUE
;
1486 /* Update Nr received */
1487 irlap_update_nr_received(self
, info
->nr
);
1488 irlap_next_state(self
, LAP_XMIT_P
);
1490 /* Start poll timer */
1491 irlap_start_poll_timer(self
, self
->poll_timeout
);
1494 del_timer(&self
->final_timer
);
1495 self
->xmitflag
= TRUE
;
1496 irlap_next_state(self
, LAP_RESET_WAIT
);
1497 irlap_reset_indication(self
);
1499 case FINAL_TIMER_EXPIRED
:
1501 * We are allowed to wait for additional 300 ms if
1502 * final timer expires when we are in the middle
1503 * of receiving a frame (page 45, IrLAP). Check that
1504 * we only do this once for each frame.
1506 if (irda_device_is_receiving(self
->netdev
) && !self
->add_wait
) {
1507 pr_debug("FINAL_TIMER_EXPIRED when receiving a frame! Waiting a little bit more!\n");
1508 irlap_start_final_timer(self
, msecs_to_jiffies(300));
1511 * Don't allow this to happen one more time in a row,
1512 * or else we can get a pretty tight loop here if
1513 * if we only receive half a frame. DB.
1515 self
->add_wait
= TRUE
;
1518 self
->add_wait
= FALSE
;
1520 /* N2 is the disconnect timer. Until we reach it, we retry */
1521 if (self
->retry_count
< self
->N2
) {
1522 if (skb_peek(&self
->wx_list
) == NULL
) {
1523 /* Retry sending the pf bit to the secondary */
1524 pr_debug("nrm_p: resending rr");
1525 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1526 irlap_send_rr_frame(self
, CMD_FRAME
);
1528 pr_debug("nrm_p: resend frames");
1529 irlap_resend_rejected_frames(self
, CMD_FRAME
);
1532 irlap_start_final_timer(self
, self
->final_timeout
);
1533 self
->retry_count
++;
1534 pr_debug("irlap_state_nrm_p: FINAL_TIMER_EXPIRED: retry_count=%d\n",
1537 /* Early warning event. I'm using a pretty liberal
1538 * interpretation of the spec and generate an event
1539 * every time the timer is multiple of N1 (and not
1540 * only the first time). This allow application
1541 * to know precisely if connectivity restart...
1543 if((self
->retry_count
% self
->N1
) == 0)
1544 irlap_status_indication(self
,
1545 STATUS_NO_ACTIVITY
);
1549 irlap_apply_default_connection_parameters(self
);
1551 /* Always switch state before calling upper layers */
1552 irlap_next_state(self
, LAP_NDM
);
1553 irlap_disconnect_indication(self
, LAP_NO_RESPONSE
);
1557 irlap_update_nr_received(self
, info
->nr
);
1558 if (self
->remote_busy
) {
1559 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1560 irlap_send_rr_frame(self
, CMD_FRAME
);
1562 irlap_resend_rejected_frames(self
, CMD_FRAME
);
1563 irlap_start_final_timer(self
, 2 * self
->final_timeout
);
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_frame(self
, CMD_FRAME
);
1572 irlap_start_final_timer(self
, 2 * self
->final_timeout
);
1575 pr_debug("%s(), RECV_RD_RSP\n", __func__
);
1577 irlap_flush_all_queues(self
);
1578 irlap_next_state(self
, LAP_XMIT_P
);
1579 /* Call back the LAP state machine to do a proper disconnect */
1580 irlap_disconnect_request(self
);
1583 pr_debug("%s(), Unknown event %s\n",
1584 __func__
, irlap_event
[event
]);
1593 * Function irlap_state_reset_wait (event, skb, info)
1595 * We have informed the service user of a reset condition, and is
1596 * awaiting reset of disconnect request.
1599 static int irlap_state_reset_wait(struct irlap_cb
*self
, IRLAP_EVENT event
,
1600 struct sk_buff
*skb
, struct irlap_info
*info
)
1604 pr_debug("%s(), event = %s\n", __func__
, irlap_event
[event
]);
1606 IRDA_ASSERT(self
!= NULL
, return -1;);
1607 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
1611 if (self
->xmitflag
) {
1612 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1613 irlap_send_snrm_frame(self
, NULL
);
1614 irlap_start_final_timer(self
, self
->final_timeout
);
1615 irlap_next_state(self
, LAP_RESET
);
1617 irlap_start_final_timer(self
, self
->final_timeout
);
1618 irlap_next_state(self
, LAP_RESET
);
1621 case DISCONNECT_REQUEST
:
1622 irlap_wait_min_turn_around( self
, &self
->qos_tx
);
1623 irlap_send_disc_frame( self
);
1624 irlap_flush_all_queues( self
);
1625 irlap_start_final_timer( self
, self
->final_timeout
);
1626 self
->retry_count
= 0;
1627 irlap_next_state( self
, LAP_PCLOSE
);
1630 pr_debug("%s(), Unknown event %s\n", __func__
,
1631 irlap_event
[event
]);
1640 * Function irlap_state_reset (self, event, skb, info)
1642 * We have sent a SNRM reset command to the peer layer, and is awaiting
1646 static int irlap_state_reset(struct irlap_cb
*self
, IRLAP_EVENT event
,
1647 struct sk_buff
*skb
, struct irlap_info
*info
)
1651 pr_debug("%s(), event = %s\n", __func__
, irlap_event
[event
]);
1653 IRDA_ASSERT(self
!= NULL
, return -1;);
1654 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
1658 del_timer(&self
->final_timer
);
1660 irlap_apply_default_connection_parameters(self
);
1662 /* Always switch state before calling upper layers */
1663 irlap_next_state(self
, LAP_NDM
);
1665 irlap_disconnect_indication(self
, LAP_NO_RESPONSE
);
1669 del_timer(&self
->final_timer
);
1671 /* Initiate connection state */
1672 irlap_initiate_connection_state(self
);
1674 irlap_reset_confirm();
1676 self
->remote_busy
= FALSE
;
1678 irlap_next_state(self
, LAP_XMIT_P
);
1680 irlap_start_poll_timer(self
, self
->poll_timeout
);
1683 case FINAL_TIMER_EXPIRED
:
1684 if (self
->retry_count
< 3) {
1685 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1687 IRDA_ASSERT(self
->netdev
!= NULL
, return -1;);
1688 irlap_send_snrm_frame(self
, self
->qos_dev
);
1690 self
->retry_count
++; /* Experimental!! */
1692 irlap_start_final_timer(self
, self
->final_timeout
);
1693 irlap_next_state(self
, LAP_RESET
);
1694 } else if (self
->retry_count
>= self
->N3
) {
1695 irlap_apply_default_connection_parameters(self
);
1697 /* Always switch state before calling upper layers */
1698 irlap_next_state(self
, LAP_NDM
);
1700 irlap_disconnect_indication(self
, LAP_NO_RESPONSE
);
1705 * SNRM frame is not allowed to contain an I-field in this
1709 pr_debug("%s(), RECV_SNRM_CMD\n", __func__
);
1710 irlap_initiate_connection_state(self
);
1711 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1712 irlap_send_ua_response_frame(self
, &self
->qos_rx
);
1713 irlap_reset_confirm();
1714 irlap_start_wd_timer(self
, self
->wd_timeout
);
1715 irlap_next_state(self
, LAP_NDM
);
1717 pr_debug("%s(), SNRM frame contained an I field!\n",
1722 pr_debug("%s(), Unknown event %s\n",
1723 __func__
, irlap_event
[event
]);
1732 * Function irlap_state_xmit_s (event, skb, info)
1734 * XMIT_S, The secondary station has been given the right to transmit,
1735 * and we therefore do not expect to receive any transmissions from other
1738 static int irlap_state_xmit_s(struct irlap_cb
*self
, IRLAP_EVENT event
,
1739 struct sk_buff
*skb
, struct irlap_info
*info
)
1743 pr_debug("%s(), event=%s\n", __func__
, irlap_event
[event
]);
1745 IRDA_ASSERT(self
!= NULL
, return -ENODEV
;);
1746 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -EBADR
;);
1751 * Send frame only if send window > 0
1753 if ((self
->window
> 0) && (!self
->remote_busy
)) {
1755 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1756 struct sk_buff
*skb_next
;
1759 * Same deal as in irlap_state_xmit_p(), so see
1760 * the comments at that point.
1761 * We are the secondary, so there are only subtle
1762 * differences. - Jean II
1765 /* Check if a subsequent skb exist and would fit in
1766 * the current window (with respect to turnaround
1767 * time). - Jean II */
1768 skb_next
= skb_peek(&self
->txq
);
1769 nextfit
= ((skb_next
!= NULL
) &&
1770 ((skb_next
->len
+ skb
->len
) <=
1774 * Test if we have transmitted more bytes over the
1775 * link than its possible to do with the current
1776 * speed and turn-around-time.
1778 if((!nextfit
) && (skb
->len
> self
->bytes_left
)) {
1779 pr_debug("%s(), Not allowed to transmit more bytes!\n",
1781 /* Requeue the skb */
1782 skb_queue_head(&self
->txq
, skb_get(skb
));
1785 * Switch to NRM_S, this is only possible
1786 * when we are in secondary mode, since we
1787 * must be sure that we don't miss any RR
1790 self
->window
= self
->window_size
;
1791 self
->bytes_left
= self
->line_capacity
;
1792 irlap_start_wd_timer(self
, self
->wd_timeout
);
1794 irlap_next_state(self
, LAP_NRM_S
);
1795 /* Slight difference with primary :
1796 * here we would wait for the other side to
1797 * expire the turnaround. - Jean II */
1799 return -EPROTO
; /* Try again later */
1801 /* Subtract space used by this skb */
1802 self
->bytes_left
-= skb
->len
;
1803 #else /* CONFIG_IRDA_DYNAMIC_WINDOW */
1804 /* Window has been adjusted for the max packet
1805 * size, so much simpler... - Jean II */
1806 nextfit
= !skb_queue_empty(&self
->txq
);
1807 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1809 * Send data with final bit cleared only if window > 1
1810 * and there is more frames to be sent
1812 if ((self
->window
> 1) && (nextfit
)) {
1813 irlap_send_data_secondary(self
, skb
);
1814 irlap_next_state(self
, LAP_XMIT_S
);
1816 irlap_send_data_secondary_final(self
, skb
);
1817 irlap_next_state(self
, LAP_NRM_S
);
1820 * Make sure state machine does not try to send
1826 pr_debug("%s(), Unable to send!\n", __func__
);
1827 skb_queue_head(&self
->txq
, skb_get(skb
));
1831 case DISCONNECT_REQUEST
:
1832 irlap_send_rd_frame(self
);
1833 irlap_flush_all_queues(self
);
1834 irlap_start_wd_timer(self
, self
->wd_timeout
);
1835 irlap_next_state(self
, LAP_SCLOSE
);
1838 /* Nothing to do, irlap_do_event() will send the packet
1839 * when we return... - Jean II */
1842 pr_debug("%s(), Unknown event %s\n", __func__
,
1843 irlap_event
[event
]);
1852 * Function irlap_state_nrm_s (event, skb, info)
1854 * NRM_S (Normal Response Mode as Secondary) state, in this state we are
1855 * expecting to receive frames from the primary station
1858 static int irlap_state_nrm_s(struct irlap_cb
*self
, IRLAP_EVENT event
,
1859 struct sk_buff
*skb
, struct irlap_info
*info
)
1865 pr_debug("%s(), event=%s\n", __func__
, irlap_event
[event
]);
1867 IRDA_ASSERT(self
!= NULL
, return -1;);
1868 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
1871 case RECV_I_CMD
: /* Optimize for the common case */
1872 /* FIXME: must check for remote_busy below */
1873 pr_debug("%s(), event=%s nr=%d, vs=%d, ns=%d, vr=%d, pf=%d\n",
1874 __func__
, irlap_event
[event
], info
->nr
,
1875 self
->vs
, info
->ns
, self
->vr
, info
->pf
);
1877 self
->retry_count
= 0;
1879 ns_status
= irlap_validate_ns_received(self
, info
->ns
);
1880 nr_status
= irlap_validate_nr_received(self
, info
->nr
);
1882 * Check for expected I(nformation) frame
1884 if ((ns_status
== NS_EXPECTED
) && (nr_status
== NR_EXPECTED
)) {
1886 /* Update Vr (next frame for us to receive) */
1887 self
->vr
= (self
->vr
+ 1) % 8;
1889 /* Update Nr received */
1890 irlap_update_nr_received(self
, info
->nr
);
1897 self
->ack_required
= TRUE
;
1900 * Starting WD-timer here is optional, but
1901 * not recommended. Note 6 IrLAP p. 83
1904 irda_start_timer(WD_TIMER
, self
->wd_timeout
);
1906 /* Keep state, do not move this line */
1907 irlap_next_state(self
, LAP_NRM_S
);
1909 irlap_data_indication(self
, skb
, FALSE
);
1913 * We should wait before sending RR, and
1914 * also before changing to XMIT_S
1915 * state. (note 1, IrLAP p. 82)
1917 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1920 * Give higher layers a chance to
1921 * immediately reply with some data before
1922 * we decide if we should send a RR frame
1925 irlap_data_indication(self
, skb
, FALSE
);
1927 /* Any pending data requests? */
1928 if (!skb_queue_empty(&self
->txq
) &&
1931 self
->ack_required
= TRUE
;
1933 del_timer(&self
->wd_timer
);
1935 irlap_next_state(self
, LAP_XMIT_S
);
1937 irlap_send_rr_frame(self
, RSP_FRAME
);
1938 irlap_start_wd_timer(self
,
1941 /* Keep the state */
1942 irlap_next_state(self
, LAP_NRM_S
);
1948 * Check for Unexpected next to send (Ns)
1950 if ((ns_status
== NS_UNEXPECTED
) && (nr_status
== NR_EXPECTED
))
1952 /* Unexpected next to send, with final bit cleared */
1954 irlap_update_nr_received(self
, info
->nr
);
1956 irlap_start_wd_timer(self
, self
->wd_timeout
);
1958 /* Update Nr received */
1959 irlap_update_nr_received(self
, info
->nr
);
1961 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1962 irlap_send_rr_frame(self
, RSP_FRAME
);
1964 irlap_start_wd_timer(self
, self
->wd_timeout
);
1970 * Unexpected Next to Receive(NR) ?
1972 if ((ns_status
== NS_EXPECTED
) && (nr_status
== NR_UNEXPECTED
))
1975 pr_debug("RECV_I_RSP: frame(s) lost\n");
1977 self
->vr
= (self
->vr
+ 1) % 8;
1979 /* Update Nr received */
1980 irlap_update_nr_received(self
, info
->nr
);
1982 /* Resend rejected frames */
1983 irlap_resend_rejected_frames(self
, RSP_FRAME
);
1985 /* Keep state, do not move this line */
1986 irlap_next_state(self
, LAP_NRM_S
);
1988 irlap_data_indication(self
, skb
, FALSE
);
1989 irlap_start_wd_timer(self
, self
->wd_timeout
);
1993 * This is not documented in IrLAP!! Unexpected NR
1994 * with poll bit cleared
1997 self
->vr
= (self
->vr
+ 1) % 8;
1999 /* Update Nr received */
2000 irlap_update_nr_received(self
, info
->nr
);
2002 /* Keep state, do not move this line */
2003 irlap_next_state(self
, LAP_NRM_S
);
2005 irlap_data_indication(self
, skb
, FALSE
);
2006 irlap_start_wd_timer(self
, self
->wd_timeout
);
2011 if (ret
== NR_INVALID
) {
2012 pr_debug("NRM_S, NR_INVALID not implemented!\n");
2014 if (ret
== NS_INVALID
) {
2015 pr_debug("NRM_S, NS_INVALID not implemented!\n");
2023 irlap_data_indication(self
, skb
, TRUE
);
2024 irlap_next_state(self
, LAP_NRM_S
); /* Keep state */
2027 * Any pending data requests?
2029 if (!skb_queue_empty(&self
->txq
) &&
2030 (self
->window
> 0) && !self
->remote_busy
)
2032 irlap_data_indication(self
, skb
, TRUE
);
2034 del_timer(&self
->wd_timer
);
2036 irlap_next_state(self
, LAP_XMIT_S
);
2038 irlap_data_indication(self
, skb
, TRUE
);
2040 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2042 irlap_send_rr_frame(self
, RSP_FRAME
);
2043 self
->ack_required
= FALSE
;
2045 irlap_start_wd_timer(self
, self
->wd_timeout
);
2047 /* Keep the state */
2048 irlap_next_state(self
, LAP_NRM_S
);
2053 self
->retry_count
= 0;
2058 nr_status
= irlap_validate_nr_received(self
, info
->nr
);
2059 if (nr_status
== NR_EXPECTED
) {
2060 if (!skb_queue_empty(&self
->txq
) &&
2061 (self
->window
> 0)) {
2062 self
->remote_busy
= FALSE
;
2064 /* Update Nr received */
2065 irlap_update_nr_received(self
, info
->nr
);
2066 del_timer(&self
->wd_timer
);
2068 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2069 irlap_next_state(self
, LAP_XMIT_S
);
2071 self
->remote_busy
= FALSE
;
2072 /* Update Nr received */
2073 irlap_update_nr_received(self
, info
->nr
);
2074 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2075 irlap_start_wd_timer(self
, self
->wd_timeout
);
2077 /* Note : if the link is idle (this case),
2078 * we never go in XMIT_S, so we never get a
2079 * chance to process any DISCONNECT_REQUEST.
2080 * Do it now ! - Jean II */
2081 if (self
->disconnect_pending
) {
2083 irlap_send_rd_frame(self
);
2084 irlap_flush_all_queues(self
);
2086 irlap_next_state(self
, LAP_SCLOSE
);
2088 /* Just send back pf bit */
2089 irlap_send_rr_frame(self
, RSP_FRAME
);
2091 irlap_next_state(self
, LAP_NRM_S
);
2094 } else if (nr_status
== NR_UNEXPECTED
) {
2095 self
->remote_busy
= FALSE
;
2096 irlap_update_nr_received(self
, info
->nr
);
2097 irlap_resend_rejected_frames(self
, RSP_FRAME
);
2099 irlap_start_wd_timer(self
, self
->wd_timeout
);
2102 irlap_next_state(self
, LAP_NRM_S
);
2104 pr_debug("%s(), invalid nr not implemented!\n",
2109 /* SNRM frame is not allowed to contain an I-field */
2111 del_timer(&self
->wd_timer
);
2112 pr_debug("%s(), received SNRM cmd\n", __func__
);
2113 irlap_next_state(self
, LAP_RESET_CHECK
);
2115 irlap_reset_indication(self
);
2117 pr_debug("%s(), SNRM frame contained an I-field!\n",
2123 irlap_update_nr_received(self
, info
->nr
);
2124 if (self
->remote_busy
) {
2125 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2126 irlap_send_rr_frame(self
, RSP_FRAME
);
2128 irlap_resend_rejected_frames(self
, RSP_FRAME
);
2129 irlap_start_wd_timer(self
, self
->wd_timeout
);
2132 irlap_update_nr_received(self
, info
->nr
);
2133 if (self
->remote_busy
) {
2134 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2135 irlap_send_rr_frame(self
, RSP_FRAME
);
2137 irlap_resend_rejected_frame(self
, RSP_FRAME
);
2138 irlap_start_wd_timer(self
, self
->wd_timeout
);
2140 case WD_TIMER_EXPIRED
:
2142 * Wait until retry_count * n matches negotiated threshold/
2143 * disconnect time (note 2 in IrLAP p. 82)
2145 * Similar to irlap_state_nrm_p() -> FINAL_TIMER_EXPIRED
2146 * Note : self->wd_timeout = (self->final_timeout * 2),
2147 * which explain why we use (self->N2 / 2) here !!!
2150 pr_debug("%s(), retry_count = %d\n", __func__
,
2153 if (self
->retry_count
< (self
->N2
/ 2)) {
2154 /* No retry, just wait for primary */
2155 irlap_start_wd_timer(self
, self
->wd_timeout
);
2156 self
->retry_count
++;
2158 if((self
->retry_count
% (self
->N1
/ 2)) == 0)
2159 irlap_status_indication(self
,
2160 STATUS_NO_ACTIVITY
);
2162 irlap_apply_default_connection_parameters(self
);
2164 /* Always switch state before calling upper layers */
2165 irlap_next_state(self
, LAP_NDM
);
2166 irlap_disconnect_indication(self
, LAP_NO_RESPONSE
);
2170 /* Always switch state before calling upper layers */
2171 irlap_next_state(self
, LAP_NDM
);
2173 /* Send disconnect response */
2174 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2175 irlap_send_ua_response_frame(self
, NULL
);
2177 del_timer(&self
->wd_timer
);
2178 irlap_flush_all_queues(self
);
2179 /* Set default link parameters */
2180 irlap_apply_default_connection_parameters(self
);
2182 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
2184 case RECV_DISCOVERY_XID_CMD
:
2185 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2186 irlap_send_rr_frame(self
, RSP_FRAME
);
2187 self
->ack_required
= TRUE
;
2188 irlap_start_wd_timer(self
, self
->wd_timeout
);
2189 irlap_next_state(self
, LAP_NRM_S
);
2193 /* Remove test frame header (only LAP header in NRM) */
2194 skb_pull(skb
, LAP_ADDR_HEADER
+ LAP_CTRL_HEADER
);
2196 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2197 irlap_start_wd_timer(self
, self
->wd_timeout
);
2199 /* Send response (info will be copied) */
2200 irlap_send_test_frame(self
, self
->caddr
, info
->daddr
, skb
);
2203 pr_debug("%s(), Unknown event %d, (%s)\n", __func__
,
2204 event
, irlap_event
[event
]);
2213 * Function irlap_state_sclose (self, event, skb, info)
2215 static int irlap_state_sclose(struct irlap_cb
*self
, IRLAP_EVENT event
,
2216 struct sk_buff
*skb
, struct irlap_info
*info
)
2218 IRDA_ASSERT(self
!= NULL
, return -ENODEV
;);
2219 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -EBADR
;);
2223 /* Always switch state before calling upper layers */
2224 irlap_next_state(self
, LAP_NDM
);
2226 /* Send disconnect response */
2227 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2228 irlap_send_ua_response_frame(self
, NULL
);
2230 del_timer(&self
->wd_timer
);
2231 /* Set default link parameters */
2232 irlap_apply_default_connection_parameters(self
);
2234 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
2237 /* IrLAP-1.1 p.82: in SCLOSE, S and I type RSP frames
2238 * shall take us down into default NDM state, like DM_RSP
2245 /* Always switch state before calling upper layers */
2246 irlap_next_state(self
, LAP_NDM
);
2248 del_timer(&self
->wd_timer
);
2249 irlap_apply_default_connection_parameters(self
);
2251 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
2253 case WD_TIMER_EXPIRED
:
2254 /* Always switch state before calling upper layers */
2255 irlap_next_state(self
, LAP_NDM
);
2257 irlap_apply_default_connection_parameters(self
);
2259 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
2262 /* IrLAP-1.1 p.82: in SCLOSE, basically any received frame
2263 * with pf=1 shall restart the wd-timer and resend the rd:rsp
2265 if (info
!= NULL
&& info
->pf
) {
2266 del_timer(&self
->wd_timer
);
2267 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2268 irlap_send_rd_frame(self
);
2269 irlap_start_wd_timer(self
, self
->wd_timeout
);
2270 break; /* stay in SCLOSE */
2273 pr_debug("%s(), Unknown event %d, (%s)\n", __func__
,
2274 event
, irlap_event
[event
]);
2282 static int irlap_state_reset_check( struct irlap_cb
*self
, IRLAP_EVENT event
,
2283 struct sk_buff
*skb
,
2284 struct irlap_info
*info
)
2288 pr_debug("%s(), event=%s\n", __func__
, irlap_event
[event
]);
2290 IRDA_ASSERT(self
!= NULL
, return -ENODEV
;);
2291 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -EBADR
;);
2294 case RESET_RESPONSE
:
2295 irlap_send_ua_response_frame(self
, &self
->qos_rx
);
2296 irlap_initiate_connection_state(self
);
2297 irlap_start_wd_timer(self
, WD_TIMEOUT
);
2298 irlap_flush_all_queues(self
);
2300 irlap_next_state(self
, LAP_NRM_S
);
2302 case DISCONNECT_REQUEST
:
2303 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2304 irlap_send_rd_frame(self
);
2305 irlap_start_wd_timer(self
, WD_TIMEOUT
);
2306 irlap_next_state(self
, LAP_SCLOSE
);
2309 pr_debug("%s(), Unknown event %d, (%s)\n", __func__
,
2310 event
, irlap_event
[event
]);