linux/drivers/net/wireless/broadcom/brcm80211/brcmsmac/mac80211_if.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2010 Broadcom Corporation
   3 * Copyright (c) 2013 Hauke Mehrtens <hauke@hauke-m.de>
   4 *
   5 * Permission to use, copy, modify, and/or distribute this software for any
   6 * purpose with or without fee is hereby granted, provided that the above
   7 * copyright notice and this permission notice appear in all copies.
   8 *
   9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  12 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  14 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  15 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16 */
  17
  18#define __UNDEF_NO_VERSION__
  19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20
  21#include <linux/etherdevice.h>
  22#include <linux/sched.h>
  23#include <linux/firmware.h>
  24#include <linux/interrupt.h>
  25#include <linux/module.h>
  26#include <linux/bcma/bcma.h>
  27#include <net/mac80211.h>
  28#include <defs.h>
  29#include "phy/phy_int.h"
  30#include "d11.h"
  31#include "channel.h"
  32#include "scb.h"
  33#include "pub.h"
  34#include "ucode_loader.h"
  35#include "mac80211_if.h"
  36#include "main.h"
  37#include "debug.h"
  38#include "led.h"
  39
  40#define N_TX_QUEUES     4 /* #tx queues on mac80211<->driver interface */
  41#define BRCMS_FLUSH_TIMEOUT     500 /* msec */
  42
  43/* Flags we support */
  44#define MAC_FILTERS (FIF_ALLMULTI | \
  45        FIF_FCSFAIL | \
  46        FIF_CONTROL | \
  47        FIF_OTHER_BSS | \
  48        FIF_BCN_PRBRESP_PROMISC | \
  49        FIF_PSPOLL)
  50
  51#define CHAN2GHZ(channel, freqency, chflags)  { \
  52        .band = IEEE80211_BAND_2GHZ, \
  53        .center_freq = (freqency), \
  54        .hw_value = (channel), \
  55        .flags = chflags, \
  56        .max_antenna_gain = 0, \
  57        .max_power = 19, \
  58}
  59
  60#define CHAN5GHZ(channel, chflags)  { \
  61        .band = IEEE80211_BAND_5GHZ, \
  62        .center_freq = 5000 + 5*(channel), \
  63        .hw_value = (channel), \
  64        .flags = chflags, \
  65        .max_antenna_gain = 0, \
  66        .max_power = 21, \
  67}
  68
  69#define RATE(rate100m, _flags) { \
  70        .bitrate = (rate100m), \
  71        .flags = (_flags), \
  72        .hw_value = (rate100m / 5), \
  73}
  74
  75struct firmware_hdr {
  76        __le32 offset;
  77        __le32 len;
  78        __le32 idx;
  79};
  80
  81static const char * const brcms_firmwares[MAX_FW_IMAGES] = {
  82        "brcm/bcm43xx",
  83        NULL
  84};
  85
  86static int n_adapters_found;
  87
  88MODULE_AUTHOR("Broadcom Corporation");
  89MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN driver.");
  90MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN cards");
  91MODULE_LICENSE("Dual BSD/GPL");
  92/* This needs to be adjusted when brcms_firmwares changes */
  93MODULE_FIRMWARE("brcm/bcm43xx-0.fw");
  94MODULE_FIRMWARE("brcm/bcm43xx_hdr-0.fw");
  95
  96/* recognized BCMA Core IDs */
  97static struct bcma_device_id brcms_coreid_table[] = {
  98        BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 17, BCMA_ANY_CLASS),
  99        BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 23, BCMA_ANY_CLASS),
 100        BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 24, BCMA_ANY_CLASS),
 101        {},
 102};
 103MODULE_DEVICE_TABLE(bcma, brcms_coreid_table);
 104
 105#if defined(CONFIG_BRCMDBG)
 106/*
 107 * Module parameter for setting the debug message level. Available
 108 * flags are specified by the BRCM_DL_* macros in
 109 * drivers/net/wireless/brcm80211/include/defs.h.
 110 */
 111module_param_named(debug, brcm_msg_level, uint, S_IRUGO | S_IWUSR);
 112#endif
 113
 114static struct ieee80211_channel brcms_2ghz_chantable[] = {
 115        CHAN2GHZ(1, 2412, IEEE80211_CHAN_NO_HT40MINUS),
 116        CHAN2GHZ(2, 2417, IEEE80211_CHAN_NO_HT40MINUS),
 117        CHAN2GHZ(3, 2422, IEEE80211_CHAN_NO_HT40MINUS),
 118        CHAN2GHZ(4, 2427, IEEE80211_CHAN_NO_HT40MINUS),
 119        CHAN2GHZ(5, 2432, 0),
 120        CHAN2GHZ(6, 2437, 0),
 121        CHAN2GHZ(7, 2442, 0),
 122        CHAN2GHZ(8, 2447, IEEE80211_CHAN_NO_HT40PLUS),
 123        CHAN2GHZ(9, 2452, IEEE80211_CHAN_NO_HT40PLUS),
 124        CHAN2GHZ(10, 2457, IEEE80211_CHAN_NO_HT40PLUS),
 125        CHAN2GHZ(11, 2462, IEEE80211_CHAN_NO_HT40PLUS),
 126        CHAN2GHZ(12, 2467,
 127                 IEEE80211_CHAN_NO_IR |
 128                 IEEE80211_CHAN_NO_HT40PLUS),
 129        CHAN2GHZ(13, 2472,
 130                 IEEE80211_CHAN_NO_IR |
 131                 IEEE80211_CHAN_NO_HT40PLUS),
 132        CHAN2GHZ(14, 2484,
 133                 IEEE80211_CHAN_NO_IR |
 134                 IEEE80211_CHAN_NO_HT40PLUS | IEEE80211_CHAN_NO_HT40MINUS |
 135                 IEEE80211_CHAN_NO_OFDM)
 136};
 137
 138static struct ieee80211_channel brcms_5ghz_nphy_chantable[] = {
 139        /* UNII-1 */
 140        CHAN5GHZ(36, IEEE80211_CHAN_NO_HT40MINUS),
 141        CHAN5GHZ(40, IEEE80211_CHAN_NO_HT40PLUS),
 142        CHAN5GHZ(44, IEEE80211_CHAN_NO_HT40MINUS),
 143        CHAN5GHZ(48, IEEE80211_CHAN_NO_HT40PLUS),
 144        /* UNII-2 */
 145        CHAN5GHZ(52,
 146                 IEEE80211_CHAN_RADAR |
 147                 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40MINUS),
 148        CHAN5GHZ(56,
 149                 IEEE80211_CHAN_RADAR |
 150                 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40PLUS),
 151        CHAN5GHZ(60,
 152                 IEEE80211_CHAN_RADAR |
 153                 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40MINUS),
 154        CHAN5GHZ(64,
 155                 IEEE80211_CHAN_RADAR |
 156                 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40PLUS),
 157        /* MID */
 158        CHAN5GHZ(100,
 159                 IEEE80211_CHAN_RADAR |
 160                 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40MINUS),
 161        CHAN5GHZ(104,
 162                 IEEE80211_CHAN_RADAR |
 163                 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40PLUS),
 164        CHAN5GHZ(108,
 165                 IEEE80211_CHAN_RADAR |
 166                 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40MINUS),
 167        CHAN5GHZ(112,
 168                 IEEE80211_CHAN_RADAR |
 169                 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40PLUS),
 170        CHAN5GHZ(116,
 171                 IEEE80211_CHAN_RADAR |
 172                 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40MINUS),
 173        CHAN5GHZ(120,
 174                 IEEE80211_CHAN_RADAR |
 175                 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40PLUS),
 176        CHAN5GHZ(124,
 177                 IEEE80211_CHAN_RADAR |
 178                 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40MINUS),
 179        CHAN5GHZ(128,
 180                 IEEE80211_CHAN_RADAR |
 181                 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40PLUS),
 182        CHAN5GHZ(132,
 183                 IEEE80211_CHAN_RADAR |
 184                 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40MINUS),
 185        CHAN5GHZ(136,
 186                 IEEE80211_CHAN_RADAR |
 187                 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40PLUS),
 188        CHAN5GHZ(140,
 189                 IEEE80211_CHAN_RADAR |
 190                 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40PLUS |
 191                 IEEE80211_CHAN_NO_HT40MINUS),
 192        /* UNII-3 */
 193        CHAN5GHZ(149, IEEE80211_CHAN_NO_HT40MINUS),
 194        CHAN5GHZ(153, IEEE80211_CHAN_NO_HT40PLUS),
 195        CHAN5GHZ(157, IEEE80211_CHAN_NO_HT40MINUS),
 196        CHAN5GHZ(161, IEEE80211_CHAN_NO_HT40PLUS),
 197        CHAN5GHZ(165, IEEE80211_CHAN_NO_HT40PLUS | IEEE80211_CHAN_NO_HT40MINUS)
 198};
 199
 200/*
 201 * The rate table is used for both 2.4G and 5G rates. The
 202 * latter being a subset as it does not support CCK rates.
 203 */
 204static struct ieee80211_rate legacy_ratetable[] = {
 205        RATE(10, 0),
 206        RATE(20, IEEE80211_RATE_SHORT_PREAMBLE),
 207        RATE(55, IEEE80211_RATE_SHORT_PREAMBLE),
 208        RATE(110, IEEE80211_RATE_SHORT_PREAMBLE),
 209        RATE(60, 0),
 210        RATE(90, 0),
 211        RATE(120, 0),
 212        RATE(180, 0),
 213        RATE(240, 0),
 214        RATE(360, 0),
 215        RATE(480, 0),
 216        RATE(540, 0),
 217};
 218
 219static const struct ieee80211_supported_band brcms_band_2GHz_nphy_template = {
 220        .band = IEEE80211_BAND_2GHZ,
 221        .channels = brcms_2ghz_chantable,
 222        .n_channels = ARRAY_SIZE(brcms_2ghz_chantable),
 223        .bitrates = legacy_ratetable,
 224        .n_bitrates = ARRAY_SIZE(legacy_ratetable),
 225        .ht_cap = {
 226                   /* from include/linux/ieee80211.h */
 227                   .cap = IEEE80211_HT_CAP_GRN_FLD |
 228                          IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40,
 229                   .ht_supported = true,
 230                   .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K,
 231                   .ampdu_density = AMPDU_DEF_MPDU_DENSITY,
 232                   .mcs = {
 233                           /* placeholders for now */
 234                           .rx_mask = {0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0},
 235                           .rx_highest = cpu_to_le16(500),
 236                           .tx_params = IEEE80211_HT_MCS_TX_DEFINED}
 237                   }
 238};
 239
 240static const struct ieee80211_supported_band brcms_band_5GHz_nphy_template = {
 241        .band = IEEE80211_BAND_5GHZ,
 242        .channels = brcms_5ghz_nphy_chantable,
 243        .n_channels = ARRAY_SIZE(brcms_5ghz_nphy_chantable),
 244        .bitrates = legacy_ratetable + BRCMS_LEGACY_5G_RATE_OFFSET,
 245        .n_bitrates = ARRAY_SIZE(legacy_ratetable) -
 246                        BRCMS_LEGACY_5G_RATE_OFFSET,
 247        .ht_cap = {
 248                   .cap = IEEE80211_HT_CAP_GRN_FLD | IEEE80211_HT_CAP_SGI_20 |
 249                          IEEE80211_HT_CAP_SGI_40,
 250                   .ht_supported = true,
 251                   .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K,
 252                   .ampdu_density = AMPDU_DEF_MPDU_DENSITY,
 253                   .mcs = {
 254                           /* placeholders for now */
 255                           .rx_mask = {0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0},
 256                           .rx_highest = cpu_to_le16(500),
 257                           .tx_params = IEEE80211_HT_MCS_TX_DEFINED}
 258                   }
 259};
 260
 261/* flags the given rate in rateset as requested */
 262static void brcms_set_basic_rate(struct brcm_rateset *rs, u16 rate, bool is_br)
 263{
 264        u32 i;
 265
 266        for (i = 0; i < rs->count; i++) {
 267                if (rate != (rs->rates[i] & 0x7f))
 268                        continue;
 269
 270                if (is_br)
 271                        rs->rates[i] |= BRCMS_RATE_FLAG;
 272                else
 273                        rs->rates[i] &= BRCMS_RATE_MASK;
 274                return;
 275        }
 276}
 277
 278/**
 279 * This function frees the WL per-device resources.
 280 *
 281 * This function frees resources owned by the WL device pointed to
 282 * by the wl parameter.
 283 *
 284 * precondition: can both be called locked and unlocked
 285 *
 286 */
 287static void brcms_free(struct brcms_info *wl)
 288{
 289        struct brcms_timer *t, *next;
 290
 291        /* free ucode data */
 292        if (wl->fw.fw_cnt)
 293                brcms_ucode_data_free(&wl->ucode);
 294        if (wl->irq)
 295                free_irq(wl->irq, wl);
 296
 297        /* kill dpc */
 298        tasklet_kill(&wl->tasklet);
 299
 300        if (wl->pub) {
 301                brcms_debugfs_detach(wl->pub);
 302                brcms_c_module_unregister(wl->pub, "linux", wl);
 303        }
 304
 305        /* free common resources */
 306        if (wl->wlc) {
 307                brcms_c_detach(wl->wlc);
 308                wl->wlc = NULL;
 309                wl->pub = NULL;
 310        }
 311
 312        /* virtual interface deletion is deferred so we cannot spinwait */
 313
 314        /* wait for all pending callbacks to complete */
 315        while (atomic_read(&wl->callbacks) > 0)
 316                schedule();
 317
 318        /* free timers */
 319        for (t = wl->timers; t; t = next) {
 320                next = t->next;
 321#ifdef DEBUG
 322                kfree(t->name);
 323#endif
 324                kfree(t);
 325        }
 326}
 327
 328/*
 329* called from both kernel as from this kernel module (error flow on attach)
 330* precondition: perimeter lock is not acquired.
 331*/
 332static void brcms_remove(struct bcma_device *pdev)
 333{
 334        struct ieee80211_hw *hw = bcma_get_drvdata(pdev);
 335        struct brcms_info *wl = hw->priv;
 336
 337        if (wl->wlc) {
 338                brcms_led_unregister(wl);
 339                wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, false);
 340                wiphy_rfkill_stop_polling(wl->pub->ieee_hw->wiphy);
 341                ieee80211_unregister_hw(hw);
 342        }
 343
 344        brcms_free(wl);
 345
 346        bcma_set_drvdata(pdev, NULL);
 347        ieee80211_free_hw(hw);
 348}
 349
 350/*
 351 * Precondition: Since this function is called in brcms_pci_probe() context,
 352 * no locking is required.
 353 */
 354static void brcms_release_fw(struct brcms_info *wl)
 355{
 356        int i;
 357        for (i = 0; i < MAX_FW_IMAGES; i++) {
 358                release_firmware(wl->fw.fw_bin[i]);
 359                release_firmware(wl->fw.fw_hdr[i]);
 360        }
 361}
 362
 363/*
 364 * Precondition: Since this function is called in brcms_pci_probe() context,
 365 * no locking is required.
 366 */
 367static int brcms_request_fw(struct brcms_info *wl, struct bcma_device *pdev)
 368{
 369        int status;
 370        struct device *device = &pdev->dev;
 371        char fw_name[100];
 372        int i;
 373
 374        memset(&wl->fw, 0, sizeof(struct brcms_firmware));
 375        for (i = 0; i < MAX_FW_IMAGES; i++) {
 376                if (brcms_firmwares[i] == NULL)
 377                        break;
 378                sprintf(fw_name, "%s-%d.fw", brcms_firmwares[i],
 379                        UCODE_LOADER_API_VER);
 380                status = request_firmware(&wl->fw.fw_bin[i], fw_name, device);
 381                if (status) {
 382                        wiphy_err(wl->wiphy, "%s: fail to load firmware %s\n",
 383                                  KBUILD_MODNAME, fw_name);
 384                        return status;
 385                }
 386                sprintf(fw_name, "%s_hdr-%d.fw", brcms_firmwares[i],
 387                        UCODE_LOADER_API_VER);
 388                status = request_firmware(&wl->fw.fw_hdr[i], fw_name, device);
 389                if (status) {
 390                        wiphy_err(wl->wiphy, "%s: fail to load firmware %s\n",
 391                                  KBUILD_MODNAME, fw_name);
 392                        return status;
 393                }
 394                wl->fw.hdr_num_entries[i] =
 395                    wl->fw.fw_hdr[i]->size / (sizeof(struct firmware_hdr));
 396        }
 397        wl->fw.fw_cnt = i;
 398        status = brcms_ucode_data_init(wl, &wl->ucode);
 399        brcms_release_fw(wl);
 400        return status;
 401}
 402
 403static void brcms_ops_tx(struct ieee80211_hw *hw,
 404                         struct ieee80211_tx_control *control,
 405                         struct sk_buff *skb)
 406{
 407        struct brcms_info *wl = hw->priv;
 408        struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
 409
 410        spin_lock_bh(&wl->lock);
 411        if (!wl->pub->up) {
 412                brcms_err(wl->wlc->hw->d11core, "ops->tx called while down\n");
 413                kfree_skb(skb);
 414                goto done;
 415        }
 416        if (brcms_c_sendpkt_mac80211(wl->wlc, skb, hw))
 417                tx_info->rate_driver_data[0] = control->sta;
 418 done:
 419        spin_unlock_bh(&wl->lock);
 420}
 421
 422static int brcms_ops_start(struct ieee80211_hw *hw)
 423{
 424        struct brcms_info *wl = hw->priv;
 425        bool blocked;
 426        int err;
 427
 428        if (!wl->ucode.bcm43xx_bomminor) {
 429                err = brcms_request_fw(wl, wl->wlc->hw->d11core);
 430                if (err)
 431                        return -ENOENT;
 432        }
 433
 434        ieee80211_wake_queues(hw);
 435        spin_lock_bh(&wl->lock);
 436        blocked = brcms_rfkill_set_hw_state(wl);
 437        spin_unlock_bh(&wl->lock);
 438        if (!blocked)
 439                wiphy_rfkill_stop_polling(wl->pub->ieee_hw->wiphy);
 440
 441        spin_lock_bh(&wl->lock);
 442        /* avoid acknowledging frames before a non-monitor device is added */
 443        wl->mute_tx = true;
 444
 445        if (!wl->pub->up)
 446                if (!blocked)
 447                        err = brcms_up(wl);
 448                else
 449                        err = -ERFKILL;
 450        else
 451                err = -ENODEV;
 452        spin_unlock_bh(&wl->lock);
 453
 454        if (err != 0)
 455                brcms_err(wl->wlc->hw->d11core, "%s: brcms_up() returned %d\n",
 456                          __func__, err);
 457
 458        bcma_core_pci_power_save(wl->wlc->hw->d11core->bus, true);
 459        return err;
 460}
 461
 462static void brcms_ops_stop(struct ieee80211_hw *hw)
 463{
 464        struct brcms_info *wl = hw->priv;
 465        int status;
 466
 467        ieee80211_stop_queues(hw);
 468
 469        if (wl->wlc == NULL)
 470                return;
 471
 472        spin_lock_bh(&wl->lock);
 473        status = brcms_c_chipmatch(wl->wlc->hw->d11core);
 474        spin_unlock_bh(&wl->lock);
 475        if (!status) {
 476                brcms_err(wl->wlc->hw->d11core,
 477                          "wl: brcms_ops_stop: chipmatch failed\n");
 478                return;
 479        }
 480
 481        bcma_core_pci_power_save(wl->wlc->hw->d11core->bus, false);
 482
 483        /* put driver in down state */
 484        spin_lock_bh(&wl->lock);
 485        brcms_down(wl);
 486        spin_unlock_bh(&wl->lock);
 487}
 488
 489static int
 490brcms_ops_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
 491{
 492        struct brcms_info *wl = hw->priv;
 493
 494        /* Just STA, AP and ADHOC for now */
 495        if (vif->type != NL80211_IFTYPE_STATION &&
 496            vif->type != NL80211_IFTYPE_AP &&
 497            vif->type != NL80211_IFTYPE_ADHOC) {
 498                brcms_err(wl->wlc->hw->d11core,
 499                          "%s: Attempt to add type %d, only STA, AP and AdHoc for now\n",
 500                          __func__, vif->type);
 501                return -EOPNOTSUPP;
 502        }
 503
 504        spin_lock_bh(&wl->lock);
 505        wl->mute_tx = false;
 506        brcms_c_mute(wl->wlc, false);
 507        if (vif->type == NL80211_IFTYPE_STATION)
 508                brcms_c_start_station(wl->wlc, vif->addr);
 509        else if (vif->type == NL80211_IFTYPE_AP)
 510                brcms_c_start_ap(wl->wlc, vif->addr, vif->bss_conf.bssid,
 511                                 vif->bss_conf.ssid, vif->bss_conf.ssid_len);
 512        else if (vif->type == NL80211_IFTYPE_ADHOC)
 513                brcms_c_start_adhoc(wl->wlc, vif->addr);
 514        spin_unlock_bh(&wl->lock);
 515
 516        return 0;
 517}
 518
 519static void
 520brcms_ops_remove_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
 521{
 522}
 523
 524static int brcms_ops_config(struct ieee80211_hw *hw, u32 changed)
 525{
 526        struct ieee80211_conf *conf = &hw->conf;
 527        struct brcms_info *wl = hw->priv;
 528        struct bcma_device *core = wl->wlc->hw->d11core;
 529        int err = 0;
 530        int new_int;
 531
 532        spin_lock_bh(&wl->lock);
 533        if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
 534                brcms_c_set_beacon_listen_interval(wl->wlc,
 535                                                   conf->listen_interval);
 536        }
 537        if (changed & IEEE80211_CONF_CHANGE_MONITOR)
 538                brcms_dbg_info(core, "%s: change monitor mode: %s\n",
 539                               __func__, conf->flags & IEEE80211_CONF_MONITOR ?
 540                               "true" : "false");
 541        if (changed & IEEE80211_CONF_CHANGE_PS)
 542                brcms_err(core, "%s: change power-save mode: %s (implement)\n",
 543                          __func__, conf->flags & IEEE80211_CONF_PS ?
 544                          "true" : "false");
 545
 546        if (changed & IEEE80211_CONF_CHANGE_POWER) {
 547                err = brcms_c_set_tx_power(wl->wlc, conf->power_level);
 548                if (err < 0) {
 549                        brcms_err(core, "%s: Error setting power_level\n",
 550                                  __func__);
 551                        goto config_out;
 552                }
 553                new_int = brcms_c_get_tx_power(wl->wlc);
 554                if (new_int != conf->power_level)
 555                        brcms_err(core,
 556                                  "%s: Power level req != actual, %d %d\n",
 557                                  __func__, conf->power_level,
 558                                  new_int);
 559        }
 560        if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
 561                if (conf->chandef.width == NL80211_CHAN_WIDTH_20 ||
 562                    conf->chandef.width == NL80211_CHAN_WIDTH_20_NOHT)
 563                        err = brcms_c_set_channel(wl->wlc,
 564                                                  conf->chandef.chan->hw_value);
 565                else
 566                        err = -ENOTSUPP;
 567        }
 568        if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
 569                err = brcms_c_set_rate_limit(wl->wlc,
 570                                             conf->short_frame_max_tx_count,
 571                                             conf->long_frame_max_tx_count);
 572
 573 config_out:
 574        spin_unlock_bh(&wl->lock);
 575        return err;
 576}
 577
 578static void
 579brcms_ops_bss_info_changed(struct ieee80211_hw *hw,
 580                        struct ieee80211_vif *vif,
 581                        struct ieee80211_bss_conf *info, u32 changed)
 582{
 583        struct brcms_info *wl = hw->priv;
 584        struct bcma_device *core = wl->wlc->hw->d11core;
 585
 586        if (changed & BSS_CHANGED_ASSOC) {
 587                /* association status changed (associated/disassociated)
 588                 * also implies a change in the AID.
 589                 */
 590                brcms_err(core, "%s: %s: %sassociated\n", KBUILD_MODNAME,
 591                          __func__, info->assoc ? "" : "dis");
 592                spin_lock_bh(&wl->lock);
 593                brcms_c_associate_upd(wl->wlc, info->assoc);
 594                spin_unlock_bh(&wl->lock);
 595        }
 596        if (changed & BSS_CHANGED_ERP_SLOT) {
 597                s8 val;
 598
 599                /* slot timing changed */
 600                if (info->use_short_slot)
 601                        val = 1;
 602                else
 603                        val = 0;
 604                spin_lock_bh(&wl->lock);
 605                brcms_c_set_shortslot_override(wl->wlc, val);
 606                spin_unlock_bh(&wl->lock);
 607        }
 608
 609        if (changed & BSS_CHANGED_HT) {
 610                /* 802.11n parameters changed */
 611                u16 mode = info->ht_operation_mode;
 612
 613                spin_lock_bh(&wl->lock);
 614                brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_CFG,
 615                        mode & IEEE80211_HT_OP_MODE_PROTECTION);
 616                brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_NONGF,
 617                        mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
 618                brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_OBSS,
 619                        mode & IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT);
 620                spin_unlock_bh(&wl->lock);
 621        }
 622        if (changed & BSS_CHANGED_BASIC_RATES) {
 623                struct ieee80211_supported_band *bi;
 624                u32 br_mask, i;
 625                u16 rate;
 626                struct brcm_rateset rs;
 627                int error;
 628
 629                /* retrieve the current rates */
 630                spin_lock_bh(&wl->lock);
 631                brcms_c_get_current_rateset(wl->wlc, &rs);
 632                spin_unlock_bh(&wl->lock);
 633
 634                br_mask = info->basic_rates;
 635                bi = hw->wiphy->bands[brcms_c_get_curband(wl->wlc)];
 636                for (i = 0; i < bi->n_bitrates; i++) {
 637                        /* convert to internal rate value */
 638                        rate = (bi->bitrates[i].bitrate << 1) / 10;
 639
 640                        /* set/clear basic rate flag */
 641                        brcms_set_basic_rate(&rs, rate, br_mask & 1);
 642                        br_mask >>= 1;
 643                }
 644
 645                /* update the rate set */
 646                spin_lock_bh(&wl->lock);
 647                error = brcms_c_set_rateset(wl->wlc, &rs);
 648                spin_unlock_bh(&wl->lock);
 649                if (error)
 650                        brcms_err(core, "changing basic rates failed: %d\n",
 651                                  error);
 652        }
 653        if (changed & BSS_CHANGED_BEACON_INT) {
 654                /* Beacon interval changed */
 655                spin_lock_bh(&wl->lock);
 656                brcms_c_set_beacon_period(wl->wlc, info->beacon_int);
 657                spin_unlock_bh(&wl->lock);
 658        }
 659        if (changed & BSS_CHANGED_BSSID) {
 660                /* BSSID changed, for whatever reason (IBSS and managed mode) */
 661                spin_lock_bh(&wl->lock);
 662                brcms_c_set_addrmatch(wl->wlc, RCM_BSSID_OFFSET, info->bssid);
 663                spin_unlock_bh(&wl->lock);
 664        }
 665        if (changed & BSS_CHANGED_SSID) {
 666                /* BSSID changed, for whatever reason (IBSS and managed mode) */
 667                spin_lock_bh(&wl->lock);
 668                brcms_c_set_ssid(wl->wlc, info->ssid, info->ssid_len);
 669                spin_unlock_bh(&wl->lock);
 670        }
 671        if (changed & BSS_CHANGED_BEACON) {
 672                /* Beacon data changed, retrieve new beacon (beaconing modes) */
 673                struct sk_buff *beacon;
 674                u16 tim_offset = 0;
 675
 676                spin_lock_bh(&wl->lock);
 677                beacon = ieee80211_beacon_get_tim(hw, vif, &tim_offset, NULL);
 678                brcms_c_set_new_beacon(wl->wlc, beacon, tim_offset,
 679                                       info->dtim_period);
 680                spin_unlock_bh(&wl->lock);
 681        }
 682
 683        if (changed & BSS_CHANGED_AP_PROBE_RESP) {
 684                struct sk_buff *probe_resp;
 685
 686                spin_lock_bh(&wl->lock);
 687                probe_resp = ieee80211_proberesp_get(hw, vif);
 688                brcms_c_set_new_probe_resp(wl->wlc, probe_resp);
 689                spin_unlock_bh(&wl->lock);
 690        }
 691
 692        if (changed & BSS_CHANGED_BEACON_ENABLED) {
 693                /* Beaconing should be enabled/disabled (beaconing modes) */
 694                brcms_err(core, "%s: Beacon enabled: %s\n", __func__,
 695                          info->enable_beacon ? "true" : "false");
 696                if (info->enable_beacon &&
 697                    hw->wiphy->flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) {
 698                        brcms_c_enable_probe_resp(wl->wlc, true);
 699                } else {
 700                        brcms_c_enable_probe_resp(wl->wlc, false);
 701                }
 702        }
 703
 704        if (changed & BSS_CHANGED_CQM) {
 705                /* Connection quality monitor config changed */
 706                brcms_err(core, "%s: cqm change: threshold %d, hys %d "
 707                          " (implement)\n", __func__, info->cqm_rssi_thold,
 708                          info->cqm_rssi_hyst);
 709        }
 710
 711        if (changed & BSS_CHANGED_IBSS) {
 712                /* IBSS join status changed */
 713                brcms_err(core, "%s: IBSS joined: %s (implement)\n",
 714                          __func__, info->ibss_joined ? "true" : "false");
 715        }
 716
 717        if (changed & BSS_CHANGED_ARP_FILTER) {
 718                /* Hardware ARP filter address list or state changed */
 719                brcms_err(core, "%s: arp filtering: %d addresses"
 720                          " (implement)\n", __func__, info->arp_addr_cnt);
 721        }
 722
 723        if (changed & BSS_CHANGED_QOS) {
 724                /*
 725                 * QoS for this association was enabled/disabled.
 726                 * Note that it is only ever disabled for station mode.
 727                 */
 728                brcms_err(core, "%s: qos enabled: %s (implement)\n",
 729                          __func__, info->qos ? "true" : "false");
 730        }
 731        return;
 732}
 733
 734static void
 735brcms_ops_configure_filter(struct ieee80211_hw *hw,
 736                        unsigned int changed_flags,
 737                        unsigned int *total_flags, u64 multicast)
 738{
 739        struct brcms_info *wl = hw->priv;
 740        struct bcma_device *core = wl->wlc->hw->d11core;
 741
 742        changed_flags &= MAC_FILTERS;
 743        *total_flags &= MAC_FILTERS;
 744
 745        if (changed_flags & FIF_ALLMULTI)
 746                brcms_dbg_info(core, "FIF_ALLMULTI\n");
 747        if (changed_flags & FIF_FCSFAIL)
 748                brcms_dbg_info(core, "FIF_FCSFAIL\n");
 749        if (changed_flags & FIF_CONTROL)
 750                brcms_dbg_info(core, "FIF_CONTROL\n");
 751        if (changed_flags & FIF_OTHER_BSS)
 752                brcms_dbg_info(core, "FIF_OTHER_BSS\n");
 753        if (changed_flags & FIF_PSPOLL)
 754                brcms_dbg_info(core, "FIF_PSPOLL\n");
 755        if (changed_flags & FIF_BCN_PRBRESP_PROMISC)
 756                brcms_dbg_info(core, "FIF_BCN_PRBRESP_PROMISC\n");
 757
 758        spin_lock_bh(&wl->lock);
 759        brcms_c_mac_promisc(wl->wlc, *total_flags);
 760        spin_unlock_bh(&wl->lock);
 761        return;
 762}
 763
 764static void brcms_ops_sw_scan_start(struct ieee80211_hw *hw,
 765                                    struct ieee80211_vif *vif,
 766                                    const u8 *mac_addr)
 767{
 768        struct brcms_info *wl = hw->priv;
 769        spin_lock_bh(&wl->lock);
 770        brcms_c_scan_start(wl->wlc);
 771        spin_unlock_bh(&wl->lock);
 772        return;
 773}
 774
 775static void brcms_ops_sw_scan_complete(struct ieee80211_hw *hw,
 776                                       struct ieee80211_vif *vif)
 777{
 778        struct brcms_info *wl = hw->priv;
 779        spin_lock_bh(&wl->lock);
 780        brcms_c_scan_stop(wl->wlc);
 781        spin_unlock_bh(&wl->lock);
 782        return;
 783}
 784
 785static int
 786brcms_ops_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
 787                  const struct ieee80211_tx_queue_params *params)
 788{
 789        struct brcms_info *wl = hw->priv;
 790
 791        spin_lock_bh(&wl->lock);
 792        brcms_c_wme_setparams(wl->wlc, queue, params, true);
 793        spin_unlock_bh(&wl->lock);
 794
 795        return 0;
 796}
 797
 798static int
 799brcms_ops_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 800               struct ieee80211_sta *sta)
 801{
 802        struct brcms_info *wl = hw->priv;
 803        struct scb *scb = &wl->wlc->pri_scb;
 804
 805        brcms_c_init_scb(scb);
 806
 807        wl->pub->global_ampdu = &(scb->scb_ampdu);
 808        wl->pub->global_ampdu->scb = scb;
 809        wl->pub->global_ampdu->max_pdu = 16;
 810
 811        /*
 812         * minstrel_ht initiates addBA on our behalf by calling
 813         * ieee80211_start_tx_ba_session()
 814         */
 815        return 0;
 816}
 817
 818static int
 819brcms_ops_ampdu_action(struct ieee80211_hw *hw,
 820                    struct ieee80211_vif *vif,
 821                    struct ieee80211_ampdu_params *params)
 822{
 823        struct brcms_info *wl = hw->priv;
 824        struct scb *scb = &wl->wlc->pri_scb;
 825        int status;
 826        struct ieee80211_sta *sta = params->sta;
 827        enum ieee80211_ampdu_mlme_action action = params->action;
 828        u16 tid = params->tid;
 829        u8 buf_size = params->buf_size;
 830
 831        if (WARN_ON(scb->magic != SCB_MAGIC))
 832                return -EIDRM;
 833        switch (action) {
 834        case IEEE80211_AMPDU_RX_START:
 835                break;
 836        case IEEE80211_AMPDU_RX_STOP:
 837                break;
 838        case IEEE80211_AMPDU_TX_START:
 839                spin_lock_bh(&wl->lock);
 840                status = brcms_c_aggregatable(wl->wlc, tid);
 841                spin_unlock_bh(&wl->lock);
 842                if (!status) {
 843                        brcms_err(wl->wlc->hw->d11core,
 844                                  "START: tid %d is not agg\'able\n", tid);
 845                        return -EINVAL;
 846                }
 847                ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
 848                break;
 849
 850        case IEEE80211_AMPDU_TX_STOP_CONT:
 851        case IEEE80211_AMPDU_TX_STOP_FLUSH:
 852        case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
 853                spin_lock_bh(&wl->lock);
 854                brcms_c_ampdu_flush(wl->wlc, sta, tid);
 855                spin_unlock_bh(&wl->lock);
 856                ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
 857                break;
 858        case IEEE80211_AMPDU_TX_OPERATIONAL:
 859                /*
 860                 * BA window size from ADDBA response ('buf_size') defines how
 861                 * many outstanding MPDUs are allowed for the BA stream by
 862                 * recipient and traffic class. 'ampdu_factor' gives maximum
 863                 * AMPDU size.
 864                 */
 865                spin_lock_bh(&wl->lock);
 866                brcms_c_ampdu_tx_operational(wl->wlc, tid, buf_size,
 867                        (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
 868                         sta->ht_cap.ampdu_factor)) - 1);
 869                spin_unlock_bh(&wl->lock);
 870                /* Power save wakeup */
 871                break;
 872        default:
 873                brcms_err(wl->wlc->hw->d11core,
 874                          "%s: Invalid command, ignoring\n", __func__);
 875        }
 876
 877        return 0;
 878}
 879
 880static void brcms_ops_rfkill_poll(struct ieee80211_hw *hw)
 881{
 882        struct brcms_info *wl = hw->priv;
 883        bool blocked;
 884
 885        spin_lock_bh(&wl->lock);
 886        blocked = brcms_c_check_radio_disabled(wl->wlc);
 887        spin_unlock_bh(&wl->lock);
 888
 889        wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, blocked);
 890}
 891
 892static bool brcms_tx_flush_completed(struct brcms_info *wl)
 893{
 894        bool result;
 895
 896        spin_lock_bh(&wl->lock);
 897        result = brcms_c_tx_flush_completed(wl->wlc);
 898        spin_unlock_bh(&wl->lock);
 899        return result;
 900}
 901
 902static void brcms_ops_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 903                            u32 queues, bool drop)
 904{
 905        struct brcms_info *wl = hw->priv;
 906        int ret;
 907
 908        no_printk("%s: drop = %s\n", __func__, drop ? "true" : "false");
 909
 910        ret = wait_event_timeout(wl->tx_flush_wq,
 911                                 brcms_tx_flush_completed(wl),
 912                                 msecs_to_jiffies(BRCMS_FLUSH_TIMEOUT));
 913
 914        brcms_dbg_mac80211(wl->wlc->hw->d11core,
 915                           "ret=%d\n", jiffies_to_msecs(ret));
 916}
 917
 918static u64 brcms_ops_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
 919{
 920        struct brcms_info *wl = hw->priv;
 921        u64 tsf;
 922
 923        spin_lock_bh(&wl->lock);
 924        tsf = brcms_c_tsf_get(wl->wlc);
 925        spin_unlock_bh(&wl->lock);
 926
 927        return tsf;
 928}
 929
 930static void brcms_ops_set_tsf(struct ieee80211_hw *hw,
 931                           struct ieee80211_vif *vif, u64 tsf)
 932{
 933        struct brcms_info *wl = hw->priv;
 934
 935        spin_lock_bh(&wl->lock);
 936        brcms_c_tsf_set(wl->wlc, tsf);
 937        spin_unlock_bh(&wl->lock);
 938}
 939
 940static const struct ieee80211_ops brcms_ops = {
 941        .tx = brcms_ops_tx,
 942        .start = brcms_ops_start,
 943        .stop = brcms_ops_stop,
 944        .add_interface = brcms_ops_add_interface,
 945        .remove_interface = brcms_ops_remove_interface,
 946        .config = brcms_ops_config,
 947        .bss_info_changed = brcms_ops_bss_info_changed,
 948        .configure_filter = brcms_ops_configure_filter,
 949        .sw_scan_start = brcms_ops_sw_scan_start,
 950        .sw_scan_complete = brcms_ops_sw_scan_complete,
 951        .conf_tx = brcms_ops_conf_tx,
 952        .sta_add = brcms_ops_sta_add,
 953        .ampdu_action = brcms_ops_ampdu_action,
 954        .rfkill_poll = brcms_ops_rfkill_poll,
 955        .flush = brcms_ops_flush,
 956        .get_tsf = brcms_ops_get_tsf,
 957        .set_tsf = brcms_ops_set_tsf,
 958};
 959
 960void brcms_dpc(unsigned long data)
 961{
 962        struct brcms_info *wl;
 963
 964        wl = (struct brcms_info *) data;
 965
 966        spin_lock_bh(&wl->lock);
 967
 968        /* call the common second level interrupt handler */
 969        if (wl->pub->up) {
 970                if (wl->resched) {
 971                        unsigned long flags;
 972
 973                        spin_lock_irqsave(&wl->isr_lock, flags);
 974                        brcms_c_intrsupd(wl->wlc);
 975                        spin_unlock_irqrestore(&wl->isr_lock, flags);
 976                }
 977
 978                wl->resched = brcms_c_dpc(wl->wlc, true);
 979        }
 980
 981        /* brcms_c_dpc() may bring the driver down */
 982        if (!wl->pub->up)
 983                goto done;
 984
 985        /* re-schedule dpc */
 986        if (wl->resched)
 987                tasklet_schedule(&wl->tasklet);
 988        else
 989                /* re-enable interrupts */
 990                brcms_intrson(wl);
 991
 992 done:
 993        spin_unlock_bh(&wl->lock);
 994        wake_up(&wl->tx_flush_wq);
 995}
 996
 997static irqreturn_t brcms_isr(int irq, void *dev_id)
 998{
 999        struct brcms_info *wl;
1000        irqreturn_t ret = IRQ_NONE;
1001
1002        wl = (struct brcms_info *) dev_id;
1003
1004        spin_lock(&wl->isr_lock);
1005
1006        /* call common first level interrupt handler */
1007        if (brcms_c_isr(wl->wlc)) {
1008                /* schedule second level handler */
1009                tasklet_schedule(&wl->tasklet);
1010                ret = IRQ_HANDLED;
1011        }
1012
1013        spin_unlock(&wl->isr_lock);
1014
1015        return ret;
1016}
1017
1018/*
1019 * is called in brcms_pci_probe() context, therefore no locking required.
1020 */
1021static int ieee_hw_rate_init(struct ieee80211_hw *hw)
1022{
1023        struct brcms_info *wl = hw->priv;
1024        struct brcms_c_info *wlc = wl->wlc;
1025        struct ieee80211_supported_band *band;
1026        int has_5g = 0;
1027        u16 phy_type;
1028
1029        hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
1030        hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
1031
1032        phy_type = brcms_c_get_phy_type(wl->wlc, 0);
1033        if (phy_type == PHY_TYPE_N || phy_type == PHY_TYPE_LCN) {
1034                band = &wlc->bandstate[BAND_2G_INDEX]->band;
1035                *band = brcms_band_2GHz_nphy_template;
1036                if (phy_type == PHY_TYPE_LCN) {
1037                        /* Single stream */
1038                        band->ht_cap.mcs.rx_mask[1] = 0;
1039                        band->ht_cap.mcs.rx_highest = cpu_to_le16(72);
1040                }
1041                hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
1042        } else {
1043                return -EPERM;
1044        }
1045
1046        /* Assume all bands use the same phy.  True for 11n devices. */
1047        if (wl->pub->_nbands > 1) {
1048                has_5g++;
1049                if (phy_type == PHY_TYPE_N || phy_type == PHY_TYPE_LCN) {
1050                        band = &wlc->bandstate[BAND_5G_INDEX]->band;
1051                        *band = brcms_band_5GHz_nphy_template;
1052                        hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
1053                } else {
1054                        return -EPERM;
1055                }
1056        }
1057        return 0;
1058}
1059
1060/*
1061 * is called in brcms_pci_probe() context, therefore no locking required.
1062 */
1063static int ieee_hw_init(struct ieee80211_hw *hw)
1064{
1065        ieee80211_hw_set(hw, AMPDU_AGGREGATION);
1066        ieee80211_hw_set(hw, SIGNAL_DBM);
1067        ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
1068
1069        hw->extra_tx_headroom = brcms_c_get_header_len();
1070        hw->queues = N_TX_QUEUES;
1071        hw->max_rates = 2;      /* Primary rate and 1 fallback rate */
1072
1073        /* channel change time is dependent on chip and band  */
1074        hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1075                                     BIT(NL80211_IFTYPE_AP) |
1076                                     BIT(NL80211_IFTYPE_ADHOC);
1077
1078        /*
1079         * deactivate sending probe responses by ucude, because this will
1080         * cause problems when WPS is used.
1081         *
1082         * hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
1083         */
1084
1085        hw->rate_control_algorithm = "minstrel_ht";
1086
1087        hw->sta_data_size = 0;
1088        return ieee_hw_rate_init(hw);
1089}
1090
1091/**
1092 * attach to the WL device.
1093 *
1094 * Attach to the WL device identified by vendor and device parameters.
1095 * regs is a host accessible memory address pointing to WL device registers.
1096 *
1097 * is called in brcms_bcma_probe() context, therefore no locking required.
1098 */
1099static struct brcms_info *brcms_attach(struct bcma_device *pdev)
1100{
1101        struct brcms_info *wl = NULL;
1102        int unit, err;
1103        struct ieee80211_hw *hw;
1104        u8 perm[ETH_ALEN];
1105
1106        unit = n_adapters_found;
1107        err = 0;
1108
1109        if (unit < 0)
1110                return NULL;
1111
1112        /* allocate private info */
1113        hw = bcma_get_drvdata(pdev);
1114        if (hw != NULL)
1115                wl = hw->priv;
1116        if (WARN_ON(hw == NULL) || WARN_ON(wl == NULL))
1117                return NULL;
1118        wl->wiphy = hw->wiphy;
1119
1120        atomic_set(&wl->callbacks, 0);
1121
1122        init_waitqueue_head(&wl->tx_flush_wq);
1123
1124        /* setup the bottom half handler */
1125        tasklet_init(&wl->tasklet, brcms_dpc, (unsigned long) wl);
1126
1127        spin_lock_init(&wl->lock);
1128        spin_lock_init(&wl->isr_lock);
1129
1130        /* common load-time initialization */
1131        wl->wlc = brcms_c_attach((void *)wl, pdev, unit, false, &err);
1132        if (!wl->wlc) {
1133                wiphy_err(wl->wiphy, "%s: attach() failed with code %d\n",
1134                          KBUILD_MODNAME, err);
1135                goto fail;
1136        }
1137        wl->pub = brcms_c_pub(wl->wlc);
1138
1139        wl->pub->ieee_hw = hw;
1140
1141        /* register our interrupt handler */
1142        if (request_irq(pdev->irq, brcms_isr,
1143                        IRQF_SHARED, KBUILD_MODNAME, wl)) {
1144                wiphy_err(wl->wiphy, "wl%d: request_irq() failed\n", unit);
1145                goto fail;
1146        }
1147        wl->irq = pdev->irq;
1148
1149        /* register module */
1150        brcms_c_module_register(wl->pub, "linux", wl, NULL);
1151
1152        if (ieee_hw_init(hw)) {
1153                wiphy_err(wl->wiphy, "wl%d: %s: ieee_hw_init failed!\n", unit,
1154                          __func__);
1155                goto fail;
1156        }
1157
1158        brcms_c_regd_init(wl->wlc);
1159
1160        memcpy(perm, &wl->pub->cur_etheraddr, ETH_ALEN);
1161        if (WARN_ON(!is_valid_ether_addr(perm)))
1162                goto fail;
1163        SET_IEEE80211_PERM_ADDR(hw, perm);
1164
1165        err = ieee80211_register_hw(hw);
1166        if (err)
1167                wiphy_err(wl->wiphy, "%s: ieee80211_register_hw failed, status"
1168                          "%d\n", __func__, err);
1169
1170        if (wl->pub->srom_ccode[0] &&
1171            regulatory_hint(wl->wiphy, wl->pub->srom_ccode))
1172                wiphy_err(wl->wiphy, "%s: regulatory hint failed\n", __func__);
1173
1174        brcms_debugfs_attach(wl->pub);
1175        brcms_debugfs_create_files(wl->pub);
1176        n_adapters_found++;
1177        return wl;
1178
1179fail:
1180        brcms_free(wl);
1181        return NULL;
1182}
1183
1184
1185
1186/**
1187 * determines if a device is a WL device, and if so, attaches it.
1188 *
1189 * This function determines if a device pointed to by pdev is a WL device,
1190 * and if so, performs a brcms_attach() on it.
1191 *
1192 * Perimeter lock is initialized in the course of this function.
1193 */
1194static int brcms_bcma_probe(struct bcma_device *pdev)
1195{
1196        struct brcms_info *wl;
1197        struct ieee80211_hw *hw;
1198
1199        dev_info(&pdev->dev, "mfg %x core %x rev %d class %d irq %d\n",
1200                 pdev->id.manuf, pdev->id.id, pdev->id.rev, pdev->id.class,
1201                 pdev->irq);
1202
1203        if ((pdev->id.manuf != BCMA_MANUF_BCM) ||
1204            (pdev->id.id != BCMA_CORE_80211))
1205                return -ENODEV;
1206
1207        hw = ieee80211_alloc_hw(sizeof(struct brcms_info), &brcms_ops);
1208        if (!hw) {
1209                pr_err("%s: ieee80211_alloc_hw failed\n", __func__);
1210                return -ENOMEM;
1211        }
1212
1213        SET_IEEE80211_DEV(hw, &pdev->dev);
1214
1215        bcma_set_drvdata(pdev, hw);
1216
1217        memset(hw->priv, 0, sizeof(*wl));
1218
1219        wl = brcms_attach(pdev);
1220        if (!wl) {
1221                pr_err("%s: brcms_attach failed!\n", __func__);
1222                return -ENODEV;
1223        }
1224        brcms_led_register(wl);
1225
1226        return 0;
1227}
1228
1229static int brcms_suspend(struct bcma_device *pdev)
1230{
1231        struct brcms_info *wl;
1232        struct ieee80211_hw *hw;
1233
1234        hw = bcma_get_drvdata(pdev);
1235        wl = hw->priv;
1236        if (!wl) {
1237                pr_err("%s: %s: no driver private struct!\n", KBUILD_MODNAME,
1238                       __func__);
1239                return -ENODEV;
1240        }
1241
1242        /* only need to flag hw is down for proper resume */
1243        spin_lock_bh(&wl->lock);
1244        wl->pub->hw_up = false;
1245        spin_unlock_bh(&wl->lock);
1246
1247        brcms_dbg_info(wl->wlc->hw->d11core, "brcms_suspend ok\n");
1248
1249        return 0;
1250}
1251
1252static int brcms_resume(struct bcma_device *pdev)
1253{
1254        return 0;
1255}
1256
1257static struct bcma_driver brcms_bcma_driver = {
1258        .name     = KBUILD_MODNAME,
1259        .probe    = brcms_bcma_probe,
1260        .suspend  = brcms_suspend,
1261        .resume   = brcms_resume,
1262        .remove   = brcms_remove,
1263        .id_table = brcms_coreid_table,
1264};
1265
1266/**
1267 * This is the main entry point for the brcmsmac driver.
1268 *
1269 * This function is scheduled upon module initialization and
1270 * does the driver registration, which result in brcms_bcma_probe()
1271 * call resulting in the driver bringup.
1272 */
1273static void brcms_driver_init(struct work_struct *work)
1274{
1275        int error;
1276
1277        error = bcma_driver_register(&brcms_bcma_driver);
1278        if (error)
1279                pr_err("%s: register returned %d\n", __func__, error);
1280}
1281
1282static DECLARE_WORK(brcms_driver_work, brcms_driver_init);
1283
1284static int __init brcms_module_init(void)
1285{
1286        brcms_debugfs_init();
1287        if (!schedule_work(&brcms_driver_work))
1288                return -EBUSY;
1289
1290        return 0;
1291}
1292
1293/**
1294 * This function unloads the brcmsmac driver from the system.
1295 *
1296 * This function unconditionally unloads the brcmsmac driver module from the
1297 * system.
1298 *
1299 */
1300static void __exit brcms_module_exit(void)
1301{
1302        cancel_work_sync(&brcms_driver_work);
1303        bcma_driver_unregister(&brcms_bcma_driver);
1304        brcms_debugfs_exit();
1305}
1306
1307module_init(brcms_module_init);
1308module_exit(brcms_module_exit);
1309
1310/*
1311 * precondition: perimeter lock has been acquired
1312 */
1313void brcms_txflowcontrol(struct brcms_info *wl, struct brcms_if *wlif,
1314                         bool state, int prio)
1315{
1316        brcms_err(wl->wlc->hw->d11core, "Shouldn't be here %s\n", __func__);
1317}
1318
1319/*
1320 * precondition: perimeter lock has been acquired
1321 */
1322void brcms_init(struct brcms_info *wl)
1323{
1324        brcms_dbg_info(wl->wlc->hw->d11core, "Initializing wl%d\n",
1325                       wl->pub->unit);
1326        brcms_reset(wl);
1327        brcms_c_init(wl->wlc, wl->mute_tx);
1328}
1329
1330/*
1331 * precondition: perimeter lock has been acquired
1332 */
1333uint brcms_reset(struct brcms_info *wl)
1334{
1335        brcms_dbg_info(wl->wlc->hw->d11core, "Resetting wl%d\n", wl->pub->unit);
1336        brcms_c_reset(wl->wlc);
1337
1338        /* dpc will not be rescheduled */
1339        wl->resched = false;
1340
1341        /* inform publicly that interface is down */
1342        wl->pub->up = false;
1343
1344        return 0;
1345}
1346
1347void brcms_fatal_error(struct brcms_info *wl)
1348{
1349        brcms_err(wl->wlc->hw->d11core, "wl%d: fatal error, reinitializing\n",
1350                  wl->wlc->pub->unit);
1351        brcms_reset(wl);
1352        ieee80211_restart_hw(wl->pub->ieee_hw);
1353}
1354
1355/*
1356 * These are interrupt on/off entry points. Disable interrupts
1357 * during interrupt state transition.
1358 */
1359void brcms_intrson(struct brcms_info *wl)
1360{
1361        unsigned long flags;
1362
1363        spin_lock_irqsave(&wl->isr_lock, flags);
1364        brcms_c_intrson(wl->wlc);
1365        spin_unlock_irqrestore(&wl->isr_lock, flags);
1366}
1367
1368u32 brcms_intrsoff(struct brcms_info *wl)
1369{
1370        unsigned long flags;
1371        u32 status;
1372
1373        spin_lock_irqsave(&wl->isr_lock, flags);
1374        status = brcms_c_intrsoff(wl->wlc);
1375        spin_unlock_irqrestore(&wl->isr_lock, flags);
1376        return status;
1377}
1378
1379void brcms_intrsrestore(struct brcms_info *wl, u32 macintmask)
1380{
1381        unsigned long flags;
1382
1383        spin_lock_irqsave(&wl->isr_lock, flags);
1384        brcms_c_intrsrestore(wl->wlc, macintmask);
1385        spin_unlock_irqrestore(&wl->isr_lock, flags);
1386}
1387
1388/*
1389 * precondition: perimeter lock has been acquired
1390 */
1391int brcms_up(struct brcms_info *wl)
1392{
1393        int error = 0;
1394
1395        if (wl->pub->up)
1396                return 0;
1397
1398        error = brcms_c_up(wl->wlc);
1399
1400        return error;
1401}
1402
1403/*
1404 * precondition: perimeter lock has been acquired
1405 */
1406void brcms_down(struct brcms_info *wl)
1407{
1408        uint callbacks, ret_val = 0;
1409
1410        /* call common down function */
1411        ret_val = brcms_c_down(wl->wlc);
1412        callbacks = atomic_read(&wl->callbacks) - ret_val;
1413
1414        /* wait for down callbacks to complete */
1415        spin_unlock_bh(&wl->lock);
1416
1417        /* For HIGH_only driver, it's important to actually schedule other work,
1418         * not just spin wait since everything runs at schedule level
1419         */
1420        SPINWAIT((atomic_read(&wl->callbacks) > callbacks), 100 * 1000);
1421
1422        spin_lock_bh(&wl->lock);
1423}
1424
1425/*
1426* precondition: perimeter lock is not acquired
1427 */
1428static void _brcms_timer(struct work_struct *work)
1429{
1430        struct brcms_timer *t = container_of(work, struct brcms_timer,
1431                                             dly_wrk.work);
1432
1433        spin_lock_bh(&t->wl->lock);
1434
1435        if (t->set) {
1436                if (t->periodic) {
1437                        atomic_inc(&t->wl->callbacks);
1438                        ieee80211_queue_delayed_work(t->wl->pub->ieee_hw,
1439                                                     &t->dly_wrk,
1440                                                     msecs_to_jiffies(t->ms));
1441                } else {
1442                        t->set = false;
1443                }
1444
1445                t->fn(t->arg);
1446        }
1447
1448        atomic_dec(&t->wl->callbacks);
1449
1450        spin_unlock_bh(&t->wl->lock);
1451}
1452
1453/*
1454 * Adds a timer to the list. Caller supplies a timer function.
1455 * Is called from wlc.
1456 *
1457 * precondition: perimeter lock has been acquired
1458 */
1459struct brcms_timer *brcms_init_timer(struct brcms_info *wl,
1460                                     void (*fn) (void *arg),
1461                                     void *arg, const char *name)
1462{
1463        struct brcms_timer *t;
1464
1465        t = kzalloc(sizeof(struct brcms_timer), GFP_ATOMIC);
1466        if (!t)
1467                return NULL;
1468
1469        INIT_DELAYED_WORK(&t->dly_wrk, _brcms_timer);
1470        t->wl = wl;
1471        t->fn = fn;
1472        t->arg = arg;
1473        t->next = wl->timers;
1474        wl->timers = t;
1475
1476#ifdef DEBUG
1477        t->name = kstrdup(name, GFP_ATOMIC);
1478#endif
1479
1480        return t;
1481}
1482
1483/*
1484 * adds only the kernel timer since it's going to be more accurate
1485 * as well as it's easier to make it periodic
1486 *
1487 * precondition: perimeter lock has been acquired
1488 */
1489void brcms_add_timer(struct brcms_timer *t, uint ms, int periodic)
1490{
1491        struct ieee80211_hw *hw = t->wl->pub->ieee_hw;
1492
1493#ifdef DEBUG
1494        if (t->set)
1495                brcms_dbg_info(t->wl->wlc->hw->d11core,
1496                               "%s: Already set. Name: %s, per %d\n",
1497                               __func__, t->name, periodic);
1498#endif
1499        t->ms = ms;
1500        t->periodic = (bool) periodic;
1501        if (!t->set) {
1502                t->set = true;
1503                atomic_inc(&t->wl->callbacks);
1504        }
1505
1506        ieee80211_queue_delayed_work(hw, &t->dly_wrk, msecs_to_jiffies(ms));
1507}
1508
1509/*
1510 * return true if timer successfully deleted, false if still pending
1511 *
1512 * precondition: perimeter lock has been acquired
1513 */
1514bool brcms_del_timer(struct brcms_timer *t)
1515{
1516        if (t->set) {
1517                t->set = false;
1518                if (!cancel_delayed_work(&t->dly_wrk))
1519                        return false;
1520
1521                atomic_dec(&t->wl->callbacks);
1522        }
1523
1524        return true;
1525}
1526
1527/*
1528 * precondition: perimeter lock has been acquired
1529 */
1530void brcms_free_timer(struct brcms_timer *t)
1531{
1532        struct brcms_info *wl = t->wl;
1533        struct brcms_timer *tmp;
1534
1535        /* delete the timer in case it is active */
1536        brcms_del_timer(t);
1537
1538        if (wl->timers == t) {
1539                wl->timers = wl->timers->next;
1540#ifdef DEBUG
1541                kfree(t->name);
1542#endif
1543                kfree(t);
1544                return;
1545
1546        }
1547
1548        tmp = wl->timers;
1549        while (tmp) {
1550                if (tmp->next == t) {
1551                        tmp->next = t->next;
1552#ifdef DEBUG
1553                        kfree(t->name);
1554#endif
1555                        kfree(t);
1556                        return;
1557                }
1558                tmp = tmp->next;
1559        }
1560
1561}
1562
1563/*
1564 * precondition: perimeter lock has been acquired
1565 */
1566int brcms_ucode_init_buf(struct brcms_info *wl, void **pbuf, u32 idx)
1567{
1568        int i, entry;
1569        const u8 *pdata;
1570        struct firmware_hdr *hdr;
1571        for (i = 0; i < wl->fw.fw_cnt; i++) {
1572                hdr = (struct firmware_hdr *)wl->fw.fw_hdr[i]->data;
1573                for (entry = 0; entry < wl->fw.hdr_num_entries[i];
1574                     entry++, hdr++) {
1575                        u32 len = le32_to_cpu(hdr->len);
1576                        if (le32_to_cpu(hdr->idx) == idx) {
1577                                pdata = wl->fw.fw_bin[i]->data +
1578                                        le32_to_cpu(hdr->offset);
1579                                *pbuf = kmemdup(pdata, len, GFP_ATOMIC);
1580                                if (*pbuf == NULL)
1581                                        goto fail;
1582
1583                                return 0;
1584                        }
1585                }
1586        }
1587        brcms_err(wl->wlc->hw->d11core,
1588                  "ERROR: ucode buf tag:%d can not be found!\n", idx);
1589        *pbuf = NULL;
1590fail:
1591        return -ENODATA;
1592}
1593
1594/*
1595 * Precondition: Since this function is called in brcms_bcma_probe() context,
1596 * no locking is required.
1597 */
1598int brcms_ucode_init_uint(struct brcms_info *wl, size_t *n_bytes, u32 idx)
1599{
1600        int i, entry;
1601        const u8 *pdata;
1602        struct firmware_hdr *hdr;
1603        for (i = 0; i < wl->fw.fw_cnt; i++) {
1604                hdr = (struct firmware_hdr *)wl->fw.fw_hdr[i]->data;
1605                for (entry = 0; entry < wl->fw.hdr_num_entries[i];
1606                     entry++, hdr++) {
1607                        if (le32_to_cpu(hdr->idx) == idx) {
1608                                pdata = wl->fw.fw_bin[i]->data +
1609                                        le32_to_cpu(hdr->offset);
1610                                if (le32_to_cpu(hdr->len) != 4) {
1611                                        brcms_err(wl->wlc->hw->d11core,
1612                                                  "ERROR: fw hdr len\n");
1613                                        return -ENOMSG;
1614                                }
1615                                *n_bytes = le32_to_cpu(*((__le32 *) pdata));
1616                                return 0;
1617                        }
1618                }
1619        }
1620        brcms_err(wl->wlc->hw->d11core,
1621                  "ERROR: ucode tag:%d can not be found!\n", idx);
1622        return -ENOMSG;
1623}
1624
1625/*
1626 * precondition: can both be called locked and unlocked
1627 */
1628void brcms_ucode_free_buf(void *p)
1629{
1630        kfree(p);
1631}
1632
1633/*
1634 * checks validity of all firmware images loaded from user space
1635 *
1636 * Precondition: Since this function is called in brcms_bcma_probe() context,
1637 * no locking is required.
1638 */
1639int brcms_check_firmwares(struct brcms_info *wl)
1640{
1641        int i;
1642        int entry;
1643        int rc = 0;
1644        const struct firmware *fw;
1645        const struct firmware *fw_hdr;
1646        struct firmware_hdr *ucode_hdr;
1647        for (i = 0; i < MAX_FW_IMAGES && rc == 0; i++) {
1648                fw =  wl->fw.fw_bin[i];
1649                fw_hdr = wl->fw.fw_hdr[i];
1650                if (fw == NULL && fw_hdr == NULL) {
1651                        break;
1652                } else if (fw == NULL || fw_hdr == NULL) {
1653                        wiphy_err(wl->wiphy, "%s: invalid bin/hdr fw\n",
1654                                  __func__);
1655                        rc = -EBADF;
1656                } else if (fw_hdr->size % sizeof(struct firmware_hdr)) {
1657                        wiphy_err(wl->wiphy, "%s: non integral fw hdr file "
1658                                "size %zu/%zu\n", __func__, fw_hdr->size,
1659                                sizeof(struct firmware_hdr));
1660                        rc = -EBADF;
1661                } else if (fw->size < MIN_FW_SIZE || fw->size > MAX_FW_SIZE) {
1662                        wiphy_err(wl->wiphy, "%s: out of bounds fw file size %zu\n",
1663                                  __func__, fw->size);
1664                        rc = -EBADF;
1665                } else {
1666                        /* check if ucode section overruns firmware image */
1667                        ucode_hdr = (struct firmware_hdr *)fw_hdr->data;
1668                        for (entry = 0; entry < wl->fw.hdr_num_entries[i] &&
1669                             !rc; entry++, ucode_hdr++) {
1670                                if (le32_to_cpu(ucode_hdr->offset) +
1671                                    le32_to_cpu(ucode_hdr->len) >
1672                                    fw->size) {
1673                                        wiphy_err(wl->wiphy,
1674                                                  "%s: conflicting bin/hdr\n",
1675                                                  __func__);
1676                                        rc = -EBADF;
1677                                }
1678                        }
1679                }
1680        }
1681        if (rc == 0 && wl->fw.fw_cnt != i) {
1682                wiphy_err(wl->wiphy, "%s: invalid fw_cnt=%d\n", __func__,
1683                        wl->fw.fw_cnt);
1684                rc = -EBADF;
1685        }
1686        return rc;
1687}
1688
1689/*
1690 * precondition: perimeter lock has been acquired
1691 */
1692bool brcms_rfkill_set_hw_state(struct brcms_info *wl)
1693{
1694        bool blocked = brcms_c_check_radio_disabled(wl->wlc);
1695
1696        spin_unlock_bh(&wl->lock);
1697        wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, blocked);
1698        if (blocked)
1699                wiphy_rfkill_start_polling(wl->pub->ieee_hw->wiphy);
1700        spin_lock_bh(&wl->lock);
1701        return blocked;
1702}
1703