1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2009-2012  Realtek Corporation.*/
3 
4 #include "../wifi.h"
5 #include "../efuse.h"
6 #include "../base.h"
7 #include "../cam.h"
8 #include "../ps.h"
9 #include "../usb.h"
10 #include "reg.h"
11 #include "def.h"
12 #include "phy.h"
13 #include "../rtl8192c/phy_common.h"
14 #include "mac.h"
15 #include "dm.h"
16 #include "../rtl8192c/dm_common.h"
17 #include "../rtl8192c/fw_common.h"
18 #include "hw.h"
19 #include "../rtl8192ce/hw.h"
20 #include "trx.h"
21 #include "led.h"
22 #include "table.h"
23 
24 static void _rtl92cu_phy_param_tab_init(struct ieee80211_hw *hw)
25 {
26 	struct rtl_priv *rtlpriv = rtl_priv(hw);
27 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
28 	struct rtl_efuse *rtlefuse = rtl_efuse(rtlpriv);
29 
30 	rtlphy->hwparam_tables[MAC_REG].length = RTL8192CUMAC_2T_ARRAYLENGTH;
31 	rtlphy->hwparam_tables[MAC_REG].pdata = RTL8192CUMAC_2T_ARRAY;
32 	if (IS_HIGHT_PA(rtlefuse->board_type)) {
33 		rtlphy->hwparam_tables[PHY_REG_PG].length =
34 			RTL8192CUPHY_REG_ARRAY_PG_HPLENGTH;
35 		rtlphy->hwparam_tables[PHY_REG_PG].pdata =
36 			RTL8192CUPHY_REG_ARRAY_PG_HP;
37 	} else {
38 		rtlphy->hwparam_tables[PHY_REG_PG].length =
39 			RTL8192CUPHY_REG_ARRAY_PGLENGTH;
40 		rtlphy->hwparam_tables[PHY_REG_PG].pdata =
41 			RTL8192CUPHY_REG_ARRAY_PG;
42 	}
43 	/* 2T */
44 	rtlphy->hwparam_tables[PHY_REG_2T].length =
45 			RTL8192CUPHY_REG_2TARRAY_LENGTH;
46 	rtlphy->hwparam_tables[PHY_REG_2T].pdata =
47 			RTL8192CUPHY_REG_2TARRAY;
48 	rtlphy->hwparam_tables[RADIOA_2T].length =
49 			RTL8192CURADIOA_2TARRAYLENGTH;
50 	rtlphy->hwparam_tables[RADIOA_2T].pdata =
51 			RTL8192CURADIOA_2TARRAY;
52 	rtlphy->hwparam_tables[RADIOB_2T].length =
53 			RTL8192CURADIOB_2TARRAYLENGTH;
54 	rtlphy->hwparam_tables[RADIOB_2T].pdata =
55 			RTL8192CU_RADIOB_2TARRAY;
56 	rtlphy->hwparam_tables[AGCTAB_2T].length =
57 			RTL8192CUAGCTAB_2TARRAYLENGTH;
58 	rtlphy->hwparam_tables[AGCTAB_2T].pdata =
59 			RTL8192CUAGCTAB_2TARRAY;
60 	/* 1T */
61 	if (IS_HIGHT_PA(rtlefuse->board_type)) {
62 		rtlphy->hwparam_tables[PHY_REG_1T].length =
63 			RTL8192CUPHY_REG_1T_HPARRAYLENGTH;
64 		rtlphy->hwparam_tables[PHY_REG_1T].pdata =
65 			RTL8192CUPHY_REG_1T_HPARRAY;
66 		rtlphy->hwparam_tables[RADIOA_1T].length =
67 			RTL8192CURADIOA_1T_HPARRAYLENGTH;
68 		rtlphy->hwparam_tables[RADIOA_1T].pdata =
69 			RTL8192CURADIOA_1T_HPARRAY;
70 		rtlphy->hwparam_tables[RADIOB_1T].length =
71 			RTL8192CURADIOB_1TARRAYLENGTH;
72 		rtlphy->hwparam_tables[RADIOB_1T].pdata =
73 			RTL8192CU_RADIOB_1TARRAY;
74 		rtlphy->hwparam_tables[AGCTAB_1T].length =
75 			RTL8192CUAGCTAB_1T_HPARRAYLENGTH;
76 		rtlphy->hwparam_tables[AGCTAB_1T].pdata =
77 			RTL8192CUAGCTAB_1T_HPARRAY;
78 	} else {
79 		rtlphy->hwparam_tables[PHY_REG_1T].length =
80 			 RTL8192CUPHY_REG_1TARRAY_LENGTH;
81 		rtlphy->hwparam_tables[PHY_REG_1T].pdata =
82 			RTL8192CUPHY_REG_1TARRAY;
83 		rtlphy->hwparam_tables[RADIOA_1T].length =
84 			RTL8192CURADIOA_1TARRAYLENGTH;
85 		rtlphy->hwparam_tables[RADIOA_1T].pdata =
86 			RTL8192CU_RADIOA_1TARRAY;
87 		rtlphy->hwparam_tables[RADIOB_1T].length =
88 			RTL8192CURADIOB_1TARRAYLENGTH;
89 		rtlphy->hwparam_tables[RADIOB_1T].pdata =
90 			RTL8192CU_RADIOB_1TARRAY;
91 		rtlphy->hwparam_tables[AGCTAB_1T].length =
92 			RTL8192CUAGCTAB_1TARRAYLENGTH;
93 		rtlphy->hwparam_tables[AGCTAB_1T].pdata =
94 			RTL8192CUAGCTAB_1TARRAY;
95 	}
96 }
97 
98 static void _rtl92cu_read_txpower_info_from_hwpg(struct ieee80211_hw *hw,
99 						 bool autoload_fail,
100 						 u8 *hwinfo)
101 {
102 	struct rtl_priv *rtlpriv = rtl_priv(hw);
103 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
104 	u8 rf_path, index, tempval;
105 	u16 i;
106 
107 	for (rf_path = 0; rf_path < 2; rf_path++) {
108 		for (i = 0; i < 3; i++) {
109 			if (!autoload_fail) {
110 				rtlefuse->
111 				    eeprom_chnlarea_txpwr_cck[rf_path][i] =
112 				    hwinfo[EEPROM_TXPOWERCCK + rf_path * 3 + i];
113 				rtlefuse->
114 				    eeprom_chnlarea_txpwr_ht40_1s[rf_path][i] =
115 				    hwinfo[EEPROM_TXPOWERHT40_1S + rf_path * 3 +
116 					   i];
117 			} else {
118 				rtlefuse->
119 				    eeprom_chnlarea_txpwr_cck[rf_path][i] =
120 				    EEPROM_DEFAULT_TXPOWERLEVEL;
121 				rtlefuse->
122 				    eeprom_chnlarea_txpwr_ht40_1s[rf_path][i] =
123 				    EEPROM_DEFAULT_TXPOWERLEVEL;
124 			}
125 		}
126 	}
127 	for (i = 0; i < 3; i++) {
128 		if (!autoload_fail)
129 			tempval = hwinfo[EEPROM_TXPOWERHT40_2SDIFF + i];
130 		else
131 			tempval = EEPROM_DEFAULT_HT40_2SDIFF;
132 		rtlefuse->eprom_chnl_txpwr_ht40_2sdf[RF90_PATH_A][i] =
133 		    (tempval & 0xf);
134 		rtlefuse->eprom_chnl_txpwr_ht40_2sdf[RF90_PATH_B][i] =
135 		    ((tempval & 0xf0) >> 4);
136 	}
137 	for (rf_path = 0; rf_path < 2; rf_path++)
138 		for (i = 0; i < 3; i++)
139 			RTPRINT(rtlpriv, FINIT, INIT_EEPROM,
140 				"RF(%d) EEPROM CCK Area(%d) = 0x%x\n",
141 				rf_path, i,
142 				rtlefuse->
143 				eeprom_chnlarea_txpwr_cck[rf_path][i]);
144 	for (rf_path = 0; rf_path < 2; rf_path++)
145 		for (i = 0; i < 3; i++)
146 			RTPRINT(rtlpriv, FINIT, INIT_EEPROM,
147 				"RF(%d) EEPROM HT40 1S Area(%d) = 0x%x\n",
148 				rf_path, i,
149 				rtlefuse->
150 				eeprom_chnlarea_txpwr_ht40_1s[rf_path][i]);
151 	for (rf_path = 0; rf_path < 2; rf_path++)
152 		for (i = 0; i < 3; i++)
153 			RTPRINT(rtlpriv, FINIT, INIT_EEPROM,
154 				"RF(%d) EEPROM HT40 2S Diff Area(%d) = 0x%x\n",
155 				rf_path, i,
156 				rtlefuse->
157 				eprom_chnl_txpwr_ht40_2sdf[rf_path][i]);
158 	for (rf_path = 0; rf_path < 2; rf_path++) {
159 		for (i = 0; i < 14; i++) {
160 			index = rtl92c_get_chnl_group((u8)i);
161 			rtlefuse->txpwrlevel_cck[rf_path][i] =
162 			    rtlefuse->eeprom_chnlarea_txpwr_cck[rf_path][index];
163 			rtlefuse->txpwrlevel_ht40_1s[rf_path][i] =
164 			    rtlefuse->
165 			    eeprom_chnlarea_txpwr_ht40_1s[rf_path][index];
166 			if ((rtlefuse->
167 			     eeprom_chnlarea_txpwr_ht40_1s[rf_path][index] -
168 			     rtlefuse->
169 			     eprom_chnl_txpwr_ht40_2sdf[rf_path][index])
170 			    > 0) {
171 				rtlefuse->txpwrlevel_ht40_2s[rf_path][i] =
172 				    rtlefuse->
173 				    eeprom_chnlarea_txpwr_ht40_1s[rf_path]
174 				    [index] - rtlefuse->
175 				    eprom_chnl_txpwr_ht40_2sdf[rf_path]
176 				    [index];
177 			} else {
178 				rtlefuse->txpwrlevel_ht40_2s[rf_path][i] = 0;
179 			}
180 		}
181 		for (i = 0; i < 14; i++) {
182 			RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
183 				"RF(%d)-Ch(%d) [CCK / HT40_1S / HT40_2S] = [0x%x / 0x%x / 0x%x]\n", rf_path, i,
184 				rtlefuse->txpwrlevel_cck[rf_path][i],
185 				rtlefuse->txpwrlevel_ht40_1s[rf_path][i],
186 				rtlefuse->txpwrlevel_ht40_2s[rf_path][i]);
187 		}
188 	}
189 	for (i = 0; i < 3; i++) {
190 		if (!autoload_fail) {
191 			rtlefuse->eeprom_pwrlimit_ht40[i] =
192 			    hwinfo[EEPROM_TXPWR_GROUP + i];
193 			rtlefuse->eeprom_pwrlimit_ht20[i] =
194 			    hwinfo[EEPROM_TXPWR_GROUP + 3 + i];
195 		} else {
196 			rtlefuse->eeprom_pwrlimit_ht40[i] = 0;
197 			rtlefuse->eeprom_pwrlimit_ht20[i] = 0;
198 		}
199 	}
200 	for (rf_path = 0; rf_path < 2; rf_path++) {
201 		for (i = 0; i < 14; i++) {
202 			index = rtl92c_get_chnl_group((u8)i);
203 			if (rf_path == RF90_PATH_A) {
204 				rtlefuse->pwrgroup_ht20[rf_path][i] =
205 				    (rtlefuse->eeprom_pwrlimit_ht20[index]
206 				     & 0xf);
207 				rtlefuse->pwrgroup_ht40[rf_path][i] =
208 				    (rtlefuse->eeprom_pwrlimit_ht40[index]
209 				     & 0xf);
210 			} else if (rf_path == RF90_PATH_B) {
211 				rtlefuse->pwrgroup_ht20[rf_path][i] =
212 				    ((rtlefuse->eeprom_pwrlimit_ht20[index]
213 				      & 0xf0) >> 4);
214 				rtlefuse->pwrgroup_ht40[rf_path][i] =
215 				    ((rtlefuse->eeprom_pwrlimit_ht40[index]
216 				      & 0xf0) >> 4);
217 			}
218 			RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
219 				"RF-%d pwrgroup_ht20[%d] = 0x%x\n",
220 				rf_path, i,
221 				rtlefuse->pwrgroup_ht20[rf_path][i]);
222 			RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
223 				"RF-%d pwrgroup_ht40[%d] = 0x%x\n",
224 				rf_path, i,
225 				rtlefuse->pwrgroup_ht40[rf_path][i]);
226 		}
227 	}
228 	for (i = 0; i < 14; i++) {
229 		index = rtl92c_get_chnl_group((u8)i);
230 		if (!autoload_fail)
231 			tempval = hwinfo[EEPROM_TXPOWERHT20DIFF + index];
232 		else
233 			tempval = EEPROM_DEFAULT_HT20_DIFF;
234 		rtlefuse->txpwr_ht20diff[RF90_PATH_A][i] = (tempval & 0xF);
235 		rtlefuse->txpwr_ht20diff[RF90_PATH_B][i] =
236 		    ((tempval >> 4) & 0xF);
237 		if (rtlefuse->txpwr_ht20diff[RF90_PATH_A][i] & BIT(3))
238 			rtlefuse->txpwr_ht20diff[RF90_PATH_A][i] |= 0xF0;
239 		if (rtlefuse->txpwr_ht20diff[RF90_PATH_B][i] & BIT(3))
240 			rtlefuse->txpwr_ht20diff[RF90_PATH_B][i] |= 0xF0;
241 		index = rtl92c_get_chnl_group((u8)i);
242 		if (!autoload_fail)
243 			tempval = hwinfo[EEPROM_TXPOWER_OFDMDIFF + index];
244 		else
245 			tempval = EEPROM_DEFAULT_LEGACYHTTXPOWERDIFF;
246 		rtlefuse->txpwr_legacyhtdiff[RF90_PATH_A][i] = (tempval & 0xF);
247 		rtlefuse->txpwr_legacyhtdiff[RF90_PATH_B][i] =
248 		    ((tempval >> 4) & 0xF);
249 	}
250 	rtlefuse->legacy_ht_txpowerdiff =
251 	    rtlefuse->txpwr_legacyhtdiff[RF90_PATH_A][7];
252 	for (i = 0; i < 14; i++)
253 		RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
254 			"RF-A Ht20 to HT40 Diff[%d] = 0x%x\n",
255 			i, rtlefuse->txpwr_ht20diff[RF90_PATH_A][i]);
256 	for (i = 0; i < 14; i++)
257 		RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
258 			"RF-A Legacy to Ht40 Diff[%d] = 0x%x\n",
259 			i, rtlefuse->txpwr_legacyhtdiff[RF90_PATH_A][i]);
260 	for (i = 0; i < 14; i++)
261 		RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
262 			"RF-B Ht20 to HT40 Diff[%d] = 0x%x\n",
263 			i, rtlefuse->txpwr_ht20diff[RF90_PATH_B][i]);
264 	for (i = 0; i < 14; i++)
265 		RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
266 			"RF-B Legacy to HT40 Diff[%d] = 0x%x\n",
267 			i, rtlefuse->txpwr_legacyhtdiff[RF90_PATH_B][i]);
268 	if (!autoload_fail)
269 		rtlefuse->eeprom_regulatory = (hwinfo[RF_OPTION1] & 0x7);
270 	else
271 		rtlefuse->eeprom_regulatory = 0;
272 	RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
273 		"eeprom_regulatory = 0x%x\n", rtlefuse->eeprom_regulatory);
274 	if (!autoload_fail) {
275 		rtlefuse->eeprom_tssi[RF90_PATH_A] = hwinfo[EEPROM_TSSI_A];
276 		rtlefuse->eeprom_tssi[RF90_PATH_B] = hwinfo[EEPROM_TSSI_B];
277 	} else {
278 		rtlefuse->eeprom_tssi[RF90_PATH_A] = EEPROM_DEFAULT_TSSI;
279 		rtlefuse->eeprom_tssi[RF90_PATH_B] = EEPROM_DEFAULT_TSSI;
280 	}
281 	RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
282 		"TSSI_A = 0x%x, TSSI_B = 0x%x\n",
283 		rtlefuse->eeprom_tssi[RF90_PATH_A],
284 		rtlefuse->eeprom_tssi[RF90_PATH_B]);
285 	if (!autoload_fail)
286 		tempval = hwinfo[EEPROM_THERMAL_METER];
287 	else
288 		tempval = EEPROM_DEFAULT_THERMALMETER;
289 	rtlefuse->eeprom_thermalmeter = (tempval & 0x1f);
290 	if (rtlefuse->eeprom_thermalmeter < 0x06 ||
291 	    rtlefuse->eeprom_thermalmeter > 0x1c)
292 		rtlefuse->eeprom_thermalmeter = 0x12;
293 	if (rtlefuse->eeprom_thermalmeter == 0x1f || autoload_fail)
294 		rtlefuse->apk_thermalmeterignore = true;
295 	rtlefuse->thermalmeter[0] = rtlefuse->eeprom_thermalmeter;
296 	RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
297 		"thermalmeter = 0x%x\n", rtlefuse->eeprom_thermalmeter);
298 }
299 
300 static void _rtl92cu_read_board_type(struct ieee80211_hw *hw, u8 *contents)
301 {
302 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
303 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
304 	u8 boardtype;
305 
306 	if (IS_NORMAL_CHIP(rtlhal->version)) {
307 		boardtype = ((contents[EEPROM_RF_OPT1]) &
308 			    BOARD_TYPE_NORMAL_MASK) >> 5; /*bit[7:5]*/
309 	} else {
310 		boardtype = contents[EEPROM_RF_OPT4];
311 		boardtype &= BOARD_TYPE_TEST_MASK;
312 	}
313 	rtlefuse->board_type = boardtype;
314 	if (IS_HIGHT_PA(rtlefuse->board_type))
315 		rtlefuse->external_pa = 1;
316 	pr_info("Board Type %x\n", rtlefuse->board_type);
317 }
318 
319 static void _rtl92cu_read_adapter_info(struct ieee80211_hw *hw)
320 {
321 	struct rtl_priv *rtlpriv = rtl_priv(hw);
322 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
323 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
324 	int params[] = {RTL8190_EEPROM_ID, EEPROM_VID, EEPROM_DID,
325 			EEPROM_SVID, EEPROM_SMID, EEPROM_MAC_ADDR,
326 			EEPROM_CHANNELPLAN, EEPROM_VERSION, EEPROM_CUSTOMER_ID,
327 			0};
328 	u8 *hwinfo;
329 
330 	hwinfo = kzalloc(HWSET_MAX_SIZE, GFP_KERNEL);
331 	if (!hwinfo)
332 		return;
333 
334 	if (rtl_get_hwinfo(hw, rtlpriv, HWSET_MAX_SIZE, hwinfo, params))
335 		goto exit;
336 
337 	_rtl92cu_read_txpower_info_from_hwpg(hw,
338 					   rtlefuse->autoload_failflag, hwinfo);
339 	_rtl92cu_read_board_type(hw, hwinfo);
340 
341 	rtlefuse->txpwr_fromeprom = true;
342 	if (rtlhal->oem_id == RT_CID_DEFAULT) {
343 		switch (rtlefuse->eeprom_oemid) {
344 		case EEPROM_CID_DEFAULT:
345 			if (rtlefuse->eeprom_did == 0x8176) {
346 				if ((rtlefuse->eeprom_svid == 0x103C &&
347 				     rtlefuse->eeprom_smid == 0x1629))
348 					rtlhal->oem_id = RT_CID_819X_HP;
349 				else
350 					rtlhal->oem_id = RT_CID_DEFAULT;
351 			} else {
352 				rtlhal->oem_id = RT_CID_DEFAULT;
353 			}
354 			break;
355 		case EEPROM_CID_TOSHIBA:
356 			rtlhal->oem_id = RT_CID_TOSHIBA;
357 			break;
358 		case EEPROM_CID_QMI:
359 			rtlhal->oem_id = RT_CID_819X_QMI;
360 			break;
361 		case EEPROM_CID_WHQL:
362 		default:
363 			rtlhal->oem_id = RT_CID_DEFAULT;
364 			break;
365 		}
366 	}
367 exit:
368 	kfree(hwinfo);
369 }
370 
371 static void _rtl92cu_hal_customized_behavior(struct ieee80211_hw *hw)
372 {
373 	struct rtl_priv *rtlpriv = rtl_priv(hw);
374 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
375 
376 	switch (rtlhal->oem_id) {
377 	case RT_CID_819X_HP:
378 		rtlpriv->ledctl.led_opendrain = true;
379 		break;
380 	case RT_CID_819X_LENOVO:
381 	case RT_CID_DEFAULT:
382 	case RT_CID_TOSHIBA:
383 	case RT_CID_CCX:
384 	case RT_CID_819X_ACER:
385 	case RT_CID_WHQL:
386 	default:
387 		break;
388 	}
389 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "RT Customized ID: 0x%02X\n",
390 		 rtlhal->oem_id);
391 }
392 
393 void rtl92cu_read_eeprom_info(struct ieee80211_hw *hw)
394 {
395 
396 	struct rtl_priv *rtlpriv = rtl_priv(hw);
397 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
398 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
399 	u8 tmp_u1b;
400 
401 	if (!IS_NORMAL_CHIP(rtlhal->version))
402 		return;
403 	tmp_u1b = rtl_read_byte(rtlpriv, REG_9346CR);
404 	rtlefuse->epromtype = (tmp_u1b & BOOT_FROM_EEPROM) ?
405 			       EEPROM_93C46 : EEPROM_BOOT_EFUSE;
406 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "Boot from %s\n",
407 		 tmp_u1b & BOOT_FROM_EEPROM ? "EERROM" : "EFUSE");
408 	rtlefuse->autoload_failflag = (tmp_u1b & EEPROM_EN) ? false : true;
409 	RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "Autoload %s\n",
410 		 tmp_u1b & EEPROM_EN ? "OK!!" : "ERR!!");
411 	_rtl92cu_read_adapter_info(hw);
412 	_rtl92cu_hal_customized_behavior(hw);
413 	return;
414 }
415 
416 static int _rtl92cu_init_power_on(struct ieee80211_hw *hw)
417 {
418 	struct rtl_priv *rtlpriv = rtl_priv(hw);
419 	int		status = 0;
420 	u16		value16;
421 	u8		value8;
422 	/*  polling autoload done. */
423 	u32	pollingcount = 0;
424 
425 	do {
426 		if (rtl_read_byte(rtlpriv, REG_APS_FSMCO) & PFM_ALDN) {
427 			RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
428 				 "Autoload Done!\n");
429 			break;
430 		}
431 		if (pollingcount++ > 100) {
432 			pr_err("Failed to polling REG_APS_FSMCO[PFM_ALDN] done!\n");
433 			return -ENODEV;
434 		}
435 	} while (true);
436 	/* 0. RSV_CTRL 0x1C[7:0] = 0 unlock ISO/CLK/Power control register */
437 	rtl_write_byte(rtlpriv, REG_RSV_CTRL, 0x0);
438 	/* Power on when re-enter from IPS/Radio off/card disable */
439 	/* enable SPS into PWM mode */
440 	rtl_write_byte(rtlpriv, REG_SPS0_CTRL, 0x2b);
441 	udelay(100);
442 	value8 = rtl_read_byte(rtlpriv, REG_LDOV12D_CTRL);
443 	if (0 == (value8 & LDV12_EN)) {
444 		value8 |= LDV12_EN;
445 		rtl_write_byte(rtlpriv, REG_LDOV12D_CTRL, value8);
446 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
447 			 " power-on :REG_LDOV12D_CTRL Reg0x21:0x%02x\n",
448 			 value8);
449 		udelay(100);
450 		value8 = rtl_read_byte(rtlpriv, REG_SYS_ISO_CTRL);
451 		value8 &= ~ISO_MD2PP;
452 		rtl_write_byte(rtlpriv, REG_SYS_ISO_CTRL, value8);
453 	}
454 	/*  auto enable WLAN */
455 	pollingcount = 0;
456 	value16 = rtl_read_word(rtlpriv, REG_APS_FSMCO);
457 	value16 |= APFM_ONMAC;
458 	rtl_write_word(rtlpriv, REG_APS_FSMCO, value16);
459 	do {
460 		if (!(rtl_read_word(rtlpriv, REG_APS_FSMCO) & APFM_ONMAC)) {
461 			pr_info("MAC auto ON okay!\n");
462 			break;
463 		}
464 		if (pollingcount++ > 1000) {
465 			pr_err("Failed to polling REG_APS_FSMCO[APFM_ONMAC] done!\n");
466 			return -ENODEV;
467 		}
468 	} while (true);
469 	/* Enable Radio ,GPIO ,and LED function */
470 	rtl_write_word(rtlpriv, REG_APS_FSMCO, 0x0812);
471 	/* release RF digital isolation */
472 	value16 = rtl_read_word(rtlpriv, REG_SYS_ISO_CTRL);
473 	value16 &= ~ISO_DIOR;
474 	rtl_write_word(rtlpriv, REG_SYS_ISO_CTRL, value16);
475 	/* Reconsider when to do this operation after asking HWSD. */
476 	pollingcount = 0;
477 	rtl_write_byte(rtlpriv, REG_APSD_CTRL, (rtl_read_byte(rtlpriv,
478 						REG_APSD_CTRL) & ~BIT(6)));
479 	do {
480 		pollingcount++;
481 	} while ((pollingcount < 200) &&
482 		 (rtl_read_byte(rtlpriv, REG_APSD_CTRL) & BIT(7)));
483 	/* Enable MAC DMA/WMAC/SCHEDULE/SEC block */
484 	value16 = rtl_read_word(rtlpriv,  REG_CR);
485 	value16 |= (HCI_TXDMA_EN | HCI_RXDMA_EN | TXDMA_EN | RXDMA_EN |
486 		    PROTOCOL_EN | SCHEDULE_EN | MACTXEN | MACRXEN | ENSEC);
487 	rtl_write_word(rtlpriv, REG_CR, value16);
488 	return status;
489 }
490 
491 static void _rtl92cu_init_queue_reserved_page(struct ieee80211_hw *hw,
492 					      bool wmm_enable,
493 					      u8 out_ep_num,
494 					      u8 queue_sel)
495 {
496 	struct rtl_priv *rtlpriv = rtl_priv(hw);
497 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
498 	bool ischipn = IS_NORMAL_CHIP(rtlhal->version);
499 	u32 outepnum = (u32)out_ep_num;
500 	u32 numhq = 0;
501 	u32 numlq = 0;
502 	u32 numnq = 0;
503 	u32 numpubq;
504 	u32 value32;
505 	u8 value8;
506 	u32 txqpagenum, txqpageunit, txqremaininpage;
507 
508 	if (!wmm_enable) {
509 		numpubq = (ischipn) ? CHIP_B_PAGE_NUM_PUBQ :
510 			  CHIP_A_PAGE_NUM_PUBQ;
511 		txqpagenum = TX_TOTAL_PAGE_NUMBER - numpubq;
512 
513 		txqpageunit = txqpagenum / outepnum;
514 		txqremaininpage = txqpagenum % outepnum;
515 		if (queue_sel & TX_SELE_HQ)
516 			numhq = txqpageunit;
517 		if (queue_sel & TX_SELE_LQ)
518 			numlq = txqpageunit;
519 		/* HIGH priority queue always present in the configuration of
520 		 * 2 out-ep. Remainder pages have assigned to High queue */
521 		if (outepnum > 1 && txqremaininpage)
522 			numhq += txqremaininpage;
523 		/* NOTE: This step done before writting REG_RQPN. */
524 		if (ischipn) {
525 			if (queue_sel & TX_SELE_NQ)
526 				numnq = txqpageunit;
527 			value8 = (u8)_NPQ(numnq);
528 			rtl_write_byte(rtlpriv,  REG_RQPN_NPQ, value8);
529 		}
530 	} else {
531 		/* for WMM ,number of out-ep must more than or equal to 2! */
532 		numpubq = ischipn ? WMM_CHIP_B_PAGE_NUM_PUBQ :
533 			  WMM_CHIP_A_PAGE_NUM_PUBQ;
534 		if (queue_sel & TX_SELE_HQ) {
535 			numhq = ischipn ? WMM_CHIP_B_PAGE_NUM_HPQ :
536 				WMM_CHIP_A_PAGE_NUM_HPQ;
537 		}
538 		if (queue_sel & TX_SELE_LQ) {
539 			numlq = ischipn ? WMM_CHIP_B_PAGE_NUM_LPQ :
540 				WMM_CHIP_A_PAGE_NUM_LPQ;
541 		}
542 		/* NOTE: This step done before writting REG_RQPN. */
543 		if (ischipn) {
544 			if (queue_sel & TX_SELE_NQ)
545 				numnq = WMM_CHIP_B_PAGE_NUM_NPQ;
546 			value8 = (u8)_NPQ(numnq);
547 			rtl_write_byte(rtlpriv, REG_RQPN_NPQ, value8);
548 		}
549 	}
550 	/* TX DMA */
551 	value32 = _HPQ(numhq) | _LPQ(numlq) | _PUBQ(numpubq) | LD_RQPN;
552 	rtl_write_dword(rtlpriv, REG_RQPN, value32);
553 }
554 
555 static void _rtl92c_init_trx_buffer(struct ieee80211_hw *hw, bool wmm_enable)
556 {
557 	struct rtl_priv *rtlpriv = rtl_priv(hw);
558 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
559 	u8	txpktbuf_bndy;
560 	u8	value8;
561 
562 	if (!wmm_enable)
563 		txpktbuf_bndy = TX_PAGE_BOUNDARY;
564 	else /* for WMM */
565 		txpktbuf_bndy = (IS_NORMAL_CHIP(rtlhal->version))
566 						? WMM_CHIP_B_TX_PAGE_BOUNDARY
567 						: WMM_CHIP_A_TX_PAGE_BOUNDARY;
568 	rtl_write_byte(rtlpriv, REG_TXPKTBUF_BCNQ_BDNY, txpktbuf_bndy);
569 	rtl_write_byte(rtlpriv, REG_TXPKTBUF_MGQ_BDNY, txpktbuf_bndy);
570 	rtl_write_byte(rtlpriv, REG_TXPKTBUF_WMAC_LBK_BF_HD, txpktbuf_bndy);
571 	rtl_write_byte(rtlpriv, REG_TRXFF_BNDY, txpktbuf_bndy);
572 	rtl_write_byte(rtlpriv, REG_TDECTRL+1, txpktbuf_bndy);
573 	rtl_write_word(rtlpriv,  (REG_TRXFF_BNDY + 2), 0x27FF);
574 	value8 = _PSRX(RX_PAGE_SIZE_REG_VALUE) | _PSTX(PBP_128);
575 	rtl_write_byte(rtlpriv, REG_PBP, value8);
576 }
577 
578 static void _rtl92c_init_chipn_reg_priority(struct ieee80211_hw *hw, u16 beq,
579 					    u16 bkq, u16 viq, u16 voq,
580 					    u16 mgtq, u16 hiq)
581 {
582 	struct rtl_priv *rtlpriv = rtl_priv(hw);
583 	u16 value16 = (rtl_read_word(rtlpriv, REG_TRXDMA_CTRL) & 0x7);
584 
585 	value16 |= _TXDMA_BEQ_MAP(beq) | _TXDMA_BKQ_MAP(bkq) |
586 		   _TXDMA_VIQ_MAP(viq) | _TXDMA_VOQ_MAP(voq) |
587 		   _TXDMA_MGQ_MAP(mgtq) | _TXDMA_HIQ_MAP(hiq);
588 	rtl_write_word(rtlpriv,  REG_TRXDMA_CTRL, value16);
589 }
590 
591 static void _rtl92cu_init_chipn_one_out_ep_priority(struct ieee80211_hw *hw,
592 						    bool wmm_enable,
593 						    u8 queue_sel)
594 {
595 	u16 uninitialized_var(value);
596 
597 	switch (queue_sel) {
598 	case TX_SELE_HQ:
599 		value = QUEUE_HIGH;
600 		break;
601 	case TX_SELE_LQ:
602 		value = QUEUE_LOW;
603 		break;
604 	case TX_SELE_NQ:
605 		value = QUEUE_NORMAL;
606 		break;
607 	default:
608 		WARN_ON(1); /* Shall not reach here! */
609 		break;
610 	}
611 	_rtl92c_init_chipn_reg_priority(hw, value, value, value, value,
612 					value, value);
613 	pr_info("Tx queue select: 0x%02x\n", queue_sel);
614 }
615 
616 static void _rtl92cu_init_chipn_two_out_ep_priority(struct ieee80211_hw *hw,
617 						     bool wmm_enable,
618 						     u8 queue_sel)
619 {
620 	u16 beq, bkq, viq, voq, mgtq, hiq;
621 	u16 uninitialized_var(valuehi);
622 	u16 uninitialized_var(valuelow);
623 
624 	switch (queue_sel) {
625 	case (TX_SELE_HQ | TX_SELE_LQ):
626 		valuehi = QUEUE_HIGH;
627 		valuelow = QUEUE_LOW;
628 		break;
629 	case (TX_SELE_NQ | TX_SELE_LQ):
630 		valuehi = QUEUE_NORMAL;
631 		valuelow = QUEUE_LOW;
632 		break;
633 	case (TX_SELE_HQ | TX_SELE_NQ):
634 		valuehi = QUEUE_HIGH;
635 		valuelow = QUEUE_NORMAL;
636 		break;
637 	default:
638 		WARN_ON(1);
639 		break;
640 	}
641 	if (!wmm_enable) {
642 		beq = valuelow;
643 		bkq = valuelow;
644 		viq = valuehi;
645 		voq = valuehi;
646 		mgtq = valuehi;
647 		hiq = valuehi;
648 	} else {/* for WMM ,CONFIG_OUT_EP_WIFI_MODE */
649 		beq = valuehi;
650 		bkq = valuelow;
651 		viq = valuelow;
652 		voq = valuehi;
653 		mgtq = valuehi;
654 		hiq = valuehi;
655 	}
656 	_rtl92c_init_chipn_reg_priority(hw, beq, bkq, viq, voq, mgtq, hiq);
657 	pr_info("Tx queue select: 0x%02x\n", queue_sel);
658 }
659 
660 static void _rtl92cu_init_chipn_three_out_ep_priority(struct ieee80211_hw *hw,
661 						      bool wmm_enable,
662 						      u8 queue_sel)
663 {
664 	u16 beq, bkq, viq, voq, mgtq, hiq;
665 
666 	if (!wmm_enable) { /* typical setting */
667 		beq	= QUEUE_LOW;
668 		bkq	= QUEUE_LOW;
669 		viq	= QUEUE_NORMAL;
670 		voq	= QUEUE_HIGH;
671 		mgtq	= QUEUE_HIGH;
672 		hiq	= QUEUE_HIGH;
673 	} else { /* for WMM */
674 		beq	= QUEUE_LOW;
675 		bkq	= QUEUE_NORMAL;
676 		viq	= QUEUE_NORMAL;
677 		voq	= QUEUE_HIGH;
678 		mgtq	= QUEUE_HIGH;
679 		hiq	= QUEUE_HIGH;
680 	}
681 	_rtl92c_init_chipn_reg_priority(hw, beq, bkq, viq, voq, mgtq, hiq);
682 	pr_info("Tx queue select :0x%02x..\n", queue_sel);
683 }
684 
685 static void _rtl92cu_init_chipn_queue_priority(struct ieee80211_hw *hw,
686 					       bool wmm_enable,
687 					       u8 out_ep_num,
688 					       u8 queue_sel)
689 {
690 	switch (out_ep_num) {
691 	case 1:
692 		_rtl92cu_init_chipn_one_out_ep_priority(hw, wmm_enable,
693 							queue_sel);
694 		break;
695 	case 2:
696 		_rtl92cu_init_chipn_two_out_ep_priority(hw, wmm_enable,
697 							queue_sel);
698 		break;
699 	case 3:
700 		_rtl92cu_init_chipn_three_out_ep_priority(hw, wmm_enable,
701 							  queue_sel);
702 		break;
703 	default:
704 		WARN_ON(1); /* Shall not reach here! */
705 		break;
706 	}
707 }
708 
709 static void _rtl92cu_init_chipt_queue_priority(struct ieee80211_hw *hw,
710 					       bool wmm_enable,
711 					       u8 out_ep_num,
712 					       u8 queue_sel)
713 {
714 	u8 hq_sele = 0;
715 	struct rtl_priv *rtlpriv = rtl_priv(hw);
716 
717 	switch (out_ep_num) {
718 	case 2:	/* (TX_SELE_HQ|TX_SELE_LQ) */
719 		if (!wmm_enable) /* typical setting */
720 			hq_sele =  HQSEL_VOQ | HQSEL_VIQ | HQSEL_MGTQ |
721 				   HQSEL_HIQ;
722 		else	/* for WMM */
723 			hq_sele = HQSEL_VOQ | HQSEL_BEQ | HQSEL_MGTQ |
724 				  HQSEL_HIQ;
725 		break;
726 	case 1:
727 		if (TX_SELE_LQ == queue_sel) {
728 			/* map all endpoint to Low queue */
729 			hq_sele = 0;
730 		} else if (TX_SELE_HQ == queue_sel) {
731 			/* map all endpoint to High queue */
732 			hq_sele =  HQSEL_VOQ | HQSEL_VIQ | HQSEL_BEQ |
733 				   HQSEL_BKQ | HQSEL_MGTQ | HQSEL_HIQ;
734 		}
735 		break;
736 	default:
737 		WARN_ON(1); /* Shall not reach here! */
738 		break;
739 	}
740 	rtl_write_byte(rtlpriv, (REG_TRXDMA_CTRL+1), hq_sele);
741 	pr_info("Tx queue select :0x%02x..\n", hq_sele);
742 }
743 
744 static void _rtl92cu_init_queue_priority(struct ieee80211_hw *hw,
745 						bool wmm_enable,
746 						u8 out_ep_num,
747 						u8 queue_sel)
748 {
749 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
750 
751 	if (IS_NORMAL_CHIP(rtlhal->version))
752 		_rtl92cu_init_chipn_queue_priority(hw, wmm_enable, out_ep_num,
753 						   queue_sel);
754 	else
755 		_rtl92cu_init_chipt_queue_priority(hw, wmm_enable, out_ep_num,
756 						   queue_sel);
757 }
758 
759 static void _rtl92cu_init_wmac_setting(struct ieee80211_hw *hw)
760 {
761 	u16 value16;
762 	u32 value32;
763 	struct rtl_priv *rtlpriv = rtl_priv(hw);
764 
765 	value32 = (RCR_APM | RCR_AM | RCR_ADF | RCR_AB | RCR_APPFCS |
766 		   RCR_APP_ICV | RCR_AMF | RCR_HTC_LOC_CTRL |
767 		   RCR_APP_MIC | RCR_APP_PHYSTS | RCR_ACRC32);
768 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RCR, (u8 *)(&value32));
769 	/* Accept all multicast address */
770 	rtl_write_dword(rtlpriv,  REG_MAR, 0xFFFFFFFF);
771 	rtl_write_dword(rtlpriv,  REG_MAR + 4, 0xFFFFFFFF);
772 	/* Accept all management frames */
773 	value16 = 0xFFFF;
774 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_MGT_FILTER,
775 				      (u8 *)(&value16));
776 	/* Reject all control frame - default value is 0 */
777 	value16 = 0x0;
778 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_CTRL_FILTER,
779 				      (u8 *)(&value16));
780 	/* Accept all data frames */
781 	value16 = 0xFFFF;
782 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_DATA_FILTER,
783 				      (u8 *)(&value16));
784 }
785 
786 static void _rtl92cu_init_beacon_parameters(struct ieee80211_hw *hw)
787 {
788 	struct rtl_priv *rtlpriv = rtl_priv(hw);
789 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
790 
791 	rtl_write_word(rtlpriv, REG_BCN_CTRL, 0x1010);
792 
793 	/* TODO: Remove these magic number */
794 	rtl_write_word(rtlpriv, REG_TBTT_PROHIBIT, 0x6404);
795 	rtl_write_byte(rtlpriv, REG_DRVERLYINT, DRIVER_EARLY_INT_TIME);
796 	rtl_write_byte(rtlpriv, REG_BCNDMATIM, BCN_DMA_ATIME_INT_TIME);
797 	/* Change beacon AIFS to the largest number
798 	 * beacause test chip does not contension before sending beacon.
799 	 */
800 	if (IS_NORMAL_CHIP(rtlhal->version))
801 		rtl_write_word(rtlpriv, REG_BCNTCFG, 0x660F);
802 	else
803 		rtl_write_word(rtlpriv, REG_BCNTCFG, 0x66FF);
804 }
805 
806 static int _rtl92cu_init_mac(struct ieee80211_hw *hw)
807 {
808 	struct rtl_priv *rtlpriv = rtl_priv(hw);
809 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
810 	struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
811 	struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
812 	int err = 0;
813 	u32	boundary = 0;
814 	u8 wmm_enable = false; /* TODO */
815 	u8 out_ep_nums = rtlusb->out_ep_nums;
816 	u8 queue_sel = rtlusb->out_queue_sel;
817 
818 	err = _rtl92cu_init_power_on(hw);
819 
820 	if (err) {
821 		pr_err("Failed to init power on!\n");
822 		return err;
823 	}
824 	if (!wmm_enable) {
825 		boundary = TX_PAGE_BOUNDARY;
826 	} else { /* for WMM */
827 		boundary = (IS_NORMAL_CHIP(rtlhal->version))
828 					? WMM_CHIP_B_TX_PAGE_BOUNDARY
829 					: WMM_CHIP_A_TX_PAGE_BOUNDARY;
830 	}
831 	if (false == rtl92c_init_llt_table(hw, boundary)) {
832 		pr_err("Failed to init LLT Table!\n");
833 		return -EINVAL;
834 	}
835 	_rtl92cu_init_queue_reserved_page(hw, wmm_enable, out_ep_nums,
836 					  queue_sel);
837 	_rtl92c_init_trx_buffer(hw, wmm_enable);
838 	_rtl92cu_init_queue_priority(hw, wmm_enable, out_ep_nums,
839 				     queue_sel);
840 	/* Get Rx PHY status in order to report RSSI and others. */
841 	rtl92c_init_driver_info_size(hw, RTL92C_DRIVER_INFO_SIZE);
842 	rtl92c_init_interrupt(hw);
843 	rtl92c_init_network_type(hw);
844 	_rtl92cu_init_wmac_setting(hw);
845 	rtl92c_init_adaptive_ctrl(hw);
846 	rtl92c_init_edca(hw);
847 	rtl92c_init_rate_fallback(hw);
848 	rtl92c_init_retry_function(hw);
849 	rtlpriv->cfg->ops->set_bw_mode(hw, NL80211_CHAN_HT20);
850 	rtl92c_set_min_space(hw, IS_92C_SERIAL(rtlhal->version));
851 	_rtl92cu_init_beacon_parameters(hw);
852 	rtl92c_init_ampdu_aggregation(hw);
853 	rtl92c_init_beacon_max_error(hw);
854 	return err;
855 }
856 
857 void rtl92cu_enable_hw_security_config(struct ieee80211_hw *hw)
858 {
859 	struct rtl_priv *rtlpriv = rtl_priv(hw);
860 	u8 sec_reg_value = 0x0;
861 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
862 
863 	RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
864 		 "PairwiseEncAlgorithm = %d GroupEncAlgorithm = %d\n",
865 		 rtlpriv->sec.pairwise_enc_algorithm,
866 		 rtlpriv->sec.group_enc_algorithm);
867 	if (rtlpriv->cfg->mod_params->sw_crypto || rtlpriv->sec.use_sw_sec) {
868 		RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
869 			 "not open sw encryption\n");
870 		return;
871 	}
872 	sec_reg_value = SCR_TXENCENABLE | SCR_RXDECENABLE;
873 	if (rtlpriv->sec.use_defaultkey) {
874 		sec_reg_value |= SCR_TXUSEDK;
875 		sec_reg_value |= SCR_RXUSEDK;
876 	}
877 	if (IS_NORMAL_CHIP(rtlhal->version))
878 		sec_reg_value |= (SCR_RXBCUSEDK | SCR_TXBCUSEDK);
879 	rtl_write_byte(rtlpriv, REG_CR + 1, 0x02);
880 	RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD, "The SECR-value %x\n",
881 		 sec_reg_value);
882 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_WPA_CONFIG, &sec_reg_value);
883 }
884 
885 static void _rtl92cu_hw_configure(struct ieee80211_hw *hw)
886 {
887 	struct rtl_priv *rtlpriv = rtl_priv(hw);
888 	struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
889 
890 	/* To Fix MAC loopback mode fail. */
891 	rtl_write_byte(rtlpriv, REG_LDOHCI12_CTRL, 0x0f);
892 	rtl_write_byte(rtlpriv, 0x15, 0xe9);
893 	/* HW SEQ CTRL */
894 	/* set 0x0 to 0xFF by tynli. Default enable HW SEQ NUM. */
895 	rtl_write_byte(rtlpriv, REG_HWSEQ_CTRL, 0xFF);
896 	/* fixed USB interface interference issue */
897 	rtl_write_byte(rtlpriv, 0xfe40, 0xe0);
898 	rtl_write_byte(rtlpriv, 0xfe41, 0x8d);
899 	rtl_write_byte(rtlpriv, 0xfe42, 0x80);
900 	rtlusb->reg_bcn_ctrl_val = 0x18;
901 	rtl_write_byte(rtlpriv, REG_BCN_CTRL, (u8)rtlusb->reg_bcn_ctrl_val);
902 }
903 
904 static void _initpabias(struct ieee80211_hw *hw)
905 {
906 	struct rtl_priv *rtlpriv = rtl_priv(hw);
907 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
908 	u8 pa_setting;
909 
910 	/* FIXED PA current issue */
911 	pa_setting = efuse_read_1byte(hw, 0x1FA);
912 	if (!(pa_setting & BIT(0))) {
913 		rtl_set_rfreg(hw, RF90_PATH_A, 0x15, 0x0FFFFF, 0x0F406);
914 		rtl_set_rfreg(hw, RF90_PATH_A, 0x15, 0x0FFFFF, 0x4F406);
915 		rtl_set_rfreg(hw, RF90_PATH_A, 0x15, 0x0FFFFF, 0x8F406);
916 		rtl_set_rfreg(hw, RF90_PATH_A, 0x15, 0x0FFFFF, 0xCF406);
917 	}
918 	if (!(pa_setting & BIT(1)) && IS_NORMAL_CHIP(rtlhal->version) &&
919 	    IS_92C_SERIAL(rtlhal->version)) {
920 		rtl_set_rfreg(hw, RF90_PATH_B, 0x15, 0x0FFFFF, 0x0F406);
921 		rtl_set_rfreg(hw, RF90_PATH_B, 0x15, 0x0FFFFF, 0x4F406);
922 		rtl_set_rfreg(hw, RF90_PATH_B, 0x15, 0x0FFFFF, 0x8F406);
923 		rtl_set_rfreg(hw, RF90_PATH_B, 0x15, 0x0FFFFF, 0xCF406);
924 	}
925 	if (!(pa_setting & BIT(4))) {
926 		pa_setting = rtl_read_byte(rtlpriv, 0x16);
927 		pa_setting &= 0x0F;
928 		rtl_write_byte(rtlpriv, 0x16, pa_setting | 0x90);
929 	}
930 }
931 
932 int rtl92cu_hw_init(struct ieee80211_hw *hw)
933 {
934 	struct rtl_priv *rtlpriv = rtl_priv(hw);
935 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
936 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
937 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
938 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
939 	int err = 0;
940 	unsigned long flags;
941 
942 	/* As this function can take a very long time (up to 350 ms)
943 	 * and can be called with irqs disabled, reenable the irqs
944 	 * to let the other devices continue being serviced.
945 	 *
946 	 * It is safe doing so since our own interrupts will only be enabled
947 	 * in a subsequent step.
948 	 */
949 	local_save_flags(flags);
950 	local_irq_enable();
951 
952 	rtlhal->fw_ready = false;
953 	rtlhal->hw_type = HARDWARE_TYPE_RTL8192CU;
954 	err = _rtl92cu_init_mac(hw);
955 	if (err) {
956 		pr_err("init mac failed!\n");
957 		goto exit;
958 	}
959 	err = rtl92c_download_fw(hw);
960 	if (err) {
961 		RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
962 			 "Failed to download FW. Init HW without FW now..\n");
963 		err = 1;
964 		goto exit;
965 	}
966 
967 	rtlhal->fw_ready = true;
968 	rtlhal->last_hmeboxnum = 0; /* h2c */
969 	_rtl92cu_phy_param_tab_init(hw);
970 	rtl92cu_phy_mac_config(hw);
971 	rtl92cu_phy_bb_config(hw);
972 	rtlphy->rf_mode = RF_OP_BY_SW_3WIRE;
973 	rtl92c_phy_rf_config(hw);
974 	if (IS_VENDOR_UMC_A_CUT(rtlhal->version) &&
975 	    !IS_92C_SERIAL(rtlhal->version)) {
976 		rtl_set_rfreg(hw, RF90_PATH_A, RF_RX_G1, MASKDWORD, 0x30255);
977 		rtl_set_rfreg(hw, RF90_PATH_A, RF_RX_G2, MASKDWORD, 0x50a00);
978 	}
979 	rtlphy->rfreg_chnlval[0] = rtl_get_rfreg(hw, (enum radio_path)0,
980 						 RF_CHNLBW, RFREG_OFFSET_MASK);
981 	rtlphy->rfreg_chnlval[1] = rtl_get_rfreg(hw, (enum radio_path)1,
982 						 RF_CHNLBW, RFREG_OFFSET_MASK);
983 	rtl92cu_bb_block_on(hw);
984 	rtl_cam_reset_all_entry(hw);
985 	rtl92cu_enable_hw_security_config(hw);
986 	ppsc->rfpwr_state = ERFON;
987 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ETHER_ADDR, mac->mac_addr);
988 	if (ppsc->rfpwr_state == ERFON) {
989 		rtl92c_phy_set_rfpath_switch(hw, 1);
990 		if (rtlphy->iqk_initialized) {
991 			rtl92c_phy_iq_calibrate(hw, true);
992 		} else {
993 			rtl92c_phy_iq_calibrate(hw, false);
994 			rtlphy->iqk_initialized = true;
995 		}
996 		rtl92c_dm_check_txpower_tracking(hw);
997 		rtl92c_phy_lc_calibrate(hw);
998 	}
999 	_rtl92cu_hw_configure(hw);
1000 	_initpabias(hw);
1001 	rtl92c_dm_init(hw);
1002 exit:
1003 	local_irq_restore(flags);
1004 	return err;
1005 }
1006 
1007 static void disable_rfafeandresetbb(struct ieee80211_hw *hw)
1008 {
1009 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1010 /**************************************
1011 a.	TXPAUSE 0x522[7:0] = 0xFF	Pause MAC TX queue
1012 b.	RF path 0 offset 0x00 = 0x00	disable RF
1013 c.	APSD_CTRL 0x600[7:0] = 0x40
1014 d.	SYS_FUNC_EN 0x02[7:0] = 0x16	reset BB state machine
1015 e.	SYS_FUNC_EN 0x02[7:0] = 0x14	reset BB state machine
1016 ***************************************/
1017 	u8 erfpath = 0, value8 = 0;
1018 
1019 	rtl_write_byte(rtlpriv, REG_TXPAUSE, 0xFF);
1020 	rtl_set_rfreg(hw, (enum radio_path)erfpath, 0x0, MASKBYTE0, 0x0);
1021 
1022 	value8 |= APSDOFF;
1023 	rtl_write_byte(rtlpriv, REG_APSD_CTRL, value8); /*0x40*/
1024 	value8 = 0;
1025 	value8 |= (FEN_USBD | FEN_USBA | FEN_BB_GLB_RSTN);
1026 	rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, value8);/*0x16*/
1027 	value8 &= (~FEN_BB_GLB_RSTN);
1028 	rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, value8); /*0x14*/
1029 }
1030 
1031 static void  _resetdigitalprocedure1(struct ieee80211_hw *hw, bool withouthwsm)
1032 {
1033 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1034 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1035 
1036 	if (rtlhal->fw_version <=  0x20) {
1037 		/*****************************
1038 		f. MCUFWDL 0x80[7:0]=0		reset MCU ready status
1039 		g. SYS_FUNC_EN 0x02[10]= 0	reset MCU reg, (8051 reset)
1040 		h. SYS_FUNC_EN 0x02[15-12]= 5	reset MAC reg, DCORE
1041 		i. SYS_FUNC_EN 0x02[10]= 1	enable MCU reg, (8051 enable)
1042 		******************************/
1043 		u16 valu16 = 0;
1044 
1045 		rtl_write_byte(rtlpriv, REG_MCUFWDL, 0);
1046 		valu16 = rtl_read_word(rtlpriv, REG_SYS_FUNC_EN);
1047 		rtl_write_word(rtlpriv, REG_SYS_FUNC_EN, (valu16 &
1048 			       (~FEN_CPUEN))); /* reset MCU ,8051 */
1049 		valu16 = rtl_read_word(rtlpriv, REG_SYS_FUNC_EN)&0x0FFF;
1050 		rtl_write_word(rtlpriv, REG_SYS_FUNC_EN, (valu16 |
1051 			      (FEN_HWPDN|FEN_ELDR))); /* reset MAC */
1052 		valu16 = rtl_read_word(rtlpriv, REG_SYS_FUNC_EN);
1053 		rtl_write_word(rtlpriv, REG_SYS_FUNC_EN, (valu16 |
1054 			       FEN_CPUEN)); /* enable MCU ,8051 */
1055 	} else {
1056 		u8 retry_cnts = 0;
1057 
1058 		/* IF fw in RAM code, do reset */
1059 		if (rtl_read_byte(rtlpriv, REG_MCUFWDL) & BIT(1)) {
1060 			/* reset MCU ready status */
1061 			rtl_write_byte(rtlpriv, REG_MCUFWDL, 0);
1062 			/* 8051 reset by self */
1063 			rtl_write_byte(rtlpriv, REG_HMETFR+3, 0x20);
1064 			while ((retry_cnts++ < 100) &&
1065 			       (FEN_CPUEN & rtl_read_word(rtlpriv,
1066 			       REG_SYS_FUNC_EN))) {
1067 				udelay(50);
1068 			}
1069 			if (retry_cnts >= 100) {
1070 				pr_err("8051 reset failed!.........................\n");
1071 				/* if 8051 reset fail, reset MAC. */
1072 				rtl_write_byte(rtlpriv,
1073 					       REG_SYS_FUNC_EN + 1,
1074 					       0x50);
1075 				udelay(100);
1076 			}
1077 		}
1078 		/* Reset MAC and Enable 8051 */
1079 		rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN + 1, 0x54);
1080 		rtl_write_byte(rtlpriv, REG_MCUFWDL, 0);
1081 	}
1082 	if (withouthwsm) {
1083 		/*****************************
1084 		  Without HW auto state machine
1085 		g.SYS_CLKR 0x08[15:0] = 0x30A3		disable MAC clock
1086 		h.AFE_PLL_CTRL 0x28[7:0] = 0x80		disable AFE PLL
1087 		i.AFE_XTAL_CTRL 0x24[15:0] = 0x880F	gated AFE DIG_CLOCK
1088 		j.SYS_ISu_CTRL 0x00[7:0] = 0xF9		isolated digital to PON
1089 		******************************/
1090 		rtl_write_word(rtlpriv, REG_SYS_CLKR, 0x70A3);
1091 		rtl_write_byte(rtlpriv, REG_AFE_PLL_CTRL, 0x80);
1092 		rtl_write_word(rtlpriv, REG_AFE_XTAL_CTRL, 0x880F);
1093 		rtl_write_byte(rtlpriv, REG_SYS_ISO_CTRL, 0xF9);
1094 	}
1095 }
1096 
1097 static void _resetdigitalprocedure2(struct ieee80211_hw *hw)
1098 {
1099 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1100 /*****************************
1101 k. SYS_FUNC_EN 0x03[7:0] = 0x44		disable ELDR runction
1102 l. SYS_CLKR 0x08[15:0] = 0x3083		disable ELDR clock
1103 m. SYS_ISO_CTRL 0x01[7:0] = 0x83	isolated ELDR to PON
1104 ******************************/
1105 	rtl_write_word(rtlpriv, REG_SYS_CLKR, 0x70A3);
1106 	rtl_write_byte(rtlpriv, REG_SYS_ISO_CTRL+1, 0x82);
1107 }
1108 
1109 static void _disablegpio(struct ieee80211_hw *hw)
1110 {
1111 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1112 /***************************************
1113 j. GPIO_PIN_CTRL 0x44[31:0]=0x000
1114 k. Value = GPIO_PIN_CTRL[7:0]
1115 l.  GPIO_PIN_CTRL 0x44[31:0] = 0x00FF0000 | (value <<8); write ext PIN level
1116 m. GPIO_MUXCFG 0x42 [15:0] = 0x0780
1117 n. LEDCFG 0x4C[15:0] = 0x8080
1118 ***************************************/
1119 	u8	value8;
1120 	u16	value16;
1121 	u32	value32;
1122 
1123 	/* 1. Disable GPIO[7:0] */
1124 	rtl_write_word(rtlpriv, REG_GPIO_PIN_CTRL+2, 0x0000);
1125 	value32 = rtl_read_dword(rtlpriv, REG_GPIO_PIN_CTRL) & 0xFFFF00FF;
1126 	value8 = (u8)(value32&0x000000FF);
1127 	value32 |= ((value8<<8) | 0x00FF0000);
1128 	rtl_write_dword(rtlpriv, REG_GPIO_PIN_CTRL, value32);
1129 	/* 2. Disable GPIO[10:8] */
1130 	rtl_write_byte(rtlpriv, REG_GPIO_MUXCFG+3, 0x00);
1131 	value16 = rtl_read_word(rtlpriv, REG_GPIO_MUXCFG+2) & 0xFF0F;
1132 	value8 = (u8)(value16&0x000F);
1133 	value16 |= ((value8<<4) | 0x0780);
1134 	rtl_write_word(rtlpriv, REG_GPIO_PIN_CTRL+2, value16);
1135 	/* 3. Disable LED0 & 1 */
1136 	rtl_write_word(rtlpriv, REG_LEDCFG0, 0x8080);
1137 }
1138 
1139 static void disable_analog(struct ieee80211_hw *hw, bool withouthwsm)
1140 {
1141 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1142 	u16 value16 = 0;
1143 	u8 value8 = 0;
1144 
1145 	if (withouthwsm) {
1146 		/*****************************
1147 		n. LDOA15_CTRL 0x20[7:0] = 0x04	 disable A15 power
1148 		o. LDOV12D_CTRL 0x21[7:0] = 0x54 disable digital core power
1149 		r. When driver call disable, the ASIC will turn off remaining
1150 		   clock automatically
1151 		******************************/
1152 		rtl_write_byte(rtlpriv, REG_LDOA15_CTRL, 0x04);
1153 		value8 = rtl_read_byte(rtlpriv, REG_LDOV12D_CTRL);
1154 		value8 &= (~LDV12_EN);
1155 		rtl_write_byte(rtlpriv, REG_LDOV12D_CTRL, value8);
1156 	}
1157 
1158 /*****************************
1159 h. SPS0_CTRL 0x11[7:0] = 0x23		enter PFM mode
1160 i. APS_FSMCO 0x04[15:0] = 0x4802	set USB suspend
1161 ******************************/
1162 	rtl_write_byte(rtlpriv, REG_SPS0_CTRL, 0x23);
1163 	value16 |= (APDM_HOST | AFSM_HSUS | PFM_ALDN);
1164 	rtl_write_word(rtlpriv, REG_APS_FSMCO, (u16)value16);
1165 	rtl_write_byte(rtlpriv, REG_RSV_CTRL, 0x0E);
1166 }
1167 
1168 static void carddisable_hwsm(struct ieee80211_hw *hw)
1169 {
1170 	/* ==== RF Off Sequence ==== */
1171 	disable_rfafeandresetbb(hw);
1172 	/* ==== Reset digital sequence   ====== */
1173 	_resetdigitalprocedure1(hw, false);
1174 	/*  ==== Pull GPIO PIN to balance level and LED control ====== */
1175 	_disablegpio(hw);
1176 	/* ==== Disable analog sequence === */
1177 	disable_analog(hw, false);
1178 }
1179 
1180 static void carddisablewithout_hwsm(struct ieee80211_hw *hw)
1181 {
1182 	/*==== RF Off Sequence ==== */
1183 	disable_rfafeandresetbb(hw);
1184 	/*  ==== Reset digital sequence   ====== */
1185 	_resetdigitalprocedure1(hw, true);
1186 	/*  ==== Pull GPIO PIN to balance level and LED control ====== */
1187 	_disablegpio(hw);
1188 	/*  ==== Reset digital sequence   ====== */
1189 	_resetdigitalprocedure2(hw);
1190 	/*  ==== Disable analog sequence === */
1191 	disable_analog(hw, true);
1192 }
1193 
1194 static void _rtl92cu_set_bcn_ctrl_reg(struct ieee80211_hw *hw,
1195 				      u8 set_bits, u8 clear_bits)
1196 {
1197 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1198 	struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
1199 
1200 	rtlusb->reg_bcn_ctrl_val |= set_bits;
1201 	rtlusb->reg_bcn_ctrl_val &= ~clear_bits;
1202 	rtl_write_byte(rtlpriv, REG_BCN_CTRL, (u8)rtlusb->reg_bcn_ctrl_val);
1203 }
1204 
1205 static void _rtl92cu_stop_tx_beacon(struct ieee80211_hw *hw)
1206 {
1207 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1208 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
1209 	u8 tmp1byte = 0;
1210 
1211 	if (IS_NORMAL_CHIP(rtlhal->version)) {
1212 		tmp1byte = rtl_read_byte(rtlpriv, REG_FWHW_TXQ_CTRL + 2);
1213 		rtl_write_byte(rtlpriv, REG_FWHW_TXQ_CTRL + 2,
1214 			       tmp1byte & (~BIT(6)));
1215 		rtl_write_byte(rtlpriv, REG_TBTT_PROHIBIT + 1, 0x64);
1216 		tmp1byte = rtl_read_byte(rtlpriv, REG_TBTT_PROHIBIT + 2);
1217 		tmp1byte &= ~(BIT(0));
1218 		rtl_write_byte(rtlpriv, REG_TBTT_PROHIBIT + 2, tmp1byte);
1219 	} else {
1220 		rtl_write_byte(rtlpriv, REG_TXPAUSE,
1221 			       rtl_read_byte(rtlpriv, REG_TXPAUSE) | BIT(6));
1222 	}
1223 }
1224 
1225 static void _rtl92cu_resume_tx_beacon(struct ieee80211_hw *hw)
1226 {
1227 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1228 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
1229 	u8 tmp1byte = 0;
1230 
1231 	if (IS_NORMAL_CHIP(rtlhal->version)) {
1232 		tmp1byte = rtl_read_byte(rtlpriv, REG_FWHW_TXQ_CTRL + 2);
1233 		rtl_write_byte(rtlpriv, REG_FWHW_TXQ_CTRL + 2,
1234 			       tmp1byte | BIT(6));
1235 		rtl_write_byte(rtlpriv, REG_TBTT_PROHIBIT + 1, 0xff);
1236 		tmp1byte = rtl_read_byte(rtlpriv, REG_TBTT_PROHIBIT + 2);
1237 		tmp1byte |= BIT(0);
1238 		rtl_write_byte(rtlpriv, REG_TBTT_PROHIBIT + 2, tmp1byte);
1239 	} else {
1240 		rtl_write_byte(rtlpriv, REG_TXPAUSE,
1241 			       rtl_read_byte(rtlpriv, REG_TXPAUSE) & (~BIT(6)));
1242 	}
1243 }
1244 
1245 static void _rtl92cu_enable_bcn_sub_func(struct ieee80211_hw *hw)
1246 {
1247 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1248 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
1249 
1250 	if (IS_NORMAL_CHIP(rtlhal->version))
1251 		_rtl92cu_set_bcn_ctrl_reg(hw, 0, BIT(1));
1252 	else
1253 		_rtl92cu_set_bcn_ctrl_reg(hw, 0, BIT(4));
1254 }
1255 
1256 static void _rtl92cu_disable_bcn_sub_func(struct ieee80211_hw *hw)
1257 {
1258 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1259 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
1260 
1261 	if (IS_NORMAL_CHIP(rtlhal->version))
1262 		_rtl92cu_set_bcn_ctrl_reg(hw, BIT(1), 0);
1263 	else
1264 		_rtl92cu_set_bcn_ctrl_reg(hw, BIT(4), 0);
1265 }
1266 
1267 static int _rtl92cu_set_media_status(struct ieee80211_hw *hw,
1268 				     enum nl80211_iftype type)
1269 {
1270 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1271 	u8 bt_msr = rtl_read_byte(rtlpriv, MSR);
1272 	enum led_ctl_mode ledaction = LED_CTL_NO_LINK;
1273 
1274 	bt_msr &= 0xfc;
1275 	if (type == NL80211_IFTYPE_UNSPECIFIED || type ==
1276 	    NL80211_IFTYPE_STATION) {
1277 		_rtl92cu_stop_tx_beacon(hw);
1278 		_rtl92cu_enable_bcn_sub_func(hw);
1279 	} else if (type == NL80211_IFTYPE_ADHOC || type == NL80211_IFTYPE_AP) {
1280 		_rtl92cu_resume_tx_beacon(hw);
1281 		_rtl92cu_disable_bcn_sub_func(hw);
1282 	} else {
1283 		RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
1284 			 "Set HW_VAR_MEDIA_STATUS:No such media status(%x)\n",
1285 			 type);
1286 	}
1287 	switch (type) {
1288 	case NL80211_IFTYPE_UNSPECIFIED:
1289 		bt_msr |= MSR_NOLINK;
1290 		ledaction = LED_CTL_LINK;
1291 		RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1292 			 "Set Network type to NO LINK!\n");
1293 		break;
1294 	case NL80211_IFTYPE_ADHOC:
1295 		bt_msr |= MSR_ADHOC;
1296 		RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1297 			 "Set Network type to Ad Hoc!\n");
1298 		break;
1299 	case NL80211_IFTYPE_STATION:
1300 		bt_msr |= MSR_INFRA;
1301 		ledaction = LED_CTL_LINK;
1302 		RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1303 			 "Set Network type to STA!\n");
1304 		break;
1305 	case NL80211_IFTYPE_AP:
1306 		bt_msr |= MSR_AP;
1307 		RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1308 			 "Set Network type to AP!\n");
1309 		break;
1310 	default:
1311 		pr_err("Network type %d not supported!\n", type);
1312 		goto error_out;
1313 	}
1314 	rtl_write_byte(rtlpriv, MSR, bt_msr);
1315 	rtlpriv->cfg->ops->led_control(hw, ledaction);
1316 	if ((bt_msr & MSR_MASK) == MSR_AP)
1317 		rtl_write_byte(rtlpriv, REG_BCNTCFG + 1, 0x00);
1318 	else
1319 		rtl_write_byte(rtlpriv, REG_BCNTCFG + 1, 0x66);
1320 	return 0;
1321 error_out:
1322 	return 1;
1323 }
1324 
1325 void rtl92cu_card_disable(struct ieee80211_hw *hw)
1326 {
1327 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1328 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1329 	struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
1330 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1331 	enum nl80211_iftype opmode;
1332 
1333 	mac->link_state = MAC80211_NOLINK;
1334 	opmode = NL80211_IFTYPE_UNSPECIFIED;
1335 	_rtl92cu_set_media_status(hw, opmode);
1336 	rtlpriv->cfg->ops->led_control(hw, LED_CTL_POWER_OFF);
1337 	RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
1338 	if (rtlusb->disablehwsm)
1339 		carddisable_hwsm(hw);
1340 	else
1341 		carddisablewithout_hwsm(hw);
1342 
1343 	/* after power off we should do iqk again */
1344 	rtlpriv->phy.iqk_initialized = false;
1345 }
1346 
1347 void rtl92cu_set_check_bssid(struct ieee80211_hw *hw, bool check_bssid)
1348 {
1349 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1350 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
1351 	u32 reg_rcr;
1352 
1353 	if (rtlpriv->psc.rfpwr_state != ERFON)
1354 		return;
1355 
1356 	rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *)(&reg_rcr));
1357 
1358 	if (check_bssid) {
1359 		u8 tmp;
1360 
1361 		if (IS_NORMAL_CHIP(rtlhal->version)) {
1362 			reg_rcr |= (RCR_CBSSID_DATA | RCR_CBSSID_BCN);
1363 			tmp = BIT(4);
1364 		} else {
1365 			reg_rcr |= RCR_CBSSID;
1366 			tmp = BIT(4) | BIT(5);
1367 		}
1368 		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RCR,
1369 					      (u8 *) (&reg_rcr));
1370 		_rtl92cu_set_bcn_ctrl_reg(hw, 0, tmp);
1371 	} else {
1372 		u8 tmp;
1373 
1374 		if (IS_NORMAL_CHIP(rtlhal->version)) {
1375 			reg_rcr &= ~(RCR_CBSSID_DATA | RCR_CBSSID_BCN);
1376 			tmp = BIT(4);
1377 		} else {
1378 			reg_rcr &= ~RCR_CBSSID;
1379 			tmp = BIT(4) | BIT(5);
1380 		}
1381 		reg_rcr &= (~(RCR_CBSSID_DATA | RCR_CBSSID_BCN));
1382 		rtlpriv->cfg->ops->set_hw_reg(hw,
1383 					      HW_VAR_RCR, (u8 *) (&reg_rcr));
1384 		_rtl92cu_set_bcn_ctrl_reg(hw, tmp, 0);
1385 	}
1386 }
1387 
1388 /*========================================================================== */
1389 
1390 int rtl92cu_set_network_type(struct ieee80211_hw *hw, enum nl80211_iftype type)
1391 {
1392 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1393 
1394 	if (_rtl92cu_set_media_status(hw, type))
1395 		return -EOPNOTSUPP;
1396 
1397 	if (rtlpriv->mac80211.link_state == MAC80211_LINKED) {
1398 		if (type != NL80211_IFTYPE_AP)
1399 			rtl92cu_set_check_bssid(hw, true);
1400 	} else {
1401 		rtl92cu_set_check_bssid(hw, false);
1402 	}
1403 
1404 	return 0;
1405 }
1406 
1407 static void _beacon_function_enable(struct ieee80211_hw *hw)
1408 {
1409 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1410 
1411 	_rtl92cu_set_bcn_ctrl_reg(hw, (BIT(4) | BIT(3) | BIT(1)), 0x00);
1412 	rtl_write_byte(rtlpriv, REG_RD_CTRL+1, 0x6F);
1413 }
1414 
1415 void rtl92cu_set_beacon_related_registers(struct ieee80211_hw *hw)
1416 {
1417 
1418 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1419 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1420 	u16 bcn_interval, atim_window;
1421 	u32 value32;
1422 
1423 	bcn_interval = mac->beacon_interval;
1424 	atim_window = 2;	/*FIX MERGE */
1425 	rtl_write_word(rtlpriv, REG_ATIMWND, atim_window);
1426 	rtl_write_word(rtlpriv, REG_BCN_INTERVAL, bcn_interval);
1427 	_rtl92cu_init_beacon_parameters(hw);
1428 	rtl_write_byte(rtlpriv, REG_SLOT, 0x09);
1429 	/*
1430 	 * Force beacon frame transmission even after receiving beacon frame
1431 	 * from other ad hoc STA
1432 	 *
1433 	 *
1434 	 * Reset TSF Timer to zero, added by Roger. 2008.06.24
1435 	 */
1436 	value32 = rtl_read_dword(rtlpriv, REG_TCR);
1437 	value32 &= ~TSFRST;
1438 	rtl_write_dword(rtlpriv, REG_TCR, value32);
1439 	value32 |= TSFRST;
1440 	rtl_write_dword(rtlpriv, REG_TCR, value32);
1441 	RT_TRACE(rtlpriv, COMP_INIT|COMP_BEACON, DBG_LOUD,
1442 		 "SetBeaconRelatedRegisters8192CUsb(): Set TCR(%x)\n",
1443 		 value32);
1444 	/* TODO: Modify later (Find the right parameters)
1445 	 * NOTE: Fix test chip's bug (about contention windows's randomness) */
1446 	if ((mac->opmode == NL80211_IFTYPE_ADHOC) ||
1447 	    (mac->opmode == NL80211_IFTYPE_MESH_POINT) ||
1448 	    (mac->opmode == NL80211_IFTYPE_AP)) {
1449 		rtl_write_byte(rtlpriv, REG_RXTSF_OFFSET_CCK, 0x50);
1450 		rtl_write_byte(rtlpriv, REG_RXTSF_OFFSET_OFDM, 0x50);
1451 	}
1452 	_beacon_function_enable(hw);
1453 }
1454 
1455 void rtl92cu_set_beacon_interval(struct ieee80211_hw *hw)
1456 {
1457 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1458 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1459 	u16 bcn_interval = mac->beacon_interval;
1460 
1461 	RT_TRACE(rtlpriv, COMP_BEACON, DBG_DMESG, "beacon_interval:%d\n",
1462 		 bcn_interval);
1463 	rtl_write_word(rtlpriv, REG_BCN_INTERVAL, bcn_interval);
1464 }
1465 
1466 void rtl92cu_update_interrupt_mask(struct ieee80211_hw *hw,
1467 				   u32 add_msr, u32 rm_msr)
1468 {
1469 }
1470 
1471 void rtl92cu_get_hw_reg(struct ieee80211_hw *hw, u8 variable, u8 *val)
1472 {
1473 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1474 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1475 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1476 
1477 	switch (variable) {
1478 	case HW_VAR_RCR:
1479 		*((u32 *)(val)) = mac->rx_conf;
1480 		break;
1481 	case HW_VAR_RF_STATE:
1482 		*((enum rf_pwrstate *)(val)) = ppsc->rfpwr_state;
1483 		break;
1484 	case HW_VAR_FWLPS_RF_ON:{
1485 			enum rf_pwrstate rfstate;
1486 			u32 val_rcr;
1487 
1488 			rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RF_STATE,
1489 						      (u8 *)(&rfstate));
1490 			if (rfstate == ERFOFF) {
1491 				*((bool *) (val)) = true;
1492 			} else {
1493 				val_rcr = rtl_read_dword(rtlpriv, REG_RCR);
1494 				val_rcr &= 0x00070000;
1495 				if (val_rcr)
1496 					*((bool *) (val)) = false;
1497 				else
1498 					*((bool *) (val)) = true;
1499 			}
1500 			break;
1501 		}
1502 	case HW_VAR_FW_PSMODE_STATUS:
1503 		*((bool *) (val)) = ppsc->fw_current_inpsmode;
1504 		break;
1505 	case HW_VAR_CORRECT_TSF:{
1506 			u64 tsf;
1507 			u32 *ptsf_low = (u32 *)&tsf;
1508 			u32 *ptsf_high = ((u32 *)&tsf) + 1;
1509 
1510 			*ptsf_high = rtl_read_dword(rtlpriv, (REG_TSFTR + 4));
1511 			*ptsf_low = rtl_read_dword(rtlpriv, REG_TSFTR);
1512 			*((u64 *)(val)) = tsf;
1513 			break;
1514 		}
1515 	case HW_VAR_MGT_FILTER:
1516 		*((u16 *) (val)) = rtl_read_word(rtlpriv, REG_RXFLTMAP0);
1517 		break;
1518 	case HW_VAR_CTRL_FILTER:
1519 		*((u16 *) (val)) = rtl_read_word(rtlpriv, REG_RXFLTMAP1);
1520 		break;
1521 	case HW_VAR_DATA_FILTER:
1522 		*((u16 *) (val)) = rtl_read_word(rtlpriv, REG_RXFLTMAP2);
1523 		break;
1524 	case HAL_DEF_WOWLAN:
1525 		break;
1526 	default:
1527 		pr_err("switch case %#x not processed\n", variable);
1528 		break;
1529 	}
1530 }
1531 
1532 static bool usb_cmd_send_packet(struct ieee80211_hw *hw, struct sk_buff *skb)
1533 {
1534   /* Currently nothing happens here.
1535    * Traffic stops after some seconds in WPA2 802.11n mode.
1536    * Maybe because rtl8192cu chip should be set from here?
1537    * If I understand correctly, the realtek vendor driver sends some urbs
1538    * if its "here".
1539    *
1540    * This is maybe necessary:
1541    * rtlpriv->cfg->ops->fill_tx_cmddesc(hw, buffer, 1, 1, skb);
1542    */
1543 	dev_kfree_skb(skb);
1544 
1545 	return true;
1546 }
1547 
1548 void rtl92cu_set_hw_reg(struct ieee80211_hw *hw, u8 variable, u8 *val)
1549 {
1550 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1551 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1552 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1553 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1554 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1555 	enum wireless_mode wirelessmode = mac->mode;
1556 	u8 idx = 0;
1557 
1558 	switch (variable) {
1559 	case HW_VAR_ETHER_ADDR:{
1560 			for (idx = 0; idx < ETH_ALEN; idx++) {
1561 				rtl_write_byte(rtlpriv, (REG_MACID + idx),
1562 					       val[idx]);
1563 			}
1564 			break;
1565 		}
1566 	case HW_VAR_BASIC_RATE:{
1567 			u16 rate_cfg = ((u16 *) val)[0];
1568 			u8 rate_index = 0;
1569 
1570 			rate_cfg &= 0x15f;
1571 			/* TODO */
1572 			/* if (mac->current_network.vender == HT_IOT_PEER_CISCO
1573 			 *     && ((rate_cfg & 0x150) == 0)) {
1574 			 *	  rate_cfg |= 0x010;
1575 			 * } */
1576 			rate_cfg |= 0x01;
1577 			rtl_write_byte(rtlpriv, REG_RRSR, rate_cfg & 0xff);
1578 			rtl_write_byte(rtlpriv, REG_RRSR + 1,
1579 				       (rate_cfg >> 8) & 0xff);
1580 			while (rate_cfg > 0x1) {
1581 				rate_cfg >>= 1;
1582 				rate_index++;
1583 			}
1584 			rtl_write_byte(rtlpriv, REG_INIRTS_RATE_SEL,
1585 				       rate_index);
1586 			break;
1587 		}
1588 	case HW_VAR_BSSID:{
1589 			for (idx = 0; idx < ETH_ALEN; idx++) {
1590 				rtl_write_byte(rtlpriv, (REG_BSSID + idx),
1591 					       val[idx]);
1592 			}
1593 			break;
1594 		}
1595 	case HW_VAR_SIFS:{
1596 			rtl_write_byte(rtlpriv, REG_SIFS_CCK + 1, val[0]);
1597 			rtl_write_byte(rtlpriv, REG_SIFS_OFDM + 1, val[1]);
1598 			rtl_write_byte(rtlpriv, REG_SPEC_SIFS + 1, val[0]);
1599 			rtl_write_byte(rtlpriv, REG_MAC_SPEC_SIFS + 1, val[0]);
1600 			rtl_write_byte(rtlpriv, REG_R2T_SIFS+1, val[0]);
1601 			rtl_write_byte(rtlpriv, REG_T2T_SIFS+1, val[0]);
1602 			RT_TRACE(rtlpriv, COMP_MLME, DBG_LOUD, "HW_VAR_SIFS\n");
1603 			break;
1604 		}
1605 	case HW_VAR_SLOT_TIME:{
1606 			u8 e_aci;
1607 			u8 QOS_MODE = 1;
1608 
1609 			rtl_write_byte(rtlpriv, REG_SLOT, val[0]);
1610 			RT_TRACE(rtlpriv, COMP_MLME, DBG_LOUD,
1611 				 "HW_VAR_SLOT_TIME %x\n", val[0]);
1612 			if (QOS_MODE) {
1613 				for (e_aci = 0; e_aci < AC_MAX; e_aci++)
1614 					rtlpriv->cfg->ops->set_hw_reg(hw,
1615 								HW_VAR_AC_PARAM,
1616 								&e_aci);
1617 			} else {
1618 				u8 sifstime = 0;
1619 				u8	u1baifs;
1620 
1621 				if (IS_WIRELESS_MODE_A(wirelessmode) ||
1622 				    IS_WIRELESS_MODE_N_24G(wirelessmode) ||
1623 				    IS_WIRELESS_MODE_N_5G(wirelessmode))
1624 					sifstime = 16;
1625 				else
1626 					sifstime = 10;
1627 				u1baifs = sifstime + (2 *  val[0]);
1628 				rtl_write_byte(rtlpriv, REG_EDCA_VO_PARAM,
1629 					       u1baifs);
1630 				rtl_write_byte(rtlpriv, REG_EDCA_VI_PARAM,
1631 					       u1baifs);
1632 				rtl_write_byte(rtlpriv, REG_EDCA_BE_PARAM,
1633 					       u1baifs);
1634 				rtl_write_byte(rtlpriv, REG_EDCA_BK_PARAM,
1635 					       u1baifs);
1636 			}
1637 			break;
1638 		}
1639 	case HW_VAR_ACK_PREAMBLE:{
1640 			u8 reg_tmp;
1641 			u8 short_preamble = (bool)*val;
1642 
1643 			reg_tmp = 0;
1644 			if (short_preamble)
1645 				reg_tmp |= 0x80;
1646 			rtl_write_byte(rtlpriv, REG_RRSR + 2, reg_tmp);
1647 			break;
1648 		}
1649 	case HW_VAR_AMPDU_MIN_SPACE:{
1650 			u8 min_spacing_to_set;
1651 			u8 sec_min_space;
1652 
1653 			min_spacing_to_set = *val;
1654 			if (min_spacing_to_set <= 7) {
1655 				switch (rtlpriv->sec.pairwise_enc_algorithm) {
1656 				case NO_ENCRYPTION:
1657 				case AESCCMP_ENCRYPTION:
1658 					sec_min_space = 0;
1659 					break;
1660 				case WEP40_ENCRYPTION:
1661 				case WEP104_ENCRYPTION:
1662 				case TKIP_ENCRYPTION:
1663 					sec_min_space = 6;
1664 					break;
1665 				default:
1666 					sec_min_space = 7;
1667 					break;
1668 				}
1669 				if (min_spacing_to_set < sec_min_space)
1670 					min_spacing_to_set = sec_min_space;
1671 				mac->min_space_cfg = ((mac->min_space_cfg &
1672 						     0xf8) |
1673 						     min_spacing_to_set);
1674 				*val = min_spacing_to_set;
1675 				RT_TRACE(rtlpriv, COMP_MLME, DBG_LOUD,
1676 					 "Set HW_VAR_AMPDU_MIN_SPACE: %#x\n",
1677 					 mac->min_space_cfg);
1678 				rtl_write_byte(rtlpriv, REG_AMPDU_MIN_SPACE,
1679 					       mac->min_space_cfg);
1680 			}
1681 			break;
1682 		}
1683 	case HW_VAR_SHORTGI_DENSITY:{
1684 			u8 density_to_set;
1685 
1686 			density_to_set = *val;
1687 			density_to_set &= 0x1f;
1688 			mac->min_space_cfg &= 0x07;
1689 			mac->min_space_cfg |= (density_to_set << 3);
1690 			RT_TRACE(rtlpriv, COMP_MLME, DBG_LOUD,
1691 				 "Set HW_VAR_SHORTGI_DENSITY: %#x\n",
1692 				 mac->min_space_cfg);
1693 			rtl_write_byte(rtlpriv, REG_AMPDU_MIN_SPACE,
1694 				       mac->min_space_cfg);
1695 			break;
1696 		}
1697 	case HW_VAR_AMPDU_FACTOR:{
1698 			u8 regtoset_normal[4] = {0x41, 0xa8, 0x72, 0xb9};
1699 			u8 factor_toset;
1700 			u8 *p_regtoset = NULL;
1701 			u8 index = 0;
1702 
1703 			p_regtoset = regtoset_normal;
1704 			factor_toset = *val;
1705 			if (factor_toset <= 3) {
1706 				factor_toset = (1 << (factor_toset + 2));
1707 				if (factor_toset > 0xf)
1708 					factor_toset = 0xf;
1709 				for (index = 0; index < 4; index++) {
1710 					if ((p_regtoset[index] & 0xf0) >
1711 					    (factor_toset << 4))
1712 						p_regtoset[index] =
1713 						     (p_regtoset[index] & 0x0f)
1714 						     | (factor_toset << 4);
1715 					if ((p_regtoset[index] & 0x0f) >
1716 					     factor_toset)
1717 						p_regtoset[index] =
1718 						     (p_regtoset[index] & 0xf0)
1719 						     | (factor_toset);
1720 					rtl_write_byte(rtlpriv,
1721 						       (REG_AGGLEN_LMT + index),
1722 						       p_regtoset[index]);
1723 				}
1724 				RT_TRACE(rtlpriv, COMP_MLME, DBG_LOUD,
1725 					 "Set HW_VAR_AMPDU_FACTOR: %#x\n",
1726 					 factor_toset);
1727 			}
1728 			break;
1729 		}
1730 	case HW_VAR_AC_PARAM:{
1731 			u8 e_aci = *val;
1732 			u32 u4b_ac_param;
1733 			u16 cw_min = le16_to_cpu(mac->ac[e_aci].cw_min);
1734 			u16 cw_max = le16_to_cpu(mac->ac[e_aci].cw_max);
1735 			u16 tx_op = le16_to_cpu(mac->ac[e_aci].tx_op);
1736 
1737 			u4b_ac_param = (u32) mac->ac[e_aci].aifs;
1738 			u4b_ac_param |= (u32) ((cw_min & 0xF) <<
1739 					 AC_PARAM_ECW_MIN_OFFSET);
1740 			u4b_ac_param |= (u32) ((cw_max & 0xF) <<
1741 					 AC_PARAM_ECW_MAX_OFFSET);
1742 			u4b_ac_param |= (u32) tx_op << AC_PARAM_TXOP_OFFSET;
1743 			RT_TRACE(rtlpriv, COMP_MLME, DBG_LOUD,
1744 				 "queue:%x, ac_param:%x\n",
1745 				 e_aci, u4b_ac_param);
1746 			switch (e_aci) {
1747 			case AC1_BK:
1748 				rtl_write_dword(rtlpriv, REG_EDCA_BK_PARAM,
1749 						u4b_ac_param);
1750 				break;
1751 			case AC0_BE:
1752 				rtl_write_dword(rtlpriv, REG_EDCA_BE_PARAM,
1753 						u4b_ac_param);
1754 				break;
1755 			case AC2_VI:
1756 				rtl_write_dword(rtlpriv, REG_EDCA_VI_PARAM,
1757 						u4b_ac_param);
1758 				break;
1759 			case AC3_VO:
1760 				rtl_write_dword(rtlpriv, REG_EDCA_VO_PARAM,
1761 						u4b_ac_param);
1762 				break;
1763 			default:
1764 				WARN_ONCE(true, "rtl8192cu: invalid aci: %d !\n",
1765 					  e_aci);
1766 				break;
1767 			}
1768 			break;
1769 		}
1770 	case HW_VAR_RCR:{
1771 			rtl_write_dword(rtlpriv, REG_RCR, ((u32 *) (val))[0]);
1772 			mac->rx_conf = ((u32 *) (val))[0];
1773 			RT_TRACE(rtlpriv, COMP_RECV, DBG_DMESG,
1774 				 "### Set RCR(0x%08x) ###\n", mac->rx_conf);
1775 			break;
1776 		}
1777 	case HW_VAR_RETRY_LIMIT:{
1778 			u8 retry_limit = val[0];
1779 
1780 			rtl_write_word(rtlpriv, REG_RL,
1781 				       retry_limit << RETRY_LIMIT_SHORT_SHIFT |
1782 				       retry_limit << RETRY_LIMIT_LONG_SHIFT);
1783 			RT_TRACE(rtlpriv, COMP_MLME, DBG_DMESG,
1784 				 "Set HW_VAR_RETRY_LIMIT(0x%08x)\n",
1785 				 retry_limit);
1786 			break;
1787 		}
1788 	case HW_VAR_DUAL_TSF_RST:
1789 		rtl_write_byte(rtlpriv, REG_DUAL_TSF_RST, (BIT(0) | BIT(1)));
1790 		break;
1791 	case HW_VAR_EFUSE_BYTES:
1792 		rtlefuse->efuse_usedbytes = *((u16 *) val);
1793 		break;
1794 	case HW_VAR_EFUSE_USAGE:
1795 		rtlefuse->efuse_usedpercentage = *val;
1796 		break;
1797 	case HW_VAR_IO_CMD:
1798 		rtl92c_phy_set_io_cmd(hw, (*(enum io_type *)val));
1799 		break;
1800 	case HW_VAR_WPA_CONFIG:
1801 		rtl_write_byte(rtlpriv, REG_SECCFG, *val);
1802 		break;
1803 	case HW_VAR_SET_RPWM:{
1804 			u8 rpwm_val = rtl_read_byte(rtlpriv, REG_USB_HRPWM);
1805 
1806 			if (rpwm_val & BIT(7))
1807 				rtl_write_byte(rtlpriv, REG_USB_HRPWM, *val);
1808 			else
1809 				rtl_write_byte(rtlpriv, REG_USB_HRPWM,
1810 					       *val | BIT(7));
1811 			break;
1812 		}
1813 	case HW_VAR_H2C_FW_PWRMODE:{
1814 			u8 psmode = *val;
1815 
1816 			if ((psmode != FW_PS_ACTIVE_MODE) &&
1817 			   (!IS_92C_SERIAL(rtlhal->version)))
1818 				rtl92c_dm_rf_saving(hw, true);
1819 			rtl92c_set_fw_pwrmode_cmd(hw, (*val));
1820 			break;
1821 		}
1822 	case HW_VAR_FW_PSMODE_STATUS:
1823 		ppsc->fw_current_inpsmode = *((bool *) val);
1824 		break;
1825 	case HW_VAR_H2C_FW_JOINBSSRPT:{
1826 			u8 mstatus = *val;
1827 			u8 tmp_reg422;
1828 			bool recover = false;
1829 
1830 			if (mstatus == RT_MEDIA_CONNECT) {
1831 				rtlpriv->cfg->ops->set_hw_reg(hw,
1832 							 HW_VAR_AID, NULL);
1833 				rtl_write_byte(rtlpriv, REG_CR + 1, 0x03);
1834 				_rtl92cu_set_bcn_ctrl_reg(hw, 0, BIT(3));
1835 				_rtl92cu_set_bcn_ctrl_reg(hw, BIT(4), 0);
1836 				tmp_reg422 = rtl_read_byte(rtlpriv,
1837 							REG_FWHW_TXQ_CTRL + 2);
1838 				if (tmp_reg422 & BIT(6))
1839 					recover = true;
1840 				rtl_write_byte(rtlpriv, REG_FWHW_TXQ_CTRL + 2,
1841 					       tmp_reg422 & (~BIT(6)));
1842 				rtl92c_set_fw_rsvdpagepkt(hw,
1843 							  &usb_cmd_send_packet);
1844 				_rtl92cu_set_bcn_ctrl_reg(hw, BIT(3), 0);
1845 				_rtl92cu_set_bcn_ctrl_reg(hw, 0, BIT(4));
1846 				if (recover)
1847 					rtl_write_byte(rtlpriv,
1848 						 REG_FWHW_TXQ_CTRL + 2,
1849 						tmp_reg422 | BIT(6));
1850 				rtl_write_byte(rtlpriv, REG_CR + 1, 0x02);
1851 			}
1852 			rtl92c_set_fw_joinbss_report_cmd(hw, (*val));
1853 			break;
1854 		}
1855 	case HW_VAR_AID:{
1856 			u16 u2btmp;
1857 
1858 			u2btmp = rtl_read_word(rtlpriv, REG_BCN_PSR_RPT);
1859 			u2btmp &= 0xC000;
1860 			rtl_write_word(rtlpriv, REG_BCN_PSR_RPT,
1861 				       (u2btmp | mac->assoc_id));
1862 			break;
1863 		}
1864 	case HW_VAR_CORRECT_TSF:{
1865 			u8 btype_ibss = val[0];
1866 
1867 			if (btype_ibss)
1868 				_rtl92cu_stop_tx_beacon(hw);
1869 			_rtl92cu_set_bcn_ctrl_reg(hw, 0, BIT(3));
1870 			rtl_write_dword(rtlpriv, REG_TSFTR, (u32)(mac->tsf &
1871 					0xffffffff));
1872 			rtl_write_dword(rtlpriv, REG_TSFTR + 4,
1873 					(u32)((mac->tsf >> 32) & 0xffffffff));
1874 			_rtl92cu_set_bcn_ctrl_reg(hw, BIT(3), 0);
1875 			if (btype_ibss)
1876 				_rtl92cu_resume_tx_beacon(hw);
1877 			break;
1878 		}
1879 	case HW_VAR_MGT_FILTER:
1880 		rtl_write_word(rtlpriv, REG_RXFLTMAP0, *(u16 *)val);
1881 		mac->rx_mgt_filter = *(u16 *)val;
1882 		break;
1883 	case HW_VAR_CTRL_FILTER:
1884 		rtl_write_word(rtlpriv, REG_RXFLTMAP1, *(u16 *)val);
1885 		mac->rx_ctrl_filter = *(u16 *)val;
1886 		break;
1887 	case HW_VAR_DATA_FILTER:
1888 		rtl_write_word(rtlpriv, REG_RXFLTMAP2, *(u16 *)val);
1889 		mac->rx_data_filter = *(u16 *)val;
1890 		break;
1891 	case HW_VAR_KEEP_ALIVE:{
1892 			u8 array[2];
1893 
1894 			array[0] = 0xff;
1895 			array[1] = *((u8 *)val);
1896 			rtl92c_fill_h2c_cmd(hw, H2C_92C_KEEP_ALIVE_CTRL, 2,
1897 					    array);
1898 			break;
1899 		}
1900 	default:
1901 		pr_err("switch case %#x not processed\n", variable);
1902 		break;
1903 	}
1904 }
1905 
1906 static void rtl92cu_update_hal_rate_table(struct ieee80211_hw *hw,
1907 					  struct ieee80211_sta *sta)
1908 {
1909 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1910 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
1911 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1912 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1913 	u32 ratr_value;
1914 	u8 ratr_index = 0;
1915 	u8 nmode = mac->ht_enable;
1916 	u8 mimo_ps = IEEE80211_SMPS_OFF;
1917 	u16 shortgi_rate;
1918 	u32 tmp_ratr_value;
1919 	u8 curtxbw_40mhz = mac->bw_40;
1920 	u8 curshortgi_40mhz = (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40) ?
1921 			       1 : 0;
1922 	u8 curshortgi_20mhz = (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20) ?
1923 			       1 : 0;
1924 	enum wireless_mode wirelessmode = mac->mode;
1925 
1926 	if (rtlhal->current_bandtype == BAND_ON_5G)
1927 		ratr_value = sta->supp_rates[1] << 4;
1928 	else
1929 		ratr_value = sta->supp_rates[0];
1930 	if (mac->opmode == NL80211_IFTYPE_ADHOC)
1931 		ratr_value = 0xfff;
1932 
1933 	ratr_value |= (sta->ht_cap.mcs.rx_mask[1] << 20 |
1934 			sta->ht_cap.mcs.rx_mask[0] << 12);
1935 	switch (wirelessmode) {
1936 	case WIRELESS_MODE_B:
1937 		if (ratr_value & 0x0000000c)
1938 			ratr_value &= 0x0000000d;
1939 		else
1940 			ratr_value &= 0x0000000f;
1941 		break;
1942 	case WIRELESS_MODE_G:
1943 		ratr_value &= 0x00000FF5;
1944 		break;
1945 	case WIRELESS_MODE_N_24G:
1946 	case WIRELESS_MODE_N_5G:
1947 		nmode = 1;
1948 		if (mimo_ps == IEEE80211_SMPS_STATIC) {
1949 			ratr_value &= 0x0007F005;
1950 		} else {
1951 			u32 ratr_mask;
1952 
1953 			if (get_rf_type(rtlphy) == RF_1T2R ||
1954 			    get_rf_type(rtlphy) == RF_1T1R)
1955 				ratr_mask = 0x000ff005;
1956 			else
1957 				ratr_mask = 0x0f0ff005;
1958 
1959 			ratr_value &= ratr_mask;
1960 		}
1961 		break;
1962 	default:
1963 		if (rtlphy->rf_type == RF_1T2R)
1964 			ratr_value &= 0x000ff0ff;
1965 		else
1966 			ratr_value &= 0x0f0ff0ff;
1967 
1968 		break;
1969 	}
1970 
1971 	ratr_value &= 0x0FFFFFFF;
1972 
1973 	if (nmode && ((curtxbw_40mhz &&
1974 			 curshortgi_40mhz) || (!curtxbw_40mhz &&
1975 					       curshortgi_20mhz))) {
1976 		ratr_value |= 0x10000000;
1977 		tmp_ratr_value = (ratr_value >> 12);
1978 
1979 		for (shortgi_rate = 15; shortgi_rate > 0; shortgi_rate--) {
1980 			if ((1 << shortgi_rate) & tmp_ratr_value)
1981 				break;
1982 		}
1983 
1984 		shortgi_rate = (shortgi_rate << 12) | (shortgi_rate << 8) |
1985 		    (shortgi_rate << 4) | (shortgi_rate);
1986 	}
1987 
1988 	rtl_write_dword(rtlpriv, REG_ARFR0 + ratr_index * 4, ratr_value);
1989 
1990 	RT_TRACE(rtlpriv, COMP_RATR, DBG_DMESG, "%x\n",
1991 		 rtl_read_dword(rtlpriv, REG_ARFR0));
1992 }
1993 
1994 static void rtl92cu_update_hal_rate_mask(struct ieee80211_hw *hw,
1995 					 struct ieee80211_sta *sta,
1996 					 u8 rssi_level, bool update_bw)
1997 {
1998 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1999 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
2000 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
2001 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
2002 	struct rtl_sta_info *sta_entry = NULL;
2003 	u32 ratr_bitmap;
2004 	u8 ratr_index;
2005 	u8 curtxbw_40mhz = (sta->bandwidth >= IEEE80211_STA_RX_BW_40) ? 1 : 0;
2006 	u8 curshortgi_40mhz = curtxbw_40mhz &&
2007 			      (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40) ?
2008 				1 : 0;
2009 	u8 curshortgi_20mhz = (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20) ?
2010 				1 : 0;
2011 	enum wireless_mode wirelessmode = 0;
2012 	bool shortgi = false;
2013 	u8 rate_mask[5];
2014 	u8 macid = 0;
2015 	u8 mimo_ps = IEEE80211_SMPS_OFF;
2016 
2017 	sta_entry = (struct rtl_sta_info *) sta->drv_priv;
2018 	wirelessmode = sta_entry->wireless_mode;
2019 	if (mac->opmode == NL80211_IFTYPE_STATION ||
2020 	    mac->opmode == NL80211_IFTYPE_MESH_POINT)
2021 		curtxbw_40mhz = mac->bw_40;
2022 	else if (mac->opmode == NL80211_IFTYPE_AP ||
2023 		mac->opmode == NL80211_IFTYPE_ADHOC)
2024 		macid = sta->aid + 1;
2025 
2026 	if (rtlhal->current_bandtype == BAND_ON_5G)
2027 		ratr_bitmap = sta->supp_rates[1] << 4;
2028 	else
2029 		ratr_bitmap = sta->supp_rates[0];
2030 	if (mac->opmode == NL80211_IFTYPE_ADHOC)
2031 		ratr_bitmap = 0xfff;
2032 	ratr_bitmap |= (sta->ht_cap.mcs.rx_mask[1] << 20 |
2033 			sta->ht_cap.mcs.rx_mask[0] << 12);
2034 	switch (wirelessmode) {
2035 	case WIRELESS_MODE_B:
2036 		ratr_index = RATR_INX_WIRELESS_B;
2037 		if (ratr_bitmap & 0x0000000c)
2038 			ratr_bitmap &= 0x0000000d;
2039 		else
2040 			ratr_bitmap &= 0x0000000f;
2041 		break;
2042 	case WIRELESS_MODE_G:
2043 		ratr_index = RATR_INX_WIRELESS_GB;
2044 
2045 		if (rssi_level == 1)
2046 			ratr_bitmap &= 0x00000f00;
2047 		else if (rssi_level == 2)
2048 			ratr_bitmap &= 0x00000ff0;
2049 		else
2050 			ratr_bitmap &= 0x00000ff5;
2051 		break;
2052 	case WIRELESS_MODE_A:
2053 		ratr_index = RATR_INX_WIRELESS_A;
2054 		ratr_bitmap &= 0x00000ff0;
2055 		break;
2056 	case WIRELESS_MODE_N_24G:
2057 	case WIRELESS_MODE_N_5G:
2058 		ratr_index = RATR_INX_WIRELESS_NGB;
2059 
2060 		if (mimo_ps == IEEE80211_SMPS_STATIC) {
2061 			if (rssi_level == 1)
2062 				ratr_bitmap &= 0x00070000;
2063 			else if (rssi_level == 2)
2064 				ratr_bitmap &= 0x0007f000;
2065 			else
2066 				ratr_bitmap &= 0x0007f005;
2067 		} else {
2068 			if (rtlphy->rf_type == RF_1T2R ||
2069 			    rtlphy->rf_type == RF_1T1R) {
2070 				if (curtxbw_40mhz) {
2071 					if (rssi_level == 1)
2072 						ratr_bitmap &= 0x000f0000;
2073 					else if (rssi_level == 2)
2074 						ratr_bitmap &= 0x000ff000;
2075 					else
2076 						ratr_bitmap &= 0x000ff015;
2077 				} else {
2078 					if (rssi_level == 1)
2079 						ratr_bitmap &= 0x000f0000;
2080 					else if (rssi_level == 2)
2081 						ratr_bitmap &= 0x000ff000;
2082 					else
2083 						ratr_bitmap &= 0x000ff005;
2084 				}
2085 			} else {
2086 				if (curtxbw_40mhz) {
2087 					if (rssi_level == 1)
2088 						ratr_bitmap &= 0x0f0f0000;
2089 					else if (rssi_level == 2)
2090 						ratr_bitmap &= 0x0f0ff000;
2091 					else
2092 						ratr_bitmap &= 0x0f0ff015;
2093 				} else {
2094 					if (rssi_level == 1)
2095 						ratr_bitmap &= 0x0f0f0000;
2096 					else if (rssi_level == 2)
2097 						ratr_bitmap &= 0x0f0ff000;
2098 					else
2099 						ratr_bitmap &= 0x0f0ff005;
2100 				}
2101 			}
2102 		}
2103 
2104 		if ((curtxbw_40mhz && curshortgi_40mhz) ||
2105 		    (!curtxbw_40mhz && curshortgi_20mhz)) {
2106 
2107 			if (macid == 0)
2108 				shortgi = true;
2109 			else if (macid == 1)
2110 				shortgi = false;
2111 		}
2112 		break;
2113 	default:
2114 		ratr_index = RATR_INX_WIRELESS_NGB;
2115 
2116 		if (rtlphy->rf_type == RF_1T2R)
2117 			ratr_bitmap &= 0x000ff0ff;
2118 		else
2119 			ratr_bitmap &= 0x0f0ff0ff;
2120 		break;
2121 	}
2122 	sta_entry->ratr_index = ratr_index;
2123 
2124 	RT_TRACE(rtlpriv, COMP_RATR, DBG_DMESG,
2125 		 "ratr_bitmap :%x\n", ratr_bitmap);
2126 	*(u32 *)&rate_mask = (ratr_bitmap & 0x0fffffff) |
2127 				     (ratr_index << 28);
2128 	rate_mask[4] = macid | (shortgi ? 0x20 : 0x00) | 0x80;
2129 	RT_TRACE(rtlpriv, COMP_RATR, DBG_DMESG,
2130 		 "Rate_index:%x, ratr_val:%x, %5phC\n",
2131 		 ratr_index, ratr_bitmap, rate_mask);
2132 	memcpy(rtlpriv->rate_mask, rate_mask, 5);
2133 	/* rtl92c_fill_h2c_cmd() does USB I/O and will result in a
2134 	 * "scheduled while atomic" if called directly */
2135 	schedule_work(&rtlpriv->works.fill_h2c_cmd);
2136 
2137 	if (macid != 0)
2138 		sta_entry->ratr_index = ratr_index;
2139 }
2140 
2141 void rtl92cu_update_hal_rate_tbl(struct ieee80211_hw *hw,
2142 				 struct ieee80211_sta *sta,
2143 				 u8 rssi_level, bool update_bw)
2144 {
2145 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2146 
2147 	if (rtlpriv->dm.useramask)
2148 		rtl92cu_update_hal_rate_mask(hw, sta, rssi_level, update_bw);
2149 	else
2150 		rtl92cu_update_hal_rate_table(hw, sta);
2151 }
2152 
2153 void rtl92cu_update_channel_access_setting(struct ieee80211_hw *hw)
2154 {
2155 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2156 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
2157 	u16 sifs_timer;
2158 
2159 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SLOT_TIME,
2160 				      &mac->slot_time);
2161 	if (!mac->ht_enable)
2162 		sifs_timer = 0x0a0a;
2163 	else
2164 		sifs_timer = 0x0e0e;
2165 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SIFS, (u8 *)&sifs_timer);
2166 }
2167 
2168 bool rtl92cu_gpio_radio_on_off_checking(struct ieee80211_hw *hw, u8 * valid)
2169 {
2170 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2171 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
2172 	enum rf_pwrstate e_rfpowerstate_toset, cur_rfstate;
2173 	u8 u1tmp = 0;
2174 	bool actuallyset = false;
2175 	unsigned long flag = 0;
2176 	/* to do - usb autosuspend */
2177 	u8 usb_autosuspend = 0;
2178 
2179 	if (ppsc->swrf_processing)
2180 		return false;
2181 	spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flag);
2182 	if (ppsc->rfchange_inprogress) {
2183 		spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flag);
2184 		return false;
2185 	} else {
2186 		ppsc->rfchange_inprogress = true;
2187 		spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flag);
2188 	}
2189 	cur_rfstate = ppsc->rfpwr_state;
2190 	if (usb_autosuspend) {
2191 		/* to do................... */
2192 	} else {
2193 		if (ppsc->pwrdown_mode) {
2194 			u1tmp = rtl_read_byte(rtlpriv, REG_HSISR);
2195 			e_rfpowerstate_toset = (u1tmp & BIT(7)) ?
2196 					       ERFOFF : ERFON;
2197 			RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
2198 				 "pwrdown, 0x5c(BIT7)=%02x\n", u1tmp);
2199 		} else {
2200 			rtl_write_byte(rtlpriv, REG_MAC_PINMUX_CFG,
2201 				       rtl_read_byte(rtlpriv,
2202 				       REG_MAC_PINMUX_CFG) & ~(BIT(3)));
2203 			u1tmp = rtl_read_byte(rtlpriv, REG_GPIO_IO_SEL);
2204 			e_rfpowerstate_toset  = (u1tmp & BIT(3)) ?
2205 						 ERFON : ERFOFF;
2206 			RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
2207 				 "GPIO_IN=%02x\n", u1tmp);
2208 		}
2209 		RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD, "N-SS RF =%x\n",
2210 			 e_rfpowerstate_toset);
2211 	}
2212 	if ((ppsc->hwradiooff) && (e_rfpowerstate_toset == ERFON)) {
2213 		RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
2214 			 "GPIOChangeRF  - HW Radio ON, RF ON\n");
2215 		ppsc->hwradiooff = false;
2216 		actuallyset = true;
2217 	} else if ((!ppsc->hwradiooff) && (e_rfpowerstate_toset  ==
2218 		    ERFOFF)) {
2219 		RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
2220 			 "GPIOChangeRF  - HW Radio OFF\n");
2221 		ppsc->hwradiooff = true;
2222 		actuallyset = true;
2223 	} else {
2224 		RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
2225 			 "pHalData->bHwRadioOff and eRfPowerStateToSet do not match: pHalData->bHwRadioOff %x, eRfPowerStateToSet %x\n",
2226 			 ppsc->hwradiooff, e_rfpowerstate_toset);
2227 	}
2228 	if (actuallyset) {
2229 		ppsc->hwradiooff = true;
2230 		if (e_rfpowerstate_toset == ERFON) {
2231 			if ((ppsc->reg_rfps_level  & RT_RF_OFF_LEVL_ASPM) &&
2232 			     RT_IN_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_ASPM))
2233 				RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_ASPM);
2234 			else if ((ppsc->reg_rfps_level  & RT_RF_OFF_LEVL_PCI_D3)
2235 				 && RT_IN_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_PCI_D3))
2236 				RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_PCI_D3);
2237 		}
2238 		spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flag);
2239 		ppsc->rfchange_inprogress = false;
2240 		spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flag);
2241 		/* For power down module, we need to enable register block
2242 		 * contrl reg at 0x1c. Then enable power down control bit
2243 		 * of register 0x04 BIT4 and BIT15 as 1.
2244 		 */
2245 		if (ppsc->pwrdown_mode && e_rfpowerstate_toset == ERFOFF) {
2246 			/* Enable register area 0x0-0xc. */
2247 			rtl_write_byte(rtlpriv, REG_RSV_CTRL, 0x0);
2248 			rtl_write_word(rtlpriv, REG_APS_FSMCO, 0x8812);
2249 		}
2250 		if (e_rfpowerstate_toset == ERFOFF) {
2251 			if (ppsc->reg_rfps_level  & RT_RF_OFF_LEVL_ASPM)
2252 				RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_ASPM);
2253 			else if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_PCI_D3)
2254 				RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_PCI_D3);
2255 		}
2256 	} else if (e_rfpowerstate_toset == ERFOFF || cur_rfstate == ERFOFF) {
2257 		/* Enter D3 or ASPM after GPIO had been done. */
2258 		if (ppsc->reg_rfps_level  & RT_RF_OFF_LEVL_ASPM)
2259 			RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_ASPM);
2260 		else if (ppsc->reg_rfps_level  & RT_RF_OFF_LEVL_PCI_D3)
2261 			RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_PCI_D3);
2262 		spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flag);
2263 		ppsc->rfchange_inprogress = false;
2264 		spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flag);
2265 	} else {
2266 		spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flag);
2267 		ppsc->rfchange_inprogress = false;
2268 		spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flag);
2269 	}
2270 	*valid = 1;
2271 	return !ppsc->hwradiooff;
2272 }
2273