Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / staging / rtl8723au / hal / odm_HWConfig.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  ******************************************************************************/
15
16 /*  */
17 /*  include files */
18 /*  */
19
20 #include "odm_precomp.h"
21
22 static u8 odm_QueryRxPwrPercentage(s8 AntPower)
23 {
24         if ((AntPower <= -100) || (AntPower >= 20))
25                 return  0;
26         else if (AntPower >= 0)
27                 return  100;
28         else
29                 return  100 + AntPower;
30 }
31
32 static s32 odm_SignalScaleMapping_92CSeries(struct dm_odm_t *pDM_Odm, s32 CurrSig)
33 {
34         s32 RetSig = 0;
35
36         if (CurrSig >= 51 && CurrSig <= 100)
37                 RetSig = 100;
38         else if (CurrSig >= 41 && CurrSig <= 50)
39                 RetSig = 80 + ((CurrSig - 40)*2);
40         else if (CurrSig >= 31 && CurrSig <= 40)
41                 RetSig = 66 + (CurrSig - 30);
42         else if (CurrSig >= 21 && CurrSig <= 30)
43                 RetSig = 54 + (CurrSig - 20);
44         else if (CurrSig >= 10 && CurrSig <= 20)
45                 RetSig = 42 + (((CurrSig - 10) * 2) / 3);
46         else if (CurrSig >= 5 && CurrSig <= 9)
47                 RetSig = 22 + (((CurrSig - 5) * 3) / 2);
48         else if (CurrSig >= 1 && CurrSig <= 4)
49                 RetSig = 6 + (((CurrSig - 1) * 3) / 2);
50         else
51                 RetSig = CurrSig;
52
53         return RetSig;
54 }
55
56 static s32 odm_SignalScaleMapping(struct dm_odm_t *pDM_Odm, s32 CurrSig)
57 {
58         return odm_SignalScaleMapping_92CSeries(pDM_Odm, CurrSig);
59 }
60
61 static u8
62 odm_EVMdbToPercentage(
63         s8 Value
64   )
65 {
66         /*  */
67         /*  -33dB~0dB to 0%~99% */
68         /*  */
69         s8 ret_val;
70
71         ret_val = Value;
72
73         if (ret_val >= 0)
74                 ret_val = 0;
75         if (ret_val <= -33)
76                 ret_val = -33;
77
78         ret_val = 0 - ret_val;
79         ret_val *= 3;
80
81         if (ret_val == 99)
82                 ret_val = 100;
83
84         return ret_val;
85 }
86
87 static void odm_RxPhyStatus92CSeries_Parsing(struct dm_odm_t *pDM_Odm,
88                                              struct phy_info *pPhyInfo,
89                                              u8 *pPhyStatus,
90                                              struct odm_packet_info *pPktinfo)
91 {
92         struct phy_status_rpt *pPhyStaRpt = (struct phy_status_rpt *)pPhyStatus;
93         u8 i, Max_spatial_stream;
94         s8 rx_pwr[4], rx_pwr_all = 0;
95         u8 EVM, PWDB_ALL = 0, PWDB_ALL_BT;
96         u8 RSSI, total_rssi = 0;
97         u8 isCCKrate = 0;
98         u8 rf_rx_num = 0;
99         u8 cck_highpwr = 0;
100
101         isCCKrate = (pPktinfo->Rate <= DESC92C_RATE11M) ? true : false;
102         pPhyInfo->RxMIMOSignalQuality[RF_PATH_A] = -1;
103         pPhyInfo->RxMIMOSignalQuality[RF_PATH_B] = -1;
104
105         if (isCCKrate) {
106                 u8 report;
107                 u8 cck_agc_rpt;
108
109                 pDM_Odm->PhyDbgInfo.NumQryPhyStatusCCK++;
110                 /*  (1)Hardware does not provide RSSI for CCK */
111                 /*  (2)PWDB, Average PWDB cacluated by hardware (for rate adaptive) */
112
113                 cck_highpwr = pDM_Odm->bCckHighPower;
114
115                 cck_agc_rpt =  pPhyStaRpt->cck_agc_rpt_ofdm_cfosho_a;
116
117                 /* The RSSI formula should be modified according to the gain table */
118                 if (!cck_highpwr) {
119                         report = (cck_agc_rpt & 0xc0)>>6;
120                         switch (report) {
121                         /*  Modify the RF RNA gain value to -40, -20, -2, 14 by Jenyu's suggestion */
122                         /*  Note: different RF with the different RNA gain. */
123                         case 0x3:
124                                 rx_pwr_all = -46 - (cck_agc_rpt & 0x3e);
125                                 break;
126                         case 0x2:
127                                 rx_pwr_all = -26 - (cck_agc_rpt & 0x3e);
128                                 break;
129                         case 0x1:
130                                 rx_pwr_all = -12 - (cck_agc_rpt & 0x3e);
131                                 break;
132                         case 0x0:
133                                 rx_pwr_all = 16 - (cck_agc_rpt & 0x3e);
134                                 break;
135                         }
136                 } else {
137                         report = (cck_agc_rpt & 0x60)>>5;
138                         switch (report) {
139                         case 0x3:
140                                 rx_pwr_all = -46 - ((cck_agc_rpt & 0x1f)<<1);
141                                 break;
142                         case 0x2:
143                                 rx_pwr_all = -26 - ((cck_agc_rpt & 0x1f)<<1);
144                                 break;
145                         case 0x1:
146                                 rx_pwr_all = -12 - ((cck_agc_rpt & 0x1f)<<1);
147                                 break;
148                         case 0x0:
149                                 rx_pwr_all = 16 - ((cck_agc_rpt & 0x1f)<<1);
150                                 break;
151                         }
152                 }
153
154                 PWDB_ALL = odm_QueryRxPwrPercentage(rx_pwr_all);
155
156                 /* Modification for ext-LNA board */
157                 if (pDM_Odm->BoardType == ODM_BOARD_HIGHPWR) {
158                         if ((cck_agc_rpt>>7) == 0) {
159                                 PWDB_ALL = (PWDB_ALL > 94) ? 100 : (PWDB_ALL+6);
160                         } else {
161                                 if (PWDB_ALL > 38)
162                                         PWDB_ALL -= 16;
163                                 else
164                                         PWDB_ALL = (PWDB_ALL <= 16) ? (PWDB_ALL>>2) : (PWDB_ALL-12);
165                         }
166
167                         /* CCK modification */
168                         if (PWDB_ALL > 25 && PWDB_ALL <= 60)
169                                 PWDB_ALL += 6;
170                 } else { /* Modification for int-LNA board */
171                         if (PWDB_ALL > 99)
172                                 PWDB_ALL -= 8;
173                         else if (PWDB_ALL > 50 && PWDB_ALL <= 68)
174                                 PWDB_ALL += 4;
175                 }
176                 pPhyInfo->RxPWDBAll = PWDB_ALL;
177                 pPhyInfo->BTRxRSSIPercentage = PWDB_ALL;
178                 pPhyInfo->RecvSignalPower = rx_pwr_all;
179                 /*  (3) Get Signal Quality (EVM) */
180                 if (pPktinfo->bPacketMatchBSSID) {
181                         u8      SQ, SQ_rpt;
182
183                         SQ_rpt = pPhyStaRpt->cck_sig_qual_ofdm_pwdb_all;
184
185                         if (SQ_rpt > 64)
186                                 SQ = 0;
187                         else if (SQ_rpt < 20)
188                                 SQ = 100;
189                         else
190                                 SQ = ((64-SQ_rpt) * 100) / 44;
191
192                         pPhyInfo->SignalQuality = SQ;
193                         pPhyInfo->RxMIMOSignalQuality[RF_PATH_A] = SQ;
194                         pPhyInfo->RxMIMOSignalQuality[RF_PATH_B] = -1;
195                 }
196         } else { /* is OFDM rate */
197                 pDM_Odm->PhyDbgInfo.NumQryPhyStatusOFDM++;
198
199                 /*  (1)Get RSSI for HT rate */
200
201                 for (i = RF_PATH_A; i < RF_PATH_MAX; i++) {
202                         /*  2008/01/30 MH we will judge RF RX path now. */
203                         if (pDM_Odm->RFPathRxEnable & BIT(i))
204                                 rf_rx_num++;
205
206                         rx_pwr[i] = ((pPhyStaRpt->path_agc[i].gain & 0x3F)*2) - 110;
207
208                         pPhyInfo->RxPwr[i] = rx_pwr[i];
209
210                         /* Translate DBM to percentage. */
211                         RSSI = odm_QueryRxPwrPercentage(rx_pwr[i]);
212                         total_rssi += RSSI;
213
214                         /* Modification for ext-LNA board */
215                         if (pDM_Odm->BoardType == ODM_BOARD_HIGHPWR) {
216                                 if ((pPhyStaRpt->path_agc[i].trsw) == 1)
217                                         RSSI = (RSSI > 94) ? 100 : (RSSI+6);
218                                 else
219                                         RSSI = (RSSI <= 16) ? (RSSI>>3) : (RSSI-16);
220
221                                 if ((RSSI <= 34) && (RSSI >= 4))
222                                         RSSI -= 4;
223                         }
224
225                         pPhyInfo->RxMIMOSignalStrength[i] = (u8) RSSI;
226
227                         /* Get Rx snr value in DB */
228                         pPhyInfo->RxSNR[i] = pDM_Odm->PhyDbgInfo.RxSNRdB[i] = (s32)(pPhyStaRpt->path_rxsnr[i]/2);
229                 }
230
231                 /*  (2)PWDB, Average PWDB cacluated by hardware (for rate adaptive) */
232                 rx_pwr_all = (((pPhyStaRpt->cck_sig_qual_ofdm_pwdb_all) >> 1) & 0x7f)-110;
233
234                 PWDB_ALL = odm_QueryRxPwrPercentage(rx_pwr_all);
235                 PWDB_ALL_BT = PWDB_ALL;
236
237                 pPhyInfo->RxPWDBAll = PWDB_ALL;
238                 pPhyInfo->BTRxRSSIPercentage = PWDB_ALL_BT;
239                 pPhyInfo->RxPower = rx_pwr_all;
240                 pPhyInfo->RecvSignalPower = rx_pwr_all;
241
242                 /*  (3)EVM of HT rate */
243                 if (pPktinfo->Rate >= DESC92C_RATEMCS8 && pPktinfo->Rate <= DESC92C_RATEMCS15)
244                         Max_spatial_stream = 2; /* both spatial stream make sense */
245                 else
246                         Max_spatial_stream = 1; /* only spatial stream 1 makes sense */
247
248                 for (i = 0; i < Max_spatial_stream; i++) {
249                         /*  Do not use shift operation like "rx_evmX >>= 1" because the compilor of free build environment */
250                         /*  fill most significant bit to "zero" when doing shifting operation which may change a negative */
251                         /*  value to positive one, then the dbm value (which is supposed to be negative)  is not correct anymore. */
252                         EVM = odm_EVMdbToPercentage((pPhyStaRpt->stream_rxevm[i]));     /* dbm */
253
254                         if (pPktinfo->bPacketMatchBSSID) {
255                                 if (i == RF_PATH_A) {
256                                         /*  Fill value in RFD, Get the first spatial stream only */
257                                         pPhyInfo->SignalQuality = (u8)(EVM & 0xff);
258                                 }
259                                 pPhyInfo->RxMIMOSignalQuality[i] = (u8)(EVM & 0xff);
260                         }
261                 }
262         }
263         /* UI BSS List signal strength(in percentage), make it good looking, from 0~100. */
264         /* It is assigned to the BSS List in GetValueFromBeaconOrProbeRsp(). */
265         if (isCCKrate) {
266                 pPhyInfo->SignalStrength = (u8)(odm_SignalScaleMapping(pDM_Odm, PWDB_ALL));/* PWDB_ALL; */
267         } else {
268                 if (rf_rx_num != 0)
269                         pPhyInfo->SignalStrength = (u8)(odm_SignalScaleMapping(pDM_Odm, total_rssi /= rf_rx_num));
270         }
271 }
272
273 void odm_Init_RSSIForDM23a(struct dm_odm_t *pDM_Odm)
274 {
275 }
276
277 static void odm_Process_RSSIForDM(struct dm_odm_t *pDM_Odm,
278                                   struct phy_info *pPhyInfo,
279                                   struct odm_packet_info *pPktinfo)
280 {
281         s32 UndecoratedSmoothedPWDB, UndecoratedSmoothedCCK;
282         s32 UndecoratedSmoothedOFDM, RSSI_Ave;
283         u8 isCCKrate = 0;
284         u8 RSSI_max, RSSI_min, i;
285         u32 OFDM_pkt = 0;
286         u32 Weighting = 0;
287         struct sta_info *pEntry;
288
289         if (pPktinfo->StationID == 0xFF)
290                 return;
291
292         pEntry = pDM_Odm->pODM_StaInfo[pPktinfo->StationID];
293         if (!pEntry)
294                 return;
295         if ((!pPktinfo->bPacketMatchBSSID))
296                 return;
297
298         isCCKrate = (pPktinfo->Rate <= DESC92C_RATE11M) ? true : false;
299
300         /* Smart Antenna Debug Message------------------*/
301
302         UndecoratedSmoothedCCK =  pEntry->rssi_stat.UndecoratedSmoothedCCK;
303         UndecoratedSmoothedOFDM = pEntry->rssi_stat.UndecoratedSmoothedOFDM;
304         UndecoratedSmoothedPWDB = pEntry->rssi_stat.UndecoratedSmoothedPWDB;
305
306         if (pPktinfo->bPacketToSelf || pPktinfo->bPacketBeacon) {
307                 if (!isCCKrate) { /* ofdm rate */
308                         if (pPhyInfo->RxMIMOSignalStrength[RF_PATH_B] == 0) {
309                                 RSSI_Ave = pPhyInfo->RxMIMOSignalStrength[RF_PATH_A];
310                         } else {
311                                 if (pPhyInfo->RxMIMOSignalStrength[RF_PATH_A] > pPhyInfo->RxMIMOSignalStrength[RF_PATH_B]) {
312                                         RSSI_max = pPhyInfo->RxMIMOSignalStrength[RF_PATH_A];
313                                         RSSI_min = pPhyInfo->RxMIMOSignalStrength[RF_PATH_B];
314                                 } else {
315                                         RSSI_max = pPhyInfo->RxMIMOSignalStrength[RF_PATH_B];
316                                         RSSI_min = pPhyInfo->RxMIMOSignalStrength[RF_PATH_A];
317                                 }
318                                 if ((RSSI_max - RSSI_min) < 3)
319                                         RSSI_Ave = RSSI_max;
320                                 else if ((RSSI_max - RSSI_min) < 6)
321                                         RSSI_Ave = RSSI_max - 1;
322                                 else if ((RSSI_max - RSSI_min) < 10)
323                                         RSSI_Ave = RSSI_max - 2;
324                                 else
325                                         RSSI_Ave = RSSI_max - 3;
326                         }
327
328                         /* 1 Process OFDM RSSI */
329                         if (UndecoratedSmoothedOFDM <= 0) {
330                                 /*  initialize */
331                                 UndecoratedSmoothedOFDM = pPhyInfo->RxPWDBAll;
332                         } else {
333                                 if (pPhyInfo->RxPWDBAll > (u32)UndecoratedSmoothedOFDM) {
334                                         UndecoratedSmoothedOFDM =
335                                                         (((UndecoratedSmoothedOFDM)*(Rx_Smooth_Factor-1)) +
336                                                         (RSSI_Ave)) / (Rx_Smooth_Factor);
337                                         UndecoratedSmoothedOFDM = UndecoratedSmoothedOFDM + 1;
338                                 } else {
339                                         UndecoratedSmoothedOFDM =
340                                                         (((UndecoratedSmoothedOFDM)*(Rx_Smooth_Factor-1)) +
341                                                         (RSSI_Ave)) / (Rx_Smooth_Factor);
342                                 }
343                         }
344                         pEntry->rssi_stat.PacketMap =
345                                 (pEntry->rssi_stat.PacketMap<<1) | BIT(0);
346                 } else {
347                         RSSI_Ave = pPhyInfo->RxPWDBAll;
348
349                         /* 1 Process CCK RSSI */
350                         if (UndecoratedSmoothedCCK <= 0) {
351                                 /*  initialize */
352                                 UndecoratedSmoothedCCK = pPhyInfo->RxPWDBAll;
353                         } else {
354                                 if (pPhyInfo->RxPWDBAll > (u32)UndecoratedSmoothedCCK) {
355                                         UndecoratedSmoothedCCK =
356                                                         (((UndecoratedSmoothedCCK)*(Rx_Smooth_Factor-1)) +
357                                                         (pPhyInfo->RxPWDBAll)) / (Rx_Smooth_Factor);
358                                         UndecoratedSmoothedCCK = UndecoratedSmoothedCCK + 1;
359                                 } else {
360                                         UndecoratedSmoothedCCK =
361                                                         (((UndecoratedSmoothedCCK)*(Rx_Smooth_Factor-1)) +
362                                                         (pPhyInfo->RxPWDBAll)) / (Rx_Smooth_Factor);
363                                 }
364                         }
365                         pEntry->rssi_stat.PacketMap = pEntry->rssi_stat.PacketMap<<1;
366                 }
367
368                 /* 2011.07.28 LukeLee: modified to prevent unstable CCK RSSI */
369                 if (pEntry->rssi_stat.ValidBit >= 64)
370                         pEntry->rssi_stat.ValidBit = 64;
371                 else
372                         pEntry->rssi_stat.ValidBit++;
373
374                 for (i = 0; i < pEntry->rssi_stat.ValidBit; i++)
375                         OFDM_pkt +=
376                                 (u8)(pEntry->rssi_stat.PacketMap>>i) & BIT(0);
377
378                 if (pEntry->rssi_stat.ValidBit == 64) {
379                         Weighting = ((OFDM_pkt<<4) > 64)?64:(OFDM_pkt<<4);
380                         UndecoratedSmoothedPWDB = (Weighting*UndecoratedSmoothedOFDM+(64-Weighting)*UndecoratedSmoothedCCK)>>6;
381                 } else {
382                         if (pEntry->rssi_stat.ValidBit != 0)
383                                 UndecoratedSmoothedPWDB = (OFDM_pkt*UndecoratedSmoothedOFDM+(pEntry->rssi_stat.ValidBit-OFDM_pkt)*UndecoratedSmoothedCCK)/pEntry->rssi_stat.ValidBit;
384                         else
385                                 UndecoratedSmoothedPWDB = 0;
386                 }
387                 pEntry->rssi_stat.UndecoratedSmoothedCCK = UndecoratedSmoothedCCK;
388                 pEntry->rssi_stat.UndecoratedSmoothedOFDM = UndecoratedSmoothedOFDM;
389                 pEntry->rssi_stat.UndecoratedSmoothedPWDB = UndecoratedSmoothedPWDB;
390         }
391 }
392
393 void ODM_PhyStatusQuery23a(struct dm_odm_t *pDM_Odm, struct phy_info *pPhyInfo,
394                            u8 *pPhyStatus, struct odm_packet_info *pPktinfo)
395 {
396         odm_RxPhyStatus92CSeries_Parsing(pDM_Odm, pPhyInfo,
397                                          pPhyStatus, pPktinfo);
398
399         odm_Process_RSSIForDM(pDM_Odm, pPhyInfo, pPktinfo);
400 }