-
Notifications
You must be signed in to change notification settings - Fork 1
/
r8180_dm.c
1139 lines (978 loc) · 34.1 KB
/
r8180_dm.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
#include "r8180_dm.h"
#include "r8180_hw.h"
#include "r8180_93cx6.h"
/* Return TRUE if we shall perform High Power Mechanism, FALSE otherwise. */
#define RATE_ADAPTIVE_TIMER_PERIOD 300
bool CheckHighPower(struct net_device *dev)
{
struct r8180_priv *priv = ieee80211_priv(dev);
struct ieee80211_device *ieee = priv->ieee80211;
if (!priv->bRegHighPowerMechanism)
return false;
if (ieee->state == IEEE80211_LINKED_SCANNING)
return false;
return true;
}
/*
* Description:
* Update Tx power level if necessary.
* See also DoRxHighPower() and SetTxPowerLevel8185() for reference.
*
* Note:
* The reason why we udpate Tx power level here instead of DoRxHighPower()
* is the number of IO to change Tx power is much more than channel TR switch
* and they are related to OFDM and MAC registers.
* So, we don't want to update it so frequently in per-Rx packet base.
*/
static void DoTxHighPower(struct net_device *dev)
{
struct r8180_priv *priv = ieee80211_priv(dev);
u16 HiPwrUpperTh = 0;
u16 HiPwrLowerTh = 0;
u8 RSSIHiPwrUpperTh;
u8 RSSIHiPwrLowerTh;
u8 u1bTmp;
char OfdmTxPwrIdx, CckTxPwrIdx;
HiPwrUpperTh = priv->RegHiPwrUpperTh;
HiPwrLowerTh = priv->RegHiPwrLowerTh;
HiPwrUpperTh = HiPwrUpperTh * 10;
HiPwrLowerTh = HiPwrLowerTh * 10;
RSSIHiPwrUpperTh = priv->RegRSSIHiPwrUpperTh;
RSSIHiPwrLowerTh = priv->RegRSSIHiPwrLowerTh;
/* lzm add 080826 */
OfdmTxPwrIdx = priv->chtxpwr_ofdm[priv->ieee80211->current_network.channel];
CckTxPwrIdx = priv->chtxpwr[priv->ieee80211->current_network.channel];
if ((priv->UndecoratedSmoothedSS > HiPwrUpperTh) ||
(priv->bCurCCKPkt && (priv->CurCCKRSSI > RSSIHiPwrUpperTh))) {
/* Stevenl suggested that degrade 8dbm in high power sate. 2007-12-04 Isaiah */
priv->bToUpdateTxPwr = true;
u1bTmp = read_nic_byte(dev, CCK_TXAGC);
/* If it never enter High Power. */
if (CckTxPwrIdx == u1bTmp) {
u1bTmp = (u1bTmp > 16) ? (u1bTmp - 16) : 0; /* 8dbm */
write_nic_byte(dev, CCK_TXAGC, u1bTmp);
u1bTmp = read_nic_byte(dev, OFDM_TXAGC);
u1bTmp = (u1bTmp > 16) ? (u1bTmp - 16) : 0; /* 8dbm */
write_nic_byte(dev, OFDM_TXAGC, u1bTmp);
}
} else if ((priv->UndecoratedSmoothedSS < HiPwrLowerTh) &&
(!priv->bCurCCKPkt || priv->CurCCKRSSI < RSSIHiPwrLowerTh)) {
if (priv->bToUpdateTxPwr) {
priv->bToUpdateTxPwr = false;
/* SD3 required. */
u1bTmp = read_nic_byte(dev, CCK_TXAGC);
if (u1bTmp < CckTxPwrIdx) {
write_nic_byte(dev, CCK_TXAGC, CckTxPwrIdx);
}
u1bTmp = read_nic_byte(dev, OFDM_TXAGC);
if (u1bTmp < OfdmTxPwrIdx) {
write_nic_byte(dev, OFDM_TXAGC, OfdmTxPwrIdx);
}
}
}
}
/*
* Description:
* Callback function of UpdateTxPowerWorkItem.
* Because of some event happened, e.g. CCX TPC, High Power Mechanism,
* We update Tx power of current channel again.
*/
void rtl8180_tx_pw_wq(struct work_struct *work)
{
struct delayed_work *dwork = to_delayed_work(work);
struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, tx_pw_wq);
struct net_device *dev = ieee->dev;
DoTxHighPower(dev);
}
/*
* Return TRUE if we shall perform DIG Mechanism, FALSE otherwise.
*/
bool CheckDig(struct net_device *dev)
{
struct r8180_priv *priv = ieee80211_priv(dev);
struct ieee80211_device *ieee = priv->ieee80211;
if (!priv->bDigMechanism)
return false;
if (ieee->state != IEEE80211_LINKED)
return false;
if ((priv->ieee80211->rate / 5) < 36) /* Schedule Dig under all OFDM rates. By Bruce, 2007-06-01. */
return false;
return true;
}
/*
* Implementation of DIG for Zebra and Zebra2.
*/
static void DIG_Zebra(struct net_device *dev)
{
struct r8180_priv *priv = ieee80211_priv(dev);
u16 CCKFalseAlarm, OFDMFalseAlarm;
u16 OfdmFA1, OfdmFA2;
int InitialGainStep = 7; /* The number of initial gain stages. */
int LowestGainStage = 4; /* The capable lowest stage of performing dig workitem. */
u32 AwakePeriodIn2Sec = 0;
CCKFalseAlarm = (u16)(priv->FalseAlarmRegValue & 0x0000ffff);
OFDMFalseAlarm = (u16)((priv->FalseAlarmRegValue >> 16) & 0x0000ffff);
OfdmFA1 = 0x15;
OfdmFA2 = ((u16)(priv->RegDigOfdmFaUpTh)) << 8;
/* The number of initial gain steps is different, by Bruce, 2007-04-13. */
if (priv->InitialGain == 0) { /* autoDIG */
/* Advised from SD3 DZ */
priv->InitialGain = 4; /* In 87B, m74dBm means State 4 (m82dBm) */
}
/* Advised from SD3 DZ */
OfdmFA1 = 0x20;
#if 1 /* lzm reserved 080826 */
AwakePeriodIn2Sec = (2000 - priv->DozePeriodInPast2Sec);
priv->DozePeriodInPast2Sec = 0;
if (AwakePeriodIn2Sec) {
OfdmFA1 = (u16)((OfdmFA1 * AwakePeriodIn2Sec) / 2000);
OfdmFA2 = (u16)((OfdmFA2 * AwakePeriodIn2Sec) / 2000);
} else {
;
}
#endif
InitialGainStep = 8;
LowestGainStage = priv->RegBModeGainStage; /* Lowest gain stage. */
if (OFDMFalseAlarm > OfdmFA1) {
if (OFDMFalseAlarm > OfdmFA2) {
priv->DIG_NumberFallbackVote++;
if (priv->DIG_NumberFallbackVote > 1) {
/* serious OFDM False Alarm, need fallback */
if (priv->InitialGain < InitialGainStep) {
priv->InitialGainBackUp = priv->InitialGain;
priv->InitialGain = (priv->InitialGain + 1);
UpdateInitialGain(dev);
}
priv->DIG_NumberFallbackVote = 0;
priv->DIG_NumberUpgradeVote = 0;
}
} else {
if (priv->DIG_NumberFallbackVote)
priv->DIG_NumberFallbackVote--;
}
priv->DIG_NumberUpgradeVote = 0;
} else {
if (priv->DIG_NumberFallbackVote)
priv->DIG_NumberFallbackVote--;
priv->DIG_NumberUpgradeVote++;
if (priv->DIG_NumberUpgradeVote > 9) {
if (priv->InitialGain > LowestGainStage) { /* In 87B, m78dBm means State 4 (m864dBm) */
priv->InitialGainBackUp = priv->InitialGain;
priv->InitialGain = (priv->InitialGain - 1);
UpdateInitialGain(dev);
}
priv->DIG_NumberFallbackVote = 0;
priv->DIG_NumberUpgradeVote = 0;
}
}
}
/*
* Dispatch DIG implementation according to RF.
*/
static void DynamicInitGain(struct net_device *dev)
{
DIG_Zebra(dev);
}
void rtl8180_hw_dig_wq(struct work_struct *work)
{
struct delayed_work *dwork = to_delayed_work(work);
struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, hw_dig_wq);
struct net_device *dev = ieee->dev;
struct r8180_priv *priv = ieee80211_priv(dev);
/* Read CCK and OFDM False Alarm. */
priv->FalseAlarmRegValue = read_nic_dword(dev, CCK_FALSE_ALARM);
/* Adjust Initial Gain dynamically. */
DynamicInitGain(dev);
}
static int IncludedInSupportedRates(struct r8180_priv *priv, u8 TxRate)
{
u8 rate_len;
u8 rate_ex_len;
u8 RateMask = 0x7F;
u8 idx;
unsigned short Found = 0;
u8 NaiveTxRate = TxRate&RateMask;
rate_len = priv->ieee80211->current_network.rates_len;
rate_ex_len = priv->ieee80211->current_network.rates_ex_len;
for (idx = 0; idx < rate_len; idx++) {
if ((priv->ieee80211->current_network.rates[idx] & RateMask) == NaiveTxRate) {
Found = 1;
goto found_rate;
}
}
for (idx = 0; idx < rate_ex_len; idx++) {
if ((priv->ieee80211->current_network.rates_ex[idx] & RateMask) == NaiveTxRate) {
Found = 1;
goto found_rate;
}
}
return Found;
found_rate:
return Found;
}
/*
* Get the Tx rate one degree up form the input rate in the supported rates.
* Return the upgrade rate if it is successed, otherwise return the input rate.
*/
static u8 GetUpgradeTxRate(struct net_device *dev, u8 rate)
{
struct r8180_priv *priv = ieee80211_priv(dev);
u8 UpRate;
/* Upgrade 1 degree. */
switch (rate) {
case 108: /* Up to 54Mbps. */
UpRate = 108;
break;
case 96: /* Up to 54Mbps. */
UpRate = 108;
break;
case 72: /* Up to 48Mbps. */
UpRate = 96;
break;
case 48: /* Up to 36Mbps. */
UpRate = 72;
break;
case 36: /* Up to 24Mbps. */
UpRate = 48;
break;
case 22: /* Up to 18Mbps. */
UpRate = 36;
break;
case 11: /* Up to 11Mbps. */
UpRate = 22;
break;
case 4: /* Up to 5.5Mbps. */
UpRate = 11;
break;
case 2: /* Up to 2Mbps. */
UpRate = 4;
break;
default:
printk("GetUpgradeTxRate(): Input Tx Rate(%d) is undefined!\n", rate);
return rate;
}
/* Check if the rate is valid. */
if (IncludedInSupportedRates(priv, UpRate)) {
return UpRate;
} else {
return rate;
}
return rate;
}
/*
* Get the Tx rate one degree down form the input rate in the supported rates.
* Return the degrade rate if it is successed, otherwise return the input rate.
*/
static u8 GetDegradeTxRate(struct net_device *dev, u8 rate)
{
struct r8180_priv *priv = ieee80211_priv(dev);
u8 DownRate;
/* Upgrade 1 degree. */
switch (rate) {
case 108: /* Down to 48Mbps. */
DownRate = 96;
break;
case 96: /* Down to 36Mbps. */
DownRate = 72;
break;
case 72: /* Down to 24Mbps. */
DownRate = 48;
break;
case 48: /* Down to 18Mbps. */
DownRate = 36;
break;
case 36: /* Down to 11Mbps. */
DownRate = 22;
break;
case 22: /* Down to 5.5Mbps. */
DownRate = 11;
break;
case 11: /* Down to 2Mbps. */
DownRate = 4;
break;
case 4: /* Down to 1Mbps. */
DownRate = 2;
break;
case 2: /* Down to 1Mbps. */
DownRate = 2;
break;
default:
printk("GetDegradeTxRate(): Input Tx Rate(%d) is undefined!\n", rate);
return rate;
}
/* Check if the rate is valid. */
if (IncludedInSupportedRates(priv, DownRate)) {
return DownRate;
} else {
return rate;
}
return rate;
}
/*
* Helper function to determine if specified data rate is
* CCK rate.
*/
static bool MgntIsCckRate(u16 rate)
{
bool bReturn = false;
if ((rate <= 22) && (rate != 12) && (rate != 18)) {
bReturn = true;
}
return bReturn;
}
/*
* Description:
* Tx Power tracking mechanism routine on 87SE.
*/
void TxPwrTracking87SE(struct net_device *dev)
{
struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
u8 tmpu1Byte, CurrentThermal, Idx;
char CckTxPwrIdx, OfdmTxPwrIdx;
tmpu1Byte = read_nic_byte(dev, EN_LPF_CAL);
CurrentThermal = (tmpu1Byte & 0xf0) >> 4; /*[ 7:4]: thermal meter indication. */
CurrentThermal = (CurrentThermal > 0x0c) ? 0x0c : CurrentThermal;/* lzm add 080826 */
if (CurrentThermal != priv->ThermalMeter) {
/* Update Tx Power level on each channel. */
for (Idx = 1; Idx < 15; Idx++) {
CckTxPwrIdx = priv->chtxpwr[Idx];
OfdmTxPwrIdx = priv->chtxpwr_ofdm[Idx];
if (CurrentThermal > priv->ThermalMeter) {
/* higher thermal meter. */
CckTxPwrIdx += (CurrentThermal - priv->ThermalMeter) * 2;
OfdmTxPwrIdx += (CurrentThermal - priv->ThermalMeter) * 2;
if (CckTxPwrIdx > 35)
CckTxPwrIdx = 35; /* Force TxPower to maximal index. */
if (OfdmTxPwrIdx > 35)
OfdmTxPwrIdx = 35;
} else {
/* lower thermal meter. */
CckTxPwrIdx -= (priv->ThermalMeter - CurrentThermal) * 2;
OfdmTxPwrIdx -= (priv->ThermalMeter - CurrentThermal) * 2;
if (CckTxPwrIdx < 0)
CckTxPwrIdx = 0;
if (OfdmTxPwrIdx < 0)
OfdmTxPwrIdx = 0;
}
/* Update TxPower level on CCK and OFDM resp. */
priv->chtxpwr[Idx] = CckTxPwrIdx;
priv->chtxpwr_ofdm[Idx] = OfdmTxPwrIdx;
}
/* Update TxPower level immediately. */
rtl8225z2_SetTXPowerLevel(dev, priv->ieee80211->current_network.channel);
}
priv->ThermalMeter = CurrentThermal;
}
static void StaRateAdaptive87SE(struct net_device *dev)
{
struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
unsigned long CurrTxokCnt;
u16 CurrRetryCnt;
u16 CurrRetryRate;
unsigned long CurrRxokCnt;
bool bTryUp = false;
bool bTryDown = false;
u8 TryUpTh = 1;
u8 TryDownTh = 2;
u32 TxThroughput;
long CurrSignalStrength;
bool bUpdateInitialGain = false;
u8 u1bOfdm = 0, u1bCck = 0;
char OfdmTxPwrIdx, CckTxPwrIdx;
priv->RateAdaptivePeriod = RATE_ADAPTIVE_TIMER_PERIOD;
CurrRetryCnt = priv->CurrRetryCnt;
CurrTxokCnt = priv->NumTxOkTotal - priv->LastTxokCnt;
CurrRxokCnt = priv->ieee80211->NumRxOkTotal - priv->LastRxokCnt;
CurrSignalStrength = priv->Stats_RecvSignalPower;
TxThroughput = (u32)(priv->NumTxOkBytesTotal - priv->LastTxOKBytes);
priv->LastTxOKBytes = priv->NumTxOkBytesTotal;
priv->CurrentOperaRate = priv->ieee80211->rate / 5;
/* 2 Compute retry ratio. */
if (CurrTxokCnt > 0) {
CurrRetryRate = (u16)(CurrRetryCnt * 100 / CurrTxokCnt);
} else {
/* It may be serious retry. To distinguish serious retry or no packets modified by Bruce */
CurrRetryRate = (u16)(CurrRetryCnt * 100 / 1);
}
priv->LastRetryCnt = priv->CurrRetryCnt;
priv->LastTxokCnt = priv->NumTxOkTotal;
priv->LastRxokCnt = priv->ieee80211->NumRxOkTotal;
priv->CurrRetryCnt = 0;
/* 2No Tx packets, return to init_rate or not? */
if (CurrRetryRate == 0 && CurrTxokCnt == 0) {
/*
* After 9 (30*300ms) seconds in this condition, we try to raise rate.
*/
priv->TryupingCountNoData++;
/* [TRC Dell Lab] Extend raised period from 4.5sec to 9sec, Isaiah 2008-02-15 18:00 */
if (priv->TryupingCountNoData > 30) {
priv->TryupingCountNoData = 0;
priv->CurrentOperaRate = GetUpgradeTxRate(dev, priv->CurrentOperaRate);
/* Reset Fail Record */
priv->LastFailTxRate = 0;
priv->LastFailTxRateSS = -200;
priv->FailTxRateCount = 0;
}
goto SetInitialGain;
} else {
priv->TryupingCountNoData = 0; /*Reset trying up times. */
}
/*
* For Netgear case, I comment out the following signal strength estimation,
* which can results in lower rate to transmit when sample is NOT enough (e.g. PING request).
*
* Restructure rate adaptive as the following main stages:
* (1) Add retry threshold in 54M upgrading condition with signal strength.
* (2) Add the mechanism to degrade to CCK rate according to signal strength
* and retry rate.
* (3) Remove all Initial Gain Updates over OFDM rate. To avoid the complicated
* situation, Initial Gain Update is upon on DIG mechanism except CCK rate.
* (4) Add the mechanism of trying to upgrade tx rate.
* (5) Record the information of upping tx rate to avoid trying upping tx rate constantly.
*
*/
/*
* 11Mbps or 36Mbps
* Check more times in these rate(key rates).
*/
if (priv->CurrentOperaRate == 22 || priv->CurrentOperaRate == 72)
TryUpTh += 9;
/*
* Let these rates down more difficult.
*/
if (MgntIsCckRate(priv->CurrentOperaRate) || priv->CurrentOperaRate == 36)
TryDownTh += 1;
/* 1 Adjust Rate. */
if (priv->bTryuping == true) {
/* 2 For Test Upgrading mechanism
* Note:
* Sometimes the throughput is upon on the capability between the AP and NIC,
* thus the low data rate does not improve the performance.
* We randomly upgrade the data rate and check if the retry rate is improved.
*/
/* Upgrading rate did not improve the retry rate, fallback to the original rate. */
if ((CurrRetryRate > 25) && TxThroughput < priv->LastTxThroughput) {
/*Not necessary raising rate, fall back rate. */
bTryDown = true;
} else {
priv->bTryuping = false;
}
} else if (CurrSignalStrength > -47 && (CurrRetryRate < 50)) {
/*
* 2For High Power
*
* Return to highest data rate, if signal strength is good enough.
* SignalStrength threshold(-50dbm) is for RTL8186.
* Revise SignalStrength threshold to -51dbm.
*/
/* Also need to check retry rate for safety, by Bruce, 2007-06-05. */
if (priv->CurrentOperaRate != priv->ieee80211->current_network.HighestOperaRate) {
bTryUp = true;
/* Upgrade Tx Rate directly. */
priv->TryupingCount += TryUpTh;
}
} else if (CurrTxokCnt > 9 && CurrTxokCnt < 100 && CurrRetryRate >= 600) {
/*
*2 For Serious Retry
*
* Traffic is not busy but our Tx retry is serious.
*/
bTryDown = true;
/* Let Rate Mechanism to degrade tx rate directly. */
priv->TryDownCountLowData += TryDownTh;
} else if (priv->CurrentOperaRate == 108) {
/* 2For 54Mbps */
/* Air Link */
if ((CurrRetryRate > 26) && (priv->LastRetryRate > 25)) {
bTryDown = true;
}
/* Cable Link */
else if ((CurrRetryRate > 17) && (priv->LastRetryRate > 16) && (CurrSignalStrength > -72)) {
bTryDown = true;
}
if (bTryDown && (CurrSignalStrength < -75)) /* cable link */
priv->TryDownCountLowData += TryDownTh;
} else if (priv->CurrentOperaRate == 96) {
/* 2For 48Mbps */
/* Air Link */
if (((CurrRetryRate > 48) && (priv->LastRetryRate > 47))) {
bTryDown = true;
} else if (((CurrRetryRate > 21) && (priv->LastRetryRate > 20)) && (CurrSignalStrength > -74)) { /* Cable Link */
/* Down to rate 36Mbps. */
bTryDown = true;
} else if ((CurrRetryRate > (priv->LastRetryRate + 50)) && (priv->FailTxRateCount > 2)) {
bTryDown = true;
priv->TryDownCountLowData += TryDownTh;
} else if ((CurrRetryRate < 8) && (priv->LastRetryRate < 8)) { /* TO DO: need to consider (RSSI) */
bTryUp = true;
}
if (bTryDown && (CurrSignalStrength < -75)) {
priv->TryDownCountLowData += TryDownTh;
}
} else if (priv->CurrentOperaRate == 72) {
/* 2For 36Mbps */
if ((CurrRetryRate > 43) && (priv->LastRetryRate > 41)) {
/* Down to rate 24Mbps. */
bTryDown = true;
} else if ((CurrRetryRate > (priv->LastRetryRate + 50)) && (priv->FailTxRateCount > 2)) {
bTryDown = true;
priv->TryDownCountLowData += TryDownTh;
} else if ((CurrRetryRate < 15) && (priv->LastRetryRate < 16)) { /* TO DO: need to consider (RSSI) */
bTryUp = true;
}
if (bTryDown && (CurrSignalStrength < -80))
priv->TryDownCountLowData += TryDownTh;
} else if (priv->CurrentOperaRate == 48) {
/* 2For 24Mbps */
/* Air Link */
if (((CurrRetryRate > 63) && (priv->LastRetryRate > 62))) {
bTryDown = true;
} else if (((CurrRetryRate > 33) && (priv->LastRetryRate > 32)) && (CurrSignalStrength > -82)) { /* Cable Link */
bTryDown = true;
} else if ((CurrRetryRate > (priv->LastRetryRate + 50)) && (priv->FailTxRateCount > 2)) {
bTryDown = true;
priv->TryDownCountLowData += TryDownTh;
} else if ((CurrRetryRate < 20) && (priv->LastRetryRate < 21)) { /* TO DO: need to consider (RSSI) */
bTryUp = true;
}
if (bTryDown && (CurrSignalStrength < -82))
priv->TryDownCountLowData += TryDownTh;
} else if (priv->CurrentOperaRate == 36) {
if (((CurrRetryRate > 85) && (priv->LastRetryRate > 86))) {
bTryDown = true;
} else if ((CurrRetryRate > (priv->LastRetryRate + 50)) && (priv->FailTxRateCount > 2)) {
bTryDown = true;
priv->TryDownCountLowData += TryDownTh;
} else if ((CurrRetryRate < 22) && (priv->LastRetryRate < 23)) { /* TO DO: need to consider (RSSI) */
bTryUp = true;
}
} else if (priv->CurrentOperaRate == 22) {
/* 2For 11Mbps */
if (CurrRetryRate > 95) {
bTryDown = true;
} else if ((CurrRetryRate < 29) && (priv->LastRetryRate < 30)) { /*TO DO: need to consider (RSSI) */
bTryUp = true;
}
} else if (priv->CurrentOperaRate == 11) {
/* 2For 5.5Mbps */
if (CurrRetryRate > 149) {
bTryDown = true;
} else if ((CurrRetryRate < 60) && (priv->LastRetryRate < 65)) {
bTryUp = true;
}
} else if (priv->CurrentOperaRate == 4) {
/* 2For 2 Mbps */
if ((CurrRetryRate > 99) && (priv->LastRetryRate > 99)) {
bTryDown = true;
} else if ((CurrRetryRate < 65) && (priv->LastRetryRate < 70)) {
bTryUp = true;
}
} else if (priv->CurrentOperaRate == 2) {
/* 2For 1 Mbps */
if ((CurrRetryRate < 70) && (priv->LastRetryRate < 75)) {
bTryUp = true;
}
}
if (bTryUp && bTryDown)
printk("StaRateAdaptive87B(): Tx Rate tried upping and downing simultaneously!\n");
/* 1 Test Upgrading Tx Rate
* Sometimes the cause of the low throughput (high retry rate) is the compatibility between the AP and NIC.
* To test if the upper rate may cause lower retry rate, this mechanism randomly occurs to test upgrading tx rate.
*/
if (!bTryUp && !bTryDown && (priv->TryupingCount == 0) && (priv->TryDownCountLowData == 0)
&& priv->CurrentOperaRate != priv->ieee80211->current_network.HighestOperaRate && priv->FailTxRateCount < 2) {
if (jiffies % (CurrRetryRate + 101) == 0) {
bTryUp = true;
priv->bTryuping = true;
}
}
/* 1 Rate Mechanism */
if (bTryUp) {
priv->TryupingCount++;
priv->TryDownCountLowData = 0;
/*
* Check more times if we need to upgrade indeed.
* Because the largest value of pHalData->TryupingCount is 0xFFFF and
* the largest value of pHalData->FailTxRateCount is 0x14,
* this condition will be satisfied at most every 2 min.
*/
if ((priv->TryupingCount > (TryUpTh + priv->FailTxRateCount * priv->FailTxRateCount)) ||
(CurrSignalStrength > priv->LastFailTxRateSS) || priv->bTryuping) {
priv->TryupingCount = 0;
/*
* When transferring from CCK to OFDM, DIG is an important issue.
*/
if (priv->CurrentOperaRate == 22)
bUpdateInitialGain = true;
/*
* The difference in throughput between 48Mbps and 36Mbps is 8M.
* So, we must be careful in this rate scale. Isaiah 2008-02-15.
*/
if (((priv->CurrentOperaRate == 72) || (priv->CurrentOperaRate == 48) || (priv->CurrentOperaRate == 36)) &&
(priv->FailTxRateCount > 2))
priv->RateAdaptivePeriod = (RATE_ADAPTIVE_TIMER_PERIOD / 2);
/* (1)To avoid upgrade frequently to the fail tx rate, add the FailTxRateCount into the threshold. */
/* (2)If the signal strength is increased, it may be able to upgrade. */
priv->CurrentOperaRate = GetUpgradeTxRate(dev, priv->CurrentOperaRate);
if (priv->CurrentOperaRate == 36) {
priv->bUpdateARFR = true;
write_nic_word(dev, ARFR, 0x0F8F); /* bypass 12/9/6 */
} else if (priv->bUpdateARFR) {
priv->bUpdateARFR = false;
write_nic_word(dev, ARFR, 0x0FFF); /* set 1M ~ 54Mbps. */
}
/* Update Fail Tx rate and count. */
if (priv->LastFailTxRate != priv->CurrentOperaRate) {
priv->LastFailTxRate = priv->CurrentOperaRate;
priv->FailTxRateCount = 0;
priv->LastFailTxRateSS = -200; /* Set lowest power. */
}
}
} else {
if (priv->TryupingCount > 0)
priv->TryupingCount--;
}
if (bTryDown) {
priv->TryDownCountLowData++;
priv->TryupingCount = 0;
/* Check if Tx rate can be degraded or Test trying upgrading should fallback. */
if (priv->TryDownCountLowData > TryDownTh || priv->bTryuping) {
priv->TryDownCountLowData = 0;
priv->bTryuping = false;
/* Update fail information. */
if (priv->LastFailTxRate == priv->CurrentOperaRate) {
priv->FailTxRateCount++;
/* Record the Tx fail rate signal strength. */
if (CurrSignalStrength > priv->LastFailTxRateSS)
priv->LastFailTxRateSS = CurrSignalStrength;
} else {
priv->LastFailTxRate = priv->CurrentOperaRate;
priv->FailTxRateCount = 1;
priv->LastFailTxRateSS = CurrSignalStrength;
}
priv->CurrentOperaRate = GetDegradeTxRate(dev, priv->CurrentOperaRate);
/* Reduce chariot training time at weak signal strength situation. SD3 ED demand. */
if ((CurrSignalStrength < -80) && (priv->CurrentOperaRate > 72)) {
priv->CurrentOperaRate = 72;
}
if (priv->CurrentOperaRate == 36) {
priv->bUpdateARFR = true;
write_nic_word(dev, ARFR, 0x0F8F); /* bypass 12/9/6 */
} else if (priv->bUpdateARFR) {
priv->bUpdateARFR = false;
write_nic_word(dev, ARFR, 0x0FFF); /* set 1M ~ 54Mbps. */
}
/*
* When it is CCK rate, it may need to update initial gain to receive lower power packets.
*/
if (MgntIsCckRate(priv->CurrentOperaRate)) {
bUpdateInitialGain = true;
}
}
} else {
if (priv->TryDownCountLowData > 0)
priv->TryDownCountLowData--;
}
/*
* Keep the Tx fail rate count to equal to 0x15 at most.
* Reduce the fail count at least to 10 sec if tx rate is tending stable.
*/
if (priv->FailTxRateCount >= 0x15 ||
(!bTryUp && !bTryDown && priv->TryDownCountLowData == 0 && priv->TryupingCount && priv->FailTxRateCount > 0x6)) {
priv->FailTxRateCount--;
}
OfdmTxPwrIdx = priv->chtxpwr_ofdm[priv->ieee80211->current_network.channel];
CckTxPwrIdx = priv->chtxpwr[priv->ieee80211->current_network.channel];
/* Mac0x9e increase 2 level in 36M~18M situation */
if ((priv->CurrentOperaRate < 96) && (priv->CurrentOperaRate > 22)) {
u1bCck = read_nic_byte(dev, CCK_TXAGC);
u1bOfdm = read_nic_byte(dev, OFDM_TXAGC);
/* case 1: Never enter High power */
if (u1bCck == CckTxPwrIdx) {
if (u1bOfdm != (OfdmTxPwrIdx + 2)) {
priv->bEnhanceTxPwr = true;
u1bOfdm = ((u1bOfdm + 2) > 35) ? 35 : (u1bOfdm + 2);
write_nic_byte(dev, OFDM_TXAGC, u1bOfdm);
}
} else if (u1bCck < CckTxPwrIdx) {
/* case 2: enter high power */
if (!priv->bEnhanceTxPwr) {
priv->bEnhanceTxPwr = true;
u1bOfdm = ((u1bOfdm + 2) > 35) ? 35 : (u1bOfdm + 2);
write_nic_byte(dev, OFDM_TXAGC, u1bOfdm);
}
}
} else if (priv->bEnhanceTxPwr) { /* 54/48/11/5.5/2/1 */
u1bCck = read_nic_byte(dev, CCK_TXAGC);
u1bOfdm = read_nic_byte(dev, OFDM_TXAGC);
/* case 1: Never enter High power */
if (u1bCck == CckTxPwrIdx) {
priv->bEnhanceTxPwr = false;
write_nic_byte(dev, OFDM_TXAGC, OfdmTxPwrIdx);
}
/* case 2: enter high power */
else if (u1bCck < CckTxPwrIdx) {
priv->bEnhanceTxPwr = false;
u1bOfdm = ((u1bOfdm - 2) > 0) ? (u1bOfdm - 2) : 0;
write_nic_byte(dev, OFDM_TXAGC, u1bOfdm);
}
}
/*
* We need update initial gain when we set tx rate "from OFDM to CCK" or
* "from CCK to OFDM".
*/
SetInitialGain:
if (bUpdateInitialGain) {
if (MgntIsCckRate(priv->CurrentOperaRate)) { /* CCK */
if (priv->InitialGain > priv->RegBModeGainStage) {
priv->InitialGainBackUp = priv->InitialGain;
if (CurrSignalStrength < -85) /* Low power, OFDM [0x17] = 26. */
/* SD3 SYs suggest that CurrSignalStrength < -65, ofdm 0x17=26. */
priv->InitialGain = priv->RegBModeGainStage;
else if (priv->InitialGain > priv->RegBModeGainStage + 1)
priv->InitialGain -= 2;
else
priv->InitialGain--;
printk("StaRateAdaptive87SE(): update init_gain to index %d for date rate %d\n", priv->InitialGain, priv->CurrentOperaRate);
UpdateInitialGain(dev);
}
} else { /* OFDM */
if (priv->InitialGain < 4) {
priv->InitialGainBackUp = priv->InitialGain;
priv->InitialGain++;
printk("StaRateAdaptive87SE(): update init_gain to index %d for date rate %d\n", priv->InitialGain, priv->CurrentOperaRate);
UpdateInitialGain(dev);
}
}
}
/* Record the related info */
priv->LastRetryRate = CurrRetryRate;
priv->LastTxThroughput = TxThroughput;
priv->ieee80211->rate = priv->CurrentOperaRate * 5;
}
void rtl8180_rate_adapter(struct work_struct *work)
{
struct delayed_work *dwork = to_delayed_work(work);
struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, rate_adapter_wq);
struct net_device *dev = ieee->dev;
StaRateAdaptive87SE(dev);
}
void timer_rate_adaptive(unsigned long data)
{
struct r8180_priv *priv = ieee80211_priv((struct net_device *)data);
if (!priv->up) {
return;
}
if ((priv->ieee80211->iw_mode != IW_MODE_MASTER)
&& (priv->ieee80211->state == IEEE80211_LINKED) &&
(priv->ForcedDataRate == 0)) {
queue_work(priv->ieee80211->wq, (void *)&priv->ieee80211->rate_adapter_wq);
}
priv->rateadapter_timer.expires = jiffies + MSECS(priv->RateAdaptivePeriod);
add_timer(&priv->rateadapter_timer);
}
void SwAntennaDiversityRxOk8185(struct net_device *dev, u8 SignalStrength)
{
struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
priv->AdRxOkCnt++;
if (priv->AdRxSignalStrength != -1) {
priv->AdRxSignalStrength = ((priv->AdRxSignalStrength * 7) + (SignalStrength * 3)) / 10;
} else { /* Initialization case. */
priv->AdRxSignalStrength = SignalStrength;
}
if (priv->LastRxPktAntenna) /* Main antenna. */
priv->AdMainAntennaRxOkCnt++;
else /* Aux antenna. */
priv->AdAuxAntennaRxOkCnt++;
}
/* Change Antenna Switch. */
bool SetAntenna8185(struct net_device *dev, u8 u1bAntennaIndex)
{
struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
bool bAntennaSwitched = false;
switch (u1bAntennaIndex) {
case 0:
/* Mac register, main antenna */
write_nic_byte(dev, ANTSEL, 0x03);
/* base band */
write_phy_cck(dev, 0x11, 0x9b); /* Config CCK RX antenna. */
write_phy_ofdm(dev, 0x0d, 0x5c); /* Config OFDM RX antenna. */
bAntennaSwitched = true;
break;
case 1:
/* Mac register, aux antenna */
write_nic_byte(dev, ANTSEL, 0x00);
/* base band */
write_phy_cck(dev, 0x11, 0xbb); /* Config CCK RX antenna. */
write_phy_ofdm(dev, 0x0d, 0x54); /* Config OFDM RX antenna. */
bAntennaSwitched = true;
break;
default:
printk("SetAntenna8185: unknown u1bAntennaIndex(%d)\n", u1bAntennaIndex);
break;
}
if (bAntennaSwitched)
priv->CurrAntennaIndex = u1bAntennaIndex;
return bAntennaSwitched;
}
/* Toggle Antenna switch. */
bool SwitchAntenna(struct net_device *dev)
{
struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
bool bResult;
if (priv->CurrAntennaIndex == 0) {
bResult = SetAntenna8185(dev, 1);
} else {
bResult = SetAntenna8185(dev, 0);
}
return bResult;
}
/*
* Engine of SW Antenna Diversity mechanism.
* Since 8187 has no Tx part information,
* this implementation is only dependend on Rx part information.
*/
void SwAntennaDiversity(struct net_device *dev)
{
struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
bool bSwCheckSS = false;
if (bSwCheckSS) {
priv->AdTickCount++;
printk("(1) AdTickCount: %d, AdCheckPeriod: %d\n",
priv->AdTickCount, priv->AdCheckPeriod);
printk("(2) AdRxSignalStrength: %ld, AdRxSsThreshold: %ld\n",
priv->AdRxSignalStrength, priv->AdRxSsThreshold);
}
/* Case 1. No Link. */
if (priv->ieee80211->state != IEEE80211_LINKED) {
priv->bAdSwitchedChecking = false;
/* I switch antenna here to prevent any one of antenna is broken before link established, 2006.04.18, by rcnjko.. */
SwitchAntenna(dev);
/* Case 2. Linked but no packet receive.d */
} else if (priv->AdRxOkCnt == 0) {
priv->bAdSwitchedChecking = false;
SwitchAntenna(dev);
/* Case 3. Evaluate last antenna switch action and undo it if necessary. */
} else if (priv->bAdSwitchedChecking == true) {
priv->bAdSwitchedChecking = false;
/* Adjust Rx signal strength threshold. */
priv->AdRxSsThreshold = (priv->AdRxSignalStrength + priv->AdRxSsBeforeSwitched) / 2;
priv->AdRxSsThreshold = (priv->AdRxSsThreshold > priv->AdMaxRxSsThreshold) ?