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