Merge tag 'staging-5.6-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[linux-2.6-microblaze.git] / drivers / staging / rtl8188eu / hal / rtl8188e_dm.c
1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 /*  */
8 /*  Description: */
9 /*  */
10 /*  This file is for 92CE/92CU dynamic mechanism only */
11 /*  */
12 /*  */
13 /*  */
14 #define _RTL8188E_DM_C_
15
16 #include <osdep_service.h>
17 #include <drv_types.h>
18
19 #include <rtl8188e_hal.h>
20
21 /*  Initialize GPIO setting registers */
22 static void dm_InitGPIOSetting(struct adapter *Adapter)
23 {
24         u8 tmp1byte;
25
26         tmp1byte = usb_read8(Adapter, REG_GPIO_MUXCFG);
27         tmp1byte &= (GPIOSEL_GPIO | ~GPIOSEL_ENBT);
28
29         usb_write8(Adapter, REG_GPIO_MUXCFG, tmp1byte);
30 }
31
32 /*  */
33 /*  functions */
34 /*  */
35 static void Init_ODM_ComInfo_88E(struct adapter *Adapter)
36 {
37         struct hal_data_8188e *hal_data = Adapter->HalData;
38         struct dm_priv *pdmpriv = &hal_data->dmpriv;
39         struct odm_dm_struct *dm_odm = &hal_data->odmpriv;
40
41         /*  Init Value */
42         memset(dm_odm, 0, sizeof(*dm_odm));
43
44         dm_odm->Adapter = Adapter;
45         dm_odm->SupportPlatform = ODM_CE;
46         dm_odm->SupportICType = ODM_RTL8188E;
47         dm_odm->CutVersion = ODM_CUT_A;
48         dm_odm->bIsMPChip = hal_data->VersionID.ChipType == NORMAL_CHIP;
49         dm_odm->PatchID = hal_data->CustomerID;
50         dm_odm->bWIFITest = Adapter->registrypriv.wifi_spec;
51
52         dm_odm->AntDivType = hal_data->TRxAntDivType;
53
54         /* Tx power tracking BB swing table.
55          * The base index =
56          * 12. +((12-n)/2)dB 13~?? = decrease tx pwr by -((n-12)/2)dB
57          */
58         dm_odm->BbSwingIdxOfdm = 12; /*  Set defalut value as index 12. */
59         dm_odm->BbSwingIdxOfdmCurrent = 12;
60         dm_odm->BbSwingFlagOfdm = false;
61
62         pdmpriv->InitODMFlag = ODM_RF_CALIBRATION |
63                                ODM_RF_TX_PWR_TRACK;
64
65         dm_odm->SupportAbility = pdmpriv->InitODMFlag;
66 }
67
68 static void Update_ODM_ComInfo_88E(struct adapter *Adapter)
69 {
70         struct mlme_ext_priv *pmlmeext = &Adapter->mlmeextpriv;
71         struct mlme_priv *pmlmepriv = &Adapter->mlmepriv;
72         struct pwrctrl_priv *pwrctrlpriv = &Adapter->pwrctrlpriv;
73         struct hal_data_8188e *hal_data = Adapter->HalData;
74         struct odm_dm_struct *dm_odm = &hal_data->odmpriv;
75         struct dm_priv *pdmpriv = &hal_data->dmpriv;
76         int i;
77
78         pdmpriv->InitODMFlag = ODM_BB_DIG |
79                                ODM_BB_RA_MASK |
80                                ODM_BB_DYNAMIC_TXPWR |
81                                ODM_BB_FA_CNT |
82                                ODM_BB_RSSI_MONITOR |
83                                ODM_BB_CCK_PD |
84                                ODM_BB_PWR_SAVE |
85                                ODM_MAC_EDCA_TURBO |
86                                ODM_RF_CALIBRATION |
87                                ODM_RF_TX_PWR_TRACK;
88         if (hal_data->AntDivCfg)
89                 pdmpriv->InitODMFlag |= ODM_BB_ANT_DIV;
90
91         if (Adapter->registrypriv.mp_mode == 1) {
92                 pdmpriv->InitODMFlag = ODM_RF_CALIBRATION |
93                                        ODM_RF_TX_PWR_TRACK;
94         }
95
96         dm_odm->SupportAbility = pdmpriv->InitODMFlag;
97
98         dm_odm->pNumTxBytesUnicast = &Adapter->xmitpriv.tx_bytes;
99         dm_odm->pNumRxBytesUnicast = &Adapter->recvpriv.rx_bytes;
100         dm_odm->pWirelessMode = &pmlmeext->cur_wireless_mode;
101         dm_odm->pSecChOffset = &hal_data->nCur40MhzPrimeSC;
102         dm_odm->pSecurity = (u8 *)&Adapter->securitypriv.dot11PrivacyAlgrthm;
103         dm_odm->pBandWidth = (u8 *)&hal_data->CurrentChannelBW;
104         dm_odm->pChannel = &hal_data->CurrentChannel;
105         dm_odm->pbNet_closed = (bool *)&Adapter->net_closed;
106         dm_odm->mp_mode = &Adapter->registrypriv.mp_mode;
107         dm_odm->pbScanInProcess = (bool *)&pmlmepriv->bScanInProcess;
108         dm_odm->pbPowerSaving = (bool *)&pwrctrlpriv->bpower_saving;
109         dm_odm->AntDivType = hal_data->TRxAntDivType;
110
111         /* Tx power tracking BB swing table.
112          * The base index =
113          * 12. +((12-n)/2)dB 13~?? = decrease tx pwr by -((n-12)/2)dB
114          */
115         dm_odm->BbSwingIdxOfdm = 12; /*  Set defalut value as index 12. */
116         dm_odm->BbSwingIdxOfdmCurrent = 12;
117         dm_odm->BbSwingFlagOfdm = false;
118
119         for (i = 0; i < NUM_STA; i++)
120                 ODM_CmnInfoPtrArrayHook(dm_odm, ODM_CMNINFO_STA_STATUS, i,
121                                         NULL);
122 }
123
124 void rtl8188e_InitHalDm(struct adapter *Adapter)
125 {
126         struct dm_priv *pdmpriv = &Adapter->HalData->dmpriv;
127         struct odm_dm_struct *dm_odm = &Adapter->HalData->odmpriv;
128
129         dm_InitGPIOSetting(Adapter);
130         pdmpriv->DM_Type = DM_Type_ByDriver;
131         pdmpriv->DMFlag = DYNAMIC_FUNC_DISABLE;
132         Update_ODM_ComInfo_88E(Adapter);
133         ODM_DMInit(dm_odm);
134 }
135
136 void rtw_hal_dm_watchdog(struct adapter *Adapter)
137 {
138         u8 hw_init_completed = false;
139         struct mlme_priv *pmlmepriv = NULL;
140         u8 bLinked = false;
141
142         hw_init_completed = Adapter->hw_init_completed;
143
144         if (!hw_init_completed)
145                 goto skip_dm;
146
147         /* ODM */
148         pmlmepriv = &Adapter->mlmepriv;
149
150         if ((check_fwstate(pmlmepriv, WIFI_AP_STATE)) ||
151             (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE |
152                            WIFI_ADHOC_MASTER_STATE))) {
153                 if (Adapter->stapriv.asoc_sta_count > 2)
154                         bLinked = true;
155         } else {/* Station mode */
156                 if (check_fwstate(pmlmepriv, _FW_LINKED))
157                         bLinked = true;
158         }
159
160         Adapter->HalData->odmpriv.bLinked = bLinked;
161         ODM_DMWatchdog(&Adapter->HalData->odmpriv);
162 skip_dm:
163         /*  Check GPIO to determine current RF on/off and Pbc status. */
164         /*  Check Hardware Radio ON/OFF or not */
165         return;
166 }
167
168 void rtw_hal_dm_init(struct adapter *Adapter)
169 {
170         struct dm_priv *pdmpriv = &Adapter->HalData->dmpriv;
171         struct odm_dm_struct *podmpriv = &Adapter->HalData->odmpriv;
172
173         memset(pdmpriv, 0, sizeof(struct dm_priv));
174         Init_ODM_ComInfo_88E(Adapter);
175         ODM_InitDebugSetting(podmpriv);
176 }
177
178 /*  Add new function to reset the state of antenna diversity before link. */
179 /*  Compare RSSI for deciding antenna */
180 void rtw_hal_antdiv_rssi_compared(struct adapter *Adapter,
181                                   struct wlan_bssid_ex *dst,
182                                   struct wlan_bssid_ex *src)
183 {
184         if (Adapter->HalData->AntDivCfg != 0) {
185                 /* select optimum_antenna for before linked => For antenna
186                  * diversity
187                  */
188                 if (dst->Rssi >= src->Rssi) {/* keep org parameter */
189                         src->Rssi = dst->Rssi;
190                         src->PhyInfo.Optimum_antenna =
191                                 dst->PhyInfo.Optimum_antenna;
192                 }
193         }
194 }
195
196 /*  Add new function to reset the state of antenna diversity before link. */
197 bool rtw_hal_antdiv_before_linked(struct adapter *Adapter)
198 {
199         struct odm_dm_struct *dm_odm = &Adapter->HalData->odmpriv;
200         struct sw_ant_switch *dm_swat_tbl = &dm_odm->DM_SWAT_Table;
201         struct mlme_priv *pmlmepriv = &Adapter->mlmepriv;
202
203         /*  Condition that does not need to use antenna diversity. */
204         if (Adapter->HalData->AntDivCfg == 0)
205                 return false;
206
207         if (check_fwstate(pmlmepriv, _FW_LINKED))
208                 return false;
209
210         if (dm_swat_tbl->SWAS_NoLink_State != 0) {
211                 dm_swat_tbl->SWAS_NoLink_State = 0;
212                 return false;
213         }
214
215         /* switch channel */
216         dm_swat_tbl->SWAS_NoLink_State = 1;
217         dm_swat_tbl->CurAntenna = (dm_swat_tbl->CurAntenna == Antenna_A) ?
218                                   Antenna_B : Antenna_A;
219
220         rtw_antenna_select_cmd(Adapter, dm_swat_tbl->CurAntenna, false);
221         return true;
222 }