1 /*********************************************************************
3 * Filename: irlap_event.c
5 * Description: IrLAP state machine implementation
6 * Status: Experimental.
7 * Author: Dag Brattli <dag@brattli.net>
8 * Created at: Sat Aug 16 00:59:29 1997
9 * Modified at: Sat Dec 25 21:07:57 1999
10 * Modified by: Dag Brattli <dag@brattli.net>
12 * Copyright (c) 1998-2000 Dag Brattli <dag@brattli.net>,
13 * Copyright (c) 1998 Thomas Davis <ratbert@radiks.net>
14 * All Rights Reserved.
15 * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of
20 * the License, or (at your option) any later version.
22 * Neither Dag Brattli nor University of Tromsø admit liability nor
23 * provide warranty for any of this software. This material is
24 * provided "AS-IS" and at no charge.
26 ********************************************************************/
28 #include <linux/string.h>
29 #include <linux/kernel.h>
30 #include <linux/delay.h>
31 #include <linux/skbuff.h>
32 #include <linux/slab.h>
34 #include <net/irda/irda.h>
35 #include <net/irda/irlap_event.h>
37 #include <net/irda/timer.h>
38 #include <net/irda/irlap.h>
39 #include <net/irda/irlap_frame.h>
40 #include <net/irda/qos.h>
41 #include <net/irda/parameters.h>
42 #include <net/irda/irlmp.h> /* irlmp_flow_indication(), ... */
44 #include <net/irda/irda_device.h>
46 #ifdef CONFIG_IRDA_FAST_RR
47 int sysctl_fast_poll_increase
= 50;
50 static int irlap_state_ndm (struct irlap_cb
*self
, IRLAP_EVENT event
,
51 struct sk_buff
*skb
, struct irlap_info
*info
);
52 static int irlap_state_query (struct irlap_cb
*self
, IRLAP_EVENT event
,
53 struct sk_buff
*skb
, struct irlap_info
*info
);
54 static int irlap_state_reply (struct irlap_cb
*self
, IRLAP_EVENT event
,
55 struct sk_buff
*skb
, struct irlap_info
*info
);
56 static int irlap_state_conn (struct irlap_cb
*self
, IRLAP_EVENT event
,
57 struct sk_buff
*skb
, struct irlap_info
*info
);
58 static int irlap_state_setup (struct irlap_cb
*self
, IRLAP_EVENT event
,
59 struct sk_buff
*skb
, struct irlap_info
*info
);
60 static int irlap_state_offline(struct irlap_cb
*self
, IRLAP_EVENT event
,
61 struct sk_buff
*skb
, struct irlap_info
*info
);
62 static int irlap_state_xmit_p (struct irlap_cb
*self
, IRLAP_EVENT event
,
63 struct sk_buff
*skb
, struct irlap_info
*info
);
64 static int irlap_state_pclose (struct irlap_cb
*self
, IRLAP_EVENT event
,
65 struct sk_buff
*skb
, struct irlap_info
*info
);
66 static int irlap_state_nrm_p (struct irlap_cb
*self
, IRLAP_EVENT event
,
67 struct sk_buff
*skb
, struct irlap_info
*info
);
68 static int irlap_state_reset_wait(struct irlap_cb
*self
, IRLAP_EVENT event
,
69 struct sk_buff
*skb
, struct irlap_info
*info
);
70 static int irlap_state_reset (struct irlap_cb
*self
, IRLAP_EVENT event
,
71 struct sk_buff
*skb
, struct irlap_info
*info
);
72 static int irlap_state_nrm_s (struct irlap_cb
*self
, IRLAP_EVENT event
,
73 struct sk_buff
*skb
, struct irlap_info
*info
);
74 static int irlap_state_xmit_s (struct irlap_cb
*self
, IRLAP_EVENT event
,
75 struct sk_buff
*skb
, struct irlap_info
*info
);
76 static int irlap_state_sclose (struct irlap_cb
*self
, IRLAP_EVENT event
,
77 struct sk_buff
*skb
, struct irlap_info
*info
);
78 static int irlap_state_reset_check(struct irlap_cb
*, IRLAP_EVENT event
,
79 struct sk_buff
*, struct irlap_info
*);
81 #ifdef CONFIG_IRDA_DEBUG
82 static const char *const irlap_event
[] = {
92 "RECV_DISCOVERY_XID_CMD",
93 "RECV_DISCOVERY_XID_RSP",
113 "SLOT_TIMER_EXPIRED",
114 "QUERY_TIMER_EXPIRED",
115 "FINAL_TIMER_EXPIRED",
116 "POLL_TIMER_EXPIRED",
117 "DISCOVERY_TIMER_EXPIRED",
119 "BACKOFF_TIMER_EXPIRED",
120 "MEDIA_BUSY_TIMER_EXPIRED",
122 #endif /* CONFIG_IRDA_DEBUG */
124 const char *const irlap_state
[] = {
142 static int (*state
[])(struct irlap_cb
*self
, IRLAP_EVENT event
,
143 struct sk_buff
*skb
, struct irlap_info
*info
) =
154 irlap_state_reset_wait
,
159 irlap_state_reset_check
,
163 * Function irda_poll_timer_expired (data)
165 * Poll timer has expired. Normally we must now send a RR frame to the
168 static void irlap_poll_timer_expired(void *data
)
170 struct irlap_cb
*self
= (struct irlap_cb
*) data
;
172 IRDA_ASSERT(self
!= NULL
, return;);
173 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return;);
175 irlap_do_event(self
, POLL_TIMER_EXPIRED
, NULL
, NULL
);
179 * Calculate and set time before we will have to send back the pf bit
180 * to the peer. Use in primary.
181 * Make sure that state is XMIT_P/XMIT_S when calling this function
182 * (and that nobody messed up with the state). - Jean II
184 static void irlap_start_poll_timer(struct irlap_cb
*self
, int timeout
)
186 IRDA_ASSERT(self
!= NULL
, return;);
187 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return;);
189 #ifdef CONFIG_IRDA_FAST_RR
191 * Send out the RR frames faster if our own transmit queue is empty, or
192 * if the peer is busy. The effect is a much faster conversation
194 if (skb_queue_empty(&self
->txq
) || self
->remote_busy
) {
195 if (self
->fast_RR
== TRUE
) {
197 * Assert that the fast poll timer has not reached the
198 * normal poll timer yet
200 if (self
->fast_RR_timeout
< timeout
) {
202 * FIXME: this should be a more configurable
205 self
->fast_RR_timeout
+=
206 (sysctl_fast_poll_increase
* HZ
/1000);
208 /* Use this fast(er) timeout instead */
209 timeout
= self
->fast_RR_timeout
;
212 self
->fast_RR
= TRUE
;
214 /* Start with just 0 ms */
215 self
->fast_RR_timeout
= 0;
219 self
->fast_RR
= FALSE
;
221 pr_debug("%s(), timeout=%d (%ld)\n", __func__
, timeout
, jiffies
);
222 #endif /* CONFIG_IRDA_FAST_RR */
225 irlap_do_event(self
, POLL_TIMER_EXPIRED
, NULL
, NULL
);
227 irda_start_timer(&self
->poll_timer
, timeout
, self
,
228 irlap_poll_timer_expired
);
232 * Function irlap_do_event (event, skb, info)
234 * Rushes through the state machine without any delay. If state == XMIT
235 * then send queued data frames.
237 void irlap_do_event(struct irlap_cb
*self
, IRLAP_EVENT event
,
238 struct sk_buff
*skb
, struct irlap_info
*info
)
242 if (!self
|| self
->magic
!= LAP_MAGIC
)
245 pr_debug("%s(), event = %s, state = %s\n", __func__
,
246 irlap_event
[event
], irlap_state
[self
->state
]);
248 ret
= (*state
[self
->state
])(self
, event
, skb
, info
);
251 * Check if there are any pending events that needs to be executed
253 switch (self
->state
) {
254 case LAP_XMIT_P
: /* FALLTHROUGH */
257 * We just received the pf bit and are at the beginning
258 * of a new LAP transmit window.
259 * Check if there are any queued data frames, and do not
260 * try to disconnect link if we send any data frames, since
261 * that will change the state away form XMIT
263 pr_debug("%s() : queue len = %d\n", __func__
,
264 skb_queue_len(&self
->txq
));
266 if (!skb_queue_empty(&self
->txq
)) {
267 /* Prevent race conditions with irlap_data_request() */
268 self
->local_busy
= TRUE
;
270 /* Theory of operation.
271 * We send frames up to when we fill the window or
272 * reach line capacity. Those frames will queue up
273 * in the device queue, and the driver will slowly
275 * After each frame that we send, we poll the higher
276 * layer for more data. It's the right time to do
277 * that because the link layer need to perform the mtt
278 * and then send the first frame, so we can afford
279 * to send a bit of time in kernel space.
280 * The explicit flow indication allow to minimise
281 * buffers (== lower latency), to avoid higher layer
282 * polling via timers (== less context switches) and
283 * to implement a crude scheduler - Jean II */
285 /* Try to send away all queued data frames */
286 while ((skb
= skb_dequeue(&self
->txq
)) != NULL
) {
288 ret
= (*state
[self
->state
])(self
, SEND_I_CMD
,
290 /* Drop reference count.
291 * It will be increase as needed in
292 * irlap_send_data_xxx() */
295 /* Poll the higher layers for one more frame */
296 irlmp_flow_indication(self
->notify
.instance
,
300 break; /* Try again later! */
302 /* Finished transmitting */
303 self
->local_busy
= FALSE
;
304 } else if (self
->disconnect_pending
) {
305 self
->disconnect_pending
= FALSE
;
307 ret
= (*state
[self
->state
])(self
, DISCONNECT_REQUEST
,
313 /* case LAP_RESET_WAIT: */
314 /* case LAP_RESET_CHECK: */
321 * Function irlap_state_ndm (event, skb, frame)
323 * NDM (Normal Disconnected Mode) state
326 static int irlap_state_ndm(struct irlap_cb
*self
, IRLAP_EVENT event
,
327 struct sk_buff
*skb
, struct irlap_info
*info
)
329 discovery_t
*discovery_rsp
;
332 IRDA_ASSERT(self
!= NULL
, return -1;);
333 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
336 case CONNECT_REQUEST
:
337 IRDA_ASSERT(self
->netdev
!= NULL
, return -1;);
339 if (self
->media_busy
) {
340 /* Note : this will never happen, because we test
341 * media busy in irlap_connect_request() and
342 * postpone the event... - Jean II */
343 pr_debug("%s(), CONNECT_REQUEST: media busy!\n",
346 /* Always switch state before calling upper layers */
347 irlap_next_state(self
, LAP_NDM
);
349 irlap_disconnect_indication(self
, LAP_MEDIA_BUSY
);
351 irlap_send_snrm_frame(self
, &self
->qos_rx
);
353 /* Start Final-bit timer */
354 irlap_start_final_timer(self
, self
->final_timeout
);
356 self
->retry_count
= 0;
357 irlap_next_state(self
, LAP_SETUP
);
361 /* Check if the frame contains and I field */
363 self
->daddr
= info
->daddr
;
364 self
->caddr
= info
->caddr
;
366 irlap_next_state(self
, LAP_CONN
);
368 irlap_connect_indication(self
, skb
);
370 pr_debug("%s(), SNRM frame does not contain an I field!\n",
374 case DISCOVERY_REQUEST
:
375 IRDA_ASSERT(info
!= NULL
, return -1;);
377 if (self
->media_busy
) {
378 pr_debug("%s(), DISCOVERY_REQUEST: media busy!\n",
380 /* irlap->log.condition = MEDIA_BUSY; */
382 /* This will make IrLMP try again */
383 irlap_discovery_confirm(self
, NULL
);
384 /* Note : the discovery log is not cleaned up here,
385 * it will be done in irlap_discovery_request()
392 irlap_send_discovery_xid_frame(self
, info
->S
, info
->s
, TRUE
,
394 self
->frame_sent
= FALSE
;
397 irlap_start_slot_timer(self
, self
->slot_timeout
);
398 irlap_next_state(self
, LAP_QUERY
);
400 case RECV_DISCOVERY_XID_CMD
:
401 IRDA_ASSERT(info
!= NULL
, return -1;);
403 /* Assert that this is not the final slot */
404 if (info
->s
<= info
->S
) {
405 self
->slot
= irlap_generate_rand_time_slot(info
->S
,
407 if (self
->slot
== info
->s
) {
408 discovery_rsp
= irlmp_get_discovery_response();
409 discovery_rsp
->data
.daddr
= info
->daddr
;
411 irlap_send_discovery_xid_frame(self
, info
->S
,
415 self
->frame_sent
= TRUE
;
417 self
->frame_sent
= FALSE
;
420 * Go to reply state until end of discovery to
421 * inhibit our own transmissions. Set the timer
422 * to not stay forever there... Jean II
424 irlap_start_query_timer(self
, info
->S
, info
->s
);
425 irlap_next_state(self
, LAP_REPLY
);
427 /* This is the final slot. How is it possible ?
428 * This would happen is both discoveries are just slightly
429 * offset (if they are in sync, all packets are lost).
430 * Most often, all the discovery requests will be received
431 * in QUERY state (see my comment there), except for the
432 * last frame that will come here.
433 * The big trouble when it happen is that active discovery
434 * doesn't happen, because nobody answer the discoveries
435 * frame of the other guy, so the log shows up empty.
436 * What should we do ?
437 * Not much. It's too late to answer those discovery frames,
438 * so we just pass the info to IrLMP who will put it in the
439 * log (and post an event).
440 * Another cause would be devices that do discovery much
441 * slower than us, however the latest fixes should minimise
445 pr_debug("%s(), Receiving final discovery request, missed the discovery slots :-(\n",
448 /* Last discovery request -> in the log */
449 irlap_discovery_indication(self
, info
->discovery
);
452 case MEDIA_BUSY_TIMER_EXPIRED
:
453 /* A bunch of events may be postponed because the media is
454 * busy (usually immediately after we close a connection),
455 * or while we are doing discovery (state query/reply).
456 * In all those cases, the media busy flag will be cleared
457 * when it's OK for us to process those postponed events.
458 * This event is not mentioned in the state machines in the
459 * IrLAP spec. It's because they didn't consider Ultra and
460 * postponing connection request is optional.
462 #ifdef CONFIG_IRDA_ULTRA
463 /* Send any pending Ultra frames if any */
464 if (!skb_queue_empty(&self
->txq_ultra
)) {
465 /* We don't send the frame, just post an event.
466 * Also, previously this code was in timer.c...
468 ret
= (*state
[self
->state
])(self
, SEND_UI_FRAME
,
471 #endif /* CONFIG_IRDA_ULTRA */
472 /* Check if we should try to connect.
473 * This code was previously in irlap_do_event() */
474 if (self
->connect_pending
) {
475 self
->connect_pending
= FALSE
;
477 /* This one *should* not pend in this state, except
478 * if a socket try to connect and immediately
479 * disconnect. - clear - Jean II */
480 if (self
->disconnect_pending
)
481 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
483 ret
= (*state
[self
->state
])(self
,
486 self
->disconnect_pending
= FALSE
;
488 /* Note : one way to test if this code works well (including
489 * media busy and small busy) is to create a user space
490 * application generating an Ultra packet every 3.05 sec (or
491 * 2.95 sec) and to see how it interact with discovery.
492 * It's fairly easy to check that no packet is lost, that the
493 * packets are postponed during discovery and that after
494 * discovery indication you have a 100ms "gap".
495 * As connection request and Ultra are now processed the same
496 * way, this avoid the tedious job of trying IrLAP connection
497 * in all those cases...
500 #ifdef CONFIG_IRDA_ULTRA
504 /* Only allowed to repeat an operation twice */
505 for (i
=0; ((i
<2) && (self
->media_busy
== FALSE
)); i
++) {
506 skb
= skb_dequeue(&self
->txq_ultra
);
508 irlap_send_ui_frame(self
, skb
, CBROADCAST
,
512 /* irlap_send_ui_frame() won't increase skb reference
513 * count, so no dev_kfree_skb() - Jean II */
516 /* Force us to listen 500 ms again */
517 irda_device_set_media_busy(self
->netdev
, TRUE
);
522 /* Only accept broadcast frames in NDM mode */
523 if (info
->caddr
!= CBROADCAST
) {
524 pr_debug("%s(), not a broadcast frame!\n",
527 irlap_unitdata_indication(self
, skb
);
529 #endif /* CONFIG_IRDA_ULTRA */
531 /* Remove test frame header */
532 skb_pull(skb
, sizeof(struct test_frame
));
535 * Send response. This skb will not be sent out again, and
536 * will only be used to send out the same info as the cmd
538 irlap_send_test_frame(self
, CBROADCAST
, info
->daddr
, skb
);
541 pr_debug("%s() not implemented!\n", __func__
);
544 pr_debug("%s(), Unknown event %s\n", __func__
,
554 * Function irlap_state_query (event, skb, info)
559 static int irlap_state_query(struct irlap_cb
*self
, IRLAP_EVENT event
,
560 struct sk_buff
*skb
, struct irlap_info
*info
)
564 IRDA_ASSERT(self
!= NULL
, return -1;);
565 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
568 case RECV_DISCOVERY_XID_RSP
:
569 IRDA_ASSERT(info
!= NULL
, return -1;);
570 IRDA_ASSERT(info
->discovery
!= NULL
, return -1;);
572 pr_debug("%s(), daddr=%08x\n", __func__
,
573 info
->discovery
->data
.daddr
);
575 if (!self
->discovery_log
) {
576 net_warn_ratelimited("%s: discovery log is gone! maybe the discovery timeout has been set too short?\n",
580 hashbin_insert(self
->discovery_log
,
581 (irda_queue_t
*) info
->discovery
,
582 info
->discovery
->data
.daddr
, NULL
);
585 /* irlap_next_state(self, LAP_QUERY); */
588 case RECV_DISCOVERY_XID_CMD
:
589 /* Yes, it is possible to receive those frames in this mode.
590 * Note that most often the last discovery request won't
591 * occur here but in NDM state (see my comment there).
592 * What should we do ?
593 * Not much. We are currently performing our own discovery,
594 * therefore we can't answer those frames. We don't want
595 * to change state either. We just pass the info to
596 * IrLMP who will put it in the log (and post an event).
600 IRDA_ASSERT(info
!= NULL
, return -1;);
602 pr_debug("%s(), Receiving discovery request (s = %d) while performing discovery :-(\n",
605 /* Last discovery request ? */
607 irlap_discovery_indication(self
, info
->discovery
);
609 case SLOT_TIMER_EXPIRED
:
611 * Wait a little longer if we detect an incoming frame. This
612 * is not mentioned in the spec, but is a good thing to do,
613 * since we want to work even with devices that violate the
614 * timing requirements.
616 if (irda_device_is_receiving(self
->netdev
) && !self
->add_wait
) {
617 pr_debug("%s(), device is slow to answer, waiting some more!\n",
619 irlap_start_slot_timer(self
, msecs_to_jiffies(10));
620 self
->add_wait
= TRUE
;
623 self
->add_wait
= FALSE
;
625 if (self
->s
< self
->S
) {
626 irlap_send_discovery_xid_frame(self
, self
->S
,
628 self
->discovery_cmd
);
630 irlap_start_slot_timer(self
, self
->slot_timeout
);
633 irlap_next_state(self
, LAP_QUERY
);
635 /* This is the final slot! */
636 irlap_send_discovery_xid_frame(self
, self
->S
, 0xff,
638 self
->discovery_cmd
);
640 /* Always switch state before calling upper layers */
641 irlap_next_state(self
, LAP_NDM
);
644 * We are now finished with the discovery procedure,
645 * so now we must return the results
647 irlap_discovery_confirm(self
, self
->discovery_log
);
649 /* IrLMP should now have taken care of the log */
650 self
->discovery_log
= NULL
;
654 pr_debug("%s(), Unknown event %s\n", __func__
,
664 * Function irlap_state_reply (self, event, skb, info)
666 * REPLY, we have received a XID discovery frame from a device and we
667 * are waiting for the right time slot to send a response XID frame
670 static int irlap_state_reply(struct irlap_cb
*self
, IRLAP_EVENT event
,
671 struct sk_buff
*skb
, struct irlap_info
*info
)
673 discovery_t
*discovery_rsp
;
676 IRDA_ASSERT(self
!= NULL
, return -1;);
677 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
680 case QUERY_TIMER_EXPIRED
:
681 pr_debug("%s(), QUERY_TIMER_EXPIRED <%ld>\n",
683 irlap_next_state(self
, LAP_NDM
);
685 case RECV_DISCOVERY_XID_CMD
:
686 IRDA_ASSERT(info
!= NULL
, return -1;);
688 if (info
->s
== 0xff) {
689 del_timer(&self
->query_timer
);
691 /* info->log.condition = REMOTE; */
693 /* Always switch state before calling upper layers */
694 irlap_next_state(self
, LAP_NDM
);
696 irlap_discovery_indication(self
, info
->discovery
);
698 /* If it's our slot, send our reply */
699 if ((info
->s
>= self
->slot
) && (!self
->frame_sent
)) {
700 discovery_rsp
= irlmp_get_discovery_response();
701 discovery_rsp
->data
.daddr
= info
->daddr
;
703 irlap_send_discovery_xid_frame(self
, info
->S
,
708 self
->frame_sent
= TRUE
;
710 /* Readjust our timer to accommodate devices
711 * doing faster or slower discovery than us...
713 irlap_start_query_timer(self
, info
->S
, info
->s
);
716 //irlap_next_state(self, LAP_REPLY);
720 pr_debug("%s(), Unknown event %d, %s\n", __func__
,
721 event
, irlap_event
[event
]);
730 * Function irlap_state_conn (event, skb, info)
732 * CONN, we have received a SNRM command and is waiting for the upper
733 * layer to accept or refuse connection
736 static int irlap_state_conn(struct irlap_cb
*self
, IRLAP_EVENT event
,
737 struct sk_buff
*skb
, struct irlap_info
*info
)
741 pr_debug("%s(), event=%s\n", __func__
, irlap_event
[event
]);
743 IRDA_ASSERT(self
!= NULL
, return -1;);
744 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
747 case CONNECT_RESPONSE
:
748 skb_pull(skb
, sizeof(struct snrm_frame
));
750 IRDA_ASSERT(self
->netdev
!= NULL
, return -1;);
752 irlap_qos_negotiate(self
, skb
);
754 irlap_initiate_connection_state(self
);
757 * Applying the parameters now will make sure we change speed
758 * *after* we have sent the next frame
760 irlap_apply_connection_parameters(self
, FALSE
);
763 * Sending this frame will force a speed change after it has
764 * been sent (i.e. the frame will be sent at 9600).
766 irlap_send_ua_response_frame(self
, &self
->qos_rx
);
770 * We are allowed to send two frames, but this may increase
771 * the connect latency, so lets not do it for now.
773 /* This is full of good intentions, but doesn't work in
775 * After sending the first UA response, we switch the
776 * dongle to the negotiated speed, which is usually
777 * different than 9600 kb/s.
778 * From there, there is two solutions :
779 * 1) The other end has received the first UA response :
780 * it will set up the connection, move to state LAP_NRM_P,
781 * and will ignore and drop the second UA response.
782 * Actually, it's even worse : the other side will almost
783 * immediately send a RR that will likely collide with the
784 * UA response (depending on negotiated turnaround).
785 * 2) The other end has not received the first UA response,
786 * will stay at 9600 and will never see the second UA response.
788 irlap_send_ua_response_frame(self
, &self
->qos_rx
);
792 * The WD-timer could be set to the duration of the P-timer
793 * for this case, but it is recommended to use twice the
794 * value (note 3 IrLAP p. 60).
796 irlap_start_wd_timer(self
, self
->wd_timeout
);
797 irlap_next_state(self
, LAP_NRM_S
);
800 case RECV_DISCOVERY_XID_CMD
:
801 pr_debug("%s(), event RECV_DISCOVER_XID_CMD!\n",
803 irlap_next_state(self
, LAP_NDM
);
806 case DISCONNECT_REQUEST
:
807 pr_debug("%s(), Disconnect request!\n", __func__
);
808 irlap_send_dm_frame(self
);
809 irlap_next_state( self
, LAP_NDM
);
810 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
813 pr_debug("%s(), Unknown event %d, %s\n", __func__
,
814 event
, irlap_event
[event
]);
824 * Function irlap_state_setup (event, skb, frame)
826 * SETUP state, The local layer has transmitted a SNRM command frame to
827 * a remote peer layer and is awaiting a reply .
830 static int irlap_state_setup(struct irlap_cb
*self
, IRLAP_EVENT event
,
831 struct sk_buff
*skb
, struct irlap_info
*info
)
835 IRDA_ASSERT(self
!= NULL
, return -1;);
836 IRDA_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 pr_debug("%s(), SNRM battle!\n", __func__
);
864 IRDA_ASSERT(skb
!= NULL
, return 0;);
865 IRDA_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 IRDA_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 IRDA_ASSERT(skb
->len
> 10, return -1;);
909 skb_pull(skb
, sizeof(struct ua_frame
));
911 IRDA_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 /* The timer is set to half the normal timer to quickly
931 * detect a failure to negotiate the new connection
932 * parameters. IrLAP 6.11.3.2, note 3.
933 * Note that currently we don't process this failure
934 * properly, as we should do a quick disconnect.
936 irlap_start_final_timer(self
, self
->final_timeout
/2);
937 irlap_next_state(self
, LAP_NRM_P
);
939 irlap_connect_confirm(self
, skb
);
941 case RECV_DM_RSP
: /* FALLTHROUGH */
943 del_timer(&self
->final_timer
);
944 irlap_next_state(self
, LAP_NDM
);
946 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
949 pr_debug("%s(), Unknown event %d, %s\n", __func__
,
950 event
, irlap_event
[event
]);
959 * Function irlap_state_offline (self, event, skb, info)
961 * OFFLINE state, not used for now!
964 static int irlap_state_offline(struct irlap_cb
*self
, IRLAP_EVENT event
,
965 struct sk_buff
*skb
, struct irlap_info
*info
)
967 pr_debug("%s(), Unknown event\n", __func__
);
973 * Function irlap_state_xmit_p (self, event, skb, info)
975 * XMIT, Only the primary station has right to transmit, and we
976 * therefore do not expect to receive any transmissions from other
980 static int irlap_state_xmit_p(struct irlap_cb
*self
, IRLAP_EVENT event
,
981 struct sk_buff
*skb
, struct irlap_info
*info
)
988 * Only send frame if send-window > 0.
990 if ((self
->window
> 0) && (!self
->remote_busy
)) {
992 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
993 struct sk_buff
*skb_next
;
995 /* With DYNAMIC_WINDOW, we keep the window size
996 * maximum, and adapt on the packets we are sending.
997 * At 115k, we can send only 2 packets of 2048 bytes
998 * in a 500 ms turnaround. Without this option, we
999 * would always limit the window to 2. With this
1000 * option, if we send smaller packets, we can send
1001 * up to 7 of them (always depending on QoS).
1004 /* Look at the next skb. This is safe, as we are
1005 * the only consumer of the Tx queue (if we are not,
1006 * we have other problems) - Jean II */
1007 skb_next
= skb_peek(&self
->txq
);
1009 /* Check if a subsequent skb exist and would fit in
1010 * the current window (with respect to turnaround
1012 * This allow us to properly mark the current packet
1013 * with the pf bit, to avoid falling back on the
1014 * second test below, and avoid waiting the
1015 * end of the window and sending a extra RR.
1016 * Note : (skb_next != NULL) <=> (skb_queue_len() > 0)
1018 nextfit
= ((skb_next
!= NULL
) &&
1019 ((skb_next
->len
+ skb
->len
) <=
1023 * The current packet may not fit ! Because of test
1024 * above, this should not happen any more !!!
1025 * Test if we have transmitted more bytes over the
1026 * link than its possible to do with the current
1027 * speed and turn-around-time.
1029 if((!nextfit
) && (skb
->len
> self
->bytes_left
)) {
1030 pr_debug("%s(), Not allowed to transmit more bytes!\n",
1032 /* Requeue the skb */
1033 skb_queue_head(&self
->txq
, skb_get(skb
));
1035 * We should switch state to LAP_NRM_P, but
1036 * that is not possible since we must be sure
1037 * that we poll the other side. Since we have
1038 * used up our time, the poll timer should
1039 * trigger anyway now, so we just wait for it
1043 * Sorry, but that's not totally true. If
1044 * we send 2000B packets, we may wait another
1045 * 1000B until our turnaround expire. That's
1046 * why we need to be proactive in avoiding
1047 * coming here. - Jean II
1052 /* Subtract space used by this skb */
1053 self
->bytes_left
-= skb
->len
;
1054 #else /* CONFIG_IRDA_DYNAMIC_WINDOW */
1055 /* Window has been adjusted for the max packet
1056 * size, so much simpler... - Jean II */
1057 nextfit
= !skb_queue_empty(&self
->txq
);
1058 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1060 * Send data with poll bit cleared only if window > 1
1061 * and there is more frames after this one to be sent
1063 if ((self
->window
> 1) && (nextfit
)) {
1064 /* More packet to send in current window */
1065 irlap_send_data_primary(self
, skb
);
1066 irlap_next_state(self
, LAP_XMIT_P
);
1068 /* Final packet of window */
1069 irlap_send_data_primary_poll(self
, skb
);
1072 * Make sure state machine does not try to send
1077 #ifdef CONFIG_IRDA_FAST_RR
1078 /* Peer may want to reply immediately */
1079 self
->fast_RR
= FALSE
;
1080 #endif /* CONFIG_IRDA_FAST_RR */
1082 pr_debug("%s(), Unable to send! remote busy?\n",
1084 skb_queue_head(&self
->txq
, skb_get(skb
));
1087 * The next ret is important, because it tells
1088 * irlap_next_state _not_ to deliver more frames
1093 case POLL_TIMER_EXPIRED
:
1094 pr_debug("%s(), POLL_TIMER_EXPIRED <%ld>\n",
1096 irlap_send_rr_frame(self
, CMD_FRAME
);
1097 /* Return to NRM properly - Jean II */
1098 self
->window
= self
->window_size
;
1099 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1100 /* Allowed to transmit a maximum number of bytes again. */
1101 self
->bytes_left
= self
->line_capacity
;
1102 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1103 irlap_start_final_timer(self
, self
->final_timeout
);
1104 irlap_next_state(self
, LAP_NRM_P
);
1106 case DISCONNECT_REQUEST
:
1107 del_timer(&self
->poll_timer
);
1108 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1109 irlap_send_disc_frame(self
);
1110 irlap_flush_all_queues(self
);
1111 irlap_start_final_timer(self
, self
->final_timeout
);
1112 self
->retry_count
= 0;
1113 irlap_next_state(self
, LAP_PCLOSE
);
1116 /* Nothing to do, irlap_do_event() will send the packet
1117 * when we return... - Jean II */
1120 pr_debug("%s(), Unknown event %s\n",
1121 __func__
, irlap_event
[event
]);
1130 * Function irlap_state_pclose (event, skb, info)
1134 static int irlap_state_pclose(struct irlap_cb
*self
, IRLAP_EVENT event
,
1135 struct sk_buff
*skb
, struct irlap_info
*info
)
1139 IRDA_ASSERT(self
!= NULL
, return -1;);
1140 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
1143 case RECV_UA_RSP
: /* FALLTHROUGH */
1145 del_timer(&self
->final_timer
);
1147 /* Set new link parameters */
1148 irlap_apply_default_connection_parameters(self
);
1150 /* Always switch state before calling upper layers */
1151 irlap_next_state(self
, LAP_NDM
);
1153 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
1155 case FINAL_TIMER_EXPIRED
:
1156 if (self
->retry_count
< self
->N3
) {
1157 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1158 irlap_send_disc_frame(self
);
1159 irlap_start_final_timer(self
, self
->final_timeout
);
1160 self
->retry_count
++;
1163 irlap_apply_default_connection_parameters(self
);
1165 /* Always switch state before calling upper layers */
1166 irlap_next_state(self
, LAP_NDM
);
1168 irlap_disconnect_indication(self
, LAP_NO_RESPONSE
);
1172 pr_debug("%s(), Unknown event %d\n", __func__
, event
);
1181 * Function irlap_state_nrm_p (self, event, skb, info)
1183 * NRM_P (Normal Response Mode as Primary), The primary station has given
1184 * permissions to a secondary station to transmit IrLAP resonse frames
1185 * (by sending a frame with the P bit set). The primary station will not
1186 * transmit any frames and is expecting to receive frames only from the
1187 * secondary to which transmission permissions has been given.
1189 static int irlap_state_nrm_p(struct irlap_cb
*self
, IRLAP_EVENT event
,
1190 struct sk_buff
*skb
, struct irlap_info
*info
)
1197 case RECV_I_RSP
: /* Optimize for the common case */
1198 if (unlikely(skb
->len
<= LAP_ADDR_HEADER
+ LAP_CTRL_HEADER
)) {
1200 * Input validation check: a stir4200/mcp2150
1201 * combination sometimes results in an empty i:rsp.
1202 * This makes no sense; we can just ignore the frame
1203 * and send an rr:cmd immediately. This happens before
1204 * changing nr or ns so triggers a retransmit
1206 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1207 irlap_send_rr_frame(self
, CMD_FRAME
);
1211 /* FIXME: must check for remote_busy below */
1212 #ifdef CONFIG_IRDA_FAST_RR
1214 * Reset the fast_RR so we can use the fast RR code with
1215 * full speed the next time since peer may have more frames
1218 self
->fast_RR
= FALSE
;
1219 #endif /* CONFIG_IRDA_FAST_RR */
1220 IRDA_ASSERT( info
!= NULL
, return -1;);
1222 ns_status
= irlap_validate_ns_received(self
, info
->ns
);
1223 nr_status
= irlap_validate_nr_received(self
, info
->nr
);
1226 * Check for expected I(nformation) frame
1228 if ((ns_status
== NS_EXPECTED
) && (nr_status
== NR_EXPECTED
)) {
1230 /* Update Vr (next frame for us to receive) */
1231 self
->vr
= (self
->vr
+ 1) % 8;
1233 /* Update Nr received, cleanup our retry queue */
1234 irlap_update_nr_received(self
, info
->nr
);
1237 * Got expected NR, so reset the
1238 * retry_count. This is not done by IrLAP spec,
1241 self
->retry_count
= 0;
1242 self
->ack_required
= TRUE
;
1244 /* poll bit cleared? */
1246 /* Keep state, do not move this line */
1247 irlap_next_state(self
, LAP_NRM_P
);
1249 irlap_data_indication(self
, skb
, FALSE
);
1251 /* No longer waiting for pf */
1252 del_timer(&self
->final_timer
);
1254 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1256 /* Call higher layer *before* changing state
1257 * to give them a chance to send data in the
1260 irlap_data_indication(self
, skb
, FALSE
);
1262 /* XMIT states are the most dangerous state
1263 * to be in, because user requests are
1264 * processed directly and may change state.
1265 * On the other hand, in NDM_P, those
1266 * requests are queued and we will process
1267 * them when we return to irlap_do_event().
1270 irlap_next_state(self
, LAP_XMIT_P
);
1272 /* This is the last frame.
1273 * Make sure it's always called in XMIT state.
1275 irlap_start_poll_timer(self
, self
->poll_timeout
);
1280 /* Unexpected next to send (Ns) */
1281 if ((ns_status
== NS_UNEXPECTED
) && (nr_status
== NR_EXPECTED
))
1284 irlap_update_nr_received(self
, info
->nr
);
1287 * Wait until the last frame before doing
1292 irlap_next_state(self
, LAP_NRM_P
);
1294 pr_debug("%s(), missing or duplicate frame!\n",
1297 /* Update Nr received */
1298 irlap_update_nr_received(self
, info
->nr
);
1300 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1301 irlap_send_rr_frame(self
, CMD_FRAME
);
1303 self
->ack_required
= FALSE
;
1305 irlap_start_final_timer(self
, self
->final_timeout
);
1306 irlap_next_state(self
, LAP_NRM_P
);
1311 * Unexpected next to receive (Nr)
1313 if ((ns_status
== NS_EXPECTED
) && (nr_status
== NR_UNEXPECTED
))
1316 self
->vr
= (self
->vr
+ 1) % 8;
1318 /* Update Nr received */
1319 irlap_update_nr_received(self
, info
->nr
);
1321 /* Resend rejected frames */
1322 irlap_resend_rejected_frames(self
, CMD_FRAME
);
1324 self
->ack_required
= FALSE
;
1326 /* Make sure we account for the time
1327 * to transmit our frames. See comemnts
1328 * in irlap_send_data_primary_poll().
1330 irlap_start_final_timer(self
, 2 * self
->final_timeout
);
1332 /* Keep state, do not move this line */
1333 irlap_next_state(self
, LAP_NRM_P
);
1335 irlap_data_indication(self
, skb
, FALSE
);
1338 * Do not resend frames until the last
1339 * frame has arrived from the other
1340 * device. This is not documented in
1343 self
->vr
= (self
->vr
+ 1) % 8;
1345 /* Update Nr received */
1346 irlap_update_nr_received(self
, info
->nr
);
1348 self
->ack_required
= FALSE
;
1350 /* Keep state, do not move this line!*/
1351 irlap_next_state(self
, LAP_NRM_P
);
1353 irlap_data_indication(self
, skb
, FALSE
);
1358 * Unexpected next to send (Ns) and next to receive (Nr)
1359 * Not documented by IrLAP!
1361 if ((ns_status
== NS_UNEXPECTED
) &&
1362 (nr_status
== NR_UNEXPECTED
))
1364 pr_debug("%s(), unexpected nr and ns!\n",
1367 /* Resend rejected frames */
1368 irlap_resend_rejected_frames(self
, CMD_FRAME
);
1370 /* Give peer some time to retransmit!
1371 * But account for our own Tx. */
1372 irlap_start_final_timer(self
, 2 * self
->final_timeout
);
1374 /* Keep state, do not move this line */
1375 irlap_next_state(self
, LAP_NRM_P
);
1377 /* Update Nr received */
1378 /* irlap_update_nr_received( info->nr); */
1380 self
->ack_required
= FALSE
;
1388 if ((nr_status
== NR_INVALID
) || (ns_status
== NS_INVALID
)) {
1390 del_timer(&self
->final_timer
);
1392 irlap_next_state(self
, LAP_RESET_WAIT
);
1394 irlap_disconnect_indication(self
, LAP_RESET_INDICATION
);
1395 self
->xmitflag
= TRUE
;
1397 del_timer(&self
->final_timer
);
1399 irlap_disconnect_indication(self
, LAP_RESET_INDICATION
);
1401 self
->xmitflag
= FALSE
;
1405 pr_debug("%s(), Not implemented!\n", __func__
);
1406 pr_debug("%s(), event=%s, ns_status=%d, nr_status=%d\n",
1407 __func__
, irlap_event
[event
], ns_status
, nr_status
);
1410 /* Poll bit cleared? */
1412 irlap_data_indication(self
, skb
, TRUE
);
1413 irlap_next_state(self
, LAP_NRM_P
);
1415 del_timer(&self
->final_timer
);
1416 irlap_data_indication(self
, skb
, TRUE
);
1417 irlap_next_state(self
, LAP_XMIT_P
);
1418 pr_debug("%s: RECV_UI_FRAME: next state %s\n",
1419 __func__
, irlap_state
[self
->state
]);
1420 irlap_start_poll_timer(self
, self
->poll_timeout
);
1425 * If you get a RR, the remote isn't busy anymore,
1426 * no matter what the NR
1428 self
->remote_busy
= FALSE
;
1430 /* Stop final timer */
1431 del_timer(&self
->final_timer
);
1436 ret
= irlap_validate_nr_received(self
, info
->nr
);
1437 if (ret
== NR_EXPECTED
) {
1438 /* Update Nr received */
1439 irlap_update_nr_received(self
, info
->nr
);
1442 * Got expected NR, so reset the retry_count. This
1443 * is not done by the IrLAP standard , which is
1446 self
->retry_count
= 0;
1447 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1449 irlap_next_state(self
, LAP_XMIT_P
);
1451 /* Start poll timer */
1452 irlap_start_poll_timer(self
, self
->poll_timeout
);
1453 } else if (ret
== NR_UNEXPECTED
) {
1454 IRDA_ASSERT(info
!= NULL
, return -1;);
1459 /* Update Nr received */
1460 irlap_update_nr_received(self
, info
->nr
);
1462 pr_debug("RECV_RR_FRAME: Retrans:%d, nr=%d, va=%d, vs=%d, vr=%d\n",
1463 self
->retry_count
, info
->nr
, self
->va
,
1464 self
->vs
, self
->vr
);
1466 /* Resend rejected frames */
1467 irlap_resend_rejected_frames(self
, CMD_FRAME
);
1468 irlap_start_final_timer(self
, self
->final_timeout
* 2);
1470 irlap_next_state(self
, LAP_NRM_P
);
1471 } else if (ret
== NR_INVALID
) {
1472 pr_debug("%s(), Received RR with invalid nr !\n",
1475 irlap_next_state(self
, LAP_RESET_WAIT
);
1477 irlap_disconnect_indication(self
, LAP_RESET_INDICATION
);
1478 self
->xmitflag
= TRUE
;
1482 IRDA_ASSERT(info
!= NULL
, return -1;);
1484 /* Stop final timer */
1485 del_timer(&self
->final_timer
);
1486 self
->remote_busy
= TRUE
;
1488 /* Update Nr received */
1489 irlap_update_nr_received(self
, info
->nr
);
1490 irlap_next_state(self
, LAP_XMIT_P
);
1492 /* Start poll timer */
1493 irlap_start_poll_timer(self
, self
->poll_timeout
);
1496 del_timer(&self
->final_timer
);
1497 self
->xmitflag
= TRUE
;
1498 irlap_next_state(self
, LAP_RESET_WAIT
);
1499 irlap_reset_indication(self
);
1501 case FINAL_TIMER_EXPIRED
:
1503 * We are allowed to wait for additional 300 ms if
1504 * final timer expires when we are in the middle
1505 * of receiving a frame (page 45, IrLAP). Check that
1506 * we only do this once for each frame.
1508 if (irda_device_is_receiving(self
->netdev
) && !self
->add_wait
) {
1509 pr_debug("FINAL_TIMER_EXPIRED when receiving a frame! Waiting a little bit more!\n");
1510 irlap_start_final_timer(self
, msecs_to_jiffies(300));
1513 * Don't allow this to happen one more time in a row,
1514 * or else we can get a pretty tight loop here if
1515 * if we only receive half a frame. DB.
1517 self
->add_wait
= TRUE
;
1520 self
->add_wait
= FALSE
;
1522 /* N2 is the disconnect timer. Until we reach it, we retry */
1523 if (self
->retry_count
< self
->N2
) {
1524 if (skb_peek(&self
->wx_list
) == NULL
) {
1525 /* Retry sending the pf bit to the secondary */
1526 pr_debug("nrm_p: resending rr");
1527 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1528 irlap_send_rr_frame(self
, CMD_FRAME
);
1530 pr_debug("nrm_p: resend frames");
1531 irlap_resend_rejected_frames(self
, CMD_FRAME
);
1534 irlap_start_final_timer(self
, self
->final_timeout
);
1535 self
->retry_count
++;
1536 pr_debug("irlap_state_nrm_p: FINAL_TIMER_EXPIRED: retry_count=%d\n",
1539 /* Early warning event. I'm using a pretty liberal
1540 * interpretation of the spec and generate an event
1541 * every time the timer is multiple of N1 (and not
1542 * only the first time). This allow application
1543 * to know precisely if connectivity restart...
1545 if((self
->retry_count
% self
->N1
) == 0)
1546 irlap_status_indication(self
,
1547 STATUS_NO_ACTIVITY
);
1551 irlap_apply_default_connection_parameters(self
);
1553 /* Always switch state before calling upper layers */
1554 irlap_next_state(self
, LAP_NDM
);
1555 irlap_disconnect_indication(self
, LAP_NO_RESPONSE
);
1559 irlap_update_nr_received(self
, info
->nr
);
1560 if (self
->remote_busy
) {
1561 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1562 irlap_send_rr_frame(self
, CMD_FRAME
);
1564 irlap_resend_rejected_frames(self
, CMD_FRAME
);
1565 irlap_start_final_timer(self
, 2 * self
->final_timeout
);
1568 irlap_update_nr_received(self
, info
->nr
);
1569 if (self
->remote_busy
) {
1570 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1571 irlap_send_rr_frame(self
, CMD_FRAME
);
1573 irlap_resend_rejected_frame(self
, CMD_FRAME
);
1574 irlap_start_final_timer(self
, 2 * self
->final_timeout
);
1577 pr_debug("%s(), RECV_RD_RSP\n", __func__
);
1579 irlap_flush_all_queues(self
);
1580 irlap_next_state(self
, LAP_XMIT_P
);
1581 /* Call back the LAP state machine to do a proper disconnect */
1582 irlap_disconnect_request(self
);
1585 pr_debug("%s(), Unknown event %s\n",
1586 __func__
, irlap_event
[event
]);
1595 * Function irlap_state_reset_wait (event, skb, info)
1597 * We have informed the service user of a reset condition, and is
1598 * awaiting reset of disconnect request.
1601 static int irlap_state_reset_wait(struct irlap_cb
*self
, IRLAP_EVENT event
,
1602 struct sk_buff
*skb
, struct irlap_info
*info
)
1606 pr_debug("%s(), event = %s\n", __func__
, irlap_event
[event
]);
1608 IRDA_ASSERT(self
!= NULL
, return -1;);
1609 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
1613 if (self
->xmitflag
) {
1614 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1615 irlap_send_snrm_frame(self
, NULL
);
1616 irlap_start_final_timer(self
, self
->final_timeout
);
1617 irlap_next_state(self
, LAP_RESET
);
1619 irlap_start_final_timer(self
, self
->final_timeout
);
1620 irlap_next_state(self
, LAP_RESET
);
1623 case DISCONNECT_REQUEST
:
1624 irlap_wait_min_turn_around( self
, &self
->qos_tx
);
1625 irlap_send_disc_frame( self
);
1626 irlap_flush_all_queues( self
);
1627 irlap_start_final_timer( self
, self
->final_timeout
);
1628 self
->retry_count
= 0;
1629 irlap_next_state( self
, LAP_PCLOSE
);
1632 pr_debug("%s(), Unknown event %s\n", __func__
,
1633 irlap_event
[event
]);
1642 * Function irlap_state_reset (self, event, skb, info)
1644 * We have sent a SNRM reset command to the peer layer, and is awaiting
1648 static int irlap_state_reset(struct irlap_cb
*self
, IRLAP_EVENT event
,
1649 struct sk_buff
*skb
, struct irlap_info
*info
)
1653 pr_debug("%s(), event = %s\n", __func__
, irlap_event
[event
]);
1655 IRDA_ASSERT(self
!= NULL
, return -1;);
1656 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
1660 del_timer(&self
->final_timer
);
1662 irlap_apply_default_connection_parameters(self
);
1664 /* Always switch state before calling upper layers */
1665 irlap_next_state(self
, LAP_NDM
);
1667 irlap_disconnect_indication(self
, LAP_NO_RESPONSE
);
1671 del_timer(&self
->final_timer
);
1673 /* Initiate connection state */
1674 irlap_initiate_connection_state(self
);
1676 irlap_reset_confirm();
1678 self
->remote_busy
= FALSE
;
1680 irlap_next_state(self
, LAP_XMIT_P
);
1682 irlap_start_poll_timer(self
, self
->poll_timeout
);
1685 case FINAL_TIMER_EXPIRED
:
1686 if (self
->retry_count
< 3) {
1687 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1689 IRDA_ASSERT(self
->netdev
!= NULL
, return -1;);
1690 irlap_send_snrm_frame(self
, self
->qos_dev
);
1692 self
->retry_count
++; /* Experimental!! */
1694 irlap_start_final_timer(self
, self
->final_timeout
);
1695 irlap_next_state(self
, LAP_RESET
);
1696 } else if (self
->retry_count
>= self
->N3
) {
1697 irlap_apply_default_connection_parameters(self
);
1699 /* Always switch state before calling upper layers */
1700 irlap_next_state(self
, LAP_NDM
);
1702 irlap_disconnect_indication(self
, LAP_NO_RESPONSE
);
1707 * SNRM frame is not allowed to contain an I-field in this
1711 pr_debug("%s(), RECV_SNRM_CMD\n", __func__
);
1712 irlap_initiate_connection_state(self
);
1713 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1714 irlap_send_ua_response_frame(self
, &self
->qos_rx
);
1715 irlap_reset_confirm();
1716 irlap_start_wd_timer(self
, self
->wd_timeout
);
1717 irlap_next_state(self
, LAP_NDM
);
1719 pr_debug("%s(), SNRM frame contained an I field!\n",
1724 pr_debug("%s(), Unknown event %s\n",
1725 __func__
, irlap_event
[event
]);
1734 * Function irlap_state_xmit_s (event, skb, info)
1736 * XMIT_S, The secondary station has been given the right to transmit,
1737 * and we therefore do not expect to receive any transmissions from other
1740 static int irlap_state_xmit_s(struct irlap_cb
*self
, IRLAP_EVENT event
,
1741 struct sk_buff
*skb
, struct irlap_info
*info
)
1745 pr_debug("%s(), event=%s\n", __func__
, irlap_event
[event
]);
1747 IRDA_ASSERT(self
!= NULL
, return -ENODEV
;);
1748 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -EBADR
;);
1753 * Send frame only if send window > 0
1755 if ((self
->window
> 0) && (!self
->remote_busy
)) {
1757 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1758 struct sk_buff
*skb_next
;
1761 * Same deal as in irlap_state_xmit_p(), so see
1762 * the comments at that point.
1763 * We are the secondary, so there are only subtle
1764 * differences. - Jean II
1767 /* Check if a subsequent skb exist and would fit in
1768 * the current window (with respect to turnaround
1769 * time). - Jean II */
1770 skb_next
= skb_peek(&self
->txq
);
1771 nextfit
= ((skb_next
!= NULL
) &&
1772 ((skb_next
->len
+ skb
->len
) <=
1776 * Test if we have transmitted more bytes over the
1777 * link than its possible to do with the current
1778 * speed and turn-around-time.
1780 if((!nextfit
) && (skb
->len
> self
->bytes_left
)) {
1781 pr_debug("%s(), Not allowed to transmit more bytes!\n",
1783 /* Requeue the skb */
1784 skb_queue_head(&self
->txq
, skb_get(skb
));
1787 * Switch to NRM_S, this is only possible
1788 * when we are in secondary mode, since we
1789 * must be sure that we don't miss any RR
1792 self
->window
= self
->window_size
;
1793 self
->bytes_left
= self
->line_capacity
;
1794 irlap_start_wd_timer(self
, self
->wd_timeout
);
1796 irlap_next_state(self
, LAP_NRM_S
);
1797 /* Slight difference with primary :
1798 * here we would wait for the other side to
1799 * expire the turnaround. - Jean II */
1801 return -EPROTO
; /* Try again later */
1803 /* Subtract space used by this skb */
1804 self
->bytes_left
-= skb
->len
;
1805 #else /* CONFIG_IRDA_DYNAMIC_WINDOW */
1806 /* Window has been adjusted for the max packet
1807 * size, so much simpler... - Jean II */
1808 nextfit
= !skb_queue_empty(&self
->txq
);
1809 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1811 * Send data with final bit cleared only if window > 1
1812 * and there is more frames to be sent
1814 if ((self
->window
> 1) && (nextfit
)) {
1815 irlap_send_data_secondary(self
, skb
);
1816 irlap_next_state(self
, LAP_XMIT_S
);
1818 irlap_send_data_secondary_final(self
, skb
);
1819 irlap_next_state(self
, LAP_NRM_S
);
1822 * Make sure state machine does not try to send
1828 pr_debug("%s(), Unable to send!\n", __func__
);
1829 skb_queue_head(&self
->txq
, skb_get(skb
));
1833 case DISCONNECT_REQUEST
:
1834 irlap_send_rd_frame(self
);
1835 irlap_flush_all_queues(self
);
1836 irlap_start_wd_timer(self
, self
->wd_timeout
);
1837 irlap_next_state(self
, LAP_SCLOSE
);
1840 /* Nothing to do, irlap_do_event() will send the packet
1841 * when we return... - Jean II */
1844 pr_debug("%s(), Unknown event %s\n", __func__
,
1845 irlap_event
[event
]);
1854 * Function irlap_state_nrm_s (event, skb, info)
1856 * NRM_S (Normal Response Mode as Secondary) state, in this state we are
1857 * expecting to receive frames from the primary station
1860 static int irlap_state_nrm_s(struct irlap_cb
*self
, IRLAP_EVENT event
,
1861 struct sk_buff
*skb
, struct irlap_info
*info
)
1867 pr_debug("%s(), event=%s\n", __func__
, irlap_event
[event
]);
1869 IRDA_ASSERT(self
!= NULL
, return -1;);
1870 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -1;);
1873 case RECV_I_CMD
: /* Optimize for the common case */
1874 /* FIXME: must check for remote_busy below */
1875 pr_debug("%s(), event=%s nr=%d, vs=%d, ns=%d, vr=%d, pf=%d\n",
1876 __func__
, irlap_event
[event
], info
->nr
,
1877 self
->vs
, info
->ns
, self
->vr
, info
->pf
);
1879 self
->retry_count
= 0;
1881 ns_status
= irlap_validate_ns_received(self
, info
->ns
);
1882 nr_status
= irlap_validate_nr_received(self
, info
->nr
);
1884 * Check for expected I(nformation) frame
1886 if ((ns_status
== NS_EXPECTED
) && (nr_status
== NR_EXPECTED
)) {
1888 /* Update Vr (next frame for us to receive) */
1889 self
->vr
= (self
->vr
+ 1) % 8;
1891 /* Update Nr received */
1892 irlap_update_nr_received(self
, info
->nr
);
1899 self
->ack_required
= TRUE
;
1902 * Starting WD-timer here is optional, but
1903 * not recommended. Note 6 IrLAP p. 83
1906 irda_start_timer(WD_TIMER
, self
->wd_timeout
);
1908 /* Keep state, do not move this line */
1909 irlap_next_state(self
, LAP_NRM_S
);
1911 irlap_data_indication(self
, skb
, FALSE
);
1915 * We should wait before sending RR, and
1916 * also before changing to XMIT_S
1917 * state. (note 1, IrLAP p. 82)
1919 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1922 * Give higher layers a chance to
1923 * immediately reply with some data before
1924 * we decide if we should send a RR frame
1927 irlap_data_indication(self
, skb
, FALSE
);
1929 /* Any pending data requests? */
1930 if (!skb_queue_empty(&self
->txq
) &&
1933 self
->ack_required
= TRUE
;
1935 del_timer(&self
->wd_timer
);
1937 irlap_next_state(self
, LAP_XMIT_S
);
1939 irlap_send_rr_frame(self
, RSP_FRAME
);
1940 irlap_start_wd_timer(self
,
1943 /* Keep the state */
1944 irlap_next_state(self
, LAP_NRM_S
);
1950 * Check for Unexpected next to send (Ns)
1952 if ((ns_status
== NS_UNEXPECTED
) && (nr_status
== NR_EXPECTED
))
1954 /* Unexpected next to send, with final bit cleared */
1956 irlap_update_nr_received(self
, info
->nr
);
1958 irlap_start_wd_timer(self
, self
->wd_timeout
);
1960 /* Update Nr received */
1961 irlap_update_nr_received(self
, info
->nr
);
1963 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1964 irlap_send_rr_frame(self
, RSP_FRAME
);
1966 irlap_start_wd_timer(self
, self
->wd_timeout
);
1972 * Unexpected Next to Receive(NR) ?
1974 if ((ns_status
== NS_EXPECTED
) && (nr_status
== NR_UNEXPECTED
))
1977 pr_debug("RECV_I_RSP: frame(s) lost\n");
1979 self
->vr
= (self
->vr
+ 1) % 8;
1981 /* Update Nr received */
1982 irlap_update_nr_received(self
, info
->nr
);
1984 /* Resend rejected frames */
1985 irlap_resend_rejected_frames(self
, RSP_FRAME
);
1987 /* Keep state, do not move this line */
1988 irlap_next_state(self
, LAP_NRM_S
);
1990 irlap_data_indication(self
, skb
, FALSE
);
1991 irlap_start_wd_timer(self
, self
->wd_timeout
);
1995 * This is not documented in IrLAP!! Unexpected NR
1996 * with poll bit cleared
1999 self
->vr
= (self
->vr
+ 1) % 8;
2001 /* Update Nr received */
2002 irlap_update_nr_received(self
, info
->nr
);
2004 /* Keep state, do not move this line */
2005 irlap_next_state(self
, LAP_NRM_S
);
2007 irlap_data_indication(self
, skb
, FALSE
);
2008 irlap_start_wd_timer(self
, self
->wd_timeout
);
2013 if (ret
== NR_INVALID
) {
2014 pr_debug("NRM_S, NR_INVALID not implemented!\n");
2016 if (ret
== NS_INVALID
) {
2017 pr_debug("NRM_S, NS_INVALID not implemented!\n");
2025 irlap_data_indication(self
, skb
, TRUE
);
2026 irlap_next_state(self
, LAP_NRM_S
); /* Keep state */
2029 * Any pending data requests?
2031 if (!skb_queue_empty(&self
->txq
) &&
2032 (self
->window
> 0) && !self
->remote_busy
)
2034 irlap_data_indication(self
, skb
, TRUE
);
2036 del_timer(&self
->wd_timer
);
2038 irlap_next_state(self
, LAP_XMIT_S
);
2040 irlap_data_indication(self
, skb
, TRUE
);
2042 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2044 irlap_send_rr_frame(self
, RSP_FRAME
);
2045 self
->ack_required
= FALSE
;
2047 irlap_start_wd_timer(self
, self
->wd_timeout
);
2049 /* Keep the state */
2050 irlap_next_state(self
, LAP_NRM_S
);
2055 self
->retry_count
= 0;
2060 nr_status
= irlap_validate_nr_received(self
, info
->nr
);
2061 if (nr_status
== NR_EXPECTED
) {
2062 if (!skb_queue_empty(&self
->txq
) &&
2063 (self
->window
> 0)) {
2064 self
->remote_busy
= FALSE
;
2066 /* Update Nr received */
2067 irlap_update_nr_received(self
, info
->nr
);
2068 del_timer(&self
->wd_timer
);
2070 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2071 irlap_next_state(self
, LAP_XMIT_S
);
2073 self
->remote_busy
= FALSE
;
2074 /* Update Nr received */
2075 irlap_update_nr_received(self
, info
->nr
);
2076 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2077 irlap_start_wd_timer(self
, self
->wd_timeout
);
2079 /* Note : if the link is idle (this case),
2080 * we never go in XMIT_S, so we never get a
2081 * chance to process any DISCONNECT_REQUEST.
2082 * Do it now ! - Jean II */
2083 if (self
->disconnect_pending
) {
2085 irlap_send_rd_frame(self
);
2086 irlap_flush_all_queues(self
);
2088 irlap_next_state(self
, LAP_SCLOSE
);
2090 /* Just send back pf bit */
2091 irlap_send_rr_frame(self
, RSP_FRAME
);
2093 irlap_next_state(self
, LAP_NRM_S
);
2096 } else if (nr_status
== NR_UNEXPECTED
) {
2097 self
->remote_busy
= FALSE
;
2098 irlap_update_nr_received(self
, info
->nr
);
2099 irlap_resend_rejected_frames(self
, RSP_FRAME
);
2101 irlap_start_wd_timer(self
, self
->wd_timeout
);
2104 irlap_next_state(self
, LAP_NRM_S
);
2106 pr_debug("%s(), invalid nr not implemented!\n",
2111 /* SNRM frame is not allowed to contain an I-field */
2113 del_timer(&self
->wd_timer
);
2114 pr_debug("%s(), received SNRM cmd\n", __func__
);
2115 irlap_next_state(self
, LAP_RESET_CHECK
);
2117 irlap_reset_indication(self
);
2119 pr_debug("%s(), SNRM frame contained an I-field!\n",
2125 irlap_update_nr_received(self
, info
->nr
);
2126 if (self
->remote_busy
) {
2127 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2128 irlap_send_rr_frame(self
, RSP_FRAME
);
2130 irlap_resend_rejected_frames(self
, RSP_FRAME
);
2131 irlap_start_wd_timer(self
, self
->wd_timeout
);
2134 irlap_update_nr_received(self
, info
->nr
);
2135 if (self
->remote_busy
) {
2136 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2137 irlap_send_rr_frame(self
, RSP_FRAME
);
2139 irlap_resend_rejected_frame(self
, RSP_FRAME
);
2140 irlap_start_wd_timer(self
, self
->wd_timeout
);
2142 case WD_TIMER_EXPIRED
:
2144 * Wait until retry_count * n matches negotiated threshold/
2145 * disconnect time (note 2 in IrLAP p. 82)
2147 * Similar to irlap_state_nrm_p() -> FINAL_TIMER_EXPIRED
2148 * Note : self->wd_timeout = (self->final_timeout * 2),
2149 * which explain why we use (self->N2 / 2) here !!!
2152 pr_debug("%s(), retry_count = %d\n", __func__
,
2155 if (self
->retry_count
< (self
->N2
/ 2)) {
2156 /* No retry, just wait for primary */
2157 irlap_start_wd_timer(self
, self
->wd_timeout
);
2158 self
->retry_count
++;
2160 if((self
->retry_count
% (self
->N1
/ 2)) == 0)
2161 irlap_status_indication(self
,
2162 STATUS_NO_ACTIVITY
);
2164 irlap_apply_default_connection_parameters(self
);
2166 /* Always switch state before calling upper layers */
2167 irlap_next_state(self
, LAP_NDM
);
2168 irlap_disconnect_indication(self
, LAP_NO_RESPONSE
);
2172 /* Always switch state before calling upper layers */
2173 irlap_next_state(self
, LAP_NDM
);
2175 /* Send disconnect response */
2176 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2177 irlap_send_ua_response_frame(self
, NULL
);
2179 del_timer(&self
->wd_timer
);
2180 irlap_flush_all_queues(self
);
2181 /* Set default link parameters */
2182 irlap_apply_default_connection_parameters(self
);
2184 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
2186 case RECV_DISCOVERY_XID_CMD
:
2187 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2188 irlap_send_rr_frame(self
, RSP_FRAME
);
2189 self
->ack_required
= TRUE
;
2190 irlap_start_wd_timer(self
, self
->wd_timeout
);
2191 irlap_next_state(self
, LAP_NRM_S
);
2195 /* Remove test frame header (only LAP header in NRM) */
2196 skb_pull(skb
, LAP_ADDR_HEADER
+ LAP_CTRL_HEADER
);
2198 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2199 irlap_start_wd_timer(self
, self
->wd_timeout
);
2201 /* Send response (info will be copied) */
2202 irlap_send_test_frame(self
, self
->caddr
, info
->daddr
, skb
);
2205 pr_debug("%s(), Unknown event %d, (%s)\n", __func__
,
2206 event
, irlap_event
[event
]);
2215 * Function irlap_state_sclose (self, event, skb, info)
2217 static int irlap_state_sclose(struct irlap_cb
*self
, IRLAP_EVENT event
,
2218 struct sk_buff
*skb
, struct irlap_info
*info
)
2220 IRDA_ASSERT(self
!= NULL
, return -ENODEV
;);
2221 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -EBADR
;);
2225 /* Always switch state before calling upper layers */
2226 irlap_next_state(self
, LAP_NDM
);
2228 /* Send disconnect response */
2229 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2230 irlap_send_ua_response_frame(self
, NULL
);
2232 del_timer(&self
->wd_timer
);
2233 /* Set default link parameters */
2234 irlap_apply_default_connection_parameters(self
);
2236 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
2239 /* IrLAP-1.1 p.82: in SCLOSE, S and I type RSP frames
2240 * shall take us down into default NDM state, like DM_RSP
2247 /* Always switch state before calling upper layers */
2248 irlap_next_state(self
, LAP_NDM
);
2250 del_timer(&self
->wd_timer
);
2251 irlap_apply_default_connection_parameters(self
);
2253 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
2255 case WD_TIMER_EXPIRED
:
2256 /* Always switch state before calling upper layers */
2257 irlap_next_state(self
, LAP_NDM
);
2259 irlap_apply_default_connection_parameters(self
);
2261 irlap_disconnect_indication(self
, LAP_DISC_INDICATION
);
2264 /* IrLAP-1.1 p.82: in SCLOSE, basically any received frame
2265 * with pf=1 shall restart the wd-timer and resend the rd:rsp
2267 if (info
!= NULL
&& info
->pf
) {
2268 del_timer(&self
->wd_timer
);
2269 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2270 irlap_send_rd_frame(self
);
2271 irlap_start_wd_timer(self
, self
->wd_timeout
);
2272 break; /* stay in SCLOSE */
2275 pr_debug("%s(), Unknown event %d, (%s)\n", __func__
,
2276 event
, irlap_event
[event
]);
2284 static int irlap_state_reset_check( struct irlap_cb
*self
, IRLAP_EVENT event
,
2285 struct sk_buff
*skb
,
2286 struct irlap_info
*info
)
2290 pr_debug("%s(), event=%s\n", __func__
, irlap_event
[event
]);
2292 IRDA_ASSERT(self
!= NULL
, return -ENODEV
;);
2293 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return -EBADR
;);
2296 case RESET_RESPONSE
:
2297 irlap_send_ua_response_frame(self
, &self
->qos_rx
);
2298 irlap_initiate_connection_state(self
);
2299 irlap_start_wd_timer(self
, WD_TIMEOUT
);
2300 irlap_flush_all_queues(self
);
2302 irlap_next_state(self
, LAP_NRM_S
);
2304 case DISCONNECT_REQUEST
:
2305 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
2306 irlap_send_rd_frame(self
);
2307 irlap_start_wd_timer(self
, WD_TIMEOUT
);
2308 irlap_next_state(self
, LAP_SCLOSE
);
2311 pr_debug("%s(), Unknown event %d, (%s)\n", __func__
,
2312 event
, irlap_event
[event
]);