linux/drivers/net/wireless/mwifiex/sta_ioctl.c
<<
>>
Prefs
   1/*
   2 * Marvell Wireless LAN device driver: functions for station ioctl
   3 *
   4 * Copyright (C) 2011, 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 "decl.h"
  21#include "ioctl.h"
  22#include "util.h"
  23#include "fw.h"
  24#include "main.h"
  25#include "wmm.h"
  26#include "11n.h"
  27#include "cfg80211.h"
  28
  29static int disconnect_on_suspend = 1;
  30module_param(disconnect_on_suspend, int, 0644);
  31
  32/*
  33 * Copies the multicast address list from device to driver.
  34 *
  35 * This function does not validate the destination memory for
  36 * size, and the calling function must ensure enough memory is
  37 * available.
  38 */
  39int mwifiex_copy_mcast_addr(struct mwifiex_multicast_list *mlist,
  40                            struct net_device *dev)
  41{
  42        int i = 0;
  43        struct netdev_hw_addr *ha;
  44
  45        netdev_for_each_mc_addr(ha, dev)
  46                memcpy(&mlist->mac_list[i++], ha->addr, ETH_ALEN);
  47
  48        return i;
  49}
  50
  51/*
  52 * Wait queue completion handler.
  53 *
  54 * This function waits on a cmd wait queue. It also cancels the pending
  55 * request after waking up, in case of errors.
  56 */
  57int mwifiex_wait_queue_complete(struct mwifiex_adapter *adapter)
  58{
  59        bool cancel_flag = false;
  60        int status;
  61        struct cmd_ctrl_node *cmd_queued;
  62
  63        if (!adapter->cmd_queued)
  64                return 0;
  65
  66        cmd_queued = adapter->cmd_queued;
  67        adapter->cmd_queued = NULL;
  68
  69        dev_dbg(adapter->dev, "cmd pending\n");
  70        atomic_inc(&adapter->cmd_pending);
  71
  72        /* Wait for completion */
  73        wait_event_interruptible(adapter->cmd_wait_q.wait,
  74                                 *(cmd_queued->condition));
  75        if (!*(cmd_queued->condition))
  76                cancel_flag = true;
  77
  78        if (cancel_flag) {
  79                mwifiex_cancel_pending_ioctl(adapter);
  80                dev_dbg(adapter->dev, "cmd cancel\n");
  81        }
  82
  83        status = adapter->cmd_wait_q.status;
  84        adapter->cmd_wait_q.status = 0;
  85
  86        return status;
  87}
  88
  89/*
  90 * This function prepares the correct firmware command and
  91 * issues it to set the multicast list.
  92 *
  93 * This function can be used to enable promiscuous mode, or enable all
  94 * multicast packets, or to enable selective multicast.
  95 */
  96int mwifiex_request_set_multicast_list(struct mwifiex_private *priv,
  97                                struct mwifiex_multicast_list *mcast_list)
  98{
  99        int ret = 0;
 100        u16 old_pkt_filter;
 101
 102        old_pkt_filter = priv->curr_pkt_filter;
 103
 104        if (mcast_list->mode == MWIFIEX_PROMISC_MODE) {
 105                dev_dbg(priv->adapter->dev, "info: Enable Promiscuous mode\n");
 106                priv->curr_pkt_filter |= HostCmd_ACT_MAC_PROMISCUOUS_ENABLE;
 107                priv->curr_pkt_filter &=
 108                        ~HostCmd_ACT_MAC_ALL_MULTICAST_ENABLE;
 109        } else {
 110                /* Multicast */
 111                priv->curr_pkt_filter &= ~HostCmd_ACT_MAC_PROMISCUOUS_ENABLE;
 112                if (mcast_list->mode == MWIFIEX_MULTICAST_MODE) {
 113                        dev_dbg(priv->adapter->dev,
 114                                "info: Enabling All Multicast!\n");
 115                        priv->curr_pkt_filter |=
 116                                HostCmd_ACT_MAC_ALL_MULTICAST_ENABLE;
 117                } else {
 118                        priv->curr_pkt_filter &=
 119                                ~HostCmd_ACT_MAC_ALL_MULTICAST_ENABLE;
 120                        if (mcast_list->num_multicast_addr) {
 121                                dev_dbg(priv->adapter->dev,
 122                                        "info: Set multicast list=%d\n",
 123                                       mcast_list->num_multicast_addr);
 124                                /* Set multicast addresses to firmware */
 125                                if (old_pkt_filter == priv->curr_pkt_filter) {
 126                                        /* Send request to firmware */
 127                                        ret = mwifiex_send_cmd_async(priv,
 128                                                HostCmd_CMD_MAC_MULTICAST_ADR,
 129                                                HostCmd_ACT_GEN_SET, 0,
 130                                                mcast_list);
 131                                } else {
 132                                        /* Send request to firmware */
 133                                        ret = mwifiex_send_cmd_async(priv,
 134                                                HostCmd_CMD_MAC_MULTICAST_ADR,
 135                                                HostCmd_ACT_GEN_SET, 0,
 136                                                mcast_list);
 137                                }
 138                        }
 139                }
 140        }
 141        dev_dbg(priv->adapter->dev,
 142                "info: old_pkt_filter=%#x, curr_pkt_filter=%#x\n",
 143               old_pkt_filter, priv->curr_pkt_filter);
 144        if (old_pkt_filter != priv->curr_pkt_filter) {
 145                ret = mwifiex_send_cmd_async(priv, HostCmd_CMD_MAC_CONTROL,
 146                                             HostCmd_ACT_GEN_SET,
 147                                             0, &priv->curr_pkt_filter);
 148        }
 149
 150        return ret;
 151}
 152
 153/*
 154 * This function fills bss descriptor structure using provided
 155 * information.
 156 */
 157int mwifiex_fill_new_bss_desc(struct mwifiex_private *priv,
 158                              struct cfg80211_bss *bss,
 159                              struct mwifiex_bssdescriptor *bss_desc)
 160{
 161        int ret;
 162        u8 *beacon_ie;
 163        struct mwifiex_bss_priv *bss_priv = (void *)bss->priv;
 164
 165        beacon_ie = kmemdup(bss->information_elements, bss->len_beacon_ies,
 166                            GFP_KERNEL);
 167        if (!beacon_ie) {
 168                dev_err(priv->adapter->dev, " failed to alloc beacon_ie\n");
 169                return -ENOMEM;
 170        }
 171
 172        memcpy(bss_desc->mac_address, bss->bssid, ETH_ALEN);
 173        bss_desc->rssi = bss->signal;
 174        bss_desc->beacon_buf = beacon_ie;
 175        bss_desc->beacon_buf_size = bss->len_beacon_ies;
 176        bss_desc->beacon_period = bss->beacon_interval;
 177        bss_desc->cap_info_bitmap = bss->capability;
 178        bss_desc->bss_band = bss_priv->band;
 179        bss_desc->fw_tsf = bss_priv->fw_tsf;
 180        bss_desc->timestamp = bss->tsf;
 181        if (bss_desc->cap_info_bitmap & WLAN_CAPABILITY_PRIVACY) {
 182                dev_dbg(priv->adapter->dev, "info: InterpretIE: AP WEP enabled\n");
 183                bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_8021X_WEP;
 184        } else {
 185                bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_ACCEPT_ALL;
 186        }
 187        if (bss_desc->cap_info_bitmap & WLAN_CAPABILITY_IBSS)
 188                bss_desc->bss_mode = NL80211_IFTYPE_ADHOC;
 189        else
 190                bss_desc->bss_mode = NL80211_IFTYPE_STATION;
 191
 192        ret = mwifiex_update_bss_desc_with_ie(priv->adapter, bss_desc);
 193
 194        kfree(beacon_ie);
 195        return ret;
 196}
 197
 198static int mwifiex_process_country_ie(struct mwifiex_private *priv,
 199                                      struct cfg80211_bss *bss)
 200{
 201        u8 *country_ie, country_ie_len;
 202        struct mwifiex_802_11d_domain_reg *domain_info =
 203                                        &priv->adapter->domain_reg;
 204
 205        country_ie = (u8 *)ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
 206
 207        if (!country_ie)
 208                return 0;
 209
 210        country_ie_len = country_ie[1];
 211        if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
 212                return 0;
 213
 214        domain_info->country_code[0] = country_ie[2];
 215        domain_info->country_code[1] = country_ie[3];
 216        domain_info->country_code[2] = ' ';
 217
 218        country_ie_len -= IEEE80211_COUNTRY_STRING_LEN;
 219
 220        domain_info->no_of_triplet =
 221                country_ie_len / sizeof(struct ieee80211_country_ie_triplet);
 222
 223        memcpy((u8 *)domain_info->triplet,
 224               &country_ie[2] + IEEE80211_COUNTRY_STRING_LEN, country_ie_len);
 225
 226        if (mwifiex_send_cmd_async(priv, HostCmd_CMD_802_11D_DOMAIN_INFO,
 227                                   HostCmd_ACT_GEN_SET, 0, NULL)) {
 228                wiphy_err(priv->adapter->wiphy,
 229                          "11D: setting domain info in FW\n");
 230                return -1;
 231        }
 232
 233        return 0;
 234}
 235
 236/*
 237 * In Ad-Hoc mode, the IBSS is created if not found in scan list.
 238 * In both Ad-Hoc and infra mode, an deauthentication is performed
 239 * first.
 240 */
 241int mwifiex_bss_start(struct mwifiex_private *priv, struct cfg80211_bss *bss,
 242                      struct cfg80211_ssid *req_ssid)
 243{
 244        int ret;
 245        struct mwifiex_adapter *adapter = priv->adapter;
 246        struct mwifiex_bssdescriptor *bss_desc = NULL;
 247
 248        priv->scan_block = false;
 249
 250        if (bss) {
 251                mwifiex_process_country_ie(priv, bss);
 252
 253                /* Allocate and fill new bss descriptor */
 254                bss_desc = kzalloc(sizeof(struct mwifiex_bssdescriptor),
 255                                GFP_KERNEL);
 256                if (!bss_desc) {
 257                        dev_err(priv->adapter->dev, " failed to alloc bss_desc\n");
 258                        return -ENOMEM;
 259                }
 260
 261                ret = mwifiex_fill_new_bss_desc(priv, bss, bss_desc);
 262                if (ret)
 263                        goto done;
 264        }
 265
 266        if (priv->bss_mode == NL80211_IFTYPE_STATION) {
 267                /* Infra mode */
 268                ret = mwifiex_deauthenticate(priv, NULL);
 269                if (ret)
 270                        goto done;
 271
 272                ret = mwifiex_check_network_compatibility(priv, bss_desc);
 273                if (ret)
 274                        goto done;
 275
 276                dev_dbg(adapter->dev, "info: SSID found in scan list ... "
 277                                      "associating...\n");
 278
 279                if (!netif_queue_stopped(priv->netdev))
 280                        mwifiex_stop_net_dev_queue(priv->netdev, adapter);
 281                if (netif_carrier_ok(priv->netdev))
 282                        netif_carrier_off(priv->netdev);
 283
 284                /* Clear any past association response stored for
 285                 * application retrieval */
 286                priv->assoc_rsp_size = 0;
 287                ret = mwifiex_associate(priv, bss_desc);
 288
 289                /* If auth type is auto and association fails using open mode,
 290                 * try to connect using shared mode */
 291                if (ret == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
 292                    priv->sec_info.is_authtype_auto &&
 293                    priv->sec_info.wep_enabled) {
 294                        priv->sec_info.authentication_mode =
 295                                                NL80211_AUTHTYPE_SHARED_KEY;
 296                        ret = mwifiex_associate(priv, bss_desc);
 297                }
 298
 299                if (bss)
 300                        cfg80211_put_bss(bss);
 301        } else {
 302                /* Adhoc mode */
 303                /* If the requested SSID matches current SSID, return */
 304                if (bss_desc && bss_desc->ssid.ssid_len &&
 305                    (!mwifiex_ssid_cmp(&priv->curr_bss_params.bss_descriptor.
 306                                       ssid, &bss_desc->ssid))) {
 307                        kfree(bss_desc);
 308                        return 0;
 309                }
 310
 311                /* Exit Adhoc mode first */
 312                dev_dbg(adapter->dev, "info: Sending Adhoc Stop\n");
 313                ret = mwifiex_deauthenticate(priv, NULL);
 314                if (ret)
 315                        goto done;
 316
 317                priv->adhoc_is_link_sensed = false;
 318
 319                ret = mwifiex_check_network_compatibility(priv, bss_desc);
 320
 321                if (!netif_queue_stopped(priv->netdev))
 322                        mwifiex_stop_net_dev_queue(priv->netdev, adapter);
 323                if (netif_carrier_ok(priv->netdev))
 324                        netif_carrier_off(priv->netdev);
 325
 326                if (!ret) {
 327                        dev_dbg(adapter->dev, "info: network found in scan"
 328                                                        " list. Joining...\n");
 329                        ret = mwifiex_adhoc_join(priv, bss_desc);
 330                        if (bss)
 331                                cfg80211_put_bss(bss);
 332                } else {
 333                        dev_dbg(adapter->dev, "info: Network not found in "
 334                                "the list, creating adhoc with ssid = %s\n",
 335                                req_ssid->ssid);
 336                        ret = mwifiex_adhoc_start(priv, req_ssid);
 337                }
 338        }
 339
 340done:
 341        kfree(bss_desc);
 342        return ret;
 343}
 344
 345/*
 346 * IOCTL request handler to set host sleep configuration.
 347 *
 348 * This function prepares the correct firmware command and
 349 * issues it.
 350 */
 351static int mwifiex_set_hs_params(struct mwifiex_private *priv, u16 action,
 352                                 int cmd_type, struct mwifiex_ds_hs_cfg *hs_cfg)
 353
 354{
 355        struct mwifiex_adapter *adapter = priv->adapter;
 356        int status = 0;
 357        u32 prev_cond = 0;
 358
 359        if (!hs_cfg)
 360                return -ENOMEM;
 361
 362        switch (action) {
 363        case HostCmd_ACT_GEN_SET:
 364                if (adapter->pps_uapsd_mode) {
 365                        dev_dbg(adapter->dev, "info: Host Sleep IOCTL"
 366                                " is blocked in UAPSD/PPS mode\n");
 367                        status = -1;
 368                        break;
 369                }
 370                if (hs_cfg->is_invoke_hostcmd) {
 371                        if (hs_cfg->conditions == HOST_SLEEP_CFG_CANCEL) {
 372                                if (!adapter->is_hs_configured)
 373                                        /* Already cancelled */
 374                                        break;
 375                                /* Save previous condition */
 376                                prev_cond = le32_to_cpu(adapter->hs_cfg
 377                                                        .conditions);
 378                                adapter->hs_cfg.conditions =
 379                                                cpu_to_le32(hs_cfg->conditions);
 380                        } else if (hs_cfg->conditions) {
 381                                adapter->hs_cfg.conditions =
 382                                                cpu_to_le32(hs_cfg->conditions);
 383                                adapter->hs_cfg.gpio = (u8)hs_cfg->gpio;
 384                                if (hs_cfg->gap)
 385                                        adapter->hs_cfg.gap = (u8)hs_cfg->gap;
 386                        } else if (adapter->hs_cfg.conditions
 387                                   == cpu_to_le32(HOST_SLEEP_CFG_CANCEL)) {
 388                                /* Return failure if no parameters for HS
 389                                   enable */
 390                                status = -1;
 391                                break;
 392                        }
 393                        if (cmd_type == MWIFIEX_SYNC_CMD)
 394                                status = mwifiex_send_cmd_sync(priv,
 395                                                HostCmd_CMD_802_11_HS_CFG_ENH,
 396                                                HostCmd_ACT_GEN_SET, 0,
 397                                                &adapter->hs_cfg);
 398                        else
 399                                status = mwifiex_send_cmd_async(priv,
 400                                                HostCmd_CMD_802_11_HS_CFG_ENH,
 401                                                HostCmd_ACT_GEN_SET, 0,
 402                                                &adapter->hs_cfg);
 403                        if (hs_cfg->conditions == HOST_SLEEP_CFG_CANCEL)
 404                                /* Restore previous condition */
 405                                adapter->hs_cfg.conditions =
 406                                                cpu_to_le32(prev_cond);
 407                } else {
 408                        adapter->hs_cfg.conditions =
 409                                                cpu_to_le32(hs_cfg->conditions);
 410                        adapter->hs_cfg.gpio = (u8)hs_cfg->gpio;
 411                        adapter->hs_cfg.gap = (u8)hs_cfg->gap;
 412                }
 413                break;
 414        case HostCmd_ACT_GEN_GET:
 415                hs_cfg->conditions = le32_to_cpu(adapter->hs_cfg.conditions);
 416                hs_cfg->gpio = adapter->hs_cfg.gpio;
 417                hs_cfg->gap = adapter->hs_cfg.gap;
 418                break;
 419        default:
 420                status = -1;
 421                break;
 422        }
 423
 424        return status;
 425}
 426
 427/*
 428 * Sends IOCTL request to cancel the existing Host Sleep configuration.
 429 *
 430 * This function allocates the IOCTL request buffer, fills it
 431 * with requisite parameters and calls the IOCTL handler.
 432 */
 433int mwifiex_cancel_hs(struct mwifiex_private *priv, int cmd_type)
 434{
 435        struct mwifiex_ds_hs_cfg hscfg;
 436
 437        hscfg.conditions = HOST_SLEEP_CFG_CANCEL;
 438        hscfg.is_invoke_hostcmd = true;
 439
 440        return mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_SET,
 441                                    cmd_type, &hscfg);
 442}
 443EXPORT_SYMBOL_GPL(mwifiex_cancel_hs);
 444
 445/*
 446 * Sends IOCTL request to cancel the existing Host Sleep configuration.
 447 *
 448 * This function allocates the IOCTL request buffer, fills it
 449 * with requisite parameters and calls the IOCTL handler.
 450 */
 451int mwifiex_enable_hs(struct mwifiex_adapter *adapter)
 452{
 453        struct mwifiex_ds_hs_cfg hscfg;
 454        struct mwifiex_private *priv;
 455        int i;
 456
 457        if (disconnect_on_suspend) {
 458                for (i = 0; i < adapter->priv_num; i++) {
 459                        priv = adapter->priv[i];
 460                        if (priv)
 461                                mwifiex_deauthenticate(priv, NULL);
 462                }
 463        }
 464
 465        if (adapter->hs_activated) {
 466                dev_dbg(adapter->dev, "cmd: HS Already actived\n");
 467                return true;
 468        }
 469
 470        adapter->hs_activate_wait_q_woken = false;
 471
 472        memset(&hscfg, 0, sizeof(struct mwifiex_ds_hs_cfg));
 473        hscfg.is_invoke_hostcmd = true;
 474
 475        if (mwifiex_set_hs_params(mwifiex_get_priv(adapter,
 476                                                   MWIFIEX_BSS_ROLE_STA),
 477                                  HostCmd_ACT_GEN_SET, MWIFIEX_SYNC_CMD,
 478                                  &hscfg)) {
 479                dev_err(adapter->dev, "IOCTL request HS enable failed\n");
 480                return false;
 481        }
 482
 483        wait_event_interruptible(adapter->hs_activate_wait_q,
 484                                 adapter->hs_activate_wait_q_woken);
 485
 486        return true;
 487}
 488EXPORT_SYMBOL_GPL(mwifiex_enable_hs);
 489
 490/*
 491 * IOCTL request handler to get BSS information.
 492 *
 493 * This function collates the information from different driver structures
 494 * to send to the user.
 495 */
 496int mwifiex_get_bss_info(struct mwifiex_private *priv,
 497                         struct mwifiex_bss_info *info)
 498{
 499        struct mwifiex_adapter *adapter = priv->adapter;
 500        struct mwifiex_bssdescriptor *bss_desc;
 501
 502        if (!info)
 503                return -1;
 504
 505        bss_desc = &priv->curr_bss_params.bss_descriptor;
 506
 507        info->bss_mode = priv->bss_mode;
 508
 509        memcpy(&info->ssid, &bss_desc->ssid, sizeof(struct cfg80211_ssid));
 510
 511        memcpy(&info->bssid, &bss_desc->mac_address, ETH_ALEN);
 512
 513        info->bss_chan = bss_desc->channel;
 514
 515        memcpy(info->country_code, adapter->country_code,
 516               IEEE80211_COUNTRY_STRING_LEN);
 517
 518        info->media_connected = priv->media_connected;
 519
 520        info->max_power_level = priv->max_tx_power_level;
 521        info->min_power_level = priv->min_tx_power_level;
 522
 523        info->adhoc_state = priv->adhoc_state;
 524
 525        info->bcn_nf_last = priv->bcn_nf_last;
 526
 527        if (priv->sec_info.wep_enabled)
 528                info->wep_status = true;
 529        else
 530                info->wep_status = false;
 531
 532        info->is_hs_configured = adapter->is_hs_configured;
 533        info->is_deep_sleep = adapter->is_deep_sleep;
 534
 535        return 0;
 536}
 537
 538/*
 539 * The function disables auto deep sleep mode.
 540 */
 541int mwifiex_disable_auto_ds(struct mwifiex_private *priv)
 542{
 543        struct mwifiex_ds_auto_ds auto_ds;
 544
 545        auto_ds.auto_ds = DEEP_SLEEP_OFF;
 546
 547        return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_PS_MODE_ENH,
 548                                     DIS_AUTO_PS, BITMAP_AUTO_DS, &auto_ds);
 549}
 550EXPORT_SYMBOL_GPL(mwifiex_disable_auto_ds);
 551
 552/*
 553 * Sends IOCTL request to get the data rate.
 554 *
 555 * This function allocates the IOCTL request buffer, fills it
 556 * with requisite parameters and calls the IOCTL handler.
 557 */
 558int mwifiex_drv_get_data_rate(struct mwifiex_private *priv, u32 *rate)
 559{
 560        int ret;
 561
 562        ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_TX_RATE_QUERY,
 563                                    HostCmd_ACT_GEN_GET, 0, NULL);
 564
 565        if (!ret) {
 566                if (priv->is_data_rate_auto)
 567                        *rate = mwifiex_index_to_data_rate(priv, priv->tx_rate,
 568                                                           priv->tx_htinfo);
 569                else
 570                        *rate = priv->data_rate;
 571        }
 572
 573        return ret;
 574}
 575
 576/*
 577 * IOCTL request handler to set tx power configuration.
 578 *
 579 * This function prepares the correct firmware command and
 580 * issues it.
 581 *
 582 * For non-auto power mode, all the following power groups are set -
 583 *      - Modulation class HR/DSSS
 584 *      - Modulation class OFDM
 585 *      - Modulation class HTBW20
 586 *      - Modulation class HTBW40
 587 */
 588int mwifiex_set_tx_power(struct mwifiex_private *priv,
 589                         struct mwifiex_power_cfg *power_cfg)
 590{
 591        int ret;
 592        struct host_cmd_ds_txpwr_cfg *txp_cfg;
 593        struct mwifiex_types_power_group *pg_tlv;
 594        struct mwifiex_power_group *pg;
 595        u8 *buf;
 596        u16 dbm = 0;
 597
 598        if (!power_cfg->is_power_auto) {
 599                dbm = (u16) power_cfg->power_level;
 600                if ((dbm < priv->min_tx_power_level) ||
 601                    (dbm > priv->max_tx_power_level)) {
 602                        dev_err(priv->adapter->dev, "txpower value %d dBm"
 603                                " is out of range (%d dBm-%d dBm)\n",
 604                                dbm, priv->min_tx_power_level,
 605                                priv->max_tx_power_level);
 606                        return -1;
 607                }
 608        }
 609        buf = kzalloc(MWIFIEX_SIZE_OF_CMD_BUFFER, GFP_KERNEL);
 610        if (!buf) {
 611                dev_err(priv->adapter->dev, "%s: failed to alloc cmd buffer\n",
 612                        __func__);
 613                return -ENOMEM;
 614        }
 615
 616        txp_cfg = (struct host_cmd_ds_txpwr_cfg *) buf;
 617        txp_cfg->action = cpu_to_le16(HostCmd_ACT_GEN_SET);
 618        if (!power_cfg->is_power_auto) {
 619                txp_cfg->mode = cpu_to_le32(1);
 620                pg_tlv = (struct mwifiex_types_power_group *)
 621                         (buf + sizeof(struct host_cmd_ds_txpwr_cfg));
 622                pg_tlv->type = TLV_TYPE_POWER_GROUP;
 623                pg_tlv->length = 4 * sizeof(struct mwifiex_power_group);
 624                pg = (struct mwifiex_power_group *)
 625                     (buf + sizeof(struct host_cmd_ds_txpwr_cfg)
 626                      + sizeof(struct mwifiex_types_power_group));
 627                /* Power group for modulation class HR/DSSS */
 628                pg->first_rate_code = 0x00;
 629                pg->last_rate_code = 0x03;
 630                pg->modulation_class = MOD_CLASS_HR_DSSS;
 631                pg->power_step = 0;
 632                pg->power_min = (s8) dbm;
 633                pg->power_max = (s8) dbm;
 634                pg++;
 635                /* Power group for modulation class OFDM */
 636                pg->first_rate_code = 0x00;
 637                pg->last_rate_code = 0x07;
 638                pg->modulation_class = MOD_CLASS_OFDM;
 639                pg->power_step = 0;
 640                pg->power_min = (s8) dbm;
 641                pg->power_max = (s8) dbm;
 642                pg++;
 643                /* Power group for modulation class HTBW20 */
 644                pg->first_rate_code = 0x00;
 645                pg->last_rate_code = 0x20;
 646                pg->modulation_class = MOD_CLASS_HT;
 647                pg->power_step = 0;
 648                pg->power_min = (s8) dbm;
 649                pg->power_max = (s8) dbm;
 650                pg->ht_bandwidth = HT_BW_20;
 651                pg++;
 652                /* Power group for modulation class HTBW40 */
 653                pg->first_rate_code = 0x00;
 654                pg->last_rate_code = 0x20;
 655                pg->modulation_class = MOD_CLASS_HT;
 656                pg->power_step = 0;
 657                pg->power_min = (s8) dbm;
 658                pg->power_max = (s8) dbm;
 659                pg->ht_bandwidth = HT_BW_40;
 660        }
 661        ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_TXPWR_CFG,
 662                                    HostCmd_ACT_GEN_SET, 0, buf);
 663
 664        kfree(buf);
 665        return ret;
 666}
 667
 668/*
 669 * IOCTL request handler to get power save mode.
 670 *
 671 * This function prepares the correct firmware command and
 672 * issues it.
 673 */
 674int mwifiex_drv_set_power(struct mwifiex_private *priv, u32 *ps_mode)
 675{
 676        int ret;
 677        struct mwifiex_adapter *adapter = priv->adapter;
 678        u16 sub_cmd;
 679
 680        if (*ps_mode)
 681                adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
 682        else
 683                adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
 684        sub_cmd = (*ps_mode) ? EN_AUTO_PS : DIS_AUTO_PS;
 685        ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_PS_MODE_ENH,
 686                                    sub_cmd, BITMAP_STA_PS, NULL);
 687        if ((!ret) && (sub_cmd == DIS_AUTO_PS))
 688                ret = mwifiex_send_cmd_async(priv,
 689                                             HostCmd_CMD_802_11_PS_MODE_ENH,
 690                                             GET_PS, 0, NULL);
 691
 692        return ret;
 693}
 694
 695/*
 696 * IOCTL request handler to set/reset WPA IE.
 697 *
 698 * The supplied WPA IE is treated as a opaque buffer. Only the first field
 699 * is checked to determine WPA version. If buffer length is zero, the existing
 700 * WPA IE is reset.
 701 */
 702static int mwifiex_set_wpa_ie_helper(struct mwifiex_private *priv,
 703                                     u8 *ie_data_ptr, u16 ie_len)
 704{
 705        if (ie_len) {
 706                if (ie_len > sizeof(priv->wpa_ie)) {
 707                        dev_err(priv->adapter->dev,
 708                                "failed to copy WPA IE, too big\n");
 709                        return -1;
 710                }
 711                memcpy(priv->wpa_ie, ie_data_ptr, ie_len);
 712                priv->wpa_ie_len = (u8) ie_len;
 713                dev_dbg(priv->adapter->dev, "cmd: Set Wpa_ie_len=%d IE=%#x\n",
 714                        priv->wpa_ie_len, priv->wpa_ie[0]);
 715
 716                if (priv->wpa_ie[0] == WLAN_EID_WPA) {
 717                        priv->sec_info.wpa_enabled = true;
 718                } else if (priv->wpa_ie[0] == WLAN_EID_RSN) {
 719                        priv->sec_info.wpa2_enabled = true;
 720                } else {
 721                        priv->sec_info.wpa_enabled = false;
 722                        priv->sec_info.wpa2_enabled = false;
 723                }
 724        } else {
 725                memset(priv->wpa_ie, 0, sizeof(priv->wpa_ie));
 726                priv->wpa_ie_len = 0;
 727                dev_dbg(priv->adapter->dev, "info: reset wpa_ie_len=%d IE=%#x\n",
 728                        priv->wpa_ie_len, priv->wpa_ie[0]);
 729                priv->sec_info.wpa_enabled = false;
 730                priv->sec_info.wpa2_enabled = false;
 731        }
 732
 733        return 0;
 734}
 735
 736/*
 737 * IOCTL request handler to set/reset WAPI IE.
 738 *
 739 * The supplied WAPI IE is treated as a opaque buffer. Only the first field
 740 * is checked to internally enable WAPI. If buffer length is zero, the existing
 741 * WAPI IE is reset.
 742 */
 743static int mwifiex_set_wapi_ie(struct mwifiex_private *priv,
 744                               u8 *ie_data_ptr, u16 ie_len)
 745{
 746        if (ie_len) {
 747                if (ie_len > sizeof(priv->wapi_ie)) {
 748                        dev_dbg(priv->adapter->dev,
 749                                "info: failed to copy WAPI IE, too big\n");
 750                        return -1;
 751                }
 752                memcpy(priv->wapi_ie, ie_data_ptr, ie_len);
 753                priv->wapi_ie_len = ie_len;
 754                dev_dbg(priv->adapter->dev, "cmd: Set wapi_ie_len=%d IE=%#x\n",
 755                        priv->wapi_ie_len, priv->wapi_ie[0]);
 756
 757                if (priv->wapi_ie[0] == WLAN_EID_BSS_AC_ACCESS_DELAY)
 758                        priv->sec_info.wapi_enabled = true;
 759        } else {
 760                memset(priv->wapi_ie, 0, sizeof(priv->wapi_ie));
 761                priv->wapi_ie_len = ie_len;
 762                dev_dbg(priv->adapter->dev,
 763                        "info: Reset wapi_ie_len=%d IE=%#x\n",
 764                       priv->wapi_ie_len, priv->wapi_ie[0]);
 765                priv->sec_info.wapi_enabled = false;
 766        }
 767        return 0;
 768}
 769
 770/*
 771 * IOCTL request handler to set/reset WPS IE.
 772 *
 773 * The supplied WPS IE is treated as a opaque buffer. Only the first field
 774 * is checked to internally enable WPS. If buffer length is zero, the existing
 775 * WPS IE is reset.
 776 */
 777static int mwifiex_set_wps_ie(struct mwifiex_private *priv,
 778                               u8 *ie_data_ptr, u16 ie_len)
 779{
 780        if (ie_len) {
 781                priv->wps_ie = kzalloc(MWIFIEX_MAX_VSIE_LEN, GFP_KERNEL);
 782                if (!priv->wps_ie)
 783                        return -ENOMEM;
 784                if (ie_len > sizeof(priv->wps_ie)) {
 785                        dev_dbg(priv->adapter->dev,
 786                                "info: failed to copy WPS IE, too big\n");
 787                        kfree(priv->wps_ie);
 788                        return -1;
 789                }
 790                memcpy(priv->wps_ie, ie_data_ptr, ie_len);
 791                priv->wps_ie_len = ie_len;
 792                dev_dbg(priv->adapter->dev, "cmd: Set wps_ie_len=%d IE=%#x\n",
 793                        priv->wps_ie_len, priv->wps_ie[0]);
 794        } else {
 795                kfree(priv->wps_ie);
 796                priv->wps_ie_len = ie_len;
 797                dev_dbg(priv->adapter->dev,
 798                        "info: Reset wps_ie_len=%d\n", priv->wps_ie_len);
 799        }
 800        return 0;
 801}
 802
 803/*
 804 * IOCTL request handler to set WAPI key.
 805 *
 806 * This function prepares the correct firmware command and
 807 * issues it.
 808 */
 809static int mwifiex_sec_ioctl_set_wapi_key(struct mwifiex_private *priv,
 810                               struct mwifiex_ds_encrypt_key *encrypt_key)
 811{
 812
 813        return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
 814                                     HostCmd_ACT_GEN_SET, KEY_INFO_ENABLED,
 815                                     encrypt_key);
 816}
 817
 818/*
 819 * IOCTL request handler to set WEP network key.
 820 *
 821 * This function prepares the correct firmware command and
 822 * issues it, after validation checks.
 823 */
 824static int mwifiex_sec_ioctl_set_wep_key(struct mwifiex_private *priv,
 825                              struct mwifiex_ds_encrypt_key *encrypt_key)
 826{
 827        int ret;
 828        struct mwifiex_wep_key *wep_key;
 829        int index;
 830
 831        if (priv->wep_key_curr_index >= NUM_WEP_KEYS)
 832                priv->wep_key_curr_index = 0;
 833        wep_key = &priv->wep_key[priv->wep_key_curr_index];
 834        index = encrypt_key->key_index;
 835        if (encrypt_key->key_disable) {
 836                priv->sec_info.wep_enabled = 0;
 837        } else if (!encrypt_key->key_len) {
 838                /* Copy the required key as the current key */
 839                wep_key = &priv->wep_key[index];
 840                if (!wep_key->key_length) {
 841                        dev_err(priv->adapter->dev,
 842                                "key not set, so cannot enable it\n");
 843                        return -1;
 844                }
 845                priv->wep_key_curr_index = (u16) index;
 846                priv->sec_info.wep_enabled = 1;
 847        } else {
 848                wep_key = &priv->wep_key[index];
 849                memset(wep_key, 0, sizeof(struct mwifiex_wep_key));
 850                /* Copy the key in the driver */
 851                memcpy(wep_key->key_material,
 852                       encrypt_key->key_material,
 853                       encrypt_key->key_len);
 854                wep_key->key_index = index;
 855                wep_key->key_length = encrypt_key->key_len;
 856                priv->sec_info.wep_enabled = 1;
 857        }
 858        if (wep_key->key_length) {
 859                /* Send request to firmware */
 860                ret = mwifiex_send_cmd_async(priv,
 861                                             HostCmd_CMD_802_11_KEY_MATERIAL,
 862                                             HostCmd_ACT_GEN_SET, 0, NULL);
 863                if (ret)
 864                        return ret;
 865        }
 866        if (priv->sec_info.wep_enabled)
 867                priv->curr_pkt_filter |= HostCmd_ACT_MAC_WEP_ENABLE;
 868        else
 869                priv->curr_pkt_filter &= ~HostCmd_ACT_MAC_WEP_ENABLE;
 870
 871        ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_MAC_CONTROL,
 872                                    HostCmd_ACT_GEN_SET, 0,
 873                                    &priv->curr_pkt_filter);
 874
 875        return ret;
 876}
 877
 878/*
 879 * IOCTL request handler to set WPA key.
 880 *
 881 * This function prepares the correct firmware command and
 882 * issues it, after validation checks.
 883 *
 884 * Current driver only supports key length of up to 32 bytes.
 885 *
 886 * This function can also be used to disable a currently set key.
 887 */
 888static int mwifiex_sec_ioctl_set_wpa_key(struct mwifiex_private *priv,
 889                              struct mwifiex_ds_encrypt_key *encrypt_key)
 890{
 891        int ret;
 892        u8 remove_key = false;
 893        struct host_cmd_ds_802_11_key_material *ibss_key;
 894
 895        /* Current driver only supports key length of up to 32 bytes */
 896        if (encrypt_key->key_len > WLAN_MAX_KEY_LEN) {
 897                dev_err(priv->adapter->dev, "key length too long\n");
 898                return -1;
 899        }
 900
 901        if (priv->bss_mode == NL80211_IFTYPE_ADHOC) {
 902                /*
 903                 * IBSS/WPA-None uses only one key (Group) for both receiving
 904                 * and sending unicast and multicast packets.
 905                 */
 906                /* Send the key as PTK to firmware */
 907                encrypt_key->key_index = MWIFIEX_KEY_INDEX_UNICAST;
 908                ret = mwifiex_send_cmd_async(priv,
 909                                             HostCmd_CMD_802_11_KEY_MATERIAL,
 910                                             HostCmd_ACT_GEN_SET,
 911                                             KEY_INFO_ENABLED, encrypt_key);
 912                if (ret)
 913                        return ret;
 914
 915                ibss_key = &priv->aes_key;
 916                memset(ibss_key, 0,
 917                       sizeof(struct host_cmd_ds_802_11_key_material));
 918                /* Copy the key in the driver */
 919                memcpy(ibss_key->key_param_set.key, encrypt_key->key_material,
 920                       encrypt_key->key_len);
 921                memcpy(&ibss_key->key_param_set.key_len, &encrypt_key->key_len,
 922                       sizeof(ibss_key->key_param_set.key_len));
 923                ibss_key->key_param_set.key_type_id
 924                        = cpu_to_le16(KEY_TYPE_ID_TKIP);
 925                ibss_key->key_param_set.key_info = cpu_to_le16(KEY_ENABLED);
 926
 927                /* Send the key as GTK to firmware */
 928                encrypt_key->key_index = ~MWIFIEX_KEY_INDEX_UNICAST;
 929        }
 930
 931        if (!encrypt_key->key_index)
 932                encrypt_key->key_index = MWIFIEX_KEY_INDEX_UNICAST;
 933
 934        if (remove_key)
 935                ret = mwifiex_send_cmd_sync(priv,
 936                                            HostCmd_CMD_802_11_KEY_MATERIAL,
 937                                            HostCmd_ACT_GEN_SET,
 938                                            !KEY_INFO_ENABLED, encrypt_key);
 939        else
 940                ret = mwifiex_send_cmd_sync(priv,
 941                                            HostCmd_CMD_802_11_KEY_MATERIAL,
 942                                            HostCmd_ACT_GEN_SET,
 943                                            KEY_INFO_ENABLED, encrypt_key);
 944
 945        return ret;
 946}
 947
 948/*
 949 * IOCTL request handler to set/get network keys.
 950 *
 951 * This is a generic key handling function which supports WEP, WPA
 952 * and WAPI.
 953 */
 954static int
 955mwifiex_sec_ioctl_encrypt_key(struct mwifiex_private *priv,
 956                              struct mwifiex_ds_encrypt_key *encrypt_key)
 957{
 958        int status;
 959
 960        if (encrypt_key->is_wapi_key)
 961                status = mwifiex_sec_ioctl_set_wapi_key(priv, encrypt_key);
 962        else if (encrypt_key->key_len > WLAN_KEY_LEN_WEP104)
 963                status = mwifiex_sec_ioctl_set_wpa_key(priv, encrypt_key);
 964        else
 965                status = mwifiex_sec_ioctl_set_wep_key(priv, encrypt_key);
 966        return status;
 967}
 968
 969/*
 970 * This function returns the driver version.
 971 */
 972int
 973mwifiex_drv_get_driver_version(struct mwifiex_adapter *adapter, char *version,
 974                               int max_len)
 975{
 976        union {
 977                u32 l;
 978                u8 c[4];
 979        } ver;
 980        char fw_ver[32];
 981
 982        ver.l = adapter->fw_release_number;
 983        sprintf(fw_ver, "%u.%u.%u.p%u", ver.c[2], ver.c[1], ver.c[0], ver.c[3]);
 984
 985        snprintf(version, max_len, driver_version, fw_ver);
 986
 987        dev_dbg(adapter->dev, "info: MWIFIEX VERSION: %s\n", version);
 988
 989        return 0;
 990}
 991
 992/*
 993 * Sends IOCTL request to set encoding parameters.
 994 *
 995 * This function allocates the IOCTL request buffer, fills it
 996 * with requisite parameters and calls the IOCTL handler.
 997 */
 998int mwifiex_set_encode(struct mwifiex_private *priv, struct key_params *kp,
 999                       const u8 *key, int key_len, u8 key_index,
1000                       const u8 *mac_addr, int disable)
1001{
1002        struct mwifiex_ds_encrypt_key encrypt_key;
1003
1004        memset(&encrypt_key, 0, sizeof(struct mwifiex_ds_encrypt_key));
1005        encrypt_key.key_len = key_len;
1006
1007        if (kp && kp->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
1008                encrypt_key.is_igtk_key = true;
1009
1010        if (!disable) {
1011                encrypt_key.key_index = key_index;
1012                if (key_len)
1013                        memcpy(encrypt_key.key_material, key, key_len);
1014                if (mac_addr)
1015                        memcpy(encrypt_key.mac_addr, mac_addr, ETH_ALEN);
1016                if (kp && kp->seq && kp->seq_len)
1017                        memcpy(encrypt_key.pn, kp->seq, kp->seq_len);
1018        } else {
1019                encrypt_key.key_disable = true;
1020                if (mac_addr)
1021                        memcpy(encrypt_key.mac_addr, mac_addr, ETH_ALEN);
1022        }
1023
1024        return mwifiex_sec_ioctl_encrypt_key(priv, &encrypt_key);
1025}
1026
1027/*
1028 * Sends IOCTL request to get extended version.
1029 *
1030 * This function allocates the IOCTL request buffer, fills it
1031 * with requisite parameters and calls the IOCTL handler.
1032 */
1033int
1034mwifiex_get_ver_ext(struct mwifiex_private *priv)
1035{
1036        struct mwifiex_ver_ext ver_ext;
1037
1038        memset(&ver_ext, 0, sizeof(struct host_cmd_ds_version_ext));
1039        if (mwifiex_send_cmd_sync(priv, HostCmd_CMD_VERSION_EXT,
1040                                  HostCmd_ACT_GEN_GET, 0, &ver_ext))
1041                return -1;
1042
1043        return 0;
1044}
1045
1046int
1047mwifiex_remain_on_chan_cfg(struct mwifiex_private *priv, u16 action,
1048                           struct ieee80211_channel *chan,
1049                           enum nl80211_channel_type *ct,
1050                           unsigned int duration)
1051{
1052        struct host_cmd_ds_remain_on_chan roc_cfg;
1053        u8 sc;
1054
1055        memset(&roc_cfg, 0, sizeof(roc_cfg));
1056        roc_cfg.action = cpu_to_le16(action);
1057        if (action == HostCmd_ACT_GEN_SET) {
1058                roc_cfg.band_cfg = chan->band;
1059                sc = mwifiex_chan_type_to_sec_chan_offset(*ct);
1060                roc_cfg.band_cfg |= (sc << 2);
1061
1062                roc_cfg.channel =
1063                        ieee80211_frequency_to_channel(chan->center_freq);
1064                roc_cfg.duration = cpu_to_le32(duration);
1065        }
1066        if (mwifiex_send_cmd_sync(priv, HostCmd_CMD_REMAIN_ON_CHAN,
1067                                  action, 0, &roc_cfg)) {
1068                dev_err(priv->adapter->dev, "failed to remain on channel\n");
1069                return -1;
1070        }
1071
1072        return roc_cfg.status;
1073}
1074
1075int
1076mwifiex_set_bss_role(struct mwifiex_private *priv, u8 bss_role)
1077{
1078        if (GET_BSS_ROLE(priv) == bss_role) {
1079                dev_dbg(priv->adapter->dev,
1080                        "info: already in the desired role.\n");
1081                return 0;
1082        }
1083
1084        mwifiex_free_priv(priv);
1085        mwifiex_init_priv(priv);
1086
1087        priv->bss_role = bss_role;
1088        switch (bss_role) {
1089        case MWIFIEX_BSS_ROLE_UAP:
1090                priv->bss_mode = NL80211_IFTYPE_AP;
1091                break;
1092        case MWIFIEX_BSS_ROLE_STA:
1093        case MWIFIEX_BSS_ROLE_ANY:
1094        default:
1095                priv->bss_mode = NL80211_IFTYPE_STATION;
1096                break;
1097        }
1098
1099        mwifiex_send_cmd_sync(priv, HostCmd_CMD_SET_BSS_MODE,
1100                              HostCmd_ACT_GEN_SET, 0, NULL);
1101
1102        return mwifiex_sta_init_cmd(priv, false);
1103}
1104
1105/*
1106 * Sends IOCTL request to get statistics information.
1107 *
1108 * This function allocates the IOCTL request buffer, fills it
1109 * with requisite parameters and calls the IOCTL handler.
1110 */
1111int
1112mwifiex_get_stats_info(struct mwifiex_private *priv,
1113                       struct mwifiex_ds_get_stats *log)
1114{
1115        return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_GET_LOG,
1116                                     HostCmd_ACT_GEN_GET, 0, log);
1117}
1118
1119/*
1120 * IOCTL request handler to read/write register.
1121 *
1122 * This function prepares the correct firmware command and
1123 * issues it.
1124 *
1125 * Access to the following registers are supported -
1126 *      - MAC
1127 *      - BBP
1128 *      - RF
1129 *      - PMIC
1130 *      - CAU
1131 */
1132static int mwifiex_reg_mem_ioctl_reg_rw(struct mwifiex_private *priv,
1133                                        struct mwifiex_ds_reg_rw *reg_rw,
1134                                        u16 action)
1135{
1136        u16 cmd_no;
1137
1138        switch (le32_to_cpu(reg_rw->type)) {
1139        case MWIFIEX_REG_MAC:
1140                cmd_no = HostCmd_CMD_MAC_REG_ACCESS;
1141                break;
1142        case MWIFIEX_REG_BBP:
1143                cmd_no = HostCmd_CMD_BBP_REG_ACCESS;
1144                break;
1145        case MWIFIEX_REG_RF:
1146                cmd_no = HostCmd_CMD_RF_REG_ACCESS;
1147                break;
1148        case MWIFIEX_REG_PMIC:
1149                cmd_no = HostCmd_CMD_PMIC_REG_ACCESS;
1150                break;
1151        case MWIFIEX_REG_CAU:
1152                cmd_no = HostCmd_CMD_CAU_REG_ACCESS;
1153                break;
1154        default:
1155                return -1;
1156        }
1157
1158        return mwifiex_send_cmd_sync(priv, cmd_no, action, 0, reg_rw);
1159
1160}
1161
1162/*
1163 * Sends IOCTL request to write to a register.
1164 *
1165 * This function allocates the IOCTL request buffer, fills it
1166 * with requisite parameters and calls the IOCTL handler.
1167 */
1168int
1169mwifiex_reg_write(struct mwifiex_private *priv, u32 reg_type,
1170                  u32 reg_offset, u32 reg_value)
1171{
1172        struct mwifiex_ds_reg_rw reg_rw;
1173
1174        reg_rw.type = cpu_to_le32(reg_type);
1175        reg_rw.offset = cpu_to_le32(reg_offset);
1176        reg_rw.value = cpu_to_le32(reg_value);
1177
1178        return mwifiex_reg_mem_ioctl_reg_rw(priv, &reg_rw, HostCmd_ACT_GEN_SET);
1179}
1180
1181/*
1182 * Sends IOCTL request to read from a register.
1183 *
1184 * This function allocates the IOCTL request buffer, fills it
1185 * with requisite parameters and calls the IOCTL handler.
1186 */
1187int
1188mwifiex_reg_read(struct mwifiex_private *priv, u32 reg_type,
1189                 u32 reg_offset, u32 *value)
1190{
1191        int ret;
1192        struct mwifiex_ds_reg_rw reg_rw;
1193
1194        reg_rw.type = cpu_to_le32(reg_type);
1195        reg_rw.offset = cpu_to_le32(reg_offset);
1196        ret = mwifiex_reg_mem_ioctl_reg_rw(priv, &reg_rw, HostCmd_ACT_GEN_GET);
1197
1198        if (ret)
1199                goto done;
1200
1201        *value = le32_to_cpu(reg_rw.value);
1202
1203done:
1204        return ret;
1205}
1206
1207/*
1208 * Sends IOCTL request to read from EEPROM.
1209 *
1210 * This function allocates the IOCTL request buffer, fills it
1211 * with requisite parameters and calls the IOCTL handler.
1212 */
1213int
1214mwifiex_eeprom_read(struct mwifiex_private *priv, u16 offset, u16 bytes,
1215                    u8 *value)
1216{
1217        int ret;
1218        struct mwifiex_ds_read_eeprom rd_eeprom;
1219
1220        rd_eeprom.offset = cpu_to_le16((u16) offset);
1221        rd_eeprom.byte_count = cpu_to_le16((u16) bytes);
1222
1223        /* Send request to firmware */
1224        ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_EEPROM_ACCESS,
1225                                    HostCmd_ACT_GEN_GET, 0, &rd_eeprom);
1226
1227        if (!ret)
1228                memcpy(value, rd_eeprom.value, MAX_EEPROM_DATA);
1229        return ret;
1230}
1231
1232/*
1233 * This function sets a generic IE. In addition to generic IE, it can
1234 * also handle WPA, WPA2 and WAPI IEs.
1235 */
1236static int
1237mwifiex_set_gen_ie_helper(struct mwifiex_private *priv, u8 *ie_data_ptr,
1238                          u16 ie_len)
1239{
1240        int ret = 0;
1241        struct ieee_types_vendor_header *pvendor_ie;
1242        const u8 wpa_oui[] = { 0x00, 0x50, 0xf2, 0x01 };
1243        const u8 wps_oui[] = { 0x00, 0x50, 0xf2, 0x04 };
1244
1245        /* If the passed length is zero, reset the buffer */
1246        if (!ie_len) {
1247                priv->gen_ie_buf_len = 0;
1248                priv->wps.session_enable = false;
1249
1250                return 0;
1251        } else if (!ie_data_ptr) {
1252                return -1;
1253        }
1254        pvendor_ie = (struct ieee_types_vendor_header *) ie_data_ptr;
1255        /* Test to see if it is a WPA IE, if not, then it is a gen IE */
1256        if (((pvendor_ie->element_id == WLAN_EID_WPA) &&
1257             (!memcmp(pvendor_ie->oui, wpa_oui, sizeof(wpa_oui)))) ||
1258            (pvendor_ie->element_id == WLAN_EID_RSN)) {
1259
1260                /* IE is a WPA/WPA2 IE so call set_wpa function */
1261                ret = mwifiex_set_wpa_ie_helper(priv, ie_data_ptr, ie_len);
1262                priv->wps.session_enable = false;
1263
1264                return ret;
1265        } else if (pvendor_ie->element_id == WLAN_EID_BSS_AC_ACCESS_DELAY) {
1266                /* IE is a WAPI IE so call set_wapi function */
1267                ret = mwifiex_set_wapi_ie(priv, ie_data_ptr, ie_len);
1268
1269                return ret;
1270        }
1271        /*
1272         * Verify that the passed length is not larger than the
1273         * available space remaining in the buffer
1274         */
1275        if (ie_len < (sizeof(priv->gen_ie_buf) - priv->gen_ie_buf_len)) {
1276
1277                /* Test to see if it is a WPS IE, if so, enable
1278                 * wps session flag
1279                 */
1280                pvendor_ie = (struct ieee_types_vendor_header *) ie_data_ptr;
1281                if ((pvendor_ie->element_id == WLAN_EID_VENDOR_SPECIFIC) &&
1282                    (!memcmp(pvendor_ie->oui, wps_oui, sizeof(wps_oui)))) {
1283                        priv->wps.session_enable = true;
1284                        dev_dbg(priv->adapter->dev,
1285                                "info: WPS Session Enabled.\n");
1286                        ret = mwifiex_set_wps_ie(priv, ie_data_ptr, ie_len);
1287                }
1288
1289                /* Append the passed data to the end of the
1290                   genIeBuffer */
1291                memcpy(priv->gen_ie_buf + priv->gen_ie_buf_len, ie_data_ptr,
1292                       ie_len);
1293                /* Increment the stored buffer length by the
1294                   size passed */
1295                priv->gen_ie_buf_len += ie_len;
1296        } else {
1297                /* Passed data does not fit in the remaining
1298                   buffer space */
1299                ret = -1;
1300        }
1301
1302        /* Return 0, or -1 for error case */
1303        return ret;
1304}
1305
1306/*
1307 * IOCTL request handler to set/get generic IE.
1308 *
1309 * In addition to various generic IEs, this function can also be
1310 * used to set the ARP filter.
1311 */
1312static int mwifiex_misc_ioctl_gen_ie(struct mwifiex_private *priv,
1313                                     struct mwifiex_ds_misc_gen_ie *gen_ie,
1314                                     u16 action)
1315{
1316        struct mwifiex_adapter *adapter = priv->adapter;
1317
1318        switch (gen_ie->type) {
1319        case MWIFIEX_IE_TYPE_GEN_IE:
1320                if (action == HostCmd_ACT_GEN_GET) {
1321                        gen_ie->len = priv->wpa_ie_len;
1322                        memcpy(gen_ie->ie_data, priv->wpa_ie, gen_ie->len);
1323                } else {
1324                        mwifiex_set_gen_ie_helper(priv, gen_ie->ie_data,
1325                                                  (u16) gen_ie->len);
1326                }
1327                break;
1328        case MWIFIEX_IE_TYPE_ARP_FILTER:
1329                memset(adapter->arp_filter, 0, sizeof(adapter->arp_filter));
1330                if (gen_ie->len > ARP_FILTER_MAX_BUF_SIZE) {
1331                        adapter->arp_filter_size = 0;
1332                        dev_err(adapter->dev, "invalid ARP filter size\n");
1333                        return -1;
1334                } else {
1335                        memcpy(adapter->arp_filter, gen_ie->ie_data,
1336                               gen_ie->len);
1337                        adapter->arp_filter_size = gen_ie->len;
1338                }
1339                break;
1340        default:
1341                dev_err(adapter->dev, "invalid IE type\n");
1342                return -1;
1343        }
1344        return 0;
1345}
1346
1347/*
1348 * Sends IOCTL request to set a generic IE.
1349 *
1350 * This function allocates the IOCTL request buffer, fills it
1351 * with requisite parameters and calls the IOCTL handler.
1352 */
1353int
1354mwifiex_set_gen_ie(struct mwifiex_private *priv, u8 *ie, int ie_len)
1355{
1356        struct mwifiex_ds_misc_gen_ie gen_ie;
1357
1358        if (ie_len > IEEE_MAX_IE_SIZE)
1359                return -EFAULT;
1360
1361        gen_ie.type = MWIFIEX_IE_TYPE_GEN_IE;
1362        gen_ie.len = ie_len;
1363        memcpy(gen_ie.ie_data, ie, ie_len);
1364        if (mwifiex_misc_ioctl_gen_ie(priv, &gen_ie, HostCmd_ACT_GEN_SET))
1365                return -EFAULT;
1366
1367        return 0;
1368}
1369