1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
4  *
5  * Contact Information: wlanfae <wlanfae@realtek.com>
6  */
7 #include <linux/bitops.h>
8 #include "rtl_core.h"
9 #include "r8192E_hw.h"
10 #include "r8192E_phyreg.h"
11 #include "r8190P_rtl8256.h"
12 #include "r8192E_phy.h"
13 #include "rtl_dm.h"
14 
15 #include "table.h"
16 
17 /*************************Define local function prototype**********************/
18 
19 static u32 _rtl92e_phy_rf_fw_read(struct net_device *dev,
20 				  enum rf90_radio_path eRFPath, u32 Offset);
21 static void _rtl92e_phy_rf_fw_write(struct net_device *dev,
22 				    enum rf90_radio_path eRFPath, u32 Offset,
23 				    u32 Data);
24 
_rtl92e_calculate_bit_shift(u32 dwBitMask)25 static u32 _rtl92e_calculate_bit_shift(u32 dwBitMask)
26 {
27 	if (!dwBitMask)
28 		return 32;
29 	return ffs(dwBitMask) - 1;
30 }
31 
rtl92e_set_bb_reg(struct net_device * dev,u32 dwRegAddr,u32 dwBitMask,u32 dwData)32 void rtl92e_set_bb_reg(struct net_device *dev, u32 dwRegAddr, u32 dwBitMask,
33 		       u32 dwData)
34 {
35 	u32 OriginalValue, BitShift, NewValue;
36 
37 	if (dwBitMask != bMaskDWord) {
38 		OriginalValue = rtl92e_readl(dev, dwRegAddr);
39 		BitShift = _rtl92e_calculate_bit_shift(dwBitMask);
40 		NewValue = (OriginalValue & ~dwBitMask) | (dwData << BitShift);
41 		rtl92e_writel(dev, dwRegAddr, NewValue);
42 	} else {
43 		rtl92e_writel(dev, dwRegAddr, dwData);
44 	}
45 }
46 
rtl92e_get_bb_reg(struct net_device * dev,u32 dwRegAddr,u32 dwBitMask)47 u32 rtl92e_get_bb_reg(struct net_device *dev, u32 dwRegAddr, u32 dwBitMask)
48 {
49 	u32 OriginalValue, BitShift;
50 
51 	OriginalValue = rtl92e_readl(dev, dwRegAddr);
52 	BitShift = _rtl92e_calculate_bit_shift(dwBitMask);
53 
54 	return (OriginalValue & dwBitMask) >> BitShift;
55 }
56 
_rtl92e_phy_rf_read(struct net_device * dev,enum rf90_radio_path eRFPath,u32 Offset)57 static u32 _rtl92e_phy_rf_read(struct net_device *dev,
58 			       enum rf90_radio_path eRFPath, u32 Offset)
59 {
60 	struct r8192_priv *priv = rtllib_priv(dev);
61 	u32 ret = 0;
62 	u32 NewOffset = 0;
63 	struct bb_reg_definition *pPhyReg = &priv->phy_reg_def[eRFPath];
64 
65 	Offset &= 0x3f;
66 
67 	rtl92e_set_bb_reg(dev, rFPGA0_AnalogParameter4, 0xf00, 0x0);
68 	if (Offset >= 31) {
69 		priv->rf_reg_0value[eRFPath] |= 0x140;
70 		rtl92e_set_bb_reg(dev, pPhyReg->rf3wireOffset,
71 				  bMaskDWord,
72 				  (priv->rf_reg_0value[eRFPath] << 16));
73 		NewOffset = Offset - 30;
74 	} else if (Offset >= 16) {
75 		priv->rf_reg_0value[eRFPath] |= 0x100;
76 		priv->rf_reg_0value[eRFPath] &= (~0x40);
77 		rtl92e_set_bb_reg(dev, pPhyReg->rf3wireOffset,
78 				  bMaskDWord,
79 				  (priv->rf_reg_0value[eRFPath] << 16));
80 		NewOffset = Offset - 15;
81 	} else {
82 		NewOffset = Offset;
83 	}
84 	rtl92e_set_bb_reg(dev, pPhyReg->rfHSSIPara2, bLSSIReadAddress,
85 			  NewOffset);
86 	rtl92e_set_bb_reg(dev, pPhyReg->rfHSSIPara2,  bLSSIReadEdge, 0x0);
87 	rtl92e_set_bb_reg(dev, pPhyReg->rfHSSIPara2,  bLSSIReadEdge, 0x1);
88 
89 	mdelay(1);
90 
91 	ret = rtl92e_get_bb_reg(dev, pPhyReg->rfLSSIReadBack,
92 				bLSSIReadBackData);
93 
94 	priv->rf_reg_0value[eRFPath] &= 0xebf;
95 
96 	rtl92e_set_bb_reg(dev, pPhyReg->rf3wireOffset, bMaskDWord,
97 			  (priv->rf_reg_0value[eRFPath] << 16));
98 
99 	rtl92e_set_bb_reg(dev, rFPGA0_AnalogParameter4, 0x300, 0x3);
100 
101 	return ret;
102 }
103 
_rtl92e_phy_rf_write(struct net_device * dev,enum rf90_radio_path eRFPath,u32 Offset,u32 Data)104 static void _rtl92e_phy_rf_write(struct net_device *dev,
105 				 enum rf90_radio_path eRFPath, u32 Offset,
106 				 u32 Data)
107 {
108 	struct r8192_priv *priv = rtllib_priv(dev);
109 	u32 DataAndAddr = 0, NewOffset = 0;
110 	struct bb_reg_definition *pPhyReg = &priv->phy_reg_def[eRFPath];
111 
112 	Offset &= 0x3f;
113 
114 	rtl92e_set_bb_reg(dev, rFPGA0_AnalogParameter4, 0xf00, 0x0);
115 
116 	if (Offset >= 31) {
117 		priv->rf_reg_0value[eRFPath] |= 0x140;
118 		rtl92e_set_bb_reg(dev, pPhyReg->rf3wireOffset,
119 				  bMaskDWord,
120 				  (priv->rf_reg_0value[eRFPath] << 16));
121 		NewOffset = Offset - 30;
122 	} else if (Offset >= 16) {
123 		priv->rf_reg_0value[eRFPath] |= 0x100;
124 		priv->rf_reg_0value[eRFPath] &= (~0x40);
125 		rtl92e_set_bb_reg(dev, pPhyReg->rf3wireOffset,
126 				  bMaskDWord,
127 				  (priv->rf_reg_0value[eRFPath] << 16));
128 		NewOffset = Offset - 15;
129 	} else {
130 		NewOffset = Offset;
131 	}
132 
133 	DataAndAddr = (NewOffset & 0x3f) | (Data << 16);
134 
135 	rtl92e_set_bb_reg(dev, pPhyReg->rf3wireOffset, bMaskDWord, DataAndAddr);
136 
137 	if (Offset == 0x0)
138 		priv->rf_reg_0value[eRFPath] = Data;
139 
140 	if (Offset != 0) {
141 		priv->rf_reg_0value[eRFPath] &= 0xebf;
142 		rtl92e_set_bb_reg(dev, pPhyReg->rf3wireOffset,
143 				  bMaskDWord,
144 				  (priv->rf_reg_0value[eRFPath] << 16));
145 	}
146 	rtl92e_set_bb_reg(dev, rFPGA0_AnalogParameter4, 0x300, 0x3);
147 }
148 
rtl92e_set_rf_reg(struct net_device * dev,enum rf90_radio_path eRFPath,u32 RegAddr,u32 BitMask,u32 Data)149 void rtl92e_set_rf_reg(struct net_device *dev, enum rf90_radio_path eRFPath,
150 		       u32 RegAddr, u32 BitMask, u32 Data)
151 {
152 	struct r8192_priv *priv = rtllib_priv(dev);
153 	u32 Original_Value, BitShift, New_Value;
154 
155 	if (priv->rtllib->rf_power_state != rf_on && !priv->being_init_adapter)
156 		return;
157 
158 	if (priv->rf_mode == RF_OP_By_FW) {
159 		if (BitMask != bMask12Bits) {
160 			Original_Value = _rtl92e_phy_rf_fw_read(dev, eRFPath,
161 								RegAddr);
162 			BitShift =  _rtl92e_calculate_bit_shift(BitMask);
163 			New_Value = (Original_Value & ~BitMask) | (Data << BitShift);
164 
165 			_rtl92e_phy_rf_fw_write(dev, eRFPath, RegAddr,
166 						New_Value);
167 		} else {
168 			_rtl92e_phy_rf_fw_write(dev, eRFPath, RegAddr, Data);
169 		}
170 		udelay(200);
171 	} else {
172 		if (BitMask != bMask12Bits) {
173 			Original_Value = _rtl92e_phy_rf_read(dev, eRFPath,
174 							     RegAddr);
175 			BitShift =  _rtl92e_calculate_bit_shift(BitMask);
176 			New_Value = (Original_Value & ~BitMask) | (Data << BitShift);
177 
178 			_rtl92e_phy_rf_write(dev, eRFPath, RegAddr, New_Value);
179 		} else {
180 			_rtl92e_phy_rf_write(dev, eRFPath, RegAddr, Data);
181 		}
182 	}
183 }
184 
rtl92e_get_rf_reg(struct net_device * dev,enum rf90_radio_path eRFPath,u32 RegAddr,u32 BitMask)185 u32 rtl92e_get_rf_reg(struct net_device *dev, enum rf90_radio_path eRFPath,
186 		      u32 RegAddr, u32 BitMask)
187 {
188 	u32 Original_Value, Readback_Value, BitShift;
189 	struct r8192_priv *priv = rtllib_priv(dev);
190 
191 	if (priv->rtllib->rf_power_state != rf_on && !priv->being_init_adapter)
192 		return	0;
193 	mutex_lock(&priv->rf_mutex);
194 	if (priv->rf_mode == RF_OP_By_FW) {
195 		Original_Value = _rtl92e_phy_rf_fw_read(dev, eRFPath, RegAddr);
196 		udelay(200);
197 	} else {
198 		Original_Value = _rtl92e_phy_rf_read(dev, eRFPath, RegAddr);
199 	}
200 	BitShift =  _rtl92e_calculate_bit_shift(BitMask);
201 	Readback_Value = (Original_Value & BitMask) >> BitShift;
202 	mutex_unlock(&priv->rf_mutex);
203 	return Readback_Value;
204 }
205 
_rtl92e_phy_rf_fw_read(struct net_device * dev,enum rf90_radio_path eRFPath,u32 Offset)206 static u32 _rtl92e_phy_rf_fw_read(struct net_device *dev,
207 				  enum rf90_radio_path eRFPath, u32 Offset)
208 {
209 	u32		Data = 0;
210 	u8		time = 0;
211 
212 	Data |= ((Offset & 0xFF) << 12);
213 	Data |= ((eRFPath & 0x3) << 20);
214 	Data |= 0x80000000;
215 	while (rtl92e_readl(dev, QPNR) & 0x80000000) {
216 		if (time++ < 100)
217 			udelay(10);
218 		else
219 			break;
220 	}
221 	rtl92e_writel(dev, QPNR, Data);
222 	while (rtl92e_readl(dev, QPNR) & 0x80000000) {
223 		if (time++ < 100)
224 			udelay(10);
225 		else
226 			return 0;
227 	}
228 	return rtl92e_readl(dev, RF_DATA);
229 }
230 
_rtl92e_phy_rf_fw_write(struct net_device * dev,enum rf90_radio_path eRFPath,u32 Offset,u32 Data)231 static void _rtl92e_phy_rf_fw_write(struct net_device *dev,
232 				    enum rf90_radio_path eRFPath, u32 Offset,
233 				    u32 Data)
234 {
235 	u8	time = 0;
236 
237 	Data |= ((Offset & 0xFF) << 12);
238 	Data |= ((eRFPath & 0x3) << 20);
239 	Data |= 0x400000;
240 	Data |= 0x80000000;
241 
242 	while (rtl92e_readl(dev, QPNR) & 0x80000000) {
243 		if (time++ < 100)
244 			udelay(10);
245 		else
246 			break;
247 	}
248 	rtl92e_writel(dev, QPNR, Data);
249 }
250 
rtl92e_config_mac(struct net_device * dev)251 void rtl92e_config_mac(struct net_device *dev)
252 {
253 	u32 dwArrayLen = 0, i = 0;
254 	u32 *pdwArray = NULL;
255 	struct r8192_priv *priv = rtllib_priv(dev);
256 
257 	if (priv->tx_pwr_data_read_from_eeprom) {
258 		dwArrayLen = RTL8192E_MACPHY_ARR_PG_LEN;
259 		pdwArray = RTL8192E_MACPHY_ARR_PG;
260 
261 	} else {
262 		dwArrayLen = RTL8192E_MACPHY_ARR_LEN;
263 		pdwArray = RTL8192E_MACPHY_ARR;
264 	}
265 	for (i = 0; i < dwArrayLen; i += 3) {
266 		if (pdwArray[i] == 0x318)
267 			pdwArray[i + 2] = 0x00000800;
268 		rtl92e_set_bb_reg(dev, pdwArray[i], pdwArray[i + 1],
269 				  pdwArray[i + 2]);
270 	}
271 }
272 
_rtl92e_phy_config_bb(struct net_device * dev,u8 ConfigType)273 static void _rtl92e_phy_config_bb(struct net_device *dev, u8 ConfigType)
274 {
275 	int i;
276 	u32 *Rtl819XPHY_REGArray_Table = NULL;
277 	u32 *Rtl819XAGCTAB_Array_Table = NULL;
278 	u16 AGCTAB_ArrayLen, PHY_REGArrayLen = 0;
279 
280 	AGCTAB_ArrayLen = RTL8192E_AGCTAB_ARR_LEN;
281 	Rtl819XAGCTAB_Array_Table = RTL8192E_AGCTAB_ARR;
282 	PHY_REGArrayLen = RTL8192E_PHY_REG_1T2R_ARR_LEN;
283 	Rtl819XPHY_REGArray_Table = RTL8192E_PHY_REG_1T2R_ARR;
284 
285 	if (ConfigType == BB_CONFIG_PHY_REG) {
286 		for (i = 0; i < PHY_REGArrayLen; i += 2) {
287 			rtl92e_set_bb_reg(dev, Rtl819XPHY_REGArray_Table[i],
288 					  bMaskDWord,
289 					  Rtl819XPHY_REGArray_Table[i + 1]);
290 		}
291 	} else if (ConfigType == BB_CONFIG_AGC_TAB) {
292 		for (i = 0; i < AGCTAB_ArrayLen; i += 2) {
293 			rtl92e_set_bb_reg(dev, Rtl819XAGCTAB_Array_Table[i],
294 					  bMaskDWord,
295 					  Rtl819XAGCTAB_Array_Table[i + 1]);
296 		}
297 	}
298 }
299 
_rtl92e_init_bb_rf_reg_def(struct net_device * dev)300 static void _rtl92e_init_bb_rf_reg_def(struct net_device *dev)
301 {
302 	struct r8192_priv *priv = rtllib_priv(dev);
303 
304 	priv->phy_reg_def[RF90_PATH_A].rfintfs = rFPGA0_XAB_RFInterfaceSW;
305 	priv->phy_reg_def[RF90_PATH_B].rfintfs = rFPGA0_XAB_RFInterfaceSW;
306 
307 	priv->phy_reg_def[RF90_PATH_A].rfintfo = rFPGA0_XA_RFInterfaceOE;
308 	priv->phy_reg_def[RF90_PATH_B].rfintfo = rFPGA0_XB_RFInterfaceOE;
309 
310 	priv->phy_reg_def[RF90_PATH_A].rfintfe = rFPGA0_XA_RFInterfaceOE;
311 	priv->phy_reg_def[RF90_PATH_B].rfintfe = rFPGA0_XB_RFInterfaceOE;
312 
313 	priv->phy_reg_def[RF90_PATH_A].rf3wireOffset = rFPGA0_XA_LSSIParameter;
314 	priv->phy_reg_def[RF90_PATH_B].rf3wireOffset = rFPGA0_XB_LSSIParameter;
315 
316 	priv->phy_reg_def[RF90_PATH_A].rfHSSIPara2 = rFPGA0_XA_HSSIParameter2;
317 	priv->phy_reg_def[RF90_PATH_B].rfHSSIPara2 = rFPGA0_XB_HSSIParameter2;
318 
319 	priv->phy_reg_def[RF90_PATH_A].rfLSSIReadBack = rFPGA0_XA_LSSIReadBack;
320 	priv->phy_reg_def[RF90_PATH_B].rfLSSIReadBack = rFPGA0_XB_LSSIReadBack;
321 }
322 
rtl92e_check_bb_and_rf(struct net_device * dev,enum hw90_block CheckBlock,enum rf90_radio_path eRFPath)323 bool rtl92e_check_bb_and_rf(struct net_device *dev, enum hw90_block CheckBlock,
324 			    enum rf90_radio_path eRFPath)
325 {
326 	bool ret = true;
327 	u32 i, CheckTimes = 4, dwRegRead = 0;
328 	u32 WriteAddr[4];
329 	u32 WriteData[] = {0xfffff027, 0xaa55a02f, 0x00000027, 0x55aa502f};
330 
331 	WriteAddr[HW90_BLOCK_MAC] = 0x100;
332 	WriteAddr[HW90_BLOCK_PHY0] = 0x900;
333 	WriteAddr[HW90_BLOCK_PHY1] = 0x800;
334 	WriteAddr[HW90_BLOCK_RF] = 0x3;
335 
336 	if (CheckBlock == HW90_BLOCK_MAC) {
337 		netdev_warn(dev, "%s(): No checks available for MAC block.\n",
338 			    __func__);
339 		return ret;
340 	}
341 
342 	for (i = 0; i < CheckTimes; i++) {
343 		switch (CheckBlock) {
344 		case HW90_BLOCK_PHY0:
345 		case HW90_BLOCK_PHY1:
346 			rtl92e_writel(dev, WriteAddr[CheckBlock],
347 				      WriteData[i]);
348 			dwRegRead = rtl92e_readl(dev, WriteAddr[CheckBlock]);
349 			break;
350 
351 		case HW90_BLOCK_RF:
352 			WriteData[i] &= 0xfff;
353 			rtl92e_set_rf_reg(dev, eRFPath,
354 					  WriteAddr[HW90_BLOCK_RF],
355 					  bMask12Bits, WriteData[i]);
356 			mdelay(10);
357 			dwRegRead = rtl92e_get_rf_reg(dev, eRFPath,
358 						      WriteAddr[HW90_BLOCK_RF],
359 						      bMaskDWord);
360 			mdelay(10);
361 			break;
362 
363 		default:
364 			ret = false;
365 			break;
366 		}
367 
368 		if (dwRegRead != WriteData[i]) {
369 			netdev_warn(dev, "%s(): Check failed.\n", __func__);
370 			ret = false;
371 			break;
372 		}
373 	}
374 
375 	return ret;
376 }
377 
_rtl92e_bb_config_para_file(struct net_device * dev)378 static bool _rtl92e_bb_config_para_file(struct net_device *dev)
379 {
380 	struct r8192_priv *priv = rtllib_priv(dev);
381 	bool rtStatus = true;
382 	u8 bRegValue = 0, eCheckItem = 0;
383 	u32 dwRegValue = 0;
384 
385 	bRegValue = rtl92e_readb(dev, BB_GLOBAL_RESET);
386 	rtl92e_writeb(dev, BB_GLOBAL_RESET, (bRegValue | BB_GLOBAL_RESET_BIT));
387 
388 	dwRegValue = rtl92e_readl(dev, CPU_GEN);
389 	rtl92e_writel(dev, CPU_GEN, (dwRegValue & (~CPU_GEN_BB_RST)));
390 
391 	for (eCheckItem = (enum hw90_block)HW90_BLOCK_PHY0;
392 	     eCheckItem <= HW90_BLOCK_PHY1; eCheckItem++) {
393 		rtStatus  = rtl92e_check_bb_and_rf(dev,
394 						   (enum hw90_block)eCheckItem,
395 						   (enum rf90_radio_path)0);
396 		if (!rtStatus)
397 			return rtStatus;
398 	}
399 	rtl92e_set_bb_reg(dev, rFPGA0_RFMOD, bCCKEn | bOFDMEn, 0x0);
400 	_rtl92e_phy_config_bb(dev, BB_CONFIG_PHY_REG);
401 
402 	dwRegValue = rtl92e_readl(dev, CPU_GEN);
403 	rtl92e_writel(dev, CPU_GEN, (dwRegValue | CPU_GEN_BB_RST));
404 
405 	_rtl92e_phy_config_bb(dev, BB_CONFIG_AGC_TAB);
406 
407 	if (priv->ic_cut  > VERSION_8190_BD) {
408 		dwRegValue = 0x0;
409 		rtl92e_set_bb_reg(dev, rFPGA0_TxGainStage,
410 				  (bXBTxAGC | bXCTxAGC | bXDTxAGC), dwRegValue);
411 
412 		dwRegValue = priv->crystal_cap;
413 		rtl92e_set_bb_reg(dev, rFPGA0_AnalogParameter1, bXtalCap92x,
414 				  dwRegValue);
415 	}
416 
417 	return rtStatus;
418 }
419 
rtl92e_config_bb(struct net_device * dev)420 bool rtl92e_config_bb(struct net_device *dev)
421 {
422 	_rtl92e_init_bb_rf_reg_def(dev);
423 	return _rtl92e_bb_config_para_file(dev);
424 }
425 
rtl92e_get_tx_power(struct net_device * dev)426 void rtl92e_get_tx_power(struct net_device *dev)
427 {
428 	struct r8192_priv *priv = rtllib_priv(dev);
429 
430 	priv->mcs_tx_pwr_level_org_offset[0] =
431 		rtl92e_readl(dev, rTxAGC_Rate18_06);
432 	priv->mcs_tx_pwr_level_org_offset[1] =
433 		rtl92e_readl(dev, rTxAGC_Rate54_24);
434 	priv->mcs_tx_pwr_level_org_offset[2] =
435 		rtl92e_readl(dev, rTxAGC_Mcs03_Mcs00);
436 	priv->mcs_tx_pwr_level_org_offset[3] =
437 		rtl92e_readl(dev, rTxAGC_Mcs07_Mcs04);
438 	priv->mcs_tx_pwr_level_org_offset[4] =
439 		rtl92e_readl(dev, rTxAGC_Mcs11_Mcs08);
440 	priv->mcs_tx_pwr_level_org_offset[5] =
441 		rtl92e_readl(dev, rTxAGC_Mcs15_Mcs12);
442 
443 	priv->def_initial_gain[0] = rtl92e_readb(dev, rOFDM0_XAAGCCore1);
444 	priv->def_initial_gain[1] = rtl92e_readb(dev, rOFDM0_XBAGCCore1);
445 	priv->def_initial_gain[2] = rtl92e_readb(dev, rOFDM0_XCAGCCore1);
446 	priv->def_initial_gain[3] = rtl92e_readb(dev, rOFDM0_XDAGCCore1);
447 
448 	priv->framesync = rtl92e_readb(dev, rOFDM0_RxDetector3);
449 }
450 
rtl92e_set_tx_power(struct net_device * dev,u8 channel)451 void rtl92e_set_tx_power(struct net_device *dev, u8 channel)
452 {
453 	struct r8192_priv *priv = rtllib_priv(dev);
454 	u8	powerlevel = 0, powerlevelOFDM24G = 0;
455 
456 	if (priv->epromtype == EEPROM_93C46) {
457 		powerlevel = priv->tx_pwr_level_cck[channel - 1];
458 		powerlevelOFDM24G = priv->tx_pwr_level_ofdm_24g[channel - 1];
459 	}
460 
461 	rtl92e_set_cck_tx_power(dev, powerlevel);
462 	rtl92e_set_ofdm_tx_power(dev, powerlevelOFDM24G);
463 }
464 
rtl92e_config_rf_path(struct net_device * dev,enum rf90_radio_path eRFPath)465 u8 rtl92e_config_rf_path(struct net_device *dev, enum rf90_radio_path eRFPath)
466 {
467 	int i;
468 
469 	switch (eRFPath) {
470 	case RF90_PATH_A:
471 		for (i = 0; i < RTL8192E_RADIO_A_ARR_LEN; i += 2) {
472 			if (RTL8192E_RADIO_A_ARR[i] == 0xfe) {
473 				msleep(100);
474 				continue;
475 			}
476 			rtl92e_set_rf_reg(dev, eRFPath, RTL8192E_RADIO_A_ARR[i],
477 					  bMask12Bits,
478 					  RTL8192E_RADIO_A_ARR[i + 1]);
479 		}
480 		break;
481 	case RF90_PATH_B:
482 		for (i = 0; i < RTL8192E_RADIO_B_ARR_LEN; i += 2) {
483 			if (RTL8192E_RADIO_B_ARR[i] == 0xfe) {
484 				msleep(100);
485 				continue;
486 			}
487 			rtl92e_set_rf_reg(dev, eRFPath, RTL8192E_RADIO_B_ARR[i],
488 					  bMask12Bits,
489 					  RTL8192E_RADIO_B_ARR[i + 1]);
490 		}
491 		break;
492 	default:
493 		break;
494 	}
495 
496 	return 0;
497 }
498 
_rtl92e_set_tx_power_level(struct net_device * dev,u8 channel)499 static void _rtl92e_set_tx_power_level(struct net_device *dev, u8 channel)
500 {
501 	struct r8192_priv *priv = rtllib_priv(dev);
502 	u8	powerlevel = priv->tx_pwr_level_cck[channel - 1];
503 	u8	powerlevelOFDM24G = priv->tx_pwr_level_ofdm_24g[channel - 1];
504 
505 	rtl92e_set_cck_tx_power(dev, powerlevel);
506 	rtl92e_set_ofdm_tx_power(dev, powerlevelOFDM24G);
507 }
508 
_rtl92e_phy_set_sw_chnl_cmd_array(struct net_device * dev,struct sw_chnl_cmd * CmdTable,u32 CmdTableIdx,u32 CmdTableSz,enum sw_chnl_cmd_id cmd_id,u32 para1,u32 para2,u32 ms_delay)509 static u8 _rtl92e_phy_set_sw_chnl_cmd_array(struct net_device *dev,
510 					    struct sw_chnl_cmd *CmdTable,
511 					    u32 CmdTableIdx, u32 CmdTableSz,
512 					    enum sw_chnl_cmd_id cmd_id,
513 					    u32 para1, u32 para2, u32 ms_delay)
514 {
515 	struct sw_chnl_cmd *pCmd;
516 
517 	if (!CmdTable) {
518 		netdev_err(dev, "%s(): CmdTable cannot be NULL.\n", __func__);
519 		return false;
520 	}
521 	if (CmdTableIdx >= CmdTableSz) {
522 		netdev_err(dev, "%s(): Invalid index requested.\n", __func__);
523 		return false;
524 	}
525 
526 	pCmd = CmdTable + CmdTableIdx;
527 	pCmd->cmd_id = cmd_id;
528 	pCmd->para1 = para1;
529 	pCmd->para2 = para2;
530 	pCmd->ms_delay = ms_delay;
531 
532 	return true;
533 }
534 
_rtl92e_phy_switch_channel_step(struct net_device * dev,u8 channel,u8 * stage,u8 * step,u32 * delay)535 static u8 _rtl92e_phy_switch_channel_step(struct net_device *dev, u8 channel,
536 					  u8 *stage, u8 *step, u32 *delay)
537 {
538 	struct r8192_priv *priv = rtllib_priv(dev);
539 	struct rtllib_device *ieee = priv->rtllib;
540 	u32					PreCommonCmdCnt;
541 	u32					PostCommonCmdCnt;
542 	u32					RfDependCmdCnt;
543 	struct sw_chnl_cmd *CurrentCmd = NULL;
544 	u8		eRFPath;
545 
546 	if (!rtllib_legal_channel(priv->rtllib, channel)) {
547 		netdev_err(dev, "Invalid channel requested: %d\n", channel);
548 		return true;
549 	}
550 
551 	{
552 		PreCommonCmdCnt = 0;
553 		_rtl92e_phy_set_sw_chnl_cmd_array(dev, ieee->PreCommonCmd,
554 						  PreCommonCmdCnt++,
555 						  MAX_PRECMD_CNT,
556 						  cmd_id_set_tx_power_level,
557 						  0, 0, 0);
558 		_rtl92e_phy_set_sw_chnl_cmd_array(dev, ieee->PreCommonCmd,
559 						  PreCommonCmdCnt++,
560 						  MAX_PRECMD_CNT, cmd_id_end,
561 						  0, 0, 0);
562 
563 		PostCommonCmdCnt = 0;
564 
565 		_rtl92e_phy_set_sw_chnl_cmd_array(dev, ieee->PostCommonCmd,
566 						  PostCommonCmdCnt++,
567 						  MAX_POSTCMD_CNT, cmd_id_end,
568 						  0, 0, 0);
569 
570 		RfDependCmdCnt = 0;
571 
572 		if (!(channel >= 1 && channel <= 14)) {
573 			netdev_err(dev,
574 				   "Invalid channel requested for 8256: %d\n",
575 				   channel);
576 			return false;
577 		}
578 		_rtl92e_phy_set_sw_chnl_cmd_array(dev,
579 						  ieee->RfDependCmd,
580 						  RfDependCmdCnt++,
581 						  MAX_RFDEPENDCMD_CNT,
582 						  cmd_id_rf_write_reg,
583 						  rZebra1_Channel,
584 						  channel, 10);
585 		_rtl92e_phy_set_sw_chnl_cmd_array(dev,
586 						  ieee->RfDependCmd,
587 						  RfDependCmdCnt++,
588 						  MAX_RFDEPENDCMD_CNT,
589 						  cmd_id_end, 0, 0, 0);
590 
591 		do {
592 			switch (*stage) {
593 			case 0:
594 				CurrentCmd = &ieee->PreCommonCmd[*step];
595 				break;
596 			case 1:
597 				CurrentCmd = &ieee->RfDependCmd[*step];
598 				break;
599 			case 2:
600 				CurrentCmd = &ieee->PostCommonCmd[*step];
601 				break;
602 			}
603 
604 			if (CurrentCmd && CurrentCmd->cmd_id == cmd_id_end) {
605 				if ((*stage) == 2)
606 					return true;
607 				(*stage)++;
608 				(*step) = 0;
609 				continue;
610 			}
611 
612 			if (!CurrentCmd)
613 				continue;
614 			switch (CurrentCmd->cmd_id) {
615 			case cmd_id_set_tx_power_level:
616 				if (priv->ic_cut > VERSION_8190_BD)
617 					_rtl92e_set_tx_power_level(dev,
618 								   channel);
619 				break;
620 			case cmd_id_write_port_ulong:
621 				rtl92e_writel(dev, CurrentCmd->para1,
622 					      CurrentCmd->para2);
623 				break;
624 			case cmd_id_write_port_ushort:
625 				rtl92e_writew(dev, CurrentCmd->para1,
626 					      CurrentCmd->para2);
627 				break;
628 			case cmd_id_write_port_uchar:
629 				rtl92e_writeb(dev, CurrentCmd->para1,
630 					      CurrentCmd->para2);
631 				break;
632 			case cmd_id_rf_write_reg:
633 				for (eRFPath = 0; eRFPath <
634 				     priv->num_total_rf_path; eRFPath++)
635 					rtl92e_set_rf_reg(dev,
636 						 (enum rf90_radio_path)eRFPath,
637 						 CurrentCmd->para1, bMask12Bits,
638 						 CurrentCmd->para2 << 7);
639 				break;
640 			default:
641 				break;
642 			}
643 
644 			break;
645 		} while (true);
646 	} /*for (Number of RF paths)*/
647 
648 	(*delay) = CurrentCmd->ms_delay;
649 	(*step)++;
650 	return false;
651 }
652 
_rtl92e_phy_switch_channel(struct net_device * dev,u8 channel)653 static void _rtl92e_phy_switch_channel(struct net_device *dev, u8 channel)
654 {
655 	struct r8192_priv *priv = rtllib_priv(dev);
656 	u32 delay = 0;
657 
658 	while (!_rtl92e_phy_switch_channel_step(dev, channel,
659 						&priv->sw_chnl_stage,
660 						&priv->sw_chnl_step, &delay)) {
661 		if (delay > 0)
662 			msleep(delay);
663 		if (!priv->up)
664 			break;
665 	}
666 }
667 
_rtl92e_phy_switch_channel_work_item(struct net_device * dev)668 static void _rtl92e_phy_switch_channel_work_item(struct net_device *dev)
669 {
670 	struct r8192_priv *priv = rtllib_priv(dev);
671 
672 	_rtl92e_phy_switch_channel(dev, priv->chan);
673 }
674 
rtl92e_set_channel(struct net_device * dev,u8 channel)675 void rtl92e_set_channel(struct net_device *dev, u8 channel)
676 {
677 	struct r8192_priv *priv = rtllib_priv(dev);
678 
679 	if (!priv->up) {
680 		netdev_err(dev, "%s(): Driver is not initialized\n", __func__);
681 		return;
682 	}
683 	if (priv->sw_chnl_in_progress)
684 		return;
685 
686 	switch (priv->rtllib->mode) {
687 	case WIRELESS_MODE_B:
688 		if (channel > 14) {
689 			netdev_warn(dev,
690 				    "Channel %d not available in 802.11b.\n",
691 				    channel);
692 			return;
693 		}
694 		break;
695 	case WIRELESS_MODE_G:
696 	case WIRELESS_MODE_N_24G:
697 		if (channel > 14) {
698 			netdev_warn(dev,
699 				    "Channel %d not available in 802.11g.\n",
700 				    channel);
701 			return;
702 		}
703 		break;
704 	}
705 
706 	priv->sw_chnl_in_progress = true;
707 	if (channel == 0)
708 		channel = 1;
709 
710 	priv->chan = channel;
711 
712 	priv->sw_chnl_stage = 0;
713 	priv->sw_chnl_step = 0;
714 
715 	if (priv->up)
716 		_rtl92e_phy_switch_channel_work_item(dev);
717 	priv->sw_chnl_in_progress = false;
718 }
719 
_rtl92e_cck_tx_power_track_bw_switch_tssi(struct net_device * dev)720 static void _rtl92e_cck_tx_power_track_bw_switch_tssi(struct net_device *dev)
721 {
722 	struct r8192_priv *priv = rtllib_priv(dev);
723 
724 	switch (priv->current_chnl_bw) {
725 	case HT_CHANNEL_WIDTH_20:
726 		priv->cck_present_attn =
727 			priv->cck_present_attn_20m_def +
728 			    priv->cck_present_attn_diff;
729 
730 		if (priv->cck_present_attn >
731 		    (CCK_TX_BB_GAIN_TABLE_LEN - 1))
732 			priv->cck_present_attn =
733 					 CCK_TX_BB_GAIN_TABLE_LEN - 1;
734 		if (priv->cck_present_attn < 0)
735 			priv->cck_present_attn = 0;
736 
737 		if (priv->rtllib->current_network.channel == 14 &&
738 		    !priv->bcck_in_ch14) {
739 			priv->bcck_in_ch14 = true;
740 			rtl92e_dm_cck_txpower_adjust(dev, priv->bcck_in_ch14);
741 		} else if (priv->rtllib->current_network.channel !=
742 			   14 && priv->bcck_in_ch14) {
743 			priv->bcck_in_ch14 = false;
744 			rtl92e_dm_cck_txpower_adjust(dev, priv->bcck_in_ch14);
745 		} else {
746 			rtl92e_dm_cck_txpower_adjust(dev, priv->bcck_in_ch14);
747 		}
748 		break;
749 
750 	case HT_CHANNEL_WIDTH_20_40:
751 		priv->cck_present_attn =
752 			priv->cck_present_attn_40m_def +
753 			priv->cck_present_attn_diff;
754 
755 		if (priv->cck_present_attn >
756 		    (CCK_TX_BB_GAIN_TABLE_LEN - 1))
757 			priv->cck_present_attn =
758 					 CCK_TX_BB_GAIN_TABLE_LEN - 1;
759 		if (priv->cck_present_attn < 0)
760 			priv->cck_present_attn = 0;
761 
762 		if (priv->rtllib->current_network.channel == 14 &&
763 		    !priv->bcck_in_ch14) {
764 			priv->bcck_in_ch14 = true;
765 			rtl92e_dm_cck_txpower_adjust(dev, priv->bcck_in_ch14);
766 		} else if (priv->rtllib->current_network.channel != 14
767 			   && priv->bcck_in_ch14) {
768 			priv->bcck_in_ch14 = false;
769 			rtl92e_dm_cck_txpower_adjust(dev, priv->bcck_in_ch14);
770 		} else {
771 			rtl92e_dm_cck_txpower_adjust(dev, priv->bcck_in_ch14);
772 		}
773 		break;
774 	}
775 }
776 
_rtl92e_cck_tx_power_track_bw_switch_thermal(struct net_device * dev)777 static void _rtl92e_cck_tx_power_track_bw_switch_thermal(struct net_device *dev)
778 {
779 	struct r8192_priv *priv = rtllib_priv(dev);
780 
781 	if (priv->rtllib->current_network.channel == 14 &&
782 	    !priv->bcck_in_ch14)
783 		priv->bcck_in_ch14 = true;
784 	else if (priv->rtllib->current_network.channel != 14 &&
785 		 priv->bcck_in_ch14)
786 		priv->bcck_in_ch14 = false;
787 
788 	switch (priv->current_chnl_bw) {
789 	case HT_CHANNEL_WIDTH_20:
790 		if (priv->rec_cck_20m_idx == 0)
791 			priv->rec_cck_20m_idx = 6;
792 		priv->cck_index = priv->rec_cck_20m_idx;
793 	break;
794 
795 	case HT_CHANNEL_WIDTH_20_40:
796 		priv->cck_index = priv->rec_cck_40m_idx;
797 	break;
798 	}
799 	rtl92e_dm_cck_txpower_adjust(dev, priv->bcck_in_ch14);
800 }
801 
_rtl92e_cck_tx_power_track_bw_switch(struct net_device * dev)802 static void _rtl92e_cck_tx_power_track_bw_switch(struct net_device *dev)
803 {
804 	struct r8192_priv *priv = rtllib_priv(dev);
805 
806 	if (priv->ic_cut >= IC_VersionCut_D)
807 		_rtl92e_cck_tx_power_track_bw_switch_tssi(dev);
808 	else
809 		_rtl92e_cck_tx_power_track_bw_switch_thermal(dev);
810 }
811 
_rtl92e_set_bw_mode_work_item(struct net_device * dev)812 static void _rtl92e_set_bw_mode_work_item(struct net_device *dev)
813 {
814 	struct r8192_priv *priv = rtllib_priv(dev);
815 	u8 regBwOpMode;
816 
817 	if (!priv->up) {
818 		netdev_err(dev, "%s(): Driver is not initialized\n", __func__);
819 		return;
820 	}
821 	regBwOpMode = rtl92e_readb(dev, BW_OPMODE);
822 
823 	switch (priv->current_chnl_bw) {
824 	case HT_CHANNEL_WIDTH_20:
825 		regBwOpMode |= BW_OPMODE_20MHZ;
826 		rtl92e_writeb(dev, BW_OPMODE, regBwOpMode);
827 		break;
828 
829 	case HT_CHANNEL_WIDTH_20_40:
830 		regBwOpMode &= ~BW_OPMODE_20MHZ;
831 		rtl92e_writeb(dev, BW_OPMODE, regBwOpMode);
832 		break;
833 
834 	default:
835 		netdev_err(dev, "%s(): unknown Bandwidth: %#X\n", __func__,
836 			   priv->current_chnl_bw);
837 		break;
838 	}
839 
840 	switch (priv->current_chnl_bw) {
841 	case HT_CHANNEL_WIDTH_20:
842 		rtl92e_set_bb_reg(dev, rFPGA0_RFMOD, bRFMOD, 0x0);
843 		rtl92e_set_bb_reg(dev, rFPGA1_RFMOD, bRFMOD, 0x0);
844 
845 		if (!priv->btxpower_tracking) {
846 			rtl92e_writel(dev, rCCK0_TxFilter1, 0x1a1b0000);
847 			rtl92e_writel(dev, rCCK0_TxFilter2, 0x090e1317);
848 			rtl92e_writel(dev, rCCK0_DebugPort, 0x00000204);
849 		} else {
850 			_rtl92e_cck_tx_power_track_bw_switch(dev);
851 		}
852 
853 		rtl92e_set_bb_reg(dev, rFPGA0_AnalogParameter1, 0x00100000, 1);
854 
855 		break;
856 	case HT_CHANNEL_WIDTH_20_40:
857 		rtl92e_set_bb_reg(dev, rFPGA0_RFMOD, bRFMOD, 0x1);
858 		rtl92e_set_bb_reg(dev, rFPGA1_RFMOD, bRFMOD, 0x1);
859 
860 		if (!priv->btxpower_tracking) {
861 			rtl92e_writel(dev, rCCK0_TxFilter1, 0x35360000);
862 			rtl92e_writel(dev, rCCK0_TxFilter2, 0x121c252e);
863 			rtl92e_writel(dev, rCCK0_DebugPort, 0x00000409);
864 		} else {
865 			_rtl92e_cck_tx_power_track_bw_switch(dev);
866 		}
867 
868 		rtl92e_set_bb_reg(dev, rCCK0_System, bCCKSideBand,
869 				  (priv->n_cur_40mhz_prime_sc >> 1));
870 		rtl92e_set_bb_reg(dev, rOFDM1_LSTF, 0xC00,
871 				  priv->n_cur_40mhz_prime_sc);
872 
873 		rtl92e_set_bb_reg(dev, rFPGA0_AnalogParameter1, 0x00100000, 0);
874 		break;
875 	default:
876 		netdev_err(dev, "%s(): unknown Bandwidth: %#X\n", __func__,
877 			   priv->current_chnl_bw);
878 		break;
879 	}
880 
881 	rtl92e_set_bandwidth(dev, priv->current_chnl_bw);
882 
883 	atomic_dec(&(priv->rtllib->atm_swbw));
884 	priv->set_bw_mode_in_progress = false;
885 }
886 
rtl92e_set_bw_mode(struct net_device * dev,enum ht_channel_width bandwidth,enum ht_extchnl_offset Offset)887 void rtl92e_set_bw_mode(struct net_device *dev, enum ht_channel_width bandwidth,
888 			enum ht_extchnl_offset Offset)
889 {
890 	struct r8192_priv *priv = rtllib_priv(dev);
891 
892 	if (priv->set_bw_mode_in_progress)
893 		return;
894 
895 	atomic_inc(&(priv->rtllib->atm_swbw));
896 	priv->set_bw_mode_in_progress = true;
897 
898 	priv->current_chnl_bw = bandwidth;
899 
900 	if (Offset == HT_EXTCHNL_OFFSET_LOWER)
901 		priv->n_cur_40mhz_prime_sc = HAL_PRIME_CHNL_OFFSET_UPPER;
902 	else if (Offset == HT_EXTCHNL_OFFSET_UPPER)
903 		priv->n_cur_40mhz_prime_sc = HAL_PRIME_CHNL_OFFSET_LOWER;
904 	else
905 		priv->n_cur_40mhz_prime_sc = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
906 
907 	_rtl92e_set_bw_mode_work_item(dev);
908 }
909 
rtl92e_init_gain(struct net_device * dev,u8 Operation)910 void rtl92e_init_gain(struct net_device *dev, u8 Operation)
911 {
912 #define SCAN_RX_INITIAL_GAIN	0x17
913 #define POWER_DETECTION_TH	0x08
914 	struct r8192_priv *priv = rtllib_priv(dev);
915 	u32 BitMask;
916 	u8 initial_gain;
917 
918 	if (priv->up) {
919 		switch (Operation) {
920 		case IG_Backup:
921 			initial_gain = SCAN_RX_INITIAL_GAIN;
922 			BitMask = bMaskByte0;
923 			priv->initgain_backup.xaagccore1 =
924 				 rtl92e_get_bb_reg(dev, rOFDM0_XAAGCCore1,
925 						   BitMask);
926 			priv->initgain_backup.xbagccore1 =
927 				 rtl92e_get_bb_reg(dev, rOFDM0_XBAGCCore1,
928 						   BitMask);
929 			priv->initgain_backup.xcagccore1 =
930 				 rtl92e_get_bb_reg(dev, rOFDM0_XCAGCCore1,
931 						   BitMask);
932 			priv->initgain_backup.xdagccore1 =
933 				 rtl92e_get_bb_reg(dev, rOFDM0_XDAGCCore1,
934 						   BitMask);
935 			BitMask = bMaskByte2;
936 			priv->initgain_backup.cca = (u8)rtl92e_get_bb_reg(dev,
937 						    rCCK0_CCA, BitMask);
938 
939 			rtl92e_writeb(dev, rOFDM0_XAAGCCore1, initial_gain);
940 			rtl92e_writeb(dev, rOFDM0_XBAGCCore1, initial_gain);
941 			rtl92e_writeb(dev, rOFDM0_XCAGCCore1, initial_gain);
942 			rtl92e_writeb(dev, rOFDM0_XDAGCCore1, initial_gain);
943 			rtl92e_writeb(dev, 0xa0a, POWER_DETECTION_TH);
944 			break;
945 		case IG_Restore:
946 			BitMask = 0x7f;
947 			rtl92e_set_bb_reg(dev, rOFDM0_XAAGCCore1, BitMask,
948 					  (u32)priv->initgain_backup.xaagccore1);
949 			rtl92e_set_bb_reg(dev, rOFDM0_XBAGCCore1, BitMask,
950 					  (u32)priv->initgain_backup.xbagccore1);
951 			rtl92e_set_bb_reg(dev, rOFDM0_XCAGCCore1, BitMask,
952 					  (u32)priv->initgain_backup.xcagccore1);
953 			rtl92e_set_bb_reg(dev, rOFDM0_XDAGCCore1, BitMask,
954 					  (u32)priv->initgain_backup.xdagccore1);
955 			BitMask  = bMaskByte2;
956 			rtl92e_set_bb_reg(dev, rCCK0_CCA, BitMask,
957 					  (u32)priv->initgain_backup.cca);
958 
959 			rtl92e_set_tx_power(dev,
960 					    priv->rtllib->current_network.channel);
961 			break;
962 		}
963 	}
964 }
965 
rtl92e_set_rf_off(struct net_device * dev)966 void rtl92e_set_rf_off(struct net_device *dev)
967 {
968 	rtl92e_set_bb_reg(dev, rFPGA0_XA_RFInterfaceOE, BIT(4), 0x0);
969 	rtl92e_set_bb_reg(dev, rFPGA0_AnalogParameter4, 0x300, 0x0);
970 	rtl92e_set_bb_reg(dev, rFPGA0_AnalogParameter1, 0x18, 0x0);
971 	rtl92e_set_bb_reg(dev, rOFDM0_TRxPathEnable, 0xf, 0x0);
972 	rtl92e_set_bb_reg(dev, rOFDM1_TRxPathEnable, 0xf, 0x0);
973 	rtl92e_set_bb_reg(dev, rFPGA0_AnalogParameter1, 0x60, 0x0);
974 	rtl92e_set_bb_reg(dev, rFPGA0_AnalogParameter1, 0x4, 0x0);
975 	rtl92e_writeb(dev, ANAPAR_FOR_8192PCIE, 0x07);
976 }
977 
_rtl92e_set_rf_power_state(struct net_device * dev,enum rt_rf_power_state rf_power_state)978 static bool _rtl92e_set_rf_power_state(struct net_device *dev,
979 				       enum rt_rf_power_state rf_power_state)
980 {
981 	struct r8192_priv *priv = rtllib_priv(dev);
982 	struct rt_pwr_save_ctrl *psc = (struct rt_pwr_save_ctrl *)
983 					(&priv->rtllib->pwr_save_ctrl);
984 	bool bResult = true;
985 	u8	i = 0, QueueID = 0;
986 	struct rtl8192_tx_ring  *ring = NULL;
987 
988 	if (priv->set_rf_pwr_state_in_progress)
989 		return false;
990 	priv->set_rf_pwr_state_in_progress = true;
991 
992 	switch (rf_power_state) {
993 	case rf_on:
994 		if ((priv->rtllib->rf_power_state == rf_off) &&
995 		     RT_IN_PS_LEVEL(psc, RT_RF_OFF_LEVL_HALT_NIC)) {
996 			bool rtstatus;
997 			u32 InitilizeCount = 3;
998 
999 			do {
1000 				InitilizeCount--;
1001 				rtstatus = rtl92e_enable_nic(dev);
1002 			} while (!rtstatus && (InitilizeCount > 0));
1003 			if (!rtstatus) {
1004 				netdev_err(dev,
1005 					   "%s(): Failed to initialize Adapter.\n",
1006 					   __func__);
1007 				priv->set_rf_pwr_state_in_progress = false;
1008 				return false;
1009 			}
1010 			RT_CLEAR_PS_LEVEL(psc,
1011 					  RT_RF_OFF_LEVL_HALT_NIC);
1012 		} else {
1013 			rtl92e_writeb(dev, ANAPAR, 0x37);
1014 			mdelay(1);
1015 			rtl92e_set_bb_reg(dev, rFPGA0_AnalogParameter1,
1016 					 0x4, 0x1);
1017 			priv->hw_rf_off_action = 0;
1018 			rtl92e_set_bb_reg(dev, rFPGA0_XA_RFInterfaceOE,
1019 					  BIT(4), 0x1);
1020 			rtl92e_set_bb_reg(dev, rFPGA0_AnalogParameter4,
1021 					  0x300, 0x3);
1022 			rtl92e_set_bb_reg(dev, rFPGA0_AnalogParameter1,
1023 					  0x18, 0x3);
1024 			rtl92e_set_bb_reg(dev, rOFDM0_TRxPathEnable,
1025 					  0x3, 0x3);
1026 			rtl92e_set_bb_reg(dev, rOFDM1_TRxPathEnable,
1027 					  0x3, 0x3);
1028 			rtl92e_set_bb_reg(dev, rFPGA0_AnalogParameter1,
1029 					  0x60, 0x3);
1030 		}
1031 		break;
1032 	case rf_sleep:
1033 		if (priv->rtllib->rf_power_state == rf_off)
1034 			break;
1035 		for (QueueID = 0, i = 0; QueueID < MAX_TX_QUEUE; ) {
1036 			ring = &priv->tx_ring[QueueID];
1037 			if (skb_queue_len(&ring->queue) == 0) {
1038 				QueueID++;
1039 				continue;
1040 			} else {
1041 				udelay(10);
1042 				i++;
1043 			}
1044 			if (i >= MAX_DOZE_WAITING_TIMES_9x)
1045 				break;
1046 		}
1047 		rtl92e_set_rf_off(dev);
1048 		break;
1049 	case rf_off:
1050 		for (QueueID = 0, i = 0; QueueID < MAX_TX_QUEUE; ) {
1051 			ring = &priv->tx_ring[QueueID];
1052 			if (skb_queue_len(&ring->queue) == 0) {
1053 				QueueID++;
1054 				continue;
1055 			} else {
1056 				udelay(10);
1057 				i++;
1058 			}
1059 			if (i >= MAX_DOZE_WAITING_TIMES_9x)
1060 				break;
1061 		}
1062 		rtl92e_set_rf_off(dev);
1063 		break;
1064 	default:
1065 		bResult = false;
1066 		netdev_warn(dev,
1067 			    "%s(): Unknown state requested: 0x%X.\n",
1068 			    __func__, rf_power_state);
1069 		break;
1070 	}
1071 
1072 	if (bResult)
1073 		priv->rtllib->rf_power_state = rf_power_state;
1074 
1075 	priv->set_rf_pwr_state_in_progress = false;
1076 	return bResult;
1077 }
1078 
rtl92e_set_rf_power_state(struct net_device * dev,enum rt_rf_power_state rf_power_state)1079 bool rtl92e_set_rf_power_state(struct net_device *dev,
1080 			       enum rt_rf_power_state rf_power_state)
1081 {
1082 	struct r8192_priv *priv = rtllib_priv(dev);
1083 
1084 	bool bResult = false;
1085 
1086 	if (rf_power_state == priv->rtllib->rf_power_state &&
1087 	    priv->hw_rf_off_action == 0) {
1088 		return bResult;
1089 	}
1090 
1091 	bResult = _rtl92e_set_rf_power_state(dev, rf_power_state);
1092 	return bResult;
1093 }
1094 
rtl92e_scan_op_backup(struct net_device * dev,u8 Operation)1095 void rtl92e_scan_op_backup(struct net_device *dev, u8 Operation)
1096 {
1097 	struct r8192_priv *priv = rtllib_priv(dev);
1098 
1099 	if (priv->up) {
1100 		switch (Operation) {
1101 		case SCAN_OPT_BACKUP:
1102 			priv->rtllib->init_gain_handler(dev, IG_Backup);
1103 			break;
1104 
1105 		case SCAN_OPT_RESTORE:
1106 			priv->rtllib->init_gain_handler(dev, IG_Restore);
1107 			break;
1108 		}
1109 	}
1110 }
1111