Linux Audio

Check our new training course

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