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 */
dm_InitGPIOSetting(struct adapter * Adapter)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 /*  */
Init_ODM_ComInfo_88E(struct adapter * Adapter)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 = 12. +((12-n)/2)dB 13~?? = decrease tx pwr by -((n-12)/2)dB */
56 	dm_odm->BbSwingIdxOfdm = 12; /*  Set defalut value as index 12. */
57 	dm_odm->BbSwingIdxOfdmCurrent = 12;
58 	dm_odm->BbSwingFlagOfdm = false;
59 
60 	pdmpriv->InitODMFlag =	ODM_RF_CALIBRATION |
61 				ODM_RF_TX_PWR_TRACK;
62 
63 	dm_odm->SupportAbility = pdmpriv->InitODMFlag;
64 }
65 
Update_ODM_ComInfo_88E(struct adapter * Adapter)66 static void Update_ODM_ComInfo_88E(struct adapter *Adapter)
67 {
68 	struct mlme_ext_priv	*pmlmeext = &Adapter->mlmeextpriv;
69 	struct mlme_priv	*pmlmepriv = &Adapter->mlmepriv;
70 	struct pwrctrl_priv *pwrctrlpriv = &Adapter->pwrctrlpriv;
71 	struct hal_data_8188e *hal_data = Adapter->HalData;
72 	struct odm_dm_struct *dm_odm = &(hal_data->odmpriv);
73 	struct dm_priv	*pdmpriv = &hal_data->dmpriv;
74 	int i;
75 
76 	pdmpriv->InitODMFlag =	ODM_BB_DIG |
77 				ODM_BB_RA_MASK |
78 				ODM_BB_DYNAMIC_TXPWR |
79 				ODM_BB_FA_CNT |
80 				ODM_BB_RSSI_MONITOR |
81 				ODM_BB_CCK_PD |
82 				ODM_BB_PWR_SAVE |
83 				ODM_MAC_EDCA_TURBO |
84 				ODM_RF_CALIBRATION |
85 				ODM_RF_TX_PWR_TRACK;
86 	if (hal_data->AntDivCfg)
87 		pdmpriv->InitODMFlag |= ODM_BB_ANT_DIV;
88 
89 	if (Adapter->registrypriv.mp_mode == 1) {
90 		pdmpriv->InitODMFlag =	ODM_RF_CALIBRATION |
91 					ODM_RF_TX_PWR_TRACK;
92 	}
93 
94 	dm_odm->SupportAbility = pdmpriv->InitODMFlag;
95 
96 	dm_odm->pNumTxBytesUnicast = &Adapter->xmitpriv.tx_bytes;
97 	dm_odm->pNumRxBytesUnicast = &Adapter->recvpriv.rx_bytes;
98 	dm_odm->pWirelessMode = &pmlmeext->cur_wireless_mode;
99 	dm_odm->pSecChOffset = &hal_data->nCur40MhzPrimeSC;
100 	dm_odm->pSecurity = (u8 *)&Adapter->securitypriv.dot11PrivacyAlgrthm;
101 	dm_odm->pBandWidth = (u8 *)&hal_data->CurrentChannelBW;
102 	dm_odm->pChannel = &hal_data->CurrentChannel;
103 	dm_odm->pbNet_closed = (bool *)&Adapter->net_closed;
104 	dm_odm->mp_mode = &Adapter->registrypriv.mp_mode;
105 	dm_odm->pbScanInProcess = (bool *)&pmlmepriv->bScanInProcess;
106 	dm_odm->pbPowerSaving = (bool *)&pwrctrlpriv->bpower_saving;
107 	dm_odm->AntDivType = hal_data->TRxAntDivType;
108 
109 	/*  Tx power tracking BB swing table. */
110 	/*  The base index = 12. +((12-n)/2)dB 13~?? = decrease tx pwr by -((n-12)/2)dB */
111 	dm_odm->BbSwingIdxOfdm = 12; /*  Set defalut value as index 12. */
112 	dm_odm->BbSwingIdxOfdmCurrent = 12;
113 	dm_odm->BbSwingFlagOfdm = false;
114 
115 	for (i = 0; i < NUM_STA; i++)
116 		ODM_CmnInfoPtrArrayHook(dm_odm, ODM_CMNINFO_STA_STATUS, i, NULL);
117 }
118 
rtl8188e_InitHalDm(struct adapter * Adapter)119 void rtl8188e_InitHalDm(struct adapter *Adapter)
120 {
121 	struct dm_priv	*pdmpriv = &Adapter->HalData->dmpriv;
122 	struct odm_dm_struct *dm_odm = &(Adapter->HalData->odmpriv);
123 
124 	dm_InitGPIOSetting(Adapter);
125 	pdmpriv->DM_Type = DM_Type_ByDriver;
126 	pdmpriv->DMFlag = DYNAMIC_FUNC_DISABLE;
127 	Update_ODM_ComInfo_88E(Adapter);
128 	ODM_DMInit(dm_odm);
129 }
130 
rtw_hal_dm_watchdog(struct adapter * Adapter)131 void rtw_hal_dm_watchdog(struct adapter *Adapter)
132 {
133 	u8 hw_init_completed = false;
134 	struct mlme_priv *pmlmepriv = NULL;
135 	u8 bLinked = false;
136 
137 	hw_init_completed = Adapter->hw_init_completed;
138 
139 	if (!hw_init_completed)
140 		goto skip_dm;
141 
142 	/* ODM */
143 	pmlmepriv = &Adapter->mlmepriv;
144 
145 	if ((check_fwstate(pmlmepriv, WIFI_AP_STATE)) ||
146 	    (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE |
147 			   WIFI_ADHOC_MASTER_STATE))) {
148 		if (Adapter->stapriv.asoc_sta_count > 2)
149 			bLinked = true;
150 	} else {/* Station mode */
151 		if (check_fwstate(pmlmepriv, _FW_LINKED))
152 			bLinked = true;
153 	}
154 
155 	Adapter->HalData->odmpriv.bLinked = bLinked;
156 	ODM_DMWatchdog(&Adapter->HalData->odmpriv);
157 skip_dm:
158 	/*  Check GPIO to determine current RF on/off and Pbc status. */
159 	/*  Check Hardware Radio ON/OFF or not */
160 	return;
161 }
162 
rtw_hal_dm_init(struct adapter * Adapter)163 void rtw_hal_dm_init(struct adapter *Adapter)
164 {
165 	struct dm_priv	*pdmpriv = &Adapter->HalData->dmpriv;
166 	struct odm_dm_struct *podmpriv = &Adapter->HalData->odmpriv;
167 
168 	memset(pdmpriv, 0, sizeof(struct dm_priv));
169 	Init_ODM_ComInfo_88E(Adapter);
170 	ODM_InitDebugSetting(podmpriv);
171 }
172 
173 /*  Add new function to reset the state of antenna diversity before link. */
174 /*  Compare RSSI for deciding antenna */
rtw_hal_antdiv_rssi_compared(struct adapter * Adapter,struct wlan_bssid_ex * dst,struct wlan_bssid_ex * src)175 void rtw_hal_antdiv_rssi_compared(struct adapter *Adapter, struct wlan_bssid_ex *dst, struct wlan_bssid_ex *src)
176 {
177 	if (Adapter->HalData->AntDivCfg != 0) {
178 		/* select optimum_antenna for before linked =>For antenna diversity */
179 		if (dst->Rssi >=  src->Rssi) {/* keep org parameter */
180 			src->Rssi = dst->Rssi;
181 			src->PhyInfo.Optimum_antenna = dst->PhyInfo.Optimum_antenna;
182 		}
183 	}
184 }
185 
186 /*  Add new function to reset the state of antenna diversity before link. */
rtw_hal_antdiv_before_linked(struct adapter * Adapter)187 u8 rtw_hal_antdiv_before_linked(struct adapter *Adapter)
188 {
189 	struct odm_dm_struct *dm_odm = &Adapter->HalData->odmpriv;
190 	struct sw_ant_switch *dm_swat_tbl = &dm_odm->DM_SWAT_Table;
191 	struct mlme_priv *pmlmepriv = &(Adapter->mlmepriv);
192 
193 	/*  Condition that does not need to use antenna diversity. */
194 	if (Adapter->HalData->AntDivCfg == 0)
195 		return false;
196 
197 	if (check_fwstate(pmlmepriv, _FW_LINKED))
198 		return false;
199 
200 	if (dm_swat_tbl->SWAS_NoLink_State == 0) {
201 		/* switch channel */
202 		dm_swat_tbl->SWAS_NoLink_State = 1;
203 		dm_swat_tbl->CurAntenna = (dm_swat_tbl->CurAntenna == Antenna_A) ? Antenna_B : Antenna_A;
204 
205 		rtw_antenna_select_cmd(Adapter, dm_swat_tbl->CurAntenna, false);
206 		return true;
207 	} else {
208 		dm_swat_tbl->SWAS_NoLink_State = 0;
209 		return false;
210 	}
211 }
212