Linux Audio

Check our new training course

Loading...
v3.15
   1/*********************************************************************
   2 *
   3 * Filename:      irlap_event.c
   4 * Version:       0.9
   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>
  11 *
  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>
  16 *
  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.
  21 *
  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.
  25 *
  26 ********************************************************************/
  27
  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>
  33
  34#include <net/irda/irda.h>
  35#include <net/irda/irlap_event.h>
  36
  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(), ... */
  43
  44#include <net/irda/irda_device.h>
  45
  46#ifdef CONFIG_IRDA_FAST_RR
  47int sysctl_fast_poll_increase = 50;
  48#endif
  49
  50static int irlap_state_ndm    (struct irlap_cb *self, IRLAP_EVENT event,
  51			       struct sk_buff *skb, struct irlap_info *info);
  52static int irlap_state_query  (struct irlap_cb *self, IRLAP_EVENT event,
  53			       struct sk_buff *skb, struct irlap_info *info);
  54static int irlap_state_reply  (struct irlap_cb *self, IRLAP_EVENT event,
  55			       struct sk_buff *skb, struct irlap_info *info);
  56static int irlap_state_conn   (struct irlap_cb *self, IRLAP_EVENT event,
  57			       struct sk_buff *skb, struct irlap_info *info);
  58static int irlap_state_setup  (struct irlap_cb *self, IRLAP_EVENT event,
  59			       struct sk_buff *skb, struct irlap_info *info);
  60static int irlap_state_offline(struct irlap_cb *self, IRLAP_EVENT event,
  61			       struct sk_buff *skb, struct irlap_info *info);
  62static int irlap_state_xmit_p (struct irlap_cb *self, IRLAP_EVENT event,
  63			       struct sk_buff *skb, struct irlap_info *info);
  64static int irlap_state_pclose (struct irlap_cb *self, IRLAP_EVENT event,
  65			       struct sk_buff *skb, struct irlap_info *info);
  66static int irlap_state_nrm_p  (struct irlap_cb *self, IRLAP_EVENT event,
  67			       struct sk_buff *skb, struct irlap_info *info);
  68static int irlap_state_reset_wait(struct irlap_cb *self, IRLAP_EVENT event,
  69				  struct sk_buff *skb, struct irlap_info *info);
  70static int irlap_state_reset  (struct irlap_cb *self, IRLAP_EVENT event,
  71			       struct sk_buff *skb, struct irlap_info *info);
  72static int irlap_state_nrm_s  (struct irlap_cb *self, IRLAP_EVENT event,
  73			       struct sk_buff *skb, struct irlap_info *info);
  74static int irlap_state_xmit_s (struct irlap_cb *self, IRLAP_EVENT event,
  75			       struct sk_buff *skb, struct irlap_info *info);
  76static int irlap_state_sclose (struct irlap_cb *self, IRLAP_EVENT event,
  77			       struct sk_buff *skb, struct irlap_info *info);
  78static int irlap_state_reset_check(struct irlap_cb *, IRLAP_EVENT event,
  79				   struct sk_buff *, struct irlap_info *);
  80
  81#ifdef CONFIG_IRDA_DEBUG
  82static const char *const irlap_event[] = {
  83	"DISCOVERY_REQUEST",
  84	"CONNECT_REQUEST",
  85	"CONNECT_RESPONSE",
  86	"DISCONNECT_REQUEST",
  87	"DATA_REQUEST",
  88	"RESET_REQUEST",
  89	"RESET_RESPONSE",
  90	"SEND_I_CMD",
  91	"SEND_UI_FRAME",
  92	"RECV_DISCOVERY_XID_CMD",
  93	"RECV_DISCOVERY_XID_RSP",
  94	"RECV_SNRM_CMD",
  95	"RECV_TEST_CMD",
  96	"RECV_TEST_RSP",
  97	"RECV_UA_RSP",
  98	"RECV_DM_RSP",
  99	"RECV_RD_RSP",
 100	"RECV_I_CMD",
 101	"RECV_I_RSP",
 102	"RECV_UI_FRAME",
 103	"RECV_FRMR_RSP",
 104	"RECV_RR_CMD",
 105	"RECV_RR_RSP",
 106	"RECV_RNR_CMD",
 107	"RECV_RNR_RSP",
 108	"RECV_REJ_CMD",
 109	"RECV_REJ_RSP",
 110	"RECV_SREJ_CMD",
 111	"RECV_SREJ_RSP",
 112	"RECV_DISC_CMD",
 113	"SLOT_TIMER_EXPIRED",
 114	"QUERY_TIMER_EXPIRED",
 115	"FINAL_TIMER_EXPIRED",
 116	"POLL_TIMER_EXPIRED",
 117	"DISCOVERY_TIMER_EXPIRED",
 118	"WD_TIMER_EXPIRED",
 119	"BACKOFF_TIMER_EXPIRED",
 120	"MEDIA_BUSY_TIMER_EXPIRED",
 121};
 122#endif	/* CONFIG_IRDA_DEBUG */
 123
 124const char *const irlap_state[] = {
 125	"LAP_NDM",
 126	"LAP_QUERY",
 127	"LAP_REPLY",
 128	"LAP_CONN",
 129	"LAP_SETUP",
 130	"LAP_OFFLINE",
 131	"LAP_XMIT_P",
 132	"LAP_PCLOSE",
 133	"LAP_NRM_P",
 134	"LAP_RESET_WAIT",
 135	"LAP_RESET",
 136	"LAP_NRM_S",
 137	"LAP_XMIT_S",
 138	"LAP_SCLOSE",
 139	"LAP_RESET_CHECK",
 140};
 141
 142static int (*state[])(struct irlap_cb *self, IRLAP_EVENT event,
 143		      struct sk_buff *skb, struct irlap_info *info) =
 144{
 145	irlap_state_ndm,
 146	irlap_state_query,
 147	irlap_state_reply,
 148	irlap_state_conn,
 149	irlap_state_setup,
 150	irlap_state_offline,
 151	irlap_state_xmit_p,
 152	irlap_state_pclose,
 153	irlap_state_nrm_p,
 154	irlap_state_reset_wait,
 155	irlap_state_reset,
 156	irlap_state_nrm_s,
 157	irlap_state_xmit_s,
 158	irlap_state_sclose,
 159	irlap_state_reset_check,
 160};
 161
 162/*
 163 * Function irda_poll_timer_expired (data)
 164 *
 165 *    Poll timer has expired. Normally we must now send a RR frame to the
 166 *    remote device
 167 */
 168static void irlap_poll_timer_expired(void *data)
 169{
 170	struct irlap_cb *self = (struct irlap_cb *) data;
 171
 172	IRDA_ASSERT(self != NULL, return;);
 173	IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
 174
 175	irlap_do_event(self, POLL_TIMER_EXPIRED, NULL, NULL);
 176}
 177
 178/*
 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
 183 */
 184static void irlap_start_poll_timer(struct irlap_cb *self, int timeout)
 185{
 186	IRDA_ASSERT(self != NULL, return;);
 187	IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
 188
 189#ifdef CONFIG_IRDA_FAST_RR
 190	/*
 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
 193	 */
 194	if (skb_queue_empty(&self->txq) || self->remote_busy) {
 195		if (self->fast_RR == TRUE) {
 196			/*
 197			 *  Assert that the fast poll timer has not reached the
 198			 *  normal poll timer yet
 199			 */
 200			if (self->fast_RR_timeout < timeout) {
 201				/*
 202				 *  FIXME: this should be a more configurable
 203				 *         function
 204				 */
 205				self->fast_RR_timeout +=
 206					(sysctl_fast_poll_increase * HZ/1000);
 207
 208				/* Use this fast(er) timeout instead */
 209				timeout = self->fast_RR_timeout;
 210			}
 211		} else {
 212			self->fast_RR = TRUE;
 213
 214			/* Start with just 0 ms */
 215			self->fast_RR_timeout = 0;
 216			timeout = 0;
 217		}
 218	} else
 219		self->fast_RR = FALSE;
 220
 221	IRDA_DEBUG(3, "%s(), timeout=%d (%ld)\n", __func__, timeout, jiffies);
 222#endif /* CONFIG_IRDA_FAST_RR */
 223
 224	if (timeout == 0)
 225		irlap_do_event(self, POLL_TIMER_EXPIRED, NULL, NULL);
 226	else
 227		irda_start_timer(&self->poll_timer, timeout, self,
 228				 irlap_poll_timer_expired);
 229}
 230
 231/*
 232 * Function irlap_do_event (event, skb, info)
 233 *
 234 *    Rushes through the state machine without any delay. If state == XMIT
 235 *    then send queued data frames.
 236 */
 237void irlap_do_event(struct irlap_cb *self, IRLAP_EVENT event,
 238		    struct sk_buff *skb, struct irlap_info *info)
 239{
 240	int ret;
 241
 242	if (!self || self->magic != LAP_MAGIC)
 243		return;
 244
 245	IRDA_DEBUG(3, "%s(), event = %s, state = %s\n", __func__,
 246		   irlap_event[event], irlap_state[self->state]);
 247
 248	ret = (*state[self->state])(self, event, skb, info);
 249
 250	/*
 251	 *  Check if there are any pending events that needs to be executed
 252	 */
 253	switch (self->state) {
 254	case LAP_XMIT_P: /* FALLTHROUGH */
 255	case LAP_XMIT_S:
 256		/*
 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
 262		 */
 263		IRDA_DEBUG(2, "%s() : queue len = %d\n", __func__,
 264			   skb_queue_len(&self->txq));
 265
 266		if (!skb_queue_empty(&self->txq)) {
 267			/* Prevent race conditions with irlap_data_request() */
 268			self->local_busy = TRUE;
 269
 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
 274			 * send them.
 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 */
 284
 285			/* Try to send away all queued data frames */
 286			while ((skb = skb_dequeue(&self->txq)) != NULL) {
 287				/* Send one frame */
 288				ret = (*state[self->state])(self, SEND_I_CMD,
 289							    skb, NULL);
 290				/* Drop reference count.
 291				 * It will be increase as needed in
 292				 * irlap_send_data_xxx() */
 293				kfree_skb(skb);
 294
 295				/* Poll the higher layers for one more frame */
 296				irlmp_flow_indication(self->notify.instance,
 297						      FLOW_START);
 298
 299				if (ret == -EPROTO)
 300					break; /* Try again later! */
 301			}
 302			/* Finished transmitting */
 303			self->local_busy = FALSE;
 304		} else if (self->disconnect_pending) {
 305			self->disconnect_pending = FALSE;
 306
 307			ret = (*state[self->state])(self, DISCONNECT_REQUEST,
 308						    NULL, NULL);
 309		}
 310		break;
 311/*	case LAP_NDM: */
 312/*	case LAP_CONN: */
 313/*	case LAP_RESET_WAIT: */
 314/*	case LAP_RESET_CHECK: */
 315	default:
 316		break;
 317	}
 318}
 319
 320/*
 321 * Function irlap_state_ndm (event, skb, frame)
 322 *
 323 *    NDM (Normal Disconnected Mode) state
 324 *
 325 */
 326static int irlap_state_ndm(struct irlap_cb *self, IRLAP_EVENT event,
 327			   struct sk_buff *skb, struct irlap_info *info)
 328{
 329	discovery_t *discovery_rsp;
 330	int ret = 0;
 331
 332	IRDA_ASSERT(self != NULL, return -1;);
 333	IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
 334
 335	switch (event) {
 336	case CONNECT_REQUEST:
 337		IRDA_ASSERT(self->netdev != NULL, return -1;);
 338
 339		if (self->media_busy) {
 340			/* Note : this will never happen, because we test
 341			 * media busy in irlap_connect_request() and
 342			 * postpone the event... - Jean II */
 343			IRDA_DEBUG(0, "%s(), CONNECT_REQUEST: media busy!\n",
 344				   __func__);
 345
 346			/* Always switch state before calling upper layers */
 347			irlap_next_state(self, LAP_NDM);
 348
 349			irlap_disconnect_indication(self, LAP_MEDIA_BUSY);
 350		} else {
 351			irlap_send_snrm_frame(self, &self->qos_rx);
 352
 353			/* Start Final-bit timer */
 354			irlap_start_final_timer(self, self->final_timeout);
 355
 356			self->retry_count = 0;
 357			irlap_next_state(self, LAP_SETUP);
 358		}
 359		break;
 360	case RECV_SNRM_CMD:
 361		/* Check if the frame contains and I field */
 362		if (info) {
 363			self->daddr = info->daddr;
 364			self->caddr = info->caddr;
 365
 366			irlap_next_state(self, LAP_CONN);
 367
 368			irlap_connect_indication(self, skb);
 369		} else {
 370			IRDA_DEBUG(0, "%s(), SNRM frame does not "
 371				   "contain an I field!\n", __func__);
 372		}
 373		break;
 374	case DISCOVERY_REQUEST:
 375		IRDA_ASSERT(info != NULL, return -1;);
 376
 377		if (self->media_busy) {
 378			IRDA_DEBUG(1, "%s(), DISCOVERY_REQUEST: media busy!\n",
 379				   __func__);
 380			/* irlap->log.condition = MEDIA_BUSY; */
 381
 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()
 386			 * Jean II */
 387			return 0;
 388		}
 389
 390		self->S = info->S;
 391		self->s = info->s;
 392		irlap_send_discovery_xid_frame(self, info->S, info->s, TRUE,
 393					       info->discovery);
 394		self->frame_sent = FALSE;
 395		self->s++;
 396
 397		irlap_start_slot_timer(self, self->slot_timeout);
 398		irlap_next_state(self, LAP_QUERY);
 399		break;
 400	case RECV_DISCOVERY_XID_CMD:
 401		IRDA_ASSERT(info != NULL, return -1;);
 402
 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,
 406								   info->s);
 407			if (self->slot == info->s) {
 408				discovery_rsp = irlmp_get_discovery_response();
 409				discovery_rsp->data.daddr = info->daddr;
 410
 411				irlap_send_discovery_xid_frame(self, info->S,
 412							       self->slot,
 413							       FALSE,
 414							       discovery_rsp);
 415				self->frame_sent = TRUE;
 416			} else
 417				self->frame_sent = FALSE;
 418
 419			/*
 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
 423			 */
 424			irlap_start_query_timer(self, info->S, info->s);
 425			irlap_next_state(self, LAP_REPLY);
 426		} else {
 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
 442		 * those cases...
 443		 * Jean II
 444		 */
 445			IRDA_DEBUG(1, "%s(), Receiving final discovery request, missed the discovery slots :-(\n", __func__);
 
 446
 447			/* Last discovery request -> in the log */
 448			irlap_discovery_indication(self, info->discovery);
 449		}
 450		break;
 451	case MEDIA_BUSY_TIMER_EXPIRED:
 452		/* A bunch of events may be postponed because the media is
 453		 * busy (usually immediately after we close a connection),
 454		 * or while we are doing discovery (state query/reply).
 455		 * In all those cases, the media busy flag will be cleared
 456		 * when it's OK for us to process those postponed events.
 457		 * This event is not mentioned in the state machines in the
 458		 * IrLAP spec. It's because they didn't consider Ultra and
 459		 * postponing connection request is optional.
 460		 * Jean II */
 461#ifdef CONFIG_IRDA_ULTRA
 462		/* Send any pending Ultra frames if any */
 463		if (!skb_queue_empty(&self->txq_ultra)) {
 464			/* We don't send the frame, just post an event.
 465			 * Also, previously this code was in timer.c...
 466			 * Jean II */
 467			ret = (*state[self->state])(self, SEND_UI_FRAME,
 468						    NULL, NULL);
 469		}
 470#endif /* CONFIG_IRDA_ULTRA */
 471		/* Check if we should try to connect.
 472		 * This code was previously in irlap_do_event() */
 473		if (self->connect_pending) {
 474			self->connect_pending = FALSE;
 475
 476			/* This one *should* not pend in this state, except
 477			 * if a socket try to connect and immediately
 478			 * disconnect. - clear - Jean II */
 479			if (self->disconnect_pending)
 480				irlap_disconnect_indication(self, LAP_DISC_INDICATION);
 481			else
 482				ret = (*state[self->state])(self,
 483							    CONNECT_REQUEST,
 484							    NULL, NULL);
 485			self->disconnect_pending = FALSE;
 486		}
 487		/* Note : one way to test if this code works well (including
 488		 * media busy and small busy) is to create a user space
 489		 * application generating an Ultra packet every 3.05 sec (or
 490		 * 2.95 sec) and to see how it interact with discovery.
 491		 * It's fairly easy to check that no packet is lost, that the
 492		 * packets are postponed during discovery and that after
 493		 * discovery indication you have a 100ms "gap".
 494		 * As connection request and Ultra are now processed the same
 495		 * way, this avoid the tedious job of trying IrLAP connection
 496		 * in all those cases...
 497		 * Jean II */
 498		break;
 499#ifdef CONFIG_IRDA_ULTRA
 500	case SEND_UI_FRAME:
 501	{
 502		int i;
 503		/* Only allowed to repeat an operation twice */
 504		for (i=0; ((i<2) && (self->media_busy == FALSE)); i++) {
 505			skb = skb_dequeue(&self->txq_ultra);
 506			if (skb)
 507				irlap_send_ui_frame(self, skb, CBROADCAST,
 508						    CMD_FRAME);
 509			else
 510				break;
 511			/* irlap_send_ui_frame() won't increase skb reference
 512			 * count, so no dev_kfree_skb() - Jean II */
 513		}
 514		if (i == 2) {
 515			/* Force us to listen 500 ms again */
 516			irda_device_set_media_busy(self->netdev, TRUE);
 517		}
 518		break;
 519	}
 520	case RECV_UI_FRAME:
 521		/* Only accept broadcast frames in NDM mode */
 522		if (info->caddr != CBROADCAST) {
 523			IRDA_DEBUG(0, "%s(), not a broadcast frame!\n",
 524				   __func__);
 525		} else
 526			irlap_unitdata_indication(self, skb);
 527		break;
 528#endif /* CONFIG_IRDA_ULTRA */
 529	case RECV_TEST_CMD:
 530		/* Remove test frame header */
 531		skb_pull(skb, sizeof(struct test_frame));
 532
 533		/*
 534		 * Send response. This skb will not be sent out again, and
 535		 * will only be used to send out the same info as the cmd
 536		 */
 537		irlap_send_test_frame(self, CBROADCAST, info->daddr, skb);
 538		break;
 539	case RECV_TEST_RSP:
 540		IRDA_DEBUG(0, "%s() not implemented!\n", __func__);
 541		break;
 542	default:
 543		IRDA_DEBUG(2, "%s(), Unknown event %s\n", __func__,
 544			   irlap_event[event]);
 545
 546		ret = -1;
 547		break;
 548	}
 549	return ret;
 550}
 551
 552/*
 553 * Function irlap_state_query (event, skb, info)
 554 *
 555 *    QUERY state
 556 *
 557 */
 558static int irlap_state_query(struct irlap_cb *self, IRLAP_EVENT event,
 559			     struct sk_buff *skb, struct irlap_info *info)
 560{
 561	int ret = 0;
 562
 563	IRDA_ASSERT(self != NULL, return -1;);
 564	IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
 565
 566	switch (event) {
 567	case RECV_DISCOVERY_XID_RSP:
 568		IRDA_ASSERT(info != NULL, return -1;);
 569		IRDA_ASSERT(info->discovery != NULL, return -1;);
 570
 571		IRDA_DEBUG(4, "%s(), daddr=%08x\n", __func__,
 572			   info->discovery->data.daddr);
 573
 574		if (!self->discovery_log) {
 575			IRDA_WARNING("%s: discovery log is gone! "
 576				     "maybe the discovery timeout has been set"
 577				     " too short?\n", __func__);
 578			break;
 579		}
 580		hashbin_insert(self->discovery_log,
 581			       (irda_queue_t *) info->discovery,
 582			       info->discovery->data.daddr, NULL);
 583
 584		/* Keep state */
 585		/* irlap_next_state(self, LAP_QUERY);  */
 586
 587		break;
 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).
 597		 * Jean II
 598		 */
 599
 600		IRDA_ASSERT(info != NULL, return -1;);
 601
 602		IRDA_DEBUG(1, "%s(), Receiving discovery request (s = %d) while performing discovery :-(\n", __func__, info->s);
 
 603
 604		/* Last discovery request ? */
 605		if (info->s == 0xff)
 606			irlap_discovery_indication(self, info->discovery);
 607		break;
 608	case SLOT_TIMER_EXPIRED:
 609		/*
 610		 * Wait a little longer if we detect an incoming frame. This
 611		 * is not mentioned in the spec, but is a good thing to do,
 612		 * since we want to work even with devices that violate the
 613		 * timing requirements.
 614		 */
 615		if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
 616			IRDA_DEBUG(2, "%s(), device is slow to answer, "
 617				   "waiting some more!\n", __func__);
 618			irlap_start_slot_timer(self, msecs_to_jiffies(10));
 619			self->add_wait = TRUE;
 620			return ret;
 621		}
 622		self->add_wait = FALSE;
 623
 624		if (self->s < self->S) {
 625			irlap_send_discovery_xid_frame(self, self->S,
 626						       self->s, TRUE,
 627						       self->discovery_cmd);
 628			self->s++;
 629			irlap_start_slot_timer(self, self->slot_timeout);
 630
 631			/* Keep state */
 632			irlap_next_state(self, LAP_QUERY);
 633		} else {
 634			/* This is the final slot! */
 635			irlap_send_discovery_xid_frame(self, self->S, 0xff,
 636						       TRUE,
 637						       self->discovery_cmd);
 638
 639			/* Always switch state before calling upper layers */
 640			irlap_next_state(self, LAP_NDM);
 641
 642			/*
 643			 *  We are now finished with the discovery procedure,
 644			 *  so now we must return the results
 645			 */
 646			irlap_discovery_confirm(self, self->discovery_log);
 647
 648			/* IrLMP should now have taken care of the log */
 649			self->discovery_log = NULL;
 650		}
 651		break;
 652	default:
 653		IRDA_DEBUG(2, "%s(), Unknown event %s\n", __func__,
 654			   irlap_event[event]);
 655
 656		ret = -1;
 657		break;
 658	}
 659	return ret;
 660}
 661
 662/*
 663 * Function irlap_state_reply (self, event, skb, info)
 664 *
 665 *    REPLY, we have received a XID discovery frame from a device and we
 666 *    are waiting for the right time slot to send a response XID frame
 667 *
 668 */
 669static int irlap_state_reply(struct irlap_cb *self, IRLAP_EVENT event,
 670			     struct sk_buff *skb, struct irlap_info *info)
 671{
 672	discovery_t *discovery_rsp;
 673	int ret=0;
 674
 675	IRDA_DEBUG(4, "%s()\n", __func__);
 676
 677	IRDA_ASSERT(self != NULL, return -1;);
 678	IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
 679
 680	switch (event) {
 681	case QUERY_TIMER_EXPIRED:
 682		IRDA_DEBUG(0, "%s(), QUERY_TIMER_EXPIRED <%ld>\n",
 683			   __func__, jiffies);
 684		irlap_next_state(self, LAP_NDM);
 685		break;
 686	case RECV_DISCOVERY_XID_CMD:
 687		IRDA_ASSERT(info != NULL, return -1;);
 688		/* Last frame? */
 689		if (info->s == 0xff) {
 690			del_timer(&self->query_timer);
 691
 692			/* info->log.condition = REMOTE; */
 693
 694			/* Always switch state before calling upper layers */
 695			irlap_next_state(self, LAP_NDM);
 696
 697			irlap_discovery_indication(self, info->discovery);
 698		} else {
 699			/* If it's our slot, send our reply */
 700			if ((info->s >= self->slot) && (!self->frame_sent)) {
 701				discovery_rsp = irlmp_get_discovery_response();
 702				discovery_rsp->data.daddr = info->daddr;
 703
 704				irlap_send_discovery_xid_frame(self, info->S,
 705							       self->slot,
 706							       FALSE,
 707							       discovery_rsp);
 708
 709				self->frame_sent = TRUE;
 710			}
 711			/* Readjust our timer to accommodate devices
 712			 * doing faster or slower discovery than us...
 713			 * Jean II */
 714			irlap_start_query_timer(self, info->S, info->s);
 715
 716			/* Keep state */
 717			//irlap_next_state(self, LAP_REPLY);
 718		}
 719		break;
 720	default:
 721		IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __func__,
 722			   event, irlap_event[event]);
 723
 724		ret = -1;
 725		break;
 726	}
 727	return ret;
 728}
 729
 730/*
 731 * Function irlap_state_conn (event, skb, info)
 732 *
 733 *    CONN, we have received a SNRM command and is waiting for the upper
 734 *    layer to accept or refuse connection
 735 *
 736 */
 737static int irlap_state_conn(struct irlap_cb *self, IRLAP_EVENT event,
 738			    struct sk_buff *skb, struct irlap_info *info)
 739{
 740	int ret = 0;
 741
 742	IRDA_DEBUG(4, "%s(), event=%s\n", __func__, irlap_event[ event]);
 743
 744	IRDA_ASSERT(self != NULL, return -1;);
 745	IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
 746
 747	switch (event) {
 748	case CONNECT_RESPONSE:
 749		skb_pull(skb, sizeof(struct snrm_frame));
 750
 751		IRDA_ASSERT(self->netdev != NULL, return -1;);
 752
 753		irlap_qos_negotiate(self, skb);
 754
 755		irlap_initiate_connection_state(self);
 756
 757		/*
 758		 * Applying the parameters now will make sure we change speed
 759		 * *after* we have sent the next frame
 760		 */
 761		irlap_apply_connection_parameters(self, FALSE);
 762
 763		/*
 764		 * Sending this frame will force a speed change after it has
 765		 * been sent (i.e. the frame will be sent at 9600).
 766		 */
 767		irlap_send_ua_response_frame(self, &self->qos_rx);
 768
 769#if 0
 770		/*
 771		 * We are allowed to send two frames, but this may increase
 772		 * the connect latency, so lets not do it for now.
 773		 */
 774		/* This is full of good intentions, but doesn't work in
 775		 * practice.
 776		 * After sending the first UA response, we switch the
 777		 * dongle to the negotiated speed, which is usually
 778		 * different than 9600 kb/s.
 779		 * From there, there is two solutions :
 780		 * 1) The other end has received the first UA response :
 781		 * it will set up the connection, move to state LAP_NRM_P,
 782		 * and will ignore and drop the second UA response.
 783		 * Actually, it's even worse : the other side will almost
 784		 * immediately send a RR that will likely collide with the
 785		 * UA response (depending on negotiated turnaround).
 786		 * 2) The other end has not received the first UA response,
 787		 * will stay at 9600 and will never see the second UA response.
 788		 * Jean II */
 789		irlap_send_ua_response_frame(self, &self->qos_rx);
 790#endif
 791
 792		/*
 793		 *  The WD-timer could be set to the duration of the P-timer
 794		 *  for this case, but it is recommended to use twice the
 795		 *  value (note 3 IrLAP p. 60).
 796		 */
 797		irlap_start_wd_timer(self, self->wd_timeout);
 798		irlap_next_state(self, LAP_NRM_S);
 799
 800		break;
 801	case RECV_DISCOVERY_XID_CMD:
 802		IRDA_DEBUG(3, "%s(), event RECV_DISCOVER_XID_CMD!\n",
 803			   __func__);
 804		irlap_next_state(self, LAP_NDM);
 805
 806		break;
 807	case DISCONNECT_REQUEST:
 808		IRDA_DEBUG(0, "%s(), Disconnect request!\n", __func__);
 809		irlap_send_dm_frame(self);
 810		irlap_next_state( self, LAP_NDM);
 811		irlap_disconnect_indication(self, LAP_DISC_INDICATION);
 812		break;
 813	default:
 814		IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __func__,
 815			   event, irlap_event[event]);
 816
 817		ret = -1;
 818		break;
 819	}
 820
 821	return ret;
 822}
 823
 824/*
 825 * Function irlap_state_setup (event, skb, frame)
 826 *
 827 *    SETUP state, The local layer has transmitted a SNRM command frame to
 828 *    a remote peer layer and is awaiting a reply .
 829 *
 830 */
 831static int irlap_state_setup(struct irlap_cb *self, IRLAP_EVENT event,
 832			     struct sk_buff *skb, struct irlap_info *info)
 833{
 834	int ret = 0;
 835
 836	IRDA_DEBUG(4, "%s()\n", __func__);
 837
 838	IRDA_ASSERT(self != NULL, return -1;);
 839	IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
 840
 841	switch (event) {
 842	case FINAL_TIMER_EXPIRED:
 843		if (self->retry_count < self->N3) {
 844/*
 845 *  Perform random backoff, Wait a random number of time units, minimum
 846 *  duration half the time taken to transmitt a SNRM frame, maximum duration
 847 *  1.5 times the time taken to transmit a SNRM frame. So this time should
 848 *  between 15 msecs and 45 msecs.
 849 */
 850			irlap_start_backoff_timer(self, msecs_to_jiffies(20 +
 851							(jiffies % 30)));
 852		} else {
 853			/* Always switch state before calling upper layers */
 854			irlap_next_state(self, LAP_NDM);
 855
 856			irlap_disconnect_indication(self, LAP_FOUND_NONE);
 857		}
 858		break;
 859	case BACKOFF_TIMER_EXPIRED:
 860		irlap_send_snrm_frame(self, &self->qos_rx);
 861		irlap_start_final_timer(self, self->final_timeout);
 862		self->retry_count++;
 863		break;
 864	case RECV_SNRM_CMD:
 865		IRDA_DEBUG(4, "%s(), SNRM battle!\n", __func__);
 866
 867		IRDA_ASSERT(skb != NULL, return 0;);
 868		IRDA_ASSERT(info != NULL, return 0;);
 869
 870		/*
 871		 *  The device with the largest device address wins the battle
 872		 *  (both have sent a SNRM command!)
 873		 */
 874		if (info &&(info->daddr > self->saddr)) {
 875			del_timer(&self->final_timer);
 876			irlap_initiate_connection_state(self);
 877
 878			IRDA_ASSERT(self->netdev != NULL, return -1;);
 879
 880			skb_pull(skb, sizeof(struct snrm_frame));
 881
 882			irlap_qos_negotiate(self, skb);
 883
 884			/* Send UA frame and then change link settings */
 885			irlap_apply_connection_parameters(self, FALSE);
 886			irlap_send_ua_response_frame(self, &self->qos_rx);
 887
 888			irlap_next_state(self, LAP_NRM_S);
 889			irlap_connect_confirm(self, skb);
 890
 891			/*
 892			 *  The WD-timer could be set to the duration of the
 893			 *  P-timer for this case, but it is recommended
 894			 *  to use twice the value (note 3 IrLAP p. 60).
 895			 */
 896			irlap_start_wd_timer(self, self->wd_timeout);
 897		} else {
 898			/* We just ignore the other device! */
 899			irlap_next_state(self, LAP_SETUP);
 900		}
 901		break;
 902	case RECV_UA_RSP:
 903		/* Stop F-timer */
 904		del_timer(&self->final_timer);
 905
 906		/* Initiate connection state */
 907		irlap_initiate_connection_state(self);
 908
 909		/* Negotiate connection parameters */
 910		IRDA_ASSERT(skb->len > 10, return -1;);
 911
 912		skb_pull(skb, sizeof(struct ua_frame));
 913
 914		IRDA_ASSERT(self->netdev != NULL, return -1;);
 915
 916		irlap_qos_negotiate(self, skb);
 917
 918		/* Set the new link setting *now* (before the rr frame) */
 919		irlap_apply_connection_parameters(self, TRUE);
 920		self->retry_count = 0;
 921
 922		/* Wait for turnaround time to give a chance to the other
 923		 * device to be ready to receive us.
 924		 * Note : the time to switch speed is typically larger
 925		 * than the turnaround time, but as we don't have the other
 926		 * side speed switch time, that's our best guess...
 927		 * Jean II */
 928		irlap_wait_min_turn_around(self, &self->qos_tx);
 929
 930		/* This frame will actually be sent at the new speed */
 931		irlap_send_rr_frame(self, CMD_FRAME);
 932
 933		/* The timer is set to half the normal timer to quickly
 934		 * detect a failure to negotiate the new connection
 935		 * parameters. IrLAP 6.11.3.2, note 3.
 936		 * Note that currently we don't process this failure
 937		 * properly, as we should do a quick disconnect.
 938		 * Jean II */
 939		irlap_start_final_timer(self, self->final_timeout/2);
 940		irlap_next_state(self, LAP_NRM_P);
 941
 942		irlap_connect_confirm(self, skb);
 943		break;
 944	case RECV_DM_RSP:     /* FALLTHROUGH */
 945	case RECV_DISC_CMD:
 946		del_timer(&self->final_timer);
 947		irlap_next_state(self, LAP_NDM);
 948
 949		irlap_disconnect_indication(self, LAP_DISC_INDICATION);
 950		break;
 951	default:
 952		IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __func__,
 953			   event, irlap_event[event]);
 954
 955		ret = -1;
 956		break;
 957	}
 958	return ret;
 959}
 960
 961/*
 962 * Function irlap_state_offline (self, event, skb, info)
 963 *
 964 *    OFFLINE state, not used for now!
 965 *
 966 */
 967static int irlap_state_offline(struct irlap_cb *self, IRLAP_EVENT event,
 968			       struct sk_buff *skb, struct irlap_info *info)
 969{
 970	IRDA_DEBUG( 0, "%s(), Unknown event\n", __func__);
 971
 972	return -1;
 973}
 974
 975/*
 976 * Function irlap_state_xmit_p (self, event, skb, info)
 977 *
 978 *    XMIT, Only the primary station has right to transmit, and we
 979 *    therefore do not expect to receive any transmissions from other
 980 *    stations.
 981 *
 982 */
 983static int irlap_state_xmit_p(struct irlap_cb *self, IRLAP_EVENT event,
 984			      struct sk_buff *skb, struct irlap_info *info)
 985{
 986	int ret = 0;
 987
 988	switch (event) {
 989	case SEND_I_CMD:
 990		/*
 991		 *  Only send frame if send-window > 0.
 992		 */
 993		if ((self->window > 0) && (!self->remote_busy)) {
 994			int nextfit;
 995#ifdef CONFIG_IRDA_DYNAMIC_WINDOW
 996			struct sk_buff *skb_next;
 997
 998			/* With DYNAMIC_WINDOW, we keep the window size
 999			 * maximum, and adapt on the packets we are sending.
1000			 * At 115k, we can send only 2 packets of 2048 bytes
1001			 * in a 500 ms turnaround. Without this option, we
1002			 * would always limit the window to 2. With this
1003			 * option, if we send smaller packets, we can send
1004			 * up to 7 of them (always depending on QoS).
1005			 * Jean II */
1006
1007			/* Look at the next skb. This is safe, as we are
1008			 * the only consumer of the Tx queue (if we are not,
1009			 * we have other problems) - Jean II */
1010			skb_next = skb_peek(&self->txq);
1011
1012			/* Check if a subsequent skb exist and would fit in
1013			 * the current window (with respect to turnaround
1014			 * time).
1015			 * This allow us to properly mark the current packet
1016			 * with the pf bit, to avoid falling back on the
1017			 * second test below, and avoid waiting the
1018			 * end of the window and sending a extra RR.
1019			 * Note : (skb_next != NULL) <=> (skb_queue_len() > 0)
1020			 * Jean II */
1021			nextfit = ((skb_next != NULL) &&
1022				   ((skb_next->len + skb->len) <=
1023				    self->bytes_left));
1024
1025			/*
1026			 * The current packet may not fit ! Because of test
1027			 * above, this should not happen any more !!!
1028			 *  Test if we have transmitted more bytes over the
1029			 *  link than its possible to do with the current
1030			 *  speed and turn-around-time.
1031			 */
1032			if((!nextfit) && (skb->len > self->bytes_left)) {
1033				IRDA_DEBUG(0, "%s(), Not allowed to transmit"
1034					   " more bytes!\n", __func__);
1035				/* Requeue the skb */
1036				skb_queue_head(&self->txq, skb_get(skb));
1037				/*
1038				 *  We should switch state to LAP_NRM_P, but
1039				 *  that is not possible since we must be sure
1040				 *  that we poll the other side. Since we have
1041				 *  used up our time, the poll timer should
1042				 *  trigger anyway now, so we just wait for it
1043				 *  DB
1044				 */
1045				/*
1046				 * Sorry, but that's not totally true. If
1047				 * we send 2000B packets, we may wait another
1048				 * 1000B until our turnaround expire. That's
1049				 * why we need to be proactive in avoiding
1050				 * coming here. - Jean II
1051				 */
1052				return -EPROTO;
1053			}
1054
1055			/* Subtract space used by this skb */
1056			self->bytes_left -= skb->len;
1057#else	/* CONFIG_IRDA_DYNAMIC_WINDOW */
1058			/* Window has been adjusted for the max packet
1059			 * size, so much simpler... - Jean II */
1060			nextfit = !skb_queue_empty(&self->txq);
1061#endif	/* CONFIG_IRDA_DYNAMIC_WINDOW */
1062			/*
1063			 *  Send data with poll bit cleared only if window > 1
1064			 *  and there is more frames after this one to be sent
1065			 */
1066			if ((self->window > 1) && (nextfit)) {
1067				/* More packet to send in current window */
1068				irlap_send_data_primary(self, skb);
1069				irlap_next_state(self, LAP_XMIT_P);
1070			} else {
1071				/* Final packet of window */
1072				irlap_send_data_primary_poll(self, skb);
1073
1074				/*
1075				 * Make sure state machine does not try to send
1076				 * any more frames
1077				 */
1078				ret = -EPROTO;
1079			}
1080#ifdef CONFIG_IRDA_FAST_RR
1081			/* Peer may want to reply immediately */
1082			self->fast_RR = FALSE;
1083#endif /* CONFIG_IRDA_FAST_RR */
1084		} else {
1085			IRDA_DEBUG(4, "%s(), Unable to send! remote busy?\n",
1086				   __func__);
1087			skb_queue_head(&self->txq, skb_get(skb));
1088
1089			/*
1090			 *  The next ret is important, because it tells
1091			 *  irlap_next_state _not_ to deliver more frames
1092			 */
1093			ret = -EPROTO;
1094		}
1095		break;
1096	case POLL_TIMER_EXPIRED:
1097		IRDA_DEBUG(3, "%s(), POLL_TIMER_EXPIRED <%ld>\n",
1098			    __func__, jiffies);
1099		irlap_send_rr_frame(self, CMD_FRAME);
1100		/* Return to NRM properly - Jean II  */
1101		self->window = self->window_size;
1102#ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1103		/* Allowed to transmit a maximum number of bytes again. */
1104		self->bytes_left = self->line_capacity;
1105#endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1106		irlap_start_final_timer(self, self->final_timeout);
1107		irlap_next_state(self, LAP_NRM_P);
1108		break;
1109	case DISCONNECT_REQUEST:
1110		del_timer(&self->poll_timer);
1111		irlap_wait_min_turn_around(self, &self->qos_tx);
1112		irlap_send_disc_frame(self);
1113		irlap_flush_all_queues(self);
1114		irlap_start_final_timer(self, self->final_timeout);
1115		self->retry_count = 0;
1116		irlap_next_state(self, LAP_PCLOSE);
1117		break;
1118	case DATA_REQUEST:
1119		/* Nothing to do, irlap_do_event() will send the packet
1120		 * when we return... - Jean II */
1121		break;
1122	default:
1123		IRDA_DEBUG(0, "%s(), Unknown event %s\n",
1124			   __func__, irlap_event[event]);
1125
1126		ret = -EINVAL;
1127		break;
1128	}
1129	return ret;
1130}
1131
1132/*
1133 * Function irlap_state_pclose (event, skb, info)
1134 *
1135 *    PCLOSE state
1136 */
1137static int irlap_state_pclose(struct irlap_cb *self, IRLAP_EVENT event,
1138			      struct sk_buff *skb, struct irlap_info *info)
1139{
1140	int ret = 0;
1141
1142	IRDA_DEBUG(1, "%s()\n", __func__);
1143
1144	IRDA_ASSERT(self != NULL, return -1;);
1145	IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
1146
1147	switch (event) {
1148	case RECV_UA_RSP: /* FALLTHROUGH */
1149	case RECV_DM_RSP:
1150		del_timer(&self->final_timer);
1151
1152		/* Set new link parameters */
1153		irlap_apply_default_connection_parameters(self);
1154
1155		/* Always switch state before calling upper layers */
1156		irlap_next_state(self, LAP_NDM);
1157
1158		irlap_disconnect_indication(self, LAP_DISC_INDICATION);
1159		break;
1160	case FINAL_TIMER_EXPIRED:
1161		if (self->retry_count < self->N3) {
1162			irlap_wait_min_turn_around(self, &self->qos_tx);
1163			irlap_send_disc_frame(self);
1164			irlap_start_final_timer(self, self->final_timeout);
1165			self->retry_count++;
1166			/* Keep state */
1167		} else {
1168			irlap_apply_default_connection_parameters(self);
1169
1170			/*  Always switch state before calling upper layers */
1171			irlap_next_state(self, LAP_NDM);
1172
1173			irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1174		}
1175		break;
1176	default:
1177		IRDA_DEBUG(1, "%s(), Unknown event %d\n", __func__, event);
1178
1179		ret = -1;
1180		break;
1181	}
1182	return ret;
1183}
1184
1185/*
1186 * Function irlap_state_nrm_p (self, event, skb, info)
1187 *
1188 *   NRM_P (Normal Response Mode as Primary), The primary station has given
1189 *   permissions to a secondary station to transmit IrLAP resonse frames
1190 *   (by sending a frame with the P bit set). The primary station will not
1191 *   transmit any frames and is expecting to receive frames only from the
1192 *   secondary to which transmission permissions has been given.
1193 */
1194static int irlap_state_nrm_p(struct irlap_cb *self, IRLAP_EVENT event,
1195			     struct sk_buff *skb, struct irlap_info *info)
1196{
1197	int ret = 0;
1198	int ns_status;
1199	int nr_status;
1200
1201	switch (event) {
1202	case RECV_I_RSP: /* Optimize for the common case */
1203		if (unlikely(skb->len <= LAP_ADDR_HEADER + LAP_CTRL_HEADER)) {
1204			/*
1205			 * Input validation check: a stir4200/mcp2150
1206			 * combination sometimes results in an empty i:rsp.
1207			 * This makes no sense; we can just ignore the frame
1208			 * and send an rr:cmd immediately. This happens before
1209			 * changing nr or ns so triggers a retransmit
1210			 */
1211			irlap_wait_min_turn_around(self, &self->qos_tx);
1212			irlap_send_rr_frame(self, CMD_FRAME);
1213			/* Keep state */
1214			break;
1215		}
1216		/* FIXME: must check for remote_busy below */
1217#ifdef CONFIG_IRDA_FAST_RR
1218		/*
1219		 *  Reset the fast_RR so we can use the fast RR code with
1220		 *  full speed the next time since peer may have more frames
1221		 *  to transmitt
1222		 */
1223		self->fast_RR = FALSE;
1224#endif /* CONFIG_IRDA_FAST_RR */
1225		IRDA_ASSERT( info != NULL, return -1;);
1226
1227		ns_status = irlap_validate_ns_received(self, info->ns);
1228		nr_status = irlap_validate_nr_received(self, info->nr);
1229
1230		/*
1231		 *  Check for expected I(nformation) frame
1232		 */
1233		if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
1234
1235			/* Update Vr (next frame for us to receive) */
1236			self->vr = (self->vr + 1) % 8;
1237
1238			/* Update Nr received, cleanup our retry queue */
1239			irlap_update_nr_received(self, info->nr);
1240
1241			/*
1242			 *  Got expected NR, so reset the
1243			 *  retry_count. This is not done by IrLAP spec,
1244			 *  which is strange!
1245			 */
1246			self->retry_count = 0;
1247			self->ack_required = TRUE;
1248
1249			/*  poll bit cleared?  */
1250			if (!info->pf) {
1251				/* Keep state, do not move this line */
1252				irlap_next_state(self, LAP_NRM_P);
1253
1254				irlap_data_indication(self, skb, FALSE);
1255			} else {
1256				/* No longer waiting for pf */
1257				del_timer(&self->final_timer);
1258
1259				irlap_wait_min_turn_around(self, &self->qos_tx);
1260
1261				/* Call higher layer *before* changing state
1262				 * to give them a chance to send data in the
1263				 * next LAP frame.
1264				 * Jean II */
1265				irlap_data_indication(self, skb, FALSE);
1266
1267				/* XMIT states are the most dangerous state
1268				 * to be in, because user requests are
1269				 * processed directly and may change state.
1270				 * On the other hand, in NDM_P, those
1271				 * requests are queued and we will process
1272				 * them when we return to irlap_do_event().
1273				 * Jean II
1274				 */
1275				irlap_next_state(self, LAP_XMIT_P);
1276
1277				/* This is the last frame.
1278				 * Make sure it's always called in XMIT state.
1279				 * - Jean II */
1280				irlap_start_poll_timer(self, self->poll_timeout);
1281			}
1282			break;
1283
1284		}
1285		/* Unexpected next to send (Ns) */
1286		if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
1287		{
1288			if (!info->pf) {
1289				irlap_update_nr_received(self, info->nr);
1290
1291				/*
1292				 *  Wait until the last frame before doing
1293				 *  anything
1294				 */
1295
1296				/* Keep state */
1297				irlap_next_state(self, LAP_NRM_P);
1298			} else {
1299				IRDA_DEBUG(4,
1300				       "%s(), missing or duplicate frame!\n",
1301					   __func__);
1302
1303				/* Update Nr received */
1304				irlap_update_nr_received(self, info->nr);
1305
1306				irlap_wait_min_turn_around(self, &self->qos_tx);
1307				irlap_send_rr_frame(self, CMD_FRAME);
1308
1309				self->ack_required = FALSE;
1310
1311				irlap_start_final_timer(self, self->final_timeout);
1312				irlap_next_state(self, LAP_NRM_P);
1313			}
1314			break;
1315		}
1316		/*
1317		 *  Unexpected next to receive (Nr)
1318		 */
1319		if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
1320		{
1321			if (info->pf) {
1322				self->vr = (self->vr + 1) % 8;
1323
1324				/* Update Nr received */
1325				irlap_update_nr_received(self, info->nr);
1326
1327				/* Resend rejected frames */
1328				irlap_resend_rejected_frames(self, CMD_FRAME);
1329
1330				self->ack_required = FALSE;
1331
1332				/* Make sure we account for the time
1333				 * to transmit our frames. See comemnts
1334				 * in irlap_send_data_primary_poll().
1335				 * Jean II */
1336				irlap_start_final_timer(self, 2 * self->final_timeout);
1337
1338				/* Keep state, do not move this line */
1339				irlap_next_state(self, LAP_NRM_P);
1340
1341				irlap_data_indication(self, skb, FALSE);
1342			} else {
1343				/*
1344				 *  Do not resend frames until the last
1345				 *  frame has arrived from the other
1346				 *  device. This is not documented in
1347				 *  IrLAP!!
1348				 */
1349				self->vr = (self->vr + 1) % 8;
1350
1351				/* Update Nr received */
1352				irlap_update_nr_received(self, info->nr);
1353
1354				self->ack_required = FALSE;
1355
1356				/* Keep state, do not move this line!*/
1357				irlap_next_state(self, LAP_NRM_P);
1358
1359				irlap_data_indication(self, skb, FALSE);
1360			}
1361			break;
1362		}
1363		/*
1364		 *  Unexpected next to send (Ns) and next to receive (Nr)
1365		 *  Not documented by IrLAP!
1366		 */
1367		if ((ns_status == NS_UNEXPECTED) &&
1368		    (nr_status == NR_UNEXPECTED))
1369		{
1370			IRDA_DEBUG(4, "%s(), unexpected nr and ns!\n",
1371				   __func__);
1372			if (info->pf) {
1373				/* Resend rejected frames */
1374				irlap_resend_rejected_frames(self, CMD_FRAME);
1375
1376				/* Give peer some time to retransmit!
1377				 * But account for our own Tx. */
1378				irlap_start_final_timer(self, 2 * self->final_timeout);
1379
1380				/* Keep state, do not move this line */
1381				irlap_next_state(self, LAP_NRM_P);
1382			} else {
1383				/* Update Nr received */
1384				/* irlap_update_nr_received( info->nr); */
1385
1386				self->ack_required = FALSE;
1387			}
1388			break;
1389		}
1390
1391		/*
1392		 *  Invalid NR or NS
1393		 */
1394		if ((nr_status == NR_INVALID) || (ns_status == NS_INVALID)) {
1395			if (info->pf) {
1396				del_timer(&self->final_timer);
1397
1398				irlap_next_state(self, LAP_RESET_WAIT);
1399
1400				irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1401				self->xmitflag = TRUE;
1402			} else {
1403				del_timer(&self->final_timer);
1404
1405				irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1406
1407				self->xmitflag = FALSE;
1408			}
1409			break;
1410		}
1411		IRDA_DEBUG(1, "%s(), Not implemented!\n", __func__);
1412		IRDA_DEBUG(1, "%s(), event=%s, ns_status=%d, nr_status=%d\n",
1413		       __func__, irlap_event[event], ns_status, nr_status);
1414		break;
1415	case RECV_UI_FRAME:
1416		/* Poll bit cleared? */
1417		if (!info->pf) {
1418			irlap_data_indication(self, skb, TRUE);
1419			irlap_next_state(self, LAP_NRM_P);
1420		} else {
1421			del_timer(&self->final_timer);
1422			irlap_data_indication(self, skb, TRUE);
1423			irlap_next_state(self, LAP_XMIT_P);
1424			IRDA_DEBUG(1, "%s: RECV_UI_FRAME: next state %s\n", __func__, irlap_state[self->state]);
 
1425			irlap_start_poll_timer(self, self->poll_timeout);
1426		}
1427		break;
1428	case RECV_RR_RSP:
1429		/*
1430		 *  If you get a RR, the remote isn't busy anymore,
1431		 *  no matter what the NR
1432		 */
1433		self->remote_busy = FALSE;
1434
1435		/* Stop final timer */
1436		del_timer(&self->final_timer);
1437
1438		/*
1439		 *  Nr as expected?
1440		 */
1441		ret = irlap_validate_nr_received(self, info->nr);
1442		if (ret == NR_EXPECTED) {
1443			/* Update Nr received */
1444			irlap_update_nr_received(self, info->nr);
1445
1446			/*
1447			 *  Got expected NR, so reset the retry_count. This
1448			 *  is not done by the IrLAP standard , which is
1449			 *  strange! DB.
1450			 */
1451			self->retry_count = 0;
1452			irlap_wait_min_turn_around(self, &self->qos_tx);
1453
1454			irlap_next_state(self, LAP_XMIT_P);
1455
1456			/* Start poll timer */
1457			irlap_start_poll_timer(self, self->poll_timeout);
1458		} else if (ret == NR_UNEXPECTED) {
1459			IRDA_ASSERT(info != NULL, return -1;);
1460			/*
1461			 *  Unexpected nr!
1462			 */
1463
1464			/* Update Nr received */
1465			irlap_update_nr_received(self, info->nr);
1466
1467			IRDA_DEBUG(4, "RECV_RR_FRAME: Retrans:%d, nr=%d, va=%d, "
1468			      "vs=%d, vr=%d\n",
1469			      self->retry_count, info->nr, self->va,
1470			      self->vs, self->vr);
1471
1472			/* Resend rejected frames */
1473			irlap_resend_rejected_frames(self, CMD_FRAME);
1474			irlap_start_final_timer(self, self->final_timeout * 2);
1475
1476			irlap_next_state(self, LAP_NRM_P);
1477		} else if (ret == NR_INVALID) {
1478			IRDA_DEBUG(1, "%s(), Received RR with "
1479				   "invalid nr !\n", __func__);
1480
1481			irlap_next_state(self, LAP_RESET_WAIT);
1482
1483			irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1484			self->xmitflag = TRUE;
1485		}
1486		break;
1487	case RECV_RNR_RSP:
1488		IRDA_ASSERT(info != NULL, return -1;);
1489
1490		/* Stop final timer */
1491		del_timer(&self->final_timer);
1492		self->remote_busy = TRUE;
1493
1494		/* Update Nr received */
1495		irlap_update_nr_received(self, info->nr);
1496		irlap_next_state(self, LAP_XMIT_P);
1497
1498		/* Start poll timer */
1499		irlap_start_poll_timer(self, self->poll_timeout);
1500		break;
1501	case RECV_FRMR_RSP:
1502		del_timer(&self->final_timer);
1503		self->xmitflag = TRUE;
1504		irlap_next_state(self, LAP_RESET_WAIT);
1505		irlap_reset_indication(self);
1506		break;
1507	case FINAL_TIMER_EXPIRED:
1508		/*
1509		 *  We are allowed to wait for additional 300 ms if
1510		 *  final timer expires when we are in the middle
1511		 *  of receiving a frame (page 45, IrLAP). Check that
1512		 *  we only do this once for each frame.
1513		 */
1514		if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
1515			IRDA_DEBUG(1, "FINAL_TIMER_EXPIRED when receiving a "
1516			      "frame! Waiting a little bit more!\n");
1517			irlap_start_final_timer(self, msecs_to_jiffies(300));
1518
1519			/*
1520			 *  Don't allow this to happen one more time in a row,
1521			 *  or else we can get a pretty tight loop here if
1522			 *  if we only receive half a frame. DB.
1523			 */
1524			self->add_wait = TRUE;
1525			break;
1526		}
1527		self->add_wait = FALSE;
1528
1529		/* N2 is the disconnect timer. Until we reach it, we retry */
1530		if (self->retry_count < self->N2) {
1531			if (skb_peek(&self->wx_list) == NULL) {
1532				/* Retry sending the pf bit to the secondary */
1533				IRDA_DEBUG(4, "nrm_p: resending rr");
1534				irlap_wait_min_turn_around(self, &self->qos_tx);
1535				irlap_send_rr_frame(self, CMD_FRAME);
1536			} else {
1537				IRDA_DEBUG(4, "nrm_p: resend frames");
1538				irlap_resend_rejected_frames(self, CMD_FRAME);
1539			}
1540
1541			irlap_start_final_timer(self, self->final_timeout);
1542			self->retry_count++;
1543			IRDA_DEBUG(4, "irlap_state_nrm_p: FINAL_TIMER_EXPIRED:"
1544				   " retry_count=%d\n", self->retry_count);
1545
1546			/* Early warning event. I'm using a pretty liberal
1547			 * interpretation of the spec and generate an event
1548			 * every time the timer is multiple of N1 (and not
1549			 * only the first time). This allow application
1550			 * to know precisely if connectivity restart...
1551			 * Jean II */
1552			if((self->retry_count % self->N1) == 0)
1553				irlap_status_indication(self,
1554							STATUS_NO_ACTIVITY);
1555
1556			/* Keep state */
1557		} else {
1558			irlap_apply_default_connection_parameters(self);
1559
1560			/* Always switch state before calling upper layers */
1561			irlap_next_state(self, LAP_NDM);
1562			irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1563		}
1564		break;
1565	case RECV_REJ_RSP:
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);
1570		} else
1571			irlap_resend_rejected_frames(self, CMD_FRAME);
1572		irlap_start_final_timer(self, 2 * self->final_timeout);
1573		break;
1574	case RECV_SREJ_RSP:
1575		irlap_update_nr_received(self, info->nr);
1576		if (self->remote_busy) {
1577			irlap_wait_min_turn_around(self, &self->qos_tx);
1578			irlap_send_rr_frame(self, CMD_FRAME);
1579		} else
1580			irlap_resend_rejected_frame(self, CMD_FRAME);
1581		irlap_start_final_timer(self, 2 * self->final_timeout);
1582		break;
1583	case RECV_RD_RSP:
1584		IRDA_DEBUG(1, "%s(), RECV_RD_RSP\n", __func__);
1585
1586		irlap_flush_all_queues(self);
1587		irlap_next_state(self, LAP_XMIT_P);
1588		/* Call back the LAP state machine to do a proper disconnect */
1589		irlap_disconnect_request(self);
1590		break;
1591	default:
1592		IRDA_DEBUG(1, "%s(), Unknown event %s\n",
1593			    __func__, irlap_event[event]);
1594
1595		ret = -1;
1596		break;
1597	}
1598	return ret;
1599}
1600
1601/*
1602 * Function irlap_state_reset_wait (event, skb, info)
1603 *
1604 *    We have informed the service user of a reset condition, and is
1605 *    awaiting reset of disconnect request.
1606 *
1607 */
1608static int irlap_state_reset_wait(struct irlap_cb *self, IRLAP_EVENT event,
1609				  struct sk_buff *skb, struct irlap_info *info)
1610{
1611	int ret = 0;
1612
1613	IRDA_DEBUG(3, "%s(), event = %s\n", __func__, irlap_event[event]);
1614
1615	IRDA_ASSERT(self != NULL, return -1;);
1616	IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
1617
1618	switch (event) {
1619	case RESET_REQUEST:
1620		if (self->xmitflag) {
1621			irlap_wait_min_turn_around(self, &self->qos_tx);
1622			irlap_send_snrm_frame(self, NULL);
1623			irlap_start_final_timer(self, self->final_timeout);
1624			irlap_next_state(self, LAP_RESET);
1625		} else {
1626			irlap_start_final_timer(self, self->final_timeout);
1627			irlap_next_state(self, LAP_RESET);
1628		}
1629		break;
1630	case DISCONNECT_REQUEST:
1631		irlap_wait_min_turn_around( self, &self->qos_tx);
1632		irlap_send_disc_frame( self);
1633		irlap_flush_all_queues( self);
1634		irlap_start_final_timer( self, self->final_timeout);
1635		self->retry_count = 0;
1636		irlap_next_state( self, LAP_PCLOSE);
1637		break;
1638	default:
1639		IRDA_DEBUG(2, "%s(), Unknown event %s\n", __func__,
1640			   irlap_event[event]);
1641
1642		ret = -1;
1643		break;
1644	}
1645	return ret;
1646}
1647
1648/*
1649 * Function irlap_state_reset (self, event, skb, info)
1650 *
1651 *    We have sent a SNRM reset command to the peer layer, and is awaiting
1652 *    reply.
1653 *
1654 */
1655static int irlap_state_reset(struct irlap_cb *self, IRLAP_EVENT event,
1656			     struct sk_buff *skb, struct irlap_info *info)
1657{
1658	int ret = 0;
1659
1660	IRDA_DEBUG(3, "%s(), event = %s\n", __func__, irlap_event[event]);
1661
1662	IRDA_ASSERT(self != NULL, return -1;);
1663	IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
1664
1665	switch (event) {
1666	case RECV_DISC_CMD:
1667		del_timer(&self->final_timer);
1668
1669		irlap_apply_default_connection_parameters(self);
1670
1671		/* Always switch state before calling upper layers */
1672		irlap_next_state(self, LAP_NDM);
1673
1674		irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1675
1676		break;
1677	case RECV_UA_RSP:
1678		del_timer(&self->final_timer);
1679
1680		/* Initiate connection state */
1681		irlap_initiate_connection_state(self);
1682
1683		irlap_reset_confirm();
1684
1685		self->remote_busy = FALSE;
1686
1687		irlap_next_state(self, LAP_XMIT_P);
1688
1689		irlap_start_poll_timer(self, self->poll_timeout);
1690
1691		break;
1692	case FINAL_TIMER_EXPIRED:
1693		if (self->retry_count < 3) {
1694			irlap_wait_min_turn_around(self, &self->qos_tx);
1695
1696			IRDA_ASSERT(self->netdev != NULL, return -1;);
1697			irlap_send_snrm_frame(self, self->qos_dev);
1698
1699			self->retry_count++; /* Experimental!! */
1700
1701			irlap_start_final_timer(self, self->final_timeout);
1702			irlap_next_state(self, LAP_RESET);
1703		} else if (self->retry_count >= self->N3) {
1704			irlap_apply_default_connection_parameters(self);
1705
1706			/* Always switch state before calling upper layers */
1707			irlap_next_state(self, LAP_NDM);
1708
1709			irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1710		}
1711		break;
1712	case RECV_SNRM_CMD:
1713		/*
1714		 * SNRM frame is not allowed to contain an I-field in this
1715		 * state
1716		 */
1717		if (!info) {
1718			IRDA_DEBUG(3, "%s(), RECV_SNRM_CMD\n", __func__);
1719			irlap_initiate_connection_state(self);
1720			irlap_wait_min_turn_around(self, &self->qos_tx);
1721			irlap_send_ua_response_frame(self, &self->qos_rx);
1722			irlap_reset_confirm();
1723			irlap_start_wd_timer(self, self->wd_timeout);
1724			irlap_next_state(self, LAP_NDM);
1725		} else {
1726			IRDA_DEBUG(0,
1727				   "%s(), SNRM frame contained an I field!\n",
1728				   __func__);
1729		}
1730		break;
1731	default:
1732		IRDA_DEBUG(1, "%s(), Unknown event %s\n",
1733			   __func__, irlap_event[event]);
1734
1735		ret = -1;
1736		break;
1737	}
1738	return ret;
1739}
1740
1741/*
1742 * Function irlap_state_xmit_s (event, skb, info)
1743 *
1744 *   XMIT_S, The secondary station has been given the right to transmit,
1745 *   and we therefore do not expect to receive any transmissions from other
1746 *   stations.
1747 */
1748static int irlap_state_xmit_s(struct irlap_cb *self, IRLAP_EVENT event,
1749			      struct sk_buff *skb, struct irlap_info *info)
1750{
1751	int ret = 0;
1752
1753	IRDA_DEBUG(4, "%s(), event=%s\n", __func__, irlap_event[event]);
1754
1755	IRDA_ASSERT(self != NULL, return -ENODEV;);
1756	IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
1757
1758	switch (event) {
1759	case SEND_I_CMD:
1760		/*
1761		 *  Send frame only if send window > 0
1762		 */
1763		if ((self->window > 0) && (!self->remote_busy)) {
1764			int nextfit;
1765#ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1766			struct sk_buff *skb_next;
1767
1768			/*
1769			 * Same deal as in irlap_state_xmit_p(), so see
1770			 * the comments at that point.
1771			 * We are the secondary, so there are only subtle
1772			 * differences. - Jean II
1773			 */
1774
1775			/* Check if a subsequent skb exist and would fit in
1776			 * the current window (with respect to turnaround
1777			 * time). - Jean II */
1778			skb_next = skb_peek(&self->txq);
1779			nextfit = ((skb_next != NULL) &&
1780				   ((skb_next->len + skb->len) <=
1781				    self->bytes_left));
1782
1783			/*
1784			 *  Test if we have transmitted more bytes over the
1785			 *  link than its possible to do with the current
1786			 *  speed and turn-around-time.
1787			 */
1788			if((!nextfit) && (skb->len > self->bytes_left)) {
1789				IRDA_DEBUG(0, "%s(), Not allowed to transmit"
1790					   " more bytes!\n", __func__);
1791				/* Requeue the skb */
1792				skb_queue_head(&self->txq, skb_get(skb));
1793
1794				/*
1795				 *  Switch to NRM_S, this is only possible
1796				 *  when we are in secondary mode, since we
1797				 *  must be sure that we don't miss any RR
1798				 *  frames
1799				 */
1800				self->window = self->window_size;
1801				self->bytes_left = self->line_capacity;
1802				irlap_start_wd_timer(self, self->wd_timeout);
1803
1804				irlap_next_state(self, LAP_NRM_S);
1805				/* Slight difference with primary :
1806				 * here we would wait for the other side to
1807				 * expire the turnaround. - Jean II */
1808
1809				return -EPROTO; /* Try again later */
1810			}
1811			/* Subtract space used by this skb */
1812			self->bytes_left -= skb->len;
1813#else	/* CONFIG_IRDA_DYNAMIC_WINDOW */
1814			/* Window has been adjusted for the max packet
1815			 * size, so much simpler... - Jean II */
1816			nextfit = !skb_queue_empty(&self->txq);
1817#endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1818			/*
1819			 *  Send data with final bit cleared only if window > 1
1820			 *  and there is more frames to be sent
1821			 */
1822			if ((self->window > 1) && (nextfit)) {
1823				irlap_send_data_secondary(self, skb);
1824				irlap_next_state(self, LAP_XMIT_S);
1825			} else {
1826				irlap_send_data_secondary_final(self, skb);
1827				irlap_next_state(self, LAP_NRM_S);
1828
1829				/*
1830				 * Make sure state machine does not try to send
1831				 * any more frames
1832				 */
1833				ret = -EPROTO;
1834			}
1835		} else {
1836			IRDA_DEBUG(2, "%s(), Unable to send!\n", __func__);
1837			skb_queue_head(&self->txq, skb_get(skb));
1838			ret = -EPROTO;
1839		}
1840		break;
1841	case DISCONNECT_REQUEST:
1842		irlap_send_rd_frame(self);
1843		irlap_flush_all_queues(self);
1844		irlap_start_wd_timer(self, self->wd_timeout);
1845		irlap_next_state(self, LAP_SCLOSE);
1846		break;
1847	case DATA_REQUEST:
1848		/* Nothing to do, irlap_do_event() will send the packet
1849		 * when we return... - Jean II */
1850		break;
1851	default:
1852		IRDA_DEBUG(2, "%s(), Unknown event %s\n", __func__,
1853			   irlap_event[event]);
1854
1855		ret = -EINVAL;
1856		break;
1857	}
1858	return ret;
1859}
1860
1861/*
1862 * Function irlap_state_nrm_s (event, skb, info)
1863 *
1864 *    NRM_S (Normal Response Mode as Secondary) state, in this state we are
1865 *    expecting to receive frames from the primary station
1866 *
1867 */
1868static int irlap_state_nrm_s(struct irlap_cb *self, IRLAP_EVENT event,
1869			     struct sk_buff *skb, struct irlap_info *info)
1870{
1871	int ns_status;
1872	int nr_status;
1873	int ret = 0;
1874
1875	IRDA_DEBUG(4, "%s(), event=%s\n", __func__, irlap_event[ event]);
1876
1877	IRDA_ASSERT(self != NULL, return -1;);
1878	IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
1879
1880	switch (event) {
1881	case RECV_I_CMD: /* Optimize for the common case */
1882		/* FIXME: must check for remote_busy below */
1883		IRDA_DEBUG(4, "%s(), event=%s nr=%d, vs=%d, ns=%d, "
1884			   "vr=%d, pf=%d\n", __func__,
1885			   irlap_event[event], info->nr,
1886			   self->vs, info->ns, self->vr, info->pf);
1887
1888		self->retry_count = 0;
1889
1890		ns_status = irlap_validate_ns_received(self, info->ns);
1891		nr_status = irlap_validate_nr_received(self, info->nr);
1892		/*
1893		 *  Check for expected I(nformation) frame
1894		 */
1895		if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
1896
1897			/* Update Vr (next frame for us to receive) */
1898			self->vr = (self->vr + 1) % 8;
1899
1900			/* Update Nr received */
1901			irlap_update_nr_received(self, info->nr);
1902
1903			/*
1904			 *  poll bit cleared?
1905			 */
1906			if (!info->pf) {
1907
1908				self->ack_required = TRUE;
1909
1910				/*
1911				 *  Starting WD-timer here is optional, but
1912				 *  not recommended. Note 6 IrLAP p. 83
1913				 */
1914#if 0
1915				irda_start_timer(WD_TIMER, self->wd_timeout);
1916#endif
1917				/* Keep state, do not move this line */
1918				irlap_next_state(self, LAP_NRM_S);
1919
1920				irlap_data_indication(self, skb, FALSE);
1921				break;
1922			} else {
1923				/*
1924				 *  We should wait before sending RR, and
1925				 *  also before changing to XMIT_S
1926				 *  state. (note 1, IrLAP p. 82)
1927				 */
1928				irlap_wait_min_turn_around(self, &self->qos_tx);
1929
1930				/*
1931				 * Give higher layers a chance to
1932				 * immediately reply with some data before
1933				 * we decide if we should send a RR frame
1934				 * or not
1935				 */
1936				irlap_data_indication(self, skb, FALSE);
1937
1938				/* Any pending data requests?  */
1939				if (!skb_queue_empty(&self->txq) &&
1940				    (self->window > 0))
1941				{
1942					self->ack_required = TRUE;
1943
1944					del_timer(&self->wd_timer);
1945
1946					irlap_next_state(self, LAP_XMIT_S);
1947				} else {
1948					irlap_send_rr_frame(self, RSP_FRAME);
1949					irlap_start_wd_timer(self,
1950							     self->wd_timeout);
1951
1952					/* Keep the state */
1953					irlap_next_state(self, LAP_NRM_S);
1954				}
1955				break;
1956			}
1957		}
1958		/*
1959		 *  Check for Unexpected next to send (Ns)
1960		 */
1961		if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
1962		{
1963			/* Unexpected next to send, with final bit cleared */
1964			if (!info->pf) {
1965				irlap_update_nr_received(self, info->nr);
1966
1967				irlap_start_wd_timer(self, self->wd_timeout);
1968			} else {
1969				/* Update Nr received */
1970				irlap_update_nr_received(self, info->nr);
1971
1972				irlap_wait_min_turn_around(self, &self->qos_tx);
1973				irlap_send_rr_frame(self, RSP_FRAME);
1974
1975				irlap_start_wd_timer(self, self->wd_timeout);
1976			}
1977			break;
1978		}
1979
1980		/*
1981		 *  Unexpected Next to Receive(NR) ?
1982		 */
1983		if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
1984		{
1985			if (info->pf) {
1986				IRDA_DEBUG(4, "RECV_I_RSP: frame(s) lost\n");
1987
1988				self->vr = (self->vr + 1) % 8;
1989
1990				/* Update Nr received */
1991				irlap_update_nr_received(self, info->nr);
1992
1993				/* Resend rejected frames */
1994				irlap_resend_rejected_frames(self, RSP_FRAME);
1995
1996				/* Keep state, do not move this line */
1997				irlap_next_state(self, LAP_NRM_S);
1998
1999				irlap_data_indication(self, skb, FALSE);
2000				irlap_start_wd_timer(self, self->wd_timeout);
2001				break;
2002			}
2003			/*
2004			 *  This is not documented in IrLAP!! Unexpected NR
2005			 *  with poll bit cleared
2006			 */
2007			if (!info->pf) {
2008				self->vr = (self->vr + 1) % 8;
2009
2010				/* Update Nr received */
2011				irlap_update_nr_received(self, info->nr);
2012
2013				/* Keep state, do not move this line */
2014				irlap_next_state(self, LAP_NRM_S);
2015
2016				irlap_data_indication(self, skb, FALSE);
2017				irlap_start_wd_timer(self, self->wd_timeout);
2018			}
2019			break;
2020		}
2021
2022		if (ret == NR_INVALID) {
2023			IRDA_DEBUG(0, "NRM_S, NR_INVALID not implemented!\n");
2024		}
2025		if (ret == NS_INVALID) {
2026			IRDA_DEBUG(0, "NRM_S, NS_INVALID not implemented!\n");
2027		}
2028		break;
2029	case RECV_UI_FRAME:
2030		/*
2031		 *  poll bit cleared?
2032		 */
2033		if (!info->pf) {
2034			irlap_data_indication(self, skb, TRUE);
2035			irlap_next_state(self, LAP_NRM_S); /* Keep state */
2036		} else {
2037			/*
2038			 *  Any pending data requests?
2039			 */
2040			if (!skb_queue_empty(&self->txq) &&
2041			    (self->window > 0) && !self->remote_busy)
2042			{
2043				irlap_data_indication(self, skb, TRUE);
2044
2045				del_timer(&self->wd_timer);
2046
2047				irlap_next_state(self, LAP_XMIT_S);
2048			} else {
2049				irlap_data_indication(self, skb, TRUE);
2050
2051				irlap_wait_min_turn_around(self, &self->qos_tx);
2052
2053				irlap_send_rr_frame(self, RSP_FRAME);
2054				self->ack_required = FALSE;
2055
2056				irlap_start_wd_timer(self, self->wd_timeout);
2057
2058				/* Keep the state */
2059				irlap_next_state(self, LAP_NRM_S);
2060			}
2061		}
2062		break;
2063	case RECV_RR_CMD:
2064		self->retry_count = 0;
2065
2066		/*
2067		 *  Nr as expected?
2068		 */
2069		nr_status = irlap_validate_nr_received(self, info->nr);
2070		if (nr_status == NR_EXPECTED) {
2071			if (!skb_queue_empty(&self->txq) &&
2072			    (self->window > 0)) {
2073				self->remote_busy = FALSE;
2074
2075				/* Update Nr received */
2076				irlap_update_nr_received(self, info->nr);
2077				del_timer(&self->wd_timer);
2078
2079				irlap_wait_min_turn_around(self, &self->qos_tx);
2080				irlap_next_state(self, LAP_XMIT_S);
2081			} else {
2082				self->remote_busy = FALSE;
2083				/* Update Nr received */
2084				irlap_update_nr_received(self, info->nr);
2085				irlap_wait_min_turn_around(self, &self->qos_tx);
2086				irlap_start_wd_timer(self, self->wd_timeout);
2087
2088				/* Note : if the link is idle (this case),
2089				 * we never go in XMIT_S, so we never get a
2090				 * chance to process any DISCONNECT_REQUEST.
2091				 * Do it now ! - Jean II */
2092				if (self->disconnect_pending) {
2093					/* Disconnect */
2094					irlap_send_rd_frame(self);
2095					irlap_flush_all_queues(self);
2096
2097					irlap_next_state(self, LAP_SCLOSE);
2098				} else {
2099					/* Just send back pf bit */
2100					irlap_send_rr_frame(self, RSP_FRAME);
2101
2102					irlap_next_state(self, LAP_NRM_S);
2103				}
2104			}
2105		} else if (nr_status == NR_UNEXPECTED) {
2106			self->remote_busy = FALSE;
2107			irlap_update_nr_received(self, info->nr);
2108			irlap_resend_rejected_frames(self, RSP_FRAME);
2109
2110			irlap_start_wd_timer(self, self->wd_timeout);
2111
2112			/* Keep state */
2113			irlap_next_state(self, LAP_NRM_S);
2114		} else {
2115			IRDA_DEBUG(1, "%s(), invalid nr not implemented!\n",
2116				   __func__);
2117		}
2118		break;
2119	case RECV_SNRM_CMD:
2120		/* SNRM frame is not allowed to contain an I-field */
2121		if (!info) {
2122			del_timer(&self->wd_timer);
2123			IRDA_DEBUG(1, "%s(), received SNRM cmd\n", __func__);
2124			irlap_next_state(self, LAP_RESET_CHECK);
2125
2126			irlap_reset_indication(self);
2127		} else {
2128			IRDA_DEBUG(0,
2129				   "%s(), SNRM frame contained an I-field!\n",
2130				   __func__);
2131
2132		}
2133		break;
2134	case RECV_REJ_CMD:
2135		irlap_update_nr_received(self, info->nr);
2136		if (self->remote_busy) {
2137			irlap_wait_min_turn_around(self, &self->qos_tx);
2138			irlap_send_rr_frame(self, RSP_FRAME);
2139		} else
2140			irlap_resend_rejected_frames(self, RSP_FRAME);
2141		irlap_start_wd_timer(self, self->wd_timeout);
2142		break;
2143	case RECV_SREJ_CMD:
2144		irlap_update_nr_received(self, info->nr);
2145		if (self->remote_busy) {
2146			irlap_wait_min_turn_around(self, &self->qos_tx);
2147			irlap_send_rr_frame(self, RSP_FRAME);
2148		} else
2149			irlap_resend_rejected_frame(self, RSP_FRAME);
2150		irlap_start_wd_timer(self, self->wd_timeout);
2151		break;
2152	case WD_TIMER_EXPIRED:
2153		/*
2154		 *  Wait until retry_count * n matches negotiated threshold/
2155		 *  disconnect time (note 2 in IrLAP p. 82)
2156		 *
2157		 * Similar to irlap_state_nrm_p() -> FINAL_TIMER_EXPIRED
2158		 * Note : self->wd_timeout = (self->final_timeout * 2),
2159		 *   which explain why we use (self->N2 / 2) here !!!
2160		 * Jean II
2161		 */
2162		IRDA_DEBUG(1, "%s(), retry_count = %d\n", __func__,
2163			   self->retry_count);
2164
2165		if (self->retry_count < (self->N2 / 2)) {
2166			/* No retry, just wait for primary */
2167			irlap_start_wd_timer(self, self->wd_timeout);
2168			self->retry_count++;
2169
2170			if((self->retry_count % (self->N1 / 2)) == 0)
2171				irlap_status_indication(self,
2172							STATUS_NO_ACTIVITY);
2173		} else {
2174			irlap_apply_default_connection_parameters(self);
2175
2176			/* Always switch state before calling upper layers */
2177			irlap_next_state(self, LAP_NDM);
2178			irlap_disconnect_indication(self, LAP_NO_RESPONSE);
2179		}
2180		break;
2181	case RECV_DISC_CMD:
2182		/* Always switch state before calling upper layers */
2183		irlap_next_state(self, LAP_NDM);
2184
2185		/* Send disconnect response */
2186		irlap_wait_min_turn_around(self, &self->qos_tx);
2187		irlap_send_ua_response_frame(self, NULL);
2188
2189		del_timer(&self->wd_timer);
2190		irlap_flush_all_queues(self);
2191		/* Set default link parameters */
2192		irlap_apply_default_connection_parameters(self);
2193
2194		irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2195		break;
2196	case RECV_DISCOVERY_XID_CMD:
2197		irlap_wait_min_turn_around(self, &self->qos_tx);
2198		irlap_send_rr_frame(self, RSP_FRAME);
2199		self->ack_required = TRUE;
2200		irlap_start_wd_timer(self, self->wd_timeout);
2201		irlap_next_state(self, LAP_NRM_S);
2202
2203		break;
2204	case RECV_TEST_CMD:
2205		/* Remove test frame header (only LAP header in NRM) */
2206		skb_pull(skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
2207
2208		irlap_wait_min_turn_around(self, &self->qos_tx);
2209		irlap_start_wd_timer(self, self->wd_timeout);
2210
2211		/* Send response (info will be copied) */
2212		irlap_send_test_frame(self, self->caddr, info->daddr, skb);
2213		break;
2214	default:
2215		IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __func__,
2216			   event, irlap_event[event]);
2217
2218		ret = -EINVAL;
2219		break;
2220	}
2221	return ret;
2222}
2223
2224/*
2225 * Function irlap_state_sclose (self, event, skb, info)
2226 */
2227static int irlap_state_sclose(struct irlap_cb *self, IRLAP_EVENT event,
2228			      struct sk_buff *skb, struct irlap_info *info)
2229{
2230	IRDA_DEBUG(1, "%s()\n", __func__);
2231
2232	IRDA_ASSERT(self != NULL, return -ENODEV;);
2233	IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
2234
2235	switch (event) {
2236	case RECV_DISC_CMD:
2237		/* Always switch state before calling upper layers */
2238		irlap_next_state(self, LAP_NDM);
2239
2240		/* Send disconnect response */
2241		irlap_wait_min_turn_around(self, &self->qos_tx);
2242		irlap_send_ua_response_frame(self, NULL);
2243
2244		del_timer(&self->wd_timer);
2245		/* Set default link parameters */
2246		irlap_apply_default_connection_parameters(self);
2247
2248		irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2249		break;
2250	case RECV_DM_RSP:
2251		/* IrLAP-1.1 p.82: in SCLOSE, S and I type RSP frames
2252		 * shall take us down into default NDM state, like DM_RSP
2253		 */
2254	case RECV_RR_RSP:
2255	case RECV_RNR_RSP:
2256	case RECV_REJ_RSP:
2257	case RECV_SREJ_RSP:
2258	case RECV_I_RSP:
2259		/* Always switch state before calling upper layers */
2260		irlap_next_state(self, LAP_NDM);
2261
2262		del_timer(&self->wd_timer);
2263		irlap_apply_default_connection_parameters(self);
2264
2265		irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2266		break;
2267	case WD_TIMER_EXPIRED:
2268		/* Always switch state before calling upper layers */
2269		irlap_next_state(self, LAP_NDM);
2270
2271		irlap_apply_default_connection_parameters(self);
2272
2273		irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2274		break;
2275	default:
2276		/* IrLAP-1.1 p.82: in SCLOSE, basically any received frame
2277		 * with pf=1 shall restart the wd-timer and resend the rd:rsp
2278		 */
2279		if (info != NULL  &&  info->pf) {
2280			del_timer(&self->wd_timer);
2281			irlap_wait_min_turn_around(self, &self->qos_tx);
2282			irlap_send_rd_frame(self);
2283			irlap_start_wd_timer(self, self->wd_timeout);
2284			break;		/* stay in SCLOSE */
2285		}
2286
2287		IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __func__,
2288			   event, irlap_event[event]);
2289
2290		break;
2291	}
2292
2293	return -1;
2294}
2295
2296static int irlap_state_reset_check( struct irlap_cb *self, IRLAP_EVENT event,
2297				   struct sk_buff *skb,
2298				   struct irlap_info *info)
2299{
2300	int ret = 0;
2301
2302	IRDA_DEBUG(1, "%s(), event=%s\n", __func__, irlap_event[event]);
2303
2304	IRDA_ASSERT(self != NULL, return -ENODEV;);
2305	IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
2306
2307	switch (event) {
2308	case RESET_RESPONSE:
2309		irlap_send_ua_response_frame(self, &self->qos_rx);
2310		irlap_initiate_connection_state(self);
2311		irlap_start_wd_timer(self, WD_TIMEOUT);
2312		irlap_flush_all_queues(self);
2313
2314		irlap_next_state(self, LAP_NRM_S);
2315		break;
2316	case DISCONNECT_REQUEST:
2317		irlap_wait_min_turn_around(self, &self->qos_tx);
2318		irlap_send_rd_frame(self);
2319		irlap_start_wd_timer(self, WD_TIMEOUT);
2320		irlap_next_state(self, LAP_SCLOSE);
2321		break;
2322	default:
2323		IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __func__,
2324			   event, irlap_event[event]);
2325
2326		ret = -EINVAL;
2327		break;
2328	}
2329	return ret;
2330}
v4.6
   1/*********************************************************************
   2 *
   3 * Filename:      irlap_event.c
   4 * Version:       0.9
   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>
  11 *
  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>
  16 *
  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.
  21 *
  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.
  25 *
  26 ********************************************************************/
  27
  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>
  33
  34#include <net/irda/irda.h>
  35#include <net/irda/irlap_event.h>
  36
  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(), ... */
  43
  44#include <net/irda/irda_device.h>
  45
  46#ifdef CONFIG_IRDA_FAST_RR
  47int sysctl_fast_poll_increase = 50;
  48#endif
  49
  50static int irlap_state_ndm    (struct irlap_cb *self, IRLAP_EVENT event,
  51			       struct sk_buff *skb, struct irlap_info *info);
  52static int irlap_state_query  (struct irlap_cb *self, IRLAP_EVENT event,
  53			       struct sk_buff *skb, struct irlap_info *info);
  54static int irlap_state_reply  (struct irlap_cb *self, IRLAP_EVENT event,
  55			       struct sk_buff *skb, struct irlap_info *info);
  56static int irlap_state_conn   (struct irlap_cb *self, IRLAP_EVENT event,
  57			       struct sk_buff *skb, struct irlap_info *info);
  58static int irlap_state_setup  (struct irlap_cb *self, IRLAP_EVENT event,
  59			       struct sk_buff *skb, struct irlap_info *info);
  60static int irlap_state_offline(struct irlap_cb *self, IRLAP_EVENT event,
  61			       struct sk_buff *skb, struct irlap_info *info);
  62static int irlap_state_xmit_p (struct irlap_cb *self, IRLAP_EVENT event,
  63			       struct sk_buff *skb, struct irlap_info *info);
  64static int irlap_state_pclose (struct irlap_cb *self, IRLAP_EVENT event,
  65			       struct sk_buff *skb, struct irlap_info *info);
  66static int irlap_state_nrm_p  (struct irlap_cb *self, IRLAP_EVENT event,
  67			       struct sk_buff *skb, struct irlap_info *info);
  68static int irlap_state_reset_wait(struct irlap_cb *self, IRLAP_EVENT event,
  69				  struct sk_buff *skb, struct irlap_info *info);
  70static int irlap_state_reset  (struct irlap_cb *self, IRLAP_EVENT event,
  71			       struct sk_buff *skb, struct irlap_info *info);
  72static int irlap_state_nrm_s  (struct irlap_cb *self, IRLAP_EVENT event,
  73			       struct sk_buff *skb, struct irlap_info *info);
  74static int irlap_state_xmit_s (struct irlap_cb *self, IRLAP_EVENT event,
  75			       struct sk_buff *skb, struct irlap_info *info);
  76static int irlap_state_sclose (struct irlap_cb *self, IRLAP_EVENT event,
  77			       struct sk_buff *skb, struct irlap_info *info);
  78static int irlap_state_reset_check(struct irlap_cb *, IRLAP_EVENT event,
  79				   struct sk_buff *, struct irlap_info *);
  80
  81static const char *const irlap_event[] __maybe_unused = {
 
  82	"DISCOVERY_REQUEST",
  83	"CONNECT_REQUEST",
  84	"CONNECT_RESPONSE",
  85	"DISCONNECT_REQUEST",
  86	"DATA_REQUEST",
  87	"RESET_REQUEST",
  88	"RESET_RESPONSE",
  89	"SEND_I_CMD",
  90	"SEND_UI_FRAME",
  91	"RECV_DISCOVERY_XID_CMD",
  92	"RECV_DISCOVERY_XID_RSP",
  93	"RECV_SNRM_CMD",
  94	"RECV_TEST_CMD",
  95	"RECV_TEST_RSP",
  96	"RECV_UA_RSP",
  97	"RECV_DM_RSP",
  98	"RECV_RD_RSP",
  99	"RECV_I_CMD",
 100	"RECV_I_RSP",
 101	"RECV_UI_FRAME",
 102	"RECV_FRMR_RSP",
 103	"RECV_RR_CMD",
 104	"RECV_RR_RSP",
 105	"RECV_RNR_CMD",
 106	"RECV_RNR_RSP",
 107	"RECV_REJ_CMD",
 108	"RECV_REJ_RSP",
 109	"RECV_SREJ_CMD",
 110	"RECV_SREJ_RSP",
 111	"RECV_DISC_CMD",
 112	"SLOT_TIMER_EXPIRED",
 113	"QUERY_TIMER_EXPIRED",
 114	"FINAL_TIMER_EXPIRED",
 115	"POLL_TIMER_EXPIRED",
 116	"DISCOVERY_TIMER_EXPIRED",
 117	"WD_TIMER_EXPIRED",
 118	"BACKOFF_TIMER_EXPIRED",
 119	"MEDIA_BUSY_TIMER_EXPIRED",
 120};
 
 121
 122const char *const irlap_state[] = {
 123	"LAP_NDM",
 124	"LAP_QUERY",
 125	"LAP_REPLY",
 126	"LAP_CONN",
 127	"LAP_SETUP",
 128	"LAP_OFFLINE",
 129	"LAP_XMIT_P",
 130	"LAP_PCLOSE",
 131	"LAP_NRM_P",
 132	"LAP_RESET_WAIT",
 133	"LAP_RESET",
 134	"LAP_NRM_S",
 135	"LAP_XMIT_S",
 136	"LAP_SCLOSE",
 137	"LAP_RESET_CHECK",
 138};
 139
 140static int (*state[])(struct irlap_cb *self, IRLAP_EVENT event,
 141		      struct sk_buff *skb, struct irlap_info *info) =
 142{
 143	irlap_state_ndm,
 144	irlap_state_query,
 145	irlap_state_reply,
 146	irlap_state_conn,
 147	irlap_state_setup,
 148	irlap_state_offline,
 149	irlap_state_xmit_p,
 150	irlap_state_pclose,
 151	irlap_state_nrm_p,
 152	irlap_state_reset_wait,
 153	irlap_state_reset,
 154	irlap_state_nrm_s,
 155	irlap_state_xmit_s,
 156	irlap_state_sclose,
 157	irlap_state_reset_check,
 158};
 159
 160/*
 161 * Function irda_poll_timer_expired (data)
 162 *
 163 *    Poll timer has expired. Normally we must now send a RR frame to the
 164 *    remote device
 165 */
 166static void irlap_poll_timer_expired(void *data)
 167{
 168	struct irlap_cb *self = (struct irlap_cb *) data;
 169
 170	IRDA_ASSERT(self != NULL, return;);
 171	IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
 172
 173	irlap_do_event(self, POLL_TIMER_EXPIRED, NULL, NULL);
 174}
 175
 176/*
 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
 181 */
 182static void irlap_start_poll_timer(struct irlap_cb *self, int timeout)
 183{
 184	IRDA_ASSERT(self != NULL, return;);
 185	IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
 186
 187#ifdef CONFIG_IRDA_FAST_RR
 188	/*
 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
 191	 */
 192	if (skb_queue_empty(&self->txq) || self->remote_busy) {
 193		if (self->fast_RR == TRUE) {
 194			/*
 195			 *  Assert that the fast poll timer has not reached the
 196			 *  normal poll timer yet
 197			 */
 198			if (self->fast_RR_timeout < timeout) {
 199				/*
 200				 *  FIXME: this should be a more configurable
 201				 *         function
 202				 */
 203				self->fast_RR_timeout +=
 204					(sysctl_fast_poll_increase * HZ/1000);
 205
 206				/* Use this fast(er) timeout instead */
 207				timeout = self->fast_RR_timeout;
 208			}
 209		} else {
 210			self->fast_RR = TRUE;
 211
 212			/* Start with just 0 ms */
 213			self->fast_RR_timeout = 0;
 214			timeout = 0;
 215		}
 216	} else
 217		self->fast_RR = FALSE;
 218
 219	pr_debug("%s(), timeout=%d (%ld)\n", __func__, timeout, jiffies);
 220#endif /* CONFIG_IRDA_FAST_RR */
 221
 222	if (timeout == 0)
 223		irlap_do_event(self, POLL_TIMER_EXPIRED, NULL, NULL);
 224	else
 225		irda_start_timer(&self->poll_timer, timeout, self,
 226				 irlap_poll_timer_expired);
 227}
 228
 229/*
 230 * Function irlap_do_event (event, skb, info)
 231 *
 232 *    Rushes through the state machine without any delay. If state == XMIT
 233 *    then send queued data frames.
 234 */
 235void irlap_do_event(struct irlap_cb *self, IRLAP_EVENT event,
 236		    struct sk_buff *skb, struct irlap_info *info)
 237{
 238	int ret;
 239
 240	if (!self || self->magic != LAP_MAGIC)
 241		return;
 242
 243	pr_debug("%s(), event = %s, state = %s\n", __func__,
 244		 irlap_event[event], irlap_state[self->state]);
 245
 246	ret = (*state[self->state])(self, event, skb, info);
 247
 248	/*
 249	 *  Check if there are any pending events that needs to be executed
 250	 */
 251	switch (self->state) {
 252	case LAP_XMIT_P: /* FALLTHROUGH */
 253	case LAP_XMIT_S:
 254		/*
 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
 260		 */
 261		pr_debug("%s() : queue len = %d\n", __func__,
 262			 skb_queue_len(&self->txq));
 263
 264		if (!skb_queue_empty(&self->txq)) {
 265			/* Prevent race conditions with irlap_data_request() */
 266			self->local_busy = TRUE;
 267
 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
 272			 * send them.
 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 */
 282
 283			/* Try to send away all queued data frames */
 284			while ((skb = skb_dequeue(&self->txq)) != NULL) {
 285				/* Send one frame */
 286				ret = (*state[self->state])(self, SEND_I_CMD,
 287							    skb, NULL);
 288				/* Drop reference count.
 289				 * It will be increase as needed in
 290				 * irlap_send_data_xxx() */
 291				kfree_skb(skb);
 292
 293				/* Poll the higher layers for one more frame */
 294				irlmp_flow_indication(self->notify.instance,
 295						      FLOW_START);
 296
 297				if (ret == -EPROTO)
 298					break; /* Try again later! */
 299			}
 300			/* Finished transmitting */
 301			self->local_busy = FALSE;
 302		} else if (self->disconnect_pending) {
 303			self->disconnect_pending = FALSE;
 304
 305			ret = (*state[self->state])(self, DISCONNECT_REQUEST,
 306						    NULL, NULL);
 307		}
 308		break;
 309/*	case LAP_NDM: */
 310/*	case LAP_CONN: */
 311/*	case LAP_RESET_WAIT: */
 312/*	case LAP_RESET_CHECK: */
 313	default:
 314		break;
 315	}
 316}
 317
 318/*
 319 * Function irlap_state_ndm (event, skb, frame)
 320 *
 321 *    NDM (Normal Disconnected Mode) state
 322 *
 323 */
 324static int irlap_state_ndm(struct irlap_cb *self, IRLAP_EVENT event,
 325			   struct sk_buff *skb, struct irlap_info *info)
 326{
 327	discovery_t *discovery_rsp;
 328	int ret = 0;
 329
 330	IRDA_ASSERT(self != NULL, return -1;);
 331	IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
 332
 333	switch (event) {
 334	case CONNECT_REQUEST:
 335		IRDA_ASSERT(self->netdev != NULL, return -1;);
 336
 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",
 342				 __func__);
 343
 344			/* Always switch state before calling upper layers */
 345			irlap_next_state(self, LAP_NDM);
 346
 347			irlap_disconnect_indication(self, LAP_MEDIA_BUSY);
 348		} else {
 349			irlap_send_snrm_frame(self, &self->qos_rx);
 350
 351			/* Start Final-bit timer */
 352			irlap_start_final_timer(self, self->final_timeout);
 353
 354			self->retry_count = 0;
 355			irlap_next_state(self, LAP_SETUP);
 356		}
 357		break;
 358	case RECV_SNRM_CMD:
 359		/* Check if the frame contains and I field */
 360		if (info) {
 361			self->daddr = info->daddr;
 362			self->caddr = info->caddr;
 363
 364			irlap_next_state(self, LAP_CONN);
 365
 366			irlap_connect_indication(self, skb);
 367		} else {
 368			pr_debug("%s(), SNRM frame does not contain an I field!\n",
 369				 __func__);
 370		}
 371		break;
 372	case DISCOVERY_REQUEST:
 373		IRDA_ASSERT(info != NULL, return -1;);
 374
 375		if (self->media_busy) {
 376			pr_debug("%s(), DISCOVERY_REQUEST: media busy!\n",
 377				 __func__);
 378			/* irlap->log.condition = MEDIA_BUSY; */
 379
 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()
 384			 * Jean II */
 385			return 0;
 386		}
 387
 388		self->S = info->S;
 389		self->s = info->s;
 390		irlap_send_discovery_xid_frame(self, info->S, info->s, TRUE,
 391					       info->discovery);
 392		self->frame_sent = FALSE;
 393		self->s++;
 394
 395		irlap_start_slot_timer(self, self->slot_timeout);
 396		irlap_next_state(self, LAP_QUERY);
 397		break;
 398	case RECV_DISCOVERY_XID_CMD:
 399		IRDA_ASSERT(info != NULL, return -1;);
 400
 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,
 404								   info->s);
 405			if (self->slot == info->s) {
 406				discovery_rsp = irlmp_get_discovery_response();
 407				discovery_rsp->data.daddr = info->daddr;
 408
 409				irlap_send_discovery_xid_frame(self, info->S,
 410							       self->slot,
 411							       FALSE,
 412							       discovery_rsp);
 413				self->frame_sent = TRUE;
 414			} else
 415				self->frame_sent = FALSE;
 416
 417			/*
 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
 421			 */
 422			irlap_start_query_timer(self, info->S, info->s);
 423			irlap_next_state(self, LAP_REPLY);
 424		} else {
 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
 440		 * those cases...
 441		 * Jean II
 442		 */
 443			pr_debug("%s(), Receiving final discovery request, missed the discovery slots :-(\n",
 444				 __func__);
 445
 446			/* Last discovery request -> in the log */
 447			irlap_discovery_indication(self, info->discovery);
 448		}
 449		break;
 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.
 459		 * Jean II */
 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...
 465			 * Jean II */
 466			ret = (*state[self->state])(self, SEND_UI_FRAME,
 467						    NULL, NULL);
 468		}
 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;
 474
 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);
 480			else
 481				ret = (*state[self->state])(self,
 482							    CONNECT_REQUEST,
 483							    NULL, NULL);
 484			self->disconnect_pending = FALSE;
 485		}
 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...
 496		 * Jean II */
 497		break;
 498#ifdef CONFIG_IRDA_ULTRA
 499	case SEND_UI_FRAME:
 500	{
 501		int i;
 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);
 505			if (skb)
 506				irlap_send_ui_frame(self, skb, CBROADCAST,
 507						    CMD_FRAME);
 508			else
 509				break;
 510			/* irlap_send_ui_frame() won't increase skb reference
 511			 * count, so no dev_kfree_skb() - Jean II */
 512		}
 513		if (i == 2) {
 514			/* Force us to listen 500 ms again */
 515			irda_device_set_media_busy(self->netdev, TRUE);
 516		}
 517		break;
 518	}
 519	case RECV_UI_FRAME:
 520		/* Only accept broadcast frames in NDM mode */
 521		if (info->caddr != CBROADCAST) {
 522			pr_debug("%s(), not a broadcast frame!\n",
 523				 __func__);
 524		} else
 525			irlap_unitdata_indication(self, skb);
 526		break;
 527#endif /* CONFIG_IRDA_ULTRA */
 528	case RECV_TEST_CMD:
 529		/* Remove test frame header */
 530		skb_pull(skb, sizeof(struct test_frame));
 531
 532		/*
 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
 535		 */
 536		irlap_send_test_frame(self, CBROADCAST, info->daddr, skb);
 537		break;
 538	case RECV_TEST_RSP:
 539		pr_debug("%s() not implemented!\n", __func__);
 540		break;
 541	default:
 542		pr_debug("%s(), Unknown event %s\n", __func__,
 543			 irlap_event[event]);
 544
 545		ret = -1;
 546		break;
 547	}
 548	return ret;
 549}
 550
 551/*
 552 * Function irlap_state_query (event, skb, info)
 553 *
 554 *    QUERY state
 555 *
 556 */
 557static int irlap_state_query(struct irlap_cb *self, IRLAP_EVENT event,
 558			     struct sk_buff *skb, struct irlap_info *info)
 559{
 560	int ret = 0;
 561
 562	IRDA_ASSERT(self != NULL, return -1;);
 563	IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
 564
 565	switch (event) {
 566	case RECV_DISCOVERY_XID_RSP:
 567		IRDA_ASSERT(info != NULL, return -1;);
 568		IRDA_ASSERT(info->discovery != NULL, return -1;);
 569
 570		pr_debug("%s(), daddr=%08x\n", __func__,
 571			 info->discovery->data.daddr);
 572
 573		if (!self->discovery_log) {
 574			net_warn_ratelimited("%s: discovery log is gone! maybe the discovery timeout has been set too short?\n",
 575					     __func__);
 
 576			break;
 577		}
 578		hashbin_insert(self->discovery_log,
 579			       (irda_queue_t *) info->discovery,
 580			       info->discovery->data.daddr, NULL);
 581
 582		/* Keep state */
 583		/* irlap_next_state(self, LAP_QUERY);  */
 584
 585		break;
 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).
 595		 * Jean II
 596		 */
 597
 598		IRDA_ASSERT(info != NULL, return -1;);
 599
 600		pr_debug("%s(), Receiving discovery request (s = %d) while performing discovery :-(\n",
 601			 __func__, info->s);
 602
 603		/* Last discovery request ? */
 604		if (info->s == 0xff)
 605			irlap_discovery_indication(self, info->discovery);
 606		break;
 607	case SLOT_TIMER_EXPIRED:
 608		/*
 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.
 613		 */
 614		if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
 615			pr_debug("%s(), device is slow to answer, waiting some more!\n",
 616				 __func__);
 617			irlap_start_slot_timer(self, msecs_to_jiffies(10));
 618			self->add_wait = TRUE;
 619			return ret;
 620		}
 621		self->add_wait = FALSE;
 622
 623		if (self->s < self->S) {
 624			irlap_send_discovery_xid_frame(self, self->S,
 625						       self->s, TRUE,
 626						       self->discovery_cmd);
 627			self->s++;
 628			irlap_start_slot_timer(self, self->slot_timeout);
 629
 630			/* Keep state */
 631			irlap_next_state(self, LAP_QUERY);
 632		} else {
 633			/* This is the final slot! */
 634			irlap_send_discovery_xid_frame(self, self->S, 0xff,
 635						       TRUE,
 636						       self->discovery_cmd);
 637
 638			/* Always switch state before calling upper layers */
 639			irlap_next_state(self, LAP_NDM);
 640
 641			/*
 642			 *  We are now finished with the discovery procedure,
 643			 *  so now we must return the results
 644			 */
 645			irlap_discovery_confirm(self, self->discovery_log);
 646
 647			/* IrLMP should now have taken care of the log */
 648			self->discovery_log = NULL;
 649		}
 650		break;
 651	default:
 652		pr_debug("%s(), Unknown event %s\n", __func__,
 653			 irlap_event[event]);
 654
 655		ret = -1;
 656		break;
 657	}
 658	return ret;
 659}
 660
 661/*
 662 * Function irlap_state_reply (self, event, skb, info)
 663 *
 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
 666 *
 667 */
 668static int irlap_state_reply(struct irlap_cb *self, IRLAP_EVENT event,
 669			     struct sk_buff *skb, struct irlap_info *info)
 670{
 671	discovery_t *discovery_rsp;
 672	int ret=0;
 673
 
 
 674	IRDA_ASSERT(self != NULL, return -1;);
 675	IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
 676
 677	switch (event) {
 678	case QUERY_TIMER_EXPIRED:
 679		pr_debug("%s(), QUERY_TIMER_EXPIRED <%ld>\n",
 680			 __func__, jiffies);
 681		irlap_next_state(self, LAP_NDM);
 682		break;
 683	case RECV_DISCOVERY_XID_CMD:
 684		IRDA_ASSERT(info != NULL, return -1;);
 685		/* Last frame? */
 686		if (info->s == 0xff) {
 687			del_timer(&self->query_timer);
 688
 689			/* info->log.condition = REMOTE; */
 690
 691			/* Always switch state before calling upper layers */
 692			irlap_next_state(self, LAP_NDM);
 693
 694			irlap_discovery_indication(self, info->discovery);
 695		} else {
 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;
 700
 701				irlap_send_discovery_xid_frame(self, info->S,
 702							       self->slot,
 703							       FALSE,
 704							       discovery_rsp);
 705
 706				self->frame_sent = TRUE;
 707			}
 708			/* Readjust our timer to accommodate devices
 709			 * doing faster or slower discovery than us...
 710			 * Jean II */
 711			irlap_start_query_timer(self, info->S, info->s);
 712
 713			/* Keep state */
 714			//irlap_next_state(self, LAP_REPLY);
 715		}
 716		break;
 717	default:
 718		pr_debug("%s(), Unknown event %d, %s\n", __func__,
 719			 event, irlap_event[event]);
 720
 721		ret = -1;
 722		break;
 723	}
 724	return ret;
 725}
 726
 727/*
 728 * Function irlap_state_conn (event, skb, info)
 729 *
 730 *    CONN, we have received a SNRM command and is waiting for the upper
 731 *    layer to accept or refuse connection
 732 *
 733 */
 734static int irlap_state_conn(struct irlap_cb *self, IRLAP_EVENT event,
 735			    struct sk_buff *skb, struct irlap_info *info)
 736{
 737	int ret = 0;
 738
 739	pr_debug("%s(), event=%s\n", __func__, irlap_event[event]);
 740
 741	IRDA_ASSERT(self != NULL, return -1;);
 742	IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
 743
 744	switch (event) {
 745	case CONNECT_RESPONSE:
 746		skb_pull(skb, sizeof(struct snrm_frame));
 747
 748		IRDA_ASSERT(self->netdev != NULL, return -1;);
 749
 750		irlap_qos_negotiate(self, skb);
 751
 752		irlap_initiate_connection_state(self);
 753
 754		/*
 755		 * Applying the parameters now will make sure we change speed
 756		 * *after* we have sent the next frame
 757		 */
 758		irlap_apply_connection_parameters(self, FALSE);
 759
 760		/*
 761		 * Sending this frame will force a speed change after it has
 762		 * been sent (i.e. the frame will be sent at 9600).
 763		 */
 764		irlap_send_ua_response_frame(self, &self->qos_rx);
 765
 766#if 0
 767		/*
 768		 * We are allowed to send two frames, but this may increase
 769		 * the connect latency, so lets not do it for now.
 770		 */
 771		/* This is full of good intentions, but doesn't work in
 772		 * practice.
 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.
 785		 * Jean II */
 786		irlap_send_ua_response_frame(self, &self->qos_rx);
 787#endif
 788
 789		/*
 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).
 793		 */
 794		irlap_start_wd_timer(self, self->wd_timeout);
 795		irlap_next_state(self, LAP_NRM_S);
 796
 797		break;
 798	case RECV_DISCOVERY_XID_CMD:
 799		pr_debug("%s(), event RECV_DISCOVER_XID_CMD!\n",
 800			 __func__);
 801		irlap_next_state(self, LAP_NDM);
 802
 803		break;
 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);
 809		break;
 810	default:
 811		pr_debug("%s(), Unknown event %d, %s\n", __func__,
 812			 event, irlap_event[event]);
 813
 814		ret = -1;
 815		break;
 816	}
 817
 818	return ret;
 819}
 820
 821/*
 822 * Function irlap_state_setup (event, skb, frame)
 823 *
 824 *    SETUP state, The local layer has transmitted a SNRM command frame to
 825 *    a remote peer layer and is awaiting a reply .
 826 *
 827 */
 828static int irlap_state_setup(struct irlap_cb *self, IRLAP_EVENT event,
 829			     struct sk_buff *skb, struct irlap_info *info)
 830{
 831	int ret = 0;
 832
 
 
 833	IRDA_ASSERT(self != NULL, return -1;);
 834	IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
 835
 836	switch (event) {
 837	case FINAL_TIMER_EXPIRED:
 838		if (self->retry_count < self->N3) {
 839/*
 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.
 844 */
 845			irlap_start_backoff_timer(self, msecs_to_jiffies(20 +
 846							(jiffies % 30)));
 847		} else {
 848			/* Always switch state before calling upper layers */
 849			irlap_next_state(self, LAP_NDM);
 850
 851			irlap_disconnect_indication(self, LAP_FOUND_NONE);
 852		}
 853		break;
 854	case BACKOFF_TIMER_EXPIRED:
 855		irlap_send_snrm_frame(self, &self->qos_rx);
 856		irlap_start_final_timer(self, self->final_timeout);
 857		self->retry_count++;
 858		break;
 859	case RECV_SNRM_CMD:
 860		pr_debug("%s(), SNRM battle!\n", __func__);
 861
 862		IRDA_ASSERT(skb != NULL, return 0;);
 863		IRDA_ASSERT(info != NULL, return 0;);
 864
 865		/*
 866		 *  The device with the largest device address wins the battle
 867		 *  (both have sent a SNRM command!)
 868		 */
 869		if (info &&(info->daddr > self->saddr)) {
 870			del_timer(&self->final_timer);
 871			irlap_initiate_connection_state(self);
 872
 873			IRDA_ASSERT(self->netdev != NULL, return -1;);
 874
 875			skb_pull(skb, sizeof(struct snrm_frame));
 876
 877			irlap_qos_negotiate(self, skb);
 878
 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);
 882
 883			irlap_next_state(self, LAP_NRM_S);
 884			irlap_connect_confirm(self, skb);
 885
 886			/*
 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).
 890			 */
 891			irlap_start_wd_timer(self, self->wd_timeout);
 892		} else {
 893			/* We just ignore the other device! */
 894			irlap_next_state(self, LAP_SETUP);
 895		}
 896		break;
 897	case RECV_UA_RSP:
 898		/* Stop F-timer */
 899		del_timer(&self->final_timer);
 900
 901		/* Initiate connection state */
 902		irlap_initiate_connection_state(self);
 903
 904		/* Negotiate connection parameters */
 905		IRDA_ASSERT(skb->len > 10, return -1;);
 906
 907		skb_pull(skb, sizeof(struct ua_frame));
 908
 909		IRDA_ASSERT(self->netdev != NULL, return -1;);
 910
 911		irlap_qos_negotiate(self, skb);
 912
 913		/* Set the new link setting *now* (before the rr frame) */
 914		irlap_apply_connection_parameters(self, TRUE);
 915		self->retry_count = 0;
 916
 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...
 922		 * Jean II */
 923		irlap_wait_min_turn_around(self, &self->qos_tx);
 924
 925		/* This frame will actually be sent at the new speed */
 926		irlap_send_rr_frame(self, CMD_FRAME);
 927
 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.
 933		 * Jean II */
 934		irlap_start_final_timer(self, self->final_timeout/2);
 935		irlap_next_state(self, LAP_NRM_P);
 936
 937		irlap_connect_confirm(self, skb);
 938		break;
 939	case RECV_DM_RSP:     /* FALLTHROUGH */
 940	case RECV_DISC_CMD:
 941		del_timer(&self->final_timer);
 942		irlap_next_state(self, LAP_NDM);
 943
 944		irlap_disconnect_indication(self, LAP_DISC_INDICATION);
 945		break;
 946	default:
 947		pr_debug("%s(), Unknown event %d, %s\n", __func__,
 948			 event, irlap_event[event]);
 949
 950		ret = -1;
 951		break;
 952	}
 953	return ret;
 954}
 955
 956/*
 957 * Function irlap_state_offline (self, event, skb, info)
 958 *
 959 *    OFFLINE state, not used for now!
 960 *
 961 */
 962static int irlap_state_offline(struct irlap_cb *self, IRLAP_EVENT event,
 963			       struct sk_buff *skb, struct irlap_info *info)
 964{
 965	pr_debug("%s(), Unknown event\n", __func__);
 966
 967	return -1;
 968}
 969
 970/*
 971 * Function irlap_state_xmit_p (self, event, skb, info)
 972 *
 973 *    XMIT, Only the primary station has right to transmit, and we
 974 *    therefore do not expect to receive any transmissions from other
 975 *    stations.
 976 *
 977 */
 978static int irlap_state_xmit_p(struct irlap_cb *self, IRLAP_EVENT event,
 979			      struct sk_buff *skb, struct irlap_info *info)
 980{
 981	int ret = 0;
 982
 983	switch (event) {
 984	case SEND_I_CMD:
 985		/*
 986		 *  Only send frame if send-window > 0.
 987		 */
 988		if ((self->window > 0) && (!self->remote_busy)) {
 989			int nextfit;
 990#ifdef CONFIG_IRDA_DYNAMIC_WINDOW
 991			struct sk_buff *skb_next;
 992
 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).
1000			 * Jean II */
1001
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);
1006
1007			/* Check if a subsequent skb exist and would fit in
1008			 * the current window (with respect to turnaround
1009			 * time).
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)
1015			 * Jean II */
1016			nextfit = ((skb_next != NULL) &&
1017				   ((skb_next->len + skb->len) <=
1018				    self->bytes_left));
1019
1020			/*
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.
1026			 */
1027			if((!nextfit) && (skb->len > self->bytes_left)) {
1028				pr_debug("%s(), Not allowed to transmit more bytes!\n",
1029					 __func__);
1030				/* Requeue the skb */
1031				skb_queue_head(&self->txq, skb_get(skb));
1032				/*
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
1038				 *  DB
1039				 */
1040				/*
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
1046				 */
1047				return -EPROTO;
1048			}
1049
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 */
1057			/*
1058			 *  Send data with poll bit cleared only if window > 1
1059			 *  and there is more frames after this one to be sent
1060			 */
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);
1065			} else {
1066				/* Final packet of window */
1067				irlap_send_data_primary_poll(self, skb);
1068
1069				/*
1070				 * Make sure state machine does not try to send
1071				 * any more frames
1072				 */
1073				ret = -EPROTO;
1074			}
1075#ifdef CONFIG_IRDA_FAST_RR
1076			/* Peer may want to reply immediately */
1077			self->fast_RR = FALSE;
1078#endif /* CONFIG_IRDA_FAST_RR */
1079		} else {
1080			pr_debug("%s(), Unable to send! remote busy?\n",
1081				 __func__);
1082			skb_queue_head(&self->txq, skb_get(skb));
1083
1084			/*
1085			 *  The next ret is important, because it tells
1086			 *  irlap_next_state _not_ to deliver more frames
1087			 */
1088			ret = -EPROTO;
1089		}
1090		break;
1091	case POLL_TIMER_EXPIRED:
1092		pr_debug("%s(), POLL_TIMER_EXPIRED <%ld>\n",
1093			 __func__, jiffies);
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);
1103		break;
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);
1112		break;
1113	case DATA_REQUEST:
1114		/* Nothing to do, irlap_do_event() will send the packet
1115		 * when we return... - Jean II */
1116		break;
1117	default:
1118		pr_debug("%s(), Unknown event %s\n",
1119			 __func__, irlap_event[event]);
1120
1121		ret = -EINVAL;
1122		break;
1123	}
1124	return ret;
1125}
1126
1127/*
1128 * Function irlap_state_pclose (event, skb, info)
1129 *
1130 *    PCLOSE state
1131 */
1132static int irlap_state_pclose(struct irlap_cb *self, IRLAP_EVENT event,
1133			      struct sk_buff *skb, struct irlap_info *info)
1134{
1135	int ret = 0;
1136
 
 
1137	IRDA_ASSERT(self != NULL, return -1;);
1138	IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
1139
1140	switch (event) {
1141	case RECV_UA_RSP: /* FALLTHROUGH */
1142	case RECV_DM_RSP:
1143		del_timer(&self->final_timer);
1144
1145		/* Set new link parameters */
1146		irlap_apply_default_connection_parameters(self);
1147
1148		/* Always switch state before calling upper layers */
1149		irlap_next_state(self, LAP_NDM);
1150
1151		irlap_disconnect_indication(self, LAP_DISC_INDICATION);
1152		break;
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++;
1159			/* Keep state */
1160		} else {
1161			irlap_apply_default_connection_parameters(self);
1162
1163			/*  Always switch state before calling upper layers */
1164			irlap_next_state(self, LAP_NDM);
1165
1166			irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1167		}
1168		break;
1169	default:
1170		pr_debug("%s(), Unknown event %d\n", __func__, event);
1171
1172		ret = -1;
1173		break;
1174	}
1175	return ret;
1176}
1177
1178/*
1179 * Function irlap_state_nrm_p (self, event, skb, info)
1180 *
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.
1186 */
1187static int irlap_state_nrm_p(struct irlap_cb *self, IRLAP_EVENT event,
1188			     struct sk_buff *skb, struct irlap_info *info)
1189{
1190	int ret = 0;
1191	int ns_status;
1192	int nr_status;
1193
1194	switch (event) {
1195	case RECV_I_RSP: /* Optimize for the common case */
1196		if (unlikely(skb->len <= LAP_ADDR_HEADER + LAP_CTRL_HEADER)) {
1197			/*
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
1203			 */
1204			irlap_wait_min_turn_around(self, &self->qos_tx);
1205			irlap_send_rr_frame(self, CMD_FRAME);
1206			/* Keep state */
1207			break;
1208		}
1209		/* FIXME: must check for remote_busy below */
1210#ifdef CONFIG_IRDA_FAST_RR
1211		/*
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
1214		 *  to transmitt
1215		 */
1216		self->fast_RR = FALSE;
1217#endif /* CONFIG_IRDA_FAST_RR */
1218		IRDA_ASSERT( info != NULL, return -1;);
1219
1220		ns_status = irlap_validate_ns_received(self, info->ns);
1221		nr_status = irlap_validate_nr_received(self, info->nr);
1222
1223		/*
1224		 *  Check for expected I(nformation) frame
1225		 */
1226		if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
1227
1228			/* Update Vr (next frame for us to receive) */
1229			self->vr = (self->vr + 1) % 8;
1230
1231			/* Update Nr received, cleanup our retry queue */
1232			irlap_update_nr_received(self, info->nr);
1233
1234			/*
1235			 *  Got expected NR, so reset the
1236			 *  retry_count. This is not done by IrLAP spec,
1237			 *  which is strange!
1238			 */
1239			self->retry_count = 0;
1240			self->ack_required = TRUE;
1241
1242			/*  poll bit cleared?  */
1243			if (!info->pf) {
1244				/* Keep state, do not move this line */
1245				irlap_next_state(self, LAP_NRM_P);
1246
1247				irlap_data_indication(self, skb, FALSE);
1248			} else {
1249				/* No longer waiting for pf */
1250				del_timer(&self->final_timer);
1251
1252				irlap_wait_min_turn_around(self, &self->qos_tx);
1253
1254				/* Call higher layer *before* changing state
1255				 * to give them a chance to send data in the
1256				 * next LAP frame.
1257				 * Jean II */
1258				irlap_data_indication(self, skb, FALSE);
1259
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().
1266				 * Jean II
1267				 */
1268				irlap_next_state(self, LAP_XMIT_P);
1269
1270				/* This is the last frame.
1271				 * Make sure it's always called in XMIT state.
1272				 * - Jean II */
1273				irlap_start_poll_timer(self, self->poll_timeout);
1274			}
1275			break;
1276
1277		}
1278		/* Unexpected next to send (Ns) */
1279		if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
1280		{
1281			if (!info->pf) {
1282				irlap_update_nr_received(self, info->nr);
1283
1284				/*
1285				 *  Wait until the last frame before doing
1286				 *  anything
1287				 */
1288
1289				/* Keep state */
1290				irlap_next_state(self, LAP_NRM_P);
1291			} else {
1292				pr_debug("%s(), missing or duplicate frame!\n",
1293					 __func__);
 
1294
1295				/* Update Nr received */
1296				irlap_update_nr_received(self, info->nr);
1297
1298				irlap_wait_min_turn_around(self, &self->qos_tx);
1299				irlap_send_rr_frame(self, CMD_FRAME);
1300
1301				self->ack_required = FALSE;
1302
1303				irlap_start_final_timer(self, self->final_timeout);
1304				irlap_next_state(self, LAP_NRM_P);
1305			}
1306			break;
1307		}
1308		/*
1309		 *  Unexpected next to receive (Nr)
1310		 */
1311		if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
1312		{
1313			if (info->pf) {
1314				self->vr = (self->vr + 1) % 8;
1315
1316				/* Update Nr received */
1317				irlap_update_nr_received(self, info->nr);
1318
1319				/* Resend rejected frames */
1320				irlap_resend_rejected_frames(self, CMD_FRAME);
1321
1322				self->ack_required = FALSE;
1323
1324				/* Make sure we account for the time
1325				 * to transmit our frames. See comemnts
1326				 * in irlap_send_data_primary_poll().
1327				 * Jean II */
1328				irlap_start_final_timer(self, 2 * self->final_timeout);
1329
1330				/* Keep state, do not move this line */
1331				irlap_next_state(self, LAP_NRM_P);
1332
1333				irlap_data_indication(self, skb, FALSE);
1334			} else {
1335				/*
1336				 *  Do not resend frames until the last
1337				 *  frame has arrived from the other
1338				 *  device. This is not documented in
1339				 *  IrLAP!!
1340				 */
1341				self->vr = (self->vr + 1) % 8;
1342
1343				/* Update Nr received */
1344				irlap_update_nr_received(self, info->nr);
1345
1346				self->ack_required = FALSE;
1347
1348				/* Keep state, do not move this line!*/
1349				irlap_next_state(self, LAP_NRM_P);
1350
1351				irlap_data_indication(self, skb, FALSE);
1352			}
1353			break;
1354		}
1355		/*
1356		 *  Unexpected next to send (Ns) and next to receive (Nr)
1357		 *  Not documented by IrLAP!
1358		 */
1359		if ((ns_status == NS_UNEXPECTED) &&
1360		    (nr_status == NR_UNEXPECTED))
1361		{
1362			pr_debug("%s(), unexpected nr and ns!\n",
1363				 __func__);
1364			if (info->pf) {
1365				/* Resend rejected frames */
1366				irlap_resend_rejected_frames(self, CMD_FRAME);
1367
1368				/* Give peer some time to retransmit!
1369				 * But account for our own Tx. */
1370				irlap_start_final_timer(self, 2 * self->final_timeout);
1371
1372				/* Keep state, do not move this line */
1373				irlap_next_state(self, LAP_NRM_P);
1374			} else {
1375				/* Update Nr received */
1376				/* irlap_update_nr_received( info->nr); */
1377
1378				self->ack_required = FALSE;
1379			}
1380			break;
1381		}
1382
1383		/*
1384		 *  Invalid NR or NS
1385		 */
1386		if ((nr_status == NR_INVALID) || (ns_status == NS_INVALID)) {
1387			if (info->pf) {
1388				del_timer(&self->final_timer);
1389
1390				irlap_next_state(self, LAP_RESET_WAIT);
1391
1392				irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1393				self->xmitflag = TRUE;
1394			} else {
1395				del_timer(&self->final_timer);
1396
1397				irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1398
1399				self->xmitflag = FALSE;
1400			}
1401			break;
1402		}
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);
1406		break;
1407	case RECV_UI_FRAME:
1408		/* Poll bit cleared? */
1409		if (!info->pf) {
1410			irlap_data_indication(self, skb, TRUE);
1411			irlap_next_state(self, LAP_NRM_P);
1412		} else {
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);
1419		}
1420		break;
1421	case RECV_RR_RSP:
1422		/*
1423		 *  If you get a RR, the remote isn't busy anymore,
1424		 *  no matter what the NR
1425		 */
1426		self->remote_busy = FALSE;
1427
1428		/* Stop final timer */
1429		del_timer(&self->final_timer);
1430
1431		/*
1432		 *  Nr as expected?
1433		 */
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);
1438
1439			/*
1440			 *  Got expected NR, so reset the retry_count. This
1441			 *  is not done by the IrLAP standard , which is
1442			 *  strange! DB.
1443			 */
1444			self->retry_count = 0;
1445			irlap_wait_min_turn_around(self, &self->qos_tx);
1446
1447			irlap_next_state(self, LAP_XMIT_P);
1448
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;);
1453			/*
1454			 *  Unexpected nr!
1455			 */
1456
1457			/* Update Nr received */
1458			irlap_update_nr_received(self, info->nr);
1459
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);
 
1463
1464			/* Resend rejected frames */
1465			irlap_resend_rejected_frames(self, CMD_FRAME);
1466			irlap_start_final_timer(self, self->final_timeout * 2);
1467
1468			irlap_next_state(self, LAP_NRM_P);
1469		} else if (ret == NR_INVALID) {
1470			pr_debug("%s(), Received RR with invalid nr !\n",
1471				 __func__);
1472
1473			irlap_next_state(self, LAP_RESET_WAIT);
1474
1475			irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1476			self->xmitflag = TRUE;
1477		}
1478		break;
1479	case RECV_RNR_RSP:
1480		IRDA_ASSERT(info != NULL, return -1;);
1481
1482		/* Stop final timer */
1483		del_timer(&self->final_timer);
1484		self->remote_busy = TRUE;
1485
1486		/* Update Nr received */
1487		irlap_update_nr_received(self, info->nr);
1488		irlap_next_state(self, LAP_XMIT_P);
1489
1490		/* Start poll timer */
1491		irlap_start_poll_timer(self, self->poll_timeout);
1492		break;
1493	case RECV_FRMR_RSP:
1494		del_timer(&self->final_timer);
1495		self->xmitflag = TRUE;
1496		irlap_next_state(self, LAP_RESET_WAIT);
1497		irlap_reset_indication(self);
1498		break;
1499	case FINAL_TIMER_EXPIRED:
1500		/*
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.
1505		 */
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));
1509
1510			/*
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.
1514			 */
1515			self->add_wait = TRUE;
1516			break;
1517		}
1518		self->add_wait = FALSE;
1519
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);
1527			} else {
1528				pr_debug("nrm_p: resend frames");
1529				irlap_resend_rejected_frames(self, CMD_FRAME);
1530			}
1531
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",
1535				 self->retry_count);
1536
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...
1542			 * Jean II */
1543			if((self->retry_count % self->N1) == 0)
1544				irlap_status_indication(self,
1545							STATUS_NO_ACTIVITY);
1546
1547			/* Keep state */
1548		} else {
1549			irlap_apply_default_connection_parameters(self);
1550
1551			/* Always switch state before calling upper layers */
1552			irlap_next_state(self, LAP_NDM);
1553			irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1554		}
1555		break;
1556	case RECV_REJ_RSP:
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);
1561		} else
1562			irlap_resend_rejected_frames(self, CMD_FRAME);
1563		irlap_start_final_timer(self, 2 * self->final_timeout);
1564		break;
1565	case RECV_SREJ_RSP:
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);
1570		} else
1571			irlap_resend_rejected_frame(self, CMD_FRAME);
1572		irlap_start_final_timer(self, 2 * self->final_timeout);
1573		break;
1574	case RECV_RD_RSP:
1575		pr_debug("%s(), RECV_RD_RSP\n", __func__);
1576
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);
1581		break;
1582	default:
1583		pr_debug("%s(), Unknown event %s\n",
1584			 __func__, irlap_event[event]);
1585
1586		ret = -1;
1587		break;
1588	}
1589	return ret;
1590}
1591
1592/*
1593 * Function irlap_state_reset_wait (event, skb, info)
1594 *
1595 *    We have informed the service user of a reset condition, and is
1596 *    awaiting reset of disconnect request.
1597 *
1598 */
1599static int irlap_state_reset_wait(struct irlap_cb *self, IRLAP_EVENT event,
1600				  struct sk_buff *skb, struct irlap_info *info)
1601{
1602	int ret = 0;
1603
1604	pr_debug("%s(), event = %s\n", __func__, irlap_event[event]);
1605
1606	IRDA_ASSERT(self != NULL, return -1;);
1607	IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
1608
1609	switch (event) {
1610	case RESET_REQUEST:
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);
1616		} else {
1617			irlap_start_final_timer(self, self->final_timeout);
1618			irlap_next_state(self, LAP_RESET);
1619		}
1620		break;
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);
1628		break;
1629	default:
1630		pr_debug("%s(), Unknown event %s\n", __func__,
1631			 irlap_event[event]);
1632
1633		ret = -1;
1634		break;
1635	}
1636	return ret;
1637}
1638
1639/*
1640 * Function irlap_state_reset (self, event, skb, info)
1641 *
1642 *    We have sent a SNRM reset command to the peer layer, and is awaiting
1643 *    reply.
1644 *
1645 */
1646static int irlap_state_reset(struct irlap_cb *self, IRLAP_EVENT event,
1647			     struct sk_buff *skb, struct irlap_info *info)
1648{
1649	int ret = 0;
1650
1651	pr_debug("%s(), event = %s\n", __func__, irlap_event[event]);
1652
1653	IRDA_ASSERT(self != NULL, return -1;);
1654	IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
1655
1656	switch (event) {
1657	case RECV_DISC_CMD:
1658		del_timer(&self->final_timer);
1659
1660		irlap_apply_default_connection_parameters(self);
1661
1662		/* Always switch state before calling upper layers */
1663		irlap_next_state(self, LAP_NDM);
1664
1665		irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1666
1667		break;
1668	case RECV_UA_RSP:
1669		del_timer(&self->final_timer);
1670
1671		/* Initiate connection state */
1672		irlap_initiate_connection_state(self);
1673
1674		irlap_reset_confirm();
1675
1676		self->remote_busy = FALSE;
1677
1678		irlap_next_state(self, LAP_XMIT_P);
1679
1680		irlap_start_poll_timer(self, self->poll_timeout);
1681
1682		break;
1683	case FINAL_TIMER_EXPIRED:
1684		if (self->retry_count < 3) {
1685			irlap_wait_min_turn_around(self, &self->qos_tx);
1686
1687			IRDA_ASSERT(self->netdev != NULL, return -1;);
1688			irlap_send_snrm_frame(self, self->qos_dev);
1689
1690			self->retry_count++; /* Experimental!! */
1691
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);
1696
1697			/* Always switch state before calling upper layers */
1698			irlap_next_state(self, LAP_NDM);
1699
1700			irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1701		}
1702		break;
1703	case RECV_SNRM_CMD:
1704		/*
1705		 * SNRM frame is not allowed to contain an I-field in this
1706		 * state
1707		 */
1708		if (!info) {
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);
1716		} else {
1717			pr_debug("%s(), SNRM frame contained an I field!\n",
1718				 __func__);
 
1719		}
1720		break;
1721	default:
1722		pr_debug("%s(), Unknown event %s\n",
1723			 __func__, irlap_event[event]);
1724
1725		ret = -1;
1726		break;
1727	}
1728	return ret;
1729}
1730
1731/*
1732 * Function irlap_state_xmit_s (event, skb, info)
1733 *
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
1736 *   stations.
1737 */
1738static int irlap_state_xmit_s(struct irlap_cb *self, IRLAP_EVENT event,
1739			      struct sk_buff *skb, struct irlap_info *info)
1740{
1741	int ret = 0;
1742
1743	pr_debug("%s(), event=%s\n", __func__, irlap_event[event]);
1744
1745	IRDA_ASSERT(self != NULL, return -ENODEV;);
1746	IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
1747
1748	switch (event) {
1749	case SEND_I_CMD:
1750		/*
1751		 *  Send frame only if send window > 0
1752		 */
1753		if ((self->window > 0) && (!self->remote_busy)) {
1754			int nextfit;
1755#ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1756			struct sk_buff *skb_next;
1757
1758			/*
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
1763			 */
1764
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) <=
1771				    self->bytes_left));
1772
1773			/*
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.
1777			 */
1778			if((!nextfit) && (skb->len > self->bytes_left)) {
1779				pr_debug("%s(), Not allowed to transmit more bytes!\n",
1780					 __func__);
1781				/* Requeue the skb */
1782				skb_queue_head(&self->txq, skb_get(skb));
1783
1784				/*
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
1788				 *  frames
1789				 */
1790				self->window = self->window_size;
1791				self->bytes_left = self->line_capacity;
1792				irlap_start_wd_timer(self, self->wd_timeout);
1793
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 */
1798
1799				return -EPROTO; /* Try again later */
1800			}
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 */
1808			/*
1809			 *  Send data with final bit cleared only if window > 1
1810			 *  and there is more frames to be sent
1811			 */
1812			if ((self->window > 1) && (nextfit)) {
1813				irlap_send_data_secondary(self, skb);
1814				irlap_next_state(self, LAP_XMIT_S);
1815			} else {
1816				irlap_send_data_secondary_final(self, skb);
1817				irlap_next_state(self, LAP_NRM_S);
1818
1819				/*
1820				 * Make sure state machine does not try to send
1821				 * any more frames
1822				 */
1823				ret = -EPROTO;
1824			}
1825		} else {
1826			pr_debug("%s(), Unable to send!\n", __func__);
1827			skb_queue_head(&self->txq, skb_get(skb));
1828			ret = -EPROTO;
1829		}
1830		break;
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);
1836		break;
1837	case DATA_REQUEST:
1838		/* Nothing to do, irlap_do_event() will send the packet
1839		 * when we return... - Jean II */
1840		break;
1841	default:
1842		pr_debug("%s(), Unknown event %s\n", __func__,
1843			 irlap_event[event]);
1844
1845		ret = -EINVAL;
1846		break;
1847	}
1848	return ret;
1849}
1850
1851/*
1852 * Function irlap_state_nrm_s (event, skb, info)
1853 *
1854 *    NRM_S (Normal Response Mode as Secondary) state, in this state we are
1855 *    expecting to receive frames from the primary station
1856 *
1857 */
1858static int irlap_state_nrm_s(struct irlap_cb *self, IRLAP_EVENT event,
1859			     struct sk_buff *skb, struct irlap_info *info)
1860{
1861	int ns_status;
1862	int nr_status;
1863	int ret = 0;
1864
1865	pr_debug("%s(), event=%s\n", __func__, irlap_event[event]);
1866
1867	IRDA_ASSERT(self != NULL, return -1;);
1868	IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
1869
1870	switch (event) {
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);
 
1876
1877		self->retry_count = 0;
1878
1879		ns_status = irlap_validate_ns_received(self, info->ns);
1880		nr_status = irlap_validate_nr_received(self, info->nr);
1881		/*
1882		 *  Check for expected I(nformation) frame
1883		 */
1884		if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
1885
1886			/* Update Vr (next frame for us to receive) */
1887			self->vr = (self->vr + 1) % 8;
1888
1889			/* Update Nr received */
1890			irlap_update_nr_received(self, info->nr);
1891
1892			/*
1893			 *  poll bit cleared?
1894			 */
1895			if (!info->pf) {
1896
1897				self->ack_required = TRUE;
1898
1899				/*
1900				 *  Starting WD-timer here is optional, but
1901				 *  not recommended. Note 6 IrLAP p. 83
1902				 */
1903#if 0
1904				irda_start_timer(WD_TIMER, self->wd_timeout);
1905#endif
1906				/* Keep state, do not move this line */
1907				irlap_next_state(self, LAP_NRM_S);
1908
1909				irlap_data_indication(self, skb, FALSE);
1910				break;
1911			} else {
1912				/*
1913				 *  We should wait before sending RR, and
1914				 *  also before changing to XMIT_S
1915				 *  state. (note 1, IrLAP p. 82)
1916				 */
1917				irlap_wait_min_turn_around(self, &self->qos_tx);
1918
1919				/*
1920				 * Give higher layers a chance to
1921				 * immediately reply with some data before
1922				 * we decide if we should send a RR frame
1923				 * or not
1924				 */
1925				irlap_data_indication(self, skb, FALSE);
1926
1927				/* Any pending data requests?  */
1928				if (!skb_queue_empty(&self->txq) &&
1929				    (self->window > 0))
1930				{
1931					self->ack_required = TRUE;
1932
1933					del_timer(&self->wd_timer);
1934
1935					irlap_next_state(self, LAP_XMIT_S);
1936				} else {
1937					irlap_send_rr_frame(self, RSP_FRAME);
1938					irlap_start_wd_timer(self,
1939							     self->wd_timeout);
1940
1941					/* Keep the state */
1942					irlap_next_state(self, LAP_NRM_S);
1943				}
1944				break;
1945			}
1946		}
1947		/*
1948		 *  Check for Unexpected next to send (Ns)
1949		 */
1950		if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
1951		{
1952			/* Unexpected next to send, with final bit cleared */
1953			if (!info->pf) {
1954				irlap_update_nr_received(self, info->nr);
1955
1956				irlap_start_wd_timer(self, self->wd_timeout);
1957			} else {
1958				/* Update Nr received */
1959				irlap_update_nr_received(self, info->nr);
1960
1961				irlap_wait_min_turn_around(self, &self->qos_tx);
1962				irlap_send_rr_frame(self, RSP_FRAME);
1963
1964				irlap_start_wd_timer(self, self->wd_timeout);
1965			}
1966			break;
1967		}
1968
1969		/*
1970		 *  Unexpected Next to Receive(NR) ?
1971		 */
1972		if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
1973		{
1974			if (info->pf) {
1975				pr_debug("RECV_I_RSP: frame(s) lost\n");
1976
1977				self->vr = (self->vr + 1) % 8;
1978
1979				/* Update Nr received */
1980				irlap_update_nr_received(self, info->nr);
1981
1982				/* Resend rejected frames */
1983				irlap_resend_rejected_frames(self, RSP_FRAME);
1984
1985				/* Keep state, do not move this line */
1986				irlap_next_state(self, LAP_NRM_S);
1987
1988				irlap_data_indication(self, skb, FALSE);
1989				irlap_start_wd_timer(self, self->wd_timeout);
1990				break;
1991			}
1992			/*
1993			 *  This is not documented in IrLAP!! Unexpected NR
1994			 *  with poll bit cleared
1995			 */
1996			if (!info->pf) {
1997				self->vr = (self->vr + 1) % 8;
1998
1999				/* Update Nr received */
2000				irlap_update_nr_received(self, info->nr);
2001
2002				/* Keep state, do not move this line */
2003				irlap_next_state(self, LAP_NRM_S);
2004
2005				irlap_data_indication(self, skb, FALSE);
2006				irlap_start_wd_timer(self, self->wd_timeout);
2007			}
2008			break;
2009		}
2010
2011		if (ret == NR_INVALID) {
2012			pr_debug("NRM_S, NR_INVALID not implemented!\n");
2013		}
2014		if (ret == NS_INVALID) {
2015			pr_debug("NRM_S, NS_INVALID not implemented!\n");
2016		}
2017		break;
2018	case RECV_UI_FRAME:
2019		/*
2020		 *  poll bit cleared?
2021		 */
2022		if (!info->pf) {
2023			irlap_data_indication(self, skb, TRUE);
2024			irlap_next_state(self, LAP_NRM_S); /* Keep state */
2025		} else {
2026			/*
2027			 *  Any pending data requests?
2028			 */
2029			if (!skb_queue_empty(&self->txq) &&
2030			    (self->window > 0) && !self->remote_busy)
2031			{
2032				irlap_data_indication(self, skb, TRUE);
2033
2034				del_timer(&self->wd_timer);
2035
2036				irlap_next_state(self, LAP_XMIT_S);
2037			} else {
2038				irlap_data_indication(self, skb, TRUE);
2039
2040				irlap_wait_min_turn_around(self, &self->qos_tx);
2041
2042				irlap_send_rr_frame(self, RSP_FRAME);
2043				self->ack_required = FALSE;
2044
2045				irlap_start_wd_timer(self, self->wd_timeout);
2046
2047				/* Keep the state */
2048				irlap_next_state(self, LAP_NRM_S);
2049			}
2050		}
2051		break;
2052	case RECV_RR_CMD:
2053		self->retry_count = 0;
2054
2055		/*
2056		 *  Nr as expected?
2057		 */
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;
2063
2064				/* Update Nr received */
2065				irlap_update_nr_received(self, info->nr);
2066				del_timer(&self->wd_timer);
2067
2068				irlap_wait_min_turn_around(self, &self->qos_tx);
2069				irlap_next_state(self, LAP_XMIT_S);
2070			} else {
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);
2076
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) {
2082					/* Disconnect */
2083					irlap_send_rd_frame(self);
2084					irlap_flush_all_queues(self);
2085
2086					irlap_next_state(self, LAP_SCLOSE);
2087				} else {
2088					/* Just send back pf bit */
2089					irlap_send_rr_frame(self, RSP_FRAME);
2090
2091					irlap_next_state(self, LAP_NRM_S);
2092				}
2093			}
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);
2098
2099			irlap_start_wd_timer(self, self->wd_timeout);
2100
2101			/* Keep state */
2102			irlap_next_state(self, LAP_NRM_S);
2103		} else {
2104			pr_debug("%s(), invalid nr not implemented!\n",
2105				 __func__);
2106		}
2107		break;
2108	case RECV_SNRM_CMD:
2109		/* SNRM frame is not allowed to contain an I-field */
2110		if (!info) {
2111			del_timer(&self->wd_timer);
2112			pr_debug("%s(), received SNRM cmd\n", __func__);
2113			irlap_next_state(self, LAP_RESET_CHECK);
2114
2115			irlap_reset_indication(self);
2116		} else {
2117			pr_debug("%s(), SNRM frame contained an I-field!\n",
2118				 __func__);
 
2119
2120		}
2121		break;
2122	case RECV_REJ_CMD:
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);
2127		} else
2128			irlap_resend_rejected_frames(self, RSP_FRAME);
2129		irlap_start_wd_timer(self, self->wd_timeout);
2130		break;
2131	case RECV_SREJ_CMD:
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);
2136		} else
2137			irlap_resend_rejected_frame(self, RSP_FRAME);
2138		irlap_start_wd_timer(self, self->wd_timeout);
2139		break;
2140	case WD_TIMER_EXPIRED:
2141		/*
2142		 *  Wait until retry_count * n matches negotiated threshold/
2143		 *  disconnect time (note 2 in IrLAP p. 82)
2144		 *
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 !!!
2148		 * Jean II
2149		 */
2150		pr_debug("%s(), retry_count = %d\n", __func__,
2151			 self->retry_count);
2152
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++;
2157
2158			if((self->retry_count % (self->N1 / 2)) == 0)
2159				irlap_status_indication(self,
2160							STATUS_NO_ACTIVITY);
2161		} else {
2162			irlap_apply_default_connection_parameters(self);
2163
2164			/* Always switch state before calling upper layers */
2165			irlap_next_state(self, LAP_NDM);
2166			irlap_disconnect_indication(self, LAP_NO_RESPONSE);
2167		}
2168		break;
2169	case RECV_DISC_CMD:
2170		/* Always switch state before calling upper layers */
2171		irlap_next_state(self, LAP_NDM);
2172
2173		/* Send disconnect response */
2174		irlap_wait_min_turn_around(self, &self->qos_tx);
2175		irlap_send_ua_response_frame(self, NULL);
2176
2177		del_timer(&self->wd_timer);
2178		irlap_flush_all_queues(self);
2179		/* Set default link parameters */
2180		irlap_apply_default_connection_parameters(self);
2181
2182		irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2183		break;
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);
2190
2191		break;
2192	case RECV_TEST_CMD:
2193		/* Remove test frame header (only LAP header in NRM) */
2194		skb_pull(skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
2195
2196		irlap_wait_min_turn_around(self, &self->qos_tx);
2197		irlap_start_wd_timer(self, self->wd_timeout);
2198
2199		/* Send response (info will be copied) */
2200		irlap_send_test_frame(self, self->caddr, info->daddr, skb);
2201		break;
2202	default:
2203		pr_debug("%s(), Unknown event %d, (%s)\n", __func__,
2204			 event, irlap_event[event]);
2205
2206		ret = -EINVAL;
2207		break;
2208	}
2209	return ret;
2210}
2211
2212/*
2213 * Function irlap_state_sclose (self, event, skb, info)
2214 */
2215static int irlap_state_sclose(struct irlap_cb *self, IRLAP_EVENT event,
2216			      struct sk_buff *skb, struct irlap_info *info)
2217{
 
 
2218	IRDA_ASSERT(self != NULL, return -ENODEV;);
2219	IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
2220
2221	switch (event) {
2222	case RECV_DISC_CMD:
2223		/* Always switch state before calling upper layers */
2224		irlap_next_state(self, LAP_NDM);
2225
2226		/* Send disconnect response */
2227		irlap_wait_min_turn_around(self, &self->qos_tx);
2228		irlap_send_ua_response_frame(self, NULL);
2229
2230		del_timer(&self->wd_timer);
2231		/* Set default link parameters */
2232		irlap_apply_default_connection_parameters(self);
2233
2234		irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2235		break;
2236	case RECV_DM_RSP:
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
2239		 */
2240	case RECV_RR_RSP:
2241	case RECV_RNR_RSP:
2242	case RECV_REJ_RSP:
2243	case RECV_SREJ_RSP:
2244	case RECV_I_RSP:
2245		/* Always switch state before calling upper layers */
2246		irlap_next_state(self, LAP_NDM);
2247
2248		del_timer(&self->wd_timer);
2249		irlap_apply_default_connection_parameters(self);
2250
2251		irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2252		break;
2253	case WD_TIMER_EXPIRED:
2254		/* Always switch state before calling upper layers */
2255		irlap_next_state(self, LAP_NDM);
2256
2257		irlap_apply_default_connection_parameters(self);
2258
2259		irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2260		break;
2261	default:
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
2264		 */
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 */
2271		}
2272
2273		pr_debug("%s(), Unknown event %d, (%s)\n", __func__,
2274			 event, irlap_event[event]);
2275
2276		break;
2277	}
2278
2279	return -1;
2280}
2281
2282static int irlap_state_reset_check( struct irlap_cb *self, IRLAP_EVENT event,
2283				   struct sk_buff *skb,
2284				   struct irlap_info *info)
2285{
2286	int ret = 0;
2287
2288	pr_debug("%s(), event=%s\n", __func__, irlap_event[event]);
2289
2290	IRDA_ASSERT(self != NULL, return -ENODEV;);
2291	IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
2292
2293	switch (event) {
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);
2299
2300		irlap_next_state(self, LAP_NRM_S);
2301		break;
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);
2307		break;
2308	default:
2309		pr_debug("%s(), Unknown event %d, (%s)\n", __func__,
2310			 event, irlap_event[event]);
2311
2312		ret = -EINVAL;
2313		break;
2314	}
2315	return ret;
2316}