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