blob: 5f4a84eb35c7d6cd221f077aa7a73214e88139ea [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*********************************************************************
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 *
Jan Engelhardt96de0e22007-10-19 23:21:04 +020022 * Neither Dag Brattli nor University of Tromsø admit liability nor
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * provide warranty for any of this software. This material is
24 * provided "AS-IS" and at no charge.
25 *
26 ********************************************************************/
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/string.h>
29#include <linux/kernel.h>
30#include <linux/delay.h>
31#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
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
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -070082static const char *const irlap_event[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 "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
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700124const char *const irlap_state[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 "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 */
David S. Millerb03efcf2005-07-08 14:57:23 -0700194 if (skb_queue_empty(&self->txq) || self->remote_busy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 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
Harvey Harrison0dc47872008-03-05 20:47:47 -0800221 IRDA_DEBUG(3, "%s(), timeout=%d (%ld)\n", __func__, timeout, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222#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
Harvey Harrison0dc47872008-03-05 20:47:47 -0800245 IRDA_DEBUG(3, "%s(), event = %s, state = %s\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 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 */
Harvey Harrison0dc47872008-03-05 20:47:47 -0800263 IRDA_DEBUG(2, "%s() : queue len = %d\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 skb_queue_len(&self->txq));
265
David S. Millerb03efcf2005-07-08 14:57:23 -0700266 if (!skb_queue_empty(&self->txq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 /* 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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 * 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",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800344 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
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 "
Harvey Harrison0dc47872008-03-05 20:47:47 -0800371 "contain an I field!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
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",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800379 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /* 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 */
Harvey Harrison0dc47872008-03-05 20:47:47 -0800445 IRDA_DEBUG(1, "%s(), Receiving final discovery request, missed the discovery slots :-(\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
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",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800524 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 } 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:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800540 IRDA_DEBUG(0, "%s() not implemented!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 break;
542 default:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800543 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 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
Harvey Harrison0dc47872008-03-05 20:47:47 -0800571 IRDA_DEBUG(4, "%s(), daddr=%08x\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 info->discovery->data.daddr);
573
574 if (!self->discovery_log) {
Joe Perches6c910232014-11-11 13:37:30 -0800575 net_warn_ratelimited("%s: discovery log is gone! maybe the discovery timeout has been set too short?\n",
576 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 break;
578 }
579 hashbin_insert(self->discovery_log,
580 (irda_queue_t *) info->discovery,
581 info->discovery->data.daddr, NULL);
582
583 /* Keep state */
584 /* irlap_next_state(self, LAP_QUERY); */
585
586 break;
587 case RECV_DISCOVERY_XID_CMD:
588 /* Yes, it is possible to receive those frames in this mode.
589 * Note that most often the last discovery request won't
590 * occur here but in NDM state (see my comment there).
591 * What should we do ?
592 * Not much. We are currently performing our own discovery,
593 * therefore we can't answer those frames. We don't want
594 * to change state either. We just pass the info to
595 * IrLMP who will put it in the log (and post an event).
596 * Jean II
597 */
598
599 IRDA_ASSERT(info != NULL, return -1;);
600
Harvey Harrison0dc47872008-03-05 20:47:47 -0800601 IRDA_DEBUG(1, "%s(), Receiving discovery request (s = %d) while performing discovery :-(\n", __func__, info->s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
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 IRDA_DEBUG(2, "%s(), device is slow to answer, "
Harvey Harrison0dc47872008-03-05 20:47:47 -0800616 "waiting some more!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 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:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800652 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 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
Harvey Harrison0dc47872008-03-05 20:47:47 -0800674 IRDA_DEBUG(4, "%s()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 IRDA_ASSERT(self != NULL, return -1;);
677 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
678
679 switch (event) {
680 case QUERY_TIMER_EXPIRED:
681 IRDA_DEBUG(0, "%s(), QUERY_TIMER_EXPIRED <%ld>\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800682 __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 irlap_next_state(self, LAP_NDM);
684 break;
685 case RECV_DISCOVERY_XID_CMD:
686 IRDA_ASSERT(info != NULL, return -1;);
687 /* Last frame? */
688 if (info->s == 0xff) {
689 del_timer(&self->query_timer);
690
691 /* info->log.condition = REMOTE; */
692
693 /* Always switch state before calling upper layers */
694 irlap_next_state(self, LAP_NDM);
695
696 irlap_discovery_indication(self, info->discovery);
697 } else {
698 /* If it's our slot, send our reply */
699 if ((info->s >= self->slot) && (!self->frame_sent)) {
700 discovery_rsp = irlmp_get_discovery_response();
701 discovery_rsp->data.daddr = info->daddr;
702
703 irlap_send_discovery_xid_frame(self, info->S,
704 self->slot,
705 FALSE,
706 discovery_rsp);
707
708 self->frame_sent = TRUE;
709 }
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300710 /* Readjust our timer to accommodate devices
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 * doing faster or slower discovery than us...
712 * Jean II */
713 irlap_start_query_timer(self, info->S, info->s);
714
715 /* Keep state */
716 //irlap_next_state(self, LAP_REPLY);
717 }
718 break;
719 default:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800720 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 event, irlap_event[event]);
722
723 ret = -1;
724 break;
725 }
726 return ret;
727}
728
729/*
730 * Function irlap_state_conn (event, skb, info)
731 *
732 * CONN, we have received a SNRM command and is waiting for the upper
733 * layer to accept or refuse connection
734 *
735 */
736static int irlap_state_conn(struct irlap_cb *self, IRLAP_EVENT event,
737 struct sk_buff *skb, struct irlap_info *info)
738{
739 int ret = 0;
740
Harvey Harrison0dc47872008-03-05 20:47:47 -0800741 IRDA_DEBUG(4, "%s(), event=%s\n", __func__, irlap_event[ event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 IRDA_ASSERT(self != NULL, return -1;);
744 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
745
746 switch (event) {
747 case CONNECT_RESPONSE:
748 skb_pull(skb, sizeof(struct snrm_frame));
749
750 IRDA_ASSERT(self->netdev != NULL, return -1;);
751
752 irlap_qos_negotiate(self, skb);
753
754 irlap_initiate_connection_state(self);
755
756 /*
757 * Applying the parameters now will make sure we change speed
758 * *after* we have sent the next frame
759 */
760 irlap_apply_connection_parameters(self, FALSE);
761
762 /*
763 * Sending this frame will force a speed change after it has
764 * been sent (i.e. the frame will be sent at 9600).
765 */
766 irlap_send_ua_response_frame(self, &self->qos_rx);
767
768#if 0
769 /*
770 * We are allowed to send two frames, but this may increase
771 * the connect latency, so lets not do it for now.
772 */
773 /* This is full of good intentions, but doesn't work in
774 * practice.
775 * After sending the first UA response, we switch the
776 * dongle to the negotiated speed, which is usually
777 * different than 9600 kb/s.
778 * From there, there is two solutions :
779 * 1) The other end has received the first UA response :
780 * it will set up the connection, move to state LAP_NRM_P,
781 * and will ignore and drop the second UA response.
782 * Actually, it's even worse : the other side will almost
783 * immediately send a RR that will likely collide with the
784 * UA response (depending on negotiated turnaround).
785 * 2) The other end has not received the first UA response,
786 * will stay at 9600 and will never see the second UA response.
787 * Jean II */
788 irlap_send_ua_response_frame(self, &self->qos_rx);
789#endif
790
791 /*
792 * The WD-timer could be set to the duration of the P-timer
793 * for this case, but it is recommended to use twice the
794 * value (note 3 IrLAP p. 60).
795 */
796 irlap_start_wd_timer(self, self->wd_timeout);
797 irlap_next_state(self, LAP_NRM_S);
798
799 break;
800 case RECV_DISCOVERY_XID_CMD:
801 IRDA_DEBUG(3, "%s(), event RECV_DISCOVER_XID_CMD!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800802 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 irlap_next_state(self, LAP_NDM);
804
805 break;
806 case DISCONNECT_REQUEST:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800807 IRDA_DEBUG(0, "%s(), Disconnect request!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 irlap_send_dm_frame(self);
809 irlap_next_state( self, LAP_NDM);
810 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
811 break;
812 default:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800813 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 event, irlap_event[event]);
815
816 ret = -1;
817 break;
818 }
819
820 return ret;
821}
822
823/*
824 * Function irlap_state_setup (event, skb, frame)
825 *
826 * SETUP state, The local layer has transmitted a SNRM command frame to
827 * a remote peer layer and is awaiting a reply .
828 *
829 */
830static int irlap_state_setup(struct irlap_cb *self, IRLAP_EVENT event,
831 struct sk_buff *skb, struct irlap_info *info)
832{
833 int ret = 0;
834
Harvey Harrison0dc47872008-03-05 20:47:47 -0800835 IRDA_DEBUG(4, "%s()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 IRDA_ASSERT(self != NULL, return -1;);
838 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
839
840 switch (event) {
841 case FINAL_TIMER_EXPIRED:
842 if (self->retry_count < self->N3) {
843/*
844 * Perform random backoff, Wait a random number of time units, minimum
845 * duration half the time taken to transmitt a SNRM frame, maximum duration
846 * 1.5 times the time taken to transmit a SNRM frame. So this time should
847 * between 15 msecs and 45 msecs.
848 */
849 irlap_start_backoff_timer(self, msecs_to_jiffies(20 +
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900850 (jiffies % 30)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 } else {
852 /* Always switch state before calling upper layers */
853 irlap_next_state(self, LAP_NDM);
854
855 irlap_disconnect_indication(self, LAP_FOUND_NONE);
856 }
857 break;
858 case BACKOFF_TIMER_EXPIRED:
859 irlap_send_snrm_frame(self, &self->qos_rx);
860 irlap_start_final_timer(self, self->final_timeout);
861 self->retry_count++;
862 break;
863 case RECV_SNRM_CMD:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800864 IRDA_DEBUG(4, "%s(), SNRM battle!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 IRDA_ASSERT(skb != NULL, return 0;);
867 IRDA_ASSERT(info != NULL, return 0;);
868
869 /*
870 * The device with the largest device address wins the battle
871 * (both have sent a SNRM command!)
872 */
873 if (info &&(info->daddr > self->saddr)) {
874 del_timer(&self->final_timer);
875 irlap_initiate_connection_state(self);
876
877 IRDA_ASSERT(self->netdev != NULL, return -1;);
878
879 skb_pull(skb, sizeof(struct snrm_frame));
880
881 irlap_qos_negotiate(self, skb);
882
883 /* Send UA frame and then change link settings */
884 irlap_apply_connection_parameters(self, FALSE);
885 irlap_send_ua_response_frame(self, &self->qos_rx);
886
887 irlap_next_state(self, LAP_NRM_S);
888 irlap_connect_confirm(self, skb);
889
890 /*
891 * The WD-timer could be set to the duration of the
892 * P-timer for this case, but it is recommended
893 * to use twice the value (note 3 IrLAP p. 60).
894 */
895 irlap_start_wd_timer(self, self->wd_timeout);
896 } else {
897 /* We just ignore the other device! */
898 irlap_next_state(self, LAP_SETUP);
899 }
900 break;
901 case RECV_UA_RSP:
902 /* Stop F-timer */
903 del_timer(&self->final_timer);
904
905 /* Initiate connection state */
906 irlap_initiate_connection_state(self);
907
908 /* Negotiate connection parameters */
909 IRDA_ASSERT(skb->len > 10, return -1;);
910
911 skb_pull(skb, sizeof(struct ua_frame));
912
913 IRDA_ASSERT(self->netdev != NULL, return -1;);
914
915 irlap_qos_negotiate(self, skb);
916
917 /* Set the new link setting *now* (before the rr frame) */
918 irlap_apply_connection_parameters(self, TRUE);
919 self->retry_count = 0;
920
921 /* Wait for turnaround time to give a chance to the other
922 * device to be ready to receive us.
923 * Note : the time to switch speed is typically larger
924 * than the turnaround time, but as we don't have the other
925 * side speed switch time, that's our best guess...
926 * Jean II */
927 irlap_wait_min_turn_around(self, &self->qos_tx);
928
929 /* This frame will actually be sent at the new speed */
930 irlap_send_rr_frame(self, CMD_FRAME);
931
932 /* The timer is set to half the normal timer to quickly
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300933 * detect a failure to negotiate the new connection
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 * parameters. IrLAP 6.11.3.2, note 3.
935 * Note that currently we don't process this failure
936 * properly, as we should do a quick disconnect.
937 * Jean II */
938 irlap_start_final_timer(self, self->final_timeout/2);
939 irlap_next_state(self, LAP_NRM_P);
940
941 irlap_connect_confirm(self, skb);
942 break;
943 case RECV_DM_RSP: /* FALLTHROUGH */
944 case RECV_DISC_CMD:
945 del_timer(&self->final_timer);
946 irlap_next_state(self, LAP_NDM);
947
948 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
949 break;
950 default:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800951 IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 event, irlap_event[event]);
953
954 ret = -1;
955 break;
956 }
957 return ret;
958}
959
960/*
961 * Function irlap_state_offline (self, event, skb, info)
962 *
963 * OFFLINE state, not used for now!
964 *
965 */
966static int irlap_state_offline(struct irlap_cb *self, IRLAP_EVENT event,
967 struct sk_buff *skb, struct irlap_info *info)
968{
Harvey Harrison0dc47872008-03-05 20:47:47 -0800969 IRDA_DEBUG( 0, "%s(), Unknown event\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 return -1;
972}
973
974/*
975 * Function irlap_state_xmit_p (self, event, skb, info)
976 *
977 * XMIT, Only the primary station has right to transmit, and we
978 * therefore do not expect to receive any transmissions from other
979 * stations.
980 *
981 */
982static int irlap_state_xmit_p(struct irlap_cb *self, IRLAP_EVENT event,
983 struct sk_buff *skb, struct irlap_info *info)
984{
985 int ret = 0;
986
987 switch (event) {
988 case SEND_I_CMD:
989 /*
990 * Only send frame if send-window > 0.
991 */
992 if ((self->window > 0) && (!self->remote_busy)) {
993 int nextfit;
994#ifdef CONFIG_IRDA_DYNAMIC_WINDOW
995 struct sk_buff *skb_next;
996
997 /* With DYNAMIC_WINDOW, we keep the window size
998 * maximum, and adapt on the packets we are sending.
999 * At 115k, we can send only 2 packets of 2048 bytes
1000 * in a 500 ms turnaround. Without this option, we
1001 * would always limit the window to 2. With this
1002 * option, if we send smaller packets, we can send
1003 * up to 7 of them (always depending on QoS).
1004 * Jean II */
1005
1006 /* Look at the next skb. This is safe, as we are
1007 * the only consumer of the Tx queue (if we are not,
1008 * we have other problems) - Jean II */
1009 skb_next = skb_peek(&self->txq);
1010
1011 /* Check if a subsequent skb exist and would fit in
1012 * the current window (with respect to turnaround
1013 * time).
1014 * This allow us to properly mark the current packet
1015 * with the pf bit, to avoid falling back on the
1016 * second test below, and avoid waiting the
1017 * end of the window and sending a extra RR.
1018 * Note : (skb_next != NULL) <=> (skb_queue_len() > 0)
1019 * Jean II */
1020 nextfit = ((skb_next != NULL) &&
1021 ((skb_next->len + skb->len) <=
1022 self->bytes_left));
1023
1024 /*
1025 * The current packet may not fit ! Because of test
1026 * above, this should not happen any more !!!
1027 * Test if we have transmitted more bytes over the
1028 * link than its possible to do with the current
1029 * speed and turn-around-time.
1030 */
1031 if((!nextfit) && (skb->len > self->bytes_left)) {
1032 IRDA_DEBUG(0, "%s(), Not allowed to transmit"
Harvey Harrison0dc47872008-03-05 20:47:47 -08001033 " more bytes!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /* Requeue the skb */
1035 skb_queue_head(&self->txq, skb_get(skb));
1036 /*
1037 * We should switch state to LAP_NRM_P, but
1038 * that is not possible since we must be sure
1039 * that we poll the other side. Since we have
1040 * used up our time, the poll timer should
1041 * trigger anyway now, so we just wait for it
1042 * DB
1043 */
1044 /*
1045 * Sorry, but that's not totally true. If
1046 * we send 2000B packets, we may wait another
1047 * 1000B until our turnaround expire. That's
1048 * why we need to be proactive in avoiding
1049 * coming here. - Jean II
1050 */
1051 return -EPROTO;
1052 }
1053
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001054 /* Subtract space used by this skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 self->bytes_left -= skb->len;
1056#else /* CONFIG_IRDA_DYNAMIC_WINDOW */
1057 /* Window has been adjusted for the max packet
1058 * size, so much simpler... - Jean II */
David S. Millerb03efcf2005-07-08 14:57:23 -07001059 nextfit = !skb_queue_empty(&self->txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060#endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1061 /*
1062 * Send data with poll bit cleared only if window > 1
1063 * and there is more frames after this one to be sent
1064 */
1065 if ((self->window > 1) && (nextfit)) {
1066 /* More packet to send in current window */
1067 irlap_send_data_primary(self, skb);
1068 irlap_next_state(self, LAP_XMIT_P);
1069 } else {
1070 /* Final packet of window */
1071 irlap_send_data_primary_poll(self, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 /*
1074 * Make sure state machine does not try to send
1075 * any more frames
1076 */
1077 ret = -EPROTO;
1078 }
1079#ifdef CONFIG_IRDA_FAST_RR
1080 /* Peer may want to reply immediately */
1081 self->fast_RR = FALSE;
1082#endif /* CONFIG_IRDA_FAST_RR */
1083 } else {
1084 IRDA_DEBUG(4, "%s(), Unable to send! remote busy?\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001085 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 skb_queue_head(&self->txq, skb_get(skb));
1087
1088 /*
1089 * The next ret is important, because it tells
1090 * irlap_next_state _not_ to deliver more frames
1091 */
1092 ret = -EPROTO;
1093 }
1094 break;
1095 case POLL_TIMER_EXPIRED:
1096 IRDA_DEBUG(3, "%s(), POLL_TIMER_EXPIRED <%ld>\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001097 __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 irlap_send_rr_frame(self, CMD_FRAME);
1099 /* Return to NRM properly - Jean II */
1100 self->window = self->window_size;
1101#ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1102 /* Allowed to transmit a maximum number of bytes again. */
1103 self->bytes_left = self->line_capacity;
1104#endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1105 irlap_start_final_timer(self, self->final_timeout);
1106 irlap_next_state(self, LAP_NRM_P);
1107 break;
1108 case DISCONNECT_REQUEST:
1109 del_timer(&self->poll_timer);
1110 irlap_wait_min_turn_around(self, &self->qos_tx);
1111 irlap_send_disc_frame(self);
1112 irlap_flush_all_queues(self);
1113 irlap_start_final_timer(self, self->final_timeout);
1114 self->retry_count = 0;
1115 irlap_next_state(self, LAP_PCLOSE);
1116 break;
1117 case DATA_REQUEST:
1118 /* Nothing to do, irlap_do_event() will send the packet
1119 * when we return... - Jean II */
1120 break;
1121 default:
1122 IRDA_DEBUG(0, "%s(), Unknown event %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001123 __func__, irlap_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 ret = -EINVAL;
1126 break;
1127 }
1128 return ret;
1129}
1130
1131/*
1132 * Function irlap_state_pclose (event, skb, info)
1133 *
1134 * PCLOSE state
1135 */
1136static int irlap_state_pclose(struct irlap_cb *self, IRLAP_EVENT event,
1137 struct sk_buff *skb, struct irlap_info *info)
1138{
1139 int ret = 0;
1140
Harvey Harrison0dc47872008-03-05 20:47:47 -08001141 IRDA_DEBUG(1, "%s()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 IRDA_ASSERT(self != NULL, return -1;);
1144 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
1145
1146 switch (event) {
1147 case RECV_UA_RSP: /* FALLTHROUGH */
1148 case RECV_DM_RSP:
1149 del_timer(&self->final_timer);
1150
1151 /* Set new link parameters */
1152 irlap_apply_default_connection_parameters(self);
1153
1154 /* Always switch state before calling upper layers */
1155 irlap_next_state(self, LAP_NDM);
1156
1157 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
1158 break;
1159 case FINAL_TIMER_EXPIRED:
1160 if (self->retry_count < self->N3) {
1161 irlap_wait_min_turn_around(self, &self->qos_tx);
1162 irlap_send_disc_frame(self);
1163 irlap_start_final_timer(self, self->final_timeout);
1164 self->retry_count++;
1165 /* Keep state */
1166 } else {
1167 irlap_apply_default_connection_parameters(self);
1168
1169 /* Always switch state before calling upper layers */
1170 irlap_next_state(self, LAP_NDM);
1171
1172 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1173 }
1174 break;
1175 default:
Harvey Harrison0dc47872008-03-05 20:47:47 -08001176 IRDA_DEBUG(1, "%s(), Unknown event %d\n", __func__, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 ret = -1;
1179 break;
1180 }
1181 return ret;
1182}
1183
1184/*
1185 * Function irlap_state_nrm_p (self, event, skb, info)
1186 *
1187 * NRM_P (Normal Response Mode as Primary), The primary station has given
1188 * permissions to a secondary station to transmit IrLAP resonse frames
1189 * (by sending a frame with the P bit set). The primary station will not
1190 * transmit any frames and is expecting to receive frames only from the
1191 * secondary to which transmission permissions has been given.
1192 */
1193static int irlap_state_nrm_p(struct irlap_cb *self, IRLAP_EVENT event,
1194 struct sk_buff *skb, struct irlap_info *info)
1195{
1196 int ret = 0;
1197 int ns_status;
1198 int nr_status;
1199
1200 switch (event) {
1201 case RECV_I_RSP: /* Optimize for the common case */
Robie Basak5d780cd2008-01-18 23:58:44 -08001202 if (unlikely(skb->len <= LAP_ADDR_HEADER + LAP_CTRL_HEADER)) {
1203 /*
1204 * Input validation check: a stir4200/mcp2150
1205 * combination sometimes results in an empty i:rsp.
1206 * This makes no sense; we can just ignore the frame
1207 * and send an rr:cmd immediately. This happens before
1208 * changing nr or ns so triggers a retransmit
1209 */
1210 irlap_wait_min_turn_around(self, &self->qos_tx);
1211 irlap_send_rr_frame(self, CMD_FRAME);
1212 /* Keep state */
1213 break;
1214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 /* FIXME: must check for remote_busy below */
1216#ifdef CONFIG_IRDA_FAST_RR
1217 /*
1218 * Reset the fast_RR so we can use the fast RR code with
1219 * full speed the next time since peer may have more frames
1220 * to transmitt
1221 */
1222 self->fast_RR = FALSE;
1223#endif /* CONFIG_IRDA_FAST_RR */
1224 IRDA_ASSERT( info != NULL, return -1;);
1225
1226 ns_status = irlap_validate_ns_received(self, info->ns);
1227 nr_status = irlap_validate_nr_received(self, info->nr);
1228
1229 /*
1230 * Check for expected I(nformation) frame
1231 */
1232 if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
1233
1234 /* Update Vr (next frame for us to receive) */
1235 self->vr = (self->vr + 1) % 8;
1236
1237 /* Update Nr received, cleanup our retry queue */
1238 irlap_update_nr_received(self, info->nr);
1239
1240 /*
1241 * Got expected NR, so reset the
1242 * retry_count. This is not done by IrLAP spec,
1243 * which is strange!
1244 */
1245 self->retry_count = 0;
1246 self->ack_required = TRUE;
1247
1248 /* poll bit cleared? */
1249 if (!info->pf) {
1250 /* Keep state, do not move this line */
1251 irlap_next_state(self, LAP_NRM_P);
1252
1253 irlap_data_indication(self, skb, FALSE);
1254 } else {
1255 /* No longer waiting for pf */
1256 del_timer(&self->final_timer);
1257
1258 irlap_wait_min_turn_around(self, &self->qos_tx);
1259
1260 /* Call higher layer *before* changing state
1261 * to give them a chance to send data in the
1262 * next LAP frame.
1263 * Jean II */
1264 irlap_data_indication(self, skb, FALSE);
1265
1266 /* XMIT states are the most dangerous state
1267 * to be in, because user requests are
1268 * processed directly and may change state.
1269 * On the other hand, in NDM_P, those
1270 * requests are queued and we will process
1271 * them when we return to irlap_do_event().
1272 * Jean II
1273 */
1274 irlap_next_state(self, LAP_XMIT_P);
1275
1276 /* This is the last frame.
1277 * Make sure it's always called in XMIT state.
1278 * - Jean II */
1279 irlap_start_poll_timer(self, self->poll_timeout);
1280 }
1281 break;
1282
1283 }
1284 /* Unexpected next to send (Ns) */
1285 if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
1286 {
1287 if (!info->pf) {
1288 irlap_update_nr_received(self, info->nr);
1289
1290 /*
1291 * Wait until the last frame before doing
1292 * anything
1293 */
1294
1295 /* Keep state */
1296 irlap_next_state(self, LAP_NRM_P);
1297 } else {
1298 IRDA_DEBUG(4,
1299 "%s(), missing or duplicate frame!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001300 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 /* Update Nr received */
1303 irlap_update_nr_received(self, info->nr);
1304
1305 irlap_wait_min_turn_around(self, &self->qos_tx);
1306 irlap_send_rr_frame(self, CMD_FRAME);
1307
1308 self->ack_required = FALSE;
1309
1310 irlap_start_final_timer(self, self->final_timeout);
1311 irlap_next_state(self, LAP_NRM_P);
1312 }
1313 break;
1314 }
1315 /*
1316 * Unexpected next to receive (Nr)
1317 */
1318 if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
1319 {
1320 if (info->pf) {
1321 self->vr = (self->vr + 1) % 8;
1322
1323 /* Update Nr received */
1324 irlap_update_nr_received(self, info->nr);
1325
1326 /* Resend rejected frames */
1327 irlap_resend_rejected_frames(self, CMD_FRAME);
1328
1329 self->ack_required = FALSE;
1330
1331 /* Make sure we account for the time
1332 * to transmit our frames. See comemnts
1333 * in irlap_send_data_primary_poll().
1334 * Jean II */
1335 irlap_start_final_timer(self, 2 * self->final_timeout);
1336
1337 /* Keep state, do not move this line */
1338 irlap_next_state(self, LAP_NRM_P);
1339
1340 irlap_data_indication(self, skb, FALSE);
1341 } else {
1342 /*
1343 * Do not resend frames until the last
1344 * frame has arrived from the other
1345 * device. This is not documented in
1346 * IrLAP!!
1347 */
1348 self->vr = (self->vr + 1) % 8;
1349
1350 /* Update Nr received */
1351 irlap_update_nr_received(self, info->nr);
1352
1353 self->ack_required = FALSE;
1354
1355 /* Keep state, do not move this line!*/
1356 irlap_next_state(self, LAP_NRM_P);
1357
1358 irlap_data_indication(self, skb, FALSE);
1359 }
1360 break;
1361 }
1362 /*
1363 * Unexpected next to send (Ns) and next to receive (Nr)
1364 * Not documented by IrLAP!
1365 */
1366 if ((ns_status == NS_UNEXPECTED) &&
1367 (nr_status == NR_UNEXPECTED))
1368 {
1369 IRDA_DEBUG(4, "%s(), unexpected nr and ns!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001370 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (info->pf) {
1372 /* Resend rejected frames */
1373 irlap_resend_rejected_frames(self, CMD_FRAME);
1374
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001375 /* Give peer some time to retransmit!
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 * But account for our own Tx. */
1377 irlap_start_final_timer(self, 2 * self->final_timeout);
1378
1379 /* Keep state, do not move this line */
1380 irlap_next_state(self, LAP_NRM_P);
1381 } else {
1382 /* Update Nr received */
1383 /* irlap_update_nr_received( info->nr); */
1384
1385 self->ack_required = FALSE;
1386 }
1387 break;
1388 }
1389
1390 /*
1391 * Invalid NR or NS
1392 */
1393 if ((nr_status == NR_INVALID) || (ns_status == NS_INVALID)) {
1394 if (info->pf) {
1395 del_timer(&self->final_timer);
1396
1397 irlap_next_state(self, LAP_RESET_WAIT);
1398
1399 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1400 self->xmitflag = TRUE;
1401 } else {
1402 del_timer(&self->final_timer);
1403
1404 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1405
1406 self->xmitflag = FALSE;
1407 }
1408 break;
1409 }
Harvey Harrison0dc47872008-03-05 20:47:47 -08001410 IRDA_DEBUG(1, "%s(), Not implemented!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 IRDA_DEBUG(1, "%s(), event=%s, ns_status=%d, nr_status=%d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001412 __func__, irlap_event[event], ns_status, nr_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 break;
1414 case RECV_UI_FRAME:
1415 /* Poll bit cleared? */
1416 if (!info->pf) {
1417 irlap_data_indication(self, skb, TRUE);
1418 irlap_next_state(self, LAP_NRM_P);
1419 } else {
1420 del_timer(&self->final_timer);
1421 irlap_data_indication(self, skb, TRUE);
1422 irlap_next_state(self, LAP_XMIT_P);
Harvey Harrison0dc47872008-03-05 20:47:47 -08001423 IRDA_DEBUG(1, "%s: RECV_UI_FRAME: next state %s\n", __func__, irlap_state[self->state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 irlap_start_poll_timer(self, self->poll_timeout);
1425 }
1426 break;
1427 case RECV_RR_RSP:
1428 /*
1429 * If you get a RR, the remote isn't busy anymore,
1430 * no matter what the NR
1431 */
1432 self->remote_busy = FALSE;
1433
G. Liakhovetskib7e773b2007-06-08 19:15:56 -07001434 /* Stop final timer */
1435 del_timer(&self->final_timer);
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 /*
1438 * Nr as expected?
1439 */
1440 ret = irlap_validate_nr_received(self, info->nr);
1441 if (ret == NR_EXPECTED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 /* Update Nr received */
1443 irlap_update_nr_received(self, info->nr);
1444
1445 /*
1446 * Got expected NR, so reset the retry_count. This
1447 * is not done by the IrLAP standard , which is
1448 * strange! DB.
1449 */
1450 self->retry_count = 0;
1451 irlap_wait_min_turn_around(self, &self->qos_tx);
1452
1453 irlap_next_state(self, LAP_XMIT_P);
1454
1455 /* Start poll timer */
1456 irlap_start_poll_timer(self, self->poll_timeout);
1457 } else if (ret == NR_UNEXPECTED) {
1458 IRDA_ASSERT(info != NULL, return -1;);
1459 /*
1460 * Unexpected nr!
1461 */
1462
1463 /* Update Nr received */
1464 irlap_update_nr_received(self, info->nr);
1465
1466 IRDA_DEBUG(4, "RECV_RR_FRAME: Retrans:%d, nr=%d, va=%d, "
1467 "vs=%d, vr=%d\n",
1468 self->retry_count, info->nr, self->va,
1469 self->vs, self->vr);
1470
1471 /* Resend rejected frames */
1472 irlap_resend_rejected_frames(self, CMD_FRAME);
G. Liakhovetskib7e773b2007-06-08 19:15:56 -07001473 irlap_start_final_timer(self, self->final_timeout * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 irlap_next_state(self, LAP_NRM_P);
1476 } else if (ret == NR_INVALID) {
1477 IRDA_DEBUG(1, "%s(), Received RR with "
Harvey Harrison0dc47872008-03-05 20:47:47 -08001478 "invalid nr !\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 irlap_next_state(self, LAP_RESET_WAIT);
1481
1482 irlap_disconnect_indication(self, LAP_RESET_INDICATION);
1483 self->xmitflag = TRUE;
1484 }
1485 break;
1486 case RECV_RNR_RSP:
1487 IRDA_ASSERT(info != NULL, return -1;);
1488
1489 /* Stop final timer */
1490 del_timer(&self->final_timer);
1491 self->remote_busy = TRUE;
1492
1493 /* Update Nr received */
1494 irlap_update_nr_received(self, info->nr);
1495 irlap_next_state(self, LAP_XMIT_P);
1496
1497 /* Start poll timer */
1498 irlap_start_poll_timer(self, self->poll_timeout);
1499 break;
1500 case RECV_FRMR_RSP:
1501 del_timer(&self->final_timer);
1502 self->xmitflag = TRUE;
1503 irlap_next_state(self, LAP_RESET_WAIT);
1504 irlap_reset_indication(self);
1505 break;
1506 case FINAL_TIMER_EXPIRED:
1507 /*
1508 * We are allowed to wait for additional 300 ms if
1509 * final timer expires when we are in the middle
1510 * of receiving a frame (page 45, IrLAP). Check that
1511 * we only do this once for each frame.
1512 */
1513 if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
1514 IRDA_DEBUG(1, "FINAL_TIMER_EXPIRED when receiving a "
1515 "frame! Waiting a little bit more!\n");
1516 irlap_start_final_timer(self, msecs_to_jiffies(300));
1517
1518 /*
1519 * Don't allow this to happen one more time in a row,
1520 * or else we can get a pretty tight loop here if
1521 * if we only receive half a frame. DB.
1522 */
1523 self->add_wait = TRUE;
1524 break;
1525 }
1526 self->add_wait = FALSE;
1527
1528 /* N2 is the disconnect timer. Until we reach it, we retry */
1529 if (self->retry_count < self->N2) {
Robie Basak6d97b532008-01-18 23:56:54 -08001530 if (skb_peek(&self->wx_list) == NULL) {
1531 /* Retry sending the pf bit to the secondary */
1532 IRDA_DEBUG(4, "nrm_p: resending rr");
1533 irlap_wait_min_turn_around(self, &self->qos_tx);
1534 irlap_send_rr_frame(self, CMD_FRAME);
1535 } else {
1536 IRDA_DEBUG(4, "nrm_p: resend frames");
1537 irlap_resend_rejected_frames(self, CMD_FRAME);
1538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540 irlap_start_final_timer(self, self->final_timeout);
1541 self->retry_count++;
1542 IRDA_DEBUG(4, "irlap_state_nrm_p: FINAL_TIMER_EXPIRED:"
1543 " retry_count=%d\n", self->retry_count);
1544
1545 /* Early warning event. I'm using a pretty liberal
1546 * interpretation of the spec and generate an event
1547 * every time the timer is multiple of N1 (and not
1548 * only the first time). This allow application
1549 * to know precisely if connectivity restart...
1550 * Jean II */
1551 if((self->retry_count % self->N1) == 0)
1552 irlap_status_indication(self,
1553 STATUS_NO_ACTIVITY);
1554
1555 /* Keep state */
1556 } else {
1557 irlap_apply_default_connection_parameters(self);
1558
1559 /* Always switch state before calling upper layers */
1560 irlap_next_state(self, LAP_NDM);
1561 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1562 }
1563 break;
1564 case RECV_REJ_RSP:
1565 irlap_update_nr_received(self, info->nr);
1566 if (self->remote_busy) {
1567 irlap_wait_min_turn_around(self, &self->qos_tx);
1568 irlap_send_rr_frame(self, CMD_FRAME);
1569 } else
1570 irlap_resend_rejected_frames(self, CMD_FRAME);
1571 irlap_start_final_timer(self, 2 * self->final_timeout);
1572 break;
1573 case RECV_SREJ_RSP:
1574 irlap_update_nr_received(self, info->nr);
1575 if (self->remote_busy) {
1576 irlap_wait_min_turn_around(self, &self->qos_tx);
1577 irlap_send_rr_frame(self, CMD_FRAME);
1578 } else
1579 irlap_resend_rejected_frame(self, CMD_FRAME);
1580 irlap_start_final_timer(self, 2 * self->final_timeout);
1581 break;
1582 case RECV_RD_RSP:
Harvey Harrison0dc47872008-03-05 20:47:47 -08001583 IRDA_DEBUG(1, "%s(), RECV_RD_RSP\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 irlap_flush_all_queues(self);
1586 irlap_next_state(self, LAP_XMIT_P);
1587 /* Call back the LAP state machine to do a proper disconnect */
1588 irlap_disconnect_request(self);
1589 break;
1590 default:
1591 IRDA_DEBUG(1, "%s(), Unknown event %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001592 __func__, irlap_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 ret = -1;
1595 break;
1596 }
1597 return ret;
1598}
1599
1600/*
1601 * Function irlap_state_reset_wait (event, skb, info)
1602 *
1603 * We have informed the service user of a reset condition, and is
1604 * awaiting reset of disconnect request.
1605 *
1606 */
1607static int irlap_state_reset_wait(struct irlap_cb *self, IRLAP_EVENT event,
1608 struct sk_buff *skb, struct irlap_info *info)
1609{
1610 int ret = 0;
1611
Harvey Harrison0dc47872008-03-05 20:47:47 -08001612 IRDA_DEBUG(3, "%s(), event = %s\n", __func__, irlap_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 IRDA_ASSERT(self != NULL, return -1;);
1615 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
1616
1617 switch (event) {
1618 case RESET_REQUEST:
1619 if (self->xmitflag) {
1620 irlap_wait_min_turn_around(self, &self->qos_tx);
1621 irlap_send_snrm_frame(self, NULL);
1622 irlap_start_final_timer(self, self->final_timeout);
1623 irlap_next_state(self, LAP_RESET);
1624 } else {
1625 irlap_start_final_timer(self, self->final_timeout);
1626 irlap_next_state(self, LAP_RESET);
1627 }
1628 break;
1629 case DISCONNECT_REQUEST:
1630 irlap_wait_min_turn_around( self, &self->qos_tx);
1631 irlap_send_disc_frame( self);
1632 irlap_flush_all_queues( self);
1633 irlap_start_final_timer( self, self->final_timeout);
1634 self->retry_count = 0;
1635 irlap_next_state( self, LAP_PCLOSE);
1636 break;
1637 default:
Harvey Harrison0dc47872008-03-05 20:47:47 -08001638 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 irlap_event[event]);
1640
1641 ret = -1;
1642 break;
1643 }
1644 return ret;
1645}
1646
1647/*
1648 * Function irlap_state_reset (self, event, skb, info)
1649 *
1650 * We have sent a SNRM reset command to the peer layer, and is awaiting
1651 * reply.
1652 *
1653 */
1654static int irlap_state_reset(struct irlap_cb *self, IRLAP_EVENT event,
1655 struct sk_buff *skb, struct irlap_info *info)
1656{
1657 int ret = 0;
1658
Harvey Harrison0dc47872008-03-05 20:47:47 -08001659 IRDA_DEBUG(3, "%s(), event = %s\n", __func__, irlap_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 IRDA_ASSERT(self != NULL, return -1;);
1662 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
1663
1664 switch (event) {
1665 case RECV_DISC_CMD:
1666 del_timer(&self->final_timer);
1667
1668 irlap_apply_default_connection_parameters(self);
1669
1670 /* Always switch state before calling upper layers */
1671 irlap_next_state(self, LAP_NDM);
1672
1673 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1674
1675 break;
1676 case RECV_UA_RSP:
1677 del_timer(&self->final_timer);
1678
1679 /* Initiate connection state */
1680 irlap_initiate_connection_state(self);
1681
1682 irlap_reset_confirm();
1683
1684 self->remote_busy = FALSE;
1685
1686 irlap_next_state(self, LAP_XMIT_P);
1687
1688 irlap_start_poll_timer(self, self->poll_timeout);
1689
1690 break;
1691 case FINAL_TIMER_EXPIRED:
1692 if (self->retry_count < 3) {
1693 irlap_wait_min_turn_around(self, &self->qos_tx);
1694
1695 IRDA_ASSERT(self->netdev != NULL, return -1;);
1696 irlap_send_snrm_frame(self, self->qos_dev);
1697
1698 self->retry_count++; /* Experimental!! */
1699
1700 irlap_start_final_timer(self, self->final_timeout);
1701 irlap_next_state(self, LAP_RESET);
1702 } else if (self->retry_count >= self->N3) {
1703 irlap_apply_default_connection_parameters(self);
1704
1705 /* Always switch state before calling upper layers */
1706 irlap_next_state(self, LAP_NDM);
1707
1708 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
1709 }
1710 break;
1711 case RECV_SNRM_CMD:
1712 /*
1713 * SNRM frame is not allowed to contain an I-field in this
1714 * state
1715 */
1716 if (!info) {
Harvey Harrison0dc47872008-03-05 20:47:47 -08001717 IRDA_DEBUG(3, "%s(), RECV_SNRM_CMD\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 irlap_initiate_connection_state(self);
1719 irlap_wait_min_turn_around(self, &self->qos_tx);
1720 irlap_send_ua_response_frame(self, &self->qos_rx);
1721 irlap_reset_confirm();
1722 irlap_start_wd_timer(self, self->wd_timeout);
1723 irlap_next_state(self, LAP_NDM);
1724 } else {
1725 IRDA_DEBUG(0,
1726 "%s(), SNRM frame contained an I field!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001727 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729 break;
1730 default:
1731 IRDA_DEBUG(1, "%s(), Unknown event %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001732 __func__, irlap_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 ret = -1;
1735 break;
1736 }
1737 return ret;
1738}
1739
1740/*
1741 * Function irlap_state_xmit_s (event, skb, info)
1742 *
1743 * XMIT_S, The secondary station has been given the right to transmit,
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001744 * and we therefore do not expect to receive any transmissions from other
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 * stations.
1746 */
1747static int irlap_state_xmit_s(struct irlap_cb *self, IRLAP_EVENT event,
1748 struct sk_buff *skb, struct irlap_info *info)
1749{
1750 int ret = 0;
1751
Harvey Harrison0dc47872008-03-05 20:47:47 -08001752 IRDA_DEBUG(4, "%s(), event=%s\n", __func__, irlap_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 IRDA_ASSERT(self != NULL, return -ENODEV;);
1755 IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
1756
1757 switch (event) {
1758 case SEND_I_CMD:
1759 /*
1760 * Send frame only if send window > 0
1761 */
1762 if ((self->window > 0) && (!self->remote_busy)) {
1763 int nextfit;
1764#ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1765 struct sk_buff *skb_next;
1766
1767 /*
1768 * Same deal as in irlap_state_xmit_p(), so see
1769 * the comments at that point.
1770 * We are the secondary, so there are only subtle
1771 * differences. - Jean II
1772 */
1773
1774 /* Check if a subsequent skb exist and would fit in
1775 * the current window (with respect to turnaround
1776 * time). - Jean II */
1777 skb_next = skb_peek(&self->txq);
1778 nextfit = ((skb_next != NULL) &&
1779 ((skb_next->len + skb->len) <=
1780 self->bytes_left));
1781
1782 /*
1783 * Test if we have transmitted more bytes over the
1784 * link than its possible to do with the current
1785 * speed and turn-around-time.
1786 */
1787 if((!nextfit) && (skb->len > self->bytes_left)) {
1788 IRDA_DEBUG(0, "%s(), Not allowed to transmit"
Harvey Harrison0dc47872008-03-05 20:47:47 -08001789 " more bytes!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 /* Requeue the skb */
1791 skb_queue_head(&self->txq, skb_get(skb));
1792
1793 /*
1794 * Switch to NRM_S, this is only possible
1795 * when we are in secondary mode, since we
1796 * must be sure that we don't miss any RR
1797 * frames
1798 */
1799 self->window = self->window_size;
1800 self->bytes_left = self->line_capacity;
1801 irlap_start_wd_timer(self, self->wd_timeout);
1802
1803 irlap_next_state(self, LAP_NRM_S);
1804 /* Slight difference with primary :
1805 * here we would wait for the other side to
1806 * expire the turnaround. - Jean II */
1807
1808 return -EPROTO; /* Try again later */
1809 }
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001810 /* Subtract space used by this skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 self->bytes_left -= skb->len;
1812#else /* CONFIG_IRDA_DYNAMIC_WINDOW */
1813 /* Window has been adjusted for the max packet
1814 * size, so much simpler... - Jean II */
David S. Millerb03efcf2005-07-08 14:57:23 -07001815 nextfit = !skb_queue_empty(&self->txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816#endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1817 /*
1818 * Send data with final bit cleared only if window > 1
1819 * and there is more frames to be sent
1820 */
1821 if ((self->window > 1) && (nextfit)) {
1822 irlap_send_data_secondary(self, skb);
1823 irlap_next_state(self, LAP_XMIT_S);
1824 } else {
1825 irlap_send_data_secondary_final(self, skb);
1826 irlap_next_state(self, LAP_NRM_S);
1827
1828 /*
1829 * Make sure state machine does not try to send
1830 * any more frames
1831 */
1832 ret = -EPROTO;
1833 }
1834 } else {
Harvey Harrison0dc47872008-03-05 20:47:47 -08001835 IRDA_DEBUG(2, "%s(), Unable to send!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 skb_queue_head(&self->txq, skb_get(skb));
1837 ret = -EPROTO;
1838 }
1839 break;
1840 case DISCONNECT_REQUEST:
1841 irlap_send_rd_frame(self);
1842 irlap_flush_all_queues(self);
1843 irlap_start_wd_timer(self, self->wd_timeout);
1844 irlap_next_state(self, LAP_SCLOSE);
1845 break;
1846 case DATA_REQUEST:
1847 /* Nothing to do, irlap_do_event() will send the packet
1848 * when we return... - Jean II */
1849 break;
1850 default:
Harvey Harrison0dc47872008-03-05 20:47:47 -08001851 IRDA_DEBUG(2, "%s(), Unknown event %s\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 irlap_event[event]);
1853
1854 ret = -EINVAL;
1855 break;
1856 }
1857 return ret;
1858}
1859
1860/*
1861 * Function irlap_state_nrm_s (event, skb, info)
1862 *
1863 * NRM_S (Normal Response Mode as Secondary) state, in this state we are
1864 * expecting to receive frames from the primary station
1865 *
1866 */
1867static int irlap_state_nrm_s(struct irlap_cb *self, IRLAP_EVENT event,
1868 struct sk_buff *skb, struct irlap_info *info)
1869{
1870 int ns_status;
1871 int nr_status;
1872 int ret = 0;
1873
Harvey Harrison0dc47872008-03-05 20:47:47 -08001874 IRDA_DEBUG(4, "%s(), event=%s\n", __func__, irlap_event[ event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 IRDA_ASSERT(self != NULL, return -1;);
1877 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
1878
1879 switch (event) {
1880 case RECV_I_CMD: /* Optimize for the common case */
1881 /* FIXME: must check for remote_busy below */
1882 IRDA_DEBUG(4, "%s(), event=%s nr=%d, vs=%d, ns=%d, "
Harvey Harrison0dc47872008-03-05 20:47:47 -08001883 "vr=%d, pf=%d\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 irlap_event[event], info->nr,
1885 self->vs, info->ns, self->vr, info->pf);
1886
1887 self->retry_count = 0;
1888
1889 ns_status = irlap_validate_ns_received(self, info->ns);
1890 nr_status = irlap_validate_nr_received(self, info->nr);
1891 /*
1892 * Check for expected I(nformation) frame
1893 */
1894 if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
1895
1896 /* Update Vr (next frame for us to receive) */
1897 self->vr = (self->vr + 1) % 8;
1898
1899 /* Update Nr received */
1900 irlap_update_nr_received(self, info->nr);
1901
1902 /*
1903 * poll bit cleared?
1904 */
1905 if (!info->pf) {
1906
1907 self->ack_required = TRUE;
1908
1909 /*
1910 * Starting WD-timer here is optional, but
1911 * not recommended. Note 6 IrLAP p. 83
1912 */
1913#if 0
1914 irda_start_timer(WD_TIMER, self->wd_timeout);
1915#endif
1916 /* Keep state, do not move this line */
1917 irlap_next_state(self, LAP_NRM_S);
1918
1919 irlap_data_indication(self, skb, FALSE);
1920 break;
1921 } else {
1922 /*
1923 * We should wait before sending RR, and
1924 * also before changing to XMIT_S
1925 * state. (note 1, IrLAP p. 82)
1926 */
1927 irlap_wait_min_turn_around(self, &self->qos_tx);
1928
1929 /*
1930 * Give higher layers a chance to
1931 * immediately reply with some data before
1932 * we decide if we should send a RR frame
1933 * or not
1934 */
1935 irlap_data_indication(self, skb, FALSE);
1936
1937 /* Any pending data requests? */
David S. Millerb03efcf2005-07-08 14:57:23 -07001938 if (!skb_queue_empty(&self->txq) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 (self->window > 0))
1940 {
1941 self->ack_required = TRUE;
1942
1943 del_timer(&self->wd_timer);
1944
1945 irlap_next_state(self, LAP_XMIT_S);
1946 } else {
1947 irlap_send_rr_frame(self, RSP_FRAME);
1948 irlap_start_wd_timer(self,
1949 self->wd_timeout);
1950
1951 /* Keep the state */
1952 irlap_next_state(self, LAP_NRM_S);
1953 }
1954 break;
1955 }
1956 }
1957 /*
1958 * Check for Unexpected next to send (Ns)
1959 */
1960 if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
1961 {
1962 /* Unexpected next to send, with final bit cleared */
1963 if (!info->pf) {
1964 irlap_update_nr_received(self, info->nr);
1965
1966 irlap_start_wd_timer(self, self->wd_timeout);
1967 } else {
1968 /* Update Nr received */
1969 irlap_update_nr_received(self, info->nr);
1970
1971 irlap_wait_min_turn_around(self, &self->qos_tx);
1972 irlap_send_rr_frame(self, RSP_FRAME);
1973
1974 irlap_start_wd_timer(self, self->wd_timeout);
1975 }
1976 break;
1977 }
1978
1979 /*
1980 * Unexpected Next to Receive(NR) ?
1981 */
1982 if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
1983 {
1984 if (info->pf) {
1985 IRDA_DEBUG(4, "RECV_I_RSP: frame(s) lost\n");
1986
1987 self->vr = (self->vr + 1) % 8;
1988
1989 /* Update Nr received */
1990 irlap_update_nr_received(self, info->nr);
1991
1992 /* Resend rejected frames */
1993 irlap_resend_rejected_frames(self, RSP_FRAME);
1994
1995 /* Keep state, do not move this line */
1996 irlap_next_state(self, LAP_NRM_S);
1997
1998 irlap_data_indication(self, skb, FALSE);
1999 irlap_start_wd_timer(self, self->wd_timeout);
2000 break;
2001 }
2002 /*
2003 * This is not documented in IrLAP!! Unexpected NR
2004 * with poll bit cleared
2005 */
2006 if (!info->pf) {
2007 self->vr = (self->vr + 1) % 8;
2008
2009 /* Update Nr received */
2010 irlap_update_nr_received(self, info->nr);
2011
2012 /* Keep state, do not move this line */
2013 irlap_next_state(self, LAP_NRM_S);
2014
2015 irlap_data_indication(self, skb, FALSE);
2016 irlap_start_wd_timer(self, self->wd_timeout);
2017 }
2018 break;
2019 }
2020
2021 if (ret == NR_INVALID) {
2022 IRDA_DEBUG(0, "NRM_S, NR_INVALID not implemented!\n");
2023 }
2024 if (ret == NS_INVALID) {
2025 IRDA_DEBUG(0, "NRM_S, NS_INVALID not implemented!\n");
2026 }
2027 break;
2028 case RECV_UI_FRAME:
2029 /*
2030 * poll bit cleared?
2031 */
2032 if (!info->pf) {
2033 irlap_data_indication(self, skb, TRUE);
2034 irlap_next_state(self, LAP_NRM_S); /* Keep state */
2035 } else {
2036 /*
2037 * Any pending data requests?
2038 */
David S. Millerb03efcf2005-07-08 14:57:23 -07002039 if (!skb_queue_empty(&self->txq) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 (self->window > 0) && !self->remote_busy)
2041 {
2042 irlap_data_indication(self, skb, TRUE);
2043
2044 del_timer(&self->wd_timer);
2045
2046 irlap_next_state(self, LAP_XMIT_S);
2047 } else {
2048 irlap_data_indication(self, skb, TRUE);
2049
2050 irlap_wait_min_turn_around(self, &self->qos_tx);
2051
2052 irlap_send_rr_frame(self, RSP_FRAME);
2053 self->ack_required = FALSE;
2054
2055 irlap_start_wd_timer(self, self->wd_timeout);
2056
2057 /* Keep the state */
2058 irlap_next_state(self, LAP_NRM_S);
2059 }
2060 }
2061 break;
2062 case RECV_RR_CMD:
2063 self->retry_count = 0;
2064
2065 /*
2066 * Nr as expected?
2067 */
2068 nr_status = irlap_validate_nr_received(self, info->nr);
2069 if (nr_status == NR_EXPECTED) {
David S. Millerb03efcf2005-07-08 14:57:23 -07002070 if (!skb_queue_empty(&self->txq) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 (self->window > 0)) {
2072 self->remote_busy = FALSE;
2073
2074 /* Update Nr received */
2075 irlap_update_nr_received(self, info->nr);
2076 del_timer(&self->wd_timer);
2077
2078 irlap_wait_min_turn_around(self, &self->qos_tx);
2079 irlap_next_state(self, LAP_XMIT_S);
2080 } else {
2081 self->remote_busy = FALSE;
2082 /* Update Nr received */
2083 irlap_update_nr_received(self, info->nr);
2084 irlap_wait_min_turn_around(self, &self->qos_tx);
2085 irlap_start_wd_timer(self, self->wd_timeout);
2086
2087 /* Note : if the link is idle (this case),
2088 * we never go in XMIT_S, so we never get a
2089 * chance to process any DISCONNECT_REQUEST.
2090 * Do it now ! - Jean II */
2091 if (self->disconnect_pending) {
2092 /* Disconnect */
2093 irlap_send_rd_frame(self);
2094 irlap_flush_all_queues(self);
2095
2096 irlap_next_state(self, LAP_SCLOSE);
2097 } else {
2098 /* Just send back pf bit */
2099 irlap_send_rr_frame(self, RSP_FRAME);
2100
2101 irlap_next_state(self, LAP_NRM_S);
2102 }
2103 }
2104 } else if (nr_status == NR_UNEXPECTED) {
2105 self->remote_busy = FALSE;
2106 irlap_update_nr_received(self, info->nr);
2107 irlap_resend_rejected_frames(self, RSP_FRAME);
2108
2109 irlap_start_wd_timer(self, self->wd_timeout);
2110
2111 /* Keep state */
2112 irlap_next_state(self, LAP_NRM_S);
2113 } else {
2114 IRDA_DEBUG(1, "%s(), invalid nr not implemented!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002115 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
2117 break;
2118 case RECV_SNRM_CMD:
2119 /* SNRM frame is not allowed to contain an I-field */
2120 if (!info) {
2121 del_timer(&self->wd_timer);
Harvey Harrison0dc47872008-03-05 20:47:47 -08002122 IRDA_DEBUG(1, "%s(), received SNRM cmd\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 irlap_next_state(self, LAP_RESET_CHECK);
2124
2125 irlap_reset_indication(self);
2126 } else {
2127 IRDA_DEBUG(0,
2128 "%s(), SNRM frame contained an I-field!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002129 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131 }
2132 break;
2133 case RECV_REJ_CMD:
2134 irlap_update_nr_received(self, info->nr);
2135 if (self->remote_busy) {
2136 irlap_wait_min_turn_around(self, &self->qos_tx);
2137 irlap_send_rr_frame(self, RSP_FRAME);
2138 } else
2139 irlap_resend_rejected_frames(self, RSP_FRAME);
2140 irlap_start_wd_timer(self, self->wd_timeout);
2141 break;
2142 case RECV_SREJ_CMD:
2143 irlap_update_nr_received(self, info->nr);
2144 if (self->remote_busy) {
2145 irlap_wait_min_turn_around(self, &self->qos_tx);
2146 irlap_send_rr_frame(self, RSP_FRAME);
2147 } else
2148 irlap_resend_rejected_frame(self, RSP_FRAME);
2149 irlap_start_wd_timer(self, self->wd_timeout);
2150 break;
2151 case WD_TIMER_EXPIRED:
2152 /*
2153 * Wait until retry_count * n matches negotiated threshold/
2154 * disconnect time (note 2 in IrLAP p. 82)
2155 *
2156 * Similar to irlap_state_nrm_p() -> FINAL_TIMER_EXPIRED
2157 * Note : self->wd_timeout = (self->final_timeout * 2),
2158 * which explain why we use (self->N2 / 2) here !!!
2159 * Jean II
2160 */
Harvey Harrison0dc47872008-03-05 20:47:47 -08002161 IRDA_DEBUG(1, "%s(), retry_count = %d\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 self->retry_count);
2163
2164 if (self->retry_count < (self->N2 / 2)) {
2165 /* No retry, just wait for primary */
2166 irlap_start_wd_timer(self, self->wd_timeout);
2167 self->retry_count++;
2168
2169 if((self->retry_count % (self->N1 / 2)) == 0)
2170 irlap_status_indication(self,
2171 STATUS_NO_ACTIVITY);
2172 } else {
2173 irlap_apply_default_connection_parameters(self);
2174
2175 /* Always switch state before calling upper layers */
2176 irlap_next_state(self, LAP_NDM);
2177 irlap_disconnect_indication(self, LAP_NO_RESPONSE);
2178 }
2179 break;
2180 case RECV_DISC_CMD:
2181 /* Always switch state before calling upper layers */
2182 irlap_next_state(self, LAP_NDM);
2183
2184 /* Send disconnect response */
2185 irlap_wait_min_turn_around(self, &self->qos_tx);
2186 irlap_send_ua_response_frame(self, NULL);
2187
2188 del_timer(&self->wd_timer);
2189 irlap_flush_all_queues(self);
2190 /* Set default link parameters */
2191 irlap_apply_default_connection_parameters(self);
2192
2193 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2194 break;
2195 case RECV_DISCOVERY_XID_CMD:
2196 irlap_wait_min_turn_around(self, &self->qos_tx);
2197 irlap_send_rr_frame(self, RSP_FRAME);
2198 self->ack_required = TRUE;
2199 irlap_start_wd_timer(self, self->wd_timeout);
2200 irlap_next_state(self, LAP_NRM_S);
2201
2202 break;
2203 case RECV_TEST_CMD:
2204 /* Remove test frame header (only LAP header in NRM) */
2205 skb_pull(skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
2206
2207 irlap_wait_min_turn_around(self, &self->qos_tx);
2208 irlap_start_wd_timer(self, self->wd_timeout);
2209
2210 /* Send response (info will be copied) */
2211 irlap_send_test_frame(self, self->caddr, info->daddr, skb);
2212 break;
2213 default:
Harvey Harrison0dc47872008-03-05 20:47:47 -08002214 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 event, irlap_event[event]);
2216
2217 ret = -EINVAL;
2218 break;
2219 }
2220 return ret;
2221}
2222
2223/*
2224 * Function irlap_state_sclose (self, event, skb, info)
2225 */
2226static int irlap_state_sclose(struct irlap_cb *self, IRLAP_EVENT event,
2227 struct sk_buff *skb, struct irlap_info *info)
2228{
Harvey Harrison0dc47872008-03-05 20:47:47 -08002229 IRDA_DEBUG(1, "%s()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231 IRDA_ASSERT(self != NULL, return -ENODEV;);
2232 IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
2233
2234 switch (event) {
2235 case RECV_DISC_CMD:
2236 /* Always switch state before calling upper layers */
2237 irlap_next_state(self, LAP_NDM);
2238
2239 /* Send disconnect response */
2240 irlap_wait_min_turn_around(self, &self->qos_tx);
2241 irlap_send_ua_response_frame(self, NULL);
2242
2243 del_timer(&self->wd_timer);
2244 /* Set default link parameters */
2245 irlap_apply_default_connection_parameters(self);
2246
2247 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2248 break;
2249 case RECV_DM_RSP:
2250 /* IrLAP-1.1 p.82: in SCLOSE, S and I type RSP frames
2251 * shall take us down into default NDM state, like DM_RSP
2252 */
2253 case RECV_RR_RSP:
2254 case RECV_RNR_RSP:
2255 case RECV_REJ_RSP:
2256 case RECV_SREJ_RSP:
2257 case RECV_I_RSP:
2258 /* Always switch state before calling upper layers */
2259 irlap_next_state(self, LAP_NDM);
2260
2261 del_timer(&self->wd_timer);
2262 irlap_apply_default_connection_parameters(self);
2263
2264 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2265 break;
2266 case WD_TIMER_EXPIRED:
2267 /* Always switch state before calling upper layers */
2268 irlap_next_state(self, LAP_NDM);
2269
2270 irlap_apply_default_connection_parameters(self);
2271
2272 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
2273 break;
2274 default:
2275 /* IrLAP-1.1 p.82: in SCLOSE, basically any received frame
2276 * with pf=1 shall restart the wd-timer and resend the rd:rsp
2277 */
2278 if (info != NULL && info->pf) {
2279 del_timer(&self->wd_timer);
2280 irlap_wait_min_turn_around(self, &self->qos_tx);
2281 irlap_send_rd_frame(self);
2282 irlap_start_wd_timer(self, self->wd_timeout);
2283 break; /* stay in SCLOSE */
2284 }
2285
Harvey Harrison0dc47872008-03-05 20:47:47 -08002286 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 event, irlap_event[event]);
2288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 break;
2290 }
2291
2292 return -1;
2293}
2294
2295static int irlap_state_reset_check( struct irlap_cb *self, IRLAP_EVENT event,
2296 struct sk_buff *skb,
2297 struct irlap_info *info)
2298{
2299 int ret = 0;
2300
Harvey Harrison0dc47872008-03-05 20:47:47 -08002301 IRDA_DEBUG(1, "%s(), event=%s\n", __func__, irlap_event[event]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 IRDA_ASSERT(self != NULL, return -ENODEV;);
2304 IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
2305
2306 switch (event) {
2307 case RESET_RESPONSE:
2308 irlap_send_ua_response_frame(self, &self->qos_rx);
2309 irlap_initiate_connection_state(self);
2310 irlap_start_wd_timer(self, WD_TIMEOUT);
2311 irlap_flush_all_queues(self);
2312
2313 irlap_next_state(self, LAP_NRM_S);
2314 break;
2315 case DISCONNECT_REQUEST:
2316 irlap_wait_min_turn_around(self, &self->qos_tx);
2317 irlap_send_rd_frame(self);
2318 irlap_start_wd_timer(self, WD_TIMEOUT);
2319 irlap_next_state(self, LAP_SCLOSE);
2320 break;
2321 default:
Harvey Harrison0dc47872008-03-05 20:47:47 -08002322 IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 event, irlap_event[event]);
2324
2325 ret = -EINVAL;
2326 break;
2327 }
2328 return ret;
2329}