Linux Audio

Check our new training course

Loading...
Note: File does not exist in v6.8.
   1/******************************************************************************
   2 *
   3 * Copyright(c) 2009-2010  Realtek Corporation.
   4 *
   5 * This program is free software; you can redistribute it and/or modify it
   6 * under the terms of version 2 of the GNU General Public License as
   7 * published by the Free Software Foundation.
   8 *
   9 * This program is distributed in the hope that it will be useful, but WITHOUT
  10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  12 * more details.
  13 *
  14 * You should have received a copy of the GNU General Public License along with
  15 * this program; if not, write to the Free Software Foundation, Inc.,
  16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17 *
  18 * The full GNU General Public License is included in this distribution in the
  19 * file called LICENSE.
  20 *
  21 * Contact Information:
  22 * wlanfae <wlanfae@realtek.com>
  23 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
  24 * Hsinchu 300, Taiwan.
  25 *
  26 * Larry Finger <Larry.Finger@lwfinger.net>
  27 *
  28 *****************************************************************************/
  29
  30#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  31
  32#include <linux/ip.h>
  33#include "wifi.h"
  34#include "rc.h"
  35#include "base.h"
  36#include "efuse.h"
  37#include "cam.h"
  38#include "ps.h"
  39#include "regd.h"
  40
  41/*
  42 *NOTICE!!!: This file will be very big, we hsould
  43 *keep it clear under follwing roles:
  44 *
  45 *This file include follwing part, so, if you add new
  46 *functions into this file, please check which part it
  47 *should includes. or check if you should add new part
  48 *for this file:
  49 *
  50 *1) mac80211 init functions
  51 *2) tx information functions
  52 *3) functions called by core.c
  53 *4) wq & timer callback functions
  54 *5) frame process functions
  55 *6) IOT functions
  56 *7) sysfs functions
  57 *8) ...
  58 */
  59
  60/*********************************************************
  61 *
  62 * mac80211 init functions
  63 *
  64 *********************************************************/
  65static struct ieee80211_channel rtl_channeltable_2g[] = {
  66	{.center_freq = 2412, .hw_value = 1,},
  67	{.center_freq = 2417, .hw_value = 2,},
  68	{.center_freq = 2422, .hw_value = 3,},
  69	{.center_freq = 2427, .hw_value = 4,},
  70	{.center_freq = 2432, .hw_value = 5,},
  71	{.center_freq = 2437, .hw_value = 6,},
  72	{.center_freq = 2442, .hw_value = 7,},
  73	{.center_freq = 2447, .hw_value = 8,},
  74	{.center_freq = 2452, .hw_value = 9,},
  75	{.center_freq = 2457, .hw_value = 10,},
  76	{.center_freq = 2462, .hw_value = 11,},
  77	{.center_freq = 2467, .hw_value = 12,},
  78	{.center_freq = 2472, .hw_value = 13,},
  79	{.center_freq = 2484, .hw_value = 14,},
  80};
  81
  82static struct ieee80211_channel rtl_channeltable_5g[] = {
  83	{.center_freq = 5180, .hw_value = 36,},
  84	{.center_freq = 5200, .hw_value = 40,},
  85	{.center_freq = 5220, .hw_value = 44,},
  86	{.center_freq = 5240, .hw_value = 48,},
  87	{.center_freq = 5260, .hw_value = 52,},
  88	{.center_freq = 5280, .hw_value = 56,},
  89	{.center_freq = 5300, .hw_value = 60,},
  90	{.center_freq = 5320, .hw_value = 64,},
  91	{.center_freq = 5500, .hw_value = 100,},
  92	{.center_freq = 5520, .hw_value = 104,},
  93	{.center_freq = 5540, .hw_value = 108,},
  94	{.center_freq = 5560, .hw_value = 112,},
  95	{.center_freq = 5580, .hw_value = 116,},
  96	{.center_freq = 5600, .hw_value = 120,},
  97	{.center_freq = 5620, .hw_value = 124,},
  98	{.center_freq = 5640, .hw_value = 128,},
  99	{.center_freq = 5660, .hw_value = 132,},
 100	{.center_freq = 5680, .hw_value = 136,},
 101	{.center_freq = 5700, .hw_value = 140,},
 102	{.center_freq = 5745, .hw_value = 149,},
 103	{.center_freq = 5765, .hw_value = 153,},
 104	{.center_freq = 5785, .hw_value = 157,},
 105	{.center_freq = 5805, .hw_value = 161,},
 106	{.center_freq = 5825, .hw_value = 165,},
 107};
 108
 109static struct ieee80211_rate rtl_ratetable_2g[] = {
 110	{.bitrate = 10, .hw_value = 0x00,},
 111	{.bitrate = 20, .hw_value = 0x01,},
 112	{.bitrate = 55, .hw_value = 0x02,},
 113	{.bitrate = 110, .hw_value = 0x03,},
 114	{.bitrate = 60, .hw_value = 0x04,},
 115	{.bitrate = 90, .hw_value = 0x05,},
 116	{.bitrate = 120, .hw_value = 0x06,},
 117	{.bitrate = 180, .hw_value = 0x07,},
 118	{.bitrate = 240, .hw_value = 0x08,},
 119	{.bitrate = 360, .hw_value = 0x09,},
 120	{.bitrate = 480, .hw_value = 0x0a,},
 121	{.bitrate = 540, .hw_value = 0x0b,},
 122};
 123
 124static struct ieee80211_rate rtl_ratetable_5g[] = {
 125	{.bitrate = 60, .hw_value = 0x04,},
 126	{.bitrate = 90, .hw_value = 0x05,},
 127	{.bitrate = 120, .hw_value = 0x06,},
 128	{.bitrate = 180, .hw_value = 0x07,},
 129	{.bitrate = 240, .hw_value = 0x08,},
 130	{.bitrate = 360, .hw_value = 0x09,},
 131	{.bitrate = 480, .hw_value = 0x0a,},
 132	{.bitrate = 540, .hw_value = 0x0b,},
 133};
 134
 135static const struct ieee80211_supported_band rtl_band_2ghz = {
 136	.band = IEEE80211_BAND_2GHZ,
 137
 138	.channels = rtl_channeltable_2g,
 139	.n_channels = ARRAY_SIZE(rtl_channeltable_2g),
 140
 141	.bitrates = rtl_ratetable_2g,
 142	.n_bitrates = ARRAY_SIZE(rtl_ratetable_2g),
 143
 144	.ht_cap = {0},
 145};
 146
 147static struct ieee80211_supported_band rtl_band_5ghz = {
 148	.band = IEEE80211_BAND_5GHZ,
 149
 150	.channels = rtl_channeltable_5g,
 151	.n_channels = ARRAY_SIZE(rtl_channeltable_5g),
 152
 153	.bitrates = rtl_ratetable_5g,
 154	.n_bitrates = ARRAY_SIZE(rtl_ratetable_5g),
 155
 156	.ht_cap = {0},
 157};
 158
 159static const u8 tid_to_ac[] = {
 160	2, /* IEEE80211_AC_BE */
 161	3, /* IEEE80211_AC_BK */
 162	3, /* IEEE80211_AC_BK */
 163	2, /* IEEE80211_AC_BE */
 164	1, /* IEEE80211_AC_VI */
 165	1, /* IEEE80211_AC_VI */
 166	0, /* IEEE80211_AC_VO */
 167	0, /* IEEE80211_AC_VO */
 168};
 169
 170u8 rtl_tid_to_ac(struct ieee80211_hw *hw, u8 tid)
 171{
 172	return tid_to_ac[tid];
 173}
 174
 175static void _rtl_init_hw_ht_capab(struct ieee80211_hw *hw,
 176				  struct ieee80211_sta_ht_cap *ht_cap)
 177{
 178	struct rtl_priv *rtlpriv = rtl_priv(hw);
 179	struct rtl_phy *rtlphy = &(rtlpriv->phy);
 180
 181	ht_cap->ht_supported = true;
 182	ht_cap->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
 183	    IEEE80211_HT_CAP_SGI_40 |
 184	    IEEE80211_HT_CAP_SGI_20 |
 185	    IEEE80211_HT_CAP_DSSSCCK40 | IEEE80211_HT_CAP_MAX_AMSDU;
 186
 187	if (rtlpriv->rtlhal.disable_amsdu_8k)
 188		ht_cap->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
 189
 190	/*
 191	 *Maximum length of AMPDU that the STA can receive.
 192	 *Length = 2 ^ (13 + max_ampdu_length_exp) - 1 (octets)
 193	 */
 194	ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
 195
 196	/*Minimum MPDU start spacing , */
 197	ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
 198
 199	ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
 200
 201	/*
 202	 *hw->wiphy->bands[IEEE80211_BAND_2GHZ]
 203	 *base on ant_num
 204	 *rx_mask: RX mask
 205	 *if rx_ant =1 rx_mask[0]=0xff;==>MCS0-MCS7
 206	 *if rx_ant =2 rx_mask[1]=0xff;==>MCS8-MCS15
 207	 *if rx_ant >=3 rx_mask[2]=0xff;
 208	 *if BW_40 rx_mask[4]=0x01;
 209	 *highest supported RX rate
 210	 */
 211	if (get_rf_type(rtlphy) == RF_1T2R || get_rf_type(rtlphy) == RF_2T2R) {
 212
 213		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, ("1T2R or 2T2R\n"));
 214
 215		ht_cap->mcs.rx_mask[0] = 0xFF;
 216		ht_cap->mcs.rx_mask[1] = 0xFF;
 217		ht_cap->mcs.rx_mask[4] = 0x01;
 218
 219		ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
 220	} else if (get_rf_type(rtlphy) == RF_1T1R) {
 221
 222		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, ("1T1R\n"));
 223
 224		ht_cap->mcs.rx_mask[0] = 0xFF;
 225		ht_cap->mcs.rx_mask[1] = 0x00;
 226		ht_cap->mcs.rx_mask[4] = 0x01;
 227
 228		ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7);
 229	}
 230}
 231
 232static void _rtl_init_mac80211(struct ieee80211_hw *hw)
 233{
 234	struct rtl_priv *rtlpriv = rtl_priv(hw);
 235	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
 236	struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
 237	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
 238	struct ieee80211_supported_band *sband;
 239
 240
 241	if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY && rtlhal->bandset ==
 242	    BAND_ON_BOTH) {
 243		/* 1: 2.4 G bands */
 244		/* <1> use  mac->bands as mem for hw->wiphy->bands */
 245		sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
 246
 247		/* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
 248		 * to default value(1T1R) */
 249		memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]), &rtl_band_2ghz,
 250				sizeof(struct ieee80211_supported_band));
 251
 252		/* <3> init ht cap base on ant_num */
 253		_rtl_init_hw_ht_capab(hw, &sband->ht_cap);
 254
 255		/* <4> set mac->sband to wiphy->sband */
 256		hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
 257
 258		/* 2: 5 G bands */
 259		/* <1> use  mac->bands as mem for hw->wiphy->bands */
 260		sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
 261
 262		/* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
 263		 * to default value(1T1R) */
 264		memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]), &rtl_band_5ghz,
 265				sizeof(struct ieee80211_supported_band));
 266
 267		/* <3> init ht cap base on ant_num */
 268		_rtl_init_hw_ht_capab(hw, &sband->ht_cap);
 269
 270		/* <4> set mac->sband to wiphy->sband */
 271		hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
 272	} else {
 273		if (rtlhal->current_bandtype == BAND_ON_2_4G) {
 274			/* <1> use  mac->bands as mem for hw->wiphy->bands */
 275			sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
 276
 277			/* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
 278			 * to default value(1T1R) */
 279			memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]),
 280				 &rtl_band_2ghz,
 281				 sizeof(struct ieee80211_supported_band));
 282
 283			/* <3> init ht cap base on ant_num */
 284			_rtl_init_hw_ht_capab(hw, &sband->ht_cap);
 285
 286			/* <4> set mac->sband to wiphy->sband */
 287			hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
 288		} else if (rtlhal->current_bandtype == BAND_ON_5G) {
 289			/* <1> use  mac->bands as mem for hw->wiphy->bands */
 290			sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
 291
 292			/* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
 293			 * to default value(1T1R) */
 294			memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]),
 295				 &rtl_band_5ghz,
 296				 sizeof(struct ieee80211_supported_band));
 297
 298			/* <3> init ht cap base on ant_num */
 299			_rtl_init_hw_ht_capab(hw, &sband->ht_cap);
 300
 301			/* <4> set mac->sband to wiphy->sband */
 302			hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
 303		} else {
 304			RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG,
 305				 ("Err BAND %d\n",
 306				 rtlhal->current_bandtype));
 307		}
 308	}
 309	/* <5> set hw caps */
 310	hw->flags = IEEE80211_HW_SIGNAL_DBM |
 311	    IEEE80211_HW_RX_INCLUDES_FCS |
 312	    IEEE80211_HW_BEACON_FILTER |
 313	    IEEE80211_HW_AMPDU_AGGREGATION |
 314	    IEEE80211_HW_REPORTS_TX_ACK_STATUS | 0;
 315
 316	/* swlps or hwlps has been set in diff chip in init_sw_vars */
 317	if (rtlpriv->psc.swctrl_lps)
 318		hw->flags |= IEEE80211_HW_SUPPORTS_PS |
 319			IEEE80211_HW_PS_NULLFUNC_STACK |
 320			/* IEEE80211_HW_SUPPORTS_DYNAMIC_PS | */
 321			0;
 322
 323	hw->wiphy->interface_modes =
 324	    BIT(NL80211_IFTYPE_AP) |
 325	    BIT(NL80211_IFTYPE_STATION) |
 326	    BIT(NL80211_IFTYPE_ADHOC);
 327
 328	hw->wiphy->rts_threshold = 2347;
 329
 330	hw->queues = AC_MAX;
 331	hw->extra_tx_headroom = RTL_TX_HEADER_SIZE;
 332
 333	/* TODO: Correct this value for our hw */
 334	/* TODO: define these hard code value */
 335	hw->channel_change_time = 100;
 336	hw->max_listen_interval = 10;
 337	hw->max_rate_tries = 4;
 338	/* hw->max_rates = 1; */
 339	hw->sta_data_size = sizeof(struct rtl_sta_info);
 340
 341	/* <6> mac address */
 342	if (is_valid_ether_addr(rtlefuse->dev_addr)) {
 343		SET_IEEE80211_PERM_ADDR(hw, rtlefuse->dev_addr);
 344	} else {
 345		u8 rtlmac[] = { 0x00, 0xe0, 0x4c, 0x81, 0x92, 0x00 };
 346		get_random_bytes((rtlmac + (ETH_ALEN - 1)), 1);
 347		SET_IEEE80211_PERM_ADDR(hw, rtlmac);
 348	}
 349
 350}
 351
 352static void _rtl_init_deferred_work(struct ieee80211_hw *hw)
 353{
 354	struct rtl_priv *rtlpriv = rtl_priv(hw);
 355
 356	/* <1> timer */
 357	init_timer(&rtlpriv->works.watchdog_timer);
 358	setup_timer(&rtlpriv->works.watchdog_timer,
 359		    rtl_watch_dog_timer_callback, (unsigned long)hw);
 360
 361	/* <2> work queue */
 362	rtlpriv->works.hw = hw;
 363	rtlpriv->works.rtl_wq = alloc_workqueue(rtlpriv->cfg->name, 0, 0);
 364	INIT_DELAYED_WORK(&rtlpriv->works.watchdog_wq,
 365			  (void *)rtl_watchdog_wq_callback);
 366	INIT_DELAYED_WORK(&rtlpriv->works.ips_nic_off_wq,
 367			  (void *)rtl_ips_nic_off_wq_callback);
 368	INIT_DELAYED_WORK(&rtlpriv->works.ps_work,
 369			  (void *)rtl_swlps_wq_callback);
 370	INIT_DELAYED_WORK(&rtlpriv->works.ps_rfon_wq,
 371			  (void *)rtl_swlps_rfon_wq_callback);
 372
 373}
 374
 375void rtl_deinit_deferred_work(struct ieee80211_hw *hw)
 376{
 377	struct rtl_priv *rtlpriv = rtl_priv(hw);
 378
 379	del_timer_sync(&rtlpriv->works.watchdog_timer);
 380
 381	cancel_delayed_work(&rtlpriv->works.watchdog_wq);
 382	cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
 383	cancel_delayed_work(&rtlpriv->works.ps_work);
 384	cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
 385}
 386
 387void rtl_init_rfkill(struct ieee80211_hw *hw)
 388{
 389	struct rtl_priv *rtlpriv = rtl_priv(hw);
 390
 391	bool radio_state;
 392	bool blocked;
 393	u8 valid = 0;
 394
 395	/*set init state to on */
 396	rtlpriv->rfkill.rfkill_state = 1;
 397	wiphy_rfkill_set_hw_state(hw->wiphy, 0);
 398
 399	radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
 400
 401	if (valid) {
 402		pr_info("wireless switch is %s\n",
 403			rtlpriv->rfkill.rfkill_state ? "on" : "off");
 404
 405		rtlpriv->rfkill.rfkill_state = radio_state;
 406
 407		blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
 408		wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
 409	}
 410
 411	wiphy_rfkill_start_polling(hw->wiphy);
 412}
 413
 414void rtl_deinit_rfkill(struct ieee80211_hw *hw)
 415{
 416	wiphy_rfkill_stop_polling(hw->wiphy);
 417}
 418
 419int rtl_init_core(struct ieee80211_hw *hw)
 420{
 421	struct rtl_priv *rtlpriv = rtl_priv(hw);
 422	struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
 423
 424	/* <1> init mac80211 */
 425	_rtl_init_mac80211(hw);
 426	rtlmac->hw = hw;
 427
 428	/* <2> rate control register */
 429	hw->rate_control_algorithm = "rtl_rc";
 430
 431	/*
 432	 * <3> init CRDA must come after init
 433	 * mac80211 hw  in _rtl_init_mac80211.
 434	 */
 435	if (rtl_regd_init(hw, rtl_reg_notifier)) {
 436		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, ("REGD init failed\n"));
 437		return 1;
 438	} else {
 439		/* CRDA regd hint must after init CRDA */
 440		if (regulatory_hint(hw->wiphy, rtlpriv->regd.alpha2)) {
 441			RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
 442				 ("regulatory_hint fail\n"));
 443		}
 444	}
 445
 446	/* <4> locks */
 447	mutex_init(&rtlpriv->locks.conf_mutex);
 448	spin_lock_init(&rtlpriv->locks.ips_lock);
 449	spin_lock_init(&rtlpriv->locks.irq_th_lock);
 450	spin_lock_init(&rtlpriv->locks.h2c_lock);
 451	spin_lock_init(&rtlpriv->locks.rf_ps_lock);
 452	spin_lock_init(&rtlpriv->locks.rf_lock);
 453	spin_lock_init(&rtlpriv->locks.lps_lock);
 454	spin_lock_init(&rtlpriv->locks.waitq_lock);
 455	spin_lock_init(&rtlpriv->locks.cck_and_rw_pagea_lock);
 456
 457	rtlmac->link_state = MAC80211_NOLINK;
 458
 459	/* <5> init deferred work */
 460	_rtl_init_deferred_work(hw);
 461
 462	return 0;
 463}
 464
 465void rtl_deinit_core(struct ieee80211_hw *hw)
 466{
 467}
 468
 469void rtl_init_rx_config(struct ieee80211_hw *hw)
 470{
 471	struct rtl_priv *rtlpriv = rtl_priv(hw);
 472	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
 473
 474	rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
 475}
 476
 477/*********************************************************
 478 *
 479 * tx information functions
 480 *
 481 *********************************************************/
 482static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
 483					  struct rtl_tcb_desc *tcb_desc,
 484					  struct ieee80211_tx_info *info)
 485{
 486	struct rtl_priv *rtlpriv = rtl_priv(hw);
 487	u8 rate_flag = info->control.rates[0].flags;
 488
 489	tcb_desc->use_shortpreamble = false;
 490
 491	/* 1M can only use Long Preamble. 11B spec */
 492	if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
 493		return;
 494	else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
 495		tcb_desc->use_shortpreamble = true;
 496
 497	return;
 498}
 499
 500static void _rtl_query_shortgi(struct ieee80211_hw *hw,
 501			       struct ieee80211_sta *sta,
 502			       struct rtl_tcb_desc *tcb_desc,
 503			       struct ieee80211_tx_info *info)
 504{
 505	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
 506	u8 rate_flag = info->control.rates[0].flags;
 507	u8 sgi_40 = 0, sgi_20 = 0, bw_40 = 0;
 508	tcb_desc->use_shortgi = false;
 509
 510	if (sta == NULL)
 511		return;
 512
 513	sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
 514	sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
 515
 516	if (!(sta->ht_cap.ht_supported))
 517		return;
 518
 519	if (!sgi_40 && !sgi_20)
 520		return;
 521
 522	if (mac->opmode == NL80211_IFTYPE_STATION)
 523		bw_40 = mac->bw_40;
 524	else if (mac->opmode == NL80211_IFTYPE_AP ||
 525		mac->opmode == NL80211_IFTYPE_ADHOC)
 526		bw_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
 527
 528	if (bw_40 && sgi_40)
 529		tcb_desc->use_shortgi = true;
 530	else if ((bw_40 == false) && sgi_20)
 531		tcb_desc->use_shortgi = true;
 532
 533	if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
 534		tcb_desc->use_shortgi = false;
 535}
 536
 537static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
 538				       struct rtl_tcb_desc *tcb_desc,
 539				       struct ieee80211_tx_info *info)
 540{
 541	struct rtl_priv *rtlpriv = rtl_priv(hw);
 542	u8 rate_flag = info->control.rates[0].flags;
 543
 544	/* Common Settings */
 545	tcb_desc->rts_stbc = false;
 546	tcb_desc->cts_enable = false;
 547	tcb_desc->rts_sc = 0;
 548	tcb_desc->rts_bw = false;
 549	tcb_desc->rts_use_shortpreamble = false;
 550	tcb_desc->rts_use_shortgi = false;
 551
 552	if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
 553		/* Use CTS-to-SELF in protection mode. */
 554		tcb_desc->rts_enable = true;
 555		tcb_desc->cts_enable = true;
 556		tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
 557	} else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
 558		/* Use RTS-CTS in protection mode. */
 559		tcb_desc->rts_enable = true;
 560		tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
 561	}
 562}
 563
 564static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
 565				   struct ieee80211_sta *sta,
 566				   struct rtl_tcb_desc *tcb_desc)
 567{
 568	struct rtl_priv *rtlpriv = rtl_priv(hw);
 569	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
 570	struct rtl_sta_info *sta_entry = NULL;
 571	u8 ratr_index = 7;
 572
 573	if (sta) {
 574		sta_entry = (struct rtl_sta_info *) sta->drv_priv;
 575		ratr_index = sta_entry->ratr_index;
 576	}
 577	if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
 578		if (mac->opmode == NL80211_IFTYPE_STATION) {
 579			tcb_desc->ratr_index = 0;
 580		} else if (mac->opmode == NL80211_IFTYPE_ADHOC) {
 581			if (tcb_desc->multicast || tcb_desc->broadcast) {
 582				tcb_desc->hw_rate =
 583				    rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
 584				tcb_desc->use_driver_rate = 1;
 585			} else {
 586				/* TODO */
 587			}
 588			tcb_desc->ratr_index = ratr_index;
 589		} else if (mac->opmode == NL80211_IFTYPE_AP) {
 590			tcb_desc->ratr_index = ratr_index;
 591		}
 592	}
 593
 594	if (rtlpriv->dm.useramask) {
 595		/* TODO we will differentiate adhoc and station futrue  */
 596		if (mac->opmode == NL80211_IFTYPE_STATION) {
 597			tcb_desc->mac_id = 0;
 598
 599			if (mac->mode == WIRELESS_MODE_N_24G)
 600				tcb_desc->ratr_index = RATR_INX_WIRELESS_NGB;
 601			else if (mac->mode == WIRELESS_MODE_N_5G)
 602				tcb_desc->ratr_index = RATR_INX_WIRELESS_NG;
 603			else if (mac->mode & WIRELESS_MODE_G)
 604				tcb_desc->ratr_index = RATR_INX_WIRELESS_GB;
 605			else if (mac->mode & WIRELESS_MODE_B)
 606				tcb_desc->ratr_index = RATR_INX_WIRELESS_B;
 607			else if (mac->mode & WIRELESS_MODE_A)
 608				tcb_desc->ratr_index = RATR_INX_WIRELESS_G;
 609		} else if (mac->opmode == NL80211_IFTYPE_AP ||
 610			mac->opmode == NL80211_IFTYPE_ADHOC) {
 611			if (NULL != sta) {
 612				if (sta->aid > 0)
 613					tcb_desc->mac_id = sta->aid + 1;
 614				else
 615					tcb_desc->mac_id = 1;
 616			} else {
 617				tcb_desc->mac_id = 0;
 618			}
 619		}
 620	}
 621
 622}
 623
 624static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
 625				      struct ieee80211_sta *sta,
 626				      struct rtl_tcb_desc *tcb_desc)
 627{
 628	struct rtl_priv *rtlpriv = rtl_priv(hw);
 629	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
 630
 631	tcb_desc->packet_bw = false;
 632	if (!sta)
 633		return;
 634	if (mac->opmode == NL80211_IFTYPE_AP ||
 635	    mac->opmode == NL80211_IFTYPE_ADHOC) {
 636		if (!(sta->ht_cap.ht_supported) ||
 637		    !(sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
 638			return;
 639	} else if (mac->opmode == NL80211_IFTYPE_STATION) {
 640		if (!mac->bw_40 || !(sta->ht_cap.ht_supported))
 641			return;
 642	}
 643	if (tcb_desc->multicast || tcb_desc->broadcast)
 644		return;
 645
 646	/*use legency rate, shall use 20MHz */
 647	if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
 648		return;
 649
 650	tcb_desc->packet_bw = true;
 651}
 652
 653static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw)
 654{
 655	struct rtl_priv *rtlpriv = rtl_priv(hw);
 656	struct rtl_phy *rtlphy = &(rtlpriv->phy);
 657	u8 hw_rate;
 658
 659	if (get_rf_type(rtlphy) == RF_2T2R)
 660		hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
 661	else
 662		hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
 663
 664	return hw_rate;
 665}
 666
 667void rtl_get_tcb_desc(struct ieee80211_hw *hw,
 668		      struct ieee80211_tx_info *info,
 669		      struct ieee80211_sta *sta,
 670		      struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
 671{
 672	struct rtl_priv *rtlpriv = rtl_priv(hw);
 673	struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
 674	struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
 675	struct ieee80211_rate *txrate;
 676	__le16 fc = hdr->frame_control;
 677
 678	txrate = ieee80211_get_tx_rate(hw, info);
 679	tcb_desc->hw_rate = txrate->hw_value;
 680
 681	if (ieee80211_is_data(fc)) {
 682		/*
 683		 *we set data rate INX 0
 684		 *in rtl_rc.c   if skb is special data or
 685		 *mgt which need low data rate.
 686		 */
 687
 688		/*
 689		 *So tcb_desc->hw_rate is just used for
 690		 *special data and mgt frames
 691		 */
 692		if (info->control.rates[0].idx == 0 &&
 693				ieee80211_is_nullfunc(fc)) {
 694			tcb_desc->use_driver_rate = true;
 695			tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
 696
 697			tcb_desc->disable_ratefallback = 1;
 698		} else {
 699			/*
 700			 *because hw will nerver use hw_rate
 701			 *when tcb_desc->use_driver_rate = false
 702			 *so we never set highest N rate here,
 703			 *and N rate will all be controlled by FW
 704			 *when tcb_desc->use_driver_rate = false
 705			 */
 706			if (sta && (sta->ht_cap.ht_supported)) {
 707				tcb_desc->hw_rate = _rtl_get_highest_n_rate(hw);
 708			} else {
 709				if (rtlmac->mode == WIRELESS_MODE_B) {
 710					tcb_desc->hw_rate =
 711					   rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
 712				} else {
 713					tcb_desc->hw_rate =
 714					   rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
 715				}
 716			}
 717		}
 718
 719		if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
 720			tcb_desc->multicast = 1;
 721		else if (is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
 722			tcb_desc->broadcast = 1;
 723
 724		_rtl_txrate_selectmode(hw, sta, tcb_desc);
 725		_rtl_query_bandwidth_mode(hw, sta, tcb_desc);
 726		_rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
 727		_rtl_query_shortgi(hw, sta, tcb_desc, info);
 728		_rtl_query_protection_mode(hw, tcb_desc, info);
 729	} else {
 730		tcb_desc->use_driver_rate = true;
 731		tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
 732		tcb_desc->disable_ratefallback = 1;
 733		tcb_desc->mac_id = 0;
 734		tcb_desc->packet_bw = false;
 735	}
 736}
 737EXPORT_SYMBOL(rtl_get_tcb_desc);
 738
 739bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
 740{
 741	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
 742	struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
 743	struct rtl_priv *rtlpriv = rtl_priv(hw);
 744	__le16 fc = hdr->frame_control;
 745	u8 *act = (u8 *) (((u8 *) skb->data + MAC80211_3ADDR_LEN));
 746	u8 category;
 747
 748	if (!ieee80211_is_action(fc))
 749		return true;
 750
 751	category = *act;
 752	act++;
 753	switch (category) {
 754	case ACT_CAT_BA:
 755		switch (*act) {
 756		case ACT_ADDBAREQ:
 757			if (mac->act_scanning)
 758				return false;
 759
 760			RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
 761				 ("%s ACT_ADDBAREQ From :%pM\n",
 762				  is_tx ? "Tx" : "Rx", hdr->addr2));
 763			break;
 764		case ACT_ADDBARSP:
 765			RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
 766				 ("%s ACT_ADDBARSP From :%pM\n",
 767				  is_tx ? "Tx" : "Rx", hdr->addr2));
 768			break;
 769		case ACT_DELBA:
 770			RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
 771				 ("ACT_ADDBADEL From :%pM\n", hdr->addr2));
 772			break;
 773		}
 774		break;
 775	default:
 776		break;
 777	}
 778
 779	return true;
 780}
 781
 782/*should call before software enc*/
 783u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
 784{
 785	struct rtl_priv *rtlpriv = rtl_priv(hw);
 786	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
 787	__le16 fc = rtl_get_fc(skb);
 788	u16 ether_type;
 789	u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
 790	const struct iphdr *ip;
 791
 792	if (!ieee80211_is_data(fc))
 793		return false;
 794
 795
 796	ip = (struct iphdr *)((u8 *) skb->data + mac_hdr_len +
 797			      SNAP_SIZE + PROTOC_TYPE_SIZE);
 798	ether_type = *(u16 *) ((u8 *) skb->data + mac_hdr_len + SNAP_SIZE);
 799	/*	ether_type = ntohs(ether_type); */
 800
 801	if (ETH_P_IP == ether_type) {
 802		if (IPPROTO_UDP == ip->protocol) {
 803			struct udphdr *udp = (struct udphdr *)((u8 *) ip +
 804							       (ip->ihl << 2));
 805			if (((((u8 *) udp)[1] == 68) &&
 806			     (((u8 *) udp)[3] == 67)) ||
 807			    ((((u8 *) udp)[1] == 67) &&
 808			     (((u8 *) udp)[3] == 68))) {
 809				/*
 810				 * 68 : UDP BOOTP client
 811				 * 67 : UDP BOOTP server
 812				 */
 813				RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV),
 814					 DBG_DMESG, ("dhcp %s !!\n",
 815						     (is_tx) ? "Tx" : "Rx"));
 816
 817				if (is_tx) {
 818					rtl_lps_leave(hw);
 819					ppsc->last_delaylps_stamp_jiffies =
 820					    jiffies;
 821				}
 822
 823				return true;
 824			}
 825		}
 826	} else if (ETH_P_ARP == ether_type) {
 827		if (is_tx) {
 828			rtl_lps_leave(hw);
 829			ppsc->last_delaylps_stamp_jiffies = jiffies;
 830		}
 831
 832		return true;
 833	} else if (ETH_P_PAE == ether_type) {
 834		RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
 835			 ("802.1X %s EAPOL pkt!!\n", (is_tx) ? "Tx" : "Rx"));
 836
 837		if (is_tx) {
 838			rtl_lps_leave(hw);
 839			ppsc->last_delaylps_stamp_jiffies = jiffies;
 840		}
 841
 842		return true;
 843	} else if (ETH_P_IPV6 == ether_type) {
 844		/* IPv6 */
 845		return true;
 846	}
 847
 848	return false;
 849}
 850
 851/*********************************************************
 852 *
 853 * functions called by core.c
 854 *
 855 *********************************************************/
 856int rtl_tx_agg_start(struct ieee80211_hw *hw,
 857		struct ieee80211_sta *sta, u16 tid, u16 *ssn)
 858{
 859	struct rtl_priv *rtlpriv = rtl_priv(hw);
 860	struct rtl_tid_data *tid_data;
 861	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
 862	struct rtl_sta_info *sta_entry = NULL;
 863
 864	if (sta == NULL)
 865		return -EINVAL;
 866
 867	if (unlikely(tid >= MAX_TID_COUNT))
 868		return -EINVAL;
 869
 870	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
 871	if (!sta_entry)
 872		return -ENXIO;
 873	tid_data = &sta_entry->tids[tid];
 874
 875	RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
 876		 ("on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
 877		 tid_data->seq_number));
 878
 879	*ssn = tid_data->seq_number;
 880	tid_data->agg.agg_state = RTL_AGG_START;
 881
 882	ieee80211_start_tx_ba_cb_irqsafe(mac->vif, sta->addr, tid);
 883
 884	return 0;
 885}
 886
 887int rtl_tx_agg_stop(struct ieee80211_hw *hw,
 888		struct ieee80211_sta *sta, u16 tid)
 889{
 890	struct rtl_priv *rtlpriv = rtl_priv(hw);
 891	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
 892	struct rtl_sta_info *sta_entry = NULL;
 893
 894	if (sta == NULL)
 895		return -EINVAL;
 896
 897	if (!sta->addr) {
 898		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, ("ra = NULL\n"));
 899		return -EINVAL;
 900	}
 901
 902	RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
 903		 ("on ra = %pM tid = %d\n", sta->addr, tid));
 904
 905	if (unlikely(tid >= MAX_TID_COUNT))
 906		return -EINVAL;
 907
 908	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
 909	sta_entry->tids[tid].agg.agg_state = RTL_AGG_STOP;
 910
 911	ieee80211_stop_tx_ba_cb_irqsafe(mac->vif, sta->addr, tid);
 912
 913	return 0;
 914}
 915
 916int rtl_tx_agg_oper(struct ieee80211_hw *hw,
 917		struct ieee80211_sta *sta, u16 tid)
 918{
 919	struct rtl_priv *rtlpriv = rtl_priv(hw);
 920	struct rtl_sta_info *sta_entry = NULL;
 921
 922	if (sta == NULL)
 923		return -EINVAL;
 924
 925	if (!sta->addr) {
 926		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, ("ra = NULL\n"));
 927		return -EINVAL;
 928	}
 929
 930	RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
 931		 ("on ra = %pM tid = %d\n", sta->addr, tid));
 932
 933	if (unlikely(tid >= MAX_TID_COUNT))
 934		return -EINVAL;
 935
 936	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
 937	sta_entry->tids[tid].agg.agg_state = RTL_AGG_OPERATIONAL;
 938
 939	return 0;
 940}
 941
 942/*********************************************************
 943 *
 944 * wq & timer callback functions
 945 *
 946 *********************************************************/
 947void rtl_watchdog_wq_callback(void *data)
 948{
 949	struct rtl_works *rtlworks = container_of_dwork_rtl(data,
 950							    struct rtl_works,
 951							    watchdog_wq);
 952	struct ieee80211_hw *hw = rtlworks->hw;
 953	struct rtl_priv *rtlpriv = rtl_priv(hw);
 954	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
 955	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
 956	bool busytraffic = false;
 957	bool higher_busytraffic = false;
 958	bool higher_busyrxtraffic = false;
 959	u8 idx, tid;
 960	u32 rx_cnt_inp4eriod = 0;
 961	u32 tx_cnt_inp4eriod = 0;
 962	u32 aver_rx_cnt_inperiod = 0;
 963	u32 aver_tx_cnt_inperiod = 0;
 964	u32 aver_tidtx_inperiod[MAX_TID_COUNT] = {0};
 965	u32 tidtx_inp4eriod[MAX_TID_COUNT] = {0};
 966	bool enter_ps = false;
 967
 968	if (is_hal_stop(rtlhal))
 969		return;
 970
 971	/* <1> Determine if action frame is allowed */
 972	if (mac->link_state > MAC80211_NOLINK) {
 973		if (mac->cnt_after_linked < 20)
 974			mac->cnt_after_linked++;
 975	} else {
 976		mac->cnt_after_linked = 0;
 977	}
 978
 979	/*
 980	 *<3> to check if traffic busy, if
 981	 * busytraffic we don't change channel
 982	 */
 983	if (mac->link_state >= MAC80211_LINKED) {
 984
 985		/* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
 986		for (idx = 0; idx <= 2; idx++) {
 987			rtlpriv->link_info.num_rx_in4period[idx] =
 988			    rtlpriv->link_info.num_rx_in4period[idx + 1];
 989			rtlpriv->link_info.num_tx_in4period[idx] =
 990			    rtlpriv->link_info.num_tx_in4period[idx + 1];
 991		}
 992		rtlpriv->link_info.num_rx_in4period[3] =
 993		    rtlpriv->link_info.num_rx_inperiod;
 994		rtlpriv->link_info.num_tx_in4period[3] =
 995		    rtlpriv->link_info.num_tx_inperiod;
 996		for (idx = 0; idx <= 3; idx++) {
 997			rx_cnt_inp4eriod +=
 998			    rtlpriv->link_info.num_rx_in4period[idx];
 999			tx_cnt_inp4eriod +=
1000			    rtlpriv->link_info.num_tx_in4period[idx];
1001		}
1002		aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
1003		aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
1004
1005		/* (2) check traffic busy */
1006		if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100)
1007			busytraffic = true;
1008
1009		/* Higher Tx/Rx data. */
1010		if (aver_rx_cnt_inperiod > 4000 ||
1011		    aver_tx_cnt_inperiod > 4000) {
1012			higher_busytraffic = true;
1013
1014			/* Extremely high Rx data. */
1015			if (aver_rx_cnt_inperiod > 5000)
1016				higher_busyrxtraffic = true;
1017		}
1018
1019		/* check every tid's tx traffic */
1020		for (tid = 0; tid <= 7; tid++) {
1021			for (idx = 0; idx <= 2; idx++)
1022				rtlpriv->link_info.tidtx_in4period[tid][idx] =
1023				  rtlpriv->link_info.tidtx_in4period[tid]
1024				  [idx + 1];
1025			rtlpriv->link_info.tidtx_in4period[tid][3] =
1026				rtlpriv->link_info.tidtx_inperiod[tid];
1027
1028			for (idx = 0; idx <= 3; idx++)
1029				tidtx_inp4eriod[tid] +=
1030				  rtlpriv->link_info.tidtx_in4period[tid][idx];
1031			aver_tidtx_inperiod[tid] = tidtx_inp4eriod[tid] / 4;
1032			if (aver_tidtx_inperiod[tid] > 5000)
1033				rtlpriv->link_info.higher_busytxtraffic[tid] =
1034						   true;
1035			else
1036				rtlpriv->link_info.higher_busytxtraffic[tid] =
1037						   false;
1038		}
1039
1040		if (((rtlpriv->link_info.num_rx_inperiod +
1041		      rtlpriv->link_info.num_tx_inperiod) > 8) ||
1042		    (rtlpriv->link_info.num_rx_inperiod > 2))
1043			enter_ps = false;
1044		else
1045			enter_ps = true;
1046
1047		/* LeisurePS only work in infra mode. */
1048		if (enter_ps)
1049			rtl_lps_enter(hw);
1050		else
1051			rtl_lps_leave(hw);
1052	}
1053
1054	rtlpriv->link_info.num_rx_inperiod = 0;
1055	rtlpriv->link_info.num_tx_inperiod = 0;
1056	for (tid = 0; tid <= 7; tid++)
1057		rtlpriv->link_info.tidtx_inperiod[tid] = 0;
1058
1059	rtlpriv->link_info.busytraffic = busytraffic;
1060	rtlpriv->link_info.higher_busytraffic = higher_busytraffic;
1061	rtlpriv->link_info.higher_busyrxtraffic = higher_busyrxtraffic;
1062
1063	/* <3> DM */
1064	rtlpriv->cfg->ops->dm_watchdog(hw);
1065}
1066
1067void rtl_watch_dog_timer_callback(unsigned long data)
1068{
1069	struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1070	struct rtl_priv *rtlpriv = rtl_priv(hw);
1071
1072	queue_delayed_work(rtlpriv->works.rtl_wq,
1073			   &rtlpriv->works.watchdog_wq, 0);
1074
1075	mod_timer(&rtlpriv->works.watchdog_timer,
1076		  jiffies + MSECS(RTL_WATCH_DOG_TIME));
1077}
1078
1079/*********************************************************
1080 *
1081 * frame process functions
1082 *
1083 *********************************************************/
1084u8 *rtl_find_ie(u8 *data, unsigned int len, u8 ie)
1085{
1086	struct ieee80211_mgmt *mgmt = (void *)data;
1087	u8 *pos, *end;
1088
1089	pos = (u8 *)mgmt->u.beacon.variable;
1090	end = data + len;
1091	while (pos < end) {
1092		if (pos + 2 + pos[1] > end)
1093			return NULL;
1094
1095		if (pos[0] == ie)
1096			return pos;
1097
1098		pos += 2 + pos[1];
1099	}
1100	return NULL;
1101}
1102
1103/* when we use 2 rx ants we send IEEE80211_SMPS_OFF */
1104/* when we use 1 rx ant we send IEEE80211_SMPS_STATIC */
1105static struct sk_buff *rtl_make_smps_action(struct ieee80211_hw *hw,
1106		enum ieee80211_smps_mode smps, u8 *da, u8 *bssid)
1107{
1108	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1109	struct sk_buff *skb;
1110	struct ieee80211_mgmt *action_frame;
1111
1112	/* 27 = header + category + action + smps mode */
1113	skb = dev_alloc_skb(27 + hw->extra_tx_headroom);
1114	if (!skb)
1115		return NULL;
1116
1117	skb_reserve(skb, hw->extra_tx_headroom);
1118	action_frame = (void *)skb_put(skb, 27);
1119	memset(action_frame, 0, 27);
1120	memcpy(action_frame->da, da, ETH_ALEN);
1121	memcpy(action_frame->sa, rtlefuse->dev_addr, ETH_ALEN);
1122	memcpy(action_frame->bssid, bssid, ETH_ALEN);
1123	action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1124						  IEEE80211_STYPE_ACTION);
1125	action_frame->u.action.category = WLAN_CATEGORY_HT;
1126	action_frame->u.action.u.ht_smps.action = WLAN_HT_ACTION_SMPS;
1127	switch (smps) {
1128	case IEEE80211_SMPS_AUTOMATIC:/* 0 */
1129	case IEEE80211_SMPS_NUM_MODES:/* 4 */
1130		WARN_ON(1);
1131	case IEEE80211_SMPS_OFF:/* 1 */ /*MIMO_PS_NOLIMIT*/
1132		action_frame->u.action.u.ht_smps.smps_control =
1133				WLAN_HT_SMPS_CONTROL_DISABLED;/* 0 */
1134		break;
1135	case IEEE80211_SMPS_STATIC:/* 2 */ /*MIMO_PS_STATIC*/
1136		action_frame->u.action.u.ht_smps.smps_control =
1137				WLAN_HT_SMPS_CONTROL_STATIC;/* 1 */
1138		break;
1139	case IEEE80211_SMPS_DYNAMIC:/* 3 */ /*MIMO_PS_DYNAMIC*/
1140		action_frame->u.action.u.ht_smps.smps_control =
1141				WLAN_HT_SMPS_CONTROL_DYNAMIC;/* 3 */
1142		break;
1143	}
1144
1145	return skb;
1146}
1147
1148int rtl_send_smps_action(struct ieee80211_hw *hw,
1149		struct ieee80211_sta *sta, u8 *da, u8 *bssid,
1150		enum ieee80211_smps_mode smps)
1151{
1152	struct rtl_priv *rtlpriv = rtl_priv(hw);
1153	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1154	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1155	struct sk_buff *skb = rtl_make_smps_action(hw, smps, da, bssid);
1156	struct rtl_tcb_desc tcb_desc;
1157	memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
1158
1159	if (rtlpriv->mac80211.act_scanning)
1160		goto err_free;
1161
1162	if (!sta)
1163		goto err_free;
1164
1165	if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
1166		goto err_free;
1167
1168	if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
1169		goto err_free;
1170
1171	/* this is a type = mgmt * stype = action frame */
1172	if (skb) {
1173		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1174		struct rtl_sta_info *sta_entry =
1175			(struct rtl_sta_info *) sta->drv_priv;
1176		sta_entry->mimo_ps = smps;
1177		rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0);
1178
1179		info->control.rates[0].idx = 0;
1180		info->control.sta = sta;
1181		info->band = hw->conf.channel->band;
1182		rtlpriv->intf_ops->adapter_tx(hw, skb, &tcb_desc);
1183	}
1184err_free:
1185	return 0;
1186}
1187
1188/*********************************************************
1189 *
1190 * IOT functions
1191 *
1192 *********************************************************/
1193static bool rtl_chk_vendor_ouisub(struct ieee80211_hw *hw,
1194		struct octet_string vendor_ie)
1195{
1196	struct rtl_priv *rtlpriv = rtl_priv(hw);
1197	bool matched = false;
1198	static u8 athcap_1[] = { 0x00, 0x03, 0x7F };
1199	static u8 athcap_2[] = { 0x00, 0x13, 0x74 };
1200	static u8 broadcap_1[] = { 0x00, 0x10, 0x18 };
1201	static u8 broadcap_2[] = { 0x00, 0x0a, 0xf7 };
1202	static u8 broadcap_3[] = { 0x00, 0x05, 0xb5 };
1203	static u8 racap[] = { 0x00, 0x0c, 0x43 };
1204	static u8 ciscocap[] = { 0x00, 0x40, 0x96 };
1205	static u8 marvcap[] = { 0x00, 0x50, 0x43 };
1206
1207	if (memcmp(vendor_ie.octet, athcap_1, 3) == 0 ||
1208		memcmp(vendor_ie.octet, athcap_2, 3) == 0) {
1209		rtlpriv->mac80211.vendor = PEER_ATH;
1210		matched = true;
1211	} else if (memcmp(vendor_ie.octet, broadcap_1, 3) == 0 ||
1212		memcmp(vendor_ie.octet, broadcap_2, 3) == 0 ||
1213		memcmp(vendor_ie.octet, broadcap_3, 3) == 0) {
1214		rtlpriv->mac80211.vendor = PEER_BROAD;
1215		matched = true;
1216	} else if (memcmp(vendor_ie.octet, racap, 3) == 0) {
1217		rtlpriv->mac80211.vendor = PEER_RAL;
1218		matched = true;
1219	} else if (memcmp(vendor_ie.octet, ciscocap, 3) == 0) {
1220		rtlpriv->mac80211.vendor = PEER_CISCO;
1221		matched = true;
1222	} else if (memcmp(vendor_ie.octet, marvcap, 3) == 0) {
1223		rtlpriv->mac80211.vendor = PEER_MARV;
1224		matched = true;
1225	}
1226
1227	return matched;
1228}
1229
1230static bool rtl_find_221_ie(struct ieee80211_hw *hw, u8 *data,
1231		unsigned int len)
1232{
1233	struct ieee80211_mgmt *mgmt = (void *)data;
1234	struct octet_string vendor_ie;
1235	u8 *pos, *end;
1236
1237	pos = (u8 *)mgmt->u.beacon.variable;
1238	end = data + len;
1239	while (pos < end) {
1240		if (pos[0] == 221) {
1241			vendor_ie.length = pos[1];
1242			vendor_ie.octet = &pos[2];
1243			if (rtl_chk_vendor_ouisub(hw, vendor_ie))
1244				return true;
1245		}
1246
1247		if (pos + 2 + pos[1] > end)
1248			return false;
1249
1250		pos += 2 + pos[1];
1251	}
1252	return false;
1253}
1254
1255void rtl_recognize_peer(struct ieee80211_hw *hw, u8 *data, unsigned int len)
1256{
1257	struct rtl_priv *rtlpriv = rtl_priv(hw);
1258	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1259	struct ieee80211_hdr *hdr = (void *)data;
1260	u32 vendor = PEER_UNKNOWN;
1261
1262	static u8 ap3_1[3] = { 0x00, 0x14, 0xbf };
1263	static u8 ap3_2[3] = { 0x00, 0x1a, 0x70 };
1264	static u8 ap3_3[3] = { 0x00, 0x1d, 0x7e };
1265	static u8 ap4_1[3] = { 0x00, 0x90, 0xcc };
1266	static u8 ap4_2[3] = { 0x00, 0x0e, 0x2e };
1267	static u8 ap4_3[3] = { 0x00, 0x18, 0x02 };
1268	static u8 ap4_4[3] = { 0x00, 0x17, 0x3f };
1269	static u8 ap4_5[3] = { 0x00, 0x1c, 0xdf };
1270	static u8 ap5_1[3] = { 0x00, 0x1c, 0xf0 };
1271	static u8 ap5_2[3] = { 0x00, 0x21, 0x91 };
1272	static u8 ap5_3[3] = { 0x00, 0x24, 0x01 };
1273	static u8 ap5_4[3] = { 0x00, 0x15, 0xe9 };
1274	static u8 ap5_5[3] = { 0x00, 0x17, 0x9A };
1275	static u8 ap5_6[3] = { 0x00, 0x18, 0xE7 };
1276	static u8 ap6_1[3] = { 0x00, 0x17, 0x94 };
1277	static u8 ap7_1[3] = { 0x00, 0x14, 0xa4 };
1278
1279	if (mac->opmode != NL80211_IFTYPE_STATION)
1280		return;
1281
1282	if (mac->link_state == MAC80211_NOLINK) {
1283		mac->vendor = PEER_UNKNOWN;
1284		return;
1285	}
1286
1287	if (mac->cnt_after_linked > 2)
1288		return;
1289
1290	/* check if this really is a beacon */
1291	if (!ieee80211_is_beacon(hdr->frame_control))
1292		return;
1293
1294	/* min. beacon length + FCS_LEN */
1295	if (len <= 40 + FCS_LEN)
1296		return;
1297
1298	/* and only beacons from the associated BSSID, please */
1299	if (compare_ether_addr(hdr->addr3, rtlpriv->mac80211.bssid))
1300		return;
1301
1302	if (rtl_find_221_ie(hw, data, len))
1303		vendor = mac->vendor;
1304
1305	if ((memcmp(mac->bssid, ap5_1, 3) == 0) ||
1306		(memcmp(mac->bssid, ap5_2, 3) == 0) ||
1307		(memcmp(mac->bssid, ap5_3, 3) == 0) ||
1308		(memcmp(mac->bssid, ap5_4, 3) == 0) ||
1309		(memcmp(mac->bssid, ap5_5, 3) == 0) ||
1310		(memcmp(mac->bssid, ap5_6, 3) == 0) ||
1311		vendor == PEER_ATH) {
1312		vendor = PEER_ATH;
1313		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, ("=>ath find\n"));
1314	} else if ((memcmp(mac->bssid, ap4_4, 3) == 0) ||
1315		(memcmp(mac->bssid, ap4_5, 3) == 0) ||
1316		(memcmp(mac->bssid, ap4_1, 3) == 0) ||
1317		(memcmp(mac->bssid, ap4_2, 3) == 0) ||
1318		(memcmp(mac->bssid, ap4_3, 3) == 0) ||
1319		vendor == PEER_RAL) {
1320		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, ("=>ral findn\n"));
1321		vendor = PEER_RAL;
1322	} else if (memcmp(mac->bssid, ap6_1, 3) == 0 ||
1323		vendor == PEER_CISCO) {
1324		vendor = PEER_CISCO;
1325		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, ("=>cisco find\n"));
1326	} else if ((memcmp(mac->bssid, ap3_1, 3) == 0) ||
1327		(memcmp(mac->bssid, ap3_2, 3) == 0) ||
1328		(memcmp(mac->bssid, ap3_3, 3) == 0) ||
1329		vendor == PEER_BROAD) {
1330		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, ("=>broad find\n"));
1331		vendor = PEER_BROAD;
1332	} else if (memcmp(mac->bssid, ap7_1, 3) == 0 ||
1333		vendor == PEER_MARV) {
1334		vendor = PEER_MARV;
1335		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, ("=>marv find\n"));
1336	}
1337
1338	mac->vendor = vendor;
1339}
1340
1341/*********************************************************
1342 *
1343 * sysfs functions
1344 *
1345 *********************************************************/
1346static ssize_t rtl_show_debug_level(struct device *d,
1347				    struct device_attribute *attr, char *buf)
1348{
1349	struct ieee80211_hw *hw = dev_get_drvdata(d);
1350	struct rtl_priv *rtlpriv = rtl_priv(hw);
1351
1352	return sprintf(buf, "0x%08X\n", rtlpriv->dbg.global_debuglevel);
1353}
1354
1355static ssize_t rtl_store_debug_level(struct device *d,
1356				     struct device_attribute *attr,
1357				     const char *buf, size_t count)
1358{
1359	struct ieee80211_hw *hw = dev_get_drvdata(d);
1360	struct rtl_priv *rtlpriv = rtl_priv(hw);
1361	unsigned long val;
1362	int ret;
1363
1364	ret = strict_strtoul(buf, 0, &val);
1365	if (ret) {
1366		printk(KERN_DEBUG "%s is not in hex or decimal form.\n", buf);
1367	} else {
1368		rtlpriv->dbg.global_debuglevel = val;
1369		printk(KERN_DEBUG "debuglevel:%x\n",
1370		       rtlpriv->dbg.global_debuglevel);
1371	}
1372
1373	return strnlen(buf, count);
1374}
1375
1376static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
1377		   rtl_show_debug_level, rtl_store_debug_level);
1378
1379static struct attribute *rtl_sysfs_entries[] = {
1380
1381	&dev_attr_debug_level.attr,
1382
1383	NULL
1384};
1385
1386/*
1387 * "name" is folder name witch will be
1388 * put in device directory like :
1389 * sys/devices/pci0000:00/0000:00:1c.4/
1390 * 0000:06:00.0/rtl_sysfs
1391 */
1392struct attribute_group rtl_attribute_group = {
1393	.name = "rtlsysfs",
1394	.attrs = rtl_sysfs_entries,
1395};
1396
1397MODULE_AUTHOR("lizhaoming	<chaoming_li@realsil.com.cn>");
1398MODULE_AUTHOR("Realtek WlanFAE	<wlanfae@realtek.com>");
1399MODULE_AUTHOR("Larry Finger	<Larry.FInger@lwfinger.net>");
1400MODULE_LICENSE("GPL");
1401MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
1402
1403static int __init rtl_core_module_init(void)
1404{
1405	if (rtl_rate_control_register())
1406		pr_err("Unable to register rtl_rc, use default RC !!\n");
1407
1408	return 0;
1409}
1410
1411static void __exit rtl_core_module_exit(void)
1412{
1413	/*RC*/
1414	rtl_rate_control_unregister();
1415}
1416
1417module_init(rtl_core_module_init);
1418module_exit(rtl_core_module_exit);