core.c 51.4 KB
Newer Older
L
Larry Finger 已提交
1 2
/******************************************************************************
 *
L
Larry Finger 已提交
3
 * Copyright(c) 2009-2012  Realtek Corporation.
L
Larry Finger 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * The full GNU General Public License is included in this distribution in the
 * file called LICENSE.
 *
 * Contact Information:
 * wlanfae <wlanfae@realtek.com>
 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
 * Hsinchu 300, Taiwan.
 *
 * Larry Finger <Larry.Finger@lwfinger.net>
23
 *
L
Larry Finger 已提交
24 25 26 27 28 29 30
 *****************************************************************************/

#include "wifi.h"
#include "core.h"
#include "cam.h"
#include "base.h"
#include "ps.h"
31
#include "pwrseqcmd.h"
L
Larry Finger 已提交
32

33 34
#include "btcoexist/rtl_btc.h"
#include <linux/firmware.h>
35
#include <linux/export.h>
36
#include <net/cfg80211.h>
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
void rtl_addr_delay(u32 addr)
{
	if (addr == 0xfe)
		mdelay(50);
	else if (addr == 0xfd)
		mdelay(5);
	else if (addr == 0xfc)
		mdelay(1);
	else if (addr == 0xfb)
		udelay(50);
	else if (addr == 0xfa)
		udelay(5);
	else if (addr == 0xf9)
		udelay(1);
}
EXPORT_SYMBOL(rtl_addr_delay);

void rtl_rfreg_delay(struct ieee80211_hw *hw, enum radio_path rfpath, u32 addr,
		     u32 mask, u32 data)
{
	if (addr == 0xfe) {
		mdelay(50);
	} else if (addr == 0xfd) {
		mdelay(5);
	} else if (addr == 0xfc) {
		mdelay(1);
	} else if (addr == 0xfb) {
		udelay(50);
	} else if (addr == 0xfa) {
		udelay(5);
	} else if (addr == 0xf9) {
		udelay(1);
	} else {
		rtl_set_rfreg(hw, rfpath, addr, mask, data);
		udelay(1);
	}
}
EXPORT_SYMBOL(rtl_rfreg_delay);

void rtl_bb_delay(struct ieee80211_hw *hw, u32 addr, u32 data)
{
	if (addr == 0xfe) {
		mdelay(50);
	} else if (addr == 0xfd) {
		mdelay(5);
	} else if (addr == 0xfc) {
		mdelay(1);
	} else if (addr == 0xfb) {
		udelay(50);
	} else if (addr == 0xfa) {
		udelay(5);
	} else if (addr == 0xf9) {
		udelay(1);
	} else {
		rtl_set_bbreg(hw, addr, MASKDWORD, data);
		udelay(1);
	}
}
EXPORT_SYMBOL(rtl_bb_delay);

98 99 100 101 102 103 104
void rtl_fw_cb(const struct firmware *firmware, void *context)
{
	struct ieee80211_hw *hw = context;
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	int err;

	RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
105
		 "Firmware callback routine entered!\n");
106 107
	complete(&rtlpriv->firmware_loading_complete);
	if (!firmware) {
108 109 110 111 112 113 114 115 116
		if (rtlpriv->cfg->alt_fw_name) {
			err = request_firmware(&firmware,
					       rtlpriv->cfg->alt_fw_name,
					       rtlpriv->io.dev);
			pr_info("Loading alternative firmware %s\n",
				rtlpriv->cfg->alt_fw_name);
			if (!err)
				goto found_alt;
		}
117 118 119 120
		pr_err("Firmware %s not available\n", rtlpriv->cfg->fw_name);
		rtlpriv->max_fw_size = 0;
		return;
	}
121
found_alt:
122 123 124 125 126 127 128 129 130 131 132 133
	if (firmware->size > rtlpriv->max_fw_size) {
		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
			 "Firmware is too big!\n");
		release_firmware(firmware);
		return;
	}
	memcpy(rtlpriv->rtlhal.pfirmware, firmware->data, firmware->size);
	rtlpriv->rtlhal.fwsize = firmware->size;
	release_firmware(firmware);
}
EXPORT_SYMBOL(rtl_fw_cb);

L
Larry Finger 已提交
134 135 136
/*mutex for start & stop is must here. */
static int rtl_op_start(struct ieee80211_hw *hw)
{
137
	int err = 0;
L
Larry Finger 已提交
138 139 140 141 142 143 144
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));

	if (!is_hal_stop(rtlhal))
		return 0;
	if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
		return 0;
145
	mutex_lock(&rtlpriv->locks.conf_mutex);
L
Larry Finger 已提交
146
	err = rtlpriv->intf_ops->adapter_start(hw);
147 148
	if (!err)
		rtl_watch_dog_timer_callback((unsigned long)hw);
149
	mutex_unlock(&rtlpriv->locks.conf_mutex);
L
Larry Finger 已提交
150 151 152 153 154 155 156 157 158
	return err;
}

static void rtl_op_stop(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
159
	bool support_remote_wakeup = false;
L
Larry Finger 已提交
160 161 162 163

	if (is_hal_stop(rtlhal))
		return;

164 165
	rtlpriv->cfg->ops->get_hw_reg(hw, HAL_DEF_WOWLAN,
				      (u8 *)(&support_remote_wakeup));
166 167 168 169
	/* here is must, because adhoc do stop and start,
	 * but stop with RFOFF may cause something wrong,
	 * like adhoc TP
	 */
170
	if (unlikely(ppsc->rfpwr_state == ERFOFF))
L
Larry Finger 已提交
171 172
		rtl_ips_nic_on(hw);

173
	mutex_lock(&rtlpriv->locks.conf_mutex);
174 175 176 177 178 179
	/* if wowlan supported, DON'T clear connected info */
	if (!(support_remote_wakeup &&
	      rtlhal->enter_pnp_sleep)) {
		mac->link_state = MAC80211_NOLINK;
		memset(mac->bssid, 0, 6);
		mac->vendor = PEER_UNKNOWN;
L
Larry Finger 已提交
180

181 182
		/* reset sec info */
		rtl_cam_reset_sec_info(hw);
L
Larry Finger 已提交
183

184 185
		rtl_deinit_deferred_work(hw);
	}
L
Larry Finger 已提交
186 187
	rtlpriv->intf_ops->adapter_stop(hw);

188
	mutex_unlock(&rtlpriv->locks.conf_mutex);
L
Larry Finger 已提交
189 190
}

191 192 193
static void rtl_op_tx(struct ieee80211_hw *hw,
		      struct ieee80211_tx_control *control,
		      struct sk_buff *skb)
L
Larry Finger 已提交
194 195 196 197
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
198 199
	struct rtl_tcb_desc tcb_desc;
	memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
L
Larry Finger 已提交
200 201 202 203 204 205 206

	if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
		goto err_free;

	if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
		goto err_free;

207 208
	if (!rtlpriv->intf_ops->waitq_insert(hw, control->sta, skb))
		rtlpriv->intf_ops->adapter_tx(hw, control->sta, skb, &tcb_desc);
209
	return;
L
Larry Finger 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223

err_free:
	dev_kfree_skb_any(skb);
}

static int rtl_op_add_interface(struct ieee80211_hw *hw,
		struct ieee80211_vif *vif)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
	int err = 0;

	if (mac->vif) {
		RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
224
			 "vif has been set!! mac->vif = 0x%p\n", mac->vif);
L
Larry Finger 已提交
225 226 227
		return -EOPNOTSUPP;
	}

228 229
	vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;

L
Larry Finger 已提交
230 231
	rtl_ips_nic_on(hw);

232
	mutex_lock(&rtlpriv->locks.conf_mutex);
233 234 235 236
	switch (ieee80211_vif_type_p2p(vif)) {
	case NL80211_IFTYPE_P2P_CLIENT:
		mac->p2p = P2P_ROLE_CLIENT;
		/*fall through*/
L
Larry Finger 已提交
237 238 239
	case NL80211_IFTYPE_STATION:
		if (mac->beacon_enabled == 1) {
			RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
240
				 "NL80211_IFTYPE_STATION\n");
L
Larry Finger 已提交
241 242
			mac->beacon_enabled = 0;
			rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
243
					rtlpriv->cfg->maps[RTL_IBSS_INT_MASKS]);
L
Larry Finger 已提交
244 245 246 247
		}
		break;
	case NL80211_IFTYPE_ADHOC:
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
248
			 "NL80211_IFTYPE_ADHOC\n");
L
Larry Finger 已提交
249 250 251

		mac->link_state = MAC80211_LINKED;
		rtlpriv->cfg->ops->set_bcn_reg(hw);
252 253 254 255 256
		if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
			mac->basic_rates = 0xfff;
		else
			mac->basic_rates = 0xff0;
		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
257
				(u8 *)(&mac->basic_rates));
258

L
Larry Finger 已提交
259
		break;
260 261 262
	case NL80211_IFTYPE_P2P_GO:
		mac->p2p = P2P_ROLE_GO;
		/*fall through*/
L
Larry Finger 已提交
263 264
	case NL80211_IFTYPE_AP:
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
265
			 "NL80211_IFTYPE_AP\n");
266 267 268 269 270 271 272 273

		mac->link_state = MAC80211_LINKED;
		rtlpriv->cfg->ops->set_bcn_reg(hw);
		if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
			mac->basic_rates = 0xfff;
		else
			mac->basic_rates = 0xff0;
		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
274
					      (u8 *)(&mac->basic_rates));
L
Larry Finger 已提交
275
		break;
276 277 278 279 280 281 282 283 284 285 286 287 288
	case NL80211_IFTYPE_MESH_POINT:
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
			 "NL80211_IFTYPE_MESH_POINT\n");

		mac->link_state = MAC80211_LINKED;
		rtlpriv->cfg->ops->set_bcn_reg(hw);
		if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
			mac->basic_rates = 0xfff;
		else
			mac->basic_rates = 0xff0;
		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
				(u8 *)(&mac->basic_rates));
		break;
L
Larry Finger 已提交
289 290
	default:
		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
291
			 "operation mode %d is not support!\n", vif->type);
L
Larry Finger 已提交
292 293 294 295
		err = -EOPNOTSUPP;
		goto out;
	}

296 297 298 299 300 301 302
	if (mac->p2p) {
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
			 "p2p role %x\n", vif->type);
		mac->basic_rates = 0xff0;/*disable cck rate for p2p*/
		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
				(u8 *)(&mac->basic_rates));
	}
L
Larry Finger 已提交
303 304 305 306 307 308 309
	mac->vif = vif;
	mac->opmode = vif->type;
	rtlpriv->cfg->ops->set_network_type(hw, vif->type);
	memcpy(mac->mac_addr, vif->addr, ETH_ALEN);
	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ETHER_ADDR, mac->mac_addr);

out:
310
	mutex_unlock(&rtlpriv->locks.conf_mutex);
L
Larry Finger 已提交
311 312 313 314 315 316 317 318 319
	return err;
}

static void rtl_op_remove_interface(struct ieee80211_hw *hw,
		struct ieee80211_vif *vif)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));

320
	mutex_lock(&rtlpriv->locks.conf_mutex);
L
Larry Finger 已提交
321 322

	/* Free beacon resources */
323 324 325
	if ((vif->type == NL80211_IFTYPE_AP) ||
	    (vif->type == NL80211_IFTYPE_ADHOC) ||
	    (vif->type == NL80211_IFTYPE_MESH_POINT)) {
L
Larry Finger 已提交
326 327 328
		if (mac->beacon_enabled == 1) {
			mac->beacon_enabled = 0;
			rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
329
					rtlpriv->cfg->maps[RTL_IBSS_INT_MASKS]);
L
Larry Finger 已提交
330 331 332 333 334 335 336
		}
	}

	/*
	 *Note: We assume NL80211_IFTYPE_UNSPECIFIED as
	 *NO LINK for our hardware.
	 */
337
	mac->p2p = 0;
L
Larry Finger 已提交
338 339
	mac->vif = NULL;
	mac->link_state = MAC80211_NOLINK;
340
	memset(mac->bssid, 0, ETH_ALEN);
341
	mac->vendor = PEER_UNKNOWN;
L
Larry Finger 已提交
342 343
	mac->opmode = NL80211_IFTYPE_UNSPECIFIED;
	rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
344

345
	mutex_unlock(&rtlpriv->locks.conf_mutex);
L
Larry Finger 已提交
346
}
347
static int rtl_op_change_interface(struct ieee80211_hw *hw,
348 349
				   struct ieee80211_vif *vif,
				   enum nl80211_iftype new_type, bool p2p)
350 351 352 353 354 355 356 357 358
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	int ret;
	rtl_op_remove_interface(hw, vif);

	vif->type = new_type;
	vif->p2p = p2p;
	ret = rtl_op_add_interface(hw, vif);
	RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
359
		 "p2p  %x\n", p2p);
360 361 362
	return ret;
}

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
#ifdef CONFIG_PM
static u16 crc16_ccitt(u8 data, u16 crc)
{
	u8 shift_in, data_bit, crc_bit11, crc_bit4, crc_bit15;
	u8 i;
	u16 result;

	for (i = 0; i < 8; i++) {
		crc_bit15 = ((crc & BIT(15)) ? 1 : 0);
		data_bit  = (data & (BIT(0) << i) ? 1 : 0);
		shift_in = crc_bit15 ^ data_bit;

		result = crc << 1;
		if (shift_in == 0)
			result &= (~BIT(0));
		else
			result |= BIT(0);

		crc_bit11 = ((crc & BIT(11)) ? 1 : 0) ^ shift_in;
		if (crc_bit11 == 0)
			result &= (~BIT(12));
		else
			result |= BIT(12);

		crc_bit4 = ((crc & BIT(4)) ? 1 : 0) ^ shift_in;
		if (crc_bit4 == 0)
			result &= (~BIT(5));
		else
			result |= BIT(5);

		crc = result;
	}

	return crc;
}

static u16 _calculate_wol_pattern_crc(u8 *pattern, u16 len)
{
	u16 crc = 0xffff;
	u32 i;

	for (i = 0; i < len; i++)
		crc = crc16_ccitt(pattern[i], crc);

	crc = ~crc;

	return crc;
}

static void _rtl_add_wowlan_patterns(struct ieee80211_hw *hw,
				     struct cfg80211_wowlan *wow)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_mac *mac = &rtlpriv->mac80211;
	struct cfg80211_pkt_pattern *patterns = wow->patterns;
	struct rtl_wow_pattern rtl_pattern;
	const u8 *pattern_os, *mask_os;
	u8 mask[MAX_WOL_BIT_MASK_SIZE] = {0};
	u8 content[MAX_WOL_PATTERN_SIZE] = {0};
	u8 broadcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
	u8 multicast_addr1[2] = {0x33, 0x33};
	u8 multicast_addr2[3] = {0x01, 0x00, 0x5e};
	u8 i, mask_len;
	u16 j, len;

	for (i = 0; i < wow->n_patterns; i++) {
		memset(&rtl_pattern, 0, sizeof(struct rtl_wow_pattern));
		memset(mask, 0, MAX_WOL_BIT_MASK_SIZE);
		if (patterns[i].pattern_len > MAX_WOL_PATTERN_SIZE) {
			RT_TRACE(rtlpriv, COMP_POWER, DBG_WARNING,
				 "Pattern[%d] is too long\n", i);
			continue;
		}
		pattern_os = patterns[i].pattern;
		mask_len = DIV_ROUND_UP(patterns[i].pattern_len, 8);
		mask_os = patterns[i].mask;
		RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
			      "pattern content\n", pattern_os,
			       patterns[i].pattern_len);
		RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
			      "mask content\n", mask_os, mask_len);
		/* 1. unicast? multicast? or broadcast? */
		if (memcmp(pattern_os, broadcast_addr, 6) == 0)
			rtl_pattern.type = BROADCAST_PATTERN;
		else if (memcmp(pattern_os, multicast_addr1, 2) == 0 ||
			 memcmp(pattern_os, multicast_addr2, 3) == 0)
			rtl_pattern.type = MULTICAST_PATTERN;
		else if  (memcmp(pattern_os, mac->mac_addr, 6) == 0)
			rtl_pattern.type = UNICAST_PATTERN;
		else
			rtl_pattern.type = UNKNOWN_TYPE;

		/* 2. translate mask_from_os to mask_for_hw */

/******************************************************************************
 * pattern from OS uses 'ethenet frame', like this:

		   |    6   |    6   |   2  |     20    |  Variable  |	4  |
		   |--------+--------+------+-----------+------------+-----|
		   |    802.3 Mac Header    | IP Header | TCP Packet | FCS |
		   |   DA   |   SA   | Type |

 * BUT, packet catched by our HW is in '802.11 frame', begin from LLC,

	|     24 or 30      |    6   |   2  |     20    |  Variable  |  4  |
	|-------------------+--------+------+-----------+------------+-----|
	| 802.11 MAC Header |       LLC     | IP Header | TCP Packet | FCS |
			    | Others | Tpye |

 * Therefore, we need translate mask_from_OS to mask_to_hw.
 * We should left-shift mask by 6 bits, then set the new bit[0~5] = 0,
 * because new mask[0~5] means 'SA', but our HW packet begins from LLC,
 * bit[0~5] corresponds to first 6 Bytes in LLC, they just don't match.
 ******************************************************************************/

		/* Shift 6 bits */
		for (j = 0; j < mask_len - 1; j++) {
			mask[j] = mask_os[j] >> 6;
			mask[j] |= (mask_os[j + 1] & 0x3F) << 2;
		}
		mask[j] = (mask_os[j] >> 6) & 0x3F;
		/* Set bit 0-5 to zero */
		mask[0] &= 0xC0;

		RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
			      "mask to hw\n", mask, mask_len);
		for (j = 0; j < (MAX_WOL_BIT_MASK_SIZE + 1) / 4; j++) {
			rtl_pattern.mask[j] = mask[j * 4];
			rtl_pattern.mask[j] |= (mask[j * 4 + 1] << 8);
			rtl_pattern.mask[j] |= (mask[j * 4 + 2] << 16);
			rtl_pattern.mask[j] |= (mask[j * 4 + 3] << 24);
		}

		/* To get the wake up pattern from the mask.
		 * We do not count first 12 bits which means
		 * DA[6] and SA[6] in the pattern to match HW design.
		 */
		len = 0;
		for (j = 12; j < patterns[i].pattern_len; j++) {
			if ((mask_os[j / 8] >> (j % 8)) & 0x01) {
				content[len] = pattern_os[j];
				len++;
			}
		}

		RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
			      "pattern to hw\n", content, len);
		/* 3. calculate crc */
		rtl_pattern.crc = _calculate_wol_pattern_crc(content, len);
		RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
			 "CRC_Remainder = 0x%x", rtl_pattern.crc);

		/* 4. write crc & mask_for_hw to hw */
		rtlpriv->cfg->ops->add_wowlan_pattern(hw, &rtl_pattern, i);
	}
	rtl_write_byte(rtlpriv, 0x698, wow->n_patterns);
}

static int rtl_op_suspend(struct ieee80211_hw *hw,
			  struct cfg80211_wowlan *wow)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
	struct timeval ts;

	RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG, "\n");
	if (WARN_ON(!wow))
		return -EINVAL;

	/* to resolve s4 can not wake up*/
	do_gettimeofday(&ts);
	rtlhal->last_suspend_sec = ts.tv_sec;

	if ((ppsc->wo_wlan_mode & WAKE_ON_PATTERN_MATCH) && wow->n_patterns)
		_rtl_add_wowlan_patterns(hw, wow);

	rtlhal->driver_is_goingto_unload = true;
	rtlhal->enter_pnp_sleep = true;

	rtl_lps_leave(hw);
	rtl_op_stop(hw);
	device_set_wakeup_enable(wiphy_dev(hw->wiphy), true);
	return 0;
}

static int rtl_op_resume(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
	struct timeval ts;

	RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG, "\n");
	rtlhal->driver_is_goingto_unload = false;
	rtlhal->enter_pnp_sleep = false;
	rtlhal->wake_from_pnp_sleep = true;

	/* to resovle s4 can not wake up*/
	do_gettimeofday(&ts);
	if (ts.tv_sec - rtlhal->last_suspend_sec < 5)
		return -1;

	rtl_op_start(hw);
	device_set_wakeup_enable(wiphy_dev(hw->wiphy), false);
	ieee80211_resume_disconnect(mac->vif);
	rtlhal->wake_from_pnp_sleep = false;
	return 0;
}
#endif

L
Larry Finger 已提交
574 575 576 577 578 579 580 581
static int rtl_op_config(struct ieee80211_hw *hw, u32 changed)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
	struct ieee80211_conf *conf = &hw->conf;

582 583 584
	if (mac->skip_scan)
		return 1;

585
	mutex_lock(&rtlpriv->locks.conf_mutex);
586
	if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {	/* BIT(2)*/
L
Larry Finger 已提交
587
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
588
			 "IEEE80211_CONF_CHANGE_LISTEN_INTERVAL\n");
L
Larry Finger 已提交
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
	}

	/*For IPS */
	if (changed & IEEE80211_CONF_CHANGE_IDLE) {
		if (hw->conf.flags & IEEE80211_CONF_IDLE)
			rtl_ips_nic_off(hw);
		else
			rtl_ips_nic_on(hw);
	} else {
		/*
		 *although rfoff may not cause by ips, but we will
		 *check the reason in set_rf_power_state function
		 */
		if (unlikely(ppsc->rfpwr_state == ERFOFF))
			rtl_ips_nic_on(hw);
	}

	/*For LPS */
	if (changed & IEEE80211_CONF_CHANGE_PS) {
608 609 610 611 612 613 614 615 616 617 618
		cancel_delayed_work(&rtlpriv->works.ps_work);
		cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
		if (conf->flags & IEEE80211_CONF_PS) {
			rtlpriv->psc.sw_ps_enabled = true;
			/* sleep here is must, or we may recv the beacon and
			 * cause mac80211 into wrong ps state, this will cause
			 * power save nullfunc send fail, and further cause
			 * pkt loss, So sleep must quickly but not immediatly
			 * because that will cause nullfunc send by mac80211
			 * fail, and cause pkt loss, we have tested that 5mA
			 * is worked very well */
619
			if (!rtlpriv->psc.multi_buffered)
620
				queue_delayed_work(rtlpriv->works.rtl_wq,
621 622
						   &rtlpriv->works.ps_work,
						   MSECS(5));
623 624 625 626
		} else {
			rtl_swlps_rf_awake(hw);
			rtlpriv->psc.sw_ps_enabled = false;
		}
L
Larry Finger 已提交
627 628 629 630
	}

	if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
631 632
			 "IEEE80211_CONF_CHANGE_RETRY_LIMITS %x\n",
			 hw->conf.long_frame_max_tx_count);
L
Larry Finger 已提交
633 634 635
		mac->retry_long = hw->conf.long_frame_max_tx_count;
		mac->retry_short = hw->conf.long_frame_max_tx_count;
		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RETRY_LIMIT,
636
				(u8 *)(&hw->conf.long_frame_max_tx_count));
L
Larry Finger 已提交
637 638
	}

639 640
	if (changed & IEEE80211_CONF_CHANGE_CHANNEL &&
	    !rtlpriv->proximity.proxim_on) {
641
		struct ieee80211_channel *channel = hw->conf.chandef.chan;
642 643
		enum nl80211_chan_width width = hw->conf.chandef.width;
		enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
L
Larry Finger 已提交
644 645
		u8 wide_chan = (u8) channel->hw_value;

646 647 648 649
		/* channel_type is for 20&40M */
		if (width < NL80211_CHAN_WIDTH_80)
			channel_type =
				cfg80211_get_chandef_type(&hw->conf.chandef);
650 651 652 653
		if (mac->act_scanning)
			mac->n_channels++;

		if (rtlpriv->dm.supp_phymode_switch &&
654 655
			mac->link_state < MAC80211_LINKED &&
			!mac->act_scanning) {
656 657 658 659
			if (rtlpriv->cfg->ops->chk_switch_dmdp)
				rtlpriv->cfg->ops->chk_switch_dmdp(hw);
		}

L
Larry Finger 已提交
660 661 662 663 664 665 666 667 668
		/*
		 *because we should back channel to
		 *current_network.chan in in scanning,
		 *So if set_chan == current_network.chan
		 *we should set it.
		 *because mac80211 tell us wrong bw40
		 *info for cisco1253 bw20, so we modify
		 *it here based on UPPER & LOWER
		 */
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754

		if (width >= NL80211_CHAN_WIDTH_80) {
			if (width == NL80211_CHAN_WIDTH_80) {
				u32 center = hw->conf.chandef.center_freq1;
				u32 primary =
				(u32)hw->conf.chandef.chan->center_freq;

				rtlphy->current_chan_bw =
					HT_CHANNEL_WIDTH_80;
				mac->bw_80 = true;
				mac->bw_40 = true;
				if (center > primary) {
					mac->cur_80_prime_sc =
					PRIME_CHNL_OFFSET_LOWER;
					if (center - primary == 10) {
						mac->cur_40_prime_sc =
						PRIME_CHNL_OFFSET_UPPER;

						wide_chan += 2;
					} else if (center - primary == 30) {
						mac->cur_40_prime_sc =
						PRIME_CHNL_OFFSET_LOWER;

						wide_chan += 6;
					}
				} else {
					mac->cur_80_prime_sc =
					PRIME_CHNL_OFFSET_UPPER;
					if (primary - center == 10) {
						mac->cur_40_prime_sc =
						PRIME_CHNL_OFFSET_LOWER;

						wide_chan -= 2;
					} else if (primary - center == 30) {
						mac->cur_40_prime_sc =
						PRIME_CHNL_OFFSET_UPPER;

						wide_chan -= 6;
					}
				}
			}
		} else {
			switch (channel_type) {
			case NL80211_CHAN_HT20:
			case NL80211_CHAN_NO_HT:
					/* SC */
					mac->cur_40_prime_sc =
						PRIME_CHNL_OFFSET_DONT_CARE;
					rtlphy->current_chan_bw =
						HT_CHANNEL_WIDTH_20;
					mac->bw_40 = false;
					mac->bw_80 = false;
					break;
			case NL80211_CHAN_HT40MINUS:
					/* SC */
					mac->cur_40_prime_sc =
						PRIME_CHNL_OFFSET_UPPER;
					rtlphy->current_chan_bw =
						HT_CHANNEL_WIDTH_20_40;
					mac->bw_40 = true;
					mac->bw_80 = false;

					/*wide channel */
					wide_chan -= 2;

					break;
			case NL80211_CHAN_HT40PLUS:
					/* SC */
					mac->cur_40_prime_sc =
						PRIME_CHNL_OFFSET_LOWER;
					rtlphy->current_chan_bw =
						HT_CHANNEL_WIDTH_20_40;
					mac->bw_40 = true;
					mac->bw_80 = false;

					/*wide channel */
					wide_chan += 2;

					break;
			default:
					mac->bw_40 = false;
					mac->bw_80 = false;
					RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
						 "switch case not processed\n");
					break;
			}
L
Larry Finger 已提交
755 756 757 758
		}

		if (wide_chan <= 0)
			wide_chan = 1;
759

760
		/* In scanning, when before we offchannel we may send a ps=1
761 762 763 764 765 766
		 * null to AP, and then we may send a ps = 0 null to AP quickly,
		 * but first null may have caused AP to put lots of packet to
		 * hw tx buffer. These packets must be tx'd before we go off
		 * channel so we must delay more time to let AP flush these
		 * packets before going offchannel, or dis-association or
		 * delete BA will be caused by AP
767
		 */
768 769
		if (rtlpriv->mac80211.offchan_delay) {
			rtlpriv->mac80211.offchan_delay = false;
770 771
			mdelay(50);
		}
772

L
Larry Finger 已提交
773 774 775
		rtlphy->current_channel = wide_chan;

		rtlpriv->cfg->ops->switch_channel(hw);
776
		rtlpriv->cfg->ops->set_channel_access(hw);
777
		rtlpriv->cfg->ops->set_bw_mode(hw, channel_type);
L
Larry Finger 已提交
778 779
	}

780
	mutex_unlock(&rtlpriv->locks.conf_mutex);
L
Larry Finger 已提交
781 782 783 784 785

	return 0;
}

static void rtl_op_configure_filter(struct ieee80211_hw *hw,
786 787
				    unsigned int changed_flags,
				    unsigned int *new_flags, u64 multicast)
L
Larry Finger 已提交
788 789 790 791 792
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));

	*new_flags &= RTL_SUPPORTED_FILTERS;
793
	if (0 == changed_flags)
L
Larry Finger 已提交
794 795 796 797 798
		return;

	/*TODO: we disable broadcase now, so enable here */
	if (changed_flags & FIF_ALLMULTI) {
		if (*new_flags & FIF_ALLMULTI) {
799
			mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AM] |
L
Larry Finger 已提交
800 801
			    rtlpriv->cfg->maps[MAC_RCR_AB];
			RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
802
				 "Enable receive multicast frame\n");
L
Larry Finger 已提交
803
		} else {
804
			mac->rx_conf &= ~(rtlpriv->cfg->maps[MAC_RCR_AM] |
L
Larry Finger 已提交
805 806
					  rtlpriv->cfg->maps[MAC_RCR_AB]);
			RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
807
				 "Disable receive multicast frame\n");
L
Larry Finger 已提交
808 809 810 811 812
		}
	}

	if (changed_flags & FIF_FCSFAIL) {
		if (*new_flags & FIF_FCSFAIL) {
813
			mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACRC32];
L
Larry Finger 已提交
814
			RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
815
				 "Enable receive FCS error frame\n");
L
Larry Finger 已提交
816
		} else {
817
			mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACRC32];
L
Larry Finger 已提交
818
			RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
819
				 "Disable receive FCS error frame\n");
L
Larry Finger 已提交
820 821 822
		}
	}

823 824 825 826 827 828 829 830 831 832 833 834 835 836
	/* if ssid not set to hw don't check bssid
	 * here just used for linked scanning, & linked
	 * and nolink check bssid is set in set network_type
	 */
	if ((changed_flags & FIF_BCN_PRBRESP_PROMISC) &&
	    (mac->link_state >= MAC80211_LINKED)) {
		if (mac->opmode != NL80211_IFTYPE_AP &&
		    mac->opmode != NL80211_IFTYPE_MESH_POINT) {
			if (*new_flags & FIF_BCN_PRBRESP_PROMISC)
				rtlpriv->cfg->ops->set_chk_bssid(hw, false);
			else
				rtlpriv->cfg->ops->set_chk_bssid(hw, true);
		}
	}
L
Larry Finger 已提交
837 838 839

	if (changed_flags & FIF_CONTROL) {
		if (*new_flags & FIF_CONTROL) {
840
			mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACF];
L
Larry Finger 已提交
841 842

			RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
843
				 "Enable receive control frame.\n");
L
Larry Finger 已提交
844
		} else {
845
			mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACF];
L
Larry Finger 已提交
846
			RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
847
				 "Disable receive control frame.\n");
L
Larry Finger 已提交
848 849 850 851 852
		}
	}

	if (changed_flags & FIF_OTHER_BSS) {
		if (*new_flags & FIF_OTHER_BSS) {
853
			mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AAP];
L
Larry Finger 已提交
854
			RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
855
				 "Enable receive other BSS's frame.\n");
L
Larry Finger 已提交
856
		} else {
857
			mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_AAP];
L
Larry Finger 已提交
858
			RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
859
				 "Disable receive other BSS's frame.\n");
L
Larry Finger 已提交
860 861 862
		}
	}
}
863 864 865 866 867 868
static int rtl_op_sta_add(struct ieee80211_hw *hw,
			 struct ieee80211_vif *vif,
			 struct ieee80211_sta *sta)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
869
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
870 871 872
	struct rtl_sta_info *sta_entry;

	if (sta) {
873
		sta_entry = (struct rtl_sta_info *)sta->drv_priv;
874 875 876
		spin_lock_bh(&rtlpriv->locks.entry_list_lock);
		list_add_tail(&sta_entry->list, &rtlpriv->entry_list);
		spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
877 878 879 880
		if (rtlhal->current_bandtype == BAND_ON_2_4G) {
			sta_entry->wireless_mode = WIRELESS_MODE_G;
			if (sta->supp_rates[0] <= 0xf)
				sta_entry->wireless_mode = WIRELESS_MODE_B;
881
			if (sta->ht_cap.ht_supported)
882
				sta_entry->wireless_mode = WIRELESS_MODE_N_24G;
883 884 885

			if (vif->type == NL80211_IFTYPE_ADHOC)
				sta_entry->wireless_mode = WIRELESS_MODE_G;
886 887
		} else if (rtlhal->current_bandtype == BAND_ON_5G) {
			sta_entry->wireless_mode = WIRELESS_MODE_A;
888 889 890 891
			if (sta->ht_cap.ht_supported)
				sta_entry->wireless_mode = WIRELESS_MODE_N_5G;
			if (sta->vht_cap.vht_supported)
				sta_entry->wireless_mode = WIRELESS_MODE_AC_5G;
892

893 894 895 896 897 898
			if (vif->type == NL80211_IFTYPE_ADHOC)
				sta_entry->wireless_mode = WIRELESS_MODE_A;
		}
		/*disable cck rate for p2p*/
		if (mac->p2p)
			sta->supp_rates[0] &= 0xfffffff0;
L
Larry Finger 已提交
899

900
		memcpy(sta_entry->mac_addr, sta->addr, ETH_ALEN);
901
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
902
			"Add sta addr is %pM\n", sta->addr);
903 904
		rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0);
	}
905

906 907
	return 0;
}
908

909 910 911 912 913 914 915 916
static int rtl_op_sta_remove(struct ieee80211_hw *hw,
				struct ieee80211_vif *vif,
				struct ieee80211_sta *sta)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_sta_info *sta_entry;
	if (sta) {
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
917
			 "Remove sta addr is %pM\n", sta->addr);
918
		sta_entry = (struct rtl_sta_info *)sta->drv_priv;
919 920
		sta_entry->wireless_mode = 0;
		sta_entry->ratr_index = 0;
921 922 923
		spin_lock_bh(&rtlpriv->locks.entry_list_lock);
		list_del(&sta_entry->list);
		spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
924 925 926
	}
	return 0;
}
L
Larry Finger 已提交
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
static int _rtl_get_hal_qnum(u16 queue)
{
	int qnum;

	switch (queue) {
	case 0:
		qnum = AC3_VO;
		break;
	case 1:
		qnum = AC2_VI;
		break;
	case 2:
		qnum = AC0_BE;
		break;
	case 3:
		qnum = AC1_BK;
		break;
	default:
		qnum = AC0_BE;
		break;
	}
	return qnum;
}

/*
952 953
 *for mac80211 VO = 0, VI = 1, BE = 2, BK = 3
 *for rtl819x  BE = 0, BK = 1, VI = 2, VO = 3
L
Larry Finger 已提交
954
 */
955
static int rtl_op_conf_tx(struct ieee80211_hw *hw,
956 957
			  struct ieee80211_vif *vif, u16 queue,
			  const struct ieee80211_tx_queue_params *param)
L
Larry Finger 已提交
958 959 960 961 962 963 964
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
	int aci;

	if (queue >= AC_MAX) {
		RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
965
			 "queue number %d is incorrect!\n", queue);
L
Larry Finger 已提交
966 967 968 969 970
		return -EINVAL;
	}

	aci = _rtl_get_hal_qnum(queue);
	mac->ac[aci].aifs = param->aifs;
971 972 973
	mac->ac[aci].cw_min = cpu_to_le16(param->cw_min);
	mac->ac[aci].cw_max = cpu_to_le16(param->cw_max);
	mac->ac[aci].tx_op = cpu_to_le16(param->txop);
L
Larry Finger 已提交
974 975 976 977 978 979
	memcpy(&mac->edca_param[aci], param, sizeof(*param));
	rtlpriv->cfg->ops->set_qos(hw, aci);
	return 0;
}

static void rtl_op_bss_info_changed(struct ieee80211_hw *hw,
980 981 982
				    struct ieee80211_vif *vif,
				    struct ieee80211_bss_conf *bss_conf,
				    u32 changed)
L
Larry Finger 已提交
983 984
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
985
	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
L
Larry Finger 已提交
986 987 988
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));

989
	mutex_lock(&rtlpriv->locks.conf_mutex);
L
Larry Finger 已提交
990 991 992 993 994 995 996 997
	if ((vif->type == NL80211_IFTYPE_ADHOC) ||
	    (vif->type == NL80211_IFTYPE_AP) ||
	    (vif->type == NL80211_IFTYPE_MESH_POINT)) {
		if ((changed & BSS_CHANGED_BEACON) ||
		    (changed & BSS_CHANGED_BEACON_ENABLED &&
		     bss_conf->enable_beacon)) {
			if (mac->beacon_enabled == 0) {
				RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
998
					 "BSS_CHANGED_BEACON_ENABLED\n");
L
Larry Finger 已提交
999 1000 1001 1002 1003 1004

				/*start hw beacon interrupt. */
				/*rtlpriv->cfg->ops->set_bcn_reg(hw); */
				mac->beacon_enabled = 1;
				rtlpriv->cfg->ops->update_interrupt_mask(hw,
						rtlpriv->cfg->maps
1005
						[RTL_IBSS_INT_MASKS], 0);
1006 1007 1008

				if (rtlpriv->cfg->ops->linked_set_reg)
					rtlpriv->cfg->ops->linked_set_reg(hw);
L
Larry Finger 已提交
1009
			}
1010 1011
		}
		if ((changed & BSS_CHANGED_BEACON_ENABLED &&
1012
		    !bss_conf->enable_beacon)) {
L
Larry Finger 已提交
1013 1014
			if (mac->beacon_enabled == 1) {
				RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1015
					 "ADHOC DISABLE BEACON\n");
L
Larry Finger 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024

				mac->beacon_enabled = 0;
				rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
						rtlpriv->cfg->maps
						[RTL_IBSS_INT_MASKS]);
			}
		}
		if (changed & BSS_CHANGED_BEACON_INT) {
			RT_TRACE(rtlpriv, COMP_BEACON, DBG_TRACE,
1025
				 "BSS_CHANGED_BEACON_INT\n");
L
Larry Finger 已提交
1026 1027 1028 1029 1030 1031 1032
			mac->beacon_interval = bss_conf->beacon_int;
			rtlpriv->cfg->ops->set_bcn_intv(hw);
		}
	}

	/*TODO: reference to enum ieee80211_bss_change */
	if (changed & BSS_CHANGED_ASSOC) {
1033
		u8 mstatus;
L
Larry Finger 已提交
1034
		if (bss_conf->assoc) {
1035
			struct ieee80211_sta *sta = NULL;
1036 1037 1038
			u8 keep_alive = 10;

			mstatus = RT_MEDIA_CONNECT;
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
			/* we should reset all sec info & cam
			 * before set cam after linked, we should not
			 * reset in disassoc, that will cause tkip->wep
			 * fail because some flag will be wrong */
			/* reset sec info */
			rtl_cam_reset_sec_info(hw);
			/* reset cam to fix wep fail issue
			 * when change from wpa to wep */
			rtl_cam_reset_all_entry(hw);

L
Larry Finger 已提交
1049 1050 1051
			mac->link_state = MAC80211_LINKED;
			mac->cnt_after_linked = 0;
			mac->assoc_id = bss_conf->aid;
1052
			memcpy(mac->bssid, bss_conf->bssid, ETH_ALEN);
L
Larry Finger 已提交
1053

1054 1055
			if (rtlpriv->cfg->ops->linked_set_reg)
				rtlpriv->cfg->ops->linked_set_reg(hw);
1056

1057 1058
			rcu_read_lock();
			sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
1059 1060 1061 1062
			if (!sta) {
				rcu_read_unlock();
				goto out;
			}
1063 1064 1065 1066 1067
			RT_TRACE(rtlpriv, COMP_EASY_CONCURRENT, DBG_LOUD,
				 "send PS STATIC frame\n");
			if (rtlpriv->dm.supp_phymode_switch) {
				if (sta->ht_cap.ht_supported)
					rtl_send_smps_action(hw, sta,
1068
							IEEE80211_SMPS_STATIC);
1069
			}
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095

			if (rtlhal->current_bandtype == BAND_ON_5G) {
				mac->mode = WIRELESS_MODE_A;
			} else {
				if (sta->supp_rates[0] <= 0xf)
					mac->mode = WIRELESS_MODE_B;
				else
					mac->mode = WIRELESS_MODE_G;
			}

			if (sta->ht_cap.ht_supported) {
				if (rtlhal->current_bandtype == BAND_ON_2_4G)
					mac->mode = WIRELESS_MODE_N_24G;
				else
					mac->mode = WIRELESS_MODE_N_5G;
			}

			if (sta->vht_cap.vht_supported) {
				if (rtlhal->current_bandtype == BAND_ON_5G)
					mac->mode = WIRELESS_MODE_AC_5G;
				else
					mac->mode = WIRELESS_MODE_AC_24G;
			}

			if (vif->type == NL80211_IFTYPE_STATION && sta)
				rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0);
1096 1097
			rcu_read_unlock();

1098 1099 1100 1101 1102
			/* to avoid AP Disassociation caused by inactivity */
			rtlpriv->cfg->ops->set_hw_reg(hw,
						      HW_VAR_KEEP_ALIVE,
						      (u8 *)(&keep_alive));

L
Larry Finger 已提交
1103
			RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1104
				 "BSS_CHANGED_ASSOC\n");
L
Larry Finger 已提交
1105
		} else {
1106 1107
			mstatus = RT_MEDIA_DISCONNECT;

1108 1109 1110 1111
			if (mac->link_state == MAC80211_LINKED) {
				rtlpriv->enter_ps = false;
				schedule_work(&rtlpriv->works.lps_change_work);
			}
1112 1113
			if (ppsc->p2p_ps_info.p2p_ps_mode > P2P_PS_NONE)
				rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
L
Larry Finger 已提交
1114
			mac->link_state = MAC80211_NOLINK;
1115
			memset(mac->bssid, 0, ETH_ALEN);
1116
			mac->vendor = PEER_UNKNOWN;
1117
			mac->mode = 0;
L
Larry Finger 已提交
1118

1119 1120 1121 1122
			if (rtlpriv->dm.supp_phymode_switch) {
				if (rtlpriv->cfg->ops->chk_switch_dmdp)
					rtlpriv->cfg->ops->chk_switch_dmdp(hw);
			}
L
Larry Finger 已提交
1123
			RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1124
				 "BSS_CHANGED_UN_ASSOC\n");
L
Larry Finger 已提交
1125
		}
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
		rtlpriv->cfg->ops->set_network_type(hw, vif->type);
		/* For FW LPS:
		 * To tell firmware we have connected or disconnected
		 */
		rtlpriv->cfg->ops->set_hw_reg(hw,
					      HW_VAR_H2C_FW_JOINBSSRPT,
					      (u8 *)(&mstatus));
		ppsc->report_linked = (mstatus == RT_MEDIA_CONNECT) ?
				      true : false;

		if (rtlpriv->cfg->ops->get_btc_status())
			rtlpriv->btcoexist.btc_ops->btc_mediastatus_notify(
							rtlpriv, mstatus);
L
Larry Finger 已提交
1139 1140 1141 1142
	}

	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1143
			 "BSS_CHANGED_ERP_CTS_PROT\n");
L
Larry Finger 已提交
1144 1145 1146 1147 1148
		mac->use_cts_protect = bss_conf->use_cts_prot;
	}

	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
1149
			 "BSS_CHANGED_ERP_PREAMBLE use short preamble:%x\n",
1150
			  bss_conf->use_short_preamble);
L
Larry Finger 已提交
1151 1152 1153

		mac->short_preamble = bss_conf->use_short_preamble;
		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ACK_PREAMBLE,
1154
					      (u8 *)(&mac->short_preamble));
L
Larry Finger 已提交
1155 1156 1157 1158
	}

	if (changed & BSS_CHANGED_ERP_SLOT) {
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1159
			 "BSS_CHANGED_ERP_SLOT\n");
L
Larry Finger 已提交
1160 1161 1162 1163 1164 1165 1166

		if (bss_conf->use_short_slot)
			mac->slot_time = RTL_SLOT_TIME_9;
		else
			mac->slot_time = RTL_SLOT_TIME_20;

		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SLOT_TIME,
1167
					      (u8 *)(&mac->slot_time));
L
Larry Finger 已提交
1168 1169 1170
	}

	if (changed & BSS_CHANGED_HT) {
1171 1172 1173 1174 1175
		struct ieee80211_sta *sta = NULL;

		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
			 "BSS_CHANGED_HT\n");

1176
		rcu_read_lock();
1177
		sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
L
Larry Finger 已提交
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
		if (sta) {
			if (sta->ht_cap.ampdu_density >
			    mac->current_ampdu_density)
				mac->current_ampdu_density =
				    sta->ht_cap.ampdu_density;
			if (sta->ht_cap.ampdu_factor <
			    mac->current_ampdu_factor)
				mac->current_ampdu_factor =
				    sta->ht_cap.ampdu_factor;
		}
1188
		rcu_read_unlock();
L
Larry Finger 已提交
1189 1190

		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SHORTGI_DENSITY,
1191
					      (u8 *)(&mac->max_mss_density));
L
Larry Finger 已提交
1192 1193 1194 1195 1196 1197 1198 1199
		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_FACTOR,
					      &mac->current_ampdu_factor);
		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_MIN_SPACE,
					      &mac->current_ampdu_density);
	}

	if (changed & BSS_CHANGED_BSSID) {
		u32 basic_rates;
1200
		struct ieee80211_sta *sta = NULL;
L
Larry Finger 已提交
1201 1202

		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BSSID,
1203
					      (u8 *)bss_conf->bssid);
L
Larry Finger 已提交
1204

1205 1206
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
			 "bssid: %pM\n", bss_conf->bssid);
L
Larry Finger 已提交
1207

1208
		mac->vendor = PEER_UNKNOWN;
1209
		memcpy(mac->bssid, bss_conf->bssid, ETH_ALEN);
L
Larry Finger 已提交
1210

1211
		rcu_read_lock();
1212
		sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
1213 1214 1215 1216
		if (!sta) {
			rcu_read_unlock();
			goto out;
		}
L
Larry Finger 已提交
1217

1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
		if (rtlhal->current_bandtype == BAND_ON_5G) {
			mac->mode = WIRELESS_MODE_A;
		} else {
			if (sta->supp_rates[0] <= 0xf)
				mac->mode = WIRELESS_MODE_B;
			else
				mac->mode = WIRELESS_MODE_G;
		}

		if (sta->ht_cap.ht_supported) {
			if (rtlhal->current_bandtype == BAND_ON_2_4G)
L
Larry Finger 已提交
1229
				mac->mode = WIRELESS_MODE_N_24G;
1230 1231 1232
			else
				mac->mode = WIRELESS_MODE_N_5G;
		}
L
Larry Finger 已提交
1233

1234 1235 1236 1237 1238 1239 1240
		if (sta->vht_cap.vht_supported) {
			if (rtlhal->current_bandtype == BAND_ON_5G)
				mac->mode = WIRELESS_MODE_AC_5G;
			else
				mac->mode = WIRELESS_MODE_AC_24G;
		}

1241 1242
		/* just station need it, because ibss & ap mode will
		 * set in sta_add, and will be NULL here */
1243
		if (vif->type == NL80211_IFTYPE_STATION) {
1244
			struct rtl_sta_info *sta_entry;
1245
			sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
			sta_entry->wireless_mode = mac->mode;
		}

		if (sta->ht_cap.ht_supported) {
			mac->ht_enable = true;

			/*
			 * for cisco 1252 bw20 it's wrong
			 * if (ht_cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
			 *	mac->bw_40 = true;
			 * }
			 * */
L
Larry Finger 已提交
1258 1259
		}

1260 1261 1262
		if (sta->vht_cap.vht_supported)
			mac->vht_enable = true;

L
Larry Finger 已提交
1263
		if (changed & BSS_CHANGED_BASIC_RATES) {
1264
			/* for 5G must << RATE_6M_INDEX = 4,
1265 1266 1267
			 * because 5G have no cck rate*/
			if (rtlhal->current_bandtype == BAND_ON_5G)
				basic_rates = sta->supp_rates[1] << 4;
L
Larry Finger 已提交
1268
			else
1269
				basic_rates = sta->supp_rates[0];
L
Larry Finger 已提交
1270 1271 1272

			mac->basic_rates = basic_rates;
			rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
1273
					(u8 *)(&basic_rates));
L
Larry Finger 已提交
1274
		}
1275
		rcu_read_unlock();
L
Larry Finger 已提交
1276 1277
	}
out:
1278
	mutex_unlock(&rtlpriv->locks.conf_mutex);
L
Larry Finger 已提交
1279 1280
}

1281
static u64 rtl_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
L
Larry Finger 已提交
1282 1283 1284 1285
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u64 tsf;

1286
	rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *)(&tsf));
L
Larry Finger 已提交
1287 1288 1289
	return tsf;
}

1290 1291
static void rtl_op_set_tsf(struct ieee80211_hw *hw,
			   struct ieee80211_vif *vif, u64 tsf)
L
Larry Finger 已提交
1292 1293 1294
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1295
	u8 bibss = (mac->opmode == NL80211_IFTYPE_ADHOC) ? 1 : 0;
L
Larry Finger 已提交
1296 1297

	mac->tsf = tsf;
1298
	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *)(&bibss));
L
Larry Finger 已提交
1299 1300
}

1301
static void rtl_op_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
L
Larry Finger 已提交
1302 1303 1304 1305
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u8 tmp = 0;

1306
	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_DUAL_TSF_RST, (u8 *)(&tmp));
L
Larry Finger 已提交
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
}

static void rtl_op_sta_notify(struct ieee80211_hw *hw,
			      struct ieee80211_vif *vif,
			      enum sta_notify_cmd cmd,
			      struct ieee80211_sta *sta)
{
	switch (cmd) {
	case STA_NOTIFY_SLEEP:
		break;
	case STA_NOTIFY_AWAKE:
		break;
	default:
		break;
	}
}

static int rtl_op_ampdu_action(struct ieee80211_hw *hw,
			       struct ieee80211_vif *vif,
			       enum ieee80211_ampdu_mlme_action action,
1327 1328
			       struct ieee80211_sta *sta, u16 tid, u16 *ssn,
			       u8 buf_size)
L
Larry Finger 已提交
1329 1330 1331 1332 1333 1334
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);

	switch (action) {
	case IEEE80211_AMPDU_TX_START:
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1335
			 "IEEE80211_AMPDU_TX_START: TID:%d\n", tid);
1336
		return rtl_tx_agg_start(hw, vif, sta, tid, ssn);
1337 1338 1339
	case IEEE80211_AMPDU_TX_STOP_CONT:
	case IEEE80211_AMPDU_TX_STOP_FLUSH:
	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
L
Larry Finger 已提交
1340
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1341
			 "IEEE80211_AMPDU_TX_STOP: TID:%d\n", tid);
1342
		return rtl_tx_agg_stop(hw, vif, sta, tid);
L
Larry Finger 已提交
1343 1344
	case IEEE80211_AMPDU_TX_OPERATIONAL:
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1345
			 "IEEE80211_AMPDU_TX_OPERATIONAL:TID:%d\n", tid);
1346
		rtl_tx_agg_oper(hw, sta, tid);
L
Larry Finger 已提交
1347 1348 1349
		break;
	case IEEE80211_AMPDU_RX_START:
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1350
			 "IEEE80211_AMPDU_RX_START:TID:%d\n", tid);
1351
		return rtl_rx_agg_start(hw, sta, tid);
L
Larry Finger 已提交
1352 1353
	case IEEE80211_AMPDU_RX_STOP:
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1354
			 "IEEE80211_AMPDU_RX_STOP:TID:%d\n", tid);
1355
		return rtl_rx_agg_stop(hw, sta, tid);
L
Larry Finger 已提交
1356 1357
	default:
		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1358
			 "IEEE80211_AMPDU_ERR!!!!:\n");
L
Larry Finger 已提交
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
		return -EOPNOTSUPP;
	}
	return 0;
}

static void rtl_op_sw_scan_start(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));

1369
	RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
1370 1371 1372 1373 1374
	mac->act_scanning = true;
	if (rtlpriv->link_info.higher_busytraffic) {
		mac->skip_scan = true;
		return;
	}
L
Larry Finger 已提交
1375

1376 1377 1378
	if (rtlpriv->cfg->ops->get_btc_status())
		rtlpriv->btcoexist.btc_ops->btc_scan_notify(rtlpriv, 1);

1379 1380 1381 1382
	if (rtlpriv->dm.supp_phymode_switch) {
		if (rtlpriv->cfg->ops->chk_switch_dmdp)
			rtlpriv->cfg->ops->chk_switch_dmdp(hw);
	}
1383

L
Larry Finger 已提交
1384
	if (mac->link_state == MAC80211_LINKED) {
1385 1386
		rtlpriv->enter_ps = false;
		schedule_work(&rtlpriv->works.lps_change_work);
L
Larry Finger 已提交
1387
		mac->link_state = MAC80211_LINKED_SCANNING;
1388
	} else {
L
Larry Finger 已提交
1389
		rtl_ips_nic_on(hw);
1390 1391
	}

1392
	/* Dul mac */
1393
	rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
L
Larry Finger 已提交
1394 1395

	rtlpriv->cfg->ops->led_control(hw, LED_CTL_SITE_SURVEY);
1396
	rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_BACKUP_BAND0);
L
Larry Finger 已提交
1397 1398 1399 1400 1401 1402 1403
}

static void rtl_op_sw_scan_complete(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));

1404
	RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
L
Larry Finger 已提交
1405
	mac->act_scanning = false;
1406 1407 1408 1409
	mac->skip_scan = false;
	if (rtlpriv->link_info.higher_busytraffic)
		return;

1410
	/* p2p will use 1/6/11 to scan */
1411 1412 1413 1414 1415
	if (mac->n_channels == 3)
		mac->p2p_in_use = true;
	else
		mac->p2p_in_use = false;
	mac->n_channels = 0;
1416
	/* Dul mac */
1417 1418
	rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;

L
Larry Finger 已提交
1419 1420
	if (mac->link_state == MAC80211_LINKED_SCANNING) {
		mac->link_state = MAC80211_LINKED;
1421 1422 1423 1424
		if (mac->opmode == NL80211_IFTYPE_STATION) {
			/* fix fwlps issue */
			rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
		}
L
Larry Finger 已提交
1425 1426
	}

1427
	rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_RESTORE);
1428 1429
	if (rtlpriv->cfg->ops->get_btc_status())
		rtlpriv->btcoexist.btc_ops->btc_scan_notify(rtlpriv, 0);
L
Larry Finger 已提交
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
}

static int rtl_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
			  struct ieee80211_key_conf *key)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u8 key_type = NO_ENCRYPTION;
	u8 key_idx;
	bool group_key = false;
	bool wep_only = false;
	int err = 0;
	u8 mac_addr[ETH_ALEN];
	u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };

	if (rtlpriv->cfg->mod_params->sw_crypto || rtlpriv->sec.use_sw_sec) {
		RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
1447
			 "not open hw encryption\n");
L
Larry Finger 已提交
1448 1449
		return -ENOSPC;	/*User disabled HW-crypto */
	}
1450 1451
	/* To support IBSS, use sw-crypto for GTK */
	if (((vif->type == NL80211_IFTYPE_ADHOC) ||
1452 1453
	    (vif->type == NL80211_IFTYPE_MESH_POINT)) &&
	   !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1454
		return -ENOSPC;
L
Larry Finger 已提交
1455
	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1456
		 "%s hardware based encryption for keyidx: %d, mac: %pM\n",
1457 1458
		  cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
		  sta ? sta->addr : bcast_addr);
L
Larry Finger 已提交
1459 1460
	rtlpriv->sec.being_setkey = true;
	rtl_ips_nic_on(hw);
1461
	mutex_lock(&rtlpriv->locks.conf_mutex);
L
Larry Finger 已提交
1462
	/* <1> get encryption alg */
1463

L
Larry Finger 已提交
1464 1465 1466
	switch (key->cipher) {
	case WLAN_CIPHER_SUITE_WEP40:
		key_type = WEP40_ENCRYPTION;
1467
		RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP40\n");
L
Larry Finger 已提交
1468 1469
		break;
	case WLAN_CIPHER_SUITE_WEP104:
1470
		RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP104\n");
L
Larry Finger 已提交
1471 1472 1473 1474
		key_type = WEP104_ENCRYPTION;
		break;
	case WLAN_CIPHER_SUITE_TKIP:
		key_type = TKIP_ENCRYPTION;
1475
		RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:TKIP\n");
L
Larry Finger 已提交
1476 1477 1478
		break;
	case WLAN_CIPHER_SUITE_CCMP:
		key_type = AESCCMP_ENCRYPTION;
1479
		RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CCMP\n");
L
Larry Finger 已提交
1480
		break;
1481
	case WLAN_CIPHER_SUITE_AES_CMAC:
1482 1483 1484
		/* HW don't support CMAC encryption,
		 * use software CMAC encryption
		 */
1485 1486 1487
		key_type = AESCMAC_ENCRYPTION;
		RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CMAC\n");
		RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1488
			 "HW don't support CMAC encrypiton, use software CMAC encrypiton\n");
1489 1490
		err = -EOPNOTSUPP;
		goto out_unlock;
L
Larry Finger 已提交
1491
	default:
1492 1493
		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
			 "alg_err:%x!!!!:\n", key->cipher);
L
Larry Finger 已提交
1494 1495
		goto out_unlock;
	}
1496
	if (key_type == WEP40_ENCRYPTION ||
1497 1498
	   key_type == WEP104_ENCRYPTION ||
	   vif->type == NL80211_IFTYPE_ADHOC)
1499 1500
		rtlpriv->sec.use_defaultkey = true;

L
Larry Finger 已提交
1501 1502 1503 1504 1505 1506
	/* <2> get key_idx */
	key_idx = (u8) (key->keyidx);
	if (key_idx > 3)
		goto out_unlock;
	/* <3> if pairwise key enable_hw_sec */
	group_key = !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE);
1507 1508 1509 1510 1511

	/* wep always be group key, but there are two conditions:
	 * 1) wep only: is just for wep enc, in this condition
	 * rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION
	 * will be true & enable_hw_sec will be set when wep
1512
	 * ke setting.
1513 1514 1515 1516 1517
	 * 2) wep(group) + AES(pairwise): some AP like cisco
	 * may use it, in this condition enable_hw_sec will not
	 * be set when wep key setting */
	/* we must reset sec_info after lingked before set key,
	 * or some flag will be wrong*/
1518
	if (vif->type == NL80211_IFTYPE_AP ||
1519
		vif->type == NL80211_IFTYPE_MESH_POINT) {
1520 1521 1522 1523 1524 1525 1526
		if (!group_key || key_type == WEP40_ENCRYPTION ||
			key_type == WEP104_ENCRYPTION) {
			if (group_key)
				wep_only = true;
			rtlpriv->cfg->ops->enable_hw_sec(hw);
		}
	} else {
1527 1528
		if ((!group_key) || (vif->type == NL80211_IFTYPE_ADHOC) ||
		    rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION) {
1529 1530
			if (rtlpriv->sec.pairwise_enc_algorithm ==
			    NO_ENCRYPTION &&
1531
			   (key_type == WEP40_ENCRYPTION ||
1532 1533 1534 1535
			    key_type == WEP104_ENCRYPTION))
				wep_only = true;
			rtlpriv->sec.pairwise_enc_algorithm = key_type;
			RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1536 1537
				 "set enable_hw_sec, key_type:%x(OPEN:0 WEP40:1 TKIP:2 AES:4 WEP104:5)\n",
				 key_type);
1538 1539
			rtlpriv->cfg->ops->enable_hw_sec(hw);
		}
L
Larry Finger 已提交
1540 1541 1542 1543 1544 1545
	}
	/* <4> set key based on cmd */
	switch (cmd) {
	case SET_KEY:
		if (wep_only) {
			RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1546
				 "set WEP(group/pairwise) key\n");
L
Larry Finger 已提交
1547 1548 1549 1550 1551 1552 1553
			/* Pairwise key with an assigned MAC address. */
			rtlpriv->sec.pairwise_enc_algorithm = key_type;
			rtlpriv->sec.group_enc_algorithm = key_type;
			/*set local buf about wep key. */
			memcpy(rtlpriv->sec.key_buf[key_idx],
			       key->key, key->keylen);
			rtlpriv->sec.key_len[key_idx] = key->keylen;
1554
			eth_zero_addr(mac_addr);
L
Larry Finger 已提交
1555 1556
		} else if (group_key) {	/* group key */
			RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1557
				 "set group key\n");
L
Larry Finger 已提交
1558 1559 1560 1561 1562 1563 1564 1565 1566
			/* group key */
			rtlpriv->sec.group_enc_algorithm = key_type;
			/*set local buf about group key. */
			memcpy(rtlpriv->sec.key_buf[key_idx],
			       key->key, key->keylen);
			rtlpriv->sec.key_len[key_idx] = key->keylen;
			memcpy(mac_addr, bcast_addr, ETH_ALEN);
		} else {	/* pairwise key */
			RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1567
				 "set pairwise key\n");
L
Larry Finger 已提交
1568
			if (!sta) {
1569 1570
				RT_ASSERT(false,
					  "pairwise key without mac_addr\n");
1571

L
Larry Finger 已提交
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
				err = -EOPNOTSUPP;
				goto out_unlock;
			}
			/* Pairwise key with an assigned MAC address. */
			rtlpriv->sec.pairwise_enc_algorithm = key_type;
			/*set local buf about pairwise key. */
			memcpy(rtlpriv->sec.key_buf[PAIRWISE_KEYIDX],
			       key->key, key->keylen);
			rtlpriv->sec.key_len[PAIRWISE_KEYIDX] = key->keylen;
			rtlpriv->sec.pairwise_key =
			    rtlpriv->sec.key_buf[PAIRWISE_KEYIDX];
			memcpy(mac_addr, sta->addr, ETH_ALEN);
		}
		rtlpriv->cfg->ops->set_key(hw, key_idx, mac_addr,
					   group_key, key_type, wep_only,
					   false);
		/* <5> tell mac80211 do something: */
		/*must use sw generate IV, or can not work !!!!. */
		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
		key->hw_key_idx = key_idx;
		if (key_type == TKIP_ENCRYPTION)
			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1594 1595 1596
		/*use software CCMP encryption for management frames (MFP) */
		if (key_type == AESCCMP_ENCRYPTION)
			key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
L
Larry Finger 已提交
1597 1598 1599
		break;
	case DISABLE_KEY:
		RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1600
			 "disable key delete one entry\n");
L
Larry Finger 已提交
1601
		/*set local buf about wep key. */
1602
		if (vif->type == NL80211_IFTYPE_AP ||
1603
			vif->type == NL80211_IFTYPE_MESH_POINT) {
1604 1605 1606
			if (sta)
				rtl_cam_del_entry(hw, sta->addr);
		}
L
Larry Finger 已提交
1607 1608
		memset(rtlpriv->sec.key_buf[key_idx], 0, key->keylen);
		rtlpriv->sec.key_len[key_idx] = 0;
1609
		eth_zero_addr(mac_addr);
L
Larry Finger 已提交
1610 1611 1612 1613 1614 1615 1616 1617 1618
		/*
		 *mac80211 will delete entrys one by one,
		 *so don't use rtl_cam_reset_all_entry
		 *or clear all entry here.
		 */
		rtl_cam_delete_one_entry(hw, mac_addr, key_idx);
		break;
	default:
		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1619
			 "cmd_err:%x!!!!:\n", cmd);
L
Larry Finger 已提交
1620 1621
	}
out_unlock:
1622
	mutex_unlock(&rtlpriv->locks.conf_mutex);
L
Larry Finger 已提交
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
	rtlpriv->sec.being_setkey = false;
	return err;
}

static void rtl_op_rfkill_poll(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);

	bool radio_state;
	bool blocked;
	u8 valid = 0;

	if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
		return;

1638
	mutex_lock(&rtlpriv->locks.conf_mutex);
L
Larry Finger 已提交
1639 1640 1641 1642 1643 1644 1645 1646 1647

	/*if Radio On return true here */
	radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);

	if (valid) {
		if (unlikely(radio_state != rtlpriv->rfkill.rfkill_state)) {
			rtlpriv->rfkill.rfkill_state = radio_state;

			RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
1648
				 "wireless radio switch turned %s\n",
1649
				  radio_state ? "on" : "off");
L
Larry Finger 已提交
1650 1651 1652 1653 1654 1655

			blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
			wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
		}
	}

1656
	mutex_unlock(&rtlpriv->locks.conf_mutex);
L
Larry Finger 已提交
1657 1658
}

1659
/* this function is called by mac80211 to flush tx buffer
1660
 * before switch channle or power save, or tx buffer packet
1661 1662
 * maybe send after offchannel or rf sleep, this may cause
 * dis-association by AP */
1663 1664 1665 1666
static void rtl_op_flush(struct ieee80211_hw *hw,
			 struct ieee80211_vif *vif,
			 u32 queues,
			 bool drop)
1667 1668 1669 1670
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);

	if (rtlpriv->intf_ops->flush)
1671
		rtlpriv->intf_ops->flush(hw, queues, drop);
1672 1673
}

1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
/*	Description:
 *		This routine deals with the Power Configuration CMD
 *		 parsing for RTL8723/RTL8188E Series IC.
 *	Assumption:
 *		We should follow specific format that was released from HW SD.
 */
bool rtl_hal_pwrseqcmdparsing(struct rtl_priv *rtlpriv, u8 cut_version,
			      u8 faversion, u8 interface_type,
			      struct wlan_pwr_cfg pwrcfgcmd[])
{
	struct wlan_pwr_cfg cfg_cmd = {0};
	bool polling_bit = false;
	u32 ary_idx = 0;
	u8 value = 0;
	u32 offset = 0;
	u32 polling_count = 0;
	u32 max_polling_cnt = 5000;

	do {
		cfg_cmd = pwrcfgcmd[ary_idx];
		RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
			 "rtl_hal_pwrseqcmdparsing(): offset(%#x),cut_msk(%#x), famsk(%#x), interface_msk(%#x), base(%#x), cmd(%#x), msk(%#x), value(%#x)\n",
			 GET_PWR_CFG_OFFSET(cfg_cmd),
					    GET_PWR_CFG_CUT_MASK(cfg_cmd),
			 GET_PWR_CFG_FAB_MASK(cfg_cmd),
					      GET_PWR_CFG_INTF_MASK(cfg_cmd),
			 GET_PWR_CFG_BASE(cfg_cmd), GET_PWR_CFG_CMD(cfg_cmd),
			 GET_PWR_CFG_MASK(cfg_cmd), GET_PWR_CFG_VALUE(cfg_cmd));

		if ((GET_PWR_CFG_FAB_MASK(cfg_cmd)&faversion) &&
		    (GET_PWR_CFG_CUT_MASK(cfg_cmd)&cut_version) &&
		    (GET_PWR_CFG_INTF_MASK(cfg_cmd)&interface_type)) {
			switch (GET_PWR_CFG_CMD(cfg_cmd)) {
			case PWR_CMD_READ:
				RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
					"rtl_hal_pwrseqcmdparsing(): PWR_CMD_READ\n");
				break;
			case PWR_CMD_WRITE:
				RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
					"rtl_hal_pwrseqcmdparsing(): PWR_CMD_WRITE\n");
				offset = GET_PWR_CFG_OFFSET(cfg_cmd);

				/*Read the value from system register*/
				value = rtl_read_byte(rtlpriv, offset);
				value &= (~(GET_PWR_CFG_MASK(cfg_cmd)));
				value |= (GET_PWR_CFG_VALUE(cfg_cmd) &
					  GET_PWR_CFG_MASK(cfg_cmd));

				/*Write the value back to sytem register*/
				rtl_write_byte(rtlpriv, offset, value);
				break;
			case PWR_CMD_POLLING:
				RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
					"rtl_hal_pwrseqcmdparsing(): PWR_CMD_POLLING\n");
				polling_bit = false;
				offset = GET_PWR_CFG_OFFSET(cfg_cmd);

				do {
					value = rtl_read_byte(rtlpriv, offset);

					value &= GET_PWR_CFG_MASK(cfg_cmd);
					if (value ==
					    (GET_PWR_CFG_VALUE(cfg_cmd) &
					     GET_PWR_CFG_MASK(cfg_cmd)))
						polling_bit = true;
					else
						udelay(10);

					if (polling_count++ > max_polling_cnt)
						return false;
				} while (!polling_bit);
				break;
			case PWR_CMD_DELAY:
				RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
					 "rtl_hal_pwrseqcmdparsing(): PWR_CMD_DELAY\n");
				if (GET_PWR_CFG_VALUE(cfg_cmd) ==
				    PWRSEQ_DELAY_US)
					udelay(GET_PWR_CFG_OFFSET(cfg_cmd));
				else
					mdelay(GET_PWR_CFG_OFFSET(cfg_cmd));
				break;
			case PWR_CMD_END:
				RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
					 "rtl_hal_pwrseqcmdparsing(): PWR_CMD_END\n");
				return true;
			default:
				RT_ASSERT(false,
					  "rtl_hal_pwrseqcmdparsing(): Unknown CMD!!\n");
				break;
			}
		}
		ary_idx++;
	} while (1);

	return true;
}
EXPORT_SYMBOL(rtl_hal_pwrseqcmdparsing);
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801

bool rtl_cmd_send_packet(struct ieee80211_hw *hw, struct sk_buff *skb)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
	struct rtl8192_tx_ring *ring;
	struct rtl_tx_desc *pdesc;
	unsigned long flags;
	struct sk_buff *pskb = NULL;

	ring = &rtlpci->tx_ring[BEACON_QUEUE];

	spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
	pskb = __skb_dequeue(&ring->queue);
	if (pskb)
		kfree_skb(pskb);

	/*this is wrong, fill_tx_cmddesc needs update*/
	pdesc = &ring->desc[0];

	rtlpriv->cfg->ops->fill_tx_cmddesc(hw, (u8 *)pdesc, 1, 1, skb);

	__skb_queue_tail(&ring->queue, skb);

	spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);

	rtlpriv->cfg->ops->tx_polling(hw, BEACON_QUEUE);

	return true;
}
EXPORT_SYMBOL(rtl_cmd_send_packet);
L
Larry Finger 已提交
1802 1803 1804 1805 1806 1807
const struct ieee80211_ops rtl_ops = {
	.start = rtl_op_start,
	.stop = rtl_op_stop,
	.tx = rtl_op_tx,
	.add_interface = rtl_op_add_interface,
	.remove_interface = rtl_op_remove_interface,
1808
	.change_interface = rtl_op_change_interface,
1809 1810 1811 1812
#ifdef CONFIG_PM
	.suspend = rtl_op_suspend,
	.resume = rtl_op_resume,
#endif
L
Larry Finger 已提交
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
	.config = rtl_op_config,
	.configure_filter = rtl_op_configure_filter,
	.set_key = rtl_op_set_key,
	.conf_tx = rtl_op_conf_tx,
	.bss_info_changed = rtl_op_bss_info_changed,
	.get_tsf = rtl_op_get_tsf,
	.set_tsf = rtl_op_set_tsf,
	.reset_tsf = rtl_op_reset_tsf,
	.sta_notify = rtl_op_sta_notify,
	.ampdu_action = rtl_op_ampdu_action,
	.sw_scan_start = rtl_op_sw_scan_start,
	.sw_scan_complete = rtl_op_sw_scan_complete,
	.rfkill_poll = rtl_op_rfkill_poll,
1826 1827
	.sta_add = rtl_op_sta_add,
	.sta_remove = rtl_op_sta_remove,
1828
	.flush = rtl_op_flush,
L
Larry Finger 已提交
1829
};
1830
EXPORT_SYMBOL_GPL(rtl_ops);
1831 1832 1833 1834 1835 1836

bool rtl_btc_status_false(void)
{
	return false;
}
EXPORT_SYMBOL_GPL(rtl_btc_status_false);