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