1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2011 Realtek Corporation. */
3 
4 /*  This file is for 92CE/92CU dynamic mechanism only */
5 #define _RTL8188E_DM_C_
6 
7 #include "../include/osdep_service.h"
8 #include "../include/drv_types.h"
9 #include "../include/rtl8188e_hal.h"
10 
11 /*  Initialize GPIO setting registers */
dm_InitGPIOSetting(struct adapter * Adapter)12 static void dm_InitGPIOSetting(struct adapter *Adapter)
13 {
14 	u8	tmp1byte;
15 	int res;
16 
17 	res = rtw_read8(Adapter, REG_GPIO_MUXCFG, &tmp1byte);
18 	if (res)
19 		return;
20 
21 	tmp1byte &= (GPIOSEL_GPIO | ~GPIOSEL_ENBT);
22 
23 	rtw_write8(Adapter, REG_GPIO_MUXCFG, tmp1byte);
24 }
25 
26 /*  */
27 /*  functions */
28 /*  */
Update_ODM_ComInfo_88E(struct adapter * Adapter)29 static void Update_ODM_ComInfo_88E(struct adapter *Adapter)
30 {
31 	struct mlme_ext_priv	*pmlmeext = &Adapter->mlmeextpriv;
32 	struct mlme_priv	*pmlmepriv = &Adapter->mlmepriv;
33 	struct pwrctrl_priv *pwrctrlpriv = &Adapter->pwrctrlpriv;
34 	struct hal_data_8188e *hal_data = &Adapter->haldata;
35 	struct odm_dm_struct *dm_odm = &hal_data->odmpriv;
36 	struct dm_priv	*pdmpriv = &hal_data->dmpriv;
37 	int i;
38 
39 	pdmpriv->InitODMFlag = ODM_BB_RSSI_MONITOR;
40 	if (hal_data->AntDivCfg)
41 		pdmpriv->InitODMFlag |= ODM_BB_ANT_DIV;
42 
43 	dm_odm->SupportAbility = pdmpriv->InitODMFlag;
44 
45 	dm_odm->pWirelessMode = &pmlmeext->cur_wireless_mode;
46 	dm_odm->pSecChOffset = &hal_data->nCur40MhzPrimeSC;
47 	dm_odm->pBandWidth = &hal_data->CurrentChannelBW;
48 	dm_odm->pChannel = &hal_data->CurrentChannel;
49 	dm_odm->pbScanInProcess = &pmlmepriv->bScanInProcess;
50 	dm_odm->pbPowerSaving = &pwrctrlpriv->bpower_saving;
51 
52 	ODM_CmnInfoInit(dm_odm, ODM_CMNINFO_RF_ANTENNA_TYPE, hal_data->TRxAntDivType);
53 
54 	for (i = 0; i < NUM_STA; i++)
55 		dm_odm->pODM_StaInfo[i] = NULL;
56 }
57 
rtl8188e_InitHalDm(struct adapter * Adapter)58 void rtl8188e_InitHalDm(struct adapter *Adapter)
59 {
60 	struct hal_data_8188e *hal_data = &Adapter->haldata;
61 	struct odm_dm_struct *dm_odm = &hal_data->odmpriv;
62 
63 	dm_InitGPIOSetting(Adapter);
64 	Update_ODM_ComInfo_88E(Adapter);
65 	ODM_DMInit(dm_odm);
66 }
67 
rtl8188e_HalDmWatchDog(struct adapter * Adapter)68 void rtl8188e_HalDmWatchDog(struct adapter *Adapter)
69 {
70 	u8 hw_init_completed = Adapter->hw_init_completed;
71 	struct hal_data_8188e *hal_data = &Adapter->haldata;
72 	struct mlme_priv *pmlmepriv = &Adapter->mlmepriv;
73 	u8 bLinked = false;
74 
75 	if (!hw_init_completed)
76 		return;
77 
78 	if ((check_fwstate(pmlmepriv, WIFI_AP_STATE)) ||
79 	    (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE | WIFI_ADHOC_MASTER_STATE))) {
80 		if (Adapter->stapriv.asoc_sta_count > 2)
81 			bLinked = true;
82 	} else {/* Station mode */
83 		if (check_fwstate(pmlmepriv, _FW_LINKED))
84 			bLinked = true;
85 	}
86 
87 	hal_data->odmpriv.bLinked = bLinked;
88 	ODM_DMWatchdog(&hal_data->odmpriv);
89 }
90 
rtl8188e_init_dm_priv(struct adapter * Adapter)91 void rtl8188e_init_dm_priv(struct adapter *Adapter)
92 {
93 	struct hal_data_8188e *hal_data = &Adapter->haldata;
94 	struct dm_priv	*pdmpriv = &hal_data->dmpriv;
95 	struct odm_dm_struct *dm_odm = &hal_data->odmpriv;
96 
97 	memset(pdmpriv, 0, sizeof(struct dm_priv));
98 	memset(dm_odm, 0, sizeof(*dm_odm));
99 
100 	dm_odm->Adapter = Adapter;
101 	ODM_CmnInfoInit(dm_odm, ODM_CMNINFO_MP_TEST_CHIP, IS_NORMAL_CHIP(hal_data->VersionID));
102 	ODM_CmnInfoInit(dm_odm, ODM_CMNINFO_RF_ANTENNA_TYPE, hal_data->TRxAntDivType);
103 }
104 
105 /*  Add new function to reset the state of antenna diversity before link. */
106 /*  Compare RSSI for deciding antenna */
AntDivCompare8188E(struct adapter * Adapter,struct wlan_bssid_ex * dst,struct wlan_bssid_ex * src)107 void AntDivCompare8188E(struct adapter *Adapter, struct wlan_bssid_ex *dst, struct wlan_bssid_ex *src)
108 {
109 	struct hal_data_8188e *hal_data = &Adapter->haldata;
110 
111 	if (0 != hal_data->AntDivCfg) {
112 		/* select optimum_antenna for before linked =>For antenna diversity */
113 		if (dst->Rssi >=  src->Rssi) {/* keep org parameter */
114 			src->Rssi = dst->Rssi;
115 			src->PhyInfo.Optimum_antenna = dst->PhyInfo.Optimum_antenna;
116 		}
117 	}
118 }
119 
120 /*  Add new function to reset the state of antenna diversity before link. */
AntDivBeforeLink8188E(struct adapter * Adapter)121 u8 AntDivBeforeLink8188E(struct adapter *Adapter)
122 {
123 	struct hal_data_8188e *hal_data = &Adapter->haldata;
124 	struct odm_dm_struct *dm_odm = &hal_data->odmpriv;
125 	struct sw_ant_switch *dm_swat_tbl = &dm_odm->DM_SWAT_Table;
126 	struct mlme_priv *pmlmepriv = &Adapter->mlmepriv;
127 
128 	/*  Condition that does not need to use antenna diversity. */
129 	if (hal_data->AntDivCfg == 0)
130 		return false;
131 
132 	if (check_fwstate(pmlmepriv, _FW_LINKED))
133 		return false;
134 
135 	if (dm_swat_tbl->SWAS_NoLink_State == 0) {
136 		/* switch channel */
137 		dm_swat_tbl->SWAS_NoLink_State = 1;
138 		dm_swat_tbl->CurAntenna = (dm_swat_tbl->CurAntenna == Antenna_A) ? Antenna_B : Antenna_A;
139 
140 		rtw_antenna_select_cmd(Adapter, dm_swat_tbl->CurAntenna, false);
141 		return true;
142 	} else {
143 		dm_swat_tbl->SWAS_NoLink_State = 0;
144 		return false;
145 	}
146 }
147