forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
irlap_event.c
2330 lines (2019 loc) · 65 KB
/
irlap_event.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************
*
* Filename: irlap_event.c
* Version: 0.9
* Description: IrLAP state machine implementation
* Status: Experimental.
* Author: Dag Brattli <[email protected]>
* Created at: Sat Aug 16 00:59:29 1997
* Modified at: Sat Dec 25 21:07:57 1999
* Modified by: Dag Brattli <[email protected]>
*
* Copyright (c) 1998-2000 Dag Brattli <[email protected]>,
* Copyright (c) 1998 Thomas Davis <[email protected]>
* All Rights Reserved.
* Copyright (c) 2000-2003 Jean Tourrilhes <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* Neither Dag Brattli nor University of Tromsø admit liability nor
* provide warranty for any of this software. This material is
* provided "AS-IS" and at no charge.
*
********************************************************************/
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/skbuff.h>
#include <linux/slab.h>
#include <net/irda/irda.h>
#include <net/irda/irlap_event.h>
#include <net/irda/timer.h>
#include <net/irda/irlap.h>
#include <net/irda/irlap_frame.h>
#include <net/irda/qos.h>
#include <net/irda/parameters.h>
#include <net/irda/irlmp.h> /* irlmp_flow_indication(), ... */
#include <net/irda/irda_device.h>
#ifdef CONFIG_IRDA_FAST_RR
int sysctl_fast_poll_increase = 50;
#endif
static int irlap_state_ndm (struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info);
static int irlap_state_query (struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info);
static int irlap_state_reply (struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info);
static int irlap_state_conn (struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info);
static int irlap_state_setup (struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info);
static int irlap_state_offline(struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info);
static int irlap_state_xmit_p (struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info);
static int irlap_state_pclose (struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info);
static int irlap_state_nrm_p (struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info);
static int irlap_state_reset_wait(struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info);
static int irlap_state_reset (struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info);
static int irlap_state_nrm_s (struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info);
static int irlap_state_xmit_s (struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info);
static int irlap_state_sclose (struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info);
static int irlap_state_reset_check(struct irlap_cb *, IRLAP_EVENT event,
struct sk_buff *, struct irlap_info *);
#ifdef CONFIG_IRDA_DEBUG
static const char *const irlap_event[] = {
"DISCOVERY_REQUEST",
"CONNECT_REQUEST",
"CONNECT_RESPONSE",
"DISCONNECT_REQUEST",
"DATA_REQUEST",
"RESET_REQUEST",
"RESET_RESPONSE",
"SEND_I_CMD",
"SEND_UI_FRAME",
"RECV_DISCOVERY_XID_CMD",
"RECV_DISCOVERY_XID_RSP",
"RECV_SNRM_CMD",
"RECV_TEST_CMD",
"RECV_TEST_RSP",
"RECV_UA_RSP",
"RECV_DM_RSP",
"RECV_RD_RSP",
"RECV_I_CMD",
"RECV_I_RSP",
"RECV_UI_FRAME",
"RECV_FRMR_RSP",
"RECV_RR_CMD",
"RECV_RR_RSP",
"RECV_RNR_CMD",
"RECV_RNR_RSP",
"RECV_REJ_CMD",
"RECV_REJ_RSP",
"RECV_SREJ_CMD",
"RECV_SREJ_RSP",
"RECV_DISC_CMD",
"SLOT_TIMER_EXPIRED",
"QUERY_TIMER_EXPIRED",
"FINAL_TIMER_EXPIRED",
"POLL_TIMER_EXPIRED",
"DISCOVERY_TIMER_EXPIRED",
"WD_TIMER_EXPIRED",
"BACKOFF_TIMER_EXPIRED",
"MEDIA_BUSY_TIMER_EXPIRED",
};
#endif /* CONFIG_IRDA_DEBUG */
const char *const irlap_state[] = {
"LAP_NDM",
"LAP_QUERY",
"LAP_REPLY",
"LAP_CONN",
"LAP_SETUP",
"LAP_OFFLINE",
"LAP_XMIT_P",
"LAP_PCLOSE",
"LAP_NRM_P",
"LAP_RESET_WAIT",
"LAP_RESET",
"LAP_NRM_S",
"LAP_XMIT_S",
"LAP_SCLOSE",
"LAP_RESET_CHECK",
};
static int (*state[])(struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info) =
{
irlap_state_ndm,
irlap_state_query,
irlap_state_reply,
irlap_state_conn,
irlap_state_setup,
irlap_state_offline,
irlap_state_xmit_p,
irlap_state_pclose,
irlap_state_nrm_p,
irlap_state_reset_wait,
irlap_state_reset,
irlap_state_nrm_s,
irlap_state_xmit_s,
irlap_state_sclose,
irlap_state_reset_check,
};
/*
* Function irda_poll_timer_expired (data)
*
* Poll timer has expired. Normally we must now send a RR frame to the
* remote device
*/
static void irlap_poll_timer_expired(void *data)
{
struct irlap_cb *self = (struct irlap_cb *) data;
IRDA_ASSERT(self != NULL, return;);
IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
irlap_do_event(self, POLL_TIMER_EXPIRED, NULL, NULL);
}
/*
* Calculate and set time before we will have to send back the pf bit
* to the peer. Use in primary.
* Make sure that state is XMIT_P/XMIT_S when calling this function
* (and that nobody messed up with the state). - Jean II
*/
static void irlap_start_poll_timer(struct irlap_cb *self, int timeout)
{
IRDA_ASSERT(self != NULL, return;);
IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
#ifdef CONFIG_IRDA_FAST_RR
/*
* Send out the RR frames faster if our own transmit queue is empty, or
* if the peer is busy. The effect is a much faster conversation
*/
if (skb_queue_empty(&self->txq) || self->remote_busy) {
if (self->fast_RR == TRUE) {
/*
* Assert that the fast poll timer has not reached the
* normal poll timer yet
*/
if (self->fast_RR_timeout < timeout) {
/*
* FIXME: this should be a more configurable
* function
*/
self->fast_RR_timeout +=
(sysctl_fast_poll_increase * HZ/1000);
/* Use this fast(er) timeout instead */
timeout = self->fast_RR_timeout;
}
} else {
self->fast_RR = TRUE;
/* Start with just 0 ms */
self->fast_RR_timeout = 0;
timeout = 0;
}
} else
self->fast_RR = FALSE;
IRDA_DEBUG(3, "%s(), timeout=%d (%ld)\n", __func__, timeout, jiffies);
#endif /* CONFIG_IRDA_FAST_RR */
if (timeout == 0)
irlap_do_event(self, POLL_TIMER_EXPIRED, NULL, NULL);
else
irda_start_timer(&self->poll_timer, timeout, self,
irlap_poll_timer_expired);
}
/*
* Function irlap_do_event (event, skb, info)
*
* Rushes through the state machine without any delay. If state == XMIT
* then send queued data frames.
*/
void irlap_do_event(struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info)
{
int ret;
if (!self || self->magic != LAP_MAGIC)
return;
IRDA_DEBUG(3, "%s(), event = %s, state = %s\n", __func__,
irlap_event[event], irlap_state[self->state]);
ret = (*state[self->state])(self, event, skb, info);
/*
* Check if there are any pending events that needs to be executed
*/
switch (self->state) {
case LAP_XMIT_P: /* FALLTHROUGH */
case LAP_XMIT_S:
/*
* We just received the pf bit and are at the beginning
* of a new LAP transmit window.
* Check if there are any queued data frames, and do not
* try to disconnect link if we send any data frames, since
* that will change the state away form XMIT
*/
IRDA_DEBUG(2, "%s() : queue len = %d\n", __func__,
skb_queue_len(&self->txq));
if (!skb_queue_empty(&self->txq)) {
/* Prevent race conditions with irlap_data_request() */
self->local_busy = TRUE;
/* Theory of operation.
* We send frames up to when we fill the window or
* reach line capacity. Those frames will queue up
* in the device queue, and the driver will slowly
* send them.
* After each frame that we send, we poll the higher
* layer for more data. It's the right time to do
* that because the link layer need to perform the mtt
* and then send the first frame, so we can afford
* to send a bit of time in kernel space.
* The explicit flow indication allow to minimise
* buffers (== lower latency), to avoid higher layer
* polling via timers (== less context switches) and
* to implement a crude scheduler - Jean II */
/* Try to send away all queued data frames */
while ((skb = skb_dequeue(&self->txq)) != NULL) {
/* Send one frame */
ret = (*state[self->state])(self, SEND_I_CMD,
skb, NULL);
/* Drop reference count.
* It will be increase as needed in
* irlap_send_data_xxx() */
kfree_skb(skb);
/* Poll the higher layers for one more frame */
irlmp_flow_indication(self->notify.instance,
FLOW_START);
if (ret == -EPROTO)
break; /* Try again later! */
}
/* Finished transmitting */
self->local_busy = FALSE;
} else if (self->disconnect_pending) {
self->disconnect_pending = FALSE;
ret = (*state[self->state])(self, DISCONNECT_REQUEST,
NULL, NULL);
}
break;
/* case LAP_NDM: */
/* case LAP_CONN: */
/* case LAP_RESET_WAIT: */
/* case LAP_RESET_CHECK: */
default:
break;
}
}
/*
* Function irlap_state_ndm (event, skb, frame)
*
* NDM (Normal Disconnected Mode) state
*
*/
static int irlap_state_ndm(struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info)
{
discovery_t *discovery_rsp;
int ret = 0;
IRDA_ASSERT(self != NULL, return -1;);
IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
switch (event) {
case CONNECT_REQUEST:
IRDA_ASSERT(self->netdev != NULL, return -1;);
if (self->media_busy) {
/* Note : this will never happen, because we test
* media busy in irlap_connect_request() and
* postpone the event... - Jean II */
IRDA_DEBUG(0, "%s(), CONNECT_REQUEST: media busy!\n",
__func__);
/* Always switch state before calling upper layers */
irlap_next_state(self, LAP_NDM);
irlap_disconnect_indication(self, LAP_MEDIA_BUSY);
} else {
irlap_send_snrm_frame(self, &self->qos_rx);
/* Start Final-bit timer */
irlap_start_final_timer(self, self->final_timeout);
self->retry_count = 0;
irlap_next_state(self, LAP_SETUP);
}
break;
case RECV_SNRM_CMD:
/* Check if the frame contains and I field */
if (info) {
self->daddr = info->daddr;
self->caddr = info->caddr;
irlap_next_state(self, LAP_CONN);
irlap_connect_indication(self, skb);
} else {
IRDA_DEBUG(0, "%s(), SNRM frame does not "
"contain an I field!\n", __func__);
}
break;
case DISCOVERY_REQUEST:
IRDA_ASSERT(info != NULL, return -1;);
if (self->media_busy) {
IRDA_DEBUG(1, "%s(), DISCOVERY_REQUEST: media busy!\n",
__func__);
/* irlap->log.condition = MEDIA_BUSY; */
/* This will make IrLMP try again */
irlap_discovery_confirm(self, NULL);
/* Note : the discovery log is not cleaned up here,
* it will be done in irlap_discovery_request()
* Jean II */
return 0;
}
self->S = info->S;
self->s = info->s;
irlap_send_discovery_xid_frame(self, info->S, info->s, TRUE,
info->discovery);
self->frame_sent = FALSE;
self->s++;
irlap_start_slot_timer(self, self->slot_timeout);
irlap_next_state(self, LAP_QUERY);
break;
case RECV_DISCOVERY_XID_CMD:
IRDA_ASSERT(info != NULL, return -1;);
/* Assert that this is not the final slot */
if (info->s <= info->S) {
self->slot = irlap_generate_rand_time_slot(info->S,
info->s);
if (self->slot == info->s) {
discovery_rsp = irlmp_get_discovery_response();
discovery_rsp->data.daddr = info->daddr;
irlap_send_discovery_xid_frame(self, info->S,
self->slot,
FALSE,
discovery_rsp);
self->frame_sent = TRUE;
} else
self->frame_sent = FALSE;
/*
* Go to reply state until end of discovery to
* inhibit our own transmissions. Set the timer
* to not stay forever there... Jean II
*/
irlap_start_query_timer(self, info->S, info->s);
irlap_next_state(self, LAP_REPLY);
} else {
/* This is the final slot. How is it possible ?
* This would happen is both discoveries are just slightly
* offset (if they are in sync, all packets are lost).
* Most often, all the discovery requests will be received
* in QUERY state (see my comment there), except for the
* last frame that will come here.
* The big trouble when it happen is that active discovery
* doesn't happen, because nobody answer the discoveries
* frame of the other guy, so the log shows up empty.
* What should we do ?
* Not much. It's too late to answer those discovery frames,
* so we just pass the info to IrLMP who will put it in the
* log (and post an event).
* Another cause would be devices that do discovery much
* slower than us, however the latest fixes should minimise
* those cases...
* Jean II
*/
IRDA_DEBUG(1, "%s(), Receiving final discovery request, missed the discovery slots :-(\n", __func__);
/* Last discovery request -> in the log */
irlap_discovery_indication(self, info->discovery);
}
break;
case MEDIA_BUSY_TIMER_EXPIRED:
/* A bunch of events may be postponed because the media is
* busy (usually immediately after we close a connection),
* or while we are doing discovery (state query/reply).
* In all those cases, the media busy flag will be cleared
* when it's OK for us to process those postponed events.
* This event is not mentioned in the state machines in the
* IrLAP spec. It's because they didn't consider Ultra and
* postponing connection request is optional.
* Jean II */
#ifdef CONFIG_IRDA_ULTRA
/* Send any pending Ultra frames if any */
if (!skb_queue_empty(&self->txq_ultra)) {
/* We don't send the frame, just post an event.
* Also, previously this code was in timer.c...
* Jean II */
ret = (*state[self->state])(self, SEND_UI_FRAME,
NULL, NULL);
}
#endif /* CONFIG_IRDA_ULTRA */
/* Check if we should try to connect.
* This code was previously in irlap_do_event() */
if (self->connect_pending) {
self->connect_pending = FALSE;
/* This one *should* not pend in this state, except
* if a socket try to connect and immediately
* disconnect. - clear - Jean II */
if (self->disconnect_pending)
irlap_disconnect_indication(self, LAP_DISC_INDICATION);
else
ret = (*state[self->state])(self,
CONNECT_REQUEST,
NULL, NULL);
self->disconnect_pending = FALSE;
}
/* Note : one way to test if this code works well (including
* media busy and small busy) is to create a user space
* application generating an Ultra packet every 3.05 sec (or
* 2.95 sec) and to see how it interact with discovery.
* It's fairly easy to check that no packet is lost, that the
* packets are postponed during discovery and that after
* discovery indication you have a 100ms "gap".
* As connection request and Ultra are now processed the same
* way, this avoid the tedious job of trying IrLAP connection
* in all those cases...
* Jean II */
break;
#ifdef CONFIG_IRDA_ULTRA
case SEND_UI_FRAME:
{
int i;
/* Only allowed to repeat an operation twice */
for (i=0; ((i<2) && (self->media_busy == FALSE)); i++) {
skb = skb_dequeue(&self->txq_ultra);
if (skb)
irlap_send_ui_frame(self, skb, CBROADCAST,
CMD_FRAME);
else
break;
/* irlap_send_ui_frame() won't increase skb reference
* count, so no dev_kfree_skb() - Jean II */
}
if (i == 2) {
/* Force us to listen 500 ms again */
irda_device_set_media_busy(self->netdev, TRUE);
}
break;
}
case RECV_UI_FRAME:
/* Only accept broadcast frames in NDM mode */
if (info->caddr != CBROADCAST) {
IRDA_DEBUG(0, "%s(), not a broadcast frame!\n",
__func__);
} else
irlap_unitdata_indication(self, skb);
break;
#endif /* CONFIG_IRDA_ULTRA */
case RECV_TEST_CMD:
/* Remove test frame header */
skb_pull(skb, sizeof(struct test_frame));
/*
* Send response. This skb will not be sent out again, and
* will only be used to send out the same info as the cmd
*/
irlap_send_test_frame(self, CBROADCAST, info->daddr, skb);
break;
case RECV_TEST_RSP:
IRDA_DEBUG(0, "%s() not implemented!\n", __func__);
break;
default:
IRDA_DEBUG(2, "%s(), Unknown event %s\n", __func__,
irlap_event[event]);
ret = -1;
break;
}
return ret;
}
/*
* Function irlap_state_query (event, skb, info)
*
* QUERY state
*
*/
static int irlap_state_query(struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info)
{
int ret = 0;
IRDA_ASSERT(self != NULL, return -1;);
IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
switch (event) {
case RECV_DISCOVERY_XID_RSP:
IRDA_ASSERT(info != NULL, return -1;);
IRDA_ASSERT(info->discovery != NULL, return -1;);
IRDA_DEBUG(4, "%s(), daddr=%08x\n", __func__,
info->discovery->data.daddr);
if (!self->discovery_log) {
IRDA_WARNING("%s: discovery log is gone! "
"maybe the discovery timeout has been set"
" too short?\n", __func__);
break;
}
hashbin_insert(self->discovery_log,
(irda_queue_t *) info->discovery,
info->discovery->data.daddr, NULL);
/* Keep state */
/* irlap_next_state(self, LAP_QUERY); */
break;
case RECV_DISCOVERY_XID_CMD:
/* Yes, it is possible to receive those frames in this mode.
* Note that most often the last discovery request won't
* occur here but in NDM state (see my comment there).
* What should we do ?
* Not much. We are currently performing our own discovery,
* therefore we can't answer those frames. We don't want
* to change state either. We just pass the info to
* IrLMP who will put it in the log (and post an event).
* Jean II
*/
IRDA_ASSERT(info != NULL, return -1;);
IRDA_DEBUG(1, "%s(), Receiving discovery request (s = %d) while performing discovery :-(\n", __func__, info->s);
/* Last discovery request ? */
if (info->s == 0xff)
irlap_discovery_indication(self, info->discovery);
break;
case SLOT_TIMER_EXPIRED:
/*
* Wait a little longer if we detect an incoming frame. This
* is not mentioned in the spec, but is a good thing to do,
* since we want to work even with devices that violate the
* timing requirements.
*/
if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
IRDA_DEBUG(2, "%s(), device is slow to answer, "
"waiting some more!\n", __func__);
irlap_start_slot_timer(self, msecs_to_jiffies(10));
self->add_wait = TRUE;
return ret;
}
self->add_wait = FALSE;
if (self->s < self->S) {
irlap_send_discovery_xid_frame(self, self->S,
self->s, TRUE,
self->discovery_cmd);
self->s++;
irlap_start_slot_timer(self, self->slot_timeout);
/* Keep state */
irlap_next_state(self, LAP_QUERY);
} else {
/* This is the final slot! */
irlap_send_discovery_xid_frame(self, self->S, 0xff,
TRUE,
self->discovery_cmd);
/* Always switch state before calling upper layers */
irlap_next_state(self, LAP_NDM);
/*
* We are now finished with the discovery procedure,
* so now we must return the results
*/
irlap_discovery_confirm(self, self->discovery_log);
/* IrLMP should now have taken care of the log */
self->discovery_log = NULL;
}
break;
default:
IRDA_DEBUG(2, "%s(), Unknown event %s\n", __func__,
irlap_event[event]);
ret = -1;
break;
}
return ret;
}
/*
* Function irlap_state_reply (self, event, skb, info)
*
* REPLY, we have received a XID discovery frame from a device and we
* are waiting for the right time slot to send a response XID frame
*
*/
static int irlap_state_reply(struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info)
{
discovery_t *discovery_rsp;
int ret=0;
IRDA_DEBUG(4, "%s()\n", __func__);
IRDA_ASSERT(self != NULL, return -1;);
IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
switch (event) {
case QUERY_TIMER_EXPIRED:
IRDA_DEBUG(0, "%s(), QUERY_TIMER_EXPIRED <%ld>\n",
__func__, jiffies);
irlap_next_state(self, LAP_NDM);
break;
case RECV_DISCOVERY_XID_CMD:
IRDA_ASSERT(info != NULL, return -1;);
/* Last frame? */
if (info->s == 0xff) {
del_timer(&self->query_timer);
/* info->log.condition = REMOTE; */
/* Always switch state before calling upper layers */
irlap_next_state(self, LAP_NDM);
irlap_discovery_indication(self, info->discovery);
} else {
/* If it's our slot, send our reply */
if ((info->s >= self->slot) && (!self->frame_sent)) {
discovery_rsp = irlmp_get_discovery_response();
discovery_rsp->data.daddr = info->daddr;
irlap_send_discovery_xid_frame(self, info->S,
self->slot,
FALSE,
discovery_rsp);
self->frame_sent = TRUE;
}
/* Readjust our timer to accommodate devices
* doing faster or slower discovery than us...
* Jean II */
irlap_start_query_timer(self, info->S, info->s);
/* Keep state */
//irlap_next_state(self, LAP_REPLY);
}
break;
default:
IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __func__,
event, irlap_event[event]);
ret = -1;
break;
}
return ret;
}
/*
* Function irlap_state_conn (event, skb, info)
*
* CONN, we have received a SNRM command and is waiting for the upper
* layer to accept or refuse connection
*
*/
static int irlap_state_conn(struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info)
{
int ret = 0;
IRDA_DEBUG(4, "%s(), event=%s\n", __func__, irlap_event[ event]);
IRDA_ASSERT(self != NULL, return -1;);
IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
switch (event) {
case CONNECT_RESPONSE:
skb_pull(skb, sizeof(struct snrm_frame));
IRDA_ASSERT(self->netdev != NULL, return -1;);
irlap_qos_negotiate(self, skb);
irlap_initiate_connection_state(self);
/*
* Applying the parameters now will make sure we change speed
* *after* we have sent the next frame
*/
irlap_apply_connection_parameters(self, FALSE);
/*
* Sending this frame will force a speed change after it has
* been sent (i.e. the frame will be sent at 9600).
*/
irlap_send_ua_response_frame(self, &self->qos_rx);
#if 0
/*
* We are allowed to send two frames, but this may increase
* the connect latency, so lets not do it for now.
*/
/* This is full of good intentions, but doesn't work in
* practice.
* After sending the first UA response, we switch the
* dongle to the negotiated speed, which is usually
* different than 9600 kb/s.
* From there, there is two solutions :
* 1) The other end has received the first UA response :
* it will set up the connection, move to state LAP_NRM_P,
* and will ignore and drop the second UA response.
* Actually, it's even worse : the other side will almost
* immediately send a RR that will likely collide with the
* UA response (depending on negotiated turnaround).
* 2) The other end has not received the first UA response,
* will stay at 9600 and will never see the second UA response.
* Jean II */
irlap_send_ua_response_frame(self, &self->qos_rx);
#endif
/*
* The WD-timer could be set to the duration of the P-timer
* for this case, but it is recommended to use twice the
* value (note 3 IrLAP p. 60).
*/
irlap_start_wd_timer(self, self->wd_timeout);
irlap_next_state(self, LAP_NRM_S);
break;
case RECV_DISCOVERY_XID_CMD:
IRDA_DEBUG(3, "%s(), event RECV_DISCOVER_XID_CMD!\n",
__func__);
irlap_next_state(self, LAP_NDM);
break;
case DISCONNECT_REQUEST:
IRDA_DEBUG(0, "%s(), Disconnect request!\n", __func__);
irlap_send_dm_frame(self);
irlap_next_state( self, LAP_NDM);
irlap_disconnect_indication(self, LAP_DISC_INDICATION);
break;
default:
IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __func__,
event, irlap_event[event]);
ret = -1;
break;
}
return ret;
}
/*
* Function irlap_state_setup (event, skb, frame)
*
* SETUP state, The local layer has transmitted a SNRM command frame to
* a remote peer layer and is awaiting a reply .
*
*/
static int irlap_state_setup(struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info)
{
int ret = 0;
IRDA_DEBUG(4, "%s()\n", __func__);
IRDA_ASSERT(self != NULL, return -1;);
IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
switch (event) {
case FINAL_TIMER_EXPIRED:
if (self->retry_count < self->N3) {
/*
* Perform random backoff, Wait a random number of time units, minimum
* duration half the time taken to transmitt a SNRM frame, maximum duration
* 1.5 times the time taken to transmit a SNRM frame. So this time should
* between 15 msecs and 45 msecs.
*/
irlap_start_backoff_timer(self, msecs_to_jiffies(20 +
(jiffies % 30)));
} else {
/* Always switch state before calling upper layers */
irlap_next_state(self, LAP_NDM);
irlap_disconnect_indication(self, LAP_FOUND_NONE);
}
break;
case BACKOFF_TIMER_EXPIRED:
irlap_send_snrm_frame(self, &self->qos_rx);
irlap_start_final_timer(self, self->final_timeout);
self->retry_count++;
break;
case RECV_SNRM_CMD:
IRDA_DEBUG(4, "%s(), SNRM battle!\n", __func__);
IRDA_ASSERT(skb != NULL, return 0;);
IRDA_ASSERT(info != NULL, return 0;);
/*
* The device with the largest device address wins the battle
* (both have sent a SNRM command!)
*/
if (info &&(info->daddr > self->saddr)) {
del_timer(&self->final_timer);
irlap_initiate_connection_state(self);
IRDA_ASSERT(self->netdev != NULL, return -1;);
skb_pull(skb, sizeof(struct snrm_frame));
irlap_qos_negotiate(self, skb);
/* Send UA frame and then change link settings */
irlap_apply_connection_parameters(self, FALSE);
irlap_send_ua_response_frame(self, &self->qos_rx);
irlap_next_state(self, LAP_NRM_S);
irlap_connect_confirm(self, skb);
/*
* The WD-timer could be set to the duration of the
* P-timer for this case, but it is recommended
* to use twice the value (note 3 IrLAP p. 60).
*/
irlap_start_wd_timer(self, self->wd_timeout);
} else {
/* We just ignore the other device! */
irlap_next_state(self, LAP_SETUP);
}
break;
case RECV_UA_RSP:
/* Stop F-timer */
del_timer(&self->final_timer);
/* Initiate connection state */
irlap_initiate_connection_state(self);
/* Negotiate connection parameters */
IRDA_ASSERT(skb->len > 10, return -1;);
skb_pull(skb, sizeof(struct ua_frame));
IRDA_ASSERT(self->netdev != NULL, return -1;);
irlap_qos_negotiate(self, skb);
/* Set the new link setting *now* (before the rr frame) */
irlap_apply_connection_parameters(self, TRUE);
self->retry_count = 0;
/* Wait for turnaround time to give a chance to the other
* device to be ready to receive us.
* Note : the time to switch speed is typically larger
* than the turnaround time, but as we don't have the other
* side speed switch time, that's our best guess...
* Jean II */
irlap_wait_min_turn_around(self, &self->qos_tx);
/* This frame will actually be sent at the new speed */
irlap_send_rr_frame(self, CMD_FRAME);
/* The timer is set to half the normal timer to quickly
* detect a failure to negotiate the new connection
* parameters. IrLAP 6.11.3.2, note 3.
* Note that currently we don't process this failure
* properly, as we should do a quick disconnect.
* Jean II */
irlap_start_final_timer(self, self->final_timeout/2);
irlap_next_state(self, LAP_NRM_P);
irlap_connect_confirm(self, skb);
break;
case RECV_DM_RSP: /* FALLTHROUGH */
case RECV_DISC_CMD:
del_timer(&self->final_timer);
irlap_next_state(self, LAP_NDM);
irlap_disconnect_indication(self, LAP_DISC_INDICATION);
break;
default:
IRDA_DEBUG(1, "%s(), Unknown event %d, %s\n", __func__,
event, irlap_event[event]);
ret = -1;
break;
}
return ret;
}
/*
* Function irlap_state_offline (self, event, skb, info)
*
* OFFLINE state, not used for now!
*
*/
static int irlap_state_offline(struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info)
{
IRDA_DEBUG( 0, "%s(), Unknown event\n", __func__);
return -1;
}
/*
* Function irlap_state_xmit_p (self, event, skb, info)
*
* XMIT, Only the primary station has right to transmit, and we
* therefore do not expect to receive any transmissions from other
* stations.
*
*/
static int irlap_state_xmit_p(struct irlap_cb *self, IRLAP_EVENT event,
struct sk_buff *skb, struct irlap_info *info)
{
int ret = 0;
switch (event) {
case SEND_I_CMD:
/*
* Only send frame if send-window > 0.
*/
if ((self->window > 0) && (!self->remote_busy)) {
int nextfit;
#ifdef CONFIG_IRDA_DYNAMIC_WINDOW
struct sk_buff *skb_next;
/* With DYNAMIC_WINDOW, we keep the window size
* maximum, and adapt on the packets we are sending.
* At 115k, we can send only 2 packets of 2048 bytes