linux/drivers/net/wireless/rtlwifi/base.c
<<
>>
Prefs
   1/******************************************************************************
   2 *
   3 * Copyright(c) 2009-2012  Realtek Corporation.
   4 *
   5 * This program is free software; you can redistribute it and/or modify it
   6 * under the terms of version 2 of the GNU General Public License as
   7 * published by the Free Software Foundation.
   8 *
   9 * This program is distributed in the hope that it will be useful, but WITHOUT
  10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  12 * more details.
  13 *
  14 * 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#include "wifi.h"
  31#include "rc.h"
  32#include "base.h"
  33#include "efuse.h"
  34#include "cam.h"
  35#include "ps.h"
  36#include "regd.h"
  37
  38#include <linux/ip.h>
  39#include <linux/module.h>
  40#include <linux/udp.h>
  41
  42/*
  43 *NOTICE!!!: This file will be very big, we should
  44 *keep it clear under following roles:
  45 *
  46 *This file include following parts, so, if you add new
  47 *functions into this file, please check which part it
  48 *should includes. or check if you should add new part
  49 *for this file:
  50 *
  51 *1) mac80211 init functions
  52 *2) tx information functions
  53 *3) functions called by core.c
  54 *4) wq & timer callback functions
  55 *5) frame process functions
  56 *6) IOT functions
  57 *7) sysfs functions
  58 *8) vif functions
  59 *9) ...
  60 */
  61
  62/*********************************************************
  63 *
  64 * mac80211 init functions
  65 *
  66 *********************************************************/
  67static struct ieee80211_channel rtl_channeltable_2g[] = {
  68        {.center_freq = 2412, .hw_value = 1,},
  69        {.center_freq = 2417, .hw_value = 2,},
  70        {.center_freq = 2422, .hw_value = 3,},
  71        {.center_freq = 2427, .hw_value = 4,},
  72        {.center_freq = 2432, .hw_value = 5,},
  73        {.center_freq = 2437, .hw_value = 6,},
  74        {.center_freq = 2442, .hw_value = 7,},
  75        {.center_freq = 2447, .hw_value = 8,},
  76        {.center_freq = 2452, .hw_value = 9,},
  77        {.center_freq = 2457, .hw_value = 10,},
  78        {.center_freq = 2462, .hw_value = 11,},
  79        {.center_freq = 2467, .hw_value = 12,},
  80        {.center_freq = 2472, .hw_value = 13,},
  81        {.center_freq = 2484, .hw_value = 14,},
  82};
  83
  84static struct ieee80211_channel rtl_channeltable_5g[] = {
  85        {.center_freq = 5180, .hw_value = 36,},
  86        {.center_freq = 5200, .hw_value = 40,},
  87        {.center_freq = 5220, .hw_value = 44,},
  88        {.center_freq = 5240, .hw_value = 48,},
  89        {.center_freq = 5260, .hw_value = 52,},
  90        {.center_freq = 5280, .hw_value = 56,},
  91        {.center_freq = 5300, .hw_value = 60,},
  92        {.center_freq = 5320, .hw_value = 64,},
  93        {.center_freq = 5500, .hw_value = 100,},
  94        {.center_freq = 5520, .hw_value = 104,},
  95        {.center_freq = 5540, .hw_value = 108,},
  96        {.center_freq = 5560, .hw_value = 112,},
  97        {.center_freq = 5580, .hw_value = 116,},
  98        {.center_freq = 5600, .hw_value = 120,},
  99        {.center_freq = 5620, .hw_value = 124,},
 100        {.center_freq = 5640, .hw_value = 128,},
 101        {.center_freq = 5660, .hw_value = 132,},
 102        {.center_freq = 5680, .hw_value = 136,},
 103        {.center_freq = 5700, .hw_value = 140,},
 104        {.center_freq = 5745, .hw_value = 149,},
 105        {.center_freq = 5765, .hw_value = 153,},
 106        {.center_freq = 5785, .hw_value = 157,},
 107        {.center_freq = 5805, .hw_value = 161,},
 108        {.center_freq = 5825, .hw_value = 165,},
 109};
 110
 111static struct ieee80211_rate rtl_ratetable_2g[] = {
 112        {.bitrate = 10, .hw_value = 0x00,},
 113        {.bitrate = 20, .hw_value = 0x01,},
 114        {.bitrate = 55, .hw_value = 0x02,},
 115        {.bitrate = 110, .hw_value = 0x03,},
 116        {.bitrate = 60, .hw_value = 0x04,},
 117        {.bitrate = 90, .hw_value = 0x05,},
 118        {.bitrate = 120, .hw_value = 0x06,},
 119        {.bitrate = 180, .hw_value = 0x07,},
 120        {.bitrate = 240, .hw_value = 0x08,},
 121        {.bitrate = 360, .hw_value = 0x09,},
 122        {.bitrate = 480, .hw_value = 0x0a,},
 123        {.bitrate = 540, .hw_value = 0x0b,},
 124};
 125
 126static struct ieee80211_rate rtl_ratetable_5g[] = {
 127        {.bitrate = 60, .hw_value = 0x04,},
 128        {.bitrate = 90, .hw_value = 0x05,},
 129        {.bitrate = 120, .hw_value = 0x06,},
 130        {.bitrate = 180, .hw_value = 0x07,},
 131        {.bitrate = 240, .hw_value = 0x08,},
 132        {.bitrate = 360, .hw_value = 0x09,},
 133        {.bitrate = 480, .hw_value = 0x0a,},
 134        {.bitrate = 540, .hw_value = 0x0b,},
 135};
 136
 137static const struct ieee80211_supported_band rtl_band_2ghz = {
 138        .band = IEEE80211_BAND_2GHZ,
 139
 140        .channels = rtl_channeltable_2g,
 141        .n_channels = ARRAY_SIZE(rtl_channeltable_2g),
 142
 143        .bitrates = rtl_ratetable_2g,
 144        .n_bitrates = ARRAY_SIZE(rtl_ratetable_2g),
 145
 146        .ht_cap = {0},
 147};
 148
 149static struct ieee80211_supported_band rtl_band_5ghz = {
 150        .band = IEEE80211_BAND_5GHZ,
 151
 152        .channels = rtl_channeltable_5g,
 153        .n_channels = ARRAY_SIZE(rtl_channeltable_5g),
 154
 155        .bitrates = rtl_ratetable_5g,
 156        .n_bitrates = ARRAY_SIZE(rtl_ratetable_5g),
 157
 158        .ht_cap = {0},
 159};
 160
 161static const u8 tid_to_ac[] = {
 162        2, /* IEEE80211_AC_BE */
 163        3, /* IEEE80211_AC_BK */
 164        3, /* IEEE80211_AC_BK */
 165        2, /* IEEE80211_AC_BE */
 166        1, /* IEEE80211_AC_VI */
 167        1, /* IEEE80211_AC_VI */
 168        0, /* IEEE80211_AC_VO */
 169        0, /* IEEE80211_AC_VO */
 170};
 171
 172u8 rtl_tid_to_ac(u8 tid)
 173{
 174        return tid_to_ac[tid];
 175}
 176EXPORT_SYMBOL_GPL(rtl_tid_to_ac);
 177
 178static void _rtl_init_hw_ht_capab(struct ieee80211_hw *hw,
 179                                  struct ieee80211_sta_ht_cap *ht_cap)
 180{
 181        struct rtl_priv *rtlpriv = rtl_priv(hw);
 182        struct rtl_phy *rtlphy = &(rtlpriv->phy);
 183
 184        ht_cap->ht_supported = true;
 185        ht_cap->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
 186            IEEE80211_HT_CAP_SGI_40 |
 187            IEEE80211_HT_CAP_SGI_20 |
 188            IEEE80211_HT_CAP_DSSSCCK40 | IEEE80211_HT_CAP_MAX_AMSDU;
 189
 190        if (rtlpriv->rtlhal.disable_amsdu_8k)
 191                ht_cap->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
 192
 193        /*
 194         *Maximum length of AMPDU that the STA can receive.
 195         *Length = 2 ^ (13 + max_ampdu_length_exp) - 1 (octets)
 196         */
 197        ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
 198
 199        /*Minimum MPDU start spacing , */
 200        ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
 201
 202        ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
 203
 204        /*hw->wiphy->bands[IEEE80211_BAND_2GHZ]
 205         *base on ant_num
 206         *rx_mask: RX mask
 207         *if rx_ant = 1 rx_mask[0]= 0xff;==>MCS0-MCS7
 208         *if rx_ant = 2 rx_mask[1]= 0xff;==>MCS8-MCS15
 209         *if rx_ant >= 3 rx_mask[2]= 0xff;
 210         *if BW_40 rx_mask[4]= 0x01;
 211         *highest supported RX rate
 212         */
 213        if (rtlpriv->dm.supp_phymode_switch) {
 214
 215                RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG,
 216                         "Support phy mode switch\n");
 217
 218                ht_cap->mcs.rx_mask[0] = 0xFF;
 219                ht_cap->mcs.rx_mask[1] = 0xFF;
 220                ht_cap->mcs.rx_mask[4] = 0x01;
 221
 222                ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
 223        } else {
 224                if (get_rf_type(rtlphy) == RF_1T2R ||
 225                    get_rf_type(rtlphy) == RF_2T2R) {
 226                        RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
 227                                 "1T2R or 2T2R\n");
 228                        ht_cap->mcs.rx_mask[0] = 0xFF;
 229                        ht_cap->mcs.rx_mask[1] = 0xFF;
 230                        ht_cap->mcs.rx_mask[4] = 0x01;
 231
 232                        ht_cap->mcs.rx_highest =
 233                                 cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
 234                } else if (get_rf_type(rtlphy) == RF_1T1R) {
 235                        RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "1T1R\n");
 236
 237                        ht_cap->mcs.rx_mask[0] = 0xFF;
 238                        ht_cap->mcs.rx_mask[1] = 0x00;
 239                        ht_cap->mcs.rx_mask[4] = 0x01;
 240
 241                        ht_cap->mcs.rx_highest =
 242                                 cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7);
 243                }
 244        }
 245}
 246
 247static void _rtl_init_mac80211(struct ieee80211_hw *hw)
 248{
 249        struct rtl_priv *rtlpriv = rtl_priv(hw);
 250        struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
 251        struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
 252        struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
 253        struct ieee80211_supported_band *sband;
 254
 255
 256        if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY && rtlhal->bandset ==
 257            BAND_ON_BOTH) {
 258                /* 1: 2.4 G bands */
 259                /* <1> use  mac->bands as mem for hw->wiphy->bands */
 260                sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
 261
 262                /* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
 263                 * to default value(1T1R) */
 264                memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]), &rtl_band_2ghz,
 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_2GHZ] = sband;
 272
 273                /* 2: 5 G bands */
 274                /* <1> use  mac->bands as mem for hw->wiphy->bands */
 275                sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
 276
 277                /* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
 278                 * to default value(1T1R) */
 279                memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]), &rtl_band_5ghz,
 280                                sizeof(struct ieee80211_supported_band));
 281
 282                /* <3> init ht cap base on ant_num */
 283                _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
 284
 285                /* <4> set mac->sband to wiphy->sband */
 286                hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
 287        } else {
 288                if (rtlhal->current_bandtype == BAND_ON_2_4G) {
 289                        /* <1> use  mac->bands as mem for hw->wiphy->bands */
 290                        sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
 291
 292                        /* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
 293                         * to default value(1T1R) */
 294                        memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]),
 295                                 &rtl_band_2ghz,
 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_2GHZ] = sband;
 303                } else if (rtlhal->current_bandtype == BAND_ON_5G) {
 304                        /* <1> use  mac->bands as mem for hw->wiphy->bands */
 305                        sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
 306
 307                        /* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
 308                         * to default value(1T1R) */
 309                        memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]),
 310                                 &rtl_band_5ghz,
 311                                 sizeof(struct ieee80211_supported_band));
 312
 313                        /* <3> init ht cap base on ant_num */
 314                        _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
 315
 316                        /* <4> set mac->sband to wiphy->sband */
 317                        hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
 318                } else {
 319                        RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG, "Err BAND %d\n",
 320                                 rtlhal->current_bandtype);
 321                }
 322        }
 323        /* <5> set hw caps */
 324        hw->flags = IEEE80211_HW_SIGNAL_DBM |
 325            IEEE80211_HW_RX_INCLUDES_FCS |
 326            IEEE80211_HW_AMPDU_AGGREGATION |
 327            IEEE80211_HW_CONNECTION_MONITOR |
 328            /* IEEE80211_HW_SUPPORTS_CQM_RSSI | */
 329            IEEE80211_HW_CONNECTION_MONITOR |
 330            IEEE80211_HW_MFP_CAPABLE |
 331            IEEE80211_HW_REPORTS_TX_ACK_STATUS | 0;
 332
 333        /* swlps or hwlps has been set in diff chip in init_sw_vars */
 334        if (rtlpriv->psc.swctrl_lps)
 335                hw->flags |= IEEE80211_HW_SUPPORTS_PS |
 336                        IEEE80211_HW_PS_NULLFUNC_STACK |
 337                        /* IEEE80211_HW_SUPPORTS_DYNAMIC_PS | */
 338                        0;
 339
 340        hw->wiphy->interface_modes =
 341            BIT(NL80211_IFTYPE_AP) |
 342            BIT(NL80211_IFTYPE_STATION) |
 343            BIT(NL80211_IFTYPE_ADHOC) |
 344            BIT(NL80211_IFTYPE_MESH_POINT) |
 345            BIT(NL80211_IFTYPE_P2P_CLIENT) |
 346            BIT(NL80211_IFTYPE_P2P_GO);
 347
 348        hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
 349        hw->wiphy->rts_threshold = 2347;
 350
 351        hw->queues = AC_MAX;
 352        hw->extra_tx_headroom = RTL_TX_HEADER_SIZE;
 353
 354        /* TODO: Correct this value for our hw */
 355        /* TODO: define these hard code value */
 356        hw->max_listen_interval = 10;
 357        hw->max_rate_tries = 4;
 358        /* hw->max_rates = 1; */
 359        hw->sta_data_size = sizeof(struct rtl_sta_info);
 360
 361        /* <6> mac address */
 362        if (is_valid_ether_addr(rtlefuse->dev_addr)) {
 363                SET_IEEE80211_PERM_ADDR(hw, rtlefuse->dev_addr);
 364        } else {
 365                u8 rtlmac1[] = { 0x00, 0xe0, 0x4c, 0x81, 0x92, 0x00 };
 366                get_random_bytes((rtlmac1 + (ETH_ALEN - 1)), 1);
 367                SET_IEEE80211_PERM_ADDR(hw, rtlmac1);
 368        }
 369
 370}
 371
 372static void _rtl_init_deferred_work(struct ieee80211_hw *hw)
 373{
 374        struct rtl_priv *rtlpriv = rtl_priv(hw);
 375
 376        /* <1> timer */
 377        setup_timer(&rtlpriv->works.watchdog_timer,
 378                    rtl_watch_dog_timer_callback, (unsigned long)hw);
 379        setup_timer(&rtlpriv->works.dualmac_easyconcurrent_retrytimer,
 380                    rtl_easy_concurrent_retrytimer_callback, (unsigned long)hw);
 381
 382        /* <2> work queue */
 383        rtlpriv->works.hw = hw;
 384        rtlpriv->works.rtl_wq = alloc_workqueue("%s", 0, 0, rtlpriv->cfg->name);
 385        INIT_DELAYED_WORK(&rtlpriv->works.watchdog_wq,
 386                          (void *)rtl_watchdog_wq_callback);
 387        INIT_DELAYED_WORK(&rtlpriv->works.ips_nic_off_wq,
 388                          (void *)rtl_ips_nic_off_wq_callback);
 389        INIT_DELAYED_WORK(&rtlpriv->works.ps_work,
 390                          (void *)rtl_swlps_wq_callback);
 391        INIT_DELAYED_WORK(&rtlpriv->works.ps_rfon_wq,
 392                          (void *)rtl_swlps_rfon_wq_callback);
 393        INIT_DELAYED_WORK(&rtlpriv->works.fwevt_wq,
 394                          (void *)rtl_fwevt_wq_callback);
 395
 396}
 397
 398void rtl_deinit_deferred_work(struct ieee80211_hw *hw)
 399{
 400        struct rtl_priv *rtlpriv = rtl_priv(hw);
 401
 402        del_timer_sync(&rtlpriv->works.watchdog_timer);
 403
 404        cancel_delayed_work(&rtlpriv->works.watchdog_wq);
 405        cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
 406        cancel_delayed_work(&rtlpriv->works.ps_work);
 407        cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
 408        cancel_delayed_work(&rtlpriv->works.fwevt_wq);
 409}
 410EXPORT_SYMBOL_GPL(rtl_deinit_deferred_work);
 411
 412void rtl_init_rfkill(struct ieee80211_hw *hw)
 413{
 414        struct rtl_priv *rtlpriv = rtl_priv(hw);
 415
 416        bool radio_state;
 417        bool blocked;
 418        u8 valid = 0;
 419
 420        /*set init state to on */
 421        rtlpriv->rfkill.rfkill_state = true;
 422        wiphy_rfkill_set_hw_state(hw->wiphy, 0);
 423
 424        radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
 425
 426        if (valid) {
 427                pr_info("wireless switch is %s\n",
 428                        rtlpriv->rfkill.rfkill_state ? "on" : "off");
 429
 430                rtlpriv->rfkill.rfkill_state = radio_state;
 431
 432                blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
 433                wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
 434        }
 435
 436        wiphy_rfkill_start_polling(hw->wiphy);
 437}
 438EXPORT_SYMBOL(rtl_init_rfkill);
 439
 440void rtl_deinit_rfkill(struct ieee80211_hw *hw)
 441{
 442        wiphy_rfkill_stop_polling(hw->wiphy);
 443}
 444EXPORT_SYMBOL_GPL(rtl_deinit_rfkill);
 445
 446int rtl_init_core(struct ieee80211_hw *hw)
 447{
 448        struct rtl_priv *rtlpriv = rtl_priv(hw);
 449        struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
 450
 451        /* <1> init mac80211 */
 452        _rtl_init_mac80211(hw);
 453        rtlmac->hw = hw;
 454
 455        /* <2> rate control register */
 456        hw->rate_control_algorithm = "rtl_rc";
 457
 458        /*
 459         * <3> init CRDA must come after init
 460         * mac80211 hw  in _rtl_init_mac80211.
 461         */
 462        if (rtl_regd_init(hw, rtl_reg_notifier)) {
 463                RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "REGD init failed\n");
 464                return 1;
 465        }
 466
 467        /* <4> locks */
 468        mutex_init(&rtlpriv->locks.conf_mutex);
 469        mutex_init(&rtlpriv->locks.ps_mutex);
 470        spin_lock_init(&rtlpriv->locks.ips_lock);
 471        spin_lock_init(&rtlpriv->locks.irq_th_lock);
 472        spin_lock_init(&rtlpriv->locks.irq_pci_lock);
 473        spin_lock_init(&rtlpriv->locks.tx_lock);
 474        spin_lock_init(&rtlpriv->locks.h2c_lock);
 475        spin_lock_init(&rtlpriv->locks.rf_ps_lock);
 476        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        spin_lock_init(&rtlpriv->locks.check_sendpkt_lock);
 482        spin_lock_init(&rtlpriv->locks.fw_ps_lock);
 483        spin_lock_init(&rtlpriv->locks.lps_lock);
 484
 485        /* <5> init list */
 486        INIT_LIST_HEAD(&rtlpriv->entry_list);
 487
 488        rtlmac->link_state = MAC80211_NOLINK;
 489
 490        /* <6> init deferred work */
 491        _rtl_init_deferred_work(hw);
 492
 493        return 0;
 494}
 495EXPORT_SYMBOL_GPL(rtl_init_core);
 496
 497void rtl_deinit_core(struct ieee80211_hw *hw)
 498{
 499}
 500EXPORT_SYMBOL_GPL(rtl_deinit_core);
 501
 502void rtl_init_rx_config(struct ieee80211_hw *hw)
 503{
 504        struct rtl_priv *rtlpriv = rtl_priv(hw);
 505        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
 506
 507        rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
 508}
 509EXPORT_SYMBOL_GPL(rtl_init_rx_config);
 510
 511/*********************************************************
 512 *
 513 * tx information functions
 514 *
 515 *********************************************************/
 516static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
 517                                          struct rtl_tcb_desc *tcb_desc,
 518                                          struct ieee80211_tx_info *info)
 519{
 520        struct rtl_priv *rtlpriv = rtl_priv(hw);
 521        u8 rate_flag = info->control.rates[0].flags;
 522
 523        tcb_desc->use_shortpreamble = false;
 524
 525        /* 1M can only use Long Preamble. 11B spec */
 526        if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
 527                return;
 528        else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
 529                tcb_desc->use_shortpreamble = true;
 530
 531        return;
 532}
 533
 534static void _rtl_query_shortgi(struct ieee80211_hw *hw,
 535                               struct ieee80211_sta *sta,
 536                               struct rtl_tcb_desc *tcb_desc,
 537                               struct ieee80211_tx_info *info)
 538{
 539        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
 540        u8 rate_flag = info->control.rates[0].flags;
 541        u8 sgi_40 = 0, sgi_20 = 0, bw_40 = 0;
 542        tcb_desc->use_shortgi = false;
 543
 544        if (sta == NULL)
 545                return;
 546
 547        sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
 548        sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
 549
 550        if (!(sta->ht_cap.ht_supported))
 551                return;
 552
 553        if (!sgi_40 && !sgi_20)
 554                return;
 555
 556        if (mac->opmode == NL80211_IFTYPE_STATION)
 557                bw_40 = mac->bw_40;
 558        else if (mac->opmode == NL80211_IFTYPE_AP ||
 559                 mac->opmode == NL80211_IFTYPE_ADHOC ||
 560                 mac->opmode == NL80211_IFTYPE_MESH_POINT)
 561                bw_40 = sta->bandwidth >= IEEE80211_STA_RX_BW_40;
 562
 563        if (bw_40 && sgi_40)
 564                tcb_desc->use_shortgi = true;
 565        else if ((bw_40 == false) && sgi_20)
 566                tcb_desc->use_shortgi = true;
 567
 568        if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
 569                tcb_desc->use_shortgi = false;
 570}
 571
 572static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
 573                                       struct rtl_tcb_desc *tcb_desc,
 574                                       struct ieee80211_tx_info *info)
 575{
 576        struct rtl_priv *rtlpriv = rtl_priv(hw);
 577        u8 rate_flag = info->control.rates[0].flags;
 578
 579        /* Common Settings */
 580        tcb_desc->rts_stbc = false;
 581        tcb_desc->cts_enable = false;
 582        tcb_desc->rts_sc = 0;
 583        tcb_desc->rts_bw = false;
 584        tcb_desc->rts_use_shortpreamble = false;
 585        tcb_desc->rts_use_shortgi = false;
 586
 587        if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
 588                /* Use CTS-to-SELF in protection mode. */
 589                tcb_desc->rts_enable = true;
 590                tcb_desc->cts_enable = true;
 591                tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
 592        } else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
 593                /* Use RTS-CTS in protection mode. */
 594                tcb_desc->rts_enable = true;
 595                tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
 596        }
 597}
 598
 599static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
 600                                   struct ieee80211_sta *sta,
 601                                   struct rtl_tcb_desc *tcb_desc)
 602{
 603        struct rtl_priv *rtlpriv = rtl_priv(hw);
 604        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
 605        struct rtl_sta_info *sta_entry = NULL;
 606        u8 ratr_index = 7;
 607
 608        if (sta) {
 609                sta_entry = (struct rtl_sta_info *) sta->drv_priv;
 610                ratr_index = sta_entry->ratr_index;
 611        }
 612        if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
 613                if (mac->opmode == NL80211_IFTYPE_STATION) {
 614                        tcb_desc->ratr_index = 0;
 615                } else if (mac->opmode == NL80211_IFTYPE_ADHOC ||
 616                           mac->opmode == NL80211_IFTYPE_MESH_POINT) {
 617                        if (tcb_desc->multicast || tcb_desc->broadcast) {
 618                                tcb_desc->hw_rate =
 619                                    rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
 620                                tcb_desc->use_driver_rate = 1;
 621                                tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
 622                        } else {
 623                                tcb_desc->ratr_index = ratr_index;
 624                        }
 625                } else if (mac->opmode == NL80211_IFTYPE_AP) {
 626                        tcb_desc->ratr_index = ratr_index;
 627                }
 628        }
 629
 630        if (rtlpriv->dm.useramask) {
 631                tcb_desc->ratr_index = ratr_index;
 632                /* TODO we will differentiate adhoc and station future  */
 633                if (mac->opmode == NL80211_IFTYPE_STATION ||
 634                    mac->opmode == NL80211_IFTYPE_MESH_POINT) {
 635                        tcb_desc->mac_id = 0;
 636
 637                        if (mac->mode == WIRELESS_MODE_N_24G)
 638                                tcb_desc->ratr_index = RATR_INX_WIRELESS_NGB;
 639                        else if (mac->mode == WIRELESS_MODE_N_5G)
 640                                tcb_desc->ratr_index = RATR_INX_WIRELESS_NG;
 641                        else if (mac->mode & WIRELESS_MODE_G)
 642                                tcb_desc->ratr_index = RATR_INX_WIRELESS_GB;
 643                        else if (mac->mode & WIRELESS_MODE_B)
 644                                tcb_desc->ratr_index = RATR_INX_WIRELESS_B;
 645                        else if (mac->mode & WIRELESS_MODE_A)
 646                                tcb_desc->ratr_index = RATR_INX_WIRELESS_G;
 647                } else if (mac->opmode == NL80211_IFTYPE_AP ||
 648                           mac->opmode == NL80211_IFTYPE_ADHOC) {
 649                        if (NULL != sta) {
 650                                if (sta->aid > 0)
 651                                        tcb_desc->mac_id = sta->aid + 1;
 652                                else
 653                                        tcb_desc->mac_id = 1;
 654                        } else {
 655                                tcb_desc->mac_id = 0;
 656                        }
 657                }
 658        }
 659}
 660
 661static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
 662                                      struct ieee80211_sta *sta,
 663                                      struct rtl_tcb_desc *tcb_desc)
 664{
 665        struct rtl_priv *rtlpriv = rtl_priv(hw);
 666        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
 667
 668        tcb_desc->packet_bw = false;
 669        if (!sta)
 670                return;
 671        if (mac->opmode == NL80211_IFTYPE_AP ||
 672            mac->opmode == NL80211_IFTYPE_ADHOC ||
 673            mac->opmode == NL80211_IFTYPE_MESH_POINT) {
 674                if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
 675                        return;
 676        } else if (mac->opmode == NL80211_IFTYPE_STATION) {
 677                if (!mac->bw_40 || !(sta->ht_cap.ht_supported))
 678                        return;
 679        }
 680        if (tcb_desc->multicast || tcb_desc->broadcast)
 681                return;
 682
 683        /*use legency rate, shall use 20MHz */
 684        if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
 685                return;
 686
 687        tcb_desc->packet_bw = true;
 688}
 689
 690static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw)
 691{
 692        struct rtl_priv *rtlpriv = rtl_priv(hw);
 693        struct rtl_phy *rtlphy = &(rtlpriv->phy);
 694        u8 hw_rate;
 695
 696        if (get_rf_type(rtlphy) == RF_2T2R)
 697                hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
 698        else
 699                hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
 700
 701        return hw_rate;
 702}
 703
 704/* mac80211's rate_idx is like this:
 705 *
 706 * 2.4G band:rx_status->band == IEEE80211_BAND_2GHZ
 707 *
 708 * B/G rate:
 709 * (rx_status->flag & RX_FLAG_HT) = 0,
 710 * DESC92_RATE1M-->DESC92_RATE54M ==> idx is 0-->11,
 711 *
 712 * N rate:
 713 * (rx_status->flag & RX_FLAG_HT) = 1,
 714 * DESC92_RATEMCS0-->DESC92_RATEMCS15 ==> idx is 0-->15
 715 *
 716 * 5G band:rx_status->band == IEEE80211_BAND_5GHZ
 717 * A rate:
 718 * (rx_status->flag & RX_FLAG_HT) = 0,
 719 * DESC92_RATE6M-->DESC92_RATE54M ==> idx is 0-->7,
 720 *
 721 * N rate:
 722 * (rx_status->flag & RX_FLAG_HT) = 1,
 723 * DESC92_RATEMCS0-->DESC92_RATEMCS15 ==> idx is 0-->15
 724 */
 725int rtlwifi_rate_mapping(struct ieee80211_hw *hw,
 726                         bool isht, u8 desc_rate, bool first_ampdu)
 727{
 728        int rate_idx;
 729
 730        if (false == isht) {
 731                if (IEEE80211_BAND_2GHZ == hw->conf.chandef.chan->band) {
 732                        switch (desc_rate) {
 733                        case DESC92_RATE1M:
 734                                rate_idx = 0;
 735                                break;
 736                        case DESC92_RATE2M:
 737                                rate_idx = 1;
 738                                break;
 739                        case DESC92_RATE5_5M:
 740                                rate_idx = 2;
 741                                break;
 742                        case DESC92_RATE11M:
 743                                rate_idx = 3;
 744                                break;
 745                        case DESC92_RATE6M:
 746                                rate_idx = 4;
 747                                break;
 748                        case DESC92_RATE9M:
 749                                rate_idx = 5;
 750                                break;
 751                        case DESC92_RATE12M:
 752                                rate_idx = 6;
 753                                break;
 754                        case DESC92_RATE18M:
 755                                rate_idx = 7;
 756                                break;
 757                        case DESC92_RATE24M:
 758                                rate_idx = 8;
 759                                break;
 760                        case DESC92_RATE36M:
 761                                rate_idx = 9;
 762                                break;
 763                        case DESC92_RATE48M:
 764                                rate_idx = 10;
 765                                break;
 766                        case DESC92_RATE54M:
 767                                rate_idx = 11;
 768                                break;
 769                        default:
 770                                rate_idx = 0;
 771                                break;
 772                        }
 773                } else {
 774                        switch (desc_rate) {
 775                        case DESC92_RATE6M:
 776                                rate_idx = 0;
 777                                break;
 778                        case DESC92_RATE9M:
 779                                rate_idx = 1;
 780                                break;
 781                        case DESC92_RATE12M:
 782                                rate_idx = 2;
 783                                break;
 784                        case DESC92_RATE18M:
 785                                rate_idx = 3;
 786                                break;
 787                        case DESC92_RATE24M:
 788                                rate_idx = 4;
 789                                break;
 790                        case DESC92_RATE36M:
 791                                rate_idx = 5;
 792                                break;
 793                        case DESC92_RATE48M:
 794                                rate_idx = 6;
 795                                break;
 796                        case DESC92_RATE54M:
 797                                rate_idx = 7;
 798                                break;
 799                        default:
 800                                rate_idx = 0;
 801                                break;
 802                        }
 803                }
 804
 805        } else {
 806
 807                switch (desc_rate) {
 808                case DESC92_RATEMCS0:
 809                        rate_idx = 0;
 810                        break;
 811                case DESC92_RATEMCS1:
 812                        rate_idx = 1;
 813                        break;
 814                case DESC92_RATEMCS2:
 815                        rate_idx = 2;
 816                        break;
 817                case DESC92_RATEMCS3:
 818                        rate_idx = 3;
 819                        break;
 820                case DESC92_RATEMCS4:
 821                        rate_idx = 4;
 822                        break;
 823                case DESC92_RATEMCS5:
 824                        rate_idx = 5;
 825                        break;
 826                case DESC92_RATEMCS6:
 827                        rate_idx = 6;
 828                        break;
 829                case DESC92_RATEMCS7:
 830                        rate_idx = 7;
 831                        break;
 832                case DESC92_RATEMCS8:
 833                        rate_idx = 8;
 834                        break;
 835                case DESC92_RATEMCS9:
 836                        rate_idx = 9;
 837                        break;
 838                case DESC92_RATEMCS10:
 839                        rate_idx = 10;
 840                        break;
 841                case DESC92_RATEMCS11:
 842                        rate_idx = 11;
 843                        break;
 844                case DESC92_RATEMCS12:
 845                        rate_idx = 12;
 846                        break;
 847                case DESC92_RATEMCS13:
 848                        rate_idx = 13;
 849                        break;
 850                case DESC92_RATEMCS14:
 851                        rate_idx = 14;
 852                        break;
 853                case DESC92_RATEMCS15:
 854                        rate_idx = 15;
 855                        break;
 856                default:
 857                        rate_idx = 0;
 858                        break;
 859                }
 860        }
 861        return rate_idx;
 862}
 863EXPORT_SYMBOL(rtlwifi_rate_mapping);
 864
 865bool rtl_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb)
 866{
 867        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
 868        struct rtl_priv *rtlpriv = rtl_priv(hw);
 869        __le16 fc = rtl_get_fc(skb);
 870
 871        if (rtlpriv->dm.supp_phymode_switch &&
 872            mac->link_state < MAC80211_LINKED &&
 873            (ieee80211_is_auth(fc) || ieee80211_is_probe_req(fc))) {
 874                if (rtlpriv->cfg->ops->chk_switch_dmdp)
 875                        rtlpriv->cfg->ops->chk_switch_dmdp(hw);
 876        }
 877        if (ieee80211_is_auth(fc)) {
 878                RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
 879                rtl_ips_nic_on(hw);
 880
 881                mac->link_state = MAC80211_LINKING;
 882                /* Dual mac */
 883                rtlpriv->phy.need_iqk = true;
 884        }
 885
 886        return true;
 887}
 888EXPORT_SYMBOL_GPL(rtl_tx_mgmt_proc);
 889
 890void rtl_get_tcb_desc(struct ieee80211_hw *hw,
 891                      struct ieee80211_tx_info *info,
 892                      struct ieee80211_sta *sta,
 893                      struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
 894{
 895        struct rtl_priv *rtlpriv = rtl_priv(hw);
 896        struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
 897        struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
 898        struct ieee80211_rate *txrate;
 899        __le16 fc = hdr->frame_control;
 900
 901        txrate = ieee80211_get_tx_rate(hw, info);
 902        if (txrate)
 903                tcb_desc->hw_rate = txrate->hw_value;
 904        else
 905                tcb_desc->hw_rate = 0;
 906
 907        if (ieee80211_is_data(fc)) {
 908                /*
 909                 *we set data rate INX 0
 910                 *in rtl_rc.c   if skb is special data or
 911                 *mgt which need low data rate.
 912                 */
 913
 914                /*
 915                 *So tcb_desc->hw_rate is just used for
 916                 *special data and mgt frames
 917                 */
 918                if (info->control.rates[0].idx == 0 ||
 919                                ieee80211_is_nullfunc(fc)) {
 920                        tcb_desc->use_driver_rate = true;
 921                        tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
 922
 923                        tcb_desc->disable_ratefallback = 1;
 924                } else {
 925                        /*
 926                         *because hw will nerver use hw_rate
 927                         *when tcb_desc->use_driver_rate = false
 928                         *so we never set highest N rate here,
 929                         *and N rate will all be controlled by FW
 930                         *when tcb_desc->use_driver_rate = false
 931                         */
 932                        if (sta && (sta->ht_cap.ht_supported)) {
 933                                tcb_desc->hw_rate = _rtl_get_highest_n_rate(hw);
 934                        } else {
 935                                if (rtlmac->mode == WIRELESS_MODE_B) {
 936                                        tcb_desc->hw_rate =
 937                                           rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
 938                                } else {
 939                                        tcb_desc->hw_rate =
 940                                           rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
 941                                }
 942                        }
 943                }
 944
 945                if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
 946                        tcb_desc->multicast = 1;
 947                else if (is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
 948                        tcb_desc->broadcast = 1;
 949
 950                _rtl_txrate_selectmode(hw, sta, tcb_desc);
 951                _rtl_query_bandwidth_mode(hw, sta, tcb_desc);
 952                _rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
 953                _rtl_query_shortgi(hw, sta, tcb_desc, info);
 954                _rtl_query_protection_mode(hw, tcb_desc, info);
 955        } else {
 956                tcb_desc->use_driver_rate = true;
 957                tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
 958                tcb_desc->disable_ratefallback = 1;
 959                tcb_desc->mac_id = 0;
 960                tcb_desc->packet_bw = false;
 961        }
 962}
 963EXPORT_SYMBOL(rtl_get_tcb_desc);
 964
 965static bool addbareq_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
 966{
 967        struct rtl_priv *rtlpriv = rtl_priv(hw);
 968        struct ieee80211_sta *sta = NULL;
 969        struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
 970        struct rtl_sta_info *sta_entry = NULL;
 971        struct ieee80211_mgmt *mgmt = (void *)skb->data;
 972        u16 capab = 0, tid = 0;
 973        struct rtl_tid_data *tid_data;
 974        struct sk_buff *skb_delba = NULL;
 975        struct ieee80211_rx_status rx_status = { 0 };
 976
 977        rcu_read_lock();
 978        sta = rtl_find_sta(hw, hdr->addr3);
 979        if (sta == NULL) {
 980                RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_EMERG,
 981                         "sta is NULL\n");
 982                rcu_read_unlock();
 983                return true;
 984        }
 985
 986        sta_entry = (struct rtl_sta_info *)sta->drv_priv;
 987        if (!sta_entry) {
 988                rcu_read_unlock();
 989                return true;
 990        }
 991        capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
 992        tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
 993        tid_data = &sta_entry->tids[tid];
 994        if (tid_data->agg.rx_agg_state == RTL_RX_AGG_START) {
 995                skb_delba = rtl_make_del_ba(hw, hdr->addr2, hdr->addr3, tid);
 996                if (skb_delba) {
 997                        rx_status.freq = hw->conf.chandef.chan->center_freq;
 998                        rx_status.band = hw->conf.chandef.chan->band;
 999                        rx_status.flag |= RX_FLAG_DECRYPTED;
1000                        rx_status.flag |= RX_FLAG_MACTIME_END;
1001                        rx_status.rate_idx = 0;
1002                        rx_status.signal = 50 + 10;
1003                        memcpy(IEEE80211_SKB_RXCB(skb_delba), &rx_status,
1004                               sizeof(rx_status));
1005                        RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG,
1006                                      "fake del\n", skb_delba->data,
1007                                      skb_delba->len);
1008                        ieee80211_rx_irqsafe(hw, skb_delba);
1009                }
1010        }
1011        rcu_read_unlock();
1012        return false;
1013}
1014
1015bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
1016{
1017        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1018        struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
1019        struct rtl_priv *rtlpriv = rtl_priv(hw);
1020        __le16 fc = hdr->frame_control;
1021        u8 *act = (u8 *)skb->data + MAC80211_3ADDR_LEN;
1022        u8 category;
1023
1024        if (!ieee80211_is_action(fc))
1025                return true;
1026
1027        category = *act;
1028        act++;
1029        switch (category) {
1030        case ACT_CAT_BA:
1031                switch (*act) {
1032                case ACT_ADDBAREQ:
1033                        if (mac->act_scanning)
1034                                return false;
1035
1036                        RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1037                                 "%s ACT_ADDBAREQ From :%pM\n",
1038                                 is_tx ? "Tx" : "Rx", hdr->addr2);
1039                        RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG, "req\n",
1040                                      skb->data, skb->len);
1041                        if (!is_tx)
1042                                if (addbareq_rx(hw, skb))
1043                                        return true;
1044                        break;
1045                case ACT_ADDBARSP:
1046                        RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1047                                 "%s ACT_ADDBARSP From :%pM\n",
1048                                 is_tx ? "Tx" : "Rx", hdr->addr2);
1049                        break;
1050                case ACT_DELBA:
1051                        RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1052                                 "ACT_ADDBADEL From :%pM\n", hdr->addr2);
1053                        break;
1054                }
1055                break;
1056        default:
1057                break;
1058        }
1059
1060        return true;
1061}
1062EXPORT_SYMBOL_GPL(rtl_action_proc);
1063
1064/*should call before software enc*/
1065u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
1066{
1067        struct rtl_priv *rtlpriv = rtl_priv(hw);
1068        struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1069        __le16 fc = rtl_get_fc(skb);
1070        u16 ether_type;
1071        u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
1072        const struct iphdr *ip;
1073
1074        if (!ieee80211_is_data(fc))
1075                return false;
1076
1077        ip = (const struct iphdr *)(skb->data + mac_hdr_len +
1078                                    SNAP_SIZE + PROTOC_TYPE_SIZE);
1079        ether_type = be16_to_cpup((__be16 *)
1080                                  (skb->data + mac_hdr_len + SNAP_SIZE));
1081
1082        switch (ether_type) {
1083        case ETH_P_IP: {
1084                struct udphdr *udp;
1085                u16 src;
1086                u16 dst;
1087
1088                if (ip->protocol != IPPROTO_UDP)
1089                        return false;
1090                udp = (struct udphdr *)((u8 *)ip + (ip->ihl << 2));
1091                src = be16_to_cpu(udp->source);
1092                dst = be16_to_cpu(udp->dest);
1093
1094                /* If this case involves port 68 (UDP BOOTP client) connecting
1095                 * with port 67 (UDP BOOTP server), then return true so that
1096                 * the lowest speed is used.
1097                 */
1098                if (!((src == 68 && dst == 67) || (src == 67 && dst == 68)))
1099                        return false;
1100
1101                RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1102                         "dhcp %s !!\n", is_tx ? "Tx" : "Rx");
1103                break;
1104        }
1105        case ETH_P_ARP:
1106                break;
1107        case ETH_P_PAE:
1108                RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1109                         "802.1X %s EAPOL pkt!!\n", is_tx ? "Tx" : "Rx");
1110                break;
1111        case ETH_P_IPV6:
1112                /* TODO: Is this right? */
1113                return false;
1114        default:
1115                return false;
1116        }
1117        if (is_tx) {
1118                rtlpriv->enter_ps = false;
1119                schedule_work(&rtlpriv->works.lps_change_work);
1120                ppsc->last_delaylps_stamp_jiffies = jiffies;
1121        }
1122        return true;
1123}
1124EXPORT_SYMBOL_GPL(rtl_is_special_data);
1125
1126/*********************************************************
1127 *
1128 * functions called by core.c
1129 *
1130 *********************************************************/
1131int rtl_tx_agg_start(struct ieee80211_hw *hw,
1132                struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1133{
1134        struct rtl_priv *rtlpriv = rtl_priv(hw);
1135        struct rtl_tid_data *tid_data;
1136        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1137        struct rtl_sta_info *sta_entry = NULL;
1138
1139        if (sta == NULL)
1140                return -EINVAL;
1141
1142        if (unlikely(tid >= MAX_TID_COUNT))
1143                return -EINVAL;
1144
1145        sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1146        if (!sta_entry)
1147                return -ENXIO;
1148        tid_data = &sta_entry->tids[tid];
1149
1150        RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "on ra = %pM tid = %d seq:%d\n",
1151                 sta->addr, tid, tid_data->seq_number);
1152
1153        *ssn = tid_data->seq_number;
1154        tid_data->agg.agg_state = RTL_AGG_START;
1155
1156        ieee80211_start_tx_ba_cb_irqsafe(mac->vif, sta->addr, tid);
1157
1158        return 0;
1159}
1160
1161int rtl_tx_agg_stop(struct ieee80211_hw *hw,
1162                struct ieee80211_sta *sta, u16 tid)
1163{
1164        struct rtl_priv *rtlpriv = rtl_priv(hw);
1165        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1166        struct rtl_sta_info *sta_entry = NULL;
1167
1168        if (sta == NULL)
1169                return -EINVAL;
1170
1171        if (!sta->addr) {
1172                RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "ra = NULL\n");
1173                return -EINVAL;
1174        }
1175
1176        RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "on ra = %pM tid = %d\n",
1177                 sta->addr, tid);
1178
1179        if (unlikely(tid >= MAX_TID_COUNT))
1180                return -EINVAL;
1181
1182        sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1183        sta_entry->tids[tid].agg.agg_state = RTL_AGG_STOP;
1184
1185        ieee80211_stop_tx_ba_cb_irqsafe(mac->vif, sta->addr, tid);
1186
1187        return 0;
1188}
1189
1190int rtl_rx_agg_start(struct ieee80211_hw *hw,
1191                     struct ieee80211_sta *sta, u16 tid)
1192{
1193        struct rtl_priv *rtlpriv = rtl_priv(hw);
1194        struct rtl_tid_data *tid_data;
1195        struct rtl_sta_info *sta_entry = NULL;
1196
1197        if (sta == NULL)
1198                return -EINVAL;
1199
1200        if (unlikely(tid >= MAX_TID_COUNT))
1201                return -EINVAL;
1202
1203        sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1204        if (!sta_entry)
1205                return -ENXIO;
1206        tid_data = &sta_entry->tids[tid];
1207
1208        RT_TRACE(rtlpriv, COMP_RECV, DBG_DMESG,
1209                 "on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
1210                 tid_data->seq_number);
1211
1212        tid_data->agg.rx_agg_state = RTL_RX_AGG_START;
1213        return 0;
1214}
1215
1216int rtl_rx_agg_stop(struct ieee80211_hw *hw,
1217                    struct ieee80211_sta *sta, u16 tid)
1218{
1219        struct rtl_priv *rtlpriv = rtl_priv(hw);
1220        struct rtl_sta_info *sta_entry = NULL;
1221
1222        if (sta == NULL)
1223                return -EINVAL;
1224
1225        if (!sta->addr) {
1226                RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "ra = NULL\n");
1227                return -EINVAL;
1228        }
1229
1230        RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1231                 "on ra = %pM tid = %d\n", sta->addr, tid);
1232
1233        if (unlikely(tid >= MAX_TID_COUNT))
1234                return -EINVAL;
1235
1236        sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1237        sta_entry->tids[tid].agg.rx_agg_state = RTL_RX_AGG_STOP;
1238
1239        return 0;
1240}
1241
1242int rtl_tx_agg_oper(struct ieee80211_hw *hw,
1243                struct ieee80211_sta *sta, u16 tid)
1244{
1245        struct rtl_priv *rtlpriv = rtl_priv(hw);
1246        struct rtl_sta_info *sta_entry = NULL;
1247
1248        if (sta == NULL)
1249                return -EINVAL;
1250
1251        if (!sta->addr) {
1252                RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "ra = NULL\n");
1253                return -EINVAL;
1254        }
1255
1256        RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "on ra = %pM tid = %d\n",
1257                 sta->addr, tid);
1258
1259        if (unlikely(tid >= MAX_TID_COUNT))
1260                return -EINVAL;
1261
1262        sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1263        sta_entry->tids[tid].agg.agg_state = RTL_AGG_OPERATIONAL;
1264
1265        return 0;
1266}
1267
1268/*********************************************************
1269 *
1270 * wq & timer callback functions
1271 *
1272 *********************************************************/
1273/* this function is used for roaming */
1274void rtl_beacon_statistic(struct ieee80211_hw *hw, struct sk_buff *skb)
1275{
1276        struct rtl_priv *rtlpriv = rtl_priv(hw);
1277        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1278
1279        if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
1280                return;
1281
1282        if (rtlpriv->mac80211.link_state < MAC80211_LINKED)
1283                return;
1284
1285        /* check if this really is a beacon */
1286        if (!ieee80211_is_beacon(hdr->frame_control) &&
1287            !ieee80211_is_probe_resp(hdr->frame_control))
1288                return;
1289
1290        /* min. beacon length + FCS_LEN */
1291        if (skb->len <= 40 + FCS_LEN)
1292                return;
1293
1294        /* and only beacons from the associated BSSID, please */
1295        if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid))
1296                return;
1297
1298        rtlpriv->link_info.bcn_rx_inperiod++;
1299}
1300EXPORT_SYMBOL_GPL(rtl_beacon_statistic);
1301
1302void rtl_watchdog_wq_callback(void *data)
1303{
1304        struct rtl_works *rtlworks = container_of_dwork_rtl(data,
1305                                                            struct rtl_works,
1306                                                            watchdog_wq);
1307        struct ieee80211_hw *hw = rtlworks->hw;
1308        struct rtl_priv *rtlpriv = rtl_priv(hw);
1309        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1310        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1311        bool busytraffic = false;
1312        bool tx_busy_traffic = false;
1313        bool rx_busy_traffic = false;
1314        bool higher_busytraffic = false;
1315        bool higher_busyrxtraffic = false;
1316        u8 idx, tid;
1317        u32 rx_cnt_inp4eriod = 0;
1318        u32 tx_cnt_inp4eriod = 0;
1319        u32 aver_rx_cnt_inperiod = 0;
1320        u32 aver_tx_cnt_inperiod = 0;
1321        u32 aver_tidtx_inperiod[MAX_TID_COUNT] = {0};
1322        u32 tidtx_inp4eriod[MAX_TID_COUNT] = {0};
1323
1324        if (is_hal_stop(rtlhal))
1325                return;
1326
1327        /* <1> Determine if action frame is allowed */
1328        if (mac->link_state > MAC80211_NOLINK) {
1329                if (mac->cnt_after_linked < 20)
1330                        mac->cnt_after_linked++;
1331        } else {
1332                mac->cnt_after_linked = 0;
1333        }
1334
1335        /*
1336         *<2> to check if traffic busy, if
1337         * busytraffic we don't change channel
1338         */
1339        if (mac->link_state >= MAC80211_LINKED) {
1340
1341                /* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
1342                for (idx = 0; idx <= 2; idx++) {
1343                        rtlpriv->link_info.num_rx_in4period[idx] =
1344                            rtlpriv->link_info.num_rx_in4period[idx + 1];
1345                        rtlpriv->link_info.num_tx_in4period[idx] =
1346                            rtlpriv->link_info.num_tx_in4period[idx + 1];
1347                }
1348                rtlpriv->link_info.num_rx_in4period[3] =
1349                    rtlpriv->link_info.num_rx_inperiod;
1350                rtlpriv->link_info.num_tx_in4period[3] =
1351                    rtlpriv->link_info.num_tx_inperiod;
1352                for (idx = 0; idx <= 3; idx++) {
1353                        rx_cnt_inp4eriod +=
1354                            rtlpriv->link_info.num_rx_in4period[idx];
1355                        tx_cnt_inp4eriod +=
1356                            rtlpriv->link_info.num_tx_in4period[idx];
1357                }
1358                aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
1359                aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
1360
1361                /* (2) check traffic busy */
1362                if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100) {
1363                        busytraffic = true;
1364                        if (aver_rx_cnt_inperiod > aver_tx_cnt_inperiod)
1365                                rx_busy_traffic = true;
1366                        else
1367                                tx_busy_traffic = false;
1368                }
1369
1370                /* Higher Tx/Rx data. */
1371                if (aver_rx_cnt_inperiod > 4000 ||
1372                    aver_tx_cnt_inperiod > 4000) {
1373                        higher_busytraffic = true;
1374
1375                        /* Extremely high Rx data. */
1376                        if (aver_rx_cnt_inperiod > 5000)
1377                                higher_busyrxtraffic = true;
1378                }
1379
1380                /* check every tid's tx traffic */
1381                for (tid = 0; tid <= 7; tid++) {
1382                        for (idx = 0; idx <= 2; idx++)
1383                                rtlpriv->link_info.tidtx_in4period[tid][idx] =
1384                                  rtlpriv->link_info.tidtx_in4period[tid]
1385                                  [idx + 1];
1386                        rtlpriv->link_info.tidtx_in4period[tid][3] =
1387                                rtlpriv->link_info.tidtx_inperiod[tid];
1388
1389                        for (idx = 0; idx <= 3; idx++)
1390                                tidtx_inp4eriod[tid] +=
1391                                  rtlpriv->link_info.tidtx_in4period[tid][idx];
1392                        aver_tidtx_inperiod[tid] = tidtx_inp4eriod[tid] / 4;
1393                        if (aver_tidtx_inperiod[tid] > 5000)
1394                                rtlpriv->link_info.higher_busytxtraffic[tid] =
1395                                                   true;
1396                        else
1397                                rtlpriv->link_info.higher_busytxtraffic[tid] =
1398                                                   false;
1399                }
1400
1401                if (((rtlpriv->link_info.num_rx_inperiod +
1402                      rtlpriv->link_info.num_tx_inperiod) > 8) ||
1403                    (rtlpriv->link_info.num_rx_inperiod > 2))
1404                        rtlpriv->enter_ps = true;
1405                else
1406                        rtlpriv->enter_ps = false;
1407
1408                /* LeisurePS only work in infra mode. */
1409                schedule_work(&rtlpriv->works.lps_change_work);
1410        }
1411
1412        rtlpriv->link_info.num_rx_inperiod = 0;
1413        rtlpriv->link_info.num_tx_inperiod = 0;
1414        for (tid = 0; tid <= 7; tid++)
1415                rtlpriv->link_info.tidtx_inperiod[tid] = 0;
1416
1417        rtlpriv->link_info.busytraffic = busytraffic;
1418        rtlpriv->link_info.higher_busytraffic = higher_busytraffic;
1419        rtlpriv->link_info.rx_busy_traffic = rx_busy_traffic;
1420        rtlpriv->link_info.tx_busy_traffic = tx_busy_traffic;
1421        rtlpriv->link_info.higher_busyrxtraffic = higher_busyrxtraffic;
1422
1423        /* <3> DM */
1424        rtlpriv->cfg->ops->dm_watchdog(hw);
1425
1426        /* <4> roaming */
1427        if (mac->link_state == MAC80211_LINKED &&
1428            mac->opmode == NL80211_IFTYPE_STATION) {
1429                if ((rtlpriv->link_info.bcn_rx_inperiod +
1430                     rtlpriv->link_info.num_rx_inperiod) == 0) {
1431                        rtlpriv->link_info.roam_times++;
1432                        RT_TRACE(rtlpriv, COMP_ERR, DBG_DMESG,
1433                                 "AP off for %d s\n",
1434                                 (rtlpriv->link_info.roam_times * 2));
1435
1436                        /* if we can't recv beacon for 6s, we should
1437                         * reconnect this AP
1438                         */
1439                        if ((rtlpriv->link_info.roam_times >= 3) &&
1440                            !is_zero_ether_addr(rtlpriv->mac80211.bssid)) {
1441                                RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1442                                         "AP off, try to reconnect now\n");
1443                                rtlpriv->link_info.roam_times = 0;
1444                                ieee80211_connection_loss(rtlpriv->mac80211.vif);
1445                        }
1446                } else {
1447                        rtlpriv->link_info.roam_times = 0;
1448                }
1449        }
1450        rtlpriv->link_info.bcn_rx_inperiod = 0;
1451}
1452
1453void rtl_watch_dog_timer_callback(unsigned long data)
1454{
1455        struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1456        struct rtl_priv *rtlpriv = rtl_priv(hw);
1457
1458        queue_delayed_work(rtlpriv->works.rtl_wq,
1459                           &rtlpriv->works.watchdog_wq, 0);
1460
1461        mod_timer(&rtlpriv->works.watchdog_timer,
1462                  jiffies + MSECS(RTL_WATCH_DOG_TIME));
1463}
1464
1465void rtl_fwevt_wq_callback(void *data)
1466{
1467        struct rtl_works *rtlworks =
1468                container_of_dwork_rtl(data, struct rtl_works, fwevt_wq);
1469        struct ieee80211_hw *hw = rtlworks->hw;
1470        struct rtl_priv *rtlpriv = rtl_priv(hw);
1471
1472        rtlpriv->cfg->ops->c2h_command_handle(hw);
1473}
1474
1475void rtl_easy_concurrent_retrytimer_callback(unsigned long data)
1476{
1477        struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1478        struct rtl_priv *rtlpriv = rtl_priv(hw);
1479        struct rtl_priv *buddy_priv = rtlpriv->buddy_priv;
1480
1481        if (buddy_priv == NULL)
1482                return;
1483
1484        rtlpriv->cfg->ops->dualmac_easy_concurrent(hw);
1485}
1486
1487/*********************************************************
1488 *
1489 * frame process functions
1490 *
1491 *********************************************************/
1492u8 *rtl_find_ie(u8 *data, unsigned int len, u8 ie)
1493{
1494        struct ieee80211_mgmt *mgmt = (void *)data;
1495        u8 *pos, *end;
1496
1497        pos = (u8 *)mgmt->u.beacon.variable;
1498        end = data + len;
1499        while (pos < end) {
1500                if (pos + 2 + pos[1] > end)
1501                        return NULL;
1502
1503                if (pos[0] == ie)
1504                        return pos;
1505
1506                pos += 2 + pos[1];
1507        }
1508        return NULL;
1509}
1510
1511/* when we use 2 rx ants we send IEEE80211_SMPS_OFF */
1512/* when we use 1 rx ant we send IEEE80211_SMPS_STATIC */
1513static struct sk_buff *rtl_make_smps_action(struct ieee80211_hw *hw,
1514                enum ieee80211_smps_mode smps, u8 *da, u8 *bssid)
1515{
1516        struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1517        struct sk_buff *skb;
1518        struct ieee80211_mgmt *action_frame;
1519
1520        /* 27 = header + category + action + smps mode */
1521        skb = dev_alloc_skb(27 + hw->extra_tx_headroom);
1522        if (!skb)
1523                return NULL;
1524
1525        skb_reserve(skb, hw->extra_tx_headroom);
1526        action_frame = (void *)skb_put(skb, 27);
1527        memset(action_frame, 0, 27);
1528        memcpy(action_frame->da, da, ETH_ALEN);
1529        memcpy(action_frame->sa, rtlefuse->dev_addr, ETH_ALEN);
1530        memcpy(action_frame->bssid, bssid, ETH_ALEN);
1531        action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1532                                                  IEEE80211_STYPE_ACTION);
1533        action_frame->u.action.category = WLAN_CATEGORY_HT;
1534        action_frame->u.action.u.ht_smps.action = WLAN_HT_ACTION_SMPS;
1535        switch (smps) {
1536        case IEEE80211_SMPS_AUTOMATIC:/* 0 */
1537        case IEEE80211_SMPS_NUM_MODES:/* 4 */
1538                WARN_ON(1);
1539        case IEEE80211_SMPS_OFF:/* 1 */ /*MIMO_PS_NOLIMIT*/
1540                action_frame->u.action.u.ht_smps.smps_control =
1541                                WLAN_HT_SMPS_CONTROL_DISABLED;/* 0 */
1542                break;
1543        case IEEE80211_SMPS_STATIC:/* 2 */ /*MIMO_PS_STATIC*/
1544                action_frame->u.action.u.ht_smps.smps_control =
1545                                WLAN_HT_SMPS_CONTROL_STATIC;/* 1 */
1546                break;
1547        case IEEE80211_SMPS_DYNAMIC:/* 3 */ /*MIMO_PS_DYNAMIC*/
1548                action_frame->u.action.u.ht_smps.smps_control =
1549                                WLAN_HT_SMPS_CONTROL_DYNAMIC;/* 3 */
1550                break;
1551        }
1552
1553        return skb;
1554}
1555
1556int rtl_send_smps_action(struct ieee80211_hw *hw,
1557                struct ieee80211_sta *sta,
1558                enum ieee80211_smps_mode smps)
1559{
1560        struct rtl_priv *rtlpriv = rtl_priv(hw);
1561        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1562        struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1563        struct sk_buff *skb = NULL;
1564        struct rtl_tcb_desc tcb_desc;
1565        u8 bssid[ETH_ALEN] = {0};
1566
1567        memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
1568
1569        if (rtlpriv->mac80211.act_scanning)
1570                goto err_free;
1571
1572        if (!sta)
1573                goto err_free;
1574
1575        if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
1576                goto err_free;
1577
1578        if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
1579                goto err_free;
1580
1581        if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP)
1582                memcpy(bssid, rtlpriv->efuse.dev_addr, ETH_ALEN);
1583        else
1584                memcpy(bssid, rtlpriv->mac80211.bssid, ETH_ALEN);
1585
1586        skb = rtl_make_smps_action(hw, smps, sta->addr, bssid);
1587        /* this is a type = mgmt * stype = action frame */
1588        if (skb) {
1589                struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1590                struct rtl_sta_info *sta_entry =
1591                        (struct rtl_sta_info *) sta->drv_priv;
1592                sta_entry->mimo_ps = smps;
1593
1594                info->control.rates[0].idx = 0;
1595                info->band = hw->conf.chandef.chan->band;
1596                rtlpriv->intf_ops->adapter_tx(hw, sta, skb, &tcb_desc);
1597        }
1598        return 1;
1599
1600err_free:
1601        return 0;
1602}
1603EXPORT_SYMBOL(rtl_send_smps_action);
1604
1605void rtl_phy_scan_operation_backup(struct ieee80211_hw *hw, u8 operation)
1606{
1607        struct rtl_priv *rtlpriv = rtl_priv(hw);
1608        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1609        enum io_type iotype;
1610
1611        if (!is_hal_stop(rtlhal)) {
1612                switch (operation) {
1613                case SCAN_OPT_BACKUP:
1614                        iotype = IO_CMD_PAUSE_DM_BY_SCAN;
1615                        rtlpriv->cfg->ops->set_hw_reg(hw,
1616                                                      HW_VAR_IO_CMD,
1617                                                      (u8 *)&iotype);
1618                        break;
1619                case SCAN_OPT_RESTORE:
1620                        iotype = IO_CMD_RESUME_DM_BY_SCAN;
1621                        rtlpriv->cfg->ops->set_hw_reg(hw,
1622                                                      HW_VAR_IO_CMD,
1623                                                      (u8 *)&iotype);
1624                        break;
1625                default:
1626                        RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1627                                 "Unknown Scan Backup operation.\n");
1628                        break;
1629                }
1630        }
1631}
1632EXPORT_SYMBOL(rtl_phy_scan_operation_backup);
1633
1634/* There seem to be issues in mac80211 regarding when del ba frames can be
1635 * received. As a work around, we make a fake del_ba if we receive a ba_req;
1636 * however, rx_agg was opened to let mac80211 release some ba related
1637 * resources. This del_ba is for tx only.
1638 */
1639struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw,
1640                                u8 *sa, u8 *bssid, u16 tid)
1641{
1642        struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1643        struct sk_buff *skb;
1644        struct ieee80211_mgmt *action_frame;
1645        u16 params;
1646
1647        /* 27 = header + category + action + smps mode */
1648        skb = dev_alloc_skb(34 + hw->extra_tx_headroom);
1649        if (!skb)
1650                return NULL;
1651
1652        skb_reserve(skb, hw->extra_tx_headroom);
1653        action_frame = (void *)skb_put(skb, 34);
1654        memset(action_frame, 0, 34);
1655        memcpy(action_frame->sa, sa, ETH_ALEN);
1656        memcpy(action_frame->da, rtlefuse->dev_addr, ETH_ALEN);
1657        memcpy(action_frame->bssid, bssid, ETH_ALEN);
1658        action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1659                                                  IEEE80211_STYPE_ACTION);
1660        action_frame->u.action.category = WLAN_CATEGORY_BACK;
1661        action_frame->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1662        params = (u16)(1 << 11);        /* bit 11 initiator */
1663        params |= (u16)(tid << 12);             /* bit 15:12 TID number */
1664
1665        action_frame->u.action.u.delba.params = cpu_to_le16(params);
1666        action_frame->u.action.u.delba.reason_code =
1667                cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT);
1668
1669        return skb;
1670}
1671
1672/*********************************************************
1673 *
1674 * IOT functions
1675 *
1676 *********************************************************/
1677static bool rtl_chk_vendor_ouisub(struct ieee80211_hw *hw,
1678                struct octet_string vendor_ie)
1679{
1680        struct rtl_priv *rtlpriv = rtl_priv(hw);
1681        bool matched = false;
1682        static u8 athcap_1[] = { 0x00, 0x03, 0x7F };
1683        static u8 athcap_2[] = { 0x00, 0x13, 0x74 };
1684        static u8 broadcap_1[] = { 0x00, 0x10, 0x18 };
1685        static u8 broadcap_2[] = { 0x00, 0x0a, 0xf7 };
1686        static u8 broadcap_3[] = { 0x00, 0x05, 0xb5 };
1687        static u8 racap[] = { 0x00, 0x0c, 0x43 };
1688        static u8 ciscocap[] = { 0x00, 0x40, 0x96 };
1689        static u8 marvcap[] = { 0x00, 0x50, 0x43 };
1690
1691        if (memcmp(vendor_ie.octet, athcap_1, 3) == 0 ||
1692                memcmp(vendor_ie.octet, athcap_2, 3) == 0) {
1693                rtlpriv->mac80211.vendor = PEER_ATH;
1694                matched = true;
1695        } else if (memcmp(vendor_ie.octet, broadcap_1, 3) == 0 ||
1696                memcmp(vendor_ie.octet, broadcap_2, 3) == 0 ||
1697                memcmp(vendor_ie.octet, broadcap_3, 3) == 0) {
1698                rtlpriv->mac80211.vendor = PEER_BROAD;
1699                matched = true;
1700        } else if (memcmp(vendor_ie.octet, racap, 3) == 0) {
1701                rtlpriv->mac80211.vendor = PEER_RAL;
1702                matched = true;
1703        } else if (memcmp(vendor_ie.octet, ciscocap, 3) == 0) {
1704                rtlpriv->mac80211.vendor = PEER_CISCO;
1705                matched = true;
1706        } else if (memcmp(vendor_ie.octet, marvcap, 3) == 0) {
1707                rtlpriv->mac80211.vendor = PEER_MARV;
1708                matched = true;
1709        }
1710
1711        return matched;
1712}
1713
1714static bool rtl_find_221_ie(struct ieee80211_hw *hw, u8 *data,
1715                unsigned int len)
1716{
1717        struct ieee80211_mgmt *mgmt = (void *)data;
1718        struct octet_string vendor_ie;
1719        u8 *pos, *end;
1720
1721        pos = (u8 *)mgmt->u.beacon.variable;
1722        end = data + len;
1723        while (pos < end) {
1724                if (pos[0] == 221) {
1725                        vendor_ie.length = pos[1];
1726                        vendor_ie.octet = &pos[2];
1727                        if (rtl_chk_vendor_ouisub(hw, vendor_ie))
1728                                return true;
1729                }
1730
1731                if (pos + 2 + pos[1] > end)
1732                        return false;
1733
1734                pos += 2 + pos[1];
1735        }
1736        return false;
1737}
1738
1739void rtl_recognize_peer(struct ieee80211_hw *hw, u8 *data, unsigned int len)
1740{
1741        struct rtl_priv *rtlpriv = rtl_priv(hw);
1742        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1743        struct ieee80211_hdr *hdr = (void *)data;
1744        u32 vendor = PEER_UNKNOWN;
1745
1746        static u8 ap3_1[3] = { 0x00, 0x14, 0xbf };
1747        static u8 ap3_2[3] = { 0x00, 0x1a, 0x70 };
1748        static u8 ap3_3[3] = { 0x00, 0x1d, 0x7e };
1749        static u8 ap4_1[3] = { 0x00, 0x90, 0xcc };
1750        static u8 ap4_2[3] = { 0x00, 0x0e, 0x2e };
1751        static u8 ap4_3[3] = { 0x00, 0x18, 0x02 };
1752        static u8 ap4_4[3] = { 0x00, 0x17, 0x3f };
1753        static u8 ap4_5[3] = { 0x00, 0x1c, 0xdf };
1754        static u8 ap5_1[3] = { 0x00, 0x1c, 0xf0 };
1755        static u8 ap5_2[3] = { 0x00, 0x21, 0x91 };
1756        static u8 ap5_3[3] = { 0x00, 0x24, 0x01 };
1757        static u8 ap5_4[3] = { 0x00, 0x15, 0xe9 };
1758        static u8 ap5_5[3] = { 0x00, 0x17, 0x9A };
1759        static u8 ap5_6[3] = { 0x00, 0x18, 0xE7 };
1760        static u8 ap6_1[3] = { 0x00, 0x17, 0x94 };
1761        static u8 ap7_1[3] = { 0x00, 0x14, 0xa4 };
1762
1763        if (mac->opmode != NL80211_IFTYPE_STATION)
1764                return;
1765
1766        if (mac->link_state == MAC80211_NOLINK) {
1767                mac->vendor = PEER_UNKNOWN;
1768                return;
1769        }
1770
1771        if (mac->cnt_after_linked > 2)
1772                return;
1773
1774        /* check if this really is a beacon */
1775        if (!ieee80211_is_beacon(hdr->frame_control))
1776                return;
1777
1778        /* min. beacon length + FCS_LEN */
1779        if (len <= 40 + FCS_LEN)
1780                return;
1781
1782        /* and only beacons from the associated BSSID, please */
1783        if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid))
1784                return;
1785
1786        if (rtl_find_221_ie(hw, data, len))
1787                vendor = mac->vendor;
1788
1789        if ((memcmp(mac->bssid, ap5_1, 3) == 0) ||
1790                (memcmp(mac->bssid, ap5_2, 3) == 0) ||
1791                (memcmp(mac->bssid, ap5_3, 3) == 0) ||
1792                (memcmp(mac->bssid, ap5_4, 3) == 0) ||
1793                (memcmp(mac->bssid, ap5_5, 3) == 0) ||
1794                (memcmp(mac->bssid, ap5_6, 3) == 0) ||
1795                vendor == PEER_ATH) {
1796                vendor = PEER_ATH;
1797                RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ath find\n");
1798        } else if ((memcmp(mac->bssid, ap4_4, 3) == 0) ||
1799                (memcmp(mac->bssid, ap4_5, 3) == 0) ||
1800                (memcmp(mac->bssid, ap4_1, 3) == 0) ||
1801                (memcmp(mac->bssid, ap4_2, 3) == 0) ||
1802                (memcmp(mac->bssid, ap4_3, 3) == 0) ||
1803                vendor == PEER_RAL) {
1804                RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ral find\n");
1805                vendor = PEER_RAL;
1806        } else if (memcmp(mac->bssid, ap6_1, 3) == 0 ||
1807                vendor == PEER_CISCO) {
1808                vendor = PEER_CISCO;
1809                RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>cisco find\n");
1810        } else if ((memcmp(mac->bssid, ap3_1, 3) == 0) ||
1811                (memcmp(mac->bssid, ap3_2, 3) == 0) ||
1812                (memcmp(mac->bssid, ap3_3, 3) == 0) ||
1813                vendor == PEER_BROAD) {
1814                RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>broad find\n");
1815                vendor = PEER_BROAD;
1816        } else if (memcmp(mac->bssid, ap7_1, 3) == 0 ||
1817                vendor == PEER_MARV) {
1818                vendor = PEER_MARV;
1819                RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>marv find\n");
1820        }
1821
1822        mac->vendor = vendor;
1823}
1824EXPORT_SYMBOL_GPL(rtl_recognize_peer);
1825
1826/*********************************************************
1827 *
1828 * sysfs functions
1829 *
1830 *********************************************************/
1831static ssize_t rtl_show_debug_level(struct device *d,
1832                                    struct device_attribute *attr, char *buf)
1833{
1834        struct ieee80211_hw *hw = dev_get_drvdata(d);
1835        struct rtl_priv *rtlpriv = rtl_priv(hw);
1836
1837        return sprintf(buf, "0x%08X\n", rtlpriv->dbg.global_debuglevel);
1838}
1839
1840static ssize_t rtl_store_debug_level(struct device *d,
1841                                     struct device_attribute *attr,
1842                                     const char *buf, size_t count)
1843{
1844        struct ieee80211_hw *hw = dev_get_drvdata(d);
1845        struct rtl_priv *rtlpriv = rtl_priv(hw);
1846        unsigned long val;
1847        int ret;
1848
1849        ret = kstrtoul(buf, 0, &val);
1850        if (ret) {
1851                printk(KERN_DEBUG "%s is not in hex or decimal form.\n", buf);
1852        } else {
1853                rtlpriv->dbg.global_debuglevel = val;
1854                printk(KERN_DEBUG "debuglevel:%x\n",
1855                       rtlpriv->dbg.global_debuglevel);
1856        }
1857
1858        return strnlen(buf, count);
1859}
1860
1861static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
1862                   rtl_show_debug_level, rtl_store_debug_level);
1863
1864static struct attribute *rtl_sysfs_entries[] = {
1865
1866        &dev_attr_debug_level.attr,
1867
1868        NULL
1869};
1870
1871/*
1872 * "name" is folder name witch will be
1873 * put in device directory like :
1874 * sys/devices/pci0000:00/0000:00:1c.4/
1875 * 0000:06:00.0/rtl_sysfs
1876 */
1877struct attribute_group rtl_attribute_group = {
1878        .name = "rtlsysfs",
1879        .attrs = rtl_sysfs_entries,
1880};
1881EXPORT_SYMBOL_GPL(rtl_attribute_group);
1882
1883MODULE_AUTHOR("lizhaoming       <chaoming_li@realsil.com.cn>");
1884MODULE_AUTHOR("Realtek WlanFAE  <wlanfae@realtek.com>");
1885MODULE_AUTHOR("Larry Finger     <Larry.FInger@lwfinger.net>");
1886MODULE_LICENSE("GPL");
1887MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
1888
1889struct rtl_global_var rtl_global_var = {};
1890EXPORT_SYMBOL_GPL(rtl_global_var);
1891
1892static int __init rtl_core_module_init(void)
1893{
1894        if (rtl_rate_control_register())
1895                pr_err("Unable to register rtl_rc, use default RC !!\n");
1896
1897        /* init some global vars */
1898        INIT_LIST_HEAD(&rtl_global_var.glb_priv_list);
1899        spin_lock_init(&rtl_global_var.glb_list_lock);
1900
1901        return 0;
1902}
1903
1904static void __exit rtl_core_module_exit(void)
1905{
1906        /*RC*/
1907        rtl_rate_control_unregister();
1908}
1909
1910module_init(rtl_core_module_init);
1911module_exit(rtl_core_module_exit);
1912