1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2009-2012  Realtek Corporation.*/
3 
4 #include "wifi.h"
5 #include "core.h"
6 #include "cam.h"
7 #include "base.h"
8 #include "ps.h"
9 #include "pwrseqcmd.h"
10 
11 #include "btcoexist/rtl_btc.h"
12 #include <linux/firmware.h>
13 #include <linux/export.h>
14 #include <net/cfg80211.h>
15 
16 u8 channel5g[CHANNEL_MAX_NUMBER_5G] = {
17 	36, 38, 40, 42, 44, 46, 48,		/* Band 1 */
18 	52, 54, 56, 58, 60, 62, 64,		/* Band 2 */
19 	100, 102, 104, 106, 108, 110, 112,	/* Band 3 */
20 	116, 118, 120, 122, 124, 126, 128,	/* Band 3 */
21 	132, 134, 136, 138, 140, 142, 144,	/* Band 3 */
22 	149, 151, 153, 155, 157, 159, 161,	/* Band 4 */
23 	165, 167, 169, 171, 173, 175, 177	/* Band 4 */
24 };
25 EXPORT_SYMBOL(channel5g);
26 
27 u8 channel5g_80m[CHANNEL_MAX_NUMBER_5G_80M] = {
28 	42, 58, 106, 122, 138, 155, 171
29 };
30 EXPORT_SYMBOL(channel5g_80m);
31 
32 void rtl_addr_delay(u32 addr)
33 {
34 	if (addr == 0xfe)
35 		mdelay(50);
36 	else if (addr == 0xfd)
37 		msleep(5);
38 	else if (addr == 0xfc)
39 		msleep(1);
40 	else if (addr == 0xfb)
41 		usleep_range(50, 100);
42 	else if (addr == 0xfa)
43 		usleep_range(5, 10);
44 	else if (addr == 0xf9)
45 		usleep_range(1, 2);
46 }
47 EXPORT_SYMBOL(rtl_addr_delay);
48 
49 void rtl_rfreg_delay(struct ieee80211_hw *hw, enum radio_path rfpath, u32 addr,
50 		     u32 mask, u32 data)
51 {
52 	if (addr >= 0xf9 && addr <= 0xfe) {
53 		rtl_addr_delay(addr);
54 	} else {
55 		rtl_set_rfreg(hw, rfpath, addr, mask, data);
56 		udelay(1);
57 	}
58 }
59 EXPORT_SYMBOL(rtl_rfreg_delay);
60 
61 static void rtl_fw_do_work(const struct firmware *firmware, void *context,
62 			   bool is_wow)
63 {
64 	struct ieee80211_hw *hw = context;
65 	struct rtl_priv *rtlpriv = rtl_priv(hw);
66 	int err;
67 
68 	rtl_dbg(rtlpriv, COMP_ERR, DBG_LOUD,
69 		"Firmware callback routine entered!\n");
70 	if (!firmware) {
71 		if (rtlpriv->cfg->alt_fw_name) {
72 			err = request_firmware(&firmware,
73 					       rtlpriv->cfg->alt_fw_name,
74 					       rtlpriv->io.dev);
75 			pr_info("Loading alternative firmware %s\n",
76 				rtlpriv->cfg->alt_fw_name);
77 			if (!err)
78 				goto found_alt;
79 		}
80 		pr_err("Selected firmware is not available\n");
81 		rtlpriv->max_fw_size = 0;
82 		goto exit;
83 	}
84 found_alt:
85 	if (firmware->size > rtlpriv->max_fw_size) {
86 		pr_err("Firmware is too big!\n");
87 		release_firmware(firmware);
88 		goto exit;
89 	}
90 	if (!is_wow) {
91 		memcpy(rtlpriv->rtlhal.pfirmware, firmware->data,
92 		       firmware->size);
93 		rtlpriv->rtlhal.fwsize = firmware->size;
94 	} else {
95 		memcpy(rtlpriv->rtlhal.wowlan_firmware, firmware->data,
96 		       firmware->size);
97 		rtlpriv->rtlhal.wowlan_fwsize = firmware->size;
98 	}
99 	release_firmware(firmware);
100 
101 exit:
102 	complete(&rtlpriv->firmware_loading_complete);
103 }
104 
105 void rtl_fw_cb(const struct firmware *firmware, void *context)
106 {
107 	rtl_fw_do_work(firmware, context, false);
108 }
109 EXPORT_SYMBOL(rtl_fw_cb);
110 
111 void rtl_wowlan_fw_cb(const struct firmware *firmware, void *context)
112 {
113 	rtl_fw_do_work(firmware, context, true);
114 }
115 EXPORT_SYMBOL(rtl_wowlan_fw_cb);
116 
117 /*mutex for start & stop is must here. */
118 static int rtl_op_start(struct ieee80211_hw *hw)
119 {
120 	int err = 0;
121 	struct rtl_priv *rtlpriv = rtl_priv(hw);
122 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
123 
124 	if (!is_hal_stop(rtlhal))
125 		return 0;
126 	if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
127 		return 0;
128 	mutex_lock(&rtlpriv->locks.conf_mutex);
129 	err = rtlpriv->intf_ops->adapter_start(hw);
130 	if (!err)
131 		rtl_watch_dog_timer_callback(&rtlpriv->works.watchdog_timer);
132 	mutex_unlock(&rtlpriv->locks.conf_mutex);
133 	return err;
134 }
135 
136 static void rtl_op_stop(struct ieee80211_hw *hw, bool suspend)
137 {
138 	struct rtl_priv *rtlpriv = rtl_priv(hw);
139 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
140 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
141 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
142 	bool support_remote_wakeup = false;
143 
144 	if (is_hal_stop(rtlhal))
145 		return;
146 
147 	rtlpriv->cfg->ops->get_hw_reg(hw, HAL_DEF_WOWLAN,
148 				      (u8 *)(&support_remote_wakeup));
149 	/* here is must, because adhoc do stop and start,
150 	 * but stop with RFOFF may cause something wrong,
151 	 * like adhoc TP
152 	 */
153 	if (unlikely(ppsc->rfpwr_state == ERFOFF))
154 		rtl_ips_nic_on(hw);
155 
156 	mutex_lock(&rtlpriv->locks.conf_mutex);
157 	/* if wowlan supported, DON'T clear connected info */
158 	if (!(support_remote_wakeup &&
159 	      rtlhal->enter_pnp_sleep)) {
160 		mac->link_state = MAC80211_NOLINK;
161 		eth_zero_addr(mac->bssid);
162 		mac->vendor = PEER_UNKNOWN;
163 
164 		/* reset sec info */
165 		rtl_cam_reset_sec_info(hw);
166 
167 		rtl_deinit_deferred_work(hw, false);
168 	}
169 	rtlpriv->intf_ops->adapter_stop(hw);
170 
171 	mutex_unlock(&rtlpriv->locks.conf_mutex);
172 }
173 
174 static void rtl_op_tx(struct ieee80211_hw *hw,
175 		      struct ieee80211_tx_control *control,
176 		      struct sk_buff *skb)
177 {
178 	struct rtl_priv *rtlpriv = rtl_priv(hw);
179 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
180 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
181 	struct rtl_tcb_desc tcb_desc;
182 
183 	memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
184 
185 	if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
186 		goto err_free;
187 
188 	if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
189 		goto err_free;
190 
191 	if (!rtlpriv->intf_ops->waitq_insert(hw, control->sta, skb))
192 		rtlpriv->intf_ops->adapter_tx(hw, control->sta, skb, &tcb_desc);
193 	return;
194 
195 err_free:
196 	dev_kfree_skb_any(skb);
197 }
198 
199 static int rtl_op_add_interface(struct ieee80211_hw *hw,
200 		struct ieee80211_vif *vif)
201 {
202 	struct rtl_priv *rtlpriv = rtl_priv(hw);
203 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
204 	int err = 0;
205 	u8 retry_limit = 0x30;
206 
207 	if (mac->vif) {
208 		rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
209 			"vif has been set!! mac->vif = 0x%p\n", mac->vif);
210 		return -EOPNOTSUPP;
211 	}
212 
213 	vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
214 
215 	rtl_ips_nic_on(hw);
216 
217 	mutex_lock(&rtlpriv->locks.conf_mutex);
218 	switch (ieee80211_vif_type_p2p(vif)) {
219 	case NL80211_IFTYPE_P2P_CLIENT:
220 		mac->p2p = P2P_ROLE_CLIENT;
221 		fallthrough;
222 	case NL80211_IFTYPE_STATION:
223 		if (mac->beacon_enabled == 1) {
224 			rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
225 				"NL80211_IFTYPE_STATION\n");
226 			mac->beacon_enabled = 0;
227 			rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
228 					rtlpriv->cfg->maps[RTL_IBSS_INT_MASKS]);
229 		}
230 		break;
231 	case NL80211_IFTYPE_ADHOC:
232 		rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
233 			"NL80211_IFTYPE_ADHOC\n");
234 
235 		mac->link_state = MAC80211_LINKED;
236 		rtlpriv->cfg->ops->set_bcn_reg(hw);
237 		if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
238 			mac->basic_rates = 0xfff;
239 		else
240 			mac->basic_rates = 0xff0;
241 		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
242 				(u8 *)(&mac->basic_rates));
243 
244 		retry_limit = 0x07;
245 		break;
246 	case NL80211_IFTYPE_P2P_GO:
247 		mac->p2p = P2P_ROLE_GO;
248 		fallthrough;
249 	case NL80211_IFTYPE_AP:
250 		rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
251 			"NL80211_IFTYPE_AP\n");
252 
253 		mac->link_state = MAC80211_LINKED;
254 		rtlpriv->cfg->ops->set_bcn_reg(hw);
255 		if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
256 			mac->basic_rates = 0xfff;
257 		else
258 			mac->basic_rates = 0xff0;
259 		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
260 					      (u8 *)(&mac->basic_rates));
261 
262 		retry_limit = 0x07;
263 		break;
264 	case NL80211_IFTYPE_MESH_POINT:
265 		rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
266 			"NL80211_IFTYPE_MESH_POINT\n");
267 
268 		mac->link_state = MAC80211_LINKED;
269 		rtlpriv->cfg->ops->set_bcn_reg(hw);
270 		if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
271 			mac->basic_rates = 0xfff;
272 		else
273 			mac->basic_rates = 0xff0;
274 		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
275 				(u8 *)(&mac->basic_rates));
276 
277 		retry_limit = 0x07;
278 		break;
279 	default:
280 		pr_err("operation mode %d is not supported!\n",
281 		       vif->type);
282 		err = -EOPNOTSUPP;
283 		goto out;
284 	}
285 
286 	if (mac->p2p) {
287 		rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
288 			"p2p role %x\n", vif->type);
289 		mac->basic_rates = 0xff0;/*disable cck rate for p2p*/
290 		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
291 				(u8 *)(&mac->basic_rates));
292 	}
293 	mac->vif = vif;
294 	mac->opmode = vif->type;
295 	rtlpriv->cfg->ops->set_network_type(hw, vif->type);
296 	memcpy(mac->mac_addr, vif->addr, ETH_ALEN);
297 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ETHER_ADDR, mac->mac_addr);
298 
299 	mac->retry_long = retry_limit;
300 	mac->retry_short = retry_limit;
301 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RETRY_LIMIT,
302 			(u8 *)(&retry_limit));
303 out:
304 	mutex_unlock(&rtlpriv->locks.conf_mutex);
305 	return err;
306 }
307 
308 static void rtl_op_remove_interface(struct ieee80211_hw *hw,
309 		struct ieee80211_vif *vif)
310 {
311 	struct rtl_priv *rtlpriv = rtl_priv(hw);
312 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
313 
314 	mutex_lock(&rtlpriv->locks.conf_mutex);
315 
316 	/* Free beacon resources */
317 	if (vif->type == NL80211_IFTYPE_AP ||
318 	    vif->type == NL80211_IFTYPE_ADHOC ||
319 	    vif->type == NL80211_IFTYPE_MESH_POINT) {
320 		if (mac->beacon_enabled == 1) {
321 			mac->beacon_enabled = 0;
322 			rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
323 					rtlpriv->cfg->maps[RTL_IBSS_INT_MASKS]);
324 		}
325 	}
326 
327 	/*
328 	 *Note: We assume NL80211_IFTYPE_UNSPECIFIED as
329 	 *NO LINK for our hardware.
330 	 */
331 	mac->p2p = 0;
332 	mac->vif = NULL;
333 	mac->link_state = MAC80211_NOLINK;
334 	eth_zero_addr(mac->bssid);
335 	mac->vendor = PEER_UNKNOWN;
336 	mac->opmode = NL80211_IFTYPE_UNSPECIFIED;
337 	rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
338 
339 	mutex_unlock(&rtlpriv->locks.conf_mutex);
340 }
341 
342 static int rtl_op_change_interface(struct ieee80211_hw *hw,
343 				   struct ieee80211_vif *vif,
344 				   enum nl80211_iftype new_type, bool p2p)
345 {
346 	struct rtl_priv *rtlpriv = rtl_priv(hw);
347 	int ret;
348 
349 	rtl_op_remove_interface(hw, vif);
350 
351 	vif->type = new_type;
352 	vif->p2p = p2p;
353 	ret = rtl_op_add_interface(hw, vif);
354 	rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
355 		"p2p  %x\n", p2p);
356 	return ret;
357 }
358 
359 #ifdef CONFIG_PM
360 static u16 crc16_ccitt(u8 data, u16 crc)
361 {
362 	u8 shift_in, data_bit, crc_bit11, crc_bit4, crc_bit15;
363 	u8 i;
364 	u16 result;
365 
366 	for (i = 0; i < 8; i++) {
367 		crc_bit15 = ((crc & BIT(15)) ? 1 : 0);
368 		data_bit  = (data & (BIT(0) << i) ? 1 : 0);
369 		shift_in = crc_bit15 ^ data_bit;
370 
371 		result = crc << 1;
372 		if (shift_in == 0)
373 			result &= (~BIT(0));
374 		else
375 			result |= BIT(0);
376 
377 		crc_bit11 = ((crc & BIT(11)) ? 1 : 0) ^ shift_in;
378 		if (crc_bit11 == 0)
379 			result &= (~BIT(12));
380 		else
381 			result |= BIT(12);
382 
383 		crc_bit4 = ((crc & BIT(4)) ? 1 : 0) ^ shift_in;
384 		if (crc_bit4 == 0)
385 			result &= (~BIT(5));
386 		else
387 			result |= BIT(5);
388 
389 		crc = result;
390 	}
391 
392 	return crc;
393 }
394 
395 static u16 _calculate_wol_pattern_crc(u8 *pattern, u16 len)
396 {
397 	u16 crc = 0xffff;
398 	u32 i;
399 
400 	for (i = 0; i < len; i++)
401 		crc = crc16_ccitt(pattern[i], crc);
402 
403 	crc = ~crc;
404 
405 	return crc;
406 }
407 
408 static void _rtl_add_wowlan_patterns(struct ieee80211_hw *hw,
409 				     struct cfg80211_wowlan *wow)
410 {
411 	struct rtl_priv *rtlpriv = rtl_priv(hw);
412 	struct rtl_mac *mac = &rtlpriv->mac80211;
413 	struct cfg80211_pkt_pattern *patterns = wow->patterns;
414 	struct rtl_wow_pattern rtl_pattern;
415 	const u8 *pattern_os, *mask_os;
416 	u8 mask[MAX_WOL_BIT_MASK_SIZE] = {0};
417 	u8 content[MAX_WOL_PATTERN_SIZE] = {0};
418 	u8 broadcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
419 	u8 multicast_addr1[2] = {0x33, 0x33};
420 	u8 multicast_addr2[3] = {0x01, 0x00, 0x5e};
421 	u8 i, mask_len;
422 	u16 j, len;
423 
424 	for (i = 0; i < wow->n_patterns; i++) {
425 		memset(&rtl_pattern, 0, sizeof(struct rtl_wow_pattern));
426 		memset(mask, 0, MAX_WOL_BIT_MASK_SIZE);
427 		if (patterns[i].pattern_len < 0 ||
428 		    patterns[i].pattern_len > MAX_WOL_PATTERN_SIZE) {
429 			rtl_dbg(rtlpriv, COMP_POWER, DBG_WARNING,
430 				"Pattern[%d] is too long\n", i);
431 			continue;
432 		}
433 		pattern_os = patterns[i].pattern;
434 		mask_len = DIV_ROUND_UP(patterns[i].pattern_len, 8);
435 		mask_os = patterns[i].mask;
436 		RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
437 			      "pattern content\n", pattern_os,
438 			       patterns[i].pattern_len);
439 		RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
440 			      "mask content\n", mask_os, mask_len);
441 		/* 1. unicast? multicast? or broadcast? */
442 		if (memcmp(pattern_os, broadcast_addr, 6) == 0)
443 			rtl_pattern.type = BROADCAST_PATTERN;
444 		else if (memcmp(pattern_os, multicast_addr1, 2) == 0 ||
445 			 memcmp(pattern_os, multicast_addr2, 3) == 0)
446 			rtl_pattern.type = MULTICAST_PATTERN;
447 		else if  (memcmp(pattern_os, mac->mac_addr, 6) == 0)
448 			rtl_pattern.type = UNICAST_PATTERN;
449 		else
450 			rtl_pattern.type = UNKNOWN_TYPE;
451 
452 		/* 2. translate mask_from_os to mask_for_hw */
453 
454 /******************************************************************************
455  * pattern from OS uses 'ethenet frame', like this:
456 
457 		   |    6   |    6   |   2  |     20    |  Variable  |	4  |
458 		   |--------+--------+------+-----------+------------+-----|
459 		   |    802.3 Mac Header    | IP Header | TCP Packet | FCS |
460 		   |   DA   |   SA   | Type |
461 
462  * BUT, packet catched by our HW is in '802.11 frame', begin from LLC,
463 
464 	|     24 or 30      |    6   |   2  |     20    |  Variable  |  4  |
465 	|-------------------+--------+------+-----------+------------+-----|
466 	| 802.11 MAC Header |       LLC     | IP Header | TCP Packet | FCS |
467 			    | Others | Tpye |
468 
469  * Therefore, we need translate mask_from_OS to mask_to_hw.
470  * We should left-shift mask by 6 bits, then set the new bit[0~5] = 0,
471  * because new mask[0~5] means 'SA', but our HW packet begins from LLC,
472  * bit[0~5] corresponds to first 6 Bytes in LLC, they just don't match.
473  ******************************************************************************/
474 
475 		/* Shift 6 bits */
476 		for (j = 0; j < mask_len - 1; j++) {
477 			mask[j] = mask_os[j] >> 6;
478 			mask[j] |= (mask_os[j + 1] & 0x3F) << 2;
479 		}
480 		mask[j] = (mask_os[j] >> 6) & 0x3F;
481 		/* Set bit 0-5 to zero */
482 		mask[0] &= 0xC0;
483 
484 		RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
485 			      "mask to hw\n", mask, mask_len);
486 		for (j = 0; j < (MAX_WOL_BIT_MASK_SIZE + 1) / 4; j++) {
487 			rtl_pattern.mask[j] = mask[j * 4];
488 			rtl_pattern.mask[j] |= (mask[j * 4 + 1] << 8);
489 			rtl_pattern.mask[j] |= (mask[j * 4 + 2] << 16);
490 			rtl_pattern.mask[j] |= (mask[j * 4 + 3] << 24);
491 		}
492 
493 		/* To get the wake up pattern from the mask.
494 		 * We do not count first 12 bits which means
495 		 * DA[6] and SA[6] in the pattern to match HW design.
496 		 */
497 		len = 0;
498 		for (j = 12; j < patterns[i].pattern_len; j++) {
499 			if ((mask_os[j / 8] >> (j % 8)) & 0x01) {
500 				content[len] = pattern_os[j];
501 				len++;
502 			}
503 		}
504 
505 		RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
506 			      "pattern to hw\n", content, len);
507 		/* 3. calculate crc */
508 		rtl_pattern.crc = _calculate_wol_pattern_crc(content, len);
509 		rtl_dbg(rtlpriv, COMP_POWER, DBG_TRACE,
510 			"CRC_Remainder = 0x%x\n", rtl_pattern.crc);
511 
512 		/* 4. write crc & mask_for_hw to hw */
513 		rtlpriv->cfg->ops->add_wowlan_pattern(hw, &rtl_pattern, i);
514 	}
515 	rtl_write_byte(rtlpriv, 0x698, wow->n_patterns);
516 }
517 
518 static int rtl_op_suspend(struct ieee80211_hw *hw,
519 			  struct cfg80211_wowlan *wow)
520 {
521 	struct rtl_priv *rtlpriv = rtl_priv(hw);
522 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
523 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
524 
525 	rtl_dbg(rtlpriv, COMP_POWER, DBG_DMESG, "\n");
526 	if (WARN_ON(!wow))
527 		return -EINVAL;
528 
529 	/* to resolve s4 can not wake up*/
530 	rtlhal->last_suspend_sec = ktime_get_real_seconds();
531 
532 	if ((ppsc->wo_wlan_mode & WAKE_ON_PATTERN_MATCH) && wow->n_patterns)
533 		_rtl_add_wowlan_patterns(hw, wow);
534 
535 	rtlhal->driver_is_goingto_unload = true;
536 	rtlhal->enter_pnp_sleep = true;
537 
538 	rtl_lps_leave(hw, true);
539 	rtl_op_stop(hw, false);
540 	device_set_wakeup_enable(wiphy_dev(hw->wiphy), true);
541 	return 0;
542 }
543 
544 static int rtl_op_resume(struct ieee80211_hw *hw)
545 {
546 	struct rtl_priv *rtlpriv = rtl_priv(hw);
547 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
548 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
549 	time64_t now;
550 
551 	rtl_dbg(rtlpriv, COMP_POWER, DBG_DMESG, "\n");
552 	rtlhal->driver_is_goingto_unload = false;
553 	rtlhal->enter_pnp_sleep = false;
554 	rtlhal->wake_from_pnp_sleep = true;
555 
556 	/* to resolve s4 can not wake up*/
557 	now = ktime_get_real_seconds();
558 	if (now - rtlhal->last_suspend_sec < 5)
559 		return -1;
560 
561 	rtl_op_start(hw);
562 	device_set_wakeup_enable(wiphy_dev(hw->wiphy), false);
563 	ieee80211_resume_disconnect(mac->vif);
564 	rtlhal->wake_from_pnp_sleep = false;
565 	return 0;
566 }
567 #endif
568 
569 static int rtl_op_config(struct ieee80211_hw *hw, u32 changed)
570 {
571 	struct rtl_priv *rtlpriv = rtl_priv(hw);
572 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
573 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
574 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
575 	struct ieee80211_conf *conf = &hw->conf;
576 
577 	if (mac->skip_scan)
578 		return 1;
579 
580 	mutex_lock(&rtlpriv->locks.conf_mutex);
581 	if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {	/* BIT(2)*/
582 		rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
583 			"IEEE80211_CONF_CHANGE_LISTEN_INTERVAL\n");
584 	}
585 
586 	/*For IPS */
587 	if (changed & IEEE80211_CONF_CHANGE_IDLE) {
588 		if (hw->conf.flags & IEEE80211_CONF_IDLE)
589 			rtl_ips_nic_off(hw);
590 		else
591 			rtl_ips_nic_on(hw);
592 	} else {
593 		/*
594 		 *although rfoff may not cause by ips, but we will
595 		 *check the reason in set_rf_power_state function
596 		 */
597 		if (unlikely(ppsc->rfpwr_state == ERFOFF))
598 			rtl_ips_nic_on(hw);
599 	}
600 
601 	/*For LPS */
602 	if ((changed & IEEE80211_CONF_CHANGE_PS) &&
603 	    rtlpriv->psc.swctrl_lps && !rtlpriv->psc.fwctrl_lps) {
604 		cancel_delayed_work(&rtlpriv->works.ps_work);
605 		cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
606 		if (conf->flags & IEEE80211_CONF_PS) {
607 			rtlpriv->psc.sw_ps_enabled = true;
608 			/* sleep here is must, or we may recv the beacon and
609 			 * cause mac80211 into wrong ps state, this will cause
610 			 * power save nullfunc send fail, and further cause
611 			 * pkt loss, So sleep must quickly but not immediatly
612 			 * because that will cause nullfunc send by mac80211
613 			 * fail, and cause pkt loss, we have tested that 5mA
614 			 * is worked very well */
615 			if (!rtlpriv->psc.multi_buffered)
616 				queue_delayed_work(rtlpriv->works.rtl_wq,
617 						   &rtlpriv->works.ps_work,
618 						   MSECS(5));
619 		} else {
620 			rtl_swlps_rf_awake(hw);
621 			rtlpriv->psc.sw_ps_enabled = false;
622 		}
623 	}
624 
625 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL &&
626 	    !rtlpriv->proximity.proxim_on) {
627 		struct ieee80211_channel *channel = hw->conf.chandef.chan;
628 		enum nl80211_chan_width width = hw->conf.chandef.width;
629 		enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
630 		u8 wide_chan = (u8) channel->hw_value;
631 
632 		/* channel_type is for 20&40M */
633 		if (width < NL80211_CHAN_WIDTH_80)
634 			channel_type =
635 				cfg80211_get_chandef_type(&hw->conf.chandef);
636 		if (mac->act_scanning)
637 			mac->n_channels++;
638 
639 		/*
640 		 *because we should back channel to
641 		 *current_network.chan in scanning,
642 		 *So if set_chan == current_network.chan
643 		 *we should set it.
644 		 *because mac80211 tell us wrong bw40
645 		 *info for cisco1253 bw20, so we modify
646 		 *it here based on UPPER & LOWER
647 		 */
648 
649 		if (width >= NL80211_CHAN_WIDTH_80) {
650 			if (width == NL80211_CHAN_WIDTH_80) {
651 				u32 center = hw->conf.chandef.center_freq1;
652 				u32 primary =
653 				(u32)hw->conf.chandef.chan->center_freq;
654 
655 				rtlphy->current_chan_bw =
656 					HT_CHANNEL_WIDTH_80;
657 				mac->bw_80 = true;
658 				mac->bw_40 = true;
659 				if (center > primary) {
660 					mac->cur_80_prime_sc =
661 					PRIME_CHNL_OFFSET_LOWER;
662 					if (center - primary == 10) {
663 						mac->cur_40_prime_sc =
664 						PRIME_CHNL_OFFSET_UPPER;
665 
666 						wide_chan += 2;
667 					} else if (center - primary == 30) {
668 						mac->cur_40_prime_sc =
669 						PRIME_CHNL_OFFSET_LOWER;
670 
671 						wide_chan += 6;
672 					}
673 				} else {
674 					mac->cur_80_prime_sc =
675 					PRIME_CHNL_OFFSET_UPPER;
676 					if (primary - center == 10) {
677 						mac->cur_40_prime_sc =
678 						PRIME_CHNL_OFFSET_LOWER;
679 
680 						wide_chan -= 2;
681 					} else if (primary - center == 30) {
682 						mac->cur_40_prime_sc =
683 						PRIME_CHNL_OFFSET_UPPER;
684 
685 						wide_chan -= 6;
686 					}
687 				}
688 			}
689 		} else {
690 			switch (channel_type) {
691 			case NL80211_CHAN_HT20:
692 			case NL80211_CHAN_NO_HT:
693 					/* SC */
694 					mac->cur_40_prime_sc =
695 						PRIME_CHNL_OFFSET_DONT_CARE;
696 					rtlphy->current_chan_bw =
697 						HT_CHANNEL_WIDTH_20;
698 					mac->bw_40 = false;
699 					mac->bw_80 = false;
700 					break;
701 			case NL80211_CHAN_HT40MINUS:
702 					/* SC */
703 					mac->cur_40_prime_sc =
704 						PRIME_CHNL_OFFSET_UPPER;
705 					rtlphy->current_chan_bw =
706 						HT_CHANNEL_WIDTH_20_40;
707 					mac->bw_40 = true;
708 					mac->bw_80 = false;
709 
710 					/*wide channel */
711 					wide_chan -= 2;
712 
713 					break;
714 			case NL80211_CHAN_HT40PLUS:
715 					/* SC */
716 					mac->cur_40_prime_sc =
717 						PRIME_CHNL_OFFSET_LOWER;
718 					rtlphy->current_chan_bw =
719 						HT_CHANNEL_WIDTH_20_40;
720 					mac->bw_40 = true;
721 					mac->bw_80 = false;
722 
723 					/*wide channel */
724 					wide_chan += 2;
725 
726 					break;
727 			default:
728 					mac->bw_40 = false;
729 					mac->bw_80 = false;
730 					pr_err("switch case %#x not processed\n",
731 					       channel_type);
732 					break;
733 			}
734 		}
735 
736 		if (wide_chan <= 0)
737 			wide_chan = 1;
738 
739 		/* In scanning, when before we offchannel we may send a ps=1
740 		 * null to AP, and then we may send a ps = 0 null to AP quickly,
741 		 * but first null may have caused AP to put lots of packet to
742 		 * hw tx buffer. These packets must be tx'd before we go off
743 		 * channel so we must delay more time to let AP flush these
744 		 * packets before going offchannel, or dis-association or
745 		 * delete BA will be caused by AP
746 		 */
747 		if (rtlpriv->mac80211.offchan_delay) {
748 			rtlpriv->mac80211.offchan_delay = false;
749 			mdelay(50);
750 		}
751 
752 		rtlphy->current_channel = wide_chan;
753 
754 		rtlpriv->cfg->ops->switch_channel(hw);
755 		rtlpriv->cfg->ops->set_channel_access(hw);
756 		rtlpriv->cfg->ops->set_bw_mode(hw, channel_type);
757 	}
758 
759 	mutex_unlock(&rtlpriv->locks.conf_mutex);
760 
761 	return 0;
762 }
763 
764 static void rtl_op_configure_filter(struct ieee80211_hw *hw,
765 				    unsigned int changed_flags,
766 				    unsigned int *new_flags, u64 multicast)
767 {
768 	bool update_rcr = false;
769 	struct rtl_priv *rtlpriv = rtl_priv(hw);
770 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
771 
772 	*new_flags &= RTL_SUPPORTED_FILTERS;
773 	if (0 == changed_flags)
774 		return;
775 
776 	/*TODO: we disable broadcast now, so enable here */
777 	if (changed_flags & FIF_ALLMULTI) {
778 		if (*new_flags & FIF_ALLMULTI) {
779 			mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AM] |
780 			    rtlpriv->cfg->maps[MAC_RCR_AB];
781 			rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
782 				"Enable receive multicast frame\n");
783 		} else {
784 			mac->rx_conf &= ~(rtlpriv->cfg->maps[MAC_RCR_AM] |
785 					  rtlpriv->cfg->maps[MAC_RCR_AB]);
786 			rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
787 				"Disable receive multicast frame\n");
788 		}
789 		update_rcr = true;
790 	}
791 
792 	if (changed_flags & FIF_FCSFAIL) {
793 		if (*new_flags & FIF_FCSFAIL) {
794 			mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACRC32];
795 			rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
796 				"Enable receive FCS error frame\n");
797 		} else {
798 			mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACRC32];
799 			rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
800 				"Disable receive FCS error frame\n");
801 		}
802 		if (!update_rcr)
803 			update_rcr = true;
804 	}
805 
806 	/* if ssid not set to hw don't check bssid
807 	 * here just used for linked scanning, & linked
808 	 * and nolink check bssid is set in set network_type
809 	 */
810 	if (changed_flags & FIF_BCN_PRBRESP_PROMISC &&
811 	    mac->link_state >= MAC80211_LINKED) {
812 		if (mac->opmode != NL80211_IFTYPE_AP &&
813 		    mac->opmode != NL80211_IFTYPE_MESH_POINT) {
814 			if (*new_flags & FIF_BCN_PRBRESP_PROMISC)
815 				rtlpriv->cfg->ops->set_chk_bssid(hw, false);
816 			else
817 				rtlpriv->cfg->ops->set_chk_bssid(hw, true);
818 			if (update_rcr)
819 				update_rcr = false;
820 		}
821 	}
822 
823 	if (changed_flags & FIF_CONTROL) {
824 		if (*new_flags & FIF_CONTROL) {
825 			mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACF];
826 
827 			rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
828 				"Enable receive control frame.\n");
829 		} else {
830 			mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACF];
831 			rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
832 				"Disable receive control frame.\n");
833 		}
834 		if (!update_rcr)
835 			update_rcr = true;
836 	}
837 
838 	if (changed_flags & FIF_OTHER_BSS) {
839 		if (*new_flags & FIF_OTHER_BSS) {
840 			mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AAP];
841 			rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
842 				"Enable receive other BSS's frame.\n");
843 		} else {
844 			mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_AAP];
845 			rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
846 				"Disable receive other BSS's frame.\n");
847 		}
848 		if (!update_rcr)
849 			update_rcr = true;
850 	}
851 
852 	if (update_rcr)
853 		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RCR,
854 					      (u8 *)(&mac->rx_conf));
855 }
856 
857 static int rtl_op_sta_add(struct ieee80211_hw *hw,
858 			 struct ieee80211_vif *vif,
859 			 struct ieee80211_sta *sta)
860 {
861 	struct rtl_priv *rtlpriv = rtl_priv(hw);
862 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
863 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
864 	struct rtl_sta_info *sta_entry;
865 
866 	if (sta) {
867 		sta_entry = (struct rtl_sta_info *)sta->drv_priv;
868 		spin_lock_bh(&rtlpriv->locks.entry_list_lock);
869 		list_add_tail(&sta_entry->list, &rtlpriv->entry_list);
870 		spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
871 		if (rtlhal->current_bandtype == BAND_ON_2_4G) {
872 			sta_entry->wireless_mode = WIRELESS_MODE_G;
873 			if (sta->deflink.supp_rates[0] <= 0xf)
874 				sta_entry->wireless_mode = WIRELESS_MODE_B;
875 			if (sta->deflink.ht_cap.ht_supported)
876 				sta_entry->wireless_mode = WIRELESS_MODE_N_24G;
877 
878 			if (vif->type == NL80211_IFTYPE_ADHOC)
879 				sta_entry->wireless_mode = WIRELESS_MODE_G;
880 		} else if (rtlhal->current_bandtype == BAND_ON_5G) {
881 			sta_entry->wireless_mode = WIRELESS_MODE_A;
882 			if (sta->deflink.ht_cap.ht_supported)
883 				sta_entry->wireless_mode = WIRELESS_MODE_N_5G;
884 			if (sta->deflink.vht_cap.vht_supported)
885 				sta_entry->wireless_mode = WIRELESS_MODE_AC_5G;
886 
887 			if (vif->type == NL80211_IFTYPE_ADHOC)
888 				sta_entry->wireless_mode = WIRELESS_MODE_A;
889 		}
890 		/*disable cck rate for p2p*/
891 		if (mac->p2p)
892 			sta->deflink.supp_rates[0] &= 0xfffffff0;
893 
894 		memcpy(sta_entry->mac_addr, sta->addr, ETH_ALEN);
895 		rtl_dbg(rtlpriv, COMP_MAC80211, DBG_DMESG,
896 			"Add sta addr is %pM\n", sta->addr);
897 		rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0, true);
898 	}
899 
900 	return 0;
901 }
902 
903 static int rtl_op_sta_remove(struct ieee80211_hw *hw,
904 				struct ieee80211_vif *vif,
905 				struct ieee80211_sta *sta)
906 {
907 	struct rtl_priv *rtlpriv = rtl_priv(hw);
908 	struct rtl_sta_info *sta_entry;
909 
910 	if (sta) {
911 		rtl_dbg(rtlpriv, COMP_MAC80211, DBG_DMESG,
912 			"Remove sta addr is %pM\n", sta->addr);
913 		sta_entry = (struct rtl_sta_info *)sta->drv_priv;
914 		sta_entry->wireless_mode = 0;
915 		sta_entry->ratr_index = 0;
916 		spin_lock_bh(&rtlpriv->locks.entry_list_lock);
917 		list_del(&sta_entry->list);
918 		spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
919 	}
920 	return 0;
921 }
922 
923 static int _rtl_get_hal_qnum(u16 queue)
924 {
925 	int qnum;
926 
927 	switch (queue) {
928 	case 0:
929 		qnum = AC3_VO;
930 		break;
931 	case 1:
932 		qnum = AC2_VI;
933 		break;
934 	case 2:
935 		qnum = AC0_BE;
936 		break;
937 	case 3:
938 		qnum = AC1_BK;
939 		break;
940 	default:
941 		qnum = AC0_BE;
942 		break;
943 	}
944 	return qnum;
945 }
946 
947 /*
948  *for mac80211 VO = 0, VI = 1, BE = 2, BK = 3
949  *for rtl819x  BE = 0, BK = 1, VI = 2, VO = 3
950  */
951 static int rtl_op_conf_tx(struct ieee80211_hw *hw,
952 			  struct ieee80211_vif *vif,
953 			  unsigned int link_id, u16 queue,
954 			  const struct ieee80211_tx_queue_params *param)
955 {
956 	struct rtl_priv *rtlpriv = rtl_priv(hw);
957 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
958 	int aci;
959 
960 	if (queue >= AC_MAX) {
961 		rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
962 			"queue number %d is incorrect!\n", queue);
963 		return -EINVAL;
964 	}
965 
966 	aci = _rtl_get_hal_qnum(queue);
967 	mac->ac[aci].aifs = param->aifs;
968 	mac->ac[aci].cw_min = cpu_to_le16(param->cw_min);
969 	mac->ac[aci].cw_max = cpu_to_le16(param->cw_max);
970 	mac->ac[aci].tx_op = cpu_to_le16(param->txop);
971 	memcpy(&mac->edca_param[aci], param, sizeof(*param));
972 	rtlpriv->cfg->ops->set_qos(hw, aci);
973 	return 0;
974 }
975 
976 static void send_beacon_frame(struct ieee80211_hw *hw,
977 			      struct ieee80211_vif *vif)
978 {
979 	struct rtl_priv *rtlpriv = rtl_priv(hw);
980 	struct sk_buff *skb = ieee80211_beacon_get(hw, vif, 0);
981 	struct rtl_tcb_desc tcb_desc;
982 
983 	if (skb) {
984 		memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
985 		rtlpriv->intf_ops->adapter_tx(hw, NULL, skb, &tcb_desc);
986 	}
987 }
988 
989 void rtl_update_beacon_work_callback(struct work_struct *work)
990 {
991 	struct rtl_works *rtlworks =
992 	    container_of(work, struct rtl_works, update_beacon_work);
993 	struct ieee80211_hw *hw = rtlworks->hw;
994 	struct rtl_priv *rtlpriv = rtl_priv(hw);
995 	struct ieee80211_vif *vif = rtlpriv->mac80211.vif;
996 
997 	if (!vif) {
998 		WARN_ONCE(true, "no vif to update beacon\n");
999 		return;
1000 	}
1001 
1002 	mutex_lock(&rtlpriv->locks.conf_mutex);
1003 	send_beacon_frame(hw, vif);
1004 	mutex_unlock(&rtlpriv->locks.conf_mutex);
1005 }
1006 EXPORT_SYMBOL_GPL(rtl_update_beacon_work_callback);
1007 
1008 static void rtl_op_bss_info_changed(struct ieee80211_hw *hw,
1009 				    struct ieee80211_vif *vif,
1010 				    struct ieee80211_bss_conf *bss_conf,
1011 				    u64 changed)
1012 {
1013 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1014 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
1015 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1016 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1017 
1018 	mutex_lock(&rtlpriv->locks.conf_mutex);
1019 	if (vif->type == NL80211_IFTYPE_ADHOC ||
1020 	    vif->type == NL80211_IFTYPE_AP ||
1021 	    vif->type == NL80211_IFTYPE_MESH_POINT) {
1022 		if (changed & BSS_CHANGED_BEACON ||
1023 		    (changed & BSS_CHANGED_BEACON_ENABLED &&
1024 		     bss_conf->enable_beacon)) {
1025 			if (mac->beacon_enabled == 0) {
1026 				rtl_dbg(rtlpriv, COMP_MAC80211, DBG_DMESG,
1027 					"BSS_CHANGED_BEACON_ENABLED\n");
1028 
1029 				/*start hw beacon interrupt. */
1030 				/*rtlpriv->cfg->ops->set_bcn_reg(hw); */
1031 				mac->beacon_enabled = 1;
1032 				rtlpriv->cfg->ops->update_interrupt_mask(hw,
1033 						rtlpriv->cfg->maps
1034 						[RTL_IBSS_INT_MASKS], 0);
1035 
1036 				if (rtlpriv->cfg->ops->linked_set_reg)
1037 					rtlpriv->cfg->ops->linked_set_reg(hw);
1038 				send_beacon_frame(hw, vif);
1039 			}
1040 		}
1041 		if ((changed & BSS_CHANGED_BEACON_ENABLED &&
1042 		    !bss_conf->enable_beacon)) {
1043 			if (mac->beacon_enabled == 1) {
1044 				rtl_dbg(rtlpriv, COMP_MAC80211, DBG_DMESG,
1045 					"ADHOC DISABLE BEACON\n");
1046 
1047 				mac->beacon_enabled = 0;
1048 				rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
1049 						rtlpriv->cfg->maps
1050 						[RTL_IBSS_INT_MASKS]);
1051 			}
1052 		}
1053 		if (changed & BSS_CHANGED_BEACON_INT) {
1054 			rtl_dbg(rtlpriv, COMP_BEACON, DBG_TRACE,
1055 				"BSS_CHANGED_BEACON_INT\n");
1056 			mac->beacon_interval = bss_conf->beacon_int;
1057 			rtlpriv->cfg->ops->set_bcn_intv(hw);
1058 		}
1059 	}
1060 
1061 	/*TODO: reference to enum ieee80211_bss_change */
1062 	if (changed & BSS_CHANGED_ASSOC) {
1063 		u8 mstatus;
1064 
1065 		if (vif->cfg.assoc) {
1066 			struct ieee80211_sta *sta = NULL;
1067 			u8 keep_alive = 10;
1068 
1069 			mstatus = RT_MEDIA_CONNECT;
1070 			/* we should reset all sec info & cam
1071 			 * before set cam after linked, we should not
1072 			 * reset in disassoc, that will cause tkip->wep
1073 			 * fail because some flag will be wrong */
1074 			/* reset sec info */
1075 			rtl_cam_reset_sec_info(hw);
1076 			/* reset cam to fix wep fail issue
1077 			 * when change from wpa to wep */
1078 			rtl_cam_reset_all_entry(hw);
1079 
1080 			mac->link_state = MAC80211_LINKED;
1081 			mac->cnt_after_linked = 0;
1082 			mac->assoc_id = vif->cfg.aid;
1083 			memcpy(mac->bssid, bss_conf->bssid, ETH_ALEN);
1084 
1085 			if (rtlpriv->cfg->ops->linked_set_reg)
1086 				rtlpriv->cfg->ops->linked_set_reg(hw);
1087 
1088 			rcu_read_lock();
1089 			sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
1090 			if (!sta) {
1091 				rcu_read_unlock();
1092 				goto out;
1093 			}
1094 			rtl_dbg(rtlpriv, COMP_EASY_CONCURRENT, DBG_LOUD,
1095 				"send PS STATIC frame\n");
1096 			if (rtlpriv->dm.supp_phymode_switch) {
1097 				if (sta->deflink.ht_cap.ht_supported)
1098 					rtl_send_smps_action(hw, sta,
1099 							IEEE80211_SMPS_STATIC);
1100 			}
1101 
1102 			if (rtlhal->current_bandtype == BAND_ON_5G) {
1103 				mac->mode = WIRELESS_MODE_A;
1104 			} else {
1105 				if (sta->deflink.supp_rates[0] <= 0xf)
1106 					mac->mode = WIRELESS_MODE_B;
1107 				else
1108 					mac->mode = WIRELESS_MODE_G;
1109 			}
1110 
1111 			if (sta->deflink.ht_cap.ht_supported) {
1112 				if (rtlhal->current_bandtype == BAND_ON_2_4G)
1113 					mac->mode = WIRELESS_MODE_N_24G;
1114 				else
1115 					mac->mode = WIRELESS_MODE_N_5G;
1116 			}
1117 
1118 			if (sta->deflink.vht_cap.vht_supported) {
1119 				if (rtlhal->current_bandtype == BAND_ON_5G)
1120 					mac->mode = WIRELESS_MODE_AC_5G;
1121 				else
1122 					mac->mode = WIRELESS_MODE_AC_24G;
1123 			}
1124 
1125 			if (vif->type == NL80211_IFTYPE_STATION)
1126 				rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0,
1127 								   true);
1128 			rcu_read_unlock();
1129 
1130 			/* to avoid AP Disassociation caused by inactivity */
1131 			rtlpriv->cfg->ops->set_hw_reg(hw,
1132 						      HW_VAR_KEEP_ALIVE,
1133 						      (u8 *)(&keep_alive));
1134 
1135 			rtl_dbg(rtlpriv, COMP_MAC80211, DBG_DMESG,
1136 				"BSS_CHANGED_ASSOC\n");
1137 		} else {
1138 			struct cfg80211_bss *bss = NULL;
1139 
1140 			mstatus = RT_MEDIA_DISCONNECT;
1141 
1142 			if (mac->link_state == MAC80211_LINKED)
1143 				rtl_lps_leave(hw, true);
1144 			if (ppsc->p2p_ps_info.p2p_ps_mode > P2P_PS_NONE)
1145 				rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
1146 			mac->link_state = MAC80211_NOLINK;
1147 
1148 			bss = cfg80211_get_bss(hw->wiphy, NULL,
1149 					       (u8 *)mac->bssid, NULL, 0,
1150 					       IEEE80211_BSS_TYPE_ESS,
1151 					       IEEE80211_PRIVACY_OFF);
1152 
1153 			rtl_dbg(rtlpriv, COMP_MAC80211, DBG_DMESG,
1154 				"bssid = %pMF\n", mac->bssid);
1155 
1156 			if (bss) {
1157 				cfg80211_unlink_bss(hw->wiphy, bss);
1158 				cfg80211_put_bss(hw->wiphy, bss);
1159 				rtl_dbg(rtlpriv, COMP_MAC80211, DBG_DMESG,
1160 					"cfg80211_unlink !!\n");
1161 			}
1162 
1163 			eth_zero_addr(mac->bssid);
1164 			mac->vendor = PEER_UNKNOWN;
1165 			mac->mode = 0;
1166 
1167 			rtl_dbg(rtlpriv, COMP_MAC80211, DBG_DMESG,
1168 				"BSS_CHANGED_UN_ASSOC\n");
1169 		}
1170 		rtlpriv->cfg->ops->set_network_type(hw, vif->type);
1171 		/* For FW LPS:
1172 		 * To tell firmware we have connected or disconnected
1173 		 */
1174 		rtlpriv->cfg->ops->set_hw_reg(hw,
1175 					      HW_VAR_H2C_FW_JOINBSSRPT,
1176 					      (u8 *)(&mstatus));
1177 		ppsc->report_linked = (mstatus == RT_MEDIA_CONNECT) ?
1178 				      true : false;
1179 
1180 		if (rtlpriv->cfg->ops->get_btc_status())
1181 			rtlpriv->btcoexist.btc_ops->btc_mediastatus_notify(
1182 							rtlpriv, mstatus);
1183 	}
1184 
1185 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1186 		rtl_dbg(rtlpriv, COMP_MAC80211, DBG_TRACE,
1187 			"BSS_CHANGED_ERP_CTS_PROT\n");
1188 		mac->use_cts_protect = bss_conf->use_cts_prot;
1189 	}
1190 
1191 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1192 		rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD,
1193 			"BSS_CHANGED_ERP_PREAMBLE use short preamble:%x\n",
1194 			  bss_conf->use_short_preamble);
1195 
1196 		mac->short_preamble = bss_conf->use_short_preamble;
1197 		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ACK_PREAMBLE,
1198 					      (u8 *)(&mac->short_preamble));
1199 	}
1200 
1201 	if (changed & BSS_CHANGED_ERP_SLOT) {
1202 		rtl_dbg(rtlpriv, COMP_MAC80211, DBG_TRACE,
1203 			"BSS_CHANGED_ERP_SLOT\n");
1204 
1205 		if (bss_conf->use_short_slot)
1206 			mac->slot_time = RTL_SLOT_TIME_9;
1207 		else
1208 			mac->slot_time = RTL_SLOT_TIME_20;
1209 
1210 		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SLOT_TIME,
1211 					      (u8 *)(&mac->slot_time));
1212 	}
1213 
1214 	if (changed & BSS_CHANGED_HT) {
1215 		struct ieee80211_sta *sta = NULL;
1216 
1217 		rtl_dbg(rtlpriv, COMP_MAC80211, DBG_TRACE,
1218 			"BSS_CHANGED_HT\n");
1219 
1220 		rcu_read_lock();
1221 		sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
1222 		if (sta) {
1223 			if (sta->deflink.ht_cap.ampdu_density >
1224 			    mac->current_ampdu_density)
1225 				mac->current_ampdu_density =
1226 				    sta->deflink.ht_cap.ampdu_density;
1227 			if (sta->deflink.ht_cap.ampdu_factor <
1228 			    mac->current_ampdu_factor)
1229 				mac->current_ampdu_factor =
1230 				    sta->deflink.ht_cap.ampdu_factor;
1231 		}
1232 		rcu_read_unlock();
1233 
1234 		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SHORTGI_DENSITY,
1235 					      (u8 *)(&mac->max_mss_density));
1236 		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_FACTOR,
1237 					      &mac->current_ampdu_factor);
1238 		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_MIN_SPACE,
1239 					      &mac->current_ampdu_density);
1240 	}
1241 
1242 	if (changed & BSS_CHANGED_BSSID) {
1243 		u32 basic_rates;
1244 		struct ieee80211_sta *sta = NULL;
1245 
1246 		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BSSID,
1247 					      (u8 *)bss_conf->bssid);
1248 
1249 		rtl_dbg(rtlpriv, COMP_MAC80211, DBG_DMESG,
1250 			"bssid: %pM\n", bss_conf->bssid);
1251 
1252 		mac->vendor = PEER_UNKNOWN;
1253 		memcpy(mac->bssid, bss_conf->bssid, ETH_ALEN);
1254 
1255 		rcu_read_lock();
1256 		sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
1257 		if (!sta) {
1258 			rcu_read_unlock();
1259 			goto out;
1260 		}
1261 
1262 		if (rtlhal->current_bandtype == BAND_ON_5G) {
1263 			mac->mode = WIRELESS_MODE_A;
1264 		} else {
1265 			if (sta->deflink.supp_rates[0] <= 0xf)
1266 				mac->mode = WIRELESS_MODE_B;
1267 			else
1268 				mac->mode = WIRELESS_MODE_G;
1269 		}
1270 
1271 		if (sta->deflink.ht_cap.ht_supported) {
1272 			if (rtlhal->current_bandtype == BAND_ON_2_4G)
1273 				mac->mode = WIRELESS_MODE_N_24G;
1274 			else
1275 				mac->mode = WIRELESS_MODE_N_5G;
1276 		}
1277 
1278 		if (sta->deflink.vht_cap.vht_supported) {
1279 			if (rtlhal->current_bandtype == BAND_ON_5G)
1280 				mac->mode = WIRELESS_MODE_AC_5G;
1281 			else
1282 				mac->mode = WIRELESS_MODE_AC_24G;
1283 		}
1284 
1285 		/* just station need it, because ibss & ap mode will
1286 		 * set in sta_add, and will be NULL here */
1287 		if (vif->type == NL80211_IFTYPE_STATION) {
1288 			struct rtl_sta_info *sta_entry;
1289 
1290 			sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1291 			sta_entry->wireless_mode = mac->mode;
1292 		}
1293 
1294 		if (sta->deflink.ht_cap.ht_supported) {
1295 			mac->ht_enable = true;
1296 
1297 			/*
1298 			 * for cisco 1252 bw20 it's wrong
1299 			 * if (ht_cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
1300 			 *	mac->bw_40 = true;
1301 			 * }
1302 			 * */
1303 		}
1304 
1305 		if (sta->deflink.vht_cap.vht_supported)
1306 			mac->vht_enable = true;
1307 
1308 		if (changed & BSS_CHANGED_BASIC_RATES) {
1309 			/* for 5G must << RATE_6M_INDEX = 4,
1310 			 * because 5G have no cck rate*/
1311 			if (rtlhal->current_bandtype == BAND_ON_5G)
1312 				basic_rates = sta->deflink.supp_rates[1] << 4;
1313 			else
1314 				basic_rates = sta->deflink.supp_rates[0];
1315 
1316 			mac->basic_rates = basic_rates;
1317 			rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
1318 					(u8 *)(&basic_rates));
1319 		}
1320 		rcu_read_unlock();
1321 	}
1322 out:
1323 	mutex_unlock(&rtlpriv->locks.conf_mutex);
1324 }
1325 
1326 static u64 rtl_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1327 {
1328 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1329 	u64 tsf;
1330 
1331 	rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *)(&tsf));
1332 	return tsf;
1333 }
1334 
1335 static void rtl_op_set_tsf(struct ieee80211_hw *hw,
1336 			   struct ieee80211_vif *vif, u64 tsf)
1337 {
1338 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1339 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1340 	u8 bibss = (mac->opmode == NL80211_IFTYPE_ADHOC) ? 1 : 0;
1341 
1342 	mac->tsf = tsf;
1343 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *)(&bibss));
1344 }
1345 
1346 static void rtl_op_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1347 {
1348 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1349 	u8 tmp = 0;
1350 
1351 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_DUAL_TSF_RST, (u8 *)(&tmp));
1352 }
1353 
1354 static void rtl_op_sta_notify(struct ieee80211_hw *hw,
1355 			      struct ieee80211_vif *vif,
1356 			      enum sta_notify_cmd cmd,
1357 			      struct ieee80211_sta *sta)
1358 {
1359 	switch (cmd) {
1360 	case STA_NOTIFY_SLEEP:
1361 		break;
1362 	case STA_NOTIFY_AWAKE:
1363 		break;
1364 	default:
1365 		break;
1366 	}
1367 }
1368 
1369 static int rtl_op_ampdu_action(struct ieee80211_hw *hw,
1370 			       struct ieee80211_vif *vif,
1371 			       struct ieee80211_ampdu_params *params)
1372 {
1373 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1374 	struct ieee80211_sta *sta = params->sta;
1375 	enum ieee80211_ampdu_mlme_action action = params->action;
1376 	u16 tid = params->tid;
1377 	u16 *ssn = &params->ssn;
1378 
1379 	switch (action) {
1380 	case IEEE80211_AMPDU_TX_START:
1381 		rtl_dbg(rtlpriv, COMP_MAC80211, DBG_TRACE,
1382 			"IEEE80211_AMPDU_TX_START: TID:%d\n", tid);
1383 		return rtl_tx_agg_start(hw, vif, sta, tid, ssn);
1384 	case IEEE80211_AMPDU_TX_STOP_CONT:
1385 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
1386 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1387 		rtl_dbg(rtlpriv, COMP_MAC80211, DBG_TRACE,
1388 			"IEEE80211_AMPDU_TX_STOP: TID:%d\n", tid);
1389 		return rtl_tx_agg_stop(hw, vif, sta, tid);
1390 	case IEEE80211_AMPDU_TX_OPERATIONAL:
1391 		rtl_dbg(rtlpriv, COMP_MAC80211, DBG_TRACE,
1392 			"IEEE80211_AMPDU_TX_OPERATIONAL:TID:%d\n", tid);
1393 		rtl_tx_agg_oper(hw, sta, tid);
1394 		break;
1395 	case IEEE80211_AMPDU_RX_START:
1396 		rtl_dbg(rtlpriv, COMP_MAC80211, DBG_TRACE,
1397 			"IEEE80211_AMPDU_RX_START:TID:%d\n", tid);
1398 		return rtl_rx_agg_start(hw, sta, tid);
1399 	case IEEE80211_AMPDU_RX_STOP:
1400 		rtl_dbg(rtlpriv, COMP_MAC80211, DBG_TRACE,
1401 			"IEEE80211_AMPDU_RX_STOP:TID:%d\n", tid);
1402 		return rtl_rx_agg_stop(hw, sta, tid);
1403 	default:
1404 		pr_err("IEEE80211_AMPDU_ERR!!!!:\n");
1405 		return -EOPNOTSUPP;
1406 	}
1407 	return 0;
1408 }
1409 
1410 static void rtl_op_sw_scan_start(struct ieee80211_hw *hw,
1411 				 struct ieee80211_vif *vif,
1412 				 const u8 *mac_addr)
1413 {
1414 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1415 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1416 
1417 	rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
1418 	mac->act_scanning = true;
1419 	if (rtlpriv->link_info.higher_busytraffic) {
1420 		mac->skip_scan = true;
1421 		return;
1422 	}
1423 
1424 	if (rtlpriv->cfg->ops->get_btc_status())
1425 		rtlpriv->btcoexist.btc_ops->btc_scan_notify(rtlpriv, 1);
1426 	else if (rtlpriv->btcoexist.btc_ops)
1427 		rtlpriv->btcoexist.btc_ops->btc_scan_notify_wifi_only(rtlpriv,
1428 								      1);
1429 
1430 	if (mac->link_state == MAC80211_LINKED) {
1431 		rtl_lps_leave(hw, true);
1432 		mac->link_state = MAC80211_LINKED_SCANNING;
1433 	} else {
1434 		rtl_ips_nic_on(hw);
1435 	}
1436 
1437 	/* Dul mac */
1438 	rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
1439 
1440 	rtlpriv->cfg->ops->led_control(hw, LED_CTL_SITE_SURVEY);
1441 	rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_BACKUP_BAND0);
1442 }
1443 
1444 static void rtl_op_sw_scan_complete(struct ieee80211_hw *hw,
1445 				    struct ieee80211_vif *vif)
1446 {
1447 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1448 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1449 
1450 	rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
1451 	mac->act_scanning = false;
1452 	mac->skip_scan = false;
1453 
1454 	rtlpriv->btcoexist.btc_info.ap_num = rtlpriv->scan_list.num;
1455 
1456 	if (rtlpriv->link_info.higher_busytraffic)
1457 		return;
1458 
1459 	/* p2p will use 1/6/11 to scan */
1460 	if (mac->n_channels == 3)
1461 		mac->p2p_in_use = true;
1462 	else
1463 		mac->p2p_in_use = false;
1464 	mac->n_channels = 0;
1465 	/* Dul mac */
1466 	rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
1467 
1468 	if (mac->link_state == MAC80211_LINKED_SCANNING) {
1469 		mac->link_state = MAC80211_LINKED;
1470 		if (mac->opmode == NL80211_IFTYPE_STATION) {
1471 			/* fix fwlps issue */
1472 			rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
1473 		}
1474 	}
1475 
1476 	rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_RESTORE);
1477 	if (rtlpriv->cfg->ops->get_btc_status())
1478 		rtlpriv->btcoexist.btc_ops->btc_scan_notify(rtlpriv, 0);
1479 	else if (rtlpriv->btcoexist.btc_ops)
1480 		rtlpriv->btcoexist.btc_ops->btc_scan_notify_wifi_only(rtlpriv,
1481 								      0);
1482 }
1483 
1484 static int rtl_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
1485 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
1486 			  struct ieee80211_key_conf *key)
1487 {
1488 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1489 	u8 key_type = NO_ENCRYPTION;
1490 	u8 key_idx;
1491 	bool group_key = false;
1492 	bool wep_only = false;
1493 	int err = 0;
1494 	u8 mac_addr[ETH_ALEN];
1495 	u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1496 
1497 	rtlpriv->btcoexist.btc_info.in_4way = false;
1498 
1499 	if (rtlpriv->cfg->mod_params->sw_crypto || rtlpriv->sec.use_sw_sec) {
1500 		rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
1501 			"not open hw encryption\n");
1502 		return -ENOSPC;	/*User disabled HW-crypto */
1503 	}
1504 	/* To support IBSS, use sw-crypto for GTK */
1505 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
1506 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
1507 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1508 		return -ENOSPC;
1509 	rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
1510 		"%s hardware based encryption for keyidx: %d, mac: %pM\n",
1511 		cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
1512 		sta ? sta->addr : bcast_addr);
1513 	rtlpriv->sec.being_setkey = true;
1514 	rtl_ips_nic_on(hw);
1515 	mutex_lock(&rtlpriv->locks.conf_mutex);
1516 	/* <1> get encryption alg */
1517 
1518 	switch (key->cipher) {
1519 	case WLAN_CIPHER_SUITE_WEP40:
1520 		key_type = WEP40_ENCRYPTION;
1521 		rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP40\n");
1522 		break;
1523 	case WLAN_CIPHER_SUITE_WEP104:
1524 		rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP104\n");
1525 		key_type = WEP104_ENCRYPTION;
1526 		break;
1527 	case WLAN_CIPHER_SUITE_TKIP:
1528 		key_type = TKIP_ENCRYPTION;
1529 		rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG, "alg:TKIP\n");
1530 		break;
1531 	case WLAN_CIPHER_SUITE_CCMP:
1532 		key_type = AESCCMP_ENCRYPTION;
1533 		rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CCMP\n");
1534 		break;
1535 	case WLAN_CIPHER_SUITE_AES_CMAC:
1536 		/* HW don't support CMAC encryption,
1537 		 * use software CMAC encryption
1538 		 */
1539 		key_type = AESCMAC_ENCRYPTION;
1540 		rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CMAC\n");
1541 		rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
1542 			"HW don't support CMAC encryption, use software CMAC encryption\n");
1543 		err = -EOPNOTSUPP;
1544 		goto out_unlock;
1545 	default:
1546 		pr_err("alg_err:%x!!!!:\n", key->cipher);
1547 		goto out_unlock;
1548 	}
1549 	if (key_type == WEP40_ENCRYPTION ||
1550 	   key_type == WEP104_ENCRYPTION ||
1551 	   vif->type == NL80211_IFTYPE_ADHOC)
1552 		rtlpriv->sec.use_defaultkey = true;
1553 
1554 	/* <2> get key_idx */
1555 	key_idx = (u8) (key->keyidx);
1556 	if (key_idx > 3)
1557 		goto out_unlock;
1558 	/* <3> if pairwise key enable_hw_sec */
1559 	group_key = !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE);
1560 
1561 	/* wep always be group key, but there are two conditions:
1562 	 * 1) wep only: is just for wep enc, in this condition
1563 	 * rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION
1564 	 * will be true & enable_hw_sec will be set when wep
1565 	 * ke setting.
1566 	 * 2) wep(group) + AES(pairwise): some AP like cisco
1567 	 * may use it, in this condition enable_hw_sec will not
1568 	 * be set when wep key setting */
1569 	/* we must reset sec_info after lingked before set key,
1570 	 * or some flag will be wrong*/
1571 	if (vif->type == NL80211_IFTYPE_AP ||
1572 		vif->type == NL80211_IFTYPE_MESH_POINT) {
1573 		if (!group_key || key_type == WEP40_ENCRYPTION ||
1574 			key_type == WEP104_ENCRYPTION) {
1575 			if (group_key)
1576 				wep_only = true;
1577 			rtlpriv->cfg->ops->enable_hw_sec(hw);
1578 		}
1579 	} else {
1580 		if (!group_key || vif->type == NL80211_IFTYPE_ADHOC ||
1581 		    rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION) {
1582 			if (rtlpriv->sec.pairwise_enc_algorithm ==
1583 			    NO_ENCRYPTION &&
1584 			   (key_type == WEP40_ENCRYPTION ||
1585 			    key_type == WEP104_ENCRYPTION))
1586 				wep_only = true;
1587 			rtlpriv->sec.pairwise_enc_algorithm = key_type;
1588 			rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
1589 				"set enable_hw_sec, key_type:%x(OPEN:0 WEP40:1 TKIP:2 AES:4 WEP104:5)\n",
1590 				key_type);
1591 			rtlpriv->cfg->ops->enable_hw_sec(hw);
1592 		}
1593 	}
1594 	/* <4> set key based on cmd */
1595 	switch (cmd) {
1596 	case SET_KEY:
1597 		if (wep_only) {
1598 			rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
1599 				"set WEP(group/pairwise) key\n");
1600 			/* Pairwise key with an assigned MAC address. */
1601 			rtlpriv->sec.pairwise_enc_algorithm = key_type;
1602 			rtlpriv->sec.group_enc_algorithm = key_type;
1603 			/*set local buf about wep key. */
1604 			memcpy(rtlpriv->sec.key_buf[key_idx],
1605 			       key->key, key->keylen);
1606 			rtlpriv->sec.key_len[key_idx] = key->keylen;
1607 			eth_zero_addr(mac_addr);
1608 		} else if (group_key) {	/* group key */
1609 			rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
1610 				"set group key\n");
1611 			/* group key */
1612 			rtlpriv->sec.group_enc_algorithm = key_type;
1613 			/*set local buf about group key. */
1614 			memcpy(rtlpriv->sec.key_buf[key_idx],
1615 			       key->key, key->keylen);
1616 			rtlpriv->sec.key_len[key_idx] = key->keylen;
1617 			eth_broadcast_addr(mac_addr);
1618 		} else {	/* pairwise key */
1619 			rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
1620 				"set pairwise key\n");
1621 			if (!sta) {
1622 				WARN_ONCE(true,
1623 					  "rtlwifi: pairwise key without mac_addr\n");
1624 
1625 				err = -EOPNOTSUPP;
1626 				goto out_unlock;
1627 			}
1628 			/* Pairwise key with an assigned MAC address. */
1629 			rtlpriv->sec.pairwise_enc_algorithm = key_type;
1630 			/*set local buf about pairwise key. */
1631 			memcpy(rtlpriv->sec.key_buf[PAIRWISE_KEYIDX],
1632 			       key->key, key->keylen);
1633 			rtlpriv->sec.key_len[PAIRWISE_KEYIDX] = key->keylen;
1634 			rtlpriv->sec.pairwise_key =
1635 			    rtlpriv->sec.key_buf[PAIRWISE_KEYIDX];
1636 			memcpy(mac_addr, sta->addr, ETH_ALEN);
1637 		}
1638 		rtlpriv->cfg->ops->set_key(hw, key_idx, mac_addr,
1639 					   group_key, key_type, wep_only,
1640 					   false);
1641 		/* <5> tell mac80211 do something: */
1642 		/*must use sw generate IV, or can not work !!!!. */
1643 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1644 		key->hw_key_idx = key_idx;
1645 		if (key_type == TKIP_ENCRYPTION)
1646 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1647 		/*use software CCMP encryption for management frames (MFP) */
1648 		if (key_type == AESCCMP_ENCRYPTION)
1649 			key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
1650 		break;
1651 	case DISABLE_KEY:
1652 		rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
1653 			"disable key delete one entry\n");
1654 		/*set local buf about wep key. */
1655 		if (vif->type == NL80211_IFTYPE_AP ||
1656 			vif->type == NL80211_IFTYPE_MESH_POINT) {
1657 			if (sta)
1658 				rtl_cam_del_entry(hw, sta->addr);
1659 		}
1660 		memset(rtlpriv->sec.key_buf[key_idx], 0, key->keylen);
1661 		rtlpriv->sec.key_len[key_idx] = 0;
1662 		eth_zero_addr(mac_addr);
1663 		/*
1664 		 *mac80211 will delete entries one by one,
1665 		 *so don't use rtl_cam_reset_all_entry
1666 		 *or clear all entry here.
1667 		 */
1668 		rtl_wait_tx_report_acked(hw, 500); /* wait 500ms for TX ack */
1669 
1670 		rtl_cam_delete_one_entry(hw, mac_addr, key_idx);
1671 		break;
1672 	default:
1673 		pr_err("cmd_err:%x!!!!:\n", cmd);
1674 	}
1675 out_unlock:
1676 	mutex_unlock(&rtlpriv->locks.conf_mutex);
1677 	rtlpriv->sec.being_setkey = false;
1678 	return err;
1679 }
1680 
1681 static void rtl_op_rfkill_poll(struct ieee80211_hw *hw)
1682 {
1683 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1684 
1685 	bool radio_state;
1686 	bool blocked;
1687 	u8 valid = 0;
1688 
1689 	if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
1690 		return;
1691 
1692 	mutex_lock(&rtlpriv->locks.conf_mutex);
1693 
1694 	/*if Radio On return true here */
1695 	radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
1696 
1697 	if (valid) {
1698 		if (unlikely(radio_state != rtlpriv->rfkill.rfkill_state)) {
1699 			rtlpriv->rfkill.rfkill_state = radio_state;
1700 
1701 			rtl_dbg(rtlpriv, COMP_RF, DBG_DMESG,
1702 				"wireless radio switch turned %s\n",
1703 				radio_state ? "on" : "off");
1704 
1705 			blocked = !rtlpriv->rfkill.rfkill_state;
1706 			wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
1707 		}
1708 	}
1709 
1710 	mutex_unlock(&rtlpriv->locks.conf_mutex);
1711 }
1712 
1713 /* this function is called by mac80211 to flush tx buffer
1714  * before switch channle or power save, or tx buffer packet
1715  * maybe send after offchannel or rf sleep, this may cause
1716  * dis-association by AP */
1717 static void rtl_op_flush(struct ieee80211_hw *hw,
1718 			 struct ieee80211_vif *vif,
1719 			 u32 queues,
1720 			 bool drop)
1721 {
1722 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1723 
1724 	if (rtlpriv->intf_ops->flush)
1725 		rtlpriv->intf_ops->flush(hw, queues, drop);
1726 }
1727 
1728 static int rtl_op_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
1729 			  bool set)
1730 {
1731 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1732 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1733 
1734 	if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192CU)
1735 		schedule_work(&rtlpriv->works.update_beacon_work);
1736 
1737 	return 0;
1738 }
1739 
1740 /*	Description:
1741  *		This routine deals with the Power Configuration CMD
1742  *		 parsing for RTL8723/RTL8188E Series IC.
1743  *	Assumption:
1744  *		We should follow specific format that was released from HW SD.
1745  */
1746 bool rtl_hal_pwrseqcmdparsing(struct rtl_priv *rtlpriv, u8 cut_version,
1747 			      u8 faversion, u8 interface_type,
1748 			      struct wlan_pwr_cfg pwrcfgcmd[])
1749 {
1750 	struct wlan_pwr_cfg cfg_cmd;
1751 	bool polling_bit = false;
1752 	u32 ary_idx = 0;
1753 	u8 value = 0;
1754 	u32 offset = 0;
1755 	u32 polling_count = 0;
1756 	u32 max_polling_cnt = 5000;
1757 
1758 	do {
1759 		cfg_cmd = pwrcfgcmd[ary_idx];
1760 		rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1761 			"%s: offset(%#x),cut_msk(%#x), famsk(%#x), interface_msk(%#x), base(%#x), cmd(%#x), msk(%#x), value(%#x)\n",
1762 			__func__,
1763 			GET_PWR_CFG_OFFSET(cfg_cmd),
1764 					   GET_PWR_CFG_CUT_MASK(cfg_cmd),
1765 			GET_PWR_CFG_FAB_MASK(cfg_cmd),
1766 					     GET_PWR_CFG_INTF_MASK(cfg_cmd),
1767 			GET_PWR_CFG_BASE(cfg_cmd), GET_PWR_CFG_CMD(cfg_cmd),
1768 			GET_PWR_CFG_MASK(cfg_cmd), GET_PWR_CFG_VALUE(cfg_cmd));
1769 
1770 		if ((GET_PWR_CFG_FAB_MASK(cfg_cmd)&faversion) &&
1771 		    (GET_PWR_CFG_CUT_MASK(cfg_cmd)&cut_version) &&
1772 		    (GET_PWR_CFG_INTF_MASK(cfg_cmd)&interface_type)) {
1773 			switch (GET_PWR_CFG_CMD(cfg_cmd)) {
1774 			case PWR_CMD_READ:
1775 				rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1776 					"rtl_hal_pwrseqcmdparsing(): PWR_CMD_READ\n");
1777 				break;
1778 			case PWR_CMD_WRITE:
1779 				rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1780 					"%s(): PWR_CMD_WRITE\n", __func__);
1781 				offset = GET_PWR_CFG_OFFSET(cfg_cmd);
1782 
1783 				/*Read the value from system register*/
1784 				value = rtl_read_byte(rtlpriv, offset);
1785 				value &= (~(GET_PWR_CFG_MASK(cfg_cmd)));
1786 				value |= (GET_PWR_CFG_VALUE(cfg_cmd) &
1787 					  GET_PWR_CFG_MASK(cfg_cmd));
1788 
1789 				/*Write the value back to system register*/
1790 				rtl_write_byte(rtlpriv, offset, value);
1791 				break;
1792 			case PWR_CMD_POLLING:
1793 				rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1794 					"rtl_hal_pwrseqcmdparsing(): PWR_CMD_POLLING\n");
1795 				polling_bit = false;
1796 				offset = GET_PWR_CFG_OFFSET(cfg_cmd);
1797 
1798 				do {
1799 					value = rtl_read_byte(rtlpriv, offset);
1800 
1801 					value &= GET_PWR_CFG_MASK(cfg_cmd);
1802 					if (value ==
1803 					    (GET_PWR_CFG_VALUE(cfg_cmd) &
1804 					     GET_PWR_CFG_MASK(cfg_cmd)))
1805 						polling_bit = true;
1806 					else
1807 						udelay(10);
1808 
1809 					if (polling_count++ > max_polling_cnt)
1810 						return false;
1811 				} while (!polling_bit);
1812 				break;
1813 			case PWR_CMD_DELAY:
1814 				rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1815 					"%s: PWR_CMD_DELAY\n", __func__);
1816 				if (GET_PWR_CFG_VALUE(cfg_cmd) ==
1817 				    PWRSEQ_DELAY_US)
1818 					udelay(GET_PWR_CFG_OFFSET(cfg_cmd));
1819 				else
1820 					mdelay(GET_PWR_CFG_OFFSET(cfg_cmd));
1821 				break;
1822 			case PWR_CMD_END:
1823 				rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1824 					"%s: PWR_CMD_END\n", __func__);
1825 				return true;
1826 			default:
1827 				WARN_ONCE(true,
1828 					  "rtlwifi: rtl_hal_pwrseqcmdparsing(): Unknown CMD!!\n");
1829 				break;
1830 			}
1831 		}
1832 		ary_idx++;
1833 	} while (1);
1834 
1835 	return true;
1836 }
1837 EXPORT_SYMBOL(rtl_hal_pwrseqcmdparsing);
1838 
1839 bool rtl_cmd_send_packet(struct ieee80211_hw *hw, struct sk_buff *skb)
1840 {
1841 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1842 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1843 	struct rtl8192_tx_ring *ring;
1844 	struct rtl_tx_desc *pdesc;
1845 	unsigned long flags;
1846 	struct sk_buff *pskb = NULL;
1847 
1848 	ring = &rtlpci->tx_ring[BEACON_QUEUE];
1849 
1850 	spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
1851 	pskb = __skb_dequeue(&ring->queue);
1852 	if (pskb)
1853 		dev_kfree_skb_irq(pskb);
1854 
1855 	/*this is wrong, fill_tx_cmddesc needs update*/
1856 	pdesc = &ring->desc[0];
1857 
1858 	rtlpriv->cfg->ops->fill_tx_cmddesc(hw, (u8 *)pdesc, skb);
1859 
1860 	__skb_queue_tail(&ring->queue, skb);
1861 
1862 	spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
1863 
1864 	rtlpriv->cfg->ops->tx_polling(hw, BEACON_QUEUE);
1865 
1866 	return true;
1867 }
1868 EXPORT_SYMBOL(rtl_cmd_send_packet);
1869 
1870 void rtl_init_sw_leds(struct ieee80211_hw *hw)
1871 {
1872 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1873 
1874 	rtlpriv->ledctl.sw_led0 = LED_PIN_LED0;
1875 	rtlpriv->ledctl.sw_led1 = LED_PIN_LED1;
1876 }
1877 EXPORT_SYMBOL(rtl_init_sw_leds);
1878 
1879 const struct ieee80211_ops rtl_ops = {
1880 	.add_chanctx = ieee80211_emulate_add_chanctx,
1881 	.remove_chanctx = ieee80211_emulate_remove_chanctx,
1882 	.change_chanctx = ieee80211_emulate_change_chanctx,
1883 	.switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx,
1884 	.start = rtl_op_start,
1885 	.stop = rtl_op_stop,
1886 	.tx = rtl_op_tx,
1887 	.wake_tx_queue = ieee80211_handle_wake_tx_queue,
1888 	.add_interface = rtl_op_add_interface,
1889 	.remove_interface = rtl_op_remove_interface,
1890 	.change_interface = rtl_op_change_interface,
1891 #ifdef CONFIG_PM
1892 	.suspend = rtl_op_suspend,
1893 	.resume = rtl_op_resume,
1894 #endif
1895 	.config = rtl_op_config,
1896 	.configure_filter = rtl_op_configure_filter,
1897 	.set_key = rtl_op_set_key,
1898 	.conf_tx = rtl_op_conf_tx,
1899 	.bss_info_changed = rtl_op_bss_info_changed,
1900 	.get_tsf = rtl_op_get_tsf,
1901 	.set_tsf = rtl_op_set_tsf,
1902 	.reset_tsf = rtl_op_reset_tsf,
1903 	.sta_notify = rtl_op_sta_notify,
1904 	.ampdu_action = rtl_op_ampdu_action,
1905 	.sw_scan_start = rtl_op_sw_scan_start,
1906 	.sw_scan_complete = rtl_op_sw_scan_complete,
1907 	.rfkill_poll = rtl_op_rfkill_poll,
1908 	.sta_add = rtl_op_sta_add,
1909 	.sta_remove = rtl_op_sta_remove,
1910 	.flush = rtl_op_flush,
1911 	.set_tim = rtl_op_set_tim,
1912 };
1913 EXPORT_SYMBOL_GPL(rtl_ops);
1914 
1915 bool rtl_btc_status_false(void)
1916 {
1917 	return false;
1918 }
1919 EXPORT_SYMBOL_GPL(rtl_btc_status_false);
1920 
1921 void rtl_dm_diginit(struct ieee80211_hw *hw, u32 cur_igvalue)
1922 {
1923 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1924 	struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
1925 
1926 	dm_digtable->dig_enable_flag = true;
1927 	dm_digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_MAX;
1928 	dm_digtable->cur_igvalue = cur_igvalue;
1929 	dm_digtable->pre_igvalue = 0;
1930 	dm_digtable->cur_sta_cstate = DIG_STA_DISCONNECT;
1931 	dm_digtable->presta_cstate = DIG_STA_DISCONNECT;
1932 	dm_digtable->curmultista_cstate = DIG_MULTISTA_DISCONNECT;
1933 	dm_digtable->rssi_lowthresh = DM_DIG_THRESH_LOW;
1934 	dm_digtable->rssi_highthresh = DM_DIG_THRESH_HIGH;
1935 	dm_digtable->fa_lowthresh = DM_FALSEALARM_THRESH_LOW;
1936 	dm_digtable->fa_highthresh = DM_FALSEALARM_THRESH_HIGH;
1937 	dm_digtable->rx_gain_max = DM_DIG_MAX;
1938 	dm_digtable->rx_gain_min = DM_DIG_MIN;
1939 	dm_digtable->back_val = DM_DIG_BACKOFF_DEFAULT;
1940 	dm_digtable->back_range_max = DM_DIG_BACKOFF_MAX;
1941 	dm_digtable->back_range_min = DM_DIG_BACKOFF_MIN;
1942 	dm_digtable->pre_cck_cca_thres = 0xff;
1943 	dm_digtable->cur_cck_cca_thres = 0x83;
1944 	dm_digtable->forbidden_igi = DM_DIG_MIN;
1945 	dm_digtable->large_fa_hit = 0;
1946 	dm_digtable->recover_cnt = 0;
1947 	dm_digtable->dig_min_0 = 0x25;
1948 	dm_digtable->dig_min_1 = 0x25;
1949 	dm_digtable->media_connect_0 = false;
1950 	dm_digtable->media_connect_1 = false;
1951 	rtlpriv->dm.dm_initialgain_enable = true;
1952 	dm_digtable->bt30_cur_igi = 0x32;
1953 	dm_digtable->pre_cck_pd_state = CCK_PD_STAGE_MAX;
1954 	dm_digtable->cur_cck_pd_state = CCK_PD_STAGE_LOWRSSI;
1955 	dm_digtable->pre_cck_fa_state = 0;
1956 	dm_digtable->cur_cck_fa_state = 0;
1957 }
1958 EXPORT_SYMBOL(rtl_dm_diginit);
1959