linux/drivers/net/wireless/marvell/mwifiex/cfg80211.c
<<
>>
Prefs
   1/*
   2 * Marvell Wireless LAN device driver: CFG80211
   3 *
   4 * Copyright (C) 2011-2014, Marvell International Ltd.
   5 *
   6 * This software file (the "File") is distributed by Marvell International
   7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
   8 * (the "License").  You may use, redistribute and/or modify this File in
   9 * accordance with the terms and conditions of the License, a copy of which
  10 * is available by writing to the Free Software Foundation, Inc.,
  11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
  12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  13 *
  14 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  15 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  16 * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
  17 * this warranty disclaimer.
  18 */
  19
  20#include "cfg80211.h"
  21#include "main.h"
  22#include "11n.h"
  23#include "wmm.h"
  24
  25static char *reg_alpha2;
  26module_param(reg_alpha2, charp, 0);
  27
  28static const struct ieee80211_iface_limit mwifiex_ap_sta_limits[] = {
  29        {
  30                .max = 3, .types = BIT(NL80211_IFTYPE_STATION) |
  31                                   BIT(NL80211_IFTYPE_P2P_GO) |
  32                                   BIT(NL80211_IFTYPE_P2P_CLIENT) |
  33                                   BIT(NL80211_IFTYPE_AP),
  34        },
  35};
  36
  37static const struct ieee80211_iface_combination
  38mwifiex_iface_comb_ap_sta = {
  39        .limits = mwifiex_ap_sta_limits,
  40        .num_different_channels = 1,
  41        .n_limits = ARRAY_SIZE(mwifiex_ap_sta_limits),
  42        .max_interfaces = MWIFIEX_MAX_BSS_NUM,
  43        .beacon_int_infra_match = true,
  44        .radar_detect_widths =  BIT(NL80211_CHAN_WIDTH_20_NOHT) |
  45                                BIT(NL80211_CHAN_WIDTH_20) |
  46                                BIT(NL80211_CHAN_WIDTH_40),
  47};
  48
  49static const struct ieee80211_iface_combination
  50mwifiex_iface_comb_ap_sta_vht = {
  51        .limits = mwifiex_ap_sta_limits,
  52        .num_different_channels = 1,
  53        .n_limits = ARRAY_SIZE(mwifiex_ap_sta_limits),
  54        .max_interfaces = MWIFIEX_MAX_BSS_NUM,
  55        .beacon_int_infra_match = true,
  56        .radar_detect_widths =  BIT(NL80211_CHAN_WIDTH_20_NOHT) |
  57                                BIT(NL80211_CHAN_WIDTH_20) |
  58                                BIT(NL80211_CHAN_WIDTH_40) |
  59                                BIT(NL80211_CHAN_WIDTH_80),
  60};
  61
  62static const struct
  63ieee80211_iface_combination mwifiex_iface_comb_ap_sta_drcs = {
  64        .limits = mwifiex_ap_sta_limits,
  65        .num_different_channels = 2,
  66        .n_limits = ARRAY_SIZE(mwifiex_ap_sta_limits),
  67        .max_interfaces = MWIFIEX_MAX_BSS_NUM,
  68        .beacon_int_infra_match = true,
  69};
  70
  71/*
  72 * This function maps the nl802.11 channel type into driver channel type.
  73 *
  74 * The mapping is as follows -
  75 *      NL80211_CHAN_NO_HT     -> IEEE80211_HT_PARAM_CHA_SEC_NONE
  76 *      NL80211_CHAN_HT20      -> IEEE80211_HT_PARAM_CHA_SEC_NONE
  77 *      NL80211_CHAN_HT40PLUS  -> IEEE80211_HT_PARAM_CHA_SEC_ABOVE
  78 *      NL80211_CHAN_HT40MINUS -> IEEE80211_HT_PARAM_CHA_SEC_BELOW
  79 *      Others                 -> IEEE80211_HT_PARAM_CHA_SEC_NONE
  80 */
  81u8 mwifiex_chan_type_to_sec_chan_offset(enum nl80211_channel_type chan_type)
  82{
  83        switch (chan_type) {
  84        case NL80211_CHAN_NO_HT:
  85        case NL80211_CHAN_HT20:
  86                return IEEE80211_HT_PARAM_CHA_SEC_NONE;
  87        case NL80211_CHAN_HT40PLUS:
  88                return IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  89        case NL80211_CHAN_HT40MINUS:
  90                return IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  91        default:
  92                return IEEE80211_HT_PARAM_CHA_SEC_NONE;
  93        }
  94}
  95
  96/* This function maps IEEE HT secondary channel type to NL80211 channel type
  97 */
  98u8 mwifiex_sec_chan_offset_to_chan_type(u8 second_chan_offset)
  99{
 100        switch (second_chan_offset) {
 101        case IEEE80211_HT_PARAM_CHA_SEC_NONE:
 102                return NL80211_CHAN_HT20;
 103        case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
 104                return NL80211_CHAN_HT40PLUS;
 105        case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
 106                return NL80211_CHAN_HT40MINUS;
 107        default:
 108                return NL80211_CHAN_HT20;
 109        }
 110}
 111
 112/*
 113 * This function checks whether WEP is set.
 114 */
 115static int
 116mwifiex_is_alg_wep(u32 cipher)
 117{
 118        switch (cipher) {
 119        case WLAN_CIPHER_SUITE_WEP40:
 120        case WLAN_CIPHER_SUITE_WEP104:
 121                return 1;
 122        default:
 123                break;
 124        }
 125
 126        return 0;
 127}
 128
 129/*
 130 * This function retrieves the private structure from kernel wiphy structure.
 131 */
 132static void *mwifiex_cfg80211_get_adapter(struct wiphy *wiphy)
 133{
 134        return (void *) (*(unsigned long *) wiphy_priv(wiphy));
 135}
 136
 137/*
 138 * CFG802.11 operation handler to delete a network key.
 139 */
 140static int
 141mwifiex_cfg80211_del_key(struct wiphy *wiphy, struct net_device *netdev,
 142                         u8 key_index, bool pairwise, const u8 *mac_addr)
 143{
 144        struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
 145        const u8 bc_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
 146        const u8 *peer_mac = pairwise ? mac_addr : bc_mac;
 147
 148        if (mwifiex_set_encode(priv, NULL, NULL, 0, key_index, peer_mac, 1)) {
 149                mwifiex_dbg(priv->adapter, ERROR, "deleting the crypto keys\n");
 150                return -EFAULT;
 151        }
 152
 153        mwifiex_dbg(priv->adapter, INFO, "info: crypto keys deleted\n");
 154        return 0;
 155}
 156
 157/*
 158 * This function forms an skb for management frame.
 159 */
 160static int
 161mwifiex_form_mgmt_frame(struct sk_buff *skb, const u8 *buf, size_t len)
 162{
 163        u8 addr[ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
 164        u16 pkt_len;
 165        u32 tx_control = 0, pkt_type = PKT_TYPE_MGMT;
 166
 167        pkt_len = len + ETH_ALEN;
 168
 169        skb_reserve(skb, MWIFIEX_MIN_DATA_HEADER_LEN +
 170                    MWIFIEX_MGMT_FRAME_HEADER_SIZE + sizeof(pkt_len));
 171        memcpy(skb_push(skb, sizeof(pkt_len)), &pkt_len, sizeof(pkt_len));
 172
 173        memcpy(skb_push(skb, sizeof(tx_control)),
 174               &tx_control, sizeof(tx_control));
 175
 176        memcpy(skb_push(skb, sizeof(pkt_type)), &pkt_type, sizeof(pkt_type));
 177
 178        /* Add packet data and address4 */
 179        skb_put_data(skb, buf, sizeof(struct ieee80211_hdr_3addr));
 180        skb_put_data(skb, addr, ETH_ALEN);
 181        skb_put_data(skb, buf + sizeof(struct ieee80211_hdr_3addr),
 182                     len - sizeof(struct ieee80211_hdr_3addr));
 183
 184        skb->priority = LOW_PRIO_TID;
 185        __net_timestamp(skb);
 186
 187        return 0;
 188}
 189
 190/*
 191 * CFG802.11 operation handler to transmit a management frame.
 192 */
 193static int
 194mwifiex_cfg80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
 195                         struct cfg80211_mgmt_tx_params *params, u64 *cookie)
 196{
 197        const u8 *buf = params->buf;
 198        size_t len = params->len;
 199        struct sk_buff *skb;
 200        u16 pkt_len;
 201        const struct ieee80211_mgmt *mgmt;
 202        struct mwifiex_txinfo *tx_info;
 203        struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
 204
 205        if (!buf || !len) {
 206                mwifiex_dbg(priv->adapter, ERROR, "invalid buffer and length\n");
 207                return -EFAULT;
 208        }
 209
 210        mgmt = (const struct ieee80211_mgmt *)buf;
 211        if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_STA &&
 212            ieee80211_is_probe_resp(mgmt->frame_control)) {
 213                /* Since we support offload probe resp, we need to skip probe
 214                 * resp in AP or GO mode */
 215                mwifiex_dbg(priv->adapter, INFO,
 216                            "info: skip to send probe resp in AP or GO mode\n");
 217                return 0;
 218        }
 219
 220        pkt_len = len + ETH_ALEN;
 221        skb = dev_alloc_skb(MWIFIEX_MIN_DATA_HEADER_LEN +
 222                            MWIFIEX_MGMT_FRAME_HEADER_SIZE +
 223                            pkt_len + sizeof(pkt_len));
 224
 225        if (!skb) {
 226                mwifiex_dbg(priv->adapter, ERROR,
 227                            "allocate skb failed for management frame\n");
 228                return -ENOMEM;
 229        }
 230
 231        tx_info = MWIFIEX_SKB_TXCB(skb);
 232        memset(tx_info, 0, sizeof(*tx_info));
 233        tx_info->bss_num = priv->bss_num;
 234        tx_info->bss_type = priv->bss_type;
 235        tx_info->pkt_len = pkt_len;
 236
 237        mwifiex_form_mgmt_frame(skb, buf, len);
 238        *cookie = prandom_u32() | 1;
 239
 240        if (ieee80211_is_action(mgmt->frame_control))
 241                skb = mwifiex_clone_skb_for_tx_status(priv,
 242                                                      skb,
 243                                MWIFIEX_BUF_FLAG_ACTION_TX_STATUS, cookie);
 244        else
 245                cfg80211_mgmt_tx_status(wdev, *cookie, buf, len, true,
 246                                        GFP_ATOMIC);
 247
 248        mwifiex_queue_tx_pkt(priv, skb);
 249
 250        mwifiex_dbg(priv->adapter, INFO, "info: management frame transmitted\n");
 251        return 0;
 252}
 253
 254/*
 255 * CFG802.11 operation handler to register a mgmt frame.
 256 */
 257static void
 258mwifiex_cfg80211_mgmt_frame_register(struct wiphy *wiphy,
 259                                     struct wireless_dev *wdev,
 260                                     u16 frame_type, bool reg)
 261{
 262        struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
 263        u32 mask;
 264
 265        if (reg)
 266                mask = priv->mgmt_frame_mask | BIT(frame_type >> 4);
 267        else
 268                mask = priv->mgmt_frame_mask & ~BIT(frame_type >> 4);
 269
 270        if (mask != priv->mgmt_frame_mask) {
 271                priv->mgmt_frame_mask = mask;
 272                mwifiex_send_cmd(priv, HostCmd_CMD_MGMT_FRAME_REG,
 273                                 HostCmd_ACT_GEN_SET, 0,
 274                                 &priv->mgmt_frame_mask, false);
 275                mwifiex_dbg(priv->adapter, INFO, "info: mgmt frame registered\n");
 276        }
 277}
 278
 279/*
 280 * CFG802.11 operation handler to remain on channel.
 281 */
 282static int
 283mwifiex_cfg80211_remain_on_channel(struct wiphy *wiphy,
 284                                   struct wireless_dev *wdev,
 285                                   struct ieee80211_channel *chan,
 286                                   unsigned int duration, u64 *cookie)
 287{
 288        struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
 289        int ret;
 290
 291        if (!chan || !cookie) {
 292                mwifiex_dbg(priv->adapter, ERROR, "Invalid parameter for ROC\n");
 293                return -EINVAL;
 294        }
 295
 296        if (priv->roc_cfg.cookie) {
 297                mwifiex_dbg(priv->adapter, INFO,
 298                            "info: ongoing ROC, cookie = 0x%llx\n",
 299                            priv->roc_cfg.cookie);
 300                return -EBUSY;
 301        }
 302
 303        ret = mwifiex_remain_on_chan_cfg(priv, HostCmd_ACT_GEN_SET, chan,
 304                                         duration);
 305
 306        if (!ret) {
 307                *cookie = prandom_u32() | 1;
 308                priv->roc_cfg.cookie = *cookie;
 309                priv->roc_cfg.chan = *chan;
 310
 311                cfg80211_ready_on_channel(wdev, *cookie, chan,
 312                                          duration, GFP_ATOMIC);
 313
 314                mwifiex_dbg(priv->adapter, INFO,
 315                            "info: ROC, cookie = 0x%llx\n", *cookie);
 316        }
 317
 318        return ret;
 319}
 320
 321/*
 322 * CFG802.11 operation handler to cancel remain on channel.
 323 */
 324static int
 325mwifiex_cfg80211_cancel_remain_on_channel(struct wiphy *wiphy,
 326                                          struct wireless_dev *wdev, u64 cookie)
 327{
 328        struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
 329        int ret;
 330
 331        if (cookie != priv->roc_cfg.cookie)
 332                return -ENOENT;
 333
 334        ret = mwifiex_remain_on_chan_cfg(priv, HostCmd_ACT_GEN_REMOVE,
 335                                         &priv->roc_cfg.chan, 0);
 336
 337        if (!ret) {
 338                cfg80211_remain_on_channel_expired(wdev, cookie,
 339                                                   &priv->roc_cfg.chan,
 340                                                   GFP_ATOMIC);
 341
 342                memset(&priv->roc_cfg, 0, sizeof(struct mwifiex_roc_cfg));
 343
 344                mwifiex_dbg(priv->adapter, INFO,
 345                            "info: cancel ROC, cookie = 0x%llx\n", cookie);
 346        }
 347
 348        return ret;
 349}
 350
 351/*
 352 * CFG802.11 operation handler to set Tx power.
 353 */
 354static int
 355mwifiex_cfg80211_set_tx_power(struct wiphy *wiphy,
 356                              struct wireless_dev *wdev,
 357                              enum nl80211_tx_power_setting type,
 358                              int mbm)
 359{
 360        struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
 361        struct mwifiex_private *priv;
 362        struct mwifiex_power_cfg power_cfg;
 363        int dbm = MBM_TO_DBM(mbm);
 364
 365        if (type == NL80211_TX_POWER_FIXED) {
 366                power_cfg.is_power_auto = 0;
 367                power_cfg.power_level = dbm;
 368        } else {
 369                power_cfg.is_power_auto = 1;
 370        }
 371
 372        priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
 373
 374        return mwifiex_set_tx_power(priv, &power_cfg);
 375}
 376
 377/*
 378 * CFG802.11 operation handler to get Tx power.
 379 */
 380static int
 381mwifiex_cfg80211_get_tx_power(struct wiphy *wiphy,
 382                              struct wireless_dev *wdev,
 383                              int *dbm)
 384{
 385        struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
 386        struct mwifiex_private *priv = mwifiex_get_priv(adapter,
 387                                                        MWIFIEX_BSS_ROLE_ANY);
 388        int ret = mwifiex_send_cmd(priv, HostCmd_CMD_RF_TX_PWR,
 389                                   HostCmd_ACT_GEN_GET, 0, NULL, true);
 390
 391        if (ret < 0)
 392                return ret;
 393
 394        /* tx_power_level is set in HostCmd_CMD_RF_TX_PWR command handler */
 395        *dbm = priv->tx_power_level;
 396
 397        return 0;
 398}
 399
 400/*
 401 * CFG802.11 operation handler to set Power Save option.
 402 *
 403 * The timeout value, if provided, is currently ignored.
 404 */
 405static int
 406mwifiex_cfg80211_set_power_mgmt(struct wiphy *wiphy,
 407                                struct net_device *dev,
 408                                bool enabled, int timeout)
 409{
 410        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
 411        u32 ps_mode;
 412
 413        if (timeout)
 414                mwifiex_dbg(priv->adapter, INFO,
 415                            "info: ignore timeout value for IEEE Power Save\n");
 416
 417        ps_mode = enabled;
 418
 419        return mwifiex_drv_set_power(priv, &ps_mode);
 420}
 421
 422/*
 423 * CFG802.11 operation handler to set the default network key.
 424 */
 425static int
 426mwifiex_cfg80211_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
 427                                 u8 key_index, bool unicast,
 428                                 bool multicast)
 429{
 430        struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
 431
 432        /* Return if WEP key not configured */
 433        if (!priv->sec_info.wep_enabled)
 434                return 0;
 435
 436        if (priv->bss_type == MWIFIEX_BSS_TYPE_UAP) {
 437                priv->wep_key_curr_index = key_index;
 438        } else if (mwifiex_set_encode(priv, NULL, NULL, 0, key_index,
 439                                      NULL, 0)) {
 440                mwifiex_dbg(priv->adapter, ERROR, "set default Tx key index\n");
 441                return -EFAULT;
 442        }
 443
 444        return 0;
 445}
 446
 447/*
 448 * CFG802.11 operation handler to add a network key.
 449 */
 450static int
 451mwifiex_cfg80211_add_key(struct wiphy *wiphy, struct net_device *netdev,
 452                         u8 key_index, bool pairwise, const u8 *mac_addr,
 453                         struct key_params *params)
 454{
 455        struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
 456        struct mwifiex_wep_key *wep_key;
 457        const u8 bc_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
 458        const u8 *peer_mac = pairwise ? mac_addr : bc_mac;
 459
 460        if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP &&
 461            (params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
 462             params->cipher == WLAN_CIPHER_SUITE_WEP104)) {
 463                if (params->key && params->key_len) {
 464                        wep_key = &priv->wep_key[key_index];
 465                        memset(wep_key, 0, sizeof(struct mwifiex_wep_key));
 466                        memcpy(wep_key->key_material, params->key,
 467                               params->key_len);
 468                        wep_key->key_index = key_index;
 469                        wep_key->key_length = params->key_len;
 470                        priv->sec_info.wep_enabled = 1;
 471                }
 472                return 0;
 473        }
 474
 475        if (mwifiex_set_encode(priv, params, params->key, params->key_len,
 476                               key_index, peer_mac, 0)) {
 477                mwifiex_dbg(priv->adapter, ERROR, "crypto keys added\n");
 478                return -EFAULT;
 479        }
 480
 481        return 0;
 482}
 483
 484/*
 485 * CFG802.11 operation handler to set default mgmt key.
 486 */
 487static int
 488mwifiex_cfg80211_set_default_mgmt_key(struct wiphy *wiphy,
 489                                      struct net_device *netdev,
 490                                      u8 key_index)
 491{
 492        struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
 493        struct mwifiex_ds_encrypt_key encrypt_key;
 494
 495        wiphy_dbg(wiphy, "set default mgmt key, key index=%d\n", key_index);
 496
 497        memset(&encrypt_key, 0, sizeof(struct mwifiex_ds_encrypt_key));
 498        encrypt_key.key_len = WLAN_KEY_LEN_CCMP;
 499        encrypt_key.key_index = key_index;
 500        encrypt_key.is_igtk_def_key = true;
 501        eth_broadcast_addr(encrypt_key.mac_addr);
 502
 503        return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
 504                                HostCmd_ACT_GEN_SET, true, &encrypt_key, true);
 505}
 506
 507/*
 508 * This function sends domain information to the firmware.
 509 *
 510 * The following information are passed to the firmware -
 511 *      - Country codes
 512 *      - Sub bands (first channel, number of channels, maximum Tx power)
 513 */
 514int mwifiex_send_domain_info_cmd_fw(struct wiphy *wiphy)
 515{
 516        u8 no_of_triplet = 0;
 517        struct ieee80211_country_ie_triplet *t;
 518        u8 no_of_parsed_chan = 0;
 519        u8 first_chan = 0, next_chan = 0, max_pwr = 0;
 520        u8 i, flag = 0;
 521        enum nl80211_band band;
 522        struct ieee80211_supported_band *sband;
 523        struct ieee80211_channel *ch;
 524        struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
 525        struct mwifiex_private *priv;
 526        struct mwifiex_802_11d_domain_reg *domain_info = &adapter->domain_reg;
 527
 528        /* Set country code */
 529        domain_info->country_code[0] = adapter->country_code[0];
 530        domain_info->country_code[1] = adapter->country_code[1];
 531        domain_info->country_code[2] = ' ';
 532
 533        band = mwifiex_band_to_radio_type(adapter->config_bands);
 534        if (!wiphy->bands[band]) {
 535                mwifiex_dbg(adapter, ERROR,
 536                            "11D: setting domain info in FW\n");
 537                return -1;
 538        }
 539
 540        sband = wiphy->bands[band];
 541
 542        for (i = 0; i < sband->n_channels ; i++) {
 543                ch = &sband->channels[i];
 544                if (ch->flags & IEEE80211_CHAN_DISABLED)
 545                        continue;
 546
 547                if (!flag) {
 548                        flag = 1;
 549                        first_chan = (u32) ch->hw_value;
 550                        next_chan = first_chan;
 551                        max_pwr = ch->max_power;
 552                        no_of_parsed_chan = 1;
 553                        continue;
 554                }
 555
 556                if (ch->hw_value == next_chan + 1 &&
 557                    ch->max_power == max_pwr) {
 558                        next_chan++;
 559                        no_of_parsed_chan++;
 560                } else {
 561                        t = &domain_info->triplet[no_of_triplet];
 562                        t->chans.first_channel = first_chan;
 563                        t->chans.num_channels = no_of_parsed_chan;
 564                        t->chans.max_power = max_pwr;
 565                        no_of_triplet++;
 566                        first_chan = (u32) ch->hw_value;
 567                        next_chan = first_chan;
 568                        max_pwr = ch->max_power;
 569                        no_of_parsed_chan = 1;
 570                }
 571        }
 572
 573        if (flag) {
 574                t = &domain_info->triplet[no_of_triplet];
 575                t->chans.first_channel = first_chan;
 576                t->chans.num_channels = no_of_parsed_chan;
 577                t->chans.max_power = max_pwr;
 578                no_of_triplet++;
 579        }
 580
 581        domain_info->no_of_triplet = no_of_triplet;
 582
 583        priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
 584
 585        if (mwifiex_send_cmd(priv, HostCmd_CMD_802_11D_DOMAIN_INFO,
 586                             HostCmd_ACT_GEN_SET, 0, NULL, false)) {
 587                mwifiex_dbg(adapter, INFO,
 588                            "11D: setting domain info in FW\n");
 589                return -1;
 590        }
 591
 592        return 0;
 593}
 594
 595static void mwifiex_reg_apply_radar_flags(struct wiphy *wiphy)
 596{
 597        struct ieee80211_supported_band *sband;
 598        struct ieee80211_channel *chan;
 599        unsigned int i;
 600
 601        if (!wiphy->bands[NL80211_BAND_5GHZ])
 602                return;
 603        sband = wiphy->bands[NL80211_BAND_5GHZ];
 604
 605        for (i = 0; i < sband->n_channels; i++) {
 606                chan = &sband->channels[i];
 607                if ((!(chan->flags & IEEE80211_CHAN_DISABLED)) &&
 608                    (chan->flags & IEEE80211_CHAN_RADAR))
 609                        chan->flags |= IEEE80211_CHAN_NO_IR;
 610        }
 611}
 612
 613/*
 614 * CFG802.11 regulatory domain callback function.
 615 *
 616 * This function is called when the regulatory domain is changed due to the
 617 * following reasons -
 618 *      - Set by driver
 619 *      - Set by system core
 620 *      - Set by user
 621 *      - Set bt Country IE
 622 */
 623static void mwifiex_reg_notifier(struct wiphy *wiphy,
 624                                 struct regulatory_request *request)
 625{
 626        struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
 627        struct mwifiex_private *priv = mwifiex_get_priv(adapter,
 628                                                        MWIFIEX_BSS_ROLE_ANY);
 629        mwifiex_dbg(adapter, INFO,
 630                    "info: cfg80211 regulatory domain callback for %c%c\n",
 631                    request->alpha2[0], request->alpha2[1]);
 632        mwifiex_reg_apply_radar_flags(wiphy);
 633
 634        switch (request->initiator) {
 635        case NL80211_REGDOM_SET_BY_DRIVER:
 636        case NL80211_REGDOM_SET_BY_CORE:
 637        case NL80211_REGDOM_SET_BY_USER:
 638        case NL80211_REGDOM_SET_BY_COUNTRY_IE:
 639                break;
 640        default:
 641                mwifiex_dbg(adapter, ERROR,
 642                            "unknown regdom initiator: %d\n",
 643                            request->initiator);
 644                return;
 645        }
 646
 647        /* Don't send world or same regdom info to firmware */
 648        if (strncmp(request->alpha2, "00", 2) &&
 649            strncmp(request->alpha2, adapter->country_code,
 650                    sizeof(request->alpha2))) {
 651                memcpy(adapter->country_code, request->alpha2,
 652                       sizeof(request->alpha2));
 653                mwifiex_send_domain_info_cmd_fw(wiphy);
 654                mwifiex_dnld_txpwr_table(priv);
 655        }
 656}
 657
 658/*
 659 * This function sets the fragmentation threshold.
 660 *
 661 * The fragmentation threshold value must lie between MWIFIEX_FRAG_MIN_VALUE
 662 * and MWIFIEX_FRAG_MAX_VALUE.
 663 */
 664static int
 665mwifiex_set_frag(struct mwifiex_private *priv, u32 frag_thr)
 666{
 667        if (frag_thr < MWIFIEX_FRAG_MIN_VALUE ||
 668            frag_thr > MWIFIEX_FRAG_MAX_VALUE)
 669                frag_thr = MWIFIEX_FRAG_MAX_VALUE;
 670
 671        return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
 672                                HostCmd_ACT_GEN_SET, FRAG_THRESH_I,
 673                                &frag_thr, true);
 674}
 675
 676/*
 677 * This function sets the RTS threshold.
 678
 679 * The rts value must lie between MWIFIEX_RTS_MIN_VALUE
 680 * and MWIFIEX_RTS_MAX_VALUE.
 681 */
 682static int
 683mwifiex_set_rts(struct mwifiex_private *priv, u32 rts_thr)
 684{
 685        if (rts_thr < MWIFIEX_RTS_MIN_VALUE || rts_thr > MWIFIEX_RTS_MAX_VALUE)
 686                rts_thr = MWIFIEX_RTS_MAX_VALUE;
 687
 688        return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
 689                                HostCmd_ACT_GEN_SET, RTS_THRESH_I,
 690                                &rts_thr, true);
 691}
 692
 693/*
 694 * CFG802.11 operation handler to set wiphy parameters.
 695 *
 696 * This function can be used to set the RTS threshold and the
 697 * Fragmentation threshold of the driver.
 698 */
 699static int
 700mwifiex_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
 701{
 702        struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
 703        struct mwifiex_private *priv;
 704        struct mwifiex_uap_bss_param *bss_cfg;
 705        int ret;
 706
 707        priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
 708
 709        switch (priv->bss_role) {
 710        case MWIFIEX_BSS_ROLE_UAP:
 711                if (priv->bss_started) {
 712                        mwifiex_dbg(adapter, ERROR,
 713                                    "cannot change wiphy params when bss started");
 714                        return -EINVAL;
 715                }
 716
 717                bss_cfg = kzalloc(sizeof(*bss_cfg), GFP_KERNEL);
 718                if (!bss_cfg)
 719                        return -ENOMEM;
 720
 721                mwifiex_set_sys_config_invalid_data(bss_cfg);
 722
 723                if (changed & WIPHY_PARAM_RTS_THRESHOLD)
 724                        bss_cfg->rts_threshold = wiphy->rts_threshold;
 725                if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
 726                        bss_cfg->frag_threshold = wiphy->frag_threshold;
 727                if (changed & WIPHY_PARAM_RETRY_LONG)
 728                        bss_cfg->retry_limit = wiphy->retry_long;
 729
 730                ret = mwifiex_send_cmd(priv, HostCmd_CMD_UAP_SYS_CONFIG,
 731                                       HostCmd_ACT_GEN_SET,
 732                                       UAP_BSS_PARAMS_I, bss_cfg,
 733                                       false);
 734
 735                kfree(bss_cfg);
 736                if (ret) {
 737                        mwifiex_dbg(adapter, ERROR,
 738                                    "Failed to set wiphy phy params\n");
 739                        return ret;
 740                }
 741                break;
 742
 743        case MWIFIEX_BSS_ROLE_STA:
 744                if (priv->media_connected) {
 745                        mwifiex_dbg(adapter, ERROR,
 746                                    "cannot change wiphy params when connected");
 747                        return -EINVAL;
 748                }
 749                if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
 750                        ret = mwifiex_set_rts(priv,
 751                                              wiphy->rts_threshold);
 752                        if (ret)
 753                                return ret;
 754                }
 755                if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
 756                        ret = mwifiex_set_frag(priv,
 757                                               wiphy->frag_threshold);
 758                        if (ret)
 759                                return ret;
 760                }
 761                break;
 762        }
 763
 764        return 0;
 765}
 766
 767static int
 768mwifiex_cfg80211_deinit_p2p(struct mwifiex_private *priv)
 769{
 770        u16 mode = P2P_MODE_DISABLE;
 771
 772        if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
 773                             HostCmd_ACT_GEN_SET, 0, &mode, true))
 774                return -1;
 775
 776        return 0;
 777}
 778
 779/*
 780 * This function initializes the functionalities for P2P client.
 781 * The P2P client initialization sequence is:
 782 * disable -> device -> client
 783 */
 784static int
 785mwifiex_cfg80211_init_p2p_client(struct mwifiex_private *priv)
 786{
 787        u16 mode;
 788
 789        if (mwifiex_cfg80211_deinit_p2p(priv))
 790                return -1;
 791
 792        mode = P2P_MODE_DEVICE;
 793        if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
 794                             HostCmd_ACT_GEN_SET, 0, &mode, true))
 795                return -1;
 796
 797        mode = P2P_MODE_CLIENT;
 798        if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
 799                             HostCmd_ACT_GEN_SET, 0, &mode, true))
 800                return -1;
 801
 802        return 0;
 803}
 804
 805/*
 806 * This function initializes the functionalities for P2P GO.
 807 * The P2P GO initialization sequence is:
 808 * disable -> device -> GO
 809 */
 810static int
 811mwifiex_cfg80211_init_p2p_go(struct mwifiex_private *priv)
 812{
 813        u16 mode;
 814
 815        if (mwifiex_cfg80211_deinit_p2p(priv))
 816                return -1;
 817
 818        mode = P2P_MODE_DEVICE;
 819        if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
 820                             HostCmd_ACT_GEN_SET, 0, &mode, true))
 821                return -1;
 822
 823        mode = P2P_MODE_GO;
 824        if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
 825                             HostCmd_ACT_GEN_SET, 0, &mode, true))
 826                return -1;
 827
 828        return 0;
 829}
 830
 831static int mwifiex_deinit_priv_params(struct mwifiex_private *priv)
 832{
 833        struct mwifiex_adapter *adapter = priv->adapter;
 834        unsigned long flags;
 835
 836        priv->mgmt_frame_mask = 0;
 837        if (mwifiex_send_cmd(priv, HostCmd_CMD_MGMT_FRAME_REG,
 838                             HostCmd_ACT_GEN_SET, 0,
 839                             &priv->mgmt_frame_mask, false)) {
 840                mwifiex_dbg(adapter, ERROR,
 841                            "could not unregister mgmt frame rx\n");
 842                return -1;
 843        }
 844
 845        mwifiex_deauthenticate(priv, NULL);
 846
 847        spin_lock_irqsave(&adapter->main_proc_lock, flags);
 848        adapter->main_locked = true;
 849        if (adapter->mwifiex_processing) {
 850                spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
 851                flush_workqueue(adapter->workqueue);
 852        } else {
 853                spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
 854        }
 855
 856        spin_lock_irqsave(&adapter->rx_proc_lock, flags);
 857        adapter->rx_locked = true;
 858        if (adapter->rx_processing) {
 859                spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
 860                flush_workqueue(adapter->rx_workqueue);
 861        } else {
 862        spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
 863        }
 864
 865        mwifiex_free_priv(priv);
 866        priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
 867        priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
 868        priv->sec_info.authentication_mode = NL80211_AUTHTYPE_OPEN_SYSTEM;
 869
 870        return 0;
 871}
 872
 873static int
 874mwifiex_init_new_priv_params(struct mwifiex_private *priv,
 875                             struct net_device *dev,
 876                             enum nl80211_iftype type)
 877{
 878        struct mwifiex_adapter *adapter = priv->adapter;
 879        unsigned long flags;
 880
 881        mwifiex_init_priv(priv);
 882
 883        priv->bss_mode = type;
 884        priv->wdev.iftype = type;
 885
 886        mwifiex_init_priv_params(priv, priv->netdev);
 887        priv->bss_started = 0;
 888
 889        switch (type) {
 890        case NL80211_IFTYPE_STATION:
 891        case NL80211_IFTYPE_ADHOC:
 892                priv->bss_role =  MWIFIEX_BSS_ROLE_STA;
 893                break;
 894        case NL80211_IFTYPE_P2P_CLIENT:
 895                priv->bss_role =  MWIFIEX_BSS_ROLE_STA;
 896                break;
 897        case NL80211_IFTYPE_P2P_GO:
 898                priv->bss_role =  MWIFIEX_BSS_ROLE_UAP;
 899                break;
 900        case NL80211_IFTYPE_AP:
 901                priv->bss_role = MWIFIEX_BSS_ROLE_UAP;
 902                break;
 903        default:
 904                mwifiex_dbg(adapter, ERROR,
 905                            "%s: changing to %d not supported\n",
 906                            dev->name, type);
 907                return -EOPNOTSUPP;
 908        }
 909
 910        spin_lock_irqsave(&adapter->main_proc_lock, flags);
 911        adapter->main_locked = false;
 912        spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
 913
 914        spin_lock_irqsave(&adapter->rx_proc_lock, flags);
 915        adapter->rx_locked = false;
 916        spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
 917
 918        mwifiex_set_mac_address(priv, dev);
 919
 920        return 0;
 921}
 922
 923static int
 924mwifiex_change_vif_to_p2p(struct net_device *dev,
 925                          enum nl80211_iftype curr_iftype,
 926                          enum nl80211_iftype type,
 927                          struct vif_params *params)
 928{
 929        struct mwifiex_private *priv;
 930        struct mwifiex_adapter *adapter;
 931
 932        priv = mwifiex_netdev_get_priv(dev);
 933
 934        if (!priv)
 935                return -1;
 936
 937        adapter = priv->adapter;
 938
 939        if (adapter->curr_iface_comb.p2p_intf ==
 940            adapter->iface_limit.p2p_intf) {
 941                mwifiex_dbg(adapter, ERROR,
 942                            "cannot create multiple P2P ifaces\n");
 943                return -1;
 944        }
 945
 946        mwifiex_dbg(adapter, INFO,
 947                    "%s: changing role to p2p\n", dev->name);
 948
 949        if (mwifiex_deinit_priv_params(priv))
 950                return -1;
 951        if (mwifiex_init_new_priv_params(priv, dev, type))
 952                return -1;
 953
 954        switch (type) {
 955        case NL80211_IFTYPE_P2P_CLIENT:
 956                if (mwifiex_cfg80211_init_p2p_client(priv))
 957                        return -EFAULT;
 958                break;
 959        case NL80211_IFTYPE_P2P_GO:
 960                if (mwifiex_cfg80211_init_p2p_go(priv))
 961                        return -EFAULT;
 962                break;
 963        default:
 964                mwifiex_dbg(adapter, ERROR,
 965                            "%s: changing to %d not supported\n",
 966                            dev->name, type);
 967                return -EOPNOTSUPP;
 968        }
 969
 970        if (mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
 971                             HostCmd_ACT_GEN_SET, 0, NULL, true))
 972                return -1;
 973
 974        if (mwifiex_sta_init_cmd(priv, false, false))
 975                return -1;
 976
 977        switch (curr_iftype) {
 978        case NL80211_IFTYPE_STATION:
 979        case NL80211_IFTYPE_ADHOC:
 980                adapter->curr_iface_comb.sta_intf--;
 981                break;
 982        case NL80211_IFTYPE_AP:
 983                adapter->curr_iface_comb.uap_intf--;
 984                break;
 985        default:
 986                break;
 987        }
 988
 989        adapter->curr_iface_comb.p2p_intf++;
 990        dev->ieee80211_ptr->iftype = type;
 991
 992        return 0;
 993}
 994
 995static int
 996mwifiex_change_vif_to_sta_adhoc(struct net_device *dev,
 997                                enum nl80211_iftype curr_iftype,
 998                                enum nl80211_iftype type,
 999                                struct vif_params *params)
1000{
1001        struct mwifiex_private *priv;
1002        struct mwifiex_adapter *adapter;
1003
1004        priv = mwifiex_netdev_get_priv(dev);
1005
1006        if (!priv)
1007                return -1;
1008
1009        adapter = priv->adapter;
1010
1011        if ((curr_iftype != NL80211_IFTYPE_P2P_CLIENT &&
1012             curr_iftype != NL80211_IFTYPE_P2P_GO) &&
1013            (adapter->curr_iface_comb.sta_intf ==
1014             adapter->iface_limit.sta_intf)) {
1015                mwifiex_dbg(adapter, ERROR,
1016                            "cannot create multiple station/adhoc ifaces\n");
1017                return -1;
1018        }
1019
1020        if (type == NL80211_IFTYPE_STATION)
1021                mwifiex_dbg(adapter, INFO,
1022                            "%s: changing role to station\n", dev->name);
1023        else
1024                mwifiex_dbg(adapter, INFO,
1025                            "%s: changing role to adhoc\n", dev->name);
1026
1027        if (mwifiex_deinit_priv_params(priv))
1028                return -1;
1029        if (mwifiex_init_new_priv_params(priv, dev, type))
1030                return -1;
1031        if (mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
1032                             HostCmd_ACT_GEN_SET, 0, NULL, true))
1033                return -1;
1034        if (mwifiex_sta_init_cmd(priv, false, false))
1035                return -1;
1036
1037        switch (curr_iftype) {
1038        case NL80211_IFTYPE_P2P_CLIENT:
1039        case NL80211_IFTYPE_P2P_GO:
1040                adapter->curr_iface_comb.p2p_intf--;
1041                break;
1042        case NL80211_IFTYPE_AP:
1043                adapter->curr_iface_comb.uap_intf--;
1044                break;
1045        default:
1046                break;
1047        }
1048
1049        adapter->curr_iface_comb.sta_intf++;
1050        dev->ieee80211_ptr->iftype = type;
1051        return 0;
1052}
1053
1054static int
1055mwifiex_change_vif_to_ap(struct net_device *dev,
1056                         enum nl80211_iftype curr_iftype,
1057                         enum nl80211_iftype type,
1058                         struct vif_params *params)
1059{
1060        struct mwifiex_private *priv;
1061        struct mwifiex_adapter *adapter;
1062
1063        priv = mwifiex_netdev_get_priv(dev);
1064
1065        if (!priv)
1066                return -1;
1067
1068        adapter = priv->adapter;
1069
1070        if (adapter->curr_iface_comb.uap_intf ==
1071            adapter->iface_limit.uap_intf) {
1072                mwifiex_dbg(adapter, ERROR,
1073                            "cannot create multiple AP ifaces\n");
1074                return -1;
1075        }
1076
1077        mwifiex_dbg(adapter, INFO,
1078                    "%s: changing role to AP\n", dev->name);
1079
1080        if (mwifiex_deinit_priv_params(priv))
1081                return -1;
1082        if (mwifiex_init_new_priv_params(priv, dev, type))
1083                return -1;
1084        if (mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
1085                             HostCmd_ACT_GEN_SET, 0, NULL, true))
1086                return -1;
1087        if (mwifiex_sta_init_cmd(priv, false, false))
1088                return -1;
1089
1090        switch (curr_iftype) {
1091        case NL80211_IFTYPE_P2P_CLIENT:
1092        case NL80211_IFTYPE_P2P_GO:
1093                adapter->curr_iface_comb.p2p_intf--;
1094                break;
1095        case NL80211_IFTYPE_STATION:
1096        case NL80211_IFTYPE_ADHOC:
1097                adapter->curr_iface_comb.sta_intf--;
1098                break;
1099        default:
1100                break;
1101        }
1102
1103        adapter->curr_iface_comb.uap_intf++;
1104        dev->ieee80211_ptr->iftype = type;
1105        return 0;
1106}
1107/*
1108 * CFG802.11 operation handler to change interface type.
1109 */
1110static int
1111mwifiex_cfg80211_change_virtual_intf(struct wiphy *wiphy,
1112                                     struct net_device *dev,
1113                                     enum nl80211_iftype type,
1114                                     struct vif_params *params)
1115{
1116        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1117        enum nl80211_iftype curr_iftype = dev->ieee80211_ptr->iftype;
1118
1119        switch (curr_iftype) {
1120        case NL80211_IFTYPE_ADHOC:
1121                switch (type) {
1122                case NL80211_IFTYPE_STATION:
1123                        priv->bss_mode = type;
1124                        priv->sec_info.authentication_mode =
1125                                                   NL80211_AUTHTYPE_OPEN_SYSTEM;
1126                        dev->ieee80211_ptr->iftype = type;
1127                        mwifiex_deauthenticate(priv, NULL);
1128                        return mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
1129                                                HostCmd_ACT_GEN_SET, 0, NULL,
1130                                                true);
1131                case NL80211_IFTYPE_P2P_CLIENT:
1132                case NL80211_IFTYPE_P2P_GO:
1133                        return mwifiex_change_vif_to_p2p(dev, curr_iftype,
1134                                                         type, params);
1135                case NL80211_IFTYPE_AP:
1136                        return mwifiex_change_vif_to_ap(dev, curr_iftype, type,
1137                                                        params);
1138                case NL80211_IFTYPE_UNSPECIFIED:
1139                        mwifiex_dbg(priv->adapter, INFO,
1140                                    "%s: kept type as IBSS\n", dev->name);
1141                case NL80211_IFTYPE_ADHOC:      /* This shouldn't happen */
1142                        return 0;
1143                default:
1144                        mwifiex_dbg(priv->adapter, ERROR,
1145                                    "%s: changing to %d not supported\n",
1146                                    dev->name, type);
1147                        return -EOPNOTSUPP;
1148                }
1149                break;
1150        case NL80211_IFTYPE_STATION:
1151                switch (type) {
1152                case NL80211_IFTYPE_ADHOC:
1153                        priv->bss_mode = type;
1154                        priv->sec_info.authentication_mode =
1155                                                   NL80211_AUTHTYPE_OPEN_SYSTEM;
1156                        dev->ieee80211_ptr->iftype = type;
1157                        mwifiex_deauthenticate(priv, NULL);
1158                        return mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
1159                                                HostCmd_ACT_GEN_SET, 0, NULL,
1160                                                true);
1161                case NL80211_IFTYPE_P2P_CLIENT:
1162                case NL80211_IFTYPE_P2P_GO:
1163                        return mwifiex_change_vif_to_p2p(dev, curr_iftype,
1164                                                         type, params);
1165                case NL80211_IFTYPE_AP:
1166                        return mwifiex_change_vif_to_ap(dev, curr_iftype, type,
1167                                                        params);
1168                case NL80211_IFTYPE_UNSPECIFIED:
1169                        mwifiex_dbg(priv->adapter, INFO,
1170                                    "%s: kept type as STA\n", dev->name);
1171                case NL80211_IFTYPE_STATION:    /* This shouldn't happen */
1172                        return 0;
1173                default:
1174                        mwifiex_dbg(priv->adapter, ERROR,
1175                                    "%s: changing to %d not supported\n",
1176                                    dev->name, type);
1177                        return -EOPNOTSUPP;
1178                }
1179                break;
1180        case NL80211_IFTYPE_AP:
1181                switch (type) {
1182                case NL80211_IFTYPE_ADHOC:
1183                case NL80211_IFTYPE_STATION:
1184                        return mwifiex_change_vif_to_sta_adhoc(dev, curr_iftype,
1185                                                               type, params);
1186                        break;
1187                case NL80211_IFTYPE_P2P_CLIENT:
1188                case NL80211_IFTYPE_P2P_GO:
1189                        return mwifiex_change_vif_to_p2p(dev, curr_iftype,
1190                                                         type, params);
1191                case NL80211_IFTYPE_UNSPECIFIED:
1192                        mwifiex_dbg(priv->adapter, INFO,
1193                                    "%s: kept type as AP\n", dev->name);
1194                case NL80211_IFTYPE_AP:         /* This shouldn't happen */
1195                        return 0;
1196                default:
1197                        mwifiex_dbg(priv->adapter, ERROR,
1198                                    "%s: changing to %d not supported\n",
1199                                    dev->name, type);
1200                        return -EOPNOTSUPP;
1201                }
1202                break;
1203        case NL80211_IFTYPE_P2P_CLIENT:
1204        case NL80211_IFTYPE_P2P_GO:
1205                switch (type) {
1206                case NL80211_IFTYPE_STATION:
1207                        if (mwifiex_cfg80211_deinit_p2p(priv))
1208                                return -EFAULT;
1209                        priv->adapter->curr_iface_comb.p2p_intf--;
1210                        priv->adapter->curr_iface_comb.sta_intf++;
1211                        dev->ieee80211_ptr->iftype = type;
1212                        if (mwifiex_deinit_priv_params(priv))
1213                                return -1;
1214                        if (mwifiex_init_new_priv_params(priv, dev, type))
1215                                return -1;
1216                        if (mwifiex_sta_init_cmd(priv, false, false))
1217                                return -1;
1218                        break;
1219                case NL80211_IFTYPE_ADHOC:
1220                        if (mwifiex_cfg80211_deinit_p2p(priv))
1221                                return -EFAULT;
1222                        return mwifiex_change_vif_to_sta_adhoc(dev, curr_iftype,
1223                                                               type, params);
1224                        break;
1225                case NL80211_IFTYPE_AP:
1226                        if (mwifiex_cfg80211_deinit_p2p(priv))
1227                                return -EFAULT;
1228                        return mwifiex_change_vif_to_ap(dev, curr_iftype, type,
1229                                                        params);
1230                case NL80211_IFTYPE_UNSPECIFIED:
1231                        mwifiex_dbg(priv->adapter, INFO,
1232                                    "%s: kept type as P2P\n", dev->name);
1233                case NL80211_IFTYPE_P2P_CLIENT:
1234                case NL80211_IFTYPE_P2P_GO:
1235                        return 0;
1236                default:
1237                        mwifiex_dbg(priv->adapter, ERROR,
1238                                    "%s: changing to %d not supported\n",
1239                                    dev->name, type);
1240                        return -EOPNOTSUPP;
1241                }
1242                break;
1243        default:
1244                mwifiex_dbg(priv->adapter, ERROR,
1245                            "%s: unknown iftype: %d\n",
1246                            dev->name, dev->ieee80211_ptr->iftype);
1247                return -EOPNOTSUPP;
1248        }
1249
1250
1251        return 0;
1252}
1253
1254static void
1255mwifiex_parse_htinfo(struct mwifiex_private *priv, u8 tx_htinfo,
1256                     struct rate_info *rate)
1257{
1258        struct mwifiex_adapter *adapter = priv->adapter;
1259
1260        if (adapter->is_hw_11ac_capable) {
1261                /* bit[1-0]: 00=LG 01=HT 10=VHT */
1262                if (tx_htinfo & BIT(0)) {
1263                        /* HT */
1264                        rate->mcs = priv->tx_rate;
1265                        rate->flags |= RATE_INFO_FLAGS_MCS;
1266                }
1267                if (tx_htinfo & BIT(1)) {
1268                        /* VHT */
1269                        rate->mcs = priv->tx_rate & 0x0F;
1270                        rate->flags |= RATE_INFO_FLAGS_VHT_MCS;
1271                }
1272
1273                if (tx_htinfo & (BIT(1) | BIT(0))) {
1274                        /* HT or VHT */
1275                        switch (tx_htinfo & (BIT(3) | BIT(2))) {
1276                        case 0:
1277                                rate->bw = RATE_INFO_BW_20;
1278                                break;
1279                        case (BIT(2)):
1280                                rate->bw = RATE_INFO_BW_40;
1281                                break;
1282                        case (BIT(3)):
1283                                rate->bw = RATE_INFO_BW_80;
1284                                break;
1285                        case (BIT(3) | BIT(2)):
1286                                rate->bw = RATE_INFO_BW_160;
1287                                break;
1288                        }
1289
1290                        if (tx_htinfo & BIT(4))
1291                                rate->flags |= RATE_INFO_FLAGS_SHORT_GI;
1292
1293                        if ((priv->tx_rate >> 4) == 1)
1294                                rate->nss = 2;
1295                        else
1296                                rate->nss = 1;
1297                }
1298        } else {
1299                /*
1300                 * Bit 0 in tx_htinfo indicates that current Tx rate
1301                 * is 11n rate. Valid MCS index values for us are 0 to 15.
1302                 */
1303                if ((tx_htinfo & BIT(0)) && (priv->tx_rate < 16)) {
1304                        rate->mcs = priv->tx_rate;
1305                        rate->flags |= RATE_INFO_FLAGS_MCS;
1306                        rate->bw = RATE_INFO_BW_20;
1307                        if (tx_htinfo & BIT(1))
1308                                rate->bw = RATE_INFO_BW_40;
1309                        if (tx_htinfo & BIT(2))
1310                                rate->flags |= RATE_INFO_FLAGS_SHORT_GI;
1311                }
1312        }
1313}
1314
1315/*
1316 * This function dumps the station information on a buffer.
1317 *
1318 * The following information are shown -
1319 *      - Total bytes transmitted
1320 *      - Total bytes received
1321 *      - Total packets transmitted
1322 *      - Total packets received
1323 *      - Signal quality level
1324 *      - Transmission rate
1325 */
1326static int
1327mwifiex_dump_station_info(struct mwifiex_private *priv,
1328                          struct mwifiex_sta_node *node,
1329                          struct station_info *sinfo)
1330{
1331        u32 rate;
1332
1333        sinfo->filled = BIT(NL80211_STA_INFO_RX_BYTES) | BIT(NL80211_STA_INFO_TX_BYTES) |
1334                        BIT(NL80211_STA_INFO_RX_PACKETS) | BIT(NL80211_STA_INFO_TX_PACKETS) |
1335                        BIT(NL80211_STA_INFO_TX_BITRATE) |
1336                        BIT(NL80211_STA_INFO_SIGNAL) | BIT(NL80211_STA_INFO_SIGNAL_AVG);
1337
1338        if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1339                if (!node)
1340                        return -ENOENT;
1341
1342                sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1343                                BIT(NL80211_STA_INFO_TX_FAILED);
1344                sinfo->inactive_time =
1345                        jiffies_to_msecs(jiffies - node->stats.last_rx);
1346
1347                sinfo->signal = node->stats.rssi;
1348                sinfo->signal_avg = node->stats.rssi;
1349                sinfo->rx_bytes = node->stats.rx_bytes;
1350                sinfo->tx_bytes = node->stats.tx_bytes;
1351                sinfo->rx_packets = node->stats.rx_packets;
1352                sinfo->tx_packets = node->stats.tx_packets;
1353                sinfo->tx_failed = node->stats.tx_failed;
1354
1355                mwifiex_parse_htinfo(priv, node->stats.last_tx_htinfo,
1356                                     &sinfo->txrate);
1357                sinfo->txrate.legacy = node->stats.last_tx_rate * 5;
1358
1359                return 0;
1360        }
1361
1362        /* Get signal information from the firmware */
1363        if (mwifiex_send_cmd(priv, HostCmd_CMD_RSSI_INFO,
1364                             HostCmd_ACT_GEN_GET, 0, NULL, true)) {
1365                mwifiex_dbg(priv->adapter, ERROR,
1366                            "failed to get signal information\n");
1367                return -EFAULT;
1368        }
1369
1370        if (mwifiex_drv_get_data_rate(priv, &rate)) {
1371                mwifiex_dbg(priv->adapter, ERROR,
1372                            "getting data rate error\n");
1373                return -EFAULT;
1374        }
1375
1376        /* Get DTIM period information from firmware */
1377        mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
1378                         HostCmd_ACT_GEN_GET, DTIM_PERIOD_I,
1379                         &priv->dtim_period, true);
1380
1381        mwifiex_parse_htinfo(priv, priv->tx_htinfo, &sinfo->txrate);
1382
1383        sinfo->signal_avg = priv->bcn_rssi_avg;
1384        sinfo->rx_bytes = priv->stats.rx_bytes;
1385        sinfo->tx_bytes = priv->stats.tx_bytes;
1386        sinfo->rx_packets = priv->stats.rx_packets;
1387        sinfo->tx_packets = priv->stats.tx_packets;
1388        sinfo->signal = priv->bcn_rssi_avg;
1389        /* bit rate is in 500 kb/s units. Convert it to 100kb/s units */
1390        sinfo->txrate.legacy = rate * 5;
1391
1392        if (priv->bss_mode == NL80211_IFTYPE_STATION) {
1393                sinfo->filled |= BIT(NL80211_STA_INFO_BSS_PARAM);
1394                sinfo->bss_param.flags = 0;
1395                if (priv->curr_bss_params.bss_descriptor.cap_info_bitmap &
1396                                                WLAN_CAPABILITY_SHORT_PREAMBLE)
1397                        sinfo->bss_param.flags |=
1398                                        BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1399                if (priv->curr_bss_params.bss_descriptor.cap_info_bitmap &
1400                                                WLAN_CAPABILITY_SHORT_SLOT_TIME)
1401                        sinfo->bss_param.flags |=
1402                                        BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
1403                sinfo->bss_param.dtim_period = priv->dtim_period;
1404                sinfo->bss_param.beacon_interval =
1405                        priv->curr_bss_params.bss_descriptor.beacon_period;
1406        }
1407
1408        return 0;
1409}
1410
1411/*
1412 * CFG802.11 operation handler to get station information.
1413 *
1414 * This function only works in connected mode, and dumps the
1415 * requested station information, if available.
1416 */
1417static int
1418mwifiex_cfg80211_get_station(struct wiphy *wiphy, struct net_device *dev,
1419                             const u8 *mac, struct station_info *sinfo)
1420{
1421        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1422
1423        if (!priv->media_connected)
1424                return -ENOENT;
1425        if (memcmp(mac, priv->cfg_bssid, ETH_ALEN))
1426                return -ENOENT;
1427
1428        return mwifiex_dump_station_info(priv, NULL, sinfo);
1429}
1430
1431/*
1432 * CFG802.11 operation handler to dump station information.
1433 */
1434static int
1435mwifiex_cfg80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
1436                              int idx, u8 *mac, struct station_info *sinfo)
1437{
1438        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1439        static struct mwifiex_sta_node *node;
1440
1441        if ((GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) &&
1442            priv->media_connected && idx == 0) {
1443                ether_addr_copy(mac, priv->cfg_bssid);
1444                return mwifiex_dump_station_info(priv, NULL, sinfo);
1445        } else if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1446                mwifiex_send_cmd(priv, HOST_CMD_APCMD_STA_LIST,
1447                                 HostCmd_ACT_GEN_GET, 0, NULL, true);
1448
1449                if (node && (&node->list == &priv->sta_list)) {
1450                        node = NULL;
1451                        return -ENOENT;
1452                }
1453
1454                node = list_prepare_entry(node, &priv->sta_list, list);
1455                list_for_each_entry_continue(node, &priv->sta_list, list) {
1456                        ether_addr_copy(mac, node->mac_addr);
1457                        return mwifiex_dump_station_info(priv, node, sinfo);
1458                }
1459        }
1460
1461        return -ENOENT;
1462}
1463
1464static int
1465mwifiex_cfg80211_dump_survey(struct wiphy *wiphy, struct net_device *dev,
1466                             int idx, struct survey_info *survey)
1467{
1468        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1469        struct mwifiex_chan_stats *pchan_stats = priv->adapter->chan_stats;
1470        enum nl80211_band band;
1471
1472        mwifiex_dbg(priv->adapter, DUMP, "dump_survey idx=%d\n", idx);
1473
1474        memset(survey, 0, sizeof(struct survey_info));
1475
1476        if ((GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) &&
1477            priv->media_connected && idx == 0) {
1478                        u8 curr_bss_band = priv->curr_bss_params.band;
1479                        u32 chan = priv->curr_bss_params.bss_descriptor.channel;
1480
1481                        band = mwifiex_band_to_radio_type(curr_bss_band);
1482                        survey->channel = ieee80211_get_channel(wiphy,
1483                                ieee80211_channel_to_frequency(chan, band));
1484
1485                        if (priv->bcn_nf_last) {
1486                                survey->filled = SURVEY_INFO_NOISE_DBM;
1487                                survey->noise = priv->bcn_nf_last;
1488                        }
1489                        return 0;
1490        }
1491
1492        if (idx >= priv->adapter->num_in_chan_stats)
1493                return -ENOENT;
1494
1495        if (!pchan_stats[idx].cca_scan_dur)
1496                return 0;
1497
1498        band = pchan_stats[idx].bandcfg;
1499        survey->channel = ieee80211_get_channel(wiphy,
1500            ieee80211_channel_to_frequency(pchan_stats[idx].chan_num, band));
1501        survey->filled = SURVEY_INFO_NOISE_DBM |
1502                         SURVEY_INFO_TIME |
1503                         SURVEY_INFO_TIME_BUSY;
1504        survey->noise = pchan_stats[idx].noise;
1505        survey->time = pchan_stats[idx].cca_scan_dur;
1506        survey->time_busy = pchan_stats[idx].cca_busy_dur;
1507
1508        return 0;
1509}
1510
1511/* Supported rates to be advertised to the cfg80211 */
1512static struct ieee80211_rate mwifiex_rates[] = {
1513        {.bitrate = 10, .hw_value = 2, },
1514        {.bitrate = 20, .hw_value = 4, },
1515        {.bitrate = 55, .hw_value = 11, },
1516        {.bitrate = 110, .hw_value = 22, },
1517        {.bitrate = 60, .hw_value = 12, },
1518        {.bitrate = 90, .hw_value = 18, },
1519        {.bitrate = 120, .hw_value = 24, },
1520        {.bitrate = 180, .hw_value = 36, },
1521        {.bitrate = 240, .hw_value = 48, },
1522        {.bitrate = 360, .hw_value = 72, },
1523        {.bitrate = 480, .hw_value = 96, },
1524        {.bitrate = 540, .hw_value = 108, },
1525};
1526
1527/* Channel definitions to be advertised to cfg80211 */
1528static struct ieee80211_channel mwifiex_channels_2ghz[] = {
1529        {.center_freq = 2412, .hw_value = 1, },
1530        {.center_freq = 2417, .hw_value = 2, },
1531        {.center_freq = 2422, .hw_value = 3, },
1532        {.center_freq = 2427, .hw_value = 4, },
1533        {.center_freq = 2432, .hw_value = 5, },
1534        {.center_freq = 2437, .hw_value = 6, },
1535        {.center_freq = 2442, .hw_value = 7, },
1536        {.center_freq = 2447, .hw_value = 8, },
1537        {.center_freq = 2452, .hw_value = 9, },
1538        {.center_freq = 2457, .hw_value = 10, },
1539        {.center_freq = 2462, .hw_value = 11, },
1540        {.center_freq = 2467, .hw_value = 12, },
1541        {.center_freq = 2472, .hw_value = 13, },
1542        {.center_freq = 2484, .hw_value = 14, },
1543};
1544
1545static struct ieee80211_supported_band mwifiex_band_2ghz = {
1546        .channels = mwifiex_channels_2ghz,
1547        .n_channels = ARRAY_SIZE(mwifiex_channels_2ghz),
1548        .bitrates = mwifiex_rates,
1549        .n_bitrates = ARRAY_SIZE(mwifiex_rates),
1550};
1551
1552static struct ieee80211_channel mwifiex_channels_5ghz[] = {
1553        {.center_freq = 5040, .hw_value = 8, },
1554        {.center_freq = 5060, .hw_value = 12, },
1555        {.center_freq = 5080, .hw_value = 16, },
1556        {.center_freq = 5170, .hw_value = 34, },
1557        {.center_freq = 5190, .hw_value = 38, },
1558        {.center_freq = 5210, .hw_value = 42, },
1559        {.center_freq = 5230, .hw_value = 46, },
1560        {.center_freq = 5180, .hw_value = 36, },
1561        {.center_freq = 5200, .hw_value = 40, },
1562        {.center_freq = 5220, .hw_value = 44, },
1563        {.center_freq = 5240, .hw_value = 48, },
1564        {.center_freq = 5260, .hw_value = 52, },
1565        {.center_freq = 5280, .hw_value = 56, },
1566        {.center_freq = 5300, .hw_value = 60, },
1567        {.center_freq = 5320, .hw_value = 64, },
1568        {.center_freq = 5500, .hw_value = 100, },
1569        {.center_freq = 5520, .hw_value = 104, },
1570        {.center_freq = 5540, .hw_value = 108, },
1571        {.center_freq = 5560, .hw_value = 112, },
1572        {.center_freq = 5580, .hw_value = 116, },
1573        {.center_freq = 5600, .hw_value = 120, },
1574        {.center_freq = 5620, .hw_value = 124, },
1575        {.center_freq = 5640, .hw_value = 128, },
1576        {.center_freq = 5660, .hw_value = 132, },
1577        {.center_freq = 5680, .hw_value = 136, },
1578        {.center_freq = 5700, .hw_value = 140, },
1579        {.center_freq = 5745, .hw_value = 149, },
1580        {.center_freq = 5765, .hw_value = 153, },
1581        {.center_freq = 5785, .hw_value = 157, },
1582        {.center_freq = 5805, .hw_value = 161, },
1583        {.center_freq = 5825, .hw_value = 165, },
1584};
1585
1586static struct ieee80211_supported_band mwifiex_band_5ghz = {
1587        .channels = mwifiex_channels_5ghz,
1588        .n_channels = ARRAY_SIZE(mwifiex_channels_5ghz),
1589        .bitrates = mwifiex_rates + 4,
1590        .n_bitrates = ARRAY_SIZE(mwifiex_rates) - 4,
1591};
1592
1593
1594/* Supported crypto cipher suits to be advertised to cfg80211 */
1595static const u32 mwifiex_cipher_suites[] = {
1596        WLAN_CIPHER_SUITE_WEP40,
1597        WLAN_CIPHER_SUITE_WEP104,
1598        WLAN_CIPHER_SUITE_TKIP,
1599        WLAN_CIPHER_SUITE_CCMP,
1600        WLAN_CIPHER_SUITE_SMS4,
1601        WLAN_CIPHER_SUITE_AES_CMAC,
1602};
1603
1604/* Supported mgmt frame types to be advertised to cfg80211 */
1605static const struct ieee80211_txrx_stypes
1606mwifiex_mgmt_stypes[NUM_NL80211_IFTYPES] = {
1607        [NL80211_IFTYPE_STATION] = {
1608                .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1609                      BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1610                .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1611                      BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1612        },
1613        [NL80211_IFTYPE_AP] = {
1614                .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1615                      BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1616                .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1617                      BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1618        },
1619        [NL80211_IFTYPE_P2P_CLIENT] = {
1620                .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1621                      BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1622                .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1623                      BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1624        },
1625        [NL80211_IFTYPE_P2P_GO] = {
1626                .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1627                      BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1628                .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1629                      BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1630        },
1631};
1632
1633/*
1634 * CFG802.11 operation handler for setting bit rates.
1635 *
1636 * Function configures data rates to firmware using bitrate mask
1637 * provided by cfg80211.
1638 */
1639static int mwifiex_cfg80211_set_bitrate_mask(struct wiphy *wiphy,
1640                                struct net_device *dev,
1641                                const u8 *peer,
1642                                const struct cfg80211_bitrate_mask *mask)
1643{
1644        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1645        u16 bitmap_rates[MAX_BITMAP_RATES_SIZE];
1646        enum nl80211_band band;
1647        struct mwifiex_adapter *adapter = priv->adapter;
1648
1649        if (!priv->media_connected) {
1650                mwifiex_dbg(adapter, ERROR,
1651                            "Can not set Tx data rate in disconnected state\n");
1652                return -EINVAL;
1653        }
1654
1655        band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
1656
1657        memset(bitmap_rates, 0, sizeof(bitmap_rates));
1658
1659        /* Fill HR/DSSS rates. */
1660        if (band == NL80211_BAND_2GHZ)
1661                bitmap_rates[0] = mask->control[band].legacy & 0x000f;
1662
1663        /* Fill OFDM rates */
1664        if (band == NL80211_BAND_2GHZ)
1665                bitmap_rates[1] = (mask->control[band].legacy & 0x0ff0) >> 4;
1666        else
1667                bitmap_rates[1] = mask->control[band].legacy;
1668
1669        /* Fill HT MCS rates */
1670        bitmap_rates[2] = mask->control[band].ht_mcs[0];
1671        if (adapter->hw_dev_mcs_support == HT_STREAM_2X2)
1672                bitmap_rates[2] |= mask->control[band].ht_mcs[1] << 8;
1673
1674       /* Fill VHT MCS rates */
1675        if (adapter->fw_api_ver == MWIFIEX_FW_V15) {
1676                bitmap_rates[10] = mask->control[band].vht_mcs[0];
1677                if (adapter->hw_dev_mcs_support == HT_STREAM_2X2)
1678                        bitmap_rates[11] = mask->control[band].vht_mcs[1];
1679        }
1680
1681        return mwifiex_send_cmd(priv, HostCmd_CMD_TX_RATE_CFG,
1682                                HostCmd_ACT_GEN_SET, 0, bitmap_rates, true);
1683}
1684
1685/*
1686 * CFG802.11 operation handler for connection quality monitoring.
1687 *
1688 * This function subscribes/unsubscribes HIGH_RSSI and LOW_RSSI
1689 * events to FW.
1690 */
1691static int mwifiex_cfg80211_set_cqm_rssi_config(struct wiphy *wiphy,
1692                                                struct net_device *dev,
1693                                                s32 rssi_thold, u32 rssi_hyst)
1694{
1695        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1696        struct mwifiex_ds_misc_subsc_evt subsc_evt;
1697
1698        priv->cqm_rssi_thold = rssi_thold;
1699        priv->cqm_rssi_hyst = rssi_hyst;
1700
1701        memset(&subsc_evt, 0x00, sizeof(struct mwifiex_ds_misc_subsc_evt));
1702        subsc_evt.events = BITMASK_BCN_RSSI_LOW | BITMASK_BCN_RSSI_HIGH;
1703
1704        /* Subscribe/unsubscribe low and high rssi events */
1705        if (rssi_thold && rssi_hyst) {
1706                subsc_evt.action = HostCmd_ACT_BITWISE_SET;
1707                subsc_evt.bcn_l_rssi_cfg.abs_value = abs(rssi_thold);
1708                subsc_evt.bcn_h_rssi_cfg.abs_value = abs(rssi_thold);
1709                subsc_evt.bcn_l_rssi_cfg.evt_freq = 1;
1710                subsc_evt.bcn_h_rssi_cfg.evt_freq = 1;
1711                return mwifiex_send_cmd(priv,
1712                                        HostCmd_CMD_802_11_SUBSCRIBE_EVENT,
1713                                        0, 0, &subsc_evt, true);
1714        } else {
1715                subsc_evt.action = HostCmd_ACT_BITWISE_CLR;
1716                return mwifiex_send_cmd(priv,
1717                                        HostCmd_CMD_802_11_SUBSCRIBE_EVENT,
1718                                        0, 0, &subsc_evt, true);
1719        }
1720
1721        return 0;
1722}
1723
1724/* cfg80211 operation handler for change_beacon.
1725 * Function retrieves and sets modified management IEs to FW.
1726 */
1727static int mwifiex_cfg80211_change_beacon(struct wiphy *wiphy,
1728                                          struct net_device *dev,
1729                                          struct cfg80211_beacon_data *data)
1730{
1731        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1732        struct mwifiex_adapter *adapter = priv->adapter;
1733
1734        mwifiex_cancel_scan(adapter);
1735
1736        if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_UAP) {
1737                mwifiex_dbg(priv->adapter, ERROR,
1738                            "%s: bss_type mismatched\n", __func__);
1739                return -EINVAL;
1740        }
1741
1742        if (!priv->bss_started) {
1743                mwifiex_dbg(priv->adapter, ERROR,
1744                            "%s: bss not started\n", __func__);
1745                return -EINVAL;
1746        }
1747
1748        if (mwifiex_set_mgmt_ies(priv, data)) {
1749                mwifiex_dbg(priv->adapter, ERROR,
1750                            "%s: setting mgmt ies failed\n", __func__);
1751                return -EFAULT;
1752        }
1753
1754        return 0;
1755}
1756
1757/* cfg80211 operation handler for del_station.
1758 * Function deauthenticates station which value is provided in mac parameter.
1759 * If mac is NULL/broadcast, all stations in associated station list are
1760 * deauthenticated. If bss is not started or there are no stations in
1761 * associated stations list, no action is taken.
1762 */
1763static int
1764mwifiex_cfg80211_del_station(struct wiphy *wiphy, struct net_device *dev,
1765                             struct station_del_parameters *params)
1766{
1767        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1768        struct mwifiex_sta_node *sta_node;
1769        u8 deauth_mac[ETH_ALEN];
1770        unsigned long flags;
1771
1772        if (!priv->bss_started && priv->wdev.cac_started) {
1773                mwifiex_dbg(priv->adapter, INFO, "%s: abort CAC!\n", __func__);
1774                mwifiex_abort_cac(priv);
1775        }
1776
1777        if (list_empty(&priv->sta_list) || !priv->bss_started)
1778                return 0;
1779
1780        if (!params->mac || is_broadcast_ether_addr(params->mac))
1781                return 0;
1782
1783        mwifiex_dbg(priv->adapter, INFO, "%s: mac address %pM\n",
1784                    __func__, params->mac);
1785
1786        eth_zero_addr(deauth_mac);
1787
1788        spin_lock_irqsave(&priv->sta_list_spinlock, flags);
1789        sta_node = mwifiex_get_sta_entry(priv, params->mac);
1790        if (sta_node)
1791                ether_addr_copy(deauth_mac, params->mac);
1792        spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
1793
1794        if (is_valid_ether_addr(deauth_mac)) {
1795                if (mwifiex_send_cmd(priv, HostCmd_CMD_UAP_STA_DEAUTH,
1796                                     HostCmd_ACT_GEN_SET, 0,
1797                                     deauth_mac, true))
1798                        return -1;
1799        }
1800
1801        return 0;
1802}
1803
1804static int
1805mwifiex_cfg80211_set_antenna(struct wiphy *wiphy, u32 tx_ant, u32 rx_ant)
1806{
1807        struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
1808        struct mwifiex_private *priv = mwifiex_get_priv(adapter,
1809                                                        MWIFIEX_BSS_ROLE_ANY);
1810        struct mwifiex_ds_ant_cfg ant_cfg;
1811
1812        if (!tx_ant || !rx_ant)
1813                return -EOPNOTSUPP;
1814
1815        if (adapter->hw_dev_mcs_support != HT_STREAM_2X2) {
1816                /* Not a MIMO chip. User should provide specific antenna number
1817                 * for Tx/Rx path or enable all antennas for diversity
1818                 */
1819                if (tx_ant != rx_ant)
1820                        return -EOPNOTSUPP;
1821
1822                if ((tx_ant & (tx_ant - 1)) &&
1823                    (tx_ant != BIT(adapter->number_of_antenna) - 1))
1824                        return -EOPNOTSUPP;
1825
1826                if ((tx_ant == BIT(adapter->number_of_antenna) - 1) &&
1827                    (priv->adapter->number_of_antenna > 1)) {
1828                        tx_ant = RF_ANTENNA_AUTO;
1829                        rx_ant = RF_ANTENNA_AUTO;
1830                }
1831        } else {
1832                struct ieee80211_sta_ht_cap *ht_info;
1833                int rx_mcs_supp;
1834                enum nl80211_band band;
1835
1836                if ((tx_ant == 0x1 && rx_ant == 0x1)) {
1837                        adapter->user_dev_mcs_support = HT_STREAM_1X1;
1838                        if (adapter->is_hw_11ac_capable)
1839                                adapter->usr_dot_11ac_mcs_support =
1840                                                MWIFIEX_11AC_MCS_MAP_1X1;
1841                } else {
1842                        adapter->user_dev_mcs_support = HT_STREAM_2X2;
1843                        if (adapter->is_hw_11ac_capable)
1844                                adapter->usr_dot_11ac_mcs_support =
1845                                                MWIFIEX_11AC_MCS_MAP_2X2;
1846                }
1847
1848                for (band = 0; band < NUM_NL80211_BANDS; band++) {
1849                        if (!adapter->wiphy->bands[band])
1850                                continue;
1851
1852                        ht_info = &adapter->wiphy->bands[band]->ht_cap;
1853                        rx_mcs_supp =
1854                                GET_RXMCSSUPP(adapter->user_dev_mcs_support);
1855                        memset(&ht_info->mcs, 0, adapter->number_of_antenna);
1856                        memset(&ht_info->mcs, 0xff, rx_mcs_supp);
1857                }
1858        }
1859
1860        ant_cfg.tx_ant = tx_ant;
1861        ant_cfg.rx_ant = rx_ant;
1862
1863        return mwifiex_send_cmd(priv, HostCmd_CMD_RF_ANTENNA,
1864                                HostCmd_ACT_GEN_SET, 0, &ant_cfg, true);
1865}
1866
1867static int
1868mwifiex_cfg80211_get_antenna(struct wiphy *wiphy, u32 *tx_ant, u32 *rx_ant)
1869{
1870        struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
1871        struct mwifiex_private *priv = mwifiex_get_priv(adapter,
1872                                                        MWIFIEX_BSS_ROLE_ANY);
1873        mwifiex_send_cmd(priv, HostCmd_CMD_RF_ANTENNA,
1874                         HostCmd_ACT_GEN_GET, 0, NULL, true);
1875
1876        *tx_ant = priv->tx_ant;
1877        *rx_ant = priv->rx_ant;
1878
1879        return 0;
1880}
1881
1882/* cfg80211 operation handler for stop ap.
1883 * Function stops BSS running at uAP interface.
1884 */
1885static int mwifiex_cfg80211_stop_ap(struct wiphy *wiphy, struct net_device *dev)
1886{
1887        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1888
1889        mwifiex_abort_cac(priv);
1890
1891        if (mwifiex_del_mgmt_ies(priv))
1892                mwifiex_dbg(priv->adapter, ERROR,
1893                            "Failed to delete mgmt IEs!\n");
1894
1895        priv->ap_11n_enabled = 0;
1896        memset(&priv->bss_cfg, 0, sizeof(priv->bss_cfg));
1897
1898        if (mwifiex_send_cmd(priv, HostCmd_CMD_UAP_BSS_STOP,
1899                             HostCmd_ACT_GEN_SET, 0, NULL, true)) {
1900                mwifiex_dbg(priv->adapter, ERROR,
1901                            "Failed to stop the BSS\n");
1902                return -1;
1903        }
1904
1905        if (mwifiex_send_cmd(priv, HOST_CMD_APCMD_SYS_RESET,
1906                             HostCmd_ACT_GEN_SET, 0, NULL, true)) {
1907                mwifiex_dbg(priv->adapter, ERROR,
1908                            "Failed to reset BSS\n");
1909                return -1;
1910        }
1911
1912        if (netif_carrier_ok(priv->netdev))
1913                netif_carrier_off(priv->netdev);
1914        mwifiex_stop_net_dev_queue(priv->netdev, priv->adapter);
1915
1916        return 0;
1917}
1918
1919/* cfg80211 operation handler for start_ap.
1920 * Function sets beacon period, DTIM period, SSID and security into
1921 * AP config structure.
1922 * AP is configured with these settings and BSS is started.
1923 */
1924static int mwifiex_cfg80211_start_ap(struct wiphy *wiphy,
1925                                     struct net_device *dev,
1926                                     struct cfg80211_ap_settings *params)
1927{
1928        struct mwifiex_uap_bss_param *bss_cfg;
1929        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1930
1931        if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_UAP)
1932                return -1;
1933
1934        bss_cfg = kzalloc(sizeof(struct mwifiex_uap_bss_param), GFP_KERNEL);
1935        if (!bss_cfg)
1936                return -ENOMEM;
1937
1938        mwifiex_set_sys_config_invalid_data(bss_cfg);
1939
1940        if (params->beacon_interval)
1941                bss_cfg->beacon_period = params->beacon_interval;
1942        if (params->dtim_period)
1943                bss_cfg->dtim_period = params->dtim_period;
1944
1945        if (params->ssid && params->ssid_len) {
1946                memcpy(bss_cfg->ssid.ssid, params->ssid, params->ssid_len);
1947                bss_cfg->ssid.ssid_len = params->ssid_len;
1948        }
1949        if (params->inactivity_timeout > 0) {
1950                /* sta_ao_timer/ps_sta_ao_timer is in unit of 100ms */
1951                bss_cfg->sta_ao_timer = 10 * params->inactivity_timeout;
1952                bss_cfg->ps_sta_ao_timer = 10 * params->inactivity_timeout;
1953        }
1954
1955        switch (params->hidden_ssid) {
1956        case NL80211_HIDDEN_SSID_NOT_IN_USE:
1957                bss_cfg->bcast_ssid_ctl = 1;
1958                break;
1959        case NL80211_HIDDEN_SSID_ZERO_LEN:
1960                bss_cfg->bcast_ssid_ctl = 0;
1961                break;
1962        case NL80211_HIDDEN_SSID_ZERO_CONTENTS:
1963                /* firmware doesn't support this type of hidden SSID */
1964        default:
1965                kfree(bss_cfg);
1966                return -EINVAL;
1967        }
1968
1969        mwifiex_uap_set_channel(priv, bss_cfg, params->chandef);
1970        mwifiex_set_uap_rates(bss_cfg, params);
1971
1972        if (mwifiex_set_secure_params(priv, bss_cfg, params)) {
1973                mwifiex_dbg(priv->adapter, ERROR,
1974                            "Failed to parse security parameters!\n");
1975                goto out;
1976        }
1977
1978        mwifiex_set_ht_params(priv, bss_cfg, params);
1979
1980        if (priv->adapter->is_hw_11ac_capable) {
1981                mwifiex_set_vht_params(priv, bss_cfg, params);
1982                mwifiex_set_vht_width(priv, params->chandef.width,
1983                                      priv->ap_11ac_enabled);
1984        }
1985
1986        if (priv->ap_11ac_enabled)
1987                mwifiex_set_11ac_ba_params(priv);
1988        else
1989                mwifiex_set_ba_params(priv);
1990
1991        mwifiex_set_wmm_params(priv, bss_cfg, params);
1992
1993        if (mwifiex_is_11h_active(priv))
1994                mwifiex_set_tpc_params(priv, bss_cfg, params);
1995
1996        if (mwifiex_is_11h_active(priv) &&
1997            !cfg80211_chandef_dfs_required(wiphy, &params->chandef,
1998                                           priv->bss_mode)) {
1999                mwifiex_dbg(priv->adapter, INFO,
2000                            "Disable 11h extensions in FW\n");
2001                if (mwifiex_11h_activate(priv, false)) {
2002                        mwifiex_dbg(priv->adapter, ERROR,
2003                                    "Failed to disable 11h extensions!!");
2004                        goto out;
2005                }
2006                priv->state_11h.is_11h_active = false;
2007        }
2008
2009        mwifiex_config_uap_11d(priv, &params->beacon);
2010
2011        if (mwifiex_config_start_uap(priv, bss_cfg)) {
2012                mwifiex_dbg(priv->adapter, ERROR,
2013                            "Failed to start AP\n");
2014                goto out;
2015        }
2016
2017        if (mwifiex_set_mgmt_ies(priv, &params->beacon))
2018                goto out;
2019
2020        if (!netif_carrier_ok(priv->netdev))
2021                netif_carrier_on(priv->netdev);
2022        mwifiex_wake_up_net_dev_queue(priv->netdev, priv->adapter);
2023
2024        memcpy(&priv->bss_cfg, bss_cfg, sizeof(priv->bss_cfg));
2025        kfree(bss_cfg);
2026        return 0;
2027
2028out:
2029        kfree(bss_cfg);
2030        return -1;
2031}
2032
2033/*
2034 * CFG802.11 operation handler for disconnection request.
2035 *
2036 * This function does not work when there is already a disconnection
2037 * procedure going on.
2038 */
2039static int
2040mwifiex_cfg80211_disconnect(struct wiphy *wiphy, struct net_device *dev,
2041                            u16 reason_code)
2042{
2043        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2044
2045        if (!mwifiex_stop_bg_scan(priv))
2046                cfg80211_sched_scan_stopped_rtnl(priv->wdev.wiphy, 0);
2047
2048        if (mwifiex_deauthenticate(priv, NULL))
2049                return -EFAULT;
2050
2051        eth_zero_addr(priv->cfg_bssid);
2052        priv->hs2_enabled = false;
2053
2054        return 0;
2055}
2056
2057/*
2058 * This function informs the CFG802.11 subsystem of a new IBSS.
2059 *
2060 * The following information are sent to the CFG802.11 subsystem
2061 * to register the new IBSS. If we do not register the new IBSS,
2062 * a kernel panic will result.
2063 *      - SSID
2064 *      - SSID length
2065 *      - BSSID
2066 *      - Channel
2067 */
2068static int mwifiex_cfg80211_inform_ibss_bss(struct mwifiex_private *priv)
2069{
2070        struct ieee80211_channel *chan;
2071        struct mwifiex_bss_info bss_info;
2072        struct cfg80211_bss *bss;
2073        int ie_len;
2074        u8 ie_buf[IEEE80211_MAX_SSID_LEN + sizeof(struct ieee_types_header)];
2075        enum nl80211_band band;
2076
2077        if (mwifiex_get_bss_info(priv, &bss_info))
2078                return -1;
2079
2080        ie_buf[0] = WLAN_EID_SSID;
2081        ie_buf[1] = bss_info.ssid.ssid_len;
2082
2083        memcpy(&ie_buf[sizeof(struct ieee_types_header)],
2084               &bss_info.ssid.ssid, bss_info.ssid.ssid_len);
2085        ie_len = ie_buf[1] + sizeof(struct ieee_types_header);
2086
2087        band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
2088        chan = ieee80211_get_channel(priv->wdev.wiphy,
2089                        ieee80211_channel_to_frequency(bss_info.bss_chan,
2090                                                       band));
2091
2092        bss = cfg80211_inform_bss(priv->wdev.wiphy, chan,
2093                                  CFG80211_BSS_FTYPE_UNKNOWN,
2094                                  bss_info.bssid, 0, WLAN_CAPABILITY_IBSS,
2095                                  0, ie_buf, ie_len, 0, GFP_KERNEL);
2096        if (bss) {
2097                cfg80211_put_bss(priv->wdev.wiphy, bss);
2098                ether_addr_copy(priv->cfg_bssid, bss_info.bssid);
2099        }
2100
2101        return 0;
2102}
2103
2104/*
2105 * This function connects with a BSS.
2106 *
2107 * This function handles both Infra and Ad-Hoc modes. It also performs
2108 * validity checking on the provided parameters, disconnects from the
2109 * current BSS (if any), sets up the association/scan parameters,
2110 * including security settings, and performs specific SSID scan before
2111 * trying to connect.
2112 *
2113 * For Infra mode, the function returns failure if the specified SSID
2114 * is not found in scan table. However, for Ad-Hoc mode, it can create
2115 * the IBSS if it does not exist. On successful completion in either case,
2116 * the function notifies the CFG802.11 subsystem of the new BSS connection.
2117 */
2118static int
2119mwifiex_cfg80211_assoc(struct mwifiex_private *priv, size_t ssid_len,
2120                       const u8 *ssid, const u8 *bssid, int mode,
2121                       struct ieee80211_channel *channel,
2122                       struct cfg80211_connect_params *sme, bool privacy)
2123{
2124        struct cfg80211_ssid req_ssid;
2125        int ret, auth_type = 0;
2126        struct cfg80211_bss *bss = NULL;
2127        u8 is_scanning_required = 0;
2128
2129        memset(&req_ssid, 0, sizeof(struct cfg80211_ssid));
2130
2131        req_ssid.ssid_len = ssid_len;
2132        if (ssid_len > IEEE80211_MAX_SSID_LEN) {
2133                mwifiex_dbg(priv->adapter, ERROR, "invalid SSID - aborting\n");
2134                return -EINVAL;
2135        }
2136
2137        memcpy(req_ssid.ssid, ssid, ssid_len);
2138        if (!req_ssid.ssid_len || req_ssid.ssid[0] < 0x20) {
2139                mwifiex_dbg(priv->adapter, ERROR, "invalid SSID - aborting\n");
2140                return -EINVAL;
2141        }
2142
2143        /* As this is new association, clear locally stored
2144         * keys and security related flags */
2145        priv->sec_info.wpa_enabled = false;
2146        priv->sec_info.wpa2_enabled = false;
2147        priv->wep_key_curr_index = 0;
2148        priv->sec_info.encryption_mode = 0;
2149        priv->sec_info.is_authtype_auto = 0;
2150        ret = mwifiex_set_encode(priv, NULL, NULL, 0, 0, NULL, 1);
2151
2152        if (mode == NL80211_IFTYPE_ADHOC) {
2153                u16 enable = true;
2154
2155                /* set ibss coalescing_status */
2156                ret = mwifiex_send_cmd(
2157                                priv,
2158                                HostCmd_CMD_802_11_IBSS_COALESCING_STATUS,
2159                                HostCmd_ACT_GEN_SET, 0, &enable, true);
2160                if (ret)
2161                        return ret;
2162
2163                /* "privacy" is set only for ad-hoc mode */
2164                if (privacy) {
2165                        /*
2166                         * Keep WLAN_CIPHER_SUITE_WEP104 for now so that
2167                         * the firmware can find a matching network from the
2168                         * scan. The cfg80211 does not give us the encryption
2169                         * mode at this stage so just setting it to WEP here.
2170                         */
2171                        priv->sec_info.encryption_mode =
2172                                        WLAN_CIPHER_SUITE_WEP104;
2173                        priv->sec_info.authentication_mode =
2174                                        NL80211_AUTHTYPE_OPEN_SYSTEM;
2175                }
2176
2177                goto done;
2178        }
2179
2180        /* Now handle infra mode. "sme" is valid for infra mode only */
2181        if (sme->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
2182                auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
2183                priv->sec_info.is_authtype_auto = 1;
2184        } else {
2185                auth_type = sme->auth_type;
2186        }
2187
2188        if (sme->crypto.n_ciphers_pairwise) {
2189                priv->sec_info.encryption_mode =
2190                                                sme->crypto.ciphers_pairwise[0];
2191                priv->sec_info.authentication_mode = auth_type;
2192        }
2193
2194        if (sme->crypto.cipher_group) {
2195                priv->sec_info.encryption_mode = sme->crypto.cipher_group;
2196                priv->sec_info.authentication_mode = auth_type;
2197        }
2198        if (sme->ie)
2199                ret = mwifiex_set_gen_ie(priv, sme->ie, sme->ie_len);
2200
2201        if (sme->key) {
2202                if (mwifiex_is_alg_wep(priv->sec_info.encryption_mode)) {
2203                        mwifiex_dbg(priv->adapter, INFO,
2204                                    "info: setting wep encryption\t"
2205                                    "with key len %d\n", sme->key_len);
2206                        priv->wep_key_curr_index = sme->key_idx;
2207                        ret = mwifiex_set_encode(priv, NULL, sme->key,
2208                                                 sme->key_len, sme->key_idx,
2209                                                 NULL, 0);
2210                }
2211        }
2212done:
2213        /*
2214         * Scan entries are valid for some time (15 sec). So we can save one
2215         * active scan time if we just try cfg80211_get_bss first. If it fails
2216         * then request scan and cfg80211_get_bss() again for final output.
2217         */
2218        while (1) {
2219                if (is_scanning_required) {
2220                        /* Do specific SSID scanning */
2221                        if (mwifiex_request_scan(priv, &req_ssid)) {
2222                                mwifiex_dbg(priv->adapter, ERROR, "scan error\n");
2223                                return -EFAULT;
2224                        }
2225                }
2226
2227                /* Find the BSS we want using available scan results */
2228                if (mode == NL80211_IFTYPE_ADHOC)
2229                        bss = cfg80211_get_bss(priv->wdev.wiphy, channel,
2230                                               bssid, ssid, ssid_len,
2231                                               IEEE80211_BSS_TYPE_IBSS,
2232                                               IEEE80211_PRIVACY_ANY);
2233                else
2234                        bss = cfg80211_get_bss(priv->wdev.wiphy, channel,
2235                                               bssid, ssid, ssid_len,
2236                                               IEEE80211_BSS_TYPE_ESS,
2237                                               IEEE80211_PRIVACY_ANY);
2238
2239                if (!bss) {
2240                        if (is_scanning_required) {
2241                                mwifiex_dbg(priv->adapter, WARN,
2242                                            "assoc: requested bss not found in scan results\n");
2243                                break;
2244                        }
2245                        is_scanning_required = 1;
2246                } else {
2247                        mwifiex_dbg(priv->adapter, MSG,
2248                                    "info: trying to associate to '%.*s' bssid %pM\n",
2249                                    req_ssid.ssid_len, (char *)req_ssid.ssid,
2250                                    bss->bssid);
2251                        memcpy(&priv->cfg_bssid, bss->bssid, ETH_ALEN);
2252                        break;
2253                }
2254        }
2255
2256        ret = mwifiex_bss_start(priv, bss, &req_ssid);
2257        if (ret)
2258                return ret;
2259
2260        if (mode == NL80211_IFTYPE_ADHOC) {
2261                /* Inform the BSS information to kernel, otherwise
2262                 * kernel will give a panic after successful assoc */
2263                if (mwifiex_cfg80211_inform_ibss_bss(priv))
2264                        return -EFAULT;
2265        }
2266
2267        return ret;
2268}
2269
2270/*
2271 * CFG802.11 operation handler for association request.
2272 *
2273 * This function does not work when the current mode is set to Ad-Hoc, or
2274 * when there is already an association procedure going on. The given BSS
2275 * information is used to associate.
2276 */
2277static int
2278mwifiex_cfg80211_connect(struct wiphy *wiphy, struct net_device *dev,
2279                         struct cfg80211_connect_params *sme)
2280{
2281        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2282        struct mwifiex_adapter *adapter = priv->adapter;
2283        int ret;
2284
2285        if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_STA) {
2286                mwifiex_dbg(adapter, ERROR,
2287                            "%s: reject infra assoc request in non-STA role\n",
2288                            dev->name);
2289                return -EINVAL;
2290        }
2291
2292        if (priv->wdev.current_bss) {
2293                mwifiex_dbg(adapter, ERROR,
2294                            "%s: already connected\n", dev->name);
2295                return -EALREADY;
2296        }
2297
2298        if (priv->scan_block)
2299                priv->scan_block = false;
2300
2301        if (adapter->surprise_removed || adapter->is_cmd_timedout) {
2302                mwifiex_dbg(adapter, ERROR,
2303                            "%s: Ignore connection.\t"
2304                            "Card removed or FW in bad state\n",
2305                            dev->name);
2306                return -EFAULT;
2307        }
2308
2309        mwifiex_dbg(adapter, INFO,
2310                    "info: Trying to associate to %.*s and bssid %pM\n",
2311                    (int)sme->ssid_len, (char *)sme->ssid, sme->bssid);
2312
2313        if (!mwifiex_stop_bg_scan(priv))
2314                cfg80211_sched_scan_stopped_rtnl(priv->wdev.wiphy, 0);
2315
2316        ret = mwifiex_cfg80211_assoc(priv, sme->ssid_len, sme->ssid, sme->bssid,
2317                                     priv->bss_mode, sme->channel, sme, 0);
2318        if (!ret) {
2319                cfg80211_connect_result(priv->netdev, priv->cfg_bssid, NULL, 0,
2320                                        NULL, 0, WLAN_STATUS_SUCCESS,
2321                                        GFP_KERNEL);
2322                mwifiex_dbg(priv->adapter, MSG,
2323                            "info: associated to bssid %pM successfully\n",
2324                            priv->cfg_bssid);
2325                if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
2326                    priv->adapter->auto_tdls &&
2327                    priv->bss_type == MWIFIEX_BSS_TYPE_STA)
2328                        mwifiex_setup_auto_tdls_timer(priv);
2329        } else {
2330                mwifiex_dbg(priv->adapter, ERROR,
2331                            "info: association to bssid %pM failed\n",
2332                            priv->cfg_bssid);
2333                eth_zero_addr(priv->cfg_bssid);
2334
2335                if (ret > 0)
2336                        cfg80211_connect_result(priv->netdev, priv->cfg_bssid,
2337                                                NULL, 0, NULL, 0, ret,
2338                                                GFP_KERNEL);
2339                else
2340                        cfg80211_connect_result(priv->netdev, priv->cfg_bssid,
2341                                                NULL, 0, NULL, 0,
2342                                                WLAN_STATUS_UNSPECIFIED_FAILURE,
2343                                                GFP_KERNEL);
2344        }
2345
2346        return 0;
2347}
2348
2349/*
2350 * This function sets following parameters for ibss network.
2351 *  -  channel
2352 *  -  start band
2353 *  -  11n flag
2354 *  -  secondary channel offset
2355 */
2356static int mwifiex_set_ibss_params(struct mwifiex_private *priv,
2357                                   struct cfg80211_ibss_params *params)
2358{
2359        struct mwifiex_adapter *adapter = priv->adapter;
2360        int index = 0, i;
2361        u8 config_bands = 0;
2362
2363        if (params->chandef.chan->band == NL80211_BAND_2GHZ) {
2364                if (!params->basic_rates) {
2365                        config_bands = BAND_B | BAND_G;
2366                } else {
2367                        for (i = 0; i < mwifiex_band_2ghz.n_bitrates; i++) {
2368                                /*
2369                                 * Rates below 6 Mbps in the table are CCK
2370                                 * rates; 802.11b and from 6 they are OFDM;
2371                                 * 802.11G
2372                                 */
2373                                if (mwifiex_rates[i].bitrate == 60) {
2374                                        index = 1 << i;
2375                                        break;
2376                                }
2377                        }
2378
2379                        if (params->basic_rates < index) {
2380                                config_bands = BAND_B;
2381                        } else {
2382                                config_bands = BAND_G;
2383                                if (params->basic_rates % index)
2384                                        config_bands |= BAND_B;
2385                        }
2386                }
2387
2388                if (cfg80211_get_chandef_type(&params->chandef) !=
2389                                                NL80211_CHAN_NO_HT)
2390                        config_bands |= BAND_G | BAND_GN;
2391        } else {
2392                if (cfg80211_get_chandef_type(&params->chandef) ==
2393                                                NL80211_CHAN_NO_HT)
2394                        config_bands = BAND_A;
2395                else
2396                        config_bands = BAND_AN | BAND_A;
2397        }
2398
2399        if (!((config_bands | adapter->fw_bands) & ~adapter->fw_bands)) {
2400                adapter->config_bands = config_bands;
2401                adapter->adhoc_start_band = config_bands;
2402
2403                if ((config_bands & BAND_GN) || (config_bands & BAND_AN))
2404                        adapter->adhoc_11n_enabled = true;
2405                else
2406                        adapter->adhoc_11n_enabled = false;
2407        }
2408
2409        adapter->sec_chan_offset =
2410                mwifiex_chan_type_to_sec_chan_offset(
2411                        cfg80211_get_chandef_type(&params->chandef));
2412        priv->adhoc_channel = ieee80211_frequency_to_channel(
2413                                params->chandef.chan->center_freq);
2414
2415        mwifiex_dbg(adapter, INFO,
2416                    "info: set ibss band %d, chan %d, chan offset %d\n",
2417                    config_bands, priv->adhoc_channel,
2418                    adapter->sec_chan_offset);
2419
2420        return 0;
2421}
2422
2423/*
2424 * CFG802.11 operation handler to join an IBSS.
2425 *
2426 * This function does not work in any mode other than Ad-Hoc, or if
2427 * a join operation is already in progress.
2428 */
2429static int
2430mwifiex_cfg80211_join_ibss(struct wiphy *wiphy, struct net_device *dev,
2431                           struct cfg80211_ibss_params *params)
2432{
2433        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2434        int ret = 0;
2435
2436        if (priv->bss_mode != NL80211_IFTYPE_ADHOC) {
2437                mwifiex_dbg(priv->adapter, ERROR,
2438                            "request to join ibss received\t"
2439                            "when station is not in ibss mode\n");
2440                goto done;
2441        }
2442
2443        mwifiex_dbg(priv->adapter, MSG,
2444                    "info: trying to join to %.*s and bssid %pM\n",
2445                    params->ssid_len, (char *)params->ssid, params->bssid);
2446
2447        mwifiex_set_ibss_params(priv, params);
2448
2449        ret = mwifiex_cfg80211_assoc(priv, params->ssid_len, params->ssid,
2450                                     params->bssid, priv->bss_mode,
2451                                     params->chandef.chan, NULL,
2452                                     params->privacy);
2453done:
2454        if (!ret) {
2455                cfg80211_ibss_joined(priv->netdev, priv->cfg_bssid,
2456                                     params->chandef.chan, GFP_KERNEL);
2457                mwifiex_dbg(priv->adapter, MSG,
2458                            "info: joined/created adhoc network with bssid\t"
2459                            "%pM successfully\n", priv->cfg_bssid);
2460        } else {
2461                mwifiex_dbg(priv->adapter, ERROR,
2462                            "info: failed creating/joining adhoc network\n");
2463        }
2464
2465        return ret;
2466}
2467
2468/*
2469 * CFG802.11 operation handler to leave an IBSS.
2470 *
2471 * This function does not work if a leave operation is
2472 * already in progress.
2473 */
2474static int
2475mwifiex_cfg80211_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2476{
2477        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2478
2479        mwifiex_dbg(priv->adapter, MSG, "info: disconnecting from essid %pM\n",
2480                    priv->cfg_bssid);
2481        if (mwifiex_deauthenticate(priv, NULL))
2482                return -EFAULT;
2483
2484        eth_zero_addr(priv->cfg_bssid);
2485
2486        return 0;
2487}
2488
2489/*
2490 * CFG802.11 operation handler for scan request.
2491 *
2492 * This function issues a scan request to the firmware based upon
2493 * the user specified scan configuration. On successful completion,
2494 * it also informs the results.
2495 */
2496static int
2497mwifiex_cfg80211_scan(struct wiphy *wiphy,
2498                      struct cfg80211_scan_request *request)
2499{
2500        struct net_device *dev = request->wdev->netdev;
2501        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2502        int i, offset, ret;
2503        struct ieee80211_channel *chan;
2504        struct ieee_types_header *ie;
2505        struct mwifiex_user_scan_cfg *user_scan_cfg;
2506
2507        mwifiex_dbg(priv->adapter, CMD,
2508                    "info: received scan request on %s\n", dev->name);
2509
2510        /* Block scan request if scan operation or scan cleanup when interface
2511         * is disabled is in process
2512         */
2513        if (priv->scan_request || priv->scan_aborting) {
2514                mwifiex_dbg(priv->adapter, WARN,
2515                            "cmd: Scan already in process..\n");
2516                return -EBUSY;
2517        }
2518
2519        if (!priv->wdev.current_bss && priv->scan_block)
2520                priv->scan_block = false;
2521
2522        if (!mwifiex_stop_bg_scan(priv))
2523                cfg80211_sched_scan_stopped_rtnl(priv->wdev.wiphy, 0);
2524
2525        user_scan_cfg = kzalloc(sizeof(*user_scan_cfg), GFP_KERNEL);
2526        if (!user_scan_cfg)
2527                return -ENOMEM;
2528
2529        priv->scan_request = request;
2530
2531        if (request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
2532                ether_addr_copy(priv->random_mac, request->mac_addr);
2533                for (i = 0; i < ETH_ALEN; i++) {
2534                        priv->random_mac[i] &= request->mac_addr_mask[i];
2535                        priv->random_mac[i] |= get_random_int() &
2536                                               ~(request->mac_addr_mask[i]);
2537                }
2538                ether_addr_copy(user_scan_cfg->random_mac, priv->random_mac);
2539        } else {
2540                eth_zero_addr(priv->random_mac);
2541        }
2542
2543        user_scan_cfg->num_ssids = request->n_ssids;
2544        user_scan_cfg->ssid_list = request->ssids;
2545
2546        if (request->ie && request->ie_len) {
2547                offset = 0;
2548                for (i = 0; i < MWIFIEX_MAX_VSIE_NUM; i++) {
2549                        if (priv->vs_ie[i].mask != MWIFIEX_VSIE_MASK_CLEAR)
2550                                continue;
2551                        priv->vs_ie[i].mask = MWIFIEX_VSIE_MASK_SCAN;
2552                        ie = (struct ieee_types_header *)(request->ie + offset);
2553                        memcpy(&priv->vs_ie[i].ie, ie, sizeof(*ie) + ie->len);
2554                        offset += sizeof(*ie) + ie->len;
2555
2556                        if (offset >= request->ie_len)
2557                                break;
2558                }
2559        }
2560
2561        for (i = 0; i < min_t(u32, request->n_channels,
2562                              MWIFIEX_USER_SCAN_CHAN_MAX); i++) {
2563                chan = request->channels[i];
2564                user_scan_cfg->chan_list[i].chan_number = chan->hw_value;
2565                user_scan_cfg->chan_list[i].radio_type = chan->band;
2566
2567                if ((chan->flags & IEEE80211_CHAN_NO_IR) || !request->n_ssids)
2568                        user_scan_cfg->chan_list[i].scan_type =
2569                                                MWIFIEX_SCAN_TYPE_PASSIVE;
2570                else
2571                        user_scan_cfg->chan_list[i].scan_type =
2572                                                MWIFIEX_SCAN_TYPE_ACTIVE;
2573
2574                user_scan_cfg->chan_list[i].scan_time = 0;
2575        }
2576
2577        if (priv->adapter->scan_chan_gap_enabled &&
2578            mwifiex_is_any_intf_active(priv))
2579                user_scan_cfg->scan_chan_gap =
2580                                              priv->adapter->scan_chan_gap_time;
2581
2582        ret = mwifiex_scan_networks(priv, user_scan_cfg);
2583        kfree(user_scan_cfg);
2584        if (ret) {
2585                mwifiex_dbg(priv->adapter, ERROR,
2586                            "scan failed: %d\n", ret);
2587                priv->scan_aborting = false;
2588                priv->scan_request = NULL;
2589                return ret;
2590        }
2591
2592        if (request->ie && request->ie_len) {
2593                for (i = 0; i < MWIFIEX_MAX_VSIE_NUM; i++) {
2594                        if (priv->vs_ie[i].mask == MWIFIEX_VSIE_MASK_SCAN) {
2595                                priv->vs_ie[i].mask = MWIFIEX_VSIE_MASK_CLEAR;
2596                                memset(&priv->vs_ie[i].ie, 0,
2597                                       MWIFIEX_MAX_VSIE_LEN);
2598                        }
2599                }
2600        }
2601        return 0;
2602}
2603
2604/* CFG802.11 operation handler for sched_scan_start.
2605 *
2606 * This function issues a bgscan config request to the firmware based upon
2607 * the user specified sched_scan configuration. On successful completion,
2608 * firmware will generate BGSCAN_REPORT event, driver should issue bgscan
2609 * query command to get sched_scan results from firmware.
2610 */
2611static int
2612mwifiex_cfg80211_sched_scan_start(struct wiphy *wiphy,
2613                                  struct net_device *dev,
2614                                  struct cfg80211_sched_scan_request *request)
2615{
2616        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2617        int i, offset;
2618        struct ieee80211_channel *chan;
2619        struct mwifiex_bg_scan_cfg *bgscan_cfg;
2620        struct ieee_types_header *ie;
2621
2622        if (!request || (!request->n_ssids && !request->n_match_sets)) {
2623                wiphy_err(wiphy, "%s : Invalid Sched_scan parameters",
2624                          __func__);
2625                return -EINVAL;
2626        }
2627
2628        wiphy_info(wiphy, "sched_scan start : n_ssids=%d n_match_sets=%d ",
2629                   request->n_ssids, request->n_match_sets);
2630        wiphy_info(wiphy, "n_channels=%d interval=%d ie_len=%d\n",
2631                   request->n_channels, request->scan_plans->interval,
2632                   (int)request->ie_len);
2633
2634        bgscan_cfg = kzalloc(sizeof(*bgscan_cfg), GFP_KERNEL);
2635        if (!bgscan_cfg)
2636                return -ENOMEM;
2637
2638        if (priv->scan_request || priv->scan_aborting)
2639                bgscan_cfg->start_later = true;
2640
2641        bgscan_cfg->num_ssids = request->n_match_sets;
2642        bgscan_cfg->ssid_list = request->match_sets;
2643
2644        if (request->ie && request->ie_len) {
2645                offset = 0;
2646                for (i = 0; i < MWIFIEX_MAX_VSIE_NUM; i++) {
2647                        if (priv->vs_ie[i].mask != MWIFIEX_VSIE_MASK_CLEAR)
2648                                continue;
2649                        priv->vs_ie[i].mask = MWIFIEX_VSIE_MASK_BGSCAN;
2650                        ie = (struct ieee_types_header *)(request->ie + offset);
2651                        memcpy(&priv->vs_ie[i].ie, ie, sizeof(*ie) + ie->len);
2652                        offset += sizeof(*ie) + ie->len;
2653
2654                        if (offset >= request->ie_len)
2655                                break;
2656                }
2657        }
2658
2659        for (i = 0; i < min_t(u32, request->n_channels,
2660                              MWIFIEX_BG_SCAN_CHAN_MAX); i++) {
2661                chan = request->channels[i];
2662                bgscan_cfg->chan_list[i].chan_number = chan->hw_value;
2663                bgscan_cfg->chan_list[i].radio_type = chan->band;
2664
2665                if ((chan->flags & IEEE80211_CHAN_NO_IR) || !request->n_ssids)
2666                        bgscan_cfg->chan_list[i].scan_type =
2667                                                MWIFIEX_SCAN_TYPE_PASSIVE;
2668                else
2669                        bgscan_cfg->chan_list[i].scan_type =
2670                                                MWIFIEX_SCAN_TYPE_ACTIVE;
2671
2672                bgscan_cfg->chan_list[i].scan_time = 0;
2673        }
2674
2675        bgscan_cfg->chan_per_scan = min_t(u32, request->n_channels,
2676                                          MWIFIEX_BG_SCAN_CHAN_MAX);
2677
2678        /* Use at least 15 second for per scan cycle */
2679        bgscan_cfg->scan_interval = (request->scan_plans->interval >
2680                                     MWIFIEX_BGSCAN_INTERVAL) ?
2681                                request->scan_plans->interval :
2682                                MWIFIEX_BGSCAN_INTERVAL;
2683
2684        bgscan_cfg->repeat_count = MWIFIEX_BGSCAN_REPEAT_COUNT;
2685        bgscan_cfg->report_condition = MWIFIEX_BGSCAN_SSID_MATCH |
2686                                MWIFIEX_BGSCAN_WAIT_ALL_CHAN_DONE;
2687        bgscan_cfg->bss_type = MWIFIEX_BSS_MODE_INFRA;
2688        bgscan_cfg->action = MWIFIEX_BGSCAN_ACT_SET;
2689        bgscan_cfg->enable = true;
2690        if (request->min_rssi_thold != NL80211_SCAN_RSSI_THOLD_OFF) {
2691                bgscan_cfg->report_condition |= MWIFIEX_BGSCAN_SSID_RSSI_MATCH;
2692                bgscan_cfg->rssi_threshold = request->min_rssi_thold;
2693        }
2694
2695        if (mwifiex_send_cmd(priv, HostCmd_CMD_802_11_BG_SCAN_CONFIG,
2696                             HostCmd_ACT_GEN_SET, 0, bgscan_cfg, true)) {
2697                kfree(bgscan_cfg);
2698                return -EFAULT;
2699        }
2700
2701        priv->sched_scanning = true;
2702
2703        kfree(bgscan_cfg);
2704        return 0;
2705}
2706
2707/* CFG802.11 operation handler for sched_scan_stop.
2708 *
2709 * This function issues a bgscan config command to disable
2710 * previous bgscan configuration in the firmware
2711 */
2712static int mwifiex_cfg80211_sched_scan_stop(struct wiphy *wiphy,
2713                                            struct net_device *dev, u64 reqid)
2714{
2715        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2716
2717        wiphy_info(wiphy, "sched scan stop!");
2718        mwifiex_stop_bg_scan(priv);
2719
2720        return 0;
2721}
2722
2723static void mwifiex_setup_vht_caps(struct ieee80211_sta_vht_cap *vht_info,
2724                                   struct mwifiex_private *priv)
2725{
2726        struct mwifiex_adapter *adapter = priv->adapter;
2727
2728        vht_info->vht_supported = true;
2729
2730        vht_info->cap = adapter->hw_dot_11ac_dev_cap;
2731        /* Update MCS support for VHT */
2732        vht_info->vht_mcs.rx_mcs_map = cpu_to_le16(
2733                                adapter->hw_dot_11ac_mcs_support & 0xFFFF);
2734        vht_info->vht_mcs.rx_highest = 0;
2735        vht_info->vht_mcs.tx_mcs_map = cpu_to_le16(
2736                                adapter->hw_dot_11ac_mcs_support >> 16);
2737        vht_info->vht_mcs.tx_highest = 0;
2738}
2739
2740/*
2741 * This function sets up the CFG802.11 specific HT capability fields
2742 * with default values.
2743 *
2744 * The following default values are set -
2745 *      - HT Supported = True
2746 *      - Maximum AMPDU length factor = IEEE80211_HT_MAX_AMPDU_64K
2747 *      - Minimum AMPDU spacing = IEEE80211_HT_MPDU_DENSITY_NONE
2748 *      - HT Capabilities supported by firmware
2749 *      - MCS information, Rx mask = 0xff
2750 *      - MCD information, Tx parameters = IEEE80211_HT_MCS_TX_DEFINED (0x01)
2751 */
2752static void
2753mwifiex_setup_ht_caps(struct ieee80211_sta_ht_cap *ht_info,
2754                      struct mwifiex_private *priv)
2755{
2756        int rx_mcs_supp;
2757        struct ieee80211_mcs_info mcs_set;
2758        u8 *mcs = (u8 *)&mcs_set;
2759        struct mwifiex_adapter *adapter = priv->adapter;
2760
2761        ht_info->ht_supported = true;
2762        ht_info->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2763        ht_info->ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
2764
2765        memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
2766
2767        /* Fill HT capability information */
2768        if (ISSUPP_CHANWIDTH40(adapter->hw_dot_11n_dev_cap))
2769                ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2770        else
2771                ht_info->cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2772
2773        if (ISSUPP_SHORTGI20(adapter->hw_dot_11n_dev_cap))
2774                ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
2775        else
2776                ht_info->cap &= ~IEEE80211_HT_CAP_SGI_20;
2777
2778        if (ISSUPP_SHORTGI40(adapter->hw_dot_11n_dev_cap))
2779                ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
2780        else
2781                ht_info->cap &= ~IEEE80211_HT_CAP_SGI_40;
2782
2783        if (adapter->user_dev_mcs_support == HT_STREAM_2X2)
2784                ht_info->cap |= 2 << IEEE80211_HT_CAP_RX_STBC_SHIFT;
2785        else
2786                ht_info->cap |= 1 << IEEE80211_HT_CAP_RX_STBC_SHIFT;
2787
2788        if (ISSUPP_TXSTBC(adapter->hw_dot_11n_dev_cap))
2789                ht_info->cap |= IEEE80211_HT_CAP_TX_STBC;
2790        else
2791                ht_info->cap &= ~IEEE80211_HT_CAP_TX_STBC;
2792
2793        if (ISSUPP_GREENFIELD(adapter->hw_dot_11n_dev_cap))
2794                ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
2795        else
2796                ht_info->cap &= ~IEEE80211_HT_CAP_GRN_FLD;
2797
2798        if (ISENABLED_40MHZ_INTOLERANT(adapter->hw_dot_11n_dev_cap))
2799                ht_info->cap |= IEEE80211_HT_CAP_40MHZ_INTOLERANT;
2800        else
2801                ht_info->cap &= ~IEEE80211_HT_CAP_40MHZ_INTOLERANT;
2802
2803        if (ISSUPP_RXLDPC(adapter->hw_dot_11n_dev_cap))
2804                ht_info->cap |= IEEE80211_HT_CAP_LDPC_CODING;
2805        else
2806                ht_info->cap &= ~IEEE80211_HT_CAP_LDPC_CODING;
2807
2808        ht_info->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
2809        ht_info->cap |= IEEE80211_HT_CAP_SM_PS;
2810
2811        rx_mcs_supp = GET_RXMCSSUPP(adapter->user_dev_mcs_support);
2812        /* Set MCS for 1x1/2x2 */
2813        memset(mcs, 0xff, rx_mcs_supp);
2814        /* Clear all the other values */
2815        memset(&mcs[rx_mcs_supp], 0,
2816               sizeof(struct ieee80211_mcs_info) - rx_mcs_supp);
2817        if (priv->bss_mode == NL80211_IFTYPE_STATION ||
2818            ISSUPP_CHANWIDTH40(adapter->hw_dot_11n_dev_cap))
2819                /* Set MCS32 for infra mode or ad-hoc mode with 40MHz support */
2820                SETHT_MCS32(mcs_set.rx_mask);
2821
2822        memcpy((u8 *) &ht_info->mcs, mcs, sizeof(struct ieee80211_mcs_info));
2823
2824        ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2825}
2826
2827/*
2828 *  create a new virtual interface with the given name and name assign type
2829 */
2830struct wireless_dev *mwifiex_add_virtual_intf(struct wiphy *wiphy,
2831                                              const char *name,
2832                                              unsigned char name_assign_type,
2833                                              enum nl80211_iftype type,
2834                                              struct vif_params *params)
2835{
2836        struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
2837        struct mwifiex_private *priv;
2838        struct net_device *dev;
2839        void *mdev_priv;
2840        int ret;
2841
2842        if (!adapter)
2843                return ERR_PTR(-EFAULT);
2844
2845        switch (type) {
2846        case NL80211_IFTYPE_UNSPECIFIED:
2847        case NL80211_IFTYPE_STATION:
2848        case NL80211_IFTYPE_ADHOC:
2849                if (adapter->curr_iface_comb.sta_intf ==
2850                    adapter->iface_limit.sta_intf) {
2851                        mwifiex_dbg(adapter, ERROR,
2852                                    "cannot create multiple sta/adhoc ifaces\n");
2853                        return ERR_PTR(-EINVAL);
2854                }
2855
2856                priv = mwifiex_get_unused_priv_by_bss_type(
2857                                                adapter, MWIFIEX_BSS_TYPE_STA);
2858                if (!priv) {
2859                        mwifiex_dbg(adapter, ERROR,
2860                                    "could not get free private struct\n");
2861                        return ERR_PTR(-EFAULT);
2862                }
2863
2864                priv->wdev.wiphy = wiphy;
2865                priv->wdev.iftype = NL80211_IFTYPE_STATION;
2866
2867                if (type == NL80211_IFTYPE_UNSPECIFIED)
2868                        priv->bss_mode = NL80211_IFTYPE_STATION;
2869                else
2870                        priv->bss_mode = type;
2871
2872                priv->bss_type = MWIFIEX_BSS_TYPE_STA;
2873                priv->frame_type = MWIFIEX_DATA_FRAME_TYPE_ETH_II;
2874                priv->bss_priority = 0;
2875                priv->bss_role = MWIFIEX_BSS_ROLE_STA;
2876
2877                break;
2878        case NL80211_IFTYPE_AP:
2879                if (adapter->curr_iface_comb.uap_intf ==
2880                    adapter->iface_limit.uap_intf) {
2881                        mwifiex_dbg(adapter, ERROR,
2882                                    "cannot create multiple AP ifaces\n");
2883                        return ERR_PTR(-EINVAL);
2884                }
2885
2886                priv = mwifiex_get_unused_priv_by_bss_type(
2887                                                adapter, MWIFIEX_BSS_TYPE_UAP);
2888                if (!priv) {
2889                        mwifiex_dbg(adapter, ERROR,
2890                                    "could not get free private struct\n");
2891                        return ERR_PTR(-EFAULT);
2892                }
2893
2894                priv->wdev.wiphy = wiphy;
2895                priv->wdev.iftype = NL80211_IFTYPE_AP;
2896
2897                priv->bss_type = MWIFIEX_BSS_TYPE_UAP;
2898                priv->frame_type = MWIFIEX_DATA_FRAME_TYPE_ETH_II;
2899                priv->bss_priority = 0;
2900                priv->bss_role = MWIFIEX_BSS_ROLE_UAP;
2901                priv->bss_started = 0;
2902                priv->bss_mode = type;
2903
2904                break;
2905        case NL80211_IFTYPE_P2P_CLIENT:
2906                if (adapter->curr_iface_comb.p2p_intf ==
2907                    adapter->iface_limit.p2p_intf) {
2908                        mwifiex_dbg(adapter, ERROR,
2909                                    "cannot create multiple P2P ifaces\n");
2910                        return ERR_PTR(-EINVAL);
2911                }
2912
2913                priv = mwifiex_get_unused_priv_by_bss_type(
2914                                                adapter, MWIFIEX_BSS_TYPE_P2P);
2915                if (!priv) {
2916                        mwifiex_dbg(adapter, ERROR,
2917                                    "could not get free private struct\n");
2918                        return ERR_PTR(-EFAULT);
2919                }
2920
2921                priv->wdev.wiphy = wiphy;
2922                /* At start-up, wpa_supplicant tries to change the interface
2923                 * to NL80211_IFTYPE_STATION if it is not managed mode.
2924                 */
2925                priv->wdev.iftype = NL80211_IFTYPE_P2P_CLIENT;
2926                priv->bss_mode = NL80211_IFTYPE_P2P_CLIENT;
2927
2928                /* Setting bss_type to P2P tells firmware that this interface
2929                 * is receiving P2P peers found during find phase and doing
2930                 * action frame handshake.
2931                 */
2932                priv->bss_type = MWIFIEX_BSS_TYPE_P2P;
2933
2934                priv->frame_type = MWIFIEX_DATA_FRAME_TYPE_ETH_II;
2935                priv->bss_priority = MWIFIEX_BSS_ROLE_STA;
2936                priv->bss_role = MWIFIEX_BSS_ROLE_STA;
2937                priv->bss_started = 0;
2938
2939                if (mwifiex_cfg80211_init_p2p_client(priv)) {
2940                        memset(&priv->wdev, 0, sizeof(priv->wdev));
2941                        priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
2942                        return ERR_PTR(-EFAULT);
2943                }
2944
2945                break;
2946        default:
2947                mwifiex_dbg(adapter, ERROR, "type not supported\n");
2948                return ERR_PTR(-EINVAL);
2949        }
2950
2951        dev = alloc_netdev_mqs(sizeof(struct mwifiex_private *), name,
2952#if 0 /* Not in RHEL */
2953                               name_assign_type, ether_setup,
2954#else
2955                               ether_setup,
2956#endif
2957                               IEEE80211_NUM_ACS, 1);
2958        if (!dev) {
2959                mwifiex_dbg(adapter, ERROR,
2960                            "no memory available for netdevice\n");
2961                ret = -ENOMEM;
2962                goto err_alloc_netdev;
2963        }
2964
2965        mwifiex_init_priv_params(priv, dev);
2966        mwifiex_set_mac_address(priv, dev);
2967
2968        priv->netdev = dev;
2969
2970        ret = mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
2971                               HostCmd_ACT_GEN_SET, 0, NULL, true);
2972        if (ret)
2973                goto err_set_bss_mode;
2974
2975        ret = mwifiex_sta_init_cmd(priv, false, false);
2976        if (ret)
2977                goto err_sta_init;
2978
2979        mwifiex_setup_ht_caps(&wiphy->bands[NL80211_BAND_2GHZ]->ht_cap, priv);
2980        if (adapter->is_hw_11ac_capable)
2981                mwifiex_setup_vht_caps(
2982                        &wiphy->bands[NL80211_BAND_2GHZ]->vht_cap, priv);
2983
2984        if (adapter->config_bands & BAND_A)
2985                mwifiex_setup_ht_caps(
2986                        &wiphy->bands[NL80211_BAND_5GHZ]->ht_cap, priv);
2987
2988        if ((adapter->config_bands & BAND_A) && adapter->is_hw_11ac_capable)
2989                mwifiex_setup_vht_caps(
2990                        &wiphy->bands[NL80211_BAND_5GHZ]->vht_cap, priv);
2991
2992        dev_net_set(dev, wiphy_net(wiphy));
2993        dev->ieee80211_ptr = &priv->wdev;
2994        dev->ieee80211_ptr->iftype = priv->bss_mode;
2995        SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
2996
2997        dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
2998        dev->watchdog_timeo = MWIFIEX_DEFAULT_WATCHDOG_TIMEOUT;
2999        dev->hard_header_len += MWIFIEX_MIN_DATA_HEADER_LEN;
3000        dev->ethtool_ops = &mwifiex_ethtool_ops;
3001
3002        mdev_priv = netdev_priv(dev);
3003        *((unsigned long *) mdev_priv) = (unsigned long) priv;
3004
3005        SET_NETDEV_DEV(dev, adapter->dev);
3006
3007        priv->dfs_cac_workqueue = alloc_workqueue("MWIFIEX_DFS_CAC%s",
3008                                                  WQ_HIGHPRI |
3009                                                  WQ_MEM_RECLAIM |
3010                                                  WQ_UNBOUND, 1, name);
3011        if (!priv->dfs_cac_workqueue) {
3012                mwifiex_dbg(adapter, ERROR, "cannot alloc DFS CAC queue\n");
3013                ret = -ENOMEM;
3014                goto err_alloc_cac;
3015        }
3016
3017        INIT_DELAYED_WORK(&priv->dfs_cac_work, mwifiex_dfs_cac_work_queue);
3018
3019        priv->dfs_chan_sw_workqueue = alloc_workqueue("MWIFIEX_DFS_CHSW%s",
3020                                                      WQ_HIGHPRI | WQ_UNBOUND |
3021                                                      WQ_MEM_RECLAIM, 1, name);
3022        if (!priv->dfs_chan_sw_workqueue) {
3023                mwifiex_dbg(adapter, ERROR, "cannot alloc DFS channel sw queue\n");
3024                ret = -ENOMEM;
3025                goto err_alloc_chsw;
3026        }
3027
3028        INIT_DELAYED_WORK(&priv->dfs_chan_sw_work,
3029                          mwifiex_dfs_chan_sw_work_queue);
3030
3031        mutex_init(&priv->async_mutex);
3032
3033        /* Register network device */
3034        if (register_netdevice(dev)) {
3035                mwifiex_dbg(adapter, ERROR, "cannot register network device\n");
3036                ret = -EFAULT;
3037                goto err_reg_netdev;
3038        }
3039
3040        mwifiex_dbg(adapter, INFO,
3041                    "info: %s: Marvell 802.11 Adapter\n", dev->name);
3042
3043#ifdef CONFIG_DEBUG_FS
3044        mwifiex_dev_debugfs_init(priv);
3045#endif
3046
3047        switch (type) {
3048        case NL80211_IFTYPE_UNSPECIFIED:
3049        case NL80211_IFTYPE_STATION:
3050        case NL80211_IFTYPE_ADHOC:
3051                adapter->curr_iface_comb.sta_intf++;
3052                break;
3053        case NL80211_IFTYPE_AP:
3054                adapter->curr_iface_comb.uap_intf++;
3055                break;
3056        case NL80211_IFTYPE_P2P_CLIENT:
3057                adapter->curr_iface_comb.p2p_intf++;
3058                break;
3059        default:
3060                /* This should be dead code; checked above */
3061                mwifiex_dbg(adapter, ERROR, "type not supported\n");
3062                return ERR_PTR(-EINVAL);
3063        }
3064
3065        return &priv->wdev;
3066
3067err_reg_netdev:
3068        destroy_workqueue(priv->dfs_chan_sw_workqueue);
3069        priv->dfs_chan_sw_workqueue = NULL;
3070err_alloc_chsw:
3071        destroy_workqueue(priv->dfs_cac_workqueue);
3072        priv->dfs_cac_workqueue = NULL;
3073err_alloc_cac:
3074        free_netdev(dev);
3075        priv->netdev = NULL;
3076err_sta_init:
3077err_set_bss_mode:
3078err_alloc_netdev:
3079        memset(&priv->wdev, 0, sizeof(priv->wdev));
3080        priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
3081        priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
3082        return ERR_PTR(ret);
3083}
3084EXPORT_SYMBOL_GPL(mwifiex_add_virtual_intf);
3085
3086/*
3087 * del_virtual_intf: remove the virtual interface determined by dev
3088 */
3089int mwifiex_del_virtual_intf(struct wiphy *wiphy, struct wireless_dev *wdev)
3090{
3091        struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
3092        struct mwifiex_adapter *adapter = priv->adapter;
3093        struct sk_buff *skb, *tmp;
3094
3095#ifdef CONFIG_DEBUG_FS
3096        mwifiex_dev_debugfs_remove(priv);
3097#endif
3098
3099        if (priv->sched_scanning)
3100                priv->sched_scanning = false;
3101
3102        mwifiex_stop_net_dev_queue(priv->netdev, adapter);
3103
3104        skb_queue_walk_safe(&priv->bypass_txq, skb, tmp) {
3105                skb_unlink(skb, &priv->bypass_txq);
3106                mwifiex_write_data_complete(priv->adapter, skb, 0, -1);
3107        }
3108
3109        if (netif_carrier_ok(priv->netdev))
3110                netif_carrier_off(priv->netdev);
3111
3112        if (wdev->netdev->reg_state == NETREG_REGISTERED)
3113                unregister_netdevice(wdev->netdev);
3114
3115        if (priv->dfs_cac_workqueue) {
3116                flush_workqueue(priv->dfs_cac_workqueue);
3117                destroy_workqueue(priv->dfs_cac_workqueue);
3118                priv->dfs_cac_workqueue = NULL;
3119        }
3120
3121        if (priv->dfs_chan_sw_workqueue) {
3122                flush_workqueue(priv->dfs_chan_sw_workqueue);
3123                destroy_workqueue(priv->dfs_chan_sw_workqueue);
3124                priv->dfs_chan_sw_workqueue = NULL;
3125        }
3126        /* Clear the priv in adapter */
3127        priv->netdev = NULL;
3128
3129        switch (priv->bss_mode) {
3130        case NL80211_IFTYPE_UNSPECIFIED:
3131        case NL80211_IFTYPE_STATION:
3132        case NL80211_IFTYPE_ADHOC:
3133                adapter->curr_iface_comb.sta_intf--;
3134                break;
3135        case NL80211_IFTYPE_AP:
3136                adapter->curr_iface_comb.uap_intf--;
3137                break;
3138        case NL80211_IFTYPE_P2P_CLIENT:
3139        case NL80211_IFTYPE_P2P_GO:
3140                adapter->curr_iface_comb.p2p_intf--;
3141                break;
3142        default:
3143                mwifiex_dbg(adapter, ERROR,
3144                            "del_virtual_intf: type not supported\n");
3145                break;
3146        }
3147
3148        priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
3149
3150        if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
3151            GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP)
3152                kfree(priv->hist_data);
3153
3154        return 0;
3155}
3156EXPORT_SYMBOL_GPL(mwifiex_del_virtual_intf);
3157
3158static bool
3159mwifiex_is_pattern_supported(struct cfg80211_pkt_pattern *pat, s8 *byte_seq,
3160                             u8 max_byte_seq)
3161{
3162        int j, k, valid_byte_cnt = 0;
3163        bool dont_care_byte = false;
3164
3165        for (j = 0; j < DIV_ROUND_UP(pat->pattern_len, 8); j++) {
3166                for (k = 0; k < 8; k++) {
3167                        if (pat->mask[j] & 1 << k) {
3168                                memcpy(byte_seq + valid_byte_cnt,
3169                                       &pat->pattern[j * 8 + k], 1);
3170                                valid_byte_cnt++;
3171                                if (dont_care_byte)
3172                                        return false;
3173                        } else {
3174                                if (valid_byte_cnt)
3175                                        dont_care_byte = true;
3176                        }
3177
3178                        /* wildcard bytes record as the offset
3179                         * before the valid byte
3180                         */
3181                        if (!valid_byte_cnt && !dont_care_byte)
3182                                pat->pkt_offset++;
3183
3184                        if (valid_byte_cnt > max_byte_seq)
3185                                return false;
3186                }
3187        }
3188
3189        byte_seq[max_byte_seq] = valid_byte_cnt;
3190
3191        return true;
3192}
3193
3194#ifdef CONFIG_PM
3195static void mwifiex_set_auto_arp_mef_entry(struct mwifiex_private *priv,
3196                                           struct mwifiex_mef_entry *mef_entry)
3197{
3198        int i, filt_num = 0, num_ipv4 = 0;
3199        struct in_device *in_dev;
3200        struct in_ifaddr *ifa;
3201        __be32 ips[MWIFIEX_MAX_SUPPORTED_IPADDR];
3202        struct mwifiex_adapter *adapter = priv->adapter;
3203
3204        mef_entry->mode = MEF_MODE_HOST_SLEEP;
3205        mef_entry->action = MEF_ACTION_AUTO_ARP;
3206
3207        /* Enable ARP offload feature */
3208        memset(ips, 0, sizeof(ips));
3209        for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
3210                if (adapter->priv[i]->netdev) {
3211                        in_dev = __in_dev_get_rtnl(adapter->priv[i]->netdev);
3212                        if (!in_dev)
3213                                continue;
3214                        ifa = in_dev->ifa_list;
3215                        if (!ifa || !ifa->ifa_local)
3216                                continue;
3217                        ips[i] = ifa->ifa_local;
3218                        num_ipv4++;
3219                }
3220        }
3221
3222        for (i = 0; i < num_ipv4; i++) {
3223                if (!ips[i])
3224                        continue;
3225                mef_entry->filter[filt_num].repeat = 1;
3226                memcpy(mef_entry->filter[filt_num].byte_seq,
3227                       (u8 *)&ips[i], sizeof(ips[i]));
3228                mef_entry->filter[filt_num].
3229                        byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] =
3230                        sizeof(ips[i]);
3231                mef_entry->filter[filt_num].offset = 46;
3232                mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3233                if (filt_num) {
3234                        mef_entry->filter[filt_num].filt_action =
3235                                TYPE_OR;
3236                }
3237                filt_num++;
3238        }
3239
3240        mef_entry->filter[filt_num].repeat = 1;
3241        mef_entry->filter[filt_num].byte_seq[0] = 0x08;
3242        mef_entry->filter[filt_num].byte_seq[1] = 0x06;
3243        mef_entry->filter[filt_num].byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] = 2;
3244        mef_entry->filter[filt_num].offset = 20;
3245        mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3246        mef_entry->filter[filt_num].filt_action = TYPE_AND;
3247}
3248
3249static int mwifiex_set_wowlan_mef_entry(struct mwifiex_private *priv,
3250                                        struct mwifiex_ds_mef_cfg *mef_cfg,
3251                                        struct mwifiex_mef_entry *mef_entry,
3252                                        struct cfg80211_wowlan *wowlan)
3253{
3254        int i, filt_num = 0, ret = 0;
3255        bool first_pat = true;
3256        u8 byte_seq[MWIFIEX_MEF_MAX_BYTESEQ + 1];
3257        const u8 ipv4_mc_mac[] = {0x33, 0x33};
3258        const u8 ipv6_mc_mac[] = {0x01, 0x00, 0x5e};
3259
3260        mef_entry->mode = MEF_MODE_HOST_SLEEP;
3261        mef_entry->action = MEF_ACTION_ALLOW_AND_WAKEUP_HOST;
3262
3263        for (i = 0; i < wowlan->n_patterns; i++) {
3264                memset(byte_seq, 0, sizeof(byte_seq));
3265                if (!mwifiex_is_pattern_supported(&wowlan->patterns[i],
3266                                        byte_seq,
3267                                        MWIFIEX_MEF_MAX_BYTESEQ)) {
3268                        mwifiex_dbg(priv->adapter, ERROR,
3269                                    "Pattern not supported\n");
3270                        return -EOPNOTSUPP;
3271                }
3272
3273                if (!wowlan->patterns[i].pkt_offset) {
3274                        if (!(byte_seq[0] & 0x01) &&
3275                            (byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] == 1)) {
3276                                mef_cfg->criteria |= MWIFIEX_CRITERIA_UNICAST;
3277                                continue;
3278                        } else if (is_broadcast_ether_addr(byte_seq)) {
3279                                mef_cfg->criteria |= MWIFIEX_CRITERIA_BROADCAST;
3280                                continue;
3281                        } else if ((!memcmp(byte_seq, ipv4_mc_mac, 2) &&
3282                                    (byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] == 2)) ||
3283                                   (!memcmp(byte_seq, ipv6_mc_mac, 3) &&
3284                                    (byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] == 3))) {
3285                                mef_cfg->criteria |= MWIFIEX_CRITERIA_MULTICAST;
3286                                continue;
3287                        }
3288                }
3289                mef_entry->filter[filt_num].repeat = 1;
3290                mef_entry->filter[filt_num].offset =
3291                        wowlan->patterns[i].pkt_offset;
3292                memcpy(mef_entry->filter[filt_num].byte_seq, byte_seq,
3293                                sizeof(byte_seq));
3294                mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3295
3296                if (first_pat) {
3297                        first_pat = false;
3298                        mwifiex_dbg(priv->adapter, INFO, "Wake on patterns\n");
3299                } else {
3300                        mef_entry->filter[filt_num].filt_action = TYPE_AND;
3301                }
3302
3303                filt_num++;
3304        }
3305
3306        if (wowlan->magic_pkt) {
3307                mef_cfg->criteria |= MWIFIEX_CRITERIA_UNICAST;
3308                mef_entry->filter[filt_num].repeat = 16;
3309                memcpy(mef_entry->filter[filt_num].byte_seq, priv->curr_addr,
3310                                ETH_ALEN);
3311                mef_entry->filter[filt_num].byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] =
3312                        ETH_ALEN;
3313                mef_entry->filter[filt_num].offset = 28;
3314                mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3315                if (filt_num)
3316                        mef_entry->filter[filt_num].filt_action = TYPE_OR;
3317
3318                filt_num++;
3319                mef_entry->filter[filt_num].repeat = 16;
3320                memcpy(mef_entry->filter[filt_num].byte_seq, priv->curr_addr,
3321                                ETH_ALEN);
3322                mef_entry->filter[filt_num].byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] =
3323                        ETH_ALEN;
3324                mef_entry->filter[filt_num].offset = 56;
3325                mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3326                mef_entry->filter[filt_num].filt_action = TYPE_OR;
3327                mwifiex_dbg(priv->adapter, INFO, "Wake on magic packet\n");
3328        }
3329        return ret;
3330}
3331
3332static int mwifiex_set_mef_filter(struct mwifiex_private *priv,
3333                                  struct cfg80211_wowlan *wowlan)
3334{
3335        int ret = 0, num_entries = 1;
3336        struct mwifiex_ds_mef_cfg mef_cfg;
3337        struct mwifiex_mef_entry *mef_entry;
3338
3339        if (wowlan->n_patterns || wowlan->magic_pkt)
3340                num_entries++;
3341
3342        mef_entry = kcalloc(num_entries, sizeof(*mef_entry), GFP_KERNEL);
3343        if (!mef_entry)
3344                return -ENOMEM;
3345
3346        memset(&mef_cfg, 0, sizeof(mef_cfg));
3347        mef_cfg.criteria |= MWIFIEX_CRITERIA_BROADCAST |
3348                MWIFIEX_CRITERIA_UNICAST;
3349        mef_cfg.num_entries = num_entries;
3350        mef_cfg.mef_entry = mef_entry;
3351
3352        mwifiex_set_auto_arp_mef_entry(priv, &mef_entry[0]);
3353
3354        if (wowlan->n_patterns || wowlan->magic_pkt) {
3355                ret = mwifiex_set_wowlan_mef_entry(priv, &mef_cfg,
3356                                                   &mef_entry[1], wowlan);
3357                if (ret)
3358                        goto err;
3359        }
3360
3361        if (!mef_cfg.criteria)
3362                mef_cfg.criteria = MWIFIEX_CRITERIA_BROADCAST |
3363                        MWIFIEX_CRITERIA_UNICAST |
3364                        MWIFIEX_CRITERIA_MULTICAST;
3365
3366        ret = mwifiex_send_cmd(priv, HostCmd_CMD_MEF_CFG,
3367                        HostCmd_ACT_GEN_SET, 0,
3368                        &mef_cfg, true);
3369
3370err:
3371        kfree(mef_entry);
3372        return ret;
3373}
3374
3375static int mwifiex_cfg80211_suspend(struct wiphy *wiphy,
3376                                    struct cfg80211_wowlan *wowlan)
3377{
3378        struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
3379        struct mwifiex_ds_hs_cfg hs_cfg;
3380        int i, ret = 0, retry_num = 10;
3381        struct mwifiex_private *priv;
3382        struct mwifiex_private *sta_priv =
3383                        mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
3384
3385        sta_priv->scan_aborting = true;
3386        for (i = 0; i < adapter->priv_num; i++) {
3387                priv = adapter->priv[i];
3388                mwifiex_abort_cac(priv);
3389        }
3390
3391        mwifiex_cancel_all_pending_cmd(adapter);
3392
3393        for (i = 0; i < adapter->priv_num; i++) {
3394                priv = adapter->priv[i];
3395                if (priv && priv->netdev)
3396                        netif_device_detach(priv->netdev);
3397        }
3398
3399        for (i = 0; i < retry_num; i++) {
3400                if (!mwifiex_wmm_lists_empty(adapter) ||
3401                    !mwifiex_bypass_txlist_empty(adapter) ||
3402                    !skb_queue_empty(&adapter->tx_data_q))
3403                        usleep_range(10000, 15000);
3404                else
3405                        break;
3406        }
3407
3408        if (!wowlan) {
3409                mwifiex_dbg(adapter, ERROR,
3410                            "None of the WOWLAN triggers enabled\n");
3411                ret = 0;
3412                goto done;
3413        }
3414
3415        if (!sta_priv->media_connected && !wowlan->nd_config) {
3416                mwifiex_dbg(adapter, ERROR,
3417                            "Can not configure WOWLAN in disconnected state\n");
3418                ret = 0;
3419                goto done;
3420        }
3421
3422        ret = mwifiex_set_mef_filter(sta_priv, wowlan);
3423        if (ret) {
3424                mwifiex_dbg(adapter, ERROR, "Failed to set MEF filter\n");
3425                goto done;
3426        }
3427
3428        memset(&hs_cfg, 0, sizeof(hs_cfg));
3429        hs_cfg.conditions = le32_to_cpu(adapter->hs_cfg.conditions);
3430
3431        if (wowlan->nd_config) {
3432                mwifiex_dbg(adapter, INFO, "Wake on net detect\n");
3433                hs_cfg.conditions |= HS_CFG_COND_MAC_EVENT;
3434                mwifiex_cfg80211_sched_scan_start(wiphy, sta_priv->netdev,
3435                                                  wowlan->nd_config);
3436        }
3437
3438        if (wowlan->disconnect) {
3439                hs_cfg.conditions |= HS_CFG_COND_MAC_EVENT;
3440                mwifiex_dbg(sta_priv->adapter, INFO, "Wake on device disconnect\n");
3441        }
3442
3443        hs_cfg.is_invoke_hostcmd = false;
3444        hs_cfg.gpio = adapter->hs_cfg.gpio;
3445        hs_cfg.gap = adapter->hs_cfg.gap;
3446        ret = mwifiex_set_hs_params(sta_priv, HostCmd_ACT_GEN_SET,
3447                                    MWIFIEX_SYNC_CMD, &hs_cfg);
3448        if (ret)
3449                mwifiex_dbg(adapter, ERROR, "Failed to set HS params\n");
3450
3451done:
3452        sta_priv->scan_aborting = false;
3453        return ret;
3454}
3455
3456static int mwifiex_cfg80211_resume(struct wiphy *wiphy)
3457{
3458        struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
3459        struct mwifiex_private *priv;
3460        struct mwifiex_ds_wakeup_reason wakeup_reason;
3461        struct cfg80211_wowlan_wakeup wakeup_report;
3462        int i;
3463        bool report_wakeup_reason = true;
3464
3465        for (i = 0; i < adapter->priv_num; i++) {
3466                priv = adapter->priv[i];
3467                if (priv && priv->netdev)
3468                        netif_device_attach(priv->netdev);
3469        }
3470
3471        if (!wiphy->wowlan_config)
3472                goto done;
3473
3474        priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
3475        mwifiex_get_wakeup_reason(priv, HostCmd_ACT_GEN_GET, MWIFIEX_SYNC_CMD,
3476                                  &wakeup_reason);
3477        memset(&wakeup_report, 0, sizeof(struct cfg80211_wowlan_wakeup));
3478
3479        wakeup_report.pattern_idx = -1;
3480
3481        switch (wakeup_reason.hs_wakeup_reason) {
3482        case NO_HSWAKEUP_REASON:
3483                break;
3484        case BCAST_DATA_MATCHED:
3485                break;
3486        case MCAST_DATA_MATCHED:
3487                break;
3488        case UCAST_DATA_MATCHED:
3489                break;
3490        case MASKTABLE_EVENT_MATCHED:
3491                break;
3492        case NON_MASKABLE_EVENT_MATCHED:
3493                if (wiphy->wowlan_config->disconnect)
3494                        wakeup_report.disconnect = true;
3495                if (wiphy->wowlan_config->nd_config)
3496                        wakeup_report.net_detect = adapter->nd_info;
3497                break;
3498        case NON_MASKABLE_CONDITION_MATCHED:
3499                break;
3500        case MAGIC_PATTERN_MATCHED:
3501                if (wiphy->wowlan_config->magic_pkt)
3502                        wakeup_report.magic_pkt = true;
3503                if (wiphy->wowlan_config->n_patterns)
3504                        wakeup_report.pattern_idx = 1;
3505                break;
3506        case GTK_REKEY_FAILURE:
3507                if (wiphy->wowlan_config->gtk_rekey_failure)
3508                        wakeup_report.gtk_rekey_failure = true;
3509                break;
3510        default:
3511                report_wakeup_reason = false;
3512                break;
3513        }
3514
3515        if (report_wakeup_reason)
3516                cfg80211_report_wowlan_wakeup(&priv->wdev, &wakeup_report,
3517                                              GFP_KERNEL);
3518
3519done:
3520        if (adapter->nd_info) {
3521                for (i = 0 ; i < adapter->nd_info->n_matches ; i++)
3522                        kfree(adapter->nd_info->matches[i]);
3523                kfree(adapter->nd_info);
3524                adapter->nd_info = NULL;
3525        }
3526
3527        return 0;
3528}
3529
3530static void mwifiex_cfg80211_set_wakeup(struct wiphy *wiphy,
3531                                       bool enabled)
3532{
3533        struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
3534
3535        device_set_wakeup_enable(adapter->dev, enabled);
3536}
3537
3538static int mwifiex_set_rekey_data(struct wiphy *wiphy, struct net_device *dev,
3539                                  struct cfg80211_gtk_rekey_data *data)
3540{
3541        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3542
3543        return mwifiex_send_cmd(priv, HostCmd_CMD_GTK_REKEY_OFFLOAD_CFG,
3544                                HostCmd_ACT_GEN_SET, 0, data, true);
3545}
3546
3547#endif
3548
3549static int mwifiex_get_coalesce_pkt_type(u8 *byte_seq)
3550{
3551        const u8 ipv4_mc_mac[] = {0x33, 0x33};
3552        const u8 ipv6_mc_mac[] = {0x01, 0x00, 0x5e};
3553        const u8 bc_mac[] = {0xff, 0xff, 0xff, 0xff};
3554
3555        if ((byte_seq[0] & 0x01) &&
3556            (byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ] == 1))
3557                return PACKET_TYPE_UNICAST;
3558        else if (!memcmp(byte_seq, bc_mac, 4))
3559                return PACKET_TYPE_BROADCAST;
3560        else if ((!memcmp(byte_seq, ipv4_mc_mac, 2) &&
3561                  byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ] == 2) ||
3562                 (!memcmp(byte_seq, ipv6_mc_mac, 3) &&
3563                  byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ] == 3))
3564                return PACKET_TYPE_MULTICAST;
3565
3566        return 0;
3567}
3568
3569static int
3570mwifiex_fill_coalesce_rule_info(struct mwifiex_private *priv,
3571                                struct cfg80211_coalesce_rules *crule,
3572                                struct mwifiex_coalesce_rule *mrule)
3573{
3574        u8 byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ + 1];
3575        struct filt_field_param *param;
3576        int i;
3577
3578        mrule->max_coalescing_delay = crule->delay;
3579
3580        param = mrule->params;
3581
3582        for (i = 0; i < crule->n_patterns; i++) {
3583                memset(byte_seq, 0, sizeof(byte_seq));
3584                if (!mwifiex_is_pattern_supported(&crule->patterns[i],
3585                                                  byte_seq,
3586                                                MWIFIEX_COALESCE_MAX_BYTESEQ)) {
3587                        mwifiex_dbg(priv->adapter, ERROR,
3588                                    "Pattern not supported\n");
3589                        return -EOPNOTSUPP;
3590                }
3591
3592                if (!crule->patterns[i].pkt_offset) {
3593                        u8 pkt_type;
3594
3595                        pkt_type = mwifiex_get_coalesce_pkt_type(byte_seq);
3596                        if (pkt_type && mrule->pkt_type) {
3597                                mwifiex_dbg(priv->adapter, ERROR,
3598                                            "Multiple packet types not allowed\n");
3599                                return -EOPNOTSUPP;
3600                        } else if (pkt_type) {
3601                                mrule->pkt_type = pkt_type;
3602                                continue;
3603                        }
3604                }
3605
3606                if (crule->condition == NL80211_COALESCE_CONDITION_MATCH)
3607                        param->operation = RECV_FILTER_MATCH_TYPE_EQ;
3608                else
3609                        param->operation = RECV_FILTER_MATCH_TYPE_NE;
3610
3611                param->operand_len = byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ];
3612                memcpy(param->operand_byte_stream, byte_seq,
3613                       param->operand_len);
3614                param->offset = crule->patterns[i].pkt_offset;
3615                param++;
3616
3617                mrule->num_of_fields++;
3618        }
3619
3620        if (!mrule->pkt_type) {
3621                mwifiex_dbg(priv->adapter, ERROR,
3622                            "Packet type can not be determined\n");
3623                return -EOPNOTSUPP;
3624        }
3625
3626        return 0;
3627}
3628
3629static int mwifiex_cfg80211_set_coalesce(struct wiphy *wiphy,
3630                                         struct cfg80211_coalesce *coalesce)
3631{
3632        struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
3633        int i, ret;
3634        struct mwifiex_ds_coalesce_cfg coalesce_cfg;
3635        struct mwifiex_private *priv =
3636                        mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
3637
3638        memset(&coalesce_cfg, 0, sizeof(coalesce_cfg));
3639        if (!coalesce) {
3640                mwifiex_dbg(adapter, WARN,
3641                            "Disable coalesce and reset all previous rules\n");
3642                return mwifiex_send_cmd(priv, HostCmd_CMD_COALESCE_CFG,
3643                                        HostCmd_ACT_GEN_SET, 0,
3644                                        &coalesce_cfg, true);
3645        }
3646
3647        coalesce_cfg.num_of_rules = coalesce->n_rules;
3648        for (i = 0; i < coalesce->n_rules; i++) {
3649                ret = mwifiex_fill_coalesce_rule_info(priv, &coalesce->rules[i],
3650                                                      &coalesce_cfg.rule[i]);
3651                if (ret) {
3652                        mwifiex_dbg(adapter, ERROR,
3653                                    "Recheck the patterns provided for rule %d\n",
3654                                i + 1);
3655                        return ret;
3656                }
3657        }
3658
3659        return mwifiex_send_cmd(priv, HostCmd_CMD_COALESCE_CFG,
3660                                HostCmd_ACT_GEN_SET, 0, &coalesce_cfg, true);
3661}
3662
3663/* cfg80211 ops handler for tdls_mgmt.
3664 * Function prepares TDLS action frame packets and forwards them to FW
3665 */
3666static int
3667mwifiex_cfg80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
3668                           const u8 *peer, u8 action_code, u8 dialog_token,
3669                           u16 status_code, u32 peer_capability,
3670                           bool initiator, const u8 *extra_ies,
3671                           size_t extra_ies_len)
3672{
3673        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3674        int ret;
3675
3676        if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
3677                return -ENOTSUPP;
3678
3679        /* make sure we are in station mode and connected */
3680        if (!(priv->bss_type == MWIFIEX_BSS_TYPE_STA && priv->media_connected))
3681                return -ENOTSUPP;
3682
3683        switch (action_code) {
3684        case WLAN_TDLS_SETUP_REQUEST:
3685                mwifiex_dbg(priv->adapter, MSG,
3686                            "Send TDLS Setup Request to %pM status_code=%d\n",
3687                            peer, status_code);
3688                mwifiex_add_auto_tdls_peer(priv, peer);
3689                ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3690                                                   dialog_token, status_code,
3691                                                   extra_ies, extra_ies_len);
3692                break;
3693        case WLAN_TDLS_SETUP_RESPONSE:
3694                mwifiex_add_auto_tdls_peer(priv, peer);
3695                mwifiex_dbg(priv->adapter, MSG,
3696                            "Send TDLS Setup Response to %pM status_code=%d\n",
3697                            peer, status_code);
3698                ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3699                                                   dialog_token, status_code,
3700                                                   extra_ies, extra_ies_len);
3701                break;
3702        case WLAN_TDLS_SETUP_CONFIRM:
3703                mwifiex_dbg(priv->adapter, MSG,
3704                            "Send TDLS Confirm to %pM status_code=%d\n", peer,
3705                            status_code);
3706                ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3707                                                   dialog_token, status_code,
3708                                                   extra_ies, extra_ies_len);
3709                break;
3710        case WLAN_TDLS_TEARDOWN:
3711                mwifiex_dbg(priv->adapter, MSG,
3712                            "Send TDLS Tear down to %pM\n", peer);
3713                ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3714                                                   dialog_token, status_code,
3715                                                   extra_ies, extra_ies_len);
3716                break;
3717        case WLAN_TDLS_DISCOVERY_REQUEST:
3718                mwifiex_dbg(priv->adapter, MSG,
3719                            "Send TDLS Discovery Request to %pM\n", peer);
3720                ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3721                                                   dialog_token, status_code,
3722                                                   extra_ies, extra_ies_len);
3723                break;
3724        case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
3725                mwifiex_dbg(priv->adapter, MSG,
3726                            "Send TDLS Discovery Response to %pM\n", peer);
3727                ret = mwifiex_send_tdls_action_frame(priv, peer, action_code,
3728                                                   dialog_token, status_code,
3729                                                   extra_ies, extra_ies_len);
3730                break;
3731        default:
3732                mwifiex_dbg(priv->adapter, ERROR,
3733                            "Unknown TDLS mgmt/action frame %pM\n", peer);
3734                ret = -EINVAL;
3735                break;
3736        }
3737
3738        return ret;
3739}
3740
3741static int
3742mwifiex_cfg80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
3743                           const u8 *peer, enum nl80211_tdls_operation action)
3744{
3745        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3746
3747        if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
3748            !(wiphy->flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3749                return -ENOTSUPP;
3750
3751        /* make sure we are in station mode and connected */
3752        if (!(priv->bss_type == MWIFIEX_BSS_TYPE_STA && priv->media_connected))
3753                return -ENOTSUPP;
3754
3755        mwifiex_dbg(priv->adapter, MSG,
3756                    "TDLS peer=%pM, oper=%d\n", peer, action);
3757
3758        switch (action) {
3759        case NL80211_TDLS_ENABLE_LINK:
3760                action = MWIFIEX_TDLS_ENABLE_LINK;
3761                break;
3762        case NL80211_TDLS_DISABLE_LINK:
3763                action = MWIFIEX_TDLS_DISABLE_LINK;
3764                break;
3765        case NL80211_TDLS_TEARDOWN:
3766                /* shouldn't happen!*/
3767                mwifiex_dbg(priv->adapter, ERROR,
3768                            "tdls_oper: teardown from driver not supported\n");
3769                return -EINVAL;
3770        case NL80211_TDLS_SETUP:
3771                /* shouldn't happen!*/
3772                mwifiex_dbg(priv->adapter, ERROR,
3773                            "tdls_oper: setup from driver not supported\n");
3774                return -EINVAL;
3775        case NL80211_TDLS_DISCOVERY_REQ:
3776                /* shouldn't happen!*/
3777                mwifiex_dbg(priv->adapter, ERROR,
3778                            "tdls_oper: discovery from driver not supported\n");
3779                return -EINVAL;
3780        default:
3781                mwifiex_dbg(priv->adapter, ERROR,
3782                            "tdls_oper: operation not supported\n");
3783                return -ENOTSUPP;
3784        }
3785
3786        return mwifiex_tdls_oper(priv, peer, action);
3787}
3788
3789static int
3790mwifiex_cfg80211_tdls_chan_switch(struct wiphy *wiphy, struct net_device *dev,
3791                                  const u8 *addr, u8 oper_class,
3792                                  struct cfg80211_chan_def *chandef)
3793{
3794        struct mwifiex_sta_node *sta_ptr;
3795        unsigned long flags;
3796        u16 chan;
3797        u8 second_chan_offset, band;
3798        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3799
3800        spin_lock_irqsave(&priv->sta_list_spinlock, flags);
3801        sta_ptr = mwifiex_get_sta_entry(priv, addr);
3802        spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
3803
3804        if (!sta_ptr) {
3805                wiphy_err(wiphy, "%s: Invalid TDLS peer %pM\n",
3806                          __func__, addr);
3807                return -ENOENT;
3808        }
3809
3810        if (!(sta_ptr->tdls_cap.extcap.ext_capab[3] &
3811              WLAN_EXT_CAPA4_TDLS_CHAN_SWITCH)) {
3812                wiphy_err(wiphy, "%pM do not support tdls cs\n", addr);
3813                return -ENOENT;
3814        }
3815
3816        if (sta_ptr->tdls_status == TDLS_CHAN_SWITCHING ||
3817            sta_ptr->tdls_status == TDLS_IN_OFF_CHAN) {
3818                wiphy_err(wiphy, "channel switch is running, abort request\n");
3819                return -EALREADY;
3820        }
3821
3822        chan = chandef->chan->hw_value;
3823        second_chan_offset = mwifiex_get_sec_chan_offset(chan);
3824        band = chandef->chan->band;
3825        mwifiex_start_tdls_cs(priv, addr, chan, second_chan_offset, band);
3826
3827        return 0;
3828}
3829
3830static void
3831mwifiex_cfg80211_tdls_cancel_chan_switch(struct wiphy *wiphy,
3832                                         struct net_device *dev,
3833                                         const u8 *addr)
3834{
3835        struct mwifiex_sta_node *sta_ptr;
3836        unsigned long flags;
3837        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3838
3839        spin_lock_irqsave(&priv->sta_list_spinlock, flags);
3840        sta_ptr = mwifiex_get_sta_entry(priv, addr);
3841        spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
3842
3843        if (!sta_ptr) {
3844                wiphy_err(wiphy, "%s: Invalid TDLS peer %pM\n",
3845                          __func__, addr);
3846        } else if (!(sta_ptr->tdls_status == TDLS_CHAN_SWITCHING ||
3847                     sta_ptr->tdls_status == TDLS_IN_BASE_CHAN ||
3848                     sta_ptr->tdls_status == TDLS_IN_OFF_CHAN)) {
3849                wiphy_err(wiphy, "tdls chan switch not initialize by %pM\n",
3850                          addr);
3851        } else
3852                mwifiex_stop_tdls_cs(priv, addr);
3853}
3854
3855static int
3856mwifiex_cfg80211_add_station(struct wiphy *wiphy, struct net_device *dev,
3857                             const u8 *mac, struct station_parameters *params)
3858{
3859        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3860
3861        if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3862                return -ENOTSUPP;
3863
3864        /* make sure we are in station mode and connected */
3865        if ((priv->bss_type != MWIFIEX_BSS_TYPE_STA) || !priv->media_connected)
3866                return -ENOTSUPP;
3867
3868        return mwifiex_tdls_oper(priv, mac, MWIFIEX_TDLS_CREATE_LINK);
3869}
3870
3871static int
3872mwifiex_cfg80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
3873                                struct cfg80211_csa_settings *params)
3874{
3875        struct ieee_types_header *chsw_ie;
3876        struct ieee80211_channel_sw_ie *channel_sw;
3877        int chsw_msec;
3878        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3879
3880        if (priv->adapter->scan_processing) {
3881                mwifiex_dbg(priv->adapter, ERROR,
3882                            "radar detection: scan in process...\n");
3883                return -EBUSY;
3884        }
3885
3886        if (priv->wdev.cac_started)
3887                return -EBUSY;
3888
3889        if (cfg80211_chandef_identical(&params->chandef,
3890                                       &priv->dfs_chandef))
3891                return -EINVAL;
3892
3893        chsw_ie = (void *)cfg80211_find_ie(WLAN_EID_CHANNEL_SWITCH,
3894                                           params->beacon_csa.tail,
3895                                           params->beacon_csa.tail_len);
3896        if (!chsw_ie) {
3897                mwifiex_dbg(priv->adapter, ERROR,
3898                            "Could not parse channel switch announcement IE\n");
3899                return -EINVAL;
3900        }
3901
3902        channel_sw = (void *)(chsw_ie + 1);
3903        if (channel_sw->mode) {
3904                if (netif_carrier_ok(priv->netdev))
3905                        netif_carrier_off(priv->netdev);
3906                mwifiex_stop_net_dev_queue(priv->netdev, priv->adapter);
3907        }
3908
3909        if (mwifiex_del_mgmt_ies(priv))
3910                mwifiex_dbg(priv->adapter, ERROR,
3911                            "Failed to delete mgmt IEs!\n");
3912
3913        if (mwifiex_set_mgmt_ies(priv, &params->beacon_csa)) {
3914                mwifiex_dbg(priv->adapter, ERROR,
3915                            "%s: setting mgmt ies failed\n", __func__);
3916                return -EFAULT;
3917        }
3918
3919        memcpy(&priv->dfs_chandef, &params->chandef, sizeof(priv->dfs_chandef));
3920        memcpy(&priv->beacon_after, &params->beacon_after,
3921               sizeof(priv->beacon_after));
3922
3923        chsw_msec = max(channel_sw->count * priv->bss_cfg.beacon_period, 100);
3924        queue_delayed_work(priv->dfs_chan_sw_workqueue, &priv->dfs_chan_sw_work,
3925                           msecs_to_jiffies(chsw_msec));
3926        return 0;
3927}
3928
3929static int mwifiex_cfg80211_get_channel(struct wiphy *wiphy,
3930                                        struct wireless_dev *wdev,
3931                                        struct cfg80211_chan_def *chandef)
3932{
3933        struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
3934        struct mwifiex_bssdescriptor *curr_bss;
3935        struct ieee80211_channel *chan;
3936        u8 second_chan_offset;
3937        enum nl80211_channel_type chan_type;
3938        enum nl80211_band band;
3939        int freq;
3940        int ret = -ENODATA;
3941
3942        if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP &&
3943            cfg80211_chandef_valid(&priv->bss_chandef)) {
3944                *chandef = priv->bss_chandef;
3945                ret = 0;
3946        } else if (priv->media_connected) {
3947                curr_bss = &priv->curr_bss_params.bss_descriptor;
3948                band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
3949                freq = ieee80211_channel_to_frequency(curr_bss->channel, band);
3950                chan = ieee80211_get_channel(wiphy, freq);
3951
3952                if (priv->ht_param_present) {
3953                        second_chan_offset = priv->assoc_resp_ht_param &
3954                                        IEEE80211_HT_PARAM_CHA_SEC_OFFSET;
3955                        chan_type = mwifiex_sec_chan_offset_to_chan_type
3956                                                        (second_chan_offset);
3957                        cfg80211_chandef_create(chandef, chan, chan_type);
3958                } else {
3959                        cfg80211_chandef_create(chandef, chan,
3960                                                NL80211_CHAN_NO_HT);
3961                }
3962                ret = 0;
3963        }
3964
3965        return ret;
3966}
3967
3968#ifdef CONFIG_NL80211_TESTMODE
3969
3970enum mwifiex_tm_attr {
3971        __MWIFIEX_TM_ATTR_INVALID       = 0,
3972        MWIFIEX_TM_ATTR_CMD             = 1,
3973        MWIFIEX_TM_ATTR_DATA            = 2,
3974
3975        /* keep last */
3976        __MWIFIEX_TM_ATTR_AFTER_LAST,
3977        MWIFIEX_TM_ATTR_MAX             = __MWIFIEX_TM_ATTR_AFTER_LAST - 1,
3978};
3979
3980static const struct nla_policy mwifiex_tm_policy[MWIFIEX_TM_ATTR_MAX + 1] = {
3981        [MWIFIEX_TM_ATTR_CMD]           = { .type = NLA_U32 },
3982        [MWIFIEX_TM_ATTR_DATA]          = { .type = NLA_BINARY,
3983                                            .len = MWIFIEX_SIZE_OF_CMD_BUFFER },
3984};
3985
3986enum mwifiex_tm_command {
3987        MWIFIEX_TM_CMD_HOSTCMD  = 0,
3988};
3989
3990static int mwifiex_tm_cmd(struct wiphy *wiphy, struct wireless_dev *wdev,
3991                          void *data, int len)
3992{
3993        struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
3994        struct mwifiex_ds_misc_cmd *hostcmd;
3995        struct nlattr *tb[MWIFIEX_TM_ATTR_MAX + 1];
3996        struct sk_buff *skb;
3997        int err;
3998
3999        if (!priv)
4000                return -EINVAL;
4001
4002        err = nla_parse(tb, MWIFIEX_TM_ATTR_MAX, data, len, mwifiex_tm_policy,
4003                        NULL);
4004        if (err)
4005                return err;
4006
4007        if (!tb[MWIFIEX_TM_ATTR_CMD])
4008                return -EINVAL;
4009
4010        switch (nla_get_u32(tb[MWIFIEX_TM_ATTR_CMD])) {
4011        case MWIFIEX_TM_CMD_HOSTCMD:
4012                if (!tb[MWIFIEX_TM_ATTR_DATA])
4013                        return -EINVAL;
4014
4015                hostcmd = kzalloc(sizeof(*hostcmd), GFP_KERNEL);
4016                if (!hostcmd)
4017                        return -ENOMEM;
4018
4019                hostcmd->len = nla_len(tb[MWIFIEX_TM_ATTR_DATA]);
4020                memcpy(hostcmd->cmd, nla_data(tb[MWIFIEX_TM_ATTR_DATA]),
4021                       hostcmd->len);
4022
4023                if (mwifiex_send_cmd(priv, 0, 0, 0, hostcmd, true)) {
4024                        dev_err(priv->adapter->dev, "Failed to process hostcmd\n");
4025                        return -EFAULT;
4026                }
4027
4028                /* process hostcmd response*/
4029                skb = cfg80211_testmode_alloc_reply_skb(wiphy, hostcmd->len);
4030                if (!skb)
4031                        return -ENOMEM;
4032                err = nla_put(skb, MWIFIEX_TM_ATTR_DATA,
4033                              hostcmd->len, hostcmd->cmd);
4034                if (err) {
4035                        kfree_skb(skb);
4036                        return -EMSGSIZE;
4037                }
4038
4039                err = cfg80211_testmode_reply(skb);
4040                kfree(hostcmd);
4041                return err;
4042        default:
4043                return -EOPNOTSUPP;
4044        }
4045}
4046#endif
4047
4048static int
4049mwifiex_cfg80211_start_radar_detection(struct wiphy *wiphy,
4050                                       struct net_device *dev,
4051                                       struct cfg80211_chan_def *chandef,
4052                                       u32 cac_time_ms)
4053{
4054        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
4055        struct mwifiex_radar_params radar_params;
4056
4057        if (priv->adapter->scan_processing) {
4058                mwifiex_dbg(priv->adapter, ERROR,
4059                            "radar detection: scan already in process...\n");
4060                return -EBUSY;
4061        }
4062
4063        if (!mwifiex_is_11h_active(priv)) {
4064                mwifiex_dbg(priv->adapter, INFO,
4065                            "Enable 11h extensions in FW\n");
4066                if (mwifiex_11h_activate(priv, true)) {
4067                        mwifiex_dbg(priv->adapter, ERROR,
4068                                    "Failed to activate 11h extensions!!");
4069                        return -1;
4070                }
4071                priv->state_11h.is_11h_active = true;
4072        }
4073
4074        memset(&radar_params, 0, sizeof(struct mwifiex_radar_params));
4075        radar_params.chandef = chandef;
4076        radar_params.cac_time_ms = cac_time_ms;
4077
4078        memcpy(&priv->dfs_chandef, chandef, sizeof(priv->dfs_chandef));
4079
4080        if (mwifiex_send_cmd(priv, HostCmd_CMD_CHAN_REPORT_REQUEST,
4081                             HostCmd_ACT_GEN_SET, 0, &radar_params, true))
4082                return -1;
4083
4084        queue_delayed_work(priv->dfs_cac_workqueue, &priv->dfs_cac_work,
4085                           msecs_to_jiffies(cac_time_ms));
4086        return 0;
4087}
4088
4089static int
4090mwifiex_cfg80211_change_station(struct wiphy *wiphy, struct net_device *dev,
4091                                const u8 *mac,
4092                                struct station_parameters *params)
4093{
4094        int ret;
4095        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
4096
4097        /* we support change_station handler only for TDLS peers*/
4098        if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4099                return -ENOTSUPP;
4100
4101        /* make sure we are in station mode and connected */
4102        if ((priv->bss_type != MWIFIEX_BSS_TYPE_STA) || !priv->media_connected)
4103                return -ENOTSUPP;
4104
4105        priv->sta_params = params;
4106
4107        ret = mwifiex_tdls_oper(priv, mac, MWIFIEX_TDLS_CONFIG_LINK);
4108        priv->sta_params = NULL;
4109
4110        return ret;
4111}
4112
4113/* station cfg80211 operations */
4114static struct cfg80211_ops mwifiex_cfg80211_ops = {
4115        .add_virtual_intf = mwifiex_add_virtual_intf,
4116        .del_virtual_intf = mwifiex_del_virtual_intf,
4117        .change_virtual_intf = mwifiex_cfg80211_change_virtual_intf,
4118        .scan = mwifiex_cfg80211_scan,
4119        .connect = mwifiex_cfg80211_connect,
4120        .disconnect = mwifiex_cfg80211_disconnect,
4121        .get_station = mwifiex_cfg80211_get_station,
4122        .dump_station = mwifiex_cfg80211_dump_station,
4123        .dump_survey = mwifiex_cfg80211_dump_survey,
4124        .set_wiphy_params = mwifiex_cfg80211_set_wiphy_params,
4125        .join_ibss = mwifiex_cfg80211_join_ibss,
4126        .leave_ibss = mwifiex_cfg80211_leave_ibss,
4127        .add_key = mwifiex_cfg80211_add_key,
4128        .del_key = mwifiex_cfg80211_del_key,
4129        .set_default_mgmt_key = mwifiex_cfg80211_set_default_mgmt_key,
4130        .mgmt_tx = mwifiex_cfg80211_mgmt_tx,
4131        .mgmt_frame_register = mwifiex_cfg80211_mgmt_frame_register,
4132        .remain_on_channel = mwifiex_cfg80211_remain_on_channel,
4133        .cancel_remain_on_channel = mwifiex_cfg80211_cancel_remain_on_channel,
4134        .set_default_key = mwifiex_cfg80211_set_default_key,
4135        .set_power_mgmt = mwifiex_cfg80211_set_power_mgmt,
4136        .set_tx_power = mwifiex_cfg80211_set_tx_power,
4137        .get_tx_power = mwifiex_cfg80211_get_tx_power,
4138        .set_bitrate_mask = mwifiex_cfg80211_set_bitrate_mask,
4139        .start_ap = mwifiex_cfg80211_start_ap,
4140        .stop_ap = mwifiex_cfg80211_stop_ap,
4141        .change_beacon = mwifiex_cfg80211_change_beacon,
4142        .set_cqm_rssi_config = mwifiex_cfg80211_set_cqm_rssi_config,
4143        .set_antenna = mwifiex_cfg80211_set_antenna,
4144        .get_antenna = mwifiex_cfg80211_get_antenna,
4145        .del_station = mwifiex_cfg80211_del_station,
4146        .sched_scan_start = mwifiex_cfg80211_sched_scan_start,
4147        .sched_scan_stop = mwifiex_cfg80211_sched_scan_stop,
4148#ifdef CONFIG_PM
4149        .suspend = mwifiex_cfg80211_suspend,
4150        .resume = mwifiex_cfg80211_resume,
4151        .set_wakeup = mwifiex_cfg80211_set_wakeup,
4152        .set_rekey_data = mwifiex_set_rekey_data,
4153#endif
4154        .set_coalesce = mwifiex_cfg80211_set_coalesce,
4155        .tdls_mgmt = mwifiex_cfg80211_tdls_mgmt,
4156        .tdls_oper = mwifiex_cfg80211_tdls_oper,
4157        .tdls_channel_switch = mwifiex_cfg80211_tdls_chan_switch,
4158        .tdls_cancel_channel_switch = mwifiex_cfg80211_tdls_cancel_chan_switch,
4159        .add_station = mwifiex_cfg80211_add_station,
4160        .change_station = mwifiex_cfg80211_change_station,
4161        CFG80211_TESTMODE_CMD(mwifiex_tm_cmd)
4162        .get_channel = mwifiex_cfg80211_get_channel,
4163        .start_radar_detection = mwifiex_cfg80211_start_radar_detection,
4164        .channel_switch = mwifiex_cfg80211_channel_switch,
4165};
4166
4167#ifdef CONFIG_PM
4168static const struct wiphy_wowlan_support mwifiex_wowlan_support = {
4169        .flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT |
4170                WIPHY_WOWLAN_NET_DETECT | WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
4171                WIPHY_WOWLAN_GTK_REKEY_FAILURE,
4172        .n_patterns = MWIFIEX_MEF_MAX_FILTERS,
4173        .pattern_min_len = 1,
4174        .pattern_max_len = MWIFIEX_MAX_PATTERN_LEN,
4175        .max_pkt_offset = MWIFIEX_MAX_OFFSET_LEN,
4176        .max_nd_match_sets = MWIFIEX_MAX_ND_MATCH_SETS,
4177};
4178#endif
4179
4180static bool mwifiex_is_valid_alpha2(const char *alpha2)
4181{
4182        if (!alpha2 || strlen(alpha2) != 2)
4183                return false;
4184
4185        if (isalpha(alpha2[0]) && isalpha(alpha2[1]))
4186                return true;
4187
4188        return false;
4189}
4190
4191static const struct wiphy_coalesce_support mwifiex_coalesce_support = {
4192        .n_rules = MWIFIEX_COALESCE_MAX_RULES,
4193        .max_delay = MWIFIEX_MAX_COALESCING_DELAY,
4194        .n_patterns = MWIFIEX_COALESCE_MAX_FILTERS,
4195        .pattern_min_len = 1,
4196        .pattern_max_len = MWIFIEX_MAX_PATTERN_LEN,
4197        .max_pkt_offset = MWIFIEX_MAX_OFFSET_LEN,
4198};
4199
4200int mwifiex_init_channel_scan_gap(struct mwifiex_adapter *adapter)
4201{
4202        u32 n_channels_bg, n_channels_a = 0;
4203
4204        n_channels_bg = mwifiex_band_2ghz.n_channels;
4205
4206        if (adapter->config_bands & BAND_A)
4207                n_channels_a = mwifiex_band_5ghz.n_channels;
4208
4209        adapter->num_in_chan_stats = n_channels_bg + n_channels_a;
4210        adapter->chan_stats = vmalloc(sizeof(*adapter->chan_stats) *
4211                                      adapter->num_in_chan_stats);
4212
4213        if (!adapter->chan_stats)
4214                return -ENOMEM;
4215
4216        return 0;
4217}
4218
4219/*
4220 * This function registers the device with CFG802.11 subsystem.
4221 *
4222 * The function creates the wireless device/wiphy, populates it with
4223 * default parameters and handler function pointers, and finally
4224 * registers the device.
4225 */
4226
4227int mwifiex_register_cfg80211(struct mwifiex_adapter *adapter)
4228{
4229        int ret;
4230        void *wdev_priv;
4231        struct wiphy *wiphy;
4232        struct mwifiex_private *priv = adapter->priv[MWIFIEX_BSS_TYPE_STA];
4233        u8 *country_code;
4234        u32 thr, retry;
4235
4236        /* create a new wiphy for use with cfg80211 */
4237        wiphy = wiphy_new(&mwifiex_cfg80211_ops,
4238                          sizeof(struct mwifiex_adapter *));
4239        if (!wiphy) {
4240                mwifiex_dbg(adapter, ERROR,
4241                            "%s: creating new wiphy\n", __func__);
4242                return -ENOMEM;
4243        }
4244        wiphy->max_scan_ssids = MWIFIEX_MAX_SSID_LIST_LENGTH;
4245        wiphy->max_scan_ie_len = MWIFIEX_MAX_VSIE_LEN;
4246        wiphy->mgmt_stypes = mwifiex_mgmt_stypes;
4247        wiphy->max_remain_on_channel_duration = 5000;
4248        wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
4249                                 BIT(NL80211_IFTYPE_ADHOC) |
4250                                 BIT(NL80211_IFTYPE_P2P_CLIENT) |
4251                                 BIT(NL80211_IFTYPE_P2P_GO) |
4252                                 BIT(NL80211_IFTYPE_AP);
4253
4254        wiphy->bands[NL80211_BAND_2GHZ] = &mwifiex_band_2ghz;
4255        if (adapter->config_bands & BAND_A)
4256                wiphy->bands[NL80211_BAND_5GHZ] = &mwifiex_band_5ghz;
4257        else
4258                wiphy->bands[NL80211_BAND_5GHZ] = NULL;
4259
4260        if (adapter->drcs_enabled && ISSUPP_DRCS_ENABLED(adapter->fw_cap_info))
4261                wiphy->iface_combinations = &mwifiex_iface_comb_ap_sta_drcs;
4262        else if (adapter->is_hw_11ac_capable)
4263                wiphy->iface_combinations = &mwifiex_iface_comb_ap_sta_vht;
4264        else
4265                wiphy->iface_combinations = &mwifiex_iface_comb_ap_sta;
4266        wiphy->n_iface_combinations = 1;
4267
4268        /* Initialize cipher suits */
4269        wiphy->cipher_suites = mwifiex_cipher_suites;
4270        wiphy->n_cipher_suites = ARRAY_SIZE(mwifiex_cipher_suites);
4271
4272        if (adapter->regd) {
4273                wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
4274                                           REGULATORY_DISABLE_BEACON_HINTS |
4275                                           REGULATORY_COUNTRY_IE_IGNORE;
4276                wiphy_apply_custom_regulatory(wiphy, adapter->regd);
4277        }
4278
4279        ether_addr_copy(wiphy->perm_addr, adapter->perm_addr);
4280        wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
4281        wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
4282                        WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD |
4283                        WIPHY_FLAG_AP_UAPSD |
4284                        WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
4285                        WIPHY_FLAG_HAS_CHANNEL_SWITCH |
4286                        WIPHY_FLAG_PS_ON_BY_DEFAULT;
4287
4288        if (ISSUPP_TDLS_ENABLED(adapter->fw_cap_info))
4289                wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
4290                                WIPHY_FLAG_TDLS_EXTERNAL_SETUP;
4291
4292#ifdef CONFIG_PM
4293        wiphy->wowlan = &mwifiex_wowlan_support;
4294#endif
4295
4296        wiphy->coalesce = &mwifiex_coalesce_support;
4297
4298        wiphy->probe_resp_offload = NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
4299                                    NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
4300                                    NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
4301
4302        wiphy->max_sched_scan_reqs = 1;
4303        wiphy->max_sched_scan_ssids = MWIFIEX_MAX_SSID_LIST_LENGTH;
4304        wiphy->max_sched_scan_ie_len = MWIFIEX_MAX_VSIE_LEN;
4305        wiphy->max_match_sets = MWIFIEX_MAX_SSID_LIST_LENGTH;
4306
4307        wiphy->available_antennas_tx = BIT(adapter->number_of_antenna) - 1;
4308        wiphy->available_antennas_rx = BIT(adapter->number_of_antenna) - 1;
4309
4310        wiphy->features |= NL80211_FEATURE_HT_IBSS |
4311                           NL80211_FEATURE_INACTIVITY_TIMER |
4312                           NL80211_FEATURE_LOW_PRIORITY_SCAN |
4313                           NL80211_FEATURE_NEED_OBSS_SCAN |
4314                           NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
4315                           NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
4316                           NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
4317
4318        if (ISSUPP_TDLS_ENABLED(adapter->fw_cap_info))
4319                wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
4320
4321        if (adapter->fw_api_ver == MWIFIEX_FW_V15)
4322                wiphy->features |= NL80211_FEATURE_SK_TX_STATUS;
4323
4324        /* Reserve space for mwifiex specific private data for BSS */
4325        wiphy->bss_priv_size = sizeof(struct mwifiex_bss_priv);
4326
4327        wiphy->reg_notifier = mwifiex_reg_notifier;
4328
4329        /* Set struct mwifiex_adapter pointer in wiphy_priv */
4330        wdev_priv = wiphy_priv(wiphy);
4331        *(unsigned long *)wdev_priv = (unsigned long)adapter;
4332
4333        set_wiphy_dev(wiphy, priv->adapter->dev);
4334
4335        ret = wiphy_register(wiphy);
4336        if (ret < 0) {
4337                mwifiex_dbg(adapter, ERROR,
4338                            "%s: wiphy_register failed: %d\n", __func__, ret);
4339                wiphy_free(wiphy);
4340                return ret;
4341        }
4342
4343        if (!adapter->regd) {
4344                if (reg_alpha2 && mwifiex_is_valid_alpha2(reg_alpha2)) {
4345                        mwifiex_dbg(adapter, INFO,
4346                                    "driver hint alpha2: %2.2s\n", reg_alpha2);
4347                        regulatory_hint(wiphy, reg_alpha2);
4348                } else {
4349                        if (adapter->region_code == 0x00) {
4350                                mwifiex_dbg(adapter, WARN,
4351                                            "Ignore world regulatory domain\n");
4352                        } else {
4353                                wiphy->regulatory_flags |=
4354                                        REGULATORY_DISABLE_BEACON_HINTS |
4355                                        REGULATORY_COUNTRY_IE_IGNORE;
4356                                country_code =
4357                                        mwifiex_11d_code_2_region(
4358                                                adapter->region_code);
4359                                if (country_code &&
4360                                    regulatory_hint(wiphy, country_code))
4361                                        mwifiex_dbg(priv->adapter, ERROR,
4362                                                    "regulatory_hint() failed\n");
4363                        }
4364                }
4365        }
4366
4367        mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
4368                         HostCmd_ACT_GEN_GET, FRAG_THRESH_I, &thr, true);
4369        wiphy->frag_threshold = thr;
4370        mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
4371                         HostCmd_ACT_GEN_GET, RTS_THRESH_I, &thr, true);
4372        wiphy->rts_threshold = thr;
4373        mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
4374                         HostCmd_ACT_GEN_GET, SHORT_RETRY_LIM_I, &retry, true);
4375        wiphy->retry_short = (u8) retry;
4376        mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
4377                         HostCmd_ACT_GEN_GET, LONG_RETRY_LIM_I, &retry, true);
4378        wiphy->retry_long = (u8) retry;
4379
4380        adapter->wiphy = wiphy;
4381        return ret;
4382}
4383