]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/rtl8187se/r8180_dm.c
ARM: EXYNOS: mcpm rename the power_down_finish
[karo-tx-linux.git] / drivers / staging / rtl8187se / r8180_dm.c
1 #include "r8180_dm.h"
2 #include "r8180_hw.h"
3 #include "r8180_93cx6.h"
4
5  /*     Return TRUE if we shall perform High Power Mechanism, FALSE otherwise. */
6 #define RATE_ADAPTIVE_TIMER_PERIOD      300
7
8 bool CheckHighPower(struct net_device *dev)
9 {
10         struct r8180_priv *priv = ieee80211_priv(dev);
11         struct ieee80211_device *ieee = priv->ieee80211;
12
13         if (!priv->bRegHighPowerMechanism)
14                 return false;
15
16         if (ieee->state == IEEE80211_LINKED_SCANNING)
17                 return false;
18
19         return true;
20 }
21
22 /*
23  *      Description:
24  *              Update Tx power level if necessary.
25  *              See also DoRxHighPower() and SetTxPowerLevel8185() for reference.
26  *
27  *      Note:
28  *              The reason why we udpate Tx power level here instead of DoRxHighPower()
29  *              is the number of IO to change Tx power is much more than channel TR switch
30  *              and they are related to OFDM and MAC registers.
31  *              So, we don't want to update it so frequently in per-Rx packet base.
32  */
33 static void DoTxHighPower(struct net_device *dev)
34 {
35         struct r8180_priv *priv = ieee80211_priv(dev);
36         u16                     HiPwrUpperTh = 0;
37         u16                     HiPwrLowerTh = 0;
38         u8                      RSSIHiPwrUpperTh;
39         u8                      RSSIHiPwrLowerTh;
40         u8                      u1bTmp;
41         char                    OfdmTxPwrIdx, CckTxPwrIdx;
42
43         HiPwrUpperTh = priv->RegHiPwrUpperTh;
44         HiPwrLowerTh = priv->RegHiPwrLowerTh;
45
46         HiPwrUpperTh = HiPwrUpperTh * 10;
47         HiPwrLowerTh = HiPwrLowerTh * 10;
48         RSSIHiPwrUpperTh = priv->RegRSSIHiPwrUpperTh;
49         RSSIHiPwrLowerTh = priv->RegRSSIHiPwrLowerTh;
50
51         /* lzm add 080826 */
52         OfdmTxPwrIdx  = priv->chtxpwr_ofdm[priv->ieee80211->current_network.channel];
53         CckTxPwrIdx  = priv->chtxpwr[priv->ieee80211->current_network.channel];
54
55         if ((priv->UndecoratedSmoothedSS > HiPwrUpperTh) ||
56                 (priv->bCurCCKPkt && (priv->CurCCKRSSI > RSSIHiPwrUpperTh))) {
57                 /* Stevenl suggested that degrade 8dbm in high power sate. 2007-12-04 Isaiah */
58
59                 priv->bToUpdateTxPwr = true;
60                 u1bTmp = read_nic_byte(dev, CCK_TXAGC);
61
62                 /* If it never enter High Power. */
63                 if (CckTxPwrIdx == u1bTmp) {
64                         u1bTmp = (u1bTmp > 16) ? (u1bTmp - 16) : 0;  /* 8dbm */
65                         write_nic_byte(dev, CCK_TXAGC, u1bTmp);
66
67                         u1bTmp = read_nic_byte(dev, OFDM_TXAGC);
68                         u1bTmp = (u1bTmp > 16) ? (u1bTmp - 16) : 0;  /* 8dbm */
69                         write_nic_byte(dev, OFDM_TXAGC, u1bTmp);
70                 }
71
72         } else if ((priv->UndecoratedSmoothedSS < HiPwrLowerTh) &&
73                 (!priv->bCurCCKPkt || priv->CurCCKRSSI < RSSIHiPwrLowerTh)) {
74                 if (priv->bToUpdateTxPwr) {
75                         priv->bToUpdateTxPwr = false;
76                         /* SD3 required. */
77                         u1bTmp = read_nic_byte(dev, CCK_TXAGC);
78                         if (u1bTmp < CckTxPwrIdx) {
79                                 write_nic_byte(dev, CCK_TXAGC, CckTxPwrIdx);
80                         }
81
82                         u1bTmp = read_nic_byte(dev, OFDM_TXAGC);
83                         if (u1bTmp < OfdmTxPwrIdx) {
84                                 write_nic_byte(dev, OFDM_TXAGC, OfdmTxPwrIdx);
85                         }
86                 }
87         }
88 }
89
90
91 /*
92  *      Description:
93  *              Callback function of UpdateTxPowerWorkItem.
94  *              Because of some event happened, e.g. CCX TPC, High Power Mechanism,
95  *              We update Tx power of current channel again.
96  */
97 void rtl8180_tx_pw_wq(struct work_struct *work)
98 {
99         struct delayed_work *dwork = to_delayed_work(work);
100         struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, tx_pw_wq);
101         struct net_device *dev = ieee->dev;
102
103         DoTxHighPower(dev);
104 }
105
106
107 /*
108  *      Return TRUE if we shall perform DIG Mechanism, FALSE otherwise.
109  */
110 bool CheckDig(struct net_device *dev)
111 {
112         struct r8180_priv *priv = ieee80211_priv(dev);
113         struct ieee80211_device *ieee = priv->ieee80211;
114
115         if (!priv->bDigMechanism)
116                 return false;
117
118         if (ieee->state != IEEE80211_LINKED)
119                 return false;
120
121         if ((priv->ieee80211->rate / 5) < 36) /* Schedule Dig under all OFDM rates. By Bruce, 2007-06-01. */
122                 return false;
123         return true;
124 }
125 /*
126  *      Implementation of DIG for Zebra and Zebra2.
127  */
128 static void DIG_Zebra(struct net_device *dev)
129 {
130         struct r8180_priv *priv = ieee80211_priv(dev);
131         u16                     CCKFalseAlarm, OFDMFalseAlarm;
132         u16                     OfdmFA1, OfdmFA2;
133         int                     InitialGainStep = 7; /* The number of initial gain stages. */
134         int                     LowestGainStage = 4; /* The capable lowest stage of performing dig workitem. */
135         u32                     AwakePeriodIn2Sec = 0;
136
137         CCKFalseAlarm = (u16)(priv->FalseAlarmRegValue & 0x0000ffff);
138         OFDMFalseAlarm = (u16)((priv->FalseAlarmRegValue >> 16) & 0x0000ffff);
139         OfdmFA1 =  0x15;
140         OfdmFA2 = ((u16)(priv->RegDigOfdmFaUpTh)) << 8;
141
142         /* The number of initial gain steps is different, by Bruce, 2007-04-13. */
143         if (priv->InitialGain == 0) { /* autoDIG */
144                 /* Advised from SD3 DZ */
145                 priv->InitialGain = 4; /* In 87B, m74dBm means State 4 (m82dBm) */
146         }
147         /* Advised from SD3 DZ */
148         OfdmFA1 = 0x20;
149
150 #if 1 /* lzm reserved 080826 */
151         AwakePeriodIn2Sec = (2000 - priv->DozePeriodInPast2Sec);
152         priv->DozePeriodInPast2Sec = 0;
153
154         if (AwakePeriodIn2Sec) {
155                 OfdmFA1 = (u16)((OfdmFA1 * AwakePeriodIn2Sec) / 2000);
156                 OfdmFA2 = (u16)((OfdmFA2 * AwakePeriodIn2Sec) / 2000);
157         } else {
158                 ;
159         }
160 #endif
161
162         InitialGainStep = 8;
163         LowestGainStage = priv->RegBModeGainStage; /* Lowest gain stage. */
164
165         if (OFDMFalseAlarm > OfdmFA1) {
166                 if (OFDMFalseAlarm > OfdmFA2) {
167                         priv->DIG_NumberFallbackVote++;
168                         if (priv->DIG_NumberFallbackVote > 1) {
169                                 /* serious OFDM  False Alarm, need fallback */
170                                 if (priv->InitialGain < InitialGainStep) {
171                                         priv->InitialGainBackUp = priv->InitialGain;
172
173                                         priv->InitialGain = (priv->InitialGain + 1);
174                                         UpdateInitialGain(dev);
175                                 }
176                                 priv->DIG_NumberFallbackVote = 0;
177                                 priv->DIG_NumberUpgradeVote = 0;
178                         }
179                 } else {
180                         if (priv->DIG_NumberFallbackVote)
181                                 priv->DIG_NumberFallbackVote--;
182                 }
183                 priv->DIG_NumberUpgradeVote = 0;
184         } else {
185                 if (priv->DIG_NumberFallbackVote)
186                         priv->DIG_NumberFallbackVote--;
187                 priv->DIG_NumberUpgradeVote++;
188
189                 if (priv->DIG_NumberUpgradeVote > 9) {
190                         if (priv->InitialGain > LowestGainStage) { /* In 87B, m78dBm means State 4 (m864dBm) */
191                                 priv->InitialGainBackUp = priv->InitialGain;
192
193                                 priv->InitialGain = (priv->InitialGain - 1);
194                                 UpdateInitialGain(dev);
195                         }
196                         priv->DIG_NumberFallbackVote = 0;
197                         priv->DIG_NumberUpgradeVote = 0;
198                 }
199         }
200 }
201
202 /*
203  *      Dispatch DIG implementation according to RF.
204  */
205 static void DynamicInitGain(struct net_device *dev)
206 {
207         DIG_Zebra(dev);
208 }
209
210 void rtl8180_hw_dig_wq(struct work_struct *work)
211 {
212         struct delayed_work *dwork = to_delayed_work(work);
213         struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, hw_dig_wq);
214         struct net_device *dev = ieee->dev;
215         struct r8180_priv *priv = ieee80211_priv(dev);
216
217         /* Read CCK and OFDM False Alarm. */
218         priv->FalseAlarmRegValue = read_nic_dword(dev, CCK_FALSE_ALARM);
219
220
221         /* Adjust Initial Gain dynamically. */
222         DynamicInitGain(dev);
223
224 }
225
226 static int IncludedInSupportedRates(struct r8180_priv *priv, u8 TxRate)
227 {
228         u8 rate_len;
229         u8 rate_ex_len;
230         u8                      RateMask = 0x7F;
231         u8                      idx;
232         unsigned short          Found = 0;
233         u8                      NaiveTxRate = TxRate&RateMask;
234
235         rate_len = priv->ieee80211->current_network.rates_len;
236         rate_ex_len = priv->ieee80211->current_network.rates_ex_len;
237         for (idx = 0; idx < rate_len; idx++) {
238                 if ((priv->ieee80211->current_network.rates[idx] & RateMask) == NaiveTxRate) {
239                         Found = 1;
240                         goto found_rate;
241                 }
242         }
243         for (idx = 0; idx < rate_ex_len; idx++) {
244                 if ((priv->ieee80211->current_network.rates_ex[idx] & RateMask) == NaiveTxRate) {
245                         Found = 1;
246                         goto found_rate;
247                 }
248         }
249         return Found;
250 found_rate:
251         return Found;
252 }
253
254 /*
255  *      Get the Tx rate one degree up form the input rate in the supported rates.
256  *      Return the upgrade rate if it is successed, otherwise return the input rate.
257  */
258 static u8 GetUpgradeTxRate(struct net_device *dev, u8 rate)
259 {
260         struct r8180_priv *priv = ieee80211_priv(dev);
261         u8                      UpRate;
262
263         /* Upgrade 1 degree. */
264         switch (rate) {
265         case 108: /* Up to 54Mbps. */
266                 UpRate = 108;
267                 break;
268
269         case 96: /* Up to 54Mbps. */
270                 UpRate = 108;
271                 break;
272
273         case 72: /* Up to 48Mbps. */
274                 UpRate = 96;
275                 break;
276
277         case 48: /* Up to 36Mbps. */
278                 UpRate = 72;
279                 break;
280
281         case 36: /* Up to 24Mbps. */
282                 UpRate = 48;
283                 break;
284
285         case 22: /* Up to 18Mbps. */
286                 UpRate = 36;
287                 break;
288
289         case 11: /* Up to 11Mbps. */
290                 UpRate = 22;
291                 break;
292
293         case 4: /* Up to 5.5Mbps. */
294                 UpRate = 11;
295                 break;
296
297         case 2: /* Up to 2Mbps. */
298                 UpRate = 4;
299                 break;
300
301         default:
302                 printk("GetUpgradeTxRate(): Input Tx Rate(%d) is undefined!\n", rate);
303                 return rate;
304         }
305         /* Check if the rate is valid. */
306         if (IncludedInSupportedRates(priv, UpRate)) {
307                 return UpRate;
308         } else {
309                 return rate;
310         }
311         return rate;
312 }
313 /*
314  *      Get the Tx rate one degree down form the input rate in the supported rates.
315  *      Return the degrade rate if it is successed, otherwise return the input rate.
316  */
317
318 static u8 GetDegradeTxRate(struct net_device *dev, u8 rate)
319 {
320         struct r8180_priv *priv = ieee80211_priv(dev);
321         u8                      DownRate;
322
323         /* Upgrade 1 degree. */
324         switch (rate) {
325         case 108: /* Down to 48Mbps. */
326                 DownRate = 96;
327                 break;
328
329         case 96: /* Down to 36Mbps. */
330                 DownRate = 72;
331                 break;
332
333         case 72: /* Down to 24Mbps. */
334                 DownRate = 48;
335                 break;
336
337         case 48: /* Down to 18Mbps. */
338                 DownRate = 36;
339                 break;
340
341         case 36: /* Down to 11Mbps. */
342                 DownRate = 22;
343                 break;
344
345         case 22: /* Down to 5.5Mbps. */
346                 DownRate = 11;
347                 break;
348
349         case 11: /* Down to 2Mbps. */
350                 DownRate = 4;
351                 break;
352
353         case 4: /* Down to 1Mbps. */
354                 DownRate = 2;
355                 break;
356
357         case 2: /* Down to 1Mbps. */
358                 DownRate = 2;
359                 break;
360
361         default:
362                 printk("GetDegradeTxRate(): Input Tx Rate(%d) is undefined!\n", rate);
363                 return rate;
364         }
365         /* Check if the rate is valid. */
366         if (IncludedInSupportedRates(priv, DownRate)) {
367                 return DownRate;
368         } else {
369                 return rate;
370         }
371         return rate;
372 }
373 /*
374  *      Helper function to determine if specified data rate is
375  *      CCK rate.
376  */
377
378 static bool MgntIsCckRate(u16 rate)
379 {
380         bool bReturn = false;
381
382         if ((rate <= 22) && (rate != 12) && (rate != 18)) {
383                 bReturn = true;
384         }
385
386         return bReturn;
387 }
388 /*
389  *      Description:
390  *              Tx Power tracking mechanism routine on 87SE.
391  */
392 void TxPwrTracking87SE(struct net_device *dev)
393 {
394         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
395         u8      tmpu1Byte, CurrentThermal, Idx;
396         char    CckTxPwrIdx, OfdmTxPwrIdx;
397
398         tmpu1Byte = read_nic_byte(dev, EN_LPF_CAL);
399         CurrentThermal = (tmpu1Byte & 0xf0) >> 4; /*[ 7:4]: thermal meter indication. */
400         CurrentThermal = (CurrentThermal > 0x0c) ? 0x0c : CurrentThermal;/* lzm add 080826 */
401
402         if (CurrentThermal != priv->ThermalMeter) {
403                 /* Update Tx Power level on each channel. */
404                 for (Idx = 1; Idx < 15; Idx++) {
405                         CckTxPwrIdx = priv->chtxpwr[Idx];
406                         OfdmTxPwrIdx = priv->chtxpwr_ofdm[Idx];
407
408                         if (CurrentThermal > priv->ThermalMeter) {
409                                 /* higher thermal meter. */
410                                 CckTxPwrIdx += (CurrentThermal - priv->ThermalMeter) * 2;
411                                 OfdmTxPwrIdx += (CurrentThermal - priv->ThermalMeter) * 2;
412
413                                 if (CckTxPwrIdx > 35)
414                                         CckTxPwrIdx = 35; /* Force TxPower to maximal index. */
415                                 if (OfdmTxPwrIdx > 35)
416                                         OfdmTxPwrIdx = 35;
417                         } else {
418                                 /* lower thermal meter. */
419                                 CckTxPwrIdx -= (priv->ThermalMeter - CurrentThermal) * 2;
420                                 OfdmTxPwrIdx -= (priv->ThermalMeter - CurrentThermal) * 2;
421
422                                 if (CckTxPwrIdx < 0)
423                                         CckTxPwrIdx = 0;
424                                 if (OfdmTxPwrIdx < 0)
425                                         OfdmTxPwrIdx = 0;
426                         }
427
428                         /* Update TxPower level on CCK and OFDM resp. */
429                         priv->chtxpwr[Idx] = CckTxPwrIdx;
430                         priv->chtxpwr_ofdm[Idx] = OfdmTxPwrIdx;
431                 }
432
433                 /* Update TxPower level immediately. */
434                 rtl8225z2_SetTXPowerLevel(dev, priv->ieee80211->current_network.channel);
435         }
436         priv->ThermalMeter = CurrentThermal;
437 }
438 static void StaRateAdaptive87SE(struct net_device *dev)
439 {
440         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
441         unsigned long   CurrTxokCnt;
442         u16             CurrRetryCnt;
443         u16             CurrRetryRate;
444         unsigned long   CurrRxokCnt;
445         bool            bTryUp = false;
446         bool            bTryDown = false;
447         u8              TryUpTh = 1;
448         u8              TryDownTh = 2;
449         u32             TxThroughput;
450         long            CurrSignalStrength;
451         bool            bUpdateInitialGain = false;
452         u8              u1bOfdm = 0, u1bCck = 0;
453         char            OfdmTxPwrIdx, CckTxPwrIdx;
454
455         priv->RateAdaptivePeriod = RATE_ADAPTIVE_TIMER_PERIOD;
456
457
458         CurrRetryCnt    = priv->CurrRetryCnt;
459         CurrTxokCnt     = priv->NumTxOkTotal - priv->LastTxokCnt;
460         CurrRxokCnt     = priv->ieee80211->NumRxOkTotal - priv->LastRxokCnt;
461         CurrSignalStrength = priv->Stats_RecvSignalPower;
462         TxThroughput = (u32)(priv->NumTxOkBytesTotal - priv->LastTxOKBytes);
463         priv->LastTxOKBytes = priv->NumTxOkBytesTotal;
464         priv->CurrentOperaRate = priv->ieee80211->rate / 5;
465         /* 2 Compute retry ratio. */
466         if (CurrTxokCnt > 0) {
467                 CurrRetryRate = (u16)(CurrRetryCnt * 100 / CurrTxokCnt);
468         } else {
469         /* It may be serious retry. To distinguish serious retry or no packets modified by Bruce */
470                 CurrRetryRate = (u16)(CurrRetryCnt * 100 / 1);
471         }
472
473         priv->LastRetryCnt = priv->CurrRetryCnt;
474         priv->LastTxokCnt = priv->NumTxOkTotal;
475         priv->LastRxokCnt = priv->ieee80211->NumRxOkTotal;
476         priv->CurrRetryCnt = 0;
477
478         /* 2No Tx packets, return to init_rate or not? */
479         if (CurrRetryRate == 0 && CurrTxokCnt == 0) {
480                 /*
481                  * After 9 (30*300ms) seconds in this condition, we try to raise rate.
482                  */
483                 priv->TryupingCountNoData++;
484
485                 /* [TRC Dell Lab] Extend raised period from 4.5sec to 9sec, Isaiah 2008-02-15 18:00 */
486                 if (priv->TryupingCountNoData > 30) {
487                         priv->TryupingCountNoData = 0;
488                         priv->CurrentOperaRate = GetUpgradeTxRate(dev, priv->CurrentOperaRate);
489                         /* Reset Fail Record */
490                         priv->LastFailTxRate = 0;
491                         priv->LastFailTxRateSS = -200;
492                         priv->FailTxRateCount = 0;
493                 }
494                 goto SetInitialGain;
495         } else {
496                 priv->TryupingCountNoData = 0; /*Reset trying up times. */
497         }
498
499
500         /*
501          * For Netgear case, I comment out the following signal strength estimation,
502          * which can results in lower rate to transmit when sample is NOT enough (e.g. PING request).
503          *
504          * Restructure rate adaptive as the following main stages:
505          * (1) Add retry threshold in 54M upgrading condition with signal strength.
506          * (2) Add the mechanism to degrade to CCK rate according to signal strength
507          *              and retry rate.
508          * (3) Remove all Initial Gain Updates over OFDM rate. To avoid the complicated
509          *              situation, Initial Gain Update is upon on DIG mechanism except CCK rate.
510          * (4) Add the mechanism of trying to upgrade tx rate.
511          * (5) Record the information of upping tx rate to avoid trying upping tx rate constantly.
512          *
513          */
514
515         /*
516          * 11Mbps or 36Mbps
517          * Check more times in these rate(key rates).
518          */
519         if (priv->CurrentOperaRate == 22 || priv->CurrentOperaRate == 72)
520                 TryUpTh += 9;
521         /*
522          * Let these rates down more difficult.
523          */
524         if (MgntIsCckRate(priv->CurrentOperaRate) || priv->CurrentOperaRate == 36)
525                 TryDownTh += 1;
526
527         /* 1 Adjust Rate. */
528         if (priv->bTryuping == true) {
529                 /* 2 For Test Upgrading mechanism
530                  * Note:
531                  *      Sometimes the throughput is upon on the capability between the AP and NIC,
532                  *      thus the low data rate does not improve the performance.
533                  *      We randomly upgrade the data rate and check if the retry rate is improved.
534                  */
535
536                 /* Upgrading rate did not improve the retry rate, fallback to the original rate. */
537                 if ((CurrRetryRate > 25) && TxThroughput < priv->LastTxThroughput) {
538                         /*Not necessary raising rate, fall back rate. */
539                         bTryDown = true;
540                 } else {
541                         priv->bTryuping = false;
542                 }
543         } else if (CurrSignalStrength > -47 && (CurrRetryRate < 50)) {
544                 /*
545                  * 2For High Power
546                  *
547                  * Return to highest data rate, if signal strength is good enough.
548                  * SignalStrength threshold(-50dbm) is for RTL8186.
549                  * Revise SignalStrength threshold to -51dbm.
550                  */
551                 /* Also need to check retry rate for safety, by Bruce, 2007-06-05. */
552                 if (priv->CurrentOperaRate != priv->ieee80211->current_network.HighestOperaRate) {
553                         bTryUp = true;
554                         /* Upgrade Tx Rate directly. */
555                         priv->TryupingCount += TryUpTh;
556                 }
557
558         } else if (CurrTxokCnt > 9 && CurrTxokCnt < 100 && CurrRetryRate >= 600) {
559                 /*
560                  *2 For Serious Retry
561                  *
562                  * Traffic is not busy but our Tx retry is serious.
563                  */
564                 bTryDown = true;
565                 /* Let Rate Mechanism to degrade tx rate directly. */
566                 priv->TryDownCountLowData += TryDownTh;
567         } else if (priv->CurrentOperaRate == 108) {
568                 /* 2For 54Mbps */
569                 /* Air Link */
570                 if ((CurrRetryRate > 26) && (priv->LastRetryRate > 25)) {
571                         bTryDown = true;
572                 }
573                 /* Cable Link */
574                 else if ((CurrRetryRate > 17) && (priv->LastRetryRate > 16) && (CurrSignalStrength > -72)) {
575                         bTryDown = true;
576                 }
577
578                 if (bTryDown && (CurrSignalStrength < -75)) /* cable link */
579                         priv->TryDownCountLowData += TryDownTh;
580         } else if (priv->CurrentOperaRate == 96) {
581                 /* 2For 48Mbps */
582                 /* Air Link */
583                 if (((CurrRetryRate > 48) && (priv->LastRetryRate > 47))) {
584                         bTryDown = true;
585                 } else if (((CurrRetryRate > 21) && (priv->LastRetryRate > 20)) && (CurrSignalStrength > -74)) { /* Cable Link */
586                         /* Down to rate 36Mbps. */
587                         bTryDown = true;
588                 } else if ((CurrRetryRate > (priv->LastRetryRate + 50)) && (priv->FailTxRateCount > 2)) {
589                         bTryDown = true;
590                         priv->TryDownCountLowData += TryDownTh;
591                 } else if ((CurrRetryRate < 8) && (priv->LastRetryRate < 8)) { /* TO DO: need to consider (RSSI) */
592                         bTryUp = true;
593                 }
594
595                 if (bTryDown && (CurrSignalStrength < -75)) {
596                         priv->TryDownCountLowData += TryDownTh;
597                 }
598         } else if (priv->CurrentOperaRate == 72) {
599                 /* 2For 36Mbps */
600                 if ((CurrRetryRate > 43) && (priv->LastRetryRate > 41)) {
601                         /* Down to rate 24Mbps. */
602                         bTryDown = true;
603                 } else if ((CurrRetryRate > (priv->LastRetryRate + 50)) && (priv->FailTxRateCount > 2)) {
604                         bTryDown = true;
605                         priv->TryDownCountLowData += TryDownTh;
606                 } else if ((CurrRetryRate < 15) &&  (priv->LastRetryRate < 16)) { /* TO DO: need to consider (RSSI) */
607                         bTryUp = true;
608                 }
609
610                 if (bTryDown && (CurrSignalStrength < -80))
611                         priv->TryDownCountLowData += TryDownTh;
612
613         } else if (priv->CurrentOperaRate == 48) {
614                 /* 2For 24Mbps */
615                 /* Air Link */
616                 if (((CurrRetryRate > 63) && (priv->LastRetryRate > 62))) {
617                         bTryDown = true;
618                 } else if (((CurrRetryRate > 33) && (priv->LastRetryRate > 32)) && (CurrSignalStrength > -82)) { /* Cable Link */
619                         bTryDown = true;
620                 } else if ((CurrRetryRate > (priv->LastRetryRate + 50)) && (priv->FailTxRateCount > 2)) {
621                         bTryDown = true;
622                         priv->TryDownCountLowData += TryDownTh;
623                 } else if ((CurrRetryRate < 20) && (priv->LastRetryRate < 21)) { /* TO DO: need to consider (RSSI) */
624                         bTryUp = true;
625                 }
626
627                 if (bTryDown && (CurrSignalStrength < -82))
628                         priv->TryDownCountLowData += TryDownTh;
629
630         } else if (priv->CurrentOperaRate == 36) {
631                 if (((CurrRetryRate > 85) && (priv->LastRetryRate > 86))) {
632                         bTryDown = true;
633                 } else if ((CurrRetryRate > (priv->LastRetryRate + 50)) && (priv->FailTxRateCount > 2)) {
634                         bTryDown = true;
635                         priv->TryDownCountLowData += TryDownTh;
636                 } else if ((CurrRetryRate < 22) && (priv->LastRetryRate < 23)) { /* TO DO: need to consider (RSSI) */
637                         bTryUp = true;
638                 }
639         } else if (priv->CurrentOperaRate == 22) {
640                 /* 2For 11Mbps */
641                 if (CurrRetryRate > 95) {
642                         bTryDown = true;
643                 } else if ((CurrRetryRate < 29) && (priv->LastRetryRate < 30)) { /*TO DO: need to consider (RSSI) */
644                         bTryUp = true;
645                 }
646         } else if (priv->CurrentOperaRate == 11) {
647                 /* 2For 5.5Mbps */
648                 if (CurrRetryRate > 149) {
649                         bTryDown = true;
650                 } else if ((CurrRetryRate < 60) && (priv->LastRetryRate < 65)) {
651                         bTryUp = true;
652                 }
653         } else if (priv->CurrentOperaRate == 4) {
654                 /* 2For 2 Mbps */
655                 if ((CurrRetryRate > 99) && (priv->LastRetryRate > 99)) {
656                         bTryDown = true;
657                 } else if ((CurrRetryRate < 65) && (priv->LastRetryRate < 70)) {
658                         bTryUp = true;
659                 }
660         } else if (priv->CurrentOperaRate == 2) {
661                 /* 2For 1 Mbps */
662                 if ((CurrRetryRate < 70) && (priv->LastRetryRate < 75)) {
663                         bTryUp = true;
664                 }
665         }
666
667         if (bTryUp && bTryDown)
668                 printk("StaRateAdaptive87B(): Tx Rate tried upping and downing simultaneously!\n");
669
670         /* 1 Test Upgrading Tx Rate
671          * Sometimes the cause of the low throughput (high retry rate) is the compatibility between the AP and NIC.
672          * To test if the upper rate may cause lower retry rate, this mechanism randomly occurs to test upgrading tx rate.
673          */
674         if (!bTryUp && !bTryDown && (priv->TryupingCount == 0) && (priv->TryDownCountLowData == 0)
675                 && priv->CurrentOperaRate != priv->ieee80211->current_network.HighestOperaRate && priv->FailTxRateCount < 2) {
676                 if (jiffies % (CurrRetryRate + 101) == 0) {
677                         bTryUp = true;
678                         priv->bTryuping = true;
679                 }
680         }
681
682         /* 1 Rate Mechanism */
683         if (bTryUp) {
684                 priv->TryupingCount++;
685                 priv->TryDownCountLowData = 0;
686
687                 /*
688                  * Check more times if we need to upgrade indeed.
689                  * Because the largest value of pHalData->TryupingCount is 0xFFFF and
690                  * the largest value of pHalData->FailTxRateCount is 0x14,
691                  * this condition will be satisfied at most every 2 min.
692                  */
693
694                 if ((priv->TryupingCount > (TryUpTh + priv->FailTxRateCount * priv->FailTxRateCount)) ||
695                         (CurrSignalStrength > priv->LastFailTxRateSS) || priv->bTryuping) {
696                         priv->TryupingCount = 0;
697                         /*
698                          * When transferring from CCK to OFDM, DIG is an important issue.
699                          */
700                         if (priv->CurrentOperaRate == 22)
701                                 bUpdateInitialGain = true;
702
703                         /*
704                          * The difference in throughput between 48Mbps and 36Mbps is 8M.
705                          * So, we must be careful in this rate scale. Isaiah 2008-02-15.
706                          */
707                         if (((priv->CurrentOperaRate == 72) || (priv->CurrentOperaRate == 48) || (priv->CurrentOperaRate == 36)) &&
708                                 (priv->FailTxRateCount > 2))
709                                 priv->RateAdaptivePeriod = (RATE_ADAPTIVE_TIMER_PERIOD / 2);
710
711                         /* (1)To avoid upgrade frequently to the fail tx rate, add the FailTxRateCount into the threshold. */
712                         /* (2)If the signal strength is increased, it may be able to upgrade. */
713
714                         priv->CurrentOperaRate = GetUpgradeTxRate(dev, priv->CurrentOperaRate);
715
716                         if (priv->CurrentOperaRate == 36) {
717                                 priv->bUpdateARFR = true;
718                                 write_nic_word(dev, ARFR, 0x0F8F); /* bypass 12/9/6 */
719                         } else if (priv->bUpdateARFR) {
720                                 priv->bUpdateARFR = false;
721                                 write_nic_word(dev, ARFR, 0x0FFF); /* set 1M ~ 54Mbps. */
722                         }
723
724                         /* Update Fail Tx rate and count. */
725                         if (priv->LastFailTxRate != priv->CurrentOperaRate) {
726                                 priv->LastFailTxRate = priv->CurrentOperaRate;
727                                 priv->FailTxRateCount = 0;
728                                 priv->LastFailTxRateSS = -200; /* Set lowest power. */
729                         }
730                 }
731         } else {
732                 if (priv->TryupingCount > 0)
733                         priv->TryupingCount--;
734         }
735
736         if (bTryDown) {
737                 priv->TryDownCountLowData++;
738                 priv->TryupingCount = 0;
739
740                 /* Check if Tx rate can be degraded or Test trying upgrading should fallback. */
741                 if (priv->TryDownCountLowData > TryDownTh || priv->bTryuping) {
742                         priv->TryDownCountLowData = 0;
743                         priv->bTryuping = false;
744                         /* Update fail information. */
745                         if (priv->LastFailTxRate == priv->CurrentOperaRate) {
746                                 priv->FailTxRateCount++;
747                                 /* Record the Tx fail rate signal strength. */
748                                 if (CurrSignalStrength > priv->LastFailTxRateSS)
749                                         priv->LastFailTxRateSS = CurrSignalStrength;
750                         } else {
751                                 priv->LastFailTxRate = priv->CurrentOperaRate;
752                                 priv->FailTxRateCount = 1;
753                                 priv->LastFailTxRateSS = CurrSignalStrength;
754                         }
755                         priv->CurrentOperaRate = GetDegradeTxRate(dev, priv->CurrentOperaRate);
756
757                         /* Reduce chariot training time at weak signal strength situation. SD3 ED demand. */
758                         if ((CurrSignalStrength < -80) && (priv->CurrentOperaRate > 72)) {
759                                 priv->CurrentOperaRate = 72;
760                         }
761
762                         if (priv->CurrentOperaRate == 36) {
763                                 priv->bUpdateARFR = true;
764                                 write_nic_word(dev, ARFR, 0x0F8F); /* bypass 12/9/6 */
765                         } else if (priv->bUpdateARFR) {
766                                 priv->bUpdateARFR = false;
767                                 write_nic_word(dev, ARFR, 0x0FFF); /* set 1M ~ 54Mbps. */
768                         }
769
770                         /*
771                          * When it is CCK rate, it may need to update initial gain to receive lower power packets.
772                          */
773                         if (MgntIsCckRate(priv->CurrentOperaRate)) {
774                                 bUpdateInitialGain = true;
775                         }
776                 }
777         } else {
778                 if (priv->TryDownCountLowData > 0)
779                         priv->TryDownCountLowData--;
780         }
781
782         /*
783          * Keep the Tx fail rate count to equal to 0x15 at most.
784          * Reduce the fail count at least to 10 sec if tx rate is tending stable.
785          */
786         if (priv->FailTxRateCount >= 0x15 ||
787                 (!bTryUp && !bTryDown && priv->TryDownCountLowData == 0 && priv->TryupingCount && priv->FailTxRateCount > 0x6)) {
788                 priv->FailTxRateCount--;
789         }
790
791
792         OfdmTxPwrIdx  = priv->chtxpwr_ofdm[priv->ieee80211->current_network.channel];
793         CckTxPwrIdx  = priv->chtxpwr[priv->ieee80211->current_network.channel];
794
795         /* Mac0x9e increase 2 level in 36M~18M situation */
796         if ((priv->CurrentOperaRate < 96) && (priv->CurrentOperaRate > 22)) {
797                 u1bCck = read_nic_byte(dev, CCK_TXAGC);
798                 u1bOfdm = read_nic_byte(dev, OFDM_TXAGC);
799
800                 /* case 1: Never enter High power */
801                 if (u1bCck == CckTxPwrIdx) {
802                         if (u1bOfdm != (OfdmTxPwrIdx + 2)) {
803                         priv->bEnhanceTxPwr = true;
804                         u1bOfdm = ((u1bOfdm + 2) > 35) ? 35 : (u1bOfdm + 2);
805                         write_nic_byte(dev, OFDM_TXAGC, u1bOfdm);
806                         }
807                 } else if (u1bCck < CckTxPwrIdx) {
808                 /* case 2: enter high power */
809                         if (!priv->bEnhanceTxPwr) {
810                                 priv->bEnhanceTxPwr = true;
811                                 u1bOfdm = ((u1bOfdm + 2) > 35) ? 35 : (u1bOfdm + 2);
812                                 write_nic_byte(dev, OFDM_TXAGC, u1bOfdm);
813                         }
814                 }
815         } else if (priv->bEnhanceTxPwr) {  /* 54/48/11/5.5/2/1 */
816                 u1bCck = read_nic_byte(dev, CCK_TXAGC);
817                 u1bOfdm = read_nic_byte(dev, OFDM_TXAGC);
818
819                 /* case 1: Never enter High power */
820                 if (u1bCck == CckTxPwrIdx) {
821                         priv->bEnhanceTxPwr = false;
822                         write_nic_byte(dev, OFDM_TXAGC, OfdmTxPwrIdx);
823                 }
824                 /* case 2: enter high power */
825                 else if (u1bCck < CckTxPwrIdx) {
826                         priv->bEnhanceTxPwr = false;
827                         u1bOfdm = ((u1bOfdm - 2) > 0) ? (u1bOfdm - 2) : 0;
828                         write_nic_byte(dev, OFDM_TXAGC, u1bOfdm);
829                 }
830         }
831
832         /*
833          * We need update initial gain when we set tx rate "from OFDM to CCK" or
834          * "from CCK to OFDM".
835          */
836 SetInitialGain:
837         if (bUpdateInitialGain) {
838                 if (MgntIsCckRate(priv->CurrentOperaRate)) { /* CCK */
839                         if (priv->InitialGain > priv->RegBModeGainStage) {
840                                 priv->InitialGainBackUp = priv->InitialGain;
841
842                                 if (CurrSignalStrength < -85) /* Low power, OFDM [0x17] = 26. */
843                                         /* SD3 SYs suggest that CurrSignalStrength < -65, ofdm 0x17=26. */
844                                         priv->InitialGain = priv->RegBModeGainStage;
845
846                                 else if (priv->InitialGain > priv->RegBModeGainStage + 1)
847                                         priv->InitialGain -= 2;
848
849                                 else
850                                         priv->InitialGain--;
851
852                                 printk("StaRateAdaptive87SE(): update init_gain to index %d for date rate %d\n", priv->InitialGain, priv->CurrentOperaRate);
853                                 UpdateInitialGain(dev);
854                         }
855                 } else { /* OFDM */
856                         if (priv->InitialGain < 4) {
857                                 priv->InitialGainBackUp = priv->InitialGain;
858
859                                 priv->InitialGain++;
860                                 printk("StaRateAdaptive87SE(): update init_gain to index %d for date rate %d\n", priv->InitialGain, priv->CurrentOperaRate);
861                                 UpdateInitialGain(dev);
862                         }
863                 }
864         }
865
866         /* Record the related info */
867         priv->LastRetryRate = CurrRetryRate;
868         priv->LastTxThroughput = TxThroughput;
869         priv->ieee80211->rate = priv->CurrentOperaRate * 5;
870 }
871
872 void rtl8180_rate_adapter(struct work_struct *work)
873 {
874         struct delayed_work *dwork = to_delayed_work(work);
875         struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, rate_adapter_wq);
876         struct net_device *dev = ieee->dev;
877         StaRateAdaptive87SE(dev);
878 }
879 void timer_rate_adaptive(unsigned long data)
880 {
881         struct r8180_priv *priv = ieee80211_priv((struct net_device *)data);
882         if (!priv->up) {
883                 return;
884         }
885         if ((priv->ieee80211->iw_mode != IW_MODE_MASTER)
886                         && (priv->ieee80211->state == IEEE80211_LINKED) &&
887                         (priv->ForcedDataRate == 0)) {
888                 queue_work(priv->ieee80211->wq, (void *)&priv->ieee80211->rate_adapter_wq);
889         }
890         priv->rateadapter_timer.expires = jiffies + MSECS(priv->RateAdaptivePeriod);
891         add_timer(&priv->rateadapter_timer);
892 }
893
894 void SwAntennaDiversityRxOk8185(struct net_device *dev, u8 SignalStrength)
895 {
896         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
897
898         priv->AdRxOkCnt++;
899
900         if (priv->AdRxSignalStrength != -1) {
901                 priv->AdRxSignalStrength = ((priv->AdRxSignalStrength * 7) + (SignalStrength * 3)) / 10;
902         } else { /* Initialization case. */
903                 priv->AdRxSignalStrength = SignalStrength;
904         }
905
906         if (priv->LastRxPktAntenna) /* Main antenna. */
907                 priv->AdMainAntennaRxOkCnt++;
908         else     /* Aux antenna. */
909                 priv->AdAuxAntennaRxOkCnt++;
910 }
911  /*     Change Antenna Switch. */
912 bool SetAntenna8185(struct net_device *dev, u8 u1bAntennaIndex)
913 {
914         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
915         bool bAntennaSwitched = false;
916
917         switch (u1bAntennaIndex) {
918         case 0:
919                 /* Mac register, main antenna */
920                 write_nic_byte(dev, ANTSEL, 0x03);
921                 /* base band */
922                 write_phy_cck(dev, 0x11, 0x9b); /* Config CCK RX antenna. */
923                 write_phy_ofdm(dev, 0x0d, 0x5c); /* Config OFDM RX antenna. */
924
925                 bAntennaSwitched = true;
926                 break;
927
928         case 1:
929                 /* Mac register, aux antenna */
930                 write_nic_byte(dev, ANTSEL, 0x00);
931                 /* base band */
932                 write_phy_cck(dev, 0x11, 0xbb); /* Config CCK RX antenna. */
933                 write_phy_ofdm(dev, 0x0d, 0x54); /* Config OFDM RX antenna. */
934
935                 bAntennaSwitched = true;
936
937                 break;
938
939         default:
940                 printk("SetAntenna8185: unknown u1bAntennaIndex(%d)\n", u1bAntennaIndex);
941                 break;
942         }
943
944         if (bAntennaSwitched)
945                 priv->CurrAntennaIndex = u1bAntennaIndex;
946
947         return bAntennaSwitched;
948 }
949  /*     Toggle Antenna switch. */
950 bool SwitchAntenna(struct net_device *dev)
951 {
952         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
953
954         bool            bResult;
955
956         if (priv->CurrAntennaIndex == 0) {
957                 bResult = SetAntenna8185(dev, 1);
958         } else {
959                 bResult = SetAntenna8185(dev, 0);
960         }
961
962         return bResult;
963 }
964 /*
965  * Engine of SW Antenna Diversity mechanism.
966  * Since 8187 has no Tx part information,
967  * this implementation is only dependend on Rx part information.
968  */
969 void SwAntennaDiversity(struct net_device *dev)
970 {
971         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
972         bool   bSwCheckSS = false;
973         if (bSwCheckSS) {
974                 priv->AdTickCount++;
975
976                 printk("(1) AdTickCount: %d, AdCheckPeriod: %d\n",
977                         priv->AdTickCount, priv->AdCheckPeriod);
978                 printk("(2) AdRxSignalStrength: %ld, AdRxSsThreshold: %ld\n",
979                         priv->AdRxSignalStrength, priv->AdRxSsThreshold);
980         }
981
982         /* Case 1. No Link. */
983         if (priv->ieee80211->state != IEEE80211_LINKED) {
984                 priv->bAdSwitchedChecking = false;
985                 /* I switch antenna here to prevent any one of antenna is broken before link established, 2006.04.18, by rcnjko.. */
986                 SwitchAntenna(dev);
987
988           /* Case 2. Linked but no packet receive.d */
989         } else if (priv->AdRxOkCnt == 0) {
990                 priv->bAdSwitchedChecking = false;
991                 SwitchAntenna(dev);
992
993           /* Case 3. Evaluate last antenna switch action and undo it if necessary. */
994         } else if (priv->bAdSwitchedChecking == true) {
995                 priv->bAdSwitchedChecking = false;
996
997                 /* Adjust Rx signal strength threshold. */
998                 priv->AdRxSsThreshold = (priv->AdRxSignalStrength + priv->AdRxSsBeforeSwitched) / 2;
999
1000                 priv->AdRxSsThreshold = (priv->AdRxSsThreshold > priv->AdMaxRxSsThreshold) ?
1001                                         priv->AdMaxRxSsThreshold : priv->AdRxSsThreshold;
1002                 if (priv->AdRxSignalStrength < priv->AdRxSsBeforeSwitched) {
1003                 /* Rx signal strength is not improved after we swtiched antenna. => Swich back. */
1004                         /* Increase Antenna Diversity checking period due to bad decision. */
1005                         priv->AdCheckPeriod *= 2;
1006                         /* Increase Antenna Diversity checking period. */
1007                         if (priv->AdCheckPeriod > priv->AdMaxCheckPeriod)
1008                                 priv->AdCheckPeriod = priv->AdMaxCheckPeriod;
1009
1010                         /* Wrong decision => switch back. */
1011                         SwitchAntenna(dev);
1012                 } else {
1013                 /* Rx Signal Strength is improved. */
1014
1015                         /* Reset Antenna Diversity checking period to its min value. */
1016                         priv->AdCheckPeriod = priv->AdMinCheckPeriod;
1017                 }
1018
1019         }
1020         /* Case 4. Evaluate if we shall switch antenna now. */
1021         /* Cause Table Speed is very fast in TRC Dell Lab, we check it every time. */
1022         else {
1023                 priv->AdTickCount = 0;
1024
1025                 /*
1026                  * <Roger_Notes> We evaluate RxOk counts for each antenna first and than
1027                  * evaluate signal strength.
1028                  * The following operation can overcome the disability of CCA on both two antennas
1029                  * When signal strength was extremely low or high.
1030                  * 2008.01.30.
1031                  */
1032
1033                 /*
1034                  * Evaluate RxOk count from each antenna if we shall switch default antenna now.
1035                  */
1036                 if ((priv->AdMainAntennaRxOkCnt < priv->AdAuxAntennaRxOkCnt)
1037                         && (priv->CurrAntennaIndex == 0)) {
1038                 /* We set Main antenna as default but RxOk count was less than Aux ones. */
1039
1040                         /* Switch to Aux antenna. */
1041                         SwitchAntenna(dev);
1042                         priv->bHWAdSwitched = true;
1043                 } else if ((priv->AdAuxAntennaRxOkCnt < priv->AdMainAntennaRxOkCnt)
1044                         && (priv->CurrAntennaIndex == 1)) {
1045                 /* We set Aux antenna as default but RxOk count was less than Main ones. */
1046
1047                         /* Switch to Main antenna. */
1048                         SwitchAntenna(dev);
1049                         priv->bHWAdSwitched = true;
1050                 } else {
1051                 /* Default antenna is better. */
1052
1053                         /* Still need to check current signal strength. */
1054                         priv->bHWAdSwitched = false;
1055                 }
1056                 /*
1057                  * <Roger_Notes> We evaluate Rx signal strength ONLY when default antenna
1058                  * didn't change by HW evaluation.
1059                  * 2008.02.27.
1060                  *
1061                  * [TRC Dell Lab] SignalStrength is inaccuracy. Isaiah 2008-03-05
1062                  * For example, Throughput of aux is better than main antenna(about 10M v.s 2M),
1063                  * but AdRxSignalStrength is less than main.
1064                  * Our guess is that main antenna have lower throughput and get many change
1065                  * to receive more CCK packets(ex.Beacon) which have stronger SignalStrength.
1066                  */
1067                 if ((!priv->bHWAdSwitched) && (bSwCheckSS)) {
1068                         /* Evaluate Rx signal strength if we shall switch antenna now. */
1069                         if (priv->AdRxSignalStrength < priv->AdRxSsThreshold) {
1070                         /* Rx signal strength is weak => Switch Antenna. */
1071                                 priv->AdRxSsBeforeSwitched = priv->AdRxSignalStrength;
1072                                 priv->bAdSwitchedChecking = true;
1073
1074                                 SwitchAntenna(dev);
1075                         } else {
1076                         /* Rx signal strength is OK. */
1077                                 priv->bAdSwitchedChecking = false;
1078                                 /* Increase Rx signal strength threshold if necessary. */
1079                                 if ((priv->AdRxSignalStrength > (priv->AdRxSsThreshold + 10)) && /* Signal is much stronger than current threshold */
1080                                         priv->AdRxSsThreshold <= priv->AdMaxRxSsThreshold) { /* Current threhold is not yet reach upper limit. */
1081
1082                                         priv->AdRxSsThreshold = (priv->AdRxSsThreshold + priv->AdRxSignalStrength) / 2;
1083                                         priv->AdRxSsThreshold = (priv->AdRxSsThreshold > priv->AdMaxRxSsThreshold) ?
1084                                                                 priv->AdMaxRxSsThreshold : priv->AdRxSsThreshold;/* +by amy 080312 */
1085                                 }
1086
1087                                 /* Reduce Antenna Diversity checking period if possible. */
1088                                 if (priv->AdCheckPeriod > priv->AdMinCheckPeriod)
1089                                         priv->AdCheckPeriod /= 2;
1090                         }
1091                 }
1092         }
1093         /* Reset antenna diversity Rx related statistics. */
1094         priv->AdRxOkCnt = 0;
1095         priv->AdMainAntennaRxOkCnt = 0;
1096         priv->AdAuxAntennaRxOkCnt = 0;
1097 }
1098
1099  /*     Return TRUE if we shall perform Tx Power Tracking Mechanism, FALSE otherwise. */
1100 bool CheckTxPwrTracking(struct net_device *dev)
1101 {
1102         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1103
1104         if (!priv->bTxPowerTrack)
1105                 return false;
1106
1107         /* if 87SE is in High Power , don't do Tx Power Tracking. asked by SD3 ED. 2008-08-08 Isaiah */
1108         if (priv->bToUpdateTxPwr)
1109                 return false;
1110
1111         return true;
1112 }
1113
1114
1115  /*     Timer callback function of SW Antenna Diversity. */
1116 void SwAntennaDiversityTimerCallback(struct net_device *dev)
1117 {
1118         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1119         enum rt_rf_power_state rtState;
1120
1121          /* We do NOT need to switch antenna while RF is off. */
1122         rtState = priv->eRFPowerState;
1123         do {
1124                 if (rtState == RF_OFF) {
1125                         break;
1126                 } else if (rtState == RF_SLEEP) {
1127                         /* Don't access BB/RF under Disable PLL situation. */
1128                         break;
1129                 }
1130                 SwAntennaDiversity(dev);
1131
1132         } while (false);
1133
1134         if (priv->up) {
1135                 priv->SwAntennaDiversityTimer.expires = jiffies + MSECS(ANTENNA_DIVERSITY_TIMER_PERIOD);
1136                 add_timer(&priv->SwAntennaDiversityTimer);
1137         }
1138 }
1139