linux/drivers/net/wireless/st/cw1200/sta.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Mac80211 STA API for ST-Ericsson CW1200 drivers
   4 *
   5 * Copyright (c) 2010, ST-Ericsson
   6 * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
   7 */
   8
   9#include <linux/vmalloc.h>
  10#include <linux/sched.h>
  11#include <linux/firmware.h>
  12#include <linux/module.h>
  13#include <linux/etherdevice.h>
  14
  15#include "cw1200.h"
  16#include "sta.h"
  17#include "fwio.h"
  18#include "bh.h"
  19#include "debug.h"
  20
  21#ifndef ERP_INFO_BYTE_OFFSET
  22#define ERP_INFO_BYTE_OFFSET 2
  23#endif
  24
  25static void cw1200_do_join(struct cw1200_common *priv);
  26static void cw1200_do_unjoin(struct cw1200_common *priv);
  27
  28static int cw1200_upload_beacon(struct cw1200_common *priv);
  29static int cw1200_upload_pspoll(struct cw1200_common *priv);
  30static int cw1200_upload_null(struct cw1200_common *priv);
  31static int cw1200_upload_qosnull(struct cw1200_common *priv);
  32static int cw1200_start_ap(struct cw1200_common *priv);
  33static int cw1200_update_beaconing(struct cw1200_common *priv);
  34static int cw1200_enable_beaconing(struct cw1200_common *priv,
  35                                   bool enable);
  36static void __cw1200_sta_notify(struct ieee80211_hw *dev,
  37                                struct ieee80211_vif *vif,
  38                                enum sta_notify_cmd notify_cmd,
  39                                int link_id);
  40static int __cw1200_flush(struct cw1200_common *priv, bool drop);
  41
  42static inline void __cw1200_free_event_queue(struct list_head *list)
  43{
  44        struct cw1200_wsm_event *event, *tmp;
  45        list_for_each_entry_safe(event, tmp, list, link) {
  46                list_del(&event->link);
  47                kfree(event);
  48        }
  49}
  50
  51/* ******************************************************************** */
  52/* STA API                                                              */
  53
  54int cw1200_start(struct ieee80211_hw *dev)
  55{
  56        struct cw1200_common *priv = dev->priv;
  57        int ret = 0;
  58
  59        cw1200_pm_stay_awake(&priv->pm_state, HZ);
  60
  61        mutex_lock(&priv->conf_mutex);
  62
  63        /* default EDCA */
  64        WSM_EDCA_SET(&priv->edca, 0, 0x0002, 0x0003, 0x0007, 47, 0xc8, false);
  65        WSM_EDCA_SET(&priv->edca, 1, 0x0002, 0x0007, 0x000f, 94, 0xc8, false);
  66        WSM_EDCA_SET(&priv->edca, 2, 0x0003, 0x000f, 0x03ff, 0, 0xc8, false);
  67        WSM_EDCA_SET(&priv->edca, 3, 0x0007, 0x000f, 0x03ff, 0, 0xc8, false);
  68        ret = wsm_set_edca_params(priv, &priv->edca);
  69        if (ret)
  70                goto out;
  71
  72        ret = cw1200_set_uapsd_param(priv, &priv->edca);
  73        if (ret)
  74                goto out;
  75
  76        priv->setbssparams_done = false;
  77
  78        memcpy(priv->mac_addr, dev->wiphy->perm_addr, ETH_ALEN);
  79        priv->mode = NL80211_IFTYPE_MONITOR;
  80        priv->wep_default_key_id = -1;
  81
  82        priv->cqm_beacon_loss_count = 10;
  83
  84        ret = cw1200_setup_mac(priv);
  85        if (ret)
  86                goto out;
  87
  88out:
  89        mutex_unlock(&priv->conf_mutex);
  90        return ret;
  91}
  92
  93void cw1200_stop(struct ieee80211_hw *dev)
  94{
  95        struct cw1200_common *priv = dev->priv;
  96        LIST_HEAD(list);
  97        int i;
  98
  99        wsm_lock_tx(priv);
 100
 101        while (down_trylock(&priv->scan.lock)) {
 102                /* Scan is in progress. Force it to stop. */
 103                priv->scan.req = NULL;
 104                schedule();
 105        }
 106        up(&priv->scan.lock);
 107
 108        cancel_delayed_work_sync(&priv->scan.probe_work);
 109        cancel_delayed_work_sync(&priv->scan.timeout);
 110        cancel_delayed_work_sync(&priv->clear_recent_scan_work);
 111        cancel_delayed_work_sync(&priv->join_timeout);
 112        cw1200_cqm_bssloss_sm(priv, 0, 0, 0);
 113        cancel_work_sync(&priv->unjoin_work);
 114        cancel_delayed_work_sync(&priv->link_id_gc_work);
 115        flush_workqueue(priv->workqueue);
 116        del_timer_sync(&priv->mcast_timeout);
 117        mutex_lock(&priv->conf_mutex);
 118        priv->mode = NL80211_IFTYPE_UNSPECIFIED;
 119        priv->listening = false;
 120
 121        spin_lock(&priv->event_queue_lock);
 122        list_splice_init(&priv->event_queue, &list);
 123        spin_unlock(&priv->event_queue_lock);
 124        __cw1200_free_event_queue(&list);
 125
 126
 127        priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
 128        priv->join_pending = false;
 129
 130        for (i = 0; i < 4; i++)
 131                cw1200_queue_clear(&priv->tx_queue[i]);
 132        mutex_unlock(&priv->conf_mutex);
 133        tx_policy_clean(priv);
 134
 135        /* HACK! */
 136        if (atomic_xchg(&priv->tx_lock, 1) != 1)
 137                pr_debug("[STA] TX is force-unlocked due to stop request.\n");
 138
 139        wsm_unlock_tx(priv);
 140        atomic_xchg(&priv->tx_lock, 0); /* for recovery to work */
 141}
 142
 143static int cw1200_bssloss_mitigation = 1;
 144module_param(cw1200_bssloss_mitigation, int, 0644);
 145MODULE_PARM_DESC(cw1200_bssloss_mitigation, "BSS Loss mitigation. 0 == disabled, 1 == enabled (default)");
 146
 147
 148void __cw1200_cqm_bssloss_sm(struct cw1200_common *priv,
 149                             int init, int good, int bad)
 150{
 151        int tx = 0;
 152
 153        priv->delayed_link_loss = 0;
 154        cancel_work_sync(&priv->bss_params_work);
 155
 156        pr_debug("[STA] CQM BSSLOSS_SM: state: %d init %d good %d bad: %d txlock: %d uj: %d\n",
 157                 priv->bss_loss_state,
 158                 init, good, bad,
 159                 atomic_read(&priv->tx_lock),
 160                 priv->delayed_unjoin);
 161
 162        /* If we have a pending unjoin */
 163        if (priv->delayed_unjoin)
 164                return;
 165
 166        if (init) {
 167                queue_delayed_work(priv->workqueue,
 168                                   &priv->bss_loss_work,
 169                                   HZ);
 170                priv->bss_loss_state = 0;
 171
 172                /* Skip the confimration procedure in P2P case */
 173                if (!priv->vif->p2p && !atomic_read(&priv->tx_lock))
 174                        tx = 1;
 175        } else if (good) {
 176                cancel_delayed_work_sync(&priv->bss_loss_work);
 177                priv->bss_loss_state = 0;
 178                queue_work(priv->workqueue, &priv->bss_params_work);
 179        } else if (bad) {
 180                /* XXX Should we just keep going until we time out? */
 181                if (priv->bss_loss_state < 3)
 182                        tx = 1;
 183        } else {
 184                cancel_delayed_work_sync(&priv->bss_loss_work);
 185                priv->bss_loss_state = 0;
 186        }
 187
 188        /* Bypass mitigation if it's disabled */
 189        if (!cw1200_bssloss_mitigation)
 190                tx = 0;
 191
 192        /* Spit out a NULL packet to our AP if necessary */
 193        if (tx) {
 194                struct sk_buff *skb;
 195
 196                priv->bss_loss_state++;
 197
 198                skb = ieee80211_nullfunc_get(priv->hw, priv->vif, false);
 199                WARN_ON(!skb);
 200                if (skb)
 201                        cw1200_tx(priv->hw, NULL, skb);
 202        }
 203}
 204
 205int cw1200_add_interface(struct ieee80211_hw *dev,
 206                         struct ieee80211_vif *vif)
 207{
 208        int ret;
 209        struct cw1200_common *priv = dev->priv;
 210        /* __le32 auto_calibration_mode = __cpu_to_le32(1); */
 211
 212        vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
 213                             IEEE80211_VIF_SUPPORTS_UAPSD |
 214                             IEEE80211_VIF_SUPPORTS_CQM_RSSI;
 215
 216        mutex_lock(&priv->conf_mutex);
 217
 218        if (priv->mode != NL80211_IFTYPE_MONITOR) {
 219                mutex_unlock(&priv->conf_mutex);
 220                return -EOPNOTSUPP;
 221        }
 222
 223        switch (vif->type) {
 224        case NL80211_IFTYPE_STATION:
 225        case NL80211_IFTYPE_ADHOC:
 226        case NL80211_IFTYPE_MESH_POINT:
 227        case NL80211_IFTYPE_AP:
 228                priv->mode = vif->type;
 229                break;
 230        default:
 231                mutex_unlock(&priv->conf_mutex);
 232                return -EOPNOTSUPP;
 233        }
 234
 235        priv->vif = vif;
 236        memcpy(priv->mac_addr, vif->addr, ETH_ALEN);
 237        ret = cw1200_setup_mac(priv);
 238        /* Enable auto-calibration */
 239        /* Exception in subsequent channel switch; disabled.
 240         *  wsm_write_mib(priv, WSM_MIB_ID_SET_AUTO_CALIBRATION_MODE,
 241         *      &auto_calibration_mode, sizeof(auto_calibration_mode));
 242        */
 243
 244        mutex_unlock(&priv->conf_mutex);
 245        return ret;
 246}
 247
 248void cw1200_remove_interface(struct ieee80211_hw *dev,
 249                             struct ieee80211_vif *vif)
 250{
 251        struct cw1200_common *priv = dev->priv;
 252        struct wsm_reset reset = {
 253                .reset_statistics = true,
 254        };
 255        int i;
 256
 257        mutex_lock(&priv->conf_mutex);
 258        switch (priv->join_status) {
 259        case CW1200_JOIN_STATUS_JOINING:
 260        case CW1200_JOIN_STATUS_PRE_STA:
 261        case CW1200_JOIN_STATUS_STA:
 262        case CW1200_JOIN_STATUS_IBSS:
 263                wsm_lock_tx(priv);
 264                if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
 265                        wsm_unlock_tx(priv);
 266                break;
 267        case CW1200_JOIN_STATUS_AP:
 268                for (i = 0; priv->link_id_map; ++i) {
 269                        if (priv->link_id_map & BIT(i)) {
 270                                reset.link_id = i;
 271                                wsm_reset(priv, &reset);
 272                                priv->link_id_map &= ~BIT(i);
 273                        }
 274                }
 275                memset(priv->link_id_db, 0, sizeof(priv->link_id_db));
 276                priv->sta_asleep_mask = 0;
 277                priv->enable_beacon = false;
 278                priv->tx_multicast = false;
 279                priv->aid0_bit_set = false;
 280                priv->buffered_multicasts = false;
 281                priv->pspoll_mask = 0;
 282                reset.link_id = 0;
 283                wsm_reset(priv, &reset);
 284                break;
 285        case CW1200_JOIN_STATUS_MONITOR:
 286                cw1200_update_listening(priv, false);
 287                break;
 288        default:
 289                break;
 290        }
 291        priv->vif = NULL;
 292        priv->mode = NL80211_IFTYPE_MONITOR;
 293        eth_zero_addr(priv->mac_addr);
 294        memset(&priv->p2p_ps_modeinfo, 0, sizeof(priv->p2p_ps_modeinfo));
 295        cw1200_free_keys(priv);
 296        cw1200_setup_mac(priv);
 297        priv->listening = false;
 298        priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
 299        if (!__cw1200_flush(priv, true))
 300                wsm_unlock_tx(priv);
 301
 302        mutex_unlock(&priv->conf_mutex);
 303}
 304
 305int cw1200_change_interface(struct ieee80211_hw *dev,
 306                            struct ieee80211_vif *vif,
 307                            enum nl80211_iftype new_type,
 308                            bool p2p)
 309{
 310        int ret = 0;
 311        pr_debug("change_interface new: %d (%d), old: %d (%d)\n", new_type,
 312                 p2p, vif->type, vif->p2p);
 313
 314        if (new_type != vif->type || vif->p2p != p2p) {
 315                cw1200_remove_interface(dev, vif);
 316                vif->type = new_type;
 317                vif->p2p = p2p;
 318                ret = cw1200_add_interface(dev, vif);
 319        }
 320
 321        return ret;
 322}
 323
 324int cw1200_config(struct ieee80211_hw *dev, u32 changed)
 325{
 326        int ret = 0;
 327        struct cw1200_common *priv = dev->priv;
 328        struct ieee80211_conf *conf = &dev->conf;
 329
 330        pr_debug("CONFIG CHANGED:  %08x\n", changed);
 331
 332        down(&priv->scan.lock);
 333        mutex_lock(&priv->conf_mutex);
 334        /* TODO: IEEE80211_CONF_CHANGE_QOS */
 335        /* TODO: IEEE80211_CONF_CHANGE_LISTEN_INTERVAL */
 336
 337        if (changed & IEEE80211_CONF_CHANGE_POWER) {
 338                priv->output_power = conf->power_level;
 339                pr_debug("[STA] TX power: %d\n", priv->output_power);
 340                wsm_set_output_power(priv, priv->output_power * 10);
 341        }
 342
 343        if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) &&
 344            (priv->channel != conf->chandef.chan)) {
 345                struct ieee80211_channel *ch = conf->chandef.chan;
 346                struct wsm_switch_channel channel = {
 347                        .channel_number = ch->hw_value,
 348                };
 349                pr_debug("[STA] Freq %d (wsm ch: %d).\n",
 350                         ch->center_freq, ch->hw_value);
 351
 352                /* __cw1200_flush() implicitly locks tx, if successful */
 353                if (!__cw1200_flush(priv, false)) {
 354                        if (!wsm_switch_channel(priv, &channel)) {
 355                                ret = wait_event_timeout(priv->channel_switch_done,
 356                                                         !priv->channel_switch_in_progress,
 357                                                         3 * HZ);
 358                                if (ret) {
 359                                        /* Already unlocks if successful */
 360                                        priv->channel = ch;
 361                                        ret = 0;
 362                                } else {
 363                                        ret = -ETIMEDOUT;
 364                                }
 365                        } else {
 366                                /* Unlock if switch channel fails */
 367                                wsm_unlock_tx(priv);
 368                        }
 369                }
 370        }
 371
 372        if (changed & IEEE80211_CONF_CHANGE_PS) {
 373                if (!(conf->flags & IEEE80211_CONF_PS))
 374                        priv->powersave_mode.mode = WSM_PSM_ACTIVE;
 375                else if (conf->dynamic_ps_timeout <= 0)
 376                        priv->powersave_mode.mode = WSM_PSM_PS;
 377                else
 378                        priv->powersave_mode.mode = WSM_PSM_FAST_PS;
 379
 380                /* Firmware requires that value for this 1-byte field must
 381                 * be specified in units of 500us. Values above the 128ms
 382                 * threshold are not supported.
 383                 */
 384                if (conf->dynamic_ps_timeout >= 0x80)
 385                        priv->powersave_mode.fast_psm_idle_period = 0xFF;
 386                else
 387                        priv->powersave_mode.fast_psm_idle_period =
 388                                        conf->dynamic_ps_timeout << 1;
 389
 390                if (priv->join_status == CW1200_JOIN_STATUS_STA &&
 391                    priv->bss_params.aid)
 392                        cw1200_set_pm(priv, &priv->powersave_mode);
 393        }
 394
 395        if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
 396                /* TBD: It looks like it's transparent
 397                 * there's a monitor interface present -- use this
 398                 * to determine for example whether to calculate
 399                 * timestamps for packets or not, do not use instead
 400                 * of filter flags!
 401                 */
 402        }
 403
 404        if (changed & IEEE80211_CONF_CHANGE_IDLE) {
 405                struct wsm_operational_mode mode = {
 406                        .power_mode = cw1200_power_mode,
 407                        .disable_more_flag_usage = true,
 408                };
 409
 410                wsm_lock_tx(priv);
 411                /* Disable p2p-dev mode forced by TX request */
 412                if ((priv->join_status == CW1200_JOIN_STATUS_MONITOR) &&
 413                    (conf->flags & IEEE80211_CONF_IDLE) &&
 414                    !priv->listening) {
 415                        cw1200_disable_listening(priv);
 416                        priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
 417                }
 418                wsm_set_operational_mode(priv, &mode);
 419                wsm_unlock_tx(priv);
 420        }
 421
 422        if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
 423                pr_debug("[STA] Retry limits: %d (long), %d (short).\n",
 424                         conf->long_frame_max_tx_count,
 425                         conf->short_frame_max_tx_count);
 426                spin_lock_bh(&priv->tx_policy_cache.lock);
 427                priv->long_frame_max_tx_count = conf->long_frame_max_tx_count;
 428                priv->short_frame_max_tx_count =
 429                        (conf->short_frame_max_tx_count < 0x0F) ?
 430                        conf->short_frame_max_tx_count : 0x0F;
 431                priv->hw->max_rate_tries = priv->short_frame_max_tx_count;
 432                spin_unlock_bh(&priv->tx_policy_cache.lock);
 433        }
 434        mutex_unlock(&priv->conf_mutex);
 435        up(&priv->scan.lock);
 436        return ret;
 437}
 438
 439void cw1200_update_filtering(struct cw1200_common *priv)
 440{
 441        int ret;
 442        bool bssid_filtering = !priv->rx_filter.bssid;
 443        bool is_p2p = priv->vif && priv->vif->p2p;
 444        bool is_sta = priv->vif && NL80211_IFTYPE_STATION == priv->vif->type;
 445
 446        static struct wsm_beacon_filter_control bf_ctrl;
 447        static struct wsm_mib_beacon_filter_table bf_tbl = {
 448                .entry[0].ie_id = WLAN_EID_VENDOR_SPECIFIC,
 449                .entry[0].flags = WSM_BEACON_FILTER_IE_HAS_CHANGED |
 450                                        WSM_BEACON_FILTER_IE_NO_LONGER_PRESENT |
 451                                        WSM_BEACON_FILTER_IE_HAS_APPEARED,
 452                .entry[0].oui[0] = 0x50,
 453                .entry[0].oui[1] = 0x6F,
 454                .entry[0].oui[2] = 0x9A,
 455                .entry[1].ie_id = WLAN_EID_HT_OPERATION,
 456                .entry[1].flags = WSM_BEACON_FILTER_IE_HAS_CHANGED |
 457                                        WSM_BEACON_FILTER_IE_NO_LONGER_PRESENT |
 458                                        WSM_BEACON_FILTER_IE_HAS_APPEARED,
 459                .entry[2].ie_id = WLAN_EID_ERP_INFO,
 460                .entry[2].flags = WSM_BEACON_FILTER_IE_HAS_CHANGED |
 461                                        WSM_BEACON_FILTER_IE_NO_LONGER_PRESENT |
 462                                        WSM_BEACON_FILTER_IE_HAS_APPEARED,
 463        };
 464
 465        if (priv->join_status == CW1200_JOIN_STATUS_PASSIVE)
 466                return;
 467        else if (priv->join_status == CW1200_JOIN_STATUS_MONITOR)
 468                bssid_filtering = false;
 469
 470        if (priv->disable_beacon_filter) {
 471                bf_ctrl.enabled = 0;
 472                bf_ctrl.bcn_count = 1;
 473                bf_tbl.num = __cpu_to_le32(0);
 474        } else if (is_p2p || !is_sta) {
 475                bf_ctrl.enabled = WSM_BEACON_FILTER_ENABLE |
 476                        WSM_BEACON_FILTER_AUTO_ERP;
 477                bf_ctrl.bcn_count = 0;
 478                bf_tbl.num = __cpu_to_le32(2);
 479        } else {
 480                bf_ctrl.enabled = WSM_BEACON_FILTER_ENABLE;
 481                bf_ctrl.bcn_count = 0;
 482                bf_tbl.num = __cpu_to_le32(3);
 483        }
 484
 485        /* When acting as p2p client being connected to p2p GO, in order to
 486         * receive frames from a different p2p device, turn off bssid filter.
 487         *
 488         * WARNING: FW dependency!
 489         * This can only be used with FW WSM371 and its successors.
 490         * In that FW version even with bssid filter turned off,
 491         * device will block most of the unwanted frames.
 492         */
 493        if (is_p2p)
 494                bssid_filtering = false;
 495
 496        ret = wsm_set_rx_filter(priv, &priv->rx_filter);
 497        if (!ret)
 498                ret = wsm_set_beacon_filter_table(priv, &bf_tbl);
 499        if (!ret)
 500                ret = wsm_beacon_filter_control(priv, &bf_ctrl);
 501        if (!ret)
 502                ret = wsm_set_bssid_filtering(priv, bssid_filtering);
 503        if (!ret)
 504                ret = wsm_set_multicast_filter(priv, &priv->multicast_filter);
 505        if (ret)
 506                wiphy_err(priv->hw->wiphy,
 507                          "Update filtering failed: %d.\n", ret);
 508        return;
 509}
 510
 511void cw1200_update_filtering_work(struct work_struct *work)
 512{
 513        struct cw1200_common *priv =
 514                container_of(work, struct cw1200_common,
 515                             update_filtering_work);
 516
 517        cw1200_update_filtering(priv);
 518}
 519
 520void cw1200_set_beacon_wakeup_period_work(struct work_struct *work)
 521{
 522        struct cw1200_common *priv =
 523                container_of(work, struct cw1200_common,
 524                             set_beacon_wakeup_period_work);
 525
 526        wsm_set_beacon_wakeup_period(priv,
 527                                     priv->beacon_int * priv->join_dtim_period >
 528                                     MAX_BEACON_SKIP_TIME_MS ? 1 :
 529                                     priv->join_dtim_period, 0);
 530}
 531
 532u64 cw1200_prepare_multicast(struct ieee80211_hw *hw,
 533                             struct netdev_hw_addr_list *mc_list)
 534{
 535        static u8 broadcast_ipv6[ETH_ALEN] = {
 536                0x33, 0x33, 0x00, 0x00, 0x00, 0x01
 537        };
 538        static u8 broadcast_ipv4[ETH_ALEN] = {
 539                0x01, 0x00, 0x5e, 0x00, 0x00, 0x01
 540        };
 541        struct cw1200_common *priv = hw->priv;
 542        struct netdev_hw_addr *ha;
 543        int count = 0;
 544
 545        /* Disable multicast filtering */
 546        priv->has_multicast_subscription = false;
 547        memset(&priv->multicast_filter, 0x00, sizeof(priv->multicast_filter));
 548
 549        if (netdev_hw_addr_list_count(mc_list) > WSM_MAX_GRP_ADDRTABLE_ENTRIES)
 550                return 0;
 551
 552        /* Enable if requested */
 553        netdev_hw_addr_list_for_each(ha, mc_list) {
 554                pr_debug("[STA] multicast: %pM\n", ha->addr);
 555                memcpy(&priv->multicast_filter.macaddrs[count],
 556                       ha->addr, ETH_ALEN);
 557                if (!ether_addr_equal(ha->addr, broadcast_ipv4) &&
 558                    !ether_addr_equal(ha->addr, broadcast_ipv6))
 559                        priv->has_multicast_subscription = true;
 560                count++;
 561        }
 562
 563        if (count) {
 564                priv->multicast_filter.enable = __cpu_to_le32(1);
 565                priv->multicast_filter.num_addrs = __cpu_to_le32(count);
 566        }
 567
 568        return netdev_hw_addr_list_count(mc_list);
 569}
 570
 571void cw1200_configure_filter(struct ieee80211_hw *dev,
 572                             unsigned int changed_flags,
 573                             unsigned int *total_flags,
 574                             u64 multicast)
 575{
 576        struct cw1200_common *priv = dev->priv;
 577        bool listening = !!(*total_flags &
 578                            (FIF_OTHER_BSS |
 579                             FIF_BCN_PRBRESP_PROMISC |
 580                             FIF_PROBE_REQ));
 581
 582        *total_flags &= FIF_OTHER_BSS |
 583                        FIF_FCSFAIL |
 584                        FIF_BCN_PRBRESP_PROMISC |
 585                        FIF_PROBE_REQ;
 586
 587        down(&priv->scan.lock);
 588        mutex_lock(&priv->conf_mutex);
 589
 590        priv->rx_filter.promiscuous = 0;
 591        priv->rx_filter.bssid = (*total_flags & (FIF_OTHER_BSS |
 592                        FIF_PROBE_REQ)) ? 1 : 0;
 593        priv->rx_filter.fcs = (*total_flags & FIF_FCSFAIL) ? 1 : 0;
 594        priv->disable_beacon_filter = !(*total_flags &
 595                                        (FIF_BCN_PRBRESP_PROMISC |
 596                                         FIF_PROBE_REQ));
 597        if (priv->listening != listening) {
 598                priv->listening = listening;
 599                wsm_lock_tx(priv);
 600                cw1200_update_listening(priv, listening);
 601                wsm_unlock_tx(priv);
 602        }
 603        cw1200_update_filtering(priv);
 604        mutex_unlock(&priv->conf_mutex);
 605        up(&priv->scan.lock);
 606}
 607
 608int cw1200_conf_tx(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
 609                   u16 queue, const struct ieee80211_tx_queue_params *params)
 610{
 611        struct cw1200_common *priv = dev->priv;
 612        int ret = 0;
 613        /* To prevent re-applying PM request OID again and again*/
 614        bool old_uapsd_flags;
 615
 616        mutex_lock(&priv->conf_mutex);
 617
 618        if (queue < dev->queues) {
 619                old_uapsd_flags = le16_to_cpu(priv->uapsd_info.uapsd_flags);
 620
 621                WSM_TX_QUEUE_SET(&priv->tx_queue_params, queue, 0, 0, 0);
 622                ret = wsm_set_tx_queue_params(priv,
 623                                              &priv->tx_queue_params.params[queue], queue);
 624                if (ret) {
 625                        ret = -EINVAL;
 626                        goto out;
 627                }
 628
 629                WSM_EDCA_SET(&priv->edca, queue, params->aifs,
 630                             params->cw_min, params->cw_max,
 631                             params->txop, 0xc8,
 632                             params->uapsd);
 633                ret = wsm_set_edca_params(priv, &priv->edca);
 634                if (ret) {
 635                        ret = -EINVAL;
 636                        goto out;
 637                }
 638
 639                if (priv->mode == NL80211_IFTYPE_STATION) {
 640                        ret = cw1200_set_uapsd_param(priv, &priv->edca);
 641                        if (!ret && priv->setbssparams_done &&
 642                            (priv->join_status == CW1200_JOIN_STATUS_STA) &&
 643                            (old_uapsd_flags != le16_to_cpu(priv->uapsd_info.uapsd_flags)))
 644                                ret = cw1200_set_pm(priv, &priv->powersave_mode);
 645                }
 646        } else {
 647                ret = -EINVAL;
 648        }
 649
 650out:
 651        mutex_unlock(&priv->conf_mutex);
 652        return ret;
 653}
 654
 655int cw1200_get_stats(struct ieee80211_hw *dev,
 656                     struct ieee80211_low_level_stats *stats)
 657{
 658        struct cw1200_common *priv = dev->priv;
 659
 660        memcpy(stats, &priv->stats, sizeof(*stats));
 661        return 0;
 662}
 663
 664int cw1200_set_pm(struct cw1200_common *priv, const struct wsm_set_pm *arg)
 665{
 666        struct wsm_set_pm pm = *arg;
 667
 668        if (priv->uapsd_info.uapsd_flags != 0)
 669                pm.mode &= ~WSM_PSM_FAST_PS_FLAG;
 670
 671        if (memcmp(&pm, &priv->firmware_ps_mode,
 672                   sizeof(struct wsm_set_pm))) {
 673                priv->firmware_ps_mode = pm;
 674                return wsm_set_pm(priv, &pm);
 675        } else {
 676                return 0;
 677        }
 678}
 679
 680int cw1200_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
 681                   struct ieee80211_vif *vif, struct ieee80211_sta *sta,
 682                   struct ieee80211_key_conf *key)
 683{
 684        int ret = -EOPNOTSUPP;
 685        struct cw1200_common *priv = dev->priv;
 686        struct ieee80211_key_seq seq;
 687
 688        mutex_lock(&priv->conf_mutex);
 689
 690        if (cmd == SET_KEY) {
 691                u8 *peer_addr = NULL;
 692                int pairwise = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) ?
 693                        1 : 0;
 694                int idx = cw1200_alloc_key(priv);
 695                struct wsm_add_key *wsm_key = &priv->keys[idx];
 696
 697                if (idx < 0) {
 698                        ret = -EINVAL;
 699                        goto finally;
 700                }
 701
 702                if (sta)
 703                        peer_addr = sta->addr;
 704
 705                key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE |
 706                              IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
 707
 708                switch (key->cipher) {
 709                case WLAN_CIPHER_SUITE_WEP40:
 710                case WLAN_CIPHER_SUITE_WEP104:
 711                        if (key->keylen > 16) {
 712                                cw1200_free_key(priv, idx);
 713                                ret = -EINVAL;
 714                                goto finally;
 715                        }
 716
 717                        if (pairwise) {
 718                                wsm_key->type = WSM_KEY_TYPE_WEP_PAIRWISE;
 719                                memcpy(wsm_key->wep_pairwise.peer,
 720                                       peer_addr, ETH_ALEN);
 721                                memcpy(wsm_key->wep_pairwise.keydata,
 722                                       &key->key[0], key->keylen);
 723                                wsm_key->wep_pairwise.keylen = key->keylen;
 724                        } else {
 725                                wsm_key->type = WSM_KEY_TYPE_WEP_DEFAULT;
 726                                memcpy(wsm_key->wep_group.keydata,
 727                                       &key->key[0], key->keylen);
 728                                wsm_key->wep_group.keylen = key->keylen;
 729                                wsm_key->wep_group.keyid = key->keyidx;
 730                        }
 731                        break;
 732                case WLAN_CIPHER_SUITE_TKIP:
 733                        ieee80211_get_key_rx_seq(key, 0, &seq);
 734                        if (pairwise) {
 735                                wsm_key->type = WSM_KEY_TYPE_TKIP_PAIRWISE;
 736                                memcpy(wsm_key->tkip_pairwise.peer,
 737                                       peer_addr, ETH_ALEN);
 738                                memcpy(wsm_key->tkip_pairwise.keydata,
 739                                       &key->key[0], 16);
 740                                memcpy(wsm_key->tkip_pairwise.tx_mic_key,
 741                                       &key->key[16], 8);
 742                                memcpy(wsm_key->tkip_pairwise.rx_mic_key,
 743                                       &key->key[24], 8);
 744                        } else {
 745                                size_t mic_offset =
 746                                        (priv->mode == NL80211_IFTYPE_AP) ?
 747                                        16 : 24;
 748                                wsm_key->type = WSM_KEY_TYPE_TKIP_GROUP;
 749                                memcpy(wsm_key->tkip_group.keydata,
 750                                       &key->key[0], 16);
 751                                memcpy(wsm_key->tkip_group.rx_mic_key,
 752                                       &key->key[mic_offset], 8);
 753
 754                                wsm_key->tkip_group.rx_seqnum[0] = seq.tkip.iv16 & 0xff;
 755                                wsm_key->tkip_group.rx_seqnum[1] = (seq.tkip.iv16 >> 8) & 0xff;
 756                                wsm_key->tkip_group.rx_seqnum[2] = seq.tkip.iv32 & 0xff;
 757                                wsm_key->tkip_group.rx_seqnum[3] = (seq.tkip.iv32 >> 8) & 0xff;
 758                                wsm_key->tkip_group.rx_seqnum[4] = (seq.tkip.iv32 >> 16) & 0xff;
 759                                wsm_key->tkip_group.rx_seqnum[5] = (seq.tkip.iv32 >> 24) & 0xff;
 760                                wsm_key->tkip_group.rx_seqnum[6] = 0;
 761                                wsm_key->tkip_group.rx_seqnum[7] = 0;
 762
 763                                wsm_key->tkip_group.keyid = key->keyidx;
 764                        }
 765                        break;
 766                case WLAN_CIPHER_SUITE_CCMP:
 767                        ieee80211_get_key_rx_seq(key, 0, &seq);
 768                        if (pairwise) {
 769                                wsm_key->type = WSM_KEY_TYPE_AES_PAIRWISE;
 770                                memcpy(wsm_key->aes_pairwise.peer,
 771                                       peer_addr, ETH_ALEN);
 772                                memcpy(wsm_key->aes_pairwise.keydata,
 773                                       &key->key[0], 16);
 774                        } else {
 775                                wsm_key->type = WSM_KEY_TYPE_AES_GROUP;
 776                                memcpy(wsm_key->aes_group.keydata,
 777                                       &key->key[0], 16);
 778
 779                                wsm_key->aes_group.rx_seqnum[0] = seq.ccmp.pn[5];
 780                                wsm_key->aes_group.rx_seqnum[1] = seq.ccmp.pn[4];
 781                                wsm_key->aes_group.rx_seqnum[2] = seq.ccmp.pn[3];
 782                                wsm_key->aes_group.rx_seqnum[3] = seq.ccmp.pn[2];
 783                                wsm_key->aes_group.rx_seqnum[4] = seq.ccmp.pn[1];
 784                                wsm_key->aes_group.rx_seqnum[5] = seq.ccmp.pn[0];
 785                                wsm_key->aes_group.rx_seqnum[6] = 0;
 786                                wsm_key->aes_group.rx_seqnum[7] = 0;
 787                                wsm_key->aes_group.keyid = key->keyidx;
 788                        }
 789                        break;
 790                case WLAN_CIPHER_SUITE_SMS4:
 791                        if (pairwise) {
 792                                wsm_key->type = WSM_KEY_TYPE_WAPI_PAIRWISE;
 793                                memcpy(wsm_key->wapi_pairwise.peer,
 794                                       peer_addr, ETH_ALEN);
 795                                memcpy(wsm_key->wapi_pairwise.keydata,
 796                                       &key->key[0], 16);
 797                                memcpy(wsm_key->wapi_pairwise.mic_key,
 798                                       &key->key[16], 16);
 799                                wsm_key->wapi_pairwise.keyid = key->keyidx;
 800                        } else {
 801                                wsm_key->type = WSM_KEY_TYPE_WAPI_GROUP;
 802                                memcpy(wsm_key->wapi_group.keydata,
 803                                       &key->key[0],  16);
 804                                memcpy(wsm_key->wapi_group.mic_key,
 805                                       &key->key[16], 16);
 806                                wsm_key->wapi_group.keyid = key->keyidx;
 807                        }
 808                        break;
 809                default:
 810                        pr_warn("Unhandled key type %d\n", key->cipher);
 811                        cw1200_free_key(priv, idx);
 812                        ret = -EOPNOTSUPP;
 813                        goto finally;
 814                }
 815                ret = wsm_add_key(priv, wsm_key);
 816                if (!ret)
 817                        key->hw_key_idx = idx;
 818                else
 819                        cw1200_free_key(priv, idx);
 820        } else if (cmd == DISABLE_KEY) {
 821                struct wsm_remove_key wsm_key = {
 822                        .index = key->hw_key_idx,
 823                };
 824
 825                if (wsm_key.index > WSM_KEY_MAX_INDEX) {
 826                        ret = -EINVAL;
 827                        goto finally;
 828                }
 829
 830                cw1200_free_key(priv, wsm_key.index);
 831                ret = wsm_remove_key(priv, &wsm_key);
 832        } else {
 833                pr_warn("Unhandled key command %d\n", cmd);
 834        }
 835
 836finally:
 837        mutex_unlock(&priv->conf_mutex);
 838        return ret;
 839}
 840
 841void cw1200_wep_key_work(struct work_struct *work)
 842{
 843        struct cw1200_common *priv =
 844                container_of(work, struct cw1200_common, wep_key_work);
 845        u8 queue_id = cw1200_queue_get_queue_id(priv->pending_frame_id);
 846        struct cw1200_queue *queue = &priv->tx_queue[queue_id];
 847        __le32 wep_default_key_id = __cpu_to_le32(
 848                priv->wep_default_key_id);
 849
 850        pr_debug("[STA] Setting default WEP key: %d\n",
 851                 priv->wep_default_key_id);
 852        wsm_flush_tx(priv);
 853        wsm_write_mib(priv, WSM_MIB_ID_DOT11_WEP_DEFAULT_KEY_ID,
 854                      &wep_default_key_id, sizeof(wep_default_key_id));
 855        cw1200_queue_requeue(queue, priv->pending_frame_id);
 856        wsm_unlock_tx(priv);
 857}
 858
 859int cw1200_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
 860{
 861        int ret = 0;
 862        __le32 val32;
 863        struct cw1200_common *priv = hw->priv;
 864
 865        if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
 866                return 0;
 867
 868        if (value != (u32) -1)
 869                val32 = __cpu_to_le32(value);
 870        else
 871                val32 = 0; /* disabled */
 872
 873        if (priv->rts_threshold == value)
 874                goto out;
 875
 876        pr_debug("[STA] Setting RTS threshold: %d\n",
 877                 priv->rts_threshold);
 878
 879        /* mutex_lock(&priv->conf_mutex); */
 880        ret = wsm_write_mib(priv, WSM_MIB_ID_DOT11_RTS_THRESHOLD,
 881                            &val32, sizeof(val32));
 882        if (!ret)
 883                priv->rts_threshold = value;
 884        /* mutex_unlock(&priv->conf_mutex); */
 885
 886out:
 887        return ret;
 888}
 889
 890/* If successful, LOCKS the TX queue! */
 891static int __cw1200_flush(struct cw1200_common *priv, bool drop)
 892{
 893        int i, ret;
 894
 895        for (;;) {
 896                /* TODO: correct flush handling is required when dev_stop.
 897                 * Temporary workaround: 2s
 898                 */
 899                if (drop) {
 900                        for (i = 0; i < 4; ++i)
 901                                cw1200_queue_clear(&priv->tx_queue[i]);
 902                } else {
 903                        ret = wait_event_timeout(
 904                                priv->tx_queue_stats.wait_link_id_empty,
 905                                cw1200_queue_stats_is_empty(
 906                                        &priv->tx_queue_stats, -1),
 907                                2 * HZ);
 908                }
 909
 910                if (!drop && ret <= 0) {
 911                        ret = -ETIMEDOUT;
 912                        break;
 913                } else {
 914                        ret = 0;
 915                }
 916
 917                wsm_lock_tx(priv);
 918                if (!cw1200_queue_stats_is_empty(&priv->tx_queue_stats, -1)) {
 919                        /* Highly unlikely: WSM requeued frames. */
 920                        wsm_unlock_tx(priv);
 921                        continue;
 922                }
 923                break;
 924        }
 925        return ret;
 926}
 927
 928void cw1200_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 929                  u32 queues, bool drop)
 930{
 931        struct cw1200_common *priv = hw->priv;
 932
 933        switch (priv->mode) {
 934        case NL80211_IFTYPE_MONITOR:
 935                drop = true;
 936                break;
 937        case NL80211_IFTYPE_AP:
 938                if (!priv->enable_beacon)
 939                        drop = true;
 940                break;
 941        }
 942
 943        if (!__cw1200_flush(priv, drop))
 944                wsm_unlock_tx(priv);
 945
 946        return;
 947}
 948
 949/* ******************************************************************** */
 950/* WSM callbacks                                                        */
 951
 952void cw1200_free_event_queue(struct cw1200_common *priv)
 953{
 954        LIST_HEAD(list);
 955
 956        spin_lock(&priv->event_queue_lock);
 957        list_splice_init(&priv->event_queue, &list);
 958        spin_unlock(&priv->event_queue_lock);
 959
 960        __cw1200_free_event_queue(&list);
 961}
 962
 963void cw1200_event_handler(struct work_struct *work)
 964{
 965        struct cw1200_common *priv =
 966                container_of(work, struct cw1200_common, event_handler);
 967        struct cw1200_wsm_event *event;
 968        LIST_HEAD(list);
 969
 970        spin_lock(&priv->event_queue_lock);
 971        list_splice_init(&priv->event_queue, &list);
 972        spin_unlock(&priv->event_queue_lock);
 973
 974        list_for_each_entry(event, &list, link) {
 975                switch (event->evt.id) {
 976                case WSM_EVENT_ERROR:
 977                        pr_err("Unhandled WSM Error from LMAC\n");
 978                        break;
 979                case WSM_EVENT_BSS_LOST:
 980                        pr_debug("[CQM] BSS lost.\n");
 981                        cancel_work_sync(&priv->unjoin_work);
 982                        if (!down_trylock(&priv->scan.lock)) {
 983                                cw1200_cqm_bssloss_sm(priv, 1, 0, 0);
 984                                up(&priv->scan.lock);
 985                        } else {
 986                                /* Scan is in progress. Delay reporting.
 987                                 * Scan complete will trigger bss_loss_work
 988                                 */
 989                                priv->delayed_link_loss = 1;
 990                                /* Also start a watchdog. */
 991                                queue_delayed_work(priv->workqueue,
 992                                                   &priv->bss_loss_work, 5*HZ);
 993                        }
 994                        break;
 995                case WSM_EVENT_BSS_REGAINED:
 996                        pr_debug("[CQM] BSS regained.\n");
 997                        cw1200_cqm_bssloss_sm(priv, 0, 0, 0);
 998                        cancel_work_sync(&priv->unjoin_work);
 999                        break;
1000                case WSM_EVENT_RADAR_DETECTED:
1001                        wiphy_info(priv->hw->wiphy, "radar pulse detected\n");
1002                        break;
1003                case WSM_EVENT_RCPI_RSSI:
1004                {
1005                        /* RSSI: signed Q8.0, RCPI: unsigned Q7.1
1006                         * RSSI = RCPI / 2 - 110
1007                         */
1008                        int rcpi_rssi = (int)(event->evt.data & 0xFF);
1009                        int cqm_evt;
1010                        if (priv->cqm_use_rssi)
1011                                rcpi_rssi = (s8)rcpi_rssi;
1012                        else
1013                                rcpi_rssi =  rcpi_rssi / 2 - 110;
1014
1015                        cqm_evt = (rcpi_rssi <= priv->cqm_rssi_thold) ?
1016                                NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW :
1017                                NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
1018                        pr_debug("[CQM] RSSI event: %d.\n", rcpi_rssi);
1019                        ieee80211_cqm_rssi_notify(priv->vif, cqm_evt, rcpi_rssi,
1020                                                  GFP_KERNEL);
1021                        break;
1022                }
1023                case WSM_EVENT_BT_INACTIVE:
1024                        pr_warn("Unhandled BT INACTIVE from LMAC\n");
1025                        break;
1026                case WSM_EVENT_BT_ACTIVE:
1027                        pr_warn("Unhandled BT ACTIVE from LMAC\n");
1028                        break;
1029                }
1030        }
1031        __cw1200_free_event_queue(&list);
1032}
1033
1034void cw1200_bss_loss_work(struct work_struct *work)
1035{
1036        struct cw1200_common *priv =
1037                container_of(work, struct cw1200_common, bss_loss_work.work);
1038
1039        pr_debug("[CQM] Reporting connection loss.\n");
1040        wsm_lock_tx(priv);
1041        if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
1042                wsm_unlock_tx(priv);
1043}
1044
1045void cw1200_bss_params_work(struct work_struct *work)
1046{
1047        struct cw1200_common *priv =
1048                container_of(work, struct cw1200_common, bss_params_work);
1049        mutex_lock(&priv->conf_mutex);
1050
1051        priv->bss_params.reset_beacon_loss = 1;
1052        wsm_set_bss_params(priv, &priv->bss_params);
1053        priv->bss_params.reset_beacon_loss = 0;
1054
1055        mutex_unlock(&priv->conf_mutex);
1056}
1057
1058/* ******************************************************************** */
1059/* Internal API                                                         */
1060
1061/* This function is called to Parse the SDD file
1062 * to extract listen_interval and PTA related information
1063 * sdd is a TLV: u8 id, u8 len, u8 data[]
1064 */
1065static int cw1200_parse_sdd_file(struct cw1200_common *priv)
1066{
1067        const u8 *p = priv->sdd->data;
1068        int ret = 0;
1069
1070        while (p + 2 <= priv->sdd->data + priv->sdd->size) {
1071                if (p + p[1] + 2 > priv->sdd->data + priv->sdd->size) {
1072                        pr_warn("Malformed sdd structure\n");
1073                        return -1;
1074                }
1075                switch (p[0]) {
1076                case SDD_PTA_CFG_ELT_ID: {
1077                        u16 v;
1078                        if (p[1] < 4) {
1079                                pr_warn("SDD_PTA_CFG_ELT_ID malformed\n");
1080                                ret = -1;
1081                                break;
1082                        }
1083                        v = le16_to_cpu(*((__le16 *)(p + 2)));
1084                        if (!v)  /* non-zero means this is enabled */
1085                                break;
1086
1087                        v = le16_to_cpu(*((__le16 *)(p + 4)));
1088                        priv->conf_listen_interval = (v >> 7) & 0x1F;
1089                        pr_debug("PTA found; Listen Interval %d\n",
1090                                 priv->conf_listen_interval);
1091                        break;
1092                }
1093                case SDD_REFERENCE_FREQUENCY_ELT_ID: {
1094                        u16 clk = le16_to_cpu(*((__le16 *)(p + 2)));
1095                        if (clk != priv->hw_refclk)
1096                                pr_warn("SDD file doesn't match configured refclk (%d vs %d)\n",
1097                                        clk, priv->hw_refclk);
1098                        break;
1099                }
1100                default:
1101                        break;
1102                }
1103                p += p[1] + 2;
1104        }
1105
1106        if (!priv->bt_present) {
1107                pr_debug("PTA element NOT found.\n");
1108                priv->conf_listen_interval = 0;
1109        }
1110        return ret;
1111}
1112
1113int cw1200_setup_mac(struct cw1200_common *priv)
1114{
1115        int ret = 0;
1116
1117        /* NOTE: There is a bug in FW: it reports signal
1118         * as RSSI if RSSI subscription is enabled.
1119         * It's not enough to set WSM_RCPI_RSSI_USE_RSSI.
1120         *
1121         * NOTE2: RSSI based reports have been switched to RCPI, since
1122         * FW has a bug and RSSI reported values are not stable,
1123         * what can lead to signal level oscilations in user-end applications
1124         */
1125        struct wsm_rcpi_rssi_threshold threshold = {
1126                .rssiRcpiMode = WSM_RCPI_RSSI_THRESHOLD_ENABLE |
1127                WSM_RCPI_RSSI_DONT_USE_UPPER |
1128                WSM_RCPI_RSSI_DONT_USE_LOWER,
1129                .rollingAverageCount = 16,
1130        };
1131
1132        struct wsm_configuration cfg = {
1133                .dot11StationId = &priv->mac_addr[0],
1134        };
1135
1136        /* Remember the decission here to make sure, we will handle
1137         * the RCPI/RSSI value correctly on WSM_EVENT_RCPI_RSS
1138         */
1139        if (threshold.rssiRcpiMode & WSM_RCPI_RSSI_USE_RSSI)
1140                priv->cqm_use_rssi = true;
1141
1142        if (!priv->sdd) {
1143                ret = request_firmware(&priv->sdd, priv->sdd_path, priv->pdev);
1144                if (ret) {
1145                        pr_err("Can't load sdd file %s.\n", priv->sdd_path);
1146                        return ret;
1147                }
1148                cw1200_parse_sdd_file(priv);
1149        }
1150
1151        cfg.dpdData = priv->sdd->data;
1152        cfg.dpdData_size = priv->sdd->size;
1153        ret = wsm_configuration(priv, &cfg);
1154        if (ret)
1155                return ret;
1156
1157        /* Configure RSSI/SCPI reporting as RSSI. */
1158        wsm_set_rcpi_rssi_threshold(priv, &threshold);
1159
1160        return 0;
1161}
1162
1163static void cw1200_join_complete(struct cw1200_common *priv)
1164{
1165        pr_debug("[STA] Join complete (%d)\n", priv->join_complete_status);
1166
1167        priv->join_pending = false;
1168        if (priv->join_complete_status) {
1169                priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
1170                cw1200_update_listening(priv, priv->listening);
1171                cw1200_do_unjoin(priv);
1172                ieee80211_connection_loss(priv->vif);
1173        } else {
1174                if (priv->mode == NL80211_IFTYPE_ADHOC)
1175                        priv->join_status = CW1200_JOIN_STATUS_IBSS;
1176                else
1177                        priv->join_status = CW1200_JOIN_STATUS_PRE_STA;
1178        }
1179        wsm_unlock_tx(priv); /* Clearing the lock held before do_join() */
1180}
1181
1182void cw1200_join_complete_work(struct work_struct *work)
1183{
1184        struct cw1200_common *priv =
1185                container_of(work, struct cw1200_common, join_complete_work);
1186        mutex_lock(&priv->conf_mutex);
1187        cw1200_join_complete(priv);
1188        mutex_unlock(&priv->conf_mutex);
1189}
1190
1191void cw1200_join_complete_cb(struct cw1200_common *priv,
1192                             struct wsm_join_complete *arg)
1193{
1194        pr_debug("[STA] cw1200_join_complete_cb called, status=%d.\n",
1195                 arg->status);
1196
1197        if (cancel_delayed_work(&priv->join_timeout)) {
1198                priv->join_complete_status = arg->status;
1199                queue_work(priv->workqueue, &priv->join_complete_work);
1200        }
1201}
1202
1203/* MUST be called with tx_lock held!  It will be unlocked for us. */
1204static void cw1200_do_join(struct cw1200_common *priv)
1205{
1206        const u8 *bssid;
1207        struct ieee80211_bss_conf *conf = &priv->vif->bss_conf;
1208        struct cfg80211_bss *bss = NULL;
1209        struct wsm_protected_mgmt_policy mgmt_policy;
1210        struct wsm_join join = {
1211                .mode = conf->ibss_joined ?
1212                                WSM_JOIN_MODE_IBSS : WSM_JOIN_MODE_BSS,
1213                .preamble_type = WSM_JOIN_PREAMBLE_LONG,
1214                .probe_for_join = 1,
1215                .atim_window = 0,
1216                .basic_rate_set = cw1200_rate_mask_to_wsm(priv,
1217                                                          conf->basic_rates),
1218        };
1219        if (delayed_work_pending(&priv->join_timeout)) {
1220                pr_warn("[STA] - Join request already pending, skipping..\n");
1221                wsm_unlock_tx(priv);
1222                return;
1223        }
1224
1225        if (priv->join_status)
1226                cw1200_do_unjoin(priv);
1227
1228        bssid = priv->vif->bss_conf.bssid;
1229
1230        bss = cfg80211_get_bss(priv->hw->wiphy, priv->channel, bssid, NULL, 0,
1231                               IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
1232
1233        if (!bss && !conf->ibss_joined) {
1234                wsm_unlock_tx(priv);
1235                return;
1236        }
1237
1238        mutex_lock(&priv->conf_mutex);
1239
1240        /* Under the conf lock: check scan status and
1241         * bail out if it is in progress.
1242         */
1243        if (atomic_read(&priv->scan.in_progress)) {
1244                wsm_unlock_tx(priv);
1245                goto done_put;
1246        }
1247
1248        priv->join_pending = true;
1249
1250        /* Sanity check basic rates */
1251        if (!join.basic_rate_set)
1252                join.basic_rate_set = 7;
1253
1254        /* Sanity check beacon interval */
1255        if (!priv->beacon_int)
1256                priv->beacon_int = 1;
1257
1258        join.beacon_interval = priv->beacon_int;
1259
1260        /* BT Coex related changes */
1261        if (priv->bt_present) {
1262                if (((priv->conf_listen_interval * 100) %
1263                     priv->beacon_int) == 0)
1264                        priv->listen_interval =
1265                                ((priv->conf_listen_interval * 100) /
1266                                 priv->beacon_int);
1267                else
1268                        priv->listen_interval =
1269                                ((priv->conf_listen_interval * 100) /
1270                                 priv->beacon_int + 1);
1271        }
1272
1273        if (priv->hw->conf.ps_dtim_period)
1274                priv->join_dtim_period = priv->hw->conf.ps_dtim_period;
1275        join.dtim_period = priv->join_dtim_period;
1276
1277        join.channel_number = priv->channel->hw_value;
1278        join.band = (priv->channel->band == NL80211_BAND_5GHZ) ?
1279                WSM_PHY_BAND_5G : WSM_PHY_BAND_2_4G;
1280
1281        memcpy(join.bssid, bssid, sizeof(join.bssid));
1282
1283        pr_debug("[STA] Join BSSID: %pM DTIM: %d, interval: %d\n",
1284                 join.bssid,
1285                 join.dtim_period, priv->beacon_int);
1286
1287        if (!conf->ibss_joined) {
1288                const u8 *ssidie;
1289                rcu_read_lock();
1290                ssidie = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
1291                if (ssidie) {
1292                        join.ssid_len = ssidie[1];
1293                        memcpy(join.ssid, &ssidie[2], join.ssid_len);
1294                }
1295                rcu_read_unlock();
1296        }
1297
1298        if (priv->vif->p2p) {
1299                join.flags |= WSM_JOIN_FLAGS_P2P_GO;
1300                join.basic_rate_set =
1301                        cw1200_rate_mask_to_wsm(priv, 0xFF0);
1302        }
1303
1304        /* Enable asynchronous join calls */
1305        if (!conf->ibss_joined) {
1306                join.flags |= WSM_JOIN_FLAGS_FORCE;
1307                join.flags |= WSM_JOIN_FLAGS_FORCE_WITH_COMPLETE_IND;
1308        }
1309
1310        wsm_flush_tx(priv);
1311
1312        /* Stay Awake for Join and Auth Timeouts and a bit more */
1313        cw1200_pm_stay_awake(&priv->pm_state,
1314                             CW1200_JOIN_TIMEOUT + CW1200_AUTH_TIMEOUT);
1315
1316        cw1200_update_listening(priv, false);
1317
1318        /* Turn on Block ACKs */
1319        wsm_set_block_ack_policy(priv, priv->ba_tx_tid_mask,
1320                                 priv->ba_rx_tid_mask);
1321
1322        /* Set up timeout */
1323        if (join.flags & WSM_JOIN_FLAGS_FORCE_WITH_COMPLETE_IND) {
1324                priv->join_status = CW1200_JOIN_STATUS_JOINING;
1325                queue_delayed_work(priv->workqueue,
1326                                   &priv->join_timeout,
1327                                   CW1200_JOIN_TIMEOUT);
1328        }
1329
1330        /* 802.11w protected mgmt frames */
1331        mgmt_policy.protectedMgmtEnable = 0;
1332        mgmt_policy.unprotectedMgmtFramesAllowed = 1;
1333        mgmt_policy.encryptionForAuthFrame = 1;
1334        wsm_set_protected_mgmt_policy(priv, &mgmt_policy);
1335
1336        /* Perform actual join */
1337        if (wsm_join(priv, &join)) {
1338                pr_err("[STA] cw1200_join_work: wsm_join failed!\n");
1339                cancel_delayed_work_sync(&priv->join_timeout);
1340                cw1200_update_listening(priv, priv->listening);
1341                /* Tx lock still held, unjoin will clear it. */
1342                if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
1343                        wsm_unlock_tx(priv);
1344        } else {
1345                if (!(join.flags & WSM_JOIN_FLAGS_FORCE_WITH_COMPLETE_IND))
1346                        cw1200_join_complete(priv); /* Will clear tx_lock */
1347
1348                /* Upload keys */
1349                cw1200_upload_keys(priv);
1350
1351                /* Due to beacon filtering it is possible that the
1352                 * AP's beacon is not known for the mac80211 stack.
1353                 * Disable filtering temporary to make sure the stack
1354                 * receives at least one
1355                 */
1356                priv->disable_beacon_filter = true;
1357        }
1358        cw1200_update_filtering(priv);
1359
1360done_put:
1361        mutex_unlock(&priv->conf_mutex);
1362        if (bss)
1363                cfg80211_put_bss(priv->hw->wiphy, bss);
1364}
1365
1366void cw1200_join_timeout(struct work_struct *work)
1367{
1368        struct cw1200_common *priv =
1369                container_of(work, struct cw1200_common, join_timeout.work);
1370        pr_debug("[WSM] Join timed out.\n");
1371        wsm_lock_tx(priv);
1372        if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
1373                wsm_unlock_tx(priv);
1374}
1375
1376static void cw1200_do_unjoin(struct cw1200_common *priv)
1377{
1378        struct wsm_reset reset = {
1379                .reset_statistics = true,
1380        };
1381
1382        cancel_delayed_work_sync(&priv->join_timeout);
1383
1384        mutex_lock(&priv->conf_mutex);
1385        priv->join_pending = false;
1386
1387        if (atomic_read(&priv->scan.in_progress)) {
1388                if (priv->delayed_unjoin)
1389                        wiphy_dbg(priv->hw->wiphy, "Delayed unjoin is already scheduled.\n");
1390                else
1391                        priv->delayed_unjoin = true;
1392                goto done;
1393        }
1394
1395        priv->delayed_link_loss = false;
1396
1397        if (!priv->join_status)
1398                goto done;
1399
1400        if (priv->join_status == CW1200_JOIN_STATUS_AP)
1401                goto done;
1402
1403        cancel_work_sync(&priv->update_filtering_work);
1404        cancel_work_sync(&priv->set_beacon_wakeup_period_work);
1405        priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
1406
1407        /* Unjoin is a reset. */
1408        wsm_flush_tx(priv);
1409        wsm_keep_alive_period(priv, 0);
1410        wsm_reset(priv, &reset);
1411        wsm_set_output_power(priv, priv->output_power * 10);
1412        priv->join_dtim_period = 0;
1413        cw1200_setup_mac(priv);
1414        cw1200_free_event_queue(priv);
1415        cancel_work_sync(&priv->event_handler);
1416        cw1200_update_listening(priv, priv->listening);
1417        cw1200_cqm_bssloss_sm(priv, 0, 0, 0);
1418
1419        /* Disable Block ACKs */
1420        wsm_set_block_ack_policy(priv, 0, 0);
1421
1422        priv->disable_beacon_filter = false;
1423        cw1200_update_filtering(priv);
1424        memset(&priv->association_mode, 0,
1425               sizeof(priv->association_mode));
1426        memset(&priv->bss_params, 0, sizeof(priv->bss_params));
1427        priv->setbssparams_done = false;
1428        memset(&priv->firmware_ps_mode, 0,
1429               sizeof(priv->firmware_ps_mode));
1430
1431        pr_debug("[STA] Unjoin completed.\n");
1432
1433done:
1434        mutex_unlock(&priv->conf_mutex);
1435}
1436
1437void cw1200_unjoin_work(struct work_struct *work)
1438{
1439        struct cw1200_common *priv =
1440                container_of(work, struct cw1200_common, unjoin_work);
1441
1442        cw1200_do_unjoin(priv);
1443
1444        /* Tell the stack we're dead */
1445        ieee80211_connection_loss(priv->vif);
1446
1447        wsm_unlock_tx(priv);
1448}
1449
1450int cw1200_enable_listening(struct cw1200_common *priv)
1451{
1452        struct wsm_start start = {
1453                .mode = WSM_START_MODE_P2P_DEV,
1454                .band = WSM_PHY_BAND_2_4G,
1455                .beacon_interval = 100,
1456                .dtim_period = 1,
1457                .probe_delay = 0,
1458                .basic_rate_set = 0x0F,
1459        };
1460
1461        if (priv->channel) {
1462                start.band = priv->channel->band == NL80211_BAND_5GHZ ?
1463                             WSM_PHY_BAND_5G : WSM_PHY_BAND_2_4G;
1464                start.channel_number = priv->channel->hw_value;
1465        } else {
1466                start.band = WSM_PHY_BAND_2_4G;
1467                start.channel_number = 1;
1468        }
1469
1470        return wsm_start(priv, &start);
1471}
1472
1473int cw1200_disable_listening(struct cw1200_common *priv)
1474{
1475        int ret;
1476        struct wsm_reset reset = {
1477                .reset_statistics = true,
1478        };
1479        ret = wsm_reset(priv, &reset);
1480        return ret;
1481}
1482
1483void cw1200_update_listening(struct cw1200_common *priv, bool enabled)
1484{
1485        if (enabled) {
1486                if (priv->join_status == CW1200_JOIN_STATUS_PASSIVE) {
1487                        if (!cw1200_enable_listening(priv))
1488                                priv->join_status = CW1200_JOIN_STATUS_MONITOR;
1489                        wsm_set_probe_responder(priv, true);
1490                }
1491        } else {
1492                if (priv->join_status == CW1200_JOIN_STATUS_MONITOR) {
1493                        if (!cw1200_disable_listening(priv))
1494                                priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
1495                        wsm_set_probe_responder(priv, false);
1496                }
1497        }
1498}
1499
1500int cw1200_set_uapsd_param(struct cw1200_common *priv,
1501                           const struct wsm_edca_params *arg)
1502{
1503        int ret;
1504        u16 uapsd_flags = 0;
1505
1506        /* Here's the mapping AC [queue, bit]
1507         *  VO [0,3], VI [1, 2], BE [2, 1], BK [3, 0]
1508         */
1509
1510        if (arg->uapsd_enable[0])
1511                uapsd_flags |= 1 << 3;
1512
1513        if (arg->uapsd_enable[1])
1514                uapsd_flags |= 1 << 2;
1515
1516        if (arg->uapsd_enable[2])
1517                uapsd_flags |= 1 << 1;
1518
1519        if (arg->uapsd_enable[3])
1520                uapsd_flags |= 1;
1521
1522        /* Currently pseudo U-APSD operation is not supported, so setting
1523         * MinAutoTriggerInterval, MaxAutoTriggerInterval and
1524         * AutoTriggerStep to 0
1525         */
1526
1527        priv->uapsd_info.uapsd_flags = cpu_to_le16(uapsd_flags);
1528        priv->uapsd_info.min_auto_trigger_interval = 0;
1529        priv->uapsd_info.max_auto_trigger_interval = 0;
1530        priv->uapsd_info.auto_trigger_step = 0;
1531
1532        ret = wsm_set_uapsd_info(priv, &priv->uapsd_info);
1533        return ret;
1534}
1535
1536/* ******************************************************************** */
1537/* AP API                                                               */
1538
1539int cw1200_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1540                   struct ieee80211_sta *sta)
1541{
1542        struct cw1200_common *priv = hw->priv;
1543        struct cw1200_sta_priv *sta_priv =
1544                        (struct cw1200_sta_priv *)&sta->drv_priv;
1545        struct cw1200_link_entry *entry;
1546        struct sk_buff *skb;
1547
1548        if (priv->mode != NL80211_IFTYPE_AP)
1549                return 0;
1550
1551        sta_priv->link_id = cw1200_find_link_id(priv, sta->addr);
1552        if (WARN_ON(!sta_priv->link_id)) {
1553                wiphy_info(priv->hw->wiphy,
1554                           "[AP] No more link IDs available.\n");
1555                return -ENOENT;
1556        }
1557
1558        entry = &priv->link_id_db[sta_priv->link_id - 1];
1559        spin_lock_bh(&priv->ps_state_lock);
1560        if ((sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK) ==
1561                                        IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
1562                priv->sta_asleep_mask |= BIT(sta_priv->link_id);
1563        entry->status = CW1200_LINK_HARD;
1564        while ((skb = skb_dequeue(&entry->rx_queue)))
1565                ieee80211_rx_irqsafe(priv->hw, skb);
1566        spin_unlock_bh(&priv->ps_state_lock);
1567        return 0;
1568}
1569
1570int cw1200_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1571                      struct ieee80211_sta *sta)
1572{
1573        struct cw1200_common *priv = hw->priv;
1574        struct cw1200_sta_priv *sta_priv =
1575                        (struct cw1200_sta_priv *)&sta->drv_priv;
1576        struct cw1200_link_entry *entry;
1577
1578        if (priv->mode != NL80211_IFTYPE_AP || !sta_priv->link_id)
1579                return 0;
1580
1581        entry = &priv->link_id_db[sta_priv->link_id - 1];
1582        spin_lock_bh(&priv->ps_state_lock);
1583        entry->status = CW1200_LINK_RESERVE;
1584        entry->timestamp = jiffies;
1585        wsm_lock_tx_async(priv);
1586        if (queue_work(priv->workqueue, &priv->link_id_work) <= 0)
1587                wsm_unlock_tx(priv);
1588        spin_unlock_bh(&priv->ps_state_lock);
1589        flush_workqueue(priv->workqueue);
1590        return 0;
1591}
1592
1593static void __cw1200_sta_notify(struct ieee80211_hw *dev,
1594                                struct ieee80211_vif *vif,
1595                                enum sta_notify_cmd notify_cmd,
1596                                int link_id)
1597{
1598        struct cw1200_common *priv = dev->priv;
1599        u32 bit, prev;
1600
1601        /* Zero link id means "for all link IDs" */
1602        if (link_id)
1603                bit = BIT(link_id);
1604        else if (WARN_ON_ONCE(notify_cmd != STA_NOTIFY_AWAKE))
1605                bit = 0;
1606        else
1607                bit = priv->link_id_map;
1608        prev = priv->sta_asleep_mask & bit;
1609
1610        switch (notify_cmd) {
1611        case STA_NOTIFY_SLEEP:
1612                if (!prev) {
1613                        if (priv->buffered_multicasts &&
1614                            !priv->sta_asleep_mask)
1615                                queue_work(priv->workqueue,
1616                                           &priv->multicast_start_work);
1617                        priv->sta_asleep_mask |= bit;
1618                }
1619                break;
1620        case STA_NOTIFY_AWAKE:
1621                if (prev) {
1622                        priv->sta_asleep_mask &= ~bit;
1623                        priv->pspoll_mask &= ~bit;
1624                        if (priv->tx_multicast && link_id &&
1625                            !priv->sta_asleep_mask)
1626                                queue_work(priv->workqueue,
1627                                           &priv->multicast_stop_work);
1628                        cw1200_bh_wakeup(priv);
1629                }
1630                break;
1631        }
1632}
1633
1634void cw1200_sta_notify(struct ieee80211_hw *dev,
1635                       struct ieee80211_vif *vif,
1636                       enum sta_notify_cmd notify_cmd,
1637                       struct ieee80211_sta *sta)
1638{
1639        struct cw1200_common *priv = dev->priv;
1640        struct cw1200_sta_priv *sta_priv =
1641                (struct cw1200_sta_priv *)&sta->drv_priv;
1642
1643        spin_lock_bh(&priv->ps_state_lock);
1644        __cw1200_sta_notify(dev, vif, notify_cmd, sta_priv->link_id);
1645        spin_unlock_bh(&priv->ps_state_lock);
1646}
1647
1648static void cw1200_ps_notify(struct cw1200_common *priv,
1649                      int link_id, bool ps)
1650{
1651        if (link_id > CW1200_MAX_STA_IN_AP_MODE)
1652                return;
1653
1654        pr_debug("%s for LinkId: %d. STAs asleep: %.8X\n",
1655                 ps ? "Stop" : "Start",
1656                 link_id, priv->sta_asleep_mask);
1657
1658        __cw1200_sta_notify(priv->hw, priv->vif,
1659                            ps ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE, link_id);
1660}
1661
1662static int cw1200_set_tim_impl(struct cw1200_common *priv, bool aid0_bit_set)
1663{
1664        struct sk_buff *skb;
1665        struct wsm_update_ie update_ie = {
1666                .what = WSM_UPDATE_IE_BEACON,
1667                .count = 1,
1668        };
1669        u16 tim_offset, tim_length;
1670
1671        pr_debug("[AP] mcast: %s.\n", aid0_bit_set ? "ena" : "dis");
1672
1673        skb = ieee80211_beacon_get_tim(priv->hw, priv->vif,
1674                        &tim_offset, &tim_length);
1675        if (!skb) {
1676                if (!__cw1200_flush(priv, true))
1677                        wsm_unlock_tx(priv);
1678                return -ENOENT;
1679        }
1680
1681        if (tim_offset && tim_length >= 6) {
1682                /* Ignore DTIM count from mac80211:
1683                 * firmware handles DTIM internally.
1684                 */
1685                skb->data[tim_offset + 2] = 0;
1686
1687                /* Set/reset aid0 bit */
1688                if (aid0_bit_set)
1689                        skb->data[tim_offset + 4] |= 1;
1690                else
1691                        skb->data[tim_offset + 4] &= ~1;
1692        }
1693
1694        update_ie.ies = &skb->data[tim_offset];
1695        update_ie.length = tim_length;
1696        wsm_update_ie(priv, &update_ie);
1697
1698        dev_kfree_skb(skb);
1699
1700        return 0;
1701}
1702
1703void cw1200_set_tim_work(struct work_struct *work)
1704{
1705        struct cw1200_common *priv =
1706                container_of(work, struct cw1200_common, set_tim_work);
1707        (void)cw1200_set_tim_impl(priv, priv->aid0_bit_set);
1708}
1709
1710int cw1200_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
1711                   bool set)
1712{
1713        struct cw1200_common *priv = dev->priv;
1714        queue_work(priv->workqueue, &priv->set_tim_work);
1715        return 0;
1716}
1717
1718void cw1200_set_cts_work(struct work_struct *work)
1719{
1720        struct cw1200_common *priv =
1721                container_of(work, struct cw1200_common, set_cts_work);
1722
1723        u8 erp_ie[3] = {WLAN_EID_ERP_INFO, 0x1, 0};
1724        struct wsm_update_ie update_ie = {
1725                .what = WSM_UPDATE_IE_BEACON,
1726                .count = 1,
1727                .ies = erp_ie,
1728                .length = 3,
1729        };
1730        u32 erp_info;
1731        __le32 use_cts_prot;
1732        mutex_lock(&priv->conf_mutex);
1733        erp_info = priv->erp_info;
1734        mutex_unlock(&priv->conf_mutex);
1735        use_cts_prot =
1736                erp_info & WLAN_ERP_USE_PROTECTION ?
1737                __cpu_to_le32(1) : 0;
1738
1739        erp_ie[ERP_INFO_BYTE_OFFSET] = erp_info;
1740
1741        pr_debug("[STA] ERP information 0x%x\n", erp_info);
1742
1743        wsm_write_mib(priv, WSM_MIB_ID_NON_ERP_PROTECTION,
1744                      &use_cts_prot, sizeof(use_cts_prot));
1745        wsm_update_ie(priv, &update_ie);
1746
1747        return;
1748}
1749
1750static int cw1200_set_btcoexinfo(struct cw1200_common *priv)
1751{
1752        struct wsm_override_internal_txrate arg;
1753        int ret = 0;
1754
1755        if (priv->mode == NL80211_IFTYPE_STATION) {
1756                /* Plumb PSPOLL and NULL template */
1757                cw1200_upload_pspoll(priv);
1758                cw1200_upload_null(priv);
1759                cw1200_upload_qosnull(priv);
1760        } else {
1761                return 0;
1762        }
1763
1764        memset(&arg, 0, sizeof(struct wsm_override_internal_txrate));
1765
1766        if (!priv->vif->p2p) {
1767                /* STATION mode */
1768                if (priv->bss_params.operational_rate_set & ~0xF) {
1769                        pr_debug("[STA] STA has ERP rates\n");
1770                        /* G or BG mode */
1771                        arg.internalTxRate = (__ffs(
1772                        priv->bss_params.operational_rate_set & ~0xF));
1773                } else {
1774                        pr_debug("[STA] STA has non ERP rates\n");
1775                        /* B only mode */
1776                        arg.internalTxRate = (__ffs(le32_to_cpu(priv->association_mode.basic_rate_set)));
1777                }
1778                arg.nonErpInternalTxRate = (__ffs(le32_to_cpu(priv->association_mode.basic_rate_set)));
1779        } else {
1780                /* P2P mode */
1781                arg.internalTxRate = (__ffs(priv->bss_params.operational_rate_set & ~0xF));
1782                arg.nonErpInternalTxRate = (__ffs(priv->bss_params.operational_rate_set & ~0xF));
1783        }
1784
1785        pr_debug("[STA] BTCOEX_INFO MODE %d, internalTxRate : %x, nonErpInternalTxRate: %x\n",
1786                 priv->mode,
1787                 arg.internalTxRate,
1788                 arg.nonErpInternalTxRate);
1789
1790        ret = wsm_write_mib(priv, WSM_MIB_ID_OVERRIDE_INTERNAL_TX_RATE,
1791                            &arg, sizeof(arg));
1792
1793        return ret;
1794}
1795
1796void cw1200_bss_info_changed(struct ieee80211_hw *dev,
1797                             struct ieee80211_vif *vif,
1798                             struct ieee80211_bss_conf *info,
1799                             u32 changed)
1800{
1801        struct cw1200_common *priv = dev->priv;
1802        bool do_join = false;
1803
1804        mutex_lock(&priv->conf_mutex);
1805
1806        pr_debug("BSS CHANGED:  %08x\n", changed);
1807
1808        /* TODO: BSS_CHANGED_QOS */
1809        /* TODO: BSS_CHANGED_TXPOWER */
1810
1811        if (changed & BSS_CHANGED_ARP_FILTER) {
1812                struct wsm_mib_arp_ipv4_filter filter = {0};
1813                int i;
1814
1815                pr_debug("[STA] BSS_CHANGED_ARP_FILTER cnt: %d\n",
1816                         info->arp_addr_cnt);
1817
1818                /* Currently only one IP address is supported by firmware.
1819                 * In case of more IPs arp filtering will be disabled.
1820                 */
1821                if (info->arp_addr_cnt > 0 &&
1822                    info->arp_addr_cnt <= WSM_MAX_ARP_IP_ADDRTABLE_ENTRIES) {
1823                        for (i = 0; i < info->arp_addr_cnt; i++) {
1824                                filter.ipv4addrs[i] = info->arp_addr_list[i];
1825                                pr_debug("[STA] addr[%d]: 0x%X\n",
1826                                         i, filter.ipv4addrs[i]);
1827                        }
1828                        filter.enable = __cpu_to_le32(1);
1829                }
1830
1831                pr_debug("[STA] arp ip filter enable: %d\n",
1832                         __le32_to_cpu(filter.enable));
1833
1834                wsm_set_arp_ipv4_filter(priv, &filter);
1835        }
1836
1837        if (changed &
1838            (BSS_CHANGED_BEACON |
1839             BSS_CHANGED_AP_PROBE_RESP |
1840             BSS_CHANGED_BSSID |
1841             BSS_CHANGED_SSID |
1842             BSS_CHANGED_IBSS)) {
1843                pr_debug("BSS_CHANGED_BEACON\n");
1844                priv->beacon_int = info->beacon_int;
1845                cw1200_update_beaconing(priv);
1846                cw1200_upload_beacon(priv);
1847        }
1848
1849        if (changed & BSS_CHANGED_BEACON_ENABLED) {
1850                pr_debug("BSS_CHANGED_BEACON_ENABLED (%d)\n", info->enable_beacon);
1851
1852                if (priv->enable_beacon != info->enable_beacon) {
1853                        cw1200_enable_beaconing(priv, info->enable_beacon);
1854                        priv->enable_beacon = info->enable_beacon;
1855                }
1856        }
1857
1858        if (changed & BSS_CHANGED_BEACON_INT) {
1859                pr_debug("CHANGED_BEACON_INT\n");
1860                if (info->ibss_joined)
1861                        do_join = true;
1862                else if (priv->join_status == CW1200_JOIN_STATUS_AP)
1863                        cw1200_update_beaconing(priv);
1864        }
1865
1866        /* assoc/disassoc, or maybe AID changed */
1867        if (changed & BSS_CHANGED_ASSOC) {
1868                wsm_lock_tx(priv);
1869                priv->wep_default_key_id = -1;
1870                wsm_unlock_tx(priv);
1871        }
1872
1873        if (changed & BSS_CHANGED_BSSID) {
1874                pr_debug("BSS_CHANGED_BSSID\n");
1875                do_join = true;
1876        }
1877
1878        if (changed &
1879            (BSS_CHANGED_ASSOC |
1880             BSS_CHANGED_BSSID |
1881             BSS_CHANGED_IBSS |
1882             BSS_CHANGED_BASIC_RATES |
1883             BSS_CHANGED_HT)) {
1884                pr_debug("BSS_CHANGED_ASSOC\n");
1885                if (info->assoc) {
1886                        if (priv->join_status < CW1200_JOIN_STATUS_PRE_STA) {
1887                                ieee80211_connection_loss(vif);
1888                                mutex_unlock(&priv->conf_mutex);
1889                                return;
1890                        } else if (priv->join_status == CW1200_JOIN_STATUS_PRE_STA) {
1891                                priv->join_status = CW1200_JOIN_STATUS_STA;
1892                        }
1893                } else {
1894                        do_join = true;
1895                }
1896
1897                if (info->assoc || info->ibss_joined) {
1898                        struct ieee80211_sta *sta = NULL;
1899                        __le32 htprot = 0;
1900
1901                        if (info->dtim_period)
1902                                priv->join_dtim_period = info->dtim_period;
1903                        priv->beacon_int = info->beacon_int;
1904
1905                        rcu_read_lock();
1906
1907                        if (info->bssid && !info->ibss_joined)
1908                                sta = ieee80211_find_sta(vif, info->bssid);
1909                        if (sta) {
1910                                priv->ht_info.ht_cap = sta->ht_cap;
1911                                priv->bss_params.operational_rate_set =
1912                                        cw1200_rate_mask_to_wsm(priv,
1913                                                                sta->supp_rates[priv->channel->band]);
1914                                priv->ht_info.channel_type = cfg80211_get_chandef_type(&dev->conf.chandef);
1915                                priv->ht_info.operation_mode = info->ht_operation_mode;
1916                        } else {
1917                                memset(&priv->ht_info, 0,
1918                                       sizeof(priv->ht_info));
1919                                priv->bss_params.operational_rate_set = -1;
1920                        }
1921                        rcu_read_unlock();
1922
1923                        /* Non Greenfield stations present */
1924                        if (priv->ht_info.operation_mode &
1925                            IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT)
1926                                htprot |= cpu_to_le32(WSM_NON_GREENFIELD_STA_PRESENT);
1927
1928                        /* Set HT protection method */
1929                        htprot |= cpu_to_le32((priv->ht_info.operation_mode & IEEE80211_HT_OP_MODE_PROTECTION) << 2);
1930
1931                        /* TODO:
1932                         * STBC_param.dual_cts
1933                         *  STBC_param.LSIG_TXOP_FILL
1934                         */
1935
1936                        wsm_write_mib(priv, WSM_MIB_ID_SET_HT_PROTECTION,
1937                                      &htprot, sizeof(htprot));
1938
1939                        priv->association_mode.greenfield =
1940                                cw1200_ht_greenfield(&priv->ht_info);
1941                        priv->association_mode.flags =
1942                                WSM_ASSOCIATION_MODE_SNOOP_ASSOC_FRAMES |
1943                                WSM_ASSOCIATION_MODE_USE_PREAMBLE_TYPE |
1944                                WSM_ASSOCIATION_MODE_USE_HT_MODE |
1945                                WSM_ASSOCIATION_MODE_USE_BASIC_RATE_SET |
1946                                WSM_ASSOCIATION_MODE_USE_MPDU_START_SPACING;
1947                        priv->association_mode.preamble =
1948                                info->use_short_preamble ?
1949                                WSM_JOIN_PREAMBLE_SHORT :
1950                                WSM_JOIN_PREAMBLE_LONG;
1951                        priv->association_mode.basic_rate_set = __cpu_to_le32(
1952                                cw1200_rate_mask_to_wsm(priv,
1953                                                        info->basic_rates));
1954                        priv->association_mode.mpdu_start_spacing =
1955                                cw1200_ht_ampdu_density(&priv->ht_info);
1956
1957                        cw1200_cqm_bssloss_sm(priv, 0, 0, 0);
1958                        cancel_work_sync(&priv->unjoin_work);
1959
1960                        priv->bss_params.beacon_lost_count = priv->cqm_beacon_loss_count;
1961                        priv->bss_params.aid = info->aid;
1962
1963                        if (priv->join_dtim_period < 1)
1964                                priv->join_dtim_period = 1;
1965
1966                        pr_debug("[STA] DTIM %d, interval: %d\n",
1967                                 priv->join_dtim_period, priv->beacon_int);
1968                        pr_debug("[STA] Preamble: %d, Greenfield: %d, Aid: %d, Rates: 0x%.8X, Basic: 0x%.8X\n",
1969                                 priv->association_mode.preamble,
1970                                 priv->association_mode.greenfield,
1971                                 priv->bss_params.aid,
1972                                 priv->bss_params.operational_rate_set,
1973                                 priv->association_mode.basic_rate_set);
1974                        wsm_set_association_mode(priv, &priv->association_mode);
1975
1976                        if (!info->ibss_joined) {
1977                                wsm_keep_alive_period(priv, 30 /* sec */);
1978                                wsm_set_bss_params(priv, &priv->bss_params);
1979                                priv->setbssparams_done = true;
1980                                cw1200_set_beacon_wakeup_period_work(&priv->set_beacon_wakeup_period_work);
1981                                cw1200_set_pm(priv, &priv->powersave_mode);
1982                        }
1983                        if (priv->vif->p2p) {
1984                                pr_debug("[STA] Setting p2p powersave configuration.\n");
1985                                wsm_set_p2p_ps_modeinfo(priv,
1986                                                        &priv->p2p_ps_modeinfo);
1987                        }
1988                        if (priv->bt_present)
1989                                cw1200_set_btcoexinfo(priv);
1990                } else {
1991                        memset(&priv->association_mode, 0,
1992                               sizeof(priv->association_mode));
1993                        memset(&priv->bss_params, 0, sizeof(priv->bss_params));
1994                }
1995        }
1996
1997        /* ERP Protection */
1998        if (changed & (BSS_CHANGED_ASSOC |
1999                       BSS_CHANGED_ERP_CTS_PROT |
2000                       BSS_CHANGED_ERP_PREAMBLE)) {
2001                u32 prev_erp_info = priv->erp_info;
2002                if (info->use_cts_prot)
2003                        priv->erp_info |= WLAN_ERP_USE_PROTECTION;
2004                else if (!(prev_erp_info & WLAN_ERP_NON_ERP_PRESENT))
2005                        priv->erp_info &= ~WLAN_ERP_USE_PROTECTION;
2006
2007                if (info->use_short_preamble)
2008                        priv->erp_info |= WLAN_ERP_BARKER_PREAMBLE;
2009                else
2010                        priv->erp_info &= ~WLAN_ERP_BARKER_PREAMBLE;
2011
2012                pr_debug("[STA] ERP Protection: %x\n", priv->erp_info);
2013
2014                if (prev_erp_info != priv->erp_info)
2015                        queue_work(priv->workqueue, &priv->set_cts_work);
2016        }
2017
2018        /* ERP Slottime */
2019        if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_SLOT)) {
2020                __le32 slot_time = info->use_short_slot ?
2021                        __cpu_to_le32(9) : __cpu_to_le32(20);
2022                pr_debug("[STA] Slot time: %d us.\n",
2023                         __le32_to_cpu(slot_time));
2024                wsm_write_mib(priv, WSM_MIB_ID_DOT11_SLOT_TIME,
2025                              &slot_time, sizeof(slot_time));
2026        }
2027
2028        if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_CQM)) {
2029                struct wsm_rcpi_rssi_threshold threshold = {
2030                        .rollingAverageCount = 8,
2031                };
2032                pr_debug("[CQM] RSSI threshold subscribe: %d +- %d\n",
2033                         info->cqm_rssi_thold, info->cqm_rssi_hyst);
2034                priv->cqm_rssi_thold = info->cqm_rssi_thold;
2035                priv->cqm_rssi_hyst = info->cqm_rssi_hyst;
2036
2037                if (info->cqm_rssi_thold || info->cqm_rssi_hyst) {
2038                        /* RSSI subscription enabled */
2039                        /* TODO: It's not a correct way of setting threshold.
2040                         * Upper and lower must be set equal here and adjusted
2041                         * in callback. However current implementation is much
2042                         * more relaible and stable.
2043                         */
2044
2045                        /* RSSI: signed Q8.0, RCPI: unsigned Q7.1
2046                         * RSSI = RCPI / 2 - 110
2047                         */
2048                        if (priv->cqm_use_rssi) {
2049                                threshold.upperThreshold =
2050                                        info->cqm_rssi_thold + info->cqm_rssi_hyst;
2051                                threshold.lowerThreshold =
2052                                        info->cqm_rssi_thold;
2053                                threshold.rssiRcpiMode |= WSM_RCPI_RSSI_USE_RSSI;
2054                        } else {
2055                                threshold.upperThreshold = (info->cqm_rssi_thold + info->cqm_rssi_hyst + 110) * 2;
2056                                threshold.lowerThreshold = (info->cqm_rssi_thold + 110) * 2;
2057                        }
2058                        threshold.rssiRcpiMode |= WSM_RCPI_RSSI_THRESHOLD_ENABLE;
2059                } else {
2060                        /* There is a bug in FW, see sta.c. We have to enable
2061                         * dummy subscription to get correct RSSI values.
2062                         */
2063                        threshold.rssiRcpiMode |=
2064                                WSM_RCPI_RSSI_THRESHOLD_ENABLE |
2065                                WSM_RCPI_RSSI_DONT_USE_UPPER |
2066                                WSM_RCPI_RSSI_DONT_USE_LOWER;
2067                        if (priv->cqm_use_rssi)
2068                                threshold.rssiRcpiMode |= WSM_RCPI_RSSI_USE_RSSI;
2069                }
2070                wsm_set_rcpi_rssi_threshold(priv, &threshold);
2071        }
2072        mutex_unlock(&priv->conf_mutex);
2073
2074        if (do_join) {
2075                wsm_lock_tx(priv);
2076                cw1200_do_join(priv); /* Will unlock it for us */
2077        }
2078}
2079
2080void cw1200_multicast_start_work(struct work_struct *work)
2081{
2082        struct cw1200_common *priv =
2083                container_of(work, struct cw1200_common, multicast_start_work);
2084        long tmo = priv->join_dtim_period *
2085                        (priv->beacon_int + 20) * HZ / 1024;
2086
2087        cancel_work_sync(&priv->multicast_stop_work);
2088
2089        if (!priv->aid0_bit_set) {
2090                wsm_lock_tx(priv);
2091                cw1200_set_tim_impl(priv, true);
2092                priv->aid0_bit_set = true;
2093                mod_timer(&priv->mcast_timeout, jiffies + tmo);
2094                wsm_unlock_tx(priv);
2095        }
2096}
2097
2098void cw1200_multicast_stop_work(struct work_struct *work)
2099{
2100        struct cw1200_common *priv =
2101                container_of(work, struct cw1200_common, multicast_stop_work);
2102
2103        if (priv->aid0_bit_set) {
2104                del_timer_sync(&priv->mcast_timeout);
2105                wsm_lock_tx(priv);
2106                priv->aid0_bit_set = false;
2107                cw1200_set_tim_impl(priv, false);
2108                wsm_unlock_tx(priv);
2109        }
2110}
2111
2112void cw1200_mcast_timeout(struct timer_list *t)
2113{
2114        struct cw1200_common *priv = from_timer(priv, t, mcast_timeout);
2115
2116        wiphy_warn(priv->hw->wiphy,
2117                   "Multicast delivery timeout.\n");
2118        spin_lock_bh(&priv->ps_state_lock);
2119        priv->tx_multicast = priv->aid0_bit_set &&
2120                        priv->buffered_multicasts;
2121        if (priv->tx_multicast)
2122                cw1200_bh_wakeup(priv);
2123        spin_unlock_bh(&priv->ps_state_lock);
2124}
2125
2126int cw1200_ampdu_action(struct ieee80211_hw *hw,
2127                        struct ieee80211_vif *vif,
2128                        struct ieee80211_ampdu_params *params)
2129{
2130        /* Aggregation is implemented fully in firmware,
2131         * including block ack negotiation. Do not allow
2132         * mac80211 stack to do anything: it interferes with
2133         * the firmware.
2134         */
2135
2136        /* Note that we still need this function stubbed. */
2137        return -ENOTSUPP;
2138}
2139
2140/* ******************************************************************** */
2141/* WSM callback                                                         */
2142void cw1200_suspend_resume(struct cw1200_common *priv,
2143                          struct wsm_suspend_resume *arg)
2144{
2145        pr_debug("[AP] %s: %s\n",
2146                 arg->stop ? "stop" : "start",
2147                 arg->multicast ? "broadcast" : "unicast");
2148
2149        if (arg->multicast) {
2150                bool cancel_tmo = false;
2151                spin_lock_bh(&priv->ps_state_lock);
2152                if (arg->stop) {
2153                        priv->tx_multicast = false;
2154                } else {
2155                        /* Firmware sends this indication every DTIM if there
2156                         * is a STA in powersave connected. There is no reason
2157                         * to suspend, following wakeup will consume much more
2158                         * power than it could be saved.
2159                         */
2160                        cw1200_pm_stay_awake(&priv->pm_state,
2161                                             priv->join_dtim_period *
2162                                             (priv->beacon_int + 20) * HZ / 1024);
2163                        priv->tx_multicast = (priv->aid0_bit_set &&
2164                                              priv->buffered_multicasts);
2165                        if (priv->tx_multicast) {
2166                                cancel_tmo = true;
2167                                cw1200_bh_wakeup(priv);
2168                        }
2169                }
2170                spin_unlock_bh(&priv->ps_state_lock);
2171                if (cancel_tmo)
2172                        del_timer_sync(&priv->mcast_timeout);
2173        } else {
2174                spin_lock_bh(&priv->ps_state_lock);
2175                cw1200_ps_notify(priv, arg->link_id, arg->stop);
2176                spin_unlock_bh(&priv->ps_state_lock);
2177                if (!arg->stop)
2178                        cw1200_bh_wakeup(priv);
2179        }
2180        return;
2181}
2182
2183/* ******************************************************************** */
2184/* AP privates                                                          */
2185
2186static int cw1200_upload_beacon(struct cw1200_common *priv)
2187{
2188        int ret = 0;
2189        struct ieee80211_mgmt *mgmt;
2190        struct wsm_template_frame frame = {
2191                .frame_type = WSM_FRAME_TYPE_BEACON,
2192        };
2193
2194        u16 tim_offset;
2195        u16 tim_len;
2196
2197        if (priv->mode == NL80211_IFTYPE_STATION ||
2198            priv->mode == NL80211_IFTYPE_MONITOR ||
2199            priv->mode == NL80211_IFTYPE_UNSPECIFIED)
2200                goto done;
2201
2202        if (priv->vif->p2p)
2203                frame.rate = WSM_TRANSMIT_RATE_6;
2204
2205        frame.skb = ieee80211_beacon_get_tim(priv->hw, priv->vif,
2206                                             &tim_offset, &tim_len);
2207        if (!frame.skb)
2208                return -ENOMEM;
2209
2210        ret = wsm_set_template_frame(priv, &frame);
2211
2212        if (ret)
2213                goto done;
2214
2215        /* TODO: Distill probe resp; remove TIM
2216         * and any other beacon-specific IEs
2217         */
2218        mgmt = (void *)frame.skb->data;
2219        mgmt->frame_control =
2220                __cpu_to_le16(IEEE80211_FTYPE_MGMT |
2221                              IEEE80211_STYPE_PROBE_RESP);
2222
2223        frame.frame_type = WSM_FRAME_TYPE_PROBE_RESPONSE;
2224        if (priv->vif->p2p) {
2225                ret = wsm_set_probe_responder(priv, true);
2226        } else {
2227                ret = wsm_set_template_frame(priv, &frame);
2228                wsm_set_probe_responder(priv, false);
2229        }
2230
2231done:
2232        dev_kfree_skb(frame.skb);
2233
2234        return ret;
2235}
2236
2237static int cw1200_upload_pspoll(struct cw1200_common *priv)
2238{
2239        int ret = 0;
2240        struct wsm_template_frame frame = {
2241                .frame_type = WSM_FRAME_TYPE_PS_POLL,
2242                .rate = 0xFF,
2243        };
2244
2245
2246        frame.skb = ieee80211_pspoll_get(priv->hw, priv->vif);
2247        if (!frame.skb)
2248                return -ENOMEM;
2249
2250        ret = wsm_set_template_frame(priv, &frame);
2251
2252        dev_kfree_skb(frame.skb);
2253
2254        return ret;
2255}
2256
2257static int cw1200_upload_null(struct cw1200_common *priv)
2258{
2259        int ret = 0;
2260        struct wsm_template_frame frame = {
2261                .frame_type = WSM_FRAME_TYPE_NULL,
2262                .rate = 0xFF,
2263        };
2264
2265        frame.skb = ieee80211_nullfunc_get(priv->hw, priv->vif, false);
2266        if (!frame.skb)
2267                return -ENOMEM;
2268
2269        ret = wsm_set_template_frame(priv, &frame);
2270
2271        dev_kfree_skb(frame.skb);
2272
2273        return ret;
2274}
2275
2276static int cw1200_upload_qosnull(struct cw1200_common *priv)
2277{
2278        /* TODO:  This needs to be implemented
2279
2280        struct wsm_template_frame frame = {
2281                .frame_type = WSM_FRAME_TYPE_QOS_NULL,
2282                .rate = 0xFF,
2283        };
2284
2285        frame.skb = ieee80211_qosnullfunc_get(priv->hw, priv->vif);
2286        if (!frame.skb)
2287                return -ENOMEM;
2288
2289        ret = wsm_set_template_frame(priv, &frame);
2290
2291        dev_kfree_skb(frame.skb);
2292
2293        */
2294        return 0;
2295}
2296
2297static int cw1200_enable_beaconing(struct cw1200_common *priv,
2298                                   bool enable)
2299{
2300        struct wsm_beacon_transmit transmit = {
2301                .enable_beaconing = enable,
2302        };
2303
2304        return wsm_beacon_transmit(priv, &transmit);
2305}
2306
2307static int cw1200_start_ap(struct cw1200_common *priv)
2308{
2309        int ret;
2310        struct ieee80211_bss_conf *conf = &priv->vif->bss_conf;
2311        struct wsm_start start = {
2312                .mode = priv->vif->p2p ?
2313                                WSM_START_MODE_P2P_GO : WSM_START_MODE_AP,
2314                .band = (priv->channel->band == NL80211_BAND_5GHZ) ?
2315                                WSM_PHY_BAND_5G : WSM_PHY_BAND_2_4G,
2316                .channel_number = priv->channel->hw_value,
2317                .beacon_interval = conf->beacon_int,
2318                .dtim_period = conf->dtim_period,
2319                .preamble = conf->use_short_preamble ?
2320                                WSM_JOIN_PREAMBLE_SHORT :
2321                                WSM_JOIN_PREAMBLE_LONG,
2322                .probe_delay = 100,
2323                .basic_rate_set = cw1200_rate_mask_to_wsm(priv,
2324                                conf->basic_rates),
2325        };
2326        struct wsm_operational_mode mode = {
2327                .power_mode = cw1200_power_mode,
2328                .disable_more_flag_usage = true,
2329        };
2330
2331        memset(start.ssid, 0, sizeof(start.ssid));
2332        if (!conf->hidden_ssid) {
2333                start.ssid_len = conf->ssid_len;
2334                memcpy(start.ssid, conf->ssid, start.ssid_len);
2335        }
2336
2337        priv->beacon_int = conf->beacon_int;
2338        priv->join_dtim_period = conf->dtim_period;
2339
2340        memset(&priv->link_id_db, 0, sizeof(priv->link_id_db));
2341
2342        pr_debug("[AP] ch: %d(%d), bcn: %d(%d), brt: 0x%.8X, ssid: %.*s.\n",
2343                 start.channel_number, start.band,
2344                 start.beacon_interval, start.dtim_period,
2345                 start.basic_rate_set,
2346                 start.ssid_len, start.ssid);
2347        ret = wsm_start(priv, &start);
2348        if (!ret)
2349                ret = cw1200_upload_keys(priv);
2350        if (!ret && priv->vif->p2p) {
2351                pr_debug("[AP] Setting p2p powersave configuration.\n");
2352                wsm_set_p2p_ps_modeinfo(priv, &priv->p2p_ps_modeinfo);
2353        }
2354        if (!ret) {
2355                wsm_set_block_ack_policy(priv, 0, 0);
2356                priv->join_status = CW1200_JOIN_STATUS_AP;
2357                cw1200_update_filtering(priv);
2358        }
2359        wsm_set_operational_mode(priv, &mode);
2360        return ret;
2361}
2362
2363static int cw1200_update_beaconing(struct cw1200_common *priv)
2364{
2365        struct ieee80211_bss_conf *conf = &priv->vif->bss_conf;
2366        struct wsm_reset reset = {
2367                .link_id = 0,
2368                .reset_statistics = true,
2369        };
2370
2371        if (priv->mode == NL80211_IFTYPE_AP) {
2372                /* TODO: check if changed channel, band */
2373                if (priv->join_status != CW1200_JOIN_STATUS_AP ||
2374                    priv->beacon_int != conf->beacon_int) {
2375                        pr_debug("ap restarting\n");
2376                        wsm_lock_tx(priv);
2377                        if (priv->join_status != CW1200_JOIN_STATUS_PASSIVE)
2378                                wsm_reset(priv, &reset);
2379                        priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
2380                        cw1200_start_ap(priv);
2381                        wsm_unlock_tx(priv);
2382                } else
2383                        pr_debug("ap started join_status: %d\n",
2384                                 priv->join_status);
2385        }
2386        return 0;
2387}
2388