base.c 52.3 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
 *
 * 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>
 *
 *****************************************************************************/

#include "wifi.h"
#include "rc.h"
#include "base.h"
#include "efuse.h"
#include "cam.h"
#include "ps.h"
#include "regd.h"

38 39 40
#include <linux/ip.h>
#include <linux/module.h>

L
Larry Finger 已提交
41
/*
42 43
 *NOTICE!!!: This file will be very big, we should
 *keep it clear under following roles:
L
Larry Finger 已提交
44
 *
45
 *This file include following parts, so, if you add new
L
Larry Finger 已提交
46 47 48 49 50 51 52 53 54
 *functions into this file, please check which part it
 *should includes. or check if you should add new part
 *for this file:
 *
 *1) mac80211 init functions
 *2) tx information functions
 *3) functions called by core.c
 *4) wq & timer callback functions
 *5) frame process functions
55 56
 *6) IOT functions
 *7) sysfs functions
57 58
 *8) vif functions
 *9) ...
L
Larry Finger 已提交
59 60 61 62 63 64 65
 */

/*********************************************************
 *
 * mac80211 init functions
 *
 *********************************************************/
66
static struct ieee80211_channel rtl_channeltable_2g[] = {
L
Larry Finger 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	{.center_freq = 2412, .hw_value = 1,},
	{.center_freq = 2417, .hw_value = 2,},
	{.center_freq = 2422, .hw_value = 3,},
	{.center_freq = 2427, .hw_value = 4,},
	{.center_freq = 2432, .hw_value = 5,},
	{.center_freq = 2437, .hw_value = 6,},
	{.center_freq = 2442, .hw_value = 7,},
	{.center_freq = 2447, .hw_value = 8,},
	{.center_freq = 2452, .hw_value = 9,},
	{.center_freq = 2457, .hw_value = 10,},
	{.center_freq = 2462, .hw_value = 11,},
	{.center_freq = 2467, .hw_value = 12,},
	{.center_freq = 2472, .hw_value = 13,},
	{.center_freq = 2484, .hw_value = 14,},
};

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
static struct ieee80211_channel rtl_channeltable_5g[] = {
	{.center_freq = 5180, .hw_value = 36,},
	{.center_freq = 5200, .hw_value = 40,},
	{.center_freq = 5220, .hw_value = 44,},
	{.center_freq = 5240, .hw_value = 48,},
	{.center_freq = 5260, .hw_value = 52,},
	{.center_freq = 5280, .hw_value = 56,},
	{.center_freq = 5300, .hw_value = 60,},
	{.center_freq = 5320, .hw_value = 64,},
	{.center_freq = 5500, .hw_value = 100,},
	{.center_freq = 5520, .hw_value = 104,},
	{.center_freq = 5540, .hw_value = 108,},
	{.center_freq = 5560, .hw_value = 112,},
	{.center_freq = 5580, .hw_value = 116,},
	{.center_freq = 5600, .hw_value = 120,},
	{.center_freq = 5620, .hw_value = 124,},
	{.center_freq = 5640, .hw_value = 128,},
	{.center_freq = 5660, .hw_value = 132,},
	{.center_freq = 5680, .hw_value = 136,},
	{.center_freq = 5700, .hw_value = 140,},
	{.center_freq = 5745, .hw_value = 149,},
	{.center_freq = 5765, .hw_value = 153,},
	{.center_freq = 5785, .hw_value = 157,},
	{.center_freq = 5805, .hw_value = 161,},
	{.center_freq = 5825, .hw_value = 165,},
};

static struct ieee80211_rate rtl_ratetable_2g[] = {
L
Larry Finger 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124
	{.bitrate = 10, .hw_value = 0x00,},
	{.bitrate = 20, .hw_value = 0x01,},
	{.bitrate = 55, .hw_value = 0x02,},
	{.bitrate = 110, .hw_value = 0x03,},
	{.bitrate = 60, .hw_value = 0x04,},
	{.bitrate = 90, .hw_value = 0x05,},
	{.bitrate = 120, .hw_value = 0x06,},
	{.bitrate = 180, .hw_value = 0x07,},
	{.bitrate = 240, .hw_value = 0x08,},
	{.bitrate = 360, .hw_value = 0x09,},
	{.bitrate = 480, .hw_value = 0x0a,},
	{.bitrate = 540, .hw_value = 0x0b,},
};

125 126 127 128 129 130 131 132 133 134 135
static struct ieee80211_rate rtl_ratetable_5g[] = {
	{.bitrate = 60, .hw_value = 0x04,},
	{.bitrate = 90, .hw_value = 0x05,},
	{.bitrate = 120, .hw_value = 0x06,},
	{.bitrate = 180, .hw_value = 0x07,},
	{.bitrate = 240, .hw_value = 0x08,},
	{.bitrate = 360, .hw_value = 0x09,},
	{.bitrate = 480, .hw_value = 0x0a,},
	{.bitrate = 540, .hw_value = 0x0b,},
};

L
Larry Finger 已提交
136 137 138
static const struct ieee80211_supported_band rtl_band_2ghz = {
	.band = IEEE80211_BAND_2GHZ,

139 140
	.channels = rtl_channeltable_2g,
	.n_channels = ARRAY_SIZE(rtl_channeltable_2g),
L
Larry Finger 已提交
141

142 143
	.bitrates = rtl_ratetable_2g,
	.n_bitrates = ARRAY_SIZE(rtl_ratetable_2g),
L
Larry Finger 已提交
144 145 146 147

	.ht_cap = {0},
};

148 149 150 151 152
static struct ieee80211_supported_band rtl_band_5ghz = {
	.band = IEEE80211_BAND_5GHZ,

	.channels = rtl_channeltable_5g,
	.n_channels = ARRAY_SIZE(rtl_channeltable_5g),
L
Larry Finger 已提交
153

154 155
	.bitrates = rtl_ratetable_5g,
	.n_bitrates = ARRAY_SIZE(rtl_ratetable_5g),
L
Larry Finger 已提交
156 157 158 159

	.ht_cap = {0},
};

160 161 162 163 164 165 166 167 168 169 170
static const u8 tid_to_ac[] = {
	2, /* IEEE80211_AC_BE */
	3, /* IEEE80211_AC_BK */
	3, /* IEEE80211_AC_BK */
	2, /* IEEE80211_AC_BE */
	1, /* IEEE80211_AC_VI */
	1, /* IEEE80211_AC_VI */
	0, /* IEEE80211_AC_VO */
	0, /* IEEE80211_AC_VO */
};

171
u8 rtl_tid_to_ac(u8 tid)
172 173 174
{
	return tid_to_ac[tid];
}
175
EXPORT_SYMBOL_GPL(rtl_tid_to_ac);
176

L
Larry Finger 已提交
177 178 179 180 181 182 183 184 185 186 187 188
static void _rtl_init_hw_ht_capab(struct ieee80211_hw *hw,
				  struct ieee80211_sta_ht_cap *ht_cap)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);

	ht_cap->ht_supported = true;
	ht_cap->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
	    IEEE80211_HT_CAP_SGI_40 |
	    IEEE80211_HT_CAP_SGI_20 |
	    IEEE80211_HT_CAP_DSSSCCK40 | IEEE80211_HT_CAP_MAX_AMSDU;

189 190 191
	if (rtlpriv->rtlhal.disable_amsdu_8k)
		ht_cap->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;

L
Larry Finger 已提交
192 193 194 195 196 197 198 199 200 201 202
	/*
	 *Maximum length of AMPDU that the STA can receive.
	 *Length = 2 ^ (13 + max_ampdu_length_exp) - 1 (octets)
	 */
	ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;

	/*Minimum MPDU start spacing , */
	ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;

	ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;

203
	/*hw->wiphy->bands[IEEE80211_BAND_2GHZ]
L
Larry Finger 已提交
204 205
	 *base on ant_num
	 *rx_mask: RX mask
206 207 208 209
	 *if rx_ant = 1 rx_mask[0]= 0xff;==>MCS0-MCS7
	 *if rx_ant = 2 rx_mask[1]= 0xff;==>MCS8-MCS15
	 *if rx_ant >= 3 rx_mask[2]= 0xff;
	 *if BW_40 rx_mask[4]= 0x01;
L
Larry Finger 已提交
210 211
	 *highest supported RX rate
	 */
212
	if (rtlpriv->dm.supp_phymode_switch) {
L
Larry Finger 已提交
213

214 215
		RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG,
			 "Support phy mode switch\n");
L
Larry Finger 已提交
216 217 218 219 220

		ht_cap->mcs.rx_mask[0] = 0xFF;
		ht_cap->mcs.rx_mask[1] = 0xFF;
		ht_cap->mcs.rx_mask[4] = 0x01;

221
		ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	} else {
		if (get_rf_type(rtlphy) == RF_1T2R ||
		    get_rf_type(rtlphy) == RF_2T2R) {
			RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
				 "1T2R or 2T2R\n");
			ht_cap->mcs.rx_mask[0] = 0xFF;
			ht_cap->mcs.rx_mask[1] = 0xFF;
			ht_cap->mcs.rx_mask[4] = 0x01;

			ht_cap->mcs.rx_highest =
				 cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
		} else if (get_rf_type(rtlphy) == RF_1T1R) {
			RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "1T1R\n");

			ht_cap->mcs.rx_mask[0] = 0xFF;
			ht_cap->mcs.rx_mask[1] = 0x00;
			ht_cap->mcs.rx_mask[4] = 0x01;

			ht_cap->mcs.rx_highest =
				 cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7);
		}
L
Larry Finger 已提交
243 244 245 246 247
	}
}

static void _rtl_init_mac80211(struct ieee80211_hw *hw)
{
248 249
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
L
Larry Finger 已提交
250 251 252 253 254
	struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
	struct ieee80211_supported_band *sband;


255 256 257 258 259
	if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY && rtlhal->bandset ==
	    BAND_ON_BOTH) {
		/* 1: 2.4 G bands */
		/* <1> use  mac->bands as mem for hw->wiphy->bands */
		sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
L
Larry Finger 已提交
260

261 262 263 264
		/* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
		 * to default value(1T1R) */
		memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]), &rtl_band_2ghz,
				sizeof(struct ieee80211_supported_band));
L
Larry Finger 已提交
265

266 267
		/* <3> init ht cap base on ant_num */
		_rtl_init_hw_ht_capab(hw, &sband->ht_cap);
L
Larry Finger 已提交
268

269 270
		/* <4> set mac->sband to wiphy->sband */
		hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
L
Larry Finger 已提交
271

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
		/* 2: 5 G bands */
		/* <1> use  mac->bands as mem for hw->wiphy->bands */
		sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);

		/* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
		 * to default value(1T1R) */
		memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]), &rtl_band_5ghz,
				sizeof(struct ieee80211_supported_band));

		/* <3> init ht cap base on ant_num */
		_rtl_init_hw_ht_capab(hw, &sband->ht_cap);

		/* <4> set mac->sband to wiphy->sband */
		hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
	} else {
		if (rtlhal->current_bandtype == BAND_ON_2_4G) {
			/* <1> use  mac->bands as mem for hw->wiphy->bands */
			sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);

			/* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
			 * to default value(1T1R) */
			memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]),
				 &rtl_band_2ghz,
				 sizeof(struct ieee80211_supported_band));

			/* <3> init ht cap base on ant_num */
			_rtl_init_hw_ht_capab(hw, &sband->ht_cap);

			/* <4> set mac->sband to wiphy->sband */
			hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
		} else if (rtlhal->current_bandtype == BAND_ON_5G) {
			/* <1> use  mac->bands as mem for hw->wiphy->bands */
			sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);

			/* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
			 * to default value(1T1R) */
			memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]),
				 &rtl_band_5ghz,
				 sizeof(struct ieee80211_supported_band));

			/* <3> init ht cap base on ant_num */
			_rtl_init_hw_ht_capab(hw, &sband->ht_cap);

			/* <4> set mac->sband to wiphy->sband */
			hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
		} else {
318 319
			RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG, "Err BAND %d\n",
				 rtlhal->current_bandtype);
320 321
		}
	}
L
Larry Finger 已提交
322 323 324
	/* <5> set hw caps */
	hw->flags = IEEE80211_HW_SIGNAL_DBM |
	    IEEE80211_HW_RX_INCLUDES_FCS |
325
	    IEEE80211_HW_AMPDU_AGGREGATION |
326 327
	    IEEE80211_HW_CONNECTION_MONITOR |
	    /* IEEE80211_HW_SUPPORTS_CQM_RSSI | */
328 329
	    IEEE80211_HW_CONNECTION_MONITOR |
	    IEEE80211_HW_MFP_CAPABLE |
L
Larry Finger 已提交
330 331
	    IEEE80211_HW_REPORTS_TX_ACK_STATUS | 0;

332 333 334 335 336 337 338
	/* swlps or hwlps has been set in diff chip in init_sw_vars */
	if (rtlpriv->psc.swctrl_lps)
		hw->flags |= IEEE80211_HW_SUPPORTS_PS |
			IEEE80211_HW_PS_NULLFUNC_STACK |
			/* IEEE80211_HW_SUPPORTS_DYNAMIC_PS | */
			0;

L
Larry Finger 已提交
339
	hw->wiphy->interface_modes =
340 341
	    BIT(NL80211_IFTYPE_AP) |
	    BIT(NL80211_IFTYPE_STATION) |
342 343 344 345
	    BIT(NL80211_IFTYPE_ADHOC) |
	    BIT(NL80211_IFTYPE_MESH_POINT) |
	    BIT(NL80211_IFTYPE_P2P_CLIENT) |
	    BIT(NL80211_IFTYPE_P2P_GO);
L
Larry Finger 已提交
346

347
	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
L
Larry Finger 已提交
348 349 350 351 352 353 354 355
	hw->wiphy->rts_threshold = 2347;

	hw->queues = AC_MAX;
	hw->extra_tx_headroom = RTL_TX_HEADER_SIZE;

	/* TODO: Correct this value for our hw */
	/* TODO: define these hard code value */
	hw->channel_change_time = 100;
356
	hw->max_listen_interval = 10;
L
Larry Finger 已提交
357 358
	hw->max_rate_tries = 4;
	/* hw->max_rates = 1; */
359
	hw->sta_data_size = sizeof(struct rtl_sta_info);
L
Larry Finger 已提交
360 361 362 363 364

	/* <6> mac address */
	if (is_valid_ether_addr(rtlefuse->dev_addr)) {
		SET_IEEE80211_PERM_ADDR(hw, rtlefuse->dev_addr);
	} else {
365 366 367
		u8 rtlmac1[] = { 0x00, 0xe0, 0x4c, 0x81, 0x92, 0x00 };
		get_random_bytes((rtlmac1 + (ETH_ALEN - 1)), 1);
		SET_IEEE80211_PERM_ADDR(hw, rtlmac1);
L
Larry Finger 已提交
368 369 370 371 372 373 374 375 376 377 378
	}

}

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

	/* <1> timer */
	setup_timer(&rtlpriv->works.watchdog_timer,
		    rtl_watch_dog_timer_callback, (unsigned long)hw);
379 380
	setup_timer(&rtlpriv->works.dualmac_easyconcurrent_retrytimer,
		    rtl_easy_concurrent_retrytimer_callback, (unsigned long)hw);
L
Larry Finger 已提交
381 382 383

	/* <2> work queue */
	rtlpriv->works.hw = hw;
384
	rtlpriv->works.rtl_wq = alloc_workqueue("%s", 0, 0, rtlpriv->cfg->name);
L
Larry Finger 已提交
385 386 387 388
	INIT_DELAYED_WORK(&rtlpriv->works.watchdog_wq,
			  (void *)rtl_watchdog_wq_callback);
	INIT_DELAYED_WORK(&rtlpriv->works.ips_nic_off_wq,
			  (void *)rtl_ips_nic_off_wq_callback);
389 390 391 392
	INIT_DELAYED_WORK(&rtlpriv->works.ps_work,
			  (void *)rtl_swlps_wq_callback);
	INIT_DELAYED_WORK(&rtlpriv->works.ps_rfon_wq,
			  (void *)rtl_swlps_rfon_wq_callback);
393 394
	INIT_DELAYED_WORK(&rtlpriv->works.fwevt_wq,
			  (void *)rtl_fwevt_wq_callback);
L
Larry Finger 已提交
395 396 397 398 399 400 401 402 403 404 405

}

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

	del_timer_sync(&rtlpriv->works.watchdog_timer);

	cancel_delayed_work(&rtlpriv->works.watchdog_wq);
	cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
406 407
	cancel_delayed_work(&rtlpriv->works.ps_work);
	cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
408
	cancel_delayed_work(&rtlpriv->works.fwevt_wq);
L
Larry Finger 已提交
409
}
410
EXPORT_SYMBOL_GPL(rtl_deinit_deferred_work);
L
Larry Finger 已提交
411 412 413 414 415 416 417 418 419

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

	bool radio_state;
	bool blocked;
	u8 valid = 0;

420
	/*set init state to on */
421
	rtlpriv->rfkill.rfkill_state = true;
422
	wiphy_rfkill_set_hw_state(hw->wiphy, 0);
L
Larry Finger 已提交
423

424
	radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
L
Larry Finger 已提交
425

426
	if (valid) {
427 428
		pr_info("wireless switch is %s\n",
			rtlpriv->rfkill.rfkill_state ? "on" : "off");
429

L
Larry Finger 已提交
430 431 432 433 434 435 436 437
		rtlpriv->rfkill.rfkill_state = radio_state;

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

	wiphy_rfkill_start_polling(hw->wiphy);
}
438
EXPORT_SYMBOL(rtl_init_rfkill);
L
Larry Finger 已提交
439 440 441 442 443

void rtl_deinit_rfkill(struct ieee80211_hw *hw)
{
	wiphy_rfkill_stop_polling(hw->wiphy);
}
444
EXPORT_SYMBOL_GPL(rtl_deinit_rfkill);
L
Larry Finger 已提交
445 446 447 448 449 450 451 452 453 454 455

int rtl_init_core(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));

	/* <1> init mac80211 */
	_rtl_init_mac80211(hw);
	rtlmac->hw = hw;

	/* <2> rate control register */
456
	hw->rate_control_algorithm = "rtl_rc";
L
Larry Finger 已提交
457 458 459 460 461 462

	/*
	 * <3> init CRDA must come after init
	 * mac80211 hw  in _rtl_init_mac80211.
	 */
	if (rtl_regd_init(hw, rtl_reg_notifier)) {
463
		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "REGD init failed\n");
L
Larry Finger 已提交
464 465 466 467
		return 1;
	}

	/* <4> locks */
468
	mutex_init(&rtlpriv->locks.conf_mutex);
469
	mutex_init(&rtlpriv->locks.ps_mutex);
470
	spin_lock_init(&rtlpriv->locks.ips_lock);
L
Larry Finger 已提交
471
	spin_lock_init(&rtlpriv->locks.irq_th_lock);
472 473
	spin_lock_init(&rtlpriv->locks.irq_pci_lock);
	spin_lock_init(&rtlpriv->locks.tx_lock);
L
Larry Finger 已提交
474 475 476
	spin_lock_init(&rtlpriv->locks.h2c_lock);
	spin_lock_init(&rtlpriv->locks.rf_ps_lock);
	spin_lock_init(&rtlpriv->locks.rf_lock);
477
	spin_lock_init(&rtlpriv->locks.waitq_lock);
478
	spin_lock_init(&rtlpriv->locks.entry_list_lock);
479
	spin_lock_init(&rtlpriv->locks.fw_ps_lock);
480
	spin_lock_init(&rtlpriv->locks.cck_and_rw_pagea_lock);
481 482 483 484 485 486
	spin_lock_init(&rtlpriv->locks.check_sendpkt_lock);
	spin_lock_init(&rtlpriv->locks.fw_ps_lock);
	spin_lock_init(&rtlpriv->locks.lps_lock);

	/* <5> init list */
	INIT_LIST_HEAD(&rtlpriv->entry_list);
L
Larry Finger 已提交
487 488 489

	rtlmac->link_state = MAC80211_NOLINK;

490
	/* <6> init deferred work */
L
Larry Finger 已提交
491 492 493 494
	_rtl_init_deferred_work(hw);

	return 0;
}
495
EXPORT_SYMBOL_GPL(rtl_init_core);
L
Larry Finger 已提交
496 497 498 499

void rtl_deinit_core(struct ieee80211_hw *hw)
{
}
500
EXPORT_SYMBOL_GPL(rtl_deinit_core);
L
Larry Finger 已提交
501 502 503 504 505 506 507 508

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

	rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
}
509
EXPORT_SYMBOL_GPL(rtl_init_rx_config);
L
Larry Finger 已提交
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

/*********************************************************
 *
 * tx information functions
 *
 *********************************************************/
static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
					  struct rtl_tcb_desc *tcb_desc,
					  struct ieee80211_tx_info *info)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u8 rate_flag = info->control.rates[0].flags;

	tcb_desc->use_shortpreamble = false;

	/* 1M can only use Long Preamble. 11B spec */
	if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
		return;
	else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
		tcb_desc->use_shortpreamble = true;

	return;
}

static void _rtl_query_shortgi(struct ieee80211_hw *hw,
535
			       struct ieee80211_sta *sta,
L
Larry Finger 已提交
536 537 538 539 540
			       struct rtl_tcb_desc *tcb_desc,
			       struct ieee80211_tx_info *info)
{
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
	u8 rate_flag = info->control.rates[0].flags;
541
	u8 sgi_40 = 0, sgi_20 = 0, bw_40 = 0;
L
Larry Finger 已提交
542 543
	tcb_desc->use_shortgi = false;

544
	if (sta == NULL)
L
Larry Finger 已提交
545 546
		return;

547 548 549 550
	sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
	sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;

	if (!(sta->ht_cap.ht_supported))
L
Larry Finger 已提交
551 552
		return;

553
	if (!sgi_40 && !sgi_20)
L
Larry Finger 已提交
554 555
		return;

556 557 558
	if (mac->opmode == NL80211_IFTYPE_STATION)
		bw_40 = mac->bw_40;
	else if (mac->opmode == NL80211_IFTYPE_AP ||
559 560
		 mac->opmode == NL80211_IFTYPE_ADHOC ||
		 mac->opmode == NL80211_IFTYPE_MESH_POINT)
561
		bw_40 = sta->bandwidth >= IEEE80211_STA_RX_BW_40;
562

563
	if (bw_40 && sgi_40)
L
Larry Finger 已提交
564
		tcb_desc->use_shortgi = true;
565
	else if ((bw_40 == false) && sgi_20)
L
Larry Finger 已提交
566 567 568 569 570 571 572 573 574 575 576 577 578 579
		tcb_desc->use_shortgi = true;

	if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
		tcb_desc->use_shortgi = false;
}

static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
				       struct rtl_tcb_desc *tcb_desc,
				       struct ieee80211_tx_info *info)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u8 rate_flag = info->control.rates[0].flags;

	/* Common Settings */
580 581
	tcb_desc->rts_stbc = false;
	tcb_desc->cts_enable = false;
L
Larry Finger 已提交
582
	tcb_desc->rts_sc = 0;
583 584 585
	tcb_desc->rts_bw = false;
	tcb_desc->rts_use_shortpreamble = false;
	tcb_desc->rts_use_shortgi = false;
L
Larry Finger 已提交
586 587 588

	if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
		/* Use CTS-to-SELF in protection mode. */
589 590
		tcb_desc->rts_enable = true;
		tcb_desc->cts_enable = true;
L
Larry Finger 已提交
591 592 593
		tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
	} else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
		/* Use RTS-CTS in protection mode. */
594
		tcb_desc->rts_enable = true;
L
Larry Finger 已提交
595 596 597 598 599
		tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
	}
}

static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
600
				   struct ieee80211_sta *sta,
L
Larry Finger 已提交
601 602 603 604
				   struct rtl_tcb_desc *tcb_desc)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
605 606
	struct rtl_sta_info *sta_entry = NULL;
	u8 ratr_index = 7;
L
Larry Finger 已提交
607

608 609 610 611
	if (sta) {
		sta_entry = (struct rtl_sta_info *) sta->drv_priv;
		ratr_index = sta_entry->ratr_index;
	}
L
Larry Finger 已提交
612
	if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
613
		if (mac->opmode == NL80211_IFTYPE_STATION) {
L
Larry Finger 已提交
614
			tcb_desc->ratr_index = 0;
615 616
		} else if (mac->opmode == NL80211_IFTYPE_ADHOC ||
			   mac->opmode == NL80211_IFTYPE_MESH_POINT) {
617
			if (tcb_desc->multicast || tcb_desc->broadcast) {
L
Larry Finger 已提交
618 619 620
				tcb_desc->hw_rate =
				    rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
				tcb_desc->use_driver_rate = 1;
621
				tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
L
Larry Finger 已提交
622
			} else {
623
				tcb_desc->ratr_index = ratr_index;
L
Larry Finger 已提交
624
			}
625 626
		} else if (mac->opmode == NL80211_IFTYPE_AP) {
			tcb_desc->ratr_index = ratr_index;
L
Larry Finger 已提交
627 628 629
		}
	}

630
	if (rtlpriv->dm.useramask) {
631 632 633 634
		tcb_desc->ratr_index = ratr_index;
		/* TODO we will differentiate adhoc and station future  */
		if (mac->opmode == NL80211_IFTYPE_STATION ||
		    mac->opmode == NL80211_IFTYPE_MESH_POINT) {
635 636 637 638 639 640 641 642 643 644 645 646 647
			tcb_desc->mac_id = 0;

			if (mac->mode == WIRELESS_MODE_N_24G)
				tcb_desc->ratr_index = RATR_INX_WIRELESS_NGB;
			else if (mac->mode == WIRELESS_MODE_N_5G)
				tcb_desc->ratr_index = RATR_INX_WIRELESS_NG;
			else if (mac->mode & WIRELESS_MODE_G)
				tcb_desc->ratr_index = RATR_INX_WIRELESS_GB;
			else if (mac->mode & WIRELESS_MODE_B)
				tcb_desc->ratr_index = RATR_INX_WIRELESS_B;
			else if (mac->mode & WIRELESS_MODE_A)
				tcb_desc->ratr_index = RATR_INX_WIRELESS_G;
		} else if (mac->opmode == NL80211_IFTYPE_AP ||
648
			   mac->opmode == NL80211_IFTYPE_ADHOC) {
649 650 651 652 653 654 655 656
			if (NULL != sta) {
				if (sta->aid > 0)
					tcb_desc->mac_id = sta->aid + 1;
				else
					tcb_desc->mac_id = 1;
			} else {
				tcb_desc->mac_id = 0;
			}
L
Larry Finger 已提交
657 658 659 660 661
		}
	}
}

static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
662
				      struct ieee80211_sta *sta,
L
Larry Finger 已提交
663 664 665 666 667
				      struct rtl_tcb_desc *tcb_desc)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));

668
	tcb_desc->packet_bw = false;
669
	if (!sta)
L
Larry Finger 已提交
670
		return;
671
	if (mac->opmode == NL80211_IFTYPE_AP ||
672 673
	    mac->opmode == NL80211_IFTYPE_ADHOC ||
	    mac->opmode == NL80211_IFTYPE_MESH_POINT) {
674
		if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
675 676 677 678 679
			return;
	} else if (mac->opmode == NL80211_IFTYPE_STATION) {
		if (!mac->bw_40 || !(sta->ht_cap.ht_supported))
			return;
	}
680
	if (tcb_desc->multicast || tcb_desc->broadcast)
L
Larry Finger 已提交
681 682 683 684 685 686
		return;

	/*use legency rate, shall use 20MHz */
	if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
		return;

687
	tcb_desc->packet_bw = true;
L
Larry Finger 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
}

static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	u8 hw_rate;

	if (get_rf_type(rtlphy) == RF_2T2R)
		hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
	else
		hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];

	return hw_rate;
}

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
/* mac80211's rate_idx is like this:
 *
 * 2.4G band:rx_status->band == IEEE80211_BAND_2GHZ
 *
 * B/G rate:
 * (rx_status->flag & RX_FLAG_HT) = 0,
 * DESC92_RATE1M-->DESC92_RATE54M ==> idx is 0-->11,
 *
 * N rate:
 * (rx_status->flag & RX_FLAG_HT) = 1,
 * DESC92_RATEMCS0-->DESC92_RATEMCS15 ==> idx is 0-->15
 *
 * 5G band:rx_status->band == IEEE80211_BAND_5GHZ
 * A rate:
 * (rx_status->flag & RX_FLAG_HT) = 0,
 * DESC92_RATE6M-->DESC92_RATE54M ==> idx is 0-->7,
 *
 * N rate:
 * (rx_status->flag & RX_FLAG_HT) = 1,
 * DESC92_RATEMCS0-->DESC92_RATEMCS15 ==> idx is 0-->15
 */
int rtlwifi_rate_mapping(struct ieee80211_hw *hw,
			 bool isht, u8 desc_rate, bool first_ampdu)
{
	int rate_idx;

	if (false == isht) {
731
		if (IEEE80211_BAND_2GHZ == hw->conf.chandef.chan->band) {
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
			switch (desc_rate) {
			case DESC92_RATE1M:
				rate_idx = 0;
				break;
			case DESC92_RATE2M:
				rate_idx = 1;
				break;
			case DESC92_RATE5_5M:
				rate_idx = 2;
				break;
			case DESC92_RATE11M:
				rate_idx = 3;
				break;
			case DESC92_RATE6M:
				rate_idx = 4;
				break;
			case DESC92_RATE9M:
				rate_idx = 5;
				break;
			case DESC92_RATE12M:
				rate_idx = 6;
				break;
			case DESC92_RATE18M:
				rate_idx = 7;
				break;
			case DESC92_RATE24M:
				rate_idx = 8;
				break;
			case DESC92_RATE36M:
				rate_idx = 9;
				break;
			case DESC92_RATE48M:
				rate_idx = 10;
				break;
			case DESC92_RATE54M:
				rate_idx = 11;
				break;
			default:
				rate_idx = 0;
				break;
			}
		} else {
			switch (desc_rate) {
			case DESC92_RATE6M:
				rate_idx = 0;
				break;
			case DESC92_RATE9M:
				rate_idx = 1;
				break;
			case DESC92_RATE12M:
				rate_idx = 2;
				break;
			case DESC92_RATE18M:
				rate_idx = 3;
				break;
			case DESC92_RATE24M:
				rate_idx = 4;
				break;
			case DESC92_RATE36M:
				rate_idx = 5;
				break;
			case DESC92_RATE48M:
				rate_idx = 6;
				break;
			case DESC92_RATE54M:
				rate_idx = 7;
				break;
			default:
				rate_idx = 0;
				break;
			}
		}

	} else {

		switch (desc_rate) {
		case DESC92_RATEMCS0:
			rate_idx = 0;
			break;
		case DESC92_RATEMCS1:
			rate_idx = 1;
			break;
		case DESC92_RATEMCS2:
			rate_idx = 2;
			break;
		case DESC92_RATEMCS3:
			rate_idx = 3;
			break;
		case DESC92_RATEMCS4:
			rate_idx = 4;
			break;
		case DESC92_RATEMCS5:
			rate_idx = 5;
			break;
		case DESC92_RATEMCS6:
			rate_idx = 6;
			break;
		case DESC92_RATEMCS7:
			rate_idx = 7;
			break;
		case DESC92_RATEMCS8:
			rate_idx = 8;
			break;
		case DESC92_RATEMCS9:
			rate_idx = 9;
			break;
		case DESC92_RATEMCS10:
			rate_idx = 10;
			break;
		case DESC92_RATEMCS11:
			rate_idx = 11;
			break;
		case DESC92_RATEMCS12:
			rate_idx = 12;
			break;
		case DESC92_RATEMCS13:
			rate_idx = 13;
			break;
		case DESC92_RATEMCS14:
			rate_idx = 14;
			break;
		case DESC92_RATEMCS15:
			rate_idx = 15;
			break;
		default:
			rate_idx = 0;
			break;
		}
	}
	return rate_idx;
}
EXPORT_SYMBOL(rtlwifi_rate_mapping);

865 866 867 868 869 870 871 872 873
bool rtl_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb)
{
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	__le16 fc = rtl_get_fc(skb);

	if (rtlpriv->dm.supp_phymode_switch &&
	    mac->link_state < MAC80211_LINKED &&
	    (ieee80211_is_auth(fc) || ieee80211_is_probe_req(fc))) {
874 875
		if (rtlpriv->cfg->ops->chk_switch_dmdp)
			rtlpriv->cfg->ops->chk_switch_dmdp(hw);
876 877 878 879 880 881 882 883 884 885 886 887
	}
	if (ieee80211_is_auth(fc)) {
		RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
		rtl_ips_nic_on(hw);

		mac->link_state = MAC80211_LINKING;
		/* Dual mac */
		rtlpriv->phy.need_iqk = true;
	}

	return true;
}
888
EXPORT_SYMBOL_GPL(rtl_tx_mgmt_proc);
889

L
Larry Finger 已提交
890 891
void rtl_get_tcb_desc(struct ieee80211_hw *hw,
		      struct ieee80211_tx_info *info,
892
		      struct ieee80211_sta *sta,
L
Larry Finger 已提交
893 894 895 896
		      struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
897
	struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
L
Larry Finger 已提交
898
	struct ieee80211_rate *txrate;
899
	__le16 fc = hdr->frame_control;
L
Larry Finger 已提交
900

901
	txrate = ieee80211_get_tx_rate(hw, info);
902 903 904 905
	if (txrate)
		tcb_desc->hw_rate = txrate->hw_value;
	else
		tcb_desc->hw_rate = 0;
L
Larry Finger 已提交
906 907 908

	if (ieee80211_is_data(fc)) {
		/*
909
		 *we set data rate INX 0
L
Larry Finger 已提交
910 911 912 913 914 915 916 917
		 *in rtl_rc.c   if skb is special data or
		 *mgt which need low data rate.
		 */

		/*
		 *So tcb_desc->hw_rate is just used for
		 *special data and mgt frames
		 */
918
		if (info->control.rates[0].idx == 0 ||
919
				ieee80211_is_nullfunc(fc)) {
L
Larry Finger 已提交
920
			tcb_desc->use_driver_rate = true;
921
			tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
L
Larry Finger 已提交
922 923 924 925 926 927 928

			tcb_desc->disable_ratefallback = 1;
		} else {
			/*
			 *because hw will nerver use hw_rate
			 *when tcb_desc->use_driver_rate = false
			 *so we never set highest N rate here,
L
Lucas De Marchi 已提交
929
			 *and N rate will all be controlled by FW
L
Larry Finger 已提交
930 931
			 *when tcb_desc->use_driver_rate = false
			 */
932
			if (sta && (sta->ht_cap.ht_supported)) {
L
Larry Finger 已提交
933 934 935 936 937 938 939 940 941 942 943 944 945
				tcb_desc->hw_rate = _rtl_get_highest_n_rate(hw);
			} else {
				if (rtlmac->mode == WIRELESS_MODE_B) {
					tcb_desc->hw_rate =
					   rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
				} else {
					tcb_desc->hw_rate =
					   rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
				}
			}
		}

		if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
946
			tcb_desc->multicast = 1;
L
Larry Finger 已提交
947
		else if (is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
948
			tcb_desc->broadcast = 1;
L
Larry Finger 已提交
949

950 951
		_rtl_txrate_selectmode(hw, sta, tcb_desc);
		_rtl_query_bandwidth_mode(hw, sta, tcb_desc);
L
Larry Finger 已提交
952
		_rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
953
		_rtl_query_shortgi(hw, sta, tcb_desc, info);
L
Larry Finger 已提交
954 955 956
		_rtl_query_protection_mode(hw, tcb_desc, info);
	} else {
		tcb_desc->use_driver_rate = true;
957
		tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
L
Larry Finger 已提交
958 959
		tcb_desc->disable_ratefallback = 1;
		tcb_desc->mac_id = 0;
960
		tcb_desc->packet_bw = false;
L
Larry Finger 已提交
961 962 963 964
	}
}
EXPORT_SYMBOL(rtl_get_tcb_desc);

965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
static bool addbareq_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct ieee80211_sta *sta = NULL;
	struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
	struct rtl_sta_info *sta_entry = NULL;
	struct ieee80211_mgmt *mgmt = (void *)skb->data;
	u16 capab = 0, tid = 0;
	struct rtl_tid_data *tid_data;
	struct sk_buff *skb_delba = NULL;
	struct ieee80211_rx_status rx_status = { 0 };

	rcu_read_lock();
	sta = rtl_find_sta(hw, hdr->addr3);
	if (sta == NULL) {
		RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_EMERG,
			 "sta is NULL\n");
		rcu_read_unlock();
		return true;
	}

	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
	if (!sta_entry) {
		rcu_read_unlock();
		return true;
	}
	capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
	tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
	tid_data = &sta_entry->tids[tid];
	if (tid_data->agg.rx_agg_state == RTL_RX_AGG_START) {
		skb_delba = rtl_make_del_ba(hw, hdr->addr2, hdr->addr3, tid);
		if (skb_delba) {
997 998
			rx_status.freq = hw->conf.chandef.chan->center_freq;
			rx_status.band = hw->conf.chandef.chan->band;
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
			rx_status.flag |= RX_FLAG_DECRYPTED;
			rx_status.flag |= RX_FLAG_MACTIME_END;
			rx_status.rate_idx = 0;
			rx_status.signal = 50 + 10;
			memcpy(IEEE80211_SKB_RXCB(skb_delba), &rx_status,
			       sizeof(rx_status));
			RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG,
				      "fake del\n", skb_delba->data,
				      skb_delba->len);
			ieee80211_rx_irqsafe(hw, skb_delba);
		}
	}
	rcu_read_unlock();
	return false;
}

L
Larry Finger 已提交
1015 1016 1017
bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
{
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1018
	struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
L
Larry Finger 已提交
1019
	struct rtl_priv *rtlpriv = rtl_priv(hw);
1020
	__le16 fc = hdr->frame_control;
1021
	u8 *act = (u8 *)skb->data + MAC80211_3ADDR_LEN;
L
Larry Finger 已提交
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
	u8 category;

	if (!ieee80211_is_action(fc))
		return true;

	category = *act;
	act++;
	switch (category) {
	case ACT_CAT_BA:
		switch (*act) {
		case ACT_ADDBAREQ:
			if (mac->act_scanning)
				return false;

			RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1037 1038
				 "%s ACT_ADDBAREQ From :%pM\n",
				 is_tx ? "Tx" : "Rx", hdr->addr2);
1039 1040 1041 1042 1043
			RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG, "req\n",
				      skb->data, skb->len);
			if (!is_tx)
				if (addbareq_rx(hw, skb))
					return true;
L
Larry Finger 已提交
1044 1045 1046
			break;
		case ACT_ADDBARSP:
			RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1047 1048
				 "%s ACT_ADDBARSP From :%pM\n",
				 is_tx ? "Tx" : "Rx", hdr->addr2);
L
Larry Finger 已提交
1049 1050 1051
			break;
		case ACT_DELBA:
			RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1052
				 "ACT_ADDBADEL From :%pM\n", hdr->addr2);
L
Larry Finger 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061
			break;
		}
		break;
	default:
		break;
	}

	return true;
}
1062
EXPORT_SYMBOL_GPL(rtl_action_proc);
L
Larry Finger 已提交
1063 1064 1065 1066 1067 1068

/*should call before software enc*/
u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1069
	__le16 fc = rtl_get_fc(skb);
L
Larry Finger 已提交
1070 1071 1072 1073 1074
	u16 ether_type;
	u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
	const struct iphdr *ip;

	if (!ieee80211_is_data(fc))
1075
		return false;
L
Larry Finger 已提交
1076 1077 1078 1079 1080


	ip = (struct iphdr *)((u8 *) skb->data + mac_hdr_len +
			      SNAP_SIZE + PROTOC_TYPE_SIZE);
	ether_type = *(u16 *) ((u8 *) skb->data + mac_hdr_len + SNAP_SIZE);
1081
	/*	ether_type = ntohs(ether_type); */
L
Larry Finger 已提交
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095

	if (ETH_P_IP == ether_type) {
		if (IPPROTO_UDP == ip->protocol) {
			struct udphdr *udp = (struct udphdr *)((u8 *) ip +
							       (ip->ihl << 2));
			if (((((u8 *) udp)[1] == 68) &&
			     (((u8 *) udp)[3] == 67)) ||
			    ((((u8 *) udp)[1] == 67) &&
			     (((u8 *) udp)[3] == 68))) {
				/*
				 * 68 : UDP BOOTP client
				 * 67 : UDP BOOTP server
				 */
				RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV),
1096 1097
					 DBG_DMESG, "dhcp %s !!\n",
					 is_tx ? "Tx" : "Rx");
L
Larry Finger 已提交
1098 1099

				if (is_tx) {
1100
					rtlpriv->enter_ps = false;
1101
					schedule_work(&rtlpriv->
1102
						      works.lps_change_work);
L
Larry Finger 已提交
1103 1104 1105 1106 1107 1108 1109 1110 1111
					ppsc->last_delaylps_stamp_jiffies =
					    jiffies;
				}

				return true;
			}
		}
	} else if (ETH_P_ARP == ether_type) {
		if (is_tx) {
1112 1113
			rtlpriv->enter_ps = false;
			schedule_work(&rtlpriv->works.lps_change_work);
L
Larry Finger 已提交
1114 1115 1116 1117 1118 1119
			ppsc->last_delaylps_stamp_jiffies = jiffies;
		}

		return true;
	} else if (ETH_P_PAE == ether_type) {
		RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1120
			 "802.1X %s EAPOL pkt!!\n", is_tx ? "Tx" : "Rx");
L
Larry Finger 已提交
1121 1122

		if (is_tx) {
1123 1124
			rtlpriv->enter_ps = false;
			schedule_work(&rtlpriv->works.lps_change_work);
L
Larry Finger 已提交
1125 1126 1127 1128
			ppsc->last_delaylps_stamp_jiffies = jiffies;
		}

		return true;
1129 1130
	} else if (ETH_P_IPV6 == ether_type) {
		/* IPv6 */
L
Larry Finger 已提交
1131 1132 1133 1134 1135
		return true;
	}

	return false;
}
1136
EXPORT_SYMBOL_GPL(rtl_is_special_data);
L
Larry Finger 已提交
1137 1138 1139 1140 1141 1142

/*********************************************************
 *
 * functions called by core.c
 *
 *********************************************************/
1143 1144
int rtl_tx_agg_start(struct ieee80211_hw *hw,
		struct ieee80211_sta *sta, u16 tid, u16 *ssn)
L
Larry Finger 已提交
1145 1146 1147 1148
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_tid_data *tid_data;
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1149
	struct rtl_sta_info *sta_entry = NULL;
L
Larry Finger 已提交
1150

1151 1152
	if (sta == NULL)
		return -EINVAL;
L
Larry Finger 已提交
1153 1154 1155 1156

	if (unlikely(tid >= MAX_TID_COUNT))
		return -EINVAL;

1157 1158
	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
	if (!sta_entry)
L
Larry Finger 已提交
1159
		return -ENXIO;
1160
	tid_data = &sta_entry->tids[tid];
L
Larry Finger 已提交
1161

1162 1163
	RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "on ra = %pM tid = %d seq:%d\n",
		 sta->addr, tid, tid_data->seq_number);
L
Larry Finger 已提交
1164

1165 1166
	*ssn = tid_data->seq_number;
	tid_data->agg.agg_state = RTL_AGG_START;
L
Larry Finger 已提交
1167

1168
	ieee80211_start_tx_ba_cb_irqsafe(mac->vif, sta->addr, tid);
L
Larry Finger 已提交
1169 1170 1171 1172

	return 0;
}

1173 1174
int rtl_tx_agg_stop(struct ieee80211_hw *hw,
		struct ieee80211_sta *sta, u16 tid)
L
Larry Finger 已提交
1175 1176 1177
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1178
	struct rtl_sta_info *sta_entry = NULL;
L
Larry Finger 已提交
1179

1180 1181
	if (sta == NULL)
		return -EINVAL;
L
Larry Finger 已提交
1182

1183
	if (!sta->addr) {
1184
		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "ra = NULL\n");
L
Larry Finger 已提交
1185 1186 1187
		return -EINVAL;
	}

1188 1189
	RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "on ra = %pM tid = %d\n",
		 sta->addr, tid);
1190

L
Larry Finger 已提交
1191 1192 1193
	if (unlikely(tid >= MAX_TID_COUNT))
		return -EINVAL;

1194 1195
	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
	sta_entry->tids[tid].agg.agg_state = RTL_AGG_STOP;
L
Larry Finger 已提交
1196

1197
	ieee80211_stop_tx_ba_cb_irqsafe(mac->vif, sta->addr, tid);
L
Larry Finger 已提交
1198

1199 1200 1201
	return 0;
}

1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
int rtl_rx_agg_start(struct ieee80211_hw *hw,
		     struct ieee80211_sta *sta, u16 tid)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_tid_data *tid_data;
	struct rtl_sta_info *sta_entry = NULL;

	if (sta == NULL)
		return -EINVAL;

	if (unlikely(tid >= MAX_TID_COUNT))
		return -EINVAL;

	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
	if (!sta_entry)
		return -ENXIO;
	tid_data = &sta_entry->tids[tid];

	RT_TRACE(rtlpriv, COMP_RECV, DBG_DMESG,
		 "on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
		 tid_data->seq_number);

	tid_data->agg.rx_agg_state = RTL_RX_AGG_START;
	return 0;
}

int rtl_rx_agg_stop(struct ieee80211_hw *hw,
		    struct ieee80211_sta *sta, u16 tid)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_sta_info *sta_entry = NULL;

	if (sta == NULL)
		return -EINVAL;

	if (!sta->addr) {
		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "ra = NULL\n");
		return -EINVAL;
	}

	RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
		 "on ra = %pM tid = %d\n", sta->addr, tid);

	if (unlikely(tid >= MAX_TID_COUNT))
		return -EINVAL;

	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
	sta_entry->tids[tid].agg.rx_agg_state = RTL_RX_AGG_STOP;

	return 0;
}

1254 1255 1256 1257 1258
int rtl_tx_agg_oper(struct ieee80211_hw *hw,
		struct ieee80211_sta *sta, u16 tid)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_sta_info *sta_entry = NULL;
L
Larry Finger 已提交
1259

1260 1261 1262 1263
	if (sta == NULL)
		return -EINVAL;

	if (!sta->addr) {
1264
		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "ra = NULL\n");
1265 1266
		return -EINVAL;
	}
L
Larry Finger 已提交
1267

1268 1269
	RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "on ra = %pM tid = %d\n",
		 sta->addr, tid);
L
Larry Finger 已提交
1270

1271 1272
	if (unlikely(tid >= MAX_TID_COUNT))
		return -EINVAL;
L
Larry Finger 已提交
1273

1274 1275
	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
	sta_entry->tids[tid].agg.agg_state = RTL_AGG_OPERATIONAL;
L
Larry Finger 已提交
1276 1277 1278 1279 1280 1281 1282 1283 1284

	return 0;
}

/*********************************************************
 *
 * wq & timer callback functions
 *
 *********************************************************/
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
/* this function is used for roaming */
void rtl_beacon_statistic(struct ieee80211_hw *hw, struct sk_buff *skb)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;

	if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
		return;

	if (rtlpriv->mac80211.link_state < MAC80211_LINKED)
		return;

	/* check if this really is a beacon */
	if (!ieee80211_is_beacon(hdr->frame_control) &&
	    !ieee80211_is_probe_resp(hdr->frame_control))
		return;

	/* min. beacon length + FCS_LEN */
	if (skb->len <= 40 + FCS_LEN)
		return;

	/* and only beacons from the associated BSSID, please */
1307
	if (!ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
1308 1309 1310 1311
		return;

	rtlpriv->link_info.bcn_rx_inperiod++;
}
1312
EXPORT_SYMBOL_GPL(rtl_beacon_statistic);
1313

L
Larry Finger 已提交
1314 1315 1316 1317 1318 1319 1320 1321 1322
void rtl_watchdog_wq_callback(void *data)
{
	struct rtl_works *rtlworks = container_of_dwork_rtl(data,
							    struct rtl_works,
							    watchdog_wq);
	struct ieee80211_hw *hw = rtlworks->hw;
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1323
	bool busytraffic = false;
1324 1325
	bool tx_busy_traffic = false;
	bool rx_busy_traffic = false;
1326 1327
	bool higher_busytraffic = false;
	bool higher_busyrxtraffic = false;
1328
	u8 idx, tid;
L
Larry Finger 已提交
1329 1330 1331 1332
	u32 rx_cnt_inp4eriod = 0;
	u32 tx_cnt_inp4eriod = 0;
	u32 aver_rx_cnt_inperiod = 0;
	u32 aver_tx_cnt_inperiod = 0;
1333 1334
	u32 aver_tidtx_inperiod[MAX_TID_COUNT] = {0};
	u32 tidtx_inp4eriod[MAX_TID_COUNT] = {0};
L
Larry Finger 已提交
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347

	if (is_hal_stop(rtlhal))
		return;

	/* <1> Determine if action frame is allowed */
	if (mac->link_state > MAC80211_NOLINK) {
		if (mac->cnt_after_linked < 20)
			mac->cnt_after_linked++;
	} else {
		mac->cnt_after_linked = 0;
	}

	/*
1348
	 *<2> to check if traffic busy, if
L
Larry Finger 已提交
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
	 * busytraffic we don't change channel
	 */
	if (mac->link_state >= MAC80211_LINKED) {

		/* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
		for (idx = 0; idx <= 2; idx++) {
			rtlpriv->link_info.num_rx_in4period[idx] =
			    rtlpriv->link_info.num_rx_in4period[idx + 1];
			rtlpriv->link_info.num_tx_in4period[idx] =
			    rtlpriv->link_info.num_tx_in4period[idx + 1];
		}
		rtlpriv->link_info.num_rx_in4period[3] =
		    rtlpriv->link_info.num_rx_inperiod;
		rtlpriv->link_info.num_tx_in4period[3] =
		    rtlpriv->link_info.num_tx_inperiod;
		for (idx = 0; idx <= 3; idx++) {
			rx_cnt_inp4eriod +=
			    rtlpriv->link_info.num_rx_in4period[idx];
			tx_cnt_inp4eriod +=
			    rtlpriv->link_info.num_tx_in4period[idx];
		}
		aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
		aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;

		/* (2) check traffic busy */
1374
		if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100) {
1375
			busytraffic = true;
1376 1377 1378 1379 1380
			if (aver_rx_cnt_inperiod > aver_tx_cnt_inperiod)
				rx_busy_traffic = true;
			else
				tx_busy_traffic = false;
		}
L
Larry Finger 已提交
1381 1382 1383 1384

		/* Higher Tx/Rx data. */
		if (aver_rx_cnt_inperiod > 4000 ||
		    aver_tx_cnt_inperiod > 4000) {
1385
			higher_busytraffic = true;
L
Larry Finger 已提交
1386 1387 1388

			/* Extremely high Rx data. */
			if (aver_rx_cnt_inperiod > 5000)
1389
				higher_busyrxtraffic = true;
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
		}

		/* check every tid's tx traffic */
		for (tid = 0; tid <= 7; tid++) {
			for (idx = 0; idx <= 2; idx++)
				rtlpriv->link_info.tidtx_in4period[tid][idx] =
				  rtlpriv->link_info.tidtx_in4period[tid]
				  [idx + 1];
			rtlpriv->link_info.tidtx_in4period[tid][3] =
				rtlpriv->link_info.tidtx_inperiod[tid];

			for (idx = 0; idx <= 3; idx++)
				tidtx_inp4eriod[tid] +=
				  rtlpriv->link_info.tidtx_in4period[tid][idx];
			aver_tidtx_inperiod[tid] = tidtx_inp4eriod[tid] / 4;
			if (aver_tidtx_inperiod[tid] > 5000)
				rtlpriv->link_info.higher_busytxtraffic[tid] =
						   true;
L
Larry Finger 已提交
1408
			else
1409 1410
				rtlpriv->link_info.higher_busytxtraffic[tid] =
						   false;
L
Larry Finger 已提交
1411 1412 1413 1414 1415
		}

		if (((rtlpriv->link_info.num_rx_inperiod +
		      rtlpriv->link_info.num_tx_inperiod) > 8) ||
		    (rtlpriv->link_info.num_rx_inperiod > 2))
1416
			rtlpriv->enter_ps = true;
L
Larry Finger 已提交
1417
		else
1418
			rtlpriv->enter_ps = false;
L
Larry Finger 已提交
1419 1420

		/* LeisurePS only work in infra mode. */
1421
		schedule_work(&rtlpriv->works.lps_change_work);
L
Larry Finger 已提交
1422 1423 1424 1425
	}

	rtlpriv->link_info.num_rx_inperiod = 0;
	rtlpriv->link_info.num_tx_inperiod = 0;
1426 1427
	for (tid = 0; tid <= 7; tid++)
		rtlpriv->link_info.tidtx_inperiod[tid] = 0;
L
Larry Finger 已提交
1428

1429 1430
	rtlpriv->link_info.busytraffic = busytraffic;
	rtlpriv->link_info.higher_busytraffic = higher_busytraffic;
1431 1432
	rtlpriv->link_info.rx_busy_traffic = rx_busy_traffic;
	rtlpriv->link_info.tx_busy_traffic = tx_busy_traffic;
1433
	rtlpriv->link_info.higher_busyrxtraffic = higher_busyrxtraffic;
L
Larry Finger 已提交
1434

1435 1436
	/* <3> DM */
	rtlpriv->cfg->ops->dm_watchdog(hw);
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461

	/* <4> roaming */
	if (mac->link_state == MAC80211_LINKED &&
	    mac->opmode == NL80211_IFTYPE_STATION) {
		if ((rtlpriv->link_info.bcn_rx_inperiod +
		     rtlpriv->link_info.num_rx_inperiod) == 0) {
			rtlpriv->link_info.roam_times++;
			RT_TRACE(rtlpriv, COMP_ERR, DBG_DMESG,
				 "AP off for %d s\n",
				 (rtlpriv->link_info.roam_times * 2));

			/* if we can't recv beacon for 6s, we should
			 * reconnect this AP
			 */
			if (rtlpriv->link_info.roam_times >= 3) {
				RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
					 "AP off, try to reconnect now\n");
				rtlpriv->link_info.roam_times = 0;
				ieee80211_connection_loss(rtlpriv->mac80211.vif);
			}
		} else {
			rtlpriv->link_info.roam_times = 0;
		}
	}
	rtlpriv->link_info.bcn_rx_inperiod = 0;
L
Larry Finger 已提交
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
}

void rtl_watch_dog_timer_callback(unsigned long data)
{
	struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
	struct rtl_priv *rtlpriv = rtl_priv(hw);

	queue_delayed_work(rtlpriv->works.rtl_wq,
			   &rtlpriv->works.watchdog_wq, 0);

	mod_timer(&rtlpriv->works.watchdog_timer,
		  jiffies + MSECS(RTL_WATCH_DOG_TIME));
}

1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
void rtl_fwevt_wq_callback(void *data)
{
	struct rtl_works *rtlworks =
		container_of_dwork_rtl(data, struct rtl_works, fwevt_wq);
	struct ieee80211_hw *hw = rtlworks->hw;
	struct rtl_priv *rtlpriv = rtl_priv(hw);

	rtlpriv->cfg->ops->c2h_command_handle(hw);
}

void rtl_easy_concurrent_retrytimer_callback(unsigned long data)
{
	struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_priv *buddy_priv = rtlpriv->buddy_priv;

	if (buddy_priv == NULL)
		return;

	rtlpriv->cfg->ops->dualmac_easy_concurrent(hw);
}

1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
/*********************************************************
 *
 * frame process functions
 *
 *********************************************************/
u8 *rtl_find_ie(u8 *data, unsigned int len, u8 ie)
{
	struct ieee80211_mgmt *mgmt = (void *)data;
	u8 *pos, *end;

	pos = (u8 *)mgmt->u.beacon.variable;
	end = data + len;
	while (pos < end) {
		if (pos + 2 + pos[1] > end)
			return NULL;

		if (pos[0] == ie)
			return pos;

		pos += 2 + pos[1];
	}
	return NULL;
}

/* when we use 2 rx ants we send IEEE80211_SMPS_OFF */
/* when we use 1 rx ant we send IEEE80211_SMPS_STATIC */
1524
static struct sk_buff *rtl_make_smps_action(struct ieee80211_hw *hw,
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
		enum ieee80211_smps_mode smps, u8 *da, u8 *bssid)
{
	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
	struct sk_buff *skb;
	struct ieee80211_mgmt *action_frame;

	/* 27 = header + category + action + smps mode */
	skb = dev_alloc_skb(27 + hw->extra_tx_headroom);
	if (!skb)
		return NULL;

	skb_reserve(skb, hw->extra_tx_headroom);
	action_frame = (void *)skb_put(skb, 27);
	memset(action_frame, 0, 27);
	memcpy(action_frame->da, da, ETH_ALEN);
	memcpy(action_frame->sa, rtlefuse->dev_addr, ETH_ALEN);
	memcpy(action_frame->bssid, bssid, ETH_ALEN);
	action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
						  IEEE80211_STYPE_ACTION);
	action_frame->u.action.category = WLAN_CATEGORY_HT;
	action_frame->u.action.u.ht_smps.action = WLAN_HT_ACTION_SMPS;
	switch (smps) {
	case IEEE80211_SMPS_AUTOMATIC:/* 0 */
	case IEEE80211_SMPS_NUM_MODES:/* 4 */
		WARN_ON(1);
	case IEEE80211_SMPS_OFF:/* 1 */ /*MIMO_PS_NOLIMIT*/
		action_frame->u.action.u.ht_smps.smps_control =
				WLAN_HT_SMPS_CONTROL_DISABLED;/* 0 */
		break;
	case IEEE80211_SMPS_STATIC:/* 2 */ /*MIMO_PS_STATIC*/
		action_frame->u.action.u.ht_smps.smps_control =
				WLAN_HT_SMPS_CONTROL_STATIC;/* 1 */
		break;
	case IEEE80211_SMPS_DYNAMIC:/* 3 */ /*MIMO_PS_DYNAMIC*/
		action_frame->u.action.u.ht_smps.smps_control =
				WLAN_HT_SMPS_CONTROL_DYNAMIC;/* 3 */
		break;
	}

	return skb;
}

int rtl_send_smps_action(struct ieee80211_hw *hw,
1568
		struct ieee80211_sta *sta,
1569 1570 1571 1572 1573
		enum ieee80211_smps_mode smps)
{
	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));
1574
	struct sk_buff *skb = NULL;
1575
	struct rtl_tcb_desc tcb_desc;
1576 1577
	u8 bssid[ETH_ALEN] = {0};

1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
	memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));

	if (rtlpriv->mac80211.act_scanning)
		goto err_free;

	if (!sta)
		goto err_free;

	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;

1592 1593 1594 1595 1596 1597
	if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP)
		memcpy(bssid, rtlpriv->efuse.dev_addr, ETH_ALEN);
	else
		memcpy(bssid, rtlpriv->mac80211.bssid, ETH_ALEN);

	skb = rtl_make_smps_action(hw, smps, sta->addr, bssid);
1598 1599 1600 1601 1602 1603 1604 1605
	/* this is a type = mgmt * stype = action frame */
	if (skb) {
		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
		struct rtl_sta_info *sta_entry =
			(struct rtl_sta_info *) sta->drv_priv;
		sta_entry->mimo_ps = smps;

		info->control.rates[0].idx = 0;
1606
		info->band = hw->conf.chandef.chan->band;
1607
		rtlpriv->intf_ops->adapter_tx(hw, sta, skb, &tcb_desc);
1608
	}
1609 1610
	return 1;

1611 1612 1613
err_free:
	return 0;
}
1614 1615
EXPORT_SYMBOL(rtl_send_smps_action);

1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
void rtl_phy_scan_operation_backup(struct ieee80211_hw *hw, u8 operation)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
	enum io_type iotype;

	if (!is_hal_stop(rtlhal)) {
		switch (operation) {
		case SCAN_OPT_BACKUP:
			iotype = IO_CMD_PAUSE_DM_BY_SCAN;
			rtlpriv->cfg->ops->set_hw_reg(hw,
						      HW_VAR_IO_CMD,
						      (u8 *)&iotype);
			break;
		case SCAN_OPT_RESTORE:
			iotype = IO_CMD_RESUME_DM_BY_SCAN;
			rtlpriv->cfg->ops->set_hw_reg(hw,
						      HW_VAR_IO_CMD,
						      (u8 *)&iotype);
			break;
		default:
			RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
				 "Unknown Scan Backup operation.\n");
			break;
		}
	}
}
EXPORT_SYMBOL(rtl_phy_scan_operation_backup);

1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
/* There seem to be issues in mac80211 regarding when del ba frames can be
 * received. As a work around, we make a fake del_ba if we receive a ba_req;
 * however, rx_agg was opened to let mac80211 release some ba related
 * resources. This del_ba is for tx only.
 */
struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw,
				u8 *sa, u8 *bssid, u16 tid)
{
	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
	struct sk_buff *skb;
	struct ieee80211_mgmt *action_frame;
	u16 params;

	/* 27 = header + category + action + smps mode */
	skb = dev_alloc_skb(34 + hw->extra_tx_headroom);
	if (!skb)
		return NULL;

	skb_reserve(skb, hw->extra_tx_headroom);
	action_frame = (void *)skb_put(skb, 34);
	memset(action_frame, 0, 34);
	memcpy(action_frame->sa, sa, ETH_ALEN);
	memcpy(action_frame->da, rtlefuse->dev_addr, ETH_ALEN);
	memcpy(action_frame->bssid, bssid, ETH_ALEN);
	action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
						  IEEE80211_STYPE_ACTION);
	action_frame->u.action.category = WLAN_CATEGORY_BACK;
	action_frame->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
	params = (u16)(1 << 11);	/* bit 11 initiator */
	params |= (u16)(tid << 12);		/* bit 15:12 TID number */

	action_frame->u.action.u.delba.params = cpu_to_le16(params);
	action_frame->u.action.u.delba.reason_code =
		cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT);

	return skb;
}
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

/*********************************************************
 *
 * IOT functions
 *
 *********************************************************/
static bool rtl_chk_vendor_ouisub(struct ieee80211_hw *hw,
		struct octet_string vendor_ie)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	bool matched = false;
	static u8 athcap_1[] = { 0x00, 0x03, 0x7F };
	static u8 athcap_2[] = { 0x00, 0x13, 0x74 };
	static u8 broadcap_1[] = { 0x00, 0x10, 0x18 };
	static u8 broadcap_2[] = { 0x00, 0x0a, 0xf7 };
	static u8 broadcap_3[] = { 0x00, 0x05, 0xb5 };
	static u8 racap[] = { 0x00, 0x0c, 0x43 };
	static u8 ciscocap[] = { 0x00, 0x40, 0x96 };
	static u8 marvcap[] = { 0x00, 0x50, 0x43 };

	if (memcmp(vendor_ie.octet, athcap_1, 3) == 0 ||
		memcmp(vendor_ie.octet, athcap_2, 3) == 0) {
		rtlpriv->mac80211.vendor = PEER_ATH;
		matched = true;
	} else if (memcmp(vendor_ie.octet, broadcap_1, 3) == 0 ||
		memcmp(vendor_ie.octet, broadcap_2, 3) == 0 ||
		memcmp(vendor_ie.octet, broadcap_3, 3) == 0) {
		rtlpriv->mac80211.vendor = PEER_BROAD;
		matched = true;
	} else if (memcmp(vendor_ie.octet, racap, 3) == 0) {
		rtlpriv->mac80211.vendor = PEER_RAL;
		matched = true;
	} else if (memcmp(vendor_ie.octet, ciscocap, 3) == 0) {
		rtlpriv->mac80211.vendor = PEER_CISCO;
		matched = true;
	} else if (memcmp(vendor_ie.octet, marvcap, 3) == 0) {
		rtlpriv->mac80211.vendor = PEER_MARV;
		matched = true;
	}

	return matched;
}

1725
static bool rtl_find_221_ie(struct ieee80211_hw *hw, u8 *data,
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 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
		unsigned int len)
{
	struct ieee80211_mgmt *mgmt = (void *)data;
	struct octet_string vendor_ie;
	u8 *pos, *end;

	pos = (u8 *)mgmt->u.beacon.variable;
	end = data + len;
	while (pos < end) {
		if (pos[0] == 221) {
			vendor_ie.length = pos[1];
			vendor_ie.octet = &pos[2];
			if (rtl_chk_vendor_ouisub(hw, vendor_ie))
				return true;
		}

		if (pos + 2 + pos[1] > end)
			return false;

		pos += 2 + pos[1];
	}
	return false;
}

void rtl_recognize_peer(struct ieee80211_hw *hw, u8 *data, unsigned int len)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
	struct ieee80211_hdr *hdr = (void *)data;
	u32 vendor = PEER_UNKNOWN;

	static u8 ap3_1[3] = { 0x00, 0x14, 0xbf };
	static u8 ap3_2[3] = { 0x00, 0x1a, 0x70 };
	static u8 ap3_3[3] = { 0x00, 0x1d, 0x7e };
	static u8 ap4_1[3] = { 0x00, 0x90, 0xcc };
	static u8 ap4_2[3] = { 0x00, 0x0e, 0x2e };
	static u8 ap4_3[3] = { 0x00, 0x18, 0x02 };
	static u8 ap4_4[3] = { 0x00, 0x17, 0x3f };
	static u8 ap4_5[3] = { 0x00, 0x1c, 0xdf };
	static u8 ap5_1[3] = { 0x00, 0x1c, 0xf0 };
	static u8 ap5_2[3] = { 0x00, 0x21, 0x91 };
	static u8 ap5_3[3] = { 0x00, 0x24, 0x01 };
	static u8 ap5_4[3] = { 0x00, 0x15, 0xe9 };
	static u8 ap5_5[3] = { 0x00, 0x17, 0x9A };
	static u8 ap5_6[3] = { 0x00, 0x18, 0xE7 };
	static u8 ap6_1[3] = { 0x00, 0x17, 0x94 };
	static u8 ap7_1[3] = { 0x00, 0x14, 0xa4 };

	if (mac->opmode != NL80211_IFTYPE_STATION)
		return;

	if (mac->link_state == MAC80211_NOLINK) {
		mac->vendor = PEER_UNKNOWN;
		return;
	}

	if (mac->cnt_after_linked > 2)
		return;

	/* check if this really is a beacon */
	if (!ieee80211_is_beacon(hdr->frame_control))
		return;

	/* min. beacon length + FCS_LEN */
	if (len <= 40 + FCS_LEN)
		return;

	/* and only beacons from the associated BSSID, please */
1794
	if (!ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
		return;

	if (rtl_find_221_ie(hw, data, len))
		vendor = mac->vendor;

	if ((memcmp(mac->bssid, ap5_1, 3) == 0) ||
		(memcmp(mac->bssid, ap5_2, 3) == 0) ||
		(memcmp(mac->bssid, ap5_3, 3) == 0) ||
		(memcmp(mac->bssid, ap5_4, 3) == 0) ||
		(memcmp(mac->bssid, ap5_5, 3) == 0) ||
		(memcmp(mac->bssid, ap5_6, 3) == 0) ||
		vendor == PEER_ATH) {
		vendor = PEER_ATH;
1808
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ath find\n");
1809 1810 1811 1812 1813 1814
	} else if ((memcmp(mac->bssid, ap4_4, 3) == 0) ||
		(memcmp(mac->bssid, ap4_5, 3) == 0) ||
		(memcmp(mac->bssid, ap4_1, 3) == 0) ||
		(memcmp(mac->bssid, ap4_2, 3) == 0) ||
		(memcmp(mac->bssid, ap4_3, 3) == 0) ||
		vendor == PEER_RAL) {
1815
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ral find\n");
1816 1817 1818 1819
		vendor = PEER_RAL;
	} else if (memcmp(mac->bssid, ap6_1, 3) == 0 ||
		vendor == PEER_CISCO) {
		vendor = PEER_CISCO;
1820
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>cisco find\n");
1821 1822 1823 1824
	} else if ((memcmp(mac->bssid, ap3_1, 3) == 0) ||
		(memcmp(mac->bssid, ap3_2, 3) == 0) ||
		(memcmp(mac->bssid, ap3_3, 3) == 0) ||
		vendor == PEER_BROAD) {
1825
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>broad find\n");
1826 1827 1828 1829
		vendor = PEER_BROAD;
	} else if (memcmp(mac->bssid, ap7_1, 3) == 0 ||
		vendor == PEER_MARV) {
		vendor = PEER_MARV;
1830
		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>marv find\n");
1831 1832 1833 1834
	}

	mac->vendor = vendor;
}
1835
EXPORT_SYMBOL_GPL(rtl_recognize_peer);
1836

L
Larry Finger 已提交
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
/*********************************************************
 *
 * sysfs functions
 *
 *********************************************************/
static ssize_t rtl_show_debug_level(struct device *d,
				    struct device_attribute *attr, char *buf)
{
	struct ieee80211_hw *hw = dev_get_drvdata(d);
	struct rtl_priv *rtlpriv = rtl_priv(hw);

	return sprintf(buf, "0x%08X\n", rtlpriv->dbg.global_debuglevel);
}

static ssize_t rtl_store_debug_level(struct device *d,
				     struct device_attribute *attr,
				     const char *buf, size_t count)
{
	struct ieee80211_hw *hw = dev_get_drvdata(d);
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	unsigned long val;
	int ret;

1860
	ret = kstrtoul(buf, 0, &val);
L
Larry Finger 已提交
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
	if (ret) {
		printk(KERN_DEBUG "%s is not in hex or decimal form.\n", buf);
	} else {
		rtlpriv->dbg.global_debuglevel = val;
		printk(KERN_DEBUG "debuglevel:%x\n",
		       rtlpriv->dbg.global_debuglevel);
	}

	return strnlen(buf, count);
}

static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
		   rtl_show_debug_level, rtl_store_debug_level);

static struct attribute *rtl_sysfs_entries[] = {

	&dev_attr_debug_level.attr,

	NULL
};

/*
 * "name" is folder name witch will be
 * put in device directory like :
 * sys/devices/pci0000:00/0000:00:1c.4/
 * 0000:06:00.0/rtl_sysfs
 */
struct attribute_group rtl_attribute_group = {
	.name = "rtlsysfs",
	.attrs = rtl_sysfs_entries,
};
1892
EXPORT_SYMBOL_GPL(rtl_attribute_group);
L
Larry Finger 已提交
1893 1894 1895 1896 1897 1898 1899

MODULE_AUTHOR("lizhaoming	<chaoming_li@realsil.com.cn>");
MODULE_AUTHOR("Realtek WlanFAE	<wlanfae@realtek.com>");
MODULE_AUTHOR("Larry Finger	<Larry.FInger@lwfinger.net>");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");

1900 1901
struct rtl_global_var rtl_global_var = {};
EXPORT_SYMBOL_GPL(rtl_global_var);
1902

L
Larry Finger 已提交
1903 1904
static int __init rtl_core_module_init(void)
{
1905
	if (rtl_rate_control_register())
1906
		pr_err("Unable to register rtl_rc, use default RC !!\n");
1907

1908
	/* init some global vars */
1909 1910
	INIT_LIST_HEAD(&rtl_global_var.glb_priv_list);
	spin_lock_init(&rtl_global_var.glb_list_lock);
1911

L
Larry Finger 已提交
1912 1913 1914 1915 1916
	return 0;
}

static void __exit rtl_core_module_exit(void)
{
1917
	/*RC*/
1918
	rtl_rate_control_unregister();
L
Larry Finger 已提交
1919 1920 1921 1922
}

module_init(rtl_core_module_init);
module_exit(rtl_core_module_exit);